From ea03e94d0a79c2f0f35e05bd6c9d44bf995a3e5b Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 14 Dec 2010 20:00:47 +0300 Subject: [PATCH] Add registering options for lua modules * Add ability to output colored messages for file and console loggers --- src/cfg_file.h | 1 + src/cfg_xml.c | 6 +++ src/logger.c | 35 +++++++++++++++--- src/lua/lua_config.c | 53 +++++++++++++++++++++++++++ src/main.c | 26 ++++++++----- src/plugins/lua/forged_recipients.lua | 4 ++ src/plugins/lua/maillist.lua | 3 +- src/plugins/lua/multimap.lua | 3 ++ src/plugins/lua/once_received.lua | 6 +++ src/plugins/lua/phishing.lua | 5 ++- src/plugins/lua/received_rbl.lua | 4 ++ src/plugins/lua/whitelist.lua | 5 +++ 12 files changed, 135 insertions(+), 16 deletions(-) diff --git a/src/cfg_file.h b/src/cfg_file.h index ab4a98361..29dd35200 100644 --- a/src/cfg_file.h +++ b/src/cfg_file.h @@ -263,6 +263,7 @@ struct config_file { gchar *debug_ip_map; /**< turn on debugging for specified ip addresses */ gboolean log_urls; /**< whether we should log URLs */ GList *debug_symbols; /**< symbols to debug */ + gboolean log_color; /**< output colors for console output */ gsize max_statfile_size; /**< maximum size for statfile */ diff --git a/src/cfg_xml.c b/src/cfg_xml.c index 374941bb0..37e8dea14 100644 --- a/src/cfg_xml.c +++ b/src/cfg_xml.c @@ -218,6 +218,12 @@ static struct xml_parser_rule grammar[] = { G_STRUCT_OFFSET (struct config_file, debug_symbols), NULL }, + { + "log_color", + xml_handle_boolean, + G_STRUCT_OFFSET (struct config_file, log_color), + NULL + }, NULL_ATTR }, NULL_DEF_ATTR diff --git a/src/logger.c b/src/logger.c index 85bd2fc90..281a8385d 100644 --- a/src/logger.c +++ b/src/logger.c @@ -460,7 +460,7 @@ file_log_function (const gchar * log_domain, const gchar *function, GLogLevelFla gchar tmpbuf[256], timebuf[32]; time_t now; struct tm *tms; - struct iovec iov[3]; + struct iovec iov[4]; gint r; guint32 cksum; size_t mlen; @@ -544,11 +544,28 @@ file_log_function (const gchar * log_domain, const gchar *function, GLogLevelFla strftime (timebuf, sizeof (timebuf), "%F %H:%M:%S", tms); cptype = process_to_str (rspamd_log->process_type); + if (rspamd_log->cfg->log_color) { + if (log_level >= G_LOG_LEVEL_INFO) { + /* White */ + r = rspamd_snprintf (tmpbuf, sizeof (tmpbuf), "\033[1;37m"); + } + else if (log_level >= G_LOG_LEVEL_WARNING) { + /* Magenta */ + r = rspamd_snprintf (tmpbuf, sizeof (tmpbuf), "\033[2;32m"); + } + else if (log_level >= G_LOG_LEVEL_CRITICAL) { + /* Red */ + r = rspamd_snprintf (tmpbuf, sizeof (tmpbuf), "\033[1;31m"); + } + } + else { + r = 0; + } if (function == NULL) { - r = rspamd_snprintf (tmpbuf, sizeof (tmpbuf), "%s #%P(%s) ", timebuf, rspamd_log->pid, cptype); + r += rspamd_snprintf (tmpbuf + r, sizeof (tmpbuf) - r, "%s #%P(%s) ", timebuf, rspamd_log->pid, cptype); } else { - r = rspamd_snprintf (tmpbuf, sizeof (tmpbuf), "%s #%P(%s) %s: ", timebuf, rspamd_log->pid, cptype, function); + r += rspamd_snprintf (tmpbuf + r, sizeof (tmpbuf) -r, "%s #%P(%s) %s: ", timebuf, rspamd_log->pid, cptype, function); } /* Construct IOV for log line */ iov[0].iov_base = tmpbuf; @@ -557,9 +574,17 @@ file_log_function (const gchar * log_domain, const gchar *function, GLogLevelFla iov[1].iov_len = mlen; iov[2].iov_base = (void *)&lf_chr; iov[2].iov_len = 1; + if (rspamd_log->cfg->log_color) { + iov[3].iov_base = "\033[0m"; + iov[3].iov_len = sizeof ("\033[0m") - 1; + /* Call helper (for buffering) */ + file_log_helper (iov, 4); + } + else { + /* Call helper (for buffering) */ + file_log_helper (iov, 3); + } - /* Call helper (for buffering) */ - file_log_helper (iov, 3); } } diff --git a/src/lua/lua_config.c b/src/lua/lua_config.c index 92eaf0b20..9bd12173c 100644 --- a/src/lua/lua_config.c +++ b/src/lua/lua_config.c @@ -30,6 +30,7 @@ #include "../radix.h" #include "../trie.h" #include "../classifiers/classifiers.h" +#include "../cfg_xml.h" /* Config file methods */ LUA_FUNCTION_DEF (config, get_module_opt); @@ -40,6 +41,7 @@ LUA_FUNCTION_DEF (config, add_hash_map); LUA_FUNCTION_DEF (config, get_classifier); LUA_FUNCTION_DEF (config, register_symbol); LUA_FUNCTION_DEF (config, register_post_filter); +LUA_FUNCTION_DEF (config, register_module_option); static const struct luaL_reg configlib_m[] = { LUA_INTERFACE_DEF (config, get_module_opt), @@ -49,6 +51,7 @@ static const struct luaL_reg configlib_m[] = { LUA_INTERFACE_DEF (config, add_hash_map), LUA_INTERFACE_DEF (config, get_classifier), LUA_INTERFACE_DEF (config, register_symbol), + LUA_INTERFACE_DEF (config, register_module_option), LUA_INTERFACE_DEF (config, register_post_filter), {"__tostring", lua_class_tostring}, {NULL, NULL} @@ -321,6 +324,56 @@ lua_config_register_function (lua_State *L) return 1; } +static gint +lua_config_register_module_option (lua_State *L) +{ + struct config_file *cfg = lua_check_config (L); + const gchar *mname, *optname, *stype; + enum module_opt_type type; + + if (cfg) { + mname = memory_pool_strdup (cfg->cfg_pool, luaL_checkstring (L, 2)); + optname = memory_pool_strdup (cfg->cfg_pool, luaL_checkstring (L, 3)); + stype = memory_pool_strdup (cfg->cfg_pool, luaL_checkstring (L, 4)); + if (mname && optname) { + if (stype == NULL) { + stype = "string"; + } + if (g_ascii_strcasecmp (stype, "string") == 0) { + type = MODULE_OPT_TYPE_STRING; + } + else if (g_ascii_strcasecmp (stype, "int") == 0) { + type = MODULE_OPT_TYPE_INT; + } + else if (g_ascii_strcasecmp (stype, "uint") == 0) { + type = MODULE_OPT_TYPE_UINT; + } + else if (g_ascii_strcasecmp (stype, "time") == 0) { + type = MODULE_OPT_TYPE_TIME; + } + else if (g_ascii_strcasecmp (stype, "size") == 0) { + type = MODULE_OPT_TYPE_SIZE; + } + else if (g_ascii_strcasecmp (stype, "map") == 0) { + type = MODULE_OPT_TYPE_MAP; + } + else if (g_ascii_strcasecmp (stype, "double") == 0) { + type = MODULE_OPT_TYPE_DOUBLE; + } + else { + msg_err ("unknown type '%s' for option: %s, for module: %s", stype, optname, mname); + luaL_error (L, "unknown type '%s' for option: %s, for module: %s", stype, optname, mname); + return 0; + } + register_module_opt (mname, optname, type); + return 1; + } + luaL_error (L, "bad arguments for register module option, must be: register_module_option(modulename, optionname, optiontype)"); + } + + return 0; +} + void lua_call_post_filters (struct worker_task *task) { diff --git a/src/main.c b/src/main.c index db8f6f276..ab62c8037 100644 --- a/src/main.c +++ b/src/main.c @@ -841,6 +841,9 @@ main (gint argc, gchar **argv, gchar **env) /* Init events to test modules */ event_init (); res = TRUE; + if (! init_lua_filters (rspamd->cfg)) { + res = FALSE; + } if (!check_modules_config (rspamd->cfg)) { res = FALSE; } @@ -856,9 +859,6 @@ main (gint argc, gchar **argv, gchar **env) } l = g_list_next (l); } - if (! init_lua_filters (rspamd->cfg)) { - res = FALSE; - } if (dump_vars) { dump_cfg_vars (rspamd->cfg); } @@ -879,13 +879,14 @@ main (gint argc, gchar **argv, gchar **env) msg_info ("rspamd " RVERSION " is starting, build id: " RID); rspamd->cfg->cfg_name = memory_pool_strdup (rspamd->cfg->cfg_pool, rspamd->cfg->cfg_name); - (void)check_modules_config (rspamd->cfg); + /* Daemonize */ if (!rspamd->cfg->no_fork && daemon (0, 0) == -1) { fprintf (stderr, "Cannot daemonize\n"); exit (-errno); } + /* Write info */ rspamd->pid = getpid (); rspamd->type = TYPE_MAIN; @@ -907,6 +908,15 @@ main (gint argc, gchar **argv, gchar **env) event_init (); g_mime_init (0); + /* Init lua filters */ + if (! init_lua_filters (rspamd->cfg)) { + msg_err ("error loading lua plugins"); + exit (EXIT_FAILURE); + } + + /* Check configuration for modules */ + (void)check_modules_config (rspamd->cfg); + /* Perform modules configuring */ l = g_list_first (rspamd->cfg->filters); @@ -920,15 +930,13 @@ main (gint argc, gchar **argv, gchar **env) l = g_list_next (l); } - if (! init_lua_filters (rspamd->cfg)) { - msg_err ("error loading lua plugins"); - exit (EXIT_FAILURE); - } - + /* Init config cache */ init_cfg_cache (rspamd->cfg); + /* Flush log */ flush_log_buf (); + /* Spawn workers */ rspamd->workers = g_hash_table_new (g_direct_hash, g_direct_equal); spawn_workers (rspamd); diff --git a/src/plugins/lua/forged_recipients.lua b/src/plugins/lua/forged_recipients.lua index 1f0ef8635..1283fa77e 100644 --- a/src/plugins/lua/forged_recipients.lua +++ b/src/plugins/lua/forged_recipients.lua @@ -63,6 +63,10 @@ function check_forged_headers(task) end end +-- Registration +rspamd_config:register_module_option('forged_recipients', 'symbol_rcpt', 'string') +rspamd_config:register_module_option('forged_recipients', 'symbol_sender', 'string') + -- Configuration local opts = rspamd_config:get_all_opt('forged_recipients') if opts then diff --git a/src/plugins/lua/maillist.lua b/src/plugins/lua/maillist.lua index 098c512a4..16be1764f 100644 --- a/src/plugins/lua/maillist.lua +++ b/src/plugins/lua/maillist.lua @@ -162,7 +162,8 @@ function check_maillist(task) task:insert_result(symbol, 1, 'subscribe.ru') end end - +-- Registration +rspamd_config:register_module_option('maillist', 'symbol', 'string') -- Configuration local opts = rspamd_config:get_all_opt('maillist') if opts then diff --git a/src/plugins/lua/multimap.lua b/src/plugins/lua/multimap.lua index fa049d3b5..0ab22194d 100644 --- a/src/plugins/lua/multimap.lua +++ b/src/plugins/lua/multimap.lua @@ -129,6 +129,9 @@ function add_rule(params) return 1 end +-- Registration +rspamd_config:register_module_option('multimap', 'rule', 'string') + local opts = rspamd_config:get_all_opt('multimap') if opts then local strrules = opts['rule'] diff --git a/src/plugins/lua/once_received.lua b/src/plugins/lua/once_received.lua index 404f13899..d20d432be 100644 --- a/src/plugins/lua/once_received.lua +++ b/src/plugins/lua/once_received.lua @@ -44,6 +44,12 @@ function check_quantity_received (task) end end +-- Registration +rspamd_config:register_module_option('once_received', 'symbol', 'string') +rspamd_config:register_module_option('once_received', 'symbol_strict', 'string') +rspamd_config:register_module_option('once_received', 'bad_host', 'string') +rspamd_config:register_module_option('once_received', 'good_host', 'string') + -- Configuration local opts = rspamd_config:get_all_opt('once_received') if opts then diff --git a/src/plugins/lua/phishing.lua b/src/plugins/lua/phishing.lua index 70bb67a52..af5c91e15 100644 --- a/src/plugins/lua/phishing.lua +++ b/src/plugins/lua/phishing.lua @@ -27,8 +27,11 @@ function phishing_cb (task) end end +-- Registration +rspamd_config:register_module_option('phishing', 'symbol', 'string') +rspamd_config:register_module_option('phishing', 'domains', 'map') -local opts = rspamd_config:get_all_opt('phishing') +local opts = rspamd_config:get_all_opt('phishing') if opts then if opts['symbol'] then symbol = opts['symbol'] diff --git a/src/plugins/lua/received_rbl.lua b/src/plugins/lua/received_rbl.lua index 0a74272c1..df7ba3ace 100644 --- a/src/plugins/lua/received_rbl.lua +++ b/src/plugins/lua/received_rbl.lua @@ -55,6 +55,10 @@ function received_cb (task) end end +-- Registration +rspamd_config:register_module_option('received_rbl', 'symbol', 'string') +rspamd_config:register_module_option('received_rbl', 'rbl', 'string') + -- Configuration local opts = rspamd_config:get_all_opt('received_rbl') if opts then diff --git a/src/plugins/lua/whitelist.lua b/src/plugins/lua/whitelist.lua index b1cab1aa1..1899809fe 100644 --- a/src/plugins/lua/whitelist.lua +++ b/src/plugins/lua/whitelist.lua @@ -32,6 +32,11 @@ function check_whitelist (task) end +-- Registration +rspamd_config:register_module_option('whitelist', 'symbol_ip', 'string') +rspamd_config:register_module_option('whitelist', 'symbol_from', 'string') +rspamd_config:register_module_option('whitelist', 'ip_whitelist', 'map') +rspamd_config:register_module_option('whitelist', 'from_whitelist', 'map') -- Configuration local opts = rspamd_config:get_all_opt('whitelist') -- 2.39.5