]> source.dussan.org Git - rspamd.git/commitdiff
[Project] Implement symbols insertion in multiple results mode
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 6 Apr 2020 14:17:54 +0000 (15:17 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 6 Apr 2020 17:23:44 +0000 (18:23 +0100)
src/libmime/scan_result.c

index a8fa38450d990aace2aa65cb9e093ad585f766a3..44c481e671b264f5683286de29423eb22ae70c01 100644 (file)
@@ -184,12 +184,12 @@ rspamd_check_group_score (struct rspamd_task *task,
 
 static struct rspamd_symbol_result *
 insert_metric_result (struct rspamd_task *task,
-               const gchar *symbol,
-               double weight,
-               const gchar *opt,
-               enum rspamd_symbol_insert_flags flags)
+                                         const gchar *symbol,
+                                         double weight,
+                                         const gchar *opt,
+                                         struct rspamd_scan_result *metric_res,
+                                         enum rspamd_symbol_insert_flags flags)
 {
-       struct rspamd_scan_result *metric_res;
        struct rspamd_symbol_result *s = NULL;
        gdouble final_score, *gr_score = NULL, next_gf = 1.0, diff;
        struct rspamd_symbol *sdef;
@@ -201,8 +201,6 @@ insert_metric_result (struct rspamd_task *task,
        gboolean single = !!(flags & RSPAMD_SYMBOL_INSERT_SINGLE);
        gchar *sym_cpy;
 
-       metric_res = task->result;
-
        if (!isfinite (weight)) {
                msg_warn_task ("detected %s score for symbol %s, replace it with zero",
                                isnan (weight) ? "NaN" : "infinity", symbol);
@@ -482,7 +480,8 @@ rspamd_task_insert_result_full (struct rspamd_task *task,
                const gchar *opt,
                enum rspamd_symbol_insert_flags flags)
 {
-       struct rspamd_symbol_result *s = NULL;
+       struct rspamd_symbol_result *s = NULL, *ret = NULL;
+       struct rspamd_scan_result *mres;
 
        if (task->processed_stages & (RSPAMD_TASK_STAGE_IDEMPOTENT >> 1)) {
                msg_err_task ("cannot insert symbol %s on idempotent phase",
@@ -491,19 +490,55 @@ rspamd_task_insert_result_full (struct rspamd_task *task,
                return NULL;
        }
 
-       /* Insert symbol to default metric */
-       s = insert_metric_result (task,
-                       symbol,
-                       weight,
-                       opt,
-                       flags);
+       DL_FOREACH (task->result, mres) {
+               if (mres->symbol_cbref != -1) {
+                       /* Check if we can insert this symbol to this symbol result */
+                       GError *err = NULL;
+                       lua_State *L = (lua_State *)task->cfg->lua_state;
+
+                       if (!rspamd_lua_universal_pcall (L, mres->symbol_cbref,
+                                       G_STRLOC, 1, "uss", &err,
+                                       "rspamd{task}", task, symbol, mres->name ? mres->name : "default")) {
+                               msg_warn_task ("cannot call for symbol_cbref for result %s: %e",
+                                               mres->name ? mres->name : "default", err);
+                               g_error_free (err);
+
+                               continue;
+                       }
+                       else {
+                               if (!lua_toboolean (L, -1)) {
+                                       /* Skip symbol */
+                                       msg_debug_metric ("skip symbol %s for result %s due to Lua return value",
+                                                       symbol, mres->name);
+                                       lua_pop (L, 1); /* Remove result */
+
+                                       continue;
+                               }
+
+                               lua_pop (L, 1); /* Remove result */
+                       }
+               }
 
-       /* Process cache item */
-       if (s && task->cfg->cache && s->sym) {
-               rspamd_symcache_inc_frequency (task->cfg->cache, s->sym->cache_item);
+               s = insert_metric_result (task,
+                               symbol,
+                               weight,
+                               opt,
+                               mres,
+                               flags);
+
+               if (mres->name == NULL) {
+                       /* Default result */
+                       ret = s;
+
+                       /* Process cache item */
+                       if (s && task->cfg->cache && s->sym) {
+                               rspamd_symcache_inc_frequency (task->cfg->cache,
+                                               s->sym->cache_item);
+                       }
+               }
        }
 
-       return s;
+       return ret;
 }
 
 static gchar *