commit c5e6bbf: [Minor] Add method to remove symbol from a scan result

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


Author: Vsevolod Stakhov
Date: 2020-09-22 12:32:38 +0100
URL: https://github.com/rspamd/rspamd/commit/c5e6bbff4a6efe44806d1c22beb9654d1d19776b

[Minor] Add method to remove symbol from a scan result

---
 src/libmime/scan_result.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++-
 src/libmime/scan_result.h | 12 +++++++++-
 2 files changed, 66 insertions(+), 2 deletions(-)

diff --git a/src/libmime/scan_result.c b/src/libmime/scan_result.c
index d987cbff2..7b92510ca 100644
--- a/src/libmime/scan_result.c
+++ b/src/libmime/scan_result.c
@@ -354,7 +354,8 @@ insert_metric_result (struct rspamd_task *task,
 
 						diff = NAN;
 						break;
-					} else if (gr_score) {
+					}
+					else if (gr_score) {
 						*gr_score += cur_diff;
 
 						if (cur_diff < diff) {
@@ -890,6 +891,59 @@ rspamd_task_find_symbol_result (struct rspamd_task *task, const char *sym,
 	return res;
 }
 
+struct rspamd_symbol_result* rspamd_task_remove_symbol_result (
+		struct rspamd_task *task,
+		const gchar *symbol,
+		struct rspamd_scan_result *result)
+{
+	struct rspamd_symbol_result *res = NULL;
+	khiter_t k;
+
+	if (result == NULL) {
+		/* Use default result */
+		result = task->result;
+	}
+
+	k = kh_get (rspamd_symbols_hash, result->symbols, symbol);
+
+	if (k != kh_end (result->symbols)) {
+		res = &kh_value (result->symbols, k);
+
+		if (!isnan (res->score)) {
+			/* Remove score from the result */
+			result->score -= res->score;
+
+			/* Also check the group limit */
+			if (result->sym_groups && res->sym) {
+				struct rspamd_symbol_group *gr;
+				gint i;
+
+				PTR_ARRAY_FOREACH (res->sym->groups, i, gr) {
+					gdouble *gr_score;
+
+					k = kh_get (rspamd_symbols_group_hash,
+							result->sym_groups, gr);
+
+					if (k != kh_end (result->sym_groups)) {
+						gr_score = &kh_value (result->sym_groups, k);
+
+						if (gr_score) {
+							*gr_score -= res->score;
+						}
+					}
+				}
+			}
+		}
+
+		kh_del (rspamd_symbols_hash, result->symbols, k);
+	}
+	else {
+		return NULL;
+	}
+
+	return res;
+}
+
 void
 rspamd_task_symbol_result_foreach (struct rspamd_task *task,
 								   struct rspamd_scan_result *result, GHFunc func,
diff --git a/src/libmime/scan_result.h b/src/libmime/scan_result.h
index 2f20e2c62..4d55a8e62 100644
--- a/src/libmime/scan_result.h
+++ b/src/libmime/scan_result.h
@@ -149,7 +149,17 @@ struct rspamd_symbol_result *rspamd_task_insert_result_full (struct rspamd_task
 #define rspamd_task_insert_result(task, symbol, flag, opts) \
     rspamd_task_insert_result_full (task, symbol, flag, opts, RSPAMD_SYMBOL_INSERT_DEFAULT, NULL)
 
-
+/**
+ * Removes a symbol from a specific symbol result
+ * @param task
+ * @param symbol
+ * @param result
+ * @return
+ */
+struct rspamd_symbol_result* rspamd_task_remove_symbol_result (
+		struct rspamd_task *task,
+		const gchar *symbol,
+		struct rspamd_scan_result *result);
 /**
  * Adds new option to symbol
  * @param task


More information about the Commits mailing list