Mercurial > prosody-hg
annotate util/discohelper.lua @ 527:98aaa20af6a2
Update HACKERS
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Wed, 03 Dec 2008 16:20:58 +0000 |
| parents | cccd610a0ef9 |
| children | cbcadb1a6166 |
| 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 | 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 | |
| 60 local handler = handlers[to]; -- get the handler | |
| 61 if not handler then -- if not found then use default handler | |
| 62 if to_node then | |
| 63 handler = handlers["*defaultnode"]; | |
| 64 else | |
| 65 handler = handlers["*defaulthost"]; | |
| 66 end | |
| 67 end | |
| 68 local found; -- to keep track of any handlers found | |
| 69 if handler then | |
| 70 for _, h in ipairs(handler) do | |
| 71 if h(reply, to, from, node) then found = true; end | |
| 72 end | |
| 73 end | |
| 74 if to_node then -- handlers which get called always | |
| 75 handler = handlers["*node"]; | |
| 76 else | |
| 77 handler = handlers["*host"]; | |
| 78 end | |
| 79 if handler then -- call always called handler | |
| 80 for _, h in ipairs(handler) do | |
| 81 if h(reply, to, from, node) then found = true; end | |
| 82 end | |
| 83 end | |
| 84 if found then return reply; end -- return the reply if there was one | |
| 85 return st.error_reply(stanza, "cancel", "service-unavailable"); | |
| 86 end | |
| 87 end | |
| 88 | |
| 89 function new() | |
| 90 return { | |
| 91 item_handlers = {}; | |
| 92 info_handlers = {}; | |
| 93 addDiscoItemsHandler = addDiscoItemsHandler; | |
| 94 addDiscoInfoHandler = addDiscoInfoHandler; | |
| 95 handle = handle; | |
| 96 }; | |
| 97 end | |
| 98 | |
| 99 return _M; |
