From 9a6749d60fc1909cbace881e3ad761e5c81bc37f Mon Sep 17 00:00:00 2001
From: Stephan Bosch <stephan.bosch@open-xchange.com>
Date: Wed, 28 Aug 2024 22:22:56 +0200
Subject: [PATCH] lib-sieve: include: ext-include-common - Change
 ext_include_get_script_storage() into ext_include_get_script()

Later, this is needed to allow retrieving a global script from one of many
storages, rather than just one.
---
 src/lib-sieve/plugins/include/cmd-include.c   | 18 +++--------
 .../plugins/include/ext-include-binary.c      | 13 ++------
 .../plugins/include/ext-include-common.c      | 31 +++++++++++--------
 .../plugins/include/ext-include-common.h      | 11 ++++---
 4 files changed, 31 insertions(+), 42 deletions(-)

diff --git a/src/lib-sieve/plugins/include/cmd-include.c b/src/lib-sieve/plugins/include/cmd-include.c
index 395ac98fb..2eead0b27 100644
--- a/src/lib-sieve/plugins/include/cmd-include.c
+++ b/src/lib-sieve/plugins/include/cmd-include.c
@@ -6,7 +6,6 @@
 
 #include "sieve-common.h"
 #include "sieve-script.h"
-#include "sieve-storage.h"
 #include "sieve-ast.h"
 #include "sieve-code.h"
 #include "sieve-extensions.h"
