Module rspamd_expression

This module can be used to implement different logic expressions in lua using rspamd AST optimizer. There are some examples in individual methods definitions to help understanding of this module.

Brief content:

Functions:

Function Description
rspamd_expression.create(line, {parse_func, [process_func]}, pool) Create expression from the line using atom parsing routines and the specified memory pool.

Methods:

Method Description
rspamd_expression:to_string() Converts rspamd expression to string.
rspamd_expression:process([callback, ]input[, flags]) Executes the expression and pass input to process atom callbacks.
rspamd_expression:process_traced([callback, ]input[, flags]) Executes the expression and pass input to process atom callbacks.
rspamd_expression:atoms() Extract all atoms from the expression as table of strings.

Functions

The module rspamd_expression defines the following functions.

Function rspamd_expression.create(line, {parse_func, [process_func]}, pool)

Create expression from the line using atom parsing routines and the specified memory pool

Parameters:

  • line {string}: expression line
  • atom_functions {table}: parse_atom function and optional process_atom function
  • memory {rspamd_mempool}: pool to use for this function

Returns:

  • {expr, err}: expression object and error message of expr is nil

Example:

require "fun" ()
local rspamd_expression = require "rspamd_expression"
local rspamd_mempool = require "rspamd_mempool"

local function parse_func(str)
	-- extract token till the first space character
	local token = table.concat(totable(take_while(function(s) return s ~= ' ' end, iter(str))))
	-- Return token name
	return token
end

local function process_func(token)
	-- Do something using token and task
end

local pool = rspamd_mempool.create()
local expr,err = rspamd_expression.create('A & B | !C', {parse_func, process_func}, pool)
-- Expression is destroyed when the corresponding pool is destroyed
pool:destroy()

Back to module description.

Methods

The module rspamd_expression defines the following methods.

Method rspamd_expression:to_string()

Converts rspamd expression to string

Parameters:

No parameters

Returns:

  • {string}: string representation of rspamd expression

Back to module description.

Method rspamd_expression:process([callback, ]input[, flags])

Executes the expression and pass input to process atom callbacks

Parameters:

  • callback {function}: if not specified on process, then callback must be here
  • input {any}: input data for processing callbacks

Returns:

  • {number}: result of the expression evaluation

Back to module description.

Method rspamd_expression:process_traced([callback, ]input[, flags])

Executes the expression and pass input to process atom callbacks. This function also saves the full trace

Parameters:

  • callback {function}: if not specified on process, then callback must be here
  • input {any}: input data for processing callbacks

Returns:

  • {number, table of matched atoms}: result of the expression evaluation

Back to module description.

Method rspamd_expression:atoms()

Extract all atoms from the expression as table of strings

Parameters:

No parameters

Returns:

  • {table/strings}: list of all atoms in the expression

Back to module description.

Back to top.