comparison mod_report_tracker/mod_report_tracker.lua @ 6110:9db1529c06c2

Merge upstream
author tmolitor <thilo@eightysoft.de>
date Sun, 05 Jan 2025 17:50:02 +0100
parents 765e0235c202
children
comparison
equal deleted inserted replaced
6109:4cb1cad2badd 6110:9db1529c06c2
1 local um = require "core.usermanager";
2 local cache = require "util.cache";
3 local jid = require "util.jid";
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 reported_users = cache.new(256);
11
12 local function is_trusted_reporter(reporter_jid)
13 return trusted_reporters:contains(reporter_jid);
14 end
15
16 function handle_report(event)
17 local stanza = event.stanza;
18 local report = stanza:get_child("report", xmlns_reporting);
19 if not report then
20 return;
21 end
22 local reported_jid = report:get_child_text("jid", "urn:xmpp:jid:0")
23 or stanza:find("{urn:xmpp:forward:0}forwarded/{jabber:client}message@from");
24 if not reported_jid then
25 module:log("debug", "Discarding report with no JID");
26 return;
27 elseif jid.host(reported_jid) ~= module.host then
28 module:log("debug", "Discarding report about non-local user");
29 return;
30 end
31
32 local reporter_jid = stanza.attr.from;
33 if jid.node(reporter_jid) then
34 module:log("debug", "Discarding report from non-server JID");
35 return;
36 end
37
38 local reported_user = jid.node(reported_jid);
39 if not um.user_exists(reported_user, module.host) then
40 module:log("debug", "Discarding report about non-existent user");
41 return;
42 end
43
44 if is_trusted_reporter(reporter_jid) then
45 local current_reports = reports_received:get(reported_user, reporter_jid);
46
47 if not current_reports then
48 current_reports = {
49 first = os.time();
50 last = os.time();
51 count = 1;
52 };
53 else
54 current_reports.last = os.time();
55 current_reports.count = current_reports.count + 1;
56 end
57
58 reports_received:set(reported_user, reporter_jid, current_reports);
59 reported_users:set(reported_user, true);
60
61 module:log("info", "Received abuse report about <%s> from <%s>", reported_jid, reporter_jid);
62
63 module:fire_event(module.name.."/account-reported", {
64 report_from = reporter_jid;
65 reported_user = reported_user;
66 report = report;
67 });
68 else
69 module:log("warn", "Discarding abuse report about <%s> from untrusted source <%s>", reported_jid, reporter_jid);
70 end
71
72 -- Message was handled
73 return true;
74 end
75
76 module:hook("message/host", handle_report);
77
78 module:add_item("account-trait", {
79 name = "reported-by-trusted-server";
80 prob_bad_true = 0.80;
81 prob_bad_false = 0.50;
82 });
83
84 module:hook("get-account-traits", function (event)
85 local username = event.username;
86 local reported = reported_users:get(username);
87 if reported == nil then
88 -- Check storage, update cache
89 reported = not not reports_received:get(username);
90 reported_users:set(username, reported);
91 end
92 event.traits["reported-by-trusted-server"] = reported;
93 end);