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:

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

Methods:

rspamd_expression:to_string()

rspamd_expression:process(input)

rspamd_expression:process_traced(input)

rspamd_expression:atoms()

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 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.join('', take_while(function(s) return s ~= ' ' end, str))
	-- Return token name
	return token
end

local function process_func(token, task)
	-- 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(input)

Executes the expression and pass input to process atom callbacks

Parameters:

  • input {any}: input data for processing callbacks

Returns:

  • {number}: result of the expression evaluation

Back to module description.

Method rspamd_expression:process_traced(input)

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

Parameters:

  • 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.