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 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 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;
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";
68 node = "http://prosody.im";
69 ver = _cached_server_caps_hash;
72 local function clear_disco_cache()
73 _cached_server_disco_info, _cached_server_caps_feature, _cached_server_caps_hash = nil, nil, nil;
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;
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;
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;
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);
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);
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?
113 local reply = st.reply(stanza):query("http://jabber.org/protocol/disco#items");
114 for jid in pairs(get_children(module.host)) do
115 reply:tag("item", {jid = jid}):up();
117 for _, item in ipairs(disco_items) do
118 reply:tag("item", {jid=item[1], name=item[2]}):up();
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());
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 });
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 });