`list': include argument-type etc. if *verbose*
[s-mailx.git] / send.c
blob7f633c374f5c4928457acfdbc4bf5ccc514446b5
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 sh = ok_vlook(SHELL);
241 if (mhp->mh_flags & MIME_HDL_NEEDSTERM) {
242 sigset_t nset;
243 int pid;
245 sigemptyset(&nset);
246 pid = run_command(sh, NULL, term_infd, COMMAND_FD_PASS, "-c",
247 mhp->mh_shell_cmd, NULL, env_addon);
248 rbuf = (pid < 0) ? NULL : (FILE*)-1;
249 } else {
250 rbuf = Popen(mhp->mh_shell_cmd, "W", sh, env_addon,
251 (mhp->mh_flags & MIME_HDL_ASYNC ? -1 : fileno(*qbuf)));
252 jerror:
253 if (rbuf == NULL)
254 n_err(_("Cannot run MIME type handler \"%s\": %s\n"),
255 mhp->mh_msg, strerror(errno));
256 else {
257 fflush(*qbuf);
258 if (*qbuf != stdout)
259 fflush(stdout);
262 jleave:
263 NYD_LEAVE;
264 return rbuf;
267 SINLINE ssize_t
268 _out(char const *buf, size_t len, FILE *fp, enum conversion convert, enum
269 sendaction action, struct quoteflt *qf, ui64_t *stats, struct str *rest)
271 ssize_t sz = 0, n;
272 int flags;
273 NYD_ENTER;
275 #if 0
276 Well ... it turns out to not work like that since of course a valid
277 RFC 4155 compliant parser, like S-nail, takes care for From_ lines only
278 after an empty line has been seen, which cannot be detected that easily
279 right here!
280 ifdef HAVE_DEBUG /* TODO assert legacy */
281 /* TODO if at all, this CAN only happen for SEND_DECRYPT, since all
282 * TODO other input situations handle RFC 4155 OR, if newly generated,
283 * TODO enforce quoted-printable if there is From_, as "required" by
284 * TODO RFC 5751. The SEND_DECRYPT case is not yet overhauled;
285 * TODO if it may happen in this path, we should just treat decryption
286 * TODO as we do for the other input paths; i.e., handle it in SSL!! */
287 if (action == SEND_MBOX || action == SEND_DECRYPT)
288 assert(!is_head(buf, len, TRU1));
289 #else
290 if ((/*action == SEND_MBOX ||*/ action == SEND_DECRYPT) &&
291 is_head(buf, len, TRU1)) {
292 putc('>', fp);
293 ++sz;
295 #endif
297 flags = ((int)action & _TD_EOF);
298 action &= ~_TD_EOF;
299 n = mime_write(buf, len, fp,
300 action == SEND_MBOX ? CONV_NONE : convert,
301 flags | ((action == SEND_TODISP || action == SEND_TODISP_ALL ||
302 action == SEND_QUOTE || action == SEND_QUOTE_ALL)
303 ? TD_ISPR | TD_ICONV
304 : (action == SEND_TOSRCH || action == SEND_TOPIPE)
305 ? TD_ICONV : (action == SEND_SHOW ? TD_ISPR : TD_NONE)),
306 qf, rest);
307 if (n < 0)
308 sz = n;
309 else if (n > 0) {
310 sz += n;
311 if (stats != NULL)
312 *stats += sz;
314 NYD_LEAVE;
315 return sz;
318 static void
319 _send_onpipe(int signo)
321 NYD_X; /* Signal handler */
322 UNUSED(signo);
323 siglongjmp(_send_pipejmp, 1);
326 static sigjmp_buf __sendp_actjmp; /* TODO someday.. */
327 static int __sendp_sig; /* TODO someday.. */
328 static sighandler_type __sendp_opipe;
329 static void
330 __sendp_onsig(int sig) /* TODO someday, we won't need it no more */
332 NYD_X; /* Signal handler */
333 __sendp_sig = sig;
334 siglongjmp(__sendp_actjmp, 1);
337 static int
338 sendpart(struct message *zmp, struct mimepart *ip, FILE * volatile obuf,
339 struct ignoretab *doign, struct quoteflt *qf,
340 enum sendaction volatile action, ui64_t * volatile stats, int level)
342 int volatile rv = 0;
343 struct mime_handler mh;
344 struct str rest;
345 char *line = NULL, *cp, *cp2, *start;
346 char const *tmpname = NULL;
347 size_t linesize = 0, linelen, cnt;
348 int volatile term_infd;
349 int dostat, c;
350 struct mimepart *volatile np;
351 FILE * volatile ibuf = NULL, * volatile pbuf = obuf,
352 * volatile qbuf = obuf, *origobuf = obuf;
353 enum conversion volatile convert;
354 sighandler_type volatile oldpipe = SIG_DFL;
355 NYD_ENTER;
357 if (ip->m_mimecontent == MIME_PKCS7 && ip->m_multipart &&
358 action != SEND_MBOX && action != SEND_RFC822 && action != SEND_SHOW)
359 goto jskip;
361 dostat = 0;
362 if (level == 0) {
363 if (doign != NULL) {
364 if (!is_ign("status", 6, doign))
365 dostat |= 1;
366 if (!is_ign("x-status", 8, doign))
367 dostat |= 2;
368 } else
369 dostat = 3;
371 if ((ibuf = setinput(&mb, (struct message*)ip, NEED_BODY)) == NULL) {
372 rv = -1;
373 goto jleave;
375 cnt = ip->m_size;
377 if (ip->m_mimecontent == MIME_DISCARD)
378 goto jskip;
380 if (!(ip->m_flag & MNOFROM))
381 while (cnt && (c = getc(ibuf)) != EOF) {
382 cnt--;
383 if (c == '\n')
384 break;
386 convert = (action == SEND_TODISP || action == SEND_TODISP_ALL ||
387 action == SEND_QUOTE || action == SEND_QUOTE_ALL ||
388 action == SEND_TOSRCH)
389 ? CONV_FROMHDR : CONV_NONE;
391 /* Work the headers */
392 quoteflt_reset(qf, obuf);
393 /* C99 */{
394 enum {
395 HPS_NONE = 0,
396 HPS_IN_FIELD = 1<<0,
397 HPS_IGNORE = 1<<1,
398 HPS_ISENC_1 = 1<<2,
399 HPS_ISENC_2 = 1<<3
400 } hps = HPS_NONE;
401 size_t lineno = 0;
403 while (fgetline(&line, &linesize, &cnt, &linelen, ibuf, 0)) {
404 ++lineno;
405 if (line[0] == '\n') {
406 /* If line is blank, we've reached end of headers, so force out
407 * status: field and note that we are no longer in header fields */
408 if (dostat & 1)
409 statusput(zmp, obuf, qf, stats);
410 if (dostat & 2)
411 xstatusput(zmp, obuf, qf, stats);
412 if (doign != allignore)
413 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
414 break;
417 hps &= ~HPS_ISENC_1;
418 if ((hps & HPS_IN_FIELD) && blankchar(line[0])) {
419 /* If this line is a continuation (SP / HT) of a previous header
420 * field, determine if the start of the line is a MIME encoded word */
421 if (hps & HPS_ISENC_2) {
422 for (cp = line; blankchar(*cp); ++cp)
424 if (cp > line && linelen - PTR2SIZE(cp - line) > 8 &&
425 cp[0] == '=' && cp[1] == '?')
426 hps |= HPS_ISENC_1;
428 } else {
429 /* Pick up the header field if we have one */
430 for (cp = line; (c = *cp & 0377) && c != ':' && !spacechar(c); ++cp)
432 cp2 = cp;
433 while (spacechar(*cp))
434 ++cp;
435 if (cp[0] != ':') {
436 if (lineno != 1)
437 n_err(_("Malformed message: headers and body not separated "
438 "(with empty line)\n"));
439 /* Not a header line, force out status: This happens in uucp style
440 * mail where there are no headers at all */
441 if (level == 0 /*&& lineno == 1*/) {
442 if (dostat & 1)
443 statusput(zmp, obuf, qf, stats);
444 if (dostat & 2)
445 xstatusput(zmp, obuf, qf, stats);
447 if (doign != allignore)
448 _out("\n", 1, obuf, CONV_NONE,SEND_MBOX, qf, stats, NULL);
449 break;
452 /* If it is an ignored field and we care about such things, skip it.
453 * Misuse dostat also for another bit xxx use a bitenum + for more */
454 if (ok_blook(keep_content_length))
455 dostat |= 1 << 2;
456 c = *cp2;
457 *cp2 = 0; /* temporarily null terminate */
458 if ((doign && is_ign(line, PTR2SIZE(cp2 - line), doign)) ||
459 (action == SEND_MBOX && !(dostat & (1 << 2)) &&
460 (!asccasecmp(line, "content-length") ||
461 !asccasecmp(line, "lines"))))
462 hps |= HPS_IGNORE;
463 else if (!asccasecmp(line, "status")) {
464 /* If field is "status," go compute and print real Status: field */
465 if (dostat & 1) {
466 statusput(zmp, obuf, qf, stats);
467 dostat &= ~1;
468 hps |= HPS_IGNORE;
470 } else if (!asccasecmp(line, "x-status")) {
471 /* If field is "status," go compute and print real Status: field */
472 if (dostat & 2) {
473 xstatusput(zmp, obuf, qf, stats);
474 dostat &= ~2;
475 hps |= HPS_IGNORE;
477 } else {
478 hps &= ~HPS_IGNORE;
479 /* For colourization we need the complete line, so save it */
480 /* XXX This is all temporary (colour belongs into backend), so
481 * XXX use tmpname as a temporary storage in the meanwhile */
482 #ifdef HAVE_COLOUR
483 if (pstate & PS_COLOUR_ACTIVE)
484 tmpname = savestrbuf(line, PTR2SIZE(cp2 - line));
485 #endif
487 *cp2 = c;
488 dostat &= ~(1 << 2);
489 hps |= HPS_IN_FIELD;
492 /* Determine if the end of the line is a MIME encoded word */
493 /* TODO geeeh! all this lengthy stuff that follows is about is dealing
494 * TODO with header follow lines, and it should be up to the backend
495 * TODO what happens and what not, i.e., it doesn't matter wether it's
496 * TODO a MIME-encoded word or not, as long as a single separating space
497 * TODO remains in between lines (the MIME stuff will correctly remove
498 * TODO whitespace in between multiple adjacent encoded words) */
499 hps &= ~HPS_ISENC_2;
500 if (cnt && (c = getc(ibuf)) != EOF) {
501 if (blankchar(c)) {
502 cp = line + linelen - 1;
503 if (linelen > 0 && *cp == '\n')
504 --cp;
505 while (cp >= line && whitechar(*cp))
506 --cp;
507 if (PTR2SIZE(cp - line > 8) && cp[0] == '=' && cp[-1] == '?')
508 hps |= HPS_ISENC_2;
510 ungetc(c, ibuf);
513 if (!(hps & HPS_IGNORE)) {
514 size_t len = linelen;
515 start = line;
516 if (action == SEND_TODISP || action == SEND_TODISP_ALL ||
517 action == SEND_QUOTE || action == SEND_QUOTE_ALL ||
518 action == SEND_TOSRCH) {
519 /* Strip blank characters if two MIME-encoded words follow on
520 * continuing lines */
521 if (hps & HPS_ISENC_1)
522 while (len > 0 && blankchar(*start)) {
523 ++start;
524 --len;
526 if (hps & HPS_ISENC_2)
527 if (len > 0 && start[len - 1] == '\n')
528 --len;
529 while (len > 0 && blankchar(start[len - 1]))
530 --len;
532 #ifdef HAVE_COLOUR
534 bool_t colour_stripped = FAL0;
535 if (tmpname != NULL) {
536 n_colour_put(obuf, n_COLOUR_ID_VIEW_HEADER, tmpname);
537 if (len > 0 && start[len - 1] == '\n') {
538 colour_stripped = TRU1;
539 --len;
542 #endif
543 _out(start, len, obuf, convert, action, qf, stats, NULL);
544 #ifdef HAVE_COLOUR
545 if (tmpname != NULL) {
546 n_colour_reset(obuf);
547 if (colour_stripped)
548 putc('\n', obuf);
551 #endif
552 if (ferror(obuf)) {
553 free(line);
554 rv = -1;
555 goto jleave;
559 } /* C99 */
560 quoteflt_flush(qf);
561 free(line);
562 line = NULL;
563 tmpname = NULL;
565 jskip:
566 memset(&mh, 0, sizeof mh);
568 switch (ip->m_mimecontent) {
569 case MIME_822:
570 switch (action) {
571 case SEND_TODISP:
572 case SEND_TODISP_ALL:
573 case SEND_QUOTE:
574 case SEND_QUOTE_ALL:
575 if (ok_blook(rfc822_body_from_)) {
576 if (qf->qf_pfix_len > 0) {
577 size_t i = fwrite(qf->qf_pfix, sizeof *qf->qf_pfix,
578 qf->qf_pfix_len, obuf);
579 if (i == qf->qf_pfix_len && stats != NULL)
580 *stats += i;
582 put_from_(obuf, ip->m_multipart, stats);
584 /* FALLTHRU */
585 case SEND_TOSRCH:
586 case SEND_DECRYPT:
587 goto jmulti;
588 case SEND_TOFILE:
589 case SEND_TOPIPE:
590 if (ok_blook(rfc822_body_from_))
591 put_from_(obuf, ip->m_multipart, stats);
592 /* FALLTHRU */
593 case SEND_MBOX:
594 case SEND_RFC822:
595 case SEND_SHOW:
596 break;
598 break;
599 case MIME_TEXT_HTML:
600 case MIME_TEXT:
601 case MIME_TEXT_PLAIN:
602 switch (action) {
603 case SEND_TODISP:
604 case SEND_TODISP_ALL:
605 case SEND_QUOTE:
606 case SEND_QUOTE_ALL:
607 switch (mime_type_handler(&mh, ip, action)) {
608 case MIME_HDL_MSG:
609 _out(mh.mh_msg.s, mh.mh_msg.l, obuf, CONV_NONE, SEND_MBOX, qf,
610 stats, NULL);
611 /* We would print this as plain text, so better force going home */
612 goto jleave;
613 default:
614 break;
616 /* FALLTRHU */
617 default:
618 break;
620 break;
621 case MIME_DISCARD:
622 if (action != SEND_DECRYPT)
623 goto jleave;
624 break;
625 case MIME_PKCS7:
626 if (action != SEND_MBOX && action != SEND_RFC822 &&
627 action != SEND_SHOW && ip->m_multipart != NULL)
628 goto jmulti;
629 /* FALLTHRU */
630 default:
631 switch (action) {
632 case SEND_TODISP:
633 case SEND_TODISP_ALL:
634 case SEND_QUOTE:
635 case SEND_QUOTE_ALL:
636 switch (mime_type_handler(&mh, ip, action)) {
637 case MIME_HDL_MSG:
638 _out(mh.mh_msg.s, mh.mh_msg.l, obuf, CONV_NONE, SEND_MBOX, qf,
639 stats, NULL);
640 /* We would print this as plain text, so better force going home */
641 goto jleave;
642 case MIME_HDL_CMD:
643 /* FIXME WE NEED TO DO THAT IF WE ARE THE ONLY MAIL
644 * FIXME CONTENT !! */
645 case MIME_HDL_TEXT:
646 break;
647 default:
648 case MIME_HDL_NULL:
649 if (level == 0 && cnt) {
650 char const *x = _("[-- Binary content --]\n");
651 _out(x, strlen(x), obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
653 goto jleave;
655 break;
656 case SEND_TOFILE:
657 case SEND_TOPIPE:
658 case SEND_TOSRCH:
659 case SEND_DECRYPT:
660 case SEND_MBOX:
661 case SEND_RFC822:
662 case SEND_SHOW:
663 break;
665 break;
666 case MIME_ALTERNATIVE:
667 if ((action == SEND_TODISP || action == SEND_QUOTE) &&
668 !ok_blook(print_alternatives)) {
669 /* XXX This (a) should not remain (b) should be own fun
670 * TODO (despite the fact that v15 will do this completely differently
671 * TODO by having an action-specific "manager" that will traverse the
672 * TODO parsed MIME tree and decide for each part wether it'll be
673 * TODO displayed or not *before* we walk the tree for doing action */
674 struct mpstack {
675 struct mpstack *outer;
676 struct mimepart *mp;
677 } outermost, * volatile curr, * volatile mpsp;
678 bool_t volatile neednl, hadpart;
679 struct n_sigman smalter;
681 (curr = &outermost)->outer = NULL;
682 curr->mp = ip;
683 neednl = hadpart = FAL0;
685 n_SIGMAN_ENTER_SWITCH(&smalter, n_SIGMAN_ALL) {
686 case 0:
687 break;
688 default:
689 rv = -1;
690 goto jalter_leave;
693 for (np = ip->m_multipart;;) {
694 jalter_redo:
695 for (; np != NULL; np = np->m_nextpart) {
696 if (action != SEND_QUOTE && np->m_ct_type_plain != NULL) {
697 if (neednl)
698 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
699 _print_part_info(obuf, np, doign, level, qf, stats);
701 neednl = TRU1;
703 switch (np->m_mimecontent) {
704 case MIME_ALTERNATIVE:
705 case MIME_RELATED:
706 case MIME_MULTI:
707 case MIME_DIGEST:
708 mpsp = salloc(sizeof *mpsp);
709 mpsp->outer = curr;
710 mpsp->mp = np->m_multipart;
711 curr->mp = np;
712 curr = mpsp;
713 np = mpsp->mp;
714 neednl = FAL0;
715 goto jalter_redo;
716 default:
717 if (hadpart)
718 break;
719 switch (mime_type_handler(&mh, np, action)) {
720 default:
721 mh.mh_flags = MIME_HDL_NULL;
722 continue; /* break; break; */
723 case MIME_HDL_PTF:
724 if (!ok_blook(mime_alternative_favour_rich)) {/* TODO */
725 struct mimepart *x = np;
727 while ((x = x->m_nextpart) != NULL) {
728 struct mime_handler mhx;
730 if (x->m_mimecontent == MIME_TEXT_PLAIN ||
731 mime_type_handler(&mhx, x, action) ==
732 MIME_HDL_TEXT)
733 break;
735 if (x != NULL)
736 continue; /* break; break; */
737 goto jalter_plain;
739 /* FALLTHRU */
740 case MIME_HDL_TEXT:
741 break;
743 /* FALLTHRU */
744 case MIME_TEXT_PLAIN:
745 if (hadpart)
746 break;
747 if (ok_blook(mime_alternative_favour_rich)) { /* TODO */
748 struct mimepart *x = np;
750 /* TODO twice TODO, we should dive into /related and
751 * TODO check wether that has rich parts! */
752 while ((x = x->m_nextpart) != NULL) {
753 struct mime_handler mhx;
755 switch (mime_type_handler(&mhx, x, action)) {
756 case MIME_HDL_PTF:
757 break;
758 default:
759 continue;
761 break;
763 if (x != NULL)
764 continue; /* break; break; */
766 jalter_plain:
767 quoteflt_flush(qf);
768 if (action == SEND_QUOTE && hadpart) {
769 struct quoteflt *dummy = quoteflt_dummy();
770 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, dummy, stats,
771 NULL);
772 quoteflt_flush(dummy);
774 hadpart = TRU1;
775 neednl = FAL0;
776 rv = sendpart(zmp, np, obuf, doign, qf, action, stats,
777 level + 1);
778 quoteflt_reset(qf, origobuf);
780 if (rv < 0)
781 curr = &outermost; /* Cause overall loop termination */
782 break;
786 mpsp = curr->outer;
787 if (mpsp == NULL)
788 break;
789 curr = mpsp;
790 np = curr->mp->m_nextpart;
792 jalter_leave:
793 n_sigman_leave(&smalter, n_SIGMAN_VIPSIGS_NTTYOUT);
794 goto jleave;
796 /* FALLTHRU */
797 case MIME_MULTI:
798 case MIME_DIGEST:
799 case MIME_RELATED:
800 switch (action) {
801 case SEND_TODISP:
802 case SEND_TODISP_ALL:
803 case SEND_QUOTE:
804 case SEND_QUOTE_ALL:
805 case SEND_TOFILE:
806 case SEND_TOPIPE:
807 case SEND_TOSRCH:
808 case SEND_DECRYPT:
809 jmulti:
810 if ((action == SEND_TODISP || action == SEND_TODISP_ALL) &&
811 ip->m_multipart != NULL &&
812 ip->m_multipart->m_mimecontent == MIME_DISCARD &&
813 ip->m_multipart->m_nextpart == NULL) {
814 char const *x = _("[Missing multipart boundary - use \"show\" "
815 "to display the raw message]\n");
816 _out(x, strlen(x), obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
819 for (np = ip->m_multipart; np != NULL; np = np->m_nextpart) {
820 bool_t ispipe;
822 if (np->m_mimecontent == MIME_DISCARD && action != SEND_DECRYPT)
823 continue;
825 ispipe = FAL0;
826 switch (action) {
827 case SEND_TOFILE:
828 if (np->m_partstring && !strcmp(np->m_partstring, "1"))
829 break;
830 stats = NULL;
831 /* TODO Always open multipart on /dev/null, it's a hack to be
832 * TODO able to dive into that structure, and still better
833 * TODO than asking the user for something stupid.
834 * TODO oh, wait, we did ask for a filename for this MIME mail,
835 * TODO and that outer container is useless anyway ;-P */
836 if (np->m_multipart != NULL) {
837 if ((obuf = Fopen("/dev/null", "w")) == NULL)
838 continue;
839 } else if ((obuf = newfile(np, &ispipe)) == NULL)
840 continue;
841 if (!ispipe)
842 break;
843 if (sigsetjmp(_send_pipejmp, 1)) {
844 rv = -1;
845 goto jpipe_close;
847 oldpipe = safe_signal(SIGPIPE, &_send_onpipe);
848 break;
849 case SEND_TODISP:
850 case SEND_TODISP_ALL:
851 if (ip->m_mimecontent != MIME_ALTERNATIVE &&
852 ip->m_mimecontent != MIME_RELATED &&
853 ip->m_mimecontent != MIME_DIGEST &&
854 ip->m_mimecontent != MIME_MULTI)
855 break;
856 _print_part_info(obuf, np, doign, level, qf, stats);
857 break;
858 case SEND_QUOTE:
859 case SEND_QUOTE_ALL:
860 case SEND_MBOX:
861 case SEND_RFC822:
862 case SEND_SHOW:
863 case SEND_TOSRCH:
864 case SEND_DECRYPT:
865 case SEND_TOPIPE:
866 break;
869 quoteflt_flush(qf);
870 if ((action == SEND_QUOTE || action == SEND_QUOTE_ALL) &&
871 np->m_multipart == NULL && ip->m_parent != NULL) {
872 struct quoteflt *dummy = quoteflt_dummy();
873 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, dummy, stats,
874 NULL);
875 quoteflt_flush(dummy);
877 if (sendpart(zmp, np, obuf, doign, qf, action, stats, level+1) < 0)
878 rv = -1;
879 quoteflt_reset(qf, origobuf);
881 if (action == SEND_QUOTE) {
882 if (ip->m_mimecontent != MIME_RELATED)
883 break;
885 if (action == SEND_TOFILE && obuf != origobuf) {
886 if (!ispipe)
887 Fclose(obuf);
888 else {
889 jpipe_close:
890 safe_signal(SIGPIPE, SIG_IGN);
891 Pclose(obuf, TRU1);
892 safe_signal(SIGPIPE, oldpipe);
896 goto jleave;
897 case SEND_MBOX:
898 case SEND_RFC822:
899 case SEND_SHOW:
900 break;
902 break;
905 /* Copy out message body */
906 if (doign == allignore && level == 0) /* skip final blank line */
907 --cnt;
908 switch (ip->m_mime_enc) {
909 case MIMEE_BIN:
910 case MIMEE_7B:
911 case MIMEE_8B:
912 convert = CONV_NONE;
913 break;
914 case MIMEE_QP:
915 convert = CONV_FROMQP;
916 break;
917 case MIMEE_B64:
918 switch (ip->m_mimecontent) {
919 case MIME_TEXT:
920 case MIME_TEXT_PLAIN:
921 case MIME_TEXT_HTML:
922 convert = CONV_FROMB64_T;
923 break;
924 default:
925 switch (mh.mh_flags & MIME_HDL_TYPE_MASK) {
926 case MIME_HDL_TEXT:
927 case MIME_HDL_PTF:
928 convert = CONV_FROMB64_T;
929 break;
930 default:
931 convert = CONV_FROMB64;
932 break;
934 break;
936 break;
937 default:
938 convert = CONV_NONE;
941 if (action == SEND_DECRYPT || action == SEND_MBOX ||
942 action == SEND_RFC822 || action == SEND_SHOW)
943 convert = CONV_NONE;
944 #ifdef HAVE_ICONV
945 if ((action == SEND_TODISP || action == SEND_TODISP_ALL ||
946 action == SEND_QUOTE || action == SEND_QUOTE_ALL ||
947 action == SEND_TOSRCH) &&
948 (ip->m_mimecontent == MIME_TEXT_PLAIN ||
949 ip->m_mimecontent == MIME_TEXT_HTML ||
950 ip->m_mimecontent == MIME_TEXT ||
951 (mh.mh_flags & MIME_HDL_TYPE_MASK) == MIME_HDL_TEXT ||
952 (mh.mh_flags & MIME_HDL_TYPE_MASK) == MIME_HDL_PTF)) {
953 char const *tcs = charset_get_lc();
955 if (iconvd != (iconv_t)-1)
956 n_iconv_close(iconvd);
957 /* TODO Since Base64 has an odd 4:3 relation in between input
958 * TODO and output an input line may end with a partial
959 * TODO multibyte character; this is no problem at all unless
960 * TODO we send to the display or whatever, i.e., ensure
961 * TODO makeprint() or something; to avoid this trap, *force*
962 * TODO iconv(), in which case this layer will handle leftovers
963 * TODO correctly */
964 if (convert == CONV_FROMB64_T || (asccasecmp(tcs, ip->m_charset) &&
965 asccasecmp(charset_get_7bit(), ip->m_charset))) {
966 iconvd = n_iconv_open(tcs, ip->m_charset);
968 * TODO errors should DEFINETELY not be scrolled away!
969 * TODO what about an error buffer (think old shsp(1)),
970 * TODO re-dump errors since last snapshot when the
971 * TODO command loop enters again? i.e., at least print
972 * TODO "There were errors ?" before the next prompt,
973 * TODO so that the user can look at the error buffer?
975 if (iconvd == (iconv_t)-1 && errno == EINVAL) {
976 n_err(_("Cannot convert from %s to %s\n"), ip->m_charset, tcs);
977 /*rv = 1; goto jleave;*/
981 #endif
983 switch (mh.mh_flags & MIME_HDL_TYPE_MASK) {
984 case MIME_HDL_CMD:
985 case MIME_HDL_PTF:
986 tmpname = NULL;
987 qbuf = obuf;
989 term_infd = COMMAND_FD_PASS;
990 if (mh.mh_flags & (MIME_HDL_TMPF | MIME_HDL_NEEDSTERM)) {
991 enum oflags of;
993 of = OF_RDWR | OF_REGISTER;
994 if (!(mh.mh_flags & MIME_HDL_TMPF)) {
995 term_infd = 0;
996 mh.mh_flags |= MIME_HDL_TMPF_FILL;
997 of |= OF_UNLINK;
998 } else if (mh.mh_flags & MIME_HDL_TMPF_UNLINK)
999 of |= OF_REGISTER_UNLINK;
1001 if ((pbuf = Ftmp((mh.mh_flags & MIME_HDL_TMPF ? &cp : NULL),
1002 (mh.mh_flags & MIME_HDL_TMPF_FILL ? "mimehdlfill" : "mimehdl"),
1003 of)) == NULL)
1004 goto jesend;
1006 if (mh.mh_flags & MIME_HDL_TMPF) {
1007 tmpname = savestr(cp);
1008 Ftmp_free(&cp);
1011 if (mh.mh_flags & MIME_HDL_TMPF_FILL) {
1012 if (term_infd == 0)
1013 term_infd = fileno(pbuf);
1014 goto jsend;
1018 jpipe_for_real:
1019 pbuf = _pipefile(&mh, ip, UNVOLATILE(&qbuf), tmpname, term_infd);
1020 if (pbuf == NULL) {
1021 jesend:
1022 pbuf = qbuf = NULL;
1023 rv = -1;
1024 goto jend;
1025 } else if ((mh.mh_flags & MIME_HDL_NEEDSTERM) && pbuf == (FILE*)-1) {
1026 pbuf = qbuf = NULL;
1027 goto jend;
1029 tmpname = NULL;
1030 action = SEND_TOPIPE;
1031 if (pbuf != qbuf) {
1032 oldpipe = safe_signal(SIGPIPE, &_send_onpipe);
1033 if (sigsetjmp(_send_pipejmp, 1))
1034 goto jend;
1036 break;
1038 default:
1039 mh.mh_flags = MIME_HDL_NULL;
1040 pbuf = qbuf = obuf;
1041 break;
1044 jsend:
1046 bool_t volatile eof;
1047 ui32_t save_qf_pfix_len = qf->qf_pfix_len;
1048 ui64_t *save_stats = stats;
1050 if (pbuf != origobuf) {
1051 qf->qf_pfix_len = 0; /* XXX legacy (remove filter instead) */
1052 stats = NULL;
1054 eof = FAL0;
1055 rest.s = NULL;
1056 rest.l = 0;
1058 if (pbuf == qbuf) {
1059 __sendp_sig = 0;
1060 __sendp_opipe = safe_signal(SIGPIPE, &__sendp_onsig);
1061 if (sigsetjmp(__sendp_actjmp, 1)) {
1062 if (rest.s != NULL)
1063 free(rest.s);
1064 free(line);
1065 #ifdef HAVE_ICONV
1066 if (iconvd != (iconv_t)-1)
1067 n_iconv_close(iconvd);
1068 #endif
1069 safe_signal(SIGPIPE, __sendp_opipe);
1070 n_raise(__sendp_sig);
1074 quoteflt_reset(qf, pbuf);
1075 while (!eof && fgetline(&line, &linesize, &cnt, &linelen, ibuf, 0)) {
1076 joutln:
1077 if (_out(line, linelen, pbuf, convert, action, qf, stats, &rest) < 0 ||
1078 ferror(pbuf)) {
1079 rv = -1; /* XXX Should bail away?! */
1080 break;
1083 if (!eof && rest.l != 0) {
1084 linelen = 0;
1085 eof = TRU1;
1086 action |= _TD_EOF;
1087 goto joutln;
1089 quoteflt_flush(qf);
1091 if (mh.mh_flags & MIME_HDL_TMPF_FILL) {
1092 mh.mh_flags &= ~MIME_HDL_TMPF_FILL;
1093 fflush(pbuf);
1094 really_rewind(pbuf);
1095 /* Don't Fclose() the Ftmp() thing due to OF_REGISTER_UNLINK++ */
1096 goto jpipe_for_real;
1099 if (pbuf == qbuf)
1100 safe_signal(SIGPIPE, __sendp_opipe);
1102 if (rest.s != NULL)
1103 free(rest.s);
1105 if (pbuf != origobuf) {
1106 qf->qf_pfix_len = save_qf_pfix_len;
1107 stats = save_stats;
1111 jend:
1112 if (line != NULL)
1113 free(line);
1114 if (pbuf != qbuf) {
1115 safe_signal(SIGPIPE, SIG_IGN);
1116 Pclose(pbuf, !(mh.mh_flags & MIME_HDL_ASYNC));
1117 safe_signal(SIGPIPE, oldpipe);
1118 if (qbuf != NULL && qbuf != obuf)
1119 pipecpy(qbuf, obuf, origobuf, qf, stats);
1121 #ifdef HAVE_ICONV
1122 if (iconvd != (iconv_t)-1)
1123 n_iconv_close(iconvd);
1124 #endif
1125 jleave:
1126 NYD_LEAVE;
1127 return rv;
1130 static FILE *
1131 newfile(struct mimepart *ip, bool_t *ispipe)
1133 struct str in, out;
1134 char *f;
1135 FILE *fp;
1136 NYD_ENTER;
1138 f = ip->m_filename;
1139 *ispipe = FAL0;
1141 if (f != NULL && f != (char*)-1) {
1142 in.s = f;
1143 in.l = strlen(f);
1144 makeprint(&in, &out);
1145 out.l = delctrl(out.s, out.l);
1146 f = savestrbuf(out.s, out.l);
1147 free(out.s);
1150 if (options & OPT_INTERACTIVE) {
1151 struct str prompt;
1152 struct n_string shou, *shoup;
1153 char *f2, *f3;
1155 shoup = n_string_creat_auto(&shou);
1157 /* TODO Generic function which asks for filename.
1158 * TODO If the current part is the first textpart the target
1159 * TODO is implicit from outer `write' etc! */
1160 /* I18N: Filename input prompt with file type indication */
1161 str_concat_csvl(&prompt, _("Enter filename for part "),
1162 (ip->m_partstring != NULL) ? ip->m_partstring : _("?"),
1163 _(" ("), ip->m_ct_type_plain, _("): "), NULL);
1164 jgetname:
1165 f2 = n_lex_input_cp_addhist(prompt.s, ((f != (char*)-1 && f != NULL)
1166 ? n_shell_quote_cp(f, FAL0) : NULL), TRU1);
1167 if(f2 != NULL){
1168 in.s = UNCONST(f2);
1169 in.l = UIZ_MAX;
1170 if((n_shell_parse_token(shoup, &in, n_SHEXP_PARSE_TRUNC |
1171 n_SHEXP_PARSE_TRIMSPACE | n_SHEXP_PARSE_LOG |
1172 n_SHEXP_PARSE_IGNORE_EMPTY) &
1173 (n_SHEXP_STATE_OUTPUT | n_SHEXP_STATE_ERR_MASK)) !=
1174 n_SHEXP_STATE_OUTPUT)
1175 goto jgetname;
1176 if(in.l != 0)
1177 goto jgetname;
1178 f2 = n_string_cp(shoup);
1180 if (f2 == NULL || *f2 == '\0') {
1181 if (options & OPT_D_V)
1182 n_err(_("... skipping this\n"));
1183 n_string_gut(shoup);
1184 fp = NULL;
1185 goto jleave;
1188 if (*f2 == '|')
1189 /* Pipes are expanded by the shell */
1190 f = f2;
1191 else if ((f3 = fexpand(f2, FEXP_LOCAL | FEXP_NVAR)) == NULL)
1192 /* (Error message written by fexpand()) */
1193 goto jgetname;
1194 else
1195 f = f3;
1197 n_string_gut(shoup);
1199 if (f == NULL || f == (char*)-1) {
1200 fp = NULL;
1201 goto jleave;
1204 if (*f == '|') {
1205 fp = Popen(f + 1, "w", ok_vlook(SHELL), NULL, 1);
1206 if (!(*ispipe = (fp != NULL)))
1207 n_perr(f, 0);
1208 } else {
1209 if ((fp = Fopen(f, "w")) == NULL)
1210 n_err(_("Cannot open \"%s\"\n"), f);
1212 jleave:
1213 NYD_LEAVE;
1214 return fp;
1217 static void
1218 pipecpy(FILE *pipebuf, FILE *outbuf, FILE *origobuf, struct quoteflt *qf,
1219 ui64_t *stats)
1221 char *line = NULL; /* TODO line pool */
1222 size_t linesize = 0, linelen, cnt;
1223 ssize_t all_sz, sz;
1224 NYD_ENTER;
1226 fflush(pipebuf);
1227 rewind(pipebuf);
1228 cnt = fsize(pipebuf);
1229 all_sz = 0;
1231 quoteflt_reset(qf, outbuf);
1232 while (fgetline(&line, &linesize, &cnt, &linelen, pipebuf, 0) != NULL) {
1233 if ((sz = quoteflt_push(qf, line, linelen)) < 0)
1234 break;
1235 all_sz += sz;
1237 if ((sz = quoteflt_flush(qf)) > 0)
1238 all_sz += sz;
1239 if (line)
1240 free(line);
1242 if (all_sz > 0 && outbuf == origobuf && stats != NULL)
1243 *stats += all_sz;
1244 Fclose(pipebuf);
1245 NYD_LEAVE;
1248 static void
1249 statusput(const struct message *mp, FILE *obuf, struct quoteflt *qf,
1250 ui64_t *stats)
1252 char statout[3], *cp = statout;
1253 NYD_ENTER;
1255 if (mp->m_flag & MREAD)
1256 *cp++ = 'R';
1257 if (!(mp->m_flag & MNEW))
1258 *cp++ = 'O';
1259 *cp = 0;
1260 if (statout[0]) {
1261 int i = fprintf(obuf, "%.*sStatus: %s\n", (int)qf->qf_pfix_len,
1262 (qf->qf_pfix_len > 0 ? qf->qf_pfix : 0), statout);
1263 if (i > 0 && stats != NULL)
1264 *stats += i;
1266 NYD_LEAVE;
1269 static void
1270 xstatusput(const struct message *mp, FILE *obuf, struct quoteflt *qf,
1271 ui64_t *stats)
1273 char xstatout[4];
1274 char *xp = xstatout;
1275 NYD_ENTER;
1277 if (mp->m_flag & MFLAGGED)
1278 *xp++ = 'F';
1279 if (mp->m_flag & MANSWERED)
1280 *xp++ = 'A';
1281 if (mp->m_flag & MDRAFTED)
1282 *xp++ = 'T';
1283 *xp = 0;
1284 if (xstatout[0]) {
1285 int i = fprintf(obuf, "%.*sX-Status: %s\n", (int)qf->qf_pfix_len,
1286 (qf->qf_pfix_len > 0 ? qf->qf_pfix : 0), xstatout);
1287 if (i > 0 && stats != NULL)
1288 *stats += i;
1290 NYD_LEAVE;
1293 static void
1294 put_from_(FILE *fp, struct mimepart *ip, ui64_t *stats)
1296 char const *froma, *date, *nl;
1297 int i;
1298 NYD_ENTER;
1300 if (ip != NULL && ip->m_from != NULL) {
1301 froma = ip->m_from;
1302 date = fakedate(ip->m_time);
1303 nl = "\n";
1304 } else {
1305 froma = myname;
1306 date = time_current.tc_ctime;
1307 nl = "";
1310 n_COLOUR( n_colour_put(fp, n_COLOUR_ID_VIEW_FROM_, NULL); )
1311 i = fprintf(fp, "From %s %s%s", froma, date, nl);
1312 n_COLOUR( n_colour_reset(fp); )
1313 if (i > 0 && stats != NULL)
1314 *stats += i;
1315 NYD_LEAVE;
1318 FL int
1319 sendmp(struct message *mp, FILE *obuf, struct ignoretab *doign,
1320 char const *prefix, enum sendaction action, ui64_t *stats)
1322 struct quoteflt qf;
1323 size_t cnt, sz, i;
1324 FILE *ibuf;
1325 enum mime_parse_flags mpf;
1326 struct mimepart *ip;
1327 int rv = -1, c;
1328 NYD_ENTER;
1330 if (mp == dot && action != SEND_TOSRCH)
1331 pstate |= PS_DID_PRINT_DOT;
1332 if (stats != NULL)
1333 *stats = 0;
1334 quoteflt_init(&qf, prefix);
1336 /* First line is the From_ line, so no headers there to worry about */
1337 if ((ibuf = setinput(&mb, mp, NEED_BODY)) == NULL)
1338 goto jleave;
1340 cnt = mp->m_size;
1341 sz = 0;
1343 char const *cpre = "", *csuf = "";
1344 #ifdef HAVE_COLOUR
1345 struct n_colour_pen *cpen = n_colour_pen_create(n_COLOUR_ID_VIEW_FROM_,NULL);
1346 struct str const *sp = n_colour_pen_to_str(cpen);
1348 if (sp != NULL) {
1349 cpre = sp->s;
1350 sp = n_colour_reset_to_str();
1351 if (sp != NULL)
1352 csuf = sp->s;
1354 #endif
1355 if (mp->m_flag & MNOFROM) {
1356 if (doign != allignore && doign != fwdignore && action != SEND_RFC822)
1357 sz = fprintf(obuf, "%s%.*sFrom %s %s%s\n",
1358 cpre, (int)qf.qf_pfix_len,
1359 (qf.qf_pfix_len != 0 ? qf.qf_pfix : ""), fakefrom(mp),
1360 fakedate(mp->m_time), csuf);
1361 } else if (doign != allignore && doign != fwdignore &&
1362 action != SEND_RFC822) {
1363 if (qf.qf_pfix_len > 0) {
1364 i = fwrite(qf.qf_pfix, sizeof *qf.qf_pfix, qf.qf_pfix_len, obuf);
1365 if (i != qf.qf_pfix_len)
1366 goto jleave;
1367 sz += i;
1369 #ifdef HAVE_COLOUR
1370 if (cpre != NULL) {
1371 fputs(cpre, obuf);
1372 cpre = (char const*)0x1;
1374 #endif
1376 while (cnt > 0 && (c = getc(ibuf)) != EOF) {
1377 #ifdef HAVE_COLOUR
1378 if (c == '\n' && csuf != NULL) {
1379 cpre = (char const*)0x1;
1380 fputs(csuf, obuf);
1382 #endif
1383 putc(c, obuf);
1384 ++sz;
1385 --cnt;
1386 if (c == '\n')
1387 break;
1390 #ifdef HAVE_COLOUR
1391 if (csuf != NULL && cpre != (char const*)0x1)
1392 fputs(csuf, obuf);
1393 #endif
1394 } else {
1395 while (cnt > 0 && (c = getc(ibuf)) != EOF) {
1396 --cnt;
1397 if (c == '\n')
1398 break;
1402 if (sz > 0 && stats != NULL)
1403 *stats += sz;
1405 mpf = MIME_PARSE_NONE;
1406 if (action != SEND_MBOX && action != SEND_RFC822 && action != SEND_SHOW)
1407 mpf |= MIME_PARSE_DECRYPT | MIME_PARSE_PARTS;
1408 if ((ip = mime_parse_msg(mp, mpf)) == NULL)
1409 goto jleave;
1411 rv = sendpart(mp, ip, obuf, doign, &qf, action, stats, 0);
1412 jleave:
1413 quoteflt_destroy(&qf);
1414 NYD_LEAVE;
1415 return rv;
1418 /* s-it-mode */