nail.1: last fixes
[s-mailx.git] / send.c
blob66f0053b9d5b7801abbf59f9af06944deabc0578
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>.
6 */
7 /*
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
13 * are met:
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
33 * SUCH DAMAGE.
35 #undef n_FILE
36 #define n_FILE send
38 #ifndef HAVE_AMALGAMATION
39 # include "nail.h"
40 #endif
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 n_PIPEENV_* 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,
59 struct str *inrest);
61 /* SIGPIPE handler */
62 static void _send_onpipe(int signo);
64 /* Send one part */
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 char **linedat, size_t *linesize,
69 ui64_t *stats, int level);
71 /* Get a file for an attachment */
72 static FILE * newfile(struct mimepart *ip, bool_t volatile *ispipe);
74 static void pipecpy(FILE *pipebuf, FILE *outbuf, FILE *origobuf,
75 struct quoteflt *qf, ui64_t *stats);
77 /* Output a reasonable looking status field */
78 static void statusput(const struct message *mp, FILE *obuf,
79 struct quoteflt *qf, ui64_t *stats);
80 static void xstatusput(const struct message *mp, FILE *obuf,
81 struct quoteflt *qf, ui64_t *stats);
83 static void put_from_(FILE *fp, struct mimepart *ip, ui64_t *stats);
85 static void
86 _print_part_info(FILE *obuf, struct mimepart const *mpp, /* TODO strtofmt.. */
87 struct n_ignore const *doitp, int level, struct quoteflt *qf, ui64_t *stats)
89 char buf[64];
90 struct str ti, to;
91 bool_t want_ct, needsep;
92 struct str const *cpre, *csuf;
93 char const *cp;
94 NYD2_ENTER;
96 cpre = csuf = NULL;
97 #ifdef HAVE_COLOUR
98 if(n_COLOUR_IS_ACTIVE()){
99 struct n_colour_pen *cpen;
101 cpen = n_colour_pen_create(n_COLOUR_ID_VIEW_PARTINFO, NULL);
102 if((cpre = n_colour_pen_to_str(cpen)) != NULL)
103 csuf = n_colour_reset_to_str();
105 #endif
107 /* Take care of "99.99", i.e., 5 */
108 if ((cp = mpp->m_partstring) == NULL || cp[0] == '\0')
109 cp = n_qm;
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 */
115 if (cpre != NULL)
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 needsep = FAL0;
126 if((cp = mpp->m_ct_type_usr_ovwr) != NULL){
127 _out("+", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
128 want_ct = TRU1;
129 }else if((want_ct = n_ignore_is_ign(doitp,
130 "content-type", sizeof("content-type") -1)))
131 cp = mpp->m_ct_type_plain;
132 if (want_ct &&
133 (to.l = strlen(cp)) > 30 && is_asccaseprefix("application/", cp)) {
134 size_t const al = sizeof("appl../") -1, fl = sizeof("application/") -1;
135 size_t i = to.l - fl;
136 char *x = salloc(al + i +1);
138 memcpy(x, "appl../", al);
139 memcpy(x + al, cp + fl, i +1);
140 cp = x;
141 to.l = al + i;
143 if(cp != NULL){
144 _out(cp, to.l, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
145 needsep = TRU1;
148 if (mpp->m_multipart == NULL/* TODO */ && (cp = mpp->m_ct_enc) != NULL) {
149 if(needsep)
150 _out(", ", 2, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
151 if (to.l > 25 && !asccasecmp(cp, "quoted-printable"))
152 cp = "qu.-pr.";
153 _out(cp, strlen(cp), obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
154 needsep = TRU1;
157 if (want_ct && mpp->m_multipart == NULL/* TODO */ &&
158 (cp = mpp->m_charset) != NULL) {
159 if(needsep)
160 _out(", ", 2, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
161 _out(cp, strlen(cp), obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
164 _out(" --]", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
165 if (csuf != NULL)
166 _out(csuf->s, csuf->l, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
167 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
169 /* */
170 if (mpp->m_content_info & CI_MIME_ERRORS) {
171 if (cpre != NULL)
172 _out(cpre->s, cpre->l, obuf, CONV_NONE, SEND_MBOX, qf, stats,
173 NULL, NULL);
174 _out("[-- ", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
176 ti.l = strlen(ti.s = n_UNCONST(_("Defective MIME structure")));
177 makeprint(&ti, &to);
178 to.l = delctrl(to.s, to.l);
179 _out(to.s, to.l, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
180 free(to.s);
182 _out(" --]", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
183 if (csuf != NULL)
184 _out(csuf->s, csuf->l, obuf, CONV_NONE, SEND_MBOX, qf, stats,
185 NULL, NULL);
186 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
189 /* Content-Description */
190 if (n_ignore_is_ign(doitp, "content-description", 19) &&
191 (cp = mpp->m_content_description) != NULL && *cp != '\0') {
192 if (cpre != NULL)
193 _out(cpre->s, cpre->l, obuf, CONV_NONE, SEND_MBOX, qf, stats,
194 NULL, NULL);
195 _out("[-- ", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
197 ti.l = strlen(ti.s = n_UNCONST(mpp->m_content_description));
198 mime_fromhdr(&ti, &to, TD_ISPR | TD_ICONV);
199 _out(to.s, to.l, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
200 free(to.s);
202 _out(" --]", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
203 if (csuf != NULL)
204 _out(csuf->s, csuf->l, obuf, CONV_NONE, SEND_MBOX, qf, stats,
205 NULL, NULL);
206 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
209 /* Filename */
210 if (n_ignore_is_ign(doitp, "content-disposition", 19) &&
211 mpp->m_filename != NULL && *mpp->m_filename != '\0') {
212 if (cpre != NULL)
213 _out(cpre->s, cpre->l, obuf, CONV_NONE, SEND_MBOX, qf, stats,
214 NULL, NULL);
215 _out("[-- ", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
217 ti.l = strlen(ti.s = mpp->m_filename);
218 makeprint(&ti, &to);
219 to.l = delctrl(to.s, to.l);
220 _out(to.s, to.l, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
221 free(to.s);
223 _out(" --]", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
224 if (csuf != NULL)
225 _out(csuf->s, csuf->l, obuf, CONV_NONE, SEND_MBOX, qf, stats,
226 NULL, NULL);
227 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
229 NYD2_LEAVE;
232 static FILE *
233 _pipefile(struct mime_handler *mhp, struct mimepart const *mpp, FILE **qbuf,
234 char const *tmpname, int term_infd)
236 struct str s;
237 char const *env_addon[8 +8/*v15*/], *cp, *sh;
238 FILE *rbuf;
239 NYD_ENTER;
241 rbuf = *qbuf;
243 if (mhp->mh_flags & MIME_HDL_ISQUOTE) {
244 if ((*qbuf = Ftmp(NULL, "sendp", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
245 NULL) {
246 n_perr(_("tmpfile"), 0);
247 *qbuf = rbuf;
251 if ((mhp->mh_flags & MIME_HDL_TYPE_MASK) == MIME_HDL_PTF) {
252 union {int (*ptf)(void); char const *sh;} u;
254 fflush(*qbuf);
255 if (*qbuf != n_stdout) /* xxx never? v15: it'll be a filter anyway */
256 fflush(n_stdout);
258 u.ptf = mhp->mh_ptf;
259 if((rbuf = Popen((char*)-1, "W", u.sh, NULL, fileno(*qbuf))) == NULL)
260 goto jerror;
261 goto jleave;
264 /* MAILX_FILENAME */
265 if (mpp == NULL || (cp = mpp->m_filename) == NULL)
266 cp = n_empty;
267 env_addon[0] = str_concat_csvl(&s, n_PIPEENV_FILENAME, "=", cp, NULL)->s;
268 env_addon[1] = str_concat_csvl(&s, "NAIL_FILENAME", "=", cp, NULL)->s;/* v15 */
270 /* MAILX_FILENAME_GENERATED *//* TODO pathconf NAME_MAX; but user can create
271 * TODO a file wherever he wants! *Do* create a zero-size temporary file
272 * TODO and give *that* path as MAILX_FILENAME_TEMPORARY, clean it up once
273 * TODO the pipe returns? Like this we *can* verify path/name issues! */
274 cp = n_random_create_cp(n_MIN(NAME_MAX / 4, 16), NULL);
275 env_addon[2] = str_concat_csvl(&s, n_PIPEENV_FILENAME_GENERATED, "=", cp,
276 NULL)->s;
277 env_addon[3] = str_concat_csvl(&s, "NAIL_FILENAME_GENERATED", "=", cp,/* v15 */
278 NULL)->s;
280 /* MAILX_CONTENT{,_EVIDENCE} */
281 if (mpp == NULL || (cp = mpp->m_ct_type_plain) == NULL)
282 cp = n_empty;
283 env_addon[4] = str_concat_csvl(&s, n_PIPEENV_CONTENT, "=", cp, NULL)->s;
284 env_addon[5] = str_concat_csvl(&s, "NAIL_CONTENT", "=", cp, NULL)->s;/* v15 */
286 if (mpp != NULL && mpp->m_ct_type_usr_ovwr != NULL)
287 cp = mpp->m_ct_type_usr_ovwr;
288 env_addon[6] = str_concat_csvl(&s, n_PIPEENV_CONTENT_EVIDENCE, "=", cp,
289 NULL)->s;
290 env_addon[7] = str_concat_csvl(&s, "NAIL_CONTENT_EVIDENCE", "=", cp,/* v15 */
291 NULL)->s;
293 env_addon[8] = NULL;
295 /* MAILX_FILENAME_TEMPORARY? */
296 if (tmpname != NULL) {
297 env_addon[8] = str_concat_csvl(&s,
298 n_PIPEENV_FILENAME_TEMPORARY, "=", tmpname, NULL)->s;
299 env_addon[9] = str_concat_csvl(&s,
300 "NAIL_FILENAME_TEMPORARY", "=", tmpname, NULL)->s;/* v15 */
301 env_addon[10] = NULL;
304 sh = ok_vlook(SHELL);
306 if (mhp->mh_flags & MIME_HDL_NEEDSTERM) {
307 sigset_t nset;
308 int pid;
310 sigemptyset(&nset);
311 pid = n_child_run(sh, &nset, term_infd, n_CHILD_FD_PASS, "-c",
312 mhp->mh_shell_cmd, NULL, env_addon, NULL);
313 rbuf = (pid < 0) ? NULL : (FILE*)-1;
314 } else {
315 rbuf = Popen(mhp->mh_shell_cmd, "W", sh, env_addon,
316 (mhp->mh_flags & MIME_HDL_ASYNC ? -1 : fileno(*qbuf)));
317 jerror:
318 if (rbuf == NULL)
319 n_err(_("Cannot run MIME type handler: %s: %s\n"),
320 mhp->mh_msg, n_err_to_doc(n_err_no));
321 else {
322 fflush(*qbuf);
323 if (*qbuf != n_stdout)
324 fflush(n_stdout);
327 jleave:
328 NYD_LEAVE;
329 return rbuf;
332 SINLINE ssize_t
333 _out(char const *buf, size_t len, FILE *fp, enum conversion convert, enum
334 sendaction action, struct quoteflt *qf, ui64_t *stats, struct str *outrest,
335 struct str *inrest)
337 ssize_t sz = 0, n;
338 int flags;
339 NYD_ENTER;
341 /* TODO We should not need is_head() here, i think in v15 the actual Mailbox
342 * TODO subclass should detect such From_ cases and either reencode the part
343 * TODO in question, or perform From_ quoting as necessary!?!?!? How?!? */
344 /* C99 */{
345 bool_t from_;
347 if((action == SEND_MBOX || action == SEND_DECRYPT) &&
348 (from_ = is_head(buf, len, TRU1))){
349 if(from_ != TRUM1 || ok_blook(mbox_rfc4155)){
350 putc('>', fp);
351 ++sz;
356 flags = ((int)action & _TD_EOF);
357 action &= ~_TD_EOF;
358 n = mime_write(buf, len, fp,
359 action == SEND_MBOX ? CONV_NONE : convert,
360 flags | ((action == SEND_TODISP || action == SEND_TODISP_ALL ||
361 action == SEND_TODISP_PARTS ||
362 action == SEND_QUOTE || action == SEND_QUOTE_ALL)
363 ? TD_ISPR | TD_ICONV
364 : (action == SEND_TOSRCH || action == SEND_TOPIPE ||
365 action == SEND_TOFILE)
366 ? TD_ICONV : (action == SEND_SHOW ? TD_ISPR : TD_NONE)),
367 qf, outrest, inrest);
368 if (n < 0)
369 sz = n;
370 else if (n > 0) {
371 sz += n;
372 if (stats != NULL)
373 *stats += sz;
375 NYD_LEAVE;
376 return sz;
379 static void
380 _send_onpipe(int signo)
382 NYD_X; /* Signal handler */
383 n_UNUSED(signo);
384 siglongjmp(_send_pipejmp, 1);
387 static sigjmp_buf __sendp_actjmp; /* TODO someday.. */
388 static int __sendp_sig; /* TODO someday.. */
389 static sighandler_type __sendp_opipe;
390 static void
391 __sendp_onsig(int sig) /* TODO someday, we won't need it no more */
393 NYD_X; /* Signal handler */
394 __sendp_sig = sig;
395 siglongjmp(__sendp_actjmp, 1);
398 static int
399 sendpart(struct message *zmp, struct mimepart *ip, FILE * volatile obuf,
400 struct n_ignore const *doitp, struct quoteflt *qf,
401 enum sendaction volatile action,
402 char **linedat, size_t *linesize, ui64_t * volatile stats, int level)
404 int volatile rv = 0;
405 struct mime_handler mh;
406 struct str outrest, inrest;
407 char *cp;
408 char const * volatile tmpname = NULL;
409 size_t linelen, cnt;
410 int volatile dostat, term_infd;
411 int c;
412 struct mimepart * volatile np;
413 FILE * volatile ibuf = NULL, * volatile pbuf = obuf,
414 * volatile qbuf = obuf, *origobuf = obuf;
415 enum conversion volatile convert;
416 sighandler_type volatile oldpipe = SIG_DFL;
417 NYD_ENTER;
419 n_UNINIT(term_infd, 0);
420 n_UNINIT(cnt, 0);
422 quoteflt_reset(qf, obuf);
424 #if 0 /* TODO PART_INFO should be displayed here!! search PART_INFO */
425 if(ip->m_mimecontent != MIME_DISCARD && level > 0)
426 _print_part_info(obuf, ip, doitp, level, qf, stats);
427 #endif
429 if (ip->m_mimecontent == MIME_PKCS7) {
430 if (ip->m_multipart &&
431 action != SEND_MBOX && action != SEND_RFC822 && action != SEND_SHOW)
432 goto jheaders_skip;
435 dostat = 0;
436 if (level == 0 && action != SEND_TODISP_PARTS) {
437 if (doitp != NULL) {
438 if (!n_ignore_is_ign(doitp, "status", 6))
439 dostat |= 1;
440 if (!n_ignore_is_ign(doitp, "x-status", 8))
441 dostat |= 2;
442 } else
443 dostat = 3;
446 if ((ibuf = setinput(&mb, (struct message*)ip, NEED_BODY)) == NULL) {
447 rv = -1;
448 goto jleave;
451 if(action == SEND_TODISP || action == SEND_TODISP_ALL ||
452 action == SEND_TODISP_PARTS ||
453 action == SEND_QUOTE || action == SEND_QUOTE_ALL ||
454 action == SEND_TOSRCH)
455 dostat |= 4;
457 cnt = ip->m_size;
459 if (ip->m_mimecontent == MIME_DISCARD)
460 goto jheaders_skip;
462 if (!(ip->m_flag & MNOFROM))
463 while (cnt && (c = getc(ibuf)) != EOF) {
464 cnt--;
465 if (c == '\n')
466 break;
468 convert = (dostat & 4) ? CONV_FROMHDR : CONV_NONE;
470 /* Work the headers */
471 /* C99 */{
472 struct n_string hl, *hlp;
473 size_t lineno = 0;
474 bool_t hstop/*see below, hany*/;
476 hlp = n_string_creat_auto(&hl); /* TODO pool [or, v15: filter!] */
477 /* Reserve three lines, still not enough for references and DKIM etc. */
478 hlp = n_string_reserve(hlp, n_MAX(MIME_LINELEN, MIME_LINELEN_RFC2047) * 3);
480 for(hstop = /*see below hany =*/ FAL0; !hstop;){
481 size_t lcnt;
483 lcnt = cnt;
484 if(fgetline(linedat, linesize, &cnt, &linelen, ibuf, 0) == NULL)
485 break;
486 ++lineno;
487 if (linelen == 0 || (cp = *linedat)[0] == '\n')
488 /* If line is blank, we've reached end of headers */
489 break;
490 if(cp[linelen - 1] == '\n'){
491 cp[--linelen] = '\0';
492 if(linelen == 0)
493 break;
496 /* Are we in a header? */
497 if(hlp->s_len > 0){
498 if(!blankchar(*cp)){
499 fseek(ibuf, -(off_t)(lcnt - cnt), SEEK_CUR);
500 cnt = lcnt;
501 goto jhdrput;
503 goto jhdrpush;
504 }else{
505 /* Pick up the header field if we have one */
506 while((c = *cp) != ':' && !spacechar(c) && c != '\0')
507 ++cp;
508 for(;;){
509 if(!spacechar(c) || c == '\0')
510 break;
511 c = *++cp;
513 if(c != ':'){
514 /* That won't work with MIME when saving etc., before v15 */
515 if (lineno != 1)
516 /* XXX This disturbs, and may happen multiple times, and we
517 * XXX cannot heal it for multipart except for display <v15 */
518 n_err(_("Malformed message: headers and body not separated "
519 "(with empty line)\n"));
520 if(level != 0)
521 dostat &= ~(1 | 2);
522 fseek(ibuf, -(off_t)(lcnt - cnt), SEEK_CUR);
523 cnt = lcnt;
524 break;
527 cp = *linedat;
528 jhdrpush:
529 if(!(dostat & 4)){
530 hlp = n_string_push_buf(hlp, cp, (ui32_t)linelen);
531 hlp = n_string_push_c(hlp, '\n');
532 }else{
533 bool_t lblank, isblank;
535 for(lblank = FAL0, lcnt = 0; lcnt < linelen; ++cp, ++lcnt){
536 char c8;
538 c8 = *cp;
539 if(!(isblank = blankchar(c8)) || !lblank){
540 if((lblank = isblank))
541 c8 = ' ';
542 hlp = n_string_push_c(hlp, c8);
546 continue;
549 jhdrput:
550 /* If it is an ignored header, skip it */
551 *(cp = memchr(hlp->s_dat, ':', hlp->s_len)) = '\0';
552 /* C99 */{
553 size_t i;
555 i = PTR2SIZE(cp - hlp->s_dat);
556 if((doitp != NULL && n_ignore_is_ign(doitp, hlp->s_dat, i)) ||
557 !asccasecmp(hlp->s_dat, "status") ||
558 !asccasecmp(hlp->s_dat, "x-status") ||
559 (action == SEND_MBOX &&
560 (!asccasecmp(hlp->s_dat, "content-length") ||
561 !asccasecmp(hlp->s_dat, "lines")) &&
562 !ok_blook(keep_content_length)))
563 goto jhdrtrunc;
566 /* Dump it */
567 n_COLOUR(
568 if(n_COLOUR_IS_ACTIVE())
569 n_colour_put(n_COLOUR_ID_VIEW_HEADER, hlp->s_dat);
571 *cp = ':';
572 _out(hlp->s_dat, hlp->s_len, obuf, convert, action, qf, stats, NULL,NULL);
573 n_COLOUR(
574 if(n_COLOUR_IS_ACTIVE())
575 n_colour_reset();
577 if(dostat & 4)
578 putc('\n', obuf);
579 /*see below hany = TRU1;*/
581 jhdrtrunc:
582 hlp = n_string_trunc(hlp, 0);
584 hstop = TRU1;
585 if(hlp->s_len > 0)
586 goto jhdrput;
588 /* We've reached end of headers, so eventually force out status: field and
589 * note that we are no longer in header fields */
590 if(dostat & 1){
591 statusput(zmp, obuf, qf, stats);
592 /*see below hany = TRU1;*/
594 if(dostat & 2){
595 xstatusput(zmp, obuf, qf, stats);
596 /*see below hany = TRU1;*/
598 if(/* TODO PART_INFO hany && */ doitp != n_IGNORE_ALL)
599 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
600 } /* C99 */
602 quoteflt_flush(qf);
604 if(ferror(obuf)){
605 rv = -1;
606 goto jleave;
609 jheaders_skip:
610 memset(&mh, 0, sizeof mh);
612 switch (ip->m_mimecontent) {
613 case MIME_822:
614 switch (action) {
615 case SEND_TODISP_PARTS:
616 goto jleave;
617 case SEND_TODISP:
618 case SEND_TODISP_ALL:
619 case SEND_QUOTE:
620 case SEND_QUOTE_ALL:
621 if (ok_blook(rfc822_body_from_)) {
622 if (qf->qf_pfix_len > 0) {
623 size_t i = fwrite(qf->qf_pfix, sizeof *qf->qf_pfix,
624 qf->qf_pfix_len, obuf);
625 if (i == qf->qf_pfix_len && stats != NULL)
626 *stats += i;
628 put_from_(obuf, ip->m_multipart, stats);
630 /* FALLTHRU */
631 case SEND_TOSRCH:
632 case SEND_DECRYPT:
633 goto jmulti;
634 case SEND_TOFILE:
635 case SEND_TOPIPE:
636 if (ok_blook(rfc822_body_from_))
637 put_from_(obuf, ip->m_multipart, stats);
638 /* FALLTHRU */
639 case SEND_MBOX:
640 case SEND_RFC822:
641 case SEND_SHOW:
642 break;
644 break;
645 case MIME_TEXT_HTML:
646 case MIME_TEXT:
647 case MIME_TEXT_PLAIN:
648 switch (action) {
649 case SEND_TODISP:
650 case SEND_TODISP_ALL:
651 case SEND_TODISP_PARTS:
652 case SEND_QUOTE:
653 case SEND_QUOTE_ALL:
654 switch (n_mimetype_handler(&mh, ip, action)) {
655 case MIME_HDL_NULL:
656 if(action != SEND_TODISP_PARTS)
657 break;
658 /* FALLTHRU */
659 case MIME_HDL_MSG:/* TODO these should be part of partinfo! */
660 _out(mh.mh_msg.s, mh.mh_msg.l, obuf, CONV_NONE, SEND_MBOX, qf,
661 stats, NULL, NULL);
662 /* We would print this as plain text, so better force going home */
663 goto jleave;
664 case MIME_HDL_CMD:
665 if(action == SEND_TODISP_PARTS &&
666 (mh.mh_flags & MIME_HDL_COPIOUSOUTPUT))
667 goto jleave;
668 break;
669 case MIME_HDL_TEXT:
670 case MIME_HDL_PTF:
671 if(action == SEND_TODISP_PARTS)
672 goto jleave;
673 break;
674 default:
675 break;
677 /* FALLTRHU */
678 default:
679 break;
681 break;
682 case MIME_DISCARD:
683 if (action != SEND_DECRYPT)
684 goto jleave;
685 break;
686 case MIME_PKCS7:
687 if (action != SEND_MBOX && action != SEND_RFC822 &&
688 action != SEND_SHOW && ip->m_multipart != NULL)
689 goto jmulti;
690 /* FALLTHRU */
691 default:
692 switch (action) {
693 case SEND_TODISP:
694 case SEND_TODISP_ALL:
695 case SEND_TODISP_PARTS:
696 case SEND_QUOTE:
697 case SEND_QUOTE_ALL:
698 switch (n_mimetype_handler(&mh, ip, action)) {
699 default:
700 case MIME_HDL_NULL:
701 if (action != SEND_TODISP_ALL && (level != 0 || cnt))
702 goto jleave;
703 /* FALLTHRU */
704 case MIME_HDL_MSG:/* TODO these should be part of partinfo! */
705 _out(mh.mh_msg.s, mh.mh_msg.l, obuf, CONV_NONE, SEND_MBOX, qf,
706 stats, NULL, NULL);
707 /* We would print this as plain text, so better force going home */
708 goto jleave;
709 case MIME_HDL_CMD:
710 if(action == SEND_TODISP_PARTS){
711 if(mh.mh_flags & MIME_HDL_COPIOUSOUTPUT)
712 goto jleave;
713 else{
714 _print_part_info(obuf, ip, doitp, level, qf, stats);
715 if(!getapproval(_("Run MIME handler for this part?"), FAL0))
716 goto jleave;
719 break;
720 case MIME_HDL_TEXT:
721 case MIME_HDL_PTF:
722 if(action == SEND_TODISP_PARTS)
723 goto jleave;
724 break;
726 break;
727 case SEND_TOFILE:
728 case SEND_TOPIPE:
729 case SEND_TOSRCH:
730 case SEND_DECRYPT:
731 case SEND_MBOX:
732 case SEND_RFC822:
733 case SEND_SHOW:
734 break;
736 break;
737 case MIME_ALTERNATIVE:
738 if ((action == SEND_TODISP || action == SEND_QUOTE) &&
739 !ok_blook(print_alternatives)) {
740 /* XXX This (a) should not remain (b) should be own fun
741 * TODO (despite the fact that v15 will do this completely differently
742 * TODO by having an action-specific "manager" that will traverse the
743 * TODO parsed MIME tree and decide for each part whether it'll be
744 * TODO displayed or not *before* we walk the tree for doing action */
745 struct mpstack {
746 struct mpstack *outer;
747 struct mimepart *mp;
748 } outermost, * volatile curr, * volatile mpsp;
749 bool_t volatile neednl, hadpart;
750 struct n_sigman smalter;
752 (curr = &outermost)->outer = NULL;
753 curr->mp = ip;
754 neednl = hadpart = FAL0;
756 n_SIGMAN_ENTER_SWITCH(&smalter, n_SIGMAN_ALL) {
757 case 0:
758 break;
759 default:
760 rv = -1;
761 goto jalter_leave;
764 for (np = ip->m_multipart;;) {
765 jalter_redo:
766 for (; np != NULL; np = np->m_nextpart) {
767 if (action != SEND_QUOTE && np->m_ct_type_plain != NULL) {
768 if (neednl)
769 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats,
770 NULL, NULL);
771 _print_part_info(obuf, np, doitp, level, qf, stats);
773 neednl = TRU1;
775 switch (np->m_mimecontent) {
776 case MIME_ALTERNATIVE:
777 case MIME_RELATED:
778 case MIME_DIGEST:
779 case MIME_SIGNED:
780 case MIME_ENCRYPTED:
781 case MIME_MULTI:
782 mpsp = salloc(sizeof *mpsp);
783 mpsp->outer = curr;
784 mpsp->mp = np->m_multipart;
785 curr->mp = np;
786 curr = mpsp;
787 np = mpsp->mp;
788 neednl = FAL0;
789 goto jalter_redo;
790 default:
791 if (hadpart)
792 break;
793 switch (n_mimetype_handler(&mh, np, action)) {
794 default:
795 mh.mh_flags = MIME_HDL_NULL;
796 continue; /* break; break; */
797 case MIME_HDL_PTF:
798 if (!ok_blook(mime_alternative_favour_rich)) {/* TODO */
799 struct mimepart *x = np;
801 while ((x = x->m_nextpart) != NULL) {
802 struct mime_handler mhx;
804 if (x->m_mimecontent == MIME_TEXT_PLAIN ||
805 n_mimetype_handler(&mhx, x, action) ==
806 MIME_HDL_TEXT)
807 break;
809 if (x != NULL)
810 continue; /* break; break; */
811 goto jalter_plain;
813 /* FALLTHRU */
814 case MIME_HDL_TEXT:
815 break;
817 /* FALLTHRU */
818 case MIME_TEXT_PLAIN:
819 if (hadpart)
820 break;
821 if (ok_blook(mime_alternative_favour_rich)) { /* TODO */
822 struct mimepart *x = np;
824 /* TODO twice TODO, we should dive into /related and
825 * TODO check whether that has rich parts! */
826 while ((x = x->m_nextpart) != NULL) {
827 struct mime_handler mhx;
829 switch (n_mimetype_handler(&mhx, x, action)) {
830 case MIME_HDL_PTF:
831 break;
832 default:
833 continue;
835 break;
837 if (x != NULL)
838 continue; /* break; break; */
840 jalter_plain:
841 quoteflt_flush(qf);
842 if (action == SEND_QUOTE && hadpart) {
843 struct quoteflt *dummy = quoteflt_dummy();
844 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, dummy, stats,
845 NULL,NULL);
846 quoteflt_flush(dummy);
848 hadpart = TRU1;
849 neednl = FAL0;
850 rv = sendpart(zmp, np, obuf, doitp, qf, action,
851 linedat, linesize, stats, level + 1);
852 quoteflt_reset(qf, origobuf);
854 if (rv < 0)
855 curr = &outermost; /* Cause overall loop termination */
856 break;
860 mpsp = curr->outer;
861 if (mpsp == NULL)
862 break;
863 curr = mpsp;
864 np = curr->mp->m_nextpart;
866 jalter_leave:
867 n_sigman_leave(&smalter, n_SIGMAN_VIPSIGS_NTTYOUT);
868 goto jleave;
870 /* FALLTHRU */
871 case MIME_RELATED:
872 case MIME_DIGEST:
873 case MIME_SIGNED:
874 case MIME_ENCRYPTED:
875 case MIME_MULTI:
876 switch (action) {
877 case SEND_TODISP:
878 case SEND_TODISP_ALL:
879 case SEND_TODISP_PARTS:
880 case SEND_QUOTE:
881 case SEND_QUOTE_ALL:
882 case SEND_TOFILE:
883 case SEND_TOPIPE:
884 case SEND_TOSRCH:
885 case SEND_DECRYPT:
886 jmulti:
887 if ((action == SEND_TODISP || action == SEND_TODISP_ALL) &&
888 ip->m_multipart != NULL &&
889 ip->m_multipart->m_mimecontent == MIME_DISCARD &&
890 ip->m_multipart->m_nextpart == NULL) {
891 char const *x = _("[Missing multipart boundary - use `show' "
892 "to display the raw message]\n");
893 _out(x, strlen(x), obuf, CONV_NONE, SEND_MBOX, qf, stats,
894 NULL,NULL);
897 for (np = ip->m_multipart; np != NULL; np = np->m_nextpart) {
898 bool_t volatile ispipe;
900 if (np->m_mimecontent == MIME_DISCARD && action != SEND_DECRYPT)
901 continue;
903 ispipe = FAL0;
904 switch (action) {
905 case SEND_TOFILE:
906 if (np->m_partstring &&
907 np->m_partstring[0] == '1' && np->m_partstring[1] == '\0')
908 break;
909 stats = NULL;
910 /* TODO Always open multipart on /dev/null, it's a hack to be
911 * TODO able to dive into that structure, and still better
912 * TODO than asking the user for something stupid.
913 * TODO oh, wait, we did ask for a filename for this MIME mail,
914 * TODO and that outer container is useless anyway ;-P */
915 if (np->m_multipart != NULL) {
916 if ((obuf = Fopen(n_path_devnull, "w")) == NULL)
917 continue;
918 } else if ((obuf = newfile(np, &ispipe)) == NULL)
919 continue;
920 if (!ispipe)
921 break;
922 if (sigsetjmp(_send_pipejmp, 1)) {
923 rv = -1;
924 goto jpipe_close;
926 oldpipe = safe_signal(SIGPIPE, &_send_onpipe);
927 break;
928 case SEND_TODISP:
929 case SEND_TODISP_ALL:
930 if (ip->m_mimecontent != MIME_ALTERNATIVE &&
931 ip->m_mimecontent != MIME_RELATED &&
932 ip->m_mimecontent != MIME_DIGEST &&
933 ip->m_mimecontent != MIME_SIGNED &&
934 ip->m_mimecontent != MIME_ENCRYPTED &&
935 ip->m_mimecontent != MIME_MULTI)
936 break;
937 _print_part_info(obuf, np, doitp, level, qf, stats);
938 break;
939 case SEND_TODISP_PARTS:
940 case SEND_QUOTE:
941 case SEND_QUOTE_ALL:
942 case SEND_MBOX:
943 case SEND_RFC822:
944 case SEND_SHOW:
945 case SEND_TOSRCH:
946 case SEND_DECRYPT:
947 case SEND_TOPIPE:
948 break;
951 quoteflt_flush(qf);
952 if ((action == SEND_QUOTE || action == SEND_QUOTE_ALL) &&
953 np->m_multipart == NULL && ip->m_parent != NULL) {
954 struct quoteflt *dummy = quoteflt_dummy();
955 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, dummy, stats,
956 NULL,NULL);
957 quoteflt_flush(dummy);
959 if (sendpart(zmp, np, obuf, doitp, qf, action, linedat, linesize,
960 stats, level+1) < 0)
961 rv = -1;
962 quoteflt_reset(qf, origobuf);
964 if (action == SEND_QUOTE) {
965 if (ip->m_mimecontent != MIME_RELATED)
966 break;
968 if (action == SEND_TOFILE && obuf != origobuf) {
969 if (!ispipe)
970 Fclose(obuf);
971 else {
972 jpipe_close:
973 safe_signal(SIGPIPE, SIG_IGN);
974 Pclose(obuf, TRU1);
975 safe_signal(SIGPIPE, oldpipe);
979 goto jleave;
980 case SEND_MBOX:
981 case SEND_RFC822:
982 case SEND_SHOW:
983 break;
985 break;
988 /* Copy out message body */
989 if (doitp == n_IGNORE_ALL && level == 0) /* skip final blank line */
990 --cnt;
991 switch (ip->m_mime_enc) {
992 case MIMEE_BIN:
993 case MIMEE_7B:
994 case MIMEE_8B:
995 convert = CONV_NONE;
996 break;
997 case MIMEE_QP:
998 convert = CONV_FROMQP;
999 break;
1000 case MIMEE_B64:
1001 switch (ip->m_mimecontent) {
1002 case MIME_TEXT:
1003 case MIME_TEXT_PLAIN:
1004 case MIME_TEXT_HTML:
1005 convert = CONV_FROMB64_T;
1006 break;
1007 default:
1008 switch (mh.mh_flags & MIME_HDL_TYPE_MASK) {
1009 case MIME_HDL_TEXT:
1010 case MIME_HDL_PTF:
1011 convert = CONV_FROMB64_T;
1012 break;
1013 default:
1014 convert = CONV_FROMB64;
1015 break;
1017 break;
1019 break;
1020 default:
1021 convert = CONV_NONE;
1024 /* TODO Unless we have filters, ensure iconvd==-1 so that mime.c:fwrite_td()
1025 * TODO cannot mess things up misusing outrest as line buffer */
1026 #ifdef HAVE_ICONV
1027 if (iconvd != (iconv_t)-1) {
1028 n_iconv_close(iconvd);
1029 iconvd = (iconv_t)-1;
1031 #endif
1033 if (action == SEND_DECRYPT || action == SEND_MBOX ||
1034 action == SEND_RFC822 || action == SEND_SHOW)
1035 convert = CONV_NONE;
1036 #ifdef HAVE_ICONV
1037 else if ((action == SEND_TODISP || action == SEND_TODISP_ALL ||
1038 action == SEND_TODISP_PARTS ||
1039 action == SEND_QUOTE || action == SEND_QUOTE_ALL ||
1040 action == SEND_TOSRCH || action == SEND_TOFILE) &&
1041 (ip->m_mimecontent == MIME_TEXT_PLAIN ||
1042 ip->m_mimecontent == MIME_TEXT_HTML ||
1043 ip->m_mimecontent == MIME_TEXT ||
1044 (mh.mh_flags & MIME_HDL_TYPE_MASK) == MIME_HDL_TEXT ||
1045 (mh.mh_flags & MIME_HDL_TYPE_MASK) == MIME_HDL_PTF)) {
1046 char const *tcs;
1048 tcs = ok_vlook(ttycharset);
1049 if (asccasecmp(tcs, ip->m_charset) &&
1050 asccasecmp(ok_vlook(charset_7bit), ip->m_charset)) {
1051 iconvd = n_iconv_open(tcs, ip->m_charset);
1052 if (iconvd == (iconv_t)-1 && n_err_no == n_ERR_INVAL) {
1053 n_err(_("Cannot convert from %s to %s\n"), ip->m_charset, tcs);
1054 /*rv = 1; goto jleave;*/
1058 #endif
1060 switch (mh.mh_flags & MIME_HDL_TYPE_MASK) {
1061 case MIME_HDL_CMD:
1062 if(!(mh.mh_flags & MIME_HDL_COPIOUSOUTPUT) &&
1063 action != SEND_TODISP_PARTS)
1064 goto jmhp_default;
1065 /* FALLTHRU */
1066 case MIME_HDL_PTF:
1067 tmpname = NULL;
1068 qbuf = obuf;
1070 term_infd = n_CHILD_FD_PASS;
1071 if (mh.mh_flags & (MIME_HDL_TMPF | MIME_HDL_NEEDSTERM)) {
1072 enum oflags of;
1074 of = OF_RDWR | OF_REGISTER;
1075 if (!(mh.mh_flags & MIME_HDL_TMPF)) {
1076 term_infd = 0;
1077 mh.mh_flags |= MIME_HDL_TMPF_FILL;
1078 of |= OF_UNLINK;
1079 } else if (mh.mh_flags & MIME_HDL_TMPF_UNLINK)
1080 of |= OF_REGISTER_UNLINK;
1082 if ((pbuf = Ftmp((mh.mh_flags & MIME_HDL_TMPF ? &cp : NULL),
1083 (mh.mh_flags & MIME_HDL_TMPF_FILL ? "mimehdlfill" : "mimehdl"),
1084 of)) == NULL)
1085 goto jesend;
1087 if (mh.mh_flags & MIME_HDL_TMPF) {
1088 tmpname = savestr(cp);
1089 Ftmp_free(&cp);
1092 if (mh.mh_flags & MIME_HDL_TMPF_FILL) {
1093 if (term_infd == 0)
1094 term_infd = fileno(pbuf);
1095 goto jsend;
1099 jpipe_for_real:
1100 pbuf = _pipefile(&mh, ip, n_UNVOLATILE(&qbuf), tmpname, term_infd);
1101 if (pbuf == NULL) {
1102 jesend:
1103 pbuf = qbuf = NULL;
1104 rv = -1;
1105 goto jend;
1106 } else if ((mh.mh_flags & MIME_HDL_NEEDSTERM) && pbuf == (FILE*)-1) {
1107 pbuf = qbuf = NULL;
1108 goto jend;
1110 tmpname = NULL;
1111 action = SEND_TOPIPE;
1112 if (pbuf != qbuf) {
1113 oldpipe = safe_signal(SIGPIPE, &_send_onpipe);
1114 if (sigsetjmp(_send_pipejmp, 1))
1115 goto jend;
1117 break;
1119 default:
1120 jmhp_default:
1121 mh.mh_flags = MIME_HDL_NULL;
1122 pbuf = qbuf = obuf;
1123 break;
1126 jsend:
1128 bool_t volatile eof;
1129 ui32_t save_qf_pfix_len = qf->qf_pfix_len;
1130 ui64_t *save_stats = stats;
1132 if (pbuf != origobuf) {
1133 qf->qf_pfix_len = 0; /* XXX legacy (remove filter instead) */
1134 stats = NULL;
1136 eof = FAL0;
1137 outrest.s = inrest.s = NULL;
1138 outrest.l = inrest.l = 0;
1140 if (pbuf == qbuf) {
1141 __sendp_sig = 0;
1142 __sendp_opipe = safe_signal(SIGPIPE, &__sendp_onsig);
1143 if (sigsetjmp(__sendp_actjmp, 1)) {
1144 n_pstate &= ~n_PS_BASE64_STRIP_CR;/* (but protected by outer sigman) */
1145 if (outrest.s != NULL)
1146 free(outrest.s);
1147 if (inrest.s != NULL)
1148 free(inrest.s);
1149 #ifdef HAVE_ICONV
1150 if (iconvd != (iconv_t)-1)
1151 n_iconv_close(iconvd);
1152 #endif
1153 safe_signal(SIGPIPE, __sendp_opipe);
1154 n_raise(__sendp_sig);
1158 quoteflt_reset(qf, pbuf);
1159 if(dostat & 4)
1160 n_pstate |= n_PS_BASE64_STRIP_CR;
1161 while (!eof && fgetline(linedat, linesize, &cnt, &linelen, ibuf, 0)) {
1162 joutln:
1163 if (_out(*linedat, linelen, pbuf, convert, action, qf, stats, &outrest,
1164 (action & _TD_EOF ? NULL : &inrest)) < 0 || ferror(pbuf)) {
1165 rv = -1; /* XXX Should bail away?! */
1166 break;
1169 if(eof <= FAL0 && rv >= 0 && (outrest.l != 0 || inrest.l != 0)){
1170 linelen = 0;
1171 if(eof || inrest.l == 0)
1172 action |= _TD_EOF;
1173 eof = eof ? TRU1 : TRUM1;
1174 goto joutln;
1176 n_pstate &= ~n_PS_BASE64_STRIP_CR;
1177 action &= ~_TD_EOF;
1179 /* TODO HACK: when sending to the display we yet get fooled if a message
1180 * TODO doesn't end in a newline, because of our input/output 1:1.
1181 * TODO This should be handled automatically by a display filter, then */
1182 if(rv >= 0 && !qf->qf_nl_last &&
1183 (action == SEND_TODISP || action == SEND_TODISP_ALL))
1184 rv = quoteflt_push(qf, "\n", 1);
1186 quoteflt_flush(qf);
1188 if (rv >= 0 && (mh.mh_flags & MIME_HDL_TMPF_FILL)) {
1189 mh.mh_flags &= ~MIME_HDL_TMPF_FILL;
1190 fflush(pbuf);
1191 really_rewind(pbuf);
1192 /* Don't Fclose() the Ftmp() thing due to OF_REGISTER_UNLINK++ */
1193 goto jpipe_for_real;
1196 if (pbuf == qbuf)
1197 safe_signal(SIGPIPE, __sendp_opipe);
1199 if (outrest.s != NULL)
1200 free(outrest.s);
1201 if (inrest.s != NULL)
1202 free(inrest.s);
1204 if (pbuf != origobuf) {
1205 qf->qf_pfix_len = save_qf_pfix_len;
1206 stats = save_stats;
1210 jend:
1211 if (pbuf != qbuf) {
1212 safe_signal(SIGPIPE, SIG_IGN);
1213 Pclose(pbuf, !(mh.mh_flags & MIME_HDL_ASYNC));
1214 safe_signal(SIGPIPE, oldpipe);
1215 if (rv >= 0 && qbuf != NULL && qbuf != obuf)
1216 pipecpy(qbuf, obuf, origobuf, qf, stats);
1218 #ifdef HAVE_ICONV
1219 if (iconvd != (iconv_t)-1)
1220 n_iconv_close(iconvd);
1221 #endif
1222 jleave:
1223 NYD_LEAVE;
1224 return rv;
1227 static FILE *
1228 newfile(struct mimepart *ip, bool_t volatile *ispipe)
1230 struct str in, out;
1231 char *f;
1232 FILE *fp;
1233 NYD_ENTER;
1235 f = ip->m_filename;
1236 *ispipe = FAL0;
1238 if (f != NULL && f != (char*)-1) {
1239 in.s = f;
1240 in.l = strlen(f);
1241 makeprint(&in, &out);
1242 out.l = delctrl(out.s, out.l);
1243 f = savestrbuf(out.s, out.l);
1244 free(out.s);
1247 /* In interactive mode, let user perform all kind of expansions as desired,
1248 * and offer |SHELL-SPEC pipe targets, too */
1249 if (n_psonce & n_PSO_INTERACTIVE) {
1250 struct str prompt;
1251 struct n_string shou, *shoup;
1252 char *f2, *f3;
1254 shoup = n_string_creat_auto(&shou);
1256 /* TODO Generic function which asks for filename.
1257 * TODO If the current part is the first textpart the target
1258 * TODO is implicit from outer `write' etc! */
1259 /* I18N: Filename input prompt with file type indication */
1260 str_concat_csvl(&prompt, _("Enter filename for part "),
1261 (ip->m_partstring != NULL ? ip->m_partstring : n_qm),
1262 " (", ip->m_ct_type_plain, "): ", NULL);
1263 jgetname:
1264 f2 = n_go_input_cp(n_GO_INPUT_CTX_DEFAULT | n_GO_INPUT_HIST_ADD,
1265 prompt.s, ((f != (char*)-1 && f != NULL)
1266 ? n_shexp_quote_cp(f, FAL0) : NULL));
1267 if(f2 != NULL){
1268 in.s = n_UNCONST(f2);
1269 in.l = UIZ_MAX;
1270 if((n_shexp_parse_token((n_SHEXP_PARSE_TRUNC |
1271 n_SHEXP_PARSE_TRIM_SPACE | n_SHEXP_PARSE_TRIM_IFSSPACE |
1272 n_SHEXP_PARSE_LOG | n_SHEXP_PARSE_IGNORE_EMPTY),
1273 shoup, &in, NULL
1274 ) & (n_SHEXP_STATE_STOP |
1275 n_SHEXP_STATE_OUTPUT | n_SHEXP_STATE_ERR_MASK)
1276 ) != (n_SHEXP_STATE_STOP | n_SHEXP_STATE_OUTPUT))
1277 goto jgetname;
1278 f2 = n_string_cp(shoup);
1280 if (f2 == NULL || *f2 == '\0') {
1281 if (n_poption & n_PO_D_V)
1282 n_err(_("... skipping this\n"));
1283 n_string_gut(shoup);
1284 fp = NULL;
1285 goto jleave;
1288 if (*f2 == '|')
1289 /* Pipes are expanded by the shell */
1290 f = f2;
1291 else if ((f3 = fexpand(f2, FEXP_LOCAL | FEXP_NVAR)) == NULL)
1292 /* (Error message written by fexpand()) */
1293 goto jgetname;
1294 else
1295 f = f3;
1297 n_string_gut(shoup);
1300 if (f == NULL || f == (char*)-1 || *f == '\0')
1301 fp = NULL;
1302 else if (n_psonce & n_PSO_INTERACTIVE) {
1303 if (*f == '|') {
1304 fp = Popen(&f[1], "w", ok_vlook(SHELL), NULL, 1);
1305 if (!(*ispipe = (fp != NULL)))
1306 n_perr(f, 0);
1307 } else if ((fp = Fopen(f, "w")) == NULL)
1308 n_err(_("Cannot open %s\n"), n_shexp_quote_cp(f, FAL0));
1309 } else {
1310 /* Be very picky in non-interactive mode: actively disallow pipes,
1311 * prevent directory separators, and any filename member that would
1312 * become expanded by the shell if the name would be echo(1)ed */
1313 if(anyof(f, "/" n_SHEXP_MAGIC_PATH_CHARS)){
1314 char c;
1316 for(out.s = salloc((strlen(f) * 3) +1), out.l = 0; (c = *f++) != '\0';)
1317 if(strchr("/" n_SHEXP_MAGIC_PATH_CHARS, c)){
1318 out.s[out.l++] = '%';
1319 n_c_to_hex_base16(&out.s[out.l], c);
1320 out.l += 2;
1321 }else
1322 out.s[out.l++] = c;
1323 out.s[out.l] = '\0';
1324 f = out.s;
1327 /* Avoid overwriting of existing files */
1328 while((fp = Fopen(f, "wx")) == NULL){
1329 int e;
1331 if((e = n_err_no) != n_ERR_EXIST){
1332 n_err(_("Cannot open %s: %s\n"),
1333 n_shexp_quote_cp(f, FAL0), n_err_to_doc(e));
1334 break;
1337 if(ip->m_partstring != NULL)
1338 f = savecatsep(f, '#', ip->m_partstring);
1339 else
1340 f = savecat(f, "#.");
1343 jleave:
1344 NYD_LEAVE;
1345 return fp;
1348 static void
1349 pipecpy(FILE *pipebuf, FILE *outbuf, FILE *origobuf, struct quoteflt *qf,
1350 ui64_t *stats)
1352 char *line = NULL; /* TODO line pool */
1353 size_t linesize = 0, linelen, cnt;
1354 ssize_t all_sz, sz;
1355 NYD_ENTER;
1357 fflush(pipebuf);
1358 rewind(pipebuf);
1359 cnt = (size_t)fsize(pipebuf);
1360 all_sz = 0;
1362 quoteflt_reset(qf, outbuf);
1363 while (fgetline(&line, &linesize, &cnt, &linelen, pipebuf, 0) != NULL) {
1364 if ((sz = quoteflt_push(qf, line, linelen)) < 0)
1365 break;
1366 all_sz += sz;
1368 if ((sz = quoteflt_flush(qf)) > 0)
1369 all_sz += sz;
1370 if (line)
1371 free(line);
1373 if (all_sz > 0 && outbuf == origobuf && stats != NULL)
1374 *stats += all_sz;
1375 Fclose(pipebuf);
1376 NYD_LEAVE;
1379 static void
1380 statusput(const struct message *mp, FILE *obuf, struct quoteflt *qf,
1381 ui64_t *stats)
1383 char statout[3], *cp = statout;
1384 NYD_ENTER;
1386 if (mp->m_flag & MREAD)
1387 *cp++ = 'R';
1388 if (!(mp->m_flag & MNEW))
1389 *cp++ = 'O';
1390 *cp = 0;
1391 if (statout[0]) {
1392 int i = fprintf(obuf, "%.*sStatus: %s\n", (int)qf->qf_pfix_len,
1393 (qf->qf_pfix_len > 0 ? qf->qf_pfix : 0), statout);
1394 if (i > 0 && stats != NULL)
1395 *stats += i;
1397 NYD_LEAVE;
1400 static void
1401 xstatusput(const struct message *mp, FILE *obuf, struct quoteflt *qf,
1402 ui64_t *stats)
1404 char xstatout[4];
1405 char *xp = xstatout;
1406 NYD_ENTER;
1408 if (mp->m_flag & MFLAGGED)
1409 *xp++ = 'F';
1410 if (mp->m_flag & MANSWERED)
1411 *xp++ = 'A';
1412 if (mp->m_flag & MDRAFTED)
1413 *xp++ = 'T';
1414 *xp = 0;
1415 if (xstatout[0]) {
1416 int i = fprintf(obuf, "%.*sX-Status: %s\n", (int)qf->qf_pfix_len,
1417 (qf->qf_pfix_len > 0 ? qf->qf_pfix : 0), xstatout);
1418 if (i > 0 && stats != NULL)
1419 *stats += i;
1421 NYD_LEAVE;
1424 static void
1425 put_from_(FILE *fp, struct mimepart *ip, ui64_t *stats)
1427 char const *froma, *date, *nl;
1428 int i;
1429 NYD_ENTER;
1431 if (ip != NULL && ip->m_from != NULL) {
1432 froma = ip->m_from;
1433 date = fakedate(ip->m_time);
1434 nl = "\n";
1435 } else {
1436 froma = ok_vlook(LOGNAME);
1437 date = time_current.tc_ctime;
1438 nl = n_empty;
1441 n_COLOUR(
1442 if(n_COLOUR_IS_ACTIVE())
1443 n_colour_put(n_COLOUR_ID_VIEW_FROM_, NULL);
1445 i = fprintf(fp, "From %s %s%s", froma, date, nl);
1446 n_COLOUR(
1447 if(n_COLOUR_IS_ACTIVE())
1448 n_colour_reset();
1450 if (i > 0 && stats != NULL)
1451 *stats += i;
1452 NYD_LEAVE;
1455 FL int
1456 sendmp(struct message *mp, FILE *obuf, struct n_ignore const *doitp,
1457 char const *prefix, enum sendaction action, ui64_t *stats)
1459 struct n_sigman linedat_protect;
1460 struct quoteflt qf;
1461 FILE *ibuf;
1462 enum mime_parse_flags mpf;
1463 struct mimepart *ip;
1464 size_t linesize, cnt, sz, i;
1465 char *linedat;
1466 int rv, c;
1467 NYD_ENTER;
1469 time_current_update(&time_current, TRU1);
1470 rv = -1;
1471 linedat = NULL;
1472 linesize = 0;
1473 quoteflt_init(&qf, prefix);
1475 n_SIGMAN_ENTER_SWITCH(&linedat_protect, n_SIGMAN_ALL){
1476 case 0:
1477 break;
1478 default:
1479 goto jleave;
1482 if (mp == dot && action != SEND_TOSRCH)
1483 n_pstate |= n_PS_DID_PRINT_DOT;
1484 if (stats != NULL)
1485 *stats = 0;
1487 /* First line is the From_ line, so no headers there to worry about */
1488 if ((ibuf = setinput(&mb, mp, NEED_BODY)) == NULL)
1489 goto jleave;
1491 cnt = mp->m_size;
1492 sz = 0;
1494 bool_t nozap;
1495 char const *cpre = n_empty, *csuf = n_empty;
1497 #ifdef HAVE_COLOUR
1498 if(n_COLOUR_IS_ACTIVE()){
1499 struct n_colour_pen *cpen;
1500 struct str const *sp;
1502 cpen = n_colour_pen_create(n_COLOUR_ID_VIEW_FROM_,NULL);
1503 if((sp = n_colour_pen_to_str(cpen)) != NULL){
1504 cpre = sp->s;
1505 sp = n_colour_reset_to_str();
1506 if(sp != NULL)
1507 csuf = sp->s;
1510 #endif
1512 nozap = (doitp != n_IGNORE_ALL && doitp != n_IGNORE_FWD &&
1513 action != SEND_RFC822 &&
1514 !n_ignore_is_ign(doitp, "from_", sizeof("from_") -1));
1515 if (mp->m_flag & MNOFROM) {
1516 if (nozap)
1517 sz = fprintf(obuf, "%s%.*sFrom %s %s%s\n",
1518 cpre, (int)qf.qf_pfix_len,
1519 (qf.qf_pfix_len != 0 ? qf.qf_pfix : n_empty), fakefrom(mp),
1520 fakedate(mp->m_time), csuf);
1521 } else if (nozap) {
1522 if (qf.qf_pfix_len > 0) {
1523 i = fwrite(qf.qf_pfix, sizeof *qf.qf_pfix, qf.qf_pfix_len, obuf);
1524 if (i != qf.qf_pfix_len)
1525 goto jleave;
1526 sz += i;
1528 #ifdef HAVE_COLOUR
1529 if(*cpre != '\0'){
1530 fputs(cpre, obuf);
1531 cpre = (char const*)0x1;
1533 #endif
1535 while (cnt > 0 && (c = getc(ibuf)) != EOF) {
1536 #ifdef HAVE_COLOUR
1537 if(c == '\n' && *csuf != '\0'){
1538 cpre = (char const*)0x1;
1539 fputs(csuf, obuf);
1541 #endif
1542 putc(c, obuf);
1543 ++sz;
1544 --cnt;
1545 if (c == '\n')
1546 break;
1549 #ifdef HAVE_COLOUR
1550 if(*csuf != '\0' && cpre != (char const*)0x1 && *cpre != '\0')
1551 fputs(csuf, obuf);
1552 #endif
1553 } else {
1554 while (cnt > 0 && (c = getc(ibuf)) != EOF) {
1555 --cnt;
1556 if (c == '\n')
1557 break;
1561 if (sz > 0 && stats != NULL)
1562 *stats += sz;
1564 mpf = MIME_PARSE_NONE;
1565 if (action != SEND_MBOX && action != SEND_RFC822 && action != SEND_SHOW)
1566 mpf |= MIME_PARSE_PARTS | MIME_PARSE_DECRYPT;
1567 if ((ip = mime_parse_msg(mp, mpf)) == NULL)
1568 goto jleave;
1570 rv = sendpart(mp, ip, obuf, doitp, &qf, action, &linedat, &linesize,
1571 stats, 0);
1573 n_sigman_cleanup_ping(&linedat_protect);
1574 jleave:
1575 n_pstate &= ~n_PS_BASE64_STRIP_CR;
1576 quoteflt_destroy(&qf);
1577 if(linedat != NULL)
1578 free(linedat);
1579 NYD_LEAVE;
1580 n_sigman_leave(&linedat_protect, n_SIGMAN_VIPSIGS_NTTYOUT);
1581 return rv;
1584 /* s-it-mode */