2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
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;
16 local disco_items = module:get_option("disco_items") or {};
17 do -- validate disco_items
18 for _, item in ipairs(disco_items) do
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";
28 module:log("error", "option disco_items is malformed: %s", err);
29 disco_items = {}; -- TODO clean up data instead of removing it?
35 if module:get_host_type() == "normal" then
36 module:add_identity("server", "im", module:get_option_string("name", "Prosody")); -- FIXME should be in the non-existing mod_router
38 module:add_feature("http://jabber.org/protocol/disco#info");
39 module:add_feature("http://jabber.org/protocol/disco#items");
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" });
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;
53 for _,feature in ipairs(module:get_host_items("feature")) do
54 if not done[feature] then
55 query:tag("feature", {var=feature}):up();
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;
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";
70 node = "http://prosody.im";
71 ver = _cached_server_caps_hash;
74 local function clear_disco_cache()
75 _cached_server_disco_info, _cached_server_caps_feature, _cached_server_caps_hash = nil, nil, nil;
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;
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;
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;
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);
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
110 origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Node does not exist"));
114 local reply_query = get_server_disco_info();
115 reply_query.node = node;
116 local reply = st.reply(stanza):add_child(reply_query);
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
132 origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Node does not exist"));
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();
142 for _, item in ipairs(disco_items) do
143 reply:tag("item", {jid=item[1], name=item[2]}):up();
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());
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
172 origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Node does not exist"));
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 });
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
198 origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Node does not exist"));
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 });