1 /* Copyright (c) 2015-2018 Dovecot authors, see the included COPYING file */
2 
3 #include "lib.h"
4 #include "array.h"
5 
6 #include "mail-storage.h"
7 
8 #include "push-notification-drivers.h"
9 #include "push-notification-events.h"
10 #include "push-notification-triggers.h"
11 #include "push-notification-txn-mbox.h"
12 #include "push-notification-txn-msg.h"
13 
14 static void
push_notification_trigger_mbox_common(struct push_notification_txn * txn,struct mailbox * box,struct push_notification_txn_mbox ** mbox,enum push_notification_event_trigger trigger)15 push_notification_trigger_mbox_common(
16 	struct push_notification_txn *txn, struct mailbox *box,
17 	struct push_notification_txn_mbox **mbox,
18 	enum push_notification_event_trigger trigger)
19 {
20 	if (*mbox == NULL) {
21 		*mbox = push_notification_txn_mbox_create(txn, box);
22 	}
23 
24 	txn->trigger |= trigger;
25 	event_add_str(txn->event, "mailbox", mailbox_get_vname(box));
26 }
27 
push_notification_trigger_mbox_create(struct push_notification_txn * txn,struct mailbox * box,struct push_notification_txn_mbox * mbox)28 void push_notification_trigger_mbox_create(
29 	struct push_notification_txn *txn, struct mailbox *box,
30 	struct push_notification_txn_mbox *mbox)
31 {
32 	struct push_notification_event_config *ec;
33 
34 	push_notification_trigger_mbox_common(
35 		txn, box, &mbox, PUSH_NOTIFICATION_EVENT_TRIGGER_MBOX_CREATE);
36 
37 	if (array_is_created(&txn->events)) {
38 		array_foreach_elem(&txn->events, ec) {
39 			if (ec->event->mbox_triggers.create != NULL)
40 				ec->event->mbox_triggers.create(txn, ec, mbox);
41 		}
42 	}
43 }
44 
push_notification_trigger_mbox_delete(struct push_notification_txn * txn,struct mailbox * box,struct push_notification_txn_mbox * mbox)45 void push_notification_trigger_mbox_delete(
46 	struct push_notification_txn *txn, struct mailbox *box,
47 	struct push_notification_txn_mbox *mbox)
48 {
49 	struct push_notification_event_config *ec;
50 
51 	push_notification_trigger_mbox_common(
52 		txn, box, &mbox, PUSH_NOTIFICATION_EVENT_TRIGGER_MBOX_DELETE);
53 
54 	if (array_is_created(&txn->events)) {
55 		array_foreach_elem(&txn->events, ec) {
56 			if (ec->event->mbox_triggers.delete != NULL)
57 				ec->event->mbox_triggers.delete(txn, ec, mbox);
58 		}
59 	}
60 }
61 
push_notification_trigger_mbox_rename(struct push_notification_txn * txn,struct mailbox * src,struct mailbox * dest,struct push_notification_txn_mbox * mbox)62 void push_notification_trigger_mbox_rename(
63 	struct push_notification_txn *txn,
64 	struct mailbox *src, struct mailbox *dest,
65 	struct push_notification_txn_mbox *mbox)
66 {
67 	struct push_notification_event_config *ec;
68 
69 	push_notification_trigger_mbox_common(
70 		txn, dest, &mbox, PUSH_NOTIFICATION_EVENT_TRIGGER_MBOX_RENAME);
71 
72 	if (array_is_created(&txn->events)) {
73 		array_foreach_elem(&txn->events, ec) {
74 			if (ec->event->mbox_triggers.rename != NULL) {
75 				ec->event->mbox_triggers.rename(
76 					txn, ec, mbox, src);
77 			}
78 		}
79 	}
80 }
81 
push_notification_trigger_mbox_subscribe(struct push_notification_txn * txn,struct mailbox * box,bool subscribed,struct push_notification_txn_mbox * mbox)82 void push_notification_trigger_mbox_subscribe(
83 	struct push_notification_txn *txn, struct mailbox *box, bool subscribed,
84 	struct push_notification_txn_mbox *mbox)
85 {
86 	struct push_notification_event_config *ec;
87 
88 	push_notification_trigger_mbox_common(
89 		txn, box, &mbox,
90 		PUSH_NOTIFICATION_EVENT_TRIGGER_MBOX_SUBSCRIBE);
91 
92 	if (array_is_created(&txn->events)) {
93 		array_foreach_elem(&txn->events, ec) {
94 			if (subscribed == TRUE) {
95 				if (ec->event->mbox_triggers.subscribe != NULL) {
96 					ec->event->mbox_triggers.subscribe(
97 						txn, ec, mbox);
98 				}
99 			} else {
100 				if (ec->event->mbox_triggers.unsubscribe != NULL) {
101 					ec->event->mbox_triggers.unsubscribe(
102 						txn, ec, mbox);
103 				}
104 			}
105 		}
106 	}
107 }
108 
109 static void
push_notification_trigger_msg_common(struct push_notification_txn * txn,struct mail * mail,struct push_notification_txn_msg ** msg,enum push_notification_event_trigger trigger)110 push_notification_trigger_msg_common(
111 	struct push_notification_txn *txn, struct mail *mail,
112 	struct push_notification_txn_msg **msg,
113 	enum push_notification_event_trigger trigger)
114 {
115 	if (*msg == NULL)
116 		*msg = push_notification_txn_msg_create(txn, mail);
117 
118 	txn->trigger |= trigger;
119 }
120 
push_notification_trigger_msg_save_new(struct push_notification_txn * txn,struct mail * mail,struct push_notification_txn_msg * msg)121 void push_notification_trigger_msg_save_new(
122 	struct push_notification_txn *txn, struct mail *mail,
123 	struct push_notification_txn_msg *msg)
124 {
125 	struct push_notification_event_config *ec;
126 
127 	push_notification_trigger_msg_common(
128 		txn, mail, &msg, PUSH_NOTIFICATION_EVENT_TRIGGER_MSG_SAVE_NEW);
129 
130 	if (array_is_created(&txn->events)) {
131 		array_foreach_elem(&txn->events, ec) {
132 			if (ec->event->msg_triggers.save != NULL) {
133 				ec->event->msg_triggers.save(
134 					txn, ec, msg, mail);
135 			}
136 		}
137 	}
138 }
139 
push_notification_trigger_msg_save_append(struct push_notification_txn * txn,struct mail * mail,struct push_notification_txn_msg * msg)140 void push_notification_trigger_msg_save_append(
141 	struct push_notification_txn *txn, struct mail *mail,
142 	struct push_notification_txn_msg *msg)
143 {
144 	struct push_notification_event_config *ec;
145 
146 	push_notification_trigger_msg_common(
147 		txn, mail, &msg,
148 		PUSH_NOTIFICATION_EVENT_TRIGGER_MSG_SAVE_APPEND);
149 
150 	if (array_is_created(&txn->events)) {
151 		array_foreach_elem(&txn->events, ec) {
152 			if (ec->event->msg_triggers.append != NULL) {
153 				ec->event->msg_triggers.append(
154 					txn, ec, msg, mail);
155 			}
156 		}
157 	}
158 }
159 
push_notification_trigger_msg_save_expunge(struct push_notification_txn * txn,struct mail * mail,struct push_notification_txn_msg * msg)160 void push_notification_trigger_msg_save_expunge(
161 	struct push_notification_txn *txn, struct mail *mail,
162 	struct push_notification_txn_msg *msg)
163 {
164 	struct push_notification_event_config *ec;
165 
166 	push_notification_trigger_msg_common(
167 		txn, mail, &msg, PUSH_NOTIFICATION_EVENT_TRIGGER_MSG_EXPUNGE);
168 
169 	if (array_is_created(&txn->events)) {
170 		array_foreach_elem(&txn->events, ec) {
171 			if (ec->event->msg_triggers.expunge != NULL)
172 				ec->event->msg_triggers.expunge(txn, ec, msg);
173 		}
174 	}
175 }
176 
push_notification_trigger_msg_flag_change(struct push_notification_txn * txn,struct mail * mail,struct push_notification_txn_msg * msg,enum mail_flags old_flags)177 void push_notification_trigger_msg_flag_change(
178 	struct push_notification_txn *txn, struct mail *mail,
179 	struct push_notification_txn_msg *msg, enum mail_flags old_flags)
180 {
181 	struct push_notification_event_config *ec;
182 
183 	push_notification_trigger_msg_common(
184 		txn, mail, &msg,
185 		PUSH_NOTIFICATION_EVENT_TRIGGER_MSG_FLAGCHANGE);
186 
187 	if (array_is_created(&txn->events)) {
188 		array_foreach_elem(&txn->events, ec) {
189 			if (ec->event->msg_triggers.flagchange != NULL) {
190 				ec->event->msg_triggers.flagchange(
191 					txn, ec, msg, mail, old_flags);
192 			}
193 		}
194 	}
195 }
196 
push_notification_trigger_msg_keyword_change(struct push_notification_txn * txn,struct mail * mail,struct push_notification_txn_msg * msg,const char * const * old_keywords)197 void push_notification_trigger_msg_keyword_change(
198 	struct push_notification_txn *txn, struct mail *mail,
199 	struct push_notification_txn_msg *msg, const char *const *old_keywords)
200 {
201 	struct push_notification_event_config *ec;
202 
203 	push_notification_trigger_msg_common(
204 		txn, mail, &msg,
205 		PUSH_NOTIFICATION_EVENT_TRIGGER_MSG_KEYWORDCHANGE);
206 
207 	if (array_is_created(&txn->events)) {
208 		array_foreach_elem(&txn->events, ec) {
209 			if (ec->event->msg_triggers.keywordchange != NULL) {
210 				ec->event->msg_triggers.keywordchange(
211 					txn, ec, msg, mail, old_keywords);
212 			}
213 		}
214 	}
215 }
216