Software /
code /
prosody
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 |
rev | line source |
---|---|
615 | 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 | 5 -- This project is MIT/X11 licensed. Please see the |
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 | 10 |
11 local t_insert = table.insert; | |
12 local jid_split = require "util.jid".split; | |
13 local ipairs = ipairs; | |
14 local st = require "util.stanza"; | |
15 | |
16 module "discohelper"; | |
17 | |
18 local function addDiscoItemsHandler(self, jid, func) | |
19 if self.item_handlers[jid] then | |
20 t_insert(self.item_handlers[jid], func); | |
21 else | |
22 self.item_handlers[jid] = {func}; | |
23 end | |
24 end | |
25 | |
26 local function addDiscoInfoHandler(self, jid, func) | |
27 if self.info_handlers[jid] then | |
28 t_insert(self.info_handlers[jid], func); | |
29 else | |
30 self.info_handlers[jid] = {func}; | |
31 end | |
32 end | |
33 | |
34 local function handle(self, stanza) | |
35 if stanza.name == "iq" and stanza.tags[1].name == "query" then | |
36 local query = stanza.tags[1]; | |
37 local to = stanza.attr.to; | |
38 local from = stanza.attr.from | |
39 local node = query.attr.node or ""; | |
40 local to_node, to_host = jid_split(to); | |
41 | |
42 local reply = st.reply(stanza):query(query.attr.xmlns); | |
43 local handlers; | |
44 if query.attr.xmlns == "http://jabber.org/protocol/disco#info" then -- select handler set | |
45 handlers = self.info_handlers; | |
46 elseif query.attr.xmlns == "http://jabber.org/protocol/disco#items" then | |
47 handlers = self.item_handlers; | |
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 | 51 if to_node then -- handlers which get called always |
52 handler = handlers["*node"]; | |
53 else | |
54 handler = handlers["*host"]; | |
55 end | |
56 if handler then -- call always called handler | |
57 for _, h in ipairs(handler) do | |
58 if h(reply, to, from, node) then found = true; end | |
59 end | |
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 | 74 if found then return reply; end -- return the reply if there was one |
75 return st.error_reply(stanza, "cancel", "service-unavailable"); | |
76 end | |
77 end | |
78 | |
79 function new() | |
80 return { | |
81 item_handlers = {}; | |
82 info_handlers = {}; | |
83 addDiscoItemsHandler = addDiscoItemsHandler; | |
84 addDiscoInfoHandler = addDiscoInfoHandler; | |
85 handle = handle; | |
86 }; | |
87 end | |
88 | |
89 return _M; |