-
Notifications
You must be signed in to change notification settings - Fork 8
/
cmd-export-messages.c
616 lines (511 loc) · 13.2 KB
/
cmd-export-messages.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
/*
* Copyright (c) 2019 Tim van der Molen <tim@kariliq.nl>
*
* 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 <sys/types.h>
#include <sys/stat.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "sigbak.h"
static enum cmd_status cmd_export_messages(int, char **);
const struct cmd_entry cmd_export_messages_entry = {
.name = "export-messages",
.alias = "msg",
.usage = "[-f format] [-p passfile] backup [directory]",
.exec = cmd_export_messages
};
enum {
FORMAT_CSV,
FORMAT_TEXT,
FORMAT_TEXT_SHORT
};
static FILE *
get_thread_file(struct sbk_thread *thd, int dfd, const char *ext)
{
FILE *fp;
char *name;
int fd;
if ((name = get_recipient_filename(thd->recipient, ext)) == NULL)
return NULL;
fd = openat(dfd, name, O_WRONLY | O_CREAT | O_EXCL, 0666);
if (fd == -1) {
warn("%s", name);
free(name);
return NULL;
}
if ((fp = fdopen(fd, "w")) == NULL) {
warn("%s", name);
close(fd);
free(name);
return NULL;
}
free(name);
return fp;
}
static void
csv_print_quoted_string(FILE *fp, const char *str)
{
const char *c;
if (str == NULL || str[0] == '\0')
return;
putc('"', fp);
for (c = str; *c != '\0'; c++) {
putc(*c, fp);
if (*c == '"')
putc('"', fp);
}
putc('"', fp);
}
static void
csv_write_record(FILE *fp, uint64_t time_sent, uint64_t time_recv,
int thread, int type, int nattachments, const char *addr,
const char *name, const char *text)
{
fprintf(fp, "%" PRIu64 ",%" PRIu64 ",%d,%d,%d,",
time_sent, time_recv, thread, type, nattachments);
csv_print_quoted_string(fp, addr);
putc(',', fp);
csv_print_quoted_string(fp, name);
putc(',', fp);
csv_print_quoted_string(fp, text);
putc('\n', fp);
}
static int
csv_write_message(FILE *fp, struct sbk_message *msg)
{
struct sbk_attachment *att;
struct sbk_reaction *rct;
const char *addr;
int nattachments;
addr = (msg->recipient->type == SBK_CONTACT) ?
msg->recipient->contact->phone : "group";
nattachments = 0;
if (msg->attachments != NULL)
TAILQ_FOREACH(att, msg->attachments, entries)
nattachments++;
csv_write_record(fp,
msg->time_sent,
msg->time_recv,
msg->thread,
sbk_is_outgoing_message(msg),
nattachments,
addr,
sbk_get_recipient_display_name(msg->recipient),
msg->text);
if (msg->reactions != NULL)
SIMPLEQ_FOREACH(rct, msg->reactions, entries)
csv_write_record(fp,
rct->time_sent,
rct->time_recv,
msg->thread,
2,
0,
rct->recipient->contact->phone,
sbk_get_recipient_display_name(rct->recipient),
rct->emoji);
return 0;
}
static int
csv_export_thread(struct sbk_ctx *ctx, struct sbk_thread *thd, int dfd)
{
struct sbk_message_list *lst;
struct sbk_message *msg;
FILE *fp;
int ret;
if ((lst = sbk_get_messages_for_thread(ctx, thd)) == NULL)
return -1;
if (SIMPLEQ_EMPTY(lst)) {
sbk_free_message_list(lst);
return 0;
}
if ((fp = get_thread_file(thd, dfd, ".csv")) == NULL) {
sbk_free_message_list(lst);
return -1;
}
ret = 0;
SIMPLEQ_FOREACH(msg, lst, entries)
if (csv_write_message(fp, msg) == -1)
ret = -1;
fclose(fp);
sbk_free_message_list(lst);
return ret;
}
static void
text_write_recipient_field(FILE *fp, const char *field,
struct sbk_recipient *rcp)
{
fprintf(fp, "%s: %s", field, sbk_get_recipient_display_name(rcp));
if (rcp != NULL) {
if (rcp->type == SBK_GROUP)
fputs(" (group)", fp);
else if (rcp->contact->phone != NULL)
fprintf(fp, " (%s)", rcp->contact->phone);
}
putc('\n', fp);
}
static void
text_write_time_field(FILE *fp, const char *field, int64_t msec)
{
static const char *days[] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
static const char *months[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
"Oct", "Nov", "Dec" };
struct tm *tm;
time_t tt;
tt = msec / 1000;
if ((tm = localtime(&tt)) == NULL) {
warnx("localtime() failed");
return;
}
fprintf(fp, "%s: %s, %d %s %d %02d:%02d:%02d %c%02ld%02ld\n",
field,
days[tm->tm_wday],
tm->tm_mday,
months[tm->tm_mon],
tm->tm_year + 1900,
tm->tm_hour,
tm->tm_min,
tm->tm_sec,
(tm->tm_gmtoff < 0) ? '-' : '+',
labs(tm->tm_gmtoff) / 3600,
labs(tm->tm_gmtoff) % 3600 / 60);
}
static void
text_write_attachment_field(FILE *fp, struct sbk_attachment *att)
{
fputs("Attachment: ", fp);
if (att->filename == NULL || *att->filename == '\0')
fputs("no filename", fp);
else
fprintf(fp, "%s", att->filename);
fprintf(fp, " (%s, %" PRIu64 " bytes, id %s)\n",
(att->content_type != NULL) ?
att->content_type : "",
att->size,
sbk_attachment_id_to_string(att));
}
static void
text_write_quote(FILE *fp, struct sbk_quote *qte)
{
struct sbk_attachment *att;
char *s, *t;
fputs("\n> ", fp);
text_write_recipient_field(fp, "From", qte->recipient);
fputs("> ", fp);
text_write_time_field(fp, "Sent", qte->id);
if (qte->attachments != NULL)
TAILQ_FOREACH(att, qte->attachments, entries) {
fputs("> ", fp);
text_write_attachment_field(fp, att);
}
if (qte->text != NULL && *qte->text != '\0') {
fputs(">\n", fp);
for (s = qte->text; (t = strchr(s, '\n')) != NULL; s = t + 1)
fprintf(fp, "> %.*s\n", (int)(t - s), s);
fprintf(fp, "> %s\n", s);
}
}
static void
text_write_edits(FILE *fp, struct sbk_message *msg)
{
struct sbk_attachment *att;
struct sbk_edit *edit;
TAILQ_FOREACH_REVERSE(edit, msg->edits, sbk_edit_list, entries) {
fprintf(fp, "Version: %d\n", edit->revision + 1);
text_write_time_field(fp, "Sent", edit->time_sent);
if (!sbk_is_outgoing_message(msg))
text_write_time_field(fp, "Received", edit->time_recv);
if (edit->attachments != NULL)
TAILQ_FOREACH(att, edit->attachments, entries)
text_write_attachment_field(fp, att);
if (edit->quote != NULL)
text_write_quote(fp, edit->quote);
if (edit->text != NULL && *edit->text != '\0')
fprintf(fp, "\n%s\n\n", edit->text);
else
putc('\n', fp);
}
}
static int
text_write_message(FILE *fp, struct sbk_message *msg)
{
struct sbk_attachment *att;
struct sbk_reaction *rct;
if (sbk_is_outgoing_message(msg))
fputs("From: You\n", fp);
else
text_write_recipient_field(fp, "From", msg->recipient);
text_write_time_field(fp, "Sent", msg->time_sent);
if (!sbk_is_outgoing_message(msg))
text_write_time_field(fp, "Received", msg->time_recv);
if (msg->attachments != NULL)
TAILQ_FOREACH(att, msg->attachments, entries)
text_write_attachment_field(fp, att);
if (msg->reactions != NULL)
SIMPLEQ_FOREACH(rct, msg->reactions, entries)
fprintf(fp, "Reaction: %s from %s\n",
rct->emoji,
sbk_get_recipient_display_name(rct->recipient));
if (msg->edits != NULL) {
fprintf(fp, "Edited: %d versions\n\n", msg->nedits);
text_write_edits(fp, msg);
} else {
if (msg->quote != NULL)
text_write_quote(fp, msg->quote);
if (msg->text != NULL && *msg->text != '\0')
fprintf(fp, "\n%s\n\n", msg->text);
else
fputs("\n", fp);
}
return 0;
}
static int
text_export_thread(struct sbk_ctx *ctx, struct sbk_thread *thd, int dfd)
{
struct sbk_message_list *lst;
struct sbk_message *msg;
FILE *fp;
int ret;
if ((lst = sbk_get_messages_for_thread(ctx, thd)) == NULL)
return -1;
if (SIMPLEQ_EMPTY(lst)) {
sbk_free_message_list(lst);
return 0;
}
if ((fp = get_thread_file(thd, dfd, ".txt")) == NULL) {
sbk_free_message_list(lst);
return -1;
}
text_write_recipient_field(fp, "Conversation", thd->recipient);
putc('\n', fp);
ret = 0;
SIMPLEQ_FOREACH(msg, lst, entries)
if (text_write_message(fp, msg) == -1)
ret = -1;
fclose(fp);
sbk_free_message_list(lst);
return ret;
}
static const char *
text_short_format_time(int64_t msec)
{
struct tm *tm;
time_t tt;
static char buf[64];
tt = msec / 1000;
if ((tm = localtime(&tt)) == NULL) {
warnx("localtime() failed");
buf[0] = '\0';
return buf;
}
snprintf(buf, sizeof buf, "%d-%02d-%02d %02d:%02d",
tm->tm_year + 1900,
tm->tm_mon + 1,
tm->tm_mday,
tm->tm_hour,
tm->tm_min);
return buf;
}
static int
text_short_write_message(FILE *fp, struct sbk_message *msg)
{
struct sbk_attachment *att;
const char *name, *time;
int details, natts;
time = text_short_format_time(msg->time_sent);
name = sbk_is_outgoing_message(msg) ? "You" :
sbk_get_recipient_display_name(msg->recipient);
fprintf(fp, "%s %s:", time, name);
details = 0;
if (msg->quote != NULL) {
name = sbk_get_recipient_display_name(msg->quote->recipient);
time = text_short_format_time(msg->quote->id);
fprintf(fp, " [reply to %s on %s", name, time);
details = 1;
}
if (msg->edits != NULL) {
fprintf(fp, "%sedited", details ? ", " : " [");
details = 1;
}
if (msg->attachments != NULL) {
natts = 0;
TAILQ_FOREACH(att, msg->attachments, entries)
natts++;
if (natts > 0) {
fprintf(fp, "%s%d attachment%s", details ? ", " : " [",
natts, (natts > 1) ? "s" : "");
details = 1;
}
}
if (details)
putc(']', fp);
if (msg->text != NULL && *msg->text != '\0')
fprintf(fp, " %s\n", msg->text);
else
putc('\n', fp);
return 0;
}
static int
text_short_export_thread(struct sbk_ctx *ctx, struct sbk_thread *thd, int dfd)
{
struct sbk_message_list *lst;
struct sbk_message *msg;
FILE *fp;
int ret;
if ((lst = sbk_get_messages_for_thread(ctx, thd)) == NULL)
return -1;
if (SIMPLEQ_EMPTY(lst)) {
sbk_free_message_list(lst);
return 0;
}
if ((fp = get_thread_file(thd, dfd, ".txt")) == NULL) {
sbk_free_message_list(lst);
return -1;
}
ret = 0;
SIMPLEQ_FOREACH(msg, lst, entries)
if (text_short_write_message(fp, msg) == -1)
ret = -1;
fclose(fp);
sbk_free_message_list(lst);
return ret;
}
static int
export_messages(struct sbk_ctx *ctx, const char *outdir, int format)
{
struct sbk_thread_list *lst;
struct sbk_thread *thd;
int dfd, ret;
if ((dfd = open(outdir, O_RDONLY | O_DIRECTORY)) == -1) {
warn("%s", outdir);
return -1;
}
if ((lst = sbk_get_threads(ctx)) == NULL) {
close(dfd);
return -1;
}
ret = 0;
SIMPLEQ_FOREACH(thd, lst, entries) {
switch (format) {
case FORMAT_CSV:
if (csv_export_thread(ctx, thd, dfd) == -1)
ret = -1;
break;
case FORMAT_TEXT:
if (text_export_thread(ctx, thd, dfd) == -1)
ret = -1;
break;
case FORMAT_TEXT_SHORT:
if (text_short_export_thread(ctx, thd, dfd) == -1)
ret = -1;
break;
}
}
sbk_free_thread_list(lst);
close(dfd);
return ret;
}
static enum cmd_status
cmd_export_messages(int argc, char **argv)
{
struct sbk_ctx *ctx;
char *backup, *outdir, *passfile, passphr[128];
int c, format, ret;
format = FORMAT_TEXT;
passfile = NULL;
while ((c = getopt(argc, argv, "f:p:")) != -1)
switch (c) {
case 'f':
if (strcmp(optarg, "csv") == 0)
format = FORMAT_CSV;
else if (strcmp(optarg, "text") == 0)
format = FORMAT_TEXT;
else if (strcmp(optarg, "text-short") == 0)
format = FORMAT_TEXT_SHORT;
else {
warnx("%s: Invalid format", optarg);
return CMD_ERROR;
}
break;
case 'p':
passfile = optarg;
break;
default:
return CMD_USAGE;
}
argc -= optind;
argv += optind;
switch (argc) {
case 1:
backup = argv[0];
outdir = ".";
break;
case 2:
backup = argv[0];
outdir = argv[1];
if (mkdir(outdir, 0777) == -1 && errno != EEXIST) {
warn("mkdir: %s", outdir);
return CMD_ERROR;
}
break;
default:
return CMD_USAGE;
}
if (unveil(backup, "r") == -1)
err(1, "unveil: %s", backup);
if (unveil(outdir, "rwc") == -1)
err(1, "unveil: %s", outdir);
/* For SQLite */
if (unveil("/dev/urandom", "r") == -1)
err(1, "unveil: /dev/urandom");
/* For SQLite */
if (unveil("/tmp", "rwc") == -1)
err(1, "unveil: /tmp");
if (passfile == NULL) {
if (pledge("stdio rpath wpath cpath tty", NULL) == -1)
err(1, "pledge");
} else {
if (strcmp(passfile, "-") != 0 && unveil(passfile, "r") == -1)
err(1, "unveil: %s", passfile);
if (pledge("stdio rpath wpath cpath", NULL) == -1)
err(1, "pledge");
}
if ((ctx = sbk_ctx_new()) == NULL)
return CMD_ERROR;
if (get_passphrase(passfile, passphr, sizeof passphr) == -1) {
sbk_ctx_free(ctx);
return CMD_ERROR;
}
if (sbk_open(ctx, backup, passphr) == -1) {
explicit_bzero(passphr, sizeof passphr);
sbk_ctx_free(ctx);
return CMD_ERROR;
}
explicit_bzero(passphr, sizeof passphr);
if (passfile == NULL && pledge("stdio rpath wpath cpath", NULL) == -1)
err(1, "pledge");
ret = export_messages(ctx, outdir, format);
sbk_close(ctx);
sbk_ctx_free(ctx);
return (ret == -1) ? CMD_ERROR : CMD_OK;
}