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 - 2014 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. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #ifndef HAVE_AMALGAMATION
46 # include <idn-free.h>
47 # include <stringprep.h>
51 size_t tlen
; /* Length of .tdata */
52 char const *tdata
; /* Template date - see _cmatch_data[] */
55 /* Template characters for cmatch_data.tdata:
56 * 'A' An upper case char
57 * 'a' A lower case char
60 * 'O' An optional digit or space
62 * '+' Either a plus or a minus sign */
63 static struct cmatch_data
const _cmatch_data
[] = {
64 { 24, "Aaa Aaa O0 00:00:00 0000" }, /* BSD/ISO C90 ctime */
65 { 28, "Aaa Aaa O0 00:00:00 AAA 0000" }, /* BSD tmz */
66 { 21, "Aaa Aaa O0 00:00 0000" }, /* SysV ctime */
67 { 25, "Aaa Aaa O0 00:00 AAA 0000" }, /* SysV tmz */
68 /* RFC 822-alike From_ lines do not conform to RFC 4155, but seem to be used
69 * in the wild (by UW-imap) */
70 { 30, "Aaa Aaa O0 00:00:00 0000 +0000" },
71 /* RFC 822 with zone spec; 1. military, 2. UT, 3. north america time
72 * zone strings; note that 1. is strictly speaking not correct as some
73 * letters are not used, and 2. is not because only "UT" is defined */
74 #define __reuse "Aaa Aaa O0 00:00:00 0000 AAA"
75 { 28 - 2, __reuse
}, { 28 - 1, __reuse
}, { 28 - 0, __reuse
},
78 #define _DATE_MINLEN 21
80 /* Skip over "word" as found in From_ line */
81 static char const * _from__skipword(char const *wp
);
83 /* Match the date string against the date template (tp), return if match.
84 * See _cmatch_data[] for template character description */
85 static int _cmatch(size_t len
, char const *date
,
88 /* Check wether date is a valid 'From_' date.
89 * (Rather ctime(3) generated dates, according to RFC 4155) */
90 static int _is_date(char const *date
);
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 addrguts
* _idna_apply(struct 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 int _addrspec_check(int doskin
, struct addrguts
*agp
);
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
)) >= _DATE_MINLEN
)
195 for (cmdp
= _cmatch_data
; cmdp
->tdata
!= NULL
; ++cmdp
)
196 if (dl
== cmdp
->tlen
&& (rv
= _cmatch(dl
, date
, cmdp
->tdata
)))
203 static struct addrguts
*
204 _idna_apply(struct addrguts
*agp
)
206 char *idna_utf8
, *idna_ascii
, *cs
;
210 sz
= agp
->ag_slen
- agp
->ag_sdom_start
;
212 idna_utf8
= ac_alloc(sz
+1);
213 memcpy(idna_utf8
, agp
->ag_skinned
+ agp
->ag_sdom_start
, sz
);
214 idna_utf8
[sz
] = '\0';
216 /* GNU Libidn settles on top of iconv(3) without any fallback, so let's just
217 * let it perform the charset conversion, if any should be necessary */
218 if (!(options
& OPT_UNICODE
)) {
219 char const *tcs
= charset_get_lc();
220 idna_ascii
= idna_utf8
;
221 idna_utf8
= stringprep_convert(idna_ascii
, "UTF-8", tcs
);
222 i
= (idna_utf8
== NULL
&& errno
== EINVAL
);
225 if (idna_utf8
== NULL
) {
227 fprintf(stderr
, _("Cannot convert from %s to %s\n"),
229 agp
->ag_n_flags
^= NAME_ADDRSPEC_ERR_IDNA
| NAME_ADDRSPEC_ERR_CHAR
;
234 if (idna_to_ascii_8z(idna_utf8
, &idna_ascii
, 0) != IDNA_SUCCESS
) {
235 agp
->ag_n_flags
^= NAME_ADDRSPEC_ERR_IDNA
| NAME_ADDRSPEC_ERR_CHAR
;
239 /* Replace the domain part of .ag_skinned with IDNA version */
240 sz
= strlen(idna_ascii
);
241 i
= agp
->ag_sdom_start
;
242 cs
= salloc(agp
->ag_slen
- i
+ sz
+1);
243 memcpy(cs
, agp
->ag_skinned
, i
);
244 memcpy(cs
+ i
, idna_ascii
, sz
);
248 agp
->ag_skinned
= cs
;
250 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
,
251 NAME_NAME_SALLOC
| NAME_SKINNED
| NAME_IDNA
, 0);
253 idn_free(idna_ascii
);
255 if (options
& OPT_UNICODE
)
266 _addrspec_check(int skinned
, struct addrguts
*agp
)
270 ui8_t in_domain
, hadat
;
271 union {char c
; unsigned char u
;} c
;
278 use_idna
= ok_blook(idna_disable
) ? 0 : 1;
280 agp
->ag_n_flags
|= NAME_ADDRSPEC_CHECKED
;
281 addr
= agp
->ag_skinned
;
283 if (agp
->ag_iaddr_aend
- agp
->ag_iaddr_start
== 0) {
284 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
, NAME_ADDRSPEC_ERR_EMPTY
, 0);
288 /* If the field is not a recipient, it cannot be a file or a pipe */
292 /* Excerpt from nail.1:
293 * Recipient address specifications
294 * The rules are: Any name which starts with a `|' character specifies
295 * a pipe, the command string following the `|' is executed and
296 * the message is sent to its standard input; any other name which
297 * contains a `@' character is treated as a mail address; any other
298 * name which starts with a `+' character specifies a folder name; any
299 * other name which contains a `/' character but no `!' or `%'
300 * character before also specifies a folder name; what remains is
301 * treated as a mail address */
303 agp
->ag_n_flags
|= NAME_ADDRSPEC_ISPIPE
;
306 if (memchr(addr
, '@', agp
->ag_slen
) == NULL
) {
309 for (p
= addr
; (c
.c
= *p
); ++p
) {
310 if (c
.c
== '!' || c
.c
== '%')
314 agp
->ag_n_flags
|= NAME_ADDRSPEC_ISFILE
;
322 in_domain
= hadat
= 0;
324 for (p
= addr
; (c
.c
= *p
++) != '\0';) {
326 in_quote
= !in_quote
;
327 } else if (c
.u
< 040 || c
.u
>= 0177) { /* TODO no magics: !bodychar()? */
329 if (in_domain
&& use_idna
> 0) {
331 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
, NAME_ADDRSPEC_ERR_IDNA
,
337 } else if (in_domain
== 2) {
338 if ((c
.c
== ']' && *p
!= '\0') || c
.c
== '\\' || whitechar(c
.c
))
340 } else if (in_quote
&& in_domain
== 0) {
342 } else if (c
.c
== '\\' && *p
!= '\0') {
344 } else if (c
.c
== '@') {
346 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
, NAME_ADDRSPEC_ERR_ATSEQ
,
350 agp
->ag_sdom_start
= PTR2SIZE(p
- addr
);
351 in_domain
= (*p
== '[') ? 2 : 1;
353 } else if (c
.c
== '(' || c
.c
== ')' || c
.c
== '<' || c
.c
== '>' ||
354 c
.c
== ',' || c
.c
== ';' || c
.c
== ':' || c
.c
== '\\' ||
355 c
.c
== '[' || c
.c
== ']')
361 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
, NAME_ADDRSPEC_ERR_CHAR
, c
.u
);
366 agp
= _idna_apply(agp
);
370 return ((agp
->ag_n_flags
& NAME_ADDRSPEC_INVALID
) != 0);
374 gethfield(FILE *f
, char **linebuf
, size_t *linesize
, int rem
, char **colon
)
376 char *line2
= NULL
, *cp
, *cp2
;
377 size_t line2size
= 0;
381 if (*linebuf
== NULL
)
382 *linebuf
= srealloc(*linebuf
, *linesize
= 1);
389 if ((c
= readline_restart(f
, linebuf
, linesize
, 0)) <= 0) {
393 for (cp
= *linebuf
; fieldnamechar(*cp
); ++cp
)
396 while (blankchar(*cp
))
398 if (*cp
!= ':' || cp
== *linebuf
)
401 /* I guess we got a headline. Handle wraparound */
406 while (PTRCMP(--cp
, >=, *linebuf
) && blankchar(*cp
))
411 if (PTRCMP(cp
- 8, >=, *linebuf
) && cp
[-1] == '=' && cp
[-2] == '?')
413 ungetc(c
= getc(f
), f
);
416 c
= readline_restart(f
, &line2
, &line2size
, 0);
420 for (cp2
= line2
; blankchar(*cp2
); ++cp2
)
422 c
-= (int)PTR2SIZE(cp2
- line2
);
423 if (cp2
[0] == '=' && cp2
[1] == '?' && c
> 8)
425 if (PTRCMP(cp
+ c
, >=, *linebuf
+ *linesize
- 2)) {
426 size_t diff
= PTR2SIZE(cp
- *linebuf
),
427 colondiff
= PTR2SIZE(*colon
- *linebuf
);
428 *linebuf
= srealloc(*linebuf
, *linesize
+= c
+ 2);
429 cp
= &(*linebuf
)[diff
];
430 *colon
= &(*linebuf
)[colondiff
];
448 msgidnextc(char const **cp
, int *status
)
455 assert(status
!= NULL
);
473 *cp
= skip_comment(&(*cp
)[1]);
490 c
= (*status
& 02) ? lowerconv(c
) : c
;
500 charcount(char *str
, int c
)
506 for (i
= 0, cp
= str
; *cp
; ++cp
)
514 nexttoken(char const *cp
)
533 } while (nesting
> 0 && *cp
!= '\0'); /* XXX error? */
534 } else if (blankchar(*cp
) || *cp
== ',')
544 myaddrs(struct header
*hp
)
550 if (hp
!= NULL
&& (np
= hp
->h_from
) != NULL
) {
551 if ((rv
= np
->n_fullname
) != NULL
)
553 if ((rv
= np
->n_name
) != NULL
)
557 if ((rv
= ok_vlook(from
)) != NULL
)
560 /* When invoking *sendmail* directly, it's its task to generate an otherwise
561 * undeterminable From: address. However, if the user sets *hostname*,
562 * accept his desire */
563 if (ok_vlook(smtp
) != NULL
|| ok_vlook(hostname
) != NULL
) {
564 char *hn
= nodename(1);
565 size_t sz
= strlen(myname
) + strlen(hn
) + 1 +1;
567 sstpcpy(sstpcpy(sstpcpy(rv
, myname
), "@"), hn
);
575 myorigin(struct header
*hp
)
577 char const *rv
= NULL
, *ccp
;
581 if ((ccp
= myaddrs(hp
)) != NULL
&&
582 (np
= lextract(ccp
, GEXTRA
| GFULL
)) != NULL
)
583 rv
= (np
->n_flink
!= NULL
) ? ok_vlook(sender
) : ccp
;
589 is_head(char const *linebuf
, size_t linelen
) /* XXX verbose WARN */
591 char date
[FROM_DATEBUF
];
595 rv
= ((linelen
<= 5 || strncmp(linebuf
, "From ", 5) != 0 ||
596 !extract_date_from_from_(linebuf
, linelen
, date
) ||
597 !_is_date(date
)) ? 0 : 1);
603 extract_date_from_from_(char const *line
, size_t linelen
,
604 char datebuf
[FROM_DATEBUF
])
607 char const *cp
= line
;
611 cp
= _from__skipword(cp
);
615 cp
= _from__skipword(cp
);
618 if (cp
[0] == 't' && cp
[1] == 't' && cp
[2] == 'y') {
619 cp
= _from__skipword(cp
);
624 linelen
-= PTR2SIZE(cp
- line
);
625 if (linelen
< _DATE_MINLEN
)
627 if (cp
[linelen
- 1] == '\n') {
629 /* (Rather IMAP/POP3 only) */
630 if (cp
[linelen
- 1] == '\r')
632 if (linelen
< _DATE_MINLEN
)
635 if (linelen
>= FROM_DATEBUF
)
640 memcpy(datebuf
, cp
, linelen
);
641 datebuf
[linelen
] = '\0';
645 cp
= _("<Unknown date>");
646 linelen
= strlen(cp
);
647 if (linelen
>= FROM_DATEBUF
)
648 linelen
= FROM_DATEBUF
;
653 extract_header(FILE *fp
, struct header
*hp
) /* XXX no header occur-cnt check */
655 struct header nh
, *hq
= &nh
;
656 char *linebuf
= NULL
/* TODO line pool */, *colon
;
657 size_t linesize
= 0, seenfields
= 0;
659 char const *val
, *cp
;
662 memset(hq
, 0, sizeof *hq
);
663 for (lc
= 0; readline_restart(fp
, &linebuf
, &linesize
, 0) > 0; ++lc
)
666 /* TODO yippieia, cat(check(lextract)) :-) */
668 while ((lc
= gethfield(fp
, &linebuf
, &linesize
, lc
, &colon
)) >= 0) {
669 if ((val
= thisfield(linebuf
, "to")) != NULL
) {
671 hq
->h_to
= cat(hq
->h_to
, checkaddrs(lextract(val
, GTO
| GFULL
)));
672 } else if ((val
= thisfield(linebuf
, "cc")) != NULL
) {
674 hq
->h_cc
= cat(hq
->h_cc
, checkaddrs(lextract(val
, GCC
| GFULL
)));
675 } else if ((val
= thisfield(linebuf
, "bcc")) != NULL
) {
677 hq
->h_bcc
= cat(hq
->h_bcc
, checkaddrs(lextract(val
, GBCC
| GFULL
)));
678 } else if ((val
= thisfield(linebuf
, "from")) != NULL
) {
680 hq
->h_from
= cat(hq
->h_from
,
681 checkaddrs(lextract(val
, GEXTRA
| GFULL
)));
682 } else if ((val
= thisfield(linebuf
, "reply-to")) != NULL
) {
684 hq
->h_replyto
= cat(hq
->h_replyto
,
685 checkaddrs(lextract(val
, GEXTRA
| GFULL
)));
686 } else if ((val
= thisfield(linebuf
, "sender")) != NULL
) {
688 hq
->h_sender
= cat(hq
->h_sender
,
689 checkaddrs(lextract(val
, GEXTRA
| GFULL
)));
690 } else if ((val
= thisfield(linebuf
, "organization")) != NULL
) {
692 for (cp
= val
; blankchar(*cp
); ++cp
)
694 hq
->h_organization
= (hq
->h_organization
!= NULL
)
695 ? save2str(hq
->h_organization
, cp
) : savestr(cp
);
696 } else if ((val
= thisfield(linebuf
, "subject")) != NULL
||
697 (val
= thisfield(linebuf
, "subj")) != NULL
) {
699 for (cp
= val
; blankchar(*cp
); ++cp
)
701 hq
->h_subject
= (hq
->h_subject
!= NULL
)
702 ? save2str(hq
->h_subject
, cp
) : savestr(cp
);
704 fprintf(stderr
, _("Ignoring header field \"%s\"\n"), linebuf
);
707 /* In case the blank line after the header has been edited out. Otherwise,
708 * fetch the header separator */
709 if (linebuf
!= NULL
) {
710 if (linebuf
[0] != '\0') {
711 for (cp
= linebuf
; *(++cp
) != '\0';)
713 fseek(fp
, (long)-PTR2SIZE(1 + cp
- linebuf
), SEEK_CUR
);
715 if ((c
= getc(fp
)) != '\n' && c
!= EOF
)
720 if (seenfields
> 0) {
723 hp
->h_bcc
= hq
->h_bcc
;
724 hp
->h_from
= hq
->h_from
;
725 hp
->h_replyto
= hq
->h_replyto
;
726 hp
->h_sender
= hq
->h_sender
;
727 hp
->h_organization
= hq
->h_organization
;
728 hp
->h_subject
= hq
->h_subject
;
730 fprintf(stderr
, _("Restoring deleted header lines\n"));
738 hfield_mult(char const *field
, struct message
*mp
, int mult
)
742 size_t linesize
= 0; /* TODO line pool */
743 char *linebuf
= NULL
, *colon
, *oldhfield
= NULL
;
747 if ((ibuf
= setinput(&mb
, mp
, NEED_HEADER
)) == NULL
)
749 if ((lc
= mp
->m_lines
- 1) < 0)
752 if ((mp
->m_flag
& MNOFROM
) == 0 &&
753 readline_restart(ibuf
, &linebuf
, &linesize
, 0) < 0)
756 if ((lc
= gethfield(ibuf
, &linebuf
, &linesize
, lc
, &colon
)) < 0)
758 if ((hfield
= thisfield(linebuf
, field
)) != NULL
) {
759 oldhfield
= save2str(hfield
, oldhfield
);
773 thisfield(char const *linebuf
, char const *field
)
775 char const *rv
= NULL
;
778 while (lowerconv(*linebuf
) == lowerconv(*field
)) {
785 while (blankchar(*linebuf
))
787 if (*linebuf
++ != ':')
790 while (blankchar(*linebuf
)) /* TODO header parser.. strip trailing WS?!? */
799 nameof(struct message
*mp
, int reptype
)
804 cp
= skin(name1(mp
, reptype
));
805 if (reptype
!= 0 || charcount(cp
, '!') < 2)
807 cp2
= strrchr(cp
, '!');
809 while (cp2
> cp
&& *cp2
!= '!')
819 skip_comment(char const *cp
)
824 for (nesting
= 1; nesting
> 0 && *cp
; ++cp
) {
843 routeaddr(char const *name
)
845 char const *np
, *rp
= NULL
;
848 for (np
= name
; *np
; np
++) {
851 np
= skip_comment(np
+ 1) - 1;
857 if (*np
== '\\' && np
[1])
875 is_addr_invalid(struct name
*np
, int putmsg
)
877 char cbuf
[sizeof "'\\U12340'"], *name
;
880 char const *fmt
, *cs
;
888 if (!(f
& NAME_ADDRSPEC_INVALID
) || !putmsg
|| (f
& NAME_ADDRSPEC_ERR_EMPTY
))
891 if (f
& NAME_ADDRSPEC_ERR_IDNA
)
892 cs
= _("Invalid domain name: \"%s\", character %s\n"),
895 else if (f
& NAME_ADDRSPEC_ERR_ATSEQ
)
896 cs
= _("\"%s\" contains invalid %s sequence\n");
898 cs
= _("\"%s\" contains invalid character %s\n");
900 c
= NAME_ADDRSPEC_ERR_GETWC(f
);
901 if (ok8bit
&& c
>= 040 && c
<= 0177)
902 snprintf(cbuf
, sizeof cbuf
, "'%c'", c
);
904 snprintf(cbuf
, sizeof cbuf
, fmt
, c
);
906 fprintf(stderr
, cs
, name
, cbuf
);
909 return ((f
& NAME_ADDRSPEC_INVALID
) != 0);
913 skin(char const *name
)
920 addrspec_with_guts(1, name
, &ag
);
922 if (!(ag
.ag_n_flags
& NAME_NAME_SALLOC
))
923 ret
= savestrbuf(ret
, ag
.ag_slen
);
929 /* TODO addrspec_with_guts: RFC 5322 */
931 addrspec_with_guts(int doskin
, char const *name
, struct addrguts
*agp
)
934 char *cp2
, *bufend
, *nbuf
, c
, gotlt
, gotaddr
, lastsp
;
938 memset(agp
, 0, sizeof *agp
);
940 if ((agp
->ag_input
= name
) == NULL
|| (agp
->ag_ilen
= strlen(name
)) == 0) {
941 agp
->ag_skinned
= UNCONST(""); /* ok: NAME_SALLOC is not set */
943 agp
->ag_n_flags
|= NAME_ADDRSPEC_CHECKED
;
944 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
, NAME_ADDRSPEC_ERR_EMPTY
, 0);
948 if (!doskin
|| !anyof(name
, "(< ")) {
949 /*agp->ag_iaddr_start = 0;*/
950 agp
->ag_iaddr_aend
= agp
->ag_ilen
;
951 agp
->ag_skinned
= UNCONST(name
); /* (NAME_SALLOC not set) */
952 agp
->ag_slen
= agp
->ag_ilen
;
953 agp
->ag_n_flags
= NAME_SKINNED
;
957 /* Something makes us think we have to perform the skin operation */
958 nbuf
= ac_alloc(agp
->ag_ilen
+ 1);
959 /*agp->ag_iaddr_start = 0;*/
961 gotlt
= gotaddr
= lastsp
= 0;
963 for (cp
= name
++; (c
= *cp
++) != '\0'; ) {
966 cp
= skip_comment(cp
);
970 /* Start of a "quoted-string".
971 * Copy it in its entirety */
972 /* XXX RFC: quotes are "semantically invisible"
973 * XXX But it was explicitly added (Changelog.Heirloom,
974 * XXX [9.23] released 11/15/00, "Do not remove quotes
975 * XXX when skinning names"? No more info.. */
977 while ((c
= *cp
) != '\0') { /* TODO improve */
985 else if ((c
= *cp
) != '\0') {
996 agp
->ag_iaddr_aend
= PTR2SIZE(cp
- name
);
998 if (cp
[0] == 'a' && cp
[1] == 't' && blankchar(cp
[2]))
999 cp
+= 3, *cp2
++ = '@';
1000 else if (cp
[0] == '@' && blankchar(cp
[1]))
1001 cp
+= 2, *cp2
++ = '@';
1006 agp
->ag_iaddr_start
= PTR2SIZE(cp
- (name
- 1));
1008 gotlt
= gotaddr
= 1;
1013 /* (_addrspec_check() verifies these later!) */
1014 agp
->ag_iaddr_aend
= PTR2SIZE(cp
- name
);
1016 while ((c
= *cp
) != '\0' && c
!= ',') {
1019 cp
= skip_comment(cp
);
1021 while ((c
= *cp
) != '\0') {
1025 if (c
== '\\' && *cp
!= '\0')
1043 for (; blankchar(*cp
); ++cp
)
1048 } else if (!gotaddr
) {
1050 agp
->ag_iaddr_start
= PTR2SIZE(cp
- name
);
1054 agp
->ag_slen
= PTR2SIZE(cp2
- nbuf
);
1055 if (agp
->ag_iaddr_aend
== 0)
1056 agp
->ag_iaddr_aend
= agp
->ag_ilen
;
1058 agp
->ag_skinned
= savestrbuf(nbuf
, agp
->ag_slen
);
1060 agp
->ag_n_flags
= NAME_NAME_SALLOC
| NAME_SKINNED
;
1062 rv
= _addrspec_check(doskin
, agp
);
1069 realname(char const *name
)
1071 char const *cp
, *cq
, *cstart
= NULL
, *cend
= NULL
;
1074 int quoted
, good
, nogood
;
1077 if ((cp
= UNCONST(name
)) == NULL
)
1079 for (; *cp
!= '\0'; ++cp
) {
1082 if (cstart
!= NULL
) {
1083 /* More than one comment in address, doesn't make sense to display
1084 * it without context. Return the entire field */
1085 cp
= mime_fromaddr(name
);
1089 cp
= skip_comment(cp
);
1092 cend
= cstart
= NULL
;
1098 if (*cp
== '\\' && cp
[1])
1109 /* More than one address. Just use the first one */
1115 if (cstart
== NULL
) {
1117 /* If name contains only a route-addr, the surrounding angle brackets
1118 * don't serve any useful purpose when displaying, so remove */
1119 cp
= prstr(skin(name
));
1121 cp
= mime_fromaddr(name
);
1125 /* Strip quotes. Note that quotes that appear within a MIME encoded word are
1126 * not stripped. The idea is to strip only syntactical relevant things (but
1127 * this is not necessarily the most sensible way in practice) */
1128 rp
= rname
= ac_alloc(PTR2SIZE(cend
- cstart
+1));
1130 for (cp
= cstart
; cp
< cend
; ++cp
) {
1131 if (*cp
== '(' && !quoted
) {
1132 cq
= skip_comment(++cp
);
1133 if (PTRCMP(--cq
, >, cend
))
1136 if (*cp
== '\\' && PTRCMP(cp
+ 1, <, cq
))
1140 } else if (*cp
== '\\' && PTRCMP(cp
+ 1, <, cend
))
1142 else if (*cp
== '"') {
1151 mime_fromhdr(&in
, &out
, TD_ISPR
| TD_ICONV
);
1153 rname
= savestr(out
.s
);
1156 while (blankchar(*rname
))
1158 for (rp
= rname
; *rp
!= '\0'; ++rp
)
1160 while (PTRCMP(--rp
, >=, rname
) && blankchar(*rp
))
1163 cp
= mime_fromaddr(name
);
1167 /* mime_fromhdr() has converted all nonprintable characters to question
1168 * marks now. These and blanks are considered uninteresting; if the
1169 * displayed part of the real name contains more than 25% of them, it is
1170 * probably better to display the plain email address instead */
1173 for (rp
= rname
; *rp
!= '\0' && PTRCMP(rp
, <, rname
+ 20); ++rp
)
1174 if (*rp
== '?' || blankchar(*rp
))
1178 cp
= (good
* 3 < nogood
) ? prstr(skin(name
)) : rname
;
1185 name1(struct message
*mp
, int reptype
)
1187 char *namebuf
, *cp
, *cp2
, *linebuf
= NULL
/* TODO line pool */;
1188 size_t namesize
, linesize
= 0;
1193 if ((cp
= hfield1("from", mp
)) != NULL
&& *cp
!= '\0')
1195 if (reptype
== 0 && (cp
= hfield1("sender", mp
)) != NULL
&& *cp
!= '\0')
1198 namebuf
= smalloc(namesize
= 1);
1200 if (mp
->m_flag
& MNOFROM
)
1202 if ((ibuf
= setinput(&mb
, mp
, NEED_HEADER
)) == NULL
)
1204 if (readline_restart(ibuf
, &linebuf
, &linesize
, 0) < 0)
1208 if (namesize
<= linesize
)
1209 namebuf
= srealloc(namebuf
, namesize
= linesize
+1);
1210 for (cp
= linebuf
; *cp
!= '\0' && *cp
!= ' '; ++cp
)
1212 for (; blankchar(*cp
); ++cp
)
1214 for (cp2
= namebuf
+ strlen(namebuf
);
1215 *cp
&& !blankchar(*cp
) && PTRCMP(cp2
, <, namebuf
+ namesize
-1);)
1219 if (readline_restart(ibuf
, &linebuf
, &linesize
, 0) < 0)
1221 if ((cp
= strchr(linebuf
, 'F')) == NULL
)
1223 if (strncmp(cp
, "From", 4)) /* XXX is_head? */
1225 if (namesize
<= linesize
)
1226 namebuf
= srealloc(namebuf
, namesize
= linesize
+ 1);
1228 while ((cp
= strchr(cp
, 'r')) != NULL
) {
1229 if (!strncmp(cp
, "remote", 6)) {
1230 if ((cp
= strchr(cp
, 'f')) == NULL
)
1232 if (strncmp(cp
, "from", 4) != 0)
1234 if ((cp
= strchr(cp
, ' ')) == NULL
)
1238 strncpy(namebuf
, cp
, namesize
);
1241 cp2
= strrchr(namebuf
, '!') + 1;
1242 strncpy(cp2
, cp
, PTR2SIZE(namebuf
+ namesize
- cp2
));
1244 namebuf
[namesize
- 2] = '!';
1245 namebuf
[namesize
- 1] = '\0';
1251 if (*namebuf
!= '\0' || ((cp
= hfield1("return-path", mp
))) == NULL
||
1253 cp
= savestr(namebuf
);
1255 if (linebuf
!= NULL
)
1264 subject_re_trim(char *s
) /* XXX add bool_t mime_decode argument?! */
1269 } const *pp
, ignored
[] = { /* Update *reply-strings* manual upon change! */
1271 { 3, "aw:" }, { 5, "antw:" }, /* de */
1274 char *re_st
, *re_st_x
;
1278 if ((re_st
= re_st_x
= ok_vlook(reply_strings
)) != NULL
&&
1279 (re_l
= strlen(re_st_x
)) > 0) {
1280 re_st
= ac_alloc(++re_l
* 2);
1281 memcpy(re_st
, re_st_x
, re_l
);
1285 while (*s
!= '\0') {
1286 while (spacechar(*s
))
1289 /* TODO While it is maybe ok not to MIME decode these (for purpose), we
1290 * TODO should skip =?..?= at the beginning? */
1291 for (pp
= ignored
; pp
->len
> 0; ++pp
)
1292 if (is_asccaseprefix(pp
->dat
, s
)) {
1297 if (re_st
!= NULL
) {
1300 memcpy(re_st_x
= re_st
+ re_l
, re_st
, re_l
);
1301 while ((cp
= n_strsep(&re_st_x
, ',', TRU1
)) != NULL
)
1302 if (is_asccaseprefix(cp
, s
)) {
1317 msgidcmp(char const *s1
, char const *s2
)
1319 int q1
= 0, q2
= 0, c1
, c2
;
1323 c1
= msgidnextc(&s1
, &q1
);
1324 c2
= msgidnextc(&s2
, &q2
);
1333 is_ign(char const *field
, size_t fieldlen
, struct ignoretab ignoret
[2])
1340 if (ignoret
== NULL
)
1343 if (ignoret
== allignore
)
1346 /* Lowercase it so that "Status" and "status" will hash to the same place */
1347 realfld
= ac_alloc(fieldlen
+1);
1348 i_strcpy(realfld
, field
, fieldlen
+1);
1349 if (ignoret
[1].i_count
> 0)
1350 rv
= !member(realfld
, ignoret
+ 1);
1352 rv
= member(realfld
, ignoret
);
1360 member(char const *realfield
, struct ignoretab
*table
)
1366 for (igp
= table
->i_head
[hash(realfield
)]; igp
!= 0; igp
= igp
->i_link
)
1367 if (*igp
->i_field
== *realfield
&& !strcmp(igp
->i_field
, realfield
)) {
1376 fakefrom(struct message
*mp
)
1381 if (((name
= skin(hfield1("return-path", mp
))) == NULL
|| *name
== '\0' ) &&
1382 ((name
= skin(hfield1("from", mp
))) == NULL
|| *name
== '\0'))
1383 /* XXX MAILER-DAEMON is what an old MBOX manual page says.
1384 * RFC 4155 however requires a RFC 5322 (2822) conforming
1385 * "addr-spec", but we simply can't provide that */
1386 name
= "MAILER-DAEMON";
1398 for (cq
= cp
; *cq
!= '\0' && *cq
!= '\n'; ++cq
)
1406 #if defined HAVE_IMAP_SEARCH || defined HAVE_IMAP
1408 unixtime(char const *fromline
)
1413 int i
, year
, month
, day
, hour
, minute
, second
, tzdiff
;
1417 for (fp
= fromline
; *fp
!= '\0' && *fp
!= '\n'; ++fp
)
1420 if (PTR2SIZE(fp
- fromline
) < 7)
1425 if (!strncmp(fp
+ 4, month_names
[i
], 3))
1427 if (month_names
[++i
][0] == '\0')
1433 day
= strtol(fp
+ 8, &xp
, 10);
1434 if (*xp
!= ' ' || xp
!= fp
+ 10)
1436 hour
= strtol(fp
+ 11, &xp
, 10);
1437 if (*xp
!= ':' || xp
!= fp
+ 13)
1439 minute
= strtol(fp
+ 14, &xp
, 10);
1440 if (*xp
!= ':' || xp
!= fp
+ 16)
1442 second
= strtol(fp
+ 17, &xp
, 10);
1443 if (*xp
!= ' ' || xp
!= fp
+ 19)
1445 year
= strtol(fp
+ 20, &xp
, 10);
1448 if ((t
= combinetime(year
, month
, day
, hour
, minute
, second
)) == (time_t)-1)
1450 tzdiff
= t
- mktime(gmtime(&t
));
1451 tmptr
= localtime(&t
);
1452 if (tmptr
->tm_isdst
> 0)
1462 #endif /* HAVE_IMAP_SEARCH || defined HAVE_IMAP */
1465 rfctime(char const *date
)
1467 char const *cp
= date
;
1470 int i
, year
, month
, day
, hour
, minute
, second
;
1473 if ((cp
= nexttoken(cp
)) == NULL
)
1475 if (alphachar(cp
[0]) && alphachar(cp
[1]) && alphachar(cp
[2]) &&
1477 if ((cp
= nexttoken(&cp
[4])) == NULL
)
1480 day
= strtol(cp
, &x
, 10); /* XXX strtol */
1481 if ((cp
= nexttoken(x
)) == NULL
)
1484 if (!strncmp(cp
, month_names
[i
], 3))
1486 if (month_names
[++i
][0] == '\0')
1490 if ((cp
= nexttoken(&cp
[3])) == NULL
)
1493 * Where a two or three digit year occurs in a date, the year is to be
1494 * interpreted as follows: If a two digit year is encountered whose
1495 * value is between 00 and 49, the year is interpreted by adding 2000,
1496 * ending up with a value between 2000 and 2049. If a two digit year
1497 * is encountered with a value between 50 and 99, or any three digit
1498 * year is encountered, the year is interpreted by adding 1900 */
1499 year
= strtol(cp
, &x
, 10); /* XXX strtol */
1500 i
= (int)PTR2SIZE(x
- cp
);
1501 if (i
== 2 && year
>= 0 && year
<= 49)
1503 else if (i
== 3 || (i
== 2 && year
>= 50 && year
<= 99))
1505 if ((cp
= nexttoken(x
)) == NULL
)
1507 hour
= strtol(cp
, &x
, 10); /* XXX strtol */
1511 minute
= strtol(cp
, &x
, 10);
1514 second
= strtol(cp
, &x
, 10);
1517 if ((t
= combinetime(year
, month
, day
, hour
, minute
, second
)) == (time_t)-1)
1519 if ((cp
= nexttoken(x
)) != NULL
) {
1531 if (digitchar(cp
[0]) && digitchar(cp
[1]) && digitchar(cp
[2]) &&
1536 t
+= strtol(buf
, NULL
, 10) * sign
* 3600;/*XXX strtrol*/
1539 t
+= strtol(buf
, NULL
, 10) * sign
* 60; /* XXX strtol*/
1541 /* TODO WE DO NOT YET PARSE (OBSOLETE) ZONE NAMES
1542 * TODO once again, Christos Zoulas and NetBSD Mail have done
1543 * TODO a really good job already, but using strptime(3), which
1544 * TODO is not portable. Nonetheless, WE must improve, not
1545 * TODO at last because we simply ignore obsolete timezones!!
1546 * TODO See RFC 5322, 4.3! */
1556 #define is_leapyear(Y) ((((Y) % 100 ? (Y) : (Y) / 100) & 3) == 0)
1559 combinetime(int year
, int month
, int day
, int hour
, int minute
, int second
)
1564 if (second
< 0 || minute
< 0 || hour
< 0 || day
< 1) {
1569 t
= second
+ minute
* 60 + hour
* 3600 + (day
- 1) * 86400;
1573 t
+= 86400 * (is_leapyear(year
) ? 29 : 28);
1593 t
+= (year
- 70) * 31536000 + ((year
- 69) / 4) * 86400 -
1594 ((year
- 1) / 100) * 86400 + ((year
+ 299) / 400) * 86400;
1601 substdate(struct message
*m
)
1606 /* Determine the date to print in faked 'From ' lines. This is traditionally
1607 * the date the message was written to the mail file. Try to determine this
1608 * using RFC message header fields, or fall back to current time */
1609 if ((cp
= hfield1("received", m
)) != NULL
) {
1610 while ((cp
= nexttoken(cp
)) != NULL
&& *cp
!= ';') {
1613 while (alnumchar(*cp
));
1616 m
->m_time
= rfctime(cp
);
1618 if (m
->m_time
== 0 || m
->m_time
> time_current
.tc_time
) {
1619 if ((cp
= hfield1("date", m
)) != NULL
)
1620 m
->m_time
= rfctime(cp
);
1622 if (m
->m_time
== 0 || m
->m_time
> time_current
.tc_time
)
1623 m
->m_time
= time_current
.tc_time
;
1627 FL
struct name
const *
1628 check_from_and_sender(struct name
const *fromfield
,
1629 struct name
const *senderfield
)
1631 struct name
const *rv
= NULL
;
1634 if (senderfield
!= NULL
) {
1635 if (senderfield
->n_flink
!= NULL
) {
1637 "The Sender: field may contain only one address.\n"));
1643 if (fromfield
!= NULL
) {
1644 if (fromfield
->n_flink
!= NULL
&& senderfield
== NULL
) {
1645 fprintf(stderr
, _("A Sender: field is required with multiple "
1646 "addresses in From: field.\n"));
1654 rv
= (struct name
*)0x1;
1662 getsender(struct message
*mp
)
1668 if ((cp
= hfield1("from", mp
)) == NULL
||
1669 (np
= lextract(cp
, GEXTRA
| GSKIN
)) == NULL
)
1672 cp
= (np
->n_flink
!= NULL
) ? skin(hfield1("sender", mp
)) : np
->n_name
;
1679 grab_headers(struct header
*hp
, enum gfield gflags
, int subjfirst
)
1681 /* TODO grab_headers: again, check counts etc. against RFC;
1682 * TODO (now assumes check_from_and_sender() is called afterwards ++ */
1688 comma
= (ok_blook(bsdcompat
) || ok_blook(bsdmsgs
)) ? 0 : GCOMMA
;
1691 hp
->h_to
= grab_names("To: ", hp
->h_to
, comma
, GTO
| GFULL
);
1692 if (subjfirst
&& (gflags
& GSUBJECT
))
1693 hp
->h_subject
= readstr_input("Subject: ", hp
->h_subject
);
1695 hp
->h_cc
= grab_names("Cc: ", hp
->h_cc
, comma
, GCC
| GFULL
);
1697 hp
->h_bcc
= grab_names("Bcc: ", hp
->h_bcc
, comma
, GBCC
| GFULL
);
1699 if (gflags
& GEXTRA
) {
1700 if (hp
->h_from
== NULL
)
1701 hp
->h_from
= lextract(myaddrs(hp
), GEXTRA
| GFULL
);
1702 hp
->h_from
= grab_names("From: ", hp
->h_from
, comma
, GEXTRA
| GFULL
);
1703 if (hp
->h_replyto
== NULL
)
1704 hp
->h_replyto
= lextract(ok_vlook(replyto
), GEXTRA
| GFULL
);
1705 hp
->h_replyto
= grab_names("Reply-To: ", hp
->h_replyto
, comma
,
1707 if (hp
->h_sender
== NULL
)
1708 hp
->h_sender
= extract(ok_vlook(sender
), GEXTRA
| GFULL
);
1709 hp
->h_sender
= grab_names("Sender: ", hp
->h_sender
, comma
,
1711 if (hp
->h_organization
== NULL
)
1712 hp
->h_organization
= ok_vlook(ORGANIZATION
);
1713 hp
->h_organization
= readstr_input("Organization: ", hp
->h_organization
);
1716 if (!subjfirst
&& (gflags
& GSUBJECT
))
1717 hp
->h_subject
= readstr_input("Subject: ", hp
->h_subject
);
1724 header_match(struct message
*mp
, struct search_expr
const *sep
)
1729 size_t linesize
= 0; /* TODO line pool */
1730 char *linebuf
= NULL
, *colon
;
1734 if ((ibuf
= setinput(&mb
, mp
, NEED_HEADER
)) == NULL
)
1736 if ((lc
= mp
->m_lines
- 1) < 0)
1739 if ((mp
->m_flag
& MNOFROM
) == 0 &&
1740 readline_restart(ibuf
, &linebuf
, &linesize
, 0) < 0)
1743 if (gethfield(ibuf
, &linebuf
, &linesize
, lc
, &colon
) <= 0)
1745 if (blankchar(*++colon
))
1747 in
.l
= strlen(in
.s
= colon
);
1748 mime_fromhdr(&in
, &out
, TD_ICONV
);
1750 if (sep
->ss_sexpr
== NULL
)
1751 rv
= (regexec(&sep
->ss_reexpr
, out
.s
, 0,NULL
, 0) != REG_NOMATCH
);
1754 rv
= substr(out
.s
, sep
->ss_sexpr
);
1761 if (linebuf
!= NULL
)