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 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
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 wether 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 /* Convert the domain part of a skinned address to IDNA.
95 * If an error occurs before Unicode information is available, revert the IDNA
96 * error to a normal CHAR one so that the error message doesn't talk Unicode */
98 static struct addrguts
* _idna_apply(struct addrguts
*agp
);
101 /* Classify and check a (possibly skinned) header body according to RFC
102 * *addr-spec* rules; if it (is assumed to has been) skinned it may however be
103 * also a file or a pipe command, so check that first, then.
104 * Otherwise perform content checking and isolate the domain part (for IDNA) */
105 static int _addrspec_check(int doskin
, struct addrguts
*agp
);
107 /* Return the next header field found in the given message.
108 * Return >= 0 if something found, < 0 elsewise.
109 * "colon" is set to point to the colon in the header.
110 * Must deal with \ continuations & other such fraud */
111 static int gethfield(FILE *f
, char **linebuf
, size_t *linesize
,
112 int rem
, char **colon
);
114 static int msgidnextc(char const **cp
, int *status
);
116 /* Count the occurances of c in str */
117 static int charcount(char *str
, int c
);
119 static char const * nexttoken(char const *cp
);
122 _from__skipword(char const *wp
)
128 while ((c
= *wp
++) != '\0' && !blankchar(c
)) {
130 while ((c
= *wp
++) != '\0' && c
!= '"')
136 for (; blankchar(c
); c
= *wp
++)
140 return (c
== 0 ? NULL
: wp
- 1);
144 _cmatch(size_t len
, char const *date
, char const *tp
)
169 if (c
!= ' ' && !digitchar(c
))
177 if (c
!= '+' && c
!= '-')
189 _is_date(char const *date
)
191 struct cmatch_data
const *cmdp
;
196 if ((dl
= strlen(date
)) >= _DATE_MINLEN
)
197 for (cmdp
= _cmatch_data
; cmdp
->tdata
!= NULL
; ++cmdp
)
198 if (dl
== cmdp
->tlen
&& (rv
= _cmatch(dl
, date
, cmdp
->tdata
)))
205 # if HAVE_IDNA == HAVE_IDNA_LIBIDNA
206 static struct addrguts
*
207 _idna_apply(struct addrguts
*agp
)
209 char *idna_utf8
, *idna_ascii
, *cs
;
213 sz
= agp
->ag_slen
- agp
->ag_sdom_start
;
215 idna_utf8
= ac_alloc(sz
+1);
216 memcpy(idna_utf8
, agp
->ag_skinned
+ agp
->ag_sdom_start
, sz
);
217 idna_utf8
[sz
] = '\0';
219 /* GNU Libidn settles on top of iconv(3) without any fallback, so let's just
220 * let it perform the charset conversion, if any should be necessary */
221 if (!(options
& OPT_UNICODE
)) {
222 char const *tcs
= charset_get_lc();
223 idna_ascii
= idna_utf8
;
224 idna_utf8
= stringprep_convert(idna_ascii
, "UTF-8", tcs
);
225 i
= (idna_utf8
== NULL
&& errno
== EINVAL
);
228 if (idna_utf8
== NULL
) {
230 n_err(_("Cannot convert from %s to %s\n"), tcs
, "UTF-8");
231 agp
->ag_n_flags
^= NAME_ADDRSPEC_ERR_IDNA
| NAME_ADDRSPEC_ERR_CHAR
;
236 if (idna_to_ascii_8z(idna_utf8
, &idna_ascii
, 0) != IDNA_SUCCESS
) {
237 agp
->ag_n_flags
^= NAME_ADDRSPEC_ERR_IDNA
| NAME_ADDRSPEC_ERR_CHAR
;
241 /* Replace the domain part of .ag_skinned with IDNA version */
242 sz
= strlen(idna_ascii
);
243 i
= agp
->ag_sdom_start
;
244 cs
= salloc(agp
->ag_slen
- i
+ sz
+1);
245 memcpy(cs
, agp
->ag_skinned
, i
);
246 memcpy(cs
+ i
, idna_ascii
, sz
);
250 agp
->ag_skinned
= cs
;
252 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
,
253 NAME_NAME_SALLOC
| NAME_SKINNED
| NAME_IDNA
, 0);
255 idn_free(idna_ascii
);
257 if (options
& OPT_UNICODE
)
266 # elif HAVE_IDNA == HAVE_IDNA_IDNKIT /* IDNA==LIBIDNA */
267 static struct addrguts
*
268 _idna_apply(struct addrguts
*agp
)
270 char *idna_in
, *idna_out
, *cs
;
275 sz
= agp
->ag_slen
- agp
->ag_sdom_start
;
277 idna_in
= ac_alloc(sz
+1);
278 memcpy(idna_in
, agp
->ag_skinned
+ agp
->ag_sdom_start
, sz
);
281 for (idna_out
= NULL
, sz
= HOST_NAME_MAX
+1;; sz
+= HOST_NAME_MAX
) {
282 idna_out
= ac_alloc(sz
);
284 r
= idn_encodename(IDN_ENCODE_APP
, idna_in
, idna_out
, sz
);
287 case idn_buffer_overflow
:
289 case idn_invalid_encoding
:
290 n_err(_("Cannot convert from %s to %s\n"), charset_get_lc(), "UTF-8");
293 agp
->ag_n_flags
^= NAME_ADDRSPEC_ERR_IDNA
| NAME_ADDRSPEC_ERR_CHAR
;
297 if (r
== idn_success
)
302 /* Replace the domain part of .ag_skinned with IDNA version */
303 sz
= strlen(idna_out
);
304 i
= agp
->ag_sdom_start
;
305 cs
= salloc(agp
->ag_slen
- i
+ sz
+1);
306 memcpy(cs
, agp
->ag_skinned
, i
);
307 memcpy(cs
+ i
, idna_out
, sz
);
311 agp
->ag_skinned
= cs
;
313 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
,
314 NAME_NAME_SALLOC
| NAME_SKINNED
| NAME_IDNA
, 0);
322 # endif /* IDNA==IDNKIT */
323 #endif /* HAVE_IDNA */
326 _addrspec_check(int skinned
, struct addrguts
*agp
)
330 ui8_t in_domain
, hadat
;
331 union {char c
; unsigned char u
;} c
;
338 use_idna
= ok_blook(idna_disable
) ? 0 : 1;
340 agp
->ag_n_flags
|= NAME_ADDRSPEC_CHECKED
;
341 addr
= agp
->ag_skinned
;
343 if (agp
->ag_iaddr_aend
- agp
->ag_iaddr_start
== 0) {
344 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
, NAME_ADDRSPEC_ERR_EMPTY
, 0);
348 /* If the field is not a recipient, it cannot be a file or a pipe */
352 /* When changing any of the following adjust any RECIPIENTADDRSPEC;
353 * grep the latter for the complete picture */
355 agp
->ag_n_flags
|= NAME_ADDRSPEC_ISPIPE
;
358 if (addr
[0] == '/' || (addr
[0] == '.' && addr
[1] == '/'))
360 if (memchr(addr
, '@', agp
->ag_slen
) == NULL
) {
363 for (p
= addr
; (c
.c
= *p
); ++p
) {
364 if (c
.c
== '!' || c
.c
== '%')
368 agp
->ag_n_flags
|= NAME_ADDRSPEC_ISFILE
;
376 in_domain
= hadat
= 0;
378 for (p
= addr
; (c
.c
= *p
++) != '\0';) {
380 in_quote
= !in_quote
;
381 } else if (c
.u
< 040 || c
.u
>= 0177) { /* TODO no magics: !bodychar()? */
383 if (in_domain
&& use_idna
> 0) {
385 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
, NAME_ADDRSPEC_ERR_IDNA
,
391 } else if (in_domain
== 2) {
392 if ((c
.c
== ']' && *p
!= '\0') || c
.c
== '\\' || whitechar(c
.c
))
394 } else if (in_quote
&& in_domain
== 0) {
396 } else if (c
.c
== '\\' && *p
!= '\0') {
398 } else if (c
.c
== '@') {
400 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
, NAME_ADDRSPEC_ERR_ATSEQ
,
404 agp
->ag_sdom_start
= PTR2SIZE(p
- addr
);
405 agp
->ag_n_flags
|= NAME_ADDRSPEC_ISADDR
; /* TODO .. really? */
406 in_domain
= (*p
== '[') ? 2 : 1;
408 } else if (c
.c
== '(' || c
.c
== ')' || c
.c
== '<' || c
.c
== '>' ||
409 c
.c
== ',' || c
.c
== ';' || c
.c
== ':' || c
.c
== '\\' ||
410 c
.c
== '[' || c
.c
== ']')
415 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
, NAME_ADDRSPEC_ERR_CHAR
, c
.u
);
419 if (!(agp
->ag_n_flags
& NAME_ADDRSPEC_ISADDR
))
420 agp
->ag_n_flags
|= NAME_ADDRSPEC_ISNAME
;
424 agp
= _idna_apply(agp
);
428 return ((agp
->ag_n_flags
& NAME_ADDRSPEC_INVALID
) != 0);
432 gethfield(FILE *f
, char **linebuf
, size_t *linesize
, int rem
, char **colon
)
434 char *line2
= NULL
, *cp
, *cp2
;
435 size_t line2size
= 0;
439 if (*linebuf
== NULL
)
440 *linebuf
= srealloc(*linebuf
, *linesize
= 1);
447 if ((c
= readline_restart(f
, linebuf
, linesize
, 0)) <= 0) {
451 for (cp
= *linebuf
; fieldnamechar(*cp
); ++cp
)
454 while (blankchar(*cp
))
456 if (*cp
!= ':' || cp
== *linebuf
)
459 /* I guess we got a headline. Handle wraparound */
464 while (PTRCMP(--cp
, >=, *linebuf
) && blankchar(*cp
))
469 if (PTRCMP(cp
- 8, >=, *linebuf
) && cp
[-1] == '=' && cp
[-2] == '?')
471 ungetc(c
= getc(f
), f
);
474 c
= readline_restart(f
, &line2
, &line2size
, 0);
478 for (cp2
= line2
; blankchar(*cp2
); ++cp2
)
480 c
-= (int)PTR2SIZE(cp2
- line2
);
481 if (cp2
[0] == '=' && cp2
[1] == '?' && c
> 8)
483 if (PTRCMP(cp
+ c
, >=, *linebuf
+ *linesize
- 2)) {
484 size_t diff
= PTR2SIZE(cp
- *linebuf
),
485 colondiff
= PTR2SIZE(*colon
- *linebuf
);
486 *linebuf
= srealloc(*linebuf
, *linesize
+= c
+ 2);
487 cp
= &(*linebuf
)[diff
];
488 *colon
= &(*linebuf
)[colondiff
];
506 msgidnextc(char const **cp
, int *status
)
513 assert(status
!= NULL
);
531 *cp
= skip_comment(&(*cp
)[1]);
548 c
= (*status
& 02) ? lowerconv(c
) : c
;
558 charcount(char *str
, int c
)
564 for (i
= 0, cp
= str
; *cp
; ++cp
)
572 nexttoken(char const *cp
)
591 } while (nesting
> 0 && *cp
!= '\0'); /* XXX error? */
592 } else if (blankchar(*cp
) || *cp
== ',')
602 myaddrs(struct header
*hp
)
608 if (hp
!= NULL
&& (np
= hp
->h_from
) != NULL
) {
609 if ((rv
= np
->n_fullname
) != NULL
)
611 if ((rv
= np
->n_name
) != NULL
)
615 if ((rv
= ok_vlook(from
)) != NULL
)
618 /* When invoking *sendmail* directly, it's its task to generate an otherwise
619 * undeterminable From: address. However, if the user sets *hostname*,
620 * accept his desire */
621 if (ok_vlook(smtp
) != NULL
|| ok_vlook(hostname
) != NULL
) {
622 char *hn
= nodename(1);
623 size_t sz
= strlen(myname
) + strlen(hn
) + 1 +1;
625 sstpcpy(sstpcpy(sstpcpy(rv
, myname
), "@"), hn
);
633 myorigin(struct header
*hp
)
635 char const *rv
= NULL
, *ccp
;
639 if ((ccp
= myaddrs(hp
)) != NULL
&&
640 (np
= lextract(ccp
, GEXTRA
| GFULL
)) != NULL
)
641 rv
= (np
->n_flink
!= NULL
) ? ok_vlook(sender
) : ccp
;
647 is_head(char const *linebuf
, size_t linelen
, bool_t check_rfc4155
)
649 char date
[FROM_DATEBUF
];
653 if ((rv
= (linelen
>= 5 && !memcmp(linebuf
, "From ", 5))) && check_rfc4155
&&
654 (extract_date_from_from_(linebuf
, linelen
, date
) <= 0 ||
662 extract_date_from_from_(char const *line
, size_t linelen
,
663 char datebuf
[FROM_DATEBUF
])
666 char const *cp
= line
;
672 cp
= _from__skipword(cp
);
676 cp
= _from__skipword(cp
);
679 if (cp
[0] == 't' && cp
[1] == 't' && cp
[2] == 'y') {
680 cp
= _from__skipword(cp
);
684 /* It seems there are invalid MBOX archives in the wild, compare
685 * . http://bugs.debian.org/624111
686 * . [Mutt] #3868: mutt should error if the imported mailbox is invalid
687 * What they do is that they obfuscate the address to "name at host",
688 * and even "name at host dot dom dot dom. I think we should handle that */
689 else if(cp
[0] == 'a' && cp
[1] == 't' && cp
[2] == ' '){
693 cp
= _from__skipword(cp
);
696 if(cp
[0] == 'd' && cp
[1] == 'o' && cp
[2] == 't' && cp
[3] == ' '){
702 linelen
-= PTR2SIZE(cp
- line
);
703 if (linelen
< _DATE_MINLEN
)
705 if (cp
[linelen
- 1] == '\n') {
707 /* (Rather IMAP/POP3 only) */
708 if (cp
[linelen
- 1] == '\r')
710 if (linelen
< _DATE_MINLEN
)
713 if (linelen
>= FROM_DATEBUF
)
717 memcpy(datebuf
, cp
, linelen
);
718 datebuf
[linelen
] = '\0';
722 cp
= _("<Unknown date>");
723 linelen
= strlen(cp
);
724 if (linelen
>= FROM_DATEBUF
)
725 linelen
= FROM_DATEBUF
;
731 extract_header(FILE *fp
, struct header
*hp
, si8_t
*checkaddr_err
)
733 /* See the prototype declaration for the hairy relationship of
734 * options&OPT_t_FLAG and/or pstate&PS_t_FLAG in here */
735 struct header nh
, *hq
= &nh
;
736 char *linebuf
= NULL
/* TODO line pool */, *colon
;
737 size_t linesize
= 0, seenfields
= 0;
739 char const *val
, *cp
;
742 memset(hq
, 0, sizeof *hq
);
743 if ((pstate
& PS_t_FLAG
) && (options
& OPT_t_FLAG
)) {
746 hq
->h_bcc
= hp
->h_bcc
;
749 for (lc
= 0; readline_restart(fp
, &linebuf
, &linesize
, 0) > 0; ++lc
)
752 /* TODO yippieia, cat(check(lextract)) :-) */
754 while ((lc
= gethfield(fp
, &linebuf
, &linesize
, lc
, &colon
)) >= 0) {
757 /* We explicitly allow EAF_NAME for some addressees since aliases are not
758 * yet expanded when we parse these! */
759 if ((val
= thisfield(linebuf
, "to")) != NULL
) {
761 hq
->h_to
= cat(hq
->h_to
, checkaddrs(lextract(val
, GTO
| GFULL
),
762 EACM_NORMAL
| EAF_NAME
, checkaddr_err
));
763 } else if ((val
= thisfield(linebuf
, "cc")) != NULL
) {
765 hq
->h_cc
= cat(hq
->h_cc
, checkaddrs(lextract(val
, GCC
| GFULL
),
766 EACM_NORMAL
| EAF_NAME
, checkaddr_err
));
767 } else if ((val
= thisfield(linebuf
, "bcc")) != NULL
) {
769 hq
->h_bcc
= cat(hq
->h_bcc
, checkaddrs(lextract(val
, GBCC
| GFULL
),
770 EACM_NORMAL
| EAF_NAME
, checkaddr_err
));
771 } else if ((val
= thisfield(linebuf
, "from")) != NULL
) {
772 if (!(pstate
& PS_t_FLAG
) || (options
& OPT_t_FLAG
)) {
774 hq
->h_from
= cat(hq
->h_from
,
775 checkaddrs(lextract(val
, GEXTRA
| GFULL
| GFULLEXTRA
),
778 } else if ((val
= thisfield(linebuf
, "reply-to")) != NULL
) {
780 hq
->h_replyto
= cat(hq
->h_replyto
,
781 checkaddrs(lextract(val
, GEXTRA
| GFULL
), EACM_STRICT
, NULL
));
782 } else if ((val
= thisfield(linebuf
, "sender")) != NULL
) {
783 if (!(pstate
& PS_t_FLAG
) || (options
& OPT_t_FLAG
)) {
785 hq
->h_sender
= cat(hq
->h_sender
, /* TODO cat? check! */
786 checkaddrs(lextract(val
, GEXTRA
| GFULL
| GFULLEXTRA
),
789 } else if ((val
= thisfield(linebuf
, "organization")) != NULL
) {
791 for (cp
= val
; blankchar(*cp
); ++cp
)
793 hq
->h_organization
= (hq
->h_organization
!= NULL
)
794 ? save2str(hq
->h_organization
, cp
) : savestr(cp
);
795 } else if ((val
= thisfield(linebuf
, "subject")) != NULL
||
796 (val
= thisfield(linebuf
, "subj")) != NULL
) {
798 for (cp
= val
; blankchar(*cp
); ++cp
)
800 hq
->h_subject
= (hq
->h_subject
!= NULL
)
801 ? save2str(hq
->h_subject
, cp
) : savestr(cp
);
803 /* The remaining are mostly hacked in and thus TODO -- at least in
804 * TODO respect to their content checking */
805 else if ((pstate
& PS_t_FLAG
) &&
806 (val
= thisfield(linebuf
, "message-id")) != NULL
) {
807 np
= checkaddrs(lextract(val
, GREF
),
808 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG
| EACM_NONAME
,
810 if (np
== NULL
|| np
->n_flink
!= NULL
)
813 hq
->h_message_id
= np
;
814 } else if ((pstate
& PS_t_FLAG
) &&
815 (val
= thisfield(linebuf
, "in-reply-to")) != NULL
) {
816 np
= checkaddrs(lextract(val
, GREF
),
817 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG
| EACM_NONAME
,
819 if (np
== NULL
|| np
->n_flink
!= NULL
)
822 hq
->h_in_reply_to
= np
;
823 } else if ((pstate
& PS_t_FLAG
) &&
824 (val
= thisfield(linebuf
, "references")) != NULL
) {
826 /* TODO Limit number of references TODO better on parser side */
827 hq
->h_ref
= cat(hq
->h_ref
, checkaddrs(extract(val
, GREF
),
828 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG
| EACM_NONAME
,
830 /* and that is very hairy */
831 } else if ((pstate
& PS_t_FLAG
) &&
832 (val
= thisfield(linebuf
, "mail-followup-to")) != NULL
) {
834 hq
->h_mft
= cat(hq
->h_mft
, checkaddrs(lextract(val
, GEXTRA
| GFULL
),
835 /*EACM_STRICT | TODO '/' valid!! | EACM_NOLOG | */ EACM_NONAME
,
839 n_err(_("Ignoring header field \"%s\"\n"), linebuf
);
842 /* In case the blank line after the header has been edited out. Otherwise,
843 * fetch the header separator */
844 if (linebuf
!= NULL
) {
845 if (linebuf
[0] != '\0') {
846 for (cp
= linebuf
; *(++cp
) != '\0';)
848 fseek(fp
, (long)-PTR2SIZE(1 + cp
- linebuf
), SEEK_CUR
);
850 if ((c
= getc(fp
)) != '\n' && c
!= EOF
)
855 if (seenfields
> 0) {
858 hp
->h_bcc
= hq
->h_bcc
;
859 hp
->h_from
= hq
->h_from
;
860 hp
->h_replyto
= hq
->h_replyto
;
861 hp
->h_sender
= hq
->h_sender
;
862 hp
->h_organization
= hq
->h_organization
;
863 if (hq
->h_subject
!= NULL
|| !(pstate
& PS_t_FLAG
) ||
864 !(options
& OPT_t_FLAG
))
865 hp
->h_subject
= hq
->h_subject
;
867 if (pstate
& PS_t_FLAG
) {
868 hp
->h_ref
= hq
->h_ref
;
869 hp
->h_message_id
= hq
->h_message_id
;
870 hp
->h_in_reply_to
= hq
->h_in_reply_to
;
871 hp
->h_mft
= hq
->h_mft
;
873 /* And perform additional validity checks so that we don't bail later
874 * on TODO this is good and the place where this should occur,
875 * TODO unfortunately a lot of other places do again and blabla */
876 if (pstate
& PS_t_FLAG
) {
877 if (hp
->h_from
== NULL
)
878 hp
->h_from
= option_r_arg
;
879 else if (hp
->h_from
->n_flink
!= NULL
&& hp
->h_sender
== NULL
)
880 hp
->h_sender
= lextract(ok_vlook(sender
),
881 GEXTRA
| GFULL
| GFULLEXTRA
);
885 n_err(_("Restoring deleted header lines\n"));
893 hfield_mult(char const *field
, struct message
*mp
, int mult
)
898 size_t linesize
= 0; /* TODO line pool */
899 char *linebuf
= NULL
, *colon
;
903 /* There are (spam) messages which have header bytes which are many KB when
904 * joined, so resize a single heap storage until we are done if we shall
905 * collect a field that may have multiple bodies; only otherwise use the
906 * string dope directly */
907 memset(&hfs
, 0, sizeof hfs
);
909 if ((ibuf
= setinput(&mb
, mp
, NEED_HEADER
)) == NULL
)
911 if ((lc
= mp
->m_lines
- 1) < 0)
914 if ((mp
->m_flag
& MNOFROM
) == 0 &&
915 readline_restart(ibuf
, &linebuf
, &linesize
, 0) < 0)
918 if ((lc
= gethfield(ibuf
, &linebuf
, &linesize
, lc
, &colon
)) < 0)
920 if ((hfield
= thisfield(linebuf
, field
)) != NULL
&& *hfield
!= '\0') {
922 n_str_add_buf(&hfs
, hfield
, strlen(hfield
));
924 hfs
.s
= savestr(hfield
);
933 if (mult
&& hfs
.s
!= NULL
) {
934 colon
= savestrbuf(hfs
.s
, hfs
.l
);
943 thisfield(char const *linebuf
, char const *field
)
945 char const *rv
= NULL
;
948 while (lowerconv(*linebuf
) == lowerconv(*field
)) {
955 while (blankchar(*linebuf
))
957 if (*linebuf
++ != ':')
960 while (blankchar(*linebuf
)) /* TODO header parser.. strip trailing WS?!? */
969 nameof(struct message
*mp
, int reptype
)
974 cp
= skin(name1(mp
, reptype
));
975 if (reptype
!= 0 || charcount(cp
, '!') < 2)
977 cp2
= strrchr(cp
, '!');
979 while (cp2
> cp
&& *cp2
!= '!')
989 skip_comment(char const *cp
)
994 for (nesting
= 1; nesting
> 0 && *cp
; ++cp
) {
1013 routeaddr(char const *name
)
1015 char const *np
, *rp
= NULL
;
1018 for (np
= name
; *np
; np
++) {
1021 np
= skip_comment(np
+ 1) - 1;
1027 if (*np
== '\\' && np
[1])
1044 FL
enum expand_addr_flags
1045 expandaddr_to_eaf(void)
1048 char const *eafd_name
;
1049 bool_t eafd_is_target
;
1053 {"restrict", FAL0
, EAF_TARGET_MASK
, EAF_RESTRICT
| EAF_RESTRICT_TARGETS
},
1054 {"fail", FAL0
, EAF_NONE
, EAF_FAIL
},
1055 {"all", TRU1
, EAF_NONE
, EAF_TARGET_MASK
},
1056 {"file", TRU1
, EAF_NONE
, EAF_FILE
},
1057 {"pipe", TRU1
, EAF_NONE
, EAF_PIPE
},
1058 {"name", TRU1
, EAF_NONE
, EAF_NAME
},
1059 {"addr", TRU1
, EAF_NONE
, EAF_ADDR
}
1063 enum expand_addr_flags rv
;
1067 if ((cp
= ok_vlook(expandaddr
)) == NULL
)
1068 rv
= EAF_RESTRICT_TARGETS
;
1069 else if (*cp
== '\0')
1070 rv
= EAF_TARGET_MASK
;
1072 rv
= EAF_TARGET_MASK
;
1074 for (buf
= savestr(cp
); (cp
= n_strsep(&buf
, ',', TRU1
)) != NULL
;) {
1077 if ((minus
= (*cp
== '-')) || *cp
== '+')
1079 for (eafp
= eafa
;; ++eafp
) {
1080 if (eafp
== eafa
+ NELEM(eafa
)) {
1081 if (options
& OPT_D_V
)
1082 n_err(_("Unknown *expandaddr* value: \"%s\"\n"), cp
);
1084 } else if (!asccasecmp(cp
, eafp
->eafd_name
)) {
1086 rv
&= ~eafp
->eafd_andoff
;
1087 rv
|= eafp
->eafd_or
;
1089 if (eafp
->eafd_is_target
)
1090 rv
&= ~eafp
->eafd_or
;
1091 else if (options
& OPT_D_V
)
1092 n_err(_("\"-\" prefix invalid for *expandaddr* value: "
1096 } else if (!asccasecmp(cp
, "noalias")) { /* TODO v15 OBSOLETE */
1097 OBSOLETE(_("*expandaddr*: \"noalias\" is henceforth \"-name\""));
1104 if ((rv
& EAF_RESTRICT
) && (options
& (OPT_INTERACTIVE
| OPT_TILDE_FLAG
)))
1105 rv
|= EAF_TARGET_MASK
;
1106 else if (options
& OPT_D_V
) {
1107 if (!(rv
& EAF_TARGET_MASK
))
1108 n_err(_("*expandaddr* doesn't allow any addresses\n"));
1109 else if ((rv
& EAF_FAIL
) && (rv
& EAF_TARGET_MASK
) == EAF_TARGET_MASK
)
1110 n_err(_("*expandaddr* with \"fail\" but no restrictions\n"));
1118 is_addr_invalid(struct name
*np
, enum expand_addr_check_mode eacm
)
1120 char cbuf
[sizeof "'\\U12340'"];
1121 enum expand_addr_flags eaf
;
1129 if ((rv
= ((f
& NAME_ADDRSPEC_INVALID
) != 0))) {
1130 if ((eacm
& EACM_NOLOG
) || (f
& NAME_ADDRSPEC_ERR_EMPTY
)) {
1134 char const *fmt
= "'\\x%02X'";
1135 bool_t ok8bit
= TRU1
;
1137 if (f
& NAME_ADDRSPEC_ERR_IDNA
) {
1138 cs
= _("Invalid domain name: \"%s\", character %s\n");
1141 } else if (f
& NAME_ADDRSPEC_ERR_ATSEQ
)
1142 cs
= _("\"%s\" contains invalid %s sequence\n");
1144 cs
= _("\"%s\" contains invalid character %s\n");
1146 c
= NAME_ADDRSPEC_ERR_GETWC(f
);
1147 snprintf(cbuf
, sizeof cbuf
,
1148 (ok8bit
&& c
>= 040 && c
<= 0177 ? "'%c'" : fmt
), c
);
1154 /* *expandaddr* stuff */
1155 if (!(rv
= ((eacm
& EACM_MODE_MASK
) != EACM_NONE
)))
1158 eaf
= expandaddr_to_eaf();
1160 if ((eacm
& EACM_STRICT
) && (f
& NAME_ADDRSPEC_ISFILEORPIPE
)) {
1163 cs
= _("\"%s\"%s: file or pipe addressees not allowed here\n");
1164 if (eacm
& EACM_NOLOG
)
1170 eaf
|= (eacm
& EAF_TARGET_MASK
);
1171 if (eacm
& EACM_NONAME
)
1174 if (eaf
== EAF_NONE
) {
1181 if (!(eaf
& EAF_FILE
) && (f
& NAME_ADDRSPEC_ISFILE
)) {
1182 cs
= _("\"%s\"%s: *expandaddr* doesn't allow file target\n");
1183 if (eacm
& EACM_NOLOG
)
1185 } else if (!(eaf
& EAF_PIPE
) && (f
& NAME_ADDRSPEC_ISPIPE
)) {
1186 cs
= _("\"%s\"%s: *expandaddr* doesn't allow command pipe target\n");
1187 if (eacm
& EACM_NOLOG
)
1189 } else if (!(eaf
& EAF_NAME
) && (f
& NAME_ADDRSPEC_ISNAME
)) {
1190 cs
= _("\"%s\"%s: *expandaddr* doesn't allow user name target\n");
1191 if (eacm
& EACM_NOLOG
)
1193 } else if (!(eaf
& EAF_ADDR
) && (f
& NAME_ADDRSPEC_ISADDR
)) {
1194 cs
= _("\"%s\"%s: *expandaddr* doesn't allow mail address target\n");
1195 if (eacm
& EACM_NOLOG
)
1205 n_err(cs
, np
->n_name
, cbuf
);
1212 skin(char const *name
)
1219 addrspec_with_guts(1, name
, &ag
);
1220 ret
= ag
.ag_skinned
;
1221 if (!(ag
.ag_n_flags
& NAME_NAME_SALLOC
))
1222 ret
= savestrbuf(ret
, ag
.ag_slen
);
1228 /* TODO addrspec_with_guts: RFC 5322
1229 * TODO addrspec_with_guts: trim whitespace ETC. ETC. ETC.!!! */
1231 addrspec_with_guts(int doskin
, char const *name
, struct addrguts
*agp
)
1234 char *cp2
, *bufend
, *nbuf
, c
, gotlt
, gotaddr
, lastsp
;
1238 memset(agp
, 0, sizeof *agp
);
1240 if ((agp
->ag_input
= name
) == NULL
|| (agp
->ag_ilen
= strlen(name
)) == 0) {
1241 agp
->ag_skinned
= UNCONST(""); /* ok: NAME_SALLOC is not set */
1243 agp
->ag_n_flags
|= NAME_ADDRSPEC_CHECKED
;
1244 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
, NAME_ADDRSPEC_ERR_EMPTY
, 0);
1248 if (!doskin
|| !anyof(name
, "(< ")) {
1249 /*agp->ag_iaddr_start = 0;*/
1250 agp
->ag_iaddr_aend
= agp
->ag_ilen
;
1251 agp
->ag_skinned
= UNCONST(name
); /* (NAME_SALLOC not set) */
1252 agp
->ag_slen
= agp
->ag_ilen
;
1253 agp
->ag_n_flags
= NAME_SKINNED
;
1257 /* Something makes us think we have to perform the skin operation */
1258 nbuf
= ac_alloc(agp
->ag_ilen
+ 1);
1259 /*agp->ag_iaddr_start = 0;*/
1260 cp2
= bufend
= nbuf
;
1261 gotlt
= gotaddr
= lastsp
= 0;
1263 for (cp
= name
++; (c
= *cp
++) != '\0'; ) {
1266 cp
= skip_comment(cp
);
1270 /* Start of a "quoted-string".
1271 * Copy it in its entirety */
1272 /* XXX RFC: quotes are "semantically invisible"
1273 * XXX But it was explicitly added (Changelog.Heirloom,
1274 * XXX [9.23] released 11/15/00, "Do not remove quotes
1275 * XXX when skinning names"? No more info.. */
1277 while ((c
= *cp
) != '\0') { /* TODO improve */
1285 else if ((c
= *cp
) != '\0') {
1296 agp
->ag_iaddr_aend
= PTR2SIZE(cp
- name
);
1298 if (cp
[0] == 'a' && cp
[1] == 't' && blankchar(cp
[2]))
1299 cp
+= 3, *cp2
++ = '@';
1300 else if (cp
[0] == '@' && blankchar(cp
[1]))
1301 cp
+= 2, *cp2
++ = '@';
1306 agp
->ag_iaddr_start
= PTR2SIZE(cp
- (name
- 1));
1308 gotlt
= gotaddr
= 1;
1313 /* (_addrspec_check() verifies these later!) */
1314 agp
->ag_iaddr_aend
= PTR2SIZE(cp
- name
);
1316 while ((c
= *cp
) != '\0' && c
!= ',') {
1319 cp
= skip_comment(cp
);
1321 while ((c
= *cp
) != '\0') {
1325 if (c
== '\\' && *cp
!= '\0')
1343 for (; blankchar(*cp
); ++cp
)
1348 } else if (!gotaddr
) {
1350 agp
->ag_iaddr_start
= PTR2SIZE(cp
- name
);
1354 agp
->ag_slen
= PTR2SIZE(cp2
- nbuf
);
1355 if (agp
->ag_iaddr_aend
== 0)
1356 agp
->ag_iaddr_aend
= agp
->ag_ilen
;
1358 agp
->ag_skinned
= savestrbuf(nbuf
, agp
->ag_slen
);
1360 agp
->ag_n_flags
= NAME_NAME_SALLOC
| NAME_SKINNED
;
1362 rv
= _addrspec_check(doskin
, agp
);
1369 realname(char const *name
)
1371 char const *cp
, *cq
, *cstart
= NULL
, *cend
= NULL
;
1374 int quoted
, good
, nogood
;
1377 if ((cp
= UNCONST(name
)) == NULL
)
1379 for (; *cp
!= '\0'; ++cp
) {
1382 if (cstart
!= NULL
) {
1383 /* More than one comment in address, doesn't make sense to display
1384 * it without context. Return the entire field */
1385 cp
= mime_fromaddr(name
);
1389 cp
= skip_comment(cp
);
1392 cend
= cstart
= NULL
;
1398 if (*cp
== '\\' && cp
[1])
1409 /* More than one address. Just use the first one */
1415 if (cstart
== NULL
) {
1417 /* If name contains only a route-addr, the surrounding angle brackets
1418 * don't serve any useful purpose when displaying, so remove */
1419 cp
= prstr(skin(name
));
1421 cp
= mime_fromaddr(name
);
1425 /* Strip quotes. Note that quotes that appear within a MIME encoded word are
1426 * not stripped. The idea is to strip only syntactical relevant things (but
1427 * this is not necessarily the most sensible way in practice) */
1428 rp
= rname
= ac_alloc(PTR2SIZE(cend
- cstart
+1));
1430 for (cp
= cstart
; cp
< cend
; ++cp
) {
1431 if (*cp
== '(' && !quoted
) {
1432 cq
= skip_comment(++cp
);
1433 if (PTRCMP(--cq
, >, cend
))
1436 if (*cp
== '\\' && PTRCMP(cp
+ 1, <, cq
))
1440 } else if (*cp
== '\\' && PTRCMP(cp
+ 1, <, cend
))
1442 else if (*cp
== '"') {
1451 mime_fromhdr(&in
, &out
, TD_ISPR
| TD_ICONV
);
1453 rname
= savestr(out
.s
);
1456 while (blankchar(*rname
))
1458 for (rp
= rname
; *rp
!= '\0'; ++rp
)
1460 while (PTRCMP(--rp
, >=, rname
) && blankchar(*rp
))
1463 cp
= mime_fromaddr(name
);
1467 /* mime_fromhdr() has converted all nonprintable characters to question
1468 * marks now. These and blanks are considered uninteresting; if the
1469 * displayed part of the real name contains more than 25% of them, it is
1470 * probably better to display the plain email address instead */
1473 for (rp
= rname
; *rp
!= '\0' && PTRCMP(rp
, <, rname
+ 20); ++rp
)
1474 if (*rp
== '?' || blankchar(*rp
))
1478 cp
= (good
* 3 < nogood
) ? prstr(skin(name
)) : rname
;
1485 name1(struct message
*mp
, int reptype
)
1487 char *namebuf
, *cp
, *cp2
, *linebuf
= NULL
/* TODO line pool */;
1488 size_t namesize
, linesize
= 0;
1493 if ((cp
= hfield1("from", mp
)) != NULL
&& *cp
!= '\0')
1495 if (reptype
== 0 && (cp
= hfield1("sender", mp
)) != NULL
&& *cp
!= '\0')
1498 namebuf
= smalloc(namesize
= 1);
1500 if (mp
->m_flag
& MNOFROM
)
1502 if ((ibuf
= setinput(&mb
, mp
, NEED_HEADER
)) == NULL
)
1504 if (readline_restart(ibuf
, &linebuf
, &linesize
, 0) < 0)
1508 if (namesize
<= linesize
)
1509 namebuf
= srealloc(namebuf
, namesize
= linesize
+1);
1510 for (cp
= linebuf
; *cp
!= '\0' && *cp
!= ' '; ++cp
)
1512 for (; blankchar(*cp
); ++cp
)
1514 for (cp2
= namebuf
+ strlen(namebuf
);
1515 *cp
&& !blankchar(*cp
) && PTRCMP(cp2
, <, namebuf
+ namesize
-1);)
1519 if (readline_restart(ibuf
, &linebuf
, &linesize
, 0) < 0)
1521 if ((cp
= strchr(linebuf
, 'F')) == NULL
)
1523 if (strncmp(cp
, "From", 4))
1525 if (namesize
<= linesize
)
1526 namebuf
= srealloc(namebuf
, namesize
= linesize
+ 1);
1528 while ((cp
= strchr(cp
, 'r')) != NULL
) {
1529 if (!strncmp(cp
, "remote", 6)) {
1530 if ((cp
= strchr(cp
, 'f')) == NULL
)
1532 if (strncmp(cp
, "from", 4) != 0)
1534 if ((cp
= strchr(cp
, ' ')) == NULL
)
1538 strncpy(namebuf
, cp
, namesize
);
1541 cp2
= strrchr(namebuf
, '!') + 1;
1542 strncpy(cp2
, cp
, PTR2SIZE(namebuf
+ namesize
- cp2
));
1544 namebuf
[namesize
- 2] = '!';
1545 namebuf
[namesize
- 1] = '\0';
1551 if (*namebuf
!= '\0' || ((cp
= hfield1("return-path", mp
))) == NULL
||
1553 cp
= savestr(namebuf
);
1555 if (linebuf
!= NULL
)
1564 subject_re_trim(char *s
)
1569 } const *pp
, ignored
[] = { /* Update *reply-strings* manual upon change! */
1571 { 3, "aw:" }, { 5, "antw:" }, /* de */
1576 char *orig_s
= s
, *re_st
= NULL
, *re_st_x
;
1577 size_t re_l
= 0 /* pacify CC */;
1580 if ((re_st_x
= ok_vlook(reply_strings
)) != NULL
&&
1581 (re_l
= strlen(re_st_x
)) > 0) {
1582 re_st
= ac_alloc(++re_l
* 2);
1583 memcpy(re_st
, re_st_x
, re_l
);
1587 while (*s
!= '\0') {
1588 while (spacechar(*s
))
1591 for (pp
= ignored
; pp
->len
> 0; ++pp
)
1592 if (is_asccaseprefix(s
, pp
->dat
)) {
1598 if (re_st
!= NULL
) {
1601 memcpy(re_st_x
= re_st
+ re_l
, re_st
, re_l
);
1602 while ((cp
= n_strsep(&re_st_x
, ',', TRU1
)) != NULL
)
1603 if (is_asccaseprefix(s
, cp
)) {
1615 return any
? s
: orig_s
;
1619 msgidcmp(char const *s1
, char const *s2
)
1621 int q1
= 0, q2
= 0, c1
, c2
;
1630 c1
= msgidnextc(&s1
, &q1
);
1631 c2
= msgidnextc(&s2
, &q2
);
1640 is_ign(char const *field
, size_t fieldlen
, struct ignoretab igta
[2])
1650 if (igta
== allignore
)
1653 /* Lowercase it so that "Status" and "status" will hash to the same place */
1654 realfld
= ac_alloc(fieldlen
+1);
1655 i_strcpy(realfld
, field
, fieldlen
+1);
1656 if (igta
[1].i_count
> 0)
1657 rv
= !member(realfld
, igta
+ 1);
1659 rv
= member(realfld
, igta
);
1667 member(char const *realfield
, struct ignoretab
*table
)
1669 struct ignored
*igp
;
1673 for (igp
= table
->i_head
[hash(realfield
)]; igp
!= 0; igp
= igp
->i_link
)
1674 if (*igp
->i_field
== *realfield
&& !strcmp(igp
->i_field
, realfield
)) {
1683 fakefrom(struct message
*mp
)
1688 if (((name
= skin(hfield1("return-path", mp
))) == NULL
|| *name
== '\0' ) &&
1689 ((name
= skin(hfield1("from", mp
))) == NULL
|| *name
== '\0'))
1690 /* XXX MAILER-DAEMON is what an old MBOX manual page says.
1691 * RFC 4155 however requires a RFC 5322 (2822) conforming
1692 * "addr-spec", but we simply can't provide that */
1693 name
= "MAILER-DAEMON";
1705 for (cq
= cp
; *cq
!= '\0' && *cq
!= '\n'; ++cq
)
1713 #if defined HAVE_IMAP_SEARCH || defined HAVE_IMAP
1715 unixtime(char const *fromline
)
1720 int i
, year
, month
, day
, hour
, minute
, second
, tzdiff
;
1724 for (fp
= fromline
; *fp
!= '\0' && *fp
!= '\n'; ++fp
)
1727 if (PTR2SIZE(fp
- fromline
) < 7)
1732 if (!strncmp(fp
+ 4, month_names
[i
], 3))
1734 if (month_names
[++i
][0] == '\0')
1740 day
= strtol(fp
+ 8, &xp
, 10);
1741 if (*xp
!= ' ' || xp
!= fp
+ 10)
1743 hour
= strtol(fp
+ 11, &xp
, 10);
1744 if (*xp
!= ':' || xp
!= fp
+ 13)
1746 minute
= strtol(fp
+ 14, &xp
, 10);
1747 if (*xp
!= ':' || xp
!= fp
+ 16)
1749 second
= strtol(fp
+ 17, &xp
, 10);
1750 if (*xp
!= ' ' || xp
!= fp
+ 19)
1752 year
= strtol(fp
+ 20, &xp
, 10);
1755 if ((t
= combinetime(year
, month
, day
, hour
, minute
, second
)) == (time_t)-1)
1757 tzdiff
= t
- mktime(gmtime(&t
));
1758 tmptr
= localtime(&t
);
1759 if (tmptr
->tm_isdst
> 0)
1769 #endif /* HAVE_IMAP_SEARCH || defined HAVE_IMAP */
1772 rfctime(char const *date
)
1774 char const *cp
= date
;
1777 int i
, year
, month
, day
, hour
, minute
, second
;
1780 if ((cp
= nexttoken(cp
)) == NULL
)
1782 if (alphachar(cp
[0]) && alphachar(cp
[1]) && alphachar(cp
[2]) &&
1784 if ((cp
= nexttoken(&cp
[4])) == NULL
)
1787 day
= strtol(cp
, &x
, 10); /* XXX strtol */
1788 if ((cp
= nexttoken(x
)) == NULL
)
1791 if (!strncmp(cp
, month_names
[i
], 3))
1793 if (month_names
[++i
][0] == '\0')
1797 if ((cp
= nexttoken(&cp
[3])) == NULL
)
1800 * Where a two or three digit year occurs in a date, the year is to be
1801 * interpreted as follows: If a two digit year is encountered whose
1802 * value is between 00 and 49, the year is interpreted by adding 2000,
1803 * ending up with a value between 2000 and 2049. If a two digit year
1804 * is encountered with a value between 50 and 99, or any three digit
1805 * year is encountered, the year is interpreted by adding 1900 */
1806 year
= strtol(cp
, &x
, 10); /* XXX strtol */
1807 i
= (int)PTR2SIZE(x
- cp
);
1808 if (i
== 2 && year
>= 0 && year
<= 49)
1810 else if (i
== 3 || (i
== 2 && year
>= 50 && year
<= 99))
1812 if ((cp
= nexttoken(x
)) == NULL
)
1814 hour
= strtol(cp
, &x
, 10); /* XXX strtol */
1818 minute
= strtol(cp
, &x
, 10);
1821 second
= strtol(cp
, &x
, 10);
1825 if ((t
= combinetime(year
, month
, day
, hour
, minute
, second
)) == (time_t)-1)
1827 if ((cp
= nexttoken(x
)) != NULL
) {
1839 if (digitchar(cp
[0]) && digitchar(cp
[1]) && digitchar(cp
[2]) &&
1845 tadj
= strtol(buf
, NULL
, 10) * 3600;/*XXX strtrol*/
1848 tadj
+= strtol(buf
, NULL
, 10) * 60; /* XXX strtol*/
1853 /* TODO WE DO NOT YET PARSE (OBSOLETE) ZONE NAMES
1854 * TODO once again, Christos Zoulas and NetBSD Mail have done
1855 * TODO a really good job already, but using strptime(3), which
1856 * TODO is not portable. Nonetheless, WE must improve, not
1857 * TODO at last because we simply ignore obsolete timezones!!
1858 * TODO See RFC 5322, 4.3! */
1868 #define is_leapyear(Y) ((((Y) % 100 ? (Y) : (Y) / 100) & 3) == 0)
1871 combinetime(int year
, int month
, int day
, int hour
, int minute
, int second
)
1876 if (second
< 0 || minute
< 0 || hour
< 0 || day
< 1) {
1881 t
= second
+ minute
* 60 + hour
* 3600 + (day
- 1) * 86400;
1885 t
+= 86400 * (is_leapyear(year
) ? 29 : 28);
1905 t
+= (year
- 70) * 31536000 + ((year
- 69) / 4) * 86400 -
1906 ((year
- 1) / 100) * 86400 + ((year
+ 299) / 400) * 86400;
1913 substdate(struct message
*m
)
1918 /* Determine the date to print in faked 'From ' lines. This is traditionally
1919 * the date the message was written to the mail file. Try to determine this
1920 * using RFC message header fields, or fall back to current time */
1921 if ((cp
= hfield1("received", m
)) != NULL
) {
1922 while ((cp
= nexttoken(cp
)) != NULL
&& *cp
!= ';') {
1925 while (alnumchar(*cp
));
1928 m
->m_time
= rfctime(cp
);
1930 if (m
->m_time
== 0 || m
->m_time
> time_current
.tc_time
) {
1931 if ((cp
= hfield1("date", m
)) != NULL
)
1932 m
->m_time
= rfctime(cp
);
1934 if (m
->m_time
== 0 || m
->m_time
> time_current
.tc_time
)
1935 m
->m_time
= time_current
.tc_time
;
1939 FL
struct name
const *
1940 check_from_and_sender(struct name
const *fromfield
,
1941 struct name
const *senderfield
)
1943 struct name
const *rv
= NULL
;
1946 if (senderfield
!= NULL
) {
1947 if (senderfield
->n_flink
!= NULL
) {
1948 n_err(_("The \"Sender:\" field may contain only one address\n"));
1954 if (fromfield
!= NULL
) {
1955 if (fromfield
->n_flink
!= NULL
&& senderfield
== NULL
) {
1956 n_err(_("A \"Sender:\" field is required with multiple "
1957 "addresses in \"From:\" field\n"));
1965 rv
= (struct name
*)0x1;
1973 getsender(struct message
*mp
)
1979 if ((cp
= hfield1("from", mp
)) == NULL
||
1980 (np
= lextract(cp
, GEXTRA
| GSKIN
)) == NULL
)
1983 cp
= (np
->n_flink
!= NULL
) ? skin(hfield1("sender", mp
)) : np
->n_name
;
1990 grab_headers(struct header
*hp
, enum gfield gflags
, int subjfirst
)
1992 /* TODO grab_headers: again, check counts etc. against RFC;
1993 * TODO (now assumes check_from_and_sender() is called afterwards ++ */
1999 comma
= (ok_blook(bsdcompat
) || ok_blook(bsdmsgs
)) ? 0 : GCOMMA
;
2002 hp
->h_to
= grab_names("To: ", hp
->h_to
, comma
, GTO
| GFULL
);
2003 if (subjfirst
&& (gflags
& GSUBJECT
))
2004 hp
->h_subject
= n_input_cp_addhist("Subject: ", hp
->h_subject
, TRU1
);
2006 hp
->h_cc
= grab_names("Cc: ", hp
->h_cc
, comma
, GCC
| GFULL
);
2008 hp
->h_bcc
= grab_names("Bcc: ", hp
->h_bcc
, comma
, GBCC
| GFULL
);
2010 if (gflags
& GEXTRA
) {
2011 if (hp
->h_from
== NULL
)
2012 hp
->h_from
= lextract(myaddrs(hp
), GEXTRA
| GFULL
| GFULLEXTRA
);
2013 hp
->h_from
= grab_names("From: ", hp
->h_from
, comma
,
2014 GEXTRA
| GFULL
| GFULLEXTRA
);
2015 if (hp
->h_replyto
== NULL
)
2016 hp
->h_replyto
= lextract(ok_vlook(replyto
), GEXTRA
| GFULL
);
2017 hp
->h_replyto
= grab_names("Reply-To: ", hp
->h_replyto
, comma
,
2019 if (hp
->h_sender
== NULL
)
2020 hp
->h_sender
= extract(ok_vlook(sender
), GEXTRA
| GFULL
);
2021 hp
->h_sender
= grab_names("Sender: ", hp
->h_sender
, comma
,
2023 if (hp
->h_organization
== NULL
)
2024 hp
->h_organization
= ok_vlook(ORGANIZATION
);
2025 hp
->h_organization
= n_input_cp_addhist("Organization: ",
2026 hp
->h_organization
, TRU1
);
2029 if (!subjfirst
&& (gflags
& GSUBJECT
))
2030 hp
->h_subject
= n_input_cp_addhist("Subject: ", hp
->h_subject
, TRU1
);
2037 header_match(struct message
*mp
, struct search_expr
const *sep
)
2042 size_t linesize
= 0; /* TODO line pool */
2043 char *linebuf
= NULL
, *colon
;
2047 if ((ibuf
= setinput(&mb
, mp
, NEED_HEADER
)) == NULL
)
2049 if ((lc
= mp
->m_lines
- 1) < 0)
2052 if ((mp
->m_flag
& MNOFROM
) == 0 &&
2053 readline_restart(ibuf
, &linebuf
, &linesize
, 0) < 0)
2056 if (gethfield(ibuf
, &linebuf
, &linesize
, lc
, &colon
) <= 0)
2058 if (blankchar(*++colon
))
2060 in
.l
= strlen(in
.s
= colon
);
2061 mime_fromhdr(&in
, &out
, TD_ICONV
);
2063 if (sep
->ss_sexpr
== NULL
)
2064 rv
= (regexec(&sep
->ss_regex
, out
.s
, 0,NULL
, 0) != REG_NOMATCH
);
2067 rv
= substr(out
.s
, sep
->ss_sexpr
);
2074 if (linebuf
!= NULL
)