doveadm-mail-expunge.c revision 977f08d645b1779527c0938bbb848b61064839c3
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/* Copyright (c) 2010-2016 Dovecot authors, see the included COPYING file */
1a3f9d72e15fb931edf58f104fb7ff12d238f051Timo Sirainencmd_expunge_box(struct doveadm_mail_cmd_context *_ctx,
1a3f9d72e15fb931edf58f104fb7ff12d238f051Timo Sirainen struct expunge_cmd_context *ctx = (struct expunge_cmd_context *)_ctx;
1a3f9d72e15fb931edf58f104fb7ff12d238f051Timo Sirainen if (doveadm_mail_iter_init(_ctx, info, search_args, 0, NULL,
12224fcf2de6724c89f63c0f9ee857f28a270df5Timo Sirainen if (doveadm_mail_iter_deinit_keep_box(&iter, &box) < 0)
6a262c9bd8f57cf1e57112e0522dbdab28ae8c29Timo Sirainen i_error("Unsubscribing mailbox '%s' failed: %s",
4eecd3e2aadb20768a60f701e329b4345d04430cTimo Sirainenexpunge_search_args_is_mailbox_ok(struct mail_search_arg *args);
4eecd3e2aadb20768a60f701e329b4345d04430cTimo Sirainenexpunge_search_args_is_mailbox_or_ok(struct mail_search_arg *args)
4eecd3e2aadb20768a60f701e329b4345d04430cTimo Sirainen for (arg = args; arg != NULL; arg = arg->next) {
4eecd3e2aadb20768a60f701e329b4345d04430cTimo Sirainen if (!expunge_search_args_is_mailbox_or_ok(arg->value.subargs))
4eecd3e2aadb20768a60f701e329b4345d04430cTimo Sirainen if (!expunge_search_args_is_mailbox_ok(arg->value.subargs))
4eecd3e2aadb20768a60f701e329b4345d04430cTimo Sirainenexpunge_search_args_is_mailbox_ok(struct mail_search_arg *args)
4eecd3e2aadb20768a60f701e329b4345d04430cTimo Sirainen /* a) we find one mailbox here in the SUB block */
4eecd3e2aadb20768a60f701e329b4345d04430cTimo Sirainen for (arg = args; arg != NULL; arg = arg->next) {
4eecd3e2aadb20768a60f701e329b4345d04430cTimo Sirainen if (expunge_search_args_is_mailbox_ok(arg->value.subargs))
4eecd3e2aadb20768a60f701e329b4345d04430cTimo Sirainen /* b) there is at least one OR block, and all of the ORs must have
4eecd3e2aadb20768a60f701e329b4345d04430cTimo Sirainen for (arg = args; arg != NULL; arg = arg->next) {
4eecd3e2aadb20768a60f701e329b4345d04430cTimo Sirainen !expunge_search_args_is_mailbox_or_ok(arg->value.subargs))
4eecd3e2aadb20768a60f701e329b4345d04430cTimo Sirainenexpunge_search_args_is_msgset_ok(struct mail_search_arg *args);
4eecd3e2aadb20768a60f701e329b4345d04430cTimo Sirainenexpunge_search_args_is_msgset_or_ok(struct mail_search_arg *args)
4eecd3e2aadb20768a60f701e329b4345d04430cTimo Sirainen /* we're done if all OR branches contain something else besides
4eecd3e2aadb20768a60f701e329b4345d04430cTimo Sirainen for (arg = args; arg != NULL; arg = arg->next) {
4eecd3e2aadb20768a60f701e329b4345d04430cTimo Sirainen if (!expunge_search_args_is_msgset_or_ok(arg->value.subargs))
4eecd3e2aadb20768a60f701e329b4345d04430cTimo Sirainen if (!expunge_search_args_is_msgset_ok(arg->value.subargs))
4eecd3e2aadb20768a60f701e329b4345d04430cTimo Sirainenexpunge_search_args_is_msgset_ok(struct mail_search_arg *args)
4eecd3e2aadb20768a60f701e329b4345d04430cTimo Sirainen /* all args can't be just MAILBOXes */
4eecd3e2aadb20768a60f701e329b4345d04430cTimo Sirainen for (arg = args; arg != NULL; arg = arg->next) {
4eecd3e2aadb20768a60f701e329b4345d04430cTimo Sirainen /* if each OR branch has something else than just
4eecd3e2aadb20768a60f701e329b4345d04430cTimo Sirainen MAILBOXes, we're ok */
4eecd3e2aadb20768a60f701e329b4345d04430cTimo Sirainen if (expunge_search_args_is_msgset_or_ok(arg->value.subargs))
4eecd3e2aadb20768a60f701e329b4345d04430cTimo Sirainen if (expunge_search_args_is_msgset_ok(arg->value.subargs))
1db62753d9e3b5d71018889c8ef0a3722a307455Timo Sirainencmd_expunge_run(struct doveadm_mail_cmd_context *ctx, struct mail_user *user)
4eecd3e2aadb20768a60f701e329b4345d04430cTimo Sirainen const enum mailbox_list_iter_flags iter_flags =
5fbccc935e3f7b916aa7c6e302a212821072e83aTimo Sirainen iter = doveadm_mailbox_list_iter_init(ctx, user, ctx->search_args,
5fbccc935e3f7b916aa7c6e302a212821072e83aTimo Sirainen while ((info = doveadm_mailbox_list_iter_next(iter)) != NULL) T_BEGIN {
5fbccc935e3f7b916aa7c6e302a212821072e83aTimo Sirainen if (cmd_expunge_box(ctx, info, ctx->search_args) < 0)
5fbccc935e3f7b916aa7c6e302a212821072e83aTimo Sirainen if (doveadm_mailbox_list_iter_deinit(&iter) < 0)
b932ee7fbbec6e79b777dcc7ba613b9e99f8337bTimo Sirainenvoid expunge_search_args_check(struct mail_search_args *args, const char *cmd)
b932ee7fbbec6e79b777dcc7ba613b9e99f8337bTimo Sirainen if (!expunge_search_args_is_mailbox_ok(args->args)) {
5fbccc935e3f7b916aa7c6e302a212821072e83aTimo Sirainen "%s: To avoid accidents, search query "
b932ee7fbbec6e79b777dcc7ba613b9e99f8337bTimo Sirainen "must contain MAILBOX in all search branches", cmd);
b932ee7fbbec6e79b777dcc7ba613b9e99f8337bTimo Sirainen if (!expunge_search_args_is_msgset_ok(args->args)) {
5fbccc935e3f7b916aa7c6e302a212821072e83aTimo Sirainen "%s: To avoid accidents, each branch in search query "
50d16f6fefefb19e41a01400ac11a2adb4d8c231Timo Sirainen "must contain something else besides MAILBOX "
50d16f6fefefb19e41a01400ac11a2adb4d8c231Timo Sirainen "(e.g. just add \"all\" if you want everything)", cmd);
1db62753d9e3b5d71018889c8ef0a3722a307455Timo Sirainenstatic void cmd_expunge_init(struct doveadm_mail_cmd_context *ctx,
23878bd03d1de531e3261a25598beec621351910Timo Sirainen const char *const args[])
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainen ctx->search_args = doveadm_mail_build_search_args(args);
b932ee7fbbec6e79b777dcc7ba613b9e99f8337bTimo Sirainen expunge_search_args_check(ctx->search_args, "expunge");
1a3f9d72e15fb931edf58f104fb7ff12d238f051Timo Sirainenstatic bool cmd_expunge_parse_arg(struct doveadm_mail_cmd_context *_ctx, int c)
1a3f9d72e15fb931edf58f104fb7ff12d238f051Timo Sirainen struct expunge_cmd_context *ctx = (struct expunge_cmd_context *)_ctx;
23878bd03d1de531e3261a25598beec621351910Timo Sirainenstatic struct doveadm_mail_cmd_context *cmd_expunge_alloc(void)
1a3f9d72e15fb931edf58f104fb7ff12d238f051Timo Sirainen ctx = doveadm_mail_cmd_alloc(struct expunge_cmd_context);
977f08d645b1779527c0938bbb848b61064839c3Aki Tuomistruct doveadm_cmd_ver2 doveadm_cmd_expunge_ver2 = {
977f08d645b1779527c0938bbb848b61064839c3Aki Tuomi .usage = DOVEADM_CMD_MAIL_USAGE_PREFIX "[-m] <search query>",
977f08d645b1779527c0938bbb848b61064839c3Aki TuomiDOVEADM_CMD_PARAM('d', "delete-empty-mailbox", CMD_PARAM_BOOL, 0)