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
|
/* Copyright (C) 2019-2021 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 <stdbool.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");
}
static void printNums(FILE *file, struct List nums) {
for (size_t i = 0; i < nums.len; ++i) {
uint32_t num = dataCheck(nums.ptr[i], Number).number;
fprintf(file, "%s%" PRIu32, (i ? "," : ""), num);
}
}
int main(int argc, char *argv[]) {
int rppFlags = 0;
const char *host = NULL;
const char *port = "imaps";
const char *mailbox = "INBOX";
const char *subject = NULL;
const char *from = NULL;
const char *to = NULL;
const char *cc = NULL;
bool unseen = false;
bool idle = false;
const char *move = NULL;
bool seen = false;
int opt;
while (0 < (opt = getopt(argc, argv, "C:F:M:S:T:Uh:im:p:svw"))) {
switch (opt) {
break; case 'C': cc = optarg;
break; case 'F': from = optarg;
break; case 'M': move = optarg;
break; case 'S': subject = optarg;
break; case 'T': to = optarg;
break; case 'U': unseen = true;
break; case 'h': host = optarg;
break; case 'i': idle = true;
break; case 'm': mailbox = optarg;
break; case 'p': port = optarg;
break; case 's': seen = true;
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");
struct Resp resp;
struct IMAP imap = imapOpen(host, port);
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);
enum Atom examine = atom("examine");
fprintf(
imap.w, "%s %s \"%s\"\r\n",
Atoms[examine], (move || seen ? "SELECT" : "EXAMINE"), mailbox
);
for (; resp = respOk(imapResp(&imap)), resp.tag != examine; respFree(resp));
respFree(resp);
search:;
struct List nums = {0};
enum Atom search = atom("search");
fprintf(imap.w, "%s SEARCH CHARSET UTF-8 ALL", Atoms[search]);
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);
if (unseen) fprintf(imap.w, " UNSEEN");
fprintf(imap.w, "\r\n");
for (; resp = respOk(imapResp(&imap)), resp.tag != search; respFree(resp)) {
if (resp.resp != AtomSearch) continue;
nums = resp.data;
resp.data = (struct List) {0};
}
respFree(resp);
if (!nums.len) {
if (!idle) errx(EX_TEMPFAIL, "no matching messages");
respFree(respOk(imapIdle(&imap, atom("idle"))));
goto search;
}
enum Atom fetch = atom("fetch");
fprintf(imap.w, "%s FETCH ", Atoms[fetch]);
printNums(imap.w, nums);
fprintf(
imap.w, " (BODY[HEADER.FIELDS (" FETCH_HEADERS ")] BODY[TEXT])\r\n"
);
for (; resp = respOk(imapResp(&imap)), resp.tag != fetch; 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;
struct Data header = {0};
struct Data body = {0};
for (size_t i = 0; i + 2 < items.len; ++i) {
struct Data item = items.ptr[i];
if (item.type != Atom || item.atom != AtomBody) continue;
if (items.ptr[i + 1].type != List) continue;
struct List sect = items.ptr[i + 1].list;
if (!sect.len || sect.ptr[0].type != Atom) continue;
if (sect.ptr[0].atom == AtomHeader) {
header = items.ptr[i + 2];
} else if (sect.ptr[0].atom == AtomText) {
body = items.ptr[i + 2];
}
}
mboxrd(
dataCheck(header, String).string,
dataCheck(body, String).string
);
}
respFree(resp);
if (seen) {
enum Atom sto = atom("store");
fprintf(imap.w, "%s STORE ", Atoms[sto]);
printNums(imap.w, nums);
fprintf(imap.w, " +FLAGS.SILENT (\\Seen)\r\n");
for (; resp = respOk(imapResp(&imap)), resp.tag != sto; respFree(resp));
respFree(resp);
}
if (move) {
enum Atom mov = atom("move");
fprintf(imap.w, "%s MOVE ", Atoms[mov]);
printNums(imap.w, nums);
fprintf(imap.w, " \"%s\"\r\n", move);
for (; resp = respOk(imapResp(&imap)), resp.tag != mov; respFree(resp));
respFree(resp);
}
fprintf(imap.w, "ayy LOGOUT\r\n");
fclose(imap.r);
fclose(imap.w);
}
|