push-notification-plugin.c revision 4ee5a85e75d520497bd43dbfcc6fc273f3e57cea
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/* Copyright (c) 2015 Dovecot authors, see the included COPYING file */
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen#define PUSH_NOTIFICATION_CONFIG "push_notification_driver"
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen#define PUSH_NOTIFICATION_CONFIG_OLD "push_notification_backend"
6e1e9e341ffe21a69a23229c2b896d03066a071eTimo Sirainen MODULE_CONTEXT(obj, push_notification_user_module)
6e1e9e341ffe21a69a23229c2b896d03066a071eTimo Sirainenstatic MODULE_CONTEXT_DEFINE_INIT(push_notification_user_module,
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainenstatic struct push_notification_user *puser = NULL;
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainenpush_notification_transaction_create(struct mailbox *box,
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen pool = pool_alloconly_create("push notification transaction", 2048);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen ptxn = p_new(pool, struct push_notification_txn, 1);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen ptxn->puser = PUSH_NOTIFICATION_USER_CONTEXT(ptxn->muser);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen ptxn->trigger = PUSH_NOTIFICATION_EVENT_TRIGGER_NONE;
d22301419109ed4a38351715e6760011421dadecTimo Sirainen /* no notifications for autocreated raw users */
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainen array_foreach_modifiable(&ptxn->puser->drivers, duser) {
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainen dtxn = p_new(pool, struct push_notification_driver_txn, 1);
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainen if ((dtxn->duser->driver->v.begin_txn == NULL) ||
678d0463849ba777106eb7875f27db07a5d8e3dfTimo Sirainen(struct push_notification_txn *ptxn, bool success)
8da095519878426b012058e6f331a669f327f47fTimo Sirainen array_foreach_modifiable(&ptxn->drivers, dtxn) {
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen if ((*dtxn)->duser->driver->v.end_txn != NULL) {
036626b19f14bef582f96e556913ae91b1d67881Timo Sirainen (*dtxn)->duser->driver->v.end_txn(*dtxn, success);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstatic void push_notification_transaction_commit
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen(void *txn, struct mail_transaction_commit_changes *changes)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen struct push_notification_txn *ptxn = (struct push_notification_txn *)txn;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen push_notification_transaction_end(ptxn, TRUE);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstatic void push_notification_mailbox_create(struct mailbox *box)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen ptxn = push_notification_transaction_create(box, NULL);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen push_notification_trigger_mbox_create(ptxn, box, NULL);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen push_notification_transaction_commit(ptxn, NULL);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstatic void push_notification_mailbox_delete(void *txn ATTR_UNUSED,
9261dbf0675204898c6557591c7aa376e23a52b2Timo Sirainen ptxn = push_notification_transaction_create(box, NULL);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen push_notification_trigger_mbox_delete(ptxn, box, NULL);
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen push_notification_transaction_commit(ptxn, NULL);
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainenstatic void push_notification_mailbox_rename(struct mailbox *src,
04b8a90af181cc4c7959266855e8ed50a22ed413Timo Sirainen ptxn = push_notification_transaction_create(dest, NULL);
181c1aff950e6f8e0556f8974e79d0747845ac0fTimo Sirainen push_notification_trigger_mbox_rename(ptxn, src, dest, NULL);
181c1aff950e6f8e0556f8974e79d0747845ac0fTimo Sirainen push_notification_transaction_commit(ptxn, NULL);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstatic void push_notification_mailbox_subscribe(struct mailbox *box,
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen ptxn = push_notification_transaction_create(box, NULL);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen push_notification_trigger_mbox_subscribe(ptxn, box, subscribed, NULL);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen push_notification_transaction_commit(ptxn, NULL);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstatic void push_notification_mail_save(void *txn, struct mail *mail)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen /* POST_SESSION means MTA delivery. */
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen if (mail->box->flags & MAILBOX_FLAG_POST_SESSION) {
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen push_notification_trigger_msg_save_new(ptxn, mail, NULL);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen push_notification_trigger_msg_save_append(ptxn, mail, NULL);
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainenstatic void push_notification_mail_copy(void *txn,
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstatic void push_notification_mail_expunge(void *txn, struct mail *mail)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen (struct push_notification_txn *)txn, mail, NULL);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenpush_notification_mail_update_flags(void *txn, struct mail *mail,
32d69cb0794acf725c47e6d17145ec4eb0cd6e7fTimo Sirainen (struct push_notification_txn *) txn, mail, NULL, old_flags);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenpush_notification_mail_update_keywords(void *txn, struct mail *mail,
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen const char *const *old_keywords)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen (struct push_notification_txn *) txn, mail, NULL, old_keywords);
cd56a23e21f1df3f79648cf07e2f4385e2fadebbTimo Sirainenpush_notification_transaction_begin(struct mailbox_transaction_context *t)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen return push_notification_transaction_create(mailbox_transaction_get_mailbox(t), t);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstatic void push_notification_transaction_rollback(void *txn)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen struct push_notification_txn *ptxn = (struct push_notification_txn *)txn;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen push_notification_transaction_end(ptxn, FALSE);
008c010ed99d23ff601c8aec48c27781dd1c6015Timo Sirainenpush_notification_user_created_init_config(const char *config_name,
a75d470c9223a75801418fcdda258885c36317e0Timo Sirainen const char *env;
8d35582f2577c64517b2341c5d6477c7010e0a0cPhil Carmody unsigned int i;
008c010ed99d23ff601c8aec48c27781dd1c6015Timo Sirainen for (i = 2;; i++) {
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen env = mail_user_plugin_getenv(user, str_c(root_name));
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen if (push_notification_driver_init(user, env, puser->pool, &duser) < 0) {
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen // Add driver.
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstatic void push_notification_user_created_init(struct mail_user *user)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen pool = pool_alloconly_create("push notification plugin", 1024);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen puser = p_new(pool, struct push_notification_user, 1);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen push_notification_user_created_init_config(PUSH_NOTIFICATION_CONFIG, user,
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen /* Support old configuration (it was available at time initial OX
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen * driver was first released. */
008c010ed99d23ff601c8aec48c27781dd1c6015Timo Sirainen push_notification_user_created_init_config(PUSH_NOTIFICATION_CONFIG_OLD,
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstatic void push_notification_user_created(struct mail_user *user)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen MODULE_CONTEXT_SET(user, push_notification_user_module, puser);
ba3d9eeb0bec6ed8465d68fa2480ad085559b580Timo Sirainen/* Plugin interface. */
ba3d9eeb0bec6ed8465d68fa2480ad085559b580Timo Sirainenconst char *push_notification_plugin_version = DOVECOT_ABI_VERSION;
93c0be0b2c92501de882c2daf79105130501c988Timo Sirainenconst char *push_notification_plugin_dependencies[] = { "notify", NULL };
93c0be0b2c92501de882c2daf79105130501c988Timo Sirainenextern struct push_notification_driver push_notification_driver_dlog;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenextern struct push_notification_driver push_notification_driver_ox;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstatic struct notify_context *push_notification_ctx;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstatic const struct notify_vfuncs push_notification_vfuncs = {
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen /* Mailbox Events */
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen .mailbox_create = push_notification_mailbox_create,
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen .mailbox_delete_commit = push_notification_mailbox_delete,
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen .mailbox_rename = push_notification_mailbox_rename,
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen .mailbox_set_subscribed = push_notification_mailbox_subscribe,
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen /* Mail Events */
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen .mail_expunge = push_notification_mail_expunge,
008c010ed99d23ff601c8aec48c27781dd1c6015Timo Sirainen .mail_update_flags = push_notification_mail_update_flags,
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen .mail_update_keywords = push_notification_mail_update_keywords,
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen .mail_transaction_begin = push_notification_transaction_begin,
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen .mail_transaction_commit = push_notification_transaction_commit,
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen .mail_transaction_rollback = push_notification_transaction_rollback
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainenstatic struct mail_storage_hooks push_notification_storage_hooks = {
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen .mail_user_created = push_notification_user_created
d22301419109ed4a38351715e6760011421dadecTimo Sirainenvoid push_notification_plugin_init(struct module *module)
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen push_notification_ctx = notify_register(&push_notification_vfuncs);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen mail_storage_hooks_add(module, &push_notification_storage_hooks);
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen push_notification_driver_register(&push_notification_driver_dlog);
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen push_notification_driver_register(&push_notification_driver_ox);
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen push_notification_event_register_rfc5423_events();
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen array_foreach_modifiable(&puser->drivers, duser) {
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen push_notification_driver_unregister(&push_notification_driver_dlog);
ca98892a6b8a30ffc1fe26fcf02c7d59e3204e7eTimo Sirainen push_notification_driver_unregister(&push_notification_driver_ox);