filter.c:_hf_param(): CID 1377004 !
[s-mailx.git] / collect.c
blob2535d9e52f6824fbe2ae1d929bc3890121ce76a4
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Collect input from standard input, handling ~ escapes.
3 *@ TODO This needs a complete rewrite, with carriers, etc.
5 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
6 * Copyright (c) 2012 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
7 */
8 /*
9 * Copyright (c) 1980, 1993
10 * The Regents of the University of California. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
36 #undef n_FILE
37 #define n_FILE collect
39 #ifndef HAVE_AMALGAMATION
40 # include "nail.h"
41 #endif
43 struct a_coll_ocs_arg{
44 sighandler_type coa_opipe;
45 sighandler_type coa_oint;
46 FILE *coa_stdin; /* The descriptor (pipe(2)+Fdopen()) we read from */
47 FILE *coa_stdout; /* The Popen()ed pipe through which we write to the hook */
48 int coa_pipe[2]; /* ..backing .coa_stdin */
49 si8_t *coa_senderr; /* Set to 1 on failure */
50 char coa_cmd[n_VFIELD_SIZE(0)];
53 /* The following hookiness with global variables is so that on receipt of an
54 * interrupt signal, the partial message can be salted away on *DEAD* */
56 static sighandler_type _coll_saveint; /* Previous SIGINT value */
57 static sighandler_type _coll_savehup; /* Previous SIGHUP value */
58 static FILE *_coll_fp; /* File for saving away */
59 static int volatile _coll_hadintr; /* Have seen one SIGINT so far */
60 static sigjmp_buf _coll_jmp; /* To get back to work */
61 static sigjmp_buf _coll_abort; /* To end collection with error */
62 static char const *a_coll_ocs__macname; /* *on-compose-splice* */
64 /* Handle `~:', `~_' and some hooks; hp may be NULL */
65 static void _execute_command(struct header *hp, char const *linebuf,
66 size_t linesize);
68 /* Return errno */
69 static si32_t a_coll_include_file(char const *name, bool_t indent,
70 bool_t writestat);
72 /* Execute cmd and insert its standard output into fp, return errno */
73 static si32_t a_coll_insert_cmd(FILE *fp, char const *cmd);
75 /* ~p command */
76 static void print_collf(FILE *collf, struct header *hp);
78 /* Write a file, ex-like if f set */
79 static si32_t a_coll_write(char const *name, FILE *fp, int f);
81 /* *message-inject-head* */
82 static bool_t a_coll_message_inject_head(FILE *fp);
84 /* Parse off the message header from fp and store relevant fields in hp,
85 * replace _coll_fp with a shiny new version without any header */
86 static bool_t a_coll_makeheader(FILE *fp, struct header *hp,
87 si8_t *checkaddr_err, bool_t do_delayed_due_t);
89 /* Edit the message being collected on fp. On return, make the edit file the
90 * new temp file. Return errno */
91 static si32_t a_coll_edit(int c, struct header *hp);
93 /* Pipe the message through the command. Old message is on stdin of command,
94 * new message collected from stdout. Shell must return 0 to accept new msg */
95 static si32_t a_coll_pipe(char const *cmd);
97 /* Interpolate the named messages into the current message, possibly doing
98 * indent stuff. The flag argument is one of the command escapes: [mMfFuU].
99 * Return errno */
100 static si32_t a_coll_forward(char const *ms, FILE *fp, int f);
102 /* ~^ mode */
103 static bool_t a_collect_plumbing(char const *ms, struct header *p);
105 static bool_t a_collect__plumb_header(char const *cp, struct header *p,
106 char const *cmd[4]);
107 static bool_t a_collect__plumb_attach(char const *cp, struct header *p,
108 char const *cmd[4]);
110 /* On interrupt, come here to save the partial message in ~/dead.letter.
111 * Then jump out of the collection loop */
112 static void _collint(int s);
114 static void collhup(int s);
116 static int putesc(char const *s, FILE *stream); /* TODO wysh set! */
118 /* *on-compose-splice* driver and *on-compose-splice(-shell)?* finalizer */
119 static int a_coll_ocs__mac(void);
120 static void a_coll_ocs__finalize(void *vp);
122 static void
123 _execute_command(struct header *hp, char const *linebuf, size_t linesize){
124 /* The problem arises if there are rfc822 message attachments and the
125 * user uses `~:' to change the current file. TODO Unfortunately we
126 * TODO cannot simply keep a pointer to, or increment a reference count
127 * TODO of the current `file' (mailbox that is) object, because the
128 * TODO codebase doesn't deal with that at all; so, until some far
129 * TODO later time, copy the name of the path, and warn the user if it
130 * TODO changed; we COULD use the AC_TMPFILE attachment type, i.e.,
131 * TODO copy the message attachments over to temporary files, but that
132 * TODO would require more changes so that the user still can recognize
133 * TODO in `~@' etc. that its a rfc822 message attachment; see below */
134 struct n_sigman sm;
135 struct attachment *ap;
136 char * volatile mnbuf;
137 NYD_ENTER;
139 n_UNUSED(linesize);
140 mnbuf = NULL;
142 n_SIGMAN_ENTER_SWITCH(&sm, n_SIGMAN_HUP | n_SIGMAN_INT | n_SIGMAN_QUIT){
143 case 0:
144 break;
145 default:
146 n_pstate_err_no = n_ERR_INTR;
147 n_pstate_ex_no = 1;
148 goto jleave;
151 /* If the above todo is worked, remove or outsource to attachment.c! */
152 if(hp != NULL && (ap = hp->h_attach) != NULL) do
153 if(ap->a_msgno){
154 mnbuf = sstrdup(mailname);
155 break;
157 while((ap = ap->a_flink) != NULL);
159 n_go_command(n_GO_INPUT_CTX_COMPOSE, linebuf);
161 n_sigman_cleanup_ping(&sm);
162 jleave:
163 if(mnbuf != NULL){
164 if(strcmp(mnbuf, mailname))
165 n_err(_("Mailbox changed: it is likely that existing "
166 "rfc822 attachments became invalid!\n"));
167 free(mnbuf);
169 NYD_LEAVE;
170 n_sigman_leave(&sm, n_SIGMAN_VIPSIGS_NTTYOUT);
173 static si32_t
174 a_coll_include_file(char const *name, bool_t indent, bool_t writestat){
175 FILE *fbuf;
176 char const *heredb, *indb;
177 size_t linesize, heredl, indl, cnt, linelen;
178 char *linebuf;
179 si64_t lc, cc;
180 si32_t rv;
181 NYD_ENTER;
183 rv = n_ERR_NONE;
184 lc = cc = 0;
185 linebuf = NULL; /* TODO line pool */
186 linesize = 0;
187 heredb = NULL;
188 heredl = 0;
190 /* The -M case is special */
191 if(name == (char*)-1){
192 fbuf = n_stdin;
193 name = "-";
194 }else if(name[0] == '-' &&
195 (name[1] == '\0' || blankspacechar(name[1]))){
196 fbuf = n_stdin;
197 if(name[1] == '\0'){
198 if(!(n_psonce & n_PSO_INTERACTIVE)){
199 n_err(_("~< -: HERE-delimiter required in non-interactive mode\n"));
200 rv = n_ERR_INVAL;
201 goto jleave;
203 }else{
204 for(heredb = &name[2]; *heredb != '\0' && blankspacechar(*heredb);
205 ++heredb)
207 if((heredl = strlen(heredb)) == 0){
208 jdelim_empty:
209 n_err(_("~< - HERE-delimiter: delimiter must not be empty\n"));
210 rv = n_ERR_INVAL;
211 goto jleave;
214 if(*heredb == '\''){
215 for(indb = ++heredb; *indb != '\0' && *indb != '\''; ++indb)
217 if(*indb == '\0'){
218 n_err(_("~< - HERE-delimiter: missing trailing quote\n"));
219 rv = n_ERR_INVAL;
220 goto jleave;
221 }else if(indb[1] != '\0'){
222 n_err(_("~< - HERE-delimiter: trailing characters after "
223 "quote\n"));
224 rv = n_ERR_INVAL;
225 goto jleave;
227 if((heredl = PTR2SIZE(indb - heredb)) == 0)
228 goto jdelim_empty;
229 heredb = savestrbuf(heredb, heredl);
232 name = "-";
233 }else if((fbuf = Fopen(name, "r")) == NULL){
234 n_perr(name, rv = n_err_no);
235 goto jleave;
238 if(!indent)
239 indl = 0;
240 else{
241 if((indb = ok_vlook(indentprefix)) == NULL)
242 indb = INDENT_DEFAULT;
243 indl = strlen(indb);
246 if(fbuf != n_stdin)
247 cnt = fsize(fbuf);
248 while(fgetline(&linebuf, &linesize, (fbuf == n_stdin ? NULL : &cnt),
249 &linelen, fbuf, 0) != NULL){
250 if(heredl > 0 && heredl == linelen - 1 &&
251 !memcmp(heredb, linebuf, heredl)){
252 heredb = NULL;
253 break;
256 if(indl > 0){
257 if(fwrite(indb, sizeof *indb, indl, _coll_fp) != indl){
258 rv = n_err_no;
259 goto jleave;
261 cc += indl;
264 if(fwrite(linebuf, sizeof *linebuf, linelen, _coll_fp) != linelen){
265 rv = n_err_no;
266 goto jleave;
268 cc += linelen;
269 ++lc;
271 if(fflush(_coll_fp)){
272 rv = n_err_no;
273 goto jleave;
276 if(heredb != NULL)
277 rv = n_ERR_NOTOBACCO;
278 jleave:
279 if(linebuf != NULL)
280 free(linebuf);
281 if(fbuf != NULL){
282 if(fbuf != n_stdin)
283 Fclose(fbuf);
284 else if(heredl > 0)
285 clearerr(n_stdin);
288 if(writestat)
289 fprintf(n_stdout, "%s%s %" PRId64 "/%" PRId64 "\n",
290 n_shexp_quote_cp(name, FAL0), (rv ? " " n_ERROR : n_empty), lc, cc);
291 NYD_LEAVE;
292 return rv;
295 static si32_t
296 a_coll_insert_cmd(FILE *fp, char const *cmd){
297 FILE *ibuf;
298 si64_t lc, cc;
299 si32_t rv;
300 NYD_ENTER;
302 rv = n_ERR_NONE;
303 lc = cc = 0;
305 if((ibuf = Popen(cmd, "r", ok_vlook(SHELL), NULL, 0)) != NULL){
306 int c;
308 while((c = getc(ibuf)) != EOF){ /* XXX bytewise, yuck! */
309 if(putc(c, fp) == EOF){
310 rv = n_err_no;
311 break;
313 ++cc;
314 if(c == '\n')
315 ++lc;
317 if(!feof(ibuf) || ferror(ibuf)){
318 if(rv == n_ERR_NONE)
319 rv = n_ERR_IO;
321 if(!Pclose(ibuf, TRU1)){
322 if(rv == n_ERR_NONE)
323 rv = n_ERR_IO;
325 }else
326 n_perr(cmd, rv = n_err_no);
328 fprintf(n_stdout, "CMD%s %" PRId64 "/%" PRId64 "\n",
329 (rv == n_ERR_NONE ? n_empty : " " n_ERROR), lc, cc);
330 NYD_LEAVE;
331 return rv;
334 static void
335 print_collf(FILE *cf, struct header *hp)
337 char *lbuf;
338 FILE *obuf;
339 size_t cnt, linesize, linelen;
340 NYD_ENTER;
342 fflush_rewind(cf);
343 cnt = (size_t)fsize(cf);
345 if((obuf = Ftmp(NULL, "collfp", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL){
346 n_perr(_("Can't create temporary file for `~p' command"), 0);
347 goto jleave;
350 hold_all_sigs();
352 fprintf(obuf, _("-------\nMessage contains:\n"));
353 puthead(TRU1, hp, obuf,
354 (GIDENT | GTO | GSUBJECT | GCC | GBCC | GNL | GFILES | GCOMMA),
355 SEND_TODISP, CONV_NONE, NULL, NULL);
357 lbuf = NULL;
358 linesize = 0;
359 while(fgetline(&lbuf, &linesize, &cnt, &linelen, cf, 1))
360 prout(lbuf, linelen, obuf);
361 if(lbuf != NULL)
362 free(lbuf);
364 if(hp->h_attach != NULL){
365 fputs(_("-------\nAttachments:\n"), obuf);
366 n_attachment_list_print(hp->h_attach, obuf);
369 rele_all_sigs();
371 page_or_print(obuf, 0);
373 Fclose(obuf);
374 jleave:
375 NYD_LEAVE;
378 static si32_t
379 a_coll_write(char const *name, FILE *fp, int f)
381 FILE *of;
382 int c;
383 si64_t lc, cc;
384 si32_t rv;
385 NYD_ENTER;
387 rv = n_ERR_NONE;
389 if(f) {
390 fprintf(n_stdout, "%s ", n_shexp_quote_cp(name, FAL0));
391 fflush(n_stdout);
394 if ((of = Fopen(name, "a")) == NULL) {
395 n_perr(name, rv = n_err_no);
396 goto jerr;
399 lc = cc = 0;
400 while ((c = getc(fp)) != EOF) {
401 ++cc;
402 if (c == '\n')
403 ++lc;
404 if (putc(c, of) == EOF) {
405 n_perr(name, rv = n_err_no);
406 goto jerr;
409 fprintf(n_stdout, _("%" PRId64 "/%" PRId64 "\n"), lc, cc);
411 jleave:
412 if(of != NULL)
413 Fclose(of);
414 fflush(n_stdout);
415 NYD_LEAVE;
416 return rv;
417 jerr:
418 putc('-', n_stdout);
419 putc('\n', n_stdout);
420 goto jleave;
423 static bool_t
424 a_coll_message_inject_head(FILE *fp){
425 bool_t rv;
426 char const *cp, *cp_obsolete;
427 NYD2_ENTER;
429 cp_obsolete = ok_vlook(NAIL_HEAD);
430 if(cp_obsolete != NULL)
431 n_OBSOLETE(_("please use *message-inject-head*, not *NAIL_HEAD*"));
433 if(((cp = ok_vlook(message_inject_head)) != NULL ||
434 (cp = cp_obsolete) != NULL) && putesc(cp, fp) < 0)
435 rv = FAL0;
436 else
437 rv = TRU1;
438 NYD2_LEAVE;
439 return rv;
442 static bool_t
443 a_coll_makeheader(FILE *fp, struct header *hp, si8_t *checkaddr_err,
444 bool_t do_delayed_due_t)
446 FILE *nf;
447 int c;
448 bool_t rv;
449 NYD_ENTER;
451 rv = FAL0;
453 if ((nf = Ftmp(NULL, "colhead", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==NULL) {
454 n_perr(_("temporary mail edit file"), 0);
455 goto jleave;
458 extract_header(fp, hp, checkaddr_err);
459 if (checkaddr_err != NULL && *checkaddr_err != 0)
460 goto jleave;
462 /* In template mode some things have been delayed until the template has
463 * been read */
464 if(do_delayed_due_t){
465 char const *cp;
467 if((cp = ok_vlook(on_compose_enter)) != NULL){
468 setup_from_and_sender(hp);
469 temporary_compose_mode_hook_call(cp, &n_temporary_compose_hook_varset,
470 hp);
473 if(!a_coll_message_inject_head(nf))
474 goto jleave;
477 while ((c = getc(fp)) != EOF) /* XXX bytewise, yuck! */
478 putc(c, nf);
480 if (fp != _coll_fp)
481 Fclose(_coll_fp);
482 Fclose(fp);
483 _coll_fp = nf;
485 if (check_from_and_sender(hp->h_from, hp->h_sender) == NULL)
486 goto jleave;
487 rv = TRU1;
488 jleave:
489 NYD_LEAVE;
490 return rv;
493 static si32_t
494 a_coll_edit(int c, struct header *hp) /* TODO error(return) weird */
496 struct n_sigman sm;
497 FILE *nf;
498 sighandler_type volatile sigint;
499 bool_t saved;
500 si32_t volatile rv;
501 NYD_ENTER;
503 n_UNINIT(sigint, SIG_ERR);
504 rv = n_ERR_NONE;
506 if(!(saved = ok_blook(add_file_recipients)))
507 ok_bset(add_file_recipients);
509 n_SIGMAN_ENTER_SWITCH(&sm, n_SIGMAN_ALL){
510 case 0:
511 sigint = safe_signal(SIGINT, SIG_IGN);
512 break;
513 default:
514 rv = n_ERR_INTR;
515 goto jleave;
518 nf = run_editor(_coll_fp, (off_t)-1, c, FAL0, hp, NULL, SEND_MBOX, sigint);
519 if (nf != NULL) {
520 if (hp) {
521 if(!a_coll_makeheader(nf, hp, NULL, FAL0))
522 rv = n_ERR_INVAL;
523 } else {
524 fseek(nf, 0L, SEEK_END);
525 Fclose(_coll_fp);
526 _coll_fp = nf;
528 } else
529 rv = n_ERR_CHILD;
531 n_sigman_cleanup_ping(&sm);
532 jleave:
533 if(!saved)
534 ok_bclear(add_file_recipients);
535 safe_signal(SIGINT, sigint);
536 NYD_LEAVE;
537 n_sigman_leave(&sm, n_SIGMAN_VIPSIGS_NTTYOUT);
538 return rv;
541 static si32_t
542 a_coll_pipe(char const *cmd)
544 int ws;
545 FILE *nf;
546 sighandler_type sigint;
547 si32_t rv;
548 NYD_ENTER;
550 rv = n_ERR_NONE;
551 sigint = safe_signal(SIGINT, SIG_IGN);
553 if ((nf = Ftmp(NULL, "colpipe", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==NULL) {
554 n_perr(_("temporary mail edit file"), rv = n_err_no);
555 goto jout;
558 /* stdin = current message. stdout = new message */
559 fflush(_coll_fp);
560 if (n_child_run(ok_vlook(SHELL), 0, fileno(_coll_fp), fileno(nf), "-c",
561 cmd, NULL, NULL, &ws) < 0 || WEXITSTATUS(ws) != 0) {
562 Fclose(nf);
563 rv = n_ERR_CHILD;
564 goto jout;
567 if (fsize(nf) == 0) {
568 n_err(_("No bytes from %s !?\n"), n_shexp_quote_cp(cmd, FAL0));
569 Fclose(nf);
570 rv = n_ERR_NODATA;
571 goto jout;
574 /* Take new files */
575 fseek(nf, 0L, SEEK_END);
576 Fclose(_coll_fp);
577 _coll_fp = nf;
578 jout:
579 safe_signal(SIGINT, sigint);
580 NYD_LEAVE;
581 return rv;
584 static si32_t
585 a_coll_forward(char const *ms, FILE *fp, int f)
587 int *msgvec, rv = 0;
588 struct n_ignore const *itp;
589 char const *tabst;
590 enum sendaction action;
591 NYD_ENTER;
593 msgvec = salloc((size_t)(msgCount + 1) * sizeof *msgvec);
594 if (getmsglist(ms, msgvec, 0) < 0) {
595 rv = n_ERR_NOENT; /* XXX not really, should be handled there! */
596 goto jleave;
598 if (*msgvec == 0) {
599 *msgvec = first(0, MMNORM);
600 if (*msgvec == 0) {
601 n_err(_("No appropriate messages\n"));
602 rv = n_ERR_NOENT;
603 goto jleave;
605 msgvec[1] = 0;
608 if (f == 'f' || f == 'F' || f == 'u')
609 tabst = NULL;
610 else if ((tabst = ok_vlook(indentprefix)) == NULL)
611 tabst = INDENT_DEFAULT;
612 if (f == 'u' || f == 'U')
613 itp = n_IGNORE_ALL;
614 else
615 itp = upperchar(f) ? NULL : n_IGNORE_TYPE;
616 action = (upperchar(f) && f != 'U') ? SEND_QUOTE_ALL : SEND_QUOTE;
618 fprintf(n_stdout, _("Interpolating:"));
619 srelax_hold();
620 for (; *msgvec != 0; ++msgvec) {
621 struct message *mp = message + *msgvec - 1;
623 touch(mp);
624 fprintf(n_stdout, " %d", *msgvec);
625 fflush(n_stdout);
626 if (sendmp(mp, fp, itp, tabst, action, NULL) < 0) {
627 n_perr(_("temporary mail file"), 0);
628 rv = n_ERR_IO;
629 break;
631 srelax();
633 srelax_rele();
634 fprintf(n_stdout, "\n");
635 jleave:
636 NYD_LEAVE;
637 return rv;
640 static bool_t
641 a_collect_plumbing(char const *ms, struct header *hp){
642 /* TODO _collect_plumbing: instead of fields the basic headers should
643 * TODO be in an array and have IDs, like in termcap etc., so then this
644 * TODO could be simplified as table-walks. Also true for arg-checks! */
645 bool_t rv;
646 char const *cp, *cmd[4];
647 NYD2_ENTER;
649 /* Protcol version for *on-compose-splice** -- update manual on change! */
650 #define a_COLL_PLUMBING_VERSION "0 0 1"
651 cp = ms;
653 /* C99 */{
654 size_t i;
656 for(i = 0; i < n_NELEM(cmd); ++i){ /* TODO trim+strlist_split(_ifs?)() */
657 while(blankchar(*cp))
658 ++cp;
659 if(*cp == '\0')
660 cmd[i] = NULL;
661 else{
662 if(i < n_NELEM(cmd) - 1)
663 for(cmd[i] = cp++; *cp != '\0' && !blankchar(*cp); ++cp)
665 else{
666 /* Last slot takes all the rest of the line, less trailing WS */
667 for(cmd[i] = cp++; *cp != '\0'; ++cp)
669 while(blankchar(cp[-1]))
670 --cp;
672 cmd[i] = savestrbuf(cmd[i], PTR2SIZE(cp - cmd[i]));
677 if(n_UNLIKELY(cmd[0] == NULL))
678 goto jecmd;
679 if(is_asccaseprefix(cmd[0], "header"))
680 rv = a_collect__plumb_header(cp, hp, cmd);
681 else if(is_asccaseprefix(cmd[0], "attachment"))
682 rv = a_collect__plumb_attach(cp, hp, cmd);
683 else{
684 jecmd:
685 fputs("500\n", n_stdout);
686 rv = FAL0;
688 fflush(n_stdout);
690 NYD2_LEAVE;
691 return rv;
694 static bool_t
695 a_collect__plumb_header(char const *cp, struct header *hp,
696 char const *cmd[4]){
697 uiz_t i;
698 struct n_header_field *hfp;
699 struct name *np, **npp;
700 NYD2_ENTER;
702 if(cmd[1] == NULL)
703 goto jdefault;
705 if(is_asccaseprefix(cmd[1], "insert")){ /* TODO LOGIC BELONGS head.c
706 * TODO That is: Header::factory(string) -> object (blahblah).
707 * TODO I.e., as long as we don't have regular RFC compliant parsers
708 * TODO which differentiate in between structured and unstructured
709 * TODO header fields etc., a little workaround */
710 struct name *xnp;
711 si8_t aerr;
712 enum expand_addr_check_mode eacm;
713 enum gfield ntype;
714 bool_t mult_ok;
716 if(cmd[2] == NULL || cmd[3] == NULL)
717 goto jecmd;
719 /* Strip [\r\n] which would render a body invalid XXX all controls? */
720 /* C99 */{
721 char *xp, c;
723 cmd[3] = xp = savestr(cmd[3]);
724 for(; (c = *xp) != '\0'; ++xp)
725 if(c == '\n' || c == '\r')
726 *xp = ' ';
729 if(!asccasecmp(cmd[2], cp = "Subject")){
730 if(cmd[3][0] != '\0'){
731 if(hp->h_subject != NULL)
732 hp->h_subject = savecatsep(hp->h_subject, ' ', cmd[3]);
733 else
734 hp->h_subject = n_UNCONST(cmd[3]);
735 fprintf(n_stdout, "210 %s 1\n", cp);
736 goto jleave;
737 }else
738 goto j501cp;
741 mult_ok = TRU1;
742 ntype = GEXTRA | GFULL | GFULLEXTRA;
743 eacm = EACM_STRICT;
745 if(!asccasecmp(cmd[2], cp = "From")){
746 npp = &hp->h_from;
747 jins:
748 aerr = 0;
749 if((np = lextract(cmd[3], ntype)) == NULL)
750 goto j501cp;
752 if((np = checkaddrs(np, eacm, &aerr), aerr != 0)){
753 fprintf(n_stdout, "505 %s\n", cp);
754 goto jleave;
757 /* Go to the end of the list, track whether it contains any
758 * non-deleted entries */
759 i = 0;
760 if((xnp = *npp) != NULL)
761 for(;; xnp = xnp->n_flink){
762 if(!(xnp->n_type & GDEL))
763 ++i;
764 if(xnp->n_flink == NULL)
765 break;
768 if(!mult_ok && (i != 0 || np->n_flink != NULL))
769 fprintf(n_stdout, "506 %s\n", cp);
770 else{
771 if(xnp == NULL)
772 *npp = np;
773 else
774 xnp->n_flink = np;
775 np->n_blink = xnp;
776 fprintf(n_stdout, "210 %s %" PRIuZ "\n", cp, ++i);
778 goto jleave;
780 if(!asccasecmp(cmd[2], cp = "Sender")){
781 mult_ok = FAL0;
782 npp = &hp->h_sender;
783 goto jins;
785 if(!asccasecmp(cmd[2], cp = "To")){
786 npp = &hp->h_to;
787 ntype = GTO | GFULL;
788 eacm = EACM_NORMAL | EAF_NAME;
789 goto jins;
791 if(!asccasecmp(cmd[2], cp = "Cc")){
792 npp = &hp->h_cc;
793 ntype = GCC | GFULL;
794 eacm = EACM_NORMAL | EAF_NAME;
795 goto jins;
797 if(!asccasecmp(cmd[2], cp = "Bcc")){
798 npp = &hp->h_bcc;
799 ntype = GBCC | GFULL;
800 eacm = EACM_NORMAL | EAF_NAME;
801 goto jins;
803 if(!asccasecmp(cmd[2], cp = "Reply-To")){
804 npp = &hp->h_replyto;
805 eacm = EACM_NONAME;
806 goto jins;
808 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
809 npp = &hp->h_mft;
810 eacm = EACM_NONAME;
811 goto jins;
813 if(!asccasecmp(cmd[2], cp = "Message-ID")){
814 mult_ok = FAL0;
815 npp = &hp->h_message_id;
816 ntype = GREF;
817 eacm = EACM_NONAME;
818 goto jins;
820 if(!asccasecmp(cmd[2], cp = "References")){
821 npp = &hp->h_ref;
822 ntype = GREF;
823 eacm = EACM_NONAME;
824 goto jins;
826 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
827 npp = &hp->h_in_reply_to;
828 ntype = GREF;
829 eacm = EACM_NONAME;
830 goto jins;
833 if(!asccasecmp(cmd[2], cp = "Mailx-Command") ||
834 !asccasecmp(cmd[2], cp = "Mailx-Raw-To") ||
835 !asccasecmp(cmd[2], cp = "Mailx-Raw-Cc") ||
836 !asccasecmp(cmd[2], cp = "Mailx-Raw-Bcc") ||
837 !asccasecmp(cmd[2], cp = "Mailx-Orig-From") ||
838 !asccasecmp(cmd[2], cp = "Mailx-Orig-To") ||
839 !asccasecmp(cmd[2], cp = "Mailx-Orig-Cc") ||
840 !asccasecmp(cmd[2], cp = "Mailx-Orig-Bcc")){
841 fprintf(n_stdout, "505 %s\n", cp);
842 goto jleave;
845 /* Free-form header fields (note j501cp may print non-normalized name) */
846 /* C99 */{
847 size_t nl, bl;
848 struct n_header_field **hfpp;
850 for(cp = cmd[2]; *cp != '\0'; ++cp)
851 if(!fieldnamechar(*cp)){
852 cp = cmd[2];
853 goto j501cp;
856 for(i = 0, hfpp = &hp->h_user_headers; *hfpp != NULL; ++i)
857 hfpp = &(*hfpp)->hf_next;
859 nl = strlen(cp = cmd[2]) +1;
860 bl = strlen(cmd[3]) +1;
861 *hfpp = hfp = salloc(n_VSTRUCT_SIZEOF(struct n_header_field, hf_dat
862 ) + nl + bl);
863 hfp->hf_next = NULL;
864 hfp->hf_nl = --nl;
865 hfp->hf_bl = bl - 1;
866 hfp->hf_dat[nl = 0] = upperconv(*cp);
867 while(*cp++ != '\0')
868 hfp->hf_dat[++nl] = lowerconv(*cp);
869 memcpy(&hfp->hf_dat[++nl], cmd[3], bl);
870 fprintf(n_stdout, "210 %s %" PRIuZ "\n", &hfp->hf_dat[0], ++i);
872 goto jleave;
875 if(is_asccaseprefix(cmd[1], "list")){
876 jdefault:
877 if(cmd[2] == NULL){
878 fputs("210", n_stdout);
879 if(hp->h_subject != NULL) fputs(" Subject", n_stdout);
880 if(hp->h_from != NULL) fputs(" From", n_stdout);
881 if(hp->h_sender != NULL) fputs(" Sender", n_stdout);
882 if(hp->h_to != NULL) fputs(" To", n_stdout);
883 if(hp->h_cc != NULL) fputs(" Cc", n_stdout);
884 if(hp->h_bcc != NULL) fputs(" Bcc", n_stdout);
885 if(hp->h_replyto != NULL) fputs(" Reply-To", n_stdout);
886 if(hp->h_mft != NULL) fputs(" Mail-Followup-To", n_stdout);
887 if(hp->h_message_id != NULL) fputs(" Message-ID", n_stdout);
888 if(hp->h_ref != NULL) fputs(" References", n_stdout);
889 if(hp->h_in_reply_to != NULL) fputs(" In-Reply-To", n_stdout);
890 if(hp->h_mailx_command != NULL) fputs(" Mailx-Command", n_stdout);
891 if(hp->h_mailx_raw_to != NULL) fputs(" Mailx-Raw-To", n_stdout);
892 if(hp->h_mailx_raw_cc != NULL) fputs(" Mailx-Raw-Cc", n_stdout);
893 if(hp->h_mailx_raw_bcc != NULL) fputs(" Mailx-Raw-Bcc", n_stdout);
894 if(hp->h_mailx_orig_from != NULL) fputs(" Mailx-Orig-From", n_stdout);
895 if(hp->h_mailx_orig_to != NULL) fputs(" Mailx-Orig-To", n_stdout);
896 if(hp->h_mailx_orig_cc != NULL) fputs(" Mailx-Orig-Cc", n_stdout);
897 if(hp->h_mailx_orig_bcc != NULL) fputs(" Mailx-Orig-Bcc", n_stdout);
899 /* Print only one instance of each free-form header */
900 for(hfp = hp->h_user_headers; hfp != NULL; hfp = hfp->hf_next){
901 struct n_header_field *hfpx;
903 for(hfpx = hp->h_user_headers;; hfpx = hfpx->hf_next)
904 if(hfpx == hfp){
905 putc(' ', n_stdout);
906 fputs(&hfp->hf_dat[0], n_stdout);
907 break;
908 }else if(!strcmp(&hfpx->hf_dat[0], &hfp->hf_dat[0]))
909 break;
911 putc('\n', n_stdout);
912 goto jleave;
915 if(cmd[3] != NULL)
916 goto jecmd;
918 if(!asccasecmp(cmd[2], cp = "Subject")){
919 np = (hp->h_subject != NULL) ? (struct name*)-1 : NULL;
920 goto jlist;
922 if(!asccasecmp(cmd[2], cp = "From")){
923 np = hp->h_from;
924 jlist:
925 fprintf(n_stdout, "%s %s\n", (np == NULL ? "501" : "210"), cp);
926 goto jleave;
928 if(!asccasecmp(cmd[2], cp = "Sender")){
929 np = hp->h_sender;
930 goto jlist;
932 if(!asccasecmp(cmd[2], cp = "To")){
933 np = hp->h_to;
934 goto jlist;
936 if(!asccasecmp(cmd[2], cp = "Cc")){
937 np = hp->h_cc;
938 goto jlist;
940 if(!asccasecmp(cmd[2], cp = "Bcc")){
941 np = hp->h_bcc;
942 goto jlist;
944 if(!asccasecmp(cmd[2], cp = "Reply-To")){
945 np = hp->h_replyto;
946 goto jlist;
948 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
949 np = hp->h_mft;
950 goto jlist;
952 if(!asccasecmp(cmd[2], cp = "Message-ID")){
953 np = hp->h_message_id;
954 goto jlist;
956 if(!asccasecmp(cmd[2], cp = "References")){
957 np = hp->h_ref;
958 goto jlist;
960 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
961 np = hp->h_in_reply_to;
962 goto jlist;
965 if(!asccasecmp(cmd[2], cp = "Mailx-Command")){
966 np = (hp->h_mailx_command != NULL) ? (struct name*)-1 : NULL;
967 goto jlist;
969 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-To")){
970 np = hp->h_mailx_raw_to;
971 goto jlist;
973 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-Cc")){
974 np = hp->h_mailx_raw_cc;
975 goto jlist;
977 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-Bcc")){
978 np = hp->h_mailx_raw_bcc;
979 goto jlist;
981 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-From")){
982 np = hp->h_mailx_orig_from;
983 goto jlist;
985 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-To")){
986 np = hp->h_mailx_orig_to;
987 goto jlist;
989 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-Cc")){
990 np = hp->h_mailx_orig_cc;
991 goto jlist;
993 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-Bcc")){
994 np = hp->h_mailx_orig_bcc;
995 goto jlist;
998 /* Free-form header fields (note j501cp may print non-normalized name) */
999 for(cp = cmd[2]; *cp != '\0'; ++cp)
1000 if(!fieldnamechar(*cp)){
1001 cp = cmd[2];
1002 goto j501cp;
1004 cp = cmd[2];
1005 for(hfp = hp->h_user_headers;; hfp = hfp->hf_next){
1006 if(hfp == NULL)
1007 goto j501cp;
1008 else if(!asccasecmp(cp, &hfp->hf_dat[0])){
1009 fprintf(n_stdout, "210 %s\n", &hfp->hf_dat[0]);
1010 break;
1013 goto jleave;
1016 if(is_asccaseprefix(cmd[1], "remove")){
1017 if(cmd[2] == NULL || cmd[3] != NULL)
1018 goto jecmd;
1020 if(!asccasecmp(cmd[2], cp = "Subject")){
1021 if(hp->h_subject != NULL){
1022 hp->h_subject = NULL;
1023 fprintf(n_stdout, "210 %s\n", cp);
1024 goto jleave;
1025 }else
1026 goto j501cp;
1029 if(!asccasecmp(cmd[2], cp = "From")){
1030 npp = &hp->h_from;
1031 jrem:
1032 if(*npp != NULL){
1033 *npp = NULL;
1034 fprintf(n_stdout, "210 %s\n", cp);
1035 goto jleave;
1036 }else
1037 goto j501cp;
1039 if(!asccasecmp(cmd[2], cp = "Sender")){
1040 npp = &hp->h_sender;
1041 goto jrem;
1043 if(!asccasecmp(cmd[2], cp = "To")){
1044 npp = &hp->h_to;
1045 goto jrem;
1047 if(!asccasecmp(cmd[2], cp = "Cc")){
1048 npp = &hp->h_cc;
1049 goto jrem;
1051 if(!asccasecmp(cmd[2], cp = "Bcc")){
1052 npp = &hp->h_bcc;
1053 goto jrem;
1055 if(!asccasecmp(cmd[2], cp = "Reply-To")){
1056 npp = &hp->h_replyto;
1057 goto jrem;
1059 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
1060 npp = &hp->h_mft;
1061 goto jrem;
1063 if(!asccasecmp(cmd[2], cp = "Message-ID")){
1064 npp = &hp->h_message_id;
1065 goto jrem;
1067 if(!asccasecmp(cmd[2], cp = "References")){
1068 npp = &hp->h_ref;
1069 goto jrem;
1071 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
1072 npp = &hp->h_in_reply_to;
1073 goto jrem;
1076 if(!asccasecmp(cmd[2], cp = "Mailx-Command") ||
1077 !asccasecmp(cmd[2], cp = "Mailx-Raw-To") ||
1078 !asccasecmp(cmd[2], cp = "Mailx-Raw-Cc") ||
1079 !asccasecmp(cmd[2], cp = "Mailx-Raw-Bcc") ||
1080 !asccasecmp(cmd[2], cp = "Mailx-Orig-From") ||
1081 !asccasecmp(cmd[2], cp = "Mailx-Orig-To") ||
1082 !asccasecmp(cmd[2], cp = "Mailx-Orig-Cc") ||
1083 !asccasecmp(cmd[2], cp = "Mailx-Orig-Bcc")){
1084 fprintf(n_stdout, "505 %s\n", cp);
1085 goto jleave;
1088 /* Free-form header fields (note j501cp may print non-normalized name) */
1089 /* C99 */{
1090 struct n_header_field **hfpp;
1091 bool_t any;
1093 for(cp = cmd[2]; *cp != '\0'; ++cp)
1094 if(!fieldnamechar(*cp)){
1095 cp = cmd[2];
1096 goto j501cp;
1098 cp = cmd[2];
1100 for(any = FAL0, hfpp = &hp->h_user_headers; (hfp = *hfpp) != NULL;){
1101 if(!asccasecmp(cp, &hfp->hf_dat[0])){
1102 *hfpp = hfp->hf_next;
1103 if(!any)
1104 fprintf(n_stdout, "210 %s\n", &hfp->hf_dat[0]);
1105 any = TRU1;
1106 }else
1107 hfpp = &hfp->hf_next;
1109 if(!any)
1110 goto j501cp;
1112 goto jleave;
1115 if(is_asccaseprefix(cmd[1], "remove-at")){
1116 if(cmd[2] == NULL || cmd[3] == NULL)
1117 goto jecmd;
1119 if((n_idec_uiz_cp(&i, cmd[3], 0, NULL
1120 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
1121 ) != n_IDEC_STATE_CONSUMED || i == 0){
1122 fputs("505\n", n_stdout);
1123 goto jleave;
1126 if(!asccasecmp(cmd[2], cp = "Subject")){
1127 if(hp->h_subject != NULL && i == 1){
1128 hp->h_subject = NULL;
1129 fprintf(n_stdout, "210 %s 1\n", cp);
1130 goto jleave;
1131 }else
1132 goto j501cp;
1135 if(!asccasecmp(cmd[2], cp = "From")){
1136 npp = &hp->h_from;
1137 jremat:
1138 if((np = *npp) == NULL)
1139 goto j501cp;
1140 while(--i != 0 && np != NULL)
1141 np = np->n_flink;
1142 if(np == NULL)
1143 goto j501cp;
1145 if(np->n_blink != NULL)
1146 np->n_blink->n_flink = np->n_flink;
1147 else
1148 *npp = np->n_flink;
1149 if(np->n_flink != NULL)
1150 np->n_flink->n_blink = np->n_blink;
1152 fprintf(n_stdout, "210 %s\n", cp);
1153 goto jleave;
1155 if(!asccasecmp(cmd[2], cp = "Sender")){
1156 npp = &hp->h_sender;
1157 goto jremat;
1159 if(!asccasecmp(cmd[2], cp = "To")){
1160 npp = &hp->h_to;
1161 goto jremat;
1163 if(!asccasecmp(cmd[2], cp = "Cc")){
1164 npp = &hp->h_cc;
1165 goto jremat;
1167 if(!asccasecmp(cmd[2], cp = "Bcc")){
1168 npp = &hp->h_bcc;
1169 goto jremat;
1171 if(!asccasecmp(cmd[2], cp = "Reply-To")){
1172 npp = &hp->h_replyto;
1173 goto jremat;
1175 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
1176 npp = &hp->h_mft;
1177 goto jremat;
1179 if(!asccasecmp(cmd[2], cp = "Message-ID")){
1180 npp = &hp->h_message_id;
1181 goto jremat;
1183 if(!asccasecmp(cmd[2], cp = "References")){
1184 npp = &hp->h_ref;
1185 goto jremat;
1187 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
1188 npp = &hp->h_in_reply_to;
1189 goto jremat;
1192 if(!asccasecmp(cmd[2], cp = "Mailx-Command") ||
1193 !asccasecmp(cmd[2], cp = "Mailx-Raw-To") ||
1194 !asccasecmp(cmd[2], cp = "Mailx-Raw-Cc") ||
1195 !asccasecmp(cmd[2], cp = "Mailx-Raw-Bcc") ||
1196 !asccasecmp(cmd[2], cp = "Mailx-Orig-From") ||
1197 !asccasecmp(cmd[2], cp = "Mailx-Orig-To") ||
1198 !asccasecmp(cmd[2], cp = "Mailx-Orig-Cc") ||
1199 !asccasecmp(cmd[2], cp = "Mailx-Orig-Bcc")){
1200 fprintf(n_stdout, "505 %s\n", cp);
1201 goto jleave;
1204 /* Free-form header fields (note j501cp may print non-normalized name) */
1205 /* C99 */{
1206 struct n_header_field **hfpp;
1208 for(cp = cmd[2]; *cp != '\0'; ++cp)
1209 if(!fieldnamechar(*cp)){
1210 cp = cmd[2];
1211 goto j501cp;
1213 cp = cmd[2];
1215 for(hfpp = &hp->h_user_headers; (hfp = *hfpp) != NULL;){
1216 if(--i == 0){
1217 *hfpp = hfp->hf_next;
1218 fprintf(n_stdout, "210 %s %" PRIuZ "\n", &hfp->hf_dat[0], i);
1219 break;
1220 }else
1221 hfpp = &hfp->hf_next;
1223 if(hfp == NULL)
1224 goto j501cp;
1226 goto jleave;
1229 if(is_asccaseprefix(cmd[1], "show")){
1230 if(cmd[2] == NULL || cmd[3] != NULL)
1231 goto jecmd;
1233 if(!asccasecmp(cmd[2], cp = "Subject")){
1234 if(hp->h_subject == NULL)
1235 goto j501cp;
1236 fprintf(n_stdout, "212 %s\n%s\n\n", cp, hp->h_subject);
1237 goto jleave;
1240 if(!asccasecmp(cmd[2], cp = "From")){
1241 np = hp->h_from;
1242 jshow:
1243 if(np != NULL){
1244 fprintf(n_stdout, "211 %s\n", cp);
1245 do if(!(np->n_type & GDEL))
1246 fprintf(n_stdout, "%s %s\n", np->n_name, np->n_fullname);
1247 while((np = np->n_flink) != NULL);
1248 putc('\n', n_stdout);
1249 goto jleave;
1250 }else
1251 goto j501cp;
1253 if(!asccasecmp(cmd[2], cp = "Sender")){
1254 np = hp->h_sender;
1255 goto jshow;
1257 if(!asccasecmp(cmd[2], cp = "To")){
1258 np = hp->h_to;
1259 goto jshow;
1261 if(!asccasecmp(cmd[2], cp = "Cc")){
1262 np = hp->h_cc;
1263 goto jshow;
1265 if(!asccasecmp(cmd[2], cp = "Bcc")){
1266 np = hp->h_bcc;
1267 goto jshow;
1269 if(!asccasecmp(cmd[2], cp = "Reply-To")){
1270 np = hp->h_replyto;
1271 goto jshow;
1273 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
1274 np = hp->h_mft;
1275 goto jshow;
1277 if(!asccasecmp(cmd[2], cp = "Message-ID")){
1278 np = hp->h_message_id;
1279 goto jshow;
1281 if(!asccasecmp(cmd[2], cp = "References")){
1282 np = hp->h_ref;
1283 goto jshow;
1285 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
1286 np = hp->h_in_reply_to;
1287 goto jshow;
1290 if(!asccasecmp(cmd[2], cp = "Mailx-Command")){
1291 if(hp->h_mailx_command == NULL)
1292 goto j501cp;
1293 fprintf(n_stdout, "212 %s\n%s\n\n", cp, hp->h_mailx_command);
1294 goto jleave;
1296 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-To")){
1297 np = hp->h_mailx_raw_to;
1298 goto jshow;
1300 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-Cc")){
1301 np = hp->h_mailx_raw_cc;
1302 goto jshow;
1304 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-Bcc")){
1305 np = hp->h_mailx_raw_bcc;
1306 goto jshow;
1308 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-From")){
1309 np = hp->h_mailx_orig_from;
1310 goto jshow;
1312 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-To")){
1313 np = hp->h_mailx_orig_to;
1314 goto jshow;
1316 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-Cc")){
1317 np = hp->h_mailx_orig_cc;
1318 goto jshow;
1320 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-Bcc")){
1321 np = hp->h_mailx_orig_bcc;
1322 goto jshow;
1325 /* Free-form header fields (note j501cp may print non-normalized name) */
1326 /* C99 */{
1327 bool_t any;
1329 for(cp = cmd[2]; *cp != '\0'; ++cp)
1330 if(!fieldnamechar(*cp)){
1331 cp = cmd[2];
1332 goto j501cp;
1334 cp = cmd[2];
1336 for(any = FAL0, hfp = hp->h_user_headers; hfp != NULL;
1337 hfp = hfp->hf_next){
1338 if(!asccasecmp(cp, &hfp->hf_dat[0])){
1339 if(!any)
1340 fprintf(n_stdout, "212 %s\n", &hfp->hf_dat[0]);
1341 any = TRU1;
1342 fprintf(n_stdout, "%s\n", &hfp->hf_dat[hfp->hf_nl +1]);
1345 if(any)
1346 putc('\n', n_stdout);
1347 else
1348 goto j501cp;
1350 goto jleave;
1353 jecmd:
1354 fputs("500\n", n_stdout);
1355 cp = NULL;
1356 jleave:
1357 NYD2_LEAVE;
1358 return (cp != NULL);
1360 j501cp:
1361 fputs("501 ", n_stdout);
1362 fputs(cp, n_stdout);
1363 putc('\n', n_stdout);
1364 goto jleave;
1367 static bool_t
1368 a_collect__plumb_attach(char const *cp, struct header *hp,
1369 char const *cmd[4]){
1370 bool_t status;
1371 struct attachment *ap;
1372 NYD2_ENTER;
1374 if(cmd[1] == NULL)
1375 goto jdefault;
1377 if(is_asccaseprefix(cmd[1], "attribute")){
1378 if(cmd[2] == NULL || cmd[3] != NULL)
1379 goto jecmd;
1381 if((ap = n_attachment_find(hp->h_attach, cmd[2], NULL)) != NULL){
1382 jatt_att:
1383 fprintf(n_stdout, "212 %s\n", cmd[2]);
1384 if(ap->a_msgno > 0)
1385 fprintf(n_stdout, "message-number %d\n\n", ap->a_msgno);
1386 else{
1387 fprintf(n_stdout,
1388 "creation-name %s\nopen-path %s\nfilename %s\n",
1389 ap->a_path_user, ap->a_path, ap->a_name);
1390 if(ap->a_content_description != NULL)
1391 fprintf(n_stdout, "content-description %s\n",
1392 ap->a_content_description);
1393 if(ap->a_content_id != NULL)
1394 fprintf(n_stdout, "content-id %s\n",
1395 ap->a_content_id->n_name);
1396 if(ap->a_content_type != NULL)
1397 fprintf(n_stdout, "content-type %s\n", ap->a_content_type);
1398 if(ap->a_content_disposition != NULL)
1399 fprintf(n_stdout, "content-disposition %s\n",
1400 ap->a_content_disposition);
1401 putc('\n', n_stdout);
1403 }else
1404 fputs("501\n", n_stdout);
1405 goto jleave;
1408 if(is_asccaseprefix(cmd[1], "attribute-at")){
1409 uiz_t i;
1411 if(cmd[2] == NULL || cmd[3] != NULL)
1412 goto jecmd;
1414 if((n_idec_uiz_cp(&i, cmd[2], 0, NULL
1415 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
1416 ) != n_IDEC_STATE_CONSUMED || i == 0)
1417 fputs("505\n", n_stdout);
1418 else{
1419 for(ap = hp->h_attach; ap != NULL && --i != 0; ap = ap->a_flink)
1421 if(ap != NULL)
1422 goto jatt_att;
1423 else
1424 fputs("501\n", n_stdout);
1426 goto jleave;
1429 if(is_asccaseprefix(cmd[1], "attribute-set")){
1430 if(cmd[2] == NULL || cmd[3] == NULL)
1431 goto jecmd;
1433 if((ap = n_attachment_find(hp->h_attach, cmd[2], NULL)) != NULL){
1434 jatt_attset:
1435 if(ap->a_msgno > 0)
1436 fputs("505\n", n_stdout);
1437 else{
1438 char c, *keyw;
1440 cp = cmd[3];
1441 while((c = *cp) != '\0' && !blankchar(c))
1442 ++cp;
1443 keyw = savestrbuf(cmd[3], PTR2SIZE(cp - cmd[3]));
1444 if(c != '\0'){
1445 for(; (c = *++cp) != '\0' && blankchar(c);)
1447 if(c != '\0'){
1448 char *xp;
1450 /* Strip [\r\n] which would render a parameter invalid XXX
1451 * XXX all controls? */
1452 cp = xp = savestr(cp);
1453 for(; (c = *xp) != '\0'; ++xp)
1454 if(c == '\n' || c == '\r')
1455 *xp = ' ';
1456 c = *cp;
1460 if(!asccasecmp(keyw, "filename"))
1461 ap->a_name = (c == '\0') ? ap->a_path_bname : cp;
1462 else if(!asccasecmp(keyw, "content-description"))
1463 ap->a_content_description = (c == '\0') ? NULL : cp;
1464 else if(!asccasecmp(keyw, "content-id")){
1465 ap->a_content_id = NULL;
1467 if(c != '\0'){
1468 struct name *np;
1470 np = checkaddrs(lextract(cp, GREF),
1471 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG |
1472 EACM_NONAME, NULL);
1473 if(np != NULL && np->n_flink == NULL)
1474 ap->a_content_id = np;
1475 else
1476 cp = NULL;
1478 }else if(!asccasecmp(keyw, "content-type"))
1479 ap->a_content_type = (c == '\0') ? NULL : cp;
1480 else if(!asccasecmp(keyw, "content-disposition"))
1481 ap->a_content_disposition = (c == '\0') ? NULL : cp;
1482 else
1483 cp = NULL;
1485 if(cp != NULL){
1486 size_t i;
1488 for(i = 0; ap != NULL; ++i, ap = ap->a_blink)
1490 fprintf(n_stdout, "210 %" PRIuZ "\n", i);
1491 }else
1492 fputs("505\n", n_stdout);
1494 }else
1495 fputs("501\n", n_stdout);
1496 goto jleave;
1499 if(is_asccaseprefix(cmd[1], "attribute-set-at")){
1500 uiz_t i;
1502 if(cmd[2] == NULL || cmd[3] == NULL)
1503 goto jecmd;
1505 if((n_idec_uiz_cp(&i, cmd[2], 0, NULL
1506 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
1507 ) != n_IDEC_STATE_CONSUMED || i == 0)
1508 fputs("505\n", n_stdout);
1509 else{
1510 for(ap = hp->h_attach; ap != NULL && --i != 0; ap = ap->a_flink)
1512 if(ap != NULL)
1513 goto jatt_attset;
1514 else
1515 fputs("501\n", n_stdout);
1517 goto jleave;
1520 if(is_asccaseprefix(cmd[1], "insert")){
1521 enum n_attach_error aerr;
1523 if(cmd[2] == NULL || cmd[3] != NULL)
1524 goto jecmd;
1526 hp->h_attach = n_attachment_append(hp->h_attach, cmd[2], &aerr, &ap);
1527 switch(aerr){
1528 case n_ATTACH_ERR_FILE_OPEN: cp = "505\n"; goto jatt_ins;
1529 case n_ATTACH_ERR_ICONV_FAILED: cp = "506\n"; goto jatt_ins;
1530 case n_ATTACH_ERR_ICONV_NAVAIL:
1531 case n_ATTACH_ERR_OTHER:
1532 default:
1533 cp = "501\n";
1534 jatt_ins:
1535 fputs(cp, n_stdout);
1536 break;
1537 case n_ATTACH_ERR_NONE:{
1538 size_t i;
1540 for(i = 0; ap != NULL; ++i, ap = ap->a_blink)
1542 fprintf(n_stdout, "210 %" PRIuZ "\n", i);
1543 } break;
1545 goto jleave;
1548 if(is_asccaseprefix(cmd[1], "list")){
1549 jdefault:
1550 if(cmd[2] != NULL)
1551 goto jecmd;
1553 if((ap = hp->h_attach) != NULL){
1554 fputs("212\n", n_stdout);
1556 fprintf(n_stdout, "%s\n", ap->a_path_user);
1557 while((ap = ap->a_flink) != NULL);
1558 putc('\n', n_stdout);
1559 }else
1560 fputs("501\n", n_stdout);
1561 goto jleave;
1564 if(is_asccaseprefix(cmd[1], "remove")){
1565 if(cmd[2] == NULL || cmd[3] != NULL)
1566 goto jecmd;
1568 if((ap = n_attachment_find(hp->h_attach, cmd[2], &status)) != NULL){
1569 if(status == TRUM1)
1570 fputs("506\n", n_stdout);
1571 else{
1572 hp->h_attach = n_attachment_remove(hp->h_attach, ap);
1573 fprintf(n_stdout, "210 %s\n", cmd[2]);
1575 }else
1576 fputs("501\n", n_stdout);
1577 goto jleave;
1580 if(is_asccaseprefix(cmd[1], "remove-at")){
1581 uiz_t i;
1583 if(cmd[2] == NULL || cmd[3] != NULL)
1584 goto jecmd;
1586 if((n_idec_uiz_cp(&i, cmd[2], 0, NULL
1587 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
1588 ) != n_IDEC_STATE_CONSUMED || i == 0)
1589 fputs("505\n", n_stdout);
1590 else{
1591 for(ap = hp->h_attach; ap != NULL && --i != 0; ap = ap->a_flink)
1593 if(ap != NULL){
1594 hp->h_attach = n_attachment_remove(hp->h_attach, ap);
1595 fprintf(n_stdout, "210 %s\n", cmd[2]);
1596 }else
1597 fputs("501\n", n_stdout);
1599 goto jleave;
1602 jecmd:
1603 fputs("500\n", n_stdout);
1604 cp = NULL;
1605 jleave:
1606 NYD2_LEAVE;
1607 return (cp != NULL);
1610 static void
1611 _collint(int s)
1613 NYD_X; /* Signal handler */
1615 /* the control flow is subtle, because we can be called from ~q */
1616 if (_coll_hadintr == 0) {
1617 if (ok_blook(ignore)) {
1618 fputs("@\n", n_stdout);
1619 fflush(n_stdout);
1620 clearerr(n_stdin);
1621 } else
1622 _coll_hadintr = 1;
1623 siglongjmp(_coll_jmp, 1);
1625 n_exit_status |= n_EXIT_SEND_ERROR;
1626 if (s != 0)
1627 savedeadletter(_coll_fp, TRU1);
1628 /* Aborting message, no need to fflush() .. */
1629 siglongjmp(_coll_abort, 1);
1632 static void
1633 collhup(int s)
1635 NYD_X; /* Signal handler */
1636 n_UNUSED(s);
1638 savedeadletter(_coll_fp, TRU1);
1639 /* Let's pretend nobody else wants to clean up, a true statement at
1640 * this time */
1641 exit(n_EXIT_ERR);
1644 static int
1645 putesc(char const *s, FILE *stream) /* TODO: v15: drop */
1647 int n = 0, rv = -1;
1648 NYD_ENTER;
1650 while (s[0] != '\0') {
1651 if (s[0] == '\\') {
1652 if (s[1] == 't') {
1653 if (putc('\t', stream) == EOF)
1654 goto jleave;
1655 ++n;
1656 s += 2;
1657 continue;
1659 if (s[1] == 'n') {
1660 if (putc('\n', stream) == EOF)
1661 goto jleave;
1662 ++n;
1663 s += 2;
1664 continue;
1667 if (putc(s[0], stream) == EOF)
1668 goto jleave;
1669 ++n;
1670 ++s;
1672 if (putc('\n', stream) == EOF)
1673 goto jleave;
1674 rv = ++n;
1675 jleave:
1676 NYD_LEAVE;
1677 return rv;
1680 static int
1681 a_coll_ocs__mac(void){
1682 /* Executes in a fork(2)ed child TODO if remains, global MASKs for those! */
1683 setvbuf(n_stdin, NULL, _IOLBF, 0);
1684 setvbuf(n_stdout, NULL, _IOLBF, 0);
1685 n_psonce &= ~(n_PSO_INTERACTIVE | n_PSO_TTYIN | n_PSO_TTYOUT);
1686 n_pstate |= n_PS_COMPOSE_FORKHOOK;
1687 n_readctl_overlay = NULL; /* TODO we need OnForkEvent! See c_readctl() */
1688 if(n_poption & n_PO_D_VV){
1689 char buf[128];
1691 snprintf(buf, sizeof buf, "[%d]%s", getpid(), ok_vlook(log_prefix));
1692 ok_vset(log_prefix, buf);
1694 /* TODO If that uses `!' it will effectively SIG_IGN SIGINT, ...and such */
1695 temporary_compose_mode_hook_call(a_coll_ocs__macname, NULL, NULL);
1696 return 0;
1699 static void
1700 a_coll_ocs__finalize(void *vp){
1701 /* Note we use this for destruction upon setup errors, thus */
1702 sighandler_type opipe;
1703 sighandler_type oint;
1704 struct a_coll_ocs_arg **coapp, *coap;
1705 NYD2_ENTER;
1707 temporary_compose_mode_hook_call((char*)-1, NULL, NULL);
1709 coap = *(coapp = vp);
1710 *coapp = (struct a_coll_ocs_arg*)-1;
1712 if(coap->coa_stdin != NULL)
1713 Fclose(coap->coa_stdin);
1714 else if(coap->coa_pipe[0] != -1)
1715 close(coap->coa_pipe[0]);
1717 if(coap->coa_stdout != NULL && !Pclose(coap->coa_stdout, TRU1))
1718 *coap->coa_senderr = 111;
1719 if(coap->coa_pipe[1] != -1)
1720 close(coap->coa_pipe[1]);
1722 opipe = coap->coa_opipe;
1723 oint = coap->coa_oint;
1725 n_lofi_free(coap);
1727 hold_all_sigs();
1728 safe_signal(SIGPIPE, opipe);
1729 safe_signal(SIGINT, oint);
1730 rele_all_sigs();
1731 NYD2_LEAVE;
1734 FL void
1735 n_temporary_compose_hook_varset(void *arg){ /* TODO v15: drop */
1736 struct header *hp;
1737 char const *val;
1738 NYD2_ENTER;
1740 hp = arg;
1742 if((val = hp->h_subject) == NULL)
1743 val = n_empty;
1744 ok_vset(mailx_subject, val);
1745 if((val = detract(hp->h_from, GNAMEONLY)) == NULL)
1746 val = n_empty;
1747 ok_vset(mailx_from, val);
1748 if((val = detract(hp->h_sender, GNAMEONLY)) == NULL)
1749 val = n_empty;
1750 ok_vset(mailx_sender, val);
1751 if((val = detract(hp->h_to, GNAMEONLY)) == NULL)
1752 val = n_empty;
1753 ok_vset(mailx_to, val);
1754 if((val = detract(hp->h_cc, GNAMEONLY)) == NULL)
1755 val = n_empty;
1756 ok_vset(mailx_cc, val);
1757 if((val = detract(hp->h_bcc, GNAMEONLY)) == NULL)
1758 val = n_empty;
1759 ok_vset(mailx_bcc, val);
1761 if((val = hp->h_mailx_command) == NULL)
1762 val = n_empty;
1763 ok_vset(mailx_command, val);
1765 if((val = detract(hp->h_mailx_raw_to, GNAMEONLY)) == NULL)
1766 val = n_empty;
1767 ok_vset(mailx_raw_to, val);
1768 if((val = detract(hp->h_mailx_raw_cc, GNAMEONLY)) == NULL)
1769 val = n_empty;
1770 ok_vset(mailx_raw_cc, val);
1771 if((val = detract(hp->h_mailx_raw_bcc, GNAMEONLY)) == NULL)
1772 val = n_empty;
1773 ok_vset(mailx_raw_bcc, val);
1775 if((val = detract(hp->h_mailx_orig_from, GNAMEONLY)) == NULL)
1776 val = n_empty;
1777 ok_vset(mailx_orig_from, val);
1778 if((val = detract(hp->h_mailx_orig_to, GNAMEONLY)) == NULL)
1779 val = n_empty;
1780 ok_vset(mailx_orig_to, val);
1781 if((val = detract(hp->h_mailx_orig_cc, GNAMEONLY)) == NULL)
1782 val = n_empty;
1783 ok_vset(mailx_orig_cc, val);
1784 if((val = detract(hp->h_mailx_orig_bcc, GNAMEONLY)) == NULL)
1785 val = n_empty;
1786 ok_vset(mailx_orig_bcc, val);
1787 NYD2_LEAVE;
1790 FL FILE *
1791 collect(struct header *hp, int printheaders, struct message *mp,
1792 char const *quotefile, int doprefix, si8_t *checkaddr_err)
1794 struct n_string s, * volatile sp;
1795 struct n_ignore const *quoteitp;
1796 struct a_coll_ocs_arg *coap;
1797 int c;
1798 int volatile t, eofcnt, getfields;
1799 char volatile escape;
1800 enum{
1801 a_NONE,
1802 a_ERREXIT = 1u<<0,
1803 a_IGNERR = 1u<<1,
1804 a_COAP_NOSIGTERM = 1u<<8
1805 #define a_HARDERR() ((flags & (a_ERREXIT | a_IGNERR)) == a_ERREXIT)
1806 } volatile flags;
1807 char *linebuf;
1808 char const *cp, *cp_base, * volatile coapm, * volatile ifs_saved;
1809 size_t i, linesize; /* TODO line pool */
1810 long cnt;
1811 enum sendaction action;
1812 sigset_t oset, nset;
1813 FILE * volatile sigfp;
1814 NYD_ENTER;
1816 _coll_fp = NULL;
1817 sigfp = NULL;
1818 linesize = 0;
1819 linebuf = NULL;
1820 eofcnt = 0;
1821 ifs_saved = coapm = NULL;
1822 coap = NULL;
1823 sp = NULL;
1825 /* Start catching signals from here, but we're still die on interrupts
1826 * until we're in the main loop */
1827 sigfillset(&nset);
1828 sigprocmask(SIG_BLOCK, &nset, &oset);
1829 if ((_coll_saveint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
1830 safe_signal(SIGINT, &_collint);
1831 if ((_coll_savehup = safe_signal(SIGHUP, SIG_IGN)) != SIG_IGN)
1832 safe_signal(SIGHUP, collhup);
1833 if (sigsetjmp(_coll_abort, 1))
1834 goto jerr;
1835 if (sigsetjmp(_coll_jmp, 1))
1836 goto jerr;
1837 n_pstate |= n_PS_COMPOSE_MODE;
1838 sigprocmask(SIG_SETMASK, &oset, (sigset_t*)NULL);
1840 if ((_coll_fp = Ftmp(NULL, "collect", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
1841 NULL) {
1842 n_perr(_("temporary mail file"), 0);
1843 goto jerr;
1846 /* If we are going to prompt for a subject, refrain from printing a newline
1847 * after the headers (since some people mind) */
1848 getfields = 0;
1849 if (!(n_poption & n_PO_t_FLAG)) {
1850 t = GTO | GSUBJECT | GCC | GNL;
1851 if (ok_blook(fullnames))
1852 t |= GCOMMA;
1854 if (n_psonce & n_PSO_INTERACTIVE) {
1855 if (hp->h_subject == NULL && (ok_blook(ask) || ok_blook(asksub)))
1856 t &= ~GNL, getfields |= GSUBJECT;
1858 if (hp->h_to == NULL)
1859 t &= ~GNL, getfields |= GTO;
1861 if (!ok_blook(bsdcompat) && !ok_blook(askatend)) {
1862 if (hp->h_bcc == NULL && ok_blook(askbcc))
1863 t &= ~GNL, getfields |= GBCC;
1864 if (hp->h_cc == NULL && ok_blook(askcc))
1865 t &= ~GNL, getfields |= GCC;
1868 } else {
1869 n_UNINIT(t, 0);
1872 _coll_hadintr = 0;
1874 if (!sigsetjmp(_coll_jmp, 1)) {
1875 /* Ask for some headers first, as necessary */
1876 if (getfields)
1877 grab_headers(n_GO_INPUT_CTX_COMPOSE, hp, getfields, 1);
1879 /* Execute compose-enter; delayed for -t mode */
1880 if(!(n_poption & n_PO_t_FLAG) &&
1881 (cp = ok_vlook(on_compose_enter)) != NULL){
1882 setup_from_and_sender(hp);
1883 temporary_compose_mode_hook_call(cp, &n_temporary_compose_hook_varset,
1884 hp);
1887 /* TODO Mm: nope since it may require turning this into a multipart one */
1888 if(!(n_poption & (n_PO_Mm_FLAG | n_PO_t_FLAG))){
1889 if(!a_coll_message_inject_head(_coll_fp))
1890 goto jerr;
1892 /* Quote an original message */
1893 if (mp != NULL && (doprefix || (cp = ok_vlook(quote)) != NULL)) {
1894 quoteitp = n_IGNORE_ALL;
1895 action = SEND_QUOTE;
1896 if (doprefix) {
1897 char const *cp_v15compat;
1899 quoteitp = n_IGNORE_FWD;
1901 if((cp_v15compat = ok_vlook(fwdheading)) != NULL)
1902 n_OBSOLETE(_("please use *forward-inject-head*, "
1903 "not *fwdheading*"));
1904 cp = ok_vlook(forward_inject_head);
1905 if(cp == NULL && (cp = cp_v15compat) == NULL)
1906 cp = "-------- Original Message --------";
1907 if (*cp != '\0' && fprintf(_coll_fp, "%s\n", cp) < 0)
1908 goto jerr;
1909 } else if (!strcmp(cp, "noheading")) {
1910 /*EMPTY*/;
1911 } else if (!strcmp(cp, "headers")) {
1912 quoteitp = n_IGNORE_TYPE;
1913 } else if (!strcmp(cp, "allheaders")) {
1914 quoteitp = NULL;
1915 action = SEND_QUOTE_ALL;
1916 } else {
1917 cp = hfield1("from", mp);
1918 if (cp != NULL && (cnt = (long)strlen(cp)) > 0) {
1919 if (xmime_write(cp, cnt, _coll_fp, CONV_FROMHDR, TD_NONE) < 0)
1920 goto jerr;
1921 if (fprintf(_coll_fp, _(" wrote:\n\n")) < 0)
1922 goto jerr;
1925 if (fflush(_coll_fp))
1926 goto jerr;
1927 if (doprefix)
1928 cp = NULL;
1929 else if ((cp = ok_vlook(indentprefix)) == NULL)
1930 cp = INDENT_DEFAULT;
1931 if (sendmp(mp, _coll_fp, quoteitp, cp, action, NULL) < 0)
1932 goto jerr;
1936 if (quotefile != NULL) {
1937 if((n_pstate_err_no = a_coll_include_file(quotefile, FAL0, FAL0)
1938 ) != n_ERR_NONE)
1939 goto jerr;
1942 if(n_psonce & n_PSO_INTERACTIVE){
1943 if(!(n_pstate & n_PS_SOURCING)){
1944 sp = n_string_creat_auto(&s);
1945 sp = n_string_reserve(sp, 80);
1948 if(!(n_poption & n_PO_Mm_FLAG)){
1949 /* Print what we have sofar also on the terminal (if useful) */
1950 if (!ok_blook(editalong)) {
1951 if (printheaders)
1952 puthead(TRU1, hp, n_stdout, t,
1953 SEND_TODISP, CONV_NONE, NULL, NULL);
1955 rewind(_coll_fp);
1956 while ((c = getc(_coll_fp)) != EOF) /* XXX bytewise, yuck! */
1957 putc(c, n_stdout);
1958 if (fseek(_coll_fp, 0, SEEK_END))
1959 goto jerr;
1960 } else {
1961 rewind(_coll_fp);
1962 if(a_coll_edit('e', hp) != n_ERR_NONE)
1963 goto jerr;
1964 /* Print msg mandated by the Mail Reference Manual */
1965 jcont:
1966 if(n_psonce & n_PSO_INTERACTIVE)
1967 fputs(_("(continue)\n"), n_stdout);
1969 fflush(n_stdout);
1972 } else {
1973 /* Come here for printing the after-signal message. Duplicate messages
1974 * won't be printed because the write is aborted if we get a SIGTTOU */
1975 if (_coll_hadintr)
1976 n_err(_("\n(Interrupt -- one more to kill letter)\n"));
1979 /* If not under shell hook control */
1980 if(coap == NULL){
1981 /* We're done with -M or -m TODO because: we are too stupid yet, above */
1982 if(n_poption & n_PO_Mm_FLAG)
1983 goto jout;
1984 /* No command escapes, interrupts not expected. Simply copy STDIN */
1985 if(!(n_psonce & n_PSO_INTERACTIVE) &&
1986 !(n_poption & (n_PO_t_FLAG | n_PO_TILDE_FLAG))){
1987 linebuf = srealloc(linebuf, linesize = LINESIZE);
1988 while ((i = fread(linebuf, sizeof *linebuf, linesize, n_stdin)) > 0) {
1989 if (i != fwrite(linebuf, sizeof *linebuf, i, _coll_fp))
1990 goto jerr;
1992 goto jout;
1996 /* The interactive collect loop */
1997 if(coap == NULL)
1998 escape = *ok_vlook(escape);
1999 flags = ok_blook(errexit) ? a_ERREXIT : a_NONE;
2001 for(;;){
2002 enum {a_HIST_NONE, a_HIST_ADD = 1u<<0, a_HIST_GABBY = 1u<<1} hist;
2004 /* C99 */{
2005 enum n_go_input_flags gif;
2006 bool_t histadd;
2008 gif = n_GO_INPUT_CTX_COMPOSE;
2009 histadd = (sp != NULL);
2010 if((n_psonce & n_PSO_INTERACTIVE) || (n_poption & n_PO_TILDE_FLAG)){
2011 if(!(n_poption & n_PO_t_FLAG))
2012 gif |= n_GO_INPUT_NL_ESC;
2014 cnt = n_go_input(gif, n_empty, &linebuf, &linesize, NULL, &histadd);
2015 hist = histadd ? a_HIST_ADD | a_HIST_GABBY : a_HIST_NONE;
2018 if(cnt < 0){
2019 if(coap != NULL)
2020 break;
2021 if(n_poption & n_PO_t_FLAG){
2022 fflush_rewind(_coll_fp);
2023 /* It is important to set n_PSO_t_FLAG before extract_header()
2024 * *and* keep n_PO_t_FLAG for the first parse of the message!
2025 * TODO This is a hack, we need a clean approach for this */
2026 n_psonce |= n_PSO_t_FLAG;
2027 if(!a_coll_makeheader(_coll_fp, hp, checkaddr_err, TRU1))
2028 goto jerr;
2029 n_poption &= ~n_PO_t_FLAG;
2030 continue;
2031 }else if((n_psonce & n_PSO_INTERACTIVE) && ok_blook(ignoreeof) &&
2032 ++eofcnt < 4){
2033 fprintf(n_stdout,
2034 _("*ignoreeof* set, use `~.' to terminate letter\n"));
2035 n_go_input_clearerr();
2036 continue;
2038 break;
2041 _coll_hadintr = 0;
2043 cp = linebuf;
2044 if(cnt == 0)
2045 goto jputnl;
2046 else if(coap == NULL){
2047 if(!(n_psonce & n_PSO_INTERACTIVE) && !(n_poption & n_PO_TILDE_FLAG))
2048 goto jputline;
2049 else if(cp[0] == '.'){
2050 if(cnt == 1 && (ok_blook(dot) ||
2051 (ok_blook(posix) && ok_blook(ignoreeof))))
2052 break;
2055 if(cp[0] != escape){
2056 jputline:
2057 if(fwrite(cp, sizeof *cp, cnt, _coll_fp) != (size_t)cnt)
2058 goto jerr;
2059 /* TODO n_PS_READLINE_NL is a terrible hack to ensure that _in_all_-
2060 * TODO _code_paths_ a file without trailing newline isn't modified
2061 * TODO to contain one; the "saw-newline" needs to be part of an
2062 * TODO I/O input machinery object */
2063 jputnl:
2064 if(n_pstate & n_PS_READLINE_NL){
2065 if(putc('\n', _coll_fp) == EOF)
2066 goto jerr;
2068 continue;
2071 c = *(cp_base = ++cp);
2072 if(--cnt == 0)
2073 goto jearg;
2075 /* Avoid history entry? */
2076 while(spacechar(c)){
2077 hist = a_HIST_NONE;
2078 c = *(cp_base = ++cp);
2079 if(--cnt == 0)
2080 goto jearg;
2083 /* It may just be an escaped escaped character, do that quick */
2084 if(c == escape)
2085 goto jputline;
2087 /* Avoid hard *errexit*? */
2088 flags &= ~a_IGNERR;
2089 if(c == '-'){
2090 flags ^= a_IGNERR;
2091 c = *++cp;
2092 if(--cnt == 0)
2093 goto jearg;
2096 /* Trim input, also to gain a somewhat normalized history entry */
2097 ++cp;
2098 if(--cnt > 0){
2099 struct str x;
2101 x.s = n_UNCONST(cp);
2102 x.l = (size_t)cnt;
2103 n_str_trim_ifs(&x, TRU1);
2104 x.s[x.l] = '\0';
2105 cp = x.s;
2106 cnt = (int)/*XXX*/x.l;
2109 if(hist != a_HIST_NONE){
2110 sp = n_string_assign_c(sp, escape);
2111 if(flags & a_IGNERR)
2112 sp = n_string_push_c(sp, '-');
2113 sp = n_string_push_c(sp, c);
2114 if(cnt > 0){
2115 sp = n_string_push_c(sp, ' ');
2116 sp = n_string_push_buf(sp, cp, cnt);
2120 switch(c){
2121 default:
2122 if(1){
2123 char buf[sizeof(n_UNIREPL)];
2125 if(asciichar(c))
2126 buf[0] = c, buf[1] = '\0';
2127 else if(n_psonce & n_PSO_UNICODE)
2128 memcpy(buf, n_unirepl, sizeof n_unirepl);
2129 else
2130 buf[0] = '?', buf[1] = '\0';
2131 n_err(_("Unknown command escape: `%c%s'\n"), escape, buf);
2132 }else
2133 jearg:
2134 n_err(_("Invalid command escape usage: %s\n"),
2135 n_shexp_quote_cp(linebuf, FAL0));
2136 if(a_HARDERR())
2137 goto jerr;
2138 n_pstate_err_no = n_ERR_INVAL;
2139 n_pstate_ex_no = 1;
2140 continue;
2141 case '!':
2142 /* Shell escape, send the balance of line to sh -c */
2143 if(cnt == 0 || coap != NULL)
2144 goto jearg;
2145 else{
2146 char const *argv[2];
2148 argv[0] = cp;
2149 argv[1] = NULL;
2150 n_pstate_ex_no = c_shell(argv); /* TODO history norm.; errexit? */
2152 goto jhistcont;
2153 case ':':
2154 /* FALLTHRU */
2155 case '_':
2156 /* Escape to command mode, but be nice! *//* TODO command expansion
2157 * TODO should be handled here so that we have unique history! */
2158 if(cnt == 0)
2159 goto jearg;
2160 _execute_command(hp, cp, cnt);
2161 if(ok_blook(errexit))
2162 flags |= a_ERREXIT;
2163 else
2164 flags &= ~a_ERREXIT;
2165 if(n_pstate_ex_no != 0 && a_HARDERR())
2166 goto jerr;
2167 if(coap == NULL)
2168 escape = *ok_vlook(escape);
2169 break;
2170 case '.':
2171 /* Simulate end of file on input */
2172 if(cnt != 0 || coap != NULL)
2173 goto jearg;
2174 goto jout; /* TODO does not enter history, thus */
2175 case 'x':
2176 /* Same as 'q', but no *DEAD* saving */
2177 /* FALLTHRU */
2178 case 'q':
2179 /* Force a quit, act like an interrupt had happened */
2180 if(cnt != 0)
2181 goto jearg;
2182 /* If we are running a splice hook, assume it quits on its own now,
2183 * otherwise we (no true out-of-band IPC to signal this state, XXX sic)
2184 * have to SIGTERM it in order to stop this wild beast */
2185 flags |= a_COAP_NOSIGTERM;
2186 ++_coll_hadintr;
2187 _collint((c == 'x') ? 0 : SIGINT);
2188 exit(n_EXIT_ERR);
2189 /*NOTREACHED*/
2190 case 'h':
2191 /* Grab a bunch of headers */
2192 if(cnt != 0)
2193 goto jearg;
2195 grab_headers(n_GO_INPUT_CTX_COMPOSE, hp,
2196 (GTO | GSUBJECT | GCC | GBCC),
2197 (ok_blook(bsdcompat) && ok_blook(bsdorder)));
2198 while(hp->h_to == NULL);
2199 n_pstate_err_no = n_ERR_NONE; /* XXX */
2200 n_pstate_ex_no = 0; /* XXX */
2201 break;
2202 case 'H':
2203 /* Grab extra headers */
2204 if(cnt != 0)
2205 goto jearg;
2207 grab_headers(n_GO_INPUT_CTX_COMPOSE, hp, GEXTRA, 0);
2208 while(check_from_and_sender(hp->h_from, hp->h_sender) == NULL);
2209 n_pstate_err_no = n_ERR_NONE; /* XXX */
2210 n_pstate_ex_no = 0; /* XXX */
2211 break;
2212 case 't':
2213 /* Add to the To: list */
2214 if(cnt == 0)
2215 goto jearg;
2216 else{
2217 struct name *np;
2218 si8_t soe;
2220 soe = 0;
2221 if((np = checkaddrs(lextract(cp, GTO | GFULL), EACM_NORMAL, &soe)
2222 ) != NULL)
2223 hp->h_to = cat(hp->h_to, np);
2224 if(soe == 0){
2225 n_pstate_err_no = n_ERR_NONE;
2226 n_pstate_ex_no = 0;
2227 }else{
2228 n_pstate_ex_no = 1;
2229 n_pstate_err_no = (soe < 0) ? n_ERR_PERM : n_ERR_INVAL;
2232 hist &= ~a_HIST_GABBY;
2233 break;
2234 case 's':
2235 /* Set the Subject list */
2236 if(cnt == 0)
2237 goto jearg;
2238 /* Subject:; take care for Debian #419840 and strip any \r and \n */
2239 if(n_anyof_cp("\n\r", hp->h_subject = savestr(cp))){
2240 char *xp;
2242 n_err(_("-s: normalizing away invalid ASCII NL / CR bytes\n"));
2243 for(xp = hp->h_subject; *xp != '\0'; ++xp)
2244 if(*xp == '\n' || *xp == '\r')
2245 *xp = ' ';
2246 n_pstate_err_no = n_ERR_INVAL;
2247 n_pstate_ex_no = 1;
2248 }else{
2249 n_pstate_err_no = n_ERR_NONE;
2250 n_pstate_ex_no = 0;
2252 break;
2253 case '@':{
2254 struct attachment *aplist;
2256 /* Edit the attachment list */
2257 aplist = hp->h_attach;
2258 hp->h_attach = NULL;
2259 if(cnt != 0)
2260 hp->h_attach = n_attachment_append_list(aplist, cp);
2261 else
2262 hp->h_attach = n_attachment_list_edit(aplist,
2263 n_GO_INPUT_CTX_COMPOSE);
2264 n_pstate_err_no = n_ERR_NONE; /* XXX ~@ does NOT handle $!/$?! */
2265 n_pstate_ex_no = 0; /* XXX */
2266 } break;
2267 case 'c':
2268 /* Add to the CC list */
2269 if(cnt == 0)
2270 goto jearg;
2271 else{
2272 struct name *np;
2273 si8_t soe;
2275 soe = 0;
2276 if((np = checkaddrs(lextract(cp, GCC | GFULL), EACM_NORMAL, &soe)
2277 ) != NULL)
2278 hp->h_cc = cat(hp->h_cc, np);
2279 if(soe == 0){
2280 n_pstate_err_no = n_ERR_NONE;
2281 n_pstate_ex_no = 0;
2282 }else{
2283 n_pstate_ex_no = 1;
2284 n_pstate_err_no = (soe < 0) ? n_ERR_PERM : n_ERR_INVAL;
2287 hist &= ~a_HIST_GABBY;
2288 break;
2289 case 'b':
2290 /* Add stuff to blind carbon copies list */
2291 if(cnt == 0)
2292 goto jearg;
2293 else{
2294 struct name *np;
2295 si8_t soe;
2297 soe = 0;
2298 if((np = checkaddrs(lextract(cp, GBCC | GFULL), EACM_NORMAL, &soe)
2299 ) != NULL)
2300 hp->h_bcc = cat(hp->h_bcc, np);
2301 if(soe == 0){
2302 n_pstate_err_no = n_ERR_NONE;
2303 n_pstate_ex_no = 0;
2304 }else{
2305 n_pstate_ex_no = 1;
2306 n_pstate_err_no = (soe < 0) ? n_ERR_PERM : n_ERR_INVAL;
2309 hist &= ~a_HIST_GABBY;
2310 break;
2311 case 'd':
2312 if(cnt != 0)
2313 goto jearg;
2314 cp = n_getdeadletter();
2315 if(0){
2316 /*FALLTHRU*/
2317 case 'R':
2318 case 'r':
2319 case '<':
2320 /* Invoke a file: Search for the file name, then open it and copy
2321 * the contents to _coll_fp */
2322 if(cnt == 0){
2323 n_err(_("Interpolate what file?\n"));
2324 if(a_HARDERR())
2325 goto jerr;
2326 n_pstate_err_no = n_ERR_NOENT;
2327 n_pstate_ex_no = 1;
2328 break;
2330 if(*cp == '!' && c == '<'){
2331 /* TODO hist. normalization */
2332 if((n_pstate_err_no = a_coll_insert_cmd(_coll_fp, ++cp)
2333 ) != n_ERR_NONE){
2334 if(ferror(_coll_fp))
2335 goto jerr;
2336 if(a_HARDERR())
2337 goto jerr;
2338 n_pstate_ex_no = 1;
2339 break;
2341 goto jhistcont;
2343 /* Note this also expands things like
2344 * !:vput vexpr delim random 0
2345 * !< - $delim */
2346 if((cp = fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO | FEXP_NSHELL)
2347 ) == NULL){
2348 if(a_HARDERR())
2349 goto jerr;
2350 n_pstate_err_no = n_ERR_INVAL;
2351 n_pstate_ex_no = 1;
2352 break;
2355 if(n_is_dir(cp, FAL0)){
2356 n_err(_("%s: is a directory\n"), n_shexp_quote_cp(cp, FAL0));
2357 if(a_HARDERR())
2358 goto jerr;
2359 n_pstate_err_no = n_ERR_ISDIR;
2360 n_pstate_ex_no = 1;
2361 break;
2363 if((n_pstate_err_no = a_coll_include_file(cp, (c == 'R'), TRU1)
2364 ) != n_ERR_NONE){
2365 if(ferror(_coll_fp))
2366 goto jerr;
2367 if(a_HARDERR())
2368 goto jerr;
2369 n_pstate_ex_no = 1;
2370 break;
2372 n_pstate_err_no = n_ERR_NONE; /* XXX */
2373 n_pstate_ex_no = 0; /* XXX */
2374 break;
2375 case 'i':
2376 /* Insert a variable into the file */
2377 if(cnt == 0)
2378 goto jearg;
2379 if((cp = n_var_vlook(cp, TRU1)) == NULL || *cp == '\0')
2380 break;
2381 if(putesc(cp, _coll_fp) < 0) /* TODO v15: user resp upon `set' time */
2382 goto jerr;
2383 if((n_psonce & n_PSO_INTERACTIVE) && putesc(cp, n_stdout) < 0)
2384 goto jerr;
2385 n_pstate_err_no = n_ERR_NONE; /* XXX */
2386 n_pstate_ex_no = 0; /* XXX */
2387 break;
2388 case 'a':
2389 case 'A':
2390 /* Insert the contents of a signature variable */
2391 if(cnt != 0)
2392 goto jearg;
2393 cp = (c == 'a') ? ok_vlook(sign) : ok_vlook(Sign);
2394 if(cp != NULL && *cp != '\0'){
2395 if(putesc(cp, _coll_fp) < 0) /* TODO v15: user upon `set' time */
2396 goto jerr;
2397 if((n_psonce & n_PSO_INTERACTIVE) && putesc(cp, n_stdout) < 0)
2398 goto jerr;
2400 n_pstate_err_no = n_ERR_NONE; /* XXX */
2401 n_pstate_ex_no = 0; /* XXX */
2402 break;
2403 case 'w':
2404 /* Write the message on a file */
2405 if(cnt == 0)
2406 goto jearg;
2407 if((cp = fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO)) == NULL){
2408 n_err(_("Write what file!?\n"));
2409 if(a_HARDERR())
2410 goto jerr;
2411 n_pstate_err_no = n_ERR_INVAL;
2412 n_pstate_ex_no = 1;
2413 break;
2415 rewind(_coll_fp);
2416 if((n_pstate_err_no = a_coll_write(cp, _coll_fp, 1)) == n_ERR_NONE)
2417 n_pstate_ex_no = 0;
2418 else if(ferror(_coll_fp))
2419 goto jerr;
2420 else if(a_HARDERR())
2421 goto jerr;
2422 else
2423 n_pstate_ex_no = 1;
2424 break;
2425 case 'm':
2426 case 'M':
2427 case 'f':
2428 case 'F':
2429 case 'u':
2430 case 'U':
2431 /* Interpolate the named messages, if we are in receiving mail mode.
2432 * Does the standard list processing garbage. If ~f is given, we
2433 * don't shift over */
2434 if(cnt == 0)
2435 goto jearg;
2436 if((n_pstate_err_no = a_coll_forward(cp, _coll_fp, c)) == n_ERR_NONE)
2437 n_pstate_ex_no = 0;
2438 else if(ferror(_coll_fp))
2439 goto jerr;
2440 else if(a_HARDERR())
2441 goto jerr;
2442 else
2443 n_pstate_ex_no = 1;
2444 break;
2445 case 'p':
2446 /* Print current state of the message without altering anything */
2447 if(cnt != 0)
2448 goto jearg;
2449 print_collf(_coll_fp, hp); /* XXX pstate_err_no ++ */
2450 if(ferror(_coll_fp))
2451 goto jerr;
2452 n_pstate_err_no = n_ERR_NONE; /* XXX */
2453 n_pstate_ex_no = 0; /* XXX */
2454 break;
2455 case '|':
2456 /* Pipe message through command. Collect output as new message */
2457 if(cnt == 0)
2458 goto jearg;
2459 rewind(_coll_fp);
2460 if((n_pstate_err_no = a_coll_pipe(cp)) == n_ERR_NONE)
2461 n_pstate_ex_no = 0;
2462 else if(a_HARDERR())
2463 goto jerr;
2464 else
2465 n_pstate_ex_no = 1;
2466 hist &= ~a_HIST_GABBY;
2467 goto jhistcont;
2468 case 'v':
2469 case 'e':
2470 /* Edit the current message. 'e' -> use EDITOR, 'v' -> use VISUAL */
2471 if(cnt != 0 || coap != NULL)
2472 goto jearg;
2473 rewind(_coll_fp);
2474 if((n_pstate_err_no = a_coll_edit(c, ok_blook(editheaders) ? hp : NULL)
2475 ) == n_ERR_NONE)
2476 n_pstate_ex_no = 0;
2477 else if(ferror(_coll_fp))
2478 goto jerr;
2479 else if(a_HARDERR())
2480 goto jerr;
2481 else
2482 n_pstate_ex_no = 1;
2483 goto jhistcont;
2484 case '^':
2485 if(!a_collect_plumbing(cp, hp)){
2486 if(ferror(_coll_fp))
2487 goto jerr;
2488 goto jearg;
2490 n_pstate_err_no = n_ERR_NONE; /* XXX */
2491 n_pstate_ex_no = 0; /* XXX */
2492 hist &= ~a_HIST_GABBY;
2493 break;
2494 case '?':
2495 /* Last the lengthy help string. (Very ugly, but take care for
2496 * compiler supported string lengths :() */
2497 fputs(_(
2498 "COMMAND ESCAPES (to be placed after a newline) excerpt:\n"
2499 "~. Commit and send message\n"
2500 "~: <command> Execute an internal command\n"
2501 "~< <file> Insert <file> (\"~<! <command>\" inserts shell command)\n"
2502 "~@ [<files>] Edit[/Add] attachments (file[=input-charset[#output-charset]])\n"
2503 "~c <users> Add users to Cc: list (`~b': to Bcc:)\n"
2504 "~d Read in $DEAD (dead.letter)\n"
2505 "~e Edit message via $EDITOR\n"
2506 "~F <msglist> Read in with headers, do not *indentprefix* lines\n"
2507 ), n_stdout);
2508 fputs(_(
2509 "~f <msglist> Like ~F, but honour `ignore' / `retain' configuration\n"
2510 "~H Edit From:, Reply-To: and Sender:\n"
2511 "~h Prompt for Subject:, To:, Cc: and \"blind\" Bcc:\n"
2512 "~i <variable> Insert a value and a newline\n"
2513 "~M <msglist> Read in with headers, *indentprefix* (`~m': `retain' etc.)\n"
2514 "~p Show current message compose buffer\n"
2515 "~r <file> Insert <file> (`~R': likewise, but *indentprefix* lines)\n"
2516 " <file> may also be <- [HERE-DELIMITER]>\n"
2517 ), n_stdout);
2518 fputs(_(
2519 "~s <subject> Set Subject:\n"
2520 "~t <users> Add users to To: list\n"
2521 "~u <msglist> Read in message(s) without headers (`~U': indent lines)\n"
2522 "~v Edit message via $VISUAL\n"
2523 "~w <file> Write message onto file\n"
2524 "~x Abort composition, discard message (`~q': save in $DEAD)\n"
2525 "~| <command> Pipe message through shell filter\n"
2526 ), n_stdout);
2527 if(cnt != 0)
2528 goto jearg;
2529 n_pstate_err_no = n_ERR_NONE;
2530 n_pstate_ex_no = 0;
2531 break;
2534 /* Finally place an entry in history as applicable */
2535 if(0){
2536 jhistcont:
2537 c = '\1';
2538 }else
2539 c = '\0';
2540 if(hist & a_HIST_ADD)
2541 n_tty_addhist(n_string_cp(sp), ((hist & a_HIST_GABBY) != 0));
2542 if(c != '\0')
2543 goto jcont;
2546 jout:
2547 /* Do we have *on-compose-splice-shell*, or *on-compose-splice*?
2548 * TODO Usual f...ed up state of signals and terminal etc. */
2549 if(coap == NULL && (cp = ok_vlook(on_compose_splice_shell)) != NULL) Jocs:{
2550 union {int (*ptf)(void); char const *sh;} u;
2551 char const *cmd;
2553 /* Reset *escape* and more to their defaults. On change update manual! */
2554 if(ifs_saved == NULL)
2555 ifs_saved = savestr(ok_vlook(ifs));
2556 escape = n_ESCAPE[0];
2557 ok_vclear(ifs);
2559 if(coapm != NULL){
2560 /* XXX Due Popen() fflush(NULL) in PTF mode, ensure nothing to flush */
2561 /*if(!n_real_seek(_coll_fp, 0, SEEK_END))
2562 * goto jerr;*/
2563 u.ptf = &a_coll_ocs__mac;
2564 cmd = (char*)-1;
2565 a_coll_ocs__macname = cp = coapm;
2566 }else{
2567 u.sh = ok_vlook(SHELL);
2568 cmd = cp;
2571 i = strlen(cp) +1;
2572 coap = n_lofi_alloc(n_VSTRUCT_SIZEOF(struct a_coll_ocs_arg, coa_cmd
2573 ) + i);
2574 coap->coa_pipe[0] = coap->coa_pipe[1] = -1;
2575 coap->coa_stdin = coap->coa_stdout = NULL;
2576 coap->coa_senderr = checkaddr_err;
2577 memcpy(coap->coa_cmd, cp, i);
2579 hold_all_sigs();
2580 coap->coa_opipe = safe_signal(SIGPIPE, SIG_IGN);
2581 coap->coa_oint = safe_signal(SIGINT, SIG_IGN);
2582 rele_all_sigs();
2584 if(pipe_cloexec(coap->coa_pipe) != -1 &&
2585 (coap->coa_stdin = Fdopen(coap->coa_pipe[0], "r", FAL0)) != NULL &&
2586 (coap->coa_stdout = Popen(cmd, "W", u.sh, NULL, coap->coa_pipe[1])
2587 ) != NULL){
2588 close(coap->coa_pipe[1]);
2589 coap->coa_pipe[1] = -1;
2591 temporary_compose_mode_hook_call(NULL, NULL, NULL);
2592 n_go_splice_hack(coap->coa_cmd, coap->coa_stdin, coap->coa_stdout,
2593 (n_psonce & ~(n_PSO_INTERACTIVE | n_PSO_TTYIN | n_PSO_TTYOUT)),
2594 &a_coll_ocs__finalize, &coap);
2595 /* Hook version protocol for ~^: update manual upon change! */
2596 fputs(a_COLL_PLUMBING_VERSION "\n", coap->coa_stdout);
2597 #undef a_COLL_PLUMBING_VERSION
2598 goto jcont;
2601 c = n_err_no;
2602 a_coll_ocs__finalize(coap);
2603 n_perr(_("Cannot invoke *on-compose-splice(-shell)?*"), c);
2604 goto jerr;
2606 if(*checkaddr_err != 0){
2607 *checkaddr_err = 0;
2608 goto jerr;
2610 if(coapm == NULL && (coapm = ok_vlook(on_compose_splice)) != NULL)
2611 goto Jocs;
2612 if(coap != NULL){
2613 ok_vset(ifs, ifs_saved);
2614 ifs_saved = NULL;
2617 /* Final chance to edit headers, if not already above */
2618 if (ok_blook(bsdcompat) || ok_blook(askatend)) {
2619 if (hp->h_cc == NULL && ok_blook(askcc))
2620 grab_headers(n_GO_INPUT_CTX_COMPOSE, hp, GCC, 1);
2621 if (hp->h_bcc == NULL && ok_blook(askbcc))
2622 grab_headers(n_GO_INPUT_CTX_COMPOSE, hp, GBCC, 1);
2624 if (hp->h_attach == NULL && ok_blook(askattach))
2625 hp->h_attach = n_attachment_list_edit(hp->h_attach,
2626 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 | GFULL),
2638 EACM_NORMAL, checkaddr_err));
2639 if ((cp = ok_vlook(autobcc)) != NULL && *cp != '\0')
2640 hp->h_bcc = cat(hp->h_bcc, checkaddrs(lextract(cp, GBCC | GFULL),
2641 EACM_NORMAL, checkaddr_err));
2642 if (*checkaddr_err != 0)
2643 goto jerr;
2645 /* TODO Cannot do since it may require turning this into a multipart one */
2646 if(n_poption & n_PO_Mm_FLAG)
2647 goto jskiptails;
2649 /* Place signature? */
2650 if((cp = ok_vlook(signature)) != NULL && *cp != '\0'){
2651 char const *cpq;
2653 if((cpq = fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO)) == NULL){
2654 n_err(_("*signature* expands to invalid file: %s\n"),
2655 n_shexp_quote_cp(cp, FAL0));
2656 goto jerr;
2658 cpq = n_shexp_quote_cp(cp = cpq, FAL0);
2660 if((sigfp = Fopen(cp, "r")) == NULL){
2661 n_err(_("Can't open *signature* %s: %s\n"),
2662 cpq, n_err_to_doc(n_err_no));
2663 goto jerr;
2666 if(linebuf == NULL)
2667 linebuf = smalloc(linesize = LINESIZE);
2668 c = '\0';
2669 while((i = fread(linebuf, sizeof *linebuf, linesize, n_UNVOLATILE(sigfp)))
2670 > 0){
2671 c = linebuf[i - 1];
2672 if(i != fwrite(linebuf, sizeof *linebuf, i, _coll_fp))
2673 goto jerr;
2676 /* C99 */{
2677 FILE *x = n_UNVOLATILE(sigfp);
2678 int e = n_err_no, ise = ferror(x);
2680 sigfp = NULL;
2681 Fclose(x);
2683 if(ise){
2684 n_err(_("Errors while reading *signature* %s: %s\n"),
2685 cpq, n_err_to_doc(e));
2686 goto jerr;
2690 if(c != '\0' && c != '\n')
2691 putc('\n', _coll_fp);
2694 { char const *cp_obsolete = ok_vlook(NAIL_TAIL);
2696 if(cp_obsolete != NULL)
2697 n_OBSOLETE(_("please use *message-inject-tail*, not *NAIL_TAIL*"));
2699 if((cp = ok_vlook(message_inject_tail)) != NULL ||
2700 (cp = cp_obsolete) != NULL){
2701 if(putesc(cp, _coll_fp) < 0)
2702 goto jerr;
2703 if((n_psonce & n_PSO_INTERACTIVE) && putesc(cp, n_stdout) < 0)
2704 goto jerr;
2708 jskiptails:
2709 if(fflush(_coll_fp))
2710 goto jerr;
2711 rewind(_coll_fp);
2713 jleave:
2714 if (linebuf != NULL)
2715 free(linebuf);
2716 sigprocmask(SIG_BLOCK, &nset, NULL);
2717 n_pstate &= ~n_PS_COMPOSE_MODE;
2718 safe_signal(SIGINT, _coll_saveint);
2719 safe_signal(SIGHUP, _coll_savehup);
2720 sigprocmask(SIG_SETMASK, &oset, NULL);
2721 NYD_LEAVE;
2722 return _coll_fp;
2724 jerr:
2725 hold_all_sigs();
2727 if(coap != NULL && coap != (struct a_coll_ocs_arg*)-1){
2728 if(!(flags & a_COAP_NOSIGTERM))
2729 n_psignal(coap->coa_stdout, SIGTERM);
2730 n_go_splice_hack_remove_after_jump();
2731 coap = NULL;
2733 if(ifs_saved != NULL){
2734 ok_vset(ifs, ifs_saved);
2735 ifs_saved = NULL;
2737 if(sigfp != NULL){
2738 Fclose(n_UNVOLATILE(sigfp));
2739 sigfp = NULL;
2741 if(_coll_fp != NULL){
2742 Fclose(_coll_fp);
2743 _coll_fp = NULL;
2746 rele_all_sigs();
2748 assert(checkaddr_err != NULL);
2749 /* TODO We don't save in $DEAD upon error because msg not readily composed?
2750 * TODO But this no good, it should go ZOMBIE / DRAFT / POSTPONED or what! */
2751 if(*checkaddr_err != 0){
2752 if(*checkaddr_err == 111)
2753 n_err(_("Compose mode splice hook failure\n"));
2754 else
2755 n_err(_("Some addressees were classified as \"hard error\"\n"));
2756 }else if(_coll_hadintr == 0){
2757 *checkaddr_err = TRU1; /* TODO ugly: "sendout_error" now.. */
2758 n_err(_("Failed to prepare composed message (I/O error, disk full?)\n"));
2760 goto jleave;
2762 #undef a_HARDERR
2765 /* s-it-mode */