Offer a simple coloured display..
[s-mailx.git] / head.c
blobae2ce352509231845fa8d53a8f3ac17f10188bca
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Routines for processing and detecting headlines.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2013 Steffen "Daode" Nurpmeso <sdaoden@users.sf.net>.
6 */
7 /*
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
40 #ifndef HAVE_AMALGAMATION
41 # include "nail.h"
42 #endif
44 #ifdef HAVE_IDNA
45 # include <idna.h>
46 # include <stringprep.h>
47 #endif
49 struct cmatch_data {
50 size_t tlen; /* Length of .tdata */
51 char const *tdata; /* Template date - see _cmatch_data[] */
55 * Template characters for cmatch_data.tdata:
56 * 'A' An upper case char
57 * 'a' A lower case char
58 * ' ' A space
59 * '0' A digit
60 * 'O' An optional digit or space
61 * ':' A colon
62 * '+' Either a plus or a minus sign
64 static struct cmatch_data const _cmatch_data[] = {
65 { 24, "Aaa Aaa O0 00:00:00 0000" }, /* BSD/ISO C90 ctime */
66 { 28, "Aaa Aaa O0 00:00:00 AAA 0000" }, /* BSD tmz */
67 { 21, "Aaa Aaa O0 00:00 0000" }, /* SysV ctime */
68 { 25, "Aaa Aaa O0 00:00 AAA 0000" }, /* SysV tmz */
70 * RFC 822-alike From_ lines do not conform to RFC 4155, but seem to
71 * be used in the wild by UW-imap
73 { 30, "Aaa Aaa O0 00:00:00 0000 +0000" },
74 /* RFC 822 with zone spec; 1. military, 2. UT, 3. north america time
75 * zone strings; note that 1. is strictly speaking not correct as some
76 * letters are not used, and 2. is not because only "UT" is defined */
77 #define __reuse "Aaa Aaa O0 00:00:00 0000 AAA"
78 { 28 - 2, __reuse }, { 28 - 1, __reuse }, { 28 - 0, __reuse },
79 { 0, NULL }
81 #define _DATE_MINLEN 21
83 /* Skip over "word" as found in From_ line */
84 static char const * _from__skipword(char const *wp);
86 /* Match the date string against the date template (tp), return if match.
87 * See _cmatch_data[] for template character description */
88 static int _cmatch(size_t len, char const *date, char const *tp);
90 /* Check wether date is a valid 'From_' date.
91 * (Rather ctime(3) generated dates, according to RFC 4155) */
92 static int _is_date(char const *date);
94 /* Convert the domain part of a skinned address to IDNA.
95 * If an error occurs before Unicode information is available, revert the IDNA
96 * error to a normal CHAR one so that the error message doesn't talk Unicode */
97 #ifdef HAVE_IDNA
98 static struct addrguts * _idna_apply(struct addrguts *agp);
99 #endif
101 /* Classify and check a (possibly skinned) header body according to RFC
102 * *addr-spec* rules; if it (is assumed to has been) skinned it may however be
103 * also a file or a pipe command, so check that first, then.
104 * Otherwise perform content checking and isolate the domain part (for IDNA) */
105 static int _addrspec_check(int doskin, struct addrguts *agp);
107 static int gethfield(FILE *f, char **linebuf, size_t *linesize, int rem,
108 char **colon);
109 static int msgidnextc(const char **cp, int *status);
110 static int charcount(char *str, int c);
112 static char const *
113 _from__skipword(char const *wp)
115 char c = 0;
117 if (wp != NULL) {
118 while ((c = *wp++) != '\0' && ! blankchar(c)) {
119 if (c == '"') {
120 while ((c = *wp++) != '\0' && c != '"')
122 if (c != '"')
123 --wp;
126 for (; blankchar(c); c = *wp++)
129 return (c == 0 ? NULL : wp - 1);
132 static int
133 _cmatch(size_t len, char const *date, char const *tp)
135 int ret = 0;
137 while (len--) {
138 char c = date[len];
139 switch (tp[len]) {
140 case 'a':
141 if (! lowerchar(c))
142 goto jleave;
143 break;
144 case 'A':
145 if (! upperchar(c))
146 goto jleave;
147 break;
148 case ' ':
149 if (c != ' ')
150 goto jleave;
151 break;
152 case '0':
153 if (! digitchar(c))
154 goto jleave;
155 break;
156 case 'O':
157 if (c != ' ' && ! digitchar(c))
158 goto jleave;
159 break;
160 case ':':
161 if (c != ':')
162 goto jleave;
163 break;
164 case '+':
165 if (c != '+' && c != '-')
166 goto jleave;
167 break;
170 ret = 1;
171 jleave:
172 return (ret);
175 static int
176 _is_date(char const *date)
178 struct cmatch_data const *cmdp;
179 size_t dl = strlen(date);
180 int ret = 0;
182 if (dl >= _DATE_MINLEN)
183 for (cmdp = _cmatch_data; cmdp->tdata != NULL; ++cmdp)
184 if (dl == cmdp->tlen &&
185 (ret = _cmatch(dl, date, cmdp->tdata)))
186 break;
187 return (ret);
190 #ifdef HAVE_IDNA
191 static struct addrguts *
192 _idna_apply(struct addrguts *agp)
194 char *idna_utf8, *idna_ascii, *cs;
195 size_t sz, i;
197 sz = agp->ag_slen - agp->ag_sdom_start;
198 assert(sz > 0);
199 idna_utf8 = ac_alloc(sz + 1);
200 memcpy(idna_utf8, agp->ag_skinned + agp->ag_sdom_start, sz);
201 idna_utf8[sz] = '\0';
203 /* GNU Libidn settles on top of iconv(3) without having any fallback,
204 * so let's just let it perform the charset conversion, if any should
205 * be necessary */
206 if (! utf8) {
207 char const *tcs = charset_get_lc();
208 idna_ascii = idna_utf8;
209 idna_utf8 = stringprep_convert(idna_ascii, "UTF-8", tcs);
210 i = (idna_utf8 == NULL && errno == EINVAL);
211 ac_free(idna_ascii);
212 if (idna_utf8 == NULL) {
213 if (i)
214 fprintf(stderr, tr(179,
215 "Cannot convert from %s to %s\n"),
216 tcs, "UTF-8");
217 agp->ag_n_flags ^= NAME_ADDRSPEC_ERR_IDNA |
218 NAME_ADDRSPEC_ERR_CHAR;
219 goto jleave;
223 if (idna_to_ascii_8z(idna_utf8, &idna_ascii, 0) != IDNA_SUCCESS) {
224 agp->ag_n_flags ^= NAME_ADDRSPEC_ERR_IDNA |
225 NAME_ADDRSPEC_ERR_CHAR;
226 goto jleave1;
229 /* Replace the domain part of .ag_skinned with IDNA version */
230 sz = strlen(idna_ascii);
231 i = agp->ag_sdom_start;
232 cs = salloc(agp->ag_slen - i + sz + 1);
233 memcpy(cs, agp->ag_skinned, i);
234 memcpy(cs + i, idna_ascii, sz);
235 i += sz;
236 cs[i] = '\0';
238 agp->ag_skinned = cs;
239 agp->ag_slen = i;
240 NAME_ADDRSPEC_ERR_SET(agp->ag_n_flags,
241 NAME_NAME_SALLOC|NAME_SKINNED|NAME_IDNA, 0);
243 (free)(idna_ascii);
244 jleave1:
245 if (utf8)
246 ac_free(idna_utf8);
247 else
248 (free)(idna_utf8);
249 jleave:
250 return (agp);
252 #endif
254 static int
255 _addrspec_check(int skinned, struct addrguts *agp)
257 char *addr, *p, in_quote, in_domain, hadat;
258 union {char c; unsigned char u;} c;
259 #ifdef HAVE_IDNA
260 uc_it use_idna = !ok_blook(idna_disable);
261 #endif
263 agp->ag_n_flags |= NAME_ADDRSPEC_CHECKED;
264 addr = agp->ag_skinned;
266 if (agp->ag_iaddr_aend - agp->ag_iaddr_start == 0) {
267 NAME_ADDRSPEC_ERR_SET(agp->ag_n_flags, NAME_ADDRSPEC_ERR_EMPTY,
269 goto jleave;
272 /* If the field is not a recipient, it cannot be a file or a pipe */
273 if (! skinned)
274 goto jaddr_check;
277 * Excerpt from nail.1:
279 * Recipient address specifications
280 * The rules are: Any name which starts with a `|' character specifies
281 * a pipe, the command string following the `|' is executed and
282 * the message is sent to its standard input; any other name which
283 * contains a `@' character is treated as a mail address; any other
284 * name which starts with a `+' character specifies a folder name; any
285 * other name which contains a `/' character but no `!' or `%'
286 * character before also specifies a folder name; what remains is
287 * treated as a mail address.
289 if (*addr == '|') {
290 agp->ag_n_flags |= NAME_ADDRSPEC_ISPIPE;
291 goto jleave;
293 if (memchr(addr, '@', agp->ag_slen) == NULL) {
294 if (*addr == '+')
295 goto jisfile;
296 for (p = addr; (c.c = *p); ++p) {
297 if (c.c == '!' || c.c == '%')
298 break;
299 if (c.c == '/') {
300 jisfile: agp->ag_n_flags |= NAME_ADDRSPEC_ISFILE;
301 goto jleave;
306 jaddr_check:
307 in_quote = in_domain = hadat = 0;
309 for (p = addr; (c.c = *p++) != '\0';) {
310 if (c.c == '"') {
311 in_quote = ! in_quote;
312 } else if (c.u < 040 || c.u >= 0177) {
313 #ifdef HAVE_IDNA
314 if (in_domain && use_idna) {
315 if (use_idna == 1)
316 NAME_ADDRSPEC_ERR_SET(agp->ag_n_flags,
317 NAME_ADDRSPEC_ERR_IDNA, c.u);
318 use_idna = 2;
319 } else
320 #endif
321 break;
322 } else if (in_domain == 2) {
323 if ((c.c == ']' && *p != '\0') || c.c == '\\' ||
324 whitechar(c.c))
325 break;
326 } else if (in_quote && in_domain == 0) {
327 /*EMPTY*/;
328 } else if (c.c == '\\' && *p != '\0') {
329 ++p;
330 } else if (c.c == '@') {
331 if (hadat++) {
332 NAME_ADDRSPEC_ERR_SET(agp->ag_n_flags,
333 NAME_ADDRSPEC_ERR_ATSEQ, c.u);
334 goto jleave;
336 agp->ag_sdom_start = (size_t)(p - addr);
337 in_domain = (*p == '[') ? 2 : 1;
338 continue;
339 } else if (c.c == '(' || c.c == ')' ||
340 c.c == '<' || c.c == '>' ||
341 c.c == ',' || c.c == ';' || c.c == ':' ||
342 c.c == '\\' || c.c == '[' || c.c == ']')
343 break;
344 hadat = 0;
347 if (c.c != '\0') {
348 NAME_ADDRSPEC_ERR_SET(agp->ag_n_flags, NAME_ADDRSPEC_ERR_CHAR,
349 c.u);
350 goto jleave;
353 #ifdef HAVE_IDNA
354 if (use_idna == 2)
355 agp = _idna_apply(agp);
356 #endif
358 jleave:
359 return ((agp->ag_n_flags & NAME_ADDRSPEC_INVALID) != 0);
362 FL char const *
363 myaddrs(struct header *hp)
365 struct name *np;
366 char *rv = NULL;
368 if (hp != NULL && (np = hp->h_from) != NULL) {
369 if ((rv = np->n_fullname) != NULL)
370 goto jleave;
371 if ((rv = np->n_name) != NULL)
372 goto jleave;
375 if ((rv = ok_vlook(from)) != NULL)
376 goto jleave;
378 /* When invoking *sendmail* directly, it's its task
379 * to generate an otherwise undeterminable From: address.
380 * However, if the user sets *hostname*, accept his desire */
381 if (ok_vlook(smtp) != NULL || ok_vlook(hostname) != NULL) {
382 char *hn = nodename(1);
383 size_t sz = strlen(myname) + strlen(hn) + 2;
384 rv = salloc(sz);
385 snprintf(rv, sz, "%s@%s", myname, hn);
387 jleave:
388 return rv;
391 FL char const *
392 myorigin(struct header *hp)
394 char const *ret = NULL, *ccp;
395 struct name *np;
397 if ((ccp = myaddrs(hp)) != NULL &&
398 (np = lextract(ccp, GEXTRA|GFULL)) != NULL)
399 ret = np->n_flink != NULL ? ok_vlook(sender) : ccp;
400 return (ret);
403 FL int
404 is_head(char const *linebuf, size_t linelen) /* XXX verbose WARN */
406 char date[FROM_DATEBUF];
408 return ((linelen <= 5 || memcmp(linebuf, "From ", 5) != 0 ||
409 ! extract_date_from_from_(linebuf, linelen, date) ||
410 ! _is_date(date)) ? 0 : 1);
413 FL int
414 extract_date_from_from_(char const *line, size_t linelen,
415 char datebuf[FROM_DATEBUF])
417 int ret = 0;
418 char const *cp = line;
420 /* "From " */
421 cp = _from__skipword(cp);
422 if (cp == NULL)
423 goto jerr;
424 /* "addr-spec " */
425 cp = _from__skipword(cp);
426 if (cp == NULL)
427 goto jerr;
428 if (cp[0] == 't' && cp[1] == 't' && cp[2] == 'y') {
429 cp = _from__skipword(cp);
430 if (cp == NULL)
431 goto jerr;
434 linelen -= (size_t)(cp - line);
435 if (linelen < _DATE_MINLEN)
436 goto jerr;
437 if (cp[linelen - 1] == '\n') {
438 --linelen;
439 /* (Rather IMAP/POP3 only) */
440 if (cp[linelen - 1] == '\r')
441 --linelen;
442 if (linelen < _DATE_MINLEN)
443 goto jerr;
445 if (linelen >= FROM_DATEBUF)
446 goto jerr;
448 ret = 1;
449 jleave: memcpy(datebuf, cp, linelen);
450 datebuf[linelen] = '\0';
451 return (ret);
453 jerr: cp = tr(213, "<Unknown date>");
454 linelen = strlen(cp);
455 if (linelen >= FROM_DATEBUF)
456 linelen = FROM_DATEBUF;
457 goto jleave;
460 FL void
461 extract_header(FILE *fp, struct header *hp) /* XXX no header occur-cnt check */
463 struct header nh, *hq = &nh;
464 char *linebuf = NULL, *colon;
465 size_t linesize = 0;
466 int seenfields = 0, lc, c;
467 char const *val, *cp;
469 memset(hq, 0, sizeof *hq);
470 for (lc = 0; readline_restart(fp, &linebuf, &linesize, 0) > 0; lc++)
472 rewind(fp);
473 while ((lc = gethfield(fp, &linebuf, &linesize, lc, &colon)) >= 0) {
474 if ((val = thisfield(linebuf, "to")) != NULL) {
475 seenfields++;
476 hq->h_to = cat(hq->h_to, checkaddrs(
477 lextract(val, GTO|GFULL)));
478 } else if ((val = thisfield(linebuf, "cc")) != NULL) {
479 seenfields++;
480 hq->h_cc = cat(hq->h_cc, checkaddrs(
481 lextract(val, GCC|GFULL)));
482 } else if ((val = thisfield(linebuf, "bcc")) != NULL) {
483 seenfields++;
484 hq->h_bcc = cat(hq->h_bcc, checkaddrs(
485 lextract(val, GBCC|GFULL)));
486 } else if ((val = thisfield(linebuf, "from")) != NULL) {
487 seenfields++;
488 hq->h_from = cat(hq->h_from, checkaddrs(
489 lextract(val, GEXTRA|GFULL)));
490 } else if ((val = thisfield(linebuf, "reply-to")) != NULL) {
491 seenfields++;
492 hq->h_replyto = cat(hq->h_replyto, checkaddrs(
493 lextract(val, GEXTRA|GFULL)));
494 } else if ((val = thisfield(linebuf, "sender")) != NULL) {
495 seenfields++;
496 hq->h_sender = cat(hq->h_sender, checkaddrs(
497 lextract(val, GEXTRA|GFULL)));
498 } else if ((val = thisfield(linebuf,
499 "organization")) != NULL) {
500 seenfields++;
501 for (cp = val; blankchar(*cp); cp++)
503 hq->h_organization = hq->h_organization ?
504 save2str(hq->h_organization, cp) :
505 savestr(cp);
506 } else if ((val = thisfield(linebuf, "subject")) != NULL ||
507 (val = thisfield(linebuf, "subj")) != NULL) {
508 seenfields++;
509 for (cp = val; blankchar(*cp); cp++)
511 hq->h_subject = hq->h_subject ?
512 save2str(hq->h_subject, cp) :
513 savestr(cp);
514 } else
515 fprintf(stderr, tr(266,
516 "Ignoring header field \"%s\"\n"),
517 linebuf);
520 * In case the blank line after the header has been edited out.
521 * Otherwise, fetch the header separator.
523 if (linebuf) {
524 if (linebuf[0] != '\0') {
525 for (cp = linebuf; *(++cp) != '\0'; );
526 fseek(fp, (long)-(1 + cp - linebuf), SEEK_CUR);
527 } else {
528 if ((c = getc(fp)) != '\n' && c != EOF)
529 ungetc(c, fp);
532 if (seenfields) {
533 hp->h_to = hq->h_to;
534 hp->h_cc = hq->h_cc;
535 hp->h_bcc = hq->h_bcc;
536 hp->h_from = hq->h_from;
537 hp->h_replyto = hq->h_replyto;
538 hp->h_sender = hq->h_sender;
539 hp->h_organization = hq->h_organization;
540 hp->h_subject = hq->h_subject;
541 } else
542 fprintf(stderr, tr(267, "Restoring deleted header lines\n"));
543 if (linebuf)
544 free(linebuf);
548 * Return the desired header line from the passed message
549 * pointer (or NULL if the desired header field is not available).
550 * If mult is zero, return the content of the first matching header
551 * field only, the content of all matching header fields else.
553 FL char *
554 hfield_mult(char const *field, struct message *mp, int mult)
556 FILE *ibuf;
557 int lc;
558 size_t linesize = 0;
559 char *linebuf = NULL, *colon, *oldhfield = NULL;
560 char const *hfield;
562 if ((ibuf = setinput(&mb, mp, NEED_HEADER)) == NULL)
563 return NULL;
564 if ((lc = mp->m_lines - 1) < 0)
565 return NULL;
567 if ((mp->m_flag & MNOFROM) == 0 &&
568 readline_restart(ibuf, &linebuf, &linesize, 0) < 0)
569 goto jleave;
570 while (lc > 0) {
571 if ((lc = gethfield(ibuf, &linebuf, &linesize, lc, &colon)) < 0)
572 break;
573 if ((hfield = thisfield(linebuf, field)) != NULL) {
574 oldhfield = save2str(hfield, oldhfield);
575 if (mult == 0)
576 break;
580 jleave:
581 if (linebuf != NULL)
582 free(linebuf);
583 return (oldhfield);
587 * Return the next header field found in the given message.
588 * Return >= 0 if something found, < 0 elsewise.
589 * "colon" is set to point to the colon in the header.
590 * Must deal with \ continuations & other such fraud.
592 static int
593 gethfield(FILE *f, char **linebuf, size_t *linesize, int rem, char **colon)
595 char *line2 = NULL;
596 size_t line2size = 0;
597 char *cp, *cp2;
598 int c, isenc;
600 if (*linebuf == NULL)
601 *linebuf = srealloc(*linebuf, *linesize = 1);
602 **linebuf = '\0';
603 for (;;) {
604 if (--rem < 0)
605 return -1;
606 if ((c = readline_restart(f, linebuf, linesize, 0)) <= 0)
607 return -1;
608 for (cp = *linebuf; fieldnamechar(*cp & 0377); cp++);
609 if (cp > *linebuf)
610 while (blankchar(*cp & 0377))
611 cp++;
612 if (*cp != ':' || cp == *linebuf)
613 continue;
615 * I guess we got a headline.
616 * Handle wraparounding
618 *colon = cp;
619 cp = *linebuf + c;
620 for (;;) {
621 isenc = 0;
622 while (--cp >= *linebuf && blankchar(*cp & 0377));
623 cp++;
624 if (rem <= 0)
625 break;
626 if (cp-8 >= *linebuf && cp[-1] == '=' && cp[-2] == '?')
627 isenc |= 1;
628 ungetc(c = getc(f), f);
629 if (!blankchar(c))
630 break;
631 c = readline_restart(f, &line2, &line2size, 0);
632 if (c < 0)
633 break;
634 rem--;
635 for (cp2 = line2; blankchar(*cp2 & 0377); cp2++);
636 c -= cp2 - line2;
637 if (cp2[0] == '=' && cp2[1] == '?' && c > 8)
638 isenc |= 2;
639 if (cp + c >= *linebuf + *linesize - 2) {
640 size_t diff = cp - *linebuf;
641 size_t colondiff = *colon - *linebuf;
642 *linebuf = srealloc(*linebuf,
643 *linesize += c + 2);
644 cp = &(*linebuf)[diff];
645 *colon = &(*linebuf)[colondiff];
647 if (isenc != 3)
648 *cp++ = ' ';
649 memcpy(cp, cp2, c);
650 cp += c;
652 *cp = 0;
653 if (line2)
654 free(line2);
655 return rem;
657 /* NOTREACHED */
661 * Check whether the passed line is a header line of
662 * the desired breed. Return the field body, or 0.
664 FL char const *
665 thisfield(char const *linebuf, char const *field)
667 while (lowerconv(*linebuf) == lowerconv(*field)) {
668 ++linebuf;
669 ++field;
671 if (*field != '\0')
672 return NULL;
673 while (blankchar(*linebuf))
674 ++linebuf;
675 if (*linebuf++ != ':')
676 return NULL;
677 while (blankchar(*linebuf))
678 ++linebuf;
679 return linebuf;
683 * Get sender's name from this message. If the message has
684 * a bunch of arpanet stuff in it, we may have to skin the name
685 * before returning it.
687 FL char *
688 nameof(struct message *mp, int reptype)
690 char *cp, *cp2;
692 cp = skin(name1(mp, reptype));
693 if (reptype != 0 || charcount(cp, '!') < 2)
694 return(cp);
695 cp2 = strrchr(cp, '!');
696 cp2--;
697 while (cp2 > cp && *cp2 != '!')
698 cp2--;
699 if (*cp2 == '!')
700 return(cp2 + 1);
701 return(cp);
705 * Start of a "comment".
706 * Ignore it.
708 FL char const *
709 skip_comment(char const *cp)
711 int nesting = 1;
713 for (; nesting > 0 && *cp; cp++) {
714 switch (*cp) {
715 case '\\':
716 if (cp[1])
717 cp++;
718 break;
719 case '(':
720 nesting++;
721 break;
722 case ')':
723 nesting--;
724 break;
727 return (cp);
731 * Return the start of a route-addr (address in angle brackets),
732 * if present.
734 FL char const *
735 routeaddr(char const *name)
737 char const *np, *rp = NULL;
739 for (np = name; *np; np++) {
740 switch (*np) {
741 case '(':
742 np = skip_comment(&np[1]) - 1;
743 break;
744 case '"':
745 while (*np) {
746 if (*++np == '"')
747 break;
748 if (*np == '\\' && np[1])
749 np++;
751 break;
752 case '<':
753 rp = np;
754 break;
755 case '>':
756 return rp;
759 return NULL;
763 * Check if a name's address part contains invalid characters.
765 FL int
766 is_addr_invalid(struct name *np, int putmsg)
768 char cbuf[sizeof "'\\U12340'"], *name = np->n_name;
769 int f = np->n_flags, ok8bit = 1;
770 unsigned int c;
771 char const *fmt = "'\\x%02X'", *cs;
773 if ((f & NAME_ADDRSPEC_INVALID) == 0 || ! putmsg ||
774 (f & NAME_ADDRSPEC_ERR_EMPTY) != 0)
775 goto jleave;
777 if (f & NAME_ADDRSPEC_ERR_IDNA)
778 cs = tr(284, "Invalid domain name: \"%s\", character %s\n"),
779 fmt = "'\\U%04X'",
780 ok8bit = 0;
781 else if (f & NAME_ADDRSPEC_ERR_ATSEQ)
782 cs = tr(142, "\"%s\" contains invalid %s sequence\n");
783 else
784 cs = tr(143, "\"%s\" contains invalid character %s\n");
786 c = NAME_ADDRSPEC_ERR_GETWC(f);
787 if (ok8bit && c >= 040 && c <= 0177)
788 snprintf(cbuf, sizeof cbuf, "'%c'", c);
789 else
790 snprintf(cbuf, sizeof cbuf, fmt, c);
792 fprintf(stderr, cs, name, cbuf);
793 jleave:
794 return ((f & NAME_ADDRSPEC_INVALID) != 0);
797 FL char *
798 skin(char const *name)
800 struct addrguts ag;
801 char *ret = NULL;
803 if (name != NULL) {
804 (void)addrspec_with_guts(1, name, &ag);
805 ret = ag.ag_skinned;
806 if ((ag.ag_n_flags & NAME_NAME_SALLOC) == 0)
807 ret = savestrbuf(ret, ag.ag_slen);
809 return (ret);
812 /* TODO addrspec_with_guts: RFC 5322 */
813 FL int
814 addrspec_with_guts(int doskin, char const *name, struct addrguts *agp)
816 char const *cp;
817 char *cp2, *bufend, *nbuf, c;
818 char gotlt, gotaddr, lastsp;
820 memset(agp, 0, sizeof *agp);
822 if ((agp->ag_input = name) == NULL || /* XXX ever? */
823 (agp->ag_ilen = strlen(name)) == 0) {
824 agp->ag_skinned = UNCONST(""); /* ok: NAME_SALLOC is not set */
825 agp->ag_slen = 0;
826 agp->ag_n_flags |= NAME_ADDRSPEC_CHECKED;
827 NAME_ADDRSPEC_ERR_SET(agp->ag_n_flags, NAME_ADDRSPEC_ERR_EMPTY,
829 return (1);
832 if (! doskin || ! anyof(name, "(< ")) {
833 /*agp->ag_iaddr_start = 0;*/
834 agp->ag_iaddr_aend = agp->ag_ilen;
835 agp->ag_skinned = UNCONST(name); /* (NAME_SALLOC not set) */
836 agp->ag_slen = agp->ag_ilen;
837 agp->ag_n_flags = NAME_SKINNED;
838 return _addrspec_check(doskin, agp);
841 /* Something makes us think we have to perform the skin operation */
842 nbuf = ac_alloc(agp->ag_ilen + 1);
843 /*agp->ag_iaddr_start = 0;*/
844 cp2 = bufend = nbuf;
845 gotlt = gotaddr = lastsp = 0;
847 for (cp = name++; (c = *cp++) != '\0'; ) {
848 switch (c) {
849 case '(':
850 cp = skip_comment(cp);
851 lastsp = 0;
852 break;
853 case '"':
855 * Start of a "quoted-string".
856 * Copy it in its entirety.
857 * XXX RFC: quotes are "semantically invisible"
858 * XXX But it was explicitly added (Changelog.Heirloom,
859 * XXX [9.23] released 11/15/00, "Do not remove quotes
860 * XXX when skinning names"? No more info..
862 *cp2++ = c;
863 while ((c = *cp) != '\0') { /* TODO improve */
864 cp++;
865 if (c == '"') {
866 *cp2++ = c;
867 break;
869 if (c != '\\')
870 *cp2++ = c;
871 else if ((c = *cp) != '\0') {
872 *cp2++ = c;
873 cp++;
876 lastsp = 0;
877 break;
878 case ' ':
879 case '\t':
880 if (gotaddr == 1) {
881 gotaddr = 2;
882 agp->ag_iaddr_aend = (size_t)(cp - name);
884 if (cp[0] == 'a' && cp[1] == 't' && blankchar(cp[2]))
885 cp += 3, *cp2++ = '@';
886 else if (cp[0] == '@' && blankchar(cp[1]))
887 cp += 2, *cp2++ = '@';
888 else
889 lastsp = 1;
890 break;
891 case '<':
892 agp->ag_iaddr_start = (size_t)(cp - (name - 1));
893 cp2 = bufend;
894 gotlt = gotaddr = 1;
895 lastsp = 0;
896 break;
897 case '>':
898 if (gotlt) {
899 /* (_addrspec_check() verifies these later!) */
900 agp->ag_iaddr_aend = (size_t)(cp - name);
901 gotlt = 0;
902 while ((c = *cp) != '\0' && c != ',') {
903 cp++;
904 if (c == '(')
905 cp = skip_comment(cp);
906 else if (c == '"')
907 while ((c = *cp) != '\0') {
908 cp++;
909 if (c == '"')
910 break;
911 if (c == '\\' && *cp)
912 cp++;
915 lastsp = 0;
916 break;
918 /* FALLTRHOUGH */
919 default:
920 if (lastsp) {
921 lastsp = 0;
922 if (gotaddr)
923 *cp2++ = ' ';
925 *cp2++ = c;
926 if (c == ',') {
927 if (! gotlt) {
928 *cp2++ = ' ';
929 for (; blankchar(*cp); ++cp)
931 lastsp = 0;
932 bufend = cp2;
934 } else if (! gotaddr) {
935 gotaddr = 1;
936 agp->ag_iaddr_start = (size_t)(cp - name);
940 agp->ag_slen = (size_t)(cp2 - nbuf);
941 if (agp->ag_iaddr_aend == 0)
942 agp->ag_iaddr_aend = agp->ag_ilen;
944 agp->ag_skinned = savestrbuf(nbuf, agp->ag_slen);
945 ac_free(nbuf);
946 agp->ag_n_flags = NAME_NAME_SALLOC | NAME_SKINNED;
947 return _addrspec_check(doskin, agp);
951 * Fetch the real name from an internet mail address field.
953 FL char *
954 realname(char const *name)
956 char const *cp, *cq, *cstart = NULL, *cend = NULL;
957 char *rname, *rp;
958 struct str in, out;
959 int quoted, good, nogood;
961 if (name == NULL)
962 return NULL;
963 for (cp = UNCONST(name); *cp; cp++) {
964 switch (*cp) {
965 case '(':
966 if (cstart)
968 * More than one comment in address, doesn't
969 * make sense to display it without context.
970 * Return the entire field,
972 return mime_fromaddr(name);
973 cstart = cp++;
974 cp = skip_comment(cp);
975 cend = cp--;
976 if (cend <= cstart)
977 cend = cstart = NULL;
978 break;
979 case '"':
980 while (*cp) {
981 if (*++cp == '"')
982 break;
983 if (*cp == '\\' && cp[1])
984 cp++;
986 break;
987 case '<':
988 if (cp > name) {
989 cstart = name;
990 cend = cp;
992 break;
993 case ',':
995 * More than one address. Just use the first one.
997 goto brk;
1000 brk: if (cstart == NULL) {
1001 if (*name == '<')
1003 * If name contains only a route-addr, the
1004 * surrounding angle brackets don't serve any
1005 * useful purpose when displaying, so they
1006 * are removed.
1008 return prstr(skin(name));
1009 return mime_fromaddr(name);
1011 rp = rname = ac_alloc(cend - cstart + 1);
1013 * Strip quotes. Note that quotes that appear within a MIME-
1014 * encoded word are not stripped. The idea is to strip only
1015 * syntactical relevant things (but this is not necessarily
1016 * the most sensible way in practice).
1018 quoted = 0;
1019 for (cp = cstart; cp < cend; cp++) {
1020 if (*cp == '(' && !quoted) {
1021 cq = skip_comment(++cp);
1022 if (--cq > cend)
1023 cq = cend;
1024 while (cp < cq) {
1025 if (*cp == '\\' && &cp[1] < cq)
1026 cp++;
1027 *rp++ = *cp++;
1029 } else if (*cp == '\\' && &cp[1] < cend)
1030 *rp++ = *++cp;
1031 else if (*cp == '"') {
1032 quoted = !quoted;
1033 continue;
1034 } else
1035 *rp++ = *cp;
1037 *rp = '\0';
1038 in.s = rname;
1039 in.l = rp - rname;
1040 mime_fromhdr(&in, &out, TD_ISPR|TD_ICONV);
1041 ac_free(rname);
1042 rname = savestr(out.s);
1043 free(out.s);
1044 while (blankchar(*rname & 0377))
1045 rname++;
1046 for (rp = rname; *rp; rp++);
1047 while (--rp >= rname && blankchar(*rp & 0377))
1048 *rp = '\0';
1049 if (rp == rname)
1050 return mime_fromaddr(name);
1052 * mime_fromhdr() has converted all nonprintable characters to
1053 * question marks now. These and blanks are considered uninteresting;
1054 * if the displayed part of the real name contains more than 25% of
1055 * them, it is probably better to display the plain email address
1056 * instead.
1058 good = 0;
1059 nogood = 0;
1060 for (rp = rname; *rp && rp < &rname[20]; rp++)
1061 if (*rp == '?' || blankchar(*rp & 0377))
1062 nogood++;
1063 else
1064 good++;
1065 if (good*3 < nogood)
1066 return prstr(skin(name));
1067 return rname;
1071 * Fetch the sender's name from the passed message.
1072 * Reptype can be
1073 * 0 -- get sender's name for display purposes
1074 * 1 -- get sender's name for reply
1075 * 2 -- get sender's name for Reply
1077 FL char *
1078 name1(struct message *mp, int reptype)
1080 char *namebuf;
1081 size_t namesize;
1082 char *linebuf = NULL;
1083 size_t linesize = 0;
1084 char *cp, *cp2;
1085 FILE *ibuf;
1086 int f1st = 1;
1088 if ((cp = hfield1("from", mp)) != NULL && *cp != '\0')
1089 return cp;
1090 if (reptype == 0 && (cp = hfield1("sender", mp)) != NULL &&
1091 *cp != '\0')
1092 return cp;
1093 namebuf = smalloc(namesize = 1);
1094 namebuf[0] = 0;
1095 if (mp->m_flag & MNOFROM)
1096 goto out;
1097 if ((ibuf = setinput(&mb, mp, NEED_HEADER)) == NULL)
1098 goto out;
1099 if (readline_restart(ibuf, &linebuf, &linesize, 0) < 0)
1100 goto out;
1101 newname:
1102 if (namesize <= linesize)
1103 namebuf = srealloc(namebuf, namesize = linesize + 1);
1104 for (cp = linebuf; *cp && *cp != ' '; cp++)
1106 for (; blankchar(*cp & 0377); cp++);
1107 for (cp2 = &namebuf[strlen(namebuf)];
1108 *cp && !blankchar(*cp & 0377) && cp2 < namebuf + namesize - 1;)
1109 *cp2++ = *cp++;
1110 *cp2 = '\0';
1111 if (readline_restart(ibuf, &linebuf, &linesize, 0) < 0)
1112 goto out;
1113 if ((cp = strchr(linebuf, 'F')) == NULL)
1114 goto out;
1115 if (strncmp(cp, "From", 4) != 0)
1116 goto out;
1117 if (namesize <= linesize)
1118 namebuf = srealloc(namebuf, namesize = linesize + 1);
1119 while ((cp = strchr(cp, 'r')) != NULL) {
1120 if (strncmp(cp, "remote", 6) == 0) {
1121 if ((cp = strchr(cp, 'f')) == NULL)
1122 break;
1123 if (strncmp(cp, "from", 4) != 0)
1124 break;
1125 if ((cp = strchr(cp, ' ')) == NULL)
1126 break;
1127 cp++;
1128 if (f1st) {
1129 strncpy(namebuf, cp, namesize);
1130 f1st = 0;
1131 } else {
1132 cp2=strrchr(namebuf, '!')+1;
1133 strncpy(cp2, cp, (namebuf+namesize)-cp2);
1135 namebuf[namesize - 2] = '!';
1136 namebuf[namesize - 1] = '\0';
1137 goto newname;
1139 cp++;
1141 out:
1142 if (*namebuf != '\0' || ((cp = hfield1("return-path", mp))) == NULL ||
1143 *cp == '\0')
1144 cp = savestr(namebuf);
1145 if (linebuf)
1146 free(linebuf);
1147 free(namebuf);
1148 return cp;
1151 static int
1152 msgidnextc(const char **cp, int *status)
1154 int c;
1156 for (;;) {
1157 if (*status & 01) {
1158 if (**cp == '"') {
1159 *status &= ~01;
1160 (*cp)++;
1161 continue;
1163 if (**cp == '\\') {
1164 (*cp)++;
1165 if (**cp == '\0')
1166 goto eof;
1168 goto dfl;
1170 switch (**cp) {
1171 case '(':
1172 *cp = skip_comment(&(*cp)[1]);
1173 continue;
1174 case '>':
1175 case '\0':
1176 eof:
1177 return '\0';
1178 case '"':
1179 (*cp)++;
1180 *status |= 01;
1181 continue;
1182 case '@':
1183 *status |= 02;
1184 /*FALLTHRU*/
1185 default:
1186 dfl:
1187 c = *(*cp)++ & 0377;
1188 return *status & 02 ? lowerconv(c) : c;
1193 FL int
1194 msgidcmp(const char *s1, const char *s2)
1196 int q1 = 0, q2 = 0;
1197 int c1, c2;
1199 do {
1200 c1 = msgidnextc(&s1, &q1);
1201 c2 = msgidnextc(&s2, &q2);
1202 if (c1 != c2)
1203 return c1 - c2;
1204 } while (c1 && c2);
1205 return c1 - c2;
1209 * Count the occurances of c in str
1211 static int
1212 charcount(char *str, int c)
1214 char *cp;
1215 int i;
1217 for (i = 0, cp = str; *cp; cp++)
1218 if (*cp == c)
1219 i++;
1220 return(i);
1224 * See if the given header field is supposed to be ignored.
1226 FL int
1227 is_ign(char const *field, size_t fieldlen, struct ignoretab ignoret[2])
1229 char *realfld;
1230 int ret;
1232 if (ignoret == NULL)
1233 return 0;
1234 if (ignoret == allignore)
1235 return 1;
1237 * Lower-case the string, so that "Status" and "status"
1238 * will hash to the same place.
1240 realfld = ac_alloc(fieldlen + 1);
1241 i_strcpy(realfld, field, fieldlen + 1);
1242 if (ignoret[1].i_count > 0)
1243 ret = !member(realfld, ignoret + 1);
1244 else
1245 ret = member(realfld, ignoret);
1246 ac_free(realfld);
1247 return ret;
1250 FL int
1251 member(char const *realfield, struct ignoretab *table)
1253 struct ignore *igp;
1255 for (igp = table->i_head[hash(realfield)]; igp != 0; igp = igp->i_link)
1256 if (*igp->i_field == *realfield &&
1257 strcmp(igp->i_field, realfield) == 0)
1258 return (1);
1259 return (0);
1263 * Fake Sender for From_ lines if missing, e. g. with POP3.
1265 FL char const *
1266 fakefrom(struct message *mp)
1268 char const *name;
1270 if (((name = skin(hfield1("return-path", mp))) == NULL ||
1271 *name == '\0' ) &&
1272 ((name = skin(hfield1("from", mp))) == NULL ||
1273 *name == '\0'))
1275 * XXX MAILER-DAEMON is what an old MBOX manual page says.
1276 * RFC 4155 however requires a RFC 5322 (2822) conforming
1277 * "addr-spec", but we simply can't provide that
1279 name = "MAILER-DAEMON";
1280 return name;
1283 FL char const *
1284 fakedate(time_t t)
1286 char *cp, *cq;
1288 cp = ctime(&t);
1289 for (cq = cp; *cq && *cq != '\n'; ++cq)
1291 *cq = '\0';
1292 return savestr(cp);
1295 static char const *
1296 nexttoken(char const *cp)
1298 for (;;) {
1299 if (*cp == '\0')
1300 return NULL;
1301 if (*cp == '(') {
1302 int nesting = 0;
1304 while (*cp != '\0') {
1305 switch (*cp++) {
1306 case '(':
1307 nesting++;
1308 break;
1309 case ')':
1310 nesting--;
1311 break;
1313 if (nesting <= 0)
1314 break;
1316 } else if (blankchar(*cp) || *cp == ',')
1317 cp++;
1318 else
1319 break;
1321 return cp;
1325 * From username Fri Jan 2 20:13:51 2004
1326 * | | | | |
1327 * 0 5 10 15 20
1329 FL time_t
1330 unixtime(char const *fromline)
1332 char const *fp;
1333 char *xp;
1334 time_t t;
1335 int i, year, month, day, hour, minute, second;
1336 int tzdiff;
1337 struct tm *tmptr;
1339 for (fp = fromline; *fp && *fp != '\n'; fp++);
1340 fp -= 24;
1341 if (fp - fromline < 7)
1342 goto invalid;
1343 if (fp[3] != ' ')
1344 goto invalid;
1345 for (i = 0;;) {
1346 if (strncmp(&fp[4], month_names[i], 3) == 0)
1347 break;
1348 if (month_names[++i][0] == '\0')
1349 goto invalid;
1351 month = i + 1;
1352 if (fp[7] != ' ')
1353 goto invalid;
1354 day = strtol(&fp[8], &xp, 10);
1355 if (*xp != ' ' || xp != &fp[10])
1356 goto invalid;
1357 hour = strtol(&fp[11], &xp, 10);
1358 if (*xp != ':' || xp != &fp[13])
1359 goto invalid;
1360 minute = strtol(&fp[14], &xp, 10);
1361 if (*xp != ':' || xp != &fp[16])
1362 goto invalid;
1363 second = strtol(&fp[17], &xp, 10);
1364 if (*xp != ' ' || xp != &fp[19])
1365 goto invalid;
1366 year = strtol(&fp[20], &xp, 10);
1367 if (xp != &fp[24])
1368 goto invalid;
1369 if ((t = combinetime(year, month, day, hour, minute, second)) ==
1370 (time_t)-1)
1371 goto invalid;
1372 tzdiff = t - mktime(gmtime(&t));
1373 tmptr = localtime(&t);
1374 if (tmptr->tm_isdst > 0)
1375 tzdiff += 3600;
1376 t -= tzdiff;
1377 return t;
1378 invalid:
1379 time(&t);
1380 return t;
1383 FL time_t
1384 rfctime(char const *date)
1386 char const *cp = date;
1387 char *x;
1388 time_t t;
1389 int i, year, month, day, hour, minute, second;
1391 if ((cp = nexttoken(cp)) == NULL)
1392 goto invalid;
1393 if (alphachar(cp[0]) && alphachar(cp[1]) && alphachar(cp[2]) &&
1394 cp[3] == ',') {
1395 if ((cp = nexttoken(&cp[4])) == NULL)
1396 goto invalid;
1398 day = strtol(cp, &x, 10); /* XXX strtol */
1399 if ((cp = nexttoken(x)) == NULL)
1400 goto invalid;
1401 for (i = 0;;) {
1402 if (strncmp(cp, month_names[i], 3) == 0)
1403 break;
1404 if (month_names[++i][0] == '\0')
1405 goto invalid;
1407 month = i + 1;
1408 if ((cp = nexttoken(&cp[3])) == NULL)
1409 goto invalid;
1411 * RFC 5322, 4.3:
1412 * Where a two or three digit year occurs in a date, the year is to be
1413 * interpreted as follows: If a two digit year is encountered whose
1414 * value is between 00 and 49, the year is interpreted by adding 2000,
1415 * ending up with a value between 2000 and 2049. If a two digit year
1416 * is encountered with a value between 50 and 99, or any three digit
1417 * year is encountered, the year is interpreted by adding 1900.
1419 year = strtol(cp, &x, 10); /* XXX strtol */
1420 i = (int)(x - cp);
1421 if (i == 2 && year >= 0 && year <= 49)
1422 year += 2000;
1423 else if (i == 3 || (i == 2 && year >= 50 && year <= 99))
1424 year += 1900;
1425 if ((cp = nexttoken(x)) == NULL)
1426 goto invalid;
1427 hour = strtol(cp, &x, 10); /* XXX strtol */
1428 if (*x != ':')
1429 goto invalid;
1430 cp = &x[1];
1431 minute = strtol(cp, &x, 10);
1432 if (*x == ':') {
1433 cp = &x[1];
1434 second = strtol(cp, &x, 10);
1435 } else
1436 second = 0;
1437 if ((t = combinetime(year, month, day, hour, minute, second)) ==
1438 (time_t)-1)
1439 goto invalid;
1440 if ((cp = nexttoken(x)) != NULL) {
1441 int sign = -1;
1442 char buf[3];
1444 switch (*cp) {
1445 case '-':
1446 sign = 1;
1447 /*FALLTHRU*/
1448 case '+':
1449 cp++;
1451 if (digitchar(cp[0]) && digitchar(cp[1]) && digitchar(cp[2]) &&
1452 digitchar(cp[3])) {
1453 buf[2] = '\0';
1454 buf[0] = cp[0];
1455 buf[1] = cp[1];
1456 t += strtol(buf, NULL, 10) * sign * 3600;/*XXX strtrol*/
1457 buf[0] = cp[2];
1458 buf[1] = cp[3];
1459 t += strtol(buf, NULL, 10) * sign * 60; /* XXX strtol*/
1461 /* TODO WE DO NOT YET PARSE (OBSOLETE) ZONE NAMES
1462 * TODO once again, Christos Zoulas and NetBSD Mail have done
1463 * TODO a really good job already, but using strptime(3), which
1464 * TODO is not portable. Nonetheless, WE must improve, not
1465 * TODO at last because we simply ignore obsolete timezones!!
1466 * TODO See RFC 5322, 4.3! */
1468 return t;
1469 invalid:
1470 return 0;
1473 #define is_leapyear(Y) ((((Y) % 100 ? (Y) : (Y) / 100) & 3) == 0)
1475 FL time_t
1476 combinetime(int year, int month, int day, int hour, int minute, int second)
1478 time_t t;
1480 if (second < 0 || minute < 0 || hour < 0 || day < 1)
1481 return -1;
1482 t = second + minute * 60 + hour * 3600 + (day - 1) * 86400;
1483 if (month > 1)
1484 t += 86400 * 31;
1485 if (month > 2)
1486 t += 86400 * (is_leapyear(year) ? 29 : 28);
1487 if (month > 3)
1488 t += 86400 * 31;
1489 if (month > 4)
1490 t += 86400 * 30;
1491 if (month > 5)
1492 t += 86400 * 31;
1493 if (month > 6)
1494 t += 86400 * 30;
1495 if (month > 7)
1496 t += 86400 * 31;
1497 if (month > 8)
1498 t += 86400 * 31;
1499 if (month > 9)
1500 t += 86400 * 30;
1501 if (month > 10)
1502 t += 86400 * 31;
1503 if (month > 11)
1504 t += 86400 * 30;
1505 year -= 1900;
1506 t += (year - 70) * 31536000 + ((year - 69) / 4) * 86400 -
1507 ((year - 1) / 100) * 86400 + ((year + 299) / 400) * 86400;
1508 return t;
1511 FL void
1512 substdate(struct message *m)
1514 char const *cp;
1517 * Determine the date to print in faked 'From ' lines. This is
1518 * traditionally the date the message was written to the mail
1519 * file. Try to determine this using RFC message header fields,
1520 * or fall back to current time.
1522 if ((cp = hfield1("received", m)) != NULL) {
1523 while ((cp = nexttoken(cp)) != NULL && *cp != ';') {
1525 cp++;
1526 while (alnumchar(*cp));
1528 if (cp && *++cp)
1529 m->m_time = rfctime(cp);
1531 if (m->m_time == 0 || m->m_time > time_current.tc_time) {
1532 if ((cp = hfield1("date", m)) != NULL)
1533 m->m_time = rfctime(cp);
1535 if (m->m_time == 0 || m->m_time > time_current.tc_time)
1536 m->m_time = time_current.tc_time;
1539 FL int
1540 check_from_and_sender(struct name *fromfield, struct name *senderfield)
1542 if (fromfield && fromfield->n_flink && senderfield == NULL) {
1543 fprintf(stderr, "A Sender: field is required with multiple "
1544 "addresses in From: field.\n");
1545 return 1;
1547 if (senderfield && senderfield->n_flink) {
1548 fprintf(stderr, "The Sender: field may contain "
1549 "only one address.\n");
1550 return 2;
1552 return 0;
1555 FL char *
1556 getsender(struct message *mp)
1558 char *cp;
1559 struct name *np;
1561 if ((cp = hfield1("from", mp)) == NULL ||
1562 (np = lextract(cp, GEXTRA|GSKIN)) == NULL)
1563 return NULL;
1564 return np->n_flink != NULL ? skin(hfield1("sender", mp)) : np->n_name;
1567 FL int
1568 grab_headers(struct header *hp, enum gfield gflags, int subjfirst)
1570 /* TODO grab_headers: again, check counts etc. against RFC;
1571 * TODO (now assumes check_from_and_sender() is called afterwards ++ */
1572 int errs;
1573 int volatile comma;
1575 errs = 0;
1576 comma = (ok_blook(bsdcompat) || ok_blook(bsdmsgs)) ? 0 : GCOMMA;
1578 if (gflags & GTO)
1579 hp->h_to = grab_names("To: ", hp->h_to, comma, GTO|GFULL);
1581 if (subjfirst && (gflags & GSUBJECT))
1582 hp->h_subject = readstr_input("Subject: ", hp->h_subject);
1584 if (gflags & GCC)
1585 hp->h_cc = grab_names("Cc: ", hp->h_cc, comma, GCC|GFULL);
1587 if (gflags & GBCC)
1588 hp->h_bcc = grab_names("Bcc: ", hp->h_bcc, comma, GBCC|GFULL);
1590 if (gflags & GEXTRA) {
1591 if (hp->h_from == NULL)
1592 hp->h_from = lextract(myaddrs(hp), GEXTRA|GFULL);
1593 hp->h_from = grab_names("From: ", hp->h_from, comma,
1594 GEXTRA|GFULL);
1595 if (hp->h_replyto == NULL)
1596 hp->h_replyto = lextract(ok_vlook(replyto),
1597 GEXTRA | GFULL);
1598 hp->h_replyto = grab_names("Reply-To: ", hp->h_replyto, comma,
1599 GEXTRA|GFULL);
1600 if (hp->h_sender == NULL)
1601 hp->h_sender = extract(ok_vlook(sender), GEXTRA|GFULL);
1602 hp->h_sender = grab_names("Sender: ", hp->h_sender, comma,
1603 GEXTRA|GFULL);
1604 if (hp->h_organization == NULL)
1605 hp->h_organization = ok_vlook(ORGANIZATION);
1606 hp->h_organization = readstr_input("Organization: ",
1607 hp->h_organization);
1610 if (! subjfirst && (gflags & GSUBJECT))
1611 hp->h_subject = readstr_input("Subject: ", hp->h_subject);
1613 return errs;