bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2015-2018 Dovecot authors, see the included COPYING file */
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz#include "lib.h"
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz#include "hash.h"
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz#include "mail-storage-private.h"
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz#include "push-notification-drivers.h"
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz#include "push-notification-events.h"
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz#include "push-notification-txn-msg.h"
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzstruct push_notification_txn_msg *
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzpush_notification_txn_msg_create(struct push_notification_txn *txn,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct mail *mail)
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz{
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct push_notification_txn_msg *msg = NULL;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz if (hash_table_is_created(txn->messages)) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz msg = hash_table_lookup(txn->messages,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz POINTER_CAST(txn->t->save_count + 1));
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz } else {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz hash_table_create_direct(&txn->messages, txn->pool, 4);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz if (msg == NULL) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz msg = p_new(txn->pool, struct push_notification_txn_msg, 1);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz msg->mailbox = mailbox_get_vname(mail->box);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz /* Save sequence number - used to determine UID later. */
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz msg->seq = txn->t->save_count;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz msg->uid = mail->uid;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz hash_table_insert(txn->messages, POINTER_CAST(txn->t->save_count + 1),
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz msg);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz return msg;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz}
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzvoid
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzpush_notification_txn_msg_end(struct push_notification_txn *ptxn,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct mail_transaction_commit_changes *changes)
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz{
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct hash_iterate_context *hiter;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz void *key;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct push_notification_driver_txn **dtxn;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct seq_range_iter siter;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct mailbox_status status;
2d7ad82a356217c7d763c7075aa3526129373bd8Timo Sirainen uint32_t uid, uid_validity;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct push_notification_txn_msg *value;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz if (!hash_table_is_created(ptxn->messages)) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz return;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz hiter = hash_table_iterate_init(ptxn->messages);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz seq_range_array_iter_init(&siter, &changes->saved_uids);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
2d7ad82a356217c7d763c7075aa3526129373bd8Timo Sirainen /* uid_validity is only set in changes if message is new. */
2d7ad82a356217c7d763c7075aa3526129373bd8Timo Sirainen if (changes->uid_validity == 0) {
2d7ad82a356217c7d763c7075aa3526129373bd8Timo Sirainen mailbox_get_open_status(ptxn->mbox, STATUS_UIDVALIDITY, &status);
2d7ad82a356217c7d763c7075aa3526129373bd8Timo Sirainen uid_validity = status.uidvalidity;
2d7ad82a356217c7d763c7075aa3526129373bd8Timo Sirainen } else {
2d7ad82a356217c7d763c7075aa3526129373bd8Timo Sirainen uid_validity = changes->uid_validity;
2d7ad82a356217c7d763c7075aa3526129373bd8Timo Sirainen }
2d7ad82a356217c7d763c7075aa3526129373bd8Timo Sirainen
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz while (hash_table_iterate(hiter, ptxn->messages, &key, &value)) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz if (value->uid == 0) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz if (seq_range_array_iter_nth(&siter, value->seq, &uid)) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz value->uid = uid;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
2d7ad82a356217c7d763c7075aa3526129373bd8Timo Sirainen value->uid_validity = uid_validity;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz array_foreach_modifiable(&ptxn->drivers, dtxn) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz if ((*dtxn)->duser->driver->v.process_msg != NULL) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz (*dtxn)->duser->driver->v.process_msg(*dtxn, value);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz push_notification_txn_msg_deinit_eventdata(value);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz hash_table_iterate_deinit(&hiter);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz hash_table_destroy(&ptxn->messages);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz}
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzvoid *
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzpush_notification_txn_msg_get_eventdata(struct push_notification_txn_msg *msg,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz const char *event_name)
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz{
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct push_notification_txn_event **mevent;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz if (array_is_created(&msg->eventdata)) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz array_foreach_modifiable(&msg->eventdata, mevent) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz if (strcmp((*mevent)->event->event->name, event_name) == 0) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz return (*mevent)->data;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz return NULL;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz}
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzvoid
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzpush_notification_txn_msg_set_eventdata(struct push_notification_txn *txn,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct push_notification_txn_msg *msg,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct push_notification_event_config *event,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz void *data)
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz{
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct push_notification_txn_event *mevent;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz if (!array_is_created(&msg->eventdata)) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz p_array_init(&msg->eventdata, txn->pool, 4);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz mevent = p_new(txn->pool, struct push_notification_txn_event, 1);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz mevent->data = data;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz mevent->event = event;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz array_append(&msg->eventdata, &mevent, 1);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz}
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzvoid
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzpush_notification_txn_msg_deinit_eventdata(struct push_notification_txn_msg *msg)
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz{
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct push_notification_txn_event **mevent;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz if (array_is_created(&msg->eventdata)) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz array_foreach_modifiable(&msg->eventdata, mevent) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz if (((*mevent)->data != NULL) &&
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz ((*mevent)->event->event->msg.free_msg != NULL)) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz (*mevent)->event->event->msg.free_msg(*mevent);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz}