imap-expunge.c revision bcb4e51a409d94ae670de96afb8483a4f7855294
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* Copyright (c) 2003-2018 Dovecot authors, see the included COPYING file */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "imap-common.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "mail-storage.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "mail-search-build.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "imap-expunge.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenint imap_expunge(struct mailbox *box, struct mail_search_arg *next_search_arg,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen unsigned int *expunged_count)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct mail_search_context *ctx;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct mailbox_transaction_context *t;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct mail *mail;
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen struct mail_search_args *search_args;
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen bool expunges = FALSE;
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen if (mailbox_is_readonly(box)) {
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen /* silently ignore */
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen return 0;
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen }
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen search_args = mail_search_build_init();
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen search_args->args = p_new(search_args->pool, struct mail_search_arg, 1);
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen search_args->args->type = SEARCH_FLAGS;
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen search_args->args->value.flags = MAIL_DELETED;
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen search_args->args->next = next_search_arg;
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen /* Refresh the flags so we'll expunge all messages marked as \Deleted
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen by any session. */
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen t = mailbox_transaction_begin(box, MAILBOX_TRANSACTION_FLAG_REFRESH,
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen "EXPUNGE");
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ctx = mailbox_search_init(t, search_args, NULL, 0, NULL);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_search_args_unref(&search_args);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen while (mailbox_search_next(ctx, &mail)) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen *expunged_count += 1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_expunge(mail);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen expunges = TRUE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (mailbox_search_deinit(&ctx) < 0) {
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen mailbox_transaction_rollback(&t);
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen return -1;
9aa52288a4b53186d81b0ec9afa7d9e0a8ee8753Timo Sirainen } else {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (mailbox_transaction_commit(&t) < 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return -1;
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen }
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen return expunges ? 1 : 0;
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen}
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen