(BWDIC!) `if'+: add operator modifiers; change defaults: case-sensitive!
[s-mailx.git] / send.c
blobe090c59ab2172b50dec98b90670ff0e106662e19
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 if(needsep)
150 _out(", ", 2, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
151 if (to.l > 25 && !asccasecmp(cp, "quoted-printable"))
152 cp = "qu.-pr.";
153 _out(cp, strlen(cp), obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
154 needsep = TRU1;
157 if (want_ct && mpp->m_multipart == NULL/* TODO */ &&
158 (cp = mpp->m_charset) != NULL) {
159 if(needsep)
160 _out(", ", 2, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
161 _out(cp, strlen(cp), obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
164 _out(" --]", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
165 if (csuf != NULL)
166 _out(csuf->s, csuf->l, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
167 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
169 /* */
170 if (mpp->m_content_info & CI_MIME_ERRORS) {
171 if (cpre != NULL)
172 _out(cpre->s, cpre->l, obuf, CONV_NONE, SEND_MBOX, qf, stats,
173 NULL, NULL);
174 _out("[-- ", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
176 ti.l = strlen(ti.s = n_UNCONST(_("Defective MIME structure")));
177 makeprint(&ti, &to);
178 to.l = delctrl(to.s, to.l);
179 _out(to.s, to.l, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
180 free(to.s);
182 _out(" --]", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
183 if (csuf != NULL)
184 _out(csuf->s, csuf->l, obuf, CONV_NONE, SEND_MBOX, qf, stats,
185 NULL, NULL);
186 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
189 /* Content-Description */
190 if (n_ignore_is_ign(doitp, "content-description", 19) &&
191 (cp = mpp->m_content_description) != NULL && *cp != '\0') {
192 if (cpre != NULL)
193 _out(cpre->s, cpre->l, obuf, CONV_NONE, SEND_MBOX, qf, stats,
194 NULL, NULL);
195 _out("[-- ", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
197 ti.l = strlen(ti.s = n_UNCONST(mpp->m_content_description));
198 mime_fromhdr(&ti, &to, TD_ISPR | TD_ICONV);
199 _out(to.s, to.l, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
200 free(to.s);
202 _out(" --]", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
203 if (csuf != NULL)
204 _out(csuf->s, csuf->l, obuf, CONV_NONE, SEND_MBOX, qf, stats,
205 NULL, NULL);
206 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
209 /* Filename */
210 if (n_ignore_is_ign(doitp, "content-disposition", 19) &&
211 mpp->m_filename != NULL && *mpp->m_filename != '\0') {
212 if (cpre != NULL)
213 _out(cpre->s, cpre->l, obuf, CONV_NONE, SEND_MBOX, qf, stats,
214 NULL, NULL);
215 _out("[-- ", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
217 ti.l = strlen(ti.s = mpp->m_filename);
218 makeprint(&ti, &to);
219 to.l = delctrl(to.s, to.l);
220 _out(to.s, to.l, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
221 free(to.s);
223 _out(" --]", 4, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
224 if (csuf != NULL)
225 _out(csuf->s, csuf->l, obuf, CONV_NONE, SEND_MBOX, qf, stats,
226 NULL, NULL);
227 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL, NULL);
229 NYD2_LEAVE;
232 static FILE *
233 _pipefile(struct mime_handler *mhp, struct mimepart const *mpp, FILE **qbuf,
234 char const *tmpname, int term_infd)
236 struct str s;
237 char const *env_addon[8 +8/*v15*/], *cp, *sh;
238 FILE *rbuf;
239 NYD_ENTER;
241 rbuf = *qbuf;
243 if (mhp->mh_flags & MIME_HDL_ISQUOTE) {
244 if ((*qbuf = Ftmp(NULL, "sendp", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
245 NULL) {
246 n_perr(_("tmpfile"), 0);
247 *qbuf = rbuf;
251 if ((mhp->mh_flags & MIME_HDL_TYPE_MASK) == MIME_HDL_PTF) {
252 union {int (*ptf)(void); char const *sh;} u;
254 fflush(*qbuf);
255 if (*qbuf != n_stdout) /* xxx never? v15: it'll be a filter anyway */
256 fflush(n_stdout);
258 u.ptf = mhp->mh_ptf;
259 if((rbuf = Popen((char*)-1, "W", u.sh, NULL, fileno(*qbuf))) == NULL)
260 goto jerror;
261 goto jleave;
264 /* MAILX_FILENAME */
265 if (mpp == NULL || (cp = mpp->m_filename) == NULL)
266 cp = n_empty;
267 env_addon[0] = str_concat_csvl(&s, n_PIPEENV_FILENAME, "=", cp, NULL)->s;
268 env_addon[1] = str_concat_csvl(&s, "NAIL_FILENAME", "=", cp, NULL)->s;
270 /* MAILX_FILENAME_GENERATED *//* TODO pathconf NAME_MAX; but user can create
271 * TODO a file wherever he wants! *Do* create a zero-size temporary file
272 * TODO and give *that* path as MAILX_FILENAME_TEMPORARY, clean it up once
273 * TODO the pipe returns? Like this we *can* verify path/name issues! */
274 cp = n_random_create_cp(n_MIN(NAME_MAX / 4, 16), NULL);
275 env_addon[2] = str_concat_csvl(&s, n_PIPEENV_FILENAME_GENERATED, "=", cp,
276 NULL)->s;
277 env_addon[3] = str_concat_csvl(&s, "NAIL_FILENAME_GENERATED", "=", cp,
278 NULL)->s;
280 /* MAILX_CONTENT{,_EVIDENCE} */
281 if (mpp == NULL || (cp = mpp->m_ct_type_plain) == NULL)
282 cp = n_empty;
283 env_addon[4] = str_concat_csvl(&s, n_PIPEENV_CONTENT, "=", cp, NULL)->s;
284 env_addon[5] = str_concat_csvl(&s, "NAIL_CONTENT", "=", cp, NULL)->s;
286 if (mpp != NULL && mpp->m_ct_type_usr_ovwr != NULL)
287 cp = mpp->m_ct_type_usr_ovwr;
288 env_addon[6] = str_concat_csvl(&s, n_PIPEENV_CONTENT_EVIDENCE, "=", cp,
289 NULL)->s;
290 env_addon[7] = str_concat_csvl(&s, "NAIL_CONTENT_EVIDENCE", "=", cp,
291 NULL)->s;
293 env_addon[8] = NULL;
295 /* MAILX_FILENAME_TEMPORARY? */
296 if (tmpname != NULL) {
297 env_addon[8] = str_concat_csvl(&s,
298 n_PIPEENV_FILENAME_TEMPORARY, "=", tmpname, NULL)->s;
299 env_addon[9] = str_concat_csvl(&s,
300 "NAIL_FILENAME_TEMPORARY", "=", tmpname, NULL)->s;
301 env_addon[10] = NULL;
304 sh = ok_vlook(SHELL);
306 if (mhp->mh_flags & MIME_HDL_NEEDSTERM) {
307 sigset_t nset;
308 int pid;
310 sigemptyset(&nset);
311 pid = n_child_run(sh, &nset, term_infd, n_CHILD_FD_PASS, "-c",
312 mhp->mh_shell_cmd, NULL, env_addon, NULL);
313 rbuf = (pid < 0) ? NULL : (FILE*)-1;
314 } else {
315 rbuf = Popen(mhp->mh_shell_cmd, "W", sh, env_addon,
316 (mhp->mh_flags & MIME_HDL_ASYNC ? -1 : fileno(*qbuf)));
317 jerror:
318 if (rbuf == NULL)
319 n_err(_("Cannot run MIME type handler: %s: %s\n"),
320 mhp->mh_msg, n_err_to_doc(n_err_no));
321 else {
322 fflush(*qbuf);
323 if (*qbuf != n_stdout)
324 fflush(n_stdout);
327 jleave:
328 NYD_LEAVE;
329 return rbuf;
332 SINLINE ssize_t
333 _out(char const *buf, size_t len, FILE *fp, enum conversion convert, enum
334 sendaction action, struct quoteflt *qf, ui64_t *stats, struct str *outrest,
335 struct str *inrest)
337 ssize_t sz = 0, n;
338 int flags;
339 NYD_ENTER;
341 /* TODO We should not need is_head() here, i think in v15 the actual Mailbox
342 * TODO subclass should detect such From_ cases and either reencode the part
343 * TODO in question, or perform From_ quoting as necessary!?!?!? How?!? */
344 /* C99 */{
345 bool_t from_;
347 if((action == SEND_MBOX || action == SEND_DECRYPT) &&
348 (from_ = is_head(buf, len, TRU1))){
349 if(from_ != TRUM1 || ok_blook(mbox_rfc4155)){
350 putc('>', fp);
351 ++sz;
356 flags = ((int)action & _TD_EOF);
357 action &= ~_TD_EOF;
358 n = mime_write(buf, len, fp,
359 action == SEND_MBOX ? CONV_NONE : convert,
360 flags | ((action == SEND_TODISP || action == SEND_TODISP_ALL ||
361 action == SEND_QUOTE || action == SEND_QUOTE_ALL)
362 ? TD_ISPR | TD_ICONV
363 : (action == SEND_TOSRCH || action == SEND_TOPIPE ||
364 action == SEND_TOFILE)
365 ? TD_ICONV : (action == SEND_SHOW ? TD_ISPR : TD_NONE)),
366 qf, outrest, inrest);
367 if (n < 0)
368 sz = n;
369 else if (n > 0) {
370 sz += n;
371 if (stats != NULL)
372 *stats += sz;
374 NYD_LEAVE;
375 return sz;
378 static void
379 _send_onpipe(int signo)
381 NYD_X; /* Signal handler */
382 n_UNUSED(signo);
383 siglongjmp(_send_pipejmp, 1);
386 static sigjmp_buf __sendp_actjmp; /* TODO someday.. */
387 static int __sendp_sig; /* TODO someday.. */
388 static sighandler_type __sendp_opipe;
389 static void
390 __sendp_onsig(int sig) /* TODO someday, we won't need it no more */
392 NYD_X; /* Signal handler */
393 __sendp_sig = sig;
394 siglongjmp(__sendp_actjmp, 1);
397 static int
398 sendpart(struct message *zmp, struct mimepart *ip, FILE * volatile obuf,
399 struct n_ignore const *doitp, struct quoteflt *qf,
400 enum sendaction volatile action,
401 char **linedat, size_t *linesize, ui64_t * volatile stats, int level)
403 int volatile rv = 0;
404 struct mime_handler mh;
405 struct str outrest, inrest;
406 char *cp;
407 char const * volatile tmpname = NULL;
408 size_t linelen, cnt;
409 int volatile term_infd;
410 int dostat, c;
411 struct mimepart *volatile np;
412 FILE * volatile ibuf = NULL, * volatile pbuf = obuf,
413 * volatile qbuf = obuf, *origobuf = obuf;
414 enum conversion volatile convert;
415 sighandler_type volatile oldpipe = SIG_DFL;
416 NYD_ENTER;
418 n_UNINIT(term_infd, 0);
419 n_UNINIT(cnt, 0);
421 quoteflt_reset(qf, obuf);
423 if (ip->m_mimecontent == MIME_PKCS7) {
424 if (ip->m_multipart &&
425 action != SEND_MBOX && action != SEND_RFC822 && action != SEND_SHOW)
426 goto jheaders_skip;
429 dostat = 0;
430 if (level == 0) {
431 if (doitp != NULL) {
432 if (!n_ignore_is_ign(doitp, "status", 6))
433 dostat |= 1;
434 if (!n_ignore_is_ign(doitp, "x-status", 8))
435 dostat |= 2;
436 } else
437 dostat = 3;
439 if ((ibuf = setinput(&mb, (struct message*)ip, NEED_BODY)) == NULL) {
440 rv = -1;
441 goto jleave;
443 cnt = ip->m_size;
445 if (ip->m_mimecontent == MIME_DISCARD)
446 goto jheaders_skip;
448 if (!(ip->m_flag & MNOFROM))
449 while (cnt && (c = getc(ibuf)) != EOF) {
450 cnt--;
451 if (c == '\n')
452 break;
454 convert = (action == SEND_TODISP || action == SEND_TODISP_ALL ||
455 action == SEND_QUOTE || action == SEND_QUOTE_ALL ||
456 action == SEND_TOSRCH)
457 ? CONV_FROMHDR : CONV_NONE;
459 /* Work the headers */
460 /* C99 */{
461 struct n_string hl, *hlp;
462 size_t lineno = 0;
463 bool_t hstop;
465 hlp = n_string_creat_auto(&hl); /* TODO pool [or, v15: filter!] */
466 /* Reserve three lines, still not enough for references and DKIM etc. */
467 hlp = n_string_reserve(hlp, n_MAX(MIME_LINELEN, MIME_LINELEN_RFC2047) * 3);
469 for(hstop = FAL0; !hstop;){
470 size_t lcnt;
472 lcnt = cnt;
473 if(fgetline(linedat, linesize, &cnt, &linelen, ibuf, 0) == NULL)
474 break;
475 ++lineno;
476 if (linelen == 0 || (cp = *linedat)[0] == '\n')
477 /* If line is blank, we've reached end of headers */
478 break;
479 if(cp[linelen - 1] == '\n'){
480 cp[--linelen] = '\0';
481 if(linelen == 0)
482 break;
485 /* Are we in a header? */
486 if(hlp->s_len > 0){
487 if(!blankchar(*cp)){
488 fseek(ibuf, -(off_t)(lcnt - cnt), SEEK_CUR);
489 cnt = lcnt;
490 goto jhdrput;
492 goto jhdrpush;
493 }else{
494 /* Pick up the header field if we have one */
495 while((c = *cp) != ':' && !spacechar(c) && c != '\0')
496 ++cp;
497 for(;;){
498 if(!spacechar(c) || c == '\0')
499 break;
500 c = *++cp;
502 if(c != ':'){
503 /* That won't work with MIME when saving etc., before v15 */
504 if (lineno != 1)
505 /* XXX This disturbs, and may happen multiple times, and we
506 * XXX cannot heal it for multipart except for display <v15 */
507 n_err(_("Malformed message: headers and body not separated "
508 "(with empty line)\n"));
509 if(level != 0)
510 dostat &= ~(1 | 2);
511 fseek(ibuf, -(off_t)(lcnt - cnt), SEEK_CUR);
512 cnt = lcnt;
513 break;
516 cp = *linedat;
517 jhdrpush:
518 if(convert == CONV_NONE){
519 hlp = n_string_push_buf(hlp, cp, (ui32_t)linelen);
520 hlp = n_string_push_c(hlp, '\n');
521 }else{
522 bool_t lblank, isblank;
524 for(lblank = FAL0, lcnt = 0; lcnt < linelen; ++cp, ++lcnt){
525 char c8;
527 c8 = *cp;
528 if(!(isblank = blankchar(c8)) || !lblank){
529 if((lblank = isblank))
530 c8 = ' ';
531 hlp = n_string_push_c(hlp, c8);
535 continue;
538 jhdrput:
539 /* If it is an ignored header, skip it */
540 *(cp = memchr(hlp->s_dat, ':', hlp->s_len)) = '\0';
541 /* C99 */{
542 size_t i;
544 i = PTR2SIZE(cp - hlp->s_dat);
545 if((doitp != NULL && n_ignore_is_ign(doitp, hlp->s_dat, i)) ||
546 !asccasecmp(hlp->s_dat, "status") ||
547 !asccasecmp(hlp->s_dat, "x-status") ||
548 (action == SEND_MBOX &&
549 (!asccasecmp(hlp->s_dat, "content-length") ||
550 !asccasecmp(hlp->s_dat, "lines")) &&
551 !ok_blook(keep_content_length)))
552 goto jhdrtrunc;
555 /* Dump it */
556 n_COLOUR(
557 if(n_COLOUR_IS_ACTIVE())
558 n_colour_put(n_COLOUR_ID_VIEW_HEADER, hlp->s_dat);
560 *cp = ':';
561 _out(hlp->s_dat, hlp->s_len, obuf, convert, action, qf, stats, NULL,NULL);
562 n_COLOUR(
563 if(n_COLOUR_IS_ACTIVE())
564 n_colour_reset();
566 if(convert != CONV_NONE)
567 putc('\n', obuf);
569 jhdrtrunc:
570 hlp = n_string_trunc(hlp, 0);
572 hstop = TRU1;
573 if(hlp->s_len > 0)
574 goto jhdrput;
576 /* We've reached end of headers, so eventually force out status: field and
577 * note that we are no longer in header fields */
578 if(dostat & 1)
579 statusput(zmp, obuf, qf, stats);
580 if(dostat & 2)
581 xstatusput(zmp, obuf, qf, stats);
582 if(doitp != n_IGNORE_ALL)
583 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats, NULL,NULL);
584 } /* C99 */
586 quoteflt_flush(qf);
588 if(ferror(obuf)){
589 rv = -1;
590 goto jleave;
593 jheaders_skip:
594 memset(&mh, 0, sizeof mh);
596 switch (ip->m_mimecontent) {
597 case MIME_822:
598 switch (action) {
599 case SEND_TODISP:
600 case SEND_TODISP_ALL:
601 case SEND_QUOTE:
602 case SEND_QUOTE_ALL:
603 if (ok_blook(rfc822_body_from_)) {
604 if (qf->qf_pfix_len > 0) {
605 size_t i = fwrite(qf->qf_pfix, sizeof *qf->qf_pfix,
606 qf->qf_pfix_len, obuf);
607 if (i == qf->qf_pfix_len && stats != NULL)
608 *stats += i;
610 put_from_(obuf, ip->m_multipart, stats);
612 /* FALLTHRU */
613 case SEND_TOSRCH:
614 case SEND_DECRYPT:
615 goto jmulti;
616 case SEND_TOFILE:
617 case SEND_TOPIPE:
618 if (ok_blook(rfc822_body_from_))
619 put_from_(obuf, ip->m_multipart, stats);
620 /* FALLTHRU */
621 case SEND_MBOX:
622 case SEND_RFC822:
623 case SEND_SHOW:
624 break;
626 break;
627 case MIME_TEXT_HTML:
628 case MIME_TEXT:
629 case MIME_TEXT_PLAIN:
630 switch (action) {
631 case SEND_TODISP:
632 case SEND_TODISP_ALL:
633 case SEND_QUOTE:
634 case SEND_QUOTE_ALL:
635 switch (n_mimetype_handler(&mh, ip, action)) {
636 case MIME_HDL_MSG:
637 _out(mh.mh_msg.s, mh.mh_msg.l, obuf, CONV_NONE, SEND_MBOX, qf,
638 stats, NULL, NULL);
639 /* We would print this as plain text, so better force going home */
640 goto jleave;
641 default:
642 break;
644 /* FALLTRHU */
645 default:
646 break;
648 break;
649 case MIME_DISCARD:
650 if (action != SEND_DECRYPT)
651 goto jleave;
652 break;
653 case MIME_PKCS7:
654 if (action != SEND_MBOX && action != SEND_RFC822 &&
655 action != SEND_SHOW && ip->m_multipart != NULL)
656 goto jmulti;
657 /* FALLTHRU */
658 default:
659 switch (action) {
660 case SEND_TODISP:
661 case SEND_TODISP_ALL:
662 case SEND_QUOTE:
663 case SEND_QUOTE_ALL:
664 switch (n_mimetype_handler(&mh, ip, action)) {
665 case MIME_HDL_MSG:
666 _out(mh.mh_msg.s, mh.mh_msg.l, obuf, CONV_NONE, SEND_MBOX, qf,
667 stats, NULL, NULL);
668 /* We would print this as plain text, so better force going home */
669 goto jleave;
670 case MIME_HDL_CMD:
671 /* FIXME WE NEED TO DO THAT IF WE ARE THE ONLY MAIL
672 * FIXME CONTENT !! */
673 case MIME_HDL_TEXT:
674 break;
675 default:
676 case MIME_HDL_NULL:
677 if (level == 0 && cnt) {
678 char const *x = _("[-- Binary content --]\n");
679 _out(x, strlen(x), obuf, CONV_NONE, SEND_MBOX, qf, stats,
680 NULL,NULL);
682 goto jleave;
684 break;
685 case SEND_TOFILE:
686 case SEND_TOPIPE:
687 case SEND_TOSRCH:
688 case SEND_DECRYPT:
689 case SEND_MBOX:
690 case SEND_RFC822:
691 case SEND_SHOW:
692 break;
694 break;
695 case MIME_ALTERNATIVE:
696 if ((action == SEND_TODISP || action == SEND_QUOTE) &&
697 !ok_blook(print_alternatives)) {
698 /* XXX This (a) should not remain (b) should be own fun
699 * TODO (despite the fact that v15 will do this completely differently
700 * TODO by having an action-specific "manager" that will traverse the
701 * TODO parsed MIME tree and decide for each part whether it'll be
702 * TODO displayed or not *before* we walk the tree for doing action */
703 struct mpstack {
704 struct mpstack *outer;
705 struct mimepart *mp;
706 } outermost, * volatile curr, * volatile mpsp;
707 bool_t volatile neednl, hadpart;
708 struct n_sigman smalter;
710 (curr = &outermost)->outer = NULL;
711 curr->mp = ip;
712 neednl = hadpart = FAL0;
714 n_SIGMAN_ENTER_SWITCH(&smalter, n_SIGMAN_ALL) {
715 case 0:
716 break;
717 default:
718 rv = -1;
719 goto jalter_leave;
722 for (np = ip->m_multipart;;) {
723 jalter_redo:
724 for (; np != NULL; np = np->m_nextpart) {
725 if (action != SEND_QUOTE && np->m_ct_type_plain != NULL) {
726 if (neednl)
727 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats,
728 NULL, NULL);
729 _print_part_info(obuf, np, doitp, level, qf, stats);
731 neednl = TRU1;
733 switch (np->m_mimecontent) {
734 case MIME_ALTERNATIVE:
735 case MIME_RELATED:
736 case MIME_DIGEST:
737 case MIME_SIGNED:
738 case MIME_ENCRYPTED:
739 case MIME_MULTI:
740 mpsp = salloc(sizeof *mpsp);
741 mpsp->outer = curr;
742 mpsp->mp = np->m_multipart;
743 curr->mp = np;
744 curr = mpsp;
745 np = mpsp->mp;
746 neednl = FAL0;
747 goto jalter_redo;
748 default:
749 if (hadpart)
750 break;
751 switch (n_mimetype_handler(&mh, np, action)) {
752 default:
753 mh.mh_flags = MIME_HDL_NULL;
754 continue; /* break; break; */
755 case MIME_HDL_PTF:
756 if (!ok_blook(mime_alternative_favour_rich)) {/* TODO */
757 struct mimepart *x = np;
759 while ((x = x->m_nextpart) != NULL) {
760 struct mime_handler mhx;
762 if (x->m_mimecontent == MIME_TEXT_PLAIN ||
763 n_mimetype_handler(&mhx, x, action) ==
764 MIME_HDL_TEXT)
765 break;
767 if (x != NULL)
768 continue; /* break; break; */
769 goto jalter_plain;
771 /* FALLTHRU */
772 case MIME_HDL_TEXT:
773 break;
775 /* FALLTHRU */
776 case MIME_TEXT_PLAIN:
777 if (hadpart)
778 break;
779 if (ok_blook(mime_alternative_favour_rich)) { /* TODO */
780 struct mimepart *x = np;
782 /* TODO twice TODO, we should dive into /related and
783 * TODO check whether that has rich parts! */
784 while ((x = x->m_nextpart) != NULL) {
785 struct mime_handler mhx;
787 switch (n_mimetype_handler(&mhx, x, action)) {
788 case MIME_HDL_PTF:
789 break;
790 default:
791 continue;
793 break;
795 if (x != NULL)
796 continue; /* break; break; */
798 jalter_plain:
799 quoteflt_flush(qf);
800 if (action == SEND_QUOTE && hadpart) {
801 struct quoteflt *dummy = quoteflt_dummy();
802 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, dummy, stats,
803 NULL,NULL);
804 quoteflt_flush(dummy);
806 hadpart = TRU1;
807 neednl = FAL0;
808 rv = sendpart(zmp, np, obuf, doitp, qf, action,
809 linedat, linesize, stats, level + 1);
810 quoteflt_reset(qf, origobuf);
812 if (rv < 0)
813 curr = &outermost; /* Cause overall loop termination */
814 break;
818 mpsp = curr->outer;
819 if (mpsp == NULL)
820 break;
821 curr = mpsp;
822 np = curr->mp->m_nextpart;
824 jalter_leave:
825 n_sigman_leave(&smalter, n_SIGMAN_VIPSIGS_NTTYOUT);
826 goto jleave;
828 /* FALLTHRU */
829 case MIME_RELATED:
830 case MIME_DIGEST:
831 case MIME_SIGNED:
832 case MIME_ENCRYPTED:
833 case MIME_MULTI:
834 switch (action) {
835 case SEND_TODISP:
836 case SEND_TODISP_ALL:
837 case SEND_QUOTE:
838 case SEND_QUOTE_ALL:
839 case SEND_TOFILE:
840 case SEND_TOPIPE:
841 case SEND_TOSRCH:
842 case SEND_DECRYPT:
843 jmulti:
844 if ((action == SEND_TODISP || action == SEND_TODISP_ALL) &&
845 ip->m_multipart != NULL &&
846 ip->m_multipart->m_mimecontent == MIME_DISCARD &&
847 ip->m_multipart->m_nextpart == NULL) {
848 char const *x = _("[Missing multipart boundary - use show "
849 "to display the raw message]\n");
850 _out(x, strlen(x), obuf, CONV_NONE, SEND_MBOX, qf, stats,
851 NULL,NULL);
854 for (np = ip->m_multipart; np != NULL; np = np->m_nextpart) {
855 bool_t volatile ispipe;
857 if (np->m_mimecontent == MIME_DISCARD && action != SEND_DECRYPT)
858 continue;
860 ispipe = FAL0;
861 switch (action) {
862 case SEND_TOFILE:
863 if (np->m_partstring &&
864 np->m_partstring[0] == '1' && np->m_partstring[1] == '\0')
865 break;
866 stats = NULL;
867 /* TODO Always open multipart on /dev/null, it's a hack to be
868 * TODO able to dive into that structure, and still better
869 * TODO than asking the user for something stupid.
870 * TODO oh, wait, we did ask for a filename for this MIME mail,
871 * TODO and that outer container is useless anyway ;-P */
872 if (np->m_multipart != NULL) {
873 if ((obuf = Fopen(n_path_devnull, "w")) == NULL)
874 continue;
875 } else if ((obuf = newfile(np, &ispipe)) == NULL)
876 continue;
877 if (!ispipe)
878 break;
879 if (sigsetjmp(_send_pipejmp, 1)) {
880 rv = -1;
881 goto jpipe_close;
883 oldpipe = safe_signal(SIGPIPE, &_send_onpipe);
884 break;
885 case SEND_TODISP:
886 case SEND_TODISP_ALL:
887 if (ip->m_mimecontent != MIME_ALTERNATIVE &&
888 ip->m_mimecontent != MIME_RELATED &&
889 ip->m_mimecontent != MIME_DIGEST &&
890 ip->m_mimecontent != MIME_SIGNED &&
891 ip->m_mimecontent != MIME_ENCRYPTED &&
892 ip->m_mimecontent != MIME_MULTI)
893 break;
894 _print_part_info(obuf, np, doitp, level, qf, stats);
895 break;
896 case SEND_QUOTE:
897 case SEND_QUOTE_ALL:
898 case SEND_MBOX:
899 case SEND_RFC822:
900 case SEND_SHOW:
901 case SEND_TOSRCH:
902 case SEND_DECRYPT:
903 case SEND_TOPIPE:
904 break;
907 quoteflt_flush(qf);
908 if ((action == SEND_QUOTE || action == SEND_QUOTE_ALL) &&
909 np->m_multipart == NULL && ip->m_parent != NULL) {
910 struct quoteflt *dummy = quoteflt_dummy();
911 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, dummy, stats,
912 NULL,NULL);
913 quoteflt_flush(dummy);
915 if (sendpart(zmp, np, obuf, doitp, qf, action, linedat, linesize,
916 stats, level+1) < 0)
917 rv = -1;
918 quoteflt_reset(qf, origobuf);
920 if (action == SEND_QUOTE) {
921 if (ip->m_mimecontent != MIME_RELATED)
922 break;
924 if (action == SEND_TOFILE && obuf != origobuf) {
925 if (!ispipe)
926 Fclose(obuf);
927 else {
928 jpipe_close:
929 safe_signal(SIGPIPE, SIG_IGN);
930 Pclose(obuf, TRU1);
931 safe_signal(SIGPIPE, oldpipe);
935 goto jleave;
936 case SEND_MBOX:
937 case SEND_RFC822:
938 case SEND_SHOW:
939 break;
941 break;
944 /* Copy out message body */
945 if (doitp == n_IGNORE_ALL && level == 0) /* skip final blank line */
946 --cnt;
947 switch (ip->m_mime_enc) {
948 case MIMEE_BIN:
949 case MIMEE_7B:
950 case MIMEE_8B:
951 convert = CONV_NONE;
952 break;
953 case MIMEE_QP:
954 convert = CONV_FROMQP;
955 break;
956 case MIMEE_B64:
957 switch (ip->m_mimecontent) {
958 case MIME_TEXT:
959 case MIME_TEXT_PLAIN:
960 case MIME_TEXT_HTML:
961 convert = CONV_FROMB64_T;
962 break;
963 default:
964 switch (mh.mh_flags & MIME_HDL_TYPE_MASK) {
965 case MIME_HDL_TEXT:
966 case MIME_HDL_PTF:
967 convert = CONV_FROMB64_T;
968 break;
969 default:
970 convert = CONV_FROMB64;
971 break;
973 break;
975 break;
976 default:
977 convert = CONV_NONE;
980 /* TODO Unless we have filters, ensure iconvd==-1 so that mime.c:fwrite_td()
981 * TODO cannot mess things up misusing outrest as line buffer */
982 #ifdef HAVE_ICONV
983 if (iconvd != (iconv_t)-1) {
984 n_iconv_close(iconvd);
985 iconvd = (iconv_t)-1;
987 #endif
989 if (action == SEND_DECRYPT || action == SEND_MBOX ||
990 action == SEND_RFC822 || action == SEND_SHOW)
991 convert = CONV_NONE;
992 #ifdef HAVE_ICONV
993 else if ((action == SEND_TODISP || action == SEND_TODISP_ALL ||
994 action == SEND_QUOTE || action == SEND_QUOTE_ALL ||
995 action == SEND_TOSRCH || action == SEND_TOFILE) &&
996 (ip->m_mimecontent == MIME_TEXT_PLAIN ||
997 ip->m_mimecontent == MIME_TEXT_HTML ||
998 ip->m_mimecontent == MIME_TEXT ||
999 (mh.mh_flags & MIME_HDL_TYPE_MASK) == MIME_HDL_TEXT ||
1000 (mh.mh_flags & MIME_HDL_TYPE_MASK) == MIME_HDL_PTF)) {
1001 char const *tcs;
1003 tcs = ok_vlook(ttycharset);
1004 if (asccasecmp(tcs, ip->m_charset) &&
1005 asccasecmp(ok_vlook(charset_7bit), ip->m_charset)) {
1006 iconvd = n_iconv_open(tcs, ip->m_charset);
1007 if (iconvd == (iconv_t)-1 && n_err_no == n_ERR_INVAL) {
1008 n_err(_("Cannot convert from %s to %s\n"), ip->m_charset, tcs);
1009 /*rv = 1; goto jleave;*/
1013 #endif
1015 switch (mh.mh_flags & MIME_HDL_TYPE_MASK) {
1016 case MIME_HDL_CMD:
1017 case MIME_HDL_PTF:
1018 tmpname = NULL;
1019 qbuf = obuf;
1021 term_infd = n_CHILD_FD_PASS;
1022 if (mh.mh_flags & (MIME_HDL_TMPF | MIME_HDL_NEEDSTERM)) {
1023 enum oflags of;
1025 of = OF_RDWR | OF_REGISTER;
1026 if (!(mh.mh_flags & MIME_HDL_TMPF)) {
1027 term_infd = 0;
1028 mh.mh_flags |= MIME_HDL_TMPF_FILL;
1029 of |= OF_UNLINK;
1030 } else if (mh.mh_flags & MIME_HDL_TMPF_UNLINK)
1031 of |= OF_REGISTER_UNLINK;
1033 if ((pbuf = Ftmp((mh.mh_flags & MIME_HDL_TMPF ? &cp : NULL),
1034 (mh.mh_flags & MIME_HDL_TMPF_FILL ? "mimehdlfill" : "mimehdl"),
1035 of)) == NULL)
1036 goto jesend;
1038 if (mh.mh_flags & MIME_HDL_TMPF) {
1039 tmpname = savestr(cp);
1040 Ftmp_free(&cp);
1043 if (mh.mh_flags & MIME_HDL_TMPF_FILL) {
1044 if (term_infd == 0)
1045 term_infd = fileno(pbuf);
1046 goto jsend;
1050 jpipe_for_real:
1051 pbuf = _pipefile(&mh, ip, n_UNVOLATILE(&qbuf), tmpname, term_infd);
1052 if (pbuf == NULL) {
1053 jesend:
1054 pbuf = qbuf = NULL;
1055 rv = -1;
1056 goto jend;
1057 } else if ((mh.mh_flags & MIME_HDL_NEEDSTERM) && pbuf == (FILE*)-1) {
1058 pbuf = qbuf = NULL;
1059 goto jend;
1061 tmpname = NULL;
1062 action = SEND_TOPIPE;
1063 if (pbuf != qbuf) {
1064 oldpipe = safe_signal(SIGPIPE, &_send_onpipe);
1065 if (sigsetjmp(_send_pipejmp, 1))
1066 goto jend;
1068 break;
1070 default:
1071 mh.mh_flags = MIME_HDL_NULL;
1072 pbuf = qbuf = obuf;
1073 break;
1076 jsend:
1078 bool_t volatile eof;
1079 ui32_t save_qf_pfix_len = qf->qf_pfix_len;
1080 ui64_t *save_stats = stats;
1082 if (pbuf != origobuf) {
1083 qf->qf_pfix_len = 0; /* XXX legacy (remove filter instead) */
1084 stats = NULL;
1086 eof = FAL0;
1087 outrest.s = inrest.s = NULL;
1088 outrest.l = inrest.l = 0;
1090 if (pbuf == qbuf) {
1091 __sendp_sig = 0;
1092 __sendp_opipe = safe_signal(SIGPIPE, &__sendp_onsig);
1093 if (sigsetjmp(__sendp_actjmp, 1)) {
1094 if (outrest.s != NULL)
1095 free(outrest.s);
1096 if (inrest.s != NULL)
1097 free(inrest.s);
1098 #ifdef HAVE_ICONV
1099 if (iconvd != (iconv_t)-1)
1100 n_iconv_close(iconvd);
1101 #endif
1102 safe_signal(SIGPIPE, __sendp_opipe);
1103 n_raise(__sendp_sig);
1107 quoteflt_reset(qf, pbuf);
1108 while (!eof && fgetline(linedat, linesize, &cnt, &linelen, ibuf, 0)) {
1109 joutln:
1110 if (_out(*linedat, linelen, pbuf, convert, action, qf, stats, &outrest,
1111 (action & _TD_EOF ? NULL : &inrest)) < 0 || ferror(pbuf)) {
1112 rv = -1; /* XXX Should bail away?! */
1113 break;
1116 if(eof <= FAL0 && rv >= 0 && (outrest.l != 0 || inrest.l != 0)){
1117 linelen = 0;
1118 if(eof || inrest.l == 0)
1119 action |= _TD_EOF;
1120 eof = eof ? TRU1 : TRUM1;
1121 goto joutln;
1123 action &= ~_TD_EOF;
1125 /* TODO HACK: when sending to the display we yet get fooled if a message
1126 * TODO doesn't end in a newline, because of our input/output 1:1.
1127 * TODO This should be handled automatically by a display filter, then */
1128 if(rv >= 0 && !qf->qf_nl_last &&
1129 (action == SEND_TODISP || action == SEND_TODISP_ALL))
1130 rv = quoteflt_push(qf, "\n", 1);
1132 quoteflt_flush(qf);
1134 if (rv >= 0 && (mh.mh_flags & MIME_HDL_TMPF_FILL)) {
1135 mh.mh_flags &= ~MIME_HDL_TMPF_FILL;
1136 fflush(pbuf);
1137 really_rewind(pbuf);
1138 /* Don't Fclose() the Ftmp() thing due to OF_REGISTER_UNLINK++ */
1139 goto jpipe_for_real;
1142 if (pbuf == qbuf)
1143 safe_signal(SIGPIPE, __sendp_opipe);
1145 if (outrest.s != NULL)
1146 free(outrest.s);
1147 if (inrest.s != NULL)
1148 free(inrest.s);
1150 if (pbuf != origobuf) {
1151 qf->qf_pfix_len = save_qf_pfix_len;
1152 stats = save_stats;
1156 jend:
1157 if (pbuf != qbuf) {
1158 safe_signal(SIGPIPE, SIG_IGN);
1159 Pclose(pbuf, !(mh.mh_flags & MIME_HDL_ASYNC));
1160 safe_signal(SIGPIPE, oldpipe);
1161 if (rv >= 0 && qbuf != NULL && qbuf != obuf)
1162 pipecpy(qbuf, obuf, origobuf, qf, stats);
1164 #ifdef HAVE_ICONV
1165 if (iconvd != (iconv_t)-1)
1166 n_iconv_close(iconvd);
1167 #endif
1168 jleave:
1169 NYD_LEAVE;
1170 return rv;
1173 static FILE *
1174 newfile(struct mimepart *ip, bool_t volatile *ispipe)
1176 struct str in, out;
1177 char *f;
1178 FILE *fp;
1179 NYD_ENTER;
1181 f = ip->m_filename;
1182 *ispipe = FAL0;
1184 if (f != NULL && f != (char*)-1) {
1185 in.s = f;
1186 in.l = strlen(f);
1187 makeprint(&in, &out);
1188 out.l = delctrl(out.s, out.l);
1189 f = savestrbuf(out.s, out.l);
1190 free(out.s);
1193 /* In interactive mode, let user perform all kind of expansions as desired,
1194 * and offer |SHELL-SPEC pipe targets, too */
1195 if (n_psonce & n_PSO_INTERACTIVE) {
1196 struct str prompt;
1197 struct n_string shou, *shoup;
1198 char *f2, *f3;
1200 shoup = n_string_creat_auto(&shou);
1202 /* TODO Generic function which asks for filename.
1203 * TODO If the current part is the first textpart the target
1204 * TODO is implicit from outer `write' etc! */
1205 /* I18N: Filename input prompt with file type indication */
1206 str_concat_csvl(&prompt, _("Enter filename for part "),
1207 (ip->m_partstring != NULL ? ip->m_partstring : n_qm),
1208 " (", ip->m_ct_type_plain, "): ", NULL);
1209 jgetname:
1210 f2 = n_go_input_cp(n_GO_INPUT_CTX_DEFAULT | n_GO_INPUT_HIST_ADD,
1211 prompt.s, ((f != (char*)-1 && f != NULL)
1212 ? n_shexp_quote_cp(f, FAL0) : NULL));
1213 if(f2 != NULL){
1214 in.s = n_UNCONST(f2);
1215 in.l = UIZ_MAX;
1216 if((n_shexp_parse_token((n_SHEXP_PARSE_TRUNC |
1217 n_SHEXP_PARSE_TRIM_SPACE | n_SHEXP_PARSE_TRIM_IFSSPACE |
1218 n_SHEXP_PARSE_LOG | n_SHEXP_PARSE_IGNORE_EMPTY),
1219 shoup, &in, NULL
1220 ) & (n_SHEXP_STATE_STOP |
1221 n_SHEXP_STATE_OUTPUT | n_SHEXP_STATE_ERR_MASK)
1222 ) != (n_SHEXP_STATE_STOP | n_SHEXP_STATE_OUTPUT))
1223 goto jgetname;
1224 f2 = n_string_cp(shoup);
1226 if (f2 == NULL || *f2 == '\0') {
1227 if (n_poption & n_PO_D_V)
1228 n_err(_("... skipping this\n"));
1229 n_string_gut(shoup);
1230 fp = NULL;
1231 goto jleave;
1234 if (*f2 == '|')
1235 /* Pipes are expanded by the shell */
1236 f = f2;
1237 else if ((f3 = fexpand(f2, FEXP_LOCAL | FEXP_NVAR)) == NULL)
1238 /* (Error message written by fexpand()) */
1239 goto jgetname;
1240 else
1241 f = f3;
1243 n_string_gut(shoup);
1246 if (f == NULL || f == (char*)-1 || *f == '\0')
1247 fp = NULL;
1248 else if (n_psonce & n_PSO_INTERACTIVE) {
1249 if (*f == '|') {
1250 fp = Popen(&f[1], "w", ok_vlook(SHELL), NULL, 1);
1251 if (!(*ispipe = (fp != NULL)))
1252 n_perr(f, 0);
1253 } else if ((fp = Fopen(f, "w")) == NULL)
1254 n_err(_("Cannot open %s\n"), n_shexp_quote_cp(f, FAL0));
1255 } else {
1256 /* Be very picky in non-interactive mode: actively disallow pipes,
1257 * prevent directory separators, and any filename member that would
1258 * become expanded by the shell if the name would be echo(1)ed */
1259 if(anyof(f, "/" n_SHEXP_MAGIC_PATH_CHARS)){
1260 char c;
1262 for(out.s = salloc((strlen(f) * 3) +1), out.l = 0; (c = *f++) != '\0';)
1263 if(strchr("/" n_SHEXP_MAGIC_PATH_CHARS, c)){
1264 out.s[out.l++] = '%';
1265 n_c_to_hex_base16(&out.s[out.l], c);
1266 out.l += 2;
1267 }else
1268 out.s[out.l++] = c;
1269 out.s[out.l] = '\0';
1270 f = out.s;
1273 /* Avoid overwriting of existing files */
1274 while((fp = Fopen(f, "wx")) == NULL){
1275 int e;
1277 if((e = n_err_no) != n_ERR_EXIST){
1278 n_err(_("Cannot open %s: %s\n"),
1279 n_shexp_quote_cp(f, FAL0), n_err_to_doc(e));
1280 break;
1283 if(ip->m_partstring != NULL)
1284 f = savecatsep(f, '#', ip->m_partstring);
1285 else
1286 f = savecat(f, "#.");
1289 jleave:
1290 NYD_LEAVE;
1291 return fp;
1294 static void
1295 pipecpy(FILE *pipebuf, FILE *outbuf, FILE *origobuf, struct quoteflt *qf,
1296 ui64_t *stats)
1298 char *line = NULL; /* TODO line pool */
1299 size_t linesize = 0, linelen, cnt;
1300 ssize_t all_sz, sz;
1301 NYD_ENTER;
1303 fflush(pipebuf);
1304 rewind(pipebuf);
1305 cnt = (size_t)fsize(pipebuf);
1306 all_sz = 0;
1308 quoteflt_reset(qf, outbuf);
1309 while (fgetline(&line, &linesize, &cnt, &linelen, pipebuf, 0) != NULL) {
1310 if ((sz = quoteflt_push(qf, line, linelen)) < 0)
1311 break;
1312 all_sz += sz;
1314 if ((sz = quoteflt_flush(qf)) > 0)
1315 all_sz += sz;
1316 if (line)
1317 free(line);
1319 if (all_sz > 0 && outbuf == origobuf && stats != NULL)
1320 *stats += all_sz;
1321 Fclose(pipebuf);
1322 NYD_LEAVE;
1325 static void
1326 statusput(const struct message *mp, FILE *obuf, struct quoteflt *qf,
1327 ui64_t *stats)
1329 char statout[3], *cp = statout;
1330 NYD_ENTER;
1332 if (mp->m_flag & MREAD)
1333 *cp++ = 'R';
1334 if (!(mp->m_flag & MNEW))
1335 *cp++ = 'O';
1336 *cp = 0;
1337 if (statout[0]) {
1338 int i = fprintf(obuf, "%.*sStatus: %s\n", (int)qf->qf_pfix_len,
1339 (qf->qf_pfix_len > 0 ? qf->qf_pfix : 0), statout);
1340 if (i > 0 && stats != NULL)
1341 *stats += i;
1343 NYD_LEAVE;
1346 static void
1347 xstatusput(const struct message *mp, FILE *obuf, struct quoteflt *qf,
1348 ui64_t *stats)
1350 char xstatout[4];
1351 char *xp = xstatout;
1352 NYD_ENTER;
1354 if (mp->m_flag & MFLAGGED)
1355 *xp++ = 'F';
1356 if (mp->m_flag & MANSWERED)
1357 *xp++ = 'A';
1358 if (mp->m_flag & MDRAFTED)
1359 *xp++ = 'T';
1360 *xp = 0;
1361 if (xstatout[0]) {
1362 int i = fprintf(obuf, "%.*sX-Status: %s\n", (int)qf->qf_pfix_len,
1363 (qf->qf_pfix_len > 0 ? qf->qf_pfix : 0), xstatout);
1364 if (i > 0 && stats != NULL)
1365 *stats += i;
1367 NYD_LEAVE;
1370 static void
1371 put_from_(FILE *fp, struct mimepart *ip, ui64_t *stats)
1373 char const *froma, *date, *nl;
1374 int i;
1375 NYD_ENTER;
1377 if (ip != NULL && ip->m_from != NULL) {
1378 froma = ip->m_from;
1379 date = fakedate(ip->m_time);
1380 nl = "\n";
1381 } else {
1382 froma = ok_vlook(LOGNAME);
1383 date = time_current.tc_ctime;
1384 nl = n_empty;
1387 n_COLOUR(
1388 if(n_COLOUR_IS_ACTIVE())
1389 n_colour_put(n_COLOUR_ID_VIEW_FROM_, NULL);
1391 i = fprintf(fp, "From %s %s%s", froma, date, nl);
1392 n_COLOUR(
1393 if(n_COLOUR_IS_ACTIVE())
1394 n_colour_reset();
1396 if (i > 0 && stats != NULL)
1397 *stats += i;
1398 NYD_LEAVE;
1401 FL int
1402 sendmp(struct message *mp, FILE *obuf, struct n_ignore const *doitp,
1403 char const *prefix, enum sendaction action, ui64_t *stats)
1405 struct n_sigman linedat_protect;
1406 struct quoteflt qf;
1407 FILE *ibuf;
1408 enum mime_parse_flags mpf;
1409 struct mimepart *ip;
1410 size_t linesize, cnt, sz, i;
1411 char *linedat;
1412 int rv, c;
1413 NYD_ENTER;
1415 time_current_update(&time_current, TRU1);
1416 rv = -1;
1417 linedat = NULL;
1418 linesize = 0;
1419 quoteflt_init(&qf, prefix);
1421 n_SIGMAN_ENTER_SWITCH(&linedat_protect, n_SIGMAN_ALL){
1422 case 0:
1423 break;
1424 default:
1425 goto jleave;
1428 if (mp == dot && action != SEND_TOSRCH)
1429 n_pstate |= n_PS_DID_PRINT_DOT;
1430 if (stats != NULL)
1431 *stats = 0;
1433 /* First line is the From_ line, so no headers there to worry about */
1434 if ((ibuf = setinput(&mb, mp, NEED_BODY)) == NULL)
1435 goto jleave;
1437 cnt = mp->m_size;
1438 sz = 0;
1440 bool_t nozap;
1441 char const *cpre = n_empty, *csuf = n_empty;
1443 #ifdef HAVE_COLOUR
1444 if(n_COLOUR_IS_ACTIVE()){
1445 struct n_colour_pen *cpen;
1446 struct str const *sp;
1448 cpen = n_colour_pen_create(n_COLOUR_ID_VIEW_FROM_,NULL);
1449 if((sp = n_colour_pen_to_str(cpen)) != NULL){
1450 cpre = sp->s;
1451 sp = n_colour_reset_to_str();
1452 if(sp != NULL)
1453 csuf = sp->s;
1456 #endif
1458 nozap = (doitp != n_IGNORE_ALL && doitp != n_IGNORE_FWD &&
1459 action != SEND_RFC822 &&
1460 !n_ignore_is_ign(doitp, "from_", sizeof("from_") -1));
1461 if (mp->m_flag & MNOFROM) {
1462 if (nozap)
1463 sz = fprintf(obuf, "%s%.*sFrom %s %s%s\n",
1464 cpre, (int)qf.qf_pfix_len,
1465 (qf.qf_pfix_len != 0 ? qf.qf_pfix : n_empty), fakefrom(mp),
1466 fakedate(mp->m_time), csuf);
1467 } else if (nozap) {
1468 if (qf.qf_pfix_len > 0) {
1469 i = fwrite(qf.qf_pfix, sizeof *qf.qf_pfix, qf.qf_pfix_len, obuf);
1470 if (i != qf.qf_pfix_len)
1471 goto jleave;
1472 sz += i;
1474 #ifdef HAVE_COLOUR
1475 if(*cpre != '\0'){
1476 fputs(cpre, obuf);
1477 cpre = (char const*)0x1;
1479 #endif
1481 while (cnt > 0 && (c = getc(ibuf)) != EOF) {
1482 #ifdef HAVE_COLOUR
1483 if(c == '\n' && *csuf != '\0'){
1484 cpre = (char const*)0x1;
1485 fputs(csuf, obuf);
1487 #endif
1488 putc(c, obuf);
1489 ++sz;
1490 --cnt;
1491 if (c == '\n')
1492 break;
1495 #ifdef HAVE_COLOUR
1496 if(*csuf != '\0' && cpre != (char const*)0x1 && *cpre != '\0')
1497 fputs(csuf, obuf);
1498 #endif
1499 } else {
1500 while (cnt > 0 && (c = getc(ibuf)) != EOF) {
1501 --cnt;
1502 if (c == '\n')
1503 break;
1507 if (sz > 0 && stats != NULL)
1508 *stats += sz;
1510 mpf = MIME_PARSE_NONE;
1511 if (action != SEND_MBOX && action != SEND_RFC822 && action != SEND_SHOW)
1512 mpf |= MIME_PARSE_PARTS | MIME_PARSE_DECRYPT;
1513 if ((ip = mime_parse_msg(mp, mpf)) == NULL)
1514 goto jleave;
1516 rv = sendpart(mp, ip, obuf, doitp, &qf, action, &linedat, &linesize,
1517 stats, 0);
1519 n_sigman_cleanup_ping(&linedat_protect);
1520 jleave:
1521 quoteflt_destroy(&qf);
1522 if(linedat != NULL)
1523 free(linedat);
1524 NYD_LEAVE;
1525 n_sigman_leave(&linedat_protect, n_SIGMAN_VIPSIGS_NTTYOUT);
1526 return rv;
1529 /* s-it-mode */