Mercurial > prosody-modules
annotate mod_report_tracker/mod_report_tracker.lua @ 6035:b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Mon, 25 Nov 2024 13:12:20 +0000 |
| parents | |
| children | 765e0235c202 |
| rev | line source |
|---|---|
|
6035
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 local um = require "core.usermanager"; |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 local jid = require "util.jid"; |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 local trusted_reporters = module:get_option_inherited_set("trusted_reporters", {}); |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 local reports_received = module:open_store("reports_received"); |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 local xmlns_reporting = "urn:xmpp:reporting:1"; |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 local function is_trusted_reporter(reporter_jid) |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 return trusted_reporters:contains(reporter_jid); |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 end |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 function handle_report(event) |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 local stanza = event.stanza; |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 local report = stanza:get_child("report", xmlns_reporting); |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 if not report then |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 return; |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 end |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 local reported_jid = report:get_child_text("jid", "urn:xmpp:jid:0") |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 or stanza:find("{urn:xmpp:forward:0}forwarded/{jabber:client}message@from"); |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 if not reported_jid then |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 module:log("debug", "Discarding report with no JID"); |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 return; |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 elseif jid.host(reported_jid) ~= module.host then |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 module:log("debug", "Discarding report about non-local user"); |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 return; |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 end |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 local reporter_jid = stanza.attr.from; |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 if jid.node(reporter_jid) then |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 module:log("debug", "Discarding report from non-server JID"); |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 return; |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 end |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 local reported_user = jid.node(reported_jid); |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 if not um.user_exists(reported_user, module.host) then |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 module:log("debug", "Discarding report about non-existent user"); |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 return; |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 end |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 if is_trusted_reporter(reporter_jid) then |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 local current_reports = reports_received:get(reported_user, reporter_jid); |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 if not current_reports then |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 current_reports = { |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 first = os.time(); |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 last = os.time(); |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 count = 1; |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 }; |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 else |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 current_reports.last = os.time(); |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 current_reports.count = current_reports.count + 1; |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 end |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 reports_received:set(reported_user, reporter_jid, current_reports); |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 module:log("info", "Received abuse report about <%s> from <%s>", reported_jid, reporter_jid); |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 module:fire_event(module.name.."/account-reported", { |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 report_from = reporter_jid; |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 reported_user = reported_user; |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 report = report; |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 }); |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 else |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 module:log("warn", "Discarding abuse report about <%s> from untrusted source <%s>", reported_jid, reporter_jid); |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 end |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 -- Message was handled |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 return true; |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 end |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 |
|
b04518fa0987
mod_report_tracker: Keep track of spam/abuse reports about local JIDs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 module:hook("message/host", handle_report); |
