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 - 2015 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. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #ifndef HAVE_AMALGAMATION
43 PIPE_NULL
, /* No pipe- mimetype handler */
44 PIPE_COMM
, /* Normal command */
45 PIPE_ASYNC
, /* Normal command, run asynchronous */
46 PIPE_TEXT
, /* @ special command to force treatment as text */
47 PIPE_MSG
/* Display message (returned as command string) */
50 static sigjmp_buf _send_pipejmp
;
52 /* Going for user display, print Part: info string */
53 static void _print_part_info(FILE *obuf
, struct mimepart
const *mpp
,
54 struct ignoretab
*doign
, int level
,
55 struct quoteflt
*qf
, ui64_t
*stats
);
57 /* Query possible pipe command for MIME part */
58 static enum pipeflags
_pipecmd(char const **result
, struct mimepart
const *mpp
);
60 /* Create a pipe; if mpp is not NULL, place some NAILENV_* environment
61 * variables accordingly */
62 static FILE * _pipefile(char const *pipecomm
, struct mimepart
const *mpp
,
63 FILE **qbuf
, bool_t quote
, bool_t async
);
65 /* Call mime_write() as approbiate and adjust statistics */
66 SINLINE ssize_t
_out(char const *buf
, size_t len
, FILE *fp
,
67 enum conversion convert
, enum sendaction action
,
68 struct quoteflt
*qf
, ui64_t
*stats
, struct str
*rest
);
71 static void _send_onpipe(int signo
);
74 static int sendpart(struct message
*zmp
, struct mimepart
*ip
,
75 FILE *obuf
, struct ignoretab
*doign
,
76 struct quoteflt
*qf
, enum sendaction action
,
77 ui64_t
*stats
, int level
);
79 /* Get a file for an attachment */
80 static FILE * newfile(struct mimepart
*ip
, int *ispipe
);
82 static void pipecpy(FILE *pipebuf
, FILE *outbuf
, FILE *origobuf
,
83 struct quoteflt
*qf
, ui64_t
*stats
);
85 /* Output a reasonable looking status field */
86 static void statusput(const struct message
*mp
, FILE *obuf
,
87 struct quoteflt
*qf
, ui64_t
*stats
);
88 static void xstatusput(const struct message
*mp
, FILE *obuf
,
89 struct quoteflt
*qf
, ui64_t
*stats
);
91 static void put_from_(FILE *fp
, struct mimepart
*ip
, ui64_t
*stats
);
94 _print_part_info(FILE *obuf
, struct mimepart
const *mpp
, /* TODO strtofmt.. */
95 struct ignoretab
*doign
, int level
, struct quoteflt
*qf
, ui64_t
*stats
)
98 struct str ti
= {NULL
, 0}, to
;
99 struct str
const *cpre
, *csuf
;
104 cpre
= colour_get(COLOURSPEC_PARTINFO
);
105 csuf
= colour_get(COLOURSPEC_RESET
);
110 /* Take care of "99.99", i.e., 5 */
111 if ((cp
= mpp
->m_partstring
) == NULL
|| cp
[0] == '\0')
113 if (level
|| (cp
[0] != '1' && cp
[1] == '\0'))
114 _out("\n", 1, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
116 _out(cpre
->s
, cpre
->l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
117 _out("[-- #", 5, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
118 _out(cp
, strlen(cp
), obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
120 to
.l
= snprintf(buf
, sizeof buf
, " %" PRIuZ
"/%" PRIuZ
" ",
121 (uiz_t
)mpp
->m_lines
, (uiz_t
)mpp
->m_size
);
122 _out(buf
, to
.l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
124 if ((cp
= mpp
->m_ct_type_usr_ovwr
) != NULL
)
125 _out("+", 1, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
127 cp
= mpp
->m_ct_type_plain
;
128 if ((to
.l
= strlen(cp
)) > 30 && is_asccaseprefix(cp
, "application/")) {
129 size_t const al
= sizeof("appl../") -1, fl
= sizeof("application/") -1;
130 size_t i
= to
.l
- fl
;
131 char *x
= salloc(al
+ i
+1);
133 memcpy(x
, "appl../", al
);
134 memcpy(x
+ al
, cp
+ fl
, i
+1);
138 _out(cp
, to
.l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
140 if (mpp
->m_multipart
== NULL
/* TODO */ && (cp
= mpp
->m_ct_enc
) != NULL
) {
141 _out(", ", 2, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
142 if (to
.l
> 25 && !asccasecmp(cp
, "quoted-printable"))
144 _out(cp
, strlen(cp
), obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
147 if (mpp
->m_multipart
== NULL
/* TODO */ && (cp
= mpp
->m_charset
) != NULL
) {
148 _out(", ", 2, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
149 _out(cp
, strlen(cp
), obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
152 _out(" --]", 4, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
154 _out(csuf
->s
, csuf
->l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
155 _out("\n", 1, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
157 if (is_ign("content-disposition", 19, doign
) && mpp
->m_filename
!= NULL
&&
158 *mpp
->m_filename
!= '\0') {
159 makeprint(n_str_add_cp(&ti
, mpp
->m_filename
), &to
);
161 to
.l
= delctrl(to
.s
, to
.l
);
164 _out(cpre
->s
, cpre
->l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
165 _out("[-- ", 4, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
166 _out(to
.s
, to
.l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
167 _out(" --]", 4, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
169 _out(csuf
->s
, csuf
->l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
170 _out("\n", 1, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
177 static enum pipeflags
178 _pipecmd(char const **result
, struct mimepart
const *mpp
)
186 /* Do we have any handler for this part? */
187 if ((cp
= mime_type_mimepart_handler(mpp
)) == NULL
)
189 else if (cp
== MIME_TYPE_HANDLER_TEXT
)
192 #ifdef HAVE_FILTER_HTML_TAGSOUP
193 cp
== MIME_TYPE_HANDLER_HTML
||
198 } else if (*++cp
== '\0') {
199 /* Treat as plain text */
201 } else if (!(pstate
& PS_MSGLIST_DIRECT
)) {
202 /* Viewing multiple messages in one go, don't block system */
203 *result
= _("[Directly address message only to display this]\n");
206 /* Viewing a single message only */
207 /* TODO send/MIME layer rewrite: when we have a single-pass parser
208 * TODO then the parsing phase and the send phase will be separated;
209 * TODO that allows us to ask a user *before* we start the send, i.e.,
210 * TODO *before* a pager pipe is setup */
212 /* Asynchronous command, normal command line */
213 *result
= ++cp
, rv
= PIPE_ASYNC
;
215 *result
= cp
, rv
= PIPE_COMM
;
222 _pipefile(char const *pipecomm
, struct mimepart
const *mpp
, FILE **qbuf
,
223 bool_t quote
, bool_t async
)
226 char const *env_addon
[8], *cp
, *sh
;
233 if ((*qbuf
= Ftmp(NULL
, "sendp", OF_RDWR
| OF_UNLINK
| OF_REGISTER
,
235 n_perr(_("tmpfile"), 0);
241 #ifdef HAVE_FILTER_HTML_TAGSOUP
242 if (pipecomm
== MIME_TYPE_HANDLER_HTML
) {
243 union {int (*ptf
)(void); char const *sh
;} u
;
244 u
.ptf
= &htmlflt_process_main
;
245 rbuf
= Popen((char*)-1, "W", u
.sh
, NULL
, fileno(*qbuf
));
246 pipecomm
= "Builtin HTML tagsoup filter";
247 goto jafter_tagsoup_hack
;
252 if (mpp
== NULL
|| (cp
= mpp
->m_filename
) == NULL
)
254 env_addon
[0] = str_concat_csvl(&s
, NAILENV_FILENAME
, "=", cp
, NULL
)->s
;
256 /* NAIL_FILENAME_GENERATED *//* TODO pathconf NAME_MAX; but user can create
257 * TODO a file wherever he wants! *Do* create a zero-size temporary file
258 * TODO and give *that* path as NAIL_FILENAME_TEMPORARY, clean it up once
259 * TODO the pipe returns? Like this we *can* verify path/name issues! */
260 env_addon
[1] = str_concat_csvl(&s
, NAILENV_FILENAME_GENERATED
, "=",
261 getrandstring(MIN(NAME_MAX
/ 4, 16)), NULL
)->s
;
263 /* NAIL_CONTENT{,_EVIDENCE} */
264 if (mpp
== NULL
|| (cp
= mpp
->m_ct_type_plain
) == NULL
)
266 env_addon
[2] = str_concat_csvl(&s
, NAILENV_CONTENT
, "=", cp
, NULL
)->s
;
268 if (mpp
!= NULL
&& mpp
->m_ct_type_usr_ovwr
!= NULL
)
269 cp
= mpp
->m_ct_type_usr_ovwr
;
270 env_addon
[3] = str_concat_csvl(&s
, NAILENV_CONTENT_EVIDENCE
, "=", cp
,
273 env_addon
[4] = str_concat_csvl(&s
, NAILENV_TMPDIR
, "=", tempdir
, NULL
)->s
;
274 env_addon
[5] = str_concat_csvl(&s
, "TMPDIR", "=", tempdir
, NULL
)->s
;
278 if ((sh
= ok_vlook(SHELL
)) == NULL
)
281 rbuf
= Popen(pipecomm
, "W", sh
, env_addon
, (async
? -1 : fileno(*qbuf
)));
282 #ifdef HAVE_FILTER_HTML_TAGSOUP
286 n_err(_("Cannot run MIME type handler \"%s\": %s\n"),
287 pipecomm
, strerror(errno
));
298 _out(char const *buf
, size_t len
, FILE *fp
, enum conversion convert
, enum
299 sendaction action
, struct quoteflt
*qf
, ui64_t
*stats
, struct str
*rest
)
306 Well
... it turns out to
not work like that since of course a valid
307 RFC
4155 compliant parser
, like S
-nail
, takes care
for From_ lines only
308 after an empty line has been seen
, which cannot be detected that easily
310 ifdef HAVE_DEBUG
/* TODO assert legacy */
311 /* TODO if at all, this CAN only happen for SEND_DECRYPT, since all
312 * TODO other input situations handle RFC 4155 OR, if newly generated,
313 * TODO enforce quoted-printable if there is From_, as "required" by
314 * TODO RFC 5751. The SEND_DECRYPT case is not yet overhauled;
315 * TODO if it may happen in this path, we should just treat decryption
316 * TODO as we do for the other input paths; i.e., handle it in SSL!! */
317 if (action
== SEND_MBOX
|| action
== SEND_DECRYPT
)
318 assert(!is_head(buf
, len
, TRU1
));
320 if ((/*action == SEND_MBOX ||*/ action
== SEND_DECRYPT
) &&
321 is_head(buf
, len
, TRU1
)) {
327 flags
= ((int)action
& _TD_EOF
);
329 n
= mime_write(buf
, len
, fp
,
330 action
== SEND_MBOX
? CONV_NONE
: convert
,
331 flags
| ((action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
||
332 action
== SEND_QUOTE
|| action
== SEND_QUOTE_ALL
)
334 : (action
== SEND_TOSRCH
|| action
== SEND_TOPIPE
)
335 ? TD_ICONV
: (action
== SEND_SHOW
? TD_ISPR
: TD_NONE
)),
349 _send_onpipe(int signo
)
351 NYD_X
; /* Signal handler */
353 siglongjmp(_send_pipejmp
, 1);
356 static sigjmp_buf __sendp_actjmp
; /* TODO someday.. */
357 static int __sendp_sig
; /* TODO someday.. */
358 static sighandler_type __sendp_opipe
;
360 __sendp_onsig(int sig
) /* TODO someday, we won't need it no more */
362 NYD_X
; /* Signal handler */
364 siglongjmp(__sendp_actjmp
, 1);
367 static sigjmp_buf __sndalter_actjmp
; /* TODO someday.. */
368 static int __sndalter_sig
; /* TODO someday.. */
370 __sndalter_onsig(int sig
) /* TODO someday, we won't need it no more */
372 NYD_X
; /* Signal handler */
373 __sndalter_sig
= sig
;
374 siglongjmp(__sndalter_actjmp
, 1);
378 sendpart(struct message
*zmp
, struct mimepart
*ip
, FILE * volatile obuf
,
379 struct ignoretab
*doign
, struct quoteflt
*qf
,
380 enum sendaction
volatile action
, ui64_t
* volatile stats
, int level
)
382 int volatile ispipe
, rv
= 0;
384 char *line
= NULL
, *cp
, *cp2
, *start
;
385 char const *pipecomm
= NULL
;
386 size_t linesize
= 0, linelen
, cnt
;
387 int dostat
, infld
= 0, ignoring
= 1, isenc
, c
;
388 struct mimepart
*volatile np
;
389 FILE * volatile ibuf
= NULL
, * volatile pbuf
= obuf
, * volatile qbuf
= obuf
,
391 enum conversion
volatile convert
;
392 sighandler_type
volatile oldpipe
= SIG_DFL
;
396 if (ip
->m_mimecontent
== MIME_PKCS7
&& ip
->m_multipart
&&
397 action
!= SEND_MBOX
&& action
!= SEND_RFC822
&& action
!= SEND_SHOW
)
403 if (!is_ign("status", 6, doign
))
405 if (!is_ign("x-status", 8, doign
))
410 if ((ibuf
= setinput(&mb
, (struct message
*)ip
, NEED_BODY
)) == NULL
) {
416 if (ip
->m_mimecontent
== MIME_DISCARD
)
419 if (!(ip
->m_flag
& MNOFROM
))
420 while (cnt
&& (c
= getc(ibuf
)) != EOF
) {
426 convert
= (action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
||
427 action
== SEND_QUOTE
|| action
== SEND_QUOTE_ALL
||
428 action
== SEND_TOSRCH
)
429 ? CONV_FROMHDR
: CONV_NONE
;
431 /* Work the headers */
432 quoteflt_reset(qf
, obuf
);
433 while (fgetline(&line
, &linesize
, &cnt
, &linelen
, ibuf
, 0)) {
435 if (line
[0] == '\n') {
436 /* If line is blank, we've reached end of headers, so force out
437 * status: field and note that we are no longer in header fields */
439 statusput(zmp
, obuf
, qf
, stats
);
441 xstatusput(zmp
, obuf
, qf
, stats
);
442 if (doign
!= allignore
)
443 _out("\n", 1, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
448 if (infld
&& blankchar(line
[0])) {
449 /* If this line is a continuation (SP / HT) of a previous header
450 * field, determine if the start of the line is a MIME encoded word */
452 for (cp
= line
; blankchar(*cp
); ++cp
);
453 if (cp
> line
&& linelen
- PTR2SIZE(cp
- line
) > 8 &&
454 cp
[0] == '=' && cp
[1] == '?')
458 /* Pick up the header field if we have one */
459 for (cp
= line
; (c
= *cp
& 0377) && c
!= ':' && !spacechar(c
); ++cp
)
462 while (spacechar(*cp
))
464 if (cp
[0] != ':' && level
== 0 && lineno
== 1) {
465 /* Not a header line, force out status: This happens in uucp style
466 * mail where there are no headers at all */
468 statusput(zmp
, obuf
, qf
, stats
);
470 xstatusput(zmp
, obuf
, qf
, stats
);
471 if (doign
!= allignore
)
472 _out("\n", 1, obuf
, CONV_NONE
,SEND_MBOX
, qf
, stats
, NULL
);
476 /* If it is an ignored field and we care about such things, skip it.
477 * Misuse dostat also for another bit xxx use a bitenum + for more */
478 if (ok_blook(keep_content_length
))
481 *cp2
= 0; /* temporarily null terminate */
482 if ((doign
&& is_ign(line
, PTR2SIZE(cp2
- line
), doign
)) ||
483 (action
== SEND_MBOX
&& !(dostat
& (1 << 2)) &&
484 (!asccasecmp(line
, "content-length") ||
485 !asccasecmp(line
, "lines"))))
487 else if (!asccasecmp(line
, "status")) {
488 /* If field is "status," go compute and print real Status: field */
490 statusput(zmp
, obuf
, qf
, stats
);
494 } else if (!asccasecmp(line
, "x-status")) {
495 /* If field is "status," go compute and print real Status: field */
497 xstatusput(zmp
, obuf
, qf
, stats
);
503 /* For colourization we need the complete line, so save it */
504 /* XXX This is all temporary (colour belongs into backend), so
505 * XXX use pipecomm as a temporary storage in the meanwhile */
507 if (colour_table
!= NULL
)
508 pipecomm
= savestrbuf(line
, PTR2SIZE(cp2
- line
));
516 /* Determine if the end of the line is a MIME encoded word */
517 /* TODO geeeh! all this lengthy stuff that follows is about is dealing
518 * TODO with header follow lines, and it should be up to the backend
519 * TODO what happens and what not, i.e., it doesn't matter wether it's
520 * TODO a MIME-encoded word or not, as long as a single separating space
521 * TODO remains in between lines (the MIME stuff will correctly remove
522 * TODO whitespace in between multiple adjacent encoded words) */
524 if (cnt
&& (c
= getc(ibuf
)) != EOF
) {
526 cp
= line
+ linelen
- 1;
527 if (linelen
> 0 && *cp
== '\n')
529 while (cp
>= line
&& whitechar(*cp
))
531 if (PTR2SIZE(cp
- line
> 8) && cp
[0] == '=' && cp
[-1] == '?')
538 size_t len
= linelen
;
540 if (action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
||
541 action
== SEND_QUOTE
|| action
== SEND_QUOTE_ALL
||
542 action
== SEND_TOSRCH
) {
543 /* Strip blank characters if two MIME-encoded words follow on
544 * continuing lines */
546 while (len
> 0 && blankchar(*start
)) {
551 if (len
> 0 && start
[len
- 1] == '\n')
553 while (len
> 0 && blankchar(start
[len
- 1]))
558 bool_t colour_stripped
= FAL0
;
559 if (pipecomm
!= NULL
) {
560 colour_put_header(obuf
, pipecomm
);
561 if (len
> 0 && start
[len
- 1] == '\n') {
562 colour_stripped
= TRU1
;
567 _out(start
, len
, obuf
, convert
, action
, qf
, stats
, NULL
);
569 if (pipecomm
!= NULL
) {
570 colour_reset(obuf
); /* XXX reset after \n!! */
589 switch (ip
->m_mimecontent
) {
593 case SEND_TODISP_ALL
:
596 if (ok_blook(rfc822_body_from_
)) {
597 if (qf
->qf_pfix_len
> 0) {
598 size_t i
= fwrite(qf
->qf_pfix
, sizeof *qf
->qf_pfix
,
599 qf
->qf_pfix_len
, obuf
);
600 if (i
== qf
->qf_pfix_len
&& stats
!= NULL
)
603 put_from_(obuf
, ip
->m_multipart
, stats
);
611 if (ok_blook(rfc822_body_from_
))
612 put_from_(obuf
, ip
->m_multipart
, stats
);
622 case MIME_TEXT_PLAIN
:
625 case SEND_TODISP_ALL
:
629 switch (_pipecmd(&pipecomm
, ip
)) {
631 _out(pipecomm
, strlen(pipecomm
), obuf
, CONV_NONE
, SEND_MBOX
, qf
,
633 /* We would print this as plain text, so better force going home */
649 if (action
!= SEND_DECRYPT
)
653 if (action
!= SEND_MBOX
&& action
!= SEND_RFC822
&&
654 action
!= SEND_SHOW
&& ip
->m_multipart
!= NULL
)
660 case SEND_TODISP_ALL
:
664 switch (_pipecmd(&pipecomm
, ip
)) {
666 _out(pipecomm
, strlen(pipecomm
), obuf
, CONV_NONE
, SEND_MBOX
, qf
,
677 goto jcopyout
; /* break; break; */
679 if (pipecomm
!= NULL
)
681 if (level
== 0 && cnt
) {
682 char const *x
= _("[Binary content]\n");
683 _out(x
, strlen(x
), obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
696 case MIME_ALTERNATIVE
:
698 if ((action
== SEND_TODISP
|| action
== SEND_QUOTE
) &&
699 !ok_blook(print_alternatives
)) {
700 /* XXX This (a) should not remain (b) should be own fun */
702 struct mpstack
*outer
;
704 } outermost
, * volatile curr
= &outermost
, * volatile mpsp
;
705 sighandler_type
volatile opsh
, oish
, ohsh
;
706 size_t volatile partcnt
= 0/* silence CC */;
707 bool_t
volatile neednl
= FAL0
;
713 opsh
= safe_signal(SIGPIPE
, &__sndalter_onsig
);
714 oish
= safe_signal(SIGINT
, &__sndalter_onsig
);
715 ohsh
= safe_signal(SIGHUP
, &__sndalter_onsig
);
716 if (sigsetjmp(__sndalter_actjmp
, 1)) {
721 for (np
= ip
->m_multipart
;;) {
724 for (; np
!= NULL
; np
= np
->m_nextpart
) {
725 if (action
!= SEND_QUOTE
&& np
->m_ct_type_plain
!= NULL
) {
727 _out("\n", 1, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
728 _print_part_info(obuf
, np
, doign
, level
, qf
, stats
);
732 switch (np
->m_mimecontent
) {
733 case MIME_ALTERNATIVE
:
737 mpsp
= ac_alloc(sizeof *mpsp
);
739 mpsp
->mp
= np
->m_multipart
;
746 switch (_pipecmd(&pipecomm
, np
)) {
753 case MIME_TEXT_PLAIN
:
755 if (action
== SEND_QUOTE
&& partcnt
> 1 &&
756 ip
->m_mimecontent
== MIME_ALTERNATIVE
)
759 if (action
== SEND_QUOTE
&& partcnt
> 1) {
760 struct quoteflt
*dummy
= quoteflt_dummy();
761 _out("\n", 1, obuf
, CONV_NONE
, SEND_MBOX
, dummy
, stats
,
763 quoteflt_flush(dummy
);
766 rv
= sendpart(zmp
, np
, obuf
, doign
, qf
, action
, stats
,
768 quoteflt_reset(qf
, origobuf
);
772 for (;; curr
= mpsp
) {
773 if ((mpsp
= curr
->outer
) == NULL
)
787 np
= curr
->mp
->m_nextpart
;
789 safe_signal(SIGHUP
, ohsh
);
790 safe_signal(SIGINT
, oish
);
791 safe_signal(SIGPIPE
, opsh
);
792 if (__sndalter_sig
!= 0)
793 n_raise(__sndalter_sig
);
801 case SEND_TODISP_ALL
:
809 if ((action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
) &&
810 ip
->m_multipart
!= NULL
&&
811 ip
->m_multipart
->m_mimecontent
== MIME_DISCARD
&&
812 ip
->m_multipart
->m_nextpart
== NULL
) {
813 char const *x
= _("[Missing multipart boundary - use \"show\" "
814 "to display the raw message]\n");
815 _out(x
, strlen(x
), obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
818 for (np
= ip
->m_multipart
; np
!= NULL
; np
= np
->m_nextpart
) {
819 if (np
->m_mimecontent
== MIME_DISCARD
&& action
!= SEND_DECRYPT
)
824 if (np
->m_partstring
&& !strcmp(np
->m_partstring
, "1"))
827 /* TODO Always open multipart on /dev/null, it's a hack to be
828 * TODO able to dive into that structure, and still better
829 * TODO than asking the user for something stupid.
830 * TODO oh, wait, we did ask for a filename for this MIME mail,
831 * TODO and that outer container is useless anyway ;-P */
832 if (np
->m_multipart
!= NULL
) {
833 if ((obuf
= Fopen("/dev/null", "w")) == NULL
)
835 } else if ((obuf
= newfile(np
, UNVOLATILE(&ispipe
))) == NULL
)
839 if (sigsetjmp(_send_pipejmp
, 1)) {
843 oldpipe
= safe_signal(SIGPIPE
, &_send_onpipe
);
846 case SEND_TODISP_ALL
:
848 if (ip
->m_mimecontent
!= MIME_MULTI
&&
849 ip
->m_mimecontent
!= MIME_ALTERNATIVE
&&
850 ip
->m_mimecontent
!= MIME_RELATED
&&
851 ip
->m_mimecontent
!= MIME_DIGEST
)
853 _print_part_info(obuf
, np
, doign
, level
, qf
, stats
);
866 if (sendpart(zmp
, np
, obuf
, doign
, qf
, action
, stats
, level
+1) < 0)
868 quoteflt_reset(qf
, origobuf
);
870 if (action
== SEND_QUOTE
)
872 if (action
== SEND_TOFILE
&& obuf
!= origobuf
) {
877 safe_signal(SIGPIPE
, SIG_IGN
);
879 safe_signal(SIGPIPE
, oldpipe
);
891 /* Copy out message body */
893 if (doign
== allignore
&& level
== 0) /* skip final blank line */
895 switch (ip
->m_mime_enc
) {
902 convert
= CONV_FROMQP
;
905 switch (ip
->m_mimecontent
) {
907 case MIME_TEXT_PLAIN
:
909 convert
= CONV_FROMB64_T
;
912 convert
= CONV_FROMB64
;
919 if (action
== SEND_DECRYPT
|| action
== SEND_MBOX
||
920 action
== SEND_RFC822
|| action
== SEND_SHOW
)
923 if ((action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
||
924 action
== SEND_QUOTE
|| action
== SEND_QUOTE_ALL
||
925 action
== SEND_TOSRCH
) &&
926 (ip
->m_mimecontent
== MIME_TEXT_PLAIN
||
927 ip
->m_mimecontent
== MIME_TEXT_HTML
||
928 ip
->m_mimecontent
== MIME_TEXT
)) {
929 char const *tcs
= charset_get_lc();
931 if (iconvd
!= (iconv_t
)-1)
932 n_iconv_close(iconvd
);
933 /* TODO Since Base64 has an odd 4:3 relation in between input
934 * TODO and output an input line may end with a partial
935 * TODO multibyte character; this is no problem at all unless
936 * TODO we send to the display or whatever, i.e., ensure
937 * TODO makeprint() or something; to avoid this trap, *force*
938 * TODO iconv(), in which case this layer will handle leftovers
940 if (convert
== CONV_FROMB64_T
|| (asccasecmp(tcs
, ip
->m_charset
) &&
941 asccasecmp(charset_get_7bit(), ip
->m_charset
))) {
942 iconvd
= n_iconv_open(tcs
, ip
->m_charset
);
944 * TODO errors should DEFINETELY not be scrolled away!
945 * TODO what about an error buffer (think old shsp(1)),
946 * TODO re-dump errors since last snapshot when the
947 * TODO command loop enters again? i.e., at least print
948 * TODO "There were errors ?" before the next prompt,
949 * TODO so that the user can look at the error buffer?
951 if (iconvd
== (iconv_t
)-1 && errno
== EINVAL
) {
952 n_err(_("Cannot convert from %s to %s\n"), ip
->m_charset
, tcs
);
953 /*rv = 1; goto jleave;*/
959 if (pipecomm
!= NULL
&& (action
== SEND_TODISP
||
960 action
== SEND_TODISP_ALL
|| action
== SEND_QUOTE
||
961 action
== SEND_QUOTE_ALL
)) {
963 pbuf
= _pipefile(pipecomm
, ip
, UNVOLATILE(&qbuf
),
964 (action
== SEND_QUOTE
|| action
== SEND_QUOTE_ALL
), !ispipe
);
967 if (iconvd
!= (iconv_t
)-1)
968 n_iconv_close(iconvd
);
973 action
= SEND_TOPIPE
;
975 oldpipe
= safe_signal(SIGPIPE
, &_send_onpipe
);
976 if (sigsetjmp(_send_pipejmp
, 1))
984 ui32_t save_qf_pfix_len
= qf
->qf_pfix_len
;
985 ui64_t
*save_stats
= stats
;
987 if (pbuf
!= origobuf
) {
988 qf
->qf_pfix_len
= 0; /* XXX legacy (remove filter instead) */
997 __sendp_opipe
= safe_signal(SIGPIPE
, &__sendp_onsig
);
998 if (sigsetjmp(__sendp_actjmp
, 1)) {
1003 if (iconvd
!= (iconv_t
)-1)
1004 n_iconv_close(iconvd
);
1006 safe_signal(SIGPIPE
, __sendp_opipe
);
1007 n_raise(__sendp_sig
);
1011 quoteflt_reset(qf
, pbuf
);
1012 while (!eof
&& fgetline(&line
, &linesize
, &cnt
, &linelen
, ibuf
, 0)) {
1014 if (_out(line
, linelen
, pbuf
, convert
, action
, qf
, stats
, &rest
) < 0 ||
1016 rv
= -1; /* XXX Should bail away?! */
1020 if (!eof
&& rest
.l
!= 0) {
1027 safe_signal(SIGPIPE
, __sendp_opipe
);
1033 if (pbuf
!= origobuf
) {
1034 qf
->qf_pfix_len
= save_qf_pfix_len
;
1043 safe_signal(SIGPIPE
, SIG_IGN
);
1044 Pclose(pbuf
, ispipe
);
1045 safe_signal(SIGPIPE
, oldpipe
);
1046 if (qbuf
!= NULL
&& qbuf
!= obuf
)
1047 pipecpy(qbuf
, obuf
, origobuf
, qf
, stats
);
1050 if (iconvd
!= (iconv_t
)-1)
1051 n_iconv_close(iconvd
);
1059 newfile(struct mimepart
*ip
, int *ispipe
)
1069 if (f
!= NULL
&& f
!= (char*)-1) {
1072 makeprint(&in
, &out
);
1073 out
.l
= delctrl(out
.s
, out
.l
);
1074 f
= savestrbuf(out
.s
, out
.l
);
1078 if (options
& OPT_INTERACTIVE
) {
1081 printf(_("Enter filename for part %s (%s)"),
1082 (ip
->m_partstring
!= NULL
) ? ip
->m_partstring
: "?",
1083 ip
->m_ct_type_plain
);
1084 f2
= n_input_cp_addhist(": ", ((f
!= (char*)-1 && f
!= NULL
)
1085 ? fexpand_nshell_quote(f
) : NULL
), TRU1
);
1086 if (f2
== NULL
|| *f2
== '\0') {
1087 if (options
& OPT_D_V
)
1088 n_err(_("... skipping this\n"));
1091 } else if (*f2
== '|')
1092 /* Pipes are expanded by the shell */
1094 else if ((f3
= fexpand(f2
, FEXP_LOCAL
| FEXP_NSHELL
)) == NULL
)
1095 /* (Error message written by fexpand()) */
1100 if (f
== NULL
|| f
== (char*)-1) {
1107 cp
= ok_vlook(SHELL
);
1110 fp
= Popen(f
+ 1, "w", cp
, NULL
, 1);
1111 if (!(*ispipe
= (fp
!= NULL
)))
1114 if ((fp
= Fopen(f
, "w")) == NULL
)
1115 n_err(_("Cannot open \"%s\"\n"), f
);
1123 pipecpy(FILE *pipebuf
, FILE *outbuf
, FILE *origobuf
, struct quoteflt
*qf
,
1126 char *line
= NULL
; /* TODO line pool */
1127 size_t linesize
= 0, linelen
, cnt
;
1133 cnt
= fsize(pipebuf
);
1136 quoteflt_reset(qf
, outbuf
);
1137 while (fgetline(&line
, &linesize
, &cnt
, &linelen
, pipebuf
, 0) != NULL
) {
1138 if ((sz
= quoteflt_push(qf
, line
, linelen
)) < 0)
1142 if ((sz
= quoteflt_flush(qf
)) > 0)
1147 if (all_sz
> 0 && outbuf
== origobuf
&& stats
!= NULL
)
1154 statusput(const struct message
*mp
, FILE *obuf
, struct quoteflt
*qf
,
1157 char statout
[3], *cp
= statout
;
1160 if (mp
->m_flag
& MREAD
)
1162 if (!(mp
->m_flag
& MNEW
))
1166 int i
= fprintf(obuf
, "%.*sStatus: %s\n", (int)qf
->qf_pfix_len
,
1167 (qf
->qf_pfix_len
> 0 ? qf
->qf_pfix
: 0), statout
);
1168 if (i
> 0 && stats
!= NULL
)
1175 xstatusput(const struct message
*mp
, FILE *obuf
, struct quoteflt
*qf
,
1179 char *xp
= xstatout
;
1182 if (mp
->m_flag
& MFLAGGED
)
1184 if (mp
->m_flag
& MANSWERED
)
1186 if (mp
->m_flag
& MDRAFTED
)
1190 int i
= fprintf(obuf
, "%.*sX-Status: %s\n", (int)qf
->qf_pfix_len
,
1191 (qf
->qf_pfix_len
> 0 ? qf
->qf_pfix
: 0), xstatout
);
1192 if (i
> 0 && stats
!= NULL
)
1199 put_from_(FILE *fp
, struct mimepart
*ip
, ui64_t
*stats
)
1201 char const *froma
, *date
, *nl
;
1205 if (ip
!= NULL
&& ip
->m_from
!= NULL
) {
1207 date
= fakedate(ip
->m_time
);
1211 date
= time_current
.tc_ctime
;
1215 colour_put(fp
, COLOURSPEC_FROM_
);
1216 i
= fprintf(fp
, "From %s %s%s", froma
, date
, nl
);
1218 if (i
> 0 && stats
!= NULL
)
1224 sendmp(struct message
*mp
, FILE *obuf
, struct ignoretab
*doign
,
1225 char const *prefix
, enum sendaction action
, ui64_t
*stats
)
1230 enum mime_parse_flags mpf
;
1231 struct mimepart
*ip
;
1235 if (mp
== dot
&& action
!= SEND_TOSRCH
)
1236 pstate
|= PS_DID_PRINT_DOT
;
1239 quoteflt_init(&qf
, prefix
);
1241 /* First line is the From_ line, so no headers there to worry about */
1242 if ((ibuf
= setinput(&mb
, mp
, NEED_BODY
)) == NULL
)
1248 struct str
const *cpre
, *csuf
;
1250 cpre
= colour_get(COLOURSPEC_FROM_
);
1251 csuf
= colour_get(COLOURSPEC_RESET
);
1255 if (mp
->m_flag
& MNOFROM
) {
1256 if (doign
!= allignore
&& doign
!= fwdignore
&& action
!= SEND_RFC822
)
1257 sz
= fprintf(obuf
, "%s%.*sFrom %s %s%s\n",
1258 (cpre
!= NULL
? cpre
->s
: ""),
1259 (int)qf
.qf_pfix_len
, (qf
.qf_pfix_len
!= 0 ? qf
.qf_pfix
: ""),
1260 fakefrom(mp
), fakedate(mp
->m_time
),
1261 (csuf
!= NULL
? csuf
->s
: ""));
1263 if (doign
!= allignore
&& doign
!= fwdignore
&& action
!= SEND_RFC822
) {
1264 if (qf
.qf_pfix_len
> 0) {
1265 i
= fwrite(qf
.qf_pfix
, sizeof *qf
.qf_pfix
, qf
.qf_pfix_len
, obuf
);
1266 if (i
!= qf
.qf_pfix_len
)
1272 fputs(cpre
->s
, obuf
);
1273 cpre
= (struct str
const*)0x1;
1278 while (cnt
> 0 && (c
= getc(ibuf
)) != EOF
) {
1279 if (doign
!= allignore
&& doign
!= fwdignore
&&
1280 action
!= SEND_RFC822
) {
1282 if (c
== '\n' && csuf
!= NULL
) {
1283 cpre
= (struct str
const*)0x1;
1284 fputs(csuf
->s
, obuf
);
1296 if (csuf
!= NULL
&& cpre
!= (struct str
const*)0x1)
1297 fputs(csuf
->s
, obuf
);
1301 if (sz
> 0 && stats
!= NULL
)
1304 mpf
= MIME_PARSE_NONE
;
1305 if (action
!= SEND_MBOX
&& action
!= SEND_RFC822
&& action
!= SEND_SHOW
)
1306 mpf
|= MIME_PARSE_DECRYPT
| MIME_PARSE_PARTS
;
1307 if ((ip
= mime_parse_msg(mp
, mpf
)) == NULL
)
1310 rv
= sendpart(mp
, ip
, obuf
, doign
, &qf
, action
, stats
, 0);
1312 quoteflt_destroy(&qf
);