Merge 0.10->trunk
[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 if module:get_host_type() == "local" then
36         module:add_identity("server", "im", module:get_option_string("name", "Prosody")); -- FIXME should be in the non-existing mod_router
37 end
38 module:add_feature("http://jabber.org/protocol/disco#info");
39 module:add_feature("http://jabber.org/protocol/disco#items");
40
41 -- Generate and cache disco result and caps hash
42 local _cached_server_disco_info, _cached_server_caps_feature, _cached_server_caps_hash;
43 local function build_server_disco_info()
44         local query = st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#info" });
45         local done = {};
46         for _,identity in ipairs(module:get_host_items("identity")) do
47                 local identity_s = identity.category.."\0"..identity.type;
48                 if not done[identity_s] then
49                         query:tag("identity", identity):up();
50                         done[identity_s] = true;
51                 end
52         end
53         for _,feature in ipairs(module:get_host_items("feature")) do
54                 if not done[feature] then
55                         query:tag("feature", {var=feature}):up();
56                         done[feature] = true;
57                 end
58         end
59         for _,extension in ipairs(module:get_host_items("extension")) do
60                 if not done[extension] then
61                         query:add_child(extension);
62                         done[extension] = true;
63                 end
64         end
65         _cached_server_disco_info = query;
66         _cached_server_caps_hash = calculate_hash(query);
67         _cached_server_caps_feature = st.stanza("c", {
68                 xmlns = "http://jabber.org/protocol/caps";
69                 hash = "sha-1";
70                 node = "http://prosody.im";
71                 ver = _cached_server_caps_hash;
72         });
73 end
74 local function clear_disco_cache()
75         _cached_server_disco_info, _cached_server_caps_feature, _cached_server_caps_hash = nil, nil, nil;
76 end
77 local function get_server_disco_info()
78         if not _cached_server_disco_info then build_server_disco_info(); end
79         return _cached_server_disco_info;
80 end
81 local function get_server_caps_feature()
82         if not _cached_server_caps_feature then build_server_disco_info(); end
83         return _cached_server_caps_feature;
84 end
85 local function get_server_caps_hash()
86         if not _cached_server_caps_hash then build_server_disco_info(); end
87         return _cached_server_caps_hash;
88 end
89
90 module:hook("item-added/identity", clear_disco_cache);
91 module:hook("item-added/feature", clear_disco_cache);
92 module:hook("item-added/extension", clear_disco_cache);
93 module:hook("item-removed/identity", clear_disco_cache);
94 module:hook("item-removed/feature", clear_disco_cache);
95 module:hook("item-removed/extension", clear_disco_cache);
96
97 -- Handle disco requests to the server
98 module:hook("iq/host/http://jabber.org/protocol/disco#info:query", function(event)
99         local origin, stanza = event.origin, event.stanza;
100         if stanza.attr.type ~= "get" then return; end
101         local node = stanza.tags[1].attr.node;
102         if node and node ~= "" and node ~= "http://prosody.im#"..get_server_caps_hash() then
103                 local reply = st.reply(stanza):tag('query', {xmlns='http://jabber.org/protocol/disco#info', node=node});
104                 local event = { origin = origin, stanza = stanza, reply = reply, node = node, exists = false};
105                 local ret = module:fire_event("host-disco-info-node", event);
106                 if ret ~= nil then return ret; end
107                 if event.exists then
108                         origin.send(reply);
109                 else
110                         origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Node does not exist"));
111                 end
112                 return true;
113         end
114         local reply_query = get_server_disco_info();
115         reply_query.node = node;
116         local reply = st.reply(stanza):add_child(reply_query);
117         origin.send(reply);
118         return true;
119 end);
120 module:hook("iq/host/http://jabber.org/protocol/disco#items:query", function(event)
121         local origin, stanza = event.origin, event.stanza;
122         if stanza.attr.type ~= "get" then return; end
123         local node = stanza.tags[1].attr.node;
124         if node and node ~= "" then
125                 local reply = st.reply(stanza):tag('query', {xmlns='http://jabber.org/protocol/disco#items', node=node});
126                 local event = { origin = origin, stanza = stanza, reply = reply, node = node, exists = false};
127                 local ret = module:fire_event("host-disco-items-node", event);
128                 if ret ~= nil then return ret; end
129                 if event.exists then
130                         origin.send(reply);
131                 else
132                         origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Node does not exist"));
133                 end
134                 return true;
135         end
136         local reply = st.reply(stanza):query("http://jabber.org/protocol/disco#items");
137         local ret = module:fire_event("host-disco-items", { origin = origin, stanza = stanza, reply = reply });
138         if ret ~= nil then return ret; end
139         for jid, name in pairs(get_children(module.host)) do
140                 reply:tag("item", {jid = jid, name = name~=true and name or nil}):up();
141         end
142         for _, item in ipairs(disco_items) do
143                 reply:tag("item", {jid=item[1], name=item[2]}):up();
144         end
145         origin.send(reply);
146         return true;
147 end);
148
149 -- Handle caps stream feature
150 module:hook("stream-features", function (event)
151         if event.origin.type == "c2s" then
152                 event.features:add_child(get_server_caps_feature());
153         end
154 end);
155
156 -- Handle disco requests to user accounts
157 module:hook("iq/bare/http://jabber.org/protocol/disco#info:query", function(event)
158         local origin, stanza = event.origin, event.stanza;
159         if stanza.attr.type ~= "get" then return; end
160         local node = stanza.tags[1].attr.node;
161         local username = jid_split(stanza.attr.to) or origin.username;
162         if not stanza.attr.to or is_contact_subscribed(username, module.host, jid_bare(stanza.attr.from)) then
163                 if node and node ~= "" then
164                         local reply = st.reply(stanza):tag('query', {xmlns='http://jabber.org/protocol/disco#info', node=node});
165                         if not reply.attr.from then reply.attr.from = origin.username.."@"..origin.host; end -- COMPAT To satisfy Psi when querying own account
166                         local event = { origin = origin, stanza = stanza, reply = reply, node = node, exists = false};
167                         local ret = module:fire_event("account-disco-info-node", event);
168                         if ret ~= nil then return ret; end
169                         if event.exists then
170                                 origin.send(reply);
171                         else
172                                 origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Node does not exist"));
173                         end
174                         return true;
175                 end
176                 local reply = st.reply(stanza):tag('query', {xmlns='http://jabber.org/protocol/disco#info'});
177                 if not reply.attr.from then reply.attr.from = origin.username.."@"..origin.host; end -- COMPAT To satisfy Psi when querying own account
178                 module:fire_event("account-disco-info", { origin = origin, reply = reply });
179                 origin.send(reply);
180                 return true;
181         end
182 end);
183 module:hook("iq/bare/http://jabber.org/protocol/disco#items:query", function(event)
184         local origin, stanza = event.origin, event.stanza;
185         if stanza.attr.type ~= "get" then return; end
186         local node = stanza.tags[1].attr.node;
187         local username = jid_split(stanza.attr.to) or origin.username;
188         if not stanza.attr.to or is_contact_subscribed(username, module.host, jid_bare(stanza.attr.from)) then
189                 if node and node ~= "" then
190                         local reply = st.reply(stanza):tag('query', {xmlns='http://jabber.org/protocol/disco#items', node=node});
191                         if not reply.attr.from then reply.attr.from = origin.username.."@"..origin.host; end -- COMPAT To satisfy Psi when querying own account
192                         local event = { origin = origin, stanza = stanza, reply = reply, node = node, exists = false};
193                         local ret = module:fire_event("account-disco-items-node", event);
194                         if ret ~= nil then return ret; end
195                         if event.exists then
196                                 origin.send(reply);
197                         else
198                                 origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Node does not exist"));
199                         end
200                         return true;
201                 end
202                 local reply = st.reply(stanza):tag('query', {xmlns='http://jabber.org/protocol/disco#items'});
203                 if not reply.attr.from then reply.attr.from = origin.username.."@"..origin.host; end -- COMPAT To satisfy Psi when querying own account
204                 module:fire_event("account-disco-items", { origin = origin, stanza = stanza, reply = reply });
205                 origin.send(reply);
206                 return true;
207         end
208 end);