Annotate

util/discohelper.lua @ 1523:841d61be198f

Remove version number from copyright headers
author Matthew Wild <mwild1@gmail.com>
date Fri, 10 Jul 2009 03:11:45 +0100
parent 1509:a2ea99238466
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 1509
diff changeset
1 -- Prosody IM
760
90ce865eebd8 Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents: 759
diff changeset
2 -- Copyright (C) 2008-2009 Matthew Wild
90ce865eebd8 Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents: 759
diff changeset
3 -- Copyright (C) 2008-2009 Waqas Hussain
519
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
1509
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
10
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
11 local t_insert = table.insert;
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
12 local jid_split = require "util.jid".split;
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
13 local ipairs = ipairs;
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
14 local st = require "util.stanza";
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
15
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
16 module "discohelper";
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
17
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
18 local function addDiscoItemsHandler(self, jid, func)
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
19 if self.item_handlers[jid] then
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
20 t_insert(self.item_handlers[jid], func);
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
21 else
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
22 self.item_handlers[jid] = {func};
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
23 end
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
24 end
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
25
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
26 local function addDiscoInfoHandler(self, jid, func)
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
27 if self.info_handlers[jid] then
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
28 t_insert(self.info_handlers[jid], func);
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
29 else
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
30 self.info_handlers[jid] = {func};
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
31 end
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
32 end
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
33
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
34 local function handle(self, stanza)
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
35 if stanza.name == "iq" and stanza.tags[1].name == "query" then
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
36 local query = stanza.tags[1];
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
37 local to = stanza.attr.to;
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
38 local from = stanza.attr.from
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
39 local node = query.attr.node or "";
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
40 local to_node, to_host = jid_split(to);
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
41
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
42 local reply = st.reply(stanza):query(query.attr.xmlns);
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
43 local handlers;
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
44 if query.attr.xmlns == "http://jabber.org/protocol/disco#info" then -- select handler set
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
45 handlers = self.info_handlers;
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
46 elseif query.attr.xmlns == "http://jabber.org/protocol/disco#items" then
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
47 handlers = self.item_handlers;
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
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
1509
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
51 if to_node then -- handlers which get called always
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
52 handler = handlers["*node"];
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
53 else
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
54 handler = handlers["*host"];
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
55 end
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
56 if handler then -- call always called handler
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
57 for _, h in ipairs(handler) do
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
58 if h(reply, to, from, node) then found = true; end
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
59 end
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
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
1509
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
74 if found then return reply; end -- return the reply if there was one
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
75 return st.error_reply(stanza, "cancel", "service-unavailable");
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
76 end
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
77 end
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
78
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
79 function new()
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
80 return {
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
81 item_handlers = {};
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
82 info_handlers = {};
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
83 addDiscoItemsHandler = addDiscoItemsHandler;
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
84 addDiscoInfoHandler = addDiscoInfoHandler;
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
85 handle = handle;
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
86 };
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
87 end
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
88
a2ea99238466 util.discohelper: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
89 return _M;