cc-test.sh: fix behave_lreply_futh_rth_etc quoting issue
[s-mailx.git] / collect.c
blobe326d836206fddb5057c0a9d90681267ca7aa83a
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 bool_t a_coll_putesc(char const *s, bool_t addnl, FILE *stream);
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) && !a_coll_putesc(cp, TRU1, fp))
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_reply_to;
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 */
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 - 1;
868 hfp->hf_bl = bl - 1;
869 memcpy(&hfp->hf_dat[0], cp, nl);
870 memcpy(&hfp->hf_dat[nl], cmd[3], bl);
871 fprintf(n_stdout, "210 %s %" PRIuZ "\n", &hfp->hf_dat[0], ++i);
873 goto jleave;
876 if(is_asccaseprefix(cmd[1], "list")){
877 jdefault:
878 if(cmd[2] == NULL){
879 fputs("210", n_stdout);
880 if(hp->h_subject != NULL) fputs(" Subject", n_stdout);
881 if(hp->h_from != NULL) fputs(" From", n_stdout);
882 if(hp->h_sender != NULL) fputs(" Sender", n_stdout);
883 if(hp->h_to != NULL) fputs(" To", n_stdout);
884 if(hp->h_cc != NULL) fputs(" Cc", n_stdout);
885 if(hp->h_bcc != NULL) fputs(" Bcc", n_stdout);
886 if(hp->h_reply_to != NULL) fputs(" Reply-To", n_stdout);
887 if(hp->h_mft != NULL) fputs(" Mail-Followup-To", n_stdout);
888 if(hp->h_message_id != NULL) fputs(" Message-ID", n_stdout);
889 if(hp->h_ref != NULL) fputs(" References", n_stdout);
890 if(hp->h_in_reply_to != NULL) fputs(" In-Reply-To", n_stdout);
891 if(hp->h_mailx_command != NULL) fputs(" Mailx-Command", n_stdout);
892 if(hp->h_mailx_raw_to != NULL) fputs(" Mailx-Raw-To", n_stdout);
893 if(hp->h_mailx_raw_cc != NULL) fputs(" Mailx-Raw-Cc", n_stdout);
894 if(hp->h_mailx_raw_bcc != NULL) fputs(" Mailx-Raw-Bcc", n_stdout);
895 if(hp->h_mailx_orig_from != NULL) fputs(" Mailx-Orig-From", n_stdout);
896 if(hp->h_mailx_orig_to != NULL) fputs(" Mailx-Orig-To", n_stdout);
897 if(hp->h_mailx_orig_cc != NULL) fputs(" Mailx-Orig-Cc", n_stdout);
898 if(hp->h_mailx_orig_bcc != NULL) fputs(" Mailx-Orig-Bcc", n_stdout);
900 /* Print only one instance of each free-form header */
901 for(hfp = hp->h_user_headers; hfp != NULL; hfp = hfp->hf_next){
902 struct n_header_field *hfpx;
904 for(hfpx = hp->h_user_headers;; hfpx = hfpx->hf_next)
905 if(hfpx == hfp){
906 putc(' ', n_stdout);
907 fputs(&hfp->hf_dat[0], n_stdout);
908 break;
909 }else if(!asccasecmp(&hfpx->hf_dat[0], &hfp->hf_dat[0]))
910 break;
912 putc('\n', n_stdout);
913 goto jleave;
916 if(cmd[3] != NULL)
917 goto jecmd;
919 if(!asccasecmp(cmd[2], cp = "Subject")){
920 np = (hp->h_subject != NULL) ? (struct name*)-1 : NULL;
921 goto jlist;
923 if(!asccasecmp(cmd[2], cp = "From")){
924 np = hp->h_from;
925 jlist:
926 fprintf(n_stdout, "%s %s\n", (np == NULL ? "501" : "210"), cp);
927 goto jleave;
929 if(!asccasecmp(cmd[2], cp = "Sender")){
930 np = hp->h_sender;
931 goto jlist;
933 if(!asccasecmp(cmd[2], cp = "To")){
934 np = hp->h_to;
935 goto jlist;
937 if(!asccasecmp(cmd[2], cp = "Cc")){
938 np = hp->h_cc;
939 goto jlist;
941 if(!asccasecmp(cmd[2], cp = "Bcc")){
942 np = hp->h_bcc;
943 goto jlist;
945 if(!asccasecmp(cmd[2], cp = "Reply-To")){
946 np = hp->h_reply_to;
947 goto jlist;
949 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
950 np = hp->h_mft;
951 goto jlist;
953 if(!asccasecmp(cmd[2], cp = "Message-ID")){
954 np = hp->h_message_id;
955 goto jlist;
957 if(!asccasecmp(cmd[2], cp = "References")){
958 np = hp->h_ref;
959 goto jlist;
961 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
962 np = hp->h_in_reply_to;
963 goto jlist;
966 if(!asccasecmp(cmd[2], cp = "Mailx-Command")){
967 np = (hp->h_mailx_command != NULL) ? (struct name*)-1 : NULL;
968 goto jlist;
970 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-To")){
971 np = hp->h_mailx_raw_to;
972 goto jlist;
974 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-Cc")){
975 np = hp->h_mailx_raw_cc;
976 goto jlist;
978 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-Bcc")){
979 np = hp->h_mailx_raw_bcc;
980 goto jlist;
982 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-From")){
983 np = hp->h_mailx_orig_from;
984 goto jlist;
986 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-To")){
987 np = hp->h_mailx_orig_to;
988 goto jlist;
990 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-Cc")){
991 np = hp->h_mailx_orig_cc;
992 goto jlist;
994 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-Bcc")){
995 np = hp->h_mailx_orig_bcc;
996 goto jlist;
999 /* Free-form header fields */
1000 for(cp = cmd[2]; *cp != '\0'; ++cp)
1001 if(!fieldnamechar(*cp)){
1002 cp = cmd[2];
1003 goto j501cp;
1005 cp = cmd[2];
1006 for(hfp = hp->h_user_headers;; hfp = hfp->hf_next){
1007 if(hfp == NULL)
1008 goto j501cp;
1009 else if(!asccasecmp(cp, &hfp->hf_dat[0])){
1010 fprintf(n_stdout, "210 %s\n", &hfp->hf_dat[0]);
1011 break;
1014 goto jleave;
1017 if(is_asccaseprefix(cmd[1], "remove")){
1018 if(cmd[2] == NULL || cmd[3] != NULL)
1019 goto jecmd;
1021 if(!asccasecmp(cmd[2], cp = "Subject")){
1022 if(hp->h_subject != NULL){
1023 hp->h_subject = NULL;
1024 fprintf(n_stdout, "210 %s\n", cp);
1025 goto jleave;
1026 }else
1027 goto j501cp;
1030 if(!asccasecmp(cmd[2], cp = "From")){
1031 npp = &hp->h_from;
1032 jrem:
1033 if(*npp != NULL){
1034 *npp = NULL;
1035 fprintf(n_stdout, "210 %s\n", cp);
1036 goto jleave;
1037 }else
1038 goto j501cp;
1040 if(!asccasecmp(cmd[2], cp = "Sender")){
1041 npp = &hp->h_sender;
1042 goto jrem;
1044 if(!asccasecmp(cmd[2], cp = "To")){
1045 npp = &hp->h_to;
1046 goto jrem;
1048 if(!asccasecmp(cmd[2], cp = "Cc")){
1049 npp = &hp->h_cc;
1050 goto jrem;
1052 if(!asccasecmp(cmd[2], cp = "Bcc")){
1053 npp = &hp->h_bcc;
1054 goto jrem;
1056 if(!asccasecmp(cmd[2], cp = "Reply-To")){
1057 npp = &hp->h_reply_to;
1058 goto jrem;
1060 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
1061 npp = &hp->h_mft;
1062 goto jrem;
1064 if(!asccasecmp(cmd[2], cp = "Message-ID")){
1065 npp = &hp->h_message_id;
1066 goto jrem;
1068 if(!asccasecmp(cmd[2], cp = "References")){
1069 npp = &hp->h_ref;
1070 goto jrem;
1072 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
1073 npp = &hp->h_in_reply_to;
1074 goto jrem;
1077 if(!asccasecmp(cmd[2], cp = "Mailx-Command") ||
1078 !asccasecmp(cmd[2], cp = "Mailx-Raw-To") ||
1079 !asccasecmp(cmd[2], cp = "Mailx-Raw-Cc") ||
1080 !asccasecmp(cmd[2], cp = "Mailx-Raw-Bcc") ||
1081 !asccasecmp(cmd[2], cp = "Mailx-Orig-From") ||
1082 !asccasecmp(cmd[2], cp = "Mailx-Orig-To") ||
1083 !asccasecmp(cmd[2], cp = "Mailx-Orig-Cc") ||
1084 !asccasecmp(cmd[2], cp = "Mailx-Orig-Bcc")){
1085 fprintf(n_stdout, "505 %s\n", cp);
1086 goto jleave;
1089 /* Free-form header fields (note j501cp may print non-normalized name) */
1090 /* C99 */{
1091 struct n_header_field **hfpp;
1092 bool_t any;
1094 for(cp = cmd[2]; *cp != '\0'; ++cp)
1095 if(!fieldnamechar(*cp)){
1096 cp = cmd[2];
1097 goto j501cp;
1099 cp = cmd[2];
1101 for(any = FAL0, hfpp = &hp->h_user_headers; (hfp = *hfpp) != NULL;){
1102 if(!asccasecmp(cp, &hfp->hf_dat[0])){
1103 *hfpp = hfp->hf_next;
1104 if(!any)
1105 fprintf(n_stdout, "210 %s\n", &hfp->hf_dat[0]);
1106 any = TRU1;
1107 }else
1108 hfpp = &hfp->hf_next;
1110 if(!any)
1111 goto j501cp;
1113 goto jleave;
1116 if(is_asccaseprefix(cmd[1], "remove-at")){
1117 if(cmd[2] == NULL || cmd[3] == NULL)
1118 goto jecmd;
1120 if((n_idec_uiz_cp(&i, cmd[3], 0, NULL
1121 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
1122 ) != n_IDEC_STATE_CONSUMED || i == 0){
1123 fputs("505\n", n_stdout);
1124 goto jleave;
1127 if(!asccasecmp(cmd[2], cp = "Subject")){
1128 if(hp->h_subject != NULL && i == 1){
1129 hp->h_subject = NULL;
1130 fprintf(n_stdout, "210 %s 1\n", cp);
1131 goto jleave;
1132 }else
1133 goto j501cp;
1136 if(!asccasecmp(cmd[2], cp = "From")){
1137 npp = &hp->h_from;
1138 jremat:
1139 if((np = *npp) == NULL)
1140 goto j501cp;
1141 while(--i != 0 && np != NULL)
1142 np = np->n_flink;
1143 if(np == NULL)
1144 goto j501cp;
1146 if(np->n_blink != NULL)
1147 np->n_blink->n_flink = np->n_flink;
1148 else
1149 *npp = np->n_flink;
1150 if(np->n_flink != NULL)
1151 np->n_flink->n_blink = np->n_blink;
1153 fprintf(n_stdout, "210 %s\n", cp);
1154 goto jleave;
1156 if(!asccasecmp(cmd[2], cp = "Sender")){
1157 npp = &hp->h_sender;
1158 goto jremat;
1160 if(!asccasecmp(cmd[2], cp = "To")){
1161 npp = &hp->h_to;
1162 goto jremat;
1164 if(!asccasecmp(cmd[2], cp = "Cc")){
1165 npp = &hp->h_cc;
1166 goto jremat;
1168 if(!asccasecmp(cmd[2], cp = "Bcc")){
1169 npp = &hp->h_bcc;
1170 goto jremat;
1172 if(!asccasecmp(cmd[2], cp = "Reply-To")){
1173 npp = &hp->h_reply_to;
1174 goto jremat;
1176 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
1177 npp = &hp->h_mft;
1178 goto jremat;
1180 if(!asccasecmp(cmd[2], cp = "Message-ID")){
1181 npp = &hp->h_message_id;
1182 goto jremat;
1184 if(!asccasecmp(cmd[2], cp = "References")){
1185 npp = &hp->h_ref;
1186 goto jremat;
1188 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
1189 npp = &hp->h_in_reply_to;
1190 goto jremat;
1193 if(!asccasecmp(cmd[2], cp = "Mailx-Command") ||
1194 !asccasecmp(cmd[2], cp = "Mailx-Raw-To") ||
1195 !asccasecmp(cmd[2], cp = "Mailx-Raw-Cc") ||
1196 !asccasecmp(cmd[2], cp = "Mailx-Raw-Bcc") ||
1197 !asccasecmp(cmd[2], cp = "Mailx-Orig-From") ||
1198 !asccasecmp(cmd[2], cp = "Mailx-Orig-To") ||
1199 !asccasecmp(cmd[2], cp = "Mailx-Orig-Cc") ||
1200 !asccasecmp(cmd[2], cp = "Mailx-Orig-Bcc")){
1201 fprintf(n_stdout, "505 %s\n", cp);
1202 goto jleave;
1205 /* Free-form header fields */
1206 /* C99 */{
1207 struct n_header_field **hfpp;
1209 for(cp = cmd[2]; *cp != '\0'; ++cp)
1210 if(!fieldnamechar(*cp)){
1211 cp = cmd[2];
1212 goto j501cp;
1214 cp = cmd[2];
1216 for(hfpp = &hp->h_user_headers; (hfp = *hfpp) != NULL;){
1217 if(--i == 0){
1218 *hfpp = hfp->hf_next;
1219 fprintf(n_stdout, "210 %s %" PRIuZ "\n", &hfp->hf_dat[0], i);
1220 break;
1221 }else
1222 hfpp = &hfp->hf_next;
1224 if(hfp == NULL)
1225 goto j501cp;
1227 goto jleave;
1230 if(is_asccaseprefix(cmd[1], "show")){
1231 if(cmd[2] == NULL || cmd[3] != NULL)
1232 goto jecmd;
1234 if(!asccasecmp(cmd[2], cp = "Subject")){
1235 if(hp->h_subject == NULL)
1236 goto j501cp;
1237 fprintf(n_stdout, "212 %s\n%s\n\n", cp, hp->h_subject);
1238 goto jleave;
1241 if(!asccasecmp(cmd[2], cp = "From")){
1242 np = hp->h_from;
1243 jshow:
1244 if(np != NULL){
1245 fprintf(n_stdout, "211 %s\n", cp);
1246 do if(!(np->n_type & GDEL))
1247 fprintf(n_stdout, "%s %s\n", np->n_name, np->n_fullname);
1248 while((np = np->n_flink) != NULL);
1249 putc('\n', n_stdout);
1250 goto jleave;
1251 }else
1252 goto j501cp;
1254 if(!asccasecmp(cmd[2], cp = "Sender")){
1255 np = hp->h_sender;
1256 goto jshow;
1258 if(!asccasecmp(cmd[2], cp = "To")){
1259 np = hp->h_to;
1260 goto jshow;
1262 if(!asccasecmp(cmd[2], cp = "Cc")){
1263 np = hp->h_cc;
1264 goto jshow;
1266 if(!asccasecmp(cmd[2], cp = "Bcc")){
1267 np = hp->h_bcc;
1268 goto jshow;
1270 if(!asccasecmp(cmd[2], cp = "Reply-To")){
1271 np = hp->h_reply_to;
1272 goto jshow;
1274 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
1275 np = hp->h_mft;
1276 goto jshow;
1278 if(!asccasecmp(cmd[2], cp = "Message-ID")){
1279 np = hp->h_message_id;
1280 goto jshow;
1282 if(!asccasecmp(cmd[2], cp = "References")){
1283 np = hp->h_ref;
1284 goto jshow;
1286 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
1287 np = hp->h_in_reply_to;
1288 goto jshow;
1291 if(!asccasecmp(cmd[2], cp = "Mailx-Command")){
1292 if(hp->h_mailx_command == NULL)
1293 goto j501cp;
1294 fprintf(n_stdout, "212 %s\n%s\n\n", cp, hp->h_mailx_command);
1295 goto jleave;
1297 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-To")){
1298 np = hp->h_mailx_raw_to;
1299 goto jshow;
1301 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-Cc")){
1302 np = hp->h_mailx_raw_cc;
1303 goto jshow;
1305 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-Bcc")){
1306 np = hp->h_mailx_raw_bcc;
1307 goto jshow;
1309 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-From")){
1310 np = hp->h_mailx_orig_from;
1311 goto jshow;
1313 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-To")){
1314 np = hp->h_mailx_orig_to;
1315 goto jshow;
1317 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-Cc")){
1318 np = hp->h_mailx_orig_cc;
1319 goto jshow;
1321 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-Bcc")){
1322 np = hp->h_mailx_orig_bcc;
1323 goto jshow;
1326 /* Free-form header fields */
1327 /* C99 */{
1328 bool_t any;
1330 for(cp = cmd[2]; *cp != '\0'; ++cp)
1331 if(!fieldnamechar(*cp)){
1332 cp = cmd[2];
1333 goto j501cp;
1335 cp = cmd[2];
1337 for(any = FAL0, hfp = hp->h_user_headers; hfp != NULL;
1338 hfp = hfp->hf_next){
1339 if(!asccasecmp(cp, &hfp->hf_dat[0])){
1340 if(!any)
1341 fprintf(n_stdout, "212 %s\n", &hfp->hf_dat[0]);
1342 any = TRU1;
1343 fprintf(n_stdout, "%s\n", &hfp->hf_dat[hfp->hf_nl +1]);
1346 if(any)
1347 putc('\n', n_stdout);
1348 else
1349 goto j501cp;
1351 goto jleave;
1354 jecmd:
1355 fputs("500\n", n_stdout);
1356 cp = NULL;
1357 jleave:
1358 NYD2_LEAVE;
1359 return (cp != NULL);
1361 j501cp:
1362 fputs("501 ", n_stdout);
1363 fputs(cp, n_stdout);
1364 putc('\n', n_stdout);
1365 goto jleave;
1368 static bool_t
1369 a_collect__plumb_attach(char const *cp, struct header *hp,
1370 char const *cmd[4]){
1371 bool_t status;
1372 struct attachment *ap;
1373 NYD2_ENTER;
1375 if(cmd[1] == NULL)
1376 goto jdefault;
1378 if(is_asccaseprefix(cmd[1], "attribute")){
1379 if(cmd[2] == NULL || cmd[3] != NULL)
1380 goto jecmd;
1382 if((ap = n_attachment_find(hp->h_attach, cmd[2], NULL)) != NULL){
1383 jatt_att:
1384 fprintf(n_stdout, "212 %s\n", cmd[2]);
1385 if(ap->a_msgno > 0)
1386 fprintf(n_stdout, "message-number %d\n\n", ap->a_msgno);
1387 else{
1388 fprintf(n_stdout,
1389 "creation-name %s\nopen-path %s\nfilename %s\n",
1390 ap->a_path_user, ap->a_path, ap->a_name);
1391 if(ap->a_content_description != NULL)
1392 fprintf(n_stdout, "content-description %s\n",
1393 ap->a_content_description);
1394 if(ap->a_content_id != NULL)
1395 fprintf(n_stdout, "content-id %s\n",
1396 ap->a_content_id->n_name);
1397 if(ap->a_content_type != NULL)
1398 fprintf(n_stdout, "content-type %s\n", ap->a_content_type);
1399 if(ap->a_content_disposition != NULL)
1400 fprintf(n_stdout, "content-disposition %s\n",
1401 ap->a_content_disposition);
1402 putc('\n', n_stdout);
1404 }else
1405 fputs("501\n", n_stdout);
1406 goto jleave;
1409 if(is_asccaseprefix(cmd[1], "attribute-at")){
1410 uiz_t i;
1412 if(cmd[2] == NULL || cmd[3] != NULL)
1413 goto jecmd;
1415 if((n_idec_uiz_cp(&i, cmd[2], 0, NULL
1416 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
1417 ) != n_IDEC_STATE_CONSUMED || i == 0)
1418 fputs("505\n", n_stdout);
1419 else{
1420 for(ap = hp->h_attach; ap != NULL && --i != 0; ap = ap->a_flink)
1422 if(ap != NULL)
1423 goto jatt_att;
1424 else
1425 fputs("501\n", n_stdout);
1427 goto jleave;
1430 if(is_asccaseprefix(cmd[1], "attribute-set")){
1431 if(cmd[2] == NULL || cmd[3] == NULL)
1432 goto jecmd;
1434 if((ap = n_attachment_find(hp->h_attach, cmd[2], NULL)) != NULL){
1435 jatt_attset:
1436 if(ap->a_msgno > 0)
1437 fputs("505\n", n_stdout);
1438 else{
1439 char c, *keyw;
1441 cp = cmd[3];
1442 while((c = *cp) != '\0' && !blankchar(c))
1443 ++cp;
1444 keyw = savestrbuf(cmd[3], PTR2SIZE(cp - cmd[3]));
1445 if(c != '\0'){
1446 for(; (c = *++cp) != '\0' && blankchar(c);)
1448 if(c != '\0'){
1449 char *xp;
1451 /* Strip [\r\n] which would render a parameter invalid XXX
1452 * XXX all controls? */
1453 cp = xp = savestr(cp);
1454 for(; (c = *xp) != '\0'; ++xp)
1455 if(c == '\n' || c == '\r')
1456 *xp = ' ';
1457 c = *cp;
1461 if(!asccasecmp(keyw, "filename"))
1462 ap->a_name = (c == '\0') ? ap->a_path_bname : cp;
1463 else if(!asccasecmp(keyw, "content-description"))
1464 ap->a_content_description = (c == '\0') ? NULL : cp;
1465 else if(!asccasecmp(keyw, "content-id")){
1466 ap->a_content_id = NULL;
1468 if(c != '\0'){
1469 struct name *np;
1471 np = checkaddrs(lextract(cp, GREF),
1472 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG |
1473 EACM_NONAME, NULL);
1474 if(np != NULL && np->n_flink == NULL)
1475 ap->a_content_id = np;
1476 else
1477 cp = NULL;
1479 }else if(!asccasecmp(keyw, "content-type"))
1480 ap->a_content_type = (c == '\0') ? NULL : cp;
1481 else if(!asccasecmp(keyw, "content-disposition"))
1482 ap->a_content_disposition = (c == '\0') ? NULL : cp;
1483 else
1484 cp = NULL;
1486 if(cp != NULL){
1487 size_t i;
1489 for(i = 0; ap != NULL; ++i, ap = ap->a_blink)
1491 fprintf(n_stdout, "210 %" PRIuZ "\n", i);
1492 }else
1493 fputs("505\n", n_stdout);
1495 }else
1496 fputs("501\n", n_stdout);
1497 goto jleave;
1500 if(is_asccaseprefix(cmd[1], "attribute-set-at")){
1501 uiz_t i;
1503 if(cmd[2] == NULL || cmd[3] == NULL)
1504 goto jecmd;
1506 if((n_idec_uiz_cp(&i, cmd[2], 0, NULL
1507 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
1508 ) != n_IDEC_STATE_CONSUMED || i == 0)
1509 fputs("505\n", n_stdout);
1510 else{
1511 for(ap = hp->h_attach; ap != NULL && --i != 0; ap = ap->a_flink)
1513 if(ap != NULL)
1514 goto jatt_attset;
1515 else
1516 fputs("501\n", n_stdout);
1518 goto jleave;
1521 if(is_asccaseprefix(cmd[1], "insert")){
1522 enum n_attach_error aerr;
1524 if(cmd[2] == NULL || cmd[3] != NULL)
1525 goto jecmd;
1527 hp->h_attach = n_attachment_append(hp->h_attach, cmd[2], &aerr, &ap);
1528 switch(aerr){
1529 case n_ATTACH_ERR_FILE_OPEN: cp = "505\n"; goto jatt_ins;
1530 case n_ATTACH_ERR_ICONV_FAILED: cp = "506\n"; goto jatt_ins;
1531 case n_ATTACH_ERR_ICONV_NAVAIL:
1532 case n_ATTACH_ERR_OTHER:
1533 default:
1534 cp = "501\n";
1535 jatt_ins:
1536 fputs(cp, n_stdout);
1537 break;
1538 case n_ATTACH_ERR_NONE:{
1539 size_t i;
1541 for(i = 0; ap != NULL; ++i, ap = ap->a_blink)
1543 fprintf(n_stdout, "210 %" PRIuZ "\n", i);
1544 } break;
1546 goto jleave;
1549 if(is_asccaseprefix(cmd[1], "list")){
1550 jdefault:
1551 if(cmd[2] != NULL)
1552 goto jecmd;
1554 if((ap = hp->h_attach) != NULL){
1555 fputs("212\n", n_stdout);
1557 fprintf(n_stdout, "%s\n", ap->a_path_user);
1558 while((ap = ap->a_flink) != NULL);
1559 putc('\n', n_stdout);
1560 }else
1561 fputs("501\n", n_stdout);
1562 goto jleave;
1565 if(is_asccaseprefix(cmd[1], "remove")){
1566 if(cmd[2] == NULL || cmd[3] != NULL)
1567 goto jecmd;
1569 if((ap = n_attachment_find(hp->h_attach, cmd[2], &status)) != NULL){
1570 if(status == TRUM1)
1571 fputs("506\n", n_stdout);
1572 else{
1573 hp->h_attach = n_attachment_remove(hp->h_attach, ap);
1574 fprintf(n_stdout, "210 %s\n", cmd[2]);
1576 }else
1577 fputs("501\n", n_stdout);
1578 goto jleave;
1581 if(is_asccaseprefix(cmd[1], "remove-at")){
1582 uiz_t i;
1584 if(cmd[2] == NULL || cmd[3] != NULL)
1585 goto jecmd;
1587 if((n_idec_uiz_cp(&i, cmd[2], 0, NULL
1588 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
1589 ) != n_IDEC_STATE_CONSUMED || i == 0)
1590 fputs("505\n", n_stdout);
1591 else{
1592 for(ap = hp->h_attach; ap != NULL && --i != 0; ap = ap->a_flink)
1594 if(ap != NULL){
1595 hp->h_attach = n_attachment_remove(hp->h_attach, ap);
1596 fprintf(n_stdout, "210 %s\n", cmd[2]);
1597 }else
1598 fputs("501\n", n_stdout);
1600 goto jleave;
1603 jecmd:
1604 fputs("500\n", n_stdout);
1605 cp = NULL;
1606 jleave:
1607 NYD2_LEAVE;
1608 return (cp != NULL);
1611 static void
1612 _collint(int s)
1614 NYD_X; /* Signal handler */
1616 /* the control flow is subtle, because we can be called from ~q */
1617 if (_coll_hadintr == 0) {
1618 if (ok_blook(ignore)) {
1619 fputs("@\n", n_stdout);
1620 fflush(n_stdout);
1621 clearerr(n_stdin);
1622 } else
1623 _coll_hadintr = 1;
1624 siglongjmp(_coll_jmp, 1);
1626 n_exit_status |= n_EXIT_SEND_ERROR;
1627 if (s != 0)
1628 savedeadletter(_coll_fp, TRU1);
1629 /* Aborting message, no need to fflush() .. */
1630 siglongjmp(_coll_abort, 1);
1633 static void
1634 collhup(int s)
1636 NYD_X; /* Signal handler */
1637 n_UNUSED(s);
1639 savedeadletter(_coll_fp, TRU1);
1640 /* Let's pretend nobody else wants to clean up, a true statement at
1641 * this time */
1642 exit(n_EXIT_ERR);
1645 static bool_t
1646 a_coll_putesc(char const *s, bool_t addnl, FILE *stream) /* TODO: v15: drop */
1648 bool_t rv;
1649 NYD_ENTER;
1651 rv = FAL0;
1653 while (s[0] != '\0') { /* TODO v15: user resp upon `set' time! */
1654 if (s[0] == '\\') {
1655 if (s[1] == 't') {
1656 if (putc('\t', stream) == EOF)
1657 goto jleave;
1658 s += 2;
1659 continue;
1661 if (s[1] == 'n') {
1662 if (putc('\n', stream) == EOF)
1663 goto jleave;
1664 s += 2;
1665 continue;
1668 if (putc(s[0], stream) == EOF)
1669 goto jleave;
1670 ++s;
1673 if(addnl){
1674 if(putc('\n', stream) == EOF)
1675 goto jleave;
1678 rv = TRU1;
1679 jleave:
1680 NYD_LEAVE;
1681 return rv;
1684 static int
1685 a_coll_ocs__mac(void){
1686 /* Executes in a fork(2)ed child TODO if remains, global MASKs for those! */
1687 setvbuf(n_stdin, NULL, _IOLBF, 0);
1688 setvbuf(n_stdout, NULL, _IOLBF, 0);
1689 n_psonce &= ~(n_PSO_INTERACTIVE | n_PSO_TTYIN | n_PSO_TTYOUT);
1690 n_pstate |= n_PS_COMPOSE_FORKHOOK;
1691 n_readctl_overlay = NULL; /* TODO we need OnForkEvent! See c_readctl() */
1692 if(n_poption & n_PO_D_VV){
1693 char buf[128];
1695 snprintf(buf, sizeof buf, "[%d]%s", getpid(), ok_vlook(log_prefix));
1696 ok_vset(log_prefix, buf);
1698 /* TODO If that uses `!' it will effectively SIG_IGN SIGINT, ...and such */
1699 temporary_compose_mode_hook_call(a_coll_ocs__macname, NULL, NULL);
1700 return 0;
1703 static void
1704 a_coll_ocs__finalize(void *vp){
1705 /* Note we use this for destruction upon setup errors, thus */
1706 sighandler_type opipe;
1707 sighandler_type oint;
1708 struct a_coll_ocs_arg **coapp, *coap;
1709 NYD2_ENTER;
1711 temporary_compose_mode_hook_call((char*)-1, NULL, NULL);
1713 coap = *(coapp = vp);
1714 *coapp = (struct a_coll_ocs_arg*)-1;
1716 if(coap->coa_stdin != NULL)
1717 Fclose(coap->coa_stdin);
1718 else if(coap->coa_pipe[0] != -1)
1719 close(coap->coa_pipe[0]);
1721 if(coap->coa_stdout != NULL && !Pclose(coap->coa_stdout, TRU1))
1722 *coap->coa_senderr = 111;
1723 if(coap->coa_pipe[1] != -1)
1724 close(coap->coa_pipe[1]);
1726 opipe = coap->coa_opipe;
1727 oint = coap->coa_oint;
1729 n_lofi_free(coap);
1731 hold_all_sigs();
1732 safe_signal(SIGPIPE, opipe);
1733 safe_signal(SIGINT, oint);
1734 rele_all_sigs();
1735 NYD2_LEAVE;
1738 FL void
1739 n_temporary_compose_hook_varset(void *arg){ /* TODO v15: drop */
1740 struct header *hp;
1741 char const *val;
1742 NYD2_ENTER;
1744 hp = arg;
1746 if((val = hp->h_subject) == NULL)
1747 val = n_empty;
1748 ok_vset(mailx_subject, val);
1749 if((val = detract(hp->h_from, GNAMEONLY)) == NULL)
1750 val = n_empty;
1751 ok_vset(mailx_from, val);
1752 if((val = detract(hp->h_sender, GNAMEONLY)) == NULL)
1753 val = n_empty;
1754 ok_vset(mailx_sender, val);
1755 if((val = detract(hp->h_to, GNAMEONLY)) == NULL)
1756 val = n_empty;
1757 ok_vset(mailx_to, val);
1758 if((val = detract(hp->h_cc, GNAMEONLY)) == NULL)
1759 val = n_empty;
1760 ok_vset(mailx_cc, val);
1761 if((val = detract(hp->h_bcc, GNAMEONLY)) == NULL)
1762 val = n_empty;
1763 ok_vset(mailx_bcc, val);
1765 if((val = hp->h_mailx_command) == NULL)
1766 val = n_empty;
1767 ok_vset(mailx_command, val);
1769 if((val = detract(hp->h_mailx_raw_to, GNAMEONLY)) == NULL)
1770 val = n_empty;
1771 ok_vset(mailx_raw_to, val);
1772 if((val = detract(hp->h_mailx_raw_cc, GNAMEONLY)) == NULL)
1773 val = n_empty;
1774 ok_vset(mailx_raw_cc, val);
1775 if((val = detract(hp->h_mailx_raw_bcc, GNAMEONLY)) == NULL)
1776 val = n_empty;
1777 ok_vset(mailx_raw_bcc, val);
1779 if((val = detract(hp->h_mailx_orig_from, GNAMEONLY)) == NULL)
1780 val = n_empty;
1781 ok_vset(mailx_orig_from, val);
1782 if((val = detract(hp->h_mailx_orig_to, GNAMEONLY)) == NULL)
1783 val = n_empty;
1784 ok_vset(mailx_orig_to, val);
1785 if((val = detract(hp->h_mailx_orig_cc, GNAMEONLY)) == NULL)
1786 val = n_empty;
1787 ok_vset(mailx_orig_cc, val);
1788 if((val = detract(hp->h_mailx_orig_bcc, GNAMEONLY)) == NULL)
1789 val = n_empty;
1790 ok_vset(mailx_orig_bcc, val);
1791 NYD2_LEAVE;
1794 FL FILE *
1795 collect(struct header *hp, int printheaders, struct message *mp,
1796 char const *quotefile, int doprefix, si8_t *checkaddr_err)
1798 struct n_string s, * volatile sp;
1799 struct n_ignore const *quoteitp;
1800 struct a_coll_ocs_arg *coap;
1801 int c;
1802 int volatile t, eofcnt, getfields;
1803 char volatile escape;
1804 enum{
1805 a_NONE,
1806 a_ERREXIT = 1u<<0,
1807 a_IGNERR = 1u<<1,
1808 a_COAP_NOSIGTERM = 1u<<8
1809 #define a_HARDERR() ((flags & (a_ERREXIT | a_IGNERR)) == a_ERREXIT)
1810 } volatile flags;
1811 char *linebuf;
1812 char const *cp, *cp_base, * volatile coapm, * volatile ifs_saved;
1813 size_t i, linesize; /* TODO line pool */
1814 long cnt;
1815 enum sendaction action;
1816 sigset_t oset, nset;
1817 FILE * volatile sigfp;
1818 NYD_ENTER;
1820 _coll_fp = NULL;
1821 sigfp = NULL;
1822 linesize = 0;
1823 linebuf = NULL;
1824 eofcnt = 0;
1825 ifs_saved = coapm = NULL;
1826 coap = NULL;
1827 sp = NULL;
1829 /* Start catching signals from here, but we're still die on interrupts
1830 * until we're in the main loop */
1831 sigfillset(&nset);
1832 sigprocmask(SIG_BLOCK, &nset, &oset);
1833 if ((_coll_saveint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
1834 safe_signal(SIGINT, &_collint);
1835 if ((_coll_savehup = safe_signal(SIGHUP, SIG_IGN)) != SIG_IGN)
1836 safe_signal(SIGHUP, collhup);
1837 if (sigsetjmp(_coll_abort, 1))
1838 goto jerr;
1839 if (sigsetjmp(_coll_jmp, 1))
1840 goto jerr;
1841 n_pstate |= n_PS_COMPOSE_MODE;
1842 sigprocmask(SIG_SETMASK, &oset, (sigset_t*)NULL);
1844 if ((_coll_fp = Ftmp(NULL, "collect", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
1845 NULL) {
1846 n_perr(_("temporary mail file"), 0);
1847 goto jerr;
1850 /* If we are going to prompt for a subject, refrain from printing a newline
1851 * after the headers (since some people mind) */
1852 getfields = 0;
1853 if(!(n_poption & n_PO_t_FLAG)){
1854 t = GTO | GSUBJECT | GCC | GNL;
1855 if(ok_blook(fullnames))
1856 t |= GCOMMA;
1858 if(n_psonce & n_PSO_INTERACTIVE){
1859 if(hp->h_subject == NULL && (ok_blook(ask) || ok_blook(asksub)))
1860 t &= ~GNL, getfields |= GSUBJECT;
1862 if(hp->h_to == NULL)
1863 t &= ~GNL, getfields |= GTO;
1865 if(!ok_blook(bsdcompat) && !ok_blook(askatend)){
1866 if(ok_blook(askbcc))
1867 t &= ~GNL, getfields |= GBCC;
1868 if(ok_blook(askcc))
1869 t &= ~GNL, getfields |= GCC;
1872 }else{
1873 n_UNINIT(t, 0);
1876 _coll_hadintr = 0;
1878 if (!sigsetjmp(_coll_jmp, 1)) {
1879 /* Ask for some headers first, as necessary */
1880 if (getfields)
1881 grab_headers(n_GO_INPUT_CTX_COMPOSE, hp, getfields, 1);
1883 /* Execute compose-enter; delayed for -t mode */
1884 if(!(n_poption & n_PO_t_FLAG) &&
1885 (cp = ok_vlook(on_compose_enter)) != NULL){
1886 setup_from_and_sender(hp);
1887 temporary_compose_mode_hook_call(cp, &n_temporary_compose_hook_varset,
1888 hp);
1891 /* TODO Mm: nope since it may require turning this into a multipart one */
1892 if(!(n_poption & (n_PO_Mm_FLAG | n_PO_t_FLAG))){
1893 if(!a_coll_message_inject_head(_coll_fp))
1894 goto jerr;
1896 /* Quote an original message */
1897 if (mp != NULL && (doprefix || (cp = ok_vlook(quote)) != NULL)) {
1898 quoteitp = n_IGNORE_ALL;
1899 action = SEND_QUOTE;
1900 if (doprefix) {
1901 char const *cp_v15compat;
1903 quoteitp = n_IGNORE_FWD;
1905 if((cp_v15compat = ok_vlook(fwdheading)) != NULL)
1906 n_OBSOLETE(_("please use *forward-inject-head*, "
1907 "not *fwdheading*"));
1908 cp = ok_vlook(forward_inject_head);
1909 if(cp == NULL && (cp = cp_v15compat) == NULL)
1910 cp = "-------- Original Message --------";
1911 if (*cp != '\0' && fprintf(_coll_fp, "%s\n", cp) < 0)
1912 goto jerr;
1913 } else if (!strcmp(cp, "noheading")) {
1914 /*EMPTY*/;
1915 } else if (!strcmp(cp, "headers")) {
1916 quoteitp = n_IGNORE_TYPE;
1917 } else if (!strcmp(cp, "allheaders")) {
1918 quoteitp = NULL;
1919 action = SEND_QUOTE_ALL;
1920 } else {
1921 cp = hfield1("from", mp);
1922 if (cp != NULL && (cnt = (long)strlen(cp)) > 0) {
1923 if (xmime_write(cp, cnt, _coll_fp, CONV_FROMHDR, TD_NONE,
1924 NULL, NULL) < 0)
1925 goto jerr;
1926 if (fprintf(_coll_fp, _(" wrote:\n\n")) < 0)
1927 goto jerr;
1930 if (fflush(_coll_fp))
1931 goto jerr;
1932 if (doprefix)
1933 cp = NULL;
1934 else if ((cp = ok_vlook(indentprefix)) == NULL)
1935 cp = INDENT_DEFAULT;
1936 if (sendmp(mp, _coll_fp, quoteitp, cp, action, NULL) < 0)
1937 goto jerr;
1941 if (quotefile != NULL) {
1942 if((n_pstate_err_no = a_coll_include_file(quotefile, FAL0, FAL0)
1943 ) != n_ERR_NONE)
1944 goto jerr;
1947 if(n_psonce & n_PSO_INTERACTIVE){
1948 if(!(n_pstate & n_PS_SOURCING)){
1949 sp = n_string_creat_auto(&s);
1950 sp = n_string_reserve(sp, 80);
1953 if(!(n_poption & n_PO_Mm_FLAG)){
1954 /* Print what we have sofar also on the terminal (if useful) */
1955 if (!ok_blook(editalong)) {
1956 if (printheaders)
1957 puthead(TRU1, hp, n_stdout, t,
1958 SEND_TODISP, CONV_NONE, NULL, NULL);
1960 rewind(_coll_fp);
1961 while ((c = getc(_coll_fp)) != EOF) /* XXX bytewise, yuck! */
1962 putc(c, n_stdout);
1963 if (fseek(_coll_fp, 0, SEEK_END))
1964 goto jerr;
1965 } else {
1966 rewind(_coll_fp);
1967 if(a_coll_edit('e', hp) != n_ERR_NONE)
1968 goto jerr;
1969 /* Print msg mandated by the Mail Reference Manual */
1970 jcont:
1971 if(n_psonce & n_PSO_INTERACTIVE)
1972 fputs(_("(continue)\n"), n_stdout);
1974 fflush(n_stdout);
1977 } else {
1978 /* Come here for printing the after-signal message. Duplicate messages
1979 * won't be printed because the write is aborted if we get a SIGTTOU */
1980 if (_coll_hadintr)
1981 n_err(_("\n(Interrupt -- one more to kill letter)\n"));
1984 /* If not under shell hook control */
1985 if(coap == NULL){
1986 /* We're done with -M or -m TODO because: we are too stupid yet, above */
1987 if(n_poption & n_PO_Mm_FLAG)
1988 goto jout;
1989 /* No command escapes, interrupts not expected. Simply copy STDIN */
1990 if(!(n_psonce & n_PSO_INTERACTIVE) &&
1991 !(n_poption & (n_PO_t_FLAG | n_PO_TILDE_FLAG))){
1992 linebuf = srealloc(linebuf, linesize = LINESIZE);
1993 while ((i = fread(linebuf, sizeof *linebuf, linesize, n_stdin)) > 0) {
1994 if (i != fwrite(linebuf, sizeof *linebuf, i, _coll_fp))
1995 goto jerr;
1997 goto jout;
2001 /* The interactive collect loop */
2002 if(coap == NULL)
2003 escape = *ok_vlook(escape);
2004 flags = ok_blook(errexit) ? a_ERREXIT : a_NONE;
2006 for(;;){
2007 enum {a_HIST_NONE, a_HIST_ADD = 1u<<0, a_HIST_GABBY = 1u<<1} hist;
2009 /* C99 */{
2010 enum n_go_input_flags gif;
2011 bool_t histadd;
2013 gif = n_GO_INPUT_CTX_COMPOSE;
2014 histadd = (sp != NULL);
2015 if((n_psonce & n_PSO_INTERACTIVE) || (n_poption & n_PO_TILDE_FLAG)){
2016 if(!(n_poption & n_PO_t_FLAG))
2017 gif |= n_GO_INPUT_NL_ESC;
2019 cnt = n_go_input(gif, n_empty, &linebuf, &linesize, NULL, &histadd);
2020 hist = histadd ? a_HIST_ADD | a_HIST_GABBY : a_HIST_NONE;
2023 if(cnt < 0){
2024 if(coap != NULL)
2025 break;
2026 if(n_poption & n_PO_t_FLAG){
2027 fflush_rewind(_coll_fp);
2028 /* It is important to set n_PSO_t_FLAG before extract_header()
2029 * *and* keep n_PO_t_FLAG for the first parse of the message!
2030 * TODO This is a hack, we need a clean approach for this */
2031 n_psonce |= n_PSO_t_FLAG;
2032 if(!a_coll_makeheader(_coll_fp, hp, checkaddr_err, TRU1))
2033 goto jerr;
2034 n_poption &= ~n_PO_t_FLAG;
2035 continue;
2036 }else if((n_psonce & n_PSO_INTERACTIVE) && ok_blook(ignoreeof) &&
2037 ++eofcnt < 4){
2038 fprintf(n_stdout,
2039 _("*ignoreeof* set, use `~.' to terminate letter\n"));
2040 n_go_input_clearerr();
2041 continue;
2043 break;
2046 _coll_hadintr = 0;
2048 cp = linebuf;
2049 if(cnt == 0)
2050 goto jputnl;
2051 else if(coap == NULL){
2052 if(!(n_psonce & n_PSO_INTERACTIVE) && !(n_poption & n_PO_TILDE_FLAG))
2053 goto jputline;
2054 else if(cp[0] == '.'){
2055 if(cnt == 1 && (ok_blook(dot) ||
2056 (ok_blook(posix) && ok_blook(ignoreeof))))
2057 break;
2060 if(cp[0] != escape){
2061 jputline:
2062 if(fwrite(cp, sizeof *cp, cnt, _coll_fp) != (size_t)cnt)
2063 goto jerr;
2064 /* TODO n_PS_READLINE_NL is a terrible hack to ensure that _in_all_-
2065 * TODO _code_paths_ a file without trailing newline isn't modified
2066 * TODO to contain one; the "saw-newline" needs to be part of an
2067 * TODO I/O input machinery object */
2068 jputnl:
2069 if(n_pstate & n_PS_READLINE_NL){
2070 if(putc('\n', _coll_fp) == EOF)
2071 goto jerr;
2073 continue;
2076 c = *(cp_base = ++cp);
2077 if(--cnt == 0)
2078 goto jearg;
2080 /* Avoid history entry? */
2081 while(spacechar(c)){
2082 hist = a_HIST_NONE;
2083 c = *(cp_base = ++cp);
2084 if(--cnt == 0)
2085 goto jearg;
2088 /* It may just be an escaped escaped character, do that quick */
2089 if(c == escape)
2090 goto jputline;
2092 /* Avoid hard *errexit*? */
2093 flags &= ~a_IGNERR;
2094 if(c == '-'){
2095 flags ^= a_IGNERR;
2096 c = *++cp;
2097 if(--cnt == 0)
2098 goto jearg;
2101 /* Trim input, also to gain a somewhat normalized history entry */
2102 ++cp;
2103 if(--cnt > 0){
2104 struct str x;
2106 x.s = n_UNCONST(cp);
2107 x.l = (size_t)cnt;
2108 n_str_trim_ifs(&x, TRU1);
2109 x.s[x.l] = '\0';
2110 cp = x.s;
2111 cnt = (int)/*XXX*/x.l;
2114 if(hist != a_HIST_NONE){
2115 sp = n_string_assign_c(sp, escape);
2116 if(flags & a_IGNERR)
2117 sp = n_string_push_c(sp, '-');
2118 sp = n_string_push_c(sp, c);
2119 if(cnt > 0){
2120 sp = n_string_push_c(sp, ' ');
2121 sp = n_string_push_buf(sp, cp, cnt);
2125 switch(c){
2126 default:
2127 if(1){
2128 char buf[sizeof(n_UNIREPL)];
2130 if(asciichar(c))
2131 buf[0] = c, buf[1] = '\0';
2132 else if(n_psonce & n_PSO_UNICODE)
2133 memcpy(buf, n_unirepl, sizeof n_unirepl);
2134 else
2135 buf[0] = '?', buf[1] = '\0';
2136 n_err(_("Unknown command escape: `%c%s'\n"), escape, buf);
2137 }else
2138 jearg:
2139 n_err(_("Invalid command escape usage: %s\n"),
2140 n_shexp_quote_cp(linebuf, FAL0));
2141 if(a_HARDERR())
2142 goto jerr;
2143 n_pstate_err_no = n_ERR_INVAL;
2144 n_pstate_ex_no = 1;
2145 continue;
2146 case '!':
2147 /* Shell escape, send the balance of line to sh -c */
2148 if(cnt == 0 || coap != NULL)
2149 goto jearg;
2150 else{
2151 char const *argv[2];
2153 argv[0] = cp;
2154 argv[1] = NULL;
2155 n_pstate_ex_no = c_shell(argv); /* TODO history norm.; errexit? */
2157 goto jhistcont;
2158 case ':':
2159 /* FALLTHRU */
2160 case '_':
2161 /* Escape to command mode, but be nice! *//* TODO command expansion
2162 * TODO should be handled here so that we have unique history! */
2163 if(cnt == 0)
2164 goto jearg;
2165 _execute_command(hp, cp, cnt);
2166 if(ok_blook(errexit))
2167 flags |= a_ERREXIT;
2168 else
2169 flags &= ~a_ERREXIT;
2170 if(n_pstate_ex_no != 0 && a_HARDERR())
2171 goto jerr;
2172 if(coap == NULL)
2173 escape = *ok_vlook(escape);
2174 break;
2175 case '.':
2176 /* Simulate end of file on input */
2177 if(cnt != 0 || coap != NULL)
2178 goto jearg;
2179 goto jout; /* TODO does not enter history, thus */
2180 case 'x':
2181 /* Same as 'q', but no *DEAD* saving */
2182 /* FALLTHRU */
2183 case 'q':
2184 /* Force a quit, act like an interrupt had happened */
2185 if(cnt != 0)
2186 goto jearg;
2187 /* If we are running a splice hook, assume it quits on its own now,
2188 * otherwise we (no true out-of-band IPC to signal this state, XXX sic)
2189 * have to SIGTERM it in order to stop this wild beast */
2190 flags |= a_COAP_NOSIGTERM;
2191 ++_coll_hadintr;
2192 _collint((c == 'x') ? 0 : SIGINT);
2193 exit(n_EXIT_ERR);
2194 /*NOTREACHED*/
2195 case 'h':
2196 /* Grab a bunch of headers */
2197 if(cnt != 0)
2198 goto jearg;
2200 grab_headers(n_GO_INPUT_CTX_COMPOSE, hp,
2201 (GTO | GSUBJECT | GCC | GBCC),
2202 (ok_blook(bsdcompat) && ok_blook(bsdorder)));
2203 while(hp->h_to == NULL);
2204 n_pstate_err_no = n_ERR_NONE; /* XXX */
2205 n_pstate_ex_no = 0; /* XXX */
2206 break;
2207 case 'H':
2208 /* Grab extra headers */
2209 if(cnt != 0)
2210 goto jearg;
2212 grab_headers(n_GO_INPUT_CTX_COMPOSE, hp, GEXTRA, 0);
2213 while(check_from_and_sender(hp->h_from, hp->h_sender) == NULL);
2214 n_pstate_err_no = n_ERR_NONE; /* XXX */
2215 n_pstate_ex_no = 0; /* XXX */
2216 break;
2217 case 't':
2218 /* Add to the To: list */
2219 if(cnt == 0)
2220 goto jearg;
2221 else{
2222 struct name *np;
2223 si8_t soe;
2225 soe = 0;
2226 if((np = checkaddrs(lextract(cp, GTO | GFULL), EACM_NORMAL, &soe)
2227 ) != NULL)
2228 hp->h_to = cat(hp->h_to, np);
2229 if(soe == 0){
2230 n_pstate_err_no = n_ERR_NONE;
2231 n_pstate_ex_no = 0;
2232 }else{
2233 n_pstate_ex_no = 1;
2234 n_pstate_err_no = (soe < 0) ? n_ERR_PERM : n_ERR_INVAL;
2237 hist &= ~a_HIST_GABBY;
2238 break;
2239 case 's':
2240 /* Set the Subject list */
2241 if(cnt == 0)
2242 goto jearg;
2243 /* Subject:; take care for Debian #419840 and strip any \r and \n */
2244 if(n_anyof_cp("\n\r", hp->h_subject = savestr(cp))){
2245 char *xp;
2247 n_err(_("-s: normalizing away invalid ASCII NL / CR bytes\n"));
2248 for(xp = hp->h_subject; *xp != '\0'; ++xp)
2249 if(*xp == '\n' || *xp == '\r')
2250 *xp = ' ';
2251 n_pstate_err_no = n_ERR_INVAL;
2252 n_pstate_ex_no = 1;
2253 }else{
2254 n_pstate_err_no = n_ERR_NONE;
2255 n_pstate_ex_no = 0;
2257 break;
2258 case '@':{
2259 struct attachment *aplist;
2261 /* Edit the attachment list */
2262 aplist = hp->h_attach;
2263 hp->h_attach = NULL;
2264 if(cnt != 0)
2265 hp->h_attach = n_attachment_append_list(aplist, cp);
2266 else
2267 hp->h_attach = n_attachment_list_edit(aplist,
2268 n_GO_INPUT_CTX_COMPOSE);
2269 n_pstate_err_no = n_ERR_NONE; /* XXX ~@ does NOT handle $!/$?! */
2270 n_pstate_ex_no = 0; /* XXX */
2271 } break;
2272 case 'c':
2273 /* Add to the CC list */
2274 if(cnt == 0)
2275 goto jearg;
2276 else{
2277 struct name *np;
2278 si8_t soe;
2280 soe = 0;
2281 if((np = checkaddrs(lextract(cp, GCC | GFULL), EACM_NORMAL, &soe)
2282 ) != NULL)
2283 hp->h_cc = cat(hp->h_cc, np);
2284 if(soe == 0){
2285 n_pstate_err_no = n_ERR_NONE;
2286 n_pstate_ex_no = 0;
2287 }else{
2288 n_pstate_ex_no = 1;
2289 n_pstate_err_no = (soe < 0) ? n_ERR_PERM : n_ERR_INVAL;
2292 hist &= ~a_HIST_GABBY;
2293 break;
2294 case 'b':
2295 /* Add stuff to blind carbon copies list */
2296 if(cnt == 0)
2297 goto jearg;
2298 else{
2299 struct name *np;
2300 si8_t soe;
2302 soe = 0;
2303 if((np = checkaddrs(lextract(cp, GBCC | GFULL), EACM_NORMAL, &soe)
2304 ) != NULL)
2305 hp->h_bcc = cat(hp->h_bcc, np);
2306 if(soe == 0){
2307 n_pstate_err_no = n_ERR_NONE;
2308 n_pstate_ex_no = 0;
2309 }else{
2310 n_pstate_ex_no = 1;
2311 n_pstate_err_no = (soe < 0) ? n_ERR_PERM : n_ERR_INVAL;
2314 hist &= ~a_HIST_GABBY;
2315 break;
2316 case 'd':
2317 if(cnt != 0)
2318 goto jearg;
2319 cp = n_getdeadletter();
2320 if(0){
2321 /*FALLTHRU*/
2322 case 'R':
2323 case 'r':
2324 case '<':
2325 /* Invoke a file: Search for the file name, then open it and copy
2326 * the contents to _coll_fp */
2327 if(cnt == 0){
2328 n_err(_("Interpolate what file?\n"));
2329 if(a_HARDERR())
2330 goto jerr;
2331 n_pstate_err_no = n_ERR_NOENT;
2332 n_pstate_ex_no = 1;
2333 break;
2335 if(*cp == '!' && c == '<'){
2336 /* TODO hist. normalization */
2337 if((n_pstate_err_no = a_coll_insert_cmd(_coll_fp, ++cp)
2338 ) != n_ERR_NONE){
2339 if(ferror(_coll_fp))
2340 goto jerr;
2341 if(a_HARDERR())
2342 goto jerr;
2343 n_pstate_ex_no = 1;
2344 break;
2346 goto jhistcont;
2348 /* Note this also expands things like
2349 * !:vput vexpr delim random 0
2350 * !< - $delim */
2351 if((cp = fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO | FEXP_NSHELL)
2352 ) == NULL){
2353 if(a_HARDERR())
2354 goto jerr;
2355 n_pstate_err_no = n_ERR_INVAL;
2356 n_pstate_ex_no = 1;
2357 break;
2360 if(n_is_dir(cp, FAL0)){
2361 n_err(_("%s: is a directory\n"), n_shexp_quote_cp(cp, FAL0));
2362 if(a_HARDERR())
2363 goto jerr;
2364 n_pstate_err_no = n_ERR_ISDIR;
2365 n_pstate_ex_no = 1;
2366 break;
2368 if((n_pstate_err_no = a_coll_include_file(cp, (c == 'R'), TRU1)
2369 ) != n_ERR_NONE){
2370 if(ferror(_coll_fp))
2371 goto jerr;
2372 if(a_HARDERR())
2373 goto jerr;
2374 n_pstate_ex_no = 1;
2375 break;
2377 n_pstate_err_no = n_ERR_NONE; /* XXX */
2378 n_pstate_ex_no = 0; /* XXX */
2379 break;
2380 case 'i':
2381 case 'I':
2382 /* Insert a variable into the file */
2383 if(cnt == 0)
2384 goto jearg;
2385 if((cp = n_var_vlook(cp, TRU1)) == NULL || *cp == '\0')
2386 break;
2387 if(!a_coll_putesc(cp, (c != 'I'), _coll_fp))
2388 goto jerr;
2389 if((n_psonce & n_PSO_INTERACTIVE) &&
2390 !a_coll_putesc(cp, (c != 'I'), n_stdout))
2391 goto jerr;
2392 n_pstate_err_no = n_ERR_NONE; /* XXX */
2393 n_pstate_ex_no = 0; /* XXX */
2394 break;
2395 case 'a':
2396 case 'A':
2397 /* Insert the contents of a signature variable */
2398 if(cnt != 0)
2399 goto jearg;
2400 cp = (c == 'a') ? ok_vlook(sign) : ok_vlook(Sign);
2401 if(cp != NULL && *cp != '\0'){
2402 if(!a_coll_putesc(cp, TRU1, _coll_fp))
2403 goto jerr;
2404 if((n_psonce & n_PSO_INTERACTIVE) &&
2405 !a_coll_putesc(cp, TRU1, n_stdout))
2406 goto jerr;
2408 n_pstate_err_no = n_ERR_NONE; /* XXX */
2409 n_pstate_ex_no = 0; /* XXX */
2410 break;
2411 case 'w':
2412 /* Write the message on a file */
2413 if(cnt == 0)
2414 goto jearg;
2415 if((cp = fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO)) == NULL){
2416 n_err(_("Write what file!?\n"));
2417 if(a_HARDERR())
2418 goto jerr;
2419 n_pstate_err_no = n_ERR_INVAL;
2420 n_pstate_ex_no = 1;
2421 break;
2423 rewind(_coll_fp);
2424 if((n_pstate_err_no = a_coll_write(cp, _coll_fp, 1)) == n_ERR_NONE)
2425 n_pstate_ex_no = 0;
2426 else if(ferror(_coll_fp))
2427 goto jerr;
2428 else if(a_HARDERR())
2429 goto jerr;
2430 else
2431 n_pstate_ex_no = 1;
2432 break;
2433 case 'm':
2434 case 'M':
2435 case 'f':
2436 case 'F':
2437 case 'u':
2438 case 'U':
2439 /* Interpolate the named messages, if we are in receiving mail mode.
2440 * Does the standard list processing garbage. If ~f is given, we
2441 * don't shift over */
2442 if(cnt == 0)
2443 goto jearg;
2444 if((n_pstate_err_no = a_coll_forward(cp, _coll_fp, c)) == n_ERR_NONE)
2445 n_pstate_ex_no = 0;
2446 else if(ferror(_coll_fp))
2447 goto jerr;
2448 else if(a_HARDERR())
2449 goto jerr;
2450 else
2451 n_pstate_ex_no = 1;
2452 break;
2453 case 'p':
2454 /* Print current state of the message without altering anything */
2455 if(cnt != 0)
2456 goto jearg;
2457 print_collf(_coll_fp, hp); /* XXX pstate_err_no ++ */
2458 if(ferror(_coll_fp))
2459 goto jerr;
2460 n_pstate_err_no = n_ERR_NONE; /* XXX */
2461 n_pstate_ex_no = 0; /* XXX */
2462 break;
2463 case '|':
2464 /* Pipe message through command. Collect output as new message */
2465 if(cnt == 0)
2466 goto jearg;
2467 rewind(_coll_fp);
2468 if((n_pstate_err_no = a_coll_pipe(cp)) == n_ERR_NONE)
2469 n_pstate_ex_no = 0;
2470 else if(a_HARDERR())
2471 goto jerr;
2472 else
2473 n_pstate_ex_no = 1;
2474 hist &= ~a_HIST_GABBY;
2475 goto jhistcont;
2476 case 'v':
2477 case 'e':
2478 /* Edit the current message. 'e' -> use EDITOR, 'v' -> use VISUAL */
2479 if(cnt != 0 || coap != NULL)
2480 goto jearg;
2481 rewind(_coll_fp);
2482 if((n_pstate_err_no = a_coll_edit(c, ok_blook(editheaders) ? hp : NULL)
2483 ) == n_ERR_NONE)
2484 n_pstate_ex_no = 0;
2485 else if(ferror(_coll_fp))
2486 goto jerr;
2487 else if(a_HARDERR())
2488 goto jerr;
2489 else
2490 n_pstate_ex_no = 1;
2491 goto jhistcont;
2492 case '^':
2493 if(!a_collect_plumbing(cp, hp)){
2494 if(ferror(_coll_fp))
2495 goto jerr;
2496 goto jearg;
2498 n_pstate_err_no = n_ERR_NONE; /* XXX */
2499 n_pstate_ex_no = 0; /* XXX */
2500 hist &= ~a_HIST_GABBY;
2501 break;
2502 case '?':
2503 /* Last the lengthy help string. (Very ugly, but take care for
2504 * compiler supported string lengths :() */
2505 fputs(_(
2506 "COMMAND ESCAPES (to be placed after a newline) excerpt:\n"
2507 "~. Commit and send message\n"
2508 "~: <command> Execute an internal command\n"
2509 "~< <file> Insert <file> (\"~<! <command>\" inserts shell command)\n"
2510 "~@ [<files>] Edit[/Add] attachments (file[=input-charset[#output-charset]])\n"
2511 "~c <users> Add users to Cc: list (`~b': to Bcc:)\n"
2512 "~d Read in $DEAD (dead.letter)\n"
2513 "~e Edit message via $EDITOR\n"
2514 "~F <msglist> Read in with headers, do not *indentprefix* lines\n"
2515 ), n_stdout);
2516 fputs(_(
2517 "~f <msglist> Like ~F, but honour `ignore' / `retain' configuration\n"
2518 "~H Edit From:, Reply-To: and Sender:\n"
2519 "~h Prompt for Subject:, To:, Cc: and \"blind\" Bcc:\n"
2520 "~i <variable> Insert a value and a newline\n"
2521 "~M <msglist> Read in with headers, *indentprefix* (`~m': `retain' etc.)\n"
2522 "~p Show current message compose buffer\n"
2523 "~r <file> Insert <file> (`~R': likewise, but *indentprefix* lines)\n"
2524 " <file> may also be <- [HERE-DELIMITER]>\n"
2525 ), n_stdout);
2526 fputs(_(
2527 "~s <subject> Set Subject:\n"
2528 "~t <users> Add users to To: list\n"
2529 "~u <msglist> Read in message(s) without headers (`~U': indent lines)\n"
2530 "~v Edit message via $VISUAL\n"
2531 "~w <file> Write message onto file\n"
2532 "~x Abort composition, discard message (`~q': save in $DEAD)\n"
2533 "~| <command> Pipe message through shell filter\n"
2534 ), n_stdout);
2535 if(cnt != 0)
2536 goto jearg;
2537 n_pstate_err_no = n_ERR_NONE;
2538 n_pstate_ex_no = 0;
2539 break;
2542 /* Finally place an entry in history as applicable */
2543 if(0){
2544 jhistcont:
2545 c = '\1';
2546 }else
2547 c = '\0';
2548 if(hist & a_HIST_ADD)
2549 n_tty_addhist(n_string_cp(sp), ((hist & a_HIST_GABBY) != 0));
2550 if(c != '\0')
2551 goto jcont;
2554 jout:
2555 /* Do we have *on-compose-splice-shell*, or *on-compose-splice*?
2556 * TODO Usual f...ed up state of signals and terminal etc. */
2557 if(coap == NULL && (cp = ok_vlook(on_compose_splice_shell)) != NULL) Jocs:{
2558 union {int (*ptf)(void); char const *sh;} u;
2559 char const *cmd;
2561 /* Reset *escape* and more to their defaults. On change update manual! */
2562 if(ifs_saved == NULL)
2563 ifs_saved = savestr(ok_vlook(ifs));
2564 escape = n_ESCAPE[0];
2565 ok_vclear(ifs);
2567 if(coapm != NULL){
2568 /* XXX Due Popen() fflush(NULL) in PTF mode, ensure nothing to flush */
2569 /*if(!n_real_seek(_coll_fp, 0, SEEK_END))
2570 * goto jerr;*/
2571 u.ptf = &a_coll_ocs__mac;
2572 cmd = (char*)-1;
2573 a_coll_ocs__macname = cp = coapm;
2574 }else{
2575 u.sh = ok_vlook(SHELL);
2576 cmd = cp;
2579 i = strlen(cp) +1;
2580 coap = n_lofi_alloc(n_VSTRUCT_SIZEOF(struct a_coll_ocs_arg, coa_cmd
2581 ) + i);
2582 coap->coa_pipe[0] = coap->coa_pipe[1] = -1;
2583 coap->coa_stdin = coap->coa_stdout = NULL;
2584 coap->coa_senderr = checkaddr_err;
2585 memcpy(coap->coa_cmd, cp, i);
2587 hold_all_sigs();
2588 coap->coa_opipe = safe_signal(SIGPIPE, SIG_IGN);
2589 coap->coa_oint = safe_signal(SIGINT, SIG_IGN);
2590 rele_all_sigs();
2592 if(pipe_cloexec(coap->coa_pipe) != -1 &&
2593 (coap->coa_stdin = Fdopen(coap->coa_pipe[0], "r", FAL0)) != NULL &&
2594 (coap->coa_stdout = Popen(cmd, "W", u.sh, NULL, coap->coa_pipe[1])
2595 ) != NULL){
2596 close(coap->coa_pipe[1]);
2597 coap->coa_pipe[1] = -1;
2599 temporary_compose_mode_hook_call(NULL, NULL, NULL);
2600 n_go_splice_hack(coap->coa_cmd, coap->coa_stdin, coap->coa_stdout,
2601 (n_psonce & ~(n_PSO_INTERACTIVE | n_PSO_TTYIN | n_PSO_TTYOUT)),
2602 &a_coll_ocs__finalize, &coap);
2603 /* Hook version protocol for ~^: update manual upon change! */
2604 fputs(a_COLL_PLUMBING_VERSION "\n", coap->coa_stdout);
2605 #undef a_COLL_PLUMBING_VERSION
2606 goto jcont;
2609 c = n_err_no;
2610 a_coll_ocs__finalize(coap);
2611 n_perr(_("Cannot invoke *on-compose-splice(-shell)?*"), c);
2612 goto jerr;
2614 if(*checkaddr_err != 0){
2615 *checkaddr_err = 0;
2616 goto jerr;
2618 if(coapm == NULL && (coapm = ok_vlook(on_compose_splice)) != NULL)
2619 goto Jocs;
2620 if(coap != NULL){
2621 ok_vset(ifs, ifs_saved);
2622 ifs_saved = NULL;
2625 /* Final chance to edit headers, if not already above */
2626 if(n_psonce & n_PSO_INTERACTIVE){
2627 if(ok_blook(bsdcompat) || ok_blook(askatend)){
2628 enum gfield gf;
2630 gf = GNONE;
2631 if(ok_blook(askcc))
2632 gf |= GCC;
2633 if(ok_blook(askbcc))
2634 gf |= GBCC;
2635 if(gf != 0)
2636 grab_headers(n_GO_INPUT_CTX_COMPOSE, hp, gf, 1);
2638 if(ok_blook(askattach))
2639 hp->h_attach = n_attachment_list_edit(hp->h_attach,
2640 n_GO_INPUT_CTX_COMPOSE);
2643 /* Execute compose-leave */
2644 if((cp = ok_vlook(on_compose_leave)) != NULL){
2645 setup_from_and_sender(hp);
2646 temporary_compose_mode_hook_call(cp, &n_temporary_compose_hook_varset,
2647 hp);
2650 /* Add automatic receivers */
2651 if ((cp = ok_vlook(autocc)) != NULL && *cp != '\0')
2652 hp->h_cc = cat(hp->h_cc, checkaddrs(lextract(cp, GCC | GFULL),
2653 EACM_NORMAL, checkaddr_err));
2654 if ((cp = ok_vlook(autobcc)) != NULL && *cp != '\0')
2655 hp->h_bcc = cat(hp->h_bcc, checkaddrs(lextract(cp, GBCC | GFULL),
2656 EACM_NORMAL, checkaddr_err));
2657 if (*checkaddr_err != 0)
2658 goto jerr;
2660 /* TODO Cannot do since it may require turning this into a multipart one */
2661 if(n_poption & n_PO_Mm_FLAG)
2662 goto jskiptails;
2664 /* Place signature? */
2665 if((cp = ok_vlook(signature)) != NULL && *cp != '\0'){
2666 char const *cpq;
2668 if((cpq = fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO)) == NULL){
2669 n_err(_("*signature* expands to invalid file: %s\n"),
2670 n_shexp_quote_cp(cp, FAL0));
2671 goto jerr;
2673 cpq = n_shexp_quote_cp(cp = cpq, FAL0);
2675 if((sigfp = Fopen(cp, "r")) == NULL){
2676 n_err(_("Can't open *signature* %s: %s\n"),
2677 cpq, n_err_to_doc(n_err_no));
2678 goto jerr;
2681 if(linebuf == NULL)
2682 linebuf = smalloc(linesize = LINESIZE);
2683 c = '\0';
2684 while((i = fread(linebuf, sizeof *linebuf, linesize, n_UNVOLATILE(sigfp)))
2685 > 0){
2686 c = linebuf[i - 1];
2687 if(i != fwrite(linebuf, sizeof *linebuf, i, _coll_fp))
2688 goto jerr;
2691 /* C99 */{
2692 FILE *x = n_UNVOLATILE(sigfp);
2693 int e = n_err_no, ise = ferror(x);
2695 sigfp = NULL;
2696 Fclose(x);
2698 if(ise){
2699 n_err(_("Errors while reading *signature* %s: %s\n"),
2700 cpq, n_err_to_doc(e));
2701 goto jerr;
2705 if(c != '\0' && c != '\n')
2706 putc('\n', _coll_fp);
2709 { char const *cp_obsolete = ok_vlook(NAIL_TAIL);
2711 if(cp_obsolete != NULL)
2712 n_OBSOLETE(_("please use *message-inject-tail*, not *NAIL_TAIL*"));
2714 if((cp = ok_vlook(message_inject_tail)) != NULL ||
2715 (cp = cp_obsolete) != NULL){
2716 if(!a_coll_putesc(cp, TRU1, _coll_fp))
2717 goto jerr;
2718 if((n_psonce & n_PSO_INTERACTIVE) && !a_coll_putesc(cp, TRU1, n_stdout))
2719 goto jerr;
2723 jskiptails:
2724 if(fflush(_coll_fp))
2725 goto jerr;
2726 rewind(_coll_fp);
2728 jleave:
2729 if (linebuf != NULL)
2730 free(linebuf);
2731 sigprocmask(SIG_BLOCK, &nset, NULL);
2732 n_pstate &= ~n_PS_COMPOSE_MODE;
2733 safe_signal(SIGINT, _coll_saveint);
2734 safe_signal(SIGHUP, _coll_savehup);
2735 sigprocmask(SIG_SETMASK, &oset, NULL);
2736 NYD_LEAVE;
2737 return _coll_fp;
2739 jerr:
2740 hold_all_sigs();
2742 if(coap != NULL && coap != (struct a_coll_ocs_arg*)-1){
2743 if(!(flags & a_COAP_NOSIGTERM))
2744 n_psignal(coap->coa_stdout, SIGTERM);
2745 n_go_splice_hack_remove_after_jump();
2746 coap = NULL;
2748 if(ifs_saved != NULL){
2749 ok_vset(ifs, ifs_saved);
2750 ifs_saved = NULL;
2752 if(sigfp != NULL){
2753 Fclose(n_UNVOLATILE(sigfp));
2754 sigfp = NULL;
2756 if(_coll_fp != NULL){
2757 Fclose(_coll_fp);
2758 _coll_fp = NULL;
2761 rele_all_sigs();
2763 assert(checkaddr_err != NULL);
2764 /* TODO We don't save in $DEAD upon error because msg not readily composed?
2765 * TODO But this no good, it should go ZOMBIE / DRAFT / POSTPONED or what! */
2766 if(*checkaddr_err != 0){
2767 if(*checkaddr_err == 111)
2768 n_err(_("Compose mode splice hook failure\n"));
2769 else
2770 n_err(_("Some addressees were classified as \"hard error\"\n"));
2771 }else if(_coll_hadintr == 0){
2772 *checkaddr_err = TRU1; /* TODO ugly: "sendout_error" now.. */
2773 n_err(_("Failed to prepare composed message (I/O error, disk full?)\n"));
2775 goto jleave;
2777 #undef a_HARDERR
2780 /* s-it-mode */