1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Routines for processing and detecting headlines.
3 *@ TODO Mostly a hackery, we need RFC compliant parsers instead.
5 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
6 * Copyright (c) 2012 - 2018 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
9 * Copyright (c) 1980, 1993
10 * The Regents of the University of California. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #ifndef HAVE_AMALGAMATION
46 size_t tlen
; /* Length of .tdata */
47 char const *tdata
; /* Template date - see _cmatch_data[] */
50 /* Template characters for cmatch_data.tdata:
51 * 'A' An upper case char
52 * 'a' A lower case char
55 * 'O' An optional digit or space
57 * '+' Either a plus or a minus sign */
58 static struct cmatch_data
const _cmatch_data
[] = {
59 { 24, "Aaa Aaa O0 00:00:00 0000" }, /* BSD/ISO C90 ctime */
60 { 28, "Aaa Aaa O0 00:00:00 AAA 0000" }, /* BSD tmz */
61 { 21, "Aaa Aaa O0 00:00 0000" }, /* SysV ctime */
62 { 25, "Aaa Aaa O0 00:00 AAA 0000" }, /* SysV tmz */
63 /* RFC 822-alike From_ lines do not conform to RFC 4155, but seem to be used
64 * in the wild (by UW-imap) */
65 { 30, "Aaa Aaa O0 00:00:00 0000 +0000" },
66 /* RFC 822 with zone spec; 1. military, 2. UT, 3. north america time
67 * zone strings; note that 1. is strictly speaking not correct as some
68 * letters are not used, and 2. is not because only "UT" is defined */
69 #define __reuse "Aaa Aaa O0 00:00:00 0000 AAA"
70 { 28 - 2, __reuse
}, { 28 - 1, __reuse
}, { 28 - 0, __reuse
},
73 #define a_HEAD_DATE_MINLEN 21
75 /* Skip over "word" as found in From_ line */
76 static char const * _from__skipword(char const *wp
);
78 /* Match the date string against the date template (tp), return if match.
79 * See _cmatch_data[] for template character description */
80 static int _cmatch(size_t len
, char const *date
,
83 /* Check whether date is a valid 'From_' date.
84 * (Rather ctime(3) generated dates, according to RFC 4155) */
85 static int _is_date(char const *date
);
87 /* JulianDayNumber converter(s) */
88 static size_t a_head_gregorian_to_jdn(ui32_t y
, ui32_t m
, ui32_t d
);
90 static void a_head_jdn_to_gregorian(size_t jdn
,
91 ui32_t
*yp
, ui32_t
*mp
, ui32_t
*dp
);
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 n_addrguts
*a_head_idna_apply(struct n_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 * issingle_hack has the same meaning as for n_addrspec_with_guts() */
106 static bool_t
a_head_addrspec_check(struct n_addrguts
*agp
, bool_t skinned
,
107 bool_t issingle_hack
);
109 /* Return the next header field found in the given message.
110 * Return >= 0 if something found, < 0 elsewise.
111 * "colon" is set to point to the colon in the header.
112 * Must deal with \ continuations & other such fraud */
113 static long a_gethfield(FILE *f
, char **linebuf
, size_t *linesize
, long rem
,
116 static int msgidnextc(char const **cp
, int *status
);
118 /* Count the occurances of c in str */
119 static int charcount(char *str
, int c
);
121 static char const * nexttoken(char const *cp
);
124 _from__skipword(char const *wp
)
130 while ((c
= *wp
++) != '\0' && !blankchar(c
)) {
132 while ((c
= *wp
++) != '\0' && c
!= '"')
138 for (; blankchar(c
); c
= *wp
++)
142 return (c
== 0 ? NULL
: wp
- 1);
146 _cmatch(size_t len
, char const *date
, char const *tp
)
171 if (c
!= ' ' && !digitchar(c
))
179 if (c
!= '+' && c
!= '-')
191 _is_date(char const *date
)
193 struct cmatch_data
const *cmdp
;
198 if ((dl
= strlen(date
)) >= a_HEAD_DATE_MINLEN
)
199 for (cmdp
= _cmatch_data
; cmdp
->tdata
!= NULL
; ++cmdp
)
200 if (dl
== cmdp
->tlen
&& (rv
= _cmatch(dl
, date
, cmdp
->tdata
)))
207 a_head_gregorian_to_jdn(ui32_t y
, ui32_t m
, ui32_t d
){
208 /* Algorithm is taken from Communications of the ACM, Vol 6, No 8.
209 * (via third hand, plus adjustments).
210 * This algorithm is supposed to work for all dates in between 1582-10-15
211 * (0001-01-01 but that not Gregorian) and 65535-12-31 */
250 a_head_jdn_to_gregorian(size_t jdn
, ui32_t
*yp
, ui32_t
*mp
, ui32_t
*dp
){
251 /* Algorithm is taken from Communications of the ACM, Vol 6, No 8.
252 * (via third hand, plus adjustments) */
271 x
= jdn
/ 153; /* x -> month */
274 jdn
/= 5; /* jdn -> day */
282 *yp
= (ui32_t
)(y
& 0xFFFF);
283 *mp
= (ui32_t
)(x
& 0xFF);
284 *dp
= (ui32_t
)(jdn
& 0xFF);
290 static struct n_addrguts
*
291 a_head_idna_apply(struct n_addrguts
*agp
){
292 struct n_string idna_ascii
;
295 n_string_creat_auto(&idna_ascii
);
297 if(!n_idna_to_ascii(&idna_ascii
, &agp
->ag_skinned
[agp
->ag_sdom_start
],
298 agp
->ag_slen
- agp
->ag_sdom_start
))
299 agp
->ag_n_flags
^= NAME_ADDRSPEC_ERR_IDNA
| NAME_ADDRSPEC_ERR_CHAR
;
301 /* Replace the domain part of .ag_skinned with IDNA version */
302 n_string_unshift_buf(&idna_ascii
, agp
->ag_skinned
, agp
->ag_sdom_start
);
304 agp
->ag_skinned
= n_string_cp(&idna_ascii
);
305 agp
->ag_slen
= idna_ascii
.s_len
;
306 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
,
307 NAME_NAME_SALLOC
| NAME_SKINNED
| NAME_IDNA
, 0);
312 #endif /* HAVE_IDNA */
315 a_head_addrspec_check(struct n_addrguts
*agp
, bool_t skinned
,
316 bool_t issingle_hack
)
319 union {bool_t b
; char c
; unsigned char u
; ui32_t ui32
; si32_t si32
;} c
;
322 a_IDNA_ENABLE
= 1u<<0,
323 a_IDNA_APPLY
= 1u<<1,
324 a_REDO_NODE_AFTER_ADDR
= 1u<<2,
325 a_RESET_MASK
= a_IDNA_ENABLE
| a_IDNA_APPLY
| a_REDO_NODE_AFTER_ADDR
,
328 a_IN_DOMAIN
= 1u<<10,
329 a_DOMAIN_V6
= 1u<<11,
330 a_DOMAIN_MASK
= a_IN_DOMAIN
| a_DOMAIN_V6
336 if(!ok_blook(idna_disable
))
337 flags
= a_IDNA_ENABLE
;
340 if (agp
->ag_iaddr_aend
- agp
->ag_iaddr_start
== 0) {
341 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
, NAME_ADDRSPEC_ERR_EMPTY
, 0);
345 addr
= agp
->ag_skinned
;
347 /* If the field is not a recipient, it cannot be a file or a pipe */
351 /* When changing any of the following adjust any RECIPIENTADDRSPEC;
352 * grep the latter for the complete picture */
354 agp
->ag_n_flags
|= NAME_ADDRSPEC_ISPIPE
;
357 if (addr
[0] == '/' || (addr
[0] == '.' && addr
[1] == '/') ||
358 (addr
[0] == '-' && addr
[1] == '\0'))
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
;
375 /* TODO This is false. If super correct this should work on wide
376 * TODO characters, just in case (some bytes of) the ASCII set is (are)
377 * TODO shared; it may yet tear apart multibyte sequences, possibly.
378 * TODO All this should interact with mime_enc_mustquote(), too!
379 * TODO That is: once this is an object, we need to do this in a way
380 * TODO that it is valid for the wire format (instead)! */
381 /* TODO addrspec_check: we need a real RFC 5322 (un)?structured parser!
382 * TODO Note this correlats with addrspec_with_guts() which is in front
383 * TODO of us and encapsulates (what it thinks is, sigh) the address
384 * TODO boundary. ALL THIS should be one object that knows how to deal */
385 flags
&= a_RESET_MASK
;
386 for (p
= addr
; (c
.c
= *p
++) != '\0';) {
389 } else if (c
.u
< 040 || c
.u
>= 0177) { /* TODO no magics: !bodychar()? */
391 if ((flags
& (a_IN_DOMAIN
| a_IDNA_ENABLE
)) ==
392 (a_IN_DOMAIN
| a_IDNA_ENABLE
))
393 flags
|= a_IDNA_APPLY
;
397 } else if ((flags
& a_DOMAIN_MASK
) == a_DOMAIN_MASK
) {
398 if ((c
.c
== ']' && *p
!= '\0') || c
.c
== '\\' || whitechar(c
.c
))
400 } else if ((flags
& (a_IN_QUOTE
| a_DOMAIN_MASK
)) == a_IN_QUOTE
) {
402 } else if (c
.c
== '\\' && *p
!= '\0') {
404 } else if (c
.c
== '@') {
406 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
, NAME_ADDRSPEC_ERR_ATSEQ
,
410 agp
->ag_sdom_start
= PTR2SIZE(p
- addr
);
411 agp
->ag_n_flags
|= NAME_ADDRSPEC_ISADDR
; /* TODO .. really? */
412 flags
&= ~a_DOMAIN_MASK
;
413 flags
|= (*p
== '[') ? a_IN_AT
| a_IN_DOMAIN
| a_DOMAIN_V6
414 : a_IN_AT
| a_IN_DOMAIN
;
417 /* TODO This interferes with our alias handling, which allows :!
418 * TODO Update manual on support (search the several ALIASCOLON)! */
419 else if (c
.c
== '(' || c
.c
== ')' || c
.c
== '<' || c
.c
== '>' ||
420 c
.c
== '[' || c
.c
== ']' || c
.c
== ':' || c
.c
== ';' ||
421 c
.c
== '\\' || c
.c
== ',' || blankchar(c
.c
))
426 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
, NAME_ADDRSPEC_ERR_CHAR
, c
.u
);
430 /* If we do not think this is an address we may treat it as an alias name
431 * if and only if the original input is identical to the skinned version */
432 if(!(agp
->ag_n_flags
& NAME_ADDRSPEC_ISADDR
) &&
433 !strcmp(agp
->ag_skinned
, agp
->ag_input
)){
434 /* TODO This may be an UUCP address */
435 agp
->ag_n_flags
|= NAME_ADDRSPEC_ISNAME
;
436 if(!n_alias_is_valid_name(agp
->ag_input
))
437 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
, NAME_ADDRSPEC_ERR_NAME
, '.');
439 /* If we seem to know that this is an address. Ensure this is correct
440 * according to RFC 5322 TODO the entire address parser should be like
441 * TODO that for one, and then we should know whether structured or
442 * TODO unstructured, and just parse correctly overall!
443 * TODO In addition, this can be optimised a lot.
444 * TODO And it is far from perfect: it should not forget whether no
445 * TODO whitespace followed some snippet, and it was written hastily.
446 * TODO It is even wrong sometimes. Not only for strange cases */
448 struct a_token
*t_last
;
449 struct a_token
*t_next
;
455 a_T_TMASK
= (1u<<4) - 1,
457 a_T_SPECIAL
= 1u<<8 /* An atom actually needs to go TQUOTE */
462 } *thead
, *tcurr
, *tp
;
464 struct n_string ost
, *ostp
;
465 char const *cp
, *cp1st
, *cpmax
, *xp
;
468 /* Name and domain must be non-empty */
469 if(*addr
== '@' || &addr
[2] >= p
|| p
[-2] == '@'){
472 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
, NAME_ADDRSPEC_ERR_ATSEQ
, c
.u
);
478 /* Nothing to do if there is only an address (in angle brackets) */
479 /* TODO This is wrong since we allow invalid constructs in local-part
480 * TODO and domain, AT LEAST in so far as a"bc"d@abc should become
481 * TODO "abcd"@abc. Etc. */
482 if(agp
->ag_iaddr_start
== 0){
484 if(!(agp
->ag_n_flags
& NAME_ADDRSPEC_ISADDR
))
486 if(agp
->ag_iaddr_aend
== agp
->ag_ilen
)
488 }else if(agp
->ag_iaddr_start
== 1 && *cp
== '<' &&
489 agp
->ag_iaddr_aend
== agp
->ag_ilen
- 1 &&
490 cp
[agp
->ag_iaddr_aend
] == '>'){
491 /* No @ seen? Possibly insert n_nodename() */
492 if(!(agp
->ag_n_flags
& NAME_ADDRSPEC_ISADDR
)){
493 cp
= &agp
->ag_input
[agp
->ag_iaddr_start
];
494 cpmax
= &agp
->ag_input
[agp
->ag_iaddr_aend
];
500 /* It is not, so parse off all tokens, then resort and rejoin */
501 lofi_snap
= n_lofi_snap_create();
504 if((c
.ui32
= agp
->ag_iaddr_start
) > 0)
508 thead
= tcurr
= NULL
;
510 for(tp
= NULL
; cp
< cpmax
;){
514 tp
->t_end
= PTR2SIZE(cp
- cp1st
);
515 tp
= n_lofi_alloc(sizeof *tp
);
517 if((tp
->t_last
= tcurr
) != NULL
)
523 tp
->t_start
= PTR2SIZE(++cp
- cp1st
);
524 xp
= skip_comment(cp
);
525 tp
->t_end
= PTR2SIZE(xp
- cp1st
);
527 if(tp
->t_end
> tp
->t_start
){
531 /* No closing comment - strip trailing whitespace */
532 while(blankchar(*--xp
))
533 if(--tp
->t_end
== tp
->t_start
)
542 tp
->t_end
= PTR2SIZE(cp
- cp1st
);
543 tp
= n_lofi_alloc(sizeof *tp
);
545 if((tp
->t_last
= tcurr
) != NULL
)
550 tp
->t_f
= a_T_TQUOTE
;
551 tp
->t_start
= PTR2SIZE(++cp
- cp1st
);
552 for(xp
= cp
; xp
< cpmax
; ++xp
){
553 if((c
.c
= *xp
) == '"')
555 if(c
.c
== '\\' && xp
[1] != '\0')
558 tp
->t_end
= PTR2SIZE(xp
- cp1st
);
560 if(tp
->t_end
> tp
->t_start
){
561 /* No closing quote - strip trailing whitespace */
563 while(blankchar(*xp
--))
564 if(--tp
->t_end
== tp
->t_start
)
574 tp
->t_end
= PTR2SIZE(cp
- cp1st
);
581 tp
= n_lofi_alloc(sizeof *tp
);
583 if((tp
->t_last
= tcurr
) != NULL
)
589 tp
->t_start
= PTR2SIZE(cp
- cp1st
);
593 /* Reverse solidus transforms the following into a quoted-pair, and
594 * therefore (must occur in comment or quoted-string only) the
595 * entire atom into a quoted string */
597 tp
->t_f
|= a_T_SPECIAL
;
603 /* Is this plain RFC 5322 "atext", or "specials"?
604 * TODO Because we don't know structured/unstructured, nor anything
605 * TODO else, we need to treat "dot-atom" as being identical to
607 * However, if the 8th bit is set, this will be RFC 2047 converted
608 * and the entire sequence is skipped */
609 if(!(c
.u
& 0x80) && !alnumchar(c
.c
) &&
610 c
.c
!= '!' && c
.c
!= '#' && c
.c
!= '$' && c
.c
!= '%' &&
611 c
.c
!= '&' && c
.c
!= '\'' && c
.c
!= '*' && c
.c
!= '+' &&
612 c
.c
!= '-' && c
.c
!= '/' && c
.c
!= '=' && c
.c
!= '?' &&
613 c
.c
!= '^' && c
.c
!= '_' && c
.c
!= '`' && c
.c
!= '{' &&
614 c
.c
!= '}' && c
.c
!= '|' && c
.c
!= '}' && c
.c
!= '~')
615 tp
->t_f
|= a_T_SPECIAL
;
620 tp
->t_end
= PTR2SIZE(cp
- cp1st
);
622 if(!(flags
& a_REDO_NODE_AFTER_ADDR
)){
623 flags
|= a_REDO_NODE_AFTER_ADDR
;
625 /* The local-part may be in quotes.. */
626 if((tp
= tcurr
) != NULL
&& (tp
->t_f
& a_T_TQUOTE
) &&
627 tp
->t_end
== agp
->ag_iaddr_start
- 1){
628 /* ..so backward extend it, including the starting quote */
629 /* TODO This is false and the code below #if 0 away. We would
630 * TODO need to create a properly quoted local-part HERE AND NOW
631 * TODO and REPLACE the original data with that version, but the
632 * TODO current code cannot do that. The node needs the data,
633 * TODO not only offsets for that, for example. If we had all that
634 * TODO the code below could produce a really valid thing */
637 if(tp
->t_start
> 0 &&
638 (tp
->t_last
== NULL
|| tp
->t_last
->t_end
< tp
->t_start
) &&
639 agp
->ag_input
[tp
->t_start
- 1] == '\\')
641 tp
->t_f
= a_T_TADDR
| a_T_SPECIAL
;
643 tp
= n_lofi_alloc(sizeof *tp
);
645 if((tp
->t_last
= tcurr
) != NULL
)
651 tp
->t_start
= agp
->ag_iaddr_start
;
652 /* TODO Very special case because of our hacky non-object-based and
653 * TODO non-compliant address parser. Note */
654 if(tp
->t_last
== NULL
&& tp
->t_start
> 0)
656 if(agp
->ag_input
[tp
->t_start
] == '<')
659 /* TODO Very special check for whether we need to massage the
660 * TODO local part. This is wrong, but otherwise even more so */
662 cp
= &agp
->ag_input
[tp
->t_start
];
663 cpmax
= &agp
->ag_input
[agp
->ag_iaddr_aend
];
666 if(!(c
.u
& 0x80) && !alnumchar(c
.c
) &&
667 c
.c
!= '!' && c
.c
!= '#' && c
.c
!= '$' && c
.c
!= '%' &&
668 c
.c
!= '&' && c
.c
!= '\'' && c
.c
!= '*' && c
.c
!= '+' &&
669 c
.c
!= '-' && c
.c
!= '/' && c
.c
!= '=' && c
.c
!= '?' &&
670 c
.c
!= '^' && c
.c
!= '_' && c
.c
!= '`' && c
.c
!= '{' &&
671 c
.c
!= '}' && c
.c
!= '|' && c
.c
!= '}' && c
.c
!= '~'){
672 tp
->t_f
|= a_T_SPECIAL
;
678 tp
->t_end
= agp
->ag_iaddr_aend
;
679 assert(tp
->t_start
<= tp
->t_end
);
682 cp
= &agp
->ag_input
[agp
->ag_iaddr_aend
+ 1];
683 cpmax
= &agp
->ag_input
[agp
->ag_ilen
];
688 /* Nothing may follow the address, move it to the end */
689 if(!(tcurr
->t_f
& a_T_TADDR
)){
690 for(tp
= thead
; tp
!= NULL
; tp
= tp
->t_next
){
691 if(tp
->t_f
& a_T_TADDR
){
692 if(tp
->t_last
!= NULL
)
693 tp
->t_last
->t_next
= tp
->t_next
;
696 if(tp
->t_next
!= NULL
)
697 tp
->t_next
->t_last
= tp
->t_last
;
700 while(tp
->t_next
!= NULL
)
704 tcurr
->t_next
= NULL
;
710 /* Make ranges contiguous: ensure a continuous range of atoms is converted
711 * to a SPECIAL one if at least one of them requires it */
712 for(tp
= thead
; tp
!= NULL
; tp
= tp
->t_next
){
713 if(tp
->t_f
& a_T_SPECIAL
){
715 while((tp
= tp
->t_last
) != NULL
&& (tp
->t_f
& a_T_TATOM
))
716 tp
->t_f
|= a_T_SPECIAL
;
718 while((tp
= tp
->t_next
) != NULL
&& (tp
->t_f
& a_T_TATOM
))
719 tp
->t_f
|= a_T_SPECIAL
;
725 /* And yes, we want quotes to extend as much as possible */
726 for(tp
= thead
; tp
!= NULL
; tp
= tp
->t_next
){
727 if(tp
->t_f
& a_T_TQUOTE
){
729 while((tp
= tp
->t_last
) != NULL
&& (tp
->t_f
& a_T_TATOM
))
730 tp
->t_f
|= a_T_SPECIAL
;
732 while((tp
= tp
->t_next
) != NULL
&& (tp
->t_f
& a_T_TATOM
))
733 tp
->t_f
|= a_T_SPECIAL
;
740 ostp
= n_string_creat_auto(&ost
);
741 if((c
.ui32
= agp
->ag_ilen
) <= UI32_MAX
>> 1)
742 ostp
= n_string_reserve(ostp
, c
.ui32
<<= 1);
744 for(tcurr
= thead
; tcurr
!= NULL
;){
746 ostp
= n_string_push_c(ostp
, ' ');
747 if(tcurr
->t_f
& a_T_TADDR
){
748 if(tcurr
->t_last
!= NULL
)
749 ostp
= n_string_push_c(ostp
, '<');
750 agp
->ag_iaddr_start
= ostp
->s_len
;
751 /* Now it is terrible to say, but if that thing contained
752 * quotes, then those may contain quoted-pairs! */
754 if(!(tcurr
->t_f
& a_T_SPECIAL
)){
756 ostp
= n_string_push_buf(ostp
, &cp1st
[tcurr
->t_start
],
757 (tcurr
->t_end
- tcurr
->t_start
));
762 ostp
= n_string_push_c(ostp
, '"');
765 cp
= &cp1st
[tcurr
->t_start
];
766 cpmax
= &cp1st
[tcurr
->t_end
];
767 for(esc
= FAL0
; cp
< cpmax
;){
768 if((c
.c
= *cp
++) == '\\' && !esc
){
769 if(cp
< cpmax
&& (*cp
== '"' || *cp
== '\\'))
772 if(esc
|| c
.c
== '"')
773 ostp
= n_string_push_c(ostp
, '\\');
775 ostp
= n_string_push_c(ostp
, '"');
778 ostp
= n_string_push_c(ostp
, c
.c
);
784 agp
->ag_iaddr_aend
= ostp
->s_len
;
786 if(tcurr
->t_last
!= NULL
)
787 ostp
= n_string_push_c(ostp
, '>');
788 tcurr
= tcurr
->t_next
;
789 }else if(tcurr
->t_f
& a_T_TCOMM
){
790 ostp
= n_string_push_c(ostp
, '(');
791 ostp
= n_string_push_buf(ostp
, &cp1st
[tcurr
->t_start
],
792 (tcurr
->t_end
- tcurr
->t_start
));
793 while((tp
= tcurr
->t_next
) != NULL
&& (tp
->t_f
& a_T_TCOMM
)){
795 ostp
= n_string_push_c(ostp
, ' '); /* XXX may be artificial */
796 ostp
= n_string_push_buf(ostp
, &cp1st
[tcurr
->t_start
],
797 (tcurr
->t_end
- tcurr
->t_start
));
799 ostp
= n_string_push_c(ostp
, ')');
800 tcurr
= tcurr
->t_next
;
801 }else if(tcurr
->t_f
& a_T_TQUOTE
){
803 ostp
= n_string_push_c(ostp
, '"');
805 do/* while tcurr && TATOM||TQUOTE */{
806 cp
= &cp1st
[tcurr
->t_start
];
807 cpmax
= &cp1st
[tcurr
->t_end
];
812 ostp
= n_string_push_c(ostp
, ' ');
814 if((tcurr
->t_f
& (a_T_TATOM
| a_T_SPECIAL
)) == a_T_TATOM
)
815 ostp
= n_string_push_buf(ostp
, cp
, PTR2SIZE(cpmax
- cp
));
819 for(esc
= FAL0
; cp
< cpmax
;){
820 if((c
.c
= *cp
++) == '\\' && !esc
){
821 if(cp
< cpmax
&& (*cp
== '"' || *cp
== '\\'))
824 if(esc
|| c
.c
== '"'){
826 ostp
= n_string_push_c(ostp
, '\\');
828 ostp
= n_string_push_c(ostp
, c
.c
);
837 }while((tcurr
= tcurr
->t_next
) != NULL
&&
838 (tcurr
->t_f
& (a_T_TATOM
| a_T_TQUOTE
)));
839 ostp
= n_string_push_c(ostp
, '"');
840 }else if(tcurr
->t_f
& a_T_SPECIAL
)
843 /* Can we use a fast join mode? */
844 for(tp
= tcurr
; tcurr
!= NULL
; tcurr
= tcurr
->t_next
){
845 if(!(tcurr
->t_f
& a_T_TATOM
))
848 ostp
= n_string_push_c(ostp
, ' ');
849 ostp
= n_string_push_buf(ostp
, &cp1st
[tcurr
->t_start
],
850 (tcurr
->t_end
- tcurr
->t_start
));
855 n_lofi_snap_unroll(lofi_snap
);
857 agp
->ag_input
= n_string_cp(ostp
);
858 agp
->ag_ilen
= ostp
->s_len
;
859 /*ostp = n_string_drop_ownership(ostp);*/
861 /* Name and domain must be non-empty, the second */
862 cp
= &agp
->ag_input
[agp
->ag_iaddr_start
];
863 cpmax
= &agp
->ag_input
[agp
->ag_iaddr_aend
];
864 if(*cp
== '@' || &cp
[2] > cpmax
|| cpmax
[-1] == '@'){
866 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
, NAME_ADDRSPEC_ERR_ATSEQ
, c
.u
);
870 addr
= agp
->ag_skinned
= savestrbuf(cp
, PTR2SIZE(cpmax
- cp
));
872 /* TODO This parser is a mess. We do not know whether this is truly
873 * TODO valid, and all our checks are not truly RFC conforming.
874 * TODO Do check the skinned thing by itself once more, in order
875 * TODO to catch problems from reordering, e.g., this additional
876 * TODO test catches a final address without AT..
877 * TODO This is a plain copy+paste of the weird thing above, no care */
878 agp
->ag_n_flags
&= ~NAME_ADDRSPEC_ISADDR
;
879 flags
&= a_RESET_MASK
;
880 for (p
= addr
; (c
.c
= *p
++) != '\0';) {
883 else if (c
.u
< 040 || c
.u
>= 0177) {
885 if(!(flags
& a_IN_DOMAIN
))
888 } else if ((flags
& a_DOMAIN_MASK
) == a_DOMAIN_MASK
) {
889 if ((c
.c
== ']' && *p
!= '\0') || c
.c
== '\\' || whitechar(c
.c
))
891 } else if ((flags
& (a_IN_QUOTE
| a_DOMAIN_MASK
)) == a_IN_QUOTE
) {
893 } else if (c
.c
== '\\' && *p
!= '\0') {
895 } else if (c
.c
== '@') {
897 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
, NAME_ADDRSPEC_ERR_ATSEQ
,
902 agp
->ag_n_flags
|= NAME_ADDRSPEC_ISADDR
; /* TODO .. really? */
903 flags
&= ~a_DOMAIN_MASK
;
904 flags
|= (*p
== '[') ? a_IN_DOMAIN
| a_DOMAIN_V6
: a_IN_DOMAIN
;
906 } else if (c
.c
== '(' || c
.c
== ')' || c
.c
== '<' || c
.c
== '>' ||
907 c
.c
== '[' || c
.c
== ']' || c
.c
== ':' || c
.c
== ';' ||
908 c
.c
== '\\' || c
.c
== ',' || blankchar(c
.c
))
913 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
, NAME_ADDRSPEC_ERR_CHAR
, c
.u
);
914 else if(!(agp
->ag_n_flags
& NAME_ADDRSPEC_ISADDR
)){
915 /* This is not an address, but if we had seen angle brackets convert
916 * it to a n_nodename() address if the name is a valid user */
918 if(cp
> &agp
->ag_input
[0] && cp
[-1] == '<' &&
919 cpmax
<= &agp
->ag_input
[agp
->ag_ilen
] && cpmax
[0] == '>' &&
920 (!strcmp(addr
, ok_vlook(LOGNAME
)) || getpwnam(addr
) != NULL
)){
921 /* XXX However, if hostname is set to the empty string this
922 * XXX indicates that the used *mta* will perform the
923 * XXX auto-expansion instead. Not so with `addrcodec' though */
924 agp
->ag_n_flags
|= NAME_ADDRSPEC_ISADDR
;
926 (cp
= ok_vlook(hostname
)) != NULL
&& *cp
== '\0')
927 agp
->ag_n_flags
|= NAME_ADDRSPEC_WITHOUT_DOMAIN
;
929 c
.ui32
= strlen(cp
= n_nodename(TRU1
));
930 /* This is yet IDNA converted.. */
931 ostp
= n_string_creat_auto(&ost
);
932 ostp
= n_string_assign_buf(ostp
, agp
->ag_input
, agp
->ag_ilen
);
933 ostp
= n_string_insert_c(ostp
, agp
->ag_iaddr_aend
++, '@');
934 ostp
= n_string_insert_buf(ostp
, agp
->ag_iaddr_aend
, cp
,
936 agp
->ag_iaddr_aend
+= c
.ui32
;
937 agp
->ag_input
= n_string_cp(ostp
);
938 agp
->ag_ilen
= ostp
->s_len
;
939 /*ostp = n_string_drop_ownership(ostp);*/
941 cp
= &agp
->ag_input
[agp
->ag_iaddr_start
];
942 cpmax
= &agp
->ag_input
[agp
->ag_iaddr_aend
];
943 agp
->ag_skinned
= savestrbuf(cp
, PTR2SIZE(cpmax
- cp
));
946 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
, NAME_ADDRSPEC_ERR_ATSEQ
,
953 if(!(agp
->ag_n_flags
& NAME_ADDRSPEC_INVALID
) && (flags
& a_IDNA_APPLY
))
954 agp
= a_head_idna_apply(agp
);
957 return !(agp
->ag_n_flags
& NAME_ADDRSPEC_INVALID
);
961 a_gethfield(FILE *f
, char **linebuf
, size_t *linesize
, long rem
, char **colon
)
963 char *line2
= NULL
, *cp
, *cp2
;
964 size_t line2size
= 0;
968 if (*linebuf
== NULL
)
969 *linebuf
= srealloc(*linebuf
, *linesize
= 1);
976 if ((c
= readline_restart(f
, linebuf
, linesize
, 0)) <= 0) {
980 for (cp
= *linebuf
; fieldnamechar(*cp
); ++cp
)
983 while (blankchar(*cp
))
985 if (*cp
!= ':' || cp
== *linebuf
)
988 /* I guess we got a headline. Handle wraparound */
993 while (PTRCMP(--cp
, >=, *linebuf
) && blankchar(*cp
))
998 if (PTRCMP(cp
- 8, >=, *linebuf
) && cp
[-1] == '=' && cp
[-2] == '?')
1000 ungetc(c
= getc(f
), f
);
1003 c
= readline_restart(f
, &line2
, &line2size
, 0); /* TODO linepool! */
1007 for (cp2
= line2
; blankchar(*cp2
); ++cp2
)
1009 c
-= (int)PTR2SIZE(cp2
- line2
);
1010 if (cp2
[0] == '=' && cp2
[1] == '?' && c
> 8)
1012 if (PTRCMP(cp
+ c
, >=, *linebuf
+ *linesize
- 2)) {
1013 size_t diff
= PTR2SIZE(cp
- *linebuf
),
1014 colondiff
= PTR2SIZE(*colon
- *linebuf
);
1015 *linebuf
= srealloc(*linebuf
, *linesize
+= c
+ 2);
1016 cp
= &(*linebuf
)[diff
];
1017 *colon
= &(*linebuf
)[colondiff
];
1035 msgidnextc(char const **cp
, int *status
)
1041 assert(*cp
!= NULL
);
1042 assert(status
!= NULL
);
1060 *cp
= skip_comment(&(*cp
)[1]);
1076 c
= *(*cp
)++ & 0377;
1077 c
= (*status
& 02) ? lowerconv(c
) : c
;
1087 charcount(char *str
, int c
)
1093 for (i
= 0, cp
= str
; *cp
; ++cp
)
1101 nexttoken(char const *cp
)
1120 } while (nesting
> 0 && *cp
!= '\0'); /* XXX error? */
1121 } else if (blankchar(*cp
) || *cp
== ',')
1131 myaddrs(struct header
*hp
) /* TODO */
1134 char const *rv
, *mta
;
1137 if (hp
!= NULL
&& (np
= hp
->h_from
) != NULL
) {
1138 if ((rv
= np
->n_fullname
) != NULL
)
1140 if ((rv
= np
->n_name
) != NULL
)
1144 /* Verified once variable had been set */
1145 if((rv
= ok_vlook(from
)) != NULL
)
1148 /* When invoking *sendmail* directly, it's its task to generate an otherwise
1149 * undeterminable From: address. However, if the user sets *hostname*,
1150 * accept his desire */
1151 if (ok_vlook(hostname
) != NULL
)
1153 if (ok_vlook(smtp
) != NULL
|| /* TODO obsolete -> mta */
1154 /* TODO pretty hacky for now (this entire fun), later: url_creat()! */
1155 ((mta
= n_servbyname(ok_vlook(mta
), NULL
)) != NULL
&& *mta
!= '\0'))
1163 char const *hn
, *ln
;
1166 hn
= n_nodename(TRU1
);
1167 ln
= ok_vlook(LOGNAME
);
1168 i
= strlen(ln
) + strlen(hn
) + 1 +1;
1169 rv
= cp
= salloc(i
);
1170 sstpcpy(sstpcpy(sstpcpy(cp
, ln
), n_at
), hn
);
1176 myorigin(struct header
*hp
) /* TODO */
1178 char const *rv
= NULL
, *ccp
;
1182 if((ccp
= myaddrs(hp
)) != NULL
&&
1183 (np
= lextract(ccp
, GEXTRA
| GFULL
)) != NULL
){
1184 if(np
->n_flink
== NULL
)
1186 /* Verified upon variable set time */
1187 else if((ccp
= ok_vlook(sender
)) != NULL
)
1189 /* TODO why not else rv = n_poption_arg_r; ?? */
1196 is_head(char const *linebuf
, size_t linelen
, bool_t check_rfc4155
)
1198 char date
[n_FROM_DATEBUF
];
1202 if ((rv
= (linelen
>= 5 && !memcmp(linebuf
, "From ", 5))) && check_rfc4155
&&
1203 (extract_date_from_from_(linebuf
, linelen
, date
) <= 0 ||
1211 extract_date_from_from_(char const *line
, size_t linelen
,
1212 char datebuf
[n_FROM_DATEBUF
])
1215 char const *cp
= line
;
1221 cp
= _from__skipword(cp
);
1225 cp
= _from__skipword(cp
);
1228 if((cp
[0] == 't' || cp
[0] == 'T') && (cp
[1] == 't' || cp
[1] == 'T') &&
1229 (cp
[2] == 'y' || cp
[2] == 'Y')){
1230 cp
= _from__skipword(cp
);
1234 /* It seems there are invalid MBOX archives in the wild, compare
1235 * . http://bugs.debian.org/624111
1236 * . [Mutt] #3868: mutt should error if the imported mailbox is invalid
1237 * What they do is that they obfuscate the address to "name at host",
1238 * and even "name at host dot dom dot dom.
1239 * The [Aa][Tt] is also RFC 733, so be tolerant */
1240 else if((cp
[0] == 'a' || cp
[0] == 'A') && (cp
[1] == 't' || cp
[1] == 'T') &&
1245 cp
= _from__skipword(cp
);
1248 if((cp
[0] == 'd' || cp
[0] == 'D') && (cp
[1] == 'o' || cp
[1] == 'O') &&
1249 (cp
[2] == 't' || cp
[2] == 'T') && cp
[3] == ' '){
1255 linelen
-= PTR2SIZE(cp
- line
);
1256 if (linelen
< a_HEAD_DATE_MINLEN
)
1258 if (cp
[linelen
- 1] == '\n') {
1260 /* (Rather IMAP/POP3 only) */
1261 if (cp
[linelen
- 1] == '\r')
1263 if (linelen
< a_HEAD_DATE_MINLEN
)
1266 if (linelen
>= n_FROM_DATEBUF
)
1270 memcpy(datebuf
, cp
, linelen
);
1271 datebuf
[linelen
] = '\0';
1275 cp
= _("<Unknown date>");
1276 linelen
= strlen(cp
);
1277 if (linelen
>= n_FROM_DATEBUF
)
1278 linelen
= n_FROM_DATEBUF
;
1284 extract_header(FILE *fp
, struct header
*hp
, si8_t
*checkaddr_err
)
1286 /* See the prototype declaration for the hairy relationship of
1287 * n_poption&n_PO_t_FLAG and/or n_psonce&n_PSO_t_FLAG in here */
1288 struct n_header_field
**hftail
;
1289 struct header nh
, *hq
= &nh
;
1290 char *linebuf
= NULL
/* TODO line pool */, *colon
;
1291 size_t linesize
= 0, seenfields
= 0;
1294 char const *val
, *cp
;
1297 memset(hq
, 0, sizeof *hq
);
1298 if ((n_psonce
& n_PSO_t_FLAG
) && (n_poption
& n_PO_t_FLAG
)) {
1299 hq
->h_to
= hp
->h_to
;
1300 hq
->h_cc
= hp
->h_cc
;
1301 hq
->h_bcc
= hp
->h_bcc
;
1303 hftail
= &hq
->h_user_headers
;
1305 for (lc
= 0; readline_restart(fp
, &linebuf
, &linesize
, 0) > 0; ++lc
)
1308 /* TODO yippieia, cat(check(lextract)) :-) */
1310 while ((lc
= a_gethfield(fp
, &linebuf
, &linesize
, lc
, &colon
)) >= 0) {
1313 /* We explicitly allow EAF_NAME for some addressees since aliases are not
1314 * yet expanded when we parse these! */
1315 if ((val
= thisfield(linebuf
, "to")) != NULL
) {
1317 hq
->h_to
= cat(hq
->h_to
, checkaddrs(lextract(val
, GTO
| GFULL
),
1318 EACM_NORMAL
| EAF_NAME
| EAF_MAYKEEP
, checkaddr_err
));
1319 } else if ((val
= thisfield(linebuf
, "cc")) != NULL
) {
1321 hq
->h_cc
= cat(hq
->h_cc
, checkaddrs(lextract(val
, GCC
| GFULL
),
1322 EACM_NORMAL
| EAF_NAME
| EAF_MAYKEEP
, checkaddr_err
));
1323 } else if ((val
= thisfield(linebuf
, "bcc")) != NULL
) {
1325 hq
->h_bcc
= cat(hq
->h_bcc
, checkaddrs(lextract(val
, GBCC
| GFULL
),
1326 EACM_NORMAL
| EAF_NAME
| EAF_MAYKEEP
, checkaddr_err
));
1327 } else if ((val
= thisfield(linebuf
, "from")) != NULL
) {
1328 if (!(n_psonce
& n_PSO_t_FLAG
) || (n_poption
& n_PO_t_FLAG
)) {
1330 hq
->h_from
= cat(hq
->h_from
,
1331 checkaddrs(lextract(val
, GEXTRA
| GFULL
| GFULLEXTRA
),
1332 EACM_STRICT
, NULL
));
1334 } else if ((val
= thisfield(linebuf
, "reply-to")) != NULL
) {
1336 hq
->h_reply_to
= cat(hq
->h_reply_to
,
1337 checkaddrs(lextract(val
, GEXTRA
| GFULL
), EACM_STRICT
, NULL
));
1338 } else if ((val
= thisfield(linebuf
, "sender")) != NULL
) {
1339 if (!(n_psonce
& n_PSO_t_FLAG
) || (n_poption
& n_PO_t_FLAG
)) {
1341 hq
->h_sender
= cat(hq
->h_sender
, /* TODO cat? check! */
1342 checkaddrs(lextract(val
, GEXTRA
| GFULL
| GFULLEXTRA
),
1343 EACM_STRICT
, NULL
));
1346 } else if ((val
= thisfield(linebuf
, "subject")) != NULL
||
1347 (val
= thisfield(linebuf
, "subj")) != NULL
) {
1349 for (cp
= val
; blankchar(*cp
); ++cp
)
1351 hq
->h_subject
= (hq
->h_subject
!= NULL
)
1352 ? save2str(hq
->h_subject
, cp
) : savestr(cp
);
1354 /* The remaining are mostly hacked in and thus TODO -- at least in
1355 * TODO respect to their content checking */
1356 else if((val
= thisfield(linebuf
, "message-id")) != NULL
){
1357 if(n_psonce
& n_PSO_t_FLAG
){
1358 np
= checkaddrs(lextract(val
, GREF
),
1359 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG
| EACM_NONAME
,
1361 if (np
== NULL
|| np
->n_flink
!= NULL
)
1364 hq
->h_message_id
= np
;
1367 }else if((val
= thisfield(linebuf
, "in-reply-to")) != NULL
){
1368 if(n_psonce
& n_PSO_t_FLAG
){
1369 np
= checkaddrs(lextract(val
, GREF
),
1370 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG
| EACM_NONAME
,
1373 hq
->h_in_reply_to
= np
;
1376 }else if((val
= thisfield(linebuf
, "references")) != NULL
){
1377 if(n_psonce
& n_PSO_t_FLAG
){
1379 /* TODO Limit number of references TODO better on parser side */
1380 hq
->h_ref
= cat(hq
->h_ref
, checkaddrs(extract(val
, GREF
),
1381 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG
| EACM_NONAME
,
1386 /* and that is very hairy */
1387 else if((val
= thisfield(linebuf
, "mail-followup-to")) != NULL
){
1388 if(n_psonce
& n_PSO_t_FLAG
){
1390 hq
->h_mft
= cat(hq
->h_mft
, checkaddrs(lextract(val
, GEXTRA
| GFULL
),
1391 /*EACM_STRICT | TODO '/' valid!! | EACM_NOLOG | */EACM_NONAME
,
1396 /* A free-form header; a_gethfield() did some verification already.. */
1398 struct n_header_field
*hfp
;
1402 for(nstart
= cp
= linebuf
;; ++cp
)
1403 if(!fieldnamechar(*cp
))
1405 nl
= (ui32_t
)PTR2SIZE(cp
- nstart
);
1407 while(blankchar(*cp
))
1411 n_err(_("Ignoring header field: %s\n"), linebuf
);
1414 while(blankchar(*cp
))
1416 bl
= (ui32_t
)strlen(cp
) +1;
1419 *hftail
= hfp
= salloc(n_VSTRUCT_SIZEOF(struct n_header_field
, hf_dat
1421 hftail
= &hfp
->hf_next
;
1422 hfp
->hf_next
= NULL
;
1424 hfp
->hf_bl
= bl
- 1;
1425 memcpy(hfp
->hf_dat
, nstart
, nl
);
1426 hfp
->hf_dat
[nl
++] = '\0';
1427 memcpy(hfp
->hf_dat
+ nl
, cp
, bl
);
1431 /* In case the blank line after the header has been edited out. Otherwise,
1432 * fetch the header separator */
1433 if (linebuf
!= NULL
) {
1434 if (linebuf
[0] != '\0') {
1435 for (cp
= linebuf
; *(++cp
) != '\0';)
1437 fseek(fp
, (long)-PTR2SIZE(1 + cp
- linebuf
), SEEK_CUR
);
1439 if ((c
= getc(fp
)) != '\n' && c
!= EOF
)
1444 if (seenfields
> 0 && (checkaddr_err
== NULL
|| *checkaddr_err
== 0)) {
1445 hp
->h_to
= hq
->h_to
;
1446 hp
->h_cc
= hq
->h_cc
;
1447 hp
->h_bcc
= hq
->h_bcc
;
1448 hp
->h_from
= hq
->h_from
;
1449 hp
->h_reply_to
= hq
->h_reply_to
;
1450 hp
->h_sender
= hq
->h_sender
;
1451 if (hq
->h_subject
!= NULL
|| !(n_psonce
& n_PSO_t_FLAG
) ||
1452 !(n_poption
& n_PO_t_FLAG
))
1453 hp
->h_subject
= hq
->h_subject
;
1454 hp
->h_user_headers
= hq
->h_user_headers
;
1456 if (n_psonce
& n_PSO_t_FLAG
) {
1457 hp
->h_ref
= hq
->h_ref
;
1458 hp
->h_message_id
= hq
->h_message_id
;
1459 hp
->h_in_reply_to
= hq
->h_in_reply_to
;
1460 hp
->h_mft
= hq
->h_mft
;
1462 /* And perform additional validity checks so that we don't bail later
1463 * on TODO this is good and the place where this should occur,
1464 * TODO unfortunately a lot of other places do again and blabla */
1465 if (hp
->h_from
== NULL
)
1466 hp
->h_from
= n_poption_arg_r
;
1467 else if (hp
->h_from
->n_flink
!= NULL
&& hp
->h_sender
== NULL
)
1468 hp
->h_sender
= lextract(ok_vlook(sender
),
1469 GEXTRA
| GFULL
| GFULLEXTRA
);
1472 n_err(_("Restoring deleted header lines\n"));
1474 if (linebuf
!= NULL
)
1480 hfield_mult(char const *field
, struct message
*mp
, int mult
)
1485 size_t linesize
= 0; /* TODO line pool */
1486 char *linebuf
= NULL
, *colon
;
1490 /* There are (spam) messages which have header bytes which are many KB when
1491 * joined, so resize a single heap storage until we are done if we shall
1492 * collect a field that may have multiple bodies; only otherwise use the
1493 * string dope directly */
1494 memset(&hfs
, 0, sizeof hfs
);
1496 if ((ibuf
= setinput(&mb
, mp
, NEED_HEADER
)) == NULL
)
1498 if ((lc
= mp
->m_lines
- 1) < 0)
1501 if ((mp
->m_flag
& MNOFROM
) == 0 &&
1502 readline_restart(ibuf
, &linebuf
, &linesize
, 0) < 0)
1505 if ((lc
= a_gethfield(ibuf
, &linebuf
, &linesize
, lc
, &colon
)) < 0)
1507 if ((hfield
= thisfield(linebuf
, field
)) != NULL
&& *hfield
!= '\0') {
1509 n_str_add_buf(&hfs
, hfield
, strlen(hfield
));
1511 hfs
.s
= savestr(hfield
);
1518 if (linebuf
!= NULL
)
1520 if (mult
&& hfs
.s
!= NULL
) {
1521 colon
= savestrbuf(hfs
.s
, hfs
.l
);
1530 thisfield(char const *linebuf
, char const *field
)
1532 char const *rv
= NULL
;
1535 while (lowerconv(*linebuf
) == lowerconv(*field
)) {
1542 while (blankchar(*linebuf
))
1544 if (*linebuf
++ != ':')
1547 while (blankchar(*linebuf
)) /* TODO header parser.. strip trailing WS?!? */
1556 nameof(struct message
*mp
, int reptype
)
1561 cp
= skin(name1(mp
, reptype
));
1562 if (reptype
!= 0 || charcount(cp
, '!') < 2)
1564 cp2
= strrchr(cp
, '!');
1566 while (cp2
> cp
&& *cp2
!= '!')
1576 skip_comment(char const *cp
)
1581 for (nesting
= 1; nesting
> 0 && *cp
; ++cp
) {
1600 routeaddr(char const *name
)
1602 char const *np
, *rp
= NULL
;
1605 for (np
= name
; *np
; np
++) {
1608 np
= skip_comment(np
+ 1) - 1;
1614 if (*np
== '\\' && np
[1])
1631 FL
enum expand_addr_flags
1632 expandaddr_to_eaf(void)
1635 char const *eafd_name
;
1636 bool_t eafd_is_target
;
1640 {"restrict", FAL0
, EAF_TARGET_MASK
, EAF_RESTRICT
| EAF_RESTRICT_TARGETS
},
1641 {"fail", FAL0
, EAF_NONE
, EAF_FAIL
},
1642 {"failinvaddr", FAL0
, EAF_NONE
, EAF_FAILINVADDR
| EAF_ADDR
},
1643 {"all", TRU1
, EAF_NONE
, EAF_TARGET_MASK
},
1644 {"file", TRU1
, EAF_NONE
, EAF_FILE
},
1645 {"pipe", TRU1
, EAF_NONE
, EAF_PIPE
},
1646 {"name", TRU1
, EAF_NONE
, EAF_NAME
},
1647 {"addr", TRU1
, EAF_NONE
, EAF_ADDR
}
1651 enum expand_addr_flags rv
;
1655 if ((cp
= ok_vlook(expandaddr
)) == NULL
)
1656 rv
= EAF_RESTRICT_TARGETS
;
1657 else if (*cp
== '\0')
1658 rv
= EAF_TARGET_MASK
;
1660 rv
= EAF_TARGET_MASK
;
1662 for (buf
= savestr(cp
); (cp
= n_strsep(&buf
, ',', TRU1
)) != NULL
;) {
1665 if ((minus
= (*cp
== '-')) || *cp
== '+')
1667 for (eafp
= eafa
;; ++eafp
) {
1668 if (eafp
== eafa
+ n_NELEM(eafa
)) {
1669 if (n_poption
& n_PO_D_V
)
1670 n_err(_("Unknown *expandaddr* value: %s\n"), cp
);
1672 } else if (!asccasecmp(cp
, eafp
->eafd_name
)) {
1674 rv
&= ~eafp
->eafd_andoff
;
1675 rv
|= eafp
->eafd_or
;
1677 if (eafp
->eafd_is_target
)
1678 rv
&= ~eafp
->eafd_or
;
1679 else if (n_poption
& n_PO_D_V
)
1680 n_err(_("minus - prefix invalid for *expandaddr* value: "
1684 } else if (!asccasecmp(cp
, "noalias")) { /* TODO v15 OBSOLETE */
1685 n_OBSOLETE(_("*expandaddr*: noalias is henceforth -name"));
1692 if((rv
& EAF_RESTRICT
) && ((n_psonce
& n_PSO_INTERACTIVE
) ||
1693 (n_poption
& n_PO_TILDE_FLAG
)))
1694 rv
|= EAF_TARGET_MASK
;
1695 else if(n_poption
& n_PO_D_V
){
1696 if(!(rv
& EAF_TARGET_MASK
))
1697 n_err(_("*expandaddr* doesn't allow any addressees\n"));
1698 else if((rv
& EAF_FAIL
) && (rv
& EAF_TARGET_MASK
) == EAF_TARGET_MASK
)
1699 n_err(_("*expandaddr* with fail, but no restrictions to apply\n"));
1707 is_addr_invalid(struct name
*np
, enum expand_addr_check_mode eacm
)
1709 char cbuf
[sizeof "'\\U12340'"];
1713 enum expand_addr_flags eaf
;
1716 eaf
= expandaddr_to_eaf();
1719 if ((rv
= ((f
& NAME_ADDRSPEC_INVALID
) != 0))) {
1720 if (eaf
& EAF_FAILINVADDR
)
1723 if ((eacm
& EACM_NOLOG
) || (f
& NAME_ADDRSPEC_ERR_EMPTY
)) {
1727 char const *fmt
= "'\\x%02X'";
1728 bool_t ok8bit
= TRU1
;
1730 if (f
& NAME_ADDRSPEC_ERR_IDNA
) {
1731 cs
= _("Invalid domain name: %s, character %s\n");
1734 } else if (f
& NAME_ADDRSPEC_ERR_ATSEQ
)
1735 cs
= _("%s contains invalid %s sequence\n");
1736 else if (f
& NAME_ADDRSPEC_ERR_NAME
) {
1737 cs
= _("%s is an invalid alias name\n");
1739 cs
= _("%s contains invalid byte %s\n");
1741 c
= NAME_ADDRSPEC_ERR_GETWC(f
);
1742 snprintf(cbuf
, sizeof cbuf
,
1743 (ok8bit
&& c
>= 040 && c
<= 0177 ? "'%c'" : fmt
), c
);
1749 /* *expandaddr* stuff */
1750 if (!(rv
= ((eacm
& EACM_MODE_MASK
) != EACM_NONE
)))
1753 if ((eacm
& EACM_STRICT
) && (f
& NAME_ADDRSPEC_ISFILEORPIPE
)) {
1756 cs
= _("%s%s: file or pipe addressees not allowed here\n");
1757 if (eacm
& EACM_NOLOG
)
1763 eaf
|= (eacm
& EAF_TARGET_MASK
);
1764 if (eacm
& EACM_NONAME
)
1767 if (eaf
== EAF_NONE
) {
1774 if (!(eaf
& EAF_FILE
) && (f
& NAME_ADDRSPEC_ISFILE
)) {
1775 cs
= _("%s%s: *expandaddr* doesn't allow file target\n");
1776 if (eacm
& EACM_NOLOG
)
1778 } else if (!(eaf
& EAF_PIPE
) && (f
& NAME_ADDRSPEC_ISPIPE
)) {
1779 cs
= _("%s%s: *expandaddr* doesn't allow command pipe target\n");
1780 if (eacm
& EACM_NOLOG
)
1782 } else if (!(eaf
& EAF_NAME
) && (f
& NAME_ADDRSPEC_ISNAME
)) {
1783 cs
= _("%s%s: *expandaddr* doesn't allow user name target\n");
1784 if (eacm
& EACM_NOLOG
)
1786 } else if (!(eaf
& EAF_ADDR
) && (f
& NAME_ADDRSPEC_ISADDR
)) {
1787 cs
= _("%s%s: *expandaddr* doesn't allow mail address target\n");
1788 if (eacm
& EACM_NOLOG
)
1798 n_err(cs
, n_shexp_quote_cp(np
->n_name
, TRU1
), cbuf
);
1805 skin(char const *name
)
1807 struct n_addrguts ag
;
1812 /*name =*/ n_addrspec_with_guts(&ag
, name
, TRU1
, FAL0
);
1814 if(!(ag
.ag_n_flags
& NAME_NAME_SALLOC
))
1815 rv
= savestrbuf(rv
, ag
.ag_slen
);
1822 /* TODO addrspec_with_guts: RFC 5322
1823 * TODO addrspec_with_guts: trim whitespace ETC. ETC. ETC.!!! */
1825 n_addrspec_with_guts(struct n_addrguts
*agp
, char const *name
, bool_t doskin
,
1826 bool_t issingle_hack
){
1828 char *cp2
, *bufend
, *nbuf
, c
;
1838 memset(agp
, 0, sizeof *agp
);
1840 if((agp
->ag_input
= name
) == NULL
|| (agp
->ag_ilen
= strlen(name
)) == 0){
1841 agp
->ag_skinned
= n_UNCONST(n_empty
); /* ok: NAME_SALLOC is not set */
1843 NAME_ADDRSPEC_ERR_SET(agp
->ag_n_flags
, NAME_ADDRSPEC_ERR_EMPTY
, 0);
1846 /*agp->ag_iaddr_start = 0;*/
1847 agp
->ag_iaddr_aend
= agp
->ag_ilen
;
1848 agp
->ag_skinned
= n_UNCONST(name
); /* (NAME_SALLOC not set) */
1849 agp
->ag_slen
= agp
->ag_ilen
;
1850 agp
->ag_n_flags
= NAME_SKINNED
;
1855 nbuf
= n_lofi_alloc(agp
->ag_ilen
+1);
1856 /*agp->ag_iaddr_start = 0;*/
1857 cp2
= bufend
= nbuf
;
1859 /* TODO This is complete crap and should use a token parser */
1860 for(cp
= name
++; (c
= *cp
++) != '\0';){
1863 cp
= skip_comment(cp
);
1867 /* Start of a "quoted-string". Copy it in its entirety */
1868 /* XXX RFC: quotes are "semantically invisible"
1869 * XXX But it was explicitly added (Changelog.Heirloom,
1870 * XXX [9.23] released 11/15/00, "Do not remove quotes
1871 * XXX when skinning names"? No more info.. */
1873 while ((c
= *cp
) != '\0') { /* TODO improve */
1881 else if ((c
= *cp
) != '\0') {
1890 if((flags
& (a_GOTADDR
| a_GOTSPACE
)) == a_GOTADDR
){
1891 flags
|= a_GOTSPACE
;
1892 agp
->ag_iaddr_aend
= PTR2SIZE(cp
- name
);
1894 if (cp
[0] == 'a' && cp
[1] == 't' && blankchar(cp
[2]))
1895 cp
+= 3, *cp2
++ = '@';
1896 else if (cp
[0] == '@' && blankchar(cp
[1]))
1897 cp
+= 2, *cp2
++ = '@';
1902 agp
->ag_iaddr_start
= PTR2SIZE(cp
- (name
- 1));
1904 flags
&= ~(a_GOTSPACE
| a_LASTSP
);
1905 flags
|= a_GOTLT
| a_GOTADDR
;
1908 if(flags
& a_GOTLT
){
1909 /* (_addrspec_check() verifies these later!) */
1910 flags
&= ~(a_GOTLT
| a_LASTSP
);
1911 agp
->ag_iaddr_aend
= PTR2SIZE(cp
- name
);
1913 /* Skip over the entire remaining field */
1914 while((c
= *cp
) != '\0' && c
!= ','){
1917 cp
= skip_comment(cp
);
1919 while ((c
= *cp
) != '\0') {
1923 if (c
== '\\' && *cp
!= '\0')
1931 if(flags
& a_LASTSP
){
1933 if(flags
& a_GOTADDR
)
1937 /* This character is forbidden here, but it may nonetheless be
1938 * present: ensure we turn this into something valid! (E.g., if the
1939 * next character would be a "..) */
1940 if(c
== '\\' && *cp
!= '\0')
1942 if(c
== ',' && !issingle_hack
){
1943 if(!(flags
& a_GOTLT
)){
1945 for(; blankchar(*cp
); ++cp
)
1950 }else if(!(flags
& a_GOTADDR
)){
1952 agp
->ag_iaddr_start
= PTR2SIZE(cp
- name
);
1957 agp
->ag_slen
= PTR2SIZE(cp2
- nbuf
);
1958 if (agp
->ag_iaddr_aend
== 0)
1959 agp
->ag_iaddr_aend
= agp
->ag_ilen
;
1961 else if (agp
->ag_iaddr_aend
< agp
->ag_iaddr_start
) {
1962 cp2
= n_autorec_alloc(agp
->ag_ilen
+ 1 +1);
1963 memcpy(cp2
, agp
->ag_input
, agp
->ag_ilen
);
1964 agp
->ag_iaddr_aend
= agp
->ag_ilen
;
1965 cp2
[agp
->ag_ilen
++] = '>';
1966 cp2
[agp
->ag_ilen
] = '\0';
1968 agp
->ag_skinned
= savestrbuf(nbuf
, agp
->ag_slen
);
1970 agp
->ag_n_flags
= NAME_NAME_SALLOC
| NAME_SKINNED
;
1972 if(a_head_addrspec_check(agp
, doskin
, issingle_hack
) <= FAL0
)
1975 name
= agp
->ag_input
;
1982 realname(char const *name
)
1984 char const *cp
, *cq
, *cstart
= NULL
, *cend
= NULL
;
1987 int quoted
, good
, nogood
;
1990 if ((cp
= n_UNCONST(name
)) == NULL
)
1992 for (; *cp
!= '\0'; ++cp
) {
1995 if (cstart
!= NULL
) {
1996 /* More than one comment in address, doesn't make sense to display
1997 * it without context. Return the entire field */
1998 cp
= mime_fromaddr(name
);
2002 cp
= skip_comment(cp
);
2005 cend
= cstart
= NULL
;
2011 if (*cp
== '\\' && cp
[1])
2022 /* More than one address. Just use the first one */
2028 if (cstart
== NULL
) {
2030 /* If name contains only a route-addr, the surrounding angle brackets
2031 * don't serve any useful purpose when displaying, so remove */
2032 cp
= prstr(skin(name
));
2034 cp
= mime_fromaddr(name
);
2038 /* Strip quotes. Note that quotes that appear within a MIME encoded word are
2039 * not stripped. The idea is to strip only syntactical relevant things (but
2040 * this is not necessarily the most sensible way in practice) */
2041 rp
= rname
= ac_alloc(PTR2SIZE(cend
- cstart
+1));
2043 for (cp
= cstart
; cp
< cend
; ++cp
) {
2044 if (*cp
== '(' && !quoted
) {
2045 cq
= skip_comment(++cp
);
2046 if (PTRCMP(--cq
, >, cend
))
2049 if (*cp
== '\\' && PTRCMP(cp
+ 1, <, cq
))
2053 } else if (*cp
== '\\' && PTRCMP(cp
+ 1, <, cend
))
2055 else if (*cp
== '"') {
2064 mime_fromhdr(&in
, &out
, TD_ISPR
| TD_ICONV
);
2066 rname
= savestr(out
.s
);
2069 while (blankchar(*rname
))
2071 for (rp
= rname
; *rp
!= '\0'; ++rp
)
2073 while (PTRCMP(--rp
, >=, rname
) && blankchar(*rp
))
2076 cp
= mime_fromaddr(name
);
2080 /* mime_fromhdr() has converted all nonprintable characters to question
2081 * marks now. These and blanks are considered uninteresting; if the
2082 * displayed part of the real name contains more than 25% of them, it is
2083 * probably better to display the plain email address instead */
2086 for (rp
= rname
; *rp
!= '\0' && PTRCMP(rp
, <, rname
+ 20); ++rp
)
2087 if (*rp
== '?' || blankchar(*rp
))
2091 cp
= (good
* 3 < nogood
) ? prstr(skin(name
)) : rname
;
2094 return n_UNCONST(cp
);
2098 name1(struct message
*mp
, int reptype
)
2100 char *namebuf
, *cp
, *cp2
, *linebuf
= NULL
/* TODO line pool */;
2101 size_t namesize
, linesize
= 0;
2106 if ((cp
= hfield1("from", mp
)) != NULL
&& *cp
!= '\0')
2108 if (reptype
== 0 && (cp
= hfield1("sender", mp
)) != NULL
&& *cp
!= '\0')
2111 namebuf
= smalloc(namesize
= 1);
2113 if (mp
->m_flag
& MNOFROM
)
2115 if ((ibuf
= setinput(&mb
, mp
, NEED_HEADER
)) == NULL
)
2117 if (readline_restart(ibuf
, &linebuf
, &linesize
, 0) < 0)
2121 if (namesize
<= linesize
)
2122 namebuf
= srealloc(namebuf
, namesize
= linesize
+1);
2123 for (cp
= linebuf
; *cp
!= '\0' && *cp
!= ' '; ++cp
)
2125 for (; blankchar(*cp
); ++cp
)
2127 for (cp2
= namebuf
+ strlen(namebuf
);
2128 *cp
&& !blankchar(*cp
) && PTRCMP(cp2
, <, namebuf
+ namesize
-1);)
2132 if (readline_restart(ibuf
, &linebuf
, &linesize
, 0) < 0)
2134 if ((cp
= strchr(linebuf
, 'F')) == NULL
)
2136 if (strncmp(cp
, "From", 4))
2138 if (namesize
<= linesize
)
2139 namebuf
= srealloc(namebuf
, namesize
= linesize
+ 1);
2141 while ((cp
= strchr(cp
, 'r')) != NULL
) {
2142 if (!strncmp(cp
, "remote", 6)) {
2143 if ((cp
= strchr(cp
, 'f')) == NULL
)
2145 if (strncmp(cp
, "from", 4) != 0)
2147 if ((cp
= strchr(cp
, ' ')) == NULL
)
2151 strncpy(namebuf
, cp
, namesize
);
2154 cp2
= strrchr(namebuf
, '!') + 1;
2155 strncpy(cp2
, cp
, PTR2SIZE(namebuf
+ namesize
- cp2
));
2157 namebuf
[namesize
- 2] = '!';
2158 namebuf
[namesize
- 1] = '\0';
2164 if (*namebuf
!= '\0' || ((cp
= hfield1("return-path", mp
))) == NULL
||
2166 cp
= savestr(namebuf
);
2168 if (linebuf
!= NULL
)
2177 subject_re_trim(char const *s
){
2181 }const *pp
, ignored
[] = { /* Update *reply-strings* manual upon change! */
2183 {3, "aw:"}, {5, "antw:"}, /* de */
2184 {3, "wg:"}, /* Seen too often in the wild */
2189 char *re_st
, *re_st_x
;
2199 if((re_st_x
= ok_vlook(reply_strings
)) != NULL
&&
2200 (re_l
= strlen(re_st_x
)) > 0){
2201 re_st
= n_lofi_alloc(++re_l
* 2);
2202 memcpy(re_st
, re_st_x
, re_l
);
2207 while(spacechar(*s
))
2210 for(pp
= ignored
; pp
->len
> 0; ++pp
)
2211 if(is_asccaseprefix(pp
->dat
, s
)){
2220 memcpy(re_st_x
= &re_st
[re_l
], re_st
, re_l
);
2221 while((cp
= n_strsep(&re_st_x
, ',', TRU1
)) != NULL
)
2222 if(is_asccaseprefix(cp
, s
)){
2234 return any
? s
: orig_s
;
2238 msgidcmp(char const *s1
, char const *s2
)
2240 int q1
= 0, q2
= 0, c1
, c2
;
2249 c1
= msgidnextc(&s1
, &q1
);
2250 c2
= msgidnextc(&s2
, &q2
);
2259 fakefrom(struct message
*mp
)
2264 if (((name
= skin(hfield1("return-path", mp
))) == NULL
|| *name
== '\0' ) &&
2265 ((name
= skin(hfield1("from", mp
))) == NULL
|| *name
== '\0'))
2266 /* XXX MAILER-DAEMON is what an old MBOX manual page says.
2267 * RFC 4155 however requires a RFC 5322 (2822) conforming
2268 * "addr-spec", but we simply can't provide that */
2269 name
= "MAILER-DAEMON";
2274 #if defined HAVE_IMAP_SEARCH || defined HAVE_IMAP
2276 unixtime(char const *fromline
)
2278 char const *fp
, *xp
;
2280 si32_t i
, year
, month
, day
, hour
, minute
, second
, tzdiff
;
2284 for (fp
= fromline
; *fp
!= '\0' && *fp
!= '\n'; ++fp
)
2287 if (PTR2SIZE(fp
- fromline
) < 7)
2292 if (!strncmp(fp
+ 4, n_month_names
[i
], 3))
2294 if (n_month_names
[++i
][0] == '\0')
2300 n_idec_si32_cp(&day
, &fp
[8], 10, &xp
);
2301 if (*xp
!= ' ' || xp
!= fp
+ 10)
2303 n_idec_si32_cp(&hour
, &fp
[11], 10, &xp
);
2304 if (*xp
!= ':' || xp
!= fp
+ 13)
2306 n_idec_si32_cp(&minute
, &fp
[14], 10, &xp
);
2307 if (*xp
!= ':' || xp
!= fp
+ 16)
2309 n_idec_si32_cp(&second
, &fp
[17], 10, &xp
);
2310 if (*xp
!= ' ' || xp
!= fp
+ 19)
2312 n_idec_si32_cp(&year
, &fp
[20], 10, &xp
);
2315 if ((t
= combinetime(year
, month
, day
, hour
, minute
, second
)) == (time_t)-1)
2317 if((t2
= mktime(gmtime(&t
))) == (time_t)-1)
2320 if((tmptr
= localtime(&t
)) == NULL
)
2322 if (tmptr
->tm_isdst
> 0)
2323 tzdiff
+= 3600; /* TODO simply adding an hour for ISDST is .. buuh */
2332 #endif /* HAVE_IMAP_SEARCH || HAVE_IMAP */
2335 rfctime(char const *date
) /* TODO n_idec_ return tests */
2339 si32_t i
, year
, month
, day
, hour
, minute
, second
;
2344 if ((cp
= nexttoken(cp
)) == NULL
)
2346 if (alphachar(cp
[0]) && alphachar(cp
[1]) && alphachar(cp
[2]) &&
2348 if ((cp
= nexttoken(&cp
[4])) == NULL
)
2351 n_idec_si32_cp(&day
, cp
, 10, &x
);
2352 if ((cp
= nexttoken(x
)) == NULL
)
2355 if (!strncmp(cp
, n_month_names
[i
], 3))
2357 if (n_month_names
[++i
][0] == '\0')
2361 if ((cp
= nexttoken(&cp
[3])) == NULL
)
2364 * Where a two or three digit year occurs in a date, the year is to be
2365 * interpreted as follows: If a two digit year is encountered whose
2366 * value is between 00 and 49, the year is interpreted by adding 2000,
2367 * ending up with a value between 2000 and 2049. If a two digit year
2368 * is encountered with a value between 50 and 99, or any three digit
2369 * year is encountered, the year is interpreted by adding 1900 */
2370 n_idec_si32_cp(&year
, cp
, 10, &x
);
2371 i
= (int)PTR2SIZE(x
- cp
);
2372 if (i
== 2 && year
>= 0 && year
<= 49)
2374 else if (i
== 3 || (i
== 2 && year
>= 50 && year
<= 99))
2376 if ((cp
= nexttoken(x
)) == NULL
)
2378 n_idec_si32_cp(&hour
, cp
, 10, &x
);
2382 n_idec_si32_cp(&minute
, cp
, 10, &x
);
2385 n_idec_si32_cp(&second
, cp
, 10, &x
);
2389 if ((t
= combinetime(year
, month
, day
, hour
, minute
, second
)) == (time_t)-1)
2391 if ((cp
= nexttoken(x
)) != NULL
) {
2403 if (digitchar(cp
[0]) && digitchar(cp
[1]) && digitchar(cp
[2]) &&
2410 n_idec_si32_cp(&i
, buf
, 10, NULL
);
2411 tadj
= (si64_t
)i
* 3600; /* XXX */
2414 n_idec_si32_cp(&i
, buf
, 10, NULL
);
2415 tadj
+= (si64_t
)i
* 60; /* XXX */
2420 /* TODO WE DO NOT YET PARSE (OBSOLETE) ZONE NAMES
2421 * TODO once again, Christos Zoulas and NetBSD Mail have done
2422 * TODO a really good job already, but using strptime(3), which
2423 * TODO is not portable. Nonetheless, WE must improve, not
2424 * TODO at last because we simply ignore obsolete timezones!!
2425 * TODO See RFC 5322, 4.3! */
2436 combinetime(int year
, int month
, int day
, int hour
, int minute
, int second
){
2437 size_t const jdn_epoch
= 2440588;
2438 bool_t
const y2038p
= (sizeof(time_t) == 4);
2444 if(UICMP(32, second
, >/*XXX leap=*/, n_DATE_SECSMIN
) ||
2445 UICMP(32, minute
, >=, n_DATE_MINSHOUR
) ||
2446 UICMP(32, hour
, >=, n_DATE_HOURSDAY
) ||
2447 day
< 1 || day
> 31 ||
2448 month
< 1 || month
> 12 ||
2452 if(year
>= 1970 + ((y2038p
? SI32_MAX
: SI64_MAX
) /
2453 (n_DATE_SECSDAY
* n_DATE_DAYSYEAR
))){
2454 /* Be a coward regarding Y2038, many people (mostly myself, that is) do
2455 * test by stepping second-wise around the flip. Don't care otherwise */
2458 if(year
> 2038 || month
> 1 || day
> 19 ||
2459 hour
> 3 || minute
> 14 || second
> 7)
2464 t
+= minute
* n_DATE_SECSMIN
;
2465 t
+= hour
* n_DATE_SECSHOUR
;
2467 jdn
= a_head_gregorian_to_jdn(year
, month
, day
);
2469 t
+= (time_t)jdn
* n_DATE_SECSDAY
;
2479 substdate(struct message
*m
)
2484 /* Determine the date to print in faked 'From ' lines. This is traditionally
2485 * the date the message was written to the mail file. Try to determine this
2486 * using RFC message header fields, or fall back to current time */
2488 if ((cp
= hfield1("received", m
)) != NULL
) {
2489 while ((cp
= nexttoken(cp
)) != NULL
&& *cp
!= ';') {
2492 while (alnumchar(*cp
));
2495 m
->m_time
= rfctime(cp
);
2497 if (m
->m_time
== 0 || m
->m_time
> time_current
.tc_time
) {
2498 if ((cp
= hfield1("date", m
)) != NULL
)
2499 m
->m_time
= rfctime(cp
);
2501 if (m
->m_time
== 0 || m
->m_time
> time_current
.tc_time
)
2502 m
->m_time
= time_current
.tc_time
;
2507 setup_from_and_sender(struct header
*hp
)
2513 /* If -t parsed or composed From: then take it. With -t we otherwise
2514 * want -r to be honoured in favour of *from* in order to have
2515 * a behaviour that is compatible with what users would expect from e.g.
2517 if ((np
= hp
->h_from
) != NULL
||
2518 ((n_psonce
& n_PSO_t_FLAG
) && (np
= n_poption_arg_r
) != NULL
)) {
2520 } else if ((addr
= myaddrs(hp
)) != NULL
)
2521 np
= lextract(addr
, GEXTRA
| GFULL
| GFULLEXTRA
);
2524 if ((np
= hp
->h_sender
) != NULL
) {
2526 } else if ((addr
= ok_vlook(sender
)) != NULL
)
2527 np
= lextract(addr
, GEXTRA
| GFULL
| GFULLEXTRA
);
2533 FL
struct name
const *
2534 check_from_and_sender(struct name
const *fromfield
,
2535 struct name
const *senderfield
)
2537 struct name
const *rv
= NULL
;
2540 if (senderfield
!= NULL
) {
2541 if (senderfield
->n_flink
!= NULL
) {
2542 n_err(_("The Sender: field may contain only one address\n"));
2548 if (fromfield
!= NULL
) {
2549 if (fromfield
->n_flink
!= NULL
&& senderfield
== NULL
) {
2550 n_err(_("A Sender: is required when there are multiple "
2551 "addresses in From:\n"));
2559 rv
= (struct name
*)0x1;
2567 getsender(struct message
*mp
)
2573 if ((cp
= hfield1("from", mp
)) == NULL
||
2574 (np
= lextract(cp
, GEXTRA
| GSKIN
)) == NULL
)
2577 cp
= (np
->n_flink
!= NULL
) ? skin(hfield1("sender", mp
)) : np
->n_name
;
2584 grab_headers(enum n_go_input_flags gif
, struct header
*hp
, enum gfield gflags
,
2587 /* TODO grab_headers: again, check counts etc. against RFC;
2588 * TODO (now assumes check_from_and_sender() is called afterwards ++ */
2594 comma
= (ok_blook(bsdcompat
) || ok_blook(bsdmsgs
)) ? 0 : GCOMMA
;
2597 hp
->h_to
= grab_names(gif
, "To: ", hp
->h_to
, comma
, GTO
| GFULL
);
2598 if (subjfirst
&& (gflags
& GSUBJECT
))
2599 hp
->h_subject
= n_go_input_cp(gif
, "Subject: ", hp
->h_subject
);
2601 hp
->h_cc
= grab_names(gif
, "Cc: ", hp
->h_cc
, comma
, GCC
| GFULL
);
2603 hp
->h_bcc
= grab_names(gif
, "Bcc: ", hp
->h_bcc
, comma
, GBCC
| GFULL
);
2605 if (gflags
& GEXTRA
) {
2606 if (hp
->h_from
== NULL
)
2607 hp
->h_from
= lextract(myaddrs(hp
), GEXTRA
| GFULL
| GFULLEXTRA
);
2608 hp
->h_from
= grab_names(gif
, "From: ", hp
->h_from
, comma
,
2609 GEXTRA
| GFULL
| GFULLEXTRA
);
2610 if (hp
->h_reply_to
== NULL
) {
2611 struct name
*v15compat
;
2613 if((v15compat
= lextract(ok_vlook(replyto
), GEXTRA
| GFULL
)) != NULL
)
2614 n_OBSOLETE(_("please use *reply-to*, not *replyto*"));
2615 hp
->h_reply_to
= lextract(ok_vlook(reply_to
), GEXTRA
| GFULL
);
2616 if(hp
->h_reply_to
== NULL
) /* v15 */
2617 hp
->h_reply_to
= v15compat
;
2619 hp
->h_reply_to
= grab_names(gif
, "Reply-To: ", hp
->h_reply_to
, comma
,
2621 if (hp
->h_sender
== NULL
)
2622 hp
->h_sender
= extract(ok_vlook(sender
), GEXTRA
| GFULL
);
2623 hp
->h_sender
= grab_names(gif
, "Sender: ", hp
->h_sender
, comma
,
2627 if (!subjfirst
&& (gflags
& GSUBJECT
))
2628 hp
->h_subject
= n_go_input_cp(gif
, "Subject: ", hp
->h_subject
);
2635 n_header_match(struct message
*mp
, struct search_expr
const *sep
){
2636 struct str fiter
, in
, out
;
2641 char **linebuf
, *colon
;
2642 enum {a_NONE
, a_ALL
, a_ITER
, a_RE
} match
;
2648 linebuf
= &termios_state
.ts_linebuf
; /* XXX line pool */
2649 linesize
= &termios_state
.ts_linesize
; /* XXX line pool */
2650 n_UNINIT(fiter
.l
, 0);
2651 n_UNINIT(fiter
.s
, NULL
);
2653 if((ibuf
= setinput(&mb
, mp
, NEED_HEADER
)) == NULL
)
2655 if((lc
= mp
->m_lines
- 1) < 0)
2658 if((mp
->m_flag
& MNOFROM
) == 0 &&
2659 readline_restart(ibuf
, linebuf
, linesize
, 0) < 0)
2663 if((field
= sep
->ss_field
) != NULL
){
2664 if(!asccasecmp(field
, "header") || (field
[0] == '<' && field
[1] == '\0'))
2667 fiter
.s
= n_lofi_alloc((fiter
.l
= strlen(field
)) +1);
2671 }else if(sep
->ss_fieldre
!= NULL
){
2677 /* Iterate over all the headers */
2681 if((lc
= a_gethfield(ibuf
, linebuf
, linesize
, lc
, &colon
)) <= 0)
2684 /* Is this a header we are interested in? */
2685 if(match
== a_ITER
){
2688 memcpy(itercp
= fiter
.s
, sep
->ss_field
, fiter
.l
+1);
2689 while((field
= n_strsep(&itercp
, ',', TRU1
)) != NULL
){
2690 /* It may be an abbreviation */
2691 char const x
[][8] = {"from", "to", "cc", "bcc", "subject"};
2695 if(field
[0] != '\0' && field
[1] == '\0'){
2696 c1
= lowerconv(field
[0]);
2697 for(i
= 0; i
< n_NELEM(x
); ++i
){
2705 if(!ascncasecmp(field
, *linebuf
, PTR2SIZE(colon
- *linebuf
)))
2711 }else if(match
== a_RE
){
2715 i
= PTR2SIZE(colon
- *linebuf
);
2716 cp
= n_lofi_alloc(i
+1);
2717 memcpy(cp
, *linebuf
, i
);
2719 i
= (regexec(sep
->ss_fieldre
, cp
, 0,NULL
, 0) != REG_NOMATCH
);
2726 /* It could be a plain existence test */
2727 if(sep
->ss_field_exists
){
2732 /* Need to check the body */
2733 while(blankchar(*++colon
))
2737 /* Shall we split into address list and match as/the addresses only?
2738 * TODO at some later time we should ignore and log efforts to search
2739 * TODO a skinned address list if we know the header has none such */
2741 if((np
= lextract(in
.s
, GSKIN
)) == NULL
)
2746 in
.l
= strlen(in
.s
);
2747 mime_fromhdr(&in
, &out
, TD_ICONV
);
2752 if(sep
->ss_bodyre
!= NULL
)
2753 rv
= (regexec(sep
->ss_bodyre
, out
.s
, 0,NULL
, 0) != REG_NOMATCH
);
2756 rv
= substr(out
.s
, sep
->ss_body
);
2762 if(np
!= NULL
&& (np
= np
->n_flink
) != NULL
){
2770 n_lofi_free(fiter
.s
);
2776 n_header_is_standard(char const *name
, size_t len
){
2777 static char const * const names
[] = {
2778 "Bcc", "Cc", "From",
2779 "In-Reply-To", "Mail-Followup-To",
2780 "Message-ID", "References", "Reply-To",
2781 "Sender", "Subject", "To",
2783 "Mailx-Orig-Bcc", "Mailx-Orig-Cc", "Mailx-Orig-From", "Mailx-Orig-To",
2784 "Mailx-Raw-Bcc", "Mailx-Raw-Cc", "Mailx-Raw-To",
2787 char const * const *rv
;
2793 for(rv
= names
; *rv
!= NULL
; ++rv
)
2794 if(!ascncasecmp(*rv
, name
, len
))
2801 n_header_add_custom(struct n_header_field
**hflp
, char const *dat
,
2806 struct n_header_field
*hfp
;
2811 /* For (-C) convenience, allow leading WS */
2812 while(blankchar(*dat
))
2815 /* Isolate the header field from the body */
2816 for(cp
= dat
;; ++cp
){
2817 if(fieldnamechar(*cp
))
2822 }else if(*cp
!= ':' && !blankchar(*cp
)){
2824 cp
= N_("Invalid custom header (not \"field: body\"): %s\n");
2829 nl
= (ui32_t
)PTR2SIZE(cp
- dat
);
2833 /* Verify the custom header does not use standard/managed field name */
2834 if(n_header_is_standard(dat
, nl
) != NULL
){
2835 cp
= N_("Custom headers cannot use standard header names: %s\n");
2839 /* Skip on over to the body */
2840 while(blankchar(*cp
))
2844 while(blankchar(*cp
))
2846 bl
= (ui32_t
)strlen(cp
);
2847 for(i
= bl
++; i
-- != 0;)
2848 if(cntrlchar(cp
[i
])){
2849 cp
= N_("Invalid custom header: contains control characters: %s\n");
2853 i
= n_VSTRUCT_SIZEOF(struct n_header_field
, hf_dat
) + nl
+1 + bl
;
2854 *hflp
= hfp
= heap
? n_alloc(i
) : n_autorec_alloc(i
);
2855 hfp
->hf_next
= NULL
;
2857 hfp
->hf_bl
= bl
- 1;
2858 memcpy(hfp
->hf_dat
, dat
, nl
);
2859 hfp
->hf_dat
[nl
++] = '\0';
2860 memcpy(hfp
->hf_dat
+ nl
, cp
, bl
);
2863 return (hfp
!= NULL
);
2866 n_err(V_(cp
), n_shexp_quote_cp(dat
, FAL0
));