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