commit 92563db: [Project] Start results chain implementation

Vsevolod Stakhov vsevolod at highsecure.ru
Fri Apr 3 10:21:06 UTC 2020


Author: Vsevolod Stakhov
Date: 2020-04-03 11:13:56 +0100
URL: https://github.com/rspamd/rspamd/commit/92563dbf0220d91dc4be0a418fb62ccc220dd447 (HEAD -> master)

[Project] Start results chain implementation

---
 src/libmime/scan_result.c | 17 ++++++++++-------
 src/libmime/scan_result.h | 13 +++++++++----
 src/libserver/task.c      |  3 ++-
 3 files changed, 21 insertions(+), 12 deletions(-)

diff --git a/src/libmime/scan_result.c b/src/libmime/scan_result.c
index 8242c7de2..a8fa38450 100644
--- a/src/libmime/scan_result.c
+++ b/src/libmime/scan_result.c
@@ -43,6 +43,10 @@ rspamd_scan_result_dtor (gpointer d)
 
 	rspamd_set_counter_ema (&symbols_count, kh_size (r->symbols), 0.5);
 
+	if (r->symbol_cbref != -1) {
+		luaL_unref (r->task->cfg->lua_state, LUA_REGISTRYINDEX, r->symbol_cbref);
+	}
+
 	kh_foreach_value (r->symbols, sres, {
 		if (sres.options) {
 			kh_destroy (rspamd_options_hash, sres.options);
@@ -53,21 +57,19 @@ rspamd_scan_result_dtor (gpointer d)
 }
 
 struct rspamd_scan_result *
-rspamd_create_metric_result (struct rspamd_task *task)
+rspamd_create_metric_result (struct rspamd_task *task,
+							 const gchar *name, gint lua_sym_cbref)
 {
 	struct rspamd_scan_result *metric_res;
 	guint i;
 
-	metric_res = task->result;
-
-	if (metric_res != NULL) {
-		return metric_res;
-	}
-
 	metric_res = rspamd_mempool_alloc0 (task->task_pool,
 			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;
+	metric_res->symbol_cbref = lua_sym_cbref;
+	metric_res->task = task;
 
 	/* Optimize allocation */
 	kh_resize (rspamd_symbols_group_hash, metric_res->sym_groups, 4);
@@ -101,6 +103,7 @@ rspamd_create_metric_result (struct rspamd_task *task)
 	rspamd_mempool_add_destructor (task->task_pool,
 			rspamd_scan_result_dtor,
 			metric_res);
+	DL_APPEND (task->result, metric_res);
 
 	return metric_res;
 }
diff --git a/src/libmime/scan_result.h b/src/libmime/scan_result.h
index 3f9a6efe8..cdd6fe38b 100644
--- a/src/libmime/scan_result.h
+++ b/src/libmime/scan_result.h
@@ -40,7 +40,7 @@ struct rspamd_symbol_result {
 	struct rspamd_symbol_option *opts_head;        /**< head of linked list of options			*/
 	const gchar *name;
 	struct rspamd_symbol *sym;                     /**< symbol configuration					*/
-	gssize opts_len; /**< total size of all options (negative if truncated option is added) */
+	gssize opts_len;                               /**< total size of all options (negative if truncated option is added) */
 	guint nshots;
 	enum rspamd_symbol_result_flags flags;
 };
@@ -77,14 +77,18 @@ struct rspamd_scan_result {
 	double score;                                    /**< total score							*/
 	double grow_factor;                                /**< current grow factor					*/
 	struct rspamd_passthrough_result *passthrough_result;
-	guint npositive;
-	guint nnegative;
 	double positive_score;
 	double negative_score;
 	struct kh_rspamd_symbols_hash_s *symbols;            /**< symbols of metric						*/
 	struct kh_rspamd_symbols_group_hash_s *sym_groups; /**< groups of symbols						*/
 	struct rspamd_action_result *actions_limits;
+	const gchar *name;                                 /**< for named results, NULL is the default result */
+	struct rspamd_task *task;                          /**< back reference */
+	gint symbol_cbref;                                 /**< lua function that defines if a symbol can be inserted, -1 if unused */
 	guint nactions;
+	guint npositive;
+	guint nnegative;
+	struct rspamd_scan_result *prev, *next;           /**< double linked list of results */
 };
 
 /**
@@ -92,7 +96,8 @@ struct rspamd_scan_result {
  * @param task task object
  * @return metric result or NULL if metric `name` has not been found
  */
-struct rspamd_scan_result *rspamd_create_metric_result (struct rspamd_task *task);
+struct rspamd_scan_result *rspamd_create_metric_result (struct rspamd_task *task,
+		const gchar *name, gint lua_sym_cbref);
 
 /**
  * Adds a new passthrough result to a task
diff --git a/src/libserver/task.c b/src/libserver/task.c
index 3e8dd381f..a0d7bb12a 100644
--- a/src/libserver/task.c
+++ b/src/libserver/task.c
@@ -111,7 +111,8 @@ rspamd_task_new (struct rspamd_worker *worker,
 	new_task->request_headers = kh_init (rspamd_req_headers_hash);
 	new_task->sock = -1;
 	new_task->flags |= (RSPAMD_TASK_FLAG_MIME);
-	new_task->result = rspamd_create_metric_result (new_task);
+	/* Default results chain */
+	rspamd_create_metric_result (new_task, NULL, -1);
 
 	new_task->queue_id = "undef";
 	new_task->messages = ucl_object_typed_new (UCL_OBJECT);


More information about the Commits mailing list