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