]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Allow to execute expressions with process closures
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 17 May 2019 13:30:03 +0000 (14:30 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 17 May 2019 13:30:03 +0000 (14:30 +0100)
src/libutil/expression.c
src/libutil/expression.h

index bf5bb467c86c1ed2bf79b81818bce7ef0b473cfd..ecdb2e87a6aae139c2d2e6da5e6873c21d8f2e3a 100644 (file)
@@ -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
index 7f7cb2ddaacfea5e686e08aa083364b4c808633b..4f16812e6761ce23a3eaf043fcee7b429f9d694c 100644 (file)
@@ -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