Comparison

mod_muc_limits/mod_muc_limits.lua @ 5564:731ba9400c10

mod_muc_limits: Raise cost for multi-line messages
author Kim Alvefur <zash@zash.se>
date Sat, 24 Jun 2023 23:51:31 +0200
parent 3965:2b10e51d85a6
child 5566:f71d66bd87be
comparison
equal deleted inserted replaced
5563:a1f8cc591b66 5564:731ba9400c10
44 local throttle = room.throttle; 44 local throttle = room.throttle;
45 if not room.throttle then 45 if not room.throttle then
46 throttle = new_throttle(period*burst, burst); 46 throttle = new_throttle(period*burst, burst);
47 room.throttle = throttle; 47 room.throttle = throttle;
48 end 48 end
49 if not throttle:poll(1) then 49 local cost = 1;
50 local body = stanza:get_child_text("body");
51 if body then
52 -- TODO calculate a text diagonal cross-section or some mathemagical
53 -- number, maybe some cost multipliers
54 local body_lines = select(2, body:gsub("\n[^\n]*", ""));
55 cost = cost + body_lines;
56 end
57 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); 58 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 59 if not dropped_jids then
52 dropped_jids = { [from_jid] = true, from_jid }; 60 dropped_jids = { [from_jid] = true, from_jid };
53 module:add_timer(5, log_dropped); 61 module:add_timer(5, log_dropped);
54 elseif not dropped_jids[from_jid] then 62 elseif not dropped_jids[from_jid] then
58 dropped_count = dropped_count + 1; 66 dropped_count = dropped_count + 1;
59 if stanza.attr.type == "error" then -- We don't want to bounce errors 67 if stanza.attr.type == "error" then -- We don't want to bounce errors
60 return true; 68 return true;
61 end 69 end
62 local reply = st.error_reply(stanza, "wait", "policy-violation", "The room is currently overactive, please try again later"); 70 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 71 if body then
65 reply:up():tag("body"):text(body):up(); 72 reply:up():tag("body"):text(body):up();
66 end 73 end
67 local x = stanza:get_child("x", xmlns_muc); 74 local x = stanza:get_child("x", xmlns_muc);
68 if x then 75 if x then