]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Add logging of groups to the log_format
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 30 Aug 2019 18:39:42 +0000 (19:39 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 30 Aug 2019 18:40:08 +0000 (19:40 +0100)
src/libserver/cfg_file.h
src/libserver/cfg_utils.c
src/libserver/task.c

index cffe997e4c27242cfb8ca535caea8f59b1e142a1..4faca7b56c5e269a9b5c6cb59413020756f769b4 100644 (file)
@@ -258,6 +258,8 @@ enum rspamd_log_format_type {
        RSPAMD_LOG_FILENAME,
        RSPAMD_LOG_FORCED_ACTION,
        RSPAMD_LOG_SETTINGS_ID,
+       RSPAMD_LOG_GROUPS,
+       RSPAMD_LOG_PUBLIC_GROUPS,
 };
 
 enum rspamd_log_format_flags {
index 8bbfd43e0bf6ae6483551a97c123d722707848d9..619e8784e205f8c43c7e12b5687a156ab10c5229 100644 (file)
@@ -451,6 +451,12 @@ rspamd_config_process_var (struct rspamd_config *cfg, const rspamd_ftok_t *var,
                type = RSPAMD_LOG_SYMBOLS;
                flags |= RSPAMD_LOG_FMT_FLAG_SYMBOLS_PARAMS|RSPAMD_LOG_FMT_FLAG_SYMBOLS_SCORES;
        }
+       else if (rspamd_ftok_cstr_equal (&tok, "groups", TRUE)) {
+               type = RSPAMD_LOG_GROUPS;
+       }
+       else if (rspamd_ftok_cstr_equal (&tok, "public_groups", TRUE)) {
+               type = RSPAMD_LOG_PUBLIC_GROUPS;
+       }
        else if (rspamd_ftok_cstr_equal (&tok, "ip", TRUE)) {
                type = RSPAMD_LOG_IP;
        }
index b8e178a7bdcac16706c7865cf9695d1fca632b34..6b5f8538156e93c5a0ac657d5b962e2bf417baec 100644 (file)
@@ -1035,6 +1035,16 @@ rspamd_task_compare_log_sym (gconstpointer a, gconstpointer b)
        return (w2 - w1) * 1000.0;
 }
 
+static gint
+rspamd_task_compare_log_group (gconstpointer a, gconstpointer b)
+{
+       const struct rspamd_symbols_group *s1 = *(const struct rspamd_symbols_group **)a,
+                       *s2 = *(const struct rspamd_symbols_group **)b;
+
+       return strcmp (s1->name, s2->name);
+}
+
+
 static rspamd_ftok_t
 rspamd_task_log_metric_res (struct rspamd_task *task,
                struct rspamd_log_format *lf)
@@ -1047,7 +1057,10 @@ rspamd_task_log_metric_res (struct rspamd_task *task,
        struct rspamd_symbol_result *sym;
        GPtrArray *sorted_symbols;
        struct rspamd_action *act;
+       struct rspamd_symbols_group *gr;
+       gdouble gr_score;
        guint i, j;
+       khiter_t k;
 
        mres = task->result;
        act = rspamd_check_action_metric (task);
@@ -1136,6 +1149,49 @@ rspamd_task_log_metric_res (struct rspamd_task *task,
                        res.begin = symbuf->str;
                        res.len = symbuf->len;
                        break;
+
+               case RSPAMD_LOG_GROUPS:
+               case RSPAMD_LOG_PUBLIC_GROUPS:
+
+                       symbuf = rspamd_fstring_sized_new (128);
+                       sorted_symbols = g_ptr_array_sized_new (kh_size (mres->sym_groups));
+
+                       kh_foreach (mres->sym_groups, gr, gr_score,{
+                               if (!(gr->flags & RSPAMD_SYMBOL_GROUP_PUBLIC)) {
+                                       if (lf->type == RSPAMD_LOG_PUBLIC_GROUPS) {
+                                               continue;
+                                       }
+                               }
+                       });
+
+                       g_ptr_array_sort (sorted_symbols, rspamd_task_compare_log_group);
+
+                       for (i = 0; i < sorted_symbols->len; i++) {
+                               gr = g_ptr_array_index (sorted_symbols, i);
+
+                               if (first) {
+                                       rspamd_printf_fstring (&symbuf, "%s", gr->name);
+                               }
+                               else {
+                                       rspamd_printf_fstring (&symbuf, ",%s", gr->name);
+                               }
+
+                               k = kh_get (rspamd_symbols_group_hash, mres->sym_groups, gr);
+
+                               rspamd_printf_fstring (&symbuf, "(%.2f)",
+                                               kh_value (mres->sym_groups, k));
+
+                               first = FALSE;
+                       }
+
+                       g_ptr_array_free (sorted_symbols, TRUE);
+
+                       rspamd_mempool_add_destructor (task->task_pool,
+                                       (rspamd_mempool_destruct_t) rspamd_fstring_free,
+                                       symbuf);
+                       res.begin = symbuf->str;
+                       res.len = symbuf->len;
+                       break;
                default:
                        break;
                }