commit 2bce143: [Minor] Lua_config: Add routine to extract the specific symbol's configuration

Vsevolod Stakhov vsevolod at rspamd.com
Wed May 18 19:49:03 UTC 2022


Author: Vsevolod Stakhov
Date: 2022-05-18 20:23:15 +0100
URL: https://github.com/rspamd/rspamd/commit/2bce14398326263aeb4183b33d565c672f9c00d7

[Minor] Lua_config: Add routine to extract the specific symbol's configuration

---
 src/lua/lua_config.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)

diff --git a/src/lua/lua_config.c b/src/lua/lua_config.c
index aee5e25ae..1a98f0ea6 100644
--- a/src/lua/lua_config.c
+++ b/src/lua/lua_config.c
@@ -665,6 +665,20 @@ LUA_FUNCTION_DEF (config, get_symbols_counters);
  */
 LUA_FUNCTION_DEF (config, get_symbols);
 
+/***
+ * @method rspamd_config:get_symbol(sym_name)
+ * Returns table for a specific symbol getting data from the static config:
+ * - name
+ * - score
+ * - flags (e.g. `ignore` or `oneparam`)
+ * - nshots (== maxhits)
+ * - group - main group
+ * - groups - array of all groups
+ * @available 3.3+
+ * @return {table} symbol data (or nil)
+ */
+LUA_FUNCTION_DEF (config, get_symbol);
+
 /***
  * @method rspamd_config:get_symbol_callback(name)
  * Returns callback function for the specified symbol if it is a lua registered callback
@@ -900,6 +914,7 @@ static const struct luaL_reg configlib_m[] = {
 	LUA_INTERFACE_DEF (config, get_symbols_counters),
 	{"get_symbols_scores", lua_config_get_symbols},
 	LUA_INTERFACE_DEF (config, get_symbols),
+	LUA_INTERFACE_DEF (config, get_symbol),
 	LUA_INTERFACE_DEF (config, get_groups),
 	LUA_INTERFACE_DEF (config, get_symbol_callback),
 	LUA_INTERFACE_DEF (config, set_symbol_callback),
@@ -3638,6 +3653,33 @@ lua_config_get_symbols (lua_State *L)
 	return 1;
 }
 
+static gint
+lua_config_get_symbol (lua_State *L)
+{
+	LUA_TRACE_POINT;
+	struct rspamd_config *cfg = lua_check_config (L, 1);
+	const gchar *sym_name = luaL_checkstring(L, 2);
+
+	if (cfg != NULL && sym_name != NULL) {
+		struct lua_metric_symbols_cbdata cbd;
+		struct rspamd_symbol *s = g_hash_table_lookup(cfg->symbols, sym_name);
+
+		if (s) {
+			cbd.L = L;
+			cbd.cfg = cfg;
+			lua_metric_symbol_inserter((void *)sym_name, s, &cbd);
+		}
+		else {
+			/* No config for a symbol */
+			lua_pushnil(L);
+		}
+	}
+	else {
+		return luaL_error (L, "invalid arguments");
+	}
+
+	return 1;
+}
 
 static gint
 lua_config_get_symbol_callback (lua_State *L)


More information about the Commits mailing list