From: Aki Tuomi Date: Thu, 12 Apr 2018 11:09:10 +0000 (+0300) Subject: doveadm: Add rebuild attachments command X-Git-Tag: 2.3.2.rc1~156 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=63f60a620be740d97c862f746e93978d6165e0bb;p=thirdparty%2Fdovecot%2Fcore.git doveadm: Add rebuild attachments command Goes thru given mails and resets the attachment indicator. --- diff --git a/doc/man/Makefile.am b/doc/man/Makefile.am index 390c83d0ea..727f0d300f 100644 --- a/doc/man/Makefile.am +++ b/doc/man/Makefile.am @@ -44,6 +44,7 @@ nodist_man1_MANS = \ doveadm-purge.1 \ doveadm-pw.1 \ doveadm-quota.1 \ + doveadm-rebuild.1 \ doveadm-replicator.1 \ doveadm-save.1 \ doveadm-search.1 \ @@ -94,6 +95,7 @@ EXTRA_DIST = \ doveadm-purge.1.in \ doveadm-pw.1.in \ doveadm-quota.1.in \ + doveadm-rebuild.1.in \ doveadm-replicator.1.in \ doveadm-save.1.in \ doveadm-search.1.in \ diff --git a/doc/man/doveadm-rebuild.1.in b/doc/man/doveadm-rebuild.1.in new file mode 100644 index 0000000000..d8261b62a3 --- /dev/null +++ b/doc/man/doveadm-rebuild.1.in @@ -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 \ " [" \-S +.IR socket_path "] " search_query +.br +.\"------------------------------------- +.BR doveadm " [" \-Dv "] [" \-f +.IR formatter ] +.BR rebuild \ " [" \-S +.IR socket_path ] +.BI \-A \ search_query +.br +.\"------------------------------------- +.BR doveadm " [" \-Dv "] [" \-f +.IR formatter ] +.BR rebuild \ " [" \-S +.IR socket_path ] +.BI \-F " file search_query" +.br +.\"------------------------------------- +.BR doveadm " [" \-Dv "] [" \-f +.IR formatter ] +.BR rebuild \ " [" \-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) diff --git a/src/doveadm/Makefile.am b/src/doveadm/Makefile.am index e82d3a8fc8..8637250589 100644 --- a/src/doveadm/Makefile.am +++ b/src/doveadm/Makefile.am @@ -107,7 +107,8 @@ doveadm_common_mail_cmds = \ doveadm-mail-save.c \ doveadm-mail-search.c \ doveadm-mail-server.c \ - doveadm-mail-mailbox-cache.c + doveadm-mail-mailbox-cache.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 index 0000000000..e7ae242c76 --- /dev/null +++ b/src/doveadm/doveadm-mail-rebuild.c @@ -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 "", +DOVEADM_CMD_PARAMS_START +DOVEADM_CMD_MAIL_COMMON +DOVEADM_CMD_PARAM('\0', "query", CMD_PARAM_ARRAY, CMD_PARAM_FLAG_POSITIONAL) +DOVEADM_CMD_PARAMS_END +}; diff --git a/src/doveadm/doveadm-mail.c b/src/doveadm/doveadm-mail.c index 4e00767965..c9828b1741 100644 --- a/src/doveadm/doveadm-mail.c +++ b/src/doveadm/doveadm-mail.c @@ -921,6 +921,7 @@ static struct doveadm_cmd_ver2 *mail_commands_ver2[] = { &doveadm_cmd_move_ver2, &doveadm_cmd_mailbox_cache_decision, &doveadm_cmd_mailbox_cache_remove, + &doveadm_cmd_rebuild_attachments, }; void doveadm_mail_init(void) diff --git a/src/doveadm/doveadm-mail.h b/src/doveadm/doveadm-mail.h index b060621bd1..16ebcc8828 100644 --- a/src/doveadm/doveadm-mail.h +++ b/src/doveadm/doveadm-mail.h @@ -197,6 +197,7 @@ 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_mailbox_cache_decision; extern struct doveadm_cmd_ver2 doveadm_cmd_mailbox_cache_remove; +extern struct doveadm_cmd_ver2 doveadm_cmd_rebuild_attachments; #define DOVEADM_CMD_MAIL_COMMON \ DOVEADM_CMD_PARAM('A', "all-users", CMD_PARAM_BOOL, 0) \