Documentation fixup for [5cd0f50d]
[s-mailx.git] / send.c
blobab10aa53e6a2e7c447f81fce2c936c20d512b64c
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 _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 case MIME_HDL_CMD:
669 if(action == SEND_TODISP_PARTS &&
670 (mh.mh_flags & MIME_HDL_COPIOUSOUTPUT))
671 goto jleave;
672 break;
673 case MIME_HDL_TEXT:
674 case MIME_HDL_PTF:
675 if(action == SEND_TODISP_PARTS)
676 goto jleave;
677 break;
678 default:
679 break;
681 /* FALLTRHU */
682 default:
683 break;
685 break;
686 case MIME_DISCARD:
687 if (action != SEND_DECRYPT)
688 goto jleave;
689 break;
690 case MIME_PKCS7:
691 if (action != SEND_MBOX && action != SEND_RFC822 &&
692 action != SEND_SHOW && ip->m_multipart != NULL)
693 goto jmulti;
694 /* FALLTHRU */
695 default:
696 switch (action) {
697 case SEND_TODISP:
698 case SEND_TODISP_ALL:
699 case SEND_TODISP_PARTS:
700 case SEND_QUOTE:
701 case SEND_QUOTE_ALL:
702 switch (n_mimetype_handler(&mh, ip, action)) {
703 default:
704 case MIME_HDL_NULL:
705 if (action != SEND_TODISP && action != SEND_TODISP_ALL &&
706 (level != 0 || cnt))
707 goto jleave;
708 /* FALLTHRU */
709 case MIME_HDL_MSG:/* TODO these should be part of partinfo! */
710 _out(mh.mh_msg.s, mh.mh_msg.l, obuf, CONV_NONE, SEND_MBOX, qf,
711 stats, NULL, NULL);
712 /* We would print this as plain text, so better force going home */
713 goto jleave;
714 case MIME_HDL_CMD:
715 if(action == SEND_TODISP_PARTS){
716 if(mh.mh_flags & MIME_HDL_COPIOUSOUTPUT)
717 goto jleave;
718 else{
719 _print_part_info(obuf, ip, doitp, level, qf, stats);
720 if(!getapproval(_("Run MIME handler for this part?"), FAL0))
721 goto jleave;
724 break;
725 case MIME_HDL_TEXT:
726 case MIME_HDL_PTF:
727 if(action == SEND_TODISP_PARTS)
728 goto jleave;
729 break;
731 break;
732 case SEND_TOFILE:
733 case SEND_TOPIPE:
734 case SEND_TOSRCH:
735 case SEND_DECRYPT:
736 case SEND_MBOX:
737 case SEND_RFC822:
738 case SEND_SHOW:
739 break;
741 break;
742 case MIME_ALTERNATIVE:
743 if ((action == SEND_TODISP || action == SEND_QUOTE) &&
744 !ok_blook(print_alternatives)) {
745 /* XXX This (a) should not remain (b) should be own fun
746 * TODO (despite the fact that v15 will do this completely differently
747 * TODO by having an action-specific "manager" that will traverse the
748 * TODO parsed MIME tree and decide for each part whether it'll be
749 * TODO displayed or not *before* we walk the tree for doing action */
750 struct mpstack {
751 struct mpstack *outer;
752 struct mimepart *mp;
753 } outermost, * volatile curr, * volatile mpsp;
754 bool_t volatile neednl, hadpart;
755 struct n_sigman smalter;
757 (curr = &outermost)->outer = NULL;
758 curr->mp = ip;
759 neednl = hadpart = FAL0;
761 n_SIGMAN_ENTER_SWITCH(&smalter, n_SIGMAN_ALL) {
762 case 0:
763 break;
764 default:
765 rv = -1;
766 goto jalter_leave;
769 for (np = ip->m_multipart;;) {
770 jalter_redo:
771 for (; np != NULL; np = np->m_nextpart) {
772 if (action != SEND_QUOTE && np->m_ct_type_plain != NULL) {
773 if (neednl)
774 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, qf, stats,
775 NULL, NULL);
776 _print_part_info(obuf, np, doitp, level, qf, stats);
778 neednl = TRU1;
780 switch (np->m_mimecontent) {
781 case MIME_ALTERNATIVE:
782 case MIME_RELATED:
783 case MIME_DIGEST:
784 case MIME_SIGNED:
785 case MIME_ENCRYPTED:
786 case MIME_MULTI:
787 mpsp = salloc(sizeof *mpsp);
788 mpsp->outer = curr;
789 mpsp->mp = np->m_multipart;
790 curr->mp = np;
791 curr = mpsp;
792 np = mpsp->mp;
793 neednl = FAL0;
794 goto jalter_redo;
795 default:
796 if (hadpart)
797 break;
798 switch (n_mimetype_handler(&mh, np, action)) {
799 default:
800 mh.mh_flags = MIME_HDL_NULL;
801 continue; /* break; break; */
802 case MIME_HDL_PTF:
803 if (!ok_blook(mime_alternative_favour_rich)) {/* TODO */
804 struct mimepart *x = np;
806 while ((x = x->m_nextpart) != NULL) {
807 struct mime_handler mhx;
809 if (x->m_mimecontent == MIME_TEXT_PLAIN ||
810 n_mimetype_handler(&mhx, x, action) ==
811 MIME_HDL_TEXT)
812 break;
814 if (x != NULL)
815 continue; /* break; break; */
816 goto jalter_plain;
818 /* FALLTHRU */
819 case MIME_HDL_TEXT:
820 break;
822 /* FALLTHRU */
823 case MIME_TEXT_PLAIN:
824 if (hadpart)
825 break;
826 if (ok_blook(mime_alternative_favour_rich)) { /* TODO */
827 struct mimepart *x = np;
829 /* TODO twice TODO, we should dive into /related and
830 * TODO check whether that has rich parts! */
831 while ((x = x->m_nextpart) != NULL) {
832 struct mime_handler mhx;
834 switch (n_mimetype_handler(&mhx, x, action)) {
835 case MIME_HDL_PTF:
836 break;
837 default:
838 continue;
840 break;
842 if (x != NULL)
843 continue; /* break; break; */
845 jalter_plain:
846 quoteflt_flush(qf);
847 if (action == SEND_QUOTE && hadpart) {
848 struct quoteflt *dummy = quoteflt_dummy();
849 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, dummy, stats,
850 NULL,NULL);
851 quoteflt_flush(dummy);
853 hadpart = TRU1;
854 neednl = FAL0;
855 rv = sendpart(zmp, np, obuf, doitp, qf, action,
856 linedat, linesize, stats, level + 1);
857 quoteflt_reset(qf, origobuf);
859 if (rv < 0)
860 curr = &outermost; /* Cause overall loop termination */
861 break;
865 mpsp = curr->outer;
866 if (mpsp == NULL)
867 break;
868 curr = mpsp;
869 np = curr->mp->m_nextpart;
871 jalter_leave:
872 n_sigman_leave(&smalter, n_SIGMAN_VIPSIGS_NTTYOUT);
873 goto jleave;
875 /* FALLTHRU */
876 case MIME_RELATED:
877 case MIME_DIGEST:
878 case MIME_SIGNED:
879 case MIME_ENCRYPTED:
880 case MIME_MULTI:
881 switch (action) {
882 case SEND_TODISP:
883 case SEND_TODISP_ALL:
884 case SEND_TODISP_PARTS:
885 case SEND_QUOTE:
886 case SEND_QUOTE_ALL:
887 case SEND_TOFILE:
888 case SEND_TOPIPE:
889 case SEND_TOSRCH:
890 case SEND_DECRYPT:
891 jmulti:
892 if ((action == SEND_TODISP || action == SEND_TODISP_ALL) &&
893 ip->m_multipart != NULL &&
894 ip->m_multipart->m_mimecontent == MIME_DISCARD &&
895 ip->m_multipart->m_nextpart == NULL) {
896 char const *x = _("[Missing multipart boundary - use `show' "
897 "to display the raw message]\n");
898 _out(x, strlen(x), obuf, CONV_NONE, SEND_MBOX, qf, stats,
899 NULL,NULL);
902 for (np = ip->m_multipart; np != NULL; np = np->m_nextpart) {
903 bool_t volatile ispipe;
905 if (np->m_mimecontent == MIME_DISCARD && action != SEND_DECRYPT)
906 continue;
908 ispipe = FAL0;
909 switch (action) {
910 case SEND_TOFILE:
911 if (np->m_partstring &&
912 np->m_partstring[0] == '1' && np->m_partstring[1] == '\0')
913 break;
914 stats = NULL;
915 /* TODO Always open multipart on /dev/null, it's a hack to be
916 * TODO able to dive into that structure, and still better
917 * TODO than asking the user for something stupid.
918 * TODO oh, wait, we did ask for a filename for this MIME mail,
919 * TODO and that outer container is useless anyway ;-P */
920 if (np->m_multipart != NULL && np->m_mimecontent != MIME_822) {
921 if ((obuf = Fopen(n_path_devnull, "w")) == NULL)
922 continue;
923 } else if ((obuf = newfile(np, &ispipe)) == NULL)
924 continue;
925 if (!ispipe)
926 break;
927 if (sigsetjmp(_send_pipejmp, 1)) {
928 rv = -1;
929 goto jpipe_close;
931 oldpipe = safe_signal(SIGPIPE, &_send_onpipe);
932 break;
933 case SEND_TODISP:
934 case SEND_TODISP_ALL:
935 if (ip->m_mimecontent != MIME_ALTERNATIVE &&
936 ip->m_mimecontent != MIME_RELATED &&
937 ip->m_mimecontent != MIME_DIGEST &&
938 ip->m_mimecontent != MIME_SIGNED &&
939 ip->m_mimecontent != MIME_ENCRYPTED &&
940 ip->m_mimecontent != MIME_MULTI)
941 break;
942 _print_part_info(obuf, np, doitp, level, qf, stats);
943 break;
944 case SEND_TODISP_PARTS:
945 case SEND_QUOTE:
946 case SEND_QUOTE_ALL:
947 case SEND_MBOX:
948 case SEND_RFC822:
949 case SEND_SHOW:
950 case SEND_TOSRCH:
951 case SEND_DECRYPT:
952 case SEND_TOPIPE:
953 break;
956 quoteflt_flush(qf);
957 if ((action == SEND_QUOTE || action == SEND_QUOTE_ALL) &&
958 np->m_multipart == NULL && ip->m_parent != NULL) {
959 struct quoteflt *dummy = quoteflt_dummy();
960 _out("\n", 1, obuf, CONV_NONE, SEND_MBOX, dummy, stats,
961 NULL,NULL);
962 quoteflt_flush(dummy);
964 if (sendpart(zmp, np, obuf, doitp, qf, action, linedat, linesize,
965 stats, level+1) < 0)
966 rv = -1;
967 quoteflt_reset(qf, origobuf);
969 if (action == SEND_QUOTE) {
970 if (ip->m_mimecontent != MIME_RELATED)
971 break;
973 if (action == SEND_TOFILE && obuf != origobuf) {
974 if (!ispipe)
975 Fclose(obuf);
976 else {
977 jpipe_close:
978 safe_signal(SIGPIPE, SIG_IGN);
979 Pclose(obuf, TRU1);
980 safe_signal(SIGPIPE, oldpipe);
984 goto jleave;
985 case SEND_MBOX:
986 case SEND_RFC822:
987 case SEND_SHOW:
988 break;
990 break;
993 /* Copy out message body */
994 if (doitp == n_IGNORE_ALL && level == 0) /* skip final blank line */
995 --cnt;
996 switch (ip->m_mime_enc) {
997 case MIMEE_BIN:
998 case MIMEE_7B:
999 case MIMEE_8B:
1000 convert = CONV_NONE;
1001 break;
1002 case MIMEE_QP:
1003 convert = CONV_FROMQP;
1004 break;
1005 case MIMEE_B64:
1006 switch (ip->m_mimecontent) {
1007 case MIME_TEXT:
1008 case MIME_TEXT_PLAIN:
1009 case MIME_TEXT_HTML:
1010 convert = CONV_FROMB64_T;
1011 break;
1012 default:
1013 switch (mh.mh_flags & MIME_HDL_TYPE_MASK) {
1014 case MIME_HDL_TEXT:
1015 case MIME_HDL_PTF:
1016 convert = CONV_FROMB64_T;
1017 break;
1018 default:
1019 convert = CONV_FROMB64;
1020 break;
1022 break;
1024 break;
1025 default:
1026 convert = CONV_NONE;
1029 /* TODO Unless we have filters, ensure iconvd==-1 so that mime.c:fwrite_td()
1030 * TODO cannot mess things up misusing outrest as line buffer */
1031 #ifdef HAVE_ICONV
1032 if (iconvd != (iconv_t)-1) {
1033 n_iconv_close(iconvd);
1034 iconvd = (iconv_t)-1;
1036 #endif
1038 if (action == SEND_DECRYPT || action == SEND_MBOX ||
1039 action == SEND_RFC822 || action == SEND_SHOW)
1040 convert = CONV_NONE;
1041 #ifdef HAVE_ICONV
1042 else if ((action == SEND_TODISP || action == SEND_TODISP_ALL ||
1043 action == SEND_TODISP_PARTS ||
1044 action == SEND_QUOTE || action == SEND_QUOTE_ALL ||
1045 action == SEND_TOSRCH || action == SEND_TOFILE) &&
1046 (ip->m_mimecontent == MIME_TEXT_PLAIN ||
1047 ip->m_mimecontent == MIME_TEXT_HTML ||
1048 ip->m_mimecontent == MIME_TEXT ||
1049 (mh.mh_flags & MIME_HDL_TYPE_MASK) == MIME_HDL_TEXT ||
1050 (mh.mh_flags & MIME_HDL_TYPE_MASK) == MIME_HDL_PTF)) {
1051 char const *tcs;
1053 tcs = ok_vlook(ttycharset);
1054 if (asccasecmp(tcs, ip->m_charset) &&
1055 asccasecmp(ok_vlook(charset_7bit), ip->m_charset)) {
1056 iconvd = n_iconv_open(tcs, ip->m_charset);
1057 if (iconvd == (iconv_t)-1 && n_err_no == n_ERR_INVAL) {
1058 n_err(_("Cannot convert from %s to %s\n"), ip->m_charset, tcs);
1059 /*rv = 1; goto jleave;*/
1063 #endif
1065 switch (mh.mh_flags & MIME_HDL_TYPE_MASK) {
1066 case MIME_HDL_CMD:
1067 if(!(mh.mh_flags & MIME_HDL_COPIOUSOUTPUT) &&
1068 action != SEND_TODISP_PARTS)
1069 goto jmhp_default;
1070 /* FALLTHRU */
1071 case MIME_HDL_PTF:
1072 tmpname = NULL;
1073 qbuf = obuf;
1075 term_infd = n_CHILD_FD_PASS;
1076 if (mh.mh_flags & (MIME_HDL_TMPF | MIME_HDL_NEEDSTERM)) {
1077 enum oflags of;
1079 of = OF_RDWR | OF_REGISTER;
1080 if (!(mh.mh_flags & MIME_HDL_TMPF)) {
1081 term_infd = 0;
1082 mh.mh_flags |= MIME_HDL_TMPF_FILL;
1083 of |= OF_UNLINK;
1084 } else if (mh.mh_flags & MIME_HDL_TMPF_UNLINK)
1085 of |= OF_REGISTER_UNLINK;
1087 if ((pbuf = Ftmp((mh.mh_flags & MIME_HDL_TMPF ? &cp : NULL),
1088 (mh.mh_flags & MIME_HDL_TMPF_FILL ? "mimehdlfill" : "mimehdl"),
1089 of)) == NULL)
1090 goto jesend;
1092 if (mh.mh_flags & MIME_HDL_TMPF) {
1093 tmpname = savestr(cp);
1094 Ftmp_free(&cp);
1097 if (mh.mh_flags & MIME_HDL_TMPF_FILL) {
1098 if (term_infd == 0)
1099 term_infd = fileno(pbuf);
1100 goto jsend;
1104 jpipe_for_real:
1105 pbuf = _pipefile(&mh, ip, n_UNVOLATILE(&qbuf), tmpname, term_infd);
1106 if (pbuf == NULL) {
1107 jesend:
1108 pbuf = qbuf = NULL;
1109 rv = -1;
1110 goto jend;
1111 } else if ((mh.mh_flags & MIME_HDL_NEEDSTERM) && pbuf == (FILE*)-1) {
1112 pbuf = qbuf = NULL;
1113 goto jend;
1115 tmpname = NULL;
1116 action = SEND_TOPIPE;
1117 if (pbuf != qbuf) {
1118 oldpipe = safe_signal(SIGPIPE, &_send_onpipe);
1119 if (sigsetjmp(_send_pipejmp, 1))
1120 goto jend;
1122 break;
1124 default:
1125 jmhp_default:
1126 mh.mh_flags = MIME_HDL_NULL;
1127 pbuf = qbuf = obuf;
1128 break;
1131 jsend:
1133 bool_t volatile eof;
1134 ui32_t save_qf_pfix_len = qf->qf_pfix_len;
1135 ui64_t *save_stats = stats;
1137 if (pbuf != origobuf) {
1138 qf->qf_pfix_len = 0; /* XXX legacy (remove filter instead) */
1139 stats = NULL;
1141 eof = FAL0;
1142 outrest.s = inrest.s = NULL;
1143 outrest.l = inrest.l = 0;
1145 if (pbuf == qbuf) {
1146 __sendp_sig = 0;
1147 __sendp_opipe = safe_signal(SIGPIPE, &__sendp_onsig);
1148 if (sigsetjmp(__sendp_actjmp, 1)) {
1149 n_pstate &= ~n_PS_BASE64_STRIP_CR;/* (but protected by outer sigman) */
1150 if (outrest.s != NULL)
1151 free(outrest.s);
1152 if (inrest.s != NULL)
1153 free(inrest.s);
1154 #ifdef HAVE_ICONV
1155 if (iconvd != (iconv_t)-1)
1156 n_iconv_close(iconvd);
1157 #endif
1158 safe_signal(SIGPIPE, __sendp_opipe);
1159 n_raise(__sendp_sig);
1163 quoteflt_reset(qf, pbuf);
1164 if(dostat & 4)
1165 n_pstate |= n_PS_BASE64_STRIP_CR;
1166 while (!eof && fgetline(linedat, linesize, &cnt, &linelen, ibuf, 0)) {
1167 joutln:
1168 if (_out(*linedat, linelen, pbuf, convert, action, qf, stats, &outrest,
1169 (action & _TD_EOF ? NULL : &inrest)) < 0 || ferror(pbuf)) {
1170 rv = -1; /* XXX Should bail away?! */
1171 break;
1174 if(eof <= FAL0 && rv >= 0 && (outrest.l != 0 || inrest.l != 0)){
1175 linelen = 0;
1176 if(eof || inrest.l == 0)
1177 action |= _TD_EOF;
1178 eof = eof ? TRU1 : TRUM1;
1179 goto joutln;
1181 n_pstate &= ~n_PS_BASE64_STRIP_CR;
1182 action &= ~_TD_EOF;
1184 /* TODO HACK: when sending to the display we yet get fooled if a message
1185 * TODO doesn't end in a newline, because of our input/output 1:1.
1186 * TODO This should be handled automatically by a display filter, then */
1187 if(rv >= 0 && !qf->qf_nl_last &&
1188 (action == SEND_TODISP || action == SEND_TODISP_ALL))
1189 rv = quoteflt_push(qf, "\n", 1);
1191 quoteflt_flush(qf);
1193 if (rv >= 0 && (mh.mh_flags & MIME_HDL_TMPF_FILL)) {
1194 mh.mh_flags &= ~MIME_HDL_TMPF_FILL;
1195 fflush(pbuf);
1196 really_rewind(pbuf);
1197 /* Don't Fclose() the Ftmp() thing due to OF_REGISTER_UNLINK++ */
1198 goto jpipe_for_real;
1201 if (pbuf == qbuf)
1202 safe_signal(SIGPIPE, __sendp_opipe);
1204 if (outrest.s != NULL)
1205 free(outrest.s);
1206 if (inrest.s != NULL)
1207 free(inrest.s);
1209 if (pbuf != origobuf) {
1210 qf->qf_pfix_len = save_qf_pfix_len;
1211 stats = save_stats;
1215 jend:
1216 if (pbuf != qbuf) {
1217 safe_signal(SIGPIPE, SIG_IGN);
1218 Pclose(pbuf, !(mh.mh_flags & MIME_HDL_ASYNC));
1219 safe_signal(SIGPIPE, oldpipe);
1220 if (rv >= 0 && qbuf != NULL && qbuf != obuf)
1221 pipecpy(qbuf, obuf, origobuf, qf, stats);
1223 #ifdef HAVE_ICONV
1224 if (iconvd != (iconv_t)-1)
1225 n_iconv_close(iconvd);
1226 #endif
1227 jleave:
1228 NYD_LEAVE;
1229 return rv;
1232 static FILE *
1233 newfile(struct mimepart *ip, bool_t volatile *ispipe)
1235 struct str in, out;
1236 char *f;
1237 FILE *fp;
1238 NYD_ENTER;
1240 f = ip->m_filename;
1241 *ispipe = FAL0;
1243 if (f != NULL && f != (char*)-1) {
1244 in.s = f;
1245 in.l = strlen(f);
1246 makeprint(&in, &out);
1247 out.l = delctrl(out.s, out.l);
1248 f = savestrbuf(out.s, out.l);
1249 free(out.s);
1252 /* In interactive mode, let user perform all kind of expansions as desired,
1253 * and offer |SHELL-SPEC pipe targets, too */
1254 if (n_psonce & n_PSO_INTERACTIVE) {
1255 struct str prompt;
1256 struct n_string shou, *shoup;
1257 char *f2, *f3;
1259 shoup = n_string_creat_auto(&shou);
1261 /* TODO Generic function which asks for filename.
1262 * TODO If the current part is the first textpart the target
1263 * TODO is implicit from outer `write' etc! */
1264 /* I18N: Filename input prompt with file type indication */
1265 str_concat_csvl(&prompt, _("Enter filename for part "),
1266 (ip->m_partstring != NULL ? ip->m_partstring : n_qm),
1267 " (", ip->m_ct_type_plain, "): ", NULL);
1268 jgetname:
1269 f2 = n_go_input_cp(n_GO_INPUT_CTX_DEFAULT | n_GO_INPUT_HIST_ADD,
1270 prompt.s, ((f != (char*)-1 && f != NULL)
1271 ? n_shexp_quote_cp(f, FAL0) : NULL));
1272 if(f2 != NULL){
1273 in.s = n_UNCONST(f2);
1274 in.l = UIZ_MAX;
1275 if((n_shexp_parse_token((n_SHEXP_PARSE_TRUNC |
1276 n_SHEXP_PARSE_TRIM_SPACE | n_SHEXP_PARSE_TRIM_IFSSPACE |
1277 n_SHEXP_PARSE_LOG | n_SHEXP_PARSE_IGNORE_EMPTY),
1278 shoup, &in, NULL
1279 ) & (n_SHEXP_STATE_STOP |
1280 n_SHEXP_STATE_OUTPUT | n_SHEXP_STATE_ERR_MASK)
1281 ) != (n_SHEXP_STATE_STOP | n_SHEXP_STATE_OUTPUT))
1282 goto jgetname;
1283 f2 = n_string_cp(shoup);
1285 if (f2 == NULL || *f2 == '\0') {
1286 if (n_poption & n_PO_D_V)
1287 n_err(_("... skipping this\n"));
1288 n_string_gut(shoup);
1289 fp = NULL;
1290 goto jleave;
1293 if (*f2 == '|')
1294 /* Pipes are expanded by the shell */
1295 f = f2;
1296 else if ((f3 = fexpand(f2, FEXP_LOCAL | FEXP_NVAR)) == NULL)
1297 /* (Error message written by fexpand()) */
1298 goto jgetname;
1299 else
1300 f = f3;
1302 n_string_gut(shoup);
1305 if (f == NULL || f == (char*)-1 || *f == '\0')
1306 fp = NULL;
1307 else if (n_psonce & n_PSO_INTERACTIVE) {
1308 if (*f == '|') {
1309 fp = Popen(&f[1], "w", ok_vlook(SHELL), NULL, 1);
1310 if (!(*ispipe = (fp != NULL)))
1311 n_perr(f, 0);
1312 } else if ((fp = Fopen(f, "w")) == NULL)
1313 n_err(_("Cannot open %s\n"), n_shexp_quote_cp(f, FAL0));
1314 } else {
1315 /* Be very picky in non-interactive mode: actively disallow pipes,
1316 * prevent directory separators, and any filename member that would
1317 * become expanded by the shell if the name would be echo(1)ed */
1318 if(anyof(f, "/" n_SHEXP_MAGIC_PATH_CHARS)){
1319 char c;
1321 for(out.s = salloc((strlen(f) * 3) +1), out.l = 0; (c = *f++) != '\0';)
1322 if(strchr("/" n_SHEXP_MAGIC_PATH_CHARS, c)){
1323 out.s[out.l++] = '%';
1324 n_c_to_hex_base16(&out.s[out.l], c);
1325 out.l += 2;
1326 }else
1327 out.s[out.l++] = c;
1328 out.s[out.l] = '\0';
1329 f = out.s;
1332 /* Avoid overwriting of existing files */
1333 while((fp = Fopen(f, "wx")) == NULL){
1334 int e;
1336 if((e = n_err_no) != n_ERR_EXIST){
1337 n_err(_("Cannot open %s: %s\n"),
1338 n_shexp_quote_cp(f, FAL0), n_err_to_doc(e));
1339 break;
1342 if(ip->m_partstring != NULL)
1343 f = savecatsep(f, '#', ip->m_partstring);
1344 else
1345 f = savecat(f, "#.");
1348 jleave:
1349 NYD_LEAVE;
1350 return fp;
1353 static void
1354 pipecpy(FILE *pipebuf, FILE *outbuf, FILE *origobuf, struct quoteflt *qf,
1355 ui64_t *stats)
1357 char *line = NULL; /* TODO line pool */
1358 size_t linesize = 0, linelen, cnt;
1359 ssize_t all_sz, sz;
1360 NYD_ENTER;
1362 fflush(pipebuf);
1363 rewind(pipebuf);
1364 cnt = (size_t)fsize(pipebuf);
1365 all_sz = 0;
1367 quoteflt_reset(qf, outbuf);
1368 while (fgetline(&line, &linesize, &cnt, &linelen, pipebuf, 0) != NULL) {
1369 if ((sz = quoteflt_push(qf, line, linelen)) < 0)
1370 break;
1371 all_sz += sz;
1373 if ((sz = quoteflt_flush(qf)) > 0)
1374 all_sz += sz;
1375 if (line)
1376 free(line);
1378 if (all_sz > 0 && outbuf == origobuf && stats != NULL)
1379 *stats += all_sz;
1380 Fclose(pipebuf);
1381 NYD_LEAVE;
1384 static void
1385 statusput(const struct message *mp, FILE *obuf, struct quoteflt *qf,
1386 ui64_t *stats)
1388 char statout[3], *cp = statout;
1389 NYD_ENTER;
1391 if (mp->m_flag & MREAD)
1392 *cp++ = 'R';
1393 if (!(mp->m_flag & MNEW))
1394 *cp++ = 'O';
1395 *cp = 0;
1396 if (statout[0]) {
1397 int i = fprintf(obuf, "%.*sStatus: %s\n", (int)qf->qf_pfix_len,
1398 (qf->qf_pfix_len > 0 ? qf->qf_pfix : 0), statout);
1399 if (i > 0 && stats != NULL)
1400 *stats += i;
1402 NYD_LEAVE;
1405 static void
1406 xstatusput(const struct message *mp, FILE *obuf, struct quoteflt *qf,
1407 ui64_t *stats)
1409 char xstatout[4];
1410 char *xp = xstatout;
1411 NYD_ENTER;
1413 if (mp->m_flag & MFLAGGED)
1414 *xp++ = 'F';
1415 if (mp->m_flag & MANSWERED)
1416 *xp++ = 'A';
1417 if (mp->m_flag & MDRAFTED)
1418 *xp++ = 'T';
1419 *xp = 0;
1420 if (xstatout[0]) {
1421 int i = fprintf(obuf, "%.*sX-Status: %s\n", (int)qf->qf_pfix_len,
1422 (qf->qf_pfix_len > 0 ? qf->qf_pfix : 0), xstatout);
1423 if (i > 0 && stats != NULL)
1424 *stats += i;
1426 NYD_LEAVE;
1429 static void
1430 put_from_(FILE *fp, struct mimepart *ip, ui64_t *stats)
1432 char const *froma, *date, *nl;
1433 int i;
1434 NYD_ENTER;
1436 if (ip != NULL && ip->m_from != NULL) {
1437 froma = ip->m_from;
1438 date = fakedate(ip->m_time);
1439 nl = "\n";
1440 } else {
1441 froma = ok_vlook(LOGNAME);
1442 date = time_current.tc_ctime;
1443 nl = n_empty;
1446 n_COLOUR(
1447 if(n_COLOUR_IS_ACTIVE())
1448 n_colour_put(n_COLOUR_ID_VIEW_FROM_, NULL);
1450 i = fprintf(fp, "From %s %s%s", froma, date, nl);
1451 n_COLOUR(
1452 if(n_COLOUR_IS_ACTIVE())
1453 n_colour_reset();
1455 if (i > 0 && stats != NULL)
1456 *stats += i;
1457 NYD_LEAVE;
1460 FL int
1461 sendmp(struct message *mp, FILE *obuf, struct n_ignore const *doitp,
1462 char const *prefix, enum sendaction action, ui64_t *stats)
1464 struct n_sigman linedat_protect;
1465 struct quoteflt qf;
1466 FILE *ibuf;
1467 enum mime_parse_flags mpf;
1468 struct mimepart *ip;
1469 size_t linesize, cnt, sz, i;
1470 char *linedat;
1471 int rv, c;
1472 NYD_ENTER;
1474 time_current_update(&time_current, TRU1);
1475 rv = -1;
1476 linedat = NULL;
1477 linesize = 0;
1478 quoteflt_init(&qf, prefix);
1480 n_SIGMAN_ENTER_SWITCH(&linedat_protect, n_SIGMAN_ALL){
1481 case 0:
1482 break;
1483 default:
1484 goto jleave;
1487 if (mp == dot && action != SEND_TOSRCH)
1488 n_pstate |= n_PS_DID_PRINT_DOT;
1489 if (stats != NULL)
1490 *stats = 0;
1492 /* First line is the From_ line, so no headers there to worry about */
1493 if ((ibuf = setinput(&mb, mp, NEED_BODY)) == NULL)
1494 goto jleave;
1496 cnt = mp->m_size;
1497 sz = 0;
1499 bool_t nozap;
1500 char const *cpre = n_empty, *csuf = n_empty;
1502 #ifdef HAVE_COLOUR
1503 if(n_COLOUR_IS_ACTIVE()){
1504 struct n_colour_pen *cpen;
1505 struct str const *sp;
1507 cpen = n_colour_pen_create(n_COLOUR_ID_VIEW_FROM_,NULL);
1508 if((sp = n_colour_pen_to_str(cpen)) != NULL){
1509 cpre = sp->s;
1510 sp = n_colour_reset_to_str();
1511 if(sp != NULL)
1512 csuf = sp->s;
1515 #endif
1517 nozap = (doitp != n_IGNORE_ALL && doitp != n_IGNORE_FWD &&
1518 action != SEND_RFC822 &&
1519 !n_ignore_is_ign(doitp, "from_", sizeof("from_") -1));
1520 if (mp->m_flag & MNOFROM) {
1521 if (nozap)
1522 sz = fprintf(obuf, "%s%.*sFrom %s %s%s\n",
1523 cpre, (int)qf.qf_pfix_len,
1524 (qf.qf_pfix_len != 0 ? qf.qf_pfix : n_empty), fakefrom(mp),
1525 fakedate(mp->m_time), csuf);
1526 } else if (nozap) {
1527 if (qf.qf_pfix_len > 0) {
1528 i = fwrite(qf.qf_pfix, sizeof *qf.qf_pfix, qf.qf_pfix_len, obuf);
1529 if (i != qf.qf_pfix_len)
1530 goto jleave;
1531 sz += i;
1533 #ifdef HAVE_COLOUR
1534 if(*cpre != '\0'){
1535 fputs(cpre, obuf);
1536 cpre = (char const*)0x1;
1538 #endif
1540 while (cnt > 0 && (c = getc(ibuf)) != EOF) {
1541 #ifdef HAVE_COLOUR
1542 if(c == '\n' && *csuf != '\0'){
1543 cpre = (char const*)0x1;
1544 fputs(csuf, obuf);
1546 #endif
1547 putc(c, obuf);
1548 ++sz;
1549 --cnt;
1550 if (c == '\n')
1551 break;
1554 #ifdef HAVE_COLOUR
1555 if(*csuf != '\0' && cpre != (char const*)0x1 && *cpre != '\0')
1556 fputs(csuf, obuf);
1557 #endif
1558 } else {
1559 while (cnt > 0 && (c = getc(ibuf)) != EOF) {
1560 --cnt;
1561 if (c == '\n')
1562 break;
1566 if (sz > 0 && stats != NULL)
1567 *stats += sz;
1569 mpf = MIME_PARSE_NONE;
1570 if (action != SEND_MBOX && action != SEND_RFC822 && action != SEND_SHOW)
1571 mpf |= MIME_PARSE_PARTS | MIME_PARSE_DECRYPT;
1572 if ((ip = mime_parse_msg(mp, mpf)) == NULL)
1573 goto jleave;
1575 rv = sendpart(mp, ip, obuf, doitp, &qf, action, &linedat, &linesize,
1576 stats, 0);
1578 n_sigman_cleanup_ping(&linedat_protect);
1579 jleave:
1580 n_pstate &= ~n_PS_BASE64_STRIP_CR;
1581 quoteflt_destroy(&qf);
1582 if(linedat != NULL)
1583 free(linedat);
1584 NYD_LEAVE;
1585 n_sigman_leave(&linedat_protect, n_SIGMAN_VIPSIGS_NTTYOUT);
1586 return rv;
1589 /* s-it-mode */