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
42 static sigjmp_buf _send_pipejmp
;
44 /* Going for user display, print Part: info string */
45 static void _print_part_info(FILE *obuf
, struct mimepart
const *mpp
,
46 struct ignoretab
*doign
, int level
,
47 struct quoteflt
*qf
, ui64_t
*stats
);
49 /* Create a pipe; if mpp is not NULL, place some NAILENV_* environment
50 * variables accordingly */
51 static FILE * _pipefile(struct mime_handler
*mhp
,
52 struct mimepart
const *mpp
, FILE **qbuf
,
53 char const *tmpfile
, int term_infd
);
55 /* Call mime_write() as approbiate and adjust statistics */
56 SINLINE ssize_t
_out(char const *buf
, size_t len
, FILE *fp
,
57 enum conversion convert
, enum sendaction action
,
58 struct quoteflt
*qf
, ui64_t
*stats
, struct str
*rest
);
61 static void _send_onpipe(int signo
);
64 static int sendpart(struct message
*zmp
, struct mimepart
*ip
,
65 FILE *obuf
, struct ignoretab
*doign
,
66 struct quoteflt
*qf
, enum sendaction action
,
67 ui64_t
*stats
, int level
);
69 /* Get a file for an attachment */
70 static FILE * newfile(struct mimepart
*ip
, bool_t
*ispipe
);
72 static void pipecpy(FILE *pipebuf
, FILE *outbuf
, FILE *origobuf
,
73 struct quoteflt
*qf
, ui64_t
*stats
);
75 /* Output a reasonable looking status field */
76 static void statusput(const struct message
*mp
, FILE *obuf
,
77 struct quoteflt
*qf
, ui64_t
*stats
);
78 static void xstatusput(const struct message
*mp
, FILE *obuf
,
79 struct quoteflt
*qf
, ui64_t
*stats
);
81 static void put_from_(FILE *fp
, struct mimepart
*ip
, ui64_t
*stats
);
84 _print_part_info(FILE *obuf
, struct mimepart
const *mpp
, /* TODO strtofmt.. */
85 struct ignoretab
*doign
, int level
, struct quoteflt
*qf
, ui64_t
*stats
)
88 struct str ti
= {NULL
, 0}, to
;
89 struct str
const *cpre
, *csuf
;
94 cpre
= colour_get(COLOURSPEC_PARTINFO
);
95 csuf
= colour_get(COLOURSPEC_RESET
);
100 /* Take care of "99.99", i.e., 5 */
101 if ((cp
= mpp
->m_partstring
) == NULL
|| cp
[0] == '\0')
103 if (level
|| (cp
[0] != '1' && cp
[1] == '\0'))
104 _out("\n", 1, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
106 _out(cpre
->s
, cpre
->l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
107 _out("[-- #", 5, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
108 _out(cp
, strlen(cp
), obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
110 to
.l
= snprintf(buf
, sizeof buf
, " %" PRIuZ
"/%" PRIuZ
" ",
111 (uiz_t
)mpp
->m_lines
, (uiz_t
)mpp
->m_size
);
112 _out(buf
, to
.l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
114 if ((cp
= mpp
->m_ct_type_usr_ovwr
) != NULL
)
115 _out("+", 1, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
117 cp
= mpp
->m_ct_type_plain
;
118 if ((to
.l
= strlen(cp
)) > 30 && is_asccaseprefix(cp
, "application/")) {
119 size_t const al
= sizeof("appl../") -1, fl
= sizeof("application/") -1;
120 size_t i
= to
.l
- fl
;
121 char *x
= salloc(al
+ i
+1);
123 memcpy(x
, "appl../", al
);
124 memcpy(x
+ al
, cp
+ fl
, i
+1);
128 _out(cp
, to
.l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
130 if (mpp
->m_multipart
== NULL
/* TODO */ && (cp
= mpp
->m_ct_enc
) != NULL
) {
131 _out(", ", 2, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
132 if (to
.l
> 25 && !asccasecmp(cp
, "quoted-printable"))
134 _out(cp
, strlen(cp
), obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
137 if (mpp
->m_multipart
== NULL
/* TODO */ && (cp
= mpp
->m_charset
) != NULL
) {
138 _out(", ", 2, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
139 _out(cp
, strlen(cp
), obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
142 _out(" --]", 4, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
144 _out(csuf
->s
, csuf
->l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
145 _out("\n", 1, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
147 if (is_ign("content-disposition", 19, doign
) && mpp
->m_filename
!= NULL
&&
148 *mpp
->m_filename
!= '\0') {
149 makeprint(n_str_add_cp(&ti
, mpp
->m_filename
), &to
);
151 to
.l
= delctrl(to
.s
, to
.l
);
154 _out(cpre
->s
, cpre
->l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
155 _out("[-- ", 4, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
156 _out(to
.s
, to
.l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
157 _out(" --]", 4, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
159 _out(csuf
->s
, csuf
->l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
160 _out("\n", 1, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
168 _pipefile(struct mime_handler
*mhp
, struct mimepart
const *mpp
, FILE **qbuf
,
169 char const *tmpfile
, int term_infd
)
172 char const *env_addon
[8], *cp
, *sh
;
178 if (mhp
->mh_flags
& MIME_HDL_ISQUOTE
) {
179 if ((*qbuf
= Ftmp(NULL
, "sendp", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==
181 n_perr(_("tmpfile"), 0);
186 if ((mhp
->mh_flags
& MIME_HDL_TYPE_MASK
) == MIME_HDL_PTF
) {
187 union {int (*ptf
)(void); char const *sh
;} u
;
190 if (*qbuf
!= stdout
) /* xxx never? v15: it'll be a filter anyway */
194 if((rbuf
= Popen((char*)-1, "W", u
.sh
, NULL
, fileno(*qbuf
))) == NULL
)
200 if (mpp
== NULL
|| (cp
= mpp
->m_filename
) == NULL
)
202 env_addon
[0] = str_concat_csvl(&s
, NAILENV_FILENAME
, "=", cp
, NULL
)->s
;
204 /* NAIL_FILENAME_GENERATED *//* TODO pathconf NAME_MAX; but user can create
205 * TODO a file wherever he wants! *Do* create a zero-size temporary file
206 * TODO and give *that* path as NAIL_FILENAME_TEMPORARY, clean it up once
207 * TODO the pipe returns? Like this we *can* verify path/name issues! */
208 env_addon
[1] = str_concat_csvl(&s
, NAILENV_FILENAME_GENERATED
, "=",
209 getrandstring(MIN(NAME_MAX
/ 4, 16)), NULL
)->s
;
211 /* NAIL_CONTENT{,_EVIDENCE} */
212 if (mpp
== NULL
|| (cp
= mpp
->m_ct_type_plain
) == NULL
)
214 env_addon
[2] = str_concat_csvl(&s
, NAILENV_CONTENT
, "=", cp
, NULL
)->s
;
216 if (mpp
!= NULL
&& mpp
->m_ct_type_usr_ovwr
!= NULL
)
217 cp
= mpp
->m_ct_type_usr_ovwr
;
218 env_addon
[3] = str_concat_csvl(&s
, NAILENV_CONTENT_EVIDENCE
, "=", cp
,
221 env_addon
[4] = str_concat_csvl(&s
, NAILENV_TMPDIR
, "=", tempdir
, NULL
)->s
;
222 env_addon
[5] = str_concat_csvl(&s
, "TMPDIR", "=", tempdir
, NULL
)->s
;
226 /* NAIL_FILENAME_TEMPORARY? */
227 if (tmpfile
!= NULL
) {
228 env_addon
[6] = str_concat_csvl(&s
, NAILENV_FILENAME_TEMPORARY
, "=",
233 if ((sh
= ok_vlook(SHELL
)) == NULL
)
236 if (mhp
->mh_flags
& MIME_HDL_NEEDSTERM
) {
241 pid
= run_command(sh
, NULL
, term_infd
, COMMAND_FD_PASS
, "-c",
242 mhp
->mh_shell_cmd
, NULL
, env_addon
);
243 rbuf
= (pid
< 0) ? NULL
: (FILE*)-1;
245 rbuf
= Popen(mhp
->mh_shell_cmd
, "W", sh
, env_addon
,
246 (mhp
->mh_flags
& MIME_HDL_ASYNC
? -1 : fileno(*qbuf
)));
249 n_err(_("Cannot run MIME type handler \"%s\": %s\n"),
250 mhp
->mh_msg
, strerror(errno
));
263 _out(char const *buf
, size_t len
, FILE *fp
, enum conversion convert
, enum
264 sendaction action
, struct quoteflt
*qf
, ui64_t
*stats
, struct str
*rest
)
271 Well
... it turns out to
not work like that since of course a valid
272 RFC
4155 compliant parser
, like S
-nail
, takes care
for From_ lines only
273 after an empty line has been seen
, which cannot be detected that easily
275 ifdef HAVE_DEBUG
/* TODO assert legacy */
276 /* TODO if at all, this CAN only happen for SEND_DECRYPT, since all
277 * TODO other input situations handle RFC 4155 OR, if newly generated,
278 * TODO enforce quoted-printable if there is From_, as "required" by
279 * TODO RFC 5751. The SEND_DECRYPT case is not yet overhauled;
280 * TODO if it may happen in this path, we should just treat decryption
281 * TODO as we do for the other input paths; i.e., handle it in SSL!! */
282 if (action
== SEND_MBOX
|| action
== SEND_DECRYPT
)
283 assert(!is_head(buf
, len
, TRU1
));
285 if ((/*action == SEND_MBOX ||*/ action
== SEND_DECRYPT
) &&
286 is_head(buf
, len
, TRU1
)) {
292 flags
= ((int)action
& _TD_EOF
);
294 n
= mime_write(buf
, len
, fp
,
295 action
== SEND_MBOX
? CONV_NONE
: convert
,
296 flags
| ((action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
||
297 action
== SEND_QUOTE
|| action
== SEND_QUOTE_ALL
)
299 : (action
== SEND_TOSRCH
|| action
== SEND_TOPIPE
)
300 ? TD_ICONV
: (action
== SEND_SHOW
? TD_ISPR
: TD_NONE
)),
314 _send_onpipe(int signo
)
316 NYD_X
; /* Signal handler */
318 siglongjmp(_send_pipejmp
, 1);
321 static sigjmp_buf __sendp_actjmp
; /* TODO someday.. */
322 static int __sendp_sig
; /* TODO someday.. */
323 static sighandler_type __sendp_opipe
;
325 __sendp_onsig(int sig
) /* TODO someday, we won't need it no more */
327 NYD_X
; /* Signal handler */
329 siglongjmp(__sendp_actjmp
, 1);
332 static sigjmp_buf __sndalter_actjmp
; /* TODO someday.. */
333 static int __sndalter_sig
; /* TODO someday.. */
335 __sndalter_onsig(int sig
) /* TODO someday, we won't need it no more */
337 NYD_X
; /* Signal handler */
338 __sndalter_sig
= sig
;
339 siglongjmp(__sndalter_actjmp
, 1);
343 sendpart(struct message
*zmp
, struct mimepart
*ip
, FILE * volatile obuf
,
344 struct ignoretab
*doign
, struct quoteflt
*qf
,
345 enum sendaction
volatile action
, ui64_t
* volatile stats
, int level
)
348 struct mime_handler mh
;
350 char *line
= NULL
, *cp
, *cp2
, *start
;
351 char const *tmpfile
= NULL
;
352 size_t linesize
= 0, linelen
, cnt
;
353 int volatile term_infd
;
354 int dostat
, infld
= 0, ignoring
= 1, isenc
, c
;
355 struct mimepart
*volatile np
;
356 FILE * volatile ibuf
= NULL
, * volatile pbuf
= obuf
,
357 * volatile qbuf
= obuf
, *origobuf
= obuf
;
358 enum conversion
volatile convert
;
359 sighandler_type
volatile oldpipe
= SIG_DFL
;
363 if (ip
->m_mimecontent
== MIME_PKCS7
&& ip
->m_multipart
&&
364 action
!= SEND_MBOX
&& action
!= SEND_RFC822
&& action
!= SEND_SHOW
)
370 if (!is_ign("status", 6, doign
))
372 if (!is_ign("x-status", 8, doign
))
377 if ((ibuf
= setinput(&mb
, (struct message
*)ip
, NEED_BODY
)) == NULL
) {
383 if (ip
->m_mimecontent
== MIME_DISCARD
)
386 if (!(ip
->m_flag
& MNOFROM
))
387 while (cnt
&& (c
= getc(ibuf
)) != EOF
) {
393 convert
= (action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
||
394 action
== SEND_QUOTE
|| action
== SEND_QUOTE_ALL
||
395 action
== SEND_TOSRCH
)
396 ? CONV_FROMHDR
: CONV_NONE
;
398 /* Work the headers */
399 quoteflt_reset(qf
, obuf
);
400 while (fgetline(&line
, &linesize
, &cnt
, &linelen
, ibuf
, 0)) {
402 if (line
[0] == '\n') {
403 /* If line is blank, we've reached end of headers, so force out
404 * status: field and note that we are no longer in header fields */
406 statusput(zmp
, obuf
, qf
, stats
);
408 xstatusput(zmp
, obuf
, qf
, stats
);
409 if (doign
!= allignore
)
410 _out("\n", 1, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
415 if (infld
&& blankchar(line
[0])) {
416 /* If this line is a continuation (SP / HT) of a previous header
417 * field, determine if the start of the line is a MIME encoded word */
419 for (cp
= line
; blankchar(*cp
); ++cp
);
420 if (cp
> line
&& linelen
- PTR2SIZE(cp
- line
) > 8 &&
421 cp
[0] == '=' && cp
[1] == '?')
425 /* Pick up the header field if we have one */
426 for (cp
= line
; (c
= *cp
& 0377) && c
!= ':' && !spacechar(c
); ++cp
)
429 while (spacechar(*cp
))
431 if (cp
[0] != ':' && level
== 0 && lineno
== 1) {
432 /* Not a header line, force out status: This happens in uucp style
433 * mail where there are no headers at all */
435 statusput(zmp
, obuf
, qf
, stats
);
437 xstatusput(zmp
, obuf
, qf
, stats
);
438 if (doign
!= allignore
)
439 _out("\n", 1, obuf
, CONV_NONE
,SEND_MBOX
, qf
, stats
, NULL
);
443 /* If it is an ignored field and we care about such things, skip it.
444 * Misuse dostat also for another bit xxx use a bitenum + for more */
445 if (ok_blook(keep_content_length
))
448 *cp2
= 0; /* temporarily null terminate */
449 if ((doign
&& is_ign(line
, PTR2SIZE(cp2
- line
), doign
)) ||
450 (action
== SEND_MBOX
&& !(dostat
& (1 << 2)) &&
451 (!asccasecmp(line
, "content-length") ||
452 !asccasecmp(line
, "lines"))))
454 else if (!asccasecmp(line
, "status")) {
455 /* If field is "status," go compute and print real Status: field */
457 statusput(zmp
, obuf
, qf
, stats
);
461 } else if (!asccasecmp(line
, "x-status")) {
462 /* If field is "status," go compute and print real Status: field */
464 xstatusput(zmp
, obuf
, qf
, stats
);
470 /* For colourization we need the complete line, so save it */
471 /* XXX This is all temporary (colour belongs into backend), so
472 * XXX use tmpfile as a temporary storage in the meanwhile */
474 if (colour_table
!= NULL
)
475 tmpfile
= savestrbuf(line
, PTR2SIZE(cp2
- line
));
483 /* Determine if the end of the line is a MIME encoded word */
484 /* TODO geeeh! all this lengthy stuff that follows is about is dealing
485 * TODO with header follow lines, and it should be up to the backend
486 * TODO what happens and what not, i.e., it doesn't matter wether it's
487 * TODO a MIME-encoded word or not, as long as a single separating space
488 * TODO remains in between lines (the MIME stuff will correctly remove
489 * TODO whitespace in between multiple adjacent encoded words) */
491 if (cnt
&& (c
= getc(ibuf
)) != EOF
) {
493 cp
= line
+ linelen
- 1;
494 if (linelen
> 0 && *cp
== '\n')
496 while (cp
>= line
&& whitechar(*cp
))
498 if (PTR2SIZE(cp
- line
> 8) && cp
[0] == '=' && cp
[-1] == '?')
505 size_t len
= linelen
;
507 if (action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
||
508 action
== SEND_QUOTE
|| action
== SEND_QUOTE_ALL
||
509 action
== SEND_TOSRCH
) {
510 /* Strip blank characters if two MIME-encoded words follow on
511 * continuing lines */
513 while (len
> 0 && blankchar(*start
)) {
518 if (len
> 0 && start
[len
- 1] == '\n')
520 while (len
> 0 && blankchar(start
[len
- 1]))
525 bool_t colour_stripped
= FAL0
;
526 if (tmpfile
!= NULL
) {
527 colour_put_header(obuf
, tmpfile
);
528 if (len
> 0 && start
[len
- 1] == '\n') {
529 colour_stripped
= TRU1
;
534 _out(start
, len
, obuf
, convert
, action
, qf
, stats
, NULL
);
536 if (tmpfile
!= NULL
) {
537 colour_reset(obuf
); /* XXX reset after \n!! */
556 memset(&mh
, 0, sizeof mh
);
558 switch (ip
->m_mimecontent
) {
562 case SEND_TODISP_ALL
:
565 if (ok_blook(rfc822_body_from_
)) {
566 if (qf
->qf_pfix_len
> 0) {
567 size_t i
= fwrite(qf
->qf_pfix
, sizeof *qf
->qf_pfix
,
568 qf
->qf_pfix_len
, obuf
);
569 if (i
== qf
->qf_pfix_len
&& stats
!= NULL
)
572 put_from_(obuf
, ip
->m_multipart
, stats
);
580 if (ok_blook(rfc822_body_from_
))
581 put_from_(obuf
, ip
->m_multipart
, stats
);
591 case MIME_TEXT_PLAIN
:
594 case SEND_TODISP_ALL
:
597 switch (mime_type_handler(&mh
, ip
, action
)) {
599 _out(mh
.mh_msg
.s
, mh
.mh_msg
.l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
,
601 /* We would print this as plain text, so better force going home */
612 if (action
!= SEND_DECRYPT
)
616 if (action
!= SEND_MBOX
&& action
!= SEND_RFC822
&&
617 action
!= SEND_SHOW
&& ip
->m_multipart
!= NULL
)
623 case SEND_TODISP_ALL
:
626 switch (mime_type_handler(&mh
, ip
, action
)) {
628 _out(mh
.mh_msg
.s
, mh
.mh_msg
.l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
,
630 /* We would print this as plain text, so better force going home */
633 /* FIXME WE NEED TO DO THAT IF WE ARE THE ONLY MAIL
634 * FIXME CONTENT !! */
639 if (level
== 0 && cnt
) {
640 char const *x
= _("[-- Binary content --]\n");
641 _out(x
, strlen(x
), obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
656 case MIME_ALTERNATIVE
:
657 if ((action
== SEND_TODISP
|| action
== SEND_QUOTE
) &&
658 !ok_blook(print_alternatives
)) {
659 /* XXX This (a) should not remain (b) should be own fun
660 * TODO (despite the fact that v15 will do this completely differently
661 * TODO by having an action-specific "manager" that will traverse the
662 * TODO parsed MIME tree and decide for each part wether it'll be
663 * TODO displayed or not *before* we walk the tree for doing action */
665 struct mpstack
*outer
;
667 } outermost
, * volatile curr
, * volatile mpsp
;
668 bool_t
volatile neednl
, hadpart
;
669 sighandler_type
volatile opsh
, oish
, ohsh
;
671 (curr
= &outermost
)->outer
= NULL
;
673 neednl
= hadpart
= FAL0
;
676 opsh
= safe_signal(SIGPIPE
, &__sndalter_onsig
);
677 oish
= safe_signal(SIGINT
, &__sndalter_onsig
);
678 ohsh
= safe_signal(SIGHUP
, &__sndalter_onsig
);
679 if (sigsetjmp(__sndalter_actjmp
, 1)) {
684 for (np
= ip
->m_multipart
;;) {
686 for (; np
!= NULL
; np
= np
->m_nextpart
) {
687 if (action
!= SEND_QUOTE
&& np
->m_ct_type_plain
!= NULL
) {
689 _out("\n", 1, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
690 _print_part_info(obuf
, np
, doign
, level
, qf
, stats
);
694 switch (np
->m_mimecontent
) {
695 case MIME_ALTERNATIVE
:
699 mpsp
= salloc(sizeof *mpsp
);
701 mpsp
->mp
= np
->m_multipart
;
710 switch (mime_type_handler(&mh
, np
, action
)) {
712 mh
.mh_flags
= MIME_HDL_NULL
;
713 continue; /* break; break; */
715 if (!ok_blook(mime_alternative_favour_rich
)) {/* TODO */
716 struct mimepart
*x
= np
;
718 while ((x
= x
->m_nextpart
) != NULL
) {
719 struct mime_handler mhx
;
721 if (x
->m_mimecontent
== MIME_TEXT_PLAIN
||
722 mime_type_handler(&mhx
, x
, action
) ==
727 continue; /* break; break; */
735 case MIME_TEXT_PLAIN
:
738 if (ok_blook(mime_alternative_favour_rich
)) { /* TODO */
739 struct mimepart
*x
= np
;
741 /* TODO twice TODO, we should dive into /related and
742 * TODO check wether that has rich parts! */
743 while ((x
= x
->m_nextpart
) != NULL
) {
744 struct mime_handler mhx
;
746 switch (mime_type_handler(&mhx
, x
, action
)) {
755 continue; /* break; break; */
759 if (action
== SEND_QUOTE
&& hadpart
) {
760 struct quoteflt
*dummy
= quoteflt_dummy();
761 _out("\n", 1, obuf
, CONV_NONE
, SEND_MBOX
, dummy
, stats
,
763 quoteflt_flush(dummy
);
767 rv
= sendpart(zmp
, np
, obuf
, doign
, qf
, action
, stats
,
769 quoteflt_reset(qf
, origobuf
);
773 curr
= &outermost
; /* Cause overall loop termination */
782 np
= curr
->mp
->m_nextpart
;
784 safe_signal(SIGHUP
, ohsh
);
785 safe_signal(SIGINT
, oish
);
786 safe_signal(SIGPIPE
, opsh
);
787 if (__sndalter_sig
!= 0)
788 n_raise(__sndalter_sig
);
797 case SEND_TODISP_ALL
:
805 if ((action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
) &&
806 ip
->m_multipart
!= NULL
&&
807 ip
->m_multipart
->m_mimecontent
== MIME_DISCARD
&&
808 ip
->m_multipart
->m_nextpart
== NULL
) {
809 char const *x
= _("[Missing multipart boundary - use \"show\" "
810 "to display the raw message]\n");
811 _out(x
, strlen(x
), obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
814 for (np
= ip
->m_multipart
; np
!= NULL
; np
= np
->m_nextpart
) {
817 if (np
->m_mimecontent
== MIME_DISCARD
&& action
!= SEND_DECRYPT
)
823 if (np
->m_partstring
&& !strcmp(np
->m_partstring
, "1"))
826 /* TODO Always open multipart on /dev/null, it's a hack to be
827 * TODO able to dive into that structure, and still better
828 * TODO than asking the user for something stupid.
829 * TODO oh, wait, we did ask for a filename for this MIME mail,
830 * TODO and that outer container is useless anyway ;-P */
831 if (np
->m_multipart
!= NULL
) {
832 if ((obuf
= Fopen("/dev/null", "w")) == NULL
)
834 } else if ((obuf
= newfile(np
, &ispipe
)) == NULL
)
838 if (sigsetjmp(_send_pipejmp
, 1)) {
842 oldpipe
= safe_signal(SIGPIPE
, &_send_onpipe
);
845 case SEND_TODISP_ALL
:
846 if (ip
->m_mimecontent
!= MIME_ALTERNATIVE
&&
847 ip
->m_mimecontent
!= MIME_RELATED
&&
848 ip
->m_mimecontent
!= MIME_DIGEST
&&
849 ip
->m_mimecontent
!= MIME_MULTI
)
851 _print_part_info(obuf
, np
, doign
, level
, qf
, stats
);
865 if ((action
== SEND_QUOTE
|| action
== SEND_QUOTE_ALL
) &&
866 np
->m_multipart
== NULL
&& ip
->m_parent
!= NULL
) {
867 struct quoteflt
*dummy
= quoteflt_dummy();
868 _out("\n", 1, obuf
, CONV_NONE
, SEND_MBOX
, dummy
, stats
,
870 quoteflt_flush(dummy
);
872 if (sendpart(zmp
, np
, obuf
, doign
, qf
, action
, stats
, level
+1) < 0)
874 quoteflt_reset(qf
, origobuf
);
876 if (action
== SEND_QUOTE
) {
877 if (ip
->m_mimecontent
!= MIME_RELATED
)
880 if (action
== SEND_TOFILE
&& obuf
!= origobuf
) {
885 safe_signal(SIGPIPE
, SIG_IGN
);
887 safe_signal(SIGPIPE
, oldpipe
);
900 /* Copy out message body */
901 if (doign
== allignore
&& level
== 0) /* skip final blank line */
903 switch (ip
->m_mime_enc
) {
910 convert
= CONV_FROMQP
;
913 switch (ip
->m_mimecontent
) {
915 case MIME_TEXT_PLAIN
:
917 convert
= CONV_FROMB64_T
;
920 switch (mh
.mh_flags
& MIME_HDL_TYPE_MASK
) {
923 convert
= CONV_FROMB64_T
;
926 convert
= CONV_FROMB64
;
936 if (action
== SEND_DECRYPT
|| action
== SEND_MBOX
||
937 action
== SEND_RFC822
|| action
== SEND_SHOW
)
940 if ((action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
||
941 action
== SEND_QUOTE
|| action
== SEND_QUOTE_ALL
||
942 action
== SEND_TOSRCH
) &&
943 (ip
->m_mimecontent
== MIME_TEXT_PLAIN
||
944 ip
->m_mimecontent
== MIME_TEXT_HTML
||
945 ip
->m_mimecontent
== MIME_TEXT
||
946 (mh
.mh_flags
& MIME_HDL_TYPE_MASK
) == MIME_HDL_TEXT
||
947 (mh
.mh_flags
& MIME_HDL_TYPE_MASK
) == MIME_HDL_PTF
)) {
948 char const *tcs
= charset_get_lc();
950 if (iconvd
!= (iconv_t
)-1)
951 n_iconv_close(iconvd
);
952 /* TODO Since Base64 has an odd 4:3 relation in between input
953 * TODO and output an input line may end with a partial
954 * TODO multibyte character; this is no problem at all unless
955 * TODO we send to the display or whatever, i.e., ensure
956 * TODO makeprint() or something; to avoid this trap, *force*
957 * TODO iconv(), in which case this layer will handle leftovers
959 if (convert
== CONV_FROMB64_T
|| (asccasecmp(tcs
, ip
->m_charset
) &&
960 asccasecmp(charset_get_7bit(), ip
->m_charset
))) {
961 iconvd
= n_iconv_open(tcs
, ip
->m_charset
);
963 * TODO errors should DEFINETELY not be scrolled away!
964 * TODO what about an error buffer (think old shsp(1)),
965 * TODO re-dump errors since last snapshot when the
966 * TODO command loop enters again? i.e., at least print
967 * TODO "There were errors ?" before the next prompt,
968 * TODO so that the user can look at the error buffer?
970 if (iconvd
== (iconv_t
)-1 && errno
== EINVAL
) {
971 n_err(_("Cannot convert from %s to %s\n"), ip
->m_charset
, tcs
);
972 /*rv = 1; goto jleave;*/
978 switch (mh
.mh_flags
& MIME_HDL_TYPE_MASK
) {
984 term_infd
= COMMAND_FD_PASS
;
985 if (mh
.mh_flags
& (MIME_HDL_TMPF
| MIME_HDL_NEEDSTERM
)) {
988 of
= OF_RDWR
| OF_REGISTER
;
989 if (!(mh
.mh_flags
& MIME_HDL_TMPF
)) {
991 mh
.mh_flags
|= MIME_HDL_TMPF_FILL
;
993 } else if (mh
.mh_flags
& MIME_HDL_TMPF_UNLINK
)
994 of
|= OF_REGISTER_UNLINK
;
996 if ((pbuf
= Ftmp((mh
.mh_flags
& MIME_HDL_TMPF
? &cp
: NULL
),
997 (mh
.mh_flags
& MIME_HDL_TMPF_FILL
? "mimehdlfill" : "mimehdl"),
1001 if (mh
.mh_flags
& MIME_HDL_TMPF
) {
1002 tmpfile
= savestr(cp
);
1006 if (mh
.mh_flags
& MIME_HDL_TMPF_FILL
) {
1008 term_infd
= fileno(pbuf
);
1014 pbuf
= _pipefile(&mh
, ip
, UNVOLATILE(&qbuf
), tmpfile
, term_infd
);
1020 } else if ((mh
.mh_flags
& MIME_HDL_NEEDSTERM
) && pbuf
== (FILE*)-1) {
1025 action
= SEND_TOPIPE
;
1027 oldpipe
= safe_signal(SIGPIPE
, &_send_onpipe
);
1028 if (sigsetjmp(_send_pipejmp
, 1))
1034 mh
.mh_flags
= MIME_HDL_NULL
;
1041 bool_t
volatile eof
;
1042 ui32_t save_qf_pfix_len
= qf
->qf_pfix_len
;
1043 ui64_t
*save_stats
= stats
;
1045 if (pbuf
!= origobuf
) {
1046 qf
->qf_pfix_len
= 0; /* XXX legacy (remove filter instead) */
1055 __sendp_opipe
= safe_signal(SIGPIPE
, &__sendp_onsig
);
1056 if (sigsetjmp(__sendp_actjmp
, 1)) {
1061 if (iconvd
!= (iconv_t
)-1)
1062 n_iconv_close(iconvd
);
1064 safe_signal(SIGPIPE
, __sendp_opipe
);
1065 n_raise(__sendp_sig
);
1069 quoteflt_reset(qf
, pbuf
);
1070 while (!eof
&& fgetline(&line
, &linesize
, &cnt
, &linelen
, ibuf
, 0)) {
1072 if (_out(line
, linelen
, pbuf
, convert
, action
, qf
, stats
, &rest
) < 0 ||
1074 rv
= -1; /* XXX Should bail away?! */
1078 if (!eof
&& rest
.l
!= 0) {
1086 if (mh
.mh_flags
& MIME_HDL_TMPF_FILL
) {
1087 mh
.mh_flags
&= ~MIME_HDL_TMPF_FILL
;
1089 really_rewind(pbuf
);
1090 /* Don't Fclose() the Ftmp() thing due to OF_REGISTER_UNLINK++ */
1091 goto jpipe_for_real
;
1095 safe_signal(SIGPIPE
, __sendp_opipe
);
1100 if (pbuf
!= origobuf
) {
1101 qf
->qf_pfix_len
= save_qf_pfix_len
;
1110 safe_signal(SIGPIPE
, SIG_IGN
);
1111 Pclose(pbuf
, !(mh
.mh_flags
& MIME_HDL_ASYNC
));
1112 safe_signal(SIGPIPE
, oldpipe
);
1113 if (qbuf
!= NULL
&& qbuf
!= obuf
)
1114 pipecpy(qbuf
, obuf
, origobuf
, qf
, stats
);
1117 if (iconvd
!= (iconv_t
)-1)
1118 n_iconv_close(iconvd
);
1126 newfile(struct mimepart
*ip
, bool_t
*ispipe
)
1136 if (f
!= NULL
&& f
!= (char*)-1) {
1139 makeprint(&in
, &out
);
1140 out
.l
= delctrl(out
.s
, out
.l
);
1141 f
= savestrbuf(out
.s
, out
.l
);
1145 if (options
& OPT_INTERACTIVE
) {
1149 /* TODO Generic function which asks for filename.
1150 * TODO If the current part is the first textpart the target
1151 * TODO is implicit from outer `write' etc! */
1152 /* I18N: Filename input prompt with file type indication */
1153 str_concat_csvl(&prompt
, _("Enter filename for part "),
1154 (ip
->m_partstring
!= NULL
) ? ip
->m_partstring
: _("?"),
1155 _(" ("), ip
->m_ct_type_plain
, _("): "), NULL
);
1157 f2
= n_input_cp_addhist(prompt
.s
, ((f
!= (char*)-1 && f
!= NULL
)
1158 ? fexpand_nshell_quote(f
) : NULL
), TRU1
);
1159 if (f2
== NULL
|| *f2
== '\0') {
1160 if (options
& OPT_D_V
)
1161 n_err(_("... skipping this\n"));
1164 } else if (*f2
== '|')
1165 /* Pipes are expanded by the shell */
1167 else if ((f3
= fexpand(f2
, FEXP_LOCAL
| FEXP_NSHELL
)) == NULL
)
1168 /* (Error message written by fexpand()) */
1173 if (f
== NULL
|| f
== (char*)-1) {
1180 cp
= ok_vlook(SHELL
);
1183 fp
= Popen(f
+ 1, "w", cp
, NULL
, 1);
1184 if (!(*ispipe
= (fp
!= NULL
)))
1187 if ((fp
= Fopen(f
, "w")) == NULL
)
1188 n_err(_("Cannot open \"%s\"\n"), f
);
1196 pipecpy(FILE *pipebuf
, FILE *outbuf
, FILE *origobuf
, struct quoteflt
*qf
,
1199 char *line
= NULL
; /* TODO line pool */
1200 size_t linesize
= 0, linelen
, cnt
;
1206 cnt
= fsize(pipebuf
);
1209 quoteflt_reset(qf
, outbuf
);
1210 while (fgetline(&line
, &linesize
, &cnt
, &linelen
, pipebuf
, 0) != NULL
) {
1211 if ((sz
= quoteflt_push(qf
, line
, linelen
)) < 0)
1215 if ((sz
= quoteflt_flush(qf
)) > 0)
1220 if (all_sz
> 0 && outbuf
== origobuf
&& stats
!= NULL
)
1227 statusput(const struct message
*mp
, FILE *obuf
, struct quoteflt
*qf
,
1230 char statout
[3], *cp
= statout
;
1233 if (mp
->m_flag
& MREAD
)
1235 if (!(mp
->m_flag
& MNEW
))
1239 int i
= fprintf(obuf
, "%.*sStatus: %s\n", (int)qf
->qf_pfix_len
,
1240 (qf
->qf_pfix_len
> 0 ? qf
->qf_pfix
: 0), statout
);
1241 if (i
> 0 && stats
!= NULL
)
1248 xstatusput(const struct message
*mp
, FILE *obuf
, struct quoteflt
*qf
,
1252 char *xp
= xstatout
;
1255 if (mp
->m_flag
& MFLAGGED
)
1257 if (mp
->m_flag
& MANSWERED
)
1259 if (mp
->m_flag
& MDRAFTED
)
1263 int i
= fprintf(obuf
, "%.*sX-Status: %s\n", (int)qf
->qf_pfix_len
,
1264 (qf
->qf_pfix_len
> 0 ? qf
->qf_pfix
: 0), xstatout
);
1265 if (i
> 0 && stats
!= NULL
)
1272 put_from_(FILE *fp
, struct mimepart
*ip
, ui64_t
*stats
)
1274 char const *froma
, *date
, *nl
;
1278 if (ip
!= NULL
&& ip
->m_from
!= NULL
) {
1280 date
= fakedate(ip
->m_time
);
1284 date
= time_current
.tc_ctime
;
1288 colour_put(fp
, COLOURSPEC_FROM_
);
1289 i
= fprintf(fp
, "From %s %s%s", froma
, date
, nl
);
1291 if (i
> 0 && stats
!= NULL
)
1297 sendmp(struct message
*mp
, FILE *obuf
, struct ignoretab
*doign
,
1298 char const *prefix
, enum sendaction action
, ui64_t
*stats
)
1303 enum mime_parse_flags mpf
;
1304 struct mimepart
*ip
;
1308 if (mp
== dot
&& action
!= SEND_TOSRCH
)
1309 pstate
|= PS_DID_PRINT_DOT
;
1312 quoteflt_init(&qf
, prefix
);
1314 /* First line is the From_ line, so no headers there to worry about */
1315 if ((ibuf
= setinput(&mb
, mp
, NEED_BODY
)) == NULL
)
1321 struct str
const *cpre
, *csuf
;
1323 cpre
= colour_get(COLOURSPEC_FROM_
);
1324 csuf
= colour_get(COLOURSPEC_RESET
);
1328 if (mp
->m_flag
& MNOFROM
) {
1329 if (doign
!= allignore
&& doign
!= fwdignore
&& action
!= SEND_RFC822
)
1330 sz
= fprintf(obuf
, "%s%.*sFrom %s %s%s\n",
1331 (cpre
!= NULL
? cpre
->s
: ""),
1332 (int)qf
.qf_pfix_len
, (qf
.qf_pfix_len
!= 0 ? qf
.qf_pfix
: ""),
1333 fakefrom(mp
), fakedate(mp
->m_time
),
1334 (csuf
!= NULL
? csuf
->s
: ""));
1335 } else if (doign
!= allignore
&& doign
!= fwdignore
&&
1336 action
!= SEND_RFC822
) {
1337 if (qf
.qf_pfix_len
> 0) {
1338 i
= fwrite(qf
.qf_pfix
, sizeof *qf
.qf_pfix
, qf
.qf_pfix_len
, obuf
);
1339 if (i
!= qf
.qf_pfix_len
)
1345 fputs(cpre
->s
, obuf
);
1346 cpre
= (struct str
const*)0x1;
1350 while (cnt
> 0 && (c
= getc(ibuf
)) != EOF
) {
1352 if (c
== '\n' && csuf
!= NULL
) {
1353 cpre
= (struct str
const*)0x1;
1354 fputs(csuf
->s
, obuf
);
1365 if (csuf
!= NULL
&& cpre
!= (struct str
const*)0x1)
1366 fputs(csuf
->s
, obuf
);
1369 while (cnt
> 0 && (c
= getc(ibuf
)) != EOF
) {
1376 if (sz
> 0 && stats
!= NULL
)
1379 mpf
= MIME_PARSE_NONE
;
1380 if (action
!= SEND_MBOX
&& action
!= SEND_RFC822
&& action
!= SEND_SHOW
)
1381 mpf
|= MIME_PARSE_DECRYPT
| MIME_PARSE_PARTS
;
1382 if ((ip
= mime_parse_msg(mp
, mpf
)) == NULL
)
1385 rv
= sendpart(mp
, ip
, obuf
, doign
, &qf
, action
, stats
, 0);
1387 quoteflt_destroy(&qf
);