MLE, mle-complete: do not start new line for each non-iswalnum(3)
[s-mailx.git] / send.c
blobca7de728f74a94cd562aa8116dad8aa6a0292fe1
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 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);
84 static void
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)
88 char buf[64];
89 struct str ti, to;
90 struct str const *cpre, *csuf;
91 char const *cp;
92 NYD2_ENTER;
94 #ifdef HAVE_COLOUR
96 struct n_colour_pen *cpen = n_colour_pen_create(n_COLOUR_ID_VIEW_PARTINFO,
97 NULL);
98 if ((cpre = n_colour_pen_to_str(cpen)) != NULL)
99 csuf = n_colour_reset_to_str();
100 else
101 csuf = NULL;
103 #else
104 cpre = csuf = NULL;
105 #endif
107 /* Take care of "99.99", i.e., 5 */
108 if ((cp = mpp->m_partstring) == NULL || cp[0] == '\0')
109 cp = "?";
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 if ((cp = mpp->m_ct_type_usr_ovwr) != NULL)
125 _out("+", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
126 else
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);
135 cp = x;
136 to.l = al + i;
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"))
143 cp = "qu.-pr.";
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);
153 if (csuf != 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);
157 /* */
158 if (mpp->m_content_info & CI_MIME_ERRORS) {
159 if (cpre != NULL)
160 _out(cpre->s, cpre->l, obuf, CONV_NONE, SEND_MBOX, qf, stats,
161 NULL, NULL);
162 _out("[-- ", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
164 ti.l = strlen(ti.s = n_UNCONST(_("Defective MIME structure")));
165 makeprint(&ti, &to);
166 to.l = delctrl(to.s, to.l);
167 _out(to.s, to.l, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
168 free(to.s);
170 _out(" --]", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
171 if (csuf != NULL)
172 _out(csuf->s, csuf->l, obuf, CONV_NONE, SEND_MBOX, qf, stats,
173 NULL, NULL);
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') {
180 if (cpre != NULL)
181 _out(cpre->s, cpre->l, obuf, CONV_NONE, SEND_MBOX, qf, stats,
182 NULL, NULL);
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);
188 free(to.s);
190 _out(" --]", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
191 if (csuf != NULL)
192 _out(csuf->s, csuf->l, obuf, CONV_NONE, SEND_MBOX, qf, stats,
193 NULL, NULL);
194 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
197 /* Filename */
198 if (n_ignore_is_ign(doitp, "content-disposition", 19) &&
199 mpp->m_filename != NULL && *mpp->m_filename != '\0') {
200 if (cpre != NULL)
201 _out(cpre->s, cpre->l, obuf, CONV_NONE, SEND_MBOX, qf, stats,
202 NULL, NULL);
203 _out("[-- ", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
205 ti.l = strlen(ti.s = mpp->m_filename);
206 makeprint(&ti, &to);
207 to.l = delctrl(to.s, to.l);
208 _out(to.s, to.l, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
209 free(to.s);
211 _out(" --]", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
212 if (csuf != NULL)
213 _out(csuf->s, csuf->l, obuf, CONV_NONE, SEND_MBOX, qf, stats,
214 NULL, NULL);
215 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
217 NYD2_LEAVE;
220 static FILE *
221 _pipefile(struct mime_handler *mhp, struct mimepart const *mpp, FILE **qbuf,
222 char const *tmpname, int term_infd)
224 struct str s;
225 char const *env_addon[8 +8/*v15*/], *cp, *sh;
226 FILE *rbuf;
227 NYD_ENTER;
229 rbuf = *qbuf;
231 if (mhp->mh_flags & MIME_HDL_ISQUOTE) {
232 if ((*qbuf = Ftmp(NULL, "sendp", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
233 NULL) {
234 n_perr(_("tmpfile"), 0);
235 *qbuf = rbuf;
239 if ((mhp->mh_flags & MIME_HDL_TYPE_MASK) == MIME_HDL_PTF) {
240 union {int (*ptf)(void); char const *sh;} u;
242 fflush(*qbuf);
243 if (*qbuf != n_stdout) /* xxx never? v15: it'll be a filter anyway */
244 fflush(n_stdout);
246 u.ptf = mhp->mh_ptf;
247 if((rbuf = Popen((char*)-1, "W", u.sh, NULL, fileno(*qbuf))) == NULL)
248 goto jerror;
249 goto jleave;
252 /* MAILX_FILENAME */
253 if (mpp == NULL || (cp = mpp->m_filename) == NULL)
254 cp = n_empty;
255 env_addon[0] = str_concat_csvl(&s, n_PIPEENV_FILENAME, "=", cp, NULL)->s;
256 env_addon[1] = str_concat_csvl(&s, "NAIL_FILENAME", "=", cp, NULL)->s;
258 /* MAILX_FILENAME_GENERATED *//* TODO pathconf NAME_MAX; but user can create
259 * TODO a file wherever he wants! *Do* create a zero-size temporary file
260 * TODO and give *that* path as MAILX_FILENAME_TEMPORARY, clean it up once
261 * TODO the pipe returns? Like this we *can* verify path/name issues! */
262 cp = getrandstring(n_MIN(NAME_MAX / 4, 16));
263 env_addon[2] = str_concat_csvl(&s, n_PIPEENV_FILENAME_GENERATED, "=", cp,
264 NULL)->s;
265 env_addon[3] = str_concat_csvl(&s, "NAIL_FILENAME_GENERATED", "=", cp,
266 NULL)->s;
268 /* MAILX_CONTENT{,_EVIDENCE} */
269 if (mpp == NULL || (cp = mpp->m_ct_type_plain) == NULL)
270 cp = n_empty;
271 env_addon[4] = str_concat_csvl(&s, n_PIPEENV_CONTENT, "=", cp, NULL)->s;
272 env_addon[5] = str_concat_csvl(&s, "NAIL_CONTENT", "=", cp, NULL)->s;
274 if (mpp != NULL && mpp->m_ct_type_usr_ovwr != NULL)
275 cp = mpp->m_ct_type_usr_ovwr;
276 env_addon[6] = str_concat_csvl(&s, n_PIPEENV_CONTENT_EVIDENCE, "=", cp,
277 NULL)->s;
278 env_addon[7] = str_concat_csvl(&s, "NAIL_CONTENT_EVIDENCE", "=", cp,
279 NULL)->s;
281 env_addon[8] = NULL;
283 /* MAILX_FILENAME_TEMPORARY? */
284 if (tmpname != NULL) {
285 env_addon[8] = str_concat_csvl(&s,
286 n_PIPEENV_FILENAME_TEMPORARY, "=", tmpname, NULL)->s;
287 env_addon[9] = str_concat_csvl(&s,
288 "NAIL_FILENAME_TEMPORARY", "=", tmpname, NULL)->s;
289 env_addon[10] = NULL;
292 sh = ok_vlook(SHELL);
294 if (mhp->mh_flags & MIME_HDL_NEEDSTERM) {
295 sigset_t nset;
296 int pid;
298 sigemptyset(&nset);
299 pid = run_command(sh, &nset, term_infd, COMMAND_FD_PASS, "-c",
300 mhp->mh_shell_cmd, NULL, env_addon);
301 rbuf = (pid < 0) ? NULL : (FILE*)-1;
302 } else {
303 rbuf = Popen(mhp->mh_shell_cmd, "W", sh, env_addon,
304 (mhp->mh_flags & MIME_HDL_ASYNC ? -1 : fileno(*qbuf)));
305 jerror:
306 if (rbuf == NULL)
307 n_err(_("Cannot run MIME type handler: %s: %s\n"),
308 mhp->mh_msg, strerror(errno));
309 else {
310 fflush(*qbuf);
311 if (*qbuf != n_stdout)
312 fflush(n_stdout);
315 jleave:
316 NYD_LEAVE;
317 return rbuf;
320 SINLINE ssize_t
321 _out(char const *buf, size_t len, FILE *fp, enum conversion convert, enum
322 sendaction action, struct quoteflt *qf, ui64_t *stats, struct str *outrest,
323 struct str *inrest)
325 ssize_t sz = 0, n;
326 int flags;
327 NYD_ENTER;
329 /* TODO We should not need is_head() here, i think in v15 the actual Mailbox
330 * TODO subclass should detect such From_ cases and either reencode the part
331 * TODO in question, or perform From_ quoting as necessary!?!?!? How?!? */
332 /* C99 */{
333 bool_t from_;
335 if((action == SEND_MBOX || action == SEND_DECRYPT) &&
336 (from_ = is_head(buf, len, TRU1))){
337 if(from_ != TRUM1 || ok_blook(mbox_rfc4155)){
338 putc('>', fp);
339 ++sz;
344 flags = ((int)action & _TD_EOF);
345 action &= ~_TD_EOF;
346 n = mime_write(buf, len, fp,
347 action == SEND_MBOX ? CONV_NONE : convert,
348 flags | ((action == SEND_TODISP || action == SEND_TODISP_ALL ||
349 action == SEND_QUOTE || action == SEND_QUOTE_ALL)
350 ? TD_ISPR | TD_ICONV
351 : (action == SEND_TOSRCH || action == SEND_TOPIPE ||
352 action == SEND_TOFILE)
353 ? TD_ICONV : (action == SEND_SHOW ? TD_ISPR : TD_NONE)),
354 qf, outrest, inrest);
355 if (n < 0)
356 sz = n;
357 else if (n > 0) {
358 sz += n;
359 if (stats != NULL)
360 *stats += sz;
362 NYD_LEAVE;
363 return sz;
366 static void
367 _send_onpipe(int signo)
369 NYD_X; /* Signal handler */
370 n_UNUSED(signo);
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;
377 static void
378 __sendp_onsig(int sig) /* TODO someday, we won't need it no more */
380 NYD_X; /* Signal handler */
381 __sendp_sig = sig;
382 siglongjmp(__sendp_actjmp, 1);
385 static int
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)
390 int volatile rv = 0;
391 struct mime_handler mh;
392 struct str outrest, inrest;
393 char *line = NULL, *cp;
394 char const * volatile tmpname = NULL;
395 size_t linesize = 0, linelen, cnt;
396 int volatile term_infd;
397 int dostat, c;
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;
403 NYD_ENTER;
405 n_UNINIT(term_infd, 0);
406 n_UNINIT(cnt, 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)
413 goto jheaders_skip;
416 dostat = 0;
417 if (level == 0) {
418 if (doitp != NULL) {
419 if (!n_ignore_is_ign(doitp, "status", 6))
420 dostat |= 1;
421 if (!n_ignore_is_ign(doitp, "x-status", 8))
422 dostat |= 2;
423 } else
424 dostat = 3;
426 if ((ibuf = setinput(&mb, (struct message*)ip, NEED_BODY)) == NULL) {
427 rv = -1;
428 goto jleave;
430 cnt = ip->m_size;
432 if (ip->m_mimecontent == MIME_DISCARD)
433 goto jheaders_skip;
435 if (!(ip->m_flag & MNOFROM))
436 while (cnt && (c = getc(ibuf)) != EOF) {
437 cnt--;
438 if (c == '\n')
439 break;
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 */
447 /* C99 */{
448 struct n_string hl, *hlp;
449 size_t lineno = 0;
450 bool_t hstop;
452 hlp = n_string_creat_auto(&hl); /* TODO pool [or, v15: filter!] */
453 /* Reserve three lines, still not enough for references and DKIM etc. */
454 hlp = n_string_reserve(hlp, n_MAX(MIME_LINELEN, MIME_LINELEN_RFC2047) * 3);
456 for(hstop = FAL0; !hstop;){
457 size_t lcnt;
459 lcnt = cnt;
460 if(fgetline(&line, &linesize, &cnt, &linelen, ibuf, 0) == NULL)
461 break;
462 ++lineno;
463 if (linelen == 0 || (cp = line)[0] == '\n')
464 /* If line is blank, we've reached end of headers */
465 break;
466 if(cp[linelen - 1] == '\n')
467 cp[--linelen] = '\0';
469 /* Are we in a header? */
470 if(hlp->s_len > 0){
471 if(!blankchar(*cp)){
472 fseek(ibuf, -(off_t)(lcnt - cnt), SEEK_CUR);
473 cnt = lcnt;
474 goto jhdrput;
476 goto jhdrpush;
477 }else{
478 /* Pick up the header field if we have one */
479 while(*cp != ':' && !spacechar(*cp))
480 ++cp;
481 while(spacechar(*cp))
482 ++cp;
483 if(*cp != ':'){
484 /* That won't work with MIME when saving etc., before v15 */
485 if (lineno != 1)
486 /* XXX This disturbs, and may happen multiple times, and we
487 * XXX cannot heal it for multipart except for display <v15 */
488 n_err(_("Malformed message: headers and body not separated "
489 "(with empty line)\n"));
490 if(level != 0)
491 dostat &= ~(1 | 2);
492 fseek(ibuf, -(off_t)(lcnt - cnt), SEEK_CUR);
493 cnt = lcnt;
494 break;
496 cp = line;
497 jhdrpush:
498 if(convert == CONV_NONE){
499 hlp = n_string_push_buf(hlp, cp, (ui32_t)linelen);
500 hlp = n_string_push_c(hlp, '\n');
501 }else{
502 bool_t lblank, isblank;
504 for(lblank = FAL0, lcnt = 0; lcnt < linelen; ++cp, ++lcnt){
505 char c8;
507 c8 = *cp;
508 if(!(isblank = blankchar(c8)) || !lblank){
509 if((lblank = isblank))
510 c8 = ' ';
511 hlp = n_string_push_c(hlp, c8);
515 continue;
518 jhdrput:
519 /* If it is an ignored header, skip it */
520 *(cp = memchr(hlp->s_dat, ':', hlp->s_len)) = '\0';
521 /* C99 */{
522 size_t i;
524 i = PTR2SIZE(cp - hlp->s_dat);
525 if((doitp != NULL && n_ignore_is_ign(doitp, hlp->s_dat, i)) ||
526 !asccasecmp(hlp->s_dat, "status") ||
527 !asccasecmp(hlp->s_dat, "x-status") ||
528 (action == SEND_MBOX &&
529 (!asccasecmp(hlp->s_dat, "content-length") ||
530 !asccasecmp(hlp->s_dat, "lines")) &&
531 !ok_blook(keep_content_length)))
532 goto jhdrtrunc;
535 /* Dump it */
536 #ifdef HAVE_COLOUR
537 if(n_pstate & n_PS_COLOUR_ACTIVE)
538 n_colour_put(obuf, n_COLOUR_ID_VIEW_HEADER, hlp->s_dat);
539 #endif
540 *cp = ':';
541 _out(hlp->s_dat, hlp->s_len, obuf, convert, action, qf, stats, NULL,NULL);
542 #ifdef HAVE_COLOUR
543 if(n_pstate & n_PS_COLOUR_ACTIVE)
544 n_colour_reset(obuf);
545 #endif
546 if(convert != CONV_NONE)
547 putc('\n', obuf);
549 jhdrtrunc:
550 hlp = n_string_trunc(hlp, 0);
552 hstop = TRU1;
553 if(hlp->s_len > 0)
554 goto jhdrput;
556 /* We've reached end of headers, so eventually force out status: field and
557 * note that we are no longer in header fields */
558 if(dostat & 1)
559 statusput(zmp, obuf, qf, stats);
560 if(dostat & 2)
561 xstatusput(zmp, obuf, qf, stats);
562 if(doitp != n_IGNORE_ALL)
563 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
564 } /* C99 */
566 quoteflt_flush(qf);
567 cp = line;
568 line = NULL;
569 free(cp);
571 if(ferror(obuf)){
572 rv = -1;
573 goto jleave;
576 jheaders_skip:
577 memset(&mh, 0, sizeof mh);
579 switch (ip->m_mimecontent) {
580 case MIME_822:
581 switch (action) {
582 case SEND_TODISP:
583 case SEND_TODISP_ALL:
584 case SEND_QUOTE:
585 case SEND_QUOTE_ALL:
586 if (ok_blook(rfc822_body_from_)) {
587 if (qf->qf_pfix_len > 0) {
588 size_t i = fwrite(qf->qf_pfix, sizeof *qf->qf_pfix,
589 qf->qf_pfix_len, obuf);
590 if (i == qf->qf_pfix_len && stats != NULL)
591 *stats += i;
593 put_from_(obuf, ip->m_multipart, stats);
595 /* FALLTHRU */
596 case SEND_TOSRCH:
597 case SEND_DECRYPT:
598 goto jmulti;
599 case SEND_TOFILE:
600 case SEND_TOPIPE:
601 if (ok_blook(rfc822_body_from_))
602 put_from_(obuf, ip->m_multipart, stats);
603 /* FALLTHRU */
604 case SEND_MBOX:
605 case SEND_RFC822:
606 case SEND_SHOW:
607 break;
609 break;
610 case MIME_TEXT_HTML:
611 case MIME_TEXT:
612 case MIME_TEXT_PLAIN:
613 switch (action) {
614 case SEND_TODISP:
615 case SEND_TODISP_ALL:
616 case SEND_QUOTE:
617 case SEND_QUOTE_ALL:
618 switch (mime_type_handler(&mh, ip, action)) {
619 case MIME_HDL_MSG:
620 _out(mh.mh_msg.s, mh.mh_msg.l, obuf, CONV_NONE, SEND_MBOX, qf,
621 stats, NULL, NULL);
622 /* We would print this as plain text, so better force going home */
623 goto jleave;
624 default:
625 break;
627 /* FALLTRHU */
628 default:
629 break;
631 break;
632 case MIME_DISCARD:
633 if (action != SEND_DECRYPT)
634 goto jleave;
635 break;
636 case MIME_PKCS7:
637 if (action != SEND_MBOX && action != SEND_RFC822 &&
638 action != SEND_SHOW && ip->m_multipart != NULL)
639 goto jmulti;
640 /* FALLTHRU */
641 default:
642 switch (action) {
643 case SEND_TODISP:
644 case SEND_TODISP_ALL:
645 case SEND_QUOTE:
646 case SEND_QUOTE_ALL:
647 switch (mime_type_handler(&mh, ip, action)) {
648 case MIME_HDL_MSG:
649 _out(mh.mh_msg.s, mh.mh_msg.l, obuf, CONV_NONE, SEND_MBOX, qf,
650 stats, NULL, NULL);
651 /* We would print this as plain text, so better force going home */
652 goto jleave;
653 case MIME_HDL_CMD:
654 /* FIXME WE NEED TO DO THAT IF WE ARE THE ONLY MAIL
655 * FIXME CONTENT !! */
656 case MIME_HDL_TEXT:
657 break;
658 default:
659 case MIME_HDL_NULL:
660 if (level == 0 && cnt) {
661 char const *x = _("[-- Binary content --]\n");
662 _out(x, strlen(x), obuf, CONV_NONE, SEND_MBOX, qf, stats,
663 NULL,NULL);
665 goto jleave;
667 break;
668 case SEND_TOFILE:
669 case SEND_TOPIPE:
670 case SEND_TOSRCH:
671 case SEND_DECRYPT:
672 case SEND_MBOX:
673 case SEND_RFC822:
674 case SEND_SHOW:
675 break;
677 break;
678 case MIME_ALTERNATIVE:
679 if ((action == SEND_TODISP || action == SEND_QUOTE) &&
680 !ok_blook(print_alternatives)) {
681 /* XXX This (a) should not remain (b) should be own fun
682 * TODO (despite the fact that v15 will do this completely differently
683 * TODO by having an action-specific "manager" that will traverse the
684 * TODO parsed MIME tree and decide for each part whether it'll be
685 * TODO displayed or not *before* we walk the tree for doing action */
686 struct mpstack {
687 struct mpstack *outer;
688 struct mimepart *mp;
689 } outermost, * volatile curr, * volatile mpsp;
690 bool_t volatile neednl, hadpart;
691 struct n_sigman smalter;
693 (curr = &outermost)->outer = NULL;
694 curr->mp = ip;
695 neednl = hadpart = FAL0;
697 n_SIGMAN_ENTER_SWITCH(&smalter, n_SIGMAN_ALL) {
698 case 0:
699 break;
700 default:
701 rv = -1;
702 goto jalter_leave;
705 for (np = ip->m_multipart;;) {
706 jalter_redo:
707 for (; np != NULL; np = np->m_nextpart) {
708 if (action != SEND_QUOTE && np->m_ct_type_plain != NULL) {
709 if (neednl)
710 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats,
711 NULL, NULL);
712 _print_part_info(obuf, np, doitp, level, qf, stats);
714 neednl = TRU1;
716 switch (np->m_mimecontent) {
717 case MIME_ALTERNATIVE:
718 case MIME_RELATED:
719 case MIME_DIGEST:
720 case MIME_SIGNED:
721 case MIME_ENCRYPTED:
722 case MIME_MULTI:
723 mpsp = salloc(sizeof *mpsp);
724 mpsp->outer = curr;
725 mpsp->mp = np->m_multipart;
726 curr->mp = np;
727 curr = mpsp;
728 np = mpsp->mp;
729 neednl = FAL0;
730 goto jalter_redo;
731 default:
732 if (hadpart)
733 break;
734 switch (mime_type_handler(&mh, np, action)) {
735 default:
736 mh.mh_flags = MIME_HDL_NULL;
737 continue; /* break; break; */
738 case MIME_HDL_PTF:
739 if (!ok_blook(mime_alternative_favour_rich)) {/* TODO */
740 struct mimepart *x = np;
742 while ((x = x->m_nextpart) != NULL) {
743 struct mime_handler mhx;
745 if (x->m_mimecontent == MIME_TEXT_PLAIN ||
746 mime_type_handler(&mhx, x, action) ==
747 MIME_HDL_TEXT)
748 break;
750 if (x != NULL)
751 continue; /* break; break; */
752 goto jalter_plain;
754 /* FALLTHRU */
755 case MIME_HDL_TEXT:
756 break;
758 /* FALLTHRU */
759 case MIME_TEXT_PLAIN:
760 if (hadpart)
761 break;
762 if (ok_blook(mime_alternative_favour_rich)) { /* TODO */
763 struct mimepart *x = np;
765 /* TODO twice TODO, we should dive into /related and
766 * TODO check whether that has rich parts! */
767 while ((x = x->m_nextpart) != NULL) {
768 struct mime_handler mhx;
770 switch (mime_type_handler(&mhx, x, action)) {
771 case MIME_HDL_PTF:
772 break;
773 default:
774 continue;
776 break;
778 if (x != NULL)
779 continue; /* break; break; */
781 jalter_plain:
782 quoteflt_flush(qf);
783 if (action == SEND_QUOTE && hadpart) {
784 struct quoteflt *dummy = quoteflt_dummy();
785 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, dummy, stats,
786 NULL,NULL);
787 quoteflt_flush(dummy);
789 hadpart = TRU1;
790 neednl = FAL0;
791 rv = sendpart(zmp, np, obuf, doitp, qf, action, stats,
792 level + 1);
793 quoteflt_reset(qf, origobuf);
795 if (rv < 0)
796 curr = &outermost; /* Cause overall loop termination */
797 break;
801 mpsp = curr->outer;
802 if (mpsp == NULL)
803 break;
804 curr = mpsp;
805 np = curr->mp->m_nextpart;
807 jalter_leave:
808 n_sigman_leave(&smalter, n_SIGMAN_VIPSIGS_NTTYOUT);
809 goto jleave;
811 /* FALLTHRU */
812 case MIME_RELATED:
813 case MIME_DIGEST:
814 case MIME_SIGNED:
815 case MIME_ENCRYPTED:
816 case MIME_MULTI:
817 switch (action) {
818 case SEND_TODISP:
819 case SEND_TODISP_ALL:
820 case SEND_QUOTE:
821 case SEND_QUOTE_ALL:
822 case SEND_TOFILE:
823 case SEND_TOPIPE:
824 case SEND_TOSRCH:
825 case SEND_DECRYPT:
826 jmulti:
827 if ((action == SEND_TODISP || action == SEND_TODISP_ALL) &&
828 ip->m_multipart != NULL &&
829 ip->m_multipart->m_mimecontent == MIME_DISCARD &&
830 ip->m_multipart->m_nextpart == NULL) {
831 char const *x = _("[Missing multipart boundary - use show "
832 "to display the raw message]\n");
833 _out(x, strlen(x), obuf, CONV_NONE, SEND_MBOX, qf, stats,
834 NULL,NULL);
837 for (np = ip->m_multipart; np != NULL; np = np->m_nextpart) {
838 bool_t volatile ispipe;
840 if (np->m_mimecontent == MIME_DISCARD && action != SEND_DECRYPT)
841 continue;
843 ispipe = FAL0;
844 switch (action) {
845 case SEND_TOFILE:
846 if (np->m_partstring && !strcmp(np->m_partstring, "1"))
847 break;
848 stats = NULL;
849 /* TODO Always open multipart on /dev/null, it's a hack to be
850 * TODO able to dive into that structure, and still better
851 * TODO than asking the user for something stupid.
852 * TODO oh, wait, we did ask for a filename for this MIME mail,
853 * TODO and that outer container is useless anyway ;-P */
854 if (np->m_multipart != NULL) {
855 if ((obuf = Fopen("/dev/null", "w")) == NULL)
856 continue;
857 } else if ((obuf = newfile(np, &ispipe)) == NULL)
858 continue;
859 if (!ispipe)
860 break;
861 if (sigsetjmp(_send_pipejmp, 1)) {
862 rv = -1;
863 goto jpipe_close;
865 oldpipe = safe_signal(SIGPIPE, &_send_onpipe);
866 break;
867 case SEND_TODISP:
868 case SEND_TODISP_ALL:
869 if (ip->m_mimecontent != MIME_ALTERNATIVE &&
870 ip->m_mimecontent != MIME_RELATED &&
871 ip->m_mimecontent != MIME_DIGEST &&
872 ip->m_mimecontent != MIME_SIGNED &&
873 ip->m_mimecontent != MIME_ENCRYPTED &&
874 ip->m_mimecontent != MIME_MULTI)
875 break;
876 _print_part_info(obuf, np, doitp, level, qf, stats);
877 break;
878 case SEND_QUOTE:
879 case SEND_QUOTE_ALL:
880 case SEND_MBOX:
881 case SEND_RFC822:
882 case SEND_SHOW:
883 case SEND_TOSRCH:
884 case SEND_DECRYPT:
885 case SEND_TOPIPE:
886 break;
889 quoteflt_flush(qf);
890 if ((action == SEND_QUOTE || action == SEND_QUOTE_ALL) &&
891 np->m_multipart == NULL && ip->m_parent != NULL) {
892 struct quoteflt *dummy = quoteflt_dummy();
893 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, dummy, stats,
894 NULL,NULL);
895 quoteflt_flush(dummy);
897 if (sendpart(zmp, np, obuf, doitp, qf, action, stats, level+1) < 0)
898 rv = -1;
899 quoteflt_reset(qf, origobuf);
901 if (action == SEND_QUOTE) {
902 if (ip->m_mimecontent != MIME_RELATED)
903 break;
905 if (action == SEND_TOFILE && obuf != origobuf) {
906 if (!ispipe)
907 Fclose(obuf);
908 else {
909 jpipe_close:
910 safe_signal(SIGPIPE, SIG_IGN);
911 Pclose(obuf, TRU1);
912 safe_signal(SIGPIPE, oldpipe);
916 goto jleave;
917 case SEND_MBOX:
918 case SEND_RFC822:
919 case SEND_SHOW:
920 break;
922 break;
925 /* Copy out message body */
926 if (doitp == n_IGNORE_ALL && level == 0) /* skip final blank line */
927 --cnt;
928 switch (ip->m_mime_enc) {
929 case MIMEE_BIN:
930 case MIMEE_7B:
931 case MIMEE_8B:
932 convert = CONV_NONE;
933 break;
934 case MIMEE_QP:
935 convert = CONV_FROMQP;
936 break;
937 case MIMEE_B64:
938 switch (ip->m_mimecontent) {
939 case MIME_TEXT:
940 case MIME_TEXT_PLAIN:
941 case MIME_TEXT_HTML:
942 convert = CONV_FROMB64_T;
943 break;
944 default:
945 switch (mh.mh_flags & MIME_HDL_TYPE_MASK) {
946 case MIME_HDL_TEXT:
947 case MIME_HDL_PTF:
948 convert = CONV_FROMB64_T;
949 break;
950 default:
951 convert = CONV_FROMB64;
952 break;
954 break;
956 break;
957 default:
958 convert = CONV_NONE;
961 /* TODO Unless we have filters, ensure iconvd==-1 so that mime.c:fwrite_td()
962 * TODO cannot mess things up misusing outrest as line buffer */
963 #ifdef HAVE_ICONV
964 if (iconvd != (iconv_t)-1) {
965 n_iconv_close(iconvd);
966 iconvd = (iconv_t)-1;
968 #endif
970 if (action == SEND_DECRYPT || action == SEND_MBOX ||
971 action == SEND_RFC822 || action == SEND_SHOW)
972 convert = CONV_NONE;
973 #ifdef HAVE_ICONV
974 else if ((action == SEND_TODISP || action == SEND_TODISP_ALL ||
975 action == SEND_QUOTE || action == SEND_QUOTE_ALL ||
976 action == SEND_TOSRCH || action == SEND_TOFILE) &&
977 (ip->m_mimecontent == MIME_TEXT_PLAIN ||
978 ip->m_mimecontent == MIME_TEXT_HTML ||
979 ip->m_mimecontent == MIME_TEXT ||
980 (mh.mh_flags & MIME_HDL_TYPE_MASK) == MIME_HDL_TEXT ||
981 (mh.mh_flags & MIME_HDL_TYPE_MASK) == MIME_HDL_PTF)) {
982 char const *tcs;
984 tcs = ok_vlook(ttycharset);
985 if (asccasecmp(tcs, ip->m_charset) &&
986 asccasecmp(ok_vlook(charset_7bit), ip->m_charset)) {
987 iconvd = n_iconv_open(tcs, ip->m_charset);
988 if (iconvd == (iconv_t)-1 && errno == EINVAL) {
989 n_err(_("Cannot convert from %s to %s\n"), ip->m_charset, tcs);
990 /*rv = 1; goto jleave;*/
994 #endif
996 switch (mh.mh_flags & MIME_HDL_TYPE_MASK) {
997 case MIME_HDL_CMD:
998 case MIME_HDL_PTF:
999 tmpname = NULL;
1000 qbuf = obuf;
1002 term_infd = COMMAND_FD_PASS;
1003 if (mh.mh_flags & (MIME_HDL_TMPF | MIME_HDL_NEEDSTERM)) {
1004 enum oflags of;
1006 of = OF_RDWR | OF_REGISTER;
1007 if (!(mh.mh_flags & MIME_HDL_TMPF)) {
1008 term_infd = 0;
1009 mh.mh_flags |= MIME_HDL_TMPF_FILL;
1010 of |= OF_UNLINK;
1011 } else if (mh.mh_flags & MIME_HDL_TMPF_UNLINK)
1012 of |= OF_REGISTER_UNLINK;
1014 if ((pbuf = Ftmp((mh.mh_flags & MIME_HDL_TMPF ? &cp : NULL),
1015 (mh.mh_flags & MIME_HDL_TMPF_FILL ? "mimehdlfill" : "mimehdl"),
1016 of)) == NULL)
1017 goto jesend;
1019 if (mh.mh_flags & MIME_HDL_TMPF) {
1020 tmpname = savestr(cp);
1021 Ftmp_free(&cp);
1024 if (mh.mh_flags & MIME_HDL_TMPF_FILL) {
1025 if (term_infd == 0)
1026 term_infd = fileno(pbuf);
1027 goto jsend;
1031 jpipe_for_real:
1032 pbuf = _pipefile(&mh, ip, n_UNVOLATILE(&qbuf), tmpname, term_infd);
1033 if (pbuf == NULL) {
1034 jesend:
1035 pbuf = qbuf = NULL;
1036 rv = -1;
1037 goto jend;
1038 } else if ((mh.mh_flags & MIME_HDL_NEEDSTERM) && pbuf == (FILE*)-1) {
1039 pbuf = qbuf = NULL;
1040 goto jend;
1042 tmpname = NULL;
1043 action = SEND_TOPIPE;
1044 if (pbuf != qbuf) {
1045 oldpipe = safe_signal(SIGPIPE, &_send_onpipe);
1046 if (sigsetjmp(_send_pipejmp, 1))
1047 goto jend;
1049 break;
1051 default:
1052 mh.mh_flags = MIME_HDL_NULL;
1053 pbuf = qbuf = obuf;
1054 break;
1057 jsend:
1059 bool_t volatile eof;
1060 ui32_t save_qf_pfix_len = qf->qf_pfix_len;
1061 ui64_t *save_stats = stats;
1063 if (pbuf != origobuf) {
1064 qf->qf_pfix_len = 0; /* XXX legacy (remove filter instead) */
1065 stats = NULL;
1067 eof = FAL0;
1068 outrest.s = inrest.s = NULL;
1069 outrest.l = inrest.l = 0;
1071 if (pbuf == qbuf) {
1072 __sendp_sig = 0;
1073 __sendp_opipe = safe_signal(SIGPIPE, &__sendp_onsig);
1074 if (sigsetjmp(__sendp_actjmp, 1)) {
1075 if (outrest.s != NULL)
1076 free(outrest.s);
1077 if (inrest.s != NULL)
1078 free(inrest.s);
1079 free(line);
1080 #ifdef HAVE_ICONV
1081 if (iconvd != (iconv_t)-1)
1082 n_iconv_close(iconvd);
1083 #endif
1084 safe_signal(SIGPIPE, __sendp_opipe);
1085 n_raise(__sendp_sig);
1089 quoteflt_reset(qf, pbuf);
1090 while (!eof && fgetline(&line, &linesize, &cnt, &linelen, ibuf, 0)) {
1091 joutln:
1092 if (_out(line, linelen, pbuf, convert, action, qf, stats, &outrest,
1093 (action & _TD_EOF ? NULL : &inrest)) < 0 || ferror(pbuf)) {
1094 rv = -1; /* XXX Should bail away?! */
1095 break;
1098 if(eof <= FAL0 && rv >= 0 && (outrest.l != 0 || inrest.l != 0)){
1099 linelen = 0;
1100 if(eof || inrest.l == 0)
1101 action |= _TD_EOF;
1102 eof = eof ? TRU1 : TRUM1;
1103 goto joutln;
1105 action &= ~_TD_EOF;
1107 /* TODO HACK: when sending to the display we yet get fooled if a message
1108 * TODO doesn't end in a newline, because of our input/output 1:1.
1109 * TODO This should be handled automatically by a display filter, then */
1110 if(rv >= 0 && !qf->qf_nl_last &&
1111 (action == SEND_TODISP || action == SEND_TODISP_ALL))
1112 rv = quoteflt_push(qf, "\n", 1);
1114 quoteflt_flush(qf);
1116 if (rv >= 0 && (mh.mh_flags & MIME_HDL_TMPF_FILL)) {
1117 mh.mh_flags &= ~MIME_HDL_TMPF_FILL;
1118 fflush(pbuf);
1119 really_rewind(pbuf);
1120 /* Don't Fclose() the Ftmp() thing due to OF_REGISTER_UNLINK++ */
1121 goto jpipe_for_real;
1124 if (pbuf == qbuf)
1125 safe_signal(SIGPIPE, __sendp_opipe);
1127 if (outrest.s != NULL)
1128 free(outrest.s);
1129 if (inrest.s != NULL)
1130 free(inrest.s);
1132 if (pbuf != origobuf) {
1133 qf->qf_pfix_len = save_qf_pfix_len;
1134 stats = save_stats;
1138 jend:
1139 if (line != NULL)
1140 free(line);
1141 if (pbuf != qbuf) {
1142 safe_signal(SIGPIPE, SIG_IGN);
1143 Pclose(pbuf, !(mh.mh_flags & MIME_HDL_ASYNC));
1144 safe_signal(SIGPIPE, oldpipe);
1145 if (rv >= 0 && qbuf != NULL && qbuf != obuf)
1146 pipecpy(qbuf, obuf, origobuf, qf, stats);
1148 #ifdef HAVE_ICONV
1149 if (iconvd != (iconv_t)-1)
1150 n_iconv_close(iconvd);
1151 #endif
1152 jleave:
1153 NYD_LEAVE;
1154 return rv;
1157 static FILE *
1158 newfile(struct mimepart *ip, bool_t volatile *ispipe)
1160 struct str in, out;
1161 char *f;
1162 FILE *fp;
1163 NYD_ENTER;
1165 f = ip->m_filename;
1166 *ispipe = FAL0;
1168 if (f != NULL && f != (char*)-1) {
1169 in.s = f;
1170 in.l = strlen(f);
1171 makeprint(&in, &out);
1172 out.l = delctrl(out.s, out.l);
1173 f = savestrbuf(out.s, out.l);
1174 free(out.s);
1177 /* In interactive mode, let user perform all kind of expansions as desired,
1178 * and offer |SHELL-SPEC pipe targets, too */
1179 if (n_psonce & n_PSO_INTERACTIVE) {
1180 struct str prompt;
1181 struct n_string shou, *shoup;
1182 char *f2, *f3;
1184 shoup = n_string_creat_auto(&shou);
1186 /* TODO Generic function which asks for filename.
1187 * TODO If the current part is the first textpart the target
1188 * TODO is implicit from outer `write' etc! */
1189 /* I18N: Filename input prompt with file type indication */
1190 str_concat_csvl(&prompt, _("Enter filename for part "),
1191 (ip->m_partstring != NULL) ? ip->m_partstring : _("?"),
1192 _(" ("), ip->m_ct_type_plain, _("): "), NULL);
1193 jgetname:
1194 f2 = n_lex_input_cp(n_LEXINPUT_CTX_DEFAULT | n_LEXINPUT_HIST_ADD,
1195 prompt.s, ((f != (char*)-1 && f != NULL)
1196 ? n_shexp_quote_cp(f, FAL0) : NULL));
1197 if(f2 != NULL){
1198 in.s = n_UNCONST(f2);
1199 in.l = UIZ_MAX;
1200 if((n_shexp_parse_token((n_SHEXP_PARSE_TRUNC |
1201 n_SHEXP_PARSE_TRIMSPACE | n_SHEXP_PARSE_LOG |
1202 n_SHEXP_PARSE_IGNORE_EMPTY), shoup, &in, NULL
1203 ) & (n_SHEXP_STATE_STOP |
1204 n_SHEXP_STATE_OUTPUT | n_SHEXP_STATE_ERR_MASK)
1205 ) != (n_SHEXP_STATE_STOP | n_SHEXP_STATE_OUTPUT))
1206 goto jgetname;
1207 f2 = n_string_cp(shoup);
1209 if (f2 == NULL || *f2 == '\0') {
1210 if (n_poption & n_PO_D_V)
1211 n_err(_("... skipping this\n"));
1212 n_string_gut(shoup);
1213 fp = NULL;
1214 goto jleave;
1217 if (*f2 == '|')
1218 /* Pipes are expanded by the shell */
1219 f = f2;
1220 else if ((f3 = fexpand(f2, FEXP_LOCAL | FEXP_NVAR)) == NULL)
1221 /* (Error message written by fexpand()) */
1222 goto jgetname;
1223 else
1224 f = f3;
1226 n_string_gut(shoup);
1229 if (f == NULL || f == (char*)-1 || *f == '\0')
1230 fp = NULL;
1231 else if (n_psonce & n_PSO_INTERACTIVE) {
1232 if (*f == '|') {
1233 fp = Popen(&f[1], "w", ok_vlook(SHELL), NULL, 1);
1234 if (!(*ispipe = (fp != NULL)))
1235 n_perr(f, 0);
1236 } else if ((fp = Fopen(f, "w")) == NULL)
1237 n_err(_("Cannot open %s\n"), n_shexp_quote_cp(f, FAL0));
1238 } else {
1239 /* Be very picky in non-interactive mode: actively disallow pipes,
1240 * prevent directory separators, and any filename member that would
1241 * become expanded by the shell if the name would be echo(1)ed */
1242 if(anyof(f, "/" n_SHEXP_MAGIC_PATH_CHARS)){
1243 char c;
1245 for(out.s = salloc((strlen(f) * 3) +1), out.l = 0; (c = *f++) != '\0';)
1246 if(strchr("/" n_SHEXP_MAGIC_PATH_CHARS, c)){
1247 out.s[out.l++] = '%';
1248 n_c_to_hex_base16(&out.s[out.l], c);
1249 out.l += 2;
1250 }else
1251 out.s[out.l++] = c;
1252 out.s[out.l] = '\0';
1253 f = out.s;
1256 /* Avoid overwriting of existing files */
1257 while((fp = Fopen(f, "wx")) == NULL){
1258 int e;
1260 if((e = errno) != EEXIST){
1261 n_err(_("Cannot open %s: %s\n"),
1262 n_shexp_quote_cp(f, FAL0), strerror(e));
1263 break;
1266 if(ip->m_partstring != NULL)
1267 f = savecatsep(f, '#', ip->m_partstring);
1268 else
1269 f = savecat(f, "#.");
1272 jleave:
1273 NYD_LEAVE;
1274 return fp;
1277 static void
1278 pipecpy(FILE *pipebuf, FILE *outbuf, FILE *origobuf, struct quoteflt *qf,
1279 ui64_t *stats)
1281 char *line = NULL; /* TODO line pool */
1282 size_t linesize = 0, linelen, cnt;
1283 ssize_t all_sz, sz;
1284 NYD_ENTER;
1286 fflush(pipebuf);
1287 rewind(pipebuf);
1288 cnt = (size_t)fsize(pipebuf);
1289 all_sz = 0;
1291 quoteflt_reset(qf, outbuf);
1292 while (fgetline(&line, &linesize, &cnt, &linelen, pipebuf, 0) != NULL) {
1293 if ((sz = quoteflt_push(qf, line, linelen)) < 0)
1294 break;
1295 all_sz += sz;
1297 if ((sz = quoteflt_flush(qf)) > 0)
1298 all_sz += sz;
1299 if (line)
1300 free(line);
1302 if (all_sz > 0 && outbuf == origobuf && stats != NULL)
1303 *stats += all_sz;
1304 Fclose(pipebuf);
1305 NYD_LEAVE;
1308 static void
1309 statusput(const struct message *mp, FILE *obuf, struct quoteflt *qf,
1310 ui64_t *stats)
1312 char statout[3], *cp = statout;
1313 NYD_ENTER;
1315 if (mp->m_flag & MREAD)
1316 *cp++ = 'R';
1317 if (!(mp->m_flag & MNEW))
1318 *cp++ = 'O';
1319 *cp = 0;
1320 if (statout[0]) {
1321 int i = fprintf(obuf, "%.*sStatus: %s\n", (int)qf->qf_pfix_len,
1322 (qf->qf_pfix_len > 0 ? qf->qf_pfix : 0), statout);
1323 if (i > 0 && stats != NULL)
1324 *stats += i;
1326 NYD_LEAVE;
1329 static void
1330 xstatusput(const struct message *mp, FILE *obuf, struct quoteflt *qf,
1331 ui64_t *stats)
1333 char xstatout[4];
1334 char *xp = xstatout;
1335 NYD_ENTER;
1337 if (mp->m_flag & MFLAGGED)
1338 *xp++ = 'F';
1339 if (mp->m_flag & MANSWERED)
1340 *xp++ = 'A';
1341 if (mp->m_flag & MDRAFTED)
1342 *xp++ = 'T';
1343 *xp = 0;
1344 if (xstatout[0]) {
1345 int i = fprintf(obuf, "%.*sX-Status: %s\n", (int)qf->qf_pfix_len,
1346 (qf->qf_pfix_len > 0 ? qf->qf_pfix : 0), xstatout);
1347 if (i > 0 && stats != NULL)
1348 *stats += i;
1350 NYD_LEAVE;
1353 static void
1354 put_from_(FILE *fp, struct mimepart *ip, ui64_t *stats)
1356 char const *froma, *date, *nl;
1357 int i;
1358 NYD_ENTER;
1360 if (ip != NULL && ip->m_from != NULL) {
1361 froma = ip->m_from;
1362 date = fakedate(ip->m_time);
1363 nl = "\n";
1364 } else {
1365 froma = ok_vlook(LOGNAME);
1366 date = time_current.tc_ctime;
1367 nl = n_empty;
1370 n_COLOUR( n_colour_put(fp, n_COLOUR_ID_VIEW_FROM_, NULL); )
1371 i = fprintf(fp, "From %s %s%s", froma, date, nl);
1372 n_COLOUR( n_colour_reset(fp); )
1373 if (i > 0 && stats != NULL)
1374 *stats += i;
1375 NYD_LEAVE;
1378 FL int
1379 sendmp(struct message *mp, FILE *obuf, struct n_ignore const *doitp,
1380 char const *prefix, enum sendaction action, ui64_t *stats)
1382 struct quoteflt qf;
1383 size_t cnt, sz, i;
1384 FILE *ibuf;
1385 enum mime_parse_flags mpf;
1386 struct mimepart *ip;
1387 int rv = -1, c;
1388 NYD_ENTER;
1390 time_current_update(&time_current, TRU1);
1392 if (mp == dot && action != SEND_TOSRCH)
1393 n_pstate |= n_PS_DID_PRINT_DOT;
1394 if (stats != NULL)
1395 *stats = 0;
1396 quoteflt_init(&qf, prefix);
1398 /* First line is the From_ line, so no headers there to worry about */
1399 if ((ibuf = setinput(&mb, mp, NEED_BODY)) == NULL)
1400 goto jleave;
1402 cnt = mp->m_size;
1403 sz = 0;
1405 bool_t nozap;
1406 char const *cpre = n_empty, *csuf = n_empty;
1407 #ifdef HAVE_COLOUR
1408 struct n_colour_pen *cpen = n_colour_pen_create(n_COLOUR_ID_VIEW_FROM_,NULL);
1409 struct str const *sp = n_colour_pen_to_str(cpen);
1411 if (sp != NULL) {
1412 cpre = sp->s;
1413 sp = n_colour_reset_to_str();
1414 if (sp != NULL)
1415 csuf = sp->s;
1417 #endif
1419 nozap = (doitp != n_IGNORE_ALL && doitp != n_IGNORE_FWD &&
1420 action != SEND_RFC822 &&
1421 !n_ignore_is_ign(doitp, "from_", sizeof("from_") -1));
1422 if (mp->m_flag & MNOFROM) {
1423 if (nozap)
1424 sz = fprintf(obuf, "%s%.*sFrom %s %s%s\n",
1425 cpre, (int)qf.qf_pfix_len,
1426 (qf.qf_pfix_len != 0 ? qf.qf_pfix : n_empty), fakefrom(mp),
1427 fakedate(mp->m_time), csuf);
1428 } else if (nozap) {
1429 if (qf.qf_pfix_len > 0) {
1430 i = fwrite(qf.qf_pfix, sizeof *qf.qf_pfix, qf.qf_pfix_len, obuf);
1431 if (i != qf.qf_pfix_len)
1432 goto jleave;
1433 sz += i;
1435 #ifdef HAVE_COLOUR
1436 if (cpre != NULL) {
1437 fputs(cpre, obuf);
1438 cpre = (char const*)0x1;
1440 #endif
1442 while (cnt > 0 && (c = getc(ibuf)) != EOF) {
1443 #ifdef HAVE_COLOUR
1444 if (c == '\n' && csuf != NULL) {
1445 cpre = (char const*)0x1;
1446 fputs(csuf, obuf);
1448 #endif
1449 putc(c, obuf);
1450 ++sz;
1451 --cnt;
1452 if (c == '\n')
1453 break;
1456 #ifdef HAVE_COLOUR
1457 if (csuf != NULL && cpre != (char const*)0x1)
1458 fputs(csuf, obuf);
1459 #endif
1460 } else {
1461 while (cnt > 0 && (c = getc(ibuf)) != EOF) {
1462 --cnt;
1463 if (c == '\n')
1464 break;
1468 if (sz > 0 && stats != NULL)
1469 *stats += sz;
1471 mpf = MIME_PARSE_NONE;
1472 if (action != SEND_MBOX && action != SEND_RFC822 && action != SEND_SHOW)
1473 mpf |= MIME_PARSE_PARTS | MIME_PARSE_DECRYPT;
1474 if ((ip = mime_parse_msg(mp, mpf)) == NULL)
1475 goto jleave;
1477 rv = sendpart(mp, ip, obuf, doitp, &qf, action, stats, 0);
1478 jleave:
1479 quoteflt_destroy(&qf);
1480 NYD_LEAVE;
1481 return rv;
1484 /* s-it-mode */