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 type */
81 static enum pipeflags
_pipecmd(char **result
, char const *content_type
);
84 static FILE * _pipefile(char const *pipecomm
, FILE **qbuf
, bool_t quote
,
87 /* Adjust output statistics */
88 SINLINE
void _addstats(off_t
*stats
, off_t lines
, off_t bytes
);
90 /* Call mime_write() as approbiate and adjust statistics */
91 SINLINE ssize_t
_out(char const *buf
, size_t len
, FILE *fp
,
92 enum conversion convert
, enum sendaction action
,
93 struct quoteflt
*qf
, off_t
*stats
,
97 static void _send_onpipe(int signo
);
100 static int sendpart(struct message
*zmp
, struct mimepart
*ip
,
101 FILE *obuf
, struct ignoretab
*doign
,
102 struct quoteflt
*qf
, enum sendaction action
,
103 off_t
*stats
, int level
);
105 /* Get a file for an attachment */
106 static FILE * newfile(struct mimepart
*ip
, int *ispipe
);
108 static void pipecpy(FILE *pipebuf
, FILE *outbuf
, FILE *origobuf
,
109 struct quoteflt
*qf
, off_t
*stats
);
111 /* Output a reasonable looking status field */
112 static void statusput(const struct message
*mp
, FILE *obuf
,
113 struct quoteflt
*qf
, off_t
*stats
);
114 static void xstatusput(const struct message
*mp
, FILE *obuf
,
115 struct quoteflt
*qf
, off_t
*stats
);
117 static void put_from_(FILE *fp
, struct mimepart
*ip
, off_t
*stats
);
119 static struct mimepart
*
120 parsemsg(struct message
*mp
, enum parseflags pf
)
125 ip
= csalloc(1, sizeof *ip
);
126 ip
->m_flag
= mp
->m_flag
;
127 ip
->m_have
= mp
->m_have
;
128 ip
->m_block
= mp
->m_block
;
129 ip
->m_offset
= mp
->m_offset
;
130 ip
->m_size
= mp
->m_size
;
131 ip
->m_xsize
= mp
->m_xsize
;
132 ip
->m_lines
= mp
->m_lines
;
133 ip
->m_xlines
= mp
->m_lines
;
134 if (parsepart(mp
, ip
, pf
, 0) != OKAY
)
141 parsepart(struct message
*zmp
, struct mimepart
*ip
, enum parseflags pf
,
148 ip
->m_ct_type
= hfield1("content-type", (struct message
*)ip
);
149 if (ip
->m_ct_type
!= NULL
) {
150 ip
->m_ct_type_plain
= savestr(ip
->m_ct_type
);
151 if ((cp
= strchr(ip
->m_ct_type_plain
, ';')) != NULL
)
153 } else if (ip
->m_parent
&& ip
->m_parent
->m_mimecontent
== MIME_DIGEST
)
154 ip
->m_ct_type_plain
= UNCONST("message/rfc822");
156 ip
->m_ct_type_plain
= UNCONST("text/plain");
159 ip
->m_charset
= mime_getparam("charset", ip
->m_ct_type
);
160 if (ip
->m_charset
== NULL
)
161 ip
->m_charset
= charset_get_7bit();
163 ip
->m_ct_transfer_enc
= hfield1("content-transfer-encoding",
164 (struct message
*)ip
);
165 ip
->m_mimeenc
= ip
->m_ct_transfer_enc
?
166 mime_getenc(ip
->m_ct_transfer_enc
) : MIME_7B
;
168 if (((cp
= hfield1("content-disposition", (struct message
*)ip
)) == 0 ||
169 (ip
->m_filename
= mime_getparam("filename", cp
)) == 0) &&
170 ip
->m_ct_type
!= NULL
)
171 ip
->m_filename
= mime_getparam("name", ip
->m_ct_type
);
173 ip
->m_mimecontent
= mime_classify_content_of_part(ip
);
175 if (pf
& PARSE_PARTS
) {
176 if (level
> 9999) { /* TODO MAGIC */
177 fprintf(stderr
, tr(36, "MIME content too deeply nested\n"));
180 switch (ip
->m_mimecontent
) {
182 if (pf
& PARSE_DECRYPT
) {
184 parsepkcs7(zmp
, ip
, pf
, level
);
187 fprintf(stderr
, tr(225, "No SSL support compiled in.\n"));
195 case MIME_ALTERNATIVE
:
197 _parsemultipart(zmp
, ip
, pf
, level
);
200 parse822(zmp
, ip
, pf
, level
);
211 parse822(struct message
*zmp
, struct mimepart
*ip
, enum parseflags pf
,
222 if ((ibuf
= setinput(&mb
, (struct message
*)ip
, NEED_BODY
)) == NULL
)
227 while (cnt
&& ((c
= getc(ibuf
)) != EOF
)) {
238 np
= csalloc(1, sizeof *np
);
239 np
->m_flag
= MNOFROM
;
240 np
->m_have
= HAVE_HEADER
| HAVE_BODY
;
241 np
->m_block
= mailx_blockof(offs
);
242 np
->m_offset
= mailx_offsetof(offs
);
243 np
->m_size
= np
->m_xsize
= cnt
;
244 np
->m_lines
= np
->m_xlines
= lines
;
245 np
->m_partstring
= ip
->m_partstring
;
247 ip
->m_multipart
= np
;
249 if (ok_blook(rfc822_body_from_
)) {
250 substdate((struct message
*)np
);
251 np
->m_from
= fakefrom((struct message
*)np
);/* TODO strip MNOFROM flag? */
254 parsepart(zmp
, np
, pf
, level
+1);
261 parsepkcs7(struct message
*zmp
, struct mimepart
*ip
, enum parseflags pf
,
264 struct message m
, *xmp
;
269 memcpy(&m
, ip
, sizeof m
);
270 to
= hfield1("to", zmp
);
271 cc
= hfield1("cc", zmp
);
273 if ((xmp
= smime_decrypt(&m
, to
, cc
, 0)) != NULL
) {
274 np
= csalloc(1, sizeof *np
);
275 np
->m_flag
= xmp
->m_flag
;
276 np
->m_have
= xmp
->m_have
;
277 np
->m_block
= xmp
->m_block
;
278 np
->m_offset
= xmp
->m_offset
;
279 np
->m_size
= xmp
->m_size
;
280 np
->m_xsize
= xmp
->m_xsize
;
281 np
->m_lines
= xmp
->m_lines
;
282 np
->m_xlines
= xmp
->m_xlines
;
283 np
->m_partstring
= ip
->m_partstring
;
285 if (parsepart(zmp
, np
, pf
, level
+1) == OKAY
) {
287 ip
->m_multipart
= np
;
295 _parsemultipart(struct message
*zmp
, struct mimepart
*ip
, enum parseflags pf
,
298 /* TODO Instead of the recursive multiple run parse we have today,
299 * TODO the send/MIME layer rewrite must create a "tree" of parts with
300 * TODO a single-pass parse, then address each part directly as
301 * TODO necessary; since boundaries start with -- and the content
302 * TODO rather forms a stack this is pretty cheap indeed! */
303 struct mimepart
*np
= NULL
;
304 char *boundary
, *line
= NULL
;
305 size_t linesize
= 0, linelen
, cnt
, boundlen
;
312 if ((boundary
= mime_get_boundary(ip
->m_ct_type
, &linelen
)) == NULL
)
316 if ((ibuf
= setinput(&mb
, (struct message
*)ip
, NEED_BODY
)) == NULL
)
320 while (fgetline(&line
, &linesize
, &cnt
, &linelen
, ibuf
, 0))
325 __newpart(ip
, &np
, offs
, NULL
);
326 while (fgetline(&line
, &linesize
, &cnt
, &linelen
, ibuf
, 0)) {
327 /* XXX linelen includes LF */
328 if (!((lines
> 0 || part
== 0) && linelen
> boundlen
&&
329 !strncmp(line
, boundary
, boundlen
))) {
334 /* Subpart boundary? */
335 if (line
[boundlen
] == '\n') {
338 __endpart(&np
, offs
- boundlen
- 2, lines
);
339 __newpart(ip
, &np
, offs
- boundlen
- 2, NULL
);
341 __endpart(&np
, offs
, 2);
342 __newpart(ip
, &np
, offs
, &part
);
347 /* Final boundary? Be aware of cases where there is no separating
348 * newline in between boundaries, as has been seen in a message with
349 * "Content-Type: multipart/appledouble;" */
350 if (linelen
< boundlen
+ 2)
352 linelen
-= boundlen
+ 2;
353 if (line
[boundlen
] != '-' || line
[boundlen
+ 1] != '-' ||
354 (linelen
> 0 && line
[boundlen
+ 2] != '\n'))
358 __endpart(&np
, offs
- boundlen
- 4, lines
);
359 __newpart(ip
, &np
, offs
- boundlen
- 4, NULL
);
361 __endpart(&np
, offs
+ cnt
, 2);
366 __endpart(&np
, offs
, lines
);
369 for (np
= ip
->m_multipart
; np
; np
= np
->m_nextpart
)
370 if (np
->m_mimecontent
!= MIME_DISCARD
)
371 parsepart(zmp
, np
, pf
, level
+ 1);
378 __newpart(struct mimepart
*ip
, struct mimepart
**np
, off_t offs
, int *part
)
384 *np
= csalloc(1, sizeof **np
);
385 (*np
)->m_flag
= MNOFROM
;
386 (*np
)->m_have
= HAVE_HEADER
| HAVE_BODY
;
387 (*np
)->m_block
= mailx_blockof(offs
);
388 (*np
)->m_offset
= mailx_offsetof(offs
);
392 sz
= (ip
->m_partstring
!= NULL
) ? strlen(ip
->m_partstring
) : 0;
394 (*np
)->m_partstring
= salloc(sz
);
395 if (ip
->m_partstring
)
396 snprintf((*np
)->m_partstring
, sz
, "%s.%u", ip
->m_partstring
, *part
);
398 snprintf((*np
)->m_partstring
, sz
, "%u", *part
);
400 (*np
)->m_mimecontent
= MIME_DISCARD
;
401 (*np
)->m_parent
= ip
;
403 if (ip
->m_multipart
) {
404 for (pp
= ip
->m_multipart
; pp
->m_nextpart
!= NULL
; pp
= pp
->m_nextpart
)
406 pp
->m_nextpart
= *np
;
408 ip
->m_multipart
= *np
;
413 __endpart(struct mimepart
**np
, off_t xoffs
, long lines
)
418 offs
= mailx_positionof((*np
)->m_block
, (*np
)->m_offset
);
419 (*np
)->m_size
= (*np
)->m_xsize
= xoffs
- offs
;
420 (*np
)->m_lines
= (*np
)->m_xlines
= lines
;
426 _print_part_info(struct str
*out
, struct mimepart
*mip
,
427 struct ignoretab
*doign
, int level
)
429 struct str ct
= {NULL
, 0}, cd
= {NULL
, 0};
431 struct str
const *cpre
, *csuf
;
435 if (is_ign("content-type", 12, doign
)) {
436 out
->s
= mip
->m_ct_type_plain
;
437 out
->l
= strlen(out
->s
);
438 ct
.s
= ac_alloc(out
->l
+ 2 +1);
442 if (is_prefix("application/", out
->s
)) {
443 memcpy(ct
.s
+ 2, "appl../", 7);
447 out
->l
= MIN(out
->l
, 17);
449 out
->l
= MIN(out
->l
, 24);
450 memcpy(ct
.s
+ ct
.l
, out
->s
, out
->l
);
456 if (is_ign("content-disposition", 19, doign
) &&
457 mip
->m_filename
!= NULL
) {
460 ti
.l
= strlen(ti
.s
= mip
->m_filename
);
461 mime_fromhdr(&ti
, &to
, TD_ISPR
| TD_ICONV
| TD_DELCTRL
);
462 to
.l
= MIN(to
.l
, 25); /* FIXME MIME: filename may be multibyte enc!! */
463 cd
.s
= ac_alloc(to
.l
+ 2 +1); /* FIXME ..visual length would be better */
466 memcpy(cd
.s
+ 2, to
.s
, to
.l
);
472 /* Take care of "99.99", i.e., 5 */
473 if ((ps
= mip
->m_partstring
) == NULL
|| ps
[0] == '\0')
477 cpre
= colour_get(COLOURSPEC_PARTINFO
);
478 csuf
= colour_get(COLOURSPEC_RESET
);
483 /* Assume maximum possible sizes for 64 bit integers here to avoid any
484 * buffer overflows just in case we have a bug somewhere and / or the
485 * snprintf() is our internal version that doesn't really provide hard
487 #define __msg "%s%s[-- #%s : %lu/%lu%s%s --]%s\n"
488 out
->l
= sizeof(__msg
) +
490 (cpre
!= NULL
? cpre
->l
+ csuf
->l
: 0) +
492 strlen(ps
) + 2*21 + ct
.l
+ cd
.l
+ 1;
493 out
->s
= salloc(out
->l
);
494 out
->l
= snprintf(out
->s
, out
->l
, __msg
,
495 (level
|| (ps
[0] != '1' && ps
[1] == '\0') ? "\n" : ""),
496 (cpre
!= NULL
? cpre
->s
: ""),
497 ps
, (ul_it
)mip
->m_lines
, (ul_it
)mip
->m_size
,
498 (ct
.s
!= NULL
? ct
.s
: ""),
499 (cd
.s
!= NULL
? cd
.s
: ""),
500 (csuf
!= NULL
? csuf
->s
: ""));
501 out
->s
[out
->l
] = '\0';
511 static enum pipeflags
512 _pipecmd(char **result
, char const *content_type
)
521 if (content_type
== NULL
)
524 /* First check wether there is a special pipe-MIMETYPE handler */
525 s
= ac_alloc(strlen(content_type
) + 6);
526 memcpy(s
, "pipe-", 5);
530 *cp
++ = lowerconv(*cq
);
531 while (*cq
++ != '\0');
538 /* User specified a command, inspect for special cases */
540 /* Normal command line */
543 } else if (*++cp
== '\0') {
544 /* Treat as plain text */
546 } else if (!msglist_is_single
) {
547 /* Viewing multiple messages in one go, don't block system */
549 *result
= UNCONST(tr(86,
550 "[Directly address message only to display this]\n"));
552 /* Viewing a single message only */
553 #if 0 /* TODO send/MIME layer rewrite: when we have a single-pass parser
554 * TODO then the parsing phase and the send phase will be separated;
555 * TODO that allows us to ask a user *before* we start the send, i.e.,
556 * TODO *before* a pager pipe is setup (which is the problem with
557 * TODO the '#if 0' code here) */
558 size_t l
= strlen(content_type
);
559 char const *x
= tr(999, "Should i display a part `%s' (y/n)? ");
560 s
= ac_alloc(l
+= strlen(x
) + 1);
561 snprintf(s
, l
- 1, x
, content_type
);
563 puts(""); /* .. we've hijacked a pipe 8-] ... */
566 x
= tr(210, "[User skipped diplay]\n");
568 *result
= UNCONST(x
);
572 /* Asynchronous command, normal command line */
573 ret
= PIPE_ASYNC
, *result
= ++cp
;
575 ret
= PIPE_COMM
, *result
= cp
;
583 _pipefile(char const *pipecomm
, FILE **qbuf
, bool_t quote
, bool_t async
)
592 if ((*qbuf
= Ftmp(NULL
, "sendp", OF_RDWR
| OF_UNLINK
| OF_REGISTER
,
594 perror(tr(173, "tmpfile"));
600 if ((sh
= ok_vlook(SHELL
)) == NULL
)
602 if ((rbuf
= Popen(pipecomm
, "W", sh
, async
? -1 : fileno(*qbuf
))) == NULL
)
614 _addstats(off_t
*stats
, off_t lines
, off_t bytes
)
626 _out(char const *buf
, size_t len
, FILE *fp
, enum conversion convert
, enum
627 sendaction action
, struct quoteflt
*qf
, off_t
*stats
, struct str
*rest
)
635 Well
... it turns out to
not work like that since of course a valid
636 RFC
4155 compliant parser
, like S
-nail
, takes care
for From_ lines only
637 after an empty line has been seen
, which cannot be detected that easily
639 ifdef HAVE_DEBUG
/* TODO assert legacy */
640 /* TODO if at all, this CAN only happen for SEND_DECRYPT, since all
641 * TODO other input situations handle RFC 4155 OR, if newly generated,
642 * TODO enforce quoted-printable if there is From_, as "required" by
643 * TODO RFC 5751. The SEND_DECRYPT case is not yet overhauled;
644 * TODO if it may happen in this path, we should just treat decryption
645 * TODO as we do for the other input paths; i.e., handle it in SSL!! */
646 if (action
== SEND_MBOX
|| action
== SEND_DECRYPT
)
647 assert(!is_head(buf
, len
));
649 if ((/*action == SEND_MBOX ||*/ action
== SEND_DECRYPT
) &&
656 flags
= ((int)action
& _TD_EOF
);
658 n
= mime_write(buf
, len
, fp
,
659 action
== SEND_MBOX
? CONV_NONE
: convert
,
660 flags
| ((action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
||
661 action
== SEND_QUOTE
|| action
== SEND_QUOTE_ALL
)
663 : (action
== SEND_TOSRCH
|| action
== SEND_TOPIPE
)
664 ? TD_ICONV
: (action
== SEND_TOFLTR
)
665 ? TD_DELCTRL
: (action
== SEND_SHOW
? TD_ISPR
: TD_NONE
)),
670 sz
= (ssize_t
)((size_t)sz
+ n
);
672 if (stats
!= NULL
&& stats
[0] != -1)
673 for (cp
= buf
; cp
< &buf
[sz
]; ++cp
)
676 _addstats(stats
, n
, sz
);
683 _send_onpipe(int signo
)
685 NYD_X
; /* Signal handler */
687 siglongjmp(_send_pipejmp
, 1);
691 sendpart(struct message
*zmp
, struct mimepart
*ip
, FILE *obuf
,
692 struct ignoretab
*doign
, struct quoteflt
*qf
,
693 enum sendaction
volatile action
, off_t
*volatile stats
, int level
)
695 int volatile ispipe
, rv
= 0;
697 char *line
= NULL
, *cp
, *cp2
, *start
, *pipecomm
= NULL
;
698 size_t linesize
= 0, linelen
, cnt
;
699 int dostat
, infld
= 0, ignoring
= 1, isenc
, c
;
700 struct mimepart
*volatile np
;
701 FILE *volatile ibuf
= NULL
, *volatile pbuf
= obuf
,
702 *volatile qbuf
= obuf
, *origobuf
= obuf
;
703 enum conversion
volatile convert
;
704 sighandler_type
volatile oldpipe
= SIG_DFL
;
708 if (ip
->m_mimecontent
== MIME_PKCS7
&& ip
->m_multipart
&&
709 action
!= SEND_MBOX
&& action
!= SEND_RFC822
&& action
!= SEND_SHOW
)
715 if (!is_ign("status", 6, doign
))
717 if (!is_ign("x-status", 8, doign
))
722 if ((ibuf
= setinput(&mb
, (struct message
*)ip
, NEED_BODY
)) == NULL
) {
728 if (ip
->m_mimecontent
== MIME_DISCARD
)
731 if ((ip
->m_flag
& MNOFROM
) == 0)
732 while (cnt
&& (c
= getc(ibuf
)) != EOF
) {
738 convert
= (action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
||
739 action
== SEND_QUOTE
|| action
== SEND_QUOTE_ALL
||
740 action
== SEND_TOSRCH
|| action
== SEND_TOFLTR
)
741 ? CONV_FROMHDR
: CONV_NONE
;
743 /* Work the headers */
744 quoteflt_reset(qf
, obuf
);
745 while (fgetline(&line
, &linesize
, &cnt
, &linelen
, ibuf
, 0)) {
747 if (line
[0] == '\n') {
748 /* If line is blank, we've reached end of headers, so force out
749 * status: field and note that we are no longer in header fields */
751 statusput(zmp
, obuf
, qf
, stats
);
753 xstatusput(zmp
, obuf
, qf
, stats
);
754 if (doign
!= allignore
)
755 _out("\n", 1, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
760 if (infld
&& blankchar(line
[0])) {
761 /* If this line is a continuation (SP / HT) of a previous header
762 * field, determine if the start of the line is a MIME encoded word */
764 for (cp
= line
; blankchar(*cp
); cp
++);
765 if (cp
> line
&& linelen
- (cp
- line
) > 8 && cp
[0] == '=' &&
770 /* Pick up the header field if we have one */
771 for (cp
= line
; (c
= *cp
& 0377) && c
!= ':' && !spacechar(c
); ++cp
)
774 while (spacechar(*cp
))
776 if (cp
[0] != ':' && level
== 0 && lineno
== 1) {
777 /* Not a header line, force out status: This happens in uucp style
778 * mail where there are no headers at all */
780 statusput(zmp
, obuf
, qf
, stats
);
782 xstatusput(zmp
, obuf
, qf
, stats
);
783 if (doign
!= allignore
)
784 _out("\n", 1, obuf
, CONV_NONE
,SEND_MBOX
, qf
, stats
, NULL
);
788 /* If it is an ignored field and we care about such things, skip it */
790 *cp2
= 0; /* temporarily null terminate */
791 if ((doign
&& is_ign(line
, PTR2SIZE(cp2
- line
), doign
)) ||
792 (action
== SEND_MBOX
&& !ok_blook(keep_content_length
) &&
793 (!asccasecmp(line
, "content-length") ||
794 !asccasecmp(line
, "lines"))))
796 else if (!asccasecmp(line
, "status")) {
797 /* If field is "status," go compute and print real Status: field */
799 statusput(zmp
, obuf
, qf
, stats
);
803 } else if (!asccasecmp(line
, "x-status")) {
804 /* If field is "status," go compute and print real Status: field */
806 xstatusput(zmp
, obuf
, qf
, stats
);
813 pipecomm
= savestrbuf(line
, PTR2SIZE(cp2
- line
));
820 /* Determine if the end of the line is a MIME encoded word */
822 if (cnt
&& (c
= getc(ibuf
)) != EOF
) {
824 if (linelen
> 0 && line
[linelen
- 1] == '\n')
825 cp
= &line
[linelen
- 2];
827 cp
= &line
[linelen
- 1];
828 while (cp
>= line
&& whitechar(*cp
))
830 if (cp
- line
> 8 && cp
[0] == '=' && cp
[-1] == '?')
837 size_t len
= linelen
;
839 if (action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
||
840 action
== SEND_QUOTE
|| action
== SEND_QUOTE_ALL
||
841 action
== SEND_TOSRCH
|| action
== SEND_TOFLTR
) {
842 /* Strip blank characters if two MIME-encoded words follow on
843 * continuing lines */
845 while (len
> 0 && blankchar(*start
)) {
850 if (len
> 0 && start
[len
- 1] == '\n')
852 while (len
> 0 && blankchar(start
[len
- 1]))
857 bool_t colour_stripped
= FAL0
;
858 if (pipecomm
!= NULL
) {
859 colour_put_header(obuf
, pipecomm
);
860 if (len
> 0 && start
[len
- 1] == '\n') {
861 colour_stripped
= TRU1
;
866 _out(start
, len
, obuf
, convert
, action
, qf
, stats
, NULL
);
868 if (pipecomm
!= NULL
) {
869 colour_reset(obuf
); /* XXX reset after \n!! */
887 switch (ip
->m_mimecontent
) {
894 case SEND_TODISP_ALL
:
897 if (ok_blook(rfc822_body_from_
)) {
898 if (qf
->qf_pfix_len
> 0) {
899 size_t i
= fwrite(qf
->qf_pfix
, sizeof *qf
->qf_pfix
,
900 qf
->qf_pfix_len
, obuf
);
901 if (i
== qf
->qf_pfix_len
)
902 _addstats(stats
, 0, i
);
904 put_from_(obuf
, ip
->m_multipart
, stats
);
912 if (ok_blook(rfc822_body_from_
))
913 put_from_(obuf
, ip
->m_multipart
, stats
);
922 if (action
== SEND_TOFLTR
)
926 case MIME_TEXT_PLAIN
:
929 case SEND_TODISP_ALL
:
933 switch (_pipecmd(&pipecomm
, ip
->m_ct_type_plain
)) {
935 _out(pipecomm
, strlen(pipecomm
), obuf
, CONV_NONE
, SEND_MBOX
, qf
,
951 if (action
!= SEND_DECRYPT
)
955 if (action
!= SEND_MBOX
&& action
!= SEND_RFC822
&&
956 action
!= SEND_SHOW
&& ip
->m_multipart
)
962 case SEND_TODISP_ALL
:
966 switch (_pipecmd(&pipecomm
, ip
->m_ct_type_plain
)) {
968 _out(pipecomm
, strlen(pipecomm
), obuf
, CONV_NONE
, SEND_MBOX
, qf
,
979 goto jcopyout
; /* break; break; */
981 if (pipecomm
!= NULL
)
983 if (level
== 0 && cnt
) {
984 char const *x
= tr(210, "[Binary content]\n");
985 _out(x
, strlen(x
), obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
1000 case MIME_ALTERNATIVE
:
1001 if ((action
== SEND_TODISP
|| action
== SEND_QUOTE
) &&
1002 !ok_blook(print_alternatives
)) {
1003 bool_t doact
= FAL0
;
1005 for (np
= ip
->m_multipart
; np
; np
= np
->m_nextpart
)
1006 if (np
->m_mimecontent
== MIME_TEXT_PLAIN
)
1009 for (np
= ip
->m_multipart
; np
; np
= np
->m_nextpart
) {
1010 if (np
->m_ct_type_plain
!= NULL
&& action
!= SEND_QUOTE
) {
1011 _print_part_info(&rest
, np
, doign
, level
);
1012 _out(rest
.s
, rest
.l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
,
1015 if (doact
&& np
->m_mimecontent
== MIME_TEXT_PLAIN
) {
1017 rv
= sendpart(zmp
, np
, obuf
, doign
, qf
, action
, stats
,
1019 quoteflt_reset(qf
, origobuf
);
1032 case SEND_TODISP_ALL
:
1034 case SEND_QUOTE_ALL
:
1041 if ((action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
) &&
1042 ip
->m_multipart
!= NULL
&&
1043 ip
->m_multipart
->m_mimecontent
== MIME_DISCARD
&&
1044 ip
->m_multipart
->m_nextpart
== NULL
) {
1045 char const *x
= tr(85,
1046 "[Missing multipart boundary - use \"show\" to display "
1047 "the raw message]\n");
1048 _out(x
, strlen(x
), obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
1051 for (np
= ip
->m_multipart
; np
; np
= np
->m_nextpart
) {
1052 if (np
->m_mimecontent
== MIME_DISCARD
&& action
!= SEND_DECRYPT
)
1057 if (np
->m_partstring
&& !strcmp(np
->m_partstring
, "1"))
1060 if ((obuf
= newfile(np
, UNVOLATILE(&ispipe
))) == NULL
)
1064 if (sigsetjmp(_send_pipejmp
, 1)) {
1068 oldpipe
= safe_signal(SIGPIPE
, &_send_onpipe
);
1071 case SEND_TODISP_ALL
:
1072 case SEND_QUOTE_ALL
:
1073 if (ip
->m_mimecontent
!= MIME_MULTI
&&
1074 ip
->m_mimecontent
!= MIME_ALTERNATIVE
&&
1075 ip
->m_mimecontent
!= MIME_DIGEST
)
1077 _print_part_info(&rest
, np
, doign
, level
);
1078 _out(rest
.s
, rest
.l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
,
1095 if (sendpart(zmp
, np
, obuf
, doign
, qf
, action
, stats
, level
+1) < 0)
1097 quoteflt_reset(qf
, origobuf
);
1098 if (action
== SEND_QUOTE
)
1100 if (action
== SEND_TOFILE
&& obuf
!= origobuf
) {
1105 safe_signal(SIGPIPE
, SIG_IGN
);
1107 safe_signal(SIGPIPE
, oldpipe
);
1119 /* Copy out message body */
1121 if (doign
== allignore
&& level
== 0) /* skip final blank line */
1123 switch (ip
->m_mimeenc
) {
1130 convert
= CONV_NONE
;
1133 convert
= CONV_FROMQP
;
1136 switch (ip
->m_mimecontent
) {
1138 case MIME_TEXT_PLAIN
:
1139 case MIME_TEXT_HTML
:
1140 convert
= CONV_FROMB64_T
;
1143 convert
= CONV_FROMB64
;
1147 convert
= CONV_NONE
;
1150 if (action
== SEND_DECRYPT
|| action
== SEND_MBOX
||
1151 action
== SEND_RFC822
|| action
== SEND_SHOW
)
1152 convert
= CONV_NONE
;
1154 if ((action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
||
1155 action
== SEND_QUOTE
|| action
== SEND_QUOTE_ALL
||
1156 action
== SEND_TOSRCH
) &&
1157 (ip
->m_mimecontent
== MIME_TEXT_PLAIN
||
1158 ip
->m_mimecontent
== MIME_TEXT_HTML
||
1159 ip
->m_mimecontent
== MIME_TEXT
)) {
1160 char const *tcs
= charset_get_lc();
1162 if (iconvd
!= (iconv_t
)-1)
1163 n_iconv_close(iconvd
);
1164 /* TODO Since Base64 has an odd 4:3 relation in between input
1165 * TODO and output an input line may end with a partial
1166 * TODO multibyte character; this is no problem at all unless
1167 * TODO we send to the display or whatever, i.e., ensure
1168 * TODO makeprint() or something; to avoid this trap, *force*
1169 * TODO iconv(), in which case this layer will handle leftovers
1171 if (convert
== CONV_FROMB64_T
|| (asccasecmp(tcs
, ip
->m_charset
) &&
1172 asccasecmp(charset_get_7bit(), ip
->m_charset
))) {
1173 iconvd
= n_iconv_open(tcs
, ip
->m_charset
);
1174 /* XXX Don't bail out if we cannot iconv(3) here;
1175 * XXX alternatively we could avoid trying to open
1176 * XXX if ip->m_charset is "unknown-8bit", which was
1177 * XXX the one that has bitten me?? */
1179 * TODO errors should DEFINETELY not be scrolled away!
1180 * TODO what about an error buffer (think old shsp(1)),
1181 * TODO re-dump errors since last snapshot when the
1182 * TODO command loop enters again? i.e., at least print
1183 * TODO "There were errors ?" before the next prompt,
1184 * TODO so that the user can look at the error buffer?
1186 if (iconvd
== (iconv_t
)-1 && errno
== EINVAL
) {
1187 fprintf(stderr
, tr(179, "Cannot convert from %s to %s\n"),
1188 ip
->m_charset
, tcs
);
1189 /*rv = 1; goto jleave*/
1195 if (pipecomm
!= NULL
&& (action
== SEND_TODISP
||
1196 action
== SEND_TODISP_ALL
|| action
== SEND_QUOTE
||
1197 action
== SEND_QUOTE_ALL
)) {
1199 pbuf
= _pipefile(pipecomm
, UNVOLATILE(&qbuf
),
1200 (action
== SEND_QUOTE
|| action
== SEND_QUOTE_ALL
), !ispipe
);
1201 action
= SEND_TOPIPE
;
1203 oldpipe
= safe_signal(SIGPIPE
, &_send_onpipe
);
1204 if (sigsetjmp(_send_pipejmp
, 1))
1212 ui32_t save_qf_pfix_len
= qf
->qf_pfix_len
;
1213 off_t
*save_stats
= stats
;
1215 if (pbuf
!= origobuf
) {
1216 qf
->qf_pfix_len
= 0; /* XXX legacy (remove filter instead) */
1223 quoteflt_reset(qf
, pbuf
);
1224 while (!eof
&& fgetline(&line
, &linesize
, &cnt
, &linelen
, ibuf
, 0)) {
1226 if (_out(line
, linelen
, pbuf
, convert
, action
, qf
, stats
, &rest
) < 0 ||
1228 rv
= -1; /* XXX Should bail away?! */
1232 if (!eof
&& rest
.l
!= 0) {
1242 if (pbuf
!= origobuf
) {
1243 qf
->qf_pfix_len
= save_qf_pfix_len
;
1251 safe_signal(SIGPIPE
, SIG_IGN
);
1252 Pclose(pbuf
, ispipe
);
1253 safe_signal(SIGPIPE
, oldpipe
);
1255 pipecpy(qbuf
, obuf
, origobuf
, qf
, stats
);
1258 if (iconvd
!= (iconv_t
)-1)
1259 n_iconv_close(iconvd
);
1267 newfile(struct mimepart
*ip
, int *ispipe
)
1277 if (f
!= NULL
&& f
!= (char*)-1) {
1280 mime_fromhdr(&in
, &out
, TD_ISPR
);
1281 memcpy(f
, out
.s
, out
.l
);
1282 *(f
+ out
.l
) = '\0';
1286 if (options
& OPT_INTERACTIVE
) {
1289 printf(tr(278, "Enter filename for part %s (%s)"),
1290 ip
->m_partstring
? ip
->m_partstring
: "?",
1291 ip
->m_ct_type_plain
);
1292 f2
= readstr_input(": ", (f
!= (char*)-1) ? f
: NULL
);
1293 if (f2
== NULL
|| *f2
== '\0') {
1294 fprintf(stderr
, tr(279, "... skipping this\n"));
1297 } else if (*f2
== '|')
1298 /* Pipes are expanded by the shell */
1300 else if ((f3
= file_expand(f2
)) == NULL
)
1301 /* (Error message written by file_expand()) */
1306 if (f
== NULL
|| f
== (char *)-1) {
1313 cp
= ok_vlook(SHELL
);
1316 fp
= Popen(f
+ 1, "w", cp
, 1);
1317 if (!(*ispipe
= (fp
!= NULL
)))
1320 if ((fp
= Fopen(f
, "w")) == NULL
)
1321 fprintf(stderr
, tr(176, "Cannot open %s\n"), f
);
1329 pipecpy(FILE *pipebuf
, FILE *outbuf
, FILE *origobuf
, struct quoteflt
*qf
,
1333 size_t linesize
= 0, linelen
, cnt
;
1339 cnt
= fsize(pipebuf
);
1342 quoteflt_reset(qf
, outbuf
);
1343 while (fgetline(&line
, &linesize
, &cnt
, &linelen
, pipebuf
, 0) != NULL
) {
1344 if ((sz
= quoteflt_push(qf
, line
, linelen
)) < 0)
1348 if ((sz
= quoteflt_flush(qf
)) > 0)
1353 if (all_sz
> 0 && outbuf
== origobuf
)
1354 _addstats(stats
, 1, all_sz
);
1360 statusput(const struct message
*mp
, FILE *obuf
, struct quoteflt
*qf
,
1363 char statout
[3], *cp
= statout
;
1366 if (mp
->m_flag
& MREAD
)
1368 if ((mp
->m_flag
& MNEW
) == 0)
1372 int i
= fprintf(obuf
, "%.*sStatus: %s\n", (int)qf
->qf_pfix_len
,
1373 (qf
->qf_pfix_len
> 0 ? qf
->qf_pfix
: 0), statout
);
1375 _addstats(stats
, 1, i
);
1381 xstatusput(const struct message
*mp
, FILE *obuf
, struct quoteflt
*qf
,
1385 char *xp
= xstatout
;
1388 if (mp
->m_flag
& MFLAGGED
)
1390 if (mp
->m_flag
& MANSWERED
)
1392 if (mp
->m_flag
& MDRAFTED
)
1396 int i
= fprintf(obuf
, "%.*sX-Status: %s\n", (int)qf
->qf_pfix_len
,
1397 (qf
->qf_pfix_len
> 0 ? qf
->qf_pfix
: 0), xstatout
);
1399 _addstats(stats
, 1, i
);
1405 put_from_(FILE *fp
, struct mimepart
*ip
, off_t
*stats
)
1407 char const *froma
, *date
, *nl
;
1411 if (ip
&& ip
->m_from
) {
1413 date
= fakedate(ip
->m_time
);
1417 date
= time_current
.tc_ctime
;
1421 colour_put(fp
, COLOURSPEC_FROM_
);
1422 i
= fprintf(fp
, "From %s %s%s", froma
, date
, nl
);
1425 _addstats(stats
, (*nl
!= '\0'), i
);
1430 sendmp(struct message
*mp
, FILE *obuf
, struct ignoretab
*doign
,
1431 char const *prefix
, enum sendaction action
, off_t
*stats
)
1437 struct mimepart
*ip
;
1441 if (mp
== dot
&& action
!= SEND_TOSRCH
&& action
!= SEND_TOFLTR
)
1444 stats
[0] = stats
[1] = 0;
1445 quoteflt_init(&qf
, prefix
);
1447 /* First line is the From_ line, so no headers there to worry about */
1448 if ((ibuf
= setinput(&mb
, mp
, NEED_BODY
)) == NULL
)
1454 struct str
const *cpre
, *csuf
;
1456 cpre
= colour_get(COLOURSPEC_FROM_
);
1457 csuf
= colour_get(COLOURSPEC_RESET
);
1461 if (mp
->m_flag
& MNOFROM
) {
1462 if (doign
!= allignore
&& doign
!= fwdignore
&& action
!= SEND_RFC822
)
1463 sz
= fprintf(obuf
, "%s%.*sFrom %s %s%s\n",
1464 (cpre
!= NULL
? cpre
->s
: ""),
1465 (int)qf
.qf_pfix_len
, (qf
.qf_pfix_len
!= 0 ? qf
.qf_pfix
: ""),
1466 fakefrom(mp
), fakedate(mp
->m_time
),
1467 (csuf
!= NULL
? csuf
->s
: ""));
1469 if (doign
!= allignore
&& doign
!= fwdignore
&& action
!= SEND_RFC822
) {
1470 if (qf
.qf_pfix_len
> 0) {
1471 i
= fwrite(qf
.qf_pfix
, sizeof *qf
.qf_pfix
, qf
.qf_pfix_len
, obuf
);
1472 if (i
!= qf
.qf_pfix_len
)
1478 fputs(cpre
->s
, obuf
);
1479 cpre
= (struct str
const*)0x1;
1484 while (cnt
&& (c
= getc(ibuf
)) != EOF
) {
1485 if (doign
!= allignore
&& doign
!= fwdignore
&&
1486 action
!= SEND_RFC822
) {
1488 if (c
== '\n' && csuf
!= NULL
) {
1489 cpre
= (struct str
const*)0x1;
1490 fputs(csuf
->s
, obuf
);
1502 if (csuf
!= NULL
&& cpre
!= (struct str
const*)0x1)
1503 fputs(csuf
->s
, obuf
);
1508 _addstats(stats
, 1, sz
);
1511 if (action
!= SEND_MBOX
&& action
!= SEND_RFC822
&& action
!= SEND_SHOW
)
1512 pf
|= PARSE_DECRYPT
| PARSE_PARTS
;
1513 if ((ip
= parsemsg(mp
, pf
)) == NULL
)
1516 rv
= sendpart(mp
, ip
, obuf
, doign
, &qf
, action
, stats
, 0);
1518 quoteflt_destroy(&qf
);
1523 /* vim:set fenc=utf-8:s-it-mode */