]> source.dussan.org Git - rspamd.git/commitdiff
* Add utility function for parsing regexp from given string (type flags and pcre...
authorcebka@mailsupport.rambler.ru <cebka@mailsupport.rambler.ru>
Tue, 14 Oct 2008 01:03:54 +0000 (05:03 +0400)
committercebka@mailsupport.rambler.ru <cebka@mailsupport.rambler.ru>
Tue, 14 Oct 2008 01:03:54 +0000 (05:03 +0400)
cfg_file.h
cfg_utils.c

index 5b4e2d84bb97491159f27bdf1f10984a82821ff1..21c8b26fac3da5a747c4e3c8f0f8c8635fea9fca 100644 (file)
 
 enum { VAL_UNDEF=0, VAL_TRUE, VAL_FALSE };
 
+enum rspamd_regexp_type {
+       REGEXP_NONE = 0,
+       REGEXP_HEADER,
+       REGEXP_MIME,
+       REGEXP_MESSAGE,
+       REGEXP_URL,
+};
+
+struct rspamd_regexp {
+       enum rspamd_regexp_type type;
+       char *regexp_text;
+       GRegex *regexp;
+};
+
 struct memcached_server {
        struct upstream up;
        struct in_addr addr;
@@ -117,6 +131,7 @@ unsigned int parse_seconds (const char *t);
 char parse_flag (const char *str);
 char* substitute_variable (struct config_file *cfg, char *str, u_char recursive);
 void post_load_config (struct config_file *cfg);
+struct rspamd_regexp* parse_regexp (memory_pool_t *pool, char *line);
 
 int yylex (void);
 int yyparse (void);
index 6f290d2d876f8574952f6ba376c3e52f2ae4e563..3f46b0dd81aca39477e3eed40d37fad3a5e85ae3 100644 (file)
@@ -399,6 +399,106 @@ post_load_config (struct config_file *cfg)
        parse_filters_str (cfg, cfg->url_filters_str, SCRIPT_URL);
 }
 
+/*
+ * Rspamd regexp utility functions
+ */
+struct rspamd_regexp*
+parse_regexp (memory_pool_t *pool, char *line)
+{
+       char *begin, *end, *p;
+       struct rspamd_regexp *result;
+       int regexp_flags = 0;
+       enum rspamd_regexp_type type = REGEXP_NONE;
+       GError *err;
+       
+       /* Find begin */
+       while (*line != '/') {
+               line ++;
+       }
+       if (*line != '\0') {
+               begin = line + 1;
+       }
+       else {
+               return NULL;
+       }
+       /* Find end */
+       end = begin;
+       while (*end && (*end != '/' || *(end - 1) == '\\')) {
+               end ++;
+       }
+       if (end == begin || *end != '/') {
+               return NULL;
+       }
+       /* Parse flags */
+       p = end + 1;
+       while (p != NULL) {
+               switch (*p) {
+                       case 'i':
+                               regexp_flags |= G_REGEX_CASELESS;
+                               p ++;
+                               break;
+                       case 'm':
+                               regexp_flags |= G_REGEX_MULTILINE;
+                               p ++;
+                               break;
+                       case 's':
+                               regexp_flags |= G_REGEX_DOTALL;
+                               p ++;
+                               break;
+                       case 'x':
+                               regexp_flags |= G_REGEX_EXTENDED;
+                               p ++;
+                               break;
+                       case 'u':
+                               regexp_flags |= G_REGEX_UNGREEDY;
+                               p ++;
+                               break;
+                       case 'o':
+                               regexp_flags |= G_REGEX_OPTIMIZE;
+                               p ++;
+                               break;
+                       /* Type flags */
+                       case 'H':
+                               if (type != REGEXP_NONE) {
+                                       type = REGEXP_HEADER;
+                               }
+                               p ++;
+                               break;
+                       case 'M':
+                               if (type != REGEXP_NONE) {
+                                       type = REGEXP_MESSAGE;
+                               }
+                               p ++;
+                               break;
+                       case 'P':
+                               if (type != REGEXP_NONE) {
+                                       type = REGEXP_MIME;
+                               }
+                               p ++;
+                               break;
+                       case 'U':
+                               if (type != REGEXP_NONE) {
+                                       type = REGEXP_URL;
+                               }
+                               p ++;
+                               break;
+                       /* Stop flags parsing */
+                       default:
+                               p = NULL;
+                               break;
+               }
+       }
+
+       result = memory_pool_alloc (pool, sizeof (struct rspamd_regexp));
+       result->type = type;
+       *end = '\0';
+       result->regexp = g_regex_new (begin, regexp_flags, 0, &err);
+       result->regexp_text = memory_pool_strdup (pool, begin);
+       *end = '/';
+
+       return result;
+}
+
 /*
  * vi:ts=4
  */