push-notification-txn-msg.c revision 2d7ad82a356217c7d763c7075aa3526129373bd8
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/* Copyright (c) 2015-2016 Dovecot authors, see the included COPYING file */
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen#include "lib.h"
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen#include "hash.h"
0536ccb51d41e3078c3a9fa33e509fb4b2420f95Timo Sirainen#include "mail-storage-private.h"
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen#include "push-notification-drivers.h"
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen#include "push-notification-events.h"
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen#include "push-notification-txn-msg.h"
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainenstruct push_notification_txn_msg *
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainenpush_notification_txn_msg_create(struct push_notification_txn *txn,
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen struct mail *mail)
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen{
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen struct push_notification_txn_msg *msg = NULL;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen if (hash_table_is_created(txn->messages)) {
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen msg = hash_table_lookup(txn->messages,
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Sirainen POINTER_CAST(txn->t->save_count + 1));
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Sirainen } else {
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen hash_table_create_direct(&txn->messages, txn->pool, 4);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen }
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen if (msg == NULL) {
948b838c8e9896132e3a2d802fb5dad37e8dc716Timo Sirainen msg = p_new(txn->pool, struct push_notification_txn_msg, 1);
948b838c8e9896132e3a2d802fb5dad37e8dc716Timo Sirainen msg->mailbox = mailbox_get_vname(mail->box);
948b838c8e9896132e3a2d802fb5dad37e8dc716Timo Sirainen /* Save sequence number - used to determine UID later. */
948b838c8e9896132e3a2d802fb5dad37e8dc716Timo Sirainen msg->seq = txn->t->save_count;
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen msg->uid = mail->uid;
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Sirainen
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Sirainen hash_table_insert(txn->messages, POINTER_CAST(txn->t->save_count + 1),
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Sirainen msg);
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Sirainen }
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen return msg;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen}
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainenvoid
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainenpush_notification_txn_msg_end(struct push_notification_txn *ptxn,
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen struct mail_transaction_commit_changes *changes)
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen{
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen struct hash_iterate_context *hiter;
5a0ac2e5ef482016e00575a7dce83f52c1704732Timo Sirainen void *key;
5a0ac2e5ef482016e00575a7dce83f52c1704732Timo Sirainen struct push_notification_driver_txn **dtxn;
7823ef73e51bb81a17dcb306aff89016d4ce258fTimo Sirainen struct seq_range_iter siter;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen struct mailbox_status status;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen uint32_t uid, uid_validity;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen struct push_notification_txn_msg *value;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen if (!hash_table_is_created(ptxn->messages)) {
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen return;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen }
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen hiter = hash_table_iterate_init(ptxn->messages);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen seq_range_array_iter_init(&siter, &changes->saved_uids);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen /* uid_validity is only set in changes if message is new. */
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen if (changes->uid_validity == 0) {
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen mailbox_get_open_status(ptxn->mbox, STATUS_UIDVALIDITY, &status);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen uid_validity = status.uidvalidity;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen } else {
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen uid_validity = changes->uid_validity;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen }
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen while (hash_table_iterate(hiter, ptxn->messages, &key, &value)) {
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen if (value->uid == 0) {
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen if (seq_range_array_iter_nth(&siter, value->seq, &uid)) {
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen value->uid = uid;
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen }
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen }
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen value->uid_validity = uid_validity;
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen array_foreach_modifiable(&ptxn->drivers, dtxn) {
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen if ((*dtxn)->duser->driver->v.process_msg != NULL) {
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen (*dtxn)->duser->driver->v.process_msg(*dtxn, value);
4ce6338bf945cccfff9e4ce7cc6aa2246851b84aTimo Sirainen }
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen }
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen push_notification_txn_msg_deinit_eventdata(value);
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen }
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen hash_table_iterate_deinit(&hiter);
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen hash_table_destroy(&ptxn->messages);
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen}
4ce6338bf945cccfff9e4ce7cc6aa2246851b84aTimo Sirainen
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainenvoid *
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainenpush_notification_txn_msg_get_eventdata(struct push_notification_txn_msg *msg,
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen const char *event_name)
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen{
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen struct push_notification_txn_event **mevent;
64055bc6d2ed9e25b3b1db3b5b90d0bdb77cd715Timo Sirainen
dd2df6a67f10792ce31a3666197c0b6885893a3aTimo Sirainen if (array_is_created(&msg->eventdata)) {
64055bc6d2ed9e25b3b1db3b5b90d0bdb77cd715Timo Sirainen array_foreach_modifiable(&msg->eventdata, mevent) {
64055bc6d2ed9e25b3b1db3b5b90d0bdb77cd715Timo Sirainen if (strcmp((*mevent)->event->event->name, event_name) == 0) {
64055bc6d2ed9e25b3b1db3b5b90d0bdb77cd715Timo Sirainen return (*mevent)->data;
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen }
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen }
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen }
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen return NULL;
4ce6338bf945cccfff9e4ce7cc6aa2246851b84aTimo Sirainen}
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainenvoid
0c3ec2538c366bb5583f0b4ca2ce60804756b51cTimo Sirainenpush_notification_txn_msg_set_eventdata(struct push_notification_txn *txn,
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen struct push_notification_txn_msg *msg,
0c3ec2538c366bb5583f0b4ca2ce60804756b51cTimo Sirainen struct push_notification_event_config *event,
0c3ec2538c366bb5583f0b4ca2ce60804756b51cTimo Sirainen void *data)
f89cb43088c8b46d12d66ac924724b53ab14ce66Timo Sirainen{
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen struct push_notification_txn_event *mevent;
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen if (!array_is_created(&msg->eventdata)) {
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen p_array_init(&msg->eventdata, txn->pool, 4);
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen }
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen mevent = p_new(txn->pool, struct push_notification_txn_event, 1);
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen mevent->data = data;
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen mevent->event = event;
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen array_append(&msg->eventdata, &mevent, 1);
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen}
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainen
5c92436a61569c0b56a9374e60e779fa4455edefTimo Sirainenvoid
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainenpush_notification_txn_msg_deinit_eventdata(struct push_notification_txn_msg *msg)
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen{
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen struct push_notification_txn_event **mevent;
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen
beffc30d933c5e134c45cc871852a8427eba7e70Timo Sirainen if (array_is_created(&msg->eventdata)) {
4afd5082f38342fd688acb5796912329f57dd02cTimo Sirainen array_foreach_modifiable(&msg->eventdata, mevent) {
4afd5082f38342fd688acb5796912329f57dd02cTimo Sirainen if (((*mevent)->data != NULL) &&
4afd5082f38342fd688acb5796912329f57dd02cTimo Sirainen ((*mevent)->event->event->msg.free_msg != NULL)) {
0c3ec2538c366bb5583f0b4ca2ce60804756b51cTimo Sirainen (*mevent)->event->event->msg.free_msg(*mevent);
0c3ec2538c366bb5583f0b4ca2ce60804756b51cTimo Sirainen }
0c3ec2538c366bb5583f0b4ca2ce60804756b51cTimo Sirainen }
0c3ec2538c366bb5583f0b4ca2ce60804756b51cTimo Sirainen }
0c3ec2538c366bb5583f0b4ca2ce60804756b51cTimo Sirainen}
0c3ec2538c366bb5583f0b4ca2ce60804756b51cTimo Sirainen