Comparison

plugins/disco.lua @ 179:5e2ff25c1dd9

plugins.disco: Use the new presence plugin for caching and resending
author Kim Alvefur <zash@zash.se>
date Fri, 31 Dec 2010 02:12:27 +0100
parent 176:6004486e8b6c
child 180:58537eb98506
comparison
equal deleted inserted replaced
178:2189f75e09b9 179:5e2ff25c1dd9
29 stream.disco.nodes = {} 29 stream.disco.nodes = {}
30 30
31 stream.caps = {} 31 stream.caps = {}
32 stream.caps.node = 'http://code.matthewwild.co.uk/verse/' 32 stream.caps.node = 'http://code.matthewwild.co.uk/verse/'
33 33
34 local _resend_presence; -- Forward declaration of a function
35
36 local function cmp_identity(item1, item2) 34 local function cmp_identity(item1, item2)
37 if item1.category < item2.category then 35 if item1.category < item2.category then
38 return true; 36 return true;
39 elseif item2.category < item1.category then 37 elseif item2.category < item1.category then
40 return false; 38 return false;
89 end 87 end
90 }) 88 })
91 89
92 function stream:add_disco_feature(feature) 90 function stream:add_disco_feature(feature)
93 table.insert(self.disco.info.features, {var=feature}); 91 table.insert(self.disco.info.features, {var=feature});
94 _resend_presence(); 92 stream:resend_presence();
95 end 93 end
96 94
97 function stream:remove_disco_feature(feature) 95 function stream:remove_disco_feature(feature)
98 for idx, disco_feature in ipairs(self.disco.info.features) do 96 for idx, disco_feature in ipairs(self.disco.info.features) do
99 if disco_feature.var == feature then 97 if disco_feature.var == feature then
100 table.remove(self.disco.info.features, idx); 98 table.remove(self.disco.info.features, idx);
101 _resend_presence(); 99 stream:resend_presence();
102 return true; 100 return true;
103 end 101 end
104 end 102 end
105 end 103 end
106 104
377 stream:event("ready"); 375 stream:event("ready");
378 end); 376 end);
379 return true; 377 return true;
380 end, 5); 378 end, 5);
381 379
382 local last_presence; -- Cache to re-send with updated caps
383
384 stream:hook("presence-out", function (presence) 380 stream:hook("presence-out", function (presence)
385 if not presence:get_child("c", xmlns_caps) then 381 if not presence:get_child("c", xmlns_caps) then
386 presence:reset():add_child(stream:caps()):reset(); 382 presence:reset():add_child(stream:caps()):reset();
387 end 383 end
388 if not presence.attr.to then 384 end, 10);
389 last_presence = presence; -- Cache non-directed presence
390 end
391 end);
392
393 local function update_caps(tag)
394 if tag.name == "c" and tag.attr.xmlns == xmlns_caps then
395 return stream:caps();
396 end
397 end
398
399 function _resend_presence() -- Local via forward declaration
400 if last_presence then
401 last_presence = last_presence:maptags(update_caps);
402 stream:send(last_presence);
403 end
404 end
405 end 385 end
406 386
407 -- end of disco.lua 387 -- end of disco.lua