/
mms_task_notification.c
417 lines (390 loc) · 11.6 KB
/
mms_task_notification.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
* Copyright (C) 2013-2016 Jolla Ltd.
* Contact: Slava Monich <slava.monich@jolla.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include "mms_task.h"
#include "mms_util.h"
#include "mms_codec.h"
#include "mms_handler.h"
#include "mms_file_util.h"
#include "mms_transfer_list.h"
/* Logging */
#define MMS_LOG_MODULE_NAME mms_task_notification_log
#include "mms_lib_log.h"
#include "mms_error.h"
MMS_LOG_MODULE_DEFINE2("mms-task-notification", MMS_TASK_LOG);
/* Class definition */
typedef MMSTaskClass MMSTaskNotificationClass;
typedef struct mms_task_notification {
MMSTask task;
MMSPdu* pdu;
GBytes* push;
MMSHandlerMessageNotifyCall* notify;
MMSTransferList* transfers;
} MMSTaskNotification;
G_DEFINE_TYPE(MMSTaskNotification, mms_task_notification, MMS_TYPE_TASK)
#define MMS_TYPE_TASK_NOTIFICATION (mms_task_notification_get_type())
#define MMS_TASK_NOTIFICATION(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),\
MMS_TYPE_TASK_NOTIFICATION, MMSTaskNotification))
/**
* Writes the datagram to a file in the message directory.
*/
static
gboolean
mms_task_notification_write_file(
MMSTaskNotification* ind,
const char* file)
{
char* dir;
gboolean ok;
mms_task_make_id(&ind->task);
dir = mms_task_dir(&ind->task);
ok = mms_write_bytes(dir, file, ind->push, NULL);
g_free(dir);
return ok;
}
/**
* Rejects the notification
*/
static
void
mms_task_notification_reject(
MMSTaskNotification* ind)
{
mms_task_make_id(&ind->task);
mms_task_queue_and_unref(ind->task.delegate,
mms_task_notifyresp_new(&ind->task, ind->transfers,
ind->pdu->transaction_id, MMS_MESSAGE_NOTIFY_STATUS_REJECTED));
}
/**
* Handles response from the MMS handler. Non-empty message id means
* that we start download the message immediately, empty string means that
* download is postponed, NULL id means that an error has occured.
*/
static
void
mms_task_notification_done(
MMSHandlerMessageNotifyCall* notify,
const char* id,
void* param)
{
MMSTaskNotification* ind = MMS_TASK_NOTIFICATION(param);
MMSTask* task = &ind->task;
MMS_ASSERT(ind->notify == notify);
ind->notify = NULL;
if (id) {
if (id[0]) {
MMS_DEBUG(" Database id: %s", id);
if (task->id) {
char* olddir = mms_task_dir(task);
char* file = g_strconcat(olddir, "/"
MMS_NOTIFICATION_IND_FILE, NULL);
/* Replace fake id with the real one */
g_free(task->id);
task->id = g_strdup(id);
if (task_config(task)->keep_temp_files) {
/* Move file to the new place */
char* newdir = mms_task_dir(task);
if (rename(olddir, newdir) == 0) {
MMS_VERBOSE("Moved %s to %s", file, newdir);
} else {
MMS_ERR("Failed to rename %s to %s: %s", olddir,
newdir, strerror(errno));
}
g_free(newdir);
} else {
/* Remove temporary directory and file */
remove(file);
remove(olddir);
}
g_free(file);
g_free(olddir);
} else {
task->id = g_strdup(id);
}
/* Schedule the download task */
if (!mms_task_queue_and_unref(task->delegate,
mms_task_retrieve_new(task->settings, task->handler,
ind->transfers, task->id, task->imsi, ind->pdu,
FALSE, NULL))) {
mms_handler_message_receive_state_changed(task->handler, id,
MMS_RECEIVE_STATE_DOWNLOAD_ERROR);
}
}
mms_task_set_state(task, MMS_TASK_STATE_DONE);
} else if (!mms_task_retry(task)) {
mms_task_notification_reject(ind);
}
mms_task_unref(task);
}
/**
* Handles M-Notification.ind PDU
*/
static
void
mms_task_notification_ind(
MMSTaskNotification* ind)
{
MMSTask* task = &ind->task;
const struct mms_notification_ind* ni = &ind->pdu->ni;
#if MMS_LOG_DEBUG
char expiry[128];
strftime(expiry, sizeof(expiry), "%Y-%m-%dT%H:%M:%S%z",
localtime(&ni->expiry));
expiry[sizeof(expiry)-1] = '\0';
MMS_DEBUG("Processing M-Notification.ind");
MMS_DEBUG(" From: %s", ni->from);
if (ni->subject) MMS_DEBUG(" Subject: %s", ni->subject);
MMS_DEBUG(" Size: %d bytes", ni->size);
MMS_DEBUG(" Location: %s", ni->location);
MMS_DEBUG(" Expiry: %s", expiry);
#endif /* MMS_LOG_DEBUG */
if (task->deadline > ni->expiry) {
task->deadline = ni->expiry;
}
MMS_ASSERT(!ind->notify);
mms_task_ref(task);
ind->notify = mms_handler_message_notify(task->handler, task->imsi,
mms_strip_address_type(ni->from), ni->subject, ni->expiry,
ind->push, mms_task_notification_done, ind);
if (ind->notify) {
mms_task_set_state(task, MMS_TASK_STATE_PENDING);
} else {
mms_task_unref(task);
if (!mms_task_retry(task)) mms_task_notification_reject(ind);
}
if (task_config(task)->keep_temp_files) {
mms_task_notification_write_file(ind, MMS_NOTIFICATION_IND_FILE);
}
}
/**
* Handles M-Delivery.ind PDU
*/
static
void
mms_task_delivery_ind(
MMSTaskNotification* ind)
{
MMS_DELIVERY_STATUS ds;
MMSTask* task = &ind->task;
const struct mms_delivery_ind* di = &ind->pdu->di;
const char* to = mms_strip_address_type(di->to);
MMS_DEBUG("Processing M-Delivery.ind");
MMS_DEBUG(" MMS message id: %s", di->msgid);
MMS_DEBUG(" Recipient: %s", to);
switch (di->dr_status) {
case MMS_MESSAGE_DELIVERY_STATUS_EXPIRED:
ds = MMS_DELIVERY_STATUS_EXPIRED;
break;
case MMS_MESSAGE_DELIVERY_STATUS_RETRIEVED:
ds = MMS_DELIVERY_STATUS_RETRIEVED;
break;
case MMS_MESSAGE_DELIVERY_STATUS_REJECTED:
ds = MMS_DELIVERY_STATUS_REJECTED;
break;
case MMS_MESSAGE_DELIVERY_STATUS_DEFERRED:
ds = MMS_DELIVERY_STATUS_DEFERRED;
break;
case MMS_MESSAGE_DELIVERY_STATUS_UNRECOGNISED:
ds = MMS_DELIVERY_STATUS_UNRECOGNISED;
break;
case MMS_MESSAGE_DELIVERY_STATUS_FORWARDED:
ds = MMS_DELIVERY_STATUS_FORWARDED;
break;
case MMS_MESSAGE_DELIVERY_STATUS_UNREACHABLE:
ds = MMS_DELIVERY_STATUS_UNREACHABLE;
break;
case MMS_MESSAGE_DELIVERY_STATUS_INDETERMINATE:
default:
ds = MMS_DELIVERY_STATUS_UNKNOWN;
break;
}
mms_handler_delivery_report(task->handler, task->imsi, di->msgid, to, ds);
if (task_config(task)->keep_temp_files) {
mms_task_notification_write_file(ind, MMS_DELIVERY_IND_FILE);
}
}
/**
* Handles M-Read-Orig.ind PDU
*/
static
void
mms_task_read_orig_ind(
MMSTaskNotification* ind)
{
MMS_READ_STATUS rs;
MMSTask* task = &ind->task;
const struct mms_read_ind* ri = &ind->pdu->ri;
const char* to = mms_strip_address_type(ri->to);
MMS_DEBUG("Processing M-Read-Orig.ind");
MMS_DEBUG(" MMS message id: %s", ri->msgid);
MMS_DEBUG(" Recipient: %s", to);
switch (ri->rr_status) {
case MMS_MESSAGE_READ_STATUS_READ:
rs = MMS_READ_STATUS_READ;
break;
case MMS_MESSAGE_READ_STATUS_DELETED:
rs = MMS_READ_STATUS_DELETED;
break;
default:
rs = MMS_READ_STATUS_INVALID;
break;
}
mms_handler_read_report(task->handler, task->imsi, ri->msgid, to, rs);
if (task_config(task)->keep_temp_files) {
mms_task_notification_write_file(ind, MMS_READ_ORIG_IND_FILE);
}
}
/**
* Handles unrecognized PDU
*/
static
void
mms_task_notification_unrecornized(
const MMSConfig* config,
GBytes* push)
{
if (config->attic_enabled) {
char* attic_dir = NULL;
int i;
for (i=0; i<100; i++) {
g_free(attic_dir);
attic_dir = g_strdup_printf("%s/" MMS_ATTIC_DIR "/%03d",
config->root_dir, i);
if (!g_file_test(attic_dir, G_FILE_TEST_IS_DIR)) break;
}
mms_write_bytes(attic_dir, MMS_UNRECOGNIZED_PUSH_FILE, push, NULL);
g_free(attic_dir);
}
}
/**
* Runs the task
*/
static
void
mms_task_notification_run(
MMSTask* task)
{
MMSTaskNotification* ind = MMS_TASK_NOTIFICATION(task);
switch (ind->pdu->type) {
case MMS_MESSAGE_TYPE_NOTIFICATION_IND:
mms_task_notification_ind(ind);
break;
case MMS_MESSAGE_TYPE_DELIVERY_IND:
mms_task_delivery_ind(ind);
break;
case MMS_MESSAGE_TYPE_READ_ORIG_IND:
mms_task_read_orig_ind(ind);
break;
default:
MMS_INFO("Ignoring MMS push PDU of type %u", ind->pdu->type);
mms_task_notification_unrecornized(task_config(task), ind->push);
break;
}
if (task->state == MMS_TASK_STATE_READY) {
mms_task_set_state(task, MMS_TASK_STATE_DONE);
}
}
/**
* Cancels the task
*/
static
void
mms_task_notification_cancel(
MMSTask* task)
{
MMSTaskNotification* ind = MMS_TASK_NOTIFICATION(task);
if (ind->notify) {
mms_handler_message_notify_cancel(task->handler, ind->notify);
ind->notify = NULL;
mms_task_unref(task);
}
MMS_TASK_CLASS(mms_task_notification_parent_class)->fn_cancel(task);
}
/**
* Final stage of deinitialization
*/
static
void
mms_task_notification_finalize(
GObject* object)
{
MMSTaskNotification* ind = MMS_TASK_NOTIFICATION(object);
MMS_ASSERT(!ind->notify);
g_bytes_unref(ind->push);
mms_message_free(ind->pdu);
mms_transfer_list_unref(ind->transfers);
G_OBJECT_CLASS(mms_task_notification_parent_class)->finalize(object);
}
/**
* Per class initializer
*/
static
void
mms_task_notification_class_init(
MMSTaskNotificationClass* klass)
{
klass->fn_run = mms_task_notification_run;
klass->fn_cancel = mms_task_notification_cancel;
G_OBJECT_CLASS(klass)->finalize = mms_task_notification_finalize;
}
/**
* Per instance initializer
*/
static
void
mms_task_notification_init(
MMSTaskNotification* notification)
{
}
/* Create MMS notification task */
MMSTask*
mms_task_notification_new(
MMSSettings* settings,
MMSHandler* handler,
MMSTransferList* transfers,
const char* imsi,
GBytes* bytes,
GError** error)
{
MMSPdu* pdu = mms_decode_bytes(bytes);
MMS_ASSERT(!error || !(*error));
if (pdu) {
MMSTaskNotification* ind;
/* Looks like a legitimate MMS Push PDU */
#if MMS_LOG_DEBUG
MMS_DEBUG(" MMS version: %u.%u", (pdu->version & 0x70) >> 4,
pdu->version & 0x0f);
if (pdu->transaction_id) {
MMS_DEBUG(" MMS transaction id: %s", pdu->transaction_id);
}
#endif /* MMS_LOG_DEBUG */
ind = mms_task_alloc(MMS_TYPE_TASK_NOTIFICATION,
settings, handler, "Notification", NULL, imsi);
ind->transfers = mms_transfer_list_ref(transfers);
ind->push = g_bytes_ref(bytes);
ind->pdu = pdu;
return &ind->task;
} else {
MMS_ERROR(error, MMS_LIB_ERROR_DECODE, "Failed to decode MMS PDU");
mms_task_notification_unrecornized(settings->config, bytes);
return NULL;
}
}
/*
* Local Variables:
* mode: C
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*/