Mon, 21 Mar 2016 16:11:54 +0100
clix.adhoc: Print form title and instructions if available
108 | 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); | |
124
ff422623e0ba
clix.adhoc: Print form title and instructions if available
Kim Alvefur <zash@zash.se>
parents:
108
diff
changeset
|
57 | if data.title then |
ff422623e0ba
clix.adhoc: Print form title and instructions if available
Kim Alvefur <zash@zash.se>
parents:
108
diff
changeset
|
58 | print("= " .. data.title .. " ="); |
ff422623e0ba
clix.adhoc: Print form title and instructions if available
Kim Alvefur <zash@zash.se>
parents:
108
diff
changeset
|
59 | print() |
ff422623e0ba
clix.adhoc: Print form title and instructions if available
Kim Alvefur <zash@zash.se>
parents:
108
diff
changeset
|
60 | end |
ff422623e0ba
clix.adhoc: Print form title and instructions if available
Kim Alvefur <zash@zash.se>
parents:
108
diff
changeset
|
61 | if data.instructions then |
ff422623e0ba
clix.adhoc: Print form title and instructions if available
Kim Alvefur <zash@zash.se>
parents:
108
diff
changeset
|
62 | print(data.instructions); |
ff422623e0ba
clix.adhoc: Print form title and instructions if available
Kim Alvefur <zash@zash.se>
parents:
108
diff
changeset
|
63 | print() |
ff422623e0ba
clix.adhoc: Print form title and instructions if available
Kim Alvefur <zash@zash.se>
parents:
108
diff
changeset
|
64 | end |
108 | 65 | for i, item in ipairs(command_form_layout) do |
66 | if item.type ~= "hidden" then | |
67 | print("== " .. item.name .. " ==") | |
68 | print(data[item.name]); | |
69 | end | |
70 | end | |
71 | ||
72 | end | |
73 | conn:close(); | |
74 | else | |
75 | conn:warn("unhandled command status: %s", tostring(cmd.status)); | |
76 | end | |
77 | end); | |
78 | else | |
79 | conn:disco_items(opts.to or conn.host, "http://jabber.org/protocol/commands", function(items) | |
80 | -- TODO It would be neat to be able to choose from this list | |
81 | if items then | |
82 | for i=1,#items do | |
83 | print(items[i].name, items[i].node); | |
84 | end | |
85 | end | |
86 | conn:close(); | |
87 | end); | |
88 | end | |
89 | end | |
90 | clix_connect(opts, on_connect, {"adhoc"}); | |
91 | end | |
92 |