Module lua_util

This module contains utility functions for working with Lua and/or Rspamd

Brief content:

Functions:

Function Description
lua_util.str_split(text, delimiter) Splits text into a numeric table by delimiter.
lua_util.str_trim(text) Returns a string with no trailing and leading spaces.
lua_util.str_startswith(text, prefix) No description
lua_util.str_endswith(text, suffix) No description
lua_util.round(number, decimalPlaces) Round number to fixed number of decimal points.
lua_util.template(text, replacements) Replaces values in a text template.
lua_util.jinja_template(text, env[, skip_global_env][, is_orig][, custom_filters]) Replaces values in a text template according to jinja2 syntax.
lua_util.jinja_file(filename, env[, skip_global_env][, is_orig][, custom_filters]) Replaces values in a text template according to jinja2 syntax.
lua_util.unpack(table) Converts numeric table to varargs.
lua_util.flatten(table) Flatten underlying tables in a single table.
lua_util.spairs(table) Like pairs but keys are sorted lexicographically.
lua_util.disable_module(modname) Checks experimental plugins state and disable if needed.
lua_util.list_to_hash(list) Converts numerically-indexed table to table indexed by values.
lua_util.nkeys(table|gen, param, state) Returns number of keys in a table (i.e.
lua_util.parse_time_interval(str) Parses human readable time interval.
lua_util.dehumanize_number(str) Parses human readable number.
lua_util.table_cmp(t1, t2) Compare two tables deeply.
lua_util.table_merge(t1, t2) Merge two tables.
lua_util.table_cmp(task, name, value, stop_chars) Performs header folding.
lua_util.override_defaults(defaults, override) Overrides values from defaults with override.
lua_util.filter_specific_urls(urls, params) params.
lua_util.extract_specific_urls(params) params.
lua_util.deepcopy(table) params.
lua_util.deepsort(table) params.
lua_util.shallowcopy(tbl) Performs shallow (and fast) copy of a table or another Lua type.
lua_util.debugm(module, [log_object], format, ...) Performs fast debug log for a specific module.
lua_util.add_debug_alias(mod, alias) Add debugging alias so logging to alias will be treated as logging to mod.

Functions

The module lua_util defines the following functions.

Function lua_util.str_split(text, delimiter)

Splits text into a numeric table by delimiter

Parameters:

  • text {string}: delimited text
  • delimiter {string}: the delimiter

Returns:

  • {table}: numeric table containing string parts

Back to module description.

Function lua_util.str_trim(text)

Returns a string with no trailing and leading spaces

Parameters:

  • text {string}: input text

Returns:

  • {string}: string with no trailing and leading spaces

Back to module description.

Function lua_util.str_startswith(text, prefix)

Parameters:

  • text {string}: no description
  • prefix {string}: no description

Returns:

  • {boolean}: true if text starts with the specified prefix, false otherwise

Back to module description.

Function lua_util.str_endswith(text, suffix)

Parameters:

  • text {string}: no description
  • suffix {string}: no description

Returns:

  • {boolean}: true if text ends with the specified suffix, false otherwise

Back to module description.

Function lua_util.round(number, decimalPlaces)

Round number to fixed number of decimal points

Parameters:

  • number {number}: number to round
  • decimalPlaces {number}: number of decimal points

Returns:

  • {number}: rounded number

Back to module description.

Function lua_util.template(text, replacements)

Replaces values in a text template Variable names can contain letters, numbers and underscores, are prefixed with $ and may or not use curly braces.

Parameters:

  • text {string}: text containing variables
  • replacements {table}: key/value pairs for replacements

Returns:

  • {string}: string containing replaced values

Example:

local goop = lua_util.template("HELLO $FOO ${BAR}!", {['FOO'] = 'LUA', ['BAR'] = 'WORLD'})
-- goop contains "HELLO LUA WORLD!"

Back to module description.

Function lua_util.jinja_template(text, env[, skip_global_env][, is_orig][, custom_filters])

Replaces values in a text template according to jinja2 syntax

Parameters:

  • text {string}: text containing variables
  • replacements {table}: key/value pairs for replacements
  • skip_global_env {boolean}: don’t export Rspamd superglobals
  • is_orig {boolean}: use the original lupa configuration with {{ for variables
  • custom_filters {table}: custom filters to use (or nil if not needed)

Returns:

  • {string}: string containing replaced values

Example:

lua_util.jinja_template("HELLO {=FOO=} {=BAR=}!", {['FOO'] = 'LUA', ['BAR'] = 'WORLD'})
"HELLO LUA WORLD!"

Back to module description.

Function lua_util.jinja_file(filename, env[, skip_global_env][, is_orig][, custom_filters])

Replaces values in a text template according to jinja2 syntax

Parameters:

  • filename {string}: name of file to expand
  • replacements {table}: key/value pairs for replacements
  • skip_global_env {boolean}: don’t export Rspamd superglobals
  • is_orig {boolean}: use the original lupa configuration with {{ for variables
  • custom_filters {table}: custom filters to use (or nil if not needed)

Returns:

  • {string}: string containing replaced values

Example:

lua_util.jinja_template("HELLO {=FOO=} {=BAR=}!", {['FOO'] = 'LUA', ['BAR'] = 'WORLD'})
"HELLO LUA WORLD!"

Back to module description.

Function lua_util.unpack(table)

Converts numeric table to varargs This is unpack on Lua 5.1/5.2/LuaJIT and table.unpack on Lua 5.3

Parameters:

  • table {table}: numerically indexed table to unpack

Returns:

  • {varargs}: unpacked table elements

Back to module description.

Function lua_util.flatten(table)

Flatten underlying tables in a single table

Parameters:

  • table {table}: table of tables

Returns:

  • {table}: flattened table

Back to module description.

Function lua_util.spairs(table)

Like pairs but keys are sorted lexicographically

Parameters:

  • table {table}: table containing key/value pairs

Returns:

  • {function}: generator function returning key/value pairs

Back to module description.

Function lua_util.disable_module(modname)

Checks experimental plugins state and disable if needed

Parameters:

  • modname {string}: name of plugin to check

Returns:

  • {boolean}: true if plugin should be enabled, false otherwise

Back to module description.

Function lua_util.list_to_hash(list)

Converts numerically-indexed table to table indexed by values

Parameters:

  • list {table}: numerically-indexed table or string, which is treated as a one-element list

Returns:

  • {table}: table indexed by values

Example:

local h = lua_util.list_to_hash({"a", "b"})
-- h contains {a = true, b = true}

Back to module description.

Function lua_util.nkeys(table|gen, param, state)

Returns number of keys in a table (i.e. from both the array and hash parts combined)

Parameters:

  • list {table}: numerically-indexed table or string, which is treated as a one-element list

Returns:

  • {number}: number of keys

Example:

print(lua_util.nkeys({}))  -- 0
print(lua_util.nkeys({ "a", nil, "b" }))  -- 2
print(lua_util.nkeys({ dog = 3, cat = 4, bird = nil }))  -- 2
print(lua_util.nkeys({ "a", dog = 3, cat = 4 }))  -- 3
--

Back to module description.

Function lua_util.parse_time_interval(str)

Parses human readable time interval Accepts ‘s’ for seconds, ‘m’ for minutes, ‘h’ for hours, ‘d’ for days, ‘w’ for weeks, ‘y’ for years

Parameters:

  • str {string}: input string

Returns:

  • {number|nil}: parsed interval as seconds (might be fractional)

Back to module description.

Function lua_util.dehumanize_number(str)

Parses human readable number Accepts ‘k’ for thousands, ‘m’ for millions, ‘g’ for billions, ‘b’ suffix for 1024 multiplier, e.g. 10mb equal to 10 * 1024 * 1024

Parameters:

  • str {string}: input string

Returns:

  • {number|nil}: parsed number

Back to module description.

Function lua_util.table_cmp(t1, t2)

Compare two tables deeply

Parameters:

No parameters

Returns:

No return

Back to module description.

Function lua_util.table_merge(t1, t2)

Merge two tables

Parameters:

No parameters

Returns:

No return

Back to module description.

Function lua_util.table_cmp(task, name, value, stop_chars)

Performs header folding

Parameters:

No parameters

Returns:

No return

Back to module description.

Function lua_util.override_defaults(defaults, override)

Overrides values from defaults with override

Parameters:

No parameters

Returns:

No return

Back to module description.

Function lua_util.filter_specific_urls(urls, params)

params: {

    • task - if needed to save in the cache
    • limit (default = 9999)
    • esld_limit (default = 9999) n domains per eSLD (effective second level domain) works only if number of unique eSLD less than `limit`
    • need_emails (default = false)
    • filter (default = nil)
    • prefix cache prefix (default = nil) } Apply heuristic in extracting of urls from `urls` table, this function tries its best to extract specific number of urls from a task based on their characteristics

Parameters:

No parameters

Returns:

No return

Back to module description.

Function lua_util.extract_specific_urls(params)

params: {

    • task
    • limit (default = 9999)
    • esld_limit (default = 9999) n domains per eSLD (effective second level domain) works only if number of unique eSLD less than `limit`
    • need_emails (default = false)
    • filter (default = nil)
    • prefix cache prefix (default = nil)
    • ignore_redirected (default = false)
    • need_images (default = false)
    • need_content (default = false) } Apply heuristic in extracting of urls from task, this function tries its best to extract specific number of urls from a task based on their characteristics

Parameters:

No parameters

Returns:

No return

Back to module description.

Function lua_util.deepcopy(table)

params: {

    • table } Performs deep copy of the table. Including metatables

Parameters:

No parameters

Returns:

No return

Back to module description.

Function lua_util.deepsort(table)

params: {

    • table } Performs recursive in-place sort of a table

Parameters:

No parameters

Returns:

No return

Back to module description.

Function lua_util.shallowcopy(tbl)

Performs shallow (and fast) copy of a table or another Lua type

Parameters:

No parameters

Returns:

No return

Back to module description.

Function lua_util.debugm(module, [log_object], format, ...)

Performs fast debug log for a specific module

Parameters:

No parameters

Returns:

No return

Back to module description.

Function lua_util.add_debug_alias(mod, alias)

Add debugging alias so logging to alias will be treated as logging to mod

Parameters:

No parameters

Returns:

No return

Back to module description.

Back to top.