forked from freswa/dovecot-xaps-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxaps-push-notification-plugin.c
200 lines (173 loc) · 7.94 KB
/
xaps-push-notification-plugin.c
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Stefan Arentz <[email protected]>
* Copyright (c) 2017 Frederik Schwan <frederik dot schwan at linux dot com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <config.h>
#include <lib.h>
#include <mail-storage.h>
#include <mail-storage-private.h>
#include <push-notification-events.h>
#include <push-notification-txn-mbox.h>
#include <push-notification-txn-msg.h>
#include <push-notification-event-messagenew.h>
#include <push-notification-event-messageappend.h>
#include <http-client-private.h>
#include "xaps-push-notification-plugin.h"
#include "xaps-utils.h"
const char *xaps_plugin_version = DOVECOT_ABI_VERSION;
/*
* Prepare message handling.
* On return of false, the event gets dismissed for this driver
*/
static bool xaps_plugin_begin_txn(struct push_notification_driver_txn *dtxn) {
const struct push_notification_event *const *event;
struct push_notification_event_messagenew_config *eventMessagenewConfig;
struct push_notification_event_messageappend_config *eventMessageappendConfig;
push_notification_driver_debug(XAPS_LOG_LABEL, dtxn->ptxn->muser, "begin_txn: user: %s mailbox: %s",
dtxn->ptxn->muser->username, dtxn->ptxn->mbox->name);
// we have to initialize each event
// the MessageNew event needs a config to appear in the process_msg function
// so it's handled separately
array_foreach(&push_notification_events, event) {
if (strcmp((*event)->name,"MessageNew") == 0) {
eventMessagenewConfig = p_new(dtxn->ptxn->pool, struct push_notification_event_messagenew_config, 1);
// Take what you can, give nothing back
eventMessagenewConfig->flags = PUSH_NOTIFICATION_MESSAGE_HDR_DATE |
PUSH_NOTIFICATION_MESSAGE_HDR_FROM |
PUSH_NOTIFICATION_MESSAGE_HDR_TO |
PUSH_NOTIFICATION_MESSAGE_HDR_SUBJECT |
PUSH_NOTIFICATION_MESSAGE_BODY_SNIPPET;
push_notification_event_init(dtxn, "MessageNew", eventMessagenewConfig);
} else if (strcmp((*event)->name,"MessageAppend") == 0) {
eventMessageappendConfig = p_new(dtxn->ptxn->pool, struct push_notification_event_messageappend_config, 1);
// Take what you can, give nothing back
eventMessageappendConfig->flags = PUSH_NOTIFICATION_MESSAGE_HDR_DATE |
PUSH_NOTIFICATION_MESSAGE_HDR_FROM |
PUSH_NOTIFICATION_MESSAGE_HDR_TO |
PUSH_NOTIFICATION_MESSAGE_HDR_SUBJECT |
PUSH_NOTIFICATION_MESSAGE_BODY_SNIPPET;
push_notification_event_init(dtxn, "MessageAppend", eventMessageappendConfig);
} else {
push_notification_event_init(dtxn, (*event)->name, NULL);
}
}
return TRUE;
}
/**
* Notify the backend daemon of an incoming mail. Right now we tell
* the daemon the username and the mailbox in which a new email was
* posted. The daemon can then lookup the user and see if any of the
* devices want to receive a notification for that mailbox.
*/
static void xaps_notify(struct push_notification_driver_txn *dtxn, struct push_notification_txn_msg *msg) {
struct push_notification_txn_event *const *event;
struct http_client_request *http_req;
string_t *str;
struct istream *payload;
xaps_init(dtxn->ptxn->muser, "/notify", dtxn->ptxn->pool);
i_assert(xaps_global != NULL);
i_assert(xaps_global->http_client != NULL);
http_req = http_client_request_url(
xaps_global->http_client, "POST", xaps_global->http_url,
push_notification_driver_xaps_http_callback, dtxn->duser->context);
http_client_request_set_event(http_req, dtxn->ptxn->event);
http_client_request_add_header(http_req, "Content-Type",
"application/json; charset=utf-8");
str = str_new(default_pool, 256);
str_append(str, "{\"Username\":\"");
json_append_escaped(str, get_real_mbox_user(dtxn->ptxn->muser));
str_append(str, "\",\"Mailbox\":\"");
json_append_escaped(str, msg->mailbox);
str_append(str, "\"");
if (array_is_created(&msg->eventdata)) {
str_append(str, ",\"Events\": [");
int count = 0;
array_foreach(&msg->eventdata, event) {
if (count) {
str_append(str, ",");
}
str_append(str, "\"");
json_append_escaped(str, (*event)->event->event->name);
str_append(str, "\"");
count++;
}
str_append(str, "]");
}
str_append(str, "}");
i_debug("Sending notification: %s", str_c(str));
payload = i_stream_create_from_data(str_data(str), str_len(str));
i_stream_add_destroy_callback(payload, str_free_i, str);
http_client_request_set_payload(http_req, payload, FALSE);
http_client_request_submit(http_req);
i_stream_unref(&payload);
}
void push_notification_driver_xaps_http_callback(const struct http_response *response, void *context) {
switch (response->status / 100) {
case 2:
// Success.
i_debug("Notification sent successfully: %s", http_response_get_message(response));
break;
default:
if (response->status == 404) {
i_debug("Notification sent successfully, but no registered device found: %s", http_response_get_message(response));
} else {
// Error.
i_error("Error when sending notification: %s", http_response_get_message(response));
}
break;
}
}
// push-notification driver definition
const char *xaps_plugin_dependencies[] = { "push_notification", NULL };
extern struct push_notification_driver push_notification_driver_xaps;
int xaps_push_plugin_init(struct push_notification_driver_config *dconfig ATTR_UNUSED,
struct mail_user *muser,
pool_t pPool ATTR_UNUSED,
void **pVoid ATTR_UNUSED,
const char **pString ATTR_UNUSED) {
// xaps_init(muser, "/notify", pPool);
return 0;
}
void xaps_plugin_deinit(struct push_notification_driver_user *duser) {
push_notification_driver_xaps_deinit(duser);
}
void xaps_plugin_cleanup(void) {
push_notification_driver_xaps_cleanup();
}
struct push_notification_driver push_notification_driver_xaps = {
.name = "xaps",
.v = {
.init = xaps_push_plugin_init,
.begin_txn = xaps_plugin_begin_txn,
.process_msg = xaps_notify,
.deinit = xaps_plugin_deinit,
.cleanup = xaps_plugin_cleanup,
}
};
// plugin init and deinit
void xaps_push_notification_plugin_init(struct module *module ATTR_UNUSED) {
push_notification_driver_register(&push_notification_driver_xaps);
}
void xaps_push_notification_plugin_deinit(void) {
push_notification_driver_unregister(&push_notification_driver_xaps);
}