From b4679a32d4a586200399d941fa1acc982deeb98c Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Thu, 15 Jul 2021 19:49:58 +0100 Subject: [PATCH] [Project] Fix lua bindings --- src/libserver/cfg_rcl.c | 3 +- src/libserver/composites/composites.h | 8 +- .../composites/composites_internal.hxx | 1 + .../composites/composites_manager.cxx | 73 ++++++++++++++----- src/lua/lua_config.c | 37 ++-------- 5 files changed, 71 insertions(+), 51 deletions(-) diff --git a/src/libserver/cfg_rcl.c b/src/libserver/cfg_rcl.c index a8f3c57a6..b19a1dedc 100644 --- a/src/libserver/cfg_rcl.c +++ b/src/libserver/cfg_rcl.c @@ -1382,7 +1382,8 @@ rspamd_rcl_composite_handler (rspamd_mempool_t *pool, composite_name = key; - if ((composite = rspamd_composites_manager_add_from_ucl(cfg->composites_manager, obj)) != NULL) { + if ((composite = rspamd_composites_manager_add_from_ucl(cfg->composites_manager, + composite_name, obj)) != NULL) { rspamd_symcache_add_symbol (cfg->cache, composite_name, 0, NULL, composite, SYMBOL_TYPE_COMPOSITE, -1); } diff --git a/src/libserver/composites/composites.h b/src/libserver/composites/composites.h index 2db020e74..6ccfbd58f 100644 --- a/src/libserver/composites/composites.h +++ b/src/libserver/composites/composites.h @@ -47,7 +47,13 @@ gsize rspamd_composites_manager_nelts(void *); * Adds a composite from config * @return */ -void* rspamd_composites_manager_add_from_ucl(void *, const ucl_object_t *); +void* rspamd_composites_manager_add_from_ucl(void *, const char *, const ucl_object_t *); + +/** + * Adds a composite from config + * @return + */ +void* rspamd_composites_manager_add_from_string(void *, const char *, const char *); #ifdef __cplusplus } diff --git a/src/libserver/composites/composites_internal.hxx b/src/libserver/composites/composites_internal.hxx index d06f592db..12fa3ecb2 100644 --- a/src/libserver/composites/composites_internal.hxx +++ b/src/libserver/composites/composites_internal.hxx @@ -71,6 +71,7 @@ public: } auto add_composite(std::string_view, const ucl_object_t *) -> rspamd_composite *; + auto add_composite(std::string_view name, std::string_view expression) -> rspamd_composite *; private: ~composites_manager() = default; static void composites_manager_dtor(void *ptr) { diff --git a/src/libserver/composites/composites_manager.cxx b/src/libserver/composites/composites_manager.cxx index 0cffcdcf3..c2e719c89 100644 --- a/src/libserver/composites/composites_manager.cxx +++ b/src/libserver/composites/composites_manager.cxx @@ -30,13 +30,13 @@ composite_policy_from_str(const std::string_view &inp) -> enum rspamd_composite_ { const static robin_hood::unordered_flat_map names{ - {"remove", rspamd_composite_policy::RSPAMD_COMPOSITE_POLICY_REMOVE_ALL}, - {"remove_all", rspamd_composite_policy::RSPAMD_COMPOSITE_POLICY_REMOVE_ALL}, - {"default", rspamd_composite_policy::RSPAMD_COMPOSITE_POLICY_REMOVE_ALL}, + {"remove", rspamd_composite_policy::RSPAMD_COMPOSITE_POLICY_REMOVE_ALL}, + {"remove_all", rspamd_composite_policy::RSPAMD_COMPOSITE_POLICY_REMOVE_ALL}, + {"default", rspamd_composite_policy::RSPAMD_COMPOSITE_POLICY_REMOVE_ALL}, {"remove_symbol", rspamd_composite_policy::RSPAMD_COMPOSITE_POLICY_REMOVE_SYMBOL}, {"remove_weight", rspamd_composite_policy::RSPAMD_COMPOSITE_POLICY_REMOVE_WEIGHT}, - {"leave", rspamd_composite_policy::RSPAMD_COMPOSITE_POLICY_LEAVE}, - {"remove_none", rspamd_composite_policy::RSPAMD_COMPOSITE_POLICY_LEAVE}, + {"leave", rspamd_composite_policy::RSPAMD_COMPOSITE_POLICY_LEAVE}, + {"remove_none", rspamd_composite_policy::RSPAMD_COMPOSITE_POLICY_LEAVE}, }; auto found = names.find(inp); @@ -51,8 +51,8 @@ auto composites_manager::add_composite(std::string_view composite_name, const ucl_object_t *obj) -> rspamd_composite * { - const auto *val = ucl_object_lookup (obj, "enabled"); - if (val != nullptr && !ucl_object_toboolean (val)) { + const auto *val = ucl_object_lookup(obj, "enabled"); + if (val != nullptr && !ucl_object_toboolean(val)) { msg_info_config ("composite %s is disabled", composite_name.data()); return nullptr; } @@ -62,9 +62,9 @@ composites_manager::add_composite(std::string_view composite_name, const ucl_obj } const char *composite_expression = nullptr; - val = ucl_object_lookup (obj, "expression"); + val = ucl_object_lookup(obj, "expression"); - if (val == NULL || !ucl_object_tostring_safe (val, &composite_expression)) { + if (val == NULL || !ucl_object_tostring_safe(val, &composite_expression)) { msg_err_config ("composite must have an expression defined in %s", composite_name.data()); return nullptr; @@ -92,20 +92,20 @@ composites_manager::add_composite(std::string_view composite_name, const ucl_obj composite->sym = composite_name; double score; - val = ucl_object_lookup (obj, "score"); - if (val != nullptr && ucl_object_todouble_safe (val, &score)) { + val = ucl_object_lookup(obj, "score"); + if (val != nullptr && ucl_object_todouble_safe(val, &score)) { /* Also set score in the metric */ const auto *group = "composite"; - val = ucl_object_lookup (obj, "group"); + val = ucl_object_lookup(obj, "group"); if (val != nullptr) { - group = ucl_object_tostring (val); + group = ucl_object_tostring(val); } const auto *description = composite_expression; - val = ucl_object_lookup (obj, "description"); + val = ucl_object_lookup(obj, "description"); if (val != nullptr) { - description = ucl_object_tostring (val); + description = ucl_object_tostring(val); } else { description = composite_expression; @@ -114,13 +114,13 @@ composites_manager::add_composite(std::string_view composite_name, const ucl_obj rspamd_config_add_symbol(cfg, composite_name.data(), score, description, group, 0, - ucl_object_get_priority (obj), /* No +1 as it is default... */ + ucl_object_get_priority(obj), /* No +1 as it is default... */ 1); - const auto *elt = ucl_object_lookup (obj, "groups"); + const auto *elt = ucl_object_lookup(obj, "groups"); if (elt) { const ucl_object_t *cur_gr; - auto *gr_it = ucl_object_iterate_new (elt); + auto *gr_it = ucl_object_iterate_new(elt); while ((cur_gr = ucl_object_iterate_safe(gr_it, true)) != nullptr) { rspamd_config_add_symbol_group(cfg, composite_name.data(), @@ -146,6 +146,37 @@ composites_manager::add_composite(std::string_view composite_name, const ucl_obj return composite.get(); } +auto +composites_manager::add_composite(std::string_view composite_name, + std::string_view composite_expression) -> rspamd_composite * +{ + GError *err = nullptr; + rspamd_expression *expr = nullptr; + + if (!rspamd_parse_expression(composite_expression.data(), + composite_expression.size(), &composite_expr_subr, + nullptr, cfg->cfg_pool, &err, &expr)) { + msg_err_config ("cannot parse composite expression for %s: %e", + composite_name.data(), err); + + if (err) { + g_error_free(err); + } + + return nullptr; + } + + auto &composite = all_composites.emplace_back(std::make_shared()); + composite->expr = expr; + composite->id = all_composites.size(); + composite->str_expr = composite_expression; + composite->sym = composite_name; + + composites[std::string(composite_name)] = composite; + + return composite.get(); +} + } @@ -169,3 +200,9 @@ rspamd_composites_manager_add_from_ucl(void *cm, const char *sym, const ucl_obje { return reinterpret_cast(COMPOSITE_MANAGER_FROM_PTR(cm)->add_composite(sym, obj)); } + +void* +rspamd_composites_manager_add_from_string(void *cm, const char *sym, const char *expr) +{ + return reinterpret_cast(COMPOSITE_MANAGER_FROM_PTR(cm)->add_composite(sym, expr)); +} diff --git a/src/lua/lua_config.c b/src/lua/lua_config.c index c2f5efb42..b0e2df672 100644 --- a/src/lua/lua_config.c +++ b/src/lua/lua_config.c @@ -2600,47 +2600,22 @@ lua_config_add_composite (lua_State * L) { LUA_TRACE_POINT; struct rspamd_config *cfg = lua_check_config (L, 1); - struct rspamd_expression *expr; gchar *name; const gchar *expr_str; struct rspamd_composite *composite; - gboolean ret = FALSE, new = TRUE; - GError *err = NULL; + gboolean ret = FALSE; if (cfg) { name = rspamd_mempool_strdup (cfg->cfg_pool, luaL_checkstring (L, 2)); expr_str = luaL_checkstring (L, 3); if (name && expr_str) { - if (!rspamd_parse_expression (expr_str, 0, &composite_expr_subr, - NULL, cfg->cfg_pool, &err, &expr)) { - msg_err_config ("cannot parse composite expression %s: %e", - expr_str, - err); - g_error_free (err); - } - else { - if (g_hash_table_lookup (cfg->composite_symbols, name) != NULL) { - msg_warn_config ("composite %s is redefined", name); - new = FALSE; - } - - composite = rspamd_mempool_alloc0 (cfg->cfg_pool, - sizeof (struct rspamd_composite)); - composite->expr = expr; - composite->id = g_hash_table_size (cfg->composite_symbols); - composite->str_expr = rspamd_mempool_strdup (cfg->cfg_pool, - expr_str); - composite->sym = name; - g_hash_table_insert (cfg->composite_symbols, - (gpointer)name, - composite); - - if (new) { - rspamd_symcache_add_symbol (cfg->cache, name, - 0, NULL, composite, SYMBOL_TYPE_COMPOSITE, -1); - } + composite = rspamd_composites_manager_add_from_string(cfg->composites_manager, + name, expr_str); + if (composite) { + rspamd_symcache_add_symbol (cfg->cache, name, + 0, NULL, composite, SYMBOL_TYPE_COMPOSITE, -1); ret = TRUE; } } -- 2.39.5