collect.c revision 1.31 1 /* $NetBSD: collect.c,v 1.31 2005/07/19 01:38:38 christos Exp $ */
2
3 /*
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)collect.c 8.2 (Berkeley) 4/19/94";
36 #else
37 __RCSID("$NetBSD: collect.c,v 1.31 2005/07/19 01:38:38 christos Exp $");
38 #endif
39 #endif /* not lint */
40
41 /*
42 * Mail -- a mail program
43 *
44 * Collect input from standard input, handling
45 * ~ escapes.
46 */
47
48 #include "rcv.h"
49 #include "extern.h"
50
51 extern char *tmpdir;
52
53 /*
54 * Read a message from standard input and return a read file to it
55 * or NULL on error.
56 */
57
58 /*
59 * The following hokiness with global variables is so that on
60 * receipt of an interrupt signal, the partial message can be salted
61 * away on dead.letter.
62 */
63
64 static sig_t saveint; /* Previous SIGINT value */
65 static sig_t savehup; /* Previous SIGHUP value */
66 static sig_t savetstp; /* Previous SIGTSTP value */
67 static sig_t savettou; /* Previous SIGTTOU value */
68 static sig_t savettin; /* Previous SIGTTIN value */
69 static FILE *collf; /* File for saving away */
70 static int hadintr; /* Have seen one SIGINT so far */
71
72 static jmp_buf colljmp; /* To get back to work */
73 static int colljmp_p; /* whether to long jump */
74 static jmp_buf collabort; /* To end collection with error */
75
76 FILE *
77 collect(struct header *hp, int printheaders)
78 {
79 FILE *fbuf;
80 int lc, cc, escape, eofcount;
81 int c, fd, t;
82 char linebuf[LINESIZE];
83 const char *cp;
84 char getsub;
85 char tempname[PATHSIZE];
86 char mailtempname[PATHSIZE];
87 sigset_t nset;
88 int longline, lastlong, rc; /* So we don't make 2 or more lines
89 out of a long input line. */
90 #if __GNUC__
91 /* Avoid longjmp clobbering */
92 (void)&escape;
93 (void)&eofcount;
94 (void)&getsub;
95 (void)&longline;
96 #endif
97
98 memset(mailtempname, 0, sizeof(mailtempname));
99 collf = NULL;
100 /*
101 * Start catching signals from here, but we're still die on interrupts
102 * until we're in the main loop.
103 */
104 sigemptyset(&nset);
105 sigaddset(&nset, SIGINT);
106 sigaddset(&nset, SIGHUP);
107 sigprocmask(SIG_BLOCK, &nset, NULL);
108 if ((saveint = signal(SIGINT, SIG_IGN)) != SIG_IGN)
109 signal(SIGINT, collint);
110 if ((savehup = signal(SIGHUP, SIG_IGN)) != SIG_IGN)
111 signal(SIGHUP, collhup);
112 savetstp = signal(SIGTSTP, collstop);
113 savettou = signal(SIGTTOU, collstop);
114 savettin = signal(SIGTTIN, collstop);
115 if (setjmp(collabort) || setjmp(colljmp)) {
116 (void)rm(mailtempname);
117 goto err;
118 }
119 sigprocmask(SIG_UNBLOCK, &nset, NULL);
120
121 noreset++;
122 (void)snprintf(mailtempname, sizeof(mailtempname),
123 "%s/mail.RsXXXXXXXXXX", tmpdir);
124 if ((fd = mkstemp(mailtempname)) == -1 ||
125 (collf = Fdopen(fd, "w+")) == NULL) {
126 if (fd != -1)
127 close(fd);
128 warn("%s", mailtempname);
129 goto err;
130 }
131 (void)rm(mailtempname);
132
133 /*
134 * If we are going to prompt for a subject,
135 * refrain from printing a newline after
136 * the headers (since some people mind).
137 */
138 t = GTO|GSUBJECT|GCC|GNL;
139 getsub = 0;
140 if (hp->h_subject == NULL && value("interactive") != NULL &&
141 (value("ask") != NULL || value("asksub") != NULL))
142 t &= ~GNL, getsub++;
143 if (printheaders) {
144 puthead(hp, stdout, t);
145 fflush(stdout);
146 }
147 if ((cp = value("escape")) != NULL)
148 escape = *cp;
149 else
150 escape = ESCAPE;
151 eofcount = 0;
152 hadintr = 0;
153 lastlong = 0;
154 longline = 0;
155
156 if (!setjmp(colljmp)) {
157 if (getsub)
158 grabh(hp, GSUBJECT);
159 } else {
160 /*
161 * Come here for printing the after-signal message.
162 * Duplicate messages won't be printed because
163 * the write is aborted if we get a SIGTTOU.
164 */
165 cont:
166 if (hadintr) {
167 fflush(stdout);
168 fprintf(stderr,
169 "\n(Interrupt -- one more to kill letter)\n");
170 } else {
171 printf("(continue)\n");
172 fflush(stdout);
173 }
174 }
175 for (;;) {
176 colljmp_p = 1;
177 c = readline(stdin, linebuf, LINESIZE);
178 colljmp_p = 0;
179 if (c < 0) {
180 if (value("interactive") != NULL &&
181 value("ignoreeof") != NULL && ++eofcount < 25) {
182 printf("Use \".\" to terminate letter\n");
183 continue;
184 }
185 break;
186 }
187 lastlong = longline;
188 longline = c == LINESIZE-1;
189 eofcount = 0;
190 hadintr = 0;
191 if (linebuf[0] == '.' && linebuf[1] == '\0' &&
192 value("interactive") != NULL && !lastlong &&
193 (value("dot") != NULL || value("ignoreeof") != NULL))
194 break;
195 if (linebuf[0] != escape || value("interactive") == NULL ||
196 lastlong) {
197 if (putline(collf, linebuf, !longline) < 0)
198 goto err;
199 continue;
200 }
201 c = linebuf[1];
202 switch (c) {
203 default:
204 /*
205 * On double escape, just send the single one.
206 * Otherwise, it's an error.
207 */
208 if (c == escape) {
209 if (putline(collf, &linebuf[1], !longline) < 0)
210 goto err;
211 else
212 break;
213 }
214 printf("Unknown tilde escape.\n");
215 break;
216 case 'C':
217 /*
218 * Dump core.
219 */
220 core(NULL);
221 break;
222 case '!':
223 /*
224 * Shell escape, send the balance of the
225 * line to sh -c.
226 */
227 shell(&linebuf[2]);
228 break;
229 case ':':
230 case '_':
231 /*
232 * Escape to command mode, but be nice!
233 */
234 execute(&linebuf[2], 1);
235 goto cont;
236 case '.':
237 /*
238 * Simulate end of file on input.
239 */
240 goto out;
241 case 'q':
242 /*
243 * Force a quit of sending mail.
244 * Act like an interrupt happened.
245 */
246 hadintr++;
247 collint(SIGINT);
248 exit(1);
249
250 case 'x': /* exit, do not save in dead.letter */
251 goto err;
252
253 case 'h':
254 /*
255 * Grab a bunch of headers.
256 */
257 grabh(hp, GTO|GSUBJECT|GCC|GBCC);
258 goto cont;
259 case 't':
260 /*
261 * Add to the To list.
262 */
263 hp->h_to = cat(hp->h_to, extract(&linebuf[2], GTO));
264 break;
265 case 's':
266 /*
267 * Set the Subject list.
268 */
269 cp = &linebuf[2];
270 while (isspace((unsigned char)*cp))
271 cp++;
272 hp->h_subject = savestr(cp);
273 break;
274 case 'c':
275 /*
276 * Add to the CC list.
277 */
278 hp->h_cc = cat(hp->h_cc, extract(&linebuf[2], GCC));
279 break;
280 case 'b':
281 /*
282 * Add stuff to blind carbon copies list.
283 */
284 hp->h_bcc = cat(hp->h_bcc, extract(&linebuf[2], GBCC));
285 break;
286 case 'i':
287 case 'A':
288 case 'a':
289 /*
290 * Insert named variable in message
291 */
292
293 switch(c) {
294 case 'i':
295 cp = &linebuf[2];
296 while(isspace((unsigned char) *cp))
297 cp++;
298
299 break;
300 case 'a':
301 cp = "sign";
302 break;
303 case 'A':
304 cp = "Sign";
305 break;
306 default:
307 goto err;
308 }
309
310 if(*cp && (cp = value(cp)) != NULL) {
311 printf("%s\n", cp);
312 if(putline(collf, cp, 1) < 0)
313 goto err;
314 }
315
316 break;
317
318 case 'd':
319 strcpy(linebuf + 2, getdeadletter());
320 /* fall into . . . */
321 case 'r':
322 case '<':
323 /*
324 * Invoke a file:
325 * Search for the file name,
326 * then open it and copy the contents to collf.
327 */
328 cp = &linebuf[2];
329 while (isspace((unsigned char)*cp))
330 cp++;
331 if (*cp == '\0') {
332 printf("Interpolate what file?\n");
333 break;
334 }
335
336 cp = expand(cp);
337 if (cp == NULL)
338 break;
339
340 if (*cp == '!') { /* insert stdout of command */
341 const char *shellcmd;
342 int nullfd;
343 int rc2;
344
345 if((nullfd = open("/dev/null", O_RDONLY, 0)) == -1) {
346 warn("/dev/null");
347 break;
348 }
349
350 (void)snprintf(tempname, sizeof(tempname),
351 "%s/mail.ReXXXXXXXXXX", tmpdir);
352 if ((fd = mkstemp(tempname)) == -1 ||
353 (fbuf = Fdopen(fd, "w+")) == NULL) {
354 if (fd != -1)
355 close(fd);
356 warn("%s", tempname);
357 break;
358 }
359 (void)unlink(tempname);
360
361 if ((shellcmd = value("SHELL")) == NULL)
362 shellcmd = _PATH_CSHELL;
363
364 rc2 = run_command(shellcmd, 0, nullfd, fileno(fbuf), "-c", cp+1, NULL);
365
366 close(nullfd);
367
368 if (rc2 < 0) {
369 (void)Fclose(fbuf);
370 break;
371 }
372
373 if (fsize(fbuf) == 0) {
374 fprintf(stderr, "No bytes from command \"%s\"\n", cp+1);
375 (void)Fclose(fbuf);
376 break;
377 }
378
379 rewind(fbuf);
380 }
381 else if (isdir(cp)) {
382 printf("%s: Directory\n", cp);
383 break;
384 }
385 else if ((fbuf = Fopen(cp, "r")) == NULL) {
386 warn("%s", cp);
387 break;
388 }
389 printf("\"%s\" ", cp);
390 fflush(stdout);
391 lc = 0;
392 cc = 0;
393 while ((rc = readline(fbuf, linebuf, LINESIZE)) >= 0) {
394 if (rc != LINESIZE-1) lc++;
395 if ((t = putline(collf, linebuf,
396 rc != LINESIZE-1)) < 0) {
397 Fclose(fbuf);
398 goto err;
399 }
400 cc += t;
401 }
402 Fclose(fbuf);
403 printf("%d/%d\n", lc, cc);
404 break;
405 case 'w':
406 /*
407 * Write the message on a file.
408 */
409 cp = &linebuf[2];
410 while (*cp == ' ' || *cp == '\t')
411 cp++;
412 if (*cp == '\0') {
413 fprintf(stderr, "Write what file!?\n");
414 break;
415 }
416 if ((cp = expand(cp)) == NULL)
417 break;
418 rewind(collf);
419 exwrite(cp, collf, 1);
420 break;
421 case 'm':
422 case 'M':
423 case 'f':
424 case 'F':
425 /*
426 * Interpolate the named messages, if we
427 * are in receiving mail mode. Does the
428 * standard list processing garbage.
429 * If ~f is given, we don't shift over.
430 */
431 if (forward(linebuf + 2, collf, mailtempname, c) < 0)
432 goto err;
433 goto cont;
434 case '?':
435 if ((fbuf = Fopen(_PATH_TILDE, "r")) == NULL) {
436 warn(_PATH_TILDE);
437 break;
438 }
439 while ((t = getc(fbuf)) != EOF)
440 (void)putchar(t);
441 Fclose(fbuf);
442 break;
443 case 'p':
444 /*
445 * Print out the current state of the
446 * message without altering anything.
447 */
448 rewind(collf);
449 printf("-------\nMessage contains:\n");
450 puthead(hp, stdout, GTO|GSUBJECT|GCC|GBCC|GNL);
451 while ((t = getc(collf)) != EOF)
452 (void)putchar(t);
453 goto cont;
454 case '|':
455 /*
456 * Pipe message through command.
457 * Collect output as new message.
458 */
459 rewind(collf);
460 mespipe(collf, &linebuf[2]);
461 goto cont;
462 case 'v':
463 case 'e':
464 /*
465 * Edit the current message.
466 * 'e' means to use EDITOR
467 * 'v' means to use VISUAL
468 */
469 rewind(collf);
470 mesedit(collf, c);
471 goto cont;
472 }
473 }
474 goto out;
475 err:
476 if (collf != NULL) {
477 Fclose(collf);
478 collf = NULL;
479 }
480 out:
481 if (collf != NULL)
482 rewind(collf);
483 noreset--;
484 sigprocmask(SIG_BLOCK, &nset, NULL);
485 signal(SIGINT, saveint);
486 signal(SIGHUP, savehup);
487 signal(SIGTSTP, savetstp);
488 signal(SIGTTOU, savettou);
489 signal(SIGTTIN, savettin);
490 sigprocmask(SIG_UNBLOCK, &nset, NULL);
491 return collf;
492 }
493
494 /*
495 * Write a file, ex-like if f set.
496 */
497 int
498 exwrite(const char name[], FILE *fp, int f)
499 {
500 FILE *of;
501 int c;
502 long cc;
503 int lc;
504 struct stat junk;
505
506 if (f) {
507 printf("\"%s\" ", name);
508 fflush(stdout);
509 }
510 if (stat(name, &junk) >= 0 && S_ISREG(junk.st_mode)) {
511 if (!f)
512 fprintf(stderr, "%s: ", name);
513 fprintf(stderr, "File exists\n");
514 return(-1);
515 }
516 if ((of = Fopen(name, "w")) == NULL) {
517 warn("%s", name);
518 return(-1);
519 }
520 lc = 0;
521 cc = 0;
522 while ((c = getc(fp)) != EOF) {
523 cc++;
524 if (c == '\n')
525 lc++;
526 (void)putc(c, of);
527 if (ferror(of)) {
528 warn("%s", name);
529 Fclose(of);
530 return(-1);
531 }
532 }
533 Fclose(of);
534 printf("%d/%ld\n", lc, cc);
535 fflush(stdout);
536 return(0);
537 }
538
539 /*
540 * Edit the message being collected on fp.
541 * On return, make the edit file the new temp file.
542 */
543 void
544 mesedit(FILE *fp, int c)
545 {
546 sig_t sigint = signal(SIGINT, SIG_IGN);
547 FILE *nf = run_editor(fp, (off_t)-1, c, 0);
548
549 if (nf != NULL) {
550 fseek(nf, 0L, 2);
551 collf = nf;
552 Fclose(fp);
553 }
554 (void)signal(SIGINT, sigint);
555 }
556
557 /*
558 * Pipe the message through the command.
559 * Old message is on stdin of command;
560 * New message collected from stdout.
561 * Sh -c must return 0 to accept the new message.
562 */
563 void
564 mespipe(FILE *fp, char cmd[])
565 {
566 FILE *nf;
567 sig_t sigint = signal(SIGINT, SIG_IGN);
568 const char *shellcmd;
569 int fd;
570 char tempname[PATHSIZE];
571
572 (void)snprintf(tempname, sizeof(tempname),
573 "%s/mail.ReXXXXXXXXXX", tmpdir);
574 if ((fd = mkstemp(tempname)) == -1 ||
575 (nf = Fdopen(fd, "w+")) == NULL) {
576 if (fd != -1)
577 close(fd);
578 warn("%s", tempname);
579 goto out;
580 }
581 (void)unlink(tempname);
582 /*
583 * stdin = current message.
584 * stdout = new message.
585 */
586 if ((shellcmd = value("SHELL")) == NULL)
587 shellcmd = _PATH_CSHELL;
588 if (run_command(shellcmd,
589 0, fileno(fp), fileno(nf), "-c", cmd, NULL) < 0) {
590 (void)Fclose(nf);
591 goto out;
592 }
593 if (fsize(nf) == 0) {
594 fprintf(stderr, "No bytes from \"%s\" !?\n", cmd);
595 (void)Fclose(nf);
596 goto out;
597 }
598 /*
599 * Take new files.
600 */
601 (void)fseek(nf, 0L, 2);
602 collf = nf;
603 (void)Fclose(fp);
604 out:
605 (void)signal(SIGINT, sigint);
606 }
607
608 /*
609 * Interpolate the named messages into the current
610 * message, preceding each line with a tab.
611 * Return a count of the number of characters now in
612 * the message, or -1 if an error is encountered writing
613 * the message temporary. The flag argument is 'm' if we
614 * should shift over and 'f' if not.
615 */
616 int
617 forward(char ms[], FILE *fp, char *fn, int f)
618 {
619 int *msgvec;
620 struct ignoretab *ig;
621 const char *tabst;
622
623 msgvec = salloc((msgCount+1) * sizeof *msgvec);
624 if (msgvec == NULL)
625 return(0);
626 if (getmsglist(ms, msgvec, 0) < 0)
627 return(0);
628 if (*msgvec == 0) {
629 *msgvec = first(0, MMNORM);
630 if (*msgvec == 0) {
631 printf("No appropriate messages\n");
632 return(0);
633 }
634 msgvec[1] = 0;
635 }
636 if (f == 'f' || f == 'F')
637 tabst = NULL;
638 else if ((tabst = value("indentprefix")) == NULL)
639 tabst = "\t";
640 ig = isupper(f) ? NULL : ignore;
641 printf("Interpolating:");
642 for (; *msgvec != 0; msgvec++) {
643 struct message *mp = message + *msgvec - 1;
644
645 touch(mp);
646 printf(" %d", *msgvec);
647 if (sendmessage(mp, fp, ig, tabst) < 0) {
648 warn("%s", fn);
649 return(-1);
650 }
651 }
652 printf("\n");
653 return(0);
654 }
655
656 /*
657 * Print (continue) when continued after ^Z.
658 */
659 /*ARGSUSED*/
660 void
661 collstop(int s)
662 {
663 sig_t old_action = signal(s, SIG_DFL);
664 sigset_t nset;
665
666 sigemptyset(&nset);
667 sigaddset(&nset, s);
668 sigprocmask(SIG_UNBLOCK, &nset, NULL);
669 kill(0, s);
670 sigprocmask(SIG_BLOCK, &nset, NULL);
671 signal(s, old_action);
672 if (colljmp_p) {
673 colljmp_p = 0;
674 hadintr = 0;
675 longjmp(colljmp, 1);
676 }
677 }
678
679 /*
680 * On interrupt, come here to save the partial message in ~/dead.letter.
681 * Then jump out of the collection loop.
682 */
683 /*ARGSUSED*/
684 void
685 collint(int s)
686 {
687 /*
688 * the control flow is subtle, because we can be called from ~q.
689 */
690 if (!hadintr) {
691 if (value("ignore") != NULL) {
692 puts("@");
693 fflush(stdout);
694 clearerr(stdin);
695 return;
696 }
697 hadintr = 1;
698 longjmp(colljmp, 1);
699 }
700 rewind(collf);
701 if (value("nosave") == NULL)
702 savedeadletter(collf);
703 longjmp(collabort, 1);
704 }
705
706 /*ARGSUSED*/
707 void
708 collhup(int s)
709 {
710 rewind(collf);
711 savedeadletter(collf);
712 /*
713 * Let's pretend nobody else wants to clean up,
714 * a true statement at this time.
715 */
716 exit(1);
717 }
718
719 void
720 savedeadletter(FILE *fp)
721 {
722 FILE *dbuf;
723 int c;
724 const char *cp;
725
726 if (fsize(fp) == 0)
727 return;
728 cp = getdeadletter();
729 c = umask(077);
730 dbuf = Fopen(cp, "a");
731 (void)umask(c);
732 if (dbuf == NULL)
733 return;
734 while ((c = getc(fp)) != EOF)
735 (void)putc(c, dbuf);
736 Fclose(dbuf);
737 rewind(fp);
738 }
739