doveadm-mail-expunge.c revision cf63dc8723b971cc80638fccbf494d961cbafc7f
4eecd3e2aadb20768a60f701e329b4345d04430cTimo Sirainen/* Copyright (c) 2010 Dovecot authors, see the included COPYING file */
4eecd3e2aadb20768a60f701e329b4345d04430cTimo Sirainencmd_expunge_box(const struct mailbox_info *info,
4eecd3e2aadb20768a60f701e329b4345d04430cTimo Sirainen if (doveadm_mail_iter_init(info, search_args, &trans, &iter) < 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))
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainencmd_expunge_run(struct doveadm_mail_cmd_context *_ctx, struct mail_user *user)
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainen struct expunge_cmd_context *ctx = (struct expunge_cmd_context *)_ctx;
4eecd3e2aadb20768a60f701e329b4345d04430cTimo Sirainen const enum mailbox_list_iter_flags iter_flags =
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainen if (!expunge_search_args_is_mailbox_ok(ctx->search_args->args)) {
4eecd3e2aadb20768a60f701e329b4345d04430cTimo Sirainen i_fatal("expunge: To avoid accidents, search query "
4eecd3e2aadb20768a60f701e329b4345d04430cTimo Sirainen "must contain MAILBOX in all search branches");
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainen if (!expunge_search_args_is_msgset_ok(ctx->search_args->args)) {
4eecd3e2aadb20768a60f701e329b4345d04430cTimo Sirainen i_fatal("expunge: To avoid accidents, each branch in "
4eecd3e2aadb20768a60f701e329b4345d04430cTimo Sirainen "search query must contain something else "
4eecd3e2aadb20768a60f701e329b4345d04430cTimo Sirainen "besides MAILBOX");
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainen iter = doveadm_mail_list_iter_init(user, ctx->search_args, iter_flags);
4eecd3e2aadb20768a60f701e329b4345d04430cTimo Sirainen while ((info = doveadm_mail_list_iter_next(iter)) != NULL) T_BEGIN {
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainen (void)cmd_expunge_box(info, ctx->search_args);
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainenstruct doveadm_mail_cmd_context *cmd_expunge(const char *const args[])
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainen ctx = doveadm_mail_cmd_init(struct expunge_cmd_context);