commit 76cef50: [Rework] Allow to specify exat metric result when adding a symbol

Vsevolod Stakhov vsevolod at highsecure.ru
Wed May 27 16:21:25 UTC 2020


Author: Vsevolod Stakhov
Date: 2020-05-27 16:52:10 +0100
URL: https://github.com/rspamd/rspamd/commit/76cef50178e9dc66f18e2eeab0e6dcc7a65b2830

[Rework] Allow to specify exat metric result when adding a symbol

---
 src/libmime/scan_result.c | 84 ++++++++++++++++++++++++++++++-----------------
 src/libmime/scan_result.h |  7 ++--
 2 files changed, 57 insertions(+), 34 deletions(-)

diff --git a/src/libmime/scan_result.c b/src/libmime/scan_result.c
index fb8024928..d987cbff2 100644
--- a/src/libmime/scan_result.c
+++ b/src/libmime/scan_result.c
@@ -479,10 +479,11 @@ insert_metric_result (struct rspamd_task *task,
 
 struct rspamd_symbol_result *
 rspamd_task_insert_result_full (struct rspamd_task *task,
-		const gchar *symbol,
-		double weight,
-		const gchar *opt,
-		enum rspamd_symbol_insert_flags flags)
+								const gchar *symbol,
+								double weight,
+								const gchar *opt,
+								enum rspamd_symbol_insert_flags flags,
+								struct rspamd_scan_result *result)
 {
 	struct rspamd_symbol_result *s = NULL, *ret = NULL;
 	struct rspamd_scan_result *mres;
@@ -494,46 +495,67 @@ rspamd_task_insert_result_full (struct rspamd_task *task,
 		return NULL;
 	}
 
-	DL_FOREACH (task->result, mres) {
-		if (mres->symbol_cbref != -1) {
-			/* Check if we can insert this symbol to this symbol result */
-			GError *err = NULL;
-			lua_State *L = (lua_State *)task->cfg->lua_state;
+	if (result == NULL) {
+		/* Insert everywhere */
+		DL_FOREACH (task->result, mres) {
+			if (mres->symbol_cbref != -1) {
+				/* Check if we can insert this symbol to this symbol result */
+				GError *err = NULL;
+				lua_State *L = (lua_State *) task->cfg->lua_state;
+
+				if (!rspamd_lua_universal_pcall (L, mres->symbol_cbref,
+						G_STRLOC, 1, "uss", &err,
+						"rspamd{task}", task, symbol, mres->name ? mres->name : "default")) {
+					msg_warn_task ("cannot call for symbol_cbref for result %s: %e",
+							mres->name ? mres->name : "default", err);
+					g_error_free (err);
 
-			if (!rspamd_lua_universal_pcall (L, mres->symbol_cbref,
-					G_STRLOC, 1, "uss", &err,
-					"rspamd{task}", task, symbol, mres->name ? mres->name : "default")) {
-				msg_warn_task ("cannot call for symbol_cbref for result %s: %e",
-						mres->name ? mres->name : "default", err);
-				g_error_free (err);
+					continue;
+				}
+				else {
+					if (!lua_toboolean (L, -1)) {
+						/* Skip symbol */
+						msg_debug_metric ("skip symbol %s for result %s due to Lua return value",
+								symbol, mres->name);
+						lua_pop (L, 1); /* Remove result */
 
-				continue;
-			}
-			else {
-				if (!lua_toboolean (L, -1)) {
-					/* Skip symbol */
-					msg_debug_metric ("skip symbol %s for result %s due to Lua return value",
-							symbol, mres->name);
-					lua_pop (L, 1); /* Remove result */
+						continue;
+					}
 
-					continue;
+					lua_pop (L, 1); /* Remove result */
 				}
+			}
 
-				lua_pop (L, 1); /* Remove result */
+			s = insert_metric_result (task,
+					symbol,
+					weight,
+					opt,
+					mres,
+					flags);
+
+			if (mres->name == NULL) {
+				/* Default result */
+				ret = s;
+
+				/* Process cache item */
+				if (s && task->cfg->cache && s->sym) {
+					rspamd_symcache_inc_frequency (task->cfg->cache,
+							s->sym->cache_item);
+				}
 			}
 		}
-
+	}
+	else {
+		/* Specific insertion */
 		s = insert_metric_result (task,
 				symbol,
 				weight,
 				opt,
-				mres,
+				result,
 				flags);
+		ret = s;
 
-		if (mres->name == NULL) {
-			/* Default result */
-			ret = s;
-
+		if (result->name == NULL) {
 			/* Process cache item */
 			if (s && task->cfg->cache && s->sym) {
 				rspamd_symcache_inc_frequency (task->cfg->cache,
diff --git a/src/libmime/scan_result.h b/src/libmime/scan_result.h
index 3dee11ddd..2f20e2c62 100644
--- a/src/libmime/scan_result.h
+++ b/src/libmime/scan_result.h
@@ -141,12 +141,13 @@ struct rspamd_symbol_result *rspamd_task_insert_result_full (struct rspamd_task
 															 const gchar *symbol,
 															 double weight,
 															 const gchar *opts,
-															 enum rspamd_symbol_insert_flags flags);
+															 enum rspamd_symbol_insert_flags flags,
+															 struct rspamd_scan_result *result);
 
 #define rspamd_task_insert_result_single(task, symbol, flag, opts) \
-    rspamd_task_insert_result_full (task, symbol, flag, opts, RSPAMD_SYMBOL_INSERT_SINGLE)
+    rspamd_task_insert_result_full (task, symbol, flag, opts, RSPAMD_SYMBOL_INSERT_SINGLE, NULL)
 #define rspamd_task_insert_result(task, symbol, flag, opts) \
-    rspamd_task_insert_result_full (task, symbol, flag, opts, RSPAMD_SYMBOL_INSERT_DEFAULT)
+    rspamd_task_insert_result_full (task, symbol, flag, opts, RSPAMD_SYMBOL_INSERT_DEFAULT, NULL)
 
 
 /**


More information about the Commits mailing list