Implement `xcall' stack-avoidance optimization
[s-mailx.git] / head.c
blob428daa0164dd7cc18fe8ea5a9f88b818c1d56a10
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 && n_err_no == n_ERR_INVAL);
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 cp = &cp1st[tcurr->t_start];
787 cpmax = &cp1st[tcurr->t_end];
788 if(cp == cpmax)
789 continue;
791 if(tcurr != tp)
792 ostp = n_string_push_c(ostp, ' ');
794 if((tcurr->t_f & (a_T_TATOM | a_T_SPECIAL)) == a_T_TATOM)
795 ostp = n_string_push_buf(ostp, cp, PTR2SIZE(cpmax - cp));
796 else{
797 bool_t esc;
799 for(esc = FAL0; cp < cpmax;){
800 if((c.c = *cp++) == '\\' && !esc)
801 esc = TRU1;
802 else{
803 if(esc || c.c == '"'){
804 jput_quote_esc:
805 ostp = n_string_push_c(ostp, '\\');
807 ostp = n_string_push_c(ostp, c.c);
808 esc = FAL0;
811 if(esc){
812 c.c = '\\';
813 goto jput_quote_esc;
816 }while((tcurr = tcurr->t_next) != NULL &&
817 (tcurr->t_f & (a_T_TATOM | a_T_TQUOTE)));
818 ostp = n_string_push_c(ostp, '"');
819 }else if(tcurr->t_f & a_T_SPECIAL)
820 goto jput_quote;
821 else{
822 /* Can we use a fast join mode? */
823 for(tp = tcurr; tcurr != NULL; tcurr = tcurr->t_next){
824 if(!(tcurr->t_f & a_T_TATOM))
825 break;
826 if(tcurr != tp)
827 ostp = n_string_push_c(ostp, ' ');
828 ostp = n_string_push_buf(ostp, &cp1st[tcurr->t_start],
829 (tcurr->t_end - tcurr->t_start));
834 n_lofi_snap_unroll(lofi_snap);
836 agp->ag_input = n_string_cp(ostp);
837 agp->ag_ilen = ostp->s_len;
838 ostp = n_string_drop_ownership(ostp);
840 jleave:
841 NYD_LEAVE;
842 return ((agp->ag_n_flags & NAME_ADDRSPEC_INVALID) == 0);
845 static int
846 gethfield(FILE *f, char **linebuf, size_t *linesize, int rem, char **colon)
848 char *line2 = NULL, *cp, *cp2;
849 size_t line2size = 0;
850 int c, isenc;
851 NYD2_ENTER;
853 if (*linebuf == NULL)
854 *linebuf = srealloc(*linebuf, *linesize = 1);
855 **linebuf = '\0';
856 for (;;) {
857 if (--rem < 0) {
858 rem = -1;
859 break;
861 if ((c = readline_restart(f, linebuf, linesize, 0)) <= 0) {
862 rem = -1;
863 break;
865 for (cp = *linebuf; fieldnamechar(*cp); ++cp)
867 if (cp > *linebuf)
868 while (blankchar(*cp))
869 ++cp;
870 if (*cp != ':' || cp == *linebuf)
871 continue;
873 /* I guess we got a headline. Handle wraparound */
874 *colon = cp;
875 cp = *linebuf + c;
876 for (;;) {
877 isenc = 0;
878 while (PTRCMP(--cp, >=, *linebuf) && blankchar(*cp))
880 cp++;
881 if (rem <= 0)
882 break;
883 if (PTRCMP(cp - 8, >=, *linebuf) && cp[-1] == '=' && cp[-2] == '?')
884 isenc |= 1;
885 ungetc(c = getc(f), f);
886 if (!blankchar(c))
887 break;
888 c = readline_restart(f, &line2, &line2size, 0);
889 if (c < 0)
890 break;
891 --rem;
892 for (cp2 = line2; blankchar(*cp2); ++cp2)
894 c -= (int)PTR2SIZE(cp2 - line2);
895 if (cp2[0] == '=' && cp2[1] == '?' && c > 8)
896 isenc |= 2;
897 if (PTRCMP(cp + c, >=, *linebuf + *linesize - 2)) {
898 size_t diff = PTR2SIZE(cp - *linebuf),
899 colondiff = PTR2SIZE(*colon - *linebuf);
900 *linebuf = srealloc(*linebuf, *linesize += c + 2);
901 cp = &(*linebuf)[diff];
902 *colon = &(*linebuf)[colondiff];
904 if (isenc != 3)
905 *cp++ = ' ';
906 memcpy(cp, cp2, c);
907 cp += c;
909 *cp = '\0';
911 if (line2 != NULL)
912 free(line2);
913 break;
915 NYD2_LEAVE;
916 return rem;
919 static int
920 msgidnextc(char const **cp, int *status)
922 int c;
923 NYD2_ENTER;
925 assert(cp != NULL);
926 assert(*cp != NULL);
927 assert(status != NULL);
929 for (;;) {
930 if (*status & 01) {
931 if (**cp == '"') {
932 *status &= ~01;
933 (*cp)++;
934 continue;
936 if (**cp == '\\') {
937 (*cp)++;
938 if (**cp == '\0')
939 goto jeof;
941 goto jdfl;
943 switch (**cp) {
944 case '(':
945 *cp = skip_comment(&(*cp)[1]);
946 continue;
947 case '>':
948 case '\0':
949 jeof:
950 c = '\0';
951 goto jleave;
952 case '"':
953 (*cp)++;
954 *status |= 01;
955 continue;
956 case '@':
957 *status |= 02;
958 /*FALLTHRU*/
959 default:
960 jdfl:
961 c = *(*cp)++ & 0377;
962 c = (*status & 02) ? lowerconv(c) : c;
963 goto jleave;
966 jleave:
967 NYD2_LEAVE;
968 return c;
971 static int
972 charcount(char *str, int c)
974 char *cp;
975 int i;
976 NYD2_ENTER;
978 for (i = 0, cp = str; *cp; ++cp)
979 if (*cp == c)
980 ++i;
981 NYD2_LEAVE;
982 return i;
985 static char const *
986 nexttoken(char const *cp)
988 NYD2_ENTER;
989 for (;;) {
990 if (*cp == '\0') {
991 cp = NULL;
992 break;
995 if (*cp == '(') {
996 size_t nesting = 1;
998 do switch (*++cp) {
999 case '(':
1000 ++nesting;
1001 break;
1002 case ')':
1003 --nesting;
1004 break;
1005 } while (nesting > 0 && *cp != '\0'); /* XXX error? */
1006 } else if (blankchar(*cp) || *cp == ',')
1007 ++cp;
1008 else
1009 break;
1011 NYD2_LEAVE;
1012 return cp;
1015 static char *
1016 a_head_customhdr__sep(char **iolist){
1017 char *cp, c, *base;
1018 bool_t isesc, anyesc;
1019 NYD2_ENTER;
1021 for(base = *iolist; base != NULL; base = *iolist){
1022 while((c = *base) != '\0' && blankspacechar(c))
1023 ++base;
1025 for(isesc = anyesc = FAL0, cp = base;; ++cp){
1026 if(n_UNLIKELY((c = *cp) == '\0')){
1027 *iolist = NULL;
1028 break;
1029 }else if(!isesc){
1030 if(c == ','){
1031 *iolist = cp + 1;
1032 break;
1034 isesc = (c == '\\');
1035 }else{
1036 isesc = FAL0;
1037 anyesc |= (c == ',');
1041 while(cp > base && blankspacechar(cp[-1]))
1042 --cp;
1043 *cp = '\0';
1045 if(*base != '\0'){
1046 if(anyesc){
1047 char *ins;
1049 for(ins = cp = base;; ++ins)
1050 if((c = *cp) == '\\' && cp[1] == ','){
1051 *ins = ',';
1052 cp += 2;
1053 }else if((*ins = (++cp, c)) == '\0')
1054 break;
1056 break;
1059 NYD2_LEAVE;
1060 return base;
1063 FL char const *
1064 myaddrs(struct header *hp) /* TODO */
1066 struct name *np;
1067 char const *rv, *mta;
1068 NYD_ENTER;
1070 if (hp != NULL && (np = hp->h_from) != NULL) {
1071 if ((rv = np->n_fullname) != NULL)
1072 goto jleave;
1073 if ((rv = np->n_name) != NULL)
1074 goto jleave;
1077 if((rv = ok_vlook(from)) != NULL){
1078 if((np = lextract(rv, GEXTRA | GFULL)) == NULL)
1079 jefrom:
1080 n_err(_("An address given in *from* is invalid: %s\n"), rv);
1081 else for(; np != NULL; np = np->n_flink)
1082 if(is_addr_invalid(np, EACM_STRICT | EACM_NOLOG | EACM_NONAME))
1083 goto jefrom;
1084 goto jleave;
1087 /* When invoking *sendmail* directly, it's its task to generate an otherwise
1088 * undeterminable From: address. However, if the user sets *hostname*,
1089 * accept his desire */
1090 if (ok_vlook(hostname) != NULL)
1091 goto jnodename;
1092 if (ok_vlook(smtp) != NULL || /* TODO obsolete -> mta */
1093 /* TODO pretty hacky for now (this entire fun), later: url_creat()! */
1094 ((mta = ok_vlook(mta)) != NULL &&
1095 (mta = n_servbyname(mta, NULL)) != NULL && *mta != '\0'))
1096 goto jnodename;
1097 jleave:
1098 NYD_LEAVE;
1099 return rv;
1101 jnodename:{
1102 char *cp;
1103 char const *hn, *ln;
1104 size_t i;
1106 hn = nodename(1);
1107 ln = ok_vlook(LOGNAME);
1108 i = strlen(ln) + strlen(hn) + 1 +1;
1109 rv = cp = salloc(i);
1110 sstpcpy(sstpcpy(sstpcpy(cp, ln), "@"), hn);
1112 goto jleave;
1115 FL char const *
1116 myorigin(struct header *hp) /* TODO */
1118 char const *rv = NULL, *ccp;
1119 struct name *np;
1120 NYD_ENTER;
1122 if((ccp = myaddrs(hp)) != NULL &&
1123 (np = lextract(ccp, GEXTRA | GFULL)) != NULL){
1124 if(np->n_flink == NULL)
1125 rv = ccp;
1126 else if((ccp = ok_vlook(sender)) != NULL) {
1127 if((np = lextract(ccp, GEXTRA | GFULL)) == NULL ||
1128 np->n_flink != NULL ||
1129 is_addr_invalid(np, EACM_STRICT | EACM_NOLOG | EACM_NONAME))
1130 n_err(_("The address given in *sender* is invalid: %s\n"), ccp);
1131 else
1132 rv = ccp;
1135 NYD_LEAVE;
1136 return rv;
1139 FL bool_t
1140 is_head(char const *linebuf, size_t linelen, bool_t check_rfc4155)
1142 char date[FROM_DATEBUF];
1143 bool_t rv;
1144 NYD2_ENTER;
1146 if ((rv = (linelen >= 5 && !memcmp(linebuf, "From ", 5))) && check_rfc4155 &&
1147 (extract_date_from_from_(linebuf, linelen, date) <= 0 ||
1148 !_is_date(date)))
1149 rv = TRUM1;
1150 NYD2_LEAVE;
1151 return rv;
1154 FL int
1155 extract_date_from_from_(char const *line, size_t linelen,
1156 char datebuf[FROM_DATEBUF])
1158 int rv;
1159 char const *cp = line;
1160 NYD_ENTER;
1162 rv = 1;
1164 /* "From " */
1165 cp = _from__skipword(cp);
1166 if (cp == NULL)
1167 goto jerr;
1168 /* "addr-spec " */
1169 cp = _from__skipword(cp);
1170 if (cp == NULL)
1171 goto jerr;
1172 if (cp[0] == 't' && cp[1] == 't' && cp[2] == 'y') {
1173 cp = _from__skipword(cp);
1174 if (cp == NULL)
1175 goto jerr;
1177 /* It seems there are invalid MBOX archives in the wild, compare
1178 * . http://bugs.debian.org/624111
1179 * . [Mutt] #3868: mutt should error if the imported mailbox is invalid
1180 * What they do is that they obfuscate the address to "name at host",
1181 * and even "name at host dot dom dot dom. I think we should handle that */
1182 else if(cp[0] == 'a' && cp[1] == 't' && cp[2] == ' '){
1183 rv = -1;
1184 cp += 3;
1185 jat_dot:
1186 cp = _from__skipword(cp);
1187 if (cp == NULL)
1188 goto jerr;
1189 if(cp[0] == 'd' && cp[1] == 'o' && cp[2] == 't' && cp[3] == ' '){
1190 cp += 4;
1191 goto jat_dot;
1195 linelen -= PTR2SIZE(cp - line);
1196 if (linelen < _DATE_MINLEN)
1197 goto jerr;
1198 if (cp[linelen - 1] == '\n') {
1199 --linelen;
1200 /* (Rather IMAP/POP3 only) */
1201 if (cp[linelen - 1] == '\r')
1202 --linelen;
1203 if (linelen < _DATE_MINLEN)
1204 goto jerr;
1206 if (linelen >= FROM_DATEBUF)
1207 goto jerr;
1209 jleave:
1210 memcpy(datebuf, cp, linelen);
1211 datebuf[linelen] = '\0';
1212 NYD_LEAVE;
1213 return rv;
1214 jerr:
1215 cp = _("<Unknown date>");
1216 linelen = strlen(cp);
1217 if (linelen >= FROM_DATEBUF)
1218 linelen = FROM_DATEBUF;
1219 rv = 0;
1220 goto jleave;
1223 FL void
1224 extract_header(FILE *fp, struct header *hp, si8_t *checkaddr_err)
1226 /* See the prototype declaration for the hairy relationship of
1227 * n_poption&n_PO_t_FLAG and/or n_psonce&n_PSO_t_FLAG in here */
1228 struct n_header_field **hftail;
1229 struct header nh, *hq = &nh;
1230 char *linebuf = NULL /* TODO line pool */, *colon;
1231 size_t linesize = 0, seenfields = 0;
1232 int lc, c;
1233 char const *val, *cp;
1234 NYD_ENTER;
1236 memset(hq, 0, sizeof *hq);
1237 if ((n_psonce & n_PSO_t_FLAG) && (n_poption & n_PO_t_FLAG)) {
1238 hq->h_to = hp->h_to;
1239 hq->h_cc = hp->h_cc;
1240 hq->h_bcc = hp->h_bcc;
1242 hftail = &hq->h_user_headers;
1244 for (lc = 0; readline_restart(fp, &linebuf, &linesize, 0) > 0; ++lc)
1247 /* TODO yippieia, cat(check(lextract)) :-) */
1248 rewind(fp);
1249 while ((lc = gethfield(fp, &linebuf, &linesize, lc, &colon)) >= 0) {
1250 struct name *np;
1252 /* We explicitly allow EAF_NAME for some addressees since aliases are not
1253 * yet expanded when we parse these! */
1254 if ((val = thisfield(linebuf, "to")) != NULL) {
1255 ++seenfields;
1256 hq->h_to = cat(hq->h_to, checkaddrs(lextract(val, GTO | GFULL),
1257 EACM_NORMAL | EAF_NAME | EAF_MAYKEEP, checkaddr_err));
1258 } else if ((val = thisfield(linebuf, "cc")) != NULL) {
1259 ++seenfields;
1260 hq->h_cc = cat(hq->h_cc, checkaddrs(lextract(val, GCC | GFULL),
1261 EACM_NORMAL | EAF_NAME | EAF_MAYKEEP, checkaddr_err));
1262 } else if ((val = thisfield(linebuf, "bcc")) != NULL) {
1263 ++seenfields;
1264 hq->h_bcc = cat(hq->h_bcc, checkaddrs(lextract(val, GBCC | GFULL),
1265 EACM_NORMAL | EAF_NAME | EAF_MAYKEEP, checkaddr_err));
1266 } else if ((val = thisfield(linebuf, "from")) != NULL) {
1267 if (!(n_psonce & n_PSO_t_FLAG) || (n_poption & n_PO_t_FLAG)) {
1268 ++seenfields;
1269 hq->h_from = cat(hq->h_from,
1270 checkaddrs(lextract(val, GEXTRA | GFULL | GFULLEXTRA),
1271 EACM_STRICT, NULL));
1273 } else if ((val = thisfield(linebuf, "reply-to")) != NULL) {
1274 ++seenfields;
1275 hq->h_replyto = cat(hq->h_replyto,
1276 checkaddrs(lextract(val, GEXTRA | GFULL), EACM_STRICT, NULL));
1277 } else if ((val = thisfield(linebuf, "sender")) != NULL) {
1278 if (!(n_psonce & n_PSO_t_FLAG) || (n_poption & n_PO_t_FLAG)) {
1279 ++seenfields;
1280 hq->h_sender = cat(hq->h_sender, /* TODO cat? check! */
1281 checkaddrs(lextract(val, GEXTRA | GFULL | GFULLEXTRA),
1282 EACM_STRICT, NULL));
1283 } else
1284 goto jebadhead;
1285 } else if ((val = thisfield(linebuf, "subject")) != NULL ||
1286 (val = thisfield(linebuf, "subj")) != NULL) {
1287 ++seenfields;
1288 for (cp = val; blankchar(*cp); ++cp)
1290 hq->h_subject = (hq->h_subject != NULL)
1291 ? save2str(hq->h_subject, cp) : savestr(cp);
1293 /* The remaining are mostly hacked in and thus TODO -- at least in
1294 * TODO respect to their content checking */
1295 else if((val = thisfield(linebuf, "message-id")) != NULL){
1296 if(n_psonce & n_PSO_t_FLAG){
1297 np = checkaddrs(lextract(val, GREF),
1298 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG | EACM_NONAME,
1299 NULL);
1300 if (np == NULL || np->n_flink != NULL)
1301 goto jebadhead;
1302 ++seenfields;
1303 hq->h_message_id = np;
1304 }else
1305 goto jebadhead;
1306 }else if((val = thisfield(linebuf, "in-reply-to")) != NULL){
1307 if(n_psonce & n_PSO_t_FLAG){
1308 np = checkaddrs(lextract(val, GREF),
1309 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG | EACM_NONAME,
1310 NULL);
1311 ++seenfields;
1312 hq->h_in_reply_to = np;
1313 }else
1314 goto jebadhead;
1315 }else if((val = thisfield(linebuf, "references")) != NULL){
1316 if(n_psonce & n_PSO_t_FLAG){
1317 ++seenfields;
1318 /* TODO Limit number of references TODO better on parser side */
1319 hq->h_ref = cat(hq->h_ref, checkaddrs(extract(val, GREF),
1320 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG | EACM_NONAME,
1321 NULL));
1322 }else
1323 goto jebadhead;
1325 /* and that is very hairy */
1326 else if((val = thisfield(linebuf, "mail-followup-to")) != NULL){
1327 if(n_psonce & n_PSO_t_FLAG){
1328 ++seenfields;
1329 hq->h_mft = cat(hq->h_mft, checkaddrs(lextract(val, GEXTRA | GFULL),
1330 /*EACM_STRICT | TODO '/' valid!! | EACM_NOLOG | */EACM_NONAME,
1331 checkaddr_err));
1332 }else
1333 goto jebadhead;
1335 /* A free-form user header; gethfield() did some verification already.. */
1336 else{
1337 struct n_header_field *hfp;
1338 ui32_t nl, bl;
1339 char const *nstart;
1341 for(nstart = cp = linebuf;; ++cp)
1342 if(!fieldnamechar(*cp))
1343 break;
1344 nl = (ui32_t)PTR2SIZE(cp - nstart);
1346 while(blankchar(*cp))
1347 ++cp;
1348 if(*cp++ != ':'){
1349 jebadhead:
1350 n_err(_("Ignoring header field: %s\n"), linebuf);
1351 continue;
1353 while(blankchar(*cp))
1354 ++cp;
1355 bl = (ui32_t)strlen(cp) +1;
1357 ++seenfields;
1358 *hftail = hfp = salloc(n_VSTRUCT_SIZEOF(struct n_header_field, hf_dat
1359 ) + nl +1 + bl);
1360 hftail = &hfp->hf_next;
1361 hfp->hf_next = NULL;
1362 hfp->hf_nl = nl;
1363 hfp->hf_bl = bl - 1;
1364 memcpy(hfp->hf_dat, nstart, nl);
1365 hfp->hf_dat[nl++] = '\0';
1366 memcpy(hfp->hf_dat + nl, cp, bl);
1370 /* In case the blank line after the header has been edited out. Otherwise,
1371 * fetch the header separator */
1372 if (linebuf != NULL) {
1373 if (linebuf[0] != '\0') {
1374 for (cp = linebuf; *(++cp) != '\0';)
1376 fseek(fp, (long)-PTR2SIZE(1 + cp - linebuf), SEEK_CUR);
1377 } else {
1378 if ((c = getc(fp)) != '\n' && c != EOF)
1379 ungetc(c, fp);
1383 if (seenfields > 0 && (checkaddr_err == NULL || *checkaddr_err == 0)) {
1384 hp->h_to = hq->h_to;
1385 hp->h_cc = hq->h_cc;
1386 hp->h_bcc = hq->h_bcc;
1387 hp->h_from = hq->h_from;
1388 hp->h_replyto = hq->h_replyto;
1389 hp->h_sender = hq->h_sender;
1390 if (hq->h_subject != NULL || !(n_psonce & n_PSO_t_FLAG) ||
1391 !(n_poption & n_PO_t_FLAG))
1392 hp->h_subject = hq->h_subject;
1393 hp->h_user_headers = hq->h_user_headers;
1395 if (n_psonce & n_PSO_t_FLAG) {
1396 hp->h_ref = hq->h_ref;
1397 hp->h_message_id = hq->h_message_id;
1398 hp->h_in_reply_to = hq->h_in_reply_to;
1399 hp->h_mft = hq->h_mft;
1401 /* And perform additional validity checks so that we don't bail later
1402 * on TODO this is good and the place where this should occur,
1403 * TODO unfortunately a lot of other places do again and blabla */
1404 if (hp->h_from == NULL)
1405 hp->h_from = n_poption_arg_r;
1406 else if (hp->h_from->n_flink != NULL && hp->h_sender == NULL)
1407 hp->h_sender = lextract(ok_vlook(sender),
1408 GEXTRA | GFULL | GFULLEXTRA);
1410 } else
1411 n_err(_("Restoring deleted header lines\n"));
1413 if (linebuf != NULL)
1414 free(linebuf);
1415 NYD_LEAVE;
1418 FL char *
1419 hfield_mult(char const *field, struct message *mp, int mult)
1421 FILE *ibuf;
1422 int lc;
1423 struct str hfs;
1424 size_t linesize = 0; /* TODO line pool */
1425 char *linebuf = NULL, *colon;
1426 char const *hfield;
1427 NYD_ENTER;
1429 /* There are (spam) messages which have header bytes which are many KB when
1430 * joined, so resize a single heap storage until we are done if we shall
1431 * collect a field that may have multiple bodies; only otherwise use the
1432 * string dope directly */
1433 memset(&hfs, 0, sizeof hfs);
1435 if ((ibuf = setinput(&mb, mp, NEED_HEADER)) == NULL)
1436 goto jleave;
1437 if ((lc = mp->m_lines - 1) < 0)
1438 goto jleave;
1440 if ((mp->m_flag & MNOFROM) == 0 &&
1441 readline_restart(ibuf, &linebuf, &linesize, 0) < 0)
1442 goto jleave;
1443 while (lc > 0) {
1444 if ((lc = gethfield(ibuf, &linebuf, &linesize, lc, &colon)) < 0)
1445 break;
1446 if ((hfield = thisfield(linebuf, field)) != NULL && *hfield != '\0') {
1447 if (mult)
1448 n_str_add_buf(&hfs, hfield, strlen(hfield));
1449 else {
1450 hfs.s = savestr(hfield);
1451 break;
1456 jleave:
1457 if (linebuf != NULL)
1458 free(linebuf);
1459 if (mult && hfs.s != NULL) {
1460 colon = savestrbuf(hfs.s, hfs.l);
1461 free(hfs.s);
1462 hfs.s = colon;
1464 NYD_LEAVE;
1465 return hfs.s;
1468 FL char const *
1469 thisfield(char const *linebuf, char const *field)
1471 char const *rv = NULL;
1472 NYD2_ENTER;
1474 while (lowerconv(*linebuf) == lowerconv(*field)) {
1475 ++linebuf;
1476 ++field;
1478 if (*field != '\0')
1479 goto jleave;
1481 while (blankchar(*linebuf))
1482 ++linebuf;
1483 if (*linebuf++ != ':')
1484 goto jleave;
1486 while (blankchar(*linebuf)) /* TODO header parser.. strip trailing WS?!? */
1487 ++linebuf;
1488 rv = linebuf;
1489 jleave:
1490 NYD2_LEAVE;
1491 return rv;
1494 FL char *
1495 nameof(struct message *mp, int reptype)
1497 char *cp, *cp2;
1498 NYD_ENTER;
1500 cp = skin(name1(mp, reptype));
1501 if (reptype != 0 || charcount(cp, '!') < 2)
1502 goto jleave;
1503 cp2 = strrchr(cp, '!');
1504 --cp2;
1505 while (cp2 > cp && *cp2 != '!')
1506 --cp2;
1507 if (*cp2 == '!')
1508 cp = cp2 + 1;
1509 jleave:
1510 NYD_LEAVE;
1511 return cp;
1514 FL char const *
1515 skip_comment(char const *cp)
1517 size_t nesting;
1518 NYD_ENTER;
1520 for (nesting = 1; nesting > 0 && *cp; ++cp) {
1521 switch (*cp) {
1522 case '\\':
1523 if (cp[1])
1524 ++cp;
1525 break;
1526 case '(':
1527 ++nesting;
1528 break;
1529 case ')':
1530 --nesting;
1531 break;
1534 NYD_LEAVE;
1535 return cp;
1538 FL char const *
1539 routeaddr(char const *name)
1541 char const *np, *rp = NULL;
1542 NYD_ENTER;
1544 for (np = name; *np; np++) {
1545 switch (*np) {
1546 case '(':
1547 np = skip_comment(np + 1) - 1;
1548 break;
1549 case '"':
1550 while (*np) {
1551 if (*++np == '"')
1552 break;
1553 if (*np == '\\' && np[1])
1554 np++;
1556 break;
1557 case '<':
1558 rp = np;
1559 break;
1560 case '>':
1561 goto jleave;
1564 rp = NULL;
1565 jleave:
1566 NYD_LEAVE;
1567 return rp;
1570 FL enum expand_addr_flags
1571 expandaddr_to_eaf(void)
1573 struct eafdesc {
1574 char const *eafd_name;
1575 bool_t eafd_is_target;
1576 ui8_t eafd_andoff;
1577 ui8_t eafd_or;
1578 } const eafa[] = {
1579 {"restrict", FAL0, EAF_TARGET_MASK, EAF_RESTRICT | EAF_RESTRICT_TARGETS},
1580 {"fail", FAL0, EAF_NONE, EAF_FAIL},
1581 {"failinvaddr", FAL0, EAF_NONE, EAF_FAILINVADDR | EAF_ADDR},
1582 {"all", TRU1, EAF_NONE, EAF_TARGET_MASK},
1583 {"file", TRU1, EAF_NONE, EAF_FILE},
1584 {"pipe", TRU1, EAF_NONE, EAF_PIPE},
1585 {"name", TRU1, EAF_NONE, EAF_NAME},
1586 {"addr", TRU1, EAF_NONE, EAF_ADDR}
1587 }, *eafp;
1589 char *buf;
1590 enum expand_addr_flags rv;
1591 char const *cp;
1592 NYD2_ENTER;
1594 if ((cp = ok_vlook(expandaddr)) == NULL)
1595 rv = EAF_RESTRICT_TARGETS;
1596 else if (*cp == '\0')
1597 rv = EAF_TARGET_MASK;
1598 else {
1599 rv = EAF_TARGET_MASK;
1601 for (buf = savestr(cp); (cp = n_strsep(&buf, ',', TRU1)) != NULL;) {
1602 bool_t minus;
1604 if ((minus = (*cp == '-')) || *cp == '+')
1605 ++cp;
1606 for (eafp = eafa;; ++eafp) {
1607 if (eafp == eafa + n_NELEM(eafa)) {
1608 if (n_poption & n_PO_D_V)
1609 n_err(_("Unknown *expandaddr* value: %s\n"), cp);
1610 break;
1611 } else if (!asccasecmp(cp, eafp->eafd_name)) {
1612 if (!minus) {
1613 rv &= ~eafp->eafd_andoff;
1614 rv |= eafp->eafd_or;
1615 } else {
1616 if (eafp->eafd_is_target)
1617 rv &= ~eafp->eafd_or;
1618 else if (n_poption & n_PO_D_V)
1619 n_err(_("minus - prefix invalid for *expandaddr* value: "
1620 "%s\n"), --cp);
1622 break;
1623 } else if (!asccasecmp(cp, "noalias")) { /* TODO v15 OBSOLETE */
1624 n_OBSOLETE(_("*expandaddr*: noalias is henceforth -name"));
1625 rv &= ~EAF_NAME;
1626 break;
1631 if((rv & EAF_RESTRICT) && ((n_psonce & n_PSO_INTERACTIVE) ||
1632 (n_poption & n_PO_TILDE_FLAG)))
1633 rv |= EAF_TARGET_MASK;
1634 else if(n_poption & n_PO_D_V){
1635 if(!(rv & EAF_TARGET_MASK))
1636 n_err(_("*expandaddr* doesn't allow any addressees\n"));
1637 else if((rv & EAF_FAIL) && (rv & EAF_TARGET_MASK) == EAF_TARGET_MASK)
1638 n_err(_("*expandaddr* with fail, but no restrictions to apply\n"));
1641 NYD2_LEAVE;
1642 return rv;
1645 FL si8_t
1646 is_addr_invalid(struct name *np, enum expand_addr_check_mode eacm)
1648 char cbuf[sizeof "'\\U12340'"];
1649 char const *cs;
1650 int f;
1651 si8_t rv;
1652 enum expand_addr_flags eaf;
1653 NYD_ENTER;
1655 eaf = expandaddr_to_eaf();
1656 f = np->n_flags;
1658 if ((rv = ((f & NAME_ADDRSPEC_INVALID) != 0))) {
1659 if (eaf & EAF_FAILINVADDR)
1660 rv = -rv;
1662 if ((eacm & EACM_NOLOG) || (f & NAME_ADDRSPEC_ERR_EMPTY)) {
1664 } else {
1665 ui32_t c;
1666 char const *fmt = "'\\x%02X'";
1667 bool_t ok8bit = TRU1;
1669 if (f & NAME_ADDRSPEC_ERR_IDNA) {
1670 cs = _("Invalid domain name: %s, character %s\n");
1671 fmt = "'\\U%04X'";
1672 ok8bit = FAL0;
1673 } else if (f & NAME_ADDRSPEC_ERR_ATSEQ)
1674 cs = _("%s contains invalid %s sequence\n");
1675 else if (f & NAME_ADDRSPEC_ERR_NAME) {
1676 cs = _("%s is an invalid alias name\n");
1677 } else
1678 cs = _("%s contains invalid byte %s\n");
1680 c = NAME_ADDRSPEC_ERR_GETWC(f);
1681 snprintf(cbuf, sizeof cbuf,
1682 (ok8bit && c >= 040 && c <= 0177 ? "'%c'" : fmt), c);
1683 goto jprint;
1685 goto jleave;
1688 /* *expandaddr* stuff */
1689 if (!(rv = ((eacm & EACM_MODE_MASK) != EACM_NONE)))
1690 goto jleave;
1692 if ((eacm & EACM_STRICT) && (f & NAME_ADDRSPEC_ISFILEORPIPE)) {
1693 if (eaf & EAF_FAIL)
1694 rv = -rv;
1695 cs = _("%s%s: file or pipe addressees not allowed here\n");
1696 if (eacm & EACM_NOLOG)
1697 goto jleave;
1698 else
1699 goto j0print;
1702 eaf |= (eacm & EAF_TARGET_MASK);
1703 if (eacm & EACM_NONAME)
1704 eaf &= ~EAF_NAME;
1706 if (eaf == EAF_NONE) {
1707 rv = FAL0;
1708 goto jleave;
1710 if (eaf & EAF_FAIL)
1711 rv = -rv;
1713 if (!(eaf & EAF_FILE) && (f & NAME_ADDRSPEC_ISFILE)) {
1714 cs = _("%s%s: *expandaddr* doesn't allow file target\n");
1715 if (eacm & EACM_NOLOG)
1716 goto jleave;
1717 } else if (!(eaf & EAF_PIPE) && (f & NAME_ADDRSPEC_ISPIPE)) {
1718 cs = _("%s%s: *expandaddr* doesn't allow command pipe target\n");
1719 if (eacm & EACM_NOLOG)
1720 goto jleave;
1721 } else if (!(eaf & EAF_NAME) && (f & NAME_ADDRSPEC_ISNAME)) {
1722 cs = _("%s%s: *expandaddr* doesn't allow user name target\n");
1723 if (eacm & EACM_NOLOG)
1724 goto jleave;
1725 } else if (!(eaf & EAF_ADDR) && (f & NAME_ADDRSPEC_ISADDR)) {
1726 cs = _("%s%s: *expandaddr* doesn't allow mail address target\n");
1727 if (eacm & EACM_NOLOG)
1728 goto jleave;
1729 } else {
1730 rv = FAL0;
1731 goto jleave;
1734 j0print:
1735 cbuf[0] = '\0';
1736 jprint:
1737 n_err(cs, n_shexp_quote_cp(np->n_name, TRU1), cbuf);
1738 jleave:
1739 NYD_LEAVE;
1740 return rv;
1743 FL char *
1744 skin(char const *name)
1746 struct n_addrguts ag;
1747 char *rv;
1748 NYD_ENTER;
1750 if(name != NULL){
1751 name = n_addrspec_with_guts(&ag,name, TRU1);
1752 rv = ag.ag_skinned;
1753 if(!(ag.ag_n_flags & NAME_NAME_SALLOC))
1754 rv = savestrbuf(rv, ag.ag_slen);
1755 }else
1756 rv = NULL;
1757 NYD_LEAVE;
1758 return rv;
1761 /* TODO addrspec_with_guts: RFC 5322
1762 * TODO addrspec_with_guts: trim whitespace ETC. ETC. ETC.!!! */
1763 FL char const *
1764 n_addrspec_with_guts(struct n_addrguts *agp, char const *name, bool_t doskin){
1765 char const *cp;
1766 char *cp2, *bufend, *nbuf, c;
1767 enum{
1768 a_NONE,
1769 a_GOTLT = 1<<0,
1770 a_GOTADDR = 1<<1,
1771 a_GOTSPACE = 1<<2,
1772 a_LASTSP = 1<<3
1773 } flags;
1774 NYD_ENTER;
1776 memset(agp, 0, sizeof *agp);
1778 if((agp->ag_input = name) == NULL || (agp->ag_ilen = strlen(name)) == 0){
1779 agp->ag_skinned = n_UNCONST(n_empty); /* ok: NAME_SALLOC is not set */
1780 agp->ag_slen = 0;
1781 agp->ag_n_flags |= NAME_ADDRSPEC_CHECKED;
1782 NAME_ADDRSPEC_ERR_SET(agp->ag_n_flags, NAME_ADDRSPEC_ERR_EMPTY, 0);
1783 goto jleave;
1784 }else if(!doskin){
1785 /*agp->ag_iaddr_start = 0;*/
1786 agp->ag_iaddr_aend = agp->ag_ilen;
1787 agp->ag_skinned = n_UNCONST(name); /* (NAME_SALLOC not set) */
1788 agp->ag_slen = agp->ag_ilen;
1789 agp->ag_n_flags = NAME_SKINNED;
1790 goto jcheck;
1793 flags = a_NONE;
1794 nbuf = n_lofi_alloc(agp->ag_ilen +1);
1795 /*agp->ag_iaddr_start = 0;*/
1796 cp2 = bufend = nbuf;
1798 /* TODO This is complete crap and should use a token parser */
1799 for(cp = name++; (c = *cp++) != '\0';){
1800 switch (c) {
1801 case '(':
1802 cp = skip_comment(cp);
1803 flags &= ~a_LASTSP;
1804 break;
1805 case '"':
1806 /* Start of a "quoted-string". Copy it in its entirety */
1807 /* XXX RFC: quotes are "semantically invisible"
1808 * XXX But it was explicitly added (Changelog.Heirloom,
1809 * XXX [9.23] released 11/15/00, "Do not remove quotes
1810 * XXX when skinning names"? No more info.. */
1811 *cp2++ = c;
1812 while ((c = *cp) != '\0') { /* TODO improve */
1813 ++cp;
1814 if (c == '"') {
1815 *cp2++ = c;
1816 break;
1818 if (c != '\\')
1819 *cp2++ = c;
1820 else if ((c = *cp) != '\0') {
1821 *cp2++ = c;
1822 ++cp;
1825 flags &= ~a_LASTSP;
1826 break;
1827 case ' ':
1828 case '\t':
1829 if((flags & (a_GOTADDR | a_GOTSPACE)) == a_GOTADDR){
1830 flags |= a_GOTSPACE;
1831 agp->ag_iaddr_aend = PTR2SIZE(cp - name);
1833 if (cp[0] == 'a' && cp[1] == 't' && blankchar(cp[2]))
1834 cp += 3, *cp2++ = '@';
1835 else if (cp[0] == '@' && blankchar(cp[1]))
1836 cp += 2, *cp2++ = '@';
1837 else
1838 flags |= a_LASTSP;
1839 break;
1840 case '<':
1841 agp->ag_iaddr_start = PTR2SIZE(cp - (name - 1));
1842 cp2 = bufend;
1843 flags &= ~(a_GOTSPACE | a_LASTSP);
1844 flags |= a_GOTLT | a_GOTADDR;
1845 break;
1846 case '>':
1847 if(flags & a_GOTLT){
1848 /* (_addrspec_check() verifies these later!) */
1849 flags &= ~(a_GOTLT | a_LASTSP);
1850 agp->ag_iaddr_aend = PTR2SIZE(cp - name);
1852 /* Skip over the entire remaining field */
1853 while((c = *cp) != '\0' && c != ','){
1854 ++cp;
1855 if (c == '(')
1856 cp = skip_comment(cp);
1857 else if (c == '"')
1858 while ((c = *cp) != '\0') {
1859 ++cp;
1860 if (c == '"')
1861 break;
1862 if (c == '\\' && *cp != '\0')
1863 ++cp;
1866 break;
1868 /* FALLTHRU */
1869 default:
1870 if(flags & a_LASTSP){
1871 flags &= ~a_LASTSP;
1872 if(flags & a_GOTADDR)
1873 *cp2++ = ' ';
1875 *cp2++ = c;
1876 /* This character is forbidden here, but it may nonetheless be
1877 * present: ensure we turn this into something valid! (E.g., if the
1878 * next character would be a "..) */
1879 if(c == '\\' && *cp != '\0')
1880 *cp2++ = *cp++;
1881 if(c == ','){
1882 if(!(flags & a_GOTLT)){
1883 *cp2++ = ' ';
1884 for(; blankchar(*cp); ++cp)
1886 flags &= ~a_LASTSP;
1887 bufend = cp2;
1889 }else if(!(flags & a_GOTADDR)){
1890 flags |= a_GOTADDR;
1891 agp->ag_iaddr_start = PTR2SIZE(cp - name);
1895 --name;
1896 agp->ag_slen = PTR2SIZE(cp2 - nbuf);
1897 if (agp->ag_iaddr_aend == 0)
1898 agp->ag_iaddr_aend = agp->ag_ilen;
1899 /* Misses > */
1900 else if (agp->ag_iaddr_aend < agp->ag_iaddr_start) {
1901 cp2 = n_autorec_alloc(NULL, agp->ag_ilen + 1 +1);
1902 memcpy(cp2, agp->ag_input, agp->ag_ilen);
1903 agp->ag_iaddr_aend = agp->ag_ilen;
1904 cp2[agp->ag_ilen++] = '>';
1905 cp2[agp->ag_ilen] = '\0';
1907 agp->ag_skinned = savestrbuf(nbuf, agp->ag_slen);
1908 n_lofi_free(nbuf);
1909 agp->ag_n_flags = NAME_NAME_SALLOC | NAME_SKINNED;
1910 jcheck:
1911 if(a_head_addrspec_check(agp, doskin) <= FAL0)
1912 name = NULL;
1913 else
1914 name = agp->ag_input;
1915 jleave:
1916 NYD_LEAVE;
1917 return name;
1920 FL char *
1921 realname(char const *name)
1923 char const *cp, *cq, *cstart = NULL, *cend = NULL;
1924 char *rname, *rp;
1925 struct str in, out;
1926 int quoted, good, nogood;
1927 NYD_ENTER;
1929 if ((cp = n_UNCONST(name)) == NULL)
1930 goto jleave;
1931 for (; *cp != '\0'; ++cp) {
1932 switch (*cp) {
1933 case '(':
1934 if (cstart != NULL) {
1935 /* More than one comment in address, doesn't make sense to display
1936 * it without context. Return the entire field */
1937 cp = mime_fromaddr(name);
1938 goto jleave;
1940 cstart = cp++;
1941 cp = skip_comment(cp);
1942 cend = cp--;
1943 if (cend <= cstart)
1944 cend = cstart = NULL;
1945 break;
1946 case '"':
1947 while (*cp) {
1948 if (*++cp == '"')
1949 break;
1950 if (*cp == '\\' && cp[1])
1951 ++cp;
1953 break;
1954 case '<':
1955 if (cp > name) {
1956 cstart = name;
1957 cend = cp;
1959 break;
1960 case ',':
1961 /* More than one address. Just use the first one */
1962 goto jbrk;
1966 jbrk:
1967 if (cstart == NULL) {
1968 if (*name == '<') {
1969 /* If name contains only a route-addr, the surrounding angle brackets
1970 * don't serve any useful purpose when displaying, so remove */
1971 cp = prstr(skin(name));
1972 } else
1973 cp = mime_fromaddr(name);
1974 goto jleave;
1977 /* Strip quotes. Note that quotes that appear within a MIME encoded word are
1978 * not stripped. The idea is to strip only syntactical relevant things (but
1979 * this is not necessarily the most sensible way in practice) */
1980 rp = rname = ac_alloc(PTR2SIZE(cend - cstart +1));
1981 quoted = 0;
1982 for (cp = cstart; cp < cend; ++cp) {
1983 if (*cp == '(' && !quoted) {
1984 cq = skip_comment(++cp);
1985 if (PTRCMP(--cq, >, cend))
1986 cq = cend;
1987 while (cp < cq) {
1988 if (*cp == '\\' && PTRCMP(cp + 1, <, cq))
1989 ++cp;
1990 *rp++ = *cp++;
1992 } else if (*cp == '\\' && PTRCMP(cp + 1, <, cend))
1993 *rp++ = *++cp;
1994 else if (*cp == '"') {
1995 quoted = !quoted;
1996 continue;
1997 } else
1998 *rp++ = *cp;
2000 *rp = '\0';
2001 in.s = rname;
2002 in.l = rp - rname;
2003 mime_fromhdr(&in, &out, TD_ISPR | TD_ICONV);
2004 ac_free(rname);
2005 rname = savestr(out.s);
2006 free(out.s);
2008 while (blankchar(*rname))
2009 ++rname;
2010 for (rp = rname; *rp != '\0'; ++rp)
2012 while (PTRCMP(--rp, >=, rname) && blankchar(*rp))
2013 *rp = '\0';
2014 if (rp == rname) {
2015 cp = mime_fromaddr(name);
2016 goto jleave;
2019 /* mime_fromhdr() has converted all nonprintable characters to question
2020 * marks now. These and blanks are considered uninteresting; if the
2021 * displayed part of the real name contains more than 25% of them, it is
2022 * probably better to display the plain email address instead */
2023 good = 0;
2024 nogood = 0;
2025 for (rp = rname; *rp != '\0' && PTRCMP(rp, <, rname + 20); ++rp)
2026 if (*rp == '?' || blankchar(*rp))
2027 ++nogood;
2028 else
2029 ++good;
2030 cp = (good * 3 < nogood) ? prstr(skin(name)) : rname;
2031 jleave:
2032 NYD_LEAVE;
2033 return n_UNCONST(cp);
2036 FL char *
2037 name1(struct message *mp, int reptype)
2039 char *namebuf, *cp, *cp2, *linebuf = NULL /* TODO line pool */;
2040 size_t namesize, linesize = 0;
2041 FILE *ibuf;
2042 int f1st = 1;
2043 NYD_ENTER;
2045 if ((cp = hfield1("from", mp)) != NULL && *cp != '\0')
2046 goto jleave;
2047 if (reptype == 0 && (cp = hfield1("sender", mp)) != NULL && *cp != '\0')
2048 goto jleave;
2050 namebuf = smalloc(namesize = 1);
2051 namebuf[0] = 0;
2052 if (mp->m_flag & MNOFROM)
2053 goto jout;
2054 if ((ibuf = setinput(&mb, mp, NEED_HEADER)) == NULL)
2055 goto jout;
2056 if (readline_restart(ibuf, &linebuf, &linesize, 0) < 0)
2057 goto jout;
2059 jnewname:
2060 if (namesize <= linesize)
2061 namebuf = srealloc(namebuf, namesize = linesize +1);
2062 for (cp = linebuf; *cp != '\0' && *cp != ' '; ++cp)
2064 for (; blankchar(*cp); ++cp)
2066 for (cp2 = namebuf + strlen(namebuf);
2067 *cp && !blankchar(*cp) && PTRCMP(cp2, <, namebuf + namesize -1);)
2068 *cp2++ = *cp++;
2069 *cp2 = '\0';
2071 if (readline_restart(ibuf, &linebuf, &linesize, 0) < 0)
2072 goto jout;
2073 if ((cp = strchr(linebuf, 'F')) == NULL)
2074 goto jout;
2075 if (strncmp(cp, "From", 4))
2076 goto jout;
2077 if (namesize <= linesize)
2078 namebuf = srealloc(namebuf, namesize = linesize + 1);
2080 while ((cp = strchr(cp, 'r')) != NULL) {
2081 if (!strncmp(cp, "remote", 6)) {
2082 if ((cp = strchr(cp, 'f')) == NULL)
2083 break;
2084 if (strncmp(cp, "from", 4) != 0)
2085 break;
2086 if ((cp = strchr(cp, ' ')) == NULL)
2087 break;
2088 cp++;
2089 if (f1st) {
2090 strncpy(namebuf, cp, namesize);
2091 f1st = 0;
2092 } else {
2093 cp2 = strrchr(namebuf, '!') + 1;
2094 strncpy(cp2, cp, PTR2SIZE(namebuf + namesize - cp2));
2096 namebuf[namesize - 2] = '!';
2097 namebuf[namesize - 1] = '\0';
2098 goto jnewname;
2100 cp++;
2102 jout:
2103 if (*namebuf != '\0' || ((cp = hfield1("return-path", mp))) == NULL ||
2104 *cp == '\0')
2105 cp = savestr(namebuf);
2107 if (linebuf != NULL)
2108 free(linebuf);
2109 free(namebuf);
2110 jleave:
2111 NYD_LEAVE;
2112 return cp;
2115 FL char const *
2116 subject_re_trim(char const *s){
2117 struct{
2118 ui8_t len;
2119 char dat[7];
2120 }const *pp, ignored[] = { /* Update *reply-strings* manual upon change! */
2121 {3, "re:"},
2122 {3, "aw:"}, {5, "antw:"}, /* de */
2123 {3, "wg:"}, /* Seen too often in the wild */
2124 {0, ""}
2127 bool_t any;
2128 char *re_st, *re_st_x;
2129 char const *orig_s;
2130 size_t re_l;
2131 NYD_ENTER;
2133 any = FAL0;
2134 orig_s = s;
2135 re_st = NULL;
2136 n_UNINIT(re_l, 0);
2138 if((re_st_x = ok_vlook(reply_strings)) != NULL &&
2139 (re_l = strlen(re_st_x)) > 0){
2140 re_st = n_lofi_alloc(++re_l * 2);
2141 memcpy(re_st, re_st_x, re_l);
2144 jouter:
2145 while(*s != '\0'){
2146 while(spacechar(*s))
2147 ++s;
2149 for(pp = ignored; pp->len > 0; ++pp)
2150 if(is_asccaseprefix(pp->dat, s)){
2151 s += pp->len;
2152 any = TRU1;
2153 goto jouter;
2156 if(re_st != NULL){
2157 char *cp;
2159 memcpy(re_st_x = &re_st[re_l], re_st, re_l);
2160 while((cp = n_strsep(&re_st_x, ',', TRU1)) != NULL)
2161 if(is_asccaseprefix(cp, s)){
2162 s += strlen(cp);
2163 any = TRU1;
2164 goto jouter;
2167 break;
2170 if(re_st != NULL)
2171 n_lofi_free(re_st);
2172 NYD_LEAVE;
2173 return any ? s : orig_s;
2176 FL int
2177 msgidcmp(char const *s1, char const *s2)
2179 int q1 = 0, q2 = 0, c1, c2;
2180 NYD_ENTER;
2182 while(*s1 == '<')
2183 ++s1;
2184 while(*s2 == '<')
2185 ++s2;
2187 do {
2188 c1 = msgidnextc(&s1, &q1);
2189 c2 = msgidnextc(&s2, &q2);
2190 if (c1 != c2)
2191 break;
2192 } while (c1 && c2);
2193 NYD_LEAVE;
2194 return c1 - c2;
2197 FL char const *
2198 fakefrom(struct message *mp)
2200 char const *name;
2201 NYD_ENTER;
2203 if (((name = skin(hfield1("return-path", mp))) == NULL || *name == '\0' ) &&
2204 ((name = skin(hfield1("from", mp))) == NULL || *name == '\0'))
2205 /* XXX MAILER-DAEMON is what an old MBOX manual page says.
2206 * RFC 4155 however requires a RFC 5322 (2822) conforming
2207 * "addr-spec", but we simply can't provide that */
2208 name = "MAILER-DAEMON";
2209 NYD_LEAVE;
2210 return name;
2213 FL char const *
2214 fakedate(time_t t)
2216 char *cp, *cq;
2217 NYD_ENTER;
2219 cp = ctime(&t);
2220 for (cq = cp; *cq != '\0' && *cq != '\n'; ++cq)
2222 *cq = '\0';
2223 cp = savestr(cp);
2224 NYD_LEAVE;
2225 return cp;
2228 #ifdef HAVE_IMAP_SEARCH
2229 FL time_t
2230 unixtime(char const *fromline)
2232 char const *fp, *xp;
2233 time_t t;
2234 si32_t i, year, month, day, hour, minute, second, tzdiff;
2235 struct tm *tmptr;
2236 NYD2_ENTER;
2238 for (fp = fromline; *fp != '\0' && *fp != '\n'; ++fp)
2240 fp -= 24;
2241 if (PTR2SIZE(fp - fromline) < 7)
2242 goto jinvalid;
2243 if (fp[3] != ' ')
2244 goto jinvalid;
2245 for (i = 0;;) {
2246 if (!strncmp(fp + 4, n_month_names[i], 3))
2247 break;
2248 if (n_month_names[++i][0] == '\0')
2249 goto jinvalid;
2251 month = i + 1;
2252 if (fp[7] != ' ')
2253 goto jinvalid;
2254 n_idec_si32_cp(&day, &fp[8], 10, &xp);
2255 if (*xp != ' ' || xp != fp + 10)
2256 goto jinvalid;
2257 n_idec_si32_cp(&hour, &fp[11], 10, &xp);
2258 if (*xp != ':' || xp != fp + 13)
2259 goto jinvalid;
2260 n_idec_si32_cp(&minute, &fp[14], 10, &xp);
2261 if (*xp != ':' || xp != fp + 16)
2262 goto jinvalid;
2263 n_idec_si32_cp(&second, &fp[17], 10, &xp);
2264 if (*xp != ' ' || xp != fp + 19)
2265 goto jinvalid;
2266 n_idec_si32_cp(&year, &fp[20], 10, &xp);
2267 if (xp != fp + 24)
2268 goto jinvalid;
2269 if ((t = combinetime(year, month, day, hour, minute, second)) == (time_t)-1)
2270 goto jinvalid;
2271 tzdiff = t - mktime(gmtime(&t));
2272 tmptr = localtime(&t);
2273 if (tmptr->tm_isdst > 0)
2274 tzdiff += 3600;
2275 t -= tzdiff;
2276 jleave:
2277 NYD2_LEAVE;
2278 return t;
2279 jinvalid:
2280 t = n_time_epoch();
2281 goto jleave;
2283 #endif /* HAVE_IMAP_SEARCH */
2285 FL time_t
2286 rfctime(char const *date) /* TODO n_idec_ return tests */
2288 char const *cp, *x;
2289 time_t t;
2290 si32_t i, year, month, day, hour, minute, second;
2291 NYD2_ENTER;
2293 cp = date;
2295 if ((cp = nexttoken(cp)) == NULL)
2296 goto jinvalid;
2297 if (alphachar(cp[0]) && alphachar(cp[1]) && alphachar(cp[2]) &&
2298 cp[3] == ',') {
2299 if ((cp = nexttoken(&cp[4])) == NULL)
2300 goto jinvalid;
2302 n_idec_si32_cp(&day, cp, 10, &x);
2303 if ((cp = nexttoken(x)) == NULL)
2304 goto jinvalid;
2305 for (i = 0;;) {
2306 if (!strncmp(cp, n_month_names[i], 3))
2307 break;
2308 if (n_month_names[++i][0] == '\0')
2309 goto jinvalid;
2311 month = i + 1;
2312 if ((cp = nexttoken(&cp[3])) == NULL)
2313 goto jinvalid;
2314 /* RFC 5322, 4.3:
2315 * Where a two or three digit year occurs in a date, the year is to be
2316 * interpreted as follows: If a two digit year is encountered whose
2317 * value is between 00 and 49, the year is interpreted by adding 2000,
2318 * ending up with a value between 2000 and 2049. If a two digit year
2319 * is encountered with a value between 50 and 99, or any three digit
2320 * year is encountered, the year is interpreted by adding 1900 */
2321 n_idec_si32_cp(&year, cp, 10, &x);
2322 i = (int)PTR2SIZE(x - cp);
2323 if (i == 2 && year >= 0 && year <= 49)
2324 year += 2000;
2325 else if (i == 3 || (i == 2 && year >= 50 && year <= 99))
2326 year += 1900;
2327 if ((cp = nexttoken(x)) == NULL)
2328 goto jinvalid;
2329 n_idec_si32_cp(&hour, cp, 10, &x);
2330 if (*x != ':')
2331 goto jinvalid;
2332 cp = &x[1];
2333 n_idec_si32_cp(&minute, cp, 10, &x);
2334 if (*x == ':') {
2335 cp = &x[1];
2336 n_idec_si32_cp(&second, cp, 10, &x);
2337 } else
2338 second = 0;
2340 if ((t = combinetime(year, month, day, hour, minute, second)) == (time_t)-1)
2341 goto jinvalid;
2342 if ((cp = nexttoken(x)) != NULL) {
2343 char buf[3];
2344 int sign = 1;
2346 switch (*cp) {
2347 case '+':
2348 sign = -1;
2349 /* FALLTHRU */
2350 case '-':
2351 ++cp;
2352 break;
2354 if (digitchar(cp[0]) && digitchar(cp[1]) && digitchar(cp[2]) &&
2355 digitchar(cp[3])) {
2356 si64_t tadj;
2358 buf[2] = '\0';
2359 buf[0] = cp[0];
2360 buf[1] = cp[1];
2361 n_idec_si32_cp(&i, buf, 10, NULL);
2362 tadj = (si64_t)i * 3600; /* XXX */
2363 buf[0] = cp[2];
2364 buf[1] = cp[3];
2365 n_idec_si32_cp(&i, buf, 10, NULL);
2366 tadj += (si64_t)i * 60; /* XXX */
2367 if (sign < 0)
2368 tadj = -tadj;
2369 t += (time_t)tadj;
2371 /* TODO WE DO NOT YET PARSE (OBSOLETE) ZONE NAMES
2372 * TODO once again, Christos Zoulas and NetBSD Mail have done
2373 * TODO a really good job already, but using strptime(3), which
2374 * TODO is not portable. Nonetheless, WE must improve, not
2375 * TODO at last because we simply ignore obsolete timezones!!
2376 * TODO See RFC 5322, 4.3! */
2378 jleave:
2379 NYD2_LEAVE;
2380 return t;
2381 jinvalid:
2382 t = 0;
2383 goto jleave;
2386 FL time_t
2387 combinetime(int year, int month, int day, int hour, int minute, int second){
2388 size_t const jdn_epoch = 2440588;
2389 bool_t const y2038p = (sizeof(time_t) == 4);
2391 size_t jdn;
2392 time_t t;
2393 NYD2_ENTER;
2395 if(UICMP(32, second, >/*XXX leap=*/, DATE_SECSMIN) ||
2396 UICMP(32, minute, >=, DATE_MINSHOUR) ||
2397 UICMP(32, hour, >=, DATE_HOURSDAY) ||
2398 day < 1 || day > 31 ||
2399 month < 1 || month > 12 ||
2400 year < 1970)
2401 goto jerr;
2403 if(year >= 1970 + ((y2038p ? SI32_MAX : SI64_MAX) /
2404 (DATE_SECSDAY * DATE_DAYSYEAR))){
2405 /* Be a coward regarding Y2038, many people (mostly myself, that is) do
2406 * test by stepping second-wise around the flip. Don't care otherwise */
2407 if(!y2038p)
2408 goto jerr;
2409 if(year > 2038 || month > 1 || day > 19 ||
2410 hour > 3 || minute > 14 || second > 7)
2411 goto jerr;
2414 t = second;
2415 t += minute * DATE_SECSMIN;
2416 t += hour * DATE_SECSHOUR;
2418 jdn = a_head_gregorian_to_jdn(year, month, day);
2419 jdn -= jdn_epoch;
2420 t += (time_t)jdn * DATE_SECSDAY;
2421 jleave:
2422 NYD2_LEAVE;
2423 return t;
2424 jerr:
2425 t = (time_t)-1;
2426 goto jleave;
2429 FL void
2430 substdate(struct message *m)
2432 char const *cp;
2433 NYD_ENTER;
2435 /* Determine the date to print in faked 'From ' lines. This is traditionally
2436 * the date the message was written to the mail file. Try to determine this
2437 * using RFC message header fields, or fall back to current time */
2438 if ((cp = hfield1("received", m)) != NULL) {
2439 while ((cp = nexttoken(cp)) != NULL && *cp != ';') {
2441 ++cp;
2442 while (alnumchar(*cp));
2444 if (cp && *++cp)
2445 m->m_time = rfctime(cp);
2447 if (m->m_time == 0 || m->m_time > time_current.tc_time) {
2448 if ((cp = hfield1("date", m)) != NULL)
2449 m->m_time = rfctime(cp);
2451 if (m->m_time == 0 || m->m_time > time_current.tc_time)
2452 m->m_time = time_current.tc_time;
2453 NYD_LEAVE;
2456 FL void
2457 setup_from_and_sender(struct header *hp)
2459 char const *addr;
2460 struct name *np;
2461 NYD_ENTER;
2463 /* If -t parsed or composed From: then take it. With -t we otherwise
2464 * want -r to be honoured in favour of *from* in order to have
2465 * a behaviour that is compatible with what users would expect from e.g.
2466 * postfix(1) */
2467 if ((np = hp->h_from) != NULL ||
2468 ((n_psonce & n_PSO_t_FLAG) && (np = n_poption_arg_r) != NULL)) {
2470 } else if ((addr = myaddrs(hp)) != NULL)
2471 np = lextract(addr, GEXTRA | GFULL | GFULLEXTRA);
2472 hp->h_from = np;
2474 if ((np = hp->h_sender) != NULL) {
2476 } else if ((addr = ok_vlook(sender)) != NULL)
2477 np = lextract(addr, GEXTRA | GFULL | GFULLEXTRA);
2478 hp->h_sender = np;
2480 NYD_LEAVE;
2483 FL struct name const *
2484 check_from_and_sender(struct name const *fromfield,
2485 struct name const *senderfield)
2487 struct name const *rv = NULL;
2488 NYD_ENTER;
2490 if (senderfield != NULL) {
2491 if (senderfield->n_flink != NULL) {
2492 n_err(_("The Sender: field may contain only one address\n"));
2493 goto jleave;
2495 rv = senderfield;
2498 if (fromfield != NULL) {
2499 if (fromfield->n_flink != NULL && senderfield == NULL) {
2500 n_err(_("A Sender: is required when there are multiple "
2501 "addresses in From:\n"));
2502 goto jleave;
2504 if (rv == NULL)
2505 rv = fromfield;
2508 if (rv == NULL)
2509 rv = (struct name*)0x1;
2510 jleave:
2511 NYD_LEAVE;
2512 return rv;
2515 #ifdef HAVE_XSSL
2516 FL char *
2517 getsender(struct message *mp)
2519 char *cp;
2520 struct name *np;
2521 NYD_ENTER;
2523 if ((cp = hfield1("from", mp)) == NULL ||
2524 (np = lextract(cp, GEXTRA | GSKIN)) == NULL)
2525 cp = NULL;
2526 else
2527 cp = (np->n_flink != NULL) ? skin(hfield1("sender", mp)) : np->n_name;
2528 NYD_LEAVE;
2529 return cp;
2531 #endif
2533 FL int
2534 grab_headers(enum n_lexinput_flags lif, struct header *hp, enum gfield gflags,
2535 int subjfirst)
2537 /* TODO grab_headers: again, check counts etc. against RFC;
2538 * TODO (now assumes check_from_and_sender() is called afterwards ++ */
2539 int errs;
2540 int volatile comma;
2541 NYD_ENTER;
2543 errs = 0;
2544 comma = (ok_blook(bsdcompat) || ok_blook(bsdmsgs)) ? 0 : GCOMMA;
2546 if (gflags & GTO)
2547 hp->h_to = grab_names(lif, "To: ", hp->h_to, comma, GTO | GFULL);
2548 if (subjfirst && (gflags & GSUBJECT))
2549 hp->h_subject = n_lex_input_cp(lif, "Subject: ", hp->h_subject);
2550 if (gflags & GCC)
2551 hp->h_cc = grab_names(lif, "Cc: ", hp->h_cc, comma, GCC | GFULL);
2552 if (gflags & GBCC)
2553 hp->h_bcc = grab_names(lif, "Bcc: ", hp->h_bcc, comma, GBCC | GFULL);
2555 if (gflags & GEXTRA) {
2556 if (hp->h_from == NULL)
2557 hp->h_from = lextract(myaddrs(hp), GEXTRA | GFULL | GFULLEXTRA);
2558 hp->h_from = grab_names(lif, "From: ", hp->h_from, comma,
2559 GEXTRA | GFULL | GFULLEXTRA);
2560 if (hp->h_replyto == NULL)
2561 hp->h_replyto = lextract(ok_vlook(replyto), GEXTRA | GFULL);
2562 hp->h_replyto = grab_names(lif, "Reply-To: ", hp->h_replyto, comma,
2563 GEXTRA | GFULL);
2564 if (hp->h_sender == NULL)
2565 hp->h_sender = extract(ok_vlook(sender), GEXTRA | GFULL);
2566 hp->h_sender = grab_names(lif, "Sender: ", hp->h_sender, comma,
2567 GEXTRA | GFULL);
2570 if (!subjfirst && (gflags & GSUBJECT))
2571 hp->h_subject = n_lex_input_cp(lif, "Subject: ", hp->h_subject);
2573 NYD_LEAVE;
2574 return errs;
2577 FL bool_t
2578 header_match(struct message *mp, struct search_expr const *sep)
2580 struct str in, out;
2581 FILE *ibuf;
2582 int lc;
2583 size_t linesize = 0; /* TODO line pool */
2584 char *linebuf = NULL, *colon;
2585 bool_t rv = FAL0;
2586 NYD_ENTER;
2588 if ((ibuf = setinput(&mb, mp, NEED_HEADER)) == NULL)
2589 goto jleave;
2590 if ((lc = mp->m_lines - 1) < 0)
2591 goto jleave;
2593 if ((mp->m_flag & MNOFROM) == 0 &&
2594 readline_restart(ibuf, &linebuf, &linesize, 0) < 0)
2595 goto jleave;
2596 while (lc > 0) {
2597 if (gethfield(ibuf, &linebuf, &linesize, lc, &colon) <= 0)
2598 break;
2599 if (blankchar(*++colon))
2600 ++colon;
2601 in.l = strlen(in.s = colon);
2602 mime_fromhdr(&in, &out, TD_ICONV);
2603 #ifdef HAVE_REGEX
2604 if (sep->ss_sexpr == NULL)
2605 rv = (regexec(&sep->ss_regex, out.s, 0,NULL, 0) != REG_NOMATCH);
2606 else
2607 #endif
2608 rv = substr(out.s, sep->ss_sexpr);
2609 free(out.s);
2610 if (rv)
2611 break;
2614 jleave:
2615 if (linebuf != NULL)
2616 free(linebuf);
2617 NYD_LEAVE;
2618 return rv;
2621 FL struct n_header_field *
2622 n_customhdr_query(void){
2623 char const *vp;
2624 struct n_header_field *rv, **tail, *hfp;
2625 NYD_ENTER;
2627 rv = NULL;
2629 if((vp = ok_vlook(customhdr)) != NULL){
2630 char *buf;
2632 tail = &rv;
2633 buf = savestr(vp);
2634 jch_outer:
2635 while((vp = a_head_customhdr__sep(&buf)) != NULL){
2636 ui32_t nl, bl;
2637 char const *nstart, *cp;
2639 for(nstart = cp = vp;; ++cp){
2640 if(fieldnamechar(*cp))
2641 continue;
2642 if(*cp == '\0'){
2643 if(cp == nstart){
2644 n_err(_("Invalid nameless *customhdr* entry\n"));
2645 goto jch_outer;
2647 }else if(*cp != ':' && !blankchar(*cp)){
2648 jch_badent:
2649 n_err(_("Invalid *customhdr* entry: %s\n"), vp);
2650 goto jch_outer;
2652 break;
2654 nl = (ui32_t)PTR2SIZE(cp - nstart);
2656 while(blankchar(*cp))
2657 ++cp;
2658 if(*cp++ != ':')
2659 goto jch_badent;
2660 while(blankchar(*cp))
2661 ++cp;
2662 bl = (ui32_t)strlen(cp) +1;
2664 *tail =
2665 hfp = salloc(n_VSTRUCT_SIZEOF(struct n_header_field, hf_dat) +
2666 nl +1 + bl);
2667 tail = &hfp->hf_next;
2668 hfp->hf_next = NULL;
2669 hfp->hf_nl = nl;
2670 hfp->hf_bl = bl - 1;
2671 memcpy(hfp->hf_dat, nstart, nl);
2672 hfp->hf_dat[nl++] = '\0';
2673 memcpy(hfp->hf_dat + nl, cp, bl);
2676 NYD_LEAVE;
2677 return rv;
2680 /* s-it-mode */