cc-test.sh: t_behave_iconv_mainbody() should compile test instead, sigh!
[s-mailx.git] / collect.c
blob01d03cc960fc7993ae78ef9a0b616af820d140e3
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Collect input from standard input, handling ~ escapes.
3 *@ TODO This needs a complete rewrite, with carriers, etc.
5 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
6 * Copyright (c) 2012 - 2018 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
7 */
8 /*
9 * Copyright (c) 1980, 1993
10 * The Regents of the University of California. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
36 #undef n_FILE
37 #define n_FILE collect
39 #ifndef HAVE_AMALGAMATION
40 # include "nail.h"
41 #endif
43 struct a_coll_ocs_arg{
44 sighandler_type coa_opipe;
45 sighandler_type coa_oint;
46 FILE *coa_stdin; /* The descriptor (pipe(2)+Fdopen()) we read from */
47 FILE *coa_stdout; /* The Popen()ed pipe through which we write to the hook */
48 int coa_pipe[2]; /* ..backing .coa_stdin */
49 si8_t *coa_senderr; /* Set to 1 on failure */
50 char coa_cmd[n_VFIELD_SIZE(0)];
53 /* The following hookiness with global variables is so that on receipt of an
54 * interrupt signal, the partial message can be salted away on *DEAD* */
56 static sighandler_type _coll_saveint; /* Previous SIGINT value */
57 static sighandler_type _coll_savehup; /* Previous SIGHUP value */
58 static FILE *_coll_fp; /* File for saving away */
59 static int volatile _coll_hadintr; /* Have seen one SIGINT so far */
60 static sigjmp_buf _coll_jmp; /* To get back to work */
61 static sigjmp_buf _coll_abort; /* To end collection with error */
62 static char const *a_coll_ocs__macname; /* *on-compose-splice* */
64 /* Handle `~:', `~_' and some hooks; hp may be NULL */
65 static void _execute_command(struct header *hp, char const *linebuf,
66 size_t linesize);
68 /* Return errno */
69 static si32_t a_coll_include_file(char const *name, bool_t indent,
70 bool_t writestat);
72 /* Execute cmd and insert its standard output into fp, return errno */
73 static si32_t a_coll_insert_cmd(FILE *fp, char const *cmd);
75 /* ~p command */
76 static void print_collf(FILE *collf, struct header *hp);
78 /* Write a file, ex-like if f set */
79 static si32_t a_coll_write(char const *name, FILE *fp, int f);
81 /* *message-inject-head* */
82 static bool_t a_coll_message_inject_head(FILE *fp);
84 /* Parse off the message header from fp and store relevant fields in hp,
85 * replace _coll_fp with a shiny new version without any header */
86 static bool_t a_coll_makeheader(FILE *fp, struct header *hp,
87 si8_t *checkaddr_err, bool_t do_delayed_due_t);
89 /* Edit the message being collected on fp. On return, make the edit file the
90 * new temp file. Return errno */
91 static si32_t a_coll_edit(int c, struct header *hp);
93 /* Pipe the message through the command. Old message is on stdin of command,
94 * new message collected from stdout. Shell must return 0 to accept new msg */
95 static si32_t a_coll_pipe(char const *cmd);
97 /* Interpolate the named messages into the current message, possibly doing
98 * indent stuff. The flag argument is one of the command escapes: [mMfFuU].
99 * Return errno */
100 static si32_t a_coll_forward(char const *ms, FILE *fp, int f);
102 /* ~^ mode */
103 static bool_t a_collect_plumbing(char const *ms, struct header *p);
105 static bool_t a_collect__plumb_header(char const *cp, struct header *p,
106 char const *cmd[4]);
107 static bool_t a_collect__plumb_attach(char const *cp, struct header *p,
108 char const *cmd[4]);
110 /* On interrupt, come here to save the partial message in ~/dead.letter.
111 * Then jump out of the collection loop */
112 static void _collint(int s);
114 static void collhup(int s);
116 /* ~[AaIi], *message-inject-**: put value, expand \[nt] if *posix* */
117 static bool_t a_coll_putesc(char const *s, bool_t addnl, FILE *stream);
119 /* *on-compose-splice* driver and *on-compose-splice(-shell)?* finalizer */
120 static int a_coll_ocs__mac(void);
121 static void a_coll_ocs__finalize(void *vp);
123 static void
124 _execute_command(struct header *hp, char const *linebuf, size_t linesize){
125 /* The problem arises if there are rfc822 message attachments and the
126 * user uses `~:' to change the current file. TODO Unfortunately we
127 * TODO cannot simply keep a pointer to, or increment a reference count
128 * TODO of the current `file' (mailbox that is) object, because the
129 * TODO codebase doesn't deal with that at all; so, until some far
130 * TODO later time, copy the name of the path, and warn the user if it
131 * TODO changed; we COULD use the AC_TMPFILE attachment type, i.e.,
132 * TODO copy the message attachments over to temporary files, but that
133 * TODO would require more changes so that the user still can recognize
134 * TODO in `~@' etc. that its a rfc822 message attachment; see below */
135 struct n_sigman sm;
136 struct attachment *ap;
137 char * volatile mnbuf;
138 NYD_ENTER;
140 n_UNUSED(linesize);
141 mnbuf = NULL;
143 n_SIGMAN_ENTER_SWITCH(&sm, n_SIGMAN_HUP | n_SIGMAN_INT | n_SIGMAN_QUIT){
144 case 0:
145 break;
146 default:
147 n_pstate_err_no = n_ERR_INTR;
148 n_pstate_ex_no = 1;
149 goto jleave;
152 /* If the above todo is worked, remove or outsource to attachment.c! */
153 if(hp != NULL && (ap = hp->h_attach) != NULL) do
154 if(ap->a_msgno){
155 mnbuf = sstrdup(mailname);
156 break;
158 while((ap = ap->a_flink) != NULL);
160 n_go_command(n_GO_INPUT_CTX_COMPOSE, linebuf);
162 n_sigman_cleanup_ping(&sm);
163 jleave:
164 if(mnbuf != NULL){
165 if(strcmp(mnbuf, mailname))
166 n_err(_("Mailbox changed: it is likely that existing "
167 "rfc822 attachments became invalid!\n"));
168 free(mnbuf);
170 NYD_LEAVE;
171 n_sigman_leave(&sm, n_SIGMAN_VIPSIGS_NTTYOUT);
174 static si32_t
175 a_coll_include_file(char const *name, bool_t indent, bool_t writestat){
176 FILE *fbuf;
177 char const *heredb, *indb;
178 size_t linesize, heredl, indl, cnt, linelen;
179 char *linebuf;
180 si64_t lc, cc;
181 si32_t rv;
182 NYD_ENTER;
184 rv = n_ERR_NONE;
185 lc = cc = 0;
186 linebuf = NULL; /* TODO line pool */
187 linesize = 0;
188 heredb = NULL;
189 heredl = 0;
191 /* The -M case is special */
192 if(name == (char*)-1){
193 fbuf = n_stdin;
194 name = "-";
195 }else if(name[0] == '-' &&
196 (name[1] == '\0' || blankspacechar(name[1]))){
197 fbuf = n_stdin;
198 if(name[1] == '\0'){
199 if(!(n_psonce & n_PSO_INTERACTIVE)){
200 n_err(_("~< -: HERE-delimiter required in non-interactive mode\n"));
201 rv = n_ERR_INVAL;
202 goto jleave;
204 }else{
205 for(heredb = &name[2]; *heredb != '\0' && blankspacechar(*heredb);
206 ++heredb)
208 if((heredl = strlen(heredb)) == 0){
209 jdelim_empty:
210 n_err(_("~< - HERE-delimiter: delimiter must not be empty\n"));
211 rv = n_ERR_INVAL;
212 goto jleave;
215 if(*heredb == '\''){
216 for(indb = ++heredb; *indb != '\0' && *indb != '\''; ++indb)
218 if(*indb == '\0'){
219 n_err(_("~< - HERE-delimiter: missing trailing quote\n"));
220 rv = n_ERR_INVAL;
221 goto jleave;
222 }else if(indb[1] != '\0'){
223 n_err(_("~< - HERE-delimiter: trailing characters after "
224 "quote\n"));
225 rv = n_ERR_INVAL;
226 goto jleave;
228 if((heredl = PTR2SIZE(indb - heredb)) == 0)
229 goto jdelim_empty;
230 heredb = savestrbuf(heredb, heredl);
233 name = "-";
234 }else if((fbuf = Fopen(name, "r")) == NULL){
235 n_perr(name, rv = n_err_no);
236 goto jleave;
239 indl = indent ? strlen(indb = ok_vlook(indentprefix)) : 0;
241 if(fbuf != n_stdin)
242 cnt = fsize(fbuf);
243 while(fgetline(&linebuf, &linesize, (fbuf == n_stdin ? NULL : &cnt),
244 &linelen, fbuf, 0) != NULL){
245 if(heredl > 0 && heredl == linelen - 1 &&
246 !memcmp(heredb, linebuf, heredl)){
247 heredb = NULL;
248 break;
251 if(indl > 0){
252 if(fwrite(indb, sizeof *indb, indl, _coll_fp) != indl){
253 rv = n_err_no;
254 goto jleave;
256 cc += indl;
259 if(fwrite(linebuf, sizeof *linebuf, linelen, _coll_fp) != linelen){
260 rv = n_err_no;
261 goto jleave;
263 cc += linelen;
264 ++lc;
266 if(fflush(_coll_fp)){
267 rv = n_err_no;
268 goto jleave;
271 if(heredb != NULL)
272 rv = n_ERR_NOTOBACCO;
273 jleave:
274 if(linebuf != NULL)
275 free(linebuf);
276 if(fbuf != NULL){
277 if(fbuf != n_stdin)
278 Fclose(fbuf);
279 else if(heredl > 0)
280 clearerr(n_stdin);
283 if(writestat)
284 fprintf(n_stdout, "%s%s %" PRId64 "/%" PRId64 "\n",
285 n_shexp_quote_cp(name, FAL0), (rv ? " " n_ERROR : n_empty), lc, cc);
286 NYD_LEAVE;
287 return rv;
290 static si32_t
291 a_coll_insert_cmd(FILE *fp, char const *cmd){
292 FILE *ibuf;
293 si64_t lc, cc;
294 si32_t rv;
295 NYD_ENTER;
297 rv = n_ERR_NONE;
298 lc = cc = 0;
300 if((ibuf = Popen(cmd, "r", ok_vlook(SHELL), NULL, 0)) != NULL){
301 int c;
303 while((c = getc(ibuf)) != EOF){ /* XXX bytewise, yuck! */
304 if(putc(c, fp) == EOF){
305 rv = n_err_no;
306 break;
308 ++cc;
309 if(c == '\n')
310 ++lc;
312 if(!feof(ibuf) || ferror(ibuf)){
313 if(rv == n_ERR_NONE)
314 rv = n_ERR_IO;
316 if(!Pclose(ibuf, TRU1)){
317 if(rv == n_ERR_NONE)
318 rv = n_ERR_IO;
320 }else
321 n_perr(cmd, rv = n_err_no);
323 fprintf(n_stdout, "CMD%s %" PRId64 "/%" PRId64 "\n",
324 (rv == n_ERR_NONE ? n_empty : " " n_ERROR), lc, cc);
325 NYD_LEAVE;
326 return rv;
329 static void
330 print_collf(FILE *cf, struct header *hp)
332 char *lbuf;
333 FILE *obuf;
334 size_t cnt, linesize, linelen;
335 NYD_ENTER;
337 fflush_rewind(cf);
338 cnt = (size_t)fsize(cf);
340 if((obuf = Ftmp(NULL, "collfp", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL){
341 n_perr(_("Can't create temporary file for `~p' command"), 0);
342 goto jleave;
345 hold_all_sigs();
347 fprintf(obuf, _("-------\nMessage contains:\n")); /* xxx SEARCH112 */
348 puthead(TRU1, hp, obuf,
349 (GIDENT | GTO | GSUBJECT | GCC | GBCC | GNL | GFILES | GCOMMA),
350 SEND_TODISP, CONV_NONE, NULL, NULL);
352 lbuf = NULL;
353 linesize = 0;
354 while(fgetline(&lbuf, &linesize, &cnt, &linelen, cf, 1))
355 prout(lbuf, linelen, obuf);
356 if(lbuf != NULL)
357 free(lbuf);
359 if(hp->h_attach != NULL){
360 fputs(_("-------\nAttachments:\n"), obuf);
361 n_attachment_list_print(hp->h_attach, obuf);
364 rele_all_sigs();
366 page_or_print(obuf, 0);
368 Fclose(obuf);
369 jleave:
370 NYD_LEAVE;
373 static si32_t
374 a_coll_write(char const *name, FILE *fp, int f)
376 FILE *of;
377 int c;
378 si64_t lc, cc;
379 si32_t rv;
380 NYD_ENTER;
382 rv = n_ERR_NONE;
384 if(f) {
385 fprintf(n_stdout, "%s ", n_shexp_quote_cp(name, FAL0));
386 fflush(n_stdout);
389 if ((of = Fopen(name, "a")) == NULL) {
390 n_perr(name, rv = n_err_no);
391 goto jerr;
394 lc = cc = 0;
395 while ((c = getc(fp)) != EOF) {
396 ++cc;
397 if (c == '\n')
398 ++lc;
399 if (putc(c, of) == EOF) {
400 n_perr(name, rv = n_err_no);
401 goto jerr;
404 fprintf(n_stdout, _("%" PRId64 "/%" PRId64 "\n"), lc, cc);
406 jleave:
407 if(of != NULL)
408 Fclose(of);
409 fflush(n_stdout);
410 NYD_LEAVE;
411 return rv;
412 jerr:
413 putc('-', n_stdout);
414 putc('\n', n_stdout);
415 goto jleave;
418 static bool_t
419 a_coll_message_inject_head(FILE *fp){
420 bool_t rv;
421 char const *cp, *cp_obsolete;
422 NYD2_ENTER;
424 cp_obsolete = ok_vlook(NAIL_HEAD);
425 if(cp_obsolete != NULL)
426 n_OBSOLETE(_("please use *message-inject-head*, not *NAIL_HEAD*"));
428 if(((cp = ok_vlook(message_inject_head)) != NULL ||
429 (cp = cp_obsolete) != NULL) && !a_coll_putesc(cp, TRU1, fp))
430 rv = FAL0;
431 else
432 rv = TRU1;
433 NYD2_LEAVE;
434 return rv;
437 static bool_t
438 a_coll_makeheader(FILE *fp, struct header *hp, si8_t *checkaddr_err,
439 bool_t do_delayed_due_t)
441 FILE *nf;
442 int c;
443 bool_t rv;
444 NYD_ENTER;
446 rv = FAL0;
448 if ((nf = Ftmp(NULL, "colhead", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==NULL) {
449 n_perr(_("temporary mail edit file"), 0);
450 goto jleave;
453 extract_header(fp, hp, checkaddr_err);
454 if (checkaddr_err != NULL && *checkaddr_err != 0)
455 goto jleave;
457 /* In template mode some things have been delayed until the template has
458 * been read */
459 if(do_delayed_due_t){
460 char const *cp;
462 if((cp = ok_vlook(on_compose_enter)) != NULL){
463 setup_from_and_sender(hp);
464 temporary_compose_mode_hook_call(cp, &n_temporary_compose_hook_varset,
465 hp);
468 if(!a_coll_message_inject_head(nf))
469 goto jleave;
472 while ((c = getc(fp)) != EOF) /* XXX bytewise, yuck! */
473 putc(c, nf);
475 if (fp != _coll_fp)
476 Fclose(_coll_fp);
477 Fclose(fp);
478 _coll_fp = nf;
479 nf = NULL;
481 if (check_from_and_sender(hp->h_from, hp->h_sender) == NULL)
482 goto jleave;
483 rv = TRU1;
484 jleave:
485 if(nf != NULL)
486 Fclose(nf);
487 NYD_LEAVE;
488 return rv;
491 static si32_t
492 a_coll_edit(int c, struct header *hp) /* TODO error(return) weird */
494 struct n_sigman sm;
495 FILE *nf;
496 sighandler_type volatile sigint;
497 bool_t saved;
498 si32_t volatile rv;
499 NYD_ENTER;
501 n_UNINIT(sigint, SIG_ERR);
502 rv = n_ERR_NONE;
504 if(!(saved = ok_blook(add_file_recipients)))
505 ok_bset(add_file_recipients);
507 n_SIGMAN_ENTER_SWITCH(&sm, n_SIGMAN_ALL){
508 case 0:
509 sigint = safe_signal(SIGINT, SIG_IGN);
510 break;
511 default:
512 rv = n_ERR_INTR;
513 goto jleave;
516 nf = run_editor(_coll_fp, (off_t)-1, c, FAL0, hp, NULL, SEND_MBOX, sigint);
517 if (nf != NULL) {
518 if (hp) {
519 if(!a_coll_makeheader(nf, hp, NULL, FAL0))
520 rv = n_ERR_INVAL;
521 } else {
522 fseek(nf, 0L, SEEK_END);
523 Fclose(_coll_fp);
524 _coll_fp = nf;
526 } else
527 rv = n_ERR_CHILD;
529 n_sigman_cleanup_ping(&sm);
530 jleave:
531 if(!saved)
532 ok_bclear(add_file_recipients);
533 safe_signal(SIGINT, sigint);
534 NYD_LEAVE;
535 n_sigman_leave(&sm, n_SIGMAN_VIPSIGS_NTTYOUT);
536 return rv;
539 static si32_t
540 a_coll_pipe(char const *cmd)
542 int ws;
543 FILE *nf;
544 sighandler_type sigint;
545 si32_t rv;
546 NYD_ENTER;
548 rv = n_ERR_NONE;
549 sigint = safe_signal(SIGINT, SIG_IGN);
551 if ((nf = Ftmp(NULL, "colpipe", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==NULL) {
552 n_perr(_("temporary mail edit file"), rv = n_err_no);
553 goto jout;
556 /* stdin = current message. stdout = new message */
557 fflush(_coll_fp);
558 if (n_child_run(ok_vlook(SHELL), 0, fileno(_coll_fp), fileno(nf), "-c",
559 cmd, NULL, NULL, &ws) < 0 || WEXITSTATUS(ws) != 0) {
560 Fclose(nf);
561 rv = n_ERR_CHILD;
562 goto jout;
565 if (fsize(nf) == 0) {
566 n_err(_("No bytes from %s !?\n"), n_shexp_quote_cp(cmd, FAL0));
567 Fclose(nf);
568 rv = n_ERR_NODATA;
569 goto jout;
572 /* Take new files */
573 fseek(nf, 0L, SEEK_END);
574 Fclose(_coll_fp);
575 _coll_fp = nf;
576 jout:
577 safe_signal(SIGINT, sigint);
578 NYD_LEAVE;
579 return rv;
582 static si32_t
583 a_coll_forward(char const *ms, FILE *fp, int f)
585 int *msgvec, rv = 0;
586 struct n_ignore const *itp;
587 char const *tabst;
588 enum sendaction action;
589 NYD_ENTER;
591 msgvec = salloc((size_t)(msgCount + 1) * sizeof *msgvec);
592 if (getmsglist(ms, msgvec, 0) < 0) {
593 rv = n_ERR_NOENT; /* XXX not really, should be handled there! */
594 goto jleave;
596 if (*msgvec == 0) {
597 *msgvec = first(0, MMNORM);
598 if (*msgvec == 0) {
599 n_err(_("No appropriate messages\n"));
600 rv = n_ERR_NOENT;
601 goto jleave;
603 msgvec[1] = 0;
606 if (f == 'f' || f == 'F' || f == 'u')
607 tabst = NULL;
608 else
609 tabst = ok_vlook(indentprefix);
610 if (f == 'u' || f == 'U')
611 itp = n_IGNORE_ALL;
612 else
613 itp = upperchar(f) ? NULL : n_IGNORE_TYPE;
614 action = (upperchar(f) && f != 'U') ? SEND_QUOTE_ALL : SEND_QUOTE;
616 fprintf(n_stdout, _("Interpolating:"));
617 srelax_hold();
618 for (; *msgvec != 0; ++msgvec) {
619 struct message *mp = message + *msgvec - 1;
621 touch(mp);
622 fprintf(n_stdout, " %d", *msgvec);
623 fflush(n_stdout);
624 if (sendmp(mp, fp, itp, tabst, action, NULL) < 0) {
625 n_perr(_("temporary mail file"), 0);
626 rv = n_ERR_IO;
627 break;
629 srelax();
631 srelax_rele();
632 fprintf(n_stdout, "\n");
633 jleave:
634 NYD_LEAVE;
635 return rv;
638 static bool_t
639 a_collect_plumbing(char const *ms, struct header *hp){
640 /* TODO _collect_plumbing: instead of fields the basic headers should
641 * TODO be in an array and have IDs, like in termcap etc., so then this
642 * TODO could be simplified as table-walks. Also true for arg-checks! */
643 bool_t rv;
644 char const *cp, *cmd[4];
645 NYD2_ENTER;
647 /* Protcol version for *on-compose-splice** -- update manual on change! */
648 #define a_COLL_PLUMBING_VERSION "0 0 1"
649 cp = ms;
651 /* C99 */{
652 size_t i;
654 for(i = 0; i < n_NELEM(cmd); ++i){ /* TODO trim+strlist_split(_ifs?)() */
655 while(blankchar(*cp))
656 ++cp;
657 if(*cp == '\0')
658 cmd[i] = NULL;
659 else{
660 if(i < n_NELEM(cmd) - 1)
661 for(cmd[i] = cp++; *cp != '\0' && !blankchar(*cp); ++cp)
663 else{
664 /* Last slot takes all the rest of the line, less trailing WS */
665 for(cmd[i] = cp++; *cp != '\0'; ++cp)
667 while(blankchar(cp[-1]))
668 --cp;
670 cmd[i] = savestrbuf(cmd[i], PTR2SIZE(cp - cmd[i]));
675 if(n_UNLIKELY(cmd[0] == NULL))
676 goto jecmd;
677 if(is_asccaseprefix(cmd[0], "header"))
678 rv = a_collect__plumb_header(cp, hp, cmd);
679 else if(is_asccaseprefix(cmd[0], "attachment"))
680 rv = a_collect__plumb_attach(cp, hp, cmd);
681 else{
682 jecmd:
683 fputs("500\n", n_stdout);
684 rv = FAL0;
686 fflush(n_stdout);
688 NYD2_LEAVE;
689 return rv;
692 static bool_t
693 a_collect__plumb_header(char const *cp, struct header *hp,
694 char const *cmd[4]){
695 uiz_t i;
696 struct n_header_field *hfp;
697 struct name *np, **npp;
698 NYD2_ENTER;
700 if(cmd[1] == NULL)
701 goto jdefault;
703 if(is_asccaseprefix(cmd[1], "insert")){ /* TODO LOGIC BELONGS head.c
704 * TODO That is: Header::factory(string) -> object (blahblah).
705 * TODO I.e., as long as we don't have regular RFC compliant parsers
706 * TODO which differentiate in between structured and unstructured
707 * TODO header fields etc., a little workaround */
708 struct name *xnp;
709 si8_t aerr;
710 enum expand_addr_check_mode eacm;
711 enum gfield ntype;
712 bool_t mult_ok;
714 if(cmd[2] == NULL || cmd[3] == NULL)
715 goto jecmd;
717 /* Strip [\r\n] which would render a body invalid XXX all controls? */
718 /* C99 */{
719 char *xp, c;
721 cmd[3] = xp = savestr(cmd[3]);
722 for(; (c = *xp) != '\0'; ++xp)
723 if(c == '\n' || c == '\r')
724 *xp = ' ';
727 if(!asccasecmp(cmd[2], cp = "Subject")){
728 if(cmd[3][0] != '\0'){
729 if(hp->h_subject != NULL)
730 hp->h_subject = savecatsep(hp->h_subject, ' ', cmd[3]);
731 else
732 hp->h_subject = n_UNCONST(cmd[3]);
733 fprintf(n_stdout, "210 %s 1\n", cp);
734 goto jleave;
735 }else
736 goto j501cp;
739 mult_ok = TRU1;
740 ntype = GEXTRA | GFULL | GFULLEXTRA;
741 eacm = EACM_STRICT;
743 if(!asccasecmp(cmd[2], cp = "From")){
744 npp = &hp->h_from;
745 jins:
746 aerr = 0;
747 if((np = lextract(cmd[3], ntype)) == NULL)
748 goto j501cp;
750 if((np = checkaddrs(np, eacm, &aerr), aerr != 0)){
751 fprintf(n_stdout, "505 %s\n", cp);
752 goto jleave;
755 /* Go to the end of the list, track whether it contains any
756 * non-deleted entries */
757 i = 0;
758 if((xnp = *npp) != NULL)
759 for(;; xnp = xnp->n_flink){
760 if(!(xnp->n_type & GDEL))
761 ++i;
762 if(xnp->n_flink == NULL)
763 break;
766 if(!mult_ok && (i != 0 || np->n_flink != NULL))
767 fprintf(n_stdout, "506 %s\n", cp);
768 else{
769 if(xnp == NULL)
770 *npp = np;
771 else
772 xnp->n_flink = np;
773 np->n_blink = xnp;
774 fprintf(n_stdout, "210 %s %" PRIuZ "\n", cp, ++i);
776 goto jleave;
778 if(!asccasecmp(cmd[2], cp = "Sender")){
779 mult_ok = FAL0;
780 npp = &hp->h_sender;
781 goto jins;
783 if(!asccasecmp(cmd[2], cp = "To")){
784 npp = &hp->h_to;
785 ntype = GTO | GFULL;
786 eacm = EACM_NORMAL | EAF_NAME;
787 goto jins;
789 if(!asccasecmp(cmd[2], cp = "Cc")){
790 npp = &hp->h_cc;
791 ntype = GCC | GFULL;
792 eacm = EACM_NORMAL | EAF_NAME;
793 goto jins;
795 if(!asccasecmp(cmd[2], cp = "Bcc")){
796 npp = &hp->h_bcc;
797 ntype = GBCC | GFULL;
798 eacm = EACM_NORMAL | EAF_NAME;
799 goto jins;
801 if(!asccasecmp(cmd[2], cp = "Reply-To")){
802 npp = &hp->h_reply_to;
803 eacm = EACM_NONAME;
804 goto jins;
806 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
807 npp = &hp->h_mft;
808 eacm = EACM_NONAME;
809 goto jins;
811 if(!asccasecmp(cmd[2], cp = "Message-ID")){
812 mult_ok = FAL0;
813 npp = &hp->h_message_id;
814 ntype = GREF;
815 eacm = EACM_NONAME;
816 goto jins;
818 if(!asccasecmp(cmd[2], cp = "References")){
819 npp = &hp->h_ref;
820 ntype = GREF;
821 eacm = EACM_NONAME;
822 goto jins;
824 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
825 npp = &hp->h_in_reply_to;
826 ntype = GREF;
827 eacm = EACM_NONAME;
828 goto jins;
831 if(!asccasecmp(cmd[2], cp = "Mailx-Command") ||
832 !asccasecmp(cmd[2], cp = "Mailx-Raw-To") ||
833 !asccasecmp(cmd[2], cp = "Mailx-Raw-Cc") ||
834 !asccasecmp(cmd[2], cp = "Mailx-Raw-Bcc") ||
835 !asccasecmp(cmd[2], cp = "Mailx-Orig-From") ||
836 !asccasecmp(cmd[2], cp = "Mailx-Orig-To") ||
837 !asccasecmp(cmd[2], cp = "Mailx-Orig-Cc") ||
838 !asccasecmp(cmd[2], cp = "Mailx-Orig-Bcc")){
839 fprintf(n_stdout, "505 %s\n", cp);
840 goto jleave;
843 /* Free-form header fields */
844 /* C99 */{
845 size_t nl, bl;
846 struct n_header_field **hfpp;
848 for(cp = cmd[2]; *cp != '\0'; ++cp)
849 if(!fieldnamechar(*cp)){
850 cp = cmd[2];
851 goto j501cp;
854 for(i = 0, hfpp = &hp->h_user_headers; *hfpp != NULL; ++i)
855 hfpp = &(*hfpp)->hf_next;
857 nl = strlen(cp = cmd[2]) +1;
858 bl = strlen(cmd[3]) +1;
859 *hfpp = hfp = salloc(n_VSTRUCT_SIZEOF(struct n_header_field, hf_dat
860 ) + nl + bl);
861 hfp->hf_next = NULL;
862 hfp->hf_nl = nl - 1;
863 hfp->hf_bl = bl - 1;
864 memcpy(&hfp->hf_dat[0], cp, nl);
865 memcpy(&hfp->hf_dat[nl], cmd[3], bl);
866 fprintf(n_stdout, "210 %s %" PRIuZ "\n", &hfp->hf_dat[0], ++i);
868 goto jleave;
871 if(is_asccaseprefix(cmd[1], "list")){
872 jdefault:
873 if(cmd[2] == NULL){
874 fputs("210", n_stdout);
875 if(hp->h_subject != NULL) fputs(" Subject", n_stdout);
876 if(hp->h_from != NULL) fputs(" From", n_stdout);
877 if(hp->h_sender != NULL) fputs(" Sender", n_stdout);
878 if(hp->h_to != NULL) fputs(" To", n_stdout);
879 if(hp->h_cc != NULL) fputs(" Cc", n_stdout);
880 if(hp->h_bcc != NULL) fputs(" Bcc", n_stdout);
881 if(hp->h_reply_to != NULL) fputs(" Reply-To", n_stdout);
882 if(hp->h_mft != NULL) fputs(" Mail-Followup-To", n_stdout);
883 if(hp->h_message_id != NULL) fputs(" Message-ID", n_stdout);
884 if(hp->h_ref != NULL) fputs(" References", n_stdout);
885 if(hp->h_in_reply_to != NULL) fputs(" In-Reply-To", n_stdout);
886 if(hp->h_mailx_command != NULL) fputs(" Mailx-Command", n_stdout);
887 if(hp->h_mailx_raw_to != NULL) fputs(" Mailx-Raw-To", n_stdout);
888 if(hp->h_mailx_raw_cc != NULL) fputs(" Mailx-Raw-Cc", n_stdout);
889 if(hp->h_mailx_raw_bcc != NULL) fputs(" Mailx-Raw-Bcc", n_stdout);
890 if(hp->h_mailx_orig_from != NULL) fputs(" Mailx-Orig-From", n_stdout);
891 if(hp->h_mailx_orig_to != NULL) fputs(" Mailx-Orig-To", n_stdout);
892 if(hp->h_mailx_orig_cc != NULL) fputs(" Mailx-Orig-Cc", n_stdout);
893 if(hp->h_mailx_orig_bcc != NULL) fputs(" Mailx-Orig-Bcc", n_stdout);
895 /* Print only one instance of each free-form header */
896 for(hfp = hp->h_user_headers; hfp != NULL; hfp = hfp->hf_next){
897 struct n_header_field *hfpx;
899 for(hfpx = hp->h_user_headers;; hfpx = hfpx->hf_next)
900 if(hfpx == hfp){
901 putc(' ', n_stdout);
902 fputs(&hfp->hf_dat[0], n_stdout);
903 break;
904 }else if(!asccasecmp(&hfpx->hf_dat[0], &hfp->hf_dat[0]))
905 break;
907 putc('\n', n_stdout);
908 goto jleave;
911 if(cmd[3] != NULL)
912 goto jecmd;
914 if(!asccasecmp(cmd[2], cp = "Subject")){
915 np = (hp->h_subject != NULL) ? (struct name*)-1 : NULL;
916 goto jlist;
918 if(!asccasecmp(cmd[2], cp = "From")){
919 np = hp->h_from;
920 jlist:
921 fprintf(n_stdout, "%s %s\n", (np == NULL ? "501" : "210"), cp);
922 goto jleave;
924 if(!asccasecmp(cmd[2], cp = "Sender")){
925 np = hp->h_sender;
926 goto jlist;
928 if(!asccasecmp(cmd[2], cp = "To")){
929 np = hp->h_to;
930 goto jlist;
932 if(!asccasecmp(cmd[2], cp = "Cc")){
933 np = hp->h_cc;
934 goto jlist;
936 if(!asccasecmp(cmd[2], cp = "Bcc")){
937 np = hp->h_bcc;
938 goto jlist;
940 if(!asccasecmp(cmd[2], cp = "Reply-To")){
941 np = hp->h_reply_to;
942 goto jlist;
944 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
945 np = hp->h_mft;
946 goto jlist;
948 if(!asccasecmp(cmd[2], cp = "Message-ID")){
949 np = hp->h_message_id;
950 goto jlist;
952 if(!asccasecmp(cmd[2], cp = "References")){
953 np = hp->h_ref;
954 goto jlist;
956 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
957 np = hp->h_in_reply_to;
958 goto jlist;
961 if(!asccasecmp(cmd[2], cp = "Mailx-Command")){
962 np = (hp->h_mailx_command != NULL) ? (struct name*)-1 : NULL;
963 goto jlist;
965 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-To")){
966 np = hp->h_mailx_raw_to;
967 goto jlist;
969 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-Cc")){
970 np = hp->h_mailx_raw_cc;
971 goto jlist;
973 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-Bcc")){
974 np = hp->h_mailx_raw_bcc;
975 goto jlist;
977 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-From")){
978 np = hp->h_mailx_orig_from;
979 goto jlist;
981 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-To")){
982 np = hp->h_mailx_orig_to;
983 goto jlist;
985 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-Cc")){
986 np = hp->h_mailx_orig_cc;
987 goto jlist;
989 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-Bcc")){
990 np = hp->h_mailx_orig_bcc;
991 goto jlist;
994 /* Free-form header fields */
995 for(cp = cmd[2]; *cp != '\0'; ++cp)
996 if(!fieldnamechar(*cp)){
997 cp = cmd[2];
998 goto j501cp;
1000 cp = cmd[2];
1001 for(hfp = hp->h_user_headers;; hfp = hfp->hf_next){
1002 if(hfp == NULL)
1003 goto j501cp;
1004 else if(!asccasecmp(cp, &hfp->hf_dat[0])){
1005 fprintf(n_stdout, "210 %s\n", &hfp->hf_dat[0]);
1006 break;
1009 goto jleave;
1012 if(is_asccaseprefix(cmd[1], "remove")){
1013 if(cmd[2] == NULL || cmd[3] != NULL)
1014 goto jecmd;
1016 if(!asccasecmp(cmd[2], cp = "Subject")){
1017 if(hp->h_subject != NULL){
1018 hp->h_subject = NULL;
1019 fprintf(n_stdout, "210 %s\n", cp);
1020 goto jleave;
1021 }else
1022 goto j501cp;
1025 if(!asccasecmp(cmd[2], cp = "From")){
1026 npp = &hp->h_from;
1027 jrem:
1028 if(*npp != NULL){
1029 *npp = NULL;
1030 fprintf(n_stdout, "210 %s\n", cp);
1031 goto jleave;
1032 }else
1033 goto j501cp;
1035 if(!asccasecmp(cmd[2], cp = "Sender")){
1036 npp = &hp->h_sender;
1037 goto jrem;
1039 if(!asccasecmp(cmd[2], cp = "To")){
1040 npp = &hp->h_to;
1041 goto jrem;
1043 if(!asccasecmp(cmd[2], cp = "Cc")){
1044 npp = &hp->h_cc;
1045 goto jrem;
1047 if(!asccasecmp(cmd[2], cp = "Bcc")){
1048 npp = &hp->h_bcc;
1049 goto jrem;
1051 if(!asccasecmp(cmd[2], cp = "Reply-To")){
1052 npp = &hp->h_reply_to;
1053 goto jrem;
1055 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
1056 npp = &hp->h_mft;
1057 goto jrem;
1059 if(!asccasecmp(cmd[2], cp = "Message-ID")){
1060 npp = &hp->h_message_id;
1061 goto jrem;
1063 if(!asccasecmp(cmd[2], cp = "References")){
1064 npp = &hp->h_ref;
1065 goto jrem;
1067 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
1068 npp = &hp->h_in_reply_to;
1069 goto jrem;
1072 if(!asccasecmp(cmd[2], cp = "Mailx-Command") ||
1073 !asccasecmp(cmd[2], cp = "Mailx-Raw-To") ||
1074 !asccasecmp(cmd[2], cp = "Mailx-Raw-Cc") ||
1075 !asccasecmp(cmd[2], cp = "Mailx-Raw-Bcc") ||
1076 !asccasecmp(cmd[2], cp = "Mailx-Orig-From") ||
1077 !asccasecmp(cmd[2], cp = "Mailx-Orig-To") ||
1078 !asccasecmp(cmd[2], cp = "Mailx-Orig-Cc") ||
1079 !asccasecmp(cmd[2], cp = "Mailx-Orig-Bcc")){
1080 fprintf(n_stdout, "505 %s\n", cp);
1081 goto jleave;
1084 /* Free-form header fields (note j501cp may print non-normalized name) */
1085 /* C99 */{
1086 struct n_header_field **hfpp;
1087 bool_t any;
1089 for(cp = cmd[2]; *cp != '\0'; ++cp)
1090 if(!fieldnamechar(*cp)){
1091 cp = cmd[2];
1092 goto j501cp;
1094 cp = cmd[2];
1096 for(any = FAL0, hfpp = &hp->h_user_headers; (hfp = *hfpp) != NULL;){
1097 if(!asccasecmp(cp, &hfp->hf_dat[0])){
1098 *hfpp = hfp->hf_next;
1099 if(!any)
1100 fprintf(n_stdout, "210 %s\n", &hfp->hf_dat[0]);
1101 any = TRU1;
1102 }else
1103 hfpp = &hfp->hf_next;
1105 if(!any)
1106 goto j501cp;
1108 goto jleave;
1111 if(is_asccaseprefix(cmd[1], "remove-at")){
1112 if(cmd[2] == NULL || cmd[3] == NULL)
1113 goto jecmd;
1115 if((n_idec_uiz_cp(&i, cmd[3], 0, NULL
1116 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
1117 ) != n_IDEC_STATE_CONSUMED || i == 0){
1118 fputs("505\n", n_stdout);
1119 goto jleave;
1122 if(!asccasecmp(cmd[2], cp = "Subject")){
1123 if(hp->h_subject != NULL && i == 1){
1124 hp->h_subject = NULL;
1125 fprintf(n_stdout, "210 %s 1\n", cp);
1126 goto jleave;
1127 }else
1128 goto j501cp;
1131 if(!asccasecmp(cmd[2], cp = "From")){
1132 npp = &hp->h_from;
1133 jremat:
1134 if((np = *npp) == NULL)
1135 goto j501cp;
1136 while(--i != 0 && np != NULL)
1137 np = np->n_flink;
1138 if(np == NULL)
1139 goto j501cp;
1141 if(np->n_blink != NULL)
1142 np->n_blink->n_flink = np->n_flink;
1143 else
1144 *npp = np->n_flink;
1145 if(np->n_flink != NULL)
1146 np->n_flink->n_blink = np->n_blink;
1148 fprintf(n_stdout, "210 %s\n", cp);
1149 goto jleave;
1151 if(!asccasecmp(cmd[2], cp = "Sender")){
1152 npp = &hp->h_sender;
1153 goto jremat;
1155 if(!asccasecmp(cmd[2], cp = "To")){
1156 npp = &hp->h_to;
1157 goto jremat;
1159 if(!asccasecmp(cmd[2], cp = "Cc")){
1160 npp = &hp->h_cc;
1161 goto jremat;
1163 if(!asccasecmp(cmd[2], cp = "Bcc")){
1164 npp = &hp->h_bcc;
1165 goto jremat;
1167 if(!asccasecmp(cmd[2], cp = "Reply-To")){
1168 npp = &hp->h_reply_to;
1169 goto jremat;
1171 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
1172 npp = &hp->h_mft;
1173 goto jremat;
1175 if(!asccasecmp(cmd[2], cp = "Message-ID")){
1176 npp = &hp->h_message_id;
1177 goto jremat;
1179 if(!asccasecmp(cmd[2], cp = "References")){
1180 npp = &hp->h_ref;
1181 goto jremat;
1183 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
1184 npp = &hp->h_in_reply_to;
1185 goto jremat;
1188 if(!asccasecmp(cmd[2], cp = "Mailx-Command") ||
1189 !asccasecmp(cmd[2], cp = "Mailx-Raw-To") ||
1190 !asccasecmp(cmd[2], cp = "Mailx-Raw-Cc") ||
1191 !asccasecmp(cmd[2], cp = "Mailx-Raw-Bcc") ||
1192 !asccasecmp(cmd[2], cp = "Mailx-Orig-From") ||
1193 !asccasecmp(cmd[2], cp = "Mailx-Orig-To") ||
1194 !asccasecmp(cmd[2], cp = "Mailx-Orig-Cc") ||
1195 !asccasecmp(cmd[2], cp = "Mailx-Orig-Bcc")){
1196 fprintf(n_stdout, "505 %s\n", cp);
1197 goto jleave;
1200 /* Free-form header fields */
1201 /* C99 */{
1202 struct n_header_field **hfpp;
1204 for(cp = cmd[2]; *cp != '\0'; ++cp)
1205 if(!fieldnamechar(*cp)){
1206 cp = cmd[2];
1207 goto j501cp;
1209 cp = cmd[2];
1211 for(hfpp = &hp->h_user_headers; (hfp = *hfpp) != NULL;){
1212 if(--i == 0){
1213 *hfpp = hfp->hf_next;
1214 fprintf(n_stdout, "210 %s %" PRIuZ "\n", &hfp->hf_dat[0], i);
1215 break;
1216 }else
1217 hfpp = &hfp->hf_next;
1219 if(hfp == NULL)
1220 goto j501cp;
1222 goto jleave;
1225 if(is_asccaseprefix(cmd[1], "show")){
1226 if(cmd[2] == NULL || cmd[3] != NULL)
1227 goto jecmd;
1229 if(!asccasecmp(cmd[2], cp = "Subject")){
1230 if(hp->h_subject == NULL)
1231 goto j501cp;
1232 fprintf(n_stdout, "212 %s\n%s\n\n", cp, hp->h_subject);
1233 goto jleave;
1236 if(!asccasecmp(cmd[2], cp = "From")){
1237 np = hp->h_from;
1238 jshow:
1239 if(np != NULL){
1240 fprintf(n_stdout, "211 %s\n", cp);
1241 do if(!(np->n_type & GDEL))
1242 fprintf(n_stdout, "%s %s\n", np->n_name, np->n_fullname);
1243 while((np = np->n_flink) != NULL);
1244 putc('\n', n_stdout);
1245 goto jleave;
1246 }else
1247 goto j501cp;
1249 if(!asccasecmp(cmd[2], cp = "Sender")){
1250 np = hp->h_sender;
1251 goto jshow;
1253 if(!asccasecmp(cmd[2], cp = "To")){
1254 np = hp->h_to;
1255 goto jshow;
1257 if(!asccasecmp(cmd[2], cp = "Cc")){
1258 np = hp->h_cc;
1259 goto jshow;
1261 if(!asccasecmp(cmd[2], cp = "Bcc")){
1262 np = hp->h_bcc;
1263 goto jshow;
1265 if(!asccasecmp(cmd[2], cp = "Reply-To")){
1266 np = hp->h_reply_to;
1267 goto jshow;
1269 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
1270 np = hp->h_mft;
1271 goto jshow;
1273 if(!asccasecmp(cmd[2], cp = "Message-ID")){
1274 np = hp->h_message_id;
1275 goto jshow;
1277 if(!asccasecmp(cmd[2], cp = "References")){
1278 np = hp->h_ref;
1279 goto jshow;
1281 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
1282 np = hp->h_in_reply_to;
1283 goto jshow;
1286 if(!asccasecmp(cmd[2], cp = "Mailx-Command")){
1287 if(hp->h_mailx_command == NULL)
1288 goto j501cp;
1289 fprintf(n_stdout, "212 %s\n%s\n\n", cp, hp->h_mailx_command);
1290 goto jleave;
1292 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-To")){
1293 np = hp->h_mailx_raw_to;
1294 goto jshow;
1296 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-Cc")){
1297 np = hp->h_mailx_raw_cc;
1298 goto jshow;
1300 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-Bcc")){
1301 np = hp->h_mailx_raw_bcc;
1302 goto jshow;
1304 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-From")){
1305 np = hp->h_mailx_orig_from;
1306 goto jshow;
1308 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-To")){
1309 np = hp->h_mailx_orig_to;
1310 goto jshow;
1312 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-Cc")){
1313 np = hp->h_mailx_orig_cc;
1314 goto jshow;
1316 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-Bcc")){
1317 np = hp->h_mailx_orig_bcc;
1318 goto jshow;
1321 /* Free-form header fields */
1322 /* C99 */{
1323 bool_t any;
1325 for(cp = cmd[2]; *cp != '\0'; ++cp)
1326 if(!fieldnamechar(*cp)){
1327 cp = cmd[2];
1328 goto j501cp;
1330 cp = cmd[2];
1332 for(any = FAL0, hfp = hp->h_user_headers; hfp != NULL;
1333 hfp = hfp->hf_next){
1334 if(!asccasecmp(cp, &hfp->hf_dat[0])){
1335 if(!any)
1336 fprintf(n_stdout, "212 %s\n", &hfp->hf_dat[0]);
1337 any = TRU1;
1338 fprintf(n_stdout, "%s\n", &hfp->hf_dat[hfp->hf_nl +1]);
1341 if(any)
1342 putc('\n', n_stdout);
1343 else
1344 goto j501cp;
1346 goto jleave;
1349 jecmd:
1350 fputs("500\n", n_stdout);
1351 cp = NULL;
1352 jleave:
1353 NYD2_LEAVE;
1354 return (cp != NULL);
1356 j501cp:
1357 fputs("501 ", n_stdout);
1358 fputs(cp, n_stdout);
1359 putc('\n', n_stdout);
1360 goto jleave;
1363 static bool_t
1364 a_collect__plumb_attach(char const *cp, struct header *hp,
1365 char const *cmd[4]){
1366 bool_t status;
1367 struct attachment *ap;
1368 NYD2_ENTER;
1370 if(cmd[1] == NULL)
1371 goto jdefault;
1373 if(is_asccaseprefix(cmd[1], "attribute")){
1374 if(cmd[2] == NULL || cmd[3] != NULL)
1375 goto jecmd;
1377 if((ap = n_attachment_find(hp->h_attach, cmd[2], NULL)) != NULL){
1378 jatt_att:
1379 fprintf(n_stdout, "212 %s\n", cmd[2]);
1380 if(ap->a_msgno > 0)
1381 fprintf(n_stdout, "message-number %d\n\n", ap->a_msgno);
1382 else{
1383 fprintf(n_stdout,
1384 "creation-name %s\nopen-path %s\nfilename %s\n",
1385 ap->a_path_user, ap->a_path, ap->a_name);
1386 if(ap->a_content_description != NULL)
1387 fprintf(n_stdout, "content-description %s\n",
1388 ap->a_content_description);
1389 if(ap->a_content_id != NULL)
1390 fprintf(n_stdout, "content-id %s\n",
1391 ap->a_content_id->n_name);
1392 if(ap->a_content_type != NULL)
1393 fprintf(n_stdout, "content-type %s\n", ap->a_content_type);
1394 if(ap->a_content_disposition != NULL)
1395 fprintf(n_stdout, "content-disposition %s\n",
1396 ap->a_content_disposition);
1397 putc('\n', n_stdout);
1399 }else
1400 fputs("501\n", n_stdout);
1401 goto jleave;
1404 if(is_asccaseprefix(cmd[1], "attribute-at")){
1405 uiz_t i;
1407 if(cmd[2] == NULL || cmd[3] != NULL)
1408 goto jecmd;
1410 if((n_idec_uiz_cp(&i, cmd[2], 0, NULL
1411 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
1412 ) != n_IDEC_STATE_CONSUMED || i == 0)
1413 fputs("505\n", n_stdout);
1414 else{
1415 for(ap = hp->h_attach; ap != NULL && --i != 0; ap = ap->a_flink)
1417 if(ap != NULL)
1418 goto jatt_att;
1419 else
1420 fputs("501\n", n_stdout);
1422 goto jleave;
1425 if(is_asccaseprefix(cmd[1], "attribute-set")){
1426 if(cmd[2] == NULL || cmd[3] == NULL)
1427 goto jecmd;
1429 if((ap = n_attachment_find(hp->h_attach, cmd[2], NULL)) != NULL){
1430 jatt_attset:
1431 if(ap->a_msgno > 0)
1432 fputs("505\n", n_stdout);
1433 else{
1434 char c, *keyw;
1436 cp = cmd[3];
1437 while((c = *cp) != '\0' && !blankchar(c))
1438 ++cp;
1439 keyw = savestrbuf(cmd[3], PTR2SIZE(cp - cmd[3]));
1440 if(c != '\0'){
1441 for(; (c = *++cp) != '\0' && blankchar(c);)
1443 if(c != '\0'){
1444 char *xp;
1446 /* Strip [\r\n] which would render a parameter invalid XXX
1447 * XXX all controls? */
1448 cp = xp = savestr(cp);
1449 for(; (c = *xp) != '\0'; ++xp)
1450 if(c == '\n' || c == '\r')
1451 *xp = ' ';
1452 c = *cp;
1456 if(!asccasecmp(keyw, "filename"))
1457 ap->a_name = (c == '\0') ? ap->a_path_bname : cp;
1458 else if(!asccasecmp(keyw, "content-description"))
1459 ap->a_content_description = (c == '\0') ? NULL : cp;
1460 else if(!asccasecmp(keyw, "content-id")){
1461 ap->a_content_id = NULL;
1463 if(c != '\0'){
1464 struct name *np;
1466 np = checkaddrs(lextract(cp, GREF),
1467 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG |
1468 EACM_NONAME, NULL);
1469 if(np != NULL && np->n_flink == NULL)
1470 ap->a_content_id = np;
1471 else
1472 cp = NULL;
1474 }else if(!asccasecmp(keyw, "content-type"))
1475 ap->a_content_type = (c == '\0') ? NULL : cp;
1476 else if(!asccasecmp(keyw, "content-disposition"))
1477 ap->a_content_disposition = (c == '\0') ? NULL : cp;
1478 else
1479 cp = NULL;
1481 if(cp != NULL){
1482 size_t i;
1484 for(i = 0; ap != NULL; ++i, ap = ap->a_blink)
1486 fprintf(n_stdout, "210 %" PRIuZ "\n", i);
1487 }else
1488 fputs("505\n", n_stdout);
1490 }else
1491 fputs("501\n", n_stdout);
1492 goto jleave;
1495 if(is_asccaseprefix(cmd[1], "attribute-set-at")){
1496 uiz_t i;
1498 if(cmd[2] == NULL || cmd[3] == NULL)
1499 goto jecmd;
1501 if((n_idec_uiz_cp(&i, cmd[2], 0, NULL
1502 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
1503 ) != n_IDEC_STATE_CONSUMED || i == 0)
1504 fputs("505\n", n_stdout);
1505 else{
1506 for(ap = hp->h_attach; ap != NULL && --i != 0; ap = ap->a_flink)
1508 if(ap != NULL)
1509 goto jatt_attset;
1510 else
1511 fputs("501\n", n_stdout);
1513 goto jleave;
1516 if(is_asccaseprefix(cmd[1], "insert")){
1517 enum n_attach_error aerr;
1519 if(cmd[2] == NULL || cmd[3] != NULL)
1520 goto jecmd;
1522 hp->h_attach = n_attachment_append(hp->h_attach, cmd[2], &aerr, &ap);
1523 switch(aerr){
1524 case n_ATTACH_ERR_FILE_OPEN: cp = "505\n"; goto jatt_ins;
1525 case n_ATTACH_ERR_ICONV_FAILED: cp = "506\n"; goto jatt_ins;
1526 case n_ATTACH_ERR_ICONV_NAVAIL:
1527 case n_ATTACH_ERR_OTHER:
1528 default:
1529 cp = "501\n";
1530 jatt_ins:
1531 fputs(cp, n_stdout);
1532 break;
1533 case n_ATTACH_ERR_NONE:{
1534 size_t i;
1536 for(i = 0; ap != NULL; ++i, ap = ap->a_blink)
1538 fprintf(n_stdout, "210 %" PRIuZ "\n", i);
1539 }break;
1541 goto jleave;
1544 if(is_asccaseprefix(cmd[1], "list")){
1545 jdefault:
1546 if(cmd[2] != NULL)
1547 goto jecmd;
1549 if((ap = hp->h_attach) != NULL){
1550 fputs("212\n", n_stdout);
1552 fprintf(n_stdout, "%s\n", ap->a_path_user);
1553 while((ap = ap->a_flink) != NULL);
1554 putc('\n', n_stdout);
1555 }else
1556 fputs("501\n", n_stdout);
1557 goto jleave;
1560 if(is_asccaseprefix(cmd[1], "remove")){
1561 if(cmd[2] == NULL || cmd[3] != NULL)
1562 goto jecmd;
1564 if((ap = n_attachment_find(hp->h_attach, cmd[2], &status)) != NULL){
1565 if(status == TRUM1)
1566 fputs("506\n", n_stdout);
1567 else{
1568 hp->h_attach = n_attachment_remove(hp->h_attach, ap);
1569 fprintf(n_stdout, "210 %s\n", cmd[2]);
1571 }else
1572 fputs("501\n", n_stdout);
1573 goto jleave;
1576 if(is_asccaseprefix(cmd[1], "remove-at")){
1577 uiz_t i;
1579 if(cmd[2] == NULL || cmd[3] != NULL)
1580 goto jecmd;
1582 if((n_idec_uiz_cp(&i, cmd[2], 0, NULL
1583 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
1584 ) != n_IDEC_STATE_CONSUMED || i == 0)
1585 fputs("505\n", n_stdout);
1586 else{
1587 for(ap = hp->h_attach; ap != NULL && --i != 0; ap = ap->a_flink)
1589 if(ap != NULL){
1590 hp->h_attach = n_attachment_remove(hp->h_attach, ap);
1591 fprintf(n_stdout, "210 %s\n", cmd[2]);
1592 }else
1593 fputs("501\n", n_stdout);
1595 goto jleave;
1598 jecmd:
1599 fputs("500\n", n_stdout);
1600 cp = NULL;
1601 jleave:
1602 NYD2_LEAVE;
1603 return (cp != NULL);
1606 static void
1607 _collint(int s)
1609 NYD_X; /* Signal handler */
1611 /* the control flow is subtle, because we can be called from ~q */
1612 if (_coll_hadintr == 0) {
1613 if (ok_blook(ignore)) {
1614 fputs("@\n", n_stdout);
1615 fflush(n_stdout);
1616 clearerr(n_stdin);
1617 } else
1618 _coll_hadintr = 1;
1619 siglongjmp(_coll_jmp, 1);
1621 n_exit_status |= n_EXIT_SEND_ERROR;
1622 if (s != 0)
1623 savedeadletter(_coll_fp, TRU1);
1624 /* Aborting message, no need to fflush() .. */
1625 siglongjmp(_coll_abort, 1);
1628 static void
1629 collhup(int s)
1631 NYD_X; /* Signal handler */
1632 n_UNUSED(s);
1634 savedeadletter(_coll_fp, TRU1);
1635 /* Let's pretend nobody else wants to clean up, a true statement at
1636 * this time */
1637 exit(n_EXIT_ERR);
1640 static bool_t
1641 a_coll_putesc(char const *s, bool_t addnl, FILE *stream){
1642 char c1, c2;
1643 bool_t isposix;
1644 NYD2_ENTER;
1646 isposix = ok_blook(posix);
1648 while((c1 = *s++) != '\0'){
1649 if(c1 == '\\' && ((c2 = *s) == 't' || c2 == 'n')){
1650 if(!isposix){
1651 isposix = TRU1; /* TODO v15 OBSOLETE! */
1652 n_err(_("Compose mode warning: expanding \\t or \\n in variable "
1653 "without *posix*!"
1654 "\n Support remains only for ~A,~a,~I,~i in *posix* mode!\n"));
1656 ++s;
1657 c1 = (c2 == 't') ? '\t' : '\n';
1660 if(putc(c1, stream) == EOF)
1661 goto jleave;
1664 if(addnl && putc('\n', stream) == EOF)
1665 goto jleave;
1667 jleave:
1668 NYD2_LEAVE;
1669 return (c1 == '\0');
1672 static int
1673 a_coll_ocs__mac(void){
1674 /* Executes in a fork(2)ed child TODO if remains, global MASKs for those! */
1675 setvbuf(n_stdin, NULL, _IOLBF, 0);
1676 setvbuf(n_stdout, NULL, _IOLBF, 0);
1677 n_psonce &= ~(n_PSO_INTERACTIVE | n_PSO_TTYIN | n_PSO_TTYOUT);
1678 n_pstate |= n_PS_COMPOSE_FORKHOOK;
1679 n_readctl_overlay = NULL; /* TODO we need OnForkEvent! See c_readctl() */
1680 if(n_poption & n_PO_D_VV){
1681 char buf[128];
1683 snprintf(buf, sizeof buf, "[%d]%s", getpid(), ok_vlook(log_prefix));
1684 ok_vset(log_prefix, buf);
1686 /* TODO If that uses `!' it will effectively SIG_IGN SIGINT, ...and such */
1687 temporary_compose_mode_hook_call(a_coll_ocs__macname, NULL, NULL);
1688 return 0;
1691 static void
1692 a_coll_ocs__finalize(void *vp){
1693 /* Note we use this for destruction upon setup errors, thus */
1694 sighandler_type opipe;
1695 sighandler_type oint;
1696 struct a_coll_ocs_arg **coapp, *coap;
1697 NYD2_ENTER;
1699 temporary_compose_mode_hook_call((char*)-1, NULL, NULL);
1701 coap = *(coapp = vp);
1702 *coapp = (struct a_coll_ocs_arg*)-1;
1704 if(coap->coa_stdin != NULL)
1705 Fclose(coap->coa_stdin);
1706 else if(coap->coa_pipe[0] != -1)
1707 close(coap->coa_pipe[0]);
1709 if(coap->coa_stdout != NULL && !Pclose(coap->coa_stdout, TRU1))
1710 *coap->coa_senderr = 111;
1711 if(coap->coa_pipe[1] != -1)
1712 close(coap->coa_pipe[1]);
1714 opipe = coap->coa_opipe;
1715 oint = coap->coa_oint;
1717 n_lofi_free(coap);
1719 hold_all_sigs();
1720 safe_signal(SIGPIPE, opipe);
1721 safe_signal(SIGINT, oint);
1722 rele_all_sigs();
1723 NYD2_LEAVE;
1726 FL void
1727 n_temporary_compose_hook_varset(void *arg){ /* TODO v15: drop */
1728 struct header *hp;
1729 char const *val;
1730 NYD2_ENTER;
1732 hp = arg;
1734 if((val = hp->h_subject) == NULL)
1735 val = n_empty;
1736 ok_vset(mailx_subject, val);
1737 if((val = detract(hp->h_from, GNAMEONLY)) == NULL)
1738 val = n_empty;
1739 ok_vset(mailx_from, val);
1740 if((val = detract(hp->h_sender, GNAMEONLY)) == NULL)
1741 val = n_empty;
1742 ok_vset(mailx_sender, val);
1743 if((val = detract(hp->h_to, GNAMEONLY)) == NULL)
1744 val = n_empty;
1745 ok_vset(mailx_to, val);
1746 if((val = detract(hp->h_cc, GNAMEONLY)) == NULL)
1747 val = n_empty;
1748 ok_vset(mailx_cc, val);
1749 if((val = detract(hp->h_bcc, GNAMEONLY)) == NULL)
1750 val = n_empty;
1751 ok_vset(mailx_bcc, val);
1753 if((val = hp->h_mailx_command) == NULL)
1754 val = n_empty;
1755 ok_vset(mailx_command, val);
1757 if((val = detract(hp->h_mailx_raw_to, GNAMEONLY)) == NULL)
1758 val = n_empty;
1759 ok_vset(mailx_raw_to, val);
1760 if((val = detract(hp->h_mailx_raw_cc, GNAMEONLY)) == NULL)
1761 val = n_empty;
1762 ok_vset(mailx_raw_cc, val);
1763 if((val = detract(hp->h_mailx_raw_bcc, GNAMEONLY)) == NULL)
1764 val = n_empty;
1765 ok_vset(mailx_raw_bcc, val);
1767 if((val = detract(hp->h_mailx_orig_from, GNAMEONLY)) == NULL)
1768 val = n_empty;
1769 ok_vset(mailx_orig_from, val);
1770 if((val = detract(hp->h_mailx_orig_to, GNAMEONLY)) == NULL)
1771 val = n_empty;
1772 ok_vset(mailx_orig_to, val);
1773 if((val = detract(hp->h_mailx_orig_cc, GNAMEONLY)) == NULL)
1774 val = n_empty;
1775 ok_vset(mailx_orig_cc, val);
1776 if((val = detract(hp->h_mailx_orig_bcc, GNAMEONLY)) == NULL)
1777 val = n_empty;
1778 ok_vset(mailx_orig_bcc, val);
1779 NYD2_LEAVE;
1782 FL FILE *
1783 collect(struct header *hp, int printheaders, struct message *mp,
1784 char const *quotefile, int doprefix, si8_t *checkaddr_err)
1786 struct n_string s, * volatile sp;
1787 struct n_ignore const *quoteitp;
1788 struct a_coll_ocs_arg *coap;
1789 int c;
1790 int volatile t, eofcnt, getfields;
1791 char volatile escape;
1792 enum{
1793 a_NONE,
1794 a_ERREXIT = 1u<<0,
1795 a_IGNERR = 1u<<1,
1796 a_COAP_NOSIGTERM = 1u<<8
1797 #define a_HARDERR() ((flags & (a_ERREXIT | a_IGNERR)) == a_ERREXIT)
1798 } volatile flags;
1799 char *linebuf;
1800 char const *cp, *cp_base, * volatile coapm, * volatile ifs_saved;
1801 size_t i, linesize; /* TODO line pool */
1802 long cnt;
1803 enum sendaction action;
1804 sigset_t oset, nset;
1805 FILE * volatile sigfp;
1806 NYD_ENTER;
1808 _coll_fp = NULL;
1809 sigfp = NULL;
1810 linesize = 0;
1811 linebuf = NULL;
1812 eofcnt = 0;
1813 ifs_saved = coapm = NULL;
1814 coap = NULL;
1815 sp = NULL;
1817 /* Start catching signals from here, but we're still die on interrupts
1818 * until we're in the main loop */
1819 sigfillset(&nset);
1820 sigprocmask(SIG_BLOCK, &nset, &oset);
1821 if ((_coll_saveint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
1822 safe_signal(SIGINT, &_collint);
1823 if ((_coll_savehup = safe_signal(SIGHUP, SIG_IGN)) != SIG_IGN)
1824 safe_signal(SIGHUP, collhup);
1825 if (sigsetjmp(_coll_abort, 1))
1826 goto jerr;
1827 if (sigsetjmp(_coll_jmp, 1))
1828 goto jerr;
1829 n_pstate |= n_PS_COMPOSE_MODE;
1830 sigprocmask(SIG_SETMASK, &oset, (sigset_t*)NULL);
1832 if ((_coll_fp = Ftmp(NULL, "collect", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
1833 NULL) {
1834 n_perr(_("temporary mail file"), 0);
1835 goto jerr;
1838 /* If we are going to prompt for a subject, refrain from printing a newline
1839 * after the headers (since some people mind) */
1840 getfields = 0;
1841 if(!(n_poption & n_PO_t_FLAG)){
1842 t = GTO | GSUBJECT | GCC | GNL;
1843 if(ok_blook(fullnames))
1844 t |= GCOMMA;
1846 if((n_psonce & n_PSO_INTERACTIVE) && !(n_pstate & n_PS_ROBOT)){
1847 if(hp->h_subject == NULL && ok_blook(asksub)/* *ask* auto warped! */)
1848 t &= ~GNL, getfields |= GSUBJECT;
1850 if(hp->h_to == NULL)
1851 t &= ~GNL, getfields |= GTO;
1853 if(!ok_blook(bsdcompat) && !ok_blook(askatend)){
1854 if(ok_blook(askbcc))
1855 t &= ~GNL, getfields |= GBCC;
1856 if(ok_blook(askcc))
1857 t &= ~GNL, getfields |= GCC;
1860 }else{
1861 n_UNINIT(t, 0);
1864 _coll_hadintr = 0;
1866 if (!sigsetjmp(_coll_jmp, 1)) {
1867 /* Ask for some headers first, as necessary */
1868 if (getfields)
1869 grab_headers(n_GO_INPUT_CTX_COMPOSE, hp, getfields, 1);
1871 /* Execute compose-enter; delayed for -t mode */
1872 if(!(n_poption & n_PO_t_FLAG) &&
1873 (cp = ok_vlook(on_compose_enter)) != NULL){
1874 setup_from_and_sender(hp);
1875 temporary_compose_mode_hook_call(cp, &n_temporary_compose_hook_varset,
1876 hp);
1879 /* TODO Mm: nope since it may require turning this into a multipart one */
1880 if(!(n_poption & (n_PO_Mm_FLAG | n_PO_t_FLAG))){
1881 if(!a_coll_message_inject_head(_coll_fp))
1882 goto jerr;
1884 /* Quote an original message */
1885 if (mp != NULL && (doprefix || (cp = ok_vlook(quote)) != NULL)) {
1886 quoteitp = n_IGNORE_ALL;
1887 action = SEND_QUOTE;
1888 if (doprefix) {
1889 char const *cp_v15compat;
1891 quoteitp = n_IGNORE_FWD;
1893 if((cp_v15compat = ok_vlook(fwdheading)) != NULL)
1894 n_OBSOLETE(_("please use *forward-inject-head*, "
1895 "not *fwdheading*"));
1896 cp = ok_vlook(forward_inject_head);
1897 if(cp == NULL && (cp = cp_v15compat) == NULL)
1898 cp = "-------- Original Message --------";
1899 if (*cp != '\0' && fprintf(_coll_fp, "%s\n", cp) < 0)
1900 goto jerr;
1901 } else if (!strcmp(cp, "noheading")) {
1902 /*EMPTY*/;
1903 } else if (!strcmp(cp, "headers")) {
1904 quoteitp = n_IGNORE_TYPE;
1905 } else if (!strcmp(cp, "allheaders")) {
1906 quoteitp = NULL;
1907 action = SEND_QUOTE_ALL;
1908 } else {
1909 cp = hfield1("from", mp);
1910 if (cp != NULL && (cnt = (long)strlen(cp)) > 0) {
1911 if (xmime_write(cp, cnt, _coll_fp, CONV_FROMHDR, TD_NONE,
1912 NULL, NULL) < 0)
1913 goto jerr;
1914 if (fprintf(_coll_fp, _(" wrote:\n\n")) < 0)
1915 goto jerr;
1918 if (fflush(_coll_fp))
1919 goto jerr;
1920 if (sendmp(mp, _coll_fp, quoteitp,
1921 (doprefix ? NULL : ok_vlook(indentprefix)),
1922 action, NULL) < 0)
1923 goto jerr;
1927 if (quotefile != NULL) {
1928 if((n_pstate_err_no = a_coll_include_file(quotefile, FAL0, FAL0)
1929 ) != n_ERR_NONE)
1930 goto jerr;
1933 if(n_psonce & n_PSO_INTERACTIVE){
1934 if(!(n_pstate & n_PS_SOURCING)){
1935 sp = n_string_creat_auto(&s);
1936 sp = n_string_reserve(sp, 80);
1939 if(!(n_poption & n_PO_Mm_FLAG) && !(n_pstate & n_PS_ROBOT)){
1940 /* Print what we have sofar also on the terminal (if useful) */
1941 if (!ok_blook(editalong)) {
1942 if (printheaders)
1943 puthead(TRU1, hp, n_stdout, t,
1944 SEND_TODISP, CONV_NONE, NULL, NULL);
1946 rewind(_coll_fp);
1947 while ((c = getc(_coll_fp)) != EOF) /* XXX bytewise, yuck! */
1948 putc(c, n_stdout);
1949 if (fseek(_coll_fp, 0, SEEK_END))
1950 goto jerr;
1951 } else {
1952 rewind(_coll_fp);
1953 if(a_coll_edit('e', hp) != n_ERR_NONE)
1954 goto jerr;
1955 /* Print msg mandated by the Mail Reference Manual */
1956 jcont:
1957 if((n_psonce & n_PSO_INTERACTIVE) && !(n_pstate & n_PS_ROBOT))
1958 fputs(_("(continue)\n"), n_stdout);
1960 fflush(n_stdout);
1963 } else {
1964 /* Come here for printing the after-signal message. Duplicate messages
1965 * won't be printed because the write is aborted if we get a SIGTTOU */
1966 if(_coll_hadintr && (n_psonce & n_PSO_INTERACTIVE) &&
1967 !(n_pstate & n_PS_ROBOT))
1968 n_err(_("\n(Interrupt -- one more to kill letter)\n"));
1971 /* If not under shell hook control */
1972 if(coap == NULL){
1973 /* We're done with -M or -m TODO because: we are too stupid yet, above */
1974 if(n_poption & n_PO_Mm_FLAG)
1975 goto jout;
1976 /* No command escapes, interrupts not expected. Simply copy STDIN */
1977 if(!(n_psonce & n_PSO_INTERACTIVE) &&
1978 !(n_poption & (n_PO_t_FLAG | n_PO_TILDE_FLAG))){
1979 linebuf = srealloc(linebuf, linesize = LINESIZE);
1980 while ((i = fread(linebuf, sizeof *linebuf, linesize, n_stdin)) > 0) {
1981 if (i != fwrite(linebuf, sizeof *linebuf, i, _coll_fp))
1982 goto jerr;
1984 goto jout;
1988 /* The interactive collect loop */
1989 if(coap == NULL)
1990 escape = *ok_vlook(escape);
1991 flags = ok_blook(errexit) ? a_ERREXIT : a_NONE;
1993 for(;;){
1994 enum {a_HIST_NONE, a_HIST_ADD = 1u<<0, a_HIST_GABBY = 1u<<1} hist;
1996 /* C99 */{
1997 enum n_go_input_flags gif;
1998 bool_t histadd;
2000 gif = n_GO_INPUT_CTX_COMPOSE;
2001 histadd = (sp != NULL);
2002 if((n_psonce & n_PSO_INTERACTIVE) || (n_poption & n_PO_TILDE_FLAG)){
2003 if(!(n_poption & n_PO_t_FLAG))
2004 gif |= n_GO_INPUT_NL_ESC;
2006 cnt = n_go_input(gif, n_empty, &linebuf, &linesize, NULL, &histadd);
2007 hist = histadd ? a_HIST_ADD | a_HIST_GABBY : a_HIST_NONE;
2010 if(cnt < 0){
2011 if(coap != NULL)
2012 break;
2013 if(n_poption & n_PO_t_FLAG){
2014 fflush_rewind(_coll_fp);
2015 /* It is important to set n_PSO_t_FLAG before extract_header()
2016 * *and* keep n_PO_t_FLAG for the first parse of the message!
2017 * TODO This is a hack, we need a clean approach for this */
2018 n_psonce |= n_PSO_t_FLAG;
2019 if(!a_coll_makeheader(_coll_fp, hp, checkaddr_err, TRU1))
2020 goto jerr;
2021 n_poption &= ~n_PO_t_FLAG;
2022 continue;
2023 }else if((n_psonce & n_PSO_INTERACTIVE) && !(n_pstate & n_PS_ROBOT) &&
2024 ok_blook(ignoreeof) && ++eofcnt < 4){
2025 fprintf(n_stdout,
2026 _("*ignoreeof* set, use `~.' to terminate letter\n"));
2027 n_go_input_clearerr();
2028 continue;
2030 break;
2033 _coll_hadintr = 0;
2035 cp = linebuf;
2036 if(cnt == 0)
2037 goto jputnl;
2038 else if(coap == NULL){
2039 if(!(n_psonce & n_PSO_INTERACTIVE) && !(n_poption & n_PO_TILDE_FLAG))
2040 goto jputline;
2041 else if(cp[0] == '.'){
2042 if(cnt == 1 && (ok_blook(dot) ||
2043 (ok_blook(posix) && ok_blook(ignoreeof))))
2044 break;
2047 if(cp[0] != escape){
2048 jputline:
2049 if(fwrite(cp, sizeof *cp, cnt, _coll_fp) != (size_t)cnt)
2050 goto jerr;
2051 /* TODO n_PS_READLINE_NL is a terrible hack to ensure that _in_all_-
2052 * TODO _code_paths_ a file without trailing newline isn't modified
2053 * TODO to contain one; the "saw-newline" needs to be part of an
2054 * TODO I/O input machinery object */
2055 jputnl:
2056 if(n_pstate & n_PS_READLINE_NL){
2057 if(putc('\n', _coll_fp) == EOF)
2058 goto jerr;
2060 continue;
2063 c = *(cp_base = ++cp);
2064 if(--cnt == 0)
2065 goto jearg;
2067 /* Avoid history entry? */
2068 while(spacechar(c)){
2069 hist = a_HIST_NONE;
2070 c = *(cp_base = ++cp);
2071 if(--cnt == 0)
2072 goto jearg;
2075 /* It may just be an escaped escaped character, do that quick */
2076 if(c == escape)
2077 goto jputline;
2079 /* Avoid hard *errexit*? */
2080 flags &= ~a_IGNERR;
2081 if(c == '-'){
2082 flags ^= a_IGNERR;
2083 c = *++cp;
2084 if(--cnt == 0)
2085 goto jearg;
2088 /* Trim input, also to gain a somewhat normalized history entry */
2089 ++cp;
2090 if(--cnt > 0){
2091 struct str x;
2093 x.s = n_UNCONST(cp);
2094 x.l = (size_t)cnt;
2095 n_str_trim_ifs(&x, TRU1);
2096 x.s[x.l] = '\0';
2097 cp = x.s;
2098 cnt = (int)/*XXX*/x.l;
2101 if(hist != a_HIST_NONE){
2102 sp = n_string_assign_c(sp, escape);
2103 if(flags & a_IGNERR)
2104 sp = n_string_push_c(sp, '-');
2105 sp = n_string_push_c(sp, c);
2106 if(cnt > 0){
2107 sp = n_string_push_c(sp, ' ');
2108 sp = n_string_push_buf(sp, cp, cnt);
2112 /* Switch over all command escapes */
2113 switch(c){
2114 default:
2115 if(1){
2116 char buf[sizeof(n_UNIREPL)];
2118 if(asciichar(c))
2119 buf[0] = c, buf[1] = '\0';
2120 else if(n_psonce & n_PSO_UNICODE)
2121 memcpy(buf, n_unirepl, sizeof n_unirepl);
2122 else
2123 buf[0] = '?', buf[1] = '\0';
2124 n_err(_("Unknown command escape: `%c%s'\n"), escape, buf);
2125 }else
2126 jearg:
2127 n_err(_("Invalid command escape usage: %s\n"),
2128 n_shexp_quote_cp(linebuf, FAL0));
2129 if(a_HARDERR())
2130 goto jerr;
2131 n_pstate_err_no = n_ERR_INVAL;
2132 n_pstate_ex_no = 1;
2133 continue;
2134 case '!':
2135 /* Shell escape, send the balance of line to sh -c */
2136 if(cnt == 0 || coap != NULL)
2137 goto jearg;
2138 else{
2139 char const *argv[2];
2141 argv[0] = cp;
2142 argv[1] = NULL;
2143 n_pstate_ex_no = c_shell(argv); /* TODO history norm.; errexit? */
2145 goto jhistcont;
2146 case '.':
2147 /* Simulate end of file on input */
2148 if(cnt != 0 || coap != NULL)
2149 goto jearg;
2150 goto jout; /* TODO does not enter history, thus */
2151 case ':':
2152 case '_':
2153 /* Escape to command mode, but be nice! *//* TODO command expansion
2154 * TODO should be handled here so that we have unique history! */
2155 if(cnt == 0)
2156 goto jearg;
2157 _execute_command(hp, cp, cnt);
2158 if(ok_blook(errexit))
2159 flags |= a_ERREXIT;
2160 else
2161 flags &= ~a_ERREXIT;
2162 if(n_pstate_ex_no != 0 && a_HARDERR())
2163 goto jerr;
2164 if(coap == NULL)
2165 escape = *ok_vlook(escape);
2166 break;
2167 /* case '<': <> 'd' */
2168 case '?':
2169 fputs(_(
2170 "COMMAND ESCAPES (to be placed after a newline) excerpt:\n"
2171 "~. Commit and send message\n"
2172 "~: <command> Execute an internal command\n"
2173 "~< <file> Insert <file> (\"~<! <command>\" inserts shell command)\n"
2174 "~@ [<files>] Edit[/Add] attachments (file[=input-charset[#output-charset]])\n"
2175 "~c <users> Add users to Cc: list (`~b': to Bcc:)\n"
2176 "~d Read in $DEAD (dead.letter)\n"
2177 "~e Edit message via $EDITOR\n"
2178 "~F <msglist> Read in with headers, do not *indentprefix* lines\n"
2179 ), n_stdout);
2180 fputs(_(
2181 "~f <msglist> Like ~F, but honour `ignore' / `retain' configuration\n"
2182 "~H Edit From:, Reply-To: and Sender:\n"
2183 "~h Prompt for Subject:, To:, Cc: and \"blind\" Bcc:\n"
2184 "~i <variable> Insert a value and a newline\n"
2185 "~M <msglist> Read in with headers, *indentprefix* (`~m': `retain' etc.)\n"
2186 "~p Show current message compose buffer\n"
2187 "~r <file> Insert <file> (`~R': likewise, but *indentprefix* lines)\n"
2188 " <file> may also be <- [HERE-DELIMITER]>\n"
2189 ), n_stdout);
2190 fputs(_(
2191 "~s <subject> Set Subject:\n"
2192 "~t <users> Add users to To: list\n"
2193 "~u <msglist> Read in message(s) without headers (`~U': indent lines)\n"
2194 "~v Edit message via $VISUAL\n"
2195 "~w <file> Write message onto file\n"
2196 "~x Abort composition, discard message (`~q': save in $DEAD)\n"
2197 "~| <command> Pipe message through shell filter\n"
2198 ), n_stdout);
2199 if(cnt != 0)
2200 goto jearg;
2201 n_pstate_err_no = n_ERR_NONE;
2202 n_pstate_ex_no = 0;
2203 break;
2204 case '@':{
2205 struct attachment *aplist;
2207 /* Edit the attachment list */
2208 aplist = hp->h_attach;
2209 hp->h_attach = NULL;
2210 if(cnt != 0)
2211 hp->h_attach = n_attachment_append_list(aplist, cp);
2212 else
2213 hp->h_attach = n_attachment_list_edit(aplist,
2214 n_GO_INPUT_CTX_COMPOSE);
2215 n_pstate_err_no = n_ERR_NONE; /* XXX ~@ does NOT handle $!/$?! */
2216 n_pstate_ex_no = 0; /* XXX */
2217 }break;
2218 case '^':
2219 if(!a_collect_plumbing(cp, hp)){
2220 if(ferror(_coll_fp))
2221 goto jerr;
2222 goto jearg;
2224 n_pstate_err_no = n_ERR_NONE; /* XXX */
2225 n_pstate_ex_no = 0; /* XXX */
2226 hist &= ~a_HIST_GABBY;
2227 break;
2228 /* case '_': <> ':' */
2229 case '|':
2230 /* Pipe message through command. Collect output as new message */
2231 if(cnt == 0)
2232 goto jearg;
2233 rewind(_coll_fp);
2234 if((n_pstate_err_no = a_coll_pipe(cp)) == n_ERR_NONE)
2235 n_pstate_ex_no = 0;
2236 else if(a_HARDERR())
2237 goto jerr;
2238 else
2239 n_pstate_ex_no = 1;
2240 hist &= ~a_HIST_GABBY;
2241 goto jhistcont;
2242 case 'A':
2243 case 'a':
2244 /* Insert the contents of a sign variable */
2245 if(cnt != 0)
2246 goto jearg;
2247 cp = (c == 'a') ? ok_vlook(sign) : ok_vlook(Sign);
2248 goto jIi_putesc;
2249 case 'b':
2250 /* Add stuff to blind carbon copies list TODO join 'c' */
2251 if(cnt == 0)
2252 goto jearg;
2253 else{
2254 struct name *np;
2255 si8_t soe;
2257 soe = 0;
2258 if((np = checkaddrs(lextract(cp, GBCC | GFULL), EACM_NORMAL, &soe)
2259 ) != NULL)
2260 hp->h_bcc = cat(hp->h_bcc, np);
2261 if(soe == 0){
2262 n_pstate_err_no = n_ERR_NONE;
2263 n_pstate_ex_no = 0;
2264 }else{
2265 n_pstate_ex_no = 1;
2266 n_pstate_err_no = (soe < 0) ? n_ERR_PERM : n_ERR_INVAL;
2269 hist &= ~a_HIST_GABBY;
2270 break;
2271 case 'c':
2272 /* Add to the CC list TODO join 'b' */
2273 if(cnt == 0)
2274 goto jearg;
2275 else{
2276 struct name *np;
2277 si8_t soe;
2279 soe = 0;
2280 if((np = checkaddrs(lextract(cp, GCC | GFULL), EACM_NORMAL, &soe)
2281 ) != NULL)
2282 hp->h_cc = cat(hp->h_cc, np);
2283 if(soe == 0){
2284 n_pstate_err_no = n_ERR_NONE;
2285 n_pstate_ex_no = 0;
2286 }else{
2287 n_pstate_ex_no = 1;
2288 n_pstate_err_no = (soe < 0) ? n_ERR_PERM : n_ERR_INVAL;
2291 hist &= ~a_HIST_GABBY;
2292 break;
2293 case 'd':
2294 if(cnt != 0)
2295 goto jearg;
2296 cp = n_getdeadletter();
2297 if(0){
2298 case '<':
2299 case 'R':
2300 case 'r':
2301 /* Invoke a file: Search for the file name, then open it and copy
2302 * the contents to _coll_fp */
2303 if(cnt == 0){
2304 n_err(_("Interpolate what file?\n"));
2305 if(a_HARDERR())
2306 goto jerr;
2307 n_pstate_err_no = n_ERR_NOENT;
2308 n_pstate_ex_no = 1;
2309 break;
2311 if(*cp == '!' && c == '<'){
2312 /* TODO hist. normalization */
2313 if((n_pstate_err_no = a_coll_insert_cmd(_coll_fp, ++cp)
2314 ) != n_ERR_NONE){
2315 if(ferror(_coll_fp))
2316 goto jerr;
2317 if(a_HARDERR())
2318 goto jerr;
2319 n_pstate_ex_no = 1;
2320 break;
2322 goto jhistcont;
2324 /* Note this also expands things like
2325 * !:vput vexpr delim random 0
2326 * !< - $delim */
2327 if((cp = fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO | FEXP_NSHELL)
2328 ) == NULL){
2329 if(a_HARDERR())
2330 goto jerr;
2331 n_pstate_err_no = n_ERR_INVAL;
2332 n_pstate_ex_no = 1;
2333 break;
2336 if(n_is_dir(cp, FAL0)){
2337 n_err(_("%s: is a directory\n"), n_shexp_quote_cp(cp, FAL0));
2338 if(a_HARDERR())
2339 goto jerr;
2340 n_pstate_err_no = n_ERR_ISDIR;
2341 n_pstate_ex_no = 1;
2342 break;
2344 if((n_pstate_err_no = a_coll_include_file(cp, (c == 'R'), TRU1)
2345 ) != n_ERR_NONE){
2346 if(ferror(_coll_fp))
2347 goto jerr;
2348 if(a_HARDERR())
2349 goto jerr;
2350 n_pstate_ex_no = 1;
2351 break;
2353 n_pstate_err_no = n_ERR_NONE; /* XXX */
2354 n_pstate_ex_no = 0; /* XXX */
2355 break;
2356 case 'e':
2357 case 'v':
2358 /* Edit the current message. 'e' -> use EDITOR, 'v' -> use VISUAL */
2359 if(cnt != 0 || coap != NULL)
2360 goto jearg;
2361 rewind(_coll_fp);
2362 if((n_pstate_err_no = a_coll_edit(c, ok_blook(editheaders) ? hp : NULL)
2363 ) == n_ERR_NONE)
2364 n_pstate_ex_no = 0;
2365 else if(ferror(_coll_fp))
2366 goto jerr;
2367 else if(a_HARDERR())
2368 goto jerr;
2369 else
2370 n_pstate_ex_no = 1;
2371 goto jhistcont;
2372 case 'F':
2373 case 'f':
2374 case 'M':
2375 case 'm':
2376 case 'U':
2377 case 'u':
2378 /* Interpolate the named messages, if we are in receiving mail mode.
2379 * Does the standard list processing garbage. If ~f is given, we
2380 * don't shift over */
2381 if(cnt == 0)
2382 goto jearg;
2383 if((n_pstate_err_no = a_coll_forward(cp, _coll_fp, c)) == n_ERR_NONE)
2384 n_pstate_ex_no = 0;
2385 else if(ferror(_coll_fp))
2386 goto jerr;
2387 else if(a_HARDERR())
2388 goto jerr;
2389 else
2390 n_pstate_ex_no = 1;
2391 break;
2392 case 'H':
2393 /* Grab extra headers */
2394 if(cnt != 0)
2395 goto jearg;
2397 grab_headers(n_GO_INPUT_CTX_COMPOSE, hp, GEXTRA, 0);
2398 while(check_from_and_sender(hp->h_from, hp->h_sender) == NULL);
2399 n_pstate_err_no = n_ERR_NONE; /* XXX */
2400 n_pstate_ex_no = 0; /* XXX */
2401 break;
2402 case 'h':
2403 /* Grab a bunch of headers */
2404 if(cnt != 0)
2405 goto jearg;
2407 grab_headers(n_GO_INPUT_CTX_COMPOSE, hp,
2408 (GTO | GSUBJECT | GCC | GBCC),
2409 (ok_blook(bsdcompat) && ok_blook(bsdorder)));
2410 while(hp->h_to == NULL);
2411 n_pstate_err_no = n_ERR_NONE; /* XXX */
2412 n_pstate_ex_no = 0; /* XXX */
2413 break;
2414 case 'I':
2415 case 'i':
2416 /* Insert a variable into the file */
2417 if(cnt == 0)
2418 goto jearg;
2419 cp = n_var_vlook(cp, TRU1);
2420 jIi_putesc:
2421 if(cp == NULL || *cp == '\0')
2422 break;
2423 if(!a_coll_putesc(cp, (c != 'I'), _coll_fp))
2424 goto jerr;
2425 if((n_psonce & n_PSO_INTERACTIVE) && !(n_pstate & n_PS_ROBOT) &&
2426 !a_coll_putesc(cp, (c != 'I'), n_stdout))
2427 goto jerr;
2428 n_pstate_err_no = n_ERR_NONE; /* XXX */
2429 n_pstate_ex_no = 0; /* XXX */
2430 break;
2431 /* case 'M': <> 'F' */
2432 /* case 'm': <> 'f' */
2433 case 'p':
2434 /* Print current state of the message without altering anything */
2435 if(cnt != 0)
2436 goto jearg;
2437 print_collf(_coll_fp, hp); /* XXX pstate_err_no ++ */
2438 if(ferror(_coll_fp))
2439 goto jerr;
2440 n_pstate_err_no = n_ERR_NONE; /* XXX */
2441 n_pstate_ex_no = 0; /* XXX */
2442 break;
2443 case 'q':
2444 case 'x':
2445 /* Force a quit, act like an interrupt had happened */
2446 if(cnt != 0)
2447 goto jearg;
2448 /* If we are running a splice hook, assume it quits on its own now,
2449 * otherwise we (no true out-of-band IPC to signal this state, XXX sic)
2450 * have to SIGTERM it in order to stop this wild beast */
2451 flags |= a_COAP_NOSIGTERM;
2452 ++_coll_hadintr;
2453 _collint((c == 'x') ? 0 : SIGINT);
2454 exit(n_EXIT_ERR);
2455 /*NOTREACHED*/
2456 /* case 'R': <> 'd' */
2457 /* case 'r': <> 'd' */
2458 case 's':
2459 /* Set the Subject list */
2460 if(cnt == 0)
2461 goto jearg;
2462 /* Subject:; take care for Debian #419840 and strip any \r and \n */
2463 if(n_anyof_cp("\n\r", hp->h_subject = savestr(cp))){
2464 char *xp;
2466 n_err(_("-s: normalizing away invalid ASCII NL / CR bytes\n"));
2467 for(xp = hp->h_subject; *xp != '\0'; ++xp)
2468 if(*xp == '\n' || *xp == '\r')
2469 *xp = ' ';
2470 n_pstate_err_no = n_ERR_INVAL;
2471 n_pstate_ex_no = 1;
2472 }else{
2473 n_pstate_err_no = n_ERR_NONE;
2474 n_pstate_ex_no = 0;
2476 break;
2477 case 't':
2478 /* Add to the To: list TODO join 'b', 'c' */
2479 if(cnt == 0)
2480 goto jearg;
2481 else{
2482 struct name *np;
2483 si8_t soe;
2485 soe = 0;
2486 if((np = checkaddrs(lextract(cp, GTO | GFULL), EACM_NORMAL, &soe)
2487 ) != NULL)
2488 hp->h_to = cat(hp->h_to, np);
2489 if(soe == 0){
2490 n_pstate_err_no = n_ERR_NONE;
2491 n_pstate_ex_no = 0;
2492 }else{
2493 n_pstate_ex_no = 1;
2494 n_pstate_err_no = (soe < 0) ? n_ERR_PERM : n_ERR_INVAL;
2497 hist &= ~a_HIST_GABBY;
2498 break;
2499 /* case 'U': <> 'F' */
2500 /* case 'u': <> 'f' */
2501 /* case 'v': <> 'e' */
2502 case 'w':
2503 /* Write the message on a file */
2504 if(cnt == 0)
2505 goto jearg;
2506 if((cp = fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO)) == NULL){
2507 n_err(_("Write what file!?\n"));
2508 if(a_HARDERR())
2509 goto jerr;
2510 n_pstate_err_no = n_ERR_INVAL;
2511 n_pstate_ex_no = 1;
2512 break;
2514 rewind(_coll_fp);
2515 if((n_pstate_err_no = a_coll_write(cp, _coll_fp, 1)) == n_ERR_NONE)
2516 n_pstate_ex_no = 0;
2517 else if(ferror(_coll_fp))
2518 goto jerr;
2519 else if(a_HARDERR())
2520 goto jerr;
2521 else
2522 n_pstate_ex_no = 1;
2523 break;
2524 /* case 'x': <> 'q' */
2527 /* Finally place an entry in history as applicable */
2528 if(0){
2529 jhistcont:
2530 c = '\1';
2531 }else
2532 c = '\0';
2533 if(hist & a_HIST_ADD)
2534 n_tty_addhist(n_string_cp(sp), ((hist & a_HIST_GABBY) != 0));
2535 if(c != '\0')
2536 goto jcont;
2539 jout:
2540 /* Do we have *on-compose-splice-shell*, or *on-compose-splice*?
2541 * TODO Usual f...ed up state of signals and terminal etc. */
2542 if(coap == NULL && (cp = ok_vlook(on_compose_splice_shell)) != NULL) Jocs:{
2543 union {int (*ptf)(void); char const *sh;} u;
2544 char const *cmd;
2546 /* Reset *escape* and more to their defaults. On change update manual! */
2547 if(ifs_saved == NULL)
2548 ifs_saved = savestr(ok_vlook(ifs));
2549 escape = n_ESCAPE[0];
2550 ok_vclear(ifs);
2552 if(coapm != NULL){
2553 /* XXX Due Popen() fflush(NULL) in PTF mode, ensure nothing to flush */
2554 /*if(!n_real_seek(_coll_fp, 0, SEEK_END))
2555 * goto jerr;*/
2556 u.ptf = &a_coll_ocs__mac;
2557 cmd = (char*)-1;
2558 a_coll_ocs__macname = cp = coapm;
2559 }else{
2560 u.sh = ok_vlook(SHELL);
2561 cmd = cp;
2564 i = strlen(cp) +1;
2565 coap = n_lofi_alloc(n_VSTRUCT_SIZEOF(struct a_coll_ocs_arg, coa_cmd
2566 ) + i);
2567 coap->coa_pipe[0] = coap->coa_pipe[1] = -1;
2568 coap->coa_stdin = coap->coa_stdout = NULL;
2569 coap->coa_senderr = checkaddr_err;
2570 memcpy(coap->coa_cmd, cp, i);
2572 hold_all_sigs();
2573 coap->coa_opipe = safe_signal(SIGPIPE, SIG_IGN);
2574 coap->coa_oint = safe_signal(SIGINT, SIG_IGN);
2575 rele_all_sigs();
2577 if(pipe_cloexec(coap->coa_pipe) != -1 &&
2578 (coap->coa_stdin = Fdopen(coap->coa_pipe[0], "r", FAL0)) != NULL &&
2579 (coap->coa_stdout = Popen(cmd, "W", u.sh, NULL, coap->coa_pipe[1])
2580 ) != NULL){
2581 close(coap->coa_pipe[1]);
2582 coap->coa_pipe[1] = -1;
2584 temporary_compose_mode_hook_call(NULL, NULL, NULL);
2585 n_go_splice_hack(coap->coa_cmd, coap->coa_stdin, coap->coa_stdout,
2586 (n_psonce & ~(n_PSO_INTERACTIVE | n_PSO_TTYIN | n_PSO_TTYOUT)),
2587 &a_coll_ocs__finalize, &coap);
2588 /* Hook version protocol for ~^: update manual upon change! */
2589 fputs(a_COLL_PLUMBING_VERSION "\n", coap->coa_stdout);
2590 #undef a_COLL_PLUMBING_VERSION
2591 goto jcont;
2594 c = n_err_no;
2595 a_coll_ocs__finalize(coap);
2596 n_perr(_("Cannot invoke *on-compose-splice(-shell)?*"), c);
2597 goto jerr;
2599 if(*checkaddr_err != 0){
2600 *checkaddr_err = 0;
2601 goto jerr;
2603 if(coapm == NULL && (coapm = ok_vlook(on_compose_splice)) != NULL)
2604 goto Jocs;
2605 if(coap != NULL){
2606 ok_vset(ifs, ifs_saved);
2607 ifs_saved = NULL;
2610 /* Final chance to edit headers, if not already above */
2611 if((n_psonce & n_PSO_INTERACTIVE) && !(n_pstate & n_PS_ROBOT)){
2612 if(ok_blook(bsdcompat) || ok_blook(askatend)){
2613 enum gfield gf;
2615 gf = GNONE;
2616 if(ok_blook(askcc))
2617 gf |= GCC;
2618 if(ok_blook(askbcc))
2619 gf |= GBCC;
2620 if(gf != 0)
2621 grab_headers(n_GO_INPUT_CTX_COMPOSE, hp, gf, 1);
2623 if(ok_blook(askattach))
2624 hp->h_attach = n_attachment_list_edit(hp->h_attach,
2625 n_GO_INPUT_CTX_COMPOSE);
2628 /* Execute compose-leave */
2629 if((cp = ok_vlook(on_compose_leave)) != NULL){
2630 setup_from_and_sender(hp);
2631 temporary_compose_mode_hook_call(cp, &n_temporary_compose_hook_varset,
2632 hp);
2635 /* Add automatic receivers */
2636 if ((cp = ok_vlook(autocc)) != NULL && *cp != '\0')
2637 hp->h_cc = cat(hp->h_cc, checkaddrs(lextract(cp, GCC |
2638 (ok_blook(fullnames) ? GFULL | GSKIN : GSKIN)),
2639 EACM_NORMAL, checkaddr_err));
2640 if ((cp = ok_vlook(autobcc)) != NULL && *cp != '\0')
2641 hp->h_bcc = cat(hp->h_bcc, checkaddrs(lextract(cp, GBCC |
2642 (ok_blook(fullnames) ? GFULL | GSKIN : GSKIN)),
2643 EACM_NORMAL, checkaddr_err));
2644 if (*checkaddr_err != 0)
2645 goto jerr;
2647 if((n_psonce & n_PSO_INTERACTIVE) && !(n_pstate & n_PS_ROBOT) &&
2648 ok_blook(asksend)){
2649 bool_t b;
2651 ifs_saved = coapm = NULL;
2652 coap = NULL;
2654 fprintf(n_stdout, _("-------\nEnvelope contains:\n")); /* xxx SEARCH112 */
2655 puthead(TRU1, hp, n_stdout, GIDENT | GSUBJECT | GTO | GCC | GBCC | GCOMMA,
2656 SEND_TODISP, CONV_NONE, NULL, NULL);
2658 while((cp = n_go_input_cp(n_GO_INPUT_CTX_COMPOSE | n_GO_INPUT_NL_ESC,
2659 _("Send this message [yes/no, or recompose]? "), NULL)) == NULL)
2660 if(n_go_input_is_eof()){
2661 cp = n_1;
2662 break;
2665 b = boolify(cp, UIZ_MAX, TRU1);
2666 if(b < FAL0)
2667 goto jcont;
2668 else if(!b)
2669 goto jerr;
2672 /* TODO Cannot do since it may require turning this into a multipart one */
2673 if(n_poption & n_PO_Mm_FLAG)
2674 goto jskiptails;
2676 /* Place signature? */
2677 if((cp = ok_vlook(signature)) != NULL && *cp != '\0'){ /* TODO OBSOLETE */
2678 char const *cpq;
2680 n_OBSOLETE(_("please use *on-compose-{leave,splice}* and/or "
2681 "*message-inject-tail*, not *signature*"));
2683 if((cpq = fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO)) == NULL){
2684 n_err(_("*signature* expands to invalid file: %s\n"),
2685 n_shexp_quote_cp(cp, FAL0));
2686 goto jerr;
2688 cpq = n_shexp_quote_cp(cp = cpq, FAL0);
2690 if((sigfp = Fopen(cp, "r")) == NULL){
2691 n_err(_("Can't open *signature* %s: %s\n"),
2692 cpq, n_err_to_doc(n_err_no));
2693 goto jerr;
2696 if(linebuf == NULL)
2697 linebuf = smalloc(linesize = LINESIZE);
2698 c = '\0';
2699 while((i = fread(linebuf, sizeof *linebuf, linesize, n_UNVOLATILE(sigfp)))
2700 > 0){
2701 c = linebuf[i - 1];
2702 if(i != fwrite(linebuf, sizeof *linebuf, i, _coll_fp))
2703 goto jerr;
2706 /* C99 */{
2707 FILE *x = n_UNVOLATILE(sigfp);
2708 int e = n_err_no, ise = ferror(x);
2710 sigfp = NULL;
2711 Fclose(x);
2713 if(ise){
2714 n_err(_("Errors while reading *signature* %s: %s\n"),
2715 cpq, n_err_to_doc(e));
2716 goto jerr;
2720 if(c != '\0' && c != '\n')
2721 putc('\n', _coll_fp);
2724 { char const *cp_obsolete = ok_vlook(NAIL_TAIL);
2726 if(cp_obsolete != NULL)
2727 n_OBSOLETE(_("please use *message-inject-tail*, not *NAIL_TAIL*"));
2729 if((cp = ok_vlook(message_inject_tail)) != NULL ||
2730 (cp = cp_obsolete) != NULL){
2731 if(!a_coll_putesc(cp, TRU1, _coll_fp))
2732 goto jerr;
2733 if((n_psonce & n_PSO_INTERACTIVE) && !(n_pstate & n_PS_ROBOT) &&
2734 !a_coll_putesc(cp, TRU1, n_stdout))
2735 goto jerr;
2739 jskiptails:
2740 if(fflush(_coll_fp))
2741 goto jerr;
2742 rewind(_coll_fp);
2744 jleave:
2745 if (linebuf != NULL)
2746 free(linebuf);
2747 sigprocmask(SIG_BLOCK, &nset, NULL);
2748 n_pstate &= ~n_PS_COMPOSE_MODE;
2749 safe_signal(SIGINT, _coll_saveint);
2750 safe_signal(SIGHUP, _coll_savehup);
2751 sigprocmask(SIG_SETMASK, &oset, NULL);
2752 NYD_LEAVE;
2753 return _coll_fp;
2755 jerr:
2756 hold_all_sigs();
2758 if(coap != NULL && coap != (struct a_coll_ocs_arg*)-1){
2759 if(!(flags & a_COAP_NOSIGTERM))
2760 n_psignal(coap->coa_stdout, SIGTERM);
2761 n_go_splice_hack_remove_after_jump();
2762 coap = NULL;
2764 if(ifs_saved != NULL){
2765 ok_vset(ifs, ifs_saved);
2766 ifs_saved = NULL;
2768 if(sigfp != NULL){
2769 Fclose(n_UNVOLATILE(sigfp));
2770 sigfp = NULL;
2772 if(_coll_fp != NULL){
2773 Fclose(_coll_fp);
2774 _coll_fp = NULL;
2777 rele_all_sigs();
2779 assert(checkaddr_err != NULL);
2780 /* TODO We don't save in $DEAD upon error because msg not readily composed?
2781 * TODO But this no good, it should go ZOMBIE / DRAFT / POSTPONED or what! */
2782 if(*checkaddr_err != 0){
2783 if(*checkaddr_err == 111)
2784 n_err(_("Compose mode splice hook failure\n"));
2785 else
2786 n_err(_("Some addressees were classified as \"hard error\"\n"));
2787 }else if(_coll_hadintr == 0){
2788 *checkaddr_err = TRU1; /* TODO ugly: "sendout_error" now.. */
2789 n_err(_("Failed to prepare composed message\n"));
2791 goto jleave;
2793 #undef a_HARDERR
2796 /* s-it-mode */