mod_disco: Handle and fire events for service discovery queries for bare account...
[prosody.git] / plugins / mod_disco.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 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 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
15 module:add_identity("server", "im", "Prosody"); -- FIXME should be in the non-existing mod_router
16 module:add_feature("http://jabber.org/protocol/disco#info");
17 module:add_feature("http://jabber.org/protocol/disco#items");
18
19 module:hook("iq/host/http://jabber.org/protocol/disco#info:query", function(event)
20         local origin, stanza = event.origin, event.stanza;
21         if stanza.attr.type ~= "get" then return; end
22         local node = stanza.tags[1].attr.node;
23         if node and node ~= "" then return; end -- TODO fire event?
24
25         local reply = st.reply(stanza):query("http://jabber.org/protocol/disco#info");
26         local done = {};
27         for _,identity in ipairs(module:get_host_items("identity")) do
28                 local identity_s = identity.category.."\0"..identity.type;
29                 if not done[identity_s] then
30                         reply:tag("identity", identity):up();
31                         done[identity_s] = true;
32                 end
33         end
34         for _,feature in ipairs(module:get_host_items("feature")) do
35                 if not done[feature] then
36                         reply:tag("feature", {var=feature}):up();
37                         done[feature] = true;
38                 end
39         end
40         origin.send(reply);
41         return true;
42 end);
43 module:hook("iq/host/http://jabber.org/protocol/disco#items:query", function(event)
44         local origin, stanza = event.origin, event.stanza;
45         if stanza.attr.type ~= "get" then return; end
46         local node = stanza.tags[1].attr.node;
47         if node and node ~= "" then return; end -- TODO fire event?
48
49         local reply = st.reply(stanza):query("http://jabber.org/protocol/disco#items");
50         for jid in pairs(componentmanager_get_children(module.host)) do
51                 reply:tag("item", {jid = jid}):up();
52         end
53         origin.send(reply);
54         return true;
55 end);
56 module:hook("iq/bare/http://jabber.org/protocol/disco#info:query", function(event)
57         local origin, stanza = event.origin, event.stanza;
58         if stanza.attr.type ~= "get" then return; end
59         local node = stanza.tags[1].attr.node;
60         if node and node ~= "" then return; end -- TODO fire event?
61         local username = jid_split(stanza.attr.to) or origin.username;
62         if not stanza.attr.to or is_contact_subscribed(username, module.host, jid_bare(stanza.attr.from)) then
63                 local reply = st.reply(stanza):tag('query', {xmlns='http://jabber.org/protocol/disco#info'});
64                 if not reply.attr.from then reply.attr.from = origin.username.."@"..origin.host; end -- COMPAT To satisfy Psi when querying own account
65                 module:fire_event("account-disco-info", { session = origin, stanza = reply });
66                 origin.send(reply);
67                 return true;
68         end
69 end);
70 module:hook("iq/bare/http://jabber.org/protocol/disco#items:query", function(event)
71         local origin, stanza = event.origin, event.stanza;
72         if stanza.attr.type ~= "get" then return; end
73         local node = stanza.tags[1].attr.node;
74         if node and node ~= "" then return; end -- TODO fire event?
75         local username = jid_split(stanza.attr.to) or origin.username;
76         if not stanza.attr.to or is_contact_subscribed(username, module.host, jid_bare(stanza.attr.from)) then
77                 local reply = st.reply(stanza):tag('query', {xmlns='http://jabber.org/protocol/disco#items'});
78                 if not reply.attr.from then reply.attr.from = origin.username.."@"..origin.host; end -- COMPAT To satisfy Psi when querying own account
79                 module:fire_event("account-disco-items", { session = origin, stanza = reply });
80                 origin.send(reply);
81                 return true;
82         end
83 end);