mod_adhoc/mod_adhoc.lua
changeset 37 6018c0370d89
parent 31 a0dfa3e5883c
parent 36 58d326d86a9a
child 38 b9bf8a35b064
equal deleted inserted replaced
31:a0dfa3e5883c 37:6018c0370d89
     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);