summaryrefslogtreecommitdiffstats
path: root/src/plugins/push-notification/push-notification-txn-mbox.c
blob: 4401819539a6234134a5462c916ef725e68c86e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* Copyright (c) 2015-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "hash.h"
#include "mail-storage-private.h"

#include "push-notification-drivers.h"
#include "push-notification-events.h"
#include "push-notification-txn-mbox.h"

struct push_notification_txn_mbox *
push_notification_txn_mbox_create(struct push_notification_txn *txn,
				  struct mailbox *box)
{
	if (txn->mbox_txn == NULL) {
		txn->mbox_txn = p_new(txn->pool,
				      struct push_notification_txn_mbox, 1);
		txn->mbox_txn->mailbox = mailbox_get_vname(box);
	}

	return txn->mbox_txn;
}

void push_notification_txn_mbox_end(struct push_notification_txn *ptxn)
{
	struct push_notification_driver_txn **dtxn;

	if (ptxn->mbox_txn != NULL) {
		array_foreach_modifiable(&ptxn->drivers, dtxn) {
			if ((*dtxn)->duser->driver->v.process_mbox != NULL) {
				(*dtxn)->duser->driver->v.process_mbox(
					*dtxn, ptxn->mbox_txn);
			}
		}

		push_notification_txn_mbox_deinit_eventdata(ptxn->mbox_txn);
	}
}

void *
push_notification_txn_mbox_get_eventdata(
	struct push_notification_txn_mbox *mbox, const char *event_name)
{
	struct push_notification_txn_event **mevent;

	if (array_is_created(&mbox->eventdata)) {
		array_foreach_modifiable(&mbox->eventdata, mevent) {
			if (strcmp((*mevent)->event->event->name,
				   event_name) == 0) {
				return (*mevent)->data;
			}
		}
	}

	return NULL;
}

void push_notification_txn_mbox_set_eventdata(
	struct push_notification_txn *txn,
	struct push_notification_txn_mbox *mbox,
	struct push_notification_event_config *event, void *data)
{
	struct push_notification_txn_event *mevent;

	if (!array_is_created(&mbox->eventdata)) {
		p_array_init(&mbox->eventdata, txn->pool, 4);
	}

	mevent = p_new(txn->pool, struct push_notification_txn_event, 1);
	mevent->data = data;
	mevent->event = event;

	array_push_back(&mbox->eventdata, &mevent);
}

void push_notification_txn_mbox_deinit_eventdata(
	struct push_notification_txn_mbox *mbox)
{
	struct push_notification_txn_event **mevent;

	if (array_is_created(&mbox->eventdata)) {
		array_foreach_modifiable(&mbox->eventdata, mevent) {
			if (((*mevent)->data != NULL) &&
			    ((*mevent)->event->event->mbox.free_mbox != NULL)) {
				(*mevent)->event->event->mbox.free_mbox(
					*mevent);
			}
		}
	}
}