forked from rc0/mairix
-
Notifications
You must be signed in to change notification settings - Fork 8
/
imapinterface.c
422 lines (378 loc) · 10.5 KB
/
imapinterface.c
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#include <stdio.h>
#include <string.h>
#ifdef USE_OPENSSL
#include <openssl/ssl.h>
#include <openssl/err.h>
#endif
#include "imapinterface.h"
#include "imap.h"
#include "mairix.h"
struct imap_ll *
imap_start(const char *pipe, const char *server, const char *username, const char *password)
{
#ifdef USE_OPENSSL
SSL_CTX *sslctx;
#endif
struct imap_ll *imapc;
#ifdef USE_OPENSSL
SSL_load_error_strings();
SSL_library_init();
sslctx = SSL_CTX_new(SSLv23_client_method());
if (!sslctx) {
fprintf(stderr, "SSL_CTX_new failed\n");
ERR_print_errors_fp(stderr);
return NULL;
}
SSL_CTX_load_verify_locations(sslctx, NULL, "/etc/ssl/certs");
SSL_CTX_set_verify(sslctx, SSL_VERIFY_PEER, NULL);
#endif
imapc = pipe ? imap_ll_pipe_connect(pipe) : imap_ll_connect(server, "143");
if (!imapc) return NULL;
switch (imap_login(
imapc, username, -1, password, -1
#ifdef USE_OPENSSL
, sslctx, server
#endif
)) {
case imap_login_error:
case imap_login_denied:
return NULL;
case imap_login_ok:
break;
}
return imapc;
}
static void
add_imap_message_to_list(const char *folder, const char *uidvalidity, const char *uid, struct msgpath_array *arr)
{
char *pseudopath;
pseudopath = Malloc(strlen(folder) + strlen(uidvalidity) + strlen(uid) + 3);
sprintf(pseudopath, "%s:%s:%s", uidvalidity, uid, folder);
if (arr->n == arr->max) {
arr->max += 1024;
arr->paths = grow_array(struct msgpath, arr->max, arr->paths);
}
arr->paths[arr->n].type = MTY_IMAP;
arr->paths[arr->n].src.mpf.path = pseudopath;
++arr->n;
}
static void
scan_folder(struct imap_ll *imapc, const char *folder, struct msgpath_array *msgs)
{
struct imap_ll_tokenlist *cmd, *result, *l, *l2, *l3;
const char *uidvalidity;
char *uid;
int deleted;
long exists;
uidvalidity = imap_select(imapc, folder, -1, 0, &exists);
if (!uidvalidity) return;
if (exists < 1) return;
cmd = imap_ll_build(
TLTYPE_TAGGED,
TLTYPE_ATOM, "UID", (size_t)-1,
TLTYPE_ATOM, "FETCH", (size_t)-1,
TLTYPE_ATOM, "1:*", (size_t)-1,
TLTYPE_ATOM, "FLAGS", (size_t)-1,
TLTYPE_END
);
result = imap_ll_command(imapc, cmd, 40);
imap_ll_freeline(cmd);
if (!result) {
fprintf(stderr, "unable to FETCH in folder \"%s\": no response from IMAP server\n", folder);
return;
}
if (0 != strcmp(imap_ll_status(result), "OK")) {
fprintf(stderr, "unable to FETCH in folder \"%s\":\n", folder);
imap_ll_pprint(result, 0, stderr);
imap_ll_freeline(result);
return;
}
for (l = result->first; l != result->last; l = l->next) {
l2 = l->first;
if (!l2) continue;
if (l2->type != TLTYPE_ATOM) continue; /* sequence */
l2 = l2->next;
if (l2->type != TLTYPE_ATOM) continue;
if (0 != strcmp(l2->leaf, "FETCH")) continue;
l2 = l2->next;
if (l2->type != TLTYPE_LIST) continue;
uid = NULL;
deleted = 0;
for (l2 = l2->first; l2 && (l2->next); l2 = l2->next) {
if (l2->type != TLTYPE_ATOM) continue;
if (0 == strcmp(l2->leaf, "UID")) {
l2 = l2->next;
if (l2->type != TLTYPE_ATOM) continue;
uid = l2->leaf;
} else if (0 == strcmp(l2->leaf, "FLAGS")) {
l2 = l2->next;
if (l2->type != TLTYPE_LIST) continue;
for (l3 = l2->first; l3; l3 = l3->next) {
if (l3->type != TLTYPE_ATOM) continue;
if (0 == strcmp(l3->leaf, "\\Deleted")) {
deleted = 1;
}
}
}
}
if (deleted) continue;
if (!uid) continue;
add_imap_message_to_list(folder, uidvalidity, uid, msgs);
}
imap_ll_freeline(result);
}
void
build_imap_message_list(
const char *folders, struct msgpath_array *msgs,
struct globber_array *omit_globs, struct imap_ll *imapc
)
{
char **folderlist;
int n_folders;
int i;
split_on_colons(folders, &n_folders, &folderlist);
for (i = 0; i < n_folders; i++) {
if (!is_globber_array_match(omit_globs, folderlist[i])) {
scan_folder(imapc, folderlist[i], msgs);
}
}
}
int
imap_fetch_message_raw(
const char *pseudopath, struct imap_ll *imapc,
void (*callback)(const char *, size_t, void *), void *arg
)
{
struct imap_ll_tokenlist *cmd, *result, *l, *l2;
const char *uidvalidity, *uid, *folder, *p;
size_t uidvalidity_len;
size_t uid_len, raw_len = 0, got_uid_len = 0;
const char *actual_uidvalidity, *got_uid, *got_raw;
/* first part is the uidvalidity */
uidvalidity = pseudopath;
p = strchr(pseudopath, ':');
if (!p) return 0;
uidvalidity_len = p-pseudopath;
uid = p+1;
/* second part is the uid */
p = strchr(uid, ':');
if (!p) return 0;
uid_len = p-uid;
folder = p+1;
actual_uidvalidity = imap_select(imapc, folder, -1, 0, NULL);
if (!actual_uidvalidity) return 0;
if (
(strlen(actual_uidvalidity) != uidvalidity_len) ||
(0 != memcmp(uidvalidity, actual_uidvalidity, uidvalidity_len))
) {
fprintf(stderr, "IMAP message \"%s\" cannot be loaded because UIDVALIDITY changed\n", pseudopath);
return 0;
}
cmd = imap_ll_build(
TLTYPE_TAGGED,
TLTYPE_ATOM, "UID", (size_t)-1,
TLTYPE_ATOM, "FETCH", (size_t)-1,
TLTYPE_ATOM, uid, uid_len,
TLTYPE_ATOM, "BODY.PEEK[]", (size_t)-1,
TLTYPE_END
);
result = imap_ll_command(imapc, cmd, 40);
imap_ll_freeline(cmd);
if (!result) {
fprintf(stderr, "unable to FETCH message \"%s\": no response from IMAP server\n", pseudopath);
return 0;
}
if (0 != strcmp(imap_ll_status(result), "OK")) {
fprintf(stderr, "unable to FETCH message \"%s\":\n", pseudopath);
imap_ll_pprint(result, 0, stderr);
imap_ll_freeline(result);
return 0;
}
for (l = result->first; l != result->last; l = l->next) {
l2 = l->first;
if (!l2) continue;
if (l2->type != TLTYPE_ATOM) continue; /* sequence */
l2 = l2->next;
if (l2->type != TLTYPE_ATOM) continue;
if (0 != strcmp(l2->leaf, "FETCH")) continue;
l2 = l2->next;
if (l2->type != TLTYPE_LIST) continue;
got_uid = got_raw = NULL;
for (l2 = l2->first; l2 && (l2->next); l2 = l2->next) {
if (l2->type != TLTYPE_ATOM) continue;
if (0 == strcmp(l2->leaf, "UID")) {
l2 = l2->next;
if (l2->type != TLTYPE_ATOM) continue;
got_uid = l2->leaf;
got_uid_len = l2->leaflen;
} else if (0 == strcmp(l2->leaf, "BODY[]")) {
l2 = l2->next;
if (l2->type != TLTYPE_STRING) continue;
got_raw = l2->leaf;
raw_len = l2->leaflen;
}
}
if ((!got_uid) || (!got_raw)) continue;
if (got_uid_len != uid_len) continue;
if (0 != memcmp(got_uid, uid, got_uid_len)) continue;
callback(got_raw, raw_len, arg);
imap_ll_freeline(result);
return 1;
}
fprintf(stderr, "IMAP server did not return message \"%s\"\n", pseudopath);
imap_ll_freeline(result);
return 0;
}
struct make_rfc822_from_imap_s {
const char *pseudopath;
struct rfc822 *r;
};
static void
callback822(const char *data, size_t len, void *arg)
{
struct make_rfc822_from_imap_s *s = (struct make_rfc822_from_imap_s *)arg;
struct msg_src src; /* assuming this is for error message presentation only! */
src.type = MS_FILE;
src.filename = (char *)(s->pseudopath); /* XXX breaking "const" */
s->r = data_to_rfc822(&src, (char *)data /* XXX breaking "const" */, len, NULL);
}
struct rfc822 *
make_rfc822_from_imap(const char *pseudopath, struct imap_ll *imapc)
{
struct make_rfc822_from_imap_s s;
s.pseudopath = pseudopath;
imap_fetch_message_raw(pseudopath, imapc, callback822, &s);
return s.r;
}
void
imap_clear_folder(struct imap_ll *imapc, const char *folder)
{
struct imap_ll_tokenlist *cmd, *result;
if (!imap_select(imapc, folder, -1, 1, NULL)) return;
cmd = imap_ll_build(
TLTYPE_TAGGED,
TLTYPE_ATOM, "STORE", (size_t)-1,
TLTYPE_ATOM, "1:*", (size_t)-1,
TLTYPE_ATOM, "+FLAGS", (size_t)-1,
TLTYPE_ATOM, "\\Deleted", (size_t)-1,
TLTYPE_END
);
result = imap_ll_command(imapc, cmd, 10);
imap_ll_freeline(cmd);
imap_ll_freeline(result);
cmd = imap_ll_build(
TLTYPE_TAGGED,
TLTYPE_ATOM, "EXPUNGE", (size_t)-1,
TLTYPE_END
);
result = imap_ll_command(imapc, cmd, 10);
imap_ll_freeline(cmd);
imap_ll_freeline(result);
}
void
create_folder(struct imap_ll *imapc, const char *folder)
{
struct imap_ll_tokenlist *cmd, *result;
cmd = imap_ll_build(
TLTYPE_TAGGED,
TLTYPE_ATOM, "CREATE", (size_t)-1,
TLTYPE_STRING, folder, (size_t)-1,
TLTYPE_END
);
result = imap_ll_command(imapc, cmd, 10);
imap_ll_freeline(cmd);
imap_ll_freeline(result);
}
void
imap_copy_message(struct imap_ll *imapc, const char *pseudopath, const char *to_folder)
{
struct imap_ll_tokenlist *cmd, *result;
const char *uidvalidity, *uid, *folder, *p;
size_t uidvalidity_len, uid_len;
const char *actual_uidvalidity;
/* first part is the uidvalidity */
uidvalidity = pseudopath;
p = strchr(pseudopath, ':');
if (!p) return;
uidvalidity_len = p-pseudopath;
uid = p+1;
/* second part is the uid */
p = strchr(uid, ':');
if (!p) return;
uid_len = p-uid;
folder = p+1;
actual_uidvalidity = imap_select(imapc, folder, -1, 0, NULL);
if (!actual_uidvalidity) return;
if (
(strlen(actual_uidvalidity) != uidvalidity_len) ||
(0 != memcmp(uidvalidity, actual_uidvalidity, uidvalidity_len))
) {
return;
}
cmd = imap_ll_build(
TLTYPE_TAGGED,
TLTYPE_ATOM, "UID", (size_t)-1,
TLTYPE_ATOM, "COPY", (size_t)-1,
TLTYPE_ATOM, uid, uid_len,
TLTYPE_STRING, to_folder, (size_t)-1,
TLTYPE_END
);
result = imap_ll_command(imapc, cmd, 10);
if (imap_ll_is_trycreate(result)) {
imap_ll_freeline(result);
create_folder(imapc, to_folder);
result = imap_ll_command(imapc, cmd, 10);
}
imap_ll_freeline(cmd);
if (0 != strcmp(imap_ll_status(result), "OK")) {
fprintf(stderr, "unable to COPY message to folder \"%s\":\n", to_folder);
imap_ll_pprint(result, 0, stderr);
}
imap_ll_freeline(result);
}
void
imap_append_new_message(
struct imap_ll *imapc, const char *folder,
const unsigned char *data, size_t len,
int seen, int answered, int flagged
)
{
struct imap_ll_tokenlist *flags, *cmd, *result;
if (!imap_select(imapc, folder, -1, 1, NULL)) return;
flags = imap_ll_build(TLTYPE_LIST, TLTYPE_END);
if (seen) {
imap_ll_append(flags, imap_ll_build(
TLTYPE_ATOM, "\\Seen", (size_t)-1, TLTYPE_END
));
}
if (answered) {
imap_ll_append(flags, imap_ll_build(
TLTYPE_ATOM, "\\Answered", (size_t)-1, TLTYPE_END
));
}
if (flagged) {
imap_ll_append(flags, imap_ll_build(
TLTYPE_ATOM, "\\Flagged", (size_t)-1, TLTYPE_END
));
}
cmd = imap_ll_build(
TLTYPE_TAGGED,
TLTYPE_ATOM, "APPEND", (size_t)-1,
TLTYPE_STRING, folder, (size_t)-1,
TLTYPE_SUB, flags,
TLTYPE_STRING, data, len,
TLTYPE_END
);
result = imap_ll_command(imapc, cmd, 10);
if (imap_ll_is_trycreate(result)) {
imap_ll_freeline(result);
create_folder(imapc, folder);
result = imap_ll_command(imapc, cmd, 10);
}
imap_ll_freeline(cmd);
if (0 != strcmp(imap_ll_status(result), "OK")) {
fprintf(stderr, "unable to APPEND message to folder \"%s\":\n", folder);
imap_ll_pprint(result, 0, stderr);
}
imap_ll_freeline(result);
}