mime-types.c: (just) (satisfy) scan-build
[s-mailx.git] / collect.c
blob401b12158f21a0a084b52e65387ab999bf9702e5
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);
372 jleave:
373 Fclose(obuf);
374 NYD_LEAVE;
377 static si32_t
378 a_coll_write(char const *name, FILE *fp, int f)
380 FILE *of;
381 int c;
382 si64_t lc, cc;
383 si32_t rv;
384 NYD_ENTER;
386 rv = n_ERR_NONE;
388 if(f) {
389 fprintf(n_stdout, "%s ", n_shexp_quote_cp(name, FAL0));
390 fflush(n_stdout);
393 if ((of = Fopen(name, "a")) == NULL) {
394 n_perr(name, rv = n_err_no);
395 goto jerr;
398 lc = cc = 0;
399 while ((c = getc(fp)) != EOF) {
400 ++cc;
401 if (c == '\n')
402 ++lc;
403 if (putc(c, of) == EOF) {
404 n_perr(name, rv = n_err_no);
405 goto jerr;
408 fprintf(n_stdout, _("%" PRId64 "/%" PRId64 "\n"), lc, cc);
410 jleave:
411 if(of != NULL)
412 Fclose(of);
413 fflush(n_stdout);
414 NYD_LEAVE;
415 return rv;
416 jerr:
417 putc('-', n_stdout);
418 putc('\n', n_stdout);
419 goto jleave;
422 static bool_t
423 a_coll_message_inject_head(FILE *fp){
424 bool_t rv;
425 char const *cp, *cp_obsolete;
426 NYD2_ENTER;
428 cp_obsolete = ok_vlook(NAIL_HEAD);
429 if(cp_obsolete != NULL)
430 n_OBSOLETE(_("please use *message-inject-head*, not *NAIL_HEAD*"));
432 if(((cp = ok_vlook(message_inject_head)) != NULL ||
433 (cp = cp_obsolete) != NULL) && putesc(cp, fp) < 0)
434 rv = FAL0;
435 else
436 rv = TRU1;
437 NYD2_LEAVE;
438 return rv;
441 static bool_t
442 a_coll_makeheader(FILE *fp, struct header *hp, si8_t *checkaddr_err,
443 bool_t do_delayed_due_t)
445 FILE *nf;
446 int c;
447 bool_t rv;
448 NYD_ENTER;
450 rv = FAL0;
452 if ((nf = Ftmp(NULL, "colhead", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==NULL) {
453 n_perr(_("temporary mail edit file"), 0);
454 goto jleave;
457 extract_header(fp, hp, checkaddr_err);
458 if (checkaddr_err != NULL && *checkaddr_err != 0)
459 goto jleave;
461 /* In template mode some things have been delayed until the template has
462 * been read */
463 if(do_delayed_due_t){
464 char const *cp;
466 if((cp = ok_vlook(on_compose_enter)) != NULL){
467 setup_from_and_sender(hp);
468 temporary_compose_mode_hook_call(cp, &n_temporary_compose_hook_varset,
469 hp);
472 if(!a_coll_message_inject_head(nf))
473 goto jleave;
476 while ((c = getc(fp)) != EOF) /* XXX bytewise, yuck! */
477 putc(c, nf);
479 if (fp != _coll_fp)
480 Fclose(_coll_fp);
481 Fclose(fp);
482 _coll_fp = nf;
484 if (check_from_and_sender(hp->h_from, hp->h_sender) == NULL)
485 goto jleave;
486 rv = TRU1;
487 jleave:
488 NYD_LEAVE;
489 return rv;
492 static si32_t
493 a_coll_edit(int c, struct header *hp) /* TODO error(return) weird */
495 struct n_sigman sm;
496 FILE *nf;
497 sighandler_type volatile sigint;
498 bool_t saved;
499 si32_t volatile rv;
500 NYD_ENTER;
502 n_UNINIT(sigint, SIG_ERR);
503 rv = n_ERR_NONE;
505 if(!(saved = ok_blook(add_file_recipients)))
506 ok_bset(add_file_recipients);
508 n_SIGMAN_ENTER_SWITCH(&sm, n_SIGMAN_ALL){
509 case 0:
510 sigint = safe_signal(SIGINT, SIG_IGN);
511 break;
512 default:
513 rv = n_ERR_INTR;
514 goto jleave;
517 nf = run_editor(_coll_fp, (off_t)-1, c, FAL0, hp, NULL, SEND_MBOX, sigint);
518 if (nf != NULL) {
519 if (hp) {
520 if(!a_coll_makeheader(nf, hp, NULL, FAL0))
521 rv = n_ERR_INVAL;
522 } else {
523 fseek(nf, 0L, SEEK_END);
524 Fclose(_coll_fp);
525 _coll_fp = nf;
527 } else
528 rv = n_ERR_CHILD;
530 n_sigman_cleanup_ping(&sm);
531 jleave:
532 if(!saved)
533 ok_bclear(add_file_recipients);
534 safe_signal(SIGINT, sigint);
535 NYD_LEAVE;
536 n_sigman_leave(&sm, n_SIGMAN_VIPSIGS_NTTYOUT);
537 return rv;
540 static si32_t
541 a_coll_pipe(char const *cmd)
543 int ws;
544 FILE *nf;
545 sighandler_type sigint;
546 si32_t rv;
547 NYD_ENTER;
549 rv = n_ERR_NONE;
550 sigint = safe_signal(SIGINT, SIG_IGN);
552 if ((nf = Ftmp(NULL, "colpipe", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==NULL) {
553 n_perr(_("temporary mail edit file"), rv = n_err_no);
554 goto jout;
557 /* stdin = current message. stdout = new message */
558 fflush(_coll_fp);
559 if (n_child_run(ok_vlook(SHELL), 0, fileno(_coll_fp), fileno(nf), "-c",
560 cmd, NULL, NULL, &ws) < 0 || WEXITSTATUS(ws) != 0) {
561 Fclose(nf);
562 rv = n_ERR_CHILD;
563 goto jout;
566 if (fsize(nf) == 0) {
567 n_err(_("No bytes from %s !?\n"), n_shexp_quote_cp(cmd, FAL0));
568 Fclose(nf);
569 rv = n_ERR_NODATA;
570 goto jout;
573 /* Take new files */
574 fseek(nf, 0L, SEEK_END);
575 Fclose(_coll_fp);
576 _coll_fp = nf;
577 jout:
578 safe_signal(SIGINT, sigint);
579 NYD_LEAVE;
580 return rv;
583 static si32_t
584 a_coll_forward(char const *ms, FILE *fp, int f)
586 int *msgvec, rv = 0;
587 struct n_ignore const *itp;
588 char const *tabst;
589 enum sendaction action;
590 NYD_ENTER;
592 msgvec = salloc((size_t)(msgCount + 1) * sizeof *msgvec);
593 if (getmsglist(ms, msgvec, 0) < 0) {
594 rv = n_ERR_NOENT; /* XXX not really, should be handled there! */
595 goto jleave;
597 if (*msgvec == 0) {
598 *msgvec = first(0, MMNORM);
599 if (*msgvec == 0) {
600 n_err(_("No appropriate messages\n"));
601 rv = n_ERR_NOENT;
602 goto jleave;
604 msgvec[1] = 0;
607 if (f == 'f' || f == 'F' || f == 'u')
608 tabst = NULL;
609 else if ((tabst = ok_vlook(indentprefix)) == NULL)
610 tabst = INDENT_DEFAULT;
611 if (f == 'u' || f == 'U')
612 itp = n_IGNORE_ALL;
613 else
614 itp = upperchar(f) ? NULL : n_IGNORE_TYPE;
615 action = (upperchar(f) && f != 'U') ? SEND_QUOTE_ALL : SEND_QUOTE;
617 fprintf(n_stdout, _("Interpolating:"));
618 srelax_hold();
619 for (; *msgvec != 0; ++msgvec) {
620 struct message *mp = message + *msgvec - 1;
622 touch(mp);
623 fprintf(n_stdout, " %d", *msgvec);
624 fflush(n_stdout);
625 if (sendmp(mp, fp, itp, tabst, action, NULL) < 0) {
626 n_perr(_("temporary mail file"), 0);
627 rv = n_ERR_IO;
628 break;
630 srelax();
632 srelax_rele();
633 fprintf(n_stdout, "\n");
634 jleave:
635 NYD_LEAVE;
636 return rv;
639 static bool_t
640 a_collect_plumbing(char const *ms, struct header *hp){
641 /* TODO _collect_plumbing: instead of fields the basic headers should
642 * TODO be in an array and have IDs, like in termcap etc., so then this
643 * TODO could be simplified as table-walks. Also true for arg-checks! */
644 bool_t rv;
645 char const *cp, *cmd[4];
646 NYD2_ENTER;
648 /* Protcol version for *on-compose-splice** -- update manual on change! */
649 #define a_COLL_PLUMBING_VERSION "0 0 1"
650 cp = ms;
652 /* C99 */{
653 size_t i;
655 for(i = 0; i < n_NELEM(cmd); ++i){ /* TODO trim+strlist_split(_ifs?)() */
656 while(blankchar(*cp))
657 ++cp;
658 if(*cp == '\0')
659 cmd[i] = NULL;
660 else{
661 if(i < n_NELEM(cmd) - 1)
662 for(cmd[i] = cp++; *cp != '\0' && !blankchar(*cp); ++cp)
664 else{
665 /* Last slot takes all the rest of the line, less trailing WS */
666 for(cmd[i] = cp++; *cp != '\0'; ++cp)
668 while(blankchar(cp[-1]))
669 --cp;
671 cmd[i] = savestrbuf(cmd[i], PTR2SIZE(cp - cmd[i]));
676 if(n_UNLIKELY(cmd[0] == NULL))
677 goto jecmd;
678 if(is_asccaseprefix(cmd[0], "header"))
679 rv = a_collect__plumb_header(cp, hp, cmd);
680 else if(is_asccaseprefix(cmd[0], "attachment"))
681 rv = a_collect__plumb_attach(cp, hp, cmd);
682 else{
683 jecmd:
684 fputs("500\n", n_stdout);
685 rv = FAL0;
687 fflush(n_stdout);
689 NYD2_LEAVE;
690 return rv;
693 static bool_t
694 a_collect__plumb_header(char const *cp, struct header *hp,
695 char const *cmd[4]){
696 uiz_t i;
697 struct n_header_field *hfp;
698 struct name *np, **npp;
699 NYD2_ENTER;
701 if(cmd[1] == NULL)
702 goto jdefault;
704 if(is_asccaseprefix(cmd[1], "insert")){ /* TODO LOGIC BELONGS head.c
705 * TODO That is: Header::factory(string) -> object (blahblah).
706 * TODO I.e., as long as we don't have regular RFC compliant parsers
707 * TODO which differentiate in between structured and unstructured
708 * TODO header fields etc., a little workaround */
709 struct name *xnp;
710 si8_t aerr;
711 enum expand_addr_check_mode eacm;
712 enum gfield ntype;
713 bool_t mult_ok;
715 if(cmd[2] == NULL || cmd[3] == NULL)
716 goto jecmd;
718 /* Strip [\r\n] which would render a body invalid XXX all controls? */
719 /* C99 */{
720 char *xp, c;
722 cmd[3] = xp = savestr(cmd[3]);
723 for(; (c = *xp) != '\0'; ++xp)
724 if(c == '\n' || c == '\r')
725 *xp = ' ';
728 if(!asccasecmp(cmd[2], cp = "Subject")){
729 if(cmd[3][0] != '\0'){
730 if(hp->h_subject != NULL)
731 hp->h_subject = savecatsep(hp->h_subject, ' ', cmd[3]);
732 else
733 hp->h_subject = n_UNCONST(cmd[3]);
734 fprintf(n_stdout, "210 %s 1\n", cp);
735 goto jleave;
736 }else
737 goto j501cp;
740 mult_ok = TRU1;
741 ntype = GEXTRA | GFULL | GFULLEXTRA;
742 eacm = EACM_STRICT;
744 if(!asccasecmp(cmd[2], cp = "From")){
745 npp = &hp->h_from;
746 jins:
747 aerr = 0;
748 if((np = lextract(cmd[3], ntype)) == NULL)
749 goto j501cp;
751 if((np = checkaddrs(np, eacm, &aerr), aerr != 0)){
752 fprintf(n_stdout, "505 %s\n", cp);
753 goto jleave;
756 /* Go to the end of the list, track whether it contains any
757 * non-deleted entries */
758 i = 0;
759 if((xnp = *npp) != NULL)
760 for(;; xnp = xnp->n_flink){
761 if(!(xnp->n_type & GDEL))
762 ++i;
763 if(xnp->n_flink == NULL)
764 break;
767 if(!mult_ok && (i != 0 || np->n_flink != NULL))
768 fprintf(n_stdout, "506 %s\n", cp);
769 else{
770 if(xnp == NULL)
771 *npp = np;
772 else
773 xnp->n_flink = np;
774 np->n_blink = xnp;
775 fprintf(n_stdout, "210 %s %" PRIuZ "\n", cp, ++i);
777 goto jleave;
779 if(!asccasecmp(cmd[2], cp = "Sender")){
780 mult_ok = FAL0;
781 npp = &hp->h_sender;
782 goto jins;
784 if(!asccasecmp(cmd[2], cp = "To")){
785 npp = &hp->h_to;
786 ntype = GTO | GFULL;
787 eacm = EACM_NORMAL | EAF_NAME;
788 goto jins;
790 if(!asccasecmp(cmd[2], cp = "Cc")){
791 npp = &hp->h_cc;
792 ntype = GCC | GFULL;
793 eacm = EACM_NORMAL | EAF_NAME;
794 goto jins;
796 if(!asccasecmp(cmd[2], cp = "Bcc")){
797 npp = &hp->h_bcc;
798 ntype = GBCC | GFULL;
799 eacm = EACM_NORMAL | EAF_NAME;
800 goto jins;
802 if(!asccasecmp(cmd[2], cp = "Reply-To")){
803 npp = &hp->h_replyto;
804 eacm = EACM_NONAME;
805 goto jins;
807 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
808 npp = &hp->h_mft;
809 eacm = EACM_NONAME;
810 goto jins;
812 if(!asccasecmp(cmd[2], cp = "Message-ID")){
813 mult_ok = FAL0;
814 npp = &hp->h_message_id;
815 ntype = GREF;
816 eacm = EACM_NONAME;
817 goto jins;
819 if(!asccasecmp(cmd[2], cp = "References")){
820 npp = &hp->h_ref;
821 ntype = GREF;
822 eacm = EACM_NONAME;
823 goto jins;
825 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
826 npp = &hp->h_in_reply_to;
827 ntype = GREF;
828 eacm = EACM_NONAME;
829 goto jins;
832 if(!asccasecmp(cmd[2], cp = "Mailx-Command") ||
833 !asccasecmp(cmd[2], cp = "Mailx-Raw-To") ||
834 !asccasecmp(cmd[2], cp = "Mailx-Raw-Cc") ||
835 !asccasecmp(cmd[2], cp = "Mailx-Raw-Bcc") ||
836 !asccasecmp(cmd[2], cp = "Mailx-Orig-From") ||
837 !asccasecmp(cmd[2], cp = "Mailx-Orig-To") ||
838 !asccasecmp(cmd[2], cp = "Mailx-Orig-Cc") ||
839 !asccasecmp(cmd[2], cp = "Mailx-Orig-Bcc")){
840 fprintf(n_stdout, "505 %s\n", cp);
841 goto jleave;
844 /* Free-form header fields (note j501cp may print non-normalized name) */
845 /* C99 */{
846 size_t nl, bl;
847 struct n_header_field **hfpp;
849 for(cp = cmd[2]; *cp != '\0'; ++cp)
850 if(!fieldnamechar(*cp)){
851 cp = cmd[2];
852 goto j501cp;
855 for(i = 0, hfpp = &hp->h_user_headers; *hfpp != NULL; ++i)
856 hfpp = &(*hfpp)->hf_next;
858 nl = strlen(cp = cmd[2]) +1;
859 bl = strlen(cmd[3]) +1;
860 *hfpp = hfp = salloc(n_VSTRUCT_SIZEOF(struct n_header_field, hf_dat
861 ) + nl + bl);
862 hfp->hf_next = NULL;
863 hfp->hf_nl = --nl;
864 hfp->hf_bl = bl - 1;
865 hfp->hf_dat[nl = 0] = upperconv(*cp);
866 while(*cp++ != '\0')
867 hfp->hf_dat[++nl] = lowerconv(*cp);
868 memcpy(&hfp->hf_dat[++nl], cmd[3], bl);
869 fprintf(n_stdout, "210 %s %" PRIuZ "\n", &hfp->hf_dat[0], ++i);
871 goto jleave;
874 if(is_asccaseprefix(cmd[1], "list")){
875 jdefault:
876 if(cmd[2] == NULL){
877 fputs("210", n_stdout);
878 if(hp->h_subject != NULL) fputs(" Subject", n_stdout);
879 if(hp->h_from != NULL) fputs(" From", n_stdout);
880 if(hp->h_sender != NULL) fputs(" Sender", n_stdout);
881 if(hp->h_to != NULL) fputs(" To", n_stdout);
882 if(hp->h_cc != NULL) fputs(" Cc", n_stdout);
883 if(hp->h_bcc != NULL) fputs(" Bcc", n_stdout);
884 if(hp->h_replyto != NULL) fputs(" Reply-To", n_stdout);
885 if(hp->h_mft != NULL) fputs(" Mail-Followup-To", n_stdout);
886 if(hp->h_message_id != NULL) fputs(" Message-ID", n_stdout);
887 if(hp->h_ref != NULL) fputs(" References", n_stdout);
888 if(hp->h_in_reply_to != NULL) fputs(" In-Reply-To", n_stdout);
889 if(hp->h_mailx_command != NULL) fputs(" Mailx-Command", n_stdout);
890 if(hp->h_mailx_raw_to != NULL) fputs(" Mailx-Raw-To", n_stdout);
891 if(hp->h_mailx_raw_cc != NULL) fputs(" Mailx-Raw-Cc", n_stdout);
892 if(hp->h_mailx_raw_bcc != NULL) fputs(" Mailx-Raw-Bcc", n_stdout);
893 if(hp->h_mailx_orig_from != NULL) fputs(" Mailx-Orig-From", n_stdout);
894 if(hp->h_mailx_orig_to != NULL) fputs(" Mailx-Orig-To", n_stdout);
895 if(hp->h_mailx_orig_cc != NULL) fputs(" Mailx-Orig-Cc", n_stdout);
896 if(hp->h_mailx_orig_bcc != NULL) fputs(" Mailx-Orig-Bcc", n_stdout);
898 /* Print only one instance of each free-form header */
899 for(hfp = hp->h_user_headers; hfp != NULL; hfp = hfp->hf_next){
900 struct n_header_field *hfpx;
902 for(hfpx = hp->h_user_headers;; hfpx = hfpx->hf_next)
903 if(hfpx == hfp){
904 putc(' ', n_stdout);
905 fputs(&hfp->hf_dat[0], n_stdout);
906 break;
907 }else if(!strcmp(&hfpx->hf_dat[0], &hfp->hf_dat[0]))
908 break;
910 putc('\n', n_stdout);
911 goto jleave;
914 if(cmd[3] != NULL)
915 goto jecmd;
917 if(!asccasecmp(cmd[2], cp = "Subject")){
918 np = (hp->h_subject != NULL) ? (struct name*)-1 : NULL;
919 goto jlist;
921 if(!asccasecmp(cmd[2], cp = "From")){
922 np = hp->h_from;
923 jlist:
924 fprintf(n_stdout, "%s %s\n", (np == NULL ? "501" : "210"), cp);
925 goto jleave;
927 if(!asccasecmp(cmd[2], cp = "Sender")){
928 np = hp->h_sender;
929 goto jlist;
931 if(!asccasecmp(cmd[2], cp = "To")){
932 np = hp->h_to;
933 goto jlist;
935 if(!asccasecmp(cmd[2], cp = "Cc")){
936 np = hp->h_cc;
937 goto jlist;
939 if(!asccasecmp(cmd[2], cp = "Bcc")){
940 np = hp->h_bcc;
941 goto jlist;
943 if(!asccasecmp(cmd[2], cp = "Reply-To")){
944 np = hp->h_replyto;
945 goto jlist;
947 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
948 np = hp->h_mft;
949 goto jlist;
951 if(!asccasecmp(cmd[2], cp = "Message-ID")){
952 np = hp->h_message_id;
953 goto jlist;
955 if(!asccasecmp(cmd[2], cp = "References")){
956 np = hp->h_ref;
957 goto jlist;
959 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
960 np = hp->h_in_reply_to;
961 goto jlist;
964 if(!asccasecmp(cmd[2], cp = "Mailx-Command")){
965 np = (hp->h_mailx_command != NULL) ? (struct name*)-1 : NULL;
966 goto jlist;
968 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-To")){
969 np = hp->h_mailx_raw_to;
970 goto jlist;
972 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-Cc")){
973 np = hp->h_mailx_raw_cc;
974 goto jlist;
976 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-Bcc")){
977 np = hp->h_mailx_raw_bcc;
978 goto jlist;
980 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-From")){
981 np = hp->h_mailx_orig_from;
982 goto jlist;
984 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-To")){
985 np = hp->h_mailx_orig_to;
986 goto jlist;
988 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-Cc")){
989 np = hp->h_mailx_orig_cc;
990 goto jlist;
992 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-Bcc")){
993 np = hp->h_mailx_orig_bcc;
994 goto jlist;
997 /* Free-form header fields (note j501cp may print non-normalized name) */
998 for(cp = cmd[2]; *cp != '\0'; ++cp)
999 if(!fieldnamechar(*cp)){
1000 cp = cmd[2];
1001 goto j501cp;
1003 cp = cmd[2];
1004 for(hfp = hp->h_user_headers;; hfp = hfp->hf_next){
1005 if(hfp == NULL)
1006 goto j501cp;
1007 else if(!asccasecmp(cp, &hfp->hf_dat[0])){
1008 fprintf(n_stdout, "210 %s\n", &hfp->hf_dat[0]);
1009 break;
1012 goto jleave;
1015 if(is_asccaseprefix(cmd[1], "remove")){
1016 if(cmd[2] == NULL || cmd[3] != NULL)
1017 goto jecmd;
1019 if(!asccasecmp(cmd[2], cp = "Subject")){
1020 if(hp->h_subject != NULL){
1021 hp->h_subject = NULL;
1022 fprintf(n_stdout, "210 %s\n", cp);
1023 goto jleave;
1024 }else
1025 goto j501cp;
1028 if(!asccasecmp(cmd[2], cp = "From")){
1029 npp = &hp->h_from;
1030 jrem:
1031 if(*npp != NULL){
1032 *npp = NULL;
1033 fprintf(n_stdout, "210 %s\n", cp);
1034 goto jleave;
1035 }else
1036 goto j501cp;
1038 if(!asccasecmp(cmd[2], cp = "Sender")){
1039 npp = &hp->h_sender;
1040 goto jrem;
1042 if(!asccasecmp(cmd[2], cp = "To")){
1043 npp = &hp->h_to;
1044 goto jrem;
1046 if(!asccasecmp(cmd[2], cp = "Cc")){
1047 npp = &hp->h_cc;
1048 goto jrem;
1050 if(!asccasecmp(cmd[2], cp = "Bcc")){
1051 npp = &hp->h_bcc;
1052 goto jrem;
1054 if(!asccasecmp(cmd[2], cp = "Reply-To")){
1055 npp = &hp->h_replyto;
1056 goto jrem;
1058 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
1059 npp = &hp->h_mft;
1060 goto jrem;
1062 if(!asccasecmp(cmd[2], cp = "Message-ID")){
1063 npp = &hp->h_message_id;
1064 goto jrem;
1066 if(!asccasecmp(cmd[2], cp = "References")){
1067 npp = &hp->h_ref;
1068 goto jrem;
1070 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
1071 npp = &hp->h_in_reply_to;
1072 goto jrem;
1075 if(!asccasecmp(cmd[2], cp = "Mailx-Command") ||
1076 !asccasecmp(cmd[2], cp = "Mailx-Raw-To") ||
1077 !asccasecmp(cmd[2], cp = "Mailx-Raw-Cc") ||
1078 !asccasecmp(cmd[2], cp = "Mailx-Raw-Bcc") ||
1079 !asccasecmp(cmd[2], cp = "Mailx-Orig-From") ||
1080 !asccasecmp(cmd[2], cp = "Mailx-Orig-To") ||
1081 !asccasecmp(cmd[2], cp = "Mailx-Orig-Cc") ||
1082 !asccasecmp(cmd[2], cp = "Mailx-Orig-Bcc")){
1083 fprintf(n_stdout, "505 %s\n", cp);
1084 goto jleave;
1087 /* Free-form header fields (note j501cp may print non-normalized name) */
1088 /* C99 */{
1089 struct n_header_field **hfpp;
1090 bool_t any;
1092 for(cp = cmd[2]; *cp != '\0'; ++cp)
1093 if(!fieldnamechar(*cp)){
1094 cp = cmd[2];
1095 goto j501cp;
1097 cp = cmd[2];
1099 for(any = FAL0, hfpp = &hp->h_user_headers; (hfp = *hfpp) != NULL;){
1100 if(!asccasecmp(cp, &hfp->hf_dat[0])){
1101 *hfpp = hfp->hf_next;
1102 if(!any)
1103 fprintf(n_stdout, "210 %s\n", &hfp->hf_dat[0]);
1104 any = TRU1;
1105 }else
1106 hfpp = &hfp->hf_next;
1108 if(!any)
1109 goto j501cp;
1111 goto jleave;
1114 if(is_asccaseprefix(cmd[1], "remove-at")){
1115 if(cmd[2] == NULL || cmd[3] == NULL)
1116 goto jecmd;
1118 if((n_idec_uiz_cp(&i, cmd[3], 0, NULL
1119 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
1120 ) != n_IDEC_STATE_CONSUMED || i == 0){
1121 fputs("505\n", n_stdout);
1122 goto jleave;
1125 if(!asccasecmp(cmd[2], cp = "Subject")){
1126 if(hp->h_subject != NULL && i == 1){
1127 hp->h_subject = NULL;
1128 fprintf(n_stdout, "210 %s 1\n", cp);
1129 goto jleave;
1130 }else
1131 goto j501cp;
1134 if(!asccasecmp(cmd[2], cp = "From")){
1135 npp = &hp->h_from;
1136 jremat:
1137 if((np = *npp) == NULL)
1138 goto j501cp;
1139 while(--i != 0 && np != NULL)
1140 np = np->n_flink;
1141 if(np == NULL)
1142 goto j501cp;
1144 if(np->n_blink != NULL)
1145 np->n_blink->n_flink = np->n_flink;
1146 else
1147 *npp = np->n_flink;
1148 if(np->n_flink != NULL)
1149 np->n_flink->n_blink = np->n_blink;
1151 fprintf(n_stdout, "210 %s\n", cp);
1152 goto jleave;
1154 if(!asccasecmp(cmd[2], cp = "Sender")){
1155 npp = &hp->h_sender;
1156 goto jremat;
1158 if(!asccasecmp(cmd[2], cp = "To")){
1159 npp = &hp->h_to;
1160 goto jremat;
1162 if(!asccasecmp(cmd[2], cp = "Cc")){
1163 npp = &hp->h_cc;
1164 goto jremat;
1166 if(!asccasecmp(cmd[2], cp = "Bcc")){
1167 npp = &hp->h_bcc;
1168 goto jremat;
1170 if(!asccasecmp(cmd[2], cp = "Reply-To")){
1171 npp = &hp->h_replyto;
1172 goto jremat;
1174 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
1175 npp = &hp->h_mft;
1176 goto jremat;
1178 if(!asccasecmp(cmd[2], cp = "Message-ID")){
1179 npp = &hp->h_message_id;
1180 goto jremat;
1182 if(!asccasecmp(cmd[2], cp = "References")){
1183 npp = &hp->h_ref;
1184 goto jremat;
1186 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
1187 npp = &hp->h_in_reply_to;
1188 goto jremat;
1191 if(!asccasecmp(cmd[2], cp = "Mailx-Command") ||
1192 !asccasecmp(cmd[2], cp = "Mailx-Raw-To") ||
1193 !asccasecmp(cmd[2], cp = "Mailx-Raw-Cc") ||
1194 !asccasecmp(cmd[2], cp = "Mailx-Raw-Bcc") ||
1195 !asccasecmp(cmd[2], cp = "Mailx-Orig-From") ||
1196 !asccasecmp(cmd[2], cp = "Mailx-Orig-To") ||
1197 !asccasecmp(cmd[2], cp = "Mailx-Orig-Cc") ||
1198 !asccasecmp(cmd[2], cp = "Mailx-Orig-Bcc")){
1199 fprintf(n_stdout, "505 %s\n", cp);
1200 goto jleave;
1203 /* Free-form header fields (note j501cp may print non-normalized name) */
1204 /* C99 */{
1205 struct n_header_field **hfpp;
1207 for(cp = cmd[2]; *cp != '\0'; ++cp)
1208 if(!fieldnamechar(*cp)){
1209 cp = cmd[2];
1210 goto j501cp;
1212 cp = cmd[2];
1214 for(hfpp = &hp->h_user_headers; (hfp = *hfpp) != NULL;){
1215 if(--i == 0){
1216 *hfpp = hfp->hf_next;
1217 fprintf(n_stdout, "210 %s %" PRIuZ "\n", &hfp->hf_dat[0], i);
1218 break;
1219 }else
1220 hfpp = &hfp->hf_next;
1222 if(hfp == NULL)
1223 goto j501cp;
1225 goto jleave;
1228 if(is_asccaseprefix(cmd[1], "show")){
1229 if(cmd[2] == NULL || cmd[3] != NULL)
1230 goto jecmd;
1232 if(!asccasecmp(cmd[2], cp = "Subject")){
1233 if(hp->h_subject == NULL)
1234 goto j501cp;
1235 fprintf(n_stdout, "212 %s\n%s\n\n", cp, hp->h_subject);
1236 goto jleave;
1239 if(!asccasecmp(cmd[2], cp = "From")){
1240 np = hp->h_from;
1241 jshow:
1242 if(np != NULL){
1243 fprintf(n_stdout, "211 %s\n", cp);
1244 do if(!(np->n_type & GDEL))
1245 fprintf(n_stdout, "%s %s\n", np->n_name, np->n_fullname);
1246 while((np = np->n_flink) != NULL);
1247 putc('\n', n_stdout);
1248 goto jleave;
1249 }else
1250 goto j501cp;
1252 if(!asccasecmp(cmd[2], cp = "Sender")){
1253 np = hp->h_sender;
1254 goto jshow;
1256 if(!asccasecmp(cmd[2], cp = "To")){
1257 np = hp->h_to;
1258 goto jshow;
1260 if(!asccasecmp(cmd[2], cp = "Cc")){
1261 np = hp->h_cc;
1262 goto jshow;
1264 if(!asccasecmp(cmd[2], cp = "Bcc")){
1265 np = hp->h_bcc;
1266 goto jshow;
1268 if(!asccasecmp(cmd[2], cp = "Reply-To")){
1269 np = hp->h_replyto;
1270 goto jshow;
1272 if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
1273 np = hp->h_mft;
1274 goto jshow;
1276 if(!asccasecmp(cmd[2], cp = "Message-ID")){
1277 np = hp->h_message_id;
1278 goto jshow;
1280 if(!asccasecmp(cmd[2], cp = "References")){
1281 np = hp->h_ref;
1282 goto jshow;
1284 if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
1285 np = hp->h_in_reply_to;
1286 goto jshow;
1289 if(!asccasecmp(cmd[2], cp = "Mailx-Command")){
1290 if(hp->h_mailx_command == NULL)
1291 goto j501cp;
1292 fprintf(n_stdout, "212 %s\n%s\n\n", cp, hp->h_mailx_command);
1293 goto jleave;
1295 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-To")){
1296 np = hp->h_mailx_raw_to;
1297 goto jshow;
1299 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-Cc")){
1300 np = hp->h_mailx_raw_cc;
1301 goto jshow;
1303 if(!asccasecmp(cmd[2], cp = "Mailx-Raw-Bcc")){
1304 np = hp->h_mailx_raw_bcc;
1305 goto jshow;
1307 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-From")){
1308 np = hp->h_mailx_orig_from;
1309 goto jshow;
1311 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-To")){
1312 np = hp->h_mailx_orig_to;
1313 goto jshow;
1315 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-Cc")){
1316 np = hp->h_mailx_orig_cc;
1317 goto jshow;
1319 if(!asccasecmp(cmd[2], cp = "Mailx-Orig-Bcc")){
1320 np = hp->h_mailx_orig_bcc;
1321 goto jshow;
1324 /* Free-form header fields (note j501cp may print non-normalized name) */
1325 /* C99 */{
1326 bool_t any;
1328 for(cp = cmd[2]; *cp != '\0'; ++cp)
1329 if(!fieldnamechar(*cp)){
1330 cp = cmd[2];
1331 goto j501cp;
1333 cp = cmd[2];
1335 for(any = FAL0, hfp = hp->h_user_headers; hfp != NULL;
1336 hfp = hfp->hf_next){
1337 if(!asccasecmp(cp, &hfp->hf_dat[0])){
1338 if(!any)
1339 fprintf(n_stdout, "212 %s\n", &hfp->hf_dat[0]);
1340 any = TRU1;
1341 fprintf(n_stdout, "%s\n", &hfp->hf_dat[hfp->hf_nl +1]);
1344 if(any)
1345 putc('\n', n_stdout);
1346 else
1347 goto j501cp;
1349 goto jleave;
1352 jecmd:
1353 fputs("500\n", n_stdout);
1354 cp = NULL;
1355 jleave:
1356 NYD2_LEAVE;
1357 return (cp != NULL);
1359 j501cp:
1360 fputs("501 ", n_stdout);
1361 fputs(cp, n_stdout);
1362 putc('\n', n_stdout);
1363 goto jleave;
1366 static bool_t
1367 a_collect__plumb_attach(char const *cp, struct header *hp,
1368 char const *cmd[4]){
1369 bool_t status;
1370 struct attachment *ap;
1371 NYD2_ENTER;
1373 if(cmd[1] == NULL)
1374 goto jdefault;
1376 if(is_asccaseprefix(cmd[1], "attribute")){
1377 if(cmd[2] == NULL || cmd[3] != NULL)
1378 goto jecmd;
1380 if((ap = n_attachment_find(hp->h_attach, cmd[2], NULL)) != NULL){
1381 jatt_att:
1382 fprintf(n_stdout, "212 %s\n", cmd[2]);
1383 if(ap->a_msgno > 0)
1384 fprintf(n_stdout, "message-number %d\n\n", ap->a_msgno);
1385 else{
1386 fprintf(n_stdout,
1387 "creation-name %s\nopen-path %s\nfilename %s\n",
1388 ap->a_path_user, ap->a_path, ap->a_name);
1389 if(ap->a_content_description != NULL)
1390 fprintf(n_stdout, "content-description %s\n",
1391 ap->a_content_description);
1392 if(ap->a_content_id != NULL)
1393 fprintf(n_stdout, "content-id %s\n",
1394 ap->a_content_id->n_name);
1395 if(ap->a_content_type != NULL)
1396 fprintf(n_stdout, "content-type %s\n", ap->a_content_type);
1397 if(ap->a_content_disposition != NULL)
1398 fprintf(n_stdout, "content-disposition %s\n",
1399 ap->a_content_disposition);
1400 putc('\n', n_stdout);
1402 }else
1403 fputs("501\n", n_stdout);
1404 goto jleave;
1407 if(is_asccaseprefix(cmd[1], "attribute-at")){
1408 uiz_t i;
1410 if(cmd[2] == NULL || cmd[3] != NULL)
1411 goto jecmd;
1413 if((n_idec_uiz_cp(&i, cmd[2], 0, NULL
1414 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
1415 ) != n_IDEC_STATE_CONSUMED || i == 0)
1416 fputs("505\n", n_stdout);
1417 else{
1418 for(ap = hp->h_attach; ap != NULL && --i != 0; ap = ap->a_flink)
1420 if(ap != NULL)
1421 goto jatt_att;
1422 else
1423 fputs("501\n", n_stdout);
1425 goto jleave;
1428 if(is_asccaseprefix(cmd[1], "attribute-set")){
1429 if(cmd[2] == NULL || cmd[3] == NULL)
1430 goto jecmd;
1432 if((ap = n_attachment_find(hp->h_attach, cmd[2], NULL)) != NULL){
1433 jatt_attset:
1434 if(ap->a_msgno > 0)
1435 fputs("505\n", n_stdout);
1436 else{
1437 char c, *keyw;
1439 cp = cmd[3];
1440 while((c = *cp) != '\0' && !blankchar(c))
1441 ++cp;
1442 keyw = savestrbuf(cmd[3], PTR2SIZE(cp - cmd[3]));
1443 if(c != '\0'){
1444 for(; (c = *++cp) != '\0' && blankchar(c);)
1446 if(c != '\0'){
1447 char *xp;
1449 /* Strip [\r\n] which would render a parameter invalid XXX
1450 * XXX all controls? */
1451 cp = xp = savestr(cp);
1452 for(; (c = *xp) != '\0'; ++xp)
1453 if(c == '\n' || c == '\r')
1454 *xp = ' ';
1455 c = *cp;
1459 if(!asccasecmp(keyw, "filename"))
1460 ap->a_name = (c == '\0') ? ap->a_path_bname : cp;
1461 else if(!asccasecmp(keyw, "content-description"))
1462 ap->a_content_description = (c == '\0') ? NULL : cp;
1463 else if(!asccasecmp(keyw, "content-id")){
1464 ap->a_content_id = NULL;
1466 if(c != '\0'){
1467 struct name *np;
1469 np = checkaddrs(lextract(cp, GREF),
1470 /*EACM_STRICT | TODO '/' valid!! */ EACM_NOLOG |
1471 EACM_NONAME, NULL);
1472 if(np != NULL && np->n_flink == NULL)
1473 ap->a_content_id = np;
1474 else
1475 cp = NULL;
1477 }else if(!asccasecmp(keyw, "content-type"))
1478 ap->a_content_type = (c == '\0') ? NULL : cp;
1479 else if(!asccasecmp(keyw, "content-disposition"))
1480 ap->a_content_disposition = (c == '\0') ? NULL : cp;
1481 else
1482 cp = NULL;
1484 if(cp != NULL){
1485 size_t i;
1487 for(i = 0; ap != NULL; ++i, ap = ap->a_blink)
1489 fprintf(n_stdout, "210 %" PRIuZ "\n", i);
1490 }else
1491 fputs("505\n", n_stdout);
1493 }else
1494 fputs("501\n", n_stdout);
1495 goto jleave;
1498 if(is_asccaseprefix(cmd[1], "attribute-set-at")){
1499 uiz_t i;
1501 if(cmd[2] == NULL || cmd[3] == NULL)
1502 goto jecmd;
1504 if((n_idec_uiz_cp(&i, cmd[2], 0, NULL
1505 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
1506 ) != n_IDEC_STATE_CONSUMED || i == 0)
1507 fputs("505\n", n_stdout);
1508 else{
1509 for(ap = hp->h_attach; ap != NULL && --i != 0; ap = ap->a_flink)
1511 if(ap != NULL)
1512 goto jatt_attset;
1513 else
1514 fputs("501\n", n_stdout);
1516 goto jleave;
1519 if(is_asccaseprefix(cmd[1], "insert")){
1520 enum n_attach_error aerr;
1522 if(cmd[2] == NULL || cmd[3] != NULL)
1523 goto jecmd;
1525 hp->h_attach = n_attachment_append(hp->h_attach, cmd[2], &aerr, &ap);
1526 switch(aerr){
1527 case n_ATTACH_ERR_FILE_OPEN: cp = "505\n"; goto jatt_ins;
1528 case n_ATTACH_ERR_ICONV_FAILED: cp = "506\n"; goto jatt_ins;
1529 case n_ATTACH_ERR_ICONV_NAVAIL:
1530 case n_ATTACH_ERR_OTHER:
1531 default:
1532 cp = "501\n";
1533 jatt_ins:
1534 fputs(cp, n_stdout);
1535 break;
1536 case n_ATTACH_ERR_NONE:{
1537 size_t i;
1539 for(i = 0; ap != NULL; ++i, ap = ap->a_blink)
1541 fprintf(n_stdout, "210 %" PRIuZ "\n", i);
1542 } break;
1544 goto jleave;
1547 if(is_asccaseprefix(cmd[1], "list")){
1548 jdefault:
1549 if(cmd[2] != NULL)
1550 goto jecmd;
1552 if((ap = hp->h_attach) != NULL){
1553 fputs("212\n", n_stdout);
1555 fprintf(n_stdout, "%s\n", ap->a_path_user);
1556 while((ap = ap->a_flink) != NULL);
1557 putc('\n', n_stdout);
1558 }else
1559 fputs("501\n", n_stdout);
1560 goto jleave;
1563 if(is_asccaseprefix(cmd[1], "remove")){
1564 if(cmd[2] == NULL || cmd[3] != NULL)
1565 goto jecmd;
1567 if((ap = n_attachment_find(hp->h_attach, cmd[2], &status)) != NULL){
1568 if(status == TRUM1)
1569 fputs("506\n", n_stdout);
1570 else{
1571 hp->h_attach = n_attachment_remove(hp->h_attach, ap);
1572 fprintf(n_stdout, "210 %s\n", cmd[2]);
1574 }else
1575 fputs("501\n", n_stdout);
1576 goto jleave;
1579 if(is_asccaseprefix(cmd[1], "remove-at")){
1580 uiz_t i;
1582 if(cmd[2] == NULL || cmd[3] != NULL)
1583 goto jecmd;
1585 if((n_idec_uiz_cp(&i, cmd[2], 0, NULL
1586 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
1587 ) != n_IDEC_STATE_CONSUMED || i == 0)
1588 fputs("505\n", n_stdout);
1589 else{
1590 for(ap = hp->h_attach; ap != NULL && --i != 0; ap = ap->a_flink)
1592 if(ap != NULL){
1593 hp->h_attach = n_attachment_remove(hp->h_attach, ap);
1594 fprintf(n_stdout, "210 %s\n", cmd[2]);
1595 }else
1596 fputs("501\n", n_stdout);
1598 goto jleave;
1601 jecmd:
1602 fputs("500\n", n_stdout);
1603 cp = NULL;
1604 jleave:
1605 NYD2_LEAVE;
1606 return (cp != NULL);
1609 static void
1610 _collint(int s)
1612 NYD_X; /* Signal handler */
1614 /* the control flow is subtle, because we can be called from ~q */
1615 if (_coll_hadintr == 0) {
1616 if (ok_blook(ignore)) {
1617 fputs("@\n", n_stdout);
1618 fflush(n_stdout);
1619 clearerr(n_stdin);
1620 } else
1621 _coll_hadintr = 1;
1622 siglongjmp(_coll_jmp, 1);
1624 n_exit_status |= n_EXIT_SEND_ERROR;
1625 if (s != 0)
1626 savedeadletter(_coll_fp, TRU1);
1627 /* Aborting message, no need to fflush() .. */
1628 siglongjmp(_coll_abort, 1);
1631 static void
1632 collhup(int s)
1634 NYD_X; /* Signal handler */
1635 n_UNUSED(s);
1637 savedeadletter(_coll_fp, TRU1);
1638 /* Let's pretend nobody else wants to clean up, a true statement at
1639 * this time */
1640 exit(n_EXIT_ERR);
1643 static int
1644 putesc(char const *s, FILE *stream) /* TODO: v15: drop */
1646 int n = 0, rv = -1;
1647 NYD_ENTER;
1649 while (s[0] != '\0') {
1650 if (s[0] == '\\') {
1651 if (s[1] == 't') {
1652 if (putc('\t', stream) == EOF)
1653 goto jleave;
1654 ++n;
1655 s += 2;
1656 continue;
1658 if (s[1] == 'n') {
1659 if (putc('\n', stream) == EOF)
1660 goto jleave;
1661 ++n;
1662 s += 2;
1663 continue;
1666 if (putc(s[0], stream) == EOF)
1667 goto jleave;
1668 ++n;
1669 ++s;
1671 if (putc('\n', stream) == EOF)
1672 goto jleave;
1673 rv = ++n;
1674 jleave:
1675 NYD_LEAVE;
1676 return rv;
1679 static int
1680 a_coll_ocs__mac(void){
1681 /* Executes in a fork(2)ed child TODO if remains, global MASKs for those! */
1682 setvbuf(n_stdin, NULL, _IOLBF, 0);
1683 setvbuf(n_stdout, NULL, _IOLBF, 0);
1684 n_psonce &= ~(n_PSO_INTERACTIVE | n_PSO_TTYIN | n_PSO_TTYOUT);
1685 n_pstate |= n_PS_COMPOSE_FORKHOOK;
1686 n_readctl_overlay = NULL; /* TODO we need OnForkEvent! See c_readctl() */
1687 if(n_poption & n_PO_D_VV){
1688 char buf[128];
1690 snprintf(buf, sizeof buf, "[%d]%s", getpid(), ok_vlook(log_prefix));
1691 ok_vset(log_prefix, buf);
1693 /* TODO If that uses `!' it will effectively SIG_IGN SIGINT, ...and such */
1694 temporary_compose_mode_hook_call(a_coll_ocs__macname, NULL, NULL);
1695 return 0;
1698 static void
1699 a_coll_ocs__finalize(void *vp){
1700 /* Note we use this for destruction upon setup errors, thus */
1701 sighandler_type opipe;
1702 sighandler_type oint;
1703 struct a_coll_ocs_arg **coapp, *coap;
1704 NYD2_ENTER;
1706 temporary_compose_mode_hook_call((char*)-1, NULL, NULL);
1708 coap = *(coapp = vp);
1709 *coapp = (struct a_coll_ocs_arg*)-1;
1711 if(coap->coa_stdin != NULL)
1712 Fclose(coap->coa_stdin);
1713 else if(coap->coa_pipe[0] != -1)
1714 close(coap->coa_pipe[0]);
1716 if(coap->coa_stdout != NULL && !Pclose(coap->coa_stdout, TRU1))
1717 *coap->coa_senderr = 111;
1718 if(coap->coa_pipe[1] != -1)
1719 close(coap->coa_pipe[1]);
1721 opipe = coap->coa_opipe;
1722 oint = coap->coa_oint;
1724 n_lofi_free(coap);
1726 hold_all_sigs();
1727 safe_signal(SIGPIPE, opipe);
1728 safe_signal(SIGINT, oint);
1729 rele_all_sigs();
1730 NYD2_LEAVE;
1733 FL void
1734 n_temporary_compose_hook_varset(void *arg){ /* TODO v15: drop */
1735 struct header *hp;
1736 char const *val;
1737 NYD2_ENTER;
1739 hp = arg;
1741 if((val = hp->h_subject) == NULL)
1742 val = n_empty;
1743 ok_vset(mailx_subject, val);
1744 if((val = detract(hp->h_from, GNAMEONLY)) == NULL)
1745 val = n_empty;
1746 ok_vset(mailx_from, val);
1747 if((val = detract(hp->h_sender, GNAMEONLY)) == NULL)
1748 val = n_empty;
1749 ok_vset(mailx_sender, val);
1750 if((val = detract(hp->h_to, GNAMEONLY)) == NULL)
1751 val = n_empty;
1752 ok_vset(mailx_to, val);
1753 if((val = detract(hp->h_cc, GNAMEONLY)) == NULL)
1754 val = n_empty;
1755 ok_vset(mailx_cc, val);
1756 if((val = detract(hp->h_bcc, GNAMEONLY)) == NULL)
1757 val = n_empty;
1758 ok_vset(mailx_bcc, val);
1760 if((val = hp->h_mailx_command) == NULL)
1761 val = n_empty;
1762 ok_vset(mailx_command, val);
1764 if((val = detract(hp->h_mailx_raw_to, GNAMEONLY)) == NULL)
1765 val = n_empty;
1766 ok_vset(mailx_raw_to, val);
1767 if((val = detract(hp->h_mailx_raw_cc, GNAMEONLY)) == NULL)
1768 val = n_empty;
1769 ok_vset(mailx_raw_cc, val);
1770 if((val = detract(hp->h_mailx_raw_bcc, GNAMEONLY)) == NULL)
1771 val = n_empty;
1772 ok_vset(mailx_raw_bcc, val);
1774 if((val = detract(hp->h_mailx_orig_from, GNAMEONLY)) == NULL)
1775 val = n_empty;
1776 ok_vset(mailx_orig_from, val);
1777 if((val = detract(hp->h_mailx_orig_to, GNAMEONLY)) == NULL)
1778 val = n_empty;
1779 ok_vset(mailx_orig_to, val);
1780 if((val = detract(hp->h_mailx_orig_cc, GNAMEONLY)) == NULL)
1781 val = n_empty;
1782 ok_vset(mailx_orig_cc, val);
1783 if((val = detract(hp->h_mailx_orig_bcc, GNAMEONLY)) == NULL)
1784 val = n_empty;
1785 ok_vset(mailx_orig_bcc, val);
1786 NYD2_LEAVE;
1789 FL FILE *
1790 collect(struct header *hp, int printheaders, struct message *mp,
1791 char const *quotefile, int doprefix, si8_t *checkaddr_err)
1793 struct n_string s, * volatile sp;
1794 struct n_ignore const *quoteitp;
1795 struct a_coll_ocs_arg *coap;
1796 int c;
1797 int volatile t, eofcnt, getfields;
1798 char volatile escape;
1799 enum{
1800 a_NONE,
1801 a_ERREXIT = 1u<<0,
1802 a_IGNERR = 1u<<1,
1803 a_COAP_NOSIGTERM = 1u<<8
1804 #define a_HARDERR() ((flags & (a_ERREXIT | a_IGNERR)) == a_ERREXIT)
1805 } volatile flags;
1806 char *linebuf;
1807 char const *cp, *cp_base, * volatile coapm, * volatile ifs_saved;
1808 size_t i, linesize; /* TODO line pool */
1809 long cnt;
1810 enum sendaction action;
1811 sigset_t oset, nset;
1812 FILE * volatile sigfp;
1813 NYD_ENTER;
1815 _coll_fp = NULL;
1816 sigfp = NULL;
1817 linesize = 0;
1818 linebuf = NULL;
1819 eofcnt = 0;
1820 ifs_saved = coapm = NULL;
1821 coap = NULL;
1822 sp = NULL;
1824 /* Start catching signals from here, but we're still die on interrupts
1825 * until we're in the main loop */
1826 sigfillset(&nset);
1827 sigprocmask(SIG_BLOCK, &nset, &oset);
1828 if ((_coll_saveint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
1829 safe_signal(SIGINT, &_collint);
1830 if ((_coll_savehup = safe_signal(SIGHUP, SIG_IGN)) != SIG_IGN)
1831 safe_signal(SIGHUP, collhup);
1832 if (sigsetjmp(_coll_abort, 1))
1833 goto jerr;
1834 if (sigsetjmp(_coll_jmp, 1))
1835 goto jerr;
1836 n_pstate |= n_PS_COMPOSE_MODE;
1837 sigprocmask(SIG_SETMASK, &oset, (sigset_t*)NULL);
1839 if ((_coll_fp = Ftmp(NULL, "collect", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
1840 NULL) {
1841 n_perr(_("temporary mail file"), 0);
1842 goto jerr;
1845 /* If we are going to prompt for a subject, refrain from printing a newline
1846 * after the headers (since some people mind) */
1847 getfields = 0;
1848 if (!(n_poption & n_PO_t_FLAG)) {
1849 t = GTO | GSUBJECT | GCC | GNL;
1850 if (ok_blook(fullnames))
1851 t |= GCOMMA;
1853 if (n_psonce & n_PSO_INTERACTIVE) {
1854 if (hp->h_subject == NULL && (ok_blook(ask) || ok_blook(asksub)))
1855 t &= ~GNL, getfields |= GSUBJECT;
1857 if (hp->h_to == NULL)
1858 t &= ~GNL, getfields |= GTO;
1860 if (!ok_blook(bsdcompat) && !ok_blook(askatend)) {
1861 if (hp->h_bcc == NULL && ok_blook(askbcc))
1862 t &= ~GNL, getfields |= GBCC;
1863 if (hp->h_cc == NULL && ok_blook(askcc))
1864 t &= ~GNL, getfields |= GCC;
1867 } else {
1868 n_UNINIT(t, 0);
1871 _coll_hadintr = 0;
1873 if (!sigsetjmp(_coll_jmp, 1)) {
1874 /* Ask for some headers first, as necessary */
1875 if (getfields)
1876 grab_headers(n_GO_INPUT_CTX_COMPOSE, hp, getfields, 1);
1878 /* Execute compose-enter; delayed for -t mode */
1879 if(!(n_poption & n_PO_t_FLAG) &&
1880 (cp = ok_vlook(on_compose_enter)) != NULL){
1881 setup_from_and_sender(hp);
1882 temporary_compose_mode_hook_call(cp, &n_temporary_compose_hook_varset,
1883 hp);
1886 /* TODO Mm: nope since it may require turning this into a multipart one */
1887 if(!(n_poption & (n_PO_Mm_FLAG | n_PO_t_FLAG))){
1888 if(!a_coll_message_inject_head(_coll_fp))
1889 goto jerr;
1891 /* Quote an original message */
1892 if (mp != NULL && (doprefix || (cp = ok_vlook(quote)) != NULL)) {
1893 quoteitp = n_IGNORE_ALL;
1894 action = SEND_QUOTE;
1895 if (doprefix) {
1896 char const *cp_v15compat;
1898 quoteitp = n_IGNORE_FWD;
1900 if((cp_v15compat = ok_vlook(fwdheading)) != NULL)
1901 n_OBSOLETE(_("please use *forward-inject-head*, "
1902 "not *fwdheading*"));
1903 cp = ok_vlook(forward_inject_head);
1904 if(cp == NULL && cp_v15compat == NULL)
1905 cp = "-------- Original Message --------";
1906 if (*cp != '\0' && fprintf(_coll_fp, "%s\n", cp) < 0)
1907 goto jerr;
1908 } else if (!strcmp(cp, "noheading")) {
1909 /*EMPTY*/;
1910 } else if (!strcmp(cp, "headers")) {
1911 quoteitp = n_IGNORE_TYPE;
1912 } else if (!strcmp(cp, "allheaders")) {
1913 quoteitp = NULL;
1914 action = SEND_QUOTE_ALL;
1915 } else {
1916 cp = hfield1("from", mp);
1917 if (cp != NULL && (cnt = (long)strlen(cp)) > 0) {
1918 if (xmime_write(cp, cnt, _coll_fp, CONV_FROMHDR, TD_NONE) < 0)
1919 goto jerr;
1920 if (fprintf(_coll_fp, _(" wrote:\n\n")) < 0)
1921 goto jerr;
1924 if (fflush(_coll_fp))
1925 goto jerr;
1926 if (doprefix)
1927 cp = NULL;
1928 else if ((cp = ok_vlook(indentprefix)) == NULL)
1929 cp = INDENT_DEFAULT;
1930 if (sendmp(mp, _coll_fp, quoteitp, cp, action, NULL) < 0)
1931 goto jerr;
1935 if (quotefile != NULL) {
1936 if((n_pstate_err_no = a_coll_include_file(quotefile, FAL0, FAL0)
1937 ) != n_ERR_NONE)
1938 goto jerr;
1941 if(n_psonce & n_PSO_INTERACTIVE){
1942 if(!(n_pstate & n_PS_SOURCING)){
1943 sp = n_string_creat_auto(&s);
1944 sp = n_string_reserve(sp, 80);
1947 if(!(n_poption & n_PO_Mm_FLAG)){
1948 /* Print what we have sofar also on the terminal (if useful) */
1949 if (!ok_blook(editalong)) {
1950 if (printheaders)
1951 puthead(TRU1, hp, n_stdout, t,
1952 SEND_TODISP, CONV_NONE, NULL, NULL);
1954 rewind(_coll_fp);
1955 while ((c = getc(_coll_fp)) != EOF) /* XXX bytewise, yuck! */
1956 putc(c, n_stdout);
1957 if (fseek(_coll_fp, 0, SEEK_END))
1958 goto jerr;
1959 } else {
1960 rewind(_coll_fp);
1961 if(a_coll_edit('e', hp) != n_ERR_NONE)
1962 goto jerr;
1963 /* Print msg mandated by the Mail Reference Manual */
1964 jcont:
1965 if(n_psonce & n_PSO_INTERACTIVE)
1966 fputs(_("(continue)\n"), n_stdout);
1968 fflush(n_stdout);
1971 } else {
1972 /* Come here for printing the after-signal message. Duplicate messages
1973 * won't be printed because the write is aborted if we get a SIGTTOU */
1974 if (_coll_hadintr)
1975 n_err(_("\n(Interrupt -- one more to kill letter)\n"));
1978 /* If not under shell hook control */
1979 if(coap == NULL){
1980 /* We're done with -M or -m TODO because: we are too stupid yet, above */
1981 if(n_poption & n_PO_Mm_FLAG)
1982 goto jout;
1983 /* No command escapes, interrupts not expected. Simply copy STDIN */
1984 if(!(n_psonce & n_PSO_INTERACTIVE) &&
1985 !(n_poption & (n_PO_t_FLAG | n_PO_TILDE_FLAG))){
1986 linebuf = srealloc(linebuf, linesize = LINESIZE);
1987 while ((i = fread(linebuf, sizeof *linebuf, linesize, n_stdin)) > 0) {
1988 if (i != fwrite(linebuf, sizeof *linebuf, i, _coll_fp))
1989 goto jerr;
1991 goto jout;
1995 /* The interactive collect loop */
1996 if(coap == NULL)
1997 escape = *ok_vlook(escape);
1998 flags = ok_blook(errexit) ? a_ERREXIT : a_NONE;
2000 for(;;){
2001 enum {a_HIST_NONE, a_HIST_ADD = 1u<<0, a_HIST_GABBY = 1u<<1} hist;
2003 /* C99 */{
2004 enum n_go_input_flags gif;
2005 bool_t histadd;
2007 gif = n_GO_INPUT_CTX_COMPOSE;
2008 histadd = (sp != NULL);
2009 if((n_psonce & n_PSO_INTERACTIVE) || (n_poption & n_PO_TILDE_FLAG)){
2010 if(!(n_poption & n_PO_t_FLAG))
2011 gif |= n_GO_INPUT_NL_ESC;
2013 cnt = n_go_input(gif, n_empty, &linebuf, &linesize, NULL, &histadd);
2014 hist = histadd ? a_HIST_ADD | a_HIST_GABBY : a_HIST_NONE;
2017 if(cnt < 0){
2018 if(coap != NULL)
2019 break;
2020 if(n_poption & n_PO_t_FLAG){
2021 fflush_rewind(_coll_fp);
2022 /* It is important to set n_PSO_t_FLAG before extract_header()
2023 * *and* keep n_PO_t_FLAG for the first parse of the message!
2024 * TODO This is a hack, we need a clean approach for this */
2025 n_psonce |= n_PSO_t_FLAG;
2026 if(!a_coll_makeheader(_coll_fp, hp, checkaddr_err, TRU1))
2027 goto jerr;
2028 n_poption &= ~n_PO_t_FLAG;
2029 continue;
2030 }else if((n_psonce & n_PSO_INTERACTIVE) && ok_blook(ignoreeof) &&
2031 ++eofcnt < 4){
2032 fprintf(n_stdout,
2033 _("*ignoreeof* set, use `~.' to terminate letter\n"));
2034 n_go_input_clearerr();
2035 continue;
2037 break;
2040 _coll_hadintr = 0;
2042 cp = linebuf;
2043 if(cnt == 0)
2044 goto jputnl;
2045 else if(coap == NULL){
2046 if(!(n_psonce & n_PSO_INTERACTIVE) && !(n_poption & n_PO_TILDE_FLAG))
2047 goto jputline;
2048 else if(cp[0] == '.'){
2049 if(cnt == 1 && (ok_blook(dot) ||
2050 (ok_blook(posix) && ok_blook(ignoreeof))))
2051 break;
2054 if(cp[0] != escape){
2055 jputline:
2056 if(fwrite(cp, sizeof *cp, cnt, _coll_fp) != (size_t)cnt)
2057 goto jerr;
2058 /* TODO n_PS_READLINE_NL is a terrible hack to ensure that _in_all_-
2059 * TODO _code_paths_ a file without trailing newline isn't modified
2060 * TODO to contain one; the "saw-newline" needs to be part of an
2061 * TODO I/O input machinery object */
2062 jputnl:
2063 if(n_pstate & n_PS_READLINE_NL){
2064 if(putc('\n', _coll_fp) == EOF)
2065 goto jerr;
2067 continue;
2070 c = *(cp_base = ++cp);
2071 if(--cnt == 0)
2072 goto jearg;
2074 /* Avoid history entry? */
2075 while(spacechar(c)){
2076 hist = a_HIST_NONE;
2077 c = *(cp_base = ++cp);
2078 if(--cnt == 0)
2079 goto jearg;
2082 /* It may just be an escaped escaped character, do that quick */
2083 if(c == escape)
2084 goto jputline;
2086 /* Avoid hard *errexit*? */
2087 flags &= ~a_IGNERR;
2088 if(c == '-'){
2089 flags ^= a_IGNERR;
2090 c = *++cp;
2091 if(--cnt == 0)
2092 goto jearg;
2095 /* Trim input, also to gain a somewhat normalized history entry */
2096 ++cp;
2097 if(--cnt > 0){
2098 struct str x;
2100 x.s = n_UNCONST(cp);
2101 x.l = (size_t)cnt;
2102 n_str_trim_ifs(&x, TRU1);
2103 x.s[x.l] = '\0';
2104 cp = x.s;
2105 cnt = (int)/*XXX*/x.l;
2108 if(hist != a_HIST_NONE){
2109 sp = n_string_assign_c(sp, escape);
2110 if(flags & a_IGNERR)
2111 sp = n_string_push_c(sp, '-');
2112 sp = n_string_push_c(sp, c);
2113 if(cnt > 0){
2114 sp = n_string_push_c(sp, ' ');
2115 sp = n_string_push_buf(sp, cp, cnt);
2119 switch(c){
2120 default:
2121 if(1){
2122 char buf[sizeof(n_UNIREPL)];
2124 if(asciichar(c))
2125 buf[0] = c, buf[1] = '\0';
2126 else if(n_psonce & n_PSO_UNICODE)
2127 memcpy(buf, n_unirepl, sizeof n_unirepl);
2128 else
2129 buf[0] = '?', buf[1] = '\0';
2130 n_err(_("Unknown command escape: `%c%s'\n"), escape, buf);
2131 }else
2132 jearg:
2133 n_err(_("Invalid command escape usage: %s\n"),
2134 n_shexp_quote_cp(linebuf, FAL0));
2135 if(a_HARDERR())
2136 goto jerr;
2137 n_pstate_err_no = n_ERR_INVAL;
2138 n_pstate_ex_no = 1;
2139 continue;
2140 case '!':
2141 /* Shell escape, send the balance of line to sh -c */
2142 if(cnt == 0 || coap != NULL)
2143 goto jearg;
2144 else{
2145 char const *argv[2];
2147 argv[0] = cp;
2148 argv[1] = NULL;
2149 n_pstate_ex_no = c_shell(argv); /* TODO history norm.; errexit? */
2151 goto jhistcont;
2152 case ':':
2153 /* FALLTHRU */
2154 case '_':
2155 /* Escape to command mode, but be nice! *//* TODO command expansion
2156 * TODO should be handled here so that we have unique history! */
2157 if(cnt == 0)
2158 goto jearg;
2159 _execute_command(hp, cp, cnt);
2160 if(ok_blook(errexit))
2161 flags |= a_ERREXIT;
2162 else
2163 flags &= ~a_ERREXIT;
2164 if(n_pstate_ex_no != 0 && a_HARDERR())
2165 goto jerr;
2166 if(coap == NULL)
2167 escape = *ok_vlook(escape);
2168 break;
2169 case '.':
2170 /* Simulate end of file on input */
2171 if(cnt != 0 || coap != NULL)
2172 goto jearg;
2173 goto jout; /* TODO does not enter history, thus */
2174 case 'x':
2175 /* Same as 'q', but no *DEAD* saving */
2176 /* FALLTHRU */
2177 case 'q':
2178 /* Force a quit, act like an interrupt had happened */
2179 if(cnt != 0)
2180 goto jearg;
2181 /* If we are running a splice hook, assume it quits on its own now,
2182 * otherwise we (no true out-of-band IPC to signal this state, XXX sic)
2183 * have to SIGTERM it in order to stop this wild beast */
2184 flags |= a_COAP_NOSIGTERM;
2185 ++_coll_hadintr;
2186 _collint((c == 'x') ? 0 : SIGINT);
2187 exit(n_EXIT_ERR);
2188 /*NOTREACHED*/
2189 case 'h':
2190 /* Grab a bunch of headers */
2191 if(cnt != 0)
2192 goto jearg;
2194 grab_headers(n_GO_INPUT_CTX_COMPOSE, hp,
2195 (GTO | GSUBJECT | GCC | GBCC),
2196 (ok_blook(bsdcompat) && ok_blook(bsdorder)));
2197 while(hp->h_to == NULL);
2198 n_pstate_err_no = n_ERR_NONE; /* XXX */
2199 n_pstate_ex_no = 0; /* XXX */
2200 break;
2201 case 'H':
2202 /* Grab extra headers */
2203 if(cnt != 0)
2204 goto jearg;
2206 grab_headers(n_GO_INPUT_CTX_COMPOSE, hp, GEXTRA, 0);
2207 while(check_from_and_sender(hp->h_from, hp->h_sender) == NULL);
2208 n_pstate_err_no = n_ERR_NONE; /* XXX */
2209 n_pstate_ex_no = 0; /* XXX */
2210 break;
2211 case 't':
2212 /* Add to the To: list */
2213 if(cnt == 0)
2214 goto jearg;
2215 hp->h_to = cat(hp->h_to,
2216 checkaddrs(lextract(cp, GTO | GFULL), EACM_NORMAL, NULL));
2217 n_pstate_err_no = n_ERR_NONE; /* XXX */
2218 n_pstate_ex_no = 0; /* XXX */
2219 hist &= ~a_HIST_GABBY;
2220 break;
2221 case 's':
2222 /* Set the Subject list */
2223 if(cnt == 0)
2224 goto jearg;
2225 /* Subject:; take care for Debian #419840 and strip any \r and \n */
2226 if(n_anyof_cp("\n\r", hp->h_subject = savestr(cp))){
2227 char *xp;
2229 n_err(_("-s: normalizing away invalid ASCII NL / CR bytes\n"));
2230 for(xp = hp->h_subject; *xp != '\0'; ++xp)
2231 if(*xp == '\n' || *xp == '\r')
2232 *xp = ' ';
2233 n_pstate_err_no = n_ERR_INVAL;
2234 n_pstate_ex_no = 1;
2235 }else{
2236 n_pstate_err_no = n_ERR_NONE;
2237 n_pstate_ex_no = 0;
2239 break;
2240 case '@':{
2241 struct attachment *aplist;
2243 /* Edit the attachment list */
2244 aplist = hp->h_attach;
2245 hp->h_attach = NULL;
2246 if(cnt != 0)
2247 hp->h_attach = n_attachment_append_list(aplist, cp);
2248 else
2249 hp->h_attach = n_attachment_list_edit(aplist,
2250 n_GO_INPUT_CTX_COMPOSE);
2251 n_pstate_err_no = n_ERR_NONE; /* XXX ~@ does NOT handle $!/$?! */
2252 n_pstate_ex_no = 0; /* XXX */
2253 } break;
2254 case 'c':
2255 /* Add to the CC list */
2256 if(cnt == 0)
2257 goto jearg;
2258 hp->h_cc = cat(hp->h_cc,
2259 checkaddrs(lextract(cp, GCC | GFULL), EACM_NORMAL, NULL));
2260 n_pstate_err_no = n_ERR_NONE; /* XXX */
2261 n_pstate_ex_no = 0; /* XXX */
2262 hist &= ~a_HIST_GABBY;
2263 break;
2264 case 'b':
2265 /* Add stuff to blind carbon copies list */
2266 if(cnt == 0)
2267 goto jearg;
2268 hp->h_bcc = cat(hp->h_bcc,
2269 checkaddrs(lextract(cp, GBCC | GFULL), EACM_NORMAL, NULL));
2270 n_pstate_err_no = n_ERR_NONE; /* XXX */
2271 n_pstate_ex_no = 0; /* XXX */
2272 hist &= ~a_HIST_GABBY;
2273 break;
2274 case 'd':
2275 if(cnt != 0)
2276 goto jearg;
2277 cp = n_getdeadletter();
2278 if(0){
2279 /*FALLTHRU*/
2280 case 'R':
2281 case 'r':
2282 case '<':
2283 /* Invoke a file: Search for the file name, then open it and copy
2284 * the contents to _coll_fp */
2285 if(cnt == 0){
2286 n_err(_("Interpolate what file?\n"));
2287 if(a_HARDERR())
2288 goto jerr;
2289 n_pstate_err_no = n_ERR_NOENT;
2290 n_pstate_ex_no = 1;
2291 break;
2293 if(*cp == '!' && c == '<'){
2294 /* TODO hist. normalization */
2295 if((n_pstate_err_no = a_coll_insert_cmd(_coll_fp, ++cp)
2296 ) != n_ERR_NONE){
2297 if(ferror(_coll_fp))
2298 goto jerr;
2299 if(a_HARDERR())
2300 goto jerr;
2301 n_pstate_ex_no = 1;
2302 break;
2304 goto jhistcont;
2306 /* Note this also expands things like
2307 * !:vput vexpr delim random 0
2308 * !< - $delim */
2309 if((cp = fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO | FEXP_NSHELL)
2310 ) == NULL){
2311 if(a_HARDERR())
2312 goto jerr;
2313 n_pstate_err_no = n_ERR_INVAL;
2314 n_pstate_ex_no = 1;
2315 break;
2318 if(n_is_dir(cp, FAL0)){
2319 n_err(_("%s: is a directory\n"), n_shexp_quote_cp(cp, FAL0));
2320 if(a_HARDERR())
2321 goto jerr;
2322 n_pstate_err_no = n_ERR_ISDIR;
2323 n_pstate_ex_no = 1;
2324 break;
2326 if((n_pstate_err_no = a_coll_include_file(cp, (c == 'R'), TRU1)
2327 ) != n_ERR_NONE){
2328 if(ferror(_coll_fp))
2329 goto jerr;
2330 if(a_HARDERR())
2331 goto jerr;
2332 n_pstate_ex_no = 1;
2333 break;
2335 n_pstate_err_no = n_ERR_NONE; /* XXX */
2336 n_pstate_ex_no = 0; /* XXX */
2337 break;
2338 case 'i':
2339 /* Insert a variable into the file */
2340 if(cnt == 0)
2341 goto jearg;
2342 if((cp = n_var_vlook(cp, TRU1)) == NULL || *cp == '\0')
2343 break;
2344 if(putesc(cp, _coll_fp) < 0) /* TODO v15: user resp upon `set' time */
2345 goto jerr;
2346 if((n_psonce & n_PSO_INTERACTIVE) && putesc(cp, n_stdout) < 0)
2347 goto jerr;
2348 n_pstate_err_no = n_ERR_NONE; /* XXX */
2349 n_pstate_ex_no = 0; /* XXX */
2350 break;
2351 case 'a':
2352 case 'A':
2353 /* Insert the contents of a signature variable */
2354 if(cnt != 0)
2355 goto jearg;
2356 cp = (c == 'a') ? ok_vlook(sign) : ok_vlook(Sign);
2357 if(cp != NULL && *cp != '\0'){
2358 if(putesc(cp, _coll_fp) < 0) /* TODO v15: user upon `set' time */
2359 goto jerr;
2360 if((n_psonce & n_PSO_INTERACTIVE) && putesc(cp, n_stdout) < 0)
2361 goto jerr;
2363 n_pstate_err_no = n_ERR_NONE; /* XXX */
2364 n_pstate_ex_no = 0; /* XXX */
2365 break;
2366 case 'w':
2367 /* Write the message on a file */
2368 if(cnt == 0)
2369 goto jearg;
2370 if((cp = fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO)) == NULL){
2371 n_err(_("Write what file!?\n"));
2372 if(a_HARDERR())
2373 goto jerr;
2374 n_pstate_err_no = n_ERR_INVAL;
2375 n_pstate_ex_no = 1;
2376 break;
2378 rewind(_coll_fp);
2379 if((n_pstate_err_no = a_coll_write(cp, _coll_fp, 1)) == n_ERR_NONE)
2380 n_pstate_ex_no = 0;
2381 else if(ferror(_coll_fp))
2382 goto jerr;
2383 else if(a_HARDERR())
2384 goto jerr;
2385 else
2386 n_pstate_ex_no = 1;
2387 break;
2388 case 'm':
2389 case 'M':
2390 case 'f':
2391 case 'F':
2392 case 'u':
2393 case 'U':
2394 /* Interpolate the named messages, if we are in receiving mail mode.
2395 * Does the standard list processing garbage. If ~f is given, we
2396 * don't shift over */
2397 if(cnt == 0)
2398 goto jearg;
2399 if((n_pstate_err_no = a_coll_forward(cp, _coll_fp, c)) == n_ERR_NONE)
2400 n_pstate_ex_no = 0;
2401 else if(ferror(_coll_fp))
2402 goto jerr;
2403 else if(a_HARDERR())
2404 goto jerr;
2405 else
2406 n_pstate_ex_no = 1;
2407 break;
2408 case 'p':
2409 /* Print current state of the message without altering anything */
2410 if(cnt != 0)
2411 goto jearg;
2412 print_collf(_coll_fp, hp); /* XXX pstate_err_no ++ */
2413 if(ferror(_coll_fp))
2414 goto jerr;
2415 n_pstate_err_no = n_ERR_NONE; /* XXX */
2416 n_pstate_ex_no = 0; /* XXX */
2417 break;
2418 case '|':
2419 /* Pipe message through command. Collect output as new message */
2420 if(cnt == 0)
2421 goto jearg;
2422 rewind(_coll_fp);
2423 if((n_pstate_err_no = a_coll_pipe(cp)) == n_ERR_NONE)
2424 n_pstate_ex_no = 0;
2425 else if(a_HARDERR())
2426 goto jerr;
2427 else
2428 n_pstate_ex_no = 1;
2429 hist &= ~a_HIST_GABBY;
2430 goto jhistcont;
2431 case 'v':
2432 case 'e':
2433 /* Edit the current message. 'e' -> use EDITOR, 'v' -> use VISUAL */
2434 if(cnt != 0 || coap != NULL)
2435 goto jearg;
2436 rewind(_coll_fp);
2437 if((n_pstate_err_no = a_coll_edit(c, ok_blook(editheaders) ? hp : NULL)
2438 ) == n_ERR_NONE)
2439 n_pstate_ex_no = 0;
2440 else if(ferror(_coll_fp))
2441 goto jerr;
2442 else if(a_HARDERR())
2443 goto jerr;
2444 else
2445 n_pstate_ex_no = 1;
2446 goto jhistcont;
2447 case '^':
2448 if(!a_collect_plumbing(cp, hp)){
2449 if(ferror(_coll_fp))
2450 goto jerr;
2451 goto jearg;
2453 n_pstate_err_no = n_ERR_NONE; /* XXX */
2454 n_pstate_ex_no = 0; /* XXX */
2455 hist &= ~a_HIST_GABBY;
2456 break;
2457 case '?':
2458 /* Last the lengthy help string. (Very ugly, but take care for
2459 * compiler supported string lengths :() */
2460 fputs(_(
2461 "COMMAND ESCAPES (to be placed after a newline) excerpt:\n"
2462 "~. Commit and send message\n"
2463 "~: <command> Execute an internal command\n"
2464 "~< <file> Insert <file> (\"~<! <command>\" inserts shell command)\n"
2465 "~@ [<files>] Edit[/Add] attachments (file[=input-charset[#output-charset]])\n"
2466 "~A Insert *Sign* variable (`~a': insert *sign*)\n"
2467 "~c <users> Add users to Cc: list (`~b': to Bcc:)\n"
2468 "~d Read in $DEAD (dead.letter)\n"
2469 "~e Edit message via $EDITOR\n"
2470 ), n_stdout);
2471 fputs(_(
2472 "~F <msglist> Read in with headers, do not *indentprefix* lines\n"
2473 "~f <msglist> Like ~F, but honour `ignore' / `retain' configuration\n"
2474 "~H Edit From:, Reply-To: and Sender:\n"
2475 "~h Prompt for Subject:, To:, Cc: and \"blind\" Bcc:\n"
2476 "~i <variable> Insert a value and a newline\n"
2477 "~M <msglist> Read in with headers, *indentprefix* (`~m': `retain' etc.)\n"
2478 "~p Show current message compose buffer\n"
2479 "~r <file> Insert <file> (`~R': likewise, but *indentprefix* lines)\n"
2480 ), n_stdout);
2481 fputs(_(
2482 "~s <subject> Set Subject:\n"
2483 "~t <users> Add users to To: list\n"
2484 "~u <msglist> Read in message(s) without headers (`~U': indent lines)\n"
2485 "~v Edit message via $VISUAL\n"
2486 "~w <file> Write message onto file\n"
2487 "~x Abort composition, discard message (`~q': save in $DEAD)\n"
2488 "~| <command> Pipe message through shell filter\n"
2489 ), n_stdout);
2490 if(cnt != 0)
2491 goto jearg;
2492 n_pstate_err_no = n_ERR_NONE;
2493 n_pstate_ex_no = 0;
2494 break;
2497 /* Finally place an entry in history as applicable */
2498 if(0){
2499 jhistcont:
2500 c = '\1';
2501 }else
2502 c = '\0';
2503 if(hist & a_HIST_ADD)
2504 n_tty_addhist(n_string_cp(sp), ((hist & a_HIST_GABBY) != 0));
2505 if(c != '\0')
2506 goto jcont;
2509 jout:
2510 /* Do we have *on-compose-splice-shell*, or *on-compose-splice*?
2511 * TODO Usual f...ed up state of signals and terminal etc. */
2512 if(coap == NULL && (cp = ok_vlook(on_compose_splice_shell)) != NULL) Jocs:{
2513 union {int (*ptf)(void); char const *sh;} u;
2514 char const *cmd;
2516 /* Reset *escape* and more to their defaults. On change update manual! */
2517 if(ifs_saved == NULL)
2518 ifs_saved = savestr(ok_vlook(ifs));
2519 escape = n_ESCAPE[0];
2520 ok_vclear(ifs);
2522 if(coapm != NULL){
2523 /* XXX Due Popen() fflush(NULL) in PTF mode, ensure nothing to flush */
2524 /*if(!n_real_seek(_coll_fp, 0, SEEK_END))
2525 * goto jerr;*/
2526 u.ptf = &a_coll_ocs__mac;
2527 cmd = (char*)-1;
2528 a_coll_ocs__macname = cp = coapm;
2529 }else{
2530 u.sh = ok_vlook(SHELL);
2531 cmd = cp;
2534 i = strlen(cp) +1;
2535 coap = n_lofi_alloc(n_VSTRUCT_SIZEOF(struct a_coll_ocs_arg, coa_cmd
2536 ) + i);
2537 coap->coa_pipe[0] = coap->coa_pipe[1] = -1;
2538 coap->coa_stdin = coap->coa_stdout = NULL;
2539 coap->coa_senderr = checkaddr_err;
2540 memcpy(coap->coa_cmd, cp, i);
2542 hold_all_sigs();
2543 coap->coa_opipe = safe_signal(SIGPIPE, SIG_IGN);
2544 coap->coa_oint = safe_signal(SIGINT, SIG_IGN);
2545 rele_all_sigs();
2547 if(pipe_cloexec(coap->coa_pipe) != -1 &&
2548 (coap->coa_stdin = Fdopen(coap->coa_pipe[0], "r", FAL0)) != NULL &&
2549 (coap->coa_stdout = Popen(cmd, "W", u.sh, NULL, coap->coa_pipe[1])
2550 ) != NULL){
2551 close(coap->coa_pipe[1]);
2552 coap->coa_pipe[1] = -1;
2554 temporary_compose_mode_hook_call(NULL, NULL, NULL);
2555 n_go_splice_hack(coap->coa_cmd, coap->coa_stdin, coap->coa_stdout,
2556 (n_psonce & ~(n_PSO_INTERACTIVE | n_PSO_TTYIN | n_PSO_TTYOUT)),
2557 &a_coll_ocs__finalize, &coap);
2558 /* Hook version protocol for ~^: update manual upon change! */
2559 fputs(a_COLL_PLUMBING_VERSION "\n", coap->coa_stdout);
2560 #undef a_COLL_PLUMBING_VERSION
2561 goto jcont;
2564 c = n_err_no;
2565 a_coll_ocs__finalize(coap);
2566 n_perr(_("Cannot invoke *on-compose-splice(-shell)?*"), c);
2567 goto jerr;
2569 if(*checkaddr_err != 0){
2570 *checkaddr_err = 0;
2571 goto jerr;
2573 if(coapm == NULL && (coapm = ok_vlook(on_compose_splice)) != NULL)
2574 goto Jocs;
2575 if(coap != NULL){
2576 ok_vset(ifs, ifs_saved);
2577 ifs_saved = NULL;
2580 /* Final chance to edit headers, if not already above */
2581 if (ok_blook(bsdcompat) || ok_blook(askatend)) {
2582 if (hp->h_cc == NULL && ok_blook(askcc))
2583 grab_headers(n_GO_INPUT_CTX_COMPOSE, hp, GCC, 1);
2584 if (hp->h_bcc == NULL && ok_blook(askbcc))
2585 grab_headers(n_GO_INPUT_CTX_COMPOSE, hp, GBCC, 1);
2587 if (hp->h_attach == NULL && ok_blook(askattach))
2588 hp->h_attach = n_attachment_list_edit(hp->h_attach,
2589 n_GO_INPUT_CTX_COMPOSE);
2591 /* Execute compose-leave */
2592 if((cp = ok_vlook(on_compose_leave)) != NULL){
2593 setup_from_and_sender(hp);
2594 temporary_compose_mode_hook_call(cp, &n_temporary_compose_hook_varset,
2595 hp);
2598 /* Add automatic receivers */
2599 if ((cp = ok_vlook(autocc)) != NULL && *cp != '\0')
2600 hp->h_cc = cat(hp->h_cc, checkaddrs(lextract(cp, GCC | GFULL),
2601 EACM_NORMAL, checkaddr_err));
2602 if ((cp = ok_vlook(autobcc)) != NULL && *cp != '\0')
2603 hp->h_bcc = cat(hp->h_bcc, checkaddrs(lextract(cp, GBCC | GFULL),
2604 EACM_NORMAL, checkaddr_err));
2605 if (*checkaddr_err != 0)
2606 goto jerr;
2608 /* TODO Cannot do since it may require turning this into a multipart one */
2609 if(n_poption & n_PO_Mm_FLAG)
2610 goto jskiptails;
2612 /* Place signature? */
2613 if((cp = ok_vlook(signature)) != NULL && *cp != '\0'){
2614 char const *cpq;
2616 if((cpq = fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO)) == NULL){
2617 n_err(_("*signature* expands to invalid file: %s\n"),
2618 n_shexp_quote_cp(cp, FAL0));
2619 goto jerr;
2621 cpq = n_shexp_quote_cp(cp = cpq, FAL0);
2623 if((sigfp = Fopen(cp, "r")) == NULL){
2624 n_err(_("Can't open *signature* %s: %s\n"),
2625 cpq, n_err_to_doc(n_err_no));
2626 goto jerr;
2629 if(linebuf == NULL)
2630 linebuf = smalloc(linesize = LINESIZE);
2631 c = '\0';
2632 while((i = fread(linebuf, sizeof *linebuf, linesize, n_UNVOLATILE(sigfp)))
2633 > 0){
2634 c = linebuf[i - 1];
2635 if(i != fwrite(linebuf, sizeof *linebuf, i, _coll_fp))
2636 goto jerr;
2639 /* C99 */{
2640 FILE *x = n_UNVOLATILE(sigfp);
2641 int e = n_err_no, ise = ferror(x);
2643 sigfp = NULL;
2644 Fclose(x);
2646 if(ise){
2647 n_err(_("Errors while reading *signature* %s: %s\n"),
2648 cpq, n_err_to_doc(e));
2649 goto jerr;
2653 if(c != '\0' && c != '\n')
2654 putc('\n', _coll_fp);
2657 { char const *cp_obsolete = ok_vlook(NAIL_TAIL);
2659 if(cp_obsolete != NULL)
2660 n_OBSOLETE(_("please use *message-inject-tail*, not *NAIL_TAIL*"));
2662 if((cp = ok_vlook(message_inject_tail)) != NULL ||
2663 (cp = cp_obsolete) != NULL){
2664 if(putesc(cp, _coll_fp) < 0)
2665 goto jerr;
2666 if((n_psonce & n_PSO_INTERACTIVE) && putesc(cp, n_stdout) < 0)
2667 goto jerr;
2671 jskiptails:
2672 if(fflush(_coll_fp))
2673 goto jerr;
2674 rewind(_coll_fp);
2676 jleave:
2677 if (linebuf != NULL)
2678 free(linebuf);
2679 sigprocmask(SIG_BLOCK, &nset, NULL);
2680 n_pstate &= ~n_PS_COMPOSE_MODE;
2681 safe_signal(SIGINT, _coll_saveint);
2682 safe_signal(SIGHUP, _coll_savehup);
2683 sigprocmask(SIG_SETMASK, &oset, NULL);
2684 NYD_LEAVE;
2685 return _coll_fp;
2687 jerr:
2688 hold_all_sigs();
2690 if(coap != NULL && coap != (struct a_coll_ocs_arg*)-1){
2691 if(!(flags & a_COAP_NOSIGTERM))
2692 n_psignal(coap->coa_stdout, SIGTERM);
2693 n_go_splice_hack_remove_after_jump();
2694 coap = NULL;
2696 if(ifs_saved != NULL){
2697 ok_vset(ifs, ifs_saved);
2698 ifs_saved = NULL;
2700 if(sigfp != NULL){
2701 Fclose(n_UNVOLATILE(sigfp));
2702 sigfp = NULL;
2704 if(_coll_fp != NULL){
2705 Fclose(_coll_fp);
2706 _coll_fp = NULL;
2709 rele_all_sigs();
2711 assert(checkaddr_err != NULL);
2712 /* TODO We don't save in $DEAD upon error because msg not readily composed?
2713 * TODO But this no good, it should go ZOMBIE / DRAFT / POSTPONED or what! */
2714 if(*checkaddr_err != 0){
2715 if(*checkaddr_err == 111)
2716 n_err(_("Compose mode splice hook failure\n"));
2717 else
2718 n_err(_("Some addressees were classified as \"hard error\"\n"));
2719 }else if(_coll_hadintr == 0){
2720 *checkaddr_err = TRU1; /* TODO ugly: "sendout_error" now.. */
2721 n_err(_("Failed to prepare composed message (I/O error, disk full?)\n"));
2723 goto jleave;
2725 #undef a_HARDERR
2728 /* s-it-mode */