Mon, 08 Feb 2021 22:56:45 +0100
clix.archive: Skip printing body-less messages
These are more and more common in archives, e.g. receipts, errors, OMEMO
etc.
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 | |
125
75902417ff05
clix.adhoc: Improve handling of list items
Kim Alvefur <zash@zash.se>
parents:
124
diff
changeset
|
37 | if item.type:match"list%-" then |
75902417ff05
clix.adhoc: Improve handling of list items
Kim Alvefur <zash@zash.se>
parents:
124
diff
changeset
|
38 | t[#t+1] = { label = line, value = line, default = true }; |
75902417ff05
clix.adhoc: Improve handling of list items
Kim Alvefur <zash@zash.se>
parents:
124
diff
changeset
|
39 | else |
75902417ff05
clix.adhoc: Improve handling of list items
Kim Alvefur <zash@zash.se>
parents:
124
diff
changeset
|
40 | t[#t+1] = line; |
75902417ff05
clix.adhoc: Improve handling of list items
Kim Alvefur <zash@zash.se>
parents:
124
diff
changeset
|
41 | end |
108 | 42 | end |
43 | until not line or line == ""; | |
44 | if item.type == "text-multi" then | |
45 | t = table.concat(t, "\n"); | |
46 | end | |
47 | data[item.name] = t; | |
48 | --elseif item.type == "list-single" then | |
49 | --data[item.name] = { (io.read("*l")) }; | |
50 | else | |
51 | data[item.name] = io.read("*l"); | |
52 | end | |
53 | end | |
54 | end | |
55 | end | |
56 | cmd:next(command_form_layout:form(data, "submit")); | |
57 | elseif cmd.status == "completed" then | |
58 | if cmd.form then | |
59 | local command_form_layout = dataforms.from_stanza(cmd.form) | |
60 | 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
|
61 | if data.title then |
ff422623e0ba
clix.adhoc: Print form title and instructions if available
Kim Alvefur <zash@zash.se>
parents:
108
diff
changeset
|
62 | print("= " .. data.title .. " ="); |
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 |
ff422623e0ba
clix.adhoc: Print form title and instructions if available
Kim Alvefur <zash@zash.se>
parents:
108
diff
changeset
|
65 | if data.instructions then |
ff422623e0ba
clix.adhoc: Print form title and instructions if available
Kim Alvefur <zash@zash.se>
parents:
108
diff
changeset
|
66 | print(data.instructions); |
ff422623e0ba
clix.adhoc: Print form title and instructions if available
Kim Alvefur <zash@zash.se>
parents:
108
diff
changeset
|
67 | print() |
ff422623e0ba
clix.adhoc: Print form title and instructions if available
Kim Alvefur <zash@zash.se>
parents:
108
diff
changeset
|
68 | end |
108 | 69 | for i, item in ipairs(command_form_layout) do |
70 | if item.type ~= "hidden" then | |
126
c9d03a70a936
clix.adhoc: Print field labels if available
Kim Alvefur <zash@zash.se>
parents:
125
diff
changeset
|
71 | print("== " .. (item.label or item.name) .. " ==") |
108 | 72 | print(data[item.name]); |
73 | end | |
74 | end | |
75 | ||
76 | end | |
77 | conn:close(); | |
78 | else | |
79 | conn:warn("unhandled command status: %s", tostring(cmd.status)); | |
80 | end | |
81 | end); | |
82 | else | |
83 | conn:disco_items(opts.to or conn.host, "http://jabber.org/protocol/commands", function(items) | |
84 | -- TODO It would be neat to be able to choose from this list | |
85 | if items then | |
86 | for i=1,#items do | |
87 | print(items[i].name, items[i].node); | |
88 | end | |
89 | end | |
90 | conn:close(); | |
91 | end); | |
92 | end | |
93 | end | |
94 | clix_connect(opts, on_connect, {"adhoc"}); | |
95 | end | |
96 |