Annotate

plugins/adhoc.lua @ 116:151c8cc776df

verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
author Matthew Wild <mwild1@gmail.com>
date Thu, 26 Aug 2010 17:52:16 +0100
child 122:e2600454fd54
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 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";
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local commands = {};
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 function verse.plugins.adhoc(stream)
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 stream:add_disco_feature(xmlns_commands);
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local function has_affiliation(jid, aff)
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 if not(aff) or aff == "user" then return true; end
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 -- TODO: Support 'roster', and callback etc.
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 end
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 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
16 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
17 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
18 return commands[node];
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 end
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 local function handle_command(stanza)
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 local command_tag = stanza.tags[1];
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 local node = command_tag.attr.node;
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 local handler = commands[node];
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 if not handler then return; end
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 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
29 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
30 :add_child(handler:cmdtag("canceled")
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 :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
32 return true
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 end
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 -- 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
36 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
37 end
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 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
40 local type = stanza.attr.type;
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 local name = stanza.tags[1].name;
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 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
43 return handle_command(stanza);
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 end);
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 end