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
|
/* Copyright (C) 2020 C. McEnroe <june@causal.agency>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <err.h>
#include <inttypes.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sysexits.h>
#include <unistd.h>
#include "archive.h"
#include "imap.h"
static const char *uidPath(uint32_t uid, const char *type) {
static char buf[PATH_MAX];
snprintf(buf, sizeof(buf), "UID/%" PRIu32 ".%s", uid, type);
return buf;
}
static const char *messagePath(const char *messageID, const char *type) {
static char buf[PATH_MAX];
snprintf(buf, sizeof(buf), "message/%s.%s", messageID, type);
return buf;
}
bool exportFetch(FILE *imap, enum Atom tag, struct List threads) {
struct List uids = {0};
listFlatten(&uids, threads);
for (size_t i = uids.len - 1; i < uids.len; --i) {
uint32_t uid = dataCheck(uids.ptr[i], Number).number;
int error = 0
|| access(uidPath(uid, "atom"), F_OK)
|| access(uidPath(uid, "html"), F_OK)
|| access(uidPath(uid, "mbox"), F_OK);
if (!error) uids.ptr[i] = uids.ptr[--uids.len];
}
if (!uids.len) {
listFree(uids);
return false;
}
fprintf(imap, "%s UID FETCH ", Atoms[tag]);
for (size_t i = 0; i < uids.len; ++i) {
fprintf(imap, "%s%" PRIu32, (i ? "," : ""), uids.ptr[i].number);
}
fprintf(
imap,
" (UID ENVELOPE"
" BODY[HEADER.FIELDS (" MBOX_HEADERS ")] BODY[TEXT])\r\n"
);
return true;
}
void exportData(struct List items) {
uint32_t uid = 0;
struct Envelope envelope = {0};
char *header = NULL;
char *body = NULL;
for (size_t i = 0; i + 1 < items.len; i += 2) {
enum Atom name;
if (items.ptr[i].type == Atom) {
name = items.ptr[i].atom;
} else if (
items.ptr[i].type == List &&
items.ptr[i].list.len &&
items.ptr[i].list.ptr[0].type == Atom
) {
name = items.ptr[i].list.ptr[0].atom;
} else {
errx(EX_PROTOCOL, "invalid data item name");
}
struct Data data = items.ptr[i + 1];
switch (name) {
break; case AtomBody:
i--;
break; case AtomUID:
uid = dataCheck(data, Number).number;
break; case AtomEnvelope:
parseEnvelope(&envelope, dataCheck(data, List).list);
break; case AtomHeaderFields:
header = dataCheck(data, String).string;
break; case AtomText:
body = dataCheck(data, String).string;
break; default:;
}
}
if (!uid) errx(EX_PROTOCOL, "missing UID data item");
if (!envelope.subject) errx(EX_PROTOCOL, "missing ENVELOPE data item");
if (!header) errx(EX_PROTOCOL, "missing BODY[HEADER.FIELDS] data item");
if (!body) errx(EX_PROTOCOL, "missing BODY[TEXT] data item");
const char *path;
FILE *file;
int error;
path = uidPath(uid, "mbox");
file = fopen(path, "w");
if (!file) err(EX_CANTCREAT, "%s", path);
error = mboxFrom(file)
|| mboxHeader(file, header)
|| mboxBody(file, body)
|| fclose(file);
if (error) err(EX_IOERR, "%s", path);
const char *dest = messagePath(envelope.messageID, "mbox");
error = link(path, dest);
if (error) err(EX_CANTCREAT, "%s", dest);
path = uidPath(uid, "html");
file = fopen(path, "w");
if (!file) err(EX_CANTCREAT, "%s", path);
error = htmlEnvelope(file, &envelope)
|| fclose(file);
if (error) err(EX_IOERR, "%s", path);
path = uidPath(uid, "atom");
file = fopen(path, "w");
if (!file) err(EX_CANTCREAT, "%s", path);
error = atomEntryHead(file, &envelope)
|| atomEntryTail(file)
|| fclose(file);
if (error) err(EX_IOERR, "%s", path);
envelopeFree(envelope);
}
|