summary refs log tree commit diff
path: root/imbox.c
blob: 8c4d351d4e05273665c515b8a14711d471ad7238 (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
/* Copyright (C) 2019, 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/>.
 *
 * 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 <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>

#ifdef DECLARE_RPP
#define RPP_STDIN 1
char * readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags);
#else
#include <readpassphrase.h>
#endif

#include "imap.h"

#define FETCH_HEADERS \
	"Date Subject From Sender Reply-To To Cc Bcc " \
	"Message-Id In-Reply-To References " \
	"MIME-Version Content-Type Content-Disposition Content-Transfer-Encoding"

static void mboxrd(char *header, char *body) {
	printf("From mboxrd@z Thu Jan  1 00:00:00 1970\n");
	for (char *crlf; (crlf = strstr(header, "\r\n")); header = &crlf[2]) {
		*crlf = '\0';
		printf("%s\n", header);
	}
	for (char *crlf; (crlf = strstr(body, "\r\n")); body = &crlf[2]) {
		*crlf = '\0';
		char *from = body;
		while (*from == '>') from++;
		if (!strncmp(from, "From ", 5)) {
			printf(">%s\n", body);
		} else {
			printf("%s\n", body);
		}
	}
	printf("\n");
}

int main(int argc, char *argv[]) {
	const char *host = NULL;
	const char *port = "imaps";
	const char *mailbox = "INBOX";
	const char *subject = "[PATCH";
	const char *from = NULL;
	const char *to = NULL;
	const char *cc = NULL;
	int rppFlags = 0;

	int opt;
	while (0 < (opt = getopt(argc, argv, "C:F:S:T:h:m:p:vw"))) {
		switch (opt) {
			break; case 'C': cc = optarg;
			break; case 'F': from = optarg;
			break; case 'S': subject = optarg;
			break; case 'T': to = optarg;
			break; case 'h': host = optarg;
			break; case 'm': mailbox = optarg;
			break; case 'p': port = optarg;
			break; case 'v': imapVerbose = true;
			break; case 'w': rppFlags |= RPP_STDIN;
			break; default:  return EX_USAGE;
		}
	}

	const char *user = argv[optind];
	if (!user) errx(EX_USAGE, "username required");

	if (!host) {
		host = strchr(user, '@');
		if (!host) errx(EX_USAGE, "no domain in username");
		host++;
	}

	char buf[1024];
	char *pass = readpassphrase(
		(rppFlags & RPP_STDIN ? "" : "Password: "),
		buf, sizeof(buf), rppFlags
	);
	if (!pass) err(EX_UNAVAILABLE, "readpassphrase");

	enum Atom login = 0;
	enum Atom examine = atom("examine");

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

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

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

		if (resp.tag == examine) {
			fprintf(imap.w, "%s SEARCH CHARSET UTF-8", Atoms[AtomSearch]);
			if (subject) fprintf(imap.w, " SUBJECT \"%s\"", subject);
			if (from) fprintf(imap.w, " FROM \"%s\"", from);
			if (to) fprintf(imap.w, " TO \"%s\"", to);
			if (cc) fprintf(imap.w, " CC \"%s\"", cc);
			fprintf(imap.w, "\r\n");
		}

		if (resp.resp == AtomSearch) {
			if (!resp.data.len) errx(EX_TEMPFAIL, "no matching messages");
			fprintf(imap.w, "%s FETCH ", Atoms[AtomFetch]);
			for (size_t i = 0; i < resp.data.len; ++i) {
				uint32_t num = dataCheck(resp.data.ptr[i], Number).number;
				fprintf(imap.w, "%s%" PRIu32, (i ? "," : ""), num);
			}
			fprintf(
				imap.w,
				" (BODY[HEADER.FIELDS (" FETCH_HEADERS ")] BODY[TEXT])\r\n"
			);
		}

		if (resp.resp == AtomFetch) {
			if (!resp.data.len) errx(EX_PROTOCOL, "missing fetch data");
			struct List items = dataCheck(resp.data.ptr[0], List).list;
			struct Data header = {0};
			struct Data body = {0};
			for (size_t i = 0; i < items.len; ++i) {
				struct Data item = items.ptr[i];
				if (item.type != List) continue;
				if (!item.list.len) continue;
				if (item.list.ptr[0].type != Atom) continue;
				if (item.list.ptr[0].atom == AtomHeader) {
					if (i + 1 < items.len) header = items.ptr[i + 1];
				}
				if (item.list.ptr[0].atom == AtomText) {
					if (i + 1 < items.len) body = items.ptr[i + 1];
				}
			}
			mboxrd(
				dataCheck(header, String).string,
				dataCheck(body, String).string
			);
		}

		if (resp.tag == AtomFetch) {
			fprintf(imap.w, "ayy LOGOUT\r\n");
		}
	}
	fclose(imap.r);
	fclose(imap.w);
}