comparison mod_muc_slow_mode/mod_muc_slow_mode.lua @ 5939:d7c207964aa5

mod_muc_slow_mode: initial commit.
author John Livingston <git@john-livingston.fr>
date Fri, 26 Jul 2024 15:47:18 +0200
parents
children
comparison
equal deleted inserted replaced
5938:959382fac20c 5939:d7c207964aa5
1 -- mod_muc_slow_mode
2 --
3 -- SPDX-FileCopyrightText: 2024 John Livingston <https://www.john-livingston.fr/>
4 -- SPDX-License-Identifier: AGPL-3.0-only
5 --
6 -- Implements: XEP-????: MUC Slow Mode (XEP to come).
7 --
8 -- Imports
9 local st = require "util.stanza";
10 local jid_bare = require "util.jid".bare;
11 local gettime = require 'socket'.gettime;
12
13 -- Plugin dependencies
14 local mod_muc = module:depends "muc";
15
16 local muc_util = module:require "muc/util";
17 local valid_roles = muc_util.valid_roles;
18
19 -- Namespaces
20 local xmlns_muc = "http://jabber.org/protocol/muc";
21
22 -- Options
23
24 -- form_position: the position in the room config form (this value will be passed as priority for the "muc-config-form" hook).
25 -- Depending on your application, it is possible that the slow mode is more important than other fields (for example for a video streaming service).
26 -- So there is an option to change this.
27 -- By default, field will be between muc#roomconfig_changesubject and muc#roomconfig_moderatedroom
28 local form_position = module:get_option_number("slow_mode_duration_form_position") or 80-2;
29
30 -- Getter/Setter
31 local function get_slow_mode_duration(room)
32 return room._data.slow_mode_duration or 0;
33 end
34
35 local function set_slow_mode_duration(room, duration)
36 if duration then
37 duration = assert(tonumber(duration), "Slow mode duration is not a valid number");
38 end
39 if duration and duration < 0 then
40 duration = 0;
41 end
42
43 if get_slow_mode_duration(room) == duration then return false; end
44
45 room._data.slow_mode_duration = duration;
46 return true;
47 end
48
49 -- Discovering support
50 local function add_disco_form(event)
51 table.insert(event.form, {
52 name = "muc#roominfo_slow_mode_duration";
53 value = "";
54 });
55 event.formdata["muc#roominfo_slow_mode_duration"] = get_slow_mode_duration(event.room);
56 end
57
58 module:hook("muc-disco#info", add_disco_form);
59
60 -- Config form declaration
61 local function add_form_option(event)
62 table.insert(event.form, {
63 name = "muc#roomconfig_slow_mode_duration";
64 type = "text-single";
65 datatype = "xs:integer";
66 range_min = 0;
67 label = "Slow Mode (0=disabled, any positive integer= users can send a message every X seconds.)";
68 -- desc = "";
69 value = get_slow_mode_duration(event.room);
70 });
71 end
72
73 module:hook("muc-config-submitted/muc#roomconfig_slow_mode_duration", function(event)
74 if set_slow_mode_duration(event.room, event.value) then
75 -- status 104 = configuration change: Inform occupants that a non-privacy-related room configuration change has occurred
76 event.status_codes["104"] = true;
77 end
78 end);
79
80 module:hook("muc-config-form", add_form_option, form_position);
81
82 -- handling groupchat messages
83 function handle_groupchat(event)
84 local origin, stanza = event.origin, event.stanza;
85 local room = event.room;
86
87 -- only consider messages with body (ie: ignore chatstate and other non-text xmpp messages)
88 local body = stanza:get_child_text("body")
89 if not body or #body < 1 then
90 -- module:log("debug", "No body, message accepted");
91 return;
92 end
93
94 local duration = get_slow_mode_duration(room) or 0;
95 if duration <= 0 then
96 -- no slow mode for this room
97 -- module:log("debug", "No slow mode for this room");
98 return;
99 end
100
101 -- Checking user's permissions (moderators are not subject to slow mode)
102 local actor = stanza.attr.from;
103 local actor_nick = room:get_occupant_jid(actor);
104 local actor_jid = jid_bare(actor);
105 -- Only checking role, not affiliation (slow mode only applies on users currently connected to the room)
106 local role = room:get_role(actor_nick);
107 if valid_roles[role or "none"] >= valid_roles.moderator then
108 -- user bypasses the slow mode.
109 -- module:log("debug", "User is moderator, bypassing slow mode");
110 return;
111 end
112
113 if not room.slow_mode_last_messages then
114 -- We store last message time for each users in room.slow_mode_last_messages:
115 -- * key: bare jid (without the nickname)
116 -- * value: last message timestamp
117 -- If room is cleared from memory, these data are lost. But should not be an issue.
118 -- For now, i don't clean slow_mode_last_messages, it should not use too much memory.
119 -- module:log("debug", "Initializing slow_mode_last_messages for the room.");
120 room.slow_mode_last_messages = {};
121 end
122
123 local now = gettime();
124 local previous = room.slow_mode_last_messages[actor_jid];
125 -- module:log(
126 -- "debug",
127 -- "Last message for user %s was at %s, now is %s, duration is %s, now - previous is %s",
128 -- actor_jid,
129 -- previous or 0,
130 -- now,
131 -- duration,
132 -- (now - (previous or 0))
133 -- );
134 if ((not previous) or (now - previous > duration)) then
135 -- module:log("debug", "Message accepted");
136 room.slow_mode_last_messages[actor_jid] = now;
137 return;
138 end
139
140 module:log("debug", "Bouncing message for user %s", actor_nick);
141 local reply = st.error_reply(
142 stanza,
143 -- error_type = 'wait' (see descriptions in RFC 6120 https://xmpp.org/rfcs/rfc6120.html#stanzas-error-syntax)
144 "wait",
145 -- error_condition = 'policy-violation' (see RFC 6120 Defined Error Conditions https://xmpp.org/rfcs/rfc6120.html#stanzas-error-conditions)
146 "policy-violation",
147 "You have exceeded the limit imposed by the slow mode in this room. You have to wait " .. duration .. " seconds between messages. Please try again later"
148 );
149
150 -- Note: following commented lines were inspired by mod_muc_limits, but it seems it is not required.
151 -- if body then
152 -- reply:up():tag("body"):text(body):up();
153 -- end
154 -- local x = stanza:get_child("x", xmlns_muc);
155 -- if x then
156 -- reply:add_child(st.clone(x));
157 -- end
158
159 origin.send(reply);
160 return true; -- stoping propagation
161 end
162
163 module:hook("muc-occupant-groupchat", handle_groupchat);