IMAP: [a65afca] (Add *imap-delim{,-{{,USER@}HOST}}*) for real now
[s-mailx.git] / collect.c
blobfa9081d225bf3b40149cc5cb316c2f52a06c72af
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 (IS_TTY_SESSION() && (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);
262 m += (hp->h_organization != NULL || ok_vlook(ORGANIZATION) != NULL);
264 l = (*cp == '\0') ? screensize() : atoi(cp);
265 if (m > l)
266 goto jpager;
267 l -= m;
269 for (m = 0; fgetline(&lbuf, &linesize, &cnt2, NULL, cf, 0); ++m)
271 rewind(cf);
272 if (l < m) {
273 jpager:
274 cp = get_pager(NULL);
275 if (sigsetjmp(_coll_pipejmp, 1))
276 goto jendpipe;
277 obuf = Popen(cp, "w", NULL, NULL, 1);
278 if (obuf == NULL) {
279 n_perr(cp, 0);
280 obuf = stdout;
281 } else
282 safe_signal(SIGPIPE, &_collect_onpipe);
286 fprintf(obuf, _("-------\nMessage contains:\n"));
287 gf = GIDENT | GTO | GSUBJECT | GCC | GBCC | GNL | GFILES | GCOMMA;
288 puthead(hp, obuf, gf, SEND_TODISP, CONV_NONE, NULL, NULL);
289 while (fgetline(&lbuf, &linesize, &cnt, &linelen, cf, 1))
290 prout(lbuf, linelen, obuf);
291 if (hp->h_attach != NULL) {
292 fputs(_("-------\nAttachments:\n"), obuf);
293 for (ap = hp->h_attach; ap != NULL; ap = ap->a_flink) {
294 if (ap->a_msgno)
295 fprintf(obuf, " - message %u\n", ap->a_msgno);
296 else {
297 /* TODO after MIME/send layer rewrite we *know*
298 * TODO the details of the attachment here,
299 * TODO so adjust this again, then */
300 char const *cs, *csi = "-> ";
302 if ((cs = ap->a_charset) == NULL &&
303 (csi = "<- ", cs = ap->a_input_charset) == NULL)
304 cs = charset_get_lc();
305 if ((cp = ap->a_content_type) == NULL)
306 cp = "?";
307 else if (ascncasecmp(cp, "text/", 5))
308 csi = "";
309 fprintf(obuf, " - [%s, %s%s] %s\n", cp, csi, cs, ap->a_name);
314 jendpipe:
315 if (obuf != stdout) {
316 safe_signal(SIGPIPE, SIG_IGN);
317 Pclose(obuf, TRU1);
318 safe_signal(SIGPIPE, dflpipe);
320 if (lbuf != NULL)
321 free(lbuf);
322 safe_signal(SIGINT, sigint);
323 NYD_LEAVE;
326 static int
327 exwrite(char const *name, FILE *fp, int f)
329 FILE *of;
330 int c, lc, rv = -1;
331 long cc;
332 NYD_ENTER;
334 if (f) {
335 printf("\"%s\" ", name);
336 fflush(stdout);
338 if ((of = Fopen(name, "a")) == NULL) {
339 n_perr(NULL, 0);
340 goto jleave;
343 lc = 0;
344 cc = 0;
345 while ((c = getc(fp)) != EOF) {
346 ++cc;
347 if (c == '\n')
348 ++lc;
349 putc(c, of);
350 if (ferror(of)) {
351 n_perr(name, 0);
352 Fclose(of);
353 goto jleave;
356 Fclose(of);
357 printf(_("%d/%ld\n"), lc, cc);
358 fflush(stdout);
359 rv = 0;
360 jleave:
361 NYD_LEAVE;
362 return rv;
365 static enum okay
366 makeheader(FILE *fp, struct header *hp, si8_t *checkaddr_err)
368 FILE *nf;
369 int c;
370 enum okay rv = STOP;
371 NYD_ENTER;
373 if ((nf = Ftmp(NULL, "colhead", OF_RDWR | OF_UNLINK | OF_REGISTER, 0600)) ==
374 NULL) {
375 n_perr(_("temporary mail edit file"), 0);
376 goto jleave;
379 extract_header(fp, hp, checkaddr_err);
381 while ((c = getc(fp)) != EOF) /* XXX bytewise, yuck! */
382 putc(c, nf);
383 if (fp != _coll_fp)
384 Fclose(_coll_fp);
385 Fclose(fp);
386 _coll_fp = nf;
387 if (check_from_and_sender(hp->h_from, hp->h_sender) == NULL)
388 goto jleave;
389 rv = OKAY;
390 jleave:
391 NYD_LEAVE;
392 return rv;
395 static void
396 mesedit(int c, struct header *hp)
398 bool_t saved;
399 sighandler_type sigint;
400 FILE *nf;
401 NYD_ENTER;
403 saved = ok_blook(add_file_recipients);
404 ok_bset(add_file_recipients, TRU1);
406 sigint = safe_signal(SIGINT, SIG_IGN);
407 nf = run_editor(_coll_fp, (off_t)-1, c, 0, hp, NULL, SEND_MBOX, sigint);
408 if (nf != NULL) {
409 if (hp) {
410 rewind(nf);
411 makeheader(nf, hp, NULL);
412 } else {
413 fseek(nf, 0L, SEEK_END);
414 Fclose(_coll_fp);
415 _coll_fp = nf;
418 safe_signal(SIGINT, sigint);
420 ok_bset(add_file_recipients, saved);
421 NYD_LEAVE;
424 static void
425 mespipe(char *cmd)
427 FILE *nf;
428 sighandler_type sigint;
429 char const *sh;
430 NYD_ENTER;
432 sigint = safe_signal(SIGINT, SIG_IGN);
434 if ((nf = Ftmp(NULL, "colpipe", OF_RDWR | OF_UNLINK | OF_REGISTER, 0600)) ==
435 NULL) {
436 n_perr(_("temporary mail edit file"), 0);
437 goto jout;
440 /* stdin = current message. stdout = new message */
441 if ((sh = ok_vlook(SHELL)) == NULL)
442 sh = XSHELL;
443 fflush(_coll_fp);
444 if (run_command(sh, 0, fileno(_coll_fp), fileno(nf), "-c", cmd, NULL) < 0) {
445 Fclose(nf);
446 goto jout;
449 if (fsize(nf) == 0) {
450 n_err(_("No bytes from \"%s\" !?\n"), cmd);
451 Fclose(nf);
452 goto jout;
455 /* Take new files */
456 fseek(nf, 0L, SEEK_END);
457 Fclose(_coll_fp);
458 _coll_fp = nf;
459 jout:
460 safe_signal(SIGINT, sigint);
461 NYD_LEAVE;
464 static int
465 forward(char *ms, FILE *fp, int f)
467 int *msgvec, rv = 0;
468 struct ignoretab *ig;
469 char const *tabst;
470 enum sendaction action;
471 NYD_ENTER;
473 msgvec = salloc((size_t)(msgCount + 1) * sizeof *msgvec);
474 if (getmsglist(ms, msgvec, 0) < 0)
475 goto jleave;
476 if (*msgvec == 0) {
477 *msgvec = first(0, MMNORM);
478 if (*msgvec == 0) {
479 n_err(_("No appropriate messages\n"));
480 goto jleave;
482 msgvec[1] = 0;
485 if (f == 'f' || f == 'F' || f == 'u')
486 tabst = NULL;
487 else if ((tabst = ok_vlook(indentprefix)) == NULL)
488 tabst = INDENT_DEFAULT;
489 if (f == 'u' || f == 'U')
490 ig = allignore;
491 else
492 ig = upperchar(f) ? NULL : ignore;
493 action = (upperchar(f) && f != 'U') ? SEND_QUOTE_ALL : SEND_QUOTE;
495 printf(_("Interpolating:"));
496 for (; *msgvec != 0; ++msgvec) {
497 struct message *mp = message + *msgvec - 1;
499 touch(mp);
500 printf(" %d", *msgvec);
501 fflush(stdout);
502 if (sendmp(mp, fp, ig, tabst, action, NULL) < 0) {
503 n_perr(_("temporary mail file"), 0);
504 rv = -1;
505 break;
508 printf("\n");
509 jleave:
510 NYD_LEAVE;
511 return rv;
514 static void
515 collstop(int s)
517 sighandler_type old_action;
518 sigset_t nset;
519 NYD_X; /* Signal handler */
521 old_action = safe_signal(s, SIG_DFL);
523 sigemptyset(&nset);
524 sigaddset(&nset, s);
525 sigprocmask(SIG_UNBLOCK, &nset, NULL);
526 n_raise(s);
527 sigprocmask(SIG_BLOCK, &nset, NULL);
529 safe_signal(s, old_action);
530 if (_coll_jmp_p) {
531 _coll_jmp_p = 0;
532 _coll_hadintr = 0;
533 siglongjmp(_coll_jmp, 1);
537 static void
538 _collint(int s)
540 NYD_X; /* Signal handler */
542 /* the control flow is subtle, because we can be called from ~q */
543 if (_coll_hadintr == 0) {
544 if (ok_blook(ignore)) {
545 puts("@");
546 fflush(stdout);
547 clearerr(stdin);
548 } else
549 _coll_hadintr = 1;
550 siglongjmp(_coll_jmp, 1);
552 exit_status |= EXIT_SEND_ERROR;
553 if (s != 0)
554 savedeadletter(_coll_fp, 1);
555 /* Aborting message, no need to fflush() .. */
556 siglongjmp(_coll_abort, 1);
559 static void
560 collhup(int s)
562 NYD_X; /* Signal handler */
563 UNUSED(s);
565 savedeadletter(_coll_fp, 1);
566 /* Let's pretend nobody else wants to clean up, a true statement at
567 * this time */
568 exit(EXIT_ERR);
571 static int
572 putesc(char const *s, FILE *stream)
574 int n = 0, rv = -1;
575 NYD_ENTER;
577 while (s[0] != '\0') {
578 if (s[0] == '\\') {
579 if (s[1] == 't') {
580 if (putc('\t', stream) == EOF)
581 goto jleave;
582 ++n;
583 s += 2;
584 continue;
586 if (s[1] == 'n') {
587 if (putc('\n', stream) == EOF)
588 goto jleave;
589 ++n;
590 s += 2;
591 continue;
594 if (putc(s[0], stream) == EOF)
595 goto jleave;
596 ++n;
597 ++s;
599 if (putc('\n', stream) == EOF)
600 goto jleave;
601 rv = ++n;
602 jleave:
603 NYD_LEAVE;
604 return rv;
607 FL FILE *
608 collect(struct header *hp, int printheaders, struct message *mp,
609 char *quotefile, int doprefix, si8_t *checkaddr_err)
611 struct ignoretab *quoteig;
612 int lc, cc, c, t;
613 int volatile escape, getfields;
614 char *linebuf, *quote;
615 char const *cp;
616 size_t i, linesize; /* TODO line pool */
617 long cnt;
618 enum sendaction action;
619 sigset_t oset, nset;
620 sighandler_type savedtop;
621 FILE * volatile sigfp;
622 NYD_ENTER;
624 _coll_fp = NULL;
625 sigfp = NULL;
626 linesize = 0;
627 linebuf = quote = NULL;
629 /* Start catching signals from here, but we're still die on interrupts
630 * until we're in the main loop */
631 sigemptyset(&nset);
632 sigaddset(&nset, SIGINT);
633 sigaddset(&nset, SIGHUP);
634 sigprocmask(SIG_BLOCK, &nset, &oset);
635 handlerpush(&_collint);
636 if ((_coll_saveint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
637 safe_signal(SIGINT, &_collint);
638 if ((_coll_savehup = safe_signal(SIGHUP, SIG_IGN)) != SIG_IGN)
639 safe_signal(SIGHUP, collhup);
640 /* TODO We do a lot of redundant signal handling, especially
641 * TODO with the command line editor(s); try to merge this */
642 _coll_savetstp = safe_signal(SIGTSTP, collstop);
643 _coll_savettou = safe_signal(SIGTTOU, collstop);
644 _coll_savettin = safe_signal(SIGTTIN, collstop);
645 if (sigsetjmp(_coll_abort, 1))
646 goto jerr;
647 if (sigsetjmp(_coll_jmp, 1))
648 goto jerr;
649 sigprocmask(SIG_SETMASK, &oset, (sigset_t*)NULL);
651 ++noreset;
652 if ((_coll_fp = Ftmp(NULL, "collect", OF_RDWR | OF_UNLINK | OF_REGISTER,
653 0600)) == NULL) {
654 n_perr(_("temporary mail file"), 0);
655 goto jerr;
658 if ((cp = ok_vlook(NAIL_HEAD)) != NULL && putesc(cp, _coll_fp) < 0)
659 goto jerr;
661 /* If we are going to prompt for a subject, refrain from printing a newline
662 * after the headers (since some people mind) */
663 getfields = 0;
664 if (!(options & OPT_t_FLAG)) {
665 t = GTO | GSUBJECT | GCC | GNL;
666 if (ok_blook(fullnames))
667 t |= GCOMMA;
669 if (options & OPT_INTERACTIVE) {
670 if (hp->h_subject == NULL && (ok_blook(ask) || ok_blook(asksub)))
671 t &= ~GNL, getfields |= GSUBJECT;
673 if (hp->h_to == NULL)
674 t &= ~GNL, getfields |= GTO;
676 if (!ok_blook(bsdcompat) && !ok_blook(askatend)) {
677 if (hp->h_bcc == NULL && ok_blook(askbcc))
678 t &= ~GNL, getfields |= GBCC;
679 if (hp->h_cc == NULL && ok_blook(askcc))
680 t &= ~GNL, getfields |= GCC;
684 if (printheaders && !ok_blook(editalong)) {
685 puthead(hp, stdout, t, SEND_TODISP, CONV_NONE, NULL, NULL);
686 fflush(stdout);
690 /* Quote an original message */
691 if (mp != NULL && (doprefix || (quote = ok_vlook(quote)) != NULL)) {
692 quoteig = allignore;
693 action = SEND_QUOTE;
694 if (doprefix) {
695 quoteig = fwdignore;
696 if ((cp = ok_vlook(fwdheading)) == NULL)
697 cp = "-------- Original Message --------";
698 if (*cp != '\0' && fprintf(_coll_fp, "%s\n", cp) < 0)
699 goto jerr;
700 } else if (!strcmp(quote, "noheading")) {
701 /*EMPTY*/;
702 } else if (!strcmp(quote, "headers")) {
703 quoteig = ignore;
704 } else if (!strcmp(quote, "allheaders")) {
705 quoteig = NULL;
706 action = SEND_QUOTE_ALL;
707 } else {
708 cp = hfield1("from", mp);
709 if (cp != NULL && (cnt = (long)strlen(cp)) > 0) {
710 if (xmime_write(cp, cnt, _coll_fp, CONV_FROMHDR, TD_NONE) < 0)
711 goto jerr;
712 if (fprintf(_coll_fp, _(" wrote:\n\n")) < 0)
713 goto jerr;
716 if (fflush(_coll_fp))
717 goto jerr;
718 if (doprefix)
719 cp = NULL;
720 else if ((cp = ok_vlook(indentprefix)) == NULL)
721 cp = INDENT_DEFAULT;
722 if (sendmp(mp, _coll_fp, quoteig, cp, action, NULL) < 0)
723 goto jerr;
726 /* Print what we have sofar also on the terminal (if useful) */
727 if ((options & OPT_INTERACTIVE) && !ok_blook(editalong)) {
728 rewind(_coll_fp);
729 while ((c = getc(_coll_fp)) != EOF) /* XXX bytewise, yuck! */
730 putc(c, stdout);
731 if (fseek(_coll_fp, 0, SEEK_END))
732 goto jerr;
733 /* Ensure this is clean xxx not really necessary? */
734 fflush(stdout);
737 escape = ((cp = ok_vlook(escape)) != NULL) ? *cp : ESCAPE;
738 _coll_hadintr = 0;
740 if (!sigsetjmp(_coll_jmp, 1)) {
741 if (getfields)
742 grab_headers(hp, getfields, 1);
743 if (quotefile != NULL) {
744 if (_include_file(quotefile, &lc, &cc, TRU1, FAL0) != 0)
745 goto jerr;
747 if ((options & OPT_INTERACTIVE) && ok_blook(editalong)) {
748 rewind(_coll_fp);
749 mesedit('e', hp);
750 goto jcont;
752 } else {
753 /* Come here for printing the after-signal message. Duplicate messages
754 * won't be printed because the write is aborted if we get a SIGTTOU */
755 if (_coll_hadintr) {
756 n_err(_("\n(Interrupt -- one more to kill letter)\n"));
757 } else {
758 jcont:
759 printf(_("(continue)\n"));
760 fflush(stdout);
764 /* No tilde escapes, interrupts not expected. Simply copy STDIN */
765 if (!(options & (OPT_INTERACTIVE | OPT_t_FLAG | OPT_TILDE_FLAG))) {
766 linebuf = srealloc(linebuf, linesize = LINESIZE);
767 while ((i = fread(linebuf, sizeof *linebuf, linesize, stdin)) > 0) {
768 if (i != fwrite(linebuf, sizeof *linebuf, i, _coll_fp))
769 goto jerr;
771 goto jout;
774 /* The interactive collect loop.
775 * All commands which come here are forbidden when sourcing! */
776 assert(_coll_hadintr || !(pstate & PS_SOURCING));
777 for (;;) {
778 _coll_jmp_p = 1;
779 cnt = readline_input("", FAL0, &linebuf, &linesize, NULL);
780 _coll_jmp_p = 0;
782 if (cnt < 0) {
783 assert(!(pstate & PS_SOURCING));
784 if (options & OPT_t_FLAG) {
785 fflush_rewind(_coll_fp);
786 /* It is important to set PS_t_FLAG before extract_header() *and*
787 * keep OPT_t_FLAG for the first parse of the message, too! */
788 pstate |= PS_t_FLAG;
789 if (makeheader(_coll_fp, hp, checkaddr_err) != OKAY)
790 goto jerr;
791 options &= ~OPT_t_FLAG;
792 continue;
793 } else if ((options & OPT_INTERACTIVE) && ok_blook(ignoreeof)) {
794 printf(_("*ignoreeof* set, use \"~.\" to terminate letter\n"));
795 continue;
797 break;
800 _coll_hadintr = 0;
802 if (cnt == 0 || !(options & (OPT_INTERACTIVE | OPT_TILDE_FLAG))) {
803 jputline:
804 /* TODO calls putline(), which *always* appends LF;
805 * TODO thus, STDIN with -t will ALWAYS end with LF,
806 * TODO even if no trailing LF and QP encoding.
807 * TODO when finally changed, update cc-test.sh */
808 if (putline(_coll_fp, linebuf, cnt) < 0)
809 goto jerr;
810 continue;
811 } else if (linebuf[0] == '.') {
812 if (linebuf[1] == '\0' && (ok_blook(dot) || ok_blook(ignoreeof)))
813 break;
815 if (linebuf[0] != escape)
816 goto jputline;
818 if (!(options & OPT_t_FLAG))
819 tty_addhist(linebuf, TRU1);
821 c = linebuf[1];
822 switch (c) {
823 default:
824 /* On double escape, send a single one. Otherwise, it's an error */
825 if (c == escape) {
826 if (putline(_coll_fp, linebuf + 1, cnt - 1) < 0)
827 goto jerr;
828 else
829 break;
831 n_err(_("Unknown tilde escape: ~%c\n"), asciichar(c) ? c : '?');
832 break;
833 case '!':
834 /* Shell escape, send the balance of line to sh -c */
835 c_shell(linebuf + 2);
836 break;
837 case ':':
838 /* FALLTHRU */
839 case '_':
840 /* Escape to command mode, but be nice! */
841 _execute_command(hp, linebuf + 2, cnt - 2);
842 goto jcont;
843 case '.':
844 /* Simulate end of file on input */
845 goto jout;
846 case 'x':
847 /* Same as 'q', but no *DEAD* saving */
848 /* FALLTHRU */
849 case 'q':
850 /* Force a quit, act like an interrupt had happened */
851 ++_coll_hadintr;
852 _collint((c == 'x') ? 0 : SIGINT);
853 exit(EXIT_ERR);
854 /*NOTREACHED*/
855 case 'h':
856 /* Grab a bunch of headers */
858 grab_headers(hp, GTO | GSUBJECT | GCC | GBCC,
859 (ok_blook(bsdcompat) && ok_blook(bsdorder)));
860 while (hp->h_to == NULL);
861 goto jcont;
862 case 'H':
863 /* Grab extra headers */
865 grab_headers(hp, GEXTRA, 0);
866 while (check_from_and_sender(hp->h_from, hp->h_sender) == NULL);
867 goto jcont;
868 case 't':
869 /* Add to the To list */
870 hp->h_to = cat(hp->h_to,
871 checkaddrs(lextract(linebuf + 2, GTO | GFULL), EACM_NORMAL,
872 NULL));
873 break;
874 case 's':
875 /* Set the Subject list */
876 cp = linebuf + 2;
877 while (whitechar(*cp))
878 ++cp;
879 hp->h_subject = savestr(cp);
880 break;
881 #ifdef HAVE_DEBUG
882 case 'S':
883 c_sstats(NULL);
884 break;
885 #endif
886 case '@':
887 /* Edit the attachment list */
888 if (linebuf[2] != '\0')
889 append_attachments(&hp->h_attach, linebuf + 2);
890 else
891 edit_attachments(&hp->h_attach);
892 break;
893 case 'c':
894 /* Add to the CC list */
895 hp->h_cc = cat(hp->h_cc,
896 checkaddrs(lextract(linebuf + 2, GCC | GFULL), EACM_NORMAL,
897 NULL));
898 break;
899 case 'b':
900 /* Add stuff to blind carbon copies list */
901 hp->h_bcc = cat(hp->h_bcc,
902 checkaddrs(lextract(linebuf + 2, GBCC | GFULL), EACM_NORMAL,
903 NULL));
904 break;
905 case 'd':
906 strncpy(linebuf + 2, getdeadletter(), linesize - 2);
907 linebuf[linesize - 1] = '\0';
908 /*FALLTHRU*/
909 case 'R':
910 case 'r':
911 case '<':
912 /* Invoke a file: Search for the file name, then open it and copy the
913 * contents to _coll_fp */
914 cp = linebuf + 2;
915 while (whitechar(*cp))
916 ++cp;
917 if (*cp == '\0') {
918 n_err(_("Interpolate what file?\n"));
919 break;
921 if (*cp == '!') {
922 insertcommand(_coll_fp, cp + 1);
923 break;
925 if ((cp = file_expand(cp)) == NULL)
926 break;
927 if (is_dir(cp)) {
928 n_err(_("\"%s\": Directory\n"), cp);
929 break;
931 printf(_("\"%s\" "), cp);
932 fflush(stdout);
933 if (_include_file(cp, &lc, &cc, FAL0, (c == 'R')) != 0)
934 goto jerr;
935 printf(_("%d/%d\n"), lc, cc);
936 break;
937 case 'i':
938 /* Insert a variable into the file */
939 cp = linebuf + 2;
940 while (whitechar(*cp))
941 ++cp;
942 if ((cp = vok_vlook(cp)) == NULL || *cp == '\0')
943 break;
944 if (putesc(cp, _coll_fp) < 0)
945 goto jerr;
946 if ((options & OPT_INTERACTIVE) && putesc(cp, stdout) < 0)
947 goto jerr;
948 break;
949 case 'a':
950 case 'A':
951 /* Insert the contents of a signature variable */
952 cp = (c == 'a') ? ok_vlook(sign) : ok_vlook(Sign);
953 if (cp != NULL && *cp != '\0') {
954 if (putesc(cp, _coll_fp) < 0)
955 goto jerr;
956 if ((options & OPT_INTERACTIVE) && putesc(cp, stdout) < 0)
957 goto jerr;
959 break;
960 case 'w':
961 /* Write the message on a file */
962 cp = linebuf + 2;
963 while (blankchar(*cp))
964 ++cp;
965 if (*cp == '\0' || (cp = file_expand(cp)) == NULL) {
966 n_err(_("Write what file!?\n"));
967 break;
969 rewind(_coll_fp);
970 if (exwrite(cp, _coll_fp, 1) < 0)
971 goto jerr;
972 break;
973 case 'm':
974 case 'M':
975 case 'f':
976 case 'F':
977 case 'u':
978 case 'U':
979 /* Interpolate the named messages, if we are in receiving mail mode.
980 * Does the standard list processing garbage. If ~f is given, we
981 * don't shift over */
982 if (forward(linebuf + 2, _coll_fp, c) < 0)
983 goto jerr;
984 goto jcont;
985 case 'p':
986 /* Print current state of the message without altering anything */
987 print_collf(_coll_fp, hp);
988 goto jcont;
989 case '|':
990 /* Pipe message through command. Collect output as new message */
991 rewind(_coll_fp);
992 mespipe(linebuf + 2);
993 goto jcont;
994 case 'v':
995 case 'e':
996 /* Edit the current message. 'e' -> use EDITOR, 'v' -> use VISUAL */
997 rewind(_coll_fp);
998 mesedit(c, ok_blook(editheaders) ? hp : NULL);
999 goto jcont;
1000 case '?':
1001 /* Last the lengthy help string. (Very ugly, but take care for
1002 * compiler supported string lengths :() */
1003 puts(_(
1004 "TILDE ESCAPES (to be placed after a newline) excerpt:\n"
1005 "~. Commit and send message\n"
1006 "~: <command> Execute a mail command\n"
1007 "~<! <command> Insert output of command\n"
1008 "~@ [<files>] Edit attachment list\n"
1009 "~A Insert *Sign* variable (`~a' inserts *sign*)\n"
1010 "~c <users> Add users to Cc: list (`~b' for Bcc:)\n"
1011 "~d Read in *DEAD* (dead.letter)\n"
1012 "~e Edit message via *EDITOR*"
1014 puts(_(
1015 "~F <msglist> Read in with headers, don't *indentprefix* lines\n"
1016 "~f <msglist> Like ~F, but honour `ignore' / `retain' configuration\n"
1017 "~H Edit From:, Reply-To: and Sender:\n"
1018 "~h Prompt for Subject:, To:, Cc: and \"blind\" Bcc:\n"
1019 "~i <variable> Insert a value and a newline\n"
1020 "~M <msglist> Read in with headers, *indentprefix* (`~m': `retain' etc.)\n"
1021 "~p Print current message compose buffer\n"
1022 "~r <file> Read in a file (`~R' *indentprefix* lines)"
1024 puts(_(
1025 "~s <subject> Set Subject:\n"
1026 "~t <users> Add users to To: list\n"
1027 "~u <msglist> Read in message(s) without headers (`~U' indents lines)\n"
1028 "~v Edit message via *VISUAL*\n"
1029 "~w <file> Write message onto file\n"
1030 "~x Abort composition, discard message (`~q' saves in *DEAD*)\n"
1031 "~| <command> Pipe message through shell filter"
1033 break;
1037 jout:
1038 /* Place signature? */
1039 if((cp = ok_vlook(signature)) != NULL && *cp != '\0'){
1040 if((quote = file_expand(cp)) == NULL){
1041 n_err(_("*signature* expands to invalid file: \"%s\"\n"), cp);
1042 goto jerr;
1044 if((sigfp = Fopen(cp = quote, "r")) == NULL){
1045 n_err(_("Can't open *signature* \"%s\": %s\n"), cp, strerror(errno));
1046 goto jerr;
1049 if(linebuf == NULL)
1050 linebuf = smalloc(linesize = LINESIZE);
1051 c = '\0';
1052 while((i = fread(linebuf, sizeof *linebuf, linesize, UNVOLATILE(sigfp)))
1053 > 0){
1054 c = linebuf[i - 1];
1055 if(i != fwrite(linebuf, sizeof *linebuf, i, _coll_fp))
1056 goto jerr;
1059 /* C99 */{
1060 FILE *x = UNVOLATILE(sigfp);
1061 int e = errno, ise = ferror(x);
1063 sigfp = NULL;
1064 Fclose(x);
1066 if(ise){
1067 n_err(_("Errors while reading *signature* \"%s\": %s\n"),
1068 cp, strerror(e));
1069 goto jerr;
1073 if(c != '\0' && c != '\n')
1074 putc('\n', _coll_fp);
1077 if(fflush(_coll_fp))
1078 goto jerr;
1080 if ((cp = ok_vlook(NAIL_TAIL)) != NULL) {
1081 if (putesc(cp, _coll_fp) < 0)
1082 goto jerr;
1083 if ((options & OPT_INTERACTIVE) && putesc(cp, stdout) < 0)
1084 goto jerr;
1086 rewind(_coll_fp);
1088 jleave:
1089 if (linebuf != NULL)
1090 free(linebuf);
1091 handlerpop();
1092 --noreset;
1093 sigemptyset(&nset);
1094 sigaddset(&nset, SIGINT);
1095 sigaddset(&nset, SIGHUP);
1096 sigprocmask(SIG_BLOCK, &nset, NULL);
1097 safe_signal(SIGINT, _coll_saveint);
1098 safe_signal(SIGHUP, _coll_savehup);
1099 safe_signal(SIGTSTP, _coll_savetstp);
1100 safe_signal(SIGTTOU, _coll_savettou);
1101 safe_signal(SIGTTIN, _coll_savettin);
1102 sigprocmask(SIG_SETMASK, &oset, NULL);
1103 NYD_LEAVE;
1104 return _coll_fp;
1106 jerr:
1107 if(sigfp != NULL)
1108 Fclose(UNVOLATILE(sigfp));
1109 if (_coll_fp != NULL) {
1110 Fclose(_coll_fp);
1111 _coll_fp = NULL;
1113 goto jleave;
1116 FL void
1117 savedeadletter(FILE *fp, int fflush_rewind_first)
1119 char const *cp;
1120 int c;
1121 FILE *dbuf;
1122 ul_i lines, bytes;
1123 NYD_ENTER;
1125 if ((options & OPT_DEBUG) || !ok_blook(save))
1126 goto jleave;
1128 if (fflush_rewind_first) {
1129 fflush(fp);
1130 rewind(fp);
1132 if (fsize(fp) == 0)
1133 goto jleave;
1135 cp = getdeadletter();
1136 c = umask(077);
1137 dbuf = Fopen(cp, "a");
1138 umask(c);
1139 if (dbuf == NULL)
1140 goto jleave;
1142 really_rewind(fp);
1144 printf("\"%s\" ", cp);
1145 for (lines = bytes = 0; (c = getc(fp)) != EOF; ++bytes) {
1146 putc(c, dbuf);
1147 if (c == '\n')
1148 ++lines;
1150 printf("%lu/%lu\n", lines, bytes);
1152 Fclose(dbuf);
1153 rewind(fp);
1154 jleave:
1155 NYD_LEAVE;
1158 /* s-it-mode */