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 const *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 const *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
);
447 mespipe(char const *cmd
)
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 const *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
);
1357 a_coll_ocs__finalize(void *vp
){
1358 /* Note we use this for destruction upon setup errors, thus */
1359 sighandler_type opipe
;
1360 sighandler_type oint
;
1361 struct a_coll_ocs_arg
**coapp
, *coap
;
1364 temporary_compose_mode_hook_call((char*)-1, NULL
, NULL
);
1366 coap
= *(coapp
= vp
);
1367 *coapp
= (struct a_coll_ocs_arg
*)-1;
1369 if(coap
->coa_stdout
!= NULL
)
1370 if(!Pclose(coap
->coa_stdout
, FAL0
)){
1371 *coap
->coa_senderr
= TRU1
;
1372 n_err(_("*on-compose-splice(-shell)?* failed: %s\n"),
1373 n_shexp_quote_cp(coap
->coa_cmd
, FAL0
));
1376 if(coap
->coa_stdin
!= NULL
)
1377 Fclose(coap
->coa_stdin
);
1378 else if(coap
->coa_pipe
[0] != -1)
1379 close(coap
->coa_pipe
[0]);
1381 if(coap
->coa_pipe
[1] != -1)
1382 close(coap
->coa_pipe
[1]);
1384 opipe
= coap
->coa_opipe
;
1385 oint
= coap
->coa_oint
;
1390 safe_signal(SIGPIPE
, opipe
);
1391 safe_signal(SIGINT
, oint
);
1397 collect(struct header
*hp
, int printheaders
, struct message
*mp
,
1398 char const *quotefile
, int doprefix
, si8_t
*checkaddr_err
)
1400 struct n_string s
, * volatile sp
;
1401 struct n_ignore
const *quoteitp
;
1402 struct a_coll_ocs_arg
*coap
;
1404 int volatile t
, eofcnt
, getfields
;
1405 char volatile escape
;
1406 char *linebuf
, escape_saved
;
1407 char const *cp
, *cp_base
, *coapm
, *ifs_saved
;
1408 size_t i
, linesize
; /* TODO line pool */
1410 enum sendaction action
;
1411 sigset_t oset
, nset
;
1412 FILE * volatile sigfp
;
1420 ifs_saved
= coapm
= NULL
;
1423 /* Start catching signals from here, but we're still die on interrupts
1424 * until we're in the main loop */
1426 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
1427 /* FIXME have dropped handlerpush() and localized onintr() in go.c! */
1428 if ((_coll_saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
1429 safe_signal(SIGINT
, &_collint
);
1430 if ((_coll_savehup
= safe_signal(SIGHUP
, SIG_IGN
)) != SIG_IGN
)
1431 safe_signal(SIGHUP
, collhup
);
1432 if (sigsetjmp(_coll_abort
, 1))
1434 if (sigsetjmp(_coll_jmp
, 1))
1436 n_pstate
|= n_PS_COMPOSE_MODE
;
1437 sigprocmask(SIG_SETMASK
, &oset
, (sigset_t
*)NULL
);
1439 if ((_coll_fp
= Ftmp(NULL
, "collect", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==
1441 n_perr(_("temporary mail file"), 0);
1445 /* If we are going to prompt for a subject, refrain from printing a newline
1446 * after the headers (since some people mind) */
1448 if (!(n_poption
& n_PO_t_FLAG
)) {
1449 t
= GTO
| GSUBJECT
| GCC
| GNL
;
1450 if (ok_blook(fullnames
))
1453 if (n_psonce
& n_PSO_INTERACTIVE
) {
1454 if (hp
->h_subject
== NULL
&& (ok_blook(ask
) || ok_blook(asksub
)))
1455 t
&= ~GNL
, getfields
|= GSUBJECT
;
1457 if (hp
->h_to
== NULL
)
1458 t
&= ~GNL
, getfields
|= GTO
;
1460 if (!ok_blook(bsdcompat
) && !ok_blook(askatend
)) {
1461 if (hp
->h_bcc
== NULL
&& ok_blook(askbcc
))
1462 t
&= ~GNL
, getfields
|= GBCC
;
1463 if (hp
->h_cc
== NULL
&& ok_blook(askcc
))
1464 t
&= ~GNL
, getfields
|= GCC
;
1471 escape_saved
= escape
= *(cp
= ok_vlook(escape
));
1474 if (!sigsetjmp(_coll_jmp
, 1)) {
1475 /* Ask for some headers first, as necessary */
1477 grab_headers(n_GO_INPUT_CTX_COMPOSE
, hp
, getfields
, 1);
1479 /* Execute compose-enter TODO completely v15-compat intermediate!! */
1480 if((cp
= ok_vlook(on_compose_enter
)) != NULL
){
1481 setup_from_and_sender(hp
);
1482 temporary_compose_mode_hook_call(cp
, &a_coll__hook_setter
, hp
);
1485 /* TODO Mm: nope since it may require turning this into a multipart one */
1486 if(!(n_poption
& n_PO_Mm_FLAG
)){
1487 char const *cp_obsolete
= ok_vlook(NAIL_HEAD
);
1488 if(cp_obsolete
!= NULL
)
1489 n_OBSOLETE(_("please use *message-inject-head*, not *NAIL_HEAD*"));
1491 if(((cp
= ok_vlook(message_inject_head
)) != NULL
||
1492 (cp
= cp_obsolete
) != NULL
) && putesc(cp
, _coll_fp
) < 0)
1495 /* Quote an original message */
1496 if (mp
!= NULL
&& (doprefix
|| (cp
= ok_vlook(quote
)) != NULL
)) {
1497 quoteitp
= n_IGNORE_ALL
;
1498 action
= SEND_QUOTE
;
1500 quoteitp
= n_IGNORE_FWD
;
1501 if ((cp
= ok_vlook(fwdheading
)) == NULL
)
1502 cp
= "-------- Original Message --------";
1503 if (*cp
!= '\0' && fprintf(_coll_fp
, "%s\n", cp
) < 0)
1505 } else if (!strcmp(cp
, "noheading")) {
1507 } else if (!strcmp(cp
, "headers")) {
1508 quoteitp
= n_IGNORE_TYPE
;
1509 } else if (!strcmp(cp
, "allheaders")) {
1511 action
= SEND_QUOTE_ALL
;
1513 cp
= hfield1("from", mp
);
1514 if (cp
!= NULL
&& (cnt
= (long)strlen(cp
)) > 0) {
1515 if (xmime_write(cp
, cnt
, _coll_fp
, CONV_FROMHDR
, TD_NONE
) < 0)
1517 if (fprintf(_coll_fp
, _(" wrote:\n\n")) < 0)
1521 if (fflush(_coll_fp
))
1525 else if ((cp
= ok_vlook(indentprefix
)) == NULL
)
1526 cp
= INDENT_DEFAULT
;
1527 if (sendmp(mp
, _coll_fp
, quoteitp
, cp
, action
, NULL
) < 0)
1532 if (quotefile
!= NULL
) {
1533 if(!a_coll_include_file(quotefile
, FAL0
, FAL0
))
1538 if(n_psonce
& n_PSO_INTERACTIVE
){
1539 if(!(n_pstate
& n_PS_SOURCING
)){
1540 sp
= n_string_creat_auto(&s
);
1541 sp
= n_string_reserve(sp
, 80);
1544 if(!(n_poption
& n_PO_Mm_FLAG
)){
1545 /* Print what we have sofar also on the terminal (if useful) */
1546 if (!ok_blook(editalong
)) {
1548 puthead(TRU1
, hp
, n_stdout
, t
,
1549 SEND_TODISP
, CONV_NONE
, NULL
, NULL
);
1552 while ((c
= getc(_coll_fp
)) != EOF
) /* XXX bytewise, yuck! */
1554 if (fseek(_coll_fp
, 0, SEEK_END
))
1559 /* Print msg mandated by the Mail Reference Manual */
1561 if(n_psonce
& n_PSO_INTERACTIVE
)
1562 fputs(_("(continue)\n"), n_stdout
);
1568 /* Come here for printing the after-signal message. Duplicate messages
1569 * won't be printed because the write is aborted if we get a SIGTTOU */
1571 n_err(_("\n(Interrupt -- one more to kill letter)\n"));
1574 /* If not under shell hook control */
1576 /* We're done with -M or -m TODO because: we are too stupid yet, above */
1577 if(n_poption
& n_PO_Mm_FLAG
)
1579 /* No command escapes, interrupts not expected. Simply copy STDIN */
1580 if(!(n_psonce
& n_PSO_INTERACTIVE
) &&
1581 !(n_poption
& (n_PO_t_FLAG
| n_PO_TILDE_FLAG
))){
1582 linebuf
= srealloc(linebuf
, linesize
= LINESIZE
);
1583 while ((i
= fread(linebuf
, sizeof *linebuf
, linesize
, n_stdin
)) > 0) {
1584 if (i
!= fwrite(linebuf
, sizeof *linebuf
, i
, _coll_fp
))
1591 /* The interactive collect loop */
1593 enum {a_HIST_NONE
, a_HIST_ADD
= 1u<<0, a_HIST_GABBY
= 1u<<1} hist
;
1596 enum n_go_input_flags gif
;
1599 gif
= n_GO_INPUT_CTX_COMPOSE
;
1600 histadd
= (sp
!= NULL
);
1601 if((n_psonce
& n_PSO_INTERACTIVE
) || (n_poption
& n_PO_TILDE_FLAG
)){
1602 if(!(n_poption
& n_PO_t_FLAG
))
1603 gif
|= n_GO_INPUT_NL_ESC
;
1605 cnt
= n_go_input(gif
, n_empty
, &linebuf
, &linesize
, NULL
, &histadd
);
1607 hist
= histadd
? a_HIST_ADD
| a_HIST_GABBY
: a_HIST_NONE
;
1613 if(n_poption
& n_PO_t_FLAG
){
1614 fflush_rewind(_coll_fp
);
1615 /* It is important to set n_PSO_t_FLAG before extract_header()
1616 * *and* keep n_PO_t_FLAG for the first parse of the message! */
1617 n_psonce
|= n_PSO_t_FLAG
;
1618 if(makeheader(_coll_fp
, hp
, checkaddr_err
) != OKAY
)
1620 n_poption
&= ~n_PO_t_FLAG
;
1622 }else if((n_psonce
& n_PSO_INTERACTIVE
) && ok_blook(ignoreeof
) &&
1625 _("*ignoreeof* set, use `~.' to terminate letter\n"));
1626 n_go_input_clearerr();
1637 else if(coap
== NULL
){
1638 if(!(n_psonce
& n_PSO_INTERACTIVE
) && !(n_poption
& n_PO_TILDE_FLAG
))
1640 else if(cp
[0] == '.'){
1641 if(cnt
== 1 && (ok_blook(dot
) ||
1642 (ok_blook(posix
) && ok_blook(ignoreeof
))))
1646 if(cp
[0] != escape
){
1648 if(fwrite(cp
, sizeof *cp
, cnt
, _coll_fp
) != (size_t)cnt
)
1650 /* TODO n_PS_READLINE_NL is a terrible hack to ensure that _in_all_-
1651 * TODO _code_paths_ a file without trailing newline isn't modified
1652 * TODO to contain one; the "saw-newline" needs to be part of an
1653 * TODO I/O input machinery object */
1655 if(n_pstate
& n_PS_READLINE_NL
){
1656 if(putc('\n', _coll_fp
) == EOF
)
1662 c
= *(cp_base
= ++cp
);
1664 /* Avoid history entry? */
1667 c
= *(cp_base
= ++cp
);
1670 /* It may just be an escaped escaped character, do that quick */
1671 else if(c
== escape
)
1674 /* Trim input, also to gain a somewhat normalized history entry */
1679 x
.s
= n_UNCONST(cp
);
1681 n_str_trim_ifs(&x
, TRU1
);
1684 cnt
= (int)/*XXX*/x
.l
;
1687 if(hist
!= a_HIST_NONE
){
1688 sp
= n_string_assign_c(sp
, escape
);
1689 sp
= n_string_push_c(sp
, c
);
1691 sp
= n_string_push_c(sp
, ' ');
1692 sp
= n_string_push_buf(sp
, cp
, cnt
);
1699 char buf
[sizeof(n_UNIREPL
)];
1702 buf
[0] = c
, buf
[1] = '\0';
1703 else if(n_psonce
& n_PSO_UNICODE
)
1704 memcpy(buf
, n_unirepl
, sizeof n_unirepl
);
1706 buf
[0] = '?', buf
[1] = '\0';
1707 n_err(_("Unknown command escape: `%c%s'\n"), escape
, buf
);
1711 n_err(_("Invalid command escape usage: %s\n"),
1712 n_shexp_quote_cp(linebuf
, FAL0
));
1715 /* Shell escape, send the balance of line to sh -c */
1716 if(cnt
== 0 || coap
!= NULL
)
1719 char const *argv
[2];
1729 /* Escape to command mode, but be nice! *//* TODO command expansion
1730 * TODO should be handled here so that we have unique history! */
1733 _execute_command(hp
, cp
, cnt
);
1736 /* Simulate end of file on input */
1737 if(cnt
!= 0 || coap
!= NULL
)
1741 /* Same as 'q', but no *DEAD* saving */
1744 /* Force a quit, act like an interrupt had happened */
1748 _collint((c
== 'x') ? 0 : SIGINT
);
1752 /* Grab a bunch of headers */
1756 grab_headers(n_GO_INPUT_CTX_COMPOSE
, hp
,
1757 (GTO
| GSUBJECT
| GCC
| GBCC
),
1758 (ok_blook(bsdcompat
) && ok_blook(bsdorder
)));
1759 }while(hp
->h_to
== NULL
);
1762 /* Grab extra headers */
1766 grab_headers(n_GO_INPUT_CTX_COMPOSE
, hp
, GEXTRA
, 0);
1767 }while(check_from_and_sender(hp
->h_from
, hp
->h_sender
) == NULL
);
1770 /* Add to the To: list */
1773 hp
->h_to
= cat(hp
->h_to
,
1774 checkaddrs(lextract(cp
, GTO
| GFULL
), EACM_NORMAL
,
1776 hist
&= ~a_HIST_GABBY
;
1779 /* Set the Subject list */
1782 /* Subject:; take care for Debian #419840 and strip any \r and \n */
1783 if(n_anyof_cp("\n\r", hp
->h_subject
= savestr(cp
))){
1786 n_err(_("-s: normalizing away invalid ASCII NL / CR bytes\n"));
1787 for(xp
= hp
->h_subject
; *xp
!= '\0'; ++xp
)
1788 if(*xp
== '\n' || *xp
== '\r')
1793 struct attachment
*aplist
;
1795 /* Edit the attachment list */
1796 aplist
= hp
->h_attach
;
1797 hp
->h_attach
= NULL
;
1799 hp
->h_attach
= n_attachment_append_list(aplist
, cp
);
1801 hp
->h_attach
= n_attachment_list_edit(aplist
,
1802 n_GO_INPUT_CTX_COMPOSE
);
1805 /* Add to the CC list */
1808 hp
->h_cc
= cat(hp
->h_cc
,
1809 checkaddrs(lextract(cp
, GCC
| GFULL
), EACM_NORMAL
,
1811 hist
&= ~a_HIST_GABBY
;
1814 /* Add stuff to blind carbon copies list */
1817 hp
->h_bcc
= cat(hp
->h_bcc
,
1818 checkaddrs(lextract(cp
, GBCC
| GFULL
), EACM_NORMAL
, NULL
));
1819 hist
&= ~a_HIST_GABBY
;
1824 cp
= n_getdeadletter();
1830 /* Invoke a file: Search for the file name, then open it and copy
1831 * the contents to _coll_fp */
1833 n_err(_("Interpolate what file?\n"));
1836 if(*cp
== '!' && c
== '<'){
1837 if(!a_coll_insert_cmd(_coll_fp
, ++cp
)){
1838 if(ferror(_coll_fp
))
1844 /* Note this also expands things like
1845 * !:vput vexpr delim random 0
1847 if((cp
= fexpand(cp
, FEXP_LOCAL
| FEXP_NOPROTO
| FEXP_NSHELL
)
1851 if(n_is_dir(cp
, FAL0
)){
1852 n_err(_("%s: is a directory\n"), n_shexp_quote_cp(cp
, FAL0
));
1855 if(!a_coll_include_file(cp
, (c
== 'R'), TRU1
)){
1856 if(ferror(_coll_fp
))
1862 /* Insert a variable into the file */
1865 if((cp
= n_var_vlook(cp
, TRU1
)) == NULL
|| *cp
== '\0')
1867 if(putesc(cp
, _coll_fp
) < 0) /* TODO v15: user resp upon `set' time */
1869 if((n_psonce
& n_PSO_INTERACTIVE
) && putesc(cp
, n_stdout
) < 0)
1874 /* Insert the contents of a signature variable */
1877 cp
= (c
== 'a') ? ok_vlook(sign
) : ok_vlook(Sign
);
1878 if(cp
!= NULL
&& *cp
!= '\0'){
1879 if(putesc(cp
, _coll_fp
) < 0) /* TODO v15: user upon `set' time */
1881 if((n_psonce
& n_PSO_INTERACTIVE
) && putesc(cp
, n_stdout
) < 0)
1886 /* Write the message on a file */
1889 if((cp
= fexpand(cp
, FEXP_LOCAL
| FEXP_NOPROTO
)) == NULL
){
1890 n_err(_("Write what file!?\n"));
1894 if(exwrite(cp
, _coll_fp
, 1) < 0)
1903 /* Interpolate the named messages, if we are in receiving mail mode.
1904 * Does the standard list processing garbage. If ~f is given, we
1905 * don't shift over */
1908 if(forward(cp
, _coll_fp
, c
) < 0)
1912 /* Print current state of the message without altering anything */
1915 print_collf(_coll_fp
, hp
);
1918 /* Pipe message through command. Collect output as new message */
1923 hist
&= ~a_HIST_GABBY
;
1927 /* Edit the current message. 'e' -> use EDITOR, 'v' -> use VISUAL */
1928 if(cnt
!= 0 || coap
!= NULL
)
1931 mesedit(c
, ok_blook(editheaders
) ? hp
: NULL
);
1934 if(!a_collect_plumbing(cp
, hp
))
1936 hist
&= ~a_HIST_GABBY
;
1939 /* Last the lengthy help string. (Very ugly, but take care for
1940 * compiler supported string lengths :() */
1942 "COMMAND ESCAPES (to be placed after a newline) excerpt:\n"
1943 "~. Commit and send message\n"
1944 "~: <command> Execute a mail command\n"
1945 "~< <file> Insert <file> (\"~<! <command>\" inserts output of command)\n"
1946 "~@ [<files>] Edit attachment list\n"
1947 "~A Insert *Sign* variable (`~a': insert *sign*)\n"
1948 "~c <users> Add users to Cc: list (`~b': to Bcc:)\n"
1949 "~d Read in $DEAD (dead.letter)\n"
1950 "~e Edit message via $EDITOR\n"
1953 "~F <msglist> Read in with headers, do not *indentprefix* lines\n"
1954 "~f <msglist> Like ~F, but honour `ignore' / `retain' configuration\n"
1955 "~H Edit From:, Reply-To: and Sender:\n"
1956 "~h Prompt for Subject:, To:, Cc: and \"blind\" Bcc:\n"
1957 "~i <variable> Insert a value and a newline\n"
1958 "~M <msglist> Read in with headers, *indentprefix* (`~m': `retain' etc.)\n"
1959 "~p Show current message compose buffer\n"
1960 "~r <file> Insert <file> (`~R': likewise, but *indentprefix* lines)\n"
1963 "~s <subject> Set Subject:\n"
1964 "~t <users> Add users to To: list\n"
1965 "~u <msglist> Read in message(s) without headers (`~U': indent lines)\n"
1966 "~v Edit message via $VISUAL\n"
1967 "~w <file> Write message onto file\n"
1968 "~x Abort composition, discard message (`~q': save in $DEAD)\n"
1969 "~| <command> Pipe message through shell filter\n"
1976 /* Finally place an entry in history as applicable */
1982 if(hist
& a_HIST_ADD
)
1983 n_tty_addhist(n_string_cp(sp
), ((hist
& a_HIST_GABBY
) != 0));
1989 /* Do we have *on-compose-splice-shell*, or *on-compose-splice*?
1990 * TODO Usual f...ed up state of signals and terminal etc. */
1991 if(coap
== NULL
&& (cp
= ok_vlook(on_compose_splice_shell
)) != NULL
) Jocs
:{
1992 union {int (*ptf
)(void); char const *sh
;} u
;
1995 /* Reset *escape* and more to their defaults. On change update manual! */
1996 if(ifs_saved
== NULL
){
1997 escape
= n_ESCAPE
[0];
1998 ifs_saved
= savestr(ok_vlook(ifs
));
2003 /* XXX Due Popen() fflush(NULL) in PTF mode, ensure nothing to flush */
2004 /*if(!n_real_seek(_coll_fp, 0, SEEK_END))
2006 u
.ptf
= &a_coll_ocs__mac
;
2008 a_coll_ocs__macname
= cp
= coapm
;
2010 u
.sh
= ok_vlook(SHELL
);
2015 coap
= n_lofi_alloc(n_VSTRUCT_SIZEOF(struct a_coll_ocs_arg
, coa_cmd
2017 coap
->coa_pipe
[0] = coap
->coa_pipe
[1] = -1;
2018 coap
->coa_stdin
= coap
->coa_stdout
= NULL
;
2019 coap
->coa_senderr
= checkaddr_err
;
2020 memcpy(coap
->coa_cmd
, cp
, i
);
2023 coap
->coa_opipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2024 coap
->coa_oint
= safe_signal(SIGINT
, SIG_IGN
);
2027 if(pipe_cloexec(coap
->coa_pipe
) != -1 &&
2028 (coap
->coa_stdin
= Fdopen(coap
->coa_pipe
[0], "r", FAL0
)) != NULL
&&
2029 (coap
->coa_stdout
= Popen(cmd
, "W", u
.sh
, NULL
, coap
->coa_pipe
[1])
2031 close(coap
->coa_pipe
[1]);
2032 coap
->coa_pipe
[1] = -1;
2034 temporary_compose_mode_hook_call(NULL
, NULL
, NULL
);
2035 n_go_splice_hack(coap
->coa_cmd
, coap
->coa_stdin
, coap
->coa_stdout
,
2036 (n_psonce
& ~(n_PSO_INTERACTIVE
| n_PSO_TTYIN
| n_PSO_TTYOUT
)),
2037 &a_coll_ocs__finalize
, &coap
);
2038 /* Hook version protocol for ~^: update manual upon change! */
2039 fputs(a_COLL_PLUMBING_VERSION
"\n", coap
->coa_stdout
);
2040 #undef a_COLL_PLUMBING_VERSION
2045 a_coll_ocs__finalize(coap
);
2046 n_perr(_("Cannot invoke *on-compose-splice(-shell)?*"), c
);
2049 if(*checkaddr_err
!= 0){
2053 if(coapm
== NULL
&& (coapm
= ok_vlook(on_compose_splice
)) != NULL
)
2056 ok_vset(ifs
, ifs_saved
);
2058 escape
= escape_saved
;
2061 /* Final chance to edit headers, if not already above */
2062 if (ok_blook(bsdcompat
) || ok_blook(askatend
)) {
2063 if (hp
->h_cc
== NULL
&& ok_blook(askcc
))
2064 grab_headers(n_GO_INPUT_CTX_COMPOSE
, hp
, GCC
, 1);
2065 if (hp
->h_bcc
== NULL
&& ok_blook(askbcc
))
2066 grab_headers(n_GO_INPUT_CTX_COMPOSE
, hp
, GBCC
, 1);
2068 if (hp
->h_attach
== NULL
&& ok_blook(askattach
))
2069 hp
->h_attach
= n_attachment_list_edit(hp
->h_attach
,
2070 n_GO_INPUT_CTX_COMPOSE
);
2072 /* Execute compose-leave */
2073 if((cp
= ok_vlook(on_compose_leave
)) != NULL
){
2074 setup_from_and_sender(hp
);
2075 temporary_compose_mode_hook_call(cp
, &a_coll__hook_setter
, hp
);
2078 /* Add automatic receivers */
2079 if ((cp
= ok_vlook(autocc
)) != NULL
&& *cp
!= '\0')
2080 hp
->h_cc
= cat(hp
->h_cc
, checkaddrs(lextract(cp
, GCC
| GFULL
),
2081 EACM_NORMAL
, checkaddr_err
));
2082 if ((cp
= ok_vlook(autobcc
)) != NULL
&& *cp
!= '\0')
2083 hp
->h_bcc
= cat(hp
->h_bcc
, checkaddrs(lextract(cp
, GBCC
| GFULL
),
2084 EACM_NORMAL
, checkaddr_err
));
2085 if (*checkaddr_err
!= 0)
2088 /* TODO Cannot do since it may require turning this into a multipart one */
2089 if(n_poption
& n_PO_Mm_FLAG
)
2092 /* Place signature? */
2093 if((cp
= ok_vlook(signature
)) != NULL
&& *cp
!= '\0'){
2096 if((cpq
= fexpand(cp
, FEXP_LOCAL
| FEXP_NOPROTO
)) == NULL
){
2097 n_err(_("*signature* expands to invalid file: %s\n"),
2098 n_shexp_quote_cp(cp
, FAL0
));
2101 cpq
= n_shexp_quote_cp(cp
= cpq
, FAL0
);
2103 if((sigfp
= Fopen(cp
, "r")) == NULL
){
2104 n_err(_("Can't open *signature* %s: %s\n"),
2105 cpq
, n_err_to_doc(n_err_no
));
2110 linebuf
= smalloc(linesize
= LINESIZE
);
2112 while((i
= fread(linebuf
, sizeof *linebuf
, linesize
, n_UNVOLATILE(sigfp
)))
2115 if(i
!= fwrite(linebuf
, sizeof *linebuf
, i
, _coll_fp
))
2120 FILE *x
= n_UNVOLATILE(sigfp
);
2121 int e
= n_err_no
, ise
= ferror(x
);
2127 n_err(_("Errors while reading *signature* %s: %s\n"),
2128 cpq
, n_err_to_doc(e
));
2133 if(c
!= '\0' && c
!= '\n')
2134 putc('\n', _coll_fp
);
2137 { char const *cp_obsolete
= ok_vlook(NAIL_TAIL
);
2139 if(cp_obsolete
!= NULL
)
2140 n_OBSOLETE(_("please use *message-inject-tail*, not *NAIL_TAIL*"));
2142 if((cp
= ok_vlook(message_inject_tail
)) != NULL
||
2143 (cp
= cp_obsolete
) != NULL
){
2144 if(putesc(cp
, _coll_fp
) < 0)
2146 if((n_psonce
& n_PSO_INTERACTIVE
) && putesc(cp
, n_stdout
) < 0)
2152 if(fflush(_coll_fp
))
2157 if (linebuf
!= NULL
)
2160 sigprocmask(SIG_BLOCK
, &nset
, NULL
);
2161 n_pstate
&= ~n_PS_COMPOSE_MODE
;
2162 safe_signal(SIGINT
, _coll_saveint
);
2163 safe_signal(SIGHUP
, _coll_savehup
);
2164 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
2169 if(coap
!= NULL
&& coap
!= (struct a_coll_ocs_arg
*)-1)
2170 n_go_splice_hack_remove_after_jump();
2171 if(ifs_saved
!= NULL
)
2172 ok_vset(ifs
, ifs_saved
);
2174 Fclose(n_UNVOLATILE(sigfp
));
2175 if (_coll_fp
!= NULL
) {
2179 assert(checkaddr_err
!= NULL
);
2180 /* TODO We don't save in $DEAD upon error because msg not readily composed?
2181 * TODO But this no good, it should go ZOMBIE / DRAFT / POSTPONED or what! */
2182 if(*checkaddr_err
!= 0)
2183 n_err(_("Some addressees were classified as \"hard error\"\n"));
2184 else if(_coll_hadintr
== 0){
2185 *checkaddr_err
= TRU1
; /* TODO ugly: "sendout_error" now.. */
2186 n_err(_("Failed to prepare composed message (I/O error, disk full?)\n"));