Annotate

libs/adhoc.lib.lua @ 116:151c8cc776df

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