imap-expunge.c revision abd162fee549940f212f20fc1d7442d386cf8c3a
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;
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen bool failed = FALSE;
25fdbae285d682b7e54ac103ccfb95e032b6ff1fTimo 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);
abd162fee549940f212f20fc1d7442d386cf8c3aTimo Sirainen while (mailbox_search_next(ctx, mail) > 0) {
abd162fee549940f212f20fc1d7442d386cf8c3aTimo Sirainen if (mail_expunge(mail) < 0) {
abd162fee549940f212f20fc1d7442d386cf8c3aTimo Sirainen failed = TRUE;
abd162fee549940f212f20fc1d7442d386cf8c3aTimo Sirainen break;
25fdbae285d682b7e54ac103ccfb95e032b6ff1fTimo Sirainen }
25fdbae285d682b7e54ac103ccfb95e032b6ff1fTimo Sirainen }
abd162fee549940f212f20fc1d7442d386cf8c3aTimo Sirainen mail_free(&mail);
25fdbae285d682b7e54ac103ccfb95e032b6ff1fTimo Sirainen
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen if (mailbox_search_deinit(&ctx) < 0)
25fdbae285d682b7e54ac103ccfb95e032b6ff1fTimo Sirainen return FALSE;
25fdbae285d682b7e54ac103ccfb95e032b6ff1fTimo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (failed)
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen mailbox_transaction_rollback(&t);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen else {
e5ee67f18b03015c88b579c8c1f17ebe6ce19b76Timo Sirainen flags = MAILBOX_SYNC_FLAG_FULL_READ |
e5ee67f18b03015c88b579c8c1f17ebe6ce19b76Timo Sirainen MAILBOX_SYNC_FLAG_FULL_WRITE;
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen if (mailbox_transaction_commit(&t, flags) < 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen failed = TRUE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
25fdbae285d682b7e54ac103ccfb95e032b6ff1fTimo Sirainen return !failed;
25fdbae285d682b7e54ac103ccfb95e032b6ff1fTimo Sirainen}