imap-expunge.c revision e5ee67f18b03015c88b579c8c1f17ebe6ce19b76
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
345212e8f61ebf14ff4f80df26df9e655eb5121eTimo 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;
e5ee67f18b03015c88b579c8c1f17ebe6ce19b76Timo Sirainen enum mailbox_sync_flags flags;
25fdbae285d682b7e54ac103ccfb95e032b6ff1fTimo Sirainen int 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
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen t = mailbox_transaction_begin(box, FALSE);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ctx = mailbox_search_init(t, NULL, &search_arg, NULL, 0, NULL);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (ctx == NULL)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen failed = TRUE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen else {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen while ((mail = mailbox_search_next(ctx)) != NULL) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (mail->expunge(mail) < 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen failed = TRUE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen break;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
25fdbae285d682b7e54ac103ccfb95e032b6ff1fTimo Sirainen }
25fdbae285d682b7e54ac103ccfb95e032b6ff1fTimo Sirainen }
25fdbae285d682b7e54ac103ccfb95e032b6ff1fTimo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (mailbox_search_deinit(ctx) < 0)
25fdbae285d682b7e54ac103ccfb95e032b6ff1fTimo Sirainen return FALSE;
25fdbae285d682b7e54ac103ccfb95e032b6ff1fTimo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (failed)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mailbox_transaction_rollback(t);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen else {
e5ee67f18b03015c88b579c8c1f17ebe6ce19b76Timo Sirainen flags = MAILBOX_SYNC_FLAG_FULL_READ |
e5ee67f18b03015c88b579c8c1f17ebe6ce19b76Timo Sirainen MAILBOX_SYNC_FLAG_FULL_WRITE;
e5ee67f18b03015c88b579c8c1f17ebe6ce19b76Timo Sirainen if (mailbox_transaction_commit(t, flags) < 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen failed = TRUE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
25fdbae285d682b7e54ac103ccfb95e032b6ff1fTimo Sirainen return !failed;
25fdbae285d682b7e54ac103ccfb95e032b6ff1fTimo Sirainen}