mod_adhoc: Fix passing data to util.dataforms
[prosody.git] / plugins / adhoc / adhoc.lib.lua
1 -- Copyright (C) 2009-2010 Florian Zeitz
2 --
3 -- This file is MIT/X11 licensed. Please see the
4 -- COPYING file in the source package for more information.
5 --
6
7 local st, uuid = require "util.stanza", require "util.uuid";
8
9 local xmlns_cmd = "http://jabber.org/protocol/commands";
10
11 local states = {}
12
13 local _M = {};
14
15 function _cmdtag(desc, status, sessionid, action)
16         local cmd = st.stanza("command", { xmlns = xmlns_cmd, node = desc.node, status = status });
17         if sessionid then cmd.attr.sessionid = sessionid; end
18         if action then cmd.attr.action = action; end
19
20         return cmd;
21 end
22
23 function _M.new(name, node, handler, permission)
24         return { name = name, node = node, handler = handler, cmdtag = _cmdtag, permission = (permission or "user") };
25 end
26
27 function _M.handle_cmd(command, origin, stanza)
28         local sessionid = stanza.tags[1].attr.sessionid or uuid.generate();
29         local dataIn = {};
30         dataIn.to = stanza.attr.to;
31         dataIn.from = stanza.attr.from;
32         dataIn.action = stanza.tags[1].attr.action or "execute";
33         dataIn.form = stanza.tags[1]:child_with_ns("jabber:x:data");
34
35         local data, state = command:handler(dataIn, states[sessionid]);
36         states[sessionid] = state;
37         local stanza = st.reply(stanza);
38         if data.status == "completed" then
39                 states[sessionid] = nil;
40                 cmdtag = command:cmdtag("completed", sessionid);
41         elseif data.status == "canceled" then
42                 states[sessionid] = nil;
43                 cmdtag = command:cmdtag("canceled", sessionid);
44         elseif data.status == "error" then
45                 states[sessionid] = nil;
46                 stanza = st.error_reply(stanza, data.error.type, data.error.condition, data.error.message);
47                 origin.send(stanza);
48                 return true;
49         else 
50                 cmdtag = command:cmdtag("executing", sessionid);
51         end
52
53         for name, content in pairs(data) do
54                 if name == "info" then
55                         cmdtag:tag("note", {type="info"}):text(content):up();
56                 elseif name == "warn" then
57                         cmdtag:tag("note", {type="warn"}):text(content):up();
58                 elseif name == "error" then
59                         cmdtag:tag("note", {type="error"}):text(content.message):up();
60                 elseif name =="actions" then
61                         local actions = st.stanza("actions");
62                         for _, action in ipairs(content) do
63                                 if (action == "prev") or (action == "next") or (action == "complete") then
64                                         actions:tag(action):up();
65                                 else
66                                         module:log("error", 'Command "'..command.name..
67                                                 '" at node "'..command.node..'" provided an invalid action "'..action..'"');
68                                 end
69                         end
70                         cmdtag:add_child(actions);
71                 elseif name == "form" then
72                         cmdtag:add_child((content.layout or content):form(content.values));
73                 elseif name == "result" then
74                         cmdtag:add_child((content.layout or content):form(content.values, "result"));
75                 elseif name == "other" then
76                         cmdtag:add_child(content);
77                 end
78         end
79         stanza:add_child(cmdtag);
80         origin.send(stanza);
81
82         return true;
83 end
84
85 return _M;