doveadm-mail-expunge.c revision 6a262c9bd8f57cf1e57112e0522dbdab28ae8c29
305N/A/* Copyright (c) 2010-2015 Dovecot authors, see the included COPYING file */
305N/A
305N/A#include "lib.h"
305N/A#include "array.h"
305N/A#include "mail-index.h"
305N/A#include "mail-storage.h"
305N/A#include "mail-search.h"
305N/A#include "doveadm-mailbox-list-iter.h"
305N/A#include "doveadm-mail-iter.h"
305N/A#include "doveadm-mail.h"
305N/A
305N/Astruct expunge_cmd_context {
305N/A struct doveadm_mail_cmd_context ctx;
305N/A bool delete_empty_mailbox;
305N/A};
305N/A
305N/Astatic int
305N/Acmd_expunge_box(struct doveadm_mail_cmd_context *_ctx,
305N/A const struct mailbox_info *info,
305N/A struct mail_search_args *search_args)
305N/A{
305N/A struct expunge_cmd_context *ctx = (struct expunge_cmd_context *)_ctx;
844N/A struct doveadm_mail_iter *iter;
305N/A struct mailbox *box;
305N/A struct mail *mail;
305N/A enum mail_error error;
305N/A int ret = 0;
305N/A
305N/A if (doveadm_mail_iter_init(_ctx, info, search_args, 0, NULL,
615N/A &iter) < 0)
586N/A return -1;
618N/A
586N/A while (doveadm_mail_iter_next(iter, &mail)) {
305N/A if (doveadm_debug) {
844N/A i_debug("expunge: box=%s uid=%u",
844N/A info->vname, mail->uid);
305N/A }
1273N/A mail_expunge(mail);
305N/A }
305N/A
305N/A if (doveadm_mail_iter_deinit_keep_box(&iter, &box) < 0)
305N/A ret = -1;
305N/A else if (mailbox_sync(box, 0) < 0) {
305N/A i_error("Syncing mailbox '%s' failed: %s",
844N/A mailbox_get_vname(box),
844N/A mailbox_get_last_error(box, NULL));
305N/A doveadm_mail_failed_mailbox(_ctx, box);
305N/A ret = -1;
305N/A }
305N/A
305N/A if (ctx->delete_empty_mailbox && ret == 0) {
305N/A if (mailbox_delete_empty(box) < 0) {
305N/A error = mailbox_get_last_mail_error(box);
305N/A if (error != MAIL_ERROR_EXISTS) {
305N/A i_error("Deleting mailbox '%s' failed: %s",
305N/A mailbox_get_vname(box),
615N/A mailbox_get_last_error(box, NULL));
305N/A doveadm_mail_failed_mailbox(_ctx, box);
940N/A ret = -1;
455N/A }
455N/A } else {
305N/A if (mailbox_set_subscribed(box, FALSE) < 0) {
305N/A i_error("Unsubscribing mailbox '%s' failed: %s",
305N/A mailbox_get_vname(box),
305N/A mailbox_get_last_error(box, NULL));
305N/A doveadm_mail_failed_mailbox(_ctx, box);
305N/A ret = -1;
305N/A }
305N/A }
305N/A }
305N/A mailbox_free(&box);
305N/A return ret;
305N/A}
305N/A
305N/Astatic bool
437N/Aexpunge_search_args_is_mailbox_ok(struct mail_search_arg *args);
305N/A
305N/Astatic bool
305N/Aexpunge_search_args_is_mailbox_or_ok(struct mail_search_arg *args)
305N/A{
305N/A struct mail_search_arg *arg;
305N/A
305N/A for (arg = args; arg != NULL; arg = arg->next) {
305N/A switch (arg->type) {
979N/A case SEARCH_OR:
305N/A if (!expunge_search_args_is_mailbox_or_ok(arg->value.subargs))
305N/A return FALSE;
305N/A break;
305N/A case SEARCH_SUB:
305N/A case SEARCH_INTHREAD:
305N/A if (!expunge_search_args_is_mailbox_ok(arg->value.subargs))
305N/A return FALSE;
305N/A break;
305N/A case SEARCH_MAILBOX:
305N/A case SEARCH_MAILBOX_GUID:
305N/A case SEARCH_MAILBOX_GLOB:
305N/A break;
305N/A default:
305N/A return FALSE;
305N/A }
305N/A }
305N/A return TRUE;
305N/A}
305N/A
static bool
expunge_search_args_is_mailbox_ok(struct mail_search_arg *args)
{
struct mail_search_arg *arg;
bool have_or = FALSE;
/* a) we find one mailbox here in the SUB block */
for (arg = args; arg != NULL; arg = arg->next) {
switch (arg->type) {
case SEARCH_MAILBOX:
case SEARCH_MAILBOX_GUID:
case SEARCH_MAILBOX_GLOB:
return TRUE;
case SEARCH_OR:
have_or = TRUE;
break;
case SEARCH_SUB:
case SEARCH_INTHREAD:
if (expunge_search_args_is_mailbox_ok(arg->value.subargs))
return TRUE;
break;
default:
break;
}
}
/* b) there is at least one OR block, and all of the ORs must have
mailbox */
if (!have_or)
return FALSE;
for (arg = args; arg != NULL; arg = arg->next) {
if (arg->type == SEARCH_OR &&
!expunge_search_args_is_mailbox_or_ok(arg->value.subargs))
return FALSE;
}
return TRUE;
}
static bool
expunge_search_args_is_msgset_ok(struct mail_search_arg *args);
static bool
expunge_search_args_is_msgset_or_ok(struct mail_search_arg *args)
{
struct mail_search_arg *arg;
/* we're done if all OR branches contain something else besides
MAILBOXes */
for (arg = args; arg != NULL; arg = arg->next) {
switch (arg->type) {
case SEARCH_MAILBOX:
case SEARCH_MAILBOX_GUID:
case SEARCH_MAILBOX_GLOB:
return FALSE;
case SEARCH_OR:
if (!expunge_search_args_is_msgset_or_ok(arg->value.subargs))
return FALSE;
break;
case SEARCH_SUB:
if (!expunge_search_args_is_msgset_ok(arg->value.subargs))
return FALSE;
break;
default:
break;
}
}
return TRUE;
}
static bool
expunge_search_args_is_msgset_ok(struct mail_search_arg *args)
{
struct mail_search_arg *arg;
/* all args can't be just MAILBOXes */
for (arg = args; arg != NULL; arg = arg->next) {
switch (arg->type) {
case SEARCH_MAILBOX:
case SEARCH_MAILBOX_GUID:
case SEARCH_MAILBOX_GLOB:
break;
case SEARCH_OR:
/* if each OR branch has something else than just
MAILBOXes, we're ok */
if (expunge_search_args_is_msgset_or_ok(arg->value.subargs))
return TRUE;
break;
case SEARCH_SUB:
if (expunge_search_args_is_msgset_ok(arg->value.subargs))
return TRUE;
break;
default:
return TRUE;
}
}
return FALSE;
}
static int
cmd_expunge_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_expunge_box(ctx, info, ctx->search_args) < 0)
ret = -1;
} T_END;
if (doveadm_mailbox_list_iter_deinit(&iter) < 0)
ret = -1;
return ret;
}
void expunge_search_args_check(struct mail_search_args *args, const char *cmd)
{
mail_search_args_simplify(args);
if (!expunge_search_args_is_mailbox_ok(args->args)) {
i_fatal_status(EX_USAGE,
"%s: To avoid accidents, search query "
"must contain MAILBOX in all search branches", cmd);
}
if (!expunge_search_args_is_msgset_ok(args->args)) {
i_fatal_status(EX_USAGE,
"%s: To avoid accidents, each branch in search query "
"must contain something else besides MAILBOX "
"(e.g. just add \"all\" if you want everything)", cmd);
}
}
static void cmd_expunge_init(struct doveadm_mail_cmd_context *ctx,
const char *const args[])
{
if (args[0] == NULL)
doveadm_mail_help_name("expunge");
ctx->search_args = doveadm_mail_build_search_args(args);
expunge_search_args_check(ctx->search_args, "expunge");
}
static bool cmd_expunge_parse_arg(struct doveadm_mail_cmd_context *_ctx, int c)
{
struct expunge_cmd_context *ctx = (struct expunge_cmd_context *)_ctx;
switch (c) {
case 'd':
ctx->delete_empty_mailbox = TRUE;
break;
default:
return FALSE;
}
return TRUE;
}
static struct doveadm_mail_cmd_context *cmd_expunge_alloc(void)
{
struct expunge_cmd_context *ctx;
ctx = doveadm_mail_cmd_alloc(struct expunge_cmd_context);
ctx->ctx.getopt_args = "d";
ctx->ctx.v.parse_arg = cmd_expunge_parse_arg;
ctx->ctx.v.init = cmd_expunge_init;
ctx->ctx.v.run = cmd_expunge_run;
return &ctx->ctx;
}
struct doveadm_mail_cmd cmd_expunge = {
cmd_expunge_alloc, "expunge", "[-d] <search query>"
};