forked from nicm/fdm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch-maildir.c
429 lines (358 loc) · 9.77 KB
/
fetch-maildir.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
423
424
425
426
427
428
429
/* $Id$ */
/*
* Copyright (c) 2006 Nicholas Marriott <nicholas.marriott@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <glob.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "fdm.h"
#include "fetch.h"
int fetch_maildir_commit(struct account *, struct mail *);
void fetch_maildir_abort(struct account *);
u_int fetch_maildir_total(struct account *);
void fetch_maildir_desc(struct account *, char *, size_t);
void fetch_maildir_free(void *);
int fetch_maildir_makepaths(struct account *);
void fetch_maildir_freepaths(struct account *);
int fetch_maildir_poll(struct account *);
int fetch_maildir_state_init(struct account *, struct fetch_ctx *);
int fetch_maildir_state_build(struct account *, struct fetch_ctx *);
int fetch_maildir_state_next(struct account *, struct fetch_ctx *);
int fetch_maildir_state_open(struct account *, struct fetch_ctx *);
int fetch_maildir_state_mail(struct account *, struct fetch_ctx *);
struct fetch fetch_maildir = {
"maildir",
fetch_maildir_state_init,
NULL,
fetch_maildir_commit,
fetch_maildir_abort,
fetch_maildir_total,
fetch_maildir_desc
};
void
fetch_maildir_free(void *ptr)
{
struct fetch_maildir_mail *aux = ptr;
xfree(aux);
}
/* Make an array of all the paths to visit. */
int
fetch_maildir_makepaths(struct account *a)
{
struct fetch_maildir_data *data = a->data;
char *path;
u_int i, j;
glob_t g;
struct stat sb;
data->paths = xmalloc(sizeof *data->paths);
ARRAY_INIT(data->paths);
for (i = 0; i < ARRAY_LENGTH(data->maildirs); i++) {
path = ARRAY_ITEM(data->maildirs, i);
if (glob(path, GLOB_BRACE|GLOB_NOCHECK, NULL, &g) != 0) {
log_warn("%s: glob(\"%s\")", a->name, path);
goto error;
}
if (g.gl_pathc < 1)
fatalx("glob returned garbage");
for (j = 0; j < (u_int) g.gl_pathc; j++) {
xasprintf(&path, "%s/cur", g.gl_pathv[j]);
ARRAY_ADD(data->paths, path);
if (stat(path, &sb) != 0) {
log_warn("%s: %s", a->name, path);
ARRAY_TRUNC(data->paths, 1);
continue;
}
if (!S_ISDIR(sb.st_mode)) {
errno = ENOTDIR;
log_warn("%s: %s", a->name, path);
ARRAY_TRUNC(data->paths, 1);
continue;
}
xasprintf(&path, "%s/new", g.gl_pathv[j]);
ARRAY_ADD(data->paths, path);
if (stat(path, &sb) != 0) {
log_warn("%s", path);
ARRAY_TRUNC(data->paths, 2);
continue;
}
if (!S_ISDIR(sb.st_mode)) {
errno = ENOTDIR;
log_warn("%s", path);
ARRAY_TRUNC(data->paths, 2);
continue;
}
}
globfree(&g);
}
return (0);
error:
fetch_maildir_freepaths(a);
return (-1);
}
/* Free the array. */
void
fetch_maildir_freepaths(struct account *a)
{
struct fetch_maildir_data *data = a->data;
u_int i;
if (data->paths == NULL)
return;
for (i = 0; i < ARRAY_LENGTH(data->paths); i++)
xfree(ARRAY_ITEM(data->paths, i));
ARRAY_FREEALL(data->paths);
data->paths = NULL;
}
/* Count maildir total. */
int
fetch_maildir_poll(struct account *a)
{
struct fetch_maildir_data *data = a->data;
u_int i;
char *path, entry[MAXPATHLEN];
DIR *dirp;
struct dirent *dp;
struct stat sb;
data->total = 0;
for (i = 0; i < ARRAY_LENGTH(data->paths); i++) {
path = ARRAY_ITEM(data->paths, i);
log_debug("%s: trying path: %s", a->name, path);
if ((dirp = opendir(path)) == NULL) {
log_warn("%s: %s: opendir", a->name, path);
return (-1);
}
while ((dp = readdir(dirp)) != NULL) {
if (dp->d_type == DT_REG) {
data->total++;
continue;
}
if (dp->d_type != DT_UNKNOWN)
continue;
if (ppath(entry, sizeof entry, "%s/%s", path,
dp->d_name) != 0) {
log_warn("%s: %s: printpath", a->name, path);
closedir(dirp);
return (-1);
}
if (stat(entry, &sb) != 0) {
log_warn("%s: %s: stat", a->name, entry);
closedir(dirp);
return (-1);
}
if (!S_ISREG(sb.st_mode))
continue;
data->total++;
}
if (closedir(dirp) != 0) {
log_warn("%s: %s: closedir", a->name, path);
return (-1);
}
}
return (0);
}
/* Commit mail. */
int
fetch_maildir_commit(struct account *a, struct mail *m)
{
struct fetch_maildir_data *data = a->data;
struct fetch_maildir_mail *aux;
aux = m->auxdata;
if (m->decision == DECISION_DROP) {
/* Add mail to the unlink list. */
ARRAY_ADD(&data->unlinklist, xstrdup(aux->path));
}
return (FETCH_AGAIN);
}
/* Abort fetch. */
void
fetch_maildir_abort(struct account *a)
{
struct fetch_maildir_data *data = a->data;
u_int i;
for (i = 0; i < ARRAY_LENGTH(&data->unlinklist); i++)
xfree(ARRAY_ITEM(&data->unlinklist, i));
ARRAY_FREE(&data->unlinklist);
if (data->dirp != NULL)
closedir(data->dirp);
fetch_maildir_freepaths(a);
}
/* Return total mails. */
u_int
fetch_maildir_total(struct account *a)
{
struct fetch_maildir_data *data = a->data;
return (data->total);
}
/* Initialise maildir fetch context. */
int
fetch_maildir_state_init(struct account *a, struct fetch_ctx *fctx)
{
struct fetch_maildir_data *data = a->data;
if (fetch_maildir_makepaths(a) != 0)
return (FETCH_ERROR);
if (ARRAY_EMPTY(data->paths)) {
log_warnx("%s: no maildirs found", a->name);
return (FETCH_ERROR);
}
data->index = 0;
data->dirp = NULL;
ARRAY_INIT(&data->unlinklist);
/* Poll counts mails and exits. */
if (fctx->flags & FETCH_POLL) {
if (fetch_maildir_poll(a) != 0)
return (FETCH_ERROR);
fetch_maildir_freepaths(a);
return (FETCH_EXIT);
}
fctx->state = fetch_maildir_state_open;
return (FETCH_AGAIN);
}
/* Next state. Move to next path. */
int
fetch_maildir_state_next(struct account *a, struct fetch_ctx *fctx)
{
struct fetch_maildir_data *data = a->data;
char *path;
u_int i;
for (i = 0; i < ARRAY_LENGTH(&data->unlinklist); i++) {
path = ARRAY_ITEM(&data->unlinklist, i);
log_debug2("%s: unlinking: %s", a->name, path);
if (unlink(path) != 0) {
log_warn("%s: %s: unlink", a->name, path);
return (FETCH_ERROR);
}
xfree(path);
}
ARRAY_FREE(&data->unlinklist);
if (data->index < ARRAY_LENGTH(data->paths))
data->index++;
if (data->index == ARRAY_LENGTH(data->paths)) {
if (!(fctx->flags & FETCH_EMPTY))
return (FETCH_BLOCK);
fetch_maildir_freepaths(a);
return (FETCH_EXIT);
}
fctx->state = fetch_maildir_state_open;
return (FETCH_AGAIN);
}
/* Open state. */
int
fetch_maildir_state_open(struct account *a, struct fetch_ctx *fctx)
{
struct fetch_maildir_data *data = a->data;
char *path;
path = ARRAY_ITEM(data->paths, data->index);
/* Open the directory. */
log_debug2("%s: trying path: %s", a->name, path);
if ((data->dirp = opendir(path)) == NULL) {
log_warn("%s: %s: opendir", a->name, path);
return (FETCH_ERROR);
}
fctx->state = fetch_maildir_state_mail;
return (FETCH_AGAIN);
}
/* Mail state. Find and read mail file. */
int
fetch_maildir_state_mail(struct account *a, struct fetch_ctx *fctx)
{
struct fetch_maildir_data *data = a->data;
struct mail *m = fctx->mail;
struct fetch_maildir_mail *aux;
struct dirent *dp;
char *path, *maildir, name[MAXPATHLEN];
struct stat sb;
uintmax_t size;
int fd;
ssize_t n;
path = ARRAY_ITEM(data->paths, data->index);
restart:
/* Read the next dir entry. */
dp = readdir(data->dirp);
if (dp == NULL) {
if (closedir(data->dirp) != 0) {
log_warn("%s: %s: closedir", a->name, path);
return (FETCH_ERROR);
}
data->dirp = NULL;
fctx->state = fetch_maildir_state_next;
return (FETCH_AGAIN);
}
if (ppath(name, sizeof name, "%s/%s", path, dp->d_name) != 0) {
log_warn("%s: %s: printpath", a->name, path);
return (FETCH_ERROR);
}
if (stat(name, &sb) != 0) {
log_warn("%s: %s: stat", a->name, name);
return (FETCH_ERROR);
}
if (!S_ISREG(sb.st_mode))
goto restart;
/* Open the mail. */
if (mail_open(m, sb.st_size) != 0) {
log_warn("%s: failed to create mail", a->name);
return (FETCH_ERROR);
}
/* Got a valid entry, start reading it. */
log_debug2("%s: reading mail from: %s", a->name, name);
size = sb.st_size;
if (sb.st_size <= 0) {
m->size = 0;
return (FETCH_MAIL);
} else if (size > SIZE_MAX || size > conf.max_size) {
m->size = SIZE_MAX;
return (FETCH_MAIL);
}
/* Open the file. */
if ((fd = open(name, O_RDONLY, 0)) == -1) {
log_warn("%s: %s: open", a->name, name);
return (FETCH_ERROR);
}
/* Add the tags. */
maildir = xbasename(xdirname(path));
default_tags(&m->tags, maildir);
add_tag(&m->tags, "maildir", "%s", maildir);
add_tag(&m->tags, "maildir_path", "%s", xdirname(path));
add_tag(&m->tags, "maildir_file", "%s", path);
/* Add aux data. */
aux = xmalloc(sizeof *aux);
strlcpy(aux->path, name, sizeof aux->path);
m->auxdata = aux;
m->auxfree = fetch_maildir_free;
/* Read the mail. */
if ((n = read(fd, m->data, size)) == -1 || (size_t) n != size) {
close(fd);
log_warn("%s: %s: read", a->name, name);
return (FETCH_ERROR);
}
close(fd);
log_debug2("%s: read %ju bytes", a->name, size);
m->size = size;
return (FETCH_MAIL);
}
void
fetch_maildir_desc(struct account *a, char *buf, size_t len)
{
struct fetch_maildir_data *data = a->data;
char *maildirs;
maildirs = fmt_strings("maildir ", data->maildirs);
strlcpy(buf, maildirs, len);
xfree(maildirs);
}