quota-clone-plugin.c revision 0d1d485939b9b7f1a0f588aea40c504e3d26e35b
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/* Copyright (c) 2015-2016 Dovecot authors, see the included COPYING file */
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen#define DICT_QUOTA_CLONE_PATH DICT_PATH_PRIVATE"quota/"
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen#define DICT_QUOTA_CLONE_BYTES_PATH DICT_QUOTA_CLONE_PATH"storage"
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen#define DICT_QUOTA_CLONE_COUNT_PATH DICT_QUOTA_CLONE_PATH"messages"
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen MODULE_CONTEXT(obj, quota_clone_storage_module)
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainenstatic MODULE_CONTEXT_DEFINE_INIT(quota_clone_user_module,
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainenstatic MODULE_CONTEXT_DEFINE_INIT(quota_clone_storage_module,
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainenstatic void quota_clone_flush(struct mailbox *box)
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen struct quota_clone_mailbox *qbox = QUOTA_CLONE_CONTEXT(box);
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen /* we'll clone the first quota root */
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen /* no quota roots defined for this mailbox - ignore */
d861bc0977b229cdaeb3fb77377e2a2bd9d40d3dTimo Sirainen /* get new values first */
d861bc0977b229cdaeb3fb77377e2a2bd9d40d3dTimo Sirainen if (quota_get_resource(root, "", QUOTA_NAME_STORAGE_BYTES,
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen i_error("quota_clone_plugin: Failed to lookup current quota bytes");
d861bc0977b229cdaeb3fb77377e2a2bd9d40d3dTimo Sirainen if (quota_get_resource(root, "", QUOTA_NAME_MESSAGES,
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen i_error("quota_clone_plugin: Failed to lookup current quota count");
d861bc0977b229cdaeb3fb77377e2a2bd9d40d3dTimo Sirainen /* then update them */
d861bc0977b229cdaeb3fb77377e2a2bd9d40d3dTimo Sirainen t_strdup_printf("%llu", (unsigned long long)bytes_value));
d861bc0977b229cdaeb3fb77377e2a2bd9d40d3dTimo Sirainen t_strdup_printf("%llu", (unsigned long long)count_value));
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen if (dict_transaction_commit(&trans, &error) < 0)
661998e2ccd772ad92a9d4a75cb712692a8c94b3Timo Sirainen i_error("quota_clone_plugin: Failed to commit dict update: %s", error);
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainenstatic int quota_clone_save_finish(struct mail_save_context *ctx)
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen return qbox->module_ctx.super.save_finish(ctx);
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainenquota_clone_copy(struct mail_save_context *ctx, struct mail *mail)
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen return qbox->module_ctx.super.copy(ctx, mail);
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainenquota_clone_mailbox_sync_notify(struct mailbox *box, uint32_t uid,
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen struct quota_clone_mailbox *qbox = QUOTA_CLONE_CONTEXT(box);
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen if (qbox->module_ctx.super.sync_notify != NULL)
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen qbox->module_ctx.super.sync_notify(box, uid, sync_type);
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainenstatic void quota_clone_mailbox_close(struct mailbox *box)
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen struct quota_clone_mailbox *qbox = QUOTA_CLONE_CONTEXT(box);
8705e45564a2e87d32bd825e0e997a8177846f77Timo Sirainen /* recursing back from quota recalculation */
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainenstatic void quota_clone_mailbox_allocated(struct mailbox *box)
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen qbox = p_new(box->pool, struct quota_clone_mailbox, 1);
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen v->sync_notify = quota_clone_mailbox_sync_notify;
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen MODULE_CONTEXT_SET(box, quota_clone_storage_module, qbox);
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainenstatic void quota_clone_mail_user_deinit(struct mail_user *user)
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen struct quota_clone_user *quser = QUOTA_CLONE_USER_CONTEXT(user);
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainenstatic void quota_clone_mail_user_created(struct mail_user *user)
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen uri = mail_user_plugin_getenv(user, "quota_clone_dict");
0d1d485939b9b7f1a0f588aea40c504e3d26e35bJ. Nick Koston i_debug("The quota_clone_dict setting is missing from configuration");
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen (void)mail_user_get_home(user, &dict_set.home_dir);
20e04227229970d148801c507946666e2a9bd838Timo Sirainen if (dict_init(uri, &dict_set, &dict, &error) < 0) {
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen i_error("quota_clone_dict: Failed to initialize '%s': %s",
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen quser = p_new(user->pool, struct quota_clone_user, 1);
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen MODULE_CONTEXT_SET(user, quota_clone_user_module, quser);
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainenstatic struct mail_storage_hooks quota_clone_mail_storage_hooks = {
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen .mailbox_allocated = quota_clone_mailbox_allocated,
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen .mail_user_created = quota_clone_mail_user_created
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainenvoid quota_clone_plugin_init(struct module *module ATTR_UNUSED)
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen mail_storage_hooks_add(module, "a_clone_mail_storage_hooks);
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen mail_storage_hooks_remove("a_clone_mail_storage_hooks);
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainenconst char *quota_clone_plugin_dependencies[] = { "quota", NULL };