Mercurial > prosody-modules
annotate mod_throttle_presence/mod_throttle_presence.lua @ 2436:a01a3fb96302
mod_measure_memory: Module is global, no need for a global hook
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 06 Jan 2017 17:07:01 +0100 |
parents | 552faee596b7 |
children |
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) |
1867
34468d3bfcac
mod_throttle_presence: Skip past things that are not stanza objects
Kim Alvefur <zash@zash.se>
parents:
1504
diff
changeset
|
7 if getmetatable(stanza) ~= st.stanza_mt then |
34468d3bfcac
mod_throttle_presence: Skip past things that are not stanza objects
Kim Alvefur <zash@zash.se>
parents:
1504
diff
changeset
|
8 return stanza; -- Things we don't want to touch |
34468d3bfcac
mod_throttle_presence: Skip past things that are not stanza objects
Kim Alvefur <zash@zash.se>
parents:
1504
diff
changeset
|
9 end |
1504
b8164f18142a
mod_throttle_presence: Make sure flushed stanzas get through the filter
Kim Alvefur <zash@zash.se>
parents:
1489
diff
changeset
|
10 if stanza._flush then |
b8164f18142a
mod_throttle_presence: Make sure flushed stanzas get through the filter
Kim Alvefur <zash@zash.se>
parents:
1489
diff
changeset
|
11 stanza._flush = nil; |
b8164f18142a
mod_throttle_presence: Make sure flushed stanzas get through the filter
Kim Alvefur <zash@zash.se>
parents:
1489
diff
changeset
|
12 return stanza; |
b8164f18142a
mod_throttle_presence: Make sure flushed stanzas get through the filter
Kim Alvefur <zash@zash.se>
parents:
1489
diff
changeset
|
13 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
|
14 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
|
15 local from = stanza.attr.from; |
1925
552faee596b7
mod_throttle_presence: Switch if-else statement around to improve readability
Kim Alvefur <zash@zash.se>
parents:
1867
diff
changeset
|
16 if stanza.name == "presence" and (stanza.attr.type == nil or stanza.attr.type == "unavailable") then |
552faee596b7
mod_throttle_presence: Switch if-else statement around to improve readability
Kim Alvefur <zash@zash.se>
parents:
1867
diff
changeset
|
17 module:log("debug", "Buffering presence stanza from %s to %s", stanza.attr.from, session.full_jid); |
552faee596b7
mod_throttle_presence: Switch if-else statement around to improve readability
Kim Alvefur <zash@zash.se>
parents:
1867
diff
changeset
|
18 buffer[stanza.attr.from] = st.clone(stanza); |
552faee596b7
mod_throttle_presence: Switch if-else statement around to improve readability
Kim Alvefur <zash@zash.se>
parents:
1867
diff
changeset
|
19 return nil; -- Drop this stanza (we've stored it for later) |
552faee596b7
mod_throttle_presence: Switch if-else statement around to improve readability
Kim Alvefur <zash@zash.se>
parents:
1867
diff
changeset
|
20 else |
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 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
|
22 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
|
23 module:log("debug", "Important stanza for %s from %s, flushing presence", session.full_jid, from); |
1504
b8164f18142a
mod_throttle_presence: Make sure flushed stanzas get through the filter
Kim Alvefur <zash@zash.se>
parents:
1489
diff
changeset
|
24 stanza._flush = true; |
b8164f18142a
mod_throttle_presence: Make sure flushed stanzas get through the filter
Kim Alvefur <zash@zash.se>
parents:
1489
diff
changeset
|
25 cached_presence._flush = true; |
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 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
|
27 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
|
28 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
|
29 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
|
30 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
|
31 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
|
32 |
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
|
33 local function throttle_session(event) |
1488
ba97f9be4f76
mod_throttle_presence: Fix traceback (thanks Ge0rG)
Kim Alvefur <zash@zash.se>
parents:
1485
diff
changeset
|
34 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
|
35 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
|
36 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
|
37 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
|
38 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
|
39 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
|
40 |
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
|
41 local function restore_session(event) |
1488
ba97f9be4f76
mod_throttle_presence: Fix traceback (thanks Ge0rG)
Kim Alvefur <zash@zash.se>
parents:
1485
diff
changeset
|
42 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
|
43 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
|
44 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
|
45 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
|
46 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
|
47 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
|
48 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
|
49 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
|
50 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
|
51 |
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
|
52 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
|
53 module:hook("csi-client-active", restore_session); |