bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2007-2018 Dovecot authors, see the included COPYING file */
bd63b5b860658b01b1f46f26d406e1e4a9dc019aTimo Sirainenconst char *snarf_plugin_version = DOVECOT_ABI_VERSION;
a6e79dffa06db28bcfad9c1e5fc819c48172d5deTimo Sirainenstatic MODULE_CONTEXT_DEFINE_INIT(snarf_storage_module,
a6e79dffa06db28bcfad9c1e5fc819c48172d5deTimo Sirainenstatic int snarf(struct mailbox *srcbox, struct mailbox *destbox)
a6e79dffa06db28bcfad9c1e5fc819c48172d5deTimo Sirainen struct mailbox_transaction_context *src_trans, *dest_trans;
4abfd8fcb814730b17cede56d8bd10fa7529361fTimo Sirainen /* make sure the destination mailbox has been opened.
4abfd8fcb814730b17cede56d8bd10fa7529361fTimo Sirainen note that this locks the mailbox. */
a6e79dffa06db28bcfad9c1e5fc819c48172d5deTimo Sirainen if (mailbox_sync(srcbox, MAILBOX_SYNC_FLAG_FULL_READ) < 0)
0dab9cb35a976c49b28a11e28d5570f5191f1a7aMartti Rannanjärvi src_trans = mailbox_transaction_begin(srcbox, 0, "snarf src_trans");
a6e79dffa06db28bcfad9c1e5fc819c48172d5deTimo Sirainen dest_trans = mailbox_transaction_begin(destbox,
0dab9cb35a976c49b28a11e28d5570f5191f1a7aMartti Rannanjärvi "snarf dest_trans");
eef4ba0cc3e78f8c26804c1c9251a76580a41f0cTimo Sirainen search_ctx = mailbox_search_init(src_trans, search_args, NULL,
eef4ba0cc3e78f8c26804c1c9251a76580a41f0cTimo Sirainen while (mailbox_search_next(search_ctx, &mail)) {
a6e79dffa06db28bcfad9c1e5fc819c48172d5deTimo Sirainen if (mailbox_copy(&save_ctx, mail) < 0 && !mail->expunged) {
a6e79dffa06db28bcfad9c1e5fc819c48172d5deTimo Sirainen /* if we failed because of out of disk space, just
a6e79dffa06db28bcfad9c1e5fc819c48172d5deTimo Sirainen move those messages we managed to move so far. */
a6e79dffa06db28bcfad9c1e5fc819c48172d5deTimo Sirainen /* commit the copied messages to the destination mailbox. if we crash
a6e79dffa06db28bcfad9c1e5fc819c48172d5deTimo Sirainen between that and between expunging the messages from the source
a6e79dffa06db28bcfad9c1e5fc819c48172d5deTimo Sirainen mailbox, we're left with duplicates. */
a6e79dffa06db28bcfad9c1e5fc819c48172d5deTimo Sirainen else if (mailbox_transaction_commit(&dest_trans) < 0)
a6e79dffa06db28bcfad9c1e5fc819c48172d5deTimo Sirainen if (mailbox_transaction_commit(&src_trans) < 0)
a6e79dffa06db28bcfad9c1e5fc819c48172d5deTimo Sirainensnarf_sync_init(struct mailbox *box, enum mailbox_sync_flags flags)
ff9fabd3e38f5dd5e5982d33cd216da7560a99f4Aki Tuomi struct snarf_mailbox *sbox = SNARF_CONTEXT_REQUIRE(box);
4abfd8fcb814730b17cede56d8bd10fa7529361fTimo Sirainen /* close the mailbox so that we don't have to keep it locked */
a6e79dffa06db28bcfad9c1e5fc819c48172d5deTimo Sirainen return sbox->module_ctx.super.sync_init(box, flags);
a6e79dffa06db28bcfad9c1e5fc819c48172d5deTimo Sirainenstatic void snarf_mailbox_free(struct mailbox *box)
ff9fabd3e38f5dd5e5982d33cd216da7560a99f4Aki Tuomi struct snarf_mailbox *sbox = SNARF_CONTEXT_REQUIRE(box);
9bd607718368ffb39bcfbc82010073364901c5a2Timo Sirainensnarf_box_find(struct mail_user *user, struct mailbox_list **list_r,
9bd607718368ffb39bcfbc82010073364901c5a2Timo Sirainen const char **name_r)
9bd607718368ffb39bcfbc82010073364901c5a2Timo Sirainen snarf_name = mail_user_plugin_getenv(user, "snarf");
636f017be100bce67d66fd3ae1544a47681efd33Timo Sirainen i_error("snarf: Mailbox name not UTF-8: %s", snarf_name);
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen snarf_ns = mail_namespace_find(user->namespaces, snarf_name);
a6e79dffa06db28bcfad9c1e5fc819c48172d5deTimo Sirainenstatic void snarf_mailbox_allocated(struct mailbox *box)
9bd607718368ffb39bcfbc82010073364901c5a2Timo Sirainen struct snarf_mail_storage *sstorage = SNARF_CONTEXT(box->storage);
040b26a36509e180a02acaf1cbc9db5d8c153c82Aki Tuomi if (sstorage == NULL || sstorage->snarfing_disabled)
9bd607718368ffb39bcfbc82010073364901c5a2Timo Sirainen if (!snarf_box_find(box->storage->user, &snarf_list, &snarf_name))
a6e79dffa06db28bcfad9c1e5fc819c48172d5deTimo Sirainen sbox = p_new(box->pool, struct snarf_mailbox, 1);
4abfd8fcb814730b17cede56d8bd10fa7529361fTimo Sirainen sbox->snarf_box = mailbox_alloc(snarf_list, snarf_name,
a6e79dffa06db28bcfad9c1e5fc819c48172d5deTimo Sirainen MODULE_CONTEXT_SET(box, snarf_storage_module, sbox);
9bd607718368ffb39bcfbc82010073364901c5a2Timo Sirainenstatic struct mailbox *
9bd607718368ffb39bcfbc82010073364901c5a2Timo Sirainensnarf_mailbox_alloc(struct mail_storage *storage,
ff9fabd3e38f5dd5e5982d33cd216da7560a99f4Aki Tuomi struct snarf_mail_storage *sstorage = SNARF_CONTEXT_REQUIRE(storage);
9bd607718368ffb39bcfbc82010073364901c5a2Timo Sirainen struct mail_namespace *ns = mailbox_list_get_namespace(list);
9bd607718368ffb39bcfbc82010073364901c5a2Timo Sirainen (ns->flags & NAMESPACE_FLAG_INBOX_USER) != 0) {
9bd607718368ffb39bcfbc82010073364901c5a2Timo Sirainen "stat(%s) failed: %m",
9bd607718368ffb39bcfbc82010073364901c5a2Timo Sirainen /* use the snarf box as our real INBOX */
9bd607718368ffb39bcfbc82010073364901c5a2Timo Sirainen if (snarf_box_find(storage->user, &snarf_list,
9bd607718368ffb39bcfbc82010073364901c5a2Timo Sirainensnarf_mail_storage_create(struct mail_storage *storage, const char *path)
9bd607718368ffb39bcfbc82010073364901c5a2Timo Sirainen struct mail_storage_vfuncs *v = storage->vlast;
9bd607718368ffb39bcfbc82010073364901c5a2Timo Sirainen path = mail_user_home_expand(storage->user, path);
9bd607718368ffb39bcfbc82010073364901c5a2Timo Sirainen mstorage = p_new(storage->pool, struct snarf_mail_storage, 1);
9bd607718368ffb39bcfbc82010073364901c5a2Timo Sirainen mstorage->snarf_path = p_strdup(storage->pool, path);
9bd607718368ffb39bcfbc82010073364901c5a2Timo Sirainen MODULE_CONTEXT_SET(storage, snarf_storage_module, mstorage);
9bd607718368ffb39bcfbc82010073364901c5a2Timo Sirainenstatic void snarf_mail_storage_created(struct mail_storage *storage)
9bd607718368ffb39bcfbc82010073364901c5a2Timo Sirainen /* snarfing is optional: do it only if the path specified
9bd607718368ffb39bcfbc82010073364901c5a2Timo Sirainen by mbox_snarf exists */
9bd607718368ffb39bcfbc82010073364901c5a2Timo Sirainen path = mail_user_plugin_getenv(storage->user, "mbox_snarf");
a6e79dffa06db28bcfad9c1e5fc819c48172d5deTimo Sirainenstatic struct mail_storage_hooks snarf_mail_storage_hooks = {
9bd607718368ffb39bcfbc82010073364901c5a2Timo Sirainen .mail_storage_created = snarf_mail_storage_created
a6e79dffa06db28bcfad9c1e5fc819c48172d5deTimo Sirainen mail_storage_hooks_add(module, &snarf_mail_storage_hooks);