]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Fuzzy_check: Add weight_threshold option for fuzzy rules
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 22 Sep 2020 12:00:24 +0000 (13:00 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 22 Sep 2020 12:00:24 +0000 (13:00 +0100)
src/libmime/scan_result.h
src/plugins/fuzzy_check.c

index 4d55a8e62e21d0b7ff330954bd65cf2eef3a36f0..edba4d491888e5f82dacc3222d7a7c704ed73c12 100644 (file)
@@ -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
index 5c9ef7fe7de515e506be4d412e78cee7efca2d48..810abf5874da65cf27b53557154cb0b399f7c504 100644 (file)
@@ -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;