rspamd_taskThis module provides routines for tasks manipulation in rspamd. Tasks usually represent messages being scanned, and this API provides access to such elements as headers, symbols, metrics and so on and so forth. Normally, task objects are passed to the lua callbacks allowing to check specific properties of messages and add the corresponding symbols to the scan’s results.
rspamd_config.DATE_IN_PAST = function(task)
local dm = task:get_date{format = 'message', gmt = true}
local dt = task:get_date{format = 'connect', gmt = true}
-- A day
if dt - dm > 86400 then
return true
end
return false
endMethods:
The module rspamd_task defines the following methods.
task:get_cfg()Get configuration object for a task.
Parameters:
No parameters
Returns:
{rspamd_config}: (config.html)[configuration object] for the taskBack to module description.
task:get_mempool()Returns memory pool valid for a lifetime of task. It is used internally by many rspamd routines.
Parameters:
No parameters
Returns:
{rspamd_mempool}: memory pool objectBack to module description.
task:get_session()Returns asynchronous session object that is used by many rspamd asynchronous utilities internally.
Parameters:
No parameters
Returns:
{rspamd_session}: session objectBack to module description.
task:get_ev_base()Return asynchronous event base for using in callbacks and resolver.
Parameters:
No parameters
Returns:
{rspamd_ev_base}: event baseBack to module description.
task:get_worker()Returns a worker object associated with the task
Parameters:
No parameters
Returns:
{rspamd_worker}: worker objectBack to module description.
task:insert_result(symbol, weight[, option1, ...])Insert specific symbol to the tasks scanning results assigning the initial weight to it.
Parameters:
symbol {string}: symbol to insertweight {number}: initial weight (this weight is multiplied by the metric weight)options {string}: list of optional options attached to a symbol insertedReturns:
No return
local function cb(task)
if task:get_header('Some header') then
task:insert_result('SOME_HEADER', 1.0, 'Got some header')
end
endBack to module description.
task:set_pre_result(action, description)Sets pre-result for a task. It is used in pre-filters to specify early result
of the task scanned. If a pre-filter sets some result, then further processing
may be skipped. For selecting action it is possible to use global table
rspamd_actions or a string value:
reject: reject message permanentlyadd header: add spam headerrewrite subject: rewrite subject to spam subjectgreylist: greylist messageaccept or no action: whitelist messageParameters:
action {rspamd_action or string}: a numeric or string action valuedescription {string}: optional descriptionReturns:
No return
local function cb(task)
local gr = task:get_header('Greylist')
if gr and gr == 'greylist' then
task:set_pre_result(rspamd_actions['greylist'], 'Greylisting required')
end
endBack to module description.
task:append_message(message)Adds a message to scanning output.
Parameters:
message {string}:Returns:
No return
local function cb(task)
task:append_message('Example message')
endBack to module description.
task:get_urls([need_emails])Get all URLs found in a message.
Parameters:
need_emails {boolean}: if true then reutrn also email urlsReturns:
{table rspamd_url}: list of all urls foundlocal function phishing_cb(task)
local urls = task:get_urls();
if urls then
for _,url in ipairs(urls) do
if url:is_phished() then
return true
end
end
end
return false
endBack to module description.
task:has_urls([need_emails])Returns ‘true’ if a task has urls listed
Parameters:
need_emails {boolean}: if true then reutrn also email urlsReturns:
{boolean}: true if a task has urls (urls or emails if need_emails is true)Back to module description.
task:get_content()Get raw content for the specified task
Parameters:
No parameters
Returns:
{text}: the data contained in the taskBack to module description.
task:get_rawbody()Get raw body for the specified task
Parameters:
No parameters
Returns:
{text}: the data contained in the taskBack to module description.
task:get_emails()Get all email addresses found in a message.
Parameters:
No parameters
Returns:
{table rspamd_url}: list of all email addresses foundBack to module description.
task:get_text_parts()Get all text (and HTML) parts found in a message
Parameters:
No parameters
Returns:
{table rspamd_text_part}: list of text partsBack to module description.
task:get_parts()Get all mime parts found in a message
Parameters:
No parameters
Returns:
{table rspamd_mime_part}: list of mime partsBack to module description.
task:get_request_header(name)Get value of a HTTP request header.
Parameters:
name {string}: name of header to getReturns:
{rspamd_text}: value of an HTTP headerBack to module description.
task:set_request_header(name, value)Set value of a HTTP request header. If value is omitted, then a header is removed
Parameters:
name {string}: name of header to getvalue {rspamd_text/string}: new header’s valueReturns:
No return
Back to module description.
task:get_subject()Returns task subject (either from the protocol override or from a header)
Parameters:
No parameters
Returns:
{string}: value of a subject (decoded)Back to module description.
task:get_header(name[, case_sensitive])Get decoded value of a header specified with optional case_sensitive flag. By default headers are searched in caseless matter.
Parameters:
name {string}: name of header to getcase_sensitive {boolean}: case sensitiveness flag to search for a headerReturns:
{string}: decoded value of a headerBack to module description.
task:get_header_raw(name[, case_sensitive])Get raw value of a header specified with optional case_sensitive flag. By default headers are searched in caseless matter.
Parameters:
name {string}: name of header to getcase_sensitive {boolean}: case sensitiveness flag to search for a headerReturns:
{string}: raw value of a headerBack to module description.
task:get_header_full(name[, case_sensitive])Get raw value of a header specified with optional case_sensitive flag. By default headers are searched in caseless matter. This method returns more information about the header as a list of tables with the following structure:
name - name of a headervalue - raw value of a headerdecoded - decoded value of a headertab_separated - true if a header and a value are separated by tab characterempty_separator - true if there are no separator between a header and a valueParameters:
name {string}: name of header to getcase_sensitive {boolean}: case sensitiveness flag to search for a headerReturns:
{list of tables}: all values of a header as specified abovefunction check_header_delimiter_tab(task, header_name)
for _,rh in ipairs(task:get_header_full(header_name)) do
if rh['tab_separated'] then return true end
end
return false
endBack to module description.
task:get_raw_headers()Get all undecoded headers of a message as a string
Parameters:
No parameters
Returns:
{rspamd_text}: all raw headers for a message as opaque textBack to module description.
task:get_received_headers()Returns a list of tables of parsed received headers. A tables returned have the following structure:
from_hostname - string that represents hostname provided by a peerfrom_ip - string representation of IP address as provided by a peerreal_hostname - hostname as resolved by MTAreal_ip - string representation of IP as resolved by PTR request of MTAby_hostname - MTA hostnameproto - protocol, e.g. ESMTP or ESMTPStimestamp - received timestampfor - for value (unparsed mailbox)Please note that in some situations rspamd cannot parse all the fields of received headers. In that case you should check all strings for validity.
Parameters:
No parameters
Returns:
{table of tables}: list of received headers described aboveBack to module description.
task:get_queue_id()Returns queue ID of the message being processed.
Parameters:
No parameters
Returns:
No return
Back to module description.
task:get_uid()Returns ID of the task being processed.
Parameters:
No parameters
Returns:
No return
Back to module description.
task:get_resolver()Returns ready to use rspamd_resolver object suitable for making asynchronous DNS requests.
Parameters:
No parameters
Returns:
{rspamd_resolver}: resolver object associated with the task’s sessionlocal logger = require "rspamd_logger"
local function task_cb(task)
local function dns_cb(resolver, to_resolve, results, err)
-- task object is available due to closure
task:inc_dns_req()
if results then
logger.info(string.format('<%s> [%s] resolved for symbol: %s',
task:get_message_id(), to_resolve, 'EXAMPLE_SYMBOL'))
task:insert_result('EXAMPLE_SYMBOL', 1)
end
end
local r = task:get_resolver()
r:resolve_a(task:get_session(), task:get_mempool(), 'example.com', dns_cb)
endBack to module description.
task:inc_dns_req()Increment number of DNS requests for the task. Is used just for logging purposes.
Parameters:
No parameters
Returns:
No return
Back to module description.
task:get_dns_req()Get number of dns requests being sent in the task
Parameters:
No parameters
Returns:
{number}: number of DNS requestsBack to module description.
task:has_recipients([type])Return true if there are SMTP or MIME recipients for a task.
Parameters:
type {integer|string}: if specified has the following meaning: 0 or any means try SMTP recipients and fallback to MIME if failed, 1 or smtp means checking merely SMTP recipients and 2 or mime means MIME recipients onlyReturns:
{bool}: true if there are recipients of the following typeBack to module description.
task:get_recipients([type])Return SMTP or MIME recipients for a task. This function returns list of internet addresses each one is a table with the following structure:
name - name of internet address in UTF8, e.g. for Vsevolod Stakhov <blah@foo.com> it returns Vsevolod Stakhovaddr - address part of the addressuser - user part (if present) of the address, e.g. blahdomain - domain part (if present), e.g. foo.comParameters:
type {integer|string}: if specified has the following meaning: 0 or any means try SMTP recipients and fallback to MIME if failed, 1 or smtp means checking merely SMTP recipients and 2 or mime means MIME recipients onlyReturns:
{list of addresses}: list of recipients or nilBack to module description.
task:get_principal_recipient()Returns a single string with so called principal recipient for a message. The order
of check is the following:
Parameters:
No parameters
Returns:
{string}: principal recipientBack to module description.
task:set_recipients([type], {rcpt1, rcpt2...})Sets sender for a task. This function accepts table that will be converted to the address. If some fields are missing they are subsequently reconstructed by this function. E.g. if you specify ‘user’ and ‘domain’, then address and raw string will be reconstructed
name - name of internet address in UTF8, e.g. for Vsevolod Stakhov <blah@foo.com> it returns Vsevolod Stakhovaddr - address part of the addressuser - user part (if present) of the address, e.g. blahdomain - domain part (if present), e.g. foo.comParameters:
type {integer|string}: if specified has the following meaning: 0 or any means try SMTP recipients and fallback to MIME if failed, 1 or smtp means checking merely SMTP recipients and 2 or mime means MIME recipients onlyrecipients {list of tables}: recipients to setReturns:
{boolean}: result of the operationBack to module description.
task:has_from([type])Return true if there is SMTP or MIME sender for a task.
Parameters:
type {integer|string}: if specified has the following meaning: 0 or any means try SMTP recipients and fallback to MIME if failed, 1 or smtp means checking merely SMTP recipients and 2 or mime means MIME recipients onlyReturns:
{bool}: true if there is sender of the following typeBack to module description.
task:get_from([type])Return SMTP or MIME sender for a task. This function returns an internet address which one is a table with the following structure:
name - name of internet address in UTF8, e.g. for Vsevolod Stakhov <blah@foo.com> it returns Vsevolod Stakhovaddr - address part of the addressuser - user part (if present) of the address, e.g. blahdomain - domain part (if present), e.g. foo.comParameters:
type {integer|string}: if specified has the following meaning: 0 or any means try SMTP sender and fallback to MIME if failed, 1 or smtp means checking merely SMTP sender and 2 or mime means MIME From: onlyReturns:
{address}: sender or nilBack to module description.
task:set_from(type, addr)Sets sender for a task. This function accepts table that will be converted to the address. If some fields are missing they are subsequently reconstructed by this function. E.g. if you specify ‘user’ and ‘domain’, then address and raw string will be reconstructed
name - name of internet address in UTF8, e.g. for Vsevolod Stakhov <blah@foo.com> it returns Vsevolod Stakhovaddr - address part of the addressuser - user part (if present) of the address, e.g. blahdomain - domain part (if present), e.g. foo.comParameters:
type {integer|string}: if specified has the following meaning: 0 or any means try SMTP sender and fallback to MIME if failed, 1 or smtp means checking merely SMTP sender and 2 or mime means MIME From: only{table:Returns:
{boolean}: success or notBack to module description.
task:get_user()Returns authenticated user name for this task if specified by an MTA.
Parameters:
No parameters
Returns:
{string}: username or nilBack to module description.
task:get_from_ip()Returns ip_addr object of a sender that is provided by MTA
Parameters:
No parameters
Returns:
{rspamd_ip}: ip address objectBack to module description.
task:set_from_ip(str)Set tasks’s IP address based on the passed string
Parameters:
str {string}: string representation of ipReturns:
No return
Back to module description.
task:get_client_ip()Returns ip_addr object of a client connected to rspamd (normally, it is an IP address of MTA)
Parameters:
No parameters
Returns:
{rspamd_ip}: ip address objectBack to module description.
task:get_helo()Returns the value of SMTP helo provided by MTA.
Parameters:
No parameters
Returns:
{string}: HELO valueBack to module description.
task:get_hostname()Returns the value of sender’s hostname provided by MTA
Parameters:
No parameters
Returns:
{string}: hostname valueBack to module description.
task:get_images()Returns list of all images found in a task as a table of rspamd_image.
Each image has the following methods:
get_width - return width of an image in pixelsget_height - return height of an image in pixelsget_type - return string representation of image’s type (e.g. ‘jpeg’)get_filename - return string with image’s file nameget_size - return size in bytesParameters:
No parameters
Returns:
{list of rspamd_image}: images found in a messageBack to module description.
task:get_archives()Returns list of all archives found in a task as a table of rspamd_archive.
Each archive has the following methods available:
get_files - return list of strings with filenames inside archiveget_files_full - return list of tables with all information about filesis_encrypted - return true if an archive is encryptedget_type - return string representation of image’s type (e.g. ‘zip’)get_filename - return string with archive’s file nameget_size - return size in bytesParameters:
No parameters
Returns:
{list of rspamd_archive}: archives found in a messageBack to module description.
task:get_symbol(name)Searches for a symbol name in all metrics results and returns a list of tables
one per metric that describes the symbol inserted. Please note that this function
is intended to return values for inserted symbols, so if this symbol was not
inserted it won’t be in the function’s output. This method is useful for post-filters mainly.
The symbols are returned as the list of the following tables:
metric - name of metricscore - score of a symbol in that metricoptions - a table of strings representing options of a symbolgroup - a group of symbol (or ‘ungrouped’)Parameters:
name {string}: symbol’s nameReturns:
{list of tables}: list of tables or nil if symbol was not found in any metricBack to module description.
task:get_symbols_all()Returns array of symbols matched in default metric with all metadata
Parameters:
No parameters
Returns:
{table}: table of tables formatted as in task:get_symbol() except that metric is absent and name is addedBack to module description.
task:get_symbols()Returns array of all symbols matched for this task
Parameters:
No parameters
Returns:
{table, table}: table of strings with symbols names + table of theirs scoresBack to module description.
task:get_symbols_numeric()Returns array of all symbols matched for this task
Parameters:
No parameters
Returns:
{table|number, table|number}: table of numbers with symbols ids + table of theirs scoresBack to module description.
task:get_symbols_tokens()Returns array of all symbols as statistical tokens
Parameters:
No parameters
Returns:
{table|number}: table of numbersBack to module description.
task:has_symbol(name)Fast path to check if a specified symbol is in the task’s results
Parameters:
name {string}: symbol’s nameReturns:
{boolean}: true if symbol has been foundBack to module description.
task:get_date(type[, gmt])Returns timestamp for a connection or for a MIME message. This function can be called with a single table arguments with the following fields:
format - a format of date returned:message - returns a mime date as integer (unix timestamp)message_str - returns a mime date as string (UTC format)connect - returns a unix timestamp of a connection to rspamdconnect_str - returns connection time in UTC formatgmt - returns date in GMT timezone (normal for unix timestamps)By default this function returns connection time in numeric format.
Parameters:
type {string}: date format as described abovegmt {boolean}: gmt flag as described aboveReturns:
{string/number}: date representation according to formatrspamd_config.DATE_IN_PAST = function(task)
local dm = task:get_date{format = 'message', gmt = true}
local dt = task:get_date{format = 'connect', gmt = true}
-- A day
if dt - dm > 86400 then
return true
end
return false
endBack to module description.
task:get_message_id()Returns message id of the specified task
Parameters:
No parameters
Returns:
{string}: if of a messageBack to module description.
task:get_metric_score(name)Get the current score of metric name. Should be used in post-filters only.
Parameters:
name {string}: name of a metricReturns:
{table}: table containing the current score and required score of the metricBack to module description.
task:get_metric_action(name)Get the current action of metric name. Should be used in post-filters only.
Parameters:
name {string}: name of a metricReturns:
{string}: the current action of the metric as a stringBack to module description.
task:set_metric_score(name, score)Set the current score of metric name. Should be used in post-filters only.
Parameters:
name {string}: name of a metricscore {number}: the current score of the metricReturns:
No return
Back to module description.
task:set_metric_action(name, action)Set the current action of metric name. Should be used in post-filters only.
Parameters:
name {string}: name of a metricaction {string}: name to setReturns:
No return
Back to module description.
task:set_metric_subject(subject)Set the subject in the default metric
Parameters:
subject {string}: subject to setReturns:
No return
Back to module description.
task:learn(is_spam[, classifier)Learn classifier classifier with the task. If is_spam is true then message
is learnt as spam. Otherwise HAM is learnt. By default, this function learns
bayes classifier.
Parameters:
is_spam {boolean}: learn spam or hamclassifier {string}: classifier’s nameReturns:
{boolean}: true if classifier has been learnt successfullyBack to module description.
task:set_settings(obj)Set users settings object for a task. The format of this object is described here.
Parameters:
obj {any}: any lua object that corresponds to the settings formatReturns:
No return
Back to module description.
task:get_settings()Gets users settings object for a task. The format of this object is described here.
Parameters:
No parameters
Returns:
{lua object}: lua object generated from UCLBack to module description.
task:lookup_settings(key)Gets users settings object with the specified key for a task.
Parameters:
key {string}: key to lookupReturns:
{lua object}: lua object generated from UCLBack to module description.
task:get_settings_id()Get numeric hash of settings id if specified for this task. 0 is returned otherwise.
Parameters:
No parameters
Returns:
{number}: settings-id hashBack to module description.
task:set_milter_reply(obj)Set special reply for milter
Parameters:
obj {any}: any lua object that corresponds to the settings formatReturns:
No return
task:set_milter_reply({
add_headers = {['X-Lua'] = 'test'},
-- 1 is the position of header to remove
remove_headers = {['DKIM-Signature'] = 1},
})Back to module description.
task:process_re(params)Processes the specified regexp and returns number of captures (cached or new)
Params is the table with the following fields (mandatory fields are marked with *):
re* : regular expression objecttype*: type of regular expression:mime: mime regexpheader: header regexprawheader: raw header expressionrawmime: raw mime regexpbody: raw body regexpurl: url regexpheader: for header and rawheader regexp means the name of headerstrong: case sensitive match for headersParameters:
No parameters
Returns:
{number}: number of regexp occurrences in the task (limited by 255 so far)Back to module description.
task:cache_set(key, value)Store some value to the task cache
Parameters:
key {string}: key to usevalue {any}: any value (including functions and tables)Returns:
No return
Back to module description.
task:cache_get(key)Returns cached value or nil if nothing is cached
Parameters:
key {string}: key to useReturns:
{any}: cached valueBack to module description.
task:get_size()Returns size of the task in bytes (that includes headers + parts size)
Parameters:
No parameters
Returns:
{number}: size in bytesBack to module description.
task:set_flag(flag_name[, set])Set specific flag for task:
no_log: do not log task summaryno_stat: do not include task into scanned statspass_all: check all filters for taskextended_urls: output extended info about urlsskip: skip task processinglearn_spam: learn message as spamlearn_ham: learn message as hambroken_headers: header data is broken for a messageParameters:
flag {string}: to setset {boolean}: set or clear flag (default is set)Returns:
No return
--[[
For messages with undefined queue ID (scanned with rspamc or WebUI)
do not include results into statistics and do not log task summary
(it will not appear in the WebUI history as well).
]]--
-- Callback function to set flags
local function no_log_stat_cb(task)
if not task:get_queue_id() then
task:set_flag('no_log')
task:set_flag('no_stat')
end
end
rspamd_config:register_symbol({
name = 'LOCAL_NO_LOG_STAT',
type = 'postfilter',
callback = no_log_stat_cb
})Back to module description.
task:has_flag(flag_name)Checks for a specific flag in task:
no_log: do not log task summaryno_stat: do not include task into scanned statspass_all: check all filters for taskextended_urls: output extended info about urlsskip: skip task processinglearn_spam: learn message as spamlearn_ham: learn message as hambroken_headers: header data is broken for a messageParameters:
flag {string}: to checkReturns:
{boolean}: true if flags is setBack to module description.
task:get_flags()Get list of flags for task:
no_log: do not log task summaryno_stat: do not include task into scanned statspass_all: check all filters for taskextended_urls: output extended info about urlsskip: skip task processinglearn_spam: learn message as spamlearn_ham: learn message as hambroken_headers: header data is broken for a messagemilter: task is initiated by milter connectionParameters:
No parameters
Returns:
{array of strings}: table with all flags as stringsBack to module description.
task:get_digest()Returns message’s unique digest (32 hex symbols)
Parameters:
No parameters
Returns:
{string}: hex digestBack to module description.
task:store_in_file([mode])If task was loaded using file scan, then this method just returns its name,
otherwise, a fresh temporary file is created and its name is returned. Default
mode is 0600. To convert lua number to the octal mode you can use the following
trick: tonumber("0644", 8). The file is automatically removed when task is
destroyed.
Parameters:
mode {number}: mode for new fileReturns:
{string}: file name with task contentBack to module description.
task:get_protocol_reply([flags])This method being called from a postfilter will return reply for a message as it is returned to a client. This method returns the Lua table corresponding to the UCL object. Flags is a table that specify which information should be there in a reply:
basic: basic info, such as message-idmetrics: metrics and symbolsmessages: messagesdkim: dkim signaturemilter: milter control blockextra: extra data, such as profilingurls: list of all urls in a messageParameters:
flags {table}: table of flags (default is all flags but urls)Returns:
{table}: ucl object corresponding to the replyBack to module description.
task:headers_foreach(callback, [params])This method calls callback for each header that satisfies some condition.
By default, all headers are iterated unless callback returns true. Nil or
false means continue of iterations.
Params could be as following:
full: header value is full table of all attributes task:get_header_full for detailsregexp: return headers that satisfies the specified regexpParameters:
callback {function}: function from header name and header valueparams {table}: optional parametersReturns:
No return
Back to module description.
Back to top.