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 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
9 * Copyright (c) 1980, 1993
10 * The Regents of the University of California. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #define n_FILE collect
39 #ifndef HAVE_AMALGAMATION
43 struct a_coll_ocs_arg
{
44 sighandler_type coa_opipe
;
45 sighandler_type coa_oint
;
46 FILE *coa_stdin
; /* The descriptor (pipe(2)+Fdopen()) we read from */
47 FILE *coa_stdout
; /* The Popen()ed pipe through which we write to the hook */
48 int coa_pipe
[2]; /* ..backing .coa_stdin */
49 si8_t
*coa_senderr
; /* Set to 1 on failure */
50 char coa_cmd
[n_VFIELD_SIZE(0)];
53 /* The following hookiness with global variables is so that on receipt of an
54 * interrupt signal, the partial message can be salted away on *DEAD* */
56 static sighandler_type _coll_saveint
; /* Previous SIGINT value */
57 static sighandler_type _coll_savehup
; /* Previous SIGHUP value */
58 static FILE *_coll_fp
; /* File for saving away */
59 static int volatile _coll_hadintr
; /* Have seen one SIGINT so far */
60 static sigjmp_buf _coll_jmp
; /* To get back to work */
61 static sigjmp_buf _coll_abort
; /* To end collection with error */
62 static char const *a_coll_ocs__macname
; /* *on-compose-splice* */
64 /* Handle `~:', `~_' and some hooks; hp may be NULL */
65 static void _execute_command(struct header
*hp
, char const *linebuf
,
69 static bool_t
a_coll_include_file(char const *name
, bool_t indent
,
72 /* Execute cmd and insert its standard output into fp */
73 static bool_t
a_coll_insert_cmd(FILE *fp
, char const *cmd
);
76 static void print_collf(FILE *collf
, struct header
*hp
);
78 /* Write a file, ex-like if f set */
79 static int exwrite(char const *name
, FILE *fp
, int f
);
81 /* Parse off the message header from fp and store relevant fields in hp,
82 * replace _coll_fp with a shiny new version without any header */
83 static enum okay
makeheader(FILE *fp
, struct header
*hp
, si8_t
*checkaddr_err
);
85 /* Edit the message being collected on fp. On return, make the edit file the
87 static void mesedit(int c
, struct header
*hp
);
89 /* Pipe the message through the command. Old message is on stdin of command,
90 * new message collected from stdout. Shell must return 0 to accept new msg */
91 static void mespipe(char *cmd
);
93 /* Interpolate the named messages into the current message, possibly doing
94 * indent stuff. The flag argument is one of the command escapes: [mMfFuU].
95 * Return a count of the number of characters now in the message, or -1 if an
96 * error is encountered writing the message temporary */
97 static int forward(char *ms
, FILE *fp
, int f
);
100 static bool_t
a_collect_plumbing(char const *ms
, struct header
*p
);
102 /* On interrupt, come here to save the partial message in ~/dead.letter.
103 * Then jump out of the collection loop */
104 static void _collint(int s
);
106 static void collhup(int s
);
108 static int putesc(char const *s
, FILE *stream
); /* TODO wysh set! */
110 /* temporary_compose_mode_hook_call() setter hook */
111 static void a_coll__hook_setter(void *arg
);
113 /* *on-compose-splice* driver and *on-compose-splice(-shell)?* finalizer */
114 static int a_coll_ocs__mac(void);
115 static void a_coll_ocs__finalize(void *vp
);
118 _execute_command(struct header
*hp
, char const *linebuf
, size_t linesize
){
119 /* The problem arises if there are rfc822 message attachments and the
120 * user uses `~:' to change the current file. TODO Unfortunately we
121 * TODO cannot simply keep a pointer to, or increment a reference count
122 * TODO of the current `file' (mailbox that is) object, because the
123 * TODO codebase doesn't deal with that at all; so, until some far
124 * TODO later time, copy the name of the path, and warn the user if it
125 * TODO changed; we COULD use the AC_TMPFILE attachment type, i.e.,
126 * TODO copy the message attachments over to temporary files, but that
127 * TODO would require more changes so that the user still can recognize
128 * TODO in `~@' etc. that its a rfc822 message attachment; see below */
130 struct attachment
*ap
;
131 char * volatile mnbuf
;
137 n_SIGMAN_ENTER_SWITCH(&sm
, n_SIGMAN_ALL
){
144 /* If the above todo is worked, remove or outsource to attachment.c! */
145 if(hp
!= NULL
&& (ap
= hp
->h_attach
) != NULL
) do
147 mnbuf
= sstrdup(mailname
);
150 while((ap
= ap
->a_flink
) != NULL
);
152 n_go_command(n_GO_INPUT_CTX_COMPOSE
, linebuf
);
154 n_sigman_cleanup_ping(&sm
);
157 if(strcmp(mnbuf
, mailname
))
158 n_err(_("Mailbox changed: it is likely that existing "
159 "rfc822 attachments became invalid!\n"));
163 n_sigman_leave(&sm
, n_SIGMAN_VIPSIGS_NTTYOUT
);
167 a_coll_include_file(char const *name
, bool_t indent
, bool_t writestat
){
169 char const *heredb
, *indb
;
171 size_t linesize
, heredl
, indl
, cnt
, linelen
;
178 linebuf
= 0; /* TODO line pool */
183 /* The -M case is special */
184 if(name
== (char*)-1){
187 }else if(name
[0] == '-' &&
188 (name
[1] == '\0' || blankspacechar(name
[1]))){
191 if(!(n_psonce
& n_PSO_INTERACTIVE
)){
192 n_err(_("~< -: HERE-delimiter required in non-interactive mode\n"));
196 for(heredb
= &name
[2]; *heredb
!= '\0' && blankspacechar(*heredb
);
199 if((heredl
= strlen(heredb
)) == 0){
200 n_err(_("~< - HERE-delimiter: delimiter must not be empty\n"));
205 }else if((fbuf
= Fopen(name
, "r")) == NULL
){
213 if((indb
= ok_vlook(indentprefix
)) == NULL
)
214 indb
= INDENT_DEFAULT
;
220 while(fgetline(&linebuf
, &linesize
, (fbuf
== n_stdin
? NULL
: &cnt
),
221 &linelen
, fbuf
, 0) != NULL
){
222 if(heredl
> 0 && heredl
== linelen
- 1 &&
223 !memcmp(heredb
, linebuf
, heredl
)){
229 if(fwrite(indb
, sizeof *indb
, indl
, _coll_fp
) != indl
)
234 if(fwrite(linebuf
, sizeof *linebuf
, linelen
, _coll_fp
) != linelen
)
255 fprintf(n_stdout
, "%s%s %" PRId64
"/%" PRId64
"\n",
256 n_shexp_quote_cp(name
, FAL0
), (rv
? n_empty
: " " n_ERROR
), lc
, cc
);
262 a_coll_insert_cmd(FILE *fp
, char const *cmd
){
270 if((ibuf
= Popen(cmd
, "r", ok_vlook(SHELL
), NULL
, 0)) != NULL
){
274 while((c
= getc(ibuf
)) != EOF
){ /* XXX bytewise, yuck! */
275 if(putc(c
, fp
) == EOF
){
283 if(!feof(ibuf
) || ferror(ibuf
))
285 if(!Pclose(ibuf
, TRU1
))
292 fprintf(n_stdout
, "CMD%s %" PRId64
"/%" PRId64
"\n",
293 (rv
? n_empty
: " " n_ERROR
), lc
, cc
);
299 print_collf(FILE *cf
, struct header
*hp
)
303 size_t cnt
, linesize
, linelen
;
307 cnt
= (size_t)fsize(cf
);
309 if((obuf
= Ftmp(NULL
, "collfp", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
){
310 n_perr(_("Can't create temporary file for `~p' command"), 0);
316 fprintf(obuf
, _("-------\nMessage contains:\n"));
317 puthead(TRU1
, hp
, obuf
,
318 (GIDENT
| GTO
| GSUBJECT
| GCC
| GBCC
| GNL
| GFILES
| GCOMMA
),
319 SEND_TODISP
, CONV_NONE
, NULL
, NULL
);
323 while(fgetline(&lbuf
, &linesize
, &cnt
, &linelen
, cf
, 1))
324 prout(lbuf
, linelen
, obuf
);
328 if(hp
->h_attach
!= NULL
){
329 fputs(_("-------\nAttachments:\n"), obuf
);
330 n_attachment_list_print(hp
->h_attach
, obuf
);
335 page_or_print(obuf
, 0);
342 exwrite(char const *name
, FILE *fp
, int f
)
350 fprintf(n_stdout
, "%s ", n_shexp_quote_cp(name
, FAL0
));
354 if ((of
= Fopen(name
, "a")) == NULL
) {
360 while ((c
= getc(fp
)) != EOF
) {
364 if (putc(c
, of
) == EOF
) {
369 fprintf(n_stdout
, _("%" PRId64
"/%" PRId64
"\n"), lc
, cc
);
380 putc('\n', n_stdout
);
386 makeheader(FILE *fp
, struct header
*hp
, si8_t
*checkaddr_err
)
393 if ((nf
= Ftmp(NULL
, "colhead", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==NULL
) {
394 n_perr(_("temporary mail edit file"), 0);
398 extract_header(fp
, hp
, checkaddr_err
);
399 if (checkaddr_err
!= NULL
&& *checkaddr_err
!= 0)
402 while ((c
= getc(fp
)) != EOF
) /* XXX bytewise, yuck! */
408 if (check_from_and_sender(hp
->h_from
, hp
->h_sender
) == NULL
)
417 mesedit(int c
, struct header
*hp
)
420 sighandler_type sigint
;
424 if(!(saved
= ok_blook(add_file_recipients
)))
425 ok_bset(add_file_recipients
);
427 sigint
= safe_signal(SIGINT
, SIG_IGN
);
428 nf
= run_editor(_coll_fp
, (off_t
)-1, c
, FAL0
, hp
, NULL
, SEND_MBOX
, sigint
);
432 makeheader(nf
, hp
, NULL
);
434 fseek(nf
, 0L, SEEK_END
);
439 safe_signal(SIGINT
, sigint
);
442 ok_bclear(add_file_recipients
);
450 sighandler_type sigint
;
453 sigint
= safe_signal(SIGINT
, SIG_IGN
);
455 if ((nf
= Ftmp(NULL
, "colpipe", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==NULL
) {
456 n_perr(_("temporary mail edit file"), 0);
460 /* stdin = current message. stdout = new message */
462 if (n_child_run(ok_vlook(SHELL
), 0, fileno(_coll_fp
), fileno(nf
), "-c",
463 cmd
, NULL
, NULL
) < 0) {
468 if (fsize(nf
) == 0) {
469 n_err(_("No bytes from %s !?\n"), n_shexp_quote_cp(cmd
, FAL0
));
475 fseek(nf
, 0L, SEEK_END
);
479 safe_signal(SIGINT
, sigint
);
484 forward(char *ms
, FILE *fp
, int f
)
487 struct n_ignore
const *itp
;
489 enum sendaction action
;
492 msgvec
= salloc((size_t)(msgCount
+ 1) * sizeof *msgvec
);
493 if (getmsglist(ms
, msgvec
, 0) < 0)
496 *msgvec
= first(0, MMNORM
);
498 n_err(_("No appropriate messages\n"));
504 if (f
== 'f' || f
== 'F' || f
== 'u')
506 else if ((tabst
= ok_vlook(indentprefix
)) == NULL
)
507 tabst
= INDENT_DEFAULT
;
508 if (f
== 'u' || f
== 'U')
511 itp
= upperchar(f
) ? NULL
: n_IGNORE_TYPE
;
512 action
= (upperchar(f
) && f
!= 'U') ? SEND_QUOTE_ALL
: SEND_QUOTE
;
514 fprintf(n_stdout
, _("Interpolating:"));
516 for (; *msgvec
!= 0; ++msgvec
) {
517 struct message
*mp
= message
+ *msgvec
- 1;
520 fprintf(n_stdout
, " %d", *msgvec
);
522 if (sendmp(mp
, fp
, itp
, tabst
, action
, NULL
) < 0) {
523 n_perr(_("temporary mail file"), 0);
530 fprintf(n_stdout
, "\n");
537 a_collect_plumbing(char const *ms
, struct header
*hp
){
538 /* TODO _collect_plumbing: instead of fields the basic headers should
539 * TODO be in an array and have IDs, like in termcap etc., so then this
540 * TODO could be simplified as table-walks. Also true for arg-checks! */
541 char const *cp
, *cmd
[4];
544 /* Protcol version for *on-compose-splice** -- update manual on change! */
545 #define a_COLL_PLUMBING_VERSION "0 0 1"
551 for(i
= 0; i
< n_NELEM(cmd
); ++i
){
552 while(blankchar(*cp
))
557 if(i
< n_NELEM(cmd
) - 1)
558 for(cmd
[i
] = cp
++; *cp
!= '\0' && !blankchar(*cp
); ++cp
)
561 /* Last slot takes all the rest of the line, less trailing WS */
562 for(cmd
[i
] = cp
++; *cp
!= '\0'; ++cp
)
564 while(blankchar(cp
[-1]))
567 cmd
[i
] = savestrbuf(cmd
[i
], PTR2SIZE(cp
- cmd
[i
]));
572 if(n_UNLIKELY(cmd
[0] == NULL
))
575 if(is_asccaseprefix(cmd
[0], "header")){
576 struct n_header_field
*hfp
;
577 struct name
*np
, **npp
;
579 if(cmd
[1] == NULL
|| is_asccaseprefix(cmd
[1], "list")){
581 fputs("210", n_stdout
);
582 if(hp
->h_from
!= NULL
) fputs(" From", n_stdout
);
583 if(hp
->h_sender
!= NULL
) fputs(" Sender", n_stdout
);
584 if(hp
->h_to
!= NULL
) fputs(" To", n_stdout
);
585 if(hp
->h_cc
!= NULL
) fputs(" Cc", n_stdout
);
586 if(hp
->h_bcc
!= NULL
) fputs(" Bcc", n_stdout
);
587 if(hp
->h_subject
!= NULL
) fputs(" Subject", n_stdout
);
588 if(hp
->h_replyto
!= NULL
) fputs(" Reply-To", n_stdout
);
589 if(hp
->h_mft
!= NULL
) fputs(" Mail-Followup-To", n_stdout
);
590 if(hp
->h_message_id
!= NULL
) fputs(" Message-ID", n_stdout
);
591 if(hp
->h_ref
!= NULL
) fputs(" References", n_stdout
);
592 if(hp
->h_in_reply_to
!= NULL
) fputs(" In-Reply-To", n_stdout
);
593 for(hfp
= hp
->h_user_headers
; hfp
!= NULL
; hfp
= hfp
->hf_next
){
595 fputs(&hfp
->hf_dat
[0], n_stdout
);
597 putc('\n', n_stdout
);
604 if(!asccasecmp(cmd
[2], "from")){
607 fprintf(n_stdout
, "%s %s\n", (np
== NULL
? "501" : "210"), cp
);
610 if(!asccasecmp(cmd
[2], cp
= "Sender")){
614 if(!asccasecmp(cmd
[2], cp
= "To")){
618 if(!asccasecmp(cmd
[2], cp
= "Cc")){
622 if(!asccasecmp(cmd
[2], cp
= "Bcc")){
626 if(!asccasecmp(cmd
[2], cp
= "Subject")){
627 np
= (struct name
*)-1;
630 if(!asccasecmp(cmd
[2], cp
= "Reply-To")){
634 if(!asccasecmp(cmd
[2], cp
= "Mail-Followup-To")){
638 if(!asccasecmp(cmd
[2], cp
= "Message-ID")){
639 np
= hp
->h_message_id
;
642 if(!asccasecmp(cmd
[2], cp
= "References")){
646 if(!asccasecmp(cmd
[2], cp
= "In-Reply-To")){
647 np
= hp
->h_in_reply_to
;
651 /* Primitive name normalization XXX header object should
652 * XXX have a more sophisticated accessible one */
656 cp
= xp
= savestr(cmd
[2]);
657 xp
[0] = upperchar(xp
[0]);
659 xp
[0] = lowerchar(xp
[0]);
661 for(hfp
= hp
->h_user_headers
;; hfp
= hfp
->hf_next
){
664 else if(!asccasecmp(cp
, &hfp
->hf_dat
[0])){
665 fprintf(n_stdout
, "210 %s\n", cp
);
673 if(is_asccaseprefix(cmd
[1], "show")){
674 if(cmd
[2] == NULL
|| cmd
[3] != NULL
)
677 if(!asccasecmp(cmd
[2], "from")){
681 fprintf(n_stdout
, "211 %s\n", cp
);
682 do if(!(np
->n_type
& GDEL
))
683 fprintf(n_stdout
, "%s %s\n", np
->n_name
, np
->n_fullname
);
684 while((np
= np
->n_flink
) != NULL
);
685 putc('\n', n_stdout
);
690 if(!asccasecmp(cmd
[2], cp
= "Sender")){
694 if(!asccasecmp(cmd
[2], cp
= "To")){
698 if(!asccasecmp(cmd
[2], cp
= "Cc")){
702 if(!asccasecmp(cmd
[2], cp
= "Bcc")){
706 if(!asccasecmp(cmd
[2], cp
= "Reply-To")){
710 if(!asccasecmp(cmd
[2], cp
= "Mail-Followup-To")){
714 if(!asccasecmp(cmd
[2], cp
= "Message-ID")){
715 np
= hp
->h_message_id
;
718 if(!asccasecmp(cmd
[2], cp
= "References")){
722 if(!asccasecmp(cmd
[2], cp
= "In-Reply-To")){
723 np
= hp
->h_in_reply_to
;
727 if(!asccasecmp(cmd
[2], cp
= "Subject")){
728 if(hp
->h_subject
!= NULL
)
729 fprintf(n_stdout
, "212 %s\n%s\n\n", cp
, hp
->h_subject
);
731 fprintf(n_stdout
, "501 %s\n", cp
);
735 /* Primitive name normalization XXX header object should
736 * XXX have a more sophisticated accessible one */
741 cp
= xp
= savestr(cmd
[2]);
742 xp
[0] = upperchar(xp
[0]);
744 xp
[0] = lowerchar(xp
[0]);
746 for(any
= FAL0
, hfp
= hp
->h_user_headers
; hfp
!= NULL
;
748 if(!asccasecmp(cp
, &hfp
->hf_dat
[0])){
750 fprintf(n_stdout
, "212 %s\n", cp
);
752 fprintf(n_stdout
, "%s\n", &hfp
->hf_dat
[hfp
->hf_nl
+1]);
756 putc('\n', n_stdout
);
763 if(is_asccaseprefix(cmd
[1], "remove")){
764 if(cmd
[2] == NULL
|| cmd
[3] != NULL
)
767 if(!asccasecmp(cmd
[2], "from")){
772 fprintf(n_stdout
, "210 %s\n", cp
);
777 if(!asccasecmp(cmd
[2], cp
= "Sender")){
781 if(!asccasecmp(cmd
[2], cp
= "To")){
785 if(!asccasecmp(cmd
[2], cp
= "Cc")){
789 if(!asccasecmp(cmd
[2], cp
= "Bcc")){
793 if(!asccasecmp(cmd
[2], cp
= "Reply-To")){
794 npp
= &hp
->h_replyto
;
797 if(!asccasecmp(cmd
[2], cp
= "Mail-Followup-To")){
801 if(!asccasecmp(cmd
[2], cp
= "Message-ID")){
802 npp
= &hp
->h_message_id
;
805 if(!asccasecmp(cmd
[2], cp
= "References")){
809 if(!asccasecmp(cmd
[2], cp
= "In-Reply-To")){
810 npp
= &hp
->h_in_reply_to
;
814 if(!asccasecmp(cmd
[2], cp
= "Subject")){
815 if(hp
->h_subject
!= NULL
){
816 hp
->h_subject
= NULL
;
817 fprintf(n_stdout
, "210 %s\n", cp
);
823 /* Primitive name normalization XXX header object should
824 * XXX have a more sophisticated accessible one */
826 struct n_header_field
**hfpp
;
830 cp
= xp
= savestr(cmd
[2]);
831 xp
[0] = upperchar(xp
[0]);
833 xp
[0] = lowerchar(xp
[0]);
835 for(any
= FAL0
, hfpp
= &hp
->h_user_headers
; (hfp
= *hfpp
) != NULL
;){
836 if(!asccasecmp(cp
, &hfp
->hf_dat
[0])){
837 *hfpp
= hfp
->hf_next
;
839 fprintf(n_stdout
, "210 %s\n", cp
);
842 hfp
= *(hfpp
= &hfp
->hf_next
);
850 if(is_asccaseprefix(cmd
[1], "insert")){ /* TODO LOGIC BELONGS head.c
851 * TODO That is: Header::factory(string) -> object (blahblah).
852 * TODO I.e., as long as we don't have regular RFC compliant parsers
853 * TODO which differentiate in between structured and unstructured
854 * TODO header fields etc., a little workaround */
856 enum expand_addr_check_mode eacm
;
860 if(cmd
[2] == NULL
|| cmd
[3] == NULL
)
863 /* Strip [\r\n] which would render a body invalid XXX all controls? */
867 cmd
[3] = xp
= savestr(cmd
[3]);
868 for(; (c
= *xp
) != '\0'; ++xp
)
869 if(c
== '\n' || c
== '\r')
874 ntype
= GEXTRA
| GFULL
| GFULLEXTRA
;
877 if(!asccasecmp(cmd
[2], "from")){
881 if((np
= lextract(cmd
[3], ntype
)) == NULL
)
884 if((np
= checkaddrs(np
, eacm
, &aerr
), aerr
!= 0)){
885 fprintf(n_stdout
, "505 %s\n", cp
);
892 for(xnp
= *npp
; xnp
!= NULL
&& !(xnp
->n_type
& GDEL
);
895 if(xnp
!= NULL
|| np
->n_flink
!= NULL
){
896 fprintf(n_stdout
, "506 %s\n", cp
);
901 *npp
= cat(*npp
, np
);
902 fprintf(n_stdout
, "210 %s\n", cp
);
905 if(!asccasecmp(cmd
[2], cp
= "Sender")){
910 if(!asccasecmp(cmd
[2], cp
= "To")){
913 eacm
= EACM_NORMAL
| EAF_NAME
;
916 if(!asccasecmp(cmd
[2], cp
= "Cc")){
919 eacm
= EACM_NORMAL
| EAF_NAME
;
922 if(!asccasecmp(cmd
[2], cp
= "Bcc")){
924 ntype
= GBCC
| GFULL
;
925 eacm
= EACM_NORMAL
| EAF_NAME
;
928 if(!asccasecmp(cmd
[2], cp
= "Reply-To")){
929 npp
= &hp
->h_replyto
;
932 if(!asccasecmp(cmd
[2], cp
= "Mail-Followup-To")){
937 if(!asccasecmp(cmd
[2], cp
= "Message-ID")){
939 npp
= &hp
->h_message_id
;
943 if(!asccasecmp(cmd
[2], cp
= "References")){
949 if(!asccasecmp(cmd
[2], cp
= "In-Reply-To")){
950 npp
= &hp
->h_in_reply_to
;
956 if(!asccasecmp(cmd
[2], cp
= "Subject")){
957 if(cmd
[3][0] != '\0'){
958 if(hp
->h_subject
!= NULL
)
959 hp
->h_subject
= savecatsep(hp
->h_subject
, ' ', cmd
[3]);
961 hp
->h_subject
= n_UNCONST(cmd
[3]);
962 fprintf(n_stdout
, "210 %s\n", cp
);
968 /* Primitive name normalization XXX header object should
969 * XXX have a more sophisticated accessible one */
972 struct n_header_field
**hfpp
;
974 for(cp
= cmd
[2]; *cp
!= '\0'; ++cp
)
975 if(!fieldnamechar(*cp
)){
980 for(hfpp
= &hp
->h_user_headers
; *hfpp
!= NULL
;)
981 hfpp
= &(*hfpp
)->hf_next
;
983 nl
= strlen(cp
= cmd
[2]);
984 bl
= strlen(cmd
[3]) +1;
985 *hfpp
= hfp
= salloc(n_VSTRUCT_SIZEOF(struct n_header_field
, hf_dat
990 memcpy(hfp
->hf_dat
, cp
, nl
);
991 hfp
->hf_dat
[nl
++] = '\0';
992 memcpy(hfp
->hf_dat
+ nl
, cmd
[3], bl
);
993 fprintf(n_stdout
, "210 %s\n", cp
);
1001 if(is_asccaseprefix(cmd
[0], "attachment")){
1003 struct attachment
*ap
;
1005 if(cmd
[1] == NULL
|| is_asccaseprefix(cmd
[1], "list")){
1009 if((ap
= hp
->h_attach
) != NULL
){
1010 fputs("212\n", n_stdout
);
1012 fprintf(n_stdout
, "%s\n", ap
->a_path_user
);
1013 while((ap
= ap
->a_flink
) != NULL
);
1014 putc('\n', n_stdout
);
1016 fputs("501\n", n_stdout
);
1020 if(is_asccaseprefix(cmd
[1], "remove")){
1021 if(cmd
[2] == NULL
|| cmd
[3] != NULL
)
1024 if((ap
= n_attachment_find(hp
->h_attach
, cmd
[2], &status
)) != NULL
){
1026 fputs("506\n", n_stdout
);
1028 hp
->h_attach
= n_attachment_remove(hp
->h_attach
, ap
);
1029 fprintf(n_stdout
, "210 %s\n", cmd
[2]);
1032 fputs("501\n", n_stdout
);
1036 if(is_asccaseprefix(cmd
[1], "remove-at")){
1039 if(cmd
[2] == NULL
|| cmd
[3] != NULL
)
1042 if((n_idec_uiz_cp(&i
, cmd
[2], 0, NULL
1043 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
1044 ) != n_IDEC_STATE_CONSUMED
|| i
== 0)
1045 fputs("505\n", n_stdout
);
1047 for(ap
= hp
->h_attach
; ap
!= NULL
&& --i
!= 0; ap
= ap
->a_flink
)
1050 hp
->h_attach
= n_attachment_remove(hp
->h_attach
, ap
);
1051 fprintf(n_stdout
, "210 %s\n", cmd
[2]);
1053 fputs("501\n", n_stdout
);
1058 if(is_asccaseprefix(cmd
[1], "insert")){
1059 enum n_attach_error aerr
;
1061 if(cmd
[2] == NULL
|| cmd
[3] != NULL
)
1064 hp
->h_attach
= n_attachment_append(hp
->h_attach
, cmd
[2], &aerr
, &ap
);
1066 case n_ATTACH_ERR_FILE_OPEN
: cp
= "505\n"; goto jatt_ins
;
1067 case n_ATTACH_ERR_ICONV_FAILED
: cp
= "506\n"; goto jatt_ins
;
1068 case n_ATTACH_ERR_ICONV_NAVAIL
:
1069 case n_ATTACH_ERR_OTHER
:
1073 fputs(cp
, n_stdout
);
1075 case n_ATTACH_ERR_NONE
:{
1078 for(i
= 0; ap
!= NULL
; ++i
, ap
= ap
->a_blink
)
1080 fprintf(n_stdout
, "210 %" PRIuZ
"\n", i
);
1086 if(is_asccaseprefix(cmd
[1], "attribute")){
1087 if(cmd
[2] == NULL
|| cmd
[3] != NULL
)
1090 if((ap
= n_attachment_find(hp
->h_attach
, cmd
[2], NULL
)) != NULL
){
1092 fprintf(n_stdout
, "212 %s\n", cmd
[2]);
1094 fprintf(n_stdout
, "message-number %d\n\n", ap
->a_msgno
);
1097 "creation-name %s\nopen-path %s\nfilename %s\n",
1098 ap
->a_path_user
, ap
->a_path
, ap
->a_name
);
1099 if(ap
->a_content_description
!= NULL
)
1100 fprintf(n_stdout
, "content-description %s\n",
1101 ap
->a_content_description
);
1102 if(ap
->a_content_id
!= NULL
)
1103 fprintf(n_stdout
, "content-id %s\n",
1104 ap
->a_content_id
->n_name
);
1105 if(ap
->a_content_type
!= NULL
)
1106 fprintf(n_stdout
, "content-type %s\n", ap
->a_content_type
);
1107 if(ap
->a_content_disposition
!= NULL
)
1108 fprintf(n_stdout
, "content-disposition %s\n",
1109 ap
->a_content_disposition
);
1110 putc('\n', n_stdout
);
1113 fputs("501\n", n_stdout
);
1117 if(is_asccaseprefix(cmd
[1], "attribute-at")){
1120 if(cmd
[2] == NULL
|| cmd
[3] != NULL
)
1123 if((n_idec_uiz_cp(&i
, cmd
[2], 0, NULL
1124 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
1125 ) != n_IDEC_STATE_CONSUMED
|| i
== 0)
1126 fputs("505\n", n_stdout
);
1128 for(ap
= hp
->h_attach
; ap
!= NULL
&& --i
!= 0; ap
= ap
->a_flink
)
1133 fputs("501\n", n_stdout
);
1138 if(is_asccaseprefix(cmd
[1], "attribute-set")){
1139 if(cmd
[2] == NULL
|| cmd
[3] == NULL
)
1142 if((ap
= n_attachment_find(hp
->h_attach
, cmd
[2], NULL
)) != NULL
){
1145 fputs("505\n", n_stdout
);
1150 while((c
= *cp
) != '\0' && !blankchar(c
))
1152 keyw
= savestrbuf(cmd
[3], PTR2SIZE(cp
- cmd
[3]));
1154 for(; (c
= *++cp
) != '\0' && blankchar(c
);)
1159 /* Strip [\r\n] which would render a parameter invalid XXX
1160 * XXX all controls? */
1161 cp
= xp
= savestr(cp
);
1162 for(; (c
= *xp
) != '\0'; ++xp
)
1163 if(c
== '\n' || c
== '\r')
1169 if(!asccasecmp(keyw
, "filename"))
1170 ap
->a_name
= (c
== '\0') ? ap
->a_path_bname
: cp
;
1171 else if(!asccasecmp(keyw
, "content-description"))
1172 ap
->a_content_description
= (c
== '\0') ? NULL
: cp
;
1173 else if(!asccasecmp(keyw
, "content-id")){
1174 ap
->a_content_id
= NULL
;
1179 np
= checkaddrs(lextract(cp
, GREF
),
1180 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG
|
1182 if(np
!= NULL
&& np
->n_flink
== NULL
)
1183 ap
->a_content_id
= np
;
1187 }else if(!asccasecmp(keyw
, "content-type"))
1188 ap
->a_content_type
= (c
== '\0') ? NULL
: cp
;
1189 else if(!asccasecmp(keyw
, "content-disposition"))
1190 ap
->a_content_disposition
= (c
== '\0') ? NULL
: cp
;
1197 for(i
= 0; ap
!= NULL
; ++i
, ap
= ap
->a_blink
)
1199 fprintf(n_stdout
, "210 %" PRIuZ
"\n", i
);
1201 fputs("505\n", n_stdout
);
1204 fputs("501\n", n_stdout
);
1208 if(is_asccaseprefix(cmd
[1], "attribute-set-at")){
1211 if(cmd
[2] == NULL
|| cmd
[3] == NULL
)
1214 if((n_idec_uiz_cp(&i
, cmd
[2], 0, NULL
1215 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
1216 ) != n_IDEC_STATE_CONSUMED
|| i
== 0)
1217 fputs("505\n", n_stdout
);
1219 for(ap
= hp
->h_attach
; ap
!= NULL
&& --i
!= 0; ap
= ap
->a_flink
)
1224 fputs("501\n", n_stdout
);
1233 fputs("500\n", n_stdout
);
1238 return (ms
!= NULL
);
1241 fputs("501 ", n_stdout
);
1242 fputs(cp
, n_stdout
);
1243 putc('\n', n_stdout
);
1250 NYD_X
; /* Signal handler */
1252 /* the control flow is subtle, because we can be called from ~q */
1253 if (_coll_hadintr
== 0) {
1254 if (ok_blook(ignore
)) {
1255 fputs("@\n", n_stdout
);
1260 siglongjmp(_coll_jmp
, 1);
1262 n_exit_status
|= n_EXIT_SEND_ERROR
;
1264 savedeadletter(_coll_fp
, TRU1
);
1265 /* Aborting message, no need to fflush() .. */
1266 siglongjmp(_coll_abort
, 1);
1272 NYD_X
; /* Signal handler */
1275 savedeadletter(_coll_fp
, TRU1
);
1276 /* Let's pretend nobody else wants to clean up, a true statement at
1282 putesc(char const *s
, FILE *stream
)
1287 while (s
[0] != '\0') {
1290 if (putc('\t', stream
) == EOF
)
1297 if (putc('\n', stream
) == EOF
)
1304 if (putc(s
[0], stream
) == EOF
)
1309 if (putc('\n', stream
) == EOF
)
1318 a_coll__hook_setter(void *arg
){ /* TODO v15: drop */
1325 if((val
= detract(hp
->h_from
, GNAMEONLY
)) == NULL
)
1327 ok_vset(compose_from
, val
);
1328 if((val
= detract(hp
->h_sender
, 0)) == NULL
)
1330 ok_vset(compose_sender
, val
);
1331 if((val
= detract(hp
->h_to
, GNAMEONLY
)) == NULL
)
1333 ok_vset(compose_to
, val
);
1334 if((val
= detract(hp
->h_cc
, GNAMEONLY
)) == NULL
)
1336 ok_vset(compose_cc
, val
);
1337 if((val
= detract(hp
->h_bcc
, GNAMEONLY
)) == NULL
)
1339 ok_vset(compose_bcc
, val
);
1340 if((val
= hp
->h_subject
) == NULL
)
1342 ok_vset(compose_subject
, val
);
1347 a_coll_ocs__mac(void){
1348 /* Executes in a fork(2)ed child */
1349 setvbuf(n_stdout
, NULL
, _IOLBF
, 0);
1350 n_psonce
&= ~(n_PSO_INTERACTIVE
| n_PSO_TTYIN
| n_PSO_TTYOUT
);
1351 n_pstate
|= n_PS_COMPOSE_FORKHOOK
;
1352 temporary_compose_mode_hook_call(a_coll_ocs__macname
, NULL
, NULL
);
1359 a_coll_ocs__finalize(void *vp
){
1360 /* Note we use this for destruction upon setup errors, thus */
1361 sighandler_type opipe
;
1362 sighandler_type oint
;
1363 struct a_coll_ocs_arg
**coapp
, *coap
;
1366 temporary_compose_mode_hook_call((char*)-1, NULL
, NULL
);
1368 coap
= *(coapp
= vp
);
1369 *coapp
= (struct a_coll_ocs_arg
*)-1;
1371 if(coap
->coa_stdout
!= NULL
)
1372 if(!Pclose(coap
->coa_stdout
, FAL0
)){
1373 *coap
->coa_senderr
= TRU1
;
1374 n_err(_("*on-compose-splice(-shell)?* failed: %s\n"),
1375 n_shexp_quote_cp(coap
->coa_cmd
, FAL0
));
1378 if(coap
->coa_stdin
!= NULL
)
1379 Fclose(coap
->coa_stdin
);
1380 else if(coap
->coa_pipe
[0] != -1)
1381 close(coap
->coa_pipe
[0]);
1383 if(coap
->coa_pipe
[1] != -1)
1384 close(coap
->coa_pipe
[1]);
1386 opipe
= coap
->coa_opipe
;
1387 oint
= coap
->coa_oint
;
1392 safe_signal(SIGPIPE
, opipe
);
1393 safe_signal(SIGINT
, oint
);
1399 collect(struct header
*hp
, int printheaders
, struct message
*mp
,
1400 char const *quotefile
, int doprefix
, si8_t
*checkaddr_err
)
1402 struct n_ignore
const *quoteitp
;
1403 struct a_coll_ocs_arg
*coap
;
1405 int volatile t
, eofcnt
, getfields
;
1406 char volatile escape
;
1407 char *linebuf
, escape_saved
;
1408 char const *cp
, *coapm
;
1409 size_t i
, linesize
; /* TODO line pool */
1411 enum sendaction action
;
1412 sigset_t oset
, nset
;
1413 FILE * volatile sigfp
;
1424 /* Start catching signals from here, but we're still die on interrupts
1425 * until we're in the main loop */
1427 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
1428 /* FIXME have dropped handlerpush() and localized onintr() in go.c! */
1429 if ((_coll_saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
1430 safe_signal(SIGINT
, &_collint
);
1431 if ((_coll_savehup
= safe_signal(SIGHUP
, SIG_IGN
)) != SIG_IGN
)
1432 safe_signal(SIGHUP
, collhup
);
1433 if (sigsetjmp(_coll_abort
, 1))
1435 if (sigsetjmp(_coll_jmp
, 1))
1437 n_pstate
|= n_PS_COMPOSE_MODE
;
1438 sigprocmask(SIG_SETMASK
, &oset
, (sigset_t
*)NULL
);
1440 if ((_coll_fp
= Ftmp(NULL
, "collect", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==
1442 n_perr(_("temporary mail file"), 0);
1446 /* If we are going to prompt for a subject, refrain from printing a newline
1447 * after the headers (since some people mind) */
1449 if (!(n_poption
& n_PO_t_FLAG
)) {
1450 t
= GTO
| GSUBJECT
| GCC
| GNL
;
1451 if (ok_blook(fullnames
))
1454 if (n_psonce
& n_PSO_INTERACTIVE
) {
1455 if (hp
->h_subject
== NULL
&& (ok_blook(ask
) || ok_blook(asksub
)))
1456 t
&= ~GNL
, getfields
|= GSUBJECT
;
1458 if (hp
->h_to
== NULL
)
1459 t
&= ~GNL
, getfields
|= GTO
;
1461 if (!ok_blook(bsdcompat
) && !ok_blook(askatend
)) {
1462 if (hp
->h_bcc
== NULL
&& ok_blook(askbcc
))
1463 t
&= ~GNL
, getfields
|= GBCC
;
1464 if (hp
->h_cc
== NULL
&& ok_blook(askcc
))
1465 t
&= ~GNL
, getfields
|= GCC
;
1472 escape_saved
= escape
= ((cp
= ok_vlook(escape
)) != NULL
) ? *cp
: n_ESCAPE
;
1475 if (!sigsetjmp(_coll_jmp
, 1)) {
1476 /* Ask for some headers first, as necessary */
1478 grab_headers(n_GO_INPUT_CTX_COMPOSE
, hp
, getfields
, 1);
1480 /* Execute compose-enter TODO completely v15-compat intermediate!! */
1481 if((cp
= ok_vlook(on_compose_enter
)) != NULL
){
1482 setup_from_and_sender(hp
);
1483 temporary_compose_mode_hook_call(cp
, &a_coll__hook_setter
, hp
);
1486 /* Cannot do since it may require turning this into a multipart one */
1487 if(!(n_poption
& n_PO_Mm_FLAG
)){
1488 char const *cp_obsolete
= ok_vlook(NAIL_HEAD
);
1489 if(cp_obsolete
!= NULL
)
1490 n_OBSOLETE(_("please use *message-inject-head*, not *NAIL_HEAD*"));
1492 if(((cp
= ok_vlook(message_inject_head
)) != NULL
||
1493 (cp
= cp_obsolete
) != NULL
) && putesc(cp
, _coll_fp
) < 0)
1496 /* Quote an original message */
1497 if (mp
!= NULL
&& (doprefix
|| (cp
= ok_vlook(quote
)) != NULL
)) {
1498 quoteitp
= n_IGNORE_ALL
;
1499 action
= SEND_QUOTE
;
1501 quoteitp
= n_IGNORE_FWD
;
1502 if ((cp
= ok_vlook(fwdheading
)) == NULL
)
1503 cp
= "-------- Original Message --------";
1504 if (*cp
!= '\0' && fprintf(_coll_fp
, "%s\n", cp
) < 0)
1506 } else if (!strcmp(cp
, "noheading")) {
1508 } else if (!strcmp(cp
, "headers")) {
1509 quoteitp
= n_IGNORE_TYPE
;
1510 } else if (!strcmp(cp
, "allheaders")) {
1512 action
= SEND_QUOTE_ALL
;
1514 cp
= hfield1("from", mp
);
1515 if (cp
!= NULL
&& (cnt
= (long)strlen(cp
)) > 0) {
1516 if (xmime_write(cp
, cnt
, _coll_fp
, CONV_FROMHDR
, TD_NONE
) < 0)
1518 if (fprintf(_coll_fp
, _(" wrote:\n\n")) < 0)
1522 if (fflush(_coll_fp
))
1526 else if ((cp
= ok_vlook(indentprefix
)) == NULL
)
1527 cp
= INDENT_DEFAULT
;
1528 if (sendmp(mp
, _coll_fp
, quoteitp
, cp
, action
, NULL
) < 0)
1533 if (quotefile
!= NULL
) {
1534 if(!a_coll_include_file(quotefile
, FAL0
, FAL0
))
1538 if((n_psonce
& n_PSO_INTERACTIVE
) && !(n_poption
& n_PO_Mm_FLAG
)){
1539 /* Print what we have sofar also on the terminal (if useful) */
1540 if (!ok_blook(editalong
)) {
1542 puthead(TRU1
, hp
, n_stdout
, t
,
1543 SEND_TODISP
, CONV_NONE
, NULL
, NULL
);
1546 while ((c
= getc(_coll_fp
)) != EOF
) /* XXX bytewise, yuck! */
1548 if (fseek(_coll_fp
, 0, SEEK_END
))
1553 /* As mandated by the Mail Reference Manual, print "(continue)" */
1555 if(n_psonce
& n_PSO_INTERACTIVE
)
1556 fputs(_("(continue)\n"), n_stdout
);
1561 /* Come here for printing the after-signal message. Duplicate messages
1562 * won't be printed because the write is aborted if we get a SIGTTOU */
1564 n_err(_("\n(Interrupt -- one more to kill letter)\n"));
1567 /* If not under shell hook control */
1569 /* We're done with -M or -m (because we are too simple minded) */
1570 if(n_poption
& n_PO_Mm_FLAG
)
1572 /* No command escapes, interrupts not expected. Simply copy STDIN */
1573 if(!(n_psonce
& n_PSO_INTERACTIVE
) &&
1574 !(n_poption
& (n_PO_t_FLAG
| n_PO_TILDE_FLAG
))){
1575 linebuf
= srealloc(linebuf
, linesize
= LINESIZE
);
1576 while ((i
= fread(linebuf
, sizeof *linebuf
, linesize
, n_stdin
)) > 0) {
1577 if (i
!= fwrite(linebuf
, sizeof *linebuf
, i
, _coll_fp
))
1584 /* The interactive collect loop */
1587 enum n_go_input_flags gif
;
1589 gif
= n_GO_INPUT_CTX_COMPOSE
;
1590 if((n_psonce
& n_PSO_INTERACTIVE
) || (n_poption
& n_PO_TILDE_FLAG
)){
1591 if(!(n_poption
& n_PO_t_FLAG
))
1592 gif
|= n_GO_INPUT_NL_ESC
;
1594 cnt
= n_go_input(gif
, n_empty
, &linebuf
, &linesize
, NULL
);
1600 if(n_poption
& n_PO_t_FLAG
){
1601 fflush_rewind(_coll_fp
);
1602 /* It is important to set n_PSO_t_FLAG before extract_header()
1603 * *and* keep n_PO_t_FLAG for the first parse of the message! */
1604 n_psonce
|= n_PSO_t_FLAG
;
1605 if (makeheader(_coll_fp
, hp
, checkaddr_err
) != OKAY
)
1607 n_poption
&= ~n_PO_t_FLAG
;
1609 }else if((n_psonce
& n_PSO_INTERACTIVE
) && ok_blook(ignoreeof
) &&
1612 _("*ignoreeof* set, use `~.' to terminate letter\n"));
1613 n_go_input_clearerr();
1624 else if(coap
== NULL
){
1625 if(!(n_psonce
& n_PSO_INTERACTIVE
) && !(n_poption
& n_PO_TILDE_FLAG
))
1627 else if(cp
[0] == '.'){
1628 if(cnt
== 1 && (ok_blook(dot
) || ok_blook(ignoreeof
)))
1632 if(cp
[0] != escape
){
1634 if(fwrite(cp
, sizeof *cp
, cnt
, _coll_fp
) != (size_t)cnt
)
1636 /* TODO n_PS_READLINE_NL is a terrible hack to ensure that _in_all_-
1637 * TODO _code_paths_ a file without trailing newline isn't modified
1638 * TODO to continue one; the "saw-newline" needs to be part of an
1639 * TODO I/O input machinery object */
1641 if(n_pstate
& n_PS_READLINE_NL
){
1642 if(putc('\n', _coll_fp
) == EOF
)
1648 /* Cleanup the input string: like this we can perform a little bit of
1649 * usage testing and also have somewhat normalized history entries */
1650 for(cp
= &linebuf
[2]; (c
= *cp
) != '\0' && blankspacechar(c
); ++cp
)
1656 i
= PTR2SIZE(cp
- linebuf
) - 3;
1657 memmove(&linebuf
[3], cp
, (cnt
-= i
));
1659 linebuf
[cnt
] = '\0';
1661 if(cnt
> 0){ /* TODO v15 no more trailing WS from lex_input please */
1666 if(!blankspacechar(c
))
1669 ((char*)n_UNCONST(cp
))[0] = '\0';
1670 cnt
= PTR2SIZE(cp
- linebuf
);
1674 switch((c
= linebuf
[1])){
1676 /* On double escape, send a single one. Otherwise, it's an error */
1682 char buf
[sizeof(n_UNIREPL
)];
1685 buf
[0] = c
, buf
[1] = '\0';
1686 else if(n_psonce
& n_PSO_UNICODE
)
1687 memcpy(buf
, n_unirepl
, sizeof n_unirepl
);
1689 buf
[0] = '?', buf
[1] = '\0';
1690 n_err(_("Unknown command escape: ~%s\n"), buf
);
1694 n_err(_("Invalid command escape usage: %s\n"), linebuf
);
1697 /* Shell escape, send the balance of line to sh -c */
1698 if(cnt
== 2 || coap
!= NULL
)
1700 c_shell(&linebuf
[3]);
1705 /* Escape to command mode, but be nice! *//* TODO command expansion
1706 * TODO should be handled here so that we have unique history! */
1709 _execute_command(hp
, &linebuf
[3], cnt
-= 3);
1712 /* Simulate end of file on input */
1713 if(cnt
!= 2 || coap
!= NULL
)
1717 /* Same as 'q', but no *DEAD* saving */
1720 /* Force a quit, act like an interrupt had happened */
1724 _collint((c
== 'x') ? 0 : SIGINT
);
1728 /* Grab a bunch of headers */
1732 grab_headers(n_GO_INPUT_CTX_COMPOSE
, hp
,
1733 (GTO
| GSUBJECT
| GCC
| GBCC
),
1734 (ok_blook(bsdcompat
) && ok_blook(bsdorder
)));
1735 while(hp
->h_to
== NULL
);
1738 /* Grab extra headers */
1742 grab_headers(n_GO_INPUT_CTX_COMPOSE
, hp
, GEXTRA
, 0);
1743 while(check_from_and_sender(hp
->h_from
, hp
->h_sender
) == NULL
);
1746 /* Add to the To: list */
1749 hp
->h_to
= cat(hp
->h_to
,
1750 checkaddrs(lextract(&linebuf
[3], GTO
| GFULL
), EACM_NORMAL
,
1754 /* Set the Subject list */
1757 /* Subject:; take care for Debian #419840 and strip any \r and \n */
1758 if(n_anyof_cp("\n\r", hp
->h_subject
= savestr(&linebuf
[3]))){
1761 n_err(_("-s: normalizing away invalid ASCII NL / CR bytes\n"));
1762 for(xp
= hp
->h_subject
; *xp
!= '\0'; ++xp
)
1763 if(*xp
== '\n' || *xp
== '\r')
1768 struct attachment
*aplist
;
1770 /* Edit the attachment list */
1771 aplist
= hp
->h_attach
;
1772 hp
->h_attach
= NULL
;
1774 hp
->h_attach
= n_attachment_append_list(aplist
, &linebuf
[3]);
1776 hp
->h_attach
= n_attachment_list_edit(aplist
,
1777 n_GO_INPUT_CTX_COMPOSE
);
1780 /* Add to the CC list */
1783 hp
->h_cc
= cat(hp
->h_cc
,
1784 checkaddrs(lextract(&linebuf
[3], GCC
| GFULL
), EACM_NORMAL
,
1788 /* Add stuff to blind carbon copies list */
1791 hp
->h_bcc
= cat(hp
->h_bcc
,
1792 checkaddrs(lextract(&linebuf
[3], GBCC
| GFULL
), EACM_NORMAL
,
1798 cp
= n_getdeadletter();
1804 /* Invoke a file: Search for the file name, then open it and copy
1805 * the contents to _coll_fp */
1807 n_err(_("Interpolate what file?\n"));
1810 if(*(cp
= &linebuf
[3]) == '!' && c
== '<'){
1811 if(!a_coll_insert_cmd(_coll_fp
, ++cp
)){
1812 if(ferror(_coll_fp
))
1818 /* Note this also expands things like
1819 * !:vput vexpr delim random 0
1821 if((cp
= fexpand(cp
, FEXP_LOCAL
| FEXP_NOPROTO
| FEXP_NSHELL
)) == NULL
)
1824 if(n_is_dir(cp
, FAL0
)){
1825 n_err(_("%s: is a directory\n"), n_shexp_quote_cp(cp
, FAL0
));
1828 if(!a_coll_include_file(cp
, (c
== 'R'), TRU1
)){
1829 if(ferror(_coll_fp
))
1835 /* Insert a variable into the file */
1838 if((cp
= n_var_vlook(&linebuf
[3], TRU1
)) == NULL
|| *cp
== '\0')
1840 if(putesc(cp
, _coll_fp
) < 0) /* TODO v15: user resp upon `set' time */
1842 if((n_psonce
& n_PSO_INTERACTIVE
) && putesc(cp
, n_stdout
) < 0)
1847 /* Insert the contents of a signature variable */
1850 cp
= (c
== 'a') ? ok_vlook(sign
) : ok_vlook(Sign
);
1851 if(cp
!= NULL
&& *cp
!= '\0'){
1852 if(putesc(cp
, _coll_fp
) < 0) /* TODO v15: user upon `set' time */
1854 if((n_psonce
& n_PSO_INTERACTIVE
) && putesc(cp
, n_stdout
) < 0)
1859 /* Write the message on a file */
1862 if((cp
= fexpand(&linebuf
[3], FEXP_LOCAL
| FEXP_NOPROTO
)) == NULL
){
1863 n_err(_("Write what file!?\n"));
1867 if(exwrite(cp
, _coll_fp
, 1) < 0)
1876 /* Interpolate the named messages, if we are in receiving mail mode.
1877 * Does the standard list processing garbage. If ~f is given, we
1878 * don't shift over */
1881 if(forward(&linebuf
[3], _coll_fp
, c
) < 0)
1885 /* Print current state of the message without altering anything */
1888 print_collf(_coll_fp
, hp
);
1891 /* Pipe message through command. Collect output as new message */
1895 mespipe(&linebuf
[3]);
1899 /* Edit the current message. 'e' -> use EDITOR, 'v' -> use VISUAL */
1900 if(cnt
!= 2 || coap
!= NULL
)
1903 mesedit(c
, ok_blook(editheaders
) ? hp
: NULL
);
1906 if(!a_collect_plumbing(&linebuf
[3], hp
))
1908 if(n_psonce
& n_PSO_INTERACTIVE
)
1912 /* Last the lengthy help string. (Very ugly, but take care for
1913 * compiler supported string lengths :() */
1915 "COMMAND ESCAPES (to be placed after a newline) excerpt:\n"
1916 "~. Commit and send message\n"
1917 "~: <command> Execute a mail command\n"
1918 "~< <file> Insert <file> (\"~<! <command>\" inserts output of command)\n"
1919 "~@ [<files>] Edit attachment list\n"
1920 "~A Insert *Sign* variable (`~a': insert *sign*)\n"
1921 "~c <users> Add users to Cc: list (`~b': to Bcc:)\n"
1922 "~d Read in $DEAD (dead.letter)\n"
1923 "~e Edit message via $EDITOR\n"
1926 "~F <msglist> Read in with headers, don't *indentprefix* lines\n"
1927 "~f <msglist> Like ~F, but honour `ignore' / `retain' configuration\n"
1928 "~H Edit From:, Reply-To: and Sender:\n"
1929 "~h Prompt for Subject:, To:, Cc: and \"blind\" Bcc:\n"
1930 "~i <variable> Insert a value and a newline\n"
1931 "~M <msglist> Read in with headers, *indentprefix* (`~m': `retain' etc.)\n"
1932 "~p Show current message compose buffer\n"
1933 "~r <file> Insert <file> (`~R': likewise, but *indentprefix* lines)\n"
1936 "~s <subject> Set Subject:\n"
1937 "~t <users> Add users to To: list\n"
1938 "~u <msglist> Read in message(s) without headers (`~U': indent lines)\n"
1939 "~v Edit message via $VISUAL\n"
1940 "~w <file> Write message onto file\n"
1941 "~x Abort composition, discard message (`~q': save in $DEAD)\n"
1942 "~| <command> Pipe message through shell filter\n"
1949 /* Finally place an entry in history as applicable */
1955 if(n_psonce
& n_PSO_INTERACTIVE
)
1956 n_tty_addhist(linebuf
, TRU1
);
1962 /* Do we have *on-compose-splice-shell*, or *on-compose-splice*?
1963 * TODO Usual f...ed up state of signals and terminal etc. */
1964 if(coap
== NULL
&& (cp
= ok_vlook(on_compose_splice_shell
)) != NULL
) Jocs
:{
1965 union {int (*ptf
)(void); char const *sh
;} u
;
1968 /* Reset *escape* to be available and guaranteed! */
1972 u
.ptf
= &a_coll_ocs__mac
;
1974 a_coll_ocs__macname
= cp
= coapm
;
1976 u
.sh
= ok_vlook(SHELL
);
1981 coap
= n_lofi_alloc(n_VSTRUCT_SIZEOF(struct a_coll_ocs_arg
, coa_cmd
1983 coap
->coa_pipe
[0] = coap
->coa_pipe
[1] = -1;
1984 coap
->coa_stdin
= coap
->coa_stdout
= NULL
;
1985 coap
->coa_senderr
= checkaddr_err
;
1986 memcpy(coap
->coa_cmd
, cp
, i
);
1989 coap
->coa_opipe
= safe_signal(SIGPIPE
, SIG_IGN
);
1990 coap
->coa_oint
= safe_signal(SIGINT
, SIG_IGN
);
1993 if(pipe_cloexec(coap
->coa_pipe
) != -1 &&
1994 (coap
->coa_stdin
= Fdopen(coap
->coa_pipe
[0], "r", FAL0
)) != NULL
&&
1995 (coap
->coa_stdout
= Popen(cmd
, "W", u
.sh
, NULL
, coap
->coa_pipe
[1])
1997 close(coap
->coa_pipe
[1]);
1998 coap
->coa_pipe
[1] = -1;
2000 temporary_compose_mode_hook_call(NULL
, NULL
, NULL
);
2001 n_go_splice_hack(coap
->coa_cmd
, coap
->coa_stdin
, coap
->coa_stdout
,
2002 (n_psonce
& ~(n_PSO_INTERACTIVE
| n_PSO_TTYIN
| n_PSO_TTYOUT
)),
2003 &a_coll_ocs__finalize
, &coap
);
2004 /* Hook version protocol for ~^: update manual upon change! */
2005 fputs(a_COLL_PLUMBING_VERSION
"\n", coap
->coa_stdout
);
2006 #undef a_COLL_PLUMBING_VERSION
2011 a_coll_ocs__finalize(coap
);
2012 n_perr(_("Cannot invoke *on-compose-splice(-shell)?*"), c
);
2015 if(*checkaddr_err
!= 0){
2019 if(coapm
== NULL
&& (coapm
= ok_vlook(on_compose_splice
)) != NULL
)
2021 escape
= escape_saved
;
2023 /* Final chance to edit headers, if not already above */
2024 if (ok_blook(bsdcompat
) || ok_blook(askatend
)) {
2025 if (hp
->h_cc
== NULL
&& ok_blook(askcc
))
2026 grab_headers(n_GO_INPUT_CTX_COMPOSE
, hp
, GCC
, 1);
2027 if (hp
->h_bcc
== NULL
&& ok_blook(askbcc
))
2028 grab_headers(n_GO_INPUT_CTX_COMPOSE
, hp
, GBCC
, 1);
2030 if (hp
->h_attach
== NULL
&& ok_blook(askattach
))
2031 hp
->h_attach
= n_attachment_list_edit(hp
->h_attach
,
2032 n_GO_INPUT_CTX_COMPOSE
);
2034 /* Execute compose-leave */
2035 if((cp
= ok_vlook(on_compose_leave
)) != NULL
){
2036 setup_from_and_sender(hp
);
2037 temporary_compose_mode_hook_call(cp
, &a_coll__hook_setter
, hp
);
2040 /* Add automatic receivers */
2041 if ((cp
= ok_vlook(autocc
)) != NULL
&& *cp
!= '\0')
2042 hp
->h_cc
= cat(hp
->h_cc
, checkaddrs(lextract(cp
, GCC
| GFULL
),
2043 EACM_NORMAL
, checkaddr_err
));
2044 if ((cp
= ok_vlook(autobcc
)) != NULL
&& *cp
!= '\0')
2045 hp
->h_bcc
= cat(hp
->h_bcc
, checkaddrs(lextract(cp
, GBCC
| GFULL
),
2046 EACM_NORMAL
, checkaddr_err
));
2047 if (*checkaddr_err
!= 0)
2050 /* TODO Cannot do since it may require turning this into a multipart one */
2051 if(n_poption
& n_PO_Mm_FLAG
)
2054 /* Place signature? */
2055 if((cp
= ok_vlook(signature
)) != NULL
&& *cp
!= '\0'){
2058 if((cpq
= fexpand(cp
, FEXP_LOCAL
| FEXP_NOPROTO
)) == NULL
){
2059 n_err(_("*signature* expands to invalid file: %s\n"),
2060 n_shexp_quote_cp(cp
, FAL0
));
2063 cpq
= n_shexp_quote_cp(cp
= cpq
, FAL0
);
2065 if((sigfp
= Fopen(cp
, "r")) == NULL
){
2066 n_err(_("Can't open *signature* %s: %s\n"),
2067 cpq
, n_err_to_doc(n_err_no
));
2072 linebuf
= smalloc(linesize
= LINESIZE
);
2074 while((i
= fread(linebuf
, sizeof *linebuf
, linesize
, n_UNVOLATILE(sigfp
)))
2077 if(i
!= fwrite(linebuf
, sizeof *linebuf
, i
, _coll_fp
))
2082 FILE *x
= n_UNVOLATILE(sigfp
);
2083 int e
= n_err_no
, ise
= ferror(x
);
2089 n_err(_("Errors while reading *signature* %s: %s\n"),
2090 cpq
, n_err_to_doc(e
));
2095 if(c
!= '\0' && c
!= '\n')
2096 putc('\n', _coll_fp
);
2099 { char const *cp_obsolete
= ok_vlook(NAIL_TAIL
);
2101 if(cp_obsolete
!= NULL
)
2102 n_OBSOLETE(_("please use *message-inject-tail*, not *NAIL_TAIL*"));
2104 if((cp
= ok_vlook(message_inject_tail
)) != NULL
||
2105 (cp
= cp_obsolete
) != NULL
){
2106 if(putesc(cp
, _coll_fp
) < 0)
2108 if((n_psonce
& n_PSO_INTERACTIVE
) && putesc(cp
, n_stdout
) < 0)
2114 if(fflush(_coll_fp
))
2119 if (linebuf
!= NULL
)
2122 sigprocmask(SIG_BLOCK
, &nset
, NULL
);
2123 n_pstate
&= ~n_PS_COMPOSE_MODE
;
2124 safe_signal(SIGINT
, _coll_saveint
);
2125 safe_signal(SIGHUP
, _coll_savehup
);
2126 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
2131 if(coap
!= NULL
&& coap
!= (struct a_coll_ocs_arg
*)-1)
2132 n_go_splice_hack_remove_after_jump();
2134 Fclose(n_UNVOLATILE(sigfp
));
2135 if (_coll_fp
!= NULL
) {
2139 assert(checkaddr_err
!= NULL
);
2140 /* TODO We don't save in $DEAD upon error because msg not readily composed?
2141 * TODO But this no good, it should go ZOMBIE / DRAFT / POSTPONED or what! */
2142 if(*checkaddr_err
!= 0)
2143 n_err(_("Some addressees were classified as \"hard error\"\n"));
2144 else if(_coll_hadintr
== 0){
2145 *checkaddr_err
= TRU1
; /* TODO ugly: "sendout_error" now.. */
2146 n_err(_("Failed to prepare composed message (I/O error, disk full?)\n"));