From: Vsevolod Stakhov Date: Mon, 28 Mar 2016 13:20:39 +0000 (+0100) Subject: [Feature] Add tracking for rspamd expressions X-Git-Tag: 1.2.2~30 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=54781dd54db908d90b7d1e4acee46c89cbd6aeb0;p=rspamd.git [Feature] Add tracking for rspamd expressions --- diff --git a/src/libutil/expression.c b/src/libutil/expression.c index 782ad2371..5b31ef27d 100644 --- a/src/libutil/expression.c +++ b/src/libutil/expression.c @@ -920,7 +920,7 @@ rspamd_ast_do_op (struct rspamd_expression_elt *elt, gint val, gint acc, gint li static gint rspamd_ast_process_node (struct rspamd_expression *expr, gint flags, GNode *node, - gpointer data) + gpointer data, GPtrArray *track) { struct rspamd_expression_elt *elt, *celt, *parelt = NULL; GNode *cld; @@ -949,6 +949,10 @@ rspamd_ast_process_node (struct rspamd_expression *expr, gint flags, GNode *node if (elt->value) { elt->p.atom->hits ++; + + if (track) { + g_ptr_array_add (track, elt->p.atom); + } } if (calc_ticks) { @@ -989,7 +993,7 @@ rspamd_ast_process_node (struct rspamd_expression *expr, gint flags, GNode *node continue; } - val = rspamd_ast_process_node (expr, flags, cld, data); + val = rspamd_ast_process_node (expr, flags, cld, data, track); if (acc == G_MININT) { acc = val; @@ -1021,8 +1025,8 @@ rspamd_ast_cleanup_traverse (GNode *n, gpointer d) } gint -rspamd_process_expression (struct rspamd_expression *expr, gint flags, - gpointer data) +rspamd_process_expression_track (struct rspamd_expression *expr, gint flags, + gpointer data, GPtrArray *track) { gint ret = 0; @@ -1030,7 +1034,7 @@ rspamd_process_expression (struct rspamd_expression *expr, gint flags, /* Ensure that stack is empty at this point */ g_assert (expr->expression_stack->len == 0); - ret = rspamd_ast_process_node (expr, flags, expr->ast, data); + ret = rspamd_ast_process_node (expr, flags, expr->ast, data, track); /* Cleanup */ g_node_traverse (expr->ast, G_IN_ORDER, G_TRAVERSE_ALL, -1, @@ -1054,6 +1058,13 @@ rspamd_process_expression (struct rspamd_expression *expr, gint flags, return ret; } +gint +rspamd_process_expression (struct rspamd_expression *expr, gint flags, + gpointer data) +{ + return rspamd_process_expression_track (expr, flags, data, NULL); +} + static gboolean rspamd_ast_string_traverse (GNode *n, gpointer d) { diff --git a/src/libutil/expression.h b/src/libutil/expression.h index 82c0c60c1..2c0ec7497 100644 --- a/src/libutil/expression.h +++ b/src/libutil/expression.h @@ -95,6 +95,17 @@ gboolean rspamd_parse_expression (const gchar *line, gsize len, gint rspamd_process_expression (struct rspamd_expression *expr, gint flags, gpointer data); +/** + * Process the expression and return its value using atom 'process' functions with the specified data pointer. + * This function also accepts `track` argument where it writes matched atoms (those whose value is more than 0) + * @param expr expression to process + * @param data opaque data pointer for all the atoms + * @param track pointer array to atoms tracking + * @return the value of expression + */ +gint rspamd_process_expression_track (struct rspamd_expression *expr, gint flags, + gpointer data, GPtrArray *track); + /** * Shows string representation of an expression * @param expr expression to show