comparison mod_spam_reporting/mod_spam_reporting.lua @ 4660:ce826ac8496e

mod_spam_reporting: Refactor to deduplicate code Same event firing code regardless of protocol now.
author Kim Alvefur <zash@zash.se>
date Wed, 25 Aug 2021 19:04:40 +0200
parents cc8b221f137c
children e40ed7d93d68
comparison
equal deleted inserted replaced
4659:cc8b221f137c 4660:ce826ac8496e
12 module:add_feature("urn:xmpp:reporting:reason:abuse:0"); 12 module:add_feature("urn:xmpp:reporting:reason:abuse:0");
13 module:add_feature("urn:xmpp:reporting:1"); 13 module:add_feature("urn:xmpp:reporting:1");
14 14
15 module:hook("iq-set/self/urn:xmpp:blocking:block", function (event) 15 module:hook("iq-set/self/urn:xmpp:blocking:block", function (event)
16 for item in event.stanza.tags[1]:childtags("item") do 16 for item in event.stanza.tags[1]:childtags("item") do
17 local report = item:get_child("report", "urn:xmpp:reporting:0"); 17 local report = item:get_child("report", "urn:xmpp:reporting:0") or item:get_child("report", "urn:xmpp:reporting:1");
18 local jid = jid_prep(item.attr.jid); 18 local jid = jid_prep(item.attr.jid);
19 if report and jid then 19 if report and jid then
20 local report_type = report:get_child("spam") and "spam" or 20 local report_type, reason;
21 report:get_child("abuse") and "abuse" or 21 if report.attr.xmlns == "urn:xmpp:reporting:0" then
22 "unknown"; 22 report_type = report:get_child("spam") and "spam" or report:get_child("abuse") and "abuse" or "unknown";
23 local reason = report:get_child_text("text"); 23 reason = report:get_child_text("text");
24 module:log("warn", "Received report of %s from JID '%s', %s", type, jid, reason); 24 elseif report.attr.xmlns == "urn:xmpp:reporting:1" then
25 module:fire_event(module.name.."/"..report_type.."-report", { 25 report_type = "unknown";
26 origin = event.origin, stanza = event.stanza, jid = jid,
27 item = item, report = report, reason = reason, });
28 else
29 report = item:get_child("report", "urn:xmpp:reporting:1");
30 if report and jid then
31 local report_type = "unknown";
32 if report.attr.reason == "urn:xmpp:reporting:abuse" then 26 if report.attr.reason == "urn:xmpp:reporting:abuse" then
33 report_type = "abuse"; 27 report_type = "abuse";
34 end 28 end
35 if report.attr.reason == "urn:xmpp:reporting:spam" then 29 if report.attr.reason == "urn:xmpp:reporting:spam" then
36 report_type = "spam"; 30 report_type = "spam";
37 end 31 end
38 local reason = report:get_child_text("text"); 32 reason = report:get_child_text("text");
33 end
34
35 if report_type then
39 module:log("warn", "Received report of %s from JID '%s', %s", type, jid, reason); 36 module:log("warn", "Received report of %s from JID '%s', %s", type, jid, reason);
40 module:fire_event(module.name.."/"..report_type.."-report", { 37 module:fire_event(module.name.."/"..report_type.."-report", {
41 origin = event.origin, stanza = event.stanza, jid = jid, 38 origin = event.origin, stanza = event.stanza, jid = jid,
42 item = item, report = report, reason = reason, }); 39 item = item, report = report, reason = reason, });
43 end 40 end