]> source.dussan.org Git - rspamd.git/commitdiff
[Project] Implement more methods
authorVsevolod Stakhov <vsevolod@rspamd.com>
Sat, 30 Apr 2022 12:42:38 +0000 (13:42 +0100)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Sat, 30 Apr 2022 12:42:38 +0000 (13:42 +0100)
src/libserver/symcache/symcache_c.cxx
src/libserver/symcache/symcache_internal.hxx
src/libserver/symcache/symcache_item.hxx

index 4547f23a4a8a94539b560570c5af757ce63527dc..ba3ccd9a673fa5705b871e2d0618cb94b763afd6 100644 (file)
@@ -433,7 +433,7 @@ rspamd_symcache_set_cur_item(struct rspamd_task *task, struct rspamd_symcache_it
        auto *cache_runtime = C_API_SYMCACHE_RUNTIME(task->symcache_runtime);
        auto *real_item = C_API_SYMCACHE_ITEM(item);
 
-       return (struct rspamd_symcache_item *)cache_runtime->set_cur_item(real_item);
+       return (struct rspamd_symcache_item *) cache_runtime->set_cur_item(real_item);
 }
 
 void
@@ -494,4 +494,38 @@ rspamd_symcache_item_async_dec_check_full(struct rspamd_task *task,
        }
 
        return FALSE;
+}
+
+struct rspamd_abstract_callback_data *
+rspamd_symcache_get_cbdata(struct rspamd_symcache *cache,
+                                                  const gchar *symbol)
+{
+       auto *real_cache = C_API_SYMCACHE(cache);
+
+       auto *item = real_cache->get_item_by_name(symbol, true);
+
+       if (item) {
+               return (struct rspamd_abstract_callback_data *) item->get_cbdata();
+       }
+
+       return nullptr;
+}
+
+void
+rspamd_symcache_composites_foreach(struct rspamd_task *task,
+                                                                  struct rspamd_symcache *cache,
+                                                                  GHFunc func,
+                                                                  gpointer fd)
+{
+       auto *real_cache = C_API_SYMCACHE(cache);
+       auto *cache_runtime = C_API_SYMCACHE_RUNTIME(task->symcache_runtime);
+
+       real_cache->composites_foreach([&](const auto *item) {
+               auto *dyn_item = cache_runtime->get_dynamic_item(item->id, false);
+
+               if (dyn_item->started) {
+                       func((void *)item->get_name().c_str(), item->get_cbdata(), fd);
+                       dyn_item->finished = true;
+               }
+       });
 }
\ No newline at end of file
index 457dc58483c3f8f1984a71b453df98d2d3dc6be1..1b4d5a509e3c88348131dc39d17f71be92315645 100644 (file)
@@ -355,6 +355,18 @@ public:
                }
        }
 
+       /**
+        * Iterate over all composites using a specific functor
+        * @tparam Functor
+        * @param f
+        */
+       template<typename Functor>
+       auto composites_foreach(Functor f) -> void {
+               for (const auto &sym_it : composites) {
+                       f(sym_it.get());
+               }
+       }
+
        /**
         * Resort cache if anything has been changed since last time
         * @return
index f4ed75eb1fb47e281c2803888238fd0d407cdb19..a194e9b0d51a34d0848cb9db2bb01e8ddeaddbbb 100644 (file)
@@ -97,6 +97,10 @@ public:
                return std::all_of(std::begin(conditions), std::end(conditions),
                                                   [&](const auto &cond) { return cond.check(sym_name, task); });
        }
+
+       auto get_cbdata() const -> auto {
+               return user_data;
+       }
 };
 
 class virtual_item {
@@ -302,6 +306,20 @@ public:
         */
        auto is_allowed(struct rspamd_task *task, bool exec_only) const -> bool;
 
+       /**
+        * Returns callback data
+        * @return
+        */
+       auto get_cbdata() const -> void * {
+               if (std::holds_alternative<normal_item>(specific)) {
+                       const auto &filter_data = std::get<normal_item>(specific);
+
+                       return filter_data.get_cbdata();
+               }
+
+               return nullptr;
+       }
+
 private:
        /**
         * Constructor for a normal symbols with callback
@@ -326,6 +344,7 @@ private:
                                                         priority(_priority),
                                                         specific(normal_item{func, user_data})
        {
+               /* These structures are kept trivial, so they need to be explicitly reset */
                forbidden_ids.reset();
                allowed_ids.reset();
                exec_only_ids.reset();
@@ -352,6 +371,7 @@ private:
                                                         flags(_flags),
                                                         specific(virtual_item{parent})
        {
+               /* These structures are kept trivial, so they need to be explicitly reset */
                forbidden_ids.reset();
                allowed_ids.reset();
                exec_only_ids.reset();