In compose-mode, removing In-Reply-To: starts thread (Doug McIlroy)..
[s-mailx.git] / collect.c
blob0dbcf9beb7f6f64464fbca75483555559523ce03
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 - 2018 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 n_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 indl = indent ? strlen(indb = ok_vlook(indentprefix)) : 0;
241 if(fbuf != n_stdin)
242 cnt = fsize(fbuf);
243 while(fgetline(&linebuf, &linesize, (fbuf == n_stdin ? NULL : &cnt),
244 &linelen, fbuf, 0) != NULL){
245 if(heredl > 0 && heredl == linelen - 1 &&
246 !memcmp(heredb, linebuf, heredl)){
247 heredb = NULL;
248 break;
251 if(indl > 0){
252 if(fwrite(indb, sizeof *indb, indl, _coll_fp) != indl){
253 rv = n_err_no;
254 goto jleave;
256 cc += indl;
259 if(fwrite(linebuf, sizeof *linebuf, linelen, _coll_fp) != linelen){
260 rv = n_err_no;
261 goto jleave;
263 cc += linelen;
264 ++lc;
266 if(fflush(_coll_fp)){
267 rv = n_err_no;
268 goto jleave;
271 if(heredb != NULL)
272 rv = n_ERR_NOTOBACCO;
273 jleave:
274 if(linebuf != NULL)
275 n_free(linebuf);
276 if(fbuf != NULL){
277 if(fbuf != n_stdin)
278 Fclose(fbuf);
279 else if(heredl > 0)
280 clearerr(n_stdin);
283 if(writestat)
284 fprintf(n_stdout, "%s%s %" PRId64 "/%" PRId64 "\n",
285 n_shexp_quote_cp(name, FAL0), (rv ? " " n_ERROR : n_empty), lc, cc);
286 NYD_LEAVE;
287 return rv;
290 static si32_t
291 a_coll_insert_cmd(FILE *fp, char const *cmd){
292 FILE *ibuf;
293 si64_t lc, cc;
294 si32_t rv;
295 NYD_ENTER;
297 rv = n_ERR_NONE;
298 lc = cc = 0;
300 if((ibuf = Popen(cmd, "r", ok_vlook(SHELL), NULL, 0)) != NULL){
301 int c;
303 while((c = getc(ibuf)) != EOF){ /* XXX bytewise, yuck! */
304 if(putc(c, fp) == EOF){
305 rv = n_err_no;
306 break;
308 ++cc;
309 if(c == '\n')
310 ++lc;
312 if(!feof(ibuf) || ferror(ibuf)){
313 if(rv == n_ERR_NONE)
314 rv = n_ERR_IO;
316 if(!Pclose(ibuf, TRU1)){
317 if(rv == n_ERR_NONE)
318 rv = n_ERR_IO;
320 }else
321 n_perr(cmd, rv = n_err_no);
323 fprintf(n_stdout, "CMD%s %" PRId64 "/%" PRId64 "\n",
324 (rv == n_ERR_NONE ? n_empty : " " n_ERROR), lc, cc);
325 NYD_LEAVE;
326 return rv;
329 static void
330 print_collf(FILE *cf, struct header *hp)
332 char *lbuf;
333 FILE *obuf;
334 size_t cnt, linesize, linelen;
335 NYD_ENTER;
337 fflush_rewind(cf);
338 cnt = (size_t)fsize(cf);
340 if((obuf = Ftmp(NULL, "collfp", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL){
341 n_perr(_("Can't create temporary file for `~p' command"), 0);
342 goto jleave;
345 hold_all_sigs();
347 fprintf(obuf, _("-------\nMessage contains:\n")); /* xxx SEARCH112 */
348 puthead(TRU1, hp, obuf,
349 (GIDENT | GTO | GSUBJECT | GCC | GBCC | GNL | GFILES | GCOMMA),
350 SEND_TODISP, CONV_NONE, NULL, NULL);
352 lbuf = NULL;
353 linesize = 0;
354 while(fgetline(&lbuf, &linesize, &cnt, &linelen, cf, 1))
355 prout(lbuf, linelen, obuf);
356 if(lbuf != NULL)
357 n_free(lbuf);
359 if(hp->h_attach != NULL){
360 fputs(_("-------\nAttachments:\n"), obuf);
361 n_attachment_list_print(hp->h_attach, obuf);
364 rele_all_sigs();
366 page_or_print(obuf, 0);
368 Fclose(obuf);
369 jleave:
370 NYD_LEAVE;
373 static si32_t
374 a_coll_write(char const *name, FILE *fp, int f)
376 FILE *of;
377 int c;
378 si64_t lc, cc;
379 si32_t rv;
380 NYD_ENTER;
382 rv = n_ERR_NONE;
384 if(f) {
385 fprintf(n_stdout, "%s ", n_shexp_quote_cp(name, FAL0));
386 fflush(n_stdout);
389 if ((of = Fopen(name, "a")) == NULL) {
390 n_perr(name, rv = n_err_no);
391 goto jerr;
394 lc = cc = 0;
395 while ((c = getc(fp)) != EOF) {
396 ++cc;
397 if (c == '\n')
398 ++lc;
399 if (putc(c, of) == EOF) {
400 n_perr(name, rv = n_err_no);
401 goto jerr;
404 fprintf(n_stdout, _("%" PRId64 "/%" PRId64 "\n"), lc, cc);
406 jleave:
407 if(of != NULL)
408 Fclose(of);
409 fflush(n_stdout);
410 NYD_LEAVE;
411 return rv;
412 jerr:
413 putc('-', n_stdout);
414 putc('\n', n_stdout);
415 goto jleave;
418 static bool_t
419 a_coll_message_inject_head(FILE *fp){
420 bool_t rv;
421 char const *cp, *cp_obsolete;
422 NYD2_ENTER;
424 cp_obsolete = ok_vlook(NAIL_HEAD);
425 if(cp_obsolete != NULL)
426 n_OBSOLETE(_("please use *message-inject-head*, not *NAIL_HEAD*"));
428 if(((cp = ok_vlook(message_inject_head)) != NULL ||
429 (cp = cp_obsolete) != NULL) && !a_coll_putesc(cp, TRU1, fp))
430 rv = FAL0;
431 else
432 rv = TRU1;
433 NYD2_LEAVE;
434 return rv;
437 static bool_t
438 a_coll_makeheader(FILE *fp, struct header *hp, si8_t *checkaddr_err,
439 bool_t do_delayed_due_t)
441 FILE *nf;
442 int c;
443 bool_t rv;
444 NYD_ENTER;
446 rv = FAL0;
448 if ((nf = Ftmp(NULL, "colhead", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==NULL) {
449 n_perr(_("temporary mail edit file"), 0);
450 goto jleave;
453 n_header_extract(fp, hp, (do_delayed_due_t ? TRU1 : TRUM1), checkaddr_err);
454 if (checkaddr_err != NULL && *checkaddr_err != 0)
455 goto jleave;
457 /* In template mode some things have been delayed until the template has
458 * been read */
459 if(do_delayed_due_t){
460 char const *cp;
462 if((cp = ok_vlook(on_compose_enter)) != NULL){
463 setup_from_and_sender(hp);
464 temporary_compose_mode_hook_call(cp, &n_temporary_compose_hook_varset,
465 hp);
468 if(!a_coll_message_inject_head(nf))
469 goto jleave;
472 while ((c = getc(fp)) != EOF) /* XXX bytewise, yuck! */
473 putc(c, nf);
475 if (fp != _coll_fp)
476 Fclose(_coll_fp);
477 Fclose(fp);
478 _coll_fp = nf;
479 nf = NULL;
481 if (check_from_and_sender(hp->h_from, hp->h_sender) == NULL)
482 goto jleave;
483 rv = TRU1;
484 jleave:
485 if(nf != NULL)
486 Fclose(nf);
487 NYD_LEAVE;
488 return rv;
491 static si32_t
492 a_coll_edit(int c, struct header *hp) /* TODO error(return) weird */
494 struct n_sigman sm;
495 FILE *nf;
496 sighandler_type volatile sigint;
497 struct name *saved_in_reply_to;
498 bool_t saved_filrec;
499 si32_t volatile rv;
500 NYD_ENTER;
502 n_UNINIT(sigint, SIG_ERR);
503 rv = n_ERR_NONE;
505 if(!(saved_filrec = ok_blook(add_file_recipients)))
506 ok_bset(add_file_recipients);
508 saved_in_reply_to = NULL;
509 if(hp != NULL){
510 struct name *np;
512 if((np = hp->h_in_reply_to) == NULL)
513 hp->h_in_reply_to = np = n_header_setup_in_reply_to(hp);
514 if(np != NULL)
515 saved_in_reply_to = ndup(np, np->n_type);
518 n_SIGMAN_ENTER_SWITCH(&sm, n_SIGMAN_ALL){
519 case 0:
520 sigint = safe_signal(SIGINT, SIG_IGN);
521 break;
522 default:
523 rv = n_ERR_INTR;
524 goto jleave;
527 nf = run_editor(_coll_fp, (off_t)-1, c, FAL0, hp, NULL, SEND_MBOX, sigint);
528 if(nf != NULL){
529 if(hp != NULL){
530 if(!a_coll_makeheader(nf, hp, NULL, FAL0))
531 rv = n_ERR_INVAL;
532 /* Break the thread if In-Reply-To: has been modified */
533 if(hp->h_in_reply_to == NULL || (saved_in_reply_to != NULL &&
534 asccasecmp(hp->h_in_reply_to->n_fullname,
535 saved_in_reply_to->n_fullname)))
536 hp->h_ref = NULL;
537 }else{
538 fseek(nf, 0L, SEEK_END);
539 Fclose(_coll_fp);
540 _coll_fp = nf;
542 }else
543 rv = n_ERR_CHILD;
545 n_sigman_cleanup_ping(&sm);
546 jleave:
547 if(!saved_filrec)
548 ok_bclear(add_file_recipients);
549 safe_signal(SIGINT, sigint);
550 NYD_LEAVE;
551 n_sigman_leave(&sm, n_SIGMAN_VIPSIGS_NTTYOUT);
552 return rv;
555 static si32_t
556 a_coll_pipe(char const *cmd)
558 int ws;
559 FILE *nf;
560 sighandler_type sigint;
561 si32_t rv;
562 NYD_ENTER;
564 rv = n_ERR_NONE;
565 sigint = safe_signal(SIGINT, SIG_IGN);
567 if ((nf = Ftmp(NULL, "colpipe", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==NULL) {
568 n_perr(_("temporary mail edit file"), rv = n_err_no);
569 goto jout;
572 /* stdin = current message. stdout = new message */
573 fflush(_coll_fp);
574 if (n_child_run(ok_vlook(SHELL), 0, fileno(_coll_fp), fileno(nf), "-c",
575 cmd, NULL, NULL, &ws) < 0 || WEXITSTATUS(ws) != 0) {
576 Fclose(nf);
577 rv = n_ERR_CHILD;
578 goto jout;
581 if (fsize(nf) == 0) {
582 n_err(_("No bytes from %s !?\n"), n_shexp_quote_cp(cmd, FAL0));
583 Fclose(nf);
584 rv = n_ERR_NODATA;
585 goto jout;
588 /* Take new files */
589 fseek(nf, 0L, SEEK_END);
590 Fclose(_coll_fp);
591 _coll_fp = nf;
592 jout:
593 safe_signal(SIGINT, sigint);
594 NYD_LEAVE;
595 return rv;
598 static si32_t
599 a_coll_forward(char const *ms, FILE *fp, int f)
601 int *msgvec, rv = 0;
602 struct n_ignore const *itp;
603 char const *tabst;
604 enum sendaction action;
605 NYD_ENTER;
607 msgvec = n_autorec_alloc((size_t)(msgCount + 1) * sizeof *msgvec);
608 if (getmsglist(ms, msgvec, 0) < 0) {
609 rv = n_ERR_NOENT; /* XXX not really, should be handled there! */
610 goto jleave;
612 if (*msgvec == 0) {
613 *msgvec = first(0, MMNORM);
614 if (*msgvec == 0) {
615 n_err(_("No appropriate messages\n"));
616 rv = n_ERR_NOENT;
617 goto jleave;
619 msgvec[1] = 0;
622 if (f == 'f' || f == 'F' || f == 'u')
623 tabst = NULL;
624 else
625 tabst = ok_vlook(indentprefix);
626 if (f == 'u' || f == 'U')
627 itp = n_IGNORE_ALL;
628 else
629 itp = upperchar(f) ? NULL : n_IGNORE_TYPE;
630 action = (upperchar(f) && f != 'U') ? SEND_QUOTE_ALL : SEND_QUOTE;
632 fprintf(n_stdout, _("Interpolating:"));
633 srelax_hold();
634 for (; *msgvec != 0; ++msgvec) {
635 struct message *mp = message + *msgvec - 1;
637 touch(mp);
638 fprintf(n_stdout, " %d", *msgvec);
639 fflush(n_stdout);
640 if (sendmp(mp, fp, itp, tabst, action, NULL) < 0) {
641 n_perr(_("forward: temporary mail file"), 0);
642 rv = n_ERR_IO;
643 break;
645 srelax();
647 srelax_rele();
648 fprintf(n_stdout, "\n");
649 jleave:
650 NYD_LEAVE;
651 return rv;
654 static bool_t
655 a_collect_plumbing(char const *ms, struct header *hp){
656 /* TODO _collect_plumbing: instead of fields the basic headers should
657 * TODO be in an array and have IDs, like in termcap etc., so then this
658 * TODO could be simplified as table-walks. Also true for arg-checks! */
659 bool_t rv;
660 char const *cp, *cmd[4];
661 NYD2_ENTER;
663 /* Protcol version for *on-compose-splice** -- update manual on change! */
664 #define a_COLL_PLUMBING_VERSION "0 0 1"
665 cp = ms;
667 /* C99 */{
668 size_t i;
670 for(i = 0; i < n_NELEM(cmd); ++i){ /* TODO trim+strlist_split(_ifs?)() */
671 while(blankchar(*cp))
672 ++cp;
673 if(*cp == '\0')
674 cmd[i] = NULL;
675 else{
676 if(i < n_NELEM(cmd) - 1)
677 for(cmd[i] = cp++; *cp != '\0' && !blankchar(*cp); ++cp)
679 else{
680 /* Last slot takes all the rest of the line, less trailing WS */
681 for(cmd[i] = cp++; *cp != '\0'; ++cp)
683 while(blankchar(cp[-1]))
684 --cp;
686 cmd[i] = savestrbuf(cmd[i], PTR2SIZE(cp - cmd[i]));
691 if(n_UNLIKELY(cmd[0] == NULL))
692 goto jecmd;
693 if(is_asccaseprefix(cmd[0], "header"))
694 rv = a_collect__plumb_header(cp, hp, cmd);
695 else if(is_asccaseprefix(cmd[0], "attachment"))
696 rv = a_collect__plumb_attach(cp, hp, cmd);
697 else{
698 jecmd:
699 fputs("500\n", n_stdout);
700 rv = FAL0;
702 fflush(n_stdout);
704 NYD2_LEAVE;
705 return rv;
708 static bool_t
709 a_collect__plumb_header(char const *cp, struct header *hp,
710 char const *cmd[4]){
711 uiz_t i;
712 struct n_header_field *hfp;
713 struct name *np, **npp;
714 NYD2_ENTER;
716 if(cmd[1] == NULL)
717 goto jdefault;
719 if(is_asccaseprefix(cmd[1], "insert")){ /* TODO LOGIC BELONGS head.c
720 * TODO That is: Header::factory(string) -> object (blahblah).
721 * TODO I.e., as long as we don't have regular RFC compliant parsers
722 * TODO which differentiate in between structured and unstructured
723 * TODO header fields etc., a little workaround */
724 struct name *xnp;
725 si8_t aerr;
726 enum expand_addr_check_mode eacm;
727 enum gfield ntype;
728 bool_t mult_ok;
730 if(cmd[2] == NULL || cmd[3] == NULL)
731 goto jecmd;
733 /* Strip [\r\n] which would render a body invalid XXX all controls? */
734 /* C99 */{
735 char *xp, c;
737 cmd[3] = xp = savestr(cmd[3]);
738 for(; (c = *xp) != '\0'; ++xp)
739 if(c == '\n' || c == '\r')
740 *xp = ' ';
743 if(!asccasecmp(cmd[2], cp = "Subject")){
744 if(cmd[3][0] != '\0'){
745 if(hp->h_subject != NULL)
746 hp->h_subject = savecatsep(hp->h_subject, ' ', cmd[3]);
747 else
748 hp->h_subject = n_UNCONST(cmd[3]);
749 fprintf(n_stdout, "210 %s 1\n", cp);
750 goto jleave;
751 }else
752 goto j501cp;
755 mult_ok = TRU1;
756 ntype = GEXTRA | GFULL | GFULLEXTRA;
757 eacm = EACM_STRICT;
759 if(!asccasecmp(cmd[2], cp = "From")){
760 npp = &hp->h_from;
761 jins:
762 aerr = 0;
763 if((np = lextract(cmd[3], ntype)) == NULL)
764 goto j501cp;
766 if((np = checkaddrs(np, eacm, &aerr), aerr != 0)){
767 fprintf(n_stdout, "505 %s\n", cp);
768 goto jleave;
771 /* Go to the end of the list, track whether it contains any
772 * non-deleted entries */
773 i = 0;
774 if((xnp = *npp) != NULL)
775 for(;; xnp = xnp->n_flink){
776 if(!(xnp->n_type & GDEL))
777 ++i;
778 if(xnp->n_flink == NULL)
779 break;
782 if(!mult_ok && (i != 0 || np->n_flink != NULL))
783 fprintf(n_stdout, "506 %s\n", cp);
784 else{
785 if(xnp == NULL)
786 *npp = np;
787 else
788 xnp->n_flink = np;
789 np->n_blink = xnp;
790 fprintf(n_stdout, "210 %s %" PRIuZ "\n", cp, ++i);
792 goto jleave;
794 if(!asccasecmp(cmd[2], cp = "Sender")){
795 mult_ok = FAL0;
796 npp = &hp->h_sender;
797 goto jins;
799 if(!asccasecmp(cmd[2], cp = "To")){
800 npp = &hp->h_to;
801 ntype = GTO | GFULL;
802 eacm = EACM_NORMAL | EAF_NAME;
803 goto jins;
805 if(!asccasecmp(cmd[2], cp = "Cc")){
806 npp = &hp->h_cc;
807 ntype = GCC | GFULL;
808 eacm = EACM_NORMAL | EAF_NAME;
809 goto jins;
811 if(!asccasecmp(cmd[2], cp = "Bcc")){
812 npp = &hp->h_bcc;
813 ntype = GBCC | GFULL;
814 eacm = EACM_NORMAL | EAF_NAME;
815 goto jins;
817 if(!asccasecmp(cmd[2], cp = "Reply-To")){
818 npp = &hp->h_reply_to;
819 eacm = EACM_NONAME;
820 goto jins;
822 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
823 npp = &hp->h_mft;
824 eacm = EACM_NONAME;
825 goto jins;
827 if(!asccasecmp(cmd[2], cp = "Message-ID")){
828 mult_ok = FAL0;
829 npp = &hp->h_message_id;
830 ntype = GREF;
831 eacm = EACM_NONAME;
832 goto jins;
834 if(!asccasecmp(cmd[2], cp = "References")){
835 npp = &hp->h_ref;
836 ntype = GREF;
837 eacm = EACM_NONAME;
838 goto jins;
840 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
841 npp = &hp->h_in_reply_to;
842 ntype = GREF;
843 eacm = EACM_NONAME;
844 goto jins;
847 if((cp = n_header_is_standard(cmd[2], UIZ_MAX)) != NULL){
848 fprintf(n_stdout, "505 %s\n", cp);
849 goto jleave;
852 /* Free-form header fields */
853 /* C99 */{
854 size_t nl, bl;
855 struct n_header_field **hfpp;
857 for(cp = cmd[2]; *cp != '\0'; ++cp)
858 if(!fieldnamechar(*cp)){
859 cp = cmd[2];
860 goto j501cp;
863 for(i = 0, hfpp = &hp->h_user_headers; *hfpp != NULL; ++i)
864 hfpp = &(*hfpp)->hf_next;
866 nl = strlen(cp = cmd[2]) +1;
867 bl = strlen(cmd[3]) +1;
868 *hfpp = hfp = n_autorec_alloc(n_VSTRUCT_SIZEOF(struct n_header_field,
869 hf_dat) + nl + bl);
870 hfp->hf_next = NULL;
871 hfp->hf_nl = nl - 1;
872 hfp->hf_bl = bl - 1;
873 memcpy(&hfp->hf_dat[0], cp, nl);
874 memcpy(&hfp->hf_dat[nl], cmd[3], bl);
875 fprintf(n_stdout, "210 %s %" PRIuZ "\n", &hfp->hf_dat[0], ++i);
877 goto jleave;
880 if(is_asccaseprefix(cmd[1], "list")){
881 jdefault:
882 if(cmd[2] == NULL){
883 fputs("210", n_stdout);
884 if(hp->h_subject != NULL) fputs(" Subject", n_stdout);
885 if(hp->h_from != NULL) fputs(" From", n_stdout);
886 if(hp->h_sender != NULL) fputs(" Sender", n_stdout);
887 if(hp->h_to != NULL) fputs(" To", n_stdout);
888 if(hp->h_cc != NULL) fputs(" Cc", n_stdout);
889 if(hp->h_bcc != NULL) fputs(" Bcc", n_stdout);
890 if(hp->h_reply_to != NULL) fputs(" Reply-To", n_stdout);
891 if(hp->h_mft != NULL) fputs(" Mail-Followup-To", n_stdout);
892 if(hp->h_message_id != NULL) fputs(" Message-ID", n_stdout);
893 if(hp->h_ref != NULL) fputs(" References", n_stdout);
894 if(hp->h_in_reply_to != NULL) fputs(" In-Reply-To", n_stdout);
895 if(hp->h_mailx_command != NULL) fputs(" Mailx-Command", n_stdout);
896 if(hp->h_mailx_raw_to != NULL) fputs(" Mailx-Raw-To", n_stdout);
897 if(hp->h_mailx_raw_cc != NULL) fputs(" Mailx-Raw-Cc", n_stdout);
898 if(hp->h_mailx_raw_bcc != NULL) fputs(" Mailx-Raw-Bcc", n_stdout);
899 if(hp->h_mailx_orig_from != NULL) fputs(" Mailx-Orig-From", n_stdout);
900 if(hp->h_mailx_orig_to != NULL) fputs(" Mailx-Orig-To", n_stdout);
901 if(hp->h_mailx_orig_cc != NULL) fputs(" Mailx-Orig-Cc", n_stdout);
902 if(hp->h_mailx_orig_bcc != NULL) fputs(" Mailx-Orig-Bcc", n_stdout);
904 /* Print only one instance of each free-form header */
905 for(hfp = hp->h_user_headers; hfp != NULL; hfp = hfp->hf_next){
906 struct n_header_field *hfpx;
908 for(hfpx = hp->h_user_headers;; hfpx = hfpx->hf_next)
909 if(hfpx == hfp){
910 putc(' ', n_stdout);
911 fputs(&hfp->hf_dat[0], n_stdout);
912 break;
913 }else if(!asccasecmp(&hfpx->hf_dat[0], &hfp->hf_dat[0]))
914 break;
916 putc('\n', n_stdout);
917 goto jleave;
920 if(cmd[3] != NULL)
921 goto jecmd;
923 if(!asccasecmp(cmd[2], cp = "Subject")){
924 np = (hp->h_subject != NULL) ? (struct name*)-1 : NULL;
925 goto jlist;
927 if(!asccasecmp(cmd[2], cp = "From")){
928 np = hp->h_from;
929 jlist:
930 fprintf(n_stdout, "%s %s\n", (np == NULL ? "501" : "210"), cp);
931 goto jleave;
933 if(!asccasecmp(cmd[2], cp = "Sender")){
934 np = hp->h_sender;
935 goto jlist;
937 if(!asccasecmp(cmd[2], cp = "To")){
938 np = hp->h_to;
939 goto jlist;
941 if(!asccasecmp(cmd[2], cp = "Cc")){
942 np = hp->h_cc;
943 goto jlist;
945 if(!asccasecmp(cmd[2], cp = "Bcc")){
946 np = hp->h_bcc;
947 goto jlist;
949 if(!asccasecmp(cmd[2], cp = "Reply-To")){
950 np = hp->h_reply_to;
951 goto jlist;
953 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
954 np = hp->h_mft;
955 goto jlist;
957 if(!asccasecmp(cmd[2], cp = "Message-ID")){
958 np = hp->h_message_id;
959 goto jlist;
961 if(!asccasecmp(cmd[2], cp = "References")){
962 np = hp->h_ref;
963 goto jlist;
965 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
966 np = hp->h_in_reply_to;
967 goto jlist;
970 if(!asccasecmp(cmd[2], cp = "Mailx-Command")){
971 np = (hp->h_mailx_command != NULL) ? (struct name*)-1 : NULL;
972 goto jlist;
974 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-To")){
975 np = hp->h_mailx_raw_to;
976 goto jlist;
978 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-Cc")){
979 np = hp->h_mailx_raw_cc;
980 goto jlist;
982 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-Bcc")){
983 np = hp->h_mailx_raw_bcc;
984 goto jlist;
986 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-From")){
987 np = hp->h_mailx_orig_from;
988 goto jlist;
990 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-To")){
991 np = hp->h_mailx_orig_to;
992 goto jlist;
994 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-Cc")){
995 np = hp->h_mailx_orig_cc;
996 goto jlist;
998 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-Bcc")){
999 np = hp->h_mailx_orig_bcc;
1000 goto jlist;
1003 /* Free-form header fields */
1004 for(cp = cmd[2]; *cp != '\0'; ++cp)
1005 if(!fieldnamechar(*cp)){
1006 cp = cmd[2];
1007 goto j501cp;
1009 cp = cmd[2];
1010 for(hfp = hp->h_user_headers;; hfp = hfp->hf_next){
1011 if(hfp == NULL)
1012 goto j501cp;
1013 else if(!asccasecmp(cp, &hfp->hf_dat[0])){
1014 fprintf(n_stdout, "210 %s\n", &hfp->hf_dat[0]);
1015 break;
1018 goto jleave;
1021 if(is_asccaseprefix(cmd[1], "remove")){
1022 if(cmd[2] == NULL || cmd[3] != NULL)
1023 goto jecmd;
1025 if(!asccasecmp(cmd[2], cp = "Subject")){
1026 if(hp->h_subject != NULL){
1027 hp->h_subject = NULL;
1028 fprintf(n_stdout, "210 %s\n", cp);
1029 goto jleave;
1030 }else
1031 goto j501cp;
1034 if(!asccasecmp(cmd[2], cp = "From")){
1035 npp = &hp->h_from;
1036 jrem:
1037 if(*npp != NULL){
1038 *npp = NULL;
1039 fprintf(n_stdout, "210 %s\n", cp);
1040 goto jleave;
1041 }else
1042 goto j501cp;
1044 if(!asccasecmp(cmd[2], cp = "Sender")){
1045 npp = &hp->h_sender;
1046 goto jrem;
1048 if(!asccasecmp(cmd[2], cp = "To")){
1049 npp = &hp->h_to;
1050 goto jrem;
1052 if(!asccasecmp(cmd[2], cp = "Cc")){
1053 npp = &hp->h_cc;
1054 goto jrem;
1056 if(!asccasecmp(cmd[2], cp = "Bcc")){
1057 npp = &hp->h_bcc;
1058 goto jrem;
1060 if(!asccasecmp(cmd[2], cp = "Reply-To")){
1061 npp = &hp->h_reply_to;
1062 goto jrem;
1064 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
1065 npp = &hp->h_mft;
1066 goto jrem;
1068 if(!asccasecmp(cmd[2], cp = "Message-ID")){
1069 npp = &hp->h_message_id;
1070 goto jrem;
1072 if(!asccasecmp(cmd[2], cp = "References")){
1073 npp = &hp->h_ref;
1074 goto jrem;
1076 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
1077 npp = &hp->h_in_reply_to;
1078 goto jrem;
1081 if((cp = n_header_is_standard(cmd[2], UIZ_MAX)) != NULL){
1082 fprintf(n_stdout, "505 %s\n", cp);
1083 goto jleave;
1086 /* Free-form header fields (note j501cp may print non-normalized name) */
1087 /* C99 */{
1088 struct n_header_field **hfpp;
1089 bool_t any;
1091 for(cp = cmd[2]; *cp != '\0'; ++cp)
1092 if(!fieldnamechar(*cp)){
1093 cp = cmd[2];
1094 goto j501cp;
1096 cp = cmd[2];
1098 for(any = FAL0, hfpp = &hp->h_user_headers; (hfp = *hfpp) != NULL;){
1099 if(!asccasecmp(cp, &hfp->hf_dat[0])){
1100 *hfpp = hfp->hf_next;
1101 if(!any)
1102 fprintf(n_stdout, "210 %s\n", &hfp->hf_dat[0]);
1103 any = TRU1;
1104 }else
1105 hfpp = &hfp->hf_next;
1107 if(!any)
1108 goto j501cp;
1110 goto jleave;
1113 if(is_asccaseprefix(cmd[1], "remove-at")){
1114 if(cmd[2] == NULL || cmd[3] == NULL)
1115 goto jecmd;
1117 if((n_idec_uiz_cp(&i, cmd[3], 0, NULL
1118 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
1119 ) != n_IDEC_STATE_CONSUMED || i == 0){
1120 fputs("505\n", n_stdout);
1121 goto jleave;
1124 if(!asccasecmp(cmd[2], cp = "Subject")){
1125 if(hp->h_subject != NULL && i == 1){
1126 hp->h_subject = NULL;
1127 fprintf(n_stdout, "210 %s 1\n", cp);
1128 goto jleave;
1129 }else
1130 goto j501cp;
1133 if(!asccasecmp(cmd[2], cp = "From")){
1134 npp = &hp->h_from;
1135 jremat:
1136 if((np = *npp) == NULL)
1137 goto j501cp;
1138 while(--i != 0 && np != NULL)
1139 np = np->n_flink;
1140 if(np == NULL)
1141 goto j501cp;
1143 if(np->n_blink != NULL)
1144 np->n_blink->n_flink = np->n_flink;
1145 else
1146 *npp = np->n_flink;
1147 if(np->n_flink != NULL)
1148 np->n_flink->n_blink = np->n_blink;
1150 fprintf(n_stdout, "210 %s\n", cp);
1151 goto jleave;
1153 if(!asccasecmp(cmd[2], cp = "Sender")){
1154 npp = &hp->h_sender;
1155 goto jremat;
1157 if(!asccasecmp(cmd[2], cp = "To")){
1158 npp = &hp->h_to;
1159 goto jremat;
1161 if(!asccasecmp(cmd[2], cp = "Cc")){
1162 npp = &hp->h_cc;
1163 goto jremat;
1165 if(!asccasecmp(cmd[2], cp = "Bcc")){
1166 npp = &hp->h_bcc;
1167 goto jremat;
1169 if(!asccasecmp(cmd[2], cp = "Reply-To")){
1170 npp = &hp->h_reply_to;
1171 goto jremat;
1173 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
1174 npp = &hp->h_mft;
1175 goto jremat;
1177 if(!asccasecmp(cmd[2], cp = "Message-ID")){
1178 npp = &hp->h_message_id;
1179 goto jremat;
1181 if(!asccasecmp(cmd[2], cp = "References")){
1182 npp = &hp->h_ref;
1183 goto jremat;
1185 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
1186 npp = &hp->h_in_reply_to;
1187 goto jremat;
1190 if((cp = n_header_is_standard(cmd[2], UIZ_MAX)) != NULL){
1191 fprintf(n_stdout, "505 %s\n", cp);
1192 goto jleave;
1195 /* Free-form header fields */
1196 /* C99 */{
1197 struct n_header_field **hfpp;
1199 for(cp = cmd[2]; *cp != '\0'; ++cp)
1200 if(!fieldnamechar(*cp)){
1201 cp = cmd[2];
1202 goto j501cp;
1204 cp = cmd[2];
1206 for(hfpp = &hp->h_user_headers; (hfp = *hfpp) != NULL;){
1207 if(--i == 0){
1208 *hfpp = hfp->hf_next;
1209 fprintf(n_stdout, "210 %s %" PRIuZ "\n", &hfp->hf_dat[0], i);
1210 break;
1211 }else
1212 hfpp = &hfp->hf_next;
1214 if(hfp == NULL)
1215 goto j501cp;
1217 goto jleave;
1220 if(is_asccaseprefix(cmd[1], "show")){
1221 if(cmd[2] == NULL || cmd[3] != NULL)
1222 goto jecmd;
1224 if(!asccasecmp(cmd[2], cp = "Subject")){
1225 if(hp->h_subject == NULL)
1226 goto j501cp;
1227 fprintf(n_stdout, "212 %s\n%s\n\n", cp, hp->h_subject);
1228 goto jleave;
1231 if(!asccasecmp(cmd[2], cp = "From")){
1232 np = hp->h_from;
1233 jshow:
1234 if(np != NULL){
1235 fprintf(n_stdout, "211 %s\n", cp);
1236 do if(!(np->n_type & GDEL))
1237 fprintf(n_stdout, "%s %s\n", np->n_name, np->n_fullname);
1238 while((np = np->n_flink) != NULL);
1239 putc('\n', n_stdout);
1240 goto jleave;
1241 }else
1242 goto j501cp;
1244 if(!asccasecmp(cmd[2], cp = "Sender")){
1245 np = hp->h_sender;
1246 goto jshow;
1248 if(!asccasecmp(cmd[2], cp = "To")){
1249 np = hp->h_to;
1250 goto jshow;
1252 if(!asccasecmp(cmd[2], cp = "Cc")){
1253 np = hp->h_cc;
1254 goto jshow;
1256 if(!asccasecmp(cmd[2], cp = "Bcc")){
1257 np = hp->h_bcc;
1258 goto jshow;
1260 if(!asccasecmp(cmd[2], cp = "Reply-To")){
1261 np = hp->h_reply_to;
1262 goto jshow;
1264 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
1265 np = hp->h_mft;
1266 goto jshow;
1268 if(!asccasecmp(cmd[2], cp = "Message-ID")){
1269 np = hp->h_message_id;
1270 goto jshow;
1272 if(!asccasecmp(cmd[2], cp = "References")){
1273 np = hp->h_ref;
1274 goto jshow;
1276 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
1277 np = hp->h_in_reply_to;
1278 goto jshow;
1281 if(!asccasecmp(cmd[2], cp = "Mailx-Command")){
1282 if(hp->h_mailx_command == NULL)
1283 goto j501cp;
1284 fprintf(n_stdout, "212 %s\n%s\n\n", cp, hp->h_mailx_command);
1285 goto jleave;
1287 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-To")){
1288 np = hp->h_mailx_raw_to;
1289 goto jshow;
1291 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-Cc")){
1292 np = hp->h_mailx_raw_cc;
1293 goto jshow;
1295 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-Bcc")){
1296 np = hp->h_mailx_raw_bcc;
1297 goto jshow;
1299 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-From")){
1300 np = hp->h_mailx_orig_from;
1301 goto jshow;
1303 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-To")){
1304 np = hp->h_mailx_orig_to;
1305 goto jshow;
1307 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-Cc")){
1308 np = hp->h_mailx_orig_cc;
1309 goto jshow;
1311 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-Bcc")){
1312 np = hp->h_mailx_orig_bcc;
1313 goto jshow;
1316 /* Free-form header fields */
1317 /* C99 */{
1318 bool_t any;
1320 for(cp = cmd[2]; *cp != '\0'; ++cp)
1321 if(!fieldnamechar(*cp)){
1322 cp = cmd[2];
1323 goto j501cp;
1325 cp = cmd[2];
1327 for(any = FAL0, hfp = hp->h_user_headers; hfp != NULL;
1328 hfp = hfp->hf_next){
1329 if(!asccasecmp(cp, &hfp->hf_dat[0])){
1330 if(!any)
1331 fprintf(n_stdout, "212 %s\n", &hfp->hf_dat[0]);
1332 any = TRU1;
1333 fprintf(n_stdout, "%s\n", &hfp->hf_dat[hfp->hf_nl +1]);
1336 if(any)
1337 putc('\n', n_stdout);
1338 else
1339 goto j501cp;
1341 goto jleave;
1344 jecmd:
1345 fputs("500\n", n_stdout);
1346 cp = NULL;
1347 jleave:
1348 NYD2_LEAVE;
1349 return (cp != NULL);
1351 j501cp:
1352 fputs("501 ", n_stdout);
1353 fputs(cp, n_stdout);
1354 putc('\n', n_stdout);
1355 goto jleave;
1358 static bool_t
1359 a_collect__plumb_attach(char const *cp, struct header *hp,
1360 char const *cmd[4]){
1361 bool_t status;
1362 struct attachment *ap;
1363 NYD2_ENTER;
1365 if(cmd[1] == NULL)
1366 goto jdefault;
1368 if(is_asccaseprefix(cmd[1], "attribute")){
1369 if(cmd[2] == NULL || cmd[3] != NULL)
1370 goto jecmd;
1372 if((ap = n_attachment_find(hp->h_attach, cmd[2], NULL)) != NULL){
1373 jatt_att:
1374 fprintf(n_stdout, "212 %s\n", cmd[2]);
1375 if(ap->a_msgno > 0)
1376 fprintf(n_stdout, "message-number %d\n\n", ap->a_msgno);
1377 else{
1378 fprintf(n_stdout,
1379 "creation-name %s\nopen-path %s\nfilename %s\n",
1380 ap->a_path_user, ap->a_path, ap->a_name);
1381 if(ap->a_content_description != NULL)
1382 fprintf(n_stdout, "content-description %s\n",
1383 ap->a_content_description);
1384 if(ap->a_content_id != NULL)
1385 fprintf(n_stdout, "content-id %s\n",
1386 ap->a_content_id->n_name);
1387 if(ap->a_content_type != NULL)
1388 fprintf(n_stdout, "content-type %s\n", ap->a_content_type);
1389 if(ap->a_content_disposition != NULL)
1390 fprintf(n_stdout, "content-disposition %s\n",
1391 ap->a_content_disposition);
1392 putc('\n', n_stdout);
1394 }else
1395 fputs("501\n", n_stdout);
1396 goto jleave;
1399 if(is_asccaseprefix(cmd[1], "attribute-at")){
1400 uiz_t i;
1402 if(cmd[2] == NULL || cmd[3] != NULL)
1403 goto jecmd;
1405 if((n_idec_uiz_cp(&i, cmd[2], 0, NULL
1406 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
1407 ) != n_IDEC_STATE_CONSUMED || i == 0)
1408 fputs("505\n", n_stdout);
1409 else{
1410 for(ap = hp->h_attach; ap != NULL && --i != 0; ap = ap->a_flink)
1412 if(ap != NULL)
1413 goto jatt_att;
1414 else
1415 fputs("501\n", n_stdout);
1417 goto jleave;
1420 if(is_asccaseprefix(cmd[1], "attribute-set")){
1421 if(cmd[2] == NULL || cmd[3] == NULL)
1422 goto jecmd;
1424 if((ap = n_attachment_find(hp->h_attach, cmd[2], NULL)) != NULL){
1425 jatt_attset:
1426 if(ap->a_msgno > 0)
1427 fputs("505\n", n_stdout);
1428 else{
1429 char c, *keyw;
1431 cp = cmd[3];
1432 while((c = *cp) != '\0' && !blankchar(c))
1433 ++cp;
1434 keyw = savestrbuf(cmd[3], PTR2SIZE(cp - cmd[3]));
1435 if(c != '\0'){
1436 for(; (c = *++cp) != '\0' && blankchar(c);)
1438 if(c != '\0'){
1439 char *xp;
1441 /* Strip [\r\n] which would render a parameter invalid XXX
1442 * XXX all controls? */
1443 cp = xp = savestr(cp);
1444 for(; (c = *xp) != '\0'; ++xp)
1445 if(c == '\n' || c == '\r')
1446 *xp = ' ';
1447 c = *cp;
1451 if(!asccasecmp(keyw, "filename"))
1452 ap->a_name = (c == '\0') ? ap->a_path_bname : cp;
1453 else if(!asccasecmp(keyw, "content-description"))
1454 ap->a_content_description = (c == '\0') ? NULL : cp;
1455 else if(!asccasecmp(keyw, "content-id")){
1456 ap->a_content_id = NULL;
1458 if(c != '\0'){
1459 struct name *np;
1461 np = checkaddrs(lextract(cp, GREF),
1462 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG |
1463 EACM_NONAME, NULL);
1464 if(np != NULL && np->n_flink == NULL)
1465 ap->a_content_id = np;
1466 else
1467 cp = NULL;
1469 }else if(!asccasecmp(keyw, "content-type"))
1470 ap->a_content_type = (c == '\0') ? NULL : cp;
1471 else if(!asccasecmp(keyw, "content-disposition"))
1472 ap->a_content_disposition = (c == '\0') ? NULL : cp;
1473 else
1474 cp = NULL;
1476 if(cp != NULL){
1477 size_t i;
1479 for(i = 0; ap != NULL; ++i, ap = ap->a_blink)
1481 fprintf(n_stdout, "210 %" PRIuZ "\n", i);
1482 }else
1483 fputs("505\n", n_stdout);
1485 }else
1486 fputs("501\n", n_stdout);
1487 goto jleave;
1490 if(is_asccaseprefix(cmd[1], "attribute-set-at")){
1491 uiz_t i;
1493 if(cmd[2] == NULL || cmd[3] == NULL)
1494 goto jecmd;
1496 if((n_idec_uiz_cp(&i, cmd[2], 0, NULL
1497 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
1498 ) != n_IDEC_STATE_CONSUMED || i == 0)
1499 fputs("505\n", n_stdout);
1500 else{
1501 for(ap = hp->h_attach; ap != NULL && --i != 0; ap = ap->a_flink)
1503 if(ap != NULL)
1504 goto jatt_attset;
1505 else
1506 fputs("501\n", n_stdout);
1508 goto jleave;
1511 if(is_asccaseprefix(cmd[1], "insert")){
1512 enum n_attach_error aerr;
1514 if(cmd[2] == NULL || cmd[3] != NULL)
1515 goto jecmd;
1517 hp->h_attach = n_attachment_append(hp->h_attach, cmd[2], &aerr, &ap);
1518 switch(aerr){
1519 case n_ATTACH_ERR_FILE_OPEN: cp = "505\n"; goto jatt_ins;
1520 case n_ATTACH_ERR_ICONV_FAILED: cp = "506\n"; goto jatt_ins;
1521 case n_ATTACH_ERR_ICONV_NAVAIL:
1522 case n_ATTACH_ERR_OTHER:
1523 default:
1524 cp = "501\n";
1525 jatt_ins:
1526 fputs(cp, n_stdout);
1527 break;
1528 case n_ATTACH_ERR_NONE:{
1529 size_t i;
1531 for(i = 0; ap != NULL; ++i, ap = ap->a_blink)
1533 fprintf(n_stdout, "210 %" PRIuZ "\n", i);
1534 }break;
1536 goto jleave;
1539 if(is_asccaseprefix(cmd[1], "list")){
1540 jdefault:
1541 if(cmd[2] != NULL)
1542 goto jecmd;
1544 if((ap = hp->h_attach) != NULL){
1545 fputs("212\n", n_stdout);
1547 fprintf(n_stdout, "%s\n", ap->a_path_user);
1548 while((ap = ap->a_flink) != NULL);
1549 putc('\n', n_stdout);
1550 }else
1551 fputs("501\n", n_stdout);
1552 goto jleave;
1555 if(is_asccaseprefix(cmd[1], "remove")){
1556 if(cmd[2] == NULL || cmd[3] != NULL)
1557 goto jecmd;
1559 if((ap = n_attachment_find(hp->h_attach, cmd[2], &status)) != NULL){
1560 if(status == TRUM1)
1561 fputs("506\n", n_stdout);
1562 else{
1563 hp->h_attach = n_attachment_remove(hp->h_attach, ap);
1564 fprintf(n_stdout, "210 %s\n", cmd[2]);
1566 }else
1567 fputs("501\n", n_stdout);
1568 goto jleave;
1571 if(is_asccaseprefix(cmd[1], "remove-at")){
1572 uiz_t i;
1574 if(cmd[2] == NULL || cmd[3] != NULL)
1575 goto jecmd;
1577 if((n_idec_uiz_cp(&i, cmd[2], 0, NULL
1578 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
1579 ) != n_IDEC_STATE_CONSUMED || i == 0)
1580 fputs("505\n", n_stdout);
1581 else{
1582 for(ap = hp->h_attach; ap != NULL && --i != 0; ap = ap->a_flink)
1584 if(ap != NULL){
1585 hp->h_attach = n_attachment_remove(hp->h_attach, ap);
1586 fprintf(n_stdout, "210 %s\n", cmd[2]);
1587 }else
1588 fputs("501\n", n_stdout);
1590 goto jleave;
1593 jecmd:
1594 fputs("500\n", n_stdout);
1595 cp = NULL;
1596 jleave:
1597 NYD2_LEAVE;
1598 return (cp != NULL);
1601 static void
1602 _collint(int s)
1604 NYD_X; /* Signal handler */
1606 /* the control flow is subtle, because we can be called from ~q */
1607 if (_coll_hadintr == 0) {
1608 if (ok_blook(ignore)) {
1609 fputs("@\n", n_stdout);
1610 fflush(n_stdout);
1611 clearerr(n_stdin);
1612 } else
1613 _coll_hadintr = 1;
1614 siglongjmp(_coll_jmp, 1);
1616 n_exit_status |= n_EXIT_SEND_ERROR;
1617 if (s != 0)
1618 savedeadletter(_coll_fp, TRU1);
1619 /* Aborting message, no need to fflush() .. */
1620 siglongjmp(_coll_abort, 1);
1623 static void
1624 collhup(int s)
1626 NYD_X; /* Signal handler */
1627 n_UNUSED(s);
1629 savedeadletter(_coll_fp, TRU1);
1630 /* Let's pretend nobody else wants to clean up, a true statement at
1631 * this time */
1632 exit(n_EXIT_ERR);
1635 static bool_t
1636 a_coll_putesc(char const *s, bool_t addnl, FILE *stream){
1637 char c1, c2;
1638 bool_t isposix;
1639 NYD2_ENTER;
1641 isposix = ok_blook(posix);
1643 while((c1 = *s++) != '\0'){
1644 if(c1 == '\\' && ((c2 = *s) == 't' || c2 == 'n')){
1645 if(!isposix){
1646 isposix = TRU1; /* TODO v15 OBSOLETE! */
1647 n_err(_("Compose mode warning: expanding \\t or \\n in variable "
1648 "without *posix*!"
1649 "\n Support remains only for ~A,~a,~I,~i in *posix* mode!\n"));
1651 ++s;
1652 c1 = (c2 == 't') ? '\t' : '\n';
1655 if(putc(c1, stream) == EOF)
1656 goto jleave;
1659 if(addnl && putc('\n', stream) == EOF)
1660 goto jleave;
1662 jleave:
1663 NYD2_LEAVE;
1664 return (c1 == '\0');
1667 static int
1668 a_coll_ocs__mac(void){
1669 /* Executes in a fork(2)ed child TODO if remains, global MASKs for those! */
1670 setvbuf(n_stdin, NULL, _IOLBF, 0);
1671 setvbuf(n_stdout, NULL, _IOLBF, 0);
1672 n_psonce &= ~(n_PSO_INTERACTIVE | n_PSO_TTYIN | n_PSO_TTYOUT);
1673 n_pstate |= n_PS_COMPOSE_FORKHOOK;
1674 n_readctl_overlay = NULL; /* TODO we need OnForkEvent! See c_readctl() */
1675 if(n_poption & n_PO_D_VV){
1676 char buf[128];
1678 snprintf(buf, sizeof buf, "[%d]%s", getpid(), ok_vlook(log_prefix));
1679 ok_vset(log_prefix, buf);
1681 /* TODO If that uses `!' it will effectively SIG_IGN SIGINT, ...and such */
1682 temporary_compose_mode_hook_call(a_coll_ocs__macname, NULL, NULL);
1683 return 0;
1686 static void
1687 a_coll_ocs__finalize(void *vp){
1688 /* Note we use this for destruction upon setup errors, thus */
1689 sighandler_type opipe;
1690 sighandler_type oint;
1691 struct a_coll_ocs_arg **coapp, *coap;
1692 NYD2_ENTER;
1694 temporary_compose_mode_hook_call((char*)-1, NULL, NULL);
1696 coap = *(coapp = vp);
1697 *coapp = (struct a_coll_ocs_arg*)-1;
1699 if(coap->coa_stdin != NULL)
1700 Fclose(coap->coa_stdin);
1701 else if(coap->coa_pipe[0] != -1)
1702 close(coap->coa_pipe[0]);
1704 if(coap->coa_stdout != NULL && !Pclose(coap->coa_stdout, TRU1))
1705 *coap->coa_senderr = 111;
1706 if(coap->coa_pipe[1] != -1)
1707 close(coap->coa_pipe[1]);
1709 opipe = coap->coa_opipe;
1710 oint = coap->coa_oint;
1712 n_lofi_free(coap);
1714 hold_all_sigs();
1715 safe_signal(SIGPIPE, opipe);
1716 safe_signal(SIGINT, oint);
1717 rele_all_sigs();
1718 NYD2_LEAVE;
1721 FL void
1722 n_temporary_compose_hook_varset(void *arg){ /* TODO v15: drop */
1723 struct header *hp;
1724 char const *val;
1725 NYD2_ENTER;
1727 hp = arg;
1729 if((val = hp->h_subject) == NULL)
1730 val = n_empty;
1731 ok_vset(mailx_subject, val);
1732 if((val = detract(hp->h_from, GNAMEONLY)) == NULL)
1733 val = n_empty;
1734 ok_vset(mailx_from, val);
1735 if((val = detract(hp->h_sender, GNAMEONLY)) == NULL)
1736 val = n_empty;
1737 ok_vset(mailx_sender, val);
1738 if((val = detract(hp->h_to, GNAMEONLY)) == NULL)
1739 val = n_empty;
1740 ok_vset(mailx_to, val);
1741 if((val = detract(hp->h_cc, GNAMEONLY)) == NULL)
1742 val = n_empty;
1743 ok_vset(mailx_cc, val);
1744 if((val = detract(hp->h_bcc, GNAMEONLY)) == NULL)
1745 val = n_empty;
1746 ok_vset(mailx_bcc, val);
1748 if((val = hp->h_mailx_command) == NULL)
1749 val = n_empty;
1750 ok_vset(mailx_command, val);
1752 if((val = detract(hp->h_mailx_raw_to, GNAMEONLY)) == NULL)
1753 val = n_empty;
1754 ok_vset(mailx_raw_to, val);
1755 if((val = detract(hp->h_mailx_raw_cc, GNAMEONLY)) == NULL)
1756 val = n_empty;
1757 ok_vset(mailx_raw_cc, val);
1758 if((val = detract(hp->h_mailx_raw_bcc, GNAMEONLY)) == NULL)
1759 val = n_empty;
1760 ok_vset(mailx_raw_bcc, val);
1762 if((val = detract(hp->h_mailx_orig_from, GNAMEONLY)) == NULL)
1763 val = n_empty;
1764 ok_vset(mailx_orig_from, val);
1765 if((val = detract(hp->h_mailx_orig_to, GNAMEONLY)) == NULL)
1766 val = n_empty;
1767 ok_vset(mailx_orig_to, val);
1768 if((val = detract(hp->h_mailx_orig_cc, GNAMEONLY)) == NULL)
1769 val = n_empty;
1770 ok_vset(mailx_orig_cc, val);
1771 if((val = detract(hp->h_mailx_orig_bcc, GNAMEONLY)) == NULL)
1772 val = n_empty;
1773 ok_vset(mailx_orig_bcc, val);
1774 NYD2_LEAVE;
1777 FL FILE *
1778 collect(struct header *hp, int printheaders, struct message *mp,
1779 char const *quotefile, int doprefix, si8_t *checkaddr_err)
1781 struct n_string s, * volatile sp;
1782 struct n_ignore const *quoteitp;
1783 struct a_coll_ocs_arg *coap;
1784 int c;
1785 int volatile t, eofcnt, getfields;
1786 char volatile escape;
1787 enum{
1788 a_NONE,
1789 a_ERREXIT = 1u<<0,
1790 a_IGNERR = 1u<<1,
1791 a_COAP_NOSIGTERM = 1u<<8
1792 #define a_HARDERR() ((flags & (a_ERREXIT | a_IGNERR)) == a_ERREXIT)
1793 } volatile flags;
1794 char *linebuf;
1795 char const *cp, *cp_base, * volatile coapm, * volatile ifs_saved;
1796 size_t i, linesize; /* TODO line pool */
1797 long cnt;
1798 enum sendaction action;
1799 sigset_t oset, nset;
1800 FILE * volatile sigfp;
1801 NYD_ENTER;
1803 _coll_fp = NULL;
1804 sigfp = NULL;
1805 linesize = 0;
1806 linebuf = NULL;
1807 flags = a_NONE;
1808 eofcnt = 0;
1809 ifs_saved = coapm = NULL;
1810 coap = NULL;
1811 sp = NULL;
1813 /* Start catching signals from here, but we're still die on interrupts
1814 * until we're in the main loop */
1815 sigfillset(&nset);
1816 sigprocmask(SIG_BLOCK, &nset, &oset);
1817 if ((_coll_saveint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
1818 safe_signal(SIGINT, &_collint);
1819 if ((_coll_savehup = safe_signal(SIGHUP, SIG_IGN)) != SIG_IGN)
1820 safe_signal(SIGHUP, collhup);
1821 if (sigsetjmp(_coll_abort, 1))
1822 goto jerr;
1823 if (sigsetjmp(_coll_jmp, 1))
1824 goto jerr;
1825 n_pstate |= n_PS_COMPOSE_MODE;
1826 sigprocmask(SIG_SETMASK, &oset, (sigset_t*)NULL);
1828 if ((_coll_fp = Ftmp(NULL, "collect", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
1829 NULL) {
1830 n_perr(_("collect: temporary mail file"), 0);
1831 goto jerr;
1834 /* If we are going to prompt for a subject, refrain from printing a newline
1835 * after the headers (since some people mind) */
1836 getfields = 0;
1837 if(!(n_poption & n_PO_t_FLAG)){
1838 t = GTO | GSUBJECT | GCC | GNL;
1839 if(ok_blook(fullnames))
1840 t |= GCOMMA;
1842 if((n_psonce & n_PSO_INTERACTIVE) && !(n_pstate & n_PS_ROBOT)){
1843 if(hp->h_subject == NULL && ok_blook(asksub)/* *ask* auto warped! */)
1844 t &= ~GNL, getfields |= GSUBJECT;
1846 if(hp->h_to == NULL)
1847 t &= ~GNL, getfields |= GTO;
1849 if(!ok_blook(bsdcompat) && !ok_blook(askatend)){
1850 if(ok_blook(askbcc))
1851 t &= ~GNL, getfields |= GBCC;
1852 if(ok_blook(askcc))
1853 t &= ~GNL, getfields |= GCC;
1856 }else{
1857 n_UNINIT(t, 0);
1860 _coll_hadintr = 0;
1862 if (!sigsetjmp(_coll_jmp, 1)) {
1863 /* Ask for some headers first, as necessary */
1864 if (getfields)
1865 grab_headers(n_GO_INPUT_CTX_COMPOSE, hp, getfields, 1);
1867 /* Execute compose-enter; delayed for -t mode */
1868 if(!(n_poption & n_PO_t_FLAG) &&
1869 (cp = ok_vlook(on_compose_enter)) != NULL){
1870 setup_from_and_sender(hp);
1871 temporary_compose_mode_hook_call(cp, &n_temporary_compose_hook_varset,
1872 hp);
1875 /* TODO Mm: nope since it may require turning this into a multipart one */
1876 if(!(n_poption & (n_PO_Mm_FLAG | n_PO_t_FLAG))){
1877 if(!a_coll_message_inject_head(_coll_fp))
1878 goto jerr;
1880 /* Quote an original message */
1881 if (mp != NULL && (doprefix || (cp = ok_vlook(quote)) != NULL)) {
1882 quoteitp = n_IGNORE_ALL;
1883 action = SEND_QUOTE;
1884 if (doprefix) {
1885 char const *cp_v15compat;
1887 quoteitp = n_IGNORE_FWD;
1889 if((cp_v15compat = ok_vlook(fwdheading)) != NULL)
1890 n_OBSOLETE(_("please use *forward-inject-head*, "
1891 "not *fwdheading*"));
1892 cp = ok_vlook(forward_inject_head);
1893 if(cp == NULL && (cp = cp_v15compat) == NULL)
1894 cp = "-------- Original Message --------";
1895 if (*cp != '\0' && fprintf(_coll_fp, "%s\n", cp) < 0)
1896 goto jerr;
1897 } else if (!strcmp(cp, "noheading")) {
1898 /*EMPTY*/;
1899 } else if (!strcmp(cp, "headers")) {
1900 quoteitp = n_IGNORE_TYPE;
1901 } else if (!strcmp(cp, "allheaders")) {
1902 quoteitp = NULL;
1903 action = SEND_QUOTE_ALL;
1904 } else {
1905 cp = hfield1("from", mp);
1906 if (cp != NULL && (cnt = (long)strlen(cp)) > 0) {
1907 if (xmime_write(cp, cnt, _coll_fp, CONV_FROMHDR, TD_NONE,
1908 NULL, NULL) < 0)
1909 goto jerr;
1910 if (fprintf(_coll_fp, _(" wrote:\n\n")) < 0)
1911 goto jerr;
1914 if (fflush(_coll_fp))
1915 goto jerr;
1916 if (sendmp(mp, _coll_fp, quoteitp,
1917 (doprefix ? NULL : ok_vlook(indentprefix)),
1918 action, NULL) < 0)
1919 goto jerr;
1923 if (quotefile != NULL) {
1924 if((n_pstate_err_no = a_coll_include_file(quotefile, FAL0, FAL0)
1925 ) != n_ERR_NONE)
1926 goto jerr;
1929 if(n_psonce & n_PSO_INTERACTIVE){
1930 if(!(n_pstate & n_PS_SOURCING)){
1931 sp = n_string_creat_auto(&s);
1932 sp = n_string_reserve(sp, 80);
1935 if(!(n_poption & n_PO_Mm_FLAG) && !(n_pstate & n_PS_ROBOT)){
1936 /* Print what we have sofar also on the terminal (if useful) */
1937 if((cp = ok_vlook(editalong)) == NULL){
1938 if (printheaders)
1939 puthead(TRU1, hp, n_stdout, t,
1940 SEND_TODISP, CONV_NONE, NULL, NULL);
1942 rewind(_coll_fp);
1943 while ((c = getc(_coll_fp)) != EOF) /* XXX bytewise, yuck! */
1944 putc(c, n_stdout);
1945 if (fseek(_coll_fp, 0, SEEK_END))
1946 goto jerr;
1947 }else{
1948 rewind(_coll_fp);
1949 if(a_coll_edit(((*cp == 'v') ? 'v' : 'e'), hp) != n_ERR_NONE)
1950 goto jerr;
1951 /* Print msg mandated by the Mail Reference Manual */
1952 jcont:
1953 if((n_psonce & n_PSO_INTERACTIVE) && !(n_pstate & n_PS_ROBOT))
1954 fputs(_("(continue)\n"), n_stdout);
1956 fflush(n_stdout);
1959 } else {
1960 /* Come here for printing the after-signal message. Duplicate messages
1961 * won't be printed because the write is aborted if we get a SIGTTOU */
1962 if(_coll_hadintr && (n_psonce & n_PSO_INTERACTIVE) &&
1963 !(n_pstate & n_PS_ROBOT))
1964 n_err(_("\n(Interrupt -- one more to kill letter)\n"));
1967 /* If not under shell hook control */
1968 if(coap == NULL){
1969 /* We're done with -M or -m TODO because: we are too stupid yet, above */
1970 if(n_poption & n_PO_Mm_FLAG)
1971 goto jout;
1972 /* No command escapes, interrupts not expected. Simply copy STDIN */
1973 if(!(n_psonce & n_PSO_INTERACTIVE) &&
1974 !(n_poption & (n_PO_t_FLAG | n_PO_TILDE_FLAG))){
1975 linebuf = n_realloc(linebuf, linesize = LINESIZE);
1976 while ((i = fread(linebuf, sizeof *linebuf, linesize, n_stdin)) > 0) {
1977 if (i != fwrite(linebuf, sizeof *linebuf, i, _coll_fp))
1978 goto jerr;
1980 goto jout;
1984 /* The interactive collect loop */
1985 if(coap == NULL)
1986 escape = *ok_vlook(escape);
1987 flags = ok_blook(errexit) ? a_ERREXIT : a_NONE;
1989 for(;;){
1990 enum {a_HIST_NONE, a_HIST_ADD = 1u<<0, a_HIST_GABBY = 1u<<1} hist;
1992 /* C99 */{
1993 enum n_go_input_flags gif;
1994 bool_t histadd;
1996 /* TODO optimize: no need to evaluate that anew for each loop tick! */
1997 gif = n_GO_INPUT_CTX_COMPOSE;
1998 histadd = (sp != NULL);
1999 if((n_psonce & n_PSO_INTERACTIVE) || (n_poption & n_PO_TILDE_FLAG)){
2000 if(!(n_poption & n_PO_t_FLAG) || (n_psonce & n_PSO_t_FLAG_DONE))
2001 gif |= n_GO_INPUT_NL_ESC;
2003 cnt = n_go_input(gif, n_empty, &linebuf, &linesize, NULL, &histadd);
2004 hist = histadd ? a_HIST_ADD | a_HIST_GABBY : a_HIST_NONE;
2007 if(cnt < 0){
2008 if(coap != NULL)
2009 break;
2010 if((n_poption & n_PO_t_FLAG) && !(n_psonce & n_PSO_t_FLAG_DONE)){
2011 fflush_rewind(_coll_fp);
2012 n_psonce |= n_PSO_t_FLAG_DONE;
2013 if(!a_coll_makeheader(_coll_fp, hp, checkaddr_err, TRU1))
2014 goto jerr;
2015 continue;
2016 }else if((n_psonce & n_PSO_INTERACTIVE) && !(n_pstate & n_PS_ROBOT) &&
2017 ok_blook(ignoreeof) && ++eofcnt < 4){
2018 fprintf(n_stdout,
2019 _("*ignoreeof* set, use `~.' to terminate letter\n"));
2020 n_go_input_clearerr();
2021 continue;
2023 break;
2026 _coll_hadintr = 0;
2028 cp = linebuf;
2029 if(cnt == 0)
2030 goto jputnl;
2031 else if(coap == NULL){
2032 if(!(n_psonce & n_PSO_INTERACTIVE) && !(n_poption & n_PO_TILDE_FLAG))
2033 goto jputline;
2034 else if(cp[0] == '.'){
2035 if(cnt == 1 && (ok_blook(dot) ||
2036 (ok_blook(posix) && ok_blook(ignoreeof))))
2037 break;
2040 if(cp[0] != escape){
2041 jputline:
2042 if(fwrite(cp, sizeof *cp, cnt, _coll_fp) != (size_t)cnt)
2043 goto jerr;
2044 /* TODO n_PS_READLINE_NL is a terrible hack to ensure that _in_all_-
2045 * TODO _code_paths_ a file without trailing newline isn't modified
2046 * TODO to contain one; the "saw-newline" needs to be part of an
2047 * TODO I/O input machinery object */
2048 jputnl:
2049 if(n_pstate & n_PS_READLINE_NL){
2050 if(putc('\n', _coll_fp) == EOF)
2051 goto jerr;
2053 continue;
2056 c = *(cp_base = ++cp);
2057 if(--cnt == 0)
2058 goto jearg;
2060 /* Avoid history entry? */
2061 while(spacechar(c)){
2062 hist = a_HIST_NONE;
2063 c = *(cp_base = ++cp);
2064 if(--cnt == 0)
2065 goto jearg;
2068 /* It may just be an escaped escaped character, do that quick */
2069 if(c == escape)
2070 goto jputline;
2072 /* Avoid hard *errexit*? */
2073 flags &= ~a_IGNERR;
2074 if(c == '-'){
2075 flags ^= a_IGNERR;
2076 c = *++cp;
2077 if(--cnt == 0)
2078 goto jearg;
2081 /* Trim input, also to gain a somewhat normalized history entry */
2082 ++cp;
2083 if(--cnt > 0){
2084 struct str x;
2086 x.s = n_UNCONST(cp);
2087 x.l = (size_t)cnt;
2088 n_str_trim_ifs(&x, TRU1);
2089 x.s[x.l] = '\0';
2090 cp = x.s;
2091 cnt = (int)/*XXX*/x.l;
2094 if(hist != a_HIST_NONE){
2095 sp = n_string_assign_c(sp, escape);
2096 if(flags & a_IGNERR)
2097 sp = n_string_push_c(sp, '-');
2098 sp = n_string_push_c(sp, c);
2099 if(cnt > 0){
2100 sp = n_string_push_c(sp, ' ');
2101 sp = n_string_push_buf(sp, cp, cnt);
2105 /* Switch over all command escapes */
2106 switch(c){
2107 default:
2108 if(1){
2109 char buf[sizeof(n_UNIREPL)];
2111 if(asciichar(c))
2112 buf[0] = c, buf[1] = '\0';
2113 else if(n_psonce & n_PSO_UNICODE)
2114 memcpy(buf, n_unirepl, sizeof n_unirepl);
2115 else
2116 buf[0] = '?', buf[1] = '\0';
2117 n_err(_("Unknown command escape: `%c%s'\n"), escape, buf);
2118 }else
2119 jearg:
2120 n_err(_("Invalid command escape usage: %s\n"),
2121 n_shexp_quote_cp(linebuf, FAL0));
2122 if(a_HARDERR())
2123 goto jerr;
2124 n_pstate_err_no = n_ERR_INVAL;
2125 n_pstate_ex_no = 1;
2126 continue;
2127 case '!':
2128 /* Shell escape, send the balance of line to sh -c */
2129 if(cnt == 0 || coap != NULL)
2130 goto jearg;
2131 else{
2132 char const *argv[2];
2134 argv[0] = cp;
2135 argv[1] = NULL;
2136 n_pstate_ex_no = c_shell(argv); /* TODO history norm.; errexit? */
2138 goto jhistcont;
2139 case '.':
2140 /* Simulate end of file on input */
2141 if(cnt != 0 || coap != NULL)
2142 goto jearg;
2143 goto jout; /* TODO does not enter history, thus */
2144 case ':':
2145 case '_':
2146 /* Escape to command mode, but be nice! *//* TODO command expansion
2147 * TODO should be handled here so that we have unique history! */
2148 if(cnt == 0)
2149 goto jearg;
2150 _execute_command(hp, cp, cnt);
2151 if(ok_blook(errexit))
2152 flags |= a_ERREXIT;
2153 else
2154 flags &= ~a_ERREXIT;
2155 if(n_pstate_ex_no != 0 && a_HARDERR())
2156 goto jerr;
2157 if(coap == NULL)
2158 escape = *ok_vlook(escape);
2159 hist &= ~a_HIST_GABBY;
2160 break;
2161 /* case '<': <> 'd' */
2162 case '?':
2163 #ifdef HAVE_UISTRINGS
2164 fputs(_(
2165 "COMMAND ESCAPES (to be placed after a newline) excerpt:\n"
2166 "~. Commit and send message\n"
2167 "~: <command> Execute an internal command\n"
2168 "~< <file> Insert <file> (\"~<! <command>\" inserts shell command)\n"
2169 "~@ [<files>] Edit[/Add] attachments (file[=input-charset[#output-charset]])\n"
2170 "~c <users> Add users to Cc: list (`~b': to Bcc:)\n"
2171 "~d Read in $DEAD (dead.letter)\n"
2172 "~e Edit message via $EDITOR\n"
2173 "~F <msglist> Read in with headers, do not *indentprefix* lines\n"
2174 ), n_stdout);
2175 fputs(_(
2176 "~f <msglist> Like ~F, but honour `ignore' / `retain' configuration\n"
2177 "~H Edit From:, Reply-To: and Sender:\n"
2178 "~h Prompt for Subject:, To:, Cc: and \"blind\" Bcc:\n"
2179 "~i <variable> Insert a value and a newline\n"
2180 "~M <msglist> Read in with headers, *indentprefix* (`~m': `retain' etc.)\n"
2181 "~p Show current message compose buffer\n"
2182 "~r <file> Insert <file> (`~R': likewise, but *indentprefix* lines)\n"
2183 " <file> may also be <- [HERE-DELIMITER]>\n"
2184 ), n_stdout);
2185 fputs(_(
2186 "~s <subject> Set Subject:\n"
2187 "~t <users> Add users to To: list\n"
2188 "~u <msglist> Read in message(s) without headers (`~U': indent lines)\n"
2189 "~v Edit message via $VISUAL\n"
2190 "~w <file> Write message onto file\n"
2191 "~x Abort composition, discard message (`~q': save in $DEAD)\n"
2192 "~| <command> Pipe message through shell filter\n"
2193 ), n_stdout);
2194 #endif /* HAVE_UISTRINGS */
2195 if(cnt != 0)
2196 goto jearg;
2197 n_pstate_err_no = n_ERR_NONE;
2198 n_pstate_ex_no = 0;
2199 break;
2200 case '@':{
2201 struct attachment *aplist;
2203 /* Edit the attachment list */
2204 aplist = hp->h_attach;
2205 hp->h_attach = NULL;
2206 if(cnt != 0)
2207 hp->h_attach = n_attachment_append_list(aplist, cp);
2208 else
2209 hp->h_attach = n_attachment_list_edit(aplist,
2210 n_GO_INPUT_CTX_COMPOSE);
2211 n_pstate_err_no = n_ERR_NONE; /* XXX ~@ does NOT handle $!/$?! */
2212 n_pstate_ex_no = 0; /* XXX */
2213 }break;
2214 case '^':
2215 if(!a_collect_plumbing(cp, hp)){
2216 if(ferror(_coll_fp))
2217 goto jerr;
2218 goto jearg;
2220 n_pstate_err_no = n_ERR_NONE; /* XXX */
2221 n_pstate_ex_no = 0; /* XXX */
2222 hist &= ~a_HIST_GABBY;
2223 break;
2224 /* case '_': <> ':' */
2225 case '|':
2226 /* Pipe message through command. Collect output as new message */
2227 if(cnt == 0)
2228 goto jearg;
2229 rewind(_coll_fp);
2230 if((n_pstate_err_no = a_coll_pipe(cp)) == n_ERR_NONE)
2231 n_pstate_ex_no = 0;
2232 else if(a_HARDERR())
2233 goto jerr;
2234 else
2235 n_pstate_ex_no = 1;
2236 hist &= ~a_HIST_GABBY;
2237 goto jhistcont;
2238 case 'A':
2239 case 'a':
2240 /* Insert the contents of a sign variable */
2241 if(cnt != 0)
2242 goto jearg;
2243 cp = (c == 'a') ? ok_vlook(sign) : ok_vlook(Sign);
2244 goto jIi_putesc;
2245 case 'b':
2246 /* Add stuff to blind carbon copies list TODO join 'c' */
2247 if(cnt == 0)
2248 goto jearg;
2249 else{
2250 struct name *np;
2251 si8_t soe;
2253 soe = 0;
2254 if((np = checkaddrs(lextract(cp, GBCC | GFULL), EACM_NORMAL, &soe)
2255 ) != NULL)
2256 hp->h_bcc = cat(hp->h_bcc, np);
2257 if(soe == 0){
2258 n_pstate_err_no = n_ERR_NONE;
2259 n_pstate_ex_no = 0;
2260 }else{
2261 n_pstate_ex_no = 1;
2262 n_pstate_err_no = (soe < 0) ? n_ERR_PERM : n_ERR_INVAL;
2265 hist &= ~a_HIST_GABBY;
2266 break;
2267 case 'c':
2268 /* Add to the CC list TODO join 'b' */
2269 if(cnt == 0)
2270 goto jearg;
2271 else{
2272 struct name *np;
2273 si8_t soe;
2275 soe = 0;
2276 if((np = checkaddrs(lextract(cp, GCC | GFULL), EACM_NORMAL, &soe)
2277 ) != NULL)
2278 hp->h_cc = cat(hp->h_cc, np);
2279 if(soe == 0){
2280 n_pstate_err_no = n_ERR_NONE;
2281 n_pstate_ex_no = 0;
2282 }else{
2283 n_pstate_ex_no = 1;
2284 n_pstate_err_no = (soe < 0) ? n_ERR_PERM : n_ERR_INVAL;
2287 hist &= ~a_HIST_GABBY;
2288 break;
2289 case 'd':
2290 if(cnt != 0)
2291 goto jearg;
2292 cp = n_getdeadletter();
2293 if(0){
2294 case '<':
2295 case 'R':
2296 case 'r':
2297 /* Invoke a file: Search for the file name, then open it and copy
2298 * the contents to _coll_fp */
2299 if(cnt == 0){
2300 n_err(_("Interpolate what file?\n"));
2301 if(a_HARDERR())
2302 goto jerr;
2303 n_pstate_err_no = n_ERR_NOENT;
2304 n_pstate_ex_no = 1;
2305 break;
2307 if(*cp == '!' && c == '<'){
2308 /* TODO hist. normalization */
2309 if((n_pstate_err_no = a_coll_insert_cmd(_coll_fp, ++cp)
2310 ) != n_ERR_NONE){
2311 if(ferror(_coll_fp))
2312 goto jerr;
2313 if(a_HARDERR())
2314 goto jerr;
2315 n_pstate_ex_no = 1;
2316 break;
2318 goto jhistcont;
2320 /* Note this also expands things like
2321 * !:vput vexpr delim random 0
2322 * !< - $delim */
2323 if((cp = fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO | FEXP_NSHELL)
2324 ) == NULL){
2325 if(a_HARDERR())
2326 goto jerr;
2327 n_pstate_err_no = n_ERR_INVAL;
2328 n_pstate_ex_no = 1;
2329 break;
2332 if(n_is_dir(cp, FAL0)){
2333 n_err(_("%s: is a directory\n"), n_shexp_quote_cp(cp, FAL0));
2334 if(a_HARDERR())
2335 goto jerr;
2336 n_pstate_err_no = n_ERR_ISDIR;
2337 n_pstate_ex_no = 1;
2338 break;
2340 if((n_pstate_err_no = a_coll_include_file(cp, (c == 'R'), TRU1)
2341 ) != n_ERR_NONE){
2342 if(ferror(_coll_fp))
2343 goto jerr;
2344 if(a_HARDERR())
2345 goto jerr;
2346 n_pstate_ex_no = 1;
2347 break;
2349 n_pstate_err_no = n_ERR_NONE; /* XXX */
2350 n_pstate_ex_no = 0; /* XXX */
2351 break;
2352 case 'e':
2353 case 'v':
2354 /* Edit the current message. 'e' -> use EDITOR, 'v' -> use VISUAL */
2355 if(cnt != 0 || coap != NULL)
2356 goto jearg;
2357 rewind(_coll_fp);
2358 if((n_pstate_err_no = a_coll_edit(c, ok_blook(editheaders) ? hp : NULL)
2359 ) == n_ERR_NONE)
2360 n_pstate_ex_no = 0;
2361 else if(ferror(_coll_fp))
2362 goto jerr;
2363 else if(a_HARDERR())
2364 goto jerr;
2365 else
2366 n_pstate_ex_no = 1;
2367 goto jhistcont;
2368 case 'F':
2369 case 'f':
2370 case 'M':
2371 case 'm':
2372 case 'U':
2373 case 'u':
2374 /* Interpolate the named messages, if we are in receiving mail mode.
2375 * Does the standard list processing garbage. If ~f is given, we
2376 * don't shift over */
2377 if((n_pstate_err_no = a_coll_forward(cp, _coll_fp, c)) == n_ERR_NONE)
2378 n_pstate_ex_no = 0;
2379 else if(ferror(_coll_fp))
2380 goto jerr;
2381 else if(a_HARDERR())
2382 goto jerr;
2383 else
2384 n_pstate_ex_no = 1;
2385 break;
2386 case 'H':
2387 /* Grab extra headers */
2388 if(cnt != 0)
2389 goto jearg;
2391 grab_headers(n_GO_INPUT_CTX_COMPOSE, hp, GEXTRA, 0);
2392 while(check_from_and_sender(hp->h_from, hp->h_sender) == NULL);
2393 n_pstate_err_no = n_ERR_NONE; /* XXX */
2394 n_pstate_ex_no = 0; /* XXX */
2395 break;
2396 case 'h':
2397 /* Grab a bunch of headers */
2398 if(cnt != 0)
2399 goto jearg;
2401 grab_headers(n_GO_INPUT_CTX_COMPOSE, hp,
2402 (GTO | GSUBJECT | GCC | GBCC),
2403 (ok_blook(bsdcompat) && ok_blook(bsdorder)));
2404 while(hp->h_to == NULL);
2405 n_pstate_err_no = n_ERR_NONE; /* XXX */
2406 n_pstate_ex_no = 0; /* XXX */
2407 break;
2408 case 'I':
2409 case 'i':
2410 /* Insert a variable into the file */
2411 if(cnt == 0)
2412 goto jearg;
2413 cp = n_var_vlook(cp, TRU1);
2414 jIi_putesc:
2415 if(cp == NULL || *cp == '\0')
2416 break;
2417 if(!a_coll_putesc(cp, (c != 'I'), _coll_fp))
2418 goto jerr;
2419 if((n_psonce & n_PSO_INTERACTIVE) && !(n_pstate & n_PS_ROBOT) &&
2420 !a_coll_putesc(cp, (c != 'I'), n_stdout))
2421 goto jerr;
2422 n_pstate_err_no = n_ERR_NONE; /* XXX */
2423 n_pstate_ex_no = 0; /* XXX */
2424 break;
2425 /* case 'M': <> 'F' */
2426 /* case 'm': <> 'f' */
2427 case 'p':
2428 /* Print current state of the message without altering anything */
2429 if(cnt != 0)
2430 goto jearg;
2431 print_collf(_coll_fp, hp); /* XXX pstate_err_no ++ */
2432 if(ferror(_coll_fp))
2433 goto jerr;
2434 n_pstate_err_no = n_ERR_NONE; /* XXX */
2435 n_pstate_ex_no = 0; /* XXX */
2436 break;
2437 case 'q':
2438 case 'x':
2439 /* Force a quit, act like an interrupt had happened */
2440 if(cnt != 0)
2441 goto jearg;
2442 /* If we are running a splice hook, assume it quits on its own now,
2443 * otherwise we (no true out-of-band IPC to signal this state, XXX sic)
2444 * have to SIGTERM it in order to stop this wild beast */
2445 flags |= a_COAP_NOSIGTERM;
2446 ++_coll_hadintr;
2447 _collint((c == 'x') ? 0 : SIGINT);
2448 exit(n_EXIT_ERR);
2449 /*NOTREACHED*/
2450 /* case 'R': <> 'd' */
2451 /* case 'r': <> 'd' */
2452 case 's':
2453 /* Set the Subject list */
2454 if(cnt == 0)
2455 goto jearg;
2456 /* Subject:; take care for Debian #419840 and strip any \r and \n */
2457 if(n_anyof_cp("\n\r", hp->h_subject = savestr(cp))){
2458 char *xp;
2460 n_err(_("-s: normalizing away invalid ASCII NL / CR bytes\n"));
2461 for(xp = hp->h_subject; *xp != '\0'; ++xp)
2462 if(*xp == '\n' || *xp == '\r')
2463 *xp = ' ';
2464 n_pstate_err_no = n_ERR_INVAL;
2465 n_pstate_ex_no = 1;
2466 }else{
2467 n_pstate_err_no = n_ERR_NONE;
2468 n_pstate_ex_no = 0;
2470 break;
2471 case 't':
2472 /* Add to the To: list TODO join 'b', 'c' */
2473 if(cnt == 0)
2474 goto jearg;
2475 else{
2476 struct name *np;
2477 si8_t soe;
2479 soe = 0;
2480 if((np = checkaddrs(lextract(cp, GTO | GFULL), EACM_NORMAL, &soe)
2481 ) != NULL)
2482 hp->h_to = cat(hp->h_to, np);
2483 if(soe == 0){
2484 n_pstate_err_no = n_ERR_NONE;
2485 n_pstate_ex_no = 0;
2486 }else{
2487 n_pstate_ex_no = 1;
2488 n_pstate_err_no = (soe < 0) ? n_ERR_PERM : n_ERR_INVAL;
2491 hist &= ~a_HIST_GABBY;
2492 break;
2493 /* case 'U': <> 'F' */
2494 /* case 'u': <> 'f' */
2495 /* case 'v': <> 'e' */
2496 case 'w':
2497 /* Write the message on a file */
2498 if(cnt == 0)
2499 goto jearg;
2500 if((cp = fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO)) == NULL){
2501 n_err(_("Write what file!?\n"));
2502 if(a_HARDERR())
2503 goto jerr;
2504 n_pstate_err_no = n_ERR_INVAL;
2505 n_pstate_ex_no = 1;
2506 break;
2508 rewind(_coll_fp);
2509 if((n_pstate_err_no = a_coll_write(cp, _coll_fp, 1)) == n_ERR_NONE)
2510 n_pstate_ex_no = 0;
2511 else if(ferror(_coll_fp))
2512 goto jerr;
2513 else if(a_HARDERR())
2514 goto jerr;
2515 else
2516 n_pstate_ex_no = 1;
2517 break;
2518 /* case 'x': <> 'q' */
2521 /* Finally place an entry in history as applicable */
2522 if(0){
2523 jhistcont:
2524 c = '\1';
2525 }else
2526 c = '\0';
2527 if(hist & a_HIST_ADD){
2528 /* Do not add *escape* to the history in order to allow history search
2529 * to be handled generically in the MLE regardless of actual *escape*
2530 * settings etc. */
2531 n_tty_addhist(&n_string_cp(sp)[1], (n_GO_INPUT_CTX_COMPOSE |
2532 (hist & a_HIST_GABBY ? n_GO_INPUT_HIST_GABBY : n_GO_INPUT_NONE)));
2534 if(c != '\0')
2535 goto jcont;
2538 jout:
2539 /* Do we have *on-compose-splice-shell*, or *on-compose-splice*?
2540 * TODO Usual f...ed up state of signals and terminal etc. */
2541 if(coap == NULL && (cp = ok_vlook(on_compose_splice_shell)) != NULL) Jocs:{
2542 union {int (*ptf)(void); char const *sh;} u;
2543 char const *cmd;
2545 /* Reset *escape* and more to their defaults. On change update manual! */
2546 if(ifs_saved == NULL)
2547 ifs_saved = savestr(ok_vlook(ifs));
2548 escape = n_ESCAPE[0];
2549 ok_vclear(ifs);
2551 if(coapm != NULL){
2552 /* XXX Due Popen() fflush(NULL) in PTF mode, ensure nothing to flush */
2553 /*if(!n_real_seek(_coll_fp, 0, SEEK_END))
2554 * goto jerr;*/
2555 u.ptf = &a_coll_ocs__mac;
2556 cmd = (char*)-1;
2557 a_coll_ocs__macname = cp = coapm;
2558 }else{
2559 u.sh = ok_vlook(SHELL);
2560 cmd = cp;
2563 i = strlen(cp) +1;
2564 coap = n_lofi_alloc(n_VSTRUCT_SIZEOF(struct a_coll_ocs_arg, coa_cmd
2565 ) + i);
2566 coap->coa_pipe[0] = coap->coa_pipe[1] = -1;
2567 coap->coa_stdin = coap->coa_stdout = NULL;
2568 coap->coa_senderr = checkaddr_err;
2569 memcpy(coap->coa_cmd, cp, i);
2571 hold_all_sigs();
2572 coap->coa_opipe = safe_signal(SIGPIPE, SIG_IGN);
2573 coap->coa_oint = safe_signal(SIGINT, SIG_IGN);
2574 rele_all_sigs();
2576 if(pipe_cloexec(coap->coa_pipe) != -1 &&
2577 (coap->coa_stdin = Fdopen(coap->coa_pipe[0], "r", FAL0)) != NULL &&
2578 (coap->coa_stdout = Popen(cmd, "W", u.sh, NULL, coap->coa_pipe[1])
2579 ) != NULL){
2580 close(coap->coa_pipe[1]);
2581 coap->coa_pipe[1] = -1;
2583 temporary_compose_mode_hook_call(NULL, NULL, NULL);
2584 n_go_splice_hack(coap->coa_cmd, coap->coa_stdin, coap->coa_stdout,
2585 (n_psonce & ~(n_PSO_INTERACTIVE | n_PSO_TTYIN | n_PSO_TTYOUT)),
2586 &a_coll_ocs__finalize, &coap);
2587 /* Hook version protocol for ~^: update manual upon change! */
2588 fputs(a_COLL_PLUMBING_VERSION "\n", coap->coa_stdout);
2589 #undef a_COLL_PLUMBING_VERSION
2590 goto jcont;
2593 c = n_err_no;
2594 a_coll_ocs__finalize(coap);
2595 n_perr(_("Cannot invoke *on-compose-splice(-shell)?*"), c);
2596 goto jerr;
2598 if(*checkaddr_err != 0){
2599 *checkaddr_err = 0;
2600 goto jerr;
2602 if(coapm == NULL && (coapm = ok_vlook(on_compose_splice)) != NULL)
2603 goto Jocs;
2604 if(coap != NULL){
2605 ok_vset(ifs, ifs_saved);
2606 ifs_saved = NULL;
2609 /* Final chance to edit headers, if not already above */
2610 if((n_psonce & n_PSO_INTERACTIVE) && !(n_pstate & n_PS_ROBOT)){
2611 if(ok_blook(bsdcompat) || ok_blook(askatend)){
2612 enum gfield gf;
2614 gf = GNONE;
2615 if(ok_blook(askcc))
2616 gf |= GCC;
2617 if(ok_blook(askbcc))
2618 gf |= GBCC;
2619 if(gf != 0)
2620 grab_headers(n_GO_INPUT_CTX_COMPOSE, hp, gf, 1);
2622 if(ok_blook(askattach))
2623 hp->h_attach = n_attachment_list_edit(hp->h_attach,
2624 n_GO_INPUT_CTX_COMPOSE);
2627 /* Execute compose-leave */
2628 if((cp = ok_vlook(on_compose_leave)) != NULL){
2629 setup_from_and_sender(hp);
2630 temporary_compose_mode_hook_call(cp, &n_temporary_compose_hook_varset,
2631 hp);
2634 /* Add automatic receivers */
2635 if ((cp = ok_vlook(autocc)) != NULL && *cp != '\0')
2636 hp->h_cc = cat(hp->h_cc, checkaddrs(lextract(cp, GCC |
2637 (ok_blook(fullnames) ? GFULL | GSKIN : GSKIN)),
2638 EACM_NORMAL, checkaddr_err));
2639 if ((cp = ok_vlook(autobcc)) != NULL && *cp != '\0')
2640 hp->h_bcc = cat(hp->h_bcc, checkaddrs(lextract(cp, GBCC |
2641 (ok_blook(fullnames) ? GFULL | GSKIN : GSKIN)),
2642 EACM_NORMAL, checkaddr_err));
2643 if (*checkaddr_err != 0)
2644 goto jerr;
2646 if((n_psonce & n_PSO_INTERACTIVE) && !(n_pstate & n_PS_ROBOT) &&
2647 ok_blook(asksend)){
2648 bool_t b;
2650 ifs_saved = coapm = NULL;
2651 coap = NULL;
2653 fprintf(n_stdout, _("-------\nEnvelope contains:\n")); /* xxx SEARCH112 */
2654 puthead(TRU1, hp, n_stdout,
2655 GIDENT | GREF_IRT | GSUBJECT | GTO | GCC | GBCC | GCOMMA,
2656 SEND_TODISP, CONV_NONE, NULL, NULL);
2658 jreasksend:
2659 if(n_go_input(n_GO_INPUT_CTX_COMPOSE | n_GO_INPUT_NL_ESC,
2660 _("Send this message [yes/no, empty: recompose]? "),
2661 &linebuf, &linesize, NULL, NULL) < 0){
2662 if(!n_go_input_is_eof())
2663 goto jerr;
2664 cp = n_1;
2667 if((b = n_boolify(linebuf, UIZ_MAX, TRUM1)) < FAL0)
2668 goto jreasksend;
2669 if(b == TRU2)
2670 goto jcont;
2671 if(!b)
2672 goto jerr;
2675 /* TODO Cannot do since it may require turning this into a multipart one */
2676 if(n_poption & n_PO_Mm_FLAG)
2677 goto jskiptails;
2679 /* Place signature? */
2680 if((cp = ok_vlook(signature)) != NULL && *cp != '\0'){ /* TODO OBSOLETE */
2681 char const *cpq;
2683 n_OBSOLETE(_("please use *on-compose-{leave,splice}* and/or "
2684 "*message-inject-tail*, not *signature*"));
2686 if((cpq = fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO)) == NULL){
2687 n_err(_("*signature* expands to invalid file: %s\n"),
2688 n_shexp_quote_cp(cp, FAL0));
2689 goto jerr;
2691 cpq = n_shexp_quote_cp(cp = cpq, FAL0);
2693 if((sigfp = Fopen(cp, "r")) == NULL){
2694 n_err(_("Can't open *signature* %s: %s\n"),
2695 cpq, n_err_to_doc(n_err_no));
2696 goto jerr;
2699 if(linebuf == NULL)
2700 linebuf = n_alloc(linesize = LINESIZE);
2701 c = '\0';
2702 while((i = fread(linebuf, sizeof *linebuf, linesize, n_UNVOLATILE(sigfp)))
2703 > 0){
2704 c = linebuf[i - 1];
2705 if(i != fwrite(linebuf, sizeof *linebuf, i, _coll_fp))
2706 goto jerr;
2709 /* C99 */{
2710 FILE *x = n_UNVOLATILE(sigfp);
2711 int e = n_err_no, ise = ferror(x);
2713 sigfp = NULL;
2714 Fclose(x);
2716 if(ise){
2717 n_err(_("Errors while reading *signature* %s: %s\n"),
2718 cpq, n_err_to_doc(e));
2719 goto jerr;
2723 if(c != '\0' && c != '\n')
2724 putc('\n', _coll_fp);
2727 { char const *cp_obsolete = ok_vlook(NAIL_TAIL);
2729 if(cp_obsolete != NULL)
2730 n_OBSOLETE(_("please use *message-inject-tail*, not *NAIL_TAIL*"));
2732 if((cp = ok_vlook(message_inject_tail)) != NULL ||
2733 (cp = cp_obsolete) != NULL){
2734 if(!a_coll_putesc(cp, TRU1, _coll_fp))
2735 goto jerr;
2736 if((n_psonce & n_PSO_INTERACTIVE) && !(n_pstate & n_PS_ROBOT) &&
2737 !a_coll_putesc(cp, TRU1, n_stdout))
2738 goto jerr;
2742 jskiptails:
2743 if(fflush(_coll_fp))
2744 goto jerr;
2745 rewind(_coll_fp);
2747 jleave:
2748 if (linebuf != NULL)
2749 n_free(linebuf);
2750 sigprocmask(SIG_BLOCK, &nset, NULL);
2751 n_pstate &= ~n_PS_COMPOSE_MODE;
2752 safe_signal(SIGINT, _coll_saveint);
2753 safe_signal(SIGHUP, _coll_savehup);
2754 sigprocmask(SIG_SETMASK, &oset, NULL);
2755 NYD_LEAVE;
2756 return _coll_fp;
2758 jerr:
2759 hold_all_sigs();
2761 if(coap != NULL && coap != (struct a_coll_ocs_arg*)-1){
2762 if(!(flags & a_COAP_NOSIGTERM))
2763 n_psignal(coap->coa_stdout, SIGTERM);
2764 n_go_splice_hack_remove_after_jump();
2765 coap = NULL;
2767 if(ifs_saved != NULL){
2768 ok_vset(ifs, ifs_saved);
2769 ifs_saved = NULL;
2771 if(sigfp != NULL){
2772 Fclose(n_UNVOLATILE(sigfp));
2773 sigfp = NULL;
2775 if(_coll_fp != NULL){
2776 Fclose(_coll_fp);
2777 _coll_fp = NULL;
2780 rele_all_sigs();
2782 assert(checkaddr_err != NULL);
2783 /* TODO We don't save in $DEAD upon error because msg not readily composed?
2784 * TODO But this no good, it should go ZOMBIE / DRAFT / POSTPONED or what! */
2785 if(*checkaddr_err != 0){
2786 if(*checkaddr_err == 111)
2787 n_err(_("Compose mode splice hook failure\n"));
2788 else
2789 n_err(_("Some addressees were classified as \"hard error\"\n"));
2790 }else if(_coll_hadintr == 0){
2791 *checkaddr_err = TRU1; /* TODO ugly: "sendout_error" now.. */
2792 n_err(_("Failed to prepare composed message\n"));
2794 goto jleave;
2796 #undef a_HARDERR
2799 /* s-it-mode */