Bump S-nail v14.9.4.ar ("(5th anniversary) Marsh tit"), 2017-09-18
[s-mailx.git] / collect.c
blob89e91090d18aeb263c381e7c26485e56160a3372
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 /* Return errno */
69 static si32_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, return errno */
73 static si32_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 si32_t a_coll_write(char const *name, FILE *fp, int f);
81 /* *message-inject-head* */
82 static bool_t a_coll_message_inject_head(FILE *fp);
84 /* Parse off the message header from fp and store relevant fields in hp,
85 * replace _coll_fp with a shiny new version without any header */
86 static bool_t a_coll_makeheader(FILE *fp, struct header *hp,
87 si8_t *checkaddr_err, bool_t do_delayed_due_t);
89 /* Edit the message being collected on fp. On return, make the edit file the
90 * new temp file. Return errno */
91 static si32_t a_coll_edit(int c, struct header *hp);
93 /* Pipe the message through the command. Old message is on stdin of command,
94 * new message collected from stdout. Shell must return 0 to accept new msg */
95 static si32_t a_coll_pipe(char const *cmd);
97 /* Interpolate the named messages into the current message, possibly doing
98 * indent stuff. The flag argument is one of the command escapes: [mMfFuU].
99 * Return errno */
100 static si32_t a_coll_forward(char const *ms, FILE *fp, int f);
102 /* ~^ mode */
103 static bool_t a_collect_plumbing(char const *ms, struct header *p);
105 static bool_t a_collect__plumb_header(char const *cp, struct header *p,
106 char const *cmd[4]);
107 static bool_t a_collect__plumb_attach(char const *cp, struct header *p,
108 char const *cmd[4]);
110 /* On interrupt, come here to save the partial message in ~/dead.letter.
111 * Then jump out of the collection loop */
112 static void _collint(int s);
114 static void collhup(int s);
116 /* ~[AaIi], *message-inject-**: put value, expand \[nt] if *posix* */
117 static bool_t a_coll_putesc(char const *s, bool_t addnl, FILE *stream);
119 /* *on-compose-splice* driver and *on-compose-splice(-shell)?* finalizer */
120 static int a_coll_ocs__mac(void);
121 static void a_coll_ocs__finalize(void *vp);
123 static void
124 _execute_command(struct header *hp, char const *linebuf, size_t linesize){
125 /* The problem arises if there are rfc822 message attachments and the
126 * user uses `~:' to change the current file. TODO Unfortunately we
127 * TODO cannot simply keep a pointer to, or increment a reference count
128 * TODO of the current `file' (mailbox that is) object, because the
129 * TODO codebase doesn't deal with that at all; so, until some far
130 * TODO later time, copy the name of the path, and warn the user if it
131 * TODO changed; we COULD use the AC_TMPFILE attachment type, i.e.,
132 * TODO copy the message attachments over to temporary files, but that
133 * TODO would require more changes so that the user still can recognize
134 * TODO in `~@' etc. that its a rfc822 message attachment; see below */
135 struct n_sigman sm;
136 struct attachment *ap;
137 char * volatile mnbuf;
138 NYD_ENTER;
140 n_UNUSED(linesize);
141 mnbuf = NULL;
143 n_SIGMAN_ENTER_SWITCH(&sm, n_SIGMAN_HUP | n_SIGMAN_INT | n_SIGMAN_QUIT){
144 case 0:
145 break;
146 default:
147 n_pstate_err_no = n_ERR_INTR;
148 n_pstate_ex_no = 1;
149 goto jleave;
152 /* If the above todo is worked, remove or outsource to attachment.c! */
153 if(hp != NULL && (ap = hp->h_attach) != NULL) do
154 if(ap->a_msgno){
155 mnbuf = sstrdup(mailname);
156 break;
158 while((ap = ap->a_flink) != NULL);
160 n_go_command(n_GO_INPUT_CTX_COMPOSE, linebuf);
162 n_sigman_cleanup_ping(&sm);
163 jleave:
164 if(mnbuf != NULL){
165 if(strcmp(mnbuf, mailname))
166 n_err(_("Mailbox changed: it is likely that existing "
167 "rfc822 attachments became invalid!\n"));
168 free(mnbuf);
170 NYD_LEAVE;
171 n_sigman_leave(&sm, n_SIGMAN_VIPSIGS_NTTYOUT);
174 static si32_t
175 a_coll_include_file(char const *name, bool_t indent, bool_t writestat){
176 FILE *fbuf;
177 char const *heredb, *indb;
178 size_t linesize, heredl, indl, cnt, linelen;
179 char *linebuf;
180 si64_t lc, cc;
181 si32_t rv;
182 NYD_ENTER;
184 rv = n_ERR_NONE;
185 lc = cc = 0;
186 linebuf = NULL; /* TODO line pool */
187 linesize = 0;
188 heredb = NULL;
189 heredl = 0;
191 /* The -M case is special */
192 if(name == (char*)-1){
193 fbuf = n_stdin;
194 name = "-";
195 }else if(name[0] == '-' &&
196 (name[1] == '\0' || blankspacechar(name[1]))){
197 fbuf = n_stdin;
198 if(name[1] == '\0'){
199 if(!(n_psonce & n_PSO_INTERACTIVE)){
200 n_err(_("~< -: HERE-delimiter required in non-interactive mode\n"));
201 rv = n_ERR_INVAL;
202 goto jleave;
204 }else{
205 for(heredb = &name[2]; *heredb != '\0' && blankspacechar(*heredb);
206 ++heredb)
208 if((heredl = strlen(heredb)) == 0){
209 jdelim_empty:
210 n_err(_("~< - HERE-delimiter: delimiter must not be empty\n"));
211 rv = n_ERR_INVAL;
212 goto jleave;
215 if(*heredb == '\''){
216 for(indb = ++heredb; *indb != '\0' && *indb != '\''; ++indb)
218 if(*indb == '\0'){
219 n_err(_("~< - HERE-delimiter: missing trailing quote\n"));
220 rv = n_ERR_INVAL;
221 goto jleave;
222 }else if(indb[1] != '\0'){
223 n_err(_("~< - HERE-delimiter: trailing characters after "
224 "quote\n"));
225 rv = n_ERR_INVAL;
226 goto jleave;
228 if((heredl = PTR2SIZE(indb - heredb)) == 0)
229 goto jdelim_empty;
230 heredb = savestrbuf(heredb, heredl);
233 name = "-";
234 }else if((fbuf = Fopen(name, "r")) == NULL){
235 n_perr(name, rv = n_err_no);
236 goto jleave;
239 if(!indent)
240 indl = 0;
241 else{
242 if((indb = ok_vlook(indentprefix)) == NULL)
243 indb = INDENT_DEFAULT;
244 indl = strlen(indb);
247 if(fbuf != n_stdin)
248 cnt = fsize(fbuf);
249 while(fgetline(&linebuf, &linesize, (fbuf == n_stdin ? NULL : &cnt),
250 &linelen, fbuf, 0) != NULL){
251 if(heredl > 0 && heredl == linelen - 1 &&
252 !memcmp(heredb, linebuf, heredl)){
253 heredb = NULL;
254 break;
257 if(indl > 0){
258 if(fwrite(indb, sizeof *indb, indl, _coll_fp) != indl){
259 rv = n_err_no;
260 goto jleave;
262 cc += indl;
265 if(fwrite(linebuf, sizeof *linebuf, linelen, _coll_fp) != linelen){
266 rv = n_err_no;
267 goto jleave;
269 cc += linelen;
270 ++lc;
272 if(fflush(_coll_fp)){
273 rv = n_err_no;
274 goto jleave;
277 if(heredb != NULL)
278 rv = n_ERR_NOTOBACCO;
279 jleave:
280 if(linebuf != NULL)
281 free(linebuf);
282 if(fbuf != NULL){
283 if(fbuf != n_stdin)
284 Fclose(fbuf);
285 else if(heredl > 0)
286 clearerr(n_stdin);
289 if(writestat)
290 fprintf(n_stdout, "%s%s %" PRId64 "/%" PRId64 "\n",
291 n_shexp_quote_cp(name, FAL0), (rv ? " " n_ERROR : n_empty), lc, cc);
292 NYD_LEAVE;
293 return rv;
296 static si32_t
297 a_coll_insert_cmd(FILE *fp, char const *cmd){
298 FILE *ibuf;
299 si64_t lc, cc;
300 si32_t rv;
301 NYD_ENTER;
303 rv = n_ERR_NONE;
304 lc = cc = 0;
306 if((ibuf = Popen(cmd, "r", ok_vlook(SHELL), NULL, 0)) != NULL){
307 int c;
309 while((c = getc(ibuf)) != EOF){ /* XXX bytewise, yuck! */
310 if(putc(c, fp) == EOF){
311 rv = n_err_no;
312 break;
314 ++cc;
315 if(c == '\n')
316 ++lc;
318 if(!feof(ibuf) || ferror(ibuf)){
319 if(rv == n_ERR_NONE)
320 rv = n_ERR_IO;
322 if(!Pclose(ibuf, TRU1)){
323 if(rv == n_ERR_NONE)
324 rv = n_ERR_IO;
326 }else
327 n_perr(cmd, rv = n_err_no);
329 fprintf(n_stdout, "CMD%s %" PRId64 "/%" PRId64 "\n",
330 (rv == n_ERR_NONE ? n_empty : " " n_ERROR), lc, cc);
331 NYD_LEAVE;
332 return rv;
335 static void
336 print_collf(FILE *cf, struct header *hp)
338 char *lbuf;
339 FILE *obuf;
340 size_t cnt, linesize, linelen;
341 NYD_ENTER;
343 fflush_rewind(cf);
344 cnt = (size_t)fsize(cf);
346 if((obuf = Ftmp(NULL, "collfp", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL){
347 n_perr(_("Can't create temporary file for `~p' command"), 0);
348 goto jleave;
351 hold_all_sigs();
353 fprintf(obuf, _("-------\nMessage contains:\n")); /* xxx SEARCH112 */
354 puthead(TRU1, hp, obuf,
355 (GIDENT | GTO | GSUBJECT | GCC | GBCC | GNL | GFILES | GCOMMA),
356 SEND_TODISP, CONV_NONE, NULL, NULL);
358 lbuf = NULL;
359 linesize = 0;
360 while(fgetline(&lbuf, &linesize, &cnt, &linelen, cf, 1))
361 prout(lbuf, linelen, obuf);
362 if(lbuf != NULL)
363 free(lbuf);
365 if(hp->h_attach != NULL){
366 fputs(_("-------\nAttachments:\n"), obuf);
367 n_attachment_list_print(hp->h_attach, obuf);
370 rele_all_sigs();
372 page_or_print(obuf, 0);
374 Fclose(obuf);
375 jleave:
376 NYD_LEAVE;
379 static si32_t
380 a_coll_write(char const *name, FILE *fp, int f)
382 FILE *of;
383 int c;
384 si64_t lc, cc;
385 si32_t rv;
386 NYD_ENTER;
388 rv = n_ERR_NONE;
390 if(f) {
391 fprintf(n_stdout, "%s ", n_shexp_quote_cp(name, FAL0));
392 fflush(n_stdout);
395 if ((of = Fopen(name, "a")) == NULL) {
396 n_perr(name, rv = n_err_no);
397 goto jerr;
400 lc = cc = 0;
401 while ((c = getc(fp)) != EOF) {
402 ++cc;
403 if (c == '\n')
404 ++lc;
405 if (putc(c, of) == EOF) {
406 n_perr(name, rv = n_err_no);
407 goto jerr;
410 fprintf(n_stdout, _("%" PRId64 "/%" PRId64 "\n"), lc, cc);
412 jleave:
413 if(of != NULL)
414 Fclose(of);
415 fflush(n_stdout);
416 NYD_LEAVE;
417 return rv;
418 jerr:
419 putc('-', n_stdout);
420 putc('\n', n_stdout);
421 goto jleave;
424 static bool_t
425 a_coll_message_inject_head(FILE *fp){
426 bool_t rv;
427 char const *cp, *cp_obsolete;
428 NYD2_ENTER;
430 cp_obsolete = ok_vlook(NAIL_HEAD);
431 if(cp_obsolete != NULL)
432 n_OBSOLETE(_("please use *message-inject-head*, not *NAIL_HEAD*"));
434 if(((cp = ok_vlook(message_inject_head)) != NULL ||
435 (cp = cp_obsolete) != NULL) && !a_coll_putesc(cp, TRU1, fp))
436 rv = FAL0;
437 else
438 rv = TRU1;
439 NYD2_LEAVE;
440 return rv;
443 static bool_t
444 a_coll_makeheader(FILE *fp, struct header *hp, si8_t *checkaddr_err,
445 bool_t do_delayed_due_t)
447 FILE *nf;
448 int c;
449 bool_t rv;
450 NYD_ENTER;
452 rv = FAL0;
454 if ((nf = Ftmp(NULL, "colhead", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==NULL) {
455 n_perr(_("temporary mail edit file"), 0);
456 goto jleave;
459 extract_header(fp, hp, checkaddr_err);
460 if (checkaddr_err != NULL && *checkaddr_err != 0)
461 goto jleave;
463 /* In template mode some things have been delayed until the template has
464 * been read */
465 if(do_delayed_due_t){
466 char const *cp;
468 if((cp = ok_vlook(on_compose_enter)) != NULL){
469 setup_from_and_sender(hp);
470 temporary_compose_mode_hook_call(cp, &n_temporary_compose_hook_varset,
471 hp);
474 if(!a_coll_message_inject_head(nf))
475 goto jleave;
478 while ((c = getc(fp)) != EOF) /* XXX bytewise, yuck! */
479 putc(c, nf);
481 if (fp != _coll_fp)
482 Fclose(_coll_fp);
483 Fclose(fp);
484 _coll_fp = nf;
485 nf = NULL;
487 if (check_from_and_sender(hp->h_from, hp->h_sender) == NULL)
488 goto jleave;
489 rv = TRU1;
490 jleave:
491 if(nf != NULL)
492 Fclose(nf);
493 NYD_LEAVE;
494 return rv;
497 static si32_t
498 a_coll_edit(int c, struct header *hp) /* TODO error(return) weird */
500 struct n_sigman sm;
501 FILE *nf;
502 sighandler_type volatile sigint;
503 bool_t saved;
504 si32_t volatile rv;
505 NYD_ENTER;
507 n_UNINIT(sigint, SIG_ERR);
508 rv = n_ERR_NONE;
510 if(!(saved = ok_blook(add_file_recipients)))
511 ok_bset(add_file_recipients);
513 n_SIGMAN_ENTER_SWITCH(&sm, n_SIGMAN_ALL){
514 case 0:
515 sigint = safe_signal(SIGINT, SIG_IGN);
516 break;
517 default:
518 rv = n_ERR_INTR;
519 goto jleave;
522 nf = run_editor(_coll_fp, (off_t)-1, c, FAL0, hp, NULL, SEND_MBOX, sigint);
523 if (nf != NULL) {
524 if (hp) {
525 if(!a_coll_makeheader(nf, hp, NULL, FAL0))
526 rv = n_ERR_INVAL;
527 } else {
528 fseek(nf, 0L, SEEK_END);
529 Fclose(_coll_fp);
530 _coll_fp = nf;
532 } else
533 rv = n_ERR_CHILD;
535 n_sigman_cleanup_ping(&sm);
536 jleave:
537 if(!saved)
538 ok_bclear(add_file_recipients);
539 safe_signal(SIGINT, sigint);
540 NYD_LEAVE;
541 n_sigman_leave(&sm, n_SIGMAN_VIPSIGS_NTTYOUT);
542 return rv;
545 static si32_t
546 a_coll_pipe(char const *cmd)
548 int ws;
549 FILE *nf;
550 sighandler_type sigint;
551 si32_t rv;
552 NYD_ENTER;
554 rv = n_ERR_NONE;
555 sigint = safe_signal(SIGINT, SIG_IGN);
557 if ((nf = Ftmp(NULL, "colpipe", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==NULL) {
558 n_perr(_("temporary mail edit file"), rv = n_err_no);
559 goto jout;
562 /* stdin = current message. stdout = new message */
563 fflush(_coll_fp);
564 if (n_child_run(ok_vlook(SHELL), 0, fileno(_coll_fp), fileno(nf), "-c",
565 cmd, NULL, NULL, &ws) < 0 || WEXITSTATUS(ws) != 0) {
566 Fclose(nf);
567 rv = n_ERR_CHILD;
568 goto jout;
571 if (fsize(nf) == 0) {
572 n_err(_("No bytes from %s !?\n"), n_shexp_quote_cp(cmd, FAL0));
573 Fclose(nf);
574 rv = n_ERR_NODATA;
575 goto jout;
578 /* Take new files */
579 fseek(nf, 0L, SEEK_END);
580 Fclose(_coll_fp);
581 _coll_fp = nf;
582 jout:
583 safe_signal(SIGINT, sigint);
584 NYD_LEAVE;
585 return rv;
588 static si32_t
589 a_coll_forward(char const *ms, FILE *fp, int f)
591 int *msgvec, rv = 0;
592 struct n_ignore const *itp;
593 char const *tabst;
594 enum sendaction action;
595 NYD_ENTER;
597 msgvec = salloc((size_t)(msgCount + 1) * sizeof *msgvec);
598 if (getmsglist(ms, msgvec, 0) < 0) {
599 rv = n_ERR_NOENT; /* XXX not really, should be handled there! */
600 goto jleave;
602 if (*msgvec == 0) {
603 *msgvec = first(0, MMNORM);
604 if (*msgvec == 0) {
605 n_err(_("No appropriate messages\n"));
606 rv = n_ERR_NOENT;
607 goto jleave;
609 msgvec[1] = 0;
612 if (f == 'f' || f == 'F' || f == 'u')
613 tabst = NULL;
614 else if ((tabst = ok_vlook(indentprefix)) == NULL)
615 tabst = INDENT_DEFAULT;
616 if (f == 'u' || f == 'U')
617 itp = n_IGNORE_ALL;
618 else
619 itp = upperchar(f) ? NULL : n_IGNORE_TYPE;
620 action = (upperchar(f) && f != 'U') ? SEND_QUOTE_ALL : SEND_QUOTE;
622 fprintf(n_stdout, _("Interpolating:"));
623 srelax_hold();
624 for (; *msgvec != 0; ++msgvec) {
625 struct message *mp = message + *msgvec - 1;
627 touch(mp);
628 fprintf(n_stdout, " %d", *msgvec);
629 fflush(n_stdout);
630 if (sendmp(mp, fp, itp, tabst, action, NULL) < 0) {
631 n_perr(_("temporary mail file"), 0);
632 rv = n_ERR_IO;
633 break;
635 srelax();
637 srelax_rele();
638 fprintf(n_stdout, "\n");
639 jleave:
640 NYD_LEAVE;
641 return rv;
644 static bool_t
645 a_collect_plumbing(char const *ms, struct header *hp){
646 /* TODO _collect_plumbing: instead of fields the basic headers should
647 * TODO be in an array and have IDs, like in termcap etc., so then this
648 * TODO could be simplified as table-walks. Also true for arg-checks! */
649 bool_t rv;
650 char const *cp, *cmd[4];
651 NYD2_ENTER;
653 /* Protcol version for *on-compose-splice** -- update manual on change! */
654 #define a_COLL_PLUMBING_VERSION "0 0 1"
655 cp = ms;
657 /* C99 */{
658 size_t i;
660 for(i = 0; i < n_NELEM(cmd); ++i){ /* TODO trim+strlist_split(_ifs?)() */
661 while(blankchar(*cp))
662 ++cp;
663 if(*cp == '\0')
664 cmd[i] = NULL;
665 else{
666 if(i < n_NELEM(cmd) - 1)
667 for(cmd[i] = cp++; *cp != '\0' && !blankchar(*cp); ++cp)
669 else{
670 /* Last slot takes all the rest of the line, less trailing WS */
671 for(cmd[i] = cp++; *cp != '\0'; ++cp)
673 while(blankchar(cp[-1]))
674 --cp;
676 cmd[i] = savestrbuf(cmd[i], PTR2SIZE(cp - cmd[i]));
681 if(n_UNLIKELY(cmd[0] == NULL))
682 goto jecmd;
683 if(is_asccaseprefix(cmd[0], "header"))
684 rv = a_collect__plumb_header(cp, hp, cmd);
685 else if(is_asccaseprefix(cmd[0], "attachment"))
686 rv = a_collect__plumb_attach(cp, hp, cmd);
687 else{
688 jecmd:
689 fputs("500\n", n_stdout);
690 rv = FAL0;
692 fflush(n_stdout);
694 NYD2_LEAVE;
695 return rv;
698 static bool_t
699 a_collect__plumb_header(char const *cp, struct header *hp,
700 char const *cmd[4]){
701 uiz_t i;
702 struct n_header_field *hfp;
703 struct name *np, **npp;
704 NYD2_ENTER;
706 if(cmd[1] == NULL)
707 goto jdefault;
709 if(is_asccaseprefix(cmd[1], "insert")){ /* TODO LOGIC BELONGS head.c
710 * TODO That is: Header::factory(string) -> object (blahblah).
711 * TODO I.e., as long as we don't have regular RFC compliant parsers
712 * TODO which differentiate in between structured and unstructured
713 * TODO header fields etc., a little workaround */
714 struct name *xnp;
715 si8_t aerr;
716 enum expand_addr_check_mode eacm;
717 enum gfield ntype;
718 bool_t mult_ok;
720 if(cmd[2] == NULL || cmd[3] == NULL)
721 goto jecmd;
723 /* Strip [\r\n] which would render a body invalid XXX all controls? */
724 /* C99 */{
725 char *xp, c;
727 cmd[3] = xp = savestr(cmd[3]);
728 for(; (c = *xp) != '\0'; ++xp)
729 if(c == '\n' || c == '\r')
730 *xp = ' ';
733 if(!asccasecmp(cmd[2], cp = "Subject")){
734 if(cmd[3][0] != '\0'){
735 if(hp->h_subject != NULL)
736 hp->h_subject = savecatsep(hp->h_subject, ' ', cmd[3]);
737 else
738 hp->h_subject = n_UNCONST(cmd[3]);
739 fprintf(n_stdout, "210 %s 1\n", cp);
740 goto jleave;
741 }else
742 goto j501cp;
745 mult_ok = TRU1;
746 ntype = GEXTRA | GFULL | GFULLEXTRA;
747 eacm = EACM_STRICT;
749 if(!asccasecmp(cmd[2], cp = "From")){
750 npp = &hp->h_from;
751 jins:
752 aerr = 0;
753 if((np = lextract(cmd[3], ntype)) == NULL)
754 goto j501cp;
756 if((np = checkaddrs(np, eacm, &aerr), aerr != 0)){
757 fprintf(n_stdout, "505 %s\n", cp);
758 goto jleave;
761 /* Go to the end of the list, track whether it contains any
762 * non-deleted entries */
763 i = 0;
764 if((xnp = *npp) != NULL)
765 for(;; xnp = xnp->n_flink){
766 if(!(xnp->n_type & GDEL))
767 ++i;
768 if(xnp->n_flink == NULL)
769 break;
772 if(!mult_ok && (i != 0 || np->n_flink != NULL))
773 fprintf(n_stdout, "506 %s\n", cp);
774 else{
775 if(xnp == NULL)
776 *npp = np;
777 else
778 xnp->n_flink = np;
779 np->n_blink = xnp;
780 fprintf(n_stdout, "210 %s %" PRIuZ "\n", cp, ++i);
782 goto jleave;
784 if(!asccasecmp(cmd[2], cp = "Sender")){
785 mult_ok = FAL0;
786 npp = &hp->h_sender;
787 goto jins;
789 if(!asccasecmp(cmd[2], cp = "To")){
790 npp = &hp->h_to;
791 ntype = GTO | GFULL;
792 eacm = EACM_NORMAL | EAF_NAME;
793 goto jins;
795 if(!asccasecmp(cmd[2], cp = "Cc")){
796 npp = &hp->h_cc;
797 ntype = GCC | GFULL;
798 eacm = EACM_NORMAL | EAF_NAME;
799 goto jins;
801 if(!asccasecmp(cmd[2], cp = "Bcc")){
802 npp = &hp->h_bcc;
803 ntype = GBCC | GFULL;
804 eacm = EACM_NORMAL | EAF_NAME;
805 goto jins;
807 if(!asccasecmp(cmd[2], cp = "Reply-To")){
808 npp = &hp->h_reply_to;
809 eacm = EACM_NONAME;
810 goto jins;
812 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
813 npp = &hp->h_mft;
814 eacm = EACM_NONAME;
815 goto jins;
817 if(!asccasecmp(cmd[2], cp = "Message-ID")){
818 mult_ok = FAL0;
819 npp = &hp->h_message_id;
820 ntype = GREF;
821 eacm = EACM_NONAME;
822 goto jins;
824 if(!asccasecmp(cmd[2], cp = "References")){
825 npp = &hp->h_ref;
826 ntype = GREF;
827 eacm = EACM_NONAME;
828 goto jins;
830 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
831 npp = &hp->h_in_reply_to;
832 ntype = GREF;
833 eacm = EACM_NONAME;
834 goto jins;
837 if(!asccasecmp(cmd[2], cp = "Mailx-Command") ||
838 !asccasecmp(cmd[2], cp = "Mailx-Raw-To") ||
839 !asccasecmp(cmd[2], cp = "Mailx-Raw-Cc") ||
840 !asccasecmp(cmd[2], cp = "Mailx-Raw-Bcc") ||
841 !asccasecmp(cmd[2], cp = "Mailx-Orig-From") ||
842 !asccasecmp(cmd[2], cp = "Mailx-Orig-To") ||
843 !asccasecmp(cmd[2], cp = "Mailx-Orig-Cc") ||
844 !asccasecmp(cmd[2], cp = "Mailx-Orig-Bcc")){
845 fprintf(n_stdout, "505 %s\n", cp);
846 goto jleave;
849 /* Free-form header fields */
850 /* C99 */{
851 size_t nl, bl;
852 struct n_header_field **hfpp;
854 for(cp = cmd[2]; *cp != '\0'; ++cp)
855 if(!fieldnamechar(*cp)){
856 cp = cmd[2];
857 goto j501cp;
860 for(i = 0, hfpp = &hp->h_user_headers; *hfpp != NULL; ++i)
861 hfpp = &(*hfpp)->hf_next;
863 nl = strlen(cp = cmd[2]) +1;
864 bl = strlen(cmd[3]) +1;
865 *hfpp = hfp = salloc(n_VSTRUCT_SIZEOF(struct n_header_field, hf_dat
866 ) + nl + bl);
867 hfp->hf_next = NULL;
868 hfp->hf_nl = nl - 1;
869 hfp->hf_bl = bl - 1;
870 memcpy(&hfp->hf_dat[0], cp, nl);
871 memcpy(&hfp->hf_dat[nl], cmd[3], bl);
872 fprintf(n_stdout, "210 %s %" PRIuZ "\n", &hfp->hf_dat[0], ++i);
874 goto jleave;
877 if(is_asccaseprefix(cmd[1], "list")){
878 jdefault:
879 if(cmd[2] == NULL){
880 fputs("210", n_stdout);
881 if(hp->h_subject != NULL) fputs(" Subject", n_stdout);
882 if(hp->h_from != NULL) fputs(" From", n_stdout);
883 if(hp->h_sender != NULL) fputs(" Sender", n_stdout);
884 if(hp->h_to != NULL) fputs(" To", n_stdout);
885 if(hp->h_cc != NULL) fputs(" Cc", n_stdout);
886 if(hp->h_bcc != NULL) fputs(" Bcc", n_stdout);
887 if(hp->h_reply_to != NULL) fputs(" Reply-To", n_stdout);
888 if(hp->h_mft != NULL) fputs(" Mail-Followup-To", n_stdout);
889 if(hp->h_message_id != NULL) fputs(" Message-ID", n_stdout);
890 if(hp->h_ref != NULL) fputs(" References", n_stdout);
891 if(hp->h_in_reply_to != NULL) fputs(" In-Reply-To", n_stdout);
892 if(hp->h_mailx_command != NULL) fputs(" Mailx-Command", n_stdout);
893 if(hp->h_mailx_raw_to != NULL) fputs(" Mailx-Raw-To", n_stdout);
894 if(hp->h_mailx_raw_cc != NULL) fputs(" Mailx-Raw-Cc", n_stdout);
895 if(hp->h_mailx_raw_bcc != NULL) fputs(" Mailx-Raw-Bcc", n_stdout);
896 if(hp->h_mailx_orig_from != NULL) fputs(" Mailx-Orig-From", n_stdout);
897 if(hp->h_mailx_orig_to != NULL) fputs(" Mailx-Orig-To", n_stdout);
898 if(hp->h_mailx_orig_cc != NULL) fputs(" Mailx-Orig-Cc", n_stdout);
899 if(hp->h_mailx_orig_bcc != NULL) fputs(" Mailx-Orig-Bcc", n_stdout);
901 /* Print only one instance of each free-form header */
902 for(hfp = hp->h_user_headers; hfp != NULL; hfp = hfp->hf_next){
903 struct n_header_field *hfpx;
905 for(hfpx = hp->h_user_headers;; hfpx = hfpx->hf_next)
906 if(hfpx == hfp){
907 putc(' ', n_stdout);
908 fputs(&hfp->hf_dat[0], n_stdout);
909 break;
910 }else if(!asccasecmp(&hfpx->hf_dat[0], &hfp->hf_dat[0]))
911 break;
913 putc('\n', n_stdout);
914 goto jleave;
917 if(cmd[3] != NULL)
918 goto jecmd;
920 if(!asccasecmp(cmd[2], cp = "Subject")){
921 np = (hp->h_subject != NULL) ? (struct name*)-1 : NULL;
922 goto jlist;
924 if(!asccasecmp(cmd[2], cp = "From")){
925 np = hp->h_from;
926 jlist:
927 fprintf(n_stdout, "%s %s\n", (np == NULL ? "501" : "210"), cp);
928 goto jleave;
930 if(!asccasecmp(cmd[2], cp = "Sender")){
931 np = hp->h_sender;
932 goto jlist;
934 if(!asccasecmp(cmd[2], cp = "To")){
935 np = hp->h_to;
936 goto jlist;
938 if(!asccasecmp(cmd[2], cp = "Cc")){
939 np = hp->h_cc;
940 goto jlist;
942 if(!asccasecmp(cmd[2], cp = "Bcc")){
943 np = hp->h_bcc;
944 goto jlist;
946 if(!asccasecmp(cmd[2], cp = "Reply-To")){
947 np = hp->h_reply_to;
948 goto jlist;
950 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
951 np = hp->h_mft;
952 goto jlist;
954 if(!asccasecmp(cmd[2], cp = "Message-ID")){
955 np = hp->h_message_id;
956 goto jlist;
958 if(!asccasecmp(cmd[2], cp = "References")){
959 np = hp->h_ref;
960 goto jlist;
962 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
963 np = hp->h_in_reply_to;
964 goto jlist;
967 if(!asccasecmp(cmd[2], cp = "Mailx-Command")){
968 np = (hp->h_mailx_command != NULL) ? (struct name*)-1 : NULL;
969 goto jlist;
971 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-To")){
972 np = hp->h_mailx_raw_to;
973 goto jlist;
975 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-Cc")){
976 np = hp->h_mailx_raw_cc;
977 goto jlist;
979 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-Bcc")){
980 np = hp->h_mailx_raw_bcc;
981 goto jlist;
983 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-From")){
984 np = hp->h_mailx_orig_from;
985 goto jlist;
987 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-To")){
988 np = hp->h_mailx_orig_to;
989 goto jlist;
991 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-Cc")){
992 np = hp->h_mailx_orig_cc;
993 goto jlist;
995 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-Bcc")){
996 np = hp->h_mailx_orig_bcc;
997 goto jlist;
1000 /* Free-form header fields */
1001 for(cp = cmd[2]; *cp != '\0'; ++cp)
1002 if(!fieldnamechar(*cp)){
1003 cp = cmd[2];
1004 goto j501cp;
1006 cp = cmd[2];
1007 for(hfp = hp->h_user_headers;; hfp = hfp->hf_next){
1008 if(hfp == NULL)
1009 goto j501cp;
1010 else if(!asccasecmp(cp, &hfp->hf_dat[0])){
1011 fprintf(n_stdout, "210 %s\n", &hfp->hf_dat[0]);
1012 break;
1015 goto jleave;
1018 if(is_asccaseprefix(cmd[1], "remove")){
1019 if(cmd[2] == NULL || cmd[3] != NULL)
1020 goto jecmd;
1022 if(!asccasecmp(cmd[2], cp = "Subject")){
1023 if(hp->h_subject != NULL){
1024 hp->h_subject = NULL;
1025 fprintf(n_stdout, "210 %s\n", cp);
1026 goto jleave;
1027 }else
1028 goto j501cp;
1031 if(!asccasecmp(cmd[2], cp = "From")){
1032 npp = &hp->h_from;
1033 jrem:
1034 if(*npp != NULL){
1035 *npp = NULL;
1036 fprintf(n_stdout, "210 %s\n", cp);
1037 goto jleave;
1038 }else
1039 goto j501cp;
1041 if(!asccasecmp(cmd[2], cp = "Sender")){
1042 npp = &hp->h_sender;
1043 goto jrem;
1045 if(!asccasecmp(cmd[2], cp = "To")){
1046 npp = &hp->h_to;
1047 goto jrem;
1049 if(!asccasecmp(cmd[2], cp = "Cc")){
1050 npp = &hp->h_cc;
1051 goto jrem;
1053 if(!asccasecmp(cmd[2], cp = "Bcc")){
1054 npp = &hp->h_bcc;
1055 goto jrem;
1057 if(!asccasecmp(cmd[2], cp = "Reply-To")){
1058 npp = &hp->h_reply_to;
1059 goto jrem;
1061 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
1062 npp = &hp->h_mft;
1063 goto jrem;
1065 if(!asccasecmp(cmd[2], cp = "Message-ID")){
1066 npp = &hp->h_message_id;
1067 goto jrem;
1069 if(!asccasecmp(cmd[2], cp = "References")){
1070 npp = &hp->h_ref;
1071 goto jrem;
1073 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
1074 npp = &hp->h_in_reply_to;
1075 goto jrem;
1078 if(!asccasecmp(cmd[2], cp = "Mailx-Command") ||
1079 !asccasecmp(cmd[2], cp = "Mailx-Raw-To") ||
1080 !asccasecmp(cmd[2], cp = "Mailx-Raw-Cc") ||
1081 !asccasecmp(cmd[2], cp = "Mailx-Raw-Bcc") ||
1082 !asccasecmp(cmd[2], cp = "Mailx-Orig-From") ||
1083 !asccasecmp(cmd[2], cp = "Mailx-Orig-To") ||
1084 !asccasecmp(cmd[2], cp = "Mailx-Orig-Cc") ||
1085 !asccasecmp(cmd[2], cp = "Mailx-Orig-Bcc")){
1086 fprintf(n_stdout, "505 %s\n", cp);
1087 goto jleave;
1090 /* Free-form header fields (note j501cp may print non-normalized name) */
1091 /* C99 */{
1092 struct n_header_field **hfpp;
1093 bool_t any;
1095 for(cp = cmd[2]; *cp != '\0'; ++cp)
1096 if(!fieldnamechar(*cp)){
1097 cp = cmd[2];
1098 goto j501cp;
1100 cp = cmd[2];
1102 for(any = FAL0, hfpp = &hp->h_user_headers; (hfp = *hfpp) != NULL;){
1103 if(!asccasecmp(cp, &hfp->hf_dat[0])){
1104 *hfpp = hfp->hf_next;
1105 if(!any)
1106 fprintf(n_stdout, "210 %s\n", &hfp->hf_dat[0]);
1107 any = TRU1;
1108 }else
1109 hfpp = &hfp->hf_next;
1111 if(!any)
1112 goto j501cp;
1114 goto jleave;
1117 if(is_asccaseprefix(cmd[1], "remove-at")){
1118 if(cmd[2] == NULL || cmd[3] == NULL)
1119 goto jecmd;
1121 if((n_idec_uiz_cp(&i, cmd[3], 0, NULL
1122 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
1123 ) != n_IDEC_STATE_CONSUMED || i == 0){
1124 fputs("505\n", n_stdout);
1125 goto jleave;
1128 if(!asccasecmp(cmd[2], cp = "Subject")){
1129 if(hp->h_subject != NULL && i == 1){
1130 hp->h_subject = NULL;
1131 fprintf(n_stdout, "210 %s 1\n", cp);
1132 goto jleave;
1133 }else
1134 goto j501cp;
1137 if(!asccasecmp(cmd[2], cp = "From")){
1138 npp = &hp->h_from;
1139 jremat:
1140 if((np = *npp) == NULL)
1141 goto j501cp;
1142 while(--i != 0 && np != NULL)
1143 np = np->n_flink;
1144 if(np == NULL)
1145 goto j501cp;
1147 if(np->n_blink != NULL)
1148 np->n_blink->n_flink = np->n_flink;
1149 else
1150 *npp = np->n_flink;
1151 if(np->n_flink != NULL)
1152 np->n_flink->n_blink = np->n_blink;
1154 fprintf(n_stdout, "210 %s\n", cp);
1155 goto jleave;
1157 if(!asccasecmp(cmd[2], cp = "Sender")){
1158 npp = &hp->h_sender;
1159 goto jremat;
1161 if(!asccasecmp(cmd[2], cp = "To")){
1162 npp = &hp->h_to;
1163 goto jremat;
1165 if(!asccasecmp(cmd[2], cp = "Cc")){
1166 npp = &hp->h_cc;
1167 goto jremat;
1169 if(!asccasecmp(cmd[2], cp = "Bcc")){
1170 npp = &hp->h_bcc;
1171 goto jremat;
1173 if(!asccasecmp(cmd[2], cp = "Reply-To")){
1174 npp = &hp->h_reply_to;
1175 goto jremat;
1177 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
1178 npp = &hp->h_mft;
1179 goto jremat;
1181 if(!asccasecmp(cmd[2], cp = "Message-ID")){
1182 npp = &hp->h_message_id;
1183 goto jremat;
1185 if(!asccasecmp(cmd[2], cp = "References")){
1186 npp = &hp->h_ref;
1187 goto jremat;
1189 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
1190 npp = &hp->h_in_reply_to;
1191 goto jremat;
1194 if(!asccasecmp(cmd[2], cp = "Mailx-Command") ||
1195 !asccasecmp(cmd[2], cp = "Mailx-Raw-To") ||
1196 !asccasecmp(cmd[2], cp = "Mailx-Raw-Cc") ||
1197 !asccasecmp(cmd[2], cp = "Mailx-Raw-Bcc") ||
1198 !asccasecmp(cmd[2], cp = "Mailx-Orig-From") ||
1199 !asccasecmp(cmd[2], cp = "Mailx-Orig-To") ||
1200 !asccasecmp(cmd[2], cp = "Mailx-Orig-Cc") ||
1201 !asccasecmp(cmd[2], cp = "Mailx-Orig-Bcc")){
1202 fprintf(n_stdout, "505 %s\n", cp);
1203 goto jleave;
1206 /* Free-form header fields */
1207 /* C99 */{
1208 struct n_header_field **hfpp;
1210 for(cp = cmd[2]; *cp != '\0'; ++cp)
1211 if(!fieldnamechar(*cp)){
1212 cp = cmd[2];
1213 goto j501cp;
1215 cp = cmd[2];
1217 for(hfpp = &hp->h_user_headers; (hfp = *hfpp) != NULL;){
1218 if(--i == 0){
1219 *hfpp = hfp->hf_next;
1220 fprintf(n_stdout, "210 %s %" PRIuZ "\n", &hfp->hf_dat[0], i);
1221 break;
1222 }else
1223 hfpp = &hfp->hf_next;
1225 if(hfp == NULL)
1226 goto j501cp;
1228 goto jleave;
1231 if(is_asccaseprefix(cmd[1], "show")){
1232 if(cmd[2] == NULL || cmd[3] != NULL)
1233 goto jecmd;
1235 if(!asccasecmp(cmd[2], cp = "Subject")){
1236 if(hp->h_subject == NULL)
1237 goto j501cp;
1238 fprintf(n_stdout, "212 %s\n%s\n\n", cp, hp->h_subject);
1239 goto jleave;
1242 if(!asccasecmp(cmd[2], cp = "From")){
1243 np = hp->h_from;
1244 jshow:
1245 if(np != NULL){
1246 fprintf(n_stdout, "211 %s\n", cp);
1247 do if(!(np->n_type & GDEL))
1248 fprintf(n_stdout, "%s %s\n", np->n_name, np->n_fullname);
1249 while((np = np->n_flink) != NULL);
1250 putc('\n', n_stdout);
1251 goto jleave;
1252 }else
1253 goto j501cp;
1255 if(!asccasecmp(cmd[2], cp = "Sender")){
1256 np = hp->h_sender;
1257 goto jshow;
1259 if(!asccasecmp(cmd[2], cp = "To")){
1260 np = hp->h_to;
1261 goto jshow;
1263 if(!asccasecmp(cmd[2], cp = "Cc")){
1264 np = hp->h_cc;
1265 goto jshow;
1267 if(!asccasecmp(cmd[2], cp = "Bcc")){
1268 np = hp->h_bcc;
1269 goto jshow;
1271 if(!asccasecmp(cmd[2], cp = "Reply-To")){
1272 np = hp->h_reply_to;
1273 goto jshow;
1275 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
1276 np = hp->h_mft;
1277 goto jshow;
1279 if(!asccasecmp(cmd[2], cp = "Message-ID")){
1280 np = hp->h_message_id;
1281 goto jshow;
1283 if(!asccasecmp(cmd[2], cp = "References")){
1284 np = hp->h_ref;
1285 goto jshow;
1287 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
1288 np = hp->h_in_reply_to;
1289 goto jshow;
1292 if(!asccasecmp(cmd[2], cp = "Mailx-Command")){
1293 if(hp->h_mailx_command == NULL)
1294 goto j501cp;
1295 fprintf(n_stdout, "212 %s\n%s\n\n", cp, hp->h_mailx_command);
1296 goto jleave;
1298 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-To")){
1299 np = hp->h_mailx_raw_to;
1300 goto jshow;
1302 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-Cc")){
1303 np = hp->h_mailx_raw_cc;
1304 goto jshow;
1306 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-Bcc")){
1307 np = hp->h_mailx_raw_bcc;
1308 goto jshow;
1310 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-From")){
1311 np = hp->h_mailx_orig_from;
1312 goto jshow;
1314 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-To")){
1315 np = hp->h_mailx_orig_to;
1316 goto jshow;
1318 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-Cc")){
1319 np = hp->h_mailx_orig_cc;
1320 goto jshow;
1322 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-Bcc")){
1323 np = hp->h_mailx_orig_bcc;
1324 goto jshow;
1327 /* Free-form header fields */
1328 /* C99 */{
1329 bool_t any;
1331 for(cp = cmd[2]; *cp != '\0'; ++cp)
1332 if(!fieldnamechar(*cp)){
1333 cp = cmd[2];
1334 goto j501cp;
1336 cp = cmd[2];
1338 for(any = FAL0, hfp = hp->h_user_headers; hfp != NULL;
1339 hfp = hfp->hf_next){
1340 if(!asccasecmp(cp, &hfp->hf_dat[0])){
1341 if(!any)
1342 fprintf(n_stdout, "212 %s\n", &hfp->hf_dat[0]);
1343 any = TRU1;
1344 fprintf(n_stdout, "%s\n", &hfp->hf_dat[hfp->hf_nl +1]);
1347 if(any)
1348 putc('\n', n_stdout);
1349 else
1350 goto j501cp;
1352 goto jleave;
1355 jecmd:
1356 fputs("500\n", n_stdout);
1357 cp = NULL;
1358 jleave:
1359 NYD2_LEAVE;
1360 return (cp != NULL);
1362 j501cp:
1363 fputs("501 ", n_stdout);
1364 fputs(cp, n_stdout);
1365 putc('\n', n_stdout);
1366 goto jleave;
1369 static bool_t
1370 a_collect__plumb_attach(char const *cp, struct header *hp,
1371 char const *cmd[4]){
1372 bool_t status;
1373 struct attachment *ap;
1374 NYD2_ENTER;
1376 if(cmd[1] == NULL)
1377 goto jdefault;
1379 if(is_asccaseprefix(cmd[1], "attribute")){
1380 if(cmd[2] == NULL || cmd[3] != NULL)
1381 goto jecmd;
1383 if((ap = n_attachment_find(hp->h_attach, cmd[2], NULL)) != NULL){
1384 jatt_att:
1385 fprintf(n_stdout, "212 %s\n", cmd[2]);
1386 if(ap->a_msgno > 0)
1387 fprintf(n_stdout, "message-number %d\n\n", ap->a_msgno);
1388 else{
1389 fprintf(n_stdout,
1390 "creation-name %s\nopen-path %s\nfilename %s\n",
1391 ap->a_path_user, ap->a_path, ap->a_name);
1392 if(ap->a_content_description != NULL)
1393 fprintf(n_stdout, "content-description %s\n",
1394 ap->a_content_description);
1395 if(ap->a_content_id != NULL)
1396 fprintf(n_stdout, "content-id %s\n",
1397 ap->a_content_id->n_name);
1398 if(ap->a_content_type != NULL)
1399 fprintf(n_stdout, "content-type %s\n", ap->a_content_type);
1400 if(ap->a_content_disposition != NULL)
1401 fprintf(n_stdout, "content-disposition %s\n",
1402 ap->a_content_disposition);
1403 putc('\n', n_stdout);
1405 }else
1406 fputs("501\n", n_stdout);
1407 goto jleave;
1410 if(is_asccaseprefix(cmd[1], "attribute-at")){
1411 uiz_t i;
1413 if(cmd[2] == NULL || cmd[3] != NULL)
1414 goto jecmd;
1416 if((n_idec_uiz_cp(&i, cmd[2], 0, NULL
1417 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
1418 ) != n_IDEC_STATE_CONSUMED || i == 0)
1419 fputs("505\n", n_stdout);
1420 else{
1421 for(ap = hp->h_attach; ap != NULL && --i != 0; ap = ap->a_flink)
1423 if(ap != NULL)
1424 goto jatt_att;
1425 else
1426 fputs("501\n", n_stdout);
1428 goto jleave;
1431 if(is_asccaseprefix(cmd[1], "attribute-set")){
1432 if(cmd[2] == NULL || cmd[3] == NULL)
1433 goto jecmd;
1435 if((ap = n_attachment_find(hp->h_attach, cmd[2], NULL)) != NULL){
1436 jatt_attset:
1437 if(ap->a_msgno > 0)
1438 fputs("505\n", n_stdout);
1439 else{
1440 char c, *keyw;
1442 cp = cmd[3];
1443 while((c = *cp) != '\0' && !blankchar(c))
1444 ++cp;
1445 keyw = savestrbuf(cmd[3], PTR2SIZE(cp - cmd[3]));
1446 if(c != '\0'){
1447 for(; (c = *++cp) != '\0' && blankchar(c);)
1449 if(c != '\0'){
1450 char *xp;
1452 /* Strip [\r\n] which would render a parameter invalid XXX
1453 * XXX all controls? */
1454 cp = xp = savestr(cp);
1455 for(; (c = *xp) != '\0'; ++xp)
1456 if(c == '\n' || c == '\r')
1457 *xp = ' ';
1458 c = *cp;
1462 if(!asccasecmp(keyw, "filename"))
1463 ap->a_name = (c == '\0') ? ap->a_path_bname : cp;
1464 else if(!asccasecmp(keyw, "content-description"))
1465 ap->a_content_description = (c == '\0') ? NULL : cp;
1466 else if(!asccasecmp(keyw, "content-id")){
1467 ap->a_content_id = NULL;
1469 if(c != '\0'){
1470 struct name *np;
1472 np = checkaddrs(lextract(cp, GREF),
1473 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG |
1474 EACM_NONAME, NULL);
1475 if(np != NULL && np->n_flink == NULL)
1476 ap->a_content_id = np;
1477 else
1478 cp = NULL;
1480 }else if(!asccasecmp(keyw, "content-type"))
1481 ap->a_content_type = (c == '\0') ? NULL : cp;
1482 else if(!asccasecmp(keyw, "content-disposition"))
1483 ap->a_content_disposition = (c == '\0') ? NULL : cp;
1484 else
1485 cp = NULL;
1487 if(cp != NULL){
1488 size_t i;
1490 for(i = 0; ap != NULL; ++i, ap = ap->a_blink)
1492 fprintf(n_stdout, "210 %" PRIuZ "\n", i);
1493 }else
1494 fputs("505\n", n_stdout);
1496 }else
1497 fputs("501\n", n_stdout);
1498 goto jleave;
1501 if(is_asccaseprefix(cmd[1], "attribute-set-at")){
1502 uiz_t i;
1504 if(cmd[2] == NULL || cmd[3] == NULL)
1505 goto jecmd;
1507 if((n_idec_uiz_cp(&i, cmd[2], 0, NULL
1508 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
1509 ) != n_IDEC_STATE_CONSUMED || i == 0)
1510 fputs("505\n", n_stdout);
1511 else{
1512 for(ap = hp->h_attach; ap != NULL && --i != 0; ap = ap->a_flink)
1514 if(ap != NULL)
1515 goto jatt_attset;
1516 else
1517 fputs("501\n", n_stdout);
1519 goto jleave;
1522 if(is_asccaseprefix(cmd[1], "insert")){
1523 enum n_attach_error aerr;
1525 if(cmd[2] == NULL || cmd[3] != NULL)
1526 goto jecmd;
1528 hp->h_attach = n_attachment_append(hp->h_attach, cmd[2], &aerr, &ap);
1529 switch(aerr){
1530 case n_ATTACH_ERR_FILE_OPEN: cp = "505\n"; goto jatt_ins;
1531 case n_ATTACH_ERR_ICONV_FAILED: cp = "506\n"; goto jatt_ins;
1532 case n_ATTACH_ERR_ICONV_NAVAIL:
1533 case n_ATTACH_ERR_OTHER:
1534 default:
1535 cp = "501\n";
1536 jatt_ins:
1537 fputs(cp, n_stdout);
1538 break;
1539 case n_ATTACH_ERR_NONE:{
1540 size_t i;
1542 for(i = 0; ap != NULL; ++i, ap = ap->a_blink)
1544 fprintf(n_stdout, "210 %" PRIuZ "\n", i);
1545 } break;
1547 goto jleave;
1550 if(is_asccaseprefix(cmd[1], "list")){
1551 jdefault:
1552 if(cmd[2] != NULL)
1553 goto jecmd;
1555 if((ap = hp->h_attach) != NULL){
1556 fputs("212\n", n_stdout);
1558 fprintf(n_stdout, "%s\n", ap->a_path_user);
1559 while((ap = ap->a_flink) != NULL);
1560 putc('\n', n_stdout);
1561 }else
1562 fputs("501\n", n_stdout);
1563 goto jleave;
1566 if(is_asccaseprefix(cmd[1], "remove")){
1567 if(cmd[2] == NULL || cmd[3] != NULL)
1568 goto jecmd;
1570 if((ap = n_attachment_find(hp->h_attach, cmd[2], &status)) != NULL){
1571 if(status == TRUM1)
1572 fputs("506\n", n_stdout);
1573 else{
1574 hp->h_attach = n_attachment_remove(hp->h_attach, ap);
1575 fprintf(n_stdout, "210 %s\n", cmd[2]);
1577 }else
1578 fputs("501\n", n_stdout);
1579 goto jleave;
1582 if(is_asccaseprefix(cmd[1], "remove-at")){
1583 uiz_t i;
1585 if(cmd[2] == NULL || cmd[3] != NULL)
1586 goto jecmd;
1588 if((n_idec_uiz_cp(&i, cmd[2], 0, NULL
1589 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
1590 ) != n_IDEC_STATE_CONSUMED || i == 0)
1591 fputs("505\n", n_stdout);
1592 else{
1593 for(ap = hp->h_attach; ap != NULL && --i != 0; ap = ap->a_flink)
1595 if(ap != NULL){
1596 hp->h_attach = n_attachment_remove(hp->h_attach, ap);
1597 fprintf(n_stdout, "210 %s\n", cmd[2]);
1598 }else
1599 fputs("501\n", n_stdout);
1601 goto jleave;
1604 jecmd:
1605 fputs("500\n", n_stdout);
1606 cp = NULL;
1607 jleave:
1608 NYD2_LEAVE;
1609 return (cp != NULL);
1612 static void
1613 _collint(int s)
1615 NYD_X; /* Signal handler */
1617 /* the control flow is subtle, because we can be called from ~q */
1618 if (_coll_hadintr == 0) {
1619 if (ok_blook(ignore)) {
1620 fputs("@\n", n_stdout);
1621 fflush(n_stdout);
1622 clearerr(n_stdin);
1623 } else
1624 _coll_hadintr = 1;
1625 siglongjmp(_coll_jmp, 1);
1627 n_exit_status |= n_EXIT_SEND_ERROR;
1628 if (s != 0)
1629 savedeadletter(_coll_fp, TRU1);
1630 /* Aborting message, no need to fflush() .. */
1631 siglongjmp(_coll_abort, 1);
1634 static void
1635 collhup(int s)
1637 NYD_X; /* Signal handler */
1638 n_UNUSED(s);
1640 savedeadletter(_coll_fp, TRU1);
1641 /* Let's pretend nobody else wants to clean up, a true statement at
1642 * this time */
1643 exit(n_EXIT_ERR);
1646 static bool_t
1647 a_coll_putesc(char const *s, bool_t addnl, FILE *stream){
1648 char c1, c2;
1649 bool_t isposix;
1650 NYD2_ENTER;
1652 isposix = ok_blook(posix);
1654 while((c1 = *s++) != '\0'){
1655 if(c1 == '\\' && ((c2 = *s) == 't' || c2 == 'n')){
1656 if(!isposix){
1657 isposix = TRU1; /* TODO v15 OBSOLETE! */
1658 n_err(_("Compose mode warning: expanding \\t or \\n in variable "
1659 "without *posix*!"
1660 "\n Support remains only for ~A,~a,~I,~i in *posix* mode!\n"));
1662 ++s;
1663 c1 = (c2 == 't') ? '\t' : '\n';
1666 if(putc(c1, stream) == EOF)
1667 goto jleave;
1670 if(addnl && putc('\n', stream) == EOF)
1671 goto jleave;
1673 jleave:
1674 NYD2_LEAVE;
1675 return (c1 == '\0');
1678 static int
1679 a_coll_ocs__mac(void){
1680 /* Executes in a fork(2)ed child TODO if remains, global MASKs for those! */
1681 setvbuf(n_stdin, NULL, _IOLBF, 0);
1682 setvbuf(n_stdout, NULL, _IOLBF, 0);
1683 n_psonce &= ~(n_PSO_INTERACTIVE | n_PSO_TTYIN | n_PSO_TTYOUT);
1684 n_pstate |= n_PS_COMPOSE_FORKHOOK;
1685 n_readctl_overlay = NULL; /* TODO we need OnForkEvent! See c_readctl() */
1686 if(n_poption & n_PO_D_VV){
1687 char buf[128];
1689 snprintf(buf, sizeof buf, "[%d]%s", getpid(), ok_vlook(log_prefix));
1690 ok_vset(log_prefix, buf);
1692 /* TODO If that uses `!' it will effectively SIG_IGN SIGINT, ...and such */
1693 temporary_compose_mode_hook_call(a_coll_ocs__macname, NULL, NULL);
1694 return 0;
1697 static void
1698 a_coll_ocs__finalize(void *vp){
1699 /* Note we use this for destruction upon setup errors, thus */
1700 sighandler_type opipe;
1701 sighandler_type oint;
1702 struct a_coll_ocs_arg **coapp, *coap;
1703 NYD2_ENTER;
1705 temporary_compose_mode_hook_call((char*)-1, NULL, NULL);
1707 coap = *(coapp = vp);
1708 *coapp = (struct a_coll_ocs_arg*)-1;
1710 if(coap->coa_stdin != NULL)
1711 Fclose(coap->coa_stdin);
1712 else if(coap->coa_pipe[0] != -1)
1713 close(coap->coa_pipe[0]);
1715 if(coap->coa_stdout != NULL && !Pclose(coap->coa_stdout, TRU1))
1716 *coap->coa_senderr = 111;
1717 if(coap->coa_pipe[1] != -1)
1718 close(coap->coa_pipe[1]);
1720 opipe = coap->coa_opipe;
1721 oint = coap->coa_oint;
1723 n_lofi_free(coap);
1725 hold_all_sigs();
1726 safe_signal(SIGPIPE, opipe);
1727 safe_signal(SIGINT, oint);
1728 rele_all_sigs();
1729 NYD2_LEAVE;
1732 FL void
1733 n_temporary_compose_hook_varset(void *arg){ /* TODO v15: drop */
1734 struct header *hp;
1735 char const *val;
1736 NYD2_ENTER;
1738 hp = arg;
1740 if((val = hp->h_subject) == NULL)
1741 val = n_empty;
1742 ok_vset(mailx_subject, val);
1743 if((val = detract(hp->h_from, GNAMEONLY)) == NULL)
1744 val = n_empty;
1745 ok_vset(mailx_from, val);
1746 if((val = detract(hp->h_sender, GNAMEONLY)) == NULL)
1747 val = n_empty;
1748 ok_vset(mailx_sender, val);
1749 if((val = detract(hp->h_to, GNAMEONLY)) == NULL)
1750 val = n_empty;
1751 ok_vset(mailx_to, val);
1752 if((val = detract(hp->h_cc, GNAMEONLY)) == NULL)
1753 val = n_empty;
1754 ok_vset(mailx_cc, val);
1755 if((val = detract(hp->h_bcc, GNAMEONLY)) == NULL)
1756 val = n_empty;
1757 ok_vset(mailx_bcc, val);
1759 if((val = hp->h_mailx_command) == NULL)
1760 val = n_empty;
1761 ok_vset(mailx_command, val);
1763 if((val = detract(hp->h_mailx_raw_to, GNAMEONLY)) == NULL)
1764 val = n_empty;
1765 ok_vset(mailx_raw_to, val);
1766 if((val = detract(hp->h_mailx_raw_cc, GNAMEONLY)) == NULL)
1767 val = n_empty;
1768 ok_vset(mailx_raw_cc, val);
1769 if((val = detract(hp->h_mailx_raw_bcc, GNAMEONLY)) == NULL)
1770 val = n_empty;
1771 ok_vset(mailx_raw_bcc, val);
1773 if((val = detract(hp->h_mailx_orig_from, GNAMEONLY)) == NULL)
1774 val = n_empty;
1775 ok_vset(mailx_orig_from, val);
1776 if((val = detract(hp->h_mailx_orig_to, GNAMEONLY)) == NULL)
1777 val = n_empty;
1778 ok_vset(mailx_orig_to, val);
1779 if((val = detract(hp->h_mailx_orig_cc, GNAMEONLY)) == NULL)
1780 val = n_empty;
1781 ok_vset(mailx_orig_cc, val);
1782 if((val = detract(hp->h_mailx_orig_bcc, GNAMEONLY)) == NULL)
1783 val = n_empty;
1784 ok_vset(mailx_orig_bcc, val);
1785 NYD2_LEAVE;
1788 FL FILE *
1789 collect(struct header *hp, int printheaders, struct message *mp,
1790 char const *quotefile, int doprefix, si8_t *checkaddr_err)
1792 struct n_string s, * volatile sp;
1793 struct n_ignore const *quoteitp;
1794 struct a_coll_ocs_arg *coap;
1795 int c;
1796 int volatile t, eofcnt, getfields;
1797 char volatile escape;
1798 enum{
1799 a_NONE,
1800 a_ERREXIT = 1u<<0,
1801 a_IGNERR = 1u<<1,
1802 a_COAP_NOSIGTERM = 1u<<8
1803 #define a_HARDERR() ((flags & (a_ERREXIT | a_IGNERR)) == a_ERREXIT)
1804 } volatile flags;
1805 char *linebuf;
1806 char const *cp, *cp_base, * volatile coapm, * volatile ifs_saved;
1807 size_t i, linesize; /* TODO line pool */
1808 long cnt;
1809 enum sendaction action;
1810 sigset_t oset, nset;
1811 FILE * volatile sigfp;
1812 NYD_ENTER;
1814 _coll_fp = NULL;
1815 sigfp = NULL;
1816 linesize = 0;
1817 linebuf = NULL;
1818 eofcnt = 0;
1819 ifs_saved = coapm = NULL;
1820 coap = NULL;
1821 sp = NULL;
1823 /* Start catching signals from here, but we're still die on interrupts
1824 * until we're in the main loop */
1825 sigfillset(&nset);
1826 sigprocmask(SIG_BLOCK, &nset, &oset);
1827 if ((_coll_saveint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
1828 safe_signal(SIGINT, &_collint);
1829 if ((_coll_savehup = safe_signal(SIGHUP, SIG_IGN)) != SIG_IGN)
1830 safe_signal(SIGHUP, collhup);
1831 if (sigsetjmp(_coll_abort, 1))
1832 goto jerr;
1833 if (sigsetjmp(_coll_jmp, 1))
1834 goto jerr;
1835 n_pstate |= n_PS_COMPOSE_MODE;
1836 sigprocmask(SIG_SETMASK, &oset, (sigset_t*)NULL);
1838 if ((_coll_fp = Ftmp(NULL, "collect", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
1839 NULL) {
1840 n_perr(_("temporary mail file"), 0);
1841 goto jerr;
1844 /* If we are going to prompt for a subject, refrain from printing a newline
1845 * after the headers (since some people mind) */
1846 getfields = 0;
1847 if(!(n_poption & n_PO_t_FLAG)){
1848 t = GTO | GSUBJECT | GCC | GNL;
1849 if(ok_blook(fullnames))
1850 t |= GCOMMA;
1852 if((n_psonce & n_PSO_INTERACTIVE) && !(n_pstate & n_PS_ROBOT)){
1853 if(hp->h_subject == NULL && ok_blook(asksub)/* *ask* auto warped! */)
1854 t &= ~GNL, getfields |= GSUBJECT;
1856 if(hp->h_to == NULL)
1857 t &= ~GNL, getfields |= GTO;
1859 if(!ok_blook(bsdcompat) && !ok_blook(askatend)){
1860 if(ok_blook(askbcc))
1861 t &= ~GNL, getfields |= GBCC;
1862 if(ok_blook(askcc))
1863 t &= ~GNL, getfields |= GCC;
1866 }else{
1867 n_UNINIT(t, 0);
1870 _coll_hadintr = 0;
1872 if (!sigsetjmp(_coll_jmp, 1)) {
1873 /* Ask for some headers first, as necessary */
1874 if (getfields)
1875 grab_headers(n_GO_INPUT_CTX_COMPOSE, hp, getfields, 1);
1877 /* Execute compose-enter; delayed for -t mode */
1878 if(!(n_poption & n_PO_t_FLAG) &&
1879 (cp = ok_vlook(on_compose_enter)) != NULL){
1880 setup_from_and_sender(hp);
1881 temporary_compose_mode_hook_call(cp, &n_temporary_compose_hook_varset,
1882 hp);
1885 /* TODO Mm: nope since it may require turning this into a multipart one */
1886 if(!(n_poption & (n_PO_Mm_FLAG | n_PO_t_FLAG))){
1887 if(!a_coll_message_inject_head(_coll_fp))
1888 goto jerr;
1890 /* Quote an original message */
1891 if (mp != NULL && (doprefix || (cp = ok_vlook(quote)) != NULL)) {
1892 quoteitp = n_IGNORE_ALL;
1893 action = SEND_QUOTE;
1894 if (doprefix) {
1895 char const *cp_v15compat;
1897 quoteitp = n_IGNORE_FWD;
1899 if((cp_v15compat = ok_vlook(fwdheading)) != NULL)
1900 n_OBSOLETE(_("please use *forward-inject-head*, "
1901 "not *fwdheading*"));
1902 cp = ok_vlook(forward_inject_head);
1903 if(cp == NULL && (cp = cp_v15compat) == NULL)
1904 cp = "-------- Original Message --------";
1905 if (*cp != '\0' && fprintf(_coll_fp, "%s\n", cp) < 0)
1906 goto jerr;
1907 } else if (!strcmp(cp, "noheading")) {
1908 /*EMPTY*/;
1909 } else if (!strcmp(cp, "headers")) {
1910 quoteitp = n_IGNORE_TYPE;
1911 } else if (!strcmp(cp, "allheaders")) {
1912 quoteitp = NULL;
1913 action = SEND_QUOTE_ALL;
1914 } else {
1915 cp = hfield1("from", mp);
1916 if (cp != NULL && (cnt = (long)strlen(cp)) > 0) {
1917 if (xmime_write(cp, cnt, _coll_fp, CONV_FROMHDR, TD_NONE,
1918 NULL, NULL) < 0)
1919 goto jerr;
1920 if (fprintf(_coll_fp, _(" wrote:\n\n")) < 0)
1921 goto jerr;
1924 if (fflush(_coll_fp))
1925 goto jerr;
1926 if (doprefix)
1927 cp = NULL;
1928 else if ((cp = ok_vlook(indentprefix)) == NULL)
1929 cp = INDENT_DEFAULT;
1930 if (sendmp(mp, _coll_fp, quoteitp, cp, action, NULL) < 0)
1931 goto jerr;
1935 if (quotefile != NULL) {
1936 if((n_pstate_err_no = a_coll_include_file(quotefile, FAL0, FAL0)
1937 ) != n_ERR_NONE)
1938 goto jerr;
1941 if(n_psonce & n_PSO_INTERACTIVE){
1942 if(!(n_pstate & n_PS_SOURCING)){
1943 sp = n_string_creat_auto(&s);
1944 sp = n_string_reserve(sp, 80);
1947 if(!(n_poption & n_PO_Mm_FLAG) && !(n_pstate & n_PS_ROBOT)){
1948 /* Print what we have sofar also on the terminal (if useful) */
1949 if (!ok_blook(editalong)) {
1950 if (printheaders)
1951 puthead(TRU1, hp, n_stdout, t,
1952 SEND_TODISP, CONV_NONE, NULL, NULL);
1954 rewind(_coll_fp);
1955 while ((c = getc(_coll_fp)) != EOF) /* XXX bytewise, yuck! */
1956 putc(c, n_stdout);
1957 if (fseek(_coll_fp, 0, SEEK_END))
1958 goto jerr;
1959 } else {
1960 rewind(_coll_fp);
1961 if(a_coll_edit('e', hp) != n_ERR_NONE)
1962 goto jerr;
1963 /* Print msg mandated by the Mail Reference Manual */
1964 jcont:
1965 if((n_psonce & n_PSO_INTERACTIVE) && !(n_pstate & n_PS_ROBOT))
1966 fputs(_("(continue)\n"), n_stdout);
1968 fflush(n_stdout);
1971 } else {
1972 /* Come here for printing the after-signal message. Duplicate messages
1973 * won't be printed because the write is aborted if we get a SIGTTOU */
1974 if(_coll_hadintr && (n_psonce & n_PSO_INTERACTIVE) &&
1975 !(n_pstate & n_PS_ROBOT))
1976 n_err(_("\n(Interrupt -- one more to kill letter)\n"));
1979 /* If not under shell hook control */
1980 if(coap == NULL){
1981 /* We're done with -M or -m TODO because: we are too stupid yet, above */
1982 if(n_poption & n_PO_Mm_FLAG)
1983 goto jout;
1984 /* No command escapes, interrupts not expected. Simply copy STDIN */
1985 if(!(n_psonce & n_PSO_INTERACTIVE) &&
1986 !(n_poption & (n_PO_t_FLAG | n_PO_TILDE_FLAG))){
1987 linebuf = srealloc(linebuf, linesize = LINESIZE);
1988 while ((i = fread(linebuf, sizeof *linebuf, linesize, n_stdin)) > 0) {
1989 if (i != fwrite(linebuf, sizeof *linebuf, i, _coll_fp))
1990 goto jerr;
1992 goto jout;
1996 /* The interactive collect loop */
1997 if(coap == NULL)
1998 escape = *ok_vlook(escape);
1999 flags = ok_blook(errexit) ? a_ERREXIT : a_NONE;
2001 for(;;){
2002 enum {a_HIST_NONE, a_HIST_ADD = 1u<<0, a_HIST_GABBY = 1u<<1} hist;
2004 /* C99 */{
2005 enum n_go_input_flags gif;
2006 bool_t histadd;
2008 gif = n_GO_INPUT_CTX_COMPOSE;
2009 histadd = (sp != NULL);
2010 if((n_psonce & n_PSO_INTERACTIVE) || (n_poption & n_PO_TILDE_FLAG)){
2011 if(!(n_poption & n_PO_t_FLAG))
2012 gif |= n_GO_INPUT_NL_ESC;
2014 cnt = n_go_input(gif, n_empty, &linebuf, &linesize, NULL, &histadd);
2015 hist = histadd ? a_HIST_ADD | a_HIST_GABBY : a_HIST_NONE;
2018 if(cnt < 0){
2019 if(coap != NULL)
2020 break;
2021 if(n_poption & n_PO_t_FLAG){
2022 fflush_rewind(_coll_fp);
2023 /* It is important to set n_PSO_t_FLAG before extract_header()
2024 * *and* keep n_PO_t_FLAG for the first parse of the message!
2025 * TODO This is a hack, we need a clean approach for this */
2026 n_psonce |= n_PSO_t_FLAG;
2027 if(!a_coll_makeheader(_coll_fp, hp, checkaddr_err, TRU1))
2028 goto jerr;
2029 n_poption &= ~n_PO_t_FLAG;
2030 continue;
2031 }else if((n_psonce & n_PSO_INTERACTIVE) && !(n_pstate & n_PS_ROBOT) &&
2032 ok_blook(ignoreeof) && ++eofcnt < 4){
2033 fprintf(n_stdout,
2034 _("*ignoreeof* set, use `~.' to terminate letter\n"));
2035 n_go_input_clearerr();
2036 continue;
2038 break;
2041 _coll_hadintr = 0;
2043 cp = linebuf;
2044 if(cnt == 0)
2045 goto jputnl;
2046 else if(coap == NULL){
2047 if(!(n_psonce & n_PSO_INTERACTIVE) && !(n_poption & n_PO_TILDE_FLAG))
2048 goto jputline;
2049 else if(cp[0] == '.'){
2050 if(cnt == 1 && (ok_blook(dot) ||
2051 (ok_blook(posix) && ok_blook(ignoreeof))))
2052 break;
2055 if(cp[0] != escape){
2056 jputline:
2057 if(fwrite(cp, sizeof *cp, cnt, _coll_fp) != (size_t)cnt)
2058 goto jerr;
2059 /* TODO n_PS_READLINE_NL is a terrible hack to ensure that _in_all_-
2060 * TODO _code_paths_ a file without trailing newline isn't modified
2061 * TODO to contain one; the "saw-newline" needs to be part of an
2062 * TODO I/O input machinery object */
2063 jputnl:
2064 if(n_pstate & n_PS_READLINE_NL){
2065 if(putc('\n', _coll_fp) == EOF)
2066 goto jerr;
2068 continue;
2071 c = *(cp_base = ++cp);
2072 if(--cnt == 0)
2073 goto jearg;
2075 /* Avoid history entry? */
2076 while(spacechar(c)){
2077 hist = a_HIST_NONE;
2078 c = *(cp_base = ++cp);
2079 if(--cnt == 0)
2080 goto jearg;
2083 /* It may just be an escaped escaped character, do that quick */
2084 if(c == escape)
2085 goto jputline;
2087 /* Avoid hard *errexit*? */
2088 flags &= ~a_IGNERR;
2089 if(c == '-'){
2090 flags ^= a_IGNERR;
2091 c = *++cp;
2092 if(--cnt == 0)
2093 goto jearg;
2096 /* Trim input, also to gain a somewhat normalized history entry */
2097 ++cp;
2098 if(--cnt > 0){
2099 struct str x;
2101 x.s = n_UNCONST(cp);
2102 x.l = (size_t)cnt;
2103 n_str_trim_ifs(&x, TRU1);
2104 x.s[x.l] = '\0';
2105 cp = x.s;
2106 cnt = (int)/*XXX*/x.l;
2109 if(hist != a_HIST_NONE){
2110 sp = n_string_assign_c(sp, escape);
2111 if(flags & a_IGNERR)
2112 sp = n_string_push_c(sp, '-');
2113 sp = n_string_push_c(sp, c);
2114 if(cnt > 0){
2115 sp = n_string_push_c(sp, ' ');
2116 sp = n_string_push_buf(sp, cp, cnt);
2120 /* Switch over all command escapes */
2121 switch(c){
2122 default:
2123 if(1){
2124 char buf[sizeof(n_UNIREPL)];
2126 if(asciichar(c))
2127 buf[0] = c, buf[1] = '\0';
2128 else if(n_psonce & n_PSO_UNICODE)
2129 memcpy(buf, n_unirepl, sizeof n_unirepl);
2130 else
2131 buf[0] = '?', buf[1] = '\0';
2132 n_err(_("Unknown command escape: `%c%s'\n"), escape, buf);
2133 }else
2134 jearg:
2135 n_err(_("Invalid command escape usage: %s\n"),
2136 n_shexp_quote_cp(linebuf, FAL0));
2137 if(a_HARDERR())
2138 goto jerr;
2139 n_pstate_err_no = n_ERR_INVAL;
2140 n_pstate_ex_no = 1;
2141 continue;
2142 case '!':
2143 /* Shell escape, send the balance of line to sh -c */
2144 if(cnt == 0 || coap != NULL)
2145 goto jearg;
2146 else{
2147 char const *argv[2];
2149 argv[0] = cp;
2150 argv[1] = NULL;
2151 n_pstate_ex_no = c_shell(argv); /* TODO history norm.; errexit? */
2153 goto jhistcont;
2154 case '.':
2155 /* Simulate end of file on input */
2156 if(cnt != 0 || coap != NULL)
2157 goto jearg;
2158 goto jout; /* TODO does not enter history, thus */
2159 case ':':
2160 case '_':
2161 /* Escape to command mode, but be nice! *//* TODO command expansion
2162 * TODO should be handled here so that we have unique history! */
2163 if(cnt == 0)
2164 goto jearg;
2165 _execute_command(hp, cp, cnt);
2166 if(ok_blook(errexit))
2167 flags |= a_ERREXIT;
2168 else
2169 flags &= ~a_ERREXIT;
2170 if(n_pstate_ex_no != 0 && a_HARDERR())
2171 goto jerr;
2172 if(coap == NULL)
2173 escape = *ok_vlook(escape);
2174 break;
2175 /* case '<': <> 'd' */
2176 case '?':
2177 fputs(_(
2178 "COMMAND ESCAPES (to be placed after a newline) excerpt:\n"
2179 "~. Commit and send message\n"
2180 "~: <command> Execute an internal command\n"
2181 "~< <file> Insert <file> (\"~<! <command>\" inserts shell command)\n"
2182 "~@ [<files>] Edit[/Add] attachments (file[=input-charset[#output-charset]])\n"
2183 "~c <users> Add users to Cc: list (`~b': to Bcc:)\n"
2184 "~d Read in $DEAD (dead.letter)\n"
2185 "~e Edit message via $EDITOR\n"
2186 "~F <msglist> Read in with headers, do not *indentprefix* lines\n"
2187 ), n_stdout);
2188 fputs(_(
2189 "~f <msglist> Like ~F, but honour `ignore' / `retain' configuration\n"
2190 "~H Edit From:, Reply-To: and Sender:\n"
2191 "~h Prompt for Subject:, To:, Cc: and \"blind\" Bcc:\n"
2192 "~i <variable> Insert a value and a newline\n"
2193 "~M <msglist> Read in with headers, *indentprefix* (`~m': `retain' etc.)\n"
2194 "~p Show current message compose buffer\n"
2195 "~r <file> Insert <file> (`~R': likewise, but *indentprefix* lines)\n"
2196 " <file> may also be <- [HERE-DELIMITER]>\n"
2197 ), n_stdout);
2198 fputs(_(
2199 "~s <subject> Set Subject:\n"
2200 "~t <users> Add users to To: list\n"
2201 "~u <msglist> Read in message(s) without headers (`~U': indent lines)\n"
2202 "~v Edit message via $VISUAL\n"
2203 "~w <file> Write message onto file\n"
2204 "~x Abort composition, discard message (`~q': save in $DEAD)\n"
2205 "~| <command> Pipe message through shell filter\n"
2206 ), n_stdout);
2207 if(cnt != 0)
2208 goto jearg;
2209 n_pstate_err_no = n_ERR_NONE;
2210 n_pstate_ex_no = 0;
2211 break;
2212 case '@':{
2213 struct attachment *aplist;
2215 /* Edit the attachment list */
2216 aplist = hp->h_attach;
2217 hp->h_attach = NULL;
2218 if(cnt != 0)
2219 hp->h_attach = n_attachment_append_list(aplist, cp);
2220 else
2221 hp->h_attach = n_attachment_list_edit(aplist,
2222 n_GO_INPUT_CTX_COMPOSE);
2223 n_pstate_err_no = n_ERR_NONE; /* XXX ~@ does NOT handle $!/$?! */
2224 n_pstate_ex_no = 0; /* XXX */
2225 } break;
2226 case '^':
2227 if(!a_collect_plumbing(cp, hp)){
2228 if(ferror(_coll_fp))
2229 goto jerr;
2230 goto jearg;
2232 n_pstate_err_no = n_ERR_NONE; /* XXX */
2233 n_pstate_ex_no = 0; /* XXX */
2234 hist &= ~a_HIST_GABBY;
2235 break;
2236 /* case '_': <> ':' */
2237 case '|':
2238 /* Pipe message through command. Collect output as new message */
2239 if(cnt == 0)
2240 goto jearg;
2241 rewind(_coll_fp);
2242 if((n_pstate_err_no = a_coll_pipe(cp)) == n_ERR_NONE)
2243 n_pstate_ex_no = 0;
2244 else if(a_HARDERR())
2245 goto jerr;
2246 else
2247 n_pstate_ex_no = 1;
2248 hist &= ~a_HIST_GABBY;
2249 goto jhistcont;
2250 case 'A':
2251 case 'a':
2252 /* Insert the contents of a sign variable */
2253 if(cnt != 0)
2254 goto jearg;
2255 cp = (c == 'a') ? ok_vlook(sign) : ok_vlook(Sign);
2256 goto jIi_putesc;
2257 case 'b':
2258 /* Add stuff to blind carbon copies list TODO join 'c' */
2259 if(cnt == 0)
2260 goto jearg;
2261 else{
2262 struct name *np;
2263 si8_t soe;
2265 soe = 0;
2266 if((np = checkaddrs(lextract(cp, GBCC | GFULL), EACM_NORMAL, &soe)
2267 ) != NULL)
2268 hp->h_bcc = cat(hp->h_bcc, np);
2269 if(soe == 0){
2270 n_pstate_err_no = n_ERR_NONE;
2271 n_pstate_ex_no = 0;
2272 }else{
2273 n_pstate_ex_no = 1;
2274 n_pstate_err_no = (soe < 0) ? n_ERR_PERM : n_ERR_INVAL;
2277 hist &= ~a_HIST_GABBY;
2278 break;
2279 case 'c':
2280 /* Add to the CC list TODO join 'b' */
2281 if(cnt == 0)
2282 goto jearg;
2283 else{
2284 struct name *np;
2285 si8_t soe;
2287 soe = 0;
2288 if((np = checkaddrs(lextract(cp, GCC | GFULL), EACM_NORMAL, &soe)
2289 ) != NULL)
2290 hp->h_cc = cat(hp->h_cc, np);
2291 if(soe == 0){
2292 n_pstate_err_no = n_ERR_NONE;
2293 n_pstate_ex_no = 0;
2294 }else{
2295 n_pstate_ex_no = 1;
2296 n_pstate_err_no = (soe < 0) ? n_ERR_PERM : n_ERR_INVAL;
2299 hist &= ~a_HIST_GABBY;
2300 break;
2301 case 'd':
2302 if(cnt != 0)
2303 goto jearg;
2304 cp = n_getdeadletter();
2305 if(0){
2306 case '<':
2307 case 'R':
2308 case 'r':
2309 /* Invoke a file: Search for the file name, then open it and copy
2310 * the contents to _coll_fp */
2311 if(cnt == 0){
2312 n_err(_("Interpolate what file?\n"));
2313 if(a_HARDERR())
2314 goto jerr;
2315 n_pstate_err_no = n_ERR_NOENT;
2316 n_pstate_ex_no = 1;
2317 break;
2319 if(*cp == '!' && c == '<'){
2320 /* TODO hist. normalization */
2321 if((n_pstate_err_no = a_coll_insert_cmd(_coll_fp, ++cp)
2322 ) != n_ERR_NONE){
2323 if(ferror(_coll_fp))
2324 goto jerr;
2325 if(a_HARDERR())
2326 goto jerr;
2327 n_pstate_ex_no = 1;
2328 break;
2330 goto jhistcont;
2332 /* Note this also expands things like
2333 * !:vput vexpr delim random 0
2334 * !< - $delim */
2335 if((cp = fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO | FEXP_NSHELL)
2336 ) == NULL){
2337 if(a_HARDERR())
2338 goto jerr;
2339 n_pstate_err_no = n_ERR_INVAL;
2340 n_pstate_ex_no = 1;
2341 break;
2344 if(n_is_dir(cp, FAL0)){
2345 n_err(_("%s: is a directory\n"), n_shexp_quote_cp(cp, FAL0));
2346 if(a_HARDERR())
2347 goto jerr;
2348 n_pstate_err_no = n_ERR_ISDIR;
2349 n_pstate_ex_no = 1;
2350 break;
2352 if((n_pstate_err_no = a_coll_include_file(cp, (c == 'R'), TRU1)
2353 ) != n_ERR_NONE){
2354 if(ferror(_coll_fp))
2355 goto jerr;
2356 if(a_HARDERR())
2357 goto jerr;
2358 n_pstate_ex_no = 1;
2359 break;
2361 n_pstate_err_no = n_ERR_NONE; /* XXX */
2362 n_pstate_ex_no = 0; /* XXX */
2363 break;
2364 case 'e':
2365 case 'v':
2366 /* Edit the current message. 'e' -> use EDITOR, 'v' -> use VISUAL */
2367 if(cnt != 0 || coap != NULL)
2368 goto jearg;
2369 rewind(_coll_fp);
2370 if((n_pstate_err_no = a_coll_edit(c, ok_blook(editheaders) ? hp : NULL)
2371 ) == n_ERR_NONE)
2372 n_pstate_ex_no = 0;
2373 else if(ferror(_coll_fp))
2374 goto jerr;
2375 else if(a_HARDERR())
2376 goto jerr;
2377 else
2378 n_pstate_ex_no = 1;
2379 goto jhistcont;
2380 case 'F':
2381 case 'f':
2382 case 'M':
2383 case 'm':
2384 case 'U':
2385 case 'u':
2386 /* Interpolate the named messages, if we are in receiving mail mode.
2387 * Does the standard list processing garbage. If ~f is given, we
2388 * don't shift over */
2389 if(cnt == 0)
2390 goto jearg;
2391 if((n_pstate_err_no = a_coll_forward(cp, _coll_fp, c)) == n_ERR_NONE)
2392 n_pstate_ex_no = 0;
2393 else if(ferror(_coll_fp))
2394 goto jerr;
2395 else if(a_HARDERR())
2396 goto jerr;
2397 else
2398 n_pstate_ex_no = 1;
2399 break;
2400 case 'H':
2401 /* Grab extra headers */
2402 if(cnt != 0)
2403 goto jearg;
2405 grab_headers(n_GO_INPUT_CTX_COMPOSE, hp, GEXTRA, 0);
2406 while(check_from_and_sender(hp->h_from, hp->h_sender) == NULL);
2407 n_pstate_err_no = n_ERR_NONE; /* XXX */
2408 n_pstate_ex_no = 0; /* XXX */
2409 break;
2410 case 'h':
2411 /* Grab a bunch of headers */
2412 if(cnt != 0)
2413 goto jearg;
2415 grab_headers(n_GO_INPUT_CTX_COMPOSE, hp,
2416 (GTO | GSUBJECT | GCC | GBCC),
2417 (ok_blook(bsdcompat) && ok_blook(bsdorder)));
2418 while(hp->h_to == NULL);
2419 n_pstate_err_no = n_ERR_NONE; /* XXX */
2420 n_pstate_ex_no = 0; /* XXX */
2421 break;
2422 case 'I':
2423 case 'i':
2424 /* Insert a variable into the file */
2425 if(cnt == 0)
2426 goto jearg;
2427 cp = n_var_vlook(cp, TRU1);
2428 jIi_putesc:
2429 if(cp == NULL || *cp == '\0')
2430 break;
2431 if(!a_coll_putesc(cp, (c != 'I'), _coll_fp))
2432 goto jerr;
2433 if((n_psonce & n_PSO_INTERACTIVE) && !(n_pstate & n_PS_ROBOT) &&
2434 !a_coll_putesc(cp, (c != 'I'), n_stdout))
2435 goto jerr;
2436 n_pstate_err_no = n_ERR_NONE; /* XXX */
2437 n_pstate_ex_no = 0; /* XXX */
2438 break;
2439 /* case 'M': <> 'F' */
2440 /* case 'm': <> 'f' */
2441 case 'p':
2442 /* Print current state of the message without altering anything */
2443 if(cnt != 0)
2444 goto jearg;
2445 print_collf(_coll_fp, hp); /* XXX pstate_err_no ++ */
2446 if(ferror(_coll_fp))
2447 goto jerr;
2448 n_pstate_err_no = n_ERR_NONE; /* XXX */
2449 n_pstate_ex_no = 0; /* XXX */
2450 break;
2451 case 'q':
2452 case 'x':
2453 /* Force a quit, act like an interrupt had happened */
2454 if(cnt != 0)
2455 goto jearg;
2456 /* If we are running a splice hook, assume it quits on its own now,
2457 * otherwise we (no true out-of-band IPC to signal this state, XXX sic)
2458 * have to SIGTERM it in order to stop this wild beast */
2459 flags |= a_COAP_NOSIGTERM;
2460 ++_coll_hadintr;
2461 _collint((c == 'x') ? 0 : SIGINT);
2462 exit(n_EXIT_ERR);
2463 /*NOTREACHED*/
2464 /* case 'R': <> 'd' */
2465 /* case 'r': <> 'd' */
2466 case 's':
2467 /* Set the Subject list */
2468 if(cnt == 0)
2469 goto jearg;
2470 /* Subject:; take care for Debian #419840 and strip any \r and \n */
2471 if(n_anyof_cp("\n\r", hp->h_subject = savestr(cp))){
2472 char *xp;
2474 n_err(_("-s: normalizing away invalid ASCII NL / CR bytes\n"));
2475 for(xp = hp->h_subject; *xp != '\0'; ++xp)
2476 if(*xp == '\n' || *xp == '\r')
2477 *xp = ' ';
2478 n_pstate_err_no = n_ERR_INVAL;
2479 n_pstate_ex_no = 1;
2480 }else{
2481 n_pstate_err_no = n_ERR_NONE;
2482 n_pstate_ex_no = 0;
2484 break;
2485 case 't':
2486 /* Add to the To: list TODO join 'b', 'c' */
2487 if(cnt == 0)
2488 goto jearg;
2489 else{
2490 struct name *np;
2491 si8_t soe;
2493 soe = 0;
2494 if((np = checkaddrs(lextract(cp, GTO | GFULL), EACM_NORMAL, &soe)
2495 ) != NULL)
2496 hp->h_to = cat(hp->h_to, np);
2497 if(soe == 0){
2498 n_pstate_err_no = n_ERR_NONE;
2499 n_pstate_ex_no = 0;
2500 }else{
2501 n_pstate_ex_no = 1;
2502 n_pstate_err_no = (soe < 0) ? n_ERR_PERM : n_ERR_INVAL;
2505 hist &= ~a_HIST_GABBY;
2506 break;
2507 /* case 'U': <> 'F' */
2508 /* case 'u': <> 'f' */
2509 /* case 'v': <> 'e' */
2510 case 'w':
2511 /* Write the message on a file */
2512 if(cnt == 0)
2513 goto jearg;
2514 if((cp = fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO)) == NULL){
2515 n_err(_("Write what file!?\n"));
2516 if(a_HARDERR())
2517 goto jerr;
2518 n_pstate_err_no = n_ERR_INVAL;
2519 n_pstate_ex_no = 1;
2520 break;
2522 rewind(_coll_fp);
2523 if((n_pstate_err_no = a_coll_write(cp, _coll_fp, 1)) == n_ERR_NONE)
2524 n_pstate_ex_no = 0;
2525 else if(ferror(_coll_fp))
2526 goto jerr;
2527 else if(a_HARDERR())
2528 goto jerr;
2529 else
2530 n_pstate_ex_no = 1;
2531 break;
2532 /* case 'x': <> 'q' */
2535 /* Finally place an entry in history as applicable */
2536 if(0){
2537 jhistcont:
2538 c = '\1';
2539 }else
2540 c = '\0';
2541 if(hist & a_HIST_ADD)
2542 n_tty_addhist(n_string_cp(sp), ((hist & a_HIST_GABBY) != 0));
2543 if(c != '\0')
2544 goto jcont;
2547 jout:
2548 /* Do we have *on-compose-splice-shell*, or *on-compose-splice*?
2549 * TODO Usual f...ed up state of signals and terminal etc. */
2550 if(coap == NULL && (cp = ok_vlook(on_compose_splice_shell)) != NULL) Jocs:{
2551 union {int (*ptf)(void); char const *sh;} u;
2552 char const *cmd;
2554 /* Reset *escape* and more to their defaults. On change update manual! */
2555 if(ifs_saved == NULL)
2556 ifs_saved = savestr(ok_vlook(ifs));
2557 escape = n_ESCAPE[0];
2558 ok_vclear(ifs);
2560 if(coapm != NULL){
2561 /* XXX Due Popen() fflush(NULL) in PTF mode, ensure nothing to flush */
2562 /*if(!n_real_seek(_coll_fp, 0, SEEK_END))
2563 * goto jerr;*/
2564 u.ptf = &a_coll_ocs__mac;
2565 cmd = (char*)-1;
2566 a_coll_ocs__macname = cp = coapm;
2567 }else{
2568 u.sh = ok_vlook(SHELL);
2569 cmd = cp;
2572 i = strlen(cp) +1;
2573 coap = n_lofi_alloc(n_VSTRUCT_SIZEOF(struct a_coll_ocs_arg, coa_cmd
2574 ) + i);
2575 coap->coa_pipe[0] = coap->coa_pipe[1] = -1;
2576 coap->coa_stdin = coap->coa_stdout = NULL;
2577 coap->coa_senderr = checkaddr_err;
2578 memcpy(coap->coa_cmd, cp, i);
2580 hold_all_sigs();
2581 coap->coa_opipe = safe_signal(SIGPIPE, SIG_IGN);
2582 coap->coa_oint = safe_signal(SIGINT, SIG_IGN);
2583 rele_all_sigs();
2585 if(pipe_cloexec(coap->coa_pipe) != -1 &&
2586 (coap->coa_stdin = Fdopen(coap->coa_pipe[0], "r", FAL0)) != NULL &&
2587 (coap->coa_stdout = Popen(cmd, "W", u.sh, NULL, coap->coa_pipe[1])
2588 ) != NULL){
2589 close(coap->coa_pipe[1]);
2590 coap->coa_pipe[1] = -1;
2592 temporary_compose_mode_hook_call(NULL, NULL, NULL);
2593 n_go_splice_hack(coap->coa_cmd, coap->coa_stdin, coap->coa_stdout,
2594 (n_psonce & ~(n_PSO_INTERACTIVE | n_PSO_TTYIN | n_PSO_TTYOUT)),
2595 &a_coll_ocs__finalize, &coap);
2596 /* Hook version protocol for ~^: update manual upon change! */
2597 fputs(a_COLL_PLUMBING_VERSION "\n", coap->coa_stdout);
2598 #undef a_COLL_PLUMBING_VERSION
2599 goto jcont;
2602 c = n_err_no;
2603 a_coll_ocs__finalize(coap);
2604 n_perr(_("Cannot invoke *on-compose-splice(-shell)?*"), c);
2605 goto jerr;
2607 if(*checkaddr_err != 0){
2608 *checkaddr_err = 0;
2609 goto jerr;
2611 if(coapm == NULL && (coapm = ok_vlook(on_compose_splice)) != NULL)
2612 goto Jocs;
2613 if(coap != NULL){
2614 ok_vset(ifs, ifs_saved);
2615 ifs_saved = NULL;
2618 /* Final chance to edit headers, if not already above */
2619 if((n_psonce & n_PSO_INTERACTIVE) && !(n_pstate & n_PS_ROBOT)){
2620 if(ok_blook(bsdcompat) || ok_blook(askatend)){
2621 enum gfield gf;
2623 gf = GNONE;
2624 if(ok_blook(askcc))
2625 gf |= GCC;
2626 if(ok_blook(askbcc))
2627 gf |= GBCC;
2628 if(gf != 0)
2629 grab_headers(n_GO_INPUT_CTX_COMPOSE, hp, gf, 1);
2631 if(ok_blook(askattach))
2632 hp->h_attach = n_attachment_list_edit(hp->h_attach,
2633 n_GO_INPUT_CTX_COMPOSE);
2636 /* Execute compose-leave */
2637 if((cp = ok_vlook(on_compose_leave)) != NULL){
2638 setup_from_and_sender(hp);
2639 temporary_compose_mode_hook_call(cp, &n_temporary_compose_hook_varset,
2640 hp);
2643 /* Add automatic receivers */
2644 if ((cp = ok_vlook(autocc)) != NULL && *cp != '\0')
2645 hp->h_cc = cat(hp->h_cc, checkaddrs(lextract(cp, GCC |
2646 (ok_blook(fullnames) ? GFULL | GSKIN : GSKIN)),
2647 EACM_NORMAL, checkaddr_err));
2648 if ((cp = ok_vlook(autobcc)) != NULL && *cp != '\0')
2649 hp->h_bcc = cat(hp->h_bcc, checkaddrs(lextract(cp, GBCC |
2650 (ok_blook(fullnames) ? GFULL | GSKIN : GSKIN)),
2651 EACM_NORMAL, checkaddr_err));
2652 if (*checkaddr_err != 0)
2653 goto jerr;
2655 if((n_psonce & n_PSO_INTERACTIVE) && !(n_pstate & n_PS_ROBOT) &&
2656 ok_blook(asksend)){
2657 bool_t b;
2659 ifs_saved = coapm = NULL;
2660 coap = NULL;
2662 fprintf(n_stdout, _("-------\nEnvelope contains:\n")); /* xxx SEARCH112 */
2663 puthead(TRU1, hp, n_stdout, GIDENT | GSUBJECT | GTO | GCC | GBCC | GCOMMA,
2664 SEND_TODISP, CONV_NONE, NULL, NULL);
2666 while((cp = n_go_input_cp(n_GO_INPUT_CTX_COMPOSE | n_GO_INPUT_NL_ESC,
2667 _("Send this message [yes/no, or recompose]? "), NULL)) == NULL)
2668 if(n_go_input_is_eof()){
2669 cp = n_1;
2670 break;
2673 b = boolify(cp, UIZ_MAX, TRU1);
2674 if(b < FAL0)
2675 goto jcont;
2676 else if(!b)
2677 goto jerr;
2680 /* TODO Cannot do since it may require turning this into a multipart one */
2681 if(n_poption & n_PO_Mm_FLAG)
2682 goto jskiptails;
2684 /* Place signature? */
2685 if((cp = ok_vlook(signature)) != NULL && *cp != '\0'){ /* TODO OBSOLETE */
2686 char const *cpq;
2688 n_OBSOLETE(_("please use *on-compose-{leave,splice}* and/or "
2689 "*message-inject-tail*, not *signature*"));
2691 if((cpq = fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO)) == NULL){
2692 n_err(_("*signature* expands to invalid file: %s\n"),
2693 n_shexp_quote_cp(cp, FAL0));
2694 goto jerr;
2696 cpq = n_shexp_quote_cp(cp = cpq, FAL0);
2698 if((sigfp = Fopen(cp, "r")) == NULL){
2699 n_err(_("Can't open *signature* %s: %s\n"),
2700 cpq, n_err_to_doc(n_err_no));
2701 goto jerr;
2704 if(linebuf == NULL)
2705 linebuf = smalloc(linesize = LINESIZE);
2706 c = '\0';
2707 while((i = fread(linebuf, sizeof *linebuf, linesize, n_UNVOLATILE(sigfp)))
2708 > 0){
2709 c = linebuf[i - 1];
2710 if(i != fwrite(linebuf, sizeof *linebuf, i, _coll_fp))
2711 goto jerr;
2714 /* C99 */{
2715 FILE *x = n_UNVOLATILE(sigfp);
2716 int e = n_err_no, ise = ferror(x);
2718 sigfp = NULL;
2719 Fclose(x);
2721 if(ise){
2722 n_err(_("Errors while reading *signature* %s: %s\n"),
2723 cpq, n_err_to_doc(e));
2724 goto jerr;
2728 if(c != '\0' && c != '\n')
2729 putc('\n', _coll_fp);
2732 { char const *cp_obsolete = ok_vlook(NAIL_TAIL);
2734 if(cp_obsolete != NULL)
2735 n_OBSOLETE(_("please use *message-inject-tail*, not *NAIL_TAIL*"));
2737 if((cp = ok_vlook(message_inject_tail)) != NULL ||
2738 (cp = cp_obsolete) != NULL){
2739 if(!a_coll_putesc(cp, TRU1, _coll_fp))
2740 goto jerr;
2741 if((n_psonce & n_PSO_INTERACTIVE) && !(n_pstate & n_PS_ROBOT) &&
2742 !a_coll_putesc(cp, TRU1, n_stdout))
2743 goto jerr;
2747 jskiptails:
2748 if(fflush(_coll_fp))
2749 goto jerr;
2750 rewind(_coll_fp);
2752 jleave:
2753 if (linebuf != NULL)
2754 free(linebuf);
2755 sigprocmask(SIG_BLOCK, &nset, NULL);
2756 n_pstate &= ~n_PS_COMPOSE_MODE;
2757 safe_signal(SIGINT, _coll_saveint);
2758 safe_signal(SIGHUP, _coll_savehup);
2759 sigprocmask(SIG_SETMASK, &oset, NULL);
2760 NYD_LEAVE;
2761 return _coll_fp;
2763 jerr:
2764 hold_all_sigs();
2766 if(coap != NULL && coap != (struct a_coll_ocs_arg*)-1){
2767 if(!(flags & a_COAP_NOSIGTERM))
2768 n_psignal(coap->coa_stdout, SIGTERM);
2769 n_go_splice_hack_remove_after_jump();
2770 coap = NULL;
2772 if(ifs_saved != NULL){
2773 ok_vset(ifs, ifs_saved);
2774 ifs_saved = NULL;
2776 if(sigfp != NULL){
2777 Fclose(n_UNVOLATILE(sigfp));
2778 sigfp = NULL;
2780 if(_coll_fp != NULL){
2781 Fclose(_coll_fp);
2782 _coll_fp = NULL;
2785 rele_all_sigs();
2787 assert(checkaddr_err != NULL);
2788 /* TODO We don't save in $DEAD upon error because msg not readily composed?
2789 * TODO But this no good, it should go ZOMBIE / DRAFT / POSTPONED or what! */
2790 if(*checkaddr_err != 0){
2791 if(*checkaddr_err == 111)
2792 n_err(_("Compose mode splice hook failure\n"));
2793 else
2794 n_err(_("Some addressees were classified as \"hard error\"\n"));
2795 }else if(_coll_hadintr == 0){
2796 *checkaddr_err = TRU1; /* TODO ugly: "sendout_error" now.. */
2797 n_err(_("Failed to prepare composed message\n"));
2799 goto jleave;
2801 #undef a_HARDERR
2804 /* s-it-mode */