Software /
code /
prosody-modules
Comparison
mod_report_forward/mod_report_forward.lua @ 6211:750d64c47ec6 draft
Merge
author | Trần H. Trung <xmpp:trần.h.trung@trung.fun> |
---|---|
date | Tue, 18 Mar 2025 00:31:36 +0700 |
parent | 6142:fc521fb5ffa0 |
comparison
equal
deleted
inserted
replaced
6210:24316a399978 | 6211:750d64c47ec6 |
---|---|
3 local st = require "util.stanza"; | 3 local st = require "util.stanza"; |
4 local url = require "socket.url"; | 4 local url = require "socket.url"; |
5 | 5 |
6 local new_id = require "util.id".short; | 6 local new_id = require "util.id".short; |
7 local render = require"util.interpolation".new("%b{}", function (s) return s; end); | 7 local render = require"util.interpolation".new("%b{}", function (s) return s; end); |
8 | |
9 local count_report = module:metric("counter", "forwarded", "reports", "Number of spam and abuse reports forwarded to remote receivers."); | |
8 | 10 |
9 module:depends("spam_reporting"); | 11 module:depends("spam_reporting"); |
10 | 12 |
11 local destinations = module:get_option_set("report_forward_to", {}); | 13 local destinations = module:get_option_set("report_forward_to", {}); |
12 | 14 |
13 local archive = module:open_store("archive", "archive"); | 15 local archive = module:open_store("archive", "archive"); |
14 | 16 |
15 local cache_size = module:get_option_number("report_forward_contact_cache_size", 256); | 17 local cache_size = module:get_option_number("report_forward_contact_cache_size", 256); |
16 local report_to_origin = module:get_option_boolean("report_forward_to_origin", true); | 18 local report_to_origin = module:get_option_boolean("report_forward_to_origin", true); |
19 local report_to_origin_fallback = module:get_option_boolean("report_forward_to_origin_fallback", true); | |
17 local contact_lookup_timeout = module:get_option_number("report_forward_contact_lookup_timeout", 180); | 20 local contact_lookup_timeout = module:get_option_number("report_forward_contact_lookup_timeout", 180); |
18 | 21 |
19 local body_template = module:get_option_string("report_forward_body_template", [[ | 22 local body_template = module:get_option_string("report_forward_body_template", [[ |
20 SPAM/ABUSE REPORT | 23 SPAM/ABUSE REPORT |
21 ----------------- | 24 ----------------- |
28 {reported_message_time&The reported message was sent at: {reported_message_time}} | 31 {reported_message_time&The reported message was sent at: {reported_message_time}} |
29 | 32 |
30 -- | 33 -- |
31 This message contains also machine-readable payloads, including XEP-0377, in case | 34 This message contains also machine-readable payloads, including XEP-0377, in case |
32 you want to automate handling of these reports. You can receive these reports | 35 you want to automate handling of these reports. You can receive these reports |
33 to a different address by setting 'spam-report-addresses' in your server | 36 to a different address by setting 'report-addresses' in your server |
34 contact info configuration. For more information, see https://xmppbl.org/reports/ | 37 contact info configuration. For more information, see https://xmppbl.org/reports/ |
35 ]]):gsub("^%s+", ""):gsub("(%S)\n(%S)", "%1 %2"); | 38 ]]):gsub("^%s+", ""):gsub("(%S)\n(%S)", "%1 %2"); |
36 | 39 |
37 local report_addresses = require "util.cache".new(cache_size); | 40 local report_addresses = require "util.cache".new(cache_size); |
38 | 41 |
63 | 66 |
64 return module:send_iq(contact_query, prosody.hosts[module.host], contact_lookup_timeout) | 67 return module:send_iq(contact_query, prosody.hosts[module.host], contact_lookup_timeout) |
65 :next(function (result) | 68 :next(function (result) |
66 module:log("debug", "Processing contact form..."); | 69 module:log("debug", "Processing contact form..."); |
67 local response = result.stanza; | 70 local response = result.stanza; |
68 if response.attr.type ~= "result" then | 71 if response.attr.type == "result" then |
69 module:log("warn", "Failed to query contact addresses of %s: %s", host, response); | 72 for form in response.tags[1]:childtags("x", "jabber:x:data") do |
70 return; | 73 local form_type = form:get_child_with_attr("field", nil, "var", "FORM_TYPE"); |
74 if form_type and form_type:get_child_text("value") == "http://jabber.org/network/serverinfo" then | |
75 address = get_address(form, "report-addresses", "abuse-addresses"); | |
76 break; | |
77 end | |
78 end | |
71 end | 79 end |
72 | 80 |
73 for form in response.tags[1]:childtags("x", "jabber:x:data") do | 81 if not address then |
74 local form_type = form:get_child_with_attr("field", nil, "var", "FORM_TYPE"); | 82 if report_to_origin_fallback then |
75 if form_type and form_type:get_child_text("value") == "http://jabber.org/network/serverinfo" then | 83 -- If no contact address found, but fallback is enabled, |
76 address = get_address(form, "spam-report-addresses", "abuse-addresses"); | 84 -- just send the report to the domain |
77 break; | 85 module:log("debug", "Falling back to domain to send report to %s", host); |
86 address = host; | |
87 else | |
88 module:log("warn", "Failed to query contact addresses of %s: %s", host, response); | |
78 end | 89 end |
79 end | 90 end |
91 | |
80 return address; | 92 return address; |
81 end); | 93 end); |
82 end | 94 end |
83 | 95 |
84 local function send_report(to, message) | 96 local function send_report(to, message) |
106 local reported_message, reported_message_time, reported_message_with; | 118 local reported_message, reported_message_time, reported_message_with; |
107 if reported_message_el then | 119 if reported_message_el then |
108 reported_message, reported_message_time, reported_message_with = archive:get(reporter_username, reported_message_el.attr.id); | 120 reported_message, reported_message_time, reported_message_with = archive:get(reporter_username, reported_message_el.attr.id); |
109 if jid.bare(reported_message_with) ~= event.jid then | 121 if jid.bare(reported_message_with) ~= event.jid then |
110 reported_message = nil; | 122 reported_message = nil; |
123 reported_message_time = nil; | |
111 end | 124 end |
112 end | 125 end |
113 | 126 |
114 local body_text = render(body_template, { | 127 local body_text = render(body_template, { |
115 reporter_jid = reporter_jid; | 128 reporter_jid = reporter_jid; |
128 :add_child(reported_message); | 141 :add_child(reported_message); |
129 message:add_child(fwd); | 142 message:add_child(fwd); |
130 end | 143 end |
131 | 144 |
132 for destination in destinations do | 145 for destination in destinations do |
146 count_report:with_labels():add(1); | |
133 send_report(destination, message); | 147 send_report(destination, message); |
134 end | 148 end |
135 | 149 |
136 if report_to_origin then | 150 if report_to_origin then |
137 module:log("debug", "Sending report to origin server..."); | 151 module:log("debug", "Sending report to origin server..."); |