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>.
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
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
39 #ifndef HAVE_AMALGAMATION
44 size_t tlen
; /* Length of .tdata */
45 char const *tdata
; /* Template date - see _cmatch_data[] */
48 /* Template characters for cmatch_data.tdata:
49 * 'A' An upper case char
50 * 'a' A lower case char
53 * 'O' An optional digit or space
55 * '+' Either a plus or a minus sign */
56 static struct cmatch_data
const _cmatch_data
[] = {
57 { 24, "Aaa Aaa O0 00:00:00 0000" }, /* BSD/ISO C90 ctime */
58 { 28, "Aaa Aaa O0 00:00:00 AAA 0000" }, /* BSD tmz */
59 { 21, "Aaa Aaa O0 00:00 0000" }, /* SysV ctime */
60 { 25, "Aaa Aaa O0 00:00 AAA 0000" }, /* SysV tmz */
61 /* RFC 822-alike From_ lines do not conform to RFC 4155, but seem to be used
62 * in the wild (by UW-imap) */
63 { 30, "Aaa Aaa O0 00:00:00 0000 +0000" },
64 /* RFC 822 with zone spec; 1. military, 2. UT, 3. north america time
65 * zone strings; note that 1. is strictly speaking not correct as some
66 * letters are not used, and 2. is not because only "UT" is defined */
67 #define __reuse "Aaa Aaa O0 00:00:00 0000 AAA"
68 { 28 - 2, __reuse
}, { 28 - 1, __reuse
}, { 28 - 0, __reuse
},
71 #define a_HEAD_DATE_MINLEN 21
73 /* Skip over "word" as found in From_ line */
74 static char const * _from__skipword(char const *wp
);
76 /* Match the date string against the date template (tp), return if match.
77 * See _cmatch_data[] for template character description */
78 static int _cmatch(size_t len
, char const *date
,
81 /* Check whether date is a valid 'From_' date.
82 * (Rather ctime(3) generated dates, according to RFC 4155) */
83 static int _is_date(char const *date
);
85 /* JulianDayNumber converter(s) */
86 static size_t a_head_gregorian_to_jdn(ui32_t y
, ui32_t m
, ui32_t d
);
88 static void a_head_jdn_to_gregorian(size_t jdn
,
89 ui32_t
*yp
, ui32_t
*mp
, ui32_t
*dp
);
92 /* Convert the domain part of a skinned address to IDNA.
93 * If an error occurs before Unicode information is available, revert the IDNA
94 * error to a normal CHAR one so that the error message doesn't talk Unicode */
96 static struct n_addrguts
*a_head_idna_apply(struct n_addrguts
*agp
);
99 /* Classify and check a (possibly skinned) header body according to RFC
100 * *addr-spec* rules; if it (is assumed to has been) skinned it may however be
101 * also a file or a pipe command, so check that first, then.
102 * Otherwise perform content checking and isolate the domain part (for IDNA) */
103 static bool_t
a_head_addrspec_check(struct n_addrguts
*agp
, bool_t skinned
);
105 /* Return the next header field found in the given message.
106 * Return >= 0 if something found, < 0 elsewise.
107 * "colon" is set to point to the colon in the header.
108 * Must deal with \ continuations & other such fraud */
109 static int gethfield(FILE *f
, char **linebuf
, size_t *linesize
,
110 int rem
, char **colon
);
112 static int msgidnextc(char const **cp
, int *status
);
114 /* Count the occurances of c in str */
115 static int charcount(char *str
, int c
);
117 static char const * nexttoken(char const *cp
);
120 _from__skipword(char const *wp
)
126 while ((c
= *wp
++) != '\0' && !blankchar(c
)) {
128 while ((c
= *wp
++) != '\0' && c
!= '"')
134 for (; blankchar(c
); c
= *wp
++)
138 return (c
== 0 ? NULL
: wp
- 1);
142 _cmatch(size_t len
, char const *date
, char const *tp
)
167 if (c
!= ' ' && !digitchar(c
))
175 if (c
!= '+' && c
!= '-')
187 _is_date(char const *date
)
189 struct cmatch_data
const *cmdp
;
194 if ((dl
= strlen(date
)) >= a_HEAD_DATE_MINLEN
)
195 for (cmdp
= _cmatch_data
; cmdp
->tdata
!= NULL
; ++cmdp
)
196 if (dl
== cmdp
->tlen
&& (rv
= _cmatch(dl
, date
, cmdp
->tdata
)))
203 a_head_gregorian_to_jdn(ui32_t y
, ui32_t m
, ui32_t d
){
204 /* Algorithm is taken from Communications of the ACM, Vol 6, No 8.
205 * (via third hand, plus adjustments).
206 * This algorithm is supposed to work for all dates in between 1582-10-15
207 * (0001-01-01 but that not Gregorian) and 65535-12-31 */
246 a_head_jdn_to_gregorian(size_t jdn
, ui32_t
*yp
, ui32_t
*mp
, ui32_t
*dp
){
247 /* Algorithm is taken from Communications of the ACM, Vol 6, No 8.
248 * (via third hand, plus adjustments) */
267 x
= jdn
/ 153; /* x -> month */
270 jdn
/= 5; /* jdn -> day */
278 *yp
= (ui32_t
)(y
& 0xFFFF);
279 *mp
= (ui32_t
)(x
& 0xFF);
280 *dp
= (ui32_t
)(jdn
& 0xFF);
286 static struct n_addrguts
*
287 a_head_idna_apply(struct n_addrguts
*agp
){
288 struct n_string idna_ascii
;
291 n_string_creat_auto(&idna_ascii
);
293 if(!n_idna_to_ascii(&idna_ascii
, &agp
->ag_skinned
[agp
->ag_sdom_start
],
294 agp
->ag_slen
- agp
->ag_sdom_start
))
295 agp
->ag_n_flags
^= NAME_ADDRSPEC_ERR_IDNA
| NAME_ADDRSPEC_ERR_CHAR
;
297 /* Replace the domain part of .ag_skinned with IDNA version */
298 n_string_unshift_buf(&idna_ascii
, agp
->ag_skinned
, agp
->ag_sdom_start
);
300 agp
->ag_skinned
= n_string_cp(&idna_ascii
);
301 agp
->ag_slen
= idna_ascii
.s_len
;
302 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
,
303 NAME_NAME_SALLOC
| NAME_SKINNED
| NAME_IDNA
, 0);
308 #endif /* HAVE_IDNA */
311 a_head_addrspec_check(struct n_addrguts
*agp
, bool_t skinned
)
315 ui8_t in_domain
, hadat
;
316 union {bool_t b
; char c
; unsigned char u
; ui32_t ui32
; si32_t si32
;} c
;
323 use_idna
= ok_blook(idna_disable
) ? 0 : 1;
325 agp
->ag_n_flags
|= NAME_ADDRSPEC_CHECKED
;
326 addr
= agp
->ag_skinned
;
328 if (agp
->ag_iaddr_aend
- agp
->ag_iaddr_start
== 0) {
329 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
, NAME_ADDRSPEC_ERR_EMPTY
, 0);
333 /* If the field is not a recipient, it cannot be a file or a pipe */
337 /* When changing any of the following adjust any RECIPIENTADDRSPEC;
338 * grep the latter for the complete picture */
340 agp
->ag_n_flags
|= NAME_ADDRSPEC_ISPIPE
;
343 if (addr
[0] == '/' || (addr
[0] == '.' && addr
[1] == '/') ||
344 (addr
[0] == '-' && addr
[1] == '\0'))
346 if (memchr(addr
, '@', agp
->ag_slen
) == NULL
) {
349 for (p
= addr
; (c
.c
= *p
); ++p
) {
350 if (c
.c
== '!' || c
.c
== '%')
354 agp
->ag_n_flags
|= NAME_ADDRSPEC_ISFILE
;
361 /* TODO This is false. If super correct this should work on wide
362 * TODO characters, just in case (some bytes of) the ASCII set is (are)
363 * TODO shared; it may yet tear apart multibyte sequences, possibly.
364 * TODO All this should interact with mime_enc_mustquote(), too!
365 * TODO That is: once this is an object, we need to do this in a way
366 * TODO that it is valid for the wire format (instead)! */
368 in_domain
= hadat
= 0;
370 /* TODO addrspec_check: we need a real RFC 5322 (un)?structured parser! */
371 for (p
= addr
; (c
.c
= *p
++) != '\0';) {
373 in_quote
= !in_quote
;
374 } else if (c
.u
< 040 || c
.u
>= 0177) { /* TODO no magics: !bodychar()? */
376 if (in_domain
&& use_idna
> 0) {
378 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
, NAME_ADDRSPEC_ERR_IDNA
,
384 } else if (in_domain
== 2) {
385 if ((c
.c
== ']' && *p
!= '\0') || c
.c
== '\\' || whitechar(c
.c
))
387 } else if (in_quote
&& in_domain
== 0) {
389 } else if (c
.c
== '\\' && *p
!= '\0') {
391 } else if (c
.c
== '@') {
393 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
, NAME_ADDRSPEC_ERR_ATSEQ
,
397 agp
->ag_sdom_start
= PTR2SIZE(p
- addr
);
398 agp
->ag_n_flags
|= NAME_ADDRSPEC_ISADDR
; /* TODO .. really? */
399 in_domain
= (*p
== '[') ? 2 : 1;
401 } else if (c
.c
== '(' || c
.c
== ')' || c
.c
== '<' || c
.c
== '>' ||
402 c
.c
== '[' || c
.c
== ']' || c
.c
== ':' || c
.c
== ';' ||
403 c
.c
== '\\' || c
.c
== ',')
408 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
, NAME_ADDRSPEC_ERR_CHAR
, c
.u
);
412 if(!(agp
->ag_n_flags
& NAME_ADDRSPEC_ISADDR
)){
413 /* TODO This may be an UUCP address */
414 agp
->ag_n_flags
|= NAME_ADDRSPEC_ISNAME
;
415 if(!n_alias_is_valid_name(agp
->ag_input
))
416 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
, NAME_ADDRSPEC_ERR_NAME
, '.');
418 /* If we seem to know that this is an address. Ensure this is correct
419 * according to RFC 5322 TODO the entire address parser should be like
420 * TODO that for one, and then we should know whether structured or
421 * TODO unstructured, and just parse correctly overall!
422 * TODO In addition, this can be optimised a lot.
423 * TODO And it is far from perfect: it should not forget whether no
424 * TODO whitespace followed some snippet, and it was written hastily */
426 struct a_token
*t_last
;
427 struct a_token
*t_next
;
433 a_T_TMASK
= (1<<4) - 1,
435 a_T_SPECIAL
= 1<<8 /* An atom actually needs to go TQUOTE */
440 } *thead
, *tcurr
, *tp
;
442 struct n_string ost
, *ostp
;
443 char const *cp
, *cp1st
, *cpmax
, *xp
;
446 /* Name and domain must be non-empty */
447 if(*addr
== '@' || &addr
[2] >= p
|| p
[-2] == '@'){
449 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
, NAME_ADDRSPEC_ERR_ATSEQ
, c
.u
);
455 agp
= a_head_idna_apply(agp
);
460 /* Nothing to do if there is only an address (in angle brackets) */
461 if(agp
->ag_iaddr_start
== 0){
462 if(agp
->ag_iaddr_aend
== agp
->ag_ilen
)
464 }else if(agp
->ag_iaddr_start
== 1 && *cp
== '<' &&
465 agp
->ag_iaddr_aend
== agp
->ag_ilen
- 1 &&
466 cp
[agp
->ag_iaddr_aend
] == '>')
469 /* It is not, so parse off all tokens, then resort and rejoin */
470 lofi_snap
= n_lofi_snap_create();
473 if((c
.ui32
= agp
->ag_iaddr_start
) > 0)
477 thead
= tcurr
= NULL
;
480 for(tp
= NULL
; cp
< cpmax
;){
484 tp
->t_end
= PTR2SIZE(cp
- cp1st
);
485 tp
= n_lofi_alloc(sizeof *tp
);
487 if((tp
->t_last
= tcurr
) != NULL
)
493 tp
->t_start
= PTR2SIZE(++cp
- cp1st
);
494 xp
= skip_comment(cp
);
495 tp
->t_end
= PTR2SIZE(xp
- cp1st
);
497 if(tp
->t_end
> tp
->t_start
){
501 /* No closing comment - strip trailing whitespace */
502 while(blankchar(*--xp
))
503 if(--tp
->t_end
== tp
->t_start
)
512 tp
->t_end
= PTR2SIZE(cp
- cp1st
);
513 tp
= n_lofi_alloc(sizeof *tp
);
515 if((tp
->t_last
= tcurr
) != NULL
)
520 tp
->t_f
= a_T_TQUOTE
;
521 tp
->t_start
= PTR2SIZE(++cp
- cp1st
);
522 for(xp
= cp
; xp
< cpmax
; ++xp
){
523 if((c
.c
= *xp
) == '"')
525 if(c
.c
== '\\' && xp
[1] != '\0')
528 tp
->t_end
= PTR2SIZE(xp
- cp1st
);
530 if(tp
->t_end
> tp
->t_start
){
531 /* No closing quote - strip trailing whitespace */
533 while(blankchar(*xp
--))
534 if(--tp
->t_end
== tp
->t_start
)
544 tp
->t_end
= PTR2SIZE(cp
- cp1st
);
551 tp
= n_lofi_alloc(sizeof *tp
);
553 if((tp
->t_last
= tcurr
) != NULL
)
559 tp
->t_start
= PTR2SIZE(cp
- cp1st
);
563 /* Reverse solidus transforms the following into a quoted-pair, and
564 * therefore (must occur in comment or quoted-string only) the
565 * entire atom into a quoted string */
567 tp
->t_f
|= a_T_SPECIAL
;
573 /* Is this plain RFC 5322 "atext", or "specials"?
574 * TODO Because we don't know structured/unstructured, nor anything
575 * TODO else, we need to treat "dot-atom" as being identical to
577 * However, if the 8th bit is set, this will be RFC 2047 converted
578 * and the entire sequence is skipped */
579 if(!(c
.u
& 0x80) && !alnumchar(c
.c
) &&
580 c
.c
!= '!' && c
.c
!= '#' && c
.c
!= '$' && c
.c
!= '%' &&
581 c
.c
!= '&' && c
.c
!= '\'' && c
.c
!= '*' && c
.c
!= '+' &&
582 c
.c
!= '-' && c
.c
!= '/' && c
.c
!= '=' && c
.c
!= '?' &&
583 c
.c
!= '^' && c
.c
!= '_' && c
.c
!= '`' && c
.c
!= '{' &&
584 c
.c
!= '}' && c
.c
!= '|' && c
.c
!= '}' && c
.c
!= '~')
585 tp
->t_f
|= a_T_SPECIAL
;
590 tp
->t_end
= PTR2SIZE(cp
- cp1st
);
594 tp
= n_lofi_alloc(sizeof *tp
);
596 if((tp
->t_last
= tcurr
) != NULL
)
602 tp
->t_start
= agp
->ag_iaddr_start
;
603 tp
->t_end
= agp
->ag_iaddr_aend
;
606 cp
= &agp
->ag_input
[agp
->ag_iaddr_aend
+ 1];
607 cpmax
= &agp
->ag_input
[agp
->ag_ilen
];
612 /* Nothing may follow the address, move it to the end */
613 if(!(tcurr
->t_f
& a_T_TADDR
)){
614 for(tp
= thead
; tp
!= NULL
; tp
= tp
->t_next
){
615 if(tp
->t_f
& a_T_TADDR
){
616 if(tp
->t_last
!= NULL
)
617 tp
->t_last
->t_next
= tp
->t_next
;
620 if(tp
->t_next
!= NULL
)
621 tp
->t_next
->t_last
= tp
->t_last
;
624 while(tp
->t_next
!= NULL
)
628 tcurr
->t_next
= NULL
;
634 /* Make ranges contiguous: ensure a continuous range of atoms is converted
635 * to a SPECIAL one if at least one of them requires it */
636 for(tp
= thead
; tp
!= NULL
; tp
= tp
->t_next
){
637 if(tp
->t_f
& a_T_SPECIAL
){
639 while((tp
= tp
->t_last
) != NULL
&& (tp
->t_f
& a_T_TATOM
))
640 tp
->t_f
|= a_T_SPECIAL
;
642 while((tp
= tp
->t_next
) != NULL
&& (tp
->t_f
& a_T_TATOM
))
643 tp
->t_f
|= a_T_SPECIAL
;
647 /* And yes, we want quotes to extend as much as possible */
648 for(tp
= thead
; tp
!= NULL
; tp
= tp
->t_next
){
649 if(tp
->t_f
& a_T_TQUOTE
){
651 while((tp
= tp
->t_last
) != NULL
&& (tp
->t_f
& a_T_TATOM
))
652 tp
->t_f
|= a_T_SPECIAL
;
654 while((tp
= tp
->t_next
) != NULL
&& (tp
->t_f
& a_T_TATOM
))
655 tp
->t_f
|= a_T_SPECIAL
;
660 ostp
= n_string_creat_auto(&ost
);
661 if((c
.ui32
= agp
->ag_ilen
) <= UI32_MAX
>> 1)
662 ostp
= n_string_reserve(ostp
, c
.ui32
<<= 1);
664 for(tcurr
= thead
; tcurr
!= NULL
;){
666 ostp
= n_string_push_c(ostp
, ' ');
667 if(tcurr
->t_f
& a_T_TADDR
){
668 ostp
= n_string_push_c(ostp
, '<');
669 agp
->ag_iaddr_start
= ostp
->s_len
;
670 ostp
= n_string_push_buf(ostp
, &cp1st
[tcurr
->t_start
],
671 (tcurr
->t_end
- tcurr
->t_start
));
672 agp
->ag_iaddr_aend
= ostp
->s_len
;
673 ostp
= n_string_push_c(ostp
, '>');
674 tcurr
= tcurr
->t_next
;
675 }else if(tcurr
->t_f
& a_T_TCOMM
){
676 ostp
= n_string_push_c(ostp
, '(');
677 ostp
= n_string_push_buf(ostp
, &cp1st
[tcurr
->t_start
],
678 (tcurr
->t_end
- tcurr
->t_start
));
679 while((tp
= tcurr
->t_next
) != NULL
&& (tp
->t_f
& a_T_TCOMM
)){
681 ostp
= n_string_push_c(ostp
, ' '); /* XXX may be artificial */
682 ostp
= n_string_push_buf(ostp
, &cp1st
[tcurr
->t_start
],
683 (tcurr
->t_end
- tcurr
->t_start
));
685 ostp
= n_string_push_c(ostp
, ')');
686 tcurr
= tcurr
->t_next
;
687 }else if(tcurr
->t_f
& a_T_TQUOTE
){
689 ostp
= n_string_push_c(ostp
, '"');
691 do/* while tcurr && TATOM||TQUOTE */{
692 cp
= &cp1st
[tcurr
->t_start
];
693 cpmax
= &cp1st
[tcurr
->t_end
];
698 ostp
= n_string_push_c(ostp
, ' ');
700 if((tcurr
->t_f
& (a_T_TATOM
| a_T_SPECIAL
)) == a_T_TATOM
)
701 ostp
= n_string_push_buf(ostp
, cp
, PTR2SIZE(cpmax
- cp
));
705 for(esc
= FAL0
; cp
< cpmax
;){
706 if((c
.c
= *cp
++) == '\\' && !esc
){
707 if(cp
< cpmax
&& (*cp
== '"' || *cp
== '\\'))
710 if(esc
|| c
.c
== '"'){
712 ostp
= n_string_push_c(ostp
, '\\');
714 ostp
= n_string_push_c(ostp
, c
.c
);
723 }while((tcurr
= tcurr
->t_next
) != NULL
&&
724 (tcurr
->t_f
& (a_T_TATOM
| a_T_TQUOTE
)));
725 ostp
= n_string_push_c(ostp
, '"');
726 }else if(tcurr
->t_f
& a_T_SPECIAL
)
729 /* Can we use a fast join mode? */
730 for(tp
= tcurr
; tcurr
!= NULL
; tcurr
= tcurr
->t_next
){
731 if(!(tcurr
->t_f
& a_T_TATOM
))
734 ostp
= n_string_push_c(ostp
, ' ');
735 ostp
= n_string_push_buf(ostp
, &cp1st
[tcurr
->t_start
],
736 (tcurr
->t_end
- tcurr
->t_start
));
741 n_lofi_snap_unroll(lofi_snap
);
743 agp
->ag_input
= n_string_cp(ostp
);
744 agp
->ag_ilen
= ostp
->s_len
;
745 /*ostp = n_string_drop_ownership(ostp);*/
749 return ((agp
->ag_n_flags
& NAME_ADDRSPEC_INVALID
) == 0);
753 gethfield(FILE *f
, char **linebuf
, size_t *linesize
, int rem
, char **colon
)
755 char *line2
= NULL
, *cp
, *cp2
;
756 size_t line2size
= 0;
760 if (*linebuf
== NULL
)
761 *linebuf
= srealloc(*linebuf
, *linesize
= 1);
768 if ((c
= readline_restart(f
, linebuf
, linesize
, 0)) <= 0) {
772 for (cp
= *linebuf
; fieldnamechar(*cp
); ++cp
)
775 while (blankchar(*cp
))
777 if (*cp
!= ':' || cp
== *linebuf
)
780 /* I guess we got a headline. Handle wraparound */
785 while (PTRCMP(--cp
, >=, *linebuf
) && blankchar(*cp
))
790 if (PTRCMP(cp
- 8, >=, *linebuf
) && cp
[-1] == '=' && cp
[-2] == '?')
792 ungetc(c
= getc(f
), f
);
795 c
= readline_restart(f
, &line2
, &line2size
, 0);
799 for (cp2
= line2
; blankchar(*cp2
); ++cp2
)
801 c
-= (int)PTR2SIZE(cp2
- line2
);
802 if (cp2
[0] == '=' && cp2
[1] == '?' && c
> 8)
804 if (PTRCMP(cp
+ c
, >=, *linebuf
+ *linesize
- 2)) {
805 size_t diff
= PTR2SIZE(cp
- *linebuf
),
806 colondiff
= PTR2SIZE(*colon
- *linebuf
);
807 *linebuf
= srealloc(*linebuf
, *linesize
+= c
+ 2);
808 cp
= &(*linebuf
)[diff
];
809 *colon
= &(*linebuf
)[colondiff
];
827 msgidnextc(char const **cp
, int *status
)
834 assert(status
!= NULL
);
852 *cp
= skip_comment(&(*cp
)[1]);
869 c
= (*status
& 02) ? lowerconv(c
) : c
;
879 charcount(char *str
, int c
)
885 for (i
= 0, cp
= str
; *cp
; ++cp
)
893 nexttoken(char const *cp
)
912 } while (nesting
> 0 && *cp
!= '\0'); /* XXX error? */
913 } else if (blankchar(*cp
) || *cp
== ',')
923 myaddrs(struct header
*hp
) /* TODO */
926 char const *rv
, *mta
;
929 if (hp
!= NULL
&& (np
= hp
->h_from
) != NULL
) {
930 if ((rv
= np
->n_fullname
) != NULL
)
932 if ((rv
= np
->n_name
) != NULL
)
936 if((rv
= ok_vlook(from
)) != NULL
){
937 if((np
= lextract(rv
, GEXTRA
| GFULL
)) == NULL
)
939 n_err(_("An address given in *from* is invalid: %s\n"), rv
);
940 else for(; np
!= NULL
; np
= np
->n_flink
)
941 if(is_addr_invalid(np
, EACM_STRICT
| EACM_NOLOG
| EACM_NONAME
))
946 /* When invoking *sendmail* directly, it's its task to generate an otherwise
947 * undeterminable From: address. However, if the user sets *hostname*,
948 * accept his desire */
949 if (ok_vlook(hostname
) != NULL
)
951 if (ok_vlook(smtp
) != NULL
|| /* TODO obsolete -> mta */
952 /* TODO pretty hacky for now (this entire fun), later: url_creat()! */
953 ((mta
= n_servbyname(ok_vlook(mta
), NULL
)) != NULL
&& *mta
!= '\0'))
964 hn
= n_nodename(TRU1
);
965 ln
= ok_vlook(LOGNAME
);
966 i
= strlen(ln
) + strlen(hn
) + 1 +1;
968 sstpcpy(sstpcpy(sstpcpy(cp
, ln
), n_at
), hn
);
974 myorigin(struct header
*hp
) /* TODO */
976 char const *rv
= NULL
, *ccp
;
980 if((ccp
= myaddrs(hp
)) != NULL
&&
981 (np
= lextract(ccp
, GEXTRA
| GFULL
)) != NULL
){
982 if(np
->n_flink
== NULL
)
984 else if((ccp
= ok_vlook(sender
)) != NULL
) {
985 if((np
= lextract(ccp
, GEXTRA
| GFULL
)) == NULL
||
986 np
->n_flink
!= NULL
||
987 is_addr_invalid(np
, EACM_STRICT
| EACM_NOLOG
| EACM_NONAME
))
988 n_err(_("The address given in *sender* is invalid: %s\n"), ccp
);
998 is_head(char const *linebuf
, size_t linelen
, bool_t check_rfc4155
)
1000 char date
[n_FROM_DATEBUF
];
1004 if ((rv
= (linelen
>= 5 && !memcmp(linebuf
, "From ", 5))) && check_rfc4155
&&
1005 (extract_date_from_from_(linebuf
, linelen
, date
) <= 0 ||
1013 extract_date_from_from_(char const *line
, size_t linelen
,
1014 char datebuf
[n_FROM_DATEBUF
])
1017 char const *cp
= line
;
1023 cp
= _from__skipword(cp
);
1027 cp
= _from__skipword(cp
);
1030 if((cp
[0] == 't' || cp
[0] == 'T') && (cp
[1] == 't' || cp
[1] == 'T') &&
1031 (cp
[2] == 'y' || cp
[2] == 'Y')){
1032 cp
= _from__skipword(cp
);
1036 /* It seems there are invalid MBOX archives in the wild, compare
1037 * . http://bugs.debian.org/624111
1038 * . [Mutt] #3868: mutt should error if the imported mailbox is invalid
1039 * What they do is that they obfuscate the address to "name at host",
1040 * and even "name at host dot dom dot dom.
1041 * The [Aa][Tt] is also RFC 733, so be tolerant */
1042 else if((cp
[0] == 'a' || cp
[0] == 'A') && (cp
[1] == 't' || cp
[1] == 'T') &&
1047 cp
= _from__skipword(cp
);
1050 if((cp
[0] == 'd' || cp
[0] == 'D') && (cp
[1] == 'o' || cp
[1] == 'O') &&
1051 (cp
[2] == 't' || cp
[2] == 'T') && cp
[3] == ' '){
1057 linelen
-= PTR2SIZE(cp
- line
);
1058 if (linelen
< a_HEAD_DATE_MINLEN
)
1060 if (cp
[linelen
- 1] == '\n') {
1062 /* (Rather IMAP/POP3 only) */
1063 if (cp
[linelen
- 1] == '\r')
1065 if (linelen
< a_HEAD_DATE_MINLEN
)
1068 if (linelen
>= n_FROM_DATEBUF
)
1072 memcpy(datebuf
, cp
, linelen
);
1073 datebuf
[linelen
] = '\0';
1077 cp
= _("<Unknown date>");
1078 linelen
= strlen(cp
);
1079 if (linelen
>= n_FROM_DATEBUF
)
1080 linelen
= n_FROM_DATEBUF
;
1086 extract_header(FILE *fp
, struct header
*hp
, si8_t
*checkaddr_err
)
1088 /* See the prototype declaration for the hairy relationship of
1089 * n_poption&n_PO_t_FLAG and/or n_psonce&n_PSO_t_FLAG in here */
1090 struct n_header_field
**hftail
;
1091 struct header nh
, *hq
= &nh
;
1092 char *linebuf
= NULL
/* TODO line pool */, *colon
;
1093 size_t linesize
= 0, seenfields
= 0;
1095 char const *val
, *cp
;
1098 memset(hq
, 0, sizeof *hq
);
1099 if ((n_psonce
& n_PSO_t_FLAG
) && (n_poption
& n_PO_t_FLAG
)) {
1100 hq
->h_to
= hp
->h_to
;
1101 hq
->h_cc
= hp
->h_cc
;
1102 hq
->h_bcc
= hp
->h_bcc
;
1104 hftail
= &hq
->h_user_headers
;
1106 for (lc
= 0; readline_restart(fp
, &linebuf
, &linesize
, 0) > 0; ++lc
)
1109 /* TODO yippieia, cat(check(lextract)) :-) */
1111 while ((lc
= gethfield(fp
, &linebuf
, &linesize
, lc
, &colon
)) >= 0) {
1114 /* We explicitly allow EAF_NAME for some addressees since aliases are not
1115 * yet expanded when we parse these! */
1116 if ((val
= thisfield(linebuf
, "to")) != NULL
) {
1118 hq
->h_to
= cat(hq
->h_to
, checkaddrs(lextract(val
, GTO
| GFULL
),
1119 EACM_NORMAL
| EAF_NAME
| EAF_MAYKEEP
, checkaddr_err
));
1120 } else if ((val
= thisfield(linebuf
, "cc")) != NULL
) {
1122 hq
->h_cc
= cat(hq
->h_cc
, checkaddrs(lextract(val
, GCC
| GFULL
),
1123 EACM_NORMAL
| EAF_NAME
| EAF_MAYKEEP
, checkaddr_err
));
1124 } else if ((val
= thisfield(linebuf
, "bcc")) != NULL
) {
1126 hq
->h_bcc
= cat(hq
->h_bcc
, checkaddrs(lextract(val
, GBCC
| GFULL
),
1127 EACM_NORMAL
| EAF_NAME
| EAF_MAYKEEP
, checkaddr_err
));
1128 } else if ((val
= thisfield(linebuf
, "from")) != NULL
) {
1129 if (!(n_psonce
& n_PSO_t_FLAG
) || (n_poption
& n_PO_t_FLAG
)) {
1131 hq
->h_from
= cat(hq
->h_from
,
1132 checkaddrs(lextract(val
, GEXTRA
| GFULL
| GFULLEXTRA
),
1133 EACM_STRICT
, NULL
));
1135 } else if ((val
= thisfield(linebuf
, "reply-to")) != NULL
) {
1137 hq
->h_reply_to
= cat(hq
->h_reply_to
,
1138 checkaddrs(lextract(val
, GEXTRA
| GFULL
), EACM_STRICT
, NULL
));
1139 } else if ((val
= thisfield(linebuf
, "sender")) != NULL
) {
1140 if (!(n_psonce
& n_PSO_t_FLAG
) || (n_poption
& n_PO_t_FLAG
)) {
1142 hq
->h_sender
= cat(hq
->h_sender
, /* TODO cat? check! */
1143 checkaddrs(lextract(val
, GEXTRA
| GFULL
| GFULLEXTRA
),
1144 EACM_STRICT
, NULL
));
1147 } else if ((val
= thisfield(linebuf
, "subject")) != NULL
||
1148 (val
= thisfield(linebuf
, "subj")) != NULL
) {
1150 for (cp
= val
; blankchar(*cp
); ++cp
)
1152 hq
->h_subject
= (hq
->h_subject
!= NULL
)
1153 ? save2str(hq
->h_subject
, cp
) : savestr(cp
);
1155 /* The remaining are mostly hacked in and thus TODO -- at least in
1156 * TODO respect to their content checking */
1157 else if((val
= thisfield(linebuf
, "message-id")) != NULL
){
1158 if(n_psonce
& n_PSO_t_FLAG
){
1159 np
= checkaddrs(lextract(val
, GREF
),
1160 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG
| EACM_NONAME
,
1162 if (np
== NULL
|| np
->n_flink
!= NULL
)
1165 hq
->h_message_id
= np
;
1168 }else if((val
= thisfield(linebuf
, "in-reply-to")) != NULL
){
1169 if(n_psonce
& n_PSO_t_FLAG
){
1170 np
= checkaddrs(lextract(val
, GREF
),
1171 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG
| EACM_NONAME
,
1174 hq
->h_in_reply_to
= np
;
1177 }else if((val
= thisfield(linebuf
, "references")) != NULL
){
1178 if(n_psonce
& n_PSO_t_FLAG
){
1180 /* TODO Limit number of references TODO better on parser side */
1181 hq
->h_ref
= cat(hq
->h_ref
, checkaddrs(extract(val
, GREF
),
1182 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG
| EACM_NONAME
,
1187 /* and that is very hairy */
1188 else if((val
= thisfield(linebuf
, "mail-followup-to")) != NULL
){
1189 if(n_psonce
& n_PSO_t_FLAG
){
1191 hq
->h_mft
= cat(hq
->h_mft
, checkaddrs(lextract(val
, GEXTRA
| GFULL
),
1192 /*EACM_STRICT | TODO '/' valid!! | EACM_NOLOG | */EACM_NONAME
,
1197 /* A free-form user header; gethfield() did some verification already.. */
1199 struct n_header_field
*hfp
;
1203 for(nstart
= cp
= linebuf
;; ++cp
)
1204 if(!fieldnamechar(*cp
))
1206 nl
= (ui32_t
)PTR2SIZE(cp
- nstart
);
1208 while(blankchar(*cp
))
1212 n_err(_("Ignoring header field: %s\n"), linebuf
);
1215 while(blankchar(*cp
))
1217 bl
= (ui32_t
)strlen(cp
) +1;
1220 *hftail
= hfp
= salloc(n_VSTRUCT_SIZEOF(struct n_header_field
, hf_dat
1222 hftail
= &hfp
->hf_next
;
1223 hfp
->hf_next
= NULL
;
1225 hfp
->hf_bl
= bl
- 1;
1226 memcpy(hfp
->hf_dat
, nstart
, nl
);
1227 hfp
->hf_dat
[nl
++] = '\0';
1228 memcpy(hfp
->hf_dat
+ nl
, cp
, bl
);
1232 /* In case the blank line after the header has been edited out. Otherwise,
1233 * fetch the header separator */
1234 if (linebuf
!= NULL
) {
1235 if (linebuf
[0] != '\0') {
1236 for (cp
= linebuf
; *(++cp
) != '\0';)
1238 fseek(fp
, (long)-PTR2SIZE(1 + cp
- linebuf
), SEEK_CUR
);
1240 if ((c
= getc(fp
)) != '\n' && c
!= EOF
)
1245 if (seenfields
> 0 && (checkaddr_err
== NULL
|| *checkaddr_err
== 0)) {
1246 hp
->h_to
= hq
->h_to
;
1247 hp
->h_cc
= hq
->h_cc
;
1248 hp
->h_bcc
= hq
->h_bcc
;
1249 hp
->h_from
= hq
->h_from
;
1250 hp
->h_reply_to
= hq
->h_reply_to
;
1251 hp
->h_sender
= hq
->h_sender
;
1252 if (hq
->h_subject
!= NULL
|| !(n_psonce
& n_PSO_t_FLAG
) ||
1253 !(n_poption
& n_PO_t_FLAG
))
1254 hp
->h_subject
= hq
->h_subject
;
1255 hp
->h_user_headers
= hq
->h_user_headers
;
1257 if (n_psonce
& n_PSO_t_FLAG
) {
1258 hp
->h_ref
= hq
->h_ref
;
1259 hp
->h_message_id
= hq
->h_message_id
;
1260 hp
->h_in_reply_to
= hq
->h_in_reply_to
;
1261 hp
->h_mft
= hq
->h_mft
;
1263 /* And perform additional validity checks so that we don't bail later
1264 * on TODO this is good and the place where this should occur,
1265 * TODO unfortunately a lot of other places do again and blabla */
1266 if (hp
->h_from
== NULL
)
1267 hp
->h_from
= n_poption_arg_r
;
1268 else if (hp
->h_from
->n_flink
!= NULL
&& hp
->h_sender
== NULL
)
1269 hp
->h_sender
= lextract(ok_vlook(sender
),
1270 GEXTRA
| GFULL
| GFULLEXTRA
);
1273 n_err(_("Restoring deleted header lines\n"));
1275 if (linebuf
!= NULL
)
1281 hfield_mult(char const *field
, struct message
*mp
, int mult
)
1286 size_t linesize
= 0; /* TODO line pool */
1287 char *linebuf
= NULL
, *colon
;
1291 /* There are (spam) messages which have header bytes which are many KB when
1292 * joined, so resize a single heap storage until we are done if we shall
1293 * collect a field that may have multiple bodies; only otherwise use the
1294 * string dope directly */
1295 memset(&hfs
, 0, sizeof hfs
);
1297 if ((ibuf
= setinput(&mb
, mp
, NEED_HEADER
)) == NULL
)
1299 if ((lc
= mp
->m_lines
- 1) < 0)
1302 if ((mp
->m_flag
& MNOFROM
) == 0 &&
1303 readline_restart(ibuf
, &linebuf
, &linesize
, 0) < 0)
1306 if ((lc
= gethfield(ibuf
, &linebuf
, &linesize
, lc
, &colon
)) < 0)
1308 if ((hfield
= thisfield(linebuf
, field
)) != NULL
&& *hfield
!= '\0') {
1310 n_str_add_buf(&hfs
, hfield
, strlen(hfield
));
1312 hfs
.s
= savestr(hfield
);
1319 if (linebuf
!= NULL
)
1321 if (mult
&& hfs
.s
!= NULL
) {
1322 colon
= savestrbuf(hfs
.s
, hfs
.l
);
1331 thisfield(char const *linebuf
, char const *field
)
1333 char const *rv
= NULL
;
1336 while (lowerconv(*linebuf
) == lowerconv(*field
)) {
1343 while (blankchar(*linebuf
))
1345 if (*linebuf
++ != ':')
1348 while (blankchar(*linebuf
)) /* TODO header parser.. strip trailing WS?!? */
1357 nameof(struct message
*mp
, int reptype
)
1362 cp
= skin(name1(mp
, reptype
));
1363 if (reptype
!= 0 || charcount(cp
, '!') < 2)
1365 cp2
= strrchr(cp
, '!');
1367 while (cp2
> cp
&& *cp2
!= '!')
1377 skip_comment(char const *cp
)
1382 for (nesting
= 1; nesting
> 0 && *cp
; ++cp
) {
1401 routeaddr(char const *name
)
1403 char const *np
, *rp
= NULL
;
1406 for (np
= name
; *np
; np
++) {
1409 np
= skip_comment(np
+ 1) - 1;
1415 if (*np
== '\\' && np
[1])
1432 FL
enum expand_addr_flags
1433 expandaddr_to_eaf(void)
1436 char const *eafd_name
;
1437 bool_t eafd_is_target
;
1441 {"restrict", FAL0
, EAF_TARGET_MASK
, EAF_RESTRICT
| EAF_RESTRICT_TARGETS
},
1442 {"fail", FAL0
, EAF_NONE
, EAF_FAIL
},
1443 {"failinvaddr", FAL0
, EAF_NONE
, EAF_FAILINVADDR
| EAF_ADDR
},
1444 {"all", TRU1
, EAF_NONE
, EAF_TARGET_MASK
},
1445 {"file", TRU1
, EAF_NONE
, EAF_FILE
},
1446 {"pipe", TRU1
, EAF_NONE
, EAF_PIPE
},
1447 {"name", TRU1
, EAF_NONE
, EAF_NAME
},
1448 {"addr", TRU1
, EAF_NONE
, EAF_ADDR
}
1452 enum expand_addr_flags rv
;
1456 if ((cp
= ok_vlook(expandaddr
)) == NULL
)
1457 rv
= EAF_RESTRICT_TARGETS
;
1458 else if (*cp
== '\0')
1459 rv
= EAF_TARGET_MASK
;
1461 rv
= EAF_TARGET_MASK
;
1463 for (buf
= savestr(cp
); (cp
= n_strsep(&buf
, ',', TRU1
)) != NULL
;) {
1466 if ((minus
= (*cp
== '-')) || *cp
== '+')
1468 for (eafp
= eafa
;; ++eafp
) {
1469 if (eafp
== eafa
+ n_NELEM(eafa
)) {
1470 if (n_poption
& n_PO_D_V
)
1471 n_err(_("Unknown *expandaddr* value: %s\n"), cp
);
1473 } else if (!asccasecmp(cp
, eafp
->eafd_name
)) {
1475 rv
&= ~eafp
->eafd_andoff
;
1476 rv
|= eafp
->eafd_or
;
1478 if (eafp
->eafd_is_target
)
1479 rv
&= ~eafp
->eafd_or
;
1480 else if (n_poption
& n_PO_D_V
)
1481 n_err(_("minus - prefix invalid for *expandaddr* value: "
1485 } else if (!asccasecmp(cp
, "noalias")) { /* TODO v15 OBSOLETE */
1486 n_OBSOLETE(_("*expandaddr*: noalias is henceforth -name"));
1493 if((rv
& EAF_RESTRICT
) && ((n_psonce
& n_PSO_INTERACTIVE
) ||
1494 (n_poption
& n_PO_TILDE_FLAG
)))
1495 rv
|= EAF_TARGET_MASK
;
1496 else if(n_poption
& n_PO_D_V
){
1497 if(!(rv
& EAF_TARGET_MASK
))
1498 n_err(_("*expandaddr* doesn't allow any addressees\n"));
1499 else if((rv
& EAF_FAIL
) && (rv
& EAF_TARGET_MASK
) == EAF_TARGET_MASK
)
1500 n_err(_("*expandaddr* with fail, but no restrictions to apply\n"));
1508 is_addr_invalid(struct name
*np
, enum expand_addr_check_mode eacm
)
1510 char cbuf
[sizeof "'\\U12340'"];
1514 enum expand_addr_flags eaf
;
1517 eaf
= expandaddr_to_eaf();
1520 if ((rv
= ((f
& NAME_ADDRSPEC_INVALID
) != 0))) {
1521 if (eaf
& EAF_FAILINVADDR
)
1524 if ((eacm
& EACM_NOLOG
) || (f
& NAME_ADDRSPEC_ERR_EMPTY
)) {
1528 char const *fmt
= "'\\x%02X'";
1529 bool_t ok8bit
= TRU1
;
1531 if (f
& NAME_ADDRSPEC_ERR_IDNA
) {
1532 cs
= _("Invalid domain name: %s, character %s\n");
1535 } else if (f
& NAME_ADDRSPEC_ERR_ATSEQ
)
1536 cs
= _("%s contains invalid %s sequence\n");
1537 else if (f
& NAME_ADDRSPEC_ERR_NAME
) {
1538 cs
= _("%s is an invalid alias name\n");
1540 cs
= _("%s contains invalid byte %s\n");
1542 c
= NAME_ADDRSPEC_ERR_GETWC(f
);
1543 snprintf(cbuf
, sizeof cbuf
,
1544 (ok8bit
&& c
>= 040 && c
<= 0177 ? "'%c'" : fmt
), c
);
1550 /* *expandaddr* stuff */
1551 if (!(rv
= ((eacm
& EACM_MODE_MASK
) != EACM_NONE
)))
1554 if ((eacm
& EACM_STRICT
) && (f
& NAME_ADDRSPEC_ISFILEORPIPE
)) {
1557 cs
= _("%s%s: file or pipe addressees not allowed here\n");
1558 if (eacm
& EACM_NOLOG
)
1564 eaf
|= (eacm
& EAF_TARGET_MASK
);
1565 if (eacm
& EACM_NONAME
)
1568 if (eaf
== EAF_NONE
) {
1575 if (!(eaf
& EAF_FILE
) && (f
& NAME_ADDRSPEC_ISFILE
)) {
1576 cs
= _("%s%s: *expandaddr* doesn't allow file target\n");
1577 if (eacm
& EACM_NOLOG
)
1579 } else if (!(eaf
& EAF_PIPE
) && (f
& NAME_ADDRSPEC_ISPIPE
)) {
1580 cs
= _("%s%s: *expandaddr* doesn't allow command pipe target\n");
1581 if (eacm
& EACM_NOLOG
)
1583 } else if (!(eaf
& EAF_NAME
) && (f
& NAME_ADDRSPEC_ISNAME
)) {
1584 cs
= _("%s%s: *expandaddr* doesn't allow user name target\n");
1585 if (eacm
& EACM_NOLOG
)
1587 } else if (!(eaf
& EAF_ADDR
) && (f
& NAME_ADDRSPEC_ISADDR
)) {
1588 cs
= _("%s%s: *expandaddr* doesn't allow mail address target\n");
1589 if (eacm
& EACM_NOLOG
)
1599 n_err(cs
, n_shexp_quote_cp(np
->n_name
, TRU1
), cbuf
);
1606 skin(char const *name
)
1608 struct n_addrguts ag
;
1613 /*name =*/ n_addrspec_with_guts(&ag
, name
, TRU1
, FAL0
);
1615 if(!(ag
.ag_n_flags
& NAME_NAME_SALLOC
))
1616 rv
= savestrbuf(rv
, ag
.ag_slen
);
1623 /* TODO addrspec_with_guts: RFC 5322
1624 * TODO addrspec_with_guts: trim whitespace ETC. ETC. ETC.!!! */
1626 n_addrspec_with_guts(struct n_addrguts
*agp
, char const *name
, bool_t doskin
,
1627 bool_t issingle_hack
){
1629 char *cp2
, *bufend
, *nbuf
, c
;
1639 memset(agp
, 0, sizeof *agp
);
1641 if((agp
->ag_input
= name
) == NULL
|| (agp
->ag_ilen
= strlen(name
)) == 0){
1642 agp
->ag_skinned
= n_UNCONST(n_empty
); /* ok: NAME_SALLOC is not set */
1644 agp
->ag_n_flags
|= NAME_ADDRSPEC_CHECKED
;
1645 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
, NAME_ADDRSPEC_ERR_EMPTY
, 0);
1648 /*agp->ag_iaddr_start = 0;*/
1649 agp
->ag_iaddr_aend
= agp
->ag_ilen
;
1650 agp
->ag_skinned
= n_UNCONST(name
); /* (NAME_SALLOC not set) */
1651 agp
->ag_slen
= agp
->ag_ilen
;
1652 agp
->ag_n_flags
= NAME_SKINNED
;
1657 nbuf
= n_lofi_alloc(agp
->ag_ilen
+1);
1658 /*agp->ag_iaddr_start = 0;*/
1659 cp2
= bufend
= nbuf
;
1661 /* TODO This is complete crap and should use a token parser */
1662 for(cp
= name
++; (c
= *cp
++) != '\0';){
1665 cp
= skip_comment(cp
);
1669 /* Start of a "quoted-string". Copy it in its entirety */
1670 /* XXX RFC: quotes are "semantically invisible"
1671 * XXX But it was explicitly added (Changelog.Heirloom,
1672 * XXX [9.23] released 11/15/00, "Do not remove quotes
1673 * XXX when skinning names"? No more info.. */
1675 while ((c
= *cp
) != '\0') { /* TODO improve */
1683 else if ((c
= *cp
) != '\0') {
1692 if((flags
& (a_GOTADDR
| a_GOTSPACE
)) == a_GOTADDR
){
1693 flags
|= a_GOTSPACE
;
1694 agp
->ag_iaddr_aend
= PTR2SIZE(cp
- name
);
1696 if (cp
[0] == 'a' && cp
[1] == 't' && blankchar(cp
[2]))
1697 cp
+= 3, *cp2
++ = '@';
1698 else if (cp
[0] == '@' && blankchar(cp
[1]))
1699 cp
+= 2, *cp2
++ = '@';
1704 agp
->ag_iaddr_start
= PTR2SIZE(cp
- (name
- 1));
1706 flags
&= ~(a_GOTSPACE
| a_LASTSP
);
1707 flags
|= a_GOTLT
| a_GOTADDR
;
1710 if(flags
& a_GOTLT
){
1711 /* (_addrspec_check() verifies these later!) */
1712 flags
&= ~(a_GOTLT
| a_LASTSP
);
1713 agp
->ag_iaddr_aend
= PTR2SIZE(cp
- name
);
1715 /* Skip over the entire remaining field */
1716 while((c
= *cp
) != '\0' && c
!= ','){
1719 cp
= skip_comment(cp
);
1721 while ((c
= *cp
) != '\0') {
1725 if (c
== '\\' && *cp
!= '\0')
1733 if(flags
& a_LASTSP
){
1735 if(flags
& a_GOTADDR
)
1739 /* This character is forbidden here, but it may nonetheless be
1740 * present: ensure we turn this into something valid! (E.g., if the
1741 * next character would be a "..) */
1742 if(c
== '\\' && *cp
!= '\0')
1744 if(c
== ',' && !issingle_hack
){
1745 if(!(flags
& a_GOTLT
)){
1747 for(; blankchar(*cp
); ++cp
)
1752 }else if(!(flags
& a_GOTADDR
)){
1754 agp
->ag_iaddr_start
= PTR2SIZE(cp
- name
);
1759 agp
->ag_slen
= PTR2SIZE(cp2
- nbuf
);
1760 if (agp
->ag_iaddr_aend
== 0)
1761 agp
->ag_iaddr_aend
= agp
->ag_ilen
;
1763 else if (agp
->ag_iaddr_aend
< agp
->ag_iaddr_start
) {
1764 cp2
= n_autorec_alloc(agp
->ag_ilen
+ 1 +1);
1765 memcpy(cp2
, agp
->ag_input
, agp
->ag_ilen
);
1766 agp
->ag_iaddr_aend
= agp
->ag_ilen
;
1767 cp2
[agp
->ag_ilen
++] = '>';
1768 cp2
[agp
->ag_ilen
] = '\0';
1770 agp
->ag_skinned
= savestrbuf(nbuf
, agp
->ag_slen
);
1772 agp
->ag_n_flags
= NAME_NAME_SALLOC
| NAME_SKINNED
;
1774 if(a_head_addrspec_check(agp
, doskin
) <= FAL0
)
1777 name
= agp
->ag_input
;
1784 realname(char const *name
)
1786 char const *cp
, *cq
, *cstart
= NULL
, *cend
= NULL
;
1789 int quoted
, good
, nogood
;
1792 if ((cp
= n_UNCONST(name
)) == NULL
)
1794 for (; *cp
!= '\0'; ++cp
) {
1797 if (cstart
!= NULL
) {
1798 /* More than one comment in address, doesn't make sense to display
1799 * it without context. Return the entire field */
1800 cp
= mime_fromaddr(name
);
1804 cp
= skip_comment(cp
);
1807 cend
= cstart
= NULL
;
1813 if (*cp
== '\\' && cp
[1])
1824 /* More than one address. Just use the first one */
1830 if (cstart
== NULL
) {
1832 /* If name contains only a route-addr, the surrounding angle brackets
1833 * don't serve any useful purpose when displaying, so remove */
1834 cp
= prstr(skin(name
));
1836 cp
= mime_fromaddr(name
);
1840 /* Strip quotes. Note that quotes that appear within a MIME encoded word are
1841 * not stripped. The idea is to strip only syntactical relevant things (but
1842 * this is not necessarily the most sensible way in practice) */
1843 rp
= rname
= ac_alloc(PTR2SIZE(cend
- cstart
+1));
1845 for (cp
= cstart
; cp
< cend
; ++cp
) {
1846 if (*cp
== '(' && !quoted
) {
1847 cq
= skip_comment(++cp
);
1848 if (PTRCMP(--cq
, >, cend
))
1851 if (*cp
== '\\' && PTRCMP(cp
+ 1, <, cq
))
1855 } else if (*cp
== '\\' && PTRCMP(cp
+ 1, <, cend
))
1857 else if (*cp
== '"') {
1866 mime_fromhdr(&in
, &out
, TD_ISPR
| TD_ICONV
);
1868 rname
= savestr(out
.s
);
1871 while (blankchar(*rname
))
1873 for (rp
= rname
; *rp
!= '\0'; ++rp
)
1875 while (PTRCMP(--rp
, >=, rname
) && blankchar(*rp
))
1878 cp
= mime_fromaddr(name
);
1882 /* mime_fromhdr() has converted all nonprintable characters to question
1883 * marks now. These and blanks are considered uninteresting; if the
1884 * displayed part of the real name contains more than 25% of them, it is
1885 * probably better to display the plain email address instead */
1888 for (rp
= rname
; *rp
!= '\0' && PTRCMP(rp
, <, rname
+ 20); ++rp
)
1889 if (*rp
== '?' || blankchar(*rp
))
1893 cp
= (good
* 3 < nogood
) ? prstr(skin(name
)) : rname
;
1896 return n_UNCONST(cp
);
1900 name1(struct message
*mp
, int reptype
)
1902 char *namebuf
, *cp
, *cp2
, *linebuf
= NULL
/* TODO line pool */;
1903 size_t namesize
, linesize
= 0;
1908 if ((cp
= hfield1("from", mp
)) != NULL
&& *cp
!= '\0')
1910 if (reptype
== 0 && (cp
= hfield1("sender", mp
)) != NULL
&& *cp
!= '\0')
1913 namebuf
= smalloc(namesize
= 1);
1915 if (mp
->m_flag
& MNOFROM
)
1917 if ((ibuf
= setinput(&mb
, mp
, NEED_HEADER
)) == NULL
)
1919 if (readline_restart(ibuf
, &linebuf
, &linesize
, 0) < 0)
1923 if (namesize
<= linesize
)
1924 namebuf
= srealloc(namebuf
, namesize
= linesize
+1);
1925 for (cp
= linebuf
; *cp
!= '\0' && *cp
!= ' '; ++cp
)
1927 for (; blankchar(*cp
); ++cp
)
1929 for (cp2
= namebuf
+ strlen(namebuf
);
1930 *cp
&& !blankchar(*cp
) && PTRCMP(cp2
, <, namebuf
+ namesize
-1);)
1934 if (readline_restart(ibuf
, &linebuf
, &linesize
, 0) < 0)
1936 if ((cp
= strchr(linebuf
, 'F')) == NULL
)
1938 if (strncmp(cp
, "From", 4))
1940 if (namesize
<= linesize
)
1941 namebuf
= srealloc(namebuf
, namesize
= linesize
+ 1);
1943 while ((cp
= strchr(cp
, 'r')) != NULL
) {
1944 if (!strncmp(cp
, "remote", 6)) {
1945 if ((cp
= strchr(cp
, 'f')) == NULL
)
1947 if (strncmp(cp
, "from", 4) != 0)
1949 if ((cp
= strchr(cp
, ' ')) == NULL
)
1953 strncpy(namebuf
, cp
, namesize
);
1956 cp2
= strrchr(namebuf
, '!') + 1;
1957 strncpy(cp2
, cp
, PTR2SIZE(namebuf
+ namesize
- cp2
));
1959 namebuf
[namesize
- 2] = '!';
1960 namebuf
[namesize
- 1] = '\0';
1966 if (*namebuf
!= '\0' || ((cp
= hfield1("return-path", mp
))) == NULL
||
1968 cp
= savestr(namebuf
);
1970 if (linebuf
!= NULL
)
1979 subject_re_trim(char const *s
){
1983 }const *pp
, ignored
[] = { /* Update *reply-strings* manual upon change! */
1985 {3, "aw:"}, {5, "antw:"}, /* de */
1986 {3, "wg:"}, /* Seen too often in the wild */
1991 char *re_st
, *re_st_x
;
2001 if((re_st_x
= ok_vlook(reply_strings
)) != NULL
&&
2002 (re_l
= strlen(re_st_x
)) > 0){
2003 re_st
= n_lofi_alloc(++re_l
* 2);
2004 memcpy(re_st
, re_st_x
, re_l
);
2009 while(spacechar(*s
))
2012 for(pp
= ignored
; pp
->len
> 0; ++pp
)
2013 if(is_asccaseprefix(pp
->dat
, s
)){
2022 memcpy(re_st_x
= &re_st
[re_l
], re_st
, re_l
);
2023 while((cp
= n_strsep(&re_st_x
, ',', TRU1
)) != NULL
)
2024 if(is_asccaseprefix(cp
, s
)){
2036 return any
? s
: orig_s
;
2040 msgidcmp(char const *s1
, char const *s2
)
2042 int q1
= 0, q2
= 0, c1
, c2
;
2051 c1
= msgidnextc(&s1
, &q1
);
2052 c2
= msgidnextc(&s2
, &q2
);
2061 fakefrom(struct message
*mp
)
2066 if (((name
= skin(hfield1("return-path", mp
))) == NULL
|| *name
== '\0' ) &&
2067 ((name
= skin(hfield1("from", mp
))) == NULL
|| *name
== '\0'))
2068 /* XXX MAILER-DAEMON is what an old MBOX manual page says.
2069 * RFC 4155 however requires a RFC 5322 (2822) conforming
2070 * "addr-spec", but we simply can't provide that */
2071 name
= "MAILER-DAEMON";
2083 for (cq
= cp
; *cq
!= '\0' && *cq
!= '\n'; ++cq
)
2091 #if defined HAVE_IMAP_SEARCH || defined HAVE_IMAP
2093 unixtime(char const *fromline
)
2095 char const *fp
, *xp
;
2097 si32_t i
, year
, month
, day
, hour
, minute
, second
, tzdiff
;
2101 for (fp
= fromline
; *fp
!= '\0' && *fp
!= '\n'; ++fp
)
2104 if (PTR2SIZE(fp
- fromline
) < 7)
2109 if (!strncmp(fp
+ 4, n_month_names
[i
], 3))
2111 if (n_month_names
[++i
][0] == '\0')
2117 n_idec_si32_cp(&day
, &fp
[8], 10, &xp
);
2118 if (*xp
!= ' ' || xp
!= fp
+ 10)
2120 n_idec_si32_cp(&hour
, &fp
[11], 10, &xp
);
2121 if (*xp
!= ':' || xp
!= fp
+ 13)
2123 n_idec_si32_cp(&minute
, &fp
[14], 10, &xp
);
2124 if (*xp
!= ':' || xp
!= fp
+ 16)
2126 n_idec_si32_cp(&second
, &fp
[17], 10, &xp
);
2127 if (*xp
!= ' ' || xp
!= fp
+ 19)
2129 n_idec_si32_cp(&year
, &fp
[20], 10, &xp
);
2132 if ((t
= combinetime(year
, month
, day
, hour
, minute
, second
)) == (time_t)-1)
2134 tzdiff
= t
- mktime(gmtime(&t
));
2135 tmptr
= localtime(&t
);
2136 if (tmptr
->tm_isdst
> 0)
2146 #endif /* HAVE_IMAP_SEARCH || HAVE_IMAP */
2149 rfctime(char const *date
) /* TODO n_idec_ return tests */
2153 si32_t i
, year
, month
, day
, hour
, minute
, second
;
2158 if ((cp
= nexttoken(cp
)) == NULL
)
2160 if (alphachar(cp
[0]) && alphachar(cp
[1]) && alphachar(cp
[2]) &&
2162 if ((cp
= nexttoken(&cp
[4])) == NULL
)
2165 n_idec_si32_cp(&day
, cp
, 10, &x
);
2166 if ((cp
= nexttoken(x
)) == NULL
)
2169 if (!strncmp(cp
, n_month_names
[i
], 3))
2171 if (n_month_names
[++i
][0] == '\0')
2175 if ((cp
= nexttoken(&cp
[3])) == NULL
)
2178 * Where a two or three digit year occurs in a date, the year is to be
2179 * interpreted as follows: If a two digit year is encountered whose
2180 * value is between 00 and 49, the year is interpreted by adding 2000,
2181 * ending up with a value between 2000 and 2049. If a two digit year
2182 * is encountered with a value between 50 and 99, or any three digit
2183 * year is encountered, the year is interpreted by adding 1900 */
2184 n_idec_si32_cp(&year
, cp
, 10, &x
);
2185 i
= (int)PTR2SIZE(x
- cp
);
2186 if (i
== 2 && year
>= 0 && year
<= 49)
2188 else if (i
== 3 || (i
== 2 && year
>= 50 && year
<= 99))
2190 if ((cp
= nexttoken(x
)) == NULL
)
2192 n_idec_si32_cp(&hour
, cp
, 10, &x
);
2196 n_idec_si32_cp(&minute
, cp
, 10, &x
);
2199 n_idec_si32_cp(&second
, cp
, 10, &x
);
2203 if ((t
= combinetime(year
, month
, day
, hour
, minute
, second
)) == (time_t)-1)
2205 if ((cp
= nexttoken(x
)) != NULL
) {
2217 if (digitchar(cp
[0]) && digitchar(cp
[1]) && digitchar(cp
[2]) &&
2224 n_idec_si32_cp(&i
, buf
, 10, NULL
);
2225 tadj
= (si64_t
)i
* 3600; /* XXX */
2228 n_idec_si32_cp(&i
, buf
, 10, NULL
);
2229 tadj
+= (si64_t
)i
* 60; /* XXX */
2234 /* TODO WE DO NOT YET PARSE (OBSOLETE) ZONE NAMES
2235 * TODO once again, Christos Zoulas and NetBSD Mail have done
2236 * TODO a really good job already, but using strptime(3), which
2237 * TODO is not portable. Nonetheless, WE must improve, not
2238 * TODO at last because we simply ignore obsolete timezones!!
2239 * TODO See RFC 5322, 4.3! */
2250 combinetime(int year
, int month
, int day
, int hour
, int minute
, int second
){
2251 size_t const jdn_epoch
= 2440588;
2252 bool_t
const y2038p
= (sizeof(time_t) == 4);
2258 if(UICMP(32, second
, >/*XXX leap=*/, n_DATE_SECSMIN
) ||
2259 UICMP(32, minute
, >=, n_DATE_MINSHOUR
) ||
2260 UICMP(32, hour
, >=, n_DATE_HOURSDAY
) ||
2261 day
< 1 || day
> 31 ||
2262 month
< 1 || month
> 12 ||
2266 if(year
>= 1970 + ((y2038p
? SI32_MAX
: SI64_MAX
) /
2267 (n_DATE_SECSDAY
* n_DATE_DAYSYEAR
))){
2268 /* Be a coward regarding Y2038, many people (mostly myself, that is) do
2269 * test by stepping second-wise around the flip. Don't care otherwise */
2272 if(year
> 2038 || month
> 1 || day
> 19 ||
2273 hour
> 3 || minute
> 14 || second
> 7)
2278 t
+= minute
* n_DATE_SECSMIN
;
2279 t
+= hour
* n_DATE_SECSHOUR
;
2281 jdn
= a_head_gregorian_to_jdn(year
, month
, day
);
2283 t
+= (time_t)jdn
* n_DATE_SECSDAY
;
2293 substdate(struct message
*m
)
2298 /* Determine the date to print in faked 'From ' lines. This is traditionally
2299 * the date the message was written to the mail file. Try to determine this
2300 * using RFC message header fields, or fall back to current time */
2302 if ((cp
= hfield1("received", m
)) != NULL
) {
2303 while ((cp
= nexttoken(cp
)) != NULL
&& *cp
!= ';') {
2306 while (alnumchar(*cp
));
2309 m
->m_time
= rfctime(cp
);
2311 if (m
->m_time
== 0 || m
->m_time
> time_current
.tc_time
) {
2312 if ((cp
= hfield1("date", m
)) != NULL
)
2313 m
->m_time
= rfctime(cp
);
2315 if (m
->m_time
== 0 || m
->m_time
> time_current
.tc_time
)
2316 m
->m_time
= time_current
.tc_time
;
2321 setup_from_and_sender(struct header
*hp
)
2327 /* If -t parsed or composed From: then take it. With -t we otherwise
2328 * want -r to be honoured in favour of *from* in order to have
2329 * a behaviour that is compatible with what users would expect from e.g.
2331 if ((np
= hp
->h_from
) != NULL
||
2332 ((n_psonce
& n_PSO_t_FLAG
) && (np
= n_poption_arg_r
) != NULL
)) {
2334 } else if ((addr
= myaddrs(hp
)) != NULL
)
2335 np
= lextract(addr
, GEXTRA
| GFULL
| GFULLEXTRA
);
2338 if ((np
= hp
->h_sender
) != NULL
) {
2340 } else if ((addr
= ok_vlook(sender
)) != NULL
)
2341 np
= lextract(addr
, GEXTRA
| GFULL
| GFULLEXTRA
);
2347 FL
struct name
const *
2348 check_from_and_sender(struct name
const *fromfield
,
2349 struct name
const *senderfield
)
2351 struct name
const *rv
= NULL
;
2354 if (senderfield
!= NULL
) {
2355 if (senderfield
->n_flink
!= NULL
) {
2356 n_err(_("The Sender: field may contain only one address\n"));
2362 if (fromfield
!= NULL
) {
2363 if (fromfield
->n_flink
!= NULL
&& senderfield
== NULL
) {
2364 n_err(_("A Sender: is required when there are multiple "
2365 "addresses in From:\n"));
2373 rv
= (struct name
*)0x1;
2381 getsender(struct message
*mp
)
2387 if ((cp
= hfield1("from", mp
)) == NULL
||
2388 (np
= lextract(cp
, GEXTRA
| GSKIN
)) == NULL
)
2391 cp
= (np
->n_flink
!= NULL
) ? skin(hfield1("sender", mp
)) : np
->n_name
;
2398 grab_headers(enum n_go_input_flags gif
, struct header
*hp
, enum gfield gflags
,
2401 /* TODO grab_headers: again, check counts etc. against RFC;
2402 * TODO (now assumes check_from_and_sender() is called afterwards ++ */
2408 comma
= (ok_blook(bsdcompat
) || ok_blook(bsdmsgs
)) ? 0 : GCOMMA
;
2411 hp
->h_to
= grab_names(gif
, "To: ", hp
->h_to
, comma
, GTO
| GFULL
);
2412 if (subjfirst
&& (gflags
& GSUBJECT
))
2413 hp
->h_subject
= n_go_input_cp(gif
, "Subject: ", hp
->h_subject
);
2415 hp
->h_cc
= grab_names(gif
, "Cc: ", hp
->h_cc
, comma
, GCC
| GFULL
);
2417 hp
->h_bcc
= grab_names(gif
, "Bcc: ", hp
->h_bcc
, comma
, GBCC
| GFULL
);
2419 if (gflags
& GEXTRA
) {
2420 if (hp
->h_from
== NULL
)
2421 hp
->h_from
= lextract(myaddrs(hp
), GEXTRA
| GFULL
| GFULLEXTRA
);
2422 hp
->h_from
= grab_names(gif
, "From: ", hp
->h_from
, comma
,
2423 GEXTRA
| GFULL
| GFULLEXTRA
);
2424 if (hp
->h_reply_to
== NULL
) {
2425 struct name
*v15compat
;
2427 if((v15compat
= lextract(ok_vlook(replyto
), GEXTRA
| GFULL
)) != NULL
)
2428 n_OBSOLETE(_("please use *reply-to*, not *replyto*"));
2429 hp
->h_reply_to
= lextract(ok_vlook(reply_to
), GEXTRA
| GFULL
);
2430 if(hp
->h_reply_to
== NULL
) /* v15 */
2431 hp
->h_reply_to
= v15compat
;
2433 hp
->h_reply_to
= grab_names(gif
, "Reply-To: ", hp
->h_reply_to
, comma
,
2435 if (hp
->h_sender
== NULL
)
2436 hp
->h_sender
= extract(ok_vlook(sender
), GEXTRA
| GFULL
);
2437 hp
->h_sender
= grab_names(gif
, "Sender: ", hp
->h_sender
, comma
,
2441 if (!subjfirst
&& (gflags
& GSUBJECT
))
2442 hp
->h_subject
= n_go_input_cp(gif
, "Subject: ", hp
->h_subject
);
2449 header_match(struct message
*mp
, struct search_expr
const *sep
)
2454 size_t linesize
= 0; /* TODO line pool */
2455 char *linebuf
= NULL
, *colon
;
2459 if ((ibuf
= setinput(&mb
, mp
, NEED_HEADER
)) == NULL
)
2461 if ((lc
= mp
->m_lines
- 1) < 0)
2464 if ((mp
->m_flag
& MNOFROM
) == 0 &&
2465 readline_restart(ibuf
, &linebuf
, &linesize
, 0) < 0)
2468 if (gethfield(ibuf
, &linebuf
, &linesize
, lc
, &colon
) <= 0)
2470 if (blankchar(*++colon
))
2472 in
.l
= strlen(in
.s
= colon
);
2473 mime_fromhdr(&in
, &out
, TD_ICONV
);
2475 if (sep
->ss_sexpr
== NULL
)
2476 rv
= (regexec(&sep
->ss_regex
, out
.s
, 0,NULL
, 0) != REG_NOMATCH
);
2479 rv
= substr(out
.s
, sep
->ss_sexpr
);
2486 if (linebuf
!= NULL
)
2492 FL
struct n_header_field
*
2493 n_customhdr_query(void){
2495 struct n_header_field
*rv
, **tail
, *hfp
;
2500 if((vp
= ok_vlook(customhdr
)) != NULL
){
2506 while((vp
= n_strsep_esc(&buf
, ',', TRU1
)) != NULL
){
2508 char const *nstart
, *cp
;
2510 for(nstart
= cp
= vp
;; ++cp
){
2511 if(fieldnamechar(*cp
))
2515 n_err(_("Invalid nameless *customhdr* entry\n"));
2518 }else if(*cp
!= ':' && !blankchar(*cp
)){
2520 n_err(_("Invalid *customhdr* entry: %s\n"), vp
);
2525 nl
= (ui32_t
)PTR2SIZE(cp
- nstart
);
2527 while(blankchar(*cp
))
2531 while(blankchar(*cp
))
2533 bl
= (ui32_t
)strlen(cp
) +1;
2536 hfp
= salloc(n_VSTRUCT_SIZEOF(struct n_header_field
, hf_dat
) +
2538 tail
= &hfp
->hf_next
;
2539 hfp
->hf_next
= NULL
;
2541 hfp
->hf_bl
= bl
- 1;
2542 memcpy(hfp
->hf_dat
, nstart
, nl
);
2543 hfp
->hf_dat
[nl
++] = '\0';
2544 memcpy(hfp
->hf_dat
+ nl
, cp
, bl
);