Comparison

mod_muc_limits/mod_muc_limits.lua @ 5593:04f36a470dca

Update from upstream
author Trần H. Trung <xmpp:trần.h.trung@trung.fun>
date Sun, 09 Jul 2023 01:31:29 +0700
parent 5567:d52cc18f0aa8
child 5608:6680a1f53353
comparison
equal deleted inserted replaced
5591:680fb3344357 5593:04f36a470dca
11 11
12 local period = math.max(module:get_option_number("muc_event_rate", 0.5), 0); 12 local period = math.max(module:get_option_number("muc_event_rate", 0.5), 0);
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/
17 local max_char_count = module:get_option_number("muc_max_char_count", 5664); -- Default chosen by multiplying a number by 23
18
16 local join_only = module:get_option_boolean("muc_limit_joins_only", false); 19 local join_only = module:get_option_boolean("muc_limit_joins_only", false);
17 local dropped_count = 0; 20 local dropped_count = 0;
18 local dropped_jids; 21 local dropped_jids;
19 22
20 local function log_dropped() 23 local function log_dropped()
44 local throttle = room.throttle; 47 local throttle = room.throttle;
45 if not room.throttle then 48 if not room.throttle then
46 throttle = new_throttle(period*burst, burst); 49 throttle = new_throttle(period*burst, burst);
47 room.throttle = throttle; 50 room.throttle = throttle;
48 end 51 end
49 if not throttle:poll(1) then 52 local cost = 1;
53 local body = stanza:get_child_text("body");
54 if body then
55 -- TODO calculate a text diagonal cross-section or some mathemagical
56 -- number, maybe some cost multipliers
57 if #body > max_char_count then
58 origin.send(st.error_reply(stanza, "modify", "policy-violation", "Your message is too long, please write a shorter one")
59 :up():tag("x", { xmlns = xmlns_muc }));
60 return true;
61 end
62 local body_lines = select(2, body:gsub("\n[^\n]*", ""));
63 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()
65 :tag("x", { xmlns = xmlns_muc; }));
66 return true;
67 end
68 cost = cost + body_lines;
69 end
70 if not throttle:poll(cost) then
50 module:log("debug", "Dropping stanza for %s@%s from %s, over rate limit", dest_room, dest_host, from_jid); 71 module:log("debug", "Dropping stanza for %s@%s from %s, over rate limit", dest_room, dest_host, from_jid);
51 if not dropped_jids then 72 if not dropped_jids then
52 dropped_jids = { [from_jid] = true, from_jid }; 73 dropped_jids = { [from_jid] = true, from_jid };
53 module:add_timer(5, log_dropped); 74 module:add_timer(5, log_dropped);
54 elseif not dropped_jids[from_jid] then 75 elseif not dropped_jids[from_jid] then
58 dropped_count = dropped_count + 1; 79 dropped_count = dropped_count + 1;
59 if stanza.attr.type == "error" then -- We don't want to bounce errors 80 if stanza.attr.type == "error" then -- We don't want to bounce errors
60 return true; 81 return true;
61 end 82 end
62 local reply = st.error_reply(stanza, "wait", "policy-violation", "The room is currently overactive, please try again later"); 83 local reply = st.error_reply(stanza, "wait", "policy-violation", "The room is currently overactive, please try again later");
63 local body = stanza:get_child_text("body");
64 if body then 84 if body then
65 reply:up():tag("body"):text(body):up(); 85 reply:up():tag("body"):text(body):up();
66 end 86 end
67 local x = stanza:get_child("x", xmlns_muc); 87 local x = stanza:get_child("x", xmlns_muc);
68 if x then 88 if x then