]> source.dussan.org Git - rspamd.git/commitdiff
* Add classifiers and composites sections to dump
authorVsevolod Stakhov <vsevolod@rambler-co.ru>
Tue, 6 Apr 2010 16:55:42 +0000 (20:55 +0400)
committerVsevolod Stakhov <vsevolod@rambler-co.ru>
Tue, 6 Apr 2010 16:55:42 +0000 (20:55 +0400)
src/cfg_file.h
src/cfg_file.y
src/cfg_xml.c
src/expressions.c
src/expressions.h

index 17f81b3a6ac439384a52cd6f492b22a8b6c4bb40..0cf70e8a00587b65ca977062aae71ef1bf92005f 100644 (file)
@@ -169,12 +169,13 @@ typedef double (*statfile_normalize_func)(double score, void *params);
 struct statfile {
        gchar *symbol;                                                                  /**< symbol of statfile                                                                 */
        gchar *path;                                                                    /**< filesystem pattern (with %r or %f)                                 */
-       gsize size;                                                                     /**< size of statfile                                                                   */
+       gsize size;                                                                             /**< size of statfile                                                                   */
        GList *sections;                                                                /**< list of sections in statfile                                               */
        struct statfile_autolearn_params *autolearn;    /**< autolearn params                                                                   */
        struct statfile_binlog_params *binlog;                  /**< binlog params                                                                              */
     statfile_normalize_func normalizer;             /**< function that is used as normaliser                */
     void *normalizer_data;                          /**< normalizer function params                         */
+       gchar *normalizer_str;                                                  /**< source string (for dump)                                                   */
 };
 
 /**
@@ -239,6 +240,7 @@ struct config_file {
        gboolean no_fork;                                                               /**< if 1 do not call daemon()                                                  */
        gboolean config_test;                                                   /**< if TRUE do only config file test                                   */
        gboolean raw_mode;                                                              /**< work in raw mode instead of utf one                                */
+       gboolean convert_config;                                                /**< convert config to XML format                                               */
 
        enum rspamd_log_type log_type;                                  /**< log type                                                                                   */
        int log_facility;                                                               /**< log facility in case of syslog                                             */
index 21509f2d69a8f718a6701828b66551a51d46d271..47d3a545c4b6b9c4031ae31de57a4ab0a20ad9a4 100644 (file)
@@ -1006,6 +1006,7 @@ statfilenormalizer:
             yyerror ("cannot parse normalizer string: %s", $3);
             YYERROR;
         }
+               cur_statfile->normalizer_str = memory_pool_strdup (cfg->cfg_pool, $3);
     }
     ;
 
index 46ccd1e2e5a2288a3143cdaed9967a8bd7a7ffa7..c089bf7a075b5b960d32f9d55875573d7917be3e 100644 (file)
@@ -1426,24 +1426,24 @@ xml_dump_main (struct config_file *cfg, FILE *f)
        fprintf (f, "<!-- Main section -->" CRLF);
 
        escaped_str = g_markup_escape_text (cfg->temp_dir, -1); 
-       fprintf (f, "  <tempdir>%s</tempdir>" CRLF, escaped_str);
+       fprintf (f, "<tempdir>%s</tempdir>" CRLF, escaped_str);
        g_free (escaped_str);
 
        escaped_str = g_markup_escape_text (cfg->pid_file, -1); 
-       fprintf (f, "  <pidfile>%s</pidfile>" CRLF, escaped_str);
+       fprintf (f, "<pidfile>%s</pidfile>" CRLF, escaped_str);
        g_free (escaped_str);
 
        escaped_str = g_markup_escape_text (cfg->filters_str, -1); 
-       fprintf (f, "  <filters>%s</filters>" CRLF, escaped_str);
+       fprintf (f, "<filters>%s</filters>" CRLF, escaped_str);
        g_free (escaped_str);
 
        if (cfg->checksum)  {
                escaped_str = g_markup_escape_text (cfg->checksum, -1); 
-               fprintf (f, "  <checksum>%s</checksum>" CRLF, escaped_str);
+               fprintf (f, "<checksum>%s</checksum>" CRLF, escaped_str);
                g_free (escaped_str);
        }
 
-       fprintf (f, "  <raw_mode>%s</raw_mode>" CRLF, cfg->raw_mode ? "yes" : "no");
+       fprintf (f, "<raw_mode>%s</raw_mode>" CRLF, cfg->raw_mode ? "yes" : "no");
 
        /* Print footer comment */
        fprintf (f, "<!-- End of main section -->" CRLF);
@@ -1460,7 +1460,7 @@ xml_variable_callback (gpointer key, gpointer value, gpointer user_data)
 
        escaped_key = g_markup_escape_text (key, -1); 
        escaped_value = g_markup_escape_text (value, -1);
-       fprintf (f,  "  <variable name=\"%s\">%s</variable>" CRLF, escaped_key, escaped_value);
+       fprintf (f,  "<variable name=\"%s\">%s</variable>" CRLF, escaped_key, escaped_value);
        g_free (escaped_key);
        g_free (escaped_value);
 }
