]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Allow augmentations set in Lua API
authorVsevolod Stakhov <vsevolod@rspamd.com>
Sat, 14 May 2022 12:20:44 +0000 (13:20 +0100)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Sat, 14 May 2022 12:20:44 +0000 (13:20 +0100)
src/libserver/rspamd_symcache.h
src/libserver/symcache/symcache_c.cxx
src/libserver/symcache/symcache_impl.cxx
src/libserver/symcache/symcache_internal.hxx
src/libserver/symcache/symcache_item.cxx
src/lua/lua_config.c

index 99158fbc3ef1c6d613885f01324da75edfe29039..95ca614af8b2458349b07e1a75bbfa9642f69f56 100644 (file)
@@ -126,6 +126,16 @@ gint rspamd_symcache_add_symbol (struct rspamd_symcache *cache,
                                                                 enum rspamd_symbol_type type,
                                                                 gint parent);
 
+/**
+ * Adds augmentation to the symbol
+ * @param cache
+ * @param sym_id
+ * @param augmentation
+ * @return
+ */
+bool rspamd_symcache_add_symbol_augmentation(struct rspamd_symcache *cache,
+               int sym_id, const char *augmentation);
+
 /**
  * Add callback to be executed whenever symbol has peak value
  * @param cache
index fafc89d1943e5d8dd60c28b23e786eb58f1758d5..4d1f28800f7f612ff91a761af50d7d7382ffbe1c 100644 (file)
@@ -84,6 +84,29 @@ rspamd_symcache_add_symbol(struct rspamd_symcache *cache,
        }
 }
 
+bool
+rspamd_symcache_add_symbol_augmentation(struct rspamd_symcache *cache,
+                                                                                        int sym_id, const char *augmentation)
+{
+       auto *real_cache = C_API_SYMCACHE(cache);
+       auto log_tag = [&]() { return real_cache->log_tag(); };
+
+       if (augmentation == nullptr) {
+               msg_err_cache("null augmentation is not allowed for item %d", sym_id);
+               return false;
+       }
+
+
+       auto *item = real_cache->get_item_by_id_mut(sym_id, false);
+
+       if (item == nullptr) {
+               msg_err_cache("item %d is not found", sym_id);
+               return false;
+       }
+
+       return item->add_augmentation(*real_cache, augmentation);
+}
+
 void
 rspamd_symcache_set_peak_callback(struct rspamd_symcache *cache, gint cbref)
 {
index e557f62124b7a48264ed287a6085503cfd5aafd4..fb3d2d965ce895ecc6773d86ca262491dcc41148 100644 (file)
@@ -369,6 +369,29 @@ auto symcache::get_item_by_id(int id, bool resolve_parent) const -> const cache_
        return ret.get();
 }
 
+auto symcache::get_item_by_id_mut(int id, bool resolve_parent) const -> cache_item *
+{
+       if (id < 0 || id >= items_by_id.size()) {
+               msg_err_cache("internal error: requested item with id %d, when we have just %d items in the cache",
+                               id, (int) items_by_id.size());
+               return nullptr;
+       }
+
+       auto &ret = items_by_id[id];
+
+       if (!ret) {
+               msg_err_cache("internal error: requested item with id %d but it is empty; qed",
+                               id);
+               return nullptr;
+       }
+
+       if (resolve_parent && ret->is_virtual()) {
+               return (cache_item *)ret->get_parent(*this);
+       }
+
+       return ret.get();
+}
+
 auto symcache::get_item_by_name(std::string_view name, bool resolve_parent) const -> const cache_item *
 {
        auto it = items_by_symbol.find(name);
index 6a96eb5474327f61fcc425bf5033777cbf55829c..9bce37532eb28677936bc6ef0697c657e56719fe 100644 (file)
@@ -223,6 +223,7 @@ public:
         * @return
         */
        auto get_item_by_id(int id, bool resolve_parent) const -> const cache_item *;
