contrib/ksmtpproxy: Fix typo
[navymail.git] / mbox-walk.c
blobbcfd675cdf9c3e0aefb63ebb8464c846acf38191
1 /* iterate messages in mbox
2 * Copyright (C) 2012 Kirill Smelkov <kirr@navytux.spb.ru>
4 * This program is free software: you can Use, Study, Modify and Redistribute it
5 * under the terms of the GNU General Public License version 2. This program is
6 * distributed WITHOUT ANY WARRANTY. See COPYING file for full License terms.
7 */
10 #include "navymail/mbox-walk.h"
11 #include "cache.h"
14 void prepare_mbox_walk(struct mbox_walk *m, FILE *fmbox)
16 m->fmbox = fmbox;
17 strbuf_init(&m->msg, 0);
18 strbuf_init(&m->line, 0);
19 m->seen_from_ = 0;
23 /* see if line is an mbox(5) "From " line */
24 static int is_from_line(struct strbuf *line)
27 * 1) "From "
28 * 2) envelope sender-addr ; addr-spec as per RFC 2822 3.4.1
29 * 3) whitespace
30 * 4) timestamp ; date-time as output by asctime(3)
32 * e.g.
34 * From example@example.com Fri Jun 23 02:56:55 2000
37 int envelope_ok, date_ok;
38 const char *p, *q;
40 /* "From " */
41 if (strncmp(line->buf, "From ", 5))
42 return 0;
44 p = line->buf + 5;
46 /* envelope */
47 for (q=p; *q && !isspace(*q); ++q)
49 envelope_ok = (q!=p); /* simply check whether it is present */
51 /* whitespace */
52 for (; isspace(*q); ++q)
55 /* date */
56 date_ok = !parse_date_basic(q, /* timestamp,offset = */NULL,NULL);
58 if (!envelope_ok || !date_ok)
59 warning("mbox: Strange From_ line \"%s\"--%s%s\n",
60 line->buf,
61 !envelope_ok ? " !envelope_ok" : "",
62 !date_ok ? " !date_ok" : "");
65 return 1;
68 int next_mbox_entry(struct mbox_walk *m)
70 int eof, entry_ok;
72 strbuf_reset(&m->msg);
74 while (1) {
75 strbuf_addbuf(&m->msg, &m->line);
77 eof = strbuf_getwholeline(&m->line, m->fmbox, '\n');
78 if (eof)
79 break;
81 if (is_from_line(&m->line)) {
82 if (m->seen_from_)
83 break;
85 /* this was first From_ line - continue
86 * NOTE: we are skipping everything before first From_ !
88 m->seen_from_ = 1;
92 entry_ok = (m->msg.len != 0);
93 if (!entry_ok) {
94 /* free msg & line on stop */
95 strbuf_release(&m->msg);
96 strbuf_release(&m->line);
99 return entry_ok;