]> source.dussan.org Git - rspamd.git/commitdiff
[Rework] Implement flags for inserting a symbol
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 30 Apr 2018 15:11:50 +0000 (16:11 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 30 Apr 2018 15:11:50 +0000 (16:11 +0100)
src/libmime/filter.c
src/libmime/filter.h
src/lua/lua_task.c

index 1efe898ce873f67a4bd1c29aa9df071de98a4ec4..5806c16143352d5475831afef41530662ae24147 100644 (file)
@@ -23,8 +23,6 @@
 #include "contrib/uthash/utlist.h"
 
 
-#define COMMON_PART_FACTOR 95
-
 struct rspamd_metric_result *
 rspamd_create_metric_result (struct rspamd_task *task)
 {
@@ -38,7 +36,7 @@ rspamd_create_metric_result (struct rspamd_task *task)
        }
 
        metric_res = rspamd_mempool_alloc (task->task_pool,
-                                       sizeof (struct rspamd_metric_result));
+                       sizeof (struct rspamd_metric_result));
        metric_res->symbols = g_hash_table_new (rspamd_str_hash,
                        rspamd_str_equal);
        rspamd_mempool_add_destructor (task->task_pool,
@@ -68,7 +66,7 @@ rspamd_check_group_score (struct rspamd_task *task,
        if (gr != NULL && group_score && gr->max_score > 0.0 && w > 0.0) {
                if (*group_score >= gr->max_score && w > 0) {
                        msg_info_task ("maximum group score %.2f for group %s has been reached,"
-                                       " ignoring symbol %s with weight %.2f", gr->max_score,
+                                                  " ignoring symbol %s with weight %.2f", gr->max_score,
                                        gr->name, symbol, w);
                        return NAN;
                }
@@ -82,10 +80,10 @@ rspamd_check_group_score (struct rspamd_task *task,
 
 static struct rspamd_symbol_result *
 insert_metric_result (struct rspamd_task *task,
-       const gchar *symbol,
-       double flag,
-       const gchar *opt,
-       gboolean single)
+               const gchar *symbol,
+               double flag,
+               const gchar *opt,
+               enum rspamd_symbol_insert_flags flags)
 {
        struct rspamd_metric_result *metric_res;
        struct rspamd_symbol_result *s = NULL;
@@ -94,6 +92,7 @@ insert_metric_result (struct rspamd_task *task,
        struct rspamd_symbols_group *gr = NULL;
        const ucl_object_t *mobj, *sobj;
        gint max_shots;
+       gboolean single = !!(flags & RSPAMD_SYMBOL_INSERT_SINGLE);
 
        metric_res = rspamd_create_metric_result (task);
 
@@ -105,7 +104,12 @@ insert_metric_result (struct rspamd_task *task,
 
        sdef = g_hash_table_lookup (task->cfg->symbols, symbol);
        if (sdef == NULL) {
-               w = 0.0;
+               if (flags & RSPAMD_SYMBOL_INSERT_ENFORCE) {
+                       w = 1.0 * flag; /* Enforce static weight to 1.0 */
+               }
+               else {
+                       w = 0.0;
+               }
        }
        else {
                w = (*sdef->weight_ptr) * flag;
@@ -248,18 +252,18 @@ insert_metric_result (struct rspamd_task *task,
        return s;
 }
 
-static struct rspamd_symbol_result *
-insert_result_common (struct rspamd_task *task,
-       const gchar *symbol,
-       double flag,
-       const gchar *opt,
-       gboolean single)
+struct rspamd_symbol_result *
+rspamd_task_insert_result_full (struct rspamd_task *task,
+               const gchar *symbol,
+               double flag,
+               const gchar *opt,
+               enum rspamd_symbol_insert_flags flags)
 {
        struct rspamd_symbol_result *s = NULL;
 
        if (task->processed_stages & (RSPAMD_TASK_STAGE_IDEMPOTENT >> 1)) {
                msg_err_task ("cannot insert symbol %s on idempotent phase",
-                       symbol);
+                               symbol);
 
                return NULL;
        }
@@ -269,7 +273,7 @@ insert_result_common (struct rspamd_task *task,
                        symbol,
                        flag,
                        opt,
-                       single);
+                       flags);
 
        /* Process cache item */
        if (task->cfg->cache) {
@@ -279,27 +283,6 @@ insert_result_common (struct rspamd_task *task,
        return s;
 }
 
-/* Insert result that may be increased on next insertions */
-struct rspamd_symbol_result *
-rspamd_task_insert_result (struct rspamd_task *task,
-       const gchar *symbol,
-       double flag,
-       const gchar *opt)
-{
-       return insert_result_common (task, symbol, flag, opt,
-                       FALSE);
-}
-
-/* Insert result as a single option */
-struct rspamd_symbol_result *
-rspamd_task_insert_result_single (struct rspamd_task *task,
-       const gchar *symbol,
-       double flag,
-       const gchar *opt)
-{
-       return insert_result_common (task, symbol, flag, opt, TRUE);
-}
-
 gboolean
 rspamd_task_add_result_option (struct rspamd_task *task,
                struct rspamd_symbol_result *s, const gchar *val)
index 3914413924b5f4644b8add37eeee6cc4ce0b6a1e..120749e3a827e770b348ba63e6e01ddc8f60990e 100644 (file)
@@ -49,6 +49,12 @@ struct rspamd_metric_result {
  */
 struct rspamd_metric_result * rspamd_create_metric_result (struct rspamd_task *task);
 
+enum rspamd_symbol_insert_flags {
+       RSPAMD_SYMBOL_INSERT_DEFAULT = 0,
+       RSPAMD_SYMBOL_INSERT_SINGLE = (1 << 0),
+       RSPAMD_SYMBOL_INSERT_ENFORCE = (1 << 1),
+};
+
 /**
  * Insert a result to task
  * @param task worker's task that present message from user
@@ -57,23 +63,16 @@ struct rspamd_metric_result * rspamd_create_metric_result (struct rspamd_task *t
  * @param flag numeric weight for symbol
  * @param opts list of symbol's options
  */
-struct rspamd_symbol_result* rspamd_task_insert_result (struct rspamd_task *task,
+struct rspamd_symbol_result* rspamd_task_insert_result_full (struct rspamd_task *task,
        const gchar *symbol,
        double flag,
-       const gchar *opts);
+       const gchar *opts,
+       enum rspamd_symbol_insert_flags flags);
 
-/**
- * Insert a single result to task
- * @param task worker's task that present message from user
- * @param metric_name metric's name to which we need to insert result
- * @param symbol symbol to insert
- * @param flag numeric weight for symbol
- * @param opts list of symbol's options
- */
-struct rspamd_symbol_result* rspamd_task_insert_result_single (struct rspamd_task *task,
-       const gchar *symbol,
-       double flag,
-       const gchar *opts);
+#define rspamd_task_insert_result_single(task, symbol, flag, opts) \
+       rspamd_task_insert_result_full (task, symbol, flag, opts, RSPAMD_SYMBOL_INSERT_SINGLE)
+#define rspamd_task_insert_result(task, symbol, flag, opts) \
+       rspamd_task_insert_result_full (task, symbol, flag, opts, RSPAMD_SYMBOL_INSERT_DEFAULT)
 
 
 /**
index 9819c4eac8232e908cb65a4f60e4fcfe8993ab09..dbd0dcd9bd936e2ec3d6d832dcc8340d627becd7 100644 (file)
@@ -81,9 +81,10 @@ LUA_FUNCTION_DEF (task, get_ev_base);
  */
 LUA_FUNCTION_DEF (task, get_worker);
 /***
- * @method task:insert_result(symbol, weight[, option1, ...])
+ * @method task:insert_result([enforce_symbol,]symbol, weight[, option1, ...])
  * Insert specific symbol to the tasks scanning results assigning the initial
  * weight to it.
+ * @param {boolean} enforce_symbol if represented and true, then insert symbol even if it is not registered in the metric
  * @param {string} symbol symbol to insert
  * @param {number} weight initial weight (this weight is multiplied by the metric weight)
  * @param {string} options list of optional options attached to a symbol inserted