Annotate

libs/adhoc.lib.lua @ 409:171c4a8cc3de

plugins.archive: Update to XEP-0313 v0.6
author Kim Alvefur <zash@zash.se>
date Fri, 07 Apr 2017 19:38:52 +0200
parent 383:72be3c2cf602
child 457:73d4eb93657b
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
383
72be3c2cf602 libs.adhoc: Don't set unintentional globals
Kim Alvefur <zash@zash.se>
parents: 198
diff changeset
15 local function _cmdtag(desc, status, sessionid, action)
116
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);
383
72be3c2cf602 libs.adhoc: Don't set unintentional globals
Kim Alvefur <zash@zash.se>
parents: 198
diff changeset
38 local cmdtag;
116
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 if data.status == "completed" then
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 states[sessionid] = nil;
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 cmdtag = command:cmdtag("completed", sessionid);
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 elseif data.status == "canceled" then
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 states[sessionid] = nil;
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 cmdtag = command:cmdtag("canceled", sessionid);
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 elseif data.status == "error" then
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 states[sessionid] = nil;
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 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
48 origin.send(stanza);
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 return true;
198
f6702e7ce5f9 libs/adhoc.lib.lua: Update copy (ugh) from Prosody
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
50 else
116
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 cmdtag = command:cmdtag("executing", sessionid);
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 end
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 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
55 if name == "info" then
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 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
57 elseif name == "warn" then
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 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
59 elseif name == "error" then
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 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
61 elseif name =="actions" then
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 local actions = st.stanza("actions");
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 for _, action in ipairs(content) do
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 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
65 actions:tag(action):up();
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 else
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 module:log("error", 'Command "'..command.name..
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 '" 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
69 end
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 end
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 cmdtag:add_child(actions);
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 elseif name == "form" then
198
f6702e7ce5f9 libs/adhoc.lib.lua: Update copy (ugh) from Prosody
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
73 cmdtag:add_child((content.layout or content):form(content.values));
116
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 elseif name == "result" then
198
f6702e7ce5f9 libs/adhoc.lib.lua: Update copy (ugh) from Prosody
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
75 cmdtag:add_child((content.layout or content):form(content.values, "result"));
116
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 elseif name == "other" then
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 cmdtag:add_child(content);
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 end
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 stanza:add_child(cmdtag);
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 origin.send(stanza);
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 return true;
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 end
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 return _M;