comparison mod_adhoc/adhoc/mod_adhoc.lua @ 36:58d326d86a9a

mod_adhoc: add adhoc.lib.lua to ease implementing new commands (as a consequence mod_adhoc is a directory now) mod_adhoc_cmd_*: Convert to use adhoc.lib.lua
author Florian Zeitz <florob@babelmonkeys.de>
date Sun, 11 Oct 2009 01:20:16 +0200
parents mod_adhoc/mod_adhoc.lua@b9d063dd16d5
children adc9eff8adb2
comparison
equal deleted inserted replaced
35:3c49411d4aa3 36:58d326d86a9a
1 -- Copyright (C) 2009 Thilo Cestonaro
2 --
3 -- This file is MIT/X11 licensed. Please see the
4 -- COPYING file in the source package for more information.
5 --
6
7 local st = require "util.stanza";
8 local commands = {};
9
10 module:add_feature("http://jabber.org/protocol/commands");
11
12 module:hook("iq/host/http://jabber.org/protocol/disco#items:query", function (event)
13 local origin, stanza = event.origin, event.stanza;
14 if stanza.attr.type == "get" and stanza.tags[1].attr.node and stanza.tags[1].attr.node == "http://jabber.org/protocol/commands" then
15 reply = st.reply(stanza);
16 reply:tag("query", {xmlns="http://jabber.org/protocol/disco#items", node="http://jabber.org/protocol/commands"})
17 for i = 1, #commands do
18 -- module:log("info", "adding command %s", commands[i].name);
19 reply:tag("item", {name=commands[i].name, node=commands[i].node, jid=module:get_host()});
20 reply:up();
21 end
22 origin.send(reply);
23 return true;
24 end
25 end, 500);
26
27 module:hook("iq/host", function (event)
28 local origin, stanza = event.origin, event.stanza;
29 if stanza.attr.type == "set" and stanza.tags[1] and stanza.tags[1].name == "command" then
30 local node = stanza.tags[1].attr.node
31 for i = 1, #commands do
32 if commands[i].node == node then
33 return commands[i].handler(commands[i], origin, stanza);
34 end
35 end
36 end
37 end, 500);
38
39 module:hook("item-added/adhoc", function (event)
40 commands[ # commands + 1] = event.item;
41 end, 500);
42
43 local _G = _G;
44 local t_remove = _G.table.remove;
45 module:hook("item-removed/adhoc", function (event)
46 for i = 1, #commands do
47 if commands[i].node == event.item.node then
48 t_remove(commands, i);
49 break;
50 end
51 end
52 end, 500);