From 2809ad4747b7e3a3795aec4e433d7754c7efc365 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 2 Sep 2008 19:42:58 +0400 Subject: [PATCH] * Add ability to pass options to rspamd modules via flex start conditions --- cfg_file.h | 7 +++++++ cfg_file.l | 12 ++++++++++++ cfg_file.y | 33 +++++++++++++++++++++++++++++++-- cfg_utils.c | 23 +++++++++++++++++++++++ 4 files changed, 73 insertions(+), 2 deletions(-) diff --git a/cfg_file.h b/cfg_file.h index 62856b489..401593584 100644 --- a/cfg_file.h +++ b/cfg_file.h @@ -84,6 +84,12 @@ struct filter_chain { LIST_ENTRY (filter_chain) next; }; +struct module_opt { + char *param; + char *value; + LIST_ENTRY (module_opt) next; +}; + struct config_file { char *cfg_name; char *pid_file; @@ -108,6 +114,7 @@ struct config_file { LIST_HEAD (perlq, filter_chain) filters; LIST_HEAD (modulesq, perl_module) perl_modules; LIST_HEAD (cmodulesq, c_module) c_modules; + GHashTable* modules_opts; }; int add_memcached_server (struct config_file *cf, char *str); diff --git a/cfg_file.l b/cfg_file.l index 760afc1ad..8cde589b5 100644 --- a/cfg_file.l +++ b/cfg_file.l @@ -1,4 +1,5 @@ %x incl +%x module %{ #include @@ -98,6 +99,7 @@ parse_flag (const char *str) %% ^[ \t]*#.* /* ignore comments */; .include BEGIN(incl); +.module BEGIN(module); tempdir return TEMPDIR; pidfile return PIDFILE; workers return WORKERS; @@ -120,6 +122,7 @@ script_message return SCRIPT_MESSAGE; script_url return SCRIPT_URL; script_chain return SCRIPT_CHAIN; + \{ return OBRACE; \} return EBRACE; ; return SEMICOLON; @@ -177,6 +180,15 @@ yes|YES|no|NO|[yY]|[nN] yylval.flag=parse_flag(yytext); return FLAG; } } +\n /* ignore EOL */; +[ \t]+ /* ignore whitespace */; +\'[a-zA-Z0-9_-]\' yylval.string=strdup(yytext + 1); yylval.string[strlen(yylval.string) - 1] = '\0'; return MODULE_OPT; +\{ return OBRACE; +\} return EBRACE; +\; return SEMICOLON; +[a-zA-Z0-9_-] yylval.string=strdup(yytext); return PARAM; +\"[^"]+\" yylval.string=strdup(yytext + 1); yylval.string[strlen(yylval.string) - 1] = '\0'; return QUOTEDSTRING; + %% /* * vi:ts=4 diff --git a/cfg_file.y b/cfg_file.y index 91f84f4ce..8448933b4 100644 --- a/cfg_file.y +++ b/cfg_file.y @@ -26,6 +26,7 @@ extern char *yytext; struct scriptq *cur_scripts; unsigned int cur_scripts_num = 0; +LIST_HEAD (moduleoptq, module_opt) *cur_module_opt = NULL; %} @@ -47,9 +48,10 @@ unsigned int cur_scripts_num = 0; %token READ_SERVERS WRITE_SERVER DIRECTORY_SERVERS MAILBOX_QUERY USERS_QUERY LASTLOGIN_QUERY %token MEMCACHED WORKERS REQUIRE MODULE %token FILTER METRIC SCRIPT_HEADER SCRIPT_MIME SCRIPT_MESSAGE SCRIPT_URL SCRIPT_CHAIN SCRIPT_PARAM +%token MODULE_OPT, PARAM %type STRING -%type QUOTEDSTRING +%type QUOTEDSTRING MODULE_OPT PARAM %type FILENAME %type SOCKCRED %type IPADDR IPNETWORK @@ -77,6 +79,7 @@ command : | workers | require | filter + | module_opt ; tempdir : @@ -130,7 +133,7 @@ bind_cred: ; memcached: - BEANSTALK OBRACE memcachedbody EBRACE + MEMCACHED OBRACE memcachedbody EBRACE ; memcachedbody: @@ -374,6 +377,32 @@ requirecmd: } ; +module_opt: + MODULE_OPT OBRACE moduleoptbody EBRACE { + g_hash_table_insert (cfg->modules_opts, $1, cur_module_opt); + cur_module_opt = NULL; + } + ; + +moduleoptbody: + optcmd SEMICOLON + | moduleoptbody optcmd SEMICOLON + ; + +optcmd: + PARAM EQSIGN QUOTEDSTRING { + struct module_opt *mopt; + if (cur_module_opt == NULL) { + cur_module_opt = g_malloc (sizeof (cur_module_opt)); + LIST_INIT (cur_module_opt); + } + mopt = g_malloc (sizeof (struct module_opt)); + mopt->param = $1; + mopt->value = $3; + LIST_INSERT_HEAD (cur_module_opt, mopt, next); + } + ; + %% /* * vi:ts=4 diff --git a/cfg_utils.c b/cfg_utils.c index 78c37dabd..83738aea6 100644 --- a/cfg_utils.c +++ b/cfg_utils.c @@ -24,6 +24,24 @@ extern int yylineno; extern char *yytext; +static void +clean_hash_bucket (gpointer key, gpointer value, gpointer unused) +{ + LIST_HEAD (moduleoptq, module_opt) *cur_module_opt = (struct moduleoptq *)value; + struct module_opt *cur, *tmp; + + LIST_FOREACH_SAFE (cur, cur_module_opt, next, tmp) { + if (cur->param) { + free (cur->param); + } + if (cur->value) { + free (cur->value); + } + LIST_REMOVE (cur, next); + free (cur); + } +} + int add_memcached_server (struct config_file *cf, char *str) { @@ -126,6 +144,7 @@ init_defaults (struct config_file *cfg) cfg->memcached_protocol = TCP_TEXT; cfg->workers_number = DEFAULT_WORKERS_NUM; + cfg->modules_opts = g_hash_table_new (g_str_hash, g_str_equal); LIST_INIT (&cfg->filters); LIST_INIT (&cfg->perl_modules); @@ -178,6 +197,10 @@ free_config (struct config_file *cfg) } free (cmodule); } + + g_hash_table_foreach (cfg->modules_opts, clean_hash_bucket, NULL); + g_hash_table_remove_all (cfg->modules_opts); + g_hash_table_unref (cfg->modules_opts); } int -- 2.39.5