commit 54e1991: [Minor] Selectors: Add extractor & transform for metatokens

Andrew Lewis nerf at judo.za.org
Wed Nov 11 10:49:06 UTC 2020


Author: Andrew Lewis
Date: 2020-11-11 09:47:02 +0200
URL: https://github.com/rspamd/rspamd/commit/54e19913f1febb1cf5824e6c7f2cb277524e7c08 (refs/pull/3548/head)

[Minor] Selectors: Add extractor & transform for metatokens

---
 lualib/lua_selectors/extractors.lua | 16 ++++++++++++++++
 lualib/lua_selectors/transforms.lua | 17 +++++++++++++++++
 2 files changed, 33 insertions(+)

diff --git a/lualib/lua_selectors/extractors.lua b/lualib/lua_selectors/extractors.lua
index acb13deaa..162f01808 100644
--- a/lualib/lua_selectors/extractors.lua
+++ b/lualib/lua_selectors/extractors.lua
@@ -15,6 +15,7 @@ limitations under the License.
 ]]--
 
 local fun = require 'fun'
+local meta_functions = require "lua_meta"
 local lua_util = require "lua_util"
 local common = require "lua_selectors/common"
 local ts = require("tableshape").types
@@ -438,6 +439,21 @@ The first argument must be header name.]],
         'Returns the result table. See task:get_metric_result()',
     ['args_schema'] = {ts.string:is_optional()}
   },
+  -- Get list of metatokens as strings
+  ['metatokens'] = {
+    ['get_value'] = function(task)
+      local tokens = meta_functions.gen_metatokens(task)
+      if not tokens[1] then
+        return nil
+      end
+      local res = {}
+      for _, t in ipairs(tokens) do
+        table.insert(res, tostring(t))
+      end
+      return res, 'string_list'
+    end,
+    ['description'] = 'Get metatokens for a message as strings',
+  },
 }
 
 return extractors
diff --git a/lualib/lua_selectors/transforms.lua b/lualib/lua_selectors/transforms.lua
index 6251180ec..1a27b775d 100644
--- a/lualib/lua_selectors/transforms.lua
+++ b/lualib/lua_selectors/transforms.lua
@@ -441,6 +441,23 @@ Empty string comes the first argument or 'true', non-empty string comes nil]],
     ['description'] = 'Extracts tld from a hostname represented as a string',
     ['args_schema'] = {}
   },
+  -- Converts list of strings to numbers and returns a packed string
+  ['pack_numbers'] = {
+    ['types'] = {
+      ['string_list'] = true
+    },
+    ['map_type'] = 'string',
+    ['process'] = function(inp, _, args)
+      local fmt = args[1] or 'f'
+      local res = {}
+      for _, s in ipairs(inp) do
+        table.insert(res, tonumber(s))
+      end
+      return rspamd_util.pack(string.rep(fmt, #res), lua_util.unpack(res)), 'string'
+    end,
+    ['description'] = 'Converts a list of strings to numbers & returns a packed string',
+    ['args_schema'] = {ts.string:is_optional()}
+  },
 }
 
 transform_function.match = transform_function.regexp


More information about the Commits mailing list