Changeset

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 (18 months ago)
parents 5563:a1f8cc591b66
children 5565:b97ae1cd7813
files mod_muc_limits/mod_muc_limits.lua
diffstat 1 files changed, 9 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/mod_muc_limits/mod_muc_limits.lua	Thu Jun 22 22:00:51 2023 +0200
+++ b/mod_muc_limits/mod_muc_limits.lua	Sat Jun 24 23:51:31 2023 +0200
@@ -46,7 +46,15 @@
 		throttle = new_throttle(period*burst, burst);
 		room.throttle = throttle;
 	end
-	if not throttle:poll(1) then
+	local cost = 1;
+	local body = stanza:get_child_text("body");
+	if body then
+		-- TODO calculate a text diagonal cross-section or some mathemagical
+		-- number, maybe some cost multipliers
+		local body_lines = select(2, body:gsub("\n[^\n]*", ""));
+		cost = cost + body_lines;
+	end
+	if not throttle:poll(cost) then
 		module:log("debug", "Dropping stanza for %s@%s from %s, over rate limit", dest_room, dest_host, from_jid);
 		if not dropped_jids then
 			dropped_jids = { [from_jid] = true, from_jid };
@@ -60,7 +68,6 @@
 			return true;
 		end
 		local reply = st.error_reply(stanza, "wait", "policy-violation", "The room is currently overactive, please try again later");
-		local body = stanza:get_child_text("body");
 		if body then
 			reply:up():tag("body"):text(body):up();
 		end