@@ -1480,6 +1480,38 @@ xml_dump_variables (struct config_file *cfg, FILE *f)
        return TRUE;
 }
 
+/* Composites section */
+static void
+xml_composite_callback (gpointer key, gpointer value, gpointer user_data)
+{
+       FILE *f = user_data;
+       struct expression *expr;
+       char *escaped_key, *escaped_value;
+       
+       expr = value;
+
+       escaped_key = g_markup_escape_text (key, -1); 
+       escaped_value = g_markup_escape_text (expr->orig, -1);
+       fprintf (f,  "<composite name=\"%s\">%s</composite>" CRLF, escaped_key, escaped_value);
+       g_free (escaped_key);
+       g_free (escaped_value);
+}
+
+static gboolean
+xml_dump_composites (struct config_file *cfg, FILE *f)
+{
+       /* Print header comment */
+       fprintf (f, "<!-- Composites section -->" CRLF);
+
+       /* Iterate through variables */
+       g_hash_table_foreach (cfg->composite_symbols, xml_composite_callback, (gpointer)f);
+
+       /* Print footer comment */
+       fprintf (f, "<!-- End of composites section -->" CRLF);
+
+       return TRUE;
+}
+
 /* Workers */
 static void
 xml_worker_param_callback (gpointer key, gpointer value, gpointer user_data)
@@ -1589,6 +1621,73 @@ xml_dump_modules (struct config_file *cfg, FILE *f)
        return TRUE;
 }
 
+/* Classifiers dump */
+static void
+xml_classifier_callback (gpointer key, gpointer value, gpointer user_data)
+{
+       FILE *f = user_data;
+       char *escaped_key, *escaped_value;
+
+       escaped_key = g_markup_escape_text (key, -1); 
+       escaped_value = g_markup_escape_text (value, -1);
+       fprintf (f,  " <param name=\"%s\">%s</param>" CRLF, escaped_key, escaped_value);
+       g_free (escaped_key);
+       g_free (escaped_value);
+}
+
+static gboolean
+xml_dump_classifiers (struct config_file *cfg, FILE *f)
+{
+       GList *cur, *cur_st;
+       struct classifier_config *ccf;
+       struct statfile *st;
+
+       /* Print header comment */
+       fprintf (f, "<!-- Classifiers section -->" CRLF);
+
+       /* Iterate through classifiers */
+       cur = g_list_first (cfg->classifiers);
+       while (cur) {
+               ccf = cur->data;
+               fprintf (f, "<classifier type=\"%s\">" CRLF, ccf->classifier->name);
+               fprintf (f, " <tokenizer>%s</tokenizer>" CRLF, ccf->tokenizer->name);
+               fprintf (f, " <metric>%s</metric>" CRLF, ccf->metric);
+               g_hash_table_foreach (ccf->opts, xml_classifier_callback, f);
+               /* Statfiles */
+               cur_st = g_list_first (ccf->statfiles);
+               while (cur_st) {
+                       st = cur_st->data;
+                       fprintf (f, " <statfile>" CRLF);
+                       fprintf (f, "  <symbol>%s</symbol>" CRLF "  <size>%lu</size>" CRLF "  <path>%s</path>" CRLF,
+                                               st->symbol, (long unsigned)st->size, st->path);
+                       fprintf (f, "  <normalizer>%s</normalizer>" CRLF, st->normalizer_str);
+                       /* Binlog */
+                       if (st->binlog) {
+                               if (st->binlog->affinity == AFFINITY_MASTER) {
+                                       fprintf (f, "  <binlog>master</binlog>" CRLF);
+                               }
+                               else if (st->binlog->affinity == AFFINITY_SLAVE) {
+                                       fprintf (f, "  <binlog>slave</binlog>" CRLF);
+                                       fprintf (f, "  <binlog_master>%s:%d</binlog_master>" CRLF, 
+                                                       inet_ntoa (st->binlog->master_addr), ntohs (st->binlog->master_port)); 
+                               }
+                               fprintf (f, "  <binlog_rotate>%lu</binlog_rotate>" CRLF, (long unsigned)st->binlog->rotate_time);
+                       }
+                       fprintf (f, " </statfile>" CRLF);
+                       cur_st = g_list_next (cur_st);
+               }
+
+               fprintf (f, "</classifier>" CRLF);
+               cur = g_list_next (cur);
+       }
+
+       /* Print footer comment */
+       fprintf (f, "<!-- End of modules section -->" CRLF);
+
+       return TRUE;
+
+}
+
 #define CHECK_RES do { if (!res) { fclose (f); return FALSE; } } while (0)
 gboolean 
 xml_dump_config (struct config_file *cfg, const char *filename)
@@ -1609,10 +1708,14 @@ xml_dump_config (struct config_file *cfg, const char *filename)
        CHECK_RES;
        res = xml_dump_variables (cfg, f);
        CHECK_RES;
+       res = xml_dump_composites (cfg, f);
+       CHECK_RES;
        res = xml_dump_workers (cfg, f);
        CHECK_RES;
        res = xml_dump_modules (cfg, f);
        CHECK_RES;
+       res = xml_dump_classifiers (cfg, f);
+       CHECK_RES;
        /* Footer */
        fprintf (f, "</rspamd>" CRLF);
        fclose (f);
