imap-expunge.c revision c4b376dd6e0c423006d7ac83a39253bcaf8e7c47
76b43e4417bab52e913da39b5f5bc2a130d3f149Timo Sirainen/* Copyright (c) 2003-2008 Dovecot authors, see the included COPYING file */
25fdbae285d682b7e54ac103ccfb95e032b6ff1fTimo Sirainen
25fdbae285d682b7e54ac103ccfb95e032b6ff1fTimo Sirainen#include "common.h"
25fdbae285d682b7e54ac103ccfb95e032b6ff1fTimo Sirainen#include "mail-storage.h"
c4b376dd6e0c423006d7ac83a39253bcaf8e7c47Timo Sirainen#include "mail-search-build.h"
25fdbae285d682b7e54ac103ccfb95e032b6ff1fTimo Sirainen#include "imap-expunge.h"
25fdbae285d682b7e54ac103ccfb95e032b6ff1fTimo Sirainen
5f90a4f0f2d1262e2d5167a79435891ee2972726Timo Sirainenint imap_expunge(struct mailbox *box, struct mail_search_arg *next_search_arg)
25fdbae285d682b7e54ac103ccfb95e032b6ff1fTimo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct mail_search_context *ctx;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct mailbox_transaction_context *t;
25fdbae285d682b7e54ac103ccfb95e032b6ff1fTimo Sirainen struct mail *mail;
c4b376dd6e0c423006d7ac83a39253bcaf8e7c47Timo Sirainen struct mail_search_args *search_args;
5f90a4f0f2d1262e2d5167a79435891ee2972726Timo Sirainen bool expunges = FALSE;
25fdbae285d682b7e54ac103ccfb95e032b6ff1fTimo Sirainen
06b3ce71a592e5575b5e1d4f412bd364dc6da69dTimo Sirainen if (mailbox_is_readonly(box)) {
06b3ce71a592e5575b5e1d4f412bd364dc6da69dTimo Sirainen /* silently ignore */
5f90a4f0f2d1262e2d5167a79435891ee2972726Timo Sirainen return 0;
06b3ce71a592e5575b5e1d4f412bd364dc6da69dTimo Sirainen }
06b3ce71a592e5575b5e1d4f412bd364dc6da69dTimo Sirainen
c4b376dd6e0c423006d7ac83a39253bcaf8e7c47Timo Sirainen search_args = mail_search_build_init();
c4b376dd6e0c423006d7ac83a39253bcaf8e7c47Timo Sirainen search_args->args = p_new(search_args->pool, struct mail_search_arg, 1);
c4b376dd6e0c423006d7ac83a39253bcaf8e7c47Timo Sirainen search_args->args->type = SEARCH_FLAGS;
c4b376dd6e0c423006d7ac83a39253bcaf8e7c47Timo Sirainen search_args->args->value.flags = MAIL_DELETED;
c4b376dd6e0c423006d7ac83a39253bcaf8e7c47Timo Sirainen search_args->args->next = next_search_arg;
25fdbae285d682b7e54ac103ccfb95e032b6ff1fTimo Sirainen
1807ae2cbeb06f804d5b9af3e3d920417c3d4991Timo Sirainen /* Refresh the flags so we'll expunge all messages marked as \Deleted
1807ae2cbeb06f804d5b9af3e3d920417c3d4991Timo Sirainen by any session. */
1807ae2cbeb06f804d5b9af3e3d920417c3d4991Timo Sirainen t = mailbox_transaction_begin(box, MAILBOX_TRANSACTION_FLAG_REFRESH);
c4b376dd6e0c423006d7ac83a39253bcaf8e7c47Timo Sirainen ctx = mailbox_search_init(t, search_args, NULL);
c4b376dd6e0c423006d7ac83a39253bcaf8e7c47Timo Sirainen mail_search_args_unref(&search_args);
abd162fee549940f212f20fc1d7442d386cf8c3aTimo Sirainen
abd162fee549940f212f20fc1d7442d386cf8c3aTimo Sirainen mail = mail_alloc(t, 0, NULL);
5f90a4f0f2d1262e2d5167a79435891ee2972726Timo Sirainen while (mailbox_search_next(ctx, mail) > 0) {
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen mail_expunge(mail);
5f90a4f0f2d1262e2d5167a79435891ee2972726Timo Sirainen expunges = TRUE;
5f90a4f0f2d1262e2d5167a79435891ee2972726Timo Sirainen }
abd162fee549940f212f20fc1d7442d386cf8c3aTimo Sirainen mail_free(&mail);
25fdbae285d682b7e54ac103ccfb95e032b6ff1fTimo Sirainen
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen if (mailbox_search_deinit(&ctx) < 0) {
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen mailbox_transaction_rollback(&t);
5f90a4f0f2d1262e2d5167a79435891ee2972726Timo Sirainen return -1;
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen } else {
5f90a4f0f2d1262e2d5167a79435891ee2972726Timo Sirainen if (mailbox_transaction_commit(&t) < 0)
5f90a4f0f2d1262e2d5167a79435891ee2972726Timo Sirainen return -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
5f90a4f0f2d1262e2d5167a79435891ee2972726Timo Sirainen return expunges ? 1 : 0;
25fdbae285d682b7e54ac103ccfb95e032b6ff1fTimo Sirainen}