commit b2ffc2a: [Minor] Allow silent addition of the composites from the configuration

Vsevolod Stakhov vsevolod at rspamd.com
Sat Jun 17 14:49:05 UTC 2023


Author: Vsevolod Stakhov
Date: 2023-06-17 15:43:12 +0100
URL: https://github.com/rspamd/rspamd/commit/b2ffc2a06b3a62befd0486010930b7da97eda2c7

[Minor] Allow silent addition of the composites from the configuration

---
 src/libserver/composites/composites.h            |  2 ++
 src/libserver/composites/composites_internal.hxx |  4 +--
 src/libserver/composites/composites_manager.cxx  | 40 +++++++++++++++++++++---
 3 files changed, 39 insertions(+), 7 deletions(-)

diff --git a/src/libserver/composites/composites.h b/src/libserver/composites/composites.h
index 6ccfbd58f..335a5d424 100644
--- a/src/libserver/composites/composites.h
+++ b/src/libserver/composites/composites.h
@@ -48,12 +48,14 @@ gsize rspamd_composites_manager_nelts(void *);
  * @return
  */
 void* rspamd_composites_manager_add_from_ucl(void *,  const char *, const ucl_object_t *);
+void* rspamd_composites_manager_add_from_ucl_silent(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 *);
+void* rspamd_composites_manager_add_from_string_silent(void *, const char *, const char *);
 
 #ifdef  __cplusplus
 }
diff --git a/src/libserver/composites/composites_internal.hxx b/src/libserver/composites/composites_internal.hxx
index 570cb4ea9..5b311fc6c 100644
--- a/src/libserver/composites/composites_internal.hxx
+++ b/src/libserver/composites/composites_internal.hxx
@@ -71,8 +71,8 @@ public:
 		return nullptr;
 	}
 
-	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 *;
+	auto add_composite(std::string_view, const ucl_object_t *, bool silent_duplicate) -> rspamd_composite *;
+	auto add_composite(std::string_view name, std::string_view expression, bool silent_duplicate) -> rspamd_composite *;
 private:
 	~composites_manager() = default;
 	static void composites_manager_dtor(void *ptr) {
diff --git a/src/libserver/composites/composites_manager.cxx b/src/libserver/composites/composites_manager.cxx
index 27fb170b9..bf4491097 100644
--- a/src/libserver/composites/composites_manager.cxx
+++ b/src/libserver/composites/composites_manager.cxx
@@ -49,7 +49,7 @@ composite_policy_from_str(const std::string_view &inp) -> enum rspamd_composite_
 }
 
 auto
-composites_manager::add_composite(std::string_view composite_name, const ucl_object_t *obj) -> rspamd_composite *
+composites_manager::add_composite(std::string_view composite_name, const ucl_object_t *obj, bool silent_duplicate) -> rspamd_composite *
 {
 
 	const auto *val = ucl_object_lookup(obj, "enabled");
@@ -59,7 +59,13 @@ composites_manager::add_composite(std::string_view composite_name, const ucl_obj
 	}
 
 	if (composites.contains(composite_name)) {
-		msg_warn_config ("composite %s is redefined", composite_name.data());
+		if (silent_duplicate) {
+			msg_debug_config ("composite %s is redefined", composite_name.data());
+			return nullptr;
+		}
+		else {
+			msg_warn_config ("composite %s is redefined", composite_name.data());
+		}
 	}
 
 	const char *composite_expression = nullptr;
@@ -142,11 +148,23 @@ composites_manager::add_composite(std::string_view composite_name, const ucl_obj
 
 auto
 composites_manager::add_composite(std::string_view composite_name,
-								  std::string_view composite_expression) -> rspamd_composite *
+								  std::string_view composite_expression,
+								  bool silent_duplicate) -> rspamd_composite *
 {
 	GError *err = nullptr;
 	rspamd_expression *expr = nullptr;
 
+	if (composites.contains(composite_name)) {
+		/* Duplicate composite - refuse to add */
+		if (silent_duplicate) {
+			msg_debug_config ("composite %s is redefined", composite_name.data());
+			return nullptr;
+		}
+		else {
+			msg_warn_config ("composite %s is redefined", composite_name.data());
+		}
+	}
+
 	if (!rspamd_parse_expression(composite_expression.data(),
 			composite_expression.size(), &composite_expr_subr,
 			nullptr, cfg->cfg_pool, &err, &expr)) {
@@ -191,11 +209,23 @@ rspamd_composites_manager_nelts(void *ptr)
 void*
 rspamd_composites_manager_add_from_ucl(void *cm, const char *sym, const ucl_object_t *obj)
 {
-	return reinterpret_cast<void *>(COMPOSITE_MANAGER_FROM_PTR(cm)->add_composite(sym, obj));
+	return reinterpret_cast<void *>(COMPOSITE_MANAGER_FROM_PTR(cm)->add_composite(sym, obj, false));
 }
 
 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));
+	return reinterpret_cast<void *>(COMPOSITE_MANAGER_FROM_PTR(cm)->add_composite(sym, expr, false));
+}
+
+void*
+rspamd_composites_manager_add_from_ucl_silent(void *cm, const char *sym, const ucl_object_t *obj)
+{
+	return reinterpret_cast<void *>(COMPOSITE_MANAGER_FROM_PTR(cm)->add_composite(sym, obj, true));
+}
+
+void*
+rspamd_composites_manager_add_from_string_silent(void *cm, const char *sym, const char *expr)
+{
+	return reinterpret_cast<void *>(COMPOSITE_MANAGER_FROM_PTR(cm)->add_composite(sym, expr, true));
 }


More information about the Commits mailing list