doveadm-mail-expunge.c revision c57ba1788a6598993a9625bbf26a4fedca23815e
5a580c3a38ced62d4bcc95b8ac7c4f2935b5d294Timo Sirainen/* Copyright (c) 2010-2013 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)
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 "
5fbccc935e3f7b916aa7c6e302a212821072e83aTimo Sirainen "must contain something else besides MAILBOX", 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);
1a3f9d72e15fb931edf58f104fb7ff12d238f051Timo Sirainen cmd_expunge_alloc, "expunge", "[-d] <search query>"