xref: /freebsd/usr.bin/mail/main.c (revision 9ce73e901878324070d0420a285d144d5acffe2d)
19b50d902SRodney W. Grimes /*
29b50d902SRodney W. Grimes  * Copyright (c) 1980, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
69b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
79b50d902SRodney W. Grimes  * are met:
89b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
99b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
109b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
129b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
139b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
149b50d902SRodney W. Grimes  *    must display the following acknowledgement:
159b50d902SRodney W. Grimes  *	This product includes software developed by the University of
169b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
179b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
189b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
199b50d902SRodney W. Grimes  *    without specific prior written permission.
209b50d902SRodney W. Grimes  *
219b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
229b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
249b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
259b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
269b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
279b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
289b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
299b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
309b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319b50d902SRodney W. Grimes  * SUCH DAMAGE.
329b50d902SRodney W. Grimes  */
339b50d902SRodney W. Grimes 
349b50d902SRodney W. Grimes #ifndef lint
359b50d902SRodney W. Grimes static char copyright[] =
369b50d902SRodney W. Grimes "@(#) Copyright (c) 1980, 1993\n\
379b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
389b50d902SRodney W. Grimes #endif /* not lint */
399b50d902SRodney W. Grimes 
409b50d902SRodney W. Grimes #ifndef lint
410c3a8314SMike Heffner #if 0
429b50d902SRodney W. Grimes static char sccsid[] = "@(#)main.c	8.1 (Berkeley) 6/6/93";
430c3a8314SMike Heffner #endif
440c3a8314SMike Heffner static const char rcsid[] =
450c3a8314SMike Heffner   "$FreeBSD$";
469b50d902SRodney W. Grimes #endif /* not lint */
479b50d902SRodney W. Grimes 
489b50d902SRodney W. Grimes #include "rcv.h"
499b50d902SRodney W. Grimes #include <fcntl.h>
509b50d902SRodney W. Grimes #include "extern.h"
519b50d902SRodney W. Grimes 
529b50d902SRodney W. Grimes /*
539b50d902SRodney W. Grimes  * Mail -- a mail program
549b50d902SRodney W. Grimes  *
559b50d902SRodney W. Grimes  * Startup -- interface with user.
569b50d902SRodney W. Grimes  */
579b50d902SRodney W. Grimes 
589b50d902SRodney W. Grimes jmp_buf	hdrjmp;
599b50d902SRodney W. Grimes 
609ce73e90SMike Heffner extern const char *version;
619ce73e90SMike Heffner 
629b50d902SRodney W. Grimes int
639b50d902SRodney W. Grimes main(argc, argv)
649b50d902SRodney W. Grimes 	int argc;
659b50d902SRodney W. Grimes 	char *argv[];
669b50d902SRodney W. Grimes {
679ce73e90SMike Heffner 	int i;
689b50d902SRodney W. Grimes 	struct name *to, *cc, *bcc, *smopts;
6999bd6601SJoerg Wunsch 	char *subject, *replyto;
7099bd6601SJoerg Wunsch 	char *ef, *cp;
719b50d902SRodney W. Grimes 	char nosrc = 0;
729b50d902SRodney W. Grimes 	sig_t prevint;
739b50d902SRodney W. Grimes 
749b50d902SRodney W. Grimes 	/*
759b50d902SRodney W. Grimes 	 * Set up a reasonable environment.
769b50d902SRodney W. Grimes 	 * Figure out whether we are being run interactively,
779b50d902SRodney W. Grimes 	 * start the SIGCHLD catcher, and so forth.
789b50d902SRodney W. Grimes 	 */
799b50d902SRodney W. Grimes 	(void)signal(SIGCHLD, sigchild);
809b50d902SRodney W. Grimes 	if (isatty(0))
819b50d902SRodney W. Grimes 		assign("interactive", "");
829b50d902SRodney W. Grimes 	image = -1;
839b50d902SRodney W. Grimes 	/*
849b50d902SRodney W. Grimes 	 * Now, determine how we are being used.
859b50d902SRodney W. Grimes 	 * We successively pick off - flags.
869b50d902SRodney W. Grimes 	 * If there is anything left, it is the base of the list
879b50d902SRodney W. Grimes 	 * of users to mail to.  Argp will be set to point to the
889b50d902SRodney W. Grimes 	 * first of these users.
899b50d902SRodney W. Grimes 	 */
909ce73e90SMike Heffner 	ef = NULL;
919ce73e90SMike Heffner 	to = NULL;
929ce73e90SMike Heffner 	cc = NULL;
939ce73e90SMike Heffner 	bcc = NULL;
949ce73e90SMike Heffner 	smopts = NULL;
959ce73e90SMike Heffner 	subject = NULL;
969ce73e90SMike Heffner 	replyto = NULL;
971c8af878SWarner Losh 	while ((i = getopt(argc, argv, "INT:b:c:dfins:u:v")) != -1) {
989b50d902SRodney W. Grimes 		switch (i) {
999b50d902SRodney W. Grimes 		case 'T':
1009b50d902SRodney W. Grimes 			/*
1019b50d902SRodney W. Grimes 			 * Next argument is temp file to write which
1029b50d902SRodney W. Grimes 			 * articles have been read/deleted for netnews.
1039b50d902SRodney W. Grimes 			 */
1049b50d902SRodney W. Grimes 			Tflag = optarg;
1050c3a8314SMike Heffner 			if ((i = open(Tflag, O_CREAT | O_TRUNC | O_WRONLY,
1060c3a8314SMike Heffner 			    0600)) < 0)
1070c3a8314SMike Heffner 				err(1, "%s", Tflag);
1089ce73e90SMike Heffner 			(void)close(i);
1099b50d902SRodney W. Grimes 			break;
1109b50d902SRodney W. Grimes 		case 'u':
1119b50d902SRodney W. Grimes 			/*
1129b50d902SRodney W. Grimes 			 * Next argument is person to pretend to be.
1139b50d902SRodney W. Grimes 			 */
1149b50d902SRodney W. Grimes 			myname = optarg;
11549936df3SJean-Marc Zucconi 			unsetenv("MAIL");
1169b50d902SRodney W. Grimes 			break;
1179b50d902SRodney W. Grimes 		case 'i':
1189b50d902SRodney W. Grimes 			/*
1199b50d902SRodney W. Grimes 			 * User wants to ignore interrupts.
1209b50d902SRodney W. Grimes 			 * Set the variable "ignore"
1219b50d902SRodney W. Grimes 			 */
1229b50d902SRodney W. Grimes 			assign("ignore", "");
1239b50d902SRodney W. Grimes 			break;
1249b50d902SRodney W. Grimes 		case 'd':
1259b50d902SRodney W. Grimes 			debug++;
1269b50d902SRodney W. Grimes 			break;
1279b50d902SRodney W. Grimes 		case 's':
1289b50d902SRodney W. Grimes 			/*
1299b50d902SRodney W. Grimes 			 * Give a subject field for sending from
1309b50d902SRodney W. Grimes 			 * non terminal
1319b50d902SRodney W. Grimes 			 */
1329b50d902SRodney W. Grimes 			subject = optarg;
1339b50d902SRodney W. Grimes 			break;
1349b50d902SRodney W. Grimes 		case 'f':
1359b50d902SRodney W. Grimes 			/*
1369b50d902SRodney W. Grimes 			 * User is specifying file to "edit" with Mail,
1379b50d902SRodney W. Grimes 			 * as opposed to reading system mailbox.
1389b50d902SRodney W. Grimes 			 * If no argument is given after -f, we read his
1399b50d902SRodney W. Grimes 			 * mbox file.
1409b50d902SRodney W. Grimes 			 *
1419b50d902SRodney W. Grimes 			 * getopt() can't handle optional arguments, so here
1429b50d902SRodney W. Grimes 			 * is an ugly hack to get around it.
1439b50d902SRodney W. Grimes 			 */
1449ce73e90SMike Heffner 			if ((argv[optind] != NULL) && (argv[optind][0] != '-'))
1459b50d902SRodney W. Grimes 				ef = argv[optind++];
1469b50d902SRodney W. Grimes 			else
1479b50d902SRodney W. Grimes 				ef = "&";
1489b50d902SRodney W. Grimes 			break;
1499b50d902SRodney W. Grimes 		case 'n':
1509b50d902SRodney W. Grimes 			/*
1519b50d902SRodney W. Grimes 			 * User doesn't want to source /usr/lib/Mail.rc
1529b50d902SRodney W. Grimes 			 */
1539b50d902SRodney W. Grimes 			nosrc++;
1549b50d902SRodney W. Grimes 			break;
1559b50d902SRodney W. Grimes 		case 'N':
1569b50d902SRodney W. Grimes 			/*
1579b50d902SRodney W. Grimes 			 * Avoid initial header printing.
1589b50d902SRodney W. Grimes 			 */
1599b50d902SRodney W. Grimes 			assign("noheader", "");
1609b50d902SRodney W. Grimes 			break;
1619b50d902SRodney W. Grimes 		case 'v':
1629b50d902SRodney W. Grimes 			/*
1639b50d902SRodney W. Grimes 			 * Send mailer verbose flag
1649b50d902SRodney W. Grimes 			 */
1659b50d902SRodney W. Grimes 			assign("verbose", "");
1669b50d902SRodney W. Grimes 			break;
1679b50d902SRodney W. Grimes 		case 'I':
1689b50d902SRodney W. Grimes 			/*
1699b50d902SRodney W. Grimes 			 * We're interactive
1709b50d902SRodney W. Grimes 			 */
1719b50d902SRodney W. Grimes 			assign("interactive", "");
1729b50d902SRodney W. Grimes 			break;
1739b50d902SRodney W. Grimes 		case 'c':
1749b50d902SRodney W. Grimes 			/*
1759b50d902SRodney W. Grimes 			 * Get Carbon Copy Recipient list
1769b50d902SRodney W. Grimes 			 */
1779b50d902SRodney W. Grimes 			cc = cat(cc, nalloc(optarg, GCC));
1789b50d902SRodney W. Grimes 			break;
1799b50d902SRodney W. Grimes 		case 'b':
1809b50d902SRodney W. Grimes 			/*
1819b50d902SRodney W. Grimes 			 * Get Blind Carbon Copy Recipient list
1829b50d902SRodney W. Grimes 			 */
1839b50d902SRodney W. Grimes 			bcc = cat(bcc, nalloc(optarg, GBCC));
1849b50d902SRodney W. Grimes 			break;
1859b50d902SRodney W. Grimes 		case '?':
1860c3a8314SMike Heffner 			fprintf(stderr, "\
1870c3a8314SMike Heffner Usage: %s [-iInv] [-s subject] [-c cc-addr] [-b bcc-addr] to-addr ...\n\
1880c3a8314SMike Heffner        %*s [- sendmail-options ...]\n\
1890c3a8314SMike Heffner        %s [-iInNv] -f [name]\n\
1900c3a8314SMike Heffner        %s [-iInNv] [-u user]\n",__progname, strlen(__progname), "", __progname,
1910c3a8314SMike Heffner 				__progname);
1929b50d902SRodney W. Grimes 			exit(1);
1939b50d902SRodney W. Grimes 		}
1949b50d902SRodney W. Grimes 	}
1959ce73e90SMike Heffner 	for (i = optind; (argv[i] != NULL) && (*argv[i] != '-'); i++)
1969b50d902SRodney W. Grimes 		to = cat(to, nalloc(argv[i], GTO));
1979ce73e90SMike Heffner 	for (; argv[i] != NULL; i++)
1989b50d902SRodney W. Grimes 		smopts = cat(smopts, nalloc(argv[i], 0));
1999b50d902SRodney W. Grimes 	/*
2009b50d902SRodney W. Grimes 	 * Check for inconsistent arguments.
2019b50d902SRodney W. Grimes 	 */
2029ce73e90SMike Heffner 	if (to == NULL && (subject != NULL || cc != NULL || bcc != NULL))
2030c3a8314SMike Heffner 		errx(1, "You must specify direct recipients with -s, -c, or -b.");
2049ce73e90SMike Heffner 	if (ef != NULL && to != NULL)
2050c3a8314SMike Heffner 		errx(1, "Cannot give -f and people to send to.");
2069b50d902SRodney W. Grimes 	tinit();
2079b50d902SRodney W. Grimes 	setscreensize();
2089b50d902SRodney W. Grimes 	input = stdin;
2099b50d902SRodney W. Grimes 	rcvmode = !to;
2109b50d902SRodney W. Grimes 	spreserve();
211e9b074c3SJordan K. Hubbard 	if (!nosrc) {
212e9b074c3SJordan K. Hubbard 		char *s, *path_rc;
213e9b074c3SJordan K. Hubbard 
2140c3a8314SMike Heffner 		if ((path_rc = malloc(sizeof(_PATH_MASTER_RC))) == NULL)
2150c3a8314SMike Heffner 			err(1, "malloc(path_rc) failed");
216e9b074c3SJordan K. Hubbard 
217e9b074c3SJordan K. Hubbard 		strcpy(path_rc, _PATH_MASTER_RC);
218e9b074c3SJordan K. Hubbard 		while ((s = strsep(&path_rc, ":")) != NULL)
219e9b074c3SJordan K. Hubbard 			if (*s != '\0')
220e9b074c3SJordan K. Hubbard 				load(s);
221e9b074c3SJordan K. Hubbard 	}
22299bd6601SJoerg Wunsch 	if ((cp = getenv("REPLYTO")) != NULL)
22399bd6601SJoerg Wunsch 		replyto = cp;
224e9b074c3SJordan K. Hubbard 
2259b50d902SRodney W. Grimes 	/*
2269b50d902SRodney W. Grimes 	 * Expand returns a savestr, but load only uses the file name
2279b50d902SRodney W. Grimes 	 * for fopen, so it's safe to do this.
2289b50d902SRodney W. Grimes 	 */
2299b50d902SRodney W. Grimes 	load(expand("~/.mailrc"));
2309b50d902SRodney W. Grimes 	if (!rcvmode) {
23199bd6601SJoerg Wunsch 		mail(to, cc, bcc, smopts, subject, replyto);
2329b50d902SRodney W. Grimes 		/*
2339b50d902SRodney W. Grimes 		 * why wait?
2349b50d902SRodney W. Grimes 		 */
2359b50d902SRodney W. Grimes 		exit(senderr);
2369b50d902SRodney W. Grimes 	}
2379b50d902SRodney W. Grimes 	/*
2389b50d902SRodney W. Grimes 	 * Ok, we are reading mail.
2399b50d902SRodney W. Grimes 	 * Decide whether we are editing a mailbox or reading
2409b50d902SRodney W. Grimes 	 * the system mailbox, and open up the right stuff.
2419b50d902SRodney W. Grimes 	 */
2429ce73e90SMike Heffner 	if (ef == NULL)
2439b50d902SRodney W. Grimes 		ef = "%";
2449b50d902SRodney W. Grimes 	if (setfile(ef) < 0)
2459b50d902SRodney W. Grimes 		exit(1);		/* error already reported */
2469b50d902SRodney W. Grimes 	if (setjmp(hdrjmp) == 0) {
2479b50d902SRodney W. Grimes 		if ((prevint = signal(SIGINT, SIG_IGN)) != SIG_IGN)
2489ce73e90SMike Heffner 			(void)signal(SIGINT, hdrstop);
2499ce73e90SMike Heffner 		if (value("quiet") == NULL)
2509b50d902SRodney W. Grimes 			printf("Mail version %s.  Type ? for help.\n",
2519b50d902SRodney W. Grimes 				version);
2529b50d902SRodney W. Grimes 		announce();
2539ce73e90SMike Heffner 		(void)fflush(stdout);
2549ce73e90SMike Heffner 		(void)signal(SIGINT, prevint);
2559b50d902SRodney W. Grimes 	}
2569b50d902SRodney W. Grimes 	commands();
2579ce73e90SMike Heffner 	(void)signal(SIGHUP, SIG_IGN);
2589ce73e90SMike Heffner 	(void)signal(SIGINT, SIG_IGN);
2599ce73e90SMike Heffner 	(void)signal(SIGQUIT, SIG_IGN);
2609b50d902SRodney W. Grimes 	quit();
2619b50d902SRodney W. Grimes 	exit(0);
2629b50d902SRodney W. Grimes }
2639b50d902SRodney W. Grimes 
2649b50d902SRodney W. Grimes /*
2659b50d902SRodney W. Grimes  * Interrupt printing of the headers.
2669b50d902SRodney W. Grimes  */
2679ce73e90SMike Heffner /*ARGSUSED*/
2689b50d902SRodney W. Grimes void
2699b50d902SRodney W. Grimes hdrstop(signo)
2709b50d902SRodney W. Grimes 	int signo;
2719b50d902SRodney W. Grimes {
2729b50d902SRodney W. Grimes 
2739ce73e90SMike Heffner 	(void)fflush(stdout);
2749b50d902SRodney W. Grimes 	fprintf(stderr, "\nInterrupt\n");
2759b50d902SRodney W. Grimes 	longjmp(hdrjmp, 1);
2769b50d902SRodney W. Grimes }
2779b50d902SRodney W. Grimes 
2789b50d902SRodney W. Grimes /*
2799b50d902SRodney W. Grimes  * Compute what the screen size for printing headers should be.
2809b50d902SRodney W. Grimes  * We use the following algorithm for the height:
2819b50d902SRodney W. Grimes  *	If baud rate < 1200, use  9
2829b50d902SRodney W. Grimes  *	If baud rate = 1200, use 14
2839b50d902SRodney W. Grimes  *	If baud rate > 1200, use 24 or ws_row
2849b50d902SRodney W. Grimes  * Width is either 80 or ws_col;
2859b50d902SRodney W. Grimes  */
2869b50d902SRodney W. Grimes void
2879b50d902SRodney W. Grimes setscreensize()
2889b50d902SRodney W. Grimes {
2899b50d902SRodney W. Grimes 	struct winsize ws;
2907c6d202aSPeter Wemm 	struct termios tio;
2917c6d202aSPeter Wemm 	speed_t speed = 0;
2929b50d902SRodney W. Grimes 
2939b50d902SRodney W. Grimes 	if (ioctl(1, TIOCGWINSZ, (char *)&ws) < 0)
2949b50d902SRodney W. Grimes 		ws.ws_col = ws.ws_row = 0;
2957c6d202aSPeter Wemm 	if (tcgetattr(1, &tio) != -1)
2967c6d202aSPeter Wemm 		speed = cfgetospeed(&tio);
2977c6d202aSPeter Wemm 	if (speed <= 0)
2987c6d202aSPeter Wemm 		speed = B9600;
2997c6d202aSPeter Wemm 	if (speed < B1200)
3009b50d902SRodney W. Grimes 		screenheight = 9;
3017c6d202aSPeter Wemm 	else if (speed == B1200)
3029b50d902SRodney W. Grimes 		screenheight = 14;
3039b50d902SRodney W. Grimes 	else if (ws.ws_row != 0)
3049b50d902SRodney W. Grimes 		screenheight = ws.ws_row;
3059b50d902SRodney W. Grimes 	else
3069b50d902SRodney W. Grimes 		screenheight = 24;
3079b50d902SRodney W. Grimes 	if ((realscreenheight = ws.ws_row) == 0)
3089b50d902SRodney W. Grimes 		realscreenheight = 24;
3099b50d902SRodney W. Grimes 	if ((screenwidth = ws.ws_col) == 0)
3109b50d902SRodney W. Grimes 		screenwidth = 80;
3119b50d902SRodney W. Grimes }
312