main.c: support $COLUMNS/$LINES in non-interactive batch mode
[s-mailx.git] / collect.c
blob39c22fd0bdafe9c140063b71daeb1d67c984c1fd
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 static int putesc(char const *s, FILE *stream); /* TODO wysh set! */
118 /* *on-compose-splice* driver and *on-compose-splice(-shell)?* finalizer */
119 static int a_coll_ocs__mac(void);
120 static void a_coll_ocs__finalize(void *vp);
122 static void
123 _execute_command(struct header *hp, char const *linebuf, size_t linesize){
124 /* The problem arises if there are rfc822 message attachments and the
125 * user uses `~:' to change the current file. TODO Unfortunately we
126 * TODO cannot simply keep a pointer to, or increment a reference count
127 * TODO of the current `file' (mailbox that is) object, because the
128 * TODO codebase doesn't deal with that at all; so, until some far
129 * TODO later time, copy the name of the path, and warn the user if it
130 * TODO changed; we COULD use the AC_TMPFILE attachment type, i.e.,
131 * TODO copy the message attachments over to temporary files, but that
132 * TODO would require more changes so that the user still can recognize
133 * TODO in `~@' etc. that its a rfc822 message attachment; see below */
134 struct n_sigman sm;
135 struct attachment *ap;
136 char * volatile mnbuf;
137 NYD_ENTER;
139 n_UNUSED(linesize);
140 mnbuf = NULL;
142 n_SIGMAN_ENTER_SWITCH(&sm, n_SIGMAN_HUP | n_SIGMAN_INT | n_SIGMAN_QUIT){
143 case 0:
144 break;
145 default:
146 n_pstate_err_no = n_ERR_INTR;
147 n_pstate_ex_no = 1;
148 goto jleave;
151 /* If the above todo is worked, remove or outsource to attachment.c! */
152 if(hp != NULL && (ap = hp->h_attach) != NULL) do
153 if(ap->a_msgno){
154 mnbuf = sstrdup(mailname);
155 break;
157 while((ap = ap->a_flink) != NULL);
159 n_go_command(n_GO_INPUT_CTX_COMPOSE, linebuf);
161 n_sigman_cleanup_ping(&sm);
162 jleave:
163 if(mnbuf != NULL){
164 if(strcmp(mnbuf, mailname))
165 n_err(_("Mailbox changed: it is likely that existing "
166 "rfc822 attachments became invalid!\n"));
167 free(mnbuf);
169 NYD_LEAVE;
170 n_sigman_leave(&sm, n_SIGMAN_VIPSIGS_NTTYOUT);
173 static si32_t
174 a_coll_include_file(char const *name, bool_t indent, bool_t writestat){
175 FILE *fbuf;
176 char const *heredb, *indb;
177 size_t linesize, heredl, indl, cnt, linelen;
178 char *linebuf;
179 si64_t lc, cc;
180 si32_t rv;
181 NYD_ENTER;
183 rv = n_ERR_NONE;
184 lc = cc = 0;
185 linebuf = NULL; /* TODO line pool */
186 linesize = 0;
187 heredb = NULL;
188 heredl = 0;
190 /* The -M case is special */
191 if(name == (char*)-1){
192 fbuf = n_stdin;
193 name = "-";
194 }else if(name[0] == '-' &&
195 (name[1] == '\0' || blankspacechar(name[1]))){
196 fbuf = n_stdin;
197 if(name[1] == '\0'){
198 if(!(n_psonce & n_PSO_INTERACTIVE)){
199 n_err(_("~< -: HERE-delimiter required in non-interactive mode\n"));
200 rv = n_ERR_INVAL;
201 goto jleave;
203 }else{
204 for(heredb = &name[2]; *heredb != '\0' && blankspacechar(*heredb);
205 ++heredb)
207 if((heredl = strlen(heredb)) == 0){
208 jdelim_empty:
209 n_err(_("~< - HERE-delimiter: delimiter must not be empty\n"));
210 rv = n_ERR_INVAL;
211 goto jleave;
214 if(*heredb == '\''){
215 for(indb = ++heredb; *indb != '\0' && *indb != '\''; ++indb)
217 if(*indb == '\0'){
218 n_err(_("~< - HERE-delimiter: missing trailing quote\n"));
219 rv = n_ERR_INVAL;
220 goto jleave;
221 }else if(indb[1] != '\0'){
222 n_err(_("~< - HERE-delimiter: trailing characters after "
223 "quote\n"));
224 rv = n_ERR_INVAL;
225 goto jleave;
227 if((heredl = PTR2SIZE(indb - heredb)) == 0)
228 goto jdelim_empty;
229 heredb = savestrbuf(heredb, heredl);
232 name = "-";
233 }else if((fbuf = Fopen(name, "r")) == NULL){
234 n_perr(name, rv = n_err_no);
235 goto jleave;
238 if(!indent)
239 indl = 0;
240 else{
241 if((indb = ok_vlook(indentprefix)) == NULL)
242 indb = INDENT_DEFAULT;
243 indl = strlen(indb);
246 if(fbuf != n_stdin)
247 cnt = fsize(fbuf);
248 while(fgetline(&linebuf, &linesize, (fbuf == n_stdin ? NULL : &cnt),
249 &linelen, fbuf, 0) != NULL){
250 if(heredl > 0 && heredl == linelen - 1 &&
251 !memcmp(heredb, linebuf, heredl)){
252 heredb = NULL;
253 break;
256 if(indl > 0){
257 if(fwrite(indb, sizeof *indb, indl, _coll_fp) != indl){
258 rv = n_err_no;
259 goto jleave;
261 cc += indl;
264 if(fwrite(linebuf, sizeof *linebuf, linelen, _coll_fp) != linelen){
265 rv = n_err_no;
266 goto jleave;
268 cc += linelen;
269 ++lc;
271 if(fflush(_coll_fp)){
272 rv = n_err_no;
273 goto jleave;
276 if(heredb != NULL)
277 rv = n_ERR_NOTOBACCO;
278 jleave:
279 if(linebuf != NULL)
280 free(linebuf);
281 if(fbuf != NULL){
282 if(fbuf != n_stdin)
283 Fclose(fbuf);
284 else if(heredl > 0)
285 clearerr(n_stdin);
288 if(writestat)
289 fprintf(n_stdout, "%s%s %" PRId64 "/%" PRId64 "\n",
290 n_shexp_quote_cp(name, FAL0), (rv ? " " n_ERROR : n_empty), lc, cc);
291 NYD_LEAVE;
292 return rv;
295 static si32_t
296 a_coll_insert_cmd(FILE *fp, char const *cmd){
297 FILE *ibuf;
298 si64_t lc, cc;
299 si32_t rv;
300 NYD_ENTER;
302 rv = n_ERR_NONE;
303 lc = cc = 0;
305 if((ibuf = Popen(cmd, "r", ok_vlook(SHELL), NULL, 0)) != NULL){
306 int c;
308 while((c = getc(ibuf)) != EOF){ /* XXX bytewise, yuck! */
309 if(putc(c, fp) == EOF){
310 rv = n_err_no;
311 break;
313 ++cc;
314 if(c == '\n')
315 ++lc;
317 if(!feof(ibuf) || ferror(ibuf)){
318 if(rv == n_ERR_NONE)
319 rv = n_ERR_IO;
321 if(!Pclose(ibuf, TRU1)){
322 if(rv == n_ERR_NONE)
323 rv = n_ERR_IO;
325 }else
326 n_perr(cmd, rv = n_err_no);
328 fprintf(n_stdout, "CMD%s %" PRId64 "/%" PRId64 "\n",
329 (rv == n_ERR_NONE ? n_empty : " " n_ERROR), lc, cc);
330 NYD_LEAVE;
331 return rv;
334 static void
335 print_collf(FILE *cf, struct header *hp)
337 char *lbuf;
338 FILE *obuf;
339 size_t cnt, linesize, linelen;
340 NYD_ENTER;
342 fflush_rewind(cf);
343 cnt = (size_t)fsize(cf);
345 if((obuf = Ftmp(NULL, "collfp", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL){
346 n_perr(_("Can't create temporary file for `~p' command"), 0);
347 goto jleave;
350 hold_all_sigs();
352 fprintf(obuf, _("-------\nMessage contains:\n"));
353 puthead(TRU1, hp, obuf,
354 (GIDENT | GTO | GSUBJECT | GCC | GBCC | GNL | GFILES | GCOMMA),
355 SEND_TODISP, CONV_NONE, NULL, NULL);
357 lbuf = NULL;
358 linesize = 0;
359 while(fgetline(&lbuf, &linesize, &cnt, &linelen, cf, 1))
360 prout(lbuf, linelen, obuf);
361 if(lbuf != NULL)
362 free(lbuf);
364 if(hp->h_attach != NULL){
365 fputs(_("-------\nAttachments:\n"), obuf);
366 n_attachment_list_print(hp->h_attach, obuf);
369 rele_all_sigs();
371 page_or_print(obuf, 0);
373 Fclose(obuf);
374 jleave:
375 NYD_LEAVE;
378 static si32_t
379 a_coll_write(char const *name, FILE *fp, int f)
381 FILE *of;
382 int c;
383 si64_t lc, cc;
384 si32_t rv;
385 NYD_ENTER;
387 rv = n_ERR_NONE;
389 if(f) {
390 fprintf(n_stdout, "%s ", n_shexp_quote_cp(name, FAL0));
391 fflush(n_stdout);
394 if ((of = Fopen(name, "a")) == NULL) {
395 n_perr(name, rv = n_err_no);
396 goto jerr;
399 lc = cc = 0;
400 while ((c = getc(fp)) != EOF) {
401 ++cc;
402 if (c == '\n')
403 ++lc;
404 if (putc(c, of) == EOF) {
405 n_perr(name, rv = n_err_no);
406 goto jerr;
409 fprintf(n_stdout, _("%" PRId64 "/%" PRId64 "\n"), lc, cc);
411 jleave:
412 if(of != NULL)
413 Fclose(of);
414 fflush(n_stdout);
415 NYD_LEAVE;
416 return rv;
417 jerr:
418 putc('-', n_stdout);
419 putc('\n', n_stdout);
420 goto jleave;
423 static bool_t
424 a_coll_message_inject_head(FILE *fp){
425 bool_t rv;
426 char const *cp, *cp_obsolete;
427 NYD2_ENTER;
429 cp_obsolete = ok_vlook(NAIL_HEAD);
430 if(cp_obsolete != NULL)
431 n_OBSOLETE(_("please use *message-inject-head*, not *NAIL_HEAD*"));
433 if(((cp = ok_vlook(message_inject_head)) != NULL ||
434 (cp = cp_obsolete) != NULL) && putesc(cp, fp) < 0)
435 rv = FAL0;
436 else
437 rv = TRU1;
438 NYD2_LEAVE;
439 return rv;
442 static bool_t
443 a_coll_makeheader(FILE *fp, struct header *hp, si8_t *checkaddr_err,
444 bool_t do_delayed_due_t)
446 FILE *nf;
447 int c;
448 bool_t rv;
449 NYD_ENTER;
451 rv = FAL0;
453 if ((nf = Ftmp(NULL, "colhead", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==NULL) {
454 n_perr(_("temporary mail edit file"), 0);
455 goto jleave;
458 extract_header(fp, hp, checkaddr_err);
459 if (checkaddr_err != NULL && *checkaddr_err != 0)
460 goto jleave;
462 /* In template mode some things have been delayed until the template has
463 * been read */
464 if(do_delayed_due_t){
465 char const *cp;
467 if((cp = ok_vlook(on_compose_enter)) != NULL){
468 setup_from_and_sender(hp);
469 temporary_compose_mode_hook_call(cp, &n_temporary_compose_hook_varset,
470 hp);
473 if(!a_coll_message_inject_head(nf))
474 goto jleave;
477 while ((c = getc(fp)) != EOF) /* XXX bytewise, yuck! */
478 putc(c, nf);
480 if (fp != _coll_fp)
481 Fclose(_coll_fp);
482 Fclose(fp);
483 _coll_fp = nf;
484 nf = NULL;
486 if (check_from_and_sender(hp->h_from, hp->h_sender) == NULL)
487 goto jleave;
488 rv = TRU1;
489 jleave:
490 if(nf != NULL)
491 Fclose(nf);
492 NYD_LEAVE;
493 return rv;
496 static si32_t
497 a_coll_edit(int c, struct header *hp) /* TODO error(return) weird */
499 struct n_sigman sm;
500 FILE *nf;
501 sighandler_type volatile sigint;
502 bool_t saved;
503 si32_t volatile rv;
504 NYD_ENTER;
506 n_UNINIT(sigint, SIG_ERR);
507 rv = n_ERR_NONE;
509 if(!(saved = ok_blook(add_file_recipients)))
510 ok_bset(add_file_recipients);
512 n_SIGMAN_ENTER_SWITCH(&sm, n_SIGMAN_ALL){
513 case 0:
514 sigint = safe_signal(SIGINT, SIG_IGN);
515 break;
516 default:
517 rv = n_ERR_INTR;
518 goto jleave;
521 nf = run_editor(_coll_fp, (off_t)-1, c, FAL0, hp, NULL, SEND_MBOX, sigint);
522 if (nf != NULL) {
523 if (hp) {
524 if(!a_coll_makeheader(nf, hp, NULL, FAL0))
525 rv = n_ERR_INVAL;
526 } else {
527 fseek(nf, 0L, SEEK_END);
528 Fclose(_coll_fp);
529 _coll_fp = nf;
531 } else
532 rv = n_ERR_CHILD;
534 n_sigman_cleanup_ping(&sm);
535 jleave:
536 if(!saved)
537 ok_bclear(add_file_recipients);
538 safe_signal(SIGINT, sigint);
539 NYD_LEAVE;
540 n_sigman_leave(&sm, n_SIGMAN_VIPSIGS_NTTYOUT);
541 return rv;
544 static si32_t
545 a_coll_pipe(char const *cmd)
547 int ws;
548 FILE *nf;
549 sighandler_type sigint;
550 si32_t rv;
551 NYD_ENTER;
553 rv = n_ERR_NONE;
554 sigint = safe_signal(SIGINT, SIG_IGN);
556 if ((nf = Ftmp(NULL, "colpipe", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==NULL) {
557 n_perr(_("temporary mail edit file"), rv = n_err_no);
558 goto jout;
561 /* stdin = current message. stdout = new message */
562 fflush(_coll_fp);
563 if (n_child_run(ok_vlook(SHELL), 0, fileno(_coll_fp), fileno(nf), "-c",
564 cmd, NULL, NULL, &ws) < 0 || WEXITSTATUS(ws) != 0) {
565 Fclose(nf);
566 rv = n_ERR_CHILD;
567 goto jout;
570 if (fsize(nf) == 0) {
571 n_err(_("No bytes from %s !?\n"), n_shexp_quote_cp(cmd, FAL0));
572 Fclose(nf);
573 rv = n_ERR_NODATA;
574 goto jout;
577 /* Take new files */
578 fseek(nf, 0L, SEEK_END);
579 Fclose(_coll_fp);
580 _coll_fp = nf;
581 jout:
582 safe_signal(SIGINT, sigint);
583 NYD_LEAVE;
584 return rv;
587 static si32_t
588 a_coll_forward(char const *ms, FILE *fp, int f)
590 int *msgvec, rv = 0;
591 struct n_ignore const *itp;
592 char const *tabst;
593 enum sendaction action;
594 NYD_ENTER;
596 msgvec = salloc((size_t)(msgCount + 1) * sizeof *msgvec);
597 if (getmsglist(ms, msgvec, 0) < 0) {
598 rv = n_ERR_NOENT; /* XXX not really, should be handled there! */
599 goto jleave;
601 if (*msgvec == 0) {
602 *msgvec = first(0, MMNORM);
603 if (*msgvec == 0) {
604 n_err(_("No appropriate messages\n"));
605 rv = n_ERR_NOENT;
606 goto jleave;
608 msgvec[1] = 0;
611 if (f == 'f' || f == 'F' || f == 'u')
612 tabst = NULL;
613 else if ((tabst = ok_vlook(indentprefix)) == NULL)
614 tabst = INDENT_DEFAULT;
615 if (f == 'u' || f == 'U')
616 itp = n_IGNORE_ALL;
617 else
618 itp = upperchar(f) ? NULL : n_IGNORE_TYPE;
619 action = (upperchar(f) && f != 'U') ? SEND_QUOTE_ALL : SEND_QUOTE;
621 fprintf(n_stdout, _("Interpolating:"));
622 srelax_hold();
623 for (; *msgvec != 0; ++msgvec) {
624 struct message *mp = message + *msgvec - 1;
626 touch(mp);
627 fprintf(n_stdout, " %d", *msgvec);
628 fflush(n_stdout);
629 if (sendmp(mp, fp, itp, tabst, action, NULL) < 0) {
630 n_perr(_("temporary mail file"), 0);
631 rv = n_ERR_IO;
632 break;
634 srelax();
636 srelax_rele();
637 fprintf(n_stdout, "\n");
638 jleave:
639 NYD_LEAVE;
640 return rv;
643 static bool_t
644 a_collect_plumbing(char const *ms, struct header *hp){
645 /* TODO _collect_plumbing: instead of fields the basic headers should
646 * TODO be in an array and have IDs, like in termcap etc., so then this
647 * TODO could be simplified as table-walks. Also true for arg-checks! */
648 bool_t rv;
649 char const *cp, *cmd[4];
650 NYD2_ENTER;
652 /* Protcol version for *on-compose-splice** -- update manual on change! */
653 #define a_COLL_PLUMBING_VERSION "0 0 1"
654 cp = ms;
656 /* C99 */{
657 size_t i;
659 for(i = 0; i < n_NELEM(cmd); ++i){ /* TODO trim+strlist_split(_ifs?)() */
660 while(blankchar(*cp))
661 ++cp;
662 if(*cp == '\0')
663 cmd[i] = NULL;
664 else{
665 if(i < n_NELEM(cmd) - 1)
666 for(cmd[i] = cp++; *cp != '\0' && !blankchar(*cp); ++cp)
668 else{
669 /* Last slot takes all the rest of the line, less trailing WS */
670 for(cmd[i] = cp++; *cp != '\0'; ++cp)
672 while(blankchar(cp[-1]))
673 --cp;
675 cmd[i] = savestrbuf(cmd[i], PTR2SIZE(cp - cmd[i]));
680 if(n_UNLIKELY(cmd[0] == NULL))
681 goto jecmd;
682 if(is_asccaseprefix(cmd[0], "header"))
683 rv = a_collect__plumb_header(cp, hp, cmd);
684 else if(is_asccaseprefix(cmd[0], "attachment"))
685 rv = a_collect__plumb_attach(cp, hp, cmd);
686 else{
687 jecmd:
688 fputs("500\n", n_stdout);
689 rv = FAL0;
691 fflush(n_stdout);
693 NYD2_LEAVE;
694 return rv;
697 static bool_t
698 a_collect__plumb_header(char const *cp, struct header *hp,
699 char const *cmd[4]){
700 uiz_t i;
701 struct n_header_field *hfp;
702 struct name *np, **npp;
703 NYD2_ENTER;
705 if(cmd[1] == NULL)
706 goto jdefault;
708 if(is_asccaseprefix(cmd[1], "insert")){ /* TODO LOGIC BELONGS head.c
709 * TODO That is: Header::factory(string) -> object (blahblah).
710 * TODO I.e., as long as we don't have regular RFC compliant parsers
711 * TODO which differentiate in between structured and unstructured
712 * TODO header fields etc., a little workaround */
713 struct name *xnp;
714 si8_t aerr;
715 enum expand_addr_check_mode eacm;
716 enum gfield ntype;
717 bool_t mult_ok;
719 if(cmd[2] == NULL || cmd[3] == NULL)
720 goto jecmd;
722 /* Strip [\r\n] which would render a body invalid XXX all controls? */
723 /* C99 */{
724 char *xp, c;
726 cmd[3] = xp = savestr(cmd[3]);
727 for(; (c = *xp) != '\0'; ++xp)
728 if(c == '\n' || c == '\r')
729 *xp = ' ';
732 if(!asccasecmp(cmd[2], cp = "Subject")){
733 if(cmd[3][0] != '\0'){
734 if(hp->h_subject != NULL)
735 hp->h_subject = savecatsep(hp->h_subject, ' ', cmd[3]);
736 else
737 hp->h_subject = n_UNCONST(cmd[3]);
738 fprintf(n_stdout, "210 %s 1\n", cp);
739 goto jleave;
740 }else
741 goto j501cp;
744 mult_ok = TRU1;
745 ntype = GEXTRA | GFULL | GFULLEXTRA;
746 eacm = EACM_STRICT;
748 if(!asccasecmp(cmd[2], cp = "From")){
749 npp = &hp->h_from;
750 jins:
751 aerr = 0;
752 if((np = lextract(cmd[3], ntype)) == NULL)
753 goto j501cp;
755 if((np = checkaddrs(np, eacm, &aerr), aerr != 0)){
756 fprintf(n_stdout, "505 %s\n", cp);
757 goto jleave;
760 /* Go to the end of the list, track whether it contains any
761 * non-deleted entries */
762 i = 0;
763 if((xnp = *npp) != NULL)
764 for(;; xnp = xnp->n_flink){
765 if(!(xnp->n_type & GDEL))
766 ++i;
767 if(xnp->n_flink == NULL)
768 break;
771 if(!mult_ok && (i != 0 || np->n_flink != NULL))
772 fprintf(n_stdout, "506 %s\n", cp);
773 else{
774 if(xnp == NULL)
775 *npp = np;
776 else
777 xnp->n_flink = np;
778 np->n_blink = xnp;
779 fprintf(n_stdout, "210 %s %" PRIuZ "\n", cp, ++i);
781 goto jleave;
783 if(!asccasecmp(cmd[2], cp = "Sender")){
784 mult_ok = FAL0;
785 npp = &hp->h_sender;
786 goto jins;
788 if(!asccasecmp(cmd[2], cp = "To")){
789 npp = &hp->h_to;
790 ntype = GTO | GFULL;
791 eacm = EACM_NORMAL | EAF_NAME;
792 goto jins;
794 if(!asccasecmp(cmd[2], cp = "Cc")){
795 npp = &hp->h_cc;
796 ntype = GCC | GFULL;
797 eacm = EACM_NORMAL | EAF_NAME;
798 goto jins;
800 if(!asccasecmp(cmd[2], cp = "Bcc")){
801 npp = &hp->h_bcc;
802 ntype = GBCC | GFULL;
803 eacm = EACM_NORMAL | EAF_NAME;
804 goto jins;
806 if(!asccasecmp(cmd[2], cp = "Reply-To")){
807 npp = &hp->h_replyto;
808 eacm = EACM_NONAME;
809 goto jins;
811 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
812 npp = &hp->h_mft;
813 eacm = EACM_NONAME;
814 goto jins;
816 if(!asccasecmp(cmd[2], cp = "Message-ID")){
817 mult_ok = FAL0;
818 npp = &hp->h_message_id;
819 ntype = GREF;
820 eacm = EACM_NONAME;
821 goto jins;
823 if(!asccasecmp(cmd[2], cp = "References")){
824 npp = &hp->h_ref;
825 ntype = GREF;
826 eacm = EACM_NONAME;
827 goto jins;
829 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
830 npp = &hp->h_in_reply_to;
831 ntype = GREF;
832 eacm = EACM_NONAME;
833 goto jins;
836 if(!asccasecmp(cmd[2], cp = "Mailx-Command") ||
837 !asccasecmp(cmd[2], cp = "Mailx-Raw-To") ||
838 !asccasecmp(cmd[2], cp = "Mailx-Raw-Cc") ||
839 !asccasecmp(cmd[2], cp = "Mailx-Raw-Bcc") ||
840 !asccasecmp(cmd[2], cp = "Mailx-Orig-From") ||
841 !asccasecmp(cmd[2], cp = "Mailx-Orig-To") ||
842 !asccasecmp(cmd[2], cp = "Mailx-Orig-Cc") ||
843 !asccasecmp(cmd[2], cp = "Mailx-Orig-Bcc")){
844 fprintf(n_stdout, "505 %s\n", cp);
845 goto jleave;
848 /* Free-form header fields (note j501cp may print non-normalized name) */
849 /* C99 */{
850 size_t nl, bl;
851 struct n_header_field **hfpp;
853 for(cp = cmd[2]; *cp != '\0'; ++cp)
854 if(!fieldnamechar(*cp)){
855 cp = cmd[2];
856 goto j501cp;
859 for(i = 0, hfpp = &hp->h_user_headers; *hfpp != NULL; ++i)
860 hfpp = &(*hfpp)->hf_next;
862 nl = strlen(cp = cmd[2]) +1;
863 bl = strlen(cmd[3]) +1;
864 *hfpp = hfp = salloc(n_VSTRUCT_SIZEOF(struct n_header_field, hf_dat
865 ) + nl + bl);
866 hfp->hf_next = NULL;
867 hfp->hf_nl = --nl;
868 hfp->hf_bl = bl - 1;
869 hfp->hf_dat[nl = 0] = upperconv(*cp);
870 while(*cp++ != '\0')
871 hfp->hf_dat[++nl] = lowerconv(*cp);
872 memcpy(&hfp->hf_dat[++nl], cmd[3], bl);
873 fprintf(n_stdout, "210 %s %" PRIuZ "\n", &hfp->hf_dat[0], ++i);
875 goto jleave;
878 if(is_asccaseprefix(cmd[1], "list")){
879 jdefault:
880 if(cmd[2] == NULL){
881 fputs("210", n_stdout);
882 if(hp->h_subject != NULL) fputs(" Subject", n_stdout);
883 if(hp->h_from != NULL) fputs(" From", n_stdout);
884 if(hp->h_sender != NULL) fputs(" Sender", n_stdout);
885 if(hp->h_to != NULL) fputs(" To", n_stdout);
886 if(hp->h_cc != NULL) fputs(" Cc", n_stdout);
887 if(hp->h_bcc != NULL) fputs(" Bcc", n_stdout);
888 if(hp->h_replyto != NULL) fputs(" Reply-To", n_stdout);
889 if(hp->h_mft != NULL) fputs(" Mail-Followup-To", n_stdout);
890 if(hp->h_message_id != NULL) fputs(" Message-ID", n_stdout);
891 if(hp->h_ref != NULL) fputs(" References", n_stdout);
892 if(hp->h_in_reply_to != NULL) fputs(" In-Reply-To", n_stdout);
893 if(hp->h_mailx_command != NULL) fputs(" Mailx-Command", n_stdout);
894 if(hp->h_mailx_raw_to != NULL) fputs(" Mailx-Raw-To", n_stdout);
895 if(hp->h_mailx_raw_cc != NULL) fputs(" Mailx-Raw-Cc", n_stdout);
896 if(hp->h_mailx_raw_bcc != NULL) fputs(" Mailx-Raw-Bcc", n_stdout);
897 if(hp->h_mailx_orig_from != NULL) fputs(" Mailx-Orig-From", n_stdout);
898 if(hp->h_mailx_orig_to != NULL) fputs(" Mailx-Orig-To", n_stdout);
899 if(hp->h_mailx_orig_cc != NULL) fputs(" Mailx-Orig-Cc", n_stdout);
900 if(hp->h_mailx_orig_bcc != NULL) fputs(" Mailx-Orig-Bcc", n_stdout);
902 /* Print only one instance of each free-form header */
903 for(hfp = hp->h_user_headers; hfp != NULL; hfp = hfp->hf_next){
904 struct n_header_field *hfpx;
906 for(hfpx = hp->h_user_headers;; hfpx = hfpx->hf_next)
907 if(hfpx == hfp){
908 putc(' ', n_stdout);
909 fputs(&hfp->hf_dat[0], n_stdout);
910 break;
911 }else if(!strcmp(&hfpx->hf_dat[0], &hfp->hf_dat[0]))
912 break;
914 putc('\n', n_stdout);
915 goto jleave;
918 if(cmd[3] != NULL)
919 goto jecmd;
921 if(!asccasecmp(cmd[2], cp = "Subject")){
922 np = (hp->h_subject != NULL) ? (struct name*)-1 : NULL;
923 goto jlist;
925 if(!asccasecmp(cmd[2], cp = "From")){
926 np = hp->h_from;
927 jlist:
928 fprintf(n_stdout, "%s %s\n", (np == NULL ? "501" : "210"), cp);
929 goto jleave;
931 if(!asccasecmp(cmd[2], cp = "Sender")){
932 np = hp->h_sender;
933 goto jlist;
935 if(!asccasecmp(cmd[2], cp = "To")){
936 np = hp->h_to;
937 goto jlist;
939 if(!asccasecmp(cmd[2], cp = "Cc")){
940 np = hp->h_cc;
941 goto jlist;
943 if(!asccasecmp(cmd[2], cp = "Bcc")){
944 np = hp->h_bcc;
945 goto jlist;
947 if(!asccasecmp(cmd[2], cp = "Reply-To")){
948 np = hp->h_replyto;
949 goto jlist;
951 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
952 np = hp->h_mft;
953 goto jlist;
955 if(!asccasecmp(cmd[2], cp = "Message-ID")){
956 np = hp->h_message_id;
957 goto jlist;
959 if(!asccasecmp(cmd[2], cp = "References")){
960 np = hp->h_ref;
961 goto jlist;
963 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
964 np = hp->h_in_reply_to;
965 goto jlist;
968 if(!asccasecmp(cmd[2], cp = "Mailx-Command")){
969 np = (hp->h_mailx_command != NULL) ? (struct name*)-1 : NULL;
970 goto jlist;
972 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-To")){
973 np = hp->h_mailx_raw_to;
974 goto jlist;
976 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-Cc")){
977 np = hp->h_mailx_raw_cc;
978 goto jlist;
980 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-Bcc")){
981 np = hp->h_mailx_raw_bcc;
982 goto jlist;
984 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-From")){
985 np = hp->h_mailx_orig_from;
986 goto jlist;
988 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-To")){
989 np = hp->h_mailx_orig_to;
990 goto jlist;
992 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-Cc")){
993 np = hp->h_mailx_orig_cc;
994 goto jlist;
996 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-Bcc")){
997 np = hp->h_mailx_orig_bcc;
998 goto jlist;
1001 /* Free-form header fields (note j501cp may print non-normalized name) */
1002 for(cp = cmd[2]; *cp != '\0'; ++cp)
1003 if(!fieldnamechar(*cp)){
1004 cp = cmd[2];
1005 goto j501cp;
1007 cp = cmd[2];
1008 for(hfp = hp->h_user_headers;; hfp = hfp->hf_next){
1009 if(hfp == NULL)
1010 goto j501cp;
1011 else if(!asccasecmp(cp, &hfp->hf_dat[0])){
1012 fprintf(n_stdout, "210 %s\n", &hfp->hf_dat[0]);
1013 break;
1016 goto jleave;
1019 if(is_asccaseprefix(cmd[1], "remove")){
1020 if(cmd[2] == NULL || cmd[3] != NULL)
1021 goto jecmd;
1023 if(!asccasecmp(cmd[2], cp = "Subject")){
1024 if(hp->h_subject != NULL){
1025 hp->h_subject = NULL;
1026 fprintf(n_stdout, "210 %s\n", cp);
1027 goto jleave;
1028 }else
1029 goto j501cp;
1032 if(!asccasecmp(cmd[2], cp = "From")){
1033 npp = &hp->h_from;
1034 jrem:
1035 if(*npp != NULL){
1036 *npp = NULL;
1037 fprintf(n_stdout, "210 %s\n", cp);
1038 goto jleave;
1039 }else
1040 goto j501cp;
1042 if(!asccasecmp(cmd[2], cp = "Sender")){
1043 npp = &hp->h_sender;
1044 goto jrem;
1046 if(!asccasecmp(cmd[2], cp = "To")){
1047 npp = &hp->h_to;
1048 goto jrem;
1050 if(!asccasecmp(cmd[2], cp = "Cc")){
1051 npp = &hp->h_cc;
1052 goto jrem;
1054 if(!asccasecmp(cmd[2], cp = "Bcc")){
1055 npp = &hp->h_bcc;
1056 goto jrem;
1058 if(!asccasecmp(cmd[2], cp = "Reply-To")){
1059 npp = &hp->h_replyto;
1060 goto jrem;
1062 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
1063 npp = &hp->h_mft;
1064 goto jrem;
1066 if(!asccasecmp(cmd[2], cp = "Message-ID")){
1067 npp = &hp->h_message_id;
1068 goto jrem;
1070 if(!asccasecmp(cmd[2], cp = "References")){
1071 npp = &hp->h_ref;
1072 goto jrem;
1074 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
1075 npp = &hp->h_in_reply_to;
1076 goto jrem;
1079 if(!asccasecmp(cmd[2], cp = "Mailx-Command") ||
1080 !asccasecmp(cmd[2], cp = "Mailx-Raw-To") ||
1081 !asccasecmp(cmd[2], cp = "Mailx-Raw-Cc") ||
1082 !asccasecmp(cmd[2], cp = "Mailx-Raw-Bcc") ||
1083 !asccasecmp(cmd[2], cp = "Mailx-Orig-From") ||
1084 !asccasecmp(cmd[2], cp = "Mailx-Orig-To") ||
1085 !asccasecmp(cmd[2], cp = "Mailx-Orig-Cc") ||
1086 !asccasecmp(cmd[2], cp = "Mailx-Orig-Bcc")){
1087 fprintf(n_stdout, "505 %s\n", cp);
1088 goto jleave;
1091 /* Free-form header fields (note j501cp may print non-normalized name) */
1092 /* C99 */{
1093 struct n_header_field **hfpp;
1094 bool_t any;
1096 for(cp = cmd[2]; *cp != '\0'; ++cp)
1097 if(!fieldnamechar(*cp)){
1098 cp = cmd[2];
1099 goto j501cp;
1101 cp = cmd[2];
1103 for(any = FAL0, hfpp = &hp->h_user_headers; (hfp = *hfpp) != NULL;){
1104 if(!asccasecmp(cp, &hfp->hf_dat[0])){
1105 *hfpp = hfp->hf_next;
1106 if(!any)
1107 fprintf(n_stdout, "210 %s\n", &hfp->hf_dat[0]);
1108 any = TRU1;
1109 }else
1110 hfpp = &hfp->hf_next;
1112 if(!any)
1113 goto j501cp;
1115 goto jleave;
1118 if(is_asccaseprefix(cmd[1], "remove-at")){
1119 if(cmd[2] == NULL || cmd[3] == NULL)
1120 goto jecmd;
1122 if((n_idec_uiz_cp(&i, cmd[3], 0, NULL
1123 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
1124 ) != n_IDEC_STATE_CONSUMED || i == 0){
1125 fputs("505\n", n_stdout);
1126 goto jleave;
1129 if(!asccasecmp(cmd[2], cp = "Subject")){
1130 if(hp->h_subject != NULL && i == 1){
1131 hp->h_subject = NULL;
1132 fprintf(n_stdout, "210 %s 1\n", cp);
1133 goto jleave;
1134 }else
1135 goto j501cp;
1138 if(!asccasecmp(cmd[2], cp = "From")){
1139 npp = &hp->h_from;
1140 jremat:
1141 if((np = *npp) == NULL)
1142 goto j501cp;
1143 while(--i != 0 && np != NULL)
1144 np = np->n_flink;
1145 if(np == NULL)
1146 goto j501cp;
1148 if(np->n_blink != NULL)
1149 np->n_blink->n_flink = np->n_flink;
1150 else
1151 *npp = np->n_flink;
1152 if(np->n_flink != NULL)
1153 np->n_flink->n_blink = np->n_blink;
1155 fprintf(n_stdout, "210 %s\n", cp);
1156 goto jleave;
1158 if(!asccasecmp(cmd[2], cp = "Sender")){
1159 npp = &hp->h_sender;
1160 goto jremat;
1162 if(!asccasecmp(cmd[2], cp = "To")){
1163 npp = &hp->h_to;
1164 goto jremat;
1166 if(!asccasecmp(cmd[2], cp = "Cc")){
1167 npp = &hp->h_cc;
1168 goto jremat;
1170 if(!asccasecmp(cmd[2], cp = "Bcc")){
1171 npp = &hp->h_bcc;
1172 goto jremat;
1174 if(!asccasecmp(cmd[2], cp = "Reply-To")){
1175 npp = &hp->h_replyto;
1176 goto jremat;
1178 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
1179 npp = &hp->h_mft;
1180 goto jremat;
1182 if(!asccasecmp(cmd[2], cp = "Message-ID")){
1183 npp = &hp->h_message_id;
1184 goto jremat;
1186 if(!asccasecmp(cmd[2], cp = "References")){
1187 npp = &hp->h_ref;
1188 goto jremat;
1190 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
1191 npp = &hp->h_in_reply_to;
1192 goto jremat;
1195 if(!asccasecmp(cmd[2], cp = "Mailx-Command") ||
1196 !asccasecmp(cmd[2], cp = "Mailx-Raw-To") ||
1197 !asccasecmp(cmd[2], cp = "Mailx-Raw-Cc") ||
1198 !asccasecmp(cmd[2], cp = "Mailx-Raw-Bcc") ||
1199 !asccasecmp(cmd[2], cp = "Mailx-Orig-From") ||
1200 !asccasecmp(cmd[2], cp = "Mailx-Orig-To") ||
1201 !asccasecmp(cmd[2], cp = "Mailx-Orig-Cc") ||
1202 !asccasecmp(cmd[2], cp = "Mailx-Orig-Bcc")){
1203 fprintf(n_stdout, "505 %s\n", cp);
1204 goto jleave;
1207 /* Free-form header fields (note j501cp may print non-normalized name) */
1208 /* C99 */{
1209 struct n_header_field **hfpp;
1211 for(cp = cmd[2]; *cp != '\0'; ++cp)
1212 if(!fieldnamechar(*cp)){
1213 cp = cmd[2];
1214 goto j501cp;
1216 cp = cmd[2];
1218 for(hfpp = &hp->h_user_headers; (hfp = *hfpp) != NULL;){
1219 if(--i == 0){
1220 *hfpp = hfp->hf_next;
1221 fprintf(n_stdout, "210 %s %" PRIuZ "\n", &hfp->hf_dat[0], i);
1222 break;
1223 }else
1224 hfpp = &hfp->hf_next;
1226 if(hfp == NULL)
1227 goto j501cp;
1229 goto jleave;
1232 if(is_asccaseprefix(cmd[1], "show")){
1233 if(cmd[2] == NULL || cmd[3] != NULL)
1234 goto jecmd;
1236 if(!asccasecmp(cmd[2], cp = "Subject")){
1237 if(hp->h_subject == NULL)
1238 goto j501cp;
1239 fprintf(n_stdout, "212 %s\n%s\n\n", cp, hp->h_subject);
1240 goto jleave;
1243 if(!asccasecmp(cmd[2], cp = "From")){
1244 np = hp->h_from;
1245 jshow:
1246 if(np != NULL){
1247 fprintf(n_stdout, "211 %s\n", cp);
1248 do if(!(np->n_type & GDEL))
1249 fprintf(n_stdout, "%s %s\n", np->n_name, np->n_fullname);
1250 while((np = np->n_flink) != NULL);
1251 putc('\n', n_stdout);
1252 goto jleave;
1253 }else
1254 goto j501cp;
1256 if(!asccasecmp(cmd[2], cp = "Sender")){
1257 np = hp->h_sender;
1258 goto jshow;
1260 if(!asccasecmp(cmd[2], cp = "To")){
1261 np = hp->h_to;
1262 goto jshow;
1264 if(!asccasecmp(cmd[2], cp = "Cc")){
1265 np = hp->h_cc;
1266 goto jshow;
1268 if(!asccasecmp(cmd[2], cp = "Bcc")){
1269 np = hp->h_bcc;
1270 goto jshow;
1272 if(!asccasecmp(cmd[2], cp = "Reply-To")){
1273 np = hp->h_replyto;
1274 goto jshow;
1276 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
1277 np = hp->h_mft;
1278 goto jshow;
1280 if(!asccasecmp(cmd[2], cp = "Message-ID")){
1281 np = hp->h_message_id;
1282 goto jshow;
1284 if(!asccasecmp(cmd[2], cp = "References")){
1285 np = hp->h_ref;
1286 goto jshow;
1288 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
1289 np = hp->h_in_reply_to;
1290 goto jshow;
1293 if(!asccasecmp(cmd[2], cp = "Mailx-Command")){
1294 if(hp->h_mailx_command == NULL)
1295 goto j501cp;
1296 fprintf(n_stdout, "212 %s\n%s\n\n", cp, hp->h_mailx_command);
1297 goto jleave;
1299 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-To")){
1300 np = hp->h_mailx_raw_to;
1301 goto jshow;
1303 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-Cc")){
1304 np = hp->h_mailx_raw_cc;
1305 goto jshow;
1307 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-Bcc")){
1308 np = hp->h_mailx_raw_bcc;
1309 goto jshow;
1311 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-From")){
1312 np = hp->h_mailx_orig_from;
1313 goto jshow;
1315 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-To")){
1316 np = hp->h_mailx_orig_to;
1317 goto jshow;
1319 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-Cc")){
1320 np = hp->h_mailx_orig_cc;
1321 goto jshow;
1323 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-Bcc")){
1324 np = hp->h_mailx_orig_bcc;
1325 goto jshow;
1328 /* Free-form header fields (note j501cp may print non-normalized name) */
1329 /* C99 */{
1330 bool_t any;
1332 for(cp = cmd[2]; *cp != '\0'; ++cp)
1333 if(!fieldnamechar(*cp)){
1334 cp = cmd[2];
1335 goto j501cp;
1337 cp = cmd[2];
1339 for(any = FAL0, hfp = hp->h_user_headers; hfp != NULL;
1340 hfp = hfp->hf_next){
1341 if(!asccasecmp(cp, &hfp->hf_dat[0])){
1342 if(!any)
1343 fprintf(n_stdout, "212 %s\n", &hfp->hf_dat[0]);
1344 any = TRU1;
1345 fprintf(n_stdout, "%s\n", &hfp->hf_dat[hfp->hf_nl +1]);
1348 if(any)
1349 putc('\n', n_stdout);
1350 else
1351 goto j501cp;
1353 goto jleave;
1356 jecmd:
1357 fputs("500\n", n_stdout);
1358 cp = NULL;
1359 jleave:
1360 NYD2_LEAVE;
1361 return (cp != NULL);
1363 j501cp:
1364 fputs("501 ", n_stdout);
1365 fputs(cp, n_stdout);
1366 putc('\n', n_stdout);
1367 goto jleave;
1370 static bool_t
1371 a_collect__plumb_attach(char const *cp, struct header *hp,
1372 char const *cmd[4]){
1373 bool_t status;
1374 struct attachment *ap;
1375 NYD2_ENTER;
1377 if(cmd[1] == NULL)
1378 goto jdefault;
1380 if(is_asccaseprefix(cmd[1], "attribute")){
1381 if(cmd[2] == NULL || cmd[3] != NULL)
1382 goto jecmd;
1384 if((ap = n_attachment_find(hp->h_attach, cmd[2], NULL)) != NULL){
1385 jatt_att:
1386 fprintf(n_stdout, "212 %s\n", cmd[2]);
1387 if(ap->a_msgno > 0)
1388 fprintf(n_stdout, "message-number %d\n\n", ap->a_msgno);
1389 else{
1390 fprintf(n_stdout,
1391 "creation-name %s\nopen-path %s\nfilename %s\n",
1392 ap->a_path_user, ap->a_path, ap->a_name);
1393 if(ap->a_content_description != NULL)
1394 fprintf(n_stdout, "content-description %s\n",
1395 ap->a_content_description);
1396 if(ap->a_content_id != NULL)
1397 fprintf(n_stdout, "content-id %s\n",
1398 ap->a_content_id->n_name);
1399 if(ap->a_content_type != NULL)
1400 fprintf(n_stdout, "content-type %s\n", ap->a_content_type);
1401 if(ap->a_content_disposition != NULL)
1402 fprintf(n_stdout, "content-disposition %s\n",
1403 ap->a_content_disposition);
1404 putc('\n', n_stdout);
1406 }else
1407 fputs("501\n", n_stdout);
1408 goto jleave;
1411 if(is_asccaseprefix(cmd[1], "attribute-at")){
1412 uiz_t i;
1414 if(cmd[2] == NULL || cmd[3] != NULL)
1415 goto jecmd;
1417 if((n_idec_uiz_cp(&i, cmd[2], 0, NULL
1418 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
1419 ) != n_IDEC_STATE_CONSUMED || i == 0)
1420 fputs("505\n", n_stdout);
1421 else{
1422 for(ap = hp->h_attach; ap != NULL && --i != 0; ap = ap->a_flink)
1424 if(ap != NULL)
1425 goto jatt_att;
1426 else
1427 fputs("501\n", n_stdout);
1429 goto jleave;
1432 if(is_asccaseprefix(cmd[1], "attribute-set")){
1433 if(cmd[2] == NULL || cmd[3] == NULL)
1434 goto jecmd;
1436 if((ap = n_attachment_find(hp->h_attach, cmd[2], NULL)) != NULL){
1437 jatt_attset:
1438 if(ap->a_msgno > 0)
1439 fputs("505\n", n_stdout);
1440 else{
1441 char c, *keyw;
1443 cp = cmd[3];
1444 while((c = *cp) != '\0' && !blankchar(c))
1445 ++cp;
1446 keyw = savestrbuf(cmd[3], PTR2SIZE(cp - cmd[3]));
1447 if(c != '\0'){
1448 for(; (c = *++cp) != '\0' && blankchar(c);)
1450 if(c != '\0'){
1451 char *xp;
1453 /* Strip [\r\n] which would render a parameter invalid XXX
1454 * XXX all controls? */
1455 cp = xp = savestr(cp);
1456 for(; (c = *xp) != '\0'; ++xp)
1457 if(c == '\n' || c == '\r')
1458 *xp = ' ';
1459 c = *cp;
1463 if(!asccasecmp(keyw, "filename"))
1464 ap->a_name = (c == '\0') ? ap->a_path_bname : cp;
1465 else if(!asccasecmp(keyw, "content-description"))
1466 ap->a_content_description = (c == '\0') ? NULL : cp;
1467 else if(!asccasecmp(keyw, "content-id")){
1468 ap->a_content_id = NULL;
1470 if(c != '\0'){
1471 struct name *np;
1473 np = checkaddrs(lextract(cp, GREF),
1474 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG |
1475 EACM_NONAME, NULL);
1476 if(np != NULL && np->n_flink == NULL)
1477 ap->a_content_id = np;
1478 else
1479 cp = NULL;
1481 }else if(!asccasecmp(keyw, "content-type"))
1482 ap->a_content_type = (c == '\0') ? NULL : cp;
1483 else if(!asccasecmp(keyw, "content-disposition"))
1484 ap->a_content_disposition = (c == '\0') ? NULL : cp;
1485 else
1486 cp = NULL;
1488 if(cp != NULL){
1489 size_t i;
1491 for(i = 0; ap != NULL; ++i, ap = ap->a_blink)
1493 fprintf(n_stdout, "210 %" PRIuZ "\n", i);
1494 }else
1495 fputs("505\n", n_stdout);
1497 }else
1498 fputs("501\n", n_stdout);
1499 goto jleave;
1502 if(is_asccaseprefix(cmd[1], "attribute-set-at")){
1503 uiz_t i;
1505 if(cmd[2] == NULL || cmd[3] == NULL)
1506 goto jecmd;
1508 if((n_idec_uiz_cp(&i, cmd[2], 0, NULL
1509 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
1510 ) != n_IDEC_STATE_CONSUMED || i == 0)
1511 fputs("505\n", n_stdout);
1512 else{
1513 for(ap = hp->h_attach; ap != NULL && --i != 0; ap = ap->a_flink)
1515 if(ap != NULL)
1516 goto jatt_attset;
1517 else
1518 fputs("501\n", n_stdout);
1520 goto jleave;
1523 if(is_asccaseprefix(cmd[1], "insert")){
1524 enum n_attach_error aerr;
1526 if(cmd[2] == NULL || cmd[3] != NULL)
1527 goto jecmd;
1529 hp->h_attach = n_attachment_append(hp->h_attach, cmd[2], &aerr, &ap);
1530 switch(aerr){
1531 case n_ATTACH_ERR_FILE_OPEN: cp = "505\n"; goto jatt_ins;
1532 case n_ATTACH_ERR_ICONV_FAILED: cp = "506\n"; goto jatt_ins;
1533 case n_ATTACH_ERR_ICONV_NAVAIL:
1534 case n_ATTACH_ERR_OTHER:
1535 default:
1536 cp = "501\n";
1537 jatt_ins:
1538 fputs(cp, n_stdout);
1539 break;
1540 case n_ATTACH_ERR_NONE:{
1541 size_t i;
1543 for(i = 0; ap != NULL; ++i, ap = ap->a_blink)
1545 fprintf(n_stdout, "210 %" PRIuZ "\n", i);
1546 } break;
1548 goto jleave;
1551 if(is_asccaseprefix(cmd[1], "list")){
1552 jdefault:
1553 if(cmd[2] != NULL)
1554 goto jecmd;
1556 if((ap = hp->h_attach) != NULL){
1557 fputs("212\n", n_stdout);
1559 fprintf(n_stdout, "%s\n", ap->a_path_user);
1560 while((ap = ap->a_flink) != NULL);
1561 putc('\n', n_stdout);
1562 }else
1563 fputs("501\n", n_stdout);
1564 goto jleave;
1567 if(is_asccaseprefix(cmd[1], "remove")){
1568 if(cmd[2] == NULL || cmd[3] != NULL)
1569 goto jecmd;
1571 if((ap = n_attachment_find(hp->h_attach, cmd[2], &status)) != NULL){
1572 if(status == TRUM1)
1573 fputs("506\n", n_stdout);
1574 else{
1575 hp->h_attach = n_attachment_remove(hp->h_attach, ap);
1576 fprintf(n_stdout, "210 %s\n", cmd[2]);
1578 }else
1579 fputs("501\n", n_stdout);
1580 goto jleave;
1583 if(is_asccaseprefix(cmd[1], "remove-at")){
1584 uiz_t i;
1586 if(cmd[2] == NULL || cmd[3] != NULL)
1587 goto jecmd;
1589 if((n_idec_uiz_cp(&i, cmd[2], 0, NULL
1590 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
1591 ) != n_IDEC_STATE_CONSUMED || i == 0)
1592 fputs("505\n", n_stdout);
1593 else{
1594 for(ap = hp->h_attach; ap != NULL && --i != 0; ap = ap->a_flink)
1596 if(ap != NULL){
1597 hp->h_attach = n_attachment_remove(hp->h_attach, ap);
1598 fprintf(n_stdout, "210 %s\n", cmd[2]);
1599 }else
1600 fputs("501\n", n_stdout);
1602 goto jleave;
1605 jecmd:
1606 fputs("500\n", n_stdout);
1607 cp = NULL;
1608 jleave:
1609 NYD2_LEAVE;
1610 return (cp != NULL);
1613 static void
1614 _collint(int s)
1616 NYD_X; /* Signal handler */
1618 /* the control flow is subtle, because we can be called from ~q */
1619 if (_coll_hadintr == 0) {
1620 if (ok_blook(ignore)) {
1621 fputs("@\n", n_stdout);
1622 fflush(n_stdout);
1623 clearerr(n_stdin);
1624 } else
1625 _coll_hadintr = 1;
1626 siglongjmp(_coll_jmp, 1);
1628 n_exit_status |= n_EXIT_SEND_ERROR;
1629 if (s != 0)
1630 savedeadletter(_coll_fp, TRU1);
1631 /* Aborting message, no need to fflush() .. */
1632 siglongjmp(_coll_abort, 1);
1635 static void
1636 collhup(int s)
1638 NYD_X; /* Signal handler */
1639 n_UNUSED(s);
1641 savedeadletter(_coll_fp, TRU1);
1642 /* Let's pretend nobody else wants to clean up, a true statement at
1643 * this time */
1644 exit(n_EXIT_ERR);
1647 static int
1648 putesc(char const *s, FILE *stream) /* TODO: v15: drop */
1650 int n = 0, rv = -1;
1651 NYD_ENTER;
1653 while (s[0] != '\0') {
1654 if (s[0] == '\\') {
1655 if (s[1] == 't') {
1656 if (putc('\t', stream) == EOF)
1657 goto jleave;
1658 ++n;
1659 s += 2;
1660 continue;
1662 if (s[1] == 'n') {
1663 if (putc('\n', stream) == EOF)
1664 goto jleave;
1665 ++n;
1666 s += 2;
1667 continue;
1670 if (putc(s[0], stream) == EOF)
1671 goto jleave;
1672 ++n;
1673 ++s;
1675 if (putc('\n', stream) == EOF)
1676 goto jleave;
1677 rv = ++n;
1678 jleave:
1679 NYD_LEAVE;
1680 return rv;
1683 static int
1684 a_coll_ocs__mac(void){
1685 /* Executes in a fork(2)ed child TODO if remains, global MASKs for those! */
1686 setvbuf(n_stdin, NULL, _IOLBF, 0);
1687 setvbuf(n_stdout, NULL, _IOLBF, 0);
1688 n_psonce &= ~(n_PSO_INTERACTIVE | n_PSO_TTYIN | n_PSO_TTYOUT);
1689 n_pstate |= n_PS_COMPOSE_FORKHOOK;
1690 n_readctl_overlay = NULL; /* TODO we need OnForkEvent! See c_readctl() */
1691 if(n_poption & n_PO_D_VV){
1692 char buf[128];
1694 snprintf(buf, sizeof buf, "[%d]%s", getpid(), ok_vlook(log_prefix));
1695 ok_vset(log_prefix, buf);
1697 /* TODO If that uses `!' it will effectively SIG_IGN SIGINT, ...and such */
1698 temporary_compose_mode_hook_call(a_coll_ocs__macname, NULL, NULL);
1699 return 0;
1702 static void
1703 a_coll_ocs__finalize(void *vp){
1704 /* Note we use this for destruction upon setup errors, thus */
1705 sighandler_type opipe;
1706 sighandler_type oint;
1707 struct a_coll_ocs_arg **coapp, *coap;
1708 NYD2_ENTER;
1710 temporary_compose_mode_hook_call((char*)-1, NULL, NULL);
1712 coap = *(coapp = vp);
1713 *coapp = (struct a_coll_ocs_arg*)-1;
1715 if(coap->coa_stdin != NULL)
1716 Fclose(coap->coa_stdin);
1717 else if(coap->coa_pipe[0] != -1)
1718 close(coap->coa_pipe[0]);
1720 if(coap->coa_stdout != NULL && !Pclose(coap->coa_stdout, TRU1))
1721 *coap->coa_senderr = 111;
1722 if(coap->coa_pipe[1] != -1)
1723 close(coap->coa_pipe[1]);
1725 opipe = coap->coa_opipe;
1726 oint = coap->coa_oint;
1728 n_lofi_free(coap);
1730 hold_all_sigs();
1731 safe_signal(SIGPIPE, opipe);
1732 safe_signal(SIGINT, oint);
1733 rele_all_sigs();
1734 NYD2_LEAVE;
1737 FL void
1738 n_temporary_compose_hook_varset(void *arg){ /* TODO v15: drop */
1739 struct header *hp;
1740 char const *val;
1741 NYD2_ENTER;
1743 hp = arg;
1745 if((val = hp->h_subject) == NULL)
1746 val = n_empty;
1747 ok_vset(mailx_subject, val);
1748 if((val = detract(hp->h_from, GNAMEONLY)) == NULL)
1749 val = n_empty;
1750 ok_vset(mailx_from, val);
1751 if((val = detract(hp->h_sender, GNAMEONLY)) == NULL)
1752 val = n_empty;
1753 ok_vset(mailx_sender, val);
1754 if((val = detract(hp->h_to, GNAMEONLY)) == NULL)
1755 val = n_empty;
1756 ok_vset(mailx_to, val);
1757 if((val = detract(hp->h_cc, GNAMEONLY)) == NULL)
1758 val = n_empty;
1759 ok_vset(mailx_cc, val);
1760 if((val = detract(hp->h_bcc, GNAMEONLY)) == NULL)
1761 val = n_empty;
1762 ok_vset(mailx_bcc, val);
1764 if((val = hp->h_mailx_command) == NULL)
1765 val = n_empty;
1766 ok_vset(mailx_command, val);
1768 if((val = detract(hp->h_mailx_raw_to, GNAMEONLY)) == NULL)
1769 val = n_empty;
1770 ok_vset(mailx_raw_to, val);
1771 if((val = detract(hp->h_mailx_raw_cc, GNAMEONLY)) == NULL)
1772 val = n_empty;
1773 ok_vset(mailx_raw_cc, val);
1774 if((val = detract(hp->h_mailx_raw_bcc, GNAMEONLY)) == NULL)
1775 val = n_empty;
1776 ok_vset(mailx_raw_bcc, val);
1778 if((val = detract(hp->h_mailx_orig_from, GNAMEONLY)) == NULL)
1779 val = n_empty;
1780 ok_vset(mailx_orig_from, val);
1781 if((val = detract(hp->h_mailx_orig_to, GNAMEONLY)) == NULL)
1782 val = n_empty;
1783 ok_vset(mailx_orig_to, val);
1784 if((val = detract(hp->h_mailx_orig_cc, GNAMEONLY)) == NULL)
1785 val = n_empty;
1786 ok_vset(mailx_orig_cc, val);
1787 if((val = detract(hp->h_mailx_orig_bcc, GNAMEONLY)) == NULL)
1788 val = n_empty;
1789 ok_vset(mailx_orig_bcc, val);
1790 NYD2_LEAVE;
1793 FL FILE *
1794 collect(struct header *hp, int printheaders, struct message *mp,
1795 char const *quotefile, int doprefix, si8_t *checkaddr_err)
1797 struct n_string s, * volatile sp;
1798 struct n_ignore const *quoteitp;
1799 struct a_coll_ocs_arg *coap;
1800 int c;
1801 int volatile t, eofcnt, getfields;
1802 char volatile escape;
1803 enum{
1804 a_NONE,
1805 a_ERREXIT = 1u<<0,
1806 a_IGNERR = 1u<<1,
1807 a_COAP_NOSIGTERM = 1u<<8
1808 #define a_HARDERR() ((flags & (a_ERREXIT | a_IGNERR)) == a_ERREXIT)
1809 } volatile flags;
1810 char *linebuf;
1811 char const *cp, *cp_base, * volatile coapm, * volatile ifs_saved;
1812 size_t i, linesize; /* TODO line pool */
1813 long cnt;
1814 enum sendaction action;
1815 sigset_t oset, nset;
1816 FILE * volatile sigfp;
1817 NYD_ENTER;
1819 _coll_fp = NULL;
1820 sigfp = NULL;
1821 linesize = 0;
1822 linebuf = NULL;
1823 eofcnt = 0;
1824 ifs_saved = coapm = NULL;
1825 coap = NULL;
1826 sp = NULL;
1828 /* Start catching signals from here, but we're still die on interrupts
1829 * until we're in the main loop */
1830 sigfillset(&nset);
1831 sigprocmask(SIG_BLOCK, &nset, &oset);
1832 if ((_coll_saveint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
1833 safe_signal(SIGINT, &_collint);
1834 if ((_coll_savehup = safe_signal(SIGHUP, SIG_IGN)) != SIG_IGN)
1835 safe_signal(SIGHUP, collhup);
1836 if (sigsetjmp(_coll_abort, 1))
1837 goto jerr;
1838 if (sigsetjmp(_coll_jmp, 1))
1839 goto jerr;
1840 n_pstate |= n_PS_COMPOSE_MODE;
1841 sigprocmask(SIG_SETMASK, &oset, (sigset_t*)NULL);
1843 if ((_coll_fp = Ftmp(NULL, "collect", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
1844 NULL) {
1845 n_perr(_("temporary mail file"), 0);
1846 goto jerr;
1849 /* If we are going to prompt for a subject, refrain from printing a newline
1850 * after the headers (since some people mind) */
1851 getfields = 0;
1852 if (!(n_poption & n_PO_t_FLAG)) {
1853 t = GTO | GSUBJECT | GCC | GNL;
1854 if (ok_blook(fullnames))
1855 t |= GCOMMA;
1857 if (n_psonce & n_PSO_INTERACTIVE) {
1858 if (hp->h_subject == NULL && (ok_blook(ask) || ok_blook(asksub)))
1859 t &= ~GNL, getfields |= GSUBJECT;
1861 if (hp->h_to == NULL)
1862 t &= ~GNL, getfields |= GTO;
1864 if (!ok_blook(bsdcompat) && !ok_blook(askatend)) {
1865 if (hp->h_bcc == NULL && ok_blook(askbcc))
1866 t &= ~GNL, getfields |= GBCC;
1867 if (hp->h_cc == NULL && ok_blook(askcc))
1868 t &= ~GNL, getfields |= GCC;
1871 } else {
1872 n_UNINIT(t, 0);
1875 _coll_hadintr = 0;
1877 if (!sigsetjmp(_coll_jmp, 1)) {
1878 /* Ask for some headers first, as necessary */
1879 if (getfields)
1880 grab_headers(n_GO_INPUT_CTX_COMPOSE, hp, getfields, 1);
1882 /* Execute compose-enter; delayed for -t mode */
1883 if(!(n_poption & n_PO_t_FLAG) &&
1884 (cp = ok_vlook(on_compose_enter)) != NULL){
1885 setup_from_and_sender(hp);
1886 temporary_compose_mode_hook_call(cp, &n_temporary_compose_hook_varset,
1887 hp);
1890 /* TODO Mm: nope since it may require turning this into a multipart one */
1891 if(!(n_poption & (n_PO_Mm_FLAG | n_PO_t_FLAG))){
1892 if(!a_coll_message_inject_head(_coll_fp))
1893 goto jerr;
1895 /* Quote an original message */
1896 if (mp != NULL && (doprefix || (cp = ok_vlook(quote)) != NULL)) {
1897 quoteitp = n_IGNORE_ALL;
1898 action = SEND_QUOTE;
1899 if (doprefix) {
1900 char const *cp_v15compat;
1902 quoteitp = n_IGNORE_FWD;
1904 if((cp_v15compat = ok_vlook(fwdheading)) != NULL)
1905 n_OBSOLETE(_("please use *forward-inject-head*, "
1906 "not *fwdheading*"));
1907 cp = ok_vlook(forward_inject_head);
1908 if(cp == NULL && (cp = cp_v15compat) == NULL)
1909 cp = "-------- Original Message --------";
1910 if (*cp != '\0' && fprintf(_coll_fp, "%s\n", cp) < 0)
1911 goto jerr;
1912 } else if (!strcmp(cp, "noheading")) {
1913 /*EMPTY*/;
1914 } else if (!strcmp(cp, "headers")) {
1915 quoteitp = n_IGNORE_TYPE;
1916 } else if (!strcmp(cp, "allheaders")) {
1917 quoteitp = NULL;
1918 action = SEND_QUOTE_ALL;
1919 } else {
1920 cp = hfield1("from", mp);
1921 if (cp != NULL && (cnt = (long)strlen(cp)) > 0) {
1922 if (xmime_write(cp, cnt, _coll_fp, CONV_FROMHDR, TD_NONE) < 0)
1923 goto jerr;
1924 if (fprintf(_coll_fp, _(" wrote:\n\n")) < 0)
1925 goto jerr;
1928 if (fflush(_coll_fp))
1929 goto jerr;
1930 if (doprefix)
1931 cp = NULL;
1932 else if ((cp = ok_vlook(indentprefix)) == NULL)
1933 cp = INDENT_DEFAULT;
1934 if (sendmp(mp, _coll_fp, quoteitp, cp, action, NULL) < 0)
1935 goto jerr;
1939 if (quotefile != NULL) {
1940 if((n_pstate_err_no = a_coll_include_file(quotefile, FAL0, FAL0)
1941 ) != n_ERR_NONE)
1942 goto jerr;
1945 if(n_psonce & n_PSO_INTERACTIVE){
1946 if(!(n_pstate & n_PS_SOURCING)){
1947 sp = n_string_creat_auto(&s);
1948 sp = n_string_reserve(sp, 80);
1951 if(!(n_poption & n_PO_Mm_FLAG)){
1952 /* Print what we have sofar also on the terminal (if useful) */
1953 if (!ok_blook(editalong)) {
1954 if (printheaders)
1955 puthead(TRU1, hp, n_stdout, t,
1956 SEND_TODISP, CONV_NONE, NULL, NULL);
1958 rewind(_coll_fp);
1959 while ((c = getc(_coll_fp)) != EOF) /* XXX bytewise, yuck! */
1960 putc(c, n_stdout);
1961 if (fseek(_coll_fp, 0, SEEK_END))
1962 goto jerr;
1963 } else {
1964 rewind(_coll_fp);
1965 if(a_coll_edit('e', hp) != n_ERR_NONE)
1966 goto jerr;
1967 /* Print msg mandated by the Mail Reference Manual */
1968 jcont:
1969 if(n_psonce & n_PSO_INTERACTIVE)
1970 fputs(_("(continue)\n"), n_stdout);
1972 fflush(n_stdout);
1975 } else {
1976 /* Come here for printing the after-signal message. Duplicate messages
1977 * won't be printed because the write is aborted if we get a SIGTTOU */
1978 if (_coll_hadintr)
1979 n_err(_("\n(Interrupt -- one more to kill letter)\n"));
1982 /* If not under shell hook control */
1983 if(coap == NULL){
1984 /* We're done with -M or -m TODO because: we are too stupid yet, above */
1985 if(n_poption & n_PO_Mm_FLAG)
1986 goto jout;
1987 /* No command escapes, interrupts not expected. Simply copy STDIN */
1988 if(!(n_psonce & n_PSO_INTERACTIVE) &&
1989 !(n_poption & (n_PO_t_FLAG | n_PO_TILDE_FLAG))){
1990 linebuf = srealloc(linebuf, linesize = LINESIZE);
1991 while ((i = fread(linebuf, sizeof *linebuf, linesize, n_stdin)) > 0) {
1992 if (i != fwrite(linebuf, sizeof *linebuf, i, _coll_fp))
1993 goto jerr;
1995 goto jout;
1999 /* The interactive collect loop */
2000 if(coap == NULL)
2001 escape = *ok_vlook(escape);
2002 flags = ok_blook(errexit) ? a_ERREXIT : a_NONE;
2004 for(;;){
2005 enum {a_HIST_NONE, a_HIST_ADD = 1u<<0, a_HIST_GABBY = 1u<<1} hist;
2007 /* C99 */{
2008 enum n_go_input_flags gif;
2009 bool_t histadd;
2011 gif = n_GO_INPUT_CTX_COMPOSE;
2012 histadd = (sp != NULL);
2013 if((n_psonce & n_PSO_INTERACTIVE) || (n_poption & n_PO_TILDE_FLAG)){
2014 if(!(n_poption & n_PO_t_FLAG))
2015 gif |= n_GO_INPUT_NL_ESC;
2017 cnt = n_go_input(gif, n_empty, &linebuf, &linesize, NULL, &histadd);
2018 hist = histadd ? a_HIST_ADD | a_HIST_GABBY : a_HIST_NONE;
2021 if(cnt < 0){
2022 if(coap != NULL)
2023 break;
2024 if(n_poption & n_PO_t_FLAG){
2025 fflush_rewind(_coll_fp);
2026 /* It is important to set n_PSO_t_FLAG before extract_header()
2027 * *and* keep n_PO_t_FLAG for the first parse of the message!
2028 * TODO This is a hack, we need a clean approach for this */
2029 n_psonce |= n_PSO_t_FLAG;
2030 if(!a_coll_makeheader(_coll_fp, hp, checkaddr_err, TRU1))
2031 goto jerr;
2032 n_poption &= ~n_PO_t_FLAG;
2033 continue;
2034 }else if((n_psonce & n_PSO_INTERACTIVE) && ok_blook(ignoreeof) &&
2035 ++eofcnt < 4){
2036 fprintf(n_stdout,
2037 _("*ignoreeof* set, use `~.' to terminate letter\n"));
2038 n_go_input_clearerr();
2039 continue;
2041 break;
2044 _coll_hadintr = 0;
2046 cp = linebuf;
2047 if(cnt == 0)
2048 goto jputnl;
2049 else if(coap == NULL){
2050 if(!(n_psonce & n_PSO_INTERACTIVE) && !(n_poption & n_PO_TILDE_FLAG))
2051 goto jputline;
2052 else if(cp[0] == '.'){
2053 if(cnt == 1 && (ok_blook(dot) ||
2054 (ok_blook(posix) && ok_blook(ignoreeof))))
2055 break;
2058 if(cp[0] != escape){
2059 jputline:
2060 if(fwrite(cp, sizeof *cp, cnt, _coll_fp) != (size_t)cnt)
2061 goto jerr;
2062 /* TODO n_PS_READLINE_NL is a terrible hack to ensure that _in_all_-
2063 * TODO _code_paths_ a file without trailing newline isn't modified
2064 * TODO to contain one; the "saw-newline" needs to be part of an
2065 * TODO I/O input machinery object */
2066 jputnl:
2067 if(n_pstate & n_PS_READLINE_NL){
2068 if(putc('\n', _coll_fp) == EOF)
2069 goto jerr;
2071 continue;
2074 c = *(cp_base = ++cp);
2075 if(--cnt == 0)
2076 goto jearg;
2078 /* Avoid history entry? */
2079 while(spacechar(c)){
2080 hist = a_HIST_NONE;
2081 c = *(cp_base = ++cp);
2082 if(--cnt == 0)
2083 goto jearg;
2086 /* It may just be an escaped escaped character, do that quick */
2087 if(c == escape)
2088 goto jputline;
2090 /* Avoid hard *errexit*? */
2091 flags &= ~a_IGNERR;
2092 if(c == '-'){
2093 flags ^= a_IGNERR;
2094 c = *++cp;
2095 if(--cnt == 0)
2096 goto jearg;
2099 /* Trim input, also to gain a somewhat normalized history entry */
2100 ++cp;
2101 if(--cnt > 0){
2102 struct str x;
2104 x.s = n_UNCONST(cp);
2105 x.l = (size_t)cnt;
2106 n_str_trim_ifs(&x, TRU1);
2107 x.s[x.l] = '\0';
2108 cp = x.s;
2109 cnt = (int)/*XXX*/x.l;
2112 if(hist != a_HIST_NONE){
2113 sp = n_string_assign_c(sp, escape);
2114 if(flags & a_IGNERR)
2115 sp = n_string_push_c(sp, '-');
2116 sp = n_string_push_c(sp, c);
2117 if(cnt > 0){
2118 sp = n_string_push_c(sp, ' ');
2119 sp = n_string_push_buf(sp, cp, cnt);
2123 switch(c){
2124 default:
2125 if(1){
2126 char buf[sizeof(n_UNIREPL)];
2128 if(asciichar(c))
2129 buf[0] = c, buf[1] = '\0';
2130 else if(n_psonce & n_PSO_UNICODE)
2131 memcpy(buf, n_unirepl, sizeof n_unirepl);
2132 else
2133 buf[0] = '?', buf[1] = '\0';
2134 n_err(_("Unknown command escape: `%c%s'\n"), escape, buf);
2135 }else
2136 jearg:
2137 n_err(_("Invalid command escape usage: %s\n"),
2138 n_shexp_quote_cp(linebuf, FAL0));
2139 if(a_HARDERR())
2140 goto jerr;
2141 n_pstate_err_no = n_ERR_INVAL;
2142 n_pstate_ex_no = 1;
2143 continue;
2144 case '!':
2145 /* Shell escape, send the balance of line to sh -c */
2146 if(cnt == 0 || coap != NULL)
2147 goto jearg;
2148 else{
2149 char const *argv[2];
2151 argv[0] = cp;
2152 argv[1] = NULL;
2153 n_pstate_ex_no = c_shell(argv); /* TODO history norm.; errexit? */
2155 goto jhistcont;
2156 case ':':
2157 /* FALLTHRU */
2158 case '_':
2159 /* Escape to command mode, but be nice! *//* TODO command expansion
2160 * TODO should be handled here so that we have unique history! */
2161 if(cnt == 0)
2162 goto jearg;
2163 _execute_command(hp, cp, cnt);
2164 if(ok_blook(errexit))
2165 flags |= a_ERREXIT;
2166 else
2167 flags &= ~a_ERREXIT;
2168 if(n_pstate_ex_no != 0 && a_HARDERR())
2169 goto jerr;
2170 if(coap == NULL)
2171 escape = *ok_vlook(escape);
2172 break;
2173 case '.':
2174 /* Simulate end of file on input */
2175 if(cnt != 0 || coap != NULL)
2176 goto jearg;
2177 goto jout; /* TODO does not enter history, thus */
2178 case 'x':
2179 /* Same as 'q', but no *DEAD* saving */
2180 /* FALLTHRU */
2181 case 'q':
2182 /* Force a quit, act like an interrupt had happened */
2183 if(cnt != 0)
2184 goto jearg;
2185 /* If we are running a splice hook, assume it quits on its own now,
2186 * otherwise we (no true out-of-band IPC to signal this state, XXX sic)
2187 * have to SIGTERM it in order to stop this wild beast */
2188 flags |= a_COAP_NOSIGTERM;
2189 ++_coll_hadintr;
2190 _collint((c == 'x') ? 0 : SIGINT);
2191 exit(n_EXIT_ERR);
2192 /*NOTREACHED*/
2193 case 'h':
2194 /* Grab a bunch of headers */
2195 if(cnt != 0)
2196 goto jearg;
2198 grab_headers(n_GO_INPUT_CTX_COMPOSE, hp,
2199 (GTO | GSUBJECT | GCC | GBCC),
2200 (ok_blook(bsdcompat) && ok_blook(bsdorder)));
2201 while(hp->h_to == NULL);
2202 n_pstate_err_no = n_ERR_NONE; /* XXX */
2203 n_pstate_ex_no = 0; /* XXX */
2204 break;
2205 case 'H':
2206 /* Grab extra headers */
2207 if(cnt != 0)
2208 goto jearg;
2210 grab_headers(n_GO_INPUT_CTX_COMPOSE, hp, GEXTRA, 0);
2211 while(check_from_and_sender(hp->h_from, hp->h_sender) == NULL);
2212 n_pstate_err_no = n_ERR_NONE; /* XXX */
2213 n_pstate_ex_no = 0; /* XXX */
2214 break;
2215 case 't':
2216 /* Add to the To: list */
2217 if(cnt == 0)
2218 goto jearg;
2219 else{
2220 struct name *np;
2221 si8_t soe;
2223 soe = 0;
2224 if((np = checkaddrs(lextract(cp, GTO | GFULL), EACM_NORMAL, &soe)
2225 ) != NULL)
2226 hp->h_to = cat(hp->h_to, np);
2227 if(soe == 0){
2228 n_pstate_err_no = n_ERR_NONE;
2229 n_pstate_ex_no = 0;
2230 }else{
2231 n_pstate_ex_no = 1;
2232 n_pstate_err_no = (soe < 0) ? n_ERR_PERM : n_ERR_INVAL;
2235 hist &= ~a_HIST_GABBY;
2236 break;
2237 case 's':
2238 /* Set the Subject list */
2239 if(cnt == 0)
2240 goto jearg;
2241 /* Subject:; take care for Debian #419840 and strip any \r and \n */
2242 if(n_anyof_cp("\n\r", hp->h_subject = savestr(cp))){
2243 char *xp;
2245 n_err(_("-s: normalizing away invalid ASCII NL / CR bytes\n"));
2246 for(xp = hp->h_subject; *xp != '\0'; ++xp)
2247 if(*xp == '\n' || *xp == '\r')
2248 *xp = ' ';
2249 n_pstate_err_no = n_ERR_INVAL;
2250 n_pstate_ex_no = 1;
2251 }else{
2252 n_pstate_err_no = n_ERR_NONE;
2253 n_pstate_ex_no = 0;
2255 break;
2256 case '@':{
2257 struct attachment *aplist;
2259 /* Edit the attachment list */
2260 aplist = hp->h_attach;
2261 hp->h_attach = NULL;
2262 if(cnt != 0)
2263 hp->h_attach = n_attachment_append_list(aplist, cp);
2264 else
2265 hp->h_attach = n_attachment_list_edit(aplist,
2266 n_GO_INPUT_CTX_COMPOSE);
2267 n_pstate_err_no = n_ERR_NONE; /* XXX ~@ does NOT handle $!/$?! */
2268 n_pstate_ex_no = 0; /* XXX */
2269 } break;
2270 case 'c':
2271 /* Add to the CC list */
2272 if(cnt == 0)
2273 goto jearg;
2274 else{
2275 struct name *np;
2276 si8_t soe;
2278 soe = 0;
2279 if((np = checkaddrs(lextract(cp, GCC | GFULL), EACM_NORMAL, &soe)
2280 ) != NULL)
2281 hp->h_cc = cat(hp->h_cc, np);
2282 if(soe == 0){
2283 n_pstate_err_no = n_ERR_NONE;
2284 n_pstate_ex_no = 0;
2285 }else{
2286 n_pstate_ex_no = 1;
2287 n_pstate_err_no = (soe < 0) ? n_ERR_PERM : n_ERR_INVAL;
2290 hist &= ~a_HIST_GABBY;
2291 break;
2292 case 'b':
2293 /* Add stuff to blind carbon copies list */
2294 if(cnt == 0)
2295 goto jearg;
2296 else{
2297 struct name *np;
2298 si8_t soe;
2300 soe = 0;
2301 if((np = checkaddrs(lextract(cp, GBCC | GFULL), EACM_NORMAL, &soe)
2302 ) != NULL)
2303 hp->h_bcc = cat(hp->h_bcc, np);
2304 if(soe == 0){
2305 n_pstate_err_no = n_ERR_NONE;
2306 n_pstate_ex_no = 0;
2307 }else{
2308 n_pstate_ex_no = 1;
2309 n_pstate_err_no = (soe < 0) ? n_ERR_PERM : n_ERR_INVAL;
2312 hist &= ~a_HIST_GABBY;
2313 break;
2314 case 'd':
2315 if(cnt != 0)
2316 goto jearg;
2317 cp = n_getdeadletter();
2318 if(0){
2319 /*FALLTHRU*/
2320 case 'R':
2321 case 'r':
2322 case '<':
2323 /* Invoke a file: Search for the file name, then open it and copy
2324 * the contents to _coll_fp */
2325 if(cnt == 0){
2326 n_err(_("Interpolate what file?\n"));
2327 if(a_HARDERR())
2328 goto jerr;
2329 n_pstate_err_no = n_ERR_NOENT;
2330 n_pstate_ex_no = 1;
2331 break;
2333 if(*cp == '!' && c == '<'){
2334 /* TODO hist. normalization */
2335 if((n_pstate_err_no = a_coll_insert_cmd(_coll_fp, ++cp)
2336 ) != n_ERR_NONE){
2337 if(ferror(_coll_fp))
2338 goto jerr;
2339 if(a_HARDERR())
2340 goto jerr;
2341 n_pstate_ex_no = 1;
2342 break;
2344 goto jhistcont;
2346 /* Note this also expands things like
2347 * !:vput vexpr delim random 0
2348 * !< - $delim */
2349 if((cp = fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO | FEXP_NSHELL)
2350 ) == NULL){
2351 if(a_HARDERR())
2352 goto jerr;
2353 n_pstate_err_no = n_ERR_INVAL;
2354 n_pstate_ex_no = 1;
2355 break;
2358 if(n_is_dir(cp, FAL0)){
2359 n_err(_("%s: is a directory\n"), n_shexp_quote_cp(cp, FAL0));
2360 if(a_HARDERR())
2361 goto jerr;
2362 n_pstate_err_no = n_ERR_ISDIR;
2363 n_pstate_ex_no = 1;
2364 break;
2366 if((n_pstate_err_no = a_coll_include_file(cp, (c == 'R'), TRU1)
2367 ) != n_ERR_NONE){
2368 if(ferror(_coll_fp))
2369 goto jerr;
2370 if(a_HARDERR())
2371 goto jerr;
2372 n_pstate_ex_no = 1;
2373 break;
2375 n_pstate_err_no = n_ERR_NONE; /* XXX */
2376 n_pstate_ex_no = 0; /* XXX */
2377 break;
2378 case 'i':
2379 /* Insert a variable into the file */
2380 if(cnt == 0)
2381 goto jearg;
2382 if((cp = n_var_vlook(cp, TRU1)) == NULL || *cp == '\0')
2383 break;
2384 if(putesc(cp, _coll_fp) < 0) /* TODO v15: user resp upon `set' time */
2385 goto jerr;
2386 if((n_psonce & n_PSO_INTERACTIVE) && putesc(cp, n_stdout) < 0)
2387 goto jerr;
2388 n_pstate_err_no = n_ERR_NONE; /* XXX */
2389 n_pstate_ex_no = 0; /* XXX */
2390 break;
2391 case 'a':
2392 case 'A':
2393 /* Insert the contents of a signature variable */
2394 if(cnt != 0)
2395 goto jearg;
2396 cp = (c == 'a') ? ok_vlook(sign) : ok_vlook(Sign);
2397 if(cp != NULL && *cp != '\0'){
2398 if(putesc(cp, _coll_fp) < 0) /* TODO v15: user upon `set' time */
2399 goto jerr;
2400 if((n_psonce & n_PSO_INTERACTIVE) && putesc(cp, n_stdout) < 0)
2401 goto jerr;
2403 n_pstate_err_no = n_ERR_NONE; /* XXX */
2404 n_pstate_ex_no = 0; /* XXX */
2405 break;
2406 case 'w':
2407 /* Write the message on a file */
2408 if(cnt == 0)
2409 goto jearg;
2410 if((cp = fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO)) == NULL){
2411 n_err(_("Write what file!?\n"));
2412 if(a_HARDERR())
2413 goto jerr;
2414 n_pstate_err_no = n_ERR_INVAL;
2415 n_pstate_ex_no = 1;
2416 break;
2418 rewind(_coll_fp);
2419 if((n_pstate_err_no = a_coll_write(cp, _coll_fp, 1)) == n_ERR_NONE)
2420 n_pstate_ex_no = 0;
2421 else if(ferror(_coll_fp))
2422 goto jerr;
2423 else if(a_HARDERR())
2424 goto jerr;
2425 else
2426 n_pstate_ex_no = 1;
2427 break;
2428 case 'm':
2429 case 'M':
2430 case 'f':
2431 case 'F':
2432 case 'u':
2433 case 'U':
2434 /* Interpolate the named messages, if we are in receiving mail mode.
2435 * Does the standard list processing garbage. If ~f is given, we
2436 * don't shift over */
2437 if(cnt == 0)
2438 goto jearg;
2439 if((n_pstate_err_no = a_coll_forward(cp, _coll_fp, c)) == n_ERR_NONE)
2440 n_pstate_ex_no = 0;
2441 else if(ferror(_coll_fp))
2442 goto jerr;
2443 else if(a_HARDERR())
2444 goto jerr;
2445 else
2446 n_pstate_ex_no = 1;
2447 break;
2448 case 'p':
2449 /* Print current state of the message without altering anything */
2450 if(cnt != 0)
2451 goto jearg;
2452 print_collf(_coll_fp, hp); /* XXX pstate_err_no ++ */
2453 if(ferror(_coll_fp))
2454 goto jerr;
2455 n_pstate_err_no = n_ERR_NONE; /* XXX */
2456 n_pstate_ex_no = 0; /* XXX */
2457 break;
2458 case '|':
2459 /* Pipe message through command. Collect output as new message */
2460 if(cnt == 0)
2461 goto jearg;
2462 rewind(_coll_fp);
2463 if((n_pstate_err_no = a_coll_pipe(cp)) == n_ERR_NONE)
2464 n_pstate_ex_no = 0;
2465 else if(a_HARDERR())
2466 goto jerr;
2467 else
2468 n_pstate_ex_no = 1;
2469 hist &= ~a_HIST_GABBY;
2470 goto jhistcont;
2471 case 'v':
2472 case 'e':
2473 /* Edit the current message. 'e' -> use EDITOR, 'v' -> use VISUAL */
2474 if(cnt != 0 || coap != NULL)
2475 goto jearg;
2476 rewind(_coll_fp);
2477 if((n_pstate_err_no = a_coll_edit(c, ok_blook(editheaders) ? hp : NULL)
2478 ) == n_ERR_NONE)
2479 n_pstate_ex_no = 0;
2480 else if(ferror(_coll_fp))
2481 goto jerr;
2482 else if(a_HARDERR())
2483 goto jerr;
2484 else
2485 n_pstate_ex_no = 1;
2486 goto jhistcont;
2487 case '^':
2488 if(!a_collect_plumbing(cp, hp)){
2489 if(ferror(_coll_fp))
2490 goto jerr;
2491 goto jearg;
2493 n_pstate_err_no = n_ERR_NONE; /* XXX */
2494 n_pstate_ex_no = 0; /* XXX */
2495 hist &= ~a_HIST_GABBY;
2496 break;
2497 case '?':
2498 /* Last the lengthy help string. (Very ugly, but take care for
2499 * compiler supported string lengths :() */
2500 fputs(_(
2501 "COMMAND ESCAPES (to be placed after a newline) excerpt:\n"
2502 "~. Commit and send message\n"
2503 "~: <command> Execute an internal command\n"
2504 "~< <file> Insert <file> (\"~<! <command>\" inserts shell command)\n"
2505 "~@ [<files>] Edit[/Add] attachments (file[=input-charset[#output-charset]])\n"
2506 "~c <users> Add users to Cc: list (`~b': to Bcc:)\n"
2507 "~d Read in $DEAD (dead.letter)\n"
2508 "~e Edit message via $EDITOR\n"
2509 "~F <msglist> Read in with headers, do not *indentprefix* lines\n"
2510 ), n_stdout);
2511 fputs(_(
2512 "~f <msglist> Like ~F, but honour `ignore' / `retain' configuration\n"
2513 "~H Edit From:, Reply-To: and Sender:\n"
2514 "~h Prompt for Subject:, To:, Cc: and \"blind\" Bcc:\n"
2515 "~i <variable> Insert a value and a newline\n"
2516 "~M <msglist> Read in with headers, *indentprefix* (`~m': `retain' etc.)\n"
2517 "~p Show current message compose buffer\n"
2518 "~r <file> Insert <file> (`~R': likewise, but *indentprefix* lines)\n"
2519 " <file> may also be <- [HERE-DELIMITER]>\n"
2520 ), n_stdout);
2521 fputs(_(
2522 "~s <subject> Set Subject:\n"
2523 "~t <users> Add users to To: list\n"
2524 "~u <msglist> Read in message(s) without headers (`~U': indent lines)\n"
2525 "~v Edit message via $VISUAL\n"
2526 "~w <file> Write message onto file\n"
2527 "~x Abort composition, discard message (`~q': save in $DEAD)\n"
2528 "~| <command> Pipe message through shell filter\n"
2529 ), n_stdout);
2530 if(cnt != 0)
2531 goto jearg;
2532 n_pstate_err_no = n_ERR_NONE;
2533 n_pstate_ex_no = 0;
2534 break;
2537 /* Finally place an entry in history as applicable */
2538 if(0){
2539 jhistcont:
2540 c = '\1';
2541 }else
2542 c = '\0';
2543 if(hist & a_HIST_ADD)
2544 n_tty_addhist(n_string_cp(sp), ((hist & a_HIST_GABBY) != 0));
2545 if(c != '\0')
2546 goto jcont;
2549 jout:
2550 /* Do we have *on-compose-splice-shell*, or *on-compose-splice*?
2551 * TODO Usual f...ed up state of signals and terminal etc. */
2552 if(coap == NULL && (cp = ok_vlook(on_compose_splice_shell)) != NULL) Jocs:{
2553 union {int (*ptf)(void); char const *sh;} u;
2554 char const *cmd;
2556 /* Reset *escape* and more to their defaults. On change update manual! */
2557 if(ifs_saved == NULL)
2558 ifs_saved = savestr(ok_vlook(ifs));
2559 escape = n_ESCAPE[0];
2560 ok_vclear(ifs);
2562 if(coapm != NULL){
2563 /* XXX Due Popen() fflush(NULL) in PTF mode, ensure nothing to flush */
2564 /*if(!n_real_seek(_coll_fp, 0, SEEK_END))
2565 * goto jerr;*/
2566 u.ptf = &a_coll_ocs__mac;
2567 cmd = (char*)-1;
2568 a_coll_ocs__macname = cp = coapm;
2569 }else{
2570 u.sh = ok_vlook(SHELL);
2571 cmd = cp;
2574 i = strlen(cp) +1;
2575 coap = n_lofi_alloc(n_VSTRUCT_SIZEOF(struct a_coll_ocs_arg, coa_cmd
2576 ) + i);
2577 coap->coa_pipe[0] = coap->coa_pipe[1] = -1;
2578 coap->coa_stdin = coap->coa_stdout = NULL;
2579 coap->coa_senderr = checkaddr_err;
2580 memcpy(coap->coa_cmd, cp, i);
2582 hold_all_sigs();
2583 coap->coa_opipe = safe_signal(SIGPIPE, SIG_IGN);
2584 coap->coa_oint = safe_signal(SIGINT, SIG_IGN);
2585 rele_all_sigs();
2587 if(pipe_cloexec(coap->coa_pipe) != -1 &&
2588 (coap->coa_stdin = Fdopen(coap->coa_pipe[0], "r", FAL0)) != NULL &&
2589 (coap->coa_stdout = Popen(cmd, "W", u.sh, NULL, coap->coa_pipe[1])
2590 ) != NULL){
2591 close(coap->coa_pipe[1]);
2592 coap->coa_pipe[1] = -1;
2594 temporary_compose_mode_hook_call(NULL, NULL, NULL);
2595 n_go_splice_hack(coap->coa_cmd, coap->coa_stdin, coap->coa_stdout,
2596 (n_psonce & ~(n_PSO_INTERACTIVE | n_PSO_TTYIN | n_PSO_TTYOUT)),
2597 &a_coll_ocs__finalize, &coap);
2598 /* Hook version protocol for ~^: update manual upon change! */
2599 fputs(a_COLL_PLUMBING_VERSION "\n", coap->coa_stdout);
2600 #undef a_COLL_PLUMBING_VERSION
2601 goto jcont;
2604 c = n_err_no;
2605 a_coll_ocs__finalize(coap);
2606 n_perr(_("Cannot invoke *on-compose-splice(-shell)?*"), c);
2607 goto jerr;
2609 if(*checkaddr_err != 0){
2610 *checkaddr_err = 0;
2611 goto jerr;
2613 if(coapm == NULL && (coapm = ok_vlook(on_compose_splice)) != NULL)
2614 goto Jocs;
2615 if(coap != NULL){
2616 ok_vset(ifs, ifs_saved);
2617 ifs_saved = NULL;
2620 /* Final chance to edit headers, if not already above */
2621 if (ok_blook(bsdcompat) || ok_blook(askatend)) {
2622 if (hp->h_cc == NULL && ok_blook(askcc))
2623 grab_headers(n_GO_INPUT_CTX_COMPOSE, hp, GCC, 1);
2624 if (hp->h_bcc == NULL && ok_blook(askbcc))
2625 grab_headers(n_GO_INPUT_CTX_COMPOSE, hp, GBCC, 1);
2627 if (hp->h_attach == NULL && ok_blook(askattach))
2628 hp->h_attach = n_attachment_list_edit(hp->h_attach,
2629 n_GO_INPUT_CTX_COMPOSE);
2631 /* Execute compose-leave */
2632 if((cp = ok_vlook(on_compose_leave)) != NULL){
2633 setup_from_and_sender(hp);
2634 temporary_compose_mode_hook_call(cp, &n_temporary_compose_hook_varset,
2635 hp);
2638 /* Add automatic receivers */
2639 if ((cp = ok_vlook(autocc)) != NULL && *cp != '\0')
2640 hp->h_cc = cat(hp->h_cc, checkaddrs(lextract(cp, GCC | GFULL),
2641 EACM_NORMAL, checkaddr_err));
2642 if ((cp = ok_vlook(autobcc)) != NULL && *cp != '\0')
2643 hp->h_bcc = cat(hp->h_bcc, checkaddrs(lextract(cp, GBCC | GFULL),
2644 EACM_NORMAL, checkaddr_err));
2645 if (*checkaddr_err != 0)
2646 goto jerr;
2648 /* TODO Cannot do since it may require turning this into a multipart one */
2649 if(n_poption & n_PO_Mm_FLAG)
2650 goto jskiptails;
2652 /* Place signature? */
2653 if((cp = ok_vlook(signature)) != NULL && *cp != '\0'){
2654 char const *cpq;
2656 if((cpq = fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO)) == NULL){
2657 n_err(_("*signature* expands to invalid file: %s\n"),
2658 n_shexp_quote_cp(cp, FAL0));
2659 goto jerr;
2661 cpq = n_shexp_quote_cp(cp = cpq, FAL0);
2663 if((sigfp = Fopen(cp, "r")) == NULL){
2664 n_err(_("Can't open *signature* %s: %s\n"),
2665 cpq, n_err_to_doc(n_err_no));
2666 goto jerr;
2669 if(linebuf == NULL)
2670 linebuf = smalloc(linesize = LINESIZE);
2671 c = '\0';
2672 while((i = fread(linebuf, sizeof *linebuf, linesize, n_UNVOLATILE(sigfp)))
2673 > 0){
2674 c = linebuf[i - 1];
2675 if(i != fwrite(linebuf, sizeof *linebuf, i, _coll_fp))
2676 goto jerr;
2679 /* C99 */{
2680 FILE *x = n_UNVOLATILE(sigfp);
2681 int e = n_err_no, ise = ferror(x);
2683 sigfp = NULL;
2684 Fclose(x);
2686 if(ise){
2687 n_err(_("Errors while reading *signature* %s: %s\n"),
2688 cpq, n_err_to_doc(e));
2689 goto jerr;
2693 if(c != '\0' && c != '\n')
2694 putc('\n', _coll_fp);
2697 { char const *cp_obsolete = ok_vlook(NAIL_TAIL);
2699 if(cp_obsolete != NULL)
2700 n_OBSOLETE(_("please use *message-inject-tail*, not *NAIL_TAIL*"));
2702 if((cp = ok_vlook(message_inject_tail)) != NULL ||
2703 (cp = cp_obsolete) != NULL){
2704 if(putesc(cp, _coll_fp) < 0)
2705 goto jerr;
2706 if((n_psonce & n_PSO_INTERACTIVE) && putesc(cp, n_stdout) < 0)
2707 goto jerr;
2711 jskiptails:
2712 if(fflush(_coll_fp))
2713 goto jerr;
2714 rewind(_coll_fp);
2716 jleave:
2717 if (linebuf != NULL)
2718 free(linebuf);
2719 sigprocmask(SIG_BLOCK, &nset, NULL);
2720 n_pstate &= ~n_PS_COMPOSE_MODE;
2721 safe_signal(SIGINT, _coll_saveint);
2722 safe_signal(SIGHUP, _coll_savehup);
2723 sigprocmask(SIG_SETMASK, &oset, NULL);
2724 NYD_LEAVE;
2725 return _coll_fp;
2727 jerr:
2728 hold_all_sigs();
2730 if(coap != NULL && coap != (struct a_coll_ocs_arg*)-1){
2731 if(!(flags & a_COAP_NOSIGTERM))
2732 n_psignal(coap->coa_stdout, SIGTERM);
2733 n_go_splice_hack_remove_after_jump();
2734 coap = NULL;
2736 if(ifs_saved != NULL){
2737 ok_vset(ifs, ifs_saved);
2738 ifs_saved = NULL;
2740 if(sigfp != NULL){
2741 Fclose(n_UNVOLATILE(sigfp));
2742 sigfp = NULL;
2744 if(_coll_fp != NULL){
2745 Fclose(_coll_fp);
2746 _coll_fp = NULL;
2749 rele_all_sigs();
2751 assert(checkaddr_err != NULL);
2752 /* TODO We don't save in $DEAD upon error because msg not readily composed?
2753 * TODO But this no good, it should go ZOMBIE / DRAFT / POSTPONED or what! */
2754 if(*checkaddr_err != 0){
2755 if(*checkaddr_err == 111)
2756 n_err(_("Compose mode splice hook failure\n"));
2757 else
2758 n_err(_("Some addressees were classified as \"hard error\"\n"));
2759 }else if(_coll_hadintr == 0){
2760 *checkaddr_err = TRU1; /* TODO ugly: "sendout_error" now.. */
2761 n_err(_("Failed to prepare composed message (I/O error, disk full?)\n"));
2763 goto jleave;
2765 #undef a_HARDERR
2768 /* s-it-mode */