# HG changeset patch # User Matthew Wild # Date 1292424970 0 # Node ID a2ae7a9d360f672d0704539d273a5c95962b0a22 # Parent 3499b4ea32775ddea9de262d3c8822e9271fa663 plugins.disco: Automatically insert caps into outgoing presence, and re-send last global presence with new caps when features change diff -r 3499b4ea3277 -r a2ae7a9d360f plugins/disco.lua --- 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