]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lda/lmtp: Moved common raw mailbox allocation code to raw-storage.
authorTimo Sirainen <tss@iki.fi>
Sun, 2 Oct 2011 13:35:22 +0000 (16:35 +0300)
committerTimo Sirainen <tss@iki.fi>
Sun, 2 Oct 2011 13:35:22 +0000 (16:35 +0300)
src/lda/main.c
src/lib-storage/index/raw/raw-storage.c
src/lib-storage/index/raw/raw-storage.h
src/lmtp/commands.c

index 35a1077a0161b65fe269e171f881611805f26cb0..ccc1b5d09bd807753d637a24c9418df3037e9e1f 100644 (file)
@@ -212,14 +212,13 @@ int main(int argc, char *argv[])
        struct mail_storage_service_user *service_user;
        struct mail_storage_service_input service_input;
        struct mail_user *raw_mail_user;
-       struct mail_namespace *raw_ns;
        struct mail_storage *storage;
        struct mailbox *box;
-       struct raw_mailbox *raw_box;
        struct istream *input;
        struct mailbox_transaction_context *t;
        struct mailbox_header_lookup_ctx *headers_ctx;
        const char *user_source = "", *destaddr_source = "";
+       const char *envelope_sender;
        void **sets;
        uid_t process_euid;
        bool stderr_rejection = FALSE;
@@ -372,35 +371,24 @@ int main(int argc, char *argv[])
        sets = master_service_settings_get_others(master_service);
        raw_mail_user =
                raw_storage_create_from_set(ctx.dest_user->set_info, sets[0]);
-       raw_ns = raw_mail_user->namespaces;
 
+       envelope_sender = ctx.src_envelope_sender != NULL ?
+               ctx.src_envelope_sender : DEFAULT_ENVELOPE_SENDER;
        if (path == NULL) {
                input = create_raw_stream(&ctx, 0, &mtime);
                i_stream_set_name(input, "stdin");
-               box = mailbox_alloc(raw_ns->list, "Dovecot Delivery Mail",
-                                   MAILBOX_FLAG_NO_INDEX_FILES);
-               if (mailbox_open_stream(box, input) < 0) {
-                       i_fatal("Can't open delivery mail as raw: %s",
-                               mailbox_get_last_error(box, &error));
-               }
+               ret = raw_mailbox_alloc_stream(raw_mail_user, input, mtime,
+                                              envelope_sender, &box);
                i_stream_unref(&input);
        } else {
-               mtime = (time_t)-1;
-               box = mailbox_alloc(raw_ns->list, path,
-                                   MAILBOX_FLAG_NO_INDEX_FILES);
-               if (mailbox_open(box) < 0) {
-                       i_fatal("Can't open delivery mail as raw: %s",
-                               mailbox_get_last_error(box, &error));
-               }
+               ret = raw_mailbox_alloc_path(raw_mail_user, path, (time_t)-1,
+                                            envelope_sender, &box);
        }
