quota-clone-plugin.c revision 1f2f38f518ea14d1042c98ab039e6df053f7b285
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen/* Copyright (c) 2015 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 */
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen /* update bytes */
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen ret = quota_get_resource(root, "", QUOTA_NAME_STORAGE_BYTES,
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen i_error("quota_clone_plugin: Failed to lookup current quota bytes");
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen t_strdup_printf("%llu", (unsigned long long)value));
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen /* update messages */
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen ret = quota_get_resource(root, "", QUOTA_NAME_MESSAGES,
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen i_error("quota_clone_plugin: Failed to lookup current quota count");
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen t_strdup_printf("%llu", (unsigned long long)value));
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen i_error("quota_clone_plugin: Failed to commit dict update");
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);
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");
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen i_error("The quota_clone_dict setting is missing from configuration");
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen (void)mail_user_get_home(user, &dict_set.home_dir);
1f2f38f518ea14d1042c98ab039e6df053f7b285Timo Sirainen if (dict_init_full(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 };