1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Collect input from standard input, handling ~ escapes.
3 *@ TODO This needs a complete rewrite, with carriers, etc.
5 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
6 * Copyright (c) 2012 - 2018 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
7 * SPDX-License-Identifier: BSD-3-Clause
10 * Copyright (c) 1980, 1993
11 * The Regents of the University of California. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #define n_FILE collect
40 #ifndef HAVE_AMALGAMATION
44 struct a_coll_fmt_ctx
{ /* xxx This is temporary until v15 has objects */
47 struct message
*cfc_mp
;
53 char const *cfc_msgid
; /* Or NULL */
56 struct a_coll_ocs_arg
{
57 sighandler_type coa_opipe
;
58 sighandler_type coa_oint
;
59 FILE *coa_stdin
; /* The descriptor (pipe(2)+Fdopen()) we read from */
60 FILE *coa_stdout
; /* The Popen()ed pipe through which we write to the hook */
61 int coa_pipe
[2]; /* ..backing .coa_stdin */
62 si8_t
*coa_senderr
; /* Set to 1 on failure */
63 char coa_cmd
[n_VFIELD_SIZE(0)];
66 /* The following hookiness with global variables is so that on receipt of an
67 * interrupt signal, the partial message can be salted away on *DEAD* */
69 static sighandler_type _coll_saveint
; /* Previous SIGINT value */
70 static sighandler_type _coll_savehup
; /* Previous SIGHUP value */
71 static FILE *_coll_fp
; /* File for saving away */
72 static int volatile _coll_hadintr
; /* Have seen one SIGINT so far */
73 static sigjmp_buf _coll_jmp
; /* To get back to work */
74 static sigjmp_buf _coll_abort
; /* To end collection with error */
75 static char const *a_coll_ocs__macname
; /* *on-compose-splice* */
77 /* Handle `~:', `~_' and some hooks; hp may be NULL */
78 static void _execute_command(struct header
*hp
, char const *linebuf
,
82 static si32_t
a_coll_include_file(char const *name
, bool_t indent
,
85 /* Execute cmd and insert its standard output into fp, return errno */
86 static si32_t
a_coll_insert_cmd(FILE *fp
, char const *cmd
);
89 static void print_collf(FILE *collf
, struct header
*hp
);
91 /* Write a file, ex-like if f set */
92 static si32_t
a_coll_write(char const *name
, FILE *fp
, int f
);
94 /* *message-inject-head* */
95 static bool_t
a_coll_message_inject_head(FILE *fp
);
97 /* With bells and whistles */
98 static bool_t
a_coll_quote_message(FILE *fp
, struct message
*mp
, bool_t isfwd
);
100 /* *{forward,quote}-inject-{head,tail}*.
101 * fmt may be NULL or the empty string, in which case no output is produced */
102 static bool_t
a_coll__fmt_inj(struct a_coll_fmt_ctx
const *cfcp
);
104 /* Parse off the message header from fp and store relevant fields in hp,
105 * replace _coll_fp with a shiny new version without any header.
106 * Takes care for closing of fp and _coll_fp as necessary */
107 static bool_t
a_coll_makeheader(FILE *fp
, struct header
*hp
,
108 si8_t
*checkaddr_err
, bool_t do_delayed_due_t
);
110 /* Edit the message being collected on fp.
111 * If c=='|' pipecmd must be set and is passed through to n_run_editor().
112 * On successful return, make the edit file the new temp file; return errno */
113 static si32_t
a_coll_edit(int c
, struct header
*hp
, char const *pipecmd
);
115 /* Pipe the message through the command. Old message is on stdin of command,
116 * new message collected from stdout. Shell must return 0 to accept new msg */
117 static si32_t
a_coll_pipe(char const *cmd
);
119 /* Interpolate the named messages into the current message, possibly doing
120 * indent stuff. The flag argument is one of the command escapes: [mMfFuU].
122 static si32_t
a_coll_forward(char const *ms
, FILE *fp
, int f
);
124 /* On interrupt, come here to save the partial message in ~/dead.letter.
125 * Then jump out of the collection loop */
126 static void _collint(int s
);
128 static void collhup(int s
);
130 /* ~[AaIi], *message-inject-**: put value, expand \[nt] if *posix* */
131 static bool_t
a_coll_putesc(char const *s
, bool_t addnl
, FILE *stream
);
133 /* *on-compose-splice* driver and *on-compose-splice(-shell)?* finalizer */
134 static int a_coll_ocs__mac(void);
135 static void a_coll_ocs__finalize(void *vp
);
138 _execute_command(struct header
*hp
, char const *linebuf
, size_t linesize
){
139 /* The problem arises if there are rfc822 message attachments and the
140 * user uses `~:' to change the current file. TODO Unfortunately we
141 * TODO cannot simply keep a pointer to, or increment a reference count
142 * TODO of the current `file' (mailbox that is) object, because the
143 * TODO codebase doesn't deal with that at all; so, until some far
144 * TODO later time, copy the name of the path, and warn the user if it
145 * TODO changed; we COULD use the AC_TMPFILE attachment type, i.e.,
146 * TODO copy the message attachments over to temporary files, but that
147 * TODO would require more changes so that the user still can recognize
148 * TODO in `~@' etc. that its a rfc822 message attachment; see below */
150 struct attachment
*ap
;
151 char * volatile mnbuf
;
157 n_SIGMAN_ENTER_SWITCH(&sm
, n_SIGMAN_HUP
| n_SIGMAN_INT
| n_SIGMAN_QUIT
){
161 n_pstate_err_no
= n_ERR_INTR
;
166 /* If the above todo is worked, remove or outsource to attachment.c! */
167 if(hp
!= NULL
&& (ap
= hp
->h_attach
) != NULL
) do
169 mnbuf
= sstrdup(mailname
);
172 while((ap
= ap
->a_flink
) != NULL
);
174 n_go_command(n_GO_INPUT_CTX_COMPOSE
, linebuf
);
176 n_sigman_cleanup_ping(&sm
);
179 if(strcmp(mnbuf
, mailname
))
180 n_err(_("Mailbox changed: it is likely that existing "
181 "rfc822 attachments became invalid!\n"));
185 n_sigman_leave(&sm
, n_SIGMAN_VIPSIGS_NTTYOUT
);
189 a_coll_include_file(char const *name
, bool_t indent
, bool_t writestat
){
191 char const *heredb
, *indb
;
192 size_t linesize
, heredl
, indl
, cnt
, linelen
;
200 linebuf
= NULL
; /* TODO line pool */
205 /* The -M case is special */
206 if(name
== (char*)-1){
209 }else if(name
[0] == '-' &&
210 (name
[1] == '\0' || blankspacechar(name
[1]))){
213 if(!(n_psonce
& n_PSO_INTERACTIVE
)){
214 n_err(_("~< -: HERE-delimiter required in non-interactive mode\n"));
219 for(heredb
= &name
[2]; *heredb
!= '\0' && blankspacechar(*heredb
);
222 if((heredl
= strlen(heredb
)) == 0){
224 n_err(_("~< - HERE-delimiter: delimiter must not be empty\n"));
230 for(indb
= ++heredb
; *indb
!= '\0' && *indb
!= '\''; ++indb
)
233 n_err(_("~< - HERE-delimiter: missing trailing quote\n"));
236 }else if(indb
[1] != '\0'){
237 n_err(_("~< - HERE-delimiter: trailing characters after "
242 if((heredl
= PTR2SIZE(indb
- heredb
)) == 0)
244 heredb
= savestrbuf(heredb
, heredl
);
248 }else if((fbuf
= Fopen(name
, "r")) == NULL
){
249 n_perr(name
, rv
= n_err_no
);
253 indl
= indent
? strlen(indb
= ok_vlook(indentprefix
)) : 0;
257 while(fgetline(&linebuf
, &linesize
, (fbuf
== n_stdin
? NULL
: &cnt
),
258 &linelen
, fbuf
, 0) != NULL
){
259 if(heredl
> 0 && heredl
== linelen
- 1 &&
260 !memcmp(heredb
, linebuf
, heredl
)){
266 if(fwrite(indb
, sizeof *indb
, indl
, _coll_fp
) != indl
){
273 if(fwrite(linebuf
, sizeof *linebuf
, linelen
, _coll_fp
) != linelen
){
280 if(fflush(_coll_fp
)){
286 rv
= n_ERR_NOTOBACCO
;
298 fprintf(n_stdout
, "%s%s %" PRId64
"/%" PRId64
"\n",
299 n_shexp_quote_cp(name
, FAL0
), (rv
? " " n_ERROR
: n_empty
), lc
, cc
);
305 a_coll_insert_cmd(FILE *fp
, char const *cmd
){
314 if((ibuf
= Popen(cmd
, "r", ok_vlook(SHELL
), NULL
, 0)) != NULL
){
317 while((c
= getc(ibuf
)) != EOF
){ /* XXX bytewise, yuck! */
318 if(putc(c
, fp
) == EOF
){
326 if(!feof(ibuf
) || ferror(ibuf
)){
330 if(!Pclose(ibuf
, TRU1
)){
335 n_perr(cmd
, rv
= n_err_no
);
337 fprintf(n_stdout
, "CMD%s %" PRId64
"/%" PRId64
"\n",
338 (rv
== n_ERR_NONE
? n_empty
: " " n_ERROR
), lc
, cc
);
344 print_collf(FILE *cf
, struct header
*hp
)
348 size_t cnt
, linesize
, linelen
;
352 cnt
= (size_t)fsize(cf
);
354 if((obuf
= Ftmp(NULL
, "collfp", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
){
355 n_perr(_("Can't create temporary file for `~p' command"), 0);
361 fprintf(obuf
, _("-------\nMessage contains:\n")); /* xxx SEARCH112 */
362 n_puthead(TRU1
, hp
, obuf
,
363 (GIDENT
| GTO
| GSUBJECT
| GCC
| GBCC
| GBCC_IS_FCC
| GNL
| GFILES
|
364 GCOMMA
), SEND_TODISP
, CONV_NONE
, NULL
, NULL
);
368 while(fgetline(&lbuf
, &linesize
, &cnt
, &linelen
, cf
, 1))
369 prout(lbuf
, linelen
, obuf
);
373 if(hp
->h_attach
!= NULL
){
374 fputs(_("-------\nAttachments:\n"), obuf
);
375 n_attachment_list_print(hp
->h_attach
, obuf
);
380 page_or_print(obuf
, 0);
388 a_coll_write(char const *name
, FILE *fp
, int f
)
399 fprintf(n_stdout
, "%s ", n_shexp_quote_cp(name
, FAL0
));
403 if ((of
= Fopen(name
, "a")) == NULL
) {
404 n_perr(name
, rv
= n_err_no
);
409 while ((c
= getc(fp
)) != EOF
) {
413 if (putc(c
, of
) == EOF
) {
414 n_perr(name
, rv
= n_err_no
);
418 fprintf(n_stdout
, "%" PRId64
"/%" PRId64
"\n", lc
, cc
);
428 putc('\n', n_stdout
);
433 a_coll_message_inject_head(FILE *fp
){
435 char const *cp
, *cp_obsolete
;
438 cp_obsolete
= ok_vlook(NAIL_HEAD
);
439 if(cp_obsolete
!= NULL
)
440 n_OBSOLETE(_("please use *message-inject-head*, not *NAIL_HEAD*"));
442 if(((cp
= ok_vlook(message_inject_head
)) != NULL
||
443 (cp
= cp_obsolete
) != NULL
) && !a_coll_putesc(cp
, TRU1
, fp
))
452 a_coll_quote_message(FILE *fp
, struct message
*mp
, bool_t isfwd
){
453 struct a_coll_fmt_ctx cfc
;
455 struct n_ignore
const *quoteitp
;
456 enum sendaction action
;
462 if(isfwd
|| (cp
= ok_vlook(quote
)) != NULL
){
463 quoteitp
= n_IGNORE_ALL
;
467 char const *cp_v15compat
;
469 if((cp_v15compat
= ok_vlook(fwdheading
)) != NULL
)
470 n_OBSOLETE(_("please use *forward-inject-head* instead of "
472 if((cp
= ok_vlook(forward_inject_head
)) == NULL
&&
473 (cp
= cp_v15compat
) == NULL
)
474 cp
= n_FORWARD_INJECT_HEAD
;
475 quoteitp
= n_IGNORE_FWD
;
477 if(!strcmp(cp
, "noheading")){
479 }else if(!strcmp(cp
, "headers")){
480 quoteitp
= n_IGNORE_TYPE
;
482 }else if(!strcmp(cp
, "allheaders")){
484 action
= SEND_QUOTE_ALL
;
486 }else if((cp
= ok_vlook(quote_inject_head
)) == NULL
)
487 cp
= n_QUOTE_INJECT_HEAD
;
489 /* We we pass through our formatter? */
490 if((cfc
.cfc_fmt
= cp
) != NULL
){
491 /* TODO In v15 [-textual_-]sender_info() should only create a list
492 * TODO of matching header objects, and the formatter should simply
493 * TODO iterate over this list and call OBJ->to_ui_str(FLAGS) or so.
494 * TODO For now fully initialize this thing once (grrrr!!) */
497 n_header_textual_sender_info(cfc
.cfc_mp
= mp
, &cfc
.cfc_cumul
,
498 &cfc
.cfc_addr
, &cfc
.cfc_real
, &cfc
.cfc_full
, NULL
);
499 cfc
.cfc_date
= n_header_textual_date_info(mp
, NULL
);
503 if((cp
= hfield1("message-id", mp
)) != NULL
&&
504 (np
= lextract(cp
, GREF
)) != NULL
)
511 if(!a_coll__fmt_inj(&cfc
) || fflush(fp
))
515 if(sendmp(mp
, fp
, quoteitp
, (isfwd
? NULL
: ok_vlook(indentprefix
)),
520 if((cp
= ok_vlook(forward_inject_tail
)) == NULL
)
521 cp
= n_FORWARD_INJECT_TAIL
;
522 }else if(cp
!= NULL
&& (cp
= ok_vlook(quote_inject_tail
)) == NULL
)
523 cp
= n_QUOTE_INJECT_TAIL
;
524 if((cfc
.cfc_fmt
= cp
) != NULL
&& (!a_coll__fmt_inj(&cfc
) || fflush(fp
)))
535 a_coll__fmt_inj(struct a_coll_fmt_ctx
const *cfcp
){
537 struct n_string s_b
, *sp
;
542 if((fmt
= cfcp
->cfc_fmt
) == NULL
|| *fmt
== '\0')
545 sp
= n_string_book(n_string_creat_auto(&s_b
), 127);
547 while((c
= *fmt
++) != '\0'){
548 if(c
!= '%' || (c
= *fmt
++) == '%'){
550 sp
= n_string_push_c(sp
, c
);
553 sp
= n_string_push_cp(sp
, cfcp
->cfc_addr
);
556 sp
= n_string_push_cp(sp
, cfcp
->cfc_date
);
559 sp
= n_string_push_cp(sp
, cfcp
->cfc_full
);
562 if(cfcp
->cfc_msgid
!= NULL
)
563 sp
= n_string_push_cp(sp
, cfcp
->cfc_msgid
);
566 sp
= n_string_push_cp(sp
, cfcp
->cfc_cumul
);
569 sp
= n_string_push_cp(sp
, cfcp
->cfc_real
);
576 n_err(_("*{forward,quote}-inject-{head,tail}*: "
577 "unknown format: %c (in: %s)\n"),
578 c
, n_shexp_quote_cp(cfcp
->cfc_fmt
, FAL0
));
583 quoteflt_init(&qf
, NULL
, FAL0
);
584 quoteflt_reset(&qf
, cfcp
->cfc_fp
);
585 if(quoteflt_push(&qf
, sp
->s_dat
, sp
->s_len
) < 0 || quoteflt_flush(&qf
) < 0)
587 quoteflt_destroy(&qf
);
589 /*n_string_gut(sp);*/
592 return (cfcp
!= NULL
);
596 a_coll_makeheader(FILE *fp
, struct header
*hp
, si8_t
*checkaddr_err
,
597 bool_t do_delayed_due_t
)
606 if ((nf
= Ftmp(NULL
, "colhead", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==NULL
) {
607 n_perr(_("temporary mail edit file"), 0);
611 n_header_extract(((do_delayed_due_t
612 ? n_HEADER_EXTRACT_FULL
| n_HEADER_EXTRACT_PREFILL_RECEIVERS
613 : n_HEADER_EXTRACT_EXTENDED
) |
614 n_HEADER_EXTRACT_IGNORE_SHELL_COMMENTS
), fp
, hp
, checkaddr_err
);
615 if (checkaddr_err
!= NULL
&& *checkaddr_err
!= 0)
618 /* In template mode some things have been delayed until the template has
620 if(do_delayed_due_t
){
623 if((cp
= ok_vlook(on_compose_enter
)) != NULL
){
624 setup_from_and_sender(hp
);
625 temporary_compose_mode_hook_call(cp
, &n_temporary_compose_hook_varset
,
629 if(!a_coll_message_inject_head(nf
))
633 while ((c
= getc(fp
)) != EOF
) /* XXX bytewise, yuck! */
642 if (check_from_and_sender(hp
->h_from
, hp
->h_sender
) == NULL
)
653 a_coll_edit(int c
, struct header
*hp
, char const *pipecmd
) /* TODO errret */
657 sighandler_type
volatile sigint
;
658 struct name
*saved_in_reply_to
;
664 n_UNINIT(sigint
, SIG_ERR
);
665 saved_filrec
= ok_blook(add_file_recipients
);
667 n_SIGMAN_ENTER_SWITCH(&sm
, n_SIGMAN_ALL
){
669 sigint
= safe_signal(SIGINT
, SIG_IGN
);
677 ok_bset(add_file_recipients
);
679 saved_in_reply_to
= NULL
;
683 if((np
= hp
->h_in_reply_to
) == NULL
)
684 hp
->h_in_reply_to
= np
= n_header_setup_in_reply_to(hp
);
686 saved_in_reply_to
= ndup(np
, np
->n_type
);
690 nf
= n_run_editor(_coll_fp
, (off_t
)-1, c
, FAL0
, hp
, NULL
, SEND_MBOX
, sigint
,
694 /* Overtaking of nf->_coll_fp is done by a_coll_makeheader()! */
695 if(!a_coll_makeheader(nf
, hp
, NULL
, FAL0
))
697 /* Break the thread if In-Reply-To: has been modified */
698 if(hp
->h_in_reply_to
== NULL
|| (saved_in_reply_to
!= NULL
&&
699 asccasecmp(hp
->h_in_reply_to
->n_fullname
,
700 saved_in_reply_to
->n_fullname
)))
703 fseek(nf
, 0L, SEEK_END
);
710 n_sigman_cleanup_ping(&sm
);
713 ok_bclear(add_file_recipients
);
714 safe_signal(SIGINT
, sigint
);
716 n_sigman_leave(&sm
, n_SIGMAN_VIPSIGS_NTTYOUT
);
721 a_coll_pipe(char const *cmd
)
725 sighandler_type sigint
;
730 sigint
= safe_signal(SIGINT
, SIG_IGN
);
732 if ((nf
= Ftmp(NULL
, "colpipe", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==NULL
) {
734 n_perr(_("temporary mail edit file"), rv
= n_err_no
);
738 /* stdin = current message. stdout = new message */
739 if(fflush(_coll_fp
) == EOF
)
742 if (n_child_run(ok_vlook(SHELL
), 0, fileno(_coll_fp
), fileno(nf
), "-c",
743 cmd
, NULL
, NULL
, &ws
) < 0 || WEXITSTATUS(ws
) != 0) {
749 if (fsize(nf
) == 0) {
750 n_err(_("No bytes from %s !?\n"), n_shexp_quote_cp(cmd
, FAL0
));
757 fseek(nf
, 0L, SEEK_END
);
761 safe_signal(SIGINT
, sigint
);
767 a_coll_forward(char const *ms
, FILE *fp
, int f
)
770 struct n_ignore
const *itp
;
772 enum sendaction action
;
775 if ((rv
= n_getmsglist(ms
, n_msgvec
, 0, NULL
)) < 0) {
776 rv
= n_ERR_NOENT
; /* XXX not really, should be handled there! */
780 *n_msgvec
= first(0, MMNORM
);
781 if (*n_msgvec
== 0) {
782 n_err(_("No appropriate messages\n"));
788 msgvec
= n_autorec_calloc(rv
+1, sizeof *msgvec
);
790 msgvec
[rv
] = n_msgvec
[rv
];
793 if (f
== 'f' || f
== 'F' || f
== 'u')
796 tabst
= ok_vlook(indentprefix
);
797 if (f
== 'u' || f
== 'U')
800 itp
= upperchar(f
) ? NULL
: n_IGNORE_TYPE
;
801 action
= (upperchar(f
) && f
!= 'U') ? SEND_QUOTE_ALL
: SEND_QUOTE
;
803 fprintf(n_stdout
, A_("Interpolating:"));
805 for(; *msgvec
!= 0; ++msgvec
){
808 mp
= &message
[*msgvec
- 1];
811 fprintf(n_stdout
, " %d", *msgvec
);
814 if(!a_coll_quote_message(fp
, mp
, FAL0
)){
818 }else if(sendmp(mp
, fp
, itp
, tabst
, action
, NULL
) < 0){
819 n_perr(_("forward: temporary mail file"), 0);
826 fprintf(n_stdout
, "\n");
835 NYD_X
; /* Signal handler */
837 /* the control flow is subtle, because we can be called from ~q */
838 if (_coll_hadintr
== 0) {
839 if (ok_blook(ignore
)) {
840 fputs("@\n", n_stdout
);
845 siglongjmp(_coll_jmp
, 1);
847 n_exit_status
|= n_EXIT_SEND_ERROR
;
849 savedeadletter(_coll_fp
, TRU1
);
850 /* Aborting message, no need to fflush() .. */
851 siglongjmp(_coll_abort
, 1);
857 NYD_X
; /* Signal handler */
860 savedeadletter(_coll_fp
, TRU1
);
861 /* Let's pretend nobody else wants to clean up, a true statement at
867 a_coll_putesc(char const *s
, bool_t addnl
, FILE *stream
){
872 isposix
= ok_blook(posix
);
874 while((c1
= *s
++) != '\0'){
875 if(c1
== '\\' && ((c2
= *s
) == 't' || c2
== 'n')){
877 isposix
= TRU1
; /* TODO v15 OBSOLETE! */
878 n_err(_("Compose mode warning: expanding \\t or \\n in variable "
880 " Support remains only for ~A,~a,~I,~i in *posix* mode!\n"
881 " Please use \"wysh set X=y..\" instead\n"));
884 c1
= (c2
== 't') ? '\t' : '\n';
887 if(putc(c1
, stream
) == EOF
)
891 if(addnl
&& putc('\n', stream
) == EOF
)
900 a_coll_ocs__mac(void){
901 /* Executes in a fork(2)ed child TODO if remains, global MASKs for those! */
902 setvbuf(n_stdin
, NULL
, _IOLBF
, 0);
903 setvbuf(n_stdout
, NULL
, _IOLBF
, 0);
904 n_psonce
&= ~(n_PSO_INTERACTIVE
| n_PSO_TTYIN
| n_PSO_TTYOUT
);
905 n_pstate
|= n_PS_COMPOSE_FORKHOOK
;
906 n_readctl_read_overlay
= NULL
; /* TODO need OnForkEvent! See c_readctl() */
907 n_digmsg_read_overlay
= NULL
; /* TODO need OnForkEvent! See c_digmsg() */
908 if(n_poption
& n_PO_D_VV
){
911 snprintf(buf
, sizeof buf
, "[%d]%s", getpid(), ok_vlook(log_prefix
));
912 ok_vset(log_prefix
, buf
);
914 /* TODO If that uses `!' it will effectively SIG_IGN SIGINT, ...and such */
915 temporary_compose_mode_hook_call(a_coll_ocs__macname
, NULL
, NULL
);
920 a_coll_ocs__finalize(void *vp
){
921 /* Note we use this for destruction upon setup errors, thus */
922 sighandler_type opipe
;
923 sighandler_type oint
;
924 struct a_coll_ocs_arg
**coapp
, *coap
;
927 temporary_compose_mode_hook_call((char*)-1, NULL
, NULL
);
929 coap
= *(coapp
= vp
);
930 *coapp
= (struct a_coll_ocs_arg
*)-1;
932 if(coap
->coa_stdin
!= NULL
)
933 Fclose(coap
->coa_stdin
);
934 else if(coap
->coa_pipe
[0] != -1)
935 close(coap
->coa_pipe
[0]);
937 if(coap
->coa_stdout
!= NULL
&& !Pclose(coap
->coa_stdout
, TRU1
))
938 *coap
->coa_senderr
= 111;
939 if(coap
->coa_pipe
[1] != -1)
940 close(coap
->coa_pipe
[1]);
942 opipe
= coap
->coa_opipe
;
943 oint
= coap
->coa_oint
;
948 safe_signal(SIGPIPE
, opipe
);
949 safe_signal(SIGINT
, oint
);
955 n_temporary_compose_hook_varset(void *arg
){ /* TODO v15: drop */
962 if((val
= hp
->h_subject
) == NULL
)
964 ok_vset(mailx_subject
, val
);
965 if((val
= detract(hp
->h_from
, GNAMEONLY
)) == NULL
)
967 ok_vset(mailx_from
, val
);
968 if((val
= detract(hp
->h_sender
, GNAMEONLY
)) == NULL
)
970 ok_vset(mailx_sender
, val
);
971 if((val
= detract(hp
->h_to
, GNAMEONLY
)) == NULL
)
973 ok_vset(mailx_to
, val
);
974 if((val
= detract(hp
->h_cc
, GNAMEONLY
)) == NULL
)
976 ok_vset(mailx_cc
, val
);
977 if((val
= detract(hp
->h_bcc
, GNAMEONLY
)) == NULL
)
979 ok_vset(mailx_bcc
, val
);
981 if((val
= hp
->h_mailx_command
) == NULL
)
983 ok_vset(mailx_command
, val
);
985 if((val
= detract(hp
->h_mailx_raw_to
, GNAMEONLY
)) == NULL
)
987 ok_vset(mailx_raw_to
, val
);
988 if((val
= detract(hp
->h_mailx_raw_cc
, GNAMEONLY
)) == NULL
)
990 ok_vset(mailx_raw_cc
, val
);
991 if((val
= detract(hp
->h_mailx_raw_bcc
, GNAMEONLY
)) == NULL
)
993 ok_vset(mailx_raw_bcc
, val
);
995 if((val
= detract(hp
->h_mailx_orig_from
, GNAMEONLY
)) == NULL
)
997 ok_vset(mailx_orig_from
, val
);
998 if((val
= detract(hp
->h_mailx_orig_to
, GNAMEONLY
)) == NULL
)
1000 ok_vset(mailx_orig_to
, val
);
1001 if((val
= detract(hp
->h_mailx_orig_cc
, GNAMEONLY
)) == NULL
)
1003 ok_vset(mailx_orig_cc
, val
);
1004 if((val
= detract(hp
->h_mailx_orig_bcc
, GNAMEONLY
)) == NULL
)
1006 ok_vset(mailx_orig_bcc
, val
);
1011 n_collect(enum n_mailsend_flags msf
, struct header
*hp
, struct message
*mp
,
1012 char const *quotefile
, si8_t
*checkaddr_err
)
1014 struct n_dig_msg_ctx dmc
;
1015 struct n_string s
, * volatile sp
;
1016 struct a_coll_ocs_arg
*coap
;
1018 int volatile t
, eofcnt
, getfields
;
1019 char volatile escape
;
1024 a_COAP_NOSIGTERM
= 1u<<8
1025 #define a_HARDERR() ((flags & (a_ERREXIT | a_IGNERR)) == a_ERREXIT)
1028 char const *cp
, *cp_base
, * volatile coapm
, * volatile ifs_saved
;
1029 size_t i
, linesize
; /* TODO line pool */
1031 sigset_t oset
, nset
;
1032 FILE * volatile sigfp
;
1035 n_DIG_MSG_COMPOSE_CREATE(&dmc
, hp
);
1043 ifs_saved
= coapm
= NULL
;
1047 /* Start catching signals from here, but we still die on interrupts
1048 * until we're in the main loop */
1050 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
1051 if ((_coll_saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
1052 safe_signal(SIGINT
, &_collint
);
1053 if ((_coll_savehup
= safe_signal(SIGHUP
, SIG_IGN
)) != SIG_IGN
)
1054 safe_signal(SIGHUP
, collhup
);
1055 if (sigsetjmp(_coll_abort
, 1))
1057 if (sigsetjmp(_coll_jmp
, 1))
1059 n_pstate
|= n_PS_COMPOSE_MODE
;
1060 sigprocmask(SIG_SETMASK
, &oset
, (sigset_t
*)NULL
);
1062 if ((_coll_fp
= Ftmp(NULL
, "collect", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==
1064 n_perr(_("collect: temporary mail file"), 0);
1068 /* If we are going to prompt for a subject, refrain from printing a newline
1069 * after the headers (since some people mind) */
1071 if(!(n_poption
& n_PO_t_FLAG
)){
1072 t
= GTO
| GSUBJECT
| GCC
| GNL
;
1073 if(ok_blook(fullnames
))
1076 if((n_psonce
& n_PSO_INTERACTIVE
) && !(n_pstate
& n_PS_ROBOT
)){
1077 if(hp
->h_subject
== NULL
&& ok_blook(asksub
)/* *ask* auto warped! */)
1078 t
&= ~GNL
, getfields
|= GSUBJECT
;
1080 if(hp
->h_to
== NULL
)
1081 t
&= ~GNL
, getfields
|= GTO
;
1083 if(!ok_blook(bsdcompat
) && !ok_blook(askatend
)){
1084 if(ok_blook(askbcc
))
1085 t
&= ~GNL
, getfields
|= GBCC
;
1087 t
&= ~GNL
, getfields
|= GCC
;
1096 if (!sigsetjmp(_coll_jmp
, 1)) {
1097 /* Ask for some headers first, as necessary */
1099 grab_headers(n_GO_INPUT_CTX_COMPOSE
, hp
, getfields
, 1);
1101 /* Execute compose-enter; delayed for -t mode */
1102 if(!(n_poption
& n_PO_t_FLAG
) &&
1103 (cp
= ok_vlook(on_compose_enter
)) != NULL
){
1104 setup_from_and_sender(hp
);
1105 temporary_compose_mode_hook_call(cp
, &n_temporary_compose_hook_varset
,
1109 /* TODO Mm: nope since it may require turning this into a multipart one */
1110 if(!(n_poption
& (n_PO_Mm_FLAG
| n_PO_t_FLAG
))){
1111 if(!a_coll_message_inject_head(_coll_fp
))
1114 /* Quote an original message */
1115 if(mp
!= NULL
&& !a_coll_quote_message(_coll_fp
, mp
,
1116 ((msf
& n_MAILSEND_IS_FWD
) != 0)))
1120 if (quotefile
!= NULL
) {
1121 if((n_pstate_err_no
= a_coll_include_file(quotefile
, FAL0
, FAL0
)
1126 if(n_psonce
& n_PSO_INTERACTIVE
){
1127 if(!(n_pstate
& n_PS_SOURCING
)){
1128 sp
= n_string_creat_auto(&s
);
1129 sp
= n_string_reserve(sp
, 80);
1132 if(!(n_poption
& n_PO_Mm_FLAG
) && !(n_pstate
& n_PS_ROBOT
)){
1133 /* Print what we have sofar also on the terminal (if useful) */
1134 if((cp
= ok_vlook(editalong
)) == NULL
){
1135 if(msf
& n_MAILSEND_HEADERS_PRINT
)
1136 n_puthead(TRU1
, hp
, n_stdout
, t
, SEND_TODISP
, CONV_NONE
,
1140 while ((c
= getc(_coll_fp
)) != EOF
) /* XXX bytewise, yuck! */
1142 if (fseek(_coll_fp
, 0, SEEK_END
))
1145 if(a_coll_edit(((*cp
== 'v') ? 'v' : 'e'), hp
, NULL
1148 /* Print msg mandated by the Mail Reference Manual */
1150 if((n_psonce
& n_PSO_INTERACTIVE
) && !(n_pstate
& n_PS_ROBOT
))
1151 fputs(_("(continue)\n"), n_stdout
);
1157 /* Come here for printing the after-signal message. Duplicate messages
1158 * won't be printed because the write is aborted if we get a SIGTTOU */
1159 if(_coll_hadintr
&& (n_psonce
& n_PSO_INTERACTIVE
) &&
1160 !(n_pstate
& n_PS_ROBOT
))
1161 n_err(_("\n(Interrupt -- one more to kill letter)\n"));
1164 /* If not under shell hook control */
1166 /* We're done with -M or -m TODO because: we are too stupid yet, above */
1167 if(n_poption
& n_PO_Mm_FLAG
)
1169 /* No command escapes, interrupts not expected. Simply copy STDIN */
1170 if(!(n_psonce
& n_PSO_INTERACTIVE
) &&
1171 !(n_poption
& (n_PO_t_FLAG
| n_PO_TILDE_FLAG
))){
1172 linebuf
= n_realloc(linebuf
, linesize
= LINESIZE
);
1173 while ((i
= fread(linebuf
, sizeof *linebuf
, linesize
, n_stdin
)) > 0) {
1174 if (i
!= fwrite(linebuf
, sizeof *linebuf
, i
, _coll_fp
))
1181 /* The interactive collect loop */
1183 escape
= *ok_vlook(escape
);
1184 flags
= ok_blook(errexit
) ? a_ERREXIT
: a_NONE
;
1187 enum {a_HIST_NONE
, a_HIST_ADD
= 1u<<0, a_HIST_GABBY
= 1u<<1} hist
;
1190 enum n_go_input_flags gif
;
1193 /* TODO optimize: no need to evaluate that anew for each loop tick! */
1194 gif
= n_GO_INPUT_CTX_COMPOSE
;
1195 histadd
= (sp
!= NULL
);
1196 if((n_psonce
& n_PSO_INTERACTIVE
) || (n_poption
& n_PO_TILDE_FLAG
)){
1197 if(!(n_poption
& n_PO_t_FLAG
) || (n_psonce
& n_PSO_t_FLAG_DONE
))
1198 gif
|= n_GO_INPUT_NL_ESC
;
1200 cnt
= n_go_input(gif
, n_empty
, &linebuf
, &linesize
, NULL
, &histadd
);
1201 hist
= histadd
? a_HIST_ADD
| a_HIST_GABBY
: a_HIST_NONE
;
1207 if((n_poption
& n_PO_t_FLAG
) && !(n_psonce
& n_PSO_t_FLAG_DONE
)){
1208 fflush_rewind(_coll_fp
);
1209 n_psonce
|= n_PSO_t_FLAG_DONE
;
1210 if(!a_coll_makeheader(_coll_fp
, hp
, checkaddr_err
, TRU1
))
1213 }else if((n_psonce
& n_PSO_INTERACTIVE
) && !(n_pstate
& n_PS_ROBOT
) &&
1214 ok_blook(ignoreeof
) && ++eofcnt
< 4){
1216 _("*ignoreeof* set, use `~.' to terminate letter\n"));
1217 n_go_input_clearerr();
1228 else if(coap
== NULL
){
1229 if(!(n_psonce
& n_PSO_INTERACTIVE
) && !(n_poption
& n_PO_TILDE_FLAG
))
1231 else if(cp
[0] == '.'){
1232 if(cnt
== 1 && (ok_blook(dot
) ||
1233 (ok_blook(posix
) && ok_blook(ignoreeof
))))
1237 if(cp
[0] != escape
){
1239 if(fwrite(cp
, sizeof *cp
, cnt
, _coll_fp
) != (size_t)cnt
)
1241 /* TODO n_PS_READLINE_NL is a terrible hack to ensure that _in_all_-
1242 * TODO _code_paths_ a file without trailing newline isn't modified
1243 * TODO to contain one; the "saw-newline" needs to be part of an
1244 * TODO I/O input machinery object */
1246 if(n_pstate
& n_PS_READLINE_NL
){
1247 if(putc('\n', _coll_fp
) == EOF
)
1253 c
= *(cp_base
= ++cp
);
1257 /* Avoid history entry? */
1258 while(spacechar(c
)){
1260 c
= *(cp_base
= ++cp
);
1265 /* It may just be an escaped escaped character, do that quick */
1269 /* Avoid hard *errexit*? */
1278 /* Trim input, also to gain a somewhat normalized history entry */
1283 x
.s
= n_UNCONST(cp
);
1285 n_str_trim_ifs(&x
, TRU1
);
1288 cnt
= (int)/*XXX*/x
.l
;
1291 if(hist
!= a_HIST_NONE
){
1292 sp
= n_string_assign_c(sp
, escape
);
1293 if(flags
& a_IGNERR
)
1294 sp
= n_string_push_c(sp
, '-');
1295 sp
= n_string_push_c(sp
, c
);
1297 sp
= n_string_push_c(sp
, ' ');
1298 sp
= n_string_push_buf(sp
, cp
, cnt
);
1302 /* Switch over all command escapes */
1306 char buf
[sizeof(n_UNIREPL
)];
1309 buf
[0] = c
, buf
[1] = '\0';
1310 else if(n_psonce
& n_PSO_UNICODE
)
1311 memcpy(buf
, n_unirepl
, sizeof n_unirepl
);
1313 buf
[0] = '?', buf
[1] = '\0';
1314 n_err(_("Unknown command escape: `%c%s'\n"), escape
, buf
);
1317 n_err(_("Invalid command escape usage: %s\n"),
1318 n_shexp_quote_cp(linebuf
, FAL0
));
1321 n_pstate_err_no
= n_ERR_INVAL
;
1325 /* Shell escape, send the balance of line to sh -c */
1326 if(cnt
== 0 || coap
!= NULL
)
1329 char const *argv
[2];
1333 n_pstate_ex_no
= c_shell(argv
); /* TODO history norm.; errexit? */
1337 /* Simulate end of file on input */
1338 if(cnt
!= 0 || coap
!= NULL
)
1340 goto jout
; /* TODO does not enter history, thus */
1343 /* Escape to command mode, but be nice! *//* TODO command expansion
1344 * TODO should be handled here so that we have unique history! */
1347 _execute_command(hp
, cp
, cnt
);
1348 if(ok_blook(errexit
))
1351 flags
&= ~a_ERREXIT
;
1352 if(n_pstate_ex_no
!= 0 && a_HARDERR())
1355 escape
= *ok_vlook(escape
);
1356 hist
&= ~a_HIST_GABBY
;
1358 /* case '<': <> 'd' */
1360 #ifdef HAVE_UISTRINGS
1362 "COMMAND ESCAPES (to be placed after a newline) excerpt:\n"
1363 "~. Commit and send message\n"
1364 "~: <command> Execute an internal command\n"
1365 "~< <file> Insert <file> (\"~<! <command>\": insert shell command)\n"
1366 "~@ [<files>] Edit [Add] attachments (file[=in-charset[#out-charset]])\n"
1367 "~c <users> Add users to Cc: list (`~b': to Bcc:)\n"
1368 "~e, ~v Edit message via $EDITOR / $VISUAL\n"
1371 "~F <msglist> Read in with headers, do not *indentprefix* lines\n"
1372 "~f <msglist> Like `~F', but honour `headerpick' configuration\n"
1373 "~H Edit From:, Reply-To: and Sender:\n"
1374 "~h Prompt for Subject:, To:, Cc: and Bcc:\n"
1375 "~i <variable> Insert a value and a newline (`~I': insert value)\n"
1376 "~M <msglist> Read in with headers, *indentprefix* (`~m': use `headerpick')\n"
1377 "~p Show current message compose buffer\n"
1378 "~Q <msglist> Read in using normal *quote* algorithm\n"
1381 "~r <file> Insert <file> (`~R': *indentprefix* lines)\n"
1382 " <file> may also be <- [HERE-DELIMITER]>\n"
1383 "~s <subject> Set Subject:\n"
1384 "~t <users> Add users to To: list\n"
1385 "~u <msglist> Read in without headers (`~U': *indentprefix* lines)\n"
1386 "~w <file> Write message onto file\n"
1387 "~x Abort composition, discard message (`~q': save in $DEAD)\n"
1388 "~| <command> Pipe message through shell filter (`~||': with headers)\n"
1390 #endif /* HAVE_UISTRINGS */
1393 n_pstate_err_no
= n_ERR_NONE
;
1397 struct attachment
*aplist
;
1399 /* Edit the attachment list */
1400 aplist
= hp
->h_attach
;
1401 hp
->h_attach
= NULL
;
1403 hp
->h_attach
= n_attachment_append_list(aplist
, cp
);
1405 hp
->h_attach
= n_attachment_list_edit(aplist
,
1406 n_GO_INPUT_CTX_COMPOSE
);
1407 n_pstate_err_no
= n_ERR_NONE
; /* XXX ~@ does NOT handle $!/$?! */
1408 n_pstate_ex_no
= 0; /* XXX */
1411 if(!n_dig_msg_circumflex(&dmc
, n_stdout
, cp
)){
1412 if(ferror(_coll_fp
))
1416 n_pstate_err_no
= n_ERR_NONE
; /* XXX */
1417 n_pstate_ex_no
= 0; /* XXX */
1418 hist
&= ~a_HIST_GABBY
;
1420 /* case '_': <> ':' */
1422 /* Pipe message through command. Collect output as new message */
1425 /* Is this request to do a "stream equivalent" to 'e' and 'v'? */
1430 if((n_pstate_err_no
= a_coll_pipe(cp
)) == n_ERR_NONE
)
1432 else if(ferror(_coll_fp
))
1434 else if(a_HARDERR())
1438 hist
&= ~a_HIST_GABBY
;
1442 /* Insert the contents of a sign variable */
1445 cp
= (c
== 'a') ? ok_vlook(sign
) : ok_vlook(Sign
);
1448 /* Add stuff to blind carbon copies list TODO join 'c' */
1456 if((np
= checkaddrs(lextract(cp
, GBCC
| GFULL
), EACM_NORMAL
, &soe
)
1458 hp
->h_bcc
= cat(hp
->h_bcc
, np
);
1460 n_pstate_err_no
= n_ERR_NONE
;
1464 n_pstate_err_no
= (soe
< 0) ? n_ERR_PERM
: n_ERR_INVAL
;
1467 hist
&= ~a_HIST_GABBY
;
1470 /* Add to the CC list TODO join 'b' */
1478 if((np
= checkaddrs(lextract(cp
, GCC
| GFULL
), EACM_NORMAL
, &soe
)
1480 hp
->h_cc
= cat(hp
->h_cc
, np
);
1482 n_pstate_err_no
= n_ERR_NONE
;
1486 n_pstate_err_no
= (soe
< 0) ? n_ERR_PERM
: n_ERR_INVAL
;
1489 hist
&= ~a_HIST_GABBY
;
1494 cp
= n_getdeadletter();
1499 /* Invoke a file: Search for the file name, then open it and copy
1500 * the contents to _coll_fp */
1502 n_err(_("Interpolate what file?\n"));
1505 n_pstate_err_no
= n_ERR_NOENT
;
1509 if(*cp
== '!' && c
== '<'){
1510 /* TODO hist. normalization */
1511 if((n_pstate_err_no
= a_coll_insert_cmd(_coll_fp
, ++cp
)
1513 if(ferror(_coll_fp
))
1522 /* Note this also expands things like
1523 * !:vput vexpr delim random 0
1525 if((cp
= fexpand(cp
, FEXP_LOCAL
| FEXP_NOPROTO
| FEXP_NSHELL
)
1529 n_pstate_err_no
= n_ERR_INVAL
;
1534 /* XXX race, and why not test everywhere, then? */
1535 if(n_is_dir(cp
, FAL0
)){
1536 n_err(_("%s: is a directory\n"), n_shexp_quote_cp(cp
, FAL0
));
1539 n_pstate_err_no
= n_ERR_ISDIR
;
1543 if((n_pstate_err_no
= a_coll_include_file(cp
, (c
== 'R'), TRU1
)
1545 if(ferror(_coll_fp
))
1552 n_pstate_err_no
= n_ERR_NONE
; /* XXX */
1553 n_pstate_ex_no
= 0; /* XXX */
1557 /* Edit the current message. 'e' -> use EDITOR, 'v' -> use VISUAL */
1558 if(cnt
!= 0 || coap
!= NULL
)
1561 if((n_pstate_err_no
= a_coll_edit(c
,
1562 ((c
== '|' || ok_blook(editheaders
)) ? hp
: NULL
), cp
)
1565 else if(ferror(_coll_fp
))
1567 else if(a_HARDERR())
1579 /* Interpolate the named messages, if we are in receiving mail mode.
1580 * Does the standard list processing garbage. If ~f is given, we
1581 * don't shift over */
1582 if((n_pstate_err_no
= a_coll_forward(cp
, _coll_fp
, c
)) == n_ERR_NONE
)
1584 else if(ferror(_coll_fp
))
1586 else if(a_HARDERR())
1592 /* Grab extra headers */
1596 grab_headers(n_GO_INPUT_CTX_COMPOSE
, hp
, GEXTRA
, 0);
1597 while(check_from_and_sender(hp
->h_from
, hp
->h_sender
) == NULL
);
1598 n_pstate_err_no
= n_ERR_NONE
; /* XXX */
1599 n_pstate_ex_no
= 0; /* XXX */
1602 /* Grab a bunch of headers */
1606 grab_headers(n_GO_INPUT_CTX_COMPOSE
, hp
,
1607 (GTO
| GSUBJECT
| GCC
| GBCC
),
1608 (ok_blook(bsdcompat
) && ok_blook(bsdorder
)));
1609 while(hp
->h_to
== NULL
);
1610 n_pstate_err_no
= n_ERR_NONE
; /* XXX */
1611 n_pstate_ex_no
= 0; /* XXX */
1615 /* Insert a variable into the file */
1618 cp
= n_var_vlook(cp
, TRU1
);
1620 if(cp
== NULL
|| *cp
== '\0')
1622 if(!a_coll_putesc(cp
, (c
!= 'I'), _coll_fp
))
1624 if((n_psonce
& n_PSO_INTERACTIVE
) && !(n_pstate
& n_PS_ROBOT
) &&
1625 !a_coll_putesc(cp
, (c
!= 'I'), n_stdout
) ||
1626 fflush(n_stdout
) == EOF
)
1628 n_pstate_err_no
= n_ERR_NONE
; /* XXX */
1629 n_pstate_ex_no
= 0; /* XXX */
1631 /* case 'M': <> 'F' */
1632 /* case 'm': <> 'f' */
1634 /* Print current state of the message without altering anything */
1637 print_collf(_coll_fp
, hp
); /* XXX pstate_err_no ++ */
1638 if(ferror(_coll_fp
))
1640 n_pstate_err_no
= n_ERR_NONE
; /* XXX */
1641 n_pstate_ex_no
= 0; /* XXX */
1643 /* case 'Q': <> 'F' */
1646 /* Force a quit, act like an interrupt had happened */
1649 /* If we are running a splice hook, assume it quits on its own now,
1650 * otherwise we (no true out-of-band IPC to signal this state, XXX sic)
1651 * have to SIGTERM it in order to stop this wild beast */
1652 flags
|= a_COAP_NOSIGTERM
;
1654 _collint((c
== 'x') ? 0 : SIGINT
);
1657 /* case 'R': <> 'd' */
1658 /* case 'r': <> 'd' */
1660 /* Set the Subject list */
1663 /* Subject:; take care for Debian #419840 and strip any \r and \n */
1664 if(n_anyof_cp("\n\r", hp
->h_subject
= savestr(cp
))){
1667 n_err(_("-s: normalizing away invalid ASCII NL / CR bytes\n"));
1668 for(xp
= hp
->h_subject
; *xp
!= '\0'; ++xp
)
1669 if(*xp
== '\n' || *xp
== '\r')
1671 n_pstate_err_no
= n_ERR_INVAL
;
1674 n_pstate_err_no
= n_ERR_NONE
;
1679 /* Add to the To: list TODO join 'b', 'c' */
1687 if((np
= checkaddrs(lextract(cp
, GTO
| GFULL
), EACM_NORMAL
, &soe
)
1689 hp
->h_to
= cat(hp
->h_to
, np
);
1691 n_pstate_err_no
= n_ERR_NONE
;
1695 n_pstate_err_no
= (soe
< 0) ? n_ERR_PERM
: n_ERR_INVAL
;
1698 hist
&= ~a_HIST_GABBY
;
1700 /* case 'U': <> 'F' */
1701 /* case 'u': <> 'f' */
1702 /* case 'v': <> 'e' */
1704 /* Write the message on a file */
1707 if((cp
= fexpand(cp
, FEXP_LOCAL
| FEXP_NOPROTO
)) == NULL
){
1708 n_err(_("Write what file!?\n"));
1711 n_pstate_err_no
= n_ERR_INVAL
;
1716 if((n_pstate_err_no
= a_coll_write(cp
, _coll_fp
, 1)) == n_ERR_NONE
)
1718 else if(ferror(_coll_fp
))
1720 else if(a_HARDERR())
1725 /* case 'x': <> 'q' */
1728 /* Finally place an entry in history as applicable */
1734 if(hist
& a_HIST_ADD
){
1735 /* Do not add *escape* to the history in order to allow history search
1736 * to be handled generically in the MLE regardless of actual *escape*
1738 n_tty_addhist(&n_string_cp(sp
)[1], (n_GO_INPUT_CTX_COMPOSE
|
1739 (hist
& a_HIST_GABBY
? n_GO_INPUT_HIST_GABBY
: n_GO_INPUT_NONE
)));
1746 /* Do we have *on-compose-splice-shell*, or *on-compose-splice*?
1747 * TODO Usual f...ed up state of signals and terminal etc. */
1748 if(coap
== NULL
&& (cp
= ok_vlook(on_compose_splice_shell
)) != NULL
) Jocs
:{
1749 union {int (*ptf
)(void); char const *sh
;} u
;
1752 /* Reset *escape* and more to their defaults. On change update manual! */
1753 if(ifs_saved
== NULL
)
1754 ifs_saved
= savestr(ok_vlook(ifs
));
1755 escape
= n_ESCAPE
[0];
1759 /* XXX Due Popen() fflush(NULL) in PTF mode, ensure nothing to flush */
1760 /*if(!n_real_seek(_coll_fp, 0, SEEK_END))
1762 u
.ptf
= &a_coll_ocs__mac
;
1764 a_coll_ocs__macname
= cp
= coapm
;
1766 u
.sh
= ok_vlook(SHELL
);
1771 coap
= n_lofi_alloc(n_VSTRUCT_SIZEOF(struct a_coll_ocs_arg
, coa_cmd
1773 coap
->coa_pipe
[0] = coap
->coa_pipe
[1] = -1;
1774 coap
->coa_stdin
= coap
->coa_stdout
= NULL
;
1775 coap
->coa_senderr
= checkaddr_err
;
1776 memcpy(coap
->coa_cmd
, cp
, i
);
1779 coap
->coa_opipe
= safe_signal(SIGPIPE
, SIG_IGN
);
1780 coap
->coa_oint
= safe_signal(SIGINT
, SIG_IGN
);
1783 if(pipe_cloexec(coap
->coa_pipe
) != -1 &&
1784 (coap
->coa_stdin
= Fdopen(coap
->coa_pipe
[0], "r", FAL0
)) != NULL
&&
1785 (coap
->coa_stdout
= Popen(cmd
, "W", u
.sh
, NULL
, coap
->coa_pipe
[1])
1787 close(coap
->coa_pipe
[1]);
1788 coap
->coa_pipe
[1] = -1;
1790 temporary_compose_mode_hook_call(NULL
, NULL
, NULL
);
1791 n_go_splice_hack(coap
->coa_cmd
, coap
->coa_stdin
, coap
->coa_stdout
,
1792 (n_psonce
& ~(n_PSO_INTERACTIVE
| n_PSO_TTYIN
| n_PSO_TTYOUT
)),
1793 &a_coll_ocs__finalize
, &coap
);
1794 /* Hook version protocol for ~^: update manual upon change! */
1795 fputs(n_DIG_MSG_PLUMBING_VERSION
"\n", n_stdout
/*coap->coa_stdout*/);
1800 a_coll_ocs__finalize(coap
);
1801 n_perr(_("Cannot invoke *on-compose-splice(-shell)?*"), c
);
1804 if(*checkaddr_err
!= 0){
1808 if(coapm
== NULL
&& (coapm
= ok_vlook(on_compose_splice
)) != NULL
)
1811 ok_vset(ifs
, ifs_saved
);
1815 /* Final chance to edit headers, if not already above */
1816 if((n_psonce
& n_PSO_INTERACTIVE
) && !(n_pstate
& n_PS_ROBOT
)){
1817 if(ok_blook(bsdcompat
) || ok_blook(askatend
)){
1823 if(ok_blook(askbcc
))
1826 grab_headers(n_GO_INPUT_CTX_COMPOSE
, hp
, gf
, 1);
1828 if(ok_blook(askattach
))
1829 hp
->h_attach
= n_attachment_list_edit(hp
->h_attach
,
1830 n_GO_INPUT_CTX_COMPOSE
);
1833 /* Execute compose-leave */
1834 if((cp
= ok_vlook(on_compose_leave
)) != NULL
){
1835 setup_from_and_sender(hp
);
1836 temporary_compose_mode_hook_call(cp
, &n_temporary_compose_hook_varset
,
1840 /* Add automatic receivers */
1841 if ((cp
= ok_vlook(autocc
)) != NULL
&& *cp
!= '\0')
1842 hp
->h_cc
= cat(hp
->h_cc
, checkaddrs(lextract(cp
, GCC
|
1843 (ok_blook(fullnames
) ? GFULL
| GSKIN
: GSKIN
)),
1844 EACM_NORMAL
, checkaddr_err
));
1845 if ((cp
= ok_vlook(autobcc
)) != NULL
&& *cp
!= '\0')
1846 hp
->h_bcc
= cat(hp
->h_bcc
, checkaddrs(lextract(cp
, GBCC
|
1847 (ok_blook(fullnames
) ? GFULL
| GSKIN
: GSKIN
)),
1848 EACM_NORMAL
, checkaddr_err
));
1849 if (*checkaddr_err
!= 0)
1852 if((n_psonce
& n_PSO_INTERACTIVE
) && !(n_pstate
& n_PS_ROBOT
) &&
1856 ifs_saved
= coapm
= NULL
;
1859 fprintf(n_stdout
, _("-------\nEnvelope contains:\n")); /* xxx SEARCH112 */
1860 if(!n_puthead(TRU1
, hp
, n_stdout
,
1861 GIDENT
| GREF_IRT
| GSUBJECT
| GTO
| GCC
| GBCC
| GBCC_IS_FCC
|
1862 GCOMMA
, SEND_TODISP
, CONV_NONE
, NULL
, NULL
))
1866 if(n_go_input(n_GO_INPUT_CTX_COMPOSE
| n_GO_INPUT_NL_ESC
,
1867 _("Send this message [yes/no, empty: recompose]? "),
1868 &linebuf
, &linesize
, NULL
, NULL
) < 0){
1869 if(!n_go_input_is_eof())
1874 if((b
= n_boolify(linebuf
, UIZ_MAX
, TRUM1
)) < FAL0
)
1882 /* TODO Cannot do since it may require turning this into a multipart one */
1883 if(n_poption
& n_PO_Mm_FLAG
)
1886 /* Place signature? */
1887 if((cp
= ok_vlook(signature
)) != NULL
&& *cp
!= '\0'){ /* TODO OBSOLETE */
1890 n_OBSOLETE(_("please use *on-compose-{leave,splice}* and/or "
1891 "*message-inject-tail*, not *signature*"));
1893 if((cpq
= fexpand(cp
, FEXP_LOCAL
| FEXP_NOPROTO
)) == NULL
){
1894 n_err(_("*signature* expands to invalid file: %s\n"),
1895 n_shexp_quote_cp(cp
, FAL0
));
1898 cpq
= n_shexp_quote_cp(cp
= cpq
, FAL0
);
1900 if((sigfp
= Fopen(cp
, "r")) == NULL
){
1901 n_err(_("Can't open *signature* %s: %s\n"),
1902 cpq
, n_err_to_doc(n_err_no
));
1907 linebuf
= n_alloc(linesize
= LINESIZE
);
1909 while((i
= fread(linebuf
, sizeof *linebuf
, linesize
, n_UNVOLATILE(sigfp
)))
1912 if(i
!= fwrite(linebuf
, sizeof *linebuf
, i
, _coll_fp
))
1917 FILE *x
= n_UNVOLATILE(sigfp
);
1918 int e
= n_err_no
, ise
= ferror(x
);
1924 n_err(_("Errors while reading *signature* %s: %s\n"),
1925 cpq
, n_err_to_doc(e
));
1930 if(c
!= '\0' && c
!= '\n')
1931 putc('\n', _coll_fp
);
1934 { char const *cp_obsolete
= ok_vlook(NAIL_TAIL
);
1936 if(cp_obsolete
!= NULL
)
1937 n_OBSOLETE(_("please use *message-inject-tail*, not *NAIL_TAIL*"));
1939 if((cp
= ok_vlook(message_inject_tail
)) != NULL
||
1940 (cp
= cp_obsolete
) != NULL
){
1941 if(!a_coll_putesc(cp
, TRU1
, _coll_fp
))
1943 if((n_psonce
& n_PSO_INTERACTIVE
) && !(n_pstate
& n_PS_ROBOT
) &&
1944 !a_coll_putesc(cp
, TRU1
, n_stdout
) || fflush(n_stdout
) == EOF
)
1950 if(fflush(_coll_fp
))
1954 if(mp
!= NULL
&& ok_blook(quote_as_attachment
)){
1955 struct attachment
*ap
;
1957 ap
= n_autorec_calloc(1, sizeof *ap
);
1958 if((ap
->a_flink
= hp
->h_attach
) != NULL
)
1959 hp
->h_attach
->a_blink
= ap
;
1961 ap
->a_msgno
= (int)PTR2SIZE(mp
- message
+ 1);
1962 ap
->a_content_description
= _("Original message content");
1966 if (linebuf
!= NULL
)
1968 sigprocmask(SIG_BLOCK
, &nset
, NULL
);
1969 n_DIG_MSG_COMPOSE_GUT(&dmc
);
1970 n_pstate
&= ~n_PS_COMPOSE_MODE
;
1971 safe_signal(SIGINT
, _coll_saveint
);
1972 safe_signal(SIGHUP
, _coll_savehup
);
1973 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1980 if(coap
!= NULL
&& coap
!= (struct a_coll_ocs_arg
*)-1){
1981 if(!(flags
& a_COAP_NOSIGTERM
))
1982 n_psignal(coap
->coa_stdout
, SIGTERM
);
1983 n_go_splice_hack_remove_after_jump();
1986 if(ifs_saved
!= NULL
){
1987 ok_vset(ifs
, ifs_saved
);
1991 Fclose(n_UNVOLATILE(sigfp
));
1994 if(_coll_fp
!= NULL
){
2001 assert(checkaddr_err
!= NULL
);
2002 /* TODO We don't save in $DEAD upon error because msg not readily composed?
2003 * TODO But this no good, it should go ZOMBIE / DRAFT / POSTPONED or what! */
2004 if(*checkaddr_err
!= 0){
2005 if(*checkaddr_err
== 111)
2006 n_err(_("Compose mode splice hook failure\n"));
2008 n_err(_("Some addressees were classified as \"hard error\"\n"));
2009 }else if(_coll_hadintr
== 0){
2010 *checkaddr_err
= TRU1
; /* TODO ugly: "sendout_error" now.. */
2011 n_err(_("Failed to prepare composed message\n"));