From 05cfe989feba8e015cbfecbe7ae28123de89e2a8 Mon Sep 17 00:00:00 2001
From: Stephan Bosch <stephan@rename-it.nl>
Date: Sun, 21 Jun 2009 13:35:00 +0200
Subject: [PATCH] Adjusted to mailbox API changes in Dovecot.

---
 src/lib-sieve-tool/mail-raw.c | 33 +++++++-------
 src/lib-sieve-tool/mail-raw.h |  1 -
 src/lib-sieve/sieve-actions.c | 86 +++++++++++++++++++----------------
 3 files changed, 64 insertions(+), 56 deletions(-)

diff --git a/src/lib-sieve-tool/mail-raw.c b/src/lib-sieve-tool/mail-raw.c
index b0ec7109f..d66088d1d 100644
--- a/src/lib-sieve-tool/mail-raw.c
+++ b/src/lib-sieve-tool/mail-raw.c
@@ -204,6 +204,7 @@ static struct mail_raw *mail_raw_create
 	struct raw_mailbox *raw_box;
 	struct mail_raw *mailr;
 	enum mail_error error;
+	struct mailbox_header_lookup_ctx *headers_ctx;
 
 	if ( mailfile != NULL ) {
 		if ( *mailfile != '/') {
@@ -222,31 +223,31 @@ static struct mail_raw *mail_raw_create
 	mailr->pool = pool;
 
 	if ( mailfile == NULL ) {
-		mailr->box = mailbox_open(&raw_ns->storage, "Dovecot Raw Mail",
-				   input, MAILBOX_OPEN_NO_INDEX_FILES);
+		mailr->box = mailbox_alloc(raw_ns->list, "Dovecot Delivery Mail",
+			input, MAILBOX_FLAG_NO_INDEX_FILES);
 	} else {
 		mtime = (time_t)-1;
-		mailr->box = mailbox_open(&raw_ns->storage, mailfile, NULL,
-				   MAILBOX_OPEN_NO_INDEX_FILES);
+		mailr->box = mailbox_alloc(raw_ns->list, mailfile, NULL,
+			MAILBOX_FLAG_NO_INDEX_FILES);
 	}
 
-	if ( mailr->box == NULL ) {
-		i_fatal("Can't open mail stream as raw: %s",
-			mail_storage_get_last_error(raw_ns->storage, &error));
-	}
-
-	if ( mailbox_sync(mailr->box, 0, 0, NULL ) < 0) {
-		i_fatal("Can't sync delivery mail: %s",
-			mail_storage_get_last_error(raw_ns->storage, &error));
-	}
+	if ( mailbox_open(mailr->box) < 0 ) {
+        i_fatal("Can't open mail stream as raw: %s",
+            mail_storage_get_last_error(raw_ns->storage, &error));
+    }
+    if ( mailbox_sync(mailr->box, 0, 0, NULL) < 0 ) {
+        i_fatal("Can't sync delivery mail: %s",
+            mail_storage_get_last_error(raw_ns->storage, &error));
+    }
 
 	raw_box = (struct raw_mailbox *)mailr->box;
 	raw_box->envelope_sender = sender != NULL ? sender : DEFAULT_ENVELOPE_SENDER;
 	raw_box->mtime = mtime;
 
 	mailr->trans = mailbox_transaction_begin(mailr->box, 0);
-	mailr->headers_ctx = mailbox_header_lookup_init(mailr->box, wanted_headers);
-	mailr->mail = mail_alloc(mailr->trans, 0, mailr->headers_ctx);
+	headers_ctx = mailbox_header_lookup_init(mailr->box, wanted_headers);
+	mailr->mail = mail_alloc(mailr->trans, 0, headers_ctx);
+    mailbox_header_lookup_unref(&headers_ctx);
 	mail_set_seq(mailr->mail, 1);
 
 	return mailr;
@@ -288,8 +289,6 @@ struct mail_raw *mail_raw_open_file(const char *path)
 
 void mail_raw_close(struct mail_raw *mailr) 
 {
-	mailbox_header_lookup_unref(&mailr->headers_ctx);
-
 	mail_free(&mailr->mail);
 	mailbox_transaction_rollback(&mailr->trans);
 	mailbox_close(&mailr->box);
diff --git a/src/lib-sieve-tool/mail-raw.h b/src/lib-sieve-tool/mail-raw.h
index 0f2c23e86..8d9169178 100644
--- a/src/lib-sieve-tool/mail-raw.h
+++ b/src/lib-sieve-tool/mail-raw.h
@@ -12,7 +12,6 @@ struct mail_raw {
 	struct mail *mail;
 
 	struct mailbox *box;
-	struct mailbox_header_lookup_ctx *headers_ctx;
 	struct mailbox_transaction_context *trans;
 };
 
diff --git a/src/lib-sieve/sieve-actions.c b/src/lib-sieve/sieve-actions.c
index bcb537427..b88c7d5ff 100644
--- a/src/lib-sieve/sieve-actions.c
+++ b/src/lib-sieve/sieve-actions.c
@@ -186,50 +186,61 @@ static void act_store_get_storage_error
 }
 
 static struct mailbox *act_store_mailbox_open
-(const struct sieve_action_exec_env *aenv, struct mail_namespace *ns, 
-	const char *folder)
+(const struct sieve_action_exec_env *aenv, struct mail_namespace *ns,
+	const char *folder, const char **error_r)
 {
 	struct mail_storage **storage = &(aenv->exec_status->last_storage);
-	enum mailbox_open_flags open_flags = 
-		MAILBOX_OPEN_FAST | MAILBOX_OPEN_KEEP_RECENT | 
-		MAILBOX_OPEN_SAVEONLY | MAILBOX_OPEN_POST_SESSION;
+	enum mailbox_flags flags =
+		MAILBOX_FLAG_KEEP_RECENT | MAILBOX_FLAG_SAVEONLY |
+		MAILBOX_FLAG_POST_SESSION;
 	struct mailbox *box;
+	enum mail_error error;
+
+	*error_r = NULL;
 
 	if (strcasecmp(folder, "INBOX") == 0) {
 		/* Deliveries to INBOX must always succeed, regardless of ACLs */
-		open_flags |= MAILBOX_OPEN_IGNORE_ACLS;
+		flags |= MAILBOX_FLAG_IGNORE_ACLS;
 	}
 
-	*storage = ns->storage;
+	/* First attempt at opening the box */
+	box = mailbox_alloc(ns->list, folder, NULL, flags);
+	if ( mailbox_open(box) == 0 ) {
+		/* Success */
+		return box;
+	}
 
-	box = mailbox_open(storage, folder, NULL, open_flags);
-		
-	if ( box == NULL && aenv->scriptenv->mailbox_autocreate ) {
-		enum mail_error error;
+	/* Failed */
+
+	*storage = mailbox_get_storage(box);
+	*error_r = mail_storage_get_last_error(*storage, &error);
+	mailbox_close(&box);
 	
-		(void)mail_storage_get_last_error(*storage, &error);
-		if ( error != MAIL_ERROR_NOTFOUND )
-			return NULL;
-
-		/* Try creating it */
-		if ( mail_storage_mailbox_create(*storage, folder, FALSE) < 0 )
-			return NULL;
-   
-		if ( aenv->scriptenv->mailbox_autosubscribe ) {
-			/* Subscribe to it */
-			(void)mailbox_list_set_subscribed(ns->list, folder, TRUE);
-		}
+	/* Only continue when the mailbox is missing and when we are allowed to
+	 * create it.
+	 */
+	if ( !aenv->scriptenv->mailbox_autocreate || error != MAIL_ERROR_NOTFOUND )
+		return NULL;
 
-		/* Try opening again */
-		box = mailbox_open(storage, folder, NULL, open_flags);
-    
-		if (box == NULL)
-			return NULL;
+	/* Try creating it. */
+	if ( mail_storage_mailbox_create(*storage, ns, folder, FALSE) < 0 ) {
+		*error_r = mail_storage_get_last_error(*storage, &error);
+		return NULL;
+	}
 
-		if (mailbox_sync(box, 0, 0, NULL) < 0) {
-			mailbox_close(&box);
-			return NULL;
-		}
+	/* Subscribe to it if required */
+	if ( aenv->scriptenv->mailbox_autosubscribe ) {
+		(void)mailbox_list_set_subscribed(ns->list, folder, TRUE);
+	}
+
+	/* Try opening again */
+	box = mailbox_alloc(ns->list, folder, NULL, flags);
+	*storage = mailbox_get_storage(box);
+	if ( mailbox_open(box) < 0 || mailbox_sync(box, 0, 0, NULL) < 0 ) {
+		/* Failed definitively */
+		*error_r = mail_storage_get_last_error(*storage, &error);
+		mailbox_close(&box);
+		return NULL;
 	}
 
 	return box;
@@ -244,6 +255,7 @@ static bool act_store_start
 	struct mail_namespace *ns = NULL;
 	struct mailbox *box = NULL;
 	pool_t pool = sieve_result_pool(aenv->result);
+	const char *open_error = NULL;
 
 	/* If context is NULL, the store action is the result of (implicit) keep */	
 	if ( ctx == NULL ) {
@@ -260,7 +272,7 @@ static bool act_store_start
 		ns = mail_namespace_find(aenv->scriptenv->namespaces, &ctx->folder);
 
 		if ( ns != NULL ) {		
-			box = act_store_mailbox_open(aenv, ns, ctx->folder);
+			box = act_store_mailbox_open(aenv, ns, ctx->folder, &open_error);
 		}
 	}
 				
@@ -270,13 +282,11 @@ static bool act_store_start
 	trans->namespace = ns;
 	trans->box = box;
 	trans->flags = 0;
-		
-	if ( ns != NULL && box == NULL ) 
-		act_store_get_storage_error(aenv, trans);	
+	trans->error = open_error;		
 	
 	*tr_context = (void *)trans;
 
-	return ( aenv->scriptenv->namespaces == NULL || (box != NULL) );
+	return ( (aenv->scriptenv->namespaces == NULL) || (box != NULL) );
 }
 
 static bool act_store_execute
@@ -307,7 +317,7 @@ static bool act_store_execute
 	/* Mark attempt to use storage. Can only get here when all previous actions
 	 * succeeded. 
 	 */
-	aenv->exec_status->last_storage = trans->namespace->storage;
+	aenv->exec_status->last_storage = mailbox_get_storage(trans->box);
 	
 	/* Start mail transaction */
 	trans->mail_trans = mailbox_transaction_begin
-- 
GitLab