From: cebka@mailsupport.rambler.ru Date: Tue, 14 Oct 2008 01:03:54 +0000 (+0400) Subject: * Add utility function for parsing regexp from given string (type flags and pcre... X-Git-Tag: 0.2.7~362 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=fd84d363d87414274dc24e77f301ab5c966ede0b;p=rspamd.git * Add utility function for parsing regexp from given string (type flags and pcre flags) --- diff --git a/cfg_file.h b/cfg_file.h index 5b4e2d84b..21c8b26fa 100644 --- a/cfg_file.h +++ b/cfg_file.h @@ -50,6 +50,20 @@ 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); diff --git a/cfg_utils.c b/cfg_utils.c index 6f290d2d8..3f46b0dd8 100644 --- a/cfg_utils.c +++ b/cfg_utils.c @@ -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 */