rspamd_configThis module is used to configure rspamd and is normally available as global
variable named rspamd_config. Unlike other modules, it is not necessary to
require it before usage.
-- Register some callback symbol
local function foo(task)
-- do something
end
rspamd_config:register_symbol('SYMBOL', 1.0, foo)
-- Get configuration
local tab = rspamd_config:get_all_opt('module') -- get table for module's options
local opts = rspamd_config:get_key('options') -- get content of the specified key in rspamd configurationMethods:
rspamd_config:register_symbols(callback, [weight], callback_name, [, symbol, ...])
rspamd_config:register_callback_symbol(name, weight, callback)
rspamd_config:add_periodic(event_base, timeout, function(cfg, ev_base) ... end, [jitter = false])
rspamd_config:add_example(path, option, doc_string, example)
The module rspamd_config defines the following methods.
rspamd_config:get_module_opt(mname, optname)Returns value of specified option optname for a module mname,
Parameters:
mname {string}: name of moduleoptname {string}: option to getReturns:
{string or table}: value of the option or nil if option is not foundBack to module description.
rspamd_config:get_all_opt(mname)Returns value of all options for a module mname, flattening values into a single table consisting
of all sections with such a name.
Parameters:
mname {string}: name of moduleReturns:
{table}: table of all options for mname or nil if a module’s configuration is not foundBack to module description.
rspamd_config:get_ucl()Returns full configuration as a native Lua object (ucl to lua conversion). This method uses caching if possible.
Parameters:
No parameters
Returns:
{table}: table of all options in the configurationBack to module description.
rspamd_config:get_mempool()Returns static configuration memory pool.
Parameters:
No parameters
Returns:
{mempool}: memory pool objectBack to module description.
rspamd_config:get_resolver()Returns DNS resolver.
Parameters:
No parameters
Returns:
{dns_resolver}: opaque DNS resolver pointer if anyBack to module description.
rspamd_config:add_radix_map(mapline[, description])Creates new dynamic map of IP/mask addresses.
Parameters:
mapline {string}: URL for a mapdescription {string}: optional map descriptionReturns:
{map}: radix tree objectlocal ip_map = rspamd_config:add_radix_map ('file:///path/to/file', 'my radix map')
...
local function foo(task)
local ip = task:get_from_ip()
if ip_map:get_key(ip) then
return true
end
return false
endBack to module description.
rspamd_config:radix_from_config(mname, optname)Creates new embedded map of IP/mask addresses from config.
Parameters:
mname {string}: name of moduleoptname {string}: option to getReturns:
{map}: radix tree objectlocal ip_map = rspamd_config:radix_from_config ('mymodule', 'ips')
...
local function foo(task)
local ip = task:get_from_ip()
if ip_map:get_key(ip) then
return true
end
return false
endBack to module description.
rspamd_config:add_hash_map(mapline[, description])Creates new dynamic map string objects.
Parameters:
mapline {string}: URL for a mapdescription {string}: optional map descriptionReturns:
{map}: hash set objectlocal hash_map = rspamd_config:add_hash_map ('file:///path/to/file', 'my hash map')
...
local function foo(task)
local from = task:get_from()
if hash_map:get_key(from['user']) then
return true
end
return false
endBack to module description.
rspamd_config:add_kv_map(mapline[, description])Creates new dynamic map of key/values associations.
Parameters:
mapline {string}: URL for a mapdescription {string}: optional map descriptionReturns:
{map}: hash table objectlocal kv_map = rspamd_config:add_kv_map ('file:///path/to/file', 'my kv map')
...
local function foo(task)
local from = task:get_from()
if from then
local value = kv_map:get_key(from['user'])
if value then
return true,value
end
end
return false
endBack to module description.
rspamd_config:add_map({args})Creates new dynamic map according to the attributes passed.
type: type of map to be created, can be one of the following set:set: set of stringsradix: map of IP addresses to stringsmap: map of strings to stringsregexp: map of regexps to stringscallback: map processed by lua callbackurl: url to load map fromdescription: map’s descriptioncallback: lua callback for the mapParameters:
No parameters
Returns:
{map}: true if map has been addedlocal str = ''
local function process_map(in)
str = in
end
rspamd_config:add_map('http://example.com/map', "settings map", process_map)Back to module description.
rspamd_config:get_classifier(name)Returns classifier config.
Parameters:
name {string}: name of classifier (e.g. bayes)Returns:
{classifier}: classifier object or nilBack to module description.
rspamd_config:register_symbol(table)Register symbol of a specified type in rspamd. This function accepts table of arguments:
name: name of symbol (can be missing for callback symbols)callback: function to be called for symbol’s check (can be absent for virtual symbols)weight: weight of symbol (should normally be 1 or missing)priority: priority of symbol (normally 0 or missing)type: type of symbol: normal (default), virtual or callbackflags: various flags split by commas or spaces:nice if symbol can produce negative score;empty if symbol can be called for empty messagesskip if symbol should be skipped nownostat if symbol should be excluded from stat tokensparent: id of parent symbol (useful for virtual symbols)Parameters:
No parameters
Returns:
{number}: id of symbol registeredBack to module description.
rspamd_config:register_symbols(callback, [weight], callback_name, [, symbol, ...])Register callback function to be called for a set of symbols with initial weight.
Parameters:
callback {function}: callback function to be called for a specified symbolweight {number}: initial weight of symbol (can be less than zero to specify non-spam symbols)callback_name {string}: symbolic name of callbacksymbol {list of strings}: list of symbols registered by this functionReturns:
No return
Back to module description.
rspamd_config:register_virtual_symbol(name, weight,)Register virtual symbol that is not associated with any callback.
**This method is deprecated and should not be used in newly written code **
Parameters:
virtual {string}: name symbol’s nameweight {number}: initial weight of symbol (can be less than zero to specify non-spam symbols)Returns:
No return
Back to module description.
rspamd_config:register_callback_symbol(name, weight, callback)Register callback function to be called for a specified symbol with initial weight. Symbol itself is not registered in the metric and is not intended to be visible by a user.
**This method is deprecated and should not be used in newly written code **
Parameters:
name {string}: symbol’s name (just for unique id purposes)weight {number}: initial weight of symbol (can be less than zero to specify non-spam symbols)callback {function}: callback function to be called for a specified symbolReturns:
No return
Back to module description.
rspamd_config:register_dependency(id, dep)Create a dependency between symbol identified by id and a symbol identified
by some symbolic name dep
Parameters:
id {number|string}: id or name of source (numeric id is returned by all register_*_symbol)dep {string}: dependency nameReturns:
No return
local function cb(task)
...
end
local id = rspamd_config:register_symbol('SYM', 1.0, cb)
rspamd_config:register_dependency(id, 'OTHER_SYM')
-- Alternative form
rspamd_config:register_dependency('SYMBOL_FROM', 'SYMBOL_TO')Back to module description.
rspamd_config:register_pre_filter(callback[, order])Register function to be called prior to symbols processing.
Parameters:
callback {function}: callback functionorder {number}: filters are called from lower orders to higher orders, order is equal to 0 by defaultReturns:
No return
local function check_function(task)
-- It is possible to manipulate the task object here: set settings, set pre-action and so on
...
end
rspamd_config:register_pre_filter(check_function)Back to module description.
rspamd_config:register_post_filter(callback[, order])Register function to be called after symbols are processed.
Parameters:
callback {function}: callback functionorder {number}: filters are called from lower orders to higher orders, order is equal to 0 by defaultReturns:
No return
Back to module description.
rspamd_config:get_key(name)Returns configuration section with the specified name.
Parameters:
name {string}: name of config sectionReturns:
{variant}: specific value of sectionlocal set_section = rspamd_config:get_key("settings")
if type(set_section) == "string" then
-- Just a map of ucl
if rspamd_config:add_map(set_section, "settings map", process_settings_map) then
rspamd_config:register_pre_filter(check_settings)
end
elseif type(set_section) == "table" then
if process_settings_table(set_section) then
rspamd_config:register_pre_filter(check_settings)
end
endBack to module description.
rspamd_config:add_condition(symbol, condition)Adds condition callback for specified symbol
Parameters:
symbol {string}: symbol’s namecondition {function}: condition callbackReturns:
{boolean}: true if condition has been addedrspamd_config:add_condition('FUZZY_DENIED', function(task)
if some_map:find_key(task:get_from()) then return false end
return true
end)Back to module description.
rspamd_config:enable_symbol(symbol)Enables execution for the specified symbol
Parameters:
symbol {string}: symbol’s nameReturns:
No return
Back to module description.
rspamd_config:disable_symbol(symbol)Disables execution for the specified symbol
Parameters:
symbol {string}: symbol’s nameReturns:
No return
Back to module description.
rspamd_config:__newindex(name, callback)This metamethod is called if new indicies are added to the rspamd_config object.
Technically, it is the equivalent of rspamd_config:register_symbol where weight is 1.0.
There is also table form invocation that allows to control more things:
callback: has the same meaning and acts as function of taskscore: default score for a symbolgroup: default group for a symboldescription: default symbol’s descriptionpriority: additional priority valueone_shot: default value for one shot attributecondition: function of task that can enable or disable this specific rule’s executionParameters:
name {string}: index namecallback {function/table}: callback to be calledReturns:
{number}: id of the new symbol addedrspamd_config.R_EMPTY_IMAGE = function (task)
parts = task:get_text_parts()
if parts then
for _,part in ipairs(parts) do
if part:is_empty() then
images = task:get_images()
if images then
-- Symbol `R_EMPTY_IMAGE` is inserted
return true
end
return false
end
end
end
return false
end
rspamd_config.SYMBOL = {
callback = function(task)
...
end,
score = 5.1,
description = 'sample symbol',
group = 'sample symbols',
condition = function(task)
if task:get_from()[1]['addr'] == 'user@example.com' then
return false
end
return true
endBack to module description.
rspamd_config:register_regexp(params)Registers new re for further cached usage
Params is the table with the following fields (mandatory fields are marked with *):
re* : regular expression objecttype*: type of regular expression:mime: mime regexprawmime: raw mime regexpheader: header regexprawheader: raw header expressionbody: raw body regexpurl: url regexpheader: for header and rawheader regexp means the name of headerpcre_only: flag regexp as pcre only regexpParameters:
No parameters
Returns:
No return
Back to module description.
rspamd_config:replace_regexp(params)Replaces regexp with a new one
Params is the table with the following fields (mandatory fields are marked with *):
old_re* : old regular expression object (must be in the cache)new_re* : old regular expression object (must not be in the cache)Parameters:
No parameters
Returns:
No return
Back to module description.
rspamd_config:register_worker_script(worker_type, script)Registers the following script for workers of a specified type. The exact type of script function depends on worker type
Parameters:
worker_type {string}: worker type (e.g. “normal”)script {function}: script for a workerReturns:
{boolean}: true if a script has been registeredBack to module description.
rspamd_config:add_on_load(function(cfg, ev_base) ... end)Registers the following script to be executed when configuration is completely loaded
Parameters:
script {function}: function to be executedReturns:
No return
rspamd_config:add_on_load(function(cfg, ev_base)
rspamd_config:add_periodic(ev_base, 1.0, function(cfg, ev_base)
local logger = require "rspamd_logger"
logger.infox(cfg, "periodic function")
return true
end)
end)Back to module description.
rspamd_config:add_periodic(event_base, timeout, function(cfg, ev_base) ... end, [jitter = false])Registers function to be periodically executed by Rspamd
Parameters:
event_base {ev_base}: event base that is needed for async eventstimeout {number}: time in seconds (could be fractional)script {function}: function to be executedjitter {boolean}: true if timeout jittering is neededReturns:
No return
rspamd_config:add_on_load(function(cfg, ev_base)
rspamd_config:add_periodic(ev_base, 1.0, function(cfg, ev_base)
local logger = require "rspamd_logger"
logger.infox(cfg, "periodic function")
return true -- if return false, then the periodic event is removed
end)
end)Back to module description.
rspamd_config:get_symbols_count()Returns number of symbols registered in rspamd configuration
Parameters:
No parameters
Returns:
{number}: number of symbols registered in the configurationBack to module description.
rspamd_config:get_symbols_cksum()Returns checksum for all symbols in the cache
Parameters:
No parameters
Returns:
{int64}: boxed value of the 64 bit checksumBack to module description.
rspamd_config:get_symbol_callback(name)Returns callback function for the specified symbol if it is a lua registered callback
Parameters:
No parameters
Returns:
{function}: callback function or nilBack to module description.
rspamd_config:get_symbol_stat(name)Returns table with statistics for a specific symbol:
frequency: frequency for symbol’s hitsstddev: standard deviation of frequencytime: average time in seconds (floating point)count: total number of hitsParameters:
No parameters
Returns:
{table}: symbol statsBack to module description.
rspamd_config:set_symbol_callback(name, callback)Sets callback for the specified symbol
Parameters:
No parameters
Returns:
{boolean}: true if function has been replacedBack to module description.
rspamd_config:register_finish_script(callback)Adds new callback that is called on worker process termination when all tasks pending are processed
Parameters:
callback: {function} a function with one argument (rspamd_task)Returns:
No return
Back to module description.
rspamd_config:register_monitored(url, type, [{params}])Registers monitored resource to watch its availability. Supported types:
dns: DNS monitored objectParams are optional table specific for each type. For DNS it supports the following options:
prefix: prefix to add before making requesttype: type of request (e.g. ‘a’ or ‘txt’)ipnet: array of ip/networks to expect on replyrcode: expected return code (e.g. nxdomain)Returned object has the following methods:
alive: returns true if monitored resource is aliveoffline: returns number of seconds of the current offline period (or 0 if alive)total_offline: returns number of seconds of the overall offlinelatency: returns the current average latency in seconds (or 0 if offline)Parameters:
url {string}: resource to monitortype {string}: type of monitoringopts {table}: optional parametersReturns:
{rspamd_monitored}: rspamd monitored objectBack to module description.
rspamd_config:add_doc(path, option, doc_string, [{params}])Adds new documentation string for an option option at path path
Options defines optional params, such as:
default: default option valuetype: type of an option (string, number, object, array etc)reqired: if an option is requiredParameters:
path {string}: documentation path (e.g. module name)option {string}: name of the optiondoc_string {string}: documentation stringparams {table}: optional parametersReturns:
No return
Back to module description.
rspamd_config:add_example(path, option, doc_string, example)Adds new documentation
Parameters:
path {string}: documentation path (e.g. module name or nil for top)option {string}: name of the optiondoc_string {string}: documentation stringexample {string}: example in ucl format, comments are also parsedReturns:
No return
Back to module description.
rspamd_config:set_peak_cb(function)Sets a function that will be called when frequency of some symbol goes out of stddev * 2 over the last period of refreshment.
Parameters:
No parameters
Returns:
No return
rspamd_config:set_peak_cb(function(ev_base, sym, mean, stddev, value, error)
-- ev_base: event base for async events (e.g. redis)
-- sym: symbol's name
-- mean: mean frequency value
-- stddev: standard deviation of frequency
-- value: current frequency value
-- error: squared error
local logger = require "rspamd_logger"
logger.infox(rspamd_config, "symbol %s has changed frequency significantly: %s(%s) over %s(%s)",
sym, value, error, mean, stddev)
end)Back to module description.
rspamd_config:get_cpu_flags()Returns architecture dependent flags supported by the CPU Currently, only x86 flags are supported:
Parameters:
No parameters
Returns:
{table}: flag -> true tableBack to module description.
Back to top.