Home | History | Annotate | Line # | Download | only in mail
collect.c revision 1.14
      1 /*	$NetBSD: collect.c,v 1.14 1997/10/31 22:15:52 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. 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.14 1997/10/31 22:15:52 christos 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 /*
     56  * Read a message from standard output and return a read file to it
     57  * or NULL on error.
     58  */
     59 
     60 /*
     61  * The following hokiness with global variables is so that on
     62  * receipt of an interrupt signal, the partial message can be salted
     63  * away on dead.letter.
     64  */
     65 
     66 static	sig_t	saveint;		/* Previous SIGINT value */
     67 static	sig_t	savehup;		/* Previous SIGHUP value */
     68 static	sig_t	savetstp;		/* Previous SIGTSTP value */
     69 static	sig_t	savettou;		/* Previous SIGTTOU value */
     70 static	sig_t	savettin;		/* Previous SIGTTIN value */
     71 static	FILE	*collf;			/* File for saving away */
     72 static	int	hadintr;		/* Have seen one SIGINT so far */
     73 
     74 static	jmp_buf	colljmp;		/* To get back to work */
     75 static	int	colljmp_p;		/* whether to long jump */
     76 static	jmp_buf	collabort;		/* To end collection with error */
     77 
     78 FILE *
     79 collect(hp, printheaders)
     80 	struct header *hp;
     81 	int printheaders;
     82 {
     83 	FILE *fbuf;
     84 	int lc, cc, escape, eofcount;
     85 	int c, t;
     86 	char linebuf[LINESIZE], *cp;
     87 	extern char *tempMail;
     88 	char getsub;
     89 	sigset_t oset, nset;
     90 	int longline, lastlong, rc;	/* So we don't make 2 or more lines
     91 					   out of a long input line. */
     92 #if __GNUC__
     93 	/* Avoid longjmp clobbering */
     94 	(void) &escape;
     95 	(void) &eofcount;
     96 	(void) &getsub;
     97 	(void) &longline;
     98 #endif
     99 
    100 	collf = NULL;
    101 	/*
    102 	 * Start catching signals from here, but we're still die on interrupts
    103 	 * until we're in the main loop.
    104 	 */
    105 	sigemptyset(&nset);
    106 	sigaddset(&nset, SIGINT);
    107 	sigaddset(&nset, SIGHUP);
    108 	sigprocmask(SIG_BLOCK, &nset, &oset);
    109 	if ((saveint = signal(SIGINT, SIG_IGN)) != SIG_IGN)
    110 		signal(SIGINT, collint);
    111 	if ((savehup = signal(SIGHUP, SIG_IGN)) != SIG_IGN)
    112 		signal(SIGHUP, collhup);
    113 	savetstp = signal(SIGTSTP, collstop);
    114 	savettou = signal(SIGTTOU, collstop);
    115 	savettin = signal(SIGTTIN, collstop);
    116 	if (setjmp(collabort) || setjmp(colljmp)) {
    117 		rm(tempMail);
    118 		goto err;
    119 	}
    120 	sigdelset(&oset, SIGINT);
    121 	sigdelset(&oset, SIGHUP);
    122 	sigprocmask(SIG_SETMASK, &oset, NULL);
    123 
    124 	noreset++;
    125 	if ((collf = Fopen(tempMail, "w+")) == NULL) {
    126 		perror(tempMail);
    127 		goto err;
    128 	}
    129 	unlink(tempMail);
    130 
    131 	/*
    132 	 * If we are going to prompt for a subject,
    133 	 * refrain from printing a newline after
    134 	 * the headers (since some people mind).
    135 	 */
    136 	t = GTO|GSUBJECT|GCC|GNL;
    137 	getsub = 0;
    138 	if (hp->h_subject == NOSTR && value("interactive") != NOSTR &&
    139 	    (value("ask") != NOSTR || value("asksub") != NOSTR))
    140 		t &= ~GNL, getsub++;
    141 	if (printheaders) {
    142 		puthead(hp, stdout, t);
    143 		fflush(stdout);
    144 	}
    145 	if ((cp = value("escape")) != NOSTR)
    146 		escape = *cp;
    147 	else
    148 		escape = ESCAPE;
    149 	eofcount = 0;
    150 	hadintr = 0;
    151 	lastlong = 0;
    152 	longline = 0;
    153 
    154 	if (!setjmp(colljmp)) {
    155 		if (getsub)
    156 			grabh(hp, GSUBJECT);
    157 	} else {
    158 		/*
    159 		 * Come here for printing the after-signal message.
    160 		 * Duplicate messages won't be printed because
    161 		 * the write is aborted if we get a SIGTTOU.
    162 		 */
    163 cont:
    164 		if (hadintr) {
    165 			fflush(stdout);
    166 			fprintf(stderr,
    167 			"\n(Interrupt -- one more to kill letter)\n");
    168 		} else {
    169 			printf("(continue)\n");
    170 			fflush(stdout);
    171 		}
    172 	}
    173 	for (;;) {
    174 		colljmp_p = 1;
    175 		c = readline(stdin, linebuf, LINESIZE);
    176 		colljmp_p = 0;
    177 		if (c < 0) {
    178 			if (value("interactive") != NOSTR &&
    179 			    value("ignoreeof") != NOSTR && ++eofcount < 25) {
    180 				printf("Use \".\" to terminate letter\n");
    181 				continue;
    182 			}
    183 			break;
    184 		}
    185 		lastlong = longline;
    186 		longline = c == LINESIZE-1;
    187 		eofcount = 0;
    188 		hadintr = 0;
    189 		if (linebuf[0] == '.' && linebuf[1] == '\0' &&
    190 		    value("interactive") != NOSTR && !lastlong &&
    191 		    (value("dot") != NOSTR || value("ignoreeof") != NOSTR))
    192 			break;
    193 		if (linebuf[0] != escape || value("interactive") == NOSTR ||
    194 		    lastlong) {
    195 			if (putline(collf, linebuf, !longline) < 0)
    196 				goto err;
    197 			continue;
    198 		}
    199 		c = linebuf[1];
    200 		switch (c) {
    201 		default:
    202 			/*
    203 			 * On double escape, just send the single one.
    204 			 * Otherwise, it's an error.
    205 			 */
    206 			if (c == escape) {
    207 				if (putline(collf, &linebuf[1], !longline) < 0)
    208 					goto err;
    209 				else
    210 					break;
    211 			}
    212 			printf("Unknown tilde escape.\n");
    213 			break;
    214 		case 'C':
    215 			/*
    216 			 * Dump core.
    217 			 */
    218 			core(NULL);
    219 			break;
    220 		case '!':
    221 			/*
    222 			 * Shell escape, send the balance of the
    223 			 * line to sh -c.
    224 			 */
    225 			shell(&linebuf[2]);
    226 			break;
    227 		case ':':
    228 		case '_':
    229 			/*
    230 			 * Escape to command mode, but be nice!
    231 			 */
    232 			execute(&linebuf[2], 1);
    233 			goto cont;
    234 		case '.':
    235 			/*
    236 			 * Simulate end of file on input.
    237 			 */
    238 			goto out;
    239 		case 'q':
    240 			/*
    241 			 * Force a quit of sending mail.
    242 			 * Act like an interrupt happened.
    243 			 */
    244 			hadintr++;
    245 			collint(SIGINT);
    246 			exit(1);
    247 		case 'h':
    248 			/*
    249 			 * Grab a bunch of headers.
    250 			 */
    251 			grabh(hp, GTO|GSUBJECT|GCC|GBCC);
    252 			goto cont;
    253 		case 't':
    254 			/*
    255 			 * Add to the To list.
    256 			 */
    257 			hp->h_to = cat(hp->h_to, extract(&linebuf[2], GTO));
    258 			break;
    259 		case 's':
    260 			/*
    261 			 * Set the Subject list.
    262 			 */
    263 			cp = &linebuf[2];
    264 			while (isspace(*cp))
    265 				cp++;
    266 			hp->h_subject = savestr(cp);
    267 			break;
    268 		case 'c':
    269 			/*
    270 			 * Add to the CC list.
    271 			 */
    272 			hp->h_cc = cat(hp->h_cc, extract(&linebuf[2], GCC));
    273 			break;
    274 		case 'b':
    275 			/*
    276 			 * Add stuff to blind carbon copies list.
    277 			 */
    278 			hp->h_bcc = cat(hp->h_bcc, extract(&linebuf[2], GBCC));
    279 			break;
    280 		case 'd':
    281 			strcpy(linebuf + 2, getdeadletter());
    282 			/* fall into . . . */
    283 		case 'r':
    284 		case '<':
    285 			/*
    286 			 * Invoke a file:
    287 			 * Search for the file name,
    288 			 * then open it and copy the contents to collf.
    289 			 */
    290 			cp = &linebuf[2];
    291 			while (isspace(*cp))
    292 				cp++;
    293 			if (*cp == '\0') {
    294 				printf("Interpolate what file?\n");
    295 				break;
    296 			}
    297 			cp = expand(cp);
    298 			if (cp == NOSTR)
    299 				break;
    300 			if (isdir(cp)) {
    301 				printf("%s: Directory\n", cp);
    302 				break;
    303 			}
    304 			if ((fbuf = Fopen(cp, "r")) == NULL) {
    305 				perror(cp);
    306 				break;
    307 			}
    308 			printf("\"%s\" ", cp);
    309 			fflush(stdout);
    310 			lc = 0;
    311 			cc = 0;
    312 			while ((rc = readline(fbuf, linebuf, LINESIZE)) >= 0) {
    313 				if (rc != LINESIZE-1) lc++;
    314 				if ((t = putline(collf, linebuf,
    315 						 rc != LINESIZE-1)) < 0) {
    316 					Fclose(fbuf);
    317 					goto err;
    318 				}
    319 				cc += t;
    320 			}
    321 			Fclose(fbuf);
    322 			printf("%d/%d\n", lc, cc);
    323 			break;
    324 		case 'w':
    325 			/*
    326 			 * Write the message on a file.
    327 			 */
    328 			cp = &linebuf[2];
    329 			while (*cp == ' ' || *cp == '\t')
    330 				cp++;
    331 			if (*cp == '\0') {
    332 				fprintf(stderr, "Write what file!?\n");
    333 				break;
    334 			}
    335 			if ((cp = expand(cp)) == NOSTR)
    336 				break;
    337 			rewind(collf);
    338 			exwrite(cp, collf, 1);
    339 			break;
    340 		case 'm':
    341 		case 'M':
    342 		case 'f':
    343 		case 'F':
    344 			/*
    345 			 * Interpolate the named messages, if we
    346 			 * are in receiving mail mode.  Does the
    347 			 * standard list processing garbage.
    348 			 * If ~f is given, we don't shift over.
    349 			 */
    350 			if (forward(linebuf + 2, collf, c) < 0)
    351 				goto err;
    352 			goto cont;
    353 		case '?':
    354 			if ((fbuf = Fopen(_PATH_TILDE, "r")) == NULL) {
    355 				perror(_PATH_TILDE);
    356 				break;
    357 			}
    358 			while ((t = getc(fbuf)) != EOF)
    359 				(void) putchar(t);
    360 			Fclose(fbuf);
    361 			break;
    362 		case 'p':
    363 			/*
    364 			 * Print out the current state of the
    365 			 * message without altering anything.
    366 			 */
    367 			rewind(collf);
    368 			printf("-------\nMessage contains:\n");
    369 			puthead(hp, stdout, GTO|GSUBJECT|GCC|GBCC|GNL);
    370 			while ((t = getc(collf)) != EOF)
    371 				(void) putchar(t);
    372 			goto cont;
    373 		case '|':
    374 			/*
    375 			 * Pipe message through command.
    376 			 * Collect output as new message.
    377 			 */
    378 			rewind(collf);
    379 			mespipe(collf, &linebuf[2]);
    380 			goto cont;
    381 		case 'v':
    382 		case 'e':
    383 			/*
    384 			 * Edit the current message.
    385 			 * 'e' means to use EDITOR
    386 			 * 'v' means to use VISUAL
    387 			 */
    388 			rewind(collf);
    389 			mesedit(collf, c);
    390 			goto cont;
    391 		}
    392 	}
    393 	goto out;
    394 err:
    395 	if (collf != NULL) {
    396 		Fclose(collf);
    397 		collf = NULL;
    398 	}
    399 out:
    400 	if (collf != NULL)
    401 		rewind(collf);
    402 	noreset--;
    403 	sigemptyset(&nset);
    404 	sigaddset(&nset, SIGINT);
    405 	sigaddset(&nset, SIGHUP);
    406 	sigprocmask(SIG_BLOCK, &nset, &oset);
    407 	signal(SIGINT, saveint);
    408 	signal(SIGHUP, savehup);
    409 	signal(SIGTSTP, savetstp);
    410 	signal(SIGTTOU, savettou);
    411 	signal(SIGTTIN, savettin);
    412 	sigprocmask(SIG_SETMASK, &oset, NULL);
    413 	return collf;
    414 }
    415 
    416 /*
    417  * Write a file, ex-like if f set.
    418  */
    419 int
    420 exwrite(name, fp, f)
    421 	char name[];
    422 	FILE *fp;
    423 	int f;
    424 {
    425 	FILE *of;
    426 	int c;
    427 	long cc;
    428 	int lc;
    429 	struct stat junk;
    430 
    431 	if (f) {
    432 		printf("\"%s\" ", name);
    433 		fflush(stdout);
    434 	}
    435 	if (stat(name, &junk) >= 0 && S_ISREG(junk.st_mode)) {
    436 		if (!f)
    437 			fprintf(stderr, "%s: ", name);
    438 		fprintf(stderr, "File exists\n");
    439 		return(-1);
    440 	}
    441 	if ((of = Fopen(name, "w")) == NULL) {
    442 		perror(NOSTR);
    443 		return(-1);
    444 	}
    445 	lc = 0;
    446 	cc = 0;
    447 	while ((c = getc(fp)) != EOF) {
    448 		cc++;
    449 		if (c == '\n')
    450 			lc++;
    451 		(void) putc(c, of);
    452 		if (ferror(of)) {
    453 			perror(name);
    454 			Fclose(of);
    455 			return(-1);
    456 		}
    457 	}
    458 	Fclose(of);
    459 	printf("%d/%ld\n", lc, cc);
    460 	fflush(stdout);
    461 	return(0);
    462 }
    463 
    464 /*
    465  * Edit the message being collected on fp.
    466  * On return, make the edit file the new temp file.
    467  */
    468 void
    469 mesedit(fp, c)
    470 	FILE *fp;
    471 	int c;
    472 {
    473 	sig_t sigint = signal(SIGINT, SIG_IGN);
    474 	FILE *nf = run_editor(fp, (off_t)-1, c, 0);
    475 
    476 	if (nf != NULL) {
    477 		fseek(nf, 0L, 2);
    478 		collf = nf;
    479 		Fclose(fp);
    480 	}
    481 	(void) signal(SIGINT, sigint);
    482 }
    483 
    484 /*
    485  * Pipe the message through the command.
    486  * Old message is on stdin of command;
    487  * New message collected from stdout.
    488  * Sh -c must return 0 to accept the new message.
    489  */
    490 void
    491 mespipe(fp, cmd)
    492 	FILE *fp;
    493 	char cmd[];
    494 {
    495 	FILE *nf;
    496 	sig_t sigint = signal(SIGINT, SIG_IGN);
    497 	extern char *tempEdit;
    498 	char *shell;
    499 
    500 	if ((nf = Fopen(tempEdit, "w+")) == NULL) {
    501 		perror(tempEdit);
    502 		goto out;
    503 	}
    504 	(void) unlink(tempEdit);
    505 	/*
    506 	 * stdin = current message.
    507 	 * stdout = new message.
    508 	 */
    509 	if ((shell = value("SHELL")) == NOSTR)
    510 		shell = _PATH_CSHELL;
    511 	if (run_command(shell,
    512 	    0, fileno(fp), fileno(nf), "-c", cmd, NOSTR) < 0) {
    513 		(void) Fclose(nf);
    514 		goto out;
    515 	}
    516 	if (fsize(nf) == 0) {
    517 		fprintf(stderr, "No bytes from \"%s\" !?\n", cmd);
    518 		(void) Fclose(nf);
    519 		goto out;
    520 	}
    521 	/*
    522 	 * Take new files.
    523 	 */
    524 	(void) fseek(nf, 0L, 2);
    525 	collf = nf;
    526 	(void) Fclose(fp);
    527 out:
    528 	(void) signal(SIGINT, sigint);
    529 }
    530 
    531 /*
    532  * Interpolate the named messages into the current
    533  * message, preceding each line with a tab.
    534  * Return a count of the number of characters now in
    535  * the message, or -1 if an error is encountered writing
    536  * the message temporary.  The flag argument is 'm' if we
    537  * should shift over and 'f' if not.
    538  */
    539 int
    540 forward(ms, fp, f)
    541 	char ms[];
    542 	FILE *fp;
    543 	int f;
    544 {
    545 	int *msgvec;
    546 	extern char *tempMail;
    547 	struct ignoretab *ig;
    548 	char *tabst;
    549 
    550 	msgvec = (int *) salloc((msgCount+1) * sizeof *msgvec);
    551 	if (msgvec == (int *) NOSTR)
    552 		return(0);
    553 	if (getmsglist(ms, msgvec, 0) < 0)
    554 		return(0);
    555 	if (*msgvec == 0) {
    556 		*msgvec = first(0, MMNORM);
    557 		if (*msgvec == 0) {
    558 			printf("No appropriate messages\n");
    559 			return(0);
    560 		}
    561 		msgvec[1] = 0;
    562 	}
    563 	if (f == 'f' || f == 'F')
    564 		tabst = NOSTR;
    565 	else if ((tabst = value("indentprefix")) == NOSTR)
    566 		tabst = "\t";
    567 	ig = isupper(f) ? NULL : ignore;
    568 	printf("Interpolating:");
    569 	for (; *msgvec != 0; msgvec++) {
    570 		struct message *mp = message + *msgvec - 1;
    571 
    572 		touch(mp);
    573 		printf(" %d", *msgvec);
    574 		if (send(mp, fp, ig, tabst) < 0) {
    575 			perror(tempMail);
    576 			return(-1);
    577 		}
    578 	}
    579 	printf("\n");
    580 	return(0);
    581 }
    582 
    583 /*
    584  * Print (continue) when continued after ^Z.
    585  */
    586 /*ARGSUSED*/
    587 void
    588 collstop(s)
    589 	int s;
    590 {
    591 	sig_t old_action = signal(s, SIG_DFL);
    592 	sigset_t nset;
    593 
    594 	sigemptyset(&nset);
    595 	sigaddset(&nset, s);
    596 	sigprocmask(SIG_UNBLOCK, &nset, NULL);
    597 	kill(0, s);
    598 	sigprocmask(SIG_BLOCK, &nset, NULL);
    599 	signal(s, old_action);
    600 	if (colljmp_p) {
    601 		colljmp_p = 0;
    602 		hadintr = 0;
    603 		longjmp(colljmp, 1);
    604 	}
    605 }
    606 
    607 /*
    608  * On interrupt, come here to save the partial message in ~/dead.letter.
    609  * Then jump out of the collection loop.
    610  */
    611 /*ARGSUSED*/
    612 void
    613 collint(s)
    614 	int s;
    615 {
    616 	/*
    617 	 * the control flow is subtle, because we can be called from ~q.
    618 	 */
    619 	if (!hadintr) {
    620 		if (value("ignore") != NOSTR) {
    621 			puts("@");
    622 			fflush(stdout);
    623 			clearerr(stdin);
    624 			return;
    625 		}
    626 		hadintr = 1;
    627 		longjmp(colljmp, 1);
    628 	}
    629 	rewind(collf);
    630 	if (value("nosave") == NOSTR)
    631 		savedeadletter(collf);
    632 	longjmp(collabort, 1);
    633 }
    634 
    635 /*ARGSUSED*/
    636 void
    637 collhup(s)
    638 	int s;
    639 {
    640 	rewind(collf);
    641 	savedeadletter(collf);
    642 	/*
    643 	 * Let's pretend nobody else wants to clean up,
    644 	 * a true statement at this time.
    645 	 */
    646 	exit(1);
    647 }
    648 
    649 void
    650 savedeadletter(fp)
    651 	FILE *fp;
    652 {
    653 	FILE *dbuf;
    654 	int c;
    655 	char *cp;
    656 
    657 	if (fsize(fp) == 0)
    658 		return;
    659 	cp = getdeadletter();
    660 	c = umask(077);
    661 	dbuf = Fopen(cp, "a");
    662 	(void) umask(c);
    663 	if (dbuf == NULL)
    664 		return;
    665 	while ((c = getc(fp)) != EOF)
    666 		(void) putc(c, dbuf);
    667 	Fclose(dbuf);
    668 	rewind(fp);
    669 }
    670