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