Diff

plugins/mod_disco.lua @ 3343:c70c6d5bf270

mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
author Matthew Wild <mwild1@gmail.com>
date Fri, 09 Jul 2010 13:20:00 +0100
parent 2925:692b3c6c5bd2
child 3347:99f56bed5228
line wrap: on
line diff
--- a/plugins/mod_disco.lua	Fri Jul 09 13:18:42 2010 +0100
+++ b/plugins/mod_disco.lua	Fri Jul 09 13:20:00 2010 +0100
@@ -11,6 +11,7 @@
 local jid_split = require "util.jid".split;
 local jid_bare = require "util.jid".bare;
 local st = require "util.stanza"
+local calculate_hash = require "util.caps".calculate_hash;
 
 local disco_items = module:get_option("disco_items") or {};
 do -- validate disco_items
@@ -35,27 +36,31 @@
 module:add_feature("http://jabber.org/protocol/disco#info");
 module:add_feature("http://jabber.org/protocol/disco#items");
 
+-- Handle disco requests to the server
+
+local function build_server_disco_info(stanza)
+	local done = {};
+	for _,identity in ipairs(module:get_host_items("identity")) do
+		local identity_s = identity.category.."\0"..identity.type;
+		if not done[identity_s] then
+			stanza:tag("identity", identity):up();
+			done[identity_s] = true;
+		end
+	end
+	for _,feature in ipairs(module:get_host_items("feature")) do
+		if not done[feature] then
+			stanza:tag("feature", {var=feature}):up();
+			done[feature] = true;
+		end
+	end
+end
 module:hook("iq/host/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 reply = st.reply(stanza):query("http://jabber.org/protocol/disco#info");
-	local done = {};
-	for _,identity in ipairs(module:get_host_items("identity")) do
-		local identity_s = identity.category.."\0"..identity.type;
-		if not done[identity_s] then
-			reply:tag("identity", identity):up();
-			done[identity_s] = true;
-		end
-	end
-	for _,feature in ipairs(module:get_host_items("feature")) do
-		if not done[feature] then
-			reply:tag("feature", {var=feature}):up();
-			done[feature] = true;
-		end
-	end
+	build_server_disco_info(reply);
 	origin.send(reply);
 	return true;
 end);
@@ -75,6 +80,31 @@
 	origin.send(reply);
 	return true;
 end);
+
+-- Server caps hash calculation
+local caps_hash_feature;
+
+local function recalculate_server_caps()
+	local caps_hash = calculate_hash(st.stanza());
+	caps_hash_feature = st.stanza("c", {
+		xmlns = "http://jabber.org/protocol/caps";
+		hash = "sha-1";
+		node = "http://prosody.im";
+		ver = caps_hash;
+	});
+end
+recalculate_server_caps();
+
+module:hook("item-added/identity", recalculate_server_caps);
+module:hook("item-added/feature", recalculate_server_caps);
+
+module:hook("stream-features", function (event)
+	if caps_hash_feature then
+		event.features:add_child(caps_hash_feature);
+	end
+end);
+
+-- Handle disco requests to user accounts
 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