imap-expunge.c revision 1807ae2cbeb06f804d5b9af3e3d920417c3d4991
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync/* Copyright (c) 2003-2008 Dovecot authors, see the included COPYING file */
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync#include "common.h"
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync#include "mail-storage.h"
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync#include "mail-search.h"
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync#include "imap-expunge.h"
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync
deb4998ba50060c48cce222fd18a8eed053918d7vboxsyncint imap_expunge(struct mailbox *box, struct mail_search_arg *next_search_arg)
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync{
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync struct mail_search_context *ctx;
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync struct mailbox_transaction_context *t;
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync struct mail *mail;
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync struct mail_search_arg search_arg;
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync bool expunges = FALSE;
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync if (mailbox_is_readonly(box)) {
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync /* silently ignore */
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync return 0;
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync }
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync memset(&search_arg, 0, sizeof(search_arg));
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync search_arg.type = SEARCH_FLAGS;
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync search_arg.value.flags = MAIL_DELETED;
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync search_arg.next = next_search_arg;
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync /* Refresh the flags so we'll expunge all messages marked as \Deleted
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync by any session. */
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync t = mailbox_transaction_begin(box, MAILBOX_TRANSACTION_FLAG_REFRESH);
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync ctx = mailbox_search_init(t, NULL, &search_arg, NULL);
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync mail = mail_alloc(t, 0, NULL);
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync while (mailbox_search_next(ctx, mail) > 0) {
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync mail_expunge(mail);
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync expunges = TRUE;
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync }
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync mail_free(&mail);
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync if (mailbox_search_deinit(&ctx) < 0) {
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync mailbox_transaction_rollback(&t);
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync return -1;
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync } else {
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync if (mailbox_transaction_commit(&t) < 0)
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync return -1;
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync }
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync return expunges ? 1 : 0;
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync}
deb4998ba50060c48cce222fd18a8eed053918d7vboxsync