clix/raw.lua

Sat, 14 Nov 2020 15:30:35 +0100

author
Kim Alvefur <zash@zash.se>
date
Sat, 14 Nov 2020 15:30:35 +0100
changeset 132
3addfb97296c
parent 85
8bc27e310e64
child 133
be3b857e991f
permissions
-rw-r--r--

clix.raw: Add a shorthand for top level stanza attributes

The m, p and iq functions are amended so that indexing them sets the
'type' attribute and a subsequent function call with a string sets the
'to' attribute.

Examples:

m.chat"recipient":body"Hello"

iq.get"example.com":query"jabber:iq:version"

19
3a0a156e0a79 clix.raw: New command to set up a raw XML in/out session with the server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 short_opts.i = "interactive";
3a0a156e0a79 clix.raw: New command to set up a raw XML in/out session with the server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 short_opts.e = "echo";
3a0a156e0a79 clix.raw: New command to set up a raw XML in/out session with the server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
3a0a156e0a79 clix.raw: New command to set up a raw XML in/out session with the server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 return function (opts, args)
27
5b58c002d6ad clix.*: Add --short-help and make sure they are working correctly
Matthew Wild <mwild1@gmail.com>
parents: 23
diff changeset
5 if opts.short_help then
5b58c002d6ad clix.*: Add --short-help and make sure they are working correctly
Matthew Wild <mwild1@gmail.com>
parents: 23
diff changeset
6 print("Send/receive raw XML to/from the server");
5b58c002d6ad clix.*: Add --short-help and make sure they are working correctly
Matthew Wild <mwild1@gmail.com>
parents: 23
diff changeset
7 return;
5b58c002d6ad clix.*: Add --short-help and make sure they are working correctly
Matthew Wild <mwild1@gmail.com>
parents: 23
diff changeset
8 end
46
b5d6e443e571 raw: If using --stdin and not --interactive then buffer all data before connecting, more reliable in a non-interactive environment
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
9
b5d6e443e571 raw: If using --stdin and not --interactive then buffer all data before connecting, more reliable in a non-interactive environment
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
10 local send_xml;
b5d6e443e571 raw: If using --stdin and not --interactive then buffer all data before connecting, more reliable in a non-interactive environment
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
11 if opts.stdin then
b5d6e443e571 raw: If using --stdin and not --interactive then buffer all data before connecting, more reliable in a non-interactive environment
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
12 send_xml = io.read("*a");
b5d6e443e571 raw: If using --stdin and not --interactive then buffer all data before connecting, more reliable in a non-interactive environment
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
13 end
b5d6e443e571 raw: If using --stdin and not --interactive then buffer all data before connecting, more reliable in a non-interactive environment
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
14
19
3a0a156e0a79 clix.raw: New command to set up a raw XML in/out session with the server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local function on_connect(conn)
83
040fadcc86f9 clix.raw: Print one incoming stanza per line, instead of raw incoming data.
Kim Alvefur <zash@zash.se>
parents: 68
diff changeset
16 local print = print;
040fadcc86f9 clix.raw: Print one incoming stanza per line, instead of raw incoming data.
Kim Alvefur <zash@zash.se>
parents: 68
diff changeset
17 local function stprint(stanza)
040fadcc86f9 clix.raw: Print one incoming stanza per line, instead of raw incoming data.
Kim Alvefur <zash@zash.se>
parents: 68
diff changeset
18 if stanza.attr.to == conn.jid then
040fadcc86f9 clix.raw: Print one incoming stanza per line, instead of raw incoming data.
Kim Alvefur <zash@zash.se>
parents: 68
diff changeset
19 stanza.attr.to = nil;
040fadcc86f9 clix.raw: Print one incoming stanza per line, instead of raw incoming data.
Kim Alvefur <zash@zash.se>
parents: 68
diff changeset
20 end
040fadcc86f9 clix.raw: Print one incoming stanza per line, instead of raw incoming data.
Kim Alvefur <zash@zash.se>
parents: 68
diff changeset
21 return print(stanza);
040fadcc86f9 clix.raw: Print one incoming stanza per line, instead of raw incoming data.
Kim Alvefur <zash@zash.se>
parents: 68
diff changeset
22 end
040fadcc86f9 clix.raw: Print one incoming stanza per line, instead of raw incoming data.
Kim Alvefur <zash@zash.se>
parents: 68
diff changeset
23 conn:hook("stanza", stprint)
46
b5d6e443e571 raw: If using --stdin and not --interactive then buffer all data before connecting, more reliable in a non-interactive environment
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
24 if opts.interactive then
19
3a0a156e0a79 clix.raw: New command to set up a raw XML in/out session with the server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 local stdin = {
3a0a156e0a79 clix.raw: New command to set up a raw XML in/out session with the server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 getfd = function () return 0; end;
3a0a156e0a79 clix.raw: New command to set up a raw XML in/out session with the server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 dirty = function (self) return false; end;
3a0a156e0a79 clix.raw: New command to set up a raw XML in/out session with the server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 settimeout = function () end;
3a0a156e0a79 clix.raw: New command to set up a raw XML in/out session with the server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 send = function (_, d) return #d, 0; end;
3a0a156e0a79 clix.raw: New command to set up a raw XML in/out session with the server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 close = function () end;
3a0a156e0a79 clix.raw: New command to set up a raw XML in/out session with the server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 receive = function (_, patt)
3a0a156e0a79 clix.raw: New command to set up a raw XML in/out session with the server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 local data = io.stdin:read(patt);
68
b7d92ed9c268 clix.raw: Add a small sandbox with util.stanza functions
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
33 if data == nil then
b7d92ed9c268 clix.raw: Add a small sandbox with util.stanza functions
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
34 conn:close();
b7d92ed9c268 clix.raw: Add a small sandbox with util.stanza functions
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
35 end
19
3a0a156e0a79 clix.raw: New command to set up a raw XML in/out session with the server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 if opts.echo then
3a0a156e0a79 clix.raw: New command to set up a raw XML in/out session with the server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 io.write(data, patt == "*l" and "\n" or "");
3a0a156e0a79 clix.raw: New command to set up a raw XML in/out session with the server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 end
3a0a156e0a79 clix.raw: New command to set up a raw XML in/out session with the server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 return data;
3a0a156e0a79 clix.raw: New command to set up a raw XML in/out session with the server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 end
3a0a156e0a79 clix.raw: New command to set up a raw XML in/out session with the server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 };
132
3addfb97296c clix.raw: Add a shorthand for top level stanza attributes
Kim Alvefur <zash@zash.se>
parents: 85
diff changeset
42 local stwrap;
3addfb97296c clix.raw: Add a shorthand for top level stanza attributes
Kim Alvefur <zash@zash.se>
parents: 85
diff changeset
43 do
3addfb97296c clix.raw: Add a shorthand for top level stanza attributes
Kim Alvefur <zash@zash.se>
parents: 85
diff changeset
44 local f_mt, r_mt = {}, {};
3addfb97296c clix.raw: Add a shorthand for top level stanza attributes
Kim Alvefur <zash@zash.se>
parents: 85
diff changeset
45 function f_mt:__call(...) if ... and type(...) == "string" then return self{ to=... } end return self._f(...) end
3addfb97296c clix.raw: Add a shorthand for top level stanza attributes
Kim Alvefur <zash@zash.se>
parents: 85
diff changeset
46 function f_mt:__index(k) return setmetatable({_st = self._f{ type = k }}, r_mt); end
3addfb97296c clix.raw: Add a shorthand for top level stanza attributes
Kim Alvefur <zash@zash.se>
parents: 85
diff changeset
47 function r_mt:__call(to) self._st.attr.to = to; return self._st end
3addfb97296c clix.raw: Add a shorthand for top level stanza attributes
Kim Alvefur <zash@zash.se>
parents: 85
diff changeset
48 function stwrap(f) return setmetatable({_f=f},f_mt) end
3addfb97296c clix.raw: Add a shorthand for top level stanza attributes
Kim Alvefur <zash@zash.se>
parents: 85
diff changeset
49 end
68
b7d92ed9c268 clix.raw: Add a small sandbox with util.stanza functions
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
50 local env = setmetatable({}, { __index = {
b7d92ed9c268 clix.raw: Add a small sandbox with util.stanza functions
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
51 s = verse.stanza,
132
3addfb97296c clix.raw: Add a shorthand for top level stanza attributes
Kim Alvefur <zash@zash.se>
parents: 85
diff changeset
52 m = stwrap(verse.message),
3addfb97296c clix.raw: Add a shorthand for top level stanza attributes
Kim Alvefur <zash@zash.se>
parents: 85
diff changeset
53 p = stwrap(verse.presence),
3addfb97296c clix.raw: Add a shorthand for top level stanza attributes
Kim Alvefur <zash@zash.se>
parents: 85
diff changeset
54 iq = stwrap(verse.iq),
85
8bc27e310e64 clix.raw: Add a ping command to the sandbox
Kim Alvefur <zash@zash.se>
parents: 84
diff changeset
55 ping = function(host)
8bc27e310e64 clix.raw: Add a ping command to the sandbox
Kim Alvefur <zash@zash.se>
parents: 84
diff changeset
56 return verse.iq{ type="get", to=host}:tag("ping", {xmlns="urn:xmpp:ping"});
8bc27e310e64 clix.raw: Add a ping command to the sandbox
Kim Alvefur <zash@zash.se>
parents: 84
diff changeset
57 end,
68
b7d92ed9c268 clix.raw: Add a small sandbox with util.stanza functions
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
58 }});
19
3a0a156e0a79 clix.raw: New command to set up a raw XML in/out session with the server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 local function on_incoming(stdin, data)
68
b7d92ed9c268 clix.raw: Add a small sandbox with util.stanza functions
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
60 if not data then
b7d92ed9c268 clix.raw: Add a small sandbox with util.stanza functions
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
61 conn:close();
b7d92ed9c268 clix.raw: Add a small sandbox with util.stanza functions
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
62 return
b7d92ed9c268 clix.raw: Add a small sandbox with util.stanza functions
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
63 end
b7d92ed9c268 clix.raw: Add a small sandbox with util.stanza functions
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
64 if data:sub(1,1) ~= "<" then
84
1b4e64176288 clix.raw: Nicer error reporting from the sandbox.
Kim Alvefur <zash@zash.se>
parents: 83
diff changeset
65 local chunk, err = loadstring("return "..data, "@stdin");
1b4e64176288 clix.raw: Nicer error reporting from the sandbox.
Kim Alvefur <zash@zash.se>
parents: 83
diff changeset
66 if not chunk then
1b4e64176288 clix.raw: Nicer error reporting from the sandbox.
Kim Alvefur <zash@zash.se>
parents: 83
diff changeset
67 conn:error(err);
1b4e64176288 clix.raw: Nicer error reporting from the sandbox.
Kim Alvefur <zash@zash.se>
parents: 83
diff changeset
68 return;
1b4e64176288 clix.raw: Nicer error reporting from the sandbox.
Kim Alvefur <zash@zash.se>
parents: 83
diff changeset
69 end
68
b7d92ed9c268 clix.raw: Add a small sandbox with util.stanza functions
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
70 data = "";
b7d92ed9c268 clix.raw: Add a small sandbox with util.stanza functions
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
71 setfenv(chunk, env);
b7d92ed9c268 clix.raw: Add a small sandbox with util.stanza functions
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
72 local ok, ret = pcall(chunk);
b7d92ed9c268 clix.raw: Add a small sandbox with util.stanza functions
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
73 if ok then
b7d92ed9c268 clix.raw: Add a small sandbox with util.stanza functions
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
74 data = ret
84
1b4e64176288 clix.raw: Nicer error reporting from the sandbox.
Kim Alvefur <zash@zash.se>
parents: 83
diff changeset
75 else
1b4e64176288 clix.raw: Nicer error reporting from the sandbox.
Kim Alvefur <zash@zash.se>
parents: 83
diff changeset
76 conn:error(ret);
1b4e64176288 clix.raw: Nicer error reporting from the sandbox.
Kim Alvefur <zash@zash.se>
parents: 83
diff changeset
77 return;
68
b7d92ed9c268 clix.raw: Add a small sandbox with util.stanza functions
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
78 end
b7d92ed9c268 clix.raw: Add a small sandbox with util.stanza functions
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
79 end
b7d92ed9c268 clix.raw: Add a small sandbox with util.stanza functions
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
80 if data then
b7d92ed9c268 clix.raw: Add a small sandbox with util.stanza functions
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
81 conn:send(data);
b7d92ed9c268 clix.raw: Add a small sandbox with util.stanza functions
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
82 end
19
3a0a156e0a79 clix.raw: New command to set up a raw XML in/out session with the server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 if not opts.interactive then
3a0a156e0a79 clix.raw: New command to set up a raw XML in/out session with the server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 conn:close();
3a0a156e0a79 clix.raw: New command to set up a raw XML in/out session with the server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 end
3a0a156e0a79 clix.raw: New command to set up a raw XML in/out session with the server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 end
3a0a156e0a79 clix.raw: New command to set up a raw XML in/out session with the server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 stdin = require "net.server".wrapclient(stdin, "stdin", 0, {
3a0a156e0a79 clix.raw: New command to set up a raw XML in/out session with the server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 onincoming = on_incoming, ondisconnect = function () end
3a0a156e0a79 clix.raw: New command to set up a raw XML in/out session with the server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 }, "*l");
3a0a156e0a79 clix.raw: New command to set up a raw XML in/out session with the server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 else
46
b5d6e443e571 raw: If using --stdin and not --interactive then buffer all data before connecting, more reliable in a non-interactive environment
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
91 if send_xml then
b5d6e443e571 raw: If using --stdin and not --interactive then buffer all data before connecting, more reliable in a non-interactive environment
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
92 conn:send(send_xml);
b5d6e443e571 raw: If using --stdin and not --interactive then buffer all data before connecting, more reliable in a non-interactive environment
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
93 else
b5d6e443e571 raw: If using --stdin and not --interactive then buffer all data before connecting, more reliable in a non-interactive environment
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
94 conn:send(table.concat(arg, " "));
b5d6e443e571 raw: If using --stdin and not --interactive then buffer all data before connecting, more reliable in a non-interactive environment
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
95 end
19
3a0a156e0a79 clix.raw: New command to set up a raw XML in/out session with the server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 conn:close();
3a0a156e0a79 clix.raw: New command to set up a raw XML in/out session with the server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 end
3a0a156e0a79 clix.raw: New command to set up a raw XML in/out session with the server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98
3a0a156e0a79 clix.raw: New command to set up a raw XML in/out session with the server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 end
3a0a156e0a79 clix.raw: New command to set up a raw XML in/out session with the server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 return clix_connect(opts, on_connect);
3a0a156e0a79 clix.raw: New command to set up a raw XML in/out session with the server
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 end

mercurial