comparison mod_throttle_presence/mod_throttle_presence.lua @ 1485:d8c50448d0e7

mod_throttle_presence: Remove timer support, depend on mod_csi to enable/disable. Untested, but the previous version was definitely broken anyway.
author Matthew Wild <mwild1@gmail.com>
date Fri, 15 Aug 2014 19:46:44 +0100
parents fe9c4daee076
children ba97f9be4f76
comparison
equal deleted inserted replaced
1484:53a3a19d6093 1485:d8c50448d0e7
1 local add_filter = require "util.filters".add_filter; 1 local filters = require "util.filters";
2 local add_task = require "util.timer".add_task; 2 local st = require "util.stanza";
3 3
4 local buffer_seconds = module:get_option_number("flush_presence_seconds"); 4 module:depends("csi");
5 5
6 local function throttle_session(data) 6 local function presence_filter(stanza, session)
7 local session = data.session; 7 local buffer = session.presence_buffer;
8 local buffer, flushing = {}, false; 8 local from = stanza.attr.from;
9 local timer_active = false; 9 if stanza.name ~= "presence" or (stanza.attr.type and stanza.attr.type ~= "unavailable") then
10 local function flush_buffer() 10 local cached_presence = buffer[stanza.attr.from];
11 module:log("debug", "Flushing buffer for %s", session.full_jid); 11 if cached_presence then
12 flushing = true; 12 module:log("debug", "Important stanza for %s from %s, flushing presence", session.full_jid, from);
13 for jid, presence in pairs(buffer) do 13 session.send(cached_presence);
14 session.send(presence); 14 buffer[stanza.attr.from] = nil;
15 end 15 end
16 flushing = false; 16 else
17 module:log("debug", "Buffering presence stanza from %s to %s", stanza.attr.from, session.full_jid);
18 session.buffer[stanza.attr.from] = st.clone(stanza);
19 return nil; -- Drop this stanza (we've stored it for later)
17 end 20 end
18 local function throttle_presence(stanza) 21 return 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 22 end
35 23
24 local function throttle_session(event)
25 local session = event.session;
26 if session.presence_buffer then return; end
27 module:log("debug", "Suppressing presence updates to %s", session.full_jid);
28 session.presence_buffer = {};
29 filters.add_filter(session, "stanzas/out", presence_filter);
30 end
36 31
37 module:hook("resource-bind", throttle_session); 32 local function restore_session(event)
33 local session = event.session;
34 if not session.presence_buffer then return; end
35 filters.remove_filter(session, "stanzas/out", presence_filter);
36 module:log("debug", "Flushing buffer for %s", session.full_jid);
37 for jid, presence in pairs(session.presence_buffer) do
38 session.send(presence);
39 end
40 session.presence_buffer = nil;
41 end
42
43 module:hook("csi-client-inactive", throttle_session);
44 module:hook("csi-client-active", restore_session);