Bump S-nail v14.9.0-pre4, 2017-04-13
[s-mailx.git] / collect.c
bloba3a36c60770c8e518971a9738947b774faa8315c
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>.
7 */
8 /*
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
14 * are met:
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
34 * SUCH DAMAGE.
36 #undef n_FILE
37 #define n_FILE collect
39 #ifndef HAVE_AMALGAMATION
40 # include "nail.h"
41 #endif
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,
66 size_t linesize);
68 /* */
69 static bool_t a_coll_include_file(char const *name, bool_t indent,
70 bool_t writestat);
72 /* Execute cmd and insert its standard output into fp */
73 static bool_t a_coll_insert_cmd(FILE *fp, char const *cmd);
75 /* ~p command */
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
86 * new temp file */
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);
99 /* ~^ mode */
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);
117 static void
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 */
129 struct n_sigman sm;
130 struct attachment *ap;
131 char * volatile mnbuf;
132 NYD_ENTER;
134 n_UNUSED(linesize);
135 mnbuf = NULL;
137 n_SIGMAN_ENTER_SWITCH(&sm, n_SIGMAN_ALL){
138 case 0:
139 break;
140 default:
141 goto jleave;
144 /* If the above todo is worked, remove or outsource to attachment.c! */
145 if(hp != NULL && (ap = hp->h_attach) != NULL) do
146 if(ap->a_msgno){
147 mnbuf = sstrdup(mailname);
148 break;
150 while((ap = ap->a_flink) != NULL);
152 n_go_command(n_GO_INPUT_CTX_COMPOSE, linebuf);
154 n_sigman_cleanup_ping(&sm);
155 jleave:
156 if(mnbuf != NULL){
157 if(strcmp(mnbuf, mailname))
158 n_err(_("Mailbox changed: it is likely that existing "
159 "rfc822 attachments became invalid!\n"));
160 free(mnbuf);
162 NYD_LEAVE;
163 n_sigman_leave(&sm, n_SIGMAN_VIPSIGS_NTTYOUT);
166 static bool_t
167 a_coll_include_file(char const *name, bool_t indent, bool_t writestat){
168 FILE *fbuf;
169 char const *heredb, *indb;
170 char *linebuf;
171 size_t linesize, heredl, indl, cnt, linelen;
172 si64_t lc, cc;
173 bool_t rv;
174 NYD_ENTER;
176 rv = FAL0;
177 lc = cc = 0;
178 linebuf = 0; /* TODO line pool */
179 linesize = 0;
180 heredb = NULL;
181 heredl = 0;
183 /* The -M case is special */
184 if(name == (char*)-1){
185 fbuf = n_stdin;
186 name = "-";
187 }else if(name[0] == '-' &&
188 (name[1] == '\0' || blankspacechar(name[1]))){
189 fbuf = n_stdin;
190 if(name[1] == '\0'){
191 if(!(n_psonce & n_PSO_INTERACTIVE)){
192 n_err(_("~< -: HERE-delimiter required in non-interactive mode\n"));
193 goto jleave;
195 }else{
196 for(heredb = &name[2]; *heredb != '\0' && blankspacechar(*heredb);
197 ++heredb)
199 if((heredl = strlen(heredb)) == 0){
200 n_err(_("~< - HERE-delimiter: delimiter must not be empty\n"));
201 goto jleave;
204 name = "-";
205 }else if((fbuf = Fopen(name, "r")) == NULL){
206 n_perr(name, 0);
207 goto jleave;
210 if(!indent)
211 indl = 0;
212 else{
213 if((indb = ok_vlook(indentprefix)) == NULL)
214 indb = INDENT_DEFAULT;
215 indl = strlen(indb);
218 if(fbuf != n_stdin)
219 cnt = fsize(fbuf);
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)){
224 heredb = NULL;
225 break;
228 if(indl > 0){
229 if(fwrite(indb, sizeof *indb, indl, _coll_fp) != indl)
230 goto jleave;
231 cc += indl;
234 if(fwrite(linebuf, sizeof *linebuf, linelen, _coll_fp) != linelen)
235 goto jleave;
236 cc += linelen;
237 ++lc;
239 if(fflush(_coll_fp))
240 goto jleave;
242 if(heredb == NULL)
243 rv = TRU1;
244 jleave:
245 if(linebuf != NULL)
246 free(linebuf);
247 if(fbuf != NULL){
248 if(fbuf != n_stdin)
249 Fclose(fbuf);
250 else if(heredl > 0)
251 clearerr(n_stdin);
254 if(writestat)
255 fprintf(n_stdout, "%s%s %" PRId64 "/%" PRId64 "\n",
256 n_shexp_quote_cp(name, FAL0), (rv ? n_empty : " " n_ERROR), lc, cc);
257 NYD_LEAVE;
258 return rv;
261 static bool_t
262 a_coll_insert_cmd(FILE *fp, char const *cmd){
263 bool_t rv;
264 FILE *ibuf;
265 si64_t lc, cc;
266 NYD_ENTER;
268 lc = cc = 0;
270 if((ibuf = Popen(cmd, "r", ok_vlook(SHELL), NULL, 0)) != NULL){
271 int c;
273 rv = TRU1;
274 while((c = getc(ibuf)) != EOF){ /* XXX bytewise, yuck! */
275 if(putc(c, fp) == EOF){
276 rv = FAL0;
277 break;
279 ++cc;
280 if(c == '\n')
281 ++lc;
283 if(!feof(ibuf) || ferror(ibuf))
284 rv = FAL0;
285 if(!Pclose(ibuf, TRU1))
286 rv = FAL0;
287 }else{
288 n_perr(cmd, 0);
289 rv = FAL0;
292 fprintf(n_stdout, "CMD%s %" PRId64 "/%" PRId64 "\n",
293 (rv ? n_empty : " " n_ERROR), lc, cc);
294 NYD_LEAVE;
295 return rv;
298 static void
299 print_collf(FILE *cf, struct header *hp)
301 char *lbuf;
302 FILE *obuf;
303 size_t cnt, linesize, linelen;
304 NYD_ENTER;
306 fflush_rewind(cf);
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);
311 goto jleave;
314 hold_all_sigs();
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);
321 lbuf = NULL;
322 linesize = 0;
323 while(fgetline(&lbuf, &linesize, &cnt, &linelen, cf, 1))
324 prout(lbuf, linelen, obuf);
325 if(lbuf != NULL)
326 free(lbuf);
328 if(hp->h_attach != NULL){
329 fputs(_("-------\nAttachments:\n"), obuf);
330 n_attachment_list_print(hp->h_attach, obuf);
333 rele_all_sigs();
335 page_or_print(obuf, 0);
336 jleave:
337 Fclose(obuf);
338 NYD_LEAVE;
341 static int
342 exwrite(char const *name, FILE *fp, int f)
344 FILE *of;
345 int c, rv;
346 si64_t lc, cc;
347 NYD_ENTER;
349 if(f) {
350 fprintf(n_stdout, "%s ", n_shexp_quote_cp(name, FAL0));
351 fflush(n_stdout);
354 if ((of = Fopen(name, "a")) == NULL) {
355 n_perr(name, 0);
356 goto jerr;
359 lc = cc = 0;
360 while ((c = getc(fp)) != EOF) {
361 ++cc;
362 if (c == '\n')
363 ++lc;
364 if (putc(c, of) == EOF) {
365 n_perr(name, 0);
366 goto jerr;
369 fprintf(n_stdout, _("%" PRId64 "/%" PRId64 "\n"), lc, cc);
371 rv = 0;
372 jleave:
373 if(of != NULL)
374 Fclose(of);
375 fflush(n_stdout);
376 NYD_LEAVE;
377 return rv;
378 jerr:
379 putc('-', n_stdout);
380 putc('\n', n_stdout);
381 rv = -1;
382 goto jleave;
385 static enum okay
386 makeheader(FILE *fp, struct header *hp, si8_t *checkaddr_err)
388 FILE *nf;
389 int c;
390 enum okay rv = STOP;
391 NYD_ENTER;
393 if ((nf = Ftmp(NULL, "colhead", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==NULL) {
394 n_perr(_("temporary mail edit file"), 0);
395 goto jleave;
398 extract_header(fp, hp, checkaddr_err);
399 if (checkaddr_err != NULL && *checkaddr_err != 0)
400 goto jleave;
402 while ((c = getc(fp)) != EOF) /* XXX bytewise, yuck! */
403 putc(c, nf);
404 if (fp != _coll_fp)
405 Fclose(_coll_fp);
406 Fclose(fp);
407 _coll_fp = nf;
408 if (check_from_and_sender(hp->h_from, hp->h_sender) == NULL)
409 goto jleave;
410 rv = OKAY;
411 jleave:
412 NYD_LEAVE;
413 return rv;
416 static void
417 mesedit(int c, struct header *hp)
419 bool_t saved;
420 sighandler_type sigint;
421 FILE *nf;
422 NYD_ENTER;
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);
429 if (nf != NULL) {
430 if (hp) {
431 rewind(nf);
432 makeheader(nf, hp, NULL);
433 } else {
434 fseek(nf, 0L, SEEK_END);
435 Fclose(_coll_fp);
436 _coll_fp = nf;
439 safe_signal(SIGINT, sigint);
441 if(!saved)
442 ok_bclear(add_file_recipients);
443 NYD_LEAVE;
446 static void
447 mespipe(char *cmd)
449 FILE *nf;
450 sighandler_type sigint;
451 NYD_ENTER;
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);
457 goto jout;
460 /* stdin = current message. stdout = new message */
461 fflush(_coll_fp);
462 if (n_child_run(ok_vlook(SHELL), 0, fileno(_coll_fp), fileno(nf), "-c",
463 cmd, NULL, NULL) < 0) {
464 Fclose(nf);
465 goto jout;
468 if (fsize(nf) == 0) {
469 n_err(_("No bytes from %s !?\n"), n_shexp_quote_cp(cmd, FAL0));
470 Fclose(nf);
471 goto jout;
474 /* Take new files */
475 fseek(nf, 0L, SEEK_END);
476 Fclose(_coll_fp);
477 _coll_fp = nf;
478 jout:
479 safe_signal(SIGINT, sigint);
480 NYD_LEAVE;
483 static int
484 forward(char *ms, FILE *fp, int f)
486 int *msgvec, rv = 0;
487 struct n_ignore const *itp;
488 char const *tabst;
489 enum sendaction action;
490 NYD_ENTER;
492 msgvec = salloc((size_t)(msgCount + 1) * sizeof *msgvec);
493 if (getmsglist(ms, msgvec, 0) < 0)
494 goto jleave;
495 if (*msgvec == 0) {
496 *msgvec = first(0, MMNORM);
497 if (*msgvec == 0) {
498 n_err(_("No appropriate messages\n"));
499 goto jleave;
501 msgvec[1] = 0;
504 if (f == 'f' || f == 'F' || f == 'u')
505 tabst = NULL;
506 else if ((tabst = ok_vlook(indentprefix)) == NULL)
507 tabst = INDENT_DEFAULT;
508 if (f == 'u' || f == 'U')
509 itp = n_IGNORE_ALL;
510 else
511 itp = upperchar(f) ? NULL : n_IGNORE_TYPE;
512 action = (upperchar(f) && f != 'U') ? SEND_QUOTE_ALL : SEND_QUOTE;
514 fprintf(n_stdout, _("Interpolating:"));
515 srelax_hold();
516 for (; *msgvec != 0; ++msgvec) {
517 struct message *mp = message + *msgvec - 1;
519 touch(mp);
520 fprintf(n_stdout, " %d", *msgvec);
521 fflush(n_stdout);
522 if (sendmp(mp, fp, itp, tabst, action, NULL) < 0) {
523 n_perr(_("temporary mail file"), 0);
524 rv = -1;
525 break;
527 srelax();
529 srelax_rele();
530 fprintf(n_stdout, "\n");
531 jleave:
532 NYD_LEAVE;
533 return rv;
536 static bool_t
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];
542 NYD2_ENTER;
544 /* Protcol version for *on-compose-splice** -- update manual on change! */
545 #define a_COLL_PLUMBING_VERSION "0 0 1"
546 cp = ms;
548 /* C99 */{
549 size_t i;
551 for(i = 0; i < n_NELEM(cmd); ++i){
552 while(blankchar(*cp))
553 ++cp;
554 if(*cp == '\0')
555 cmd[i] = NULL;
556 else{
557 if(i < n_NELEM(cmd) - 1)
558 for(cmd[i] = cp++; *cp != '\0' && !blankchar(*cp); ++cp)
560 else{
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]))
565 --cp;
567 cmd[i] = savestrbuf(cmd[i], PTR2SIZE(cp - cmd[i]));
572 if(n_UNLIKELY(cmd[0] == NULL))
573 goto jecmd;
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")){
580 if(cmd[2] == NULL){
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){
594 putc(' ', n_stdout);
595 fputs(&hfp->hf_dat[0], n_stdout);
597 putc('\n', n_stdout);
598 goto jleave;
601 if(cmd[3] != NULL)
602 goto jecmd;
604 if(!asccasecmp(cmd[2], "from")){
605 np = hp->h_from;
606 jlist:
607 fprintf(n_stdout, "%s %s\n", (np == NULL ? "501" : "210"), cp);
608 goto jleave;
610 if(!asccasecmp(cmd[2], cp = "Sender")){
611 np = hp->h_sender;
612 goto jlist;
614 if(!asccasecmp(cmd[2], cp = "To")){
615 np = hp->h_to;
616 goto jlist;
618 if(!asccasecmp(cmd[2], cp = "Cc")){
619 np = hp->h_cc;
620 goto jlist;
622 if(!asccasecmp(cmd[2], cp = "Bcc")){
623 np = hp->h_bcc;
624 goto jlist;
626 if(!asccasecmp(cmd[2], cp = "Subject")){
627 np = (struct name*)-1;
628 goto jlist;
630 if(!asccasecmp(cmd[2], cp = "Reply-To")){
631 np = hp->h_replyto;
632 goto jlist;
634 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
635 np = hp->h_mft;
636 goto jlist;
638 if(!asccasecmp(cmd[2], cp = "Message-ID")){
639 np = hp->h_message_id;
640 goto jlist;
642 if(!asccasecmp(cmd[2], cp = "References")){
643 np = hp->h_ref;
644 goto jlist;
646 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
647 np = hp->h_in_reply_to;
648 goto jlist;
651 /* Primitive name normalization XXX header object should
652 * XXX have a more sophisticated accessible one */
653 /* C99 */{
654 char *xp;
656 cp = xp = savestr(cmd[2]);
657 xp[0] = upperchar(xp[0]);
658 while(*++xp != '\0')
659 xp[0] = lowerchar(xp[0]);
661 for(hfp = hp->h_user_headers;; hfp = hfp->hf_next){
662 if(hfp == NULL)
663 goto j501cp;
664 else if(!asccasecmp(cp, &hfp->hf_dat[0])){
665 fprintf(n_stdout, "210 %s\n", cp);
666 break;
670 goto jleave;
673 if(is_asccaseprefix(cmd[1], "show")){
674 if(cmd[2] == NULL || cmd[3] != NULL)
675 goto jecmd;
677 if(!asccasecmp(cmd[2], "from")){
678 np = hp->h_from;
679 jshow:
680 if(np != NULL){
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);
686 goto jleave;
687 }else
688 goto j501cp;
690 if(!asccasecmp(cmd[2], cp = "Sender")){
691 np = hp->h_sender;
692 goto jshow;
694 if(!asccasecmp(cmd[2], cp = "To")){
695 np = hp->h_to;
696 goto jshow;
698 if(!asccasecmp(cmd[2], cp = "Cc")){
699 np = hp->h_cc;
700 goto jshow;
702 if(!asccasecmp(cmd[2], cp = "Bcc")){
703 np = hp->h_bcc;
704 goto jshow;
706 if(!asccasecmp(cmd[2], cp = "Reply-To")){
707 np = hp->h_replyto;
708 goto jshow;
710 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
711 np = hp->h_mft;
712 goto jshow;
714 if(!asccasecmp(cmd[2], cp = "Message-ID")){
715 np = hp->h_message_id;
716 goto jshow;
718 if(!asccasecmp(cmd[2], cp = "References")){
719 np = hp->h_ref;
720 goto jshow;
722 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
723 np = hp->h_in_reply_to;
724 goto jshow;
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);
730 else
731 fprintf(n_stdout, "501 %s\n", cp);
732 goto jleave;
735 /* Primitive name normalization XXX header object should
736 * XXX have a more sophisticated accessible one */
737 /* C99 */{
738 bool_t any;
739 char *xp;
741 cp = xp = savestr(cmd[2]);
742 xp[0] = upperchar(xp[0]);
743 while(*++xp != '\0')
744 xp[0] = lowerchar(xp[0]);
746 for(any = FAL0, hfp = hp->h_user_headers; hfp != NULL;
747 hfp = hfp->hf_next){
748 if(!asccasecmp(cp, &hfp->hf_dat[0])){
749 if(!any)
750 fprintf(n_stdout, "212 %s\n", cp);
751 any = TRU1;
752 fprintf(n_stdout, "%s\n", &hfp->hf_dat[hfp->hf_nl +1]);
755 if(any)
756 putc('\n', n_stdout);
757 else
758 goto j501cp;
760 goto jleave;
763 if(is_asccaseprefix(cmd[1], "remove")){
764 if(cmd[2] == NULL || cmd[3] != NULL)
765 goto jecmd;
767 if(!asccasecmp(cmd[2], "from")){
768 npp = &hp->h_from;
769 jrem:
770 if(*npp != NULL){
771 *npp = NULL;
772 fprintf(n_stdout, "210 %s\n", cp);
773 goto jleave;
774 }else
775 goto j501cp;
777 if(!asccasecmp(cmd[2], cp = "Sender")){
778 npp = &hp->h_sender;
779 goto jrem;
781 if(!asccasecmp(cmd[2], cp = "To")){
782 npp = &hp->h_to;
783 goto jrem;
785 if(!asccasecmp(cmd[2], cp = "Cc")){
786 npp = &hp->h_cc;
787 goto jrem;
789 if(!asccasecmp(cmd[2], cp = "Bcc")){
790 npp = &hp->h_bcc;
791 goto jrem;
793 if(!asccasecmp(cmd[2], cp = "Reply-To")){
794 npp = &hp->h_replyto;
795 goto jrem;
797 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
798 npp = &hp->h_mft;
799 goto jrem;
801 if(!asccasecmp(cmd[2], cp = "Message-ID")){
802 npp = &hp->h_message_id;
803 goto jrem;
805 if(!asccasecmp(cmd[2], cp = "References")){
806 npp = &hp->h_ref;
807 goto jrem;
809 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
810 npp = &hp->h_in_reply_to;
811 goto jrem;
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);
818 goto jleave;
819 }else
820 goto j501cp;
823 /* Primitive name normalization XXX header object should
824 * XXX have a more sophisticated accessible one */
825 /* C99 */{
826 struct n_header_field **hfpp;
827 bool_t any;
828 char *xp;
830 cp = xp = savestr(cmd[2]);
831 xp[0] = upperchar(xp[0]);
832 while(*++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;
838 if(!any)
839 fprintf(n_stdout, "210 %s\n", cp);
840 any = TRU1;
841 }else
842 hfp = *(hfpp = &hfp->hf_next);
844 if(!any)
845 goto j501cp;
847 goto jleave;
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 */
855 si8_t aerr;
856 enum expand_addr_check_mode eacm;
857 enum gfield ntype;
858 bool_t mult_ok;
860 if(cmd[2] == NULL || cmd[3] == NULL)
861 goto jecmd;
863 /* Strip [\r\n] which would render a body invalid XXX all controls? */
864 /* C99 */{
865 char *xp, c;
867 cmd[3] = xp = savestr(cmd[3]);
868 for(; (c = *xp) != '\0'; ++xp)
869 if(c == '\n' || c == '\r')
870 *xp = ' ';
873 mult_ok = TRU1;
874 ntype = GEXTRA | GFULL | GFULLEXTRA;
875 eacm = EACM_STRICT;
877 if(!asccasecmp(cmd[2], "from")){
878 npp = &hp->h_from;
879 jins:
880 aerr = 0;
881 if((np = lextract(cmd[3], ntype)) == NULL)
882 goto j501cp;
884 if((np = checkaddrs(np, eacm, &aerr), aerr != 0)){
885 fprintf(n_stdout, "505 %s\n", cp);
886 goto jleave;
889 if(!mult_ok){
890 struct name *xnp;
892 for(xnp = *npp; xnp != NULL && !(xnp->n_type & GDEL);
893 xnp = xnp->n_flink)
895 if(xnp != NULL || np->n_flink != NULL){
896 fprintf(n_stdout, "506 %s\n", cp);
897 goto jleave;
901 *npp = cat(*npp, np);
902 fprintf(n_stdout, "210 %s\n", cp);
903 goto jleave;
905 if(!asccasecmp(cmd[2], cp = "Sender")){
906 mult_ok = FAL0;
907 npp = &hp->h_sender;
908 goto jins;
910 if(!asccasecmp(cmd[2], cp = "To")){
911 npp = &hp->h_to;
912 ntype = GTO | GFULL;
913 eacm = EACM_NORMAL | EAF_NAME;
914 goto jins;
916 if(!asccasecmp(cmd[2], cp = "Cc")){
917 npp = &hp->h_cc;
918 ntype = GCC | GFULL;
919 eacm = EACM_NORMAL | EAF_NAME;
920 goto jins;
922 if(!asccasecmp(cmd[2], cp = "Bcc")){
923 npp = &hp->h_bcc;
924 ntype = GBCC | GFULL;
925 eacm = EACM_NORMAL | EAF_NAME;
926 goto jins;
928 if(!asccasecmp(cmd[2], cp = "Reply-To")){
929 npp = &hp->h_replyto;
930 goto jins;
932 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
933 npp = &hp->h_mft;
934 eacm = EACM_NONAME;
935 goto jins;
937 if(!asccasecmp(cmd[2], cp = "Message-ID")){
938 mult_ok = FAL0;
939 npp = &hp->h_message_id;
940 eacm = EACM_NONAME;
941 goto jins;
943 if(!asccasecmp(cmd[2], cp = "References")){
944 npp = &hp->h_ref;
945 ntype = GREF;
946 eacm = EACM_NONAME;
947 goto jins;
949 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
950 npp = &hp->h_in_reply_to;
951 ntype = GREF;
952 eacm = EACM_NONAME;
953 goto jins;
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]);
960 else
961 hp->h_subject = n_UNCONST(cmd[3]);
962 fprintf(n_stdout, "210 %s\n", cp);
963 goto jleave;
964 }else
965 goto j501cp;
968 /* Primitive name normalization XXX header object should
969 * XXX have a more sophisticated accessible one */
970 /* C99 */{
971 size_t nl, bl;
972 struct n_header_field **hfpp;
974 for(cp = cmd[2]; *cp != '\0'; ++cp)
975 if(!fieldnamechar(*cp)){
976 cp = cmd[2];
977 goto j501cp;
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
986 ) + nl +1 + bl);
987 hfp->hf_next = NULL;
988 hfp->hf_nl = nl;
989 hfp->hf_bl = bl - 1;
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);
995 goto jleave;
998 goto jecmd;
1001 if(is_asccaseprefix(cmd[0], "attachment")){
1002 bool_t status;
1003 struct attachment *ap;
1005 if(cmd[1] == NULL || is_asccaseprefix(cmd[1], "list")){
1006 if(cmd[2] != NULL)
1007 goto jecmd;
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);
1015 }else
1016 fputs("501\n", n_stdout);
1017 goto jleave;
1020 if(is_asccaseprefix(cmd[1], "remove")){
1021 if(cmd[2] == NULL || cmd[3] != NULL)
1022 goto jecmd;
1024 if((ap = n_attachment_find(hp->h_attach, cmd[2], &status)) != NULL){
1025 if(status == TRUM1)
1026 fputs("506\n", n_stdout);
1027 else{
1028 hp->h_attach = n_attachment_remove(hp->h_attach, ap);
1029 fprintf(n_stdout, "210 %s\n", cmd[2]);
1031 }else
1032 fputs("501\n", n_stdout);
1033 goto jleave;
1036 if(is_asccaseprefix(cmd[1], "remove-at")){
1037 uiz_t i;
1039 if(cmd[2] == NULL || cmd[3] != NULL)
1040 goto jecmd;
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);
1046 else{
1047 for(ap = hp->h_attach; ap != NULL && --i != 0; ap = ap->a_flink)
1049 if(ap != NULL){
1050 hp->h_attach = n_attachment_remove(hp->h_attach, ap);
1051 fprintf(n_stdout, "210 %s\n", cmd[2]);
1052 }else
1053 fputs("501\n", n_stdout);
1055 goto jleave;
1058 if(is_asccaseprefix(cmd[1], "insert")){
1059 enum n_attach_error aerr;
1061 if(cmd[2] == NULL || cmd[3] != NULL)
1062 goto jecmd;
1064 hp->h_attach = n_attachment_append(hp->h_attach, cmd[2], &aerr, &ap);
1065 switch(aerr){
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:
1070 default:
1071 cp = "501\n";
1072 jatt_ins:
1073 fputs(cp, n_stdout);
1074 break;
1075 case n_ATTACH_ERR_NONE:{
1076 size_t i;
1078 for(i = 0; ap != NULL; ++i, ap = ap->a_blink)
1080 fprintf(n_stdout, "210 %" PRIuZ "\n", i);
1081 } break;
1083 goto jleave;
1086 if(is_asccaseprefix(cmd[1], "attribute")){
1087 if(cmd[2] == NULL || cmd[3] != NULL)
1088 goto jecmd;
1090 if((ap = n_attachment_find(hp->h_attach, cmd[2], NULL)) != NULL){
1091 jatt_att:
1092 fprintf(n_stdout, "212 %s\n", cmd[2]);
1093 if(ap->a_msgno > 0)
1094 fprintf(n_stdout, "message-number %d\n\n", ap->a_msgno);
1095 else{
1096 fprintf(n_stdout,
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);
1112 }else
1113 fputs("501\n", n_stdout);
1114 goto jleave;
1117 if(is_asccaseprefix(cmd[1], "attribute-at")){
1118 uiz_t i;
1120 if(cmd[2] == NULL || cmd[3] != NULL)
1121 goto jecmd;
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);
1127 else{
1128 for(ap = hp->h_attach; ap != NULL && --i != 0; ap = ap->a_flink)
1130 if(ap != NULL)
1131 goto jatt_att;
1132 else
1133 fputs("501\n", n_stdout);
1135 goto jleave;
1138 if(is_asccaseprefix(cmd[1], "attribute-set")){
1139 if(cmd[2] == NULL || cmd[3] == NULL)
1140 goto jecmd;
1142 if((ap = n_attachment_find(hp->h_attach, cmd[2], NULL)) != NULL){
1143 jatt_attset:
1144 if(ap->a_msgno > 0)
1145 fputs("505\n", n_stdout);
1146 else{
1147 char c, *keyw;
1149 cp = cmd[3];
1150 while((c = *cp) != '\0' && !blankchar(c))
1151 ++cp;
1152 keyw = savestrbuf(cmd[3], PTR2SIZE(cp - cmd[3]));
1153 if(c != '\0'){
1154 for(; (c = *++cp) != '\0' && blankchar(c);)
1156 if(c != '\0'){
1157 char *xp;
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')
1164 *xp = ' ';
1165 c = *cp;
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;
1176 if(c != '\0'){
1177 struct name *np;
1179 np = checkaddrs(lextract(cp, GREF),
1180 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG |
1181 EACM_NONAME, NULL);
1182 if(np != NULL && np->n_flink == NULL)
1183 ap->a_content_id = np;
1184 else
1185 cp = NULL;
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;
1191 else
1192 cp = NULL;
1194 if(cp != NULL){
1195 size_t i;
1197 for(i = 0; ap != NULL; ++i, ap = ap->a_blink)
1199 fprintf(n_stdout, "210 %" PRIuZ "\n", i);
1200 }else
1201 fputs("505\n", n_stdout);
1203 }else
1204 fputs("501\n", n_stdout);
1205 goto jleave;
1208 if(is_asccaseprefix(cmd[1], "attribute-set-at")){
1209 uiz_t i;
1211 if(cmd[2] == NULL || cmd[3] == NULL)
1212 goto jecmd;
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);
1218 else{
1219 for(ap = hp->h_attach; ap != NULL && --i != 0; ap = ap->a_flink)
1221 if(ap != NULL)
1222 goto jatt_attset;
1223 else
1224 fputs("501\n", n_stdout);
1226 goto jleave;
1229 goto jecmd;
1232 jecmd:
1233 fputs("500\n", n_stdout);
1234 ms = NULL;
1235 jleave:
1236 fflush(n_stdout);
1237 NYD2_LEAVE;
1238 return (ms != NULL);
1240 j501cp:
1241 fputs("501 ", n_stdout);
1242 fputs(cp, n_stdout);
1243 putc('\n', n_stdout);
1244 goto jleave;
1247 static void
1248 _collint(int s)
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);
1256 fflush(n_stdout);
1257 clearerr(n_stdin);
1258 } else
1259 _coll_hadintr = 1;
1260 siglongjmp(_coll_jmp, 1);
1262 n_exit_status |= n_EXIT_SEND_ERROR;
1263 if (s != 0)
1264 savedeadletter(_coll_fp, TRU1);
1265 /* Aborting message, no need to fflush() .. */
1266 siglongjmp(_coll_abort, 1);
1269 static void
1270 collhup(int s)
1272 NYD_X; /* Signal handler */
1273 n_UNUSED(s);
1275 savedeadletter(_coll_fp, TRU1);
1276 /* Let's pretend nobody else wants to clean up, a true statement at
1277 * this time */
1278 exit(n_EXIT_ERR);
1281 static int
1282 putesc(char const *s, FILE *stream)
1284 int n = 0, rv = -1;
1285 NYD_ENTER;
1287 while (s[0] != '\0') {
1288 if (s[0] == '\\') {
1289 if (s[1] == 't') {
1290 if (putc('\t', stream) == EOF)
1291 goto jleave;
1292 ++n;
1293 s += 2;
1294 continue;
1296 if (s[1] == 'n') {
1297 if (putc('\n', stream) == EOF)
1298 goto jleave;
1299 ++n;
1300 s += 2;
1301 continue;
1304 if (putc(s[0], stream) == EOF)
1305 goto jleave;
1306 ++n;
1307 ++s;
1309 if (putc('\n', stream) == EOF)
1310 goto jleave;
1311 rv = ++n;
1312 jleave:
1313 NYD_LEAVE;
1314 return rv;
1317 static void
1318 a_coll__hook_setter(void *arg){ /* TODO v15: drop */
1319 struct header *hp;
1320 char const *val;
1321 NYD2_ENTER;
1323 hp = arg;
1325 if((val = detract(hp->h_from, GNAMEONLY)) == NULL)
1326 val = n_empty;
1327 ok_vset(compose_from, val);
1328 if((val = detract(hp->h_sender, 0)) == NULL)
1329 val = n_empty;
1330 ok_vset(compose_sender, val);
1331 if((val = detract(hp->h_to, GNAMEONLY)) == NULL)
1332 val = n_empty;
1333 ok_vset(compose_to, val);
1334 if((val = detract(hp->h_cc, GNAMEONLY)) == NULL)
1335 val = n_empty;
1336 ok_vset(compose_cc, val);
1337 if((val = detract(hp->h_bcc, GNAMEONLY)) == NULL)
1338 val = n_empty;
1339 ok_vset(compose_bcc, val);
1340 if((val = hp->h_subject) == NULL)
1341 val = n_empty;
1342 ok_vset(compose_subject, val);
1343 NYD2_LEAVE;
1346 static int
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);
1353 _exit(n_EXIT_OK);
1354 /* NOTREACHED */
1355 return 0;
1358 static void
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;
1364 NYD2_ENTER;
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;
1389 n_lofi_free(coap);
1391 hold_all_sigs();
1392 safe_signal(SIGPIPE, opipe);
1393 safe_signal(SIGINT, oint);
1394 rele_all_sigs();
1395 NYD2_LEAVE;
1398 FL FILE *
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;
1404 int c;
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 */
1410 long cnt;
1411 enum sendaction action;
1412 sigset_t oset, nset;
1413 FILE * volatile sigfp;
1414 NYD_ENTER;
1416 _coll_fp = NULL;
1417 sigfp = NULL;
1418 linesize = 0;
1419 linebuf = NULL;
1420 eofcnt = 0;
1421 coapm = NULL;
1422 coap = NULL;
1424 /* Start catching signals from here, but we're still die on interrupts
1425 * until we're in the main loop */
1426 sigfillset(&nset);
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))
1434 goto jerr;
1435 if (sigsetjmp(_coll_jmp, 1))
1436 goto jerr;
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)) ==
1441 NULL) {
1442 n_perr(_("temporary mail file"), 0);
1443 goto jerr;
1446 /* If we are going to prompt for a subject, refrain from printing a newline
1447 * after the headers (since some people mind) */
1448 getfields = 0;
1449 if (!(n_poption & n_PO_t_FLAG)) {
1450 t = GTO | GSUBJECT | GCC | GNL;
1451 if (ok_blook(fullnames))
1452 t |= GCOMMA;
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;
1468 } else {
1469 n_UNINIT(t, 0);
1472 escape_saved = escape = ((cp = ok_vlook(escape)) != NULL) ? *cp : n_ESCAPE;
1473 _coll_hadintr = 0;
1475 if (!sigsetjmp(_coll_jmp, 1)) {
1476 /* Ask for some headers first, as necessary */
1477 if (getfields)
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)
1494 goto jerr;
1496 /* Quote an original message */
1497 if (mp != NULL && (doprefix || (cp = ok_vlook(quote)) != NULL)) {
1498 quoteitp = n_IGNORE_ALL;
1499 action = SEND_QUOTE;
1500 if (doprefix) {
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)
1505 goto jerr;
1506 } else if (!strcmp(cp, "noheading")) {
1507 /*EMPTY*/;
1508 } else if (!strcmp(cp, "headers")) {
1509 quoteitp = n_IGNORE_TYPE;
1510 } else if (!strcmp(cp, "allheaders")) {
1511 quoteitp = NULL;
1512 action = SEND_QUOTE_ALL;
1513 } else {
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)
1517 goto jerr;
1518 if (fprintf(_coll_fp, _(" wrote:\n\n")) < 0)
1519 goto jerr;
1522 if (fflush(_coll_fp))
1523 goto jerr;
1524 if (doprefix)
1525 cp = NULL;
1526 else if ((cp = ok_vlook(indentprefix)) == NULL)
1527 cp = INDENT_DEFAULT;
1528 if (sendmp(mp, _coll_fp, quoteitp, cp, action, NULL) < 0)
1529 goto jerr;
1533 if (quotefile != NULL) {
1534 if(!a_coll_include_file(quotefile, FAL0, FAL0))
1535 goto jerr;
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)) {
1541 if (printheaders)
1542 puthead(TRU1, hp, n_stdout, t,
1543 SEND_TODISP, CONV_NONE, NULL, NULL);
1545 rewind(_coll_fp);
1546 while ((c = getc(_coll_fp)) != EOF) /* XXX bytewise, yuck! */
1547 putc(c, n_stdout);
1548 if (fseek(_coll_fp, 0, SEEK_END))
1549 goto jerr;
1550 } else {
1551 rewind(_coll_fp);
1552 mesedit('e', hp);
1553 /* As mandated by the Mail Reference Manual, print "(continue)" */
1554 jcont:
1555 if(n_psonce & n_PSO_INTERACTIVE)
1556 fputs(_("(continue)\n"), n_stdout);
1558 fflush(n_stdout);
1560 } else {
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 */
1563 if (_coll_hadintr)
1564 n_err(_("\n(Interrupt -- one more to kill letter)\n"));
1567 /* If not under shell hook control */
1568 if(coap == NULL){
1569 /* We're done with -M or -m (because we are too simple minded) */
1570 if(n_poption & n_PO_Mm_FLAG)
1571 goto jout;
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))
1578 goto jerr;
1580 goto jout;
1584 /* The interactive collect loop */
1585 for(;;){
1586 /* C99 */{
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);
1597 if (cnt < 0) {
1598 if(coap != NULL)
1599 break;
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)
1606 goto jerr;
1607 n_poption &= ~n_PO_t_FLAG;
1608 continue;
1609 }else if((n_psonce & n_PSO_INTERACTIVE) && ok_blook(ignoreeof) &&
1610 ++eofcnt < 4){
1611 fprintf(n_stdout,
1612 _("*ignoreeof* set, use `~.' to terminate letter\n"));
1613 n_go_input_clearerr();
1614 continue;
1616 break;
1619 _coll_hadintr = 0;
1621 cp = linebuf;
1622 if(cnt == 0)
1623 goto jputnl;
1624 else if(coap == NULL){
1625 if(!(n_psonce & n_PSO_INTERACTIVE) && !(n_poption & n_PO_TILDE_FLAG))
1626 goto jputline;
1627 else if(cp[0] == '.'){
1628 if(cnt == 1 && (ok_blook(dot) || ok_blook(ignoreeof)))
1629 break;
1632 if(cp[0] != escape){
1633 jputline:
1634 if(fwrite(cp, sizeof *cp, cnt, _coll_fp) != (size_t)cnt)
1635 goto jerr;
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 */
1640 jputnl:
1641 if(n_pstate & n_PS_READLINE_NL){
1642 if(putc('\n', _coll_fp) == EOF)
1643 goto jerr;
1645 continue;
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)
1651 continue;
1652 if(c == '\0'){
1653 linebuf[2] = '\0';
1654 cnt = 2;
1655 }else{
1656 i = PTR2SIZE(cp - linebuf) - 3;
1657 memmove(&linebuf[3], cp, (cnt -= i));
1658 linebuf[2] = ' ';
1659 linebuf[cnt] = '\0';
1661 if(cnt > 0){ /* TODO v15 no more trailing WS from lex_input please */
1662 cp = &linebuf[cnt];
1664 for(;; --cp){
1665 c = cp[-1];
1666 if(!blankspacechar(c))
1667 break;
1669 ((char*)n_UNCONST(cp))[0] = '\0';
1670 cnt = PTR2SIZE(cp - linebuf);
1674 switch((c = linebuf[1])){
1675 default:
1676 /* On double escape, send a single one. Otherwise, it's an error */
1677 if(c == escape){
1678 cp = &linebuf[1];
1679 --cnt;
1680 goto jputline;
1681 }else{
1682 char buf[sizeof(n_UNIREPL)];
1684 if(asciichar(c))
1685 buf[0] = c, buf[1] = '\0';
1686 else if(n_psonce & n_PSO_UNICODE)
1687 memcpy(buf, n_unirepl, sizeof n_unirepl);
1688 else
1689 buf[0] = '?', buf[1] = '\0';
1690 n_err(_("Unknown command escape: ~%s\n"), buf);
1691 continue;
1693 jearg:
1694 n_err(_("Invalid command escape usage: %s\n"), linebuf);
1695 continue;
1696 case '!':
1697 /* Shell escape, send the balance of line to sh -c */
1698 if(cnt == 2 || coap != NULL)
1699 goto jearg;
1700 c_shell(&linebuf[3]);
1701 goto jhistcont;
1702 case ':':
1703 /* FALLTHRU */
1704 case '_':
1705 /* Escape to command mode, but be nice! *//* TODO command expansion
1706 * TODO should be handled here so that we have unique history! */
1707 if(cnt == 2)
1708 goto jearg;
1709 _execute_command(hp, &linebuf[3], cnt -= 3);
1710 break;
1711 case '.':
1712 /* Simulate end of file on input */
1713 if(cnt != 2 || coap != NULL)
1714 goto jearg;
1715 goto jout;
1716 case 'x':
1717 /* Same as 'q', but no *DEAD* saving */
1718 /* FALLTHRU */
1719 case 'q':
1720 /* Force a quit, act like an interrupt had happened */
1721 if(cnt != 2)
1722 goto jearg;
1723 ++_coll_hadintr;
1724 _collint((c == 'x') ? 0 : SIGINT);
1725 exit(n_EXIT_ERR);
1726 /*NOTREACHED*/
1727 case 'h':
1728 /* Grab a bunch of headers */
1729 if(cnt != 2)
1730 goto jearg;
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);
1736 break;
1737 case 'H':
1738 /* Grab extra headers */
1739 if(cnt != 2)
1740 goto jearg;
1742 grab_headers(n_GO_INPUT_CTX_COMPOSE, hp, GEXTRA, 0);
1743 while(check_from_and_sender(hp->h_from, hp->h_sender) == NULL);
1744 break;
1745 case 't':
1746 /* Add to the To: list */
1747 if(cnt == 2)
1748 goto jearg;
1749 hp->h_to = cat(hp->h_to,
1750 checkaddrs(lextract(&linebuf[3], GTO | GFULL), EACM_NORMAL,
1751 NULL));
1752 break;
1753 case 's':
1754 /* Set the Subject list */
1755 if(cnt == 2)
1756 goto jearg;
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]))){
1759 char *xp;
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')
1764 *xp = ' ';
1766 break;
1767 case '@':{
1768 struct attachment *aplist;
1770 /* Edit the attachment list */
1771 aplist = hp->h_attach;
1772 hp->h_attach = NULL;
1773 if(cnt != 2)
1774 hp->h_attach = n_attachment_append_list(aplist, &linebuf[3]);
1775 else
1776 hp->h_attach = n_attachment_list_edit(aplist,
1777 n_GO_INPUT_CTX_COMPOSE);
1778 } break;
1779 case 'c':
1780 /* Add to the CC list */
1781 if(cnt == 2)
1782 goto jearg;
1783 hp->h_cc = cat(hp->h_cc,
1784 checkaddrs(lextract(&linebuf[3], GCC | GFULL), EACM_NORMAL,
1785 NULL));
1786 break;
1787 case 'b':
1788 /* Add stuff to blind carbon copies list */
1789 if(cnt == 2)
1790 goto jearg;
1791 hp->h_bcc = cat(hp->h_bcc,
1792 checkaddrs(lextract(&linebuf[3], GBCC | GFULL), EACM_NORMAL,
1793 NULL));
1794 break;
1795 case 'd':
1796 if(cnt != 2)
1797 goto jearg;
1798 cp = n_getdeadletter();
1799 if(0){
1800 /*FALLTHRU*/
1801 case 'R':
1802 case 'r':
1803 case '<':
1804 /* Invoke a file: Search for the file name, then open it and copy
1805 * the contents to _coll_fp */
1806 if(cnt == 2){
1807 n_err(_("Interpolate what file?\n"));
1808 break;
1810 if(*(cp = &linebuf[3]) == '!' && c == '<'){
1811 if(!a_coll_insert_cmd(_coll_fp, ++cp)){
1812 if(ferror(_coll_fp))
1813 goto jerr;
1814 break;
1816 goto jhistcont;
1818 /* Note this also expands things like
1819 * !:vput vexpr delim random 0
1820 * !< - $delim */
1821 if((cp = fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO | FEXP_NSHELL)) == NULL)
1822 break;
1824 if(n_is_dir(cp, FAL0)){
1825 n_err(_("%s: is a directory\n"), n_shexp_quote_cp(cp, FAL0));
1826 break;
1828 if(!a_coll_include_file(cp, (c == 'R'), TRU1)){
1829 if(ferror(_coll_fp))
1830 goto jerr;
1831 break;
1833 break;
1834 case 'i':
1835 /* Insert a variable into the file */
1836 if(cnt == 2)
1837 goto jearg;
1838 if((cp = n_var_vlook(&linebuf[3], TRU1)) == NULL || *cp == '\0')
1839 break;
1840 if(putesc(cp, _coll_fp) < 0) /* TODO v15: user resp upon `set' time */
1841 goto jerr;
1842 if((n_psonce & n_PSO_INTERACTIVE) && putesc(cp, n_stdout) < 0)
1843 goto jerr;
1844 break;
1845 case 'a':
1846 case 'A':
1847 /* Insert the contents of a signature variable */
1848 if(cnt != 2)
1849 goto jearg;
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 */
1853 goto jerr;
1854 if((n_psonce & n_PSO_INTERACTIVE) && putesc(cp, n_stdout) < 0)
1855 goto jerr;
1857 break;
1858 case 'w':
1859 /* Write the message on a file */
1860 if(cnt == 2)
1861 goto jearg;
1862 if((cp = fexpand(&linebuf[3], FEXP_LOCAL | FEXP_NOPROTO)) == NULL){
1863 n_err(_("Write what file!?\n"));
1864 break;
1866 rewind(_coll_fp);
1867 if(exwrite(cp, _coll_fp, 1) < 0)
1868 goto jerr;
1869 break;
1870 case 'm':
1871 case 'M':
1872 case 'f':
1873 case 'F':
1874 case 'u':
1875 case 'U':
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 */
1879 if(cnt == 2)
1880 goto jearg;
1881 if(forward(&linebuf[3], _coll_fp, c) < 0)
1882 break;
1883 break;
1884 case 'p':
1885 /* Print current state of the message without altering anything */
1886 if(cnt != 2)
1887 goto jearg;
1888 print_collf(_coll_fp, hp);
1889 break;
1890 case '|':
1891 /* Pipe message through command. Collect output as new message */
1892 if(cnt == 2)
1893 goto jearg;
1894 rewind(_coll_fp);
1895 mespipe(&linebuf[3]);
1896 goto jhistcont;
1897 case 'v':
1898 case 'e':
1899 /* Edit the current message. 'e' -> use EDITOR, 'v' -> use VISUAL */
1900 if(cnt != 2 || coap != NULL)
1901 goto jearg;
1902 rewind(_coll_fp);
1903 mesedit(c, ok_blook(editheaders) ? hp : NULL);
1904 goto jhistcont;
1905 case '^':
1906 if(!a_collect_plumbing(&linebuf[3], hp))
1907 goto jearg;
1908 if(n_psonce & n_PSO_INTERACTIVE)
1909 break;
1910 continue;
1911 case '?':
1912 /* Last the lengthy help string. (Very ugly, but take care for
1913 * compiler supported string lengths :() */
1914 fputs(_(
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"
1924 ), n_stdout);
1925 fputs(_(
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"
1934 ), n_stdout);
1935 fputs(_(
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"
1943 ), n_stdout);
1944 if(cnt != 2)
1945 goto jearg;
1946 break;
1949 /* Finally place an entry in history as applicable */
1950 if(0){
1951 jhistcont:
1952 c = '\1';
1953 }else
1954 c = '\0';
1955 if(n_psonce & n_PSO_INTERACTIVE)
1956 n_tty_addhist(linebuf, TRU1);
1957 if(c != '\0')
1958 goto jcont;
1961 jout:
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;
1966 char const *cmd;
1968 /* Reset *escape* to be available and guaranteed! */
1969 escape = n_ESCAPE;
1971 if(coapm != NULL){
1972 u.ptf = &a_coll_ocs__mac;
1973 cmd = (char*)-1;
1974 a_coll_ocs__macname = cp = coapm;
1975 }else{
1976 u.sh = ok_vlook(SHELL);
1977 cmd = cp;
1980 i = strlen(cp) +1;
1981 coap = n_lofi_alloc(n_VSTRUCT_SIZEOF(struct a_coll_ocs_arg, coa_cmd
1982 ) + i);
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);
1988 hold_all_sigs();
1989 coap->coa_opipe = safe_signal(SIGPIPE, SIG_IGN);
1990 coap->coa_oint = safe_signal(SIGINT, SIG_IGN);
1991 rele_all_sigs();
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])
1996 ) != NULL){
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
2007 goto jcont;
2010 c = n_err_no;
2011 a_coll_ocs__finalize(coap);
2012 n_perr(_("Cannot invoke *on-compose-splice(-shell)?*"), c);
2013 goto jerr;
2015 if(*checkaddr_err != 0){
2016 *checkaddr_err = 0;
2017 goto jerr;
2019 if(coapm == NULL && (coapm = ok_vlook(on_compose_splice)) != NULL)
2020 goto Jocs;
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)
2048 goto jerr;
2050 /* TODO Cannot do since it may require turning this into a multipart one */
2051 if(n_poption & n_PO_Mm_FLAG)
2052 goto jskiptails;
2054 /* Place signature? */
2055 if((cp = ok_vlook(signature)) != NULL && *cp != '\0'){
2056 char const *cpq;
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));
2061 goto jerr;
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));
2068 goto jerr;
2071 if(linebuf == NULL)
2072 linebuf = smalloc(linesize = LINESIZE);
2073 c = '\0';
2074 while((i = fread(linebuf, sizeof *linebuf, linesize, n_UNVOLATILE(sigfp)))
2075 > 0){
2076 c = linebuf[i - 1];
2077 if(i != fwrite(linebuf, sizeof *linebuf, i, _coll_fp))
2078 goto jerr;
2081 /* C99 */{
2082 FILE *x = n_UNVOLATILE(sigfp);
2083 int e = n_err_no, ise = ferror(x);
2085 sigfp = NULL;
2086 Fclose(x);
2088 if(ise){
2089 n_err(_("Errors while reading *signature* %s: %s\n"),
2090 cpq, n_err_to_doc(e));
2091 goto jerr;
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)
2107 goto jerr;
2108 if((n_psonce & n_PSO_INTERACTIVE) && putesc(cp, n_stdout) < 0)
2109 goto jerr;
2113 jskiptails:
2114 if(fflush(_coll_fp))
2115 goto jerr;
2116 rewind(_coll_fp);
2118 jleave:
2119 if (linebuf != NULL)
2120 free(linebuf);
2121 sigfillset(&nset);
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);
2127 NYD_LEAVE;
2128 return _coll_fp;
2130 jerr:
2131 if(coap != NULL && coap != (struct a_coll_ocs_arg*)-1)
2132 n_go_splice_hack_remove_after_jump();
2133 if(sigfp != NULL)
2134 Fclose(n_UNVOLATILE(sigfp));
2135 if (_coll_fp != NULL) {
2136 Fclose(_coll_fp);
2137 _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"));
2148 goto jleave;
2151 /* s-it-mode */