]> source.dussan.org Git - rspamd.git/commitdiff
* Add Subject header that indicate how Subject must be rewritten in case of 'rewrite...
authorVsevolod Stakhov <vsevolod@rambler-co.ru>
Tue, 10 May 2011 18:52:01 +0000 (22:52 +0400)
committerVsevolod Stakhov <vsevolod@rambler-co.ru>
Tue, 10 May 2011 18:52:01 +0000 (22:52 +0400)
Fix initialization order of json settings.

src/cfg_xml.c
src/filter.h
src/protocol.c
src/settings.c

index 7fda9e14fe575216b070f59f3a6b8656ba79184b..00da907f475659a26d579501e5ba7edbc838f06c 100644 (file)
@@ -396,6 +396,12 @@ static struct xml_parser_rule grammar[] = {
                                G_STRUCT_OFFSET (struct metric, reject_score),
                                NULL
                        },
+                       {
+                               "subject",
+                               xml_handle_string,
+                               G_STRUCT_OFFSET (struct metric, subject),
+                               NULL
+                       },
                        {
                                "symbol",
                                handle_metric_symbol,
index 2c3dde4fc0e824650171dcb942fd6b6352e3bf24..dbc13defa27dca1c55a36fe0956f346576f43afa 100644 (file)
@@ -62,6 +62,7 @@ struct metric {
        GHashTable *descriptions;                                               /**< descriptions of symbols in metric                          */
        enum rspamd_metric_action action;                               /**< action to do by this metric by default                     */
        GList *actions;                                                                 /**< actions that can be performed by this metric       */
+       gchar *subject;                                                                 /**< subject rewrite string                                                     */
 };
 
 /**
index ac8515004882a9e59a5ba7dc31d7a3eda97e0e53..4d6507652f038f5e31f47198af50b2e7187891f1 100644 (file)
@@ -909,6 +909,39 @@ show_metric_symbols_json (struct metric_result *metric_res, struct metric_callba
        return cd->alive;
 }
 
+/* Write new subject */
+static const gchar *
+make_rewritten_subject (struct metric *metric, struct worker_task *task)
+{
+       static gchar                    subj_buf[1024];
+       gchar                          *p = subj_buf, *end, *c, *res;
+       const gchar                    *s;
+
+       end = p + sizeof(subj_buf);
+       c = metric->subject;
+       s = g_mime_message_get_subject (task->message);
+
+       while (p < end) {
+               if (*c == '\0') {
+                       *p = '\0';
+                       break;
+               }
+               else if (*c == '%' && *(c + 1) == 's') {
+                       p += rspamd_strlcpy (p, (s != NULL) ? s : "", end - p);
+                       c += 2;
+               }
+               else {
+                       *p = *c ++;
+               }
+               p ++;
+       }
+       res = g_mime_utils_header_encode_text (subj_buf);
+
+       memory_pool_add_destructor (task->task_pool, (pool_destruct_func)g_free, res);
+
+       return res;
+}
+
 /* Print a single metric line */
 static gint
 print_metric_data_rspamc (struct worker_task *task, gchar *outbuf, gsize size,
@@ -980,6 +1013,10 @@ print_metric_data_rspamc (struct worker_task *task, gchar *outbuf, gsize size,
                        r += rspamd_snprintf (outbuf + r, size - r,
                                        "Action: %s" CRLF, str_action_metric (action));
                }
+               if (action == METRIC_ACTION_REWRITE_SUBJECT && metric_res->metric->subject != NULL) {
+                       r += rspamd_snprintf (outbuf + r, size - r,
+                                       "Subject: %s" CRLF, make_rewritten_subject (metric_res->metric, task));
+               }
        }
 
        return r;
@@ -1020,6 +1057,10 @@ print_metric_data_json (struct worker_task *task, gchar *outbuf, gsize size,
                                        metric_res->score,
                                        task->is_skipped ? "true" : "false", ms, rs,
                                        str_action_metric (action));
+               if (action == METRIC_ACTION_REWRITE_SUBJECT && metric_res->metric->subject != NULL) {
+                       r += rspamd_snprintf (outbuf + r, size - r,
+                                       "    \"subject\": \"%s\"," CRLF, make_rewritten_subject (metric_res->metric, task));
+               }
        }
 
        return r;
index af4c37b8075dfcd6c5b051600fc2348b3aa31d59..8d18880a0b7a86dce4250b7524d9193f580ae0d8 100644 (file)
@@ -229,27 +229,25 @@ json_fin_cb (memory_pool_t * pool, struct map_cb_data *data)
                                                act_value = json_object_iter_value (act_it);
 
                                                if (act_value && json_is_number (act_value)) {
-                                                       if (check_action_str (json_object_iter_key (act_it), &j)) {
+                                                       /* Special cases */
+                                                       if (g_ascii_strcasecmp (json_object_iter_key (act_it), "spam_score") == 0) {
+                                                               score = g_malloc (sizeof (double));
+                                                               *score = json_number_value (act_value);
+                                                               g_hash_table_insert (cur_settings->metric_scores,
+                                                                               g_strdup (json_object_iter_key (json_it)), score);
+                                                       }
+                                                       else if (g_ascii_strcasecmp (json_object_iter_key (act_it), "reject_score") == 0) {
+                                                               score = g_malloc (sizeof (double));
+                                                               *score = json_number_value (act_value);
+                                                               g_hash_table_insert (cur_settings->reject_scores,
+                                                                               g_strdup (json_object_iter_key (json_it)), score);
+                                                       }
+                                                       else if (check_action_str (json_object_iter_key (act_it), &j)) {
                                                                new_act = g_malloc (sizeof (struct metric_action));
                                                                new_act->action = j;
                                                                new_act->score = json_number_value (act_value);
                                                                cur_act = g_list_prepend (cur_act, new_act);
                                                        }
-                                                       else {
-                                                               /* Special cases */
-                                                               if (g_ascii_strcasecmp (json_object_iter_key (act_it), "spam_score") == 0) {
-                                                                       score = g_malloc (sizeof (double));
-                                                                       *score = json_number_value (act_value);
-                                                                       g_hash_table_insert (cur_settings->metric_scores,
-                                                                                       g_strdup (json_object_iter_key (json_it)), score);
-                                                               }
-                                                               else if (g_ascii_strcasecmp (json_object_iter_key (act_it), "reject_score") == 0) {
-                                                                       score = g_malloc (sizeof (double));
-                                                                       *score = json_number_value (act_value);
-                                                                       g_hash_table_insert (cur_settings->reject_scores,
-                                                                                       g_strdup (json_object_iter_key (json_it)), score);
-                                                               }
-                                                       }
                                                }
                                                act_it = json_object_iter_next (it_val, act_it);
                                        }