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 componentmanager_get_children = require "core.componentmanager".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 module:add_identity("server", "im", "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");
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" });
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;
51 for _,feature in ipairs(module:get_host_items("feature")) do
52 if not done[feature] then
53 query:tag("feature", {var=feature}):up();
57 _cached_server_disco_info = query;
58 _cached_server_caps_hash = calculate_hash(query);
59 _cached_server_caps_feature = st.stanza("c", {
60 xmlns = "http://jabber.org/protocol/caps";
62 node = "http://prosody.im";
63 ver = _cached_server_caps_hash;
66 local function clear_disco_cache()
67 _cached_server_disco_info, _cached_server_caps_feature, _cached_server_caps_hash = nil, nil, nil;
69 local function get_server_disco_info()
70 if not _cached_server_disco_info then build_server_disco_info(); end
71 return _cached_server_disco_info;
73 local function get_server_caps_feature()
74 if not _cached_server_caps_feature then build_server_disco_info(); end
75 return _cached_server_caps_feature;
77 local function get_server_caps_hash()
78 if not _cached_server_caps_hash then build_server_disco_info(); end
79 return _cached_server_caps_hash;
82 module:hook("item-added/identity", clear_disco_cache);
83 module:hook("item-added/feature", clear_disco_cache);
85 -- Handle disco requests to the server
86 module:hook("iq/host/http://jabber.org/protocol/disco#info:query", function(event)
87 local origin, stanza = event.origin, event.stanza;
88 if stanza.attr.type ~= "get" then return; end
89 local node = stanza.tags[1].attr.node;
90 if node and node ~= "" and node ~= "http://prosody.im#"..get_server_caps_hash() then return; end -- TODO fire event?
91 local reply_query = get_server_disco_info();
92 reply_query.node = node;
93 local reply = st.reply(stanza):add_child(reply_query);
97 module:hook("iq/host/http://jabber.org/protocol/disco#items:query", function(event)
98 local origin, stanza = event.origin, event.stanza;
99 if stanza.attr.type ~= "get" then return; end
100 local node = stanza.tags[1].attr.node;
101 if node and node ~= "" then return; end -- TODO fire event?
103 local reply = st.reply(stanza):query("http://jabber.org/protocol/disco#items");
104 for jid in pairs(componentmanager_get_children(module.host)) do
105 reply:tag("item", {jid = jid}):up();
107 for _, item in ipairs(disco_items) do
108 reply:tag("item", {jid=item[1], name=item[2]}):up();
114 -- Handle caps stream feature
115 module:hook("stream-features", function (event)
116 event.features:add_child(get_server_caps_feature());
119 -- Handle disco requests to user accounts
120 module:hook("iq/bare/http://jabber.org/protocol/disco#info: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 return; end -- TODO fire event?
125 local username = jid_split(stanza.attr.to) or origin.username;
126 if not stanza.attr.to or is_contact_subscribed(username, module.host, jid_bare(stanza.attr.from)) then
127 local reply = st.reply(stanza):tag('query', {xmlns='http://jabber.org/protocol/disco#info'});
128 if not reply.attr.from then reply.attr.from = origin.username.."@"..origin.host; end -- COMPAT To satisfy Psi when querying own account
129 module:fire_event("account-disco-info", { session = origin, stanza = reply });
134 module:hook("iq/bare/http://jabber.org/protocol/disco#items:query", function(event)
135 local origin, stanza = event.origin, event.stanza;
136 if stanza.attr.type ~= "get" then return; end
137 local node = stanza.tags[1].attr.node;
138 if node and node ~= "" then return; end -- TODO fire event?
139 local username = jid_split(stanza.attr.to) or origin.username;
140 if not stanza.attr.to or is_contact_subscribed(username, module.host, jid_bare(stanza.attr.from)) then
141 local reply = st.reply(stanza):tag('query', {xmlns='http://jabber.org/protocol/disco#items'});
142 if not reply.attr.from then reply.attr.from = origin.username.."@"..origin.host; end -- COMPAT To satisfy Psi when querying own account
143 module:fire_event("account-disco-items", { session = origin, stanza = reply });