Temporarily freeze variables set via -S..
[s-mailx.git] / send.c
blobdb41d6169ab4138b2bf60517fa79e63709205420
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 n_PIPEENV_* 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 char **linedat, size_t *linesize,
69 ui64_t *stats, int level);
71 /* Get a file for an attachment */
72 static FILE * newfile(struct mimepart *ip, bool_t volatile *ispipe);
74 static void pipecpy(FILE *pipebuf, FILE *outbuf, FILE *origobuf,
75 struct quoteflt *qf, ui64_t *stats);
77 /* Output a reasonable looking status field */
78 static void statusput(const struct message *mp, FILE *obuf,
79 struct quoteflt *qf, ui64_t *stats);
80 static void xstatusput(const struct message *mp, FILE *obuf,
81 struct quoteflt *qf, ui64_t *stats);
83 static void put_from_(FILE *fp, struct mimepart *ip, ui64_t *stats);
85 static void
86 _print_part_info(FILE *obuf, struct mimepart const *mpp, /* TODO strtofmt.. */
87 struct n_ignore const *doitp, int level, struct quoteflt *qf, ui64_t *stats)
89 char buf[64];
90 struct str ti, to;
91 bool_t want_ct, needsep;
92 struct str const *cpre, *csuf;
93 char const *cp;
94 NYD2_ENTER;
96 cpre = csuf = NULL;
97 #ifdef HAVE_COLOUR
98 if(n_COLOUR_IS_ACTIVE()){
99 struct n_colour_pen *cpen;
101 cpen = n_colour_pen_create(n_COLOUR_ID_VIEW_PARTINFO, NULL);
102 if((cpre = n_colour_pen_to_str(cpen)) != NULL)
103 csuf = n_colour_reset_to_str();
105 #endif
107 /* Take care of "99.99", i.e., 5 */
108 if ((cp = mpp->m_partstring) == NULL || cp[0] == '\0')
109 cp = n_qm;
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 needsep = FAL0;
126 if((cp = mpp->m_ct_type_usr_ovwr) != NULL){
127 _out("+", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
128 want_ct = TRU1;
129 }else if((want_ct = n_ignore_is_ign(doitp,
130 "content-type", sizeof("content-type") -1)))
131 cp = mpp->m_ct_type_plain;
132 if (want_ct &&
133 (to.l = strlen(cp)) > 30 && is_asccaseprefix("application/", cp)) {
134 size_t const al = sizeof("appl../") -1, fl = sizeof("application/") -1;
135 size_t i = to.l - fl;
136 char *x = salloc(al + i +1);
138 memcpy(x, "appl../", al);
139 memcpy(x + al, cp + fl, i +1);
140 cp = x;
141 to.l = al + i;
143 if(cp != NULL){
144 _out(cp, to.l, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
145 needsep = TRU1;
148 if(mpp->m_multipart == NULL/* TODO */ && (cp = mpp->m_ct_enc) != NULL &&
149 (!asccasecmp(cp, "7bit") ||
150 n_ignore_is_ign(doitp, "content-transfer-encoding",
151 sizeof("content-transfer-encoding") -1))){
152 if(needsep)
153 _out(", ", 2, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
154 if (to.l > 25 && !asccasecmp(cp, "quoted-printable"))
155 cp = "qu.-pr.";
156 _out(cp, strlen(cp), obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
157 needsep = TRU1;
160 if (want_ct && mpp->m_multipart == NULL/* TODO */ &&
161 (cp = mpp->m_charset) != NULL) {
162 if(needsep)
163 _out(", ", 2, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
164 _out(cp, strlen(cp), obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
167 needsep = !needsep;
168 _out(&" --]"[needsep], 4 - needsep,
169 obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
170 if (csuf != NULL)
171 _out(csuf->s, csuf->l, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
172 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
174 /* */
175 if (mpp->m_content_info & CI_MIME_ERRORS) {
176 if (cpre != NULL)
177 _out(cpre->s, cpre->l, obuf, CONV_NONE, SEND_MBOX, qf, stats,
178 NULL, NULL);
179 _out("[-- ", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
181 ti.l = strlen(ti.s = n_UNCONST(_("Defective MIME structure")));
182 makeprint(&ti, &to);
183 to.l = delctrl(to.s, to.l);
184 _out(to.s, to.l, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
185 free(to.s);
187 _out(" --]", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
188 if (csuf != NULL)
189 _out(csuf->s, csuf->l, obuf, CONV_NONE, SEND_MBOX, qf, stats,
190 NULL, NULL);
191 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
194 /* Content-Description */
195 if (n_ignore_is_ign(doitp, "content-description", 19) &&
196 (cp = mpp->m_content_description) != NULL && *cp != '\0') {
197 if (cpre != NULL)
198 _out(cpre->s, cpre->l, obuf, CONV_NONE, SEND_MBOX, qf, stats,
199 NULL, NULL);
200 _out("[-- ", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
202 ti.l = strlen(ti.s = n_UNCONST(mpp->m_content_description));
203 mime_fromhdr(&ti, &to, TD_ISPR | TD_ICONV);
204 _out(to.s, to.l, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
205 free(to.s);
207 _out(" --]", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
208 if (csuf != NULL)
209 _out(csuf->s, csuf->l, obuf, CONV_NONE, SEND_MBOX, qf, stats,
210 NULL, NULL);
211 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
214 /* Filename */
215 if (n_ignore_is_ign(doitp, "content-disposition", 19) &&
216 mpp->m_filename != NULL && *mpp->m_filename != '\0') {
217 if (cpre != NULL)
218 _out(cpre->s, cpre->l, obuf, CONV_NONE, SEND_MBOX, qf, stats,
219 NULL, NULL);
220 _out("[-- ", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
222 ti.l = strlen(ti.s = mpp->m_filename);
223 makeprint(&ti, &to);
224 to.l = delctrl(to.s, to.l);
225 _out(to.s, to.l, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
226 free(to.s);
228 _out(" --]", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
229 if (csuf != NULL)
230 _out(csuf->s, csuf->l, obuf, CONV_NONE, SEND_MBOX, qf, stats,
231 NULL, NULL);
232 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
234 NYD2_LEAVE;
237 static FILE *
238 _pipefile(struct mime_handler *mhp, struct mimepart const *mpp, FILE **qbuf,
239 char const *tmpname, int term_infd)
241 struct str s;
242 char const *env_addon[8 +8/*v15*/], *cp, *sh;
243 FILE *rbuf;
244 NYD_ENTER;
246 rbuf = *qbuf;
248 if (mhp->mh_flags & MIME_HDL_ISQUOTE) {
249 if ((*qbuf = Ftmp(NULL, "sendp", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
250 NULL) {
251 n_perr(_("tmpfile"), 0);
252 *qbuf = rbuf;
256 if ((mhp->mh_flags & MIME_HDL_TYPE_MASK) == MIME_HDL_PTF) {
257 union {int (*ptf)(void); char const *sh;} u;
259 fflush(*qbuf);
260 if (*qbuf != n_stdout) /* xxx never? v15: it'll be a filter anyway */
261 fflush(n_stdout);
263 u.ptf = mhp->mh_ptf;
264 if((rbuf = Popen((char*)-1, "W", u.sh, NULL, fileno(*qbuf))) == NULL)
265 goto jerror;
266 goto jleave;
269 /* MAILX_FILENAME */
270 if (mpp == NULL || (cp = mpp->m_filename) == NULL)
271 cp = n_empty;
272 env_addon[0] = str_concat_csvl(&s, n_PIPEENV_FILENAME, "=", cp, NULL)->s;
273 env_addon[1] = str_concat_csvl(&s, "NAIL_FILENAME", "=", cp, NULL)->s;/* v15 */
275 /* MAILX_FILENAME_GENERATED *//* TODO pathconf NAME_MAX; but user can create
276 * TODO a file wherever he wants! *Do* create a zero-size temporary file
277 * TODO and give *that* path as MAILX_FILENAME_TEMPORARY, clean it up once
278 * TODO the pipe returns? Like this we *can* verify path/name issues! */
279 cp = n_random_create_cp(n_MIN(NAME_MAX / 4, 16), NULL);
280 env_addon[2] = str_concat_csvl(&s, n_PIPEENV_FILENAME_GENERATED, "=", cp,
281 NULL)->s;
282 env_addon[3] = str_concat_csvl(&s, "NAIL_FILENAME_GENERATED", "=", cp,/* v15 */
283 NULL)->s;
285 /* MAILX_CONTENT{,_EVIDENCE} */
286 if (mpp == NULL || (cp = mpp->m_ct_type_plain) == NULL)
287 cp = n_empty;
288 env_addon[4] = str_concat_csvl(&s, n_PIPEENV_CONTENT, "=", cp, NULL)->s;
289 env_addon[5] = str_concat_csvl(&s, "NAIL_CONTENT", "=", cp, NULL)->s;/* v15 */
291 if (mpp != NULL && mpp->m_ct_type_usr_ovwr != NULL)
292 cp = mpp->m_ct_type_usr_ovwr;
293 env_addon[6] = str_concat_csvl(&s, n_PIPEENV_CONTENT_EVIDENCE, "=", cp,
294 NULL)->s;
295 env_addon[7] = str_concat_csvl(&s, "NAIL_CONTENT_EVIDENCE", "=", cp,/* v15 */
296 NULL)->s;
298 env_addon[8] = NULL;
300 /* MAILX_FILENAME_TEMPORARY? */
301 if (tmpname != NULL) {
302 env_addon[8] = str_concat_csvl(&s,
303 n_PIPEENV_FILENAME_TEMPORARY, "=", tmpname, NULL)->s;
304 env_addon[9] = str_concat_csvl(&s,
305 "NAIL_FILENAME_TEMPORARY", "=", tmpname, NULL)->s;/* v15 */
306 env_addon[10] = NULL;
309 sh = ok_vlook(SHELL);
311 if (mhp->mh_flags & MIME_HDL_NEEDSTERM) {
312 sigset_t nset;
313 int pid;
315 sigemptyset(&nset);
316 pid = n_child_run(sh, &nset, term_infd, n_CHILD_FD_PASS, "-c",
317 mhp->mh_shell_cmd, NULL, env_addon, NULL);
318 rbuf = (pid < 0) ? NULL : (FILE*)-1;
319 } else {
320 rbuf = Popen(mhp->mh_shell_cmd, "W", sh, env_addon,
321 (mhp->mh_flags & MIME_HDL_ASYNC ? -1 : fileno(*qbuf)));
322 jerror:
323 if (rbuf == NULL)
324 n_err(_("Cannot run MIME type handler: %s: %s\n"),
325 mhp->mh_msg, n_err_to_doc(n_err_no));
326 else {
327 fflush(*qbuf);
328 if (*qbuf != n_stdout)
329 fflush(n_stdout);
332 jleave:
333 NYD_LEAVE;
334 return rbuf;
337 SINLINE ssize_t
338 _out(char const *buf, size_t len, FILE *fp, enum conversion convert, enum
339 sendaction action, struct quoteflt *qf, ui64_t *stats, struct str *outrest,
340 struct str *inrest)
342 ssize_t sz = 0, n;
343 int flags;
344 NYD_ENTER;
346 /* TODO We should not need is_head() here, i think in v15 the actual Mailbox
347 * TODO subclass should detect such From_ cases and either reencode the part
348 * TODO in question, or perform From_ quoting as necessary!?!?!? How?!? */
349 /* C99 */{
350 bool_t from_;
352 if((action == SEND_MBOX || action == SEND_DECRYPT) &&
353 (from_ = is_head(buf, len, TRU1))){
354 if(from_ != TRUM1 || ok_blook(mbox_rfc4155)){
355 putc('>', fp);
356 ++sz;
361 flags = ((int)action & _TD_EOF);
362 action &= ~_TD_EOF;
363 n = mime_write(buf, len, fp,
364 action == SEND_MBOX ? CONV_NONE : convert,
365 flags | ((action == SEND_TODISP || action == SEND_TODISP_ALL ||
366 action == SEND_TODISP_PARTS ||
367 action == SEND_QUOTE || action == SEND_QUOTE_ALL)
368 ? TD_ISPR | TD_ICONV
369 : (action == SEND_TOSRCH || action == SEND_TOPIPE ||
370 action == SEND_TOFILE)
371 ? TD_ICONV : (action == SEND_SHOW ? TD_ISPR : TD_NONE)),
372 qf, outrest, inrest);
373 if (n < 0)
374 sz = n;
375 else if (n > 0) {
376 sz += n;
377 if (stats != NULL)
378 *stats += sz;
380 NYD_LEAVE;
381 return sz;
384 static void
385 _send_onpipe(int signo)
387 NYD_X; /* Signal handler */
388 n_UNUSED(signo);
389 siglongjmp(_send_pipejmp, 1);
392 static sigjmp_buf __sendp_actjmp; /* TODO someday.. */
393 static int __sendp_sig; /* TODO someday.. */
394 static sighandler_type __sendp_opipe;
395 static void
396 __sendp_onsig(int sig) /* TODO someday, we won't need it no more */
398 NYD_X; /* Signal handler */
399 __sendp_sig = sig;
400 siglongjmp(__sendp_actjmp, 1);
403 static int
404 sendpart(struct message *zmp, struct mimepart *ip, FILE * volatile obuf,
405 struct n_ignore const *doitp, struct quoteflt *qf,
406 enum sendaction volatile action,
407 char **linedat, size_t *linesize, ui64_t * volatile stats, int level)
409 int volatile rv = 0;
410 struct mime_handler mh;
411 struct str outrest, inrest;
412 char *cp;
413 char const * volatile tmpname = NULL;
414 size_t linelen, cnt;
415 int volatile dostat, term_infd;
416 int c;
417 struct mimepart * volatile np;
418 FILE * volatile ibuf = NULL, * volatile pbuf = obuf,
419 * volatile qbuf = obuf, *origobuf = obuf;
420 enum conversion volatile convert;
421 sighandler_type volatile oldpipe = SIG_DFL;
422 NYD_ENTER;
424 n_UNINIT(term_infd, 0);
425 n_UNINIT(cnt, 0);
427 quoteflt_reset(qf, obuf);
429 #if 0 /* TODO PART_INFO should be displayed here!! search PART_INFO */
430 if(ip->m_mimecontent != MIME_DISCARD && level > 0)
431 _print_part_info(obuf, ip, doitp, level, qf, stats);
432 #endif
434 if (ip->m_mimecontent == MIME_PKCS7) {
435 if (ip->m_multipart &&
436 action != SEND_MBOX && action != SEND_RFC822 && action != SEND_SHOW)
437 goto jheaders_skip;
440 dostat = 0;
441 if (level == 0 && action != SEND_TODISP_PARTS) {
442 if (doitp != NULL) {
443 if (!n_ignore_is_ign(doitp, "status", 6))
444 dostat |= 1;
445 if (!n_ignore_is_ign(doitp, "x-status", 8))
446 dostat |= 2;
447 } else
448 dostat = 3;
451 if ((ibuf = setinput(&mb, (struct message*)ip, NEED_BODY)) == NULL) {
452 rv = -1;
453 goto jleave;
456 if(action == SEND_TODISP || action == SEND_TODISP_ALL ||
457 action == SEND_TODISP_PARTS ||
458 action == SEND_QUOTE || action == SEND_QUOTE_ALL ||
459 action == SEND_TOSRCH)
460 dostat |= 4;
462 cnt = ip->m_size;
464 if (ip->m_mimecontent == MIME_DISCARD)
465 goto jheaders_skip;
467 if (!(ip->m_flag & MNOFROM))
468 while (cnt && (c = getc(ibuf)) != EOF) {
469 cnt--;
470 if (c == '\n')
471 break;
473 convert = (dostat & 4) ? CONV_FROMHDR : CONV_NONE;
475 /* Work the headers */
476 /* C99 */{
477 struct n_string hl, *hlp;
478 size_t lineno = 0;
479 bool_t hstop/*see below, hany*/;
481 hlp = n_string_creat_auto(&hl); /* TODO pool [or, v15: filter!] */
482 /* Reserve three lines, still not enough for references and DKIM etc. */
483 hlp = n_string_reserve(hlp, n_MAX(MIME_LINELEN, MIME_LINELEN_RFC2047) * 3);
485 for(hstop = /*see below hany =*/ FAL0; !hstop;){
486 size_t lcnt;
488 lcnt = cnt;
489 if(fgetline(linedat, linesize, &cnt, &linelen, ibuf, 0) == NULL)
490 break;
491 ++lineno;
492 if (linelen == 0 || (cp = *linedat)[0] == '\n')
493 /* If line is blank, we've reached end of headers */
494 break;
495 if(cp[linelen - 1] == '\n'){
496 cp[--linelen] = '\0';
497 if(linelen == 0)
498 break;
501 /* Are we in a header? */
502 if(hlp->s_len > 0){
503 if(!blankchar(*cp)){
504 fseek(ibuf, -(off_t)(lcnt - cnt), SEEK_CUR);
505 cnt = lcnt;
506 goto jhdrput;
508 goto jhdrpush;
509 }else{
510 /* Pick up the header field if we have one */
511 while((c = *cp) != ':' && !spacechar(c) && c != '\0')
512 ++cp;
513 for(;;){
514 if(!spacechar(c) || c == '\0')
515 break;
516 c = *++cp;
518 if(c != ':'){
519 /* That won't work with MIME when saving etc., before v15 */
520 if (lineno != 1)
521 /* XXX This disturbs, and may happen multiple times, and we
522 * XXX cannot heal it for multipart except for display <v15 */
523 n_err(_("Malformed message: headers and body not separated "
524 "(with empty line)\n"));
525 if(level != 0)
526 dostat &= ~(1 | 2);
527 fseek(ibuf, -(off_t)(lcnt - cnt), SEEK_CUR);
528 cnt = lcnt;
529 break;
532 cp = *linedat;
533 jhdrpush:
534 if(!(dostat & 4)){
535 hlp = n_string_push_buf(hlp, cp, (ui32_t)linelen);
536 hlp = n_string_push_c(hlp, '\n');
537 }else{
538 bool_t lblank, isblank;
540 for(lblank = FAL0, lcnt = 0; lcnt < linelen; ++cp, ++lcnt){
541 char c8;
543 c8 = *cp;
544 if(!(isblank = blankchar(c8)) || !lblank){
545 if((lblank = isblank))
546 c8 = ' ';
547 hlp = n_string_push_c(hlp, c8);
551 continue;
554 jhdrput:
555 /* If it is an ignored header, skip it */
556 *(cp = memchr(hlp->s_dat, ':', hlp->s_len)) = '\0';
557 /* C99 */{
558 size_t i;
560 i = PTR2SIZE(cp - hlp->s_dat);
561 if((doitp != NULL && n_ignore_is_ign(doitp, hlp->s_dat, i)) ||
562 !asccasecmp(hlp->s_dat, "status") ||
563 !asccasecmp(hlp->s_dat, "x-status") ||
564 (action == SEND_MBOX &&
565 (!asccasecmp(hlp->s_dat, "content-length") ||
566 !asccasecmp(hlp->s_dat, "lines")) &&
567 !ok_blook(keep_content_length)))
568 goto jhdrtrunc;
571 /* Dump it */
572 n_COLOUR(
573 if(n_COLOUR_IS_ACTIVE())
574 n_colour_put(n_COLOUR_ID_VIEW_HEADER, hlp->s_dat);
576 *cp = ':';
577 _out(hlp->s_dat, hlp->s_len, obuf, convert, action, qf, stats, NULL,NULL);
578 n_COLOUR(
579 if(n_COLOUR_IS_ACTIVE())
580 n_colour_reset();
582 if(dostat & 4)
583 putc('\n', obuf);
584 /*see below hany = TRU1;*/
586 jhdrtrunc:
587 hlp = n_string_trunc(hlp, 0);
589 hstop = TRU1;
590 if(hlp->s_len > 0)
591 goto jhdrput;
593 /* We've reached end of headers, so eventually force out status: field and
594 * note that we are no longer in header fields */
595 if(dostat & 1){
596 statusput(zmp, obuf, qf, stats);
597 /*see below hany = TRU1;*/
599 if(dostat & 2){
600 xstatusput(zmp, obuf, qf, stats);
601 /*see below hany = TRU1;*/
603 if(/* TODO PART_INFO hany && */ doitp != n_IGNORE_ALL)
604 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
605 } /* C99 */
607 quoteflt_flush(qf);
609 if(ferror(obuf)){
610 rv = -1;
611 goto jleave;
614 jheaders_skip:
615 memset(&mh, 0, sizeof mh);
617 switch (ip->m_mimecontent) {
618 case MIME_822:
619 switch (action) {
620 case SEND_TODISP_PARTS:
621 goto jleave;
622 case SEND_TODISP:
623 case SEND_TODISP_ALL:
624 case SEND_QUOTE:
625 case SEND_QUOTE_ALL:
626 if (ok_blook(rfc822_body_from_)) {
627 if (qf->qf_pfix_len > 0) {
628 size_t i = fwrite(qf->qf_pfix, sizeof *qf->qf_pfix,
629 qf->qf_pfix_len, obuf);
630 if (i == qf->qf_pfix_len && stats != NULL)
631 *stats += i;
633 put_from_(obuf, ip->m_multipart, stats);
635 /* FALLTHRU */
636 case SEND_TOSRCH:
637 case SEND_DECRYPT:
638 goto jmulti;
639 case SEND_TOFILE:
640 case SEND_TOPIPE:
641 put_from_(obuf, ip->m_multipart, stats);
642 /* FALLTHRU */
643 case SEND_MBOX:
644 case SEND_RFC822:
645 case SEND_SHOW:
646 break;
648 break;
649 case MIME_TEXT_HTML:
650 case MIME_TEXT:
651 case MIME_TEXT_PLAIN:
652 switch (action) {
653 case SEND_TODISP:
654 case SEND_TODISP_ALL:
655 case SEND_TODISP_PARTS:
656 case SEND_QUOTE:
657 case SEND_QUOTE_ALL:
658 switch (n_mimetype_handler(&mh, ip, action)) {
659 case MIME_HDL_NULL:
660 if(action != SEND_TODISP_PARTS)
661 break;
662 /* FALLTHRU */
663 case MIME_HDL_MSG:/* TODO these should be part of partinfo! */
664 if(mh.mh_msg.l > 0)
665 _out(mh.mh_msg.s, mh.mh_msg.l, obuf, CONV_NONE, SEND_MBOX,
666 qf, stats, NULL, NULL);
667 /* We would print this as plain text, so better force going home */
668 goto jleave;
669 case MIME_HDL_CMD:
670 if(action == SEND_TODISP_PARTS &&
671 (mh.mh_flags & MIME_HDL_COPIOUSOUTPUT))
672 goto jleave;
673 break;
674 case MIME_HDL_TEXT:
675 case MIME_HDL_PTF:
676 if(action == SEND_TODISP_PARTS)
677 goto jleave;
678 break;
679 default:
680 break;
682 /* FALLTRHU */
683 default:
684 break;
686 break;
687 case MIME_DISCARD:
688 if (action != SEND_DECRYPT)
689 goto jleave;
690 break;
691 case MIME_PKCS7:
692 if (action != SEND_MBOX && action != SEND_RFC822 &&
693 action != SEND_SHOW && ip->m_multipart != NULL)
694 goto jmulti;
695 /* FALLTHRU */
696 default:
697 switch (action) {
698 case SEND_TODISP:
699 case SEND_TODISP_ALL:
700 case SEND_TODISP_PARTS:
701 case SEND_QUOTE:
702 case SEND_QUOTE_ALL:
703 switch (n_mimetype_handler(&mh, ip, action)) {
704 default:
705 case MIME_HDL_NULL:
706 if (action != SEND_TODISP && action != SEND_TODISP_ALL &&
707 (level != 0 || cnt))
708 goto jleave;
709 /* FALLTHRU */
710 case MIME_HDL_MSG:/* TODO these should be part of partinfo! */
711 if(mh.mh_msg.l > 0)
712 _out(mh.mh_msg.s, mh.mh_msg.l, obuf, CONV_NONE, SEND_MBOX,
713 qf, stats, NULL, NULL);
714 /* We would print this as plain text, so better force going home */
715 goto jleave;
716 case MIME_HDL_CMD:
717 if(action == SEND_TODISP_PARTS){
718 if(mh.mh_flags & MIME_HDL_COPIOUSOUTPUT)
719 goto jleave;
720 else{
721 _print_part_info(obuf, ip, doitp, level, qf, stats);
722 if(!getapproval(_("Run MIME handler for this part?"), FAL0))
723 goto jleave;
726 break;
727 case MIME_HDL_TEXT:
728 case MIME_HDL_PTF:
729 if(action == SEND_TODISP_PARTS)
730 goto jleave;
731 break;
733 break;
734 case SEND_TOFILE:
735 case SEND_TOPIPE:
736 case SEND_TOSRCH:
737 case SEND_DECRYPT:
738 case SEND_MBOX:
739 case SEND_RFC822:
740 case SEND_SHOW:
741 break;
743 break;
744 case MIME_ALTERNATIVE:
745 if ((action == SEND_TODISP || action == SEND_QUOTE) &&
746 !ok_blook(print_alternatives)) {
747 /* XXX This (a) should not remain (b) should be own fun
748 * TODO (despite the fact that v15 will do this completely differently
749 * TODO by having an action-specific "manager" that will traverse the
750 * TODO parsed MIME tree and decide for each part whether it'll be
751 * TODO displayed or not *before* we walk the tree for doing action */
752 struct mpstack {
753 struct mpstack *outer;
754 struct mimepart *mp;
755 } outermost, * volatile curr, * volatile mpsp;
756 bool_t volatile neednl, hadpart;
757 struct n_sigman smalter;
759 (curr = &outermost)->outer = NULL;
760 curr->mp = ip;
761 neednl = hadpart = FAL0;
763 n_SIGMAN_ENTER_SWITCH(&smalter, n_SIGMAN_ALL) {
764 case 0:
765 break;
766 default:
767 rv = -1;
768 goto jalter_leave;
771 for (np = ip->m_multipart;;) {
772 jalter_redo:
773 for (; np != NULL; np = np->m_nextpart) {
774 if (action != SEND_QUOTE && np->m_ct_type_plain != NULL) {
775 if (neednl)
776 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats,
777 NULL, NULL);
778 _print_part_info(obuf, np, doitp, level, qf, stats);
780 neednl = TRU1;
782 switch (np->m_mimecontent) {
783 case MIME_ALTERNATIVE:
784 case MIME_RELATED:
785 case MIME_DIGEST:
786 case MIME_SIGNED:
787 case MIME_ENCRYPTED:
788 case MIME_MULTI:
789 mpsp = salloc(sizeof *mpsp);
790 mpsp->outer = curr;
791 mpsp->mp = np->m_multipart;
792 curr->mp = np;
793 curr = mpsp;
794 np = mpsp->mp;
795 neednl = FAL0;
796 goto jalter_redo;
797 default:
798 if (hadpart)
799 break;
800 switch (n_mimetype_handler(&mh, np, action)) {
801 default:
802 mh.mh_flags = MIME_HDL_NULL;
803 continue; /* break; break; */
804 case MIME_HDL_CMD:
805 if(!(mh.mh_flags & MIME_HDL_COPIOUSOUTPUT)){
806 mh.mh_flags = MIME_HDL_NULL;
807 continue; /* break; break; */
809 /* FALLTHRU */
810 case MIME_HDL_PTF:
811 if (!ok_blook(mime_alternative_favour_rich)) {/* TODO */
812 struct mimepart *x = np;
814 while ((x = x->m_nextpart) != NULL) {
815 struct mime_handler mhx;
817 if (x->m_mimecontent == MIME_TEXT_PLAIN ||
818 n_mimetype_handler(&mhx, x, action) ==
819 MIME_HDL_TEXT)
820 break;
822 if (x != NULL)
823 continue; /* break; break; */
824 goto jalter_plain;
826 /* FALLTHRU */
827 case MIME_HDL_TEXT:
828 break;
830 /* FALLTHRU */
831 case MIME_TEXT_PLAIN:
832 if (hadpart)
833 break;
834 if (ok_blook(mime_alternative_favour_rich)) { /* TODO */
835 struct mimepart *x = np;
837 /* TODO twice TODO, we should dive into /related and
838 * TODO check whether that has rich parts! */
839 while ((x = x->m_nextpart) != NULL) {
840 struct mime_handler mhx;
842 switch (n_mimetype_handler(&mhx, x, action)) {
843 case MIME_HDL_CMD:
844 if(!(mhx.mh_flags & MIME_HDL_COPIOUSOUTPUT))
845 continue;
846 /* FALLTHRU */
847 case MIME_HDL_PTF:
848 break;
849 default:
850 continue;
852 break;
854 if (x != NULL)
855 continue; /* break; break; */
857 jalter_plain:
858 quoteflt_flush(qf);
859 if (action == SEND_QUOTE && hadpart) {
860 struct quoteflt *dummy = quoteflt_dummy();
861 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, dummy, stats,
862 NULL,NULL);
863 quoteflt_flush(dummy);
865 hadpart = TRU1;
866 neednl = FAL0;
867 rv = sendpart(zmp, np, obuf, doitp, qf, action,
868 linedat, linesize, stats, level + 1);
869 quoteflt_reset(qf, origobuf);
871 if (rv < 0)
872 curr = &outermost; /* Cause overall loop termination */
873 break;
877 mpsp = curr->outer;
878 if (mpsp == NULL)
879 break;
880 curr = mpsp;
881 np = curr->mp->m_nextpart;
883 jalter_leave:
884 n_sigman_leave(&smalter, n_SIGMAN_VIPSIGS_NTTYOUT);
885 goto jleave;
887 /* FALLTHRU */
888 case MIME_RELATED:
889 case MIME_DIGEST:
890 case MIME_SIGNED:
891 case MIME_ENCRYPTED:
892 case MIME_MULTI:
893 switch (action) {
894 case SEND_TODISP:
895 case SEND_TODISP_ALL:
896 case SEND_TODISP_PARTS:
897 case SEND_QUOTE:
898 case SEND_QUOTE_ALL:
899 case SEND_TOFILE:
900 case SEND_TOPIPE:
901 case SEND_TOSRCH:
902 case SEND_DECRYPT:
903 jmulti:
904 if ((action == SEND_TODISP || action == SEND_TODISP_ALL) &&
905 ip->m_multipart != NULL &&
906 ip->m_multipart->m_mimecontent == MIME_DISCARD &&
907 ip->m_multipart->m_nextpart == NULL) {
908 char const *x = _("[Missing multipart boundary - use `show' "
909 "to display the raw message]\n");
910 _out(x, strlen(x), obuf, CONV_NONE, SEND_MBOX, qf, stats,
911 NULL,NULL);
914 for (np = ip->m_multipart; np != NULL; np = np->m_nextpart) {
915 bool_t volatile ispipe;
917 if (np->m_mimecontent == MIME_DISCARD && action != SEND_DECRYPT)
918 continue;
920 ispipe = FAL0;
921 switch (action) {
922 case SEND_TOFILE:
923 if (np->m_partstring &&
924 np->m_partstring[0] == '1' && np->m_partstring[1] == '\0')
925 break;
926 stats = NULL;
927 /* TODO Always open multipart on /dev/null, it's a hack to be
928 * TODO able to dive into that structure, and still better
929 * TODO than asking the user for something stupid.
930 * TODO oh, wait, we did ask for a filename for this MIME mail,
931 * TODO and that outer container is useless anyway ;-P */
932 if (np->m_multipart != NULL && np->m_mimecontent != MIME_822) {
933 if ((obuf = Fopen(n_path_devnull, "w")) == NULL)
934 continue;
935 } else if ((obuf = newfile(np, &ispipe)) == NULL)
936 continue;
937 if (!ispipe)
938 break;
939 if (sigsetjmp(_send_pipejmp, 1)) {
940 rv = -1;
941 goto jpipe_close;
943 oldpipe = safe_signal(SIGPIPE, &_send_onpipe);
944 break;
945 case SEND_TODISP:
946 case SEND_TODISP_ALL:
947 if (ip->m_mimecontent != MIME_ALTERNATIVE &&
948 ip->m_mimecontent != MIME_RELATED &&
949 ip->m_mimecontent != MIME_DIGEST &&
950 ip->m_mimecontent != MIME_SIGNED &&
951 ip->m_mimecontent != MIME_ENCRYPTED &&
952 ip->m_mimecontent != MIME_MULTI)
953 break;
954 _print_part_info(obuf, np, doitp, level, qf, stats);
955 break;
956 case SEND_TODISP_PARTS:
957 case SEND_QUOTE:
958 case SEND_QUOTE_ALL:
959 case SEND_MBOX:
960 case SEND_RFC822:
961 case SEND_SHOW:
962 case SEND_TOSRCH:
963 case SEND_DECRYPT:
964 case SEND_TOPIPE:
965 break;
968 quoteflt_flush(qf);
969 if ((action == SEND_QUOTE || action == SEND_QUOTE_ALL) &&
970 np->m_multipart == NULL && ip->m_parent != NULL) {
971 struct quoteflt *dummy = quoteflt_dummy();
972 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, dummy, stats,
973 NULL,NULL);
974 quoteflt_flush(dummy);
976 if (sendpart(zmp, np, obuf, doitp, qf, action, linedat, linesize,
977 stats, level+1) < 0)
978 rv = -1;
979 quoteflt_reset(qf, origobuf);
981 if (action == SEND_QUOTE) {
982 if (ip->m_mimecontent != MIME_RELATED)
983 break;
985 if (action == SEND_TOFILE && obuf != origobuf) {
986 if (!ispipe)
987 Fclose(obuf);
988 else {
989 jpipe_close:
990 safe_signal(SIGPIPE, SIG_IGN);
991 Pclose(obuf, TRU1);
992 safe_signal(SIGPIPE, oldpipe);
996 goto jleave;
997 case SEND_MBOX:
998 case SEND_RFC822:
999 case SEND_SHOW:
1000 break;
1002 break;
1005 /* Copy out message body */
1006 if (doitp == n_IGNORE_ALL && level == 0) /* skip final blank line */
1007 --cnt;
1008 switch (ip->m_mime_enc) {
1009 case MIMEE_BIN:
1010 case MIMEE_7B:
1011 case MIMEE_8B:
1012 convert = CONV_NONE;
1013 break;
1014 case MIMEE_QP:
1015 convert = CONV_FROMQP;
1016 break;
1017 case MIMEE_B64:
1018 switch (ip->m_mimecontent) {
1019 case MIME_TEXT:
1020 case MIME_TEXT_PLAIN:
1021 case MIME_TEXT_HTML:
1022 convert = CONV_FROMB64_T;
1023 break;
1024 default:
1025 switch (mh.mh_flags & MIME_HDL_TYPE_MASK) {
1026 case MIME_HDL_TEXT:
1027 case MIME_HDL_PTF:
1028 convert = CONV_FROMB64_T;
1029 break;
1030 default:
1031 convert = CONV_FROMB64;
1032 break;
1034 break;
1036 break;
1037 default:
1038 convert = CONV_NONE;
1041 /* TODO Unless we have filters, ensure iconvd==-1 so that mime.c:fwrite_td()
1042 * TODO cannot mess things up misusing outrest as line buffer */
1043 #ifdef HAVE_ICONV
1044 if (iconvd != (iconv_t)-1) {
1045 n_iconv_close(iconvd);
1046 iconvd = (iconv_t)-1;
1048 #endif
1050 if (action == SEND_DECRYPT || action == SEND_MBOX ||
1051 action == SEND_RFC822 || action == SEND_SHOW)
1052 convert = CONV_NONE;
1053 #ifdef HAVE_ICONV
1054 else if ((action == SEND_TODISP || action == SEND_TODISP_ALL ||
1055 action == SEND_TODISP_PARTS ||
1056 action == SEND_QUOTE || action == SEND_QUOTE_ALL ||
1057 action == SEND_TOSRCH || action == SEND_TOFILE) &&
1058 (ip->m_mimecontent == MIME_TEXT_PLAIN ||
1059 ip->m_mimecontent == MIME_TEXT_HTML ||
1060 ip->m_mimecontent == MIME_TEXT ||
1061 (mh.mh_flags & MIME_HDL_TYPE_MASK) == MIME_HDL_TEXT ||
1062 (mh.mh_flags & MIME_HDL_TYPE_MASK) == MIME_HDL_PTF)) {
1063 char const *tcs;
1065 tcs = ok_vlook(ttycharset);
1066 if (asccasecmp(tcs, ip->m_charset) &&
1067 asccasecmp(ok_vlook(charset_7bit), ip->m_charset)) {
1068 iconvd = n_iconv_open(tcs, ip->m_charset);
1069 if (iconvd == (iconv_t)-1 && n_err_no == n_ERR_INVAL) {
1070 n_err(_("Cannot convert from %s to %s\n"), ip->m_charset, tcs);
1071 /*rv = 1; goto jleave;*/
1075 #endif
1077 switch (mh.mh_flags & MIME_HDL_TYPE_MASK) {
1078 case MIME_HDL_CMD:
1079 if(!(mh.mh_flags & MIME_HDL_COPIOUSOUTPUT) &&
1080 action != SEND_TODISP_PARTS)
1081 goto jmhp_default;
1082 /* FALLTHRU */
1083 case MIME_HDL_PTF:
1084 tmpname = NULL;
1085 qbuf = obuf;
1087 term_infd = n_CHILD_FD_PASS;
1088 if (mh.mh_flags & (MIME_HDL_TMPF | MIME_HDL_NEEDSTERM)) {
1089 enum oflags of;
1091 of = OF_RDWR | OF_REGISTER;
1092 if (!(mh.mh_flags & MIME_HDL_TMPF)) {
1093 term_infd = 0;
1094 mh.mh_flags |= MIME_HDL_TMPF_FILL;
1095 of |= OF_UNLINK;
1096 } else if (mh.mh_flags & MIME_HDL_TMPF_UNLINK)
1097 of |= OF_REGISTER_UNLINK;
1099 if ((pbuf = Ftmp((mh.mh_flags & MIME_HDL_TMPF ? &cp : NULL),
1100 (mh.mh_flags & MIME_HDL_TMPF_FILL ? "mimehdlfill" : "mimehdl"),
1101 of)) == NULL)
1102 goto jesend;
1104 if (mh.mh_flags & MIME_HDL_TMPF) {
1105 tmpname = savestr(cp);
1106 Ftmp_free(&cp);
1109 if (mh.mh_flags & MIME_HDL_TMPF_FILL) {
1110 if (term_infd == 0)
1111 term_infd = fileno(pbuf);
1112 goto jsend;
1116 jpipe_for_real:
1117 pbuf = _pipefile(&mh, ip, n_UNVOLATILE(&qbuf), tmpname, term_infd);
1118 if (pbuf == NULL) {
1119 jesend:
1120 pbuf = qbuf = NULL;
1121 rv = -1;
1122 goto jend;
1123 } else if ((mh.mh_flags & MIME_HDL_NEEDSTERM) && pbuf == (FILE*)-1) {
1124 pbuf = qbuf = NULL;
1125 goto jend;
1127 tmpname = NULL;
1128 action = SEND_TOPIPE;
1129 if (pbuf != qbuf) {
1130 oldpipe = safe_signal(SIGPIPE, &_send_onpipe);
1131 if (sigsetjmp(_send_pipejmp, 1))
1132 goto jend;
1134 break;
1136 default:
1137 jmhp_default:
1138 mh.mh_flags = MIME_HDL_NULL;
1139 pbuf = qbuf = obuf;
1140 break;
1143 jsend:
1145 bool_t volatile eof;
1146 ui32_t save_qf_pfix_len = qf->qf_pfix_len;
1147 ui64_t *save_stats = stats;
1149 if (pbuf != origobuf) {
1150 qf->qf_pfix_len = 0; /* XXX legacy (remove filter instead) */
1151 stats = NULL;
1153 eof = FAL0;
1154 outrest.s = inrest.s = NULL;
1155 outrest.l = inrest.l = 0;
1157 if (pbuf == qbuf) {
1158 __sendp_sig = 0;
1159 __sendp_opipe = safe_signal(SIGPIPE, &__sendp_onsig);
1160 if (sigsetjmp(__sendp_actjmp, 1)) {
1161 n_pstate &= ~n_PS_BASE64_STRIP_CR;/* (but protected by outer sigman) */
1162 if (outrest.s != NULL)
1163 free(outrest.s);
1164 if (inrest.s != NULL)
1165 free(inrest.s);
1166 #ifdef HAVE_ICONV
1167 if (iconvd != (iconv_t)-1)
1168 n_iconv_close(iconvd);
1169 #endif
1170 safe_signal(SIGPIPE, __sendp_opipe);
1171 n_raise(__sendp_sig);
1175 quoteflt_reset(qf, pbuf);
1176 if(dostat & 4)
1177 n_pstate |= n_PS_BASE64_STRIP_CR;
1178 while (!eof && fgetline(linedat, linesize, &cnt, &linelen, ibuf, 0)) {
1179 joutln:
1180 if (_out(*linedat, linelen, pbuf, convert, action, qf, stats, &outrest,
1181 (action & _TD_EOF ? NULL : &inrest)) < 0 || ferror(pbuf)) {
1182 rv = -1; /* XXX Should bail away?! */
1183 break;
1186 if(eof <= FAL0 && rv >= 0 && (outrest.l != 0 || inrest.l != 0)){
1187 linelen = 0;
1188 if(eof || inrest.l == 0)
1189 action |= _TD_EOF;
1190 eof = eof ? TRU1 : TRUM1;
1191 goto joutln;
1193 n_pstate &= ~n_PS_BASE64_STRIP_CR;
1194 action &= ~_TD_EOF;
1196 /* TODO HACK: when sending to the display we yet get fooled if a message
1197 * TODO doesn't end in a newline, because of our input/output 1:1.
1198 * TODO This should be handled automatically by a display filter, then */
1199 if(rv >= 0 && !qf->qf_nl_last &&
1200 (action == SEND_TODISP || action == SEND_TODISP_ALL))
1201 rv = quoteflt_push(qf, "\n", 1);
1203 quoteflt_flush(qf);
1205 if (rv >= 0 && (mh.mh_flags & MIME_HDL_TMPF_FILL)) {
1206 mh.mh_flags &= ~MIME_HDL_TMPF_FILL;
1207 fflush(pbuf);
1208 really_rewind(pbuf);
1209 /* Don't Fclose() the Ftmp() thing due to OF_REGISTER_UNLINK++ */
1210 goto jpipe_for_real;
1213 if (pbuf == qbuf)
1214 safe_signal(SIGPIPE, __sendp_opipe);
1216 if (outrest.s != NULL)
1217 free(outrest.s);
1218 if (inrest.s != NULL)
1219 free(inrest.s);
1221 if (pbuf != origobuf) {
1222 qf->qf_pfix_len = save_qf_pfix_len;
1223 stats = save_stats;
1227 jend:
1228 if (pbuf != qbuf) {
1229 safe_signal(SIGPIPE, SIG_IGN);
1230 Pclose(pbuf, !(mh.mh_flags & MIME_HDL_ASYNC));
1231 safe_signal(SIGPIPE, oldpipe);
1232 if (rv >= 0 && qbuf != NULL && qbuf != obuf)
1233 pipecpy(qbuf, obuf, origobuf, qf, stats);
1235 #ifdef HAVE_ICONV
1236 if (iconvd != (iconv_t)-1)
1237 n_iconv_close(iconvd);
1238 #endif
1239 jleave:
1240 NYD_LEAVE;
1241 return rv;
1244 static FILE *
1245 newfile(struct mimepart *ip, bool_t volatile *ispipe)
1247 struct str in, out;
1248 char *f;
1249 FILE *fp;
1250 NYD_ENTER;
1252 f = ip->m_filename;
1253 *ispipe = FAL0;
1255 if (f != NULL && f != (char*)-1) {
1256 in.s = f;
1257 in.l = strlen(f);
1258 makeprint(&in, &out);
1259 out.l = delctrl(out.s, out.l);
1260 f = savestrbuf(out.s, out.l);
1261 free(out.s);
1264 /* In interactive mode, let user perform all kind of expansions as desired,
1265 * and offer |SHELL-SPEC pipe targets, too */
1266 if (n_psonce & n_PSO_INTERACTIVE) {
1267 struct str prompt;
1268 struct n_string shou, *shoup;
1269 char *f2, *f3;
1271 shoup = n_string_creat_auto(&shou);
1273 /* TODO Generic function which asks for filename.
1274 * TODO If the current part is the first textpart the target
1275 * TODO is implicit from outer `write' etc! */
1276 /* I18N: Filename input prompt with file type indication */
1277 str_concat_csvl(&prompt, _("Enter filename for part "),
1278 (ip->m_partstring != NULL ? ip->m_partstring : n_qm),
1279 " (", ip->m_ct_type_plain, "): ", NULL);
1280 jgetname:
1281 f2 = n_go_input_cp(n_GO_INPUT_CTX_DEFAULT | n_GO_INPUT_HIST_ADD,
1282 prompt.s, ((f != (char*)-1 && f != NULL)
1283 ? n_shexp_quote_cp(f, FAL0) : NULL));
1284 if(f2 != NULL){
1285 in.s = n_UNCONST(f2);
1286 in.l = UIZ_MAX;
1287 if((n_shexp_parse_token((n_SHEXP_PARSE_TRUNC |
1288 n_SHEXP_PARSE_TRIM_SPACE | n_SHEXP_PARSE_TRIM_IFSSPACE |
1289 n_SHEXP_PARSE_LOG | n_SHEXP_PARSE_IGNORE_EMPTY),
1290 shoup, &in, NULL
1291 ) & (n_SHEXP_STATE_STOP |
1292 n_SHEXP_STATE_OUTPUT | n_SHEXP_STATE_ERR_MASK)
1293 ) != (n_SHEXP_STATE_STOP | n_SHEXP_STATE_OUTPUT))
1294 goto jgetname;
1295 f2 = n_string_cp(shoup);
1297 if (f2 == NULL || *f2 == '\0') {
1298 if (n_poption & n_PO_D_V)
1299 n_err(_("... skipping this\n"));
1300 n_string_gut(shoup);
1301 fp = NULL;
1302 goto jleave;
1305 if (*f2 == '|')
1306 /* Pipes are expanded by the shell */
1307 f = f2;
1308 else if ((f3 = fexpand(f2, FEXP_LOCAL | FEXP_NVAR)) == NULL)
1309 /* (Error message written by fexpand()) */
1310 goto jgetname;
1311 else
1312 f = f3;
1314 n_string_gut(shoup);
1317 if (f == NULL || f == (char*)-1 || *f == '\0')
1318 fp = NULL;
1319 else if (n_psonce & n_PSO_INTERACTIVE) {
1320 if (*f == '|') {
1321 fp = Popen(&f[1], "w", ok_vlook(SHELL), NULL, 1);
1322 if (!(*ispipe = (fp != NULL)))
1323 n_perr(f, 0);
1324 } else if ((fp = Fopen(f, "w")) == NULL)
1325 n_err(_("Cannot open %s\n"), n_shexp_quote_cp(f, FAL0));
1326 } else {
1327 /* Be very picky in non-interactive mode: actively disallow pipes,
1328 * prevent directory separators, and any filename member that would
1329 * become expanded by the shell if the name would be echo(1)ed */
1330 if(anyof(f, "/" n_SHEXP_MAGIC_PATH_CHARS)){
1331 char c;
1333 for(out.s = salloc((strlen(f) * 3) +1), out.l = 0; (c = *f++) != '\0';)
1334 if(strchr("/" n_SHEXP_MAGIC_PATH_CHARS, c)){
1335 out.s[out.l++] = '%';
1336 n_c_to_hex_base16(&out.s[out.l], c);
1337 out.l += 2;
1338 }else
1339 out.s[out.l++] = c;
1340 out.s[out.l] = '\0';
1341 f = out.s;
1344 /* Avoid overwriting of existing files */
1345 while((fp = Fopen(f, "wx")) == NULL){
1346 int e;
1348 if((e = n_err_no) != n_ERR_EXIST){
1349 n_err(_("Cannot open %s: %s\n"),
1350 n_shexp_quote_cp(f, FAL0), n_err_to_doc(e));
1351 break;
1354 if(ip->m_partstring != NULL)
1355 f = savecatsep(f, '#', ip->m_partstring);
1356 else
1357 f = savecat(f, "#.");
1360 jleave:
1361 NYD_LEAVE;
1362 return fp;
1365 static void
1366 pipecpy(FILE *pipebuf, FILE *outbuf, FILE *origobuf, struct quoteflt *qf,
1367 ui64_t *stats)
1369 char *line = NULL; /* TODO line pool */
1370 size_t linesize = 0, linelen, cnt;
1371 ssize_t all_sz, sz;
1372 NYD_ENTER;
1374 fflush(pipebuf);
1375 rewind(pipebuf);
1376 cnt = (size_t)fsize(pipebuf);
1377 all_sz = 0;
1379 quoteflt_reset(qf, outbuf);
1380 while (fgetline(&line, &linesize, &cnt, &linelen, pipebuf, 0) != NULL) {
1381 if ((sz = quoteflt_push(qf, line, linelen)) < 0)
1382 break;
1383 all_sz += sz;
1385 if ((sz = quoteflt_flush(qf)) > 0)
1386 all_sz += sz;
1387 if (line)
1388 free(line);
1390 if (all_sz > 0 && outbuf == origobuf && stats != NULL)
1391 *stats += all_sz;
1392 Fclose(pipebuf);
1393 NYD_LEAVE;
1396 static void
1397 statusput(const struct message *mp, FILE *obuf, struct quoteflt *qf,
1398 ui64_t *stats)
1400 char statout[3], *cp = statout;
1401 NYD_ENTER;
1403 if (mp->m_flag & MREAD)
1404 *cp++ = 'R';
1405 if (!(mp->m_flag & MNEW))
1406 *cp++ = 'O';
1407 *cp = 0;
1408 if (statout[0]) {
1409 int i = fprintf(obuf, "%.*sStatus: %s\n", (int)qf->qf_pfix_len,
1410 (qf->qf_pfix_len > 0 ? qf->qf_pfix : 0), statout);
1411 if (i > 0 && stats != NULL)
1412 *stats += i;
1414 NYD_LEAVE;
1417 static void
1418 xstatusput(const struct message *mp, FILE *obuf, struct quoteflt *qf,
1419 ui64_t *stats)
1421 char xstatout[4];
1422 char *xp = xstatout;
1423 NYD_ENTER;
1425 if (mp->m_flag & MFLAGGED)
1426 *xp++ = 'F';
1427 if (mp->m_flag & MANSWERED)
1428 *xp++ = 'A';
1429 if (mp->m_flag & MDRAFTED)
1430 *xp++ = 'T';
1431 *xp = 0;
1432 if (xstatout[0]) {
1433 int i = fprintf(obuf, "%.*sX-Status: %s\n", (int)qf->qf_pfix_len,
1434 (qf->qf_pfix_len > 0 ? qf->qf_pfix : 0), xstatout);
1435 if (i > 0 && stats != NULL)
1436 *stats += i;
1438 NYD_LEAVE;
1441 static void
1442 put_from_(FILE *fp, struct mimepart *ip, ui64_t *stats)
1444 char const *froma, *date, *nl;
1445 int i;
1446 NYD_ENTER;
1448 if (ip != NULL && ip->m_from != NULL) {
1449 froma = ip->m_from;
1450 date = fakedate(ip->m_time);
1451 nl = "\n";
1452 } else {
1453 froma = ok_vlook(LOGNAME);
1454 date = time_current.tc_ctime;
1455 nl = n_empty;
1458 n_COLOUR(
1459 if(n_COLOUR_IS_ACTIVE())
1460 n_colour_put(n_COLOUR_ID_VIEW_FROM_, NULL);
1462 i = fprintf(fp, "From %s %s%s", froma, date, nl);
1463 n_COLOUR(
1464 if(n_COLOUR_IS_ACTIVE())
1465 n_colour_reset();
1467 if (i > 0 && stats != NULL)
1468 *stats += i;
1469 NYD_LEAVE;
1472 FL int
1473 sendmp(struct message *mp, FILE *obuf, struct n_ignore const *doitp,
1474 char const *prefix, enum sendaction action, ui64_t *stats)
1476 struct n_sigman linedat_protect;
1477 struct quoteflt qf;
1478 FILE *ibuf;
1479 enum mime_parse_flags mpf;
1480 struct mimepart *ip;
1481 size_t linesize, cnt, sz, i;
1482 char *linedat;
1483 int rv, c;
1484 NYD_ENTER;
1486 time_current_update(&time_current, TRU1);
1487 rv = -1;
1488 linedat = NULL;
1489 linesize = 0;
1490 quoteflt_init(&qf, prefix);
1492 n_SIGMAN_ENTER_SWITCH(&linedat_protect, n_SIGMAN_ALL){
1493 case 0:
1494 break;
1495 default:
1496 goto jleave;
1499 if (mp == dot && action != SEND_TOSRCH)
1500 n_pstate |= n_PS_DID_PRINT_DOT;
1501 if (stats != NULL)
1502 *stats = 0;
1504 /* First line is the From_ line, so no headers there to worry about */
1505 if ((ibuf = setinput(&mb, mp, NEED_BODY)) == NULL)
1506 goto jleave;
1508 cnt = mp->m_size;
1509 sz = 0;
1511 bool_t nozap;
1512 char const *cpre = n_empty, *csuf = n_empty;
1514 #ifdef HAVE_COLOUR
1515 if(n_COLOUR_IS_ACTIVE()){
1516 struct n_colour_pen *cpen;
1517 struct str const *sp;
1519 cpen = n_colour_pen_create(n_COLOUR_ID_VIEW_FROM_,NULL);
1520 if((sp = n_colour_pen_to_str(cpen)) != NULL){
1521 cpre = sp->s;
1522 sp = n_colour_reset_to_str();
1523 if(sp != NULL)
1524 csuf = sp->s;
1527 #endif
1529 nozap = (doitp != n_IGNORE_ALL && doitp != n_IGNORE_FWD &&
1530 action != SEND_RFC822 &&
1531 !n_ignore_is_ign(doitp, "from_", sizeof("from_") -1));
1532 if (mp->m_flag & MNOFROM) {
1533 if (nozap)
1534 sz = fprintf(obuf, "%s%.*sFrom %s %s%s\n",
1535 cpre, (int)qf.qf_pfix_len,
1536 (qf.qf_pfix_len != 0 ? qf.qf_pfix : n_empty), fakefrom(mp),
1537 fakedate(mp->m_time), csuf);
1538 } else if (nozap) {
1539 if (qf.qf_pfix_len > 0) {
1540 i = fwrite(qf.qf_pfix, sizeof *qf.qf_pfix, qf.qf_pfix_len, obuf);
1541 if (i != qf.qf_pfix_len)
1542 goto jleave;
1543 sz += i;
1545 #ifdef HAVE_COLOUR
1546 if(*cpre != '\0'){
1547 fputs(cpre, obuf);
1548 cpre = (char const*)0x1;
1550 #endif
1552 while (cnt > 0 && (c = getc(ibuf)) != EOF) {
1553 #ifdef HAVE_COLOUR
1554 if(c == '\n' && *csuf != '\0'){
1555 cpre = (char const*)0x1;
1556 fputs(csuf, obuf);
1558 #endif
1559 putc(c, obuf);
1560 ++sz;
1561 --cnt;
1562 if (c == '\n')
1563 break;
1566 #ifdef HAVE_COLOUR
1567 if(*csuf != '\0' && cpre != (char const*)0x1 && *cpre != '\0')
1568 fputs(csuf, obuf);
1569 #endif
1570 } else {
1571 while (cnt > 0 && (c = getc(ibuf)) != EOF) {
1572 --cnt;
1573 if (c == '\n')
1574 break;
1578 if (sz > 0 && stats != NULL)
1579 *stats += sz;
1581 mpf = MIME_PARSE_NONE;
1582 if (action != SEND_MBOX && action != SEND_RFC822 && action != SEND_SHOW)
1583 mpf |= MIME_PARSE_PARTS | MIME_PARSE_DECRYPT;
1584 if ((ip = mime_parse_msg(mp, mpf)) == NULL)
1585 goto jleave;
1587 rv = sendpart(mp, ip, obuf, doitp, &qf, action, &linedat, &linesize,
1588 stats, 0);
1590 n_sigman_cleanup_ping(&linedat_protect);
1591 jleave:
1592 n_pstate &= ~n_PS_BASE64_STRIP_CR;
1593 quoteflt_destroy(&qf);
1594 if(linedat != NULL)
1595 free(linedat);
1596 NYD_LEAVE;
1597 n_sigman_leave(&linedat_protect, n_SIGMAN_VIPSIGS_NTTYOUT);
1598 return rv;
1601 /* s-it-mode */