(Pseudo) Fix history (non gabbiness) by stripping PS_ARGLIST_MASK
[s-mailx.git] / head.c
blobaa554549850eabb2e6e9fdf512555ac419b520b5
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 - 2016 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
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. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
35 #undef n_FILE
36 #define n_FILE head
38 #ifndef HAVE_AMALGAMATION
39 # include "nail.h"
40 #endif
42 #ifdef HAVE_IDNA
43 # if HAVE_IDNA == HAVE_IDNA_LIBIDNA
44 # include <idna.h>
45 # include <idn-free.h>
46 # include <stringprep.h>
47 # elif HAVE_IDNA == HAVE_IDNA_IDNKIT
48 # include <idn/api.h>
49 # endif
50 #endif
52 struct cmatch_data {
53 size_t tlen; /* Length of .tdata */
54 char const *tdata; /* Template date - see _cmatch_data[] */
57 /* Template characters for cmatch_data.tdata:
58 * 'A' An upper case char
59 * 'a' A lower case char
60 * ' ' A space
61 * '0' A digit
62 * 'O' An optional digit or space
63 * ':' A colon
64 * '+' Either a plus or a minus sign */
65 static struct cmatch_data const _cmatch_data[] = {
66 { 24, "Aaa Aaa O0 00:00:00 0000" }, /* BSD/ISO C90 ctime */
67 { 28, "Aaa Aaa O0 00:00:00 AAA 0000" }, /* BSD tmz */
68 { 21, "Aaa Aaa O0 00:00 0000" }, /* SysV ctime */
69 { 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 be used
71 * in the wild (by UW-imap) */
72 { 30, "Aaa Aaa O0 00:00:00 0000 +0000" },
73 /* RFC 822 with zone spec; 1. military, 2. UT, 3. north america time
74 * zone strings; note that 1. is strictly speaking not correct as some
75 * letters are not used, and 2. is not because only "UT" is defined */
76 #define __reuse "Aaa Aaa O0 00:00:00 0000 AAA"
77 { 28 - 2, __reuse }, { 28 - 1, __reuse }, { 28 - 0, __reuse },
78 { 0, NULL }
80 #define _DATE_MINLEN 21
82 /* Skip over "word" as found in From_ line */
83 static char const * _from__skipword(char const *wp);
85 /* Match the date string against the date template (tp), return if match.
86 * See _cmatch_data[] for template character description */
87 static int _cmatch(size_t len, char const *date,
88 char const *tp);
90 /* Check whether 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 /* Return the next header field found in the given message.
108 * Return >= 0 if something found, < 0 elsewise.
109 * "colon" is set to point to the colon in the header.
110 * Must deal with \ continuations & other such fraud */
111 static int gethfield(FILE *f, char **linebuf, size_t *linesize,
112 int rem, char **colon);
114 static int msgidnextc(char const **cp, int *status);
116 /* Count the occurances of c in str */
117 static int charcount(char *str, int c);
119 static char const * nexttoken(char const *cp);
121 static char const *
122 _from__skipword(char const *wp)
124 char c = 0;
125 NYD2_ENTER;
127 if (wp != NULL) {
128 while ((c = *wp++) != '\0' && !blankchar(c)) {
129 if (c == '"') {
130 while ((c = *wp++) != '\0' && c != '"')
132 if (c != '"')
133 --wp;
136 for (; blankchar(c); c = *wp++)
139 NYD2_LEAVE;
140 return (c == 0 ? NULL : wp - 1);
143 static int
144 _cmatch(size_t len, char const *date, char const *tp)
146 int ret = 0;
147 NYD2_ENTER;
149 while (len--) {
150 char c = date[len];
151 switch (tp[len]) {
152 case 'a':
153 if (!lowerchar(c))
154 goto jleave;
155 break;
156 case 'A':
157 if (!upperchar(c))
158 goto jleave;
159 break;
160 case ' ':
161 if (c != ' ')
162 goto jleave;
163 break;
164 case '0':
165 if (!digitchar(c))
166 goto jleave;
167 break;
168 case 'O':
169 if (c != ' ' && !digitchar(c))
170 goto jleave;
171 break;
172 case ':':
173 if (c != ':')
174 goto jleave;
175 break;
176 case '+':
177 if (c != '+' && c != '-')
178 goto jleave;
179 break;
182 ret = 1;
183 jleave:
184 NYD2_LEAVE;
185 return ret;
188 static int
189 _is_date(char const *date)
191 struct cmatch_data const *cmdp;
192 size_t dl;
193 int rv = 0;
194 NYD2_ENTER;
196 if ((dl = strlen(date)) >= _DATE_MINLEN)
197 for (cmdp = _cmatch_data; cmdp->tdata != NULL; ++cmdp)
198 if (dl == cmdp->tlen && (rv = _cmatch(dl, date, cmdp->tdata)))
199 break;
200 NYD2_LEAVE;
201 return rv;
204 #ifdef HAVE_IDNA
205 # if HAVE_IDNA == HAVE_IDNA_LIBIDNA
206 static struct addrguts *
207 _idna_apply(struct addrguts *agp)
209 char *idna_utf8, *idna_ascii, *cs;
210 size_t sz, i;
211 NYD_ENTER;
213 sz = agp->ag_slen - agp->ag_sdom_start;
214 assert(sz > 0);
215 idna_utf8 = ac_alloc(sz +1);
216 memcpy(idna_utf8, agp->ag_skinned + agp->ag_sdom_start, sz);
217 idna_utf8[sz] = '\0';
219 /* GNU Libidn settles on top of iconv(3) without any fallback, so let's just
220 * let it perform the charset conversion, if any should be necessary */
221 if (!(options & OPT_UNICODE)) {
222 char const *tcs = charset_get_lc();
223 idna_ascii = idna_utf8;
224 idna_utf8 = stringprep_convert(idna_ascii, "UTF-8", tcs);
225 i = (idna_utf8 == NULL && errno == EINVAL);
226 ac_free(idna_ascii);
228 if (idna_utf8 == NULL) {
229 if (i)
230 n_err(_("Cannot convert from %s to %s\n"), tcs, "UTF-8");
231 agp->ag_n_flags ^= NAME_ADDRSPEC_ERR_IDNA | NAME_ADDRSPEC_ERR_CHAR;
232 goto jleave;
236 if (idna_to_ascii_8z(idna_utf8, &idna_ascii, 0) != IDNA_SUCCESS) {
237 agp->ag_n_flags ^= NAME_ADDRSPEC_ERR_IDNA | NAME_ADDRSPEC_ERR_CHAR;
238 goto jleave1;
241 /* Replace the domain part of .ag_skinned with IDNA version */
242 sz = strlen(idna_ascii);
243 i = agp->ag_sdom_start;
244 cs = salloc(agp->ag_slen - i + sz +1);
245 memcpy(cs, agp->ag_skinned, i);
246 memcpy(cs + i, idna_ascii, sz);
247 i += sz;
248 cs[i] = '\0';
250 agp->ag_skinned = cs;
251 agp->ag_slen = i;
252 NAME_ADDRSPEC_ERR_SET(agp->ag_n_flags,
253 NAME_NAME_SALLOC | NAME_SKINNED | NAME_IDNA, 0);
255 idn_free(idna_ascii);
256 jleave1:
257 if (options & OPT_UNICODE)
258 ac_free(idna_utf8);
259 else
260 idn_free(idna_utf8);
261 jleave:
262 NYD_LEAVE;
263 return agp;
266 # elif HAVE_IDNA == HAVE_IDNA_IDNKIT /* IDNA==LIBIDNA */
267 static struct addrguts *
268 _idna_apply(struct addrguts *agp)
270 char *idna_in, *idna_out, *cs;
271 size_t sz, i;
272 idn_result_t r;
273 NYD_ENTER;
275 sz = agp->ag_slen - agp->ag_sdom_start;
276 assert(sz > 0);
277 idna_in = ac_alloc(sz +1);
278 memcpy(idna_in, agp->ag_skinned + agp->ag_sdom_start, sz);
279 idna_in[sz] = '\0';
281 for (idna_out = NULL, sz = HOST_NAME_MAX +1;; sz += HOST_NAME_MAX) {
282 idna_out = ac_alloc(sz);
284 r = idn_encodename(IDN_ENCODE_APP, idna_in, idna_out, sz);
285 switch (r) {
286 case idn_success:
287 case idn_buffer_overflow:
288 break;
289 case idn_invalid_encoding:
290 n_err(_("Cannot convert from %s to %s\n"), charset_get_lc(), "UTF-8");
291 /* FALLTHRU */
292 default:
293 agp->ag_n_flags ^= NAME_ADDRSPEC_ERR_IDNA | NAME_ADDRSPEC_ERR_CHAR;
294 goto jleave;
297 if (r == idn_success)
298 break;
299 ac_free(idna_out);
302 /* Replace the domain part of .ag_skinned with IDNA version */
303 sz = strlen(idna_out);
304 i = agp->ag_sdom_start;
305 cs = salloc(agp->ag_slen - i + sz +1);
306 memcpy(cs, agp->ag_skinned, i);
307 memcpy(cs + i, idna_out, sz);
308 i += sz;
309 cs[i] = '\0';
311 agp->ag_skinned = cs;
312 agp->ag_slen = i;
313 NAME_ADDRSPEC_ERR_SET(agp->ag_n_flags,
314 NAME_NAME_SALLOC | NAME_SKINNED | NAME_IDNA, 0);
316 jleave:
317 ac_free(idna_out);
318 ac_free(idna_in);
319 NYD_LEAVE;
320 return agp;
322 # endif /* IDNA==IDNKIT */
323 #endif /* HAVE_IDNA */
325 static int
326 _addrspec_check(int skinned, struct addrguts *agp)
328 char *addr, *p;
329 bool_t in_quote;
330 ui8_t in_domain, hadat;
331 union {char c; unsigned char u;} c;
332 #ifdef HAVE_IDNA
333 ui8_t use_idna;
334 #endif
335 NYD_ENTER;
337 #ifdef HAVE_IDNA
338 use_idna = ok_blook(idna_disable) ? 0 : 1;
339 #endif
340 agp->ag_n_flags |= NAME_ADDRSPEC_CHECKED;
341 addr = agp->ag_skinned;
343 if (agp->ag_iaddr_aend - agp->ag_iaddr_start == 0) {
344 NAME_ADDRSPEC_ERR_SET(agp->ag_n_flags, NAME_ADDRSPEC_ERR_EMPTY, 0);
345 goto jleave;
348 /* If the field is not a recipient, it cannot be a file or a pipe */
349 if (!skinned)
350 goto jaddr_check;
352 /* When changing any of the following adjust any RECIPIENTADDRSPEC;
353 * grep the latter for the complete picture */
354 if (*addr == '|') {
355 agp->ag_n_flags |= NAME_ADDRSPEC_ISPIPE;
356 goto jleave;
358 if (addr[0] == '/' || (addr[0] == '.' && addr[1] == '/'))
359 goto jisfile;
360 if (memchr(addr, '@', agp->ag_slen) == NULL) {
361 if (*addr == '+')
362 goto jisfile;
363 for (p = addr; (c.c = *p); ++p) {
364 if (c.c == '!' || c.c == '%')
365 break;
366 if (c.c == '/') {
367 jisfile:
368 agp->ag_n_flags |= NAME_ADDRSPEC_ISFILE;
369 goto jleave;
374 jaddr_check:
375 in_quote = FAL0;
376 in_domain = hadat = 0;
378 for (p = addr; (c.c = *p++) != '\0';) {
379 if (c.c == '"') {
380 in_quote = !in_quote;
381 } else if (c.u < 040 || c.u >= 0177) { /* TODO no magics: !bodychar()? */
382 #ifdef HAVE_IDNA
383 if (in_domain && use_idna > 0) {
384 if (use_idna == 1)
385 NAME_ADDRSPEC_ERR_SET(agp->ag_n_flags, NAME_ADDRSPEC_ERR_IDNA,
386 c.u);
387 use_idna = 2;
388 } else
389 #endif
390 break;
391 } else if (in_domain == 2) {
392 if ((c.c == ']' && *p != '\0') || c.c == '\\' || whitechar(c.c))
393 break;
394 } else if (in_quote && in_domain == 0) {
395 /*EMPTY*/;
396 } else if (c.c == '\\' && *p != '\0') {
397 ++p;
398 } else if (c.c == '@') {
399 if (hadat++ > 0) {
400 NAME_ADDRSPEC_ERR_SET(agp->ag_n_flags, NAME_ADDRSPEC_ERR_ATSEQ,
401 c.u);
402 goto jleave;
404 agp->ag_sdom_start = PTR2SIZE(p - addr);
405 agp->ag_n_flags |= NAME_ADDRSPEC_ISADDR; /* TODO .. really? */
406 in_domain = (*p == '[') ? 2 : 1;
407 continue;
408 } else if (c.c == '(' || c.c == ')' || c.c == '<' || c.c == '>' ||
409 c.c == ',' || c.c == ';' || c.c == ':' || c.c == '\\' ||
410 c.c == '[' || c.c == ']')
411 break;
412 hadat = 0;
414 if (c.c != '\0') {
415 NAME_ADDRSPEC_ERR_SET(agp->ag_n_flags, NAME_ADDRSPEC_ERR_CHAR, c.u);
416 goto jleave;
419 if (!(agp->ag_n_flags & NAME_ADDRSPEC_ISADDR))
420 agp->ag_n_flags |= NAME_ADDRSPEC_ISNAME;
422 #ifdef HAVE_IDNA
423 if (use_idna == 2)
424 agp = _idna_apply(agp);
425 #endif
426 jleave:
427 NYD_LEAVE;
428 return ((agp->ag_n_flags & NAME_ADDRSPEC_INVALID) != 0);
431 static int
432 gethfield(FILE *f, char **linebuf, size_t *linesize, int rem, char **colon)
434 char *line2 = NULL, *cp, *cp2;
435 size_t line2size = 0;
436 int c, isenc;
437 NYD2_ENTER;
439 if (*linebuf == NULL)
440 *linebuf = srealloc(*linebuf, *linesize = 1);
441 **linebuf = '\0';
442 for (;;) {
443 if (--rem < 0) {
444 rem = -1;
445 break;
447 if ((c = readline_restart(f, linebuf, linesize, 0)) <= 0) {
448 rem = -1;
449 break;
451 for (cp = *linebuf; fieldnamechar(*cp); ++cp)
453 if (cp > *linebuf)
454 while (blankchar(*cp))
455 ++cp;
456 if (*cp != ':' || cp == *linebuf)
457 continue;
459 /* I guess we got a headline. Handle wraparound */
460 *colon = cp;
461 cp = *linebuf + c;
462 for (;;) {
463 isenc = 0;
464 while (PTRCMP(--cp, >=, *linebuf) && blankchar(*cp))
466 cp++;
467 if (rem <= 0)
468 break;
469 if (PTRCMP(cp - 8, >=, *linebuf) && cp[-1] == '=' && cp[-2] == '?')
470 isenc |= 1;
471 ungetc(c = getc(f), f);
472 if (!blankchar(c))
473 break;
474 c = readline_restart(f, &line2, &line2size, 0);
475 if (c < 0)
476 break;
477 --rem;
478 for (cp2 = line2; blankchar(*cp2); ++cp2)
480 c -= (int)PTR2SIZE(cp2 - line2);
481 if (cp2[0] == '=' && cp2[1] == '?' && c > 8)
482 isenc |= 2;
483 if (PTRCMP(cp + c, >=, *linebuf + *linesize - 2)) {
484 size_t diff = PTR2SIZE(cp - *linebuf),
485 colondiff = PTR2SIZE(*colon - *linebuf);
486 *linebuf = srealloc(*linebuf, *linesize += c + 2);
487 cp = &(*linebuf)[diff];
488 *colon = &(*linebuf)[colondiff];
490 if (isenc != 3)
491 *cp++ = ' ';
492 memcpy(cp, cp2, c);
493 cp += c;
495 *cp = '\0';
497 if (line2 != NULL)
498 free(line2);
499 break;
501 NYD2_LEAVE;
502 return rem;
505 static int
506 msgidnextc(char const **cp, int *status)
508 int c;
509 NYD2_ENTER;
511 assert(cp != NULL);
512 assert(*cp != NULL);
513 assert(status != NULL);
515 for (;;) {
516 if (*status & 01) {
517 if (**cp == '"') {
518 *status &= ~01;
519 (*cp)++;
520 continue;
522 if (**cp == '\\') {
523 (*cp)++;
524 if (**cp == '\0')
525 goto jeof;
527 goto jdfl;
529 switch (**cp) {
530 case '(':
531 *cp = skip_comment(&(*cp)[1]);
532 continue;
533 case '>':
534 case '\0':
535 jeof:
536 c = '\0';
537 goto jleave;
538 case '"':
539 (*cp)++;
540 *status |= 01;
541 continue;
542 case '@':
543 *status |= 02;
544 /*FALLTHRU*/
545 default:
546 jdfl:
547 c = *(*cp)++ & 0377;
548 c = (*status & 02) ? lowerconv(c) : c;
549 goto jleave;
552 jleave:
553 NYD2_LEAVE;
554 return c;
557 static int
558 charcount(char *str, int c)
560 char *cp;
561 int i;
562 NYD2_ENTER;
564 for (i = 0, cp = str; *cp; ++cp)
565 if (*cp == c)
566 ++i;
567 NYD2_LEAVE;
568 return i;
571 static char const *
572 nexttoken(char const *cp)
574 NYD2_ENTER;
575 for (;;) {
576 if (*cp == '\0') {
577 cp = NULL;
578 break;
581 if (*cp == '(') {
582 size_t nesting = 1;
584 do switch (*++cp) {
585 case '(':
586 ++nesting;
587 break;
588 case ')':
589 --nesting;
590 break;
591 } while (nesting > 0 && *cp != '\0'); /* XXX error? */
592 } else if (blankchar(*cp) || *cp == ',')
593 ++cp;
594 else
595 break;
597 NYD2_LEAVE;
598 return cp;
601 FL char const *
602 myaddrs(struct header *hp)
604 struct name *np;
605 char const *rv, *mta;
606 NYD_ENTER;
608 if (hp != NULL && (np = hp->h_from) != NULL) {
609 if ((rv = np->n_fullname) != NULL)
610 goto jleave;
611 if ((rv = np->n_name) != NULL)
612 goto jleave;
615 if ((rv = ok_vlook(from)) != NULL)
616 goto jleave;
618 /* When invoking *sendmail* directly, it's its task to generate an otherwise
619 * undeterminable From: address. However, if the user sets *hostname*,
620 * accept his desire */
621 if (ok_vlook(hostname) != NULL)
622 goto jnodename;
623 if (ok_vlook(smtp) != NULL || /* TODO obsolete -> mta */
624 /* TODO pretty hacky for now (this entire fun), later: url_creat()! */
625 ((mta = ok_vlook(mta)) != NULL &&
626 (mta = n_servbyname(mta, NULL)) != NULL && *mta != '\0'))
627 goto jnodename;
628 jleave:
629 NYD_LEAVE;
630 return rv;
632 jnodename:{
633 char *hn, *cp;
634 size_t i;
636 hn = nodename(1);
637 i = strlen(myname) + strlen(hn) + 1 +1;
638 rv = cp = salloc(i);
639 sstpcpy(sstpcpy(sstpcpy(cp, myname), "@"), hn);
641 goto jleave;
644 FL char const *
645 myorigin(struct header *hp)
647 char const *rv = NULL, *ccp;
648 struct name *np;
649 NYD_ENTER;
651 if ((ccp = myaddrs(hp)) != NULL &&
652 (np = lextract(ccp, GEXTRA | GFULL)) != NULL)
653 rv = (np->n_flink != NULL) ? ok_vlook(sender) : ccp;
654 NYD_LEAVE;
655 return rv;
658 FL bool_t
659 is_head(char const *linebuf, size_t linelen, bool_t check_rfc4155)
661 char date[FROM_DATEBUF];
662 bool_t rv;
663 NYD2_ENTER;
665 if ((rv = (linelen >= 5 && !memcmp(linebuf, "From ", 5))) && check_rfc4155 &&
666 (extract_date_from_from_(linebuf, linelen, date) <= 0 ||
667 !_is_date(date)))
668 rv = TRUM1;
669 NYD2_LEAVE;
670 return rv;
673 FL int
674 extract_date_from_from_(char const *line, size_t linelen,
675 char datebuf[FROM_DATEBUF])
677 int rv;
678 char const *cp = line;
679 NYD_ENTER;
681 rv = 1;
683 /* "From " */
684 cp = _from__skipword(cp);
685 if (cp == NULL)
686 goto jerr;
687 /* "addr-spec " */
688 cp = _from__skipword(cp);
689 if (cp == NULL)
690 goto jerr;
691 if (cp[0] == 't' && cp[1] == 't' && cp[2] == 'y') {
692 cp = _from__skipword(cp);
693 if (cp == NULL)
694 goto jerr;
696 /* It seems there are invalid MBOX archives in the wild, compare
697 * . http://bugs.debian.org/624111
698 * . [Mutt] #3868: mutt should error if the imported mailbox is invalid
699 * What they do is that they obfuscate the address to "name at host",
700 * and even "name at host dot dom dot dom. I think we should handle that */
701 else if(cp[0] == 'a' && cp[1] == 't' && cp[2] == ' '){
702 rv = -1;
703 cp += 3;
704 jat_dot:
705 cp = _from__skipword(cp);
706 if (cp == NULL)
707 goto jerr;
708 if(cp[0] == 'd' && cp[1] == 'o' && cp[2] == 't' && cp[3] == ' '){
709 cp += 4;
710 goto jat_dot;
714 linelen -= PTR2SIZE(cp - line);
715 if (linelen < _DATE_MINLEN)
716 goto jerr;
717 if (cp[linelen - 1] == '\n') {
718 --linelen;
719 /* (Rather IMAP/POP3 only) */
720 if (cp[linelen - 1] == '\r')
721 --linelen;
722 if (linelen < _DATE_MINLEN)
723 goto jerr;
725 if (linelen >= FROM_DATEBUF)
726 goto jerr;
728 jleave:
729 memcpy(datebuf, cp, linelen);
730 datebuf[linelen] = '\0';
731 NYD_LEAVE;
732 return rv;
733 jerr:
734 cp = _("<Unknown date>");
735 linelen = strlen(cp);
736 if (linelen >= FROM_DATEBUF)
737 linelen = FROM_DATEBUF;
738 rv = 0;
739 goto jleave;
742 FL void
743 extract_header(FILE *fp, struct header *hp, si8_t *checkaddr_err)
745 /* See the prototype declaration for the hairy relationship of
746 * options&OPT_t_FLAG and/or pstate&PS_t_FLAG in here */
747 struct n_header_field **hftail;
748 struct header nh, *hq = &nh;
749 char *linebuf = NULL /* TODO line pool */, *colon;
750 size_t linesize = 0, seenfields = 0;
751 int lc, c;
752 char const *val, *cp;
753 NYD_ENTER;
755 memset(hq, 0, sizeof *hq);
756 if ((pstate & PS_t_FLAG) && (options & OPT_t_FLAG)) {
757 hq->h_to = hp->h_to;
758 hq->h_cc = hp->h_cc;
759 hq->h_bcc = hp->h_bcc;
761 hftail = &hq->h_user_headers;
763 for (lc = 0; readline_restart(fp, &linebuf, &linesize, 0) > 0; ++lc)
766 /* TODO yippieia, cat(check(lextract)) :-) */
767 rewind(fp);
768 while ((lc = gethfield(fp, &linebuf, &linesize, lc, &colon)) >= 0) {
769 struct name *np;
771 /* We explicitly allow EAF_NAME for some addressees since aliases are not
772 * yet expanded when we parse these! */
773 if ((val = thisfield(linebuf, "to")) != NULL) {
774 ++seenfields;
775 hq->h_to = cat(hq->h_to, checkaddrs(lextract(val, GTO | GFULL),
776 EACM_NORMAL | EAF_NAME, checkaddr_err));
777 } else if ((val = thisfield(linebuf, "cc")) != NULL) {
778 ++seenfields;
779 hq->h_cc = cat(hq->h_cc, checkaddrs(lextract(val, GCC | GFULL),
780 EACM_NORMAL | EAF_NAME, checkaddr_err));
781 } else if ((val = thisfield(linebuf, "bcc")) != NULL) {
782 ++seenfields;
783 hq->h_bcc = cat(hq->h_bcc, checkaddrs(lextract(val, GBCC | GFULL),
784 EACM_NORMAL | EAF_NAME, checkaddr_err));
785 } else if ((val = thisfield(linebuf, "from")) != NULL) {
786 if (!(pstate & PS_t_FLAG) || (options & OPT_t_FLAG)) {
787 ++seenfields;
788 hq->h_from = cat(hq->h_from,
789 checkaddrs(lextract(val, GEXTRA | GFULL | GFULLEXTRA),
790 EACM_STRICT, NULL));
792 } else if ((val = thisfield(linebuf, "reply-to")) != NULL) {
793 ++seenfields;
794 hq->h_replyto = cat(hq->h_replyto,
795 checkaddrs(lextract(val, GEXTRA | GFULL), EACM_STRICT, NULL));
796 } else if ((val = thisfield(linebuf, "sender")) != NULL) {
797 if (!(pstate & PS_t_FLAG) || (options & OPT_t_FLAG)) {
798 ++seenfields;
799 hq->h_sender = cat(hq->h_sender, /* TODO cat? check! */
800 checkaddrs(lextract(val, GEXTRA | GFULL | GFULLEXTRA),
801 EACM_STRICT, NULL));
802 } else
803 goto jebadhead;
804 } else if ((val = thisfield(linebuf, "subject")) != NULL ||
805 (val = thisfield(linebuf, "subj")) != NULL) {
806 ++seenfields;
807 for (cp = val; blankchar(*cp); ++cp)
809 hq->h_subject = (hq->h_subject != NULL)
810 ? save2str(hq->h_subject, cp) : savestr(cp);
812 /* The remaining are mostly hacked in and thus TODO -- at least in
813 * TODO respect to their content checking */
814 else if((val = thisfield(linebuf, "message-id")) != NULL){
815 if(pstate & PS_t_FLAG){
816 np = checkaddrs(lextract(val, GREF),
817 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG | EACM_NONAME,
818 NULL);
819 if (np == NULL || np->n_flink != NULL)
820 goto jebadhead;
821 ++seenfields;
822 hq->h_message_id = np;
823 }else
824 goto jebadhead;
825 }else if((val = thisfield(linebuf, "in-reply-to")) != NULL){
826 if(pstate & PS_t_FLAG){
827 np = checkaddrs(lextract(val, GREF),
828 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG | EACM_NONAME,
829 NULL);
830 if (np == NULL || np->n_flink != NULL)
831 goto jebadhead;
832 ++seenfields;
833 hq->h_in_reply_to = np;
834 }else
835 goto jebadhead;
836 }else if((val = thisfield(linebuf, "references")) != NULL){
837 if(pstate & PS_t_FLAG){
838 ++seenfields;
839 /* TODO Limit number of references TODO better on parser side */
840 hq->h_ref = cat(hq->h_ref, checkaddrs(extract(val, GREF),
841 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG | EACM_NONAME,
842 NULL));
843 }else
844 goto jebadhead;
846 /* and that is very hairy */
847 else if((val = thisfield(linebuf, "mail-followup-to")) != NULL){
848 if(pstate & PS_t_FLAG){
849 ++seenfields;
850 hq->h_mft = cat(hq->h_mft, checkaddrs(lextract(val, GEXTRA | GFULL),
851 /*EACM_STRICT | TODO '/' valid!! | EACM_NOLOG | */EACM_NONAME,
852 checkaddr_err));
853 }else
854 goto jebadhead;
856 /* A free-form user header; gethfield() did some verification already.. */
857 else{
858 struct n_header_field *hfp;
859 ui32_t nl, bl;
860 char const *nstart;
862 for(nstart = cp = linebuf;; ++cp)
863 if(!fieldnamechar(*cp))
864 break;
865 nl = (ui32_t)PTR2SIZE(cp - nstart);
867 while(blankchar(*cp))
868 ++cp;
869 if(*cp++ != ':'){
870 jebadhead:
871 n_err(_("Ignoring header field: %s\n"), linebuf);
872 continue;
874 while(blankchar(*cp))
875 ++cp;
876 bl = (ui32_t)strlen(cp) +1;
878 ++seenfields;
879 *hftail = hfp = salloc(VSTRUCT_SIZEOF(struct n_header_field, hf_dat) +
880 nl +1 + bl);
881 hftail = &hfp->hf_next;
882 hfp->hf_next = NULL;
883 hfp->hf_nl = nl;
884 hfp->hf_bl = bl - 1;
885 memcpy(hfp->hf_dat, nstart, nl);
886 hfp->hf_dat[nl++] = '\0';
887 memcpy(hfp->hf_dat + nl, cp, bl);
891 /* In case the blank line after the header has been edited out. Otherwise,
892 * fetch the header separator */
893 if (linebuf != NULL) {
894 if (linebuf[0] != '\0') {
895 for (cp = linebuf; *(++cp) != '\0';)
897 fseek(fp, (long)-PTR2SIZE(1 + cp - linebuf), SEEK_CUR);
898 } else {
899 if ((c = getc(fp)) != '\n' && c != EOF)
900 ungetc(c, fp);
904 if (seenfields > 0) {
905 hp->h_to = hq->h_to;
906 hp->h_cc = hq->h_cc;
907 hp->h_bcc = hq->h_bcc;
908 hp->h_from = hq->h_from;
909 hp->h_replyto = hq->h_replyto;
910 hp->h_sender = hq->h_sender;
911 if (hq->h_subject != NULL || !(pstate & PS_t_FLAG) ||
912 !(options & OPT_t_FLAG))
913 hp->h_subject = hq->h_subject;
914 hp->h_user_headers = hq->h_user_headers;
916 if (pstate & PS_t_FLAG) {
917 hp->h_ref = hq->h_ref;
918 hp->h_message_id = hq->h_message_id;
919 hp->h_in_reply_to = hq->h_in_reply_to;
920 hp->h_mft = hq->h_mft;
922 /* And perform additional validity checks so that we don't bail later
923 * on TODO this is good and the place where this should occur,
924 * TODO unfortunately a lot of other places do again and blabla */
925 if (pstate & PS_t_FLAG) {
926 if (hp->h_from == NULL)
927 hp->h_from = option_r_arg;
928 else if (hp->h_from->n_flink != NULL && hp->h_sender == NULL)
929 hp->h_sender = lextract(ok_vlook(sender),
930 GEXTRA | GFULL | GFULLEXTRA);
933 } else
934 n_err(_("Restoring deleted header lines\n"));
936 if (linebuf != NULL)
937 free(linebuf);
938 NYD_LEAVE;
941 FL char *
942 hfield_mult(char const *field, struct message *mp, int mult)
944 FILE *ibuf;
945 int lc;
946 struct str hfs;
947 size_t linesize = 0; /* TODO line pool */
948 char *linebuf = NULL, *colon;
949 char const *hfield;
950 NYD_ENTER;
952 /* There are (spam) messages which have header bytes which are many KB when
953 * joined, so resize a single heap storage until we are done if we shall
954 * collect a field that may have multiple bodies; only otherwise use the
955 * string dope directly */
956 memset(&hfs, 0, sizeof hfs);
958 if ((ibuf = setinput(&mb, mp, NEED_HEADER)) == NULL)
959 goto jleave;
960 if ((lc = mp->m_lines - 1) < 0)
961 goto jleave;
963 if ((mp->m_flag & MNOFROM) == 0 &&
964 readline_restart(ibuf, &linebuf, &linesize, 0) < 0)
965 goto jleave;
966 while (lc > 0) {
967 if ((lc = gethfield(ibuf, &linebuf, &linesize, lc, &colon)) < 0)
968 break;
969 if ((hfield = thisfield(linebuf, field)) != NULL && *hfield != '\0') {
970 if (mult)
971 n_str_add_buf(&hfs, hfield, strlen(hfield));
972 else {
973 hfs.s = savestr(hfield);
974 break;
979 jleave:
980 if (linebuf != NULL)
981 free(linebuf);
982 if (mult && hfs.s != NULL) {
983 colon = savestrbuf(hfs.s, hfs.l);
984 free(hfs.s);
985 hfs.s = colon;
987 NYD_LEAVE;
988 return hfs.s;
991 FL char const *
992 thisfield(char const *linebuf, char const *field)
994 char const *rv = NULL;
995 NYD2_ENTER;
997 while (lowerconv(*linebuf) == lowerconv(*field)) {
998 ++linebuf;
999 ++field;
1001 if (*field != '\0')
1002 goto jleave;
1004 while (blankchar(*linebuf))
1005 ++linebuf;
1006 if (*linebuf++ != ':')
1007 goto jleave;
1009 while (blankchar(*linebuf)) /* TODO header parser.. strip trailing WS?!? */
1010 ++linebuf;
1011 rv = linebuf;
1012 jleave:
1013 NYD2_LEAVE;
1014 return rv;
1017 FL char *
1018 nameof(struct message *mp, int reptype)
1020 char *cp, *cp2;
1021 NYD_ENTER;
1023 cp = skin(name1(mp, reptype));
1024 if (reptype != 0 || charcount(cp, '!') < 2)
1025 goto jleave;
1026 cp2 = strrchr(cp, '!');
1027 --cp2;
1028 while (cp2 > cp && *cp2 != '!')
1029 --cp2;
1030 if (*cp2 == '!')
1031 cp = cp2 + 1;
1032 jleave:
1033 NYD_LEAVE;
1034 return cp;
1037 FL char const *
1038 skip_comment(char const *cp)
1040 size_t nesting;
1041 NYD_ENTER;
1043 for (nesting = 1; nesting > 0 && *cp; ++cp) {
1044 switch (*cp) {
1045 case '\\':
1046 if (cp[1])
1047 ++cp;
1048 break;
1049 case '(':
1050 ++nesting;
1051 break;
1052 case ')':
1053 --nesting;
1054 break;
1057 NYD_LEAVE;
1058 return cp;
1061 FL char const *
1062 routeaddr(char const *name)
1064 char const *np, *rp = NULL;
1065 NYD_ENTER;
1067 for (np = name; *np; np++) {
1068 switch (*np) {
1069 case '(':
1070 np = skip_comment(np + 1) - 1;
1071 break;
1072 case '"':
1073 while (*np) {
1074 if (*++np == '"')
1075 break;
1076 if (*np == '\\' && np[1])
1077 np++;
1079 break;
1080 case '<':
1081 rp = np;
1082 break;
1083 case '>':
1084 goto jleave;
1087 rp = NULL;
1088 jleave:
1089 NYD_LEAVE;
1090 return rp;
1093 FL enum expand_addr_flags
1094 expandaddr_to_eaf(void)
1096 struct eafdesc {
1097 char const *eafd_name;
1098 bool_t eafd_is_target;
1099 ui8_t eafd_andoff;
1100 ui8_t eafd_or;
1101 } const eafa[] = {
1102 {"restrict", FAL0, EAF_TARGET_MASK, EAF_RESTRICT | EAF_RESTRICT_TARGETS},
1103 {"fail", FAL0, EAF_NONE, EAF_FAIL},
1104 {"all", TRU1, EAF_NONE, EAF_TARGET_MASK},
1105 {"file", TRU1, EAF_NONE, EAF_FILE},
1106 {"pipe", TRU1, EAF_NONE, EAF_PIPE},
1107 {"name", TRU1, EAF_NONE, EAF_NAME},
1108 {"addr", TRU1, EAF_NONE, EAF_ADDR}
1109 }, *eafp;
1111 char *buf;
1112 enum expand_addr_flags rv;
1113 char const *cp;
1114 NYD2_ENTER;
1116 if ((cp = ok_vlook(expandaddr)) == NULL)
1117 rv = EAF_RESTRICT_TARGETS;
1118 else if (*cp == '\0')
1119 rv = EAF_TARGET_MASK;
1120 else {
1121 rv = EAF_TARGET_MASK;
1123 for (buf = savestr(cp); (cp = n_strsep(&buf, ',', TRU1)) != NULL;) {
1124 bool_t minus;
1126 if ((minus = (*cp == '-')) || *cp == '+')
1127 ++cp;
1128 for (eafp = eafa;; ++eafp) {
1129 if (eafp == eafa + NELEM(eafa)) {
1130 if (options & OPT_D_V)
1131 n_err(_("Unknown *expandaddr* value: %s\n"), cp);
1132 break;
1133 } else if (!asccasecmp(cp, eafp->eafd_name)) {
1134 if (!minus) {
1135 rv &= ~eafp->eafd_andoff;
1136 rv |= eafp->eafd_or;
1137 } else {
1138 if (eafp->eafd_is_target)
1139 rv &= ~eafp->eafd_or;
1140 else if (options & OPT_D_V)
1141 n_err(_("minus - prefix invalid for *expandaddr* value: "
1142 "%s\n"), --cp);
1144 break;
1145 } else if (!asccasecmp(cp, "noalias")) { /* TODO v15 OBSOLETE */
1146 OBSOLETE(_("*expandaddr*: noalias is henceforth -name"));
1147 rv &= ~EAF_NAME;
1148 break;
1153 if ((rv & EAF_RESTRICT) && (options & (OPT_INTERACTIVE | OPT_TILDE_FLAG)))
1154 rv |= EAF_TARGET_MASK;
1155 else if (options & OPT_D_V) {
1156 if (!(rv & EAF_TARGET_MASK))
1157 n_err(_("*expandaddr* doesn't allow any addressees\n"));
1158 else if ((rv & EAF_FAIL) && (rv & EAF_TARGET_MASK) == EAF_TARGET_MASK)
1159 n_err(_("*expandaddr* with fail, but no restrictions to apply\n"));
1162 NYD2_LEAVE;
1163 return rv;
1166 FL si8_t
1167 is_addr_invalid(struct name *np, enum expand_addr_check_mode eacm)
1169 char cbuf[sizeof "'\\U12340'"];
1170 enum expand_addr_flags eaf;
1171 char const *cs;
1172 int f;
1173 si8_t rv;
1174 NYD_ENTER;
1176 f = np->n_flags;
1178 if ((rv = ((f & NAME_ADDRSPEC_INVALID) != 0))) {
1179 if ((eacm & EACM_NOLOG) || (f & NAME_ADDRSPEC_ERR_EMPTY)) {
1181 } else {
1182 ui32_t c;
1183 char const *fmt = "'\\x%02X'";
1184 bool_t ok8bit = TRU1;
1186 if (f & NAME_ADDRSPEC_ERR_IDNA) {
1187 cs = _("Invalid domain name: %s, character %s\n");
1188 fmt = "'\\U%04X'";
1189 ok8bit = FAL0;
1190 } else if (f & NAME_ADDRSPEC_ERR_ATSEQ)
1191 cs = _("%s contains invalid %s sequence\n");
1192 else
1193 cs = _("%s contains invalid character %s\n");
1195 c = NAME_ADDRSPEC_ERR_GETWC(f);
1196 snprintf(cbuf, sizeof cbuf,
1197 (ok8bit && c >= 040 && c <= 0177 ? "'%c'" : fmt), c);
1198 goto jprint;
1200 goto jleave;
1203 /* *expandaddr* stuff */
1204 if (!(rv = ((eacm & EACM_MODE_MASK) != EACM_NONE)))
1205 goto jleave;
1207 eaf = expandaddr_to_eaf();
1209 if ((eacm & EACM_STRICT) && (f & NAME_ADDRSPEC_ISFILEORPIPE)) {
1210 if (eaf & EAF_FAIL)
1211 rv = -rv;
1212 cs = _("%s%s: file or pipe addressees not allowed here\n");
1213 if (eacm & EACM_NOLOG)
1214 goto jleave;
1215 else
1216 goto j0print;
1219 eaf |= (eacm & EAF_TARGET_MASK);
1220 if (eacm & EACM_NONAME)
1221 eaf &= ~EAF_NAME;
1223 if (eaf == EAF_NONE) {
1224 rv = FAL0;
1225 goto jleave;
1227 if (eaf & EAF_FAIL)
1228 rv = -rv;
1230 if (!(eaf & EAF_FILE) && (f & NAME_ADDRSPEC_ISFILE)) {
1231 cs = _("%s%s: *expandaddr* doesn't allow file target\n");
1232 if (eacm & EACM_NOLOG)
1233 goto jleave;
1234 } else if (!(eaf & EAF_PIPE) && (f & NAME_ADDRSPEC_ISPIPE)) {
1235 cs = _("%s%s: *expandaddr* doesn't allow command pipe target\n");
1236 if (eacm & EACM_NOLOG)
1237 goto jleave;
1238 } else if (!(eaf & EAF_NAME) && (f & NAME_ADDRSPEC_ISNAME)) {
1239 cs = _("%s%s: *expandaddr* doesn't allow user name target\n");
1240 if (eacm & EACM_NOLOG)
1241 goto jleave;
1242 } else if (!(eaf & EAF_ADDR) && (f & NAME_ADDRSPEC_ISADDR)) {
1243 cs = _("%s%s: *expandaddr* doesn't allow mail address target\n");
1244 if (eacm & EACM_NOLOG)
1245 goto jleave;
1246 } else {
1247 rv = FAL0;
1248 goto jleave;
1251 j0print:
1252 cbuf[0] = '\0';
1253 jprint:
1254 n_err(cs, n_shexp_quote_cp(np->n_name, TRU1), cbuf);
1255 jleave:
1256 NYD_LEAVE;
1257 return rv;
1260 FL char *
1261 skin(char const *name)
1263 struct addrguts ag;
1264 char *ret = NULL;
1265 NYD_ENTER;
1267 if (name != NULL) {
1268 addrspec_with_guts(1, name, &ag);
1269 ret = ag.ag_skinned;
1270 if (!(ag.ag_n_flags & NAME_NAME_SALLOC))
1271 ret = savestrbuf(ret, ag.ag_slen);
1273 NYD_LEAVE;
1274 return ret;
1277 /* TODO addrspec_with_guts: RFC 5322
1278 * TODO addrspec_with_guts: trim whitespace ETC. ETC. ETC.!!! */
1279 FL int
1280 addrspec_with_guts(int doskin, char const *name, struct addrguts *agp)
1282 char const *cp;
1283 char *cp2, *bufend, *nbuf, c, gotlt, gotaddr, lastsp;
1284 int rv = 1;
1285 NYD_ENTER;
1287 memset(agp, 0, sizeof *agp);
1289 if ((agp->ag_input = name) == NULL || (agp->ag_ilen = strlen(name)) == 0) {
1290 agp->ag_skinned = UNCONST(""); /* ok: NAME_SALLOC is not set */
1291 agp->ag_slen = 0;
1292 agp->ag_n_flags |= NAME_ADDRSPEC_CHECKED;
1293 NAME_ADDRSPEC_ERR_SET(agp->ag_n_flags, NAME_ADDRSPEC_ERR_EMPTY, 0);
1294 goto jleave;
1297 if (!doskin || !anyof(name, "(< ")) {
1298 /*agp->ag_iaddr_start = 0;*/
1299 agp->ag_iaddr_aend = agp->ag_ilen;
1300 agp->ag_skinned = UNCONST(name); /* (NAME_SALLOC not set) */
1301 agp->ag_slen = agp->ag_ilen;
1302 agp->ag_n_flags = NAME_SKINNED;
1303 goto jcheck;
1306 /* Something makes us think we have to perform the skin operation */
1307 nbuf = ac_alloc(agp->ag_ilen + 1);
1308 /*agp->ag_iaddr_start = 0;*/
1309 cp2 = bufend = nbuf;
1310 gotlt = gotaddr = lastsp = 0;
1312 for (cp = name++; (c = *cp++) != '\0'; ) {
1313 switch (c) {
1314 case '(':
1315 cp = skip_comment(cp);
1316 lastsp = 0;
1317 break;
1318 case '"':
1319 /* Start of a "quoted-string".
1320 * Copy it in its entirety */
1321 /* XXX RFC: quotes are "semantically invisible"
1322 * XXX But it was explicitly added (Changelog.Heirloom,
1323 * XXX [9.23] released 11/15/00, "Do not remove quotes
1324 * XXX when skinning names"? No more info.. */
1325 *cp2++ = c;
1326 while ((c = *cp) != '\0') { /* TODO improve */
1327 cp++;
1328 if (c == '"') {
1329 *cp2++ = c;
1330 break;
1332 if (c != '\\')
1333 *cp2++ = c;
1334 else if ((c = *cp) != '\0') {
1335 *cp2++ = c;
1336 cp++;
1339 lastsp = 0;
1340 break;
1341 case ' ':
1342 case '\t':
1343 if (gotaddr == 1) {
1344 gotaddr = 2;
1345 agp->ag_iaddr_aend = PTR2SIZE(cp - name);
1347 if (cp[0] == 'a' && cp[1] == 't' && blankchar(cp[2]))
1348 cp += 3, *cp2++ = '@';
1349 else if (cp[0] == '@' && blankchar(cp[1]))
1350 cp += 2, *cp2++ = '@';
1351 else
1352 lastsp = 1;
1353 break;
1354 case '<':
1355 agp->ag_iaddr_start = PTR2SIZE(cp - (name - 1));
1356 cp2 = bufend;
1357 gotlt = gotaddr = 1;
1358 lastsp = 0;
1359 break;
1360 case '>':
1361 if (gotlt) {
1362 /* (_addrspec_check() verifies these later!) */
1363 agp->ag_iaddr_aend = PTR2SIZE(cp - name);
1364 gotlt = 0;
1365 while ((c = *cp) != '\0' && c != ',') {
1366 cp++;
1367 if (c == '(')
1368 cp = skip_comment(cp);
1369 else if (c == '"')
1370 while ((c = *cp) != '\0') {
1371 cp++;
1372 if (c == '"')
1373 break;
1374 if (c == '\\' && *cp != '\0')
1375 ++cp;
1378 lastsp = 0;
1379 break;
1381 /* FALLTRHOUGH */
1382 default:
1383 if (lastsp) {
1384 lastsp = 0;
1385 if (gotaddr)
1386 *cp2++ = ' ';
1388 *cp2++ = c;
1389 if (c == ',') {
1390 if (!gotlt) {
1391 *cp2++ = ' ';
1392 for (; blankchar(*cp); ++cp)
1394 lastsp = 0;
1395 bufend = cp2;
1397 } else if (!gotaddr) {
1398 gotaddr = 1;
1399 agp->ag_iaddr_start = PTR2SIZE(cp - name);
1403 agp->ag_slen = PTR2SIZE(cp2 - nbuf);
1404 if (agp->ag_iaddr_aend == 0)
1405 agp->ag_iaddr_aend = agp->ag_ilen;
1407 agp->ag_skinned = savestrbuf(nbuf, agp->ag_slen);
1408 ac_free(nbuf);
1409 agp->ag_n_flags = NAME_NAME_SALLOC | NAME_SKINNED;
1410 jcheck:
1411 rv = _addrspec_check(doskin, agp);
1412 jleave:
1413 NYD_LEAVE;
1414 return rv;
1417 FL char *
1418 realname(char const *name)
1420 char const *cp, *cq, *cstart = NULL, *cend = NULL;
1421 char *rname, *rp;
1422 struct str in, out;
1423 int quoted, good, nogood;
1424 NYD_ENTER;
1426 if ((cp = UNCONST(name)) == NULL)
1427 goto jleave;
1428 for (; *cp != '\0'; ++cp) {
1429 switch (*cp) {
1430 case '(':
1431 if (cstart != NULL) {
1432 /* More than one comment in address, doesn't make sense to display
1433 * it without context. Return the entire field */
1434 cp = mime_fromaddr(name);
1435 goto jleave;
1437 cstart = cp++;
1438 cp = skip_comment(cp);
1439 cend = cp--;
1440 if (cend <= cstart)
1441 cend = cstart = NULL;
1442 break;
1443 case '"':
1444 while (*cp) {
1445 if (*++cp == '"')
1446 break;
1447 if (*cp == '\\' && cp[1])
1448 ++cp;
1450 break;
1451 case '<':
1452 if (cp > name) {
1453 cstart = name;
1454 cend = cp;
1456 break;
1457 case ',':
1458 /* More than one address. Just use the first one */
1459 goto jbrk;
1463 jbrk:
1464 if (cstart == NULL) {
1465 if (*name == '<') {
1466 /* If name contains only a route-addr, the surrounding angle brackets
1467 * don't serve any useful purpose when displaying, so remove */
1468 cp = prstr(skin(name));
1469 } else
1470 cp = mime_fromaddr(name);
1471 goto jleave;
1474 /* Strip quotes. Note that quotes that appear within a MIME encoded word are
1475 * not stripped. The idea is to strip only syntactical relevant things (but
1476 * this is not necessarily the most sensible way in practice) */
1477 rp = rname = ac_alloc(PTR2SIZE(cend - cstart +1));
1478 quoted = 0;
1479 for (cp = cstart; cp < cend; ++cp) {
1480 if (*cp == '(' && !quoted) {
1481 cq = skip_comment(++cp);
1482 if (PTRCMP(--cq, >, cend))
1483 cq = cend;
1484 while (cp < cq) {
1485 if (*cp == '\\' && PTRCMP(cp + 1, <, cq))
1486 ++cp;
1487 *rp++ = *cp++;
1489 } else if (*cp == '\\' && PTRCMP(cp + 1, <, cend))
1490 *rp++ = *++cp;
1491 else if (*cp == '"') {
1492 quoted = !quoted;
1493 continue;
1494 } else
1495 *rp++ = *cp;
1497 *rp = '\0';
1498 in.s = rname;
1499 in.l = rp - rname;
1500 mime_fromhdr(&in, &out, TD_ISPR | TD_ICONV);
1501 ac_free(rname);
1502 rname = savestr(out.s);
1503 free(out.s);
1505 while (blankchar(*rname))
1506 ++rname;
1507 for (rp = rname; *rp != '\0'; ++rp)
1509 while (PTRCMP(--rp, >=, rname) && blankchar(*rp))
1510 *rp = '\0';
1511 if (rp == rname) {
1512 cp = mime_fromaddr(name);
1513 goto jleave;
1516 /* mime_fromhdr() has converted all nonprintable characters to question
1517 * marks now. These and blanks are considered uninteresting; if the
1518 * displayed part of the real name contains more than 25% of them, it is
1519 * probably better to display the plain email address instead */
1520 good = 0;
1521 nogood = 0;
1522 for (rp = rname; *rp != '\0' && PTRCMP(rp, <, rname + 20); ++rp)
1523 if (*rp == '?' || blankchar(*rp))
1524 ++nogood;
1525 else
1526 ++good;
1527 cp = (good * 3 < nogood) ? prstr(skin(name)) : rname;
1528 jleave:
1529 NYD_LEAVE;
1530 return UNCONST(cp);
1533 FL char *
1534 name1(struct message *mp, int reptype)
1536 char *namebuf, *cp, *cp2, *linebuf = NULL /* TODO line pool */;
1537 size_t namesize, linesize = 0;
1538 FILE *ibuf;
1539 int f1st = 1;
1540 NYD_ENTER;
1542 if ((cp = hfield1("from", mp)) != NULL && *cp != '\0')
1543 goto jleave;
1544 if (reptype == 0 && (cp = hfield1("sender", mp)) != NULL && *cp != '\0')
1545 goto jleave;
1547 namebuf = smalloc(namesize = 1);
1548 namebuf[0] = 0;
1549 if (mp->m_flag & MNOFROM)
1550 goto jout;
1551 if ((ibuf = setinput(&mb, mp, NEED_HEADER)) == NULL)
1552 goto jout;
1553 if (readline_restart(ibuf, &linebuf, &linesize, 0) < 0)
1554 goto jout;
1556 jnewname:
1557 if (namesize <= linesize)
1558 namebuf = srealloc(namebuf, namesize = linesize +1);
1559 for (cp = linebuf; *cp != '\0' && *cp != ' '; ++cp)
1561 for (; blankchar(*cp); ++cp)
1563 for (cp2 = namebuf + strlen(namebuf);
1564 *cp && !blankchar(*cp) && PTRCMP(cp2, <, namebuf + namesize -1);)
1565 *cp2++ = *cp++;
1566 *cp2 = '\0';
1568 if (readline_restart(ibuf, &linebuf, &linesize, 0) < 0)
1569 goto jout;
1570 if ((cp = strchr(linebuf, 'F')) == NULL)
1571 goto jout;
1572 if (strncmp(cp, "From", 4))
1573 goto jout;
1574 if (namesize <= linesize)
1575 namebuf = srealloc(namebuf, namesize = linesize + 1);
1577 while ((cp = strchr(cp, 'r')) != NULL) {
1578 if (!strncmp(cp, "remote", 6)) {
1579 if ((cp = strchr(cp, 'f')) == NULL)
1580 break;
1581 if (strncmp(cp, "from", 4) != 0)
1582 break;
1583 if ((cp = strchr(cp, ' ')) == NULL)
1584 break;
1585 cp++;
1586 if (f1st) {
1587 strncpy(namebuf, cp, namesize);
1588 f1st = 0;
1589 } else {
1590 cp2 = strrchr(namebuf, '!') + 1;
1591 strncpy(cp2, cp, PTR2SIZE(namebuf + namesize - cp2));
1593 namebuf[namesize - 2] = '!';
1594 namebuf[namesize - 1] = '\0';
1595 goto jnewname;
1597 cp++;
1599 jout:
1600 if (*namebuf != '\0' || ((cp = hfield1("return-path", mp))) == NULL ||
1601 *cp == '\0')
1602 cp = savestr(namebuf);
1604 if (linebuf != NULL)
1605 free(linebuf);
1606 free(namebuf);
1607 jleave:
1608 NYD_LEAVE;
1609 return cp;
1612 FL char *
1613 subject_re_trim(char *s)
1615 struct {
1616 ui8_t len;
1617 char dat[7];
1618 } const *pp, ignored[] = { /* Update *reply-strings* manual upon change! */
1619 { 3, "re:" },
1620 { 3, "aw:" }, { 5, "antw:" }, /* de */
1621 { 0, "" }
1624 bool_t any = FAL0;
1625 char *orig_s = s, *re_st = NULL, *re_st_x;
1626 size_t re_l = 0 /* pacify CC */;
1627 NYD_ENTER;
1629 if ((re_st_x = ok_vlook(reply_strings)) != NULL &&
1630 (re_l = strlen(re_st_x)) > 0) {
1631 re_st = ac_alloc(++re_l * 2);
1632 memcpy(re_st, re_st_x, re_l);
1635 jouter:
1636 while (*s != '\0') {
1637 while (spacechar(*s))
1638 ++s;
1640 for (pp = ignored; pp->len > 0; ++pp)
1641 if (is_asccaseprefix(s, pp->dat)) {
1642 s += pp->len;
1643 any = TRU1;
1644 goto jouter;
1647 if (re_st != NULL) {
1648 char *cp;
1650 memcpy(re_st_x = re_st + re_l, re_st, re_l);
1651 while ((cp = n_strsep(&re_st_x, ',', TRU1)) != NULL)
1652 if (is_asccaseprefix(s, cp)) {
1653 s += strlen(cp);
1654 any = TRU1;
1655 goto jouter;
1658 break;
1661 if (re_st != NULL)
1662 ac_free(re_st);
1663 NYD_LEAVE;
1664 return any ? s : orig_s;
1667 FL int
1668 msgidcmp(char const *s1, char const *s2)
1670 int q1 = 0, q2 = 0, c1, c2;
1671 NYD_ENTER;
1673 do {
1674 c1 = msgidnextc(&s1, &q1);
1675 c2 = msgidnextc(&s2, &q2);
1676 if (c1 != c2)
1677 break;
1678 } while (c1 && c2);
1679 NYD_LEAVE;
1680 return c1 - c2;
1683 FL char const *
1684 fakefrom(struct message *mp)
1686 char const *name;
1687 NYD_ENTER;
1689 if (((name = skin(hfield1("return-path", mp))) == NULL || *name == '\0' ) &&
1690 ((name = skin(hfield1("from", mp))) == NULL || *name == '\0'))
1691 /* XXX MAILER-DAEMON is what an old MBOX manual page says.
1692 * RFC 4155 however requires a RFC 5322 (2822) conforming
1693 * "addr-spec", but we simply can't provide that */
1694 name = "MAILER-DAEMON";
1695 NYD_LEAVE;
1696 return name;
1699 FL char const *
1700 fakedate(time_t t)
1702 char *cp, *cq;
1703 NYD_ENTER;
1705 cp = ctime(&t);
1706 for (cq = cp; *cq != '\0' && *cq != '\n'; ++cq)
1708 *cq = '\0';
1709 cp = savestr(cp);
1710 NYD_LEAVE;
1711 return cp;
1714 #ifdef HAVE_IMAP_SEARCH
1715 FL time_t
1716 unixtime(char const *fromline)
1718 char const *fp;
1719 char *xp;
1720 time_t t;
1721 int i, year, month, day, hour, minute, second, tzdiff;
1722 struct tm *tmptr;
1723 NYD2_ENTER;
1725 for (fp = fromline; *fp != '\0' && *fp != '\n'; ++fp)
1727 fp -= 24;
1728 if (PTR2SIZE(fp - fromline) < 7)
1729 goto jinvalid;
1730 if (fp[3] != ' ')
1731 goto jinvalid;
1732 for (i = 0;;) {
1733 if (!strncmp(fp + 4, month_names[i], 3))
1734 break;
1735 if (month_names[++i][0] == '\0')
1736 goto jinvalid;
1738 month = i + 1;
1739 if (fp[7] != ' ')
1740 goto jinvalid;
1741 day = strtol(fp + 8, &xp, 10);
1742 if (*xp != ' ' || xp != fp + 10)
1743 goto jinvalid;
1744 hour = strtol(fp + 11, &xp, 10);
1745 if (*xp != ':' || xp != fp + 13)
1746 goto jinvalid;
1747 minute = strtol(fp + 14, &xp, 10);
1748 if (*xp != ':' || xp != fp + 16)
1749 goto jinvalid;
1750 second = strtol(fp + 17, &xp, 10);
1751 if (*xp != ' ' || xp != fp + 19)
1752 goto jinvalid;
1753 year = strtol(fp + 20, &xp, 10);
1754 if (xp != fp + 24)
1755 goto jinvalid;
1756 if ((t = combinetime(year, month, day, hour, minute, second)) == (time_t)-1)
1757 goto jinvalid;
1758 tzdiff = t - mktime(gmtime(&t));
1759 tmptr = localtime(&t);
1760 if (tmptr->tm_isdst > 0)
1761 tzdiff += 3600;
1762 t -= tzdiff;
1763 jleave:
1764 NYD2_LEAVE;
1765 return t;
1766 jinvalid:
1767 t = n_time_epoch();
1768 goto jleave;
1770 #endif /* HAVE_IMAP_SEARCH */
1772 FL time_t
1773 rfctime(char const *date)
1775 char const *cp = date;
1776 char *x;
1777 time_t t;
1778 int i, year, month, day, hour, minute, second;
1779 NYD2_ENTER;
1781 if ((cp = nexttoken(cp)) == NULL)
1782 goto jinvalid;
1783 if (alphachar(cp[0]) && alphachar(cp[1]) && alphachar(cp[2]) &&
1784 cp[3] == ',') {
1785 if ((cp = nexttoken(&cp[4])) == NULL)
1786 goto jinvalid;
1788 day = strtol(cp, &x, 10); /* XXX strtol */
1789 if ((cp = nexttoken(x)) == NULL)
1790 goto jinvalid;
1791 for (i = 0;;) {
1792 if (!strncmp(cp, month_names[i], 3))
1793 break;
1794 if (month_names[++i][0] == '\0')
1795 goto jinvalid;
1797 month = i + 1;
1798 if ((cp = nexttoken(&cp[3])) == NULL)
1799 goto jinvalid;
1800 /* RFC 5322, 4.3:
1801 * Where a two or three digit year occurs in a date, the year is to be
1802 * interpreted as follows: If a two digit year is encountered whose
1803 * value is between 00 and 49, the year is interpreted by adding 2000,
1804 * ending up with a value between 2000 and 2049. If a two digit year
1805 * is encountered with a value between 50 and 99, or any three digit
1806 * year is encountered, the year is interpreted by adding 1900 */
1807 year = strtol(cp, &x, 10); /* XXX strtol */
1808 i = (int)PTR2SIZE(x - cp);
1809 if (i == 2 && year >= 0 && year <= 49)
1810 year += 2000;
1811 else if (i == 3 || (i == 2 && year >= 50 && year <= 99))
1812 year += 1900;
1813 if ((cp = nexttoken(x)) == NULL)
1814 goto jinvalid;
1815 hour = strtol(cp, &x, 10); /* XXX strtol */
1816 if (*x != ':')
1817 goto jinvalid;
1818 cp = &x[1];
1819 minute = strtol(cp, &x, 10);
1820 if (*x == ':') {
1821 cp = x + 1;
1822 second = strtol(cp, &x, 10);
1823 } else
1824 second = 0;
1826 if ((t = combinetime(year, month, day, hour, minute, second)) == (time_t)-1)
1827 goto jinvalid;
1828 if ((cp = nexttoken(x)) != NULL) {
1829 char buf[3];
1830 int sign = 1;
1832 switch (*cp) {
1833 case '+':
1834 sign = -1;
1835 /* FALLTHRU */
1836 case '-':
1837 ++cp;
1838 break;
1840 if (digitchar(cp[0]) && digitchar(cp[1]) && digitchar(cp[2]) &&
1841 digitchar(cp[3])) {
1842 long tadj;
1843 buf[2] = '\0';
1844 buf[0] = cp[0];
1845 buf[1] = cp[1];
1846 tadj = strtol(buf, NULL, 10) * 3600;/*XXX strtrol*/
1847 buf[0] = cp[2];
1848 buf[1] = cp[3];
1849 tadj += strtol(buf, NULL, 10) * 60; /* XXX strtol*/
1850 if (sign < 0)
1851 tadj = -tadj;
1852 t += tadj;
1854 /* TODO WE DO NOT YET PARSE (OBSOLETE) ZONE NAMES
1855 * TODO once again, Christos Zoulas and NetBSD Mail have done
1856 * TODO a really good job already, but using strptime(3), which
1857 * TODO is not portable. Nonetheless, WE must improve, not
1858 * TODO at last because we simply ignore obsolete timezones!!
1859 * TODO See RFC 5322, 4.3! */
1861 jleave:
1862 NYD2_LEAVE;
1863 return t;
1864 jinvalid:
1865 t = 0;
1866 goto jleave;
1869 #define is_leapyear(Y) ((((Y) % 100 ? (Y) : (Y) / 100) & 3) == 0)
1871 FL time_t
1872 combinetime(int year, int month, int day, int hour, int minute, int second)
1874 time_t t;
1875 NYD2_ENTER;
1877 if (second < 0 || minute < 0 || hour < 0 || day < 1) {
1878 t = (time_t)-1;
1879 goto jleave;
1882 t = second + minute * 60 + hour * 3600 + (day - 1) * 86400;
1883 if (month > 1)
1884 t += 86400 * 31;
1885 if (month > 2)
1886 t += 86400 * (is_leapyear(year) ? 29 : 28);
1887 if (month > 3)
1888 t += 86400 * 31;
1889 if (month > 4)
1890 t += 86400 * 30;
1891 if (month > 5)
1892 t += 86400 * 31;
1893 if (month > 6)
1894 t += 86400 * 30;
1895 if (month > 7)
1896 t += 86400 * 31;
1897 if (month > 8)
1898 t += 86400 * 31;
1899 if (month > 9)
1900 t += 86400 * 30;
1901 if (month > 10)
1902 t += 86400 * 31;
1903 if (month > 11)
1904 t += 86400 * 30;
1905 year -= 1900;
1906 t += (year - 70) * 31536000 + ((year - 69) / 4) * 86400 -
1907 ((year - 1) / 100) * 86400 + ((year + 299) / 400) * 86400;
1908 jleave:
1909 NYD2_LEAVE;
1910 return t;
1913 FL void
1914 substdate(struct message *m)
1916 char const *cp;
1917 NYD_ENTER;
1919 /* Determine the date to print in faked 'From ' lines. This is traditionally
1920 * the date the message was written to the mail file. Try to determine this
1921 * using RFC message header fields, or fall back to current time */
1922 if ((cp = hfield1("received", m)) != NULL) {
1923 while ((cp = nexttoken(cp)) != NULL && *cp != ';') {
1925 ++cp;
1926 while (alnumchar(*cp));
1928 if (cp && *++cp)
1929 m->m_time = rfctime(cp);
1931 if (m->m_time == 0 || m->m_time > time_current.tc_time) {
1932 if ((cp = hfield1("date", m)) != NULL)
1933 m->m_time = rfctime(cp);
1935 if (m->m_time == 0 || m->m_time > time_current.tc_time)
1936 m->m_time = time_current.tc_time;
1937 NYD_LEAVE;
1940 FL void
1941 setup_from_and_sender(struct header *hp)
1943 char const *addr;
1944 struct name *np;
1945 NYD_ENTER;
1947 /* If -t parsed or composed From: then take it. With -t we otherwise
1948 * want -r to be honoured in favour of *from* in order to have
1949 * a behaviour that is compatible with what users would expect from e.g.
1950 * postfix(1) */
1951 if ((np = hp->h_from) != NULL ||
1952 ((pstate & PS_t_FLAG) && (np = option_r_arg) != NULL)) {
1954 } else if ((addr = myaddrs(hp)) != NULL)
1955 np = lextract(addr, GEXTRA | GFULL | GFULLEXTRA);
1956 hp->h_from = np;
1958 if ((np = hp->h_sender) != NULL) {
1960 } else if ((addr = ok_vlook(sender)) != NULL)
1961 np = lextract(addr, GEXTRA | GFULL | GFULLEXTRA);
1962 hp->h_sender = np;
1964 NYD_LEAVE;
1967 FL struct name const *
1968 check_from_and_sender(struct name const *fromfield,
1969 struct name const *senderfield)
1971 struct name const *rv = NULL;
1972 NYD_ENTER;
1974 if (senderfield != NULL) {
1975 if (senderfield->n_flink != NULL) {
1976 n_err(_("The Sender: field may contain only one address\n"));
1977 goto jleave;
1979 rv = senderfield;
1982 if (fromfield != NULL) {
1983 if (fromfield->n_flink != NULL && senderfield == NULL) {
1984 n_err(_("A Sender: is required when there are multiple "
1985 "addresses in From:\n"));
1986 goto jleave;
1988 if (rv == NULL)
1989 rv = fromfield;
1992 if (rv == NULL)
1993 rv = (struct name*)0x1;
1994 jleave:
1995 NYD_LEAVE;
1996 return rv;
1999 #ifdef HAVE_OPENSSL
2000 FL char *
2001 getsender(struct message *mp)
2003 char *cp;
2004 struct name *np;
2005 NYD_ENTER;
2007 if ((cp = hfield1("from", mp)) == NULL ||
2008 (np = lextract(cp, GEXTRA | GSKIN)) == NULL)
2009 cp = NULL;
2010 else
2011 cp = (np->n_flink != NULL) ? skin(hfield1("sender", mp)) : np->n_name;
2012 NYD_LEAVE;
2013 return cp;
2015 #endif
2017 FL int
2018 grab_headers(enum n_lexinput_flags lif, struct header *hp, enum gfield gflags,
2019 int subjfirst)
2021 /* TODO grab_headers: again, check counts etc. against RFC;
2022 * TODO (now assumes check_from_and_sender() is called afterwards ++ */
2023 int errs;
2024 int volatile comma;
2025 NYD_ENTER;
2027 errs = 0;
2028 comma = (ok_blook(bsdcompat) || ok_blook(bsdmsgs)) ? 0 : GCOMMA;
2030 if (gflags & GTO)
2031 hp->h_to = grab_names(lif, "To: ", hp->h_to, comma, GTO | GFULL);
2032 if (subjfirst && (gflags & GSUBJECT))
2033 hp->h_subject = n_lex_input_cp(lif, "Subject: ", hp->h_subject);
2034 if (gflags & GCC)
2035 hp->h_cc = grab_names(lif, "Cc: ", hp->h_cc, comma, GCC | GFULL);
2036 if (gflags & GBCC)
2037 hp->h_bcc = grab_names(lif, "Bcc: ", hp->h_bcc, comma, GBCC | GFULL);
2039 if (gflags & GEXTRA) {
2040 if (hp->h_from == NULL)
2041 hp->h_from = lextract(myaddrs(hp), GEXTRA | GFULL | GFULLEXTRA);
2042 hp->h_from = grab_names(lif, "From: ", hp->h_from, comma,
2043 GEXTRA | GFULL | GFULLEXTRA);
2044 if (hp->h_replyto == NULL)
2045 hp->h_replyto = lextract(ok_vlook(replyto), GEXTRA | GFULL);
2046 hp->h_replyto = grab_names(lif, "Reply-To: ", hp->h_replyto, comma,
2047 GEXTRA | GFULL);
2048 if (hp->h_sender == NULL)
2049 hp->h_sender = extract(ok_vlook(sender), GEXTRA | GFULL);
2050 hp->h_sender = grab_names(lif, "Sender: ", hp->h_sender, comma,
2051 GEXTRA | GFULL);
2054 if (!subjfirst && (gflags & GSUBJECT))
2055 hp->h_subject = n_lex_input_cp(lif, "Subject: ", hp->h_subject);
2057 NYD_LEAVE;
2058 return errs;
2061 FL bool_t
2062 header_match(struct message *mp, struct search_expr const *sep)
2064 struct str in, out;
2065 FILE *ibuf;
2066 int lc;
2067 size_t linesize = 0; /* TODO line pool */
2068 char *linebuf = NULL, *colon;
2069 bool_t rv = FAL0;
2070 NYD_ENTER;
2072 if ((ibuf = setinput(&mb, mp, NEED_HEADER)) == NULL)
2073 goto jleave;
2074 if ((lc = mp->m_lines - 1) < 0)
2075 goto jleave;
2077 if ((mp->m_flag & MNOFROM) == 0 &&
2078 readline_restart(ibuf, &linebuf, &linesize, 0) < 0)
2079 goto jleave;
2080 while (lc > 0) {
2081 if (gethfield(ibuf, &linebuf, &linesize, lc, &colon) <= 0)
2082 break;
2083 if (blankchar(*++colon))
2084 ++colon;
2085 in.l = strlen(in.s = colon);
2086 mime_fromhdr(&in, &out, TD_ICONV);
2087 #ifdef HAVE_REGEX
2088 if (sep->ss_sexpr == NULL)
2089 rv = (regexec(&sep->ss_regex, out.s, 0,NULL, 0) != REG_NOMATCH);
2090 else
2091 #endif
2092 rv = substr(out.s, sep->ss_sexpr);
2093 free(out.s);
2094 if (rv)
2095 break;
2098 jleave:
2099 if (linebuf != NULL)
2100 free(linebuf);
2101 NYD_LEAVE;
2102 return rv;
2105 /* s-it-mode */