comparison mod_muc_limits/mod_muc_limits.lua @ 5653:62c6e17a5e9d

Merge
author Stephen Paul Weber <singpolyma@singpolyma.net>
date Mon, 18 Sep 2023 08:24:19 -0500
parents 6680a1f53353
children
comparison
equal deleted inserted replaced
5652:eade7ff9f52c 5653:62c6e17a5e9d
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 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);
20
16 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);
17 local dropped_count = 0; 22 local dropped_count = 0;
18 local dropped_jids; 23 local dropped_jids;
19 24
20 local function log_dropped() 25 local function log_dropped()
44 local throttle = room.throttle; 49 local throttle = room.throttle;
45 if not room.throttle then 50 if not room.throttle then
46 throttle = new_throttle(period*burst, burst); 51 throttle = new_throttle(period*burst, burst);
47 room.throttle = throttle; 52 room.throttle = throttle;
48 end 53 end
49 if not throttle:poll(1) then 54 local cost = base_cost;
55 local body = stanza:get_child_text("body");
56 if body then
57 -- TODO calculate a text diagonal cross-section or some mathemagical
58 -- number, maybe some cost multipliers
59 if #body > max_char_count then
60 origin.send(st.error_reply(stanza, "modify", "policy-violation", "Your message is too long, please write a shorter one")
61 :up():tag("x", { xmlns = xmlns_muc }));
62 return true;
63 end
64 local body_lines = select(2, body:gsub("\n[^\n]*", ""));
65 if body_lines > max_line_count then
66 origin.send(st.error_reply(stanza, "modify", "policy-violation", "Your message is too long, please write a shorter one"):up()
67 :tag("x", { xmlns = xmlns_muc; }));
68 return true;
69 end
70 cost = cost + (body_lines * line_multiplier);
71 end
72 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); 73 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 74 if not dropped_jids then
52 dropped_jids = { [from_jid] = true, from_jid }; 75 dropped_jids = { [from_jid] = true, from_jid };
53 module:add_timer(5, log_dropped); 76 module:add_timer(5, log_dropped);
54 elseif not dropped_jids[from_jid] then 77 elseif not dropped_jids[from_jid] then
58 dropped_count = dropped_count + 1; 81 dropped_count = dropped_count + 1;
59 if stanza.attr.type == "error" then -- We don't want to bounce errors 82 if stanza.attr.type == "error" then -- We don't want to bounce errors
60 return true; 83 return true;
61 end 84 end
62 local reply = st.error_reply(stanza, "wait", "policy-violation", "The room is currently overactive, please try again later"); 85 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 86 if body then
65 reply:up():tag("body"):text(body):up(); 87 reply:up():tag("body"):text(body):up();
66 end 88 end
67 local x = stanza:get_child("x", xmlns_muc); 89 local x = stanza:get_child("x", xmlns_muc);
68 if x then 90 if x then