]> source.dussan.org Git - rspamd.git/commitdiff
Rework classifiers configuration and naming.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 23 Jan 2015 15:36:31 +0000 (15:36 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 23 Jan 2015 15:36:31 +0000 (15:36 +0000)
Now it is possible to register multiple classifiers with the same type.

src/libserver/cfg_file.h
src/libserver/cfg_rcl.c
src/libserver/cfg_utils.c
src/lua/lua_classifier.c
src/lua/lua_config.c
src/lua/lua_task.c

index 7338b90688b4a651fbd3adf0c84aa836edfc41fe..23e785443e6eeaae7fcb2db45dbadeb1827ec866 100644 (file)
@@ -148,11 +148,12 @@ struct rspamd_classifier_config {
        GList *statfiles;                               /**< statfiles list                                     */
        GHashTable *labels;                             /**< statfiles with labels                                                              */
        gchar *metric;                                  /**< metric of this classifier                          */
-       struct classifier *classifier;                  /**< classifier interface                               */
-       struct tokenizer *tokenizer;                    /**< tokenizer used for classifier                                              */
+       gchar *classifier;                                      /**< classifier interface                               */
+       gchar *tokenizer;                                       /**< tokenizer used for classifier                                              */
        ucl_object_t *opts;                             /**< other options                                      */
        GList *pre_callbacks;                           /**< list of callbacks that are called before classification */
        GList *post_callbacks;                          /**< list of callbacks that are called after classification */
+       gchar *name;                                                                    /**< unique name of classifier                                                  */
 };
 
 struct rspamd_worker_bind_conf {
index 97481e6b2c4e3faff8416666898943eeddc1c43f..60ec7f641f796fbe355e9d72534129972ffb10bc 100644 (file)
@@ -28,8 +28,6 @@
 #include "cfg_file.h"
 #include "lua/lua_common.h"
 #include "expressions.h"
-#include "classifiers.h"
-#include "tokenizers.h"
 
 
 struct rspamd_rcl_default_handler_data {
@@ -933,70 +931,44 @@ rspamd_rcl_classifier_handler (struct rspamd_config *cfg,
        const ucl_object_t *val, *cur;
        ucl_object_iter_t it = NULL;
        const gchar *key, *type;
-       struct rspamd_classifier_config *ccf, *found = NULL;
+       struct rspamd_classifier_config *ccf;
        gboolean res = TRUE;
        struct rspamd_rcl_section *stat_section;
-       GList *cur_cl;
 
-       val = ucl_object_find_key (obj, "type");
-       if (val == NULL || !ucl_object_tostring_safe (val, &type)) {
-               g_set_error (err,
-                       CFG_RCL_ERROR,
-                       EINVAL,
-                       "classifier should have type defined");
-               return FALSE;
-       }
+       ccf = rspamd_config_new_classifier (cfg, NULL);
 
-       cur_cl = cfg->classifiers;
-       while (cur_cl != NULL) {
-               ccf = cur_cl->data;
-               if (g_ascii_strcasecmp (ccf->classifier->name, type) == 0) {
-                       found = ccf;
-                       break;
+       if (rspamd_rcl_section_parse_defaults (section, cfg, obj, ccf, err)) {
+
+               HASH_FIND_STR (section->subsections, "statfile", stat_section);
+
+               if (ccf->classifier == NULL) {
+                       ccf->classifier = "bayes";
                }
-               cur_cl = g_list_next (cur_cl);
-       }
 
-       if (found == NULL) {
-               ccf = rspamd_config_new_classifier (cfg, NULL);
-               ccf->classifier = rspamd_stat_get_classifier (type);
-       }
-       else {
-               ccf = found;
-       }
-
-       HASH_FIND_STR (section->subsections, "statfile", stat_section);
-
-       while ((val = ucl_iterate_object (obj, &it, true)) != NULL && res) {
-               key = ucl_object_key (val);
-               if (key != NULL) {
-                       if (g_ascii_strcasecmp (key, "statfile") == 0) {
-                               LL_FOREACH (val, cur)
-                               {
-                                       res = rspamd_rcl_statfile_handler (cfg,
-                                                       cur,
-                                                       ccf,
-                                                       stat_section,
-                                                       err);
-                                       if (!res) {
-                                               return FALSE;
+               if (ccf->name == NULL) {
+                       ccf->name = ccf->classifier;
+               }
+
+               while ((val = ucl_iterate_object (obj, &it, true)) != NULL && res) {
+                       key = ucl_object_key (val);
+                       if (key != NULL) {
+                               if (g_ascii_strcasecmp (key, "statfile") == 0) {
+                                       LL_FOREACH (val, cur) {
+                                               res = rspamd_rcl_statfile_handler (cfg,
+                                                               cur,
+                                                               ccf,
+                                                               stat_section,
+                                                               err);
+                                               if (!res) {
+                                                       return FALSE;
+                                               }
                                        }
                                }
                        }
-                       else if (g_ascii_strcasecmp (key,
-                               "type") == 0 && val->type == UCL_STRING) {
-                               continue;
-                       }
-                       else if (g_ascii_strcasecmp (key,
-                               "tokenizer") == 0 && val->type == UCL_STRING) {
-                               ccf->tokenizer = rspamd_stat_get_tokenizer (ucl_object_tostring (val));
-                       }
                }
        }
 
-       if (found == NULL) {
-               cfg->classifiers = g_list_prepend (cfg->classifiers, ccf);
-       }
+       cfg->classifiers = g_list_prepend (cfg->classifiers, ccf);
 
 
        return res;
@@ -1368,12 +1340,37 @@ rspamd_rcl_config_init (void)
                        UCL_OBJECT,
                        FALSE,
                        TRUE);
+       rspamd_rcl_add_default_handler (sub,
+               "name",
+               rspamd_rcl_parse_struct_string,
+               G_STRUCT_OFFSET (struct rspamd_classifier_config, name),
+               0);
+       rspamd_rcl_add_default_handler (sub,
+               "classifier",
+               rspamd_rcl_parse_struct_string,
+               G_STRUCT_OFFSET (struct rspamd_classifier_config, classifier),
+               0);
+       /* type is an alias for classifier now */
+       rspamd_rcl_add_default_handler (sub,
+               "type",
+               rspamd_rcl_parse_struct_string,
+               G_STRUCT_OFFSET (struct rspamd_classifier_config, classifier),
+               0);
+       rspamd_rcl_add_default_handler (sub,
+               "tokenizer",
+               rspamd_rcl_parse_struct_string,
+               G_STRUCT_OFFSET (struct rspamd_classifier_config, tokenizer),
+               0);
+
+       /*
+        * Statfile defaults
+        */
        ssub = rspamd_rcl_add_section (&sub->subsections,
-                       "statfile",
-                       rspamd_rcl_statfile_handler,
-                       UCL_OBJECT,
-                       TRUE,
-                       TRUE);
+               "statfile",
+               rspamd_rcl_statfile_handler,
+               UCL_OBJECT,
+               TRUE,
+               TRUE);
        rspamd_rcl_add_default_handler (ssub,
                "symbol",
                rspamd_rcl_parse_struct_string,
index 3a0186c89db96c12a9ae8edef3b86f0193df313b..fef983758232a18421ecd180488937face4627e2 100644 (file)
@@ -29,7 +29,6 @@
 #include "main.h"
 #include "uthash_strcase.h"
 #include "filter.h"
-#include "classifiers.h"
 #include "lua/lua_common.h"
 #include "kvstorage_config.h"
 #include "map.h"
@@ -681,7 +680,7 @@ rspamd_config_find_classifier (struct rspamd_config *cfg, const gchar *name)
        while (cur) {
                cf = cur->data;
 
-               if (g_ascii_strcasecmp (cf->classifier->name, name) == 0) {
+               if (g_ascii_strcasecmp (cf->name, name) == 0) {
                        return cf;
                }
 
index 7adc473ba3cc56343247c35af7c10ff43ffb7bec..c338fb2947d4d1d6c0b3dbd7a1b8b1653b301339 100644 (file)
@@ -25,7 +25,6 @@
 
 #include "lua_common.h"
 #include "cfg_file.h"
-#include "classifiers.h"
 
 /* Classifier methods */
 LUA_FUNCTION_DEF (classifier, register_pre_callback);
@@ -147,7 +146,7 @@ rspamd_lua_call_cls_pre_callbacks (struct rspamd_classifier_config *ccf,
                /* Check function from global table 'classifiers' */
                lua_getglobal (L, "classifiers");
                if (lua_istable (L, -1)) {
-                       lua_pushstring (L, ccf->classifier->name);
+                       lua_pushstring (L, ccf->name);
                        lua_gettable (L, -2);
                        /* Function is now on top */
                        if (lua_isfunction (L, -1)) {
index c1eec7655bf70a8cf883f103de2bd247e601b727..8e166ac32e316fb2857df44f9866c89b402958fb 100644 (file)
@@ -29,7 +29,6 @@
 #include "message.h"
 #include "radix.h"
 #include "trie.h"
-#include "classifiers.h"
 
 /***
  * This module is used to configure rspamd and is normally available as global
@@ -450,7 +449,7 @@ lua_config_get_classifier (lua_State * L)
                cur = g_list_first (cfg->classifiers);
                while (cur) {
                        clc = cur->data;
-                       if (g_ascii_strcasecmp (clc->classifier->name, name) == 0) {
+                       if (g_ascii_strcasecmp (clc->name, name) == 0) {
                                pclc = &clc;
                                break;
                        }
index 86b19827f3db4a9351f3dd832444f04f39d25a43..98aba7f1535002381a225b090778c9c0e21ed784 100644 (file)
@@ -32,8 +32,6 @@
 #include "util.h"
 #include "images.h"
 #include "cfg_file.h"
-#include "tokenizers.h"
-#include "classifiers.h"
 #include "diff.h"
 
 /***