1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Routines for processing and detecting headlines.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2016 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #ifndef HAVE_AMALGAMATION
43 # if HAVE_IDNA == HAVE_IDNA_LIBIDNA
45 # include <idn-free.h>
46 # include <stringprep.h>
47 # elif HAVE_IDNA == HAVE_IDNA_IDNKIT
53 size_t tlen
; /* Length of .tdata */
54 char const *tdata
; /* Template date - see _cmatch_data[] */
57 /* Template characters for cmatch_data.tdata:
58 * 'A' An upper case char
59 * 'a' A lower case char
62 * 'O' An optional digit or space
64 * '+' Either a plus or a minus sign */
65 static struct cmatch_data
const _cmatch_data
[] = {
66 { 24, "Aaa Aaa O0 00:00:00 0000" }, /* BSD/ISO C90 ctime */
67 { 28, "Aaa Aaa O0 00:00:00 AAA 0000" }, /* BSD tmz */
68 { 21, "Aaa Aaa O0 00:00 0000" }, /* SysV ctime */
69 { 25, "Aaa Aaa O0 00:00 AAA 0000" }, /* SysV tmz */
70 /* RFC 822-alike From_ lines do not conform to RFC 4155, but seem to be used
71 * in the wild (by UW-imap) */
72 { 30, "Aaa Aaa O0 00:00:00 0000 +0000" },
73 /* RFC 822 with zone spec; 1. military, 2. UT, 3. north america time
74 * zone strings; note that 1. is strictly speaking not correct as some
75 * letters are not used, and 2. is not because only "UT" is defined */
76 #define __reuse "Aaa Aaa O0 00:00:00 0000 AAA"
77 { 28 - 2, __reuse
}, { 28 - 1, __reuse
}, { 28 - 0, __reuse
},
80 #define _DATE_MINLEN 21
82 /* Skip over "word" as found in From_ line */
83 static char const * _from__skipword(char const *wp
);
85 /* Match the date string against the date template (tp), return if match.
86 * See _cmatch_data[] for template character description */
87 static int _cmatch(size_t len
, char const *date
,
90 /* Check whether date is a valid 'From_' date.
91 * (Rather ctime(3) generated dates, according to RFC 4155) */
92 static int _is_date(char const *date
);
94 /* JulianDayNumber converter(s) */
95 static size_t a_head_gregorian_to_jdn(ui32_t y
, ui32_t m
, ui32_t d
);
97 static void a_head_jdn_to_gregorian(size_t jdn
,
98 ui32_t
*yp
, ui32_t
*mp
, ui32_t
*dp
);
101 /* Convert the domain part of a skinned address to IDNA.
102 * If an error occurs before Unicode information is available, revert the IDNA
103 * error to a normal CHAR one so that the error message doesn't talk Unicode */
105 static struct addrguts
* _idna_apply(struct addrguts
*agp
);
108 /* Classify and check a (possibly skinned) header body according to RFC
109 * *addr-spec* rules; if it (is assumed to has been) skinned it may however be
110 * also a file or a pipe command, so check that first, then.
111 * Otherwise perform content checking and isolate the domain part (for IDNA) */
112 static int _addrspec_check(int doskin
, struct addrguts
*agp
);
114 /* Return the next header field found in the given message.
115 * Return >= 0 if something found, < 0 elsewise.
116 * "colon" is set to point to the colon in the header.
117 * Must deal with \ continuations & other such fraud */
118 static int gethfield(FILE *f
, char **linebuf
, size_t *linesize
,
119 int rem
, char **colon
);
121 static int msgidnextc(char const **cp
, int *status
);
123 /* Count the occurances of c in str */
124 static int charcount(char *str
, int c
);
126 static char const * nexttoken(char const *cp
);
129 _from__skipword(char const *wp
)
135 while ((c
= *wp
++) != '\0' && !blankchar(c
)) {
137 while ((c
= *wp
++) != '\0' && c
!= '"')
143 for (; blankchar(c
); c
= *wp
++)
147 return (c
== 0 ? NULL
: wp
- 1);
151 _cmatch(size_t len
, char const *date
, char const *tp
)
176 if (c
!= ' ' && !digitchar(c
))
184 if (c
!= '+' && c
!= '-')
196 _is_date(char const *date
)
198 struct cmatch_data
const *cmdp
;
203 if ((dl
= strlen(date
)) >= _DATE_MINLEN
)
204 for (cmdp
= _cmatch_data
; cmdp
->tdata
!= NULL
; ++cmdp
)
205 if (dl
== cmdp
->tlen
&& (rv
= _cmatch(dl
, date
, cmdp
->tdata
)))
212 a_head_gregorian_to_jdn(ui32_t y
, ui32_t m
, ui32_t d
){
213 /* Algorithm is taken from Communications of the ACM, Vol 6, No 8.
214 * (via third hand, plus adjustments).
215 * This algorithm is supposed to work for all dates in between 1582-10-15
216 * (0001-01-01 but that not Gregorian) and 65535-12-31 */
255 a_head_jdn_to_gregorian(size_t jdn
, ui32_t
*yp
, ui32_t
*mp
, ui32_t
*dp
){
256 /* Algorithm is taken from Communications of the ACM, Vol 6, No 8.
257 * (via third hand, plus adjustments) */
276 x
= jdn
/ 153; /* x -> month */
279 jdn
/= 5; /* jdn -> day */
287 *yp
= (ui32_t
)(y
& 0xFFFF);
288 *mp
= (ui32_t
)(x
& 0xFF);
289 *dp
= (ui32_t
)(jdn
& 0xFF);
295 # if HAVE_IDNA == HAVE_IDNA_LIBIDNA
296 static struct addrguts
*
297 _idna_apply(struct addrguts
*agp
)
299 char *idna_utf8
, *idna_ascii
, *cs
;
303 sz
= agp
->ag_slen
- agp
->ag_sdom_start
;
305 idna_utf8
= ac_alloc(sz
+1);
306 memcpy(idna_utf8
, agp
->ag_skinned
+ agp
->ag_sdom_start
, sz
);
307 idna_utf8
[sz
] = '\0';
309 /* GNU Libidn settles on top of iconv(3) without any fallback, so let's just
310 * let it perform the charset conversion, if any should be necessary */
311 if (!(options
& OPT_UNICODE
)) {
312 char const *tcs
= charset_get_lc();
313 idna_ascii
= idna_utf8
;
314 idna_utf8
= stringprep_convert(idna_ascii
, "UTF-8", tcs
);
315 i
= (idna_utf8
== NULL
&& errno
== EINVAL
);
318 if (idna_utf8
== NULL
) {
320 n_err(_("Cannot convert from %s to %s\n"), tcs
, "UTF-8");
321 agp
->ag_n_flags
^= NAME_ADDRSPEC_ERR_IDNA
| NAME_ADDRSPEC_ERR_CHAR
;
326 if (idna_to_ascii_8z(idna_utf8
, &idna_ascii
, 0) != IDNA_SUCCESS
) {
327 agp
->ag_n_flags
^= NAME_ADDRSPEC_ERR_IDNA
| NAME_ADDRSPEC_ERR_CHAR
;
331 /* Replace the domain part of .ag_skinned with IDNA version */
332 sz
= strlen(idna_ascii
);
333 i
= agp
->ag_sdom_start
;
334 cs
= salloc(agp
->ag_slen
- i
+ sz
+1);
335 memcpy(cs
, agp
->ag_skinned
, i
);
336 memcpy(cs
+ i
, idna_ascii
, sz
);
340 agp
->ag_skinned
= cs
;
342 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
,
343 NAME_NAME_SALLOC
| NAME_SKINNED
| NAME_IDNA
, 0);
345 idn_free(idna_ascii
);
347 if (options
& OPT_UNICODE
)
356 # elif HAVE_IDNA == HAVE_IDNA_IDNKIT /* IDNA==LIBIDNA */
357 static struct addrguts
*
358 _idna_apply(struct addrguts
*agp
)
360 char *idna_in
, *idna_out
, *cs
;
365 sz
= agp
->ag_slen
- agp
->ag_sdom_start
;
367 idna_in
= ac_alloc(sz
+1);
368 memcpy(idna_in
, agp
->ag_skinned
+ agp
->ag_sdom_start
, sz
);
371 for (idna_out
= NULL
, sz
= HOST_NAME_MAX
+1;; sz
+= HOST_NAME_MAX
) {
372 idna_out
= ac_alloc(sz
);
374 r
= idn_encodename(IDN_ENCODE_APP
, idna_in
, idna_out
, sz
);
377 case idn_buffer_overflow
:
379 case idn_invalid_encoding
:
380 n_err(_("Cannot convert from %s to %s\n"), charset_get_lc(), "UTF-8");
383 agp
->ag_n_flags
^= NAME_ADDRSPEC_ERR_IDNA
| NAME_ADDRSPEC_ERR_CHAR
;
387 if (r
== idn_success
)
392 /* Replace the domain part of .ag_skinned with IDNA version */
393 sz
= strlen(idna_out
);
394 i
= agp
->ag_sdom_start
;
395 cs
= salloc(agp
->ag_slen
- i
+ sz
+1);
396 memcpy(cs
, agp
->ag_skinned
, i
);
397 memcpy(cs
+ i
, idna_out
, sz
);
401 agp
->ag_skinned
= cs
;
403 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
,
404 NAME_NAME_SALLOC
| NAME_SKINNED
| NAME_IDNA
, 0);
412 # endif /* IDNA==IDNKIT */
413 #endif /* HAVE_IDNA */
416 _addrspec_check(int skinned
, struct addrguts
*agp
)
420 ui8_t in_domain
, hadat
;
421 union {char c
; unsigned char u
;} c
;
428 use_idna
= ok_blook(idna_disable
) ? 0 : 1;
430 agp
->ag_n_flags
|= NAME_ADDRSPEC_CHECKED
;
431 addr
= agp
->ag_skinned
;
433 if (agp
->ag_iaddr_aend
- agp
->ag_iaddr_start
== 0) {
434 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
, NAME_ADDRSPEC_ERR_EMPTY
, 0);
438 /* If the field is not a recipient, it cannot be a file or a pipe */
442 /* When changing any of the following adjust any RECIPIENTADDRSPEC;
443 * grep the latter for the complete picture */
445 agp
->ag_n_flags
|= NAME_ADDRSPEC_ISPIPE
;
448 if (addr
[0] == '/' || (addr
[0] == '.' && addr
[1] == '/'))
450 if (memchr(addr
, '@', agp
->ag_slen
) == NULL
) {
453 for (p
= addr
; (c
.c
= *p
); ++p
) {
454 if (c
.c
== '!' || c
.c
== '%')
458 agp
->ag_n_flags
|= NAME_ADDRSPEC_ISFILE
;
466 in_domain
= hadat
= 0;
468 for (p
= addr
; (c
.c
= *p
++) != '\0';) {
470 in_quote
= !in_quote
;
471 } else if (c
.u
< 040 || c
.u
>= 0177) { /* TODO no magics: !bodychar()? */
473 if (in_domain
&& use_idna
> 0) {
475 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
, NAME_ADDRSPEC_ERR_IDNA
,
481 } else if (in_domain
== 2) {
482 if ((c
.c
== ']' && *p
!= '\0') || c
.c
== '\\' || whitechar(c
.c
))
484 } else if (in_quote
&& in_domain
== 0) {
486 } else if (c
.c
== '\\' && *p
!= '\0') {
488 } else if (c
.c
== '@') {
490 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
, NAME_ADDRSPEC_ERR_ATSEQ
,
494 agp
->ag_sdom_start
= PTR2SIZE(p
- addr
);
495 agp
->ag_n_flags
|= NAME_ADDRSPEC_ISADDR
; /* TODO .. really? */
496 in_domain
= (*p
== '[') ? 2 : 1;
498 } else if (c
.c
== '(' || c
.c
== ')' || c
.c
== '<' || c
.c
== '>' ||
499 c
.c
== ',' || c
.c
== ';' || c
.c
== ':' || c
.c
== '\\' ||
500 c
.c
== '[' || c
.c
== ']')
505 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
, NAME_ADDRSPEC_ERR_CHAR
, c
.u
);
509 if (!(agp
->ag_n_flags
& NAME_ADDRSPEC_ISADDR
))
510 agp
->ag_n_flags
|= NAME_ADDRSPEC_ISNAME
;
514 agp
= _idna_apply(agp
);
518 return ((agp
->ag_n_flags
& NAME_ADDRSPEC_INVALID
) != 0);
522 gethfield(FILE *f
, char **linebuf
, size_t *linesize
, int rem
, char **colon
)
524 char *line2
= NULL
, *cp
, *cp2
;
525 size_t line2size
= 0;
529 if (*linebuf
== NULL
)
530 *linebuf
= srealloc(*linebuf
, *linesize
= 1);
537 if ((c
= readline_restart(f
, linebuf
, linesize
, 0)) <= 0) {
541 for (cp
= *linebuf
; fieldnamechar(*cp
); ++cp
)
544 while (blankchar(*cp
))
546 if (*cp
!= ':' || cp
== *linebuf
)
549 /* I guess we got a headline. Handle wraparound */
554 while (PTRCMP(--cp
, >=, *linebuf
) && blankchar(*cp
))
559 if (PTRCMP(cp
- 8, >=, *linebuf
) && cp
[-1] == '=' && cp
[-2] == '?')
561 ungetc(c
= getc(f
), f
);
564 c
= readline_restart(f
, &line2
, &line2size
, 0);
568 for (cp2
= line2
; blankchar(*cp2
); ++cp2
)
570 c
-= (int)PTR2SIZE(cp2
- line2
);
571 if (cp2
[0] == '=' && cp2
[1] == '?' && c
> 8)
573 if (PTRCMP(cp
+ c
, >=, *linebuf
+ *linesize
- 2)) {
574 size_t diff
= PTR2SIZE(cp
- *linebuf
),
575 colondiff
= PTR2SIZE(*colon
- *linebuf
);
576 *linebuf
= srealloc(*linebuf
, *linesize
+= c
+ 2);
577 cp
= &(*linebuf
)[diff
];
578 *colon
= &(*linebuf
)[colondiff
];
596 msgidnextc(char const **cp
, int *status
)
603 assert(status
!= NULL
);
621 *cp
= skip_comment(&(*cp
)[1]);
638 c
= (*status
& 02) ? lowerconv(c
) : c
;
648 charcount(char *str
, int c
)
654 for (i
= 0, cp
= str
; *cp
; ++cp
)
662 nexttoken(char const *cp
)
681 } while (nesting
> 0 && *cp
!= '\0'); /* XXX error? */
682 } else if (blankchar(*cp
) || *cp
== ',')
692 myaddrs(struct header
*hp
)
695 char const *rv
, *mta
;
698 if (hp
!= NULL
&& (np
= hp
->h_from
) != NULL
) {
699 if ((rv
= np
->n_fullname
) != NULL
)
701 if ((rv
= np
->n_name
) != NULL
)
705 if ((rv
= ok_vlook(from
)) != NULL
)
708 /* When invoking *sendmail* directly, it's its task to generate an otherwise
709 * undeterminable From: address. However, if the user sets *hostname*,
710 * accept his desire */
711 if (ok_vlook(hostname
) != NULL
)
713 if (ok_vlook(smtp
) != NULL
|| /* TODO obsolete -> mta */
714 /* TODO pretty hacky for now (this entire fun), later: url_creat()! */
715 ((mta
= ok_vlook(mta
)) != NULL
&&
716 (mta
= n_servbyname(mta
, NULL
)) != NULL
&& *mta
!= '\0'))
727 i
= strlen(myname
) + strlen(hn
) + 1 +1;
729 sstpcpy(sstpcpy(sstpcpy(cp
, myname
), "@"), hn
);
735 myorigin(struct header
*hp
)
737 char const *rv
= NULL
, *ccp
;
741 if ((ccp
= myaddrs(hp
)) != NULL
&&
742 (np
= lextract(ccp
, GEXTRA
| GFULL
)) != NULL
)
743 rv
= (np
->n_flink
!= NULL
) ? ok_vlook(sender
) : ccp
;
749 is_head(char const *linebuf
, size_t linelen
, bool_t check_rfc4155
)
751 char date
[FROM_DATEBUF
];
755 if ((rv
= (linelen
>= 5 && !memcmp(linebuf
, "From ", 5))) && check_rfc4155
&&
756 (extract_date_from_from_(linebuf
, linelen
, date
) <= 0 ||
764 extract_date_from_from_(char const *line
, size_t linelen
,
765 char datebuf
[FROM_DATEBUF
])
768 char const *cp
= line
;
774 cp
= _from__skipword(cp
);
778 cp
= _from__skipword(cp
);
781 if (cp
[0] == 't' && cp
[1] == 't' && cp
[2] == 'y') {
782 cp
= _from__skipword(cp
);
786 /* It seems there are invalid MBOX archives in the wild, compare
787 * . http://bugs.debian.org/624111
788 * . [Mutt] #3868: mutt should error if the imported mailbox is invalid
789 * What they do is that they obfuscate the address to "name at host",
790 * and even "name at host dot dom dot dom. I think we should handle that */
791 else if(cp
[0] == 'a' && cp
[1] == 't' && cp
[2] == ' '){
795 cp
= _from__skipword(cp
);
798 if(cp
[0] == 'd' && cp
[1] == 'o' && cp
[2] == 't' && cp
[3] == ' '){
804 linelen
-= PTR2SIZE(cp
- line
);
805 if (linelen
< _DATE_MINLEN
)
807 if (cp
[linelen
- 1] == '\n') {
809 /* (Rather IMAP/POP3 only) */
810 if (cp
[linelen
- 1] == '\r')
812 if (linelen
< _DATE_MINLEN
)
815 if (linelen
>= FROM_DATEBUF
)
819 memcpy(datebuf
, cp
, linelen
);
820 datebuf
[linelen
] = '\0';
824 cp
= _("<Unknown date>");
825 linelen
= strlen(cp
);
826 if (linelen
>= FROM_DATEBUF
)
827 linelen
= FROM_DATEBUF
;
833 extract_header(FILE *fp
, struct header
*hp
, si8_t
*checkaddr_err
)
835 /* See the prototype declaration for the hairy relationship of
836 * options&OPT_t_FLAG and/or pstate&PS_t_FLAG in here */
837 struct n_header_field
**hftail
;
838 struct header nh
, *hq
= &nh
;
839 char *linebuf
= NULL
/* TODO line pool */, *colon
;
840 size_t linesize
= 0, seenfields
= 0;
842 char const *val
, *cp
;
845 memset(hq
, 0, sizeof *hq
);
846 if ((pstate
& PS_t_FLAG
) && (options
& OPT_t_FLAG
)) {
849 hq
->h_bcc
= hp
->h_bcc
;
851 hftail
= &hq
->h_user_headers
;
853 for (lc
= 0; readline_restart(fp
, &linebuf
, &linesize
, 0) > 0; ++lc
)
856 /* TODO yippieia, cat(check(lextract)) :-) */
858 while ((lc
= gethfield(fp
, &linebuf
, &linesize
, lc
, &colon
)) >= 0) {
861 /* We explicitly allow EAF_NAME for some addressees since aliases are not
862 * yet expanded when we parse these! */
863 if ((val
= thisfield(linebuf
, "to")) != NULL
) {
865 hq
->h_to
= cat(hq
->h_to
, checkaddrs(lextract(val
, GTO
| GFULL
),
866 EACM_NORMAL
| EAF_NAME
, checkaddr_err
));
867 } else if ((val
= thisfield(linebuf
, "cc")) != NULL
) {
869 hq
->h_cc
= cat(hq
->h_cc
, checkaddrs(lextract(val
, GCC
| GFULL
),
870 EACM_NORMAL
| EAF_NAME
, checkaddr_err
));
871 } else if ((val
= thisfield(linebuf
, "bcc")) != NULL
) {
873 hq
->h_bcc
= cat(hq
->h_bcc
, checkaddrs(lextract(val
, GBCC
| GFULL
),
874 EACM_NORMAL
| EAF_NAME
, checkaddr_err
));
875 } else if ((val
= thisfield(linebuf
, "from")) != NULL
) {
876 if (!(pstate
& PS_t_FLAG
) || (options
& OPT_t_FLAG
)) {
878 hq
->h_from
= cat(hq
->h_from
,
879 checkaddrs(lextract(val
, GEXTRA
| GFULL
| GFULLEXTRA
),
882 } else if ((val
= thisfield(linebuf
, "reply-to")) != NULL
) {
884 hq
->h_replyto
= cat(hq
->h_replyto
,
885 checkaddrs(lextract(val
, GEXTRA
| GFULL
), EACM_STRICT
, NULL
));
886 } else if ((val
= thisfield(linebuf
, "sender")) != NULL
) {
887 if (!(pstate
& PS_t_FLAG
) || (options
& OPT_t_FLAG
)) {
889 hq
->h_sender
= cat(hq
->h_sender
, /* TODO cat? check! */
890 checkaddrs(lextract(val
, GEXTRA
| GFULL
| GFULLEXTRA
),
894 } else if ((val
= thisfield(linebuf
, "subject")) != NULL
||
895 (val
= thisfield(linebuf
, "subj")) != NULL
) {
897 for (cp
= val
; blankchar(*cp
); ++cp
)
899 hq
->h_subject
= (hq
->h_subject
!= NULL
)
900 ? save2str(hq
->h_subject
, cp
) : savestr(cp
);
902 /* The remaining are mostly hacked in and thus TODO -- at least in
903 * TODO respect to their content checking */
904 else if((val
= thisfield(linebuf
, "message-id")) != NULL
){
905 if(pstate
& PS_t_FLAG
){
906 np
= checkaddrs(lextract(val
, GREF
),
907 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG
| EACM_NONAME
,
909 if (np
== NULL
|| np
->n_flink
!= NULL
)
912 hq
->h_message_id
= np
;
915 }else if((val
= thisfield(linebuf
, "in-reply-to")) != NULL
){
916 if(pstate
& PS_t_FLAG
){
917 np
= checkaddrs(lextract(val
, GREF
),
918 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG
| EACM_NONAME
,
921 hq
->h_in_reply_to
= np
;
924 }else if((val
= thisfield(linebuf
, "references")) != NULL
){
925 if(pstate
& PS_t_FLAG
){
927 /* TODO Limit number of references TODO better on parser side */
928 hq
->h_ref
= cat(hq
->h_ref
, checkaddrs(extract(val
, GREF
),
929 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG
| EACM_NONAME
,
934 /* and that is very hairy */
935 else if((val
= thisfield(linebuf
, "mail-followup-to")) != NULL
){
936 if(pstate
& PS_t_FLAG
){
938 hq
->h_mft
= cat(hq
->h_mft
, checkaddrs(lextract(val
, GEXTRA
| GFULL
),
939 /*EACM_STRICT | TODO '/' valid!! | EACM_NOLOG | */EACM_NONAME
,
944 /* A free-form user header; gethfield() did some verification already.. */
946 struct n_header_field
*hfp
;
950 for(nstart
= cp
= linebuf
;; ++cp
)
951 if(!fieldnamechar(*cp
))
953 nl
= (ui32_t
)PTR2SIZE(cp
- nstart
);
955 while(blankchar(*cp
))
959 n_err(_("Ignoring header field: %s\n"), linebuf
);
962 while(blankchar(*cp
))
964 bl
= (ui32_t
)strlen(cp
) +1;
967 *hftail
= hfp
= salloc(n_VSTRUCT_SIZEOF(struct n_header_field
, hf_dat
969 hftail
= &hfp
->hf_next
;
973 memcpy(hfp
->hf_dat
, nstart
, nl
);
974 hfp
->hf_dat
[nl
++] = '\0';
975 memcpy(hfp
->hf_dat
+ nl
, cp
, bl
);
979 /* In case the blank line after the header has been edited out. Otherwise,
980 * fetch the header separator */
981 if (linebuf
!= NULL
) {
982 if (linebuf
[0] != '\0') {
983 for (cp
= linebuf
; *(++cp
) != '\0';)
985 fseek(fp
, (long)-PTR2SIZE(1 + cp
- linebuf
), SEEK_CUR
);
987 if ((c
= getc(fp
)) != '\n' && c
!= EOF
)
992 if (seenfields
> 0) {
995 hp
->h_bcc
= hq
->h_bcc
;
996 hp
->h_from
= hq
->h_from
;
997 hp
->h_replyto
= hq
->h_replyto
;
998 hp
->h_sender
= hq
->h_sender
;
999 if (hq
->h_subject
!= NULL
|| !(pstate
& PS_t_FLAG
) ||
1000 !(options
& OPT_t_FLAG
))
1001 hp
->h_subject
= hq
->h_subject
;
1002 hp
->h_user_headers
= hq
->h_user_headers
;
1004 if (pstate
& PS_t_FLAG
) {
1005 hp
->h_ref
= hq
->h_ref
;
1006 hp
->h_message_id
= hq
->h_message_id
;
1007 hp
->h_in_reply_to
= hq
->h_in_reply_to
;
1008 hp
->h_mft
= hq
->h_mft
;
1010 /* And perform additional validity checks so that we don't bail later
1011 * on TODO this is good and the place where this should occur,
1012 * TODO unfortunately a lot of other places do again and blabla */
1013 if (pstate
& PS_t_FLAG
) {
1014 if (hp
->h_from
== NULL
)
1015 hp
->h_from
= option_r_arg
;
1016 else if (hp
->h_from
->n_flink
!= NULL
&& hp
->h_sender
== NULL
)
1017 hp
->h_sender
= lextract(ok_vlook(sender
),
1018 GEXTRA
| GFULL
| GFULLEXTRA
);
1022 n_err(_("Restoring deleted header lines\n"));
1024 if (linebuf
!= NULL
)
1030 hfield_mult(char const *field
, struct message
*mp
, int mult
)
1035 size_t linesize
= 0; /* TODO line pool */
1036 char *linebuf
= NULL
, *colon
;
1040 /* There are (spam) messages which have header bytes which are many KB when
1041 * joined, so resize a single heap storage until we are done if we shall
1042 * collect a field that may have multiple bodies; only otherwise use the
1043 * string dope directly */
1044 memset(&hfs
, 0, sizeof hfs
);
1046 if ((ibuf
= setinput(&mb
, mp
, NEED_HEADER
)) == NULL
)
1048 if ((lc
= mp
->m_lines
- 1) < 0)
1051 if ((mp
->m_flag
& MNOFROM
) == 0 &&
1052 readline_restart(ibuf
, &linebuf
, &linesize
, 0) < 0)
1055 if ((lc
= gethfield(ibuf
, &linebuf
, &linesize
, lc
, &colon
)) < 0)
1057 if ((hfield
= thisfield(linebuf
, field
)) != NULL
&& *hfield
!= '\0') {
1059 n_str_add_buf(&hfs
, hfield
, strlen(hfield
));
1061 hfs
.s
= savestr(hfield
);
1068 if (linebuf
!= NULL
)
1070 if (mult
&& hfs
.s
!= NULL
) {
1071 colon
= savestrbuf(hfs
.s
, hfs
.l
);
1080 thisfield(char const *linebuf
, char const *field
)
1082 char const *rv
= NULL
;
1085 while (lowerconv(*linebuf
) == lowerconv(*field
)) {
1092 while (blankchar(*linebuf
))
1094 if (*linebuf
++ != ':')
1097 while (blankchar(*linebuf
)) /* TODO header parser.. strip trailing WS?!? */
1106 nameof(struct message
*mp
, int reptype
)
1111 cp
= skin(name1(mp
, reptype
));
1112 if (reptype
!= 0 || charcount(cp
, '!') < 2)
1114 cp2
= strrchr(cp
, '!');
1116 while (cp2
> cp
&& *cp2
!= '!')
1126 skip_comment(char const *cp
)
1131 for (nesting
= 1; nesting
> 0 && *cp
; ++cp
) {
1150 routeaddr(char const *name
)
1152 char const *np
, *rp
= NULL
;
1155 for (np
= name
; *np
; np
++) {
1158 np
= skip_comment(np
+ 1) - 1;
1164 if (*np
== '\\' && np
[1])
1181 FL
enum expand_addr_flags
1182 expandaddr_to_eaf(void)
1185 char const *eafd_name
;
1186 bool_t eafd_is_target
;
1190 {"restrict", FAL0
, EAF_TARGET_MASK
, EAF_RESTRICT
| EAF_RESTRICT_TARGETS
},
1191 {"fail", FAL0
, EAF_NONE
, EAF_FAIL
},
1192 {"all", TRU1
, EAF_NONE
, EAF_TARGET_MASK
},
1193 {"file", TRU1
, EAF_NONE
, EAF_FILE
},
1194 {"pipe", TRU1
, EAF_NONE
, EAF_PIPE
},
1195 {"name", TRU1
, EAF_NONE
, EAF_NAME
},
1196 {"addr", TRU1
, EAF_NONE
, EAF_ADDR
}
1200 enum expand_addr_flags rv
;
1204 if ((cp
= ok_vlook(expandaddr
)) == NULL
)
1205 rv
= EAF_RESTRICT_TARGETS
;
1206 else if (*cp
== '\0')
1207 rv
= EAF_TARGET_MASK
;
1209 rv
= EAF_TARGET_MASK
;
1211 for (buf
= savestr(cp
); (cp
= n_strsep(&buf
, ',', TRU1
)) != NULL
;) {
1214 if ((minus
= (*cp
== '-')) || *cp
== '+')
1216 for (eafp
= eafa
;; ++eafp
) {
1217 if (eafp
== eafa
+ n_NELEM(eafa
)) {
1218 if (options
& OPT_D_V
)
1219 n_err(_("Unknown *expandaddr* value: %s\n"), cp
);
1221 } else if (!asccasecmp(cp
, eafp
->eafd_name
)) {
1223 rv
&= ~eafp
->eafd_andoff
;
1224 rv
|= eafp
->eafd_or
;
1226 if (eafp
->eafd_is_target
)
1227 rv
&= ~eafp
->eafd_or
;
1228 else if (options
& OPT_D_V
)
1229 n_err(_("minus - prefix invalid for *expandaddr* value: "
1233 } else if (!asccasecmp(cp
, "noalias")) { /* TODO v15 OBSOLETE */
1234 OBSOLETE(_("*expandaddr*: noalias is henceforth -name"));
1241 if ((rv
& EAF_RESTRICT
) && (options
& (OPT_INTERACTIVE
| OPT_TILDE_FLAG
)))
1242 rv
|= EAF_TARGET_MASK
;
1243 else if (options
& OPT_D_V
) {
1244 if (!(rv
& EAF_TARGET_MASK
))
1245 n_err(_("*expandaddr* doesn't allow any addressees\n"));
1246 else if ((rv
& EAF_FAIL
) && (rv
& EAF_TARGET_MASK
) == EAF_TARGET_MASK
)
1247 n_err(_("*expandaddr* with fail, but no restrictions to apply\n"));
1255 is_addr_invalid(struct name
*np
, enum expand_addr_check_mode eacm
)
1257 char cbuf
[sizeof "'\\U12340'"];
1258 enum expand_addr_flags eaf
;
1266 if ((rv
= ((f
& NAME_ADDRSPEC_INVALID
) != 0))) {
1267 if ((eacm
& EACM_NOLOG
) || (f
& NAME_ADDRSPEC_ERR_EMPTY
)) {
1271 char const *fmt
= "'\\x%02X'";
1272 bool_t ok8bit
= TRU1
;
1274 if (f
& NAME_ADDRSPEC_ERR_IDNA
) {
1275 cs
= _("Invalid domain name: %s, character %s\n");
1278 } else if (f
& NAME_ADDRSPEC_ERR_ATSEQ
)
1279 cs
= _("%s contains invalid %s sequence\n");
1281 cs
= _("%s contains invalid character %s\n");
1283 c
= NAME_ADDRSPEC_ERR_GETWC(f
);
1284 snprintf(cbuf
, sizeof cbuf
,
1285 (ok8bit
&& c
>= 040 && c
<= 0177 ? "'%c'" : fmt
), c
);
1291 /* *expandaddr* stuff */
1292 if (!(rv
= ((eacm
& EACM_MODE_MASK
) != EACM_NONE
)))
1295 eaf
= expandaddr_to_eaf();
1297 if ((eacm
& EACM_STRICT
) && (f
& NAME_ADDRSPEC_ISFILEORPIPE
)) {
1300 cs
= _("%s%s: file or pipe addressees not allowed here\n");
1301 if (eacm
& EACM_NOLOG
)
1307 eaf
|= (eacm
& EAF_TARGET_MASK
);
1308 if (eacm
& EACM_NONAME
)
1311 if (eaf
== EAF_NONE
) {
1318 if (!(eaf
& EAF_FILE
) && (f
& NAME_ADDRSPEC_ISFILE
)) {
1319 cs
= _("%s%s: *expandaddr* doesn't allow file target\n");
1320 if (eacm
& EACM_NOLOG
)
1322 } else if (!(eaf
& EAF_PIPE
) && (f
& NAME_ADDRSPEC_ISPIPE
)) {
1323 cs
= _("%s%s: *expandaddr* doesn't allow command pipe target\n");
1324 if (eacm
& EACM_NOLOG
)
1326 } else if (!(eaf
& EAF_NAME
) && (f
& NAME_ADDRSPEC_ISNAME
)) {
1327 cs
= _("%s%s: *expandaddr* doesn't allow user name target\n");
1328 if (eacm
& EACM_NOLOG
)
1330 } else if (!(eaf
& EAF_ADDR
) && (f
& NAME_ADDRSPEC_ISADDR
)) {
1331 cs
= _("%s%s: *expandaddr* doesn't allow mail address target\n");
1332 if (eacm
& EACM_NOLOG
)
1342 n_err(cs
, n_shexp_quote_cp(np
->n_name
, TRU1
), cbuf
);
1349 skin(char const *name
)
1356 addrspec_with_guts(1, name
, &ag
);
1357 ret
= ag
.ag_skinned
;
1358 if (!(ag
.ag_n_flags
& NAME_NAME_SALLOC
))
1359 ret
= savestrbuf(ret
, ag
.ag_slen
);
1365 /* TODO addrspec_with_guts: RFC 5322
1366 * TODO addrspec_with_guts: trim whitespace ETC. ETC. ETC.!!! */
1368 addrspec_with_guts(int doskin
, char const *name
, struct addrguts
*agp
)
1371 char *cp2
, *bufend
, *nbuf
, c
, gotlt
, gotaddr
, lastsp
;
1375 memset(agp
, 0, sizeof *agp
);
1377 if ((agp
->ag_input
= name
) == NULL
|| (agp
->ag_ilen
= strlen(name
)) == 0) {
1378 agp
->ag_skinned
= n_UNCONST(""); /* ok: NAME_SALLOC is not set */
1380 agp
->ag_n_flags
|= NAME_ADDRSPEC_CHECKED
;
1381 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
, NAME_ADDRSPEC_ERR_EMPTY
, 0);
1385 if (!doskin
|| !anyof(name
, "(< ")) {
1386 /*agp->ag_iaddr_start = 0;*/
1387 agp
->ag_iaddr_aend
= agp
->ag_ilen
;
1388 agp
->ag_skinned
= n_UNCONST(name
); /* (NAME_SALLOC not set) */
1389 agp
->ag_slen
= agp
->ag_ilen
;
1390 agp
->ag_n_flags
= NAME_SKINNED
;
1394 /* Something makes us think we have to perform the skin operation */
1395 nbuf
= ac_alloc(agp
->ag_ilen
+ 1);
1396 /*agp->ag_iaddr_start = 0;*/
1397 cp2
= bufend
= nbuf
;
1398 gotlt
= gotaddr
= lastsp
= 0;
1400 for (cp
= name
++; (c
= *cp
++) != '\0'; ) {
1403 cp
= skip_comment(cp
);
1407 /* Start of a "quoted-string".
1408 * Copy it in its entirety */
1409 /* XXX RFC: quotes are "semantically invisible"
1410 * XXX But it was explicitly added (Changelog.Heirloom,
1411 * XXX [9.23] released 11/15/00, "Do not remove quotes
1412 * XXX when skinning names"? No more info.. */
1414 while ((c
= *cp
) != '\0') { /* TODO improve */
1422 else if ((c
= *cp
) != '\0') {
1433 agp
->ag_iaddr_aend
= PTR2SIZE(cp
- name
);
1435 if (cp
[0] == 'a' && cp
[1] == 't' && blankchar(cp
[2]))
1436 cp
+= 3, *cp2
++ = '@';
1437 else if (cp
[0] == '@' && blankchar(cp
[1]))
1438 cp
+= 2, *cp2
++ = '@';
1443 agp
->ag_iaddr_start
= PTR2SIZE(cp
- (name
- 1));
1445 gotlt
= gotaddr
= 1;
1450 /* (_addrspec_check() verifies these later!) */
1451 agp
->ag_iaddr_aend
= PTR2SIZE(cp
- name
);
1453 while ((c
= *cp
) != '\0' && c
!= ',') {
1456 cp
= skip_comment(cp
);
1458 while ((c
= *cp
) != '\0') {
1462 if (c
== '\\' && *cp
!= '\0')
1480 for (; blankchar(*cp
); ++cp
)
1485 } else if (!gotaddr
) {
1487 agp
->ag_iaddr_start
= PTR2SIZE(cp
- name
);
1491 agp
->ag_slen
= PTR2SIZE(cp2
- nbuf
);
1492 if (agp
->ag_iaddr_aend
== 0)
1493 agp
->ag_iaddr_aend
= agp
->ag_ilen
;
1495 agp
->ag_skinned
= savestrbuf(nbuf
, agp
->ag_slen
);
1497 agp
->ag_n_flags
= NAME_NAME_SALLOC
| NAME_SKINNED
;
1499 rv
= _addrspec_check(doskin
, agp
);
1506 realname(char const *name
)
1508 char const *cp
, *cq
, *cstart
= NULL
, *cend
= NULL
;
1511 int quoted
, good
, nogood
;
1514 if ((cp
= n_UNCONST(name
)) == NULL
)
1516 for (; *cp
!= '\0'; ++cp
) {
1519 if (cstart
!= NULL
) {
1520 /* More than one comment in address, doesn't make sense to display
1521 * it without context. Return the entire field */
1522 cp
= mime_fromaddr(name
);
1526 cp
= skip_comment(cp
);
1529 cend
= cstart
= NULL
;
1535 if (*cp
== '\\' && cp
[1])
1546 /* More than one address. Just use the first one */
1552 if (cstart
== NULL
) {
1554 /* If name contains only a route-addr, the surrounding angle brackets
1555 * don't serve any useful purpose when displaying, so remove */
1556 cp
= prstr(skin(name
));
1558 cp
= mime_fromaddr(name
);
1562 /* Strip quotes. Note that quotes that appear within a MIME encoded word are
1563 * not stripped. The idea is to strip only syntactical relevant things (but
1564 * this is not necessarily the most sensible way in practice) */
1565 rp
= rname
= ac_alloc(PTR2SIZE(cend
- cstart
+1));
1567 for (cp
= cstart
; cp
< cend
; ++cp
) {
1568 if (*cp
== '(' && !quoted
) {
1569 cq
= skip_comment(++cp
);
1570 if (PTRCMP(--cq
, >, cend
))
1573 if (*cp
== '\\' && PTRCMP(cp
+ 1, <, cq
))
1577 } else if (*cp
== '\\' && PTRCMP(cp
+ 1, <, cend
))
1579 else if (*cp
== '"') {
1588 mime_fromhdr(&in
, &out
, TD_ISPR
| TD_ICONV
);
1590 rname
= savestr(out
.s
);
1593 while (blankchar(*rname
))
1595 for (rp
= rname
; *rp
!= '\0'; ++rp
)
1597 while (PTRCMP(--rp
, >=, rname
) && blankchar(*rp
))
1600 cp
= mime_fromaddr(name
);
1604 /* mime_fromhdr() has converted all nonprintable characters to question
1605 * marks now. These and blanks are considered uninteresting; if the
1606 * displayed part of the real name contains more than 25% of them, it is
1607 * probably better to display the plain email address instead */
1610 for (rp
= rname
; *rp
!= '\0' && PTRCMP(rp
, <, rname
+ 20); ++rp
)
1611 if (*rp
== '?' || blankchar(*rp
))
1615 cp
= (good
* 3 < nogood
) ? prstr(skin(name
)) : rname
;
1618 return n_UNCONST(cp
);
1622 name1(struct message
*mp
, int reptype
)
1624 char *namebuf
, *cp
, *cp2
, *linebuf
= NULL
/* TODO line pool */;
1625 size_t namesize
, linesize
= 0;
1630 if ((cp
= hfield1("from", mp
)) != NULL
&& *cp
!= '\0')
1632 if (reptype
== 0 && (cp
= hfield1("sender", mp
)) != NULL
&& *cp
!= '\0')
1635 namebuf
= smalloc(namesize
= 1);
1637 if (mp
->m_flag
& MNOFROM
)
1639 if ((ibuf
= setinput(&mb
, mp
, NEED_HEADER
)) == NULL
)
1641 if (readline_restart(ibuf
, &linebuf
, &linesize
, 0) < 0)
1645 if (namesize
<= linesize
)
1646 namebuf
= srealloc(namebuf
, namesize
= linesize
+1);
1647 for (cp
= linebuf
; *cp
!= '\0' && *cp
!= ' '; ++cp
)
1649 for (; blankchar(*cp
); ++cp
)
1651 for (cp2
= namebuf
+ strlen(namebuf
);
1652 *cp
&& !blankchar(*cp
) && PTRCMP(cp2
, <, namebuf
+ namesize
-1);)
1656 if (readline_restart(ibuf
, &linebuf
, &linesize
, 0) < 0)
1658 if ((cp
= strchr(linebuf
, 'F')) == NULL
)
1660 if (strncmp(cp
, "From", 4))
1662 if (namesize
<= linesize
)
1663 namebuf
= srealloc(namebuf
, namesize
= linesize
+ 1);
1665 while ((cp
= strchr(cp
, 'r')) != NULL
) {
1666 if (!strncmp(cp
, "remote", 6)) {
1667 if ((cp
= strchr(cp
, 'f')) == NULL
)
1669 if (strncmp(cp
, "from", 4) != 0)
1671 if ((cp
= strchr(cp
, ' ')) == NULL
)
1675 strncpy(namebuf
, cp
, namesize
);
1678 cp2
= strrchr(namebuf
, '!') + 1;
1679 strncpy(cp2
, cp
, PTR2SIZE(namebuf
+ namesize
- cp2
));
1681 namebuf
[namesize
- 2] = '!';
1682 namebuf
[namesize
- 1] = '\0';
1688 if (*namebuf
!= '\0' || ((cp
= hfield1("return-path", mp
))) == NULL
||
1690 cp
= savestr(namebuf
);
1692 if (linebuf
!= NULL
)
1701 subject_re_trim(char *s
)
1706 } const *pp
, ignored
[] = { /* Update *reply-strings* manual upon change! */
1708 { 3, "aw:" }, { 5, "antw:" }, /* de */
1713 char *orig_s
= s
, *re_st
= NULL
, *re_st_x
;
1714 size_t re_l
= 0 /* pacify CC */;
1717 if ((re_st_x
= ok_vlook(reply_strings
)) != NULL
&&
1718 (re_l
= strlen(re_st_x
)) > 0) {
1719 re_st
= ac_alloc(++re_l
* 2);
1720 memcpy(re_st
, re_st_x
, re_l
);
1724 while (*s
!= '\0') {
1725 while (spacechar(*s
))
1728 for (pp
= ignored
; pp
->len
> 0; ++pp
)
1729 if (is_asccaseprefix(s
, pp
->dat
)) {
1735 if (re_st
!= NULL
) {
1738 memcpy(re_st_x
= re_st
+ re_l
, re_st
, re_l
);
1739 while ((cp
= n_strsep(&re_st_x
, ',', TRU1
)) != NULL
)
1740 if (is_asccaseprefix(s
, cp
)) {
1752 return any
? s
: orig_s
;
1756 msgidcmp(char const *s1
, char const *s2
)
1758 int q1
= 0, q2
= 0, c1
, c2
;
1767 c1
= msgidnextc(&s1
, &q1
);
1768 c2
= msgidnextc(&s2
, &q2
);
1777 fakefrom(struct message
*mp
)
1782 if (((name
= skin(hfield1("return-path", mp
))) == NULL
|| *name
== '\0' ) &&
1783 ((name
= skin(hfield1("from", mp
))) == NULL
|| *name
== '\0'))
1784 /* XXX MAILER-DAEMON is what an old MBOX manual page says.
1785 * RFC 4155 however requires a RFC 5322 (2822) conforming
1786 * "addr-spec", but we simply can't provide that */
1787 name
= "MAILER-DAEMON";
1799 for (cq
= cp
; *cq
!= '\0' && *cq
!= '\n'; ++cq
)
1807 #ifdef HAVE_IMAP_SEARCH
1809 unixtime(char const *fromline
)
1814 int i
, year
, month
, day
, hour
, minute
, second
, tzdiff
;
1818 for (fp
= fromline
; *fp
!= '\0' && *fp
!= '\n'; ++fp
)
1821 if (PTR2SIZE(fp
- fromline
) < 7)
1826 if (!strncmp(fp
+ 4, month_names
[i
], 3))
1828 if (month_names
[++i
][0] == '\0')
1834 day
= strtol(fp
+ 8, &xp
, 10);
1835 if (*xp
!= ' ' || xp
!= fp
+ 10)
1837 hour
= strtol(fp
+ 11, &xp
, 10);
1838 if (*xp
!= ':' || xp
!= fp
+ 13)
1840 minute
= strtol(fp
+ 14, &xp
, 10);
1841 if (*xp
!= ':' || xp
!= fp
+ 16)
1843 second
= strtol(fp
+ 17, &xp
, 10);
1844 if (*xp
!= ' ' || xp
!= fp
+ 19)
1846 year
= strtol(fp
+ 20, &xp
, 10);
1849 if ((t
= combinetime(year
, month
, day
, hour
, minute
, second
)) == (time_t)-1)
1851 tzdiff
= t
- mktime(gmtime(&t
));
1852 tmptr
= localtime(&t
);
1853 if (tmptr
->tm_isdst
> 0)
1863 #endif /* HAVE_IMAP_SEARCH */
1866 rfctime(char const *date
)
1868 char const *cp
= date
;
1871 int i
, year
, month
, day
, hour
, minute
, second
;
1874 if ((cp
= nexttoken(cp
)) == NULL
)
1876 if (alphachar(cp
[0]) && alphachar(cp
[1]) && alphachar(cp
[2]) &&
1878 if ((cp
= nexttoken(&cp
[4])) == NULL
)
1881 day
= strtol(cp
, &x
, 10); /* XXX strtol */
1882 if ((cp
= nexttoken(x
)) == NULL
)
1885 if (!strncmp(cp
, month_names
[i
], 3))
1887 if (month_names
[++i
][0] == '\0')
1891 if ((cp
= nexttoken(&cp
[3])) == NULL
)
1894 * Where a two or three digit year occurs in a date, the year is to be
1895 * interpreted as follows: If a two digit year is encountered whose
1896 * value is between 00 and 49, the year is interpreted by adding 2000,
1897 * ending up with a value between 2000 and 2049. If a two digit year
1898 * is encountered with a value between 50 and 99, or any three digit
1899 * year is encountered, the year is interpreted by adding 1900 */
1900 year
= strtol(cp
, &x
, 10); /* XXX strtol */
1901 i
= (int)PTR2SIZE(x
- cp
);
1902 if (i
== 2 && year
>= 0 && year
<= 49)
1904 else if (i
== 3 || (i
== 2 && year
>= 50 && year
<= 99))
1906 if ((cp
= nexttoken(x
)) == NULL
)
1908 hour
= strtol(cp
, &x
, 10); /* XXX strtol */
1912 minute
= strtol(cp
, &x
, 10);
1915 second
= strtol(cp
, &x
, 10);
1919 if ((t
= combinetime(year
, month
, day
, hour
, minute
, second
)) == (time_t)-1)
1921 if ((cp
= nexttoken(x
)) != NULL
) {
1933 if (digitchar(cp
[0]) && digitchar(cp
[1]) && digitchar(cp
[2]) &&
1939 tadj
= strtol(buf
, NULL
, 10) * 3600;/*XXX strtrol*/
1942 tadj
+= strtol(buf
, NULL
, 10) * 60; /* XXX strtol*/
1947 /* TODO WE DO NOT YET PARSE (OBSOLETE) ZONE NAMES
1948 * TODO once again, Christos Zoulas and NetBSD Mail have done
1949 * TODO a really good job already, but using strptime(3), which
1950 * TODO is not portable. Nonetheless, WE must improve, not
1951 * TODO at last because we simply ignore obsolete timezones!!
1952 * TODO See RFC 5322, 4.3! */
1963 combinetime(int year
, int month
, int day
, int hour
, int minute
, int second
){
1964 size_t const jdn_epoch
= 2440588;
1965 bool_t
const y2038p
= (sizeof(time_t) == 4);
1971 if(UICMP(32, second
, >=, DATE_SECSMIN
) || /* XXX (leap- */
1972 UICMP(32, minute
, >=, DATE_MINSHOUR
) ||
1973 UICMP(32, hour
, >=, DATE_HOURSDAY
) ||
1974 day
< 1 || day
> 31 ||
1975 month
< 1 || month
> 12 ||
1979 if(year
>= 1970 + ((y2038p
? SI32_MAX
: SI64_MAX
) /
1980 (DATE_SECSDAY
* DATE_DAYSYEAR
))){
1981 /* Be a coward regarding Y2038, many people (mostly myself, that is) do
1982 * test by stepping second-wise around the flip. Don't care otherwise */
1985 if(year
> 2038 || month
> 1 || day
> 19 ||
1986 hour
> 3 || minute
> 14 || second
> 7)
1991 t
+= minute
* DATE_SECSMIN
;
1992 t
+= hour
* DATE_SECSHOUR
;
1994 jdn
= a_head_gregorian_to_jdn(year
, month
, day
);
1996 t
+= (time_t)jdn
* DATE_SECSDAY
;
2006 substdate(struct message
*m
)
2011 /* Determine the date to print in faked 'From ' lines. This is traditionally
2012 * the date the message was written to the mail file. Try to determine this
2013 * using RFC message header fields, or fall back to current time */
2014 if ((cp
= hfield1("received", m
)) != NULL
) {
2015 while ((cp
= nexttoken(cp
)) != NULL
&& *cp
!= ';') {
2018 while (alnumchar(*cp
));
2021 m
->m_time
= rfctime(cp
);
2023 if (m
->m_time
== 0 || m
->m_time
> time_current
.tc_time
) {
2024 if ((cp
= hfield1("date", m
)) != NULL
)
2025 m
->m_time
= rfctime(cp
);
2027 if (m
->m_time
== 0 || m
->m_time
> time_current
.tc_time
)
2028 m
->m_time
= time_current
.tc_time
;
2033 setup_from_and_sender(struct header
*hp
)
2039 /* If -t parsed or composed From: then take it. With -t we otherwise
2040 * want -r to be honoured in favour of *from* in order to have
2041 * a behaviour that is compatible with what users would expect from e.g.
2043 if ((np
= hp
->h_from
) != NULL
||
2044 ((pstate
& PS_t_FLAG
) && (np
= option_r_arg
) != NULL
)) {
2046 } else if ((addr
= myaddrs(hp
)) != NULL
)
2047 np
= lextract(addr
, GEXTRA
| GFULL
| GFULLEXTRA
);
2050 if ((np
= hp
->h_sender
) != NULL
) {
2052 } else if ((addr
= ok_vlook(sender
)) != NULL
)
2053 np
= lextract(addr
, GEXTRA
| GFULL
| GFULLEXTRA
);
2059 FL
struct name
const *
2060 check_from_and_sender(struct name
const *fromfield
,
2061 struct name
const *senderfield
)
2063 struct name
const *rv
= NULL
;
2066 if (senderfield
!= NULL
) {
2067 if (senderfield
->n_flink
!= NULL
) {
2068 n_err(_("The Sender: field may contain only one address\n"));
2074 if (fromfield
!= NULL
) {
2075 if (fromfield
->n_flink
!= NULL
&& senderfield
== NULL
) {
2076 n_err(_("A Sender: is required when there are multiple "
2077 "addresses in From:\n"));
2085 rv
= (struct name
*)0x1;
2093 getsender(struct message
*mp
)
2099 if ((cp
= hfield1("from", mp
)) == NULL
||
2100 (np
= lextract(cp
, GEXTRA
| GSKIN
)) == NULL
)
2103 cp
= (np
->n_flink
!= NULL
) ? skin(hfield1("sender", mp
)) : np
->n_name
;
2110 grab_headers(enum n_lexinput_flags lif
, struct header
*hp
, enum gfield gflags
,
2113 /* TODO grab_headers: again, check counts etc. against RFC;
2114 * TODO (now assumes check_from_and_sender() is called afterwards ++ */
2120 comma
= (ok_blook(bsdcompat
) || ok_blook(bsdmsgs
)) ? 0 : GCOMMA
;
2123 hp
->h_to
= grab_names(lif
, "To: ", hp
->h_to
, comma
, GTO
| GFULL
);
2124 if (subjfirst
&& (gflags
& GSUBJECT
))
2125 hp
->h_subject
= n_lex_input_cp(lif
, "Subject: ", hp
->h_subject
);
2127 hp
->h_cc
= grab_names(lif
, "Cc: ", hp
->h_cc
, comma
, GCC
| GFULL
);
2129 hp
->h_bcc
= grab_names(lif
, "Bcc: ", hp
->h_bcc
, comma
, GBCC
| GFULL
);
2131 if (gflags
& GEXTRA
) {
2132 if (hp
->h_from
== NULL
)
2133 hp
->h_from
= lextract(myaddrs(hp
), GEXTRA
| GFULL
| GFULLEXTRA
);
2134 hp
->h_from
= grab_names(lif
, "From: ", hp
->h_from
, comma
,
2135 GEXTRA
| GFULL
| GFULLEXTRA
);
2136 if (hp
->h_replyto
== NULL
)
2137 hp
->h_replyto
= lextract(ok_vlook(replyto
), GEXTRA
| GFULL
);
2138 hp
->h_replyto
= grab_names(lif
, "Reply-To: ", hp
->h_replyto
, comma
,
2140 if (hp
->h_sender
== NULL
)
2141 hp
->h_sender
= extract(ok_vlook(sender
), GEXTRA
| GFULL
);
2142 hp
->h_sender
= grab_names(lif
, "Sender: ", hp
->h_sender
, comma
,
2146 if (!subjfirst
&& (gflags
& GSUBJECT
))
2147 hp
->h_subject
= n_lex_input_cp(lif
, "Subject: ", hp
->h_subject
);
2154 header_match(struct message
*mp
, struct search_expr
const *sep
)
2159 size_t linesize
= 0; /* TODO line pool */
2160 char *linebuf
= NULL
, *colon
;
2164 if ((ibuf
= setinput(&mb
, mp
, NEED_HEADER
)) == NULL
)
2166 if ((lc
= mp
->m_lines
- 1) < 0)
2169 if ((mp
->m_flag
& MNOFROM
) == 0 &&
2170 readline_restart(ibuf
, &linebuf
, &linesize
, 0) < 0)
2173 if (gethfield(ibuf
, &linebuf
, &linesize
, lc
, &colon
) <= 0)
2175 if (blankchar(*++colon
))
2177 in
.l
= strlen(in
.s
= colon
);
2178 mime_fromhdr(&in
, &out
, TD_ICONV
);
2180 if (sep
->ss_sexpr
== NULL
)
2181 rv
= (regexec(&sep
->ss_regex
, out
.s
, 0,NULL
, 0) != REG_NOMATCH
);
2184 rv
= substr(out
.s
, sep
->ss_sexpr
);
2191 if (linebuf
!= NULL
)