Add *{smime,ssl}-ca-flags*..
[s-mailx.git] / head.c
blob24579a87b500f8311e7512a7e023e3d1ca307773
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Routines for processing and detecting headlines.
3 *@ TODO Mostly a hackery, we need RFC compliant parsers instead.
5 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
6 * Copyright (c) 2012 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
7 */
8 /*
9 * Copyright (c) 1980, 1993
10 * The Regents of the University of California. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
36 #undef n_FILE
37 #define n_FILE head
39 #ifndef HAVE_AMALGAMATION
40 # include "nail.h"
41 #endif
43 #ifdef HAVE_IDNA
44 # if HAVE_IDNA == HAVE_IDNA_LIBIDNA
45 # include <idna.h>
46 # include <idn-free.h>
47 # include <stringprep.h>
48 # elif HAVE_IDNA == HAVE_IDNA_IDNKIT
49 # include <idn/api.h>
50 # endif
51 #endif
53 struct cmatch_data {
54 size_t tlen; /* Length of .tdata */
55 char const *tdata; /* Template date - see _cmatch_data[] */
58 /* Template characters for cmatch_data.tdata:
59 * 'A' An upper case char
60 * 'a' A lower case char
61 * ' ' A space
62 * '0' A digit
63 * 'O' An optional digit or space
64 * ':' A colon
65 * '+' Either a plus or a minus sign */
66 static struct cmatch_data const _cmatch_data[] = {
67 { 24, "Aaa Aaa O0 00:00:00 0000" }, /* BSD/ISO C90 ctime */
68 { 28, "Aaa Aaa O0 00:00:00 AAA 0000" }, /* BSD tmz */
69 { 21, "Aaa Aaa O0 00:00 0000" }, /* SysV ctime */
70 { 25, "Aaa Aaa O0 00:00 AAA 0000" }, /* SysV tmz */
71 /* RFC 822-alike From_ lines do not conform to RFC 4155, but seem to be used
72 * in the wild (by UW-imap) */
73 { 30, "Aaa Aaa O0 00:00:00 0000 +0000" },
74 /* RFC 822 with zone spec; 1. military, 2. UT, 3. north america time
75 * zone strings; note that 1. is strictly speaking not correct as some
76 * letters are not used, and 2. is not because only "UT" is defined */
77 #define __reuse "Aaa Aaa O0 00:00:00 0000 AAA"
78 { 28 - 2, __reuse }, { 28 - 1, __reuse }, { 28 - 0, __reuse },
79 { 0, NULL }
81 #define _DATE_MINLEN 21
83 /* Skip over "word" as found in From_ line */
84 static char const * _from__skipword(char const *wp);
86 /* Match the date string against the date template (tp), return if match.
87 * See _cmatch_data[] for template character description */
88 static int _cmatch(size_t len, char const *date,
89 char const *tp);
91 /* Check whether date is a valid 'From_' date.
92 * (Rather ctime(3) generated dates, according to RFC 4155) */
93 static int _is_date(char const *date);
95 /* JulianDayNumber converter(s) */
96 static size_t a_head_gregorian_to_jdn(ui32_t y, ui32_t m, ui32_t d);
97 #if 0
98 static void a_head_jdn_to_gregorian(size_t jdn,
99 ui32_t *yp, ui32_t *mp, ui32_t *dp);
100 #endif
102 /* Convert the domain part of a skinned address to IDNA.
103 * If an error occurs before Unicode information is available, revert the IDNA
104 * error to a normal CHAR one so that the error message doesn't talk Unicode */
105 #ifdef HAVE_IDNA
106 static struct n_addrguts *a_head_idna_apply(struct n_addrguts *agp);
107 #endif
109 /* Classify and check a (possibly skinned) header body according to RFC
110 * *addr-spec* rules; if it (is assumed to has been) skinned it may however be
111 * also a file or a pipe command, so check that first, then.
112 * Otherwise perform content checking and isolate the domain part (for IDNA) */
113 static bool_t a_head_addrspec_check(struct n_addrguts *agp, bool_t skinned);
115 /* Return the next header field found in the given message.
116 * Return >= 0 if something found, < 0 elsewise.
117 * "colon" is set to point to the colon in the header.
118 * Must deal with \ continuations & other such fraud */
119 static int gethfield(FILE *f, char **linebuf, size_t *linesize,
120 int rem, char **colon);
122 static int msgidnextc(char const **cp, int *status);
124 /* Count the occurances of c in str */
125 static int charcount(char *str, int c);
127 static char const * nexttoken(char const *cp);
129 /* TODO v15: change *customhdr* syntax and use shell tokens?! */
130 static char *a_head_customhdr__sep(char **iolist);
132 static char const *
133 _from__skipword(char const *wp)
135 char c = 0;
136 NYD2_ENTER;
138 if (wp != NULL) {
139 while ((c = *wp++) != '\0' && !blankchar(c)) {
140 if (c == '"') {
141 while ((c = *wp++) != '\0' && c != '"')
143 if (c != '"')
144 --wp;
147 for (; blankchar(c); c = *wp++)
150 NYD2_LEAVE;
151 return (c == 0 ? NULL : wp - 1);
154 static int
155 _cmatch(size_t len, char const *date, char const *tp)
157 int ret = 0;
158 NYD2_ENTER;
160 while (len--) {
161 char c = date[len];
162 switch (tp[len]) {
163 case 'a':
164 if (!lowerchar(c))
165 goto jleave;
166 break;
167 case 'A':
168 if (!upperchar(c))
169 goto jleave;
170 break;
171 case ' ':
172 if (c != ' ')
173 goto jleave;
174 break;
175 case '0':
176 if (!digitchar(c))
177 goto jleave;
178 break;
179 case 'O':
180 if (c != ' ' && !digitchar(c))
181 goto jleave;
182 break;
183 case ':':
184 if (c != ':')
185 goto jleave;
186 break;
187 case '+':
188 if (c != '+' && c != '-')
189 goto jleave;
190 break;
193 ret = 1;
194 jleave:
195 NYD2_LEAVE;
196 return ret;
199 static int
200 _is_date(char const *date)
202 struct cmatch_data const *cmdp;
203 size_t dl;
204 int rv = 0;
205 NYD2_ENTER;
207 if ((dl = strlen(date)) >= _DATE_MINLEN)
208 for (cmdp = _cmatch_data; cmdp->tdata != NULL; ++cmdp)
209 if (dl == cmdp->tlen && (rv = _cmatch(dl, date, cmdp->tdata)))
210 break;
211 NYD2_LEAVE;
212 return rv;
215 static size_t
216 a_head_gregorian_to_jdn(ui32_t y, ui32_t m, ui32_t d){
217 /* Algorithm is taken from Communications of the ACM, Vol 6, No 8.
218 * (via third hand, plus adjustments).
219 * This algorithm is supposed to work for all dates in between 1582-10-15
220 * (0001-01-01 but that not Gregorian) and 65535-12-31 */
221 size_t jdn;
222 NYD2_ENTER;
224 #if 0
225 if(y == 0)
226 y = 1;
227 if(m == 0)
228 m = 1;
229 if(d == 0)
230 d = 1;
231 #endif
233 if(m > 2)
234 m -= 3;
235 else{
236 m += 9;
237 --y;
239 jdn = y;
240 jdn /= 100;
241 y -= 100 * jdn;
242 y *= 1461;
243 y >>= 2;
244 jdn *= 146097;
245 jdn >>= 2;
246 jdn += y;
247 jdn += d;
248 jdn += 1721119;
249 m *= 153;
250 m += 2;
251 m /= 5;
252 jdn += m;
253 NYD2_LEAVE;
254 return jdn;
257 #if 0
258 static void
259 a_head_jdn_to_gregorian(size_t jdn, ui32_t *yp, ui32_t *mp, ui32_t *dp){
260 /* Algorithm is taken from Communications of the ACM, Vol 6, No 8.
261 * (via third hand, plus adjustments) */
262 size_t y, x;
263 NYD2_ENTER;
265 jdn -= 1721119;
266 jdn <<= 2;
267 --jdn;
268 y = jdn / 146097;
269 jdn %= 146097;
270 jdn |= 3;
271 y *= 100;
272 y += jdn / 1461;
273 jdn %= 1461;
274 jdn += 4;
275 jdn >>= 2;
276 x = jdn;
277 jdn <<= 2;
278 jdn += x;
279 jdn -= 3;
280 x = jdn / 153; /* x -> month */
281 jdn %= 153;
282 jdn += 5;
283 jdn /= 5; /* jdn -> day */
284 if(x < 10)
285 x += 3;
286 else{
287 x -= 9;
288 ++y;
291 *yp = (ui32_t)(y & 0xFFFF);
292 *mp = (ui32_t)(x & 0xFF);
293 *dp = (ui32_t)(jdn & 0xFF);
294 NYD2_LEAVE;
296 #endif /* 0 */
298 #ifdef HAVE_IDNA
299 # if HAVE_IDNA == HAVE_IDNA_LIBIDNA
300 static struct n_addrguts *
301 a_head_idna_apply(struct n_addrguts *agp)
303 char *idna_utf8, *idna_ascii, *cs;
304 size_t sz, i;
305 NYD_ENTER;
307 sz = agp->ag_slen - agp->ag_sdom_start;
308 assert(sz > 0);
309 idna_utf8 = ac_alloc(sz +1);
310 memcpy(idna_utf8, agp->ag_skinned + agp->ag_sdom_start, sz);
311 idna_utf8[sz] = '\0';
313 /* GNU Libidn settles on top of iconv(3) without any fallback, so let's just
314 * let it perform the charset conversion, if any should be necessary */
315 if (!(n_psonce & n_PSO_UNICODE)) {
316 char const *tcs = ok_vlook(ttycharset);
317 idna_ascii = idna_utf8;
318 idna_utf8 = stringprep_convert(idna_ascii, "utf-8", tcs);
319 i = (idna_utf8 == NULL && errno == EINVAL);
320 ac_free(idna_ascii);
322 if (idna_utf8 == NULL) {
323 if (i)
324 n_err(_("Cannot convert from %s to %s\n"), tcs, "utf-8");
325 agp->ag_n_flags ^= NAME_ADDRSPEC_ERR_IDNA | NAME_ADDRSPEC_ERR_CHAR;
326 goto jleave;
330 if (idna_to_ascii_8z(idna_utf8, &idna_ascii, 0) != IDNA_SUCCESS) {
331 agp->ag_n_flags ^= NAME_ADDRSPEC_ERR_IDNA | NAME_ADDRSPEC_ERR_CHAR;
332 goto jleave1;
335 /* Replace the domain part of .ag_skinned with IDNA version */
336 sz = strlen(idna_ascii);
337 i = agp->ag_sdom_start;
338 cs = salloc(i + sz +1);
339 memcpy(cs, agp->ag_skinned, i);
340 memcpy(&cs[i], idna_ascii, sz);
341 i += sz;
342 cs[i] = '\0';
344 agp->ag_skinned = cs;
345 agp->ag_slen = i;
346 NAME_ADDRSPEC_ERR_SET(agp->ag_n_flags,
347 NAME_NAME_SALLOC | NAME_SKINNED | NAME_IDNA, 0);
349 idn_free(idna_ascii);
350 jleave1:
351 if (n_psonce & n_PSO_UNICODE)
352 ac_free(idna_utf8);
353 else
354 idn_free(idna_utf8);
355 jleave:
356 NYD_LEAVE;
357 return agp;
360 # elif HAVE_IDNA == HAVE_IDNA_IDNKIT /* IDNA==LIBIDNA */
361 static struct n_addrguts *
362 a_head_idna_apply(struct n_addrguts *agp)
364 char *idna_in, *idna_out, *cs;
365 size_t sz, i;
366 idn_result_t r;
367 NYD_ENTER;
369 sz = agp->ag_slen - agp->ag_sdom_start;
370 assert(sz > 0);
371 idna_in = ac_alloc(sz +1);
372 memcpy(idna_in, agp->ag_skinned + agp->ag_sdom_start, sz);
373 idna_in[sz] = '\0';
375 for (idna_out = NULL, sz = HOST_NAME_MAX +1;; sz += HOST_NAME_MAX) {
376 idna_out = ac_alloc(sz);
378 r = idn_encodename(IDN_ENCODE_APP, idna_in, idna_out, sz);
379 switch (r) {
380 case idn_success:
381 case idn_buffer_overflow:
382 break;
383 case idn_invalid_encoding:
384 n_err(_("Cannot convert from %s to %s\n"),
385 ok_vlook(ttycharset), "utf-8");
386 /* FALLTHRU */
387 default:
388 agp->ag_n_flags ^= NAME_ADDRSPEC_ERR_IDNA | NAME_ADDRSPEC_ERR_CHAR;
389 goto jleave;
392 if (r == idn_success)
393 break;
394 ac_free(idna_out);
397 /* Replace the domain part of .ag_skinned with IDNA version */
398 sz = strlen(idna_out);
399 i = agp->ag_sdom_start;
400 cs = salloc(i + sz +1);
401 memcpy(cs, agp->ag_skinned, i);
402 memcpy(&cs[i], idna_out, sz);
403 i += sz;
404 cs[i] = '\0';
406 agp->ag_skinned = cs;
407 agp->ag_slen = i;
408 NAME_ADDRSPEC_ERR_SET(agp->ag_n_flags,
409 NAME_NAME_SALLOC | NAME_SKINNED | NAME_IDNA, 0);
411 jleave:
412 ac_free(idna_out);
413 ac_free(idna_in);
414 NYD_LEAVE;
415 return agp;
417 # endif /* IDNA==IDNKIT */
418 #endif /* HAVE_IDNA */
420 static bool_t
421 a_head_addrspec_check(struct n_addrguts *agp, bool_t skinned)
423 char *addr, *p;
424 bool_t in_quote;
425 ui8_t in_domain, hadat;
426 union {bool_t b; char c; unsigned char u; ui32_t ui32; si32_t si32;} c;
427 #ifdef HAVE_IDNA
428 ui8_t use_idna;
429 #endif
430 NYD_ENTER;
432 #ifdef HAVE_IDNA
433 use_idna = ok_blook(idna_disable) ? 0 : 1;
434 #endif
435 agp->ag_n_flags |= NAME_ADDRSPEC_CHECKED;
436 addr = agp->ag_skinned;
438 if (agp->ag_iaddr_aend - agp->ag_iaddr_start == 0) {
439 NAME_ADDRSPEC_ERR_SET(agp->ag_n_flags, NAME_ADDRSPEC_ERR_EMPTY, 0);
440 goto jleave;
443 /* If the field is not a recipient, it cannot be a file or a pipe */
444 if (!skinned)
445 goto jaddr_check;
447 /* When changing any of the following adjust any RECIPIENTADDRSPEC;
448 * grep the latter for the complete picture */
449 if (*addr == '|') {
450 agp->ag_n_flags |= NAME_ADDRSPEC_ISPIPE;
451 goto jleave;
453 if (addr[0] == '/' || (addr[0] == '.' && addr[1] == '/') ||
454 (addr[0] == '-' && addr[1] == '\0'))
455 goto jisfile;
456 if (memchr(addr, '@', agp->ag_slen) == NULL) {
457 if (*addr == '+')
458 goto jisfile;
459 for (p = addr; (c.c = *p); ++p) {
460 if (c.c == '!' || c.c == '%')
461 break;
462 if (c.c == '/') {
463 jisfile:
464 agp->ag_n_flags |= NAME_ADDRSPEC_ISFILE;
465 goto jleave;
470 jaddr_check:
471 in_quote = FAL0;
472 in_domain = hadat = 0;
474 /* TODO addrspec_check: we need a real RFC 5322 (un)?structured parser! */
475 for (p = addr; (c.c = *p++) != '\0';) {
476 if (c.c == '"') {
477 in_quote = !in_quote;
478 } else if (c.u < 040 || c.u >= 0177) { /* TODO no magics: !bodychar()? */
479 #ifdef HAVE_IDNA
480 if (in_domain && use_idna > 0) {
481 if (use_idna == 1)
482 NAME_ADDRSPEC_ERR_SET(agp->ag_n_flags, NAME_ADDRSPEC_ERR_IDNA,
483 c.u);
484 use_idna = 2;
485 } else
486 #endif
487 break;
488 } else if (in_domain == 2) {
489 if ((c.c == ']' && *p != '\0') || c.c == '\\' || whitechar(c.c))
490 break;
491 } else if (in_quote && in_domain == 0) {
492 /*EMPTY*/;
493 } else if (c.c == '\\' && *p != '\0') {
494 ++p;
495 } else if (c.c == '@') {
496 if (hadat++ > 0) {
497 NAME_ADDRSPEC_ERR_SET(agp->ag_n_flags, NAME_ADDRSPEC_ERR_ATSEQ,
498 c.u);
499 goto jleave;
501 agp->ag_sdom_start = PTR2SIZE(p - addr);
502 agp->ag_n_flags |= NAME_ADDRSPEC_ISADDR; /* TODO .. really? */
503 in_domain = (*p == '[') ? 2 : 1;
504 continue;
505 } else if (c.c == '(' || c.c == ')' || c.c == '<' || c.c == '>' ||
506 c.c == '[' || c.c == ']' || c.c == ':' || c.c == ';' ||
507 c.c == '\\' || c.c == ',')
508 break;
509 hadat = 0;
511 if (c.c != '\0') {
512 NAME_ADDRSPEC_ERR_SET(agp->ag_n_flags, NAME_ADDRSPEC_ERR_CHAR, c.u);
513 goto jleave;
516 if(!(agp->ag_n_flags & NAME_ADDRSPEC_ISADDR)){
517 agp->ag_n_flags |= NAME_ADDRSPEC_ISNAME;
518 if(!n_alias_is_valid_name(agp->ag_input))
519 NAME_ADDRSPEC_ERR_SET(agp->ag_n_flags, NAME_ADDRSPEC_ERR_NAME, '.');
520 }else{
521 /* If we seem to know that this is an address. Ensure this is correct
522 * according to RFC 5322 TODO the entire address parser should be like
523 * TODO that for one, and then we should now whether structured or
524 * TODO unstructured, and just parse correctly overall!
525 * TODO In addition, this can be optimised a lot */
526 struct a_token{
527 struct a_token *t_last;
528 struct a_token *t_next;
529 enum{
530 a_T_TATOM = 1<<0,
531 a_T_TCOMM = 1<<1,
532 a_T_TQUOTE = 1<<2,
533 a_T_TADDR = 1<<3,
534 a_T_TMASK = (1<<4) - 1,
536 a_T_SPECIAL = 1<<8 /* An atom actually needs to go TQUOTE */
537 } t_f;
538 ui8_t t__pad[4];
539 size_t t_start;
540 size_t t_end;
541 } *thead, *tcurr, *tp;
543 struct n_string ost, *ostp;
544 char const *cp, *cp1st, *cpmax, *xp;
545 void *lofi_snap;
547 /* Name and domain must be non-empty */
548 if(*addr == '@' || &addr[2] >= p || p[-2] == '@'){
549 c.c = '@';
550 NAME_ADDRSPEC_ERR_SET(agp->ag_n_flags, NAME_ADDRSPEC_ERR_ATSEQ, c.u);
551 goto jleave;
554 #ifdef HAVE_IDNA
555 if(use_idna == 2)
556 agp = a_head_idna_apply(agp);
557 #endif
559 cp = agp->ag_input;
561 /* Nothing to do if there is only an address (in angle brackets) */
562 if(agp->ag_iaddr_start == 0){
563 if(agp->ag_iaddr_aend == agp->ag_ilen)
564 goto jleave;
565 }else if(agp->ag_iaddr_start == 1 && *cp == '<' &&
566 agp->ag_iaddr_aend == agp->ag_ilen - 1 &&
567 cp[agp->ag_iaddr_aend] == '>')
568 goto jleave;
570 /* It is not, so parse off all tokens, then resort and rejoin */
571 lofi_snap = n_lofi_snap_create();
573 cp1st = cp;
574 if((c.ui32 = agp->ag_iaddr_start) > 0)
575 --c.ui32;
576 cpmax = &cp[c.ui32];
578 thead = tcurr = NULL;
579 hadat = FAL0;
580 jnode_redo:
581 for(tp = NULL; cp < cpmax;){
582 switch((c.c = *cp)){
583 case '(':
584 if(tp != NULL)
585 tp->t_end = PTR2SIZE(cp - cp1st);
586 tp = n_lofi_alloc(sizeof *tp);
587 tp->t_next = NULL;
588 if((tp->t_last = tcurr) != NULL)
589 tcurr->t_next = tp;
590 else
591 thead = tp;
592 tcurr = tp;
593 tp->t_f = a_T_TCOMM;
594 tp->t_start = PTR2SIZE(++cp - cp1st);
595 xp = skip_comment(cp);
596 tp->t_end = PTR2SIZE(xp - cp1st);
597 cp = xp;
598 if(tp->t_end > tp->t_start){
599 if(xp[-1] == ')')
600 --tp->t_end;
601 else{
602 /* No closing comment - strip trailing whitespace */
603 while(blankchar(*--xp))
604 if(--tp->t_end == tp->t_start)
605 break;
608 tp = NULL;
609 break;
611 case '"':
612 if(tp != NULL)
613 tp->t_end = PTR2SIZE(cp - cp1st);
614 tp = n_lofi_alloc(sizeof *tp);
615 tp->t_next = NULL;
616 if((tp->t_last = tcurr) != NULL)
617 tcurr->t_next = tp;
618 else
619 thead = tp;
620 tcurr = tp;
621 tp->t_f = a_T_TQUOTE;
622 tp->t_start = PTR2SIZE(++cp - cp1st);
623 for(xp = cp; xp < cpmax; ++xp){
624 if((c.c = *xp) == '"')
625 break;
626 if(c.c == '\\' && xp[1] != '\0')
627 ++xp;
629 tp->t_end = PTR2SIZE(xp - cp1st);
630 cp = &xp[1];
631 if(tp->t_end > tp->t_start){
632 /* No closing quote - strip trailing whitespace */
633 if(*xp != '"'){
634 while(blankchar(*xp--))
635 if(--tp->t_end == tp->t_start)
636 break;
639 tp = NULL;
640 break;
642 default:
643 if(blankchar(c.c)){
644 if(tp != NULL)
645 tp->t_end = PTR2SIZE(cp - cp1st);
646 tp = NULL;
647 ++cp;
648 break;
651 if(tp == NULL){
652 tp = n_lofi_alloc(sizeof *tp);
653 tp->t_next = NULL;
654 if((tp->t_last = tcurr) != NULL)
655 tcurr->t_next = tp;
656 else
657 thead = tp;
658 tcurr = tp;
659 tp->t_f = a_T_TATOM;
660 tp->t_start = PTR2SIZE(cp - cp1st);
662 ++cp;
664 /* Reverse solidus transforms the following into a quoted-pair, and
665 * therefore (must occur in comment or quoted-string only) the
666 * entire atom into a quoted string */
667 if(c.c == '\\'){
668 tp->t_f |= a_T_SPECIAL;
669 if(cp < cpmax)
670 ++cp;
672 /* Is this plain RFC 5322 "atext", or "specials"? Because we don't
673 * TODO know structured/unstructured, nor anything else, we need to
674 * TODO treat "dot-atom" as being identical to "specials" */
675 else if(!alnumchar(c.c) &&
676 c.c != '!' && c.c != '#' && c.c != '$' && c.c != '%' &&
677 c.c != '&' && c.c != '\'' && c.c != '*' && c.c != '+' &&
678 c.c != '-' && c.c != '/' && c.c != '=' && c.c != '?' &&
679 c.c != '^' && c.c != '_' && c.c != '`' && c.c != '{' &&
680 c.c != '}' && c.c != '|' && c.c != '}' && c.c != '~')
681 tp->t_f |= a_T_SPECIAL;
682 break;
685 if(tp != NULL)
686 tp->t_end = PTR2SIZE(cp - cp1st);
688 if(hadat == FAL0){
689 hadat = TRU1;
690 tp = n_lofi_alloc(sizeof *tp);
691 tp->t_next = NULL;
692 if((tp->t_last = tcurr) != NULL)
693 tcurr->t_next = tp;
694 else
695 thead = tp;
696 tcurr = tp;
697 tp->t_f = a_T_TADDR;
698 tp->t_start = agp->ag_iaddr_start;
699 tp->t_end = agp->ag_iaddr_aend;
700 tp = NULL;
702 cp = &agp->ag_input[agp->ag_iaddr_aend + 1];
703 cpmax = &agp->ag_input[agp->ag_ilen];
704 if(cp < cpmax)
705 goto jnode_redo;
708 /* Nothing may follow the address, move it to the end */
709 if(!(tcurr->t_f & a_T_TADDR)){
710 for(tp = thead; tp != NULL; tp = tp->t_next)
711 if(tp->t_f & a_T_TADDR){
712 if(tp->t_last != NULL)
713 tp->t_last->t_next = tp->t_next;
714 else
715 thead = tp->t_next;
716 if(tp->t_next != NULL)
717 tp->t_next->t_last = tp->t_last;
719 tcurr = tp;
720 while(tp->t_next != NULL)
721 tp = tp->t_next;
722 tp->t_next = tcurr;
723 tcurr->t_last = tp;
724 tcurr->t_next = NULL;
725 break;
729 /* Make ranges contiguous: ensure a continuous range of atoms is converted
730 * to a SPECIAL one if at least one of them requires it */
731 for(tp = thead; tp != NULL; tp = tp->t_next){
732 if(tp->t_f & a_T_SPECIAL){
733 tcurr = tp;
734 while((tp = tp->t_last) != NULL && (tp->t_f & a_T_TATOM))
735 tp->t_f |= a_T_SPECIAL;
736 tp = tcurr;
737 while((tp = tp->t_next) != NULL && (tp->t_f & a_T_TATOM))
738 tp->t_f |= a_T_SPECIAL;
741 /* And yes, we want quotes to extend as much as possible */
742 for(tp = thead; tp != NULL; tp = tp->t_next){
743 if(tp->t_f & a_T_TQUOTE){
744 tcurr = tp;
745 while((tp = tp->t_last) != NULL && (tp->t_f & a_T_TATOM))
746 tp->t_f |= a_T_SPECIAL;
747 tp = tcurr;
748 while((tp = tp->t_next) != NULL && (tp->t_f & a_T_TATOM))
749 tp->t_f |= a_T_SPECIAL;
753 /* Then rejoin */
754 ostp = n_string_creat_auto(&ost);
755 if((c.ui32 = agp->ag_ilen) <= UI32_MAX >> 1)
756 ostp = n_string_reserve(ostp, c.ui32 <<= 1);
758 for(tcurr = thead; tcurr != NULL;){
759 if(tcurr != thead)
760 ostp = n_string_push_c(ostp, ' ');
761 if(tcurr->t_f & a_T_TADDR){
762 ostp = n_string_push_c(ostp, '<');
763 agp->ag_iaddr_start = ostp->s_len;
764 ostp = n_string_push_buf(ostp, &cp1st[tcurr->t_start],
765 (tcurr->t_end - tcurr->t_start));
766 agp->ag_iaddr_aend = ostp->s_len;
767 ostp = n_string_push_c(ostp, '>');
768 tcurr = tcurr->t_next;
769 }else if(tcurr->t_f & a_T_TCOMM){
770 ostp = n_string_push_c(ostp, '(');
771 ostp = n_string_push_buf(ostp, &cp1st[tcurr->t_start],
772 (tcurr->t_end - tcurr->t_start));
773 while((tp = tcurr->t_next) != NULL && (tp->t_f & a_T_TCOMM)){
774 tcurr = tp;
775 ostp = n_string_push_c(ostp, ' ');
776 ostp = n_string_push_buf(ostp, &cp1st[tcurr->t_start],
777 (tcurr->t_end - tcurr->t_start));
779 ostp = n_string_push_c(ostp, ')');
780 tcurr = tcurr->t_next;
781 }else if(tcurr->t_f & a_T_TQUOTE){
782 jput_quote:
783 ostp = n_string_push_c(ostp, '"');
784 tp = tcurr;
785 do/* while tcurr && TATOM||TQUOTE */{
786 if(tcurr != tp)
787 ostp = n_string_push_c(ostp, ' ');
788 if((tcurr->t_f & (a_T_TATOM | a_T_SPECIAL)) == a_T_TATOM)
789 ostp = n_string_push_buf(ostp, &cp1st[tcurr->t_start],
790 (tcurr->t_end - tcurr->t_start));
791 else{
792 bool_t esc;
794 cp = &cp1st[tcurr->t_start];
795 cpmax = &cp1st[tcurr->t_end];
797 for(esc = FAL0; cp < cpmax;){
798 if((c.c = *cp++) == '\\' && !esc)
799 esc = TRU1;
800 else{
801 if(esc || c.c == '"'){
802 jput_quote_esc:
803 ostp = n_string_push_c(ostp, '\\');
805 ostp = n_string_push_c(ostp, c.c);
806 esc = FAL0;
809 if(esc){
810 c.c = '\\';
811 goto jput_quote_esc;
814 }while((tcurr = tcurr->t_next) != NULL &&
815 (tcurr->t_f & (a_T_TATOM | a_T_TQUOTE)));
816 ostp = n_string_push_c(ostp, '"');
817 }else if(tcurr->t_f & a_T_SPECIAL)
818 goto jput_quote;
819 else{
820 /* Can we use a fast join mode? */
821 for(tp = tcurr; tcurr != NULL; tcurr = tcurr->t_next){
822 if(!(tcurr->t_f & a_T_TATOM))
823 break;
824 if(tcurr != tp)
825 ostp = n_string_push_c(ostp, ' ');
826 ostp = n_string_push_buf(ostp, &cp1st[tcurr->t_start],
827 (tcurr->t_end - tcurr->t_start));
832 n_lofi_snap_unroll(lofi_snap);
834 agp->ag_input = n_string_cp(ostp);
835 agp->ag_ilen = ostp->s_len;
836 ostp = n_string_drop_ownership(ostp);
838 jleave:
839 NYD_LEAVE;
840 return ((agp->ag_n_flags & NAME_ADDRSPEC_INVALID) == 0);
843 static int
844 gethfield(FILE *f, char **linebuf, size_t *linesize, int rem, char **colon)
846 char *line2 = NULL, *cp, *cp2;
847 size_t line2size = 0;
848 int c, isenc;
849 NYD2_ENTER;
851 if (*linebuf == NULL)
852 *linebuf = srealloc(*linebuf, *linesize = 1);
853 **linebuf = '\0';
854 for (;;) {
855 if (--rem < 0) {
856 rem = -1;
857 break;
859 if ((c = readline_restart(f, linebuf, linesize, 0)) <= 0) {
860 rem = -1;
861 break;
863 for (cp = *linebuf; fieldnamechar(*cp); ++cp)
865 if (cp > *linebuf)
866 while (blankchar(*cp))
867 ++cp;
868 if (*cp != ':' || cp == *linebuf)
869 continue;
871 /* I guess we got a headline. Handle wraparound */
872 *colon = cp;
873 cp = *linebuf + c;
874 for (;;) {
875 isenc = 0;
876 while (PTRCMP(--cp, >=, *linebuf) && blankchar(*cp))
878 cp++;
879 if (rem <= 0)
880 break;
881 if (PTRCMP(cp - 8, >=, *linebuf) && cp[-1] == '=' && cp[-2] == '?')
882 isenc |= 1;
883 ungetc(c = getc(f), f);
884 if (!blankchar(c))
885 break;
886 c = readline_restart(f, &line2, &line2size, 0);
887 if (c < 0)
888 break;
889 --rem;
890 for (cp2 = line2; blankchar(*cp2); ++cp2)
892 c -= (int)PTR2SIZE(cp2 - line2);
893 if (cp2[0] == '=' && cp2[1] == '?' && c > 8)
894 isenc |= 2;
895 if (PTRCMP(cp + c, >=, *linebuf + *linesize - 2)) {
896 size_t diff = PTR2SIZE(cp - *linebuf),
897 colondiff = PTR2SIZE(*colon - *linebuf);
898 *linebuf = srealloc(*linebuf, *linesize += c + 2);
899 cp = &(*linebuf)[diff];
900 *colon = &(*linebuf)[colondiff];
902 if (isenc != 3)
903 *cp++ = ' ';
904 memcpy(cp, cp2, c);
905 cp += c;
907 *cp = '\0';
909 if (line2 != NULL)
910 free(line2);
911 break;
913 NYD2_LEAVE;
914 return rem;
917 static int
918 msgidnextc(char const **cp, int *status)
920 int c;
921 NYD2_ENTER;
923 assert(cp != NULL);
924 assert(*cp != NULL);
925 assert(status != NULL);
927 for (;;) {
928 if (*status & 01) {
929 if (**cp == '"') {
930 *status &= ~01;
931 (*cp)++;
932 continue;
934 if (**cp == '\\') {
935 (*cp)++;
936 if (**cp == '\0')
937 goto jeof;
939 goto jdfl;
941 switch (**cp) {
942 case '(':
943 *cp = skip_comment(&(*cp)[1]);
944 continue;
945 case '>':
946 case '\0':
947 jeof:
948 c = '\0';
949 goto jleave;
950 case '"':
951 (*cp)++;
952 *status |= 01;
953 continue;
954 case '@':
955 *status |= 02;
956 /*FALLTHRU*/
957 default:
958 jdfl:
959 c = *(*cp)++ & 0377;
960 c = (*status & 02) ? lowerconv(c) : c;
961 goto jleave;
964 jleave:
965 NYD2_LEAVE;
966 return c;
969 static int
970 charcount(char *str, int c)
972 char *cp;
973 int i;
974 NYD2_ENTER;
976 for (i = 0, cp = str; *cp; ++cp)
977 if (*cp == c)
978 ++i;
979 NYD2_LEAVE;
980 return i;
983 static char const *
984 nexttoken(char const *cp)
986 NYD2_ENTER;
987 for (;;) {
988 if (*cp == '\0') {
989 cp = NULL;
990 break;
993 if (*cp == '(') {
994 size_t nesting = 1;
996 do switch (*++cp) {
997 case '(':
998 ++nesting;
999 break;
1000 case ')':
1001 --nesting;
1002 break;
1003 } while (nesting > 0 && *cp != '\0'); /* XXX error? */
1004 } else if (blankchar(*cp) || *cp == ',')
1005 ++cp;
1006 else
1007 break;
1009 NYD2_LEAVE;
1010 return cp;
1013 static char *
1014 a_head_customhdr__sep(char **iolist){
1015 char *cp, c, *base;
1016 bool_t isesc, anyesc;
1017 NYD2_ENTER;
1019 for(base = *iolist; base != NULL; base = *iolist){
1020 while((c = *base) != '\0' && blankspacechar(c))
1021 ++base;
1023 for(isesc = anyesc = FAL0, cp = base;; ++cp){
1024 if(n_UNLIKELY((c = *cp) == '\0')){
1025 *iolist = NULL;
1026 break;
1027 }else if(!isesc){
1028 if(c == ','){
1029 *iolist = cp + 1;
1030 break;
1032 isesc = (c == '\\');
1033 }else{
1034 isesc = FAL0;
1035 anyesc |= (c == ',');
1039 while(cp > base && blankspacechar(cp[-1]))
1040 --cp;
1041 *cp = '\0';
1043 if(*base != '\0'){
1044 if(anyesc){
1045 char *ins;
1047 for(ins = cp = base;; ++ins)
1048 if((c = *cp) == '\\' && cp[1] == ','){
1049 *ins = ',';
1050 cp += 2;
1051 }else if((*ins = (++cp, c)) == '\0')
1052 break;
1054 break;
1057 NYD2_LEAVE;
1058 return base;
1061 FL char const *
1062 myaddrs(struct header *hp) /* TODO */
1064 struct name *np;
1065 char const *rv, *mta;
1066 NYD_ENTER;
1068 if (hp != NULL && (np = hp->h_from) != NULL) {
1069 if ((rv = np->n_fullname) != NULL)
1070 goto jleave;
1071 if ((rv = np->n_name) != NULL)
1072 goto jleave;
1075 if((rv = ok_vlook(from)) != NULL){
1076 if((np = lextract(rv, GEXTRA | GFULL)) == NULL)
1077 jefrom:
1078 n_err(_("An address given in *from* is invalid: %s\n"), rv);
1079 else for(; np != NULL; np = np->n_flink)
1080 if(is_addr_invalid(np, EACM_STRICT | EACM_NOLOG | EACM_NONAME))
1081 goto jefrom;
1082 goto jleave;
1085 /* When invoking *sendmail* directly, it's its task to generate an otherwise
1086 * undeterminable From: address. However, if the user sets *hostname*,
1087 * accept his desire */
1088 if (ok_vlook(hostname) != NULL)
1089 goto jnodename;
1090 if (ok_vlook(smtp) != NULL || /* TODO obsolete -> mta */
1091 /* TODO pretty hacky for now (this entire fun), later: url_creat()! */
1092 ((mta = ok_vlook(mta)) != NULL &&
1093 (mta = n_servbyname(mta, NULL)) != NULL && *mta != '\0'))
1094 goto jnodename;
1095 jleave:
1096 NYD_LEAVE;
1097 return rv;
1099 jnodename:{
1100 char *cp;
1101 char const *hn, *ln;
1102 size_t i;
1104 hn = nodename(1);
1105 ln = ok_vlook(LOGNAME);
1106 i = strlen(ln) + strlen(hn) + 1 +1;
1107 rv = cp = salloc(i);
1108 sstpcpy(sstpcpy(sstpcpy(cp, ln), "@"), hn);
1110 goto jleave;
1113 FL char const *
1114 myorigin(struct header *hp) /* TODO */
1116 char const *rv = NULL, *ccp;
1117 struct name *np;
1118 NYD_ENTER;
1120 if((ccp = myaddrs(hp)) != NULL &&
1121 (np = lextract(ccp, GEXTRA | GFULL)) != NULL){
1122 if(np->n_flink == NULL)
1123 rv = ccp;
1124 else if((ccp = ok_vlook(sender)) != NULL) {
1125 if((np = lextract(ccp, GEXTRA | GFULL)) == NULL ||
1126 np->n_flink != NULL ||
1127 is_addr_invalid(np, EACM_STRICT | EACM_NOLOG | EACM_NONAME))
1128 n_err(_("The address given in *sender* is invalid: %s\n"), ccp);
1129 else
1130 rv = ccp;
1133 NYD_LEAVE;
1134 return rv;
1137 FL bool_t
1138 is_head(char const *linebuf, size_t linelen, bool_t check_rfc4155)
1140 char date[FROM_DATEBUF];
1141 bool_t rv;
1142 NYD2_ENTER;
1144 if ((rv = (linelen >= 5 && !memcmp(linebuf, "From ", 5))) && check_rfc4155 &&
1145 (extract_date_from_from_(linebuf, linelen, date) <= 0 ||
1146 !_is_date(date)))
1147 rv = TRUM1;
1148 NYD2_LEAVE;
1149 return rv;
1152 FL int
1153 extract_date_from_from_(char const *line, size_t linelen,
1154 char datebuf[FROM_DATEBUF])
1156 int rv;
1157 char const *cp = line;
1158 NYD_ENTER;
1160 rv = 1;
1162 /* "From " */
1163 cp = _from__skipword(cp);
1164 if (cp == NULL)
1165 goto jerr;
1166 /* "addr-spec " */
1167 cp = _from__skipword(cp);
1168 if (cp == NULL)
1169 goto jerr;
1170 if (cp[0] == 't' && cp[1] == 't' && cp[2] == 'y') {
1171 cp = _from__skipword(cp);
1172 if (cp == NULL)
1173 goto jerr;
1175 /* It seems there are invalid MBOX archives in the wild, compare
1176 * . http://bugs.debian.org/624111
1177 * . [Mutt] #3868: mutt should error if the imported mailbox is invalid
1178 * What they do is that they obfuscate the address to "name at host",
1179 * and even "name at host dot dom dot dom. I think we should handle that */
1180 else if(cp[0] == 'a' && cp[1] == 't' && cp[2] == ' '){
1181 rv = -1;
1182 cp += 3;
1183 jat_dot:
1184 cp = _from__skipword(cp);
1185 if (cp == NULL)
1186 goto jerr;
1187 if(cp[0] == 'd' && cp[1] == 'o' && cp[2] == 't' && cp[3] == ' '){
1188 cp += 4;
1189 goto jat_dot;
1193 linelen -= PTR2SIZE(cp - line);
1194 if (linelen < _DATE_MINLEN)
1195 goto jerr;
1196 if (cp[linelen - 1] == '\n') {
1197 --linelen;
1198 /* (Rather IMAP/POP3 only) */
1199 if (cp[linelen - 1] == '\r')
1200 --linelen;
1201 if (linelen < _DATE_MINLEN)
1202 goto jerr;
1204 if (linelen >= FROM_DATEBUF)
1205 goto jerr;
1207 jleave:
1208 memcpy(datebuf, cp, linelen);
1209 datebuf[linelen] = '\0';
1210 NYD_LEAVE;
1211 return rv;
1212 jerr:
1213 cp = _("<Unknown date>");
1214 linelen = strlen(cp);
1215 if (linelen >= FROM_DATEBUF)
1216 linelen = FROM_DATEBUF;
1217 rv = 0;
1218 goto jleave;
1221 FL void
1222 extract_header(FILE *fp, struct header *hp, si8_t *checkaddr_err)
1224 /* See the prototype declaration for the hairy relationship of
1225 * n_poption&n_PO_t_FLAG and/or n_psonce&n_PSO_t_FLAG in here */
1226 struct n_header_field **hftail;
1227 struct header nh, *hq = &nh;
1228 char *linebuf = NULL /* TODO line pool */, *colon;
1229 size_t linesize = 0, seenfields = 0;
1230 int lc, c;
1231 char const *val, *cp;
1232 NYD_ENTER;
1234 memset(hq, 0, sizeof *hq);
1235 if ((n_psonce & n_PSO_t_FLAG) && (n_poption & n_PO_t_FLAG)) {
1236 hq->h_to = hp->h_to;
1237 hq->h_cc = hp->h_cc;
1238 hq->h_bcc = hp->h_bcc;
1240 hftail = &hq->h_user_headers;
1242 for (lc = 0; readline_restart(fp, &linebuf, &linesize, 0) > 0; ++lc)
1245 /* TODO yippieia, cat(check(lextract)) :-) */
1246 rewind(fp);
1247 while ((lc = gethfield(fp, &linebuf, &linesize, lc, &colon)) >= 0) {
1248 struct name *np;
1250 /* We explicitly allow EAF_NAME for some addressees since aliases are not
1251 * yet expanded when we parse these! */
1252 if ((val = thisfield(linebuf, "to")) != NULL) {
1253 ++seenfields;
1254 hq->h_to = cat(hq->h_to, checkaddrs(lextract(val, GTO | GFULL),
1255 EACM_NORMAL | EAF_NAME | EAF_MAYKEEP, checkaddr_err));
1256 } else if ((val = thisfield(linebuf, "cc")) != NULL) {
1257 ++seenfields;
1258 hq->h_cc = cat(hq->h_cc, checkaddrs(lextract(val, GCC | GFULL),
1259 EACM_NORMAL | EAF_NAME | EAF_MAYKEEP, checkaddr_err));
1260 } else if ((val = thisfield(linebuf, "bcc")) != NULL) {
1261 ++seenfields;
1262 hq->h_bcc = cat(hq->h_bcc, checkaddrs(lextract(val, GBCC | GFULL),
1263 EACM_NORMAL | EAF_NAME | EAF_MAYKEEP, checkaddr_err));
1264 } else if ((val = thisfield(linebuf, "from")) != NULL) {
1265 if (!(n_psonce & n_PSO_t_FLAG) || (n_poption & n_PO_t_FLAG)) {
1266 ++seenfields;
1267 hq->h_from = cat(hq->h_from,
1268 checkaddrs(lextract(val, GEXTRA | GFULL | GFULLEXTRA),
1269 EACM_STRICT, NULL));
1271 } else if ((val = thisfield(linebuf, "reply-to")) != NULL) {
1272 ++seenfields;
1273 hq->h_replyto = cat(hq->h_replyto,
1274 checkaddrs(lextract(val, GEXTRA | GFULL), EACM_STRICT, NULL));
1275 } else if ((val = thisfield(linebuf, "sender")) != NULL) {
1276 if (!(n_psonce & n_PSO_t_FLAG) || (n_poption & n_PO_t_FLAG)) {
1277 ++seenfields;
1278 hq->h_sender = cat(hq->h_sender, /* TODO cat? check! */
1279 checkaddrs(lextract(val, GEXTRA | GFULL | GFULLEXTRA),
1280 EACM_STRICT, NULL));
1281 } else
1282 goto jebadhead;
1283 } else if ((val = thisfield(linebuf, "subject")) != NULL ||
1284 (val = thisfield(linebuf, "subj")) != NULL) {
1285 ++seenfields;
1286 for (cp = val; blankchar(*cp); ++cp)
1288 hq->h_subject = (hq->h_subject != NULL)
1289 ? save2str(hq->h_subject, cp) : savestr(cp);
1291 /* The remaining are mostly hacked in and thus TODO -- at least in
1292 * TODO respect to their content checking */
1293 else if((val = thisfield(linebuf, "message-id")) != NULL){
1294 if(n_psonce & n_PSO_t_FLAG){
1295 np = checkaddrs(lextract(val, GREF),
1296 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG | EACM_NONAME,
1297 NULL);
1298 if (np == NULL || np->n_flink != NULL)
1299 goto jebadhead;
1300 ++seenfields;
1301 hq->h_message_id = np;
1302 }else
1303 goto jebadhead;
1304 }else if((val = thisfield(linebuf, "in-reply-to")) != NULL){
1305 if(n_psonce & n_PSO_t_FLAG){
1306 np = checkaddrs(lextract(val, GREF),
1307 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG | EACM_NONAME,
1308 NULL);
1309 ++seenfields;
1310 hq->h_in_reply_to = np;
1311 }else
1312 goto jebadhead;
1313 }else if((val = thisfield(linebuf, "references")) != NULL){
1314 if(n_psonce & n_PSO_t_FLAG){
1315 ++seenfields;
1316 /* TODO Limit number of references TODO better on parser side */
1317 hq->h_ref = cat(hq->h_ref, checkaddrs(extract(val, GREF),
1318 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG | EACM_NONAME,
1319 NULL));
1320 }else
1321 goto jebadhead;
1323 /* and that is very hairy */
1324 else if((val = thisfield(linebuf, "mail-followup-to")) != NULL){
1325 if(n_psonce & n_PSO_t_FLAG){
1326 ++seenfields;
1327 hq->h_mft = cat(hq->h_mft, checkaddrs(lextract(val, GEXTRA | GFULL),
1328 /*EACM_STRICT | TODO '/' valid!! | EACM_NOLOG | */EACM_NONAME,
1329 checkaddr_err));
1330 }else
1331 goto jebadhead;
1333 /* A free-form user header; gethfield() did some verification already.. */
1334 else{
1335 struct n_header_field *hfp;
1336 ui32_t nl, bl;
1337 char const *nstart;
1339 for(nstart = cp = linebuf;; ++cp)
1340 if(!fieldnamechar(*cp))
1341 break;
1342 nl = (ui32_t)PTR2SIZE(cp - nstart);
1344 while(blankchar(*cp))
1345 ++cp;
1346 if(*cp++ != ':'){
1347 jebadhead:
1348 n_err(_("Ignoring header field: %s\n"), linebuf);
1349 continue;
1351 while(blankchar(*cp))
1352 ++cp;
1353 bl = (ui32_t)strlen(cp) +1;
1355 ++seenfields;
1356 *hftail = hfp = salloc(n_VSTRUCT_SIZEOF(struct n_header_field, hf_dat
1357 ) + nl +1 + bl);
1358 hftail = &hfp->hf_next;
1359 hfp->hf_next = NULL;
1360 hfp->hf_nl = nl;
1361 hfp->hf_bl = bl - 1;
1362 memcpy(hfp->hf_dat, nstart, nl);
1363 hfp->hf_dat[nl++] = '\0';
1364 memcpy(hfp->hf_dat + nl, cp, bl);
1368 /* In case the blank line after the header has been edited out. Otherwise,
1369 * fetch the header separator */
1370 if (linebuf != NULL) {
1371 if (linebuf[0] != '\0') {
1372 for (cp = linebuf; *(++cp) != '\0';)
1374 fseek(fp, (long)-PTR2SIZE(1 + cp - linebuf), SEEK_CUR);
1375 } else {
1376 if ((c = getc(fp)) != '\n' && c != EOF)
1377 ungetc(c, fp);
1381 if (seenfields > 0 && (checkaddr_err == NULL || *checkaddr_err == 0)) {
1382 hp->h_to = hq->h_to;
1383 hp->h_cc = hq->h_cc;
1384 hp->h_bcc = hq->h_bcc;
1385 hp->h_from = hq->h_from;
1386 hp->h_replyto = hq->h_replyto;
1387 hp->h_sender = hq->h_sender;
1388 if (hq->h_subject != NULL || !(n_psonce & n_PSO_t_FLAG) ||
1389 !(n_poption & n_PO_t_FLAG))
1390 hp->h_subject = hq->h_subject;
1391 hp->h_user_headers = hq->h_user_headers;
1393 if (n_psonce & n_PSO_t_FLAG) {
1394 hp->h_ref = hq->h_ref;
1395 hp->h_message_id = hq->h_message_id;
1396 hp->h_in_reply_to = hq->h_in_reply_to;
1397 hp->h_mft = hq->h_mft;
1399 /* And perform additional validity checks so that we don't bail later
1400 * on TODO this is good and the place where this should occur,
1401 * TODO unfortunately a lot of other places do again and blabla */
1402 if (hp->h_from == NULL)
1403 hp->h_from = n_poption_arg_r;
1404 else if (hp->h_from->n_flink != NULL && hp->h_sender == NULL)
1405 hp->h_sender = lextract(ok_vlook(sender),
1406 GEXTRA | GFULL | GFULLEXTRA);
1408 } else
1409 n_err(_("Restoring deleted header lines\n"));
1411 if (linebuf != NULL)
1412 free(linebuf);
1413 NYD_LEAVE;
1416 FL char *
1417 hfield_mult(char const *field, struct message *mp, int mult)
1419 FILE *ibuf;
1420 int lc;
1421 struct str hfs;
1422 size_t linesize = 0; /* TODO line pool */
1423 char *linebuf = NULL, *colon;
1424 char const *hfield;
1425 NYD_ENTER;
1427 /* There are (spam) messages which have header bytes which are many KB when
1428 * joined, so resize a single heap storage until we are done if we shall
1429 * collect a field that may have multiple bodies; only otherwise use the
1430 * string dope directly */
1431 memset(&hfs, 0, sizeof hfs);
1433 if ((ibuf = setinput(&mb, mp, NEED_HEADER)) == NULL)
1434 goto jleave;
1435 if ((lc = mp->m_lines - 1) < 0)
1436 goto jleave;
1438 if ((mp->m_flag & MNOFROM) == 0 &&
1439 readline_restart(ibuf, &linebuf, &linesize, 0) < 0)
1440 goto jleave;
1441 while (lc > 0) {
1442 if ((lc = gethfield(ibuf, &linebuf, &linesize, lc, &colon)) < 0)
1443 break;
1444 if ((hfield = thisfield(linebuf, field)) != NULL && *hfield != '\0') {
1445 if (mult)
1446 n_str_add_buf(&hfs, hfield, strlen(hfield));
1447 else {
1448 hfs.s = savestr(hfield);
1449 break;
1454 jleave:
1455 if (linebuf != NULL)
1456 free(linebuf);
1457 if (mult && hfs.s != NULL) {
1458 colon = savestrbuf(hfs.s, hfs.l);
1459 free(hfs.s);
1460 hfs.s = colon;
1462 NYD_LEAVE;
1463 return hfs.s;
1466 FL char const *
1467 thisfield(char const *linebuf, char const *field)
1469 char const *rv = NULL;
1470 NYD2_ENTER;
1472 while (lowerconv(*linebuf) == lowerconv(*field)) {
1473 ++linebuf;
1474 ++field;
1476 if (*field != '\0')
1477 goto jleave;
1479 while (blankchar(*linebuf))
1480 ++linebuf;
1481 if (*linebuf++ != ':')
1482 goto jleave;
1484 while (blankchar(*linebuf)) /* TODO header parser.. strip trailing WS?!? */
1485 ++linebuf;
1486 rv = linebuf;
1487 jleave:
1488 NYD2_LEAVE;
1489 return rv;
1492 FL char *
1493 nameof(struct message *mp, int reptype)
1495 char *cp, *cp2;
1496 NYD_ENTER;
1498 cp = skin(name1(mp, reptype));
1499 if (reptype != 0 || charcount(cp, '!') < 2)
1500 goto jleave;
1501 cp2 = strrchr(cp, '!');
1502 --cp2;
1503 while (cp2 > cp && *cp2 != '!')
1504 --cp2;
1505 if (*cp2 == '!')
1506 cp = cp2 + 1;
1507 jleave:
1508 NYD_LEAVE;
1509 return cp;
1512 FL char const *
1513 skip_comment(char const *cp)
1515 size_t nesting;
1516 NYD_ENTER;
1518 for (nesting = 1; nesting > 0 && *cp; ++cp) {
1519 switch (*cp) {
1520 case '\\':
1521 if (cp[1])
1522 ++cp;
1523 break;
1524 case '(':
1525 ++nesting;
1526 break;
1527 case ')':
1528 --nesting;
1529 break;
1532 NYD_LEAVE;
1533 return cp;
1536 FL char const *
1537 routeaddr(char const *name)
1539 char const *np, *rp = NULL;
1540 NYD_ENTER;
1542 for (np = name; *np; np++) {
1543 switch (*np) {
1544 case '(':
1545 np = skip_comment(np + 1) - 1;
1546 break;
1547 case '"':
1548 while (*np) {
1549 if (*++np == '"')
1550 break;
1551 if (*np == '\\' && np[1])
1552 np++;
1554 break;
1555 case '<':
1556 rp = np;
1557 break;
1558 case '>':
1559 goto jleave;
1562 rp = NULL;
1563 jleave:
1564 NYD_LEAVE;
1565 return rp;
1568 FL enum expand_addr_flags
1569 expandaddr_to_eaf(void)
1571 struct eafdesc {
1572 char const *eafd_name;
1573 bool_t eafd_is_target;
1574 ui8_t eafd_andoff;
1575 ui8_t eafd_or;
1576 } const eafa[] = {
1577 {"restrict", FAL0, EAF_TARGET_MASK, EAF_RESTRICT | EAF_RESTRICT_TARGETS},
1578 {"fail", FAL0, EAF_NONE, EAF_FAIL},
1579 {"failinvaddr", FAL0, EAF_NONE, EAF_FAILINVADDR | EAF_ADDR},
1580 {"all", TRU1, EAF_NONE, EAF_TARGET_MASK},
1581 {"file", TRU1, EAF_NONE, EAF_FILE},
1582 {"pipe", TRU1, EAF_NONE, EAF_PIPE},
1583 {"name", TRU1, EAF_NONE, EAF_NAME},
1584 {"addr", TRU1, EAF_NONE, EAF_ADDR}
1585 }, *eafp;
1587 char *buf;
1588 enum expand_addr_flags rv;
1589 char const *cp;
1590 NYD2_ENTER;
1592 if ((cp = ok_vlook(expandaddr)) == NULL)
1593 rv = EAF_RESTRICT_TARGETS;
1594 else if (*cp == '\0')
1595 rv = EAF_TARGET_MASK;
1596 else {
1597 rv = EAF_TARGET_MASK;
1599 for (buf = savestr(cp); (cp = n_strsep(&buf, ',', TRU1)) != NULL;) {
1600 bool_t minus;
1602 if ((minus = (*cp == '-')) || *cp == '+')
1603 ++cp;
1604 for (eafp = eafa;; ++eafp) {
1605 if (eafp == eafa + n_NELEM(eafa)) {
1606 if (n_poption & n_PO_D_V)
1607 n_err(_("Unknown *expandaddr* value: %s\n"), cp);
1608 break;
1609 } else if (!asccasecmp(cp, eafp->eafd_name)) {
1610 if (!minus) {
1611 rv &= ~eafp->eafd_andoff;
1612 rv |= eafp->eafd_or;
1613 } else {
1614 if (eafp->eafd_is_target)
1615 rv &= ~eafp->eafd_or;
1616 else if (n_poption & n_PO_D_V)
1617 n_err(_("minus - prefix invalid for *expandaddr* value: "
1618 "%s\n"), --cp);
1620 break;
1621 } else if (!asccasecmp(cp, "noalias")) { /* TODO v15 OBSOLETE */
1622 n_OBSOLETE(_("*expandaddr*: noalias is henceforth -name"));
1623 rv &= ~EAF_NAME;
1624 break;
1629 if((rv & EAF_RESTRICT) && ((n_psonce & n_PSO_INTERACTIVE) ||
1630 (n_poption & n_PO_TILDE_FLAG)))
1631 rv |= EAF_TARGET_MASK;
1632 else if(n_poption & n_PO_D_V){
1633 if(!(rv & EAF_TARGET_MASK))
1634 n_err(_("*expandaddr* doesn't allow any addressees\n"));
1635 else if((rv & EAF_FAIL) && (rv & EAF_TARGET_MASK) == EAF_TARGET_MASK)
1636 n_err(_("*expandaddr* with fail, but no restrictions to apply\n"));
1639 NYD2_LEAVE;
1640 return rv;
1643 FL si8_t
1644 is_addr_invalid(struct name *np, enum expand_addr_check_mode eacm)
1646 char cbuf[sizeof "'\\U12340'"];
1647 char const *cs;
1648 int f;
1649 si8_t rv;
1650 enum expand_addr_flags eaf;
1651 NYD_ENTER;
1653 eaf = expandaddr_to_eaf();
1654 f = np->n_flags;
1656 if ((rv = ((f & NAME_ADDRSPEC_INVALID) != 0))) {
1657 if (eaf & EAF_FAILINVADDR)
1658 rv = -rv;
1660 if ((eacm & EACM_NOLOG) || (f & NAME_ADDRSPEC_ERR_EMPTY)) {
1662 } else {
1663 ui32_t c;
1664 char const *fmt = "'\\x%02X'";
1665 bool_t ok8bit = TRU1;
1667 if (f & NAME_ADDRSPEC_ERR_IDNA) {
1668 cs = _("Invalid domain name: %s, character %s\n");
1669 fmt = "'\\U%04X'";
1670 ok8bit = FAL0;
1671 } else if (f & NAME_ADDRSPEC_ERR_ATSEQ)
1672 cs = _("%s contains invalid %s sequence\n");
1673 else if (f & NAME_ADDRSPEC_ERR_NAME) {
1674 cs = _("%s is an invalid alias name\n");
1675 } else
1676 cs = _("%s contains invalid byte %s\n");
1678 c = NAME_ADDRSPEC_ERR_GETWC(f);
1679 snprintf(cbuf, sizeof cbuf,
1680 (ok8bit && c >= 040 && c <= 0177 ? "'%c'" : fmt), c);
1681 goto jprint;
1683 goto jleave;
1686 /* *expandaddr* stuff */
1687 if (!(rv = ((eacm & EACM_MODE_MASK) != EACM_NONE)))
1688 goto jleave;
1690 if ((eacm & EACM_STRICT) && (f & NAME_ADDRSPEC_ISFILEORPIPE)) {
1691 if (eaf & EAF_FAIL)
1692 rv = -rv;
1693 cs = _("%s%s: file or pipe addressees not allowed here\n");
1694 if (eacm & EACM_NOLOG)
1695 goto jleave;
1696 else
1697 goto j0print;
1700 eaf |= (eacm & EAF_TARGET_MASK);
1701 if (eacm & EACM_NONAME)
1702 eaf &= ~EAF_NAME;
1704 if (eaf == EAF_NONE) {
1705 rv = FAL0;
1706 goto jleave;
1708 if (eaf & EAF_FAIL)
1709 rv = -rv;
1711 if (!(eaf & EAF_FILE) && (f & NAME_ADDRSPEC_ISFILE)) {
1712 cs = _("%s%s: *expandaddr* doesn't allow file target\n");
1713 if (eacm & EACM_NOLOG)
1714 goto jleave;
1715 } else if (!(eaf & EAF_PIPE) && (f & NAME_ADDRSPEC_ISPIPE)) {
1716 cs = _("%s%s: *expandaddr* doesn't allow command pipe target\n");
1717 if (eacm & EACM_NOLOG)
1718 goto jleave;
1719 } else if (!(eaf & EAF_NAME) && (f & NAME_ADDRSPEC_ISNAME)) {
1720 cs = _("%s%s: *expandaddr* doesn't allow user name target\n");
1721 if (eacm & EACM_NOLOG)
1722 goto jleave;
1723 } else if (!(eaf & EAF_ADDR) && (f & NAME_ADDRSPEC_ISADDR)) {
1724 cs = _("%s%s: *expandaddr* doesn't allow mail address target\n");
1725 if (eacm & EACM_NOLOG)
1726 goto jleave;
1727 } else {
1728 rv = FAL0;
1729 goto jleave;
1732 j0print:
1733 cbuf[0] = '\0';
1734 jprint:
1735 n_err(cs, n_shexp_quote_cp(np->n_name, TRU1), cbuf);
1736 jleave:
1737 NYD_LEAVE;
1738 return rv;
1741 FL char *
1742 skin(char const *name)
1744 struct n_addrguts ag;
1745 char *rv;
1746 NYD_ENTER;
1748 if(name != NULL){
1749 name = n_addrspec_with_guts(&ag,name, TRU1);
1750 rv = ag.ag_skinned;
1751 if(!(ag.ag_n_flags & NAME_NAME_SALLOC))
1752 rv = savestrbuf(rv, ag.ag_slen);
1753 }else
1754 rv = NULL;
1755 NYD_LEAVE;
1756 return rv;
1759 /* TODO addrspec_with_guts: RFC 5322
1760 * TODO addrspec_with_guts: trim whitespace ETC. ETC. ETC.!!! */
1761 FL char const *
1762 n_addrspec_with_guts(struct n_addrguts *agp, char const *name, bool_t doskin){
1763 char const *cp;
1764 char *cp2, *bufend, *nbuf, c;
1765 enum{
1766 a_NONE,
1767 a_GOTLT = 1<<0,
1768 a_GOTADDR = 1<<1,
1769 a_GOTSPACE = 1<<2,
1770 a_LASTSP = 1<<3
1771 } flags;
1772 NYD_ENTER;
1774 memset(agp, 0, sizeof *agp);
1776 if((agp->ag_input = name) == NULL || (agp->ag_ilen = strlen(name)) == 0){
1777 agp->ag_skinned = n_UNCONST(n_empty); /* ok: NAME_SALLOC is not set */
1778 agp->ag_slen = 0;
1779 agp->ag_n_flags |= NAME_ADDRSPEC_CHECKED;
1780 NAME_ADDRSPEC_ERR_SET(agp->ag_n_flags, NAME_ADDRSPEC_ERR_EMPTY, 0);
1781 goto jleave;
1782 }else if(!doskin){
1783 /*agp->ag_iaddr_start = 0;*/
1784 agp->ag_iaddr_aend = agp->ag_ilen;
1785 agp->ag_skinned = n_UNCONST(name); /* (NAME_SALLOC not set) */
1786 agp->ag_slen = agp->ag_ilen;
1787 agp->ag_n_flags = NAME_SKINNED;
1788 goto jcheck;
1791 flags = a_NONE;
1792 nbuf = n_lofi_alloc(agp->ag_ilen +1);
1793 /*agp->ag_iaddr_start = 0;*/
1794 cp2 = bufend = nbuf;
1796 /* TODO This is complete crap and should use a token parser */
1797 for(cp = name++; (c = *cp++) != '\0';){
1798 switch (c) {
1799 case '(':
1800 cp = skip_comment(cp);
1801 flags &= ~a_LASTSP;
1802 break;
1803 case '"':
1804 /* Start of a "quoted-string". Copy it in its entirety */
1805 /* XXX RFC: quotes are "semantically invisible"
1806 * XXX But it was explicitly added (Changelog.Heirloom,
1807 * XXX [9.23] released 11/15/00, "Do not remove quotes
1808 * XXX when skinning names"? No more info.. */
1809 *cp2++ = c;
1810 while ((c = *cp) != '\0') { /* TODO improve */
1811 ++cp;
1812 if (c == '"') {
1813 *cp2++ = c;
1814 break;
1816 if (c != '\\')
1817 *cp2++ = c;
1818 else if ((c = *cp) != '\0') {
1819 *cp2++ = c;
1820 ++cp;
1823 flags &= ~a_LASTSP;
1824 break;
1825 case ' ':
1826 case '\t':
1827 if((flags & (a_GOTADDR | a_GOTSPACE)) == a_GOTADDR){
1828 flags |= a_GOTSPACE;
1829 agp->ag_iaddr_aend = PTR2SIZE(cp - name);
1831 if (cp[0] == 'a' && cp[1] == 't' && blankchar(cp[2]))
1832 cp += 3, *cp2++ = '@';
1833 else if (cp[0] == '@' && blankchar(cp[1]))
1834 cp += 2, *cp2++ = '@';
1835 else
1836 flags |= a_LASTSP;
1837 break;
1838 case '<':
1839 agp->ag_iaddr_start = PTR2SIZE(cp - (name - 1));
1840 cp2 = bufend;
1841 flags &= ~(a_GOTSPACE | a_LASTSP);
1842 flags |= a_GOTLT | a_GOTADDR;
1843 break;
1844 case '>':
1845 if(flags & a_GOTLT){
1846 /* (_addrspec_check() verifies these later!) */
1847 flags &= ~(a_GOTLT | a_LASTSP);
1848 agp->ag_iaddr_aend = PTR2SIZE(cp - name);
1850 /* Skip over the entire remaining field */
1851 while((c = *cp) != '\0' && c != ','){
1852 ++cp;
1853 if (c == '(')
1854 cp = skip_comment(cp);
1855 else if (c == '"')
1856 while ((c = *cp) != '\0') {
1857 ++cp;
1858 if (c == '"')
1859 break;
1860 if (c == '\\' && *cp != '\0')
1861 ++cp;
1864 break;
1866 /* FALLTHRU */
1867 default:
1868 if(flags & a_LASTSP){
1869 flags &= ~a_LASTSP;
1870 if(flags & a_GOTADDR)
1871 *cp2++ = ' ';
1873 *cp2++ = c;
1874 /* This character is forbidden here, but it may nonetheless be
1875 * present: ensure we turn this into something valid! (E.g., if the
1876 * next character would be a "..) */
1877 if(c == '\\' && *cp != '\0')
1878 *cp2++ = *cp++;
1879 if(c == ','){
1880 if(!(flags & a_GOTLT)){
1881 *cp2++ = ' ';
1882 for(; blankchar(*cp); ++cp)
1884 flags &= ~a_LASTSP;
1885 bufend = cp2;
1887 }else if(!(flags & a_GOTADDR)){
1888 flags |= a_GOTADDR;
1889 agp->ag_iaddr_start = PTR2SIZE(cp - name);
1893 --name;
1894 agp->ag_slen = PTR2SIZE(cp2 - nbuf);
1895 if (agp->ag_iaddr_aend == 0)
1896 agp->ag_iaddr_aend = agp->ag_ilen;
1897 /* Misses > */
1898 else if (agp->ag_iaddr_aend < agp->ag_iaddr_start) {
1899 cp2 = n_autorec_alloc(NULL, agp->ag_ilen + 1 +1);
1900 memcpy(cp2, agp->ag_input, agp->ag_ilen);
1901 agp->ag_iaddr_aend = agp->ag_ilen;
1902 cp2[agp->ag_ilen++] = '>';
1903 cp2[agp->ag_ilen] = '\0';
1905 agp->ag_skinned = savestrbuf(nbuf, agp->ag_slen);
1906 n_lofi_free(nbuf);
1907 agp->ag_n_flags = NAME_NAME_SALLOC | NAME_SKINNED;
1908 jcheck:
1909 if(a_head_addrspec_check(agp, doskin) <= FAL0)
1910 name = NULL;
1911 else
1912 name = agp->ag_input;
1913 jleave:
1914 NYD_LEAVE;
1915 return name;
1918 FL char *
1919 realname(char const *name)
1921 char const *cp, *cq, *cstart = NULL, *cend = NULL;
1922 char *rname, *rp;
1923 struct str in, out;
1924 int quoted, good, nogood;
1925 NYD_ENTER;
1927 if ((cp = n_UNCONST(name)) == NULL)
1928 goto jleave;
1929 for (; *cp != '\0'; ++cp) {
1930 switch (*cp) {
1931 case '(':
1932 if (cstart != NULL) {
1933 /* More than one comment in address, doesn't make sense to display
1934 * it without context. Return the entire field */
1935 cp = mime_fromaddr(name);
1936 goto jleave;
1938 cstart = cp++;
1939 cp = skip_comment(cp);
1940 cend = cp--;
1941 if (cend <= cstart)
1942 cend = cstart = NULL;
1943 break;
1944 case '"':
1945 while (*cp) {
1946 if (*++cp == '"')
1947 break;
1948 if (*cp == '\\' && cp[1])
1949 ++cp;
1951 break;
1952 case '<':
1953 if (cp > name) {
1954 cstart = name;
1955 cend = cp;
1957 break;
1958 case ',':
1959 /* More than one address. Just use the first one */
1960 goto jbrk;
1964 jbrk:
1965 if (cstart == NULL) {
1966 if (*name == '<') {
1967 /* If name contains only a route-addr, the surrounding angle brackets
1968 * don't serve any useful purpose when displaying, so remove */
1969 cp = prstr(skin(name));
1970 } else
1971 cp = mime_fromaddr(name);
1972 goto jleave;
1975 /* Strip quotes. Note that quotes that appear within a MIME encoded word are
1976 * not stripped. The idea is to strip only syntactical relevant things (but
1977 * this is not necessarily the most sensible way in practice) */
1978 rp = rname = ac_alloc(PTR2SIZE(cend - cstart +1));
1979 quoted = 0;
1980 for (cp = cstart; cp < cend; ++cp) {
1981 if (*cp == '(' && !quoted) {
1982 cq = skip_comment(++cp);
1983 if (PTRCMP(--cq, >, cend))
1984 cq = cend;
1985 while (cp < cq) {
1986 if (*cp == '\\' && PTRCMP(cp + 1, <, cq))
1987 ++cp;
1988 *rp++ = *cp++;
1990 } else if (*cp == '\\' && PTRCMP(cp + 1, <, cend))
1991 *rp++ = *++cp;
1992 else if (*cp == '"') {
1993 quoted = !quoted;
1994 continue;
1995 } else
1996 *rp++ = *cp;
1998 *rp = '\0';
1999 in.s = rname;
2000 in.l = rp - rname;
2001 mime_fromhdr(&in, &out, TD_ISPR | TD_ICONV);
2002 ac_free(rname);
2003 rname = savestr(out.s);
2004 free(out.s);
2006 while (blankchar(*rname))
2007 ++rname;
2008 for (rp = rname; *rp != '\0'; ++rp)
2010 while (PTRCMP(--rp, >=, rname) && blankchar(*rp))
2011 *rp = '\0';
2012 if (rp == rname) {
2013 cp = mime_fromaddr(name);
2014 goto jleave;
2017 /* mime_fromhdr() has converted all nonprintable characters to question
2018 * marks now. These and blanks are considered uninteresting; if the
2019 * displayed part of the real name contains more than 25% of them, it is
2020 * probably better to display the plain email address instead */
2021 good = 0;
2022 nogood = 0;
2023 for (rp = rname; *rp != '\0' && PTRCMP(rp, <, rname + 20); ++rp)
2024 if (*rp == '?' || blankchar(*rp))
2025 ++nogood;
2026 else
2027 ++good;
2028 cp = (good * 3 < nogood) ? prstr(skin(name)) : rname;
2029 jleave:
2030 NYD_LEAVE;
2031 return n_UNCONST(cp);
2034 FL char *
2035 name1(struct message *mp, int reptype)
2037 char *namebuf, *cp, *cp2, *linebuf = NULL /* TODO line pool */;
2038 size_t namesize, linesize = 0;
2039 FILE *ibuf;
2040 int f1st = 1;
2041 NYD_ENTER;
2043 if ((cp = hfield1("from", mp)) != NULL && *cp != '\0')
2044 goto jleave;
2045 if (reptype == 0 && (cp = hfield1("sender", mp)) != NULL && *cp != '\0')
2046 goto jleave;
2048 namebuf = smalloc(namesize = 1);
2049 namebuf[0] = 0;
2050 if (mp->m_flag & MNOFROM)
2051 goto jout;
2052 if ((ibuf = setinput(&mb, mp, NEED_HEADER)) == NULL)
2053 goto jout;
2054 if (readline_restart(ibuf, &linebuf, &linesize, 0) < 0)
2055 goto jout;
2057 jnewname:
2058 if (namesize <= linesize)
2059 namebuf = srealloc(namebuf, namesize = linesize +1);
2060 for (cp = linebuf; *cp != '\0' && *cp != ' '; ++cp)
2062 for (; blankchar(*cp); ++cp)
2064 for (cp2 = namebuf + strlen(namebuf);
2065 *cp && !blankchar(*cp) && PTRCMP(cp2, <, namebuf + namesize -1);)
2066 *cp2++ = *cp++;
2067 *cp2 = '\0';
2069 if (readline_restart(ibuf, &linebuf, &linesize, 0) < 0)
2070 goto jout;
2071 if ((cp = strchr(linebuf, 'F')) == NULL)
2072 goto jout;
2073 if (strncmp(cp, "From", 4))
2074 goto jout;
2075 if (namesize <= linesize)
2076 namebuf = srealloc(namebuf, namesize = linesize + 1);
2078 while ((cp = strchr(cp, 'r')) != NULL) {
2079 if (!strncmp(cp, "remote", 6)) {
2080 if ((cp = strchr(cp, 'f')) == NULL)
2081 break;
2082 if (strncmp(cp, "from", 4) != 0)
2083 break;
2084 if ((cp = strchr(cp, ' ')) == NULL)
2085 break;
2086 cp++;
2087 if (f1st) {
2088 strncpy(namebuf, cp, namesize);
2089 f1st = 0;
2090 } else {
2091 cp2 = strrchr(namebuf, '!') + 1;
2092 strncpy(cp2, cp, PTR2SIZE(namebuf + namesize - cp2));
2094 namebuf[namesize - 2] = '!';
2095 namebuf[namesize - 1] = '\0';
2096 goto jnewname;
2098 cp++;
2100 jout:
2101 if (*namebuf != '\0' || ((cp = hfield1("return-path", mp))) == NULL ||
2102 *cp == '\0')
2103 cp = savestr(namebuf);
2105 if (linebuf != NULL)
2106 free(linebuf);
2107 free(namebuf);
2108 jleave:
2109 NYD_LEAVE;
2110 return cp;
2113 FL char const *
2114 subject_re_trim(char const *s){
2115 struct{
2116 ui8_t len;
2117 char dat[7];
2118 }const *pp, ignored[] = { /* Update *reply-strings* manual upon change! */
2119 {3, "re:"},
2120 {3, "aw:"}, {5, "antw:"}, /* de */
2121 {3, "wg:"}, /* Seen too often in the wild */
2122 {0, ""}
2125 bool_t any;
2126 char *re_st, *re_st_x;
2127 char const *orig_s;
2128 size_t re_l;
2129 NYD_ENTER;
2131 any = FAL0;
2132 orig_s = s;
2133 re_st = NULL;
2134 n_UNINIT(re_l, 0);
2136 if((re_st_x = ok_vlook(reply_strings)) != NULL &&
2137 (re_l = strlen(re_st_x)) > 0){
2138 re_st = n_lofi_alloc(++re_l * 2);
2139 memcpy(re_st, re_st_x, re_l);
2142 jouter:
2143 while(*s != '\0'){
2144 while(spacechar(*s))
2145 ++s;
2147 for(pp = ignored; pp->len > 0; ++pp)
2148 if(is_asccaseprefix(pp->dat, s)){
2149 s += pp->len;
2150 any = TRU1;
2151 goto jouter;
2154 if(re_st != NULL){
2155 char *cp;
2157 memcpy(re_st_x = &re_st[re_l], re_st, re_l);
2158 while((cp = n_strsep(&re_st_x, ',', TRU1)) != NULL)
2159 if(is_asccaseprefix(cp, s)){
2160 s += strlen(cp);
2161 any = TRU1;
2162 goto jouter;
2165 break;
2168 if(re_st != NULL)
2169 n_lofi_free(re_st);
2170 NYD_LEAVE;
2171 return any ? s : orig_s;
2174 FL int
2175 msgidcmp(char const *s1, char const *s2)
2177 int q1 = 0, q2 = 0, c1, c2;
2178 NYD_ENTER;
2180 while(*s1 == '<')
2181 ++s1;
2182 while(*s2 == '<')
2183 ++s2;
2185 do {
2186 c1 = msgidnextc(&s1, &q1);
2187 c2 = msgidnextc(&s2, &q2);
2188 if (c1 != c2)
2189 break;
2190 } while (c1 && c2);
2191 NYD_LEAVE;
2192 return c1 - c2;
2195 FL char const *
2196 fakefrom(struct message *mp)
2198 char const *name;
2199 NYD_ENTER;
2201 if (((name = skin(hfield1("return-path", mp))) == NULL || *name == '\0' ) &&
2202 ((name = skin(hfield1("from", mp))) == NULL || *name == '\0'))
2203 /* XXX MAILER-DAEMON is what an old MBOX manual page says.
2204 * RFC 4155 however requires a RFC 5322 (2822) conforming
2205 * "addr-spec", but we simply can't provide that */
2206 name = "MAILER-DAEMON";
2207 NYD_LEAVE;
2208 return name;
2211 FL char const *
2212 fakedate(time_t t)
2214 char *cp, *cq;
2215 NYD_ENTER;
2217 cp = ctime(&t);
2218 for (cq = cp; *cq != '\0' && *cq != '\n'; ++cq)
2220 *cq = '\0';
2221 cp = savestr(cp);
2222 NYD_LEAVE;
2223 return cp;
2226 #ifdef HAVE_IMAP_SEARCH
2227 FL time_t
2228 unixtime(char const *fromline)
2230 char const *fp, *xp;
2231 time_t t;
2232 si32_t i, year, month, day, hour, minute, second, tzdiff;
2233 struct tm *tmptr;
2234 NYD2_ENTER;
2236 for (fp = fromline; *fp != '\0' && *fp != '\n'; ++fp)
2238 fp -= 24;
2239 if (PTR2SIZE(fp - fromline) < 7)
2240 goto jinvalid;
2241 if (fp[3] != ' ')
2242 goto jinvalid;
2243 for (i = 0;;) {
2244 if (!strncmp(fp + 4, n_month_names[i], 3))
2245 break;
2246 if (n_month_names[++i][0] == '\0')
2247 goto jinvalid;
2249 month = i + 1;
2250 if (fp[7] != ' ')
2251 goto jinvalid;
2252 n_idec_si32_cp(&day, &fp[8], 10, &xp);
2253 if (*xp != ' ' || xp != fp + 10)
2254 goto jinvalid;
2255 n_idec_si32_cp(&hour, &fp[11], 10, &xp);
2256 if (*xp != ':' || xp != fp + 13)
2257 goto jinvalid;
2258 n_idec_si32_cp(&minute, &fp[14], 10, &xp);
2259 if (*xp != ':' || xp != fp + 16)
2260 goto jinvalid;
2261 n_idec_si32_cp(&second, &fp[17], 10, &xp);
2262 if (*xp != ' ' || xp != fp + 19)
2263 goto jinvalid;
2264 n_idec_si32_cp(&year, &fp[20], 10, &xp);
2265 if (xp != fp + 24)
2266 goto jinvalid;
2267 if ((t = combinetime(year, month, day, hour, minute, second)) == (time_t)-1)
2268 goto jinvalid;
2269 tzdiff = t - mktime(gmtime(&t));
2270 tmptr = localtime(&t);
2271 if (tmptr->tm_isdst > 0)
2272 tzdiff += 3600;
2273 t -= tzdiff;
2274 jleave:
2275 NYD2_LEAVE;
2276 return t;
2277 jinvalid:
2278 t = n_time_epoch();
2279 goto jleave;
2281 #endif /* HAVE_IMAP_SEARCH */
2283 FL time_t
2284 rfctime(char const *date) /* TODO n_idec_ return tests */
2286 char const *cp, *x;
2287 time_t t;
2288 si32_t i, year, month, day, hour, minute, second;
2289 NYD2_ENTER;
2291 cp = date;
2293 if ((cp = nexttoken(cp)) == NULL)
2294 goto jinvalid;
2295 if (alphachar(cp[0]) && alphachar(cp[1]) && alphachar(cp[2]) &&
2296 cp[3] == ',') {
2297 if ((cp = nexttoken(&cp[4])) == NULL)
2298 goto jinvalid;
2300 n_idec_si32_cp(&day, cp, 10, &x);
2301 if ((cp = nexttoken(x)) == NULL)
2302 goto jinvalid;
2303 for (i = 0;;) {
2304 if (!strncmp(cp, n_month_names[i], 3))
2305 break;
2306 if (n_month_names[++i][0] == '\0')
2307 goto jinvalid;
2309 month = i + 1;
2310 if ((cp = nexttoken(&cp[3])) == NULL)
2311 goto jinvalid;
2312 /* RFC 5322, 4.3:
2313 * Where a two or three digit year occurs in a date, the year is to be
2314 * interpreted as follows: If a two digit year is encountered whose
2315 * value is between 00 and 49, the year is interpreted by adding 2000,
2316 * ending up with a value between 2000 and 2049. If a two digit year
2317 * is encountered with a value between 50 and 99, or any three digit
2318 * year is encountered, the year is interpreted by adding 1900 */
2319 n_idec_si32_cp(&year, cp, 10, &x);
2320 i = (int)PTR2SIZE(x - cp);
2321 if (i == 2 && year >= 0 && year <= 49)
2322 year += 2000;
2323 else if (i == 3 || (i == 2 && year >= 50 && year <= 99))
2324 year += 1900;
2325 if ((cp = nexttoken(x)) == NULL)
2326 goto jinvalid;
2327 n_idec_si32_cp(&hour, cp, 10, &x);
2328 if (*x != ':')
2329 goto jinvalid;
2330 cp = &x[1];
2331 n_idec_si32_cp(&minute, cp, 10, &x);
2332 if (*x == ':') {
2333 cp = &x[1];
2334 n_idec_si32_cp(&second, cp, 10, &x);
2335 } else
2336 second = 0;
2338 if ((t = combinetime(year, month, day, hour, minute, second)) == (time_t)-1)
2339 goto jinvalid;
2340 if ((cp = nexttoken(x)) != NULL) {
2341 char buf[3];
2342 int sign = 1;
2344 switch (*cp) {
2345 case '+':
2346 sign = -1;
2347 /* FALLTHRU */
2348 case '-':
2349 ++cp;
2350 break;
2352 if (digitchar(cp[0]) && digitchar(cp[1]) && digitchar(cp[2]) &&
2353 digitchar(cp[3])) {
2354 si64_t tadj;
2356 buf[2] = '\0';
2357 buf[0] = cp[0];
2358 buf[1] = cp[1];
2359 n_idec_si32_cp(&i, buf, 10, NULL);
2360 tadj = (si64_t)i * 3600; /* XXX */
2361 buf[0] = cp[2];
2362 buf[1] = cp[3];
2363 n_idec_si32_cp(&i, buf, 10, NULL);
2364 tadj += (si64_t)i * 60; /* XXX */
2365 if (sign < 0)
2366 tadj = -tadj;
2367 t += (time_t)tadj;
2369 /* TODO WE DO NOT YET PARSE (OBSOLETE) ZONE NAMES
2370 * TODO once again, Christos Zoulas and NetBSD Mail have done
2371 * TODO a really good job already, but using strptime(3), which
2372 * TODO is not portable. Nonetheless, WE must improve, not
2373 * TODO at last because we simply ignore obsolete timezones!!
2374 * TODO See RFC 5322, 4.3! */
2376 jleave:
2377 NYD2_LEAVE;
2378 return t;
2379 jinvalid:
2380 t = 0;
2381 goto jleave;
2384 FL time_t
2385 combinetime(int year, int month, int day, int hour, int minute, int second){
2386 size_t const jdn_epoch = 2440588;
2387 bool_t const y2038p = (sizeof(time_t) == 4);
2389 size_t jdn;
2390 time_t t;
2391 NYD2_ENTER;
2393 if(UICMP(32, second, >/*XXX leap=*/, DATE_SECSMIN) ||
2394 UICMP(32, minute, >=, DATE_MINSHOUR) ||
2395 UICMP(32, hour, >=, DATE_HOURSDAY) ||
2396 day < 1 || day > 31 ||
2397 month < 1 || month > 12 ||
2398 year < 1970)
2399 goto jerr;
2401 if(year >= 1970 + ((y2038p ? SI32_MAX : SI64_MAX) /
2402 (DATE_SECSDAY * DATE_DAYSYEAR))){
2403 /* Be a coward regarding Y2038, many people (mostly myself, that is) do
2404 * test by stepping second-wise around the flip. Don't care otherwise */
2405 if(!y2038p)
2406 goto jerr;
2407 if(year > 2038 || month > 1 || day > 19 ||
2408 hour > 3 || minute > 14 || second > 7)
2409 goto jerr;
2412 t = second;
2413 t += minute * DATE_SECSMIN;
2414 t += hour * DATE_SECSHOUR;
2416 jdn = a_head_gregorian_to_jdn(year, month, day);
2417 jdn -= jdn_epoch;
2418 t += (time_t)jdn * DATE_SECSDAY;
2419 jleave:
2420 NYD2_LEAVE;
2421 return t;
2422 jerr:
2423 t = (time_t)-1;
2424 goto jleave;
2427 FL void
2428 substdate(struct message *m)
2430 char const *cp;
2431 NYD_ENTER;
2433 /* Determine the date to print in faked 'From ' lines. This is traditionally
2434 * the date the message was written to the mail file. Try to determine this
2435 * using RFC message header fields, or fall back to current time */
2436 if ((cp = hfield1("received", m)) != NULL) {
2437 while ((cp = nexttoken(cp)) != NULL && *cp != ';') {
2439 ++cp;
2440 while (alnumchar(*cp));
2442 if (cp && *++cp)
2443 m->m_time = rfctime(cp);
2445 if (m->m_time == 0 || m->m_time > time_current.tc_time) {
2446 if ((cp = hfield1("date", m)) != NULL)
2447 m->m_time = rfctime(cp);
2449 if (m->m_time == 0 || m->m_time > time_current.tc_time)
2450 m->m_time = time_current.tc_time;
2451 NYD_LEAVE;
2454 FL void
2455 setup_from_and_sender(struct header *hp)
2457 char const *addr;
2458 struct name *np;
2459 NYD_ENTER;
2461 /* If -t parsed or composed From: then take it. With -t we otherwise
2462 * want -r to be honoured in favour of *from* in order to have
2463 * a behaviour that is compatible with what users would expect from e.g.
2464 * postfix(1) */
2465 if ((np = hp->h_from) != NULL ||
2466 ((n_psonce & n_PSO_t_FLAG) && (np = n_poption_arg_r) != NULL)) {
2468 } else if ((addr = myaddrs(hp)) != NULL)
2469 np = lextract(addr, GEXTRA | GFULL | GFULLEXTRA);
2470 hp->h_from = np;
2472 if ((np = hp->h_sender) != NULL) {
2474 } else if ((addr = ok_vlook(sender)) != NULL)
2475 np = lextract(addr, GEXTRA | GFULL | GFULLEXTRA);
2476 hp->h_sender = np;
2478 NYD_LEAVE;
2481 FL struct name const *
2482 check_from_and_sender(struct name const *fromfield,
2483 struct name const *senderfield)
2485 struct name const *rv = NULL;
2486 NYD_ENTER;
2488 if (senderfield != NULL) {
2489 if (senderfield->n_flink != NULL) {
2490 n_err(_("The Sender: field may contain only one address\n"));
2491 goto jleave;
2493 rv = senderfield;
2496 if (fromfield != NULL) {
2497 if (fromfield->n_flink != NULL && senderfield == NULL) {
2498 n_err(_("A Sender: is required when there are multiple "
2499 "addresses in From:\n"));
2500 goto jleave;
2502 if (rv == NULL)
2503 rv = fromfield;
2506 if (rv == NULL)
2507 rv = (struct name*)0x1;
2508 jleave:
2509 NYD_LEAVE;
2510 return rv;
2513 #ifdef HAVE_XSSL
2514 FL char *
2515 getsender(struct message *mp)
2517 char *cp;
2518 struct name *np;
2519 NYD_ENTER;
2521 if ((cp = hfield1("from", mp)) == NULL ||
2522 (np = lextract(cp, GEXTRA | GSKIN)) == NULL)
2523 cp = NULL;
2524 else
2525 cp = (np->n_flink != NULL) ? skin(hfield1("sender", mp)) : np->n_name;
2526 NYD_LEAVE;
2527 return cp;
2529 #endif
2531 FL int
2532 grab_headers(enum n_lexinput_flags lif, struct header *hp, enum gfield gflags,
2533 int subjfirst)
2535 /* TODO grab_headers: again, check counts etc. against RFC;
2536 * TODO (now assumes check_from_and_sender() is called afterwards ++ */
2537 int errs;
2538 int volatile comma;
2539 NYD_ENTER;
2541 errs = 0;
2542 comma = (ok_blook(bsdcompat) || ok_blook(bsdmsgs)) ? 0 : GCOMMA;
2544 if (gflags & GTO)
2545 hp->h_to = grab_names(lif, "To: ", hp->h_to, comma, GTO | GFULL);
2546 if (subjfirst && (gflags & GSUBJECT))
2547 hp->h_subject = n_lex_input_cp(lif, "Subject: ", hp->h_subject);
2548 if (gflags & GCC)
2549 hp->h_cc = grab_names(lif, "Cc: ", hp->h_cc, comma, GCC | GFULL);
2550 if (gflags & GBCC)
2551 hp->h_bcc = grab_names(lif, "Bcc: ", hp->h_bcc, comma, GBCC | GFULL);
2553 if (gflags & GEXTRA) {
2554 if (hp->h_from == NULL)
2555 hp->h_from = lextract(myaddrs(hp), GEXTRA | GFULL | GFULLEXTRA);
2556 hp->h_from = grab_names(lif, "From: ", hp->h_from, comma,
2557 GEXTRA | GFULL | GFULLEXTRA);
2558 if (hp->h_replyto == NULL)
2559 hp->h_replyto = lextract(ok_vlook(replyto), GEXTRA | GFULL);
2560 hp->h_replyto = grab_names(lif, "Reply-To: ", hp->h_replyto, comma,
2561 GEXTRA | GFULL);
2562 if (hp->h_sender == NULL)
2563 hp->h_sender = extract(ok_vlook(sender), GEXTRA | GFULL);
2564 hp->h_sender = grab_names(lif, "Sender: ", hp->h_sender, comma,
2565 GEXTRA | GFULL);
2568 if (!subjfirst && (gflags & GSUBJECT))
2569 hp->h_subject = n_lex_input_cp(lif, "Subject: ", hp->h_subject);
2571 NYD_LEAVE;
2572 return errs;
2575 FL bool_t
2576 header_match(struct message *mp, struct search_expr const *sep)
2578 struct str in, out;
2579 FILE *ibuf;
2580 int lc;
2581 size_t linesize = 0; /* TODO line pool */
2582 char *linebuf = NULL, *colon;
2583 bool_t rv = FAL0;
2584 NYD_ENTER;
2586 if ((ibuf = setinput(&mb, mp, NEED_HEADER)) == NULL)
2587 goto jleave;
2588 if ((lc = mp->m_lines - 1) < 0)
2589 goto jleave;
2591 if ((mp->m_flag & MNOFROM) == 0 &&
2592 readline_restart(ibuf, &linebuf, &linesize, 0) < 0)
2593 goto jleave;
2594 while (lc > 0) {
2595 if (gethfield(ibuf, &linebuf, &linesize, lc, &colon) <= 0)
2596 break;
2597 if (blankchar(*++colon))
2598 ++colon;
2599 in.l = strlen(in.s = colon);
2600 mime_fromhdr(&in, &out, TD_ICONV);
2601 #ifdef HAVE_REGEX
2602 if (sep->ss_sexpr == NULL)
2603 rv = (regexec(&sep->ss_regex, out.s, 0,NULL, 0) != REG_NOMATCH);
2604 else
2605 #endif
2606 rv = substr(out.s, sep->ss_sexpr);
2607 free(out.s);
2608 if (rv)
2609 break;
2612 jleave:
2613 if (linebuf != NULL)
2614 free(linebuf);
2615 NYD_LEAVE;
2616 return rv;
2619 FL struct n_header_field *
2620 n_customhdr_query(void){
2621 char const *vp;
2622 struct n_header_field *rv, **tail, *hfp;
2623 NYD_ENTER;
2625 rv = NULL;
2627 if((vp = ok_vlook(customhdr)) != NULL){
2628 char *buf;
2630 tail = &rv;
2631 buf = savestr(vp);
2632 jch_outer:
2633 while((vp = a_head_customhdr__sep(&buf)) != NULL){
2634 ui32_t nl, bl;
2635 char const *nstart, *cp;
2637 for(nstart = cp = vp;; ++cp){
2638 if(fieldnamechar(*cp))
2639 continue;
2640 if(*cp == '\0'){
2641 if(cp == nstart){
2642 n_err(_("Invalid nameless *customhdr* entry\n"));
2643 goto jch_outer;
2645 }else if(*cp != ':' && !blankchar(*cp)){
2646 jch_badent:
2647 n_err(_("Invalid *customhdr* entry: %s\n"), vp);
2648 goto jch_outer;
2650 break;
2652 nl = (ui32_t)PTR2SIZE(cp - nstart);
2654 while(blankchar(*cp))
2655 ++cp;
2656 if(*cp++ != ':')
2657 goto jch_badent;
2658 while(blankchar(*cp))
2659 ++cp;
2660 bl = (ui32_t)strlen(cp) +1;
2662 *tail =
2663 hfp = salloc(n_VSTRUCT_SIZEOF(struct n_header_field, hf_dat) +
2664 nl +1 + bl);
2665 tail = &hfp->hf_next;
2666 hfp->hf_next = NULL;
2667 hfp->hf_nl = nl;
2668 hfp->hf_bl = bl - 1;
2669 memcpy(hfp->hf_dat, nstart, nl);
2670 hfp->hf_dat[nl++] = '\0';
2671 memcpy(hfp->hf_dat + nl, cp, bl);
2674 NYD_LEAVE;
2675 return rv;
2678 /* s-it-mode */