]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
doveadm: Add rebuild attachments command
authorAki Tuomi <aki.tuomi@dovecot.fi>
Thu, 12 Apr 2018 11:09:10 +0000 (14:09 +0300)
committerTimo Sirainen <timo.sirainen@dovecot.fi>
Mon, 30 Apr 2018 12:45:17 +0000 (15:45 +0300)
Goes thru given mails and resets the attachment indicator.

doc/man/Makefile.am
doc/man/doveadm-rebuild.1.in [new file with mode: 0644]
src/doveadm/Makefile.am
src/doveadm/doveadm-mail-rebuild.c [new file with mode: 0644]
src/doveadm/doveadm-mail.c
src/doveadm/doveadm-mail.h

index d1ff0bf4d024f456a376ddd9427f30a58490ad82..eb4e7ea552c1c91bceefa9df365debb18ca437c1 100644 (file)
@@ -45,6 +45,7 @@ nodist_man1_MANS = \
        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 @@ EXTRA_DIST = \
        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 \
diff --git a/doc/man/doveadm-rebuild.1.in b/doc/man/doveadm-rebuild.1.in
new file mode 100644 (file)
index 0000000..d8261b6
--- /dev/null
@@ -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)
index c6446461161b4c26a8bad5de5f8169b5e094872a..021b931d5beaf160e9d1cb9f869f8ae5a1e4ce95 100644 (file)
@@ -100,7 +100,8 @@ doveadm_common_mail_cmds = \
        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
diff --git a/src/doveadm/doveadm-mail-rebuild.c b/src/doveadm/doveadm-mail-rebuild.c
new file mode 100644 (file)
index 0000000..e7ae242
--- /dev/null
@@ -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
+};
index c0ef4ad428a19e19613c697dd665043e5122037d..1f0238b25114bfcde95d1fcc79cc42f359fa6aee 100644 (file)
@@ -918,7 +918,8 @@ static struct doveadm_cmd_ver2 *mail_commands_ver2[] = {
        &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)
index 494cf6c4dae9eb21516968bcd6217a9df15b9b7c..5eddd9fb5fa365971b2896b1e15185a04cd7bcc3 100644 (file)
@@ -201,6 +201,7 @@ extern struct doveadm_cmd_ver2 doveadm_cmd_copy_ver2;
 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) \