main(): tweaks
[s-mailx.git] / collect.c
blobadf2ff4efadeba52c20202bb2618bd5e8609cdfd
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 const *cmd);
93 /* Interpolate the named messages into the current message, possibly doing
94 * indent stuff. The flag argument is one of the command escapes: [mMfFuU].
95 * Return a count of the number of characters now in the message, or -1 if an
96 * error is encountered writing the message temporary */
97 static int forward(char const *ms, FILE *fp, int f);
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 const *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 const *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 return 0;
1356 static void
1357 a_coll_ocs__finalize(void *vp){
1358 /* Note we use this for destruction upon setup errors, thus */
1359 sighandler_type opipe;
1360 sighandler_type oint;
1361 struct a_coll_ocs_arg **coapp, *coap;
1362 NYD2_ENTER;
1364 temporary_compose_mode_hook_call((char*)-1, NULL, NULL);
1366 coap = *(coapp = vp);
1367 *coapp = (struct a_coll_ocs_arg*)-1;
1369 if(coap->coa_stdout != NULL)
1370 if(!Pclose(coap->coa_stdout, FAL0)){
1371 *coap->coa_senderr = TRU1;
1372 n_err(_("*on-compose-splice(-shell)?* failed: %s\n"),
1373 n_shexp_quote_cp(coap->coa_cmd, FAL0));
1376 if(coap->coa_stdin != NULL)
1377 Fclose(coap->coa_stdin);
1378 else if(coap->coa_pipe[0] != -1)
1379 close(coap->coa_pipe[0]);
1381 if(coap->coa_pipe[1] != -1)
1382 close(coap->coa_pipe[1]);
1384 opipe = coap->coa_opipe;
1385 oint = coap->coa_oint;
1387 n_lofi_free(coap);
1389 hold_all_sigs();
1390 safe_signal(SIGPIPE, opipe);
1391 safe_signal(SIGINT, oint);
1392 rele_all_sigs();
1393 NYD2_LEAVE;
1396 FL FILE *
1397 collect(struct header *hp, int printheaders, struct message *mp,
1398 char const *quotefile, int doprefix, si8_t *checkaddr_err)
1400 struct n_string s, * volatile sp;
1401 struct n_ignore const *quoteitp;
1402 struct a_coll_ocs_arg *coap;
1403 int c;
1404 int volatile t, eofcnt, getfields;
1405 char volatile escape;
1406 char *linebuf, escape_saved;
1407 char const *cp, *cp_base, *coapm, *ifs_saved;
1408 size_t i, linesize; /* TODO line pool */
1409 long cnt;
1410 enum sendaction action;
1411 sigset_t oset, nset;
1412 FILE * volatile sigfp;
1413 NYD_ENTER;
1415 _coll_fp = NULL;
1416 sigfp = NULL;
1417 linesize = 0;
1418 linebuf = NULL;
1419 eofcnt = 0;
1420 ifs_saved = coapm = NULL;
1421 coap = NULL;
1423 /* Start catching signals from here, but we're still die on interrupts
1424 * until we're in the main loop */
1425 sigfillset(&nset);
1426 sigprocmask(SIG_BLOCK, &nset, &oset);
1427 /* FIXME have dropped handlerpush() and localized onintr() in go.c! */
1428 if ((_coll_saveint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
1429 safe_signal(SIGINT, &_collint);
1430 if ((_coll_savehup = safe_signal(SIGHUP, SIG_IGN)) != SIG_IGN)
1431 safe_signal(SIGHUP, collhup);
1432 if (sigsetjmp(_coll_abort, 1))
1433 goto jerr;
1434 if (sigsetjmp(_coll_jmp, 1))
1435 goto jerr;
1436 n_pstate |= n_PS_COMPOSE_MODE;
1437 sigprocmask(SIG_SETMASK, &oset, (sigset_t*)NULL);
1439 if ((_coll_fp = Ftmp(NULL, "collect", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
1440 NULL) {
1441 n_perr(_("temporary mail file"), 0);
1442 goto jerr;
1445 /* If we are going to prompt for a subject, refrain from printing a newline
1446 * after the headers (since some people mind) */
1447 getfields = 0;
1448 if (!(n_poption & n_PO_t_FLAG)) {
1449 t = GTO | GSUBJECT | GCC | GNL;
1450 if (ok_blook(fullnames))
1451 t |= GCOMMA;
1453 if (n_psonce & n_PSO_INTERACTIVE) {
1454 if (hp->h_subject == NULL && (ok_blook(ask) || ok_blook(asksub)))
1455 t &= ~GNL, getfields |= GSUBJECT;
1457 if (hp->h_to == NULL)
1458 t &= ~GNL, getfields |= GTO;
1460 if (!ok_blook(bsdcompat) && !ok_blook(askatend)) {
1461 if (hp->h_bcc == NULL && ok_blook(askbcc))
1462 t &= ~GNL, getfields |= GBCC;
1463 if (hp->h_cc == NULL && ok_blook(askcc))
1464 t &= ~GNL, getfields |= GCC;
1467 } else {
1468 n_UNINIT(t, 0);
1471 escape_saved = escape = *(cp = ok_vlook(escape));
1472 _coll_hadintr = 0;
1474 if (!sigsetjmp(_coll_jmp, 1)) {
1475 /* Ask for some headers first, as necessary */
1476 if (getfields)
1477 grab_headers(n_GO_INPUT_CTX_COMPOSE, hp, getfields, 1);
1479 /* Execute compose-enter TODO completely v15-compat intermediate!! */
1480 if((cp = ok_vlook(on_compose_enter)) != NULL){
1481 setup_from_and_sender(hp);
1482 temporary_compose_mode_hook_call(cp, &a_coll__hook_setter, hp);
1485 /* TODO Mm: nope since it may require turning this into a multipart one */
1486 if(!(n_poption & n_PO_Mm_FLAG)){
1487 char const *cp_obsolete = ok_vlook(NAIL_HEAD);
1488 if(cp_obsolete != NULL)
1489 n_OBSOLETE(_("please use *message-inject-head*, not *NAIL_HEAD*"));
1491 if(((cp = ok_vlook(message_inject_head)) != NULL ||
1492 (cp = cp_obsolete) != NULL) && putesc(cp, _coll_fp) < 0)
1493 goto jerr;
1495 /* Quote an original message */
1496 if (mp != NULL && (doprefix || (cp = ok_vlook(quote)) != NULL)) {
1497 quoteitp = n_IGNORE_ALL;
1498 action = SEND_QUOTE;
1499 if (doprefix) {
1500 quoteitp = n_IGNORE_FWD;
1501 if ((cp = ok_vlook(fwdheading)) == NULL)
1502 cp = "-------- Original Message --------";
1503 if (*cp != '\0' && fprintf(_coll_fp, "%s\n", cp) < 0)
1504 goto jerr;
1505 } else if (!strcmp(cp, "noheading")) {
1506 /*EMPTY*/;
1507 } else if (!strcmp(cp, "headers")) {
1508 quoteitp = n_IGNORE_TYPE;
1509 } else if (!strcmp(cp, "allheaders")) {
1510 quoteitp = NULL;
1511 action = SEND_QUOTE_ALL;
1512 } else {
1513 cp = hfield1("from", mp);
1514 if (cp != NULL && (cnt = (long)strlen(cp)) > 0) {
1515 if (xmime_write(cp, cnt, _coll_fp, CONV_FROMHDR, TD_NONE) < 0)
1516 goto jerr;
1517 if (fprintf(_coll_fp, _(" wrote:\n\n")) < 0)
1518 goto jerr;
1521 if (fflush(_coll_fp))
1522 goto jerr;
1523 if (doprefix)
1524 cp = NULL;
1525 else if ((cp = ok_vlook(indentprefix)) == NULL)
1526 cp = INDENT_DEFAULT;
1527 if (sendmp(mp, _coll_fp, quoteitp, cp, action, NULL) < 0)
1528 goto jerr;
1532 if (quotefile != NULL) {
1533 if(!a_coll_include_file(quotefile, FAL0, FAL0))
1534 goto jerr;
1537 sp = NULL;
1538 if(n_psonce & n_PSO_INTERACTIVE){
1539 if(!(n_pstate & n_PS_SOURCING)){
1540 sp = n_string_creat_auto(&s);
1541 sp = n_string_reserve(sp, 80);
1544 if(!(n_poption & n_PO_Mm_FLAG)){
1545 /* Print what we have sofar also on the terminal (if useful) */
1546 if (!ok_blook(editalong)) {
1547 if (printheaders)
1548 puthead(TRU1, hp, n_stdout, t,
1549 SEND_TODISP, CONV_NONE, NULL, NULL);
1551 rewind(_coll_fp);
1552 while ((c = getc(_coll_fp)) != EOF) /* XXX bytewise, yuck! */
1553 putc(c, n_stdout);
1554 if (fseek(_coll_fp, 0, SEEK_END))
1555 goto jerr;
1556 } else {
1557 rewind(_coll_fp);
1558 mesedit('e', hp);
1559 /* Print msg mandated by the Mail Reference Manual */
1560 jcont:
1561 if(n_psonce & n_PSO_INTERACTIVE)
1562 fputs(_("(continue)\n"), n_stdout);
1564 fflush(n_stdout);
1567 } else {
1568 /* Come here for printing the after-signal message. Duplicate messages
1569 * won't be printed because the write is aborted if we get a SIGTTOU */
1570 if (_coll_hadintr)
1571 n_err(_("\n(Interrupt -- one more to kill letter)\n"));
1574 /* If not under shell hook control */
1575 if(coap == NULL){
1576 /* We're done with -M or -m TODO because: we are too stupid yet, above */
1577 if(n_poption & n_PO_Mm_FLAG)
1578 goto jout;
1579 /* No command escapes, interrupts not expected. Simply copy STDIN */
1580 if(!(n_psonce & n_PSO_INTERACTIVE) &&
1581 !(n_poption & (n_PO_t_FLAG | n_PO_TILDE_FLAG))){
1582 linebuf = srealloc(linebuf, linesize = LINESIZE);
1583 while ((i = fread(linebuf, sizeof *linebuf, linesize, n_stdin)) > 0) {
1584 if (i != fwrite(linebuf, sizeof *linebuf, i, _coll_fp))
1585 goto jerr;
1587 goto jout;
1591 /* The interactive collect loop */
1592 for(;;){
1593 enum {a_HIST_NONE, a_HIST_ADD = 1u<<0, a_HIST_GABBY = 1u<<1} hist;
1595 /* C99 */{
1596 enum n_go_input_flags gif;
1597 bool_t histadd;
1599 gif = n_GO_INPUT_CTX_COMPOSE;
1600 histadd = (sp != NULL);
1601 if((n_psonce & n_PSO_INTERACTIVE) || (n_poption & n_PO_TILDE_FLAG)){
1602 if(!(n_poption & n_PO_t_FLAG))
1603 gif |= n_GO_INPUT_NL_ESC;
1605 cnt = n_go_input(gif, n_empty, &linebuf, &linesize, NULL, &histadd);
1607 hist = histadd ? a_HIST_ADD | a_HIST_GABBY : a_HIST_NONE;
1610 if (cnt < 0) {
1611 if(coap != NULL)
1612 break;
1613 if(n_poption & n_PO_t_FLAG){
1614 fflush_rewind(_coll_fp);
1615 /* It is important to set n_PSO_t_FLAG before extract_header()
1616 * *and* keep n_PO_t_FLAG for the first parse of the message! */
1617 n_psonce |= n_PSO_t_FLAG;
1618 if(makeheader(_coll_fp, hp, checkaddr_err) != OKAY)
1619 goto jerr;
1620 n_poption &= ~n_PO_t_FLAG;
1621 continue;
1622 }else if((n_psonce & n_PSO_INTERACTIVE) && ok_blook(ignoreeof) &&
1623 ++eofcnt < 4){
1624 fprintf(n_stdout,
1625 _("*ignoreeof* set, use `~.' to terminate letter\n"));
1626 n_go_input_clearerr();
1627 continue;
1629 break;
1632 _coll_hadintr = 0;
1634 cp = linebuf;
1635 if(cnt == 0)
1636 goto jputnl;
1637 else if(coap == NULL){
1638 if(!(n_psonce & n_PSO_INTERACTIVE) && !(n_poption & n_PO_TILDE_FLAG))
1639 goto jputline;
1640 else if(cp[0] == '.'){
1641 if(cnt == 1 && (ok_blook(dot) ||
1642 (ok_blook(posix) && ok_blook(ignoreeof))))
1643 break;
1646 if(cp[0] != escape){
1647 jputline:
1648 if(fwrite(cp, sizeof *cp, cnt, _coll_fp) != (size_t)cnt)
1649 goto jerr;
1650 /* TODO n_PS_READLINE_NL is a terrible hack to ensure that _in_all_-
1651 * TODO _code_paths_ a file without trailing newline isn't modified
1652 * TODO to contain one; the "saw-newline" needs to be part of an
1653 * TODO I/O input machinery object */
1654 jputnl:
1655 if(n_pstate & n_PS_READLINE_NL){
1656 if(putc('\n', _coll_fp) == EOF)
1657 goto jerr;
1659 continue;
1662 c = *(cp_base = ++cp);
1663 --cnt;
1664 /* Avoid history entry? */
1665 if(spacechar(c)){
1666 hist = a_HIST_NONE;
1667 c = *(cp_base = ++cp);
1668 --cnt;
1670 /* It may just be an escaped escaped character, do that quick */
1671 else if(c == escape)
1672 goto jputline;
1674 /* Trim input, also to gain a somewhat normalized history entry */
1675 ++cp;
1676 if(--cnt > 0){
1677 struct str x;
1679 x.s = n_UNCONST(cp);
1680 x.l = (size_t)cnt;
1681 n_str_trim_ifs(&x, TRU1);
1682 x.s[x.l] = '\0';
1683 cp = x.s;
1684 cnt = (int)/*XXX*/x.l;
1687 if(hist != a_HIST_NONE){
1688 sp = n_string_assign_c(sp, escape);
1689 sp = n_string_push_c(sp, c);
1690 if(cnt > 0){
1691 sp = n_string_push_c(sp, ' ');
1692 sp = n_string_push_buf(sp, cp, cnt);
1696 switch(c){
1697 default:
1699 char buf[sizeof(n_UNIREPL)];
1701 if(asciichar(c))
1702 buf[0] = c, buf[1] = '\0';
1703 else if(n_psonce & n_PSO_UNICODE)
1704 memcpy(buf, n_unirepl, sizeof n_unirepl);
1705 else
1706 buf[0] = '?', buf[1] = '\0';
1707 n_err(_("Unknown command escape: `%c%s'\n"), escape, buf);
1708 continue;
1710 jearg:
1711 n_err(_("Invalid command escape usage: %s\n"),
1712 n_shexp_quote_cp(linebuf, FAL0));
1713 continue;
1714 case '!':
1715 /* Shell escape, send the balance of line to sh -c */
1716 if(cnt == 0 || coap != NULL)
1717 goto jearg;
1718 else{
1719 char const *argv[2];
1721 argv[0] = cp;
1722 argv[1] = NULL;
1723 c_shell(argv);
1725 goto jhistcont;
1726 case ':':
1727 /* FALLTHRU */
1728 case '_':
1729 /* Escape to command mode, but be nice! *//* TODO command expansion
1730 * TODO should be handled here so that we have unique history! */
1731 if(cnt == 0)
1732 goto jearg;
1733 _execute_command(hp, cp, cnt);
1734 break;
1735 case '.':
1736 /* Simulate end of file on input */
1737 if(cnt != 0 || coap != NULL)
1738 goto jearg;
1739 goto jout;
1740 case 'x':
1741 /* Same as 'q', but no *DEAD* saving */
1742 /* FALLTHRU */
1743 case 'q':
1744 /* Force a quit, act like an interrupt had happened */
1745 if(cnt != 0)
1746 goto jearg;
1747 ++_coll_hadintr;
1748 _collint((c == 'x') ? 0 : SIGINT);
1749 exit(n_EXIT_ERR);
1750 /*NOTREACHED*/
1751 case 'h':
1752 /* Grab a bunch of headers */
1753 if(cnt != 0)
1754 goto jearg;
1756 grab_headers(n_GO_INPUT_CTX_COMPOSE, hp,
1757 (GTO | GSUBJECT | GCC | GBCC),
1758 (ok_blook(bsdcompat) && ok_blook(bsdorder)));
1759 }while(hp->h_to == NULL);
1760 break;
1761 case 'H':
1762 /* Grab extra headers */
1763 if(cnt != 0)
1764 goto jearg;
1766 grab_headers(n_GO_INPUT_CTX_COMPOSE, hp, GEXTRA, 0);
1767 }while(check_from_and_sender(hp->h_from, hp->h_sender) == NULL);
1768 break;
1769 case 't':
1770 /* Add to the To: list */
1771 if(cnt == 0)
1772 goto jearg;
1773 hp->h_to = cat(hp->h_to,
1774 checkaddrs(lextract(cp, GTO | GFULL), EACM_NORMAL,
1775 NULL));
1776 hist &= ~a_HIST_GABBY;
1777 break;
1778 case 's':
1779 /* Set the Subject list */
1780 if(cnt == 0)
1781 goto jearg;
1782 /* Subject:; take care for Debian #419840 and strip any \r and \n */
1783 if(n_anyof_cp("\n\r", hp->h_subject = savestr(cp))){
1784 char *xp;
1786 n_err(_("-s: normalizing away invalid ASCII NL / CR bytes\n"));
1787 for(xp = hp->h_subject; *xp != '\0'; ++xp)
1788 if(*xp == '\n' || *xp == '\r')
1789 *xp = ' ';
1791 break;
1792 case '@':{
1793 struct attachment *aplist;
1795 /* Edit the attachment list */
1796 aplist = hp->h_attach;
1797 hp->h_attach = NULL;
1798 if(cnt != 0)
1799 hp->h_attach = n_attachment_append_list(aplist, cp);
1800 else
1801 hp->h_attach = n_attachment_list_edit(aplist,
1802 n_GO_INPUT_CTX_COMPOSE);
1803 } break;
1804 case 'c':
1805 /* Add to the CC list */
1806 if(cnt == 0)
1807 goto jearg;
1808 hp->h_cc = cat(hp->h_cc,
1809 checkaddrs(lextract(cp, GCC | GFULL), EACM_NORMAL,
1810 NULL));
1811 hist &= ~a_HIST_GABBY;
1812 break;
1813 case 'b':
1814 /* Add stuff to blind carbon copies list */
1815 if(cnt == 0)
1816 goto jearg;
1817 hp->h_bcc = cat(hp->h_bcc,
1818 checkaddrs(lextract(cp, GBCC | GFULL), EACM_NORMAL, NULL));
1819 hist &= ~a_HIST_GABBY;
1820 break;
1821 case 'd':
1822 if(cnt != 0)
1823 goto jearg;
1824 cp = n_getdeadletter();
1825 if(0){
1826 /*FALLTHRU*/
1827 case 'R':
1828 case 'r':
1829 case '<':
1830 /* Invoke a file: Search for the file name, then open it and copy
1831 * the contents to _coll_fp */
1832 if(cnt == 0){
1833 n_err(_("Interpolate what file?\n"));
1834 break;
1836 if(*cp == '!' && c == '<'){
1837 if(!a_coll_insert_cmd(_coll_fp, ++cp)){
1838 if(ferror(_coll_fp))
1839 goto jerr;
1840 break;
1842 goto jhistcont;
1844 /* Note this also expands things like
1845 * !:vput vexpr delim random 0
1846 * !< - $delim */
1847 if((cp = fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO | FEXP_NSHELL)
1848 ) == NULL)
1849 break;
1851 if(n_is_dir(cp, FAL0)){
1852 n_err(_("%s: is a directory\n"), n_shexp_quote_cp(cp, FAL0));
1853 break;
1855 if(!a_coll_include_file(cp, (c == 'R'), TRU1)){
1856 if(ferror(_coll_fp))
1857 goto jerr;
1858 break;
1860 break;
1861 case 'i':
1862 /* Insert a variable into the file */
1863 if(cnt == 0)
1864 goto jearg;
1865 if((cp = n_var_vlook(cp, TRU1)) == NULL || *cp == '\0')
1866 break;
1867 if(putesc(cp, _coll_fp) < 0) /* TODO v15: user resp upon `set' time */
1868 goto jerr;
1869 if((n_psonce & n_PSO_INTERACTIVE) && putesc(cp, n_stdout) < 0)
1870 goto jerr;
1871 break;
1872 case 'a':
1873 case 'A':
1874 /* Insert the contents of a signature variable */
1875 if(cnt != 0)
1876 goto jearg;
1877 cp = (c == 'a') ? ok_vlook(sign) : ok_vlook(Sign);
1878 if(cp != NULL && *cp != '\0'){
1879 if(putesc(cp, _coll_fp) < 0) /* TODO v15: user upon `set' time */
1880 goto jerr;
1881 if((n_psonce & n_PSO_INTERACTIVE) && putesc(cp, n_stdout) < 0)
1882 goto jerr;
1884 break;
1885 case 'w':
1886 /* Write the message on a file */
1887 if(cnt == 0)
1888 goto jearg;
1889 if((cp = fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO)) == NULL){
1890 n_err(_("Write what file!?\n"));
1891 break;
1893 rewind(_coll_fp);
1894 if(exwrite(cp, _coll_fp, 1) < 0)
1895 goto jerr;
1896 break;
1897 case 'm':
1898 case 'M':
1899 case 'f':
1900 case 'F':
1901 case 'u':
1902 case 'U':
1903 /* Interpolate the named messages, if we are in receiving mail mode.
1904 * Does the standard list processing garbage. If ~f is given, we
1905 * don't shift over */
1906 if(cnt == 0)
1907 goto jearg;
1908 if(forward(cp, _coll_fp, c) < 0)
1909 break;
1910 break;
1911 case 'p':
1912 /* Print current state of the message without altering anything */
1913 if(cnt != 0)
1914 goto jearg;
1915 print_collf(_coll_fp, hp);
1916 break;
1917 case '|':
1918 /* Pipe message through command. Collect output as new message */
1919 if(cnt == 0)
1920 goto jearg;
1921 rewind(_coll_fp);
1922 mespipe(cp);
1923 hist &= ~a_HIST_GABBY;
1924 goto jhistcont;
1925 case 'v':
1926 case 'e':
1927 /* Edit the current message. 'e' -> use EDITOR, 'v' -> use VISUAL */
1928 if(cnt != 0 || coap != NULL)
1929 goto jearg;
1930 rewind(_coll_fp);
1931 mesedit(c, ok_blook(editheaders) ? hp : NULL);
1932 goto jhistcont;
1933 case '^':
1934 if(!a_collect_plumbing(cp, hp))
1935 goto jearg;
1936 hist &= ~a_HIST_GABBY;
1937 break;
1938 case '?':
1939 /* Last the lengthy help string. (Very ugly, but take care for
1940 * compiler supported string lengths :() */
1941 fputs(_(
1942 "COMMAND ESCAPES (to be placed after a newline) excerpt:\n"
1943 "~. Commit and send message\n"
1944 "~: <command> Execute a mail command\n"
1945 "~< <file> Insert <file> (\"~<! <command>\" inserts output of command)\n"
1946 "~@ [<files>] Edit attachment list\n"
1947 "~A Insert *Sign* variable (`~a': insert *sign*)\n"
1948 "~c <users> Add users to Cc: list (`~b': to Bcc:)\n"
1949 "~d Read in $DEAD (dead.letter)\n"
1950 "~e Edit message via $EDITOR\n"
1951 ), n_stdout);
1952 fputs(_(
1953 "~F <msglist> Read in with headers, do not *indentprefix* lines\n"
1954 "~f <msglist> Like ~F, but honour `ignore' / `retain' configuration\n"
1955 "~H Edit From:, Reply-To: and Sender:\n"
1956 "~h Prompt for Subject:, To:, Cc: and \"blind\" Bcc:\n"
1957 "~i <variable> Insert a value and a newline\n"
1958 "~M <msglist> Read in with headers, *indentprefix* (`~m': `retain' etc.)\n"
1959 "~p Show current message compose buffer\n"
1960 "~r <file> Insert <file> (`~R': likewise, but *indentprefix* lines)\n"
1961 ), n_stdout);
1962 fputs(_(
1963 "~s <subject> Set Subject:\n"
1964 "~t <users> Add users to To: list\n"
1965 "~u <msglist> Read in message(s) without headers (`~U': indent lines)\n"
1966 "~v Edit message via $VISUAL\n"
1967 "~w <file> Write message onto file\n"
1968 "~x Abort composition, discard message (`~q': save in $DEAD)\n"
1969 "~| <command> Pipe message through shell filter\n"
1970 ), n_stdout);
1971 if(cnt != 0)
1972 goto jearg;
1973 break;
1976 /* Finally place an entry in history as applicable */
1977 if(0){
1978 jhistcont:
1979 c = '\1';
1980 }else
1981 c = '\0';
1982 if(hist & a_HIST_ADD)
1983 n_tty_addhist(n_string_cp(sp), ((hist & a_HIST_GABBY) != 0));
1984 if(c != '\0')
1985 goto jcont;
1988 jout:
1989 /* Do we have *on-compose-splice-shell*, or *on-compose-splice*?
1990 * TODO Usual f...ed up state of signals and terminal etc. */
1991 if(coap == NULL && (cp = ok_vlook(on_compose_splice_shell)) != NULL) Jocs:{
1992 union {int (*ptf)(void); char const *sh;} u;
1993 char const *cmd;
1995 /* Reset *escape* and more to their defaults. On change update manual! */
1996 if(ifs_saved == NULL){
1997 escape = n_ESCAPE[0];
1998 ifs_saved = savestr(ok_vlook(ifs));
1999 ok_vclear(ifs);
2002 if(coapm != NULL){
2003 /* XXX Due Popen() fflush(NULL) in PTF mode, ensure nothing to flush */
2004 /*if(!n_real_seek(_coll_fp, 0, SEEK_END))
2005 * goto jerr;*/
2006 u.ptf = &a_coll_ocs__mac;
2007 cmd = (char*)-1;
2008 a_coll_ocs__macname = cp = coapm;
2009 }else{
2010 u.sh = ok_vlook(SHELL);
2011 cmd = cp;
2014 i = strlen(cp) +1;
2015 coap = n_lofi_alloc(n_VSTRUCT_SIZEOF(struct a_coll_ocs_arg, coa_cmd
2016 ) + i);
2017 coap->coa_pipe[0] = coap->coa_pipe[1] = -1;
2018 coap->coa_stdin = coap->coa_stdout = NULL;
2019 coap->coa_senderr = checkaddr_err;
2020 memcpy(coap->coa_cmd, cp, i);
2022 hold_all_sigs();
2023 coap->coa_opipe = safe_signal(SIGPIPE, SIG_IGN);
2024 coap->coa_oint = safe_signal(SIGINT, SIG_IGN);
2025 rele_all_sigs();
2027 if(pipe_cloexec(coap->coa_pipe) != -1 &&
2028 (coap->coa_stdin = Fdopen(coap->coa_pipe[0], "r", FAL0)) != NULL &&
2029 (coap->coa_stdout = Popen(cmd, "W", u.sh, NULL, coap->coa_pipe[1])
2030 ) != NULL){
2031 close(coap->coa_pipe[1]);
2032 coap->coa_pipe[1] = -1;
2034 temporary_compose_mode_hook_call(NULL, NULL, NULL);
2035 n_go_splice_hack(coap->coa_cmd, coap->coa_stdin, coap->coa_stdout,
2036 (n_psonce & ~(n_PSO_INTERACTIVE | n_PSO_TTYIN | n_PSO_TTYOUT)),
2037 &a_coll_ocs__finalize, &coap);
2038 /* Hook version protocol for ~^: update manual upon change! */
2039 fputs(a_COLL_PLUMBING_VERSION "\n", coap->coa_stdout);
2040 #undef a_COLL_PLUMBING_VERSION
2041 goto jcont;
2044 c = n_err_no;
2045 a_coll_ocs__finalize(coap);
2046 n_perr(_("Cannot invoke *on-compose-splice(-shell)?*"), c);
2047 goto jerr;
2049 if(*checkaddr_err != 0){
2050 *checkaddr_err = 0;
2051 goto jerr;
2053 if(coapm == NULL && (coapm = ok_vlook(on_compose_splice)) != NULL)
2054 goto Jocs;
2055 if(coap != NULL){
2056 ok_vset(ifs, ifs_saved);
2057 ifs_saved = NULL;
2058 escape = escape_saved;
2061 /* Final chance to edit headers, if not already above */
2062 if (ok_blook(bsdcompat) || ok_blook(askatend)) {
2063 if (hp->h_cc == NULL && ok_blook(askcc))
2064 grab_headers(n_GO_INPUT_CTX_COMPOSE, hp, GCC, 1);
2065 if (hp->h_bcc == NULL && ok_blook(askbcc))
2066 grab_headers(n_GO_INPUT_CTX_COMPOSE, hp, GBCC, 1);
2068 if (hp->h_attach == NULL && ok_blook(askattach))
2069 hp->h_attach = n_attachment_list_edit(hp->h_attach,
2070 n_GO_INPUT_CTX_COMPOSE);
2072 /* Execute compose-leave */
2073 if((cp = ok_vlook(on_compose_leave)) != NULL){
2074 setup_from_and_sender(hp);
2075 temporary_compose_mode_hook_call(cp, &a_coll__hook_setter, hp);
2078 /* Add automatic receivers */
2079 if ((cp = ok_vlook(autocc)) != NULL && *cp != '\0')
2080 hp->h_cc = cat(hp->h_cc, checkaddrs(lextract(cp, GCC | GFULL),
2081 EACM_NORMAL, checkaddr_err));
2082 if ((cp = ok_vlook(autobcc)) != NULL && *cp != '\0')
2083 hp->h_bcc = cat(hp->h_bcc, checkaddrs(lextract(cp, GBCC | GFULL),
2084 EACM_NORMAL, checkaddr_err));
2085 if (*checkaddr_err != 0)
2086 goto jerr;
2088 /* TODO Cannot do since it may require turning this into a multipart one */
2089 if(n_poption & n_PO_Mm_FLAG)
2090 goto jskiptails;
2092 /* Place signature? */
2093 if((cp = ok_vlook(signature)) != NULL && *cp != '\0'){
2094 char const *cpq;
2096 if((cpq = fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO)) == NULL){
2097 n_err(_("*signature* expands to invalid file: %s\n"),
2098 n_shexp_quote_cp(cp, FAL0));
2099 goto jerr;
2101 cpq = n_shexp_quote_cp(cp = cpq, FAL0);
2103 if((sigfp = Fopen(cp, "r")) == NULL){
2104 n_err(_("Can't open *signature* %s: %s\n"),
2105 cpq, n_err_to_doc(n_err_no));
2106 goto jerr;
2109 if(linebuf == NULL)
2110 linebuf = smalloc(linesize = LINESIZE);
2111 c = '\0';
2112 while((i = fread(linebuf, sizeof *linebuf, linesize, n_UNVOLATILE(sigfp)))
2113 > 0){
2114 c = linebuf[i - 1];
2115 if(i != fwrite(linebuf, sizeof *linebuf, i, _coll_fp))
2116 goto jerr;
2119 /* C99 */{
2120 FILE *x = n_UNVOLATILE(sigfp);
2121 int e = n_err_no, ise = ferror(x);
2123 sigfp = NULL;
2124 Fclose(x);
2126 if(ise){
2127 n_err(_("Errors while reading *signature* %s: %s\n"),
2128 cpq, n_err_to_doc(e));
2129 goto jerr;
2133 if(c != '\0' && c != '\n')
2134 putc('\n', _coll_fp);
2137 { char const *cp_obsolete = ok_vlook(NAIL_TAIL);
2139 if(cp_obsolete != NULL)
2140 n_OBSOLETE(_("please use *message-inject-tail*, not *NAIL_TAIL*"));
2142 if((cp = ok_vlook(message_inject_tail)) != NULL ||
2143 (cp = cp_obsolete) != NULL){
2144 if(putesc(cp, _coll_fp) < 0)
2145 goto jerr;
2146 if((n_psonce & n_PSO_INTERACTIVE) && putesc(cp, n_stdout) < 0)
2147 goto jerr;
2151 jskiptails:
2152 if(fflush(_coll_fp))
2153 goto jerr;
2154 rewind(_coll_fp);
2156 jleave:
2157 if (linebuf != NULL)
2158 free(linebuf);
2159 sigfillset(&nset);
2160 sigprocmask(SIG_BLOCK, &nset, NULL);
2161 n_pstate &= ~n_PS_COMPOSE_MODE;
2162 safe_signal(SIGINT, _coll_saveint);
2163 safe_signal(SIGHUP, _coll_savehup);
2164 sigprocmask(SIG_SETMASK, &oset, NULL);
2165 NYD_LEAVE;
2166 return _coll_fp;
2168 jerr:
2169 if(coap != NULL && coap != (struct a_coll_ocs_arg*)-1)
2170 n_go_splice_hack_remove_after_jump();
2171 if(ifs_saved != NULL)
2172 ok_vset(ifs, ifs_saved);
2173 if(sigfp != NULL)
2174 Fclose(n_UNVOLATILE(sigfp));
2175 if (_coll_fp != NULL) {
2176 Fclose(_coll_fp);
2177 _coll_fp = NULL;
2179 assert(checkaddr_err != NULL);
2180 /* TODO We don't save in $DEAD upon error because msg not readily composed?
2181 * TODO But this no good, it should go ZOMBIE / DRAFT / POSTPONED or what! */
2182 if(*checkaddr_err != 0)
2183 n_err(_("Some addressees were classified as \"hard error\"\n"));
2184 else if(_coll_hadintr == 0){
2185 *checkaddr_err = TRU1; /* TODO ugly: "sendout_error" now.. */
2186 n_err(_("Failed to prepare composed message (I/O error, disk full?)\n"));
2188 goto jleave;
2191 /* s-it-mode */