]> source.dussan.org Git - rspamd.git/commitdiff
[Project] Start results chain implementation
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 3 Apr 2020 10:13:56 +0000 (11:13 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 3 Apr 2020 10:13:56 +0000 (11:13 +0100)
src/libmime/scan_result.c
src/libmime/scan_result.h
src/libserver/task.c

index 8242c7de2253e68a4dad5010a60a48457a845fc8..a8fa38450d990aace2aa65cb9e093ad585f766a3 100644 (file)
@@ -43,6 +43,10 @@ rspamd_scan_result_dtor (gpointer d)
 
        rspamd_set_counter_ema (&symbols_count, kh_size (r->symbols), 0.5);
 
+       if (r->symbol_cbref != -1) {
+               luaL_unref (r->task->cfg->lua_state, LUA_REGISTRYINDEX, r->symbol_cbref);
+       }
+
        kh_foreach_value (r->symbols, sres, {
                if (sres.options) {
                        kh_destroy (rspamd_options_hash, sres.options);
@@ -53,21 +57,19 @@ rspamd_scan_result_dtor (gpointer d)
 }
 
 struct rspamd_scan_result *
-rspamd_create_metric_result (struct rspamd_task *task)
+rspamd_create_metric_result (struct rspamd_task *task,
+                                                        const gchar *name, gint lua_sym_cbref)
 {
        struct rspamd_scan_result *metric_res;
        guint i;
 
-       metric_res = task->result;
-
-       if (metric_res != NULL) {
-               return metric_res;
-       }
-
        metric_res = rspamd_mempool_alloc0 (task->task_pool,
                        sizeof (struct rspamd_scan_result));
        metric_res->symbols = kh_init (rspamd_symbols_hash);
        metric_res->sym_groups = kh_init (rspamd_symbols_group_hash);
+       metric_res->name = name;
+       metric_res->symbol_cbref = lua_sym_cbref;
+       metric_res->task = task;
 
        /* Optimize allocation */
        kh_resize (rspamd_symbols_group_hash, metric_res->sym_groups, 4);
@@ -101,6 +103,7 @@ rspamd_create_metric_result (struct rspamd_task *task)
        rspamd_mempool_add_destructor (task->task_pool,
                        rspamd_scan_result_dtor,
                        metric_res);
+       DL_APPEND (task->result, metric_res);
 
        return metric_res;
 }
index 3f9a6efe81c79337b9754fdc8288d49a82f34ec0..cdd6fe38b7796cca1a748415392f780ffe343d62 100644 (file)
@@ -40,7 +40,7 @@ struct rspamd_symbol_result {
        struct rspamd_symbol_option *opts_head;        /**< head of linked list of options                      */
        const gchar *name;
        struct rspamd_symbol *sym;                     /**< symbol configuration                                        */
-       gssize opts_len; /**< total size of all options (negative if truncated option is added) */
+       gssize opts_len;                               /**< total size of all options (negative if truncated option is added) */
        guint nshots;
        enum rspamd_symbol_result_flags flags;
 };
@@ -77,14 +77,18 @@ struct rspamd_scan_result {
        double score;                                    /**< total score                                                       */
        double grow_factor;                                /**< current grow factor                                     */
        struct rspamd_passthrough_result *passthrough_result;
-       guint npositive;
-       guint nnegative;
        double positive_score;
        double negative_score;
        struct kh_rspamd_symbols_hash_s *symbols;            /**< symbols of metric                                             */
        struct kh_rspamd_symbols_group_hash_s *sym_groups; /**< groups of symbols                                               */
        struct rspamd_action_result *actions_limits;
+       const gchar *name;                                 /**< for named results, NULL is the default result */
+       struct rspamd_task *task;                          /**< back reference */
+       gint symbol_cbref;                                 /**< lua function that defines if a symbol can be inserted, -1 if unused */
        guint nactions;
+       guint npositive;
+       guint nnegative;
+       struct rspamd_scan_result *prev, *next;           /**< double linked list of results */
 };
 
 /**
@@ -92,7 +96,8 @@ struct rspamd_scan_result {
  * @param task task object
  * @return metric result or NULL if metric `name` has not been found
  */
-struct rspamd_scan_result *rspamd_create_metric_result (struct rspamd_task *task);
+struct rspamd_scan_result *rspamd_create_metric_result (struct rspamd_task *task,
+               const gchar *name, gint lua_sym_cbref);
 
 /**
  * Adds a new passthrough result to a task
index 3e8dd381f1a8c0bc67baeb5783cac9291bbc5df4..a0d7bb12a969c72c0c75b7555d1756d8ee17eb9f 100644 (file)
@@ -111,7 +111,8 @@ rspamd_task_new (struct rspamd_worker *worker,
        new_task->request_headers = kh_init (rspamd_req_headers_hash);
        new_task->sock = -1;
        new_task->flags |= (RSPAMD_TASK_FLAG_MIME);
-       new_task->result = rspamd_create_metric_result (new_task);
+       /* Default results chain */
+       rspamd_create_metric_result (new_task, NULL, -1);
 
        new_task->queue_id = "undef";
        new_task->messages = ucl_object_typed_new (UCL_OBJECT);