Show the Content-Description:, as applicable
[s-mailx.git] / collect.c
blob43fda0f9d49b5e4688b3722a7d65dd556d7e8ee8
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 sigjmp_buf _coll_pipejmp; /* On broken pipe */
63 static char const *a_coll_ocds__macname; /* *on-compose-done* */
65 /* Handle `~:', `~_' and some hooks; hp may be NULL */
66 static void _execute_command(struct header *hp, char const *linebuf,
67 size_t linesize);
69 /* */
70 static int _include_file(char const *name, int *linecount,
71 int *charcount, bool_t indent);
73 static void _collect_onpipe(int signo);
75 /* Execute cmd and insert its standard output into fp */
76 static void insertcommand(FILE *fp, char const *cmd);
78 /* ~p command */
79 static void print_collf(FILE *collf, struct header *hp, bool_t pagerok);
81 /* Write a file, ex-like if f set */
82 static int exwrite(char const *name, FILE *fp, int f);
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 enum okay makeheader(FILE *fp, struct header *hp, si8_t *checkaddr_err);
88 /* Edit the message being collected on fp. On return, make the edit file the
89 * new temp file */
90 static void mesedit(int c, struct header *hp);
92 /* Pipe the message through the command. Old message is on stdin of command,
93 * new message collected from stdout. Shell must return 0 to accept new msg */
94 static void mespipe(char *cmd);
96 /* Interpolate the named messages into the current message, possibly doing
97 * indent stuff. The flag argument is one of the command escapes: [mMfFuU].
98 * Return a count of the number of characters now in the message, or -1 if an
99 * error is encountered writing the message temporary */
100 static int forward(char *ms, FILE *fp, int f);
102 /* ~^ mode */
103 static bool_t a_collect_plumbing(char const *ms, struct header *p);
105 /* On interrupt, come here to save the partial message in ~/dead.letter.
106 * Then jump out of the collection loop */
107 static void _collint(int s);
109 static void collhup(int s);
111 static int putesc(char const *s, FILE *stream); /* TODO wysh set! */
113 /* temporary_call_compose_mode_hook() setter hook */
114 static void a_coll__hook_setter(void *arg);
116 /* *on-compose-done* driver and *on-compose-done(-shell)?* finalizer */
117 static int a_coll_ocds__mac(void);
118 static void a_coll_ocds__finalize(void *vp);
120 static void
121 _execute_command(struct header *hp, char const *linebuf, size_t linesize){
122 /* The problem arises if there are rfc822 message attachments and the
123 * user uses `~:' to change the current file. TODO Unfortunately we
124 * TODO cannot simply keep a pointer to, or increment a reference count
125 * TODO of the current `file' (mailbox that is) object, because the
126 * TODO codebase doesn't deal with that at all; so, until some far
127 * TODO later time, copy the name of the path, and warn the user if it
128 * TODO changed; we COULD use the AC_TMPFILE attachment type, i.e.,
129 * TODO copy the message attachments over to temporary files, but that
130 * TODO would require more changes so that the user still can recognize
131 * TODO in `~@' etc. that its a rfc822 message attachment; see below */
132 struct n_sigman sm;
133 struct attachment *ap;
134 char * volatile mnbuf;
135 NYD_ENTER;
137 n_UNUSED(linesize);
138 mnbuf = NULL;
140 n_SIGMAN_ENTER_SWITCH(&sm, n_SIGMAN_ALL){
141 case 0:
142 break;
143 default:
144 goto jleave;
147 /* If the above todo is worked, remove or outsource to attachments.c! */
148 if(hp != NULL && (ap = hp->h_attach) != NULL) do
149 if(ap->a_msgno){
150 mnbuf = sstrdup(mailname);
151 break;
153 while((ap = ap->a_flink) != NULL);
155 n_source_command(n_LEXINPUT_CTX_COMPOSE, linebuf);
157 n_sigman_cleanup_ping(&sm);
158 jleave:
159 if(mnbuf != NULL){
160 if(strcmp(mnbuf, mailname))
161 n_err(_("Mailbox changed: it is likely that existing "
162 "rfc822 attachments became invalid!\n"));
163 free(mnbuf);
165 NYD_LEAVE;
166 n_sigman_leave(&sm, n_SIGMAN_VIPSIGS_NTTYOUT);
169 static int
170 _include_file(char const *name, int *linecount, int *charcount, bool_t indent)
172 FILE *fbuf;
173 char const *indb;
174 int ret = -1;
175 char *linebuf = NULL; /* TODO line pool */
176 size_t linesize = 0, indl, linelen, cnt;
177 NYD_ENTER;
179 /* The -M case is special */
180 if (name == (char*)-1)
181 fbuf = stdin;
182 else if ((fbuf = Fopen(name, "r")) == NULL) {
183 n_perr(name, 0);
184 goto jleave;
187 if (!indent)
188 indb = NULL, indl = 0;
189 else {
190 if ((indb = ok_vlook(indentprefix)) == NULL)
191 indb = INDENT_DEFAULT;
192 indl = strlen(indb);
195 *linecount = *charcount = 0;
196 cnt = fsize(fbuf);
197 while (fgetline(&linebuf, &linesize, &cnt, &linelen, fbuf, 0) != NULL) {
198 if (indl > 0 && fwrite(indb, sizeof *indb, indl, _coll_fp) != indl)
199 goto jleave;
200 if (fwrite(linebuf, sizeof *linebuf, linelen, _coll_fp) != linelen)
201 goto jleave;
202 ++(*linecount);
203 (*charcount) += linelen + indl;
205 if (fflush(_coll_fp))
206 goto jleave;
208 ret = 0;
209 jleave:
210 if (linebuf != NULL)
211 free(linebuf);
212 if (fbuf != NULL && fbuf != stdin)
213 Fclose(fbuf);
214 NYD_LEAVE;
215 return ret;
218 static void
219 _collect_onpipe(int signo)
221 NYD_X; /* Signal handler */
222 n_UNUSED(signo);
223 siglongjmp(_coll_pipejmp, 1);
226 static void
227 insertcommand(FILE *fp, char const *cmd)
229 FILE *ibuf = NULL;
230 int c;
231 NYD_ENTER;
233 if ((ibuf = Popen(cmd, "r", ok_vlook(SHELL), NULL, 0)) != NULL) {
234 while ((c = getc(ibuf)) != EOF) /* XXX bytewise, yuck! */
235 putc(c, fp);
236 Pclose(ibuf, TRU1);
237 } else
238 n_perr(cmd, 0);
239 NYD_LEAVE;
242 static void
243 print_collf(FILE *cf, struct header *hp, bool_t pagerok)
245 char *lbuf = NULL; /* TODO line pool */
246 sighandler_type sigint;
247 FILE * volatile obuf = stdout;
248 struct attachment *ap;
249 char const *cp;
250 enum gfield gf;
251 size_t linesize = 0, linelen, cnt, cnt2;
252 NYD_ENTER;
254 fflush_rewind(cf);
255 cnt = cnt2 = (size_t)fsize(cf);
257 sigint = safe_signal(SIGINT, SIG_IGN);
259 if (pagerok && (cp = ok_vlook(crt)) != NULL) {
260 size_t l, m;
262 m = 4;
263 if (hp->h_to != NULL)
264 ++m;
265 if (hp->h_subject != NULL)
266 ++m;
267 if (hp->h_cc != NULL)
268 ++m;
269 if (hp->h_bcc != NULL)
270 ++m;
271 if (hp->h_attach != NULL)
272 ++m;
273 m += (hp->h_from != NULL || myaddrs(hp) != NULL);
274 m += (hp->h_sender != NULL || ok_vlook(sender) != NULL);
275 m += (hp->h_replyto != NULL || ok_vlook(replyto) != NULL);
277 l = (*cp == '\0') ? (size_t)screensize() : strtoul(cp, NULL, 0);
278 if (m > l)
279 goto jpager;
280 l -= m;
282 for (m = 0; fgetline(&lbuf, &linesize, &cnt2, NULL, cf, 0); ++m)
284 rewind(cf);
285 if (l < m) {
286 jpager:
287 if (sigsetjmp(_coll_pipejmp, 1))
288 goto jendpipe;
289 if ((obuf = n_pager_open()) == NULL)
290 obuf = stdout;
291 else
292 safe_signal(SIGPIPE, &_collect_onpipe);
296 fprintf(obuf, _("-------\nMessage contains:\n"));
297 gf = GIDENT | GTO | GSUBJECT | GCC | GBCC | GNL | GFILES | GCOMMA;
298 puthead(TRU1, hp, obuf, gf, SEND_TODISP, CONV_NONE, NULL, NULL);
299 while (fgetline(&lbuf, &linesize, &cnt, &linelen, cf, 1))
300 prout(lbuf, linelen, obuf);
301 if (hp->h_attach != NULL) {
302 fputs(_("-------\nAttachments:\n"), obuf);
303 for (ap = hp->h_attach; ap != NULL; ap = ap->a_flink) {
304 if (ap->a_msgno)
305 fprintf(obuf, " - message %u\n", ap->a_msgno);
306 else {
307 /* TODO after MIME/send layer rewrite we *know*
308 * TODO the details of the attachment here,
309 * TODO so adjust this again, then */
310 char const *cs, *csi = "-> ";
312 if ((cs = ap->a_charset) == NULL &&
313 (csi = "<- ", cs = ap->a_input_charset) == NULL)
314 cs = charset_get_lc();
315 if ((cp = ap->a_content_type) == NULL)
316 cp = "?";
317 else if (ascncasecmp(cp, "text/", 5))
318 csi = n_empty;
319 fprintf(obuf, " - [%s, %s%s] %s\n", cp, csi, cs,
320 n_shexp_quote_cp(ap->a_name, FAL0));
325 jendpipe:
326 if (obuf != stdout)
327 n_pager_close(obuf);
328 if (lbuf != NULL)
329 free(lbuf);
330 safe_signal(SIGINT, sigint);
331 NYD_LEAVE;
334 static int
335 exwrite(char const *name, FILE *fp, int f)
337 FILE *of;
338 int c, rv;
339 long lc, cc;
340 NYD_ENTER;
342 if (f) {
343 printf("%s ", n_shexp_quote_cp(name, FAL0));
344 fflush(stdout);
347 if ((of = Fopen(name, "a")) == NULL) {
348 n_perr(name, 0);
349 goto jerr;
352 lc = cc = 0;
353 while ((c = getc(fp)) != EOF) {
354 ++cc;
355 if (c == '\n')
356 ++lc;
357 if (putc(c, of) == EOF) {
358 n_perr(name, 0);
359 goto jerr;
362 printf(_("%ld/%ld\n"), lc, cc);
364 rv = 0;
365 jleave:
366 if(of != NULL)
367 Fclose(of);
368 fflush(stdout);
369 NYD_LEAVE;
370 return rv;
371 jerr:
372 putchar('-');
373 putchar('\n');
374 rv = -1;
375 goto jleave;
378 static enum okay
379 makeheader(FILE *fp, struct header *hp, si8_t *checkaddr_err)
381 FILE *nf;
382 int c;
383 enum okay rv = STOP;
384 NYD_ENTER;
386 if ((nf = Ftmp(NULL, "colhead", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==NULL) {
387 n_perr(_("temporary mail edit file"), 0);
388 goto jleave;
391 extract_header(fp, hp, checkaddr_err);
392 if (checkaddr_err != NULL && *checkaddr_err != 0)
393 goto jleave;
395 while ((c = getc(fp)) != EOF) /* XXX bytewise, yuck! */
396 putc(c, nf);
397 if (fp != _coll_fp)
398 Fclose(_coll_fp);
399 Fclose(fp);
400 _coll_fp = nf;
401 if (check_from_and_sender(hp->h_from, hp->h_sender) == NULL)
402 goto jleave;
403 rv = OKAY;
404 jleave:
405 NYD_LEAVE;
406 return rv;
409 static void
410 mesedit(int c, struct header *hp)
412 bool_t saved;
413 sighandler_type sigint;
414 FILE *nf;
415 NYD_ENTER;
417 if(!(saved = ok_blook(add_file_recipients)))
418 ok_bset(add_file_recipients);
420 sigint = safe_signal(SIGINT, SIG_IGN);
421 nf = run_editor(_coll_fp, (off_t)-1, c, FAL0, hp, NULL, SEND_MBOX, sigint);
422 if (nf != NULL) {
423 if (hp) {
424 rewind(nf);
425 makeheader(nf, hp, NULL);
426 } else {
427 fseek(nf, 0L, SEEK_END);
428 Fclose(_coll_fp);
429 _coll_fp = nf;
432 safe_signal(SIGINT, sigint);
434 if(!saved)
435 ok_bclear(add_file_recipients);
436 NYD_LEAVE;
439 static void
440 mespipe(char *cmd)
442 FILE *nf;
443 sighandler_type sigint;
444 NYD_ENTER;
446 sigint = safe_signal(SIGINT, SIG_IGN);
448 if ((nf = Ftmp(NULL, "colpipe", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==NULL) {
449 n_perr(_("temporary mail edit file"), 0);
450 goto jout;
453 /* stdin = current message. stdout = new message */
454 fflush(_coll_fp);
455 if (run_command(ok_vlook(SHELL), 0, fileno(_coll_fp), fileno(nf), "-c",
456 cmd, NULL, NULL) < 0) {
457 Fclose(nf);
458 goto jout;
461 if (fsize(nf) == 0) {
462 n_err(_("No bytes from %s !?\n"), n_shexp_quote_cp(cmd, FAL0));
463 Fclose(nf);
464 goto jout;
467 /* Take new files */
468 fseek(nf, 0L, SEEK_END);
469 Fclose(_coll_fp);
470 _coll_fp = nf;
471 jout:
472 safe_signal(SIGINT, sigint);
473 NYD_LEAVE;
476 static int
477 forward(char *ms, FILE *fp, int f)
479 int *msgvec, rv = 0;
480 struct n_ignore const *itp;
481 char const *tabst;
482 enum sendaction action;
483 NYD_ENTER;
485 msgvec = salloc((size_t)(msgCount + 1) * sizeof *msgvec);
486 if (getmsglist(ms, msgvec, 0) < 0)
487 goto jleave;
488 if (*msgvec == 0) {
489 *msgvec = first(0, MMNORM);
490 if (*msgvec == 0) {
491 n_err(_("No appropriate messages\n"));
492 goto jleave;
494 msgvec[1] = 0;
497 if (f == 'f' || f == 'F' || f == 'u')
498 tabst = NULL;
499 else if ((tabst = ok_vlook(indentprefix)) == NULL)
500 tabst = INDENT_DEFAULT;
501 if (f == 'u' || f == 'U')
502 itp = n_IGNORE_ALL;
503 else
504 itp = upperchar(f) ? NULL : n_IGNORE_TYPE;
505 action = (upperchar(f) && f != 'U') ? SEND_QUOTE_ALL : SEND_QUOTE;
507 printf(_("Interpolating:"));
508 srelax_hold();
509 for (; *msgvec != 0; ++msgvec) {
510 struct message *mp = message + *msgvec - 1;
512 touch(mp);
513 printf(" %d", *msgvec);
514 fflush(stdout);
515 if (sendmp(mp, fp, itp, tabst, action, NULL) < 0) {
516 n_perr(_("temporary mail file"), 0);
517 rv = -1;
518 break;
520 srelax();
522 srelax_rele();
523 printf("\n");
524 jleave:
525 NYD_LEAVE;
526 return rv;
529 static bool_t
530 a_collect_plumbing(char const *ms, struct header *hp){
531 /* TODO _collect_plumbing: instead of fields the basic headers should
532 * TODO be in an array and have IDs, like in termcap etc., so then this
533 * TODO could be simplified as table-walks */
534 char const *cp, *cmd[4];
535 NYD2_ENTER;
537 /* Protcol version for *on-compose-done-shell* -- update manual on change! */
538 #define a_COLL_PLUMBING_VERSION "0 0 1"
539 cp = ms;
541 /* C99 */{
542 size_t i;
544 for(i = 0; i < n_NELEM(cmd); ++i){
545 while(blankchar(*cp))
546 ++cp;
547 if(*cp == '\0')
548 cmd[i] = NULL;
549 else{
550 if(i < n_NELEM(cmd) - 1)
551 for(cmd[i] = cp++; *cp != '\0' && !blankchar(*cp); ++cp)
553 else{
554 /* Last slot takes all the rest of the line, less trailing WS */
555 for(cmd[i] = cp++; *cp != '\0'; ++cp)
557 while(blankchar(cp[-1]))
558 --cp;
560 cmd[i] = savestrbuf(cmd[i], PTR2SIZE(cp - cmd[i]));
565 if(n_UNLIKELY(cmd[0] == NULL))
566 goto jecmd;
567 if(!asccasecmp(cmd[0], "header")){
568 struct n_header_field *hfp;
569 struct name *np, **npp;
571 if(cmd[1] == NULL || !asccasecmp(cmd[1], "list")){
572 if(cmd[2] == NULL){
573 fputs("210", stdout);
574 if(hp->h_from != NULL) fputs(" From", stdout);
575 if(hp->h_sender != NULL) fputs(" Sender", stdout);
576 if(hp->h_to != NULL) fputs(" To", stdout);
577 if(hp->h_cc != NULL) fputs(" Cc", stdout);
578 if(hp->h_bcc != NULL) fputs(" Bcc", stdout);
579 if(hp->h_subject != NULL) fputs(" Subject", stdout);
580 if(hp->h_replyto != NULL) fputs(" Reply-To", stdout);
581 if(hp->h_mft != NULL) fputs(" Mail-Followup-To", stdout);
582 if(hp->h_message_id != NULL) fputs(" Message-ID", stdout);
583 if(hp->h_ref != NULL) fputs(" References", stdout);
584 if(hp->h_in_reply_to != NULL) fputs(" In-Reply-To", stdout);
585 for(hfp = hp->h_user_headers; hfp != NULL; hfp = hfp->hf_next){
586 putc(' ', stdout);
587 fputs(&hfp->hf_dat[0], stdout);
589 putc('\n', stdout);
590 }else{
591 if(cmd[3] != NULL)
592 goto jecmd;
593 if(!asccasecmp(cmd[2], "from")){
594 np = hp->h_from;
595 jlist:
596 fprintf(stdout, "%s %s\n", (np == NULL ? "501" : "210"), cp);
597 }else if(!asccasecmp(cmd[2], cp = "Sender")){
598 np = hp->h_sender;
599 goto jlist;
600 }else if(!asccasecmp(cmd[2], cp = "To")){
601 np = hp->h_to;
602 goto jlist;
603 }else if(!asccasecmp(cmd[2], cp = "Cc")){
604 np = hp->h_cc;
605 goto jlist;
606 }else if(!asccasecmp(cmd[2], cp = "Bcc")){
607 np = hp->h_bcc;
608 goto jlist;
609 }else if(!asccasecmp(cmd[2], cp = "Subject")){
610 np = (struct name*)-1;
611 goto jlist;
612 }else if(!asccasecmp(cmd[2], cp = "Reply-To")){
613 np = hp->h_replyto;
614 goto jlist;
615 }else if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
616 np = hp->h_mft;
617 goto jlist;
618 }else if(!asccasecmp(cmd[2], cp = "Message-ID")){
619 np = hp->h_message_id;
620 goto jlist;
621 }else if(!asccasecmp(cmd[2], cp = "References")){
622 np = hp->h_ref;
623 goto jlist;
624 }else if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
625 np = hp->h_in_reply_to;
626 goto jlist;
627 }else{
628 /* Primitive name normalization XXX header object should
629 * XXX have a more sophisticated accessible one */
630 char *xp;
632 cp = xp = savestr(cmd[2]);
633 xp[0] = upperchar(xp[0]);
634 while(*++xp != '\0')
635 xp[0] = lowerchar(xp[0]);
637 for(hfp = hp->h_user_headers;; hfp = hfp->hf_next){
638 if(hfp == NULL)
639 goto j501cp;
640 else if(!asccasecmp(cp, &hfp->hf_dat[0])){
641 fprintf(stdout, "210 %s\n", cp);
642 break;
647 }else if(!asccasecmp(cmd[1], "show")){
648 if(cmd[2] == NULL || cmd[3] != NULL)
649 goto jecmd;
650 if(!asccasecmp(cmd[2], "from")){
651 np = hp->h_from;
652 jshow:
653 if(np != NULL){
654 fprintf(stdout, "211 %s\n", cp);
656 fprintf(stdout, "%s %s\n", np->n_name, np->n_fullname);
657 while((np = np->n_flink) != NULL);
658 putc('\n', stdout);
659 }else
660 goto j501cp;
661 }else if(!asccasecmp(cmd[2], cp = "Sender")){
662 np = hp->h_sender;
663 goto jshow;
664 }else if(!asccasecmp(cmd[2], cp = "To")){
665 np = hp->h_to;
666 goto jshow;
667 }else if(!asccasecmp(cmd[2], cp = "Cc")){
668 np = hp->h_cc;
669 goto jshow;
670 }else if(!asccasecmp(cmd[2], cp = "Bcc")){
671 np = hp->h_bcc;
672 goto jshow;
673 }else if(!asccasecmp(cmd[2], cp = "Reply-To")){
674 np = hp->h_replyto;
675 goto jshow;
676 }else if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
677 np = hp->h_mft;
678 goto jshow;
679 }else if(!asccasecmp(cmd[2], cp = "Message-ID")){
680 np = hp->h_message_id;
681 goto jshow;
682 }else if(!asccasecmp(cmd[2], cp = "References")){
683 np = hp->h_ref;
684 goto jshow;
685 }else if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
686 np = hp->h_in_reply_to;
687 goto jshow;
688 }else if(!asccasecmp(cmd[2], cp = "Subject")){
689 if(hp->h_subject != NULL)
690 fprintf(stdout, "212 %s\n%s\n\n", cp, hp->h_subject);
691 else
692 fprintf(stdout, "501 %s\n", cp);
693 }else{
694 /* Primitive name normalization XXX header object should
695 * XXX have a more sophisticated accessible one */
696 bool_t any;
697 char *xp;
699 cp = xp = savestr(cmd[2]);
700 xp[0] = upperchar(xp[0]);
701 while(*++xp != '\0')
702 xp[0] = lowerchar(xp[0]);
704 for(any = FAL0, hfp = hp->h_user_headers; hfp != NULL;
705 hfp = hfp->hf_next){
706 if(!asccasecmp(cp, &hfp->hf_dat[0])){
707 if(!any)
708 fprintf(stdout, "212 %s\n", cp);
709 any = TRU1;
710 fprintf(stdout, "%s\n", &hfp->hf_dat[hfp->hf_nl +1]);
713 if(any)
714 putc('\n', stdout);
715 else
716 goto j501cp;
718 }else if(!asccasecmp(cmd[1], "remove")){
719 if(cmd[2] == NULL || cmd[3] != NULL)
720 goto jecmd;
721 if(!asccasecmp(cmd[2], "from")){
722 npp = &hp->h_from;
723 jrem:
724 if(*npp != NULL){
725 *npp = NULL;
726 fprintf(stdout, "210 %s\n", cp);
727 }else
728 goto j501cp;
729 }else if(!asccasecmp(cmd[2], cp = "Sender")){
730 npp = &hp->h_sender;
731 goto jrem;
732 }else if(!asccasecmp(cmd[2], cp = "To")){
733 npp = &hp->h_to;
734 goto jrem;
735 }else if(!asccasecmp(cmd[2], cp = "Cc")){
736 npp = &hp->h_cc;
737 goto jrem;
738 }else if(!asccasecmp(cmd[2], cp = "Bcc")){
739 npp = &hp->h_bcc;
740 goto jrem;
741 }else if(!asccasecmp(cmd[2], cp = "Reply-To")){
742 npp = &hp->h_replyto;
743 goto jrem;
744 }else if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
745 npp = &hp->h_mft;
746 goto jrem;
747 }else if(!asccasecmp(cmd[2], cp = "Message-ID")){
748 npp = &hp->h_message_id;
749 goto jrem;
750 }else if(!asccasecmp(cmd[2], cp = "References")){
751 npp = &hp->h_ref;
752 goto jrem;
753 }else if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
754 npp = &hp->h_in_reply_to;
755 goto jrem;
756 }else if(!asccasecmp(cmd[2], cp = "Subject")){
757 if(hp->h_subject != NULL){
758 hp->h_subject = NULL;
759 fprintf(stdout, "210 %s\n", cp);
760 }else
761 goto j501cp;
762 }else{
763 /* Primitive name normalization XXX header object should
764 * XXX have a more sophisticated accessible one */
765 struct n_header_field **hfpp;
766 bool_t any;
767 char *xp;
769 cp = xp = savestr(cmd[2]);
770 xp[0] = upperchar(xp[0]);
771 while(*++xp != '\0')
772 xp[0] = lowerchar(xp[0]);
774 for(any = FAL0, hfpp = &hp->h_user_headers; (hfp = *hfpp) != NULL;){
775 if(!asccasecmp(cp, &hfp->hf_dat[0])){
776 *hfpp = hfp->hf_next;
777 if(!any)
778 fprintf(stdout, "210 %s\n", cp);
779 any = TRU1;
780 }else
781 hfp = *(hfpp = &hfp->hf_next);
783 if(!any)
784 goto j501cp;
786 }else if(!asccasecmp(cmd[1], "insert")){ /* TODO LOGIC BELONGS head.c!
787 * TODO That is: Header::factory(string) -> object (blahblah).
788 * TODO I.e., as long as we don't have regular RFC compliant parsers
789 * TODO which differentiate in between structured and unstructured
790 * TODO header fields etc., a little workaround */
791 si8_t aerr;
792 enum expand_addr_check_mode eacm;
793 enum gfield ntype;
794 bool_t mult_ok;
796 mult_ok = TRU1;
797 ntype = GEXTRA | GFULL | GFULLEXTRA;
798 eacm = EACM_STRICT;
800 if(cmd[2] == NULL || cmd[3] == NULL)
801 goto jecmd;
802 if(!asccasecmp(cmd[2], "from")){
803 npp = &hp->h_from;
804 jins:
805 aerr = 0;
806 if((np = lextract(cmd[3], ntype)) == NULL)
807 goto j501cp;
808 else if((np = checkaddrs(np, eacm, &aerr), aerr != 0))
809 fprintf(stdout, "505 %s\n", cp);
810 else if(!mult_ok && (np->n_flink != NULL || *npp != NULL))
811 fprintf(stdout, "506 %s\n", cp);
812 else{
813 *npp = cat(*npp, np);
814 fprintf(stdout, "210 %s\n", cp);
816 }else if(!asccasecmp(cmd[2], cp = "Sender")){
817 mult_ok = FAL0;
818 npp = &hp->h_sender;
819 goto jins;
820 }else if(!asccasecmp(cmd[2], cp = "To")){
821 npp = &hp->h_to;
822 ntype = GTO | GFULL;
823 eacm = EACM_NORMAL | EAF_NAME;
824 goto jins;
825 }else if(!asccasecmp(cmd[2], cp = "Cc")){
826 npp = &hp->h_cc;
827 ntype = GCC | GFULL;
828 eacm = EACM_NORMAL | EAF_NAME;
829 goto jins;
830 }else if(!asccasecmp(cmd[2], cp = "Bcc")){
831 npp = &hp->h_bcc;
832 ntype = GCC | GFULL;
833 eacm = EACM_NORMAL | EAF_NAME;
834 goto jins;
835 }else if(!asccasecmp(cmd[2], cp = "Reply-To")){
836 npp = &hp->h_replyto;
837 goto jins;
838 }else if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
839 npp = &hp->h_mft;
840 eacm = EACM_NONAME;
841 goto jins;
842 }else if(!asccasecmp(cmd[2], cp = "Message-ID")){
843 mult_ok = FAL0;
844 npp = &hp->h_message_id;
845 eacm = EACM_NONAME;
846 goto jins;
847 }else if(!asccasecmp(cmd[2], cp = "References")){
848 npp = &hp->h_ref;
849 ntype = GREF;
850 eacm = EACM_NONAME;
851 goto jins;
852 }else if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
853 npp = &hp->h_in_reply_to;
854 ntype = GREF;
855 eacm = EACM_NONAME;
856 goto jins;
857 }else if(!asccasecmp(cmd[2], cp = "Subject")){
858 if(cmd[3][0] != '\0'){
859 if(hp->h_subject != NULL)
860 hp->h_subject = savecatsep(hp->h_subject, ' ', cmd[3]);
861 else
862 hp->h_subject = savestr(cmd[3]);
863 fprintf(stdout, "210 %s\n", cp);
864 }else
865 goto j501cp;
866 }else{
867 /* Primitive name normalization XXX header object should
868 * XXX have a more sophisticated accessible one */
869 size_t nl, bl;
870 struct n_header_field **hfpp;
872 for(cp = cmd[2]; *cp != '\0'; ++cp)
873 if(!fieldnamechar(*cp)){
874 cp = cmd[2];
875 goto j501cp;
878 for(hfpp = &hp->h_user_headers; *hfpp != NULL;)
879 hfpp = &(*hfpp)->hf_next;
881 nl = strlen(cp = cmd[2]);
882 bl = strlen(cmd[3]) +1;
883 *hfpp = hfp = salloc(n_VSTRUCT_SIZEOF(struct n_header_field, hf_dat
884 ) + nl +1 + bl);
885 hfp->hf_next = NULL;
886 hfp->hf_nl = nl;
887 hfp->hf_bl = bl - 1;
888 memcpy(hfp->hf_dat, cp, nl);
889 hfp->hf_dat[nl++] = '\0';
890 memcpy(hfp->hf_dat + nl, cmd[3], bl);
891 fprintf(stdout, "210 %s\n", cp);
893 }else
894 goto jecmd;
895 }else{
896 jecmd:
897 fputs("500\n", stdout);
898 ms = NULL;
901 jleave:
902 fflush(stdout);
903 NYD2_LEAVE;
904 return (ms != NULL);
906 j501cp:
907 fputs("501 ", stdout);
908 fputs(cp, stdout);
909 putc('\n', stdout);
910 goto jleave;
913 static void
914 _collint(int s)
916 NYD_X; /* Signal handler */
918 /* the control flow is subtle, because we can be called from ~q */
919 if (_coll_hadintr == 0) {
920 if (ok_blook(ignore)) {
921 fputs("@\n", stdout);
922 fflush(stdout);
923 clearerr(stdin);
924 } else
925 _coll_hadintr = 1;
926 siglongjmp(_coll_jmp, 1);
928 exit_status |= EXIT_SEND_ERROR;
929 if (s != 0)
930 savedeadletter(_coll_fp, TRU1);
931 /* Aborting message, no need to fflush() .. */
932 siglongjmp(_coll_abort, 1);
935 static void
936 collhup(int s)
938 NYD_X; /* Signal handler */
939 n_UNUSED(s);
941 savedeadletter(_coll_fp, TRU1);
942 /* Let's pretend nobody else wants to clean up, a true statement at
943 * this time */
944 exit(EXIT_ERR);
947 static int
948 putesc(char const *s, FILE *stream)
950 int n = 0, rv = -1;
951 NYD_ENTER;
953 while (s[0] != '\0') {
954 if (s[0] == '\\') {
955 if (s[1] == 't') {
956 if (putc('\t', stream) == EOF)
957 goto jleave;
958 ++n;
959 s += 2;
960 continue;
962 if (s[1] == 'n') {
963 if (putc('\n', stream) == EOF)
964 goto jleave;
965 ++n;
966 s += 2;
967 continue;
970 if (putc(s[0], stream) == EOF)
971 goto jleave;
972 ++n;
973 ++s;
975 if (putc('\n', stream) == EOF)
976 goto jleave;
977 rv = ++n;
978 jleave:
979 NYD_LEAVE;
980 return rv;
983 static void
984 a_coll__hook_setter(void *arg){ /* TODO v15: drop */
985 struct header *hp;
986 char const *val;
987 NYD2_ENTER;
989 hp = arg;
991 if((val = detract(hp->h_from, GNAMEONLY)) == NULL)
992 val = n_empty;
993 ok_vset(compose_from, val);
994 if((val = detract(hp->h_sender, 0)) == NULL)
995 val = n_empty;
996 ok_vset(compose_sender, val);
997 if((val = detract(hp->h_to, GNAMEONLY)) == NULL)
998 val = n_empty;
999 ok_vset(compose_to, val);
1000 if((val = detract(hp->h_cc, GNAMEONLY)) == NULL)
1001 val = n_empty;
1002 ok_vset(compose_cc, val);
1003 if((val = detract(hp->h_bcc, GNAMEONLY)) == NULL)
1004 val = n_empty;
1005 ok_vset(compose_bcc, val);
1006 if((val = hp->h_subject) == NULL)
1007 val = n_empty;
1008 ok_vset(compose_subject, val);
1009 NYD2_LEAVE;
1012 static int
1013 a_coll_ocds__mac(void){
1014 /* Executes in a fork(2)ed child */
1015 setvbuf(stdout, NULL, _IOLBF, 0);
1016 pstate |= PS_COMPOSE_FORKHOOK;
1017 temporary_call_compose_mode_hook(a_coll_ocds__macname, NULL, NULL);
1018 _exit(EXIT_OK);
1021 static void
1022 a_coll_ocds__finalize(void *vp){
1023 /* Note we use this for destruction upon setup errors, thus */
1024 sighandler_type opipe;
1025 sighandler_type oint;
1026 struct a_coll_ocds_arg **coapp, *coap;
1027 NYD2_ENTER;
1029 temporary_call_compose_mode_hook((char*)-1, NULL, NULL);
1031 coap = *(coapp = vp);
1032 *coapp = (struct a_coll_ocds_arg*)-1;
1034 if(coap->coa_stdout != NULL)
1035 if(!Pclose(coap->coa_stdout, FAL0)){
1036 *coap->coa_senderr = TRU1;
1037 n_err(_("*on-compose-done(-shell)?* failed: %s\n"),
1038 n_shexp_quote_cp(coap->coa_cmd, FAL0));
1041 if(coap->coa_stdin != NULL)
1042 Fclose(coap->coa_stdin);
1043 else if(coap->coa_pipe[0] != -1)
1044 close(coap->coa_pipe[0]);
1046 if(coap->coa_pipe[1] != -1)
1047 close(coap->coa_pipe[1]);
1049 opipe = coap->coa_opipe;
1050 oint = coap->coa_oint;
1052 n_lofi_free(coap);
1054 hold_all_sigs();
1055 safe_signal(SIGPIPE, opipe);
1056 safe_signal(SIGINT, oint);
1057 rele_all_sigs();
1058 NYD2_LEAVE;
1061 FL FILE *
1062 collect(struct header *hp, int printheaders, struct message *mp,
1063 char *quotefile, int doprefix, si8_t *checkaddr_err)
1065 struct n_ignore const *quoteitp;
1066 struct a_coll_ocds_arg *coap;
1067 int lc, cc, c;
1068 int volatile t, eofcnt, getfields;
1069 char *linebuf, escape_saved, escape;
1070 char const *cp, *coapm;
1071 size_t i, linesize; /* TODO line pool */
1072 long cnt;
1073 enum sendaction action;
1074 sigset_t oset, nset;
1075 FILE * volatile sigfp;
1076 NYD_ENTER;
1078 _coll_fp = NULL;
1079 sigfp = NULL;
1080 linesize = 0;
1081 linebuf = NULL;
1082 eofcnt = 0;
1083 coapm = NULL;
1084 coap = NULL;
1086 /* Start catching signals from here, but we're still die on interrupts
1087 * until we're in the main loop */
1088 sigfillset(&nset);
1089 sigprocmask(SIG_BLOCK, &nset, &oset);
1090 /* FIXME have dropped handlerpush() and localized onintr() in lex.c! */
1091 if ((_coll_saveint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
1092 safe_signal(SIGINT, &_collint);
1093 if ((_coll_savehup = safe_signal(SIGHUP, SIG_IGN)) != SIG_IGN)
1094 safe_signal(SIGHUP, collhup);
1095 if (sigsetjmp(_coll_abort, 1))
1096 goto jerr;
1097 if (sigsetjmp(_coll_jmp, 1))
1098 goto jerr;
1099 pstate |= PS_COMPOSE_MODE;
1100 sigprocmask(SIG_SETMASK, &oset, (sigset_t*)NULL);
1102 if ((_coll_fp = Ftmp(NULL, "collect", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
1103 NULL) {
1104 n_perr(_("temporary mail file"), 0);
1105 goto jerr;
1108 /* If we are going to prompt for a subject, refrain from printing a newline
1109 * after the headers (since some people mind) */
1110 getfields = 0;
1111 if (!(options & OPT_t_FLAG)) {
1112 t = GTO | GSUBJECT | GCC | GNL;
1113 if (ok_blook(fullnames))
1114 t |= GCOMMA;
1116 if (options & OPT_INTERACTIVE) {
1117 if (hp->h_subject == NULL && (ok_blook(ask) || ok_blook(asksub)))
1118 t &= ~GNL, getfields |= GSUBJECT;
1120 if (hp->h_to == NULL)
1121 t &= ~GNL, getfields |= GTO;
1123 if (!ok_blook(bsdcompat) && !ok_blook(askatend)) {
1124 if (hp->h_bcc == NULL && ok_blook(askbcc))
1125 t &= ~GNL, getfields |= GBCC;
1126 if (hp->h_cc == NULL && ok_blook(askcc))
1127 t &= ~GNL, getfields |= GCC;
1130 } else {
1131 n_UNINIT(t, 0);
1134 escape_saved = escape = ((cp = ok_vlook(escape)) != NULL) ? *cp : n_ESCAPE;
1135 _coll_hadintr = 0;
1137 if (!sigsetjmp(_coll_jmp, 1)) {
1138 /* Ask for some headers first, as necessary */
1139 if (getfields)
1140 grab_headers(n_LEXINPUT_CTX_COMPOSE, hp, getfields, 1);
1142 /* Execute compose-enter TODO completely v15-compat intermediate!! */
1143 if((cp = ok_vlook(on_compose_enter)) != NULL){
1144 setup_from_and_sender(hp);
1145 temporary_call_compose_mode_hook(cp, &a_coll__hook_setter, hp);
1148 /* Cannot do since it may require turning this into a multipart one */
1149 if(!(options & OPT_Mm_FLAG)){
1150 char const *cp_obsolete = ok_vlook(NAIL_HEAD);
1151 if(cp_obsolete != NULL)
1152 OBSOLETE(_("please use *message-inject-head* "
1153 "instead of *NAIL_HEAD*"));
1155 if(((cp = ok_vlook(message_inject_head)) != NULL ||
1156 (cp = cp_obsolete) != NULL) && putesc(cp, _coll_fp) < 0)
1157 goto jerr;
1159 /* Quote an original message */
1160 if (mp != NULL && (doprefix || (cp = ok_vlook(quote)) != NULL)) {
1161 quoteitp = n_IGNORE_ALL;
1162 action = SEND_QUOTE;
1163 if (doprefix) {
1164 quoteitp = n_IGNORE_FWD;
1165 if ((cp = ok_vlook(fwdheading)) == NULL)
1166 cp = "-------- Original Message --------";
1167 if (*cp != '\0' && fprintf(_coll_fp, "%s\n", cp) < 0)
1168 goto jerr;
1169 } else if (!strcmp(cp, "noheading")) {
1170 /*EMPTY*/;
1171 } else if (!strcmp(cp, "headers")) {
1172 quoteitp = n_IGNORE_TYPE;
1173 } else if (!strcmp(cp, "allheaders")) {
1174 quoteitp = NULL;
1175 action = SEND_QUOTE_ALL;
1176 } else {
1177 cp = hfield1("from", mp);
1178 if (cp != NULL && (cnt = (long)strlen(cp)) > 0) {
1179 if (xmime_write(cp, cnt, _coll_fp, CONV_FROMHDR, TD_NONE) < 0)
1180 goto jerr;
1181 if (fprintf(_coll_fp, _(" wrote:\n\n")) < 0)
1182 goto jerr;
1185 if (fflush(_coll_fp))
1186 goto jerr;
1187 if (doprefix)
1188 cp = NULL;
1189 else if ((cp = ok_vlook(indentprefix)) == NULL)
1190 cp = INDENT_DEFAULT;
1191 if (sendmp(mp, _coll_fp, quoteitp, cp, action, NULL) < 0)
1192 goto jerr;
1196 if (quotefile != NULL) {
1197 if (_include_file(quotefile, &lc, &cc, FAL0) != 0)
1198 goto jerr;
1201 if ((options & (OPT_Mm_FLAG | OPT_INTERACTIVE)) == OPT_INTERACTIVE) {
1202 /* Print what we have sofar also on the terminal (if useful) */
1203 if (!ok_blook(editalong)) {
1204 if (printheaders)
1205 puthead(TRU1, hp, stdout, t, SEND_TODISP, CONV_NONE, NULL, NULL);
1207 rewind(_coll_fp);
1208 while ((c = getc(_coll_fp)) != EOF) /* XXX bytewise, yuck! */
1209 putc(c, stdout);
1210 if (fseek(_coll_fp, 0, SEEK_END))
1211 goto jerr;
1212 } else {
1213 rewind(_coll_fp);
1214 mesedit('e', hp);
1215 /* As mandated by the Mail Reference Manual, print "(continue)" */
1216 jcont:
1217 if(options & OPT_INTERACTIVE)
1218 fputs(_("(continue)\n"), stdout);
1220 fflush(stdout);
1222 } else {
1223 /* Come here for printing the after-signal message. Duplicate messages
1224 * won't be printed because the write is aborted if we get a SIGTTOU */
1225 if (_coll_hadintr)
1226 n_err(_("\n(Interrupt -- one more to kill letter)\n"));
1229 /* If not under shell hook control */
1230 if(coap == NULL){
1231 /* We're done with -M or -m (because we are too simple minded) */
1232 if(options & OPT_Mm_FLAG)
1233 goto jout;
1234 /* No command escapes, interrupts not expected. Simply copy STDIN */
1235 if (!(options & (OPT_INTERACTIVE | OPT_t_FLAG | OPT_TILDE_FLAG))){
1236 linebuf = srealloc(linebuf, linesize = LINESIZE);
1237 while ((i = fread(linebuf, sizeof *linebuf, linesize, stdin)) > 0) {
1238 if (i != fwrite(linebuf, sizeof *linebuf, i, _coll_fp))
1239 goto jerr;
1241 goto jout;
1245 /* The interactive collect loop */
1246 for(;;){
1247 /* C99 */{
1248 enum n_lexinput_flags lif;
1250 lif = n_LEXINPUT_CTX_COMPOSE;
1251 if(options & (OPT_INTERACTIVE | OPT_TILDE_FLAG)){
1252 if(!(options & OPT_t_FLAG))
1253 lif |= n_LEXINPUT_NL_ESC;
1255 cnt = n_lex_input(lif, n_empty, &linebuf, &linesize, NULL);
1258 if (cnt < 0) {
1259 if(coap != NULL)
1260 break;
1261 if (options & OPT_t_FLAG) {
1262 fflush_rewind(_coll_fp);
1263 /* It is important to set PS_t_FLAG before extract_header() *and*
1264 * keep OPT_t_FLAG for the first parse of the message, too! */
1265 pstate |= PS_t_FLAG;
1266 if (makeheader(_coll_fp, hp, checkaddr_err) != OKAY)
1267 goto jerr;
1268 options &= ~OPT_t_FLAG;
1269 continue;
1270 } else if ((options & OPT_INTERACTIVE) &&
1271 ok_blook(ignoreeof) && ++eofcnt < 4) {
1272 printf(_("*ignoreeof* set, use `~.' to terminate letter\n"));
1273 continue;
1275 break;
1278 _coll_hadintr = 0;
1280 cp = linebuf;
1281 if(cnt == 0)
1282 goto jputnl;
1283 else if(coap == NULL){
1284 if(!(options & (OPT_INTERACTIVE | OPT_TILDE_FLAG)))
1285 goto jputline;
1286 else if(cp[0] == '.'){
1287 if(cnt == 1 && (ok_blook(dot) || ok_blook(ignoreeof)))
1288 break;
1291 if(cp[0] != escape){
1292 jputline:
1293 if(fwrite(cp, sizeof *cp, cnt, _coll_fp) != (size_t)cnt)
1294 goto jerr;
1295 /* TODO PS_READLINE_NL is a terrible hack to ensure that _in_all_-
1296 * TODO _code_paths_ a file without trailing newline isn't modified
1297 * TODO to continue one; the "saw-newline" needs to be part of an
1298 * TODO I/O input machinery object */
1299 jputnl:
1300 if(pstate & PS_READLINE_NL){
1301 if(putc('\n', _coll_fp) == EOF)
1302 goto jerr;
1304 continue;
1307 /* Cleanup the input string: like this we can perform a little bit of
1308 * usage testing and also have somewhat normalized history entries */
1309 for(cp = &linebuf[2]; (c = *cp) != '\0' && blankspacechar(c); ++cp)
1310 continue;
1311 if(c == '\0'){
1312 linebuf[2] = '\0';
1313 cnt = 2;
1314 }else{
1315 i = PTR2SIZE(cp - linebuf) - 3;
1316 memmove(&linebuf[3], cp, (cnt -= i));
1317 linebuf[2] = ' ';
1318 linebuf[cnt] = '\0';
1320 if(cnt > 0){ /* TODO v15 no more trailing WS from lex_input please */
1321 cp = &linebuf[cnt];
1323 for(;; --cp){
1324 c = cp[-1];
1325 if(!blankspacechar(c))
1326 break;
1328 ((char*)n_UNCONST(cp))[0] = '\0';
1329 cnt = PTR2SIZE(cp - linebuf);
1333 switch((c = linebuf[1])){
1334 default:
1335 /* On double escape, send a single one. Otherwise, it's an error */
1336 if(c == escape){
1337 cp = &linebuf[1];
1338 --cnt;
1339 goto jputline;
1340 }else{
1341 char buf[sizeof(n_UNIREPL)];
1343 if(asciichar(c))
1344 buf[0] = c, buf[1] = '\0';
1345 else if(options & OPT_UNICODE)
1346 memcpy(buf, n_unirepl, sizeof n_unirepl);
1347 else
1348 buf[0] = '?', buf[1] = '\0';
1349 n_err(_("Unknown command escape: ~%s\n"), buf);
1350 continue;
1352 jearg:
1353 n_err(_("Invalid command escape usage: %s\n"), linebuf);
1354 continue;
1355 case '!':
1356 /* Shell escape, send the balance of line to sh -c */
1357 if(cnt == 2 || coap != NULL)
1358 goto jearg;
1359 c_shell(&linebuf[3]);
1360 goto jhistcont;
1361 case ':':
1362 /* FALLTHRU */
1363 case '_':
1364 /* Escape to command mode, but be nice! *//* TODO command expansion
1365 * TODO should be handled here so that we have unique history! */
1366 if(cnt == 2)
1367 goto jearg;
1368 _execute_command(hp, &linebuf[3], cnt -= 3);
1369 break;
1370 case '.':
1371 /* Simulate end of file on input */
1372 if(cnt != 2 || coap != NULL)
1373 goto jearg;
1374 goto jout;
1375 case 'x':
1376 /* Same as 'q', but no *DEAD* saving */
1377 /* FALLTHRU */
1378 case 'q':
1379 /* Force a quit, act like an interrupt had happened */
1380 if(cnt != 2)
1381 goto jearg;
1382 ++_coll_hadintr;
1383 _collint((c == 'x') ? 0 : SIGINT);
1384 exit(EXIT_ERR);
1385 /*NOTREACHED*/
1386 case 'h':
1387 /* Grab a bunch of headers */
1388 if(cnt != 2)
1389 goto jearg;
1391 grab_headers(n_LEXINPUT_CTX_COMPOSE, hp,
1392 (GTO | GSUBJECT | GCC | GBCC),
1393 (ok_blook(bsdcompat) && ok_blook(bsdorder)));
1394 while(hp->h_to == NULL);
1395 break;
1396 case 'H':
1397 /* Grab extra headers */
1398 if(cnt != 2)
1399 goto jearg;
1401 grab_headers(n_LEXINPUT_CTX_COMPOSE, hp, GEXTRA, 0);
1402 while(check_from_and_sender(hp->h_from, hp->h_sender) == NULL);
1403 break;
1404 case 't':
1405 /* Add to the To: list */
1406 if(cnt == 2)
1407 goto jearg;
1408 hp->h_to = cat(hp->h_to,
1409 checkaddrs(lextract(&linebuf[3], GTO | GFULL), EACM_NORMAL,
1410 NULL));
1411 break;
1412 case 's':
1413 /* Set the Subject list */
1414 if(cnt == 2)
1415 goto jearg;
1416 /* Subject:; take care for Debian #419840 and strip any \r and \n */
1417 if(n_anyof_cp("\n\r", hp->h_subject = savestr(&linebuf[3]))){
1418 char *xp;
1420 n_err(_("-s: normalizing away invalid ASCII NL / CR bytes\n"));
1421 for(xp = hp->h_subject; *xp != '\0'; ++xp)
1422 if(*xp == '\n' || *xp == '\r')
1423 *xp = ' ';
1425 break;
1426 case '@':
1427 /* Edit the attachment list */
1428 if(cnt != 2)
1429 append_attachments(n_LEXINPUT_CTX_COMPOSE, &hp->h_attach,
1430 &linebuf[3]);
1431 else
1432 edit_attachments(n_LEXINPUT_CTX_COMPOSE, &hp->h_attach);
1433 break;
1434 case 'c':
1435 /* Add to the CC list */
1436 if(cnt == 2)
1437 goto jearg;
1438 hp->h_cc = cat(hp->h_cc,
1439 checkaddrs(lextract(&linebuf[3], GCC | GFULL), EACM_NORMAL,
1440 NULL));
1441 break;
1442 case 'b':
1443 /* Add stuff to blind carbon copies list */
1444 if(cnt == 2)
1445 goto jearg;
1446 hp->h_bcc = cat(hp->h_bcc,
1447 checkaddrs(lextract(&linebuf[3], GBCC | GFULL), EACM_NORMAL,
1448 NULL));
1449 break;
1450 case 'd':
1451 if(cnt != 2)
1452 goto jearg;
1453 cp = n_getdeadletter();
1454 if(0){
1455 /*FALLTHRU*/
1456 case 'R':
1457 case 'r':
1458 case '<':
1459 /* Invoke a file: Search for the file name, then open it and copy
1460 * the contents to _coll_fp */
1461 if(cnt == 2){
1462 n_err(_("Interpolate what file?\n"));
1463 break;
1465 if(*(cp = &linebuf[3]) == '!'){
1466 insertcommand(_coll_fp, ++cp);
1467 goto jhistcont;
1469 if((cp = fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO)) == NULL)
1470 break;
1472 if(is_dir(cp)){
1473 n_err(_("%s: is a directory\n"), n_shexp_quote_cp(cp, FAL0));
1474 break;
1476 if(_include_file(cp, &lc, &cc, (c == 'R')) != 0){
1477 if(ferror(_coll_fp))
1478 goto jerr;
1479 break;
1481 printf(_("%s %d/%d\n"), n_shexp_quote_cp(cp, FAL0), lc, cc);
1482 break;
1483 case 'i':
1484 /* Insert a variable into the file */
1485 if(cnt == 2)
1486 goto jearg;
1487 if((cp = vok_vlook(&linebuf[3])) == NULL || *cp == '\0')
1488 break;
1489 if(putesc(cp, _coll_fp) < 0) /* TODO v15: user resp upon `set' time */
1490 goto jerr;
1491 if((options & OPT_INTERACTIVE) && putesc(cp, stdout) < 0)
1492 goto jerr;
1493 break;
1494 case 'a':
1495 case 'A':
1496 /* Insert the contents of a signature variable */
1497 if(cnt != 2)
1498 goto jearg;
1499 cp = (c == 'a') ? ok_vlook(sign) : ok_vlook(Sign);
1500 if(cp != NULL && *cp != '\0'){
1501 if(putesc(cp, _coll_fp) < 0) /* TODO v15: user upon `set' time */
1502 goto jerr;
1503 if((options & OPT_INTERACTIVE) && putesc(cp, stdout) < 0)
1504 goto jerr;
1506 break;
1507 case 'w':
1508 /* Write the message on a file */
1509 if(cnt == 2)
1510 goto jearg;
1511 if((cp = fexpand(&linebuf[3], FEXP_LOCAL | FEXP_NOPROTO)) == NULL){
1512 n_err(_("Write what file!?\n"));
1513 break;
1515 rewind(_coll_fp);
1516 if(exwrite(cp, _coll_fp, 1) < 0)
1517 goto jerr;
1518 break;
1519 case 'm':
1520 case 'M':
1521 case 'f':
1522 case 'F':
1523 case 'u':
1524 case 'U':
1525 /* Interpolate the named messages, if we are in receiving mail mode.
1526 * Does the standard list processing garbage. If ~f is given, we
1527 * don't shift over */
1528 if(cnt == 2)
1529 goto jearg;
1530 if(forward(&linebuf[3], _coll_fp, c) < 0)
1531 break;
1532 break;
1533 case 'p':
1534 /* Print current state of the message without altering anything */
1535 if(cnt != 2)
1536 goto jearg;
1537 print_collf(_coll_fp, hp, ((options & OPT_INTERACTIVE) != 0));
1538 break;
1539 case '|':
1540 /* Pipe message through command. Collect output as new message */
1541 if(cnt == 2)
1542 goto jearg;
1543 rewind(_coll_fp);
1544 mespipe(&linebuf[3]);
1545 goto jhistcont;
1546 case 'v':
1547 case 'e':
1548 /* Edit the current message. 'e' -> use EDITOR, 'v' -> use VISUAL */
1549 if(cnt != 2 || coap != NULL)
1550 goto jearg;
1551 rewind(_coll_fp);
1552 mesedit(c, ok_blook(editheaders) ? hp : NULL);
1553 goto jhistcont;
1554 case '^':
1555 if(!a_collect_plumbing(&linebuf[3], hp))
1556 goto jearg;
1557 if(options & OPT_INTERACTIVE)
1558 break;
1559 continue;
1560 case '?':
1561 /* Last the lengthy help string. (Very ugly, but take care for
1562 * compiler supported string lengths :() */
1563 puts(_(
1564 "COMMAND ESCAPES (to be placed after a newline) excerpt:\n"
1565 "~. Commit and send message\n"
1566 "~: <command> Execute a mail command\n"
1567 "~<! <command> Insert output of command\n"
1568 "~@ [<files>] Edit attachment list\n"
1569 "~A Insert *Sign* variable (`~a': insert *sign*)\n"
1570 "~c <users> Add users to Cc: list (`~b': to Bcc:)\n"
1571 "~d Read in $DEAD (dead.letter)\n"
1572 "~e Edit message via $EDITOR"
1574 puts(_(
1575 "~F <msglist> Read in with headers, don't *indentprefix* lines\n"
1576 "~f <msglist> Like ~F, but honour `ignore' / `retain' configuration\n"
1577 "~H Edit From:, Reply-To: and Sender:\n"
1578 "~h Prompt for Subject:, To:, Cc: and \"blind\" Bcc:\n"
1579 "~i <variable> Insert a value and a newline\n"
1580 "~M <msglist> Read in with headers, *indentprefix* (`~m': `retain' etc.)\n"
1581 "~p Show current message compose buffer\n"
1582 "~r <file> Read in a file (`~R': *indentprefix* lines)"
1584 puts(_(
1585 "~s <subject> Set Subject:\n"
1586 "~t <users> Add users to To: list\n"
1587 "~u <msglist> Read in message(s) without headers (`~U': indent lines)\n"
1588 "~v Edit message via $VISUAL\n"
1589 "~w <file> Write message onto file\n"
1590 "~x Abort composition, discard message (`~q': save in $DEAD)\n"
1591 "~| <command> Pipe message through shell filter"
1593 if(cnt != 2)
1594 goto jearg;
1595 break;
1598 /* Finally place an entry in history as applicable */
1599 if(0){
1600 jhistcont:
1601 c = '\1';
1602 }else
1603 c = '\0';
1604 if(options & OPT_INTERACTIVE)
1605 n_tty_addhist(linebuf, TRU1);
1606 if(c != '\0')
1607 goto jcont;
1610 jout:
1611 /* Do we have *on-compose-done-shell*, or *on-compose-done*?
1612 * TODO Usual f...ed up state of signals and terminal etc. */
1613 if(coap == NULL && (cp = ok_vlook(on_compose_done_shell)) != NULL) Jocds:{
1614 union {int (*ptf)(void); char const *sh;} u;
1615 char const *cmd;
1617 /* Reset *escape* to be available and guaranteed! */
1618 escape = n_ESCAPE;
1620 if(coapm != NULL){
1621 u.ptf = &a_coll_ocds__mac;
1622 cmd = (char*)-1;
1623 a_coll_ocds__macname = cp = coapm;
1624 }else{
1625 u.sh = ok_vlook(SHELL);
1626 cmd = cp;
1629 i = strlen(cp) +1;
1630 coap = n_lofi_alloc(n_VSTRUCT_SIZEOF(struct a_coll_ocds_arg, coa_cmd
1631 ) + i);
1632 coap->coa_pipe[0] = coap->coa_pipe[1] = -1;
1633 coap->coa_stdin = coap->coa_stdout = NULL;
1634 coap->coa_senderr = checkaddr_err;
1635 memcpy(coap->coa_cmd, cp, i);
1637 hold_all_sigs();
1638 coap->coa_opipe = safe_signal(SIGPIPE, SIG_IGN);
1639 coap->coa_oint = safe_signal(SIGINT, SIG_IGN);
1640 rele_all_sigs();
1642 if(pipe_cloexec(coap->coa_pipe) != -1 &&
1643 (coap->coa_stdin = Fdopen(coap->coa_pipe[0], "r", FAL0)) != NULL &&
1644 (coap->coa_stdout = Popen(cmd, "W", u.sh, NULL, coap->coa_pipe[1])
1645 ) != NULL){
1646 close(coap->coa_pipe[1]);
1647 coap->coa_pipe[1] = -1;
1649 temporary_call_compose_mode_hook(NULL, NULL, NULL);
1650 n_source_slice_hack(coap->coa_cmd, coap->coa_stdin, coap->coa_stdout,
1651 (options & ~OPT_INTERACTIVE), &a_coll_ocds__finalize, &coap);
1652 /* Hook version protocol for ~^: update manual upon change! */
1653 fputs(a_COLL_PLUMBING_VERSION "\n", coap->coa_stdout);
1654 #undef a_COLL_PLUMBING_VERSION
1655 goto jcont;
1658 c = errno;
1659 a_coll_ocds__finalize(coap);
1660 n_perr(_("Cannot invoke *on-compose-done(-shell)?*"), c);
1661 goto jerr;
1663 if(*checkaddr_err != 0){
1664 *checkaddr_err = 0;
1665 goto jerr;
1667 if(coapm == NULL && (coapm = ok_vlook(on_compose_done)) != NULL)
1668 goto Jocds;
1669 escape = escape_saved;
1671 /* Final chance to edit headers, if not already above */
1672 if (ok_blook(bsdcompat) || ok_blook(askatend)) {
1673 if (hp->h_cc == NULL && ok_blook(askcc))
1674 grab_headers(n_LEXINPUT_CTX_COMPOSE, hp, GCC, 1);
1675 if (hp->h_bcc == NULL && ok_blook(askbcc))
1676 grab_headers(n_LEXINPUT_CTX_COMPOSE, hp, GBCC, 1);
1678 if (hp->h_attach == NULL && ok_blook(askattach))
1679 edit_attachments(n_LEXINPUT_CTX_COMPOSE, &hp->h_attach);
1681 /* Add automatic receivers */
1682 if ((cp = ok_vlook(autocc)) != NULL && *cp != '\0')
1683 hp->h_cc = cat(hp->h_cc, checkaddrs(lextract(cp, GCC | GFULL),
1684 EACM_NORMAL, checkaddr_err));
1685 if ((cp = ok_vlook(autobcc)) != NULL && *cp != '\0')
1686 hp->h_bcc = cat(hp->h_bcc, checkaddrs(lextract(cp, GBCC | GFULL),
1687 EACM_NORMAL, checkaddr_err));
1688 if (*checkaddr_err != 0)
1689 goto jerr;
1691 /* Execute compose-leave */
1692 if((cp = ok_vlook(on_compose_leave)) != NULL){
1693 setup_from_and_sender(hp);
1694 temporary_call_compose_mode_hook(cp, &a_coll__hook_setter, hp);
1697 /* TODO Cannot do since it may require turning this into a multipart one */
1698 if(options & OPT_Mm_FLAG)
1699 goto jskiptails;
1701 /* Place signature? */
1702 if((cp = ok_vlook(signature)) != NULL && *cp != '\0'){
1703 char const *cpq;
1705 if((cpq = fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO)) == NULL){
1706 n_err(_("*signature* expands to invalid file: %s\n"),
1707 n_shexp_quote_cp(cp, FAL0));
1708 goto jerr;
1710 cpq = n_shexp_quote_cp(cp = cpq, FAL0);
1712 if((sigfp = Fopen(cp, "r")) == NULL){
1713 n_err(_("Can't open *signature* %s: %s\n"), cpq, strerror(errno));
1714 goto jerr;
1717 if(linebuf == NULL)
1718 linebuf = smalloc(linesize = LINESIZE);
1719 c = '\0';
1720 while((i = fread(linebuf, sizeof *linebuf, linesize, n_UNVOLATILE(sigfp)))
1721 > 0){
1722 c = linebuf[i - 1];
1723 if(i != fwrite(linebuf, sizeof *linebuf, i, _coll_fp))
1724 goto jerr;
1727 /* C99 */{
1728 FILE *x = n_UNVOLATILE(sigfp);
1729 int e = errno, ise = ferror(x);
1731 sigfp = NULL;
1732 Fclose(x);
1734 if(ise){
1735 n_err(_("Errors while reading *signature* %s: %s\n"),
1736 cpq, strerror(e));
1737 goto jerr;
1741 if(c != '\0' && c != '\n')
1742 putc('\n', _coll_fp);
1745 { char const *cp_obsolete = ok_vlook(NAIL_TAIL);
1747 if(cp_obsolete != NULL)
1748 OBSOLETE(_("please use *message-inject-tail* instead of *NAIL_TAIL*"));
1750 if((cp = ok_vlook(message_inject_tail)) != NULL ||
1751 (cp = cp_obsolete) != NULL){
1752 if(putesc(cp, _coll_fp) < 0)
1753 goto jerr;
1754 if((options & OPT_INTERACTIVE) && putesc(cp, stdout) < 0)
1755 goto jerr;
1759 jskiptails:
1760 if(fflush(_coll_fp))
1761 goto jerr;
1762 rewind(_coll_fp);
1764 jleave:
1765 temporary_unroll_compose_mode();
1766 if (linebuf != NULL)
1767 free(linebuf);
1768 sigfillset(&nset);
1769 sigprocmask(SIG_BLOCK, &nset, NULL);
1770 pstate &= ~PS_COMPOSE_MODE;
1771 safe_signal(SIGINT, _coll_saveint);
1772 safe_signal(SIGHUP, _coll_savehup);
1773 sigprocmask(SIG_SETMASK, &oset, NULL);
1774 NYD_LEAVE;
1775 return _coll_fp;
1777 jerr:
1778 if(coap != NULL && coap != (struct a_coll_ocds_arg*)-1)
1779 n_source_slice_hack_remove_after_jump();
1780 if(sigfp != NULL)
1781 Fclose(n_UNVOLATILE(sigfp));
1782 if (_coll_fp != NULL) {
1783 Fclose(_coll_fp);
1784 _coll_fp = NULL;
1786 assert(checkaddr_err != NULL);
1787 /* TODO We don't save in $DEAD upon error because msg not readily composed?
1788 * TODO But this no good, it should go ZOMBIE / DRAFT / POSTPONED or what! */
1789 if(*checkaddr_err != 0)
1790 n_err(_("Some addressees were classified as \"hard error\"\n"));
1791 else if(_coll_hadintr == 0){
1792 *checkaddr_err = TRU1; /* TODO ugly: "sendout_error" now.. */
1793 n_err(_("Failed to prepare composed message (I/O error, disk full?)\n"));
1795 goto jleave;
1798 /* s-it-mode */