]> source.dussan.org Git - rspamd.git/commitdiff
[Project] Fix lua bindings
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 15 Jul 2021 18:49:58 +0000 (19:49 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 15 Jul 2021 18:49:58 +0000 (19:49 +0100)
src/libserver/cfg_rcl.c
src/libserver/composites/composites.h
src/libserver/composites/composites_internal.hxx
src/libserver/composites/composites_manager.cxx
src/lua/lua_config.c

index a8f3c57a6a321a7e9e1b76d9cd9383080e635e11..b19a1dedc28057acb1b535cd1c9e20d4a25d6b4e 100644 (file)
@@ -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);
        }
index 2db020e7444509945ee45f317ae43a6d9a4b4e32..6ccfbd58f30b9f344384e0d8095a15582ea3f18d 100644 (file)
@@ -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
 }
index d06f592dbcd989054d0b428b548e611efa0a42d8..12fa3ecb287167d3362545f64b3f7ee5ea73c9e8 100644 (file)
@@ -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) {
index 0cffcdcf3272ec8caa3377c687a26d496d77f41c..c2e719c89c297e91c3c26414cd3f8fc561fda5b5 100644 (file)
@@ -30,13 +30,13 @@ composite_policy_from_str(const std::string_view &inp) -> enum rspamd_composite_
 {
        const static robin_hood::unordered_flat_map<std::string_view,
                        enum rspamd_composite_policy> 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<rspamd_composite>());
+       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<void *>(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<void *>(COMPOSITE_MANAGER_FROM_PTR(cm)->add_composite(sym, expr));
+}
index c2f5efb42e5f6defa16b59758dde21488ffe114b..b0e2df6720f708b2f8c4ebc28cbe24bc86e1c4e1 100644 (file)
@@ -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;
                        }
                }