comparison mod_throttle_presence/mod_throttle_presence.lua @ 162:fe9c4daee076

mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
author Matthew Wild <mwild1@gmail.com>
date Thu, 03 Jun 2010 03:02:33 +0100
parents
children d8c50448d0e7
comparison
equal deleted inserted replaced
161:fda7faee7677 162:fe9c4daee076
1 local add_filter = require "util.filters".add_filter;
2 local add_task = require "util.timer".add_task;
3
4 local buffer_seconds = module:get_option_number("flush_presence_seconds");
5
6 local function throttle_session(data)
7 local session = data.session;
8 local buffer, flushing = {}, false;
9 local timer_active = false;
10 local function flush_buffer()
11 module:log("debug", "Flushing buffer for %s", session.full_jid);
12 flushing = true;
13 for jid, presence in pairs(buffer) do
14 session.send(presence);
15 end
16 flushing = false;
17 end
18 local function throttle_presence(stanza)
19 if stanza.name ~= "presence" or (stanza.attr.type and stanza.attr.type ~= "unavailable") then
20 module:log("debug", "Non-presence stanza for %s: %s", session.full_jid, tostring(stanza));
21 flush_buffer();
22 elseif not flushing then
23 module:log("debug", "Buffering presence stanza from %s to %s", stanza.attr.from, session.full_jid);
24 buffer[stanza.attr.from] = stanza;
25 if not timer_active and buffer_seconds then
26 timer_active = true;
27 add_task(buffer_seconds, flush_buffer);
28 end
29 return nil; -- Drop this stanza (we've stored it for later)
30 end
31 return stanza;
32 end
33 add_filter(session, "stanzas/out", throttle_presence);
34 end
35
36
37 module:hook("resource-bind", throttle_session);