Merge branch 'topic/customhdr'
[s-mailx.git] / collect.c
blob2e273e63f4ab6a78fa7d25a8fae6381bba35beca
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 sighandler_type _coll_savetstp; /* Previous SIGTSTP value */
48 static sighandler_type _coll_savettou; /* Previous SIGTTOU value */
49 static sighandler_type _coll_savettin; /* Previous SIGTTIN value */
50 static FILE *_coll_fp; /* File for saving away */
51 static int volatile _coll_hadintr; /* Have seen one SIGINT so far */
52 static sigjmp_buf _coll_jmp; /* To get back to work */
53 static int _coll_jmp_p; /* whether to long jump */
54 static sigjmp_buf _coll_abort; /* To end collection with error */
55 static sigjmp_buf _coll_pipejmp; /* On broken pipe */
57 /* Handle `~:', `~_' */
58 static void _execute_command(struct header *hp, char *linebuf,
59 size_t linesize);
61 /* If *interactive* is set and *doecho* is, too, also dump to *stdout* */
62 static int _include_file(char const *name, int *linecount,
63 int *charcount, bool_t doecho, bool_t indent);
65 static void _collect_onpipe(int signo);
67 /* Execute cmd and insert its standard output into fp */
68 static void insertcommand(FILE *fp, char const *cmd);
70 /* ~p command */
71 static void print_collf(FILE *collf, struct header *hp);
73 /* Write a file, ex-like if f set */
74 static int exwrite(char const *name, FILE *fp, int f);
76 /* Parse off the message header from fp and store relevant fields in hp,
77 * replace _coll_fp with a shiny new version without any header */
78 static enum okay makeheader(FILE *fp, struct header *hp, si8_t *checkaddr_err);
80 /* Edit the message being collected on fp. On return, make the edit file the
81 * new temp file */
82 static void mesedit(int c, struct header *hp);
84 /* Pipe the message through the command. Old message is on stdin of command,
85 * new message collected from stdout. Shell must return 0 to accept new msg */
86 static void mespipe(char *cmd);
88 /* Interpolate the named messages into the current message, possibly doing
89 * indent stuff. The flag argument is one of the tilde escapes: [mMfFuU].
90 * Return a count of the number of characters now in the message, or -1 if an
91 * error is encountered writing the message temporary */
92 static int forward(char *ms, FILE *fp, int f);
94 /* Print (continue) when continued after ^Z */
95 static void collstop(int s);
97 /* On interrupt, come here to save the partial message in ~/dead.letter.
98 * Then jump out of the collection loop */
99 static void _collint(int s);
101 static void collhup(int s);
103 static int putesc(char const *s, FILE *stream);
105 static void
106 _execute_command(struct header *hp, char *linebuf, size_t linesize)
108 /* The problem arises if there are rfc822 message attachments and the
109 * user uses `~:' to change the current file. TODO Unfortunately we
110 * TODO cannot simply keep a pointer to, or increment a reference count
111 * TODO of the current `file' (mailbox that is) object, because the
112 * TODO codebase doesn't deal with that at all; so, until some far
113 * TODO later time, copy the name of the path, and warn the user if it
114 * TODO changed; we COULD use the AC_TMPFILE attachment type, i.e.,
115 * TODO copy the message attachments over to temporary files, but that
116 * TODO would require more changes so that the user still can recognize
117 * TODO in `~@' etc. that its a rfc822 message attachment; see below */
118 char *mnbuf = NULL;
119 size_t mnlen = 0 /* silence CC */;
120 struct attachment *ap;
121 NYD_ENTER;
123 /* If the above todo is worked, remove or outsource to attachments.c! */
124 if ((ap = hp->h_attach) != NULL) do
125 if (ap->a_msgno) {
126 mnlen = strlen(mailname) +1;
127 mnbuf = ac_alloc(mnlen);
128 memcpy(mnbuf, mailname, mnlen);
129 break;
131 while ((ap = ap->a_flink) != NULL);
133 pstate &= ~PS_HOOK_MASK;
134 execute(linebuf, linesize);
136 if (mnbuf != NULL) {
137 if (strncmp(mnbuf, mailname, mnlen))
138 n_err(_("Mailbox changed: it is likely that existing "
139 "rfc822 attachments became invalid!\n"));
140 ac_free(mnbuf);
142 NYD_LEAVE;
145 static int
146 _include_file(char const *name, int *linecount, int *charcount,
147 bool_t doecho, bool_t indent)
149 FILE *fbuf;
150 char const *indb;
151 int ret = -1;
152 char *linebuf = NULL; /* TODO line pool */
153 size_t linesize = 0, indl, linelen, cnt;
154 NYD_ENTER;
156 if ((fbuf = Fopen(name, "r")) == NULL) {
157 n_perr(name, 0);
158 goto jleave;
161 if (!indent)
162 indb = NULL, indl = 0;
163 else {
164 if ((indb = ok_vlook(indentprefix)) == NULL)
165 indb = INDENT_DEFAULT;
166 indl = strlen(indb);
169 *linecount = *charcount = 0;
170 cnt = fsize(fbuf);
171 while (fgetline(&linebuf, &linesize, &cnt, &linelen, fbuf, 0) != NULL) {
172 if (indl > 0 && fwrite(indb, sizeof *indb, indl, _coll_fp) != indl)
173 goto jleave;
174 if (fwrite(linebuf, sizeof *linebuf, linelen, _coll_fp) != linelen)
175 goto jleave;
176 ++(*linecount);
177 (*charcount) += linelen + indl;
178 if ((options & OPT_INTERACTIVE) && doecho) {
179 if (indl > 0)
180 fwrite(indb, sizeof *indb, indl, stdout);
181 fwrite(linebuf, sizeof *linebuf, linelen, stdout);
184 if (fflush(_coll_fp))
185 goto jleave;
186 if ((options & OPT_INTERACTIVE) && doecho)
187 fflush(stdout);
189 ret = 0;
190 jleave:
191 if (linebuf != NULL)
192 free(linebuf);
193 if (fbuf != NULL)
194 Fclose(fbuf);
195 NYD_LEAVE;
196 return ret;
199 static void
200 _collect_onpipe(int signo)
202 NYD_X; /* Signal handler */
203 UNUSED(signo);
204 siglongjmp(_coll_pipejmp, 1);
207 static void
208 insertcommand(FILE *fp, char const *cmd)
210 FILE *ibuf = NULL;
211 char const *cp;
212 int c;
213 NYD_ENTER;
215 cp = ok_vlook(SHELL);
216 if (cp == NULL)
217 cp = XSHELL;
219 if ((ibuf = Popen(cmd, "r", cp, NULL, 0)) != NULL) {
220 while ((c = getc(ibuf)) != EOF) /* XXX bytewise, yuck! */
221 putc(c, fp);
222 Pclose(ibuf, TRU1);
223 } else
224 n_perr(cmd, 0);
225 NYD_LEAVE;
228 static void
229 print_collf(FILE *cf, struct header *hp)
231 char *lbuf = NULL; /* TODO line pool */
232 sighandler_type sigint;
233 FILE * volatile obuf = stdout;
234 struct attachment *ap;
235 char const *cp;
236 enum gfield gf;
237 size_t linesize = 0, linelen, cnt, cnt2;
238 NYD_ENTER;
240 fflush_rewind(cf);
241 cnt = cnt2 = fsize(cf);
243 sigint = safe_signal(SIGINT, SIG_IGN);
245 if ((options & OPT_INTERACTIVE) && (cp = ok_vlook(crt)) != NULL) {
246 size_t l, m;
248 m = 4;
249 if (hp->h_to != NULL)
250 ++m;
251 if (hp->h_subject != NULL)
252 ++m;
253 if (hp->h_cc != NULL)
254 ++m;
255 if (hp->h_bcc != NULL)
256 ++m;
257 if (hp->h_attach != NULL)
258 ++m;
259 m += (hp->h_from != NULL || myaddrs(hp) != NULL);
260 m += (hp->h_sender != NULL || ok_vlook(sender) != NULL);
261 m += (hp->h_replyto != NULL || ok_vlook(replyto) != NULL);
263 l = (*cp == '\0') ? screensize() : atoi(cp);
264 if (m > l)
265 goto jpager;
266 l -= m;
268 for (m = 0; fgetline(&lbuf, &linesize, &cnt2, NULL, cf, 0); ++m)
270 rewind(cf);
271 if (l < m) {
272 jpager:
273 cp = get_pager(NULL);
274 if (sigsetjmp(_coll_pipejmp, 1))
275 goto jendpipe;
276 obuf = Popen(cp, "w", NULL, NULL, 1);
277 if (obuf == NULL) {
278 n_perr(cp, 0);
279 obuf = stdout;
280 } else
281 safe_signal(SIGPIPE, &_collect_onpipe);
285 fprintf(obuf, _("-------\nMessage contains:\n"));
286 gf = GIDENT | GTO | GSUBJECT | GCC | GBCC | GNL | GFILES | GCOMMA;
287 puthead(TRU1, hp, obuf, gf, SEND_TODISP, CONV_NONE, NULL, NULL);
288 while (fgetline(&lbuf, &linesize, &cnt, &linelen, cf, 1))
289 prout(lbuf, linelen, obuf);
290 if (hp->h_attach != NULL) {
291 fputs(_("-------\nAttachments:\n"), obuf);
292 for (ap = hp->h_attach; ap != NULL; ap = ap->a_flink) {
293 if (ap->a_msgno)
294 fprintf(obuf, " - message %u\n", ap->a_msgno);
295 else {
296 /* TODO after MIME/send layer rewrite we *know*
297 * TODO the details of the attachment here,
298 * TODO so adjust this again, then */
299 char const *cs, *csi = "-> ";
301 if ((cs = ap->a_charset) == NULL &&
302 (csi = "<- ", cs = ap->a_input_charset) == NULL)
303 cs = charset_get_lc();
304 if ((cp = ap->a_content_type) == NULL)
305 cp = "?";
306 else if (ascncasecmp(cp, "text/", 5))
307 csi = "";
308 fprintf(obuf, " - [%s, %s%s] %s\n", cp, csi, cs, ap->a_name);
313 jendpipe:
314 if (obuf != stdout) {
315 safe_signal(SIGPIPE, SIG_IGN);
316 Pclose(obuf, TRU1);
317 safe_signal(SIGPIPE, dflpipe);
319 if (lbuf != NULL)
320 free(lbuf);
321 safe_signal(SIGINT, sigint);
322 NYD_LEAVE;
325 static int
326 exwrite(char const *name, FILE *fp, int f)
328 FILE *of;
329 int c, lc, rv = -1;
330 long cc;
331 NYD_ENTER;
333 if (f) {
334 printf("\"%s\" ", name);
335 fflush(stdout);
337 if ((of = Fopen(name, "a")) == NULL) {
338 n_perr(NULL, 0);
339 goto jleave;
342 lc = 0;
343 cc = 0;
344 while ((c = getc(fp)) != EOF) {
345 ++cc;
346 if (c == '\n')
347 ++lc;
348 putc(c, of);
349 if (ferror(of)) {
350 n_perr(name, 0);
351 Fclose(of);
352 goto jleave;
355 Fclose(of);
356 printf(_("%d/%ld\n"), lc, cc);
357 fflush(stdout);
358 rv = 0;
359 jleave:
360 NYD_LEAVE;
361 return rv;
364 static enum okay
365 makeheader(FILE *fp, struct header *hp, si8_t *checkaddr_err)
367 FILE *nf;
368 int c;
369 enum okay rv = STOP;
370 NYD_ENTER;
372 if ((nf = Ftmp(NULL, "colhead", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==NULL) {
373 n_perr(_("temporary mail edit file"), 0);
374 goto jleave;
377 extract_header(fp, hp, checkaddr_err);
379 while ((c = getc(fp)) != EOF) /* XXX bytewise, yuck! */
380 putc(c, nf);
381 if (fp != _coll_fp)
382 Fclose(_coll_fp);
383 Fclose(fp);
384 _coll_fp = nf;
385 if (check_from_and_sender(hp->h_from, hp->h_sender) == NULL)
386 goto jleave;
387 rv = OKAY;
388 jleave:
389 NYD_LEAVE;
390 return rv;
393 static void
394 mesedit(int c, struct header *hp)
396 bool_t saved;
397 sighandler_type sigint;
398 FILE *nf;
399 NYD_ENTER;
401 saved = ok_blook(add_file_recipients);
402 ok_bset(add_file_recipients, TRU1);
404 sigint = safe_signal(SIGINT, SIG_IGN);
405 nf = run_editor(_coll_fp, (off_t)-1, c, FAL0, hp, NULL, SEND_MBOX, sigint);
406 if (nf != NULL) {
407 if (hp) {
408 rewind(nf);
409 makeheader(nf, hp, NULL);
410 } else {
411 fseek(nf, 0L, SEEK_END);
412 Fclose(_coll_fp);
413 _coll_fp = nf;
416 safe_signal(SIGINT, sigint);
418 ok_bset(add_file_recipients, saved);
419 NYD_LEAVE;
422 static void
423 mespipe(char *cmd)
425 FILE *nf;
426 sighandler_type sigint;
427 char const *sh;
428 NYD_ENTER;
430 sigint = safe_signal(SIGINT, SIG_IGN);
432 if ((nf = Ftmp(NULL, "colpipe", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==NULL) {
433 n_perr(_("temporary mail edit file"), 0);
434 goto jout;
437 /* stdin = current message. stdout = new message */
438 if ((sh = ok_vlook(SHELL)) == NULL)
439 sh = XSHELL;
440 fflush(_coll_fp);
441 if (run_command(sh, 0, fileno(_coll_fp), fileno(nf), "-c", cmd, NULL, NULL)
442 < 0) {
443 Fclose(nf);
444 goto jout;
447 if (fsize(nf) == 0) {
448 n_err(_("No bytes from \"%s\" !?\n"), cmd);
449 Fclose(nf);
450 goto jout;
453 /* Take new files */
454 fseek(nf, 0L, SEEK_END);
455 Fclose(_coll_fp);
456 _coll_fp = nf;
457 jout:
458 safe_signal(SIGINT, sigint);
459 NYD_LEAVE;
462 static int
463 forward(char *ms, FILE *fp, int f)
465 int *msgvec, rv = 0;
466 struct ignoretab *ig;
467 char const *tabst;
468 enum sendaction action;
469 NYD_ENTER;
471 msgvec = salloc((size_t)(msgCount + 1) * sizeof *msgvec);
472 if (getmsglist(ms, msgvec, 0) < 0)
473 goto jleave;
474 if (*msgvec == 0) {
475 *msgvec = first(0, MMNORM);
476 if (*msgvec == 0) {
477 n_err(_("No appropriate messages\n"));
478 goto jleave;
480 msgvec[1] = 0;
483 if (f == 'f' || f == 'F' || f == 'u')
484 tabst = NULL;
485 else if ((tabst = ok_vlook(indentprefix)) == NULL)
486 tabst = INDENT_DEFAULT;
487 if (f == 'u' || f == 'U')
488 ig = allignore;
489 else
490 ig = upperchar(f) ? NULL : ignore;
491 action = (upperchar(f) && f != 'U') ? SEND_QUOTE_ALL : SEND_QUOTE;
493 printf(_("Interpolating:"));
494 for (; *msgvec != 0; ++msgvec) {
495 struct message *mp = message + *msgvec - 1;
497 touch(mp);
498 printf(" %d", *msgvec);
499 fflush(stdout);
500 if (sendmp(mp, fp, ig, tabst, action, NULL) < 0) {
501 n_perr(_("temporary mail file"), 0);
502 rv = -1;
503 break;
506 printf("\n");
507 jleave:
508 NYD_LEAVE;
509 return rv;
512 static void
513 collstop(int s)
515 sighandler_type old_action;
516 sigset_t nset;
517 NYD_X; /* Signal handler */
519 old_action = safe_signal(s, SIG_DFL);
521 sigemptyset(&nset);
522 sigaddset(&nset, s);
523 sigprocmask(SIG_UNBLOCK, &nset, NULL);
524 n_raise(s);
525 sigprocmask(SIG_BLOCK, &nset, NULL);
527 safe_signal(s, old_action);
528 if (_coll_jmp_p) {
529 _coll_jmp_p = 0;
530 _coll_hadintr = 0;
531 siglongjmp(_coll_jmp, 1);
535 static void
536 _collint(int s)
538 NYD_X; /* Signal handler */
540 /* the control flow is subtle, because we can be called from ~q */
541 if (_coll_hadintr == 0) {
542 if (ok_blook(ignore)) {
543 puts("@");
544 fflush(stdout);
545 clearerr(stdin);
546 } else
547 _coll_hadintr = 1;
548 siglongjmp(_coll_jmp, 1);
550 exit_status |= EXIT_SEND_ERROR;
551 if (s != 0)
552 savedeadletter(_coll_fp, 1);
553 /* Aborting message, no need to fflush() .. */
554 siglongjmp(_coll_abort, 1);
557 static void
558 collhup(int s)
560 NYD_X; /* Signal handler */
561 UNUSED(s);
563 savedeadletter(_coll_fp, 1);
564 /* Let's pretend nobody else wants to clean up, a true statement at
565 * this time */
566 exit(EXIT_ERR);
569 static int
570 putesc(char const *s, FILE *stream)
572 int n = 0, rv = -1;
573 NYD_ENTER;
575 while (s[0] != '\0') {
576 if (s[0] == '\\') {
577 if (s[1] == 't') {
578 if (putc('\t', stream) == EOF)
579 goto jleave;
580 ++n;
581 s += 2;
582 continue;
584 if (s[1] == 'n') {
585 if (putc('\n', stream) == EOF)
586 goto jleave;
587 ++n;
588 s += 2;
589 continue;
592 if (putc(s[0], stream) == EOF)
593 goto jleave;
594 ++n;
595 ++s;
597 if (putc('\n', stream) == EOF)
598 goto jleave;
599 rv = ++n;
600 jleave:
601 NYD_LEAVE;
602 return rv;
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, *quote;
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 sighandler_type savedtop;
619 FILE * volatile sigfp;
620 NYD_ENTER;
622 _coll_fp = NULL;
623 sigfp = NULL;
624 linesize = 0;
625 linebuf = quote = NULL;
627 /* Start catching signals from here, but we're still die on interrupts
628 * until we're in the main loop */
629 sigemptyset(&nset);
630 sigaddset(&nset, SIGINT);
631 sigaddset(&nset, SIGHUP);
632 sigprocmask(SIG_BLOCK, &nset, &oset);
633 handlerpush(&_collint);
634 if ((_coll_saveint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
635 safe_signal(SIGINT, &_collint);
636 if ((_coll_savehup = safe_signal(SIGHUP, SIG_IGN)) != SIG_IGN)
637 safe_signal(SIGHUP, collhup);
638 /* TODO We do a lot of redundant signal handling, especially
639 * TODO with the command line editor(s); try to merge this */
640 _coll_savetstp = safe_signal(SIGTSTP, collstop);
641 _coll_savettou = safe_signal(SIGTTOU, collstop);
642 _coll_savettin = safe_signal(SIGTTIN, collstop);
643 if (sigsetjmp(_coll_abort, 1))
644 goto jerr;
645 if (sigsetjmp(_coll_jmp, 1))
646 goto jerr;
647 sigprocmask(SIG_SETMASK, &oset, (sigset_t*)NULL);
649 ++noreset;
650 if ((_coll_fp = Ftmp(NULL, "collect", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
651 NULL) {
652 n_perr(_("temporary mail file"), 0);
653 goto jerr;
657 char const *cp_obsolete = ok_vlook(NAIL_HEAD);
658 if(cp_obsolete != NULL)
659 OBSOLETE(_("please use *message-inject-head* instead of *NAIL_HEAD*"));
661 if(((cp = ok_vlook(message_inject_head)) != NULL ||
662 (cp = cp_obsolete) != NULL) && putesc(cp, _coll_fp) < 0)
663 goto jerr;
666 /* If we are going to prompt for a subject, refrain from printing a newline
667 * after the headers (since some people mind) */
668 getfields = 0;
669 if (!(options & OPT_t_FLAG)) {
670 t = GTO | GSUBJECT | GCC | GNL;
671 if (ok_blook(fullnames))
672 t |= GCOMMA;
674 if (options & OPT_INTERACTIVE) {
675 if (hp->h_subject == NULL && (ok_blook(ask) || ok_blook(asksub)))
676 t &= ~GNL, getfields |= GSUBJECT;
678 if (hp->h_to == NULL)
679 t &= ~GNL, getfields |= GTO;
681 if (!ok_blook(bsdcompat) && !ok_blook(askatend)) {
682 if (hp->h_bcc == NULL && ok_blook(askbcc))
683 t &= ~GNL, getfields |= GBCC;
684 if (hp->h_cc == NULL && ok_blook(askcc))
685 t &= ~GNL, getfields |= GCC;
689 if (printheaders && !ok_blook(editalong)) {
690 puthead(TRU1, hp, stdout, t, SEND_TODISP, CONV_NONE, NULL, NULL);
691 fflush(stdout);
695 /* Quote an original message */
696 if (mp != NULL && (doprefix || (quote = ok_vlook(quote)) != NULL)) {
697 quoteig = allignore;
698 action = SEND_QUOTE;
699 if (doprefix) {
700 quoteig = fwdignore;
701 if ((cp = ok_vlook(fwdheading)) == NULL)
702 cp = "-------- Original Message --------";
703 if (*cp != '\0' && fprintf(_coll_fp, "%s\n", cp) < 0)
704 goto jerr;
705 } else if (!strcmp(quote, "noheading")) {
706 /*EMPTY*/;
707 } else if (!strcmp(quote, "headers")) {
708 quoteig = ignore;
709 } else if (!strcmp(quote, "allheaders")) {
710 quoteig = NULL;
711 action = SEND_QUOTE_ALL;
712 } else {
713 cp = hfield1("from", mp);
714 if (cp != NULL && (cnt = (long)strlen(cp)) > 0) {
715 if (xmime_write(cp, cnt, _coll_fp, CONV_FROMHDR, TD_NONE) < 0)
716 goto jerr;
717 if (fprintf(_coll_fp, _(" wrote:\n\n")) < 0)
718 goto jerr;
721 if (fflush(_coll_fp))
722 goto jerr;
723 if (doprefix)
724 cp = NULL;
725 else if ((cp = ok_vlook(indentprefix)) == NULL)
726 cp = INDENT_DEFAULT;
727 if (sendmp(mp, _coll_fp, quoteig, cp, action, NULL) < 0)
728 goto jerr;
731 /* Print what we have sofar also on the terminal (if useful) */
732 if ((options & OPT_INTERACTIVE) && !ok_blook(editalong)) {
733 rewind(_coll_fp);
734 while ((c = getc(_coll_fp)) != EOF) /* XXX bytewise, yuck! */
735 putc(c, stdout);
736 if (fseek(_coll_fp, 0, SEEK_END))
737 goto jerr;
738 /* Ensure this is clean xxx not really necessary? */
739 fflush(stdout);
742 escape = ((cp = ok_vlook(escape)) != NULL) ? *cp : ESCAPE;
743 _coll_hadintr = 0;
745 if (!sigsetjmp(_coll_jmp, 1)) {
746 if (getfields)
747 grab_headers(hp, getfields, 1);
748 if (quotefile != NULL) {
749 if (_include_file(quotefile, &lc, &cc, TRU1, FAL0) != 0)
750 goto jerr;
752 if ((options & OPT_INTERACTIVE) && ok_blook(editalong)) {
753 rewind(_coll_fp);
754 mesedit('e', hp);
755 goto jcont;
757 } else {
758 /* Come here for printing the after-signal message. Duplicate messages
759 * won't be printed because the write is aborted if we get a SIGTTOU */
760 if (_coll_hadintr) {
761 n_err(_("\n(Interrupt -- one more to kill letter)\n"));
762 } else {
763 jcont:
764 printf(_("(continue)\n"));
765 fflush(stdout);
769 /* No tilde escapes, interrupts not expected. Simply copy STDIN */
770 if (!(options & (OPT_INTERACTIVE | OPT_t_FLAG | OPT_TILDE_FLAG))) {
771 linebuf = srealloc(linebuf, linesize = LINESIZE);
772 while ((i = fread(linebuf, sizeof *linebuf, linesize, stdin)) > 0) {
773 if (i != fwrite(linebuf, sizeof *linebuf, i, _coll_fp))
774 goto jerr;
776 goto jout;
779 /* The interactive collect loop.
780 * All commands which come here are forbidden when sourcing! */
781 assert(_coll_hadintr || !(pstate & PS_SOURCING));
782 for (;;) {
783 _coll_jmp_p = 1;
784 cnt = readline_input("", FAL0, &linebuf, &linesize, NULL);
785 _coll_jmp_p = 0;
787 if (cnt < 0) {
788 assert(!(pstate & PS_SOURCING));
789 if (options & OPT_t_FLAG) {
790 fflush_rewind(_coll_fp);
791 /* It is important to set PS_t_FLAG before extract_header() *and*
792 * keep OPT_t_FLAG for the first parse of the message, too! */
793 pstate |= PS_t_FLAG;
794 if (makeheader(_coll_fp, hp, checkaddr_err) != OKAY)
795 goto jerr;
796 options &= ~OPT_t_FLAG;
797 continue;
798 } else if ((options & OPT_INTERACTIVE) && ok_blook(ignoreeof)) {
799 printf(_("*ignoreeof* set, use \"~.\" to terminate letter\n"));
800 continue;
802 break;
805 _coll_hadintr = 0;
807 if (cnt == 0 || !(options & (OPT_INTERACTIVE | OPT_TILDE_FLAG))) {
808 jputline:
809 /* TODO calls putline(), which *always* appends LF;
810 * TODO thus, STDIN with -t will ALWAYS end with LF,
811 * TODO even if no trailing LF and QP encoding.
812 * TODO when finally changed, update cc-test.sh */
813 if (putline(_coll_fp, linebuf, cnt) < 0)
814 goto jerr;
815 continue;
816 } else if (linebuf[0] == '.') {
817 if (linebuf[1] == '\0' && (ok_blook(dot) || ok_blook(ignoreeof)))
818 break;
820 if (linebuf[0] != escape)
821 goto jputline;
823 if (!(options & OPT_t_FLAG))
824 n_tty_addhist(linebuf, TRU1);
826 c = linebuf[1];
827 switch (c) {
828 default:
829 /* On double escape, send a single one. Otherwise, it's an error */
830 if (c == escape) {
831 if (putline(_coll_fp, linebuf + 1, cnt - 1) < 0)
832 goto jerr;
833 else
834 break;
836 n_err(_("Unknown tilde escape: ~%c\n"), asciichar(c) ? c : '?');
837 break;
838 case '!':
839 /* Shell escape, send the balance of line to sh -c */
840 c_shell(linebuf + 2);
841 break;
842 case ':':
843 /* FALLTHRU */
844 case '_':
845 /* Escape to command mode, but be nice! */
846 _execute_command(hp, linebuf + 2, cnt - 2);
847 goto jcont;
848 case '.':
849 /* Simulate end of file on input */
850 goto jout;
851 case 'x':
852 /* Same as 'q', but no *DEAD* saving */
853 /* FALLTHRU */
854 case 'q':
855 /* Force a quit, act like an interrupt had happened */
856 ++_coll_hadintr;
857 _collint((c == 'x') ? 0 : SIGINT);
858 exit(EXIT_ERR);
859 /*NOTREACHED*/
860 case 'h':
861 /* Grab a bunch of headers */
863 grab_headers(hp, GTO | GSUBJECT | GCC | GBCC,
864 (ok_blook(bsdcompat) && ok_blook(bsdorder)));
865 while (hp->h_to == NULL);
866 goto jcont;
867 case 'H':
868 /* Grab extra headers */
870 grab_headers(hp, GEXTRA, 0);
871 while (check_from_and_sender(hp->h_from, hp->h_sender) == NULL);
872 goto jcont;
873 case 't':
874 /* Add to the To list */
875 hp->h_to = cat(hp->h_to,
876 checkaddrs(lextract(linebuf + 2, GTO | GFULL), EACM_NORMAL,
877 NULL));
878 break;
879 case 's':
880 /* Set the Subject list */
881 cp = linebuf + 2;
882 while (whitechar(*cp))
883 ++cp;
884 hp->h_subject = savestr(cp);
885 break;
886 #ifdef HAVE_DEBUG
887 case 'S':
888 c_sstats(NULL);
889 break;
890 #endif
891 case '@':
892 /* Edit the attachment list */
893 if (linebuf[2] != '\0')
894 append_attachments(&hp->h_attach, linebuf + 2);
895 else
896 edit_attachments(&hp->h_attach);
897 break;
898 case 'c':
899 /* Add to the CC list */
900 hp->h_cc = cat(hp->h_cc,
901 checkaddrs(lextract(linebuf + 2, GCC | GFULL), EACM_NORMAL,
902 NULL));
903 break;
904 case 'b':
905 /* Add stuff to blind carbon copies list */
906 hp->h_bcc = cat(hp->h_bcc,
907 checkaddrs(lextract(linebuf + 2, GBCC | GFULL), EACM_NORMAL,
908 NULL));
909 break;
910 case 'd':
911 strncpy(linebuf + 2, getdeadletter(), linesize - 2);
912 linebuf[linesize - 1] = '\0';
913 /*FALLTHRU*/
914 case 'R':
915 case 'r':
916 case '<':
917 /* Invoke a file: Search for the file name, then open it and copy the
918 * contents to _coll_fp */
919 cp = linebuf + 2;
920 while (whitechar(*cp))
921 ++cp;
922 if (*cp == '\0') {
923 n_err(_("Interpolate what file?\n"));
924 break;
926 if (*cp == '!') {
927 insertcommand(_coll_fp, cp + 1);
928 break;
930 if ((cp = file_expand(cp)) == NULL)
931 break;
932 if (is_dir(cp)) {
933 n_err(_("\"%s\": Directory\n"), cp);
934 break;
936 printf(_("\"%s\" "), cp);
937 fflush(stdout);
938 if (_include_file(cp, &lc, &cc, FAL0, (c == 'R')) != 0)
939 goto jerr;
940 printf(_("%d/%d\n"), lc, cc);
941 break;
942 case 'i':
943 /* Insert a variable into the file */
944 cp = linebuf + 2;
945 while (whitechar(*cp))
946 ++cp;
947 if ((cp = vok_vlook(cp)) == NULL || *cp == '\0')
948 break;
949 if (putesc(cp, _coll_fp) < 0)
950 goto jerr;
951 if ((options & OPT_INTERACTIVE) && putesc(cp, stdout) < 0)
952 goto jerr;
953 break;
954 case 'a':
955 case 'A':
956 /* Insert the contents of a signature variable */
957 cp = (c == 'a') ? ok_vlook(sign) : ok_vlook(Sign);
958 if (cp != NULL && *cp != '\0') {
959 if (putesc(cp, _coll_fp) < 0)
960 goto jerr;
961 if ((options & OPT_INTERACTIVE) && putesc(cp, stdout) < 0)
962 goto jerr;
964 break;
965 case 'w':
966 /* Write the message on a file */
967 cp = linebuf + 2;
968 while (blankchar(*cp))
969 ++cp;
970 if (*cp == '\0' || (cp = file_expand(cp)) == NULL) {
971 n_err(_("Write what file!?\n"));
972 break;
974 rewind(_coll_fp);
975 if (exwrite(cp, _coll_fp, 1) < 0)
976 goto jerr;
977 break;
978 case 'm':
979 case 'M':
980 case 'f':
981 case 'F':
982 case 'u':
983 case 'U':
984 /* Interpolate the named messages, if we are in receiving mail mode.
985 * Does the standard list processing garbage. If ~f is given, we
986 * don't shift over */
987 if (forward(linebuf + 2, _coll_fp, c) < 0)
988 goto jerr;
989 goto jcont;
990 case 'p':
991 /* Print current state of the message without altering anything */
992 print_collf(_coll_fp, hp);
993 goto jcont;
994 case '|':
995 /* Pipe message through command. Collect output as new message */
996 rewind(_coll_fp);
997 mespipe(linebuf + 2);
998 goto jcont;
999 case 'v':
1000 case 'e':
1001 /* Edit the current message. 'e' -> use EDITOR, 'v' -> use VISUAL */
1002 rewind(_coll_fp);
1003 mesedit(c, ok_blook(editheaders) ? hp : NULL);
1004 goto jcont;
1005 case '?':
1006 /* Last the lengthy help string. (Very ugly, but take care for
1007 * compiler supported string lengths :() */
1008 puts(_(
1009 "TILDE ESCAPES (to be placed after a newline) excerpt:\n"
1010 "~. Commit and send message\n"
1011 "~: <command> Execute a mail command\n"
1012 "~<! <command> Insert output of command\n"
1013 "~@ [<files>] Edit attachment list\n"
1014 "~A Insert *Sign* variable (`~a' inserts *sign*)\n"
1015 "~c <users> Add users to Cc: list (`~b' for Bcc:)\n"
1016 "~d Read in *DEAD* (dead.letter)\n"
1017 "~e Edit message via *EDITOR*"
1019 puts(_(
1020 "~F <msglist> Read in with headers, don't *indentprefix* lines\n"
1021 "~f <msglist> Like ~F, but honour `ignore' / `retain' configuration\n"
1022 "~H Edit From:, Reply-To: and Sender:\n"
1023 "~h Prompt for Subject:, To:, Cc: and \"blind\" Bcc:\n"
1024 "~i <variable> Insert a value and a newline\n"
1025 "~M <msglist> Read in with headers, *indentprefix* (`~m': `retain' etc.)\n"
1026 "~p Print current message compose buffer\n"
1027 "~r <file> Read in a file (`~R' *indentprefix* lines)"
1029 puts(_(
1030 "~s <subject> Set Subject:\n"
1031 "~t <users> Add users to To: list\n"
1032 "~u <msglist> Read in message(s) without headers (`~U' indents lines)\n"
1033 "~v Edit message via *VISUAL*\n"
1034 "~w <file> Write message onto file\n"
1035 "~x Abort composition, discard message (`~q' saves in *DEAD*)\n"
1036 "~| <command> Pipe message through shell filter"
1038 break;
1042 jout:
1043 /* Place signature? */
1044 if((cp = ok_vlook(signature)) != NULL && *cp != '\0'){
1045 if((quote = file_expand(cp)) == NULL){
1046 n_err(_("*signature* expands to invalid file: \"%s\"\n"), cp);
1047 goto jerr;
1049 if((sigfp = Fopen(cp = quote, "r")) == NULL){
1050 n_err(_("Can't open *signature* \"%s\": %s\n"), cp, strerror(errno));
1051 goto jerr;
1054 if(linebuf == NULL)
1055 linebuf = smalloc(linesize = LINESIZE);
1056 c = '\0';
1057 while((i = fread(linebuf, sizeof *linebuf, linesize, UNVOLATILE(sigfp)))
1058 > 0){
1059 c = linebuf[i - 1];
1060 if(i != fwrite(linebuf, sizeof *linebuf, i, _coll_fp))
1061 goto jerr;
1064 /* C99 */{
1065 FILE *x = UNVOLATILE(sigfp);
1066 int e = errno, ise = ferror(x);
1068 sigfp = NULL;
1069 Fclose(x);
1071 if(ise){
1072 n_err(_("Errors while reading *signature* \"%s\": %s\n"),
1073 cp, strerror(e));
1074 goto jerr;
1078 if(c != '\0' && c != '\n')
1079 putc('\n', _coll_fp);
1082 { char const *cp_obsolete = ok_vlook(NAIL_TAIL);
1084 if(cp_obsolete != NULL)
1085 OBSOLETE(_("please use *message-inject-tail* instead of *NAIL_TAIL*"));
1087 if((cp = ok_vlook(message_inject_tail)) != NULL ||
1088 (cp = cp_obsolete) != NULL){
1089 if(putesc(cp, _coll_fp) < 0)
1090 goto jerr;
1091 if((options & OPT_INTERACTIVE) && putesc(cp, stdout) < 0)
1092 goto jerr;
1096 if(fflush(_coll_fp))
1097 goto jerr;
1098 rewind(_coll_fp);
1100 jleave:
1101 if (linebuf != NULL)
1102 free(linebuf);
1103 handlerpop();
1104 --noreset;
1105 sigemptyset(&nset);
1106 sigaddset(&nset, SIGINT);
1107 sigaddset(&nset, SIGHUP);
1108 sigprocmask(SIG_BLOCK, &nset, NULL);
1109 safe_signal(SIGINT, _coll_saveint);
1110 safe_signal(SIGHUP, _coll_savehup);
1111 safe_signal(SIGTSTP, _coll_savetstp);
1112 safe_signal(SIGTTOU, _coll_savettou);
1113 safe_signal(SIGTTIN, _coll_savettin);
1114 sigprocmask(SIG_SETMASK, &oset, NULL);
1115 NYD_LEAVE;
1116 return _coll_fp;
1118 jerr:
1119 if(sigfp != NULL)
1120 Fclose(UNVOLATILE(sigfp));
1121 if (_coll_fp != NULL) {
1122 Fclose(_coll_fp);
1123 _coll_fp = NULL;
1125 goto jleave;
1128 FL void
1129 savedeadletter(FILE *fp, int fflush_rewind_first)
1131 char const *cp;
1132 int c;
1133 FILE *dbuf;
1134 ul_i lines, bytes;
1135 NYD_ENTER;
1137 if ((options & OPT_DEBUG) || !ok_blook(save))
1138 goto jleave;
1140 if (fflush_rewind_first) {
1141 fflush(fp);
1142 rewind(fp);
1144 if (fsize(fp) == 0)
1145 goto jleave;
1147 cp = getdeadletter();
1148 c = umask(077);
1149 dbuf = Fopen(cp, "a");
1150 umask(c);
1151 if (dbuf == NULL)
1152 goto jleave;
1154 really_rewind(fp);
1156 printf("\"%s\" ", cp);
1157 for (lines = bytes = 0; (c = getc(fp)) != EOF; ++bytes) {
1158 putc(c, dbuf);
1159 if (c == '\n')
1160 ++lines;
1162 printf("%lu/%lu\n", lines, bytes);
1164 Fclose(dbuf);
1165 rewind(fp);
1166 jleave:
1167 NYD_LEAVE;
1170 /* s-it-mode */