diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-04-14 17:14:50 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-04-14 17:14:50 +0100 |
commit | 2e93237cea96bef04a4373ea1a6ca2b61fa872da (patch) | |
tree | 53258d6f62a142ad8e610b9e724efc7c6066d8d8 | |
parent | 6b2ec2d8f2a904f748c47decb4c9808045637551 (diff) | |
download | rspamd-2e93237cea96bef04a4373ea1a6ca2b61fa872da.tar.gz rspamd-2e93237cea96bef04a4373ea1a6ca2b61fa872da.zip |
Refactor symbols parsing.
-rw-r--r-- | src/libserver/cfg_rcl.c | 98 |
1 files changed, 58 insertions, 40 deletions
diff --git a/src/libserver/cfg_rcl.c b/src/libserver/cfg_rcl.c index 6fa4c4941..7235f1825 100644 --- a/src/libserver/cfg_rcl.c +++ b/src/libserver/cfg_rcl.c @@ -368,6 +368,60 @@ rspamd_rcl_insert_symbol (struct rspamd_config *cfg, struct metric *metric, } static gboolean +rspamd_rcl_symbols_handler (rspamd_mempool_t *pool, const ucl_object_t *obj, + struct rspamd_config *cfg, struct metric *metric, + const gchar *group, gboolean new, GError **err) +{ + const ucl_object_t *val, *cur; + ucl_object_iter_t it = NULL; + + val = ucl_object_find_key (obj, "symbols"); + if (val != NULL) { + if (val->type != UCL_OBJECT) { + g_set_error (err, CFG_RCL_ERROR, EINVAL, + "symbols must be an object"); + return FALSE; + } + it = NULL; + while ((cur = ucl_iterate_object (val, &it, true)) != NULL) { + if (!rspamd_rcl_insert_symbol (cfg, metric, cur, NULL, FALSE, err)) { + return FALSE; + } + } + } + else { + /* Legacy variant */ + val = ucl_object_find_key (obj, "symbol"); + if (val != NULL) { + if (val->type != UCL_OBJECT) { + g_set_error (err, + CFG_RCL_ERROR, + EINVAL, + "symbols must be an object"); + return FALSE; + } + + it = NULL; + while ((cur = ucl_iterate_object (val, &it, false)) != NULL) { + if (!rspamd_rcl_insert_symbol (cfg, metric, cur, NULL, TRUE, err)) { + return FALSE; + } + } + } + else if (new) { + g_set_error (err, + CFG_RCL_ERROR, + EINVAL, + "metric %s has no symbols", + metric->name); + return FALSE; + } + } + + return TRUE; +} + +static gboolean rspamd_rcl_metric_handler (rspamd_mempool_t *pool, const ucl_object_t *obj, gpointer ud, struct rspamd_rcl_section *section, GError **err) { @@ -456,46 +510,6 @@ rspamd_rcl_metric_handler (rspamd_mempool_t *pool, const ucl_object_t *obj, } /* Handle symbols */ - val = ucl_object_find_key (obj, "symbols"); - if (val != NULL) { - if (val->type != UCL_OBJECT) { - g_set_error (err, CFG_RCL_ERROR, EINVAL, - "symbols must be an object"); - return FALSE; - } - it = NULL; - while ((cur = ucl_iterate_object (val, &it, true)) != NULL) { - if (!rspamd_rcl_insert_symbol (cfg, metric, cur, NULL, FALSE, err)) { - return FALSE; - } - } - } - else { - /* Legacy variant */ - val = ucl_object_find_key (obj, "symbol"); - if (val != NULL) { - if (val->type != UCL_OBJECT) { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "symbols must be an object"); - return FALSE; - } - LL_FOREACH (val, cur) { - if (!rspamd_rcl_insert_symbol (cfg, metric, cur, NULL, TRUE, err)) { - return FALSE; - } - } - } - else if (new) { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "metric %s has no symbols", - metric_name); - return FALSE; - } - } val = ucl_object_find_key (obj, "grow_factor"); if (val && ucl_object_todouble_safe (val, &grow_factor)) { @@ -514,6 +528,10 @@ rspamd_rcl_metric_handler (rspamd_mempool_t *pool, const ucl_object_t *obj, metric->accept_unknown_symbols = TRUE; } + if (!rspamd_rcl_symbols_handler (pool, obj, cfg, metric, NULL, new, err)) { + return FALSE; + } + /* Insert the resulting metric */ if (new) { g_hash_table_insert (cfg->metrics, (void *)metric->name, metric); |