-       if (mailbox_sync(box, 0) < 0) {
-               i_fatal("Can't sync delivery mail: %s",
+       if (ret < 0) {
+               i_fatal("Can't open delivery mail as raw: %s",
                        mailbox_get_last_error(box, &error));
        }
-       raw_box = (struct raw_mailbox *)box;
-       raw_box->envelope_sender = ctx.src_envelope_sender != NULL ?
-               ctx.src_envelope_sender : DEFAULT_ENVELOPE_SENDER;
-       raw_box->mtime = mtime;
+       mail_user_unref(&raw_mail_user);
 
        t = mailbox_transaction_begin(box, 0);
        headers_ctx = mailbox_header_lookup_init(box, wanted_headers);
@@ -468,7 +456,6 @@ int main(int argc, char *argv[])
        mailbox_free(&box);
 
        mail_user_unref(&ctx.dest_user);
-       mail_user_unref(&raw_mail_user);
        mail_deliver_session_deinit(&ctx.session);
 
        mail_storage_service_user_free(&service_user);
index 2ebf2d0ebd0795f500b78f7981796b1ede3e9c99..e65b52b7c1c23b003746aebae9e5f6c7f67023a3 100644 (file)
@@ -39,6 +39,52 @@ raw_storage_create_from_set(const struct setting_parser_info *set_info,
        return user;
 }
 
+static int
+raw_mailbox_alloc_common(struct mail_user *user, struct istream *input,
+                        const char *path, time_t received_time,
+                        const char *envelope_sender, struct mailbox **box_r)
+{
+       struct mail_namespace *ns = user->namespaces;
+       struct mailbox *box;
+       struct raw_mailbox *raw_box;
+       const char *name;
+
+       name = path != NULL ? path : i_stream_get_name(input);
+       box = *box_r = mailbox_alloc(ns->list, name,
+                                    MAILBOX_FLAG_NO_INDEX_FILES);
+       if (input != NULL) {
+               if (mailbox_open_stream(box, input) < 0)
+                       return -1;
+       } else {
+               if (mailbox_open(box) < 0)
+                       return -1;
+       }
+       if (mailbox_sync(box, 0) < 0)
+               return -1;
+
+       i_assert(strcmp(box->storage->name, RAW_STORAGE_NAME) == 0);
+       raw_box = (struct raw_mailbox *)box;
+       raw_box->envelope_sender = envelope_sender;
+       raw_box->mtime = received_time;
+       return 0;
+}
+
+int raw_mailbox_alloc_stream(struct mail_user *user, struct istream *input,
+                            time_t received_time, const char *envelope_sender,
+                            struct mailbox **box_r)
+{
+       return raw_mailbox_alloc_common(user, input, NULL, received_time,
+                                       envelope_sender, box_r);
+}
+
+int raw_mailbox_alloc_path(struct mail_user *user, const char *path,
+                          time_t received_time, const char *envelope_sender,
+                          struct mailbox **box_r)
+{
+       return raw_mailbox_alloc_common(user, NULL, path, received_time,
+                                       envelope_sender, box_r);
+}
+
 static struct mail_storage *raw_storage_alloc(void)
 {
        struct raw_storage *storage;
index e7f555325991728f50e25d5d98ad32060ca6d821..fddd998a915667ab65533aeb3e5472e2fae377e9 100644 (file)
@@ -28,4 +28,11 @@ struct mail_user *
 raw_storage_create_from_set(const struct setting_parser_info *set_info,
                            const struct mail_user_settings *set);
 
+int raw_mailbox_alloc_stream(struct mail_user *user, struct istream *input,
+                            time_t received_time, const char *envelope_sender,
+                            struct mailbox **box_r);
+int raw_mailbox_alloc_path(struct mail_user *user, const char *path,
+                          time_t received_time, const char *envelope_sender,
+                          struct mailbox **box_r);
+
 #endif
index 90e0bf703abb8a421a025269218cb13ae3b0315f..1637ab5971709003ddc0dfdf15e2c7e46038eda2 100644 (file)
@@ -626,23 +626,18 @@ static int client_open_raw_mail(struct client *client, struct istream *input)
                NULL
        };
        struct mailbox *box;
-       struct raw_mailbox *raw_box;
        struct mailbox_header_lookup_ctx *headers_ctx;
        enum mail_error error;
 
-       box = mailbox_alloc(client->raw_mail_user->namespaces->list,
-                           "Dovecot Delivery Mail",
-                           MAILBOX_FLAG_NO_INDEX_FILES);
-       if (mailbox_open_stream(box, input) < 0 ||
-           mailbox_sync(box, 0) < 0) {
+       if (raw_mailbox_alloc_stream(client->raw_mail_user, input,
+                                    (time_t)-1, client->state.mail_from,
+                                    &box) < 0) {
                i_error("Can't open delivery mail as raw: %s",
                        mailbox_get_last_error(box, &error));
                mailbox_free(&box);
                client_rcpt_fail_all(client);
                return -1;
        }
-       raw_box = (struct raw_mailbox *)box;
-       raw_box->envelope_sender = client->state.mail_from;
 
        client->state.raw_box = box;
        client->state.raw_trans = mailbox_transaction_begin(box, 0);