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