Module lua_util

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

Brief content:

Functions:

lua_util.str_split(text, deliminator): function.

lua_util.round(number, decimalPlaces): function.

lua_util.template(text, replacements): Replaces values in a text template.

lua_util.unpack(table): Converts numeric table to varargs.

lua_util.spairs(table): function.

lua_util.disable_module(modname, how): function.

lua_util.disable_module(modname): function.

lua_util.list_to_hash(list): function.

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): function.

lua_util.table_cmp(task, name, value, stop_chars): function.

lua_util.override_defaults(defaults, override): function.

lua_util.extract_specific_urls(params): params.

lua_util.deepcopy(table): params.

lua_util.shallowcopy(tbl): function.

lua_util.debugm(module, [log_object], format, ...): function.

Functions

The module lua_util defines the following functions.

Function lua_util.str_split(text, deliminator)

Splits text into a numeric table by deliminator

Parameters:

  • text {string}: deliminated text
  • deliminator {string}: the deliminator

Returns:

  • {table}: numeric table containing string parts

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.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.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, how)

Disables a plugin

Parameters:

  • modname {string}: name of plugin to disable
  • how {string}: ‘redis’ to disable redis, ‘config’ to disable startup

Returns:

No return

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.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_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.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) -- } 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.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.

Back to top.