Software /
code /
prosody-modules
Comparison
mod_muc_limits/mod_muc_limits.lua @ 5608:6680a1f53353
mod_muc_limits: Reduce cost of multi-line messages, make configurable
Typing a 5-line message preceded by a few chat states would have hit the
default limit.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 15 Jul 2023 09:09:41 +0200 |
parent | 5567:d52cc18f0aa8 |
comparison
equal
deleted
inserted
replaced
5607:c799b460f9f0 | 5608:6680a1f53353 |
---|---|
13 local burst = math.max(module:get_option_number("muc_burst_factor", 6), 1); | 13 local burst = math.max(module:get_option_number("muc_burst_factor", 6), 1); |
14 | 14 |
15 local max_nick_length = module:get_option_number("muc_max_nick_length", 23); -- Default chosen through scientific methods | 15 local max_nick_length = module:get_option_number("muc_max_nick_length", 23); -- Default chosen through scientific methods |
16 local max_line_count = module:get_option_number("muc_max_line_count", 23); -- Default chosen through s/scientific methods/copy and paste/ | 16 local max_line_count = module:get_option_number("muc_max_line_count", 23); -- Default chosen through s/scientific methods/copy and paste/ |
17 local max_char_count = module:get_option_number("muc_max_char_count", 5664); -- Default chosen by multiplying a number by 23 | 17 local max_char_count = module:get_option_number("muc_max_char_count", 5664); -- Default chosen by multiplying a number by 23 |
18 local base_cost = math.max(module:get_option_number("muc_limit_base_cost", 1), 0); | |
19 local line_multiplier = math.max(module:get_option_number("muc_line_count_multiplier", 0.1), 0); | |
18 | 20 |
19 local join_only = module:get_option_boolean("muc_limit_joins_only", false); | 21 local join_only = module:get_option_boolean("muc_limit_joins_only", false); |
20 local dropped_count = 0; | 22 local dropped_count = 0; |
21 local dropped_jids; | 23 local dropped_jids; |
22 | 24 |
47 local throttle = room.throttle; | 49 local throttle = room.throttle; |
48 if not room.throttle then | 50 if not room.throttle then |
49 throttle = new_throttle(period*burst, burst); | 51 throttle = new_throttle(period*burst, burst); |
50 room.throttle = throttle; | 52 room.throttle = throttle; |
51 end | 53 end |
52 local cost = 1; | 54 local cost = base_cost; |
53 local body = stanza:get_child_text("body"); | 55 local body = stanza:get_child_text("body"); |
54 if body then | 56 if body then |
55 -- TODO calculate a text diagonal cross-section or some mathemagical | 57 -- TODO calculate a text diagonal cross-section or some mathemagical |
56 -- number, maybe some cost multipliers | 58 -- number, maybe some cost multipliers |
57 if #body > max_char_count then | 59 if #body > max_char_count then |
63 if body_lines > max_line_count then | 65 if body_lines > max_line_count then |
64 origin.send(st.error_reply(stanza, "modify", "policy-violation", "Your message is too long, please write a shorter one"):up() | 66 origin.send(st.error_reply(stanza, "modify", "policy-violation", "Your message is too long, please write a shorter one"):up() |
65 :tag("x", { xmlns = xmlns_muc; })); | 67 :tag("x", { xmlns = xmlns_muc; })); |
66 return true; | 68 return true; |
67 end | 69 end |
68 cost = cost + body_lines; | 70 cost = cost + (body_lines * line_multiplier); |
69 end | 71 end |
70 if not throttle:poll(cost) then | 72 if not throttle:poll(cost) then |
71 module:log("debug", "Dropping stanza for %s@%s from %s, over rate limit", dest_room, dest_host, from_jid); | 73 module:log("debug", "Dropping stanza for %s@%s from %s, over rate limit", dest_room, dest_host, from_jid); |
72 if not dropped_jids then | 74 if not dropped_jids then |
73 dropped_jids = { [from_jid] = true, from_jid }; | 75 dropped_jids = { [from_jid] = true, from_jid }; |