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 - 2017 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 n_ignore
const *doitp
, 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 n_ignore
const *doitp
,
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 n_ignore
const *doitp
, int level
, struct quoteflt
*qf
, ui64_t
*stats
)
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') || (cp
[0] == '1' && /* TODO */
111 cp
[1] == '.' && cp
[2] != '1')) /* TODO code should not look like so */
112 _out("\n", 1, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
, NULL
);
114 /* Part id, content-type, encoding, charset */
116 _out(cpre
->s
, cpre
->l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
,NULL
);
117 _out("[-- #", 5, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
,NULL
);
118 _out(cp
, strlen(cp
), obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
,NULL
);
120 to
.l
= snprintf(buf
, sizeof buf
, " %" PRIuZ
"/%" PRIuZ
" ",
121 (uiz_t
)mpp
->m_lines
, (uiz_t
)mpp
->m_size
);
122 _out(buf
, to
.l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
,NULL
);
124 if ((cp
= mpp
->m_ct_type_usr_ovwr
) != NULL
)
125 _out("+", 1, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
,NULL
);
127 cp
= mpp
->m_ct_type_plain
;
128 if ((to
.l
= strlen(cp
)) > 30 && is_asccaseprefix("application/", cp
)) {
129 size_t const al
= sizeof("appl../") -1, fl
= sizeof("application/") -1;
130 size_t i
= to
.l
- fl
;
131 char *x
= salloc(al
+ i
+1);
133 memcpy(x
, "appl../", al
);
134 memcpy(x
+ al
, cp
+ fl
, i
+1);
138 _out(cp
, to
.l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
,NULL
);
140 if (mpp
->m_multipart
== NULL
/* TODO */ && (cp
= mpp
->m_ct_enc
) != NULL
) {
141 _out(", ", 2, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
,NULL
);
142 if (to
.l
> 25 && !asccasecmp(cp
, "quoted-printable"))
144 _out(cp
, strlen(cp
), obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
,NULL
);
147 if (mpp
->m_multipart
== NULL
/* TODO */ && (cp
= mpp
->m_charset
) != NULL
) {
148 _out(", ", 2, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
,NULL
);
149 _out(cp
, strlen(cp
), obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
,NULL
);
152 _out(" --]", 4, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
,NULL
);
154 _out(csuf
->s
, csuf
->l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
,NULL
);
155 _out("\n", 1, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
,NULL
);
158 if (mpp
->m_content_info
& CI_MIME_ERRORS
) {
160 _out(cpre
->s
, cpre
->l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
,
162 _out("[-- ", 4, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
, NULL
);
164 ti
.l
= strlen(ti
.s
= n_UNCONST(_("Defective MIME structure")));
166 to
.l
= delctrl(to
.s
, to
.l
);
167 _out(to
.s
, to
.l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
, NULL
);
170 _out(" --]", 4, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
, NULL
);
172 _out(csuf
->s
, csuf
->l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
,
174 _out("\n", 1, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
, NULL
);
177 /* Content-Description */
178 if (n_ignore_is_ign(doitp
, "content-description", 19) &&
179 (cp
= mpp
->m_content_description
) != NULL
&& *cp
!= '\0') {
181 _out(cpre
->s
, cpre
->l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
,
183 _out("[-- ", 4, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
, NULL
);
185 ti
.l
= strlen(ti
.s
= n_UNCONST(mpp
->m_content_description
));
186 mime_fromhdr(&ti
, &to
, TD_ISPR
| TD_ICONV
);
187 _out(to
.s
, to
.l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
, NULL
);
190 _out(" --]", 4, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
, NULL
);
192 _out(csuf
->s
, csuf
->l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
,
194 _out("\n", 1, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
, NULL
);
198 if (n_ignore_is_ign(doitp
, "content-disposition", 19) &&
199 mpp
->m_filename
!= NULL
&& *mpp
->m_filename
!= '\0') {
201 _out(cpre
->s
, cpre
->l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
,
203 _out("[-- ", 4, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
, NULL
);
205 ti
.l
= strlen(ti
.s
= mpp
->m_filename
);
207 to
.l
= delctrl(to
.s
, to
.l
);
208 _out(to
.s
, to
.l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
, NULL
);
211 _out(" --]", 4, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
, NULL
);
213 _out(csuf
->s
, csuf
->l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
,
215 _out("\n", 1, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
, NULL
);
221 _pipefile(struct mime_handler
*mhp
, struct mimepart
const *mpp
, FILE **qbuf
,
222 char const *tmpname
, int term_infd
)
225 char const *env_addon
[8], *cp
, *sh
;
231 if (mhp
->mh_flags
& MIME_HDL_ISQUOTE
) {
232 if ((*qbuf
= Ftmp(NULL
, "sendp", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==
234 n_perr(_("tmpfile"), 0);
239 if ((mhp
->mh_flags
& MIME_HDL_TYPE_MASK
) == MIME_HDL_PTF
) {
240 union {int (*ptf
)(void); char const *sh
;} u
;
243 if (*qbuf
!= stdout
) /* xxx never? v15: it'll be a filter anyway */
247 if((rbuf
= Popen((char*)-1, "W", u
.sh
, NULL
, fileno(*qbuf
))) == NULL
)
253 if (mpp
== NULL
|| (cp
= mpp
->m_filename
) == NULL
)
255 env_addon
[0] = str_concat_csvl(&s
, NAILENV_FILENAME
, "=", cp
, NULL
)->s
;
257 /* NAIL_FILENAME_GENERATED *//* TODO pathconf NAME_MAX; but user can create
258 * TODO a file wherever he wants! *Do* create a zero-size temporary file
259 * TODO and give *that* path as NAIL_FILENAME_TEMPORARY, clean it up once
260 * TODO the pipe returns? Like this we *can* verify path/name issues! */
261 env_addon
[1] = str_concat_csvl(&s
, NAILENV_FILENAME_GENERATED
, "=",
262 getrandstring(n_MIN(NAME_MAX
/ 4, 16)), NULL
)->s
;
264 /* NAIL_CONTENT{,_EVIDENCE} */
265 if (mpp
== NULL
|| (cp
= mpp
->m_ct_type_plain
) == NULL
)
267 env_addon
[2] = str_concat_csvl(&s
, NAILENV_CONTENT
, "=", cp
, NULL
)->s
;
269 if (mpp
!= NULL
&& mpp
->m_ct_type_usr_ovwr
!= NULL
)
270 cp
= mpp
->m_ct_type_usr_ovwr
;
271 env_addon
[3] = str_concat_csvl(&s
, NAILENV_CONTENT_EVIDENCE
, "=", cp
,
274 env_addon
[4] = str_concat_csvl(&s
, NAILENV_TMPDIR
, /* TODO v15*/
275 "=", ok_vlook(TMPDIR
), NULL
)->s
;
279 /* NAIL_FILENAME_TEMPORARY? */
280 if (tmpname
!= NULL
) {
281 env_addon
[5] = str_concat_csvl(&s
, NAILENV_FILENAME_TEMPORARY
, "=",
286 sh
= ok_vlook(SHELL
);
288 if (mhp
->mh_flags
& MIME_HDL_NEEDSTERM
) {
293 pid
= run_command(sh
, &nset
, term_infd
, COMMAND_FD_PASS
, "-c",
294 mhp
->mh_shell_cmd
, NULL
, env_addon
);
295 rbuf
= (pid
< 0) ? NULL
: (FILE*)-1;
297 rbuf
= Popen(mhp
->mh_shell_cmd
, "W", sh
, env_addon
,
298 (mhp
->mh_flags
& MIME_HDL_ASYNC
? -1 : fileno(*qbuf
)));
301 n_err(_("Cannot run MIME type handler: %s: %s\n"),
302 mhp
->mh_msg
, strerror(errno
));
315 _out(char const *buf
, size_t len
, FILE *fp
, enum conversion convert
, enum
316 sendaction action
, struct quoteflt
*qf
, ui64_t
*stats
, struct str
*outrest
,
324 Well
... it turns out to
not work like that since of course a valid
325 RFC
4155 compliant parser
, like S
-nail
, takes care
for From_ lines only
326 after an empty line has been seen
, which cannot be detected that easily
328 ifdef HAVE_DEBUG
/* TODO assert legacy */
329 /* TODO if at all, this CAN only happen for SEND_DECRYPT, since all
330 * TODO other input situations handle RFC 4155 OR, if newly generated,
331 * TODO enforce quoted-printable if there is From_, as "required" by
332 * TODO RFC 5751. The SEND_DECRYPT case is not yet overhauled;
333 * TODO if it may happen in this path, we should just treat decryption
334 * TODO as we do for the other input paths; i.e., handle it in SSL!! */
335 if (action
== SEND_MBOX
|| action
== SEND_DECRYPT
)
336 assert(!is_head(buf
, len
, FAL0
));
338 if ((/*action == SEND_MBOX ||*/ action
== SEND_DECRYPT
) &&
339 is_head(buf
, len
, FAL0
)) {
345 flags
= ((int)action
& _TD_EOF
);
347 n
= mime_write(buf
, len
, fp
,
348 action
== SEND_MBOX
? CONV_NONE
: convert
,
349 flags
| ((action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
||
350 action
== SEND_QUOTE
|| action
== SEND_QUOTE_ALL
)
352 : (action
== SEND_TOSRCH
|| action
== SEND_TOPIPE
)
353 ? TD_ICONV
: (action
== SEND_SHOW
? TD_ISPR
: TD_NONE
)),
354 qf
, outrest
, inrest
);
367 _send_onpipe(int signo
)
369 NYD_X
; /* Signal handler */
371 siglongjmp(_send_pipejmp
, 1);
374 static sigjmp_buf __sendp_actjmp
; /* TODO someday.. */
375 static int __sendp_sig
; /* TODO someday.. */
376 static sighandler_type __sendp_opipe
;
378 __sendp_onsig(int sig
) /* TODO someday, we won't need it no more */
380 NYD_X
; /* Signal handler */
382 siglongjmp(__sendp_actjmp
, 1);
386 sendpart(struct message
*zmp
, struct mimepart
*ip
, FILE * volatile obuf
,
387 struct n_ignore
const *doitp
, struct quoteflt
*qf
,
388 enum sendaction
volatile action
, ui64_t
* volatile stats
, int level
)
391 struct mime_handler mh
;
392 struct str outrest
, inrest
;
393 char *line
= NULL
, *cp
, *cp2
, *start
;
394 char const * volatile tmpname
= NULL
;
395 size_t linesize
= 0, linelen
, cnt
;
396 int volatile term_infd
;
398 struct mimepart
*volatile np
;
399 FILE * volatile ibuf
= NULL
, * volatile pbuf
= obuf
,
400 * volatile qbuf
= obuf
, *origobuf
= obuf
;
401 enum conversion
volatile convert
;
402 sighandler_type
volatile oldpipe
= SIG_DFL
;
405 n_UNINIT(term_infd
, 0);
408 quoteflt_reset(qf
, obuf
);
410 if (ip
->m_mimecontent
== MIME_PKCS7
) {
411 if (ip
->m_multipart
&&
412 action
!= SEND_MBOX
&& action
!= SEND_RFC822
&& action
!= SEND_SHOW
)
419 if (!n_ignore_is_ign(doitp
, "status", 6))
421 if (!n_ignore_is_ign(doitp
, "x-status", 8))
426 if ((ibuf
= setinput(&mb
, (struct message
*)ip
, NEED_BODY
)) == NULL
) {
432 if (ip
->m_mimecontent
== MIME_DISCARD
)
435 if (!(ip
->m_flag
& MNOFROM
))
436 while (cnt
&& (c
= getc(ibuf
)) != EOF
) {
441 convert
= (action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
||
442 action
== SEND_QUOTE
|| action
== SEND_QUOTE_ALL
||
443 action
== SEND_TOSRCH
)
444 ? CONV_FROMHDR
: CONV_NONE
;
446 /* Work the headers */
457 while (fgetline(&line
, &linesize
, &cnt
, &linelen
, ibuf
, 0)) {
459 if (line
[0] == '\n') {
460 /* If line is blank, we've reached end of headers, so force out
461 * status: field and note that we are no longer in header fields */
463 statusput(zmp
, obuf
, qf
, stats
);
465 xstatusput(zmp
, obuf
, qf
, stats
);
466 if (doitp
!= n_IGNORE_ALL
)
467 _out("\n", 1, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
, NULL
,NULL
);
472 if ((hps
& HPS_IN_FIELD
) && blankchar(line
[0])) {
473 /* If this line is a continuation (SP / HT) of a previous header
474 * field, determine if the start of the line is a MIME encoded word */
475 if (hps
& HPS_ISENC_2
) {
476 for (cp
= line
; blankchar(*cp
); ++cp
)
478 if (cp
> line
&& linelen
- PTR2SIZE(cp
- line
) > 8 &&
479 cp
[0] == '=' && cp
[1] == '?')
483 /* Pick up the header field if we have one */
484 for (cp
= line
; (c
= *cp
& 0377) && c
!= ':' && !spacechar(c
); ++cp
)
487 while (spacechar(*cp
))
491 n_err(_("Malformed message: headers and body not separated "
492 "(with empty line)\n"));
493 /* Not a header line, force out status: This happens in uucp style
494 * mail where there are no headers at all */
495 if (level
== 0 /*&& lineno == 1*/) {
497 statusput(zmp
, obuf
, qf
, stats
);
499 xstatusput(zmp
, obuf
, qf
, stats
);
501 if (doitp
!= n_IGNORE_ALL
)
502 _out("\n", 1, obuf
, CONV_NONE
,SEND_MBOX
, qf
, stats
, NULL
,NULL
);
506 /* If it is an ignored field and we care about such things, skip it.
507 * Misuse dostat also for another bit xxx use a bitenum + for more */
508 if (ok_blook(keep_content_length
))
511 *cp2
= 0; /* temporarily null terminate */
512 if ((doitp
!= NULL
&&
513 n_ignore_is_ign(doitp
, line
, PTR2SIZE(cp2
- line
))) ||
514 (action
== SEND_MBOX
&& !(dostat
& (1 << 2)) &&
515 (!asccasecmp(line
, "content-length") ||
516 !asccasecmp(line
, "lines"))))
518 else if (!asccasecmp(line
, "status")) {
519 /* If field is "status," go compute and print real Status: field */
521 statusput(zmp
, obuf
, qf
, stats
);
525 } else if (!asccasecmp(line
, "x-status")) {
526 /* If field is "status," go compute and print real Status: field */
528 xstatusput(zmp
, obuf
, qf
, stats
);
534 /* For colourization we need the complete line, so save it */
535 /* XXX This is all temporary (colour belongs into backend), so
536 * XXX use tmpname as a temporary storage in the meanwhile */
538 if (pstate
& PS_COLOUR_ACTIVE
)
539 tmpname
= savestrbuf(line
, PTR2SIZE(cp2
- line
));
547 /* Determine if the end of the line is a MIME encoded word */
548 /* TODO geeeh! all this lengthy stuff that follows is about is dealing
549 * TODO with header follow lines, and it should be up to the backend
550 * TODO what happens and what not, i.e., it doesn't matter whether it's
551 * TODO a MIME-encoded word or not, as long as a single separating space
552 * TODO remains in between lines (the MIME stuff will correctly remove
553 * TODO whitespace in between multiple adjacent encoded words) */
555 if (cnt
&& (c
= getc(ibuf
)) != EOF
) {
557 cp
= line
+ linelen
- 1;
558 if (linelen
> 0 && *cp
== '\n')
560 while (cp
>= line
&& whitechar(*cp
))
562 if (PTR2SIZE(cp
- line
> 8) && cp
[0] == '=' && cp
[-1] == '?')
568 if (!(hps
& HPS_IGNORE
)) {
569 size_t len
= linelen
;
571 if (action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
||
572 action
== SEND_QUOTE
|| action
== SEND_QUOTE_ALL
||
573 action
== SEND_TOSRCH
) {
574 /* Strip blank characters if two MIME-encoded words follow on
575 * continuing lines */
576 if (hps
& HPS_ISENC_1
)
577 while (len
> 0 && blankchar(*start
)) {
581 if (hps
& HPS_ISENC_2
)
582 if (len
> 0 && start
[len
- 1] == '\n')
584 while (len
> 0 && blankchar(start
[len
- 1]))
589 bool_t colour_stripped
= FAL0
;
590 if (tmpname
!= NULL
) {
591 n_colour_put(obuf
, n_COLOUR_ID_VIEW_HEADER
, tmpname
);
592 if (len
> 0 && start
[len
- 1] == '\n') {
593 colour_stripped
= TRU1
;
598 _out(start
, len
, obuf
, convert
, action
, qf
, stats
, NULL
,NULL
);
600 if (tmpname
!= NULL
) {
601 n_colour_reset(obuf
);
621 memset(&mh
, 0, sizeof mh
);
623 switch (ip
->m_mimecontent
) {
627 case SEND_TODISP_ALL
:
630 if (ok_blook(rfc822_body_from_
)) {
631 if (qf
->qf_pfix_len
> 0) {
632 size_t i
= fwrite(qf
->qf_pfix
, sizeof *qf
->qf_pfix
,
633 qf
->qf_pfix_len
, obuf
);
634 if (i
== qf
->qf_pfix_len
&& stats
!= NULL
)
637 put_from_(obuf
, ip
->m_multipart
, stats
);
645 if (ok_blook(rfc822_body_from_
))
646 put_from_(obuf
, ip
->m_multipart
, stats
);
656 case MIME_TEXT_PLAIN
:
659 case SEND_TODISP_ALL
:
662 switch (mime_type_handler(&mh
, ip
, action
)) {
664 _out(mh
.mh_msg
.s
, mh
.mh_msg
.l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
,
666 /* We would print this as plain text, so better force going home */
677 if (action
!= SEND_DECRYPT
)
681 if (action
!= SEND_MBOX
&& action
!= SEND_RFC822
&&
682 action
!= SEND_SHOW
&& ip
->m_multipart
!= NULL
)
688 case SEND_TODISP_ALL
:
691 switch (mime_type_handler(&mh
, ip
, action
)) {
693 _out(mh
.mh_msg
.s
, mh
.mh_msg
.l
, obuf
, CONV_NONE
, SEND_MBOX
, qf
,
695 /* We would print this as plain text, so better force going home */
698 /* FIXME WE NEED TO DO THAT IF WE ARE THE ONLY MAIL
699 * FIXME CONTENT !! */
704 if (level
== 0 && cnt
) {
705 char const *x
= _("[-- Binary content --]\n");
706 _out(x
, strlen(x
), obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
,
722 case MIME_ALTERNATIVE
:
723 if ((action
== SEND_TODISP
|| action
== SEND_QUOTE
) &&
724 !ok_blook(print_alternatives
)) {
725 /* XXX This (a) should not remain (b) should be own fun
726 * TODO (despite the fact that v15 will do this completely differently
727 * TODO by having an action-specific "manager" that will traverse the
728 * TODO parsed MIME tree and decide for each part whether it'll be
729 * TODO displayed or not *before* we walk the tree for doing action */
731 struct mpstack
*outer
;
733 } outermost
, * volatile curr
, * volatile mpsp
;
734 bool_t
volatile neednl
, hadpart
;
735 struct n_sigman smalter
;
737 (curr
= &outermost
)->outer
= NULL
;
739 neednl
= hadpart
= FAL0
;
741 n_SIGMAN_ENTER_SWITCH(&smalter
, n_SIGMAN_ALL
) {
749 for (np
= ip
->m_multipart
;;) {
751 for (; np
!= NULL
; np
= np
->m_nextpart
) {
752 if (action
!= SEND_QUOTE
&& np
->m_ct_type_plain
!= NULL
) {
754 _out("\n", 1, obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
,
756 _print_part_info(obuf
, np
, doitp
, level
, qf
, stats
);
760 switch (np
->m_mimecontent
) {
761 case MIME_ALTERNATIVE
:
767 mpsp
= salloc(sizeof *mpsp
);
769 mpsp
->mp
= np
->m_multipart
;
778 switch (mime_type_handler(&mh
, np
, action
)) {
780 mh
.mh_flags
= MIME_HDL_NULL
;
781 continue; /* break; break; */
783 if (!ok_blook(mime_alternative_favour_rich
)) {/* TODO */
784 struct mimepart
*x
= np
;
786 while ((x
= x
->m_nextpart
) != NULL
) {
787 struct mime_handler mhx
;
789 if (x
->m_mimecontent
== MIME_TEXT_PLAIN
||
790 mime_type_handler(&mhx
, x
, action
) ==
795 continue; /* break; break; */
803 case MIME_TEXT_PLAIN
:
806 if (ok_blook(mime_alternative_favour_rich
)) { /* TODO */
807 struct mimepart
*x
= np
;
809 /* TODO twice TODO, we should dive into /related and
810 * TODO check whether that has rich parts! */
811 while ((x
= x
->m_nextpart
) != NULL
) {
812 struct mime_handler mhx
;
814 switch (mime_type_handler(&mhx
, x
, action
)) {
823 continue; /* break; break; */
827 if (action
== SEND_QUOTE
&& hadpart
) {
828 struct quoteflt
*dummy
= quoteflt_dummy();
829 _out("\n", 1, obuf
, CONV_NONE
, SEND_MBOX
, dummy
, stats
,
831 quoteflt_flush(dummy
);
835 rv
= sendpart(zmp
, np
, obuf
, doitp
, qf
, action
, stats
,
837 quoteflt_reset(qf
, origobuf
);
840 curr
= &outermost
; /* Cause overall loop termination */
849 np
= curr
->mp
->m_nextpart
;
852 n_sigman_leave(&smalter
, n_SIGMAN_VIPSIGS_NTTYOUT
);
863 case SEND_TODISP_ALL
:
871 if ((action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
) &&
872 ip
->m_multipart
!= NULL
&&
873 ip
->m_multipart
->m_mimecontent
== MIME_DISCARD
&&
874 ip
->m_multipart
->m_nextpart
== NULL
) {
875 char const *x
= _("[Missing multipart boundary - use show "
876 "to display the raw message]\n");
877 _out(x
, strlen(x
), obuf
, CONV_NONE
, SEND_MBOX
, qf
, stats
,
881 for (np
= ip
->m_multipart
; np
!= NULL
; np
= np
->m_nextpart
) {
882 bool_t
volatile ispipe
;
884 if (np
->m_mimecontent
== MIME_DISCARD
&& action
!= SEND_DECRYPT
)
890 if (np
->m_partstring
&& !strcmp(np
->m_partstring
, "1"))
893 /* TODO Always open multipart on /dev/null, it's a hack to be
894 * TODO able to dive into that structure, and still better
895 * TODO than asking the user for something stupid.
896 * TODO oh, wait, we did ask for a filename for this MIME mail,
897 * TODO and that outer container is useless anyway ;-P */
898 if (np
->m_multipart
!= NULL
) {
899 if ((obuf
= Fopen("/dev/null", "w")) == NULL
)
901 } else if ((obuf
= newfile(np
, &ispipe
)) == NULL
)
905 if (sigsetjmp(_send_pipejmp
, 1)) {
909 oldpipe
= safe_signal(SIGPIPE
, &_send_onpipe
);
912 case SEND_TODISP_ALL
:
913 if (ip
->m_mimecontent
!= MIME_ALTERNATIVE
&&
914 ip
->m_mimecontent
!= MIME_RELATED
&&
915 ip
->m_mimecontent
!= MIME_DIGEST
&&
916 ip
->m_mimecontent
!= MIME_SIGNED
&&
917 ip
->m_mimecontent
!= MIME_ENCRYPTED
&&
918 ip
->m_mimecontent
!= MIME_MULTI
)
920 _print_part_info(obuf
, np
, doitp
, level
, qf
, stats
);
934 if ((action
== SEND_QUOTE
|| action
== SEND_QUOTE_ALL
) &&
935 np
->m_multipart
== NULL
&& ip
->m_parent
!= NULL
) {
936 struct quoteflt
*dummy
= quoteflt_dummy();
937 _out("\n", 1, obuf
, CONV_NONE
, SEND_MBOX
, dummy
, stats
,
939 quoteflt_flush(dummy
);
941 if (sendpart(zmp
, np
, obuf
, doitp
, qf
, action
, stats
, level
+1) < 0)
943 quoteflt_reset(qf
, origobuf
);
945 if (action
== SEND_QUOTE
) {
946 if (ip
->m_mimecontent
!= MIME_RELATED
)
949 if (action
== SEND_TOFILE
&& obuf
!= origobuf
) {
954 safe_signal(SIGPIPE
, SIG_IGN
);
956 safe_signal(SIGPIPE
, oldpipe
);
969 /* Copy out message body */
970 if (doitp
== n_IGNORE_ALL
&& level
== 0) /* skip final blank line */
972 switch (ip
->m_mime_enc
) {
979 convert
= CONV_FROMQP
;
982 switch (ip
->m_mimecontent
) {
984 case MIME_TEXT_PLAIN
:
986 convert
= CONV_FROMB64_T
;
989 switch (mh
.mh_flags
& MIME_HDL_TYPE_MASK
) {
992 convert
= CONV_FROMB64_T
;
995 convert
= CONV_FROMB64
;
1002 convert
= CONV_NONE
;
1005 /* TODO Unless we have filters, ensure iconvd==-1 so that mime.c:fwrite_td()
1006 * TODO cannot mess things up misusing outrest as line buffer */
1008 if (iconvd
!= (iconv_t
)-1) {
1009 n_iconv_close(iconvd
);
1010 iconvd
= (iconv_t
)-1;
1014 if (action
== SEND_DECRYPT
|| action
== SEND_MBOX
||
1015 action
== SEND_RFC822
|| action
== SEND_SHOW
)
1016 convert
= CONV_NONE
;
1018 else if ((action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
||
1019 action
== SEND_QUOTE
|| action
== SEND_QUOTE_ALL
||
1020 action
== SEND_TOSRCH
) &&
1021 (ip
->m_mimecontent
== MIME_TEXT_PLAIN
||
1022 ip
->m_mimecontent
== MIME_TEXT_HTML
||
1023 ip
->m_mimecontent
== MIME_TEXT
||
1024 (mh
.mh_flags
& MIME_HDL_TYPE_MASK
) == MIME_HDL_TEXT
||
1025 (mh
.mh_flags
& MIME_HDL_TYPE_MASK
) == MIME_HDL_PTF
)) {
1028 tcs
= ok_vlook(ttycharset
);
1029 if (asccasecmp(tcs
, ip
->m_charset
) &&
1030 asccasecmp(ok_vlook(charset_7bit
), ip
->m_charset
)) {
1031 iconvd
= n_iconv_open(tcs
, ip
->m_charset
);
1032 if (iconvd
== (iconv_t
)-1 && errno
== EINVAL
) {
1033 n_err(_("Cannot convert from %s to %s\n"), ip
->m_charset
, tcs
);
1034 /*rv = 1; goto jleave;*/
1040 switch (mh
.mh_flags
& MIME_HDL_TYPE_MASK
) {
1046 term_infd
= COMMAND_FD_PASS
;
1047 if (mh
.mh_flags
& (MIME_HDL_TMPF
| MIME_HDL_NEEDSTERM
)) {
1050 of
= OF_RDWR
| OF_REGISTER
;
1051 if (!(mh
.mh_flags
& MIME_HDL_TMPF
)) {
1053 mh
.mh_flags
|= MIME_HDL_TMPF_FILL
;
1055 } else if (mh
.mh_flags
& MIME_HDL_TMPF_UNLINK
)
1056 of
|= OF_REGISTER_UNLINK
;
1058 if ((pbuf
= Ftmp((mh
.mh_flags
& MIME_HDL_TMPF
? &cp
: NULL
),
1059 (mh
.mh_flags
& MIME_HDL_TMPF_FILL
? "mimehdlfill" : "mimehdl"),
1063 if (mh
.mh_flags
& MIME_HDL_TMPF
) {
1064 tmpname
= savestr(cp
);
1068 if (mh
.mh_flags
& MIME_HDL_TMPF_FILL
) {
1070 term_infd
= fileno(pbuf
);
1076 pbuf
= _pipefile(&mh
, ip
, n_UNVOLATILE(&qbuf
), tmpname
, term_infd
);
1082 } else if ((mh
.mh_flags
& MIME_HDL_NEEDSTERM
) && pbuf
== (FILE*)-1) {
1087 action
= SEND_TOPIPE
;
1089 oldpipe
= safe_signal(SIGPIPE
, &_send_onpipe
);
1090 if (sigsetjmp(_send_pipejmp
, 1))
1096 mh
.mh_flags
= MIME_HDL_NULL
;
1103 bool_t
volatile eof
;
1104 ui32_t save_qf_pfix_len
= qf
->qf_pfix_len
;
1105 ui64_t
*save_stats
= stats
;
1107 if (pbuf
!= origobuf
) {
1108 qf
->qf_pfix_len
= 0; /* XXX legacy (remove filter instead) */
1112 outrest
.s
= inrest
.s
= NULL
;
1113 outrest
.l
= inrest
.l
= 0;
1117 __sendp_opipe
= safe_signal(SIGPIPE
, &__sendp_onsig
);
1118 if (sigsetjmp(__sendp_actjmp
, 1)) {
1119 if (outrest
.s
!= NULL
)
1121 if (inrest
.s
!= NULL
)
1125 if (iconvd
!= (iconv_t
)-1)
1126 n_iconv_close(iconvd
);
1128 safe_signal(SIGPIPE
, __sendp_opipe
);
1129 n_raise(__sendp_sig
);
1133 quoteflt_reset(qf
, pbuf
);
1134 while (!eof
&& fgetline(&line
, &linesize
, &cnt
, &linelen
, ibuf
, 0)) {
1136 if (_out(line
, linelen
, pbuf
, convert
, action
, qf
, stats
, &outrest
,
1137 (action
& _TD_EOF
? NULL
: &inrest
)) < 0 || ferror(pbuf
)) {
1138 rv
= -1; /* XXX Should bail away?! */
1142 if(eof
<= FAL0
&& rv
>= 0 && (outrest
.l
!= 0 || inrest
.l
!= 0)){
1144 if(eof
|| inrest
.l
== 0)
1146 eof
= eof
? TRU1
: TRUM1
;
1151 /* TODO HACK: when sending to the display we yet get fooled if a message
1152 * TODO doesn't end in a newline, because of our input/output 1:1.
1153 * TODO This should be handled automatically by a display filter, then */
1154 if(rv
>= 0 && !qf
->qf_nl_last
&&
1155 (action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
))
1156 rv
= quoteflt_push(qf
, "\n", 1);
1160 if (rv
>= 0 && (mh
.mh_flags
& MIME_HDL_TMPF_FILL
)) {
1161 mh
.mh_flags
&= ~MIME_HDL_TMPF_FILL
;
1163 really_rewind(pbuf
);
1164 /* Don't Fclose() the Ftmp() thing due to OF_REGISTER_UNLINK++ */
1165 goto jpipe_for_real
;
1169 safe_signal(SIGPIPE
, __sendp_opipe
);
1171 if (outrest
.s
!= NULL
)
1173 if (inrest
.s
!= NULL
)
1176 if (pbuf
!= origobuf
) {
1177 qf
->qf_pfix_len
= save_qf_pfix_len
;
1186 safe_signal(SIGPIPE
, SIG_IGN
);
1187 Pclose(pbuf
, !(mh
.mh_flags
& MIME_HDL_ASYNC
));
1188 safe_signal(SIGPIPE
, oldpipe
);
1189 if (rv
>= 0 && qbuf
!= NULL
&& qbuf
!= obuf
)
1190 pipecpy(qbuf
, obuf
, origobuf
, qf
, stats
);
1193 if (iconvd
!= (iconv_t
)-1)
1194 n_iconv_close(iconvd
);
1202 newfile(struct mimepart
*ip
, bool_t
volatile *ispipe
)
1212 if (f
!= NULL
&& f
!= (char*)-1) {
1215 makeprint(&in
, &out
);
1216 out
.l
= delctrl(out
.s
, out
.l
);
1217 f
= savestrbuf(out
.s
, out
.l
);
1221 /* In interactive mode, let user perform all kind of expansions as desired,
1222 * and offer |SHELL-SPEC pipe targets, too */
1223 if (options
& OPT_INTERACTIVE
) {
1225 struct n_string shou
, *shoup
;
1228 shoup
= n_string_creat_auto(&shou
);
1230 /* TODO Generic function which asks for filename.
1231 * TODO If the current part is the first textpart the target
1232 * TODO is implicit from outer `write' etc! */
1233 /* I18N: Filename input prompt with file type indication */
1234 str_concat_csvl(&prompt
, _("Enter filename for part "),
1235 (ip
->m_partstring
!= NULL
) ? ip
->m_partstring
: _("?"),
1236 _(" ("), ip
->m_ct_type_plain
, _("): "), NULL
);
1238 f2
= n_lex_input_cp(n_LEXINPUT_CTX_DEFAULT
| n_LEXINPUT_HIST_ADD
,
1239 prompt
.s
, ((f
!= (char*)-1 && f
!= NULL
)
1240 ? n_shexp_quote_cp(f
, FAL0
) : NULL
));
1242 in
.s
= n_UNCONST(f2
);
1244 if((n_shexp_parse_token(shoup
, &in
, n_SHEXP_PARSE_TRUNC
|
1245 n_SHEXP_PARSE_TRIMSPACE
| n_SHEXP_PARSE_LOG
|
1246 n_SHEXP_PARSE_IGNORE_EMPTY
) &
1247 (n_SHEXP_STATE_STOP
| n_SHEXP_STATE_OUTPUT
|
1248 n_SHEXP_STATE_ERR_MASK
)
1249 ) != (n_SHEXP_STATE_STOP
| n_SHEXP_STATE_OUTPUT
))
1251 f2
= n_string_cp(shoup
);
1253 if (f2
== NULL
|| *f2
== '\0') {
1254 if (options
& OPT_D_V
)
1255 n_err(_("... skipping this\n"));
1256 n_string_gut(shoup
);
1262 /* Pipes are expanded by the shell */
1264 else if ((f3
= fexpand(f2
, FEXP_LOCAL
| FEXP_NVAR
)) == NULL
)
1265 /* (Error message written by fexpand()) */
1270 n_string_gut(shoup
);
1273 if (f
== NULL
|| f
== (char*)-1 || *f
== '\0')
1275 else if (options
& OPT_INTERACTIVE
) {
1277 fp
= Popen(&f
[1], "w", ok_vlook(SHELL
), NULL
, 1);
1278 if (!(*ispipe
= (fp
!= NULL
)))
1280 } else if ((fp
= Fopen(f
, "w")) == NULL
)
1281 n_err(_("Cannot open %s\n"), n_shexp_quote_cp(f
, FAL0
));
1283 /* Be very picky in non-interactive mode: actively disallow pipes,
1284 * prevent directory separators, and any filename member that would
1285 * become expanded by the shell if the name would be echo(1)ed */
1286 if(anyof(f
, "/" n_SHEXP_MAGIC_PATH_CHARS
)){
1289 for(out
.s
= salloc((strlen(f
) * 3) +1), out
.l
= 0; (c
= *f
++) != '\0';)
1290 if(strchr("/" n_SHEXP_MAGIC_PATH_CHARS
, c
)){
1291 out
.s
[out
.l
++] = '%';
1292 n_c_to_hex_base16(&out
.s
[out
.l
], c
);
1296 out
.s
[out
.l
] = '\0';
1300 /* Avoid overwriting of existing files */
1301 while((fp
= Fopen(f
, "wx")) == NULL
){
1304 if((e
= errno
) != EEXIST
){
1305 n_err(_("Cannot open %s: %s\n"),
1306 n_shexp_quote_cp(f
, FAL0
), strerror(e
));
1310 if(ip
->m_partstring
!= NULL
)
1311 f
= savecatsep(f
, '#', ip
->m_partstring
);
1313 f
= savecat(f
, "#.");
1322 pipecpy(FILE *pipebuf
, FILE *outbuf
, FILE *origobuf
, struct quoteflt
*qf
,
1325 char *line
= NULL
; /* TODO line pool */
1326 size_t linesize
= 0, linelen
, cnt
;
1332 cnt
= (size_t)fsize(pipebuf
);
1335 quoteflt_reset(qf
, outbuf
);
1336 while (fgetline(&line
, &linesize
, &cnt
, &linelen
, pipebuf
, 0) != NULL
) {
1337 if ((sz
= quoteflt_push(qf
, line
, linelen
)) < 0)
1341 if ((sz
= quoteflt_flush(qf
)) > 0)
1346 if (all_sz
> 0 && outbuf
== origobuf
&& stats
!= NULL
)
1353 statusput(const struct message
*mp
, FILE *obuf
, struct quoteflt
*qf
,
1356 char statout
[3], *cp
= statout
;
1359 if (mp
->m_flag
& MREAD
)
1361 if (!(mp
->m_flag
& MNEW
))
1365 int i
= fprintf(obuf
, "%.*sStatus: %s\n", (int)qf
->qf_pfix_len
,
1366 (qf
->qf_pfix_len
> 0 ? qf
->qf_pfix
: 0), statout
);
1367 if (i
> 0 && stats
!= NULL
)
1374 xstatusput(const struct message
*mp
, FILE *obuf
, struct quoteflt
*qf
,
1378 char *xp
= xstatout
;
1381 if (mp
->m_flag
& MFLAGGED
)
1383 if (mp
->m_flag
& MANSWERED
)
1385 if (mp
->m_flag
& MDRAFTED
)
1389 int i
= fprintf(obuf
, "%.*sX-Status: %s\n", (int)qf
->qf_pfix_len
,
1390 (qf
->qf_pfix_len
> 0 ? qf
->qf_pfix
: 0), xstatout
);
1391 if (i
> 0 && stats
!= NULL
)
1398 put_from_(FILE *fp
, struct mimepart
*ip
, ui64_t
*stats
)
1400 char const *froma
, *date
, *nl
;
1404 if (ip
!= NULL
&& ip
->m_from
!= NULL
) {
1406 date
= fakedate(ip
->m_time
);
1409 froma
= ok_vlook(LOGNAME
);
1410 date
= time_current
.tc_ctime
;
1414 n_COLOUR( n_colour_put(fp
, n_COLOUR_ID_VIEW_FROM_
, NULL
); )
1415 i
= fprintf(fp
, "From %s %s%s", froma
, date
, nl
);
1416 n_COLOUR( n_colour_reset(fp
); )
1417 if (i
> 0 && stats
!= NULL
)
1423 sendmp(struct message
*mp
, FILE *obuf
, struct n_ignore
const *doitp
,
1424 char const *prefix
, enum sendaction action
, ui64_t
*stats
)
1429 enum mime_parse_flags mpf
;
1430 struct mimepart
*ip
;
1434 time_current_update(&time_current
, TRU1
);
1436 if (mp
== dot
&& action
!= SEND_TOSRCH
)
1437 pstate
|= PS_DID_PRINT_DOT
;
1440 quoteflt_init(&qf
, prefix
);
1442 /* First line is the From_ line, so no headers there to worry about */
1443 if ((ibuf
= setinput(&mb
, mp
, NEED_BODY
)) == NULL
)
1450 char const *cpre
= n_empty
, *csuf
= n_empty
;
1452 struct n_colour_pen
*cpen
= n_colour_pen_create(n_COLOUR_ID_VIEW_FROM_
,NULL
);
1453 struct str
const *sp
= n_colour_pen_to_str(cpen
);
1457 sp
= n_colour_reset_to_str();
1463 nozap
= (doitp
!= n_IGNORE_ALL
&& doitp
!= n_IGNORE_FWD
&&
1464 action
!= SEND_RFC822
&&
1465 !n_ignore_is_ign(doitp
, "from_", sizeof("from_") -1));
1466 if (mp
->m_flag
& MNOFROM
) {
1468 sz
= fprintf(obuf
, "%s%.*sFrom %s %s%s\n",
1469 cpre
, (int)qf
.qf_pfix_len
,
1470 (qf
.qf_pfix_len
!= 0 ? qf
.qf_pfix
: n_empty
), fakefrom(mp
),
1471 fakedate(mp
->m_time
), csuf
);
1473 if (qf
.qf_pfix_len
> 0) {
1474 i
= fwrite(qf
.qf_pfix
, sizeof *qf
.qf_pfix
, qf
.qf_pfix_len
, obuf
);
1475 if (i
!= qf
.qf_pfix_len
)
1482 cpre
= (char const*)0x1;
1486 while (cnt
> 0 && (c
= getc(ibuf
)) != EOF
) {
1488 if (c
== '\n' && csuf
!= NULL
) {
1489 cpre
= (char const*)0x1;
1501 if (csuf
!= NULL
&& cpre
!= (char const*)0x1)
1505 while (cnt
> 0 && (c
= getc(ibuf
)) != EOF
) {
1512 if (sz
> 0 && stats
!= NULL
)
1515 mpf
= MIME_PARSE_NONE
;
1516 if (action
!= SEND_MBOX
&& action
!= SEND_RFC822
&& action
!= SEND_SHOW
)
1517 mpf
|= MIME_PARSE_PARTS
| MIME_PARSE_DECRYPT
;
1518 if ((ip
= mime_parse_msg(mp
, mpf
)) == NULL
)
1521 rv
= sendpart(mp
, ip
, obuf
, doitp
, &qf
, action
, stats
, 0);
1523 quoteflt_destroy(&qf
);