about summary refs log tree commit diff
path: root/archive.c
blob: 881df2100d3522fabaf8a3e6518afbe534c7f363 (plain) (blame)
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
/* Copyright (C) 2020  June 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/>.
 *
 * Additional permission under GNU GPL version 3 section 7:
 *
 * If you modify this Program, or any covered work, by linking or
 * combining it with OpenSSL (or a modified version of that library),
 * containing parts covered by the terms of the OpenSSL License and the
 * original SSLeay license, the licensors of this Program grant you
 * additional permission to convey the resulting work. Corresponding
 * Source for a non-source form of such a combination shall include the
 * source code for the parts of OpenSSL used as well as that of the
 * covered work.
 */

#include <err.h>
#include <errno.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sysexits.h>
#include <tls.h>
#include <unistd.h>

#include "archive.h"
#include "imap.h"

#define ENV_PASSWORD "BUBGER_IMAP_PASSWORD"

bool quiet;
const char *baseURL = "";
const char *baseTitle;
const char *baseMailto;
const char *baseSubscribe;
const char *baseStylesheet;

static uint32_t uidRead(const char *path) {
	FILE *file = fopen(path, "r");
	if (!file) return 0;
	uint32_t uid;
	int n = fscanf(file, "%" SCNu32, &uid);
	if (n < 1) errx(EX_DATAERR, "%s: invalid UID", path);
	return uid;
}

static void uidWrite(const char *path, uint32_t uid) {
	FILE *file = fopen(path, "w");
	if (!file) err(EX_CANTCREAT, "%s", path);
	int n = fprintf(file, "%" PRIu32 "\n", uid);
	if (n < 0) err(EX_IOERR, "%s", path);
	int error = fclose(file);
	if (error) err(EX_IOERR, "%s", path);
}

static void createDir(const char *path) {
	int error = mkdir(path, 0775);
	if (error && errno != EEXIST) err(EX_CANTCREAT, "%s", path);
}

static void createDirs(void) {
	createDir("UID");
	createDir("attachment");
	createDir("message");
	createDir("thread");
}

struct Search search;

static void searchAdd(const char *name, const char *expr) {
	if (search.len == search.cap) {
		search.cap = (search.cap ? search.cap * 2 : 8);
		search.names = realloc(
			search.names, sizeof(*search.names) * search.cap
		);
		search.exprs = realloc(
			search.exprs, sizeof(*search.exprs) * search.cap
		);
		if (!search.names || !search.exprs) err(EX_OSERR, "realloc");
	}
	size_t i = search.len++;
	search.names[i] = strdup(name);
	search.exprs[i] = strdup(expr);
	if (!search.names[i] || !search.exprs[i]) err(EX_OSERR, "strdup");
}

static int searchRead(const char *path) {
	FILE *file = fopen(path, "r");
	if (!file) return -1;

	int line = 1;
	size_t cap = 0;
	char *buf = NULL;
	for (ssize_t len; 0 < (len = getline(&buf, &cap, file)); ++line) {
		if (buf[len - 1] == '\n') buf[len - 1] = '\0';
		if (!buf[0] || buf[0] == '#') continue;

		char *expr = buf;
		char *name = strsep(&expr, " \t");
		if (!expr) {
			errx(EX_DATAERR, "%s:%d: missing search expression", path, line);
		}
		searchAdd(name, &expr[strspn(expr, " \t")]);
	}
	if (ferror(file)) err(EX_IOERR, "%s", path);
	fclose(file);
	return 0;
}

static void searchDefault(void) {
	for (size_t i = 0; i < search.len; ++i) {
		if (!strcmp(search.names[i], "index")) return;
	}
	searchAdd("index", "ALL");
}

