From bbe772242e96fc37167a33a3f894d7f04e6b4087 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Sat, 28 Mar 2009 16:45:05 +0300 Subject: [PATCH] * Add ability for plugins to register its own functions in expression's parser --- src/expressions.c | 30 ++++++++++++++++++++++++++---- src/expressions.h | 8 ++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/expressions.c b/src/expressions.c index e706fc938..d4db580e2 100644 --- a/src/expressions.c +++ b/src/expressions.c @@ -30,8 +30,6 @@ #include "fuzzy.h" #include "expressions.h" -typedef gboolean (*rspamd_internal_func_t)(struct worker_task *, GList *args); - gboolean rspamd_compare_encoding (struct worker_task *task, GList *args); gboolean rspamd_header_exists (struct worker_task *task, GList *args); gboolean rspamd_content_type_compare_param (struct worker_task *task, GList *args); @@ -45,7 +43,7 @@ gboolean rspamd_parts_distance (struct worker_task *task, GList *args); * Sorted by name to use bsearch */ static struct _fl { - char *name; + const char *name; rspamd_internal_func_t func; } rspamd_functions_list[] = { { "compare_encoding", rspamd_compare_encoding }, @@ -57,6 +55,10 @@ static struct _fl { { "header_exists", rspamd_header_exists }, }; +static struct _fl *list_ptr = &rspamd_functions_list[0]; +static uint32_t functions_number = sizeof (rspamd_functions_list) / sizeof (struct _fl); +static gboolean list_allocated = FALSE; + /* Bsearch routine */ static int fl_cmp (const void *s1, const void *s2) @@ -595,7 +597,7 @@ call_expression_function (struct expression_function *func, struct worker_task * key.name = func->name; - selected = bsearch (&key, rspamd_functions_list, sizeof (rspamd_functions_list) / sizeof (struct _fl), + selected = bsearch (&key, list_ptr, functions_number, sizeof (struct _fl), fl_cmp); if (selected == NULL) { msg_warn ("call_expression_function: call to undefined function %s", key.name); @@ -605,6 +607,26 @@ call_expression_function (struct expression_function *func, struct worker_task * return selected->func (task, func->args); } +void +register_expression_function (const char *name, rspamd_internal_func_t func) +{ + static struct _fl *new; + + functions_number ++; + + new = g_new (struct _fl, functions_number); + memcpy (new, rspamd_functions_list, (functions_number - 1) * sizeof (struct _fl)); + if (list_allocated) { + g_free (list_ptr); + } + + list_allocated = TRUE; + new[functions_number - 1].name = name; + new[functions_number - 1].func = func; + qsort (new, functions_number, sizeof (struct _fl), fl_cmp); + list_ptr = new; +} + gboolean rspamd_compare_encoding (struct worker_task *task, GList *args) { diff --git a/src/expressions.h b/src/expressions.h index 65b555566..4b5ccce5e 100644 --- a/src/expressions.h +++ b/src/expressions.h @@ -41,6 +41,8 @@ struct expression { struct expression *next; /**< chain link */ }; +typedef gboolean (*rspamd_internal_func_t)(struct worker_task *, GList *args); + /** * Parse regexp line to regexp structure * @param pool memory pool to use @@ -65,5 +67,11 @@ struct expression* parse_expression (memory_pool_t *pool, char *line); */ gboolean call_expression_function (struct expression_function *func, struct worker_task *task); +/** + * Register specified function to rspamd internal functions list + * @param name name of function + * @param func pointer to function + */ +void register_expression_function (const char *name, rspamd_internal_func_t func); #endif -- 2.39.5