Software /
code /
verse
Annotate
plugins/adhoc.lua @ 309:6297d658769f
plugins.adhoc: Collect a <note/>
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 04 Jul 2012 03:37:16 +0200 |
parent | 308:2bcc97bc5f43 |
child | 340:0ce227f6034c |
rev | line source |
---|---|
250 | 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) |
308
2bcc97bc5f43
plugins.adhoc: Add explicit dependency on the disco plugin.
Kim Alvefur <zash@zash.se>
parents:
250
diff
changeset
|
14 stream:add_plugin("disco"); |
116
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 stream:add_disco_feature(xmlns_commands); |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 |
122
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
17 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
|
18 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
|
19 stream:debug("adhoc list returned") |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
20 local command_list = {}; |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
21 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
|
22 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
|
23 end |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
24 stream:debug("adhoc calling callback") |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
25 return callback(command_list); |
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 end |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
28 |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
29 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
|
30 local cmd = setmetatable({ |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
31 stream = stream, jid = jid, |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
32 command = command, callback = callback |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
33 }, command_mt); |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
34 return cmd:execute(); |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
35 end |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
36 |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
37 -- 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
|
38 local function has_affiliation(jid, aff) |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 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
|
40 if type(aff) == "function" then |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
41 return aff(jid); |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
42 end |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
43 -- TODO: Support 'roster', etc. |
116
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 end |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 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
|
47 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
|
48 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
|
49 return commands[node]; |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 end |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 local function handle_command(stanza) |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 local command_tag = stanza.tags[1]; |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 local node = command_tag.attr.node; |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 local handler = commands[node]; |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 if not handler then return; end |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 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
|
60 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
|
61 :add_child(handler:cmdtag("canceled") |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 :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
|
63 return true |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 end |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 -- 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
|
67 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
|
68 end |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 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
|
71 local type = stanza.attr.type; |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 local name = stanza.tags[1].name; |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 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
|
74 return handle_command(stanza); |
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); |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 end |
122
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
78 |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
79 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
|
80 if result.type == "error" then |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
81 self.status = "canceled"; |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
82 self.callback(self, {}); |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
83 end |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
84 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
|
85 self.status = command.attr.status; |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
86 self.sessionid = command.attr.sessionid; |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
87 self.form = command:get_child("x", xmlns_data); |
309
6297d658769f
plugins.adhoc: Collect a <note/>
Kim Alvefur <zash@zash.se>
parents:
308
diff
changeset
|
88 self.note = command:get_child("note"); --FIXME handle multiple <note/>s |
122
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
89 self.callback(self); |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
90 end |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
91 |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
92 -- Initial execution of a command |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
93 function command_mt:execute() |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
94 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
|
95 :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
|
96 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
|
97 self:_process_response(result); |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
98 end); |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
99 end |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
100 |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
101 function command_mt:next(form) |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
102 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
|
103 :tag("command", { |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
104 xmlns = xmlns_commands, |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
105 node = self.command, |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
106 sessionid = self.sessionid |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
107 }); |
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 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
|
110 |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
111 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
|
112 self:_process_response(result); |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
113 end); |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
114 end |