1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Message content preparation (sendmp()).
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2016 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
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 *tmpname
, 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
*outrest
,
62 static void _send_onpipe(int signo
);
65 static int sendpart(struct message
*zmp
, struct mimepart
*ip
,
66 FILE *obuf
, struct ignoretab
*doign
,
67 struct quoteflt
*qf
, enum sendaction action
,
68 ui64_t
*stats
, int level
);
70 /* Get a file for an attachment */
71 static FILE * newfile(struct mimepart
*ip
, bool_t
volatile *ispipe
);
73 static void pipecpy(FILE *pipebuf
, FILE *outbuf
, FILE *origobuf
,
74 struct quoteflt
*qf
, ui64_t
*stats
);
76 /* Output a reasonable looking status field */
77 static void statusput(const struct message
*mp
, FILE *obuf
,
78 struct quoteflt
*qf
, ui64_t
*stats
);
79 static void xstatusput(const struct message
*mp
, FILE *obuf
,
80 struct quoteflt
*qf
, ui64_t
*stats
);
82 static void put_from_(FILE *fp
, struct mimepart
*ip
, ui64_t
*stats
);
85 _print_part_info(FILE *obuf
, struct mimepart
const *mpp
, /* TODO strtofmt.. */
86 struct ignoretab
*doign
, int level
, struct quoteflt
*qf
, ui64_t
*stats
)
89 struct str ti
= {NULL
, 0}, to
;
90 struct str
const *cpre
, *csuf
;
96 struct n_colour_pen
*cpen
= n_colour_pen_create(n_COLOUR_ID_VIEW_PARTINFO
,
98 if ((cpre
= n_colour_pen_to_str(cpen
)) != NULL
)
99 csuf
= n_colour_reset_to_str();
107 /* Take care of "99.99", i.e., 5 */
108 if ((cp
= mpp
->m_partstring
) == NULL
|| cp
[0] == '\0')
110 if (level
|| (cp
[0] != '1' && cp
[1] == '\0'))
111 _out("\n", 1, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
, NULL
);
113 _out(cpre
->s
, cpre
->l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
,NULL
);
114 _out("[-- #", 5, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
,NULL
);
115 _out(cp
, strlen(cp
), obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
,NULL
);
117 to
.l
= snprintf(buf
, sizeof buf
, " %" PRIuZ
"/%" PRIuZ
" ",
118 (uiz_t
)mpp
->m_lines
, (uiz_t
)mpp
->m_size
);
119 _out(buf
, to
.l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
,NULL
);
121 if ((cp
= mpp
->m_ct_type_usr_ovwr
) != NULL
)
122 _out("+", 1, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
,NULL
);
124 cp
= mpp
->m_ct_type_plain
;
125 if ((to
.l
= strlen(cp
)) > 30 && is_asccaseprefix(cp
, "application/")) {
126 size_t const al
= sizeof("appl../") -1, fl
= sizeof("application/") -1;
127 size_t i
= to
.l
- fl
;
128 char *x
= salloc(al
+ i
+1);
130 memcpy(x
, "appl../", al
);
131 memcpy(x
+ al
, cp
+ fl
, i
+1);
135 _out(cp
, to
.l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
,NULL
);
137 if (mpp
->m_multipart
== NULL
/* TODO */ && (cp
= mpp
->m_ct_enc
) != NULL
) {
138 _out(", ", 2, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
,NULL
);
139 if (to
.l
> 25 && !asccasecmp(cp
, "quoted-printable"))
141 _out(cp
, strlen(cp
), obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
,NULL
);
144 if (mpp
->m_multipart
== NULL
/* TODO */ && (cp
= mpp
->m_charset
) != NULL
) {
145 _out(", ", 2, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
,NULL
);
146 _out(cp
, strlen(cp
), obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
,NULL
);
149 _out(" --]", 4, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
,NULL
);
151 _out(csuf
->s
, csuf
->l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
,NULL
);
152 _out("\n", 1, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
,NULL
);
154 if (is_ign("content-disposition", 19, doign
) && mpp
->m_filename
!= NULL
&&
155 *mpp
->m_filename
!= '\0') {
156 makeprint(n_str_add_cp(&ti
, mpp
->m_filename
), &to
);
158 to
.l
= delctrl(to
.s
, to
.l
);
161 _out(cpre
->s
, cpre
->l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
,
163 _out("[-- ", 4, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
, NULL
);
164 _out(to
.s
, to
.l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
, NULL
);
165 _out(" --]", 4, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
, NULL
);
167 _out(csuf
->s
, csuf
->l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
,
169 _out("\n", 1, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
, NULL
);
177 _pipefile(struct mime_handler
*mhp
, struct mimepart
const *mpp
, FILE **qbuf
,
178 char const *tmpname
, int term_infd
)
181 char const *env_addon
[8], *cp
, *sh
;
187 if (mhp
->mh_flags
& MIME_HDL_ISQUOTE
) {
188 if ((*qbuf
= Ftmp(NULL
, "sendp", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==
190 n_perr(_("tmpfile"), 0);
195 if ((mhp
->mh_flags
& MIME_HDL_TYPE_MASK
) == MIME_HDL_PTF
) {
196 union {int (*ptf
)(void); char const *sh
;} u
;
199 if (*qbuf
!= stdout
) /* xxx never? v15: it'll be a filter anyway */
203 if((rbuf
= Popen((char*)-1, "W", u
.sh
, NULL
, fileno(*qbuf
))) == NULL
)
209 if (mpp
== NULL
|| (cp
= mpp
->m_filename
) == NULL
)
211 env_addon
[0] = str_concat_csvl(&s
, NAILENV_FILENAME
, "=", cp
, NULL
)->s
;
213 /* NAIL_FILENAME_GENERATED *//* TODO pathconf NAME_MAX; but user can create
214 * TODO a file wherever he wants! *Do* create a zero-size temporary file
215 * TODO and give *that* path as NAIL_FILENAME_TEMPORARY, clean it up once
216 * TODO the pipe returns? Like this we *can* verify path/name issues! */
217 env_addon
[1] = str_concat_csvl(&s
, NAILENV_FILENAME_GENERATED
, "=",
218 getrandstring(n_MIN(NAME_MAX
/ 4, 16)), NULL
)->s
;
220 /* NAIL_CONTENT{,_EVIDENCE} */
221 if (mpp
== NULL
|| (cp
= mpp
->m_ct_type_plain
) == NULL
)
223 env_addon
[2] = str_concat_csvl(&s
, NAILENV_CONTENT
, "=", cp
, NULL
)->s
;
225 if (mpp
!= NULL
&& mpp
->m_ct_type_usr_ovwr
!= NULL
)
226 cp
= mpp
->m_ct_type_usr_ovwr
;
227 env_addon
[3] = str_concat_csvl(&s
, NAILENV_CONTENT_EVIDENCE
, "=", cp
,
230 cp
= ok_vlook(TMPDIR
);
231 env_addon
[4] = str_concat_csvl(&s
, NAILENV_TMPDIR
, "=", cp
, NULL
)->s
;
232 env_addon
[5] = str_concat_csvl(&s
, "TMPDIR", "=", cp
, NULL
)->s
;
236 /* NAIL_FILENAME_TEMPORARY? */
237 if (tmpname
!= NULL
) {
238 env_addon
[6] = str_concat_csvl(&s
, NAILENV_FILENAME_TEMPORARY
, "=",
243 sh
= ok_vlook(SHELL
);
245 if (mhp
->mh_flags
& MIME_HDL_NEEDSTERM
) {
250 pid
= run_command(sh
, NULL
, term_infd
, COMMAND_FD_PASS
, "-c",
251 mhp
->mh_shell_cmd
, NULL
, env_addon
);
252 rbuf
= (pid
< 0) ? NULL
: (FILE*)-1;
254 rbuf
= Popen(mhp
->mh_shell_cmd
, "W", sh
, env_addon
,
255 (mhp
->mh_flags
& MIME_HDL_ASYNC
? -1 : fileno(*qbuf
)));
258 n_err(_("Cannot run MIME type handler: %s: %s\n"),
259 mhp
->mh_msg
, strerror(errno
));
272 _out(char const *buf
, size_t len
, FILE *fp
, enum conversion convert
, enum
273 sendaction action
, struct quoteflt
*qf
, ui64_t
*stats
, struct str
*outrest
,
281 Well
... it turns out to
not work like that since of course a valid
282 RFC
4155 compliant parser
, like S
-nail
, takes care
for From_ lines only
283 after an empty line has been seen
, which cannot be detected that easily
285 ifdef HAVE_DEBUG
/* TODO assert legacy */
286 /* TODO if at all, this CAN only happen for SEND_DECRYPT, since all
287 * TODO other input situations handle RFC 4155 OR, if newly generated,
288 * TODO enforce quoted-printable if there is From_, as "required" by
289 * TODO RFC 5751. The SEND_DECRYPT case is not yet overhauled;
290 * TODO if it may happen in this path, we should just treat decryption
291 * TODO as we do for the other input paths; i.e., handle it in SSL!! */
292 if (action
== SEND_MBOX
|| action
== SEND_DECRYPT
)
293 assert(!is_head(buf
, len
, FAL0
));
295 if ((/*action == SEND_MBOX ||*/ action
== SEND_DECRYPT
) &&
296 is_head(buf
, len
, FAL0
)) {
302 flags
= ((int)action
& _TD_EOF
);
304 n
= mime_write(buf
, len
, fp
,
305 action
== SEND_MBOX
? CONV_NONE
: convert
,
306 flags
| ((action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
||
307 action
== SEND_QUOTE
|| action
== SEND_QUOTE_ALL
)
309 : (action
== SEND_TOSRCH
|| action
== SEND_TOPIPE
)
310 ? TD_ICONV
: (action
== SEND_SHOW
? TD_ISPR
: TD_NONE
)),
311 qf
, outrest
, inrest
);
324 _send_onpipe(int signo
)
326 NYD_X
; /* Signal handler */
328 siglongjmp(_send_pipejmp
, 1);
331 static sigjmp_buf __sendp_actjmp
; /* TODO someday.. */
332 static int __sendp_sig
; /* TODO someday.. */
333 static sighandler_type __sendp_opipe
;
335 __sendp_onsig(int sig
) /* TODO someday, we won't need it no more */
337 NYD_X
; /* Signal handler */
339 siglongjmp(__sendp_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
;
349 struct str outrest
, inrest
;
350 char *line
= NULL
, *cp
, *cp2
, *start
;
351 char const * volatile tmpname
= NULL
;
352 size_t linesize
= 0, linelen
, cnt
;
353 int volatile term_infd
;
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
;
362 n_UNINIT(term_infd
, 0);
365 if (ip
->m_mimecontent
== MIME_PKCS7
&& ip
->m_multipart
&&
366 action
!= SEND_MBOX
&& action
!= SEND_RFC822
&& action
!= SEND_SHOW
)
372 if (!is_ign("status", 6, doign
))
374 if (!is_ign("x-status", 8, doign
))
379 if ((ibuf
= setinput(&mb
, (struct message
*)ip
, NEED_BODY
)) == NULL
) {
385 if (ip
->m_mimecontent
== MIME_DISCARD
)
388 if (!(ip
->m_flag
& MNOFROM
))
389 while (cnt
&& (c
= getc(ibuf
)) != EOF
) {
394 convert
= (action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
||
395 action
== SEND_QUOTE
|| action
== SEND_QUOTE_ALL
||
396 action
== SEND_TOSRCH
)
397 ? CONV_FROMHDR
: CONV_NONE
;
399 /* Work the headers */
400 quoteflt_reset(qf
, obuf
);
411 while (fgetline(&line
, &linesize
, &cnt
, &linelen
, ibuf
, 0)) {
413 if (line
[0] == '\n') {
414 /* If line is blank, we've reached end of headers, so force out
415 * status: field and note that we are no longer in header fields */
417 statusput(zmp
, obuf
, qf
, stats
);
419 xstatusput(zmp
, obuf
, qf
, stats
);
420 if (doign
!= allignore
)
421 _out("\n", 1, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
,NULL
);
426 if ((hps
& HPS_IN_FIELD
) && blankchar(line
[0])) {
427 /* If this line is a continuation (SP / HT) of a previous header
428 * field, determine if the start of the line is a MIME encoded word */
429 if (hps
& HPS_ISENC_2
) {
430 for (cp
= line
; blankchar(*cp
); ++cp
)
432 if (cp
> line
&& linelen
- PTR2SIZE(cp
- line
) > 8 &&
433 cp
[0] == '=' && cp
[1] == '?')
437 /* Pick up the header field if we have one */
438 for (cp
= line
; (c
= *cp
& 0377) && c
!= ':' && !spacechar(c
); ++cp
)
441 while (spacechar(*cp
))
445 n_err(_("Malformed message: headers and body not separated "
446 "(with empty line)\n"));
447 /* Not a header line, force out status: This happens in uucp style
448 * mail where there are no headers at all */
449 if (level
== 0 /*&& lineno == 1*/) {
451 statusput(zmp
, obuf
, qf
, stats
);
453 xstatusput(zmp
, obuf
, qf
, stats
);
455 if (doign
!= allignore
)
456 _out("\n", 1, obuf
, CONV_NONE
,SEND_MBOX
, qf
, stats
, NULL
,NULL
);
460 /* If it is an ignored field and we care about such things, skip it.
461 * Misuse dostat also for another bit xxx use a bitenum + for more */
462 if (ok_blook(keep_content_length
))
465 *cp2
= 0; /* temporarily null terminate */
466 if ((doign
&& is_ign(line
, PTR2SIZE(cp2
- line
), doign
)) ||
467 (action
== SEND_MBOX
&& !(dostat
& (1 << 2)) &&
468 (!asccasecmp(line
, "content-length") ||
469 !asccasecmp(line
, "lines"))))
471 else if (!asccasecmp(line
, "status")) {
472 /* If field is "status," go compute and print real Status: field */
474 statusput(zmp
, obuf
, qf
, stats
);
478 } else if (!asccasecmp(line
, "x-status")) {
479 /* If field is "status," go compute and print real Status: field */
481 xstatusput(zmp
, obuf
, qf
, stats
);
487 /* For colourization we need the complete line, so save it */
488 /* XXX This is all temporary (colour belongs into backend), so
489 * XXX use tmpname as a temporary storage in the meanwhile */
491 if (pstate
& PS_COLOUR_ACTIVE
)
492 tmpname
= savestrbuf(line
, PTR2SIZE(cp2
- line
));
500 /* Determine if the end of the line is a MIME encoded word */
501 /* TODO geeeh! all this lengthy stuff that follows is about is dealing
502 * TODO with header follow lines, and it should be up to the backend
503 * TODO what happens and what not, i.e., it doesn't matter whether it's
504 * TODO a MIME-encoded word or not, as long as a single separating space
505 * TODO remains in between lines (the MIME stuff will correctly remove
506 * TODO whitespace in between multiple adjacent encoded words) */
508 if (cnt
&& (c
= getc(ibuf
)) != EOF
) {
510 cp
= line
+ linelen
- 1;
511 if (linelen
> 0 && *cp
== '\n')
513 while (cp
>= line
&& whitechar(*cp
))
515 if (PTR2SIZE(cp
- line
> 8) && cp
[0] == '=' && cp
[-1] == '?')
521 if (!(hps
& HPS_IGNORE
)) {
522 size_t len
= linelen
;
524 if (action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
||
525 action
== SEND_QUOTE
|| action
== SEND_QUOTE_ALL
||
526 action
== SEND_TOSRCH
) {
527 /* Strip blank characters if two MIME-encoded words follow on
528 * continuing lines */
529 if (hps
& HPS_ISENC_1
)
530 while (len
> 0 && blankchar(*start
)) {
534 if (hps
& HPS_ISENC_2
)
535 if (len
> 0 && start
[len
- 1] == '\n')
537 while (len
> 0 && blankchar(start
[len
- 1]))
542 bool_t colour_stripped
= FAL0
;
543 if (tmpname
!= NULL
) {
544 n_colour_put(obuf
, n_COLOUR_ID_VIEW_HEADER
, tmpname
);
545 if (len
> 0 && start
[len
- 1] == '\n') {
546 colour_stripped
= TRU1
;
551 _out(start
, len
, obuf
, convert
, action
, qf
, stats
, NULL
,NULL
);
553 if (tmpname
!= NULL
) {
554 n_colour_reset(obuf
);
574 memset(&mh
, 0, sizeof mh
);
576 switch (ip
->m_mimecontent
) {
580 case SEND_TODISP_ALL
:
583 if (ok_blook(rfc822_body_from_
)) {
584 if (qf
->qf_pfix_len
> 0) {
585 size_t i
= fwrite(qf
->qf_pfix
, sizeof *qf
->qf_pfix
,
586 qf
->qf_pfix_len
, obuf
);
587 if (i
== qf
->qf_pfix_len
&& stats
!= NULL
)
590 put_from_(obuf
, ip
->m_multipart
, stats
);
598 if (ok_blook(rfc822_body_from_
))
599 put_from_(obuf
, ip
->m_multipart
, stats
);
609 case MIME_TEXT_PLAIN
:
612 case SEND_TODISP_ALL
:
615 switch (mime_type_handler(&mh
, ip
, action
)) {
617 _out(mh
.mh_msg
.s
, mh
.mh_msg
.l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
,
619 /* We would print this as plain text, so better force going home */
630 if (action
!= SEND_DECRYPT
)
634 if (action
!= SEND_MBOX
&& action
!= SEND_RFC822
&&
635 action
!= SEND_SHOW
&& ip
->m_multipart
!= NULL
)
641 case SEND_TODISP_ALL
:
644 switch (mime_type_handler(&mh
, ip
, action
)) {
646 _out(mh
.mh_msg
.s
, mh
.mh_msg
.l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
,
648 /* We would print this as plain text, so better force going home */
651 /* FIXME WE NEED TO DO THAT IF WE ARE THE ONLY MAIL
652 * FIXME CONTENT !! */
657 if (level
== 0 && cnt
) {
658 char const *x
= _("[-- Binary content --]\n");
659 _out(x
, strlen(x
), obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
,
675 case MIME_ALTERNATIVE
:
676 if ((action
== SEND_TODISP
|| action
== SEND_QUOTE
) &&
677 !ok_blook(print_alternatives
)) {
678 /* XXX This (a) should not remain (b) should be own fun
679 * TODO (despite the fact that v15 will do this completely differently
680 * TODO by having an action-specific "manager" that will traverse the
681 * TODO parsed MIME tree and decide for each part whether it'll be
682 * TODO displayed or not *before* we walk the tree for doing action */
684 struct mpstack
*outer
;
686 } outermost
, * volatile curr
, * volatile mpsp
;
687 bool_t
volatile neednl
, hadpart
;
688 struct n_sigman smalter
;
690 (curr
= &outermost
)->outer
= NULL
;
692 neednl
= hadpart
= FAL0
;
694 n_SIGMAN_ENTER_SWITCH(&smalter
, n_SIGMAN_ALL
) {
702 for (np
= ip
->m_multipart
;;) {
704 for (; np
!= NULL
; np
= np
->m_nextpart
) {
705 if (action
!= SEND_QUOTE
&& np
->m_ct_type_plain
!= NULL
) {
707 _out("\n", 1, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
,
709 _print_part_info(obuf
, np
, doign
, level
, qf
, stats
);
713 switch (np
->m_mimecontent
) {
714 case MIME_ALTERNATIVE
:
718 mpsp
= salloc(sizeof *mpsp
);
720 mpsp
->mp
= np
->m_multipart
;
729 switch (mime_type_handler(&mh
, np
, action
)) {
731 mh
.mh_flags
= MIME_HDL_NULL
;
732 continue; /* break; break; */
734 if (!ok_blook(mime_alternative_favour_rich
)) {/* TODO */
735 struct mimepart
*x
= np
;
737 while ((x
= x
->m_nextpart
) != NULL
) {
738 struct mime_handler mhx
;
740 if (x
->m_mimecontent
== MIME_TEXT_PLAIN
||
741 mime_type_handler(&mhx
, x
, action
) ==
746 continue; /* break; break; */
754 case MIME_TEXT_PLAIN
:
757 if (ok_blook(mime_alternative_favour_rich
)) { /* TODO */
758 struct mimepart
*x
= np
;
760 /* TODO twice TODO, we should dive into /related and
761 * TODO check whether that has rich parts! */
762 while ((x
= x
->m_nextpart
) != NULL
) {
763 struct mime_handler mhx
;
765 switch (mime_type_handler(&mhx
, x
, action
)) {
774 continue; /* break; break; */
778 if (action
== SEND_QUOTE
&& hadpart
) {
779 struct quoteflt
*dummy
= quoteflt_dummy();
780 _out("\n", 1, obuf
, CONV_NONE
, SEND_MBOX
, dummy
, stats
,
782 quoteflt_flush(dummy
);
786 rv
= sendpart(zmp
, np
, obuf
, doign
, qf
, action
, stats
,
788 quoteflt_reset(qf
, origobuf
);
791 curr
= &outermost
; /* Cause overall loop termination */
800 np
= curr
->mp
->m_nextpart
;
803 n_sigman_leave(&smalter
, n_SIGMAN_VIPSIGS_NTTYOUT
);
812 case SEND_TODISP_ALL
:
820 if ((action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
) &&
821 ip
->m_multipart
!= NULL
&&
822 ip
->m_multipart
->m_mimecontent
== MIME_DISCARD
&&
823 ip
->m_multipart
->m_nextpart
== NULL
) {
824 char const *x
= _("[Missing multipart boundary - use show "
825 "to display the raw message]\n");
826 _out(x
, strlen(x
), obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
,
830 for (np
= ip
->m_multipart
; np
!= NULL
; np
= np
->m_nextpart
) {
831 bool_t
volatile ispipe
;
833 if (np
->m_mimecontent
== MIME_DISCARD
&& action
!= SEND_DECRYPT
)
839 if (np
->m_partstring
&& !strcmp(np
->m_partstring
, "1"))
842 /* TODO Always open multipart on /dev/null, it's a hack to be
843 * TODO able to dive into that structure, and still better
844 * TODO than asking the user for something stupid.
845 * TODO oh, wait, we did ask for a filename for this MIME mail,
846 * TODO and that outer container is useless anyway ;-P */
847 if (np
->m_multipart
!= NULL
) {
848 if ((obuf
= Fopen("/dev/null", "w")) == NULL
)
850 } else if ((obuf
= newfile(np
, &ispipe
)) == NULL
)
854 if (sigsetjmp(_send_pipejmp
, 1)) {
858 oldpipe
= safe_signal(SIGPIPE
, &_send_onpipe
);
861 case SEND_TODISP_ALL
:
862 if (ip
->m_mimecontent
!= MIME_ALTERNATIVE
&&
863 ip
->m_mimecontent
!= MIME_RELATED
&&
864 ip
->m_mimecontent
!= MIME_DIGEST
&&
865 ip
->m_mimecontent
!= MIME_MULTI
)
867 _print_part_info(obuf
, np
, doign
, level
, qf
, stats
);
881 if ((action
== SEND_QUOTE
|| action
== SEND_QUOTE_ALL
) &&
882 np
->m_multipart
== NULL
&& ip
->m_parent
!= NULL
) {
883 struct quoteflt
*dummy
= quoteflt_dummy();
884 _out("\n", 1, obuf
, CONV_NONE
, SEND_MBOX
, dummy
, stats
,
886 quoteflt_flush(dummy
);
888 if (sendpart(zmp
, np
, obuf
, doign
, qf
, action
, stats
, level
+1) < 0)
890 quoteflt_reset(qf
, origobuf
);
892 if (action
== SEND_QUOTE
) {
893 if (ip
->m_mimecontent
!= MIME_RELATED
)
896 if (action
== SEND_TOFILE
&& obuf
!= origobuf
) {
901 safe_signal(SIGPIPE
, SIG_IGN
);
903 safe_signal(SIGPIPE
, oldpipe
);
916 /* Copy out message body */
917 if (doign
== allignore
&& level
== 0) /* skip final blank line */
919 switch (ip
->m_mime_enc
) {
926 convert
= CONV_FROMQP
;
929 switch (ip
->m_mimecontent
) {
931 case MIME_TEXT_PLAIN
:
933 convert
= CONV_FROMB64_T
;
936 switch (mh
.mh_flags
& MIME_HDL_TYPE_MASK
) {
939 convert
= CONV_FROMB64_T
;
942 convert
= CONV_FROMB64
;
952 if (action
== SEND_DECRYPT
|| action
== SEND_MBOX
||
953 action
== SEND_RFC822
|| action
== SEND_SHOW
)
956 if ((action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
||
957 action
== SEND_QUOTE
|| action
== SEND_QUOTE_ALL
||
958 action
== SEND_TOSRCH
) &&
959 (ip
->m_mimecontent
== MIME_TEXT_PLAIN
||
960 ip
->m_mimecontent
== MIME_TEXT_HTML
||
961 ip
->m_mimecontent
== MIME_TEXT
||
962 (mh
.mh_flags
& MIME_HDL_TYPE_MASK
) == MIME_HDL_TEXT
||
963 (mh
.mh_flags
& MIME_HDL_TYPE_MASK
) == MIME_HDL_PTF
)) {
964 char const *tcs
= charset_get_lc();
966 if (iconvd
!= (iconv_t
)-1)
967 n_iconv_close(iconvd
);
968 /* TODO Since Base64 has an odd 4:3 relation in between input
969 * TODO and output an input line may end with a partial
970 * TODO multibyte character; this is no problem at all unless
971 * TODO we send to the display or whatever, i.e., ensure
972 * TODO makeprint() or something; to avoid this trap, *force*
973 * TODO iconv(), in which case this layer will handle leftovers
974 * TODO correctly. It's a pre-v15 we-have-filters hack */
975 if (convert
== CONV_FROMB64_T
|| (asccasecmp(tcs
, ip
->m_charset
) &&
976 asccasecmp(charset_get_7bit(), ip
->m_charset
))) {
977 iconvd
= n_iconv_open(tcs
, ip
->m_charset
);
978 if (iconvd
== (iconv_t
)-1 && errno
== EINVAL
) {
979 n_err(_("Cannot convert from %s to %s\n"), ip
->m_charset
, tcs
);
980 /*rv = 1; goto jleave;*/
986 switch (mh
.mh_flags
& MIME_HDL_TYPE_MASK
) {
992 term_infd
= COMMAND_FD_PASS
;
993 if (mh
.mh_flags
& (MIME_HDL_TMPF
| MIME_HDL_NEEDSTERM
)) {
996 of
= OF_RDWR
| OF_REGISTER
;
997 if (!(mh
.mh_flags
& MIME_HDL_TMPF
)) {
999 mh
.mh_flags
|= MIME_HDL_TMPF_FILL
;
1001 } else if (mh
.mh_flags
& MIME_HDL_TMPF_UNLINK
)
1002 of
|= OF_REGISTER_UNLINK
;
1004 if ((pbuf
= Ftmp((mh
.mh_flags
& MIME_HDL_TMPF
? &cp
: NULL
),
1005 (mh
.mh_flags
& MIME_HDL_TMPF_FILL
? "mimehdlfill" : "mimehdl"),
1009 if (mh
.mh_flags
& MIME_HDL_TMPF
) {
1010 tmpname
= savestr(cp
);
1014 if (mh
.mh_flags
& MIME_HDL_TMPF_FILL
) {
1016 term_infd
= fileno(pbuf
);
1022 pbuf
= _pipefile(&mh
, ip
, n_UNVOLATILE(&qbuf
), tmpname
, term_infd
);
1028 } else if ((mh
.mh_flags
& MIME_HDL_NEEDSTERM
) && pbuf
== (FILE*)-1) {
1033 action
= SEND_TOPIPE
;
1035 oldpipe
= safe_signal(SIGPIPE
, &_send_onpipe
);
1036 if (sigsetjmp(_send_pipejmp
, 1))
1042 mh
.mh_flags
= MIME_HDL_NULL
;
1049 bool_t
volatile eof
;
1050 ui32_t save_qf_pfix_len
= qf
->qf_pfix_len
;
1051 ui64_t
*save_stats
= stats
;
1053 if (pbuf
!= origobuf
) {
1054 qf
->qf_pfix_len
= 0; /* XXX legacy (remove filter instead) */
1058 outrest
.s
= inrest
.s
= NULL
;
1059 outrest
.l
= inrest
.l
= 0;
1063 __sendp_opipe
= safe_signal(SIGPIPE
, &__sendp_onsig
);
1064 if (sigsetjmp(__sendp_actjmp
, 1)) {
1065 if (outrest
.s
!= NULL
)
1067 if (inrest
.s
!= NULL
)
1071 if (iconvd
!= (iconv_t
)-1)
1072 n_iconv_close(iconvd
);
1074 safe_signal(SIGPIPE
, __sendp_opipe
);
1075 n_raise(__sendp_sig
);
1079 quoteflt_reset(qf
, pbuf
);
1080 while (!eof
&& fgetline(&line
, &linesize
, &cnt
, &linelen
, ibuf
, 0)) {
1082 if (_out(line
, linelen
, pbuf
, convert
, action
, qf
, stats
, &outrest
,
1083 (action
& _TD_EOF
? NULL
: &inrest
)) < 0 || ferror(pbuf
)) {
1084 rv
= -1; /* XXX Should bail away?! */
1088 if(eof
<= FAL0
&& rv
>= 0 && (outrest
.l
!= 0 || inrest
.l
!= 0)){
1090 if(eof
|| inrest
.l
== 0)
1092 eof
= eof
? TRU1
: TRUM1
;
1096 /* TODO HACK: when sending to the display we yet get fooled if a message
1097 * TODO doesn't end in a newline, because of our input/output 1:1.
1098 * TODO This should be handled automatically by a display filter, then */
1099 if(rv
>= 0 && !qf
->qf_nl_last
&&
1100 (action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
))
1101 rv
= quoteflt_push(qf
, "\n", 1);
1105 if (rv
>= 0 && (mh
.mh_flags
& MIME_HDL_TMPF_FILL
)) {
1106 mh
.mh_flags
&= ~MIME_HDL_TMPF_FILL
;
1108 really_rewind(pbuf
);
1109 /* Don't Fclose() the Ftmp() thing due to OF_REGISTER_UNLINK++ */
1110 goto jpipe_for_real
;
1114 safe_signal(SIGPIPE
, __sendp_opipe
);
1116 if (outrest
.s
!= NULL
)
1118 if (inrest
.s
!= NULL
)
1121 if (pbuf
!= origobuf
) {
1122 qf
->qf_pfix_len
= save_qf_pfix_len
;
1131 safe_signal(SIGPIPE
, SIG_IGN
);
1132 Pclose(pbuf
, !(mh
.mh_flags
& MIME_HDL_ASYNC
));
1133 safe_signal(SIGPIPE
, oldpipe
);
1134 if (rv
>= 0 && qbuf
!= NULL
&& qbuf
!= obuf
)
1135 pipecpy(qbuf
, obuf
, origobuf
, qf
, stats
);
1138 if (iconvd
!= (iconv_t
)-1)
1139 n_iconv_close(iconvd
);
1147 newfile(struct mimepart
*ip
, bool_t
volatile *ispipe
)
1157 if (f
!= NULL
&& f
!= (char*)-1) {
1160 makeprint(&in
, &out
);
1161 out
.l
= delctrl(out
.s
, out
.l
);
1162 f
= savestrbuf(out
.s
, out
.l
);
1166 /* In interactive mode, let user perform all kind of expansions as desired,
1167 * and offer |SHELL-SPEC pipe targets, too */
1168 if (options
& OPT_INTERACTIVE
) {
1170 struct n_string shou
, *shoup
;
1173 shoup
= n_string_creat_auto(&shou
);
1175 /* TODO Generic function which asks for filename.
1176 * TODO If the current part is the first textpart the target
1177 * TODO is implicit from outer `write' etc! */
1178 /* I18N: Filename input prompt with file type indication */
1179 str_concat_csvl(&prompt
, _("Enter filename for part "),
1180 (ip
->m_partstring
!= NULL
) ? ip
->m_partstring
: _("?"),
1181 _(" ("), ip
->m_ct_type_plain
, _("): "), NULL
);
1183 f2
= n_lex_input_cp(n_LEXINPUT_CTX_DEFAULT
| n_LEXINPUT_HIST_ADD
,
1184 prompt
.s
, ((f
!= (char*)-1 && f
!= NULL
)
1185 ? n_shexp_quote_cp(f
, FAL0
) : NULL
));
1187 in
.s
= n_UNCONST(f2
);
1189 if((n_shexp_parse_token(shoup
, &in
, n_SHEXP_PARSE_TRUNC
|
1190 n_SHEXP_PARSE_TRIMSPACE
| n_SHEXP_PARSE_LOG
|
1191 n_SHEXP_PARSE_IGNORE_EMPTY
) &
1192 (n_SHEXP_STATE_STOP
| n_SHEXP_STATE_OUTPUT
|
1193 n_SHEXP_STATE_ERR_MASK
)
1194 ) != (n_SHEXP_STATE_STOP
| n_SHEXP_STATE_OUTPUT
))
1196 f2
= n_string_cp(shoup
);
1198 if (f2
== NULL
|| *f2
== '\0') {
1199 if (options
& OPT_D_V
)
1200 n_err(_("... skipping this\n"));
1201 n_string_gut(shoup
);
1207 /* Pipes are expanded by the shell */
1209 else if ((f3
= fexpand(f2
, FEXP_LOCAL
| FEXP_NVAR
)) == NULL
)
1210 /* (Error message written by fexpand()) */
1215 n_string_gut(shoup
);
1218 if (f
== NULL
|| f
== (char*)-1 || *f
== '\0')
1220 else if (options
& OPT_INTERACTIVE
) {
1222 fp
= Popen(&f
[1], "w", ok_vlook(SHELL
), NULL
, 1);
1223 if (!(*ispipe
= (fp
!= NULL
)))
1225 } else if ((fp
= Fopen(f
, "w")) == NULL
)
1226 n_err(_("Cannot open %s\n"), n_shexp_quote_cp(f
, FAL0
));
1228 /* Be very picky in non-interactive mode: actively disallow pipes,
1229 * prevent directory separators, and any filename member that would
1230 * become expanded by the shell if the name would be echo(1)ed */
1231 if(anyof(f
, "/" n_SHEXP_MAGIC_PATH_CHARS
)){
1234 for(out
.s
= salloc((strlen(f
) * 3) +1), out
.l
= 0; (c
= *f
++) != '\0';)
1235 if(strchr("/" n_SHEXP_MAGIC_PATH_CHARS
, c
)){
1236 out
.s
[out
.l
++] = '%';
1237 n_c_to_hex_base16(&out
.s
[out
.l
], c
);
1241 out
.s
[out
.l
] = '\0';
1245 /* Avoid overwriting of existing files */
1246 while((fp
= Fopen(f
, "wx")) == NULL
){
1249 if((e
= errno
) != EEXIST
){
1250 n_err(_("Cannot open %s: %s\n"),
1251 n_shexp_quote_cp(f
, FAL0
), strerror(e
));
1255 if(ip
->m_partstring
!= NULL
)
1256 f
= savecatsep(f
, '#', ip
->m_partstring
);
1258 f
= savecat(f
, "#.");
1267 pipecpy(FILE *pipebuf
, FILE *outbuf
, FILE *origobuf
, struct quoteflt
*qf
,
1270 char *line
= NULL
; /* TODO line pool */
1271 size_t linesize
= 0, linelen
, cnt
;
1277 cnt
= (size_t)fsize(pipebuf
);
1280 quoteflt_reset(qf
, outbuf
);
1281 while (fgetline(&line
, &linesize
, &cnt
, &linelen
, pipebuf
, 0) != NULL
) {
1282 if ((sz
= quoteflt_push(qf
, line
, linelen
)) < 0)
1286 if ((sz
= quoteflt_flush(qf
)) > 0)
1291 if (all_sz
> 0 && outbuf
== origobuf
&& stats
!= NULL
)
1298 statusput(const struct message
*mp
, FILE *obuf
, struct quoteflt
*qf
,
1301 char statout
[3], *cp
= statout
;
1304 if (mp
->m_flag
& MREAD
)
1306 if (!(mp
->m_flag
& MNEW
))
1310 int i
= fprintf(obuf
, "%.*sStatus: %s\n", (int)qf
->qf_pfix_len
,
1311 (qf
->qf_pfix_len
> 0 ? qf
->qf_pfix
: 0), statout
);
1312 if (i
> 0 && stats
!= NULL
)
1319 xstatusput(const struct message
*mp
, FILE *obuf
, struct quoteflt
*qf
,
1323 char *xp
= xstatout
;
1326 if (mp
->m_flag
& MFLAGGED
)
1328 if (mp
->m_flag
& MANSWERED
)
1330 if (mp
->m_flag
& MDRAFTED
)
1334 int i
= fprintf(obuf
, "%.*sX-Status: %s\n", (int)qf
->qf_pfix_len
,
1335 (qf
->qf_pfix_len
> 0 ? qf
->qf_pfix
: 0), xstatout
);
1336 if (i
> 0 && stats
!= NULL
)
1343 put_from_(FILE *fp
, struct mimepart
*ip
, ui64_t
*stats
)
1345 char const *froma
, *date
, *nl
;
1349 if (ip
!= NULL
&& ip
->m_from
!= NULL
) {
1351 date
= fakedate(ip
->m_time
);
1355 date
= time_current
.tc_ctime
;
1359 n_COLOUR( n_colour_put(fp
, n_COLOUR_ID_VIEW_FROM_
, NULL
); )
1360 i
= fprintf(fp
, "From %s %s%s", froma
, date
, nl
);
1361 n_COLOUR( n_colour_reset(fp
); )
1362 if (i
> 0 && stats
!= NULL
)
1368 sendmp(struct message
*mp
, FILE *obuf
, struct ignoretab
*doign
,
1369 char const *prefix
, enum sendaction action
, ui64_t
*stats
)
1374 enum mime_parse_flags mpf
;
1375 struct mimepart
*ip
;
1379 time_current_update(&time_current
, TRU1
);
1381 if (mp
== dot
&& action
!= SEND_TOSRCH
)
1382 pstate
|= PS_DID_PRINT_DOT
;
1385 quoteflt_init(&qf
, prefix
);
1387 /* First line is the From_ line, so no headers there to worry about */
1388 if ((ibuf
= setinput(&mb
, mp
, NEED_BODY
)) == NULL
)
1395 char const *cpre
= n_empty
, *csuf
= n_empty
;
1397 struct n_colour_pen
*cpen
= n_colour_pen_create(n_COLOUR_ID_VIEW_FROM_
,NULL
);
1398 struct str
const *sp
= n_colour_pen_to_str(cpen
);
1402 sp
= n_colour_reset_to_str();
1408 nozap
= (doign
!= allignore
&& doign
!= fwdignore
&& action
!= SEND_RFC822
&&
1409 !is_ign("from_", sizeof("from_") -1, doign
));
1410 if (mp
->m_flag
& MNOFROM
) {
1412 sz
= fprintf(obuf
, "%s%.*sFrom %s %s%s\n",
1413 cpre
, (int)qf
.qf_pfix_len
,
1414 (qf
.qf_pfix_len
!= 0 ? qf
.qf_pfix
: n_empty
), fakefrom(mp
),
1415 fakedate(mp
->m_time
), csuf
);
1417 if (qf
.qf_pfix_len
> 0) {
1418 i
= fwrite(qf
.qf_pfix
, sizeof *qf
.qf_pfix
, qf
.qf_pfix_len
, obuf
);
1419 if (i
!= qf
.qf_pfix_len
)
1426 cpre
= (char const*)0x1;
1430 while (cnt
> 0 && (c
= getc(ibuf
)) != EOF
) {
1432 if (c
== '\n' && csuf
!= NULL
) {
1433 cpre
= (char const*)0x1;
1445 if (csuf
!= NULL
&& cpre
!= (char const*)0x1)
1449 while (cnt
> 0 && (c
= getc(ibuf
)) != EOF
) {
1456 if (sz
> 0 && stats
!= NULL
)
1459 mpf
= MIME_PARSE_NONE
;
1460 if (action
!= SEND_MBOX
&& action
!= SEND_RFC822
&& action
!= SEND_SHOW
)
1461 mpf
|= MIME_PARSE_DECRYPT
| MIME_PARSE_PARTS
;
1462 if ((ip
= mime_parse_msg(mp
, mpf
)) == NULL
)
1465 rv
= sendpart(mp
, ip
, obuf
, doign
, &qf
, action
, stats
, 0);
1467 quoteflt_destroy(&qf
);