index 9d0b05f12dda79454943edf06ee5841f99a30950..34e7b0e283bc14f10ea7bcf61106046e8bb578d4 100644 (file)
@@ -242,12 +242,13 @@ is_regexp_flag (char a)
 }
 
 static void
-insert_expression (memory_pool_t * pool, struct expression **head, int type, char op, void *operand)
+insert_expression (memory_pool_t * pool, struct expression **head, int type, char op, void *operand, const char *orig)
 {
        struct expression              *new, *cur;
 
        new = memory_pool_alloc (pool, sizeof (struct expression));
        new->type = type;
+       new->orig = orig;
        if (new->type != EXPR_OPERATION) {
                new->content.operand = operand;
        }
@@ -301,7 +302,7 @@ parse_expression (memory_pool_t * pool, char *line)
        struct expression_function     *func = NULL, *old;
        struct expression              *arg;
        GQueue                         *function_stack;
-       char                           *p, *c, *str, op;
+       char                           *p, *c, *str, op, *copy;
        gboolean                        in_regexp = FALSE;
        int                             brackets = 0;
 
@@ -321,6 +322,7 @@ parse_expression (memory_pool_t * pool, char *line)
        msg_debug ("parsing expression {{ %s }}", line);
 
        function_stack = g_queue_new ();
+       copy = memory_pool_strdup (pool, line);
        p = line;
        c = p;
        while (*p) {
@@ -352,7 +354,7 @@ parse_expression (memory_pool_t * pool, char *line)
                                while (stack && stack->op != '(') {
                                        op = delete_expression_stack (&stack);
                                        if (op != '(') {
-                                               insert_expression (pool, &expr, EXPR_OPERATION, op, NULL);
+                                               insert_expression (pool, &expr, EXPR_OPERATION, op, NULL, copy);
                                        }
                                }
                                if (stack) {
@@ -377,7 +379,7 @@ parse_expression (memory_pool_t * pool, char *line)
                                                while ((stack != NULL) && (logic_priority (stack->op) >= logic_priority (*p))) {
                                                        op = delete_expression_stack (&stack);
                                                        if (op != '(') {
-                                                               insert_expression (pool, &expr, EXPR_OPERATION, op, NULL);
+                                                               insert_expression (pool, &expr, EXPR_OPERATION, op, NULL, copy);
                                                        }
                                                }
                                                stack = push_expression_stack (pool, stack, *p);
@@ -411,7 +413,7 @@ parse_expression (memory_pool_t * pool, char *line)
                                        g_strstrip (str);
                                        msg_debug ("found regexp: %s", str);
                                        if (strlen (str) > 0) {
-                                               insert_expression (pool, &expr, EXPR_REGEXP, 0, str);
+                                               insert_expression (pool, &expr, EXPR_REGEXP, 0, str, copy);
                                        }
                                }
                                c = p;
@@ -437,7 +439,7 @@ parse_expression (memory_pool_t * pool, char *line)
                                g_strstrip (func->name);
                                state = READ_FUNCTION_ARGUMENT;
                                g_queue_push_tail (function_stack, func);
-                               insert_expression (pool, &expr, EXPR_FUNCTION, 0, func);
+                               insert_expression (pool, &expr, EXPR_FUNCTION, 0, func, copy);
                                c = ++p;
                        }
                        else if (is_operation_symbol (*p)) {
@@ -447,7 +449,7 @@ parse_expression (memory_pool_t * pool, char *line)
                                        g_strlcpy (str, c, (p - c + 1));
                                        g_strstrip (str);
                                        if (strlen (str) > 0) {
-                                               insert_expression (pool, &expr, EXPR_STR, 0, str);
+                                               insert_expression (pool, &expr, EXPR_STR, 0, str, copy);
                                        }
                                }
                                state = READ_OPERATOR;
@@ -460,7 +462,7 @@ parse_expression (memory_pool_t * pool, char *line)
                                        g_strlcpy (str, c, (p - c + 1));
                                        g_strstrip (str);
                                        if (strlen (str) > 0) {
-                                               insert_expression (pool, &expr, EXPR_STR, 0, str);
+                                               insert_expression (pool, &expr, EXPR_STR, 0, str, copy);
                                        }
                                }
                                state = SKIP_SPACES;
@@ -521,7 +523,7 @@ parse_expression (memory_pool_t * pool, char *line)
        while (stack != NULL) {
                op = delete_expression_stack (&stack);
                if (op != '(') {
-                       insert_expression (pool, &expr, EXPR_OPERATION, op, NULL);
+                       insert_expression (pool, &expr, EXPR_OPERATION, op, NULL, copy);
                }
        }
 
index 4a20001cae0cd6788fc2fb942079ca39a67acf4d..519efc6daa17f05a4385cdc384491c59a260b574 100644 (file)
@@ -45,6 +45,7 @@ struct expression {
                void *operand;
                char operation;
        } content;                                                                                                      /**< union for storing operand or operation code        */
+       const gchar *orig;                                                                                      /**< original line                                                                      */
        struct expression *next;                                                                        /**< chain link                                                                         */
 };