2 local t_insert = table.insert;
\r
3 local jid_split = require "util.jid".split;
\r
4 local ipairs = ipairs;
\r
5 local st = require "util.stanza";
\r
7 module "discohelper";
\r
9 local function addDiscoItemsHandler(self, jid, func)
\r
10 if self.item_handlers[jid] then
\r
11 t_insert(self.item_handlers[jid], func);
\r
13 self.item_handlers[jid] = {func};
\r
17 local function addDiscoInfoHandler(self, jid, func)
\r
18 if self.info_handlers[jid] then
\r
19 t_insert(self.info_handlers[jid], func);
\r
21 self.info_handlers[jid] = {func};
\r
25 local function handle(self, stanza)
\r
26 if stanza.name == "iq" and stanza.tags[1].name == "query" then
\r
27 local query = stanza.tags[1];
\r
28 local to = stanza.attr.to;
\r
29 local from = stanza.attr.from
\r
30 local node = query.attr.node or "";
\r
31 local to_node, to_host = jid_split(to);
\r
33 local reply = st.reply(stanza):query(query.attr.xmlns);
\r
35 if query.attr.xmlns == "http://jabber.org/protocol/disco#info" then -- select handler set
\r
36 handlers = self.info_handlers;
\r
37 elseif query.attr.xmlns == "http://jabber.org/protocol/disco#items" then
\r
38 handlers = self.item_handlers;
\r
40 local handler = handlers[to]; -- get the handler
\r
41 if not handler then -- if not found then use default handler
\r
43 handler = handlers["*defaultnode"];
\r
45 handler = handlers["*defaulthost"];
\r
48 local found; -- to keep track of any handlers found
\r
50 for _, h in ipairs(handler) do
\r
51 if h(reply, to, from, node) then found = true; end
\r
54 if to_node then -- handlers which get called always
\r
55 handler = handlers["*node"];
\r
57 handler = handlers["*host"];
\r
59 if handler then -- call always called handler
\r
60 for _, h in ipairs(handler) do
\r
61 if h(reply, to, from, node) then found = true; end
\r
64 if found then return reply; end -- return the reply if there was one
\r
65 return st.error_reply(stanza, "cancel", "service-unavailable");
\r
73 addDiscoItemsHandler = addDiscoItemsHandler;
\r
74 addDiscoInfoHandler = addDiscoInfoHandler;
\r