author | Waqas Hussain <waqas20@gmail.com> |
Thu, 04 Dec 2008 20:01:57 +0500 | |
changeset 539 | cbcadb1a6166 |
parent 519 | cccd610a0ef9 |
child 615 | 4ae3e81513f3 |
permissions | -rw-r--r-- |
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 | 21 |
|
22 |
local t_insert = table.insert; |
|
23 |
local jid_split = require "util.jid".split; |
|
24 |
local ipairs = ipairs; |
|
25 |
local st = require "util.stanza"; |
|
26 |
||
27 |
module "discohelper"; |
|
28 |
||
29 |
local function addDiscoItemsHandler(self, jid, func) |
|
30 |
if self.item_handlers[jid] then |
|
31 |
t_insert(self.item_handlers[jid], func); |
|
32 |
else |
|
33 |
self.item_handlers[jid] = {func}; |
|
34 |
end |
|
35 |
end |
|
36 |
||
37 |
local function addDiscoInfoHandler(self, jid, func) |
|
38 |
if self.info_handlers[jid] then |
|
39 |
t_insert(self.info_handlers[jid], func); |
|
40 |
else |
|
41 |
self.info_handlers[jid] = {func}; |
|
42 |
end |
|
43 |
end |
|
44 |
||
45 |
local function handle(self, stanza) |
|
46 |
if stanza.name == "iq" and stanza.tags[1].name == "query" then |
|
47 |
local query = stanza.tags[1]; |
|
48 |
local to = stanza.attr.to; |
|
49 |
local from = stanza.attr.from |
|
50 |
local node = query.attr.node or ""; |
|
51 |
local to_node, to_host = jid_split(to); |
|
52 |
||
53 |
local reply = st.reply(stanza):query(query.attr.xmlns); |
|
54 |
local handlers; |
|
55 |
if query.attr.xmlns == "http://jabber.org/protocol/disco#info" then -- select handler set |
|
56 |
handlers = self.info_handlers; |
|
57 |
elseif query.attr.xmlns == "http://jabber.org/protocol/disco#items" then |
|
58 |
handlers = self.item_handlers; |
|
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 | 62 |
if to_node then -- handlers which get called always |
63 |
handler = handlers["*node"]; |
|
64 |
else |
|
65 |
handler = handlers["*host"]; |
|
66 |
end |
|
67 |
if handler then -- call always called handler |
|
68 |
for _, h in ipairs(handler) do |
|
69 |
if h(reply, to, from, node) then found = true; end |
|
70 |
end |
|
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 | 85 |
if found then return reply; end -- return the reply if there was one |
86 |
return st.error_reply(stanza, "cancel", "service-unavailable"); |
|
87 |
end |
|
88 |
end |
|
89 |
||
90 |
function new() |
|
91 |
return { |
|
92 |
item_handlers = {}; |
|
93 |
info_handlers = {}; |
|
94 |
addDiscoItemsHandler = addDiscoItemsHandler; |
|
95 |
addDiscoInfoHandler = addDiscoInfoHandler; |
|
96 |
handle = handle; |
|
97 |
}; |
|
98 |
end |
|
99 |
||
100 |
return _M; |