Changeset

6152:5961e01dd963

mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
author Matthew Wild <mwild1@gmail.com>
date Fri, 17 Jan 2025 10:29:28 +0000
parents 6151:0afd83bbdf09
children 6153:ef7bf4215cb3
files mod_anti_spam/mod_anti_spam.lua
diffstat 1 files changed, 35 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/mod_anti_spam/mod_anti_spam.lua	Thu Jan 16 18:51:16 2025 +0100
+++ b/mod_anti_spam/mod_anti_spam.lua	Fri Jan 17 10:29:28 2025 +0000
@@ -5,6 +5,7 @@
 local sha256 = require "util.hashes".sha256;
 local st = require"util.stanza";
 local is_contact_subscribed = require "core.rostermanager".is_contact_subscribed;
+local is_user_subscribed = require "core.rostermanager".is_user_subscribed;
 local full_sessions = prosody.full_sessions;
 
 local user_exists = require "core.usermanager".user_exists;
@@ -15,6 +16,16 @@
 local spam_source_domains = set.new();
 local spam_source_ips = trie.new();
 local spam_source_jids = set.new();
+local default_spam_action = module:get_option("anti_spam_default_action", "bounce");
+local custom_spam_actions = module:get_option("anti_spam_actions", {});
+
+local spam_actions = setmetatable({}, {
+	__index = function (t, reason)
+		local action = rawget(custom_spam_actions, reason) or default_spam_action;
+		rawset(t, reason, action);
+		return action;
+	end;
+});
 
 local count_spam_blocked = module:metric("counter", "anti_spam_blocked", "stanzas", "Stanzas blocked as spam", {"reason"});
 
@@ -26,6 +37,9 @@
 };
 
 function block_spam(event, reason, action)
+	if not action then
+		action = spam_actions[reason];
+	end
 	event.spam_reason = reason;
 	event.spam_action = action;
 	if module:fire_event("spam-blocked", event) == false then
@@ -157,23 +171,25 @@
 		return;
 	end
 
-	if not user_exists(to_user, to_host) then return; end
-
 	local from_bare = jid_bare(event.stanza.attr.from);
-	if not is_from_stranger(from_bare, event) then return; end
+	if user_exists(to_user, to_host) then
+		if not is_from_stranger(from_bare, event) then
+			return;
+		end
+	end
 
 	module:log("debug", "Processing message from stranger...");
 
 	if is_spammy_server(event.origin) then
-		return block_spam(event, "known-spam-source", "drop");
+		return block_spam(event, "known-spam-source");
 	end
 
 	if is_spammy_sender(from_bare) then
-		return block_spam(event, "known-spam-jid", "drop");
+		return block_spam(event, "known-spam-jid");
 	end
 
 	if is_spammy_content(event.stanza) then
-		return block_spam(event, "spam-content", "drop");
+		return block_spam(event, "spam-content");
 	end
 
 	module:log("debug", "Allowing message through");
@@ -184,16 +200,26 @@
 		return;
 	end
 
-	module:log("debug", "Processing subscription request...");
+
+	local to_user, to_host = jid_split(event.stanza.attr.to);
+	local from_bare = jid_bare(event.stanza.attr.from);
+
+	if user_exists(to_user, to_host) then
+		if not is_from_stranger(from_bare, event) then
+			return;
+		end
+	end
+
+	module:log("debug", "Processing subscription request from stranger...");
 
 	if is_spammy_server(event.origin) then
-		return block_spam(event, "known-spam-source", "drop");
+		return block_spam(event, "known-spam-source");
 	end
 
 	module:log("debug", "Not from known spam source server");
 
 	if is_spammy_sender(jid_bare(event.stanza.attr.from)) then
-		return block_spam(event, "known-spam-jid", "drop");
+		return block_spam(event, "known-spam-jid");
 	end
 
 	module:log("debug", "Not from known spam source JID");