2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
11 local t_insert = table.insert;
\r
12 local jid_split = require "util.jid".split;
\r
13 local ipairs = ipairs;
\r
14 local st = require "util.stanza";
\r
16 module "discohelper";
\r
18 local function addDiscoItemsHandler(self, jid, func)
\r
19 if self.item_handlers[jid] then
\r
20 t_insert(self.item_handlers[jid], func);
\r
22 self.item_handlers[jid] = {func};
\r
26 local function addDiscoInfoHandler(self, jid, func)
\r
27 if self.info_handlers[jid] then
\r
28 t_insert(self.info_handlers[jid], func);
\r
30 self.info_handlers[jid] = {func};
\r
34 local function handle(self, stanza)
\r
35 if stanza.name == "iq" and stanza.tags[1].name == "query" then
\r
36 local query = stanza.tags[1];
\r
37 local to = stanza.attr.to;
\r
38 local from = stanza.attr.from
\r
39 local node = query.attr.node or "";
\r
40 local to_node, to_host = jid_split(to);
\r
42 local reply = st.reply(stanza):query(query.attr.xmlns);
\r
44 if query.attr.xmlns == "http://jabber.org/protocol/disco#info" then -- select handler set
\r
45 handlers = self.info_handlers;
\r
46 elseif query.attr.xmlns == "http://jabber.org/protocol/disco#items" then
\r
47 handlers = self.item_handlers;
\r
50 local found; -- to keep track of any handlers found
51 if to_node then -- handlers which get called always
\r
52 handler = handlers["*node"];
\r
54 handler = handlers["*host"];
\r
56 if handler then -- call always called handler
\r
57 for _, h in ipairs(handler) do
\r
58 if h(reply, to, from, node) then found = true; end
\r
61 handler = handlers[to]; -- get the handler
62 if not handler then -- if not found then use default handler
64 handler = handlers["*defaultnode"];
66 handler = handlers["*defaulthost"];
70 for _, h in ipairs(handler) do
71 if h(reply, to, from, node) then found = true; end
74 if found then return reply; end -- return the reply if there was one
\r
75 return st.error_reply(stanza, "cancel", "service-unavailable");
\r
83 addDiscoItemsHandler = addDiscoItemsHandler;
\r
84 addDiscoInfoHandler = addDiscoInfoHandler;
\r