]> source.dussan.org Git - rspamd.git/commitdiff
* Make regexp cache functions public for using them in other places
authorVsevolod Stakhov <vsevolod@rambler-co.ru>
Sun, 29 Mar 2009 10:02:01 +0000 (14:02 +0400)
committerVsevolod Stakhov <vsevolod@rambler-co.ru>
Sun, 29 Mar 2009 10:02:01 +0000 (14:02 +0400)
* Add function regexp_match_number that checks number of matched arguments:
  e.g.: regexp_match_number(2, ${__RE1}, ${__RE2}, header_exists(Subject))

src/expressions.c
src/expressions.h
src/plugins/regexp.c

index d4db580e2ebc82d745dafc520cbeb8d1b5841e82..957c43c5c9a081e6ebf408a16c7bc7d86c51e484 100644 (file)
@@ -71,7 +71,7 @@ fl_cmp (const void *s1, const void *s2)
 /* Cache for regular expressions that are used in functions */
 static GHashTable *re_cache = NULL;
 
-static inline void *
+void *
 re_cache_check (const char *line)
 {
        if (re_cache == NULL) {
@@ -81,7 +81,7 @@ re_cache_check (const char *line)
        return g_hash_table_lookup (re_cache, line);
 }
 
-static inline void
+void
 re_cache_add (char *line, void *pointer)
 {
        if (re_cache == NULL) {
index 4b5ccce5e77df2c61f174d9bf2a52bca6e9e393f..5c2a391eb8d8b77467b704ae88a70d5e23774e24 100644 (file)
@@ -74,4 +74,18 @@ gboolean call_expression_function (struct expression_function *func, struct work
  */
 void register_expression_function (const char *name, rspamd_internal_func_t func);
 
+/**
+ * Add regexp to regexp cache
+ * @param line symbolic representation
+ * @param pointer regexp data
+ */
+void re_cache_add (char *line, void *pointer);
+
+/**
+ * Check regexp in cache
+ * @param line symbolic representation
+ * @return pointer to regexp data or NULL if regexp is not found
+ */
+void * re_cache_check (const char *line);
+
 #endif
index 15edb7d70bfd1c812915cc784beb2d929544d457..3b3328f3eb54a27f03c4bbeb6b4710bf385a17a1 100644 (file)
@@ -57,6 +57,7 @@ struct regexp_ctx {
 static struct regexp_ctx *regexp_module_ctx = NULL;
 
 static int regexp_common_filter (struct worker_task *task);
+static gboolean rspamd_regexp_match_number (struct worker_task *task, GList *args);
 
 int
 regexp_module_init (struct config_file *cfg, struct module_ctx **ctx)
@@ -71,6 +72,7 @@ regexp_module_init (struct config_file *cfg, struct module_ctx **ctx)
        regexp_module_ctx->items = NULL;
 
        *ctx = (struct module_ctx *)regexp_module_ctx;
+       register_expression_function ("regexp_match_number", rspamd_regexp_match_number);
        
        return 0;
 }
@@ -382,3 +384,47 @@ regexp_common_filter (struct worker_task *task)
 
        return 0;
 }
+
+static gboolean 
+rspamd_regexp_match_number (struct worker_task *task, GList *args)
+{
+       char *param_pattern;
+       int param_count, res = 0;
+       struct rspamd_regexp *re;
+       struct expression_argument *arg;
+       GList *cur;
+       
+       if (args == NULL) {
+               msg_warn ("rspamd_regexp_match_number: no parameters to function");
+               return FALSE;
+       }
+       
+       arg = args->data;
+       param_count = strtoul (arg->data, NULL, 10);
+       
+       cur = g_list_next (args);
+       while (cur) {
+               arg = args->data;
+               if (arg->type == EXPRESSION_ARGUMENT_FUNCTION) {
+                       if (call_expression_function ((struct expression_function *)arg->data, task)) {
+                               res ++;
+                       }
+               }
+               else {
+                       param_pattern = (char *)arg->data;
+                       /* This is regexp, so compile and create g_regexp object */
+                       if ((re = re_cache_check (param_pattern)) == NULL) {
+                               re = parse_regexp (task->task_pool, param_pattern);
+                               if (re == NULL) {
+                                       msg_warn ("rspamd_regexp_match_number: cannot compile regexp for function");
+                                       return FALSE;
+                               }
+                               re_cache_add (param_pattern, re);
+                       }
+                       res += process_regexp (re, task);
+               }
+               cur = g_list_next (args);
+       }
+
+       return res >= param_count;
+}