0.2->0.3
[prosody.git] / core / discomanager.lua
1 -- Prosody IM v0.3
2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9
10 \r
11 local helper = require "util.discohelper".new();\r
12 local hosts = hosts;\r
13 local jid_split = require "util.jid".split;\r
14 local jid_bare = require "util.jid".bare;\r
15 local usermanager_user_exists = require "core.usermanager".user_exists;\r
16 local rostermanager_is_contact_subscribed = require "core.rostermanager".is_contact_subscribed;\r
17 local print = print;\r
18 \r
19 do\r
20         helper:addDiscoInfoHandler("*host", function(reply, to, from, node)\r
21                 if hosts[to] then\r
22                         reply:tag("identity", {category="server", type="im", name="Prosody"}):up();\r
23                         return true;\r
24                 end\r
25         end);\r
26         helper:addDiscoInfoHandler("*node", function(reply, to, from, node)\r
27                 local node, host = jid_split(to);\r
28                 if hosts[host] and rostermanager_is_contact_subscribed(node, host, jid_bare(from)) then\r
29                         reply:tag("identity", {category="account", type="registered"}):up();\r
30                         return true;\r
31                 end\r
32         end);\r
33         helper:addDiscoItemsHandler("*host", function(reply, to, from, node)
34                 if hosts[to] and hosts[to].type == "local" then
35                         return true;
36                 end
37         end);
38 end\r
39 \r
40 module "discomanager"\r
41 \r
42 function handle(stanza)\r
43         return helper:handle(stanza);\r
44 end\r
45 \r
46 function addDiscoItemsHandler(jid, func)\r
47         return helper:addDiscoItemsHandler(jid, func);\r
48 end\r
49 \r
50 function addDiscoInfoHandler(jid, func)\r
51         return helper:addDiscoInfoHandler(jid, func);\r
52 end\r
53 \r
54 function set(plugin, var, origin)\r
55         -- TODO handle origin and host based on plugin.\r
56         local handler = function(reply, to, from, node) -- service discovery\r
57                 if #node == 0 then\r
58                         reply:tag("feature", {var = var}):up();\r
59                         return true;\r
60                 end\r
61         end\r
62         addDiscoInfoHandler("*node", handler);\r
63         addDiscoInfoHandler("*host", handler);\r
64 end\r
65 \r
66 return _M;\r