changeset 22927:73eef2e5e7cc

doveadm: Add rebuild attachments command Goes thru given mails and resets the attachment indicator.
author Aki Tuomi <aki.tuomi@dovecot.fi>
date Thu, 12 Apr 2018 14:09:10 +0300
parents 1e7e7ea7482d
children 057fa2be3603
files doc/man/Makefile.am doc/man/doveadm-rebuild.1.in src/doveadm/Makefile.am src/doveadm/doveadm-mail-rebuild.c src/doveadm/doveadm-mail.c src/doveadm/doveadm-mail.h
diffstat 6 files changed, 220 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/doc/man/Makefile.am	Thu Apr 12 14:07:54 2018 +0300
+++ b/doc/man/Makefile.am	Thu Apr 12 14:09:10 2018 +0300
@@ -45,6 +45,7 @@
 	doveadm-purge.1 \
 	doveadm-pw.1 \
 	doveadm-quota.1 \
+	doveadm-rebuild.1 \
 	doveadm-replicator.1 \
 	doveadm-search.1 \
 	doveadm-stats.1 \
@@ -95,6 +96,7 @@
 	doveadm-purge.1.in \
 	doveadm-pw.1.in \
 	doveadm-quota.1.in \
+	doveadm-rebuild.1.in \
 	doveadm-replicator.1.in \
 	doveadm-search.1.in \
 	doveadm-stats.1.in \
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/doc/man/doveadm-rebuild.1.in	Thu Apr 12 14:09:10 2018 +0300
@@ -0,0 +1,112 @@
+.\" Copyright (c) 2010-2018 Dovecot authors, see the included COPYING file
+.TH DOVEADM\-REBUILD 1 "2015-05-09" "Dovecot v2.3" "Dovecot"
+.SH NAME
+doveadm\-rebuild - Commands related to rebuilding various aspects of mails matching
+given search query.
+.\"------------------------------------------------------------------------
+.SH SYNOPSIS
+.BR doveadm " [" \-Dv "] [" \-f
+.IR formatter ]
+.BR rebuild \ <command> " [" \-S
+.IR socket_path "] " search_query
+.br
+.\"-------------------------------------
+.BR doveadm " [" \-Dv "] [" \-f
+.IR formatter ]
+.BR rebuild \ <command> " [" \-S
+.IR socket_path ]
+.BI \-A \ search_query
+.br
+.\"-------------------------------------
+.BR doveadm " [" \-Dv "] [" \-f
+.IR formatter ]
+.BR rebuild \ <command> " [" \-S
+.IR socket_path ]
+.BI \-F " file search_query"
+.br
+.\"-------------------------------------
+.BR doveadm " [" \-Dv "] [" \-f
+.IR formatter ]
+.BR rebuild \ <command> " [" \-S
+.IR socket_path ]
+.BI \-u " user search_query"
+.\"------------------------------------------------------------------------
+.SH DESCRIPTION
+The
+.B rebuild attachments
+command is used to rebuilds attachment presence.
+.BR doveadm (1)
+will print the message\(aqs uid for each match.
+.br
+When used with the
+.B \-A
+or
+.BI \-u \ wildcard
+options,
+.BR doveadm (1)
+will print the fields
+.BR username \ and
+.B uid
+for each matching message.
+.PP
+In the first form,
+.BR doveadm (1)
+will execute the
+.B rebuild
+action with the environment of the logged in system user.
+.PP
+In the second form, the command will be performed for all users.
+.PP
+In the third form, the command will be performed for all users listed in
+the given
+.IR file .
+.PP
+In the fourth form, only matching mails of the given
+.IR user (s)
+will be rebuilded
+.\"------------------------------------------------------------------------
+@INCLUDE:global-options-formatter@
+.\" --- command specific options --- "/.
+.PP
+This command uses by default the output formatter
+.B flow
+(without the
+.IR key =
+prefix).
+.PP
+Command specific
+.IR options :
+.\"-------------------------------------
+@INCLUDE:option-A@
+.\"-------------------------------------
+@INCLUDE:option-F-file@
+.\"-------------------------------------
+@INCLUDE:option-S-socket@
+.\"-------------------------------------
+@INCLUDE:option-u-user@
+.\"------------------------------------------------------------------------
+.SH ARGUMENTS
+.TP
+.I search_query
+Resets attachment indicator for messages matching this search query.
+See
+.BR doveadm\-search\-query (7)
+for details.
+.\"------------------------------------------------------------------------
+.SH EXAMPLE
+This example demonstrates how to rebuild user bob\(aqs attachment status.
+.PP
+.nf
+.ft B
+doveadm rebuild attachments \-u bob ALL
+.ft P
+1
+2
+3
+.fi
+.\"------------------------------------------------------------------------
+@INCLUDE:reporting-bugs@
+.\"------------------------------------------------------------------------
+.SH SEE ALSO
+.BR doveadm (1),
+.BR doveadm\-search\-query (7)
--- a/src/doveadm/Makefile.am	Thu Apr 12 14:07:54 2018 +0300
+++ b/src/doveadm/Makefile.am	Thu Apr 12 14:09:10 2018 +0300
@@ -100,7 +100,8 @@
 	doveadm-mailbox-list-iter.c \
 	doveadm-mail-save.c \
 	doveadm-mail-search.c \
-	doveadm-mail-server.c
+	doveadm-mail-server.c \
+	doveadm-mail-rebuild.c
 
 # these aren't actually useful in doveadm-server, but plugins may implement
 # both dumping and some other commands inside a single plugin. not having the
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/doveadm/doveadm-mail-rebuild.c	Thu Apr 12 14:09:10 2018 +0300
@@ -0,0 +1,101 @@
+/* Copyright (c) 2018 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "mail-storage.h"
+#include "doveadm-print.h"
+#include "doveadm-mailbox-list-iter.h"
+#include "doveadm-mail-iter.h"
+#include "doveadm-mail.h"
+#include "mail-storage-private.h"
+
+static int
+cmd_rebuild_attachment_box(struct doveadm_mail_cmd_context *ctx,
+			   const struct mailbox_info *info)
+{
+	struct doveadm_mail_iter *iter;
+	struct mail *mail;
+	int ret = 0;
+
+	if (doveadm_mail_iter_init(ctx, info, ctx->search_args,
+				   MAIL_FETCH_IMAP_BODYSTRUCTURE|
+				   MAIL_FETCH_MESSAGE_PARTS, NULL, FALSE,
+				   &iter) < 0)
+		return -1;
+
+	while (doveadm_mail_iter_next(iter, &mail) && ret >= 0) {
+		T_BEGIN {
+			doveadm_print(dec2str(mail->uid));
+			switch(mail_set_attachment_keywords(mail)) {
+			case -1:
+				doveadm_print("error");
+				doveadm_mail_failed_mailbox(ctx, mail->box);
+				ret = -1;
+				break;
+			case 0:
+				doveadm_print("no");
+				break;
+			case 1:
+				doveadm_print("yes");
+				break;
+			default:
+				i_unreached();
+			}
+		} T_END;
+	}
+
+	if (doveadm_mail_iter_deinit(&iter) < 0)
+		ret = -1;
+	return ret;
+}
+
+static int
+cmd_rebuild_attachment_run(struct doveadm_mail_cmd_context *ctx,
+			   struct mail_user *user)
+{
+	const enum mailbox_list_iter_flags iter_flags =
+		MAILBOX_LIST_ITER_NO_AUTO_BOXES |
+		MAILBOX_LIST_ITER_RETURN_NO_FLAGS;
+	struct doveadm_mailbox_list_iter *iter;
+	const struct mailbox_info *info;
+	int ret = 0;
+
+	iter = doveadm_mailbox_list_iter_init(ctx, user, ctx->search_args,
+					      iter_flags);
+	while ((info = doveadm_mailbox_list_iter_next(iter)) != NULL) T_BEGIN {
+		if (cmd_rebuild_attachment_box(ctx, info) < 0)
+			ret = -1;
+	} T_END;
+	if (doveadm_mailbox_list_iter_deinit(&iter) < 0)
+		ret = -1;
+	return ret;
+}
+
+static void cmd_rebuild_attachment_init(struct doveadm_mail_cmd_context *ctx,
+					const char *const args[])
+{
+	doveadm_print_header_simple("uid");
+	doveadm_print_header_simple("attachment");
+	ctx->search_args = doveadm_mail_build_search_args(args);
+}
+
+
+static struct doveadm_mail_cmd_context *cmd_rebuild_attachment_alloc(void)
+{
+	struct doveadm_mail_cmd_context *ctx;
+
+	ctx = doveadm_mail_cmd_alloc(struct doveadm_mail_cmd_context);
+	ctx->v.init = cmd_rebuild_attachment_init;
+	ctx->v.run = cmd_rebuild_attachment_run;
+	doveadm_print_init(DOVEADM_PRINT_TYPE_TABLE);
+	return ctx;
+}
+
+struct doveadm_cmd_ver2 doveadm_cmd_rebuild_attachments = {
+	.name = "rebuild attachments",
+	.mail_cmd = cmd_rebuild_attachment_alloc,
+	.usage = DOVEADM_CMD_MAIL_USAGE_PREFIX "<search query>",
+DOVEADM_CMD_PARAMS_START
+DOVEADM_CMD_MAIL_COMMON
+DOVEADM_CMD_PARAM('\0', "query", CMD_PARAM_ARRAY, CMD_PARAM_FLAG_POSITIONAL)
+DOVEADM_CMD_PARAMS_END
+};
--- a/src/doveadm/doveadm-mail.c	Thu Apr 12 14:07:54 2018 +0300
+++ b/src/doveadm/doveadm-mail.c	Thu Apr 12 14:09:10 2018 +0300
@@ -918,7 +918,8 @@
 	&doveadm_cmd_purge_ver2,
 	&doveadm_cmd_search_ver2,
 	&doveadm_cmd_copy_ver2,
-	&doveadm_cmd_move_ver2
+	&doveadm_cmd_move_ver2,
+	&doveadm_cmd_rebuild_attachments,
 };
 
 void doveadm_mail_init(void)
--- a/src/doveadm/doveadm-mail.h	Thu Apr 12 14:07:54 2018 +0300
+++ b/src/doveadm/doveadm-mail.h	Thu Apr 12 14:09:10 2018 +0300
@@ -201,6 +201,7 @@
 extern struct doveadm_cmd_ver2 doveadm_cmd_move_ver2;
 extern struct doveadm_cmd_ver2 doveadm_cmd_mailbox_update_ver2;
 extern struct doveadm_cmd_ver2 doveadm_cmd_mailbox_path_ver2;
+extern struct doveadm_cmd_ver2 doveadm_cmd_rebuild_attachments;
 
 #define DOVEADM_CMD_MAIL_COMMON \
 DOVEADM_CMD_PARAM('A', "all-users", CMD_PARAM_BOOL, 0) \