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