Annotate

plugins/adhoc.lua @ 303:0dda04d5eb84

plugins.archive: Treat query params correctly
author Kim Alvefur <zash@zash.se>
date Fri, 08 Jun 2012 06:44:20 +0200
parent 250:a5ac643a7fd6
child 308:2bcc97bc5f43
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
250
a5ac643a7fd6 added local verse var to all plugins
mva <mva@mva.name>
parents: 145
diff changeset
1 local verse = require "verse";
116
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local adhoc = require "lib.adhoc";
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local xmlns_commands = "http://jabber.org/protocol/commands";
122
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
5 local xmlns_data = "jabber:x:data";
116
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6
122
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
7 local command_mt = {};
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
8 command_mt.__index = command_mt;
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
9
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
10 -- Table of commands we provide
116
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local commands = {};
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 function verse.plugins.adhoc(stream)
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 stream:add_disco_feature(xmlns_commands);
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
122
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
16 function stream:query_commands(jid, callback)
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
17 stream:disco_items(jid, xmlns_commands, function (items)
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
18 stream:debug("adhoc list returned")
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
19 local command_list = {};
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
20 for _, item in ipairs(items) do
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
21 command_list[item.node] = item.name;
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
22 end
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
23 stream:debug("adhoc calling callback")
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
24 return callback(command_list);
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
25 end);
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
26 end
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
27
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
28 function stream:execute_command(jid, command, callback)
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
29 local cmd = setmetatable({
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
30 stream = stream, jid = jid,
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
31 command = command, callback = callback
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
32 }, command_mt);
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
33 return cmd:execute();
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
34 end
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
35
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
36 -- ACL checker for commands we provide
116
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 local function has_affiliation(jid, aff)
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 if not(aff) or aff == "user" then return true; end
122
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
39 if type(aff) == "function" then
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
40 return aff(jid);
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
41 end
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
42 -- TODO: Support 'roster', etc.
116
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 end
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 function stream:add_adhoc_command(name, node, handler, permission)
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 commands[node] = adhoc.new(name, node, handler, permission);
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 stream:add_disco_item({ jid = stream.jid, node = node, name = name }, xmlns_commands);
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 return commands[node];
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 end
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 local function handle_command(stanza)
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 local command_tag = stanza.tags[1];
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 local node = command_tag.attr.node;
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 local handler = commands[node];
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 if not handler then return; end
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 if not has_affiliation(stanza.attr.from, handler.permission) then
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 stream:send(verse.error_reply(stanza, "auth", "forbidden", "You don't have permission to execute this command"):up()
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 :add_child(handler:cmdtag("canceled")
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 :tag("note", {type="error"}):text("You don't have permission to execute this command")));
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 return true
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 end
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 -- User has permission now execute the command
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 return adhoc.handle_cmd(handler, { send = function (d) return stream:send(d) end }, stanza);
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 end
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 stream:hook("iq/"..xmlns_commands, function (stanza)
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 local type = stanza.attr.type;
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 local name = stanza.tags[1].name;
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 if type == "set" and name == "command" then
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 return handle_command(stanza);
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 end
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 end);
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 end
122
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
77
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
78 function command_mt:_process_response(result)
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
79 if result.type == "error" then
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
80 self.status = "canceled";
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
81 self.callback(self, {});
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
82 end
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
83 local command = result:get_child("command", xmlns_commands);
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
84 self.status = command.attr.status;
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
85 self.sessionid = command.attr.sessionid;
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
86 self.form = command:get_child("x", xmlns_data);
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
87 self.callback(self);
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
88 end
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
89
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
90 -- Initial execution of a command
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
91 function command_mt:execute()
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
92 local iq = verse.iq({ to = self.jid, type = "set" })
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
93 :tag("command", { xmlns = xmlns_commands, node = self.command });
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
94 self.stream:send_iq(iq, function (result)
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
95 self:_process_response(result);
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
96 end);
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
97 end
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
98
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
99 function command_mt:next(form)
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
100 local iq = verse.iq({ to = self.jid, type = "set" })
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
101 :tag("command", {
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
102 xmlns = xmlns_commands,
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
103 node = self.command,
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
104 sessionid = self.sessionid
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
105 });
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
106
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
107 if form then iq:add_child(form); end
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
108
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
109 self.stream:send_iq(iq, function (result)
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
110 self:_process_response(result);
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
111 end);
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
112 end