From: Vsevolod Stakhov Date: Sat, 14 May 2022 12:20:44 +0000 (+0100) Subject: [Feature] Allow augmentations set in Lua API X-Git-Tag: 3.3~249 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=3134f3e191c9eda77a05e655be0ac54542746387;p=thirdparty%2Frspamd.git [Feature] Allow augmentations set in Lua API --- diff --git a/src/libserver/rspamd_symcache.h b/src/libserver/rspamd_symcache.h index 99158fbc3e..95ca614af8 100644 --- a/src/libserver/rspamd_symcache.h +++ b/src/libserver/rspamd_symcache.h @@ -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 diff --git a/src/libserver/symcache/symcache_c.cxx b/src/libserver/symcache/symcache_c.cxx index fafc89d194..4d1f28800f 100644 --- a/src/libserver/symcache/symcache_c.cxx +++ b/src/libserver/symcache/symcache_c.cxx @@ -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) { diff --git a/src/libserver/symcache/symcache_impl.cxx b/src/libserver/symcache/symcache_impl.cxx index e557f62124..fb3d2d965c 100644 --- a/src/libserver/symcache/symcache_impl.cxx +++ b/src/libserver/symcache/symcache_impl.cxx @@ -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); diff --git a/src/libserver/symcache/symcache_internal.hxx b/src/libserver/symcache/symcache_internal.hxx index 6a96eb5474..9bce37532e 100644 --- a/src/libserver/symcache/symcache_internal.hxx +++ b/src/libserver/symcache/symcache_internal.hxx @@ -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 diff --git a/src/libserver/symcache/symcache_item.cxx b/src/libserver/symcache/symcache_item.cxx index 091e6cbf99..1bac084958 100644 --- a/src/libserver/symcache/symcache_item.cxx +++ b/src/libserver/symcache/symcache_item.cxx @@ -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 diff --git a/src/lua/lua_config.c b/src/lua/lua_config.c index 747f7b51e4..b2333eab1e 100644 --- a/src/lua/lua_config.c +++ b/src/lua/lua_config.c @@ -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); } /*