Fix initialization order of json settings.
G_STRUCT_OFFSET (struct metric, reject_score),
NULL
},
+ {
+ "subject",
+ xml_handle_string,
+ G_STRUCT_OFFSET (struct metric, subject),
+ NULL
+ },
{
"symbol",
handle_metric_symbol,
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 */
};
/**
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,
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;
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;
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);
}