mod_tls: Switch to : syntax for connection methods
[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 st = require "util.stanza"
11
12 module:add_identity("server", "im", "Prosody"); -- FIXME should be in the non-existing mod_router
13 module:add_feature("http://jabber.org/protocol/disco#info");
14 module:add_feature("http://jabber.org/protocol/disco#items");
15
16 module:hook("iq/host/http://jabber.org/protocol/disco#info:query", function(event)
17         local origin, stanza = event.origin, event.stanza;
18         if stanza.attr.type ~= "get" then return; end
19         local node = stanza.tags[1].attr.node;
20         if node and node ~= "" then return; end -- TODO fire event?
21
22         local reply = st.reply(stanza):query("http://jabber.org/protocol/disco#info");
23         local done = {};
24         for _,identity in ipairs(module:get_host_items("identity")) do
25                 local identity_s = identity.category.."\0"..identity.type;
26                 if not done[identity_s] then
27                         reply:tag("identity", identity):up();
28                         done[identity_s] = true;
29                 end
30         end
31         for _,feature in ipairs(module:get_host_items("feature")) do
32                 if not done[feature] then
33                         reply:tag("feature", {var=feature}):up();
34                         done[feature] = true;
35                 end
36         end
37         origin.send(reply);
38         return true;
39 end);
40 module:hook("iq/host/http://jabber.org/protocol/disco#items:query", function(event)
41         local origin, stanza = event.origin, event.stanza;
42         if stanza.attr.type ~= "get" then return; end
43         local node = stanza.tags[1].attr.node;
44         if node and node ~= "" then return; end -- TODO fire event?
45
46         local reply = st.reply(stanza):query("http://jabber.org/protocol/disco#items");
47         for jid in pairs(componentmanager_get_children(module.host)) do
48                 reply:tag("item", {jid = jid}):up();
49         end
50         origin.send(reply);
51         return true;
52 end);