Software /
code /
prosody-modules
Annotate
mod_throttle_presence/mod_throttle_presence.lua @ 1701:9b429fc9e8a0
mod_s2s_auth_dane: Simplify cases where there are only one SRV record
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 06 May 2015 00:53:27 +0200 |
parent | 1504:b8164f18142a |
child | 1867:34468d3bfcac |
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) |
1504
b8164f18142a
mod_throttle_presence: Make sure flushed stanzas get through the filter
Kim Alvefur <zash@zash.se>
parents:
1489
diff
changeset
|
7 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
|
8 stanza._flush = nil; |
b8164f18142a
mod_throttle_presence: Make sure flushed stanzas get through the filter
Kim Alvefur <zash@zash.se>
parents:
1489
diff
changeset
|
9 return stanza; |
b8164f18142a
mod_throttle_presence: Make sure flushed stanzas get through the filter
Kim Alvefur <zash@zash.se>
parents:
1489
diff
changeset
|
10 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
|
11 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
|
12 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
|
13 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
|
14 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
|
15 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
|
16 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
|
17 stanza._flush = true; |
b8164f18142a
mod_throttle_presence: Make sure flushed stanzas get through the filter
Kim Alvefur <zash@zash.se>
parents:
1489
diff
changeset
|
18 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
|
19 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
|
20 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
|
21 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
|
22 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
|
23 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
|
24 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
|
25 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
|
26 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
|
27 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
|
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 |
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 local function throttle_session(event) |
1488
ba97f9be4f76
mod_throttle_presence: Fix traceback (thanks Ge0rG)
Kim Alvefur <zash@zash.se>
parents:
1485
diff
changeset
|
31 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
|
32 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
|
33 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
|
34 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
|
35 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
|
36 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
|
37 |
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
|
38 local function restore_session(event) |
1488
ba97f9be4f76
mod_throttle_presence: Fix traceback (thanks Ge0rG)
Kim Alvefur <zash@zash.se>
parents:
1485
diff
changeset
|
39 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
|
40 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
|
41 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
|
42 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
|
43 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
|
44 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
|
45 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
|
46 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
|
47 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
|
48 |
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 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
|
50 module:hook("csi-client-active", restore_session); |