@@ -224,7 +223,6 @@ cmd_include_validate(struct sieve_validator *valdtr,
 	struct sieve_ast_argument *arg = cmd->first_positional;
 	struct cmd_include_context_data *ctx_data =
 		(struct cmd_include_context_data *)cmd->data;
-	struct sieve_storage *storage;
 	struct sieve_script *script;
 	const char *script_name;
 	enum sieve_error error_code = SIEVE_ERROR_NONE;
@@ -259,21 +257,19 @@ cmd_include_validate(struct sieve_validator *valdtr,
 		return FALSE;
 	}
 
-	storage = ext_include_get_script_storage(this_ext, ctx_data->location,
-						 script_name, &error_code);
-	if (storage == NULL) {
+	if (ext_include_get_script(this_ext, ctx_data->location,
+				   script_name, &script, &error_code) < 0) {
 		// FIXME: handle ':optional' in this case
 		if (error_code == SIEVE_ERROR_NOT_FOUND) {
 			sieve_argument_validate_error(
 				valdtr, arg, "include: "
-				"%s location for included script '%s' is unavailable "
-				"(contact system administrator for more information)",
+				"included %s script '%s' not found ",
 				ext_include_script_location_name(ctx_data->location),
 				str_sanitize(script_name, 80));
 		} else {
 			sieve_argument_validate_error(
 				valdtr, arg, "include: "
-				"failed to access %s location for included script '%s' "
+				"failed to access included %s script '%s' "
 				"(contact system administrator for more information)",
 				ext_include_script_location_name(ctx_data->location),
 				str_sanitize(script_name, 80));
@@ -281,11 +277,7 @@ cmd_include_validate(struct sieve_validator *valdtr,
 		return FALSE;
 	}
 
-	/* Create script object */
-	if (sieve_storage_get_script(storage, script_name,
-				     &script, &error_code) < 0)
-		return FALSE;
-
+	/* Open script */
 	ret = sieve_script_open(script, &error_code);
 	if (ret < 0) {
 		if (error_code != SIEVE_ERROR_NOT_FOUND) {
diff --git a/src/lib-sieve/plugins/include/ext-include-binary.c b/src/lib-sieve/plugins/include/ext-include-binary.c
index 8f9547def..2fe64086e 100644
--- a/src/lib-sieve/plugins/include/ext-include-binary.c
+++ b/src/lib-sieve/plugins/include/ext-include-binary.c
@@ -7,7 +7,6 @@
 #include "sieve-common.h"
 #include "sieve-error.h"
 #include "sieve-script.h"
-#include "sieve-storage.h"
 #include "sieve-binary.h"
 #include "sieve-generator.h"
 #include "sieve-interpreter.h"
@@ -330,7 +329,6 @@ ext_include_binary_open(const struct sieve_extension *ext,
 		struct sieve_binary_block *inc_block = NULL;
 		unsigned int location, flags;
 		string_t *script_name;
-		struct sieve_storage *storage;
 		struct sieve_script *script;
 		enum sieve_error error_code;
 		int ret;
@@ -369,21 +367,14 @@ ext_include_binary_open(const struct sieve_extension *ext,
 		}
 
 		/* Can we find the script dependency ? */
-		storage = ext_include_get_script_storage(ext, location,
-							 str_c(script_name),
-							 &error_code);
-		if (storage == NULL) {
+		if (ext_include_get_script(ext, location, str_c(script_name),
+					   &script, &error_code) < 0) {
 			/* No, recompile */
 			// FIXME: handle ':optional' in this case
 			return FALSE;
 		}
 
 		/* Can we open the script dependency ? */
-		if (sieve_storage_get_script(storage, str_c(script_name),
-					     &script, &error_code) < 0) {
-			/* No, recompile */
-			return FALSE;
-		}
 		if (sieve_script_open(script, &error_code) < 0) {
 			if (error_code != SIEVE_ERROR_NOT_FOUND) {
 				/* No, recompile */
diff --git a/src/lib-sieve/plugins/include/ext-include-common.c b/src/lib-sieve/plugins/include/ext-include-common.c
index 911a1a72c..bd191bc6e 100644
--- a/src/lib-sieve/plugins/include/ext-include-common.c
+++ b/src/lib-sieve/plugins/include/ext-include-common.c
@@ -129,14 +129,15 @@ void ext_include_unload(const struct sieve_extension *ext)
  * Script access
  */
 
-struct sieve_storage *
-ext_include_get_script_storage(const struct sieve_extension *ext,
-			       enum ext_include_script_location location,
-			       const char *script_name,
-			       enum sieve_error *error_code_r)
+int ext_include_get_script(const struct sieve_extension *ext,
+			   enum ext_include_script_location location,
+			   const char *script_name,
+			   struct sieve_script **script_r,
+			   enum sieve_error *error_code_r)
 {
 	struct sieve_instance *svinst = ext->svinst;
 	struct ext_include_context *extctx = ext->context;
+	struct sieve_storage *storage = NULL;
 
 	switch (location) {
 	case EXT_INCLUDE_LOCATION_PERSONAL:
@@ -144,8 +145,9 @@ ext_include_get_script_storage(const struct sieve_extension *ext,
 		    sieve_storage_create_personal(svinst, NULL, 0,
 						  &extctx->personal_storage,
 						  error_code_r) < 0)
-			return NULL;
-		return extctx->personal_storage;
+			break;
+		storage = extctx->personal_storage;
+		break;
 	case EXT_INCLUDE_LOCATION_GLOBAL:
 		if (extctx->global_location == NULL) {
 			e_info(svinst->event, "include: "
@@ -154,22 +156,25 @@ ext_include_get_script_storage(const struct sieve_extension *ext,
 				str_sanitize(script_name, 80));
 			if (error_code_r != NULL)
 				*error_code_r = SIEVE_ERROR_NOT_FOUND;
-			return NULL;
+			break;
 		}
 		if (extctx->global_storage == NULL) {
 			if (sieve_storage_create(svinst,
 						 extctx->global_location,
 						 0, &extctx->global_storage,
 						 error_code_r) < 0)
-				return NULL;
+				break;
 		}
-		return extctx->global_storage;
-	default:
+		storage = extctx->global_storage;
 		break;
+	default:
+		i_unreached();
 	}
 
-	i_unreached();
-	return NULL;
+	if (storage == NULL)
+		return -1;
+	return sieve_storage_get_script(storage, script_name,
+					script_r, error_code_r);
 }
 
 /*
diff --git a/src/lib-sieve/plugins/include/ext-include-common.h b/src/lib-sieve/plugins/include/ext-include-common.h
index 9b9ca7c63..4357ce192 100644
--- a/src/lib-sieve/plugins/include/ext-include-common.h
+++ b/src/lib-sieve/plugins/include/ext-include-common.h
@@ -82,11 +82,12 @@ extern const struct sieve_operation_def global_operation;
  * Script access
  */
 
-struct sieve_storage *
-ext_include_get_script_storage(const struct sieve_extension *ext,
-			       enum ext_include_script_location location,
-			       const char *script_name,
-			       enum sieve_error *error_code_r);
+int ext_include_get_script(const struct sieve_extension *ext,
+			   enum ext_include_script_location location,
+			   const char *script_name,
+			   struct sieve_script **script_r,
+			   enum sieve_error *error_code_r);
+
 /*
  * Context
  */
-- 
GitLab