Software /
code /
verse
Annotate
plugins/adhoc.lua @ 209:48a404e083c4
plugins.roster: Don't signal roster events if there was no such item
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 24 May 2011 20:22:02 +0200 |
parent | 145:96efa9ee8f18 |
child | 250:a5ac643a7fd6 |
rev | line source |
---|---|
116
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 local adhoc = require "lib.adhoc"; |
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 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
|
4 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
|
5 |
122
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
6 local command_mt = {}; |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
7 command_mt.__index = command_mt; |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
8 |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
9 -- Table of commands we provide |
116
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 local commands = {}; |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 function verse.plugins.adhoc(stream) |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 stream:add_disco_feature(xmlns_commands); |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 |
122
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
15 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
|
16 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
|
17 stream:debug("adhoc list returned") |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
18 local command_list = {}; |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
19 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
|
20 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
|
21 end |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
22 stream:debug("adhoc calling callback") |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
23 return callback(command_list); |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
24 end); |
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 |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
27 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
|
28 local cmd = setmetatable({ |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
29 stream = stream, jid = jid, |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
30 command = command, callback = callback |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
31 }, command_mt); |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
32 return cmd:execute(); |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
33 end |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
34 |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
35 -- 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
|
36 local function has_affiliation(jid, aff) |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 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
|
38 if type(aff) == "function" then |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
39 return aff(jid); |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
40 end |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
41 -- TODO: Support 'roster', etc. |
116
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 end |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 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
|
45 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
|
46 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
|
47 return commands[node]; |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 end |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 local function handle_command(stanza) |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 local command_tag = stanza.tags[1]; |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 local node = command_tag.attr.node; |
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 local handler = commands[node]; |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 if not handler then return; end |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 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
|
58 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
|
59 :add_child(handler:cmdtag("canceled") |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 :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
|
61 return true |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 end |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 -- 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
|
65 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
|
66 end |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 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
|
69 local type = stanza.attr.type; |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 local name = stanza.tags[1].name; |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 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
|
72 return handle_command(stanza); |
151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 end |
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 |
122
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
76 |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
77 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
|
78 if result.type == "error" then |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
79 self.status = "canceled"; |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
80 self.callback(self, {}); |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
81 end |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
82 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
|
83 self.status = command.attr.status; |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
84 self.sessionid = command.attr.sessionid; |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
85 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
|
86 self.callback(self); |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
87 end |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
88 |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
89 -- Initial execution of a command |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
90 function command_mt:execute() |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
91 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
|
92 :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
|
93 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
|
94 self:_process_response(result); |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
95 end); |
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 |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
98 function command_mt:next(form) |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
99 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
|
100 :tag("command", { |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
101 xmlns = xmlns_commands, |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
102 node = self.command, |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
103 sessionid = self.sessionid |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
104 }); |
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 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
|
107 |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
108 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
|
109 self:_process_response(result); |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
110 end); |
e2600454fd54
plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents:
116
diff
changeset
|
111 end |