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