Annotate

mod_anti_spam/mod_anti_spam.lua @ 6155:d80e398a2acc

mod_anti_spam: Also consider not a stranger if we have a pending (out) sub request
author Matthew Wild <mwild1@gmail.com>
date Fri, 17 Jan 2025 11:02:02 +0000
parent 6154:160d1bcfe341
child 6157:ddb0fac151ac
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5883
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;
6152
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6134
diff changeset
8 local is_user_subscribed = require "core.rostermanager".is_user_subscribed;
5883
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
6134
00b55c7ef393 Backed out changeset ffec70ddbffc
Matthew Wild <mwild1@gmail.com>
parents: 6132
diff changeset
16 local spam_source_domains = set.new();
00b55c7ef393 Backed out changeset ffec70ddbffc
Matthew Wild <mwild1@gmail.com>
parents: 6132
diff changeset
17 local spam_source_ips = trie.new();
00b55c7ef393 Backed out changeset ffec70ddbffc
Matthew Wild <mwild1@gmail.com>
parents: 6132
diff changeset
18 local spam_source_jids = set.new();
6152
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6134
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: 6134
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: 6134
diff changeset
21
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6134
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: 6134
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: 6134
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: 6134
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: 6134
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: 6134
diff changeset
27 end;
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6134
diff changeset
28 });
5883
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
6125
a54b94a3e994 mod_anti_spam: Fix luacheck warning
Matthew Wild <mwild1@gmail.com>
parents: 6124
diff changeset
32 local hosts = prosody.hosts;
a54b94a3e994 mod_anti_spam: Fix luacheck warning
Matthew Wild <mwild1@gmail.com>
parents: 6124
diff changeset
33
6126
4b1a4be43487 mod_anti_spam: Improve error bounce text with actionable info
Matthew Wild <mwild1@gmail.com>
parents: 6125
diff changeset
34 local reason_messages = {
4b1a4be43487 mod_anti_spam: Improve error bounce text with actionable info
Matthew Wild <mwild1@gmail.com>
parents: 6125
diff changeset
35 default = "Rejected as spam";
4b1a4be43487 mod_anti_spam: Improve error bounce text with actionable info
Matthew Wild <mwild1@gmail.com>
parents: 6125
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: 6125
diff changeset
37 };
4b1a4be43487 mod_anti_spam: Improve error bounce text with actionable info
Matthew Wild <mwild1@gmail.com>
parents: 6125
diff changeset
38
5883
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)
6152
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6134
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: 6134
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: 6134
diff changeset
42 end
5883
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);
6126
4b1a4be43487 mod_anti_spam: Improve error bounce text with actionable info
Matthew Wild <mwild1@gmail.com>
parents: 6125
diff changeset
54 event.origin.send(st.error_reply("cancel", "policy-violation", reason_messages[reason] or reason_messages.default));
5883
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
6155
d80e398a2acc mod_anti_spam: Also consider not a stranger if we have a pending (out) sub request
Matthew Wild <mwild1@gmail.com>
parents: 6154
diff changeset
71 if not (
d80e398a2acc mod_anti_spam: Also consider not a stranger if we have a pending (out) sub request
Matthew Wild <mwild1@gmail.com>
parents: 6154
diff changeset
72 is_contact_subscribed(to_user, to_host, from_jid) or
d80e398a2acc mod_anti_spam: Also consider not a stranger if we have a pending (out) sub request
Matthew Wild <mwild1@gmail.com>
parents: 6154
diff changeset
73 is_user_subscribed(to_user, to_host, from_jid) or
d80e398a2acc mod_anti_spam: Also consider not a stranger if we have a pending (out) sub request
Matthew Wild <mwild1@gmail.com>
parents: 6154
diff changeset
74 is_contact_pending_out(to_user, to_host, from_jid)
d80e398a2acc mod_anti_spam: Also consider not a stranger if we have a pending (out) sub request
Matthew Wild <mwild1@gmail.com>
parents: 6154
diff changeset
75 ) then
6153
ef7bf4215cb3 mod_anti_spam: Update definition of "stranger" to include sub to/from JID or domains
Matthew Wild <mwild1@gmail.com>
parents: 6152
diff changeset
76 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: 6152
diff changeset
77
5883
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 -- Allow all messages from your own jid
6154
160d1bcfe341 mod_anti_spam: Optimization to avoid needless string concatenation
Matthew Wild <mwild1@gmail.com>
parents: 6153
diff changeset
79 if from_user == to_user and from_host == to_host then
5883
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 return false; -- Pass through
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 if to_resource and stanza.attr.type == "groupchat" then
6153
ef7bf4215cb3 mod_anti_spam: Update definition of "stranger" to include sub to/from JID or domains
Matthew Wild <mwild1@gmail.com>
parents: 6152
diff changeset
83 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: 6152
diff changeset
84 end
ef7bf4215cb3 mod_anti_spam: Update definition of "stranger" to include sub to/from JID or domains
Matthew Wild <mwild1@gmail.com>
parents: 6152
diff changeset
85 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: 6152
diff changeset
86 -- 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: 6152
diff changeset
87 -- 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: 6152
diff changeset
88 return false;
5883
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 return true; -- Stranger danger
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 function is_spammy_server(session)
6134
00b55c7ef393 Backed out changeset ffec70ddbffc
Matthew Wild <mwild1@gmail.com>
parents: 6132
diff changeset
95 if spam_source_domains:contains(session.from_host) then
5883
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 return true;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 end
6117
b4a4f4094337 mod_anti_spam: Fix traceback on missing/invalid session IP and log warning
Matthew Wild <mwild1@gmail.com>
parents: 6116
diff changeset
98 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: 6116
diff changeset
99 local parsed_ip = raw_ip and ip.new_ip(session.ip);
6120
bd3ff802d883 mod_anti_spam: Fix another traceback for origin sessions without an IP
Matthew Wild <mwild1@gmail.com>
parents: 6117
diff changeset
100 -- 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: 6117
diff changeset
101 -- local host session
6134
00b55c7ef393 Backed out changeset ffec70ddbffc
Matthew Wild <mwild1@gmail.com>
parents: 6132
diff changeset
102 if parsed_ip and spam_source_ips:contains_ip(parsed_ip) then
5883
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 return true;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 end
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 function is_spammy_sender(sender_jid)
6134
00b55c7ef393 Backed out changeset ffec70ddbffc
Matthew Wild <mwild1@gmail.com>
parents: 6132
diff changeset
108 return spam_source_jids:contains(sha256(sender_jid, true));
5883
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 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
112 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
113
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 function is_spammy_content(stanza)
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 -- Only support message content
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 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
117 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
118
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 local body = stanza:get_child_text("body");
6127
559648c7c8d6 mod_anti_spam: Skip content filtering for messages with no body (thanks Martin)
Matthew Wild <mwild1@gmail.com>
parents: 6126
diff changeset
120 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: 6126
diff changeset
121
5883
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 if spammy_strings then
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 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
124 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
125 return true;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 if spammy_patterns then
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 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
131 if body:find(s) then
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 return true;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 end
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 -- Set up RTBLs
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139
6121
255ab0b81f14 mod_anti_spam: Fix traceback when no anti-spam services defined in config (thanks mirux)
Matthew Wild <mwild1@gmail.com>
parents: 6120
diff changeset
140 local anti_spam_services = module:get_option_array("anti_spam_services", {});
5883
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 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
143 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
144 added = function (item)
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 spam_source_domains:add(item);
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 end;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 removed = function (item)
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 spam_source_domains:remove(item);
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 end;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 });
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 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
152 added = function (item)
6130
5a0e47ad7d6b mod_anti_spam: Gracefully handle failure to parse CIDR in IP RTBL
Matthew Wild <mwild1@gmail.com>
parents: 6129
diff changeset
153 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: 6129
diff changeset
154 if not subnet_ip then
5a0e47ad7d6b mod_anti_spam: Gracefully handle failure to parse CIDR in IP RTBL
Matthew Wild <mwild1@gmail.com>
parents: 6129
diff changeset
155 return;
5a0e47ad7d6b mod_anti_spam: Gracefully handle failure to parse CIDR in IP RTBL
Matthew Wild <mwild1@gmail.com>
parents: 6129
diff changeset
156 end
5a0e47ad7d6b mod_anti_spam: Gracefully handle failure to parse CIDR in IP RTBL
Matthew Wild <mwild1@gmail.com>
parents: 6129
diff changeset
157 spam_source_ips:add_subnet(subnet_ip, subnet_bits);
5883
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 end;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 removed = function (item)
6130
5a0e47ad7d6b mod_anti_spam: Gracefully handle failure to parse CIDR in IP RTBL
Matthew Wild <mwild1@gmail.com>
parents: 6129
diff changeset
160 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: 6129
diff changeset
161 if not subnet_ip then
5a0e47ad7d6b mod_anti_spam: Gracefully handle failure to parse CIDR in IP RTBL
Matthew Wild <mwild1@gmail.com>
parents: 6129
diff changeset
162 return;
5a0e47ad7d6b mod_anti_spam: Gracefully handle failure to parse CIDR in IP RTBL
Matthew Wild <mwild1@gmail.com>
parents: 6129
diff changeset
163 end
5a0e47ad7d6b mod_anti_spam: Gracefully handle failure to parse CIDR in IP RTBL
Matthew Wild <mwild1@gmail.com>
parents: 6129
diff changeset
164 spam_source_ips:remove_subnet(subnet_ip, subnet_bits);
5883
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 end;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 });
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 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
168 added = function (item)
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 spam_source_jids:add(item);
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 end;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 removed = function (item)
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 spam_source_jids:remove(item);
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 end;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 });
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 module:hook("message/bare", function (event)
6115
b644832a8290 mod_anti_spam: Fix syntax of user_exists() check (thanks Martin/Zash)
Matthew Wild <mwild1@gmail.com>
parents: 5883
diff changeset
178 local to_user, to_host = jid_split(event.stanza.attr.to);
5883
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179
6122
15f6f1566bdb mod_anti_spam: Prevent traceback when processing a message to an unknown host
Matthew Wild <mwild1@gmail.com>
parents: 6121
diff changeset
180 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: 6121
diff changeset
181 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: 6121
diff changeset
182 return;
15f6f1566bdb mod_anti_spam: Prevent traceback when processing a message to an unknown host
Matthew Wild <mwild1@gmail.com>
parents: 6121
diff changeset
183 end
15f6f1566bdb mod_anti_spam: Prevent traceback when processing a message to an unknown host
Matthew Wild <mwild1@gmail.com>
parents: 6121
diff changeset
184
5883
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 local from_bare = jid_bare(event.stanza.attr.from);
6152
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6134
diff changeset
186 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: 6134
diff changeset
187 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: 6134
diff changeset
188 return;
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6134
diff changeset
189 end
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6134
diff changeset
190 end
5883
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191
6124
bdbf12a2a854 mod_anti_spam: Add some debug logs
Matthew Wild <mwild1@gmail.com>
parents: 6122
diff changeset
192 module:log("debug", "Processing message from stranger...");
bdbf12a2a854 mod_anti_spam: Add some debug logs
Matthew Wild <mwild1@gmail.com>
parents: 6122
diff changeset
193
5883
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 if is_spammy_server(event.origin) then
6152
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6134
diff changeset
195 return block_spam(event, "known-spam-source");
5883
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_sender(from_bare) then
6152
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6134
diff changeset
199 return block_spam(event, "known-spam-jid");
5883
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 if is_spammy_content(event.stanza) then
6152
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6134
diff changeset
203 return block_spam(event, "spam-content");
5883
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 end
6124
bdbf12a2a854 mod_anti_spam: Add some debug logs
Matthew Wild <mwild1@gmail.com>
parents: 6122
diff changeset
205
bdbf12a2a854 mod_anti_spam: Add some debug logs
Matthew Wild <mwild1@gmail.com>
parents: 6122
diff changeset
206 module:log("debug", "Allowing message through");
5883
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207 end, 500);
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209 module:hook("presence/bare", function (event)
6128
8ef4d825ad50 mod_anti_spam: Fix stanza type check in presence handler
Matthew Wild <mwild1@gmail.com>
parents: 6127
diff changeset
210 if event.stanza.attr.type ~= "subscribe" then
5883
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211 return;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
212 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213
6152
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6134
diff changeset
214
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6134
diff changeset
215 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: 6134
diff changeset
216 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: 6134
diff changeset
217
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6134
diff changeset
218 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: 6134
diff changeset
219 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: 6134
diff changeset
220 return;
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6134
diff changeset
221 end
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6134
diff changeset
222 end
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6134
diff changeset
223
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6134
diff changeset
224 module:log("debug", "Processing subscription request from stranger...");
6124
bdbf12a2a854 mod_anti_spam: Add some debug logs
Matthew Wild <mwild1@gmail.com>
parents: 6122
diff changeset
225
5883
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
226 if is_spammy_server(event.origin) then
6152
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6134
diff changeset
227 return block_spam(event, "known-spam-source");
5883
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
228 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
229
6124
bdbf12a2a854 mod_anti_spam: Add some debug logs
Matthew Wild <mwild1@gmail.com>
parents: 6122
diff changeset
230 module:log("debug", "Not from known spam source server");
bdbf12a2a854 mod_anti_spam: Add some debug logs
Matthew Wild <mwild1@gmail.com>
parents: 6122
diff changeset
231
6129
64b4ede37da1 mod_anti_spam: Fix spam sender check in presence subscriptions (thanks mirux)
Matthew Wild <mwild1@gmail.com>
parents: 6128
diff changeset
232 if is_spammy_sender(jid_bare(event.stanza.attr.from)) then
6152
5961e01dd963 mod_anti_spam: Allow customizing spam actions (drop/bounce), switching default to bounce
Matthew Wild <mwild1@gmail.com>
parents: 6134
diff changeset
233 return block_spam(event, "known-spam-jid");
5883
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
234 end
6124
bdbf12a2a854 mod_anti_spam: Add some debug logs
Matthew Wild <mwild1@gmail.com>
parents: 6122
diff changeset
235
bdbf12a2a854 mod_anti_spam: Add some debug logs
Matthew Wild <mwild1@gmail.com>
parents: 6122
diff changeset
236 module:log("debug", "Not from known spam source JID");
bdbf12a2a854 mod_anti_spam: Add some debug logs
Matthew Wild <mwild1@gmail.com>
parents: 6122
diff changeset
237
bdbf12a2a854 mod_anti_spam: Add some debug logs
Matthew Wild <mwild1@gmail.com>
parents: 6122
diff changeset
238 module:log("debug", "Allowing subscription request through");
5883
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
239 end, 500);