Fix off-by-one introduced in [57744f9] (Redefine *folder* ..)..
[s-mailx.git] / head.c
blob159d634863169f39f47d2fbcdedb00f761ec564e
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 /* JulianDayNumber converter(s) */
95 static size_t a_head_gregorian_to_jdn(ui32_t y, ui32_t m, ui32_t d);
96 #if 0
97 static void a_head_jdn_to_gregorian(size_t jdn,
98 ui32_t *yp, ui32_t *mp, ui32_t *dp);
99 #endif
101 /* Convert the domain part of a skinned address to IDNA.
102 * If an error occurs before Unicode information is available, revert the IDNA
103 * error to a normal CHAR one so that the error message doesn't talk Unicode */
104 #ifdef HAVE_IDNA
105 static struct addrguts * _idna_apply(struct addrguts *agp);
106 #endif
108 /* Classify and check a (possibly skinned) header body according to RFC
109 * *addr-spec* rules; if it (is assumed to has been) skinned it may however be
110 * also a file or a pipe command, so check that first, then.
111 * Otherwise perform content checking and isolate the domain part (for IDNA) */
112 static int _addrspec_check(int doskin, struct addrguts *agp);
114 /* Return the next header field found in the given message.
115 * Return >= 0 if something found, < 0 elsewise.
116 * "colon" is set to point to the colon in the header.
117 * Must deal with \ continuations & other such fraud */
118 static int gethfield(FILE *f, char **linebuf, size_t *linesize,
119 int rem, char **colon);
121 static int msgidnextc(char const **cp, int *status);
123 /* Count the occurances of c in str */
124 static int charcount(char *str, int c);
126 static char const * nexttoken(char const *cp);
128 static char const *
129 _from__skipword(char const *wp)
131 char c = 0;
132 NYD2_ENTER;
134 if (wp != NULL) {
135 while ((c = *wp++) != '\0' && !blankchar(c)) {
136 if (c == '"') {
137 while ((c = *wp++) != '\0' && c != '"')
139 if (c != '"')
140 --wp;
143 for (; blankchar(c); c = *wp++)
146 NYD2_LEAVE;
147 return (c == 0 ? NULL : wp - 1);
150 static int
151 _cmatch(size_t len, char const *date, char const *tp)
153 int ret = 0;
154 NYD2_ENTER;
156 while (len--) {
157 char c = date[len];
158 switch (tp[len]) {
159 case 'a':
160 if (!lowerchar(c))
161 goto jleave;
162 break;
163 case 'A':
164 if (!upperchar(c))
165 goto jleave;
166 break;
167 case ' ':
168 if (c != ' ')
169 goto jleave;
170 break;
171 case '0':
172 if (!digitchar(c))
173 goto jleave;
174 break;
175 case 'O':
176 if (c != ' ' && !digitchar(c))
177 goto jleave;
178 break;
179 case ':':
180 if (c != ':')
181 goto jleave;
182 break;
183 case '+':
184 if (c != '+' && c != '-')
185 goto jleave;
186 break;
189 ret = 1;
190 jleave:
191 NYD2_LEAVE;
192 return ret;
195 static int
196 _is_date(char const *date)
198 struct cmatch_data const *cmdp;
199 size_t dl;
200 int rv = 0;
201 NYD2_ENTER;
203 if ((dl = strlen(date)) >= _DATE_MINLEN)
204 for (cmdp = _cmatch_data; cmdp->tdata != NULL; ++cmdp)
205 if (dl == cmdp->tlen && (rv = _cmatch(dl, date, cmdp->tdata)))
206 break;
207 NYD2_LEAVE;
208 return rv;
211 static size_t
212 a_head_gregorian_to_jdn(ui32_t y, ui32_t m, ui32_t d){
213 /* Algorithm is taken from Communications of the ACM, Vol 6, No 8.
214 * (via third hand, plus adjustments).
215 * This algorithm is supposed to work for all dates in between 1582-10-15
216 * (0001-01-01 but that not Gregorian) and 65535-12-31 */
217 size_t jdn;
218 NYD2_ENTER;
220 #if 0
221 if(y == 0)
222 y = 1;
223 if(m == 0)
224 m = 1;
225 if(d == 0)
226 d = 1;
227 #endif
229 if(m > 2)
230 m -= 3;
231 else{
232 m += 9;
233 --y;
235 jdn = y;
236 jdn /= 100;
237 y -= 100 * jdn;
238 y *= 1461;
239 y >>= 2;
240 jdn *= 146097;
241 jdn >>= 2;
242 jdn += y;
243 jdn += d;
244 jdn += 1721119;
245 m *= 153;
246 m += 2;
247 m /= 5;
248 jdn += m;
249 NYD2_LEAVE;
250 return jdn;
253 #if 0
254 static void
255 a_head_jdn_to_gregorian(size_t jdn, ui32_t *yp, ui32_t *mp, ui32_t *dp){
256 /* Algorithm is taken from Communications of the ACM, Vol 6, No 8.
257 * (via third hand, plus adjustments) */
258 size_t y, x;
259 NYD2_ENTER;
261 jdn -= 1721119;
262 jdn <<= 2;
263 --jdn;
264 y = jdn / 146097;
265 jdn %= 146097;
266 jdn |= 3;
267 y *= 100;
268 y += jdn / 1461;
269 jdn %= 1461;
270 jdn += 4;
271 jdn >>= 2;
272 x = jdn;
273 jdn <<= 2;
274 jdn += x;
275 jdn -= 3;
276 x = jdn / 153; /* x -> month */
277 jdn %= 153;
278 jdn += 5;
279 jdn /= 5; /* jdn -> day */
280 if(x < 10)
281 x += 3;
282 else{
283 x -= 9;
284 ++y;
287 *yp = (ui32_t)(y & 0xFFFF);
288 *mp = (ui32_t)(x & 0xFF);
289 *dp = (ui32_t)(jdn & 0xFF);
290 NYD2_LEAVE;
292 #endif /* 0 */
294 #ifdef HAVE_IDNA
295 # if HAVE_IDNA == HAVE_IDNA_LIBIDNA
296 static struct addrguts *
297 _idna_apply(struct addrguts *agp)
299 char *idna_utf8, *idna_ascii, *cs;
300 size_t sz, i;
301 NYD_ENTER;
303 sz = agp->ag_slen - agp->ag_sdom_start;
304 assert(sz > 0);
305 idna_utf8 = ac_alloc(sz +1);
306 memcpy(idna_utf8, agp->ag_skinned + agp->ag_sdom_start, sz);
307 idna_utf8[sz] = '\0';
309 /* GNU Libidn settles on top of iconv(3) without any fallback, so let's just
310 * let it perform the charset conversion, if any should be necessary */
311 if (!(options & OPT_UNICODE)) {
312 char const *tcs = charset_get_lc();
313 idna_ascii = idna_utf8;
314 idna_utf8 = stringprep_convert(idna_ascii, "UTF-8", tcs);
315 i = (idna_utf8 == NULL && errno == EINVAL);
316 ac_free(idna_ascii);
318 if (idna_utf8 == NULL) {
319 if (i)
320 n_err(_("Cannot convert from %s to %s\n"), tcs, "UTF-8");
321 agp->ag_n_flags ^= NAME_ADDRSPEC_ERR_IDNA | NAME_ADDRSPEC_ERR_CHAR;
322 goto jleave;
326 if (idna_to_ascii_8z(idna_utf8, &idna_ascii, 0) != IDNA_SUCCESS) {
327 agp->ag_n_flags ^= NAME_ADDRSPEC_ERR_IDNA | NAME_ADDRSPEC_ERR_CHAR;
328 goto jleave1;
331 /* Replace the domain part of .ag_skinned with IDNA version */
332 sz = strlen(idna_ascii);
333 i = agp->ag_sdom_start;
334 cs = salloc(agp->ag_slen - i + sz +1);
335 memcpy(cs, agp->ag_skinned, i);
336 memcpy(cs + i, idna_ascii, sz);
337 i += sz;
338 cs[i] = '\0';
340 agp->ag_skinned = cs;
341 agp->ag_slen = i;
342 NAME_ADDRSPEC_ERR_SET(agp->ag_n_flags,
343 NAME_NAME_SALLOC | NAME_SKINNED | NAME_IDNA, 0);
345 idn_free(idna_ascii);
346 jleave1:
347 if (options & OPT_UNICODE)
348 ac_free(idna_utf8);
349 else
350 idn_free(idna_utf8);
351 jleave:
352 NYD_LEAVE;
353 return agp;
356 # elif HAVE_IDNA == HAVE_IDNA_IDNKIT /* IDNA==LIBIDNA */
357 static struct addrguts *
358 _idna_apply(struct addrguts *agp)
360 char *idna_in, *idna_out, *cs;
361 size_t sz, i;
362 idn_result_t r;
363 NYD_ENTER;
365 sz = agp->ag_slen - agp->ag_sdom_start;
366 assert(sz > 0);
367 idna_in = ac_alloc(sz +1);
368 memcpy(idna_in, agp->ag_skinned + agp->ag_sdom_start, sz);
369 idna_in[sz] = '\0';
371 for (idna_out = NULL, sz = HOST_NAME_MAX +1;; sz += HOST_NAME_MAX) {
372 idna_out = ac_alloc(sz);
374 r = idn_encodename(IDN_ENCODE_APP, idna_in, idna_out, sz);
375 switch (r) {
376 case idn_success:
377 case idn_buffer_overflow:
378 break;
379 case idn_invalid_encoding:
380 n_err(_("Cannot convert from %s to %s\n"), charset_get_lc(), "UTF-8");
381 /* FALLTHRU */
382 default:
383 agp->ag_n_flags ^= NAME_ADDRSPEC_ERR_IDNA | NAME_ADDRSPEC_ERR_CHAR;
384 goto jleave;
387 if (r == idn_success)
388 break;
389 ac_free(idna_out);
392 /* Replace the domain part of .ag_skinned with IDNA version */
393 sz = strlen(idna_out);
394 i = agp->ag_sdom_start;
395 cs = salloc(agp->ag_slen - i + sz +1);
396 memcpy(cs, agp->ag_skinned, i);
397 memcpy(cs + i, idna_out, sz);
398 i += sz;
399 cs[i] = '\0';
401 agp->ag_skinned = cs;
402 agp->ag_slen = i;
403 NAME_ADDRSPEC_ERR_SET(agp->ag_n_flags,
404 NAME_NAME_SALLOC | NAME_SKINNED | NAME_IDNA, 0);
406 jleave:
407 ac_free(idna_out);
408 ac_free(idna_in);
409 NYD_LEAVE;
410 return agp;
412 # endif /* IDNA==IDNKIT */
413 #endif /* HAVE_IDNA */
415 static int
416 _addrspec_check(int skinned, struct addrguts *agp)
418 char *addr, *p;
419 bool_t in_quote;
420 ui8_t in_domain, hadat;
421 union {char c; unsigned char u;} c;
422 #ifdef HAVE_IDNA
423 ui8_t use_idna;
424 #endif
425 NYD_ENTER;
427 #ifdef HAVE_IDNA
428 use_idna = ok_blook(idna_disable) ? 0 : 1;
429 #endif
430 agp->ag_n_flags |= NAME_ADDRSPEC_CHECKED;
431 addr = agp->ag_skinned;
433 if (agp->ag_iaddr_aend - agp->ag_iaddr_start == 0) {
434 NAME_ADDRSPEC_ERR_SET(agp->ag_n_flags, NAME_ADDRSPEC_ERR_EMPTY, 0);
435 goto jleave;
438 /* If the field is not a recipient, it cannot be a file or a pipe */
439 if (!skinned)
440 goto jaddr_check;
442 /* When changing any of the following adjust any RECIPIENTADDRSPEC;
443 * grep the latter for the complete picture */
444 if (*addr == '|') {
445 agp->ag_n_flags |= NAME_ADDRSPEC_ISPIPE;
446 goto jleave;
448 if (addr[0] == '/' || (addr[0] == '.' && addr[1] == '/'))
449 goto jisfile;
450 if (memchr(addr, '@', agp->ag_slen) == NULL) {
451 if (*addr == '+')
452 goto jisfile;
453 for (p = addr; (c.c = *p); ++p) {
454 if (c.c == '!' || c.c == '%')
455 break;
456 if (c.c == '/') {
457 jisfile:
458 agp->ag_n_flags |= NAME_ADDRSPEC_ISFILE;
459 goto jleave;
464 jaddr_check:
465 in_quote = FAL0;
466 in_domain = hadat = 0;
468 for (p = addr; (c.c = *p++) != '\0';) {
469 if (c.c == '"') {
470 in_quote = !in_quote;
471 } else if (c.u < 040 || c.u >= 0177) { /* TODO no magics: !bodychar()? */
472 #ifdef HAVE_IDNA
473 if (in_domain && use_idna > 0) {
474 if (use_idna == 1)
475 NAME_ADDRSPEC_ERR_SET(agp->ag_n_flags, NAME_ADDRSPEC_ERR_IDNA,
476 c.u);
477 use_idna = 2;
478 } else
479 #endif
480 break;
481 } else if (in_domain == 2) {
482 if ((c.c == ']' && *p != '\0') || c.c == '\\' || whitechar(c.c))
483 break;
484 } else if (in_quote && in_domain == 0) {
485 /*EMPTY*/;
486 } else if (c.c == '\\' && *p != '\0') {
487 ++p;
488 } else if (c.c == '@') {
489 if (hadat++ > 0) {
490 NAME_ADDRSPEC_ERR_SET(agp->ag_n_flags, NAME_ADDRSPEC_ERR_ATSEQ,
491 c.u);
492 goto jleave;
494 agp->ag_sdom_start = PTR2SIZE(p - addr);
495 agp->ag_n_flags |= NAME_ADDRSPEC_ISADDR; /* TODO .. really? */
496 in_domain = (*p == '[') ? 2 : 1;
497 continue;
498 } else if (c.c == '(' || c.c == ')' || c.c == '<' || c.c == '>' ||
499 c.c == ',' || c.c == ';' || c.c == ':' || c.c == '\\' ||
500 c.c == '[' || c.c == ']')
501 break;
502 hadat = 0;
504 if (c.c != '\0') {
505 NAME_ADDRSPEC_ERR_SET(agp->ag_n_flags, NAME_ADDRSPEC_ERR_CHAR, c.u);
506 goto jleave;
509 if (!(agp->ag_n_flags & NAME_ADDRSPEC_ISADDR))
510 agp->ag_n_flags |= NAME_ADDRSPEC_ISNAME;
512 #ifdef HAVE_IDNA
513 if (use_idna == 2)
514 agp = _idna_apply(agp);
515 #endif
516 jleave:
517 NYD_LEAVE;
518 return ((agp->ag_n_flags & NAME_ADDRSPEC_INVALID) != 0);
521 static int
522 gethfield(FILE *f, char **linebuf, size_t *linesize, int rem, char **colon)
524 char *line2 = NULL, *cp, *cp2;
525 size_t line2size = 0;
526 int c, isenc;
527 NYD2_ENTER;
529 if (*linebuf == NULL)
530 *linebuf = srealloc(*linebuf, *linesize = 1);
531 **linebuf = '\0';
532 for (;;) {
533 if (--rem < 0) {
534 rem = -1;
535 break;
537 if ((c = readline_restart(f, linebuf, linesize, 0)) <= 0) {
538 rem = -1;
539 break;
541 for (cp = *linebuf; fieldnamechar(*cp); ++cp)
543 if (cp > *linebuf)
544 while (blankchar(*cp))
545 ++cp;
546 if (*cp != ':' || cp == *linebuf)
547 continue;
549 /* I guess we got a headline. Handle wraparound */
550 *colon = cp;
551 cp = *linebuf + c;
552 for (;;) {
553 isenc = 0;
554 while (PTRCMP(--cp, >=, *linebuf) && blankchar(*cp))
556 cp++;
557 if (rem <= 0)
558 break;
559 if (PTRCMP(cp - 8, >=, *linebuf) && cp[-1] == '=' && cp[-2] == '?')
560 isenc |= 1;
561 ungetc(c = getc(f), f);
562 if (!blankchar(c))
563 break;
564 c = readline_restart(f, &line2, &line2size, 0);
565 if (c < 0)
566 break;
567 --rem;
568 for (cp2 = line2; blankchar(*cp2); ++cp2)
570 c -= (int)PTR2SIZE(cp2 - line2);
571 if (cp2[0] == '=' && cp2[1] == '?' && c > 8)
572 isenc |= 2;
573 if (PTRCMP(cp + c, >=, *linebuf + *linesize - 2)) {
574 size_t diff = PTR2SIZE(cp - *linebuf),
575 colondiff = PTR2SIZE(*colon - *linebuf);
576 *linebuf = srealloc(*linebuf, *linesize += c + 2);
577 cp = &(*linebuf)[diff];
578 *colon = &(*linebuf)[colondiff];
580 if (isenc != 3)
581 *cp++ = ' ';
582 memcpy(cp, cp2, c);
583 cp += c;
585 *cp = '\0';
587 if (line2 != NULL)
588 free(line2);
589 break;
591 NYD2_LEAVE;
592 return rem;
595 static int
596 msgidnextc(char const **cp, int *status)
598 int c;
599 NYD2_ENTER;
601 assert(cp != NULL);
602 assert(*cp != NULL);
603 assert(status != NULL);
605 for (;;) {
606 if (*status & 01) {
607 if (**cp == '"') {
608 *status &= ~01;
609 (*cp)++;
610 continue;
612 if (**cp == '\\') {
613 (*cp)++;
614 if (**cp == '\0')
615 goto jeof;
617 goto jdfl;
619 switch (**cp) {
620 case '(':
621 *cp = skip_comment(&(*cp)[1]);
622 continue;
623 case '>':
624 case '\0':
625 jeof:
626 c = '\0';
627 goto jleave;
628 case '"':
629 (*cp)++;
630 *status |= 01;
631 continue;
632 case '@':
633 *status |= 02;
634 /*FALLTHRU*/
635 default:
636 jdfl:
637 c = *(*cp)++ & 0377;
638 c = (*status & 02) ? lowerconv(c) : c;
639 goto jleave;
642 jleave:
643 NYD2_LEAVE;
644 return c;
647 static int
648 charcount(char *str, int c)
650 char *cp;
651 int i;
652 NYD2_ENTER;
654 for (i = 0, cp = str; *cp; ++cp)
655 if (*cp == c)
656 ++i;
657 NYD2_LEAVE;
658 return i;
661 static char const *
662 nexttoken(char const *cp)
664 NYD2_ENTER;
665 for (;;) {
666 if (*cp == '\0') {
667 cp = NULL;
668 break;
671 if (*cp == '(') {
672 size_t nesting = 1;
674 do switch (*++cp) {
675 case '(':
676 ++nesting;
677 break;
678 case ')':
679 --nesting;
680 break;
681 } while (nesting > 0 && *cp != '\0'); /* XXX error? */
682 } else if (blankchar(*cp) || *cp == ',')
683 ++cp;
684 else
685 break;
687 NYD2_LEAVE;
688 return cp;
691 FL char const *
692 myaddrs(struct header *hp)
694 struct name *np;
695 char const *rv, *mta;
696 NYD_ENTER;
698 if (hp != NULL && (np = hp->h_from) != NULL) {
699 if ((rv = np->n_fullname) != NULL)
700 goto jleave;
701 if ((rv = np->n_name) != NULL)
702 goto jleave;
705 if ((rv = ok_vlook(from)) != NULL)
706 goto jleave;
708 /* When invoking *sendmail* directly, it's its task to generate an otherwise
709 * undeterminable From: address. However, if the user sets *hostname*,
710 * accept his desire */
711 if (ok_vlook(hostname) != NULL)
712 goto jnodename;
713 if (ok_vlook(smtp) != NULL || /* TODO obsolete -> mta */
714 /* TODO pretty hacky for now (this entire fun), later: url_creat()! */
715 ((mta = ok_vlook(mta)) != NULL &&
716 (mta = n_servbyname(mta, NULL)) != NULL && *mta != '\0'))
717 goto jnodename;
718 jleave:
719 NYD_LEAVE;
720 return rv;
722 jnodename:{
723 char *hn, *cp;
724 size_t i;
726 hn = nodename(1);
727 i = strlen(myname) + strlen(hn) + 1 +1;
728 rv = cp = salloc(i);
729 sstpcpy(sstpcpy(sstpcpy(cp, myname), "@"), hn);
731 goto jleave;
734 FL char const *
735 myorigin(struct header *hp)
737 char const *rv = NULL, *ccp;
738 struct name *np;
739 NYD_ENTER;
741 if ((ccp = myaddrs(hp)) != NULL &&
742 (np = lextract(ccp, GEXTRA | GFULL)) != NULL)
743 rv = (np->n_flink != NULL) ? ok_vlook(sender) : ccp;
744 NYD_LEAVE;
745 return rv;
748 FL bool_t
749 is_head(char const *linebuf, size_t linelen, bool_t check_rfc4155)
751 char date[FROM_DATEBUF];
752 bool_t rv;
753 NYD2_ENTER;
755 if ((rv = (linelen >= 5 && !memcmp(linebuf, "From ", 5))) && check_rfc4155 &&
756 (extract_date_from_from_(linebuf, linelen, date) <= 0 ||
757 !_is_date(date)))
758 rv = TRUM1;
759 NYD2_LEAVE;
760 return rv;
763 FL int
764 extract_date_from_from_(char const *line, size_t linelen,
765 char datebuf[FROM_DATEBUF])
767 int rv;
768 char const *cp = line;
769 NYD_ENTER;
771 rv = 1;
773 /* "From " */
774 cp = _from__skipword(cp);
775 if (cp == NULL)
776 goto jerr;
777 /* "addr-spec " */
778 cp = _from__skipword(cp);
779 if (cp == NULL)
780 goto jerr;
781 if (cp[0] == 't' && cp[1] == 't' && cp[2] == 'y') {
782 cp = _from__skipword(cp);
783 if (cp == NULL)
784 goto jerr;
786 /* It seems there are invalid MBOX archives in the wild, compare
787 * . http://bugs.debian.org/624111
788 * . [Mutt] #3868: mutt should error if the imported mailbox is invalid
789 * What they do is that they obfuscate the address to "name at host",
790 * and even "name at host dot dom dot dom. I think we should handle that */
791 else if(cp[0] == 'a' && cp[1] == 't' && cp[2] == ' '){
792 rv = -1;
793 cp += 3;
794 jat_dot:
795 cp = _from__skipword(cp);
796 if (cp == NULL)
797 goto jerr;
798 if(cp[0] == 'd' && cp[1] == 'o' && cp[2] == 't' && cp[3] == ' '){
799 cp += 4;
800 goto jat_dot;
804 linelen -= PTR2SIZE(cp - line);
805 if (linelen < _DATE_MINLEN)
806 goto jerr;
807 if (cp[linelen - 1] == '\n') {
808 --linelen;
809 /* (Rather IMAP/POP3 only) */
810 if (cp[linelen - 1] == '\r')
811 --linelen;
812 if (linelen < _DATE_MINLEN)
813 goto jerr;
815 if (linelen >= FROM_DATEBUF)
816 goto jerr;
818 jleave:
819 memcpy(datebuf, cp, linelen);
820 datebuf[linelen] = '\0';
821 NYD_LEAVE;
822 return rv;
823 jerr:
824 cp = _("<Unknown date>");
825 linelen = strlen(cp);
826 if (linelen >= FROM_DATEBUF)
827 linelen = FROM_DATEBUF;
828 rv = 0;
829 goto jleave;
832 FL void
833 extract_header(FILE *fp, struct header *hp, si8_t *checkaddr_err)
835 /* See the prototype declaration for the hairy relationship of
836 * options&OPT_t_FLAG and/or pstate&PS_t_FLAG in here */
837 struct n_header_field **hftail;
838 struct header nh, *hq = &nh;
839 char *linebuf = NULL /* TODO line pool */, *colon;
840 size_t linesize = 0, seenfields = 0;
841 int lc, c;
842 char const *val, *cp;
843 NYD_ENTER;
845 memset(hq, 0, sizeof *hq);
846 if ((pstate & PS_t_FLAG) && (options & OPT_t_FLAG)) {
847 hq->h_to = hp->h_to;
848 hq->h_cc = hp->h_cc;
849 hq->h_bcc = hp->h_bcc;
851 hftail = &hq->h_user_headers;
853 for (lc = 0; readline_restart(fp, &linebuf, &linesize, 0) > 0; ++lc)
856 /* TODO yippieia, cat(check(lextract)) :-) */
857 rewind(fp);
858 while ((lc = gethfield(fp, &linebuf, &linesize, lc, &colon)) >= 0) {
859 struct name *np;
861 /* We explicitly allow EAF_NAME for some addressees since aliases are not
862 * yet expanded when we parse these! */
863 if ((val = thisfield(linebuf, "to")) != NULL) {
864 ++seenfields;
865 hq->h_to = cat(hq->h_to, checkaddrs(lextract(val, GTO | GFULL),
866 EACM_NORMAL | EAF_NAME, checkaddr_err));
867 } else if ((val = thisfield(linebuf, "cc")) != NULL) {
868 ++seenfields;
869 hq->h_cc = cat(hq->h_cc, checkaddrs(lextract(val, GCC | GFULL),
870 EACM_NORMAL | EAF_NAME, checkaddr_err));
871 } else if ((val = thisfield(linebuf, "bcc")) != NULL) {
872 ++seenfields;
873 hq->h_bcc = cat(hq->h_bcc, checkaddrs(lextract(val, GBCC | GFULL),
874 EACM_NORMAL | EAF_NAME, checkaddr_err));
875 } else if ((val = thisfield(linebuf, "from")) != NULL) {
876 if (!(pstate & PS_t_FLAG) || (options & OPT_t_FLAG)) {
877 ++seenfields;
878 hq->h_from = cat(hq->h_from,
879 checkaddrs(lextract(val, GEXTRA | GFULL | GFULLEXTRA),
880 EACM_STRICT, NULL));
882 } else if ((val = thisfield(linebuf, "reply-to")) != NULL) {
883 ++seenfields;
884 hq->h_replyto = cat(hq->h_replyto,
885 checkaddrs(lextract(val, GEXTRA | GFULL), EACM_STRICT, NULL));
886 } else if ((val = thisfield(linebuf, "sender")) != NULL) {
887 if (!(pstate & PS_t_FLAG) || (options & OPT_t_FLAG)) {
888 ++seenfields;
889 hq->h_sender = cat(hq->h_sender, /* TODO cat? check! */
890 checkaddrs(lextract(val, GEXTRA | GFULL | GFULLEXTRA),
891 EACM_STRICT, NULL));
892 } else
893 goto jebadhead;
894 } else if ((val = thisfield(linebuf, "subject")) != NULL ||
895 (val = thisfield(linebuf, "subj")) != NULL) {
896 ++seenfields;
897 for (cp = val; blankchar(*cp); ++cp)
899 hq->h_subject = (hq->h_subject != NULL)
900 ? save2str(hq->h_subject, cp) : savestr(cp);
902 /* The remaining are mostly hacked in and thus TODO -- at least in
903 * TODO respect to their content checking */
904 else if((val = thisfield(linebuf, "message-id")) != NULL){
905 if(pstate & PS_t_FLAG){
906 np = checkaddrs(lextract(val, GREF),
907 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG | EACM_NONAME,
908 NULL);
909 if (np == NULL || np->n_flink != NULL)
910 goto jebadhead;
911 ++seenfields;
912 hq->h_message_id = np;
913 }else
914 goto jebadhead;
915 }else if((val = thisfield(linebuf, "in-reply-to")) != NULL){
916 if(pstate & PS_t_FLAG){
917 np = checkaddrs(lextract(val, GREF),
918 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG | EACM_NONAME,
919 NULL);
920 if (np == NULL || np->n_flink != NULL)
921 goto jebadhead;
922 ++seenfields;
923 hq->h_in_reply_to = np;
924 }else
925 goto jebadhead;
926 }else if((val = thisfield(linebuf, "references")) != NULL){
927 if(pstate & PS_t_FLAG){
928 ++seenfields;
929 /* TODO Limit number of references TODO better on parser side */
930 hq->h_ref = cat(hq->h_ref, checkaddrs(extract(val, GREF),
931 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG | EACM_NONAME,
932 NULL));
933 }else
934 goto jebadhead;
936 /* and that is very hairy */
937 else if((val = thisfield(linebuf, "mail-followup-to")) != NULL){
938 if(pstate & PS_t_FLAG){
939 ++seenfields;
940 hq->h_mft = cat(hq->h_mft, checkaddrs(lextract(val, GEXTRA | GFULL),
941 /*EACM_STRICT | TODO '/' valid!! | EACM_NOLOG | */EACM_NONAME,
942 checkaddr_err));
943 }else
944 goto jebadhead;
946 /* A free-form user header; gethfield() did some verification already.. */
947 else{
948 struct n_header_field *hfp;
949 ui32_t nl, bl;
950 char const *nstart;
952 for(nstart = cp = linebuf;; ++cp)
953 if(!fieldnamechar(*cp))
954 break;
955 nl = (ui32_t)PTR2SIZE(cp - nstart);
957 while(blankchar(*cp))
958 ++cp;
959 if(*cp++ != ':'){
960 jebadhead:
961 n_err(_("Ignoring header field: %s\n"), linebuf);
962 continue;
964 while(blankchar(*cp))
965 ++cp;
966 bl = (ui32_t)strlen(cp) +1;
968 ++seenfields;
969 *hftail = hfp = salloc(VSTRUCT_SIZEOF(struct n_header_field, hf_dat) +
970 nl +1 + bl);
971 hftail = &hfp->hf_next;
972 hfp->hf_next = NULL;
973 hfp->hf_nl = nl;
974 hfp->hf_bl = bl - 1;
975 memcpy(hfp->hf_dat, nstart, nl);
976 hfp->hf_dat[nl++] = '\0';
977 memcpy(hfp->hf_dat + nl, cp, bl);
981 /* In case the blank line after the header has been edited out. Otherwise,
982 * fetch the header separator */
983 if (linebuf != NULL) {
984 if (linebuf[0] != '\0') {
985 for (cp = linebuf; *(++cp) != '\0';)
987 fseek(fp, (long)-PTR2SIZE(1 + cp - linebuf), SEEK_CUR);
988 } else {
989 if ((c = getc(fp)) != '\n' && c != EOF)
990 ungetc(c, fp);
994 if (seenfields > 0) {
995 hp->h_to = hq->h_to;
996 hp->h_cc = hq->h_cc;
997 hp->h_bcc = hq->h_bcc;
998 hp->h_from = hq->h_from;
999 hp->h_replyto = hq->h_replyto;
1000 hp->h_sender = hq->h_sender;
1001 if (hq->h_subject != NULL || !(pstate & PS_t_FLAG) ||
1002 !(options & OPT_t_FLAG))
1003 hp->h_subject = hq->h_subject;
1004 hp->h_user_headers = hq->h_user_headers;
1006 if (pstate & PS_t_FLAG) {
1007 hp->h_ref = hq->h_ref;
1008 hp->h_message_id = hq->h_message_id;
1009 hp->h_in_reply_to = hq->h_in_reply_to;
1010 hp->h_mft = hq->h_mft;
1012 /* And perform additional validity checks so that we don't bail later
1013 * on TODO this is good and the place where this should occur,
1014 * TODO unfortunately a lot of other places do again and blabla */
1015 if (pstate & PS_t_FLAG) {
1016 if (hp->h_from == NULL)
1017 hp->h_from = option_r_arg;
1018 else if (hp->h_from->n_flink != NULL && hp->h_sender == NULL)
1019 hp->h_sender = lextract(ok_vlook(sender),
1020 GEXTRA | GFULL | GFULLEXTRA);
1023 } else
1024 n_err(_("Restoring deleted header lines\n"));
1026 if (linebuf != NULL)
1027 free(linebuf);
1028 NYD_LEAVE;
1031 FL char *
1032 hfield_mult(char const *field, struct message *mp, int mult)
1034 FILE *ibuf;
1035 int lc;
1036 struct str hfs;
1037 size_t linesize = 0; /* TODO line pool */
1038 char *linebuf = NULL, *colon;
1039 char const *hfield;
1040 NYD_ENTER;
1042 /* There are (spam) messages which have header bytes which are many KB when
1043 * joined, so resize a single heap storage until we are done if we shall
1044 * collect a field that may have multiple bodies; only otherwise use the
1045 * string dope directly */
1046 memset(&hfs, 0, sizeof hfs);
1048 if ((ibuf = setinput(&mb, mp, NEED_HEADER)) == NULL)
1049 goto jleave;
1050 if ((lc = mp->m_lines - 1) < 0)
1051 goto jleave;
1053 if ((mp->m_flag & MNOFROM) == 0 &&
1054 readline_restart(ibuf, &linebuf, &linesize, 0) < 0)
1055 goto jleave;
1056 while (lc > 0) {
1057 if ((lc = gethfield(ibuf, &linebuf, &linesize, lc, &colon)) < 0)
1058 break;
1059 if ((hfield = thisfield(linebuf, field)) != NULL && *hfield != '\0') {
1060 if (mult)
1061 n_str_add_buf(&hfs, hfield, strlen(hfield));
1062 else {
1063 hfs.s = savestr(hfield);
1064 break;
1069 jleave:
1070 if (linebuf != NULL)
1071 free(linebuf);
1072 if (mult && hfs.s != NULL) {
1073 colon = savestrbuf(hfs.s, hfs.l);
1074 free(hfs.s);
1075 hfs.s = colon;
1077 NYD_LEAVE;
1078 return hfs.s;
1081 FL char const *
1082 thisfield(char const *linebuf, char const *field)
1084 char const *rv = NULL;
1085 NYD2_ENTER;
1087 while (lowerconv(*linebuf) == lowerconv(*field)) {
1088 ++linebuf;
1089 ++field;
1091 if (*field != '\0')
1092 goto jleave;
1094 while (blankchar(*linebuf))
1095 ++linebuf;
1096 if (*linebuf++ != ':')
1097 goto jleave;
1099 while (blankchar(*linebuf)) /* TODO header parser.. strip trailing WS?!? */
1100 ++linebuf;
1101 rv = linebuf;
1102 jleave:
1103 NYD2_LEAVE;
1104 return rv;
1107 FL char *
1108 nameof(struct message *mp, int reptype)
1110 char *cp, *cp2;
1111 NYD_ENTER;
1113 cp = skin(name1(mp, reptype));
1114 if (reptype != 0 || charcount(cp, '!') < 2)
1115 goto jleave;
1116 cp2 = strrchr(cp, '!');
1117 --cp2;
1118 while (cp2 > cp && *cp2 != '!')
1119 --cp2;
1120 if (*cp2 == '!')
1121 cp = cp2 + 1;
1122 jleave:
1123 NYD_LEAVE;
1124 return cp;
1127 FL char const *
1128 skip_comment(char const *cp)
1130 size_t nesting;
1131 NYD_ENTER;
1133 for (nesting = 1; nesting > 0 && *cp; ++cp) {
1134 switch (*cp) {
1135 case '\\':
1136 if (cp[1])
1137 ++cp;
1138 break;
1139 case '(':
1140 ++nesting;
1141 break;
1142 case ')':
1143 --nesting;
1144 break;
1147 NYD_LEAVE;
1148 return cp;
1151 FL char const *
1152 routeaddr(char const *name)
1154 char const *np, *rp = NULL;
1155 NYD_ENTER;
1157 for (np = name; *np; np++) {
1158 switch (*np) {
1159 case '(':
1160 np = skip_comment(np + 1) - 1;
1161 break;
1162 case '"':
1163 while (*np) {
1164 if (*++np == '"')
1165 break;
1166 if (*np == '\\' && np[1])
1167 np++;
1169 break;
1170 case '<':
1171 rp = np;
1172 break;
1173 case '>':
1174 goto jleave;
1177 rp = NULL;
1178 jleave:
1179 NYD_LEAVE;
1180 return rp;
1183 FL enum expand_addr_flags
1184 expandaddr_to_eaf(void)
1186 struct eafdesc {
1187 char const *eafd_name;
1188 bool_t eafd_is_target;
1189 ui8_t eafd_andoff;
1190 ui8_t eafd_or;
1191 } const eafa[] = {
1192 {"restrict", FAL0, EAF_TARGET_MASK, EAF_RESTRICT | EAF_RESTRICT_TARGETS},
1193 {"fail", FAL0, EAF_NONE, EAF_FAIL},
1194 {"all", TRU1, EAF_NONE, EAF_TARGET_MASK},
1195 {"file", TRU1, EAF_NONE, EAF_FILE},
1196 {"pipe", TRU1, EAF_NONE, EAF_PIPE},
1197 {"name", TRU1, EAF_NONE, EAF_NAME},
1198 {"addr", TRU1, EAF_NONE, EAF_ADDR}
1199 }, *eafp;
1201 char *buf;
1202 enum expand_addr_flags rv;
1203 char const *cp;
1204 NYD2_ENTER;
1206 if ((cp = ok_vlook(expandaddr)) == NULL)
1207 rv = EAF_RESTRICT_TARGETS;
1208 else if (*cp == '\0')
1209 rv = EAF_TARGET_MASK;
1210 else {
1211 rv = EAF_TARGET_MASK;
1213 for (buf = savestr(cp); (cp = n_strsep(&buf, ',', TRU1)) != NULL;) {
1214 bool_t minus;
1216 if ((minus = (*cp == '-')) || *cp == '+')
1217 ++cp;
1218 for (eafp = eafa;; ++eafp) {
1219 if (eafp == eafa + NELEM(eafa)) {
1220 if (options & OPT_D_V)
1221 n_err(_("Unknown *expandaddr* value: %s\n"), cp);
1222 break;
1223 } else if (!asccasecmp(cp, eafp->eafd_name)) {
1224 if (!minus) {
1225 rv &= ~eafp->eafd_andoff;
1226 rv |= eafp->eafd_or;
1227 } else {
1228 if (eafp->eafd_is_target)
1229 rv &= ~eafp->eafd_or;
1230 else if (options & OPT_D_V)
1231 n_err(_("minus - prefix invalid for *expandaddr* value: "
1232 "%s\n"), --cp);
1234 break;
1235 } else if (!asccasecmp(cp, "noalias")) { /* TODO v15 OBSOLETE */
1236 OBSOLETE(_("*expandaddr*: noalias is henceforth -name"));
1237 rv &= ~EAF_NAME;
1238 break;
1243 if ((rv & EAF_RESTRICT) && (options & (OPT_INTERACTIVE | OPT_TILDE_FLAG)))
1244 rv |= EAF_TARGET_MASK;
1245 else if (options & OPT_D_V) {
1246 if (!(rv & EAF_TARGET_MASK))
1247 n_err(_("*expandaddr* doesn't allow any addressees\n"));
1248 else if ((rv & EAF_FAIL) && (rv & EAF_TARGET_MASK) == EAF_TARGET_MASK)
1249 n_err(_("*expandaddr* with fail, but no restrictions to apply\n"));
1252 NYD2_LEAVE;
1253 return rv;
1256 FL si8_t
1257 is_addr_invalid(struct name *np, enum expand_addr_check_mode eacm)
1259 char cbuf[sizeof "'\\U12340'"];
1260 enum expand_addr_flags eaf;
1261 char const *cs;
1262 int f;
1263 si8_t rv;
1264 NYD_ENTER;
1266 f = np->n_flags;
1268 if ((rv = ((f & NAME_ADDRSPEC_INVALID) != 0))) {
1269 if ((eacm & EACM_NOLOG) || (f & NAME_ADDRSPEC_ERR_EMPTY)) {
1271 } else {
1272 ui32_t c;
1273 char const *fmt = "'\\x%02X'";
1274 bool_t ok8bit = TRU1;
1276 if (f & NAME_ADDRSPEC_ERR_IDNA) {
1277 cs = _("Invalid domain name: %s, character %s\n");
1278 fmt = "'\\U%04X'";
1279 ok8bit = FAL0;
1280 } else if (f & NAME_ADDRSPEC_ERR_ATSEQ)
1281 cs = _("%s contains invalid %s sequence\n");
1282 else
1283 cs = _("%s contains invalid character %s\n");
1285 c = NAME_ADDRSPEC_ERR_GETWC(f);
1286 snprintf(cbuf, sizeof cbuf,
1287 (ok8bit && c >= 040 && c <= 0177 ? "'%c'" : fmt), c);
1288 goto jprint;
1290 goto jleave;
1293 /* *expandaddr* stuff */
1294 if (!(rv = ((eacm & EACM_MODE_MASK) != EACM_NONE)))
1295 goto jleave;
1297 eaf = expandaddr_to_eaf();
1299 if ((eacm & EACM_STRICT) && (f & NAME_ADDRSPEC_ISFILEORPIPE)) {
1300 if (eaf & EAF_FAIL)
1301 rv = -rv;
1302 cs = _("%s%s: file or pipe addressees not allowed here\n");
1303 if (eacm & EACM_NOLOG)
1304 goto jleave;
1305 else
1306 goto j0print;
1309 eaf |= (eacm & EAF_TARGET_MASK);
1310 if (eacm & EACM_NONAME)
1311 eaf &= ~EAF_NAME;
1313 if (eaf == EAF_NONE) {
1314 rv = FAL0;
1315 goto jleave;
1317 if (eaf & EAF_FAIL)
1318 rv = -rv;
1320 if (!(eaf & EAF_FILE) && (f & NAME_ADDRSPEC_ISFILE)) {
1321 cs = _("%s%s: *expandaddr* doesn't allow file target\n");
1322 if (eacm & EACM_NOLOG)
1323 goto jleave;
1324 } else if (!(eaf & EAF_PIPE) && (f & NAME_ADDRSPEC_ISPIPE)) {
1325 cs = _("%s%s: *expandaddr* doesn't allow command pipe target\n");
1326 if (eacm & EACM_NOLOG)
1327 goto jleave;
1328 } else if (!(eaf & EAF_NAME) && (f & NAME_ADDRSPEC_ISNAME)) {
1329 cs = _("%s%s: *expandaddr* doesn't allow user name target\n");
1330 if (eacm & EACM_NOLOG)
1331 goto jleave;
1332 } else if (!(eaf & EAF_ADDR) && (f & NAME_ADDRSPEC_ISADDR)) {
1333 cs = _("%s%s: *expandaddr* doesn't allow mail address target\n");
1334 if (eacm & EACM_NOLOG)
1335 goto jleave;
1336 } else {
1337 rv = FAL0;
1338 goto jleave;
1341 j0print:
1342 cbuf[0] = '\0';
1343 jprint:
1344 n_err(cs, n_shexp_quote_cp(np->n_name, TRU1), cbuf);
1345 jleave:
1346 NYD_LEAVE;
1347 return rv;
1350 FL char *
1351 skin(char const *name)
1353 struct addrguts ag;
1354 char *ret = NULL;
1355 NYD_ENTER;
1357 if (name != NULL) {
1358 addrspec_with_guts(1, name, &ag);
1359 ret = ag.ag_skinned;
1360 if (!(ag.ag_n_flags & NAME_NAME_SALLOC))
1361 ret = savestrbuf(ret, ag.ag_slen);
1363 NYD_LEAVE;
1364 return ret;
1367 /* TODO addrspec_with_guts: RFC 5322
1368 * TODO addrspec_with_guts: trim whitespace ETC. ETC. ETC.!!! */
1369 FL int
1370 addrspec_with_guts(int doskin, char const *name, struct addrguts *agp)
1372 char const *cp;
1373 char *cp2, *bufend, *nbuf, c, gotlt, gotaddr, lastsp;
1374 int rv = 1;
1375 NYD_ENTER;
1377 memset(agp, 0, sizeof *agp);
1379 if ((agp->ag_input = name) == NULL || (agp->ag_ilen = strlen(name)) == 0) {
1380 agp->ag_skinned = UNCONST(""); /* ok: NAME_SALLOC is not set */
1381 agp->ag_slen = 0;
1382 agp->ag_n_flags |= NAME_ADDRSPEC_CHECKED;
1383 NAME_ADDRSPEC_ERR_SET(agp->ag_n_flags, NAME_ADDRSPEC_ERR_EMPTY, 0);
1384 goto jleave;
1387 if (!doskin || !anyof(name, "(< ")) {
1388 /*agp->ag_iaddr_start = 0;*/
1389 agp->ag_iaddr_aend = agp->ag_ilen;
1390 agp->ag_skinned = UNCONST(name); /* (NAME_SALLOC not set) */
1391 agp->ag_slen = agp->ag_ilen;
1392 agp->ag_n_flags = NAME_SKINNED;
1393 goto jcheck;
1396 /* Something makes us think we have to perform the skin operation */
1397 nbuf = ac_alloc(agp->ag_ilen + 1);
1398 /*agp->ag_iaddr_start = 0;*/
1399 cp2 = bufend = nbuf;
1400 gotlt = gotaddr = lastsp = 0;
1402 for (cp = name++; (c = *cp++) != '\0'; ) {
1403 switch (c) {
1404 case '(':
1405 cp = skip_comment(cp);
1406 lastsp = 0;
1407 break;
1408 case '"':
1409 /* Start of a "quoted-string".
1410 * Copy it in its entirety */
1411 /* XXX RFC: quotes are "semantically invisible"
1412 * XXX But it was explicitly added (Changelog.Heirloom,
1413 * XXX [9.23] released 11/15/00, "Do not remove quotes
1414 * XXX when skinning names"? No more info.. */
1415 *cp2++ = c;
1416 while ((c = *cp) != '\0') { /* TODO improve */
1417 cp++;
1418 if (c == '"') {
1419 *cp2++ = c;
1420 break;
1422 if (c != '\\')
1423 *cp2++ = c;
1424 else if ((c = *cp) != '\0') {
1425 *cp2++ = c;
1426 cp++;
1429 lastsp = 0;
1430 break;
1431 case ' ':
1432 case '\t':
1433 if (gotaddr == 1) {
1434 gotaddr = 2;
1435 agp->ag_iaddr_aend = PTR2SIZE(cp - name);
1437 if (cp[0] == 'a' && cp[1] == 't' && blankchar(cp[2]))
1438 cp += 3, *cp2++ = '@';
1439 else if (cp[0] == '@' && blankchar(cp[1]))
1440 cp += 2, *cp2++ = '@';
1441 else
1442 lastsp = 1;
1443 break;
1444 case '<':
1445 agp->ag_iaddr_start = PTR2SIZE(cp - (name - 1));
1446 cp2 = bufend;
1447 gotlt = gotaddr = 1;
1448 lastsp = 0;
1449 break;
1450 case '>':
1451 if (gotlt) {
1452 /* (_addrspec_check() verifies these later!) */
1453 agp->ag_iaddr_aend = PTR2SIZE(cp - name);
1454 gotlt = 0;
1455 while ((c = *cp) != '\0' && c != ',') {
1456 cp++;
1457 if (c == '(')
1458 cp = skip_comment(cp);
1459 else if (c == '"')
1460 while ((c = *cp) != '\0') {
1461 cp++;
1462 if (c == '"')
1463 break;
1464 if (c == '\\' && *cp != '\0')
1465 ++cp;
1468 lastsp = 0;
1469 break;
1471 /* FALLTRHOUGH */
1472 default:
1473 if (lastsp) {
1474 lastsp = 0;
1475 if (gotaddr)
1476 *cp2++ = ' ';
1478 *cp2++ = c;
1479 if (c == ',') {
1480 if (!gotlt) {
1481 *cp2++ = ' ';
1482 for (; blankchar(*cp); ++cp)
1484 lastsp = 0;
1485 bufend = cp2;
1487 } else if (!gotaddr) {
1488 gotaddr = 1;
1489 agp->ag_iaddr_start = PTR2SIZE(cp - name);
1493 agp->ag_slen = PTR2SIZE(cp2 - nbuf);
1494 if (agp->ag_iaddr_aend == 0)
1495 agp->ag_iaddr_aend = agp->ag_ilen;
1497 agp->ag_skinned = savestrbuf(nbuf, agp->ag_slen);
1498 ac_free(nbuf);
1499 agp->ag_n_flags = NAME_NAME_SALLOC | NAME_SKINNED;
1500 jcheck:
1501 rv = _addrspec_check(doskin, agp);
1502 jleave:
1503 NYD_LEAVE;
1504 return rv;
1507 FL char *
1508 realname(char const *name)
1510 char const *cp, *cq, *cstart = NULL, *cend = NULL;
1511 char *rname, *rp;
1512 struct str in, out;
1513 int quoted, good, nogood;
1514 NYD_ENTER;
1516 if ((cp = UNCONST(name)) == NULL)
1517 goto jleave;
1518 for (; *cp != '\0'; ++cp) {
1519 switch (*cp) {
1520 case '(':
1521 if (cstart != NULL) {
1522 /* More than one comment in address, doesn't make sense to display
1523 * it without context. Return the entire field */
1524 cp = mime_fromaddr(name);
1525 goto jleave;
1527 cstart = cp++;
1528 cp = skip_comment(cp);
1529 cend = cp--;
1530 if (cend <= cstart)
1531 cend = cstart = NULL;
1532 break;
1533 case '"':
1534 while (*cp) {
1535 if (*++cp == '"')
1536 break;
1537 if (*cp == '\\' && cp[1])
1538 ++cp;
1540 break;
1541 case '<':
1542 if (cp > name) {
1543 cstart = name;
1544 cend = cp;
1546 break;
1547 case ',':
1548 /* More than one address. Just use the first one */
1549 goto jbrk;
1553 jbrk:
1554 if (cstart == NULL) {
1555 if (*name == '<') {
1556 /* If name contains only a route-addr, the surrounding angle brackets
1557 * don't serve any useful purpose when displaying, so remove */
1558 cp = prstr(skin(name));
1559 } else
1560 cp = mime_fromaddr(name);
1561 goto jleave;
1564 /* Strip quotes. Note that quotes that appear within a MIME encoded word are
1565 * not stripped. The idea is to strip only syntactical relevant things (but
1566 * this is not necessarily the most sensible way in practice) */
1567 rp = rname = ac_alloc(PTR2SIZE(cend - cstart +1));
1568 quoted = 0;
1569 for (cp = cstart; cp < cend; ++cp) {
1570 if (*cp == '(' && !quoted) {
1571 cq = skip_comment(++cp);
1572 if (PTRCMP(--cq, >, cend))
1573 cq = cend;
1574 while (cp < cq) {
1575 if (*cp == '\\' && PTRCMP(cp + 1, <, cq))
1576 ++cp;
1577 *rp++ = *cp++;
1579 } else if (*cp == '\\' && PTRCMP(cp + 1, <, cend))
1580 *rp++ = *++cp;
1581 else if (*cp == '"') {
1582 quoted = !quoted;
1583 continue;
1584 } else
1585 *rp++ = *cp;
1587 *rp = '\0';
1588 in.s = rname;
1589 in.l = rp - rname;
1590 mime_fromhdr(&in, &out, TD_ISPR | TD_ICONV);
1591 ac_free(rname);
1592 rname = savestr(out.s);
1593 free(out.s);
1595 while (blankchar(*rname))
1596 ++rname;
1597 for (rp = rname; *rp != '\0'; ++rp)
1599 while (PTRCMP(--rp, >=, rname) && blankchar(*rp))
1600 *rp = '\0';
1601 if (rp == rname) {
1602 cp = mime_fromaddr(name);
1603 goto jleave;
1606 /* mime_fromhdr() has converted all nonprintable characters to question
1607 * marks now. These and blanks are considered uninteresting; if the
1608 * displayed part of the real name contains more than 25% of them, it is
1609 * probably better to display the plain email address instead */
1610 good = 0;
1611 nogood = 0;
1612 for (rp = rname; *rp != '\0' && PTRCMP(rp, <, rname + 20); ++rp)
1613 if (*rp == '?' || blankchar(*rp))
1614 ++nogood;
1615 else
1616 ++good;
1617 cp = (good * 3 < nogood) ? prstr(skin(name)) : rname;
1618 jleave:
1619 NYD_LEAVE;
1620 return UNCONST(cp);
1623 FL char *
1624 name1(struct message *mp, int reptype)
1626 char *namebuf, *cp, *cp2, *linebuf = NULL /* TODO line pool */;
1627 size_t namesize, linesize = 0;
1628 FILE *ibuf;
1629 int f1st = 1;
1630 NYD_ENTER;
1632 if ((cp = hfield1("from", mp)) != NULL && *cp != '\0')
1633 goto jleave;
1634 if (reptype == 0 && (cp = hfield1("sender", mp)) != NULL && *cp != '\0')
1635 goto jleave;
1637 namebuf = smalloc(namesize = 1);
1638 namebuf[0] = 0;
1639 if (mp->m_flag & MNOFROM)
1640 goto jout;
1641 if ((ibuf = setinput(&mb, mp, NEED_HEADER)) == NULL)
1642 goto jout;
1643 if (readline_restart(ibuf, &linebuf, &linesize, 0) < 0)
1644 goto jout;
1646 jnewname:
1647 if (namesize <= linesize)
1648 namebuf = srealloc(namebuf, namesize = linesize +1);
1649 for (cp = linebuf; *cp != '\0' && *cp != ' '; ++cp)
1651 for (; blankchar(*cp); ++cp)
1653 for (cp2 = namebuf + strlen(namebuf);
1654 *cp && !blankchar(*cp) && PTRCMP(cp2, <, namebuf + namesize -1);)
1655 *cp2++ = *cp++;
1656 *cp2 = '\0';
1658 if (readline_restart(ibuf, &linebuf, &linesize, 0) < 0)
1659 goto jout;
1660 if ((cp = strchr(linebuf, 'F')) == NULL)
1661 goto jout;
1662 if (strncmp(cp, "From", 4))
1663 goto jout;
1664 if (namesize <= linesize)
1665 namebuf = srealloc(namebuf, namesize = linesize + 1);
1667 while ((cp = strchr(cp, 'r')) != NULL) {
1668 if (!strncmp(cp, "remote", 6)) {
1669 if ((cp = strchr(cp, 'f')) == NULL)
1670 break;
1671 if (strncmp(cp, "from", 4) != 0)
1672 break;
1673 if ((cp = strchr(cp, ' ')) == NULL)
1674 break;
1675 cp++;
1676 if (f1st) {
1677 strncpy(namebuf, cp, namesize);
1678 f1st = 0;
1679 } else {
1680 cp2 = strrchr(namebuf, '!') + 1;
1681 strncpy(cp2, cp, PTR2SIZE(namebuf + namesize - cp2));
1683 namebuf[namesize - 2] = '!';
1684 namebuf[namesize - 1] = '\0';
1685 goto jnewname;
1687 cp++;
1689 jout:
1690 if (*namebuf != '\0' || ((cp = hfield1("return-path", mp))) == NULL ||
1691 *cp == '\0')
1692 cp = savestr(namebuf);
1694 if (linebuf != NULL)
1695 free(linebuf);
1696 free(namebuf);
1697 jleave:
1698 NYD_LEAVE;
1699 return cp;
1702 FL char *
1703 subject_re_trim(char *s)
1705 struct {
1706 ui8_t len;
1707 char dat[7];
1708 } const *pp, ignored[] = { /* Update *reply-strings* manual upon change! */
1709 { 3, "re:" },
1710 { 3, "aw:" }, { 5, "antw:" }, /* de */
1711 { 0, "" }
1714 bool_t any = FAL0;
1715 char *orig_s = s, *re_st = NULL, *re_st_x;
1716 size_t re_l = 0 /* pacify CC */;
1717 NYD_ENTER;
1719 if ((re_st_x = ok_vlook(reply_strings)) != NULL &&
1720 (re_l = strlen(re_st_x)) > 0) {
1721 re_st = ac_alloc(++re_l * 2);
1722 memcpy(re_st, re_st_x, re_l);
1725 jouter:
1726 while (*s != '\0') {
1727 while (spacechar(*s))
1728 ++s;
1730 for (pp = ignored; pp->len > 0; ++pp)
1731 if (is_asccaseprefix(s, pp->dat)) {
1732 s += pp->len;
1733 any = TRU1;
1734 goto jouter;
1737 if (re_st != NULL) {
1738 char *cp;
1740 memcpy(re_st_x = re_st + re_l, re_st, re_l);
1741 while ((cp = n_strsep(&re_st_x, ',', TRU1)) != NULL)
1742 if (is_asccaseprefix(s, cp)) {
1743 s += strlen(cp);
1744 any = TRU1;
1745 goto jouter;
1748 break;
1751 if (re_st != NULL)
1752 ac_free(re_st);
1753 NYD_LEAVE;
1754 return any ? s : orig_s;
1757 FL int
1758 msgidcmp(char const *s1, char const *s2)
1760 int q1 = 0, q2 = 0, c1, c2;
1761 NYD_ENTER;
1763 while(*s1 == '<')
1764 ++s1;
1765 while(*s2 == '<')
1766 ++s2;
1768 do {
1769 c1 = msgidnextc(&s1, &q1);
1770 c2 = msgidnextc(&s2, &q2);
1771 if (c1 != c2)
1772 break;
1773 } while (c1 && c2);
1774 NYD_LEAVE;
1775 return c1 - c2;
1778 FL char const *
1779 fakefrom(struct message *mp)
1781 char const *name;
1782 NYD_ENTER;
1784 if (((name = skin(hfield1("return-path", mp))) == NULL || *name == '\0' ) &&
1785 ((name = skin(hfield1("from", mp))) == NULL || *name == '\0'))
1786 /* XXX MAILER-DAEMON is what an old MBOX manual page says.
1787 * RFC 4155 however requires a RFC 5322 (2822) conforming
1788 * "addr-spec", but we simply can't provide that */
1789 name = "MAILER-DAEMON";
1790 NYD_LEAVE;
1791 return name;
1794 FL char const *
1795 fakedate(time_t t)
1797 char *cp, *cq;
1798 NYD_ENTER;
1800 cp = ctime(&t);
1801 for (cq = cp; *cq != '\0' && *cq != '\n'; ++cq)
1803 *cq = '\0';
1804 cp = savestr(cp);
1805 NYD_LEAVE;
1806 return cp;
1809 #ifdef HAVE_IMAP_SEARCH
1810 FL time_t
1811 unixtime(char const *fromline)
1813 char const *fp;
1814 char *xp;
1815 time_t t;
1816 int i, year, month, day, hour, minute, second, tzdiff;
1817 struct tm *tmptr;
1818 NYD2_ENTER;
1820 for (fp = fromline; *fp != '\0' && *fp != '\n'; ++fp)
1822 fp -= 24;
1823 if (PTR2SIZE(fp - fromline) < 7)
1824 goto jinvalid;
1825 if (fp[3] != ' ')
1826 goto jinvalid;
1827 for (i = 0;;) {
1828 if (!strncmp(fp + 4, month_names[i], 3))
1829 break;
1830 if (month_names[++i][0] == '\0')
1831 goto jinvalid;
1833 month = i + 1;
1834 if (fp[7] != ' ')
1835 goto jinvalid;
1836 day = strtol(fp + 8, &xp, 10);
1837 if (*xp != ' ' || xp != fp + 10)
1838 goto jinvalid;
1839 hour = strtol(fp + 11, &xp, 10);
1840 if (*xp != ':' || xp != fp + 13)
1841 goto jinvalid;
1842 minute = strtol(fp + 14, &xp, 10);
1843 if (*xp != ':' || xp != fp + 16)
1844 goto jinvalid;
1845 second = strtol(fp + 17, &xp, 10);
1846 if (*xp != ' ' || xp != fp + 19)
1847 goto jinvalid;
1848 year = strtol(fp + 20, &xp, 10);
1849 if (xp != fp + 24)
1850 goto jinvalid;
1851 if ((t = combinetime(year, month, day, hour, minute, second)) == (time_t)-1)
1852 goto jinvalid;
1853 tzdiff = t - mktime(gmtime(&t));
1854 tmptr = localtime(&t);
1855 if (tmptr->tm_isdst > 0)
1856 tzdiff += 3600;
1857 t -= tzdiff;
1858 jleave:
1859 NYD2_LEAVE;
1860 return t;
1861 jinvalid:
1862 t = n_time_epoch();
1863 goto jleave;
1865 #endif /* HAVE_IMAP_SEARCH */
1867 FL time_t
1868 rfctime(char const *date)
1870 char const *cp = date;
1871 char *x;
1872 time_t t;
1873 int i, year, month, day, hour, minute, second;
1874 NYD2_ENTER;
1876 if ((cp = nexttoken(cp)) == NULL)
1877 goto jinvalid;
1878 if (alphachar(cp[0]) && alphachar(cp[1]) && alphachar(cp[2]) &&
1879 cp[3] == ',') {
1880 if ((cp = nexttoken(&cp[4])) == NULL)
1881 goto jinvalid;
1883 day = strtol(cp, &x, 10); /* XXX strtol */
1884 if ((cp = nexttoken(x)) == NULL)
1885 goto jinvalid;
1886 for (i = 0;;) {
1887 if (!strncmp(cp, month_names[i], 3))
1888 break;
1889 if (month_names[++i][0] == '\0')
1890 goto jinvalid;
1892 month = i + 1;
1893 if ((cp = nexttoken(&cp[3])) == NULL)
1894 goto jinvalid;
1895 /* RFC 5322, 4.3:
1896 * Where a two or three digit year occurs in a date, the year is to be
1897 * interpreted as follows: If a two digit year is encountered whose
1898 * value is between 00 and 49, the year is interpreted by adding 2000,
1899 * ending up with a value between 2000 and 2049. If a two digit year
1900 * is encountered with a value between 50 and 99, or any three digit
1901 * year is encountered, the year is interpreted by adding 1900 */
1902 year = strtol(cp, &x, 10); /* XXX strtol */
1903 i = (int)PTR2SIZE(x - cp);
1904 if (i == 2 && year >= 0 && year <= 49)
1905 year += 2000;
1906 else if (i == 3 || (i == 2 && year >= 50 && year <= 99))
1907 year += 1900;
1908 if ((cp = nexttoken(x)) == NULL)
1909 goto jinvalid;
1910 hour = strtol(cp, &x, 10); /* XXX strtol */
1911 if (*x != ':')
1912 goto jinvalid;
1913 cp = &x[1];
1914 minute = strtol(cp, &x, 10);
1915 if (*x == ':') {
1916 cp = x + 1;
1917 second = strtol(cp, &x, 10);
1918 } else
1919 second = 0;
1921 if ((t = combinetime(year, month, day, hour, minute, second)) == (time_t)-1)
1922 goto jinvalid;
1923 if ((cp = nexttoken(x)) != NULL) {
1924 char buf[3];
1925 int sign = 1;
1927 switch (*cp) {
1928 case '+':
1929 sign = -1;
1930 /* FALLTHRU */
1931 case '-':
1932 ++cp;
1933 break;
1935 if (digitchar(cp[0]) && digitchar(cp[1]) && digitchar(cp[2]) &&
1936 digitchar(cp[3])) {
1937 long tadj;
1938 buf[2] = '\0';
1939 buf[0] = cp[0];
1940 buf[1] = cp[1];
1941 tadj = strtol(buf, NULL, 10) * 3600;/*XXX strtrol*/
1942 buf[0] = cp[2];
1943 buf[1] = cp[3];
1944 tadj += strtol(buf, NULL, 10) * 60; /* XXX strtol*/
1945 if (sign < 0)
1946 tadj = -tadj;
1947 t += tadj;
1949 /* TODO WE DO NOT YET PARSE (OBSOLETE) ZONE NAMES
1950 * TODO once again, Christos Zoulas and NetBSD Mail have done
1951 * TODO a really good job already, but using strptime(3), which
1952 * TODO is not portable. Nonetheless, WE must improve, not
1953 * TODO at last because we simply ignore obsolete timezones!!
1954 * TODO See RFC 5322, 4.3! */
1956 jleave:
1957 NYD2_LEAVE;
1958 return t;
1959 jinvalid:
1960 t = 0;
1961 goto jleave;
1964 FL time_t
1965 combinetime(int year, int month, int day, int hour, int minute, int second){
1966 size_t const jdn_epoch = 2440588;
1967 bool_t const y2038p = (sizeof(time_t) == 4);
1969 size_t jdn;
1970 time_t t;
1971 NYD2_ENTER;
1973 if(UICMP(32, second, >=, DATE_SECSMIN) || /* XXX (leap- */
1974 UICMP(32, minute, >=, DATE_MINSHOUR) ||
1975 UICMP(32, hour, >=, DATE_HOURSDAY) ||
1976 day < 1 || day > 31 ||
1977 month < 1 || month > 12 ||
1978 year < 1970)
1979 goto jerr;
1981 if(year >= 1970 + ((y2038p ? SI32_MAX : SI64_MAX) /
1982 (DATE_SECSDAY * DATE_DAYSYEAR))){
1983 /* Be a coward regarding Y2038, many people (mostly myself, that is) do
1984 * test by stepping second-wise around the flip. Don't care otherwise */
1985 if(!y2038p)
1986 goto jerr;
1987 if(year > 2038 || month > 1 || day > 19 ||
1988 hour > 3 || minute > 14 || second > 7)
1989 goto jerr;
1992 t = second;
1993 t += minute * DATE_SECSMIN;
1994 t += hour * DATE_SECSHOUR;
1996 jdn = a_head_gregorian_to_jdn(year, month, day);
1997 jdn -= jdn_epoch;
1998 t += (time_t)jdn * DATE_SECSDAY;
1999 jleave:
2000 NYD2_LEAVE;
2001 return t;
2002 jerr:
2003 t = (time_t)-1;
2004 goto jleave;
2007 FL void
2008 substdate(struct message *m)
2010 char const *cp;
2011 NYD_ENTER;
2013 /* Determine the date to print in faked 'From ' lines. This is traditionally
2014 * the date the message was written to the mail file. Try to determine this
2015 * using RFC message header fields, or fall back to current time */
2016 if ((cp = hfield1("received", m)) != NULL) {
2017 while ((cp = nexttoken(cp)) != NULL && *cp != ';') {
2019 ++cp;
2020 while (alnumchar(*cp));
2022 if (cp && *++cp)
2023 m->m_time = rfctime(cp);
2025 if (m->m_time == 0 || m->m_time > time_current.tc_time) {
2026 if ((cp = hfield1("date", m)) != NULL)
2027 m->m_time = rfctime(cp);
2029 if (m->m_time == 0 || m->m_time > time_current.tc_time)
2030 m->m_time = time_current.tc_time;
2031 NYD_LEAVE;
2034 FL void
2035 setup_from_and_sender(struct header *hp)
2037 char const *addr;
2038 struct name *np;
2039 NYD_ENTER;
2041 /* If -t parsed or composed From: then take it. With -t we otherwise
2042 * want -r to be honoured in favour of *from* in order to have
2043 * a behaviour that is compatible with what users would expect from e.g.
2044 * postfix(1) */
2045 if ((np = hp->h_from) != NULL ||
2046 ((pstate & PS_t_FLAG) && (np = option_r_arg) != NULL)) {
2048 } else if ((addr = myaddrs(hp)) != NULL)
2049 np = lextract(addr, GEXTRA | GFULL | GFULLEXTRA);
2050 hp->h_from = np;
2052 if ((np = hp->h_sender) != NULL) {
2054 } else if ((addr = ok_vlook(sender)) != NULL)
2055 np = lextract(addr, GEXTRA | GFULL | GFULLEXTRA);
2056 hp->h_sender = np;
2058 NYD_LEAVE;
2061 FL struct name const *
2062 check_from_and_sender(struct name const *fromfield,
2063 struct name const *senderfield)
2065 struct name const *rv = NULL;
2066 NYD_ENTER;
2068 if (senderfield != NULL) {
2069 if (senderfield->n_flink != NULL) {
2070 n_err(_("The Sender: field may contain only one address\n"));
2071 goto jleave;
2073 rv = senderfield;
2076 if (fromfield != NULL) {
2077 if (fromfield->n_flink != NULL && senderfield == NULL) {
2078 n_err(_("A Sender: is required when there are multiple "
2079 "addresses in From:\n"));
2080 goto jleave;
2082 if (rv == NULL)
2083 rv = fromfield;
2086 if (rv == NULL)
2087 rv = (struct name*)0x1;
2088 jleave:
2089 NYD_LEAVE;
2090 return rv;
2093 #ifdef HAVE_SSL_TLS
2094 FL char *
2095 getsender(struct message *mp)
2097 char *cp;
2098 struct name *np;
2099 NYD_ENTER;
2101 if ((cp = hfield1("from", mp)) == NULL ||
2102 (np = lextract(cp, GEXTRA | GSKIN)) == NULL)
2103 cp = NULL;
2104 else
2105 cp = (np->n_flink != NULL) ? skin(hfield1("sender", mp)) : np->n_name;
2106 NYD_LEAVE;
2107 return cp;
2109 #endif
2111 FL int
2112 grab_headers(enum n_lexinput_flags lif, struct header *hp, enum gfield gflags,
2113 int subjfirst)
2115 /* TODO grab_headers: again, check counts etc. against RFC;
2116 * TODO (now assumes check_from_and_sender() is called afterwards ++ */
2117 int errs;
2118 int volatile comma;
2119 NYD_ENTER;
2121 errs = 0;
2122 comma = (ok_blook(bsdcompat) || ok_blook(bsdmsgs)) ? 0 : GCOMMA;
2124 if (gflags & GTO)
2125 hp->h_to = grab_names(lif, "To: ", hp->h_to, comma, GTO | GFULL);
2126 if (subjfirst && (gflags & GSUBJECT))
2127 hp->h_subject = n_lex_input_cp(lif, "Subject: ", hp->h_subject);
2128 if (gflags & GCC)
2129 hp->h_cc = grab_names(lif, "Cc: ", hp->h_cc, comma, GCC | GFULL);
2130 if (gflags & GBCC)
2131 hp->h_bcc = grab_names(lif, "Bcc: ", hp->h_bcc, comma, GBCC | GFULL);
2133 if (gflags & GEXTRA) {
2134 if (hp->h_from == NULL)
2135 hp->h_from = lextract(myaddrs(hp), GEXTRA | GFULL | GFULLEXTRA);
2136 hp->h_from = grab_names(lif, "From: ", hp->h_from, comma,
2137 GEXTRA | GFULL | GFULLEXTRA);
2138 if (hp->h_replyto == NULL)
2139 hp->h_replyto = lextract(ok_vlook(replyto), GEXTRA | GFULL);
2140 hp->h_replyto = grab_names(lif, "Reply-To: ", hp->h_replyto, comma,
2141 GEXTRA | GFULL);
2142 if (hp->h_sender == NULL)
2143 hp->h_sender = extract(ok_vlook(sender), GEXTRA | GFULL);
2144 hp->h_sender = grab_names(lif, "Sender: ", hp->h_sender, comma,
2145 GEXTRA | GFULL);
2148 if (!subjfirst && (gflags & GSUBJECT))
2149 hp->h_subject = n_lex_input_cp(lif, "Subject: ", hp->h_subject);
2151 NYD_LEAVE;
2152 return errs;
2155 FL bool_t
2156 header_match(struct message *mp, struct search_expr const *sep)
2158 struct str in, out;
2159 FILE *ibuf;
2160 int lc;
2161 size_t linesize = 0; /* TODO line pool */
2162 char *linebuf = NULL, *colon;
2163 bool_t rv = FAL0;
2164 NYD_ENTER;
2166 if ((ibuf = setinput(&mb, mp, NEED_HEADER)) == NULL)
2167 goto jleave;
2168 if ((lc = mp->m_lines - 1) < 0)
2169 goto jleave;
2171 if ((mp->m_flag & MNOFROM) == 0 &&
2172 readline_restart(ibuf, &linebuf, &linesize, 0) < 0)
2173 goto jleave;
2174 while (lc > 0) {
2175 if (gethfield(ibuf, &linebuf, &linesize, lc, &colon) <= 0)
2176 break;
2177 if (blankchar(*++colon))
2178 ++colon;
2179 in.l = strlen(in.s = colon);
2180 mime_fromhdr(&in, &out, TD_ICONV);
2181 #ifdef HAVE_REGEX
2182 if (sep->ss_sexpr == NULL)
2183 rv = (regexec(&sep->ss_regex, out.s, 0,NULL, 0) != REG_NOMATCH);
2184 else
2185 #endif
2186 rv = substr(out.s, sep->ss_sexpr);
2187 free(out.s);
2188 if (rv)
2189 break;
2192 jleave:
2193 if (linebuf != NULL)
2194 free(linebuf);
2195 NYD_LEAVE;
2196 return rv;
2199 /* s-it-mode */