imap-expunge.c revision 73b50eecfc31750a312e2f940023f522eb07178c
25fdbae285d682b7e54ac103ccfb95e032b6ff1fTimo Sirainen/* Copyright (C) 2003 Timo Sirainen */
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
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainenbool 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;
e5ee67f18b03015c88b579c8c1f17ebe6ce19b76Timo Sirainen enum mailbox_sync_flags flags;
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen bool failed;
25fdbae285d682b7e54ac103ccfb95e032b6ff1fTimo Sirainen
06b3ce71a592e5575b5e1d4f412bd364dc6da69dTimo Sirainen if (mailbox_is_readonly(box)) {
06b3ce71a592e5575b5e1d4f412bd364dc6da69dTimo Sirainen /* silently ignore */
06b3ce71a592e5575b5e1d4f412bd364dc6da69dTimo Sirainen return TRUE;
06b3ce71a592e5575b5e1d4f412bd364dc6da69dTimo Sirainen }
06b3ce71a592e5575b5e1d4f412bd364dc6da69dTimo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen memset(&search_arg, 0, sizeof(search_arg));
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen search_arg.type = SEARCH_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);
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen while (mailbox_search_next(ctx, mail) > 0)
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen mail_expunge(mail);
abd162fee549940f212f20fc1d7442d386cf8c3aTimo Sirainen mail_free(&mail);
25fdbae285d682b7e54ac103ccfb95e032b6ff1fTimo Sirainen
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen if (mailbox_search_deinit(&ctx) < 0) {
7774ddcab3661f24c473bd74ad35f717f8bd7110Timo Sirainen failed = TRUE;
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen mailbox_transaction_rollback(&t);
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen } else {
e5ee67f18b03015c88b579c8c1f17ebe6ce19b76Timo Sirainen flags = MAILBOX_SYNC_FLAG_FULL_READ |
e5ee67f18b03015c88b579c8c1f17ebe6ce19b76Timo Sirainen MAILBOX_SYNC_FLAG_FULL_WRITE;
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen failed = mailbox_transaction_commit(&t, flags) < 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
25fdbae285d682b7e54ac103ccfb95e032b6ff1fTimo Sirainen return !failed;
25fdbae285d682b7e54ac103ccfb95e032b6ff1fTimo Sirainen}