sopen(): add missing space to error message
[s-mailx.git] / collect.c
blobe3914a06f07a2d392c81692e26663631cef14f89
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 */
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 /* If *interactive* is set and *doecho* is, too, also dump to *stdout* */
69 static int _include_file(char const *name, int *linecount,
70 int *charcount, bool_t doecho, bool_t indent);
72 static void _collect_onpipe(int signo);
74 /* Execute cmd and insert its standard output into fp */
75 static void insertcommand(FILE *fp, char const *cmd);
77 /* ~p command */
78 static void print_collf(FILE *collf, struct header *hp, bool_t pagerok);
80 /* Write a file, ex-like if f set */
81 static int exwrite(char const *name, FILE *fp, int f);
83 /* Parse off the message header from fp and store relevant fields in hp,
84 * replace _coll_fp with a shiny new version without any header */
85 static enum okay makeheader(FILE *fp, struct header *hp, si8_t *checkaddr_err);
87 /* Edit the message being collected on fp. On return, make the edit file the
88 * new temp file */
89 static void mesedit(int c, struct header *hp);
91 /* Pipe the message through the command. Old message is on stdin of command,
92 * new message collected from stdout. Shell must return 0 to accept new msg */
93 static void mespipe(char *cmd);
95 /* Interpolate the named messages into the current message, possibly doing
96 * indent stuff. The flag argument is one of the command escapes: [mMfFuU].
97 * Return a count of the number of characters now in the message, or -1 if an
98 * error is encountered writing the message temporary */
99 static int forward(char *ms, FILE *fp, int f);
101 /* ~^ mode */
102 static bool_t a_collect_plumbing(char const *ms, struct header *p);
104 /* On interrupt, come here to save the partial message in ~/dead.letter.
105 * Then jump out of the collection loop */
106 static void _collint(int s);
108 static void collhup(int s);
110 static int putesc(char const *s, FILE *stream); /* TODO wysh set! */
112 /* temporary_call_compose_mode_hook() setter hook */
113 static void a_coll__hook_setter(void *arg);
115 /* *on-compose-done-shell* finalizer */
116 static void a_coll_ocds__finalize(void *vp);
118 static void
119 _execute_command(struct header *hp, char const *linebuf, size_t linesize){
120 /* The problem arises if there are rfc822 message attachments and the
121 * user uses `~:' to change the current file. TODO Unfortunately we
122 * TODO cannot simply keep a pointer to, or increment a reference count
123 * TODO of the current `file' (mailbox that is) object, because the
124 * TODO codebase doesn't deal with that at all; so, until some far
125 * TODO later time, copy the name of the path, and warn the user if it
126 * TODO changed; we COULD use the AC_TMPFILE attachment type, i.e.,
127 * TODO copy the message attachments over to temporary files, but that
128 * TODO would require more changes so that the user still can recognize
129 * TODO in `~@' etc. that its a rfc822 message attachment; see below */
130 struct n_sigman sm;
131 struct attachment *ap;
132 char * volatile mnbuf;
133 NYD_ENTER;
135 n_UNUSED(linesize);
136 mnbuf = NULL;
138 n_SIGMAN_ENTER_SWITCH(&sm, n_SIGMAN_ALL){
139 case 0:
140 break;
141 default:
142 goto jleave;
145 /* If the above todo is worked, remove or outsource to attachments.c! */
146 if(hp != NULL && (ap = hp->h_attach) != NULL) do
147 if(ap->a_msgno){
148 mnbuf = sstrdup(mailname);
149 break;
151 while((ap = ap->a_flink) != NULL);
153 n_source_command(n_LEXINPUT_CTX_COMPOSE, linebuf);
155 n_sigman_cleanup_ping(&sm);
156 jleave:
157 if(mnbuf != NULL){
158 if(strcmp(mnbuf, mailname))
159 n_err(_("Mailbox changed: it is likely that existing "
160 "rfc822 attachments became invalid!\n"));
161 free(mnbuf);
163 NYD_LEAVE;
164 n_sigman_leave(&sm, n_SIGMAN_VIPSIGS_NTTYOUT);
167 static int
168 _include_file(char const *name, int *linecount, int *charcount,
169 bool_t doecho, bool_t indent)
171 FILE *fbuf;
172 char const *indb;
173 int ret = -1;
174 char *linebuf = NULL; /* TODO line pool */
175 size_t linesize = 0, indl, linelen, cnt;
176 NYD_ENTER;
178 /* The -M case is special */
179 if (name == (char*)-1)
180 fbuf = stdin;
181 else if ((fbuf = Fopen(name, "r")) == NULL) {
182 n_perr(name, 0);
183 goto jleave;
186 if (!indent)
187 indb = NULL, indl = 0;
188 else {
189 if ((indb = ok_vlook(indentprefix)) == NULL)
190 indb = INDENT_DEFAULT;
191 indl = strlen(indb);
194 *linecount = *charcount = 0;
195 cnt = fsize(fbuf);
196 while (fgetline(&linebuf, &linesize, &cnt, &linelen, fbuf, 0) != NULL) {
197 if (indl > 0 && fwrite(indb, sizeof *indb, indl, _coll_fp) != indl)
198 goto jleave;
199 if (fwrite(linebuf, sizeof *linebuf, linelen, _coll_fp) != linelen)
200 goto jleave;
201 ++(*linecount);
202 (*charcount) += linelen + indl;
203 if (doecho) {
204 if (indl > 0)
205 fwrite(indb, sizeof *indb, indl, stdout);
206 fwrite(linebuf, sizeof *linebuf, linelen, stdout);
209 if (fflush(_coll_fp))
210 goto jleave;
211 if (doecho)
212 fflush(stdout);
214 ret = 0;
215 jleave:
216 if (linebuf != NULL)
217 free(linebuf);
218 if (fbuf != NULL && fbuf != stdin)
219 Fclose(fbuf);
220 NYD_LEAVE;
221 return ret;
224 static void
225 _collect_onpipe(int signo)
227 NYD_X; /* Signal handler */
228 n_UNUSED(signo);
229 siglongjmp(_coll_pipejmp, 1);
232 static void
233 insertcommand(FILE *fp, char const *cmd)
235 FILE *ibuf = NULL;
236 int c;
237 NYD_ENTER;
239 if ((ibuf = Popen(cmd, "r", ok_vlook(SHELL), NULL, 0)) != NULL) {
240 while ((c = getc(ibuf)) != EOF) /* XXX bytewise, yuck! */
241 putc(c, fp);
242 Pclose(ibuf, TRU1);
243 } else
244 n_perr(cmd, 0);
245 NYD_LEAVE;
248 static void
249 print_collf(FILE *cf, struct header *hp, bool_t pagerok)
251 char *lbuf = NULL; /* TODO line pool */
252 sighandler_type sigint;
253 FILE * volatile obuf = stdout;
254 struct attachment *ap;
255 char const *cp;
256 enum gfield gf;
257 size_t linesize = 0, linelen, cnt, cnt2;
258 NYD_ENTER;
260 fflush_rewind(cf);
261 cnt = cnt2 = (size_t)fsize(cf);
263 sigint = safe_signal(SIGINT, SIG_IGN);
265 if (pagerok && (cp = ok_vlook(crt)) != NULL) {
266 size_t l, m;
268 m = 4;
269 if (hp->h_to != NULL)
270 ++m;
271 if (hp->h_subject != NULL)
272 ++m;
273 if (hp->h_cc != NULL)
274 ++m;
275 if (hp->h_bcc != NULL)
276 ++m;
277 if (hp->h_attach != NULL)
278 ++m;
279 m += (hp->h_from != NULL || myaddrs(hp) != NULL);
280 m += (hp->h_sender != NULL || ok_vlook(sender) != NULL);
281 m += (hp->h_replyto != NULL || ok_vlook(replyto) != NULL);
283 l = (*cp == '\0') ? (size_t)screensize() : strtoul(cp, NULL, 0);
284 if (m > l)
285 goto jpager;
286 l -= m;
288 for (m = 0; fgetline(&lbuf, &linesize, &cnt2, NULL, cf, 0); ++m)
290 rewind(cf);
291 if (l < m) {
292 jpager:
293 if (sigsetjmp(_coll_pipejmp, 1))
294 goto jendpipe;
295 if ((obuf = n_pager_open()) == NULL)
296 obuf = stdout;
297 else
298 safe_signal(SIGPIPE, &_collect_onpipe);
302 fprintf(obuf, _("-------\nMessage contains:\n"));
303 gf = GIDENT | GTO | GSUBJECT | GCC | GBCC | GNL | GFILES | GCOMMA;
304 puthead(TRU1, hp, obuf, gf, SEND_TODISP, CONV_NONE, NULL, NULL);
305 while (fgetline(&lbuf, &linesize, &cnt, &linelen, cf, 1))
306 prout(lbuf, linelen, obuf);
307 if (hp->h_attach != NULL) {
308 fputs(_("-------\nAttachments:\n"), obuf);
309 for (ap = hp->h_attach; ap != NULL; ap = ap->a_flink) {
310 if (ap->a_msgno)
311 fprintf(obuf, " - message %u\n", ap->a_msgno);
312 else {
313 /* TODO after MIME/send layer rewrite we *know*
314 * TODO the details of the attachment here,
315 * TODO so adjust this again, then */
316 char const *cs, *csi = "-> ";
318 if ((cs = ap->a_charset) == NULL &&
319 (csi = "<- ", cs = ap->a_input_charset) == NULL)
320 cs = charset_get_lc();
321 if ((cp = ap->a_content_type) == NULL)
322 cp = "?";
323 else if (ascncasecmp(cp, "text/", 5))
324 csi = n_empty;
325 fprintf(obuf, " - [%s, %s%s] %s\n", cp, csi, cs,
326 n_shexp_quote_cp(ap->a_name, FAL0));
331 jendpipe:
332 if (obuf != stdout)
333 n_pager_close(obuf);
334 if (lbuf != NULL)
335 free(lbuf);
336 safe_signal(SIGINT, sigint);
337 NYD_LEAVE;
340 static int
341 exwrite(char const *name, FILE *fp, int f)
343 FILE *of;
344 int c, rv;
345 long lc, cc;
346 NYD_ENTER;
348 if (f) {
349 printf("%s ", n_shexp_quote_cp(name, FAL0));
350 fflush(stdout);
353 if ((of = Fopen(name, "a")) == NULL) {
354 n_perr(name, 0);
355 goto jerr;
358 lc = cc = 0;
359 while ((c = getc(fp)) != EOF) {
360 ++cc;
361 if (c == '\n')
362 ++lc;
363 if (putc(c, of) == EOF) {
364 n_perr(name, 0);
365 goto jerr;
368 printf(_("%ld/%ld\n"), lc, cc);
370 rv = 0;
371 jleave:
372 if(of != NULL)
373 Fclose(of);
374 fflush(stdout);
375 NYD_LEAVE;
376 return rv;
377 jerr:
378 putchar('-');
379 putchar('\n');
380 rv = -1;
381 goto jleave;
384 static enum okay
385 makeheader(FILE *fp, struct header *hp, si8_t *checkaddr_err)
387 FILE *nf;
388 int c;
389 enum okay rv = STOP;
390 NYD_ENTER;
392 if ((nf = Ftmp(NULL, "colhead", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==NULL) {
393 n_perr(_("temporary mail edit file"), 0);
394 goto jleave;
397 extract_header(fp, hp, checkaddr_err);
398 if (checkaddr_err != NULL && *checkaddr_err != 0)
399 goto jleave;
401 while ((c = getc(fp)) != EOF) /* XXX bytewise, yuck! */
402 putc(c, nf);
403 if (fp != _coll_fp)
404 Fclose(_coll_fp);
405 Fclose(fp);
406 _coll_fp = nf;
407 if (check_from_and_sender(hp->h_from, hp->h_sender) == NULL)
408 goto jleave;
409 rv = OKAY;
410 jleave:
411 NYD_LEAVE;
412 return rv;
415 static void
416 mesedit(int c, struct header *hp)
418 bool_t saved;
419 sighandler_type sigint;
420 FILE *nf;
421 NYD_ENTER;
423 if(!(saved = ok_blook(add_file_recipients)))
424 ok_bset(add_file_recipients);
426 sigint = safe_signal(SIGINT, SIG_IGN);
427 nf = run_editor(_coll_fp, (off_t)-1, c, FAL0, hp, NULL, SEND_MBOX, sigint);
428 if (nf != NULL) {
429 if (hp) {
430 rewind(nf);
431 makeheader(nf, hp, NULL);
432 } else {
433 fseek(nf, 0L, SEEK_END);
434 Fclose(_coll_fp);
435 _coll_fp = nf;
438 safe_signal(SIGINT, sigint);
440 if(!saved)
441 ok_bclear(add_file_recipients);
442 NYD_LEAVE;
445 static void
446 mespipe(char *cmd)
448 FILE *nf;
449 sighandler_type sigint;
450 NYD_ENTER;
452 sigint = safe_signal(SIGINT, SIG_IGN);
454 if ((nf = Ftmp(NULL, "colpipe", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==NULL) {
455 n_perr(_("temporary mail edit file"), 0);
456 goto jout;
459 /* stdin = current message. stdout = new message */
460 fflush(_coll_fp);
461 if (run_command(ok_vlook(SHELL), 0, fileno(_coll_fp), fileno(nf), "-c",
462 cmd, NULL, NULL) < 0) {
463 Fclose(nf);
464 goto jout;
467 if (fsize(nf) == 0) {
468 n_err(_("No bytes from %s !?\n"), n_shexp_quote_cp(cmd, FAL0));
469 Fclose(nf);
470 goto jout;
473 /* Take new files */
474 fseek(nf, 0L, SEEK_END);
475 Fclose(_coll_fp);
476 _coll_fp = nf;
477 jout:
478 safe_signal(SIGINT, sigint);
479 NYD_LEAVE;
482 static int
483 forward(char *ms, FILE *fp, int f)
485 int *msgvec, rv = 0;
486 struct n_ignore const *itp;
487 char const *tabst;
488 enum sendaction action;
489 NYD_ENTER;
491 msgvec = salloc((size_t)(msgCount + 1) * sizeof *msgvec);
492 if (getmsglist(ms, msgvec, 0) < 0)
493 goto jleave;
494 if (*msgvec == 0) {
495 *msgvec = first(0, MMNORM);
496 if (*msgvec == 0) {
497 n_err(_("No appropriate messages\n"));
498 goto jleave;
500 msgvec[1] = 0;
503 if (f == 'f' || f == 'F' || f == 'u')
504 tabst = NULL;
505 else if ((tabst = ok_vlook(indentprefix)) == NULL)
506 tabst = INDENT_DEFAULT;
507 if (f == 'u' || f == 'U')
508 itp = n_IGNORE_ALL;
509 else
510 itp = upperchar(f) ? NULL : n_IGNORE_TYPE;
511 action = (upperchar(f) && f != 'U') ? SEND_QUOTE_ALL : SEND_QUOTE;
513 printf(_("Interpolating:"));
514 srelax_hold();
515 for (; *msgvec != 0; ++msgvec) {
516 struct message *mp = message + *msgvec - 1;
518 touch(mp);
519 printf(" %d", *msgvec);
520 fflush(stdout);
521 if (sendmp(mp, fp, itp, tabst, action, NULL) < 0) {
522 n_perr(_("temporary mail file"), 0);
523 rv = -1;
524 break;
526 srelax();
528 srelax_rele();
529 printf("\n");
530 jleave:
531 NYD_LEAVE;
532 return rv;
535 static bool_t
536 a_collect_plumbing(char const *ms, struct header *hp){
537 /* TODO _collect_plumbing: instead of fields the basic headers should
538 * TODO be in an array and have IDs, like in termcap etc., so then this
539 * TODO could be simplified as table-walks */
540 char const *cp, *cmd[4];
541 NYD2_ENTER;
543 /* Protcol version for *on-compose-done-shell* -- update manual on change! */
544 #define a_COLL_PLUMBING_VERSION "0 0 1"
545 cp = ms;
547 /* C99 */{
548 size_t i;
550 for(i = 0; i < n_NELEM(cmd); ++i){
551 while(blankchar(*cp))
552 ++cp;
553 if(*cp == '\0')
554 cmd[i] = NULL;
555 else{
556 if(i < n_NELEM(cmd) - 1)
557 for(cmd[i] = cp++; *cp != '\0' && !blankchar(*cp); ++cp)
559 else{
560 /* Last slot takes all the rest of the line, less trailing WS */
561 for(cmd[i] = cp++; *cp != '\0'; ++cp)
563 while(blankchar(cp[-1]))
564 --cp;
566 cmd[i] = savestrbuf(cmd[i], PTR2SIZE(cp - cmd[i]));
571 if(n_UNLIKELY(cmd[0] == NULL))
572 goto jecmd;
573 if(!asccasecmp(cmd[0], "header")){
574 struct n_header_field *hfp;
575 struct name *np, **npp;
577 if(cmd[1] == NULL || !asccasecmp(cmd[1], "list")){
578 if(cmd[2] == NULL){
579 fputs("210", stdout);
580 if(hp->h_from != NULL) fputs(" From", stdout);
581 if(hp->h_sender != NULL) fputs(" Sender", stdout);
582 if(hp->h_to != NULL) fputs(" To", stdout);
583 if(hp->h_cc != NULL) fputs(" Cc", stdout);
584 if(hp->h_bcc != NULL) fputs(" Bcc", stdout);
585 if(hp->h_subject != NULL) fputs(" Subject", stdout);
586 if(hp->h_replyto != NULL) fputs(" Reply-To", stdout);
587 if(hp->h_mft != NULL) fputs(" Mail-Followup-To", stdout);
588 if(hp->h_message_id != NULL) fputs(" Message-ID", stdout);
589 if(hp->h_ref != NULL) fputs(" References", stdout);
590 if(hp->h_in_reply_to != NULL) fputs(" In-Reply-To", stdout);
591 for(hfp = hp->h_user_headers; hfp != NULL; hfp = hfp->hf_next){
592 putc(' ', stdout);
593 fputs(&hfp->hf_dat[0], stdout);
595 putc('\n', stdout);
596 }else{
597 if(cmd[3] != NULL)
598 goto jecmd;
599 if(!asccasecmp(cmd[2], "from")){
600 np = hp->h_from;
601 jlist:
602 fprintf(stdout, "%s %s\n", (np == NULL ? "501" : "210"), cp);
603 }else if(!asccasecmp(cmd[2], cp = "Sender")){
604 np = hp->h_sender;
605 goto jlist;
606 }else if(!asccasecmp(cmd[2], cp = "To")){
607 np = hp->h_to;
608 goto jlist;
609 }else if(!asccasecmp(cmd[2], cp = "Cc")){
610 np = hp->h_cc;
611 goto jlist;
612 }else if(!asccasecmp(cmd[2], cp = "Bcc")){
613 np = hp->h_bcc;
614 goto jlist;
615 }else if(!asccasecmp(cmd[2], cp = "Subject")){
616 np = (struct name*)-1;
617 goto jlist;
618 }else if(!asccasecmp(cmd[2], cp = "Reply-To")){
619 np = hp->h_replyto;
620 goto jlist;
621 }else if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
622 np = hp->h_mft;
623 goto jlist;
624 }else if(!asccasecmp(cmd[2], cp = "Message-ID")){
625 np = hp->h_message_id;
626 goto jlist;
627 }else if(!asccasecmp(cmd[2], cp = "References")){
628 np = hp->h_ref;
629 goto jlist;
630 }else if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
631 np = hp->h_in_reply_to;
632 goto jlist;
633 }else{
634 /* Primitive name normalization XXX header object should
635 * XXX have a more sophisticated accessible one */
636 char *xp;
638 cp = xp = savestr(cmd[2]);
639 xp[0] = upperchar(xp[0]);
640 while(*++xp != '\0')
641 xp[0] = lowerchar(xp[0]);
643 for(hfp = hp->h_user_headers;; hfp = hfp->hf_next){
644 if(hfp == NULL)
645 goto j501cp;
646 else if(!asccasecmp(cp, &hfp->hf_dat[0])){
647 fprintf(stdout, "210 %s\n", cp);
648 break;
653 }else if(!asccasecmp(cmd[1], "show")){
654 if(cmd[2] == NULL || cmd[3] != NULL)
655 goto jecmd;
656 if(!asccasecmp(cmd[2], "from")){
657 np = hp->h_from;
658 jshow:
659 if(np != NULL){
660 fprintf(stdout, "211 %s\n", cp);
662 fprintf(stdout, "%s %s\n", np->n_name, np->n_fullname);
663 while((np = np->n_flink) != NULL);
664 putc('\n', stdout);
665 }else
666 goto j501cp;
667 }else if(!asccasecmp(cmd[2], cp = "Sender")){
668 np = hp->h_sender;
669 goto jshow;
670 }else if(!asccasecmp(cmd[2], cp = "To")){
671 np = hp->h_to;
672 goto jshow;
673 }else if(!asccasecmp(cmd[2], cp = "Cc")){
674 np = hp->h_cc;
675 goto jshow;
676 }else if(!asccasecmp(cmd[2], cp = "Bcc")){
677 np = hp->h_bcc;
678 goto jshow;
679 }else if(!asccasecmp(cmd[2], cp = "Reply-To")){
680 np = hp->h_replyto;
681 goto jshow;
682 }else if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
683 np = hp->h_mft;
684 goto jshow;
685 }else if(!asccasecmp(cmd[2], cp = "Message-ID")){
686 np = hp->h_message_id;
687 goto jshow;
688 }else if(!asccasecmp(cmd[2], cp = "References")){
689 np = hp->h_ref;
690 goto jshow;
691 }else if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
692 np = hp->h_in_reply_to;
693 goto jshow;
694 }else if(!asccasecmp(cmd[2], cp = "Subject")){
695 if(hp->h_subject != NULL)
696 fprintf(stdout, "212 %s\n%s\n\n", cp, hp->h_subject);
697 else
698 fprintf(stdout, "501 %s\n", cp);
699 }else{
700 /* Primitive name normalization XXX header object should
701 * XXX have a more sophisticated accessible one */
702 bool_t any;
703 char *xp;
705 cp = xp = savestr(cmd[2]);
706 xp[0] = upperchar(xp[0]);
707 while(*++xp != '\0')
708 xp[0] = lowerchar(xp[0]);
710 for(any = FAL0, hfp = hp->h_user_headers; hfp != NULL;
711 hfp = hfp->hf_next){
712 if(!asccasecmp(cp, &hfp->hf_dat[0])){
713 if(!any)
714 fprintf(stdout, "212 %s\n", cp);
715 any = TRU1;
716 fprintf(stdout, "%s\n", &hfp->hf_dat[hfp->hf_nl +1]);
719 if(any)
720 putc('\n', stdout);
721 else
722 goto j501cp;
724 }else if(!asccasecmp(cmd[1], "remove")){
725 if(cmd[2] == NULL || cmd[3] != NULL)
726 goto jecmd;
727 if(!asccasecmp(cmd[2], "from")){
728 npp = &hp->h_from;
729 jrem:
730 if(*npp != NULL){
731 *npp = NULL;
732 fprintf(stdout, "210 %s\n", cp);
733 }else
734 goto j501cp;
735 }else if(!asccasecmp(cmd[2], cp = "Sender")){
736 npp = &hp->h_sender;
737 goto jrem;
738 }else if(!asccasecmp(cmd[2], cp = "To")){
739 npp = &hp->h_to;
740 goto jrem;
741 }else if(!asccasecmp(cmd[2], cp = "Cc")){
742 npp = &hp->h_cc;
743 goto jrem;
744 }else if(!asccasecmp(cmd[2], cp = "Bcc")){
745 npp = &hp->h_bcc;
746 goto jrem;
747 }else if(!asccasecmp(cmd[2], cp = "Reply-To")){
748 npp = &hp->h_replyto;
749 goto jrem;
750 }else if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
751 npp = &hp->h_mft;
752 goto jrem;
753 }else if(!asccasecmp(cmd[2], cp = "Message-ID")){
754 npp = &hp->h_message_id;
755 goto jrem;
756 }else if(!asccasecmp(cmd[2], cp = "References")){
757 npp = &hp->h_ref;
758 goto jrem;
759 }else if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
760 npp = &hp->h_in_reply_to;
761 goto jrem;
762 }else if(!asccasecmp(cmd[2], cp = "Subject")){
763 if(hp->h_subject != NULL){
764 hp->h_subject = NULL;
765 fprintf(stdout, "210 %s\n", cp);
766 }else
767 goto j501cp;
768 }else{
769 /* Primitive name normalization XXX header object should
770 * XXX have a more sophisticated accessible one */
771 struct n_header_field **hfpp;
772 bool_t any;
773 char *xp;
775 cp = xp = savestr(cmd[2]);
776 xp[0] = upperchar(xp[0]);
777 while(*++xp != '\0')
778 xp[0] = lowerchar(xp[0]);
780 for(any = FAL0, hfpp = &hp->h_user_headers; (hfp = *hfpp) != NULL;){
781 if(!asccasecmp(cp, &hfp->hf_dat[0])){
782 *hfpp = hfp->hf_next;
783 if(!any)
784 fprintf(stdout, "210 %s\n", cp);
785 any = TRU1;
786 }else
787 hfp = *(hfpp = &hfp->hf_next);
789 if(!any)
790 goto j501cp;
792 }else if(!asccasecmp(cmd[1], "insert")){ /* TODO LOGIC BELONGS head.c!
793 * TODO That is: Header::factory(string) -> object (blahblah).
794 * TODO I.e., as long as we don't have regular RFC compliant parsers
795 * TODO which differentiate in between structured and unstructured
796 * TODO header fields etc., a little workaround */
797 si8_t aerr;
798 enum expand_addr_check_mode eacm;
799 enum gfield ntype;
800 bool_t mult_ok;
802 mult_ok = TRU1;
803 ntype = GEXTRA | GFULL | GFULLEXTRA;
804 eacm = EACM_STRICT;
806 if(cmd[2] == NULL || cmd[3] == NULL)
807 goto jecmd;
808 if(!asccasecmp(cmd[2], "from")){
809 npp = &hp->h_from;
810 jins:
811 aerr = 0;
812 if((np = lextract(cmd[3], ntype)) == NULL)
813 goto j501cp;
814 else if((np = checkaddrs(np, eacm, &aerr), aerr != 0))
815 fprintf(stdout, "505 %s\n", cp);
816 else if(!mult_ok && (np->n_flink != NULL || *npp != NULL))
817 fprintf(stdout, "506 %s\n", cp);
818 else{
819 *npp = cat(*npp, np);
820 fprintf(stdout, "210 %s\n", cp);
822 }else if(!asccasecmp(cmd[2], cp = "Sender")){
823 mult_ok = FAL0;
824 npp = &hp->h_sender;
825 goto jins;
826 }else if(!asccasecmp(cmd[2], cp = "To")){
827 npp = &hp->h_to;
828 ntype = GTO | GFULL;
829 eacm = EACM_NORMAL | EAF_NAME;
830 goto jins;
831 }else if(!asccasecmp(cmd[2], cp = "Cc")){
832 npp = &hp->h_cc;
833 ntype = GCC | GFULL;
834 eacm = EACM_NORMAL | EAF_NAME;
835 goto jins;
836 }else if(!asccasecmp(cmd[2], cp = "Bcc")){
837 npp = &hp->h_bcc;
838 ntype = GCC | GFULL;
839 eacm = EACM_NORMAL | EAF_NAME;
840 goto jins;
841 }else if(!asccasecmp(cmd[2], cp = "Reply-To")){
842 npp = &hp->h_replyto;
843 goto jins;
844 }else if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
845 npp = &hp->h_mft;
846 eacm = EACM_NONAME;
847 goto jins;
848 }else if(!asccasecmp(cmd[2], cp = "Message-ID")){
849 mult_ok = FAL0;
850 npp = &hp->h_message_id;
851 eacm = EACM_NONAME;
852 goto jins;
853 }else if(!asccasecmp(cmd[2], cp = "References")){
854 npp = &hp->h_ref;
855 ntype = GREF;
856 eacm = EACM_NONAME;
857 goto jins;
858 }else if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
859 npp = &hp->h_in_reply_to;
860 ntype = GREF;
861 eacm = EACM_NONAME;
862 goto jins;
863 }else if(!asccasecmp(cmd[2], cp = "Subject")){
864 if(cmd[3][0] != '\0'){
865 if(hp->h_subject != NULL)
866 hp->h_subject = savecatsep(hp->h_subject, ' ', cmd[3]);
867 else
868 hp->h_subject = savestr(cmd[3]);
869 fprintf(stdout, "210 %s\n", cp);
870 }else
871 goto j501cp;
872 }else{
873 /* Primitive name normalization XXX header object should
874 * XXX have a more sophisticated accessible one */
875 size_t nl, bl;
876 struct n_header_field **hfpp;
878 for(cp = cmd[2]; *cp != '\0'; ++cp)
879 if(!fieldnamechar(*cp)){
880 cp = cmd[2];
881 goto j501cp;
884 for(hfpp = &hp->h_user_headers; *hfpp != NULL;)
885 hfpp = &(*hfpp)->hf_next;
887 nl = strlen(cp = cmd[2]);
888 bl = strlen(cmd[3]) +1;
889 *hfpp = hfp = salloc(n_VSTRUCT_SIZEOF(struct n_header_field, hf_dat
890 ) + nl +1 + bl);
891 hfp->hf_next = NULL;
892 hfp->hf_nl = nl;
893 hfp->hf_bl = bl - 1;
894 memcpy(hfp->hf_dat, cp, nl);
895 hfp->hf_dat[nl++] = '\0';
896 memcpy(hfp->hf_dat + nl, cmd[3], bl);
897 fprintf(stdout, "210 %s\n", cp);
899 }else
900 goto jecmd;
901 }else{
902 jecmd:
903 fputs("500\n", stdout);
904 ms = NULL;
907 jleave:
908 fflush(stdout);
909 NYD2_LEAVE;
910 return (ms != NULL);
912 j501cp:
913 fputs("501 ", stdout);
914 fputs(cp, stdout);
915 putc('\n', stdout);
916 goto jleave;
919 static void
920 _collint(int s)
922 NYD_X; /* Signal handler */
924 /* the control flow is subtle, because we can be called from ~q */
925 if (_coll_hadintr == 0) {
926 if (ok_blook(ignore)) {
927 fputs("@\n", stdout);
928 fflush(stdout);
929 clearerr(stdin);
930 } else
931 _coll_hadintr = 1;
932 siglongjmp(_coll_jmp, 1);
934 exit_status |= EXIT_SEND_ERROR;
935 if (s != 0)
936 savedeadletter(_coll_fp, TRU1);
937 /* Aborting message, no need to fflush() .. */
938 siglongjmp(_coll_abort, 1);
941 static void
942 collhup(int s)
944 NYD_X; /* Signal handler */
945 n_UNUSED(s);
947 savedeadletter(_coll_fp, TRU1);
948 /* Let's pretend nobody else wants to clean up, a true statement at
949 * this time */
950 exit(EXIT_ERR);
953 static int
954 putesc(char const *s, FILE *stream)
956 int n = 0, rv = -1;
957 NYD_ENTER;
959 while (s[0] != '\0') {
960 if (s[0] == '\\') {
961 if (s[1] == 't') {
962 if (putc('\t', stream) == EOF)
963 goto jleave;
964 ++n;
965 s += 2;
966 continue;
968 if (s[1] == 'n') {
969 if (putc('\n', stream) == EOF)
970 goto jleave;
971 ++n;
972 s += 2;
973 continue;
976 if (putc(s[0], stream) == EOF)
977 goto jleave;
978 ++n;
979 ++s;
981 if (putc('\n', stream) == EOF)
982 goto jleave;
983 rv = ++n;
984 jleave:
985 NYD_LEAVE;
986 return rv;
989 static void
990 a_coll__hook_setter(void *arg){ /* TODO v15: drop */
991 struct header *hp;
992 char const *val;
993 NYD2_ENTER;
995 hp = arg;
997 if((val = detract(hp->h_from, GNAMEONLY)) == NULL)
998 val = n_empty;
999 ok_vset(compose_from, val);
1000 if((val = detract(hp->h_sender, 0)) == NULL)
1001 val = n_empty;
1002 ok_vset(compose_sender, val);
1003 if((val = detract(hp->h_to, GNAMEONLY)) == NULL)
1004 val = n_empty;
1005 ok_vset(compose_to, val);
1006 if((val = detract(hp->h_cc, GNAMEONLY)) == NULL)
1007 val = n_empty;
1008 ok_vset(compose_cc, val);
1009 if((val = detract(hp->h_bcc, GNAMEONLY)) == NULL)
1010 val = n_empty;
1011 ok_vset(compose_bcc, val);
1012 if((val = hp->h_subject) == NULL)
1013 val = n_empty;
1014 ok_vset(compose_subject, val);
1015 NYD2_LEAVE;
1018 static void
1019 a_coll_ocds__finalize(void *vp){
1020 /* Note we use this for destruction upon setup errors, thus */
1021 sighandler_type opipe;
1022 sighandler_type oint;
1023 struct a_coll_ocds_arg **coapp, *coap;
1024 NYD2_ENTER;
1026 temporary_call_compose_mode_hook((char*)-1, NULL, NULL);
1028 coap = *(coapp = vp);
1029 *coapp = (struct a_coll_ocds_arg*)-1;
1031 if(coap->coa_stdout != NULL)
1032 if(!Pclose(coap->coa_stdout, FAL0)){
1033 *coap->coa_senderr = TRU1;
1034 n_err(_("*on-compose-done-shell* failed: %s\n"),
1035 n_shexp_quote_cp(coap->coa_cmd, FAL0));
1038 if(coap->coa_stdin != NULL)
1039 Fclose(coap->coa_stdin);
1040 else if(coap->coa_pipe[0] != -1)
1041 close(coap->coa_pipe[0]);
1043 if(coap->coa_pipe[1] != -1)
1044 close(coap->coa_pipe[1]);
1046 opipe = coap->coa_opipe;
1047 oint = coap->coa_oint;
1049 n_lofi_free(coap);
1051 hold_all_sigs();
1052 safe_signal(SIGPIPE, opipe);
1053 safe_signal(SIGINT, oint);
1054 rele_all_sigs();
1055 NYD2_LEAVE;
1058 FL FILE *
1059 collect(struct header *hp, int printheaders, struct message *mp,
1060 char *quotefile, int doprefix, si8_t *checkaddr_err)
1062 struct n_ignore const *quoteitp;
1063 struct a_coll_ocds_arg *coap;
1064 int lc, cc, c;
1065 int volatile t, eofcnt;
1066 int volatile escape, getfields;
1067 char *linebuf;
1068 char const *cp;
1069 size_t i, linesize; /* TODO line pool */
1070 long cnt;
1071 enum sendaction action;
1072 sigset_t oset, nset;
1073 FILE * volatile sigfp;
1074 NYD_ENTER;
1076 _coll_fp = NULL;
1077 sigfp = NULL;
1078 linesize = 0;
1079 linebuf = NULL;
1080 eofcnt = 0;
1081 coap = NULL;
1083 /* Start catching signals from here, but we're still die on interrupts
1084 * until we're in the main loop */
1085 sigfillset(&nset);
1086 sigprocmask(SIG_BLOCK, &nset, &oset);
1087 /* FIXME have dropped handlerpush() and localized onintr() in lex.c! */
1088 if ((_coll_saveint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
1089 safe_signal(SIGINT, &_collint);
1090 if ((_coll_savehup = safe_signal(SIGHUP, SIG_IGN)) != SIG_IGN)
1091 safe_signal(SIGHUP, collhup);
1092 if (sigsetjmp(_coll_abort, 1))
1093 goto jerr;
1094 if (sigsetjmp(_coll_jmp, 1))
1095 goto jerr;
1096 pstate |= PS_RECURSED;
1097 sigprocmask(SIG_SETMASK, &oset, (sigset_t*)NULL);
1099 if ((_coll_fp = Ftmp(NULL, "collect", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
1100 NULL) {
1101 n_perr(_("temporary mail file"), 0);
1102 goto jerr;
1105 /* If we are going to prompt for a subject, refrain from printing a newline
1106 * after the headers (since some people mind) */
1107 getfields = 0;
1108 if (!(options & OPT_t_FLAG)) {
1109 t = GTO | GSUBJECT | GCC | GNL;
1110 if (ok_blook(fullnames))
1111 t |= GCOMMA;
1113 if (options & OPT_INTERACTIVE) {
1114 if (hp->h_subject == NULL && (ok_blook(ask) || ok_blook(asksub)))
1115 t &= ~GNL, getfields |= GSUBJECT;
1117 if (hp->h_to == NULL)
1118 t &= ~GNL, getfields |= GTO;
1120 if (!ok_blook(bsdcompat) && !ok_blook(askatend)) {
1121 if (hp->h_bcc == NULL && ok_blook(askbcc))
1122 t &= ~GNL, getfields |= GBCC;
1123 if (hp->h_cc == NULL && ok_blook(askcc))
1124 t &= ~GNL, getfields |= GCC;
1127 } else {
1128 n_UNINIT(t, 0);
1131 escape = ((cp = ok_vlook(escape)) != NULL) ? *cp : ESCAPE;
1132 _coll_hadintr = 0;
1134 if (!sigsetjmp(_coll_jmp, 1)) {
1135 /* Ask for some headers first, as necessary */
1136 if (getfields)
1137 grab_headers(n_LEXINPUT_CTX_COMPOSE, hp, getfields, 1);
1139 /* Execute compose-enter TODO completely v15-compat intermediate!! */
1140 if((cp = ok_vlook(on_compose_enter)) != NULL){
1141 setup_from_and_sender(hp);
1142 temporary_call_compose_mode_hook(cp, &a_coll__hook_setter, hp);
1145 /* Cannot do since it may require turning this into a multipart one */
1146 if(!(options & OPT_Mm_FLAG)){
1147 char const *cp_obsolete = ok_vlook(NAIL_HEAD);
1148 if(cp_obsolete != NULL)
1149 OBSOLETE(_("please use *message-inject-head* "
1150 "instead of *NAIL_HEAD*"));
1152 if(((cp = ok_vlook(message_inject_head)) != NULL ||
1153 (cp = cp_obsolete) != NULL) && putesc(cp, _coll_fp) < 0)
1154 goto jerr;
1156 /* Quote an original message */
1157 if (mp != NULL && (doprefix || (cp = ok_vlook(quote)) != NULL)) {
1158 quoteitp = n_IGNORE_ALL;
1159 action = SEND_QUOTE;
1160 if (doprefix) {
1161 quoteitp = n_IGNORE_FWD;
1162 if ((cp = ok_vlook(fwdheading)) == NULL)
1163 cp = "-------- Original Message --------";
1164 if (*cp != '\0' && fprintf(_coll_fp, "%s\n", cp) < 0)
1165 goto jerr;
1166 } else if (!strcmp(cp, "noheading")) {
1167 /*EMPTY*/;
1168 } else if (!strcmp(cp, "headers")) {
1169 quoteitp = n_IGNORE_TYPE;
1170 } else if (!strcmp(cp, "allheaders")) {
1171 quoteitp = NULL;
1172 action = SEND_QUOTE_ALL;
1173 } else {
1174 cp = hfield1("from", mp);
1175 if (cp != NULL && (cnt = (long)strlen(cp)) > 0) {
1176 if (xmime_write(cp, cnt, _coll_fp, CONV_FROMHDR, TD_NONE) < 0)
1177 goto jerr;
1178 if (fprintf(_coll_fp, _(" wrote:\n\n")) < 0)
1179 goto jerr;
1182 if (fflush(_coll_fp))
1183 goto jerr;
1184 if (doprefix)
1185 cp = NULL;
1186 else if ((cp = ok_vlook(indentprefix)) == NULL)
1187 cp = INDENT_DEFAULT;
1188 if (sendmp(mp, _coll_fp, quoteitp, cp, action, NULL) < 0)
1189 goto jerr;
1193 if (quotefile != NULL) {
1194 if (_include_file(quotefile, &lc, &cc,
1195 ((options & (OPT_INTERACTIVE | OPT_Mm_FLAG)) == OPT_INTERACTIVE),
1196 FAL0) != 0)
1197 goto jerr;
1200 if ((options & (OPT_Mm_FLAG | OPT_INTERACTIVE)) == OPT_INTERACTIVE) {
1201 /* Print what we have sofar also on the terminal (if useful) */
1202 if (!ok_blook(editalong)) {
1203 if (printheaders)
1204 puthead(TRU1, hp, stdout, t, SEND_TODISP, CONV_NONE, NULL, NULL);
1206 rewind(_coll_fp);
1207 while ((c = getc(_coll_fp)) != EOF) /* XXX bytewise, yuck! */
1208 putc(c, stdout);
1209 if (fseek(_coll_fp, 0, SEEK_END))
1210 goto jerr;
1211 } else {
1212 rewind(_coll_fp);
1213 mesedit('e', hp);
1214 /* As mandated by the Mail Reference Manual, print "(continue)" */
1215 jcont:
1216 if(options & OPT_INTERACTIVE)
1217 fputs(_("(continue)\n"), stdout);
1219 fflush(stdout);
1221 } else {
1222 /* Come here for printing the after-signal message. Duplicate messages
1223 * won't be printed because the write is aborted if we get a SIGTTOU */
1224 if (_coll_hadintr)
1225 n_err(_("\n(Interrupt -- one more to kill letter)\n"));
1228 /* If not under shell hook control */
1229 if(coap == NULL){
1230 /* We're done with -M or -m (because we are too simple minded) */
1231 if(options & OPT_Mm_FLAG)
1232 goto jout;
1233 /* No command escapes, interrupts not expected. Simply copy STDIN */
1234 if (!(options & (OPT_INTERACTIVE | OPT_t_FLAG | OPT_TILDE_FLAG))){
1235 linebuf = srealloc(linebuf, linesize = LINESIZE);
1236 while ((i = fread(linebuf, sizeof *linebuf, linesize, stdin)) > 0) {
1237 if (i != fwrite(linebuf, sizeof *linebuf, i, _coll_fp))
1238 goto jerr;
1240 goto jout;
1244 /* The interactive collect loop */
1245 for(;;){
1246 /* C99 */{
1247 enum n_lexinput_flags lif;
1249 lif = n_LEXINPUT_CTX_COMPOSE;
1250 if(options & (OPT_INTERACTIVE | OPT_TILDE_FLAG)){
1251 if(!(options & OPT_t_FLAG))
1252 lif |= n_LEXINPUT_NL_ESC;
1254 cnt = n_lex_input(lif, n_empty, &linebuf, &linesize, NULL);
1257 if (cnt < 0) {
1258 if(coap != NULL)
1259 break;
1260 if (options & OPT_t_FLAG) {
1261 fflush_rewind(_coll_fp);
1262 /* It is important to set PS_t_FLAG before extract_header() *and*
1263 * keep OPT_t_FLAG for the first parse of the message, too! */
1264 pstate |= PS_t_FLAG;
1265 if (makeheader(_coll_fp, hp, checkaddr_err) != OKAY)
1266 goto jerr;
1267 options &= ~OPT_t_FLAG;
1268 continue;
1269 } else if ((options & OPT_INTERACTIVE) &&
1270 ok_blook(ignoreeof) && ++eofcnt < 4) {
1271 printf(_("*ignoreeof* set, use `~.' to terminate letter\n"));
1272 continue;
1274 break;
1277 _coll_hadintr = 0;
1279 cp = linebuf;
1280 if(cnt == 0)
1281 goto jputnl;
1282 else if(coap == NULL){
1283 if(!(options & (OPT_INTERACTIVE | OPT_TILDE_FLAG)))
1284 goto jputline;
1285 else if(cp[0] == '.'){
1286 if(cnt == 1 && (ok_blook(dot) || ok_blook(ignoreeof)))
1287 break;
1290 if(cp[0] != escape){
1291 jputline:
1292 if(fwrite(cp, sizeof *cp, cnt, _coll_fp) != (size_t)cnt)
1293 goto jerr;
1294 /* TODO PS_READLINE_NL is a terrible hack to ensure that _in_all_-
1295 * TODO _code_paths_ a file without trailing newline isn't modified
1296 * TODO to continue one; the "saw-newline" needs to be part of an
1297 * TODO I/O input machinery object */
1298 jputnl:
1299 if(pstate & PS_READLINE_NL){
1300 if(putc('\n', _coll_fp) == EOF)
1301 goto jerr;
1303 continue;
1306 /* Cleanup the input string: like this we can perform a little bit of
1307 * usage testing and also have somewhat normalized history entries */
1308 for(cp = &linebuf[2]; (c = *cp) != '\0' && blankspacechar(c); ++cp)
1309 continue;
1310 if(c == '\0'){
1311 linebuf[2] = '\0';
1312 cnt = 2;
1313 }else{
1314 i = PTR2SIZE(cp - linebuf) - 3;
1315 memmove(&linebuf[3], cp, (cnt -= i));
1316 linebuf[2] = ' ';
1317 linebuf[cnt] = '\0';
1319 if(cnt > 0){ /* TODO v15 no more trailing WS from lex_input please */
1320 cp = &linebuf[cnt];
1322 for(;; --cp){
1323 c = cp[-1];
1324 if(!blankspacechar(c))
1325 break;
1327 ((char*)n_UNCONST(cp))[0] = '\0';
1328 cnt = PTR2SIZE(cp - linebuf);
1332 switch((c = linebuf[1])){
1333 default:
1334 /* On double escape, send a single one. Otherwise, it's an error */
1335 if(c == escape){
1336 cp = &linebuf[1];
1337 --cnt;
1338 goto jputline;
1339 }else{
1340 char buf[sizeof(n_UNIREPL)];
1342 if(asciichar(c))
1343 buf[0] = c, buf[1] = '\0';
1344 else if(options & OPT_UNICODE)
1345 memcpy(buf, n_unirepl, sizeof n_unirepl);
1346 else
1347 buf[0] = '?', buf[1] = '\0';
1348 n_err(_("Unknown command escape: ~%s\n"), buf);
1349 continue;
1351 jearg:
1352 n_err(_("Invalid command escape usage: %s\n"), linebuf);
1353 continue;
1354 case '!':
1355 /* Shell escape, send the balance of line to sh -c */
1356 if(cnt == 2 || coap != NULL)
1357 goto jearg;
1358 c_shell(&linebuf[3]);
1359 goto jhistcont;
1360 case ':':
1361 /* FALLTHRU */
1362 case '_':
1363 /* Escape to command mode, but be nice! *//* TODO command expansion
1364 * TODO should be handled here so that we have unique history! */
1365 if(cnt == 2)
1366 goto jearg;
1367 _execute_command(hp, &linebuf[3], cnt -= 3);
1368 break;
1369 case '.':
1370 /* Simulate end of file on input */
1371 if(cnt != 2 || coap != NULL)
1372 goto jearg;
1373 goto jout;
1374 case 'x':
1375 /* Same as 'q', but no *DEAD* saving */
1376 /* FALLTHRU */
1377 case 'q':
1378 /* Force a quit, act like an interrupt had happened */
1379 if(cnt != 2)
1380 goto jearg;
1381 ++_coll_hadintr;
1382 _collint((c == 'x') ? 0 : SIGINT);
1383 exit(EXIT_ERR);
1384 /*NOTREACHED*/
1385 case 'h':
1386 /* Grab a bunch of headers */
1387 if(cnt != 2)
1388 goto jearg;
1390 grab_headers(n_LEXINPUT_CTX_COMPOSE, hp,
1391 (GTO | GSUBJECT | GCC | GBCC),
1392 (ok_blook(bsdcompat) && ok_blook(bsdorder)));
1393 while(hp->h_to == NULL);
1394 break;
1395 case 'H':
1396 /* Grab extra headers */
1397 if(cnt != 2)
1398 goto jearg;
1400 grab_headers(n_LEXINPUT_CTX_COMPOSE, hp, GEXTRA, 0);
1401 while(check_from_and_sender(hp->h_from, hp->h_sender) == NULL);
1402 break;
1403 case 't':
1404 /* Add to the To: list */
1405 if(cnt == 2)
1406 goto jearg;
1407 hp->h_to = cat(hp->h_to,
1408 checkaddrs(lextract(&linebuf[3], GTO | GFULL), EACM_NORMAL,
1409 NULL));
1410 break;
1411 case 's':
1412 /* Set the Subject list */
1413 if(cnt == 2)
1414 goto jearg;
1415 /* Subject:; take care for Debian #419840 and strip any \r and \n */
1416 if(n_anyof_cp("\n\r", hp->h_subject = savestr(&linebuf[3]))){
1417 char *xp;
1419 n_err(_("-s: normalizing away invalid ASCII NL / CR bytes\n"));
1420 for(xp = hp->h_subject; *xp != '\0'; ++xp)
1421 if(*xp == '\n' || *xp == '\r')
1422 *xp = ' ';
1424 break;
1425 case '@':
1426 /* Edit the attachment list */
1427 if(cnt != 2)
1428 append_attachments(n_LEXINPUT_CTX_COMPOSE, &hp->h_attach,
1429 &linebuf[3]);
1430 else
1431 edit_attachments(n_LEXINPUT_CTX_COMPOSE, &hp->h_attach);
1432 break;
1433 case 'c':
1434 /* Add to the CC list */
1435 if(cnt == 2)
1436 goto jearg;
1437 hp->h_cc = cat(hp->h_cc,
1438 checkaddrs(lextract(&linebuf[3], GCC | GFULL), EACM_NORMAL,
1439 NULL));
1440 break;
1441 case 'b':
1442 /* Add stuff to blind carbon copies list */
1443 if(cnt == 2)
1444 goto jearg;
1445 hp->h_bcc = cat(hp->h_bcc,
1446 checkaddrs(lextract(&linebuf[3], GBCC | GFULL), EACM_NORMAL,
1447 NULL));
1448 break;
1449 case 'd':
1450 if(cnt != 2)
1451 goto jearg;
1452 cp = n_getdeadletter();
1453 if(0){
1454 /*FALLTHRU*/
1455 case 'R':
1456 case 'r':
1457 case '<':
1458 /* Invoke a file: Search for the file name, then open it and copy
1459 * the contents to _coll_fp */
1460 if(cnt == 2){
1461 n_err(_("Interpolate what file?\n"));
1462 break;
1464 if(*(cp = &linebuf[3]) == '!'){
1465 insertcommand(_coll_fp, ++cp);
1466 goto jhistcont;
1468 if((cp = fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO)) == NULL)
1469 break;
1471 if(is_dir(cp)){
1472 n_err(_("%s: is a directory\n"), n_shexp_quote_cp(cp, FAL0));
1473 break;
1475 if(_include_file(cp, &lc, &cc, FAL0, (c == 'R')) != 0){
1476 if(ferror(_coll_fp))
1477 goto jerr;
1478 break;
1480 printf(_("%s %d/%d\n"), n_shexp_quote_cp(cp, FAL0), lc, cc);
1481 break;
1482 case 'i':
1483 /* Insert a variable into the file */
1484 if(cnt == 2)
1485 goto jearg;
1486 if((cp = vok_vlook(&linebuf[3])) == NULL || *cp == '\0')
1487 break;
1488 if(putesc(cp, _coll_fp) < 0) /* TODO v15: user resp upon `set' time */
1489 goto jerr;
1490 if((options & OPT_INTERACTIVE) && putesc(cp, stdout) < 0)
1491 goto jerr;
1492 break;
1493 case 'a':
1494 case 'A':
1495 /* Insert the contents of a signature variable */
1496 if(cnt != 2)
1497 goto jearg;
1498 cp = (c == 'a') ? ok_vlook(sign) : ok_vlook(Sign);
1499 if(cp != NULL && *cp != '\0'){
1500 if(putesc(cp, _coll_fp) < 0) /* TODO v15: user upon `set' time */
1501 goto jerr;
1502 if((options & OPT_INTERACTIVE) && putesc(cp, stdout) < 0)
1503 goto jerr;
1505 break;
1506 case 'w':
1507 /* Write the message on a file */
1508 if(cnt == 2)
1509 goto jearg;
1510 if((cp = fexpand(&linebuf[3], FEXP_LOCAL | FEXP_NOPROTO)) == NULL){
1511 n_err(_("Write what file!?\n"));
1512 break;
1514 rewind(_coll_fp);
1515 if(exwrite(cp, _coll_fp, 1) < 0)
1516 goto jerr;
1517 break;
1518 case 'm':
1519 case 'M':
1520 case 'f':
1521 case 'F':
1522 case 'u':
1523 case 'U':
1524 /* Interpolate the named messages, if we are in receiving mail mode.
1525 * Does the standard list processing garbage. If ~f is given, we
1526 * don't shift over */
1527 if(cnt == 2)
1528 goto jearg;
1529 if(forward(&linebuf[3], _coll_fp, c) < 0)
1530 break;
1531 break;
1532 case 'p':
1533 /* Print current state of the message without altering anything */
1534 if(cnt != 2)
1535 goto jearg;
1536 print_collf(_coll_fp, hp, ((options & OPT_INTERACTIVE) != 0));
1537 break;
1538 case '|':
1539 /* Pipe message through command. Collect output as new message */
1540 if(cnt == 2)
1541 goto jearg;
1542 rewind(_coll_fp);
1543 mespipe(&linebuf[3]);
1544 goto jhistcont;
1545 case 'v':
1546 case 'e':
1547 /* Edit the current message. 'e' -> use EDITOR, 'v' -> use VISUAL */
1548 if(cnt != 2 || coap != NULL)
1549 goto jearg;
1550 rewind(_coll_fp);
1551 mesedit(c, ok_blook(editheaders) ? hp : NULL);
1552 goto jhistcont;
1553 case '^':
1554 if(!a_collect_plumbing(&linebuf[3], hp))
1555 goto jearg;
1556 if(options & OPT_INTERACTIVE)
1557 break;
1558 continue;
1559 case '?':
1560 /* Last the lengthy help string. (Very ugly, but take care for
1561 * compiler supported string lengths :() */
1562 puts(_(
1563 "COMMAND ESCAPES (to be placed after a newline) excerpt:\n"
1564 "~. Commit and send message\n"
1565 "~: <command> Execute a mail command\n"
1566 "~<! <command> Insert output of command\n"
1567 "~@ [<files>] Edit attachment list\n"
1568 "~A Insert *Sign* variable (`~a': insert *sign*)\n"
1569 "~c <users> Add users to Cc: list (`~b': to Bcc:)\n"
1570 "~d Read in $DEAD (dead.letter)\n"
1571 "~e Edit message via $EDITOR"
1573 puts(_(
1574 "~F <msglist> Read in with headers, don't *indentprefix* lines\n"
1575 "~f <msglist> Like ~F, but honour `ignore' / `retain' configuration\n"
1576 "~H Edit From:, Reply-To: and Sender:\n"
1577 "~h Prompt for Subject:, To:, Cc: and \"blind\" Bcc:\n"
1578 "~i <variable> Insert a value and a newline\n"
1579 "~M <msglist> Read in with headers, *indentprefix* (`~m': `retain' etc.)\n"
1580 "~p Show current message compose buffer\n"
1581 "~r <file> Read in a file (`~R': *indentprefix* lines)"
1583 puts(_(
1584 "~s <subject> Set Subject:\n"
1585 "~t <users> Add users to To: list\n"
1586 "~u <msglist> Read in message(s) without headers (`~U': indent lines)\n"
1587 "~v Edit message via $VISUAL\n"
1588 "~w <file> Write message onto file\n"
1589 "~x Abort composition, discard message (`~q': save in $DEAD)\n"
1590 "~| <command> Pipe message through shell filter"
1592 if(cnt != 2)
1593 goto jearg;
1594 break;
1597 /* Finally place an entry in history as applicable */
1598 if(0){
1599 jhistcont:
1600 c = '\1';
1601 }else
1602 c = '\0';
1603 if(options & OPT_INTERACTIVE)
1604 n_tty_addhist(linebuf, TRU1);
1605 if(c != '\0')
1606 goto jcont;
1609 jout:
1610 /* Do we have *on-compose-done-shell*?
1611 * TODO With the future improvements envisioned for a_LEX_SLICE in
1612 * TODO lex_input.c we could easily offer a "normal" *on-compose-done*.
1613 * TODO With either some kind of `capture_stdout [:VAR:] [--] COMMAND',
1614 * TODO backtick (or ``{..}') operator support for `if' and WYSH lists,
1615 * TODO and/or WYSH pipes (i.e., `|' on a WYSH command line) and a `read'
1616 * TODO command this may become useful, e.g., the latter _could_ be:
1617 * TODO "~^header list to | read x y; if $x == 210 ...", but granted that
1618 * TODO this also needs ";" and some loop construct would be nice...
1619 * TODO But maybe we will have better exit/return status support until then
1620 * TODO and ~^ could return the "210" in this case as *-exit-status* without
1621 * TODO causing any harm...
1622 * TODO Finally: anyway we want `localopts' for _all_ these hooks!
1623 * TODO Also: usual f...ed up state of signals and terminal etc. */
1624 if(coap == NULL &&
1625 (cp = ok_vlook(on_compose_done_shell)) != NULL){
1626 i = strlen(cp) +1;
1627 coap = n_lofi_alloc(sizeof *coap -
1628 n_VFIELD_SIZEOF(struct a_coll_ocds_arg, coa_cmd) + i);
1629 coap->coa_pipe[0] = coap->coa_pipe[1] = -1;
1630 coap->coa_stdin = coap->coa_stdout = NULL;
1631 coap->coa_senderr = checkaddr_err;
1632 memcpy(coap->coa_cmd, cp, i);
1634 hold_all_sigs();
1635 coap->coa_opipe = safe_signal(SIGPIPE, SIG_IGN);
1636 coap->coa_oint = safe_signal(SIGINT, SIG_IGN);
1637 rele_all_sigs();
1639 if(pipe_cloexec(coap->coa_pipe) != -1 &&
1640 (coap->coa_stdin = Fdopen(coap->coa_pipe[0], "r", FAL0)) != NULL &&
1641 (coap->coa_stdout = Popen(coap->coa_cmd, "W", ok_vlook(SHELL), NULL,
1642 coap->coa_pipe[1])) != NULL){
1643 close(coap->coa_pipe[1]);
1644 coap->coa_pipe[1] = -1;
1646 temporary_call_compose_mode_hook(NULL, NULL, NULL);
1647 n_source_slice_hack(coap->coa_cmd, coap->coa_stdin, coap->coa_stdout,
1648 (options & ~OPT_INTERACTIVE), &a_coll_ocds__finalize, &coap);
1649 /* Hook version protocol for ~^: update manual upon change! */
1650 fputs(a_COLL_PLUMBING_VERSION "\n", coap->coa_stdout);
1651 #undef a_COLL_PLUMBING_VERSION
1652 goto jcont;
1655 c = errno;
1656 a_coll_ocds__finalize(coap);
1657 n_perr(_("Cannot invoke *on-compose-done-shell*"), c);
1658 goto jerr;
1660 if(*checkaddr_err != 0){
1661 *checkaddr_err = 0;
1662 goto jerr;
1665 /* Execute compose-leave TODO completely v15-compat intermediate!! */
1666 if((cp = ok_vlook(on_compose_leave)) != NULL){
1667 setup_from_and_sender(hp);
1668 temporary_call_compose_mode_hook(cp, &a_coll__hook_setter, hp);
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 /* Cannot do since it may require turning this into a multipart one */
1692 if(options & OPT_Mm_FLAG)
1693 goto jskiptails;
1695 /* Place signature? */
1696 if((cp = ok_vlook(signature)) != NULL && *cp != '\0'){
1697 char const *cpq;
1699 if((cpq = fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO)) == NULL){
1700 n_err(_("*signature* expands to invalid file: %s\n"),
1701 n_shexp_quote_cp(cp, FAL0));
1702 goto jerr;
1704 cpq = n_shexp_quote_cp(cp = cpq, FAL0);
1706 if((sigfp = Fopen(cp, "r")) == NULL){
1707 n_err(_("Can't open *signature* %s: %s\n"), cpq, strerror(errno));
1708 goto jerr;
1711 if(linebuf == NULL)
1712 linebuf = smalloc(linesize = LINESIZE);
1713 c = '\0';
1714 while((i = fread(linebuf, sizeof *linebuf, linesize, n_UNVOLATILE(sigfp)))
1715 > 0){
1716 c = linebuf[i - 1];
1717 if(i != fwrite(linebuf, sizeof *linebuf, i, _coll_fp))
1718 goto jerr;
1721 /* C99 */{
1722 FILE *x = n_UNVOLATILE(sigfp);
1723 int e = errno, ise = ferror(x);
1725 sigfp = NULL;
1726 Fclose(x);
1728 if(ise){
1729 n_err(_("Errors while reading *signature* %s: %s\n"),
1730 cpq, strerror(e));
1731 goto jerr;
1735 if(c != '\0' && c != '\n')
1736 putc('\n', _coll_fp);
1739 { char const *cp_obsolete = ok_vlook(NAIL_TAIL);
1741 if(cp_obsolete != NULL)
1742 OBSOLETE(_("please use *message-inject-tail* instead of *NAIL_TAIL*"));
1744 if((cp = ok_vlook(message_inject_tail)) != NULL ||
1745 (cp = cp_obsolete) != NULL){
1746 if(putesc(cp, _coll_fp) < 0)
1747 goto jerr;
1748 if((options & OPT_INTERACTIVE) && putesc(cp, stdout) < 0)
1749 goto jerr;
1753 jskiptails:
1754 if(fflush(_coll_fp))
1755 goto jerr;
1756 rewind(_coll_fp);
1758 jleave:
1759 temporary_unroll_compose_mode();
1760 if (linebuf != NULL)
1761 free(linebuf);
1762 sigfillset(&nset);
1763 sigprocmask(SIG_BLOCK, &nset, NULL);
1764 pstate &= ~PS_RECURSED;
1765 safe_signal(SIGINT, _coll_saveint);
1766 safe_signal(SIGHUP, _coll_savehup);
1767 sigprocmask(SIG_SETMASK, &oset, NULL);
1768 NYD_LEAVE;
1769 return _coll_fp;
1771 jerr:
1772 if(coap != NULL && coap != (struct a_coll_ocds_arg*)-1)
1773 n_source_slice_hack_remove_after_jump();
1774 if(sigfp != NULL)
1775 Fclose(n_UNVOLATILE(sigfp));
1776 if (_coll_fp != NULL) {
1777 Fclose(_coll_fp);
1778 _coll_fp = NULL;
1780 assert(checkaddr_err != NULL);
1781 /* TODO We don't save in $DEAD upon error because msg not readily composed?
1782 * TODO But this no good, it should go ZOMBIE / DRAFT / POSTPONED or what! */
1783 if(*checkaddr_err != 0)
1784 n_err(_("Some addressees were classified as \"hard error\"\n"));
1785 else if(_coll_hadintr == 0){
1786 *checkaddr_err = TRU1; /* TODO ugly: "sendout_error" now.. */
1787 n_err(_("Failed to prepare composed message (I/O error, disk full?)\n"));
1789 goto jleave;
1792 /* s-it-mode */