sendout.c: fix HAVE_DEBUG From_ quoting..
[s-mailx.git] / collect.c
blob22ca693db487b99ab3b95ace4dacdcccef8a5b4f
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Collect input from standard input, handling ~ escapes.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
6 */
7 /*
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. 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 collect
38 #ifndef HAVE_AMALGAMATION
39 # include "nail.h"
40 #endif
42 /* The following hookiness with global variables is so that on receipt of an
43 * interrupt signal, the partial message can be salted away on *DEAD* */
45 static sighandler_type _coll_saveint; /* Previous SIGINT value */
46 static sighandler_type _coll_savehup; /* Previous SIGHUP value */
47 static FILE *_coll_fp; /* File for saving away */
48 static int volatile _coll_hadintr; /* Have seen one SIGINT so far */
49 static sigjmp_buf _coll_jmp; /* To get back to work */
50 static sigjmp_buf _coll_abort; /* To end collection with error */
51 static sigjmp_buf _coll_pipejmp; /* On broken pipe */
53 /* Handle `~:', `~_' and some hooks; hp may be NULL */
54 static void _execute_command(struct header *hp, char const *linebuf,
55 size_t linesize);
57 /* If *interactive* is set and *doecho* is, too, also dump to *stdout* */
58 static int _include_file(char const *name, int *linecount,
59 int *charcount, bool_t doecho, bool_t indent);
61 static void _collect_onpipe(int signo);
63 /* Execute cmd and insert its standard output into fp */
64 static void insertcommand(FILE *fp, char const *cmd);
66 /* ~p command */
67 static void print_collf(FILE *collf, struct header *hp);
69 /* Write a file, ex-like if f set */
70 static int exwrite(char const *name, FILE *fp, int f);
72 /* Parse off the message header from fp and store relevant fields in hp,
73 * replace _coll_fp with a shiny new version without any header */
74 static enum okay makeheader(FILE *fp, struct header *hp, si8_t *checkaddr_err);
76 /* Edit the message being collected on fp. On return, make the edit file the
77 * new temp file */
78 static void mesedit(int c, struct header *hp);
80 /* Pipe the message through the command. Old message is on stdin of command,
81 * new message collected from stdout. Shell must return 0 to accept new msg */
82 static void mespipe(char *cmd);
84 /* Interpolate the named messages into the current message, possibly doing
85 * indent stuff. The flag argument is one of the tilde escapes: [mMfFuU].
86 * Return a count of the number of characters now in the message, or -1 if an
87 * error is encountered writing the message temporary */
88 static int forward(char *ms, FILE *fp, int f);
90 /* On interrupt, come here to save the partial message in ~/dead.letter.
91 * Then jump out of the collection loop */
92 static void _collint(int s);
94 static void collhup(int s);
96 static int putesc(char const *s, FILE *stream); /* TODO wysh set! */
98 /* call_compose_mode_hook() setter hook */
99 static void a_coll__hook_setter(void *arg);
101 static void
102 _execute_command(struct header *hp, char const *linebuf, size_t linesize){
103 /* The problem arises if there are rfc822 message attachments and the
104 * user uses `~:' to change the current file. TODO Unfortunately we
105 * TODO cannot simply keep a pointer to, or increment a reference count
106 * TODO of the current `file' (mailbox that is) object, because the
107 * TODO codebase doesn't deal with that at all; so, until some far
108 * TODO later time, copy the name of the path, and warn the user if it
109 * TODO changed; we COULD use the AC_TMPFILE attachment type, i.e.,
110 * TODO copy the message attachments over to temporary files, but that
111 * TODO would require more changes so that the user still can recognize
112 * TODO in `~@' etc. that its a rfc822 message attachment; see below */
113 struct n_sigman sm;
114 struct attachment *ap;
115 char *mnbuf;
116 NYD_ENTER;
118 UNUSED(linesize);
119 mnbuf = NULL;
121 n_SIGMAN_ENTER_SWITCH(&sm, n_SIGMAN_ALL){
122 case 0:
123 break;
124 default:
125 goto jleave;
128 /* If the above todo is worked, remove or outsource to attachments.c! */
129 if(hp != NULL && (ap = hp->h_attach) != NULL) do
130 if(ap->a_msgno){
131 mnbuf = sstrdup(mailname);
132 break;
134 while((ap = ap->a_flink) != NULL);
136 n_source_command(linebuf);
138 n_sigman_cleanup_ping(&sm);
139 jleave:
140 if(mnbuf != NULL){
141 if(strcmp(mnbuf, mailname))
142 n_err(_("Mailbox changed: it is likely that existing "
143 "rfc822 attachments became invalid!\n"));
144 free(mnbuf);
146 NYD_LEAVE;
147 n_sigman_leave(&sm, n_SIGMAN_VIPSIGS_NTTYOUT);
150 static int
151 _include_file(char const *name, int *linecount, int *charcount,
152 bool_t doecho, bool_t indent)
154 FILE *fbuf;
155 char const *indb;
156 int ret = -1;
157 char *linebuf = NULL; /* TODO line pool */
158 size_t linesize = 0, indl, linelen, cnt;
159 NYD_ENTER;
161 if (name == (char*)-1)
162 fbuf = stdin;
163 else if ((fbuf = Fopen(name, "r")) == NULL) {
164 n_perr(name, 0);
165 goto jleave;
168 if (!indent)
169 indb = NULL, indl = 0;
170 else {
171 if ((indb = ok_vlook(indentprefix)) == NULL)
172 indb = INDENT_DEFAULT;
173 indl = strlen(indb);
176 *linecount = *charcount = 0;
177 cnt = fsize(fbuf);
178 while (fgetline(&linebuf, &linesize, &cnt, &linelen, fbuf, 0) != NULL) {
179 if (indl > 0 && fwrite(indb, sizeof *indb, indl, _coll_fp) != indl)
180 goto jleave;
181 if (fwrite(linebuf, sizeof *linebuf, linelen, _coll_fp) != linelen)
182 goto jleave;
183 ++(*linecount);
184 (*charcount) += linelen + indl;
185 if ((options & OPT_INTERACTIVE) && doecho) {
186 if (indl > 0)
187 fwrite(indb, sizeof *indb, indl, stdout);
188 fwrite(linebuf, sizeof *linebuf, linelen, stdout);
191 if (fflush(_coll_fp))
192 goto jleave;
193 if ((options & OPT_INTERACTIVE) && doecho)
194 fflush(stdout);
196 ret = 0;
197 jleave:
198 if (linebuf != NULL)
199 free(linebuf);
200 if (fbuf != NULL && fbuf != stdin)
201 Fclose(fbuf);
202 NYD_LEAVE;
203 return ret;
206 static void
207 _collect_onpipe(int signo)
209 NYD_X; /* Signal handler */
210 UNUSED(signo);
211 siglongjmp(_coll_pipejmp, 1);
214 static void
215 insertcommand(FILE *fp, char const *cmd)
217 FILE *ibuf = NULL;
218 int c;
219 NYD_ENTER;
221 if ((ibuf = Popen(cmd, "r", ok_vlook(SHELL), NULL, 0)) != NULL) {
222 while ((c = getc(ibuf)) != EOF) /* XXX bytewise, yuck! */
223 putc(c, fp);
224 Pclose(ibuf, TRU1);
225 } else
226 n_perr(cmd, 0);
227 NYD_LEAVE;
230 static void
231 print_collf(FILE *cf, struct header *hp)
233 char *lbuf = NULL; /* TODO line pool */
234 sighandler_type sigint;
235 FILE * volatile obuf = stdout;
236 struct attachment *ap;
237 char const *cp;
238 enum gfield gf;
239 size_t linesize = 0, linelen, cnt, cnt2;
240 NYD_ENTER;
242 fflush_rewind(cf);
243 cnt = cnt2 = fsize(cf);
245 sigint = safe_signal(SIGINT, SIG_IGN);
247 if ((options & OPT_INTERACTIVE) && (cp = ok_vlook(crt)) != NULL) {
248 size_t l, m;
250 m = 4;
251 if (hp->h_to != NULL)
252 ++m;
253 if (hp->h_subject != NULL)
254 ++m;
255 if (hp->h_cc != NULL)
256 ++m;
257 if (hp->h_bcc != NULL)
258 ++m;
259 if (hp->h_attach != NULL)
260 ++m;
261 m += (hp->h_from != NULL || myaddrs(hp) != NULL);
262 m += (hp->h_sender != NULL || ok_vlook(sender) != NULL);
263 m += (hp->h_replyto != NULL || ok_vlook(replyto) != NULL);
265 l = (*cp == '\0') ? (size_t)screensize() : strtoul(cp, NULL, 0);
266 if (m > l)
267 goto jpager;
268 l -= m;
270 for (m = 0; fgetline(&lbuf, &linesize, &cnt2, NULL, cf, 0); ++m)
272 rewind(cf);
273 if (l < m) {
274 jpager:
275 if (sigsetjmp(_coll_pipejmp, 1))
276 goto jendpipe;
277 if ((obuf = n_pager_open()) == NULL)
278 obuf = stdout;
279 else
280 safe_signal(SIGPIPE, &_collect_onpipe);
284 fprintf(obuf, _("-------\nMessage contains:\n"));
285 gf = GIDENT | GTO | GSUBJECT | GCC | GBCC | GNL | GFILES | GCOMMA;
286 puthead(TRU1, hp, obuf, gf, SEND_TODISP, CONV_NONE, NULL, NULL);
287 while (fgetline(&lbuf, &linesize, &cnt, &linelen, cf, 1))
288 prout(lbuf, linelen, obuf);
289 if (hp->h_attach != NULL) {
290 fputs(_("-------\nAttachments:\n"), obuf);
291 for (ap = hp->h_attach; ap != NULL; ap = ap->a_flink) {
292 if (ap->a_msgno)
293 fprintf(obuf, " - message %u\n", ap->a_msgno);
294 else {
295 /* TODO after MIME/send layer rewrite we *know*
296 * TODO the details of the attachment here,
297 * TODO so adjust this again, then */
298 char const *cs, *csi = "-> ";
300 if ((cs = ap->a_charset) == NULL &&
301 (csi = "<- ", cs = ap->a_input_charset) == NULL)
302 cs = charset_get_lc();
303 if ((cp = ap->a_content_type) == NULL)
304 cp = "?";
305 else if (ascncasecmp(cp, "text/", 5))
306 csi = "";
307 fprintf(obuf, " - [%s, %s%s] %s\n", cp, csi, cs,
308 n_shell_quote_cp(ap->a_name, FAL0));
313 jendpipe:
314 if (obuf != stdout)
315 n_pager_close(obuf);
316 if (lbuf != NULL)
317 free(lbuf);
318 safe_signal(SIGINT, sigint);
319 NYD_LEAVE;
322 static int
323 exwrite(char const *name, FILE *fp, int f)
325 FILE *of;
326 int c, lc, rv = -1;
327 long cc;
328 NYD_ENTER;
330 if (f) {
331 printf("%s ", n_shell_quote_cp(name, FAL0));
332 fflush(stdout);
334 if ((of = Fopen(name, "a")) == NULL) {
335 n_perr(NULL, 0);
336 goto jleave;
339 lc = 0;
340 cc = 0;
341 while ((c = getc(fp)) != EOF) {
342 ++cc;
343 if (c == '\n')
344 ++lc;
345 putc(c, of);
346 if (ferror(of)) {
347 n_perr(name, 0);
348 Fclose(of);
349 goto jleave;
352 Fclose(of);
353 printf(_("%d/%ld\n"), lc, cc);
354 fflush(stdout);
355 rv = 0;
356 jleave:
357 NYD_LEAVE;
358 return rv;
361 static enum okay
362 makeheader(FILE *fp, struct header *hp, si8_t *checkaddr_err)
364 FILE *nf;
365 int c;
366 enum okay rv = STOP;
367 NYD_ENTER;
369 if ((nf = Ftmp(NULL, "colhead", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==NULL) {
370 n_perr(_("temporary mail edit file"), 0);
371 goto jleave;
374 extract_header(fp, hp, checkaddr_err);
376 while ((c = getc(fp)) != EOF) /* XXX bytewise, yuck! */
377 putc(c, nf);
378 if (fp != _coll_fp)
379 Fclose(_coll_fp);
380 Fclose(fp);
381 _coll_fp = nf;
382 if (check_from_and_sender(hp->h_from, hp->h_sender) == NULL)
383 goto jleave;
384 rv = OKAY;
385 jleave:
386 NYD_LEAVE;
387 return rv;
390 static void
391 mesedit(int c, struct header *hp)
393 bool_t saved;
394 sighandler_type sigint;
395 FILE *nf;
396 NYD_ENTER;
398 saved = ok_blook(add_file_recipients);
399 ok_bset(add_file_recipients, TRU1);
401 sigint = safe_signal(SIGINT, SIG_IGN);
402 nf = run_editor(_coll_fp, (off_t)-1, c, FAL0, hp, NULL, SEND_MBOX, sigint);
403 if (nf != NULL) {
404 if (hp) {
405 rewind(nf);
406 makeheader(nf, hp, NULL);
407 } else {
408 fseek(nf, 0L, SEEK_END);
409 Fclose(_coll_fp);
410 _coll_fp = nf;
413 safe_signal(SIGINT, sigint);
415 ok_bset(add_file_recipients, saved);
416 NYD_LEAVE;
419 static void
420 mespipe(char *cmd)
422 FILE *nf;
423 sighandler_type sigint;
424 NYD_ENTER;
426 sigint = safe_signal(SIGINT, SIG_IGN);
428 if ((nf = Ftmp(NULL, "colpipe", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==NULL) {
429 n_perr(_("temporary mail edit file"), 0);
430 goto jout;
433 /* stdin = current message. stdout = new message */
434 fflush(_coll_fp);
435 if (run_command(ok_vlook(SHELL), 0, fileno(_coll_fp), fileno(nf), "-c",
436 cmd, NULL, NULL) < 0) {
437 Fclose(nf);
438 goto jout;
441 if (fsize(nf) == 0) {
442 n_err(_("No bytes from %s !?\n"), n_shell_quote_cp(cmd, FAL0));
443 Fclose(nf);
444 goto jout;
447 /* Take new files */
448 fseek(nf, 0L, SEEK_END);
449 Fclose(_coll_fp);
450 _coll_fp = nf;
451 jout:
452 safe_signal(SIGINT, sigint);
453 NYD_LEAVE;
456 static int
457 forward(char *ms, FILE *fp, int f)
459 int *msgvec, rv = 0;
460 struct ignoretab *ig;
461 char const *tabst;
462 enum sendaction action;
463 NYD_ENTER;
465 msgvec = salloc((size_t)(msgCount + 1) * sizeof *msgvec);
466 if (getmsglist(ms, msgvec, 0) < 0)
467 goto jleave;
468 if (*msgvec == 0) {
469 *msgvec = first(0, MMNORM);
470 if (*msgvec == 0) {
471 n_err(_("No appropriate messages\n"));
472 goto jleave;
474 msgvec[1] = 0;
477 if (f == 'f' || f == 'F' || f == 'u')
478 tabst = NULL;
479 else if ((tabst = ok_vlook(indentprefix)) == NULL)
480 tabst = INDENT_DEFAULT;
481 if (f == 'u' || f == 'U')
482 ig = allignore;
483 else
484 ig = upperchar(f) ? NULL : ignore;
485 action = (upperchar(f) && f != 'U') ? SEND_QUOTE_ALL : SEND_QUOTE;
487 printf(_("Interpolating:"));
488 for (; *msgvec != 0; ++msgvec) {
489 struct message *mp = message + *msgvec - 1;
491 touch(mp);
492 printf(" %d", *msgvec);
493 fflush(stdout);
494 if (sendmp(mp, fp, ig, tabst, action, NULL) < 0) {
495 n_perr(_("temporary mail file"), 0);
496 rv = -1;
497 break;
500 printf("\n");
501 jleave:
502 NYD_LEAVE;
503 return rv;
506 static void
507 _collint(int s)
509 NYD_X; /* Signal handler */
511 /* the control flow is subtle, because we can be called from ~q */
512 if (_coll_hadintr == 0) {
513 if (ok_blook(ignore)) {
514 puts("@");
515 fflush(stdout);
516 clearerr(stdin);
517 } else
518 _coll_hadintr = 1;
519 siglongjmp(_coll_jmp, 1);
521 exit_status |= EXIT_SEND_ERROR;
522 if (s != 0)
523 savedeadletter(_coll_fp, TRU1);
524 /* Aborting message, no need to fflush() .. */
525 siglongjmp(_coll_abort, 1);
528 static void
529 collhup(int s)
531 NYD_X; /* Signal handler */
532 UNUSED(s);
534 savedeadletter(_coll_fp, TRU1);
535 /* Let's pretend nobody else wants to clean up, a true statement at
536 * this time */
537 exit(EXIT_ERR);
540 static int
541 putesc(char const *s, FILE *stream)
543 int n = 0, rv = -1;
544 NYD_ENTER;
546 while (s[0] != '\0') {
547 if (s[0] == '\\') {
548 if (s[1] == 't') {
549 if (putc('\t', stream) == EOF)
550 goto jleave;
551 ++n;
552 s += 2;
553 continue;
555 if (s[1] == 'n') {
556 if (putc('\n', stream) == EOF)
557 goto jleave;
558 ++n;
559 s += 2;
560 continue;
563 if (putc(s[0], stream) == EOF)
564 goto jleave;
565 ++n;
566 ++s;
568 if (putc('\n', stream) == EOF)
569 goto jleave;
570 rv = ++n;
571 jleave:
572 NYD_LEAVE;
573 return rv;
576 static void
577 a_coll__hook_setter(void *arg){ /* TODO v15: drop */
578 struct header *hp;
579 char const *val;
580 NYD2_ENTER;
582 hp = arg;
584 if((val = detract(hp->h_from, GNAMEONLY)) == NULL)
585 val = "";
586 ok_vset(compose_from, val);
587 if((val = detract(hp->h_sender, 0)) == NULL)
588 val = "";
589 ok_vset(compose_sender, val);
590 if((val = detract(hp->h_to, GNAMEONLY)) == NULL)
591 val = "";
592 ok_vset(compose_to, val);
593 if((val = detract(hp->h_cc, GNAMEONLY)) == NULL)
594 val = "";
595 ok_vset(compose_cc, val);
596 if((val = detract(hp->h_bcc, GNAMEONLY)) == NULL)
597 val = "";
598 ok_vset(compose_bcc, val);
599 if((val = hp->h_subject) == NULL)
600 val = "";
601 ok_vset(compose_subject, val);
602 NYD2_LEAVE;
605 FL FILE *
606 collect(struct header *hp, int printheaders, struct message *mp,
607 char *quotefile, int doprefix, si8_t *checkaddr_err)
609 struct ignoretab *quoteig;
610 int lc, cc, c, t;
611 int volatile escape, getfields;
612 char *linebuf;
613 char const *cp;
614 size_t i, linesize; /* TODO line pool */
615 long cnt;
616 enum sendaction action;
617 sigset_t oset, nset;
618 FILE * volatile sigfp;
619 NYD_ENTER;
621 _coll_fp = NULL;
622 sigfp = NULL;
623 linesize = 0;
624 linebuf = NULL;
626 /* Start catching signals from here, but we're still die on interrupts
627 * until we're in the main loop */
628 sigfillset(&nset);
629 sigprocmask(SIG_BLOCK, &nset, &oset);
630 /* FIXME have dropped handlerpush() and localized onintr() in lex.c! */
631 if ((_coll_saveint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
632 safe_signal(SIGINT, &_collint);
633 if ((_coll_savehup = safe_signal(SIGHUP, SIG_IGN)) != SIG_IGN)
634 safe_signal(SIGHUP, collhup);
635 if (sigsetjmp(_coll_abort, 1))
636 goto jerr;
637 if (sigsetjmp(_coll_jmp, 1))
638 goto jerr;
639 pstate |= PS_RECURSED;
640 sigprocmask(SIG_SETMASK, &oset, (sigset_t*)NULL);
642 ++noreset;
643 if ((_coll_fp = Ftmp(NULL, "collect", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
644 NULL) {
645 n_perr(_("temporary mail file"), 0);
646 goto jerr;
649 /* If we are going to prompt for a subject, refrain from printing a newline
650 * after the headers (since some people mind) */
651 getfields = 0;
652 if (!(options & OPT_t_FLAG)) {
653 t = GTO | GSUBJECT | GCC | GNL;
654 if (ok_blook(fullnames))
655 t |= GCOMMA;
657 if (options & OPT_INTERACTIVE) {
658 if (hp->h_subject == NULL && (ok_blook(ask) || ok_blook(asksub)))
659 t &= ~GNL, getfields |= GSUBJECT;
661 if (hp->h_to == NULL)
662 t &= ~GNL, getfields |= GTO;
664 if (!ok_blook(bsdcompat) && !ok_blook(askatend)) {
665 if (hp->h_bcc == NULL && ok_blook(askbcc))
666 t &= ~GNL, getfields |= GBCC;
667 if (hp->h_cc == NULL && ok_blook(askcc))
668 t &= ~GNL, getfields |= GCC;
671 } else {
672 UNINIT(t, 0);
675 escape = ((cp = ok_vlook(escape)) != NULL) ? *cp : ESCAPE;
676 _coll_hadintr = 0;
678 if (!sigsetjmp(_coll_jmp, 1)) {
679 /* Ask for some headers first, as necessary */
680 if (getfields)
681 grab_headers(hp, getfields, 1);
683 /* Execute compose-enter TODO completely v15-compat intermediate!! */
684 if((cp = ok_vlook(on_compose_enter)) != NULL){
685 setup_from_and_sender(hp);
686 call_compose_mode_hook(cp, &a_coll__hook_setter, hp);
689 if(!(options & OPT_Mm_FLAG)){
690 char const *cp_obsolete = ok_vlook(NAIL_HEAD);
691 if(cp_obsolete != NULL)
692 OBSOLETE(_("please use *message-inject-head* "
693 "instead of *NAIL_HEAD*"));
695 if(((cp = ok_vlook(message_inject_head)) != NULL ||
696 (cp = cp_obsolete) != NULL) && putesc(cp, _coll_fp) < 0)
697 goto jerr;
699 /* Quote an original message */
700 if (mp != NULL && (doprefix || (cp = ok_vlook(quote)) != NULL)) {
701 quoteig = allignore;
702 action = SEND_QUOTE;
703 if (doprefix) {
704 quoteig = fwdignore;
705 if ((cp = ok_vlook(fwdheading)) == NULL)
706 cp = "-------- Original Message --------";
707 if (*cp != '\0' && fprintf(_coll_fp, "%s\n", cp) < 0)
708 goto jerr;
709 } else if (!strcmp(cp, "noheading")) {
710 /*EMPTY*/;
711 } else if (!strcmp(cp, "headers")) {
712 quoteig = ignore;
713 } else if (!strcmp(cp, "allheaders")) {
714 quoteig = NULL;
715 action = SEND_QUOTE_ALL;
716 } else {
717 cp = hfield1("from", mp);
718 if (cp != NULL && (cnt = (long)strlen(cp)) > 0) {
719 if (xmime_write(cp, cnt, _coll_fp, CONV_FROMHDR, TD_NONE) < 0)
720 goto jerr;
721 if (fprintf(_coll_fp, _(" wrote:\n\n")) < 0)
722 goto jerr;
725 if (fflush(_coll_fp))
726 goto jerr;
727 if (doprefix)
728 cp = NULL;
729 else if ((cp = ok_vlook(indentprefix)) == NULL)
730 cp = INDENT_DEFAULT;
731 if (sendmp(mp, _coll_fp, quoteig, cp, action, NULL) < 0)
732 goto jerr;
736 if (quotefile != NULL) {
737 if (_include_file(quotefile, &lc, &cc,
738 !(options & OPT_Mm_FLAG), FAL0) != 0)
739 goto jerr;
742 if ((options & (OPT_Mm_FLAG | OPT_INTERACTIVE)) == OPT_INTERACTIVE) {
743 /* Print what we have sofar also on the terminal (if useful) */
744 if (!ok_blook(editalong)) {
745 if (printheaders)
746 puthead(TRU1, hp, stdout, t, SEND_TODISP, CONV_NONE, NULL, NULL);
748 rewind(_coll_fp);
749 while ((c = getc(_coll_fp)) != EOF) /* XXX bytewise, yuck! */
750 putc(c, stdout);
751 if (fseek(_coll_fp, 0, SEEK_END))
752 goto jerr;
754 /* Ensure this is clean xxx not really necessary? */
755 fflush(stdout);
756 } else {
757 rewind(_coll_fp);
758 mesedit('e', hp);
759 /* As mandated by the Mail Reference Manual, print "(continue)" */
760 jcont:
761 printf(_("(continue)\n"));
762 fflush(stdout);
765 } else {
766 /* Come here for printing the after-signal message. Duplicate messages
767 * won't be printed because the write is aborted if we get a SIGTTOU */
768 if (_coll_hadintr)
769 n_err(_("\n(Interrupt -- one more to kill letter)\n"));
772 /* We're done with -M or -m */
773 if(options & OPT_Mm_FLAG)
774 goto jout;
775 /* No tilde escapes, interrupts not expected. Simply copy STDIN */
776 if (!(options & (OPT_INTERACTIVE | OPT_t_FLAG | OPT_TILDE_FLAG))) {
777 linebuf = srealloc(linebuf, linesize = LINESIZE);
778 while ((i = fread(linebuf, sizeof *linebuf, linesize, stdin)) > 0) {
779 if (i != fwrite(linebuf, sizeof *linebuf, i, _coll_fp))
780 goto jerr;
782 goto jout;
785 /* The interactive collect loop.
786 * All commands which come here are forbidden when sourcing! */
787 assert(_coll_hadintr || !(pstate & PS_SOURCING));
788 for (;;) {
789 cnt = n_lex_input("", FAL0, &linebuf, &linesize, NULL);
791 if (cnt < 0) {
792 assert(!(pstate & PS_SOURCING));
793 if (options & OPT_t_FLAG) {
794 fflush_rewind(_coll_fp);
795 /* It is important to set PS_t_FLAG before extract_header() *and*
796 * keep OPT_t_FLAG for the first parse of the message, too! */
797 pstate |= PS_t_FLAG;
798 if (makeheader(_coll_fp, hp, checkaddr_err) != OKAY)
799 goto jerr;
800 options &= ~OPT_t_FLAG;
801 continue;
802 } else if ((options & OPT_INTERACTIVE) && ok_blook(ignoreeof)) {
803 printf(_("*ignoreeof* set, use `~.' to terminate letter\n"));
804 continue;
806 break;
809 _coll_hadintr = 0;
811 if (cnt == 0 || !(options & (OPT_INTERACTIVE | OPT_TILDE_FLAG))) {
812 jputline:
813 /* TODO calls putline(), which *always* appends LF;
814 * TODO thus, STDIN with -t will ALWAYS end with LF,
815 * TODO even if no trailing LF and QP encoding.
816 * TODO when finally changed, update cc-test.sh */
817 if (putline(_coll_fp, linebuf, cnt) < 0)
818 goto jerr;
819 continue;
820 } else if (linebuf[0] == '.') {
821 if (linebuf[1] == '\0' && (ok_blook(dot) || ok_blook(ignoreeof)))
822 break;
824 if (linebuf[0] != escape)
825 goto jputline;
827 if (!(options & OPT_t_FLAG))
828 n_tty_addhist(linebuf, TRU1);
830 c = linebuf[1];
831 switch (c) {
832 default:
833 /* On double escape, send a single one. Otherwise, it's an error */
834 if (c == escape) {
835 if (putline(_coll_fp, &linebuf[1], cnt - 1) < 0)
836 goto jerr;
837 else
838 break;
840 n_err(_("Unknown tilde escape: ~%c\n"), asciichar(c) ? c : '?');
841 break;
842 case '!':
843 /* Shell escape, send the balance of line to sh -c */
844 c_shell(&linebuf[2]);
845 goto jcont;
846 break;
847 case ':':
848 /* FALLTHRU */
849 case '_':
850 /* Escape to command mode, but be nice! */
851 _execute_command(hp, &linebuf[2], cnt - 2);
852 break;
853 case '.':
854 /* Simulate end of file on input */
855 goto jout;
856 case 'x':
857 /* Same as 'q', but no *DEAD* saving */
858 /* FALLTHRU */
859 case 'q':
860 /* Force a quit, act like an interrupt had happened */
861 ++_coll_hadintr;
862 _collint((c == 'x') ? 0 : SIGINT);
863 exit(EXIT_ERR);
864 /*NOTREACHED*/
865 case 'h':
866 /* Grab a bunch of headers */
868 grab_headers(hp, GTO | GSUBJECT | GCC | GBCC,
869 (ok_blook(bsdcompat) && ok_blook(bsdorder)));
870 while (hp->h_to == NULL);
871 break;
872 case 'H':
873 /* Grab extra headers */
875 grab_headers(hp, GEXTRA, 0);
876 while (check_from_and_sender(hp->h_from, hp->h_sender) == NULL);
877 break;
878 case 't':
879 /* Add to the To list */
880 hp->h_to = cat(hp->h_to,
881 checkaddrs(lextract(&linebuf[2], GTO | GFULL), EACM_NORMAL,
882 NULL));
883 break;
884 case 's':
885 /* Set the Subject list */
886 cp = &linebuf[2];
887 while (whitechar(*cp))
888 ++cp;
889 hp->h_subject = savestr(cp);
890 break;
891 #ifdef HAVE_MEMORY_DEBUG
892 case 'S':
893 c_sstats(NULL);
894 break;
895 #endif
896 case '@':
897 /* Edit the attachment list */
898 if (linebuf[2] != '\0')
899 append_attachments(&hp->h_attach, &linebuf[2]);
900 else
901 edit_attachments(&hp->h_attach);
902 break;
903 case 'c':
904 /* Add to the CC list */
905 hp->h_cc = cat(hp->h_cc,
906 checkaddrs(lextract(&linebuf[2], GCC | GFULL), EACM_NORMAL,
907 NULL));
908 break;
909 case 'b':
910 /* Add stuff to blind carbon copies list */
911 hp->h_bcc = cat(hp->h_bcc,
912 checkaddrs(lextract(&linebuf[2], GBCC | GFULL), EACM_NORMAL,
913 NULL));
914 break;
915 case 'd':
916 strncpy(&linebuf[2], getdeadletter(), linesize - 2);
917 linebuf[linesize - 1] = '\0';
918 /*FALLTHRU*/
919 case 'R':
920 case 'r':
921 case '<':
922 /* Invoke a file: Search for the file name, then open it and copy the
923 * contents to _coll_fp */
924 cp = &linebuf[2];
925 while (whitechar(*cp))
926 ++cp;
927 if (*cp == '\0') {
928 n_err(_("Interpolate what file?\n"));
929 break;
931 if (*cp == '!') {
932 insertcommand(_coll_fp, cp + 1);
933 goto jcont;
935 if ((cp = file_expand(cp)) == NULL)
936 break;
937 if (is_dir(cp)) {
938 n_err(_("%s: Directory\n"), n_shell_quote_cp(cp, FAL0));
939 break;
941 printf(_("%s "), n_shell_quote_cp(cp, FAL0));
942 fflush(stdout);
943 if (_include_file(cp, &lc, &cc, FAL0, (c == 'R')) != 0)
944 goto jerr;
945 printf(_("%d/%d\n"), lc, cc);
946 break;
947 case 'i':
948 /* Insert a variable into the file */
949 cp = &linebuf[2];
950 while (whitechar(*cp))
951 ++cp;
952 if ((cp = vok_vlook(cp)) == NULL || *cp == '\0')
953 break;
954 if (putesc(cp, _coll_fp) < 0)
955 goto jerr;
956 if ((options & OPT_INTERACTIVE) && putesc(cp, stdout) < 0)
957 goto jerr;
958 break;
959 case 'a':
960 case 'A':
961 /* Insert the contents of a signature variable */
962 cp = (c == 'a') ? ok_vlook(sign) : ok_vlook(Sign);
963 if (cp != NULL && *cp != '\0') {
964 if (putesc(cp, _coll_fp) < 0)
965 goto jerr;
966 if ((options & OPT_INTERACTIVE) && putesc(cp, stdout) < 0)
967 goto jerr;
969 break;
970 case 'w':
971 /* Write the message on a file */
972 cp = &linebuf[2];
973 while (blankchar(*cp))
974 ++cp;
975 if (*cp == '\0' || (cp = file_expand(cp)) == NULL) {
976 n_err(_("Write what file!?\n"));
977 break;
979 rewind(_coll_fp);
980 if (exwrite(cp, _coll_fp, 1) < 0)
981 goto jerr;
982 break;
983 case 'm':
984 case 'M':
985 case 'f':
986 case 'F':
987 case 'u':
988 case 'U':
989 /* Interpolate the named messages, if we are in receiving mail mode.
990 * Does the standard list processing garbage. If ~f is given, we
991 * don't shift over */
992 if (forward(&linebuf[2], _coll_fp, c) < 0)
993 goto jerr;
994 break;
995 case 'p':
996 /* Print current state of the message without altering anything */
997 print_collf(_coll_fp, hp);
998 break;
999 case '|':
1000 /* Pipe message through command. Collect output as new message */
1001 rewind(_coll_fp);
1002 mespipe(&linebuf[2]);
1003 goto jcont;
1004 case 'v':
1005 case 'e':
1006 /* Edit the current message. 'e' -> use EDITOR, 'v' -> use VISUAL */
1007 rewind(_coll_fp);
1008 mesedit(c, ok_blook(editheaders) ? hp : NULL);
1009 goto jcont;
1010 case '?':
1011 /* Last the lengthy help string. (Very ugly, but take care for
1012 * compiler supported string lengths :() */
1013 puts(_(
1014 "TILDE ESCAPES (to be placed after a newline) excerpt:\n"
1015 "~. Commit and send message\n"
1016 "~: <command> Execute a mail command\n"
1017 "~<! <command> Insert output of command\n"
1018 "~@ [<files>] Edit attachment list\n"
1019 "~A Insert *Sign* variable (`~a' inserts *sign*)\n"
1020 "~c <users> Add users to Cc: list (`~b' for Bcc:)\n"
1021 "~d Read in *DEAD* (dead.letter)\n"
1022 "~e Edit message via *EDITOR*"
1024 puts(_(
1025 "~F <msglist> Read in with headers, don't *indentprefix* lines\n"
1026 "~f <msglist> Like ~F, but honour `ignore' / `retain' configuration\n"
1027 "~H Edit From:, Reply-To: and Sender:\n"
1028 "~h Prompt for Subject:, To:, Cc: and blind Bcc:\n"
1029 "~i <variable> Insert a value and a newline\n"
1030 "~M <msglist> Read in with headers, *indentprefix* (`~m': `retain' etc.)\n"
1031 "~p Print current message compose buffer\n"
1032 "~r <file> Read in a file (`~R' *indentprefix* lines)"
1034 puts(_(
1035 "~s <subject> Set Subject:\n"
1036 "~t <users> Add users to To: list\n"
1037 "~u <msglist> Read in message(s) without headers (`~U' indents lines)\n"
1038 "~v Edit message via *VISUAL*\n"
1039 "~w <file> Write message onto file\n"
1040 "~x Abort composition, discard message (`~q' saves in *DEAD*)\n"
1041 "~| <command> Pipe message through shell filter"
1043 break;
1047 jout:
1048 /* Execute compose-leave TODO completely v15-compat intermediate!! */
1049 if((cp = ok_vlook(on_compose_leave)) != NULL){
1050 setup_from_and_sender(hp);
1051 call_compose_mode_hook(cp, &a_coll__hook_setter, hp);
1054 /* Final change to edit headers, if not already above */
1055 if (ok_blook(bsdcompat) || ok_blook(askatend)) {
1056 if (hp->h_cc == NULL && ok_blook(askcc))
1057 grab_headers(hp, GCC, 1);
1058 if (hp->h_bcc == NULL && ok_blook(askbcc))
1059 grab_headers(hp, GBCC, 1);
1061 if (hp->h_attach == NULL && ok_blook(askattach))
1062 edit_attachments(&hp->h_attach);
1064 /* Add automatic receivers */
1065 if ((cp = ok_vlook(autocc)) != NULL && *cp != '\0')
1066 hp->h_cc = cat(hp->h_cc, checkaddrs(lextract(cp, GCC | GFULL),
1067 EACM_NORMAL, checkaddr_err));
1068 if ((cp = ok_vlook(autobcc)) != NULL && *cp != '\0')
1069 hp->h_bcc = cat(hp->h_bcc, checkaddrs(lextract(cp, GBCC | GFULL),
1070 EACM_NORMAL, checkaddr_err));
1071 if (*checkaddr_err != 0)
1072 goto jerr;
1074 if(options & OPT_Mm_FLAG)
1075 goto jskiptails;
1077 /* Place signature? */
1078 if((cp = ok_vlook(signature)) != NULL && *cp != '\0'){
1079 char const *cpq;
1081 if((cpq = file_expand(cp)) == NULL){
1082 n_err(_("*signature* expands to invalid file: %s\n"),
1083 n_shell_quote_cp(cp, FAL0));
1084 goto jerr;
1086 cpq = n_shell_quote_cp(cp = cpq, FAL0);
1088 if((sigfp = Fopen(cp, "r")) == NULL){
1089 n_err(_("Can't open *signature* %s: %s\n"), cpq, strerror(errno));
1090 goto jerr;
1093 if(linebuf == NULL)
1094 linebuf = smalloc(linesize = LINESIZE);
1095 c = '\0';
1096 while((i = fread(linebuf, sizeof *linebuf, linesize, UNVOLATILE(sigfp)))
1097 > 0){
1098 c = linebuf[i - 1];
1099 if(i != fwrite(linebuf, sizeof *linebuf, i, _coll_fp))
1100 goto jerr;
1103 /* C99 */{
1104 FILE *x = UNVOLATILE(sigfp);
1105 int e = errno, ise = ferror(x);
1107 sigfp = NULL;
1108 Fclose(x);
1110 if(ise){
1111 n_err(_("Errors while reading *signature* %s: %s\n"),
1112 cpq, strerror(e));
1113 goto jerr;
1117 if(c != '\0' && c != '\n')
1118 putc('\n', _coll_fp);
1121 { char const *cp_obsolete = ok_vlook(NAIL_TAIL);
1123 if(cp_obsolete != NULL)
1124 OBSOLETE(_("please use *message-inject-tail* instead of *NAIL_TAIL*"));
1126 if((cp = ok_vlook(message_inject_tail)) != NULL ||
1127 (cp = cp_obsolete) != NULL){
1128 if(putesc(cp, _coll_fp) < 0)
1129 goto jerr;
1130 if((options & OPT_INTERACTIVE) && putesc(cp, stdout) < 0)
1131 goto jerr;
1135 jskiptails:
1136 if(fflush(_coll_fp))
1137 goto jerr;
1138 rewind(_coll_fp);
1140 jleave:
1141 if (linebuf != NULL)
1142 free(linebuf);
1143 --noreset;
1144 sigfillset(&nset);
1145 sigprocmask(SIG_BLOCK, &nset, NULL);
1146 pstate &= ~PS_RECURSED;
1147 safe_signal(SIGINT, _coll_saveint);
1148 safe_signal(SIGHUP, _coll_savehup);
1149 sigprocmask(SIG_SETMASK, &oset, NULL);
1150 NYD_LEAVE;
1151 return _coll_fp;
1153 jerr:
1154 if(sigfp != NULL)
1155 Fclose(UNVOLATILE(sigfp));
1156 if (_coll_fp != NULL) {
1157 Fclose(_coll_fp);
1158 _coll_fp = NULL;
1160 goto jleave;
1163 /* s-it-mode */