]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Project] Few more methods
authorVsevolod Stakhov <vsevolod@rspamd.com>
Sun, 24 Apr 2022 11:42:13 +0000 (12:42 +0100)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Sun, 24 Apr 2022 11:42:13 +0000 (12:42 +0100)
src/libserver/symcache/symcache_c.cxx
src/libserver/symcache/symcache_item.cxx
src/libserver/symcache/symcache_item.hxx
src/libserver/symcache/symcache_runtime.cxx
src/libserver/symcache/symcache_runtime.hxx

index cb8a8af144c523b4004fb8f6af4b9e9ccd7f74ae..a559c3564c3ebfc4641eb00aa0e01b7ad08d1790 100644 (file)
@@ -165,15 +165,15 @@ rspamd_symcache_validate(struct rspamd_symcache *cache,
 }
 
 ucl_object_t *
-rspamd_symcache_counters (struct rspamd_symcache *cache)
+rspamd_symcache_counters(struct rspamd_symcache *cache)
 {
        auto *real_cache = C_API_SYMCACHE(cache);
        return real_cache->counters();
 }
 
 void *
-rspamd_symcache_start_refresh (struct rspamd_symcache *cache,
-                                                          struct ev_loop *ev_base, struct rspamd_worker *w)
+rspamd_symcache_start_refresh(struct rspamd_symcache *cache,
+                                                         struct ev_loop *ev_base, struct rspamd_worker *w)
 {
        auto *real_cache = C_API_SYMCACHE(cache);
        return new rspamd::symcache::cache_refresh_cbdata{real_cache, ev_base, w};
@@ -187,16 +187,16 @@ rspamd_symcache_inc_frequency(struct rspamd_symcache *_cache, struct rspamd_symc
 }
 
 void
-rspamd_symcache_add_delayed_dependency (struct rspamd_symcache *cache,
-                                                                               const gchar *from, const gchar *to)
+rspamd_symcache_add_delayed_dependency(struct rspamd_symcache *cache,
+                                                                          const gchar *from, const gchar *to)
 {
        auto *real_cache = C_API_SYMCACHE(cache);
        real_cache->add_delayed_dependency(from, to);
 }
 
 const gchar *
-rspamd_symcache_get_parent (struct rspamd_symcache *cache,
-                                                       const gchar *symbol)
+rspamd_symcache_get_parent(struct rspamd_symcache *cache,
+                                                  const gchar *symbol)
 {
        auto *real_cache = C_API_SYMCACHE(cache);
 
@@ -213,8 +213,8 @@ rspamd_symcache_get_parent (struct rspamd_symcache *cache,
        return nullptr;
 }
 
-const gchar*
-rspamd_symcache_item_name (struct rspamd_symcache_item *item)
+const gchar *
+rspamd_symcache_item_name(struct rspamd_symcache_item *item)
 {
        auto *real_item = C_API_SYMCACHE_ITEM(item);
        return real_item->get_name().c_str();
@@ -259,7 +259,7 @@ rspamd_symcache_get_symbol_details(struct rspamd_symcache *cache,
        auto *sym = real_cache->get_item_by_name(symbol, false);
 
        if (sym) {
-               ucl_object_insert_key (this_sym_ucl,
+               ucl_object_insert_key(this_sym_ucl,
                                ucl_object_fromstring(sym->get_type_str()),
                                "type", strlen("type"), false);
        }
@@ -267,13 +267,13 @@ rspamd_symcache_get_symbol_details(struct rspamd_symcache *cache,
 
 void
 rspamd_symcache_foreach(struct rspamd_symcache *cache,
-                                                         void (*func) (struct rspamd_symcache_item *item, gpointer /* userdata */),
-                                                         gpointer ud)
+                                               void (*func)(struct rspamd_symcache_item *item, gpointer /* userdata */),
+                                               gpointer ud)
 {
        auto *real_cache = C_API_SYMCACHE(cache);
 
-       real_cache->symbols_foreach([&](const rspamd::symcache::cache_itemitem) {
-               func((struct rspamd_symcache_item *)item, ud);
+       real_cache->symbols_foreach([&](const rspamd::symcache::cache_item *item) {
+               func((struct rspamd_symcache_item *) item, ud);
        });
 }
 
@@ -288,9 +288,9 @@ rspamd_symcache_disable_all_symbols(struct rspamd_task *task,
 }
 
 gboolean
-rspamd_symcache_disable_symbol (struct rspamd_task *task,
-                                                               struct rspamd_symcache *cache,
-                                                               const gchar *symbol)
+rspamd_symcache_disable_symbol(struct rspamd_task *task,
+                                                          struct rspamd_symcache *cache,
+                                                          const gchar *symbol)
 {
        auto *cache_runtime = C_API_SYMCACHE_RUNTIME(task->symcache_runtime);
        auto *real_cache = C_API_SYMCACHE(cache);
@@ -299,9 +299,9 @@ rspamd_symcache_disable_symbol (struct rspamd_task *task,
 }
 
 gboolean
-rspamd_symcache_enable_symbol (struct rspamd_task *task,
-                                                               struct rspamd_symcache *cache,
-                                                               const gchar *symbol)
+rspamd_symcache_enable_symbol(struct rspamd_task *task,
+                                                         struct rspamd_symcache *cache,
+                                                         const gchar *symbol)
 {
        auto *cache_runtime = C_API_SYMCACHE_RUNTIME(task->symcache_runtime);
        auto *real_cache = C_API_SYMCACHE(cache);
@@ -310,9 +310,9 @@ rspamd_symcache_enable_symbol (struct rspamd_task *task,
 }
 
 gboolean
-rspamd_symcache_is_checked (struct rspamd_task *task,
-                                                       struct rspamd_symcache *cache,
-                                                       const gchar *symbol)
+rspamd_symcache_is_checked(struct rspamd_task *task,
+                                                  struct rspamd_symcache *cache,
+                                                  const gchar *symbol)
 {
        auto *cache_runtime = C_API_SYMCACHE_RUNTIME(task->symcache_runtime);
        auto *real_cache = C_API_SYMCACHE(cache);
@@ -321,11 +321,32 @@ rspamd_symcache_is_checked (struct rspamd_task *task,
 }
 
 gboolean
-rspamd_symcache_process_settings (struct rspamd_task *task,
-                                                                 struct rspamd_symcache *cache)
+rspamd_symcache_process_settings(struct rspamd_task *task,
+                                                                struct rspamd_symcache *cache)
 {
        auto *cache_runtime = C_API_SYMCACHE_RUNTIME(task->symcache_runtime);
        auto *real_cache = C_API_SYMCACHE(cache);
 
        return cache_runtime->process_settings(task, *real_cache);
+}
+
+gboolean
+rspamd_symcache_is_item_allowed(struct rspamd_task *task,
+                                                               struct rspamd_symcache_item *item,
+                                                               gboolean exec_only)
+{
+       auto *real_item = C_API_SYMCACHE_ITEM(item);
+
+       return real_item->is_allowed(task, exec_only);
+}
+
+gboolean
+rspamd_symcache_is_symbol_enabled(struct rspamd_task *task,
+                                                                 struct rspamd_symcache *cache,
+                                                                 const gchar *symbol)
+{
+       auto *cache_runtime = C_API_SYMCACHE_RUNTIME(task->symcache_runtime);
+       auto *real_cache = C_API_SYMCACHE(cache);
+
+       return cache_runtime->is_symbol_enabled(task, *real_cache, symbol);
 }
\ No newline at end of file
index 99e3cfb5da9418fd959a78f6b41a189ff081b8ad..4eb2cc00827af4b6ddbc801edaa7e3b3d7902d5e 100644 (file)
@@ -229,7 +229,7 @@ auto cache_item::get_type_str() const -> const char *
        RSPAMD_UNREACHABLE;
 }
 
-auto cache_item::is_item_allowed(struct rspamd_task *task, bool exec_only) -> bool
+auto cache_item::is_allowed(struct rspamd_task *task, bool exec_only) const -> bool
 {
        const auto *what = "execution";
 
index 484065cc2785c7ffc7fd0ba89bc046ff59425980..f4ed75eb1fb47e281c2803888238fd0d407cdb19 100644 (file)
@@ -93,7 +93,7 @@ public:
                // TODO
        }
 
-       auto check_conditions(std::string_view sym_name, struct rspamd_task *task) -> bool {
+       auto check_conditions(std::string_view sym_name, struct rspamd_task *task) const -> bool {
                return std::all_of(std::begin(conditions), std::end(conditions),
                                                   [&](const auto &cond) { return cond.check(sym_name, task); });
        }
@@ -300,7 +300,7 @@ public:
         * @param exec_only
         * @return
         */
-       auto is_item_allowed(struct rspamd_task *task, bool exec_only) -> bool;
+       auto is_allowed(struct rspamd_task *task, bool exec_only) const -> bool;
 
 private:
        /**
index 74dd4a18329a0d22fe2985218cf595e8eaf52bee..a231bda3158639be79d2bb9df299f72e65f080cf 100644 (file)
@@ -96,7 +96,7 @@ symcache_runtime::process_settings(struct rspamd_task *task, const symcache &cac
                                                g_hash_table_iter_init(&gr_it, gr->symbols);
 
                                                while (g_hash_table_iter_next(&gr_it, &k, &v)) {
-                                                       functor((const char*)k);
+                                                       functor((const char *) k);
                                                }
                                        }
                                }
@@ -116,7 +116,7 @@ symcache_runtime::process_settings(struct rspamd_task *task, const symcache &cac
                it = nullptr;
 
                while ((cur = ucl_iterate_object(enabled, &it, true)) != nullptr) {
-                       enable_symbol(task, cache,ucl_object_tostring(cur));
+                       enable_symbol(task, cache, ucl_object_tostring(cur));
                }
        }
 
@@ -137,7 +137,7 @@ symcache_runtime::process_settings(struct rspamd_task *task, const symcache &cac
                it = nullptr;
 
                while ((cur = ucl_iterate_object (disabled, &it, true)) != nullptr) {
-                       disable_symbol(task, cache,ucl_object_tostring(cur));
+                       disable_symbol(task, cache, ucl_object_tostring(cur));
                }
        }
 
@@ -152,7 +152,7 @@ symcache_runtime::process_settings(struct rspamd_task *task, const symcache &cac
 
 auto symcache_runtime::disable_all_symbols(int skip_mask) -> void
 {
-       for (auto i = 0; i < order->size(); i ++) {
+       for (auto i = 0; i < order->size(); i++) {
                auto *dyn_item = &dynamic_items[i];
                const auto &item = order->d[i];
 
@@ -237,5 +237,39 @@ symcache_runtime::is_symbol_checked(const symcache &cache, std::string_view name
        return false;
 }
 
+auto
+symcache_runtime::is_symbol_enabled(struct rspamd_task *task, const symcache &cache, std::string_view name) -> bool
+{
+
+       const auto *item = cache.get_item_by_name(name, true);
+       if (item) {
+
+               if (!item->is_allowed(task, true)) {
+                       return false;
+               }
+               else {
+                       auto our_id_maybe = rspamd::find_map(order->by_cache_id, item->id);
+
+                       if (our_id_maybe) {
+                               auto *dyn_item = &dynamic_items[our_id_maybe.value()];
+                               if (dyn_item->started) {
+                                       /* Already started */
+                                       return false;
+                               }
+
+                               if (!item->is_virtual()) {
+                                       return std::get<normal_item>(item->specific).check_conditions(item->symbol, task);
+                               }
+                       }
+                       else {
+                               /* Unknown item */
+                               msg_debug_cache_task("cannot enable %s: symbol not found", name.data());
+                       }
+               }
+       }
+
+       return true;
+}
+
 }
 
index 63eda08e2b8c8c1b706ae1df087d4c235264974b..8a0da7bc0228c6e78d4bfa341fbba273c95aad9e 100644 (file)
@@ -109,6 +109,15 @@ public:
         * @return
         */
        auto is_symbol_checked(const symcache &cache, std::string_view name) -> bool;
+
+       /**
+        * Checks if a symbol is enabled for execution, checking all pending conditions
+        * @param task
+        * @param cache
+        * @param name
+        * @return
+        */
+       auto is_symbol_enabled(struct rspamd_task *task, const symcache &cache, std::string_view name) -> bool;
 };