bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2009-2018 Dovecot authors, see the included COPYING file */
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschstatic ARRAY(struct mail_storage_module_hooks) module_hooks = ARRAY_INIT;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschstatic ARRAY(const struct mail_storage_hooks *) internal_hooks = ARRAY_INIT;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* allow calling this even if mail_storage_hooks_init() hasn't been
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch called, because e.g. doveadm plugins could call
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch mail_storage_hooks_add() even though mail storage is never
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch initialized. */
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Boschvoid mail_storage_hooks_add(struct module *module,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* allow adding hooks before mail_storage_hooks_init() */
f883bf3eff62f5d27df5ee9ee664edc38a77937fStephan Boschvoid mail_storage_hooks_add_forced(struct module *module,
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Boschvoid mail_storage_hooks_remove(const struct mail_storage_hooks *hooks)
f883bf3eff62f5d27df5ee9ee664edc38a77937fStephan Bosch const struct mail_storage_module_hooks *module_hook;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch idx = array_foreach_idx(&module_hooks, module_hook);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschvoid mail_storage_hooks_add_internal(const struct mail_storage_hooks *hooks)
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Bosch const struct mail_storage_hooks *const *existing_hooksp;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* make sure we don't add duplicate hooks */
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch array_foreach(&internal_hooks, existing_hooksp)
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschvoid mail_storage_hooks_remove_internal(const struct mail_storage_hooks *hooks)
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch const struct mail_storage_hooks *const *old_hooks;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch idx = array_foreach_idx(&internal_hooks, old_hooks);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschmail_storage_module_hooks_cmp(const struct mail_storage_module_hooks *h1,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch const char *s1 = h1->module->path, *s2 = h2->module->path;
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Bosch const char *p;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschstatic void mail_user_add_plugin_hooks(struct mail_user *user)
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch const struct mail_storage_module_hooks *module_hook;
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Bosch ARRAY(struct mail_storage_module_hooks) tmp_hooks;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* first get all hooks wanted by the user */
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch t_array_init(&tmp_hooks, array_count(&module_hooks));
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch plugins = t_strsplit_spaces(user->set->mail_plugins, ", ");
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch name = module_get_plugin_name(module_hook->module);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* next we have to sort them by the modules' priority (based on name) */
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch array_sort(&tmp_hooks, mail_storage_module_hooks_cmp);
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Bosch /* now that we have them in order, save them to user's hooks */
62aa68310d6f42467ca26880f678173bf1d26a83Stephan Bosch array_count(&tmp_hooks) + array_count(&internal_hooks));
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch array_append(&user->hooks, &module_hook->hooks, 1);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch array_append_array(&user->hooks, &internal_hooks);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschvoid hook_mail_user_created(struct mail_user *user)
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch const struct mail_storage_hooks *const *hooks;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch ctx = hook_build_init((void *)&user->v, sizeof(user->v));
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch if ((*hooks)->mail_user_created != NULL) T_BEGIN {
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschvoid hook_mail_namespace_storage_added(struct mail_namespace *ns)
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch const struct mail_storage_hooks *const *hooks;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch if ((*hooks)->mail_namespace_storage_added != NULL) T_BEGIN {
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschvoid hook_mail_namespaces_created(struct mail_namespace *namespaces)
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch const struct mail_storage_hooks *const *hooks;
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Bosch array_foreach(&namespaces->user->hooks, hooks) {
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch if ((*hooks)->mail_namespaces_created != NULL) T_BEGIN {
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch (*hooks)->mail_namespaces_created(namespaces);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschvoid hook_mail_namespaces_added(struct mail_namespace *namespaces)
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch const struct mail_storage_hooks *const *hooks;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch array_foreach(&namespaces->user->hooks, hooks) {
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch if ((*hooks)->mail_namespaces_added != NULL) T_BEGIN {
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Boschvoid hook_mail_storage_created(struct mail_storage *storage)
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch const struct mail_storage_hooks *const *hooks;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch ctx = hook_build_init((void *)&storage->v, sizeof(storage->v));
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Bosch if ((*hooks)->mail_storage_created != NULL) T_BEGIN {
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschvoid hook_mailbox_list_created(struct mailbox_list *list)
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch const struct mail_storage_hooks *const *hooks;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch ctx = hook_build_init((void *)&list->v, sizeof(list->v));
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Bosch array_foreach(&list->ns->user->hooks, hooks) {
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch if ((*hooks)->mailbox_list_created != NULL) T_BEGIN {
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschvoid hook_mailbox_allocated(struct mailbox *box)
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch const struct mail_storage_hooks *const *hooks;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch ctx = hook_build_init((void *)&box->v, sizeof(box->v));
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch array_foreach(&box->storage->user->hooks, hooks) {
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch if ((*hooks)->mailbox_allocated != NULL) T_BEGIN {
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch const struct mail_storage_hooks *const *hooks;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch array_foreach(&box->storage->user->hooks, hooks) {
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch if ((*hooks)->mailbox_opened != NULL) T_BEGIN {
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch const struct mail_storage_hooks *const *hooks;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch struct mail_private *pmail = (struct mail_private *)mail;
66134fbce11778241ca9f8458ee2a0488a05bde0Stephan Bosch ctx = hook_build_init((void *)&pmail->v, sizeof(pmail->v));
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch array_foreach(&mail->box->storage->user->hooks, hooks) {