static void searchThreads(
	struct IMAP *imap, struct List threads, const struct Envelope *envelopes,
	const char *name, const char *expr
) {
	struct Resp resp;
	struct List roots = {0};

	if (!strcmp(expr, "ALL")) {
		for (size_t i = 0; i < threads.len; ++i) {
			struct Data root = {
				.type = Number,
				.number = threadRoot(dataCheck(threads.ptr[i], List).list)
			};
			listPush(&roots, root);
		}
		goto concat;
	}

	enum Atom search = atom("search");
	concatSearch(imap->w, search, threads, expr);
	for (; resp = respOk(imapResp(imap)), resp.tag != search; respFree(resp)) {
		if (resp.resp != AtomSearch) continue;
		roots = resp.data;
		resp.data = (struct List) {0}; // prevent freeing roots with resp
	}
	respFree(resp);

concat:
	concatIndex(name, roots, threads, envelopes);
	listFree(roots);
}

int main(int argc, char *argv[]) {
	int exitStatus = 0;

	const char *host = NULL;
	const char *port = "imaps";
	const char *user = NULL;
	const char *passPath = NULL;

	bool idle = false;
	const char *mailbox = "Archive";
	const char *algo = "REFERENCES";
	const char *searchPath = NULL;

	for (
		int opt;
		0 < (opt = getopt(argc, argv, "A:C:H:S:T:a:h:im:p:qs:u:vw:y:"));
	) {
		switch (opt) {
			break; case 'A': concatIndexEntries = strtoul(optarg, NULL, 10);
			break; case 'C': {
				int error = chdir(optarg);
				if (error) err(EX_NOINPUT, "%s", optarg);
			}
			break; case 'H': concatHead = optarg;
			break; case 'S': searchPath = optarg;
			break; case 'T': baseTitle = optarg;
			break; case 'a': algo = optarg;
			break; case 'h': host = optarg;
			break; case 'i': idle = true;
			break; case 'm': baseMailto = optarg;
			break; case 'p': port = optarg;
			break; case 'q': quiet = true; exitStatus = EXIT_FAILURE;
			break; case 's': baseSubscribe = optarg;
			break; case 'u': baseURL = optarg;
			break; case 'v': imapVerbose = true;
			break; case 'w': passPath = optarg;
			break; case 'y': baseStylesheet = optarg;
			break; default:  return EX_USAGE;
		}
	}
	if (optind < argc) user = argv[optind++];
	if (optind < argc) mailbox = argv[optind++];

	if (!user) errx(EX_USAGE, "user required");
	if (!host) {
		host = strchr(user, '@');
		if (!host) errx(EX_USAGE, "host required");
		host++;
	}
	if (!baseTitle) baseTitle = mailbox;

	if (searchPath) {
		int error = searchRead(searchPath);
		if (error) err(EX_NOINPUT, "%s", searchPath);
	} else {
		searchRead("SEARCH");
	}
	searchDefault();

	char *pass = NULL;
	if (passPath) {
		FILE *file = fopen(passPath, "r");
		if (!file) err(EX_NOINPUT, "%s", passPath);

		size_t cap = 0;
		ssize_t len = getline(&pass, &cap, file);
		if (len < 0) err(EX_IOERR, "%s", passPath);
		if (len && pass[len - 1] == '\n') pass[len - 1] = '\0';
		fclose(file);
	} else {
		pass = getenv(ENV_PASSWORD);
		if (!pass) errx(EX_CONFIG, ENV_PASSWORD " unset");
	}

#ifdef __OpenBSD__
	struct {
		const char *path;
		const char *perm;
	} paths[] = {
		{ ".", "rwc" },
		{ tls_default_ca_cert_file(), "r" },
		{ concatHead, "r" },
		{0},
	};
	for (int i = 0; paths[i].path; ++i) {
		int error = unveil(paths[i].path, paths[i].perm);
		if (error) err(EX_NOINPUT, "%s", paths[i].path);
	}
	int error = pledge("stdio rpath wpath cpath inet dns", NULL);
	if (error) err(EX_OSERR, "pledge");
#endif

	struct IMAP imap = imapOpen(host, port);

#ifdef __OpenBSD__
	error = pledge("stdio rpath wpath cpath", NULL);
	if (error) err(EX_OSERR, "pledge");
#endif

	struct Resp resp;
	respFree(respOk(imapResp(&imap)));

	enum Atom login = atom("login");
	fprintf(imap.w, "%s LOGIN \"%s\" \"%s\"\r\n", Atoms[login], user, pass);
	for (; resp = respOk(imapResp(&imap)), resp.tag != login; respFree(resp));
	respFree(resp);

examine:;
	uint32_t uidNext = 0;
	uint32_t uidValidity = 0;
	enum Atom examine = atom("examine");
	fprintf(imap.w, "%s EXAMINE \"%s\"\r\n", Atoms[examine], mailbox);
	for (; resp = respOk(imapResp(&imap)), resp.tag != examine; respFree(resp)) {
		if (resp.resp != AtomOk || resp.code.len < 2) continue;
		enum Atom code = dataCheck(resp.code.ptr[0], Atom).atom;
		if (code == AtomUIDValidity) {
			uidValidity = dataCheck(resp.code.ptr[1], Number).number;
		} else if (code == AtomUIDNext) {
			uidNext = dataCheck(resp.code.ptr[1], Number).number;
		}
	}
	respFree(resp);

	uint32_t previous = uidRead("UIDVALIDITY");
	if (previous && uidValidity != previous) {
		errx(EX_TEMPFAIL, "UIDVALIDITY changed; fresh export required");
	}
	if (!previous) uidWrite("UIDVALIDITY", uidValidity);

	previous = uidRead("UIDNEXT");
	if (uidNext != previous) {
		exitStatus = EXIT_SUCCESS;
	} else if (idle) {
		goto idle;
	} else {
		goto logout;
	}
	createDirs();

	struct List threads = {0};
	enum Atom thread = atom("thread");
	fprintf(imap.w, "%s UID THREAD %s UTF-8 ALL\r\n", Atoms[thread], algo);
	for (; resp = respOk(imapResp(&imap)), resp.tag != thread; respFree(resp)) {
		if (resp.resp != AtomThread) continue;
		threads = resp.data;
		resp.data = (struct List) {0}; // prevent freeing threads with resp
	}
	respFree(resp);

	enum Atom export = atom("export");
	if (!exportFetch(imap.w, export, threads)) {
		goto concat;
	}
	for (
		size_t exportTags = 1;
		resp = respOk(imapResp(&imap)), resp.tag != export || --exportTags;
		respFree(resp)
	) {
		if (resp.resp != AtomFetch) continue;
		if (!resp.data.len) errx(EX_PROTOCOL, "missing FETCH data");
		struct List items = dataCheck(resp.data.ptr[0], List).list;
		if (exportData(imap.w, export, items)) exportTags++;
	}
	respFree(resp);

concat:;
	enum Atom concat = atom("concat");
	struct List envelopeItems = {0};
	struct Envelope *envelopes = calloc(threads.len, sizeof(*envelopes));
	if (!envelopes) err(EX_OSERR, "calloc");
	concatFetch(imap.w, concat, threads);
	for (; resp = respOk(imapResp(&imap)), resp.tag != concat; respFree(resp)) {
		if (resp.resp != AtomFetch) continue;
		if (!resp.data.len) errx(EX_PROTOCOL, "missing FETCH data");
		// Prevent freeing data in envelopes with resp:
		struct Data items = dataTake(&resp.data.ptr[0]);
		concatData(threads, envelopes, dataCheck(items, List).list);
		listPush(&envelopeItems, items);
	}
	respFree(resp);

	concatThreads(threads, envelopes);
	for (size_t i = 0; i < search.len; ++i) {
		searchThreads(
			&imap, threads, envelopes,
			search.names[i], search.exprs[i]
		);
	}

	for (size_t i = 0; i < threads.len; ++i) {
		envelopeFree(envelopes[i]);
	}
	free(envelopes);
	listFree(envelopeItems);
	listFree(threads);

	fflush(stdout);
	uidWrite("UIDNEXT", uidNext);
	if (!idle) goto logout;

idle:
	respFree(respOk(imapIdle(&imap, atom("idle"))));
	goto examine;
	
logout:
	fprintf(imap.w, "ayy LOGOUT\r\n");
	fclose(imap.r);
	fclose(imap.w);
	return exitStatus;
}