Add *mime-counter-evidence* bit 4 (Aharon Robbins)..
[s-mailx.git] / send.c
blob2061b5b060379cb544c08c02436484babfb2678e
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Mail to mail folders and displays.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
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 ignoretab *doign, int level,
47 struct quoteflt *qf, ui64_t *stats);
49 /* Create a pipe; if mpp is not NULL, place some NAILENV_* environment
50 * variables accordingly */
51 static FILE * _pipefile(struct mime_handler *mhp,
52 struct mimepart const *mpp, FILE **qbuf,
53 char const *tmpfile, int term_infd);
55 /* Call mime_write() as approbiate and adjust statistics */
56 SINLINE ssize_t _out(char const *buf, size_t len, FILE *fp,
57 enum conversion convert, enum sendaction action,
58 struct quoteflt *qf, ui64_t *stats, struct str *rest);
60 /* SIGPIPE handler */
61 static void _send_onpipe(int signo);
63 /* Send one part */
64 static int sendpart(struct message *zmp, struct mimepart *ip,
65 FILE *obuf, struct ignoretab *doign,
66 struct quoteflt *qf, enum sendaction action,
67 ui64_t *stats, int level);
69 /* Get a file for an attachment */
70 static FILE * newfile(struct mimepart *ip, bool_t *ispipe);
72 static void pipecpy(FILE *pipebuf, FILE *outbuf, FILE *origobuf,
73 struct quoteflt *qf, ui64_t *stats);
75 /* Output a reasonable looking status field */
76 static void statusput(const struct message *mp, FILE *obuf,
77 struct quoteflt *qf, ui64_t *stats);
78 static void xstatusput(const struct message *mp, FILE *obuf,
79 struct quoteflt *qf, ui64_t *stats);
81 static void put_from_(FILE *fp, struct mimepart *ip, ui64_t *stats);
83 static void
84 _print_part_info(FILE *obuf, struct mimepart const *mpp, /* TODO strtofmt.. */
85 struct ignoretab *doign, int level, struct quoteflt *qf, ui64_t *stats)
87 char buf[64];
88 struct str ti = {NULL, 0}, to;
89 struct str const *cpre, *csuf;
90 char const *cp;
91 NYD2_ENTER;
93 #ifdef HAVE_COLOUR
94 cpre = colour_get(COLOURSPEC_PARTINFO);
95 csuf = colour_get(COLOURSPEC_RESET);
96 #else
97 cpre = csuf = NULL;
98 #endif
100 /* Take care of "99.99", i.e., 5 */
101 if ((cp = mpp->m_partstring) == NULL || cp[0] == '\0')
102 cp = "?";
103 if (level || (cp[0] != '1' && cp[1] == '\0'))
104 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
105 if (cpre != NULL)
106 _out(cpre->s, cpre->l, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
107 _out("[-- #", 5, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
108 _out(cp, strlen(cp), obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
110 to.l = snprintf(buf, sizeof buf, " %" PRIuZ "/%" PRIuZ " ",
111 (uiz_t)mpp->m_lines, (uiz_t)mpp->m_size);
112 _out(buf, to.l, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
114 if ((cp = mpp->m_ct_type_usr_ovwr) != NULL)
115 _out("+", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
116 else
117 cp = mpp->m_ct_type_plain;
118 if ((to.l = strlen(cp)) > 30 && is_asccaseprefix(cp, "application/")) {
119 size_t const al = sizeof("appl../") -1, fl = sizeof("application/") -1;
120 size_t i = to.l - fl;
121 char *x = salloc(al + i +1);
123 memcpy(x, "appl../", al);
124 memcpy(x + al, cp + fl, i +1);
125 cp = x;
126 to.l = al + i;
128 _out(cp, to.l, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
130 if (mpp->m_multipart == NULL/* TODO */ && (cp = mpp->m_ct_enc) != NULL) {
131 _out(", ", 2, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
132 if (to.l > 25 && !asccasecmp(cp, "quoted-printable"))
133 cp = "qu.-pr.";
134 _out(cp, strlen(cp), obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
137 if (mpp->m_multipart == NULL/* TODO */ && (cp = mpp->m_charset) != NULL) {
138 _out(", ", 2, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
139 _out(cp, strlen(cp), obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
142 _out(" --]", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
143 if (csuf != NULL)
144 _out(csuf->s, csuf->l, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
145 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
147 if (is_ign("content-disposition", 19, doign) && mpp->m_filename != NULL &&
148 *mpp->m_filename != '\0') {
149 makeprint(n_str_add_cp(&ti, mpp->m_filename), &to);
150 free(ti.s);
151 to.l = delctrl(to.s, to.l);
153 if (cpre != NULL)
154 _out(cpre->s, cpre->l, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
155 _out("[-- ", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
156 _out(to.s, to.l, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
157 _out(" --]", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
158 if (csuf != NULL)
159 _out(csuf->s, csuf->l, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
160 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
162 free(to.s);
164 NYD2_LEAVE;
167 static FILE *
168 _pipefile(struct mime_handler *mhp, struct mimepart const *mpp, FILE **qbuf,
169 char const *tmpfile, int term_infd)
171 struct str s;
172 char const *env_addon[8], *cp, *sh;
173 FILE *rbuf;
174 NYD_ENTER;
176 rbuf = *qbuf;
178 if (mhp->mh_flags & MIME_HDL_ISQUOTE) {
179 if ((*qbuf = Ftmp(NULL, "sendp", OF_RDWR | OF_UNLINK | OF_REGISTER,
180 0600)) == NULL) {
181 n_perr(_("tmpfile"), 0);
182 *qbuf = rbuf;
186 if ((mhp->mh_flags & MIME_HDL_TYPE_MASK) == MIME_HDL_PTF) {
187 union {int (*ptf)(void); char const *sh;} u;
189 fflush(*qbuf);
190 if (*qbuf != stdout) /* xxx never? v15: it'll be a filter anyway */
191 fflush(stdout);
193 u.ptf = mhp->mh_ptf;
194 if((rbuf = Popen((char*)-1, "W", u.sh, NULL, fileno(*qbuf))) == NULL)
195 goto jerror;
196 goto jleave;
199 /* NAIL_FILENAME */
200 if (mpp == NULL || (cp = mpp->m_filename) == NULL)
201 cp = "";
202 env_addon[0] = str_concat_csvl(&s, NAILENV_FILENAME, "=", cp, NULL)->s;
204 /* NAIL_FILENAME_GENERATED *//* TODO pathconf NAME_MAX; but user can create
205 * TODO a file wherever he wants! *Do* create a zero-size temporary file
206 * TODO and give *that* path as NAIL_FILENAME_TEMPORARY, clean it up once
207 * TODO the pipe returns? Like this we *can* verify path/name issues! */
208 env_addon[1] = str_concat_csvl(&s, NAILENV_FILENAME_GENERATED, "=",
209 getrandstring(MIN(NAME_MAX / 4, 16)), NULL)->s;
211 /* NAIL_CONTENT{,_EVIDENCE} */
212 if (mpp == NULL || (cp = mpp->m_ct_type_plain) == NULL)
213 cp = "";
214 env_addon[2] = str_concat_csvl(&s, NAILENV_CONTENT, "=", cp, NULL)->s;
216 if (mpp != NULL && mpp->m_ct_type_usr_ovwr != NULL)
217 cp = mpp->m_ct_type_usr_ovwr;
218 env_addon[3] = str_concat_csvl(&s, NAILENV_CONTENT_EVIDENCE, "=", cp,
219 NULL)->s;
221 env_addon[4] = str_concat_csvl(&s, NAILENV_TMPDIR, "=", tempdir, NULL)->s;
222 env_addon[5] = str_concat_csvl(&s, "TMPDIR", "=", tempdir, NULL)->s;
224 env_addon[6] = NULL;
226 /* NAIL_FILENAME_TEMPORARY? */
227 if (tmpfile != NULL) {
228 env_addon[6] = str_concat_csvl(&s, NAILENV_FILENAME_TEMPORARY, "=",
229 tmpfile, NULL)->s;
230 env_addon[7] = NULL;
233 if ((sh = ok_vlook(SHELL)) == NULL)
234 sh = XSHELL;
236 if (mhp->mh_flags & MIME_HDL_NEEDSTERM) {
237 sigset_t nset;
238 int pid;
240 sigemptyset(&nset);
241 pid = run_command(sh, NULL, term_infd, COMMAND_FD_PASS, "-c",
242 mhp->mh_shell_cmd, NULL, env_addon);
243 rbuf = (pid < 0) ? NULL : (FILE*)-1;
244 } else {
245 rbuf = Popen(mhp->mh_shell_cmd, "W", sh, env_addon,
246 (mhp->mh_flags & MIME_HDL_ASYNC ? -1 : fileno(*qbuf)));
247 jerror:
248 if (rbuf == NULL)
249 n_err(_("Cannot run MIME type handler \"%s\": %s\n"),
250 mhp->mh_msg, strerror(errno));
251 else {
252 fflush(*qbuf);
253 if (*qbuf != stdout)
254 fflush(stdout);
257 jleave:
258 NYD_LEAVE;
259 return rbuf;
262 SINLINE ssize_t
263 _out(char const *buf, size_t len, FILE *fp, enum conversion convert, enum
264 sendaction action, struct quoteflt *qf, ui64_t *stats, struct str *rest)
266 ssize_t sz = 0, n;
267 int flags;
268 NYD_ENTER;
270 #if 0
271 Well ... it turns out to not work like that since of course a valid
272 RFC 4155 compliant parser, like S-nail, takes care for From_ lines only
273 after an empty line has been seen, which cannot be detected that easily
274 right here!
275 ifdef HAVE_DEBUG /* TODO assert legacy */
276 /* TODO if at all, this CAN only happen for SEND_DECRYPT, since all
277 * TODO other input situations handle RFC 4155 OR, if newly generated,
278 * TODO enforce quoted-printable if there is From_, as "required" by
279 * TODO RFC 5751. The SEND_DECRYPT case is not yet overhauled;
280 * TODO if it may happen in this path, we should just treat decryption
281 * TODO as we do for the other input paths; i.e., handle it in SSL!! */
282 if (action == SEND_MBOX || action == SEND_DECRYPT)
283 assert(!is_head(buf, len, TRU1));
284 #else
285 if ((/*action == SEND_MBOX ||*/ action == SEND_DECRYPT) &&
286 is_head(buf, len, TRU1)) {
287 putc('>', fp);
288 ++sz;
290 #endif
292 flags = ((int)action & _TD_EOF);
293 action &= ~_TD_EOF;
294 n = mime_write(buf, len, fp,
295 action == SEND_MBOX ? CONV_NONE : convert,
296 flags | ((action == SEND_TODISP || action == SEND_TODISP_ALL ||
297 action == SEND_QUOTE || action == SEND_QUOTE_ALL)
298 ? TD_ISPR | TD_ICONV
299 : (action == SEND_TOSRCH || action == SEND_TOPIPE)
300 ? TD_ICONV : (action == SEND_SHOW ? TD_ISPR : TD_NONE)),
301 qf, rest);
302 if (n < 0)
303 sz = n;
304 else if (n > 0) {
305 sz += n;
306 if (stats != NULL)
307 *stats += sz;
309 NYD_LEAVE;
310 return sz;
313 static void
314 _send_onpipe(int signo)
316 NYD_X; /* Signal handler */
317 UNUSED(signo);
318 siglongjmp(_send_pipejmp, 1);
321 static sigjmp_buf __sendp_actjmp; /* TODO someday.. */
322 static int __sendp_sig; /* TODO someday.. */
323 static sighandler_type __sendp_opipe;
324 static void
325 __sendp_onsig(int sig) /* TODO someday, we won't need it no more */
327 NYD_X; /* Signal handler */
328 __sendp_sig = sig;
329 siglongjmp(__sendp_actjmp, 1);
332 static sigjmp_buf __sndalter_actjmp; /* TODO someday.. */
333 static int __sndalter_sig; /* TODO someday.. */
334 static void
335 __sndalter_onsig(int sig) /* TODO someday, we won't need it no more */
337 NYD_X; /* Signal handler */
338 __sndalter_sig = sig;
339 siglongjmp(__sndalter_actjmp, 1);
342 static int
343 sendpart(struct message *zmp, struct mimepart *ip, FILE * volatile obuf,
344 struct ignoretab *doign, struct quoteflt *qf,
345 enum sendaction volatile action, ui64_t * volatile stats, int level)
347 int volatile rv = 0;
348 struct mime_handler mh;
349 struct str rest;
350 char *line = NULL, *cp, *cp2, *start;
351 char const *tmpfile = NULL;
352 size_t linesize = 0, linelen, cnt;
353 int volatile term_infd;
354 int dostat, infld = 0, ignoring = 1, isenc, c;
355 struct mimepart *volatile np;
356 FILE * volatile ibuf = NULL, * volatile pbuf = obuf,
357 * volatile qbuf = obuf, *origobuf = obuf;
358 enum conversion volatile convert;
359 sighandler_type volatile oldpipe = SIG_DFL;
360 long lineno = 0;
361 NYD_ENTER;
363 if (ip->m_mimecontent == MIME_PKCS7 && ip->m_multipart &&
364 action != SEND_MBOX && action != SEND_RFC822 && action != SEND_SHOW)
365 goto jskip;
367 dostat = 0;
368 if (level == 0) {
369 if (doign != NULL) {
370 if (!is_ign("status", 6, doign))
371 dostat |= 1;
372 if (!is_ign("x-status", 8, doign))
373 dostat |= 2;
374 } else
375 dostat = 3;
377 if ((ibuf = setinput(&mb, (struct message*)ip, NEED_BODY)) == NULL) {
378 rv = -1;
379 goto jleave;
381 cnt = ip->m_size;
383 if (ip->m_mimecontent == MIME_DISCARD)
384 goto jskip;
386 if (!(ip->m_flag & MNOFROM))
387 while (cnt && (c = getc(ibuf)) != EOF) {
388 cnt--;
389 if (c == '\n')
390 break;
392 isenc = 0;
393 convert = (action == SEND_TODISP || action == SEND_TODISP_ALL ||
394 action == SEND_QUOTE || action == SEND_QUOTE_ALL ||
395 action == SEND_TOSRCH)
396 ? CONV_FROMHDR : CONV_NONE;
398 /* Work the headers */
399 quoteflt_reset(qf, obuf);
400 while (fgetline(&line, &linesize, &cnt, &linelen, ibuf, 0)) {
401 ++lineno;
402 if (line[0] == '\n') {
403 /* If line is blank, we've reached end of headers, so force out
404 * status: field and note that we are no longer in header fields */
405 if (dostat & 1)
406 statusput(zmp, obuf, qf, stats);
407 if (dostat & 2)
408 xstatusput(zmp, obuf, qf, stats);
409 if (doign != allignore)
410 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
411 break;
414 isenc &= ~1;
415 if (infld && blankchar(line[0])) {
416 /* If this line is a continuation (SP / HT) of a previous header
417 * field, determine if the start of the line is a MIME encoded word */
418 if (isenc & 2) {
419 for (cp = line; blankchar(*cp); ++cp);
420 if (cp > line && linelen - PTR2SIZE(cp - line) > 8 &&
421 cp[0] == '=' && cp[1] == '?')
422 isenc |= 1;
424 } else {
425 /* Pick up the header field if we have one */
426 for (cp = line; (c = *cp & 0377) && c != ':' && !spacechar(c); ++cp)
428 cp2 = cp;
429 while (spacechar(*cp))
430 ++cp;
431 if (cp[0] != ':' && level == 0 && lineno == 1) {
432 /* Not a header line, force out status: This happens in uucp style
433 * mail where there are no headers at all */
434 if (dostat & 1)
435 statusput(zmp, obuf, qf, stats);
436 if (dostat & 2)
437 xstatusput(zmp, obuf, qf, stats);
438 if (doign != allignore)
439 _out("\n", 1, obuf, CONV_NONE,SEND_MBOX, qf, stats, NULL);
440 break;
443 /* If it is an ignored field and we care about such things, skip it.
444 * Misuse dostat also for another bit xxx use a bitenum + for more */
445 if (ok_blook(keep_content_length))
446 dostat |= 1 << 2;
447 c = *cp2;
448 *cp2 = 0; /* temporarily null terminate */
449 if ((doign && is_ign(line, PTR2SIZE(cp2 - line), doign)) ||
450 (action == SEND_MBOX && !(dostat & (1 << 2)) &&
451 (!asccasecmp(line, "content-length") ||
452 !asccasecmp(line, "lines"))))
453 ignoring = 1;
454 else if (!asccasecmp(line, "status")) {
455 /* If field is "status," go compute and print real Status: field */
456 if (dostat & 1) {
457 statusput(zmp, obuf, qf, stats);
458 dostat &= ~1;
459 ignoring = 1;
461 } else if (!asccasecmp(line, "x-status")) {
462 /* If field is "status," go compute and print real Status: field */
463 if (dostat & 2) {
464 xstatusput(zmp, obuf, qf, stats);
465 dostat &= ~2;
466 ignoring = 1;
468 } else {
469 ignoring = 0;
470 /* For colourization we need the complete line, so save it */
471 /* XXX This is all temporary (colour belongs into backend), so
472 * XXX use tmpfile as a temporary storage in the meanwhile */
473 #ifdef HAVE_COLOUR
474 if (colour_table != NULL)
475 tmpfile = savestrbuf(line, PTR2SIZE(cp2 - line));
476 #endif
478 *cp2 = c;
479 dostat &= ~(1 << 2);
480 infld = 1;
483 /* Determine if the end of the line is a MIME encoded word */
484 /* TODO geeeh! all this lengthy stuff that follows is about is dealing
485 * TODO with header follow lines, and it should be up to the backend
486 * TODO what happens and what not, i.e., it doesn't matter wether it's
487 * TODO a MIME-encoded word or not, as long as a single separating space
488 * TODO remains in between lines (the MIME stuff will correctly remove
489 * TODO whitespace in between multiple adjacent encoded words) */
490 isenc &= ~2;
491 if (cnt && (c = getc(ibuf)) != EOF) {
492 if (blankchar(c)) {
493 cp = line + linelen - 1;
494 if (linelen > 0 && *cp == '\n')
495 --cp;
496 while (cp >= line && whitechar(*cp))
497 --cp;
498 if (PTR2SIZE(cp - line > 8) && cp[0] == '=' && cp[-1] == '?')
499 isenc |= 2;
501 ungetc(c, ibuf);
504 if (!ignoring) {
505 size_t len = linelen;
506 start = line;
507 if (action == SEND_TODISP || action == SEND_TODISP_ALL ||
508 action == SEND_QUOTE || action == SEND_QUOTE_ALL ||
509 action == SEND_TOSRCH) {
510 /* Strip blank characters if two MIME-encoded words follow on
511 * continuing lines */
512 if (isenc & 1)
513 while (len > 0 && blankchar(*start)) {
514 ++start;
515 --len;
517 if (isenc & 2)
518 if (len > 0 && start[len - 1] == '\n')
519 --len;
520 while (len > 0 && blankchar(start[len - 1]))
521 --len;
523 #ifdef HAVE_COLOUR
525 bool_t colour_stripped = FAL0;
526 if (tmpfile != NULL) {
527 colour_put_header(obuf, tmpfile);
528 if (len > 0 && start[len - 1] == '\n') {
529 colour_stripped = TRU1;
530 --len;
533 #endif
534 _out(start, len, obuf, convert, action, qf, stats, NULL);
535 #ifdef HAVE_COLOUR
536 if (tmpfile != NULL) {
537 colour_reset(obuf); /* XXX reset after \n!! */
538 if (colour_stripped)
539 putc('\n', obuf);
542 #endif
543 if (ferror(obuf)) {
544 free(line);
545 rv = -1;
546 goto jleave;
550 quoteflt_flush(qf);
551 free(line);
552 line = NULL;
553 tmpfile = NULL;
555 jskip:
556 memset(&mh, 0, sizeof mh);
558 switch (ip->m_mimecontent) {
559 case MIME_822:
560 switch (action) {
561 case SEND_TODISP:
562 case SEND_TODISP_ALL:
563 case SEND_QUOTE:
564 case SEND_QUOTE_ALL:
565 if (ok_blook(rfc822_body_from_)) {
566 if (qf->qf_pfix_len > 0) {
567 size_t i = fwrite(qf->qf_pfix, sizeof *qf->qf_pfix,
568 qf->qf_pfix_len, obuf);
569 if (i == qf->qf_pfix_len && stats != NULL)
570 *stats += i;
572 put_from_(obuf, ip->m_multipart, stats);
574 /* FALLTHRU */
575 case SEND_TOSRCH:
576 case SEND_DECRYPT:
577 goto jmulti;
578 case SEND_TOFILE:
579 case SEND_TOPIPE:
580 if (ok_blook(rfc822_body_from_))
581 put_from_(obuf, ip->m_multipart, stats);
582 /* FALLTHRU */
583 case SEND_MBOX:
584 case SEND_RFC822:
585 case SEND_SHOW:
586 break;
588 break;
589 case MIME_TEXT_HTML:
590 case MIME_TEXT:
591 case MIME_TEXT_PLAIN:
592 switch (action) {
593 case SEND_TODISP:
594 case SEND_TODISP_ALL:
595 case SEND_QUOTE:
596 case SEND_QUOTE_ALL:
597 switch (mime_type_handler(&mh, ip, action)) {
598 case MIME_HDL_MSG:
599 _out(mh.mh_msg.s, mh.mh_msg.l, obuf, CONV_NONE, SEND_MBOX, qf,
600 stats, NULL);
601 /* We would print this as plain text, so better force going home */
602 goto jleave;
603 default:
604 break;
606 /* FALLTRHU */
607 default:
608 break;
610 break;
611 case MIME_DISCARD:
612 if (action != SEND_DECRYPT)
613 goto jleave;
614 break;
615 case MIME_PKCS7:
616 if (action != SEND_MBOX && action != SEND_RFC822 &&
617 action != SEND_SHOW && ip->m_multipart != NULL)
618 goto jmulti;
619 /* FALLTHRU */
620 default:
621 switch (action) {
622 case SEND_TODISP:
623 case SEND_TODISP_ALL:
624 case SEND_QUOTE:
625 case SEND_QUOTE_ALL:
626 switch (mime_type_handler(&mh, ip, action)) {
627 case MIME_HDL_MSG:
628 _out(mh.mh_msg.s, mh.mh_msg.l, obuf, CONV_NONE, SEND_MBOX, qf,
629 stats, NULL);
630 /* We would print this as plain text, so better force going home */
631 goto jleave;
632 case MIME_HDL_CMD:
633 /* FIXME WE NEED TO DO THAT IF WE ARE THE ONLY MAIL
634 * FIXME CONTENT !! */
635 case MIME_HDL_TEXT:
636 break;
637 default:
638 case MIME_HDL_NULL:
639 if (level == 0 && cnt) {
640 char const *x = _("[-- Binary content --]\n");
641 _out(x, strlen(x), obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
643 goto jleave;
645 break;
646 case SEND_TOFILE:
647 case SEND_TOPIPE:
648 case SEND_TOSRCH:
649 case SEND_DECRYPT:
650 case SEND_MBOX:
651 case SEND_RFC822:
652 case SEND_SHOW:
653 break;
655 break;
656 case MIME_ALTERNATIVE:
657 if ((action == SEND_TODISP || action == SEND_QUOTE) &&
658 !ok_blook(print_alternatives)) {
659 /* XXX This (a) should not remain (b) should be own fun
660 * TODO (despite the fact that v15 will do this completely differently
661 * TODO by having an action-specific "manager" that will traverse the
662 * TODO parsed MIME tree and decide for each part wether it'll be
663 * TODO displayed or not *before* we walk the tree for doing action */
664 struct mpstack {
665 struct mpstack *outer;
666 struct mimepart *mp;
667 } outermost, * volatile curr, * volatile mpsp;
668 bool_t volatile neednl, hadpart;
669 sighandler_type volatile opsh, oish, ohsh;
671 (curr = &outermost)->outer = NULL;
672 curr->mp = ip;
673 neednl = hadpart = FAL0;
675 __sndalter_sig = 0;
676 opsh = safe_signal(SIGPIPE, &__sndalter_onsig);
677 oish = safe_signal(SIGINT, &__sndalter_onsig);
678 ohsh = safe_signal(SIGHUP, &__sndalter_onsig);
679 if (sigsetjmp(__sndalter_actjmp, 1)) {
680 rv = -1;
681 goto jalter_unroll;
684 for (np = ip->m_multipart;;) {
685 jalter_redo:
686 for (; np != NULL; np = np->m_nextpart) {
687 if (action != SEND_QUOTE && np->m_ct_type_plain != NULL) {
688 if (neednl)
689 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
690 _print_part_info(obuf, np, doign, level, qf, stats);
692 neednl = TRU1;
694 switch (np->m_mimecontent) {
695 case MIME_ALTERNATIVE:
696 case MIME_RELATED:
697 case MIME_MULTI:
698 case MIME_DIGEST:
699 mpsp = salloc(sizeof *mpsp);
700 mpsp->outer = curr;
701 mpsp->mp = np->m_multipart;
702 curr->mp = np;
703 curr = mpsp;
704 np = mpsp->mp;
705 neednl = FAL0;
706 goto jalter_redo;
707 default:
708 if (hadpart)
709 break;
710 switch (mime_type_handler(&mh, np, action)) {
711 default:
712 mh.mh_flags = MIME_HDL_NULL;
713 continue;
714 case MIME_HDL_TEXT:
715 case MIME_HDL_PTF:
716 break;
718 /* FALLTHRU */
719 case MIME_TEXT_PLAIN:
720 if (hadpart)
721 break;
722 quoteflt_flush(qf);
723 if (action == SEND_QUOTE && hadpart) {
724 struct quoteflt *dummy = quoteflt_dummy();
725 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, dummy, stats,
726 NULL);
727 quoteflt_flush(dummy);
729 hadpart = TRU1;
730 neednl = FAL0;
731 rv = sendpart(zmp, np, obuf, doign, qf, action, stats,
732 level + 1);
733 quoteflt_reset(qf, origobuf);
735 if (rv < 0)
736 jalter_unroll:
737 curr = &outermost; /* Cause overall loop termination */
738 break;
742 mpsp = curr->outer;
743 if (mpsp == NULL)
744 break;
745 curr = mpsp;
746 np = curr->mp->m_nextpart;
748 safe_signal(SIGHUP, ohsh);
749 safe_signal(SIGINT, oish);
750 safe_signal(SIGPIPE, opsh);
751 if (__sndalter_sig != 0)
752 n_raise(__sndalter_sig);
753 goto jleave;
755 /* FALLTHRU */
756 case MIME_MULTI:
757 case MIME_DIGEST:
758 case MIME_RELATED:
759 switch (action) {
760 case SEND_TODISP:
761 case SEND_TODISP_ALL:
762 case SEND_QUOTE:
763 case SEND_QUOTE_ALL:
764 case SEND_TOFILE:
765 case SEND_TOPIPE:
766 case SEND_TOSRCH:
767 case SEND_DECRYPT:
768 jmulti:
769 if ((action == SEND_TODISP || action == SEND_TODISP_ALL) &&
770 ip->m_multipart != NULL &&
771 ip->m_multipart->m_mimecontent == MIME_DISCARD &&
772 ip->m_multipart->m_nextpart == NULL) {
773 char const *x = _("[Missing multipart boundary - use \"show\" "
774 "to display the raw message]\n");
775 _out(x, strlen(x), obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
778 for (np = ip->m_multipart; np != NULL; np = np->m_nextpart) {
779 bool_t ispipe;
781 if (np->m_mimecontent == MIME_DISCARD && action != SEND_DECRYPT)
782 continue;
784 ispipe = FAL0;
785 switch (action) {
786 case SEND_TOFILE:
787 if (np->m_partstring && !strcmp(np->m_partstring, "1"))
788 break;
789 stats = NULL;
790 /* TODO Always open multipart on /dev/null, it's a hack to be
791 * TODO able to dive into that structure, and still better
792 * TODO than asking the user for something stupid.
793 * TODO oh, wait, we did ask for a filename for this MIME mail,
794 * TODO and that outer container is useless anyway ;-P */
795 if (np->m_multipart != NULL) {
796 if ((obuf = Fopen("/dev/null", "w")) == NULL)
797 continue;
798 } else if ((obuf = newfile(np, &ispipe)) == NULL)
799 continue;
800 if (!ispipe)
801 break;
802 if (sigsetjmp(_send_pipejmp, 1)) {
803 rv = -1;
804 goto jpipe_close;
806 oldpipe = safe_signal(SIGPIPE, &_send_onpipe);
807 break;
808 case SEND_TODISP:
809 case SEND_TODISP_ALL:
810 case SEND_QUOTE_ALL:
811 if (ip->m_mimecontent != MIME_MULTI &&
812 ip->m_mimecontent != MIME_ALTERNATIVE &&
813 ip->m_mimecontent != MIME_RELATED &&
814 ip->m_mimecontent != MIME_DIGEST)
815 break;
816 _print_part_info(obuf, np, doign, level, qf, stats);
817 break;
818 case SEND_MBOX:
819 case SEND_RFC822:
820 case SEND_SHOW:
821 case SEND_TOSRCH:
822 case SEND_QUOTE:
823 case SEND_DECRYPT:
824 case SEND_TOPIPE:
825 break;
828 quoteflt_flush(qf);
829 if (sendpart(zmp, np, obuf, doign, qf, action, stats, level+1) < 0)
830 rv = -1;
831 quoteflt_reset(qf, origobuf);
833 if (action == SEND_QUOTE)
834 break;
835 if (action == SEND_TOFILE && obuf != origobuf) {
836 if (!ispipe)
837 Fclose(obuf);
838 else {
839 jpipe_close:
840 safe_signal(SIGPIPE, SIG_IGN);
841 Pclose(obuf, TRU1);
842 safe_signal(SIGPIPE, oldpipe);
846 goto jleave;
847 case SEND_MBOX:
848 case SEND_RFC822:
849 case SEND_SHOW:
850 break;
854 /* Copy out message body */
855 if (doign == allignore && level == 0) /* skip final blank line */
856 --cnt;
857 switch (ip->m_mime_enc) {
858 case MIMEE_BIN:
859 case MIMEE_7B:
860 case MIMEE_8B:
861 convert = CONV_NONE;
862 break;
863 case MIMEE_QP:
864 convert = CONV_FROMQP;
865 break;
866 case MIMEE_B64:
867 switch (ip->m_mimecontent) {
868 case MIME_TEXT:
869 case MIME_TEXT_PLAIN:
870 case MIME_TEXT_HTML:
871 convert = CONV_FROMB64_T;
872 break;
873 default:
874 switch (mh.mh_flags & MIME_HDL_TYPE_MASK) {
875 case MIME_HDL_TEXT:
876 case MIME_HDL_PTF:
877 convert = CONV_FROMB64_T;
878 break;
879 default:
880 convert = CONV_FROMB64;
881 break;
883 break;
885 break;
886 default:
887 convert = CONV_NONE;
890 if (action == SEND_DECRYPT || action == SEND_MBOX ||
891 action == SEND_RFC822 || action == SEND_SHOW)
892 convert = CONV_NONE;
893 #ifdef HAVE_ICONV
894 if ((action == SEND_TODISP || action == SEND_TODISP_ALL ||
895 action == SEND_QUOTE || action == SEND_QUOTE_ALL ||
896 action == SEND_TOSRCH) &&
897 (ip->m_mimecontent == MIME_TEXT_PLAIN ||
898 ip->m_mimecontent == MIME_TEXT_HTML ||
899 ip->m_mimecontent == MIME_TEXT ||
900 (mh.mh_flags & MIME_HDL_TYPE_MASK) == MIME_HDL_TEXT ||
901 (mh.mh_flags & MIME_HDL_TYPE_MASK) == MIME_HDL_PTF)) {
902 char const *tcs = charset_get_lc();
904 if (iconvd != (iconv_t)-1)
905 n_iconv_close(iconvd);
906 /* TODO Since Base64 has an odd 4:3 relation in between input
907 * TODO and output an input line may end with a partial
908 * TODO multibyte character; this is no problem at all unless
909 * TODO we send to the display or whatever, i.e., ensure
910 * TODO makeprint() or something; to avoid this trap, *force*
911 * TODO iconv(), in which case this layer will handle leftovers
912 * TODO correctly */
913 if (convert == CONV_FROMB64_T || (asccasecmp(tcs, ip->m_charset) &&
914 asccasecmp(charset_get_7bit(), ip->m_charset))) {
915 iconvd = n_iconv_open(tcs, ip->m_charset);
917 * TODO errors should DEFINETELY not be scrolled away!
918 * TODO what about an error buffer (think old shsp(1)),
919 * TODO re-dump errors since last snapshot when the
920 * TODO command loop enters again? i.e., at least print
921 * TODO "There were errors ?" before the next prompt,
922 * TODO so that the user can look at the error buffer?
924 if (iconvd == (iconv_t)-1 && errno == EINVAL) {
925 n_err(_("Cannot convert from %s to %s\n"), ip->m_charset, tcs);
926 /*rv = 1; goto jleave;*/
930 #endif
932 switch (mh.mh_flags & MIME_HDL_TYPE_MASK) {
933 case MIME_HDL_CMD:
934 case MIME_HDL_PTF:
935 tmpfile = NULL;
936 qbuf = obuf;
938 term_infd = COMMAND_FD_PASS;
939 if (mh.mh_flags & (MIME_HDL_TMPF | MIME_HDL_NEEDSTERM)) {
940 enum oflags of;
942 of = OF_RDWR | OF_REGISTER;
943 if (!(mh.mh_flags & MIME_HDL_TMPF)) {
944 term_infd = 0;
945 mh.mh_flags |= MIME_HDL_TMPF_FILL;
946 of |= OF_UNLINK;
947 } else if (mh.mh_flags & MIME_HDL_TMPF_UNLINK)
948 of |= OF_REGISTER_UNLINK;
950 if ((pbuf = Ftmp((mh.mh_flags & MIME_HDL_TMPF ? &cp : NULL),
951 (mh.mh_flags & MIME_HDL_TMPF_FILL ? "mimehdlfill" : "mimehdl"),
952 of, 0600)) == NULL)
953 goto jesend;
955 if (mh.mh_flags & MIME_HDL_TMPF) {
956 tmpfile = savestr(cp);
957 Ftmp_free(&cp);
960 if (mh.mh_flags & MIME_HDL_TMPF_FILL) {
961 if (term_infd == 0)
962 term_infd = fileno(pbuf);
963 goto jsend;
967 jpipe_for_real:
968 pbuf = _pipefile(&mh, ip, UNVOLATILE(&qbuf), tmpfile, term_infd);
969 if (pbuf == NULL) {
970 jesend:
971 pbuf = qbuf = NULL;
972 rv = -1;
973 goto jend;
974 } else if ((mh.mh_flags & MIME_HDL_NEEDSTERM) && pbuf == (FILE*)-1) {
975 pbuf = qbuf = NULL;
976 goto jend;
978 tmpfile = NULL;
979 action = SEND_TOPIPE;
980 if (pbuf != qbuf) {
981 oldpipe = safe_signal(SIGPIPE, &_send_onpipe);
982 if (sigsetjmp(_send_pipejmp, 1))
983 goto jend;
985 break;
987 default:
988 mh.mh_flags = MIME_HDL_NULL;
989 pbuf = qbuf = obuf;
990 break;
993 jsend:
995 bool_t volatile eof;
996 ui32_t save_qf_pfix_len = qf->qf_pfix_len;
997 ui64_t *save_stats = stats;
999 if (pbuf != origobuf) {
1000 qf->qf_pfix_len = 0; /* XXX legacy (remove filter instead) */
1001 stats = NULL;
1003 eof = FAL0;
1004 rest.s = NULL;
1005 rest.l = 0;
1007 if (pbuf == qbuf) {
1008 __sendp_sig = 0;
1009 __sendp_opipe = safe_signal(SIGPIPE, &__sendp_onsig);
1010 if (sigsetjmp(__sendp_actjmp, 1)) {
1011 if (rest.s != NULL)
1012 free(rest.s);
1013 free(line);
1014 #ifdef HAVE_ICONV
1015 if (iconvd != (iconv_t)-1)
1016 n_iconv_close(iconvd);
1017 #endif
1018 safe_signal(SIGPIPE, __sendp_opipe);
1019 n_raise(__sendp_sig);
1023 quoteflt_reset(qf, pbuf);
1024 while (!eof && fgetline(&line, &linesize, &cnt, &linelen, ibuf, 0)) {
1025 joutln:
1026 if (_out(line, linelen, pbuf, convert, action, qf, stats, &rest) < 0 ||
1027 ferror(pbuf)) {
1028 rv = -1; /* XXX Should bail away?! */
1029 break;
1032 if (!eof && rest.l != 0) {
1033 linelen = 0;
1034 eof = TRU1;
1035 action |= _TD_EOF;
1036 goto joutln;
1038 quoteflt_flush(qf);
1040 if (mh.mh_flags & MIME_HDL_TMPF_FILL) {
1041 mh.mh_flags &= ~MIME_HDL_TMPF_FILL;
1042 fflush(pbuf);
1043 really_rewind(pbuf);
1044 /* Don't Fclose() the Ftmp() thing due to OF_REGISTER_UNLINK++ */
1045 goto jpipe_for_real;
1048 if (pbuf == qbuf)
1049 safe_signal(SIGPIPE, __sendp_opipe);
1051 if (rest.s != NULL)
1052 free(rest.s);
1054 if (pbuf != origobuf) {
1055 qf->qf_pfix_len = save_qf_pfix_len;
1056 stats = save_stats;
1060 jend:
1061 if (line != NULL)
1062 free(line);
1063 if (pbuf != qbuf) {
1064 safe_signal(SIGPIPE, SIG_IGN);
1065 Pclose(pbuf, !(mh.mh_flags & MIME_HDL_ASYNC));
1066 safe_signal(SIGPIPE, oldpipe);
1067 if (qbuf != NULL && qbuf != obuf)
1068 pipecpy(qbuf, obuf, origobuf, qf, stats);
1070 #ifdef HAVE_ICONV
1071 if (iconvd != (iconv_t)-1)
1072 n_iconv_close(iconvd);
1073 #endif
1074 jleave:
1075 NYD_LEAVE;
1076 return rv;
1079 static FILE *
1080 newfile(struct mimepart *ip, bool_t *ispipe)
1082 struct str in, out;
1083 char *f;
1084 FILE *fp;
1085 NYD_ENTER;
1087 f = ip->m_filename;
1088 *ispipe = FAL0;
1090 if (f != NULL && f != (char*)-1) {
1091 in.s = f;
1092 in.l = strlen(f);
1093 makeprint(&in, &out);
1094 out.l = delctrl(out.s, out.l);
1095 f = savestrbuf(out.s, out.l);
1096 free(out.s);
1099 if (options & OPT_INTERACTIVE) {
1100 struct str prompt;
1101 char *f2, *f3;
1103 /* TODO Generic function which asks for filename.
1104 * TODO If the current part is the first textpart the target
1105 * TODO is implicit from outer `write' etc! */
1106 /* I18N: Filename input prompt with file type indication */
1107 str_concat_csvl(&prompt, _("Enter filename for part "),
1108 (ip->m_partstring != NULL) ? ip->m_partstring : _("?"),
1109 _(" ("), ip->m_ct_type_plain, _("): "), NULL);
1110 jgetname:
1111 f2 = n_input_cp_addhist(prompt.s, ((f != (char*)-1 && f != NULL)
1112 ? fexpand_nshell_quote(f) : NULL), TRU1);
1113 if (f2 == NULL || *f2 == '\0') {
1114 if (options & OPT_D_V)
1115 n_err(_("... skipping this\n"));
1116 fp = NULL;
1117 goto jleave;
1118 } else if (*f2 == '|')
1119 /* Pipes are expanded by the shell */
1120 f = f2;
1121 else if ((f3 = fexpand(f2, FEXP_LOCAL | FEXP_NSHELL)) == NULL)
1122 /* (Error message written by fexpand()) */
1123 goto jgetname;
1124 else
1125 f = f3;
1127 if (f == NULL || f == (char*)-1) {
1128 fp = NULL;
1129 goto jleave;
1132 if (*f == '|') {
1133 char const *cp;
1134 cp = ok_vlook(SHELL);
1135 if (cp == NULL)
1136 cp = XSHELL;
1137 fp = Popen(f + 1, "w", cp, NULL, 1);
1138 if (!(*ispipe = (fp != NULL)))
1139 n_perr(f, 0);
1140 } else {
1141 if ((fp = Fopen(f, "w")) == NULL)
1142 n_err(_("Cannot open \"%s\"\n"), f);
1144 jleave:
1145 NYD_LEAVE;
1146 return fp;
1149 static void
1150 pipecpy(FILE *pipebuf, FILE *outbuf, FILE *origobuf, struct quoteflt *qf,
1151 ui64_t *stats)
1153 char *line = NULL; /* TODO line pool */
1154 size_t linesize = 0, linelen, cnt;
1155 ssize_t all_sz, sz;
1156 NYD_ENTER;
1158 fflush(pipebuf);
1159 rewind(pipebuf);
1160 cnt = fsize(pipebuf);
1161 all_sz = 0;
1163 quoteflt_reset(qf, outbuf);
1164 while (fgetline(&line, &linesize, &cnt, &linelen, pipebuf, 0) != NULL) {
1165 if ((sz = quoteflt_push(qf, line, linelen)) < 0)
1166 break;
1167 all_sz += sz;
1169 if ((sz = quoteflt_flush(qf)) > 0)
1170 all_sz += sz;
1171 if (line)
1172 free(line);
1174 if (all_sz > 0 && outbuf == origobuf && stats != NULL)
1175 *stats += all_sz;
1176 Fclose(pipebuf);
1177 NYD_LEAVE;
1180 static void
1181 statusput(const struct message *mp, FILE *obuf, struct quoteflt *qf,
1182 ui64_t *stats)
1184 char statout[3], *cp = statout;
1185 NYD_ENTER;
1187 if (mp->m_flag & MREAD)
1188 *cp++ = 'R';
1189 if (!(mp->m_flag & MNEW))
1190 *cp++ = 'O';
1191 *cp = 0;
1192 if (statout[0]) {
1193 int i = fprintf(obuf, "%.*sStatus: %s\n", (int)qf->qf_pfix_len,
1194 (qf->qf_pfix_len > 0 ? qf->qf_pfix : 0), statout);
1195 if (i > 0 && stats != NULL)
1196 *stats += i;
1198 NYD_LEAVE;
1201 static void
1202 xstatusput(const struct message *mp, FILE *obuf, struct quoteflt *qf,
1203 ui64_t *stats)
1205 char xstatout[4];
1206 char *xp = xstatout;
1207 NYD_ENTER;
1209 if (mp->m_flag & MFLAGGED)
1210 *xp++ = 'F';
1211 if (mp->m_flag & MANSWERED)
1212 *xp++ = 'A';
1213 if (mp->m_flag & MDRAFTED)
1214 *xp++ = 'T';
1215 *xp = 0;
1216 if (xstatout[0]) {
1217 int i = fprintf(obuf, "%.*sX-Status: %s\n", (int)qf->qf_pfix_len,
1218 (qf->qf_pfix_len > 0 ? qf->qf_pfix : 0), xstatout);
1219 if (i > 0 && stats != NULL)
1220 *stats += i;
1222 NYD_LEAVE;
1225 static void
1226 put_from_(FILE *fp, struct mimepart *ip, ui64_t *stats)
1228 char const *froma, *date, *nl;
1229 int i;
1230 NYD_ENTER;
1232 if (ip != NULL && ip->m_from != NULL) {
1233 froma = ip->m_from;
1234 date = fakedate(ip->m_time);
1235 nl = "\n";
1236 } else {
1237 froma = myname;
1238 date = time_current.tc_ctime;
1239 nl = "";
1242 colour_put(fp, COLOURSPEC_FROM_);
1243 i = fprintf(fp, "From %s %s%s", froma, date, nl);
1244 colour_reset(fp);
1245 if (i > 0 && stats != NULL)
1246 *stats += i;
1247 NYD_LEAVE;
1250 FL int
1251 sendmp(struct message *mp, FILE *obuf, struct ignoretab *doign,
1252 char const *prefix, enum sendaction action, ui64_t *stats)
1254 struct quoteflt qf;
1255 size_t cnt, sz, i;
1256 FILE *ibuf;
1257 enum mime_parse_flags mpf;
1258 struct mimepart *ip;
1259 int rv = -1, c;
1260 NYD_ENTER;
1262 if (mp == dot && action != SEND_TOSRCH)
1263 pstate |= PS_DID_PRINT_DOT;
1264 if (stats != NULL)
1265 *stats = 0;
1266 quoteflt_init(&qf, prefix);
1268 /* First line is the From_ line, so no headers there to worry about */
1269 if ((ibuf = setinput(&mb, mp, NEED_BODY)) == NULL)
1270 goto jleave;
1272 cnt = mp->m_size;
1273 sz = 0;
1275 struct str const *cpre, *csuf;
1276 #ifdef HAVE_COLOUR
1277 cpre = colour_get(COLOURSPEC_FROM_);
1278 csuf = colour_get(COLOURSPEC_RESET);
1279 #else
1280 cpre = csuf = NULL;
1281 #endif
1282 if (mp->m_flag & MNOFROM) {
1283 if (doign != allignore && doign != fwdignore && action != SEND_RFC822)
1284 sz = fprintf(obuf, "%s%.*sFrom %s %s%s\n",
1285 (cpre != NULL ? cpre->s : ""),
1286 (int)qf.qf_pfix_len, (qf.qf_pfix_len != 0 ? qf.qf_pfix : ""),
1287 fakefrom(mp), fakedate(mp->m_time),
1288 (csuf != NULL ? csuf->s : ""));
1289 } else if (doign != allignore && doign != fwdignore &&
1290 action != SEND_RFC822) {
1291 if (qf.qf_pfix_len > 0) {
1292 i = fwrite(qf.qf_pfix, sizeof *qf.qf_pfix, qf.qf_pfix_len, obuf);
1293 if (i != qf.qf_pfix_len)
1294 goto jleave;
1295 sz += i;
1297 #ifdef HAVE_COLOUR
1298 if (cpre != NULL) {
1299 fputs(cpre->s, obuf);
1300 cpre = (struct str const*)0x1;
1302 #endif
1304 while (cnt > 0 && (c = getc(ibuf)) != EOF) {
1305 #ifdef HAVE_COLOUR
1306 if (c == '\n' && csuf != NULL) {
1307 cpre = (struct str const*)0x1;
1308 fputs(csuf->s, obuf);
1310 #endif
1311 putc(c, obuf);
1312 ++sz;
1313 --cnt;
1314 if (c == '\n')
1315 break;
1318 #ifdef HAVE_COLOUR
1319 if (csuf != NULL && cpre != (struct str const*)0x1)
1320 fputs(csuf->s, obuf);
1321 #endif
1322 } else {
1323 while (cnt > 0 && (c = getc(ibuf)) != EOF) {
1324 --cnt;
1325 if (c == '\n')
1326 break;
1330 if (sz > 0 && stats != NULL)
1331 *stats += sz;
1333 mpf = MIME_PARSE_NONE;
1334 if (action != SEND_MBOX && action != SEND_RFC822 && action != SEND_SHOW)
1335 mpf |= MIME_PARSE_DECRYPT | MIME_PARSE_PARTS;
1336 if ((ip = mime_parse_msg(mp, mpf)) == NULL)
1337 goto jleave;
1339 rv = sendpart(mp, ip, obuf, doign, &qf, action, stats, 0);
1340 jleave:
1341 quoteflt_destroy(&qf);
1342 NYD_LEAVE;
1343 return rv;
1346 /* s-it-mode */