MLE: enable recursive command invocations
[s-mailx.git] / send.c
blobc75e1aefccefd0a5b722f6815541a4f743b270a1
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 *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 *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
95 struct n_colour_pen *cpen = n_colour_pen_create(n_COLOUR_ID_VIEW_PARTINFO,
96 NULL);
97 if ((cpre = n_colour_pen_to_str(cpen)) != NULL)
98 csuf = n_colour_reset_to_str();
99 else
100 csuf = NULL;
102 #else
103 cpre = csuf = NULL;
104 #endif
106 /* Take care of "99.99", i.e., 5 */
107 if ((cp = mpp->m_partstring) == NULL || cp[0] == '\0')
108 cp = "?";
109 if (level || (cp[0] != '1' && cp[1] == '\0'))
110 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
111 if (cpre != NULL)
112 _out(cpre->s, cpre->l, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
113 _out("[-- #", 5, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
114 _out(cp, strlen(cp), obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
116 to.l = snprintf(buf, sizeof buf, " %" PRIuZ "/%" PRIuZ " ",
117 (uiz_t)mpp->m_lines, (uiz_t)mpp->m_size);
118 _out(buf, to.l, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
120 if ((cp = mpp->m_ct_type_usr_ovwr) != NULL)
121 _out("+", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
122 else
123 cp = mpp->m_ct_type_plain;
124 if ((to.l = strlen(cp)) > 30 && is_asccaseprefix(cp, "application/")) {
125 size_t const al = sizeof("appl../") -1, fl = sizeof("application/") -1;
126 size_t i = to.l - fl;
127 char *x = salloc(al + i +1);
129 memcpy(x, "appl../", al);
130 memcpy(x + al, cp + fl, i +1);
131 cp = x;
132 to.l = al + i;
134 _out(cp, to.l, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
136 if (mpp->m_multipart == NULL/* TODO */ && (cp = mpp->m_ct_enc) != NULL) {
137 _out(", ", 2, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
138 if (to.l > 25 && !asccasecmp(cp, "quoted-printable"))
139 cp = "qu.-pr.";
140 _out(cp, strlen(cp), obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
143 if (mpp->m_multipart == NULL/* TODO */ && (cp = mpp->m_charset) != NULL) {
144 _out(", ", 2, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
145 _out(cp, strlen(cp), obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
148 _out(" --]", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
149 if (csuf != NULL)
150 _out(csuf->s, csuf->l, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
151 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
153 if (is_ign("content-disposition", 19, doign) && mpp->m_filename != NULL &&
154 *mpp->m_filename != '\0') {
155 makeprint(n_str_add_cp(&ti, mpp->m_filename), &to);
156 free(ti.s);
157 to.l = delctrl(to.s, to.l);
159 if (cpre != NULL)
160 _out(cpre->s, cpre->l, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
161 _out("[-- ", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
162 _out(to.s, to.l, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
163 _out(" --]", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
164 if (csuf != NULL)
165 _out(csuf->s, csuf->l, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
166 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
168 free(to.s);
170 NYD2_LEAVE;
173 static FILE *
174 _pipefile(struct mime_handler *mhp, struct mimepart const *mpp, FILE **qbuf,
175 char const *tmpname, int term_infd)
177 struct str s;
178 char const *env_addon[8], *cp, *sh;
179 FILE *rbuf;
180 NYD_ENTER;
182 rbuf = *qbuf;
184 if (mhp->mh_flags & MIME_HDL_ISQUOTE) {
185 if ((*qbuf = Ftmp(NULL, "sendp", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
186 NULL) {
187 n_perr(_("tmpfile"), 0);
188 *qbuf = rbuf;
192 if ((mhp->mh_flags & MIME_HDL_TYPE_MASK) == MIME_HDL_PTF) {
193 union {int (*ptf)(void); char const *sh;} u;
195 fflush(*qbuf);
196 if (*qbuf != stdout) /* xxx never? v15: it'll be a filter anyway */
197 fflush(stdout);
199 u.ptf = mhp->mh_ptf;
200 if((rbuf = Popen((char*)-1, "W", u.sh, NULL, fileno(*qbuf))) == NULL)
201 goto jerror;
202 goto jleave;
205 /* NAIL_FILENAME */
206 if (mpp == NULL || (cp = mpp->m_filename) == NULL)
207 cp = "";
208 env_addon[0] = str_concat_csvl(&s, NAILENV_FILENAME, "=", cp, NULL)->s;
210 /* NAIL_FILENAME_GENERATED *//* TODO pathconf NAME_MAX; but user can create
211 * TODO a file wherever he wants! *Do* create a zero-size temporary file
212 * TODO and give *that* path as NAIL_FILENAME_TEMPORARY, clean it up once
213 * TODO the pipe returns? Like this we *can* verify path/name issues! */
214 env_addon[1] = str_concat_csvl(&s, NAILENV_FILENAME_GENERATED, "=",
215 getrandstring(MIN(NAME_MAX / 4, 16)), NULL)->s;
217 /* NAIL_CONTENT{,_EVIDENCE} */
218 if (mpp == NULL || (cp = mpp->m_ct_type_plain) == NULL)
219 cp = "";
220 env_addon[2] = str_concat_csvl(&s, NAILENV_CONTENT, "=", cp, NULL)->s;
222 if (mpp != NULL && mpp->m_ct_type_usr_ovwr != NULL)
223 cp = mpp->m_ct_type_usr_ovwr;
224 env_addon[3] = str_concat_csvl(&s, NAILENV_CONTENT_EVIDENCE, "=", cp,
225 NULL)->s;
227 env_addon[4] = str_concat_csvl(&s, NAILENV_TMPDIR, "=", tempdir, NULL)->s;
228 env_addon[5] = str_concat_csvl(&s, "TMPDIR", "=", tempdir, NULL)->s;
230 env_addon[6] = NULL;
232 /* NAIL_FILENAME_TEMPORARY? */
233 if (tmpname != NULL) {
234 env_addon[6] = str_concat_csvl(&s, NAILENV_FILENAME_TEMPORARY, "=",
235 tmpname, NULL)->s;
236 env_addon[7] = NULL;
239 if ((sh = ok_vlook(SHELL)) == NULL)
240 sh = XSHELL;
242 if (mhp->mh_flags & MIME_HDL_NEEDSTERM) {
243 sigset_t nset;
244 int pid;
246 sigemptyset(&nset);
247 pid = run_command(sh, NULL, term_infd, COMMAND_FD_PASS, "-c",
248 mhp->mh_shell_cmd, NULL, env_addon);
249 rbuf = (pid < 0) ? NULL : (FILE*)-1;
250 } else {
251 rbuf = Popen(mhp->mh_shell_cmd, "W", sh, env_addon,
252 (mhp->mh_flags & MIME_HDL_ASYNC ? -1 : fileno(*qbuf)));
253 jerror:
254 if (rbuf == NULL)
255 n_err(_("Cannot run MIME type handler \"%s\": %s\n"),
256 mhp->mh_msg, strerror(errno));
257 else {
258 fflush(*qbuf);
259 if (*qbuf != stdout)
260 fflush(stdout);
263 jleave:
264 NYD_LEAVE;
265 return rbuf;
268 SINLINE ssize_t
269 _out(char const *buf, size_t len, FILE *fp, enum conversion convert, enum
270 sendaction action, struct quoteflt *qf, ui64_t *stats, struct str *rest)
272 ssize_t sz = 0, n;
273 int flags;
274 NYD_ENTER;
276 #if 0
277 Well ... it turns out to not work like that since of course a valid
278 RFC 4155 compliant parser, like S-nail, takes care for From_ lines only
279 after an empty line has been seen, which cannot be detected that easily
280 right here!
281 ifdef HAVE_DEBUG /* TODO assert legacy */
282 /* TODO if at all, this CAN only happen for SEND_DECRYPT, since all
283 * TODO other input situations handle RFC 4155 OR, if newly generated,
284 * TODO enforce quoted-printable if there is From_, as "required" by
285 * TODO RFC 5751. The SEND_DECRYPT case is not yet overhauled;
286 * TODO if it may happen in this path, we should just treat decryption
287 * TODO as we do for the other input paths; i.e., handle it in SSL!! */
288 if (action == SEND_MBOX || action == SEND_DECRYPT)
289 assert(!is_head(buf, len, TRU1));
290 #else
291 if ((/*action == SEND_MBOX ||*/ action == SEND_DECRYPT) &&
292 is_head(buf, len, TRU1)) {
293 putc('>', fp);
294 ++sz;
296 #endif
298 flags = ((int)action & _TD_EOF);
299 action &= ~_TD_EOF;
300 n = mime_write(buf, len, fp,
301 action == SEND_MBOX ? CONV_NONE : convert,
302 flags | ((action == SEND_TODISP || action == SEND_TODISP_ALL ||
303 action == SEND_QUOTE || action == SEND_QUOTE_ALL)
304 ? TD_ISPR | TD_ICONV
305 : (action == SEND_TOSRCH || action == SEND_TOPIPE)
306 ? TD_ICONV : (action == SEND_SHOW ? TD_ISPR : TD_NONE)),
307 qf, rest);
308 if (n < 0)
309 sz = n;
310 else if (n > 0) {
311 sz += n;
312 if (stats != NULL)
313 *stats += sz;
315 NYD_LEAVE;
316 return sz;
319 static void
320 _send_onpipe(int signo)
322 NYD_X; /* Signal handler */
323 UNUSED(signo);
324 siglongjmp(_send_pipejmp, 1);
327 static sigjmp_buf __sendp_actjmp; /* TODO someday.. */
328 static int __sendp_sig; /* TODO someday.. */
329 static sighandler_type __sendp_opipe;
330 static void
331 __sendp_onsig(int sig) /* TODO someday, we won't need it no more */
333 NYD_X; /* Signal handler */
334 __sendp_sig = sig;
335 siglongjmp(__sendp_actjmp, 1);
338 static int
339 sendpart(struct message *zmp, struct mimepart *ip, FILE * volatile obuf,
340 struct ignoretab *doign, struct quoteflt *qf,
341 enum sendaction volatile action, ui64_t * volatile stats, int level)
343 int volatile rv = 0;
344 struct mime_handler mh;
345 struct str rest;
346 char *line = NULL, *cp, *cp2, *start;
347 char const *tmpname = NULL;
348 size_t linesize = 0, linelen, cnt;
349 int volatile term_infd;
350 int dostat, c;
351 struct mimepart *volatile np;
352 FILE * volatile ibuf = NULL, * volatile pbuf = obuf,
353 * volatile qbuf = obuf, *origobuf = obuf;
354 enum conversion volatile convert;
355 sighandler_type volatile oldpipe = SIG_DFL;
356 NYD_ENTER;
358 if (ip->m_mimecontent == MIME_PKCS7 && ip->m_multipart &&
359 action != SEND_MBOX && action != SEND_RFC822 && action != SEND_SHOW)
360 goto jskip;
362 dostat = 0;
363 if (level == 0) {
364 if (doign != NULL) {
365 if (!is_ign("status", 6, doign))
366 dostat |= 1;
367 if (!is_ign("x-status", 8, doign))
368 dostat |= 2;
369 } else
370 dostat = 3;
372 if ((ibuf = setinput(&mb, (struct message*)ip, NEED_BODY)) == NULL) {
373 rv = -1;
374 goto jleave;
376 cnt = ip->m_size;
378 if (ip->m_mimecontent == MIME_DISCARD)
379 goto jskip;
381 if (!(ip->m_flag & MNOFROM))
382 while (cnt && (c = getc(ibuf)) != EOF) {
383 cnt--;
384 if (c == '\n')
385 break;
387 convert = (action == SEND_TODISP || action == SEND_TODISP_ALL ||
388 action == SEND_QUOTE || action == SEND_QUOTE_ALL ||
389 action == SEND_TOSRCH)
390 ? CONV_FROMHDR : CONV_NONE;
392 /* Work the headers */
393 quoteflt_reset(qf, obuf);
394 /* C99 */{
395 enum {
396 HPS_NONE = 0,
397 HPS_IN_FIELD = 1<<0,
398 HPS_IGNORE = 1<<1,
399 HPS_ISENC_1 = 1<<2,
400 HPS_ISENC_2 = 1<<3
401 } hps = HPS_NONE;
402 size_t lineno = 0;
404 while (fgetline(&line, &linesize, &cnt, &linelen, ibuf, 0)) {
405 ++lineno;
406 if (line[0] == '\n') {
407 /* If line is blank, we've reached end of headers, so force out
408 * status: field and note that we are no longer in header fields */
409 if (dostat & 1)
410 statusput(zmp, obuf, qf, stats);
411 if (dostat & 2)
412 xstatusput(zmp, obuf, qf, stats);
413 if (doign != allignore)
414 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
415 break;
418 hps &= ~HPS_ISENC_1;
419 if ((hps & HPS_IN_FIELD) && blankchar(line[0])) {
420 /* If this line is a continuation (SP / HT) of a previous header
421 * field, determine if the start of the line is a MIME encoded word */
422 if (hps & HPS_ISENC_2) {
423 for (cp = line; blankchar(*cp); ++cp)
425 if (cp > line && linelen - PTR2SIZE(cp - line) > 8 &&
426 cp[0] == '=' && cp[1] == '?')
427 hps |= HPS_ISENC_1;
429 } else {
430 /* Pick up the header field if we have one */
431 for (cp = line; (c = *cp & 0377) && c != ':' && !spacechar(c); ++cp)
433 cp2 = cp;
434 while (spacechar(*cp))
435 ++cp;
436 if (cp[0] != ':') {
437 if (lineno != 1)
438 n_err(_("Malformed message: headers and body not separated "
439 "(with empty line)\n"));
440 /* Not a header line, force out status: This happens in uucp style
441 * mail where there are no headers at all */
442 if (level == 0 /*&& lineno == 1*/) {
443 if (dostat & 1)
444 statusput(zmp, obuf, qf, stats);
445 if (dostat & 2)
446 xstatusput(zmp, obuf, qf, stats);
448 if (doign != allignore)
449 _out("\n", 1, obuf, CONV_NONE,SEND_MBOX, qf, stats, NULL);
450 break;
453 /* If it is an ignored field and we care about such things, skip it.
454 * Misuse dostat also for another bit xxx use a bitenum + for more */
455 if (ok_blook(keep_content_length))
456 dostat |= 1 << 2;
457 c = *cp2;
458 *cp2 = 0; /* temporarily null terminate */
459 if ((doign && is_ign(line, PTR2SIZE(cp2 - line), doign)) ||
460 (action == SEND_MBOX && !(dostat & (1 << 2)) &&
461 (!asccasecmp(line, "content-length") ||
462 !asccasecmp(line, "lines"))))
463 hps |= HPS_IGNORE;
464 else if (!asccasecmp(line, "status")) {
465 /* If field is "status," go compute and print real Status: field */
466 if (dostat & 1) {
467 statusput(zmp, obuf, qf, stats);
468 dostat &= ~1;
469 hps |= HPS_IGNORE;
471 } else if (!asccasecmp(line, "x-status")) {
472 /* If field is "status," go compute and print real Status: field */
473 if (dostat & 2) {
474 xstatusput(zmp, obuf, qf, stats);
475 dostat &= ~2;
476 hps |= HPS_IGNORE;
478 } else {
479 hps &= ~HPS_IGNORE;
480 /* For colourization we need the complete line, so save it */
481 /* XXX This is all temporary (colour belongs into backend), so
482 * XXX use tmpname as a temporary storage in the meanwhile */
483 #ifdef HAVE_COLOUR
484 if (pstate & PS_COLOUR_ACTIVE)
485 tmpname = savestrbuf(line, PTR2SIZE(cp2 - line));
486 #endif
488 *cp2 = c;
489 dostat &= ~(1 << 2);
490 hps |= HPS_IN_FIELD;
493 /* Determine if the end of the line is a MIME encoded word */
494 /* TODO geeeh! all this lengthy stuff that follows is about is dealing
495 * TODO with header follow lines, and it should be up to the backend
496 * TODO what happens and what not, i.e., it doesn't matter wether it's
497 * TODO a MIME-encoded word or not, as long as a single separating space
498 * TODO remains in between lines (the MIME stuff will correctly remove
499 * TODO whitespace in between multiple adjacent encoded words) */
500 hps &= ~HPS_ISENC_2;
501 if (cnt && (c = getc(ibuf)) != EOF) {
502 if (blankchar(c)) {
503 cp = line + linelen - 1;
504 if (linelen > 0 && *cp == '\n')
505 --cp;
506 while (cp >= line && whitechar(*cp))
507 --cp;
508 if (PTR2SIZE(cp - line > 8) && cp[0] == '=' && cp[-1] == '?')
509 hps |= HPS_ISENC_2;
511 ungetc(c, ibuf);
514 if (!(hps & HPS_IGNORE)) {
515 size_t len = linelen;
516 start = line;
517 if (action == SEND_TODISP || action == SEND_TODISP_ALL ||
518 action == SEND_QUOTE || action == SEND_QUOTE_ALL ||
519 action == SEND_TOSRCH) {
520 /* Strip blank characters if two MIME-encoded words follow on
521 * continuing lines */
522 if (hps & HPS_ISENC_1)
523 while (len > 0 && blankchar(*start)) {
524 ++start;
525 --len;
527 if (hps & HPS_ISENC_2)
528 if (len > 0 && start[len - 1] == '\n')
529 --len;
530 while (len > 0 && blankchar(start[len - 1]))
531 --len;
533 #ifdef HAVE_COLOUR
535 bool_t colour_stripped = FAL0;
536 if (tmpname != NULL) {
537 n_colour_put(obuf, n_COLOUR_ID_VIEW_HEADER, tmpname);
538 if (len > 0 && start[len - 1] == '\n') {
539 colour_stripped = TRU1;
540 --len;
543 #endif
544 _out(start, len, obuf, convert, action, qf, stats, NULL);
545 #ifdef HAVE_COLOUR
546 if (tmpname != NULL) {
547 n_colour_reset(obuf);
548 if (colour_stripped)
549 putc('\n', obuf);
552 #endif
553 if (ferror(obuf)) {
554 free(line);
555 rv = -1;
556 goto jleave;
560 } /* C99 */
561 quoteflt_flush(qf);
562 free(line);
563 line = NULL;
564 tmpname = NULL;
566 jskip:
567 memset(&mh, 0, sizeof mh);
569 switch (ip->m_mimecontent) {
570 case MIME_822:
571 switch (action) {
572 case SEND_TODISP:
573 case SEND_TODISP_ALL:
574 case SEND_QUOTE:
575 case SEND_QUOTE_ALL:
576 if (ok_blook(rfc822_body_from_)) {
577 if (qf->qf_pfix_len > 0) {
578 size_t i = fwrite(qf->qf_pfix, sizeof *qf->qf_pfix,
579 qf->qf_pfix_len, obuf);
580 if (i == qf->qf_pfix_len && stats != NULL)
581 *stats += i;
583 put_from_(obuf, ip->m_multipart, stats);
585 /* FALLTHRU */
586 case SEND_TOSRCH:
587 case SEND_DECRYPT:
588 goto jmulti;
589 case SEND_TOFILE:
590 case SEND_TOPIPE:
591 if (ok_blook(rfc822_body_from_))
592 put_from_(obuf, ip->m_multipart, stats);
593 /* FALLTHRU */
594 case SEND_MBOX:
595 case SEND_RFC822:
596 case SEND_SHOW:
597 break;
599 break;
600 case MIME_TEXT_HTML:
601 case MIME_TEXT:
602 case MIME_TEXT_PLAIN:
603 switch (action) {
604 case SEND_TODISP:
605 case SEND_TODISP_ALL:
606 case SEND_QUOTE:
607 case SEND_QUOTE_ALL:
608 switch (mime_type_handler(&mh, ip, action)) {
609 case MIME_HDL_MSG:
610 _out(mh.mh_msg.s, mh.mh_msg.l, obuf, CONV_NONE, SEND_MBOX, qf,
611 stats, NULL);
612 /* We would print this as plain text, so better force going home */
613 goto jleave;
614 default:
615 break;
617 /* FALLTRHU */
618 default:
619 break;
621 break;
622 case MIME_DISCARD:
623 if (action != SEND_DECRYPT)
624 goto jleave;
625 break;
626 case MIME_PKCS7:
627 if (action != SEND_MBOX && action != SEND_RFC822 &&
628 action != SEND_SHOW && ip->m_multipart != NULL)
629 goto jmulti;
630 /* FALLTHRU */
631 default:
632 switch (action) {
633 case SEND_TODISP:
634 case SEND_TODISP_ALL:
635 case SEND_QUOTE:
636 case SEND_QUOTE_ALL:
637 switch (mime_type_handler(&mh, ip, action)) {
638 case MIME_HDL_MSG:
639 _out(mh.mh_msg.s, mh.mh_msg.l, obuf, CONV_NONE, SEND_MBOX, qf,
640 stats, NULL);
641 /* We would print this as plain text, so better force going home */
642 goto jleave;
643 case MIME_HDL_CMD:
644 /* FIXME WE NEED TO DO THAT IF WE ARE THE ONLY MAIL
645 * FIXME CONTENT !! */
646 case MIME_HDL_TEXT:
647 break;
648 default:
649 case MIME_HDL_NULL:
650 if (level == 0 && cnt) {
651 char const *x = _("[-- Binary content --]\n");
652 _out(x, strlen(x), obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
654 goto jleave;
656 break;
657 case SEND_TOFILE:
658 case SEND_TOPIPE:
659 case SEND_TOSRCH:
660 case SEND_DECRYPT:
661 case SEND_MBOX:
662 case SEND_RFC822:
663 case SEND_SHOW:
664 break;
666 break;
667 case MIME_ALTERNATIVE:
668 if ((action == SEND_TODISP || action == SEND_QUOTE) &&
669 !ok_blook(print_alternatives)) {
670 /* XXX This (a) should not remain (b) should be own fun
671 * TODO (despite the fact that v15 will do this completely differently
672 * TODO by having an action-specific "manager" that will traverse the
673 * TODO parsed MIME tree and decide for each part wether it'll be
674 * TODO displayed or not *before* we walk the tree for doing action */
675 struct mpstack {
676 struct mpstack *outer;
677 struct mimepart *mp;
678 } outermost, * volatile curr, * volatile mpsp;
679 bool_t volatile neednl, hadpart;
680 struct n_sigman smalter;
682 (curr = &outermost)->outer = NULL;
683 curr->mp = ip;
684 neednl = hadpart = FAL0;
686 n_SIGMAN_ENTER_SWITCH(&smalter, n_SIGMAN_ALL) {
687 case 0:
688 break;
689 default:
690 rv = -1;
691 goto jalter_leave;
694 for (np = ip->m_multipart;;) {
695 jalter_redo:
696 for (; np != NULL; np = np->m_nextpart) {
697 if (action != SEND_QUOTE && np->m_ct_type_plain != NULL) {
698 if (neednl)
699 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
700 _print_part_info(obuf, np, doign, level, qf, stats);
702 neednl = TRU1;
704 switch (np->m_mimecontent) {
705 case MIME_ALTERNATIVE:
706 case MIME_RELATED:
707 case MIME_MULTI:
708 case MIME_DIGEST:
709 mpsp = salloc(sizeof *mpsp);
710 mpsp->outer = curr;
711 mpsp->mp = np->m_multipart;
712 curr->mp = np;
713 curr = mpsp;
714 np = mpsp->mp;
715 neednl = FAL0;
716 goto jalter_redo;
717 default:
718 if (hadpart)
719 break;
720 switch (mime_type_handler(&mh, np, action)) {
721 default:
722 mh.mh_flags = MIME_HDL_NULL;
723 continue; /* break; break; */
724 case MIME_HDL_PTF:
725 if (!ok_blook(mime_alternative_favour_rich)) {/* TODO */
726 struct mimepart *x = np;
728 while ((x = x->m_nextpart) != NULL) {
729 struct mime_handler mhx;
731 if (x->m_mimecontent == MIME_TEXT_PLAIN ||
732 mime_type_handler(&mhx, x, action) ==
733 MIME_HDL_TEXT)
734 break;
736 if (x != NULL)
737 continue; /* break; break; */
738 goto jalter_plain;
740 /* FALLTHRU */
741 case MIME_HDL_TEXT:
742 break;
744 /* FALLTHRU */
745 case MIME_TEXT_PLAIN:
746 if (hadpart)
747 break;
748 if (ok_blook(mime_alternative_favour_rich)) { /* TODO */
749 struct mimepart *x = np;
751 /* TODO twice TODO, we should dive into /related and
752 * TODO check wether that has rich parts! */
753 while ((x = x->m_nextpart) != NULL) {
754 struct mime_handler mhx;
756 switch (mime_type_handler(&mhx, x, action)) {
757 case MIME_HDL_PTF:
758 break;
759 default:
760 continue;
762 break;
764 if (x != NULL)
765 continue; /* break; break; */
767 jalter_plain:
768 quoteflt_flush(qf);
769 if (action == SEND_QUOTE && hadpart) {
770 struct quoteflt *dummy = quoteflt_dummy();
771 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, dummy, stats,
772 NULL);
773 quoteflt_flush(dummy);
775 hadpart = TRU1;
776 neednl = FAL0;
777 rv = sendpart(zmp, np, obuf, doign, qf, action, stats,
778 level + 1);
779 quoteflt_reset(qf, origobuf);
781 if (rv < 0)
782 curr = &outermost; /* Cause overall loop termination */
783 break;
787 mpsp = curr->outer;
788 if (mpsp == NULL)
789 break;
790 curr = mpsp;
791 np = curr->mp->m_nextpart;
793 jalter_leave:
794 n_sigman_leave(&smalter, n_SIGMAN_VIPSIGS_NTTYOUT);
795 goto jleave;
797 /* FALLTHRU */
798 case MIME_MULTI:
799 case MIME_DIGEST:
800 case MIME_RELATED:
801 switch (action) {
802 case SEND_TODISP:
803 case SEND_TODISP_ALL:
804 case SEND_QUOTE:
805 case SEND_QUOTE_ALL:
806 case SEND_TOFILE:
807 case SEND_TOPIPE:
808 case SEND_TOSRCH:
809 case SEND_DECRYPT:
810 jmulti:
811 if ((action == SEND_TODISP || action == SEND_TODISP_ALL) &&
812 ip->m_multipart != NULL &&
813 ip->m_multipart->m_mimecontent == MIME_DISCARD &&
814 ip->m_multipart->m_nextpart == NULL) {
815 char const *x = _("[Missing multipart boundary - use \"show\" "
816 "to display the raw message]\n");
817 _out(x, strlen(x), obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
820 for (np = ip->m_multipart; np != NULL; np = np->m_nextpart) {
821 bool_t ispipe;
823 if (np->m_mimecontent == MIME_DISCARD && action != SEND_DECRYPT)
824 continue;
826 ispipe = FAL0;
827 switch (action) {
828 case SEND_TOFILE:
829 if (np->m_partstring && !strcmp(np->m_partstring, "1"))
830 break;
831 stats = NULL;
832 /* TODO Always open multipart on /dev/null, it's a hack to be
833 * TODO able to dive into that structure, and still better
834 * TODO than asking the user for something stupid.
835 * TODO oh, wait, we did ask for a filename for this MIME mail,
836 * TODO and that outer container is useless anyway ;-P */
837 if (np->m_multipart != NULL) {
838 if ((obuf = Fopen("/dev/null", "w")) == NULL)
839 continue;
840 } else if ((obuf = newfile(np, &ispipe)) == NULL)
841 continue;
842 if (!ispipe)
843 break;
844 if (sigsetjmp(_send_pipejmp, 1)) {
845 rv = -1;
846 goto jpipe_close;
848 oldpipe = safe_signal(SIGPIPE, &_send_onpipe);
849 break;
850 case SEND_TODISP:
851 case SEND_TODISP_ALL:
852 if (ip->m_mimecontent != MIME_ALTERNATIVE &&
853 ip->m_mimecontent != MIME_RELATED &&
854 ip->m_mimecontent != MIME_DIGEST &&
855 ip->m_mimecontent != MIME_MULTI)
856 break;
857 _print_part_info(obuf, np, doign, level, qf, stats);
858 break;
859 case SEND_QUOTE:
860 case SEND_QUOTE_ALL:
861 case SEND_MBOX:
862 case SEND_RFC822:
863 case SEND_SHOW:
864 case SEND_TOSRCH:
865 case SEND_DECRYPT:
866 case SEND_TOPIPE:
867 break;
870 quoteflt_flush(qf);
871 if ((action == SEND_QUOTE || action == SEND_QUOTE_ALL) &&
872 np->m_multipart == NULL && ip->m_parent != NULL) {
873 struct quoteflt *dummy = quoteflt_dummy();
874 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, dummy, stats,
875 NULL);
876 quoteflt_flush(dummy);
878 if (sendpart(zmp, np, obuf, doign, qf, action, stats, level+1) < 0)
879 rv = -1;
880 quoteflt_reset(qf, origobuf);
882 if (action == SEND_QUOTE) {
883 if (ip->m_mimecontent != MIME_RELATED)
884 break;
886 if (action == SEND_TOFILE && obuf != origobuf) {
887 if (!ispipe)
888 Fclose(obuf);
889 else {
890 jpipe_close:
891 safe_signal(SIGPIPE, SIG_IGN);
892 Pclose(obuf, TRU1);
893 safe_signal(SIGPIPE, oldpipe);
897 goto jleave;
898 case SEND_MBOX:
899 case SEND_RFC822:
900 case SEND_SHOW:
901 break;
903 break;
906 /* Copy out message body */
907 if (doign == allignore && level == 0) /* skip final blank line */
908 --cnt;
909 switch (ip->m_mime_enc) {
910 case MIMEE_BIN:
911 case MIMEE_7B:
912 case MIMEE_8B:
913 convert = CONV_NONE;
914 break;
915 case MIMEE_QP:
916 convert = CONV_FROMQP;
917 break;
918 case MIMEE_B64:
919 switch (ip->m_mimecontent) {
920 case MIME_TEXT:
921 case MIME_TEXT_PLAIN:
922 case MIME_TEXT_HTML:
923 convert = CONV_FROMB64_T;
924 break;
925 default:
926 switch (mh.mh_flags & MIME_HDL_TYPE_MASK) {
927 case MIME_HDL_TEXT:
928 case MIME_HDL_PTF:
929 convert = CONV_FROMB64_T;
930 break;
931 default:
932 convert = CONV_FROMB64;
933 break;
935 break;
937 break;
938 default:
939 convert = CONV_NONE;
942 if (action == SEND_DECRYPT || action == SEND_MBOX ||
943 action == SEND_RFC822 || action == SEND_SHOW)
944 convert = CONV_NONE;
945 #ifdef HAVE_ICONV
946 if ((action == SEND_TODISP || action == SEND_TODISP_ALL ||
947 action == SEND_QUOTE || action == SEND_QUOTE_ALL ||
948 action == SEND_TOSRCH) &&
949 (ip->m_mimecontent == MIME_TEXT_PLAIN ||
950 ip->m_mimecontent == MIME_TEXT_HTML ||
951 ip->m_mimecontent == MIME_TEXT ||
952 (mh.mh_flags & MIME_HDL_TYPE_MASK) == MIME_HDL_TEXT ||
953 (mh.mh_flags & MIME_HDL_TYPE_MASK) == MIME_HDL_PTF)) {
954 char const *tcs = charset_get_lc();
956 if (iconvd != (iconv_t)-1)
957 n_iconv_close(iconvd);
958 /* TODO Since Base64 has an odd 4:3 relation in between input
959 * TODO and output an input line may end with a partial
960 * TODO multibyte character; this is no problem at all unless
961 * TODO we send to the display or whatever, i.e., ensure
962 * TODO makeprint() or something; to avoid this trap, *force*
963 * TODO iconv(), in which case this layer will handle leftovers
964 * TODO correctly */
965 if (convert == CONV_FROMB64_T || (asccasecmp(tcs, ip->m_charset) &&
966 asccasecmp(charset_get_7bit(), ip->m_charset))) {
967 iconvd = n_iconv_open(tcs, ip->m_charset);
969 * TODO errors should DEFINETELY not be scrolled away!
970 * TODO what about an error buffer (think old shsp(1)),
971 * TODO re-dump errors since last snapshot when the
972 * TODO command loop enters again? i.e., at least print
973 * TODO "There were errors ?" before the next prompt,
974 * TODO so that the user can look at the error buffer?
976 if (iconvd == (iconv_t)-1 && errno == EINVAL) {
977 n_err(_("Cannot convert from %s to %s\n"), ip->m_charset, tcs);
978 /*rv = 1; goto jleave;*/
982 #endif
984 switch (mh.mh_flags & MIME_HDL_TYPE_MASK) {
985 case MIME_HDL_CMD:
986 case MIME_HDL_PTF:
987 tmpname = NULL;
988 qbuf = obuf;
990 term_infd = COMMAND_FD_PASS;
991 if (mh.mh_flags & (MIME_HDL_TMPF | MIME_HDL_NEEDSTERM)) {
992 enum oflags of;
994 of = OF_RDWR | OF_REGISTER;
995 if (!(mh.mh_flags & MIME_HDL_TMPF)) {
996 term_infd = 0;
997 mh.mh_flags |= MIME_HDL_TMPF_FILL;
998 of |= OF_UNLINK;
999 } else if (mh.mh_flags & MIME_HDL_TMPF_UNLINK)
1000 of |= OF_REGISTER_UNLINK;
1002 if ((pbuf = Ftmp((mh.mh_flags & MIME_HDL_TMPF ? &cp : NULL),
1003 (mh.mh_flags & MIME_HDL_TMPF_FILL ? "mimehdlfill" : "mimehdl"),
1004 of)) == NULL)
1005 goto jesend;
1007 if (mh.mh_flags & MIME_HDL_TMPF) {
1008 tmpname = savestr(cp);
1009 Ftmp_free(&cp);
1012 if (mh.mh_flags & MIME_HDL_TMPF_FILL) {
1013 if (term_infd == 0)
1014 term_infd = fileno(pbuf);
1015 goto jsend;
1019 jpipe_for_real:
1020 pbuf = _pipefile(&mh, ip, UNVOLATILE(&qbuf), tmpname, term_infd);
1021 if (pbuf == NULL) {
1022 jesend:
1023 pbuf = qbuf = NULL;
1024 rv = -1;
1025 goto jend;
1026 } else if ((mh.mh_flags & MIME_HDL_NEEDSTERM) && pbuf == (FILE*)-1) {
1027 pbuf = qbuf = NULL;
1028 goto jend;
1030 tmpname = NULL;
1031 action = SEND_TOPIPE;
1032 if (pbuf != qbuf) {
1033 oldpipe = safe_signal(SIGPIPE, &_send_onpipe);
1034 if (sigsetjmp(_send_pipejmp, 1))
1035 goto jend;
1037 break;
1039 default:
1040 mh.mh_flags = MIME_HDL_NULL;
1041 pbuf = qbuf = obuf;
1042 break;
1045 jsend:
1047 bool_t volatile eof;
1048 ui32_t save_qf_pfix_len = qf->qf_pfix_len;
1049 ui64_t *save_stats = stats;
1051 if (pbuf != origobuf) {
1052 qf->qf_pfix_len = 0; /* XXX legacy (remove filter instead) */
1053 stats = NULL;
1055 eof = FAL0;
1056 rest.s = NULL;
1057 rest.l = 0;
1059 if (pbuf == qbuf) {
1060 __sendp_sig = 0;
1061 __sendp_opipe = safe_signal(SIGPIPE, &__sendp_onsig);
1062 if (sigsetjmp(__sendp_actjmp, 1)) {
1063 if (rest.s != NULL)
1064 free(rest.s);
1065 free(line);
1066 #ifdef HAVE_ICONV
1067 if (iconvd != (iconv_t)-1)
1068 n_iconv_close(iconvd);
1069 #endif
1070 safe_signal(SIGPIPE, __sendp_opipe);
1071 n_raise(__sendp_sig);
1075 quoteflt_reset(qf, pbuf);
1076 while (!eof && fgetline(&line, &linesize, &cnt, &linelen, ibuf, 0)) {
1077 joutln:
1078 if (_out(line, linelen, pbuf, convert, action, qf, stats, &rest) < 0 ||
1079 ferror(pbuf)) {
1080 rv = -1; /* XXX Should bail away?! */
1081 break;
1084 if (!eof && rest.l != 0) {
1085 linelen = 0;
1086 eof = TRU1;
1087 action |= _TD_EOF;
1088 goto joutln;
1090 quoteflt_flush(qf);
1092 if (mh.mh_flags & MIME_HDL_TMPF_FILL) {
1093 mh.mh_flags &= ~MIME_HDL_TMPF_FILL;
1094 fflush(pbuf);
1095 really_rewind(pbuf);
1096 /* Don't Fclose() the Ftmp() thing due to OF_REGISTER_UNLINK++ */
1097 goto jpipe_for_real;
1100 if (pbuf == qbuf)
1101 safe_signal(SIGPIPE, __sendp_opipe);
1103 if (rest.s != NULL)
1104 free(rest.s);
1106 if (pbuf != origobuf) {
1107 qf->qf_pfix_len = save_qf_pfix_len;
1108 stats = save_stats;
1112 jend:
1113 if (line != NULL)
1114 free(line);
1115 if (pbuf != qbuf) {
1116 safe_signal(SIGPIPE, SIG_IGN);
1117 Pclose(pbuf, !(mh.mh_flags & MIME_HDL_ASYNC));
1118 safe_signal(SIGPIPE, oldpipe);
1119 if (qbuf != NULL && qbuf != obuf)
1120 pipecpy(qbuf, obuf, origobuf, qf, stats);
1122 #ifdef HAVE_ICONV
1123 if (iconvd != (iconv_t)-1)
1124 n_iconv_close(iconvd);
1125 #endif
1126 jleave:
1127 NYD_LEAVE;
1128 return rv;
1131 static FILE *
1132 newfile(struct mimepart *ip, bool_t *ispipe)
1134 struct str in, out;
1135 char *f;
1136 FILE *fp;
1137 NYD_ENTER;
1139 f = ip->m_filename;
1140 *ispipe = FAL0;
1142 if (f != NULL && f != (char*)-1) {
1143 in.s = f;
1144 in.l = strlen(f);
1145 makeprint(&in, &out);
1146 out.l = delctrl(out.s, out.l);
1147 f = savestrbuf(out.s, out.l);
1148 free(out.s);
1151 if (options & OPT_INTERACTIVE) {
1152 struct str prompt;
1153 char *f2, *f3;
1155 /* TODO Generic function which asks for filename.
1156 * TODO If the current part is the first textpart the target
1157 * TODO is implicit from outer `write' etc! */
1158 /* I18N: Filename input prompt with file type indication */
1159 str_concat_csvl(&prompt, _("Enter filename for part "),
1160 (ip->m_partstring != NULL) ? ip->m_partstring : _("?"),
1161 _(" ("), ip->m_ct_type_plain, _("): "), NULL);
1162 jgetname:
1163 f2 = n_lex_input_cp_addhist(prompt.s, ((f != (char*)-1 && f != NULL)
1164 ? fexpand_nshell_quote(f) : NULL), TRU1);
1165 if(f2 != NULL)
1166 while(spacechar(*f2))
1167 ++f2;
1168 if (f2 == NULL || *f2 == '\0') {
1169 if (options & OPT_D_V)
1170 n_err(_("... skipping this\n"));
1171 fp = NULL;
1172 goto jleave;
1175 if (*f2 == '|')
1176 /* Pipes are expanded by the shell */
1177 f = f2;
1178 else if ((f3 = fexpand(f2, FEXP_LOCAL | FEXP_NSHELL)) == NULL)
1179 /* (Error message written by fexpand()) */
1180 goto jgetname;
1181 else
1182 f = f3;
1184 if (f == NULL || f == (char*)-1) {
1185 fp = NULL;
1186 goto jleave;
1189 if (*f == '|') {
1190 char const *cp;
1192 cp = ok_vlook(SHELL);
1193 if (cp == NULL)
1194 cp = XSHELL;
1195 fp = Popen(f + 1, "w", cp, NULL, 1);
1196 if (!(*ispipe = (fp != NULL)))
1197 n_perr(f, 0);
1198 } else {
1199 if ((fp = Fopen(f, "w")) == NULL)
1200 n_err(_("Cannot open \"%s\"\n"), f);
1202 jleave:
1203 NYD_LEAVE;
1204 return fp;
1207 static void
1208 pipecpy(FILE *pipebuf, FILE *outbuf, FILE *origobuf, struct quoteflt *qf,
1209 ui64_t *stats)
1211 char *line = NULL; /* TODO line pool */
1212 size_t linesize = 0, linelen, cnt;
1213 ssize_t all_sz, sz;
1214 NYD_ENTER;
1216 fflush(pipebuf);
1217 rewind(pipebuf);
1218 cnt = fsize(pipebuf);
1219 all_sz = 0;
1221 quoteflt_reset(qf, outbuf);
1222 while (fgetline(&line, &linesize, &cnt, &linelen, pipebuf, 0) != NULL) {
1223 if ((sz = quoteflt_push(qf, line, linelen)) < 0)
1224 break;
1225 all_sz += sz;
1227 if ((sz = quoteflt_flush(qf)) > 0)
1228 all_sz += sz;
1229 if (line)
1230 free(line);
1232 if (all_sz > 0 && outbuf == origobuf && stats != NULL)
1233 *stats += all_sz;
1234 Fclose(pipebuf);
1235 NYD_LEAVE;
1238 static void
1239 statusput(const struct message *mp, FILE *obuf, struct quoteflt *qf,
1240 ui64_t *stats)
1242 char statout[3], *cp = statout;
1243 NYD_ENTER;
1245 if (mp->m_flag & MREAD)
1246 *cp++ = 'R';
1247 if (!(mp->m_flag & MNEW))
1248 *cp++ = 'O';
1249 *cp = 0;
1250 if (statout[0]) {
1251 int i = fprintf(obuf, "%.*sStatus: %s\n", (int)qf->qf_pfix_len,
1252 (qf->qf_pfix_len > 0 ? qf->qf_pfix : 0), statout);
1253 if (i > 0 && stats != NULL)
1254 *stats += i;
1256 NYD_LEAVE;
1259 static void
1260 xstatusput(const struct message *mp, FILE *obuf, struct quoteflt *qf,
1261 ui64_t *stats)
1263 char xstatout[4];
1264 char *xp = xstatout;
1265 NYD_ENTER;
1267 if (mp->m_flag & MFLAGGED)
1268 *xp++ = 'F';
1269 if (mp->m_flag & MANSWERED)
1270 *xp++ = 'A';
1271 if (mp->m_flag & MDRAFTED)
1272 *xp++ = 'T';
1273 *xp = 0;
1274 if (xstatout[0]) {
1275 int i = fprintf(obuf, "%.*sX-Status: %s\n", (int)qf->qf_pfix_len,
1276 (qf->qf_pfix_len > 0 ? qf->qf_pfix : 0), xstatout);
1277 if (i > 0 && stats != NULL)
1278 *stats += i;
1280 NYD_LEAVE;
1283 static void
1284 put_from_(FILE *fp, struct mimepart *ip, ui64_t *stats)
1286 char const *froma, *date, *nl;
1287 int i;
1288 NYD_ENTER;
1290 if (ip != NULL && ip->m_from != NULL) {
1291 froma = ip->m_from;
1292 date = fakedate(ip->m_time);
1293 nl = "\n";
1294 } else {
1295 froma = myname;
1296 date = time_current.tc_ctime;
1297 nl = "";
1300 n_COLOUR( n_colour_put(fp, n_COLOUR_ID_VIEW_FROM_, NULL); )
1301 i = fprintf(fp, "From %s %s%s", froma, date, nl);
1302 n_COLOUR( n_colour_reset(fp); )
1303 if (i > 0 && stats != NULL)
1304 *stats += i;
1305 NYD_LEAVE;
1308 FL int
1309 sendmp(struct message *mp, FILE *obuf, struct ignoretab *doign,
1310 char const *prefix, enum sendaction action, ui64_t *stats)
1312 struct quoteflt qf;
1313 size_t cnt, sz, i;
1314 FILE *ibuf;
1315 enum mime_parse_flags mpf;
1316 struct mimepart *ip;
1317 int rv = -1, c;
1318 NYD_ENTER;
1320 if (mp == dot && action != SEND_TOSRCH)
1321 pstate |= PS_DID_PRINT_DOT;
1322 if (stats != NULL)
1323 *stats = 0;
1324 quoteflt_init(&qf, prefix);
1326 /* First line is the From_ line, so no headers there to worry about */
1327 if ((ibuf = setinput(&mb, mp, NEED_BODY)) == NULL)
1328 goto jleave;
1330 cnt = mp->m_size;
1331 sz = 0;
1333 char const *cpre = "", *csuf = "";
1334 #ifdef HAVE_COLOUR
1335 struct n_colour_pen *cpen = n_colour_pen_create(n_COLOUR_ID_VIEW_FROM_,NULL);
1336 struct str const *sp = n_colour_pen_to_str(cpen);
1338 if (sp != NULL) {
1339 cpre = sp->s;
1340 sp = n_colour_reset_to_str();
1341 if (sp != NULL)
1342 csuf = sp->s;
1344 #endif
1345 if (mp->m_flag & MNOFROM) {
1346 if (doign != allignore && doign != fwdignore && action != SEND_RFC822)
1347 sz = fprintf(obuf, "%s%.*sFrom %s %s%s\n",
1348 cpre, (int)qf.qf_pfix_len,
1349 (qf.qf_pfix_len != 0 ? qf.qf_pfix : ""), fakefrom(mp),
1350 fakedate(mp->m_time), csuf);
1351 } else if (doign != allignore && doign != fwdignore &&
1352 action != SEND_RFC822) {
1353 if (qf.qf_pfix_len > 0) {
1354 i = fwrite(qf.qf_pfix, sizeof *qf.qf_pfix, qf.qf_pfix_len, obuf);
1355 if (i != qf.qf_pfix_len)
1356 goto jleave;
1357 sz += i;
1359 #ifdef HAVE_COLOUR
1360 if (cpre != NULL) {
1361 fputs(cpre, obuf);
1362 cpre = (char const*)0x1;
1364 #endif
1366 while (cnt > 0 && (c = getc(ibuf)) != EOF) {
1367 #ifdef HAVE_COLOUR
1368 if (c == '\n' && csuf != NULL) {
1369 cpre = (char const*)0x1;
1370 fputs(csuf, obuf);
1372 #endif
1373 putc(c, obuf);
1374 ++sz;
1375 --cnt;
1376 if (c == '\n')
1377 break;
1380 #ifdef HAVE_COLOUR
1381 if (csuf != NULL && cpre != (char const*)0x1)
1382 fputs(csuf, obuf);
1383 #endif
1384 } else {
1385 while (cnt > 0 && (c = getc(ibuf)) != EOF) {
1386 --cnt;
1387 if (c == '\n')
1388 break;
1392 if (sz > 0 && stats != NULL)
1393 *stats += sz;
1395 mpf = MIME_PARSE_NONE;
1396 if (action != SEND_MBOX && action != SEND_RFC822 && action != SEND_SHOW)
1397 mpf |= MIME_PARSE_DECRYPT | MIME_PARSE_PARTS;
1398 if ((ip = mime_parse_msg(mp, mpf)) == NULL)
1399 goto jleave;
1401 rv = sendpart(mp, ip, obuf, doign, &qf, action, stats, 0);
1402 jleave:
1403 quoteflt_destroy(&qf);
1404 NYD_LEAVE;
1405 return rv;
1408 /* s-it-mode */