|
1 local dataforms = require "util.dataforms"; |
|
2 |
|
3 -- TODO Cleanup, commit |
|
4 return function (opts, arg) |
|
5 if opts.short_help then |
|
6 print("Execute an Ad-Hoc Command"); |
|
7 return; |
|
8 end |
|
9 local function on_connect(conn) |
|
10 if opts.node then |
|
11 conn:execute_command(opts.to or conn.host, opts.node, function(cmd) |
|
12 conn:info("status: %s", cmd.status); |
|
13 local note = cmd.note; |
|
14 if note then |
|
15 conn[note.attr.type or "info"](conn, note:get_text()); |
|
16 end |
|
17 if cmd.status == "executing" then |
|
18 local data = {}; |
|
19 for i=1,#arg do |
|
20 local k,v = arg[i]:match"^([^=]+)=(.*)"; |
|
21 if k and v then |
|
22 data[k] = v; --FIXME multiple |
|
23 end |
|
24 end |
|
25 local command_form_layout = dataforms.from_stanza(cmd.form) |
|
26 if opts.interactive then |
|
27 for i=1,#command_form_layout do |
|
28 local item = command_form_layout[i]; |
|
29 if item.type ~= "hidden" and not data[item.name] then |
|
30 -- FIXME Current value isn't shown |
|
31 io.stderr:write(item.label..": "); |
|
32 if item.type:match"%-multi" then |
|
33 local t = { }; |
|
34 repeat |
|
35 local line = io.read("*l"); |
|
36 if line and line ~= "" then |
|
37 t[#t+1] = line; |
|
38 end |
|
39 until not line or line == ""; |
|
40 if item.type == "text-multi" then |
|
41 t = table.concat(t, "\n"); |
|
42 end |
|
43 data[item.name] = t; |
|
44 --elseif item.type == "list-single" then |
|
45 --data[item.name] = { (io.read("*l")) }; |
|
46 else |
|
47 data[item.name] = io.read("*l"); |
|
48 end |
|
49 end |
|
50 end |
|
51 end |
|
52 cmd:next(command_form_layout:form(data, "submit")); |
|
53 elseif cmd.status == "completed" then |
|
54 if cmd.form then |
|
55 local command_form_layout = dataforms.from_stanza(cmd.form) |
|
56 local data = command_form_layout:data(cmd.form); |
|
57 for i, item in ipairs(command_form_layout) do |
|
58 if item.type ~= "hidden" then |
|
59 print("== " .. item.name .. " ==") |
|
60 print(data[item.name]); |
|
61 end |
|
62 end |
|
63 |
|
64 end |
|
65 conn:close(); |
|
66 else |
|
67 conn:warn("unhandled command status: %s", tostring(cmd.status)); |
|
68 end |
|
69 end); |
|
70 else |
|
71 conn:disco_items(opts.to or conn.host, "http://jabber.org/protocol/commands", function(items) |
|
72 -- TODO It would be neat to be able to choose from this list |
|
73 if items then |
|
74 for i=1,#items do |
|
75 print(items[i].name, items[i].node); |
|
76 end |
|
77 end |
|
78 conn:close(); |
|
79 end); |
|
80 end |
|
81 end |
|
82 clix_connect(opts, on_connect, {"adhoc"}); |
|
83 end |
|
84 |