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_ocds_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_ocds__macname
; /* *on-compose-done* */
64 /* Handle `~:', `~_' and some hooks; hp may be NULL */
65 static void _execute_command(struct header
*hp
, char const *linebuf
,
69 static int _include_file(char const *name
, int *linecount
,
70 int *charcount
, bool_t indent
);
72 /* Execute cmd and insert its standard output into fp */
73 static void insertcommand(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-done* driver and *on-compose-done(-shell)?* finalizer */
114 static int a_coll_ocds__mac(void);
115 static void a_coll_ocds__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_source_command(n_LEXINPUT_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 _include_file(char const *name
, int *linecount
, int *charcount
, bool_t indent
)
172 char *linebuf
= NULL
; /* TODO line pool */
173 size_t linesize
= 0, indl
, linelen
, cnt
;
176 /* The -M case is special */
177 if (name
== (char*)-1)
179 else if ((fbuf
= Fopen(name
, "r")) == NULL
) {
185 indb
= NULL
, indl
= 0;
187 if ((indb
= ok_vlook(indentprefix
)) == NULL
)
188 indb
= INDENT_DEFAULT
;
192 *linecount
= *charcount
= 0;
194 while (fgetline(&linebuf
, &linesize
, &cnt
, &linelen
, fbuf
, 0) != NULL
) {
195 if (indl
> 0 && fwrite(indb
, sizeof *indb
, indl
, _coll_fp
) != indl
)
197 if (fwrite(linebuf
, sizeof *linebuf
, linelen
, _coll_fp
) != linelen
)
200 (*charcount
) += linelen
+ indl
;
202 if (fflush(_coll_fp
))
209 if (fbuf
!= NULL
&& fbuf
!= n_stdin
)
216 insertcommand(FILE *fp
, char const *cmd
)
222 if ((ibuf
= Popen(cmd
, "r", ok_vlook(SHELL
), NULL
, 0)) != NULL
) {
223 while ((c
= getc(ibuf
)) != EOF
) /* XXX bytewise, yuck! */
232 print_collf(FILE *cf
, struct header
*hp
)
236 size_t cnt
, linesize
, linelen
;
240 cnt
= (size_t)fsize(cf
);
242 if((obuf
= Ftmp(NULL
, "collfp", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
){
243 n_perr(_("Can't create temporary file for `~p' command"), 0);
249 fprintf(obuf
, _("-------\nMessage contains:\n"));
250 puthead(TRU1
, hp
, obuf
,
251 (GIDENT
| GTO
| GSUBJECT
| GCC
| GBCC
| GNL
| GFILES
| GCOMMA
),
252 SEND_TODISP
, CONV_NONE
, NULL
, NULL
);
256 while(fgetline(&lbuf
, &linesize
, &cnt
, &linelen
, cf
, 1))
257 prout(lbuf
, linelen
, obuf
);
261 if(hp
->h_attach
!= NULL
){
262 fputs(_("-------\nAttachments:\n"), obuf
);
263 n_attachment_list_print(hp
->h_attach
, obuf
);
268 page_or_print(obuf
, 0);
275 exwrite(char const *name
, FILE *fp
, int f
)
283 fprintf(n_stdout
, "%s ", n_shexp_quote_cp(name
, FAL0
));
287 if ((of
= Fopen(name
, "a")) == NULL
) {
293 while ((c
= getc(fp
)) != EOF
) {
297 if (putc(c
, of
) == EOF
) {
302 fprintf(n_stdout
, _("%ld/%ld\n"), lc
, cc
);
313 putc('\n', n_stdout
);
319 makeheader(FILE *fp
, struct header
*hp
, si8_t
*checkaddr_err
)
326 if ((nf
= Ftmp(NULL
, "colhead", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==NULL
) {
327 n_perr(_("temporary mail edit file"), 0);
331 extract_header(fp
, hp
, checkaddr_err
);
332 if (checkaddr_err
!= NULL
&& *checkaddr_err
!= 0)
335 while ((c
= getc(fp
)) != EOF
) /* XXX bytewise, yuck! */
341 if (check_from_and_sender(hp
->h_from
, hp
->h_sender
) == NULL
)
350 mesedit(int c
, struct header
*hp
)
353 sighandler_type sigint
;
357 if(!(saved
= ok_blook(add_file_recipients
)))
358 ok_bset(add_file_recipients
);
360 sigint
= safe_signal(SIGINT
, SIG_IGN
);
361 nf
= run_editor(_coll_fp
, (off_t
)-1, c
, FAL0
, hp
, NULL
, SEND_MBOX
, sigint
);
365 makeheader(nf
, hp
, NULL
);
367 fseek(nf
, 0L, SEEK_END
);
372 safe_signal(SIGINT
, sigint
);
375 ok_bclear(add_file_recipients
);
383 sighandler_type sigint
;
386 sigint
= safe_signal(SIGINT
, SIG_IGN
);
388 if ((nf
= Ftmp(NULL
, "colpipe", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==NULL
) {
389 n_perr(_("temporary mail edit file"), 0);
393 /* stdin = current message. stdout = new message */
395 if (run_command(ok_vlook(SHELL
), 0, fileno(_coll_fp
), fileno(nf
), "-c",
396 cmd
, NULL
, NULL
) < 0) {
401 if (fsize(nf
) == 0) {
402 n_err(_("No bytes from %s !?\n"), n_shexp_quote_cp(cmd
, FAL0
));
408 fseek(nf
, 0L, SEEK_END
);
412 safe_signal(SIGINT
, sigint
);
417 forward(char *ms
, FILE *fp
, int f
)
420 struct n_ignore
const *itp
;
422 enum sendaction action
;
425 msgvec
= salloc((size_t)(msgCount
+ 1) * sizeof *msgvec
);
426 if (getmsglist(ms
, msgvec
, 0) < 0)
429 *msgvec
= first(0, MMNORM
);
431 n_err(_("No appropriate messages\n"));
437 if (f
== 'f' || f
== 'F' || f
== 'u')
439 else if ((tabst
= ok_vlook(indentprefix
)) == NULL
)
440 tabst
= INDENT_DEFAULT
;
441 if (f
== 'u' || f
== 'U')
444 itp
= upperchar(f
) ? NULL
: n_IGNORE_TYPE
;
445 action
= (upperchar(f
) && f
!= 'U') ? SEND_QUOTE_ALL
: SEND_QUOTE
;
447 fprintf(n_stdout
, _("Interpolating:"));
449 for (; *msgvec
!= 0; ++msgvec
) {
450 struct message
*mp
= message
+ *msgvec
- 1;
453 fprintf(n_stdout
, " %d", *msgvec
);
455 if (sendmp(mp
, fp
, itp
, tabst
, action
, NULL
) < 0) {
456 n_perr(_("temporary mail file"), 0);
463 fprintf(n_stdout
, "\n");
470 a_collect_plumbing(char const *ms
, struct header
*hp
){
471 /* TODO _collect_plumbing: instead of fields the basic headers should
472 * TODO be in an array and have IDs, like in termcap etc., so then this
473 * TODO could be simplified as table-walks. Also true for arg-checks! */
474 char const *cp
, *cmd
[4];
477 /* Protcol version for *on-compose-done-shell* -- update manual on change! */
478 #define a_COLL_PLUMBING_VERSION "0 0 1"
484 for(i
= 0; i
< n_NELEM(cmd
); ++i
){
485 while(blankchar(*cp
))
490 if(i
< n_NELEM(cmd
) - 1)
491 for(cmd
[i
] = cp
++; *cp
!= '\0' && !blankchar(*cp
); ++cp
)
494 /* Last slot takes all the rest of the line, less trailing WS */
495 for(cmd
[i
] = cp
++; *cp
!= '\0'; ++cp
)
497 while(blankchar(cp
[-1]))
500 cmd
[i
] = savestrbuf(cmd
[i
], PTR2SIZE(cp
- cmd
[i
]));
505 if(n_UNLIKELY(cmd
[0] == NULL
))
508 if(is_asccaseprefix(cmd
[0], "header")){
509 struct n_header_field
*hfp
;
510 struct name
*np
, **npp
;
512 if(cmd
[1] == NULL
|| is_asccaseprefix(cmd
[1], "list")){
514 fputs("210", n_stdout
);
515 if(hp
->h_from
!= NULL
) fputs(" From", n_stdout
);
516 if(hp
->h_sender
!= NULL
) fputs(" Sender", n_stdout
);
517 if(hp
->h_to
!= NULL
) fputs(" To", n_stdout
);
518 if(hp
->h_cc
!= NULL
) fputs(" Cc", n_stdout
);
519 if(hp
->h_bcc
!= NULL
) fputs(" Bcc", n_stdout
);
520 if(hp
->h_subject
!= NULL
) fputs(" Subject", n_stdout
);
521 if(hp
->h_replyto
!= NULL
) fputs(" Reply-To", n_stdout
);
522 if(hp
->h_mft
!= NULL
) fputs(" Mail-Followup-To", n_stdout
);
523 if(hp
->h_message_id
!= NULL
) fputs(" Message-ID", n_stdout
);
524 if(hp
->h_ref
!= NULL
) fputs(" References", n_stdout
);
525 if(hp
->h_in_reply_to
!= NULL
) fputs(" In-Reply-To", n_stdout
);
526 for(hfp
= hp
->h_user_headers
; hfp
!= NULL
; hfp
= hfp
->hf_next
){
528 fputs(&hfp
->hf_dat
[0], n_stdout
);
530 putc('\n', n_stdout
);
537 if(!asccasecmp(cmd
[2], "from")){
540 fprintf(n_stdout
, "%s %s\n", (np
== NULL
? "501" : "210"), cp
);
543 if(!asccasecmp(cmd
[2], cp
= "Sender")){
547 if(!asccasecmp(cmd
[2], cp
= "To")){
551 if(!asccasecmp(cmd
[2], cp
= "Cc")){
555 if(!asccasecmp(cmd
[2], cp
= "Bcc")){
559 if(!asccasecmp(cmd
[2], cp
= "Subject")){
560 np
= (struct name
*)-1;
563 if(!asccasecmp(cmd
[2], cp
= "Reply-To")){
567 if(!asccasecmp(cmd
[2], cp
= "Mail-Followup-To")){
571 if(!asccasecmp(cmd
[2], cp
= "Message-ID")){
572 np
= hp
->h_message_id
;
575 if(!asccasecmp(cmd
[2], cp
= "References")){
579 if(!asccasecmp(cmd
[2], cp
= "In-Reply-To")){
580 np
= hp
->h_in_reply_to
;
584 /* Primitive name normalization XXX header object should
585 * XXX have a more sophisticated accessible one */
589 cp
= xp
= savestr(cmd
[2]);
590 xp
[0] = upperchar(xp
[0]);
592 xp
[0] = lowerchar(xp
[0]);
594 for(hfp
= hp
->h_user_headers
;; hfp
= hfp
->hf_next
){
597 else if(!asccasecmp(cp
, &hfp
->hf_dat
[0])){
598 fprintf(n_stdout
, "210 %s\n", cp
);
606 if(is_asccaseprefix(cmd
[1], "show")){
607 if(cmd
[2] == NULL
|| cmd
[3] != NULL
)
610 if(!asccasecmp(cmd
[2], "from")){
614 fprintf(n_stdout
, "211 %s\n", cp
);
615 do if(!(np
->n_type
& GDEL
))
616 fprintf(n_stdout
, "%s %s\n", np
->n_name
, np
->n_fullname
);
617 while((np
= np
->n_flink
) != NULL
);
618 putc('\n', n_stdout
);
623 if(!asccasecmp(cmd
[2], cp
= "Sender")){
627 if(!asccasecmp(cmd
[2], cp
= "To")){
631 if(!asccasecmp(cmd
[2], cp
= "Cc")){
635 if(!asccasecmp(cmd
[2], cp
= "Bcc")){
639 if(!asccasecmp(cmd
[2], cp
= "Reply-To")){
643 if(!asccasecmp(cmd
[2], cp
= "Mail-Followup-To")){
647 if(!asccasecmp(cmd
[2], cp
= "Message-ID")){
648 np
= hp
->h_message_id
;
651 if(!asccasecmp(cmd
[2], cp
= "References")){
655 if(!asccasecmp(cmd
[2], cp
= "In-Reply-To")){
656 np
= hp
->h_in_reply_to
;
660 if(!asccasecmp(cmd
[2], cp
= "Subject")){
661 if(hp
->h_subject
!= NULL
)
662 fprintf(n_stdout
, "212 %s\n%s\n\n", cp
, hp
->h_subject
);
664 fprintf(n_stdout
, "501 %s\n", cp
);
668 /* Primitive name normalization XXX header object should
669 * XXX have a more sophisticated accessible one */
674 cp
= xp
= savestr(cmd
[2]);
675 xp
[0] = upperchar(xp
[0]);
677 xp
[0] = lowerchar(xp
[0]);
679 for(any
= FAL0
, hfp
= hp
->h_user_headers
; hfp
!= NULL
;
681 if(!asccasecmp(cp
, &hfp
->hf_dat
[0])){
683 fprintf(n_stdout
, "212 %s\n", cp
);
685 fprintf(n_stdout
, "%s\n", &hfp
->hf_dat
[hfp
->hf_nl
+1]);
689 putc('\n', n_stdout
);
696 if(is_asccaseprefix(cmd
[1], "remove")){
697 if(cmd
[2] == NULL
|| cmd
[3] != NULL
)
700 if(!asccasecmp(cmd
[2], "from")){
705 fprintf(n_stdout
, "210 %s\n", cp
);
710 if(!asccasecmp(cmd
[2], cp
= "Sender")){
714 if(!asccasecmp(cmd
[2], cp
= "To")){
718 if(!asccasecmp(cmd
[2], cp
= "Cc")){
722 if(!asccasecmp(cmd
[2], cp
= "Bcc")){
726 if(!asccasecmp(cmd
[2], cp
= "Reply-To")){
727 npp
= &hp
->h_replyto
;
730 if(!asccasecmp(cmd
[2], cp
= "Mail-Followup-To")){
734 if(!asccasecmp(cmd
[2], cp
= "Message-ID")){
735 npp
= &hp
->h_message_id
;
738 if(!asccasecmp(cmd
[2], cp
= "References")){
742 if(!asccasecmp(cmd
[2], cp
= "In-Reply-To")){
743 npp
= &hp
->h_in_reply_to
;
747 if(!asccasecmp(cmd
[2], cp
= "Subject")){
748 if(hp
->h_subject
!= NULL
){
749 hp
->h_subject
= NULL
;
750 fprintf(n_stdout
, "210 %s\n", cp
);
756 /* Primitive name normalization XXX header object should
757 * XXX have a more sophisticated accessible one */
759 struct n_header_field
**hfpp
;
763 cp
= xp
= savestr(cmd
[2]);
764 xp
[0] = upperchar(xp
[0]);
766 xp
[0] = lowerchar(xp
[0]);
768 for(any
= FAL0
, hfpp
= &hp
->h_user_headers
; (hfp
= *hfpp
) != NULL
;){
769 if(!asccasecmp(cp
, &hfp
->hf_dat
[0])){
770 *hfpp
= hfp
->hf_next
;
772 fprintf(n_stdout
, "210 %s\n", cp
);
775 hfp
= *(hfpp
= &hfp
->hf_next
);
783 if(is_asccaseprefix(cmd
[1], "insert")){ /* TODO LOGIC BELONGS head.c
784 * TODO That is: Header::factory(string) -> object (blahblah).
785 * TODO I.e., as long as we don't have regular RFC compliant parsers
786 * TODO which differentiate in between structured and unstructured
787 * TODO header fields etc., a little workaround */
789 enum expand_addr_check_mode eacm
;
793 if(cmd
[2] == NULL
|| cmd
[3] == NULL
)
796 /* Strip [\r\n] which would render a body invalid XXX all controls? */
800 cmd
[3] = xp
= savestr(cmd
[3]);
801 for(; (c
= *xp
) != '\0'; ++xp
)
802 if(c
== '\n' || c
== '\r')
807 ntype
= GEXTRA
| GFULL
| GFULLEXTRA
;
810 if(!asccasecmp(cmd
[2], "from")){
814 if((np
= lextract(cmd
[3], ntype
)) == NULL
)
817 if((np
= checkaddrs(np
, eacm
, &aerr
), aerr
!= 0)){
818 fprintf(n_stdout
, "505 %s\n", cp
);
825 for(xnp
= *npp
; xnp
!= NULL
&& !(xnp
->n_type
& GDEL
);
828 if(xnp
!= NULL
|| np
->n_flink
!= NULL
){
829 fprintf(n_stdout
, "506 %s\n", cp
);
834 *npp
= cat(*npp
, np
);
835 fprintf(n_stdout
, "210 %s\n", cp
);
838 if(!asccasecmp(cmd
[2], cp
= "Sender")){
843 if(!asccasecmp(cmd
[2], cp
= "To")){
846 eacm
= EACM_NORMAL
| EAF_NAME
;
849 if(!asccasecmp(cmd
[2], cp
= "Cc")){
852 eacm
= EACM_NORMAL
| EAF_NAME
;
855 if(!asccasecmp(cmd
[2], cp
= "Bcc")){
857 ntype
= GBCC
| GFULL
;
858 eacm
= EACM_NORMAL
| EAF_NAME
;
861 if(!asccasecmp(cmd
[2], cp
= "Reply-To")){
862 npp
= &hp
->h_replyto
;
865 if(!asccasecmp(cmd
[2], cp
= "Mail-Followup-To")){
870 if(!asccasecmp(cmd
[2], cp
= "Message-ID")){
872 npp
= &hp
->h_message_id
;
876 if(!asccasecmp(cmd
[2], cp
= "References")){
882 if(!asccasecmp(cmd
[2], cp
= "In-Reply-To")){
883 npp
= &hp
->h_in_reply_to
;
889 if(!asccasecmp(cmd
[2], cp
= "Subject")){
890 if(cmd
[3][0] != '\0'){
891 if(hp
->h_subject
!= NULL
)
892 hp
->h_subject
= savecatsep(hp
->h_subject
, ' ', cmd
[3]);
894 hp
->h_subject
= n_UNCONST(cmd
[3]);
895 fprintf(n_stdout
, "210 %s\n", cp
);
901 /* Primitive name normalization XXX header object should
902 * XXX have a more sophisticated accessible one */
905 struct n_header_field
**hfpp
;
907 for(cp
= cmd
[2]; *cp
!= '\0'; ++cp
)
908 if(!fieldnamechar(*cp
)){
913 for(hfpp
= &hp
->h_user_headers
; *hfpp
!= NULL
;)
914 hfpp
= &(*hfpp
)->hf_next
;
916 nl
= strlen(cp
= cmd
[2]);
917 bl
= strlen(cmd
[3]) +1;
918 *hfpp
= hfp
= salloc(n_VSTRUCT_SIZEOF(struct n_header_field
, hf_dat
923 memcpy(hfp
->hf_dat
, cp
, nl
);
924 hfp
->hf_dat
[nl
++] = '\0';
925 memcpy(hfp
->hf_dat
+ nl
, cmd
[3], bl
);
926 fprintf(n_stdout
, "210 %s\n", cp
);
934 if(is_asccaseprefix(cmd
[0], "attachment")){
936 struct attachment
*ap
;
938 if(cmd
[1] == NULL
|| is_asccaseprefix(cmd
[1], "list")){
942 if((ap
= hp
->h_attach
) != NULL
){
943 fputs("212\n", n_stdout
);
945 fprintf(n_stdout
, "%s\n", ap
->a_path_user
);
946 while((ap
= ap
->a_flink
) != NULL
);
947 putc('\n', n_stdout
);
949 fputs("501\n", n_stdout
);
953 if(is_asccaseprefix(cmd
[1], "remove")){
954 if(cmd
[2] == NULL
|| cmd
[3] != NULL
)
957 if((ap
= n_attachment_find(hp
->h_attach
, cmd
[2], &status
)) != NULL
){
959 fputs("506\n", n_stdout
);
961 hp
->h_attach
= n_attachment_remove(hp
->h_attach
, ap
);
962 fprintf(n_stdout
, "210 %s\n", cmd
[2]);
965 fputs("501\n", n_stdout
);
969 if(is_asccaseprefix(cmd
[1], "remove-at")){
972 if(cmd
[2] == NULL
|| cmd
[3] != NULL
)
975 if((n_idec_uiz_cp(&i
, cmd
[2], 0, NULL
976 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
977 ) != n_IDEC_STATE_CONSUMED
|| i
== 0)
978 fputs("505\n", n_stdout
);
980 for(ap
= hp
->h_attach
; ap
!= NULL
&& --i
!= 0; ap
= ap
->a_flink
)
983 hp
->h_attach
= n_attachment_remove(hp
->h_attach
, ap
);
984 fprintf(n_stdout
, "210 %s\n", cmd
[2]);
986 fputs("501\n", n_stdout
);
991 if(is_asccaseprefix(cmd
[1], "insert")){
992 enum n_attach_error aerr
;
994 if(cmd
[2] == NULL
|| cmd
[3] != NULL
)
997 hp
->h_attach
= n_attachment_append(hp
->h_attach
, cmd
[2], &aerr
, &ap
);
999 case n_ATTACH_ERR_FILE_OPEN
: cp
= "505\n"; goto jatt_ins
;
1000 case n_ATTACH_ERR_ICONV_FAILED
: cp
= "506\n"; goto jatt_ins
;
1001 case n_ATTACH_ERR_ICONV_NAVAIL
:
1002 case n_ATTACH_ERR_OTHER
:
1006 fputs(cp
, n_stdout
);
1008 case n_ATTACH_ERR_NONE
:{
1011 for(i
= 0; ap
!= NULL
; ++i
, ap
= ap
->a_blink
)
1013 fprintf(n_stdout
, "210 %" PRIuZ
"\n", i
);
1019 if(is_asccaseprefix(cmd
[1], "attribute")){
1020 if(cmd
[2] == NULL
|| cmd
[3] != NULL
)
1023 if((ap
= n_attachment_find(hp
->h_attach
, cmd
[2], NULL
)) != NULL
){
1025 fprintf(n_stdout
, "212 %s\n", cmd
[2]);
1027 fprintf(n_stdout
, "message-number %d\n\n", ap
->a_msgno
);
1030 "creation-name %s\nopen-path %s\nfilename %s\n",
1031 ap
->a_path_user
, ap
->a_path
, ap
->a_name
);
1032 if(ap
->a_content_description
!= NULL
)
1033 fprintf(n_stdout
, "content-description %s\n",
1034 ap
->a_content_description
);
1035 if(ap
->a_content_id
!= NULL
)
1036 fprintf(n_stdout
, "content-id %s\n",
1037 ap
->a_content_id
->n_name
);
1038 if(ap
->a_content_type
!= NULL
)
1039 fprintf(n_stdout
, "content-type %s\n", ap
->a_content_type
);
1040 if(ap
->a_content_disposition
!= NULL
)
1041 fprintf(n_stdout
, "content-disposition %s\n",
1042 ap
->a_content_disposition
);
1043 putc('\n', n_stdout
);
1046 fputs("501\n", n_stdout
);
1050 if(is_asccaseprefix(cmd
[1], "attribute-at")){
1053 if(cmd
[2] == NULL
|| cmd
[3] != NULL
)
1056 if((n_idec_uiz_cp(&i
, cmd
[2], 0, NULL
1057 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
1058 ) != n_IDEC_STATE_CONSUMED
|| i
== 0)
1059 fputs("505\n", n_stdout
);
1061 for(ap
= hp
->h_attach
; ap
!= NULL
&& --i
!= 0; ap
= ap
->a_flink
)
1066 fputs("501\n", n_stdout
);
1071 if(is_asccaseprefix(cmd
[1], "attribute-set")){
1072 if(cmd
[2] == NULL
|| cmd
[3] == NULL
)
1075 if((ap
= n_attachment_find(hp
->h_attach
, cmd
[2], NULL
)) != NULL
){
1078 fputs("505\n", n_stdout
);
1083 while((c
= *cp
) != '\0' && !blankchar(c
))
1085 keyw
= savestrbuf(cmd
[3], PTR2SIZE(cp
- cmd
[3]));
1087 for(; (c
= *++cp
) != '\0' && blankchar(c
);)
1092 /* Strip [\r\n] which would render a parameter invalid XXX
1093 * XXX all controls? */
1094 cp
= xp
= savestr(cp
);
1095 for(; (c
= *xp
) != '\0'; ++xp
)
1096 if(c
== '\n' || c
== '\r')
1102 if(!asccasecmp(keyw
, "filename"))
1103 ap
->a_name
= (c
== '\0') ? ap
->a_path_bname
: cp
;
1104 else if(!asccasecmp(keyw
, "content-description"))
1105 ap
->a_content_description
= (c
== '\0') ? NULL
: cp
;
1106 else if(!asccasecmp(keyw
, "content-id")){
1107 ap
->a_content_id
= NULL
;
1112 np
= checkaddrs(lextract(cp
, GREF
),
1113 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG
|
1115 if(np
!= NULL
&& np
->n_flink
== NULL
)
1116 ap
->a_content_id
= np
;
1120 }else if(!asccasecmp(keyw
, "content-type"))
1121 ap
->a_content_type
= (c
== '\0') ? NULL
: cp
;
1122 else if(!asccasecmp(keyw
, "content-disposition"))
1123 ap
->a_content_disposition
= (c
== '\0') ? NULL
: cp
;
1130 for(i
= 0; ap
!= NULL
; ++i
, ap
= ap
->a_blink
)
1132 fprintf(n_stdout
, "210 %" PRIuZ
"\n", i
);
1134 fputs("505\n", n_stdout
);
1137 fputs("501\n", n_stdout
);
1141 if(is_asccaseprefix(cmd
[1], "attribute-set-at")){
1144 if(cmd
[2] == NULL
|| cmd
[3] == NULL
)
1147 if((n_idec_uiz_cp(&i
, cmd
[2], 0, NULL
1148 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
1149 ) != n_IDEC_STATE_CONSUMED
|| i
== 0)
1150 fputs("505\n", n_stdout
);
1152 for(ap
= hp
->h_attach
; ap
!= NULL
&& --i
!= 0; ap
= ap
->a_flink
)
1157 fputs("501\n", n_stdout
);
1166 fputs("500\n", n_stdout
);
1171 return (ms
!= NULL
);
1174 fputs("501 ", n_stdout
);
1175 fputs(cp
, n_stdout
);
1176 putc('\n', n_stdout
);
1183 NYD_X
; /* Signal handler */
1185 /* the control flow is subtle, because we can be called from ~q */
1186 if (_coll_hadintr
== 0) {
1187 if (ok_blook(ignore
)) {
1188 fputs("@\n", n_stdout
);
1193 siglongjmp(_coll_jmp
, 1);
1195 n_exit_status
|= n_EXIT_SEND_ERROR
;
1197 savedeadletter(_coll_fp
, TRU1
);
1198 /* Aborting message, no need to fflush() .. */
1199 siglongjmp(_coll_abort
, 1);
1205 NYD_X
; /* Signal handler */
1208 savedeadletter(_coll_fp
, TRU1
);
1209 /* Let's pretend nobody else wants to clean up, a true statement at
1215 putesc(char const *s
, FILE *stream
)
1220 while (s
[0] != '\0') {
1223 if (putc('\t', stream
) == EOF
)
1230 if (putc('\n', stream
) == EOF
)
1237 if (putc(s
[0], stream
) == EOF
)
1242 if (putc('\n', stream
) == EOF
)
1251 a_coll__hook_setter(void *arg
){ /* TODO v15: drop */
1258 if((val
= detract(hp
->h_from
, GNAMEONLY
)) == NULL
)
1260 ok_vset(compose_from
, val
);
1261 if((val
= detract(hp
->h_sender
, 0)) == NULL
)
1263 ok_vset(compose_sender
, val
);
1264 if((val
= detract(hp
->h_to
, GNAMEONLY
)) == NULL
)
1266 ok_vset(compose_to
, val
);
1267 if((val
= detract(hp
->h_cc
, GNAMEONLY
)) == NULL
)
1269 ok_vset(compose_cc
, val
);
1270 if((val
= detract(hp
->h_bcc
, GNAMEONLY
)) == NULL
)
1272 ok_vset(compose_bcc
, val
);
1273 if((val
= hp
->h_subject
) == NULL
)
1275 ok_vset(compose_subject
, val
);
1280 a_coll_ocds__mac(void){
1281 /* Executes in a fork(2)ed child */
1282 setvbuf(n_stdout
, NULL
, _IOLBF
, 0);
1283 n_psonce
&= ~(n_PSO_INTERACTIVE
| n_PSO_TTYIN
| n_PSO_TTYOUT
);
1284 n_pstate
|= n_PS_COMPOSE_FORKHOOK
;
1285 temporary_compose_mode_hook_call(a_coll_ocds__macname
, NULL
, NULL
);
1292 a_coll_ocds__finalize(void *vp
){
1293 /* Note we use this for destruction upon setup errors, thus */
1294 sighandler_type opipe
;
1295 sighandler_type oint
;
1296 struct a_coll_ocds_arg
**coapp
, *coap
;
1299 temporary_compose_mode_hook_call((char*)-1, NULL
, NULL
);
1301 coap
= *(coapp
= vp
);
1302 *coapp
= (struct a_coll_ocds_arg
*)-1;
1304 if(coap
->coa_stdout
!= NULL
)
1305 if(!Pclose(coap
->coa_stdout
, FAL0
)){
1306 *coap
->coa_senderr
= TRU1
;
1307 n_err(_("*on-compose-done(-shell)?* failed: %s\n"),
1308 n_shexp_quote_cp(coap
->coa_cmd
, FAL0
));
1311 if(coap
->coa_stdin
!= NULL
)
1312 Fclose(coap
->coa_stdin
);
1313 else if(coap
->coa_pipe
[0] != -1)
1314 close(coap
->coa_pipe
[0]);
1316 if(coap
->coa_pipe
[1] != -1)
1317 close(coap
->coa_pipe
[1]);
1319 opipe
= coap
->coa_opipe
;
1320 oint
= coap
->coa_oint
;
1325 safe_signal(SIGPIPE
, opipe
);
1326 safe_signal(SIGINT
, oint
);
1332 collect(struct header
*hp
, int printheaders
, struct message
*mp
,
1333 char const *quotefile
, int doprefix
, si8_t
*checkaddr_err
)
1335 struct n_ignore
const *quoteitp
;
1336 struct a_coll_ocds_arg
*coap
;
1338 int volatile t
, eofcnt
, getfields
;
1339 char *linebuf
, escape_saved
, escape
;
1340 char const *cp
, *coapm
;
1341 size_t i
, linesize
; /* TODO line pool */
1343 enum sendaction action
;
1344 sigset_t oset
, nset
;
1345 FILE * volatile sigfp
;
1356 /* Start catching signals from here, but we're still die on interrupts
1357 * until we're in the main loop */
1359 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
1360 /* FIXME have dropped handlerpush() and localized onintr() in lex.c! */
1361 if ((_coll_saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
1362 safe_signal(SIGINT
, &_collint
);
1363 if ((_coll_savehup
= safe_signal(SIGHUP
, SIG_IGN
)) != SIG_IGN
)
1364 safe_signal(SIGHUP
, collhup
);
1365 if (sigsetjmp(_coll_abort
, 1))
1367 if (sigsetjmp(_coll_jmp
, 1))
1369 n_pstate
|= n_PS_COMPOSE_MODE
;
1370 sigprocmask(SIG_SETMASK
, &oset
, (sigset_t
*)NULL
);
1372 if ((_coll_fp
= Ftmp(NULL
, "collect", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==
1374 n_perr(_("temporary mail file"), 0);
1378 /* If we are going to prompt for a subject, refrain from printing a newline
1379 * after the headers (since some people mind) */
1381 if (!(n_poption
& n_PO_t_FLAG
)) {
1382 t
= GTO
| GSUBJECT
| GCC
| GNL
;
1383 if (ok_blook(fullnames
))
1386 if (n_psonce
& n_PSO_INTERACTIVE
) {
1387 if (hp
->h_subject
== NULL
&& (ok_blook(ask
) || ok_blook(asksub
)))
1388 t
&= ~GNL
, getfields
|= GSUBJECT
;
1390 if (hp
->h_to
== NULL
)
1391 t
&= ~GNL
, getfields
|= GTO
;
1393 if (!ok_blook(bsdcompat
) && !ok_blook(askatend
)) {
1394 if (hp
->h_bcc
== NULL
&& ok_blook(askbcc
))
1395 t
&= ~GNL
, getfields
|= GBCC
;
1396 if (hp
->h_cc
== NULL
&& ok_blook(askcc
))
1397 t
&= ~GNL
, getfields
|= GCC
;
1404 escape_saved
= escape
= ((cp
= ok_vlook(escape
)) != NULL
) ? *cp
: n_ESCAPE
;
1407 if (!sigsetjmp(_coll_jmp
, 1)) {
1408 /* Ask for some headers first, as necessary */
1410 grab_headers(n_LEXINPUT_CTX_COMPOSE
, hp
, getfields
, 1);
1412 /* Execute compose-enter TODO completely v15-compat intermediate!! */
1413 if((cp
= ok_vlook(on_compose_enter
)) != NULL
){
1414 setup_from_and_sender(hp
);
1415 temporary_compose_mode_hook_call(cp
, &a_coll__hook_setter
, hp
);
1418 /* Cannot do since it may require turning this into a multipart one */
1419 if(!(n_poption
& n_PO_Mm_FLAG
)){
1420 char const *cp_obsolete
= ok_vlook(NAIL_HEAD
);
1421 if(cp_obsolete
!= NULL
)
1422 n_OBSOLETE(_("please use *message-inject-head*, not *NAIL_HEAD*"));
1424 if(((cp
= ok_vlook(message_inject_head
)) != NULL
||
1425 (cp
= cp_obsolete
) != NULL
) && putesc(cp
, _coll_fp
) < 0)
1428 /* Quote an original message */
1429 if (mp
!= NULL
&& (doprefix
|| (cp
= ok_vlook(quote
)) != NULL
)) {
1430 quoteitp
= n_IGNORE_ALL
;
1431 action
= SEND_QUOTE
;
1433 quoteitp
= n_IGNORE_FWD
;
1434 if ((cp
= ok_vlook(fwdheading
)) == NULL
)
1435 cp
= "-------- Original Message --------";
1436 if (*cp
!= '\0' && fprintf(_coll_fp
, "%s\n", cp
) < 0)
1438 } else if (!strcmp(cp
, "noheading")) {
1440 } else if (!strcmp(cp
, "headers")) {
1441 quoteitp
= n_IGNORE_TYPE
;
1442 } else if (!strcmp(cp
, "allheaders")) {
1444 action
= SEND_QUOTE_ALL
;
1446 cp
= hfield1("from", mp
);
1447 if (cp
!= NULL
&& (cnt
= (long)strlen(cp
)) > 0) {
1448 if (xmime_write(cp
, cnt
, _coll_fp
, CONV_FROMHDR
, TD_NONE
) < 0)
1450 if (fprintf(_coll_fp
, _(" wrote:\n\n")) < 0)
1454 if (fflush(_coll_fp
))
1458 else if ((cp
= ok_vlook(indentprefix
)) == NULL
)
1459 cp
= INDENT_DEFAULT
;
1460 if (sendmp(mp
, _coll_fp
, quoteitp
, cp
, action
, NULL
) < 0)
1465 if (quotefile
!= NULL
) {
1466 if (_include_file(quotefile
, &lc
, &cc
, FAL0
) != 0)
1470 if((n_psonce
& n_PSO_INTERACTIVE
) && !(n_poption
& n_PO_Mm_FLAG
)){
1471 /* Print what we have sofar also on the terminal (if useful) */
1472 if (!ok_blook(editalong
)) {
1474 puthead(TRU1
, hp
, n_stdout
, t
,
1475 SEND_TODISP
, CONV_NONE
, NULL
, NULL
);
1478 while ((c
= getc(_coll_fp
)) != EOF
) /* XXX bytewise, yuck! */
1480 if (fseek(_coll_fp
, 0, SEEK_END
))
1485 /* As mandated by the Mail Reference Manual, print "(continue)" */
1487 if(n_psonce
& n_PSO_INTERACTIVE
)
1488 fputs(_("(continue)\n"), n_stdout
);
1493 /* Come here for printing the after-signal message. Duplicate messages
1494 * won't be printed because the write is aborted if we get a SIGTTOU */
1496 n_err(_("\n(Interrupt -- one more to kill letter)\n"));
1499 /* If not under shell hook control */
1501 /* We're done with -M or -m (because we are too simple minded) */
1502 if(n_poption
& n_PO_Mm_FLAG
)
1504 /* No command escapes, interrupts not expected. Simply copy STDIN */
1505 if(!(n_psonce
& n_PSO_INTERACTIVE
) &&
1506 !(n_poption
& (n_PO_t_FLAG
| n_PO_TILDE_FLAG
))){
1507 linebuf
= srealloc(linebuf
, linesize
= LINESIZE
);
1508 while ((i
= fread(linebuf
, sizeof *linebuf
, linesize
, n_stdin
)) > 0) {
1509 if (i
!= fwrite(linebuf
, sizeof *linebuf
, i
, _coll_fp
))
1516 /* The interactive collect loop */
1519 enum n_lexinput_flags lif
;
1521 lif
= n_LEXINPUT_CTX_COMPOSE
;
1522 if((n_psonce
& n_PSO_INTERACTIVE
) || (n_poption
& n_PO_TILDE_FLAG
)){
1523 if(!(n_poption
& n_PO_t_FLAG
))
1524 lif
|= n_LEXINPUT_NL_ESC
;
1526 cnt
= n_lex_input(lif
, n_empty
, &linebuf
, &linesize
, NULL
);
1532 if(n_poption
& n_PO_t_FLAG
){
1533 fflush_rewind(_coll_fp
);
1534 /* It is important to set n_PSO_t_FLAG before extract_header()
1535 * *and* keep n_PO_t_FLAG for the first parse of the message! */
1536 n_psonce
|= n_PSO_t_FLAG
;
1537 if (makeheader(_coll_fp
, hp
, checkaddr_err
) != OKAY
)
1539 n_poption
&= ~n_PO_t_FLAG
;
1541 }else if((n_psonce
& n_PSO_INTERACTIVE
) && ok_blook(ignoreeof
) &&
1544 _("*ignoreeof* set, use `~.' to terminate letter\n"));
1545 n_lex_input_clearerr();
1556 else if(coap
== NULL
){
1557 if(!(n_psonce
& n_PSO_INTERACTIVE
) && !(n_poption
& n_PO_TILDE_FLAG
))
1559 else if(cp
[0] == '.'){
1560 if(cnt
== 1 && (ok_blook(dot
) || ok_blook(ignoreeof
)))
1564 if(cp
[0] != escape
){
1566 if(fwrite(cp
, sizeof *cp
, cnt
, _coll_fp
) != (size_t)cnt
)
1568 /* TODO n_PS_READLINE_NL is a terrible hack to ensure that _in_all_-
1569 * TODO _code_paths_ a file without trailing newline isn't modified
1570 * TODO to continue one; the "saw-newline" needs to be part of an
1571 * TODO I/O input machinery object */
1573 if(n_pstate
& n_PS_READLINE_NL
){
1574 if(putc('\n', _coll_fp
) == EOF
)
1580 /* Cleanup the input string: like this we can perform a little bit of
1581 * usage testing and also have somewhat normalized history entries */
1582 for(cp
= &linebuf
[2]; (c
= *cp
) != '\0' && blankspacechar(c
); ++cp
)
1588 i
= PTR2SIZE(cp
- linebuf
) - 3;
1589 memmove(&linebuf
[3], cp
, (cnt
-= i
));
1591 linebuf
[cnt
] = '\0';
1593 if(cnt
> 0){ /* TODO v15 no more trailing WS from lex_input please */
1598 if(!blankspacechar(c
))
1601 ((char*)n_UNCONST(cp
))[0] = '\0';
1602 cnt
= PTR2SIZE(cp
- linebuf
);
1606 switch((c
= linebuf
[1])){
1608 /* On double escape, send a single one. Otherwise, it's an error */
1614 char buf
[sizeof(n_UNIREPL
)];
1617 buf
[0] = c
, buf
[1] = '\0';
1618 else if(n_psonce
& n_PSO_UNICODE
)
1619 memcpy(buf
, n_unirepl
, sizeof n_unirepl
);
1621 buf
[0] = '?', buf
[1] = '\0';
1622 n_err(_("Unknown command escape: ~%s\n"), buf
);
1626 n_err(_("Invalid command escape usage: %s\n"), linebuf
);
1629 /* Shell escape, send the balance of line to sh -c */
1630 if(cnt
== 2 || coap
!= NULL
)
1632 c_shell(&linebuf
[3]);
1637 /* Escape to command mode, but be nice! *//* TODO command expansion
1638 * TODO should be handled here so that we have unique history! */
1641 _execute_command(hp
, &linebuf
[3], cnt
-= 3);
1644 /* Simulate end of file on input */
1645 if(cnt
!= 2 || coap
!= NULL
)
1649 /* Same as 'q', but no *DEAD* saving */
1652 /* Force a quit, act like an interrupt had happened */
1656 _collint((c
== 'x') ? 0 : SIGINT
);
1660 /* Grab a bunch of headers */
1664 grab_headers(n_LEXINPUT_CTX_COMPOSE
, hp
,
1665 (GTO
| GSUBJECT
| GCC
| GBCC
),
1666 (ok_blook(bsdcompat
) && ok_blook(bsdorder
)));
1667 while(hp
->h_to
== NULL
);
1670 /* Grab extra headers */
1674 grab_headers(n_LEXINPUT_CTX_COMPOSE
, hp
, GEXTRA
, 0);
1675 while(check_from_and_sender(hp
->h_from
, hp
->h_sender
) == NULL
);
1678 /* Add to the To: list */
1681 hp
->h_to
= cat(hp
->h_to
,
1682 checkaddrs(lextract(&linebuf
[3], GTO
| GFULL
), EACM_NORMAL
,
1686 /* Set the Subject list */
1689 /* Subject:; take care for Debian #419840 and strip any \r and \n */
1690 if(n_anyof_cp("\n\r", hp
->h_subject
= savestr(&linebuf
[3]))){
1693 n_err(_("-s: normalizing away invalid ASCII NL / CR bytes\n"));
1694 for(xp
= hp
->h_subject
; *xp
!= '\0'; ++xp
)
1695 if(*xp
== '\n' || *xp
== '\r')
1700 struct attachment
*aplist
;
1702 /* Edit the attachment list */
1703 aplist
= hp
->h_attach
;
1704 hp
->h_attach
= NULL
;
1706 hp
->h_attach
= n_attachment_append_list(aplist
, &linebuf
[3]);
1708 hp
->h_attach
= n_attachment_list_edit(aplist
,
1709 n_LEXINPUT_CTX_COMPOSE
);
1712 /* Add to the CC list */
1715 hp
->h_cc
= cat(hp
->h_cc
,
1716 checkaddrs(lextract(&linebuf
[3], GCC
| GFULL
), EACM_NORMAL
,
1720 /* Add stuff to blind carbon copies list */
1723 hp
->h_bcc
= cat(hp
->h_bcc
,
1724 checkaddrs(lextract(&linebuf
[3], GBCC
| GFULL
), EACM_NORMAL
,
1730 cp
= n_getdeadletter();
1736 /* Invoke a file: Search for the file name, then open it and copy
1737 * the contents to _coll_fp */
1739 n_err(_("Interpolate what file?\n"));
1742 if(*(cp
= &linebuf
[3]) == '!'){
1743 insertcommand(_coll_fp
, ++cp
);
1746 if((cp
= fexpand(cp
, FEXP_LOCAL
| FEXP_NOPROTO
)) == NULL
)
1750 n_err(_("%s: is a directory\n"), n_shexp_quote_cp(cp
, FAL0
));
1753 if(_include_file(cp
, &lc
, &cc
, (c
== 'R')) != 0){
1754 if(ferror(_coll_fp
))
1758 fprintf(n_stdout
, _("%s %d/%d\n"), n_shexp_quote_cp(cp
, FAL0
), lc
, cc
);
1761 /* Insert a variable into the file */
1764 if((cp
= n_var_vlook(&linebuf
[3], TRU1
)) == NULL
|| *cp
== '\0')
1766 if(putesc(cp
, _coll_fp
) < 0) /* TODO v15: user resp upon `set' time */
1768 if((n_psonce
& n_PSO_INTERACTIVE
) && putesc(cp
, n_stdout
) < 0)
1773 /* Insert the contents of a signature variable */
1776 cp
= (c
== 'a') ? ok_vlook(sign
) : ok_vlook(Sign
);
1777 if(cp
!= NULL
&& *cp
!= '\0'){
1778 if(putesc(cp
, _coll_fp
) < 0) /* TODO v15: user upon `set' time */
1780 if((n_psonce
& n_PSO_INTERACTIVE
) && putesc(cp
, n_stdout
) < 0)
1785 /* Write the message on a file */
1788 if((cp
= fexpand(&linebuf
[3], FEXP_LOCAL
| FEXP_NOPROTO
)) == NULL
){
1789 n_err(_("Write what file!?\n"));
1793 if(exwrite(cp
, _coll_fp
, 1) < 0)
1802 /* Interpolate the named messages, if we are in receiving mail mode.
1803 * Does the standard list processing garbage. If ~f is given, we
1804 * don't shift over */
1807 if(forward(&linebuf
[3], _coll_fp
, c
) < 0)
1811 /* Print current state of the message without altering anything */
1814 print_collf(_coll_fp
, hp
);
1817 /* Pipe message through command. Collect output as new message */
1821 mespipe(&linebuf
[3]);
1825 /* Edit the current message. 'e' -> use EDITOR, 'v' -> use VISUAL */
1826 if(cnt
!= 2 || coap
!= NULL
)
1829 mesedit(c
, ok_blook(editheaders
) ? hp
: NULL
);
1832 if(!a_collect_plumbing(&linebuf
[3], hp
))
1834 if(n_psonce
& n_PSO_INTERACTIVE
)
1838 /* Last the lengthy help string. (Very ugly, but take care for
1839 * compiler supported string lengths :() */
1841 "COMMAND ESCAPES (to be placed after a newline) excerpt:\n"
1842 "~. Commit and send message\n"
1843 "~: <command> Execute a mail command\n"
1844 "~<! <command> Insert output of command\n"
1845 "~@ [<files>] Edit attachment list\n"
1846 "~A Insert *Sign* variable (`~a': insert *sign*)\n"
1847 "~c <users> Add users to Cc: list (`~b': to Bcc:)\n"
1848 "~d Read in $DEAD (dead.letter)\n"
1849 "~e Edit message via $EDITOR\n"
1852 "~F <msglist> Read in with headers, don't *indentprefix* lines\n"
1853 "~f <msglist> Like ~F, but honour `ignore' / `retain' configuration\n"
1854 "~H Edit From:, Reply-To: and Sender:\n"
1855 "~h Prompt for Subject:, To:, Cc: and \"blind\" Bcc:\n"
1856 "~i <variable> Insert a value and a newline\n"
1857 "~M <msglist> Read in with headers, *indentprefix* (`~m': `retain' etc.)\n"
1858 "~p Show current message compose buffer\n"
1859 "~r <file> Read in a file (`~<': likewise; `~R': *indentprefix* lines)\n"
1862 "~s <subject> Set Subject:\n"
1863 "~t <users> Add users to To: list\n"
1864 "~u <msglist> Read in message(s) without headers (`~U': indent lines)\n"
1865 "~v Edit message via $VISUAL\n"
1866 "~w <file> Write message onto file\n"
1867 "~x Abort composition, discard message (`~q': save in $DEAD)\n"
1868 "~| <command> Pipe message through shell filter\n"
1875 /* Finally place an entry in history as applicable */
1881 if(n_psonce
& n_PSO_INTERACTIVE
)
1882 n_tty_addhist(linebuf
, TRU1
);
1888 /* Do we have *on-compose-done-shell*, or *on-compose-done*?
1889 * TODO Usual f...ed up state of signals and terminal etc. */
1890 if(coap
== NULL
&& (cp
= ok_vlook(on_compose_done_shell
)) != NULL
) Jocds
:{
1891 union {int (*ptf
)(void); char const *sh
;} u
;
1894 /* Reset *escape* to be available and guaranteed! */
1898 u
.ptf
= &a_coll_ocds__mac
;
1900 a_coll_ocds__macname
= cp
= coapm
;
1902 u
.sh
= ok_vlook(SHELL
);
1907 coap
= n_lofi_alloc(n_VSTRUCT_SIZEOF(struct a_coll_ocds_arg
, coa_cmd
1909 coap
->coa_pipe
[0] = coap
->coa_pipe
[1] = -1;
1910 coap
->coa_stdin
= coap
->coa_stdout
= NULL
;
1911 coap
->coa_senderr
= checkaddr_err
;
1912 memcpy(coap
->coa_cmd
, cp
, i
);
1915 coap
->coa_opipe
= safe_signal(SIGPIPE
, SIG_IGN
);
1916 coap
->coa_oint
= safe_signal(SIGINT
, SIG_IGN
);
1919 if(pipe_cloexec(coap
->coa_pipe
) != -1 &&
1920 (coap
->coa_stdin
= Fdopen(coap
->coa_pipe
[0], "r", FAL0
)) != NULL
&&
1921 (coap
->coa_stdout
= Popen(cmd
, "W", u
.sh
, NULL
, coap
->coa_pipe
[1])
1923 close(coap
->coa_pipe
[1]);
1924 coap
->coa_pipe
[1] = -1;
1926 temporary_compose_mode_hook_call(NULL
, NULL
, NULL
);
1927 n_source_slice_hack(coap
->coa_cmd
, coap
->coa_stdin
, coap
->coa_stdout
,
1928 (n_psonce
& ~(n_PSO_INTERACTIVE
| n_PSO_TTYIN
| n_PSO_TTYOUT
)),
1929 &a_coll_ocds__finalize
, &coap
);
1930 /* Hook version protocol for ~^: update manual upon change! */
1931 fputs(a_COLL_PLUMBING_VERSION
"\n", coap
->coa_stdout
);
1932 #undef a_COLL_PLUMBING_VERSION
1937 a_coll_ocds__finalize(coap
);
1938 n_perr(_("Cannot invoke *on-compose-done(-shell)?*"), c
);
1941 if(*checkaddr_err
!= 0){
1945 if(coapm
== NULL
&& (coapm
= ok_vlook(on_compose_done
)) != NULL
)
1947 escape
= escape_saved
;
1949 /* Final chance to edit headers, if not already above */
1950 if (ok_blook(bsdcompat
) || ok_blook(askatend
)) {
1951 if (hp
->h_cc
== NULL
&& ok_blook(askcc
))
1952 grab_headers(n_LEXINPUT_CTX_COMPOSE
, hp
, GCC
, 1);
1953 if (hp
->h_bcc
== NULL
&& ok_blook(askbcc
))
1954 grab_headers(n_LEXINPUT_CTX_COMPOSE
, hp
, GBCC
, 1);
1956 if (hp
->h_attach
== NULL
&& ok_blook(askattach
))
1957 hp
->h_attach
= n_attachment_list_edit(hp
->h_attach
,
1958 n_LEXINPUT_CTX_COMPOSE
);
1960 /* Execute compose-leave */
1961 if((cp
= ok_vlook(on_compose_leave
)) != NULL
){
1962 setup_from_and_sender(hp
);
1963 temporary_compose_mode_hook_call(cp
, &a_coll__hook_setter
, hp
);
1966 /* Add automatic receivers */
1967 if ((cp
= ok_vlook(autocc
)) != NULL
&& *cp
!= '\0')
1968 hp
->h_cc
= cat(hp
->h_cc
, checkaddrs(lextract(cp
, GCC
| GFULL
),
1969 EACM_NORMAL
, checkaddr_err
));
1970 if ((cp
= ok_vlook(autobcc
)) != NULL
&& *cp
!= '\0')
1971 hp
->h_bcc
= cat(hp
->h_bcc
, checkaddrs(lextract(cp
, GBCC
| GFULL
),
1972 EACM_NORMAL
, checkaddr_err
));
1973 if (*checkaddr_err
!= 0)
1976 /* TODO Cannot do since it may require turning this into a multipart one */
1977 if(n_poption
& n_PO_Mm_FLAG
)
1980 /* Place signature? */
1981 if((cp
= ok_vlook(signature
)) != NULL
&& *cp
!= '\0'){
1984 if((cpq
= fexpand(cp
, FEXP_LOCAL
| FEXP_NOPROTO
)) == NULL
){
1985 n_err(_("*signature* expands to invalid file: %s\n"),
1986 n_shexp_quote_cp(cp
, FAL0
));
1989 cpq
= n_shexp_quote_cp(cp
= cpq
, FAL0
);
1991 if((sigfp
= Fopen(cp
, "r")) == NULL
){
1992 n_err(_("Can't open *signature* %s: %s\n"), cpq
, strerror(errno
));
1997 linebuf
= smalloc(linesize
= LINESIZE
);
1999 while((i
= fread(linebuf
, sizeof *linebuf
, linesize
, n_UNVOLATILE(sigfp
)))
2002 if(i
!= fwrite(linebuf
, sizeof *linebuf
, i
, _coll_fp
))
2007 FILE *x
= n_UNVOLATILE(sigfp
);
2008 int e
= errno
, ise
= ferror(x
);
2014 n_err(_("Errors while reading *signature* %s: %s\n"),
2020 if(c
!= '\0' && c
!= '\n')
2021 putc('\n', _coll_fp
);
2024 { char const *cp_obsolete
= ok_vlook(NAIL_TAIL
);
2026 if(cp_obsolete
!= NULL
)
2027 n_OBSOLETE(_("please use *message-inject-tail*, not *NAIL_TAIL*"));
2029 if((cp
= ok_vlook(message_inject_tail
)) != NULL
||
2030 (cp
= cp_obsolete
) != NULL
){
2031 if(putesc(cp
, _coll_fp
) < 0)
2033 if((n_psonce
& n_PSO_INTERACTIVE
) && putesc(cp
, n_stdout
) < 0)
2039 if(fflush(_coll_fp
))
2044 if (linebuf
!= NULL
)
2047 sigprocmask(SIG_BLOCK
, &nset
, NULL
);
2048 n_pstate
&= ~n_PS_COMPOSE_MODE
;
2049 safe_signal(SIGINT
, _coll_saveint
);
2050 safe_signal(SIGHUP
, _coll_savehup
);
2051 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
2056 if(coap
!= NULL
&& coap
!= (struct a_coll_ocds_arg
*)-1)
2057 n_source_slice_hack_remove_after_jump();
2059 Fclose(n_UNVOLATILE(sigfp
));
2060 if (_coll_fp
!= NULL
) {
2064 assert(checkaddr_err
!= NULL
);
2065 /* TODO We don't save in $DEAD upon error because msg not readily composed?
2066 * TODO But this no good, it should go ZOMBIE / DRAFT / POSTPONED or what! */
2067 if(*checkaddr_err
!= 0)
2068 n_err(_("Some addressees were classified as \"hard error\"\n"));
2069 else if(_coll_hadintr
== 0){
2070 *checkaddr_err
= TRU1
; /* TODO ugly: "sendout_error" now.. */
2071 n_err(_("Failed to prepare composed message (I/O error, disk full?)\n"));