# HG changeset patch
# User Waqas Hussain <waqas20@gmail.com>
# Date 1261358712 -18000
# Node ID 29a30884aadd8720f1b40db7dcdbc7ede2f73d6a
# Parent  5e37e28a429f6d541ad5640aa391f5f1982e6a53
mod_disco: Handle and fire events for service discovery queries for bare account JIDs (thanks darkrain).

diff -r 5e37e28a429f -r 29a30884aadd plugins/mod_disco.lua
--- a/plugins/mod_disco.lua	Thu Dec 17 21:50:02 2009 +0000
+++ b/plugins/mod_disco.lua	Mon Dec 21 06:25:12 2009 +0500
@@ -7,6 +7,9 @@
 --
 
 local componentmanager_get_children = require "core.componentmanager".get_children;
+local is_contact_subscribed = require "core.rostermanager".is_contact_subscribed;
+local jid_split = require "util.jid".split;
+local jid_bare = require "util.jid".bare;
 local st = require "util.stanza"
 
 module:add_identity("server", "im", "Prosody"); -- FIXME should be in the non-existing mod_router
@@ -50,3 +53,31 @@
 	origin.send(reply);
 	return true;
 end);
+module:hook("iq/bare/http://jabber.org/protocol/disco#info:query", function(event)
+	local origin, stanza = event.origin, event.stanza;
+	if stanza.attr.type ~= "get" then return; end
+	local node = stanza.tags[1].attr.node;
+	if node and node ~= "" then return; end -- TODO fire event?
+	local username = jid_split(stanza.attr.to) or origin.username;
+	if not stanza.attr.to or is_contact_subscribed(username, module.host, jid_bare(stanza.attr.from)) then
+		local reply = st.reply(stanza):tag('query', {xmlns='http://jabber.org/protocol/disco#info'});
+		if not reply.attr.from then reply.attr.from = origin.username.."@"..origin.host; end -- COMPAT To satisfy Psi when querying own account
+		module:fire_event("account-disco-info", { session = origin, stanza = reply });
+		origin.send(reply);
+		return true;
+	end
+end);
+module:hook("iq/bare/http://jabber.org/protocol/disco#items:query", function(event)
+	local origin, stanza = event.origin, event.stanza;
+	if stanza.attr.type ~= "get" then return; end
+	local node = stanza.tags[1].attr.node;
+	if node and node ~= "" then return; end -- TODO fire event?
+	local username = jid_split(stanza.attr.to) or origin.username;
+	if not stanza.attr.to or is_contact_subscribed(username, module.host, jid_bare(stanza.attr.from)) then
+		local reply = st.reply(stanza):tag('query', {xmlns='http://jabber.org/protocol/disco#items'});
+		if not reply.attr.from then reply.attr.from = origin.username.."@"..origin.host; end -- COMPAT To satisfy Psi when querying own account
+		module:fire_event("account-disco-items", { session = origin, stanza = reply });
+		origin.send(reply);
+		return true;
+	end
+end);