+       auto get_item_by_id_mut(int id, bool resolve_parent) const -> cache_item *;
        /**
         * Get an item by it's name
         * @param name
index 091e6cbf9967bf267aa1c0534aa7644fcdd89a1d..1bac084958700d6f38d4c530de7f2e1e1764412f 100644 (file)
@@ -370,7 +370,12 @@ cache_item::add_augmentation(const symcache &cache, std::string_view augmentatio
 
        augmentations.insert(std::string(augmentation));
 
-       return known_augmentations.contains(augmentation);
+       auto ret = known_augmentations.contains(augmentation);
+
+       msg_debug_cache("added %s augmentation %s for symbol %s",
+                       ret ? "known" : "unknown", augmentation.data(), symbol.data());
+
+       return ret;
 }
 
 auto
index 747f7b51e4785cbd4bc6a0e4059fa4cc042a59a1..b2333eab1e0a9a6c6213cbac058daee48390634f 100644 (file)
@@ -2050,31 +2050,46 @@ lua_config_register_symbol (lua_State * L)
                                allowed_ids, forbidden_ids,
                                FALSE);
 
-               if (!isnan (score) || group) {
-                       if (one_shot) {
-                               nshots = 1;
-                       }
+               if (ret != -1) {
+                       if (!isnan(score) || group) {
+                               if (one_shot) {
+                                       nshots = 1;
+                               }
 
-                       rspamd_config_add_symbol (cfg, name,
-                                       score, description, group, flags,
-                                       0, nshots);
+                               rspamd_config_add_symbol(cfg, name,
+                                               score, description, group, flags,
+                                               0, nshots);
 
-                       lua_pushstring (L, "groups");
-                       lua_gettable (L, 2);
+                               lua_pushstring(L, "groups");
+                               lua_gettable(L, 2);
 
-                       if (lua_istable (L, -1)) {
-                               for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) {
-                                       if (lua_isstring (L, -1)) {
-                                               rspamd_config_add_symbol_group (cfg, name,
-                                                               lua_tostring (L, -1));
-                                       }
-                                       else {
-                                               return luaL_error (L, "invalid groups element");
+                               if (lua_istable (L, -1)) {
+                                       for (lua_pushnil(L); lua_next(L, -2); lua_pop (L, 1)) {
+                                               if (lua_isstring(L, -1)) {
+                                                       rspamd_config_add_symbol_group(cfg, name,
+                                                                       lua_tostring (L, -1));
+                                               }
+                                               else {
+                                                       return luaL_error(L, "invalid groups element");
+                                               }
                                        }
                                }
+
+                               lua_pop (L, 1);
+                       }
+
+                       lua_pushstring (L, "augmentations");
+                       lua_gettable (L, 2);
+
+                       if (lua_type (L, -1) == LUA_TTABLE) {
+
+                               for (lua_pushnil (L); lua_next (L, 2); lua_pop (L, 1)) {
+                                       rspamd_symcache_add_symbol_augmentation(cfg->cache, ret,
+                                                       lua_tostring(L, -1));
+                               }
                        }
 
-                       lua_pop (L, 1);
+                       lua_pop (L, 1); /* Table itself */
                }
        }
        else {
@@ -2751,10 +2766,25 @@ lua_config_newindex (lua_State *L)
                                        g_assert (name != NULL);
                                        rspamd_symcache_add_condition_delayed (cfg->cache,
                                                        name, L, condref);
+
                                }
                                else {
                                        lua_pop (L, 1);
                                }
+
+                               /* Check for augmentations */
+                               lua_pushstring (L, "augmentations");
+                               lua_gettable (L, -2);
+
+                               if (lua_type (L, -1) == LUA_TTABLE) {
+
+                                       for (lua_pushnil(L); lua_next(L, 2); lua_pop (L, 1)) {
+                                               rspamd_symcache_add_symbol_augmentation(cfg->cache, id,
+                                                               lua_tostring(L, -1));
+                                       }
+                               }
+
+                               lua_pop (L, 1);
                        }
 
                        /*