Annotate

util/discohelper.lua @ 594:6d9b2da9888f

Small logging adjustment for mod_posix
author Matthew Wild <mwild1@gmail.com>
date Sun, 07 Dec 2008 03:12:56 +0000
parent 539:cbcadb1a6166
child 615:4ae3e81513f3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
1 -- Prosody IM v0.1
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
2 -- Copyright (C) 2008 Matthew Wild
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
3 -- Copyright (C) 2008 Waqas Hussain
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
4 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
5 -- This program is free software; you can redistribute it and/or
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
6 -- modify it under the terms of the GNU General Public License
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
7 -- as published by the Free Software Foundation; either version 2
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
8 -- of the License, or (at your option) any later version.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
9 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
10 -- This program is distributed in the hope that it will be useful,
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
13 -- GNU General Public License for more details.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
14 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
15 -- You should have received a copy of the GNU General Public License
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
16 -- along with this program; if not, write to the Free Software
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
18 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
19
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
20
387
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 local t_insert = table.insert;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23 local jid_split = require "util.jid".split;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 local ipairs = ipairs;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 local st = require "util.stanza";
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27 module "discohelper";
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29 local function addDiscoItemsHandler(self, jid, func)
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 if self.item_handlers[jid] then
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 t_insert(self.item_handlers[jid], func);
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32 else
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 self.item_handlers[jid] = {func};
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34 end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 local function addDiscoInfoHandler(self, jid, func)
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38 if self.info_handlers[jid] then
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 t_insert(self.info_handlers[jid], func);
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40 else
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
41 self.info_handlers[jid] = {func};
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
42 end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
44
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45 local function handle(self, stanza)
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46 if stanza.name == "iq" and stanza.tags[1].name == "query" then
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47 local query = stanza.tags[1];
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48 local to = stanza.attr.to;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49 local from = stanza.attr.from
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50 local node = query.attr.node or "";
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
51 local to_node, to_host = jid_split(to);
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
52
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
53 local reply = st.reply(stanza):query(query.attr.xmlns);
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
54 local handlers;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
55 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
56 handlers = self.info_handlers;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
57 elseif query.attr.xmlns == "http://jabber.org/protocol/disco#items" then
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
58 handlers = self.item_handlers;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59 end
539
cbcadb1a6166 Reorder the disco info elements to place always included elements first
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
60 local handler;
cbcadb1a6166 Reorder the disco info elements to place always included elements first
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
61 local found; -- to keep track of any handlers found
387
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62 if to_node then -- handlers which get called always
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
63 handler = handlers["*node"];
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
64 else
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
65 handler = handlers["*host"];
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 if handler then -- call always called handler
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68 for _, h in ipairs(handler) do
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69 if h(reply, to, from, node) then found = true; end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
70 end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
71 end
539
cbcadb1a6166 Reorder the disco info elements to place always included elements first
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
72 handler = handlers[to]; -- get the handler
cbcadb1a6166 Reorder the disco info elements to place always included elements first
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
73 if not handler then -- if not found then use default handler
cbcadb1a6166 Reorder the disco info elements to place always included elements first
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
74 if to_node then
cbcadb1a6166 Reorder the disco info elements to place always included elements first
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
75 handler = handlers["*defaultnode"];
cbcadb1a6166 Reorder the disco info elements to place always included elements first
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
76 else
cbcadb1a6166 Reorder the disco info elements to place always included elements first
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
77 handler = handlers["*defaulthost"];
cbcadb1a6166 Reorder the disco info elements to place always included elements first
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
78 end
cbcadb1a6166 Reorder the disco info elements to place always included elements first
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
79 end
cbcadb1a6166 Reorder the disco info elements to place always included elements first
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
80 if handler then
cbcadb1a6166 Reorder the disco info elements to place always included elements first
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
81 for _, h in ipairs(handler) do
cbcadb1a6166 Reorder the disco info elements to place always included elements first
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
82 if h(reply, to, from, node) then found = true; end
cbcadb1a6166 Reorder the disco info elements to place always included elements first
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
83 end
cbcadb1a6166 Reorder the disco info elements to place always included elements first
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
84 end
387
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
85 if found then return reply; end -- return the reply if there was one
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
86 return st.error_reply(stanza, "cancel", "service-unavailable");
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
87 end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
88 end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
89
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
90 function new()
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
91 return {
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
92 item_handlers = {};
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
93 info_handlers = {};
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
94 addDiscoItemsHandler = addDiscoItemsHandler;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
95 addDiscoInfoHandler = addDiscoInfoHandler;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
96 handle = handle;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
97 };
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
98 end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
99
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
100 return _M;