FIX [1e5697d6] as of -12-22!..
[s-mailx.git] / collect.c
blob52f3a28501998e1a9c2cafe30e7a31824220e980
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 - 2016 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_ocds_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_ocds__macname; /* *on-compose-done* */
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 /* */
69 static int _include_file(char const *name, int *linecount,
70 int *charcount, bool_t indent);
72 /* Execute cmd and insert its standard output into fp */
73 static void insertcommand(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 int exwrite(char const *name, FILE *fp, int f);
81 /* Parse off the message header from fp and store relevant fields in hp,
82 * replace _coll_fp with a shiny new version without any header */
83 static enum okay makeheader(FILE *fp, struct header *hp, si8_t *checkaddr_err);
85 /* Edit the message being collected on fp. On return, make the edit file the
86 * new temp file */
87 static void mesedit(int c, struct header *hp);
89 /* Pipe the message through the command. Old message is on stdin of command,
90 * new message collected from stdout. Shell must return 0 to accept new msg */
91 static void mespipe(char *cmd);
93 /* Interpolate the named messages into the current message, possibly doing
94 * indent stuff. The flag argument is one of the command escapes: [mMfFuU].
95 * Return a count of the number of characters now in the message, or -1 if an
96 * error is encountered writing the message temporary */
97 static int forward(char *ms, FILE *fp, int f);
99 /* ~^ mode */
100 static bool_t a_collect_plumbing(char const *ms, struct header *p);
102 /* On interrupt, come here to save the partial message in ~/dead.letter.
103 * Then jump out of the collection loop */
104 static void _collint(int s);
106 static void collhup(int s);
108 static int putesc(char const *s, FILE *stream); /* TODO wysh set! */
110 /* temporary_call_compose_mode_hook() setter hook */
111 static void a_coll__hook_setter(void *arg);
113 /* *on-compose-done* driver and *on-compose-done(-shell)?* finalizer */
114 static int a_coll_ocds__mac(void);
115 static void a_coll_ocds__finalize(void *vp);
117 static void
118 _execute_command(struct header *hp, char const *linebuf, size_t linesize){
119 /* The problem arises if there are rfc822 message attachments and the
120 * user uses `~:' to change the current file. TODO Unfortunately we
121 * TODO cannot simply keep a pointer to, or increment a reference count
122 * TODO of the current `file' (mailbox that is) object, because the
123 * TODO codebase doesn't deal with that at all; so, until some far
124 * TODO later time, copy the name of the path, and warn the user if it
125 * TODO changed; we COULD use the AC_TMPFILE attachment type, i.e.,
126 * TODO copy the message attachments over to temporary files, but that
127 * TODO would require more changes so that the user still can recognize
128 * TODO in `~@' etc. that its a rfc822 message attachment; see below */
129 struct n_sigman sm;
130 struct attachment *ap;
131 char * volatile mnbuf;
132 NYD_ENTER;
134 n_UNUSED(linesize);
135 mnbuf = NULL;
137 n_SIGMAN_ENTER_SWITCH(&sm, n_SIGMAN_ALL){
138 case 0:
139 break;
140 default:
141 goto jleave;
144 /* If the above todo is worked, remove or outsource to attachment.c! */
145 if(hp != NULL && (ap = hp->h_attach) != NULL) do
146 if(ap->a_msgno){
147 mnbuf = sstrdup(mailname);
148 break;
150 while((ap = ap->a_flink) != NULL);
152 n_source_command(n_LEXINPUT_CTX_COMPOSE, linebuf);
154 n_sigman_cleanup_ping(&sm);
155 jleave:
156 if(mnbuf != NULL){
157 if(strcmp(mnbuf, mailname))
158 n_err(_("Mailbox changed: it is likely that existing "
159 "rfc822 attachments became invalid!\n"));
160 free(mnbuf);
162 NYD_LEAVE;
163 n_sigman_leave(&sm, n_SIGMAN_VIPSIGS_NTTYOUT);
166 static int
167 _include_file(char const *name, int *linecount, int *charcount, bool_t indent)
169 FILE *fbuf;
170 char const *indb;
171 int ret = -1;
172 char *linebuf = NULL; /* TODO line pool */
173 size_t linesize = 0, indl, linelen, cnt;
174 NYD_ENTER;
176 /* The -M case is special */
177 if (name == (char*)-1)
178 fbuf = stdin;
179 else if ((fbuf = Fopen(name, "r")) == NULL) {
180 n_perr(name, 0);
181 goto jleave;
184 if (!indent)
185 indb = NULL, indl = 0;
186 else {
187 if ((indb = ok_vlook(indentprefix)) == NULL)
188 indb = INDENT_DEFAULT;
189 indl = strlen(indb);
192 *linecount = *charcount = 0;
193 cnt = fsize(fbuf);
194 while (fgetline(&linebuf, &linesize, &cnt, &linelen, fbuf, 0) != NULL) {
195 if (indl > 0 && fwrite(indb, sizeof *indb, indl, _coll_fp) != indl)
196 goto jleave;
197 if (fwrite(linebuf, sizeof *linebuf, linelen, _coll_fp) != linelen)
198 goto jleave;
199 ++(*linecount);
200 (*charcount) += linelen + indl;
202 if (fflush(_coll_fp))
203 goto jleave;
205 ret = 0;
206 jleave:
207 if (linebuf != NULL)
208 free(linebuf);
209 if (fbuf != NULL && fbuf != stdin)
210 Fclose(fbuf);
211 NYD_LEAVE;
212 return ret;
215 static void
216 insertcommand(FILE *fp, char const *cmd)
218 FILE *ibuf = NULL;
219 int c;
220 NYD_ENTER;
222 if ((ibuf = Popen(cmd, "r", ok_vlook(SHELL), NULL, 0)) != NULL) {
223 while ((c = getc(ibuf)) != EOF) /* XXX bytewise, yuck! */
224 putc(c, fp);
225 Pclose(ibuf, TRU1);
226 } else
227 n_perr(cmd, 0);
228 NYD_LEAVE;
231 static void
232 print_collf(FILE *cf, struct header *hp)
234 char *lbuf;
235 FILE *obuf;
236 size_t cnt, linesize, linelen;
237 NYD_ENTER;
239 fflush_rewind(cf);
240 cnt = (size_t)fsize(cf);
242 if((obuf = Ftmp(NULL, "collfp", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL){
243 n_perr(_("Can't create temporary file for `~p' command"), 0);
244 goto jleave;
247 hold_all_sigs();
249 fprintf(obuf, _("-------\nMessage contains:\n"));
250 puthead(TRU1, hp, obuf,
251 (GIDENT | GTO | GSUBJECT | GCC | GBCC | GNL | GFILES | GCOMMA),
252 SEND_TODISP, CONV_NONE, NULL, NULL);
254 lbuf = NULL;
255 linesize = 0;
256 while(fgetline(&lbuf, &linesize, &cnt, &linelen, cf, 1))
257 prout(lbuf, linelen, obuf);
258 if(lbuf != NULL)
259 free(lbuf);
261 if(hp->h_attach != NULL){
262 fputs(_("-------\nAttachments:\n"), obuf);
263 n_attachment_list_print(hp->h_attach, obuf);
266 rele_all_sigs();
268 page_or_print(obuf, 0);
269 jleave:
270 Fclose(obuf);
271 NYD_LEAVE;
274 static int
275 exwrite(char const *name, FILE *fp, int f)
277 FILE *of;
278 int c, rv;
279 long lc, cc;
280 NYD_ENTER;
282 if (f) {
283 printf("%s ", n_shexp_quote_cp(name, FAL0));
284 fflush(stdout);
287 if ((of = Fopen(name, "a")) == NULL) {
288 n_perr(name, 0);
289 goto jerr;
292 lc = cc = 0;
293 while ((c = getc(fp)) != EOF) {
294 ++cc;
295 if (c == '\n')
296 ++lc;
297 if (putc(c, of) == EOF) {
298 n_perr(name, 0);
299 goto jerr;
302 printf(_("%ld/%ld\n"), lc, cc);
304 rv = 0;
305 jleave:
306 if(of != NULL)
307 Fclose(of);
308 fflush(stdout);
309 NYD_LEAVE;
310 return rv;
311 jerr:
312 putchar('-');
313 putchar('\n');
314 rv = -1;
315 goto jleave;
318 static enum okay
319 makeheader(FILE *fp, struct header *hp, si8_t *checkaddr_err)
321 FILE *nf;
322 int c;
323 enum okay rv = STOP;
324 NYD_ENTER;
326 if ((nf = Ftmp(NULL, "colhead", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==NULL) {
327 n_perr(_("temporary mail edit file"), 0);
328 goto jleave;
331 extract_header(fp, hp, checkaddr_err);
332 if (checkaddr_err != NULL && *checkaddr_err != 0)
333 goto jleave;
335 while ((c = getc(fp)) != EOF) /* XXX bytewise, yuck! */
336 putc(c, nf);
337 if (fp != _coll_fp)
338 Fclose(_coll_fp);
339 Fclose(fp);
340 _coll_fp = nf;
341 if (check_from_and_sender(hp->h_from, hp->h_sender) == NULL)
342 goto jleave;
343 rv = OKAY;
344 jleave:
345 NYD_LEAVE;
346 return rv;
349 static void
350 mesedit(int c, struct header *hp)
352 bool_t saved;
353 sighandler_type sigint;
354 FILE *nf;
355 NYD_ENTER;
357 if(!(saved = ok_blook(add_file_recipients)))
358 ok_bset(add_file_recipients);
360 sigint = safe_signal(SIGINT, SIG_IGN);
361 nf = run_editor(_coll_fp, (off_t)-1, c, FAL0, hp, NULL, SEND_MBOX, sigint);
362 if (nf != NULL) {
363 if (hp) {
364 rewind(nf);
365 makeheader(nf, hp, NULL);
366 } else {
367 fseek(nf, 0L, SEEK_END);
368 Fclose(_coll_fp);
369 _coll_fp = nf;
372 safe_signal(SIGINT, sigint);
374 if(!saved)
375 ok_bclear(add_file_recipients);
376 NYD_LEAVE;
379 static void
380 mespipe(char *cmd)
382 FILE *nf;
383 sighandler_type sigint;
384 NYD_ENTER;
386 sigint = safe_signal(SIGINT, SIG_IGN);
388 if ((nf = Ftmp(NULL, "colpipe", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==NULL) {
389 n_perr(_("temporary mail edit file"), 0);
390 goto jout;
393 /* stdin = current message. stdout = new message */
394 fflush(_coll_fp);
395 if (run_command(ok_vlook(SHELL), 0, fileno(_coll_fp), fileno(nf), "-c",
396 cmd, NULL, NULL) < 0) {
397 Fclose(nf);
398 goto jout;
401 if (fsize(nf) == 0) {
402 n_err(_("No bytes from %s !?\n"), n_shexp_quote_cp(cmd, FAL0));
403 Fclose(nf);
404 goto jout;
407 /* Take new files */
408 fseek(nf, 0L, SEEK_END);
409 Fclose(_coll_fp);
410 _coll_fp = nf;
411 jout:
412 safe_signal(SIGINT, sigint);
413 NYD_LEAVE;
416 static int
417 forward(char *ms, FILE *fp, int f)
419 int *msgvec, rv = 0;
420 struct n_ignore const *itp;
421 char const *tabst;
422 enum sendaction action;
423 NYD_ENTER;
425 msgvec = salloc((size_t)(msgCount + 1) * sizeof *msgvec);
426 if (getmsglist(ms, msgvec, 0) < 0)
427 goto jleave;
428 if (*msgvec == 0) {
429 *msgvec = first(0, MMNORM);
430 if (*msgvec == 0) {
431 n_err(_("No appropriate messages\n"));
432 goto jleave;
434 msgvec[1] = 0;
437 if (f == 'f' || f == 'F' || f == 'u')
438 tabst = NULL;
439 else if ((tabst = ok_vlook(indentprefix)) == NULL)
440 tabst = INDENT_DEFAULT;
441 if (f == 'u' || f == 'U')
442 itp = n_IGNORE_ALL;
443 else
444 itp = upperchar(f) ? NULL : n_IGNORE_TYPE;
445 action = (upperchar(f) && f != 'U') ? SEND_QUOTE_ALL : SEND_QUOTE;
447 printf(_("Interpolating:"));
448 srelax_hold();
449 for (; *msgvec != 0; ++msgvec) {
450 struct message *mp = message + *msgvec - 1;
452 touch(mp);
453 printf(" %d", *msgvec);
454 fflush(stdout);
455 if (sendmp(mp, fp, itp, tabst, action, NULL) < 0) {
456 n_perr(_("temporary mail file"), 0);
457 rv = -1;
458 break;
460 srelax();
462 srelax_rele();
463 printf("\n");
464 jleave:
465 NYD_LEAVE;
466 return rv;
469 static bool_t
470 a_collect_plumbing(char const *ms, struct header *hp){
471 /* TODO _collect_plumbing: instead of fields the basic headers should
472 * TODO be in an array and have IDs, like in termcap etc., so then this
473 * TODO could be simplified as table-walks. Also true for arg-checks! */
474 char const *cp, *cmd[4];
475 NYD2_ENTER;
477 /* Protcol version for *on-compose-done-shell* -- update manual on change! */
478 #define a_COLL_PLUMBING_VERSION "0 0 1"
479 cp = ms;
481 /* C99 */{
482 size_t i;
484 for(i = 0; i < n_NELEM(cmd); ++i){
485 while(blankchar(*cp))
486 ++cp;
487 if(*cp == '\0')
488 cmd[i] = NULL;
489 else{
490 if(i < n_NELEM(cmd) - 1)
491 for(cmd[i] = cp++; *cp != '\0' && !blankchar(*cp); ++cp)
493 else{
494 /* Last slot takes all the rest of the line, less trailing WS */
495 for(cmd[i] = cp++; *cp != '\0'; ++cp)
497 while(blankchar(cp[-1]))
498 --cp;
500 cmd[i] = savestrbuf(cmd[i], PTR2SIZE(cp - cmd[i]));
505 if(n_UNLIKELY(cmd[0] == NULL))
506 goto jecmd;
507 if(is_asccaseprefix(cmd[0], "header")){
508 struct n_header_field *hfp;
509 struct name *np, **npp;
511 if(cmd[1] == NULL || is_asccaseprefix(cmd[1], "list")){
512 if(cmd[2] == NULL){
513 fputs("210", stdout);
514 if(hp->h_from != NULL) fputs(" From", stdout);
515 if(hp->h_sender != NULL) fputs(" Sender", stdout);
516 if(hp->h_to != NULL) fputs(" To", stdout);
517 if(hp->h_cc != NULL) fputs(" Cc", stdout);
518 if(hp->h_bcc != NULL) fputs(" Bcc", stdout);
519 if(hp->h_subject != NULL) fputs(" Subject", stdout);
520 if(hp->h_replyto != NULL) fputs(" Reply-To", stdout);
521 if(hp->h_mft != NULL) fputs(" Mail-Followup-To", stdout);
522 if(hp->h_message_id != NULL) fputs(" Message-ID", stdout);
523 if(hp->h_ref != NULL) fputs(" References", stdout);
524 if(hp->h_in_reply_to != NULL) fputs(" In-Reply-To", stdout);
525 for(hfp = hp->h_user_headers; hfp != NULL; hfp = hfp->hf_next){
526 putc(' ', stdout);
527 fputs(&hfp->hf_dat[0], stdout);
529 putc('\n', stdout);
530 }else{
531 if(cmd[3] != NULL)
532 goto jecmd;
533 if(!asccasecmp(cmd[2], "from")){
534 np = hp->h_from;
535 jlist:
536 fprintf(stdout, "%s %s\n", (np == NULL ? "501" : "210"), cp);
537 }else if(!asccasecmp(cmd[2], cp = "Sender")){
538 np = hp->h_sender;
539 goto jlist;
540 }else if(!asccasecmp(cmd[2], cp = "To")){
541 np = hp->h_to;
542 goto jlist;
543 }else if(!asccasecmp(cmd[2], cp = "Cc")){
544 np = hp->h_cc;
545 goto jlist;
546 }else if(!asccasecmp(cmd[2], cp = "Bcc")){
547 np = hp->h_bcc;
548 goto jlist;
549 }else if(!asccasecmp(cmd[2], cp = "Subject")){
550 np = (struct name*)-1;
551 goto jlist;
552 }else if(!asccasecmp(cmd[2], cp = "Reply-To")){
553 np = hp->h_replyto;
554 goto jlist;
555 }else if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
556 np = hp->h_mft;
557 goto jlist;
558 }else if(!asccasecmp(cmd[2], cp = "Message-ID")){
559 np = hp->h_message_id;
560 goto jlist;
561 }else if(!asccasecmp(cmd[2], cp = "References")){
562 np = hp->h_ref;
563 goto jlist;
564 }else if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
565 np = hp->h_in_reply_to;
566 goto jlist;
567 }else{
568 /* Primitive name normalization XXX header object should
569 * XXX have a more sophisticated accessible one */
570 char *xp;
572 cp = xp = savestr(cmd[2]);
573 xp[0] = upperchar(xp[0]);
574 while(*++xp != '\0')
575 xp[0] = lowerchar(xp[0]);
577 for(hfp = hp->h_user_headers;; hfp = hfp->hf_next){
578 if(hfp == NULL)
579 goto j501cp;
580 else if(!asccasecmp(cp, &hfp->hf_dat[0])){
581 fprintf(stdout, "210 %s\n", cp);
582 break;
587 }else if(is_asccaseprefix(cmd[1], "show")){
588 if(cmd[2] == NULL || cmd[3] != NULL)
589 goto jecmd;
590 if(!asccasecmp(cmd[2], "from")){
591 np = hp->h_from;
592 jshow:
593 if(np != NULL){
594 fprintf(stdout, "211 %s\n", cp);
596 fprintf(stdout, "%s %s\n", np->n_name, np->n_fullname);
597 while((np = np->n_flink) != NULL);
598 putc('\n', stdout);
599 }else
600 goto j501cp;
601 }else if(!asccasecmp(cmd[2], cp = "Sender")){
602 np = hp->h_sender;
603 goto jshow;
604 }else if(!asccasecmp(cmd[2], cp = "To")){
605 np = hp->h_to;
606 goto jshow;
607 }else if(!asccasecmp(cmd[2], cp = "Cc")){
608 np = hp->h_cc;
609 goto jshow;
610 }else if(!asccasecmp(cmd[2], cp = "Bcc")){
611 np = hp->h_bcc;
612 goto jshow;
613 }else if(!asccasecmp(cmd[2], cp = "Reply-To")){
614 np = hp->h_replyto;
615 goto jshow;
616 }else if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
617 np = hp->h_mft;
618 goto jshow;
619 }else if(!asccasecmp(cmd[2], cp = "Message-ID")){
620 np = hp->h_message_id;
621 goto jshow;
622 }else if(!asccasecmp(cmd[2], cp = "References")){
623 np = hp->h_ref;
624 goto jshow;
625 }else if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
626 np = hp->h_in_reply_to;
627 goto jshow;
628 }else if(!asccasecmp(cmd[2], cp = "Subject")){
629 if(hp->h_subject != NULL)
630 fprintf(stdout, "212 %s\n%s\n\n", cp, hp->h_subject);
631 else
632 fprintf(stdout, "501 %s\n", cp);
633 }else{
634 /* Primitive name normalization XXX header object should
635 * XXX have a more sophisticated accessible one */
636 bool_t any;
637 char *xp;
639 cp = xp = savestr(cmd[2]);
640 xp[0] = upperchar(xp[0]);
641 while(*++xp != '\0')
642 xp[0] = lowerchar(xp[0]);
644 for(any = FAL0, hfp = hp->h_user_headers; hfp != NULL;
645 hfp = hfp->hf_next){
646 if(!asccasecmp(cp, &hfp->hf_dat[0])){
647 if(!any)
648 fprintf(stdout, "212 %s\n", cp);
649 any = TRU1;
650 fprintf(stdout, "%s\n", &hfp->hf_dat[hfp->hf_nl +1]);
653 if(any)
654 putc('\n', stdout);
655 else
656 goto j501cp;
658 }else if(is_asccaseprefix(cmd[1], "remove")){
659 if(cmd[2] == NULL || cmd[3] != NULL)
660 goto jecmd;
661 if(!asccasecmp(cmd[2], "from")){
662 npp = &hp->h_from;
663 jrem:
664 if(*npp != NULL){
665 *npp = NULL;
666 fprintf(stdout, "210 %s\n", cp);
667 }else
668 goto j501cp;
669 }else if(!asccasecmp(cmd[2], cp = "Sender")){
670 npp = &hp->h_sender;
671 goto jrem;
672 }else if(!asccasecmp(cmd[2], cp = "To")){
673 npp = &hp->h_to;
674 goto jrem;
675 }else if(!asccasecmp(cmd[2], cp = "Cc")){
676 npp = &hp->h_cc;
677 goto jrem;
678 }else if(!asccasecmp(cmd[2], cp = "Bcc")){
679 npp = &hp->h_bcc;
680 goto jrem;
681 }else if(!asccasecmp(cmd[2], cp = "Reply-To")){
682 npp = &hp->h_replyto;
683 goto jrem;
684 }else if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
685 npp = &hp->h_mft;
686 goto jrem;
687 }else if(!asccasecmp(cmd[2], cp = "Message-ID")){
688 npp = &hp->h_message_id;
689 goto jrem;
690 }else if(!asccasecmp(cmd[2], cp = "References")){
691 npp = &hp->h_ref;
692 goto jrem;
693 }else if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
694 npp = &hp->h_in_reply_to;
695 goto jrem;
696 }else if(!asccasecmp(cmd[2], cp = "Subject")){
697 if(hp->h_subject != NULL){
698 hp->h_subject = NULL;
699 fprintf(stdout, "210 %s\n", cp);
700 }else
701 goto j501cp;
702 }else{
703 /* Primitive name normalization XXX header object should
704 * XXX have a more sophisticated accessible one */
705 struct n_header_field **hfpp;
706 bool_t any;
707 char *xp;
709 cp = xp = savestr(cmd[2]);
710 xp[0] = upperchar(xp[0]);
711 while(*++xp != '\0')
712 xp[0] = lowerchar(xp[0]);
714 for(any = FAL0, hfpp = &hp->h_user_headers; (hfp = *hfpp) != NULL;){
715 if(!asccasecmp(cp, &hfp->hf_dat[0])){
716 *hfpp = hfp->hf_next;
717 if(!any)
718 fprintf(stdout, "210 %s\n", cp);
719 any = TRU1;
720 }else
721 hfp = *(hfpp = &hfp->hf_next);
723 if(!any)
724 goto j501cp;
726 }else if(is_asccaseprefix(cmd[1], "insert")){ /* TODO LOGIC BELONGS head.c
727 * TODO That is: Header::factory(string) -> object (blahblah).
728 * TODO I.e., as long as we don't have regular RFC compliant parsers
729 * TODO which differentiate in between structured and unstructured
730 * TODO header fields etc., a little workaround */
731 si8_t aerr;
732 enum expand_addr_check_mode eacm;
733 enum gfield ntype;
734 bool_t mult_ok;
736 /* Strip [\r\n] which would render a body invalid XXX all controls? */
737 /* C99 */{
738 char *xp, c;
740 cmd[3] = xp = savestr(cmd[3]);
741 for(; (c = *xp) != '\0'; ++xp)
742 if(c == '\n' || c == '\r')
743 *xp = ' ';
746 mult_ok = TRU1;
747 ntype = GEXTRA | GFULL | GFULLEXTRA;
748 eacm = EACM_STRICT;
750 if(cmd[2] == NULL || cmd[3] == NULL)
751 goto jecmd;
752 if(!asccasecmp(cmd[2], "from")){
753 npp = &hp->h_from;
754 jins:
755 aerr = 0;
756 if((np = lextract(cmd[3], ntype)) == NULL)
757 goto j501cp;
758 else if((np = checkaddrs(np, eacm, &aerr), aerr != 0))
759 fprintf(stdout, "505 %s\n", cp);
760 else if(!mult_ok && (np->n_flink != NULL || *npp != NULL))
761 fprintf(stdout, "506 %s\n", cp);
762 else{
763 *npp = cat(*npp, np);
764 fprintf(stdout, "210 %s\n", cp);
766 }else if(!asccasecmp(cmd[2], cp = "Sender")){
767 mult_ok = FAL0;
768 npp = &hp->h_sender;
769 goto jins;
770 }else if(!asccasecmp(cmd[2], cp = "To")){
771 npp = &hp->h_to;
772 ntype = GTO | GFULL;
773 eacm = EACM_NORMAL | EAF_NAME;
774 goto jins;
775 }else if(!asccasecmp(cmd[2], cp = "Cc")){
776 npp = &hp->h_cc;
777 ntype = GCC | GFULL;
778 eacm = EACM_NORMAL | EAF_NAME;
779 goto jins;
780 }else if(!asccasecmp(cmd[2], cp = "Bcc")){
781 npp = &hp->h_bcc;
782 ntype = GCC | GFULL;
783 eacm = EACM_NORMAL | EAF_NAME;
784 goto jins;
785 }else if(!asccasecmp(cmd[2], cp = "Reply-To")){
786 npp = &hp->h_replyto;
787 goto jins;
788 }else if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
789 npp = &hp->h_mft;
790 eacm = EACM_NONAME;
791 goto jins;
792 }else if(!asccasecmp(cmd[2], cp = "Message-ID")){
793 mult_ok = FAL0;
794 npp = &hp->h_message_id;
795 eacm = EACM_NONAME;
796 goto jins;
797 }else if(!asccasecmp(cmd[2], cp = "References")){
798 npp = &hp->h_ref;
799 ntype = GREF;
800 eacm = EACM_NONAME;
801 goto jins;
802 }else if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
803 npp = &hp->h_in_reply_to;
804 ntype = GREF;
805 eacm = EACM_NONAME;
806 goto jins;
807 }else if(!asccasecmp(cmd[2], cp = "Subject")){
808 if(cmd[3][0] != '\0'){
809 if(hp->h_subject != NULL)
810 hp->h_subject = savecatsep(hp->h_subject, ' ', cmd[3]);
811 else
812 hp->h_subject = n_UNCONST(cmd[3]);
813 fprintf(stdout, "210 %s\n", cp);
814 }else
815 goto j501cp;
816 }else{
817 /* Primitive name normalization XXX header object should
818 * XXX have a more sophisticated accessible one */
819 size_t nl, bl;
820 struct n_header_field **hfpp;
822 for(cp = cmd[2]; *cp != '\0'; ++cp)
823 if(!fieldnamechar(*cp)){
824 cp = cmd[2];
825 goto j501cp;
828 for(hfpp = &hp->h_user_headers; *hfpp != NULL;)
829 hfpp = &(*hfpp)->hf_next;
831 nl = strlen(cp = cmd[2]);
832 bl = strlen(cmd[3]) +1;
833 *hfpp = hfp = salloc(n_VSTRUCT_SIZEOF(struct n_header_field, hf_dat
834 ) + nl +1 + bl);
835 hfp->hf_next = NULL;
836 hfp->hf_nl = nl;
837 hfp->hf_bl = bl - 1;
838 memcpy(hfp->hf_dat, cp, nl);
839 hfp->hf_dat[nl++] = '\0';
840 memcpy(hfp->hf_dat + nl, cmd[3], bl);
841 fprintf(stdout, "210 %s\n", cp);
843 }else
844 goto jecmd;
845 }else if(is_asccaseprefix(cmd[0], "attachment")){
846 bool_t status;
847 struct attachment *ap;
849 if(cmd[1] == NULL || is_asccaseprefix(cmd[1], "list")){
850 if(cmd[2] != NULL)
851 goto jecmd;
852 if((ap = hp->h_attach) != NULL){
853 fputs("212\n", stdout);
855 fprintf(stdout, "%s\n", ap->a_path_user);
856 while((ap = ap->a_flink) != NULL);
857 putc('\n', stdout);
858 }else
859 fputs("501\n", stdout);
860 }else if(is_asccaseprefix(cmd[1], "remove")){
861 if(cmd[2] == NULL)
862 goto jecmd;
863 if((ap = n_attachment_find(hp->h_attach, cmd[2], &status)) != NULL){
864 if(status == TRUM1)
865 fputs("506\n", stdout);
866 else{
867 hp->h_attach = n_attachment_remove(hp->h_attach, ap);
868 fprintf(stdout, "210 %s\n", cmd[2]);
870 }else
871 fputs("501\n", stdout);
872 }else if(is_asccaseprefix(cmd[1], "remove-at")){
873 char *eptr;
874 long l;
876 if(cmd[2] == NULL)
877 goto jecmd;
878 if((l = strtol(cmd[2], &eptr, 0)) <= 0 || *eptr != '\0')
879 fputs("505\n", stdout);
880 else{
881 for(ap = hp->h_attach; ap != NULL && --l != 0; ap = ap->a_flink)
883 if(ap != NULL){
884 hp->h_attach = n_attachment_remove(hp->h_attach, ap);
885 fprintf(stdout, "210 %s\n", cmd[2]);
886 }else
887 fputs("501\n", stdout);
889 }else if(is_asccaseprefix(cmd[1], "insert")){
890 enum n_attach_error aerr;
892 if(cmd[2] == NULL)
893 goto jecmd;
894 hp->h_attach = n_attachment_append(hp->h_attach, cmd[2], &aerr, &ap);
895 switch(aerr){
896 case n_ATTACH_ERR_FILE_OPEN: cp = "505\n"; goto jatt_ins;
897 case n_ATTACH_ERR_ICONV_FAILED: cp = "506\n"; goto jatt_ins;
898 case n_ATTACH_ERR_ICONV_NAVAIL:
899 case n_ATTACH_ERR_OTHER:
900 default:
901 cp = "501\n";
902 jatt_ins:
903 fputs(cp, stdout);
904 break;
905 case n_ATTACH_ERR_NONE:{
906 size_t i;
908 for(i = 0; ap != NULL; ++i, ap = ap->a_blink)
910 fprintf(stdout, "210 %" PRIuZ "\n", i);
911 } break;
913 }else if(is_asccaseprefix(cmd[1], "attribute")){
914 if(cmd[2] == NULL)
915 goto jecmd;
916 if((ap = n_attachment_find(hp->h_attach, cmd[2], NULL)) != NULL){
917 jatt_att:
918 fprintf(stdout, "212 %s\n", cmd[2]);
919 if(ap->a_msgno > 0)
920 fprintf(stdout, "message-number %d\n\n", ap->a_msgno);
921 else{
922 fprintf(stdout, "creation-name %s\nopen-path %s\nfilename %s\n",
923 ap->a_path_user, ap->a_path, ap->a_name);
924 if(ap->a_content_description != NULL)
925 fprintf(stdout, "content-description %s\n",
926 ap->a_content_description);
927 if(ap->a_content_id != NULL)
928 fprintf(stdout, "content-id %s\n", ap->a_content_id);
929 if(ap->a_content_type != NULL)
930 fprintf(stdout, "content-type %s\n", ap->a_content_type);
931 if(ap->a_content_disposition != NULL)
932 fprintf(stdout, "content-disposition %s\n",
933 ap->a_content_disposition);
934 putc('\n', stdout);
936 }else
937 fputs("501\n", stdout);
938 }else if(is_asccaseprefix(cmd[1], "attribute-at")){
939 char *eptr;
940 long l;
942 if(cmd[2] == NULL)
943 goto jecmd;
944 if((l = strtol(cmd[2], &eptr, 0)) <= 0 || *eptr != '\0')
945 fputs("505\n", stdout);
946 else{
947 for(ap = hp->h_attach; ap != NULL && --l != 0; ap = ap->a_flink)
949 if(ap != NULL)
950 goto jatt_att;
951 else
952 fputs("501\n", stdout);
954 }else if(is_asccaseprefix(cmd[1], "attribute-set")){
955 if(cmd[2] == NULL || cmd[3] == NULL)
956 goto jecmd;
957 if((ap = n_attachment_find(hp->h_attach, cmd[2], NULL)) != NULL){
958 jatt_attset:
959 if(ap->a_msgno > 0)
960 fputs("505\n", stdout);
961 else{
962 char c, *keyw;
964 cp = cmd[3];
965 while((c = *cp) != '\0' && !blankchar(c))
966 ++cp;
967 keyw = savestrbuf(cmd[3], PTR2SIZE(cp - cmd[3]));
968 if(c != '\0'){
969 for(; (c = *++cp) != '\0' && blankchar(c);)
971 if(c != '\0'){
972 char *xp;
974 /* Strip [\r\n] which would render a parameter invalid XXX
975 * XXX all controls? */
976 cp = xp = savestr(cp);
977 for(; (c = *xp) != '\0'; ++xp)
978 if(c == '\n' || c == '\r')
979 *xp = ' ';
983 if(!asccasecmp(keyw, "filename"))
984 ap->a_name = (c == '\0') ? ap->a_path_bname : cp;
985 else if(!asccasecmp(keyw, "content-description"))
986 ap->a_content_description = (c == '\0') ? NULL : cp;
987 else if(!asccasecmp(keyw, "content-id"))
988 ap->a_content_id = (c == '\0') ? NULL : cp;
989 else if(!asccasecmp(keyw, "content-type"))
990 ap->a_content_type = (c == '\0') ? NULL : cp;
991 else if(!asccasecmp(keyw, "content-disposition"))
992 ap->a_content_disposition = (c == '\0') ? NULL : cp;
993 else
994 cp = NULL;
996 if(cp != NULL){
997 size_t i;
999 for(i = 0; ap != NULL; ++i, ap = ap->a_blink)
1001 fprintf(stdout, "210 %" PRIuZ "\n", i);
1002 }else
1003 fputs("505\n", stdout);
1005 }else
1006 fputs("501\n", stdout);
1007 }else if(is_asccaseprefix(cmd[1], "attribute-set-at")){
1008 char *eptr;
1009 long l;
1011 if(cmd[2] == NULL || cmd[3] == NULL)
1012 goto jecmd;
1013 if((l = strtol(cmd[2], &eptr, 0)) <= 0 || *eptr != '\0')
1014 fputs("505\n", stdout);
1015 else{
1016 for(ap = hp->h_attach; ap != NULL && --l != 0; ap = ap->a_flink)
1018 if(ap != NULL)
1019 goto jatt_attset;
1020 else
1021 fputs("501\n", stdout);
1023 }else
1024 goto jecmd;
1025 }else{
1026 jecmd:
1027 fputs("500\n", stdout);
1028 ms = NULL;
1031 jleave:
1032 fflush(stdout);
1033 NYD2_LEAVE;
1034 return (ms != NULL);
1036 j501cp:
1037 fputs("501 ", stdout);
1038 fputs(cp, stdout);
1039 putc('\n', stdout);
1040 goto jleave;
1043 static void
1044 _collint(int s)
1046 NYD_X; /* Signal handler */
1048 /* the control flow is subtle, because we can be called from ~q */
1049 if (_coll_hadintr == 0) {
1050 if (ok_blook(ignore)) {
1051 fputs("@\n", stdout);
1052 fflush(stdout);
1053 clearerr(stdin);
1054 } else
1055 _coll_hadintr = 1;
1056 siglongjmp(_coll_jmp, 1);
1058 exit_status |= EXIT_SEND_ERROR;
1059 if (s != 0)
1060 savedeadletter(_coll_fp, TRU1);
1061 /* Aborting message, no need to fflush() .. */
1062 siglongjmp(_coll_abort, 1);
1065 static void
1066 collhup(int s)
1068 NYD_X; /* Signal handler */
1069 n_UNUSED(s);
1071 savedeadletter(_coll_fp, TRU1);
1072 /* Let's pretend nobody else wants to clean up, a true statement at
1073 * this time */
1074 exit(EXIT_ERR);
1077 static int
1078 putesc(char const *s, FILE *stream)
1080 int n = 0, rv = -1;
1081 NYD_ENTER;
1083 while (s[0] != '\0') {
1084 if (s[0] == '\\') {
1085 if (s[1] == 't') {
1086 if (putc('\t', stream) == EOF)
1087 goto jleave;
1088 ++n;
1089 s += 2;
1090 continue;
1092 if (s[1] == 'n') {
1093 if (putc('\n', stream) == EOF)
1094 goto jleave;
1095 ++n;
1096 s += 2;
1097 continue;
1100 if (putc(s[0], stream) == EOF)
1101 goto jleave;
1102 ++n;
1103 ++s;
1105 if (putc('\n', stream) == EOF)
1106 goto jleave;
1107 rv = ++n;
1108 jleave:
1109 NYD_LEAVE;
1110 return rv;
1113 static void
1114 a_coll__hook_setter(void *arg){ /* TODO v15: drop */
1115 struct header *hp;
1116 char const *val;
1117 NYD2_ENTER;
1119 hp = arg;
1121 if((val = detract(hp->h_from, GNAMEONLY)) == NULL)
1122 val = n_empty;
1123 ok_vset(compose_from, val);
1124 if((val = detract(hp->h_sender, 0)) == NULL)
1125 val = n_empty;
1126 ok_vset(compose_sender, val);
1127 if((val = detract(hp->h_to, GNAMEONLY)) == NULL)
1128 val = n_empty;
1129 ok_vset(compose_to, val);
1130 if((val = detract(hp->h_cc, GNAMEONLY)) == NULL)
1131 val = n_empty;
1132 ok_vset(compose_cc, val);
1133 if((val = detract(hp->h_bcc, GNAMEONLY)) == NULL)
1134 val = n_empty;
1135 ok_vset(compose_bcc, val);
1136 if((val = hp->h_subject) == NULL)
1137 val = n_empty;
1138 ok_vset(compose_subject, val);
1139 NYD2_LEAVE;
1142 static int
1143 a_coll_ocds__mac(void){
1144 /* Executes in a fork(2)ed child */
1145 setvbuf(stdout, NULL, _IOLBF, 0);
1146 pstate |= PS_COMPOSE_FORKHOOK;
1147 temporary_call_compose_mode_hook(a_coll_ocds__macname, NULL, NULL);
1148 _exit(EXIT_OK);
1151 static void
1152 a_coll_ocds__finalize(void *vp){
1153 /* Note we use this for destruction upon setup errors, thus */
1154 sighandler_type opipe;
1155 sighandler_type oint;
1156 struct a_coll_ocds_arg **coapp, *coap;
1157 NYD2_ENTER;
1159 temporary_call_compose_mode_hook((char*)-1, NULL, NULL);
1161 coap = *(coapp = vp);
1162 *coapp = (struct a_coll_ocds_arg*)-1;
1164 if(coap->coa_stdout != NULL)
1165 if(!Pclose(coap->coa_stdout, FAL0)){
1166 *coap->coa_senderr = TRU1;
1167 n_err(_("*on-compose-done(-shell)?* failed: %s\n"),
1168 n_shexp_quote_cp(coap->coa_cmd, FAL0));
1171 if(coap->coa_stdin != NULL)
1172 Fclose(coap->coa_stdin);
1173 else if(coap->coa_pipe[0] != -1)
1174 close(coap->coa_pipe[0]);
1176 if(coap->coa_pipe[1] != -1)
1177 close(coap->coa_pipe[1]);
1179 opipe = coap->coa_opipe;
1180 oint = coap->coa_oint;
1182 n_lofi_free(coap);
1184 hold_all_sigs();
1185 safe_signal(SIGPIPE, opipe);
1186 safe_signal(SIGINT, oint);
1187 rele_all_sigs();
1188 NYD2_LEAVE;
1191 FL FILE *
1192 collect(struct header *hp, int printheaders, struct message *mp,
1193 char *quotefile, int doprefix, si8_t *checkaddr_err)
1195 struct n_ignore const *quoteitp;
1196 struct a_coll_ocds_arg *coap;
1197 int lc, cc, c;
1198 int volatile t, eofcnt, getfields;
1199 char *linebuf, escape_saved, escape;
1200 char const *cp, *coapm;
1201 size_t i, linesize; /* TODO line pool */
1202 long cnt;
1203 enum sendaction action;
1204 sigset_t oset, nset;
1205 FILE * volatile sigfp;
1206 NYD_ENTER;
1208 _coll_fp = NULL;
1209 sigfp = NULL;
1210 linesize = 0;
1211 linebuf = NULL;
1212 eofcnt = 0;
1213 coapm = NULL;
1214 coap = NULL;
1216 /* Start catching signals from here, but we're still die on interrupts
1217 * until we're in the main loop */
1218 sigfillset(&nset);
1219 sigprocmask(SIG_BLOCK, &nset, &oset);
1220 /* FIXME have dropped handlerpush() and localized onintr() in lex.c! */
1221 if ((_coll_saveint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
1222 safe_signal(SIGINT, &_collint);
1223 if ((_coll_savehup = safe_signal(SIGHUP, SIG_IGN)) != SIG_IGN)
1224 safe_signal(SIGHUP, collhup);
1225 if (sigsetjmp(_coll_abort, 1))
1226 goto jerr;
1227 if (sigsetjmp(_coll_jmp, 1))
1228 goto jerr;
1229 pstate |= PS_COMPOSE_MODE;
1230 sigprocmask(SIG_SETMASK, &oset, (sigset_t*)NULL);
1232 if ((_coll_fp = Ftmp(NULL, "collect", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
1233 NULL) {
1234 n_perr(_("temporary mail file"), 0);
1235 goto jerr;
1238 /* If we are going to prompt for a subject, refrain from printing a newline
1239 * after the headers (since some people mind) */
1240 getfields = 0;
1241 if (!(options & OPT_t_FLAG)) {
1242 t = GTO | GSUBJECT | GCC | GNL;
1243 if (ok_blook(fullnames))
1244 t |= GCOMMA;
1246 if (options & OPT_INTERACTIVE) {
1247 if (hp->h_subject == NULL && (ok_blook(ask) || ok_blook(asksub)))
1248 t &= ~GNL, getfields |= GSUBJECT;
1250 if (hp->h_to == NULL)
1251 t &= ~GNL, getfields |= GTO;
1253 if (!ok_blook(bsdcompat) && !ok_blook(askatend)) {
1254 if (hp->h_bcc == NULL && ok_blook(askbcc))
1255 t &= ~GNL, getfields |= GBCC;
1256 if (hp->h_cc == NULL && ok_blook(askcc))
1257 t &= ~GNL, getfields |= GCC;
1260 } else {
1261 n_UNINIT(t, 0);
1264 escape_saved = escape = ((cp = ok_vlook(escape)) != NULL) ? *cp : n_ESCAPE;
1265 _coll_hadintr = 0;
1267 if (!sigsetjmp(_coll_jmp, 1)) {
1268 /* Ask for some headers first, as necessary */
1269 if (getfields)
1270 grab_headers(n_LEXINPUT_CTX_COMPOSE, hp, getfields, 1);
1272 /* Execute compose-enter TODO completely v15-compat intermediate!! */
1273 if((cp = ok_vlook(on_compose_enter)) != NULL){
1274 setup_from_and_sender(hp);
1275 temporary_call_compose_mode_hook(cp, &a_coll__hook_setter, hp);
1278 /* Cannot do since it may require turning this into a multipart one */
1279 if(!(options & OPT_Mm_FLAG)){
1280 char const *cp_obsolete = ok_vlook(NAIL_HEAD);
1281 if(cp_obsolete != NULL)
1282 OBSOLETE(_("please use *message-inject-head* "
1283 "instead of *NAIL_HEAD*"));
1285 if(((cp = ok_vlook(message_inject_head)) != NULL ||
1286 (cp = cp_obsolete) != NULL) && putesc(cp, _coll_fp) < 0)
1287 goto jerr;
1289 /* Quote an original message */
1290 if (mp != NULL && (doprefix || (cp = ok_vlook(quote)) != NULL)) {
1291 quoteitp = n_IGNORE_ALL;
1292 action = SEND_QUOTE;
1293 if (doprefix) {
1294 quoteitp = n_IGNORE_FWD;
1295 if ((cp = ok_vlook(fwdheading)) == NULL)
1296 cp = "-------- Original Message --------";
1297 if (*cp != '\0' && fprintf(_coll_fp, "%s\n", cp) < 0)
1298 goto jerr;
1299 } else if (!strcmp(cp, "noheading")) {
1300 /*EMPTY*/;
1301 } else if (!strcmp(cp, "headers")) {
1302 quoteitp = n_IGNORE_TYPE;
1303 } else if (!strcmp(cp, "allheaders")) {
1304 quoteitp = NULL;
1305 action = SEND_QUOTE_ALL;
1306 } else {
1307 cp = hfield1("from", mp);
1308 if (cp != NULL && (cnt = (long)strlen(cp)) > 0) {
1309 if (xmime_write(cp, cnt, _coll_fp, CONV_FROMHDR, TD_NONE) < 0)
1310 goto jerr;
1311 if (fprintf(_coll_fp, _(" wrote:\n\n")) < 0)
1312 goto jerr;
1315 if (fflush(_coll_fp))
1316 goto jerr;
1317 if (doprefix)
1318 cp = NULL;
1319 else if ((cp = ok_vlook(indentprefix)) == NULL)
1320 cp = INDENT_DEFAULT;
1321 if (sendmp(mp, _coll_fp, quoteitp, cp, action, NULL) < 0)
1322 goto jerr;
1326 if (quotefile != NULL) {
1327 if (_include_file(quotefile, &lc, &cc, FAL0) != 0)
1328 goto jerr;
1331 if ((options & (OPT_Mm_FLAG | OPT_INTERACTIVE)) == OPT_INTERACTIVE) {
1332 /* Print what we have sofar also on the terminal (if useful) */
1333 if (!ok_blook(editalong)) {
1334 if (printheaders)
1335 puthead(TRU1, hp, stdout, t, SEND_TODISP, CONV_NONE, NULL, NULL);
1337 rewind(_coll_fp);
1338 while ((c = getc(_coll_fp)) != EOF) /* XXX bytewise, yuck! */
1339 putc(c, stdout);
1340 if (fseek(_coll_fp, 0, SEEK_END))
1341 goto jerr;
1342 } else {
1343 rewind(_coll_fp);
1344 mesedit('e', hp);
1345 /* As mandated by the Mail Reference Manual, print "(continue)" */
1346 jcont:
1347 if(options & OPT_INTERACTIVE)
1348 fputs(_("(continue)\n"), stdout);
1350 fflush(stdout);
1352 } else {
1353 /* Come here for printing the after-signal message. Duplicate messages
1354 * won't be printed because the write is aborted if we get a SIGTTOU */
1355 if (_coll_hadintr)
1356 n_err(_("\n(Interrupt -- one more to kill letter)\n"));
1359 /* If not under shell hook control */
1360 if(coap == NULL){
1361 /* We're done with -M or -m (because we are too simple minded) */
1362 if(options & OPT_Mm_FLAG)
1363 goto jout;
1364 /* No command escapes, interrupts not expected. Simply copy STDIN */
1365 if (!(options & (OPT_INTERACTIVE | OPT_t_FLAG | OPT_TILDE_FLAG))){
1366 linebuf = srealloc(linebuf, linesize = LINESIZE);
1367 while ((i = fread(linebuf, sizeof *linebuf, linesize, stdin)) > 0) {
1368 if (i != fwrite(linebuf, sizeof *linebuf, i, _coll_fp))
1369 goto jerr;
1371 goto jout;
1375 /* The interactive collect loop */
1376 for(;;){
1377 /* C99 */{
1378 enum n_lexinput_flags lif;
1380 lif = n_LEXINPUT_CTX_COMPOSE;
1381 if(options & (OPT_INTERACTIVE | OPT_TILDE_FLAG)){
1382 if(!(options & OPT_t_FLAG))
1383 lif |= n_LEXINPUT_NL_ESC;
1385 cnt = n_lex_input(lif, n_empty, &linebuf, &linesize, NULL);
1388 if (cnt < 0) {
1389 if(coap != NULL)
1390 break;
1391 if (options & OPT_t_FLAG) {
1392 fflush_rewind(_coll_fp);
1393 /* It is important to set PS_t_FLAG before extract_header() *and*
1394 * keep OPT_t_FLAG for the first parse of the message, too! */
1395 pstate |= PS_t_FLAG;
1396 if (makeheader(_coll_fp, hp, checkaddr_err) != OKAY)
1397 goto jerr;
1398 options &= ~OPT_t_FLAG;
1399 continue;
1400 } else if ((options & OPT_INTERACTIVE) &&
1401 ok_blook(ignoreeof) && ++eofcnt < 4) {
1402 printf(_("*ignoreeof* set, use `~.' to terminate letter\n"));
1403 continue;
1405 break;
1408 _coll_hadintr = 0;
1410 cp = linebuf;
1411 if(cnt == 0)
1412 goto jputnl;
1413 else if(coap == NULL){
1414 if(!(options & (OPT_INTERACTIVE | OPT_TILDE_FLAG)))
1415 goto jputline;
1416 else if(cp[0] == '.'){
1417 if(cnt == 1 && (ok_blook(dot) || ok_blook(ignoreeof)))
1418 break;
1421 if(cp[0] != escape){
1422 jputline:
1423 if(fwrite(cp, sizeof *cp, cnt, _coll_fp) != (size_t)cnt)
1424 goto jerr;
1425 /* TODO PS_READLINE_NL is a terrible hack to ensure that _in_all_-
1426 * TODO _code_paths_ a file without trailing newline isn't modified
1427 * TODO to continue one; the "saw-newline" needs to be part of an
1428 * TODO I/O input machinery object */
1429 jputnl:
1430 if(pstate & PS_READLINE_NL){
1431 if(putc('\n', _coll_fp) == EOF)
1432 goto jerr;
1434 continue;
1437 /* Cleanup the input string: like this we can perform a little bit of
1438 * usage testing and also have somewhat normalized history entries */
1439 for(cp = &linebuf[2]; (c = *cp) != '\0' && blankspacechar(c); ++cp)
1440 continue;
1441 if(c == '\0'){
1442 linebuf[2] = '\0';
1443 cnt = 2;
1444 }else{
1445 i = PTR2SIZE(cp - linebuf) - 3;
1446 memmove(&linebuf[3], cp, (cnt -= i));
1447 linebuf[2] = ' ';
1448 linebuf[cnt] = '\0';
1450 if(cnt > 0){ /* TODO v15 no more trailing WS from lex_input please */
1451 cp = &linebuf[cnt];
1453 for(;; --cp){
1454 c = cp[-1];
1455 if(!blankspacechar(c))
1456 break;
1458 ((char*)n_UNCONST(cp))[0] = '\0';
1459 cnt = PTR2SIZE(cp - linebuf);
1463 switch((c = linebuf[1])){
1464 default:
1465 /* On double escape, send a single one. Otherwise, it's an error */
1466 if(c == escape){
1467 cp = &linebuf[1];
1468 --cnt;
1469 goto jputline;
1470 }else{
1471 char buf[sizeof(n_UNIREPL)];
1473 if(asciichar(c))
1474 buf[0] = c, buf[1] = '\0';
1475 else if(options & OPT_UNICODE)
1476 memcpy(buf, n_unirepl, sizeof n_unirepl);
1477 else
1478 buf[0] = '?', buf[1] = '\0';
1479 n_err(_("Unknown command escape: ~%s\n"), buf);
1480 continue;
1482 jearg:
1483 n_err(_("Invalid command escape usage: %s\n"), linebuf);
1484 continue;
1485 case '!':
1486 /* Shell escape, send the balance of line to sh -c */
1487 if(cnt == 2 || coap != NULL)
1488 goto jearg;
1489 c_shell(&linebuf[3]);
1490 goto jhistcont;
1491 case ':':
1492 /* FALLTHRU */
1493 case '_':
1494 /* Escape to command mode, but be nice! *//* TODO command expansion
1495 * TODO should be handled here so that we have unique history! */
1496 if(cnt == 2)
1497 goto jearg;
1498 _execute_command(hp, &linebuf[3], cnt -= 3);
1499 break;
1500 case '.':
1501 /* Simulate end of file on input */
1502 if(cnt != 2 || coap != NULL)
1503 goto jearg;
1504 goto jout;
1505 case 'x':
1506 /* Same as 'q', but no *DEAD* saving */
1507 /* FALLTHRU */
1508 case 'q':
1509 /* Force a quit, act like an interrupt had happened */
1510 if(cnt != 2)
1511 goto jearg;
1512 ++_coll_hadintr;
1513 _collint((c == 'x') ? 0 : SIGINT);
1514 exit(EXIT_ERR);
1515 /*NOTREACHED*/
1516 case 'h':
1517 /* Grab a bunch of headers */
1518 if(cnt != 2)
1519 goto jearg;
1521 grab_headers(n_LEXINPUT_CTX_COMPOSE, hp,
1522 (GTO | GSUBJECT | GCC | GBCC),
1523 (ok_blook(bsdcompat) && ok_blook(bsdorder)));
1524 while(hp->h_to == NULL);
1525 break;
1526 case 'H':
1527 /* Grab extra headers */
1528 if(cnt != 2)
1529 goto jearg;
1531 grab_headers(n_LEXINPUT_CTX_COMPOSE, hp, GEXTRA, 0);
1532 while(check_from_and_sender(hp->h_from, hp->h_sender) == NULL);
1533 break;
1534 case 't':
1535 /* Add to the To: list */
1536 if(cnt == 2)
1537 goto jearg;
1538 hp->h_to = cat(hp->h_to,
1539 checkaddrs(lextract(&linebuf[3], GTO | GFULL), EACM_NORMAL,
1540 NULL));
1541 break;
1542 case 's':
1543 /* Set the Subject list */
1544 if(cnt == 2)
1545 goto jearg;
1546 /* Subject:; take care for Debian #419840 and strip any \r and \n */
1547 if(n_anyof_cp("\n\r", hp->h_subject = savestr(&linebuf[3]))){
1548 char *xp;
1550 n_err(_("-s: normalizing away invalid ASCII NL / CR bytes\n"));
1551 for(xp = hp->h_subject; *xp != '\0'; ++xp)
1552 if(*xp == '\n' || *xp == '\r')
1553 *xp = ' ';
1555 break;
1556 case '@':{
1557 struct attachment *aplist;
1559 /* Edit the attachment list */
1560 aplist = hp->h_attach;
1561 hp->h_attach = NULL;
1562 if(cnt != 2)
1563 hp->h_attach = n_attachment_append_list(aplist, &linebuf[3]);
1564 else
1565 hp->h_attach = n_attachment_list_edit(aplist,
1566 n_LEXINPUT_CTX_COMPOSE);
1567 } break;
1568 case 'c':
1569 /* Add to the CC list */
1570 if(cnt == 2)
1571 goto jearg;
1572 hp->h_cc = cat(hp->h_cc,
1573 checkaddrs(lextract(&linebuf[3], GCC | GFULL), EACM_NORMAL,
1574 NULL));
1575 break;
1576 case 'b':
1577 /* Add stuff to blind carbon copies list */
1578 if(cnt == 2)
1579 goto jearg;
1580 hp->h_bcc = cat(hp->h_bcc,
1581 checkaddrs(lextract(&linebuf[3], GBCC | GFULL), EACM_NORMAL,
1582 NULL));
1583 break;
1584 case 'd':
1585 if(cnt != 2)
1586 goto jearg;
1587 cp = n_getdeadletter();
1588 if(0){
1589 /*FALLTHRU*/
1590 case 'R':
1591 case 'r':
1592 case '<':
1593 /* Invoke a file: Search for the file name, then open it and copy
1594 * the contents to _coll_fp */
1595 if(cnt == 2){
1596 n_err(_("Interpolate what file?\n"));
1597 break;
1599 if(*(cp = &linebuf[3]) == '!'){
1600 insertcommand(_coll_fp, ++cp);
1601 goto jhistcont;
1603 if((cp = fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO)) == NULL)
1604 break;
1606 if(is_dir(cp)){
1607 n_err(_("%s: is a directory\n"), n_shexp_quote_cp(cp, FAL0));
1608 break;
1610 if(_include_file(cp, &lc, &cc, (c == 'R')) != 0){
1611 if(ferror(_coll_fp))
1612 goto jerr;
1613 break;
1615 printf(_("%s %d/%d\n"), n_shexp_quote_cp(cp, FAL0), lc, cc);
1616 break;
1617 case 'i':
1618 /* Insert a variable into the file */
1619 if(cnt == 2)
1620 goto jearg;
1621 if((cp = vok_vlook(&linebuf[3])) == NULL || *cp == '\0')
1622 break;
1623 if(putesc(cp, _coll_fp) < 0) /* TODO v15: user resp upon `set' time */
1624 goto jerr;
1625 if((options & OPT_INTERACTIVE) && putesc(cp, stdout) < 0)
1626 goto jerr;
1627 break;
1628 case 'a':
1629 case 'A':
1630 /* Insert the contents of a signature variable */
1631 if(cnt != 2)
1632 goto jearg;
1633 cp = (c == 'a') ? ok_vlook(sign) : ok_vlook(Sign);
1634 if(cp != NULL && *cp != '\0'){
1635 if(putesc(cp, _coll_fp) < 0) /* TODO v15: user upon `set' time */
1636 goto jerr;
1637 if((options & OPT_INTERACTIVE) && putesc(cp, stdout) < 0)
1638 goto jerr;
1640 break;
1641 case 'w':
1642 /* Write the message on a file */
1643 if(cnt == 2)
1644 goto jearg;
1645 if((cp = fexpand(&linebuf[3], FEXP_LOCAL | FEXP_NOPROTO)) == NULL){
1646 n_err(_("Write what file!?\n"));
1647 break;
1649 rewind(_coll_fp);
1650 if(exwrite(cp, _coll_fp, 1) < 0)
1651 goto jerr;
1652 break;
1653 case 'm':
1654 case 'M':
1655 case 'f':
1656 case 'F':
1657 case 'u':
1658 case 'U':
1659 /* Interpolate the named messages, if we are in receiving mail mode.
1660 * Does the standard list processing garbage. If ~f is given, we
1661 * don't shift over */
1662 if(cnt == 2)
1663 goto jearg;
1664 if(forward(&linebuf[3], _coll_fp, c) < 0)
1665 break;
1666 break;
1667 case 'p':
1668 /* Print current state of the message without altering anything */
1669 if(cnt != 2)
1670 goto jearg;
1671 print_collf(_coll_fp, hp);
1672 break;
1673 case '|':
1674 /* Pipe message through command. Collect output as new message */
1675 if(cnt == 2)
1676 goto jearg;
1677 rewind(_coll_fp);
1678 mespipe(&linebuf[3]);
1679 goto jhistcont;
1680 case 'v':
1681 case 'e':
1682 /* Edit the current message. 'e' -> use EDITOR, 'v' -> use VISUAL */
1683 if(cnt != 2 || coap != NULL)
1684 goto jearg;
1685 rewind(_coll_fp);
1686 mesedit(c, ok_blook(editheaders) ? hp : NULL);
1687 goto jhistcont;
1688 case '^':
1689 if(!a_collect_plumbing(&linebuf[3], hp))
1690 goto jearg;
1691 if(options & OPT_INTERACTIVE)
1692 break;
1693 continue;
1694 case '?':
1695 /* Last the lengthy help string. (Very ugly, but take care for
1696 * compiler supported string lengths :() */
1697 puts(_(
1698 "COMMAND ESCAPES (to be placed after a newline) excerpt:\n"
1699 "~. Commit and send message\n"
1700 "~: <command> Execute a mail command\n"
1701 "~<! <command> Insert output of command\n"
1702 "~@ [<files>] Edit attachment list\n"
1703 "~A Insert *Sign* variable (`~a': insert *sign*)\n"
1704 "~c <users> Add users to Cc: list (`~b': to Bcc:)\n"
1705 "~d Read in $DEAD (dead.letter)\n"
1706 "~e Edit message via $EDITOR"
1708 puts(_(
1709 "~F <msglist> Read in with headers, don't *indentprefix* lines\n"
1710 "~f <msglist> Like ~F, but honour `ignore' / `retain' configuration\n"
1711 "~H Edit From:, Reply-To: and Sender:\n"
1712 "~h Prompt for Subject:, To:, Cc: and \"blind\" Bcc:\n"
1713 "~i <variable> Insert a value and a newline\n"
1714 "~M <msglist> Read in with headers, *indentprefix* (`~m': `retain' etc.)\n"
1715 "~p Show current message compose buffer\n"
1716 "~r <file> Read in a file (`~<': likewise; `~R': *indentprefix* lines)"
1718 puts(_(
1719 "~s <subject> Set Subject:\n"
1720 "~t <users> Add users to To: list\n"
1721 "~u <msglist> Read in message(s) without headers (`~U': indent lines)\n"
1722 "~v Edit message via $VISUAL\n"
1723 "~w <file> Write message onto file\n"
1724 "~x Abort composition, discard message (`~q': save in $DEAD)\n"
1725 "~| <command> Pipe message through shell filter"
1727 if(cnt != 2)
1728 goto jearg;
1729 break;
1732 /* Finally place an entry in history as applicable */
1733 if(0){
1734 jhistcont:
1735 c = '\1';
1736 }else
1737 c = '\0';
1738 if(options & OPT_INTERACTIVE)
1739 n_tty_addhist(linebuf, TRU1);
1740 if(c != '\0')
1741 goto jcont;
1744 jout:
1745 /* Do we have *on-compose-done-shell*, or *on-compose-done*?
1746 * TODO Usual f...ed up state of signals and terminal etc. */
1747 if(coap == NULL && (cp = ok_vlook(on_compose_done_shell)) != NULL) Jocds:{
1748 union {int (*ptf)(void); char const *sh;} u;
1749 char const *cmd;
1751 /* Reset *escape* to be available and guaranteed! */
1752 escape = n_ESCAPE;
1754 if(coapm != NULL){
1755 u.ptf = &a_coll_ocds__mac;
1756 cmd = (char*)-1;
1757 a_coll_ocds__macname = cp = coapm;
1758 }else{
1759 u.sh = ok_vlook(SHELL);
1760 cmd = cp;
1763 i = strlen(cp) +1;
1764 coap = n_lofi_alloc(n_VSTRUCT_SIZEOF(struct a_coll_ocds_arg, coa_cmd
1765 ) + i);
1766 coap->coa_pipe[0] = coap->coa_pipe[1] = -1;
1767 coap->coa_stdin = coap->coa_stdout = NULL;
1768 coap->coa_senderr = checkaddr_err;
1769 memcpy(coap->coa_cmd, cp, i);
1771 hold_all_sigs();
1772 coap->coa_opipe = safe_signal(SIGPIPE, SIG_IGN);
1773 coap->coa_oint = safe_signal(SIGINT, SIG_IGN);
1774 rele_all_sigs();
1776 if(pipe_cloexec(coap->coa_pipe) != -1 &&
1777 (coap->coa_stdin = Fdopen(coap->coa_pipe[0], "r", FAL0)) != NULL &&
1778 (coap->coa_stdout = Popen(cmd, "W", u.sh, NULL, coap->coa_pipe[1])
1779 ) != NULL){
1780 close(coap->coa_pipe[1]);
1781 coap->coa_pipe[1] = -1;
1783 temporary_call_compose_mode_hook(NULL, NULL, NULL);
1784 n_source_slice_hack(coap->coa_cmd, coap->coa_stdin, coap->coa_stdout,
1785 (options & ~OPT_INTERACTIVE), &a_coll_ocds__finalize, &coap);
1786 /* Hook version protocol for ~^: update manual upon change! */
1787 fputs(a_COLL_PLUMBING_VERSION "\n", coap->coa_stdout);
1788 #undef a_COLL_PLUMBING_VERSION
1789 goto jcont;
1792 c = errno;
1793 a_coll_ocds__finalize(coap);
1794 n_perr(_("Cannot invoke *on-compose-done(-shell)?*"), c);
1795 goto jerr;
1797 if(*checkaddr_err != 0){
1798 *checkaddr_err = 0;
1799 goto jerr;
1801 if(coapm == NULL && (coapm = ok_vlook(on_compose_done)) != NULL)
1802 goto Jocds;
1803 escape = escape_saved;
1805 /* Final chance to edit headers, if not already above */
1806 if (ok_blook(bsdcompat) || ok_blook(askatend)) {
1807 if (hp->h_cc == NULL && ok_blook(askcc))
1808 grab_headers(n_LEXINPUT_CTX_COMPOSE, hp, GCC, 1);
1809 if (hp->h_bcc == NULL && ok_blook(askbcc))
1810 grab_headers(n_LEXINPUT_CTX_COMPOSE, hp, GBCC, 1);
1812 if (hp->h_attach == NULL && ok_blook(askattach))
1813 hp->h_attach = n_attachment_list_edit(hp->h_attach,
1814 n_LEXINPUT_CTX_COMPOSE);
1816 /* Add automatic receivers */
1817 if ((cp = ok_vlook(autocc)) != NULL && *cp != '\0')
1818 hp->h_cc = cat(hp->h_cc, checkaddrs(lextract(cp, GCC | GFULL),
1819 EACM_NORMAL, checkaddr_err));
1820 if ((cp = ok_vlook(autobcc)) != NULL && *cp != '\0')
1821 hp->h_bcc = cat(hp->h_bcc, checkaddrs(lextract(cp, GBCC | GFULL),
1822 EACM_NORMAL, checkaddr_err));
1823 if (*checkaddr_err != 0)
1824 goto jerr;
1826 /* Execute compose-leave */
1827 if((cp = ok_vlook(on_compose_leave)) != NULL){
1828 setup_from_and_sender(hp);
1829 temporary_call_compose_mode_hook(cp, &a_coll__hook_setter, hp);
1832 /* TODO Cannot do since it may require turning this into a multipart one */
1833 if(options & OPT_Mm_FLAG)
1834 goto jskiptails;
1836 /* Place signature? */
1837 if((cp = ok_vlook(signature)) != NULL && *cp != '\0'){
1838 char const *cpq;
1840 if((cpq = fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO)) == NULL){
1841 n_err(_("*signature* expands to invalid file: %s\n"),
1842 n_shexp_quote_cp(cp, FAL0));
1843 goto jerr;
1845 cpq = n_shexp_quote_cp(cp = cpq, FAL0);
1847 if((sigfp = Fopen(cp, "r")) == NULL){
1848 n_err(_("Can't open *signature* %s: %s\n"), cpq, strerror(errno));
1849 goto jerr;
1852 if(linebuf == NULL)
1853 linebuf = smalloc(linesize = LINESIZE);
1854 c = '\0';
1855 while((i = fread(linebuf, sizeof *linebuf, linesize, n_UNVOLATILE(sigfp)))
1856 > 0){
1857 c = linebuf[i - 1];
1858 if(i != fwrite(linebuf, sizeof *linebuf, i, _coll_fp))
1859 goto jerr;
1862 /* C99 */{
1863 FILE *x = n_UNVOLATILE(sigfp);
1864 int e = errno, ise = ferror(x);
1866 sigfp = NULL;
1867 Fclose(x);
1869 if(ise){
1870 n_err(_("Errors while reading *signature* %s: %s\n"),
1871 cpq, strerror(e));
1872 goto jerr;
1876 if(c != '\0' && c != '\n')
1877 putc('\n', _coll_fp);
1880 { char const *cp_obsolete = ok_vlook(NAIL_TAIL);
1882 if(cp_obsolete != NULL)
1883 OBSOLETE(_("please use *message-inject-tail* instead of *NAIL_TAIL*"));
1885 if((cp = ok_vlook(message_inject_tail)) != NULL ||
1886 (cp = cp_obsolete) != NULL){
1887 if(putesc(cp, _coll_fp) < 0)
1888 goto jerr;
1889 if((options & OPT_INTERACTIVE) && putesc(cp, stdout) < 0)
1890 goto jerr;
1894 jskiptails:
1895 if(fflush(_coll_fp))
1896 goto jerr;
1897 rewind(_coll_fp);
1899 jleave:
1900 temporary_unroll_compose_mode();
1901 if (linebuf != NULL)
1902 free(linebuf);
1903 sigfillset(&nset);
1904 sigprocmask(SIG_BLOCK, &nset, NULL);
1905 pstate &= ~PS_COMPOSE_MODE;
1906 safe_signal(SIGINT, _coll_saveint);
1907 safe_signal(SIGHUP, _coll_savehup);
1908 sigprocmask(SIG_SETMASK, &oset, NULL);
1909 NYD_LEAVE;
1910 return _coll_fp;
1912 jerr:
1913 if(coap != NULL && coap != (struct a_coll_ocds_arg*)-1)
1914 n_source_slice_hack_remove_after_jump();
1915 if(sigfp != NULL)
1916 Fclose(n_UNVOLATILE(sigfp));
1917 if (_coll_fp != NULL) {
1918 Fclose(_coll_fp);
1919 _coll_fp = NULL;
1921 assert(checkaddr_err != NULL);
1922 /* TODO We don't save in $DEAD upon error because msg not readily composed?
1923 * TODO But this no good, it should go ZOMBIE / DRAFT / POSTPONED or what! */
1924 if(*checkaddr_err != 0)
1925 n_err(_("Some addressees were classified as \"hard error\"\n"));
1926 else if(_coll_hadintr == 0){
1927 *checkaddr_err = TRU1; /* TODO ugly: "sendout_error" now.. */
1928 n_err(_("Failed to prepare composed message (I/O error, disk full?)\n"));
1930 goto jleave;
1933 /* s-it-mode */