1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Mail to mail folders and displays.
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
45 PIPE_NULL
, /* No pipe- mimetype handler */
46 PIPE_COMM
, /* Normal command */
47 PIPE_ASYNC
, /* Normal command, run asynchronous */
48 PIPE_TEXT
, /* @ special command to force treatment as text */
49 PIPE_MSG
/* Display message (returned as command string) */
58 static sigjmp_buf _send_pipejmp
;
61 static struct mimepart
*parsemsg(struct message
*mp
, enum parseflags pf
);
62 static enum okay
parsepart(struct message
*zmp
, struct mimepart
*ip
,
63 enum parseflags pf
, int level
);
64 static void parse822(struct message
*zmp
, struct mimepart
*ip
,
65 enum parseflags pf
, int level
);
67 static void parsepkcs7(struct message
*zmp
, struct mimepart
*ip
,
68 enum parseflags pf
, int level
);
70 static void _parsemultipart(struct message
*zmp
,
71 struct mimepart
*ip
, enum parseflags pf
, int level
);
72 static void __newpart(struct mimepart
*ip
, struct mimepart
**np
,
73 off_t offs
, int *part
);
74 static void __endpart(struct mimepart
**np
, off_t xoffs
, long lines
);
76 /* Going for user display, print Part: info string */
77 static void _print_part_info(struct str
*out
, struct mimepart
*mip
,
78 struct ignoretab
*doign
, int level
);
80 /* Query possible pipe command for MIME part */
81 static enum pipeflags
_pipecmd(char **result
, struct mimepart
const *mpp
);
83 /* Create a pipe; if mpp is not NULL, place some PIPEHOOK_* environment
84 * variables accordingly */
85 static FILE * _pipefile(char const *pipecomm
, struct mimepart
const *mpp
,
86 FILE **qbuf
, bool_t quote
, bool_t async
);
88 /* Adjust output statistics */
89 SINLINE
void _addstats(off_t
*stats
, off_t lines
, off_t bytes
);
91 /* Call mime_write() as approbiate and adjust statistics */
92 SINLINE ssize_t
_out(char const *buf
, size_t len
, FILE *fp
,
93 enum conversion convert
, enum sendaction action
,
94 struct quoteflt
*qf
, off_t
*stats
,
98 static void _send_onpipe(int signo
);
101 static int sendpart(struct message
*zmp
, struct mimepart
*ip
,
102 FILE *obuf
, struct ignoretab
*doign
,
103 struct quoteflt
*qf
, enum sendaction action
,
104 off_t
*stats
, int level
);
106 /* Get a file for an attachment */
107 static FILE * newfile(struct mimepart
*ip
, int *ispipe
);
109 static void pipecpy(FILE *pipebuf
, FILE *outbuf
, FILE *origobuf
,
110 struct quoteflt
*qf
, off_t
*stats
);
112 /* Output a reasonable looking status field */
113 static void statusput(const struct message
*mp
, FILE *obuf
,
114 struct quoteflt
*qf
, off_t
*stats
);
115 static void xstatusput(const struct message
*mp
, FILE *obuf
,
116 struct quoteflt
*qf
, off_t
*stats
);
118 static void put_from_(FILE *fp
, struct mimepart
*ip
, off_t
*stats
);
120 static struct mimepart
*
121 parsemsg(struct message
*mp
, enum parseflags pf
)
126 ip
= csalloc(1, sizeof *ip
);
127 ip
->m_flag
= mp
->m_flag
;
128 ip
->m_have
= mp
->m_have
;
129 ip
->m_block
= mp
->m_block
;
130 ip
->m_offset
= mp
->m_offset
;
131 ip
->m_size
= mp
->m_size
;
132 ip
->m_xsize
= mp
->m_xsize
;
133 ip
->m_lines
= mp
->m_lines
;
134 ip
->m_xlines
= mp
->m_lines
;
135 if (parsepart(mp
, ip
, pf
, 0) != OKAY
)
142 parsepart(struct message
*zmp
, struct mimepart
*ip
, enum parseflags pf
,
149 ip
->m_ct_type
= hfield1("content-type", (struct message
*)ip
);
150 if (ip
->m_ct_type
!= NULL
) {
151 cp_b
= ip
->m_ct_type_plain
= savestr(ip
->m_ct_type
);
152 if ((cp
= strchr(cp_b
, ';')) != NULL
)
154 cp
= cp_b
+ strlen(cp_b
);
155 while (cp
> cp_b
&& blankchar(cp
[-1]))
158 } else if (ip
->m_parent
!= NULL
&&
159 ip
->m_parent
->m_mimecontent
== MIME_DIGEST
)
160 ip
->m_ct_type_plain
= UNCONST("message/rfc822");
162 ip
->m_ct_type_plain
= UNCONST("text/plain");
163 ip
->m_ct_type_usr_ovwr
= NULL
;
165 if (ip
->m_ct_type
!= NULL
)
166 ip
->m_charset
= mime_getparam("charset", ip
->m_ct_type
);
167 if (ip
->m_charset
== NULL
)
168 ip
->m_charset
= charset_get_7bit();
170 ip
->m_ct_transfer_enc
= hfield1("content-transfer-encoding",
171 (struct message
*)ip
);
172 ip
->m_mimeenc
= (ip
->m_ct_transfer_enc
!= NULL
)
173 ? mime_getenc(ip
->m_ct_transfer_enc
) : MIME_7B
;
175 if (((cp
= hfield1("content-disposition", (struct message
*)ip
)) == NULL
||
176 (ip
->m_filename
= mime_getparam("filename", cp
)) == NULL
) &&
177 ip
->m_ct_type
!= NULL
)
178 ip
->m_filename
= mime_getparam("name", ip
->m_ct_type
);
180 ip
->m_mimecontent
= mime_classify_content_of_part(ip
);
182 if (pf
& PARSE_PARTS
) {
183 if (level
> 9999) { /* TODO MAGIC */
184 fprintf(stderr
, _("MIME content too deeply nested\n"));
187 switch (ip
->m_mimecontent
) {
189 if (pf
& PARSE_DECRYPT
) {
191 parsepkcs7(zmp
, ip
, pf
, level
);
194 fprintf(stderr
, _("No SSL support compiled in.\n"));
202 case MIME_ALTERNATIVE
:
204 _parsemultipart(zmp
, ip
, pf
, level
);
207 parse822(zmp
, ip
, pf
, level
);
218 parse822(struct message
*zmp
, struct mimepart
*ip
, enum parseflags pf
,
229 if ((ibuf
= setinput(&mb
, (struct message
*)ip
, NEED_BODY
)) == NULL
)
234 while (cnt
&& ((c
= getc(ibuf
)) != EOF
)) {
245 np
= csalloc(1, sizeof *np
);
246 np
->m_flag
= MNOFROM
;
247 np
->m_have
= HAVE_HEADER
| HAVE_BODY
;
248 np
->m_block
= mailx_blockof(offs
);
249 np
->m_offset
= mailx_offsetof(offs
);
250 np
->m_size
= np
->m_xsize
= cnt
;
251 np
->m_lines
= np
->m_xlines
= lines
;
252 np
->m_partstring
= ip
->m_partstring
;
254 ip
->m_multipart
= np
;
256 if (ok_blook(rfc822_body_from_
)) {
257 substdate((struct message
*)np
);
258 np
->m_from
= fakefrom((struct message
*)np
);/* TODO strip MNOFROM flag? */
261 parsepart(zmp
, np
, pf
, level
+ 1);
268 parsepkcs7(struct message
*zmp
, struct mimepart
*ip
, enum parseflags pf
,
271 struct message m
, *xmp
;
276 memcpy(&m
, ip
, sizeof m
);
277 to
= hfield1("to", zmp
);
278 cc
= hfield1("cc", zmp
);
280 if ((xmp
= smime_decrypt(&m
, to
, cc
, 0)) != NULL
) {
281 np
= csalloc(1, sizeof *np
);
282 np
->m_flag
= xmp
->m_flag
;
283 np
->m_have
= xmp
->m_have
;
284 np
->m_block
= xmp
->m_block
;
285 np
->m_offset
= xmp
->m_offset
;
286 np
->m_size
= xmp
->m_size
;
287 np
->m_xsize
= xmp
->m_xsize
;
288 np
->m_lines
= xmp
->m_lines
;
289 np
->m_xlines
= xmp
->m_xlines
;
290 np
->m_partstring
= ip
->m_partstring
;
292 if (parsepart(zmp
, np
, pf
, level
+ 1) == OKAY
) {
294 ip
->m_multipart
= np
;
302 _parsemultipart(struct message
*zmp
, struct mimepart
*ip
, enum parseflags pf
,
305 /* TODO Instead of the recursive multiple run parse we have today,
306 * TODO the send/MIME layer rewrite must create a "tree" of parts with
307 * TODO a single-pass parse, then address each part directly as
308 * TODO necessary; since boundaries start with -- and the content
309 * TODO rather forms a stack this is pretty cheap indeed! */
310 struct mimepart
*np
= NULL
;
311 char *boundary
, *line
= NULL
;
312 size_t linesize
= 0, linelen
, cnt
, boundlen
;
319 if ((boundary
= mime_get_boundary(ip
->m_ct_type
, &linelen
)) == NULL
)
323 if ((ibuf
= setinput(&mb
, (struct message
*)ip
, NEED_BODY
)) == NULL
)
327 while (fgetline(&line
, &linesize
, &cnt
, &linelen
, ibuf
, 0))
332 __newpart(ip
, &np
, offs
, NULL
);
333 while (fgetline(&line
, &linesize
, &cnt
, &linelen
, ibuf
, 0)) {
334 /* XXX linelen includes LF */
335 if (!((lines
> 0 || part
== 0) && linelen
> boundlen
&&
336 !strncmp(line
, boundary
, boundlen
))) {
341 /* Subpart boundary? */
342 if (line
[boundlen
] == '\n') {
345 __endpart(&np
, offs
- boundlen
- 2, lines
);
346 __newpart(ip
, &np
, offs
- boundlen
- 2, NULL
);
348 __endpart(&np
, offs
, 2);
349 __newpart(ip
, &np
, offs
, &part
);
354 /* Final boundary? Be aware of cases where there is no separating
355 * newline in between boundaries, as has been seen in a message with
356 * "Content-Type: multipart/appledouble;" */
357 if (linelen
< boundlen
+ 2)
359 linelen
-= boundlen
+ 2;
360 if (line
[boundlen
] != '-' || line
[boundlen
+ 1] != '-' ||
361 (linelen
> 0 && line
[boundlen
+ 2] != '\n'))
365 __endpart(&np
, offs
- boundlen
- 4, lines
);
366 __newpart(ip
, &np
, offs
- boundlen
- 4, NULL
);
368 __endpart(&np
, offs
+ cnt
, 2);
373 __endpart(&np
, offs
, lines
);
376 for (np
= ip
->m_multipart
; np
!= NULL
; np
= np
->m_nextpart
)
377 if (np
->m_mimecontent
!= MIME_DISCARD
)
378 parsepart(zmp
, np
, pf
, level
+ 1);
385 __newpart(struct mimepart
*ip
, struct mimepart
**np
, off_t offs
, int *part
)
391 *np
= csalloc(1, sizeof **np
);
392 (*np
)->m_flag
= MNOFROM
;
393 (*np
)->m_have
= HAVE_HEADER
| HAVE_BODY
;
394 (*np
)->m_block
= mailx_blockof(offs
);
395 (*np
)->m_offset
= mailx_offsetof(offs
);
399 sz
= (ip
->m_partstring
!= NULL
) ? strlen(ip
->m_partstring
) : 0;
401 (*np
)->m_partstring
= salloc(sz
);
402 if (ip
->m_partstring
)
403 snprintf((*np
)->m_partstring
, sz
, "%s.%u", ip
->m_partstring
, *part
);
405 snprintf((*np
)->m_partstring
, sz
, "%u", *part
);
407 (*np
)->m_mimecontent
= MIME_DISCARD
;
408 (*np
)->m_parent
= ip
;
410 if (ip
->m_multipart
) {
411 for (pp
= ip
->m_multipart
; pp
->m_nextpart
!= NULL
; pp
= pp
->m_nextpart
)
413 pp
->m_nextpart
= *np
;
415 ip
->m_multipart
= *np
;
420 __endpart(struct mimepart
**np
, off_t xoffs
, long lines
)
425 offs
= mailx_positionof((*np
)->m_block
, (*np
)->m_offset
);
426 (*np
)->m_size
= (*np
)->m_xsize
= xoffs
- offs
;
427 (*np
)->m_lines
= (*np
)->m_xlines
= lines
;
433 _print_part_info(struct str
*out
, struct mimepart
*mip
,
434 struct ignoretab
*doign
, int level
)
436 struct str ct
= {NULL
, 0}, cd
= {NULL
, 0};
438 struct str
const *cpre
, *csuf
;
442 if (is_ign("content-type", 12, doign
)) {
445 if ((out
->s
= mip
->m_ct_type_usr_ovwr
) != NULL
)
449 out
->s
= mip
->m_ct_type_plain
;
451 out
->l
= strlen(out
->s
);
453 ct
.s
= ac_alloc(out
->l
+ 2 + addon
+1);
462 if (is_prefix("application/", out
->s
)) {
463 memcpy(ct
.s
+ ct
.l
, "appl../", 7);
467 out
->l
= MIN(out
->l
, 17 - addon
);
469 out
->l
= MIN(out
->l
, 24 - addon
);
470 memcpy(ct
.s
+ ct
.l
, out
->s
, out
->l
);
476 if (is_ign("content-disposition", 19, doign
) && mip
->m_filename
!= NULL
) {
479 ti
.l
= strlen(ti
.s
= mip
->m_filename
);
480 mime_fromhdr(&ti
, &to
, TD_ISPR
| TD_ICONV
| TD_DELCTRL
);
482 cd
.s
= ac_alloc(2 + 32 +1); /* FIXME was 25.. UNI: USE VISUAL WIDTH!!! */
485 cd
.l
= 2 + field_put_bidi_clip(cd
.s
+ 2, 32 +1, to
.s
, to
.l
);
490 /* Take care of "99.99", i.e., 5 */
491 if ((ps
= mip
->m_partstring
) == NULL
|| ps
[0] == '\0')
495 cpre
= colour_get(COLOURSPEC_PARTINFO
);
496 csuf
= colour_get(COLOURSPEC_RESET
);
501 /* Assume maximum possible sizes for 64 bit integers here to avoid any
502 * buffer overflows just in case we have a bug somewhere and / or the
503 * snprintf() is our internal version that doesn't really provide hard
504 * buffer cuts TODO ensure upper bound on numbers, use 9999999 else */
505 #define __msg "%s%s[-- #%s : %lu/%lu%s%s --]%s\n"
506 out
->l
= sizeof(__msg
) +
508 (cpre
!= NULL
? cpre
->l
+ csuf
->l
: 0) +
510 strlen(ps
) + 2*21 + ct
.l
+ cd
.l
+1;
511 out
->s
= salloc(out
->l
);
512 out
->l
= snprintf(out
->s
, out
->l
, __msg
,
513 (level
|| (ps
[0] != '1' && ps
[1] == '\0') ? "\n" : ""),
514 (cpre
!= NULL
? cpre
->s
: ""),
515 ps
, (ul_it
)mip
->m_lines
, (ul_it
)mip
->m_size
,
516 (ct
.s
!= NULL
? ct
.s
: ""),
517 (cd
.s
!= NULL
? cd
.s
: ""),
518 (csuf
!= NULL
? csuf
->s
: ""));
519 out
->s
[out
->l
] = '\0';
529 static enum pipeflags
530 _pipecmd(char **result
, struct mimepart
const *mpp
)
536 /* Do we have any handler for this part? */
537 if ((cp
= mimepart_get_handler(mpp
)) == NULL
) {
541 /* User specified a command, inspect for special cases */
542 else if (cp
[0] != '@') {
543 /* Normal command line */
546 } else if (*++cp
== '\0') {
547 /* Treat as plain text */
549 } else if (!msglist_is_single
) {
550 /* Viewing multiple messages in one go, don't block system */
552 *result
= UNCONST(_("[Directly address message only to display this]\n"));
554 /* Viewing a single message only */
555 /* TODO send/MIME layer rewrite: when we have a single-pass parser
556 * TODO then the parsing phase and the send phase will be separated;
557 * TODO that allows us to ask a user *before* we start the send, i.e.,
558 * TODO *before* a pager pipe is setup */
560 /* Asynchronous command, normal command line */
561 ret
= PIPE_ASYNC
, *result
= ++cp
;
563 ret
= PIPE_COMM
, *result
= cp
;
570 _pipefile(char const *pipecomm
, struct mimepart
const *mpp
, FILE **qbuf
,
571 bool_t quote
, bool_t async
)
574 char const *env_addon
[8], *sh
;
582 if ((*qbuf
= Ftmp(NULL
, "sendp", OF_RDWR
| OF_UNLINK
| OF_REGISTER
,
584 perror(_("tmpfile"));
591 if (mpp
== NULL
|| (cp
= mpp
->m_filename
) == NULL
)
593 env_addon
[0] = str_concat_csvl(&s
, PIPEHOOK_FILENAME
, "=", cp
, NULL
)->s
;
595 /* NAIL_FILENAME_GENERATED */
596 s
.s
= getrandstring(8);
599 else if (*cp
== '\0') {
600 if ( (((cp
= mpp
->m_ct_type_usr_ovwr
) == NULL
|| *cp
== '\0') &&
601 ((cp
= mpp
->m_ct_type_plain
) == NULL
|| *cp
== '\0')) ||
602 ((sh
= strrchr(cp
, '/')) == NULL
|| *++sh
== '\0'))
606 cp
= savecat(cp
, sh
);
609 env_addon
[1] = str_concat_csvl(&s
, PIPEHOOK_FILENAME_GENERATED
, "=", cp
,
612 /* NAIL_CONTENT{,_EVIDENCE} */
613 if (mpp
== NULL
|| (cp
= mpp
->m_ct_type_plain
) == NULL
)
615 env_addon
[2] = str_concat_csvl(&s
, PIPEHOOK_CONTENT
, "=", cp
, NULL
)->s
;
617 if (mpp
!= NULL
&& mpp
->m_ct_type_usr_ovwr
!= NULL
)
618 cp
= mpp
->m_ct_type_usr_ovwr
;
619 env_addon
[3] = str_concat_csvl(&s
, PIPEHOOK_CONTENT_EVIDENCE
, "=", cp
,
624 if ((sh
= ok_vlook(SHELL
)) == NULL
)
626 if ((rbuf
= Popen(pipecomm
, "W", sh
, env_addon
, (async
? -1 : fileno(*qbuf
)))
639 _addstats(off_t
*stats
, off_t lines
, off_t bytes
)
651 _out(char const *buf
, size_t len
, FILE *fp
, enum conversion convert
, enum
652 sendaction action
, struct quoteflt
*qf
, off_t
*stats
, struct str
*rest
)
660 Well
... it turns out to
not work like that since of course a valid
661 RFC
4155 compliant parser
, like S
-nail
, takes care
for From_ lines only
662 after an empty line has been seen
, which cannot be detected that easily
664 ifdef HAVE_DEBUG
/* TODO assert legacy */
665 /* TODO if at all, this CAN only happen for SEND_DECRYPT, since all
666 * TODO other input situations handle RFC 4155 OR, if newly generated,
667 * TODO enforce quoted-printable if there is From_, as "required" by
668 * TODO RFC 5751. The SEND_DECRYPT case is not yet overhauled;
669 * TODO if it may happen in this path, we should just treat decryption
670 * TODO as we do for the other input paths; i.e., handle it in SSL!! */
671 if (action
== SEND_MBOX
|| action
== SEND_DECRYPT
)
672 assert(!is_head(buf
, len
));
674 if ((/*action == SEND_MBOX ||*/ action
== SEND_DECRYPT
) &&
681 flags
= ((int)action
& _TD_EOF
);
683 n
= mime_write(buf
, len
, fp
,
684 action
== SEND_MBOX
? CONV_NONE
: convert
,
685 flags
| ((action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
||
686 action
== SEND_QUOTE
|| action
== SEND_QUOTE_ALL
)
688 : (action
== SEND_TOSRCH
|| action
== SEND_TOPIPE
)
689 ? TD_ICONV
: (action
== SEND_SHOW
? TD_ISPR
: TD_NONE
)),
694 sz
= (ssize_t
)((size_t)sz
+ n
);
696 if (stats
!= NULL
&& stats
[0] != -1)
697 for (cp
= buf
; PTRCMP(cp
, <, buf
+ sz
); ++cp
)
700 _addstats(stats
, n
, sz
);
707 _send_onpipe(int signo
)
709 NYD_X
; /* Signal handler */
711 siglongjmp(_send_pipejmp
, 1);
715 sendpart(struct message
*zmp
, struct mimepart
*ip
, FILE * volatile obuf
,
716 struct ignoretab
*doign
, struct quoteflt
*qf
,
717 enum sendaction
volatile action
, off_t
*volatile stats
, int level
)
719 int volatile ispipe
, rv
= 0;
721 char *line
= NULL
, *cp
, *cp2
, *start
, *pipecomm
= NULL
;
722 size_t linesize
= 0, linelen
, cnt
;
723 int dostat
, infld
= 0, ignoring
= 1, isenc
, c
;
724 struct mimepart
*volatile np
;
725 FILE * volatile ibuf
= NULL
, * volatile pbuf
= obuf
, * volatile qbuf
= obuf
,
727 enum conversion
volatile convert
;
728 sighandler_type
volatile oldpipe
= SIG_DFL
;
732 if (ip
->m_mimecontent
== MIME_PKCS7
&& ip
->m_multipart
&&
733 action
!= SEND_MBOX
&& action
!= SEND_RFC822
&& action
!= SEND_SHOW
)
739 if (!is_ign("status", 6, doign
))
741 if (!is_ign("x-status", 8, doign
))
746 if ((ibuf
= setinput(&mb
, (struct message
*)ip
, NEED_BODY
)) == NULL
) {
752 if (ip
->m_mimecontent
== MIME_DISCARD
)
755 if (!(ip
->m_flag
& MNOFROM
))
756 while (cnt
&& (c
= getc(ibuf
)) != EOF
) {
762 convert
= (action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
||
763 action
== SEND_QUOTE
|| action
== SEND_QUOTE_ALL
||
764 action
== SEND_TOSRCH
)
765 ? CONV_FROMHDR
: CONV_NONE
;
767 /* Work the headers */
768 quoteflt_reset(qf
, obuf
);
769 while (fgetline(&line
, &linesize
, &cnt
, &linelen
, ibuf
, 0)) {
771 if (line
[0] == '\n') {
772 /* If line is blank, we've reached end of headers, so force out
773 * status: field and note that we are no longer in header fields */
775 statusput(zmp
, obuf
, qf
, stats
);
777 xstatusput(zmp
, obuf
, qf
, stats
);
778 if (doign
!= allignore
)
779 _out("\n", 1, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
784 if (infld
&& blankchar(line
[0])) {
785 /* If this line is a continuation (SP / HT) of a previous header
786 * field, determine if the start of the line is a MIME encoded word */
788 for (cp
= line
; blankchar(*cp
); ++cp
);
789 if (cp
> line
&& linelen
- PTR2SIZE(cp
- line
) > 8 &&
790 cp
[0] == '=' && cp
[1] == '?')
794 /* Pick up the header field if we have one */
795 for (cp
= line
; (c
= *cp
& 0377) && c
!= ':' && !spacechar(c
); ++cp
)
798 while (spacechar(*cp
))
800 if (cp
[0] != ':' && level
== 0 && lineno
== 1) {
801 /* Not a header line, force out status: This happens in uucp style
802 * mail where there are no headers at all */
804 statusput(zmp
, obuf
, qf
, stats
);
806 xstatusput(zmp
, obuf
, qf
, stats
);
807 if (doign
!= allignore
)
808 _out("\n", 1, obuf
, CONV_NONE
,SEND_MBOX
, qf
, stats
, NULL
);
812 /* If it is an ignored field and we care about such things, skip it.
813 * Misuse dostat also for another bit xxx use a bitenum + for more */
814 if (ok_blook(keep_content_length
))
817 *cp2
= 0; /* temporarily null terminate */
818 if ((doign
&& is_ign(line
, PTR2SIZE(cp2
- line
), doign
)) ||
819 (action
== SEND_MBOX
&& !(dostat
& (1 << 2)) &&
820 (!asccasecmp(line
, "content-length") ||
821 !asccasecmp(line
, "lines"))))
823 else if (!asccasecmp(line
, "status")) {
824 /* If field is "status," go compute and print real Status: field */
826 statusput(zmp
, obuf
, qf
, stats
);
830 } else if (!asccasecmp(line
, "x-status")) {
831 /* If field is "status," go compute and print real Status: field */
833 xstatusput(zmp
, obuf
, qf
, stats
);
840 pipecomm
= savestrbuf(line
, PTR2SIZE(cp2
- line
));
848 /* Determine if the end of the line is a MIME encoded word */
849 /* TODO geeeh! all this lengthy stuff that follows is about is dealing
850 * TODO with header follow lines, and it should be up to the backend
851 * TODO what happens and what not, i.e., it doesn't matter wether it's
852 * TODO a MIME-encoded word or not, as long as a single separating space
853 * TODO remains in between lines (the MIME stuff will correctly remove
854 * TODO whitespace in between multiple adjacent encoded words) */
856 if (cnt
&& (c
= getc(ibuf
)) != EOF
) {
858 cp
= line
+ linelen
- 1;
859 if (linelen
> 0 && *cp
== '\n')
861 while (cp
>= line
&& whitechar(*cp
))
863 if (PTR2SIZE(cp
- line
> 8) && cp
[0] == '=' && cp
[-1] == '?')
870 size_t len
= linelen
;
872 if (action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
||
873 action
== SEND_QUOTE
|| action
== SEND_QUOTE_ALL
||
874 action
== SEND_TOSRCH
) {
875 /* Strip blank characters if two MIME-encoded words follow on
876 * continuing lines */
878 while (len
> 0 && blankchar(*start
)) {
883 if (len
> 0 && start
[len
- 1] == '\n')
885 while (len
> 0 && blankchar(start
[len
- 1]))
890 bool_t colour_stripped
= FAL0
;
891 if (pipecomm
!= NULL
) {
892 colour_put_header(obuf
, pipecomm
);
893 if (len
> 0 && start
[len
- 1] == '\n') {
894 colour_stripped
= TRU1
;
899 _out(start
, len
, obuf
, convert
, action
, qf
, stats
, NULL
);
901 if (pipecomm
!= NULL
) {
902 colour_reset(obuf
); /* XXX reset after \n!! */
920 switch (ip
->m_mimecontent
) {
924 case SEND_TODISP_ALL
:
927 if (ok_blook(rfc822_body_from_
)) {
928 if (qf
->qf_pfix_len
> 0) {
929 size_t i
= fwrite(qf
->qf_pfix
, sizeof *qf
->qf_pfix
,
930 qf
->qf_pfix_len
, obuf
);
931 if (i
== qf
->qf_pfix_len
)
932 _addstats(stats
, 0, i
);
934 put_from_(obuf
, ip
->m_multipart
, stats
);
942 if (ok_blook(rfc822_body_from_
))
943 put_from_(obuf
, ip
->m_multipart
, stats
);
953 case MIME_TEXT_PLAIN
:
956 case SEND_TODISP_ALL
:
960 switch (_pipecmd(&pipecomm
, ip
)) {
962 _out(pipecomm
, strlen(pipecomm
), obuf
, CONV_NONE
, SEND_MBOX
, qf
,
964 /* We would print this as plain text, so better force going home */
980 if (action
!= SEND_DECRYPT
)
984 if (action
!= SEND_MBOX
&& action
!= SEND_RFC822
&&
985 action
!= SEND_SHOW
&& ip
->m_multipart
!= NULL
)
991 case SEND_TODISP_ALL
:
995 switch (_pipecmd(&pipecomm
, ip
)) {
997 _out(pipecomm
, strlen(pipecomm
), obuf
, CONV_NONE
, SEND_MBOX
, qf
,
1008 goto jcopyout
; /* break; break; */
1010 if (pipecomm
!= NULL
)
1012 if (level
== 0 && cnt
) {
1013 char const *x
= _("[Binary content]\n");
1014 _out(x
, strlen(x
), obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
1027 case MIME_ALTERNATIVE
:
1028 if ((action
== SEND_TODISP
|| action
== SEND_QUOTE
) &&
1029 !ok_blook(print_alternatives
)) {
1030 bool_t doact
= FAL0
;
1032 for (np
= ip
->m_multipart
; np
!= NULL
; np
= np
->m_nextpart
)
1033 if (np
->m_mimecontent
== MIME_TEXT_PLAIN
)
1036 for (np
= ip
->m_multipart
; np
!= NULL
; np
= np
->m_nextpart
) {
1037 if (np
->m_ct_type_plain
!= NULL
&& action
!= SEND_QUOTE
) {
1038 _print_part_info(&rest
, np
, doign
, level
);
1039 _out(rest
.s
, rest
.l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
,
1042 if (doact
&& np
->m_mimecontent
== MIME_TEXT_PLAIN
) {
1044 rv
= sendpart(zmp
, np
, obuf
, doign
, qf
, action
, stats
,
1046 quoteflt_reset(qf
, origobuf
);
1059 case SEND_TODISP_ALL
:
1061 case SEND_QUOTE_ALL
:
1067 if ((action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
) &&
1068 ip
->m_multipart
!= NULL
&&
1069 ip
->m_multipart
->m_mimecontent
== MIME_DISCARD
&&
1070 ip
->m_multipart
->m_nextpart
== NULL
) {
1071 char const *x
= _("[Missing multipart boundary - use \"show\" "
1072 "to display the raw message]\n");
1073 _out(x
, strlen(x
), obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
1076 for (np
= ip
->m_multipart
; np
!= NULL
; np
= np
->m_nextpart
) {
1077 if (np
->m_mimecontent
== MIME_DISCARD
&& action
!= SEND_DECRYPT
)
1082 if (np
->m_partstring
&& !strcmp(np
->m_partstring
, "1"))
1085 if ((obuf
= newfile(np
, UNVOLATILE(&ispipe
))) == NULL
)
1089 if (sigsetjmp(_send_pipejmp
, 1)) {
1093 oldpipe
= safe_signal(SIGPIPE
, &_send_onpipe
);
1096 case SEND_TODISP_ALL
:
1097 case SEND_QUOTE_ALL
:
1098 if (ip
->m_mimecontent
!= MIME_MULTI
&&
1099 ip
->m_mimecontent
!= MIME_ALTERNATIVE
&&
1100 ip
->m_mimecontent
!= MIME_DIGEST
)
1102 _print_part_info(&rest
, np
, doign
, level
);
1103 _out(rest
.s
, rest
.l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
,
1117 if (sendpart(zmp
, np
, obuf
, doign
, qf
, action
, stats
, level
+1) < 0)
1119 quoteflt_reset(qf
, origobuf
);
1120 if (action
== SEND_QUOTE
)
1122 if (action
== SEND_TOFILE
&& obuf
!= origobuf
) {
1127 safe_signal(SIGPIPE
, SIG_IGN
);
1129 safe_signal(SIGPIPE
, oldpipe
);
1141 /* Copy out message body */
1143 if (doign
== allignore
&& level
== 0) /* skip final blank line */
1145 switch (ip
->m_mimeenc
) {
1152 convert
= CONV_NONE
;
1155 convert
= CONV_FROMQP
;
1158 switch (ip
->m_mimecontent
) {
1160 case MIME_TEXT_PLAIN
:
1161 case MIME_TEXT_HTML
:
1162 convert
= CONV_FROMB64_T
;
1165 convert
= CONV_FROMB64
;
1169 convert
= CONV_NONE
;
1172 if (action
== SEND_DECRYPT
|| action
== SEND_MBOX
||
1173 action
== SEND_RFC822
|| action
== SEND_SHOW
)
1174 convert
= CONV_NONE
;
1176 if ((action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
||
1177 action
== SEND_QUOTE
|| action
== SEND_QUOTE_ALL
||
1178 action
== SEND_TOSRCH
) &&
1179 (ip
->m_mimecontent
== MIME_TEXT_PLAIN
||
1180 ip
->m_mimecontent
== MIME_TEXT_HTML
||
1181 ip
->m_mimecontent
== MIME_TEXT
)) {
1182 char const *tcs
= charset_get_lc();
1184 if (iconvd
!= (iconv_t
)-1)
1185 n_iconv_close(iconvd
);
1186 /* TODO Since Base64 has an odd 4:3 relation in between input
1187 * TODO and output an input line may end with a partial
1188 * TODO multibyte character; this is no problem at all unless
1189 * TODO we send to the display or whatever, i.e., ensure
1190 * TODO makeprint() or something; to avoid this trap, *force*
1191 * TODO iconv(), in which case this layer will handle leftovers
1193 if (convert
== CONV_FROMB64_T
|| (asccasecmp(tcs
, ip
->m_charset
) &&
1194 asccasecmp(charset_get_7bit(), ip
->m_charset
))) {
1195 iconvd
= n_iconv_open(tcs
, ip
->m_charset
);
1196 /* XXX Don't bail out if we cannot iconv(3) here;
1197 * XXX alternatively we could avoid trying to open
1198 * XXX if ip->m_charset is "unknown-8bit", which was
1199 * XXX the one that has bitten me?? */
1201 * TODO errors should DEFINETELY not be scrolled away!
1202 * TODO what about an error buffer (think old shsp(1)),
1203 * TODO re-dump errors since last snapshot when the
1204 * TODO command loop enters again? i.e., at least print
1205 * TODO "There were errors ?" before the next prompt,
1206 * TODO so that the user can look at the error buffer?
1208 if (iconvd
== (iconv_t
)-1 && errno
== EINVAL
) {
1209 fprintf(stderr
, _("Cannot convert from %s to %s\n"),
1210 ip
->m_charset
, tcs
);
1211 /*rv = 1; goto jleave;*/
1217 if (pipecomm
!= NULL
&& (action
== SEND_TODISP
||
1218 action
== SEND_TODISP_ALL
|| action
== SEND_QUOTE
||
1219 action
== SEND_QUOTE_ALL
)) {
1221 pbuf
= _pipefile(pipecomm
, ip
, UNVOLATILE(&qbuf
),
1222 (action
== SEND_QUOTE
|| action
== SEND_QUOTE_ALL
), !ispipe
);
1223 action
= SEND_TOPIPE
;
1225 oldpipe
= safe_signal(SIGPIPE
, &_send_onpipe
);
1226 if (sigsetjmp(_send_pipejmp
, 1))
1234 ui32_t save_qf_pfix_len
= qf
->qf_pfix_len
;
1235 off_t
*save_stats
= stats
;
1237 if (pbuf
!= origobuf
) {
1238 qf
->qf_pfix_len
= 0; /* XXX legacy (remove filter instead) */
1245 quoteflt_reset(qf
, pbuf
);
1246 while (!eof
&& fgetline(&line
, &linesize
, &cnt
, &linelen
, ibuf
, 0)) {
1248 if (_out(line
, linelen
, pbuf
, convert
, action
, qf
, stats
, &rest
) < 0 ||
1250 rv
= -1; /* XXX Should bail away?! */
1254 if (!eof
&& rest
.l
!= 0) {
1264 if (pbuf
!= origobuf
) {
1265 qf
->qf_pfix_len
= save_qf_pfix_len
;
1273 safe_signal(SIGPIPE
, SIG_IGN
);
1274 Pclose(pbuf
, ispipe
);
1275 safe_signal(SIGPIPE
, oldpipe
);
1277 pipecpy(qbuf
, obuf
, origobuf
, qf
, stats
);
1280 if (iconvd
!= (iconv_t
)-1)
1281 n_iconv_close(iconvd
);
1289 newfile(struct mimepart
*ip
, int *ispipe
)
1299 if (f
!= NULL
&& f
!= (char*)-1) {
1302 mime_fromhdr(&in
, &out
, TD_ISPR
);
1303 memcpy(f
, out
.s
, out
.l
);
1304 *(f
+ out
.l
) = '\0';
1308 if (options
& OPT_INTERACTIVE
) {
1311 printf(_("Enter filename for part %s (%s)"),
1312 (ip
->m_partstring
!= NULL
) ? ip
->m_partstring
: "?",
1313 ip
->m_ct_type_plain
);
1314 f2
= readstr_input(": ", (f
!= (char*)-1) ? f
: NULL
);
1315 if (f2
== NULL
|| *f2
== '\0') {
1316 fprintf(stderr
, _("... skipping this\n"));
1319 } else if (*f2
== '|')
1320 /* Pipes are expanded by the shell */
1322 else if ((f3
= file_expand(f2
)) == NULL
)
1323 /* (Error message written by file_expand()) */
1328 if (f
== NULL
|| f
== (char*)-1) {
1335 cp
= ok_vlook(SHELL
);
1338 fp
= Popen(f
+ 1, "w", cp
, NULL
, 1);
1339 if (!(*ispipe
= (fp
!= NULL
)))
1342 if ((fp
= Fopen(f
, "w")) == NULL
)
1343 fprintf(stderr
, _("Cannot open `%s'\n"), f
);
1351 pipecpy(FILE *pipebuf
, FILE *outbuf
, FILE *origobuf
, struct quoteflt
*qf
,
1354 char *line
= NULL
; /* TODO line pool */
1355 size_t linesize
= 0, linelen
, cnt
;
1361 cnt
= fsize(pipebuf
);
1364 quoteflt_reset(qf
, outbuf
);
1365 while (fgetline(&line
, &linesize
, &cnt
, &linelen
, pipebuf
, 0) != NULL
) {
1366 if ((sz
= quoteflt_push(qf
, line
, linelen
)) < 0)
1370 if ((sz
= quoteflt_flush(qf
)) > 0)
1375 if (all_sz
> 0 && outbuf
== origobuf
)
1376 _addstats(stats
, 1, all_sz
);
1382 statusput(const struct message
*mp
, FILE *obuf
, struct quoteflt
*qf
,
1385 char statout
[3], *cp
= statout
;
1388 if (mp
->m_flag
& MREAD
)
1390 if (!(mp
->m_flag
& MNEW
))
1394 int i
= fprintf(obuf
, "%.*sStatus: %s\n", (int)qf
->qf_pfix_len
,
1395 (qf
->qf_pfix_len
> 0 ? qf
->qf_pfix
: 0), statout
);
1397 _addstats(stats
, 1, i
);
1403 xstatusput(const struct message
*mp
, FILE *obuf
, struct quoteflt
*qf
,
1407 char *xp
= xstatout
;
1410 if (mp
->m_flag
& MFLAGGED
)
1412 if (mp
->m_flag
& MANSWERED
)
1414 if (mp
->m_flag
& MDRAFTED
)
1418 int i
= fprintf(obuf
, "%.*sX-Status: %s\n", (int)qf
->qf_pfix_len
,
1419 (qf
->qf_pfix_len
> 0 ? qf
->qf_pfix
: 0), xstatout
);
1421 _addstats(stats
, 1, i
);
1427 put_from_(FILE *fp
, struct mimepart
*ip
, off_t
*stats
)
1429 char const *froma
, *date
, *nl
;
1433 if (ip
!= NULL
&& ip
->m_from
!= NULL
) {
1435 date
= fakedate(ip
->m_time
);
1439 date
= time_current
.tc_ctime
;
1443 colour_put(fp
, COLOURSPEC_FROM_
);
1444 i
= fprintf(fp
, "From %s %s%s", froma
, date
, nl
);
1447 _addstats(stats
, (*nl
!= '\0'), i
);
1452 sendmp(struct message
*mp
, FILE *obuf
, struct ignoretab
*doign
,
1453 char const *prefix
, enum sendaction action
, off_t
*stats
)
1459 struct mimepart
*ip
;
1463 if (mp
== dot
&& action
!= SEND_TOSRCH
)
1466 stats
[0] = stats
[1] = 0;
1467 quoteflt_init(&qf
, prefix
);
1469 /* First line is the From_ line, so no headers there to worry about */
1470 if ((ibuf
= setinput(&mb
, mp
, NEED_BODY
)) == NULL
)
1476 struct str
const *cpre
, *csuf
;
1478 cpre
= colour_get(COLOURSPEC_FROM_
);
1479 csuf
= colour_get(COLOURSPEC_RESET
);
1483 if (mp
->m_flag
& MNOFROM
) {
1484 if (doign
!= allignore
&& doign
!= fwdignore
&& action
!= SEND_RFC822
)
1485 sz
= fprintf(obuf
, "%s%.*sFrom %s %s%s\n",
1486 (cpre
!= NULL
? cpre
->s
: ""),
1487 (int)qf
.qf_pfix_len
, (qf
.qf_pfix_len
!= 0 ? qf
.qf_pfix
: ""),
1488 fakefrom(mp
), fakedate(mp
->m_time
),
1489 (csuf
!= NULL
? csuf
->s
: ""));
1491 if (doign
!= allignore
&& doign
!= fwdignore
&& action
!= SEND_RFC822
) {
1492 if (qf
.qf_pfix_len
> 0) {
1493 i
= fwrite(qf
.qf_pfix
, sizeof *qf
.qf_pfix
, qf
.qf_pfix_len
, obuf
);
1494 if (i
!= qf
.qf_pfix_len
)
1500 fputs(cpre
->s
, obuf
);
1501 cpre
= (struct str
const*)0x1;
1506 while (cnt
> 0 && (c
= getc(ibuf
)) != EOF
) {
1507 if (doign
!= allignore
&& doign
!= fwdignore
&&
1508 action
!= SEND_RFC822
) {
1510 if (c
== '\n' && csuf
!= NULL
) {
1511 cpre
= (struct str
const*)0x1;
1512 fputs(csuf
->s
, obuf
);
1524 if (csuf
!= NULL
&& cpre
!= (struct str
const*)0x1)
1525 fputs(csuf
->s
, obuf
);
1530 _addstats(stats
, 1, sz
);
1533 if (action
!= SEND_MBOX
&& action
!= SEND_RFC822
&& action
!= SEND_SHOW
)
1534 pf
|= PARSE_DECRYPT
| PARSE_PARTS
;
1535 if ((ip
= parsemsg(mp
, pf
)) == NULL
)
1538 rv
= sendpart(mp
, ip
, obuf
, doign
, &qf
, action
, stats
, 0);
1540 quoteflt_destroy(&qf
);