xref: /openbsd-src/usr.bin/mail/main.c (revision 3a3fbb3f2e2521ab7c4a56b7ff7462ebd9095ec5)
1 /*	$OpenBSD: main.c,v 1.17 2001/11/21 15:28:25 millert Exp $	*/
2 /*	$NetBSD: main.c,v 1.7 1997/05/13 06:15:57 mikel Exp $	*/
3 
4 /*
5  * Copyright (c) 1980, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #ifndef lint
38 static const char copyright[] =
39 "@(#) Copyright (c) 1980, 1993\n\
40 	The Regents of the University of California.  All rights reserved.\n";
41 #endif /* not lint */
42 
43 #ifndef lint
44 #if 0
45 static const char sccsid[] = "@(#)main.c	8.2 (Berkeley) 4/20/95";
46 #else
47 static const char rcsid[] = "$OpenBSD: main.c,v 1.17 2001/11/21 15:28:25 millert Exp $";
48 #endif
49 #endif /* not lint */
50 
51 #include "rcv.h"
52 #include <fcntl.h>
53 #include <sys/ioctl.h>
54 #include "extern.h"
55 
56 __dead	void	usage(void);
57 	int	main(int, char **);
58 
59 /*
60  * Mail -- a mail program
61  *
62  * Startup -- interface with user.
63  */
64 
65 int
66 main(int argc, char **argv)
67 {
68 	int i;
69 	struct name *to, *cc, *bcc, *smopts;
70 	char *subject;
71 	char *ef;
72 	char nosrc = 0;
73 	char *rc;
74 	extern const char version[];
75 
76 	/*
77 	 * Set up a reasonable environment.
78 	 * Figure out whether we are being run interactively,
79 	 * start the SIGCHLD catcher, and so forth.
80 	 */
81 	(void)signal(SIGCHLD, sigchild);
82 	(void)signal(SIGPIPE, SIG_IGN);
83 	if (isatty(0))
84 		assign("interactive", "");
85 	image = -1;
86 	/*
87 	 * Now, determine how we are being used.
88 	 * We successively pick off - flags.
89 	 * If there is anything left, it is the base of the list
90 	 * of users to mail to.  Argp will be set to point to the
91 	 * first of these users.
92 	 */
93 	ef = NULL;
94 	to = NULL;
95 	cc = NULL;
96 	bcc = NULL;
97 	smopts = NULL;
98 	subject = NULL;
99 	while ((i = getopt(argc, argv, "INT:b:c:dfins:u:v")) != -1) {
100 		switch (i) {
101 		case 'T':
102 			/*
103 			 * Next argument is temp file to write which
104 			 * articles have been read/deleted for netnews.
105 			 */
106 			Tflag = optarg;
107 			if ((i = creat(Tflag, 0600)) < 0)
108 				err(1, "%s", Tflag);
109 			(void)close(i);
110 			break;
111 		case 'u':
112 			/*
113 			 * Next argument is person to pretend to be.
114 			 */
115 			if (strlen(optarg) >= MAXLOGNAME)
116 				errx(1, "username `%s' too long", optarg);
117 			unsetenv("MAIL");
118 			myname = optarg;
119 			uflag = 1;
120 			break;
121 		case 'i':
122 			/*
123 			 * User wants to ignore interrupts.
124 			 * Set the variable "ignore"
125 			 */
126 			assign("ignore", "");
127 			break;
128 		case 'd':
129 			debug++;
130 			break;
131 		case 's':
132 			/*
133 			 * Give a subject field for sending from
134 			 * non terminal
135 			 */
136 			subject = optarg;
137 			break;
138 		case 'f':
139 			/*
140 			 * User is specifying file to "edit" with Mail,
141 			 * as opposed to reading system mailbox.
142 			 * If no argument is given after -f, we read his
143 			 * mbox file.
144 			 *
145 			 * getopt() can't handle optional arguments, so here
146 			 * is an ugly hack to get around it.
147 			 */
148 			if ((argv[optind]) && (argv[optind][0] != '-'))
149 				ef = argv[optind++];
150 			else
151 				ef = "&";
152 			break;
153 		case 'n':
154 			/*
155 			 * User doesn't want to source /usr/lib/Mail.rc
156 			 */
157 			nosrc++;
158 			break;
159 		case 'N':
160 			/*
161 			 * Avoid initial header printing.
162 			 */
163 			assign("noheader", "");
164 			break;
165 		case 'v':
166 			/*
167 			 * Send mailer verbose flag
168 			 */
169 			assign("verbose", "");
170 			break;
171 		case 'I':
172 			/*
173 			 * We're interactive
174 			 */
175 			assign("interactive", "");
176 			break;
177 		case 'c':
178 			/*
179 			 * Get Carbon Copy Recipient list
180 			 */
181 			cc = cat(cc, nalloc(optarg, GCC));
182 			break;
183 		case 'b':
184 			/*
185 			 * Get Blind Carbon Copy Recipient list
186 			 */
187 			bcc = cat(bcc, nalloc(optarg, GBCC));
188 			break;
189 		default:
190 			usage();
191 			/*NOTREACHED*/
192 		}
193 	}
194 	for (i = optind; (argv[i]) && (*argv[i] != '-'); i++)
195 		to = cat(to, nalloc(argv[i], GTO));
196 	for (; argv[i]; i++)
197 		smopts = cat(smopts, nalloc(argv[i], 0));
198 	/*
199 	 * Check for inconsistent arguments.
200 	 */
201 	if (to == NULL && (subject != NULL || cc != NULL || bcc != NULL))
202 		errx(1, "You must specify direct recipients with -s, -c, or -b");
203 	if (ef != NULL && to != NULL)
204 		errx(1, "Cannot give -f and people to send to");
205 	/*
206 	 * Block SIGINT except where we install an explicit handler for it.
207 	 */
208 	sigemptyset(&intset);
209 	sigaddset(&intset, SIGINT);
210 	(void)sigprocmask(SIG_BLOCK, &intset, NULL);
211 	/*
212 	 * Initialization.
213 	 */
214 	tinit();
215 	setscreensize();
216 	input = stdin;
217 	rcvmode = !to;
218 	spreserve();
219 	if (!nosrc)
220 		load(_PATH_MASTER_RC);
221 	/*
222 	 * Expand returns a savestr, but load only uses the file name
223 	 * for fopen, so it's safe to do this.
224 	 */
225 	if ((rc = getenv("MAILRC")) == 0)
226 		rc = "~/.mailrc";
227 	load(expand(rc));
228 	if (!rcvmode) {
229 		mail(to, cc, bcc, smopts, subject);
230 		/*
231 		 * why wait?
232 		 */
233 		exit(senderr);
234 	}
235 	/*
236 	 * Ok, we are reading mail.
237 	 * Decide whether we are editing a mailbox or reading
238 	 * the system mailbox, and open up the right stuff.
239 	 */
240 	if (ef == NULL)
241 		ef = "%";
242 	if (setfile(ef) < 0)
243 		exit(1);		/* error already reported */
244 
245 	if (value("quiet") == NULL)
246 		(void)printf("Mail version %s.  Type ? for help.\n",
247 			version);
248 	announce();
249 	(void)fflush(stdout);
250 	commands();
251 	(void)ignoresig(SIGHUP, NULL, NULL);
252 	(void)ignoresig(SIGINT, NULL, NULL);
253 	(void)ignoresig(SIGQUIT, NULL, NULL);
254 	quit();
255 	exit(0);
256 }
257 
258 /*
259  * Compute what the screen size for printing headers should be.
260  * We use the following algorithm for the height:
261  *	If baud rate < 1200, use  9
262  *	If baud rate = 1200, use 14
263  *	If baud rate > 1200, use 24 or ws_row
264  * Width is either 80 or ws_col;
265  */
266 void
267 setscreensize(void)
268 {
269 	struct termios tbuf;
270 	struct winsize ws;
271 	speed_t ospeed;
272 
273 	if (ioctl(1, TIOCGWINSZ, (char *) &ws) < 0)
274 		ws.ws_col = ws.ws_row = 0;
275 	if (tcgetattr(1, &tbuf) < 0)
276 		ospeed = 9600;
277 	else
278 		ospeed = cfgetospeed(&tbuf);
279 	if (ospeed < B1200)
280 		screenheight = 9;
281 	else if (ospeed == B1200)
282 		screenheight = 14;
283 	else if (ws.ws_row != 0)
284 		screenheight = ws.ws_row;
285 	else
286 		screenheight = 24;
287 	if ((realscreenheight = ws.ws_row) == 0)
288 		realscreenheight = 24;
289 	if ((screenwidth = ws.ws_col) == 0)
290 		screenwidth = 80;
291 }
292 
293 __dead void
294 usage(void)
295 {
296 
297 	fprintf(stderr, "usage: %s [-iInv] [-s subject] [-c cc-addr] "
298 	    "[-b bcc-addr] to-addr ...\n", __progname);
299 	fprintf(stderr, "       %*s [- sendmail-options ...]\n",
300 	    (int)strlen(__progname), "");
301 	fprintf(stderr, "       %s [-iInNv] -f [name]\n", __progname);
302 	fprintf(stderr, "       %s [-iInNv] [-u user]\n", __progname);
303 	exit(1);
304 }
305