forked from tbvdm/sigbak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsbk-message.c
752 lines (674 loc) · 19.6 KB
/
sbk-message.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
/*
* Copyright (c) 2018 Tim van der Molen <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <err.h>
#include <stdlib.h>
#include <string.h>
#include "sbk-internal.h"
/* For database versions < REACTIONS */
#define SBK_SELECT_SMS_1 \
"SELECT " \
STRINGIFY(SBK_SMS_TABLE) ", " \
"_id, " \
"date_sent, " \
"date AS date_received, " \
"thread_id, " \
"address, " /* recipient_id */ \
"type, " \
"body, " \
"0, " /* mms.quote_id */ \
"NULL, " /* mms.quote_author */ \
"NULL, " /* mms.quote_body */ \
"NULL, " /* mms.quote_mentions */ \
"NULL AS latest_revision_id, " \
"NULL, " /* original_message_id */ \
"0, " /* revision_number */ \
"NULL " /* reactions */ \
"FROM sms "
/* For database versions [REACTIONS, THREAD_AND_MESSAGE_FOREIGN_KEYS) */
#define SBK_SELECT_SMS_2 \
"SELECT " \
STRINGIFY(SBK_SMS_TABLE) ", " \
"_id, " \
"date_sent, " \
"date AS date_received, " \
"thread_id, " \
"address, " /* recipient_id */ \
"type, " \
"body, " \
"0, " /* mms.quote_id */ \
"NULL, " /* mms.quote_author */ \
"NULL, " /* mms.quote_body */ \
"NULL, " /* mms.quote_mentions */ \
"NULL AS latest_revision_id, " \
"NULL, " /* original_message_id */ \
"0, " /* revision_number */ \
"reactions " \
"FROM sms "
/*
* For database versions [THREAD_AND_MESSAGE_FOREIGN_KEYS,
* SINGLE_MESSAGE_TABLE_MIGRATION)
*/
#define SBK_SELECT_SMS_3 \
"SELECT " \
STRINGIFY(SBK_SMS_TABLE) ", " \
"_id, " \
"date_sent, " \
"date_received, " \
"thread_id, " \
"recipient_id, " \
"type, " \
"body, " \
"0, " /* mms.quote_id */ \
"NULL, " /* mms.quote_author */ \
"NULL, " /* mms.quote_body */ \
"NULL, " /* mms.quote_mentions */ \
"NULL AS latest_revision_id, " \
"NULL, " /* original_message_id */ \
"0, " /* revision_number */ \
"NULL " /* reactions */ \
"FROM sms "
/* For database versions < QUOTED_REPLIES */
#define SBK_SELECT_MMS_1 \
"SELECT " \
STRINGIFY(SBK_MMS_TABLE) ", " \
"_id, " \
"date, " /* date_sent */ \
"date_received, " \
"thread_id, " \
"address, " /* recipient_id */ \
"msg_box, " /* type */ \
"body, " \
"0, " /* quote_id */ \
"NULL, " /* quote_author */ \
"NULL, " /* quote_body */ \
"NULL, " /* quote_mentions */ \
"NULL AS latest_revision_id, " \
"NULL, " /* original_message_id */ \
"0, " /* revision_number */ \
"NULL " /* reactions */ \
"FROM mms "
/* For database versions [QUOTED_REPLIES, REACTIONS) */
#define SBK_SELECT_MMS_2 \
"SELECT " \
STRINGIFY(SBK_MMS_TABLE) ", " \
"_id, " \
"date, " /* date_sent */ \
"date_received, " \
"thread_id, " \
"address, " /* recipient_id */ \
"msg_box, " /* type */ \
"body, " \
"quote_id, " \
"quote_author, " \
"quote_body, " \
"NULL, " /* quote_mentions */ \
"NULL AS latest_revision_id, " \
"NULL, " /* original_message_id */ \
"0, " /* revision_number */ \
"NULL " /* reactions */ \
"FROM mms "
/* For database versions [REACTIONS, MENTIONS) */
#define SBK_SELECT_MMS_3 \
"SELECT " \
STRINGIFY(SBK_MMS_TABLE) ", " \
"_id, " \
"date, " /* date_sent */ \
"date_received, " \
"thread_id, " \
"address, " /* recipient_id */ \
"msg_box, " /* type */ \
"body, " \
"quote_id, " \
"quote_author, " \
"quote_body, " \
"NULL, " /* quote_mentions */ \
"NULL AS latest_revision_id, " \
"NULL, " /* original_message_id */ \
"0, " /* revision_number */ \
"reactions " \
"FROM mms "
/* For database versions [MENTIONS, THREAD_AND_MESSAGE_FOREIGN_KEYS) */
#define SBK_SELECT_MMS_4 \
"SELECT " \
STRINGIFY(SBK_MMS_TABLE) ", " \
"_id, " \
"date, " /* date_sent */ \
"date_received, " \
"thread_id, " \
"address, " /* recipient_id */ \
"msg_box, " /* type */ \
"body, " \
"quote_id, " \
"quote_author, " \
"quote_body, " \
"quote_mentions, " \
"NULL AS latest_revision_id, " \
"NULL, " /* original_message_id */ \
"0, " /* revision_number */ \
"reactions " \
"FROM mms "
/*
* For database versions [THREAD_AND_MESSAGE_FOREIGN_KEYS,
* SINGLE_MESSAGE_TABLE_MIGRATION)
*/
#define SBK_SELECT_MMS_5 \
"SELECT " \
STRINGIFY(SBK_MMS_TABLE) ", " \
"_id, " \
"date_sent, " \
"date_received, " \
"thread_id, " \
"recipient_id, " \
"type, " \
"body, " \
"quote_id, " \
"quote_author, " \
"quote_body, " \
"quote_mentions, " \
"NULL AS latest_revision_id, " \
"NULL, " /* original_message_id */ \
"0, " /* revision_number */ \
"NULL " /* reactions */ \
"FROM mms "
/*
* For database versions [SINGLE_MESSAGE_TABLE_MIGRATION,
* REACTION_FOREIGN_KEY_MIGRATION)
*/
#define SBK_SELECT_1 \
"SELECT " \
STRINGIFY(SBK_SINGLE_TABLE) ", " \
"_id, " \
"date_sent, " \
"date_received, " \
"thread_id, " \
"recipient_id, " \
"type, " \
"body, " \
"quote_id, " \
"quote_author, " \
"quote_body, " \
"quote_mentions, " \
"NULL AS latest_revision_id, " \
"NULL, " /* original_message_id */ \
"0, " /* revision_number */ \
"NULL " /* reactions */ \
"FROM mms "
/*
* For database versions [REACTION_FOREIGN_KEY_MIGRATION,
* MESSAGE_RECIPIENTS_AND_EDIT_MESSAGE_MIGRATION)
*/
#define SBK_SELECT_2 \
"SELECT " \
STRINGIFY(SBK_SINGLE_TABLE) ", " \
"_id, " \
"date_sent, " \
"date_received, " \
"thread_id, " \
"recipient_id, " \
"type, " \
"body, " \
"quote_id, " \
"quote_author, " \
"quote_body, " \
"quote_mentions, " \
"NULL AS latest_revision_id, " \
"NULL, " /* original_message_id */ \
"0, " /* revision_number */ \
"NULL " /* reactions */ \
"FROM message "
/*
* For database versions >= MESSAGE_RECIPIENTS_AND_EDIT_MESSAGE_MIGRATION
*
* The CASE expression below is based on the outgoingClause variable in
* V185_MessageRecipientsAndEditMessageMigration.kt in the Signal-Android
* repository.
*/
#define SBK_SELECT_3 \
"SELECT " \
STRINGIFY(SBK_SINGLE_TABLE) ", " \
"_id, " \
"date_sent, " \
"date_received, " \
"thread_id, " \
"CASE WHEN type & " STRINGIFY(SBK_BASE_TYPE_MASK) " IN (" \
STRINGIFY(SBK_OUTGOING_AUDIO_CALL_TYPE) ", " \
STRINGIFY(SBK_OUTGOING_VIDEO_CALL_TYPE) ", " \
STRINGIFY(SBK_BASE_OUTBOX_TYPE) ", " \
STRINGIFY(SBK_BASE_SENDING_TYPE) ", " \
STRINGIFY(SBK_BASE_SENT_TYPE) ", " \
STRINGIFY(SBK_BASE_SENT_FAILED_TYPE) ", " \
STRINGIFY(SBK_BASE_PENDING_SECURE_SMS_FALLBACK) ", " \
STRINGIFY(SBK_BASE_PENDING_INSECURE_SMS_FALLBACK) ") " \
"THEN to_recipient_id ELSE from_recipient_id END, " \
"type, " \
"body, " \
"quote_id, " \
"quote_author, " \
"quote_body, " \
"quote_mentions, " \
"latest_revision_id, " \
"original_message_id, " \
"revision_number, " \
"NULL " /* reactions */ \
"FROM message "
#define SBK_WHERE_THREAD \
"WHERE thread_id = ?1 AND latest_revision_id IS NULL "
#define SBK_ORDER \
"ORDER BY date_received"
/* For database versions < QUOTED_REPLIES */
#define SBK_QUERY_1 \
SBK_SELECT_SMS_1 \
SBK_WHERE_THREAD \
"UNION ALL " \
SBK_SELECT_MMS_1 \
SBK_WHERE_THREAD \
SBK_ORDER
/* For database versions [QUOTED_REPLIES, REACTIONS) */
#define SBK_QUERY_2 \
SBK_SELECT_SMS_1 \
SBK_WHERE_THREAD \
"UNION ALL " \
SBK_SELECT_MMS_2 \
SBK_WHERE_THREAD \
SBK_ORDER
/* For database versions [REACTIONS, MENTIONS) */
#define SBK_QUERY_3 \
SBK_SELECT_SMS_2 \
SBK_WHERE_THREAD \
"UNION ALL " \
SBK_SELECT_MMS_3 \
SBK_WHERE_THREAD \
SBK_ORDER
/* For database versions [MENTIONS, THREAD_AND_MESSAGE_FOREIGN_KEYS) */
#define SBK_QUERY_4 \
SBK_SELECT_SMS_2 \
SBK_WHERE_THREAD \
"UNION ALL " \
SBK_SELECT_MMS_4 \
SBK_WHERE_THREAD \
SBK_ORDER
/*
* For database versions [THREAD_AND_MESSAGE_FOREIGN_KEYS,
* SINGLE_MESSAGE_TABLE_MIGRATION)
*/
#define SBK_QUERY_5 \
SBK_SELECT_SMS_3 \
SBK_WHERE_THREAD \
"UNION ALL " \
SBK_SELECT_MMS_5 \
SBK_WHERE_THREAD \
SBK_ORDER
/*
* For database versions [SINGLE_MESSAGE_TABLE_MIGRATION,
* REACTION_FOREIGN_KEY_MIGRATION)
*/
#define SBK_QUERY_6 \
SBK_SELECT_1 \
SBK_WHERE_THREAD \
SBK_ORDER
/*
* For database versions [REACTION_FOREIGN_KEY_MIGRATION,
* MESSAGE_RECIPIENTS_AND_EDIT_MESSAGE_MIGRATION)
*/
#define SBK_QUERY_7 \
SBK_SELECT_2 \
SBK_WHERE_THREAD \
SBK_ORDER
/* For database versions >= MESSAGE_RECIPIENTS_AND_EDIT_MESSAGE_MIGRATION */
#define SBK_QUERY_8 \
SBK_SELECT_3 \
SBK_WHERE_THREAD \
SBK_ORDER
#define SBK_COLUMN_TABLE 0
#define SBK_COLUMN__ID 1
#define SBK_COLUMN_DATE_SENT 2
#define SBK_COLUMN_DATE_RECEIVED 3
#define SBK_COLUMN_THREAD_ID 4
#define SBK_COLUMN_RECIPIENT_ID 5
#define SBK_COLUMN_TYPE 6
#define SBK_COLUMN_BODY 7
#define SBK_COLUMN_QUOTE_ID 8
#define SBK_COLUMN_QUOTE_AUTHOR 9
#define SBK_COLUMN_QUOTE_BODY 10
#define SBK_COLUMN_QUOTE_MENTIONS 11
#define SBK_COLUMN_LATEST_REVISION_ID 12
#define SBK_COLUMN_ORIGINAL_MESSAGE_ID 13
#define SBK_COLUMN_REVISION_NUMBER 14
#define SBK_COLUMN_REACTIONS 15
#define SBK_MESSAGE_HAS_EDITS(stm) \
(sqlite3_column_type((stm), SBK_COLUMN_ORIGINAL_MESSAGE_ID) \
!= SQLITE_NULL)
static void
sbk_free_message(struct sbk_message *msg)
{
if (msg != NULL) {
free(msg->text);
sbk_free_attachment_list(msg->attachments);
sbk_free_mention_list(msg->mentions);
sbk_free_reaction_list(msg->reactions);
sbk_free_quote(msg->quote);
sbk_free_edit_list(msg->edits);
free(msg);
}
}
void
sbk_free_message_list(struct sbk_message_list *lst)
{
struct sbk_message *msg;
if (lst != NULL) {
while ((msg = SIMPLEQ_FIRST(lst)) != NULL) {
SIMPLEQ_REMOVE_HEAD(lst, entries);
sbk_free_message(msg);
}
free(lst);
}
}
const char *
sbk_message_id_to_string(const struct sbk_message *msg)
{
static char buf[32];
if (msg->id.table == SBK_SINGLE_TABLE)
snprintf(buf, sizeof buf, "%d", msg->id.row_id);
else
snprintf(buf, sizeof buf, "%d-%d", msg->id.table,
msg->id.row_id);
return buf;
}
int
sbk_is_outgoing_message(const struct sbk_message *msg)
{
switch (msg->type & SBK_BASE_TYPE_MASK) {
case SBK_OUTGOING_AUDIO_CALL_TYPE:
case SBK_OUTGOING_VIDEO_CALL_TYPE:
case SBK_BASE_OUTBOX_TYPE:
case SBK_BASE_SENDING_TYPE:
case SBK_BASE_SENT_TYPE:
case SBK_BASE_SENT_FAILED_TYPE:
case SBK_BASE_PENDING_SECURE_SMS_FALLBACK:
case SBK_BASE_PENDING_INSECURE_SMS_FALLBACK:
return 1;
default:
return 0;
}
}
static int
sbk_get_body(struct sbk_message *msg)
{
const char *fmt;
fmt = NULL;
if (msg->type & SBK_ENCRYPTION_REMOTE_FAILED_BIT)
fmt = "Bad encrypted message";
else if (msg->type & SBK_ENCRYPTION_REMOTE_NO_SESSION_BIT)
fmt = "Message encrypted for non-existing session";
else if (msg->type & SBK_ENCRYPTION_REMOTE_DUPLICATE_BIT)
fmt = "Duplicate message";
else if ((msg->type & SBK_ENCRYPTION_REMOTE_LEGACY_BIT) ||
(msg->type & SBK_ENCRYPTION_REMOTE_BIT))
fmt = "Encrypted message sent from an older version of Signal "
"that is no longer supported";
else if (msg->type & SBK_GROUP_UPDATE_BIT) {
if (sbk_is_outgoing_message(msg))
fmt = "You updated the group";
else
fmt = "%s updated the group";
} else if (msg->type & SBK_GROUP_QUIT_BIT) {
if (sbk_is_outgoing_message(msg))
fmt = "You have left the group";
else
fmt = "%s has left the group";
} else if (msg->type & SBK_END_SESSION_BIT) {
if (sbk_is_outgoing_message(msg))
fmt = "You reset the secure session";
else
fmt = "%s reset the secure session";
} else if (msg->type & SBK_KEY_EXCHANGE_IDENTITY_VERIFIED_BIT) {
if (sbk_is_outgoing_message(msg))
fmt = "You marked your safety number with %s verified";
else
fmt = "You marked your safety number with %s verified "
"from another device";
} else if (msg->type & SBK_KEY_EXCHANGE_IDENTITY_DEFAULT_BIT) {
if (sbk_is_outgoing_message(msg))
fmt = "You marked your safety number with %s "
"unverified";
else
fmt = "You marked your safety number with %s "
"unverified from another device";
} else if (msg->type & SBK_KEY_EXCHANGE_CORRUPTED_BIT)
fmt = "Corrupt key exchange message";
else if (msg->type & SBK_KEY_EXCHANGE_INVALID_VERSION_BIT)
fmt = "Key exchange message for invalid protocol version";
else if (msg->type & SBK_KEY_EXCHANGE_BUNDLE_BIT)
fmt = "Message with new safety number";
else if (msg->type & SBK_KEY_EXCHANGE_IDENTITY_UPDATE_BIT)
fmt = "Your safety number with %s has changed";
else if (msg->type & SBK_KEY_EXCHANGE_BIT)
fmt = "Key exchange message";
else
switch (msg->type & SBK_BASE_TYPE_MASK) {
case SBK_INCOMING_AUDIO_CALL_TYPE:
case SBK_INCOMING_VIDEO_CALL_TYPE:
fmt = "%s called you";
break;
case SBK_OUTGOING_AUDIO_CALL_TYPE:
case SBK_OUTGOING_VIDEO_CALL_TYPE:
fmt = "Called %s";
break;
case SBK_MISSED_AUDIO_CALL_TYPE:
fmt = "Missed audio call from %s";
break;
case SBK_JOINED_TYPE:
fmt = "%s is on Signal";
break;
case SBK_UNSUPPORTED_MESSAGE_TYPE:
fmt = "Unsupported message sent from a newer version "
"of Signal";
break;
case SBK_INVALID_MESSAGE_TYPE:
fmt = "Invalid message";
break;
case SBK_PROFILE_CHANGE_TYPE:
fmt = "%s changed their profile";
break;
case SBK_MISSED_VIDEO_CALL_TYPE:
fmt = "Missed video call from %s";
break;
case SBK_GV1_MIGRATION_TYPE:
fmt = "This group was updated to a new group";
break;
case SBK_BOOST_REQUEST_TYPE:
fmt = "Like this new feature? Help support Signal "
"with a one-time donation.";
break;
}
if (fmt == NULL)
return 0;
free(msg->text);
if (asprintf(&msg->text, fmt,
sbk_get_recipient_display_name(msg->recipient)) == -1) {
msg->text = NULL;
return -1;
}
return 0;
}
static void
sbk_remove_attachment(struct sbk_attachment_list **lst,
struct sbk_attachment *att)
{
TAILQ_REMOVE(*lst, att, entries);
sbk_free_attachment(att);
if (TAILQ_EMPTY(*lst)) {
sbk_free_attachment_list(*lst);
*lst = NULL;
}
}
int
sbk_get_long_message(struct sbk_ctx *ctx, char **text,
struct sbk_attachment_list **lst)
{
struct sbk_attachment *att;
char *longtext;
int found;
/* Look for a long-message attachment */
found = 0;
TAILQ_FOREACH(att, *lst, entries)
if (att->content_type != NULL &&
strcmp(att->content_type, SBK_LONG_TEXT_TYPE) == 0) {
found = 1;
break;
}
if (!found)
return 0;
if (att->file == NULL) {
warnx("Long-message attachment not available in backup");
return 0;
}
if ((longtext = sbk_get_file_data_as_string(ctx, att->file)) == NULL)
return -1;
free(*text);
*text = longtext;
/* Do not expose the long-message attachment */
sbk_remove_attachment(lst, att);
return 0;
}
static int
sbk_get_quote_for_message(struct sbk_ctx *ctx, struct sbk_message *msg,
sqlite3_stmt *stm)
{
return sbk_get_quote(ctx, &msg->quote, stm, SBK_COLUMN_QUOTE_ID,
SBK_COLUMN_QUOTE_AUTHOR, SBK_COLUMN_QUOTE_BODY,
SBK_COLUMN_QUOTE_MENTIONS, &msg->id);
}
static struct sbk_message *
sbk_get_message(struct sbk_ctx *ctx, sqlite3_stmt *stm)
{
struct sbk_message *msg;
if ((msg = calloc(1, sizeof *msg)) == NULL) {
warn(NULL);
return NULL;
}
msg->id.table = sqlite3_column_int(stm, SBK_COLUMN_TABLE);
msg->id.row_id = sqlite3_column_int(stm, SBK_COLUMN__ID);
msg->recipient = sbk_get_recipient_from_id_from_column(ctx, stm,
SBK_COLUMN_RECIPIENT_ID);
if (msg->recipient == NULL)
goto error;
if (sbk_sqlite_column_text_copy(ctx, &msg->text, stm, SBK_COLUMN_BODY)
== -1)
goto error;
msg->time_sent = sqlite3_column_int64(stm, SBK_COLUMN_DATE_SENT);
msg->time_recv = sqlite3_column_int64(stm, SBK_COLUMN_DATE_RECEIVED);
msg->type = sqlite3_column_int(stm, SBK_COLUMN_TYPE);
msg->thread = sqlite3_column_int(stm, SBK_COLUMN_THREAD_ID);
if (sbk_get_body(msg) == -1)
goto error;
if (msg->id.table != SBK_SMS_TABLE) {
if (sbk_get_attachments_for_message(ctx, msg) == -1)
goto error;
if (sbk_get_long_message(ctx, &msg->text, &msg->attachments) ==
-1)
goto error;
if (sbk_get_mentions_for_message(ctx, msg) == -1)
goto error;
if (sbk_insert_mentions(&msg->text, msg->mentions) == -1) {
warnx("Cannot insert mentions in message %s",
sbk_message_id_to_string(msg));
goto error;
}
if (sbk_get_quote_for_message(ctx, msg, stm) == -1) {
warnx("Cannot get quote for message %s",
sbk_message_id_to_string(msg));
goto error;
}
}
if (ctx->db_version >= SBK_DB_VERSION_REACTION_REFACTOR) {
if (sbk_get_reactions_from_table(ctx, msg) == -1)
goto error;
} else {
if (sbk_get_reactions_from_column(ctx, &msg->reactions, stm,
SBK_COLUMN_REACTIONS) == -1)
goto error;
}
if (SBK_MESSAGE_HAS_EDITS(stm) && sbk_get_edits(ctx, msg) == -1) {
warnx("Cannot get edits for message %s",
sbk_message_id_to_string(msg));
goto error;
}
return msg;
error:
sbk_free_message(msg);
return NULL;
}
static struct sbk_message_list *
sbk_get_messages(struct sbk_ctx *ctx, sqlite3_stmt *stm)
{
struct sbk_message_list *lst;
struct sbk_message *msg;
int ret;
if ((lst = malloc(sizeof *lst)) == NULL) {
warn(NULL);
goto error;
}
SIMPLEQ_INIT(lst);
while ((ret = sbk_sqlite_step(ctx, stm)) == SQLITE_ROW) {
if ((msg = sbk_get_message(ctx, stm)) == NULL)
goto error;
SIMPLEQ_INSERT_TAIL(lst, msg, entries);
}
if (ret != SQLITE_DONE)
goto error;
sqlite3_finalize(stm);
return lst;
error:
sbk_free_message_list(lst);
sqlite3_finalize(stm);
return NULL;
}
struct sbk_message_list *
sbk_get_messages_for_thread(struct sbk_ctx *ctx, struct sbk_thread *thd)
{
sqlite3_stmt *stm;
const char *query;
if (sbk_create_database(ctx) == -1)
return NULL;
if (ctx->db_version >=
SBK_DB_VERSION_MESSAGE_RECIPIENTS_AND_EDIT_MESSAGE_MIGRATION)
query = SBK_QUERY_8;
else if (ctx->db_version >=
SBK_DB_VERSION_REACTION_FOREIGN_KEY_MIGRATION)
query = SBK_QUERY_7;
else if (ctx->db_version >=
SBK_DB_VERSION_SINGLE_MESSAGE_TABLE_MIGRATION)
query = SBK_QUERY_6;
else if (ctx->db_version >=
SBK_DB_VERSION_THREAD_AND_MESSAGE_FOREIGN_KEYS)
query = SBK_QUERY_5;
else if (ctx->db_version >= SBK_DB_VERSION_MENTIONS)
query = SBK_QUERY_4;
else if (ctx->db_version >= SBK_DB_VERSION_REACTIONS)
query = SBK_QUERY_3;
else if (ctx->db_version >= SBK_DB_VERSION_QUOTED_REPLIES)
query = SBK_QUERY_2;
else
query = SBK_QUERY_1;
if (sbk_sqlite_prepare(ctx, &stm, query) == -1)
return NULL;
if (sbk_sqlite_bind_int(ctx, stm, 1, thd->id) == -1) {
sqlite3_finalize(stm);
return NULL;
}
return sbk_get_messages(ctx, stm);
}