Automated merge with http://waqas.ath.cx:8000/
[prosody.git] / core / discomanager.lua
1 -- Prosody IM v0.2
2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
4 -- 
5 -- This program is free software; you can redistribute it and/or
6 -- modify it under the terms of the GNU General Public License
7 -- as published by the Free Software Foundation; either version 2
8 -- of the License, or (at your option) any later version.
9 -- 
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 -- GNU General Public License for more details.
14 -- 
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 --
19
20
21 \r
22 local helper = require "util.discohelper".new();\r
23 local hosts = hosts;\r
24 local jid_split = require "util.jid".split;\r
25 local jid_bare = require "util.jid".bare;\r
26 local usermanager_user_exists = require "core.usermanager".user_exists;\r
27 local rostermanager_is_contact_subscribed = require "core.rostermanager".is_contact_subscribed;\r
28 local print = print;\r
29 \r
30 do\r
31         helper:addDiscoInfoHandler("*host", function(reply, to, from, node)\r
32                 if hosts[to] then\r
33                         reply:tag("identity", {category="server", type="im", name="Prosody"}):up();\r
34                         return true;\r
35                 end\r
36         end);\r
37         helper:addDiscoInfoHandler("*node", function(reply, to, from, node)\r
38                 local node, host = jid_split(to);\r
39                 if hosts[host] and rostermanager_is_contact_subscribed(node, host, jid_bare(from)) then\r
40                         reply:tag("identity", {category="account", type="registered"}):up();\r
41                         return true;\r
42                 end\r
43         end);\r
44         helper:addDiscoItemsHandler("*host", function(reply, to, from, node)
45                 if hosts[to] and hosts[to].type == "local" then
46                         return true;
47                 end
48         end);
49 end\r
50 \r
51 module "discomanager"\r
52 \r
53 function handle(stanza)\r
54         return helper:handle(stanza);\r
55 end\r
56 \r
57 function addDiscoItemsHandler(jid, func)\r
58         return helper:addDiscoItemsHandler(jid, func);\r
59 end\r
60 \r
61 function addDiscoInfoHandler(jid, func)\r
62         return helper:addDiscoInfoHandler(jid, func);\r
63 end\r
64 \r
65 function set(plugin, var, origin)\r
66         -- TODO handle origin and host based on plugin.\r
67         local handler = function(reply, to, from, node) -- service discovery\r
68                 if #node == 0 then\r
69                         reply:tag("feature", {var = var}):up();\r
70                         return true;\r
71                 end\r
72         end\r
73         addDiscoInfoHandler("*node", handler);\r
74         addDiscoInfoHandler("*host", handler);\r
75 end\r
76 \r
77 return _M;\r