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