MLE, mle-complete: do not start new line for each non-iswalnum(3)
[s-mailx.git] / head.c
blobcf650a6586b9add67833dfcc353295928ed1c592
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 else{
519 /* If we seem to know that this is an address. Ensure this is correct
520 * according to RFC 5322 TODO the entire address parser should be like
521 * TODO that for one, and then we should now whether structured or
522 * TODO unstructured, and just parse correctly overall!
523 * TODO In addition, this can be optimised a lot */
524 struct a_token{
525 struct a_token *t_last;
526 struct a_token *t_next;
527 enum{
528 a_T_TATOM = 1<<0,
529 a_T_TCOMM = 1<<1,
530 a_T_TQUOTE = 1<<2,
531 a_T_TADDR = 1<<3,
532 a_T_TMASK = (1<<4) - 1,
534 a_T_SPECIAL = 1<<8 /* An atom actually needs to go TQUOTE */
535 } t_f;
536 ui8_t t__pad[4];
537 size_t t_start;
538 size_t t_end;
539 } *thead, *tcurr, *tp;
541 struct n_string ost, *ostp;
542 char const *cp, *cp1st, *cpmax, *xp;
543 void *lofi_snap;
545 /* Name and domain must be non-empty */
546 if(*addr == '@' || &addr[2] >= p || p[-2] == '@'){
547 c.c = '@';
548 NAME_ADDRSPEC_ERR_SET(agp->ag_n_flags, NAME_ADDRSPEC_ERR_ATSEQ, c.u);
549 goto jleave;
552 #ifdef HAVE_IDNA
553 if(use_idna == 2)
554 agp = a_head_idna_apply(agp);
555 #endif
557 cp = agp->ag_input;
559 /* Nothing to do if there is only an address (in angle brackets) */
560 if(agp->ag_iaddr_start == 0){
561 if(agp->ag_iaddr_aend == agp->ag_ilen)
562 goto jleave;
563 }else if(agp->ag_iaddr_start == 1 && *cp == '<' &&
564 agp->ag_iaddr_aend == agp->ag_ilen - 1 &&
565 cp[agp->ag_iaddr_aend] == '>')
566 goto jleave;
568 /* It is not, so parse off all tokens, then resort and rejoin */
569 lofi_snap = n_lofi_snap_create();
571 cp1st = cp;
572 if((c.ui32 = agp->ag_iaddr_start) > 0)
573 --c.ui32;
574 cpmax = &cp[c.ui32];
576 thead = tcurr = NULL;
577 hadat = FAL0;
578 jnode_redo:
579 for(tp = NULL; cp < cpmax;){
580 switch((c.c = *cp)){
581 case '(':
582 if(tp != NULL)
583 tp->t_end = PTR2SIZE(cp - cp1st);
584 tp = n_lofi_alloc(sizeof *tp);
585 tp->t_next = NULL;
586 if((tp->t_last = tcurr) != NULL)
587 tcurr->t_next = tp;
588 else
589 thead = tp;
590 tcurr = tp;
591 tp->t_f = a_T_TCOMM;
592 tp->t_start = PTR2SIZE(++cp - cp1st);
593 xp = skip_comment(cp);
594 tp->t_end = PTR2SIZE(xp - cp1st);
595 cp = xp;
596 if(tp->t_end > tp->t_start){
597 if(xp[-1] == ')')
598 --tp->t_end;
599 else{
600 /* No closing comment - strip trailing whitespace */
601 while(blankchar(*--xp))
602 if(--tp->t_end == tp->t_start)
603 break;
606 tp = NULL;
607 break;
609 case '"':
610 if(tp != NULL)
611 tp->t_end = PTR2SIZE(cp - cp1st);
612 tp = n_lofi_alloc(sizeof *tp);
613 tp->t_next = NULL;
614 if((tp->t_last = tcurr) != NULL)
615 tcurr->t_next = tp;
616 else
617 thead = tp;
618 tcurr = tp;
619 tp->t_f = a_T_TQUOTE;
620 tp->t_start = PTR2SIZE(++cp - cp1st);
621 for(xp = cp; xp < cpmax; ++xp){
622 if((c.c = *xp) == '"')
623 break;
624 if(c.c == '\\' && xp[1] != '\0')
625 ++xp;
627 tp->t_end = PTR2SIZE(xp - cp1st);
628 cp = &xp[1];
629 if(tp->t_end > tp->t_start){
630 /* No closing quote - strip trailing whitespace */
631 if(*xp != '"'){
632 while(blankchar(*xp--))
633 if(--tp->t_end == tp->t_start)
634 break;
637 tp = NULL;
638 break;
640 default:
641 if(blankchar(c.c)){
642 if(tp != NULL)
643 tp->t_end = PTR2SIZE(cp - cp1st);
644 tp = NULL;
645 ++cp;
646 break;
649 if(tp == NULL){
650 tp = n_lofi_alloc(sizeof *tp);
651 tp->t_next = NULL;
652 if((tp->t_last = tcurr) != NULL)
653 tcurr->t_next = tp;
654 else
655 thead = tp;
656 tcurr = tp;
657 tp->t_f = a_T_TATOM;
658 tp->t_start = PTR2SIZE(cp - cp1st);
660 ++cp;
662 /* Reverse solidus transforms the following into a quoted-pair, and
663 * therefore (must occur in comment or quoted-string only) the
664 * entire atom into a quoted string */
665 if(c.c == '\\'){
666 tp->t_f |= a_T_SPECIAL;
667 if(cp < cpmax)
668 ++cp;
670 /* Is this plain RFC 5322 "atext", or "specials"? Because we don't
671 * TODO know structured/unstructured, nor anything else, we need to
672 * TODO treat "dot-atom" as being identical to "specials" */
673 else if(!alnumchar(c.c) &&
674 c.c != '!' && c.c != '#' && c.c != '$' && c.c != '%' &&
675 c.c != '&' && c.c != '\'' && c.c != '*' && 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 tp->t_f |= a_T_SPECIAL;
680 break;
683 if(tp != NULL)
684 tp->t_end = PTR2SIZE(cp - cp1st);
686 if(hadat == FAL0){
687 hadat = TRU1;
688 tp = n_lofi_alloc(sizeof *tp);
689 tp->t_next = NULL;
690 if((tp->t_last = tcurr) != NULL)
691 tcurr->t_next = tp;
692 else
693 thead = tp;
694 tcurr = tp;
695 tp->t_f = a_T_TADDR;
696 tp->t_start = agp->ag_iaddr_start;
697 tp->t_end = agp->ag_iaddr_aend;
698 tp = NULL;
700 cp = &agp->ag_input[agp->ag_iaddr_aend + 1];
701 cpmax = &agp->ag_input[agp->ag_ilen];
702 if(cp < cpmax)
703 goto jnode_redo;
706 /* Nothing may follow the address, move it to the end */
707 if(!(tcurr->t_f & a_T_TADDR)){
708 for(tp = thead; tp != NULL; tp = tp->t_next)
709 if(tp->t_f & a_T_TADDR){
710 if(tp->t_last != NULL)
711 tp->t_last->t_next = tp->t_next;
712 else
713 thead = tp->t_next;
714 if(tp->t_next != NULL)
715 tp->t_next->t_last = tp->t_last;
717 tcurr = tp;
718 while(tp->t_next != NULL)
719 tp = tp->t_next;
720 tp->t_next = tcurr;
721 tcurr->t_last = tp;
722 tcurr->t_next = NULL;
723 break;
727 /* Make ranges contiguous: ensure a continuous range of atoms is converted
728 * to a SPECIAL one if at least one of them requires it */
729 for(tp = thead; tp != NULL; tp = tp->t_next){
730 if(tp->t_f & a_T_SPECIAL){
731 tcurr = tp;
732 while((tp = tp->t_last) != NULL && (tp->t_f & a_T_TATOM))
733 tp->t_f |= a_T_SPECIAL;
734 tp = tcurr;
735 while((tp = tp->t_next) != NULL && (tp->t_f & a_T_TATOM))
736 tp->t_f |= a_T_SPECIAL;
739 /* And yes, we want quotes to extend as much as possible */
740 for(tp = thead; tp != NULL; tp = tp->t_next){
741 if(tp->t_f & a_T_TQUOTE){
742 tcurr = tp;
743 while((tp = tp->t_last) != NULL && (tp->t_f & a_T_TATOM))
744 tp->t_f |= a_T_SPECIAL;
745 tp = tcurr;
746 while((tp = tp->t_next) != NULL && (tp->t_f & a_T_TATOM))
747 tp->t_f |= a_T_SPECIAL;
751 /* Then rejoin */
752 ostp = n_string_creat_auto(&ost);
753 if((c.ui32 = agp->ag_ilen) <= UI32_MAX >> 1)
754 ostp = n_string_reserve(ostp, c.ui32 <<= 1);
756 for(tcurr = thead; tcurr != NULL;){
757 if(tcurr != thead)
758 ostp = n_string_push_c(ostp, ' ');
759 if(tcurr->t_f & a_T_TADDR){
760 ostp = n_string_push_c(ostp, '<');
761 agp->ag_iaddr_start = ostp->s_len;
762 ostp = n_string_push_buf(ostp, &cp1st[tcurr->t_start],
763 (tcurr->t_end - tcurr->t_start));
764 agp->ag_iaddr_aend = ostp->s_len;
765 ostp = n_string_push_c(ostp, '>');
766 tcurr = tcurr->t_next;
767 }else if(tcurr->t_f & a_T_TCOMM){
768 ostp = n_string_push_c(ostp, '(');
769 ostp = n_string_push_buf(ostp, &cp1st[tcurr->t_start],
770 (tcurr->t_end - tcurr->t_start));
771 while((tp = tcurr->t_next) != NULL && (tp->t_f & a_T_TCOMM)){
772 tcurr = tp;
773 ostp = n_string_push_c(ostp, ' ');
774 ostp = n_string_push_buf(ostp, &cp1st[tcurr->t_start],
775 (tcurr->t_end - tcurr->t_start));
777 ostp = n_string_push_c(ostp, ')');
778 tcurr = tcurr->t_next;
779 }else if(tcurr->t_f & a_T_TQUOTE){
780 jput_quote:
781 ostp = n_string_push_c(ostp, '"');
782 tp = tcurr;
783 do/* while tcurr && TATOM||TQUOTE */{
784 if(tcurr != tp)
785 ostp = n_string_push_c(ostp, ' ');
786 if((tcurr->t_f & (a_T_TATOM | a_T_SPECIAL)) == a_T_TATOM)
787 ostp = n_string_push_buf(ostp, &cp1st[tcurr->t_start],
788 (tcurr->t_end - tcurr->t_start));
789 else{
790 bool_t esc;
792 cp = &cp1st[tcurr->t_start];
793 cpmax = &cp1st[tcurr->t_end];
795 for(esc = FAL0; cp < cpmax;){
796 if((c.c = *cp++) == '\\' && !esc)
797 esc = TRU1;
798 else{
799 if(esc || c.c == '"'){
800 jput_quote_esc:
801 ostp = n_string_push_c(ostp, '\\');
803 ostp = n_string_push_c(ostp, c.c);
804 esc = FAL0;
807 if(esc){
808 c.c = '\\';
809 goto jput_quote_esc;
812 }while((tcurr = tcurr->t_next) != NULL &&
813 (tcurr->t_f & (a_T_TATOM | a_T_TQUOTE)));
814 ostp = n_string_push_c(ostp, '"');
815 }else if(tcurr->t_f & a_T_SPECIAL)
816 goto jput_quote;
817 else{
818 /* Can we use a fast join mode? */
819 for(tp = tcurr; tcurr != NULL; tcurr = tcurr->t_next){
820 if(!(tcurr->t_f & a_T_TATOM))
821 break;
822 if(tcurr != tp)
823 ostp = n_string_push_c(ostp, ' ');
824 ostp = n_string_push_buf(ostp, &cp1st[tcurr->t_start],
825 (tcurr->t_end - tcurr->t_start));
830 n_lofi_snap_unroll(lofi_snap);
832 agp->ag_input = n_string_cp(ostp);
833 agp->ag_ilen = ostp->s_len;
834 ostp = n_string_drop_ownership(ostp);
836 jleave:
837 NYD_LEAVE;
838 return ((agp->ag_n_flags & NAME_ADDRSPEC_INVALID) == 0);
841 static int
842 gethfield(FILE *f, char **linebuf, size_t *linesize, int rem, char **colon)
844 char *line2 = NULL, *cp, *cp2;
845 size_t line2size = 0;
846 int c, isenc;
847 NYD2_ENTER;
849 if (*linebuf == NULL)
850 *linebuf = srealloc(*linebuf, *linesize = 1);
851 **linebuf = '\0';
852 for (;;) {
853 if (--rem < 0) {
854 rem = -1;
855 break;
857 if ((c = readline_restart(f, linebuf, linesize, 0)) <= 0) {
858 rem = -1;
859 break;
861 for (cp = *linebuf; fieldnamechar(*cp); ++cp)
863 if (cp > *linebuf)
864 while (blankchar(*cp))
865 ++cp;
866 if (*cp != ':' || cp == *linebuf)
867 continue;
869 /* I guess we got a headline. Handle wraparound */
870 *colon = cp;
871 cp = *linebuf + c;
872 for (;;) {
873 isenc = 0;
874 while (PTRCMP(--cp, >=, *linebuf) && blankchar(*cp))
876 cp++;
877 if (rem <= 0)
878 break;
879 if (PTRCMP(cp - 8, >=, *linebuf) && cp[-1] == '=' && cp[-2] == '?')
880 isenc |= 1;
881 ungetc(c = getc(f), f);
882 if (!blankchar(c))
883 break;
884 c = readline_restart(f, &line2, &line2size, 0);
885 if (c < 0)
886 break;
887 --rem;
888 for (cp2 = line2; blankchar(*cp2); ++cp2)
890 c -= (int)PTR2SIZE(cp2 - line2);
891 if (cp2[0] == '=' && cp2[1] == '?' && c > 8)
892 isenc |= 2;
893 if (PTRCMP(cp + c, >=, *linebuf + *linesize - 2)) {
894 size_t diff = PTR2SIZE(cp - *linebuf),
895 colondiff = PTR2SIZE(*colon - *linebuf);
896 *linebuf = srealloc(*linebuf, *linesize += c + 2);
897 cp = &(*linebuf)[diff];
898 *colon = &(*linebuf)[colondiff];
900 if (isenc != 3)
901 *cp++ = ' ';
902 memcpy(cp, cp2, c);
903 cp += c;
905 *cp = '\0';
907 if (line2 != NULL)
908 free(line2);
909 break;
911 NYD2_LEAVE;
912 return rem;
915 static int
916 msgidnextc(char const **cp, int *status)
918 int c;
919 NYD2_ENTER;
921 assert(cp != NULL);
922 assert(*cp != NULL);
923 assert(status != NULL);
925 for (;;) {
926 if (*status & 01) {
927 if (**cp == '"') {
928 *status &= ~01;
929 (*cp)++;
930 continue;
932 if (**cp == '\\') {
933 (*cp)++;
934 if (**cp == '\0')
935 goto jeof;
937 goto jdfl;
939 switch (**cp) {
940 case '(':
941 *cp = skip_comment(&(*cp)[1]);
942 continue;
943 case '>':
944 case '\0':
945 jeof:
946 c = '\0';
947 goto jleave;
948 case '"':
949 (*cp)++;
950 *status |= 01;
951 continue;
952 case '@':
953 *status |= 02;
954 /*FALLTHRU*/
955 default:
956 jdfl:
957 c = *(*cp)++ & 0377;
958 c = (*status & 02) ? lowerconv(c) : c;
959 goto jleave;
962 jleave:
963 NYD2_LEAVE;
964 return c;
967 static int
968 charcount(char *str, int c)
970 char *cp;
971 int i;
972 NYD2_ENTER;
974 for (i = 0, cp = str; *cp; ++cp)
975 if (*cp == c)
976 ++i;
977 NYD2_LEAVE;
978 return i;
981 static char const *
982 nexttoken(char const *cp)
984 NYD2_ENTER;
985 for (;;) {
986 if (*cp == '\0') {
987 cp = NULL;
988 break;
991 if (*cp == '(') {
992 size_t nesting = 1;
994 do switch (*++cp) {
995 case '(':
996 ++nesting;
997 break;
998 case ')':
999 --nesting;
1000 break;
1001 } while (nesting > 0 && *cp != '\0'); /* XXX error? */
1002 } else if (blankchar(*cp) || *cp == ',')
1003 ++cp;
1004 else
1005 break;
1007 NYD2_LEAVE;
1008 return cp;
1011 static char *
1012 a_head_customhdr__sep(char **iolist){
1013 char *cp, c, *base;
1014 bool_t isesc, anyesc;
1015 NYD2_ENTER;
1017 for(base = *iolist; base != NULL; base = *iolist){
1018 while((c = *base) != '\0' && blankspacechar(c))
1019 ++base;
1021 for(isesc = anyesc = FAL0, cp = base;; ++cp){
1022 if(n_UNLIKELY((c = *cp) == '\0')){
1023 *iolist = NULL;
1024 break;
1025 }else if(!isesc){
1026 if(c == ','){
1027 *iolist = cp + 1;
1028 break;
1030 isesc = (c == '\\');
1031 }else{
1032 isesc = FAL0;
1033 anyesc |= (c == ',');
1037 while(cp > base && blankspacechar(cp[-1]))
1038 --cp;
1039 *cp = '\0';
1041 if(*base != '\0'){
1042 if(anyesc){
1043 char *ins;
1045 for(ins = cp = base;; ++ins)
1046 if((c = *cp) == '\\' && cp[1] == ','){
1047 *ins = ',';
1048 cp += 2;
1049 }else if((*ins = (++cp, c)) == '\0')
1050 break;
1052 break;
1055 NYD2_LEAVE;
1056 return base;
1059 FL char const *
1060 myaddrs(struct header *hp) /* TODO */
1062 struct name *np;
1063 char const *rv, *mta;
1064 NYD_ENTER;
1066 if (hp != NULL && (np = hp->h_from) != NULL) {
1067 if ((rv = np->n_fullname) != NULL)
1068 goto jleave;
1069 if ((rv = np->n_name) != NULL)
1070 goto jleave;
1073 if((rv = ok_vlook(from)) != NULL){
1074 if((np = lextract(rv, GEXTRA | GFULL)) == NULL)
1075 jefrom:
1076 n_err(_("An address given in *from* is invalid: %s\n"), rv);
1077 else for(; np != NULL; np = np->n_flink)
1078 if(is_addr_invalid(np, EACM_STRICT | EACM_NOLOG | EACM_NONAME))
1079 goto jefrom;
1080 goto jleave;
1083 /* When invoking *sendmail* directly, it's its task to generate an otherwise
1084 * undeterminable From: address. However, if the user sets *hostname*,
1085 * accept his desire */
1086 if (ok_vlook(hostname) != NULL)
1087 goto jnodename;
1088 if (ok_vlook(smtp) != NULL || /* TODO obsolete -> mta */
1089 /* TODO pretty hacky for now (this entire fun), later: url_creat()! */
1090 ((mta = ok_vlook(mta)) != NULL &&
1091 (mta = n_servbyname(mta, NULL)) != NULL && *mta != '\0'))
1092 goto jnodename;
1093 jleave:
1094 NYD_LEAVE;
1095 return rv;
1097 jnodename:{
1098 char *cp;
1099 char const *hn, *ln;
1100 size_t i;
1102 hn = nodename(1);
1103 ln = ok_vlook(LOGNAME);
1104 i = strlen(ln) + strlen(hn) + 1 +1;
1105 rv = cp = salloc(i);
1106 sstpcpy(sstpcpy(sstpcpy(cp, ln), "@"), hn);
1108 goto jleave;
1111 FL char const *
1112 myorigin(struct header *hp) /* TODO */
1114 char const *rv = NULL, *ccp;
1115 struct name *np;
1116 NYD_ENTER;
1118 if((ccp = myaddrs(hp)) != NULL &&
1119 (np = lextract(ccp, GEXTRA | GFULL)) != NULL){
1120 if(np->n_flink == NULL)
1121 rv = ccp;
1122 else if((ccp = ok_vlook(sender)) != NULL) {
1123 if((np = lextract(ccp, GEXTRA | GFULL)) == NULL ||
1124 np->n_flink != NULL ||
1125 is_addr_invalid(np, EACM_STRICT | EACM_NOLOG | EACM_NONAME))
1126 n_err(_("The address given in *sender* is invalid: %s\n"), ccp);
1127 else
1128 rv = ccp;
1131 NYD_LEAVE;
1132 return rv;
1135 FL bool_t
1136 is_head(char const *linebuf, size_t linelen, bool_t check_rfc4155)
1138 char date[FROM_DATEBUF];
1139 bool_t rv;
1140 NYD2_ENTER;
1142 if ((rv = (linelen >= 5 && !memcmp(linebuf, "From ", 5))) && check_rfc4155 &&
1143 (extract_date_from_from_(linebuf, linelen, date) <= 0 ||
1144 !_is_date(date)))
1145 rv = TRUM1;
1146 NYD2_LEAVE;
1147 return rv;
1150 FL int
1151 extract_date_from_from_(char const *line, size_t linelen,
1152 char datebuf[FROM_DATEBUF])
1154 int rv;
1155 char const *cp = line;
1156 NYD_ENTER;
1158 rv = 1;
1160 /* "From " */
1161 cp = _from__skipword(cp);
1162 if (cp == NULL)
1163 goto jerr;
1164 /* "addr-spec " */
1165 cp = _from__skipword(cp);
1166 if (cp == NULL)
1167 goto jerr;
1168 if (cp[0] == 't' && cp[1] == 't' && cp[2] == 'y') {
1169 cp = _from__skipword(cp);
1170 if (cp == NULL)
1171 goto jerr;
1173 /* It seems there are invalid MBOX archives in the wild, compare
1174 * . http://bugs.debian.org/624111
1175 * . [Mutt] #3868: mutt should error if the imported mailbox is invalid
1176 * What they do is that they obfuscate the address to "name at host",
1177 * and even "name at host dot dom dot dom. I think we should handle that */
1178 else if(cp[0] == 'a' && cp[1] == 't' && cp[2] == ' '){
1179 rv = -1;
1180 cp += 3;
1181 jat_dot:
1182 cp = _from__skipword(cp);
1183 if (cp == NULL)
1184 goto jerr;
1185 if(cp[0] == 'd' && cp[1] == 'o' && cp[2] == 't' && cp[3] == ' '){
1186 cp += 4;
1187 goto jat_dot;
1191 linelen -= PTR2SIZE(cp - line);
1192 if (linelen < _DATE_MINLEN)
1193 goto jerr;
1194 if (cp[linelen - 1] == '\n') {
1195 --linelen;
1196 /* (Rather IMAP/POP3 only) */
1197 if (cp[linelen - 1] == '\r')
1198 --linelen;
1199 if (linelen < _DATE_MINLEN)
1200 goto jerr;
1202 if (linelen >= FROM_DATEBUF)
1203 goto jerr;
1205 jleave:
1206 memcpy(datebuf, cp, linelen);
1207 datebuf[linelen] = '\0';
1208 NYD_LEAVE;
1209 return rv;
1210 jerr:
1211 cp = _("<Unknown date>");
1212 linelen = strlen(cp);
1213 if (linelen >= FROM_DATEBUF)
1214 linelen = FROM_DATEBUF;
1215 rv = 0;
1216 goto jleave;
1219 FL void
1220 extract_header(FILE *fp, struct header *hp, si8_t *checkaddr_err)
1222 /* See the prototype declaration for the hairy relationship of
1223 * n_poption&n_PO_t_FLAG and/or n_psonce&n_PSO_t_FLAG in here */
1224 struct n_header_field **hftail;
1225 struct header nh, *hq = &nh;
1226 char *linebuf = NULL /* TODO line pool */, *colon;
1227 size_t linesize = 0, seenfields = 0;
1228 int lc, c;
1229 char const *val, *cp;
1230 NYD_ENTER;
1232 memset(hq, 0, sizeof *hq);
1233 if ((n_psonce & n_PSO_t_FLAG) && (n_poption & n_PO_t_FLAG)) {
1234 hq->h_to = hp->h_to;
1235 hq->h_cc = hp->h_cc;
1236 hq->h_bcc = hp->h_bcc;
1238 hftail = &hq->h_user_headers;
1240 for (lc = 0; readline_restart(fp, &linebuf, &linesize, 0) > 0; ++lc)
1243 /* TODO yippieia, cat(check(lextract)) :-) */
1244 rewind(fp);
1245 while ((lc = gethfield(fp, &linebuf, &linesize, lc, &colon)) >= 0) {
1246 struct name *np;
1248 /* We explicitly allow EAF_NAME for some addressees since aliases are not
1249 * yet expanded when we parse these! */
1250 if ((val = thisfield(linebuf, "to")) != NULL) {
1251 ++seenfields;
1252 hq->h_to = cat(hq->h_to, checkaddrs(lextract(val, GTO | GFULL),
1253 EACM_NORMAL | EAF_NAME | EAF_MAYKEEP, checkaddr_err));
1254 } else if ((val = thisfield(linebuf, "cc")) != NULL) {
1255 ++seenfields;
1256 hq->h_cc = cat(hq->h_cc, checkaddrs(lextract(val, GCC | GFULL),
1257 EACM_NORMAL | EAF_NAME | EAF_MAYKEEP, checkaddr_err));
1258 } else if ((val = thisfield(linebuf, "bcc")) != NULL) {
1259 ++seenfields;
1260 hq->h_bcc = cat(hq->h_bcc, checkaddrs(lextract(val, GBCC | GFULL),
1261 EACM_NORMAL | EAF_NAME | EAF_MAYKEEP, checkaddr_err));
1262 } else if ((val = thisfield(linebuf, "from")) != NULL) {
1263 if (!(n_psonce & n_PSO_t_FLAG) || (n_poption & n_PO_t_FLAG)) {
1264 ++seenfields;
1265 hq->h_from = cat(hq->h_from,
1266 checkaddrs(lextract(val, GEXTRA | GFULL | GFULLEXTRA),
1267 EACM_STRICT, NULL));
1269 } else if ((val = thisfield(linebuf, "reply-to")) != NULL) {
1270 ++seenfields;
1271 hq->h_replyto = cat(hq->h_replyto,
1272 checkaddrs(lextract(val, GEXTRA | GFULL), EACM_STRICT, NULL));
1273 } else if ((val = thisfield(linebuf, "sender")) != NULL) {
1274 if (!(n_psonce & n_PSO_t_FLAG) || (n_poption & n_PO_t_FLAG)) {
1275 ++seenfields;
1276 hq->h_sender = cat(hq->h_sender, /* TODO cat? check! */
1277 checkaddrs(lextract(val, GEXTRA | GFULL | GFULLEXTRA),
1278 EACM_STRICT, NULL));
1279 } else
1280 goto jebadhead;
1281 } else if ((val = thisfield(linebuf, "subject")) != NULL ||
1282 (val = thisfield(linebuf, "subj")) != NULL) {
1283 ++seenfields;
1284 for (cp = val; blankchar(*cp); ++cp)
1286 hq->h_subject = (hq->h_subject != NULL)
1287 ? save2str(hq->h_subject, cp) : savestr(cp);
1289 /* The remaining are mostly hacked in and thus TODO -- at least in
1290 * TODO respect to their content checking */
1291 else if((val = thisfield(linebuf, "message-id")) != NULL){
1292 if(n_psonce & n_PSO_t_FLAG){
1293 np = checkaddrs(lextract(val, GREF),
1294 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG | EACM_NONAME,
1295 NULL);
1296 if (np == NULL || np->n_flink != NULL)
1297 goto jebadhead;
1298 ++seenfields;
1299 hq->h_message_id = np;
1300 }else
1301 goto jebadhead;
1302 }else if((val = thisfield(linebuf, "in-reply-to")) != NULL){
1303 if(n_psonce & n_PSO_t_FLAG){
1304 np = checkaddrs(lextract(val, GREF),
1305 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG | EACM_NONAME,
1306 NULL);
1307 ++seenfields;
1308 hq->h_in_reply_to = np;
1309 }else
1310 goto jebadhead;
1311 }else if((val = thisfield(linebuf, "references")) != NULL){
1312 if(n_psonce & n_PSO_t_FLAG){
1313 ++seenfields;
1314 /* TODO Limit number of references TODO better on parser side */
1315 hq->h_ref = cat(hq->h_ref, checkaddrs(extract(val, GREF),
1316 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG | EACM_NONAME,
1317 NULL));
1318 }else
1319 goto jebadhead;
1321 /* and that is very hairy */
1322 else if((val = thisfield(linebuf, "mail-followup-to")) != NULL){
1323 if(n_psonce & n_PSO_t_FLAG){
1324 ++seenfields;
1325 hq->h_mft = cat(hq->h_mft, checkaddrs(lextract(val, GEXTRA | GFULL),
1326 /*EACM_STRICT | TODO '/' valid!! | EACM_NOLOG | */EACM_NONAME,
1327 checkaddr_err));
1328 }else
1329 goto jebadhead;
1331 /* A free-form user header; gethfield() did some verification already.. */
1332 else{
1333 struct n_header_field *hfp;
1334 ui32_t nl, bl;
1335 char const *nstart;
1337 for(nstart = cp = linebuf;; ++cp)
1338 if(!fieldnamechar(*cp))
1339 break;
1340 nl = (ui32_t)PTR2SIZE(cp - nstart);
1342 while(blankchar(*cp))
1343 ++cp;
1344 if(*cp++ != ':'){
1345 jebadhead:
1346 n_err(_("Ignoring header field: %s\n"), linebuf);
1347 continue;
1349 while(blankchar(*cp))
1350 ++cp;
1351 bl = (ui32_t)strlen(cp) +1;
1353 ++seenfields;
1354 *hftail = hfp = salloc(n_VSTRUCT_SIZEOF(struct n_header_field, hf_dat
1355 ) + nl +1 + bl);
1356 hftail = &hfp->hf_next;
1357 hfp->hf_next = NULL;
1358 hfp->hf_nl = nl;
1359 hfp->hf_bl = bl - 1;
1360 memcpy(hfp->hf_dat, nstart, nl);
1361 hfp->hf_dat[nl++] = '\0';
1362 memcpy(hfp->hf_dat + nl, cp, bl);
1366 /* In case the blank line after the header has been edited out. Otherwise,
1367 * fetch the header separator */
1368 if (linebuf != NULL) {
1369 if (linebuf[0] != '\0') {
1370 for (cp = linebuf; *(++cp) != '\0';)
1372 fseek(fp, (long)-PTR2SIZE(1 + cp - linebuf), SEEK_CUR);
1373 } else {
1374 if ((c = getc(fp)) != '\n' && c != EOF)
1375 ungetc(c, fp);
1379 if (seenfields > 0 && (checkaddr_err == NULL || *checkaddr_err == 0)) {
1380 hp->h_to = hq->h_to;
1381 hp->h_cc = hq->h_cc;
1382 hp->h_bcc = hq->h_bcc;
1383 hp->h_from = hq->h_from;
1384 hp->h_replyto = hq->h_replyto;
1385 hp->h_sender = hq->h_sender;
1386 if (hq->h_subject != NULL || !(n_psonce & n_PSO_t_FLAG) ||
1387 !(n_poption & n_PO_t_FLAG))
1388 hp->h_subject = hq->h_subject;
1389 hp->h_user_headers = hq->h_user_headers;
1391 if (n_psonce & n_PSO_t_FLAG) {
1392 hp->h_ref = hq->h_ref;
1393 hp->h_message_id = hq->h_message_id;
1394 hp->h_in_reply_to = hq->h_in_reply_to;
1395 hp->h_mft = hq->h_mft;
1397 /* And perform additional validity checks so that we don't bail later
1398 * on TODO this is good and the place where this should occur,
1399 * TODO unfortunately a lot of other places do again and blabla */
1400 if (hp->h_from == NULL)
1401 hp->h_from = n_poption_arg_r;
1402 else if (hp->h_from->n_flink != NULL && hp->h_sender == NULL)
1403 hp->h_sender = lextract(ok_vlook(sender),
1404 GEXTRA | GFULL | GFULLEXTRA);
1406 } else
1407 n_err(_("Restoring deleted header lines\n"));
1409 if (linebuf != NULL)
1410 free(linebuf);
1411 NYD_LEAVE;
1414 FL char *
1415 hfield_mult(char const *field, struct message *mp, int mult)
1417 FILE *ibuf;
1418 int lc;
1419 struct str hfs;
1420 size_t linesize = 0; /* TODO line pool */
1421 char *linebuf = NULL, *colon;
1422 char const *hfield;
1423 NYD_ENTER;
1425 /* There are (spam) messages which have header bytes which are many KB when
1426 * joined, so resize a single heap storage until we are done if we shall
1427 * collect a field that may have multiple bodies; only otherwise use the
1428 * string dope directly */
1429 memset(&hfs, 0, sizeof hfs);
1431 if ((ibuf = setinput(&mb, mp, NEED_HEADER)) == NULL)
1432 goto jleave;
1433 if ((lc = mp->m_lines - 1) < 0)
1434 goto jleave;
1436 if ((mp->m_flag & MNOFROM) == 0 &&
1437 readline_restart(ibuf, &linebuf, &linesize, 0) < 0)
1438 goto jleave;
1439 while (lc > 0) {
1440 if ((lc = gethfield(ibuf, &linebuf, &linesize, lc, &colon)) < 0)
1441 break;
1442 if ((hfield = thisfield(linebuf, field)) != NULL && *hfield != '\0') {
1443 if (mult)
1444 n_str_add_buf(&hfs, hfield, strlen(hfield));
1445 else {
1446 hfs.s = savestr(hfield);
1447 break;
1452 jleave:
1453 if (linebuf != NULL)
1454 free(linebuf);
1455 if (mult && hfs.s != NULL) {
1456 colon = savestrbuf(hfs.s, hfs.l);
1457 free(hfs.s);
1458 hfs.s = colon;
1460 NYD_LEAVE;
1461 return hfs.s;
1464 FL char const *
1465 thisfield(char const *linebuf, char const *field)
1467 char const *rv = NULL;
1468 NYD2_ENTER;
1470 while (lowerconv(*linebuf) == lowerconv(*field)) {
1471 ++linebuf;
1472 ++field;
1474 if (*field != '\0')
1475 goto jleave;
1477 while (blankchar(*linebuf))
1478 ++linebuf;
1479 if (*linebuf++ != ':')
1480 goto jleave;
1482 while (blankchar(*linebuf)) /* TODO header parser.. strip trailing WS?!? */
1483 ++linebuf;
1484 rv = linebuf;
1485 jleave:
1486 NYD2_LEAVE;
1487 return rv;
1490 FL char *
1491 nameof(struct message *mp, int reptype)
1493 char *cp, *cp2;
1494 NYD_ENTER;
1496 cp = skin(name1(mp, reptype));
1497 if (reptype != 0 || charcount(cp, '!') < 2)
1498 goto jleave;
1499 cp2 = strrchr(cp, '!');
1500 --cp2;
1501 while (cp2 > cp && *cp2 != '!')
1502 --cp2;
1503 if (*cp2 == '!')
1504 cp = cp2 + 1;
1505 jleave:
1506 NYD_LEAVE;
1507 return cp;
1510 FL char const *
1511 skip_comment(char const *cp)
1513 size_t nesting;
1514 NYD_ENTER;
1516 for (nesting = 1; nesting > 0 && *cp; ++cp) {
1517 switch (*cp) {
1518 case '\\':
1519 if (cp[1])
1520 ++cp;
1521 break;
1522 case '(':
1523 ++nesting;
1524 break;
1525 case ')':
1526 --nesting;
1527 break;
1530 NYD_LEAVE;
1531 return cp;
1534 FL char const *
1535 routeaddr(char const *name)
1537 char const *np, *rp = NULL;
1538 NYD_ENTER;
1540 for (np = name; *np; np++) {
1541 switch (*np) {
1542 case '(':
1543 np = skip_comment(np + 1) - 1;
1544 break;
1545 case '"':
1546 while (*np) {
1547 if (*++np == '"')
1548 break;
1549 if (*np == '\\' && np[1])
1550 np++;
1552 break;
1553 case '<':
1554 rp = np;
1555 break;
1556 case '>':
1557 goto jleave;
1560 rp = NULL;
1561 jleave:
1562 NYD_LEAVE;
1563 return rp;
1566 FL enum expand_addr_flags
1567 expandaddr_to_eaf(void)
1569 struct eafdesc {
1570 char const *eafd_name;
1571 bool_t eafd_is_target;
1572 ui8_t eafd_andoff;
1573 ui8_t eafd_or;
1574 } const eafa[] = {
1575 {"restrict", FAL0, EAF_TARGET_MASK, EAF_RESTRICT | EAF_RESTRICT_TARGETS},
1576 {"fail", FAL0, EAF_NONE, EAF_FAIL},
1577 {"failinvaddr", FAL0, EAF_NONE, EAF_FAILINVADDR | EAF_ADDR},
1578 {"all", TRU1, EAF_NONE, EAF_TARGET_MASK},
1579 {"file", TRU1, EAF_NONE, EAF_FILE},
1580 {"pipe", TRU1, EAF_NONE, EAF_PIPE},
1581 {"name", TRU1, EAF_NONE, EAF_NAME},
1582 {"addr", TRU1, EAF_NONE, EAF_ADDR}
1583 }, *eafp;
1585 char *buf;
1586 enum expand_addr_flags rv;
1587 char const *cp;
1588 NYD2_ENTER;
1590 if ((cp = ok_vlook(expandaddr)) == NULL)
1591 rv = EAF_RESTRICT_TARGETS;
1592 else if (*cp == '\0')
1593 rv = EAF_TARGET_MASK;
1594 else {
1595 rv = EAF_TARGET_MASK;
1597 for (buf = savestr(cp); (cp = n_strsep(&buf, ',', TRU1)) != NULL;) {
1598 bool_t minus;
1600 if ((minus = (*cp == '-')) || *cp == '+')
1601 ++cp;
1602 for (eafp = eafa;; ++eafp) {
1603 if (eafp == eafa + n_NELEM(eafa)) {
1604 if (n_poption & n_PO_D_V)
1605 n_err(_("Unknown *expandaddr* value: %s\n"), cp);
1606 break;
1607 } else if (!asccasecmp(cp, eafp->eafd_name)) {
1608 if (!minus) {
1609 rv &= ~eafp->eafd_andoff;
1610 rv |= eafp->eafd_or;
1611 } else {
1612 if (eafp->eafd_is_target)
1613 rv &= ~eafp->eafd_or;
1614 else if (n_poption & n_PO_D_V)
1615 n_err(_("minus - prefix invalid for *expandaddr* value: "
1616 "%s\n"), --cp);
1618 break;
1619 } else if (!asccasecmp(cp, "noalias")) { /* TODO v15 OBSOLETE */
1620 n_OBSOLETE(_("*expandaddr*: noalias is henceforth -name"));
1621 rv &= ~EAF_NAME;
1622 break;
1627 if((rv & EAF_RESTRICT) && ((n_psonce & n_PSO_INTERACTIVE) ||
1628 (n_poption & n_PO_TILDE_FLAG)))
1629 rv |= EAF_TARGET_MASK;
1630 else if(n_poption & n_PO_D_V){
1631 if(!(rv & EAF_TARGET_MASK))
1632 n_err(_("*expandaddr* doesn't allow any addressees\n"));
1633 else if((rv & EAF_FAIL) && (rv & EAF_TARGET_MASK) == EAF_TARGET_MASK)
1634 n_err(_("*expandaddr* with fail, but no restrictions to apply\n"));
1637 NYD2_LEAVE;
1638 return rv;
1641 FL si8_t
1642 is_addr_invalid(struct name *np, enum expand_addr_check_mode eacm)
1644 char cbuf[sizeof "'\\U12340'"];
1645 char const *cs;
1646 int f;
1647 si8_t rv;
1648 enum expand_addr_flags eaf;
1649 NYD_ENTER;
1651 eaf = expandaddr_to_eaf();
1652 f = np->n_flags;
1654 if ((rv = ((f & NAME_ADDRSPEC_INVALID) != 0))) {
1655 if (eaf & EAF_FAILINVADDR)
1656 rv = -rv;
1658 if ((eacm & EACM_NOLOG) || (f & NAME_ADDRSPEC_ERR_EMPTY)) {
1660 } else {
1661 ui32_t c;
1662 char const *fmt = "'\\x%02X'";
1663 bool_t ok8bit = TRU1;
1665 if (f & NAME_ADDRSPEC_ERR_IDNA) {
1666 cs = _("Invalid domain name: %s, character %s\n");
1667 fmt = "'\\U%04X'";
1668 ok8bit = FAL0;
1669 } else if (f & NAME_ADDRSPEC_ERR_ATSEQ)
1670 cs = _("%s contains invalid %s sequence\n");
1671 else
1672 cs = _("%s contains invalid non-ASCII byte %s\n");
1674 c = NAME_ADDRSPEC_ERR_GETWC(f);
1675 snprintf(cbuf, sizeof cbuf,
1676 (ok8bit && c >= 040 && c <= 0177 ? "'%c'" : fmt), c);
1677 goto jprint;
1679 goto jleave;
1682 /* *expandaddr* stuff */
1683 if (!(rv = ((eacm & EACM_MODE_MASK) != EACM_NONE)))
1684 goto jleave;
1686 if ((eacm & EACM_STRICT) && (f & NAME_ADDRSPEC_ISFILEORPIPE)) {
1687 if (eaf & EAF_FAIL)
1688 rv = -rv;
1689 cs = _("%s%s: file or pipe addressees not allowed here\n");
1690 if (eacm & EACM_NOLOG)
1691 goto jleave;
1692 else
1693 goto j0print;
1696 eaf |= (eacm & EAF_TARGET_MASK);
1697 if (eacm & EACM_NONAME)
1698 eaf &= ~EAF_NAME;
1700 if (eaf == EAF_NONE) {
1701 rv = FAL0;
1702 goto jleave;
1704 if (eaf & EAF_FAIL)
1705 rv = -rv;
1707 if (!(eaf & EAF_FILE) && (f & NAME_ADDRSPEC_ISFILE)) {
1708 cs = _("%s%s: *expandaddr* doesn't allow file target\n");
1709 if (eacm & EACM_NOLOG)
1710 goto jleave;
1711 } else if (!(eaf & EAF_PIPE) && (f & NAME_ADDRSPEC_ISPIPE)) {
1712 cs = _("%s%s: *expandaddr* doesn't allow command pipe target\n");
1713 if (eacm & EACM_NOLOG)
1714 goto jleave;
1715 } else if (!(eaf & EAF_NAME) && (f & NAME_ADDRSPEC_ISNAME)) {
1716 cs = _("%s%s: *expandaddr* doesn't allow user name target\n");
1717 if (eacm & EACM_NOLOG)
1718 goto jleave;
1719 } else if (!(eaf & EAF_ADDR) && (f & NAME_ADDRSPEC_ISADDR)) {
1720 cs = _("%s%s: *expandaddr* doesn't allow mail address target\n");
1721 if (eacm & EACM_NOLOG)
1722 goto jleave;
1723 } else {
1724 rv = FAL0;
1725 goto jleave;
1728 j0print:
1729 cbuf[0] = '\0';
1730 jprint:
1731 n_err(cs, n_shexp_quote_cp(np->n_name, TRU1), cbuf);
1732 jleave:
1733 NYD_LEAVE;
1734 return rv;
1737 FL char *
1738 skin(char const *name)
1740 struct n_addrguts ag;
1741 char *rv;
1742 NYD_ENTER;
1744 if(name != NULL){
1745 name = n_addrspec_with_guts(&ag,name, TRU1);
1746 rv = ag.ag_skinned;
1747 if(!(ag.ag_n_flags & NAME_NAME_SALLOC))
1748 rv = savestrbuf(rv, ag.ag_slen);
1749 }else
1750 rv = NULL;
1751 NYD_LEAVE;
1752 return rv;
1755 /* TODO addrspec_with_guts: RFC 5322
1756 * TODO addrspec_with_guts: trim whitespace ETC. ETC. ETC.!!! */
1757 FL char const *
1758 n_addrspec_with_guts(struct n_addrguts *agp, char const *name, bool_t doskin){
1759 char const *cp;
1760 char *cp2, *bufend, *nbuf, c;
1761 enum{
1762 a_NONE,
1763 a_GOTLT = 1<<0,
1764 a_GOTADDR = 1<<1,
1765 a_GOTSPACE = 1<<2,
1766 a_LASTSP = 1<<3
1767 } flags;
1768 NYD_ENTER;
1770 memset(agp, 0, sizeof *agp);
1772 if((agp->ag_input = name) == NULL || (agp->ag_ilen = strlen(name)) == 0){
1773 agp->ag_skinned = n_UNCONST(n_empty); /* ok: NAME_SALLOC is not set */
1774 agp->ag_slen = 0;
1775 agp->ag_n_flags |= NAME_ADDRSPEC_CHECKED;
1776 NAME_ADDRSPEC_ERR_SET(agp->ag_n_flags, NAME_ADDRSPEC_ERR_EMPTY, 0);
1777 goto jleave;
1778 }else if(!doskin){
1779 /*agp->ag_iaddr_start = 0;*/
1780 agp->ag_iaddr_aend = agp->ag_ilen;
1781 agp->ag_skinned = n_UNCONST(name); /* (NAME_SALLOC not set) */
1782 agp->ag_slen = agp->ag_ilen;
1783 agp->ag_n_flags = NAME_SKINNED;
1784 goto jcheck;
1787 flags = a_NONE;
1788 nbuf = n_lofi_alloc(agp->ag_ilen +1);
1789 /*agp->ag_iaddr_start = 0;*/
1790 cp2 = bufend = nbuf;
1792 /* TODO This is complete crap and should use a token parser */
1793 for(cp = name++; (c = *cp++) != '\0';){
1794 switch (c) {
1795 case '(':
1796 cp = skip_comment(cp);
1797 flags &= ~a_LASTSP;
1798 break;
1799 case '"':
1800 /* Start of a "quoted-string". Copy it in its entirety */
1801 /* XXX RFC: quotes are "semantically invisible"
1802 * XXX But it was explicitly added (Changelog.Heirloom,
1803 * XXX [9.23] released 11/15/00, "Do not remove quotes
1804 * XXX when skinning names"? No more info.. */
1805 *cp2++ = c;
1806 while ((c = *cp) != '\0') { /* TODO improve */
1807 ++cp;
1808 if (c == '"') {
1809 *cp2++ = c;
1810 break;
1812 if (c != '\\')
1813 *cp2++ = c;
1814 else if ((c = *cp) != '\0') {
1815 *cp2++ = c;
1816 ++cp;
1819 flags &= ~a_LASTSP;
1820 break;
1821 case ' ':
1822 case '\t':
1823 if((flags & (a_GOTADDR | a_GOTSPACE)) == a_GOTADDR){
1824 flags |= a_GOTSPACE;
1825 agp->ag_iaddr_aend = PTR2SIZE(cp - name);
1827 if (cp[0] == 'a' && cp[1] == 't' && blankchar(cp[2]))
1828 cp += 3, *cp2++ = '@';
1829 else if (cp[0] == '@' && blankchar(cp[1]))
1830 cp += 2, *cp2++ = '@';
1831 else
1832 flags |= a_LASTSP;
1833 break;
1834 case '<':
1835 agp->ag_iaddr_start = PTR2SIZE(cp - (name - 1));
1836 cp2 = bufend;
1837 flags &= ~(a_GOTSPACE | a_LASTSP);
1838 flags |= a_GOTLT | a_GOTADDR;
1839 break;
1840 case '>':
1841 if(flags & a_GOTLT){
1842 /* (_addrspec_check() verifies these later!) */
1843 flags &= ~(a_GOTLT | a_LASTSP);
1844 agp->ag_iaddr_aend = PTR2SIZE(cp - name);
1846 /* Skip over the entire remaining field */
1847 while((c = *cp) != '\0' && c != ','){
1848 ++cp;
1849 if (c == '(')
1850 cp = skip_comment(cp);
1851 else if (c == '"')
1852 while ((c = *cp) != '\0') {
1853 ++cp;
1854 if (c == '"')
1855 break;
1856 if (c == '\\' && *cp != '\0')
1857 ++cp;
1860 break;
1862 /* FALLTHRU */
1863 default:
1864 if(flags & a_LASTSP){
1865 flags &= ~a_LASTSP;
1866 if(flags & a_GOTADDR)
1867 *cp2++ = ' ';
1869 *cp2++ = c;
1870 /* This character is forbidden here, but it may nonetheless be
1871 * present: ensure we turn this into something valid! (E.g., if the
1872 * next character would be a "..) */
1873 if(c == '\\' && *cp != '\0')
1874 *cp2++ = *cp++;
1875 if(c == ','){
1876 if(!(flags & a_GOTLT)){
1877 *cp2++ = ' ';
1878 for(; blankchar(*cp); ++cp)
1880 flags &= ~a_LASTSP;
1881 bufend = cp2;
1883 }else if(!(flags & a_GOTADDR)){
1884 flags |= a_GOTADDR;
1885 agp->ag_iaddr_start = PTR2SIZE(cp - name);
1889 --name;
1890 agp->ag_slen = PTR2SIZE(cp2 - nbuf);
1891 if (agp->ag_iaddr_aend == 0)
1892 agp->ag_iaddr_aend = agp->ag_ilen;
1893 /* Misses > */
1894 else if (agp->ag_iaddr_aend < agp->ag_iaddr_start) {
1895 cp2 = n_autorec_alloc(NULL, agp->ag_ilen + 1 +1);
1896 memcpy(cp2, agp->ag_input, agp->ag_ilen);
1897 agp->ag_iaddr_aend = agp->ag_ilen;
1898 cp2[agp->ag_ilen++] = '>';
1899 cp2[agp->ag_ilen] = '\0';
1901 agp->ag_skinned = savestrbuf(nbuf, agp->ag_slen);
1902 n_lofi_free(nbuf);
1903 agp->ag_n_flags = NAME_NAME_SALLOC | NAME_SKINNED;
1904 jcheck:
1905 if(a_head_addrspec_check(agp, doskin) <= FAL0)
1906 name = NULL;
1907 else
1908 name = agp->ag_input;
1909 jleave:
1910 NYD_LEAVE;
1911 return name;
1914 FL char *
1915 realname(char const *name)
1917 char const *cp, *cq, *cstart = NULL, *cend = NULL;
1918 char *rname, *rp;
1919 struct str in, out;
1920 int quoted, good, nogood;
1921 NYD_ENTER;
1923 if ((cp = n_UNCONST(name)) == NULL)
1924 goto jleave;
1925 for (; *cp != '\0'; ++cp) {
1926 switch (*cp) {
1927 case '(':
1928 if (cstart != NULL) {
1929 /* More than one comment in address, doesn't make sense to display
1930 * it without context. Return the entire field */
1931 cp = mime_fromaddr(name);
1932 goto jleave;
1934 cstart = cp++;
1935 cp = skip_comment(cp);
1936 cend = cp--;
1937 if (cend <= cstart)
1938 cend = cstart = NULL;
1939 break;
1940 case '"':
1941 while (*cp) {
1942 if (*++cp == '"')
1943 break;
1944 if (*cp == '\\' && cp[1])
1945 ++cp;
1947 break;
1948 case '<':
1949 if (cp > name) {
1950 cstart = name;
1951 cend = cp;
1953 break;
1954 case ',':
1955 /* More than one address. Just use the first one */
1956 goto jbrk;
1960 jbrk:
1961 if (cstart == NULL) {
1962 if (*name == '<') {
1963 /* If name contains only a route-addr, the surrounding angle brackets
1964 * don't serve any useful purpose when displaying, so remove */
1965 cp = prstr(skin(name));
1966 } else
1967 cp = mime_fromaddr(name);
1968 goto jleave;
1971 /* Strip quotes. Note that quotes that appear within a MIME encoded word are
1972 * not stripped. The idea is to strip only syntactical relevant things (but
1973 * this is not necessarily the most sensible way in practice) */
1974 rp = rname = ac_alloc(PTR2SIZE(cend - cstart +1));
1975 quoted = 0;
1976 for (cp = cstart; cp < cend; ++cp) {
1977 if (*cp == '(' && !quoted) {
1978 cq = skip_comment(++cp);
1979 if (PTRCMP(--cq, >, cend))
1980 cq = cend;
1981 while (cp < cq) {
1982 if (*cp == '\\' && PTRCMP(cp + 1, <, cq))
1983 ++cp;
1984 *rp++ = *cp++;
1986 } else if (*cp == '\\' && PTRCMP(cp + 1, <, cend))
1987 *rp++ = *++cp;
1988 else if (*cp == '"') {
1989 quoted = !quoted;
1990 continue;
1991 } else
1992 *rp++ = *cp;
1994 *rp = '\0';
1995 in.s = rname;
1996 in.l = rp - rname;
1997 mime_fromhdr(&in, &out, TD_ISPR | TD_ICONV);
1998 ac_free(rname);
1999 rname = savestr(out.s);
2000 free(out.s);
2002 while (blankchar(*rname))
2003 ++rname;
2004 for (rp = rname; *rp != '\0'; ++rp)
2006 while (PTRCMP(--rp, >=, rname) && blankchar(*rp))
2007 *rp = '\0';
2008 if (rp == rname) {
2009 cp = mime_fromaddr(name);
2010 goto jleave;
2013 /* mime_fromhdr() has converted all nonprintable characters to question
2014 * marks now. These and blanks are considered uninteresting; if the
2015 * displayed part of the real name contains more than 25% of them, it is
2016 * probably better to display the plain email address instead */
2017 good = 0;
2018 nogood = 0;
2019 for (rp = rname; *rp != '\0' && PTRCMP(rp, <, rname + 20); ++rp)
2020 if (*rp == '?' || blankchar(*rp))
2021 ++nogood;
2022 else
2023 ++good;
2024 cp = (good * 3 < nogood) ? prstr(skin(name)) : rname;
2025 jleave:
2026 NYD_LEAVE;
2027 return n_UNCONST(cp);
2030 FL char *
2031 name1(struct message *mp, int reptype)
2033 char *namebuf, *cp, *cp2, *linebuf = NULL /* TODO line pool */;
2034 size_t namesize, linesize = 0;
2035 FILE *ibuf;
2036 int f1st = 1;
2037 NYD_ENTER;
2039 if ((cp = hfield1("from", mp)) != NULL && *cp != '\0')
2040 goto jleave;
2041 if (reptype == 0 && (cp = hfield1("sender", mp)) != NULL && *cp != '\0')
2042 goto jleave;
2044 namebuf = smalloc(namesize = 1);
2045 namebuf[0] = 0;
2046 if (mp->m_flag & MNOFROM)
2047 goto jout;
2048 if ((ibuf = setinput(&mb, mp, NEED_HEADER)) == NULL)
2049 goto jout;
2050 if (readline_restart(ibuf, &linebuf, &linesize, 0) < 0)
2051 goto jout;
2053 jnewname:
2054 if (namesize <= linesize)
2055 namebuf = srealloc(namebuf, namesize = linesize +1);
2056 for (cp = linebuf; *cp != '\0' && *cp != ' '; ++cp)
2058 for (; blankchar(*cp); ++cp)
2060 for (cp2 = namebuf + strlen(namebuf);
2061 *cp && !blankchar(*cp) && PTRCMP(cp2, <, namebuf + namesize -1);)
2062 *cp2++ = *cp++;
2063 *cp2 = '\0';
2065 if (readline_restart(ibuf, &linebuf, &linesize, 0) < 0)
2066 goto jout;
2067 if ((cp = strchr(linebuf, 'F')) == NULL)
2068 goto jout;
2069 if (strncmp(cp, "From", 4))
2070 goto jout;
2071 if (namesize <= linesize)
2072 namebuf = srealloc(namebuf, namesize = linesize + 1);
2074 while ((cp = strchr(cp, 'r')) != NULL) {
2075 if (!strncmp(cp, "remote", 6)) {
2076 if ((cp = strchr(cp, 'f')) == NULL)
2077 break;
2078 if (strncmp(cp, "from", 4) != 0)
2079 break;
2080 if ((cp = strchr(cp, ' ')) == NULL)
2081 break;
2082 cp++;
2083 if (f1st) {
2084 strncpy(namebuf, cp, namesize);
2085 f1st = 0;
2086 } else {
2087 cp2 = strrchr(namebuf, '!') + 1;
2088 strncpy(cp2, cp, PTR2SIZE(namebuf + namesize - cp2));
2090 namebuf[namesize - 2] = '!';
2091 namebuf[namesize - 1] = '\0';
2092 goto jnewname;
2094 cp++;
2096 jout:
2097 if (*namebuf != '\0' || ((cp = hfield1("return-path", mp))) == NULL ||
2098 *cp == '\0')
2099 cp = savestr(namebuf);
2101 if (linebuf != NULL)
2102 free(linebuf);
2103 free(namebuf);
2104 jleave:
2105 NYD_LEAVE;
2106 return cp;
2109 FL char const *
2110 subject_re_trim(char const *s){
2111 struct{
2112 ui8_t len;
2113 char dat[7];
2114 }const *pp, ignored[] = { /* Update *reply-strings* manual upon change! */
2115 {3, "re:"},
2116 {3, "aw:"}, {5, "antw:"}, /* de */
2117 {3, "wg:"}, /* Seen too often in the wild */
2118 {0, ""}
2121 bool_t any;
2122 char *re_st, *re_st_x;
2123 char const *orig_s;
2124 size_t re_l;
2125 NYD_ENTER;
2127 any = FAL0;
2128 orig_s = s;
2129 re_st = NULL;
2130 n_UNINIT(re_l, 0);
2132 if((re_st_x = ok_vlook(reply_strings)) != NULL &&
2133 (re_l = strlen(re_st_x)) > 0){
2134 re_st = n_lofi_alloc(++re_l * 2);
2135 memcpy(re_st, re_st_x, re_l);
2138 jouter:
2139 while(*s != '\0'){
2140 while(spacechar(*s))
2141 ++s;
2143 for(pp = ignored; pp->len > 0; ++pp)
2144 if(is_asccaseprefix(pp->dat, s)){
2145 s += pp->len;
2146 any = TRU1;
2147 goto jouter;
2150 if(re_st != NULL){
2151 char *cp;
2153 memcpy(re_st_x = &re_st[re_l], re_st, re_l);
2154 while((cp = n_strsep(&re_st_x, ',', TRU1)) != NULL)
2155 if(is_asccaseprefix(cp, s)){
2156 s += strlen(cp);
2157 any = TRU1;
2158 goto jouter;
2161 break;
2164 if(re_st != NULL)
2165 n_lofi_free(re_st);
2166 NYD_LEAVE;
2167 return any ? s : orig_s;
2170 FL int
2171 msgidcmp(char const *s1, char const *s2)
2173 int q1 = 0, q2 = 0, c1, c2;
2174 NYD_ENTER;
2176 while(*s1 == '<')
2177 ++s1;
2178 while(*s2 == '<')
2179 ++s2;
2181 do {
2182 c1 = msgidnextc(&s1, &q1);
2183 c2 = msgidnextc(&s2, &q2);
2184 if (c1 != c2)
2185 break;
2186 } while (c1 && c2);
2187 NYD_LEAVE;
2188 return c1 - c2;
2191 FL char const *
2192 fakefrom(struct message *mp)
2194 char const *name;
2195 NYD_ENTER;
2197 if (((name = skin(hfield1("return-path", mp))) == NULL || *name == '\0' ) &&
2198 ((name = skin(hfield1("from", mp))) == NULL || *name == '\0'))
2199 /* XXX MAILER-DAEMON is what an old MBOX manual page says.
2200 * RFC 4155 however requires a RFC 5322 (2822) conforming
2201 * "addr-spec", but we simply can't provide that */
2202 name = "MAILER-DAEMON";
2203 NYD_LEAVE;
2204 return name;
2207 FL char const *
2208 fakedate(time_t t)
2210 char *cp, *cq;
2211 NYD_ENTER;
2213 cp = ctime(&t);
2214 for (cq = cp; *cq != '\0' && *cq != '\n'; ++cq)
2216 *cq = '\0';
2217 cp = savestr(cp);
2218 NYD_LEAVE;
2219 return cp;
2222 #ifdef HAVE_IMAP_SEARCH
2223 FL time_t
2224 unixtime(char const *fromline)
2226 char const *fp, *xp;
2227 time_t t;
2228 si32_t i, year, month, day, hour, minute, second, tzdiff;
2229 struct tm *tmptr;
2230 NYD2_ENTER;
2232 for (fp = fromline; *fp != '\0' && *fp != '\n'; ++fp)
2234 fp -= 24;
2235 if (PTR2SIZE(fp - fromline) < 7)
2236 goto jinvalid;
2237 if (fp[3] != ' ')
2238 goto jinvalid;
2239 for (i = 0;;) {
2240 if (!strncmp(fp + 4, n_month_names[i], 3))
2241 break;
2242 if (n_month_names[++i][0] == '\0')
2243 goto jinvalid;
2245 month = i + 1;
2246 if (fp[7] != ' ')
2247 goto jinvalid;
2248 n_idec_si32_cp(&day, &fp[8], 10, &xp);
2249 if (*xp != ' ' || xp != fp + 10)
2250 goto jinvalid;
2251 n_idec_si32_cp(&hour, &fp[11], 10, &xp);
2252 if (*xp != ':' || xp != fp + 13)
2253 goto jinvalid;
2254 n_idec_si32_cp(&minute, &fp[14], 10, &xp);
2255 if (*xp != ':' || xp != fp + 16)
2256 goto jinvalid;
2257 n_idec_si32_cp(&second, &fp[17], 10, &xp);
2258 if (*xp != ' ' || xp != fp + 19)
2259 goto jinvalid;
2260 n_idec_si32_cp(&year, &fp[20], 10, &xp);
2261 if (xp != fp + 24)
2262 goto jinvalid;
2263 if ((t = combinetime(year, month, day, hour, minute, second)) == (time_t)-1)
2264 goto jinvalid;
2265 tzdiff = t - mktime(gmtime(&t));
2266 tmptr = localtime(&t);
2267 if (tmptr->tm_isdst > 0)
2268 tzdiff += 3600;
2269 t -= tzdiff;
2270 jleave:
2271 NYD2_LEAVE;
2272 return t;
2273 jinvalid:
2274 t = n_time_epoch();
2275 goto jleave;
2277 #endif /* HAVE_IMAP_SEARCH */
2279 FL time_t
2280 rfctime(char const *date) /* TODO n_idec_ return tests */
2282 char const *cp, *x;
2283 time_t t;
2284 si32_t i, year, month, day, hour, minute, second;
2285 NYD2_ENTER;
2287 cp = date;
2289 if ((cp = nexttoken(cp)) == NULL)
2290 goto jinvalid;
2291 if (alphachar(cp[0]) && alphachar(cp[1]) && alphachar(cp[2]) &&
2292 cp[3] == ',') {
2293 if ((cp = nexttoken(&cp[4])) == NULL)
2294 goto jinvalid;
2296 n_idec_si32_cp(&day, cp, 10, &x);
2297 if ((cp = nexttoken(x)) == NULL)
2298 goto jinvalid;
2299 for (i = 0;;) {
2300 if (!strncmp(cp, n_month_names[i], 3))
2301 break;
2302 if (n_month_names[++i][0] == '\0')
2303 goto jinvalid;
2305 month = i + 1;
2306 if ((cp = nexttoken(&cp[3])) == NULL)
2307 goto jinvalid;
2308 /* RFC 5322, 4.3:
2309 * Where a two or three digit year occurs in a date, the year is to be
2310 * interpreted as follows: If a two digit year is encountered whose
2311 * value is between 00 and 49, the year is interpreted by adding 2000,
2312 * ending up with a value between 2000 and 2049. If a two digit year
2313 * is encountered with a value between 50 and 99, or any three digit
2314 * year is encountered, the year is interpreted by adding 1900 */
2315 n_idec_si32_cp(&year, cp, 10, &x);
2316 i = (int)PTR2SIZE(x - cp);
2317 if (i == 2 && year >= 0 && year <= 49)
2318 year += 2000;
2319 else if (i == 3 || (i == 2 && year >= 50 && year <= 99))
2320 year += 1900;
2321 if ((cp = nexttoken(x)) == NULL)
2322 goto jinvalid;
2323 n_idec_si32_cp(&hour, cp, 10, &x);
2324 if (*x != ':')
2325 goto jinvalid;
2326 cp = &x[1];
2327 n_idec_si32_cp(&minute, cp, 10, &x);
2328 if (*x == ':') {
2329 cp = &x[1];
2330 n_idec_si32_cp(&second, cp, 10, &x);
2331 } else
2332 second = 0;
2334 if ((t = combinetime(year, month, day, hour, minute, second)) == (time_t)-1)
2335 goto jinvalid;
2336 if ((cp = nexttoken(x)) != NULL) {
2337 char buf[3];
2338 int sign = 1;
2340 switch (*cp) {
2341 case '+':
2342 sign = -1;
2343 /* FALLTHRU */
2344 case '-':
2345 ++cp;
2346 break;
2348 if (digitchar(cp[0]) && digitchar(cp[1]) && digitchar(cp[2]) &&
2349 digitchar(cp[3])) {
2350 si64_t tadj;
2352 buf[2] = '\0';
2353 buf[0] = cp[0];
2354 buf[1] = cp[1];
2355 n_idec_si32_cp(&i, buf, 10, NULL);
2356 tadj = (si64_t)i * 3600; /* XXX */
2357 buf[0] = cp[2];
2358 buf[1] = cp[3];
2359 n_idec_si32_cp(&i, buf, 10, NULL);
2360 tadj += (si64_t)i * 60; /* XXX */
2361 if (sign < 0)
2362 tadj = -tadj;
2363 t += (time_t)tadj;
2365 /* TODO WE DO NOT YET PARSE (OBSOLETE) ZONE NAMES
2366 * TODO once again, Christos Zoulas and NetBSD Mail have done
2367 * TODO a really good job already, but using strptime(3), which
2368 * TODO is not portable. Nonetheless, WE must improve, not
2369 * TODO at last because we simply ignore obsolete timezones!!
2370 * TODO See RFC 5322, 4.3! */
2372 jleave:
2373 NYD2_LEAVE;
2374 return t;
2375 jinvalid:
2376 t = 0;
2377 goto jleave;
2380 FL time_t
2381 combinetime(int year, int month, int day, int hour, int minute, int second){
2382 size_t const jdn_epoch = 2440588;
2383 bool_t const y2038p = (sizeof(time_t) == 4);
2385 size_t jdn;
2386 time_t t;
2387 NYD2_ENTER;
2389 if(UICMP(32, second, >/*XXX leap=*/, DATE_SECSMIN) ||
2390 UICMP(32, minute, >=, DATE_MINSHOUR) ||
2391 UICMP(32, hour, >=, DATE_HOURSDAY) ||
2392 day < 1 || day > 31 ||
2393 month < 1 || month > 12 ||
2394 year < 1970)
2395 goto jerr;
2397 if(year >= 1970 + ((y2038p ? SI32_MAX : SI64_MAX) /
2398 (DATE_SECSDAY * DATE_DAYSYEAR))){
2399 /* Be a coward regarding Y2038, many people (mostly myself, that is) do
2400 * test by stepping second-wise around the flip. Don't care otherwise */
2401 if(!y2038p)
2402 goto jerr;
2403 if(year > 2038 || month > 1 || day > 19 ||
2404 hour > 3 || minute > 14 || second > 7)
2405 goto jerr;
2408 t = second;
2409 t += minute * DATE_SECSMIN;
2410 t += hour * DATE_SECSHOUR;
2412 jdn = a_head_gregorian_to_jdn(year, month, day);
2413 jdn -= jdn_epoch;
2414 t += (time_t)jdn * DATE_SECSDAY;
2415 jleave:
2416 NYD2_LEAVE;
2417 return t;
2418 jerr:
2419 t = (time_t)-1;
2420 goto jleave;
2423 FL void
2424 substdate(struct message *m)
2426 char const *cp;
2427 NYD_ENTER;
2429 /* Determine the date to print in faked 'From ' lines. This is traditionally
2430 * the date the message was written to the mail file. Try to determine this
2431 * using RFC message header fields, or fall back to current time */
2432 if ((cp = hfield1("received", m)) != NULL) {
2433 while ((cp = nexttoken(cp)) != NULL && *cp != ';') {
2435 ++cp;
2436 while (alnumchar(*cp));
2438 if (cp && *++cp)
2439 m->m_time = rfctime(cp);
2441 if (m->m_time == 0 || m->m_time > time_current.tc_time) {
2442 if ((cp = hfield1("date", m)) != NULL)
2443 m->m_time = rfctime(cp);
2445 if (m->m_time == 0 || m->m_time > time_current.tc_time)
2446 m->m_time = time_current.tc_time;
2447 NYD_LEAVE;
2450 FL void
2451 setup_from_and_sender(struct header *hp)
2453 char const *addr;
2454 struct name *np;
2455 NYD_ENTER;
2457 /* If -t parsed or composed From: then take it. With -t we otherwise
2458 * want -r to be honoured in favour of *from* in order to have
2459 * a behaviour that is compatible with what users would expect from e.g.
2460 * postfix(1) */
2461 if ((np = hp->h_from) != NULL ||
2462 ((n_psonce & n_PSO_t_FLAG) && (np = n_poption_arg_r) != NULL)) {
2464 } else if ((addr = myaddrs(hp)) != NULL)
2465 np = lextract(addr, GEXTRA | GFULL | GFULLEXTRA);
2466 hp->h_from = np;
2468 if ((np = hp->h_sender) != NULL) {
2470 } else if ((addr = ok_vlook(sender)) != NULL)
2471 np = lextract(addr, GEXTRA | GFULL | GFULLEXTRA);
2472 hp->h_sender = np;
2474 NYD_LEAVE;
2477 FL struct name const *
2478 check_from_and_sender(struct name const *fromfield,
2479 struct name const *senderfield)
2481 struct name const *rv = NULL;
2482 NYD_ENTER;
2484 if (senderfield != NULL) {
2485 if (senderfield->n_flink != NULL) {
2486 n_err(_("The Sender: field may contain only one address\n"));
2487 goto jleave;
2489 rv = senderfield;
2492 if (fromfield != NULL) {
2493 if (fromfield->n_flink != NULL && senderfield == NULL) {
2494 n_err(_("A Sender: is required when there are multiple "
2495 "addresses in From:\n"));
2496 goto jleave;
2498 if (rv == NULL)
2499 rv = fromfield;
2502 if (rv == NULL)
2503 rv = (struct name*)0x1;
2504 jleave:
2505 NYD_LEAVE;
2506 return rv;
2509 #ifdef HAVE_XSSL
2510 FL char *
2511 getsender(struct message *mp)
2513 char *cp;
2514 struct name *np;
2515 NYD_ENTER;
2517 if ((cp = hfield1("from", mp)) == NULL ||
2518 (np = lextract(cp, GEXTRA | GSKIN)) == NULL)
2519 cp = NULL;
2520 else
2521 cp = (np->n_flink != NULL) ? skin(hfield1("sender", mp)) : np->n_name;
2522 NYD_LEAVE;
2523 return cp;
2525 #endif
2527 FL int
2528 grab_headers(enum n_lexinput_flags lif, struct header *hp, enum gfield gflags,
2529 int subjfirst)
2531 /* TODO grab_headers: again, check counts etc. against RFC;
2532 * TODO (now assumes check_from_and_sender() is called afterwards ++ */
2533 int errs;
2534 int volatile comma;
2535 NYD_ENTER;
2537 errs = 0;
2538 comma = (ok_blook(bsdcompat) || ok_blook(bsdmsgs)) ? 0 : GCOMMA;
2540 if (gflags & GTO)
2541 hp->h_to = grab_names(lif, "To: ", hp->h_to, comma, GTO | GFULL);
2542 if (subjfirst && (gflags & GSUBJECT))
2543 hp->h_subject = n_lex_input_cp(lif, "Subject: ", hp->h_subject);
2544 if (gflags & GCC)
2545 hp->h_cc = grab_names(lif, "Cc: ", hp->h_cc, comma, GCC | GFULL);
2546 if (gflags & GBCC)
2547 hp->h_bcc = grab_names(lif, "Bcc: ", hp->h_bcc, comma, GBCC | GFULL);
2549 if (gflags & GEXTRA) {
2550 if (hp->h_from == NULL)
2551 hp->h_from = lextract(myaddrs(hp), GEXTRA | GFULL | GFULLEXTRA);
2552 hp->h_from = grab_names(lif, "From: ", hp->h_from, comma,
2553 GEXTRA | GFULL | GFULLEXTRA);
2554 if (hp->h_replyto == NULL)
2555 hp->h_replyto = lextract(ok_vlook(replyto), GEXTRA | GFULL);
2556 hp->h_replyto = grab_names(lif, "Reply-To: ", hp->h_replyto, comma,
2557 GEXTRA | GFULL);
2558 if (hp->h_sender == NULL)
2559 hp->h_sender = extract(ok_vlook(sender), GEXTRA | GFULL);
2560 hp->h_sender = grab_names(lif, "Sender: ", hp->h_sender, comma,
2561 GEXTRA | GFULL);
2564 if (!subjfirst && (gflags & GSUBJECT))
2565 hp->h_subject = n_lex_input_cp(lif, "Subject: ", hp->h_subject);
2567 NYD_LEAVE;
2568 return errs;
2571 FL bool_t
2572 header_match(struct message *mp, struct search_expr const *sep)
2574 struct str in, out;
2575 FILE *ibuf;
2576 int lc;
2577 size_t linesize = 0; /* TODO line pool */
2578 char *linebuf = NULL, *colon;
2579 bool_t rv = FAL0;
2580 NYD_ENTER;
2582 if ((ibuf = setinput(&mb, mp, NEED_HEADER)) == NULL)
2583 goto jleave;
2584 if ((lc = mp->m_lines - 1) < 0)
2585 goto jleave;
2587 if ((mp->m_flag & MNOFROM) == 0 &&
2588 readline_restart(ibuf, &linebuf, &linesize, 0) < 0)
2589 goto jleave;
2590 while (lc > 0) {
2591 if (gethfield(ibuf, &linebuf, &linesize, lc, &colon) <= 0)
2592 break;
2593 if (blankchar(*++colon))
2594 ++colon;
2595 in.l = strlen(in.s = colon);
2596 mime_fromhdr(&in, &out, TD_ICONV);
2597 #ifdef HAVE_REGEX
2598 if (sep->ss_sexpr == NULL)
2599 rv = (regexec(&sep->ss_regex, out.s, 0,NULL, 0) != REG_NOMATCH);
2600 else
2601 #endif
2602 rv = substr(out.s, sep->ss_sexpr);
2603 free(out.s);
2604 if (rv)
2605 break;
2608 jleave:
2609 if (linebuf != NULL)
2610 free(linebuf);
2611 NYD_LEAVE;
2612 return rv;
2615 FL struct n_header_field *
2616 n_customhdr_query(void){
2617 char const *vp;
2618 struct n_header_field *rv, **tail, *hfp;
2619 NYD_ENTER;
2621 rv = NULL;
2623 if((vp = ok_vlook(customhdr)) != NULL){
2624 char *buf;
2626 tail = &rv;
2627 buf = savestr(vp);
2628 jch_outer:
2629 while((vp = a_head_customhdr__sep(&buf)) != NULL){
2630 ui32_t nl, bl;
2631 char const *nstart, *cp;
2633 for(nstart = cp = vp;; ++cp){
2634 if(fieldnamechar(*cp))
2635 continue;
2636 if(*cp == '\0'){
2637 if(cp == nstart){
2638 n_err(_("Invalid nameless *customhdr* entry\n"));
2639 goto jch_outer;
2641 }else if(*cp != ':' && !blankchar(*cp)){
2642 jch_badent:
2643 n_err(_("Invalid *customhdr* entry: %s\n"), vp);
2644 goto jch_outer;
2646 break;
2648 nl = (ui32_t)PTR2SIZE(cp - nstart);
2650 while(blankchar(*cp))
2651 ++cp;
2652 if(*cp++ != ':')
2653 goto jch_badent;
2654 while(blankchar(*cp))
2655 ++cp;
2656 bl = (ui32_t)strlen(cp) +1;
2658 *tail =
2659 hfp = salloc(n_VSTRUCT_SIZEOF(struct n_header_field, hf_dat) +
2660 nl +1 + bl);
2661 tail = &hfp->hf_next;
2662 hfp->hf_next = NULL;
2663 hfp->hf_nl = nl;
2664 hfp->hf_bl = bl - 1;
2665 memcpy(hfp->hf_dat, nstart, nl);
2666 hfp->hf_dat[nl++] = '\0';
2667 memcpy(hfp->hf_dat + nl, cp, bl);
2670 NYD_LEAVE;
2671 return rv;
2674 /* s-it-mode */