Annotate

util/discohelper.lua @ 758:b1885732e979

GPL->MIT!
author Matthew Wild <mwild1@gmail.com>
date Fri, 30 Jan 2009 17:22:56 +0000
parent 615:4ae3e81513f3
child 759:5cccfb5da6cb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
615
4ae3e81513f3 0.1 -> 0.2
Matthew Wild <mwild1@gmail.com>
parents: 539
diff changeset
1 -- Prosody IM v0.2
519
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 --
758
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
6 -- COPYING file in the source package for more information.
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
7 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
8
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
9
387
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11 local t_insert = table.insert;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12 local jid_split = require "util.jid".split;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13 local ipairs = ipairs;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14 local st = require "util.stanza";
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16 module "discohelper";
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18 local function addDiscoItemsHandler(self, jid, func)
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19 if self.item_handlers[jid] then
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 t_insert(self.item_handlers[jid], func);
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21 else
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 self.item_handlers[jid] = {func};
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 end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26 local function addDiscoInfoHandler(self, jid, func)
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27 if self.info_handlers[jid] then
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 t_insert(self.info_handlers[jid], func);
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29 else
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 self.info_handlers[jid] = {func};
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32 end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34 local function handle(self, stanza)
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 if stanza.name == "iq" and stanza.tags[1].name == "query" then
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36 local query = stanza.tags[1];
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 local to = stanza.attr.to;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38 local from = stanza.attr.from
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 local node = query.attr.node or "";
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40 local to_node, to_host = jid_split(to);
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
41
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
42 local reply = st.reply(stanza):query(query.attr.xmlns);
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 local handlers;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
44 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
45 handlers = self.info_handlers;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46 elseif query.attr.xmlns == "http://jabber.org/protocol/disco#items" then
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47 handlers = self.item_handlers;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48 end
539
cbcadb1a6166 Reorder the disco info elements to place always included elements first
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
49 local handler;
cbcadb1a6166 Reorder the disco info elements to place always included elements first
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
50 local found; -- to keep track of any handlers found
387
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
51 if to_node then -- handlers which get called always
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
52 handler = handlers["*node"];
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
53 else
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
54 handler = handlers["*host"];
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
55 end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
56 if handler then -- call always called handler
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
57 for _, h in ipairs(handler) do
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
58 if h(reply, to, from, node) then found = true; end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59 end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60 end
539
cbcadb1a6166 Reorder the disco info elements to place always included elements first
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
61 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
62 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
63 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
64 handler = handlers["*defaultnode"];
cbcadb1a6166 Reorder the disco info elements to place always included elements first
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
65 else
cbcadb1a6166 Reorder the disco info elements to place always included elements first
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
66 handler = handlers["*defaulthost"];
cbcadb1a6166 Reorder the disco info elements to place always included elements first
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
67 end
cbcadb1a6166 Reorder the disco info elements to place always included elements first
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
68 end
cbcadb1a6166 Reorder the disco info elements to place always included elements first
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
69 if handler then
cbcadb1a6166 Reorder the disco info elements to place always included elements first
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
70 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
71 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
72 end
cbcadb1a6166 Reorder the disco info elements to place always included elements first
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
73 end
387
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
74 if found then return reply; end -- return the reply if there was one
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
75 return st.error_reply(stanza, "cancel", "service-unavailable");
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
76 end
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 function new()
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
80 return {
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
81 item_handlers = {};
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
82 info_handlers = {};
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
83 addDiscoItemsHandler = addDiscoItemsHandler;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
84 addDiscoInfoHandler = addDiscoInfoHandler;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
85 handle = handle;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
86 };
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
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
89 return _M;