From 17e466051cdcce504e5fcc2031bd57a7a2f0a402 Mon Sep 17 00:00:00 2001
From: Stephan Bosch <stephan.bosch@open-xchange.com>
Date: Tue, 9 Jul 2024 00:31:04 +0200
Subject: [PATCH] lib-sieve: sieve-storage - Refactor
 sieve_storage_open_script()

---
 src/lib-sieve/sieve-storage.c | 40 +++++++++++++++++++----------------
 1 file changed, 22 insertions(+), 18 deletions(-)

diff --git a/src/lib-sieve/sieve-storage.c b/src/lib-sieve/sieve-storage.c
index cddf071e0..792fae26d 100644
--- a/src/lib-sieve/sieve-storage.c
+++ b/src/lib-sieve/sieve-storage.c
@@ -767,7 +767,7 @@ int sieve_storage_open_script(struct sieve_storage *storage, const char *name,
 
 	if (sieve_storage_get_script(storage, name, &script, error_code_r) < 0)
 		return -1;
-	if (sieve_script_open(script, error_code_r) >= 0) {
+	if (sieve_script_open(script, error_code_r) == 0) {
 		*script_r = script;
 		return 0;
 	}
@@ -775,26 +775,30 @@ int sieve_storage_open_script(struct sieve_storage *storage, const char *name,
 	/* Error */
 	sieve_script_unref(&script);
 
-	if (storage->error_code == SIEVE_ERROR_NOT_FOUND &&
-	    (storage->flags & SIEVE_STORAGE_FLAG_SYNCHRONIZING) == 0 &&
-	    storage->default_name != NULL &&
-	    storage->default_location != NULL &&
-	    strcmp(storage->default_name, name) == 0) {
-		/* Not found; if this name maps to the default script,
-		   try to open that instead */
-		i_assert(*storage->default_location != '\0');
+	if (storage->error_code != SIEVE_ERROR_NOT_FOUND ||
+	    (storage->flags & SIEVE_STORAGE_FLAG_SYNCHRONIZING) != 0)
+		return -1;
 
-		e_debug(storage->event, "Trying default script instead");
+	/* Not found; if this name maps to the default script,
+	   try to access that instead */
 
-		if (sieve_script_create_open(svinst, storage->default_location,
-					     NULL, &script,
-					     error_code_r) < 0)
-			return -1;
+	if (storage->default_name == NULL ||
+	    storage->default_location == NULL ||
+	    strcmp(storage->default_name, name) != 0)
+		return -1;
 
-		script->storage->is_default = TRUE;
-		script->storage->default_for = storage;
-		sieve_storage_ref(storage);
-	}
+	i_assert(*storage->default_location != '\0');
+
+	e_debug(storage->event, "Trying default script instead");
+
+	if (sieve_script_create_open(svinst, storage->default_location,
+				     NULL, &script,
+				     error_code_r) < 0)
+		return -1;
+
+	script->storage->is_default = TRUE;
+	script->storage->default_for = storage;
+	sieve_storage_ref(storage);
 
 	*script_r = script;
 	return 0;
-- 
GitLab