collect.c revision 29574
1130462Sdfr/*
2130462Sdfr * Copyright (c) 1980, 1993
3130462Sdfr *	The Regents of the University of California.  All rights reserved.
4130462Sdfr *
5130462Sdfr * Redistribution and use in source and binary forms, with or without
6130462Sdfr * modification, are permitted provided that the following conditions
7130462Sdfr * are met:
8130462Sdfr * 1. Redistributions of source code must retain the above copyright
9130462Sdfr *    notice, this list of conditions and the following disclaimer.
10130462Sdfr * 2. Redistributions in binary form must reproduce the above copyright
11130462Sdfr *    notice, this list of conditions and the following disclaimer in the
12130462Sdfr *    documentation and/or other materials provided with the distribution.
13130462Sdfr * 3. All advertising materials mentioning features or use of this software
14130462Sdfr *    must display the following acknowledgement:
15130462Sdfr *	This product includes software developed by the University of
16130462Sdfr *	California, Berkeley and its contributors.
17130462Sdfr * 4. Neither the name of the University nor the names of its contributors
18130462Sdfr *    may be used to endorse or promote products derived from this software
19130462Sdfr *    without specific prior written permission.
20130462Sdfr *
21130462Sdfr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22130462Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23130462Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24130462Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25130462Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26130462Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27130462Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28148145Strhodes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29130462Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30130462Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31130462Sdfr * SUCH DAMAGE.
32130462Sdfr */
33148145Strhodes
34130462Sdfr#ifndef lint
35151046Strhodesstatic char sccsid[] = "@(#)collect.c	8.2 (Berkeley) 4/19/94";
36151046Strhodes#endif /* not lint */
37148220Strhodes
38148145Strhodes/*
39138862Sbrueffer * Mail -- a mail program
40130462Sdfr *
41148145Strhodes * Collect input from standard input, handling
42148145Strhodes * ~ escapes.
43151046Strhodes */
44152569Sru
45148145Strhodes#include "rcv.h"
46148145Strhodes#include "extern.h"
47148145Strhodes
48148145Strhodes/*
49148145Strhodes * Read a message from standard output and return a read file to it
50130462Sdfr * or NULL on error.
51130462Sdfr */
52130462Sdfr
53130462Sdfr/*
54130462Sdfr * The following hokiness with global variables is so that on
55130462Sdfr * receipt of an interrupt signal, the partial message can be salted
56130467Sdfr * away on dead.letter.
57130462Sdfr */
58130462Sdfr
59130462Sdfrstatic	sig_t	saveint;		/* Previous SIGINT value */
60130467Sdfrstatic	sig_t	savehup;		/* Previous SIGHUP value */
61130462Sdfrstatic	sig_t	savetstp;		/* Previous SIGTSTP value */
62130462Sdfrstatic	sig_t	savettou;		/* Previous SIGTTOU value */
63130462Sdfrstatic	sig_t	savettin;		/* Previous SIGTTIN value */
64130462Sdfrstatic	FILE	*collf;			/* File for saving away */
65130462Sdfrstatic	int	hadintr;		/* Have seen one SIGINT so far */
66130462Sdfr
67130462Sdfrstatic	jmp_buf	colljmp;		/* To get back to work */
68130462Sdfrstatic	int	colljmp_p;		/* whether to long jump */
69130462Sdfrstatic	jmp_buf	collabort;		/* To end collection with error */
70130462Sdfr
71138862SbruefferFILE *
72130462Sdfrcollect(hp, printheaders)
73130462Sdfr	struct header *hp;
74130462Sdfr	int printheaders;
75130462Sdfr{
76130462Sdfr	FILE *fbuf;
77130462Sdfr	int lc, cc, escape, eofcount;
78140561Sru	register int c, t;
79140561Sru	char linebuf[LINESIZE], *cp;
80140561Sru	extern char tempMail[];
81140561Sru	char getsub;
82140561Sru	int omask;
83130462Sdfr	void collint(), collhup(), collstop();
84130465Sdfr
85130462Sdfr	collf = NULL;
86130462Sdfr	/*
87130462Sdfr	 * Start catching signals from here, but we're still die on interrupts
88130462Sdfr	 * until we're in the main loop.
89130462Sdfr	 */
90130462Sdfr	omask = sigblock(sigmask(SIGINT) | sigmask(SIGHUP));
91140561Sru	if ((saveint = signal(SIGINT, SIG_IGN)) != SIG_IGN)
92140561Sru		signal(SIGINT, collint);
93140561Sru	if ((savehup = signal(SIGHUP, SIG_IGN)) != SIG_IGN)
94140561Sru		signal(SIGHUP, collhup);
95140561Sru	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		 */
141cont:
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 list.
236			 */
237			cp = &linebuf[2];
238			while (isspace(*cp))
239				cp++;
240			hp->h_subject = savestr(cp);
241			break;
242		case 'c':
243			/*
244			 * Add to the CC list.
245			 */
246			hp->h_cc = cat(hp->h_cc, extract(&linebuf[2], GCC));
247			break;
248		case 'b':
249			/*
250			 * Add stuff to blind carbon copies list.
251			 */
252			hp->h_bcc = cat(hp->h_bcc, extract(&linebuf[2], GBCC));
253			break;
254		case 'd':
255			strcpy(linebuf + 2, getdeadletter());
256			/* fall into . . . */
257		case 'r':
258			/*
259			 * Invoke a file:
260			 * Search for the file name,
261			 * then open it and copy the contents to collf.
262			 */
263			cp = &linebuf[2];
264			while (isspace(*cp))
265				cp++;
266			if (*cp == '\0') {
267				printf("Interpolate what file?\n");
268				break;
269			}
270			cp = expand(cp);
271			if (cp == NOSTR)
272				break;
273			if (isdir(cp)) {
274				printf("%s: Directory\n", cp);
275				break;
276			}
277			if ((fbuf = Fopen(cp, "r")) == NULL) {
278				perror(cp);
279				break;
280			}
281			printf("\"%s\" ", cp);
282			fflush(stdout);
283			lc = 0;
284			cc = 0;
285			while (readline(fbuf, linebuf, LINESIZE) >= 0) {
286				lc++;
287				if ((t = putline(collf, linebuf)) < 0) {
288					Fclose(fbuf);
289					goto err;
290				}
291				cc += t;
292			}
293			Fclose(fbuf);
294			printf("%d/%d\n", lc, cc);
295			break;
296		case 'w':
297			/*
298			 * Write the message on a file.
299			 */
300			cp = &linebuf[2];
301			while (*cp == ' ' || *cp == '\t')
302				cp++;
303			if (*cp == '\0') {
304				fprintf(stderr, "Write what file!?\n");
305				break;
306			}
307			if ((cp = expand(cp)) == NOSTR)
308				break;
309			rewind(collf);
310			exwrite(cp, collf, 1);
311			break;
312		case 'm':
313		case 'M':
314		case 'f':
315		case 'F':
316			/*
317			 * Interpolate the named messages, if we
318			 * are in receiving mail mode.  Does the
319			 * standard list processing garbage.
320			 * If ~f is given, we don't shift over.
321			 */
322			if (forward(linebuf + 2, collf, c) < 0)
323				goto err;
324			goto cont;
325		case '?':
326			if ((fbuf = Fopen(_PATH_TILDE, "r")) == NULL) {
327				perror(_PATH_TILDE);
328				break;
329			}
330			while ((t = getc(fbuf)) != EOF)
331				(void) putchar(t);
332			Fclose(fbuf);
333			break;
334		case 'p':
335			/*
336			 * Print out the current state of the
337			 * message without altering anything.
338			 */
339			rewind(collf);
340			printf("-------\nMessage contains:\n");
341			puthead(hp, stdout, GTO|GSUBJECT|GCC|GBCC|GNL);
342			while ((t = getc(collf)) != EOF)
343				(void) putchar(t);
344			goto cont;
345		case '|':
346			/*
347			 * Pipe message through command.
348			 * Collect output as new message.
349			 */
350			rewind(collf);
351			mespipe(collf, &linebuf[2]);
352			goto cont;
353		case 'v':
354		case 'e':
355			/*
356			 * Edit the current message.
357			 * 'e' means to use EDITOR
358			 * 'v' means to use VISUAL
359			 */
360			rewind(collf);
361			mesedit(collf, c);
362			goto cont;
363		}
364	}
365	goto out;
366err:
367	if (collf != NULL) {
368		Fclose(collf);
369		collf = NULL;
370	}
371out:
372	if (collf != NULL)
373		rewind(collf);
374	noreset--;
375	sigblock(sigmask(SIGINT) | sigmask(SIGHUP));
376	signal(SIGINT, saveint);
377	signal(SIGHUP, savehup);
378	signal(SIGTSTP, savetstp);
379	signal(SIGTTOU, savettou);
380	signal(SIGTTIN, savettin);
381	sigsetmask(omask);
382	return collf;
383}
384
385/*
386 * Write a file, ex-like if f set.
387 */
388int
389exwrite(name, fp, f)
390	char name[];
391	FILE *fp;
392	int f;
393{
394	register FILE *of;
395	register int c;
396	long cc;
397	int lc;
398	struct stat junk;
399
400	if (f) {
401		printf("\"%s\" ", name);
402		fflush(stdout);
403	}
404	if (stat(name, &junk) >= 0 && (junk.st_mode & S_IFMT) == S_IFREG) {
405		if (!f)
406			fprintf(stderr, "%s: ", name);
407		fprintf(stderr, "File exists\n");
408		return(-1);
409	}
410	if ((of = Fopen(name, "w")) == NULL) {
411		perror(NOSTR);
412		return(-1);
413	}
414	lc = 0;
415	cc = 0;
416	while ((c = getc(fp)) != EOF) {
417		cc++;
418		if (c == '\n')
419			lc++;
420		(void) putc(c, of);
421		if (ferror(of)) {
422			perror(name);
423			Fclose(of);
424			return(-1);
425		}
426	}
427	Fclose(of);
428	printf("%d/%ld\n", lc, cc);
429	fflush(stdout);
430	return(0);
431}
432
433/*
434 * Edit the message being collected on fp.
435 * On return, make the edit file the new temp file.
436 */
437void
438mesedit(fp, c)
439	FILE *fp;
440	int c;
441{
442	sig_t sigint = signal(SIGINT, SIG_IGN);
443	FILE *nf = run_editor(fp, (off_t)-1, c, 0);
444
445	if (nf != NULL) {
446		fseek(nf, 0L, 2);
447		collf = nf;
448		Fclose(fp);
449	}
450	(void) signal(SIGINT, sigint);
451}
452
453/*
454 * Pipe the message through the command.
455 * Old message is on stdin of command;
456 * New message collected from stdout.
457 * Sh -c must return 0 to accept the new message.
458 */
459void
460mespipe(fp, cmd)
461	FILE *fp;
462	char cmd[];
463{
464	FILE *nf;
465	sig_t sigint = signal(SIGINT, SIG_IGN);
466	extern char tempEdit[];
467	char *shell;
468
469	if ((nf = Fopen(tempEdit, "w+")) == NULL) {
470		perror(tempEdit);
471		goto out;
472	}
473	(void) unlink(tempEdit);
474	/*
475	 * stdin = current message.
476	 * stdout = new message.
477	 */
478	if ((shell = value("SHELL")) == NOSTR)
479		shell = _PATH_CSHELL;
480	if (run_command(shell,
481	    0, fileno(fp), fileno(nf), "-c", cmd, NOSTR) < 0) {
482		(void) Fclose(nf);
483		goto out;
484	}
485	if (fsize(nf) == 0) {
486		fprintf(stderr, "No bytes from \"%s\" !?\n", cmd);
487		(void) Fclose(nf);
488		goto out;
489	}
490	/*
491	 * Take new files.
492	 */
493	(void) fseek(nf, 0L, 2);
494	collf = nf;
495	(void) Fclose(fp);
496out:
497	(void) signal(SIGINT, sigint);
498}
499
500/*
501 * Interpolate the named messages into the current
502 * message, preceding each line with a tab.
503 * Return a count of the number of characters now in
504 * the message, or -1 if an error is encountered writing
505 * the message temporary.  The flag argument is 'm' if we
506 * should shift over and 'f' if not.
507 */
508int
509forward(ms, fp, f)
510	char ms[];
511	FILE *fp;
512	int f;
513{
514	register int *msgvec;
515	extern char tempMail[];
516	struct ignoretab *ig;
517	char *tabst;
518
519	msgvec = (int *) salloc((msgCount+1) * sizeof *msgvec);
520	if (msgvec == (int *) NOSTR)
521		return(0);
522	if (getmsglist(ms, msgvec, 0) < 0)
523		return(0);
524	if (*msgvec == 0) {
525		*msgvec = first(0, MMNORM);
526		if (*msgvec == 0) {
527			printf("No appropriate messages\n");
528			return(0);
529		}
530		msgvec[1] = 0;
531	}
532	if (f == 'f' || f == 'F')
533		tabst = NOSTR;
534	else if ((tabst = value("indentprefix")) == NOSTR)
535		tabst = "\t";
536	ig = isupper(f) ? NULL : ignore;
537	printf("Interpolating:");
538	for (; *msgvec != 0; msgvec++) {
539		struct message *mp = message + *msgvec - 1;
540
541		touch(mp);
542		printf(" %d", *msgvec);
543		if (send(mp, fp, ig, tabst) < 0) {
544			perror(tempMail);
545			return(-1);
546		}
547	}
548	printf("\n");
549	return(0);
550}
551
552/*
553 * Print (continue) when continued after ^Z.
554 */
555/*ARGSUSED*/
556void
557collstop(s)
558	int s;
559{
560	sig_t old_action = signal(s, SIG_DFL);
561
562	sigsetmask(sigblock(0) & ~sigmask(s));
563	kill(0, s);
564	sigblock(sigmask(s));
565	signal(s, old_action);
566	if (colljmp_p) {
567		colljmp_p = 0;
568		hadintr = 0;
569		longjmp(colljmp, 1);
570	}
571}
572
573/*
574 * On interrupt, come here to save the partial message in ~/dead.letter.
575 * Then jump out of the collection loop.
576 */
577/*ARGSUSED*/
578void
579collint(s)
580	int s;
581{
582	/*
583	 * the control flow is subtle, because we can be called from ~q.
584	 */
585	if (!hadintr) {
586		if (value("ignore") != NOSTR) {
587			puts("@");
588			fflush(stdout);
589			clearerr(stdin);
590			return;
591		}
592		hadintr = 1;
593		longjmp(colljmp, 1);
594	}
595	rewind(collf);
596	if (value("nosave") == NOSTR)
597		savedeadletter(collf);
598	longjmp(collabort, 1);
599}
600
601/*ARGSUSED*/
602void
603collhup(s)
604	int s;
605{
606	rewind(collf);
607	savedeadletter(collf);
608	/*
609	 * Let's pretend nobody else wants to clean up,
610	 * a true statement at this time.
611	 */
612	exit(1);
613}
614
615void
616savedeadletter(fp)
617	register FILE *fp;
618{
619	register FILE *dbuf;
620	register int c;
621	char *cp;
622
623	if (fsize(fp) == 0)
624		return;
625	cp = getdeadletter();
626	c = umask(077);
627	dbuf = Fopen(cp, "a");
628	(void) umask(c);
629	if (dbuf == NULL)
630		return;
631	while ((c = getc(fp)) != EOF)
632		(void) putc(c, dbuf);
633	Fclose(dbuf);
634	rewind(fp);
635}
636