[Rspamd-Users] I need your help with a LUA script for RSPAMD

christian usenet at schani.com
Mon Feb 17 14:02:44 UTC 2025


Hello,
can you please help me with a LUA script. I created it to extract good 
and bad emails from RSPAMD so that I can check them later and add them 
to Multimaps if necessary.

The script writes all email addresses, domains and IPs of senders to 
files where the score is over 30.
Emails that are contained in an "explicit_witheliste.txt" file should 
not be entered. This works great so far.

What I'm stuck on now is:
The correct recipient email addresses are not entered in 
"all_outgoing_emails.txt". In other words, recipient addresses that were 
sent from my local, authorized sending "locale_emails.map".

Does anyone know more about this? I can't find the error.
Some local sender addresses, but also incoming email addresses, are 
simply entered in "all_outgoing_emails.txt" in a jumbled manner.

Many thanks for your help
Christian



local rspamd_logger = require("rspamd_logger")
local output_file = '/etc/rspamd/spamadressen.txt'
local domain_output_file = '/etc/rspamd/spamdomains.txt'
local spam_ips_file = '/etc/rspamd/spamips.txt'
local verified_recipients_file = '/etc/rspamd/all_outgoing_emails.txt'
local spam_score_threshold = 30
local exclude_file = '/etc/rspamd/maps.d/locale_emails.map'
local domain_whitelist_file = '/etc/rspamd/explizit_witheliste.txt'

-- Funktion zum Laden der auszuschließenden Adressen
local function load_excluded_addresses(file_path)
     local excluded_addresses = {}
     for line in io.lines(file_path) do
         line = line:match("^%s*(.-)%s*$")  -- Entfernt Leerzeichen am 
Anfang und Ende der Zeile
         if line ~= '' then
             excluded_addresses[line] = true
         end
     end
     return excluded_addresses
end

-- Funktion zum Laden der Whitelist-Domains
local function load_whitelist_domains(file_path)
     local whitelist_domains = {}
     for line in io.lines(file_path) do
         line = line:match("^%s*(.-)%s*$")
         if line ~= '' then
             whitelist_domains[line] = true
         end
     end
     return whitelist_domains
end

-- Lädt die Ausschlusslisten und Whitelist beim Start
local excluded_addresses = load_excluded_addresses(exclude_file)
local whitelist_domains = load_whitelist_domains(domain_whitelist_file)

-- Extrahiert die Domain aus einer E-Mail-Adresse
local function extract_domain(email)
     return email:match("@(.+)$")
end

-- Hauptsymbol registrieren
rspamd_config:register_symbol({
     name = 'LOG_SPAM_SENDERS',
     type = 'postfilter',
     callback = function(task)
         local from = task:get_from('smtp')
         local envelope_from = task:get_from('smtp')
         local recipients = task:get_recipients('smtp')
         local ip = task:get_ip()
         local score = task:get_metric_score('default')[1]
         local action = task:get_metric_action('default')

	-- Hier habe ich die Probleme
         -- Verarbeiten von Empfängeradressen für lokale Absender
         if from and from[1] and recipients and not 
excluded_addresses[from[1]['addr']] then
             for _, recipient in ipairs(recipients) do
                 local file, err = io.open(verified_recipients_file, "a")
                 if file then
                     file:write(recipient['addr'] .. "\n")
                     file:close()
                 else
                     rspamd_logger.message(task, "Fehler beim Öffnen der 
Empfängerdatei: %s - %s", verified_recipients_file, err)
                 end
             end
         end


	-- Das hier funktioniert
         -- Überprüft, ob die E-Mail als "reject" markiert wurde oder 
einen Score über dem Schwellenwert hat
         if (action == 'reject') or (score >= spam_score_threshold) then
             local sender_addresses = {}

             if from and from[1] and from[1]['addr'] then
                 table.insert(sender_addresses, from[1]['addr'])
             end

             if envelope_from and envelope_from[1] and 
envelope_from[1]['addr'] then
                 table.insert(sender_addresses, envelope_from[1]['addr'])
             end

             for _, sender_address in ipairs(sender_addresses) do
                 local sender_domain = extract_domain(sender_address)
		
		-- Das hier funktioniert
                 -- Überprüfen, ob Absenderadresse oder Domain 
ausgeschlossen werden sollen
                 if not excluded_addresses[sender_address] and not 
(sender_domain and whitelist_domains[sender_domain]) then
                     -- E-Mail-Adresse in die Ausgabedatei schreiben
                     local file, err = io.open(output_file, "a")
                     if file then
                         file:write(sender_address .. "\n")
                         file:close()
                     else
                         rspamd_logger.message(task, "Fehler beim Öffnen 
der Ausgabedatei: %s - %s", output_file, err)
                     end

		-- Das hier funktioniert
                     -- Domain in die Domain-Ausgabedatei schreiben
                     if sender_domain then
                         local domain_file, domain_err = 
io.open(domain_output_file, "a")
                         if domain_file then
                             domain_file:write(sender_domain .. "\n")
                             domain_file:close()
                         else
                             rspamd_logger.message(task, "Fehler beim 
Öffnen der Domain-Ausgabedatei: %s - %s", domain_output_file, domain_err)
                         end
                     end
                 end
             end


		-- Das hier funktioniert
             -- IP-Adresse in die IP-Ausgabedatei schreiben
             if ip and not ip:is_local() then
                 local ip_file, ip_err = io.open(spam_ips_file, "a")
                 if ip_file then
                     ip_file:write(ip:to_string() .. "\n")
                     ip_file:close()
                 else
                     rspamd_logger.message(task, "Fehler beim Öffnen der 
IP-Ausgabedatei: %s - %s", spam_ips_file, ip_err)
                 end
             end
         end
     end
})


More information about the Users mailing list