]> source.dussan.org Git - rspamd.git/commitdiff
Add registering options for lua modules
authorVsevolod Stakhov <vsevolod@rambler-co.ru>
Tue, 14 Dec 2010 17:00:47 +0000 (20:00 +0300)
committerVsevolod Stakhov <vsevolod@rambler-co.ru>
Tue, 14 Dec 2010 17:00:47 +0000 (20:00 +0300)
* Add ability to output colored messages for file and console loggers

12 files changed:
src/cfg_file.h
src/cfg_xml.c
src/logger.c
src/lua/lua_config.c
src/main.c
src/plugins/lua/forged_recipients.lua
src/plugins/lua/maillist.lua
src/plugins/lua/multimap.lua
src/plugins/lua/once_received.lua
src/plugins/lua/phishing.lua
src/plugins/lua/received_rbl.lua
src/plugins/lua/whitelist.lua

index ab4a983619ca09f958c4c512ab7773d2c5475766..29dd35200a6a91f5f8ba9e47d624076a94cf190b 100644 (file)
@@ -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                                                  */
 
index 374941bb04fe0cf576f66b390d876f95779f29d9..37e8dea146840bc9aeead071977a01177d663499 100644 (file)
@@ -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
index 85bd2fc90575827332e08e57d8d212fd9ab84822..281a8385dccf0a616484d11cd8488a9cf6230659 100644 (file)
@@ -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);
        }
 }
 
index 92eaf0b2051e9d7f2573566ff4b9aa8e49fc00ce..9bd12173ca3016c125e650d44a3362352d7a4ff4 100644 (file)
@@ -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)
 {
index db8f6f2767b288beb842750d867caa2958249c0d..ab62c8037e0ea1ce85d95f0a596c79b18b8b2e29 100644 (file)
@@ -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);
 
index 1f0ef8635cd20df4f1d4a7caff9b32025a85b078..1283fa77e6cb20bc8b5d78a123c42058678ca439 100644 (file)
@@ -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
index 098c512a438a05fee7ae3cc98e06aa7b9bd939d0..16be1764fadc8c26af582fed4038b41bca1e9f42 100644 (file)
@@ -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
index fa049d3b58a6e2eca1f901d57b8976f3641543f8..0ab22194d9e4b380891a0f1147eb0b134504db90 100644 (file)
@@ -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']
index 404f138996b30c532a4e9f2f62996689c872baae..d20d432beb69644eabb4c6059ed8751ed169175d 100644 (file)
@@ -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
index 70bb67a520d5f429af4cc1a74b3f619b2189d77b..af5c91e15499fa9ed955f1908bf5e59e376aa91a 100644 (file)
@@ -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']
index 0a74272c167fa8bf04fb5253fb66c3ec80ea21fd..df7ba3ace0885bc46f2977c6a3a9ee1a38ce2fb1 100644 (file)
@@ -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
index b1cab1aa15d9e1b68a13ae385c093113f7cb308e..1899809fef66383ccbf29f79093521718e7b14fe 100644 (file)
@@ -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')