comparison mod_csi_grace_period/mod_csi_grace_period.lua @ 3507:5c37d759b1e2

mod_csi_grace_period: Server-side grace period logic for CSI
author Kim Alvefur <zash@zash.se>
date Sun, 31 Mar 2019 04:49:56 +0200
parents
children 7570976318a9
comparison
equal deleted inserted replaced
3506:7b1eede1a840 3507:5c37d759b1e2
1 -- Copyright (c) 2019 Kim Alvefur
2 --
3 -- This project is MIT/X11 licensed. Please see the
4 -- COPYING file in the source package for more information.
5 --
6 -- Yes, this module touches stores data in user sessions
7 -- luacheck: ignore 122
8
9 local grace_period = module:get_option_number("grace_period", 30);
10
11 local user_sessions = prosody.hosts[module.host].sessions;
12
13 module:hook("csi-is-stanza-important", function (event)
14 if event.stanza.name ~= "message" then return end
15 local session = event.session;
16 if not session then return; end
17
18 local user_session = user_sessions[session.username];
19 if not user_session then return; end
20
21 if user_sessions.grace_time_start then
22 if user_sessions.last_active == session.resource then
23 return;
24 end
25 if (os.time() - user_sessions.grace_time_start) < grace_period then
26 session.log("debug", "Within grace period, probably seen");
27 return false;
28 end
29 end
30 end, 1);
31
32 local function on_activity(event)
33 local stanza, origin = event.stanza, event.origin;
34 local user_session = user_sessions[origin.username];
35 if not user_session then return; end
36
37 if stanza:get_child("body") or stanza:get_child("active", "http://jabber.org/protocol/chatstates") then
38 user_sessions.last_active = origin.resource;
39 user_sessions.grace_time_start = os.time();
40 end
41 end
42 module:hook("pre-message/full", on_activity);
43 module:hook("pre-message/bare", on_activity);
44 module:hook("pre-message/host", on_activity);