xref: /freebsd/usr.bin/mail/main.c (revision 1a2cdef4962b47be5057809ce730a733b7f3c27c)
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 int
61 main(argc, argv)
62 	int argc;
63 	char *argv[];
64 {
65 	register int i;
66 	struct name *to, *cc, *bcc, *smopts;
67 	char *subject, *replyto;
68 	char *ef, *cp;
69 	char nosrc = 0;
70 	void hdrstop();
71 	sig_t prevint;
72 	void sigchild();
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 = NOSTR;
91 	to = NIL;
92 	cc = NIL;
93 	bcc = NIL;
94 	smopts = NIL;
95 	subject = NOSTR;
96 	replyto = NOSTR;
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 			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]) && (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]) && (*argv[i] != '-'); i++)
196 		to = cat(to, nalloc(argv[i], GTO));
197 	for (; argv[i]; i++)
198 		smopts = cat(smopts, nalloc(argv[i], 0));
199 	/*
200 	 * Check for inconsistent arguments.
201 	 */
202 	if (to == NIL && (subject != NOSTR || cc != NIL || bcc != NIL))
203 		errx(1, "You must specify direct recipients with -s, -c, or -b.");
204 	if (ef != NOSTR && to != NIL)
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 == NOSTR)
243 		ef = "%";
244 	if (setfile(ef) < 0)
245 		exit(1);		/* error already reported */
246 	if (setjmp(hdrjmp) == 0) {
247 		extern char *version;
248 
249 		if ((prevint = signal(SIGINT, SIG_IGN)) != SIG_IGN)
250 			signal(SIGINT, hdrstop);
251 		if (value("quiet") == NOSTR)
252 			printf("Mail version %s.  Type ? for help.\n",
253 				version);
254 		announce();
255 		fflush(stdout);
256 		signal(SIGINT, prevint);
257 	}
258 	commands();
259 	signal(SIGHUP, SIG_IGN);
260 	signal(SIGINT, SIG_IGN);
261 	signal(SIGQUIT, SIG_IGN);
262 	quit();
263 	exit(0);
264 }
265 
266 /*
267  * Interrupt printing of the headers.
268  */
269 void
270 hdrstop(signo)
271 	int signo;
272 {
273 
274 	fflush(stdout);
275 	fprintf(stderr, "\nInterrupt\n");
276 	longjmp(hdrjmp, 1);
277 }
278 
279 /*
280  * Compute what the screen size for printing headers should be.
281  * We use the following algorithm for the height:
282  *	If baud rate < 1200, use  9
283  *	If baud rate = 1200, use 14
284  *	If baud rate > 1200, use 24 or ws_row
285  * Width is either 80 or ws_col;
286  */
287 void
288 setscreensize()
289 {
290 	struct winsize ws;
291 	struct termios tio;
292 	speed_t speed = 0;
293 
294 	if (ioctl(1, TIOCGWINSZ, (char *) &ws) < 0)
295 		ws.ws_col = ws.ws_row = 0;
296 	if (tcgetattr(1, &tio) != -1)
297 		speed = cfgetospeed(&tio);
298 	if (speed <= 0)
299 		speed = B9600;
300 	if (speed < B1200)
301 		screenheight = 9;
302 	else if (speed == B1200)
303 		screenheight = 14;
304 	else if (ws.ws_row != 0)
305 		screenheight = ws.ws_row;
306 	else
307 		screenheight = 24;
308 	if ((realscreenheight = ws.ws_row) == 0)
309 		realscreenheight = 24;
310 	if ((screenwidth = ws.ws_col) == 0)
311 		screenwidth = 80;
312 }
313