diff options
Diffstat (limited to 'src/libstat/stat_config.c')
-rw-r--r-- | src/libstat/stat_config.c | 155 |
1 files changed, 121 insertions, 34 deletions
diff --git a/src/libstat/stat_config.c b/src/libstat/stat_config.c index dbfe16c27..1f16a98de 100644 --- a/src/libstat/stat_config.c +++ b/src/libstat/stat_config.c @@ -44,18 +44,12 @@ static struct rspamd_stat_tokenizer stat_tokenizers[] = { { .name = "osb-text", .get_config = rspamd_tokenizer_osb_get_config, - .compatible_config = rspamd_tokenizer_osb_compatible_config, .tokenize_func = rspamd_tokenizer_osb, - .load_config = rspamd_tokenizer_osb_load_config, - .is_compat = rspamd_tokenizer_osb_is_compat }, { .name = "osb", .get_config = rspamd_tokenizer_osb_get_config, - .compatible_config = rspamd_tokenizer_osb_compatible_config, .tokenize_func = rspamd_tokenizer_osb, - .load_config = rspamd_tokenizer_osb_load_config, - .is_compat = rspamd_tokenizer_osb_is_compat }, }; @@ -63,9 +57,9 @@ static struct rspamd_stat_tokenizer stat_tokenizers[] = { .name = #nam, \ .init = rspamd_##eltn##_init, \ .runtime = rspamd_##eltn##_runtime, \ - .process_token = rspamd_##eltn##_process_token, \ + .process_tokens = rspamd_##eltn##_process_tokens, \ .finalize_process = rspamd_##eltn##_finalize_process, \ - .learn_token = rspamd_##eltn##_learn_token, \ + .learn_tokens = rspamd_##eltn##_learn_tokens, \ .finalize_learn = rspamd_##eltn##_finalize_learn, \ .total_learns = rspamd_##eltn##_total_learns, \ .inc_learns = rspamd_##eltn##_inc_learns, \ @@ -92,32 +86,106 @@ static struct rspamd_stat_cache stat_caches[] = { void rspamd_stat_init (struct rspamd_config *cfg) { - guint i; + GList *cur, *curst; + struct rspamd_classifier_config *clf; + struct rspamd_statfile_config *stf; + struct rspamd_stat_backend *bk; + struct rspamd_statfile *st; + struct rspamd_classifier *cl; + const ucl_object_t *cache_obj = NULL, *cache_name_obj; + const gchar *cache_name = NULL; if (stat_ctx == NULL) { stat_ctx = g_slice_alloc0 (sizeof (*stat_ctx)); } - stat_ctx->backends = stat_backends; + stat_ctx->backends_subrs = stat_backends; stat_ctx->backends_count = G_N_ELEMENTS (stat_backends); - stat_ctx->classifiers = stat_classifiers; + stat_ctx->classifiers_subrs = stat_classifiers; stat_ctx->classifiers_count = G_N_ELEMENTS (stat_classifiers); - stat_ctx->tokenizers = stat_tokenizers; + stat_ctx->tokenizers_subrs = stat_tokenizers; stat_ctx->tokenizers_count = G_N_ELEMENTS (stat_tokenizers); - stat_ctx->caches = stat_caches; + stat_ctx->caches_subrs = stat_caches; stat_ctx->caches_count = G_N_ELEMENTS (stat_caches); stat_ctx->cfg = cfg; + stat_ctx->statfiles = g_ptr_array_new (); + stat_ctx->classifiers = g_ptr_array_new (); + REF_RETAIN (stat_ctx->cfg); - /* Init backends */ - for (i = 0; i < stat_ctx->backends_count; i ++) { - stat_ctx->backends[i].ctx = stat_ctx->backends[i].init (stat_ctx, cfg); - msg_debug_config ("added backend %s", stat_ctx->backends[i].name); - } + /* Create statfiles from the classifiers */ + cur = cfg->classifiers; + + while (cur) { + clf = cur->data; + bk = rspamd_stat_get_backend (clf->backend); + g_assert (bk != NULL); + + /* XXX: + * Here we get the first classifier tokenizer config as the only one + * We NO LONGER support multiple tokenizers per rspamd instance + */ + if (stat_ctx->tkcf == NULL) { + stat_ctx->tokenizer = rspamd_stat_get_tokenizer (clf->tokenizer->name); + g_assert (stat_ctx->tokenizer != NULL); + stat_ctx->tkcf = stat_ctx->tokenizer->get_config (cfg->cfg_pool, + clf->tokenizer, NULL); + } + + cl = g_slice_alloc0 (sizeof (*cl)); + cl->cfg = clf; + cl->ctx = stat_ctx; + cl->statfiles_ids = g_array_new (FALSE, FALSE, sizeof (gint)); + cl->subrs = rspamd_stat_get_classifier (clf->classifier); + g_assert (cl->subrs != NULL); + cl->subrs->init_func (cfg->cfg_pool, cl); + + /* Init classifier cache */ + if (clf->opts) { + cache_obj = ucl_object_find_key (clf->opts, "cache"); + + if (cache_obj) { + cache_name_obj = ucl_object_find_key (cache_obj, "name"); + } + + if (cache_name_obj) { + cache_name = ucl_object_tostring (cache_name_obj); + } + } + + cl->cache = rspamd_stat_get_cache (cache_name); + g_assert (cl->cache != NULL); + cl->cachecf = cl->cache->init (stat_ctx, cfg, cache_obj); + + curst = clf->statfiles; + + while (curst) { + stf = curst->data; + st = g_slice_alloc0 (sizeof (*st)); + st->classifier = cl; + st->stcf = stf; + st->backend = bk; + st->bkcf = bk->init (stat_ctx, cfg, st); + msg_debug_config ("added backend %s for symbol %s", + bk->name, stf->symbol); + + if (st->bkcf == NULL) { + msg_err_config ("cannot init backend %s for statfile %s", + clf->backend, stf->symbol); + + g_slice_free1 (sizeof (*st), st); + } + else { + st->id = stat_ctx->statfiles->len; + g_ptr_array_add (stat_ctx->statfiles, st); + g_array_append_val (cl->statfiles_ids, st->id); + } + + curst = curst->next; + } + + g_ptr_array_add (stat_ctx->classifiers, cl); - /* Init caches */ - for (i = 0; i < stat_ctx->caches_count; i ++) { - stat_ctx->caches[i].ctx = stat_ctx->caches[i].init (stat_ctx, cfg); - msg_debug_config ("added cache %s", stat_ctx->caches[i].name); + cur = cur->next; } } @@ -129,12 +197,9 @@ rspamd_stat_close (void) g_assert (stat_ctx != NULL); - for (i = 0; i < stat_ctx->backends_count; i ++) { - if (stat_ctx->backends[i].close != NULL) { - stat_ctx->backends[i].close (stat_ctx->backends[i].ctx); - msg_debug_config ("closed backend %s", stat_ctx->backends[i].name); - } - } + /* TODO: add cleanup routine */ + + REF_RELEASE (stat_ctx->cfg); } struct rspamd_stat_ctx * @@ -148,9 +213,13 @@ rspamd_stat_get_classifier (const gchar *name) { guint i; + if (name == NULL || name[0] == '\0') { + name = RSPAMD_DEFAULT_CLASSIFIER; + } + for (i = 0; i < stat_ctx->classifiers_count; i ++) { - if (strcmp (name, stat_ctx->classifiers[i].name) == 0) { - return &stat_ctx->classifiers[i]; + if (strcmp (name, stat_ctx->classifiers_subrs[i].name) == 0) { + return &stat_ctx->classifiers_subrs[i]; } } @@ -167,8 +236,8 @@ rspamd_stat_get_backend (const gchar *name) } for (i = 0; i < stat_ctx->backends_count; i ++) { - if (strcmp (name, stat_ctx->backends[i].name) == 0) { - return &stat_ctx->backends[i]; + if (strcmp (name, stat_ctx->backends_subrs[i].name) == 0) { + return &stat_ctx->backends_subrs[i]; } } @@ -185,8 +254,26 @@ rspamd_stat_get_tokenizer (const gchar *name) } for (i = 0; i < stat_ctx->tokenizers_count; i ++) { - if (strcmp (name, stat_ctx->tokenizers[i].name) == 0) { - return &stat_ctx->tokenizers[i]; + if (strcmp (name, stat_ctx->tokenizers_subrs[i].name) == 0) { + return &stat_ctx->tokenizers_subrs[i]; + } + } + + return NULL; +} + +struct rspamd_stat_cache * +rspamd_stat_get_cache (const gchar *name) +{ + guint i; + + if (name == NULL || name[0] == '\0') { + name = RSPAMD_DEFAULT_CACHE; + } + + for (i = 0; i < stat_ctx->caches_count; i++) { + if (strcmp (name, stat_ctx->caches_subrs[i].name) == 0) { + return &stat_ctx->caches_subrs[i]; } } |