commit d916f02: [Minor] Allow to add named results via lua API

Vsevolod Stakhov vsevolod at highsecure.ru
Mon Apr 6 17:28:10 UTC 2020


Author: Vsevolod Stakhov
Date: 2020-04-06 17:09:18 +0100
URL: https://github.com/rspamd/rspamd/commit/d916f02050c63045ec7eb6e238dd281337e804c5 (HEAD -> master)

[Minor] Allow to add named results via lua API

---
 src/libmime/scan_result.c |  9 ++++++++-
 src/lua/lua_task.c        | 42 +++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 49 insertions(+), 2 deletions(-)

diff --git a/src/libmime/scan_result.c b/src/libmime/scan_result.c
index 44c481e67..a27c3b0b2 100644
--- a/src/libmime/scan_result.c
+++ b/src/libmime/scan_result.c
@@ -67,7 +67,14 @@ rspamd_create_metric_result (struct rspamd_task *task,
 			sizeof (struct rspamd_scan_result));
 	metric_res->symbols = kh_init (rspamd_symbols_hash);
 	metric_res->sym_groups = kh_init (rspamd_symbols_group_hash);
-	metric_res->name = name;
+
+	if (name) {
+		metric_res->name = rspamd_mempool_strdup (task->task_pool, name);
+	}
+	else {
+		metric_res->name = NULL;
+	}
+
 	metric_res->symbol_cbref = lua_sym_cbref;
 	metric_res->task = task;
 
diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c
index 4a6040145..ce15813ac 100644
--- a/src/lua/lua_task.c
+++ b/src/lua/lua_task.c
@@ -1080,10 +1080,30 @@ LUA_FUNCTION_DEF (task, lookup_words);
 /**
  * @method task:topointer()
  *
- * Returns raw C pointer (lightuserdata) associated with task
+ * Returns raw C pointer (lightuserdata) associated with task. This might be
+ * broken with luajit and GC64, use with caution.
  */
 LUA_FUNCTION_DEF (task, topointer);
 
+/**
+ * @method task:add_named_result(name, symbol_control_function)
+ *
+ * Adds a new named result for this task. symbol_control_function is a callback
+ * called with 3 parameters:
+ * `function(task, symbol, result_name)` and it should return boolean that
+ * specifies if this symbol should be added to this named result.
+ * @param {string} name for this result
+ * @param {function} symbol_control_function predicate for symbols
+ */
+LUA_FUNCTION_DEF (task, add_named_result);
+
+/***
+ * @method task:get_dns_req()
+ * Get number of dns requests being sent in the task
+ * @return {number} number of DNS requests
+ */
+LUA_FUNCTION_DEF (task, get_dns_req);
+
 static const struct luaL_reg tasklib_f[] = {
 	LUA_INTERFACE_DEF (task, create),
 	LUA_INTERFACE_DEF (task, load_from_file),
@@ -6509,6 +6529,26 @@ lua_task_topointer (lua_State *L)
 	return 1;
 }
 
+static gint
+lua_task_add_named_result (lua_State *L)
+{
+	LUA_TRACE_POINT;
+	struct rspamd_task *task = lua_check_task (L, 1);
+	const gchar *name = luaL_checkstring (L, 2);
+	gint cbref;
+
+	if (task && name && lua_isfunction (L, 3)) {
+		lua_pushvalue (L, 3);
+		cbref = luaL_ref (L, LUA_REGISTRYINDEX);
+		rspamd_create_metric_result (task, name, cbref);
+	}
+	else {
+		return luaL_error (L, "invalid arguments");
+	}
+
+	return 0;
+}
+
 
 /* Image functions */
 static gint


More information about the Commits mailing list