For attachments, we have paths and filenames
[s-mailx.git] / collect.c
blob0dc9f44b050d23416b72c854ff758baed385ed99
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 (%s)\n", cp, csi, cs,
320 n_shexp_quote_cp(ap->a_name, FAL0),
321 n_shexp_quote_cp(ap->a_path, FAL0));
326 jendpipe:
327 if (obuf != stdout)
328 n_pager_close(obuf);
329 if (lbuf != NULL)
330 free(lbuf);
331 safe_signal(SIGINT, sigint);
332 NYD_LEAVE;
335 static int
336 exwrite(char const *name, FILE *fp, int f)
338 FILE *of;
339 int c, rv;
340 long lc, cc;
341 NYD_ENTER;
343 if (f) {
344 printf("%s ", n_shexp_quote_cp(name, FAL0));
345 fflush(stdout);
348 if ((of = Fopen(name, "a")) == NULL) {
349 n_perr(name, 0);
350 goto jerr;
353 lc = cc = 0;
354 while ((c = getc(fp)) != EOF) {
355 ++cc;
356 if (c == '\n')
357 ++lc;
358 if (putc(c, of) == EOF) {
359 n_perr(name, 0);
360 goto jerr;
363 printf(_("%ld/%ld\n"), lc, cc);
365 rv = 0;
366 jleave:
367 if(of != NULL)
368 Fclose(of);
369 fflush(stdout);
370 NYD_LEAVE;
371 return rv;
372 jerr:
373 putchar('-');
374 putchar('\n');
375 rv = -1;
376 goto jleave;
379 static enum okay
380 makeheader(FILE *fp, struct header *hp, si8_t *checkaddr_err)
382 FILE *nf;
383 int c;
384 enum okay rv = STOP;
385 NYD_ENTER;
387 if ((nf = Ftmp(NULL, "colhead", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==NULL) {
388 n_perr(_("temporary mail edit file"), 0);
389 goto jleave;
392 extract_header(fp, hp, checkaddr_err);
393 if (checkaddr_err != NULL && *checkaddr_err != 0)
394 goto jleave;
396 while ((c = getc(fp)) != EOF) /* XXX bytewise, yuck! */
397 putc(c, nf);
398 if (fp != _coll_fp)
399 Fclose(_coll_fp);
400 Fclose(fp);
401 _coll_fp = nf;
402 if (check_from_and_sender(hp->h_from, hp->h_sender) == NULL)
403 goto jleave;
404 rv = OKAY;
405 jleave:
406 NYD_LEAVE;
407 return rv;
410 static void
411 mesedit(int c, struct header *hp)
413 bool_t saved;
414 sighandler_type sigint;
415 FILE *nf;
416 NYD_ENTER;
418 if(!(saved = ok_blook(add_file_recipients)))
419 ok_bset(add_file_recipients);
421 sigint = safe_signal(SIGINT, SIG_IGN);
422 nf = run_editor(_coll_fp, (off_t)-1, c, FAL0, hp, NULL, SEND_MBOX, sigint);
423 if (nf != NULL) {
424 if (hp) {
425 rewind(nf);
426 makeheader(nf, hp, NULL);
427 } else {
428 fseek(nf, 0L, SEEK_END);
429 Fclose(_coll_fp);
430 _coll_fp = nf;
433 safe_signal(SIGINT, sigint);
435 if(!saved)
436 ok_bclear(add_file_recipients);
437 NYD_LEAVE;
440 static void
441 mespipe(char *cmd)
443 FILE *nf;
444 sighandler_type sigint;
445 NYD_ENTER;
447 sigint = safe_signal(SIGINT, SIG_IGN);
449 if ((nf = Ftmp(NULL, "colpipe", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==NULL) {
450 n_perr(_("temporary mail edit file"), 0);
451 goto jout;
454 /* stdin = current message. stdout = new message */
455 fflush(_coll_fp);
456 if (run_command(ok_vlook(SHELL), 0, fileno(_coll_fp), fileno(nf), "-c",
457 cmd, NULL, NULL) < 0) {
458 Fclose(nf);
459 goto jout;
462 if (fsize(nf) == 0) {
463 n_err(_("No bytes from %s !?\n"), n_shexp_quote_cp(cmd, FAL0));
464 Fclose(nf);
465 goto jout;
468 /* Take new files */
469 fseek(nf, 0L, SEEK_END);
470 Fclose(_coll_fp);
471 _coll_fp = nf;
472 jout:
473 safe_signal(SIGINT, sigint);
474 NYD_LEAVE;
477 static int
478 forward(char *ms, FILE *fp, int f)
480 int *msgvec, rv = 0;
481 struct n_ignore const *itp;
482 char const *tabst;
483 enum sendaction action;
484 NYD_ENTER;
486 msgvec = salloc((size_t)(msgCount + 1) * sizeof *msgvec);
487 if (getmsglist(ms, msgvec, 0) < 0)
488 goto jleave;
489 if (*msgvec == 0) {
490 *msgvec = first(0, MMNORM);
491 if (*msgvec == 0) {
492 n_err(_("No appropriate messages\n"));
493 goto jleave;
495 msgvec[1] = 0;
498 if (f == 'f' || f == 'F' || f == 'u')
499 tabst = NULL;
500 else if ((tabst = ok_vlook(indentprefix)) == NULL)
501 tabst = INDENT_DEFAULT;
502 if (f == 'u' || f == 'U')
503 itp = n_IGNORE_ALL;
504 else
505 itp = upperchar(f) ? NULL : n_IGNORE_TYPE;
506 action = (upperchar(f) && f != 'U') ? SEND_QUOTE_ALL : SEND_QUOTE;
508 printf(_("Interpolating:"));
509 srelax_hold();
510 for (; *msgvec != 0; ++msgvec) {
511 struct message *mp = message + *msgvec - 1;
513 touch(mp);
514 printf(" %d", *msgvec);
515 fflush(stdout);
516 if (sendmp(mp, fp, itp, tabst, action, NULL) < 0) {
517 n_perr(_("temporary mail file"), 0);
518 rv = -1;
519 break;
521 srelax();
523 srelax_rele();
524 printf("\n");
525 jleave:
526 NYD_LEAVE;
527 return rv;
530 static bool_t
531 a_collect_plumbing(char const *ms, struct header *hp){
532 /* TODO _collect_plumbing: instead of fields the basic headers should
533 * TODO be in an array and have IDs, like in termcap etc., so then this
534 * TODO could be simplified as table-walks */
535 char const *cp, *cmd[4];
536 NYD2_ENTER;
538 /* Protcol version for *on-compose-done-shell* -- update manual on change! */
539 #define a_COLL_PLUMBING_VERSION "0 0 1"
540 cp = ms;
542 /* C99 */{
543 size_t i;
545 for(i = 0; i < n_NELEM(cmd); ++i){
546 while(blankchar(*cp))
547 ++cp;
548 if(*cp == '\0')
549 cmd[i] = NULL;
550 else{
551 if(i < n_NELEM(cmd) - 1)
552 for(cmd[i] = cp++; *cp != '\0' && !blankchar(*cp); ++cp)
554 else{
555 /* Last slot takes all the rest of the line, less trailing WS */
556 for(cmd[i] = cp++; *cp != '\0'; ++cp)
558 while(blankchar(cp[-1]))
559 --cp;
561 cmd[i] = savestrbuf(cmd[i], PTR2SIZE(cp - cmd[i]));
566 if(n_UNLIKELY(cmd[0] == NULL))
567 goto jecmd;
568 if(!asccasecmp(cmd[0], "header")){
569 struct n_header_field *hfp;
570 struct name *np, **npp;
572 if(cmd[1] == NULL || !asccasecmp(cmd[1], "list")){
573 if(cmd[2] == NULL){
574 fputs("210", stdout);
575 if(hp->h_from != NULL) fputs(" From", stdout);
576 if(hp->h_sender != NULL) fputs(" Sender", stdout);
577 if(hp->h_to != NULL) fputs(" To", stdout);
578 if(hp->h_cc != NULL) fputs(" Cc", stdout);
579 if(hp->h_bcc != NULL) fputs(" Bcc", stdout);
580 if(hp->h_subject != NULL) fputs(" Subject", stdout);
581 if(hp->h_replyto != NULL) fputs(" Reply-To", stdout);
582 if(hp->h_mft != NULL) fputs(" Mail-Followup-To", stdout);
583 if(hp->h_message_id != NULL) fputs(" Message-ID", stdout);
584 if(hp->h_ref != NULL) fputs(" References", stdout);
585 if(hp->h_in_reply_to != NULL) fputs(" In-Reply-To", stdout);
586 for(hfp = hp->h_user_headers; hfp != NULL; hfp = hfp->hf_next){
587 putc(' ', stdout);
588 fputs(&hfp->hf_dat[0], stdout);
590 putc('\n', stdout);
591 }else{
592 if(cmd[3] != NULL)
593 goto jecmd;
594 if(!asccasecmp(cmd[2], "from")){
595 np = hp->h_from;
596 jlist:
597 fprintf(stdout, "%s %s\n", (np == NULL ? "501" : "210"), cp);
598 }else if(!asccasecmp(cmd[2], cp = "Sender")){
599 np = hp->h_sender;
600 goto jlist;
601 }else if(!asccasecmp(cmd[2], cp = "To")){
602 np = hp->h_to;
603 goto jlist;
604 }else if(!asccasecmp(cmd[2], cp = "Cc")){
605 np = hp->h_cc;
606 goto jlist;
607 }else if(!asccasecmp(cmd[2], cp = "Bcc")){
608 np = hp->h_bcc;
609 goto jlist;
610 }else if(!asccasecmp(cmd[2], cp = "Subject")){
611 np = (struct name*)-1;
612 goto jlist;
613 }else if(!asccasecmp(cmd[2], cp = "Reply-To")){
614 np = hp->h_replyto;
615 goto jlist;
616 }else if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
617 np = hp->h_mft;
618 goto jlist;
619 }else if(!asccasecmp(cmd[2], cp = "Message-ID")){
620 np = hp->h_message_id;
621 goto jlist;
622 }else if(!asccasecmp(cmd[2], cp = "References")){
623 np = hp->h_ref;
624 goto jlist;
625 }else if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
626 np = hp->h_in_reply_to;
627 goto jlist;
628 }else{
629 /* Primitive name normalization XXX header object should
630 * XXX have a more sophisticated accessible one */
631 char *xp;
633 cp = xp = savestr(cmd[2]);
634 xp[0] = upperchar(xp[0]);
635 while(*++xp != '\0')
636 xp[0] = lowerchar(xp[0]);
638 for(hfp = hp->h_user_headers;; hfp = hfp->hf_next){
639 if(hfp == NULL)
640 goto j501cp;
641 else if(!asccasecmp(cp, &hfp->hf_dat[0])){
642 fprintf(stdout, "210 %s\n", cp);
643 break;
648 }else if(!asccasecmp(cmd[1], "show")){
649 if(cmd[2] == NULL || cmd[3] != NULL)
650 goto jecmd;
651 if(!asccasecmp(cmd[2], "from")){
652 np = hp->h_from;
653 jshow:
654 if(np != NULL){
655 fprintf(stdout, "211 %s\n", cp);
657 fprintf(stdout, "%s %s\n", np->n_name, np->n_fullname);
658 while((np = np->n_flink) != NULL);
659 putc('\n', stdout);
660 }else
661 goto j501cp;
662 }else if(!asccasecmp(cmd[2], cp = "Sender")){
663 np = hp->h_sender;
664 goto jshow;
665 }else if(!asccasecmp(cmd[2], cp = "To")){
666 np = hp->h_to;
667 goto jshow;
668 }else if(!asccasecmp(cmd[2], cp = "Cc")){
669 np = hp->h_cc;
670 goto jshow;
671 }else if(!asccasecmp(cmd[2], cp = "Bcc")){
672 np = hp->h_bcc;
673 goto jshow;
674 }else if(!asccasecmp(cmd[2], cp = "Reply-To")){
675 np = hp->h_replyto;
676 goto jshow;
677 }else if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
678 np = hp->h_mft;
679 goto jshow;
680 }else if(!asccasecmp(cmd[2], cp = "Message-ID")){
681 np = hp->h_message_id;
682 goto jshow;
683 }else if(!asccasecmp(cmd[2], cp = "References")){
684 np = hp->h_ref;
685 goto jshow;
686 }else if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
687 np = hp->h_in_reply_to;
688 goto jshow;
689 }else if(!asccasecmp(cmd[2], cp = "Subject")){
690 if(hp->h_subject != NULL)
691 fprintf(stdout, "212 %s\n%s\n\n", cp, hp->h_subject);
692 else
693 fprintf(stdout, "501 %s\n", cp);
694 }else{
695 /* Primitive name normalization XXX header object should
696 * XXX have a more sophisticated accessible one */
697 bool_t any;
698 char *xp;
700 cp = xp = savestr(cmd[2]);
701 xp[0] = upperchar(xp[0]);
702 while(*++xp != '\0')
703 xp[0] = lowerchar(xp[0]);
705 for(any = FAL0, hfp = hp->h_user_headers; hfp != NULL;
706 hfp = hfp->hf_next){
707 if(!asccasecmp(cp, &hfp->hf_dat[0])){
708 if(!any)
709 fprintf(stdout, "212 %s\n", cp);
710 any = TRU1;
711 fprintf(stdout, "%s\n", &hfp->hf_dat[hfp->hf_nl +1]);
714 if(any)
715 putc('\n', stdout);
716 else
717 goto j501cp;
719 }else if(!asccasecmp(cmd[1], "remove")){
720 if(cmd[2] == NULL || cmd[3] != NULL)
721 goto jecmd;
722 if(!asccasecmp(cmd[2], "from")){
723 npp = &hp->h_from;
724 jrem:
725 if(*npp != NULL){
726 *npp = NULL;
727 fprintf(stdout, "210 %s\n", cp);
728 }else
729 goto j501cp;
730 }else if(!asccasecmp(cmd[2], cp = "Sender")){
731 npp = &hp->h_sender;
732 goto jrem;
733 }else if(!asccasecmp(cmd[2], cp = "To")){
734 npp = &hp->h_to;
735 goto jrem;
736 }else if(!asccasecmp(cmd[2], cp = "Cc")){
737 npp = &hp->h_cc;
738 goto jrem;
739 }else if(!asccasecmp(cmd[2], cp = "Bcc")){
740 npp = &hp->h_bcc;
741 goto jrem;
742 }else if(!asccasecmp(cmd[2], cp = "Reply-To")){
743 npp = &hp->h_replyto;
744 goto jrem;
745 }else if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
746 npp = &hp->h_mft;
747 goto jrem;
748 }else if(!asccasecmp(cmd[2], cp = "Message-ID")){
749 npp = &hp->h_message_id;
750 goto jrem;
751 }else if(!asccasecmp(cmd[2], cp = "References")){
752 npp = &hp->h_ref;
753 goto jrem;
754 }else if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
755 npp = &hp->h_in_reply_to;
756 goto jrem;
757 }else if(!asccasecmp(cmd[2], cp = "Subject")){
758 if(hp->h_subject != NULL){
759 hp->h_subject = NULL;
760 fprintf(stdout, "210 %s\n", cp);
761 }else
762 goto j501cp;
763 }else{
764 /* Primitive name normalization XXX header object should
765 * XXX have a more sophisticated accessible one */
766 struct n_header_field **hfpp;
767 bool_t any;
768 char *xp;
770 cp = xp = savestr(cmd[2]);
771 xp[0] = upperchar(xp[0]);
772 while(*++xp != '\0')
773 xp[0] = lowerchar(xp[0]);
775 for(any = FAL0, hfpp = &hp->h_user_headers; (hfp = *hfpp) != NULL;){
776 if(!asccasecmp(cp, &hfp->hf_dat[0])){
777 *hfpp = hfp->hf_next;
778 if(!any)
779 fprintf(stdout, "210 %s\n", cp);
780 any = TRU1;
781 }else
782 hfp = *(hfpp = &hfp->hf_next);
784 if(!any)
785 goto j501cp;
787 }else if(!asccasecmp(cmd[1], "insert")){ /* TODO LOGIC BELONGS head.c!
788 * TODO That is: Header::factory(string) -> object (blahblah).
789 * TODO I.e., as long as we don't have regular RFC compliant parsers
790 * TODO which differentiate in between structured and unstructured
791 * TODO header fields etc., a little workaround */
792 si8_t aerr;
793 enum expand_addr_check_mode eacm;
794 enum gfield ntype;
795 bool_t mult_ok;
797 mult_ok = TRU1;
798 ntype = GEXTRA | GFULL | GFULLEXTRA;
799 eacm = EACM_STRICT;
801 if(cmd[2] == NULL || cmd[3] == NULL)
802 goto jecmd;
803 if(!asccasecmp(cmd[2], "from")){
804 npp = &hp->h_from;
805 jins:
806 aerr = 0;
807 if((np = lextract(cmd[3], ntype)) == NULL)
808 goto j501cp;
809 else if((np = checkaddrs(np, eacm, &aerr), aerr != 0))
810 fprintf(stdout, "505 %s\n", cp);
811 else if(!mult_ok && (np->n_flink != NULL || *npp != NULL))
812 fprintf(stdout, "506 %s\n", cp);
813 else{
814 *npp = cat(*npp, np);
815 fprintf(stdout, "210 %s\n", cp);
817 }else if(!asccasecmp(cmd[2], cp = "Sender")){
818 mult_ok = FAL0;
819 npp = &hp->h_sender;
820 goto jins;
821 }else if(!asccasecmp(cmd[2], cp = "To")){
822 npp = &hp->h_to;
823 ntype = GTO | GFULL;
824 eacm = EACM_NORMAL | EAF_NAME;
825 goto jins;
826 }else if(!asccasecmp(cmd[2], cp = "Cc")){
827 npp = &hp->h_cc;
828 ntype = GCC | GFULL;
829 eacm = EACM_NORMAL | EAF_NAME;
830 goto jins;
831 }else if(!asccasecmp(cmd[2], cp = "Bcc")){
832 npp = &hp->h_bcc;
833 ntype = GCC | GFULL;
834 eacm = EACM_NORMAL | EAF_NAME;
835 goto jins;
836 }else if(!asccasecmp(cmd[2], cp = "Reply-To")){
837 npp = &hp->h_replyto;
838 goto jins;
839 }else if(!asccasecmp(cmd[2], cp = "Mail-Followup-To")){
840 npp = &hp->h_mft;
841 eacm = EACM_NONAME;
842 goto jins;
843 }else if(!asccasecmp(cmd[2], cp = "Message-ID")){
844 mult_ok = FAL0;
845 npp = &hp->h_message_id;
846 eacm = EACM_NONAME;
847 goto jins;
848 }else if(!asccasecmp(cmd[2], cp = "References")){
849 npp = &hp->h_ref;
850 ntype = GREF;
851 eacm = EACM_NONAME;
852 goto jins;
853 }else if(!asccasecmp(cmd[2], cp = "In-Reply-To")){
854 npp = &hp->h_in_reply_to;
855 ntype = GREF;
856 eacm = EACM_NONAME;
857 goto jins;
858 }else if(!asccasecmp(cmd[2], cp = "Subject")){
859 if(cmd[3][0] != '\0'){
860 if(hp->h_subject != NULL)
861 hp->h_subject = savecatsep(hp->h_subject, ' ', cmd[3]);
862 else
863 hp->h_subject = savestr(cmd[3]);
864 fprintf(stdout, "210 %s\n", cp);
865 }else
866 goto j501cp;
867 }else{
868 /* Primitive name normalization XXX header object should
869 * XXX have a more sophisticated accessible one */
870 size_t nl, bl;
871 struct n_header_field **hfpp;
873 for(cp = cmd[2]; *cp != '\0'; ++cp)
874 if(!fieldnamechar(*cp)){
875 cp = cmd[2];
876 goto j501cp;
879 for(hfpp = &hp->h_user_headers; *hfpp != NULL;)
880 hfpp = &(*hfpp)->hf_next;
882 nl = strlen(cp = cmd[2]);
883 bl = strlen(cmd[3]) +1;
884 *hfpp = hfp = salloc(n_VSTRUCT_SIZEOF(struct n_header_field, hf_dat
885 ) + nl +1 + bl);
886 hfp->hf_next = NULL;
887 hfp->hf_nl = nl;
888 hfp->hf_bl = bl - 1;
889 memcpy(hfp->hf_dat, cp, nl);
890 hfp->hf_dat[nl++] = '\0';
891 memcpy(hfp->hf_dat + nl, cmd[3], bl);
892 fprintf(stdout, "210 %s\n", cp);
894 }else
895 goto jecmd;
896 }else{
897 jecmd:
898 fputs("500\n", stdout);
899 ms = NULL;
902 jleave:
903 fflush(stdout);
904 NYD2_LEAVE;
905 return (ms != NULL);
907 j501cp:
908 fputs("501 ", stdout);
909 fputs(cp, stdout);
910 putc('\n', stdout);
911 goto jleave;
914 static void
915 _collint(int s)
917 NYD_X; /* Signal handler */
919 /* the control flow is subtle, because we can be called from ~q */
920 if (_coll_hadintr == 0) {
921 if (ok_blook(ignore)) {
922 fputs("@\n", stdout);
923 fflush(stdout);
924 clearerr(stdin);
925 } else
926 _coll_hadintr = 1;
927 siglongjmp(_coll_jmp, 1);
929 exit_status |= EXIT_SEND_ERROR;
930 if (s != 0)
931 savedeadletter(_coll_fp, TRU1);
932 /* Aborting message, no need to fflush() .. */
933 siglongjmp(_coll_abort, 1);
936 static void
937 collhup(int s)
939 NYD_X; /* Signal handler */
940 n_UNUSED(s);
942 savedeadletter(_coll_fp, TRU1);
943 /* Let's pretend nobody else wants to clean up, a true statement at
944 * this time */
945 exit(EXIT_ERR);
948 static int
949 putesc(char const *s, FILE *stream)
951 int n = 0, rv = -1;
952 NYD_ENTER;
954 while (s[0] != '\0') {
955 if (s[0] == '\\') {
956 if (s[1] == 't') {
957 if (putc('\t', stream) == EOF)
958 goto jleave;
959 ++n;
960 s += 2;
961 continue;
963 if (s[1] == 'n') {
964 if (putc('\n', stream) == EOF)
965 goto jleave;
966 ++n;
967 s += 2;
968 continue;
971 if (putc(s[0], stream) == EOF)
972 goto jleave;
973 ++n;
974 ++s;
976 if (putc('\n', stream) == EOF)
977 goto jleave;
978 rv = ++n;
979 jleave:
980 NYD_LEAVE;
981 return rv;
984 static void
985 a_coll__hook_setter(void *arg){ /* TODO v15: drop */
986 struct header *hp;
987 char const *val;
988 NYD2_ENTER;
990 hp = arg;
992 if((val = detract(hp->h_from, GNAMEONLY)) == NULL)
993 val = n_empty;
994 ok_vset(compose_from, val);
995 if((val = detract(hp->h_sender, 0)) == NULL)
996 val = n_empty;
997 ok_vset(compose_sender, val);
998 if((val = detract(hp->h_to, GNAMEONLY)) == NULL)
999 val = n_empty;
1000 ok_vset(compose_to, val);
1001 if((val = detract(hp->h_cc, GNAMEONLY)) == NULL)
1002 val = n_empty;
1003 ok_vset(compose_cc, val);
1004 if((val = detract(hp->h_bcc, GNAMEONLY)) == NULL)
1005 val = n_empty;
1006 ok_vset(compose_bcc, val);
1007 if((val = hp->h_subject) == NULL)
1008 val = n_empty;
1009 ok_vset(compose_subject, val);
1010 NYD2_LEAVE;
1013 static int
1014 a_coll_ocds__mac(void){
1015 /* Executes in a fork(2)ed child */
1016 setvbuf(stdout, NULL, _IOLBF, 0);
1017 pstate |= PS_COMPOSE_FORKHOOK;
1018 temporary_call_compose_mode_hook(a_coll_ocds__macname, NULL, NULL);
1019 _exit(EXIT_OK);
1022 static void
1023 a_coll_ocds__finalize(void *vp){
1024 /* Note we use this for destruction upon setup errors, thus */
1025 sighandler_type opipe;
1026 sighandler_type oint;
1027 struct a_coll_ocds_arg **coapp, *coap;
1028 NYD2_ENTER;
1030 temporary_call_compose_mode_hook((char*)-1, NULL, NULL);
1032 coap = *(coapp = vp);
1033 *coapp = (struct a_coll_ocds_arg*)-1;
1035 if(coap->coa_stdout != NULL)
1036 if(!Pclose(coap->coa_stdout, FAL0)){
1037 *coap->coa_senderr = TRU1;
1038 n_err(_("*on-compose-done(-shell)?* failed: %s\n"),
1039 n_shexp_quote_cp(coap->coa_cmd, FAL0));
1042 if(coap->coa_stdin != NULL)
1043 Fclose(coap->coa_stdin);
1044 else if(coap->coa_pipe[0] != -1)
1045 close(coap->coa_pipe[0]);
1047 if(coap->coa_pipe[1] != -1)
1048 close(coap->coa_pipe[1]);
1050 opipe = coap->coa_opipe;
1051 oint = coap->coa_oint;
1053 n_lofi_free(coap);
1055 hold_all_sigs();
1056 safe_signal(SIGPIPE, opipe);
1057 safe_signal(SIGINT, oint);
1058 rele_all_sigs();
1059 NYD2_LEAVE;
1062 FL FILE *
1063 collect(struct header *hp, int printheaders, struct message *mp,
1064 char *quotefile, int doprefix, si8_t *checkaddr_err)
1066 struct n_ignore const *quoteitp;
1067 struct a_coll_ocds_arg *coap;
1068 int lc, cc, c;
1069 int volatile t, eofcnt, getfields;
1070 char *linebuf, escape_saved, escape;
1071 char const *cp, *coapm;
1072 size_t i, linesize; /* TODO line pool */
1073 long cnt;
1074 enum sendaction action;
1075 sigset_t oset, nset;
1076 FILE * volatile sigfp;
1077 NYD_ENTER;
1079 _coll_fp = NULL;
1080 sigfp = NULL;
1081 linesize = 0;
1082 linebuf = NULL;
1083 eofcnt = 0;
1084 coapm = NULL;
1085 coap = NULL;
1087 /* Start catching signals from here, but we're still die on interrupts
1088 * until we're in the main loop */
1089 sigfillset(&nset);
1090 sigprocmask(SIG_BLOCK, &nset, &oset);
1091 /* FIXME have dropped handlerpush() and localized onintr() in lex.c! */
1092 if ((_coll_saveint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
1093 safe_signal(SIGINT, &_collint);
1094 if ((_coll_savehup = safe_signal(SIGHUP, SIG_IGN)) != SIG_IGN)
1095 safe_signal(SIGHUP, collhup);
1096 if (sigsetjmp(_coll_abort, 1))
1097 goto jerr;
1098 if (sigsetjmp(_coll_jmp, 1))
1099 goto jerr;
1100 pstate |= PS_COMPOSE_MODE;
1101 sigprocmask(SIG_SETMASK, &oset, (sigset_t*)NULL);
1103 if ((_coll_fp = Ftmp(NULL, "collect", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
1104 NULL) {
1105 n_perr(_("temporary mail file"), 0);
1106 goto jerr;
1109 /* If we are going to prompt for a subject, refrain from printing a newline
1110 * after the headers (since some people mind) */
1111 getfields = 0;
1112 if (!(options & OPT_t_FLAG)) {
1113 t = GTO | GSUBJECT | GCC | GNL;
1114 if (ok_blook(fullnames))
1115 t |= GCOMMA;
1117 if (options & OPT_INTERACTIVE) {
1118 if (hp->h_subject == NULL && (ok_blook(ask) || ok_blook(asksub)))
1119 t &= ~GNL, getfields |= GSUBJECT;
1121 if (hp->h_to == NULL)
1122 t &= ~GNL, getfields |= GTO;
1124 if (!ok_blook(bsdcompat) && !ok_blook(askatend)) {
1125 if (hp->h_bcc == NULL && ok_blook(askbcc))
1126 t &= ~GNL, getfields |= GBCC;
1127 if (hp->h_cc == NULL && ok_blook(askcc))
1128 t &= ~GNL, getfields |= GCC;
1131 } else {
1132 n_UNINIT(t, 0);
1135 escape_saved = escape = ((cp = ok_vlook(escape)) != NULL) ? *cp : n_ESCAPE;
1136 _coll_hadintr = 0;
1138 if (!sigsetjmp(_coll_jmp, 1)) {
1139 /* Ask for some headers first, as necessary */
1140 if (getfields)
1141 grab_headers(n_LEXINPUT_CTX_COMPOSE, hp, getfields, 1);
1143 /* Execute compose-enter TODO completely v15-compat intermediate!! */
1144 if((cp = ok_vlook(on_compose_enter)) != NULL){
1145 setup_from_and_sender(hp);
1146 temporary_call_compose_mode_hook(cp, &a_coll__hook_setter, hp);
1149 /* Cannot do since it may require turning this into a multipart one */
1150 if(!(options & OPT_Mm_FLAG)){
1151 char const *cp_obsolete = ok_vlook(NAIL_HEAD);
1152 if(cp_obsolete != NULL)
1153 OBSOLETE(_("please use *message-inject-head* "
1154 "instead of *NAIL_HEAD*"));
1156 if(((cp = ok_vlook(message_inject_head)) != NULL ||
1157 (cp = cp_obsolete) != NULL) && putesc(cp, _coll_fp) < 0)
1158 goto jerr;
1160 /* Quote an original message */
1161 if (mp != NULL && (doprefix || (cp = ok_vlook(quote)) != NULL)) {
1162 quoteitp = n_IGNORE_ALL;
1163 action = SEND_QUOTE;
1164 if (doprefix) {
1165 quoteitp = n_IGNORE_FWD;
1166 if ((cp = ok_vlook(fwdheading)) == NULL)
1167 cp = "-------- Original Message --------";
1168 if (*cp != '\0' && fprintf(_coll_fp, "%s\n", cp) < 0)
1169 goto jerr;
1170 } else if (!strcmp(cp, "noheading")) {
1171 /*EMPTY*/;
1172 } else if (!strcmp(cp, "headers")) {
1173 quoteitp = n_IGNORE_TYPE;
1174 } else if (!strcmp(cp, "allheaders")) {
1175 quoteitp = NULL;
1176 action = SEND_QUOTE_ALL;
1177 } else {
1178 cp = hfield1("from", mp);
1179 if (cp != NULL && (cnt = (long)strlen(cp)) > 0) {
1180 if (xmime_write(cp, cnt, _coll_fp, CONV_FROMHDR, TD_NONE) < 0)
1181 goto jerr;
1182 if (fprintf(_coll_fp, _(" wrote:\n\n")) < 0)
1183 goto jerr;
1186 if (fflush(_coll_fp))
1187 goto jerr;
1188 if (doprefix)
1189 cp = NULL;
1190 else if ((cp = ok_vlook(indentprefix)) == NULL)
1191 cp = INDENT_DEFAULT;
1192 if (sendmp(mp, _coll_fp, quoteitp, cp, action, NULL) < 0)
1193 goto jerr;
1197 if (quotefile != NULL) {
1198 if (_include_file(quotefile, &lc, &cc, FAL0) != 0)
1199 goto jerr;
1202 if ((options & (OPT_Mm_FLAG | OPT_INTERACTIVE)) == OPT_INTERACTIVE) {
1203 /* Print what we have sofar also on the terminal (if useful) */
1204 if (!ok_blook(editalong)) {
1205 if (printheaders)
1206 puthead(TRU1, hp, stdout, t, SEND_TODISP, CONV_NONE, NULL, NULL);
1208 rewind(_coll_fp);
1209 while ((c = getc(_coll_fp)) != EOF) /* XXX bytewise, yuck! */
1210 putc(c, stdout);
1211 if (fseek(_coll_fp, 0, SEEK_END))
1212 goto jerr;
1213 } else {
1214 rewind(_coll_fp);
1215 mesedit('e', hp);
1216 /* As mandated by the Mail Reference Manual, print "(continue)" */
1217 jcont:
1218 if(options & OPT_INTERACTIVE)
1219 fputs(_("(continue)\n"), stdout);
1221 fflush(stdout);
1223 } else {
1224 /* Come here for printing the after-signal message. Duplicate messages
1225 * won't be printed because the write is aborted if we get a SIGTTOU */
1226 if (_coll_hadintr)
1227 n_err(_("\n(Interrupt -- one more to kill letter)\n"));
1230 /* If not under shell hook control */
1231 if(coap == NULL){
1232 /* We're done with -M or -m (because we are too simple minded) */
1233 if(options & OPT_Mm_FLAG)
1234 goto jout;
1235 /* No command escapes, interrupts not expected. Simply copy STDIN */
1236 if (!(options & (OPT_INTERACTIVE | OPT_t_FLAG | OPT_TILDE_FLAG))){
1237 linebuf = srealloc(linebuf, linesize = LINESIZE);
1238 while ((i = fread(linebuf, sizeof *linebuf, linesize, stdin)) > 0) {
1239 if (i != fwrite(linebuf, sizeof *linebuf, i, _coll_fp))
1240 goto jerr;
1242 goto jout;
1246 /* The interactive collect loop */
1247 for(;;){
1248 /* C99 */{
1249 enum n_lexinput_flags lif;
1251 lif = n_LEXINPUT_CTX_COMPOSE;
1252 if(options & (OPT_INTERACTIVE | OPT_TILDE_FLAG)){
1253 if(!(options & OPT_t_FLAG))
1254 lif |= n_LEXINPUT_NL_ESC;
1256 cnt = n_lex_input(lif, n_empty, &linebuf, &linesize, NULL);
1259 if (cnt < 0) {
1260 if(coap != NULL)
1261 break;
1262 if (options & OPT_t_FLAG) {
1263 fflush_rewind(_coll_fp);
1264 /* It is important to set PS_t_FLAG before extract_header() *and*
1265 * keep OPT_t_FLAG for the first parse of the message, too! */
1266 pstate |= PS_t_FLAG;
1267 if (makeheader(_coll_fp, hp, checkaddr_err) != OKAY)
1268 goto jerr;
1269 options &= ~OPT_t_FLAG;
1270 continue;
1271 } else if ((options & OPT_INTERACTIVE) &&
1272 ok_blook(ignoreeof) && ++eofcnt < 4) {
1273 printf(_("*ignoreeof* set, use `~.' to terminate letter\n"));
1274 continue;
1276 break;
1279 _coll_hadintr = 0;
1281 cp = linebuf;
1282 if(cnt == 0)
1283 goto jputnl;
1284 else if(coap == NULL){
1285 if(!(options & (OPT_INTERACTIVE | OPT_TILDE_FLAG)))
1286 goto jputline;
1287 else if(cp[0] == '.'){
1288 if(cnt == 1 && (ok_blook(dot) || ok_blook(ignoreeof)))
1289 break;
1292 if(cp[0] != escape){
1293 jputline:
1294 if(fwrite(cp, sizeof *cp, cnt, _coll_fp) != (size_t)cnt)
1295 goto jerr;
1296 /* TODO PS_READLINE_NL is a terrible hack to ensure that _in_all_-
1297 * TODO _code_paths_ a file without trailing newline isn't modified
1298 * TODO to continue one; the "saw-newline" needs to be part of an
1299 * TODO I/O input machinery object */
1300 jputnl:
1301 if(pstate & PS_READLINE_NL){
1302 if(putc('\n', _coll_fp) == EOF)
1303 goto jerr;
1305 continue;
1308 /* Cleanup the input string: like this we can perform a little bit of
1309 * usage testing and also have somewhat normalized history entries */
1310 for(cp = &linebuf[2]; (c = *cp) != '\0' && blankspacechar(c); ++cp)
1311 continue;
1312 if(c == '\0'){
1313 linebuf[2] = '\0';
1314 cnt = 2;
1315 }else{
1316 i = PTR2SIZE(cp - linebuf) - 3;
1317 memmove(&linebuf[3], cp, (cnt -= i));
1318 linebuf[2] = ' ';
1319 linebuf[cnt] = '\0';
1321 if(cnt > 0){ /* TODO v15 no more trailing WS from lex_input please */
1322 cp = &linebuf[cnt];
1324 for(;; --cp){
1325 c = cp[-1];
1326 if(!blankspacechar(c))
1327 break;
1329 ((char*)n_UNCONST(cp))[0] = '\0';
1330 cnt = PTR2SIZE(cp - linebuf);
1334 switch((c = linebuf[1])){
1335 default:
1336 /* On double escape, send a single one. Otherwise, it's an error */
1337 if(c == escape){
1338 cp = &linebuf[1];
1339 --cnt;
1340 goto jputline;
1341 }else{
1342 char buf[sizeof(n_UNIREPL)];
1344 if(asciichar(c))
1345 buf[0] = c, buf[1] = '\0';
1346 else if(options & OPT_UNICODE)
1347 memcpy(buf, n_unirepl, sizeof n_unirepl);
1348 else
1349 buf[0] = '?', buf[1] = '\0';
1350 n_err(_("Unknown command escape: ~%s\n"), buf);
1351 continue;
1353 jearg:
1354 n_err(_("Invalid command escape usage: %s\n"), linebuf);
1355 continue;
1356 case '!':
1357 /* Shell escape, send the balance of line to sh -c */
1358 if(cnt == 2 || coap != NULL)
1359 goto jearg;
1360 c_shell(&linebuf[3]);
1361 goto jhistcont;
1362 case ':':
1363 /* FALLTHRU */
1364 case '_':
1365 /* Escape to command mode, but be nice! *//* TODO command expansion
1366 * TODO should be handled here so that we have unique history! */
1367 if(cnt == 2)
1368 goto jearg;
1369 _execute_command(hp, &linebuf[3], cnt -= 3);
1370 break;
1371 case '.':
1372 /* Simulate end of file on input */
1373 if(cnt != 2 || coap != NULL)
1374 goto jearg;
1375 goto jout;
1376 case 'x':
1377 /* Same as 'q', but no *DEAD* saving */
1378 /* FALLTHRU */
1379 case 'q':
1380 /* Force a quit, act like an interrupt had happened */
1381 if(cnt != 2)
1382 goto jearg;
1383 ++_coll_hadintr;
1384 _collint((c == 'x') ? 0 : SIGINT);
1385 exit(EXIT_ERR);
1386 /*NOTREACHED*/
1387 case 'h':
1388 /* Grab a bunch of headers */
1389 if(cnt != 2)
1390 goto jearg;
1392 grab_headers(n_LEXINPUT_CTX_COMPOSE, hp,
1393 (GTO | GSUBJECT | GCC | GBCC),
1394 (ok_blook(bsdcompat) && ok_blook(bsdorder)));
1395 while(hp->h_to == NULL);
1396 break;
1397 case 'H':
1398 /* Grab extra headers */
1399 if(cnt != 2)
1400 goto jearg;
1402 grab_headers(n_LEXINPUT_CTX_COMPOSE, hp, GEXTRA, 0);
1403 while(check_from_and_sender(hp->h_from, hp->h_sender) == NULL);
1404 break;
1405 case 't':
1406 /* Add to the To: list */
1407 if(cnt == 2)
1408 goto jearg;
1409 hp->h_to = cat(hp->h_to,
1410 checkaddrs(lextract(&linebuf[3], GTO | GFULL), EACM_NORMAL,
1411 NULL));
1412 break;
1413 case 's':
1414 /* Set the Subject list */
1415 if(cnt == 2)
1416 goto jearg;
1417 /* Subject:; take care for Debian #419840 and strip any \r and \n */
1418 if(n_anyof_cp("\n\r", hp->h_subject = savestr(&linebuf[3]))){
1419 char *xp;
1421 n_err(_("-s: normalizing away invalid ASCII NL / CR bytes\n"));
1422 for(xp = hp->h_subject; *xp != '\0'; ++xp)
1423 if(*xp == '\n' || *xp == '\r')
1424 *xp = ' ';
1426 break;
1427 case '@':
1428 /* Edit the attachment list */
1429 if(cnt != 2)
1430 append_attachments(n_LEXINPUT_CTX_COMPOSE, &hp->h_attach,
1431 &linebuf[3]);
1432 else
1433 edit_attachments(n_LEXINPUT_CTX_COMPOSE, &hp->h_attach);
1434 break;
1435 case 'c':
1436 /* Add to the CC list */
1437 if(cnt == 2)
1438 goto jearg;
1439 hp->h_cc = cat(hp->h_cc,
1440 checkaddrs(lextract(&linebuf[3], GCC | GFULL), EACM_NORMAL,
1441 NULL));
1442 break;
1443 case 'b':
1444 /* Add stuff to blind carbon copies list */
1445 if(cnt == 2)
1446 goto jearg;
1447 hp->h_bcc = cat(hp->h_bcc,
1448 checkaddrs(lextract(&linebuf[3], GBCC | GFULL), EACM_NORMAL,
1449 NULL));
1450 break;
1451 case 'd':
1452 if(cnt != 2)
1453 goto jearg;
1454 cp = n_getdeadletter();
1455 if(0){
1456 /*FALLTHRU*/
1457 case 'R':
1458 case 'r':
1459 case '<':
1460 /* Invoke a file: Search for the file name, then open it and copy
1461 * the contents to _coll_fp */
1462 if(cnt == 2){
1463 n_err(_("Interpolate what file?\n"));
1464 break;
1466 if(*(cp = &linebuf[3]) == '!'){
1467 insertcommand(_coll_fp, ++cp);
1468 goto jhistcont;
1470 if((cp = fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO)) == NULL)
1471 break;
1473 if(is_dir(cp)){
1474 n_err(_("%s: is a directory\n"), n_shexp_quote_cp(cp, FAL0));
1475 break;
1477 if(_include_file(cp, &lc, &cc, (c == 'R')) != 0){
1478 if(ferror(_coll_fp))
1479 goto jerr;
1480 break;
1482 printf(_("%s %d/%d\n"), n_shexp_quote_cp(cp, FAL0), lc, cc);
1483 break;
1484 case 'i':
1485 /* Insert a variable into the file */
1486 if(cnt == 2)
1487 goto jearg;
1488 if((cp = vok_vlook(&linebuf[3])) == NULL || *cp == '\0')
1489 break;
1490 if(putesc(cp, _coll_fp) < 0) /* TODO v15: user resp upon `set' time */
1491 goto jerr;
1492 if((options & OPT_INTERACTIVE) && putesc(cp, stdout) < 0)
1493 goto jerr;
1494 break;
1495 case 'a':
1496 case 'A':
1497 /* Insert the contents of a signature variable */
1498 if(cnt != 2)
1499 goto jearg;
1500 cp = (c == 'a') ? ok_vlook(sign) : ok_vlook(Sign);
1501 if(cp != NULL && *cp != '\0'){
1502 if(putesc(cp, _coll_fp) < 0) /* TODO v15: user upon `set' time */
1503 goto jerr;
1504 if((options & OPT_INTERACTIVE) && putesc(cp, stdout) < 0)
1505 goto jerr;
1507 break;
1508 case 'w':
1509 /* Write the message on a file */
1510 if(cnt == 2)
1511 goto jearg;
1512 if((cp = fexpand(&linebuf[3], FEXP_LOCAL | FEXP_NOPROTO)) == NULL){
1513 n_err(_("Write what file!?\n"));
1514 break;
1516 rewind(_coll_fp);
1517 if(exwrite(cp, _coll_fp, 1) < 0)
1518 goto jerr;
1519 break;
1520 case 'm':
1521 case 'M':
1522 case 'f':
1523 case 'F':
1524 case 'u':
1525 case 'U':
1526 /* Interpolate the named messages, if we are in receiving mail mode.
1527 * Does the standard list processing garbage. If ~f is given, we
1528 * don't shift over */
1529 if(cnt == 2)
1530 goto jearg;
1531 if(forward(&linebuf[3], _coll_fp, c) < 0)
1532 break;
1533 break;
1534 case 'p':
1535 /* Print current state of the message without altering anything */
1536 if(cnt != 2)
1537 goto jearg;
1538 print_collf(_coll_fp, hp, ((options & OPT_INTERACTIVE) != 0));
1539 break;
1540 case '|':
1541 /* Pipe message through command. Collect output as new message */
1542 if(cnt == 2)
1543 goto jearg;
1544 rewind(_coll_fp);
1545 mespipe(&linebuf[3]);
1546 goto jhistcont;
1547 case 'v':
1548 case 'e':
1549 /* Edit the current message. 'e' -> use EDITOR, 'v' -> use VISUAL */
1550 if(cnt != 2 || coap != NULL)
1551 goto jearg;
1552 rewind(_coll_fp);
1553 mesedit(c, ok_blook(editheaders) ? hp : NULL);
1554 goto jhistcont;
1555 case '^':
1556 if(!a_collect_plumbing(&linebuf[3], hp))
1557 goto jearg;
1558 if(options & OPT_INTERACTIVE)
1559 break;
1560 continue;
1561 case '?':
1562 /* Last the lengthy help string. (Very ugly, but take care for
1563 * compiler supported string lengths :() */
1564 puts(_(
1565 "COMMAND ESCAPES (to be placed after a newline) excerpt:\n"
1566 "~. Commit and send message\n"
1567 "~: <command> Execute a mail command\n"
1568 "~<! <command> Insert output of command\n"
1569 "~@ [<files>] Edit attachment list\n"
1570 "~A Insert *Sign* variable (`~a': insert *sign*)\n"
1571 "~c <users> Add users to Cc: list (`~b': to Bcc:)\n"
1572 "~d Read in $DEAD (dead.letter)\n"
1573 "~e Edit message via $EDITOR"
1575 puts(_(
1576 "~F <msglist> Read in with headers, don't *indentprefix* lines\n"
1577 "~f <msglist> Like ~F, but honour `ignore' / `retain' configuration\n"
1578 "~H Edit From:, Reply-To: and Sender:\n"
1579 "~h Prompt for Subject:, To:, Cc: and \"blind\" Bcc:\n"
1580 "~i <variable> Insert a value and a newline\n"
1581 "~M <msglist> Read in with headers, *indentprefix* (`~m': `retain' etc.)\n"
1582 "~p Show current message compose buffer\n"
1583 "~r <file> Read in a file (`~R': *indentprefix* lines)"
1585 puts(_(
1586 "~s <subject> Set Subject:\n"
1587 "~t <users> Add users to To: list\n"
1588 "~u <msglist> Read in message(s) without headers (`~U': indent lines)\n"
1589 "~v Edit message via $VISUAL\n"
1590 "~w <file> Write message onto file\n"
1591 "~x Abort composition, discard message (`~q': save in $DEAD)\n"
1592 "~| <command> Pipe message through shell filter"
1594 if(cnt != 2)
1595 goto jearg;
1596 break;
1599 /* Finally place an entry in history as applicable */
1600 if(0){
1601 jhistcont:
1602 c = '\1';
1603 }else
1604 c = '\0';
1605 if(options & OPT_INTERACTIVE)
1606 n_tty_addhist(linebuf, TRU1);
1607 if(c != '\0')
1608 goto jcont;
1611 jout:
1612 /* Do we have *on-compose-done-shell*, or *on-compose-done*?
1613 * TODO Usual f...ed up state of signals and terminal etc. */
1614 if(coap == NULL && (cp = ok_vlook(on_compose_done_shell)) != NULL) Jocds:{
1615 union {int (*ptf)(void); char const *sh;} u;
1616 char const *cmd;
1618 /* Reset *escape* to be available and guaranteed! */
1619 escape = n_ESCAPE;
1621 if(coapm != NULL){
1622 u.ptf = &a_coll_ocds__mac;
1623 cmd = (char*)-1;
1624 a_coll_ocds__macname = cp = coapm;
1625 }else{
1626 u.sh = ok_vlook(SHELL);
1627 cmd = cp;
1630 i = strlen(cp) +1;
1631 coap = n_lofi_alloc(n_VSTRUCT_SIZEOF(struct a_coll_ocds_arg, coa_cmd
1632 ) + i);
1633 coap->coa_pipe[0] = coap->coa_pipe[1] = -1;
1634 coap->coa_stdin = coap->coa_stdout = NULL;
1635 coap->coa_senderr = checkaddr_err;
1636 memcpy(coap->coa_cmd, cp, i);
1638 hold_all_sigs();
1639 coap->coa_opipe = safe_signal(SIGPIPE, SIG_IGN);
1640 coap->coa_oint = safe_signal(SIGINT, SIG_IGN);
1641 rele_all_sigs();
1643 if(pipe_cloexec(coap->coa_pipe) != -1 &&
1644 (coap->coa_stdin = Fdopen(coap->coa_pipe[0], "r", FAL0)) != NULL &&
1645 (coap->coa_stdout = Popen(cmd, "W", u.sh, NULL, coap->coa_pipe[1])
1646 ) != NULL){
1647 close(coap->coa_pipe[1]);
1648 coap->coa_pipe[1] = -1;
1650 temporary_call_compose_mode_hook(NULL, NULL, NULL);
1651 n_source_slice_hack(coap->coa_cmd, coap->coa_stdin, coap->coa_stdout,
1652 (options & ~OPT_INTERACTIVE), &a_coll_ocds__finalize, &coap);
1653 /* Hook version protocol for ~^: update manual upon change! */
1654 fputs(a_COLL_PLUMBING_VERSION "\n", coap->coa_stdout);
1655 #undef a_COLL_PLUMBING_VERSION
1656 goto jcont;
1659 c = errno;
1660 a_coll_ocds__finalize(coap);
1661 n_perr(_("Cannot invoke *on-compose-done(-shell)?*"), c);
1662 goto jerr;
1664 if(*checkaddr_err != 0){
1665 *checkaddr_err = 0;
1666 goto jerr;
1668 if(coapm == NULL && (coapm = ok_vlook(on_compose_done)) != NULL)
1669 goto Jocds;
1670 escape = escape_saved;
1672 /* Final chance to edit headers, if not already above */
1673 if (ok_blook(bsdcompat) || ok_blook(askatend)) {
1674 if (hp->h_cc == NULL && ok_blook(askcc))
1675 grab_headers(n_LEXINPUT_CTX_COMPOSE, hp, GCC, 1);
1676 if (hp->h_bcc == NULL && ok_blook(askbcc))
1677 grab_headers(n_LEXINPUT_CTX_COMPOSE, hp, GBCC, 1);
1679 if (hp->h_attach == NULL && ok_blook(askattach))
1680 edit_attachments(n_LEXINPUT_CTX_COMPOSE, &hp->h_attach);
1682 /* Add automatic receivers */
1683 if ((cp = ok_vlook(autocc)) != NULL && *cp != '\0')
1684 hp->h_cc = cat(hp->h_cc, checkaddrs(lextract(cp, GCC | GFULL),
1685 EACM_NORMAL, checkaddr_err));
1686 if ((cp = ok_vlook(autobcc)) != NULL && *cp != '\0')
1687 hp->h_bcc = cat(hp->h_bcc, checkaddrs(lextract(cp, GBCC | GFULL),
1688 EACM_NORMAL, checkaddr_err));
1689 if (*checkaddr_err != 0)
1690 goto jerr;
1692 /* Execute compose-leave */
1693 if((cp = ok_vlook(on_compose_leave)) != NULL){
1694 setup_from_and_sender(hp);
1695 temporary_call_compose_mode_hook(cp, &a_coll__hook_setter, hp);
1698 /* TODO Cannot do since it may require turning this into a multipart one */
1699 if(options & OPT_Mm_FLAG)
1700 goto jskiptails;
1702 /* Place signature? */
1703 if((cp = ok_vlook(signature)) != NULL && *cp != '\0'){
1704 char const *cpq;
1706 if((cpq = fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO)) == NULL){
1707 n_err(_("*signature* expands to invalid file: %s\n"),
1708 n_shexp_quote_cp(cp, FAL0));
1709 goto jerr;
1711 cpq = n_shexp_quote_cp(cp = cpq, FAL0);
1713 if((sigfp = Fopen(cp, "r")) == NULL){
1714 n_err(_("Can't open *signature* %s: %s\n"), cpq, strerror(errno));
1715 goto jerr;
1718 if(linebuf == NULL)
1719 linebuf = smalloc(linesize = LINESIZE);
1720 c = '\0';
1721 while((i = fread(linebuf, sizeof *linebuf, linesize, n_UNVOLATILE(sigfp)))
1722 > 0){
1723 c = linebuf[i - 1];
1724 if(i != fwrite(linebuf, sizeof *linebuf, i, _coll_fp))
1725 goto jerr;
1728 /* C99 */{
1729 FILE *x = n_UNVOLATILE(sigfp);
1730 int e = errno, ise = ferror(x);
1732 sigfp = NULL;
1733 Fclose(x);
1735 if(ise){
1736 n_err(_("Errors while reading *signature* %s: %s\n"),
1737 cpq, strerror(e));
1738 goto jerr;
1742 if(c != '\0' && c != '\n')
1743 putc('\n', _coll_fp);
1746 { char const *cp_obsolete = ok_vlook(NAIL_TAIL);
1748 if(cp_obsolete != NULL)
1749 OBSOLETE(_("please use *message-inject-tail* instead of *NAIL_TAIL*"));
1751 if((cp = ok_vlook(message_inject_tail)) != NULL ||
1752 (cp = cp_obsolete) != NULL){
1753 if(putesc(cp, _coll_fp) < 0)
1754 goto jerr;
1755 if((options & OPT_INTERACTIVE) && putesc(cp, stdout) < 0)
1756 goto jerr;
1760 jskiptails:
1761 if(fflush(_coll_fp))
1762 goto jerr;
1763 rewind(_coll_fp);
1765 jleave:
1766 temporary_unroll_compose_mode();
1767 if (linebuf != NULL)
1768 free(linebuf);
1769 sigfillset(&nset);
1770 sigprocmask(SIG_BLOCK, &nset, NULL);
1771 pstate &= ~PS_COMPOSE_MODE;
1772 safe_signal(SIGINT, _coll_saveint);
1773 safe_signal(SIGHUP, _coll_savehup);
1774 sigprocmask(SIG_SETMASK, &oset, NULL);
1775 NYD_LEAVE;
1776 return _coll_fp;
1778 jerr:
1779 if(coap != NULL && coap != (struct a_coll_ocds_arg*)-1)
1780 n_source_slice_hack_remove_after_jump();
1781 if(sigfp != NULL)
1782 Fclose(n_UNVOLATILE(sigfp));
1783 if (_coll_fp != NULL) {
1784 Fclose(_coll_fp);
1785 _coll_fp = NULL;
1787 assert(checkaddr_err != NULL);
1788 /* TODO We don't save in $DEAD upon error because msg not readily composed?
1789 * TODO But this no good, it should go ZOMBIE / DRAFT / POSTPONED or what! */
1790 if(*checkaddr_err != 0)
1791 n_err(_("Some addressees were classified as \"hard error\"\n"));
1792 else if(_coll_hadintr == 0){
1793 *checkaddr_err = TRU1; /* TODO ugly: "sendout_error" now.. */
1794 n_err(_("Failed to prepare composed message (I/O error, disk full?)\n"));
1796 goto jleave;
1799 /* s-it-mode */