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