util.pluginloader: Remove unnecessary return value suppressing the real load error
[prosody.git] / util / discohelper.lua
1 -- Prosody IM v0.4
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9
10 \r
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
15 \r
16 module "discohelper";\r
17 \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
21         else\r
22                 self.item_handlers[jid] = {func};\r
23         end\r
24 end\r
25 \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
29         else\r
30                 self.info_handlers[jid] = {func};\r
31         end\r
32 end\r
33 \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
41 \r
42                 local reply = st.reply(stanza):query(query.attr.xmlns);\r
43                 local handlers;\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
48                 end\r
49                 local handler;
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
53                 else\r
54                         handler = handlers["*host"];\r
55                 end\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
59                         end\r
60                 end\r
61                 handler = handlers[to]; -- get the handler
62                 if not handler then -- if not found then use default handler
63                         if to_node then
64                                 handler = handlers["*defaultnode"];
65                         else
66                                 handler = handlers["*defaulthost"];
67                         end
68                 end
69                 if handler then
70                         for _, h in ipairs(handler) do
71                                 if h(reply, to, from, node) then found = true; end
72                         end
73                 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
76         end\r
77 end\r
78 \r
79 function new()\r
80         return {\r
81                 item_handlers = {};\r
82                 info_handlers = {};\r
83                 addDiscoItemsHandler = addDiscoItemsHandler;\r
84                 addDiscoInfoHandler = addDiscoInfoHandler;\r
85                 handle = handle;\r
86         };\r
87 end\r
88 \r
89 return _M;\r