plugins/adhoc.lua

changeset 116
151c8cc776df
child 122
e2600454fd54
equal deleted inserted replaced
115:9f8cacfca7c7 116:151c8cc776df
1 local adhoc = require "lib.adhoc";
2
3 local xmlns_commands = "http://jabber.org/protocol/commands";
4
5 local commands = {};
6
7 function verse.plugins.adhoc(stream)
8 stream:add_disco_feature(xmlns_commands);
9
10 local function has_affiliation(jid, aff)
11 if not(aff) or aff == "user" then return true; end
12 -- TODO: Support 'roster', and callback etc.
13 end
14
15 function stream:add_adhoc_command(name, node, handler, permission)
16 commands[node] = adhoc.new(name, node, handler, permission);
17 stream:add_disco_item({ jid = stream.jid, node = node, name = name }, xmlns_commands);
18 return commands[node];
19 end
20
21 local function handle_command(stanza)
22 local command_tag = stanza.tags[1];
23 local node = command_tag.attr.node;
24
25 local handler = commands[node];
26 if not handler then return; end
27
28 if not has_affiliation(stanza.attr.from, handler.permission) then
29 stream:send(verse.error_reply(stanza, "auth", "forbidden", "You don't have permission to execute this command"):up()
30 :add_child(handler:cmdtag("canceled")
31 :tag("note", {type="error"}):text("You don't have permission to execute this command")));
32 return true
33 end
34
35 -- User has permission now execute the command
36 return adhoc.handle_cmd(handler, { send = function (d) return stream:send(d) end }, stanza);
37 end
38
39 stream:hook("iq/"..xmlns_commands, function (stanza)
40 local type = stanza.attr.type;
41 local name = stanza.tags[1].name;
42 if type == "set" and name == "command" then
43 return handle_command(stanza);
44 end
45 end);
46 end

mercurial