plugins.disco: Automatically insert caps into outgoing presence, and re-send last global presence with new caps when features change

Wed, 15 Dec 2010 14:56:10 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Wed, 15 Dec 2010 14:56:10 +0000
changeset 167
a2ae7a9d360f
parent 166
3499b4ea3277
child 168
7285e04a4797

plugins.disco: Automatically insert caps into outgoing presence, and re-send last global presence with new caps when features change

plugins/disco.lua file | annotate | diff | comparison | revisions
--- a/plugins/disco.lua	Wed Dec 15 14:52:37 2010 +0000
+++ b/plugins/disco.lua	Wed Dec 15 14:56:10 2010 +0000
@@ -30,6 +30,8 @@
 	stream.caps = {}
 	stream.caps.node = 'http://code.matthewwild.co.uk/verse/'
 
+	local _resend_presence; -- Forward declaration of a function
+
 	local function cmp_identity(item1, item2)
 		if item1.category < item2.category then
 			return true;
@@ -87,13 +89,15 @@
 	})
 	
 	function stream:add_disco_feature(feature)
-		table.insert(self.disco.info.features, {var=feature});		
+		table.insert(self.disco.info.features, {var=feature});
+		_resend_presence();
 	end
 	
 	function stream:remove_disco_feature(feature)
 		for idx, disco_feature in ipairs(self.disco.info.features) do
 			if disco_feature.var == feature then
 				table.remove(self.disco.info.features, idx);
+				_resend_presence();
 				return true;
 			end
 		end
@@ -370,6 +374,30 @@
 		end);
 		return true;
 	end, 5);
+	
+	local last_presence; -- Cache to re-send with updated caps
+	
+	stream:hook("presence-out", function (presence)
+		if not presence:get_child("c", xmlns_caps) then
+			presence:reset():add_child(stream:caps()):reset();
+		end
+		if not presence.attr.to then
+			last_presence = presence; -- Cache non-directed presence
+		end
+	end);
+	
+	local function update_caps(tag)
+		if tag.name == "c" and tag.attr.xmlns == xmlns_caps then
+			return stream:caps();
+		end
+	end
+	
+	function _resend_presence() -- Local via forward declaration
+		if last_presence then
+			last_presence = last_presence:maptags(update_caps);
+			stream:send(last_presence);
+		end
+	end
 end
 
 -- end of disco.lua

mercurial