mod_privacy: Fix selecting the top resource (fixes #694)
[prosody.git] / plugins / mod_disco.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 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 local get_children = require "core.hostmanager".get_children;
10 local is_contact_subscribed = require "core.rostermanager".is_contact_subscribed;
11 local jid_split = require "util.jid".split;
12 local jid_bare = require "util.jid".bare;
13 local st = require "util.stanza"
14 local calculate_hash = require "util.caps".calculate_hash;
15
16 local disco_items = module:get_option("disco_items") or {};
17 do -- validate disco_items
18         for _, item in ipairs(disco_items) do
19                 local err;
20                 if type(item) ~= "table" then
21                         err = "item is not a table";
22                 elseif type(item[1]) ~= "string" then
23                         err = "item jid is not a string";
24                 elseif item[2] and type(item[2]) ~= "string" then
25                         err = "item name is not a string";
26                 end
27                 if err then
28                         module:log("error", "option disco_items is malformed: %s", err);
29                         disco_items = {}; -- TODO clean up data instead of removing it?
30                         break;
31                 end
32         end
33 end
34
35 module:add_identity("server", "im", module:get_option_string("name", "Prosody")); -- FIXME should be in the non-existing mod_router
36 module:add_feature("http://jabber.org/protocol/disco#info");
37 module:add_feature("http://jabber.org/protocol/disco#items");
38
39 -- Generate and cache disco result and caps hash
40 local _cached_server_disco_info, _cached_server_caps_feature, _cached_server_caps_hash;
41 local function build_server_disco_info()
42         local query = st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#info" });
43         local done = {};
44         for _,identity in ipairs(module:get_host_items("identity")) do
45                 local identity_s = identity.category.."\0"..identity.type;
46                 if not done[identity_s] then
47                         query:tag("identity", identity):up();
48                         done[identity_s] = true;
49                 end
50         end
51         for _,feature in ipairs(module:get_host_items("feature")) do
52                 if not done[feature] then
53                         query:tag("feature", {var=feature}):up();
54                         done[feature] = true;
55                 end
56         end
57         for _,extension in ipairs(module:get_host_items("extension")) do
58                 if not done[extension] then
59                         query:add_child(extension);
60                         done[extension] = true;
61                 end
62         end
63         _cached_server_disco_info = query;
64         _cached_server_caps_hash = calculate_hash(query);
65         _cached_server_caps_feature = st.stanza("c", {
66                 xmlns = "http://jabber.org/protocol/caps";
67                 hash = "sha-1";
68                 node = "http://prosody.im";
69                 ver = _cached_server_caps_hash;
70         });
71 end
72 local function clear_disco_cache()
73         _cached_server_disco_info, _cached_server_caps_feature, _cached_server_caps_hash = nil, nil, nil;
74 end
75 local function get_server_disco_info()
76         if not _cached_server_disco_info then build_server_disco_info(); end
77         return _cached_server_disco_info;
78 end
79 local function get_server_caps_feature()
80         if not _cached_server_caps_feature then build_server_disco_info(); end
81         return _cached_server_caps_feature;
82 end
83 local function get_server_caps_hash()
84         if not _cached_server_caps_hash then build_server_disco_info(); end
85         return _cached_server_caps_hash;
86 end
87
88 module:hook("item-added/identity", clear_disco_cache);
89 module:hook("item-added/feature", clear_disco_cache);
90 module:hook("item-added/extension", clear_disco_cache);
91 module:hook("item-removed/identity", clear_disco_cache);
92 module:hook("item-removed/feature", clear_disco_cache);
93 module:hook("item-removed/extension", clear_disco_cache);
94
95 -- Handle disco requests to the server
96 module:hook("iq/host/http://jabber.org/protocol/disco#info:query", function(event)
97         local origin, stanza = event.origin, event.stanza;
98         if stanza.attr.type ~= "get" then return; end
99         local node = stanza.tags[1].attr.node;
100         if node and node ~= "" and node ~= "http://prosody.im#"..get_server_caps_hash() then return; end -- TODO fire event?
101         local reply_query = get_server_disco_info();
102         reply_query.node = node;
103         local reply = st.reply(stanza):add_child(reply_query);
104         origin.send(reply);
105         return true;
106 end);
107 module:hook("iq/host/http://jabber.org/protocol/disco#items:query", function(event)
108         local origin, stanza = event.origin, event.stanza;
109         if stanza.attr.type ~= "get" then return; end
110         local node = stanza.tags[1].attr.node;
111         if node and node ~= "" then return; end -- TODO fire event?
112
113         local reply = st.reply(stanza):query("http://jabber.org/protocol/disco#items");
114         for jid, name in pairs(get_children(module.host)) do
115                 reply:tag("item", {jid = jid, name = name~=true and name or nil}):up();
116         end
117         for _, item in ipairs(disco_items) do
118                 reply:tag("item", {jid=item[1], name=item[2]}):up();
119         end
120         origin.send(reply);
121         return true;
122 end);
123
124 -- Handle caps stream feature
125 module:hook("stream-features", function (event)
126         if event.origin.type == "c2s" then
127                 event.features:add_child(get_server_caps_feature());
128         end
129 end);
130
131 -- Handle disco requests to user accounts
132 module:hook("iq/bare/http://jabber.org/protocol/disco#info:query", function(event)
133         local origin, stanza = event.origin, event.stanza;
134         if stanza.attr.type ~= "get" then return; end
135         local node = stanza.tags[1].attr.node;
136         if node and node ~= "" then return; end -- TODO fire event?
137         local username = jid_split(stanza.attr.to) or origin.username;
138         if not stanza.attr.to or is_contact_subscribed(username, module.host, jid_bare(stanza.attr.from)) then
139                 local reply = st.reply(stanza):tag('query', {xmlns='http://jabber.org/protocol/disco#info'});
140                 if not reply.attr.from then reply.attr.from = origin.username.."@"..origin.host; end -- COMPAT To satisfy Psi when querying own account
141                 module:fire_event("account-disco-info", { origin = origin, stanza = reply });
142                 origin.send(reply);
143                 return true;
144         end
145 end);
146 module:hook("iq/bare/http://jabber.org/protocol/disco#items:query", function(event)
147         local origin, stanza = event.origin, event.stanza;
148         if stanza.attr.type ~= "get" then return; end
149         local node = stanza.tags[1].attr.node;
150         if node and node ~= "" then return; end -- TODO fire event?
151         local username = jid_split(stanza.attr.to) or origin.username;
152         if not stanza.attr.to or is_contact_subscribed(username, module.host, jid_bare(stanza.attr.from)) then
153                 local reply = st.reply(stanza):tag('query', {xmlns='http://jabber.org/protocol/disco#items'});
154                 if not reply.attr.from then reply.attr.from = origin.username.."@"..origin.host; end -- COMPAT To satisfy Psi when querying own account
155                 module:fire_event("account-disco-items", { origin = origin, stanza = reply });
156                 origin.send(reply);
157                 return true;
158         end
159 end);