]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-lda: Moved most of mail_deliver_save_open() to lib-storage.
authorStephan Bosch <stephan.bosch@dovecot.fi>
Wed, 13 Sep 2017 23:32:17 +0000 (01:32 +0200)
committerTimo Sirainen <tss@dovecot.fi>
Wed, 4 Oct 2017 22:07:46 +0000 (01:07 +0300)
This prevents the need to link Pigeonhole lib-sieve to lib-lda, which makes no sense for IMAPSIEVE.
This also allows lib-sieve to have more control over how mailboxes are opened.

src/lib-lda/mail-deliver.c

index c43ecfc4fc6fc01b8bb5d8a06b8a9f14041aa395..ead2dc6a19ff2b95f6a55cd1a76d1f0be3d28581 100644 (file)
@@ -43,7 +43,6 @@ struct mail_deliver_cache {
 
 struct mail_deliver_mailbox {
        union mailbox_module_context module_ctx;
-       bool delivery_box;
 };
 
 struct mail_deliver_transaction {
@@ -202,10 +201,8 @@ int mail_deliver_save_open(struct mail_deliver_save_open_context *ctx,
                           const char *name, struct mailbox **box_r,
                           enum mail_error *error_r, const char **error_str_r)
 {
-       struct mail_namespace *ns;
        struct mailbox *box;
-       enum mailbox_flags flags =
-               MAILBOX_FLAG_SAVEONLY | MAILBOX_FLAG_POST_SESSION;
+       enum mailbox_flags flags = 0;
 
        *box_r = NULL;
        *error_r = MAIL_ERROR_NONE;
@@ -217,54 +214,16 @@ int mail_deliver_save_open(struct mail_deliver_save_open_context *ctx,
                return -1;
        }
 
-       ns = mail_namespace_find(ctx->user->namespaces, name);
-       if (strcmp(name, ns->prefix) == 0 &&
-           (ns->flags & NAMESPACE_FLAG_INBOX_USER) != 0) {
-               /* delivering to a namespace prefix means we actually want to
-                  deliver to the INBOX instead */
-               name = "INBOX";
-               ns = mail_namespace_find_inbox(ctx->user->namespaces);
-       }
-
-       if (strcasecmp(name, "INBOX") == 0) {
-               /* deliveries to INBOX must always succeed,
-                  regardless of ACLs */
-               flags |= MAILBOX_FLAG_IGNORE_ACLS;
-       }
-
-       *box_r = box = mailbox_alloc(ns->list, name, flags);
-       mailbox_set_reason(box, "lib-lda delivery");
-       /* flag that this mailbox is used for delivering the mail.
-          the context isn't set in pigeonhole testuite. */
-       struct mail_deliver_mailbox *mbox = MAIL_DELIVER_STORAGE_CONTEXT(box);
-       if (mbox != NULL)
-               mbox->delivery_box = TRUE;
+       if (ctx->lda_mailbox_autocreate)
+               flags |= MAILBOX_FLAG_AUTO_CREATE;
+       if (ctx->lda_mailbox_autosubscribe)
+               flags |= MAILBOX_FLAG_AUTO_SUBSCRIBE;
+       *box_r = box = mailbox_alloc_delivery(ctx->user, name, flags);
 
        if (mailbox_open(box) == 0)
                return 0;
-
        *error_str_r = mailbox_get_last_internal_error(box, error_r);
-       if (!ctx->lda_mailbox_autocreate || *error_r != MAIL_ERROR_NOTFOUND)
-               return -1;
-
-       /* try creating it. */
-       if (mailbox_create(box, NULL, FALSE) < 0) {
-               *error_str_r = mailbox_get_last_internal_error(box, error_r);
-               if (*error_r != MAIL_ERROR_EXISTS)
-                       return -1;
-               /* someone else just created it */
-       }
-       if (ctx->lda_mailbox_autosubscribe) {
-               /* (try to) subscribe to it */
-               (void)mailbox_set_subscribed(box, TRUE);
-       }
-
-       /* and try opening again */
-       if (mailbox_open(box) < 0) {
-               *error_str_r = mailbox_get_last_internal_error(box, error_r);
-               return -1;
-       }
-       return 0;
+       return -1;
 }
 
 static bool mail_deliver_check_duplicate(struct mail_deliver_session *session,
@@ -640,7 +599,7 @@ mail_deliver_transaction_commit(struct mailbox_transaction_context *ctx,
           we also want to do this only for commits generated by sieve.
           other plugins or storage backends may be creating transactions as
           well, which we need to ignore. */
-       if (mbox->delivery_box)
+       if ((box->flags & MAILBOX_FLAG_POST_SESSION) != 0)
                muser->deliver_ctx->cache = &dt->cache;
 
        if (mbox->module_ctx.super.transaction_commit(ctx, changes_r) < 0)
@@ -675,6 +634,9 @@ static void mail_deliver_mailbox_allocated(struct mailbox *box)
        if (muser->deliver_ctx == NULL)
                return;
 
+       if ((box->flags & MAILBOX_FLAG_POST_SESSION) != 0)
+               mailbox_set_reason(box, "lib-lda delivery");
+
        mbox = p_new(box->pool, struct mail_deliver_mailbox, 1);
        mbox->module_ctx.super = *v;
        box->vlast = &mbox->module_ctx.super;