annotate mod_anti_spam/mod_anti_spam.lua @ 6126:160d1bcfe341

mod_anti_spam: Optimization to avoid needless string concatenation
author Matthew Wild <mwild1@gmail.com>
date Fri, 17 Jan 2025 10:32:34 +0000
parents ef7bf4215cb3
children d80e398a2acc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local ip = require "util.ip";
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local jid_bare = require "util.jid".bare;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local jid_split = require "util.jid".split;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local set = require "util.set";
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local sha256 = require "util.hashes".sha256;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local st = require"util.stanza";
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local is_contact_subscribed = require "core.rostermanager".is_contact_subscribed;
6124
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6106
diff changeset
8 local is_user_subscribed = require "core.rostermanager".is_user_subscribed;
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local full_sessions = prosody.full_sessions;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local user_exists = require "core.usermanager".user_exists;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local new_rtbl_subscription = module:require("rtbl").new_rtbl_subscription;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local trie = module:require("trie");
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
6106
00b55c7ef393 Backed out changeset ffec70ddbffc
Matthew Wild <mwild1@gmail.com>
parents: 6104
diff changeset
16 local spam_source_domains = set.new();
00b55c7ef393 Backed out changeset ffec70ddbffc
Matthew Wild <mwild1@gmail.com>
parents: 6104
diff changeset
17 local spam_source_ips = trie.new();
00b55c7ef393 Backed out changeset ffec70ddbffc
Matthew Wild <mwild1@gmail.com>
parents: 6104
diff changeset
18 local spam_source_jids = set.new();
6124
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6106
diff changeset
19 local default_spam_action = module:get_option("anti_spam_default_action", "bounce");
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6106
diff changeset
20 local custom_spam_actions = module:get_option("anti_spam_actions", {});
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6106
diff changeset
21
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6106
diff changeset
22 local spam_actions = setmetatable({}, {
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6106
diff changeset
23 __index = function (t, reason)
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6106
diff changeset
24 local action = rawget(custom_spam_actions, reason) or default_spam_action;
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6106
diff changeset
25 rawset(t, reason, action);
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6106
diff changeset
26 return action;
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6106
diff changeset
27 end;
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6106
diff changeset
28 });
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 local count_spam_blocked = module:metric("counter", "anti_spam_blocked", "stanzas", "Stanzas blocked as spam", {"reason"});
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31
6097
a54b94a3e994 mod_anti_spam: Fix luacheck warning
Matthew Wild <mwild1@gmail.com>
parents: 6096
diff changeset
32 local hosts = prosody.hosts;
a54b94a3e994 mod_anti_spam: Fix luacheck warning
Matthew Wild <mwild1@gmail.com>
parents: 6096
diff changeset
33
6098
4b1a4be43487 mod_anti_spam: Improve error bounce text with actionable info
Matthew Wild <mwild1@gmail.com>
parents: 6097
diff changeset
34 local reason_messages = {
4b1a4be43487 mod_anti_spam: Improve error bounce text with actionable info
Matthew Wild <mwild1@gmail.com>
parents: 6097
diff changeset
35 default = "Rejected as spam";
4b1a4be43487 mod_anti_spam: Improve error bounce text with actionable info
Matthew Wild <mwild1@gmail.com>
parents: 6097
diff changeset
36 ["known-spam-source"] = "Rejected as spam. Your server is listed as a known source of spam. Please contact your server operator.";
4b1a4be43487 mod_anti_spam: Improve error bounce text with actionable info
Matthew Wild <mwild1@gmail.com>
parents: 6097
diff changeset
37 };
4b1a4be43487 mod_anti_spam: Improve error bounce text with actionable info
Matthew Wild <mwild1@gmail.com>
parents: 6097
diff changeset
38
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 function block_spam(event, reason, action)
6124
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6106
diff changeset
40 if not action then
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6106
diff changeset
41 action = spam_actions[reason];
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6106
diff changeset
42 end
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 event.spam_reason = reason;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 event.spam_action = action;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 if module:fire_event("spam-blocked", event) == false then
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 module:log("debug", "Spam allowed by another module");
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 return;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 count_spam_blocked:with_labels(reason):add(1);
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 if action == "bounce" then
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 module:log("debug", "Bouncing likely spam %s from %s (%s)", event.stanza.name, event.stanza.attr.from, reason);
6098
4b1a4be43487 mod_anti_spam: Improve error bounce text with actionable info
Matthew Wild <mwild1@gmail.com>
parents: 6097
diff changeset
54 event.origin.send(st.error_reply("cancel", "policy-violation", reason_messages[reason] or reason_messages.default));
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 else
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 module:log("debug", "Discarding likely spam %s from %s (%s)", event.stanza.name, event.stanza.attr.from, reason);
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 return true;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 function is_from_stranger(from_jid, event)
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 local stanza = event.stanza;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 local to_user, to_host, to_resource = jid_split(stanza.attr.to);
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 if not to_user then return false; end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 local to_session = full_sessions[stanza.attr.to];
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 if to_session then return false; end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70
6125
ef7bf4215cb3 mod_anti_spam: Update definition of "stranger" to include sub to/from JID or domains
Matthew Wild <mwild1@gmail.com>
parents: 6124
diff changeset
71 if not (is_contact_subscribed(to_user, to_host, from_jid) or is_user_subscribed(to_user, to_host, from_jid)) then
ef7bf4215cb3 mod_anti_spam: Update definition of "stranger" to include sub to/from JID or domains
Matthew Wild <mwild1@gmail.com>
parents: 6124
diff changeset
72 local from_user, from_host = jid_split(from_jid);
ef7bf4215cb3 mod_anti_spam: Update definition of "stranger" to include sub to/from JID or domains
Matthew Wild <mwild1@gmail.com>
parents: 6124
diff changeset
73
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 -- Allow all messages from your own jid
6126
160d1bcfe341 mod_anti_spam: Optimization to avoid needless string concatenation
Matthew Wild <mwild1@gmail.com>
parents: 6125
diff changeset
75 if from_user == to_user and from_host == to_host then
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 return false; -- Pass through
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 if to_resource and stanza.attr.type == "groupchat" then
6125
ef7bf4215cb3 mod_anti_spam: Update definition of "stranger" to include sub to/from JID or domains
Matthew Wild <mwild1@gmail.com>
parents: 6124
diff changeset
79 return false; -- Pass through group chat messages
ef7bf4215cb3 mod_anti_spam: Update definition of "stranger" to include sub to/from JID or domains
Matthew Wild <mwild1@gmail.com>
parents: 6124
diff changeset
80 end
ef7bf4215cb3 mod_anti_spam: Update definition of "stranger" to include sub to/from JID or domains
Matthew Wild <mwild1@gmail.com>
parents: 6124
diff changeset
81 if is_contact_subscribed(to_user, to_host, from_host) then
ef7bf4215cb3 mod_anti_spam: Update definition of "stranger" to include sub to/from JID or domains
Matthew Wild <mwild1@gmail.com>
parents: 6124
diff changeset
82 -- If you have the sending domain in your roster,
ef7bf4215cb3 mod_anti_spam: Update definition of "stranger" to include sub to/from JID or domains
Matthew Wild <mwild1@gmail.com>
parents: 6124
diff changeset
83 -- allow through (probably a gateway)
ef7bf4215cb3 mod_anti_spam: Update definition of "stranger" to include sub to/from JID or domains
Matthew Wild <mwild1@gmail.com>
parents: 6124
diff changeset
84 return false;
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 return true; -- Stranger danger
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 function is_spammy_server(session)
6106
00b55c7ef393 Backed out changeset ffec70ddbffc
Matthew Wild <mwild1@gmail.com>
parents: 6104
diff changeset
91 if spam_source_domains:contains(session.from_host) then
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 return true;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 end
6089
b4a4f4094337 mod_anti_spam: Fix traceback on missing/invalid session IP and log warning
Matthew Wild <mwild1@gmail.com>
parents: 6088
diff changeset
94 local raw_ip = session.ip;
b4a4f4094337 mod_anti_spam: Fix traceback on missing/invalid session IP and log warning
Matthew Wild <mwild1@gmail.com>
parents: 6088
diff changeset
95 local parsed_ip = raw_ip and ip.new_ip(session.ip);
6092
bd3ff802d883 mod_anti_spam: Fix another traceback for origin sessions without an IP
Matthew Wild <mwild1@gmail.com>
parents: 6089
diff changeset
96 -- Not every session has an ip - for example, stanzas sent from a
bd3ff802d883 mod_anti_spam: Fix another traceback for origin sessions without an IP
Matthew Wild <mwild1@gmail.com>
parents: 6089
diff changeset
97 -- local host session
6106
00b55c7ef393 Backed out changeset ffec70ddbffc
Matthew Wild <mwild1@gmail.com>
parents: 6104
diff changeset
98 if parsed_ip and spam_source_ips:contains_ip(parsed_ip) then
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 return true;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 function is_spammy_sender(sender_jid)
6106
00b55c7ef393 Backed out changeset ffec70ddbffc
Matthew Wild <mwild1@gmail.com>
parents: 6104
diff changeset
104 return spam_source_jids:contains(sha256(sender_jid, true));
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 local spammy_strings = module:get_option_array("anti_spam_block_strings");
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 local spammy_patterns = module:get_option_array("anti_spam_block_patterns");
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 function is_spammy_content(stanza)
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 -- Only support message content
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 if stanza.name ~= "message" then return; end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 if not (spammy_strings or spammy_patterns) then return; end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 local body = stanza:get_child_text("body");
6099
559648c7c8d6 mod_anti_spam: Skip content filtering for messages with no body (thanks Martin)
Matthew Wild <mwild1@gmail.com>
parents: 6098
diff changeset
116 if not body then return; end
559648c7c8d6 mod_anti_spam: Skip content filtering for messages with no body (thanks Martin)
Matthew Wild <mwild1@gmail.com>
parents: 6098
diff changeset
117
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 if spammy_strings then
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 for _, s in ipairs(spammy_strings) do
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 if body:find(s, 1, true) then
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 return true;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 if spammy_patterns then
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 for _, s in ipairs(spammy_patterns) do
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 if body:find(s) then
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 return true;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 -- Set up RTBLs
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135
6093
255ab0b81f14 mod_anti_spam: Fix traceback when no anti-spam services defined in config (thanks mirux)
Matthew Wild <mwild1@gmail.com>
parents: 6092
diff changeset
136 local anti_spam_services = module:get_option_array("anti_spam_services", {});
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 for _, rtbl_service_jid in ipairs(anti_spam_services) do
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 new_rtbl_subscription(rtbl_service_jid, "spam_source_domains", {
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 added = function (item)
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 spam_source_domains:add(item);
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 end;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 removed = function (item)
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 spam_source_domains:remove(item);
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 end;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 });
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 new_rtbl_subscription(rtbl_service_jid, "spam_source_ips", {
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 added = function (item)
6102
5a0e47ad7d6b mod_anti_spam: Gracefully handle failure to parse CIDR in IP RTBL
Matthew Wild <mwild1@gmail.com>
parents: 6101
diff changeset
149 local subnet_ip, subnet_bits = ip.parse_cidr(item);
5a0e47ad7d6b mod_anti_spam: Gracefully handle failure to parse CIDR in IP RTBL
Matthew Wild <mwild1@gmail.com>
parents: 6101
diff changeset
150 if not subnet_ip then
5a0e47ad7d6b mod_anti_spam: Gracefully handle failure to parse CIDR in IP RTBL
Matthew Wild <mwild1@gmail.com>
parents: 6101
diff changeset
151 return;
5a0e47ad7d6b mod_anti_spam: Gracefully handle failure to parse CIDR in IP RTBL
Matthew Wild <mwild1@gmail.com>
parents: 6101
diff changeset
152 end
5a0e47ad7d6b mod_anti_spam: Gracefully handle failure to parse CIDR in IP RTBL
Matthew Wild <mwild1@gmail.com>
parents: 6101
diff changeset
153 spam_source_ips:add_subnet(subnet_ip, subnet_bits);
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 end;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 removed = function (item)
6102
5a0e47ad7d6b mod_anti_spam: Gracefully handle failure to parse CIDR in IP RTBL
Matthew Wild <mwild1@gmail.com>
parents: 6101
diff changeset
156 local subnet_ip, subnet_bits = ip.parse_cidr(item);
5a0e47ad7d6b mod_anti_spam: Gracefully handle failure to parse CIDR in IP RTBL
Matthew Wild <mwild1@gmail.com>
parents: 6101
diff changeset
157 if not subnet_ip then
5a0e47ad7d6b mod_anti_spam: Gracefully handle failure to parse CIDR in IP RTBL
Matthew Wild <mwild1@gmail.com>
parents: 6101
diff changeset
158 return;
5a0e47ad7d6b mod_anti_spam: Gracefully handle failure to parse CIDR in IP RTBL
Matthew Wild <mwild1@gmail.com>
parents: 6101
diff changeset
159 end
5a0e47ad7d6b mod_anti_spam: Gracefully handle failure to parse CIDR in IP RTBL
Matthew Wild <mwild1@gmail.com>
parents: 6101
diff changeset
160 spam_source_ips:remove_subnet(subnet_ip, subnet_bits);
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 end;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 });
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 new_rtbl_subscription(rtbl_service_jid, "spam_source_jids_sha256", {
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 added = function (item)
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 spam_source_jids:add(item);
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 end;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 removed = function (item)
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 spam_source_jids:remove(item);
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 end;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 });
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 module:hook("message/bare", function (event)
6087
b644832a8290 mod_anti_spam: Fix syntax of user_exists() check (thanks Martin/Zash)
Matthew Wild <mwild1@gmail.com>
parents: 5859
diff changeset
174 local to_user, to_host = jid_split(event.stanza.attr.to);
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175
6094
15f6f1566bdb mod_anti_spam: Prevent traceback when processing a message to an unknown host
Matthew Wild <mwild1@gmail.com>
parents: 6093
diff changeset
176 if not hosts[to_host] then
15f6f1566bdb mod_anti_spam: Prevent traceback when processing a message to an unknown host
Matthew Wild <mwild1@gmail.com>
parents: 6093
diff changeset
177 module:log("warn", "Skipping filtering of message to unknown host <%s>", to_host);
15f6f1566bdb mod_anti_spam: Prevent traceback when processing a message to an unknown host
Matthew Wild <mwild1@gmail.com>
parents: 6093
diff changeset
178 return;
15f6f1566bdb mod_anti_spam: Prevent traceback when processing a message to an unknown host
Matthew Wild <mwild1@gmail.com>
parents: 6093
diff changeset
179 end
15f6f1566bdb mod_anti_spam: Prevent traceback when processing a message to an unknown host
Matthew Wild <mwild1@gmail.com>
parents: 6093
diff changeset
180
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 local from_bare = jid_bare(event.stanza.attr.from);
6124
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6106
diff changeset
182 if user_exists(to_user, to_host) then
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6106
diff changeset
183 if not is_from_stranger(from_bare, event) then
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6106
diff changeset
184 return;
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6106
diff changeset
185 end
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6106
diff changeset
186 end
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187
6096
bdbf12a2a854 mod_anti_spam: Add some debug logs
Matthew Wild <mwild1@gmail.com>
parents: 6094
diff changeset
188 module:log("debug", "Processing message from stranger...");
bdbf12a2a854 mod_anti_spam: Add some debug logs
Matthew Wild <mwild1@gmail.com>
parents: 6094
diff changeset
189
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 if is_spammy_server(event.origin) then
6124
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6106
diff changeset
191 return block_spam(event, "known-spam-source");
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 if is_spammy_sender(from_bare) then
6124
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6106
diff changeset
195 return block_spam(event, "known-spam-jid");
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 if is_spammy_content(event.stanza) then
6124
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6106
diff changeset
199 return block_spam(event, "spam-content");
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 end
6096
bdbf12a2a854 mod_anti_spam: Add some debug logs
Matthew Wild <mwild1@gmail.com>
parents: 6094
diff changeset
201
bdbf12a2a854 mod_anti_spam: Add some debug logs
Matthew Wild <mwild1@gmail.com>
parents: 6094
diff changeset
202 module:log("debug", "Allowing message through");
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 end, 500);
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205 module:hook("presence/bare", function (event)
6100
8ef4d825ad50 mod_anti_spam: Fix stanza type check in presence handler
Matthew Wild <mwild1@gmail.com>
parents: 6099
diff changeset
206 if event.stanza.attr.type ~= "subscribe" then
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207 return;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209
6124
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6106
diff changeset
210
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6106
diff changeset
211 local to_user, to_host = jid_split(event.stanza.attr.to);
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6106
diff changeset
212 local from_bare = jid_bare(event.stanza.attr.from);
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6106
diff changeset
213
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6106
diff changeset
214 if user_exists(to_user, to_host) then
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6106
diff changeset
215 if not is_from_stranger(from_bare, event) then
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6106
diff changeset
216 return;
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6106
diff changeset
217 end
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6106
diff changeset
218 end
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6106
diff changeset
219
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6106
diff changeset
220 module:log("debug", "Processing subscription request from stranger...");
6096
bdbf12a2a854 mod_anti_spam: Add some debug logs
Matthew Wild <mwild1@gmail.com>
parents: 6094
diff changeset
221
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
222 if is_spammy_server(event.origin) then
6124
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6106
diff changeset
223 return block_spam(event, "known-spam-source");
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
224 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
225
6096
bdbf12a2a854 mod_anti_spam: Add some debug logs
Matthew Wild <mwild1@gmail.com>
parents: 6094
diff changeset
226 module:log("debug", "Not from known spam source server");
bdbf12a2a854 mod_anti_spam: Add some debug logs
Matthew Wild <mwild1@gmail.com>
parents: 6094
diff changeset
227
6101
64b4ede37da1 mod_anti_spam: Fix spam sender check in presence subscriptions (thanks mirux)
Matthew Wild <mwild1@gmail.com>
parents: 6100
diff changeset
228 if is_spammy_sender(jid_bare(event.stanza.attr.from)) then
6124
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6106
diff changeset
229 return block_spam(event, "known-spam-jid");
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
230 end
6096
bdbf12a2a854 mod_anti_spam: Add some debug logs
Matthew Wild <mwild1@gmail.com>
parents: 6094
diff changeset
231
bdbf12a2a854 mod_anti_spam: Add some debug logs
Matthew Wild <mwild1@gmail.com>
parents: 6094
diff changeset
232 module:log("debug", "Not from known spam source JID");
bdbf12a2a854 mod_anti_spam: Add some debug logs
Matthew Wild <mwild1@gmail.com>
parents: 6094
diff changeset
233
bdbf12a2a854 mod_anti_spam: Add some debug logs
Matthew Wild <mwild1@gmail.com>
parents: 6094
diff changeset
234 module:log("debug", "Allowing subscription request through");
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
235 end, 500);