dovecot-2.2: doveadm: Added "save" command to directly save mail...

dovecot at dovecot.org dovecot at dovecot.org
Sun Feb 15 08:11:30 UTC 2015


details:   http://hg.dovecot.org/dovecot-2.2/rev/d3bb7541ca5e
changeset: 18255:d3bb7541ca5e
user:      Timo Sirainen <tss at iki.fi>
date:      Sun Feb 15 10:11:01 2015 +0200
description:
doveadm: Added "save" command to directly save mail to specified mailbox.
The mail is read from stdin.

diffstat:

 src/doveadm/Makefile.am         |    1 +
 src/doveadm/doveadm-mail-save.c |  123 ++++++++++++++++++++++++++++++++++++++++
 src/doveadm/doveadm-mail.c      |    1 +
 src/doveadm/doveadm-mail.h      |    1 +
 4 files changed, 126 insertions(+), 0 deletions(-)

diffs (160 lines):

diff -r 22a5eda76490 -r d3bb7541ca5e src/doveadm/Makefile.am
--- a/src/doveadm/Makefile.am	Sun Feb 15 10:09:19 2015 +0200
+++ b/src/doveadm/Makefile.am	Sun Feb 15 10:11:01 2015 +0200
@@ -93,6 +93,7 @@
 	doveadm-mail-mailbox-status.c \
 	doveadm-mail-copymove.c \
 	doveadm-mailbox-list-iter.c \
+	doveadm-mail-save.c \
 	doveadm-mail-search.c \
 	doveadm-mail-server.c
 
