1 local dataforms = require "prosody.util.dataforms"; |
1 local dataforms = require "prosody.util.dataforms"; |
|
2 |
|
3 local xmlns_validate = 'http://jabber.org/protocol/xdata-validate'; |
|
4 local function dataform_from_stanza(stanza) |
|
5 local layout = { |
|
6 title = stanza:get_child_text("title"); |
|
7 instructions = stanza:get_child_text("instructions"); |
|
8 }; |
|
9 for tag in stanza:childtags("field") do |
|
10 local field = { |
|
11 name = tag.attr.var; |
|
12 label = tag.attr.label; |
|
13 type = tag.attr.type; |
|
14 required = tag:get_child("required") and true or nil; |
|
15 value = tag:get_child_text("value"); |
|
16 }; |
|
17 layout[#layout+1] = field; |
|
18 if field.type then |
|
19 local value = {}; |
|
20 if field.type:match"list%-" then |
|
21 for tag in tag:childtags("option") do |
|
22 value[#value+1] = { label = tag.attr.label, value = tag:get_child_text("value") }; |
|
23 end |
|
24 for tag in tag:childtags("value") do |
|
25 value[#value+1] = { label = tag.attr.label, value = tag:get_text(), default = true }; |
|
26 end |
|
27 elseif field.type:match"%-multi" then |
|
28 for tag in tag:childtags("value") do |
|
29 value[#value+1] = tag.attr.label and { label = tag.attr.label, value = tag:get_text() } or tag:get_text(); |
|
30 end |
|
31 if field.type == "text-multi" then |
|
32 field.value = table.concat(value, "\n"); |
|
33 else |
|
34 field.value = value; |
|
35 end |
|
36 end |
|
37 end |
|
38 local datatype_tag = tag:get_child("validate", xmlns_validate); |
|
39 if datatype_tag then |
|
40 field.datatype = datatype_tag.attr.datatype; |
|
41 local range_tag = datatype_tag:get_child("range"); |
|
42 if range_tag then |
|
43 field.range_min = tonumber(range_tag.attr.min); |
|
44 field.range_max = tonumber(range_tag.attr.max); |
|
45 end |
|
46 end |
|
47 |
|
48 end |
|
49 return dataforms.new(layout); |
|
50 end |
2 |
51 |
3 -- TODO Cleanup, commit |
52 -- TODO Cleanup, commit |
4 return function (opts, arg) |
53 return function (opts, arg) |
5 if opts.short_help then |
54 if opts.short_help then |
6 print("Execute an Ad-Hoc Command"); |
55 print("Execute an Ad-Hoc Command"); |
20 local k,v = arg[i]:match"^([^=]+)=(.*)"; |
69 local k,v = arg[i]:match"^([^=]+)=(.*)"; |
21 if k and v then |
70 if k and v then |
22 data[k] = v; --FIXME multiple |
71 data[k] = v; --FIXME multiple |
23 end |
72 end |
24 end |
73 end |
25 local command_form_layout = dataforms.from_stanza(cmd.form) |
74 local command_form_layout = dataform_from_stanza(cmd.form) |
26 if opts.interactive then |
75 if opts.interactive then |
27 for i=1,#command_form_layout do |
76 for i=1,#command_form_layout do |
28 local item = command_form_layout[i]; |
77 local item = command_form_layout[i]; |
29 if item.type ~= "hidden" and not data[item.name] then |
78 if item.type ~= "hidden" and not data[item.name] then |
30 -- FIXME Current value isn't shown |
79 -- FIXME Current value isn't shown |
54 end |
103 end |
55 end |
104 end |
56 cmd:next(command_form_layout:form(data, "submit")); |
105 cmd:next(command_form_layout:form(data, "submit")); |
57 elseif cmd.status == "completed" then |
106 elseif cmd.status == "completed" then |
58 if cmd.form then |
107 if cmd.form then |
59 local command_form_layout = dataforms.from_stanza(cmd.form) |
108 local command_form_layout = dataform_from_stanza(cmd.form) |
60 local data = command_form_layout:data(cmd.form); |
109 local data = command_form_layout:data(cmd.form); |
61 if data.title then |
110 if data.title then |
62 print("= " .. data.title .. " ="); |
111 print("= " .. data.title .. " ="); |
63 print() |
112 print() |
64 end |
113 end |