From 4fad2db5659bc6a31819b88a1c7f563d245897e8 Mon Sep 17 00:00:00 2001
From: Stephan Bosch <stephan.bosch@open-xchange.com>
Date: Sun, 20 Oct 2024 23:01:04 +0200
Subject: [PATCH] lib-sieve: plugins: enotify: ext-enotify-common - Rework
 error handling to match extension API changes

---
 .../plugins/enotify/ext-enotify-common.c         | 16 +++++++++++-----
 .../plugins/enotify/ext-enotify-common.h         |  4 ++--
 src/lib-sieve/plugins/enotify/ext-enotify.c      |  9 +++++++--
 .../plugins/enotify/mailto/ntfy-mailto.c         | 13 +++++--------
 .../plugins/enotify/sieve-ext-enotify.h          |  2 +-
 5 files changed, 26 insertions(+), 18 deletions(-)

diff --git a/src/lib-sieve/plugins/enotify/ext-enotify-common.c b/src/lib-sieve/plugins/enotify/ext-enotify-common.c
index 0cb67ee64..292ed21eb 100644
--- a/src/lib-sieve/plugins/enotify/ext-enotify-common.c
+++ b/src/lib-sieve/plugins/enotify/ext-enotify-common.c
@@ -94,21 +94,27 @@ ext_enotify_method_register(struct ext_enotify_context *extctx,
 	nmth->svinst = ntfy_ext->svinst;
 	nmth->ext = ntfy_ext;
 
-	if (nmth_def->load != NULL)
-		nmth_def->load(nmth, &nmth->context);
+	if (nmth_def->load != NULL &&
+	    nmth_def->load(nmth, &nmth->context) < 0) {
+		array_pop_back(&extctx->notify_methods);
+		return -1;
+	}
 
 	*nmth_r = nmth;
 	return 0;
 }
 
-void ext_enotify_methods_init(struct ext_enotify_context *extctx,
-			      const struct sieve_extension *ntfy_ext)
+int ext_enotify_methods_init(struct ext_enotify_context *extctx,
+			     const struct sieve_extension *ntfy_ext)
 {
 	const struct sieve_enotify_method *nmth;
 
 	p_array_init(&extctx->notify_methods, default_pool, 4);
 
-	ext_enotify_method_register(extctx, ntfy_ext, &mailto_notify, &nmth);
+	if (ext_enotify_method_register(extctx, ntfy_ext,
+					&mailto_notify, &nmth) < 0)
+		return -1;
+	return 0;
 }
 
 void ext_enotify_methods_deinit(struct ext_enotify_context *extctx)
diff --git a/src/lib-sieve/plugins/enotify/ext-enotify-common.h b/src/lib-sieve/plugins/enotify/ext-enotify-common.h
index 423524264..7029dfe45 100644
--- a/src/lib-sieve/plugins/enotify/ext-enotify-common.h
+++ b/src/lib-sieve/plugins/enotify/ext-enotify-common.h
@@ -76,8 +76,8 @@ extern const struct sieve_variables_modifier_def encodeurl_modifier;
  * Notify methods
  */
 
-void ext_enotify_methods_init(struct ext_enotify_context *extctx,
-			      const struct sieve_extension *ntfy_ext);
+int ext_enotify_methods_init(struct ext_enotify_context *extctx,
+			     const struct sieve_extension *ntfy_ext);
 void ext_enotify_methods_deinit(struct ext_enotify_context *extctx);
 
 const struct sieve_enotify_method *
diff --git a/src/lib-sieve/plugins/enotify/ext-enotify.c b/src/lib-sieve/plugins/enotify/ext-enotify.c
index 4711cb7e7..543d65e2d 100644
--- a/src/lib-sieve/plugins/enotify/ext-enotify.c
+++ b/src/lib-sieve/plugins/enotify/ext-enotify.c
@@ -67,7 +67,10 @@ static int ext_enotify_load(const struct sieve_extension *ext, void **context_r)
 	extctx = i_new(struct ext_enotify_context, 1);
 	extctx->var_ext = var_ext;
 
-	ext_enotify_methods_init(extctx, ext);
+	if (ext_enotify_methods_init(extctx, ext) < 0) {
+		i_free(extctx);
+		return -1;
+	}
 
 	sieve_extension_capabilities_register(ext, &notify_capabilities);
 
@@ -79,8 +82,10 @@ static void ext_enotify_unload(const struct sieve_extension *ext)
 {
 	struct ext_enotify_context *extctx = ext->context;
 
-	ext_enotify_methods_deinit(extctx);
+	if (extctx == NULL)
+		return;
 
+	ext_enotify_methods_deinit(extctx);
 	i_free(extctx);
 }
 
diff --git a/src/lib-sieve/plugins/enotify/mailto/ntfy-mailto.c b/src/lib-sieve/plugins/enotify/mailto/ntfy-mailto.c
index a8469eb10..09440fe45 100644
--- a/src/lib-sieve/plugins/enotify/mailto/ntfy-mailto.c
+++ b/src/lib-sieve/plugins/enotify/mailto/ntfy-mailto.c
@@ -52,7 +52,7 @@
  * Mailto notification method
  */
 
-static bool
+static int
 ntfy_mailto_load(const struct sieve_enotify_method *nmth, void **context);
 static void
 ntfy_mailto_unload(const struct sieve_enotify_method *nmth);
@@ -155,16 +155,13 @@ struct ntfy_mailto_context {
 	struct sieve_address_source envelope_from;
 };
 
-static bool
-ntfy_mailto_load(const struct sieve_enotify_method *nmth, void **context)
+static int
+ntfy_mailto_load(const struct sieve_enotify_method *nmth, void **context_r)
 {
 	struct sieve_instance *svinst = nmth->svinst;
 	struct ntfy_mailto_context *mtctx;
 	pool_t pool;
 
-	if (*context != NULL)
-		ntfy_mailto_unload(nmth);
-
 	pool = pool_alloconly_create("ntfy_mailto_context", 256);
 	mtctx = p_new(pool, struct ntfy_mailto_context, 1);
 	mtctx->pool = pool;
@@ -173,8 +170,8 @@ ntfy_mailto_load(const struct sieve_enotify_method *nmth, void **context)
 		svinst, mtctx->pool, "sieve_notify_mailto_envelope_from",
 		&mtctx->envelope_from);
 
-	*context = mtctx;
-	return TRUE;
+	*context_r = mtctx;
+	return 0;
 }
 
 static void ntfy_mailto_unload(const struct sieve_enotify_method *nmth)
diff --git a/src/lib-sieve/plugins/enotify/sieve-ext-enotify.h b/src/lib-sieve/plugins/enotify/sieve-ext-enotify.h
index e65b29e99..849a3315f 100644
--- a/src/lib-sieve/plugins/enotify/sieve-ext-enotify.h
+++ b/src/lib-sieve/plugins/enotify/sieve-ext-enotify.h
@@ -35,7 +35,7 @@ struct sieve_enotify_method_def {
 	const char *identifier;
 
 	/* Registration */
-	bool (*load)(const struct sieve_enotify_method *nmth, void **context);
+	int (*load)(const struct sieve_enotify_method *nmth, void **context);
 	void (*unload)(const struct sieve_enotify_method *nmth);
 
 	/* Validation */
-- 
GitLab