push-notification-plugin.c revision 4ee5a85e75d520497bd43dbfcc6fc273f3e57cea
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/* Copyright (c) 2015 Dovecot authors, see the included COPYING file */
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen#include "lib.h"
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen#include "array.h"
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen#include "mail-namespace.h"
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen#include "mail-storage.h"
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen#include "mail-storage-private.h"
16f816d3f3c32ae3351834253f52ddd0212bcbf3Timo Sirainen#include "notify-plugin.h"
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen#include "str.h"
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen#include "push-notification-drivers.h"
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen#include "push-notification-events.h"
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen#include "push-notification-events-rfc5423.h"
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen#include "push-notification-plugin.h"
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen#include "push-notification-triggers.h"
181c1aff950e6f8e0556f8974e79d0747845ac0fTimo Sirainen#include "push-notification-txn-mbox.h"
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen#include "push-notification-txn-msg.h"
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen#define PUSH_NOTIFICATION_CONFIG "push_notification_driver"
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen#define PUSH_NOTIFICATION_CONFIG_OLD "push_notification_backend"
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen#define PUSH_NOTIFICATION_USER_CONTEXT(obj) \
6e1e9e341ffe21a69a23229c2b896d03066a071eTimo Sirainen MODULE_CONTEXT(obj, push_notification_user_module)
6e1e9e341ffe21a69a23229c2b896d03066a071eTimo Sirainenstatic MODULE_CONTEXT_DEFINE_INIT(push_notification_user_module,
6e1e9e341ffe21a69a23229c2b896d03066a071eTimo Sirainen &mail_user_module_register);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainenstatic struct push_notification_user *puser = NULL;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstatic struct push_notification_txn *
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainenpush_notification_transaction_create(struct mailbox *box,
a75d470c9223a75801418fcdda258885c36317e0Timo Sirainen struct mailbox_transaction_context *t)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen{
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen struct push_notification_driver_txn *dtxn;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen struct push_notification_driver_user **duser;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen pool_t pool;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen struct push_notification_txn *ptxn;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen struct mail_storage *storage;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen pool = pool_alloconly_create("push notification transaction", 2048);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen ptxn = p_new(pool, struct push_notification_txn, 1);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen ptxn->mbox = box;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen storage = mailbox_get_storage(box);
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen ptxn->muser = mail_storage_get_user(storage);
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen ptxn->pool = pool;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen ptxn->puser = PUSH_NOTIFICATION_USER_CONTEXT(ptxn->muser);
785d9cca224d33ca3938e9166784f6483e8a27d7Timo Sirainen ptxn->t = t;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen ptxn->trigger = PUSH_NOTIFICATION_EVENT_TRIGGER_NONE;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen p_array_init(&ptxn->drivers, pool, 4);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen if (storage->user->autocreated &&
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen (strcmp(storage->name, "raw") == 0)) {
d22301419109ed4a38351715e6760011421dadecTimo Sirainen /* no notifications for autocreated raw users */
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen return ptxn;
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainen }
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainen
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainen array_foreach_modifiable(&ptxn->puser->drivers, duser) {
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainen dtxn = p_new(pool, struct push_notification_driver_txn, 1);
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainen dtxn->duser = *duser;
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainen dtxn->ptxn = ptxn;
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainen
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainen if ((dtxn->duser->driver->v.begin_txn == NULL) ||
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainen dtxn->duser->driver->v.begin_txn(dtxn)) {
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen array_append(&ptxn->drivers, &dtxn, 1);
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen }
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainen }
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainen
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainen return ptxn;
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainen}
0449e50c99f3fab4485f9b983e3936aaa003c79aTimo Sirainen
91dca97b367c54a139c268b56a0c67f564bd9197Timo Sirainenstatic void push_notification_transaction_end
678d0463849ba777106eb7875f27db07a5d8e3dfTimo Sirainen(struct push_notification_txn *ptxn, bool success)
6e1e9e341ffe21a69a23229c2b896d03066a071eTimo Sirainen{
6e1e9e341ffe21a69a23229c2b896d03066a071eTimo Sirainen struct push_notification_driver_txn **dtxn;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen
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);
036626b19f14bef582f96e556913ae91b1d67881Timo Sirainen }
036626b19f14bef582f96e556913ae91b1d67881Timo Sirainen }
6e1e9e341ffe21a69a23229c2b896d03066a071eTimo Sirainen
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainen pool_unref(&ptxn->pool);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen}
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstatic void push_notification_transaction_commit
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen(void *txn, struct mail_transaction_commit_changes *changes)
9740d55b228a670047c854484d5cc979a056a9afTimo Sirainen{
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen struct push_notification_txn *ptxn = (struct push_notification_txn *)txn;
9740d55b228a670047c854484d5cc979a056a9afTimo Sirainen
9740d55b228a670047c854484d5cc979a056a9afTimo Sirainen if (changes == NULL) {
9740d55b228a670047c854484d5cc979a056a9afTimo Sirainen push_notification_txn_mbox_end(ptxn);
3ccab0bac68040f179a7de45c516cec258e28fdbTimo Sirainen } else {
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen push_notification_txn_msg_end(ptxn, changes);
648d24583c1574441c4fa0331a90bd4d6e7996c5Timo Sirainen }
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen push_notification_transaction_end(ptxn, TRUE);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen}
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstatic void push_notification_mailbox_create(struct mailbox *box)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen{
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen struct push_notification_txn *ptxn;
208028f268ac67be00590267315bf7743db02a52Timo Sirainen
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 Sirainen}
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstatic void push_notification_mailbox_delete(void *txn ATTR_UNUSED,
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen struct mailbox *box)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen{
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen struct push_notification_txn *ptxn;
9261dbf0675204898c6557591c7aa376e23a52b2Timo Sirainen
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 Sirainen}
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainenstatic void push_notification_mailbox_rename(struct mailbox *src,
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen struct mailbox *dest)
04b8a90af181cc4c7959266855e8ed50a22ed413Timo Sirainen{
04b8a90af181cc4c7959266855e8ed50a22ed413Timo Sirainen struct push_notification_txn *ptxn;
04b8a90af181cc4c7959266855e8ed50a22ed413Timo Sirainen
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 Sirainen}
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstatic void push_notification_mailbox_subscribe(struct mailbox *box,
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen bool subscribed)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen{
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainen struct push_notification_txn *ptxn;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
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 Sirainen}
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstatic void push_notification_mail_save(void *txn, struct mail *mail)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen{
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen struct push_notification_txn *ptxn = txn;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
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 } else {
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen push_notification_trigger_msg_save_append(ptxn, mail, NULL);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen }
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen}
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainenstatic void push_notification_mail_copy(void *txn,
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen struct mail *src ATTR_UNUSED,
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen struct mail *dest)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen{
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen push_notification_mail_save(txn, dest);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen}
2526d52441ef368215ab6bf04fd0356d3b09d235Timo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstatic void push_notification_mail_expunge(void *txn, struct mail *mail)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen{
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen push_notification_trigger_msg_save_expunge(
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen (struct push_notification_txn *)txn, mail, NULL);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen}
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstatic void
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenpush_notification_mail_update_flags(void *txn, struct mail *mail,
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen enum mail_flags old_flags)
9261dbf0675204898c6557591c7aa376e23a52b2Timo Sirainen{
32d69cb0794acf725c47e6d17145ec4eb0cd6e7fTimo Sirainen push_notification_trigger_msg_flag_change(
32d69cb0794acf725c47e6d17145ec4eb0cd6e7fTimo Sirainen (struct push_notification_txn *) txn, mail, NULL, old_flags);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen}
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstatic void
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenpush_notification_mail_update_keywords(void *txn, struct mail *mail,
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen const char *const *old_keywords)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen{
a75d470c9223a75801418fcdda258885c36317e0Timo Sirainen push_notification_trigger_msg_keyword_change(
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen (struct push_notification_txn *) txn, mail, NULL, old_keywords);
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen}
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstatic void *
cd56a23e21f1df3f79648cf07e2f4385e2fadebbTimo Sirainenpush_notification_transaction_begin(struct mailbox_transaction_context *t)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen{
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen return push_notification_transaction_create(mailbox_transaction_get_mailbox(t), t);
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainen}
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstatic void push_notification_transaction_rollback(void *txn)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen{
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen struct push_notification_txn *ptxn = (struct push_notification_txn *)txn;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen push_notification_transaction_end(ptxn, FALSE);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen}
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstatic void
008c010ed99d23ff601c8aec48c27781dd1c6015Timo Sirainenpush_notification_user_created_init_config(const char *config_name,
008c010ed99d23ff601c8aec48c27781dd1c6015Timo Sirainen struct mail_user *user,
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen struct push_notification_user *puser)
a75d470c9223a75801418fcdda258885c36317e0Timo Sirainen{
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen struct push_notification_driver_user *duser;
a75d470c9223a75801418fcdda258885c36317e0Timo Sirainen const char *env;
8d35582f2577c64517b2341c5d6477c7010e0a0cPhil Carmody unsigned int i;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen string_t *root_name;
008c010ed99d23ff601c8aec48c27781dd1c6015Timo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen root_name = t_str_new(32);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen str_append(root_name, config_name);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
008c010ed99d23ff601c8aec48c27781dd1c6015Timo Sirainen for (i = 2;; i++) {
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen env = mail_user_plugin_getenv(user, str_c(root_name));
a75d470c9223a75801418fcdda258885c36317e0Timo Sirainen if ((env == NULL) || (*env == '\0')) {
8d35582f2577c64517b2341c5d6477c7010e0a0cPhil Carmody break;
008c010ed99d23ff601c8aec48c27781dd1c6015Timo Sirainen }
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen if (push_notification_driver_init(user, env, puser->pool, &duser) < 0) {
a75d470c9223a75801418fcdda258885c36317e0Timo Sirainen break;
008c010ed99d23ff601c8aec48c27781dd1c6015Timo Sirainen }
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen // Add driver.
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen array_append(&puser->drivers, &duser, 1);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen str_truncate(root_name, strlen(config_name));
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen str_printfa(root_name, "%d", i);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen }
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen}
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstatic void push_notification_user_created_init(struct mail_user *user)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen{
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen pool_t pool;
a75d470c9223a75801418fcdda258885c36317e0Timo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen pool = pool_alloconly_create("push notification plugin", 1024);
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen puser = p_new(pool, struct push_notification_user, 1);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen puser->pool = pool;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen p_array_init(&puser->drivers, pool, 4);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen push_notification_user_created_init_config(PUSH_NOTIFICATION_CONFIG, user,
008c010ed99d23ff601c8aec48c27781dd1c6015Timo Sirainen puser);
008c010ed99d23ff601c8aec48c27781dd1c6015Timo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen if (array_is_empty(&puser->drivers)) {
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 Sirainen user, puser);
008c010ed99d23ff601c8aec48c27781dd1c6015Timo Sirainen }
008c010ed99d23ff601c8aec48c27781dd1c6015Timo Sirainen}
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstatic void push_notification_user_created(struct mail_user *user)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen{
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen if (puser == NULL) {
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen push_notification_user_created_init(user);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen }
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen MODULE_CONTEXT_SET(user, push_notification_user_module, puser);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen}
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
ba3d9eeb0bec6ed8465d68fa2480ad085559b580Timo Sirainen/* Plugin interface. */
ba3d9eeb0bec6ed8465d68fa2480ad085559b580Timo Sirainen
ba3d9eeb0bec6ed8465d68fa2480ad085559b580Timo Sirainenconst char *push_notification_plugin_version = DOVECOT_ABI_VERSION;
93c0be0b2c92501de882c2daf79105130501c988Timo Sirainenconst char *push_notification_plugin_dependencies[] = { "notify", NULL };
93c0be0b2c92501de882c2daf79105130501c988Timo Sirainen
93c0be0b2c92501de882c2daf79105130501c988Timo Sirainenextern struct push_notification_driver push_notification_driver_dlog;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenextern struct push_notification_driver push_notification_driver_ox;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstatic struct notify_context *push_notification_ctx;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
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
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen /* Mail Events */
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen .mail_copy = push_notification_mail_copy,
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen .mail_save = push_notification_mail_save,
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
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen};
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainenstatic struct mail_storage_hooks push_notification_storage_hooks = {
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen .mail_user_created = push_notification_user_created
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen};
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen
d22301419109ed4a38351715e6760011421dadecTimo Sirainenvoid push_notification_plugin_init(struct module *module)
ca98892a6b8a30ffc1fe26fcf02c7d59e3204e7eTimo Sirainen{
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen push_notification_ctx = notify_register(&push_notification_vfuncs);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen mail_storage_hooks_add(module, &push_notification_storage_hooks);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen push_notification_driver_register(&push_notification_driver_dlog);
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen push_notification_driver_register(&push_notification_driver_ox);
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen push_notification_event_register_rfc5423_events();
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen}
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainenvoid push_notification_plugin_deinit(void)
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen{
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen struct push_notification_driver_user **duser;
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainen
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen if (puser != NULL) {
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen array_foreach_modifiable(&puser->drivers, duser) {
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen if ((*duser)->driver->v.deinit != NULL) {
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen (*duser)->driver->v.deinit(*duser);
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen }
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainen
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen if ((*duser)->driver->v.cleanup != NULL) {
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen (*duser)->driver->v.cleanup();
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen }
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen }
ca98892a6b8a30ffc1fe26fcf02c7d59e3204e7eTimo Sirainen
ca98892a6b8a30ffc1fe26fcf02c7d59e3204e7eTimo Sirainen array_free(&puser->drivers);
ca98892a6b8a30ffc1fe26fcf02c7d59e3204e7eTimo Sirainen pool_unref(&puser->pool);
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen }
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen push_notification_driver_unregister(&push_notification_driver_dlog);
ca98892a6b8a30ffc1fe26fcf02c7d59e3204e7eTimo Sirainen push_notification_driver_unregister(&push_notification_driver_ox);
ca98892a6b8a30ffc1fe26fcf02c7d59e3204e7eTimo Sirainen
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen mail_storage_hooks_remove(&push_notification_storage_hooks);
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen notify_unregister(push_notification_ctx);
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen}
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen