summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/libutil/expression.c36
-rw-r--r--src/libutil/expression.h17
2 files changed, 53 insertions, 0 deletions
diff --git a/src/libutil/expression.c b/src/libutil/expression.c
index 6e58fa40d..2f05c98a3 100644
--- a/src/libutil/expression.c
+++ b/src/libutil/expression.c
@@ -1091,3 +1091,39 @@ rspamd_expression_tostring (struct rspamd_expression *expr)
return res;
}
+
+struct atom_foreach_cbdata {
+ rspamd_expression_atom_foreach_cb cb;
+ gpointer cbdata;
+};
+
+static gboolean
+rspamd_ast_atom_traverse (GNode *n, gpointer d)
+{
+ struct atom_foreach_cbdata *data = d;
+ struct rspamd_expression_elt *elt = n->data;
+ rspamd_ftok_t tok;
+
+ if (elt->type == ELT_ATOM) {
+ tok.begin = elt->p.atom->str;
+ tok.len = elt->p.atom->len;
+
+ data->cb (&tok, data->cbdata);
+ }
+
+ return FALSE;
+}
+
+void
+rspamd_expression_atom_foreach (struct rspamd_expression *expr,
+ rspamd_expression_atom_foreach_cb cb, gpointer cbdata)
+{
+ struct atom_foreach_cbdata data;
+
+ g_assert (expr != NULL);
+
+ data.cb = cb;
+ data.cbdata = cbdata;
+ g_node_traverse (expr->ast, G_POST_ORDER, G_TRAVERSE_ALL, -1,
+ rspamd_ast_atom_traverse, &data);
+}
diff --git a/src/libutil/expression.h b/src/libutil/expression.h
index 6f422f1bf..4a60f4a56 100644
--- a/src/libutil/expression.h
+++ b/src/libutil/expression.h
@@ -27,6 +27,7 @@
#include "config.h"
#include "mem_pool.h"
+#include "fstring.h"
#define RSPAMD_EXPRESSION_MAX_PRIORITY 1024
@@ -93,4 +94,20 @@ gint rspamd_process_expression (struct rspamd_expression *expr, gint flags,
*/
GString *rspamd_expression_tostring (struct rspamd_expression *expr);
+/**
+ * Callback that is called on @see rspamd_expression_atom_foreach, atom is ephemeral
+ * and should not be modified within callback
+ */
+typedef void (*rspamd_expression_atom_foreach_cb) (const rspamd_ftok_t *atom,
+ gpointer ud);
+
+/**
+ * Traverse over all atoms in the expression
+ * @param expr expression
+ * @param cb callback to be called
+ * @param ud opaque data passed to `cb`
+ */
+void rspamd_expression_atom_foreach (struct rspamd_expression *expr,
+ rspamd_expression_atom_foreach_cb cb, gpointer cbdata);
+
#endif /* SRC_LIBUTIL_EXPRESSION_H_ */