imap-expunge.c revision 5f90a4f0f2d1262e2d5167a79435891ee2972726
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"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "mail-search.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;
e5ee67f18b03015c88b579c8c1f17ebe6ce19b76Timo Sirainen struct mail_search_arg search_arg;
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
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen memset(&search_arg, 0, sizeof(search_arg));
41da195263f4c7f0eaf58f3ecee4d9ceb92ae8c1Timo Sirainen search_arg.type = SEARCH_FLAGS;
41da195263f4c7f0eaf58f3ecee4d9ceb92ae8c1Timo Sirainen search_arg.value.flags = MAIL_DELETED;
345212e8f61ebf14ff4f80df26df9e655eb5121eTimo Sirainen search_arg.next = next_search_arg;
25fdbae285d682b7e54ac103ccfb95e032b6ff1fTimo Sirainen
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen t = mailbox_transaction_begin(box, 0);
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen ctx = mailbox_search_init(t, NULL, &search_arg, NULL);
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}