Change some "illegal"s to be "invalid" instead
[s-mailx.git] / send.c
blob219c3f38827d9abd3f4fa86efd35b02ab417dffc
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 - 2014 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. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
40 #ifndef HAVE_AMALGAMATION
41 # include "nail.h"
42 #endif
44 enum pipeflags {
45 PIPE_NULL, /* No pipe- mimetype handler */
46 PIPE_COMM, /* Normal command */
47 PIPE_ASYNC, /* Normal command, run asynchronous */
48 PIPE_TEXT, /* @ special command to force treatment as text */
49 PIPE_MSG /* Display message (returned as command string) */
52 enum parseflags {
53 PARSE_DEFAULT = 0,
54 PARSE_DECRYPT = 01,
55 PARSE_PARTS = 02
58 static sigjmp_buf _send_pipejmp;
60 /* */
61 static struct mimepart *parsemsg(struct message *mp, enum parseflags pf);
62 static enum okay parsepart(struct message *zmp, struct mimepart *ip,
63 enum parseflags pf, int level);
64 static void parse822(struct message *zmp, struct mimepart *ip,
65 enum parseflags pf, int level);
66 #ifdef HAVE_SSL
67 static void parsepkcs7(struct message *zmp, struct mimepart *ip,
68 enum parseflags pf, int level);
69 #endif
70 static void _parsemultipart(struct message *zmp,
71 struct mimepart *ip, enum parseflags pf, int level);
72 static void __newpart(struct mimepart *ip, struct mimepart **np,
73 off_t offs, int *part);
74 static void __endpart(struct mimepart **np, off_t xoffs, long lines);
76 /* Going for user display, print Part: info string */
77 static void _print_part_info(struct str *out, struct mimepart *mip,
78 struct ignoretab *doign, int level);
80 /* Query possible pipe command for MIME type */
81 static enum pipeflags _pipecmd(char **result, char const *content_type);
83 /* Create a pipe */
84 static FILE * _pipefile(char const *pipecomm, FILE **qbuf, bool_t quote,
85 bool_t async);
87 /* Adjust output statistics */
88 SINLINE void _addstats(off_t *stats, off_t lines, off_t bytes);
90 /* Call mime_write() as approbiate and adjust statistics */
91 SINLINE ssize_t _out(char const *buf, size_t len, FILE *fp,
92 enum conversion convert, enum sendaction action,
93 struct quoteflt *qf, off_t *stats,
94 struct str *rest);
96 /* SIGPIPE handler */
97 static void _send_onpipe(int signo);
99 /* Send one part */
100 static int sendpart(struct message *zmp, struct mimepart *ip,
101 FILE *obuf, struct ignoretab *doign,
102 struct quoteflt *qf, enum sendaction action,
103 off_t *stats, int level);
105 /* Get a file for an attachment */
106 static FILE * newfile(struct mimepart *ip, int *ispipe);
108 static void pipecpy(FILE *pipebuf, FILE *outbuf, FILE *origobuf,
109 struct quoteflt *qf, off_t *stats);
111 /* Output a reasonable looking status field */
112 static void statusput(const struct message *mp, FILE *obuf,
113 struct quoteflt *qf, off_t *stats);
114 static void xstatusput(const struct message *mp, FILE *obuf,
115 struct quoteflt *qf, off_t *stats);
117 static void put_from_(FILE *fp, struct mimepart *ip, off_t *stats);
119 static struct mimepart *
120 parsemsg(struct message *mp, enum parseflags pf)
122 struct mimepart *ip;
123 NYD_ENTER;
125 ip = csalloc(1, sizeof *ip);
126 ip->m_flag = mp->m_flag;
127 ip->m_have = mp->m_have;
128 ip->m_block = mp->m_block;
129 ip->m_offset = mp->m_offset;
130 ip->m_size = mp->m_size;
131 ip->m_xsize = mp->m_xsize;
132 ip->m_lines = mp->m_lines;
133 ip->m_xlines = mp->m_lines;
134 if (parsepart(mp, ip, pf, 0) != OKAY)
135 ip = NULL;
136 NYD_LEAVE;
137 return ip;
140 static enum okay
141 parsepart(struct message *zmp, struct mimepart *ip, enum parseflags pf,
142 int level)
144 char *cp_b, *cp;
145 enum okay rv = STOP;
146 NYD_ENTER;
148 ip->m_ct_type = hfield1("content-type", (struct message*)ip);
149 if (ip->m_ct_type != NULL) {
150 cp_b = ip->m_ct_type_plain = savestr(ip->m_ct_type);
151 if ((cp = strchr(cp_b, ';')) != NULL)
152 *cp = '\0';
153 cp = cp_b + strlen(cp_b);
154 while (cp > cp_b && blankchar(cp[-1]))
155 --cp;
156 *cp = '\0';
157 } else if (ip->m_parent != NULL &&
158 ip->m_parent->m_mimecontent == MIME_DIGEST)
159 ip->m_ct_type_plain = UNCONST("message/rfc822");
160 else
161 ip->m_ct_type_plain = UNCONST("text/plain");
163 if (ip->m_ct_type != NULL)
164 ip->m_charset = mime_getparam("charset", ip->m_ct_type);
165 if (ip->m_charset == NULL)
166 ip->m_charset = charset_get_7bit();
168 ip->m_ct_transfer_enc = hfield1("content-transfer-encoding",
169 (struct message*)ip);
170 ip->m_mimeenc = (ip->m_ct_transfer_enc != NULL)
171 ? mime_getenc(ip->m_ct_transfer_enc) : MIME_7B;
173 if (((cp = hfield1("content-disposition", (struct message*)ip)) == NULL ||
174 (ip->m_filename = mime_getparam("filename", cp)) == NULL) &&
175 ip->m_ct_type != NULL)
176 ip->m_filename = mime_getparam("name", ip->m_ct_type);
178 ip->m_mimecontent = mime_classify_content_of_part(ip);
180 if (pf & PARSE_PARTS) {
181 if (level > 9999) { /* TODO MAGIC */
182 fprintf(stderr, tr(36, "MIME content too deeply nested\n"));
183 goto jleave;
185 switch (ip->m_mimecontent) {
186 case MIME_PKCS7:
187 if (pf & PARSE_DECRYPT) {
188 #ifdef HAVE_SSL
189 parsepkcs7(zmp, ip, pf, level);
190 break;
191 #else
192 fprintf(stderr, tr(225, "No SSL support compiled in.\n"));
193 goto jleave;
194 #endif
196 /* FALLTHRU */
197 default:
198 break;
199 case MIME_MULTI:
200 case MIME_ALTERNATIVE:
201 case MIME_DIGEST:
202 _parsemultipart(zmp, ip, pf, level);
203 break;
204 case MIME_822:
205 parse822(zmp, ip, pf, level);
206 break;
209 rv = OKAY;
210 jleave:
211 NYD_LEAVE;
212 return rv;
215 static void
216 parse822(struct message *zmp, struct mimepart *ip, enum parseflags pf,
217 int level)
219 int c, lastc = '\n';
220 size_t cnt;
221 FILE *ibuf;
222 off_t offs;
223 struct mimepart *np;
224 long lines;
225 NYD_ENTER;
227 if ((ibuf = setinput(&mb, (struct message*)ip, NEED_BODY)) == NULL)
228 goto jleave;
230 cnt = ip->m_size;
231 lines = ip->m_lines;
232 while (cnt && ((c = getc(ibuf)) != EOF)) {
233 --cnt;
234 if (c == '\n') {
235 --lines;
236 if (lastc == '\n')
237 break;
239 lastc = c;
241 offs = ftell(ibuf);
243 np = csalloc(1, sizeof *np);
244 np->m_flag = MNOFROM;
245 np->m_have = HAVE_HEADER | HAVE_BODY;
246 np->m_block = mailx_blockof(offs);
247 np->m_offset = mailx_offsetof(offs);
248 np->m_size = np->m_xsize = cnt;
249 np->m_lines = np->m_xlines = lines;
250 np->m_partstring = ip->m_partstring;
251 np->m_parent = ip;
252 ip->m_multipart = np;
254 if (ok_blook(rfc822_body_from_)) {
255 substdate((struct message*)np);
256 np->m_from = fakefrom((struct message*)np);/* TODO strip MNOFROM flag? */
259 parsepart(zmp, np, pf, level + 1);
260 jleave:
261 NYD_LEAVE;
264 #ifdef HAVE_SSL
265 static void
266 parsepkcs7(struct message *zmp, struct mimepart *ip, enum parseflags pf,
267 int level)
269 struct message m, *xmp;
270 struct mimepart *np;
271 char *to, *cc;
272 NYD_ENTER;
274 memcpy(&m, ip, sizeof m);
275 to = hfield1("to", zmp);
276 cc = hfield1("cc", zmp);
278 if ((xmp = smime_decrypt(&m, to, cc, 0)) != NULL) {
279 np = csalloc(1, sizeof *np);
280 np->m_flag = xmp->m_flag;
281 np->m_have = xmp->m_have;
282 np->m_block = xmp->m_block;
283 np->m_offset = xmp->m_offset;
284 np->m_size = xmp->m_size;
285 np->m_xsize = xmp->m_xsize;
286 np->m_lines = xmp->m_lines;
287 np->m_xlines = xmp->m_xlines;
288 np->m_partstring = ip->m_partstring;
290 if (parsepart(zmp, np, pf, level + 1) == OKAY) {
291 np->m_parent = ip;
292 ip->m_multipart = np;
295 NYD_LEAVE;
297 #endif
299 static void
300 _parsemultipart(struct message *zmp, struct mimepart *ip, enum parseflags pf,
301 int level)
303 /* TODO Instead of the recursive multiple run parse we have today,
304 * TODO the send/MIME layer rewrite must create a "tree" of parts with
305 * TODO a single-pass parse, then address each part directly as
306 * TODO necessary; since boundaries start with -- and the content
307 * TODO rather forms a stack this is pretty cheap indeed! */
308 struct mimepart *np = NULL;
309 char *boundary, *line = NULL;
310 size_t linesize = 0, linelen, cnt, boundlen;
311 FILE *ibuf;
312 off_t offs;
313 int part = 0;
314 long lines = 0;
315 NYD_ENTER;
317 if ((boundary = mime_get_boundary(ip->m_ct_type, &linelen)) == NULL)
318 goto jleave;
320 boundlen = linelen;
321 if ((ibuf = setinput(&mb, (struct message*)ip, NEED_BODY)) == NULL)
322 goto jleave;
324 cnt = ip->m_size;
325 while (fgetline(&line, &linesize, &cnt, &linelen, ibuf, 0))
326 if (line[0] == '\n')
327 break;
328 offs = ftell(ibuf);
330 __newpart(ip, &np, offs, NULL);
331 while (fgetline(&line, &linesize, &cnt, &linelen, ibuf, 0)) {
332 /* XXX linelen includes LF */
333 if (!((lines > 0 || part == 0) && linelen > boundlen &&
334 !strncmp(line, boundary, boundlen))) {
335 ++lines;
336 continue;
339 /* Subpart boundary? */
340 if (line[boundlen] == '\n') {
341 offs = ftell(ibuf);
342 if (part > 0) {
343 __endpart(&np, offs - boundlen - 2, lines);
344 __newpart(ip, &np, offs - boundlen - 2, NULL);
346 __endpart(&np, offs, 2);
347 __newpart(ip, &np, offs, &part);
348 lines = 0;
349 continue;
352 /* Final boundary? Be aware of cases where there is no separating
353 * newline in between boundaries, as has been seen in a message with
354 * "Content-Type: multipart/appledouble;" */
355 if (linelen < boundlen + 2)
356 continue;
357 linelen -= boundlen + 2;
358 if (line[boundlen] != '-' || line[boundlen + 1] != '-' ||
359 (linelen > 0 && line[boundlen + 2] != '\n'))
360 continue;
361 offs = ftell(ibuf);
362 if (part != 0) {
363 __endpart(&np, offs - boundlen - 4, lines);
364 __newpart(ip, &np, offs - boundlen - 4, NULL);
366 __endpart(&np, offs + cnt, 2);
367 break;
369 if (np) {
370 offs = ftell(ibuf);
371 __endpart(&np, offs, lines);
374 for (np = ip->m_multipart; np != NULL; np = np->m_nextpart)
375 if (np->m_mimecontent != MIME_DISCARD)
376 parsepart(zmp, np, pf, level + 1);
377 free(line);
378 jleave:
379 NYD_LEAVE;
382 static void
383 __newpart(struct mimepart *ip, struct mimepart **np, off_t offs, int *part)
385 struct mimepart *pp;
386 size_t sz;
387 NYD_ENTER;
389 *np = csalloc(1, sizeof **np);
390 (*np)->m_flag = MNOFROM;
391 (*np)->m_have = HAVE_HEADER | HAVE_BODY;
392 (*np)->m_block = mailx_blockof(offs);
393 (*np)->m_offset = mailx_offsetof(offs);
395 if (part) {
396 ++(*part);
397 sz = (ip->m_partstring != NULL) ? strlen(ip->m_partstring) : 0;
398 sz += 20;
399 (*np)->m_partstring = salloc(sz);
400 if (ip->m_partstring)
401 snprintf((*np)->m_partstring, sz, "%s.%u", ip->m_partstring, *part);
402 else
403 snprintf((*np)->m_partstring, sz, "%u", *part);
404 } else
405 (*np)->m_mimecontent = MIME_DISCARD;
406 (*np)->m_parent = ip;
408 if (ip->m_multipart) {
409 for (pp = ip->m_multipart; pp->m_nextpart != NULL; pp = pp->m_nextpart)
411 pp->m_nextpart = *np;
412 } else
413 ip->m_multipart = *np;
414 NYD_LEAVE;
417 static void
418 __endpart(struct mimepart **np, off_t xoffs, long lines)
420 off_t offs;
421 NYD_ENTER;
423 offs = mailx_positionof((*np)->m_block, (*np)->m_offset);
424 (*np)->m_size = (*np)->m_xsize = xoffs - offs;
425 (*np)->m_lines = (*np)->m_xlines = lines;
426 *np = NULL;
427 NYD_LEAVE;
430 static void
431 _print_part_info(struct str *out, struct mimepart *mip,
432 struct ignoretab *doign, int level)
434 struct str ct = {NULL, 0}, cd = {NULL, 0};
435 char const *ps;
436 struct str const *cpre, *csuf;
437 NYD_ENTER;
439 /* Max. 24 */
440 if (is_ign("content-type", 12, doign)) {
441 out->s = mip->m_ct_type_plain;
442 out->l = strlen(out->s);
443 ct.s = ac_alloc(out->l + 2 +1);
444 ct.s[0] = ',';
445 ct.s[1] = ' ';
446 ct.l = 2;
447 if (is_prefix("application/", out->s)) {
448 memcpy(ct.s + 2, "appl../", 7);
449 ct.l += 7;
450 out->l -= 12;
451 out->s += 12;
452 out->l = MIN(out->l, 17);
453 } else
454 out->l = MIN(out->l, 24);
455 memcpy(ct.s + ct.l, out->s, out->l);
456 ct.l += out->l;
457 ct.s[ct.l] = '\0';
460 /* Max. 32 */
461 if (is_ign("content-disposition", 19, doign) && mip->m_filename != NULL) {
462 struct str ti, to;
464 ti.l = strlen(ti.s = mip->m_filename);
465 mime_fromhdr(&ti, &to, TD_ISPR | TD_ICONV | TD_DELCTRL);
467 cd.s = ac_alloc(2 + 32 +1); /* FIXME was 25.. UNI: USE VISUAL WIDTH!!! */
468 cd.s[0] = ',';
469 cd.s[1] = ' ';
470 cd.l = 2 + field_put_bidi_clip(cd.s + 2, 32 +1, to.s, to.l);
472 free(to.s);
475 /* Take care of "99.99", i.e., 5 */
476 if ((ps = mip->m_partstring) == NULL || ps[0] == '\0')
477 ps = "?";
479 #ifdef HAVE_COLOUR
480 cpre = colour_get(COLOURSPEC_PARTINFO);
481 csuf = colour_get(COLOURSPEC_RESET);
482 #else
483 cpre = csuf = NULL;
484 #endif
486 /* Assume maximum possible sizes for 64 bit integers here to avoid any
487 * buffer overflows just in case we have a bug somewhere and / or the
488 * snprintf() is our internal version that doesn't really provide hard
489 * buffer cuts TODO ensure upper bound on numbers, use 9999999 else */
490 #define __msg "%s%s[-- #%s : %lu/%lu%s%s --]%s\n"
491 out->l = sizeof(__msg) +
492 #ifdef HAVE_COLOUR
493 (cpre != NULL ? cpre->l + csuf->l : 0) +
494 #endif
495 strlen(ps) + 2*21 + ct.l + cd.l +1;
496 out->s = salloc(out->l);
497 out->l = snprintf(out->s, out->l, __msg,
498 (level || (ps[0] != '1' && ps[1] == '\0') ? "\n" : ""),
499 (cpre != NULL ? cpre->s : ""),
500 ps, (ul_it)mip->m_lines, (ul_it)mip->m_size,
501 (ct.s != NULL ? ct.s : ""),
502 (cd.s != NULL ? cd.s : ""),
503 (csuf != NULL ? csuf->s : ""));
504 out->s[out->l] = '\0';
505 #undef __msg
507 if (cd.s != NULL)
508 ac_free(cd.s);
509 if (ct.s != NULL)
510 ac_free(ct.s);
511 NYD_LEAVE;
514 static enum pipeflags
515 _pipecmd(char **result, char const *content_type)
517 enum pipeflags ret;
518 char *s, *cp;
519 char const *cq;
520 NYD_ENTER;
522 ret = PIPE_NULL;
523 *result = NULL;
524 if (content_type == NULL)
525 goto jleave;
527 /* First check wether there is a special pipe-MIMETYPE handler */
528 s = ac_alloc(strlen(content_type) + 5 +1);
529 memcpy(s, "pipe-", 5);
530 cp = s + 5;
531 cq = content_type;
533 *cp++ = lowerconv(*cq);
534 while (*cq++ != '\0');
535 cp = vok_vlook(s);
536 ac_free(s);
538 if (cp == NULL)
539 goto jleave;
541 /* User specified a command, inspect for special cases */
542 if (cp[0] != '@') {
543 /* Normal command line */
544 ret = PIPE_COMM;
545 *result = cp;
546 } else if (*++cp == '\0') {
547 /* Treat as plain text */
548 ret = PIPE_TEXT;
549 } else if (!msglist_is_single) {
550 /* Viewing multiple messages in one go, don't block system */
551 ret = PIPE_MSG;
552 *result = UNCONST(tr(86,
553 "[Directly address message only to display this]\n"));
554 } else {
555 /* Viewing a single message only */
556 #if 0 /* TODO send/MIME layer rewrite: when we have a single-pass parser
557 * TODO then the parsing phase and the send phase will be separated;
558 * TODO that allows us to ask a user *before* we start the send, i.e.,
559 * TODO *before* a pager pipe is setup (which is the problem with
560 * TODO the '#if 0' code here) */
561 size_t l = strlen(content_type);
562 char const *x = tr(999, "Should i display a part `%s' (y/n)? ");
563 s = ac_alloc(l += strlen(x) +1);
564 snprintf(s, l - 1, x, content_type);
565 l = getapproval(s), TRU1;
566 puts(""); /* .. we've hijacked a pipe 8-] ... */
567 ac_free(s);
568 if (!l) {
569 x = tr(210, "[User skipped diplay]\n");
570 ret = PIPE_MSG;
571 *result = UNCONST(x);
572 } else
573 #endif
574 if (cp[0] == '&')
575 /* Asynchronous command, normal command line */
576 ret = PIPE_ASYNC, *result = ++cp;
577 else
578 ret = PIPE_COMM, *result = cp;
580 jleave:
581 NYD_LEAVE;
582 return ret;
585 static FILE *
586 _pipefile(char const *pipecomm, FILE **qbuf, bool_t quote, bool_t async)
588 char const *sh;
589 FILE *rbuf;
590 NYD_ENTER;
592 rbuf = *qbuf;
594 if (quote) {
595 if ((*qbuf = Ftmp(NULL, "sendp", OF_RDWR | OF_UNLINK | OF_REGISTER,
596 0600)) == NULL) {
597 perror(tr(173, "tmpfile"));
598 *qbuf = rbuf;
600 async = FAL0;
603 if ((sh = ok_vlook(SHELL)) == NULL)
604 sh = XSHELL;
605 if ((rbuf = Popen(pipecomm, "W", sh, async ? -1 : fileno(*qbuf))) == NULL)
606 perror(pipecomm);
607 else {
608 fflush(*qbuf);
609 if (*qbuf != stdout)
610 fflush(stdout);
612 NYD_LEAVE;
613 return rbuf;
616 SINLINE void
617 _addstats(off_t *stats, off_t lines, off_t bytes)
619 NYD_ENTER;
620 if (stats != NULL) {
621 if (stats[0] >= 0)
622 stats[0] += lines;
623 stats[1] += bytes;
625 NYD_LEAVE;
628 SINLINE ssize_t
629 _out(char const *buf, size_t len, FILE *fp, enum conversion convert, enum
630 sendaction action, struct quoteflt *qf, off_t *stats, struct str *rest)
632 ssize_t sz = 0, n;
633 int flags;
634 char const *cp;
635 NYD_ENTER;
637 #if 0
638 Well ... it turns out to not work like that since of course a valid
639 RFC 4155 compliant parser, like S-nail, takes care for From_ lines only
640 after an empty line has been seen, which cannot be detected that easily
641 right here!
642 ifdef HAVE_DEBUG /* TODO assert legacy */
643 /* TODO if at all, this CAN only happen for SEND_DECRYPT, since all
644 * TODO other input situations handle RFC 4155 OR, if newly generated,
645 * TODO enforce quoted-printable if there is From_, as "required" by
646 * TODO RFC 5751. The SEND_DECRYPT case is not yet overhauled;
647 * TODO if it may happen in this path, we should just treat decryption
648 * TODO as we do for the other input paths; i.e., handle it in SSL!! */
649 if (action == SEND_MBOX || action == SEND_DECRYPT)
650 assert(!is_head(buf, len));
651 #else
652 if ((/*action == SEND_MBOX ||*/ action == SEND_DECRYPT) &&
653 is_head(buf, len)) {
654 putc('>', fp);
655 ++sz;
657 #endif
659 flags = ((int)action & _TD_EOF);
660 action &= ~_TD_EOF;
661 n = mime_write(buf, len, fp,
662 action == SEND_MBOX ? CONV_NONE : convert,
663 flags | ((action == SEND_TODISP || action == SEND_TODISP_ALL ||
664 action == SEND_QUOTE || action == SEND_QUOTE_ALL)
665 ? TD_ISPR | TD_ICONV
666 : (action == SEND_TOSRCH || action == SEND_TOPIPE)
667 ? TD_ICONV : (action == SEND_TOFLTR)
668 ? TD_DELCTRL : (action == SEND_SHOW ? TD_ISPR : TD_NONE)),
669 qf, rest);
670 if (n < 0)
671 sz = n;
672 else if (n > 0) {
673 sz = (ssize_t)((size_t)sz + n);
674 n = 0;
675 if (stats != NULL && stats[0] != -1)
676 for (cp = buf; PTRCMP(cp, <, buf + sz); ++cp)
677 if (*cp == '\n')
678 ++n;
679 _addstats(stats, n, sz);
681 NYD_LEAVE;
682 return sz;
685 static void
686 _send_onpipe(int signo)
688 NYD_X; /* Signal handler */
689 UNUSED(signo);
690 siglongjmp(_send_pipejmp, 1);
693 static int
694 sendpart(struct message *zmp, struct mimepart *ip, FILE * volatile obuf,
695 struct ignoretab *doign, struct quoteflt *qf,
696 enum sendaction volatile action, off_t *volatile stats, int level)
698 int volatile ispipe, rv = 0;
699 struct str rest;
700 char *line = NULL, *cp, *cp2, *start, *pipecomm = NULL;
701 size_t linesize = 0, linelen, cnt;
702 int dostat, infld = 0, ignoring = 1, isenc, c;
703 struct mimepart *volatile np;
704 FILE * volatile ibuf = NULL, * volatile pbuf = obuf, * volatile qbuf = obuf,
705 *origobuf = obuf;
706 enum conversion volatile convert;
707 sighandler_type volatile oldpipe = SIG_DFL;
708 long lineno = 0;
709 NYD_ENTER;
711 if (ip->m_mimecontent == MIME_PKCS7 && ip->m_multipart &&
712 action != SEND_MBOX && action != SEND_RFC822 && action != SEND_SHOW)
713 goto jskip;
715 dostat = 0;
716 if (level == 0) {
717 if (doign != NULL) {
718 if (!is_ign("status", 6, doign))
719 dostat |= 1;
720 if (!is_ign("x-status", 8, doign))
721 dostat |= 2;
722 } else
723 dostat = 3;
725 if ((ibuf = setinput(&mb, (struct message*)ip, NEED_BODY)) == NULL) {
726 rv = -1;
727 goto jleave;
729 cnt = ip->m_size;
731 if (ip->m_mimecontent == MIME_DISCARD)
732 goto jskip;
734 if (!(ip->m_flag & MNOFROM))
735 while (cnt && (c = getc(ibuf)) != EOF) {
736 cnt--;
737 if (c == '\n')
738 break;
740 isenc = 0;
741 convert = (action == SEND_TODISP || action == SEND_TODISP_ALL ||
742 action == SEND_QUOTE || action == SEND_QUOTE_ALL ||
743 action == SEND_TOSRCH || action == SEND_TOFLTR)
744 ? CONV_FROMHDR : CONV_NONE;
746 /* Work the headers */
747 quoteflt_reset(qf, obuf);
748 while (fgetline(&line, &linesize, &cnt, &linelen, ibuf, 0)) {
749 ++lineno;
750 if (line[0] == '\n') {
751 /* If line is blank, we've reached end of headers, so force out
752 * status: field and note that we are no longer in header fields */
753 if (dostat & 1)
754 statusput(zmp, obuf, qf, stats);
755 if (dostat & 2)
756 xstatusput(zmp, obuf, qf, stats);
757 if (doign != allignore)
758 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
759 break;
762 isenc &= ~1;
763 if (infld && blankchar(line[0])) {
764 /* If this line is a continuation (SP / HT) of a previous header
765 * field, determine if the start of the line is a MIME encoded word */
766 if (isenc & 2) {
767 for (cp = line; blankchar(*cp); ++cp);
768 if (cp > line && linelen - PTR2SIZE(cp - line) > 8 &&
769 cp[0] == '=' && cp[1] == '?')
770 isenc |= 1;
772 } else {
773 /* Pick up the header field if we have one */
774 for (cp = line; (c = *cp & 0377) && c != ':' && !spacechar(c); ++cp)
776 cp2 = cp;
777 while (spacechar(*cp))
778 ++cp;
779 if (cp[0] != ':' && level == 0 && lineno == 1) {
780 /* Not a header line, force out status: This happens in uucp style
781 * mail where there are no headers at all */
782 if (dostat & 1)
783 statusput(zmp, obuf, qf, stats);
784 if (dostat & 2)
785 xstatusput(zmp, obuf, qf, stats);
786 if (doign != allignore)
787 _out("\n", 1, obuf, CONV_NONE,SEND_MBOX, qf, stats, NULL);
788 break;
791 /* If it is an ignored field and we care about such things, skip it.
792 * Misuse dostat also for another bit xxx use a bitenum + for more */
793 if (ok_blook(keep_content_length))
794 dostat |= 1 << 2;
795 c = *cp2;
796 *cp2 = 0; /* temporarily null terminate */
797 if ((doign && is_ign(line, PTR2SIZE(cp2 - line), doign)) ||
798 (action == SEND_MBOX && !(dostat & (1 << 2)) &&
799 (!asccasecmp(line, "content-length") ||
800 !asccasecmp(line, "lines"))))
801 ignoring = 1;
802 else if (!asccasecmp(line, "status")) {
803 /* If field is "status," go compute and print real Status: field */
804 if (dostat & 1) {
805 statusput(zmp, obuf, qf, stats);
806 dostat &= ~1;
807 ignoring = 1;
809 } else if (!asccasecmp(line, "x-status")) {
810 /* If field is "status," go compute and print real Status: field */
811 if (dostat & 2) {
812 xstatusput(zmp, obuf, qf, stats);
813 dostat &= ~2;
814 ignoring = 1;
816 } else {
817 ignoring = 0;
818 #ifdef HAVE_COLOUR
819 pipecomm = savestrbuf(line, PTR2SIZE(cp2 - line));
820 #endif
822 *cp2 = c;
823 dostat &= ~(1 << 2);
824 infld = 1;
827 /* Determine if the end of the line is a MIME encoded word */
828 /* TODO geeeh! all this lengthy stuff that follows is about is dealing
829 * TODO with header follow lines, and it should be up to the backend
830 * TODO what happens and what not, i.e., it doesn't matter wether it's
831 * TODO a MIME-encoded word or not, as long as a single separating space
832 * TODO remains in between lines (the MIME stuff will correctly remove
833 * TODO whitespace in between multiple adjacent encoded words) */
834 isenc &= ~2;
835 if (cnt && (c = getc(ibuf)) != EOF) {
836 if (blankchar(c)) {
837 cp = line + linelen - 1;
838 if (linelen > 0 && *cp == '\n')
839 --cp;
840 while (cp >= line && whitechar(*cp))
841 --cp;
842 if (PTR2SIZE(cp - line > 8) && cp[0] == '=' && cp[-1] == '?')
843 isenc |= 2;
845 ungetc(c, ibuf);
848 if (!ignoring) {
849 size_t len = linelen;
850 start = line;
851 if (action == SEND_TODISP || action == SEND_TODISP_ALL ||
852 action == SEND_QUOTE || action == SEND_QUOTE_ALL ||
853 action == SEND_TOSRCH || action == SEND_TOFLTR) {
854 /* Strip blank characters if two MIME-encoded words follow on
855 * continuing lines */
856 if (isenc & 1)
857 while (len > 0 && blankchar(*start)) {
858 ++start;
859 --len;
861 if (isenc & 2)
862 if (len > 0 && start[len - 1] == '\n')
863 --len;
864 while (len > 0 && blankchar(start[len - 1]))
865 --len;
867 #ifdef HAVE_COLOUR
869 bool_t colour_stripped = FAL0;
870 if (pipecomm != NULL) {
871 colour_put_header(obuf, pipecomm);
872 if (len > 0 && start[len - 1] == '\n') {
873 colour_stripped = TRU1;
874 --len;
877 #endif
878 _out(start, len, obuf, convert, action, qf, stats, NULL);
879 #ifdef HAVE_COLOUR
880 if (pipecomm != NULL) {
881 colour_reset(obuf); /* XXX reset after \n!! */
882 if (colour_stripped)
883 fputc('\n', obuf);
886 #endif
887 if (ferror(obuf)) {
888 free(line);
889 rv = -1;
890 goto jleave;
894 quoteflt_flush(qf);
895 free(line);
896 line = NULL;
898 jskip:
899 switch (ip->m_mimecontent) {
900 case MIME_822:
901 switch (action) {
902 case SEND_TOFLTR:
903 putc('\0', obuf);
904 /* FALLTHRU */
905 case SEND_TODISP:
906 case SEND_TODISP_ALL:
907 case SEND_QUOTE:
908 case SEND_QUOTE_ALL:
909 if (ok_blook(rfc822_body_from_)) {
910 if (qf->qf_pfix_len > 0) {
911 size_t i = fwrite(qf->qf_pfix, sizeof *qf->qf_pfix,
912 qf->qf_pfix_len, obuf);
913 if (i == qf->qf_pfix_len)
914 _addstats(stats, 0, i);
916 put_from_(obuf, ip->m_multipart, stats);
918 /* FALLTHRU */
919 case SEND_TOSRCH:
920 case SEND_DECRYPT:
921 goto jmulti;
922 case SEND_TOFILE:
923 case SEND_TOPIPE:
924 if (ok_blook(rfc822_body_from_))
925 put_from_(obuf, ip->m_multipart, stats);
926 /* FALLTHRU */
927 case SEND_MBOX:
928 case SEND_RFC822:
929 case SEND_SHOW:
930 break;
932 break;
933 case MIME_TEXT_HTML:
934 if (action == SEND_TOFLTR)
935 putc('\b', obuf);
936 /* FALLTHRU */
937 case MIME_TEXT:
938 case MIME_TEXT_PLAIN:
939 switch (action) {
940 case SEND_TODISP:
941 case SEND_TODISP_ALL:
942 case SEND_QUOTE:
943 case SEND_QUOTE_ALL:
944 ispipe = TRU1;
945 switch (_pipecmd(&pipecomm, ip->m_ct_type_plain)) {
946 case PIPE_MSG:
947 _out(pipecomm, strlen(pipecomm), obuf, CONV_NONE, SEND_MBOX, qf,
948 stats, NULL);
949 pipecomm = NULL;
950 /* FALLTRHU */
951 case PIPE_TEXT:
952 case PIPE_COMM:
953 case PIPE_ASYNC:
954 case PIPE_NULL:
955 break;
957 /* FALLTRHU */
958 default:
959 break;
961 break;
962 case MIME_DISCARD:
963 if (action != SEND_DECRYPT)
964 goto jleave;
965 break;
966 case MIME_PKCS7:
967 if (action != SEND_MBOX && action != SEND_RFC822 &&
968 action != SEND_SHOW && ip->m_multipart != NULL)
969 goto jmulti;
970 /* FALLTHRU */
971 default:
972 switch (action) {
973 case SEND_TODISP:
974 case SEND_TODISP_ALL:
975 case SEND_QUOTE:
976 case SEND_QUOTE_ALL:
977 ispipe = TRU1;
978 switch (_pipecmd(&pipecomm, ip->m_ct_type_plain)) {
979 case PIPE_MSG:
980 _out(pipecomm, strlen(pipecomm), obuf, CONV_NONE, SEND_MBOX, qf,
981 stats, NULL);
982 pipecomm = NULL;
983 break;
984 case PIPE_ASYNC:
985 ispipe = FAL0;
986 /* FALLTHRU */
987 case PIPE_COMM:
988 case PIPE_NULL:
989 break;
990 case PIPE_TEXT:
991 goto jcopyout; /* break; break; */
993 if (pipecomm != NULL)
994 break;
995 if (level == 0 && cnt) {
996 char const *x = tr(210, "[Binary content]\n");
997 _out(x, strlen(x), obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
999 /* FALLTHRU */
1000 case SEND_TOFLTR:
1001 goto jleave;
1002 case SEND_TOFILE:
1003 case SEND_TOPIPE:
1004 case SEND_TOSRCH:
1005 case SEND_DECRYPT:
1006 case SEND_MBOX:
1007 case SEND_RFC822:
1008 case SEND_SHOW:
1009 break;
1011 break;
1012 case MIME_ALTERNATIVE:
1013 if ((action == SEND_TODISP || action == SEND_QUOTE) &&
1014 !ok_blook(print_alternatives)) {
1015 bool_t doact = FAL0;
1017 for (np = ip->m_multipart; np != NULL; np = np->m_nextpart)
1018 if (np->m_mimecontent == MIME_TEXT_PLAIN)
1019 doact = TRU1;
1020 if (doact) {
1021 for (np = ip->m_multipart; np != NULL; np = np->m_nextpart) {
1022 if (np->m_ct_type_plain != NULL && action != SEND_QUOTE) {
1023 _print_part_info(&rest, np, doign, level);
1024 _out(rest.s, rest.l, obuf, CONV_NONE, SEND_MBOX, qf, stats,
1025 NULL);
1027 if (doact && np->m_mimecontent == MIME_TEXT_PLAIN) {
1028 doact = FAL0;
1029 rv = sendpart(zmp, np, obuf, doign, qf, action, stats,
1030 level + 1);
1031 quoteflt_reset(qf, origobuf);
1032 if (rv < 0)
1033 break;
1036 goto jleave;
1039 /* FALLTHRU */
1040 case MIME_MULTI:
1041 case MIME_DIGEST:
1042 switch (action) {
1043 case SEND_TODISP:
1044 case SEND_TODISP_ALL:
1045 case SEND_QUOTE:
1046 case SEND_QUOTE_ALL:
1047 case SEND_TOFILE:
1048 case SEND_TOPIPE:
1049 case SEND_TOSRCH:
1050 case SEND_TOFLTR:
1051 case SEND_DECRYPT:
1052 jmulti:
1053 if ((action == SEND_TODISP || action == SEND_TODISP_ALL) &&
1054 ip->m_multipart != NULL &&
1055 ip->m_multipart->m_mimecontent == MIME_DISCARD &&
1056 ip->m_multipart->m_nextpart == NULL) {
1057 char const *x = tr(85,
1058 "[Missing multipart boundary - use \"show\" to display "
1059 "the raw message]\n");
1060 _out(x, strlen(x), obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL);
1063 for (np = ip->m_multipart; np != NULL; np = np->m_nextpart) {
1064 if (np->m_mimecontent == MIME_DISCARD && action != SEND_DECRYPT)
1065 continue;
1066 ispipe = FAL0;
1067 switch (action) {
1068 case SEND_TOFILE:
1069 if (np->m_partstring && !strcmp(np->m_partstring, "1"))
1070 break;
1071 stats = NULL;
1072 if ((obuf = newfile(np, UNVOLATILE(&ispipe))) == NULL)
1073 continue;
1074 if (!ispipe)
1075 break;
1076 if (sigsetjmp(_send_pipejmp, 1)) {
1077 rv = -1;
1078 goto jpipe_close;
1080 oldpipe = safe_signal(SIGPIPE, &_send_onpipe);
1081 break;
1082 case SEND_TODISP:
1083 case SEND_TODISP_ALL:
1084 case SEND_QUOTE_ALL:
1085 if (ip->m_mimecontent != MIME_MULTI &&
1086 ip->m_mimecontent != MIME_ALTERNATIVE &&
1087 ip->m_mimecontent != MIME_DIGEST)
1088 break;
1089 _print_part_info(&rest, np, doign, level);
1090 _out(rest.s, rest.l, obuf, CONV_NONE, SEND_MBOX, qf, stats,
1091 NULL);
1092 break;
1093 case SEND_TOFLTR:
1094 putc('\0', obuf);
1095 /* FALLTHRU */
1096 case SEND_MBOX:
1097 case SEND_RFC822:
1098 case SEND_SHOW:
1099 case SEND_TOSRCH:
1100 case SEND_QUOTE:
1101 case SEND_DECRYPT:
1102 case SEND_TOPIPE:
1103 break;
1106 quoteflt_flush(qf);
1107 if (sendpart(zmp, np, obuf, doign, qf, action, stats, level+1) < 0)
1108 rv = -1;
1109 quoteflt_reset(qf, origobuf);
1110 if (action == SEND_QUOTE)
1111 break;
1112 if (action == SEND_TOFILE && obuf != origobuf) {
1113 if (!ispipe)
1114 Fclose(obuf);
1115 else {
1116 jpipe_close:
1117 safe_signal(SIGPIPE, SIG_IGN);
1118 Pclose(obuf, TRU1);
1119 safe_signal(SIGPIPE, oldpipe);
1123 goto jleave;
1124 case SEND_MBOX:
1125 case SEND_RFC822:
1126 case SEND_SHOW:
1127 break;
1131 /* Copy out message body */
1132 jcopyout:
1133 if (doign == allignore && level == 0) /* skip final blank line */
1134 --cnt;
1135 switch (ip->m_mimeenc) {
1136 case MIME_BIN:
1137 if (stats != NULL)
1138 stats[0] = -1;
1139 /* FALLTHRU */
1140 case MIME_7B:
1141 case MIME_8B:
1142 convert = CONV_NONE;
1143 break;
1144 case MIME_QP:
1145 convert = CONV_FROMQP;
1146 break;
1147 case MIME_B64:
1148 switch (ip->m_mimecontent) {
1149 case MIME_TEXT:
1150 case MIME_TEXT_PLAIN:
1151 case MIME_TEXT_HTML:
1152 convert = CONV_FROMB64_T;
1153 break;
1154 default:
1155 convert = CONV_FROMB64;
1157 break;
1158 default:
1159 convert = CONV_NONE;
1162 if (action == SEND_DECRYPT || action == SEND_MBOX ||
1163 action == SEND_RFC822 || action == SEND_SHOW)
1164 convert = CONV_NONE;
1165 #ifdef HAVE_ICONV
1166 if ((action == SEND_TODISP || action == SEND_TODISP_ALL ||
1167 action == SEND_QUOTE || action == SEND_QUOTE_ALL ||
1168 action == SEND_TOSRCH) &&
1169 (ip->m_mimecontent == MIME_TEXT_PLAIN ||
1170 ip->m_mimecontent == MIME_TEXT_HTML ||
1171 ip->m_mimecontent == MIME_TEXT)) {
1172 char const *tcs = charset_get_lc();
1174 if (iconvd != (iconv_t)-1)
1175 n_iconv_close(iconvd);
1176 /* TODO Since Base64 has an odd 4:3 relation in between input
1177 * TODO and output an input line may end with a partial
1178 * TODO multibyte character; this is no problem at all unless
1179 * TODO we send to the display or whatever, i.e., ensure
1180 * TODO makeprint() or something; to avoid this trap, *force*
1181 * TODO iconv(), in which case this layer will handle leftovers
1182 * TODO correctly */
1183 if (convert == CONV_FROMB64_T || (asccasecmp(tcs, ip->m_charset) &&
1184 asccasecmp(charset_get_7bit(), ip->m_charset))) {
1185 iconvd = n_iconv_open(tcs, ip->m_charset);
1186 /* XXX Don't bail out if we cannot iconv(3) here;
1187 * XXX alternatively we could avoid trying to open
1188 * XXX if ip->m_charset is "unknown-8bit", which was
1189 * XXX the one that has bitten me?? */
1191 * TODO errors should DEFINETELY not be scrolled away!
1192 * TODO what about an error buffer (think old shsp(1)),
1193 * TODO re-dump errors since last snapshot when the
1194 * TODO command loop enters again? i.e., at least print
1195 * TODO "There were errors ?" before the next prompt,
1196 * TODO so that the user can look at the error buffer?
1198 if (iconvd == (iconv_t)-1 && errno == EINVAL) {
1199 fprintf(stderr, tr(179, "Cannot convert from %s to %s\n"),
1200 ip->m_charset, tcs);
1201 /*rv = 1; goto jleave;*/
1205 #endif
1207 if (pipecomm != NULL && (action == SEND_TODISP ||
1208 action == SEND_TODISP_ALL || action == SEND_QUOTE ||
1209 action == SEND_QUOTE_ALL)) {
1210 qbuf = obuf;
1211 pbuf = _pipefile(pipecomm, UNVOLATILE(&qbuf),
1212 (action == SEND_QUOTE || action == SEND_QUOTE_ALL), !ispipe);
1213 action = SEND_TOPIPE;
1214 if (pbuf != qbuf) {
1215 oldpipe = safe_signal(SIGPIPE, &_send_onpipe);
1216 if (sigsetjmp(_send_pipejmp, 1))
1217 goto jend;
1219 } else
1220 pbuf = qbuf = obuf;
1223 bool_t eof;
1224 ui32_t save_qf_pfix_len = qf->qf_pfix_len;
1225 off_t *save_stats = stats;
1227 if (pbuf != origobuf) {
1228 qf->qf_pfix_len = 0; /* XXX legacy (remove filter instead) */
1229 stats = NULL;
1231 eof = FAL0;
1232 rest.s = NULL;
1233 rest.l = 0;
1235 quoteflt_reset(qf, pbuf);
1236 while (!eof && fgetline(&line, &linesize, &cnt, &linelen, ibuf, 0)) {
1237 joutln:
1238 if (_out(line, linelen, pbuf, convert, action, qf, stats, &rest) < 0 ||
1239 ferror(pbuf)) {
1240 rv = -1; /* XXX Should bail away?! */
1241 break;
1244 if (!eof && rest.l != 0) {
1245 linelen = 0;
1246 eof = TRU1;
1247 action |= _TD_EOF;
1248 goto joutln;
1250 quoteflt_flush(qf);
1251 if (rest.s != NULL)
1252 free(rest.s);
1254 if (pbuf != origobuf) {
1255 qf->qf_pfix_len = save_qf_pfix_len;
1256 stats = save_stats;
1260 jend:
1261 free(line);
1262 if (pbuf != qbuf) {
1263 safe_signal(SIGPIPE, SIG_IGN);
1264 Pclose(pbuf, ispipe);
1265 safe_signal(SIGPIPE, oldpipe);
1266 if (qbuf != obuf)
1267 pipecpy(qbuf, obuf, origobuf, qf, stats);
1269 #ifdef HAVE_ICONV
1270 if (iconvd != (iconv_t)-1)
1271 n_iconv_close(iconvd);
1272 #endif
1273 jleave:
1274 NYD_LEAVE;
1275 return rv;
1278 static FILE *
1279 newfile(struct mimepart *ip, int *ispipe)
1281 struct str in, out;
1282 char *f;
1283 FILE *fp;
1284 NYD_ENTER;
1286 f = ip->m_filename;
1287 *ispipe = 0;
1289 if (f != NULL && f != (char*)-1) {
1290 in.s = f;
1291 in.l = strlen(f);
1292 mime_fromhdr(&in, &out, TD_ISPR);
1293 memcpy(f, out.s, out.l);
1294 *(f + out.l) = '\0';
1295 free(out.s);
1298 if (options & OPT_INTERACTIVE) {
1299 char *f2, *f3;
1300 jgetname:
1301 printf(tr(278, "Enter filename for part %s (%s)"),
1302 (ip->m_partstring != NULL) ? ip->m_partstring : "?",
1303 ip->m_ct_type_plain);
1304 f2 = readstr_input(": ", (f != (char*)-1) ? f : NULL);
1305 if (f2 == NULL || *f2 == '\0') {
1306 fprintf(stderr, tr(279, "... skipping this\n"));
1307 fp = NULL;
1308 goto jleave;
1309 } else if (*f2 == '|')
1310 /* Pipes are expanded by the shell */
1311 f = f2;
1312 else if ((f3 = file_expand(f2)) == NULL)
1313 /* (Error message written by file_expand()) */
1314 goto jgetname;
1315 else
1316 f = f3;
1318 if (f == NULL || f == (char*)-1) {
1319 fp = NULL;
1320 goto jleave;
1323 if (*f == '|') {
1324 char const *cp;
1325 cp = ok_vlook(SHELL);
1326 if (cp == NULL)
1327 cp = XSHELL;
1328 fp = Popen(f + 1, "w", cp, 1);
1329 if (!(*ispipe = (fp != NULL)))
1330 perror(f);
1331 } else {
1332 if ((fp = Fopen(f, "w")) == NULL)
1333 fprintf(stderr, tr(176, "Cannot open %s\n"), f);
1335 jleave:
1336 NYD_LEAVE;
1337 return fp;
1340 static void
1341 pipecpy(FILE *pipebuf, FILE *outbuf, FILE *origobuf, struct quoteflt *qf,
1342 off_t *stats)
1344 char *line = NULL; /* TODO line pool */
1345 size_t linesize = 0, linelen, cnt;
1346 ssize_t all_sz, sz;
1347 NYD_ENTER;
1349 fflush(pipebuf);
1350 rewind(pipebuf);
1351 cnt = fsize(pipebuf);
1352 all_sz = 0;
1354 quoteflt_reset(qf, outbuf);
1355 while (fgetline(&line, &linesize, &cnt, &linelen, pipebuf, 0) != NULL) {
1356 if ((sz = quoteflt_push(qf, line, linelen)) < 0)
1357 break;
1358 all_sz += sz;
1360 if ((sz = quoteflt_flush(qf)) > 0)
1361 all_sz += sz;
1362 if (line)
1363 free(line);
1365 if (all_sz > 0 && outbuf == origobuf)
1366 _addstats(stats, 1, all_sz);
1367 fclose(pipebuf);
1368 NYD_LEAVE;
1371 static void
1372 statusput(const struct message *mp, FILE *obuf, struct quoteflt *qf,
1373 off_t *stats)
1375 char statout[3], *cp = statout;
1376 NYD_ENTER;
1378 if (mp->m_flag & MREAD)
1379 *cp++ = 'R';
1380 if (!(mp->m_flag & MNEW))
1381 *cp++ = 'O';
1382 *cp = 0;
1383 if (statout[0]) {
1384 int i = fprintf(obuf, "%.*sStatus: %s\n", (int)qf->qf_pfix_len,
1385 (qf->qf_pfix_len > 0 ? qf->qf_pfix : 0), statout);
1386 if (i > 0)
1387 _addstats(stats, 1, i);
1389 NYD_LEAVE;
1392 static void
1393 xstatusput(const struct message *mp, FILE *obuf, struct quoteflt *qf,
1394 off_t *stats)
1396 char xstatout[4];
1397 char *xp = xstatout;
1398 NYD_ENTER;
1400 if (mp->m_flag & MFLAGGED)
1401 *xp++ = 'F';
1402 if (mp->m_flag & MANSWERED)
1403 *xp++ = 'A';
1404 if (mp->m_flag & MDRAFTED)
1405 *xp++ = 'T';
1406 *xp = 0;
1407 if (xstatout[0]) {
1408 int i = fprintf(obuf, "%.*sX-Status: %s\n", (int)qf->qf_pfix_len,
1409 (qf->qf_pfix_len > 0 ? qf->qf_pfix : 0), xstatout);
1410 if (i > 0)
1411 _addstats(stats, 1, i);
1413 NYD_LEAVE;
1416 static void
1417 put_from_(FILE *fp, struct mimepart *ip, off_t *stats)
1419 char const *froma, *date, *nl;
1420 int i;
1421 NYD_ENTER;
1423 if (ip != NULL && ip->m_from != NULL) {
1424 froma = ip->m_from;
1425 date = fakedate(ip->m_time);
1426 nl = "\n";
1427 } else {
1428 froma = myname;
1429 date = time_current.tc_ctime;
1430 nl = "";
1433 colour_put(fp, COLOURSPEC_FROM_);
1434 i = fprintf(fp, "From %s %s%s", froma, date, nl);
1435 colour_reset(fp);
1436 if (i > 0)
1437 _addstats(stats, (*nl != '\0'), i);
1438 NYD_LEAVE;
1441 FL int
1442 sendmp(struct message *mp, FILE *obuf, struct ignoretab *doign,
1443 char const *prefix, enum sendaction action, off_t *stats)
1445 struct quoteflt qf;
1446 size_t cnt, sz, i;
1447 FILE *ibuf;
1448 enum parseflags pf;
1449 struct mimepart *ip;
1450 int rv = -1, c;
1451 NYD_ENTER;
1453 if (mp == dot && action != SEND_TOSRCH && action != SEND_TOFLTR)
1454 did_print_dot = 1;
1455 if (stats != NULL)
1456 stats[0] = stats[1] = 0;
1457 quoteflt_init(&qf, prefix);
1459 /* First line is the From_ line, so no headers there to worry about */
1460 if ((ibuf = setinput(&mb, mp, NEED_BODY)) == NULL)
1461 goto jleave;
1463 cnt = mp->m_size;
1464 sz = 0;
1466 struct str const *cpre, *csuf;
1467 #ifdef HAVE_COLOUR
1468 cpre = colour_get(COLOURSPEC_FROM_);
1469 csuf = colour_get(COLOURSPEC_RESET);
1470 #else
1471 cpre = csuf = NULL;
1472 #endif
1473 if (mp->m_flag & MNOFROM) {
1474 if (doign != allignore && doign != fwdignore && action != SEND_RFC822)
1475 sz = fprintf(obuf, "%s%.*sFrom %s %s%s\n",
1476 (cpre != NULL ? cpre->s : ""),
1477 (int)qf.qf_pfix_len, (qf.qf_pfix_len != 0 ? qf.qf_pfix : ""),
1478 fakefrom(mp), fakedate(mp->m_time),
1479 (csuf != NULL ? csuf->s : ""));
1480 } else {
1481 if (doign != allignore && doign != fwdignore && action != SEND_RFC822) {
1482 if (qf.qf_pfix_len > 0) {
1483 i = fwrite(qf.qf_pfix, sizeof *qf.qf_pfix, qf.qf_pfix_len, obuf);
1484 if (i != qf.qf_pfix_len)
1485 goto jleave;
1486 sz += i;
1488 #ifdef HAVE_COLOUR
1489 if (cpre != NULL) {
1490 fputs(cpre->s, obuf);
1491 cpre = (struct str const*)0x1;
1493 #endif
1496 while (cnt > 0 && (c = getc(ibuf)) != EOF) {
1497 if (doign != allignore && doign != fwdignore &&
1498 action != SEND_RFC822) {
1499 #ifdef HAVE_COLOUR
1500 if (c == '\n' && csuf != NULL) {
1501 cpre = (struct str const*)0x1;
1502 fputs(csuf->s, obuf);
1504 #endif
1505 putc(c, obuf);
1506 sz++;
1508 --cnt;
1509 if (c == '\n')
1510 break;
1513 #ifdef HAVE_COLOUR
1514 if (csuf != NULL && cpre != (struct str const*)0x1)
1515 fputs(csuf->s, obuf);
1516 #endif
1519 if (sz > 0)
1520 _addstats(stats, 1, sz);
1522 pf = 0;
1523 if (action != SEND_MBOX && action != SEND_RFC822 && action != SEND_SHOW)
1524 pf |= PARSE_DECRYPT | PARSE_PARTS;
1525 if ((ip = parsemsg(mp, pf)) == NULL)
1526 goto jleave;
1528 rv = sendpart(mp, ip, obuf, doign, &qf, action, stats, 0);
1529 jleave:
1530 quoteflt_destroy(&qf);
1531 NYD_LEAVE;
1532 return rv;
1535 /* vim:set fenc=utf-8:s-it-mode */