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>.
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
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
36 #define n_FILE collect
38 #ifndef HAVE_AMALGAMATION
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 `~:', `~_' and some hooks; hp may be NULL */
58 static void _execute_command(struct header
*hp
, char const *linebuf
,
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
);
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
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 /* Call the hook macname */
106 static void a_coll_call_hook(struct header
*hp
, char const *macname
);
109 _execute_command(struct header
*hp
, char const *linebuf
, size_t linesize
){
110 /* The problem arises if there are rfc822 message attachments and the
111 * user uses `~:' to change the current file. TODO Unfortunately we
112 * TODO cannot simply keep a pointer to, or increment a reference count
113 * TODO of the current `file' (mailbox that is) object, because the
114 * TODO codebase doesn't deal with that at all; so, until some far
115 * TODO later time, copy the name of the path, and warn the user if it
116 * TODO changed; we COULD use the AC_TMPFILE attachment type, i.e.,
117 * TODO copy the message attachments over to temporary files, but that
118 * TODO would require more changes so that the user still can recognize
119 * TODO in `~@' etc. that its a rfc822 message attachment; see below */
121 struct attachment
*ap
;
128 n_SIGMAN_ENTER_SWITCH(&sm
, n_SIGMAN_ALL
){
135 /* If the above todo is worked, remove or outsource to attachments.c! */
136 if(hp
!= NULL
&& (ap
= hp
->h_attach
) != NULL
) do
138 mnbuf
= sstrdup(mailname
);
141 while((ap
= ap
->a_flink
) != NULL
);
143 n_source_command(linebuf
);
145 n_sigman_cleanup_ping(&sm
);
148 if(strcmp(mnbuf
, mailname
))
149 n_err(_("Mailbox changed: it is likely that existing "
150 "rfc822 attachments became invalid!\n"));
154 n_sigman_leave(&sm
, n_SIGMAN_VIPSIGS_NTTYOUT
);
158 _include_file(char const *name
, int *linecount
, int *charcount
,
159 bool_t doecho
, bool_t indent
)
164 char *linebuf
= NULL
; /* TODO line pool */
165 size_t linesize
= 0, indl
, linelen
, cnt
;
168 if (name
== (char*)-1)
170 else if ((fbuf
= Fopen(name
, "r")) == NULL
) {
176 indb
= NULL
, indl
= 0;
178 if ((indb
= ok_vlook(indentprefix
)) == NULL
)
179 indb
= INDENT_DEFAULT
;
183 *linecount
= *charcount
= 0;
185 while (fgetline(&linebuf
, &linesize
, &cnt
, &linelen
, fbuf
, 0) != NULL
) {
186 if (indl
> 0 && fwrite(indb
, sizeof *indb
, indl
, _coll_fp
) != indl
)
188 if (fwrite(linebuf
, sizeof *linebuf
, linelen
, _coll_fp
) != linelen
)
191 (*charcount
) += linelen
+ indl
;
192 if ((options
& OPT_INTERACTIVE
) && doecho
) {
194 fwrite(indb
, sizeof *indb
, indl
, stdout
);
195 fwrite(linebuf
, sizeof *linebuf
, linelen
, stdout
);
198 if (fflush(_coll_fp
))
200 if ((options
& OPT_INTERACTIVE
) && doecho
)
207 if (fbuf
!= NULL
&& fbuf
!= stdin
)
214 _collect_onpipe(int signo
)
216 NYD_X
; /* Signal handler */
218 siglongjmp(_coll_pipejmp
, 1);
222 insertcommand(FILE *fp
, char const *cmd
)
229 cp
= ok_vlook(SHELL
);
233 if ((ibuf
= Popen(cmd
, "r", cp
, NULL
, 0)) != NULL
) {
234 while ((c
= getc(ibuf
)) != EOF
) /* XXX bytewise, yuck! */
243 print_collf(FILE *cf
, struct header
*hp
)
245 char *lbuf
= NULL
; /* TODO line pool */
246 sighandler_type sigint
;
247 FILE * volatile obuf
= stdout
;
248 struct attachment
*ap
;
251 size_t linesize
= 0, linelen
, cnt
, cnt2
;
255 cnt
= cnt2
= fsize(cf
);
257 sigint
= safe_signal(SIGINT
, SIG_IGN
);
259 if ((options
& OPT_INTERACTIVE
) && (cp
= ok_vlook(crt
)) != NULL
) {
263 if (hp
->h_to
!= NULL
)
265 if (hp
->h_subject
!= NULL
)
267 if (hp
->h_cc
!= NULL
)
269 if (hp
->h_bcc
!= NULL
)
271 if (hp
->h_attach
!= NULL
)
273 m
+= (hp
->h_from
!= NULL
|| myaddrs(hp
) != NULL
);
274 m
+= (hp
->h_sender
!= NULL
|| ok_vlook(sender
) != NULL
);
275 m
+= (hp
->h_replyto
!= NULL
|| ok_vlook(replyto
) != NULL
);
277 l
= (*cp
== '\0') ? screensize() : atoi(cp
);
282 for (m
= 0; fgetline(&lbuf
, &linesize
, &cnt2
, NULL
, cf
, 0); ++m
)
287 if (sigsetjmp(_coll_pipejmp
, 1))
289 if ((obuf
= n_pager_open()) == NULL
)
292 safe_signal(SIGPIPE
, &_collect_onpipe
);
296 fprintf(obuf
, _("-------\nMessage contains:\n"));
297 gf
= GIDENT
| GTO
| GSUBJECT
| GCC
| GBCC
| GNL
| GFILES
| GCOMMA
;
298 puthead(TRU1
, hp
, obuf
, gf
, SEND_TODISP
, CONV_NONE
, NULL
, NULL
);
299 while (fgetline(&lbuf
, &linesize
, &cnt
, &linelen
, cf
, 1))
300 prout(lbuf
, linelen
, obuf
);
301 if (hp
->h_attach
!= NULL
) {
302 fputs(_("-------\nAttachments:\n"), obuf
);
303 for (ap
= hp
->h_attach
; ap
!= NULL
; ap
= ap
->a_flink
) {
305 fprintf(obuf
, " - message %u\n", ap
->a_msgno
);
307 /* TODO after MIME/send layer rewrite we *know*
308 * TODO the details of the attachment here,
309 * TODO so adjust this again, then */
310 char const *cs
, *csi
= "-> ";
312 if ((cs
= ap
->a_charset
) == NULL
&&
313 (csi
= "<- ", cs
= ap
->a_input_charset
) == NULL
)
314 cs
= charset_get_lc();
315 if ((cp
= ap
->a_content_type
) == NULL
)
317 else if (ascncasecmp(cp
, "text/", 5))
319 fprintf(obuf
, " - [%s, %s%s] %s\n", cp
, csi
, cs
, ap
->a_name
);
329 safe_signal(SIGINT
, sigint
);
334 exwrite(char const *name
, FILE *fp
, int f
)
342 printf("\"%s\" ", name
);
345 if ((of
= Fopen(name
, "a")) == NULL
) {
352 while ((c
= getc(fp
)) != EOF
) {
364 printf(_("%d/%ld\n"), lc
, cc
);
373 makeheader(FILE *fp
, struct header
*hp
, si8_t
*checkaddr_err
)
380 if ((nf
= Ftmp(NULL
, "colhead", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==NULL
) {
381 n_perr(_("temporary mail edit file"), 0);
385 extract_header(fp
, hp
, checkaddr_err
);
387 while ((c
= getc(fp
)) != EOF
) /* XXX bytewise, yuck! */
393 if (check_from_and_sender(hp
->h_from
, hp
->h_sender
) == NULL
)
402 mesedit(int c
, struct header
*hp
)
405 sighandler_type sigint
;
409 saved
= ok_blook(add_file_recipients
);
410 ok_bset(add_file_recipients
, TRU1
);
412 sigint
= safe_signal(SIGINT
, SIG_IGN
);
413 nf
= run_editor(_coll_fp
, (off_t
)-1, c
, FAL0
, hp
, NULL
, SEND_MBOX
, sigint
);
417 makeheader(nf
, hp
, NULL
);
419 fseek(nf
, 0L, SEEK_END
);
424 safe_signal(SIGINT
, sigint
);
426 ok_bset(add_file_recipients
, saved
);
434 sighandler_type sigint
;
438 sigint
= safe_signal(SIGINT
, SIG_IGN
);
440 if ((nf
= Ftmp(NULL
, "colpipe", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==NULL
) {
441 n_perr(_("temporary mail edit file"), 0);
445 /* stdin = current message. stdout = new message */
446 if ((sh
= ok_vlook(SHELL
)) == NULL
)
449 if (run_command(sh
, 0, fileno(_coll_fp
), fileno(nf
), "-c", cmd
, NULL
, NULL
)
455 if (fsize(nf
) == 0) {
456 n_err(_("No bytes from \"%s\" !?\n"), cmd
);
462 fseek(nf
, 0L, SEEK_END
);
466 safe_signal(SIGINT
, sigint
);
471 forward(char *ms
, FILE *fp
, int f
)
474 struct ignoretab
*ig
;
476 enum sendaction action
;
479 msgvec
= salloc((size_t)(msgCount
+ 1) * sizeof *msgvec
);
480 if (getmsglist(ms
, msgvec
, 0) < 0)
483 *msgvec
= first(0, MMNORM
);
485 n_err(_("No appropriate messages\n"));
491 if (f
== 'f' || f
== 'F' || f
== 'u')
493 else if ((tabst
= ok_vlook(indentprefix
)) == NULL
)
494 tabst
= INDENT_DEFAULT
;
495 if (f
== 'u' || f
== 'U')
498 ig
= upperchar(f
) ? NULL
: ignore
;
499 action
= (upperchar(f
) && f
!= 'U') ? SEND_QUOTE_ALL
: SEND_QUOTE
;
501 printf(_("Interpolating:"));
502 for (; *msgvec
!= 0; ++msgvec
) {
503 struct message
*mp
= message
+ *msgvec
- 1;
506 printf(" %d", *msgvec
);
508 if (sendmp(mp
, fp
, ig
, tabst
, action
, NULL
) < 0) {
509 n_perr(_("temporary mail file"), 0);
523 sighandler_type old_action
;
525 NYD_X
; /* Signal handler */
527 old_action
= safe_signal(s
, SIG_DFL
);
531 sigprocmask(SIG_UNBLOCK
, &nset
, NULL
);
533 sigprocmask(SIG_BLOCK
, &nset
, NULL
);
535 safe_signal(s
, old_action
);
539 siglongjmp(_coll_jmp
, 1);
546 NYD_X
; /* Signal handler */
548 /* the control flow is subtle, because we can be called from ~q */
549 if (_coll_hadintr
== 0) {
550 if (ok_blook(ignore
)) {
556 siglongjmp(_coll_jmp
, 1);
558 exit_status
|= EXIT_SEND_ERROR
;
560 savedeadletter(_coll_fp
, 1);
561 /* Aborting message, no need to fflush() .. */
562 siglongjmp(_coll_abort
, 1);
568 NYD_X
; /* Signal handler */
571 savedeadletter(_coll_fp
, 1);
572 /* Let's pretend nobody else wants to clean up, a true statement at
578 putesc(char const *s
, FILE *stream
)
583 while (s
[0] != '\0') {
586 if (putc('\t', stream
) == EOF
)
593 if (putc('\n', stream
) == EOF
)
600 if (putc(s
[0], stream
) == EOF
)
605 if (putc('\n', stream
) == EOF
)
614 a_coll_call_hook(struct header
*hp
, char const *macname
){ /* TODO v15: drop */
618 if((val
= detract(hp
->h_from
, GNAMEONLY
)) == NULL
)
620 ok_vset(compose_from
, val
);
621 if((val
= detract(hp
->h_sender
, 0)) == NULL
)
623 ok_vset(compose_sender
, val
);
624 if((val
= detract(hp
->h_to
, GNAMEONLY
)) == NULL
)
626 ok_vset(compose_to
, val
);
627 if((val
= detract(hp
->h_cc
, GNAMEONLY
)) == NULL
)
629 ok_vset(compose_cc
, val
);
630 if((val
= detract(hp
->h_bcc
, GNAMEONLY
)) == NULL
)
632 ok_vset(compose_bcc
, val
);
633 if((val
= hp
->h_subject
) == NULL
)
635 ok_vset(compose_subject
, val
);
637 call_compose_mode_hook(macname
);
639 ok_vclear(compose_subject
);
640 ok_vclear(compose_bcc
);
641 ok_vclear(compose_cc
);
642 ok_vclear(compose_to
);
643 ok_vclear(compose_sender
);
644 ok_vclear(compose_from
);
649 collect(struct header
*hp
, int printheaders
, struct message
*mp
,
650 char *quotefile
, int doprefix
, si8_t
*checkaddr_err
)
652 struct ignoretab
*quoteig
;
654 int volatile escape
, getfields
;
655 char *linebuf
, *quote
;
657 size_t i
, linesize
; /* TODO line pool */
659 enum sendaction action
;
661 FILE * volatile sigfp
;
667 linebuf
= quote
= NULL
;
669 /* Start catching signals from here, but we're still die on interrupts
670 * until we're in the main loop */
672 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
673 /* FIXME have dropped handlerpush() and localized onintr() in lex.c! */
674 if ((_coll_saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
675 safe_signal(SIGINT
, &_collint
);
676 if ((_coll_savehup
= safe_signal(SIGHUP
, SIG_IGN
)) != SIG_IGN
)
677 safe_signal(SIGHUP
, collhup
);
678 /* TODO We do a lot of redundant signal handling, especially
679 * TODO with the command line editor(s); try to merge this */
680 _coll_savetstp
= safe_signal(SIGTSTP
, collstop
);
681 _coll_savettou
= safe_signal(SIGTTOU
, collstop
);
682 _coll_savettin
= safe_signal(SIGTTIN
, collstop
);
683 if (sigsetjmp(_coll_abort
, 1))
685 if (sigsetjmp(_coll_jmp
, 1))
687 pstate
|= PS_RECURSED
;
688 sigprocmask(SIG_SETMASK
, &oset
, (sigset_t
*)NULL
);
691 if ((_coll_fp
= Ftmp(NULL
, "collect", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==
693 n_perr(_("temporary mail file"), 0);
697 /* If we are going to prompt for a subject, refrain from printing a newline
698 * after the headers (since some people mind) */
700 if (!(options
& OPT_t_FLAG
)) {
701 t
= GTO
| GSUBJECT
| GCC
| GNL
;
702 if (ok_blook(fullnames
))
705 if (options
& OPT_INTERACTIVE
) {
706 if (hp
->h_subject
== NULL
&& (ok_blook(ask
) || ok_blook(asksub
)))
707 t
&= ~GNL
, getfields
|= GSUBJECT
;
709 if (hp
->h_to
== NULL
)
710 t
&= ~GNL
, getfields
|= GTO
;
712 if (!ok_blook(bsdcompat
) && !ok_blook(askatend
)) {
713 if (hp
->h_bcc
== NULL
&& ok_blook(askbcc
))
714 t
&= ~GNL
, getfields
|= GBCC
;
715 if (hp
->h_cc
== NULL
&& ok_blook(askcc
))
716 t
&= ~GNL
, getfields
|= GCC
;
721 escape
= ((cp
= ok_vlook(escape
)) != NULL
) ? *cp
: ESCAPE
;
724 if (!sigsetjmp(_coll_jmp
, 1)) {
725 /* Ask for some headers first, as necessary */
727 grab_headers(hp
, getfields
, 1);
729 /* Execute compose-post-hook TODO completely v15-compat intermediate!! */
730 if((cp
= ok_vlook(on_compose_enter
)) != NULL
){
731 setup_from_and_sender(hp
);
732 a_coll_call_hook(hp
, cp
);
736 char const *cp_obsolete
= ok_vlook(NAIL_HEAD
);
737 if(cp_obsolete
!= NULL
)
738 OBSOLETE(_("please use *message-inject-head* "
739 "instead of *NAIL_HEAD*"));
741 if(((cp
= ok_vlook(message_inject_head
)) != NULL
||
742 (cp
= cp_obsolete
) != NULL
) && putesc(cp
, _coll_fp
) < 0)
746 /* Quote an original message */
747 if (mp
!= NULL
&& (doprefix
|| (quote
= ok_vlook(quote
)) != NULL
)) {
752 if ((cp
= ok_vlook(fwdheading
)) == NULL
)
753 cp
= "-------- Original Message --------";
754 if (*cp
!= '\0' && fprintf(_coll_fp
, "%s\n", cp
) < 0)
756 } else if (!strcmp(quote
, "noheading")) {
758 } else if (!strcmp(quote
, "headers")) {
760 } else if (!strcmp(quote
, "allheaders")) {
762 action
= SEND_QUOTE_ALL
;
764 cp
= hfield1("from", mp
);
765 if (cp
!= NULL
&& (cnt
= (long)strlen(cp
)) > 0) {
766 if (xmime_write(cp
, cnt
, _coll_fp
, CONV_FROMHDR
, TD_NONE
) < 0)
768 if (fprintf(_coll_fp
, _(" wrote:\n\n")) < 0)
772 if (fflush(_coll_fp
))
776 else if ((cp
= ok_vlook(indentprefix
)) == NULL
)
778 if (sendmp(mp
, _coll_fp
, quoteig
, cp
, action
, NULL
) < 0)
782 if (quotefile
!= NULL
) {
783 if (_include_file(quotefile
, &lc
, &cc
, TRU1
, FAL0
) != 0)
787 if (options
& OPT_INTERACTIVE
) {
788 /* Print what we have sofar also on the terminal (if useful) */
789 if (!ok_blook(editalong
)) {
791 puthead(TRU1
, hp
, stdout
, t
, SEND_TODISP
, CONV_NONE
, NULL
, NULL
);
794 while ((c
= getc(_coll_fp
)) != EOF
) /* XXX bytewise, yuck! */
796 if (fseek(_coll_fp
, 0, SEEK_END
))
799 /* Ensure this is clean xxx not really necessary? */
808 /* Come here for printing the after-signal message. Duplicate messages
809 * won't be printed because the write is aborted if we get a SIGTTOU */
811 n_err(_("\n(Interrupt -- one more to kill letter)\n"));
814 printf(_("(continue)\n"));
819 /* No tilde escapes, interrupts not expected. Simply copy STDIN */
820 if (!(options
& (OPT_INTERACTIVE
| OPT_t_FLAG
| OPT_TILDE_FLAG
))) {
821 linebuf
= srealloc(linebuf
, linesize
= LINESIZE
);
822 while ((i
= fread(linebuf
, sizeof *linebuf
, linesize
, stdin
)) > 0) {
823 if (i
!= fwrite(linebuf
, sizeof *linebuf
, i
, _coll_fp
))
829 /* The interactive collect loop.
830 * All commands which come here are forbidden when sourcing! */
831 assert(_coll_hadintr
|| !(pstate
& PS_SOURCING
));
834 cnt
= n_lex_input("", FAL0
, &linebuf
, &linesize
, NULL
);
838 assert(!(pstate
& PS_SOURCING
));
839 if (options
& OPT_t_FLAG
) {
840 fflush_rewind(_coll_fp
);
841 /* It is important to set PS_t_FLAG before extract_header() *and*
842 * keep OPT_t_FLAG for the first parse of the message, too! */
844 if (makeheader(_coll_fp
, hp
, checkaddr_err
) != OKAY
)
846 options
&= ~OPT_t_FLAG
;
848 } else if ((options
& OPT_INTERACTIVE
) && ok_blook(ignoreeof
)) {
849 printf(_("*ignoreeof* set, use \"~.\" to terminate letter\n"));
857 if (cnt
== 0 || !(options
& (OPT_INTERACTIVE
| OPT_TILDE_FLAG
))) {
859 /* TODO calls putline(), which *always* appends LF;
860 * TODO thus, STDIN with -t will ALWAYS end with LF,
861 * TODO even if no trailing LF and QP encoding.
862 * TODO when finally changed, update cc-test.sh */
863 if (putline(_coll_fp
, linebuf
, cnt
) < 0)
866 } else if (linebuf
[0] == '.') {
867 if (linebuf
[1] == '\0' && (ok_blook(dot
) || ok_blook(ignoreeof
)))
870 if (linebuf
[0] != escape
)
873 if (!(options
& OPT_t_FLAG
))
874 n_tty_addhist(linebuf
, TRU1
);
879 /* On double escape, send a single one. Otherwise, it's an error */
881 if (putline(_coll_fp
, linebuf
+ 1, cnt
- 1) < 0)
886 n_err(_("Unknown tilde escape: ~%c\n"), asciichar(c
) ? c
: '?');
889 /* Shell escape, send the balance of line to sh -c */
890 c_shell(linebuf
+ 2);
895 /* Escape to command mode, but be nice! */
896 _execute_command(hp
, linebuf
+ 2, cnt
- 2);
899 /* Simulate end of file on input */
902 /* Same as 'q', but no *DEAD* saving */
905 /* Force a quit, act like an interrupt had happened */
907 _collint((c
== 'x') ? 0 : SIGINT
);
911 /* Grab a bunch of headers */
913 grab_headers(hp
, GTO
| GSUBJECT
| GCC
| GBCC
,
914 (ok_blook(bsdcompat
) && ok_blook(bsdorder
)));
915 while (hp
->h_to
== NULL
);
918 /* Grab extra headers */
920 grab_headers(hp
, GEXTRA
, 0);
921 while (check_from_and_sender(hp
->h_from
, hp
->h_sender
) == NULL
);
924 /* Add to the To list */
925 hp
->h_to
= cat(hp
->h_to
,
926 checkaddrs(lextract(linebuf
+ 2, GTO
| GFULL
), EACM_NORMAL
,
930 /* Set the Subject list */
932 while (whitechar(*cp
))
934 hp
->h_subject
= savestr(cp
);
936 #ifdef HAVE_MEMORY_DEBUG
942 /* Edit the attachment list */
943 if (linebuf
[2] != '\0')
944 append_attachments(&hp
->h_attach
, linebuf
+ 2);
946 edit_attachments(&hp
->h_attach
);
949 /* Add to the CC list */
950 hp
->h_cc
= cat(hp
->h_cc
,
951 checkaddrs(lextract(linebuf
+ 2, GCC
| GFULL
), EACM_NORMAL
,
955 /* Add stuff to blind carbon copies list */
956 hp
->h_bcc
= cat(hp
->h_bcc
,
957 checkaddrs(lextract(linebuf
+ 2, GBCC
| GFULL
), EACM_NORMAL
,
961 strncpy(linebuf
+ 2, getdeadletter(), linesize
- 2);
962 linebuf
[linesize
- 1] = '\0';
967 /* Invoke a file: Search for the file name, then open it and copy the
968 * contents to _coll_fp */
970 while (whitechar(*cp
))
973 n_err(_("Interpolate what file?\n"));
977 insertcommand(_coll_fp
, cp
+ 1);
980 if ((cp
= file_expand(cp
)) == NULL
)
983 n_err(_("\"%s\": Directory\n"), cp
);
986 printf(_("\"%s\" "), cp
);
988 if (_include_file(cp
, &lc
, &cc
, FAL0
, (c
== 'R')) != 0)
990 printf(_("%d/%d\n"), lc
, cc
);
993 /* Insert a variable into the file */
995 while (whitechar(*cp
))
997 if ((cp
= vok_vlook(cp
)) == NULL
|| *cp
== '\0')
999 if (putesc(cp
, _coll_fp
) < 0)
1001 if ((options
& OPT_INTERACTIVE
) && putesc(cp
, stdout
) < 0)
1006 /* Insert the contents of a signature variable */
1007 cp
= (c
== 'a') ? ok_vlook(sign
) : ok_vlook(Sign
);
1008 if (cp
!= NULL
&& *cp
!= '\0') {
1009 if (putesc(cp
, _coll_fp
) < 0)
1011 if ((options
& OPT_INTERACTIVE
) && putesc(cp
, stdout
) < 0)
1016 /* Write the message on a file */
1018 while (blankchar(*cp
))
1020 if (*cp
== '\0' || (cp
= file_expand(cp
)) == NULL
) {
1021 n_err(_("Write what file!?\n"));
1025 if (exwrite(cp
, _coll_fp
, 1) < 0)
1034 /* Interpolate the named messages, if we are in receiving mail mode.
1035 * Does the standard list processing garbage. If ~f is given, we
1036 * don't shift over */
1037 if (forward(linebuf
+ 2, _coll_fp
, c
) < 0)
1041 /* Print current state of the message without altering anything */
1042 print_collf(_coll_fp
, hp
);
1045 /* Pipe message through command. Collect output as new message */
1047 mespipe(linebuf
+ 2);
1051 /* Edit the current message. 'e' -> use EDITOR, 'v' -> use VISUAL */
1053 mesedit(c
, ok_blook(editheaders
) ? hp
: NULL
);
1056 /* Last the lengthy help string. (Very ugly, but take care for
1057 * compiler supported string lengths :() */
1059 "TILDE ESCAPES (to be placed after a newline) excerpt:\n"
1060 "~. Commit and send message\n"
1061 "~: <command> Execute a mail command\n"
1062 "~<! <command> Insert output of command\n"
1063 "~@ [<files>] Edit attachment list\n"
1064 "~A Insert *Sign* variable (`~a' inserts *sign*)\n"
1065 "~c <users> Add users to Cc: list (`~b' for Bcc:)\n"
1066 "~d Read in *DEAD* (dead.letter)\n"
1067 "~e Edit message via *EDITOR*"
1070 "~F <msglist> Read in with headers, don't *indentprefix* lines\n"
1071 "~f <msglist> Like ~F, but honour `ignore' / `retain' configuration\n"
1072 "~H Edit From:, Reply-To: and Sender:\n"
1073 "~h Prompt for Subject:, To:, Cc: and \"blind\" Bcc:\n"
1074 "~i <variable> Insert a value and a newline\n"
1075 "~M <msglist> Read in with headers, *indentprefix* (`~m': `retain' etc.)\n"
1076 "~p Print current message compose buffer\n"
1077 "~r <file> Read in a file (`~R' *indentprefix* lines)"
1080 "~s <subject> Set Subject:\n"
1081 "~t <users> Add users to To: list\n"
1082 "~u <msglist> Read in message(s) without headers (`~U' indents lines)\n"
1083 "~v Edit message via *VISUAL*\n"
1084 "~w <file> Write message onto file\n"
1085 "~x Abort composition, discard message (`~q' saves in *DEAD*)\n"
1086 "~| <command> Pipe message through shell filter"
1093 /* Execute compose-post-hook TODO completely v15-compat intermediate!! */
1094 if((cp
= ok_vlook(on_compose_leave
)) != NULL
){
1095 setup_from_and_sender(hp
);
1096 a_coll_call_hook(hp
, cp
);
1099 /* Final change to edit headers, if not already above */
1100 if (ok_blook(bsdcompat
) || ok_blook(askatend
)) {
1101 if (hp
->h_cc
== NULL
&& ok_blook(askcc
))
1102 grab_headers(hp
, GCC
, 1);
1103 if (hp
->h_bcc
== NULL
&& ok_blook(askbcc
))
1104 grab_headers(hp
, GBCC
, 1);
1106 if (hp
->h_attach
== NULL
&& ok_blook(askattach
))
1107 edit_attachments(&hp
->h_attach
);
1109 /* Add automatic receivers */
1110 if ((cp
= ok_vlook(autocc
)) != NULL
&& *cp
!= '\0')
1111 hp
->h_cc
= cat(hp
->h_cc
, checkaddrs(lextract(cp
, GCC
| GFULL
),
1112 EACM_NORMAL
, checkaddr_err
));
1113 if ((cp
= ok_vlook(autobcc
)) != NULL
&& *cp
!= '\0')
1114 hp
->h_bcc
= cat(hp
->h_bcc
, checkaddrs(lextract(cp
, GBCC
| GFULL
),
1115 EACM_NORMAL
, checkaddr_err
));
1116 if (*checkaddr_err
!= 0)
1119 /* Place signature? */
1120 if((cp
= ok_vlook(signature
)) != NULL
&& *cp
!= '\0'){
1121 if((quote
= file_expand(cp
)) == NULL
){
1122 n_err(_("*signature* expands to invalid file: \"%s\"\n"), cp
);
1125 if((sigfp
= Fopen(cp
= quote
, "r")) == NULL
){
1126 n_err(_("Can't open *signature* \"%s\": %s\n"), cp
, strerror(errno
));
1131 linebuf
= smalloc(linesize
= LINESIZE
);
1133 while((i
= fread(linebuf
, sizeof *linebuf
, linesize
, UNVOLATILE(sigfp
)))
1136 if(i
!= fwrite(linebuf
, sizeof *linebuf
, i
, _coll_fp
))
1141 FILE *x
= UNVOLATILE(sigfp
);
1142 int e
= errno
, ise
= ferror(x
);
1148 n_err(_("Errors while reading *signature* \"%s\": %s\n"),
1154 if(c
!= '\0' && c
!= '\n')
1155 putc('\n', _coll_fp
);
1158 { char const *cp_obsolete
= ok_vlook(NAIL_TAIL
);
1160 if(cp_obsolete
!= NULL
)
1161 OBSOLETE(_("please use *message-inject-tail* instead of *NAIL_TAIL*"));
1163 if((cp
= ok_vlook(message_inject_tail
)) != NULL
||
1164 (cp
= cp_obsolete
) != NULL
){
1165 if(putesc(cp
, _coll_fp
) < 0)
1167 if((options
& OPT_INTERACTIVE
) && putesc(cp
, stdout
) < 0)
1172 if(fflush(_coll_fp
))
1177 if (linebuf
!= NULL
)
1181 sigprocmask(SIG_BLOCK
, &nset
, NULL
);
1182 pstate
&= ~PS_RECURSED
;
1183 safe_signal(SIGINT
, _coll_saveint
);
1184 safe_signal(SIGHUP
, _coll_savehup
);
1185 safe_signal(SIGTSTP
, _coll_savetstp
);
1186 safe_signal(SIGTTOU
, _coll_savettou
);
1187 safe_signal(SIGTTIN
, _coll_savettin
);
1188 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1194 Fclose(UNVOLATILE(sigfp
));
1195 if (_coll_fp
!= NULL
) {
1203 savedeadletter(FILE *fp
, int fflush_rewind_first
)
1211 if ((options
& OPT_DEBUG
) || !ok_blook(save
))
1214 if (fflush_rewind_first
) {
1221 cp
= getdeadletter();
1223 dbuf
= Fopen(cp
, "a");
1230 printf("\"%s\" ", cp
);
1231 for (lines
= bytes
= 0; (c
= getc(fp
)) != EOF
; ++bytes
) {
1236 printf("%lu/%lu\n", lines
, bytes
);