diff -r 22a5eda76490 -r d3bb7541ca5e src/doveadm/doveadm-mail-save.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/doveadm/doveadm-mail-save.c	Sun Feb 15 10:11:01 2015 +0200
@@ -0,0 +1,123 @@
+/* Copyright (c) 2015 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "istream.h"
+#include "mail-storage.h"
+#include "doveadm-mail.h"
+
+struct save_cmd_context {
+	struct doveadm_mail_cmd_context ctx;
+	const char *mailbox;
+};
+
+static int
+cmd_save_to_mailbox(struct save_cmd_context *ctx, struct mailbox *box,
+		    struct istream *input)
+{
+	struct mail_storage *storage = mailbox_get_storage(box);
+	struct mailbox_transaction_context *trans;
+	struct mail_save_context *save_ctx;
+	ssize_t ret;
+	bool save_failed = FALSE;
+
+	if (mailbox_open(box) < 0) {
+		i_error("Failed to open mailbox %s: %s",
+			mailbox_get_vname(box), mailbox_get_last_error(box, NULL));
+		doveadm_mail_failed_storage(&ctx->ctx, storage);
+		return -1;
+	}
+
+	trans = mailbox_transaction_begin(box, MAILBOX_TRANSACTION_FLAG_EXTERNAL);
+	save_ctx = mailbox_save_alloc(trans);
+	if (mailbox_save_begin(&save_ctx, input) < 0) {
+		i_error("Saving failed: %s", mailbox_get_last_error(box, NULL));
+		doveadm_mail_failed_storage(&ctx->ctx, storage);
+		mailbox_transaction_rollback(&trans);
+		return -1;
+	}
+	while ((ret = i_stream_read(input)) > 0 || ret == -2) {
+		if (mailbox_save_continue(save_ctx) < 0) {
+			save_failed = TRUE;
+			ret = -1;
+			break;
+		}
+	}
+	i_assert(ret == -1);
+
+	if (input->stream_errno != 0) {
+		i_error("read(msg input) failed: %s", i_stream_get_error(input));
+		doveadm_mail_failed_error(&ctx->ctx, MAIL_ERROR_TEMP);
+	} else if (save_failed) {
+		i_error("Saving failed: %s", mailbox_get_last_error(box, NULL));
+		doveadm_mail_failed_storage(&ctx->ctx, storage);
+	} else if (mailbox_save_finish(&save_ctx) < 0) {
+		i_error("Saving failed: %s",
+			mailbox_get_last_error(box, NULL));
+		doveadm_mail_failed_storage(&ctx->ctx, storage);
+	} else if (mailbox_transaction_commit(&trans) < 0) {
+		i_error("Save transaction commit failed: %s",
+			mailbox_get_last_error(box, NULL));
+		doveadm_mail_failed_storage(&ctx->ctx, storage);
+	} else {
+		ret = 0;
+	}
+	if (save_ctx != NULL)
+		mailbox_save_cancel(&save_ctx);
+	if (trans != NULL)
+		mailbox_transaction_rollback(&trans);
+	i_assert(input->eof);
+	return ret < 0 ? -1 : 0;
+}
+
+static int
+cmd_save_run(struct doveadm_mail_cmd_context *_ctx, struct mail_user *user)
+{
+	struct save_cmd_context *ctx = (struct save_cmd_context *)_ctx;
+	struct mail_namespace *ns;
+	struct mailbox *box;
+	int ret;
+
+	ns = mail_namespace_find(user->namespaces, ctx->mailbox);
+	box = mailbox_alloc(ns->list, ctx->mailbox, MAILBOX_FLAG_SAVEONLY);
+	ret = cmd_save_to_mailbox(ctx, box, _ctx->cmd_input);
+	mailbox_free(&box);
+	return ret;
+}
+
+static void cmd_save_init(struct doveadm_mail_cmd_context *_ctx,
+			  const char *const args[] ATTR_UNUSED)
+{
+	doveadm_mail_get_input(_ctx);
+}
+
+static bool
+cmd_mailbox_save_parse_arg(struct doveadm_mail_cmd_context *_ctx, int c)
+{
+	struct save_cmd_context *ctx = (struct save_cmd_context *)_ctx;
+
+	switch (c) {
+	case 'm':
+		ctx->mailbox = optarg;
+		break;
+	default:
+		return FALSE;
+	}
+	return TRUE;
+}
+
+static struct doveadm_mail_cmd_context *cmd_save_alloc(void)
+{
+	struct save_cmd_context *ctx;
+
+	ctx = doveadm_mail_cmd_alloc(struct save_cmd_context);
+	ctx->ctx.getopt_args = "m:";
+	ctx->ctx.v.parse_arg = cmd_mailbox_save_parse_arg;
+	ctx->ctx.v.init = cmd_save_init;
+	ctx->ctx.v.run = cmd_save_run;
+	ctx->mailbox = "INBOX";
+	return &ctx->ctx;
+}
+
+struct doveadm_mail_cmd cmd_save = {
+	cmd_save_alloc, "save", "[-m mailbox]"
+};
diff -r 22a5eda76490 -r d3bb7541ca5e src/doveadm/doveadm-mail.c
--- a/src/doveadm/doveadm-mail.c	Sun Feb 15 10:09:19 2015 +0200
+++ b/src/doveadm/doveadm-mail.c	Sun Feb 15 10:11:01 2015 +0200
@@ -787,6 +787,7 @@
 	&cmd_force_resync,
 	&cmd_purge,
 	&cmd_expunge,
+	&cmd_save,
 	&cmd_search,
 	&cmd_fetch,
 	&cmd_flags_add,
diff -r 22a5eda76490 -r d3bb7541ca5e src/doveadm/doveadm-mail.h
--- a/src/doveadm/doveadm-mail.h	Sun Feb 15 10:09:19 2015 +0200
+++ b/src/doveadm/doveadm-mail.h	Sun Feb 15 10:11:01 2015 +0200
@@ -167,6 +167,7 @@
 			      struct mailbox_list *list);
 
 extern struct doveadm_mail_cmd cmd_expunge;
+extern struct doveadm_mail_cmd cmd_save;
 extern struct doveadm_mail_cmd cmd_search;
 extern struct doveadm_mail_cmd cmd_fetch;
 extern struct doveadm_mail_cmd cmd_flags_add;


More information about the dovecot-cvs mailing list