xref: /freebsd/usr.bin/mail/main.c (revision 99e8005137088aafb1350e23b113d69b01b0820f)
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.1 (Berkeley) 6/6/93";
43 #endif
44 static const char rcsid[] =
45   "$FreeBSD$";
46 #endif /* not lint */
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, *cp;
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 	replyto = NULL;
97 	while ((i = getopt(argc, argv, "INT:b:c:dfins:u:v")) != -1) {
98 		switch (i) {
99 		case 'T':
100 			/*
101 			 * Next argument is temp file to write which
102 			 * articles have been read/deleted for netnews.
103 			 */
104 			Tflag = optarg;
105 			if ((i = open(Tflag, O_CREAT | O_TRUNC | O_WRONLY,
106 			    0600)) < 0)
107 				err(1, "%s", Tflag);
108 			(void)close(i);
109 			break;
110 		case 'u':
111 			/*
112 			 * Next argument is person to pretend to be.
113 			 */
114 			myname = optarg;
115 			unsetenv("MAIL");
116 			break;
117 		case 'i':
118 			/*
119 			 * User wants to ignore interrupts.
120 			 * Set the variable "ignore"
121 			 */
122 			assign("ignore", "");
123 			break;
124 		case 'd':
125 			debug++;
126 			break;
127 		case 's':
128 			/*
129 			 * Give a subject field for sending from
130 			 * non terminal
131 			 */
132 			subject = optarg;
133 			break;
134 		case 'f':
135 			/*
136 			 * User is specifying file to "edit" with Mail,
137 			 * as opposed to reading system mailbox.
138 			 * If no argument is given after -f, we read his
139 			 * mbox file.
140 			 *
141 			 * getopt() can't handle optional arguments, so here
142 			 * is an ugly hack to get around it.
143 			 */
144 			if ((argv[optind] != NULL) && (argv[optind][0] != '-'))
145 				ef = argv[optind++];
146 			else
147 				ef = "&";
148 			break;
149 		case 'n':
150 			/*
151 			 * User doesn't want to source /usr/lib/Mail.rc
152 			 */
153 			nosrc++;
154 			break;
155 		case 'N':
156 			/*
157 			 * Avoid initial header printing.
158 			 */
159 			assign("noheader", "");
160 			break;
161 		case 'v':
162 			/*
163 			 * Send mailer verbose flag
164 			 */
165 			assign("verbose", "");
166 			break;
167 		case 'I':
168 			/*
169 			 * We're interactive
170 			 */
171 			assign("interactive", "");
172 			break;
173 		case 'c':
174 			/*
175 			 * Get Carbon Copy Recipient list
176 			 */
177 			cc = cat(cc, nalloc(optarg, GCC));
178 			break;
179 		case 'b':
180 			/*
181 			 * Get Blind Carbon Copy Recipient list
182 			 */
183 			bcc = cat(bcc, nalloc(optarg, GBCC));
184 			break;
185 		case '?':
186 			fprintf(stderr, "\
187 Usage: %s [-iInv] [-s subject] [-c cc-addr] [-b bcc-addr] to-addr ...\n\
188        %*s [- sendmail-options ...]\n\
189        %s [-iInNv] -f [name]\n\
190        %s [-iInNv] [-u user]\n",__progname, strlen(__progname), "", __progname,
191 				__progname);
192 			exit(1);
193 		}
194 	}
195 	for (i = optind; (argv[i] != NULL) && (*argv[i] != '-'); i++)
196 		to = cat(to, nalloc(argv[i], GTO));
197 	for (; argv[i] != NULL; i++)
198 		smopts = cat(smopts, nalloc(argv[i], 0));
199 	/*
200 	 * Check for inconsistent arguments.
201 	 */
202 	if (to == NULL && (subject != NULL || cc != NULL || bcc != NULL))
203 		errx(1, "You must specify direct recipients with -s, -c, or -b.");
204 	if (ef != NULL && to != NULL)
205 		errx(1, "Cannot give -f and people to send to.");
206 	tinit();
207 	setscreensize();
208 	input = stdin;
209 	rcvmode = !to;
210 	spreserve();
211 	if (!nosrc) {
212 		char *s, *path_rc;
213 
214 		if ((path_rc = malloc(sizeof(_PATH_MASTER_RC))) == NULL)
215 			err(1, "malloc(path_rc) failed");
216 
217 		strcpy(path_rc, _PATH_MASTER_RC);
218 		while ((s = strsep(&path_rc, ":")) != NULL)
219 			if (*s != '\0')
220 				load(s);
221 	}
222 	if ((cp = getenv("REPLYTO")) != NULL)
223 		replyto = cp;
224 
225 	/*
226 	 * Expand returns a savestr, but load only uses the file name
227 	 * for fopen, so it's safe to do this.
228 	 */
229 	load(expand("~/.mailrc"));
230 	if (!rcvmode) {
231 		mail(to, cc, bcc, smopts, subject, replyto);
232 		/*
233 		 * why wait?
234 		 */
235 		exit(senderr);
236 	}
237 	/*
238 	 * Ok, we are reading mail.
239 	 * Decide whether we are editing a mailbox or reading
240 	 * the system mailbox, and open up the right stuff.
241 	 */
242 	if (ef == NULL)
243 		ef = "%";
244 	if (setfile(ef) < 0)
245 		exit(1);		/* error already reported */
246 	if (setjmp(hdrjmp) == 0) {
247 		if ((prevint = signal(SIGINT, SIG_IGN)) != SIG_IGN)
248 			(void)signal(SIGINT, hdrstop);
249 		if (value("quiet") == NULL)
250 			printf("Mail version %s.  Type ? for help.\n",
251 				version);
252 		announce();
253 		(void)fflush(stdout);
254 		(void)signal(SIGINT, prevint);
255 	}
256 	commands();
257 	(void)signal(SIGHUP, SIG_IGN);
258 	(void)signal(SIGINT, SIG_IGN);
259 	(void)signal(SIGQUIT, SIG_IGN);
260 	quit();
261 	exit(0);
262 }
263 
264 /*
265  * Interrupt printing of the headers.
266  */
267 /*ARGSUSED*/
268 void
269 hdrstop(signo)
270 	int signo;
271 {
272 
273 	(void)fflush(stdout);
274 	fprintf(stderr, "\nInterrupt\n");
275 	longjmp(hdrjmp, 1);
276 }
277 
278 /*
279  * Compute what the screen size for printing headers should be.
280  * We use the following algorithm for the height:
281  *	If baud rate < 1200, use  9
282  *	If baud rate = 1200, use 14
283  *	If baud rate > 1200, use 24 or ws_row
284  * Width is either 80 or ws_col;
285  */
286 void
287 setscreensize()
288 {
289 	struct winsize ws;
290 	struct termios tio;
291 	speed_t speed = 0;
292 
293 	if (ioctl(1, TIOCGWINSZ, (char *)&ws) < 0)
294 		ws.ws_col = ws.ws_row = 0;
295 	if (tcgetattr(1, &tio) != -1)
296 		speed = cfgetospeed(&tio);
297 	if (speed <= 0)
298 		speed = B9600;
299 	if (speed < B1200)
300 		screenheight = 9;
301 	else if (speed == B1200)
302 		screenheight = 14;
303 	else if (ws.ws_row != 0)
304 		screenheight = ws.ws_row;
305 	else
306 		screenheight = 24;
307 	if ((realscreenheight = ws.ws_row) == 0)
308 		realscreenheight = 24;
309 	if ((screenwidth = ws.ws_col) == 0)
310 		screenwidth = 80;
311 }
312