commit 876a816: [Feature] Fuzzy_check: Add weight_threshold option for fuzzy rules

Vsevolod Stakhov vsevolod at highsecure.ru
Tue Sep 22 12:07:09 UTC 2020


Author: Vsevolod Stakhov
Date: 2020-09-22 13:00:24 +0100
URL: https://github.com/rspamd/rspamd/commit/876a816414bc2333c077ed8bc55cc1261b4d093a (HEAD -> master)

[Feature] Fuzzy_check: Add weight_threshold option for fuzzy rules

---
 src/libmime/scan_result.h |  8 ++++----
 src/plugins/fuzzy_check.c | 31 +++++++++++++++++++++++++++----
 2 files changed, 31 insertions(+), 8 deletions(-)

diff --git a/src/libmime/scan_result.h b/src/libmime/scan_result.h
index 4d55a8e62..edba4d491 100644
--- a/src/libmime/scan_result.h
+++ b/src/libmime/scan_result.h
@@ -144,10 +144,10 @@ struct rspamd_symbol_result *rspamd_task_insert_result_full (struct rspamd_task
 															 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, NULL)
-#define rspamd_task_insert_result(task, symbol, flag, opts) \
-    rspamd_task_insert_result_full (task, symbol, flag, opts, RSPAMD_SYMBOL_INSERT_DEFAULT, NULL)
+#define rspamd_task_insert_result_single(task, symbol, weight, opts) \
+    rspamd_task_insert_result_full ((task), (symbol), (weight), (opts), RSPAMD_SYMBOL_INSERT_SINGLE, NULL)
+#define rspamd_task_insert_result(task, symbol, weight, opts) \
+    rspamd_task_insert_result_full ((task), (symbol), (weight), (opts), RSPAMD_SYMBOL_INSERT_DEFAULT, NULL)
 
 /**
  * Removes a symbol from a specific symbol result
diff --git a/src/plugins/fuzzy_check.c b/src/plugins/fuzzy_check.c
index 5c9ef7fe7..810abf587 100644
--- a/src/plugins/fuzzy_check.c
+++ b/src/plugins/fuzzy_check.c
@@ -82,6 +82,7 @@ struct fuzzy_rule {
 	struct rspamd_cryptobox_keypair *local_key;
 	struct rspamd_cryptobox_pubkey *peer_key;
 	double max_score;
+	double weight_threshold;
 	gboolean read_only;
 	gboolean skip_unknown;
 	gboolean no_share;
@@ -312,6 +313,7 @@ fuzzy_rule_new (const char *default_symbol, rspamd_mempool_t *pool)
 		(rspamd_mempool_destruct_t)g_hash_table_unref,
 		rule->mappings);
 	rule->read_only = FALSE;
+	rule->weight_threshold = NAN;
 
 	return rule;
 }
@@ -596,6 +598,10 @@ fuzzy_parse_rule (struct rspamd_config *cfg, const ucl_object_t *obj,
 				rule->algorithm_str);
 	}
 
+	if ((value = ucl_object_lookup (obj,  "weight_threshold")) != NULL) {
+		rule->weight_threshold = ucl_object_todouble (value);
+	}
+
 	/*
 	 * Process rule in Lua
 	 */
@@ -2362,7 +2368,8 @@ fuzzy_check_try_read (struct fuzzy_client_session *session)
 }
 
 static void
-fuzzy_insert_metric_results (struct rspamd_task *task, GPtrArray *results)
+fuzzy_insert_metric_results (struct rspamd_task *task, struct fuzzy_rule *rule,
+		GPtrArray *results)
 {
 	struct fuzzy_client_result *res;
 	guint i;
@@ -2439,8 +2446,22 @@ fuzzy_insert_metric_results (struct rspamd_task *task, GPtrArray *results)
 			}
 		}
 
-		rspamd_task_insert_result_single (task, res->symbol,
-				res->score * mult, res->option);
+		gdouble weight = res->score * mult;
+
+		if (!isnan (rule->weight_threshold)) {
+			if (weight >= rule->weight_threshold) {
+				rspamd_task_insert_result_single (task, res->symbol,
+						weight, res->option);
+			}
+			else {
+				msg_info_task ("%s is not added: weight=%.4f below threshold",
+						res->symbol, weight);
+			}
+		}
+		else {
+			rspamd_task_insert_result_single (task, res->symbol,
+					weight, res->option);
+		}
 	}
 }
 
@@ -2461,10 +2482,12 @@ fuzzy_check_session_is_completed (struct fuzzy_client_session *session)
 	}
 
 	if (nreplied == session->commands->len) {
-		fuzzy_insert_metric_results (session->task, session->results);
+		fuzzy_insert_metric_results (session->task, session->rule, session->results);
+
 		if (session->item) {
 			rspamd_symcache_item_async_dec_check (session->task, session->item, M);
 		}
+
 		rspamd_session_remove_event (session->task->s, fuzzy_io_fin, session);
 
 		return TRUE;


More information about the Commits mailing list