head.c:a_head_idna_apply(): FIX IDNA result length calculation!
[s-mailx.git] / send.c
blobbff2daf4ec97dedbe3ba358b04eb0ade57e6191f
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Message content preparation (sendmp()).
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
6 */
7 /*
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
35 #undef n_FILE
36 #define n_FILE send
38 #ifndef HAVE_AMALGAMATION
39 # include "nail.h"
40 #endif
42 static sigjmp_buf _send_pipejmp;
44 /* Going for user display, print Part: info string */
45 static void _print_part_info(FILE *obuf, struct mimepart const *mpp,
46 struct n_ignore const *doitp, int level,
47 struct quoteflt *qf, ui64_t *stats);
49 /* Create a pipe; if mpp is not NULL, place some 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 *outrest,
59 struct str *inrest);
61 /* SIGPIPE handler */
62 static void _send_onpipe(int signo);
64 /* Send one part */
65 static int sendpart(struct message *zmp, struct mimepart *ip,
66 FILE *obuf, struct n_ignore const *doitp,
67 struct quoteflt *qf, enum sendaction action,
68 ui64_t *stats, int level);
70 /* Get a file for an attachment */
71 static FILE * newfile(struct mimepart *ip, bool_t volatile *ispipe);
73 static void pipecpy(FILE *pipebuf, FILE *outbuf, FILE *origobuf,
74 struct quoteflt *qf, ui64_t *stats);
76 /* Output a reasonable looking status field */
77 static void statusput(const struct message *mp, FILE *obuf,
78 struct quoteflt *qf, ui64_t *stats);
79 static void xstatusput(const struct message *mp, FILE *obuf,
80 struct quoteflt *qf, ui64_t *stats);
82 static void put_from_(FILE *fp, struct mimepart *ip, ui64_t *stats);
84 static void
85 _print_part_info(FILE *obuf, struct mimepart const *mpp, /* TODO strtofmt.. */
86 struct n_ignore const *doitp, int level, struct quoteflt *qf, ui64_t *stats)
88 char buf[64];
89 struct str ti, to;
90 struct str const *cpre, *csuf;
91 char const *cp;
92 NYD2_ENTER;
94 #ifdef HAVE_COLOUR
96 struct n_colour_pen *cpen = n_colour_pen_create(n_COLOUR_ID_VIEW_PARTINFO,
97 NULL);
98 if ((cpre = n_colour_pen_to_str(cpen)) != NULL)
99 csuf = n_colour_reset_to_str();
100 else
101 csuf = NULL;
103 #else
104 cpre = csuf = NULL;
105 #endif
107 /* Take care of "99.99", i.e., 5 */
108 if ((cp = mpp->m_partstring) == NULL || cp[0] == '\0')
109 cp = "?";
110 if (level || (cp[0] != '1' && cp[1] == '\0') || (cp[0] == '1' && /* TODO */
111 cp[1] == '.' && cp[2] != '1')) /* TODO code should not look like so */
112 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
114 /* Part id, content-type, encoding, charset */
115 if (cpre != NULL)
116 _out(cpre->s, cpre->l, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
117 _out("[-- #", 5, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
118 _out(cp, strlen(cp), obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
120 to.l = snprintf(buf, sizeof buf, " %" PRIuZ "/%" PRIuZ " ",
121 (uiz_t)mpp->m_lines, (uiz_t)mpp->m_size);
122 _out(buf, to.l, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
124 if ((cp = mpp->m_ct_type_usr_ovwr) != NULL)
125 _out("+", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
126 else
127 cp = mpp->m_ct_type_plain;
128 if ((to.l = strlen(cp)) > 30 && is_asccaseprefix("application/", cp)) {
129 size_t const al = sizeof("appl../") -1, fl = sizeof("application/") -1;
130 size_t i = to.l - fl;
131 char *x = salloc(al + i +1);
133 memcpy(x, "appl../", al);
134 memcpy(x + al, cp + fl, i +1);
135 cp = x;
136 to.l = al + i;
138 _out(cp, to.l, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
140 if (mpp->m_multipart == NULL/* TODO */ && (cp = mpp->m_ct_enc) != NULL) {
141 _out(", ", 2, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
142 if (to.l > 25 && !asccasecmp(cp, "quoted-printable"))
143 cp = "qu.-pr.";
144 _out(cp, strlen(cp), obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
147 if (mpp->m_multipart == NULL/* TODO */ && (cp = mpp->m_charset) != NULL) {
148 _out(", ", 2, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
149 _out(cp, strlen(cp), obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
152 _out(" --]", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
153 if (csuf != NULL)
154 _out(csuf->s, csuf->l, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
155 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
157 /* */
158 if (mpp->m_content_info & CI_MIME_ERRORS) {
159 if (cpre != NULL)
160 _out(cpre->s, cpre->l, obuf, CONV_NONE, SEND_MBOX, qf, stats,
161 NULL, NULL);
162 _out("[-- ", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
164 ti.l = strlen(ti.s = n_UNCONST(_("Defective MIME structure")));
165 makeprint(&ti, &to);
166 to.l = delctrl(to.s, to.l);
167 _out(to.s, to.l, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
168 free(to.s);
170 _out(" --]", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
171 if (csuf != NULL)
172 _out(csuf->s, csuf->l, obuf, CONV_NONE, SEND_MBOX, qf, stats,
173 NULL, NULL);
174 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
177 /* Content-Description */
178 if (n_ignore_is_ign(doitp, "content-description", 19) &&
179 (cp = mpp->m_content_description) != NULL && *cp != '\0') {
180 if (cpre != NULL)
181 _out(cpre->s, cpre->l, obuf, CONV_NONE, SEND_MBOX, qf, stats,
182 NULL, NULL);
183 _out("[-- ", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
185 ti.l = strlen(ti.s = n_UNCONST(mpp->m_content_description));
186 mime_fromhdr(&ti, &to, TD_ISPR | TD_ICONV);
187 _out(to.s, to.l, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
188 free(to.s);
190 _out(" --]", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
191 if (csuf != NULL)
192 _out(csuf->s, csuf->l, obuf, CONV_NONE, SEND_MBOX, qf, stats,
193 NULL, NULL);
194 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
197 /* Filename */
198 if (n_ignore_is_ign(doitp, "content-disposition", 19) &&
199 mpp->m_filename != NULL && *mpp->m_filename != '\0') {
200 if (cpre != NULL)
201 _out(cpre->s, cpre->l, obuf, CONV_NONE, SEND_MBOX, qf, stats,
202 NULL, NULL);
203 _out("[-- ", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
205 ti.l = strlen(ti.s = mpp->m_filename);
206 makeprint(&ti, &to);
207 to.l = delctrl(to.s, to.l);
208 _out(to.s, to.l, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
209 free(to.s);
211 _out(" --]", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
212 if (csuf != NULL)
213 _out(csuf->s, csuf->l, obuf, CONV_NONE, SEND_MBOX, qf, stats,
214 NULL, NULL);
215 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
217 NYD2_LEAVE;
220 static FILE *
221 _pipefile(struct mime_handler *mhp, struct mimepart const *mpp, FILE **qbuf,
222 char const *tmpname, int term_infd)
224 struct str s;
225 char const *env_addon[8], *cp, *sh;
226 FILE *rbuf;
227 NYD_ENTER;
229 rbuf = *qbuf;
231 if (mhp->mh_flags & MIME_HDL_ISQUOTE) {
232 if ((*qbuf = Ftmp(NULL, "sendp", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
233 NULL) {
234 n_perr(_("tmpfile"), 0);
235 *qbuf = rbuf;
239 if ((mhp->mh_flags & MIME_HDL_TYPE_MASK) == MIME_HDL_PTF) {
240 union {int (*ptf)(void); char const *sh;} u;
242 fflush(*qbuf);
243 if (*qbuf != stdout) /* xxx never? v15: it'll be a filter anyway */
244 fflush(stdout);
246 u.ptf = mhp->mh_ptf;
247 if((rbuf = Popen((char*)-1, "W", u.sh, NULL, fileno(*qbuf))) == NULL)
248 goto jerror;
249 goto jleave;
252 /* NAIL_FILENAME */
253 if (mpp == NULL || (cp = mpp->m_filename) == NULL)
254 cp = n_empty;
255 env_addon[0] = str_concat_csvl(&s, NAILENV_FILENAME, "=", cp, NULL)->s;
257 /* NAIL_FILENAME_GENERATED *//* TODO pathconf NAME_MAX; but user can create
258 * TODO a file wherever he wants! *Do* create a zero-size temporary file
259 * TODO and give *that* path as NAIL_FILENAME_TEMPORARY, clean it up once
260 * TODO the pipe returns? Like this we *can* verify path/name issues! */
261 env_addon[1] = str_concat_csvl(&s, NAILENV_FILENAME_GENERATED, "=",
262 getrandstring(n_MIN(NAME_MAX / 4, 16)), NULL)->s;
264 /* NAIL_CONTENT{,_EVIDENCE} */
265 if (mpp == NULL || (cp = mpp->m_ct_type_plain) == NULL)
266 cp = n_empty;
267 env_addon[2] = str_concat_csvl(&s, NAILENV_CONTENT, "=", cp, NULL)->s;
269 if (mpp != NULL && mpp->m_ct_type_usr_ovwr != NULL)
270 cp = mpp->m_ct_type_usr_ovwr;
271 env_addon[3] = str_concat_csvl(&s, NAILENV_CONTENT_EVIDENCE, "=", cp,
272 NULL)->s;
274 env_addon[4] = str_concat_csvl(&s, NAILENV_TMPDIR, /* TODO v15*/
275 "=", ok_vlook(TMPDIR), NULL)->s;
277 env_addon[5] = NULL;
279 /* NAIL_FILENAME_TEMPORARY? */
280 if (tmpname != NULL) {
281 env_addon[5] = str_concat_csvl(&s, NAILENV_FILENAME_TEMPORARY, "=",
282 tmpname, NULL)->s;
283 env_addon[6] = NULL;
286 sh = ok_vlook(SHELL);
288 if (mhp->mh_flags & MIME_HDL_NEEDSTERM) {
289 sigset_t nset;
290 int pid;
292 sigemptyset(&nset);
293 pid = run_command(sh, &nset, term_infd, COMMAND_FD_PASS, "-c",
294 mhp->mh_shell_cmd, NULL, env_addon);
295 rbuf = (pid < 0) ? NULL : (FILE*)-1;
296 } else {
297 rbuf = Popen(mhp->mh_shell_cmd, "W", sh, env_addon,
298 (mhp->mh_flags & MIME_HDL_ASYNC ? -1 : fileno(*qbuf)));
299 jerror:
300 if (rbuf == NULL)
301 n_err(_("Cannot run MIME type handler: %s: %s\n"),
302 mhp->mh_msg, strerror(errno));
303 else {
304 fflush(*qbuf);
305 if (*qbuf != stdout)
306 fflush(stdout);
309 jleave:
310 NYD_LEAVE;
311 return rbuf;
314 SINLINE ssize_t
315 _out(char const *buf, size_t len, FILE *fp, enum conversion convert, enum
316 sendaction action, struct quoteflt *qf, ui64_t *stats, struct str *outrest,
317 struct str *inrest)
319 ssize_t sz = 0, n;
320 int flags;
321 NYD_ENTER;
323 #if 0
324 Well ... it turns out to not work like that since of course a valid
325 RFC 4155 compliant parser, like S-nail, takes care for From_ lines only
326 after an empty line has been seen, which cannot be detected that easily
327 right here!
328 ifdef HAVE_DEBUG /* TODO assert legacy */
329 /* TODO if at all, this CAN only happen for SEND_DECRYPT, since all
330 * TODO other input situations handle RFC 4155 OR, if newly generated,
331 * TODO enforce quoted-printable if there is From_, as "required" by
332 * TODO RFC 5751. The SEND_DECRYPT case is not yet overhauled;
333 * TODO if it may happen in this path, we should just treat decryption
334 * TODO as we do for the other input paths; i.e., handle it in SSL!! */
335 if (action == SEND_MBOX || action == SEND_DECRYPT)
336 assert(!is_head(buf, len, FAL0));
337 #else
338 if ((/*action == SEND_MBOX ||*/ action == SEND_DECRYPT) &&
339 is_head(buf, len, FAL0)) {
340 putc('>', fp);
341 ++sz;
343 #endif
345 flags = ((int)action & _TD_EOF);
346 action &= ~_TD_EOF;
347 n = mime_write(buf, len, fp,
348 action == SEND_MBOX ? CONV_NONE : convert,
349 flags | ((action == SEND_TODISP || action == SEND_TODISP_ALL ||
350 action == SEND_QUOTE || action == SEND_QUOTE_ALL)
351 ? TD_ISPR | TD_ICONV
352 : (action == SEND_TOSRCH || action == SEND_TOPIPE)
353 ? TD_ICONV : (action == SEND_SHOW ? TD_ISPR : TD_NONE)),
354 qf, outrest, inrest);
355 if (n < 0)
356 sz = n;
357 else if (n > 0) {
358 sz += n;
359 if (stats != NULL)
360 *stats += sz;
362 NYD_LEAVE;
363 return sz;
366 static void
367 _send_onpipe(int signo)
369 NYD_X; /* Signal handler */
370 n_UNUSED(signo);
371 siglongjmp(_send_pipejmp, 1);
374 static sigjmp_buf __sendp_actjmp; /* TODO someday.. */
375 static int __sendp_sig; /* TODO someday.. */
376 static sighandler_type __sendp_opipe;
377 static void
378 __sendp_onsig(int sig) /* TODO someday, we won't need it no more */
380 NYD_X; /* Signal handler */
381 __sendp_sig = sig;
382 siglongjmp(__sendp_actjmp, 1);
385 static int
386 sendpart(struct message *zmp, struct mimepart *ip, FILE * volatile obuf,
387 struct n_ignore const *doitp, struct quoteflt *qf,
388 enum sendaction volatile action, ui64_t * volatile stats, int level)
390 int volatile rv = 0;
391 struct mime_handler mh;
392 struct str outrest, inrest;
393 char *line = NULL, *cp, *cp2, *start;
394 char const * volatile tmpname = NULL;
395 size_t linesize = 0, linelen, cnt;
396 int volatile term_infd;
397 int dostat, c;
398 struct mimepart *volatile np;
399 FILE * volatile ibuf = NULL, * volatile pbuf = obuf,
400 * volatile qbuf = obuf, *origobuf = obuf;
401 enum conversion volatile convert;
402 sighandler_type volatile oldpipe = SIG_DFL;
403 NYD_ENTER;
405 n_UNINIT(term_infd, 0);
406 n_UNINIT(cnt, 0);
408 quoteflt_reset(qf, obuf);
410 if (ip->m_mimecontent == MIME_PKCS7) {
411 if (ip->m_multipart &&
412 action != SEND_MBOX && action != SEND_RFC822 && action != SEND_SHOW)
413 goto jheaders_skip;
416 dostat = 0;
417 if (level == 0) {
418 if (doitp != NULL) {
419 if (!n_ignore_is_ign(doitp, "status", 6))
420 dostat |= 1;
421 if (!n_ignore_is_ign(doitp, "x-status", 8))
422 dostat |= 2;
423 } else
424 dostat = 3;
426 if ((ibuf = setinput(&mb, (struct message*)ip, NEED_BODY)) == NULL) {
427 rv = -1;
428 goto jleave;
430 cnt = ip->m_size;
432 if (ip->m_mimecontent == MIME_DISCARD)
433 goto jheaders_skip;
435 if (!(ip->m_flag & MNOFROM))
436 while (cnt && (c = getc(ibuf)) != EOF) {
437 cnt--;
438 if (c == '\n')
439 break;
441 convert = (action == SEND_TODISP || action == SEND_TODISP_ALL ||
442 action == SEND_QUOTE || action == SEND_QUOTE_ALL ||
443 action == SEND_TOSRCH)
444 ? CONV_FROMHDR : CONV_NONE;
446 /* Work the headers */
447 /* C99 */{
448 enum {
449 HPS_NONE = 0,
450 HPS_IN_FIELD = 1<<0,
451 HPS_IGNORE = 1<<1,
452 HPS_ISENC_1 = 1<<2,
453 HPS_ISENC_2 = 1<<3
454 } hps = HPS_NONE;
455 size_t lineno = 0;
457 while (fgetline(&line, &linesize, &cnt, &linelen, ibuf, 0)) {
458 ++lineno;
459 if (line[0] == '\n') {
460 /* If line is blank, we've reached end of headers, so force out
461 * status: field and note that we are no longer in header fields */
462 if (dostat & 1)
463 statusput(zmp, obuf, qf, stats);
464 if (dostat & 2)
465 xstatusput(zmp, obuf, qf, stats);
466 if (doitp != n_IGNORE_ALL)
467 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
468 break;
471 hps &= ~HPS_ISENC_1;
472 if ((hps & HPS_IN_FIELD) && blankchar(line[0])) {
473 /* If this line is a continuation (SP / HT) of a previous header
474 * field, determine if the start of the line is a MIME encoded word */
475 if (hps & HPS_ISENC_2) {
476 for (cp = line; blankchar(*cp); ++cp)
478 if (cp > line && linelen - PTR2SIZE(cp - line) > 8 &&
479 cp[0] == '=' && cp[1] == '?')
480 hps |= HPS_ISENC_1;
482 } else {
483 /* Pick up the header field if we have one */
484 for (cp = line; (c = *cp & 0377) && c != ':' && !spacechar(c); ++cp)
486 cp2 = cp;
487 while (spacechar(*cp))
488 ++cp;
489 if (cp[0] != ':') {
490 if (lineno != 1)
491 n_err(_("Malformed message: headers and body not separated "
492 "(with empty line)\n"));
493 /* Not a header line, force out status: This happens in uucp style
494 * mail where there are no headers at all */
495 if (level == 0 /*&& lineno == 1*/) {
496 if (dostat & 1)
497 statusput(zmp, obuf, qf, stats);
498 if (dostat & 2)
499 xstatusput(zmp, obuf, qf, stats);
501 if (doitp != n_IGNORE_ALL)
502 _out("\n", 1, obuf, CONV_NONE,SEND_MBOX, qf, stats, NULL,NULL);
503 break;
506 /* If it is an ignored field and we care about such things, skip it.
507 * Misuse dostat also for another bit xxx use a bitenum + for more */
508 if (ok_blook(keep_content_length))
509 dostat |= 1 << 2;
510 c = *cp2;
511 *cp2 = 0; /* temporarily null terminate */
512 if ((doitp != NULL &&
513 n_ignore_is_ign(doitp, line, PTR2SIZE(cp2 - line))) ||
514 (action == SEND_MBOX && !(dostat & (1 << 2)) &&
515 (!asccasecmp(line, "content-length") ||
516 !asccasecmp(line, "lines"))))
517 hps |= HPS_IGNORE;
518 else if (!asccasecmp(line, "status")) {
519 /* If field is "status," go compute and print real Status: field */
520 if (dostat & 1) {
521 statusput(zmp, obuf, qf, stats);
522 dostat &= ~1;
523 hps |= HPS_IGNORE;
525 } else if (!asccasecmp(line, "x-status")) {
526 /* If field is "status," go compute and print real Status: field */
527 if (dostat & 2) {
528 xstatusput(zmp, obuf, qf, stats);
529 dostat &= ~2;
530 hps |= HPS_IGNORE;
532 } else {
533 hps &= ~HPS_IGNORE;
534 /* For colourization we need the complete line, so save it */
535 /* XXX This is all temporary (colour belongs into backend), so
536 * XXX use tmpname as a temporary storage in the meanwhile */
537 #ifdef HAVE_COLOUR
538 if (pstate & PS_COLOUR_ACTIVE)
539 tmpname = savestrbuf(line, PTR2SIZE(cp2 - line));
540 #endif
542 *cp2 = c;
543 dostat &= ~(1 << 2);
544 hps |= HPS_IN_FIELD;
547 /* Determine if the end of the line is a MIME encoded word */
548 /* TODO geeeh! all this lengthy stuff that follows is about is dealing
549 * TODO with header follow lines, and it should be up to the backend
550 * TODO what happens and what not, i.e., it doesn't matter whether it's
551 * TODO a MIME-encoded word or not, as long as a single separating space
552 * TODO remains in between lines (the MIME stuff will correctly remove
553 * TODO whitespace in between multiple adjacent encoded words) */
554 hps &= ~HPS_ISENC_2;
555 if (cnt && (c = getc(ibuf)) != EOF) {
556 if (blankchar(c)) {
557 cp = line + linelen - 1;
558 if (linelen > 0 && *cp == '\n')
559 --cp;
560 while (cp >= line && whitechar(*cp))
561 --cp;
562 if (PTR2SIZE(cp - line > 8) && cp[0] == '=' && cp[-1] == '?')
563 hps |= HPS_ISENC_2;
565 ungetc(c, ibuf);
568 if (!(hps & HPS_IGNORE)) {
569 size_t len = linelen;
570 start = line;
571 if (action == SEND_TODISP || action == SEND_TODISP_ALL ||
572 action == SEND_QUOTE || action == SEND_QUOTE_ALL ||
573 action == SEND_TOSRCH) {
574 /* Strip blank characters if two MIME-encoded words follow on
575 * continuing lines */
576 if (hps & HPS_ISENC_1)
577 while (len > 0 && blankchar(*start)) {
578 ++start;
579 --len;
581 if (hps & HPS_ISENC_2)
582 if (len > 0 && start[len - 1] == '\n')
583 --len;
584 while (len > 0 && blankchar(start[len - 1]))
585 --len;
587 #ifdef HAVE_COLOUR
589 bool_t colour_stripped = FAL0;
590 if (tmpname != NULL) {
591 n_colour_put(obuf, n_COLOUR_ID_VIEW_HEADER, tmpname);
592 if (len > 0 && start[len - 1] == '\n') {
593 colour_stripped = TRU1;
594 --len;
597 #endif
598 _out(start, len, obuf, convert, action, qf, stats, NULL,NULL);
599 #ifdef HAVE_COLOUR
600 if (tmpname != NULL) {
601 n_colour_reset(obuf);
602 if (colour_stripped)
603 putc('\n', obuf);
606 #endif
607 if (ferror(obuf)) {
608 free(line);
609 rv = -1;
610 goto jleave;
614 } /* C99 */
615 quoteflt_flush(qf);
616 free(line);
617 line = NULL;
618 tmpname = NULL;
620 jheaders_skip:
621 memset(&mh, 0, sizeof mh);
623 switch (ip->m_mimecontent) {
624 case MIME_822:
625 switch (action) {
626 case SEND_TODISP:
627 case SEND_TODISP_ALL:
628 case SEND_QUOTE:
629 case SEND_QUOTE_ALL:
630 if (ok_blook(rfc822_body_from_)) {
631 if (qf->qf_pfix_len > 0) {
632 size_t i = fwrite(qf->qf_pfix, sizeof *qf->qf_pfix,
633 qf->qf_pfix_len, obuf);
634 if (i == qf->qf_pfix_len && stats != NULL)
635 *stats += i;
637 put_from_(obuf, ip->m_multipart, stats);
639 /* FALLTHRU */
640 case SEND_TOSRCH:
641 case SEND_DECRYPT:
642 goto jmulti;
643 case SEND_TOFILE:
644 case SEND_TOPIPE:
645 if (ok_blook(rfc822_body_from_))
646 put_from_(obuf, ip->m_multipart, stats);
647 /* FALLTHRU */
648 case SEND_MBOX:
649 case SEND_RFC822:
650 case SEND_SHOW:
651 break;
653 break;
654 case MIME_TEXT_HTML:
655 case MIME_TEXT:
656 case MIME_TEXT_PLAIN:
657 switch (action) {
658 case SEND_TODISP:
659 case SEND_TODISP_ALL:
660 case SEND_QUOTE:
661 case SEND_QUOTE_ALL:
662 switch (mime_type_handler(&mh, ip, action)) {
663 case MIME_HDL_MSG:
664 _out(mh.mh_msg.s, mh.mh_msg.l, obuf, CONV_NONE, SEND_MBOX, qf,
665 stats, NULL, NULL);
666 /* We would print this as plain text, so better force going home */
667 goto jleave;
668 default:
669 break;
671 /* FALLTRHU */
672 default:
673 break;
675 break;
676 case MIME_DISCARD:
677 if (action != SEND_DECRYPT)
678 goto jleave;
679 break;
680 case MIME_PKCS7:
681 if (action != SEND_MBOX && action != SEND_RFC822 &&
682 action != SEND_SHOW && ip->m_multipart != NULL)
683 goto jmulti;
684 /* FALLTHRU */
685 default:
686 switch (action) {
687 case SEND_TODISP:
688 case SEND_TODISP_ALL:
689 case SEND_QUOTE:
690 case SEND_QUOTE_ALL:
691 switch (mime_type_handler(&mh, ip, action)) {
692 case MIME_HDL_MSG:
693 _out(mh.mh_msg.s, mh.mh_msg.l, obuf, CONV_NONE, SEND_MBOX, qf,
694 stats, NULL, NULL);
695 /* We would print this as plain text, so better force going home */
696 goto jleave;
697 case MIME_HDL_CMD:
698 /* FIXME WE NEED TO DO THAT IF WE ARE THE ONLY MAIL
699 * FIXME CONTENT !! */
700 case MIME_HDL_TEXT:
701 break;
702 default:
703 case MIME_HDL_NULL:
704 if (level == 0 && cnt) {
705 char const *x = _("[-- Binary content --]\n");
706 _out(x, strlen(x), obuf, CONV_NONE, SEND_MBOX, qf, stats,
707 NULL,NULL);
709 goto jleave;
711 break;
712 case SEND_TOFILE:
713 case SEND_TOPIPE:
714 case SEND_TOSRCH:
715 case SEND_DECRYPT:
716 case SEND_MBOX:
717 case SEND_RFC822:
718 case SEND_SHOW:
719 break;
721 break;
722 case MIME_ALTERNATIVE:
723 if ((action == SEND_TODISP || action == SEND_QUOTE) &&
724 !ok_blook(print_alternatives)) {
725 /* XXX This (a) should not remain (b) should be own fun
726 * TODO (despite the fact that v15 will do this completely differently
727 * TODO by having an action-specific "manager" that will traverse the
728 * TODO parsed MIME tree and decide for each part whether it'll be
729 * TODO displayed or not *before* we walk the tree for doing action */
730 struct mpstack {
731 struct mpstack *outer;
732 struct mimepart *mp;
733 } outermost, * volatile curr, * volatile mpsp;
734 bool_t volatile neednl, hadpart;
735 struct n_sigman smalter;
737 (curr = &outermost)->outer = NULL;
738 curr->mp = ip;
739 neednl = hadpart = FAL0;
741 n_SIGMAN_ENTER_SWITCH(&smalter, n_SIGMAN_ALL) {
742 case 0:
743 break;
744 default:
745 rv = -1;
746 goto jalter_leave;
749 for (np = ip->m_multipart;;) {
750 jalter_redo:
751 for (; np != NULL; np = np->m_nextpart) {
752 if (action != SEND_QUOTE && np->m_ct_type_plain != NULL) {
753 if (neednl)
754 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats,
755 NULL, NULL);
756 _print_part_info(obuf, np, doitp, level, qf, stats);
758 neednl = TRU1;
760 switch (np->m_mimecontent) {
761 case MIME_ALTERNATIVE:
762 case MIME_RELATED:
763 case MIME_DIGEST:
764 case MIME_SIGNED:
765 case MIME_ENCRYPTED:
766 case MIME_MULTI:
767 mpsp = salloc(sizeof *mpsp);
768 mpsp->outer = curr;
769 mpsp->mp = np->m_multipart;
770 curr->mp = np;
771 curr = mpsp;
772 np = mpsp->mp;
773 neednl = FAL0;
774 goto jalter_redo;
775 default:
776 if (hadpart)
777 break;
778 switch (mime_type_handler(&mh, np, action)) {
779 default:
780 mh.mh_flags = MIME_HDL_NULL;
781 continue; /* break; break; */
782 case MIME_HDL_PTF:
783 if (!ok_blook(mime_alternative_favour_rich)) {/* TODO */
784 struct mimepart *x = np;
786 while ((x = x->m_nextpart) != NULL) {
787 struct mime_handler mhx;
789 if (x->m_mimecontent == MIME_TEXT_PLAIN ||
790 mime_type_handler(&mhx, x, action) ==
791 MIME_HDL_TEXT)
792 break;
794 if (x != NULL)
795 continue; /* break; break; */
796 goto jalter_plain;
798 /* FALLTHRU */
799 case MIME_HDL_TEXT:
800 break;
802 /* FALLTHRU */
803 case MIME_TEXT_PLAIN:
804 if (hadpart)
805 break;
806 if (ok_blook(mime_alternative_favour_rich)) { /* TODO */
807 struct mimepart *x = np;
809 /* TODO twice TODO, we should dive into /related and
810 * TODO check whether that has rich parts! */
811 while ((x = x->m_nextpart) != NULL) {
812 struct mime_handler mhx;
814 switch (mime_type_handler(&mhx, x, action)) {
815 case MIME_HDL_PTF:
816 break;
817 default:
818 continue;
820 break;
822 if (x != NULL)
823 continue; /* break; break; */
825 jalter_plain:
826 quoteflt_flush(qf);
827 if (action == SEND_QUOTE && hadpart) {
828 struct quoteflt *dummy = quoteflt_dummy();
829 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, dummy, stats,
830 NULL,NULL);
831 quoteflt_flush(dummy);
833 hadpart = TRU1;
834 neednl = FAL0;
835 rv = sendpart(zmp, np, obuf, doitp, qf, action, stats,
836 level + 1);
837 quoteflt_reset(qf, origobuf);
839 if (rv < 0)
840 curr = &outermost; /* Cause overall loop termination */
841 break;
845 mpsp = curr->outer;
846 if (mpsp == NULL)
847 break;
848 curr = mpsp;
849 np = curr->mp->m_nextpart;
851 jalter_leave:
852 n_sigman_leave(&smalter, n_SIGMAN_VIPSIGS_NTTYOUT);
853 goto jleave;
855 /* FALLTHRU */
856 case MIME_RELATED:
857 case MIME_DIGEST:
858 case MIME_SIGNED:
859 case MIME_ENCRYPTED:
860 case MIME_MULTI:
861 switch (action) {
862 case SEND_TODISP:
863 case SEND_TODISP_ALL:
864 case SEND_QUOTE:
865 case SEND_QUOTE_ALL:
866 case SEND_TOFILE:
867 case SEND_TOPIPE:
868 case SEND_TOSRCH:
869 case SEND_DECRYPT:
870 jmulti:
871 if ((action == SEND_TODISP || action == SEND_TODISP_ALL) &&
872 ip->m_multipart != NULL &&
873 ip->m_multipart->m_mimecontent == MIME_DISCARD &&
874 ip->m_multipart->m_nextpart == NULL) {
875 char const *x = _("[Missing multipart boundary - use show "
876 "to display the raw message]\n");
877 _out(x, strlen(x), obuf, CONV_NONE, SEND_MBOX, qf, stats,
878 NULL,NULL);
881 for (np = ip->m_multipart; np != NULL; np = np->m_nextpart) {
882 bool_t volatile ispipe;
884 if (np->m_mimecontent == MIME_DISCARD && action != SEND_DECRYPT)
885 continue;
887 ispipe = FAL0;
888 switch (action) {
889 case SEND_TOFILE:
890 if (np->m_partstring && !strcmp(np->m_partstring, "1"))
891 break;
892 stats = NULL;
893 /* TODO Always open multipart on /dev/null, it's a hack to be
894 * TODO able to dive into that structure, and still better
895 * TODO than asking the user for something stupid.
896 * TODO oh, wait, we did ask for a filename for this MIME mail,
897 * TODO and that outer container is useless anyway ;-P */
898 if (np->m_multipart != NULL) {
899 if ((obuf = Fopen("/dev/null", "w")) == NULL)
900 continue;
901 } else if ((obuf = newfile(np, &ispipe)) == NULL)
902 continue;
903 if (!ispipe)
904 break;
905 if (sigsetjmp(_send_pipejmp, 1)) {
906 rv = -1;
907 goto jpipe_close;
909 oldpipe = safe_signal(SIGPIPE, &_send_onpipe);
910 break;
911 case SEND_TODISP:
912 case SEND_TODISP_ALL:
913 if (ip->m_mimecontent != MIME_ALTERNATIVE &&
914 ip->m_mimecontent != MIME_RELATED &&
915 ip->m_mimecontent != MIME_DIGEST &&
916 ip->m_mimecontent != MIME_SIGNED &&
917 ip->m_mimecontent != MIME_ENCRYPTED &&
918 ip->m_mimecontent != MIME_MULTI)
919 break;
920 _print_part_info(obuf, np, doitp, level, qf, stats);
921 break;
922 case SEND_QUOTE:
923 case SEND_QUOTE_ALL:
924 case SEND_MBOX:
925 case SEND_RFC822:
926 case SEND_SHOW:
927 case SEND_TOSRCH:
928 case SEND_DECRYPT:
929 case SEND_TOPIPE:
930 break;
933 quoteflt_flush(qf);
934 if ((action == SEND_QUOTE || action == SEND_QUOTE_ALL) &&
935 np->m_multipart == NULL && ip->m_parent != NULL) {
936 struct quoteflt *dummy = quoteflt_dummy();
937 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, dummy, stats,
938 NULL,NULL);
939 quoteflt_flush(dummy);
941 if (sendpart(zmp, np, obuf, doitp, qf, action, stats, level+1) < 0)
942 rv = -1;
943 quoteflt_reset(qf, origobuf);
945 if (action == SEND_QUOTE) {
946 if (ip->m_mimecontent != MIME_RELATED)
947 break;
949 if (action == SEND_TOFILE && obuf != origobuf) {
950 if (!ispipe)
951 Fclose(obuf);
952 else {
953 jpipe_close:
954 safe_signal(SIGPIPE, SIG_IGN);
955 Pclose(obuf, TRU1);
956 safe_signal(SIGPIPE, oldpipe);
960 goto jleave;
961 case SEND_MBOX:
962 case SEND_RFC822:
963 case SEND_SHOW:
964 break;
966 break;
969 /* Copy out message body */
970 if (doitp == n_IGNORE_ALL && level == 0) /* skip final blank line */
971 --cnt;
972 switch (ip->m_mime_enc) {
973 case MIMEE_BIN:
974 case MIMEE_7B:
975 case MIMEE_8B:
976 convert = CONV_NONE;
977 break;
978 case MIMEE_QP:
979 convert = CONV_FROMQP;
980 break;
981 case MIMEE_B64:
982 switch (ip->m_mimecontent) {
983 case MIME_TEXT:
984 case MIME_TEXT_PLAIN:
985 case MIME_TEXT_HTML:
986 convert = CONV_FROMB64_T;
987 break;
988 default:
989 switch (mh.mh_flags & MIME_HDL_TYPE_MASK) {
990 case MIME_HDL_TEXT:
991 case MIME_HDL_PTF:
992 convert = CONV_FROMB64_T;
993 break;
994 default:
995 convert = CONV_FROMB64;
996 break;
998 break;
1000 break;
1001 default:
1002 convert = CONV_NONE;
1005 /* TODO Unless we have filters, ensure iconvd==-1 so that mime.c:fwrite_td()
1006 * TODO cannot mess things up misusing outrest as line buffer */
1007 #ifdef HAVE_ICONV
1008 if (iconvd != (iconv_t)-1) {
1009 n_iconv_close(iconvd);
1010 iconvd = (iconv_t)-1;
1012 #endif
1014 if (action == SEND_DECRYPT || action == SEND_MBOX ||
1015 action == SEND_RFC822 || action == SEND_SHOW)
1016 convert = CONV_NONE;
1017 #ifdef HAVE_ICONV
1018 else if ((action == SEND_TODISP || action == SEND_TODISP_ALL ||
1019 action == SEND_QUOTE || action == SEND_QUOTE_ALL ||
1020 action == SEND_TOSRCH) &&
1021 (ip->m_mimecontent == MIME_TEXT_PLAIN ||
1022 ip->m_mimecontent == MIME_TEXT_HTML ||
1023 ip->m_mimecontent == MIME_TEXT ||
1024 (mh.mh_flags & MIME_HDL_TYPE_MASK) == MIME_HDL_TEXT ||
1025 (mh.mh_flags & MIME_HDL_TYPE_MASK) == MIME_HDL_PTF)) {
1026 char const *tcs;
1028 tcs = ok_vlook(ttycharset);
1029 if (asccasecmp(tcs, ip->m_charset) &&
1030 asccasecmp(ok_vlook(charset_7bit), ip->m_charset)) {
1031 iconvd = n_iconv_open(tcs, ip->m_charset);
1032 if (iconvd == (iconv_t)-1 && errno == EINVAL) {
1033 n_err(_("Cannot convert from %s to %s\n"), ip->m_charset, tcs);
1034 /*rv = 1; goto jleave;*/
1038 #endif
1040 switch (mh.mh_flags & MIME_HDL_TYPE_MASK) {
1041 case MIME_HDL_CMD:
1042 case MIME_HDL_PTF:
1043 tmpname = NULL;
1044 qbuf = obuf;
1046 term_infd = COMMAND_FD_PASS;
1047 if (mh.mh_flags & (MIME_HDL_TMPF | MIME_HDL_NEEDSTERM)) {
1048 enum oflags of;
1050 of = OF_RDWR | OF_REGISTER;
1051 if (!(mh.mh_flags & MIME_HDL_TMPF)) {
1052 term_infd = 0;
1053 mh.mh_flags |= MIME_HDL_TMPF_FILL;
1054 of |= OF_UNLINK;
1055 } else if (mh.mh_flags & MIME_HDL_TMPF_UNLINK)
1056 of |= OF_REGISTER_UNLINK;
1058 if ((pbuf = Ftmp((mh.mh_flags & MIME_HDL_TMPF ? &cp : NULL),
1059 (mh.mh_flags & MIME_HDL_TMPF_FILL ? "mimehdlfill" : "mimehdl"),
1060 of)) == NULL)
1061 goto jesend;
1063 if (mh.mh_flags & MIME_HDL_TMPF) {
1064 tmpname = savestr(cp);
1065 Ftmp_free(&cp);
1068 if (mh.mh_flags & MIME_HDL_TMPF_FILL) {
1069 if (term_infd == 0)
1070 term_infd = fileno(pbuf);
1071 goto jsend;
1075 jpipe_for_real:
1076 pbuf = _pipefile(&mh, ip, n_UNVOLATILE(&qbuf), tmpname, term_infd);
1077 if (pbuf == NULL) {
1078 jesend:
1079 pbuf = qbuf = NULL;
1080 rv = -1;
1081 goto jend;
1082 } else if ((mh.mh_flags & MIME_HDL_NEEDSTERM) && pbuf == (FILE*)-1) {
1083 pbuf = qbuf = NULL;
1084 goto jend;
1086 tmpname = NULL;
1087 action = SEND_TOPIPE;
1088 if (pbuf != qbuf) {
1089 oldpipe = safe_signal(SIGPIPE, &_send_onpipe);
1090 if (sigsetjmp(_send_pipejmp, 1))
1091 goto jend;
1093 break;
1095 default:
1096 mh.mh_flags = MIME_HDL_NULL;
1097 pbuf = qbuf = obuf;
1098 break;
1101 jsend:
1103 bool_t volatile eof;
1104 ui32_t save_qf_pfix_len = qf->qf_pfix_len;
1105 ui64_t *save_stats = stats;
1107 if (pbuf != origobuf) {
1108 qf->qf_pfix_len = 0; /* XXX legacy (remove filter instead) */
1109 stats = NULL;
1111 eof = FAL0;
1112 outrest.s = inrest.s = NULL;
1113 outrest.l = inrest.l = 0;
1115 if (pbuf == qbuf) {
1116 __sendp_sig = 0;
1117 __sendp_opipe = safe_signal(SIGPIPE, &__sendp_onsig);
1118 if (sigsetjmp(__sendp_actjmp, 1)) {
1119 if (outrest.s != NULL)
1120 free(outrest.s);
1121 if (inrest.s != NULL)
1122 free(inrest.s);
1123 free(line);
1124 #ifdef HAVE_ICONV
1125 if (iconvd != (iconv_t)-1)
1126 n_iconv_close(iconvd);
1127 #endif
1128 safe_signal(SIGPIPE, __sendp_opipe);
1129 n_raise(__sendp_sig);
1133 quoteflt_reset(qf, pbuf);
1134 while (!eof && fgetline(&line, &linesize, &cnt, &linelen, ibuf, 0)) {
1135 joutln:
1136 if (_out(line, linelen, pbuf, convert, action, qf, stats, &outrest,
1137 (action & _TD_EOF ? NULL : &inrest)) < 0 || ferror(pbuf)) {
1138 rv = -1; /* XXX Should bail away?! */
1139 break;
1142 if(eof <= FAL0 && rv >= 0 && (outrest.l != 0 || inrest.l != 0)){
1143 linelen = 0;
1144 if(eof || inrest.l == 0)
1145 action |= _TD_EOF;
1146 eof = eof ? TRU1 : TRUM1;
1147 goto joutln;
1149 action &= ~_TD_EOF;
1151 /* TODO HACK: when sending to the display we yet get fooled if a message
1152 * TODO doesn't end in a newline, because of our input/output 1:1.
1153 * TODO This should be handled automatically by a display filter, then */
1154 if(rv >= 0 && !qf->qf_nl_last &&
1155 (action == SEND_TODISP || action == SEND_TODISP_ALL))
1156 rv = quoteflt_push(qf, "\n", 1);
1158 quoteflt_flush(qf);
1160 if (rv >= 0 && (mh.mh_flags & MIME_HDL_TMPF_FILL)) {
1161 mh.mh_flags &= ~MIME_HDL_TMPF_FILL;
1162 fflush(pbuf);
1163 really_rewind(pbuf);
1164 /* Don't Fclose() the Ftmp() thing due to OF_REGISTER_UNLINK++ */
1165 goto jpipe_for_real;
1168 if (pbuf == qbuf)
1169 safe_signal(SIGPIPE, __sendp_opipe);
1171 if (outrest.s != NULL)
1172 free(outrest.s);
1173 if (inrest.s != NULL)
1174 free(inrest.s);
1176 if (pbuf != origobuf) {
1177 qf->qf_pfix_len = save_qf_pfix_len;
1178 stats = save_stats;
1182 jend:
1183 if (line != NULL)
1184 free(line);
1185 if (pbuf != qbuf) {
1186 safe_signal(SIGPIPE, SIG_IGN);
1187 Pclose(pbuf, !(mh.mh_flags & MIME_HDL_ASYNC));
1188 safe_signal(SIGPIPE, oldpipe);
1189 if (rv >= 0 && qbuf != NULL && qbuf != obuf)
1190 pipecpy(qbuf, obuf, origobuf, qf, stats);
1192 #ifdef HAVE_ICONV
1193 if (iconvd != (iconv_t)-1)
1194 n_iconv_close(iconvd);
1195 #endif
1196 jleave:
1197 NYD_LEAVE;
1198 return rv;
1201 static FILE *
1202 newfile(struct mimepart *ip, bool_t volatile *ispipe)
1204 struct str in, out;
1205 char *f;
1206 FILE *fp;
1207 NYD_ENTER;
1209 f = ip->m_filename;
1210 *ispipe = FAL0;
1212 if (f != NULL && f != (char*)-1) {
1213 in.s = f;
1214 in.l = strlen(f);
1215 makeprint(&in, &out);
1216 out.l = delctrl(out.s, out.l);
1217 f = savestrbuf(out.s, out.l);
1218 free(out.s);
1221 /* In interactive mode, let user perform all kind of expansions as desired,
1222 * and offer |SHELL-SPEC pipe targets, too */
1223 if (options & OPT_INTERACTIVE) {
1224 struct str prompt;
1225 struct n_string shou, *shoup;
1226 char *f2, *f3;
1228 shoup = n_string_creat_auto(&shou);
1230 /* TODO Generic function which asks for filename.
1231 * TODO If the current part is the first textpart the target
1232 * TODO is implicit from outer `write' etc! */
1233 /* I18N: Filename input prompt with file type indication */
1234 str_concat_csvl(&prompt, _("Enter filename for part "),
1235 (ip->m_partstring != NULL) ? ip->m_partstring : _("?"),
1236 _(" ("), ip->m_ct_type_plain, _("): "), NULL);
1237 jgetname:
1238 f2 = n_lex_input_cp(n_LEXINPUT_CTX_DEFAULT | n_LEXINPUT_HIST_ADD,
1239 prompt.s, ((f != (char*)-1 && f != NULL)
1240 ? n_shexp_quote_cp(f, FAL0) : NULL));
1241 if(f2 != NULL){
1242 in.s = n_UNCONST(f2);
1243 in.l = UIZ_MAX;
1244 if((n_shexp_parse_token(shoup, &in, n_SHEXP_PARSE_TRUNC |
1245 n_SHEXP_PARSE_TRIMSPACE | n_SHEXP_PARSE_LOG |
1246 n_SHEXP_PARSE_IGNORE_EMPTY) &
1247 (n_SHEXP_STATE_STOP | n_SHEXP_STATE_OUTPUT |
1248 n_SHEXP_STATE_ERR_MASK)
1249 ) != (n_SHEXP_STATE_STOP | n_SHEXP_STATE_OUTPUT))
1250 goto jgetname;
1251 f2 = n_string_cp(shoup);
1253 if (f2 == NULL || *f2 == '\0') {
1254 if (options & OPT_D_V)
1255 n_err(_("... skipping this\n"));
1256 n_string_gut(shoup);
1257 fp = NULL;
1258 goto jleave;
1261 if (*f2 == '|')
1262 /* Pipes are expanded by the shell */
1263 f = f2;
1264 else if ((f3 = fexpand(f2, FEXP_LOCAL | FEXP_NVAR)) == NULL)
1265 /* (Error message written by fexpand()) */
1266 goto jgetname;
1267 else
1268 f = f3;
1270 n_string_gut(shoup);
1273 if (f == NULL || f == (char*)-1 || *f == '\0')
1274 fp = NULL;
1275 else if (options & OPT_INTERACTIVE) {
1276 if (*f == '|') {
1277 fp = Popen(&f[1], "w", ok_vlook(SHELL), NULL, 1);
1278 if (!(*ispipe = (fp != NULL)))
1279 n_perr(f, 0);
1280 } else if ((fp = Fopen(f, "w")) == NULL)
1281 n_err(_("Cannot open %s\n"), n_shexp_quote_cp(f, FAL0));
1282 } else {
1283 /* Be very picky in non-interactive mode: actively disallow pipes,
1284 * prevent directory separators, and any filename member that would
1285 * become expanded by the shell if the name would be echo(1)ed */
1286 if(anyof(f, "/" n_SHEXP_MAGIC_PATH_CHARS)){
1287 char c;
1289 for(out.s = salloc((strlen(f) * 3) +1), out.l = 0; (c = *f++) != '\0';)
1290 if(strchr("/" n_SHEXP_MAGIC_PATH_CHARS, c)){
1291 out.s[out.l++] = '%';
1292 n_c_to_hex_base16(&out.s[out.l], c);
1293 out.l += 2;
1294 }else
1295 out.s[out.l++] = c;
1296 out.s[out.l] = '\0';
1297 f = out.s;
1300 /* Avoid overwriting of existing files */
1301 while((fp = Fopen(f, "wx")) == NULL){
1302 int e;
1304 if((e = errno) != EEXIST){
1305 n_err(_("Cannot open %s: %s\n"),
1306 n_shexp_quote_cp(f, FAL0), strerror(e));
1307 break;
1310 if(ip->m_partstring != NULL)
1311 f = savecatsep(f, '#', ip->m_partstring);
1312 else
1313 f = savecat(f, "#.");
1316 jleave:
1317 NYD_LEAVE;
1318 return fp;
1321 static void
1322 pipecpy(FILE *pipebuf, FILE *outbuf, FILE *origobuf, struct quoteflt *qf,
1323 ui64_t *stats)
1325 char *line = NULL; /* TODO line pool */
1326 size_t linesize = 0, linelen, cnt;
1327 ssize_t all_sz, sz;
1328 NYD_ENTER;
1330 fflush(pipebuf);
1331 rewind(pipebuf);
1332 cnt = (size_t)fsize(pipebuf);
1333 all_sz = 0;
1335 quoteflt_reset(qf, outbuf);
1336 while (fgetline(&line, &linesize, &cnt, &linelen, pipebuf, 0) != NULL) {
1337 if ((sz = quoteflt_push(qf, line, linelen)) < 0)
1338 break;
1339 all_sz += sz;
1341 if ((sz = quoteflt_flush(qf)) > 0)
1342 all_sz += sz;
1343 if (line)
1344 free(line);
1346 if (all_sz > 0 && outbuf == origobuf && stats != NULL)
1347 *stats += all_sz;
1348 Fclose(pipebuf);
1349 NYD_LEAVE;
1352 static void
1353 statusput(const struct message *mp, FILE *obuf, struct quoteflt *qf,
1354 ui64_t *stats)
1356 char statout[3], *cp = statout;
1357 NYD_ENTER;
1359 if (mp->m_flag & MREAD)
1360 *cp++ = 'R';
1361 if (!(mp->m_flag & MNEW))
1362 *cp++ = 'O';
1363 *cp = 0;
1364 if (statout[0]) {
1365 int i = fprintf(obuf, "%.*sStatus: %s\n", (int)qf->qf_pfix_len,
1366 (qf->qf_pfix_len > 0 ? qf->qf_pfix : 0), statout);
1367 if (i > 0 && stats != NULL)
1368 *stats += i;
1370 NYD_LEAVE;
1373 static void
1374 xstatusput(const struct message *mp, FILE *obuf, struct quoteflt *qf,
1375 ui64_t *stats)
1377 char xstatout[4];
1378 char *xp = xstatout;
1379 NYD_ENTER;
1381 if (mp->m_flag & MFLAGGED)
1382 *xp++ = 'F';
1383 if (mp->m_flag & MANSWERED)
1384 *xp++ = 'A';
1385 if (mp->m_flag & MDRAFTED)
1386 *xp++ = 'T';
1387 *xp = 0;
1388 if (xstatout[0]) {
1389 int i = fprintf(obuf, "%.*sX-Status: %s\n", (int)qf->qf_pfix_len,
1390 (qf->qf_pfix_len > 0 ? qf->qf_pfix : 0), xstatout);
1391 if (i > 0 && stats != NULL)
1392 *stats += i;
1394 NYD_LEAVE;
1397 static void
1398 put_from_(FILE *fp, struct mimepart *ip, ui64_t *stats)
1400 char const *froma, *date, *nl;
1401 int i;
1402 NYD_ENTER;
1404 if (ip != NULL && ip->m_from != NULL) {
1405 froma = ip->m_from;
1406 date = fakedate(ip->m_time);
1407 nl = "\n";
1408 } else {
1409 froma = ok_vlook(LOGNAME);
1410 date = time_current.tc_ctime;
1411 nl = n_empty;
1414 n_COLOUR( n_colour_put(fp, n_COLOUR_ID_VIEW_FROM_, NULL); )
1415 i = fprintf(fp, "From %s %s%s", froma, date, nl);
1416 n_COLOUR( n_colour_reset(fp); )
1417 if (i > 0 && stats != NULL)
1418 *stats += i;
1419 NYD_LEAVE;
1422 FL int
1423 sendmp(struct message *mp, FILE *obuf, struct n_ignore const *doitp,
1424 char const *prefix, enum sendaction action, ui64_t *stats)
1426 struct quoteflt qf;
1427 size_t cnt, sz, i;
1428 FILE *ibuf;
1429 enum mime_parse_flags mpf;
1430 struct mimepart *ip;
1431 int rv = -1, c;
1432 NYD_ENTER;
1434 time_current_update(&time_current, TRU1);
1436 if (mp == dot && action != SEND_TOSRCH)
1437 pstate |= PS_DID_PRINT_DOT;
1438 if (stats != NULL)
1439 *stats = 0;
1440 quoteflt_init(&qf, prefix);
1442 /* First line is the From_ line, so no headers there to worry about */
1443 if ((ibuf = setinput(&mb, mp, NEED_BODY)) == NULL)
1444 goto jleave;
1446 cnt = mp->m_size;
1447 sz = 0;
1449 bool_t nozap;
1450 char const *cpre = n_empty, *csuf = n_empty;
1451 #ifdef HAVE_COLOUR
1452 struct n_colour_pen *cpen = n_colour_pen_create(n_COLOUR_ID_VIEW_FROM_,NULL);
1453 struct str const *sp = n_colour_pen_to_str(cpen);
1455 if (sp != NULL) {
1456 cpre = sp->s;
1457 sp = n_colour_reset_to_str();
1458 if (sp != NULL)
1459 csuf = sp->s;
1461 #endif
1463 nozap = (doitp != n_IGNORE_ALL && doitp != n_IGNORE_FWD &&
1464 action != SEND_RFC822 &&
1465 !n_ignore_is_ign(doitp, "from_", sizeof("from_") -1));
1466 if (mp->m_flag & MNOFROM) {
1467 if (nozap)
1468 sz = fprintf(obuf, "%s%.*sFrom %s %s%s\n",
1469 cpre, (int)qf.qf_pfix_len,
1470 (qf.qf_pfix_len != 0 ? qf.qf_pfix : n_empty), fakefrom(mp),
1471 fakedate(mp->m_time), csuf);
1472 } else if (nozap) {
1473 if (qf.qf_pfix_len > 0) {
1474 i = fwrite(qf.qf_pfix, sizeof *qf.qf_pfix, qf.qf_pfix_len, obuf);
1475 if (i != qf.qf_pfix_len)
1476 goto jleave;
1477 sz += i;
1479 #ifdef HAVE_COLOUR
1480 if (cpre != NULL) {
1481 fputs(cpre, obuf);
1482 cpre = (char const*)0x1;
1484 #endif
1486 while (cnt > 0 && (c = getc(ibuf)) != EOF) {
1487 #ifdef HAVE_COLOUR
1488 if (c == '\n' && csuf != NULL) {
1489 cpre = (char const*)0x1;
1490 fputs(csuf, obuf);
1492 #endif
1493 putc(c, obuf);
1494 ++sz;
1495 --cnt;
1496 if (c == '\n')
1497 break;
1500 #ifdef HAVE_COLOUR
1501 if (csuf != NULL && cpre != (char const*)0x1)
1502 fputs(csuf, obuf);
1503 #endif
1504 } else {
1505 while (cnt > 0 && (c = getc(ibuf)) != EOF) {
1506 --cnt;
1507 if (c == '\n')
1508 break;
1512 if (sz > 0 && stats != NULL)
1513 *stats += sz;
1515 mpf = MIME_PARSE_NONE;
1516 if (action != SEND_MBOX && action != SEND_RFC822 && action != SEND_SHOW)
1517 mpf |= MIME_PARSE_PARTS | MIME_PARSE_DECRYPT;
1518 if ((ip = mime_parse_msg(mp, mpf)) == NULL)
1519 goto jleave;
1521 rv = sendpart(mp, ip, obuf, doitp, &qf, action, stats, 0);
1522 jleave:
1523 quoteflt_destroy(&qf);
1524 NYD_LEAVE;
1525 return rv;
1528 /* s-it-mode */