annotate mod_anti_spam/mod_anti_spam.lua @ 6104:ffec70ddbffc

mod_flags: trunk version backported to 0.12
author Matthew Wild <mwild1@gmail.com>
date Sat, 04 Jan 2025 17:50:35 +0000
parents 5a0e47ad7d6b
children 00b55c7ef393
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6104
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
1 local cache = require "util.cache";
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local ip = require "util.ip";
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local jid_bare = require "util.jid".bare;
6104
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
4 local jid_host = require "util.jid".host;
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 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
6 local set = require "util.set";
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 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
8 local st = require"util.stanza";
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local is_contact_subscribed = require "core.rostermanager".is_contact_subscribed;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 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
11
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 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
13
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 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
15 local trie = module:require("trie");
6104
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
16 local pset = module:require("pset");
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17
6104
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
18 -- { [service_jid] = set, ... }
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
19 local spam_source_domains_by_service = {};
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
20 local spam_source_ips_by_service = {};
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
21 local spam_source_jids_by_service = {};
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
22
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
23 local service_probabilities = {
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
24 -- if_present = probability the address is a spammer if they are on the list
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
25 -- if_absent (optional): probability the address is a spammer if they are not on the list
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
26 -- [service_jid] = { if_present = 0.9, if_absent = 0.5 };
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
27 };
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
28
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
29
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
30 -- These "probabilistic sets" combine the multiple lists according to their weights
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
31 local p_spam_source_domains = pset.new(spam_source_domains_by_service, service_probabilities);
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
32 local p_spam_source_ips = pset.new(spam_source_ips_by_service, service_probabilities);
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
33 local p_spam_source_jids = pset.new(spam_source_jids_by_service, service_probabilities);
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
34
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
35 local domain_local_report_threshold = module:get_option_number("anti_spam_local_report_threshold", 2);
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 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
38
6097
a54b94a3e994 mod_anti_spam: Fix luacheck warning
Matthew Wild <mwild1@gmail.com>
parents: 6096
diff changeset
39 local hosts = prosody.hosts;
a54b94a3e994 mod_anti_spam: Fix luacheck warning
Matthew Wild <mwild1@gmail.com>
parents: 6096
diff changeset
40
6098
4b1a4be43487 mod_anti_spam: Improve error bounce text with actionable info
Matthew Wild <mwild1@gmail.com>
parents: 6097
diff changeset
41 local reason_messages = {
4b1a4be43487 mod_anti_spam: Improve error bounce text with actionable info
Matthew Wild <mwild1@gmail.com>
parents: 6097
diff changeset
42 default = "Rejected as spam";
4b1a4be43487 mod_anti_spam: Improve error bounce text with actionable info
Matthew Wild <mwild1@gmail.com>
parents: 6097
diff changeset
43 ["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
44 };
4b1a4be43487 mod_anti_spam: Improve error bounce text with actionable info
Matthew Wild <mwild1@gmail.com>
parents: 6097
diff changeset
45
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 function block_spam(event, reason, action)
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 event.spam_reason = reason;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 event.spam_action = action;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 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
50 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
51 return;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 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
55
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 if action == "bounce" then
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 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
58 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
59 else
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 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
61 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 return true;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 end
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 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
67 local stanza = event.stanza;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 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
69
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 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
71
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 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
73 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
74
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 if not is_contact_subscribed(to_user, to_host, from_jid) then
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 -- Allow all messages from your own jid
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 if from_jid == to_user.."@"..to_host then
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 return false; -- Pass through
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 if to_resource and stanza.attr.type == "groupchat" then
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 return false; -- Pass through
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 return true; -- Stranger danger
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 end
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
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 function is_spammy_server(session)
6104
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
88 if p_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
89 return true;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 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
91 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
92 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
93 -- 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
94 -- local host session
6104
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
95 if parsed_ip and p_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
96 return true;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 function is_spammy_sender(sender_jid)
6104
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
101 return p_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
102 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 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
105 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
106
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 function is_spammy_content(stanza)
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 -- Only support message content
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 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
110 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
111
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 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
113 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
114
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 if spammy_strings then
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 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
117 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
118 return true;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 if spammy_patterns 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_patterns) 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) 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 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 -- Set up RTBLs
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132
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
133 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
134
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 for _, rtbl_service_jid in ipairs(anti_spam_services) do
6104
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
136 service_probabilities[rtbl_service_jid] = { if_present = 0.95 };
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
137
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
138 local spam_source_domains = set.new();
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
139 local spam_source_ips = trie.new();
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
140 local spam_source_jids = set.new();
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
141
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
142 spam_source_domains_by_service[rtbl_service_jid] = spam_source_domains;
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
143 spam_source_ips_by_service[rtbl_service_jid] = spam_source_ips;
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
144 spam_source_jids_by_service[rtbl_service_jid] = spam_source_jids;
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
145
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 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
147 added = 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:add(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 removed = function (item)
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 spam_source_domains:remove(item);
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 end;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 });
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 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
155 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
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: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
161 end;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 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
163 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
164 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
165 return;
5a0e47ad7d6b mod_anti_spam: Gracefully handle failure to parse CIDR in IP RTBL
Matthew Wild <mwild1@gmail.com>
parents: 6101
diff changeset
166 end
5a0e47ad7d6b mod_anti_spam: Gracefully handle failure to parse CIDR in IP RTBL
Matthew Wild <mwild1@gmail.com>
parents: 6101
diff changeset
167 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
168 end;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 });
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 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
171 added = 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:add(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 removed = function (item)
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 spam_source_jids:remove(item);
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 end;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 });
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179
6104
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
180 -- And local reports...
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
181
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
182 do
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
183 local spam_source_domains = set.new();
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
184 local spam_source_ips = set.new();
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
185
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
186 local domain_counts = cache.new(100);
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
187
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
188 service_probabilities[module.host] = { if_present = 0.6, if_absent = 0.4 };
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
189
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
190 module:hook("mod_spam_reporting/spam-report", function (event)
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
191 -- TODO: check for >= prosody:member
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
192 local reported_jid = event.jid;
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
193 local reported_domain = jid_host(reported_jid);
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
194 local report_count = (domain_counts:get(reported_domain) or 0) + 1;
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
195 domain_counts:set(reported_domain, report_count);
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
196
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
197 if report_count >= domain_local_report_threshold then
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
198 spam_source_domains:add(reported_domain);
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
199 end
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
200 end);
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
201
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
202 module:add_item("shell-command", {
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
203 section = "antispam";
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
204 section_desc = "Anti-spam management commands";
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
205 name = "filter_domain";
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
206 desc = "Restrict interactions from a remote domain to a virtual host";
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
207 args = {
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
208 { name = "host", type = "string" };
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
209 { name = "remote_domain", type = "string" };
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
210 };
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
211 host_selector = "host";
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
212 handler = function(self, host, remote_domain) --luacheck: ignore 212/self 212/host
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
213 spam_source_domains:add(remote_domain);
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
214 return true, "Remote domain now restricted: "..remote_domain;
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
215 end;
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
216 });
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
217
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
218 module:add_item("shell-command", {
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
219 section = "antispam";
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
220 section_desc = "Anti-spam management commands";
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
221 name = "filter_ip";
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
222 desc = "Restrict interactions from a remote IP/CIDR to a virtual host";
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
223 args = {
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
224 { name = "host", type = "string" };
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
225 { name = "remote_ip", type = "string" };
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
226 };
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
227 host_selector = "host";
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
228 handler = function(self, host, remote_ip) --luacheck: ignore 212/self 212/host
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
229 local subnet_ip, subnet_bits = ip.parse_cidr(remote_ip);
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
230 if not subnet_ip then
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
231 return false, subnet_bits; -- false, err
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
232 end
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
233
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
234 spam_source_ips:add_subnet(subnet_ip, subnet_bits);
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
235
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
236 return true, "Remote IP now restricted: "..remote_ip;
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
237 end;
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
238 });
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
239
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
240 end
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
241
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
242 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
243 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
244
6094
15f6f1566bdb mod_anti_spam: Prevent traceback when processing a message to an unknown host
Matthew Wild <mwild1@gmail.com>
parents: 6093
diff changeset
245 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
246 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
247 return;
15f6f1566bdb mod_anti_spam: Prevent traceback when processing a message to an unknown host
Matthew Wild <mwild1@gmail.com>
parents: 6093
diff changeset
248 end
15f6f1566bdb mod_anti_spam: Prevent traceback when processing a message to an unknown host
Matthew Wild <mwild1@gmail.com>
parents: 6093
diff changeset
249
6087
b644832a8290 mod_anti_spam: Fix syntax of user_exists() check (thanks Martin/Zash)
Matthew Wild <mwild1@gmail.com>
parents: 5859
diff changeset
250 if not user_exists(to_user, to_host) then return; end
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
251
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
252 local from_bare = jid_bare(event.stanza.attr.from);
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
253 if not is_from_stranger(from_bare, event) then return; end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
254
6096
bdbf12a2a854 mod_anti_spam: Add some debug logs
Matthew Wild <mwild1@gmail.com>
parents: 6094
diff changeset
255 module:log("debug", "Processing message from stranger...");
bdbf12a2a854 mod_anti_spam: Add some debug logs
Matthew Wild <mwild1@gmail.com>
parents: 6094
diff changeset
256
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
257 if is_spammy_server(event.origin) then
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
258 return block_spam(event, "known-spam-source", "drop");
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
259 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
260
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
261 if is_spammy_sender(from_bare) then
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
262 return block_spam(event, "known-spam-jid", "drop");
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
263 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
264
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
265 if is_spammy_content(event.stanza) then
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
266 return block_spam(event, "spam-content", "drop");
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
267 end
6096
bdbf12a2a854 mod_anti_spam: Add some debug logs
Matthew Wild <mwild1@gmail.com>
parents: 6094
diff changeset
268
bdbf12a2a854 mod_anti_spam: Add some debug logs
Matthew Wild <mwild1@gmail.com>
parents: 6094
diff changeset
269 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
270 end, 500);
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
271
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
272 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
273 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
274 return;
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
275 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
276
6096
bdbf12a2a854 mod_anti_spam: Add some debug logs
Matthew Wild <mwild1@gmail.com>
parents: 6094
diff changeset
277 module:log("debug", "Processing subscription request...");
bdbf12a2a854 mod_anti_spam: Add some debug logs
Matthew Wild <mwild1@gmail.com>
parents: 6094
diff changeset
278
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
279 if is_spammy_server(event.origin) then
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
280 return block_spam(event, "known-spam-source", "drop");
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
281 end
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
282
6096
bdbf12a2a854 mod_anti_spam: Add some debug logs
Matthew Wild <mwild1@gmail.com>
parents: 6094
diff changeset
283 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
284
6101
64b4ede37da1 mod_anti_spam: Fix spam sender check in presence subscriptions (thanks mirux)
Matthew Wild <mwild1@gmail.com>
parents: 6100
diff changeset
285 if is_spammy_sender(jid_bare(event.stanza.attr.from)) then
5859
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
286 return block_spam(event, "known-spam-jid", "drop");
259ffdbf8906 mod_anti_spam: New module for spam filtering (pre-alpha)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
287 end
6096
bdbf12a2a854 mod_anti_spam: Add some debug logs
Matthew Wild <mwild1@gmail.com>
parents: 6094
diff changeset
288
bdbf12a2a854 mod_anti_spam: Add some debug logs
Matthew Wild <mwild1@gmail.com>
parents: 6094
diff changeset
289 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
290
bdbf12a2a854 mod_anti_spam: Add some debug logs
Matthew Wild <mwild1@gmail.com>
parents: 6094
diff changeset
291 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
292 end, 500);
6104
ffec70ddbffc mod_flags: trunk version backported to 0.12
Matthew Wild <mwild1@gmail.com>
parents: 6102
diff changeset
293