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
;
719 case MIME_TEXT_PLAIN
:
723 if (action
== SEND_QUOTE
&& hadpart
) {
724 struct quoteflt
*dummy
= quoteflt_dummy();
725 _out("\n", 1, obuf
, CONV_NONE
, SEND_MBOX
, dummy
, stats
,
727 quoteflt_flush(dummy
);
731 rv
= sendpart(zmp
, np
, obuf
, doign
, qf
, action
, stats
,
733 quoteflt_reset(qf
, origobuf
);
737 curr
= &outermost
; /* Cause overall loop termination */
746 np
= curr
->mp
->m_nextpart
;
748 safe_signal(SIGHUP
, ohsh
);
749 safe_signal(SIGINT
, oish
);
750 safe_signal(SIGPIPE
, opsh
);
751 if (__sndalter_sig
!= 0)
752 n_raise(__sndalter_sig
);
761 case SEND_TODISP_ALL
:
769 if ((action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
) &&
770 ip
->m_multipart
!= NULL
&&
771 ip
->m_multipart
->m_mimecontent
== MIME_DISCARD
&&
772 ip
->m_multipart
->m_nextpart
== NULL
) {
773 char const *x
= _("[Missing multipart boundary - use \"show\" "
774 "to display the raw message]\n");
775 _out(x
, strlen(x
), obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
);
778 for (np
= ip
->m_multipart
; np
!= NULL
; np
= np
->m_nextpart
) {
781 if (np
->m_mimecontent
== MIME_DISCARD
&& action
!= SEND_DECRYPT
)
787 if (np
->m_partstring
&& !strcmp(np
->m_partstring
, "1"))
790 /* TODO Always open multipart on /dev/null, it's a hack to be
791 * TODO able to dive into that structure, and still better
792 * TODO than asking the user for something stupid.
793 * TODO oh, wait, we did ask for a filename for this MIME mail,
794 * TODO and that outer container is useless anyway ;-P */
795 if (np
->m_multipart
!= NULL
) {
796 if ((obuf
= Fopen("/dev/null", "w")) == NULL
)
798 } else if ((obuf
= newfile(np
, &ispipe
)) == NULL
)
802 if (sigsetjmp(_send_pipejmp
, 1)) {
806 oldpipe
= safe_signal(SIGPIPE
, &_send_onpipe
);
809 case SEND_TODISP_ALL
:
811 if (ip
->m_mimecontent
!= MIME_MULTI
&&
812 ip
->m_mimecontent
!= MIME_ALTERNATIVE
&&
813 ip
->m_mimecontent
!= MIME_RELATED
&&
814 ip
->m_mimecontent
!= MIME_DIGEST
)
816 _print_part_info(obuf
, np
, doign
, level
, qf
, stats
);
829 if (sendpart(zmp
, np
, obuf
, doign
, qf
, action
, stats
, level
+1) < 0)
831 quoteflt_reset(qf
, origobuf
);
833 if (action
== SEND_QUOTE
)
835 if (action
== SEND_TOFILE
&& obuf
!= origobuf
) {
840 safe_signal(SIGPIPE
, SIG_IGN
);
842 safe_signal(SIGPIPE
, oldpipe
);
854 /* Copy out message body */
855 if (doign
== allignore
&& level
== 0) /* skip final blank line */
857 switch (ip
->m_mime_enc
) {
864 convert
= CONV_FROMQP
;
867 switch (ip
->m_mimecontent
) {
869 case MIME_TEXT_PLAIN
:
871 convert
= CONV_FROMB64_T
;
874 switch (mh
.mh_flags
& MIME_HDL_TYPE_MASK
) {
877 convert
= CONV_FROMB64_T
;
880 convert
= CONV_FROMB64
;
890 if (action
== SEND_DECRYPT
|| action
== SEND_MBOX
||
891 action
== SEND_RFC822
|| action
== SEND_SHOW
)
894 if ((action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
||
895 action
== SEND_QUOTE
|| action
== SEND_QUOTE_ALL
||
896 action
== SEND_TOSRCH
) &&
897 (ip
->m_mimecontent
== MIME_TEXT_PLAIN
||
898 ip
->m_mimecontent
== MIME_TEXT_HTML
||
899 ip
->m_mimecontent
== MIME_TEXT
||
900 (mh
.mh_flags
& MIME_HDL_TYPE_MASK
) == MIME_HDL_TEXT
||
901 (mh
.mh_flags
& MIME_HDL_TYPE_MASK
) == MIME_HDL_PTF
)) {
902 char const *tcs
= charset_get_lc();
904 if (iconvd
!= (iconv_t
)-1)
905 n_iconv_close(iconvd
);
906 /* TODO Since Base64 has an odd 4:3 relation in between input
907 * TODO and output an input line may end with a partial
908 * TODO multibyte character; this is no problem at all unless
909 * TODO we send to the display or whatever, i.e., ensure
910 * TODO makeprint() or something; to avoid this trap, *force*
911 * TODO iconv(), in which case this layer will handle leftovers
913 if (convert
== CONV_FROMB64_T
|| (asccasecmp(tcs
, ip
->m_charset
) &&
914 asccasecmp(charset_get_7bit(), ip
->m_charset
))) {
915 iconvd
= n_iconv_open(tcs
, ip
->m_charset
);
917 * TODO errors should DEFINETELY not be scrolled away!
918 * TODO what about an error buffer (think old shsp(1)),
919 * TODO re-dump errors since last snapshot when the
920 * TODO command loop enters again? i.e., at least print
921 * TODO "There were errors ?" before the next prompt,
922 * TODO so that the user can look at the error buffer?
924 if (iconvd
== (iconv_t
)-1 && errno
== EINVAL
) {
925 n_err(_("Cannot convert from %s to %s\n"), ip
->m_charset
, tcs
);
926 /*rv = 1; goto jleave;*/
932 switch (mh
.mh_flags
& MIME_HDL_TYPE_MASK
) {
938 term_infd
= COMMAND_FD_PASS
;
939 if (mh
.mh_flags
& (MIME_HDL_TMPF
| MIME_HDL_NEEDSTERM
)) {
942 of
= OF_RDWR
| OF_REGISTER
;
943 if (!(mh
.mh_flags
& MIME_HDL_TMPF
)) {
945 mh
.mh_flags
|= MIME_HDL_TMPF_FILL
;
947 } else if (mh
.mh_flags
& MIME_HDL_TMPF_UNLINK
)
948 of
|= OF_REGISTER_UNLINK
;
950 if ((pbuf
= Ftmp((mh
.mh_flags
& MIME_HDL_TMPF
? &cp
: NULL
),
951 (mh
.mh_flags
& MIME_HDL_TMPF_FILL
? "mimehdlfill" : "mimehdl"),
955 if (mh
.mh_flags
& MIME_HDL_TMPF
) {
956 tmpfile
= savestr(cp
);
960 if (mh
.mh_flags
& MIME_HDL_TMPF_FILL
) {
962 term_infd
= fileno(pbuf
);
968 pbuf
= _pipefile(&mh
, ip
, UNVOLATILE(&qbuf
), tmpfile
, term_infd
);
974 } else if ((mh
.mh_flags
& MIME_HDL_NEEDSTERM
) && pbuf
== (FILE*)-1) {
979 action
= SEND_TOPIPE
;
981 oldpipe
= safe_signal(SIGPIPE
, &_send_onpipe
);
982 if (sigsetjmp(_send_pipejmp
, 1))
988 mh
.mh_flags
= MIME_HDL_NULL
;
996 ui32_t save_qf_pfix_len
= qf
->qf_pfix_len
;
997 ui64_t
*save_stats
= stats
;
999 if (pbuf
!= origobuf
) {
1000 qf
->qf_pfix_len
= 0; /* XXX legacy (remove filter instead) */
1009 __sendp_opipe
= safe_signal(SIGPIPE
, &__sendp_onsig
);
1010 if (sigsetjmp(__sendp_actjmp
, 1)) {
1015 if (iconvd
!= (iconv_t
)-1)
1016 n_iconv_close(iconvd
);
1018 safe_signal(SIGPIPE
, __sendp_opipe
);
1019 n_raise(__sendp_sig
);
1023 quoteflt_reset(qf
, pbuf
);
1024 while (!eof
&& fgetline(&line
, &linesize
, &cnt
, &linelen
, ibuf
, 0)) {
1026 if (_out(line
, linelen
, pbuf
, convert
, action
, qf
, stats
, &rest
) < 0 ||
1028 rv
= -1; /* XXX Should bail away?! */
1032 if (!eof
&& rest
.l
!= 0) {
1040 if (mh
.mh_flags
& MIME_HDL_TMPF_FILL
) {
1041 mh
.mh_flags
&= ~MIME_HDL_TMPF_FILL
;
1043 really_rewind(pbuf
);
1044 /* Don't Fclose() the Ftmp() thing due to OF_REGISTER_UNLINK++ */
1045 goto jpipe_for_real
;
1049 safe_signal(SIGPIPE
, __sendp_opipe
);
1054 if (pbuf
!= origobuf
) {
1055 qf
->qf_pfix_len
= save_qf_pfix_len
;
1064 safe_signal(SIGPIPE
, SIG_IGN
);
1065 Pclose(pbuf
, !(mh
.mh_flags
& MIME_HDL_ASYNC
));
1066 safe_signal(SIGPIPE
, oldpipe
);
1067 if (qbuf
!= NULL
&& qbuf
!= obuf
)
1068 pipecpy(qbuf
, obuf
, origobuf
, qf
, stats
);
1071 if (iconvd
!= (iconv_t
)-1)
1072 n_iconv_close(iconvd
);
1080 newfile(struct mimepart
*ip
, bool_t
*ispipe
)
1090 if (f
!= NULL
&& f
!= (char*)-1) {
1093 makeprint(&in
, &out
);
1094 out
.l
= delctrl(out
.s
, out
.l
);
1095 f
= savestrbuf(out
.s
, out
.l
);
1099 if (options
& OPT_INTERACTIVE
) {
1103 /* TODO Generic function which asks for filename.
1104 * TODO If the current part is the first textpart the target
1105 * TODO is implicit from outer `write' etc! */
1106 /* I18N: Filename input prompt with file type indication */
1107 str_concat_csvl(&prompt
, _("Enter filename for part "),
1108 (ip
->m_partstring
!= NULL
) ? ip
->m_partstring
: _("?"),
1109 _(" ("), ip
->m_ct_type_plain
, _("): "), NULL
);
1111 f2
= n_input_cp_addhist(prompt
.s
, ((f
!= (char*)-1 && f
!= NULL
)
1112 ? fexpand_nshell_quote(f
) : NULL
), TRU1
);
1113 if (f2
== NULL
|| *f2
== '\0') {
1114 if (options
& OPT_D_V
)
1115 n_err(_("... skipping this\n"));
1118 } else if (*f2
== '|')
1119 /* Pipes are expanded by the shell */
1121 else if ((f3
= fexpand(f2
, FEXP_LOCAL
| FEXP_NSHELL
)) == NULL
)
1122 /* (Error message written by fexpand()) */
1127 if (f
== NULL
|| f
== (char*)-1) {
1134 cp
= ok_vlook(SHELL
);
1137 fp
= Popen(f
+ 1, "w", cp
, NULL
, 1);
1138 if (!(*ispipe
= (fp
!= NULL
)))
1141 if ((fp
= Fopen(f
, "w")) == NULL
)
1142 n_err(_("Cannot open \"%s\"\n"), f
);
1150 pipecpy(FILE *pipebuf
, FILE *outbuf
, FILE *origobuf
, struct quoteflt
*qf
,
1153 char *line
= NULL
; /* TODO line pool */
1154 size_t linesize
= 0, linelen
, cnt
;
1160 cnt
= fsize(pipebuf
);
1163 quoteflt_reset(qf
, outbuf
);
1164 while (fgetline(&line
, &linesize
, &cnt
, &linelen
, pipebuf
, 0) != NULL
) {
1165 if ((sz
= quoteflt_push(qf
, line
, linelen
)) < 0)
1169 if ((sz
= quoteflt_flush(qf
)) > 0)
1174 if (all_sz
> 0 && outbuf
== origobuf
&& stats
!= NULL
)
1181 statusput(const struct message
*mp
, FILE *obuf
, struct quoteflt
*qf
,
1184 char statout
[3], *cp
= statout
;
1187 if (mp
->m_flag
& MREAD
)
1189 if (!(mp
->m_flag
& MNEW
))
1193 int i
= fprintf(obuf
, "%.*sStatus: %s\n", (int)qf
->qf_pfix_len
,
1194 (qf
->qf_pfix_len
> 0 ? qf
->qf_pfix
: 0), statout
);
1195 if (i
> 0 && stats
!= NULL
)
1202 xstatusput(const struct message
*mp
, FILE *obuf
, struct quoteflt
*qf
,
1206 char *xp
= xstatout
;
1209 if (mp
->m_flag
& MFLAGGED
)
1211 if (mp
->m_flag
& MANSWERED
)
1213 if (mp
->m_flag
& MDRAFTED
)
1217 int i
= fprintf(obuf
, "%.*sX-Status: %s\n", (int)qf
->qf_pfix_len
,
1218 (qf
->qf_pfix_len
> 0 ? qf
->qf_pfix
: 0), xstatout
);
1219 if (i
> 0 && stats
!= NULL
)
1226 put_from_(FILE *fp
, struct mimepart
*ip
, ui64_t
*stats
)
1228 char const *froma
, *date
, *nl
;
1232 if (ip
!= NULL
&& ip
->m_from
!= NULL
) {
1234 date
= fakedate(ip
->m_time
);
1238 date
= time_current
.tc_ctime
;
1242 colour_put(fp
, COLOURSPEC_FROM_
);
1243 i
= fprintf(fp
, "From %s %s%s", froma
, date
, nl
);
1245 if (i
> 0 && stats
!= NULL
)
1251 sendmp(struct message
*mp
, FILE *obuf
, struct ignoretab
*doign
,
1252 char const *prefix
, enum sendaction action
, ui64_t
*stats
)
1257 enum mime_parse_flags mpf
;
1258 struct mimepart
*ip
;
1262 if (mp
== dot
&& action
!= SEND_TOSRCH
)
1263 pstate
|= PS_DID_PRINT_DOT
;
1266 quoteflt_init(&qf
, prefix
);
1268 /* First line is the From_ line, so no headers there to worry about */
1269 if ((ibuf
= setinput(&mb
, mp
, NEED_BODY
)) == NULL
)
1275 struct str
const *cpre
, *csuf
;
1277 cpre
= colour_get(COLOURSPEC_FROM_
);
1278 csuf
= colour_get(COLOURSPEC_RESET
);
1282 if (mp
->m_flag
& MNOFROM
) {
1283 if (doign
!= allignore
&& doign
!= fwdignore
&& action
!= SEND_RFC822
)
1284 sz
= fprintf(obuf
, "%s%.*sFrom %s %s%s\n",
1285 (cpre
!= NULL
? cpre
->s
: ""),
1286 (int)qf
.qf_pfix_len
, (qf
.qf_pfix_len
!= 0 ? qf
.qf_pfix
: ""),
1287 fakefrom(mp
), fakedate(mp
->m_time
),
1288 (csuf
!= NULL
? csuf
->s
: ""));
1289 } else if (doign
!= allignore
&& doign
!= fwdignore
&&
1290 action
!= SEND_RFC822
) {
1291 if (qf
.qf_pfix_len
> 0) {
1292 i
= fwrite(qf
.qf_pfix
, sizeof *qf
.qf_pfix
, qf
.qf_pfix_len
, obuf
);
1293 if (i
!= qf
.qf_pfix_len
)
1299 fputs(cpre
->s
, obuf
);
1300 cpre
= (struct str
const*)0x1;
1304 while (cnt
> 0 && (c
= getc(ibuf
)) != EOF
) {
1306 if (c
== '\n' && csuf
!= NULL
) {
1307 cpre
= (struct str
const*)0x1;
1308 fputs(csuf
->s
, obuf
);
1319 if (csuf
!= NULL
&& cpre
!= (struct str
const*)0x1)
1320 fputs(csuf
->s
, obuf
);
1323 while (cnt
> 0 && (c
= getc(ibuf
)) != EOF
) {
1330 if (sz
> 0 && stats
!= NULL
)
1333 mpf
= MIME_PARSE_NONE
;
1334 if (action
!= SEND_MBOX
&& action
!= SEND_RFC822
&& action
!= SEND_SHOW
)
1335 mpf
|= MIME_PARSE_DECRYPT
| MIME_PARSE_PARTS
;
1336 if ((ip
= mime_parse_msg(mp
, mpf
)) == NULL
)
1339 rv
= sendpart(mp
, ip
, obuf
, doign
, &qf
, action
, stats
, 0);
1341 quoteflt_destroy(&qf
);