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