Mercurial > prosody-modules
annotate mod_throttle_presence/mod_throttle_presence.lua @ 1493:d5e8758d391d
mod_lastlog: Fix traceback if no session included with event (eg from mod_register_web) (thanks biszkopcik)
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 25 Aug 2014 00:38:14 +0200 |
parents | 95f10fb7f60e |
children | b8164f18142a |
rev | line source |
---|---|
1485
d8c50448d0e7
mod_throttle_presence: Remove timer support, depend on mod_csi to enable/disable. Untested, but the previous version was definitely broken anyway.
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
1 local filters = require "util.filters"; |
d8c50448d0e7
mod_throttle_presence: Remove timer support, depend on mod_csi to enable/disable. Untested, but the previous version was definitely broken anyway.
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
2 local st = require "util.stanza"; |
162
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 |
1485
d8c50448d0e7
mod_throttle_presence: Remove timer support, depend on mod_csi to enable/disable. Untested, but the previous version was definitely broken anyway.
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
4 module:depends("csi"); |
162
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 |
1485
d8c50448d0e7
mod_throttle_presence: Remove timer support, depend on mod_csi to enable/disable. Untested, but the previous version was definitely broken anyway.
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
6 local function presence_filter(stanza, session) |
d8c50448d0e7
mod_throttle_presence: Remove timer support, depend on mod_csi to enable/disable. Untested, but the previous version was definitely broken anyway.
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
7 local buffer = session.presence_buffer; |
d8c50448d0e7
mod_throttle_presence: Remove timer support, depend on mod_csi to enable/disable. Untested, but the previous version was definitely broken anyway.
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
8 local from = stanza.attr.from; |
d8c50448d0e7
mod_throttle_presence: Remove timer support, depend on mod_csi to enable/disable. Untested, but the previous version was definitely broken anyway.
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
9 if stanza.name ~= "presence" or (stanza.attr.type and stanza.attr.type ~= "unavailable") then |
d8c50448d0e7
mod_throttle_presence: Remove timer support, depend on mod_csi to enable/disable. Untested, but the previous version was definitely broken anyway.
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
10 local cached_presence = buffer[stanza.attr.from]; |
d8c50448d0e7
mod_throttle_presence: Remove timer support, depend on mod_csi to enable/disable. Untested, but the previous version was definitely broken anyway.
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
11 if cached_presence then |
d8c50448d0e7
mod_throttle_presence: Remove timer support, depend on mod_csi to enable/disable. Untested, but the previous version was definitely broken anyway.
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
12 module:log("debug", "Important stanza for %s from %s, flushing presence", session.full_jid, from); |
d8c50448d0e7
mod_throttle_presence: Remove timer support, depend on mod_csi to enable/disable. Untested, but the previous version was definitely broken anyway.
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
13 session.send(cached_presence); |
d8c50448d0e7
mod_throttle_presence: Remove timer support, depend on mod_csi to enable/disable. Untested, but the previous version was definitely broken anyway.
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
14 buffer[stanza.attr.from] = nil; |
162
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 end |
1485
d8c50448d0e7
mod_throttle_presence: Remove timer support, depend on mod_csi to enable/disable. Untested, but the previous version was definitely broken anyway.
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
16 else |
d8c50448d0e7
mod_throttle_presence: Remove timer support, depend on mod_csi to enable/disable. Untested, but the previous version was definitely broken anyway.
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
17 module:log("debug", "Buffering presence stanza from %s to %s", stanza.attr.from, session.full_jid); |
1489
95f10fb7f60e
mod_throttle_presence: Use local buffer variable, fixes a traceback (thanks Ge0rG)
Kim Alvefur <zash@zash.se>
parents:
1488
diff
changeset
|
18 buffer[stanza.attr.from] = st.clone(stanza); |
1485
d8c50448d0e7
mod_throttle_presence: Remove timer support, depend on mod_csi to enable/disable. Untested, but the previous version was definitely broken anyway.
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
19 return nil; -- Drop this stanza (we've stored it for later) |
162
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 end |
1485
d8c50448d0e7
mod_throttle_presence: Remove timer support, depend on mod_csi to enable/disable. Untested, but the previous version was definitely broken anyway.
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
21 return stanza; |
162
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 end |
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 |
1485
d8c50448d0e7
mod_throttle_presence: Remove timer support, depend on mod_csi to enable/disable. Untested, but the previous version was definitely broken anyway.
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
24 local function throttle_session(event) |
1488
ba97f9be4f76
mod_throttle_presence: Fix traceback (thanks Ge0rG)
Kim Alvefur <zash@zash.se>
parents:
1485
diff
changeset
|
25 local session = event.origin; |
1485
d8c50448d0e7
mod_throttle_presence: Remove timer support, depend on mod_csi to enable/disable. Untested, but the previous version was definitely broken anyway.
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
26 if session.presence_buffer then return; end |
d8c50448d0e7
mod_throttle_presence: Remove timer support, depend on mod_csi to enable/disable. Untested, but the previous version was definitely broken anyway.
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
27 module:log("debug", "Suppressing presence updates to %s", session.full_jid); |
d8c50448d0e7
mod_throttle_presence: Remove timer support, depend on mod_csi to enable/disable. Untested, but the previous version was definitely broken anyway.
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
28 session.presence_buffer = {}; |
d8c50448d0e7
mod_throttle_presence: Remove timer support, depend on mod_csi to enable/disable. Untested, but the previous version was definitely broken anyway.
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
29 filters.add_filter(session, "stanzas/out", presence_filter); |
d8c50448d0e7
mod_throttle_presence: Remove timer support, depend on mod_csi to enable/disable. Untested, but the previous version was definitely broken anyway.
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
30 end |
162
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 |
1485
d8c50448d0e7
mod_throttle_presence: Remove timer support, depend on mod_csi to enable/disable. Untested, but the previous version was definitely broken anyway.
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
32 local function restore_session(event) |
1488
ba97f9be4f76
mod_throttle_presence: Fix traceback (thanks Ge0rG)
Kim Alvefur <zash@zash.se>
parents:
1485
diff
changeset
|
33 local session = event.origin; |
1485
d8c50448d0e7
mod_throttle_presence: Remove timer support, depend on mod_csi to enable/disable. Untested, but the previous version was definitely broken anyway.
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
34 if not session.presence_buffer then return; end |
d8c50448d0e7
mod_throttle_presence: Remove timer support, depend on mod_csi to enable/disable. Untested, but the previous version was definitely broken anyway.
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
35 filters.remove_filter(session, "stanzas/out", presence_filter); |
d8c50448d0e7
mod_throttle_presence: Remove timer support, depend on mod_csi to enable/disable. Untested, but the previous version was definitely broken anyway.
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
36 module:log("debug", "Flushing buffer for %s", session.full_jid); |
d8c50448d0e7
mod_throttle_presence: Remove timer support, depend on mod_csi to enable/disable. Untested, but the previous version was definitely broken anyway.
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
37 for jid, presence in pairs(session.presence_buffer) do |
d8c50448d0e7
mod_throttle_presence: Remove timer support, depend on mod_csi to enable/disable. Untested, but the previous version was definitely broken anyway.
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
38 session.send(presence); |
d8c50448d0e7
mod_throttle_presence: Remove timer support, depend on mod_csi to enable/disable. Untested, but the previous version was definitely broken anyway.
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
39 end |
d8c50448d0e7
mod_throttle_presence: Remove timer support, depend on mod_csi to enable/disable. Untested, but the previous version was definitely broken anyway.
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
40 session.presence_buffer = nil; |
d8c50448d0e7
mod_throttle_presence: Remove timer support, depend on mod_csi to enable/disable. Untested, but the previous version was definitely broken anyway.
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
41 end |
d8c50448d0e7
mod_throttle_presence: Remove timer support, depend on mod_csi to enable/disable. Untested, but the previous version was definitely broken anyway.
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
42 |
d8c50448d0e7
mod_throttle_presence: Remove timer support, depend on mod_csi to enable/disable. Untested, but the previous version was definitely broken anyway.
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
43 module:hook("csi-client-inactive", throttle_session); |
d8c50448d0e7
mod_throttle_presence: Remove timer support, depend on mod_csi to enable/disable. Untested, but the previous version was definitely broken anyway.
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
44 module:hook("csi-client-active", restore_session); |