about summary refs log tree commit diff
path: root/archive.c
blob: f3c0059d873069e10dc22e6dad008d6109cc5189 (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
/* 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 "imap.h"

#define ENV_PASSWORD "BUBGER_IMAP_PASSWORD"

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 void flatten(struct List *flat, struct List nested) {
	for (size_t i = 0; i < nested.len; ++i) {
		if (nested.ptr[i].type == List) {
			flatten(flat, nested.ptr[i].list);
		} else {
			listPush(flat, nested.ptr[i]);
		}
	}
}

static enum Atom fetchNew(FILE *imap, struct List threads) {
	struct List uids = {0};
	flatten(&uids, threads);
	for (size_t i = uids.len - 1; i < uids.len; --i) {
		if (uids.ptr[i].type != Number) {
			errx(EX_PROTOCOL, "invalid thread UID");
		}
		uint32_t uid = uids.ptr[i].number;
		int error = 0;
		if (!error) error = access(uidPath(uid, "atom"), F_OK);
		if (!error) error = access(uidPath(uid, "html"), F_OK);
		if (!error) error = access(uidPath(uid, "mbox"), F_OK);
		if (!error) {
			uids.ptr[i] = uids.ptr[--uids.len];
		}
	}
	enum Atom tag = 0;
	if (!uids.len) goto done;

	tag = atom("fetchNew");
	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)\r\n");

done:
	listFree(uids);
	return tag;
}

static void checkValidity(uint32_t validity) {
	FILE *file = fopen("UIDVALIDITY", "r");
	if (file) {
		uint32_t previous;
		int n = fscanf(file, "%" SCNu32, &previous);
		if (n < 1) errx(EX_DATAERR, "invalid UIDVALIDITY file");

		if (validity != previous) {
			errx(EX_TEMPFAIL, "UIDVALIDITY changed; fresh export required");
		}

	} else {
		FILE *file = fopen("UIDVALIDITY", "w");
		if (!file) err(EX_CANTCREAT, "UIDVALIDITY");

		int n = fprintf(file, "%" PRIu32 "\n", validity);
		if (n < 0) err(EX_IOERR, "UIDVALIDITY");

		int error = fclose(file);
		if (error) err(EX_IOERR, "UIDVALIDITY");
	}
}

int main(int argc, char *argv[]) {
	const char *host = NULL;
	const char *port = "imaps";
	const char *user = NULL;
	const char *passPath = NULL;

	const char *mailbox = "Archive";
	const char *algo = "REFERENCES";
	const char *search = "ALL";

	const char *title = NULL;
	const char *headPath = NULL;

	for (int opt; 0 < (opt = getopt(argc, argv, "C:a:h:p:s:t:vw:"));) {
		switch (opt) {
			break; case 'C': {
				int error = chdir(optarg);
				if (error) err(EX_NOINPUT, "%s", optarg);
			}
			break; case 'a': algo = optarg;
			break; case 'h': headPath = optarg;
			break; case 'p': port = optarg;
			break; case 's': search = optarg;
			break; case 't': title = optarg;
			break; case 'v': imapVerbose = true;
			break; case 'w': passPath = optarg;
		}
	}
	if (optind < argc) host = argv[optind++];
	if (optind < argc) user = argv[optind++];
	if (optind < argc) mailbox = argv[optind++];

	if (!host) errx(EX_USAGE, "host required");
	if (!user) errx(EX_USAGE, "user required");
	if (!title) title = mailbox;

	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");
	}

	enum Atom login = 0;
	enum Atom examine = atom("EXAMINE");
	enum Atom thread = atom("THREAD");
	enum Atom export = 0;

	FILE *imap = imapOpen(host, port);
	for (struct Resp resp; resp = imapResp(imap), resp.resp != AtomBye;) {
		if (resp.resp == AtomNo || resp.resp == AtomBad) {
			errx(EX_CONFIG, "%s %s", Atoms[resp.resp], resp.text);
		}

		if (!login) {
			login = atom("login");
			fprintf(
				imap, "%s LOGIN \"%s\" \"%s\"\r\n",
				Atoms[login], user, pass
			);
		}

		if (resp.tag == login) {
			fprintf(imap, "%s EXAMINE \"%s\"\r\n", Atoms[examine], mailbox);
		}

		if (
			resp.resp == AtomOk &&
			resp.code.len > 1 &&
			resp.code.ptr[0].type == Atom &&
			resp.code.ptr[0].atom == AtomUIDValidity
		) {
			if (resp.code.ptr[1].type != Number) {
				errx(EX_PROTOCOL, "invalid UIDVALIDITY");
			}
			checkValidity(resp.code.ptr[1].number);
		}

		if (resp.tag == examine) {
			fprintf(
				imap, "%s UID THREAD %s UTF-8 %s\r\n",
				Atoms[thread], algo, search
			);
		}

		if (resp.resp == thread) {
			if (!resp.data.len) {
				errx(EX_TEMPFAIL, "no messages matching %s", search);
			}
			export = fetchNew(imap, resp.data);
		}

		respFree(resp);
	}
	fclose(imap);
}