Browse Source

* Add utility function for parsing regexp from given string (type flags and pcre flags)

rspamd-0.5
cebka@mailsupport.rambler.ru 17 years ago
parent
commit
fd84d363d8
  1. 15
      cfg_file.h
  2. 100
      cfg_utils.c

15
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);

100
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
*/
Loading…
Cancel
Save