From: Vsevolod Stakhov Date: Fri, 17 May 2019 13:30:03 +0000 (+0100) Subject: [Minor] Allow to execute expressions with process closures X-Git-Tag: 2.0~896 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=29a3b94f4dc310b1f8b77ec982b9062f80192424;p=rspamd.git [Minor] Allow to execute expressions with process closures --- diff --git a/src/libutil/expression.c b/src/libutil/expression.c index bf5bb467c..ecdb2e87a 100644 --- a/src/libutil/expression.c +++ b/src/libutil/expression.c @@ -1021,7 +1021,7 @@ rspamd_ast_process_node (struct rspamd_expression *expr, GNode *node, t1 = rspamd_get_ticks (TRUE); } - elt->value = expr->subr->process (process_data, elt->p.atom); + elt->value = process_data->process_closure (process_data, elt->p.atom); if (fabs (elt->value) > 1e-9) { elt->p.atom->hits ++; @@ -1101,7 +1101,9 @@ rspamd_ast_cleanup_traverse (GNode *n, gpointer d) } gdouble -rspamd_process_expression_track (struct rspamd_expression *expr, struct rspamd_expr_process_data *process_data) +rspamd_process_expression_closure (struct rspamd_expression *expr, + rspamd_expression_process_cb cb, + struct rspamd_expr_process_data *process_data) { gdouble ret = 0; @@ -1110,6 +1112,8 @@ rspamd_process_expression_track (struct rspamd_expression *expr, struct rspamd_e g_assert (expr->expression_stack->len == 0); expr->evals ++; + + process_data->process_closure = cb; ret = rspamd_ast_process_node (expr, expr->ast, process_data); /* Cleanup */ @@ -1133,9 +1137,19 @@ rspamd_process_expression_track (struct rspamd_expression *expr, struct rspamd_e } gdouble -rspamd_process_expression (struct rspamd_expression *expr, struct rspamd_expr_process_data *process_data) +rspamd_process_expression_track (struct rspamd_expression *expr, + struct rspamd_expr_process_data *process_data) +{ + return rspamd_process_expression_closure (expr, + expr->subr->process, process_data); +} + +gdouble +rspamd_process_expression (struct rspamd_expression *expr, + struct rspamd_expr_process_data *process_data) { - return rspamd_process_expression_track (expr, process_data); + return rspamd_process_expression_closure (expr, + expr->subr->process, process_data); } static gboolean diff --git a/src/libutil/expression.h b/src/libutil/expression.h index 7f7cb2dda..4f16812e6 100644 --- a/src/libutil/expression.h +++ b/src/libutil/expression.h @@ -56,6 +56,11 @@ typedef struct rspamd_expression_atom_s { gint priority; } rspamd_expression_atom_t; +struct rspamd_expr_process_data; + +typedef gdouble (*rspamd_expression_process_cb)(struct rspamd_expr_process_data *process_data, + rspamd_expression_atom_t *atom); + struct rspamd_expr_process_data { /* Current Lua state to run atom processing */ struct lua_State *L; @@ -66,6 +71,7 @@ struct rspamd_expr_process_data { GPtrArray *trace; struct composites_data *cd; struct rspamd_task *task; + rspamd_expression_process_cb process_closure; }; struct rspamd_atom_subr { @@ -73,7 +79,7 @@ struct rspamd_atom_subr { rspamd_expression_atom_t * (*parse)(const gchar *line, gsize len, rspamd_mempool_t *pool, gpointer ud, GError **err); /* Process atom via the opaque pointer (e.g. struct rspamd_task *) */ - gdouble (*process) (struct rspamd_expr_process_data *process_data, rspamd_expression_atom_t *atom); + rspamd_expression_process_cb process; /* Calculates the relative priority of the expression */ gint (*priority) (rspamd_expression_atom_t *atom); void (*destroy) (rspamd_expression_atom_t *atom); @@ -118,6 +124,16 @@ gdouble rspamd_process_expression (struct rspamd_expression *expr, gdouble rspamd_process_expression_track (struct rspamd_expression *expr, struct rspamd_expr_process_data *process_data); +/** + * Process the expression with the custom processor + * @param expr + * @param cb + * @param process_data + * @return + */ +gdouble rspamd_process_expression_closure (struct rspamd_expression *expr, + rspamd_expression_process_cb cb, + struct rspamd_expr_process_data *process_data); /** * Shows string representation of an expression * @param expr expression to show