Sat, 24 Jun 2023 09:59:07 +0200
clix.adhoc: Move stanza to dataform converter here
Removes the need for verse to have a custom util.dataforms fork only for
this
168
75e8ca131178
Update to handle Prosody module namespacing
Kim Alvefur <zash@zash.se>
parents:
126
diff
changeset
|
1 | local dataforms = require "prosody.util.dataforms"; |
108 | 2 | |
170
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
3 | local xmlns_validate = 'http://jabber.org/protocol/xdata-validate'; |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
4 | local function dataform_from_stanza(stanza) |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
5 | local layout = { |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
6 | title = stanza:get_child_text("title"); |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
7 | instructions = stanza:get_child_text("instructions"); |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
8 | }; |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
9 | for tag in stanza:childtags("field") do |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
10 | local field = { |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
11 | name = tag.attr.var; |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
12 | label = tag.attr.label; |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
13 | type = tag.attr.type; |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
14 | required = tag:get_child("required") and true or nil; |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
15 | value = tag:get_child_text("value"); |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
16 | }; |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
17 | layout[#layout+1] = field; |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
18 | if field.type then |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
19 | local value = {}; |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
20 | if field.type:match"list%-" then |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
21 | for tag in tag:childtags("option") do |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
22 | value[#value+1] = { label = tag.attr.label, value = tag:get_child_text("value") }; |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
23 | end |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
24 | for tag in tag:childtags("value") do |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
25 | value[#value+1] = { label = tag.attr.label, value = tag:get_text(), default = true }; |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
26 | end |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
27 | elseif field.type:match"%-multi" then |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
28 | for tag in tag:childtags("value") do |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
29 | value[#value+1] = tag.attr.label and { label = tag.attr.label, value = tag:get_text() } or tag:get_text(); |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
30 | end |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
31 | if field.type == "text-multi" then |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
32 | field.value = table.concat(value, "\n"); |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
33 | else |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
34 | field.value = value; |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
35 | end |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
36 | end |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
37 | end |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
38 | local datatype_tag = tag:get_child("validate", xmlns_validate); |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
39 | if datatype_tag then |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
40 | field.datatype = datatype_tag.attr.datatype; |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
41 | local range_tag = datatype_tag:get_child("range"); |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
42 | if range_tag then |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
43 | field.range_min = tonumber(range_tag.attr.min); |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
44 | field.range_max = tonumber(range_tag.attr.max); |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
45 | end |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
46 | end |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
47 | |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
48 | end |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
49 | return dataforms.new(layout); |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
50 | end |
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
51 | |
108 | 52 | -- TODO Cleanup, commit |
53 | return function (opts, arg) | |
54 | if opts.short_help then | |
55 | print("Execute an Ad-Hoc Command"); | |
56 | return; | |
57 | end | |
58 | local function on_connect(conn) | |
59 | if opts.node then | |
60 | conn:execute_command(opts.to or conn.host, opts.node, function(cmd) | |
61 | conn:info("status: %s", cmd.status); | |
62 | local note = cmd.note; | |
63 | if note then | |
64 | conn[note.attr.type or "info"](conn, note:get_text()); | |
65 | end | |
66 | if cmd.status == "executing" then | |
67 | local data = {}; | |
68 | for i=1,#arg do | |
69 | local k,v = arg[i]:match"^([^=]+)=(.*)"; | |
70 | if k and v then | |
71 | data[k] = v; --FIXME multiple | |
72 | end | |
73 | end | |
170
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
74 | local command_form_layout = dataform_from_stanza(cmd.form) |
108 | 75 | if opts.interactive then |
76 | for i=1,#command_form_layout do | |
77 | local item = command_form_layout[i]; | |
78 | if item.type ~= "hidden" and not data[item.name] then | |
79 | -- FIXME Current value isn't shown | |
80 | io.stderr:write(item.label..": "); | |
81 | if item.type:match"%-multi" then | |
82 | local t = { }; | |
83 | repeat | |
84 | local line = io.read("*l"); | |
85 | if line and line ~= "" then | |
125
75902417ff05
clix.adhoc: Improve handling of list items
Kim Alvefur <zash@zash.se>
parents:
124
diff
changeset
|
86 | if item.type:match"list%-" then |
75902417ff05
clix.adhoc: Improve handling of list items
Kim Alvefur <zash@zash.se>
parents:
124
diff
changeset
|
87 | 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
|
88 | else |
75902417ff05
clix.adhoc: Improve handling of list items
Kim Alvefur <zash@zash.se>
parents:
124
diff
changeset
|
89 | t[#t+1] = line; |
75902417ff05
clix.adhoc: Improve handling of list items
Kim Alvefur <zash@zash.se>
parents:
124
diff
changeset
|
90 | end |
108 | 91 | end |
92 | until not line or line == ""; | |
93 | if item.type == "text-multi" then | |
94 | t = table.concat(t, "\n"); | |
95 | end | |
96 | data[item.name] = t; | |
97 | --elseif item.type == "list-single" then | |
98 | --data[item.name] = { (io.read("*l")) }; | |
99 | else | |
100 | data[item.name] = io.read("*l"); | |
101 | end | |
102 | end | |
103 | end | |
104 | end | |
105 | cmd:next(command_form_layout:form(data, "submit")); | |
106 | elseif cmd.status == "completed" then | |
107 | if cmd.form then | |
170
0d561f921c13
clix.adhoc: Move stanza to dataform converter here
Kim Alvefur <zash@zash.se>
parents:
168
diff
changeset
|
108 | local command_form_layout = dataform_from_stanza(cmd.form) |
108 | 109 | 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
|
110 | if data.title then |
ff422623e0ba
clix.adhoc: Print form title and instructions if available
Kim Alvefur <zash@zash.se>
parents:
108
diff
changeset
|
111 | print("= " .. data.title .. " ="); |
ff422623e0ba
clix.adhoc: Print form title and instructions if available
Kim Alvefur <zash@zash.se>
parents:
108
diff
changeset
|
112 | print() |
ff422623e0ba
clix.adhoc: Print form title and instructions if available
Kim Alvefur <zash@zash.se>
parents:
108
diff
changeset
|
113 | end |
ff422623e0ba
clix.adhoc: Print form title and instructions if available
Kim Alvefur <zash@zash.se>
parents:
108
diff
changeset
|
114 | if data.instructions then |
ff422623e0ba
clix.adhoc: Print form title and instructions if available
Kim Alvefur <zash@zash.se>
parents:
108
diff
changeset
|
115 | print(data.instructions); |
ff422623e0ba
clix.adhoc: Print form title and instructions if available
Kim Alvefur <zash@zash.se>
parents:
108
diff
changeset
|
116 | print() |
ff422623e0ba
clix.adhoc: Print form title and instructions if available
Kim Alvefur <zash@zash.se>
parents:
108
diff
changeset
|
117 | end |
108 | 118 | for i, item in ipairs(command_form_layout) do |
119 | if item.type ~= "hidden" then | |
126
c9d03a70a936
clix.adhoc: Print field labels if available
Kim Alvefur <zash@zash.se>
parents:
125
diff
changeset
|
120 | print("== " .. (item.label or item.name) .. " ==") |
108 | 121 | print(data[item.name]); |
122 | end | |
123 | end | |
124 | ||
125 | end | |
126 | conn:close(); | |
127 | else | |
128 | conn:warn("unhandled command status: %s", tostring(cmd.status)); | |
129 | end | |
130 | end); | |
131 | else | |
132 | conn:disco_items(opts.to or conn.host, "http://jabber.org/protocol/commands", function(items) | |
133 | -- TODO It would be neat to be able to choose from this list | |
134 | if items then | |
135 | for i=1,#items do | |
136 | print(items[i].name, items[i].node); | |
137 | end | |
138 | end | |
139 | conn:close(); | |
140 | end); | |
141 | end | |
142 | end | |
143 | clix_connect(opts, on_connect, {"adhoc"}); | |
144 | end | |
145 |