util/discohelper.lua

Sun, 23 Nov 2008 03:32:57 +0500

author
Waqas Hussain <waqas20@gmail.com>
date
Sun, 23 Nov 2008 03:32:57 +0500
changeset 389
c747d6963d95
parent 387
700e95c00c5b
child 519
cccd610a0ef9
permissions
-rw-r--r--

Added mod_disco

387
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
1
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
2 local t_insert = table.insert;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
3 local jid_split = require "util.jid".split;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4 local ipairs = ipairs;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 local st = require "util.stanza";
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7 module "discohelper";
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9 local function addDiscoItemsHandler(self, jid, func)
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10 if self.item_handlers[jid] then
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11 t_insert(self.item_handlers[jid], func);
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12 else
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13 self.item_handlers[jid] = {func};
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14 end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15 end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17 local function addDiscoInfoHandler(self, jid, func)
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18 if self.info_handlers[jid] then
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19 t_insert(self.info_handlers[jid], func);
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 else
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21 self.info_handlers[jid] = {func};
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23 end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 local function handle(self, stanza)
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26 if stanza.name == "iq" and stanza.tags[1].name == "query" then
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27 local query = stanza.tags[1];
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 local to = stanza.attr.to;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29 local from = stanza.attr.from
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 local node = query.attr.node or "";
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 local to_node, to_host = jid_split(to);
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 local reply = st.reply(stanza):query(query.attr.xmlns);
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34 local handlers;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 if query.attr.xmlns == "http://jabber.org/protocol/disco#info" then -- select handler set
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36 handlers = self.info_handlers;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 elseif query.attr.xmlns == "http://jabber.org/protocol/disco#items" then
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38 handlers = self.item_handlers;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40 local handler = handlers[to]; -- get the handler
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
41 if not handler then -- if not found then use default handler
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
42 if to_node then
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 handler = handlers["*defaultnode"];
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
44 else
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45 handler = handlers["*defaulthost"];
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46 end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47 end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48 local found; -- to keep track of any handlers found
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49 if handler then
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50 for _, h in ipairs(handler) do
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
51 if h(reply, to, from, node) then found = true; end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
52 end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
53 end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
54 if to_node then -- handlers which get called always
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
55 handler = handlers["*node"];
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
56 else
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
57 handler = handlers["*host"];
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
58 end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59 if handler then -- call always called handler
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60 for _, h in ipairs(handler) do
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
61 if h(reply, to, from, node) then found = true; end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62 end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
63 end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
64 if found then return reply; end -- return the reply if there was one
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
65 return st.error_reply(stanza, "cancel", "service-unavailable");
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
66 end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
67 end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69 function new()
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
70 return {
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
71 item_handlers = {};
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
72 info_handlers = {};
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
73 addDiscoItemsHandler = addDiscoItemsHandler;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
74 addDiscoInfoHandler = addDiscoInfoHandler;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
75 handle = handle;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
76 };
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
77 end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
78
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
79 return _M;

mercurial