2 * S-nail - a mail user agent derived from Berkeley Mail.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 Steffen "Daode" Nurpmeso.
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
45 * Mail -- a mail program
47 * Routines for processing and detecting headlines.
50 static char * copyin(char *src
, char **space
);
51 static char * nextword(char *wp
, char *wbuf
);
52 static int gethfield(FILE *f
, char **linebuf
, size_t *linesize
, int rem
,
54 static int addrspec_check(int doskin
, struct addrguts
*agp
);
55 static int msgidnextc(const char **cp
, int *status
);
56 static int charcount(char *str
, int c
);
59 * See if the passed line buffer is a mail header.
60 * Return true if yes. POSIX.2 leaves the content
61 * following 'From ' unspecified, so don't care about
66 is_head(char *linebuf
, size_t linelen
)
72 if (*cp
++ != 'F' || *cp
++ != 'r' || *cp
++ != 'o' || *cp
++ != 'm' ||
79 * Split a headline into its useful components.
80 * Copy the line into dynamic string space, then set
81 * pointers into the copied line in the passed headline
82 * structure. Actually, it scans.
85 parse(char *line
, size_t linelen
, struct headline
*hl
, char *pbuf
)
96 word
= ac_alloc(linelen
+ 1);
98 * Skip over "From" first.
100 cp
= nextword(cp
, word
);
101 cp
= nextword(cp
, word
);
103 hl
->l_from
= copyin(word
, &sp
);
104 if (cp
!= NULL
&& cp
[0] == 't' && cp
[1] == 't' && cp
[2] == 'y') {
105 cp
= nextword(cp
, word
);
106 hl
->l_tty
= copyin(word
, &sp
);
109 hl
->l_date
= copyin(cp
, &sp
);
111 hl
->l_date
= catgets(catd
, CATSET
, 213, "<Unknown date>");
116 * Copy the string on the left into the string on the right
117 * and bump the right (reference) string pointer by the length.
118 * Thus, dynamically allocate space in the right string, copying
119 * the left string into it.
122 copyin(char *src
, char **space
)
128 while ((*cp
++ = *src
++) != '\0')
135 static int cmatch(char *, char *);
137 * Test to see if the passed string is a ctime(3) generated
138 * date string as documented in the manual. The template
139 * below is used as the criterion of correctness.
140 * Also, we check for a possible trailing time zone using
141 * the tmztype template.
145 * 'A' An upper case char
146 * 'a' A lower case char
149 * 'O' An optional digit or space
154 static char *tmztype
[] = {
155 "Aaa Aaa O0 00:00:00 0000",
156 "Aaa Aaa O0 00:00 0000",
157 "Aaa Aaa O0 00:00:00 AAA 0000",
158 "Aaa Aaa O0 00:00 AAA 0000",
160 * Sommer time, e.g. MET DST
162 "Aaa Aaa O0 00:00:00 AAA AAA 0000",
163 "Aaa Aaa O0 00:00 AAA AAA 0000",
165 * time zone offset, e.g.
166 * +0200 or +0200 MET or +0200 MET DST
168 "Aaa Aaa O0 00:00:00 +0000 0000",
169 "Aaa Aaa O0 00:00 +0000 0000",
170 "Aaa Aaa O0 00:00:00 +0000 AAA 0000",
171 "Aaa Aaa O0 00:00 +0000 AAA 0000",
172 "Aaa Aaa O0 00:00:00 +0000 AAA AAA 0000",
173 "Aaa Aaa O0 00:00 +0000 AAA AAA 0000",
175 * time zone offset without time zone specification (pine)
177 "Aaa Aaa O0 00:00:00 0000 +0000",
184 int ret
= 0, form
= 0;
186 while (tmztype
[form
]) {
187 if ( (ret
= cmatch(date
, tmztype
[form
])) == 1 )
196 * Match the given string (cp) against the given template (tp).
197 * Return 1 if they match, 0 if they don't
200 cmatch(char *cp
, char *tp
)
207 if (c
= *cp
++, !lowerchar(c
))
211 if (c
= *cp
++, !upperchar(c
))
219 if (c
= *cp
++, !digitchar(c
))
223 if (c
= *cp
, c
!= ' ' && !digitchar(c
))
232 if (*cp
!= '+' && *cp
!= '-')
248 * Collect a liberal (space, tab delimited) word into the word buffer
249 * passed. Also, return a pointer to the next word following that,
250 * or NULL if none follow.
253 nextword(char *wp
, char *wbuf
)
261 while ((c
= *wp
++) != '\0' && !blankchar(c
)) {
264 while ((c
= *wp
++) != '\0' && c
!= '"')
273 for (; blankchar(c
); c
= *wp
++)
281 extract_header(FILE *fp
, struct header
*hp
) /* XXX no header occur-cnt check */
283 char *linebuf
= NULL
;
286 char *colon
, *cp
, *value
;
288 struct header
*hq
= &nh
;
291 memset(hq
, 0, sizeof *hq
);
292 for (lc
= 0; readline(fp
, &linebuf
, &linesize
) > 0; lc
++);
294 while ((lc
= gethfield(fp
, &linebuf
, &linesize
, lc
, &colon
)) >= 0) {
295 if ((value
= thisfield(linebuf
, "to")) != NULL
) {
297 hq
->h_to
= cat(hq
->h_to
, checkaddrs(
298 lextract(value
, GTO
|GFULL
)));
299 } else if ((value
= thisfield(linebuf
, "cc")) != NULL
) {
301 hq
->h_cc
= cat(hq
->h_cc
, checkaddrs(
302 lextract(value
, GCC
|GFULL
)));
303 } else if ((value
= thisfield(linebuf
, "bcc")) != NULL
) {
305 hq
->h_bcc
= cat(hq
->h_bcc
, checkaddrs(
306 lextract(value
, GBCC
|GFULL
)));
307 } else if ((value
= thisfield(linebuf
, "from")) != NULL
) {
309 hq
->h_from
= cat(hq
->h_from
, checkaddrs(
310 sextract(value
, GEXTRA
|GFULL
)));
311 } else if ((value
= thisfield(linebuf
, "reply-to")) != NULL
) {
313 hq
->h_replyto
= cat(hq
->h_replyto
, checkaddrs(
314 sextract(value
, GEXTRA
|GFULL
)));
315 } else if ((value
= thisfield(linebuf
, "sender")) != NULL
) {
317 hq
->h_sender
= cat(hq
->h_sender
, checkaddrs(
318 sextract(value
, GEXTRA
|GFULL
)));
319 } else if ((value
= thisfield(linebuf
,
320 "organization")) != NULL
) {
322 for (cp
= value
; blankchar(*cp
& 0377); cp
++);
323 hq
->h_organization
= hq
->h_organization
?
324 save2str(hq
->h_organization
, cp
) :
326 } else if ((value
= thisfield(linebuf
, "subject")) != NULL
||
327 (value
= thisfield(linebuf
, "subj")) != NULL
) {
329 for (cp
= value
; blankchar(*cp
& 0377); cp
++);
330 hq
->h_subject
= hq
->h_subject
?
331 save2str(hq
->h_subject
, cp
) :
334 fprintf(stderr
, catgets(catd
, CATSET
, 266,
335 "Ignoring header field \"%s\"\n"),
339 * In case the blank line after the header has been edited out.
340 * Otherwise, fetch the header separator.
343 if (linebuf
[0] != '\0') {
344 for (cp
= linebuf
; *(++cp
) != '\0'; );
345 fseek(fp
, (long)-(1 + cp
- linebuf
), SEEK_CUR
);
347 if ((c
= getc(fp
)) != '\n' && c
!= EOF
)
354 hp
->h_bcc
= hq
->h_bcc
;
355 hp
->h_from
= hq
->h_from
;
356 hp
->h_replyto
= hq
->h_replyto
;
357 hp
->h_sender
= hq
->h_sender
;
358 hp
->h_organization
= hq
->h_organization
;
359 hp
->h_subject
= hq
->h_subject
;
361 fprintf(stderr
, catgets(catd
, CATSET
, 267,
362 "Restoring deleted header lines\n"));
368 * Return the desired header line from the passed message
369 * pointer (or NULL if the desired header field is not available).
370 * If mult is zero, return the content of the first matching header
371 * field only, the content of all matching header fields else.
374 hfield_mult(char *field
, struct message
*mp
, int mult
)
377 char *linebuf
= NULL
;
381 char *colon
, *oldhfield
= NULL
;
383 if ((ibuf
= setinput(&mb
, mp
, NEED_HEADER
)) == NULL
)
385 if ((lc
= mp
->m_lines
- 1) < 0)
387 if ((mp
->m_flag
& MNOFROM
) == 0) {
388 if (readline(ibuf
, &linebuf
, &linesize
) < 0) {
395 if ((lc
= gethfield(ibuf
, &linebuf
, &linesize
, lc
, &colon
))
401 if ((hfield
= thisfield(linebuf
, field
)) != NULL
) {
402 oldhfield
= save2str(hfield
, oldhfield
);
413 * Return the next header field found in the given message.
414 * Return >= 0 if something found, < 0 elsewise.
415 * "colon" is set to point to the colon in the header.
416 * Must deal with \ continuations & other such fraud.
419 gethfield(FILE *f
, char **linebuf
, size_t *linesize
, int rem
, char **colon
)
422 size_t line2size
= 0;
426 if (*linebuf
== NULL
)
427 *linebuf
= srealloc(*linebuf
, *linesize
= 1);
432 if ((c
= readline(f
, linebuf
, linesize
)) <= 0)
434 for (cp
= *linebuf
; fieldnamechar(*cp
& 0377); cp
++);
436 while (blankchar(*cp
& 0377))
438 if (*cp
!= ':' || cp
== *linebuf
)
441 * I guess we got a headline.
442 * Handle wraparounding
448 while (--cp
>= *linebuf
&& blankchar(*cp
& 0377));
452 if (cp
-8 >= *linebuf
&& cp
[-1] == '=' && cp
[-2] == '?')
454 ungetc(c
= getc(f
), f
);
457 if ((c
= readline(f
, &line2
, &line2size
)) < 0)
460 for (cp2
= line2
; blankchar(*cp2
& 0377); cp2
++);
462 if (cp2
[0] == '=' && cp2
[1] == '?' && c
> 8)
464 if (cp
+ c
>= *linebuf
+ *linesize
- 2) {
465 size_t diff
= cp
- *linebuf
;
466 size_t colondiff
= *colon
- *linebuf
;
467 *linebuf
= srealloc(*linebuf
,
469 cp
= &(*linebuf
)[diff
];
470 *colon
= &(*linebuf
)[colondiff
];
486 * Check whether the passed line is a header line of
487 * the desired breed. Return the field body, or 0.
490 thisfield(const char *linebuf
, const char *field
)
492 while (lowerconv(*linebuf
&0377) == lowerconv(*field
&0377)) {
498 while (blankchar(*linebuf
&0377))
500 if (*linebuf
++ != ':')
502 while (blankchar(*linebuf
&0377))
504 return (char *)linebuf
;
508 * Get sender's name from this message. If the message has
509 * a bunch of arpanet stuff in it, we may have to skin the name
510 * before returning it.
513 nameof(struct message
*mp
, int reptype
)
517 cp
= skin(name1(mp
, reptype
));
518 if (reptype
!= 0 || charcount(cp
, '!') < 2)
520 cp2
= strrchr(cp
, '!');
522 while (cp2
> cp
&& *cp2
!= '!')
530 * Start of a "comment".
534 skip_comment(const char *cp
)
538 for (; nesting
> 0 && *cp
; cp
++) {
556 * Return the start of a route-addr (address in angle brackets),
560 routeaddr(const char *name
)
562 const char *np
, *rp
= NULL
;
564 for (np
= name
; *np
; np
++) {
567 np
= skip_comment(&np
[1]) - 1;
573 if (*np
== '\\' && np
[1])
588 * Check if a name's address part contains invalid characters.
591 is_addr_invalid(struct name
*np
, int putmsg
)
593 char *name
= np
->n_name
;
596 if ((f
& NAME_ADDRSPEC_INVALID
) == 0 || ! putmsg
||
597 (f
& NAME_ADDRSPEC_ERR_EMPTY
) != 0)
599 else if (f
& NAME_ADDRSPEC_ERR_ATSEQ
)
600 fprintf(stderr
, tr(142, "%s contains invalid @@ sequence\n"),
603 char ce
[sizeof(1ul)];
604 unsigned char c
= NAME_ADDRSPEC_ERR_GETC(f
);
606 if ((unsigned char)c
>= 040 && (unsigned char)c
<= 0177)
607 ce
[0] = c
, ce
[1] = '\0';
609 snprintf(ce
, sizeof(ce
), "\\%03o", (unsigned int)c
);
610 fprintf(stderr
, tr(143,
611 "%s contains invalid character '%s'\n"),
614 return ((f
& NAME_ADDRSPEC_INVALID
) != 0);
618 * Returned the skinned n_name, use the cached value if available.
619 * Note well that it may *not* create a duplicate.
622 skinned_name(struct name
*np
) /* TODO !HAVE_ASSERTS legacy */
625 assert(np
->n_flags
& NAME_SKINNED
);
628 return ((np
->n_flags
& NAME_SKINNED
) ? np
->n_name
: skin(np
->n_name
));
633 * Skin an arpa net address according to the RFC 822 interpretation
644 (void)addrspec_with_guts(1, name
, &ag
);
645 name
= ag
.ag_skinned
;
646 if ((ag
.ag_n_flags
& NAME_NAME_SALLOC
) == 0)
647 name
= savestrbuf(name
, ag
.ag_slen
);
652 * Classify and check a (possibly skinned) header body according to RFC
653 * *addr-spec* rules; if it (is assumed to has been) skinned it may however be
654 * also a file or a pipe command, so check that first, then.
655 * Otherwise perform content checking and isolate the domain part (for IDNA).
658 addrspec_check(int skinned
, struct addrguts
*agp
)
660 char *addr
, *p
, in_quote
, in_domain
, hadat
;
661 union {char c
; unsigned char u
;} c
;
663 agp
->ag_n_flags
|= NAME_ADDRSPEC_CHECKED
;
664 addr
= agp
->ag_skinned
;
666 if (agp
->ag_iaddr_end
<= agp
->ag_iaddr_start
) {
667 agp
->ag_n_flags
|= NAME_ADDRSPEC_INVALID
|
668 NAME_ADDRSPEC_ERR_EMPTY
;
672 /* If the field is not a recipient, it cannot be a file or a pipe */
673 if (! skinned
) /* XXX || (gfield & (GTO |Â GCC | GBCC)) == 0) */
677 * Excerpt from nail.1:
679 * Recipient address specifications
680 * The rules are: Any name which starts with a `|' character specifies
681 * a pipe, the command string following the `|' is executed and
682 * the message is sent to its standard input; any other name which
683 * contains a `@' character is treated as a mail address; any other
684 * name which starts with a `+' character specifies a folder name; any
685 * other name which contains a `/' character but no `!' or `%'
686 * character before also specifies a folder name; what remains is
687 * treated as a mail address.
690 agp
->ag_n_flags
|= NAME_ADDRSPEC_ISPIPE
;
693 if (memchr(addr
, '@', agp
->ag_slen
) == NULL
) {
696 for (p
= addr
; (c
.c
= *p
); ++p
) {
697 if (c
.c
== '!' || c
.c
== '%')
700 jisfile
: agp
->ag_n_flags
|= NAME_ADDRSPEC_ISFILE
;
707 in_quote
= in_domain
= hadat
= 0;
709 for (p
= addr
; (c
.c
= *p
++) != '\0';) {
711 in_quote
= ! in_quote
;
712 } else if (c
.u
< 040 || c
.u
>= 0177) { /*FIXME IDNA!!in_domin */
715 agp->ag_n_flags |= NAME_ADDRSPEC_IDNA;
718 } else if (in_domain
== 2) {
719 if ((c
.c
== ']' && *p
!= '\0') || c
.c
== '\\' ||
722 } else if (in_quote
&& in_domain
== 0) {
724 } else if (c
.c
== '\\' && *p
!= '\0') {
726 } else if (c
.c
== '@') {
728 agp
->ag_n_flags
|= NAME_ADDRSPEC_INVALID
|
729 NAME_ADDRSPEC_ERR_ATSEQ
|
730 NAME_ADDRSPEC_ERR_SETC('@');
733 agp
->ag_sdom_start
= (size_t)(p
- addr
);
734 in_domain
= (*p
== '[') ? 2 : 1;
736 } else if (c
.c
== '(' || c
.c
== ')' ||
737 c
.c
== '<' || c
.c
== '>' ||
738 c
.c
== ',' || c
.c
== ';' || c
.c
== ':' ||
739 c
.c
== '\\' || c
.c
== '[' || c
.c
== ']')
745 agp
->ag_sdom_end
= (size_t)(--p
- addr
);
747 agp
->ag_n_flags
|= NAME_ADDRSPEC_INVALID
|
748 NAME_ADDRSPEC_ERR_SETC(c
.c
);
750 return ((agp
->ag_n_flags
& NAME_ADDRSPEC_INVALID
) != 0);
754 * TODO addrspec_with_guts(!DOSKIN): 'want to release v13, but the code is evil
755 * TODO in that {,GSKIN,GFULL} are not really enough to handle all names.
756 * TODO We will have to classify *exactly* those fields we really care about,
757 * TODO and simply perform high-bit-set checking only (?) for all the others.
758 * TODO For those we do care for, provide special parsers that classify and
759 * TODO extract the stuff *exactly* (after a short glance i think NetBSD mailx
760 * TODO does this). And *do* see namecache and header object TODO notes.
763 * Skin *name* and extract the *addr-spec* according to RFC 5322. TODO 822:5322
764 * Store the result in .ag_skinned and also fill in those .ag_ fields that have
765 * actually been seen.
766 * Return 0 if something good has been parsed, 1 if fun didn't exactly know how
767 * to deal with the input, or if that was plain invalid.
770 addrspec_with_guts(int doskin
, char *name
, struct addrguts
*agp
)
772 char *cp
, *cp2
, *bufend
, *nbuf
, c
;
775 memset(agp
, 0, sizeof *agp
);
777 if ((agp
->ag_input
= name
) == NULL
|| /* XXX ever? */
778 (agp
->ag_ilen
= strlen(name
)) == 0) {
779 agp
->ag_n_flags
|= NAME_ADDRSPEC_CHECKED
|
780 NAME_ADDRSPEC_INVALID
| NAME_ADDRSPEC_ERR_EMPTY
;
784 if (! doskin
|| (memchr(name
, '(', agp
->ag_ilen
) == NULL
&&
785 memchr(name
, '<', agp
->ag_ilen
) == NULL
&&
786 memchr(name
, ' ', agp
->ag_ilen
) == NULL
)) {
787 /*agp->ag_iaddr_start = 0;*/
788 agp
->ag_iaddr_end
= agp
->ag_ilen
- 1;
789 agp
->ag_skinned
= name
;
790 agp
->ag_slen
= agp
->ag_ilen
;
791 agp
->ag_n_flags
= NAME_SKINNED
;
792 return (addrspec_check(doskin
, agp
));
795 /* Something makes us think we have to perform the skin operation */
796 nbuf
= ac_alloc(agp
->ag_ilen
+ 1);
797 /*agp->ag_iaddr_start = 0;*/
801 for (cp
= name
; (c
= *cp
++) != '\0'; ) {
804 cp
= skip_comment(cp
);
810 * Start of a "quoted-string".
811 * Copy it in its entirety.
812 * XXX RFC: quotes are "semantically invisible"
813 * XXX But it was explicitly added (Changelog.Heirloom,
814 * XXX [9.23] released 11/15/00, "Do not remove quotes
815 * XXX when skinning names"? No more info..
818 while ((c
= *cp
) != '\0') {
826 else if ((c
= *cp
) != '\0') {
835 if (cp
[0] == 'a' && cp
[1] == 't' && cp
[2] == ' ')
836 cp
+= 3, *cp2
++ = '@';
838 if (cp
[0] == '@' && cp
[1] == ' ')
839 cp
+= 2, *cp2
++ = '@';
845 agp
->ag_iaddr_start
= (size_t)(cp
- name
);
853 /* (addrspec_check() verifies these later!) */
854 agp
->ag_iaddr_end
= (size_t)(cp
- 1 - name
);
856 while ((c
= *cp
) != '\0' && c
!= ',') {
859 cp
= skip_comment(cp
);
861 while ((c
= *cp
) != '\0') {
865 if (c
== '\\' && *cp
)
880 if (c
== ',' && ! gotlt
) {
882 for (; *cp
== ' '; ++cp
)
889 agp
->ag_slen
= (size_t)(cp2
- nbuf
);
890 if (agp
->ag_iaddr_end
== 0)
891 agp
->ag_iaddr_end
= agp
->ag_iaddr_start
+ agp
->ag_slen
;
893 agp
->ag_skinned
= cp
= savestrbuf(nbuf
, agp
->ag_slen
);
895 agp
->ag_n_flags
= NAME_NAME_SALLOC
| NAME_SKINNED
;
896 return (addrspec_check(doskin
, agp
));
900 * Fetch the real name from an internet mail address field.
905 char *cstart
= NULL
, *cend
= NULL
, *cp
, *cq
;
908 int quoted
, good
, nogood
;
912 for (cp
= name
; *cp
; cp
++) {
917 * More than one comment in address, doesn't
918 * make sense to display it without context.
919 * Return the entire field,
921 return mime_fromaddr(name
);
923 cp
= skip_comment(cp
);
926 cend
= cstart
= NULL
;
932 if (*cp
== '\\' && cp
[1])
944 * More than one address. Just use the first one.
949 brk
: if (cstart
== NULL
) {
952 * If name contains only a route-addr, the
953 * surrounding angle brackets don't serve any
954 * useful purpose when displaying, so they
957 return prstr(skin(name
));
958 return mime_fromaddr(name
);
960 rp
= rname
= ac_alloc(cend
- cstart
+ 1);
962 * Strip quotes. Note that quotes that appear within a MIME-
963 * encoded word are not stripped. The idea is to strip only
964 * syntactical relevant things (but this is not necessarily
965 * the most sensible way in practice).
968 for (cp
= cstart
; cp
< cend
; cp
++) {
969 if (*cp
== '(' && !quoted
) {
970 cq
= skip_comment(++cp
);
974 if (*cp
== '\\' && &cp
[1] < cq
)
978 } else if (*cp
== '\\' && &cp
[1] < cend
)
980 else if (*cp
== '"') {
989 mime_fromhdr(&in
, &out
, TD_ISPR
|TD_ICONV
);
991 rname
= savestr(out
.s
);
993 while (blankchar(*rname
& 0377))
995 for (rp
= rname
; *rp
; rp
++);
996 while (--rp
>= rname
&& blankchar(*rp
& 0377))
999 return mime_fromaddr(name
);
1001 * mime_fromhdr() has converted all nonprintable characters to
1002 * question marks now. These and blanks are considered uninteresting;
1003 * if the displayed part of the real name contains more than 25% of
1004 * them, it is probably better to display the plain email address
1009 for (rp
= rname
; *rp
&& rp
< &rname
[20]; rp
++)
1010 if (*rp
== '?' || blankchar(*rp
& 0377))
1014 if (good
*3 < nogood
)
1015 return prstr(skin(name
));
1020 * Fetch the sender's name from the passed message.
1022 * 0 -- get sender's name for display purposes
1023 * 1 -- get sender's name for reply
1024 * 2 -- get sender's name for Reply
1027 name1(struct message
*mp
, int reptype
)
1031 char *linebuf
= NULL
;
1032 size_t linesize
= 0;
1037 if ((cp
= hfield1("from", mp
)) != NULL
&& *cp
!= '\0')
1039 if (reptype
== 0 && (cp
= hfield1("sender", mp
)) != NULL
&&
1042 namebuf
= smalloc(namesize
= 1);
1044 if (mp
->m_flag
& MNOFROM
)
1046 if ((ibuf
= setinput(&mb
, mp
, NEED_HEADER
)) == NULL
)
1048 if (readline(ibuf
, &linebuf
, &linesize
) < 0)
1051 if (namesize
<= linesize
)
1052 namebuf
= srealloc(namebuf
, namesize
= linesize
+ 1);
1053 for (cp
= linebuf
; *cp
&& *cp
!= ' '; cp
++)
1055 for (; blankchar(*cp
& 0377); cp
++);
1056 for (cp2
= &namebuf
[strlen(namebuf
)];
1057 *cp
&& !blankchar(*cp
& 0377) && cp2
< namebuf
+ namesize
- 1;)
1060 if (readline(ibuf
, &linebuf
, &linesize
) < 0)
1062 if ((cp
= strchr(linebuf
, 'F')) == NULL
)
1064 if (strncmp(cp
, "From", 4) != 0)
1066 if (namesize
<= linesize
)
1067 namebuf
= srealloc(namebuf
, namesize
= linesize
+ 1);
1068 while ((cp
= strchr(cp
, 'r')) != NULL
) {
1069 if (strncmp(cp
, "remote", 6) == 0) {
1070 if ((cp
= strchr(cp
, 'f')) == NULL
)
1072 if (strncmp(cp
, "from", 4) != 0)
1074 if ((cp
= strchr(cp
, ' ')) == NULL
)
1078 strncpy(namebuf
, cp
, namesize
);
1081 cp2
=strrchr(namebuf
, '!')+1;
1082 strncpy(cp2
, cp
, (namebuf
+namesize
)-cp2
);
1084 namebuf
[namesize
-2]='\0';
1085 strcat(namebuf
, "!");
1091 if (*namebuf
!= '\0' || ((cp
= hfield1("return-path", mp
))) == NULL
||
1093 cp
= savestr(namebuf
);
1101 msgidnextc(const char **cp
, int *status
)
1121 *cp
= skip_comment(&(*cp
)[1]);
1136 c
= *(*cp
)++ & 0377;
1137 return *status
& 02 ? lowerconv(c
) : c
;
1143 msgidcmp(const char *s1
, const char *s2
)
1149 c1
= msgidnextc(&s1
, &q1
);
1150 c2
= msgidnextc(&s2
, &q2
);
1158 * Count the occurances of c in str
1161 charcount(char *str
, int c
)
1166 for (i
= 0, cp
= str
; *cp
; cp
++)
1173 * See if the given header field is supposed to be ignored.
1176 is_ign(char *field
, size_t fieldlen
, struct ignoretab ignore
[2])
1183 if (ignore
== allignore
)
1186 * Lower-case the string, so that "Status" and "status"
1187 * will hash to the same place.
1189 realfld
= ac_alloc(fieldlen
+ 1);
1190 i_strcpy(realfld
, field
, fieldlen
+ 1);
1191 if (ignore
[1].i_count
> 0)
1192 ret
= !member(realfld
, ignore
+ 1);
1194 ret
= member(realfld
, ignore
);
1200 member(char *realfield
, struct ignoretab
*table
)
1204 for (igp
= table
->i_head
[hash(realfield
)]; igp
!= 0; igp
= igp
->i_link
)
1205 if (*igp
->i_field
== *realfield
&&
1206 strcmp(igp
->i_field
, realfield
) == 0)
1212 * Fake Sender for From_ lines if missing, e. g. with POP3.
1215 fakefrom(struct message
*mp
)
1219 if (((name
= skin(hfield1("return-path", mp
))) == NULL
||
1221 ((name
= skin(hfield1("from", mp
))) == NULL
||
1233 for (cq
= cp
; *cq
&& *cq
!= '\n'; cq
++);
1247 while (*cp
!= '\0') {
1259 } else if (blankchar(*cp
& 0377) || *cp
== ',')
1268 * From username Fri Jan 2 20:13:51 2004
1273 unixtime(char *from
)
1277 int i
, year
, month
, day
, hour
, minute
, second
;
1281 for (fp
= from
; *fp
&& *fp
!= '\n'; fp
++);
1287 for (i
= 0; month_names
[i
]; i
++)
1288 if (strncmp(&fp
[4], month_names
[i
], 3) == 0)
1290 if (month_names
[i
] == 0)
1295 day
= strtol(&fp
[8], &xp
, 10);
1296 if (*xp
!= ' ' || xp
!= &fp
[10])
1298 hour
= strtol(&fp
[11], &xp
, 10);
1299 if (*xp
!= ':' || xp
!= &fp
[13])
1301 minute
= strtol(&fp
[14], &xp
, 10);
1302 if (*xp
!= ':' || xp
!= &fp
[16])
1304 second
= strtol(&fp
[17], &xp
, 10);
1305 if (*xp
!= ' ' || xp
!= &fp
[19])
1307 year
= strtol(&fp
[20], &xp
, 10);
1310 if ((t
= combinetime(year
, month
, day
, hour
, minute
, second
)) ==
1313 tzdiff
= t
- mktime(gmtime(&t
));
1314 tmptr
= localtime(&t
);
1315 if (tmptr
->tm_isdst
> 0)
1327 char *cp
= date
, *x
;
1329 int i
, year
, month
, day
, hour
, minute
, second
;
1331 if ((cp
= nexttoken(cp
)) == NULL
)
1333 if (alphachar(cp
[0] & 0377) && alphachar(cp
[1] & 0377) &&
1334 alphachar(cp
[2] & 0377) && cp
[3] == ',') {
1335 if ((cp
= nexttoken(&cp
[4])) == NULL
)
1338 day
= strtol(cp
, &x
, 10);
1339 if ((cp
= nexttoken(x
)) == NULL
)
1341 for (i
= 0; month_names
[i
]; i
++) {
1342 if (strncmp(cp
, month_names
[i
], 3) == 0)
1345 if (month_names
[i
] == NULL
)
1348 if ((cp
= nexttoken(&cp
[3])) == NULL
)
1350 year
= strtol(cp
, &x
, 10);
1351 if ((cp
= nexttoken(x
)) == NULL
)
1353 hour
= strtol(cp
, &x
, 10);
1357 minute
= strtol(cp
, &x
, 10);
1360 second
= strtol(cp
, &x
, 10);
1363 if ((t
= combinetime(year
, month
, day
, hour
, minute
, second
)) ==
1366 if ((cp
= nexttoken(x
)) != NULL
) {
1377 if (digitchar(cp
[0] & 0377) && digitchar(cp
[1] & 0377) &&
1378 digitchar(cp
[2] & 0377) &&
1379 digitchar(cp
[3] & 0377)) {
1383 t
+= strtol(buf
, NULL
, 10) * sign
* 3600;
1386 t
+= strtol(buf
, NULL
, 10) * sign
* 60;
1394 #define leapyear(year) ((year % 100 ? year : year / 100) % 4 == 0)
1397 combinetime(int year
, int month
, int day
, int hour
, int minute
, int second
)
1401 if (second
< 0 || minute
< 0 || hour
< 0 || day
< 1)
1403 t
= second
+ minute
* 60 + hour
* 3600 + (day
- 1) * 86400;
1406 else if (year
< 1900)
1411 t
+= 86400 * (leapyear(year
) ? 29 : 28);
1431 t
+= (year
- 70) * 31536000 + ((year
- 69) / 4) * 86400 -
1432 ((year
- 1) / 100) * 86400 + ((year
+ 299) / 400) * 86400;
1437 substdate(struct message
*m
)
1443 * Determine the date to print in faked 'From ' lines. This is
1444 * traditionally the date the message was written to the mail
1445 * file. Try to determine this using RFC message header fields,
1446 * or fall back to current time.
1449 if ((cp
= hfield1("received", m
)) != NULL
) {
1450 while ((cp
= nexttoken(cp
)) != NULL
&& *cp
!= ';') {
1453 while (alnumchar(*cp
& 0377));
1456 m
->m_time
= rfctime(cp
);
1458 if (m
->m_time
== 0 || m
->m_time
> now
)
1459 if ((cp
= hfield1("date", m
)) != NULL
)
1460 m
->m_time
= rfctime(cp
);
1461 if (m
->m_time
== 0 || m
->m_time
> now
)
1466 check_from_and_sender(struct name
*fromfield
, struct name
*senderfield
)
1468 if (fromfield
&& fromfield
->n_flink
&& senderfield
== NULL
) {
1469 fprintf(stderr
, "A Sender: field is required with multiple "
1470 "addresses in From: field.\n");
1473 if (senderfield
&& senderfield
->n_flink
) {
1474 fprintf(stderr
, "The Sender: field may contain "
1475 "only one address.\n");
1482 getsender(struct message
*mp
)
1487 if ((cp
= hfield1("from", mp
)) == NULL
||
1488 (np
= sextract(cp
, GEXTRA
|GSKIN
)) == NULL
)
1490 return np
->n_flink
!= NULL
? skin(hfield1("sender", mp
)) : np
->n_name
;