From bc953ee7602b6a4278fc5c13a83ad570444f5cc6 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 10 May 2011 22:52:01 +0400 Subject: * Add Subject header that indicate how Subject must be rewritten in case of 'rewrite subject' action Fix initialization order of json settings. --- src/cfg_xml.c | 6 ++++++ src/filter.h | 1 + src/protocol.c | 41 +++++++++++++++++++++++++++++++++++++++++ src/settings.c | 30 ++++++++++++++---------------- 4 files changed, 62 insertions(+), 16 deletions(-) diff --git a/src/cfg_xml.c b/src/cfg_xml.c index 7fda9e14f..00da907f4 100644 --- a/src/cfg_xml.c +++ b/src/cfg_xml.c @@ -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, diff --git a/src/filter.h b/src/filter.h index 2c3dde4fc..dbc13defa 100644 --- a/src/filter.h +++ b/src/filter.h @@ -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 */ }; /** diff --git a/src/protocol.c b/src/protocol.c index ac8515004..4d6507652 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -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; diff --git a/src/settings.c b/src/settings.c index af4c37b80..8d18880a0 100644 --- a/src/settings.c +++ b/src/settings.c @@ -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); } -- cgit v1.2.3