rSPAMD lua module not logging to file

76 Views Asked by At

I have the following rspamd module written in lua lanaguage:

local lua_mime = require "lua_mime"
local rspamd_logger = require "rspamd_logger"
local rspamd_task = require "rspamd_task"
local ucl = require "ucl"
local lua_util = require "lua_util"
local N = "senders"
local opts = rspamd_config:get_all_opt(N)

-- Check if a trimmed string is empty or not
local function is_not_blank(str)
  return str ~= nil and str:match("%S") ~= nil
end

-- Read allowed senders from a file
local function read_allowed_senders(filename)
    local f, err = io.open(filename, "r")
    if not f then
      rspamd_logger.info("Failed to open allowed_senders.txt: %s", err)
      return {}
    end

    local allowed_senders = {}
    for line in f:lines() do
        local trimmed_line = string.match(line, "^%s*(.-)%s*$")
        if is_not_blank(trimmed_line) then
            table.insert(allowed_senders, line)
        end
    end

    f:close()
    return allowed_senders
end

-- Define the list of senders to check against
local allowed_senders = read_allowed_senders('/etc/rspamd/plugins.d/allowed_senders.txt')

-- Function to check if the sender is allowed
local function is_sender_allowed(task)
    -- Get the message sender
    local from = task:get_from('smtp')

    if not from or not from[1] or not from[1]['addr'] then
        rspamd_logger.info("Message does not appear to have a valid sender.")
        return false
    end

    -- Check if the sender is in the allowed senders list
    local sender_address = from[1]['addr']
    for _, allowed_sender in ipairs(allowed_senders) do
        if allowed_sender == sender_address then
            rspamd_logger.info("Allowed sender: %s", sender_address)

            -- Register callback function to write log messages to a file
            local function write_log_messages(task)
                local log_json = ucl.to_format("ucl", task:to_message())
                -- Modify the file path as required for your setup
                local log_file = io.open("/var/log/rspamd/messages.log", "a")
                log_file:write(log_json)
                log_file:write("\n")
                log_file:close()
            end

            task:register_callback(write_log_messages)

            return true
        end
    end

    rspamd_logger.info("Sender not allowed: %s", sender_address)
    return false
end

The ideea of the module is that I have a list of allowed_users on disk which contains a few emails addresses. The module is supposed to check that list and if a sender is not found on the list, then it will log some messages using rspamd_logger.info. Same messages should be writeen on a file on disk in /var/log/rspamd/senders.log

Although everything seems to be working properly and emails are received, I do not see that file created nor any messages written on that file. I have checked the rspamd log and it says the module was loaded succesfully, also I have enabled debugging for this module but I see nothing in the logs which might be of use.

Any ideeas?

0

There are 0 best solutions below