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