1 /* $OpenBSD: main.c,v 1.22 2008/10/09 06:48:11 jmc Exp $ */ 2 /* $NetBSD: main.c,v 1.7 1997/05/13 06:15:57 mikel Exp $ */ 3 4 /* 5 * Copyright (c) 1980, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #ifndef lint 34 static const char copyright[] = 35 "@(#) Copyright (c) 1980, 1993\n\ 36 The Regents of the University of California. All rights reserved.\n"; 37 #endif /* not lint */ 38 39 #ifndef lint 40 #if 0 41 static const char sccsid[] = "@(#)main.c 8.2 (Berkeley) 4/20/95"; 42 #else 43 static const char rcsid[] = "$OpenBSD: main.c,v 1.22 2008/10/09 06:48:11 jmc Exp $"; 44 #endif 45 #endif /* not lint */ 46 47 #include "rcv.h" 48 #include <fcntl.h> 49 #include <sys/ioctl.h> 50 #include "extern.h" 51 52 __dead void usage(void); 53 int main(int, char **); 54 55 /* 56 * Mail -- a mail program 57 * 58 * Startup -- interface with user. 59 */ 60 61 int 62 main(int argc, char **argv) 63 { 64 int i; 65 struct name *to, *cc, *bcc, *smopts; 66 char *subject; 67 char *ef; 68 char nosrc = 0; 69 char *rc; 70 extern const char version[]; 71 72 /* 73 * Set up a reasonable environment. 74 * Figure out whether we are being run interactively, 75 * start the SIGCHLD catcher, and so forth. 76 */ 77 (void)signal(SIGCHLD, sigchild); 78 (void)signal(SIGPIPE, SIG_IGN); 79 if (isatty(0)) 80 assign("interactive", ""); 81 image = -1; 82 /* 83 * Now, determine how we are being used. 84 * We successively pick off - flags. 85 * If there is anything left, it is the base of the list 86 * of users to mail to. Argp will be set to point to the 87 * first of these users. 88 */ 89 ef = NULL; 90 to = NULL; 91 cc = NULL; 92 bcc = NULL; 93 smopts = NULL; 94 subject = NULL; 95 while ((i = getopt(argc, argv, "EINT:b:c:dfins:u:v")) != -1) { 96 switch (i) { 97 case 'T': 98 /* 99 * Next argument is temp file to write which 100 * articles have been read/deleted for netnews. 101 */ 102 Tflag = optarg; 103 if ((i = creat(Tflag, 0600)) < 0) 104 err(1, "%s", Tflag); 105 (void)close(i); 106 break; 107 case 'u': 108 /* 109 * Next argument is person to pretend to be. 110 */ 111 if (strlen(optarg) >= MAXLOGNAME) 112 errx(1, "username `%s' too long", optarg); 113 unsetenv("MAIL"); 114 myname = optarg; 115 uflag = 1; 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 'E': 186 /* 187 * Don't send messages with an empty body. 188 */ 189 assign("skipempty", ""); 190 break; 191 default: 192 usage(); 193 /*NOTREACHED*/ 194 } 195 } 196 for (i = optind; (argv[i]) && (*argv[i] != '-'); i++) 197 to = cat(to, nalloc(argv[i], GTO)); 198 for (; argv[i]; i++) 199 smopts = cat(smopts, nalloc(argv[i], 0)); 200 /* 201 * Check for inconsistent arguments. 202 */ 203 if (to == NULL && (subject != NULL || cc != NULL || bcc != NULL)) 204 errx(1, "You must specify direct recipients with -s, -c, or -b"); 205 if (ef != NULL && to != NULL) 206 errx(1, "Cannot give -f and people to send to"); 207 /* 208 * Block SIGINT except where we install an explicit handler for it. 209 */ 210 sigemptyset(&intset); 211 sigaddset(&intset, SIGINT); 212 (void)sigprocmask(SIG_BLOCK, &intset, NULL); 213 /* 214 * Initialization. 215 */ 216 tinit(); 217 setscreensize(); 218 input = stdin; 219 rcvmode = !to; 220 spreserve(); 221 if (!nosrc) 222 load(_PATH_MASTER_RC); 223 /* 224 * Expand returns a savestr, but load only uses the file name 225 * for fopen, so it's safe to do this. 226 */ 227 if ((rc = getenv("MAILRC")) == 0) 228 rc = "~/.mailrc"; 229 load(expand(rc)); 230 if (!rcvmode) { 231 mail(to, cc, bcc, smopts, subject); 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 247 if (value("quiet") == NULL) 248 (void)printf("Mail version %s. Type ? for help.\n", 249 version); 250 announce(); 251 (void)fflush(stdout); 252 commands(); 253 (void)ignoresig(SIGHUP, NULL, NULL); 254 (void)ignoresig(SIGINT, NULL, NULL); 255 (void)ignoresig(SIGQUIT, NULL, NULL); 256 quit(); 257 exit(0); 258 } 259 260 /* 261 * Compute what the screen size for printing headers should be. 262 * We use the following algorithm for the height: 263 * If baud rate < 1200, use 9 264 * If baud rate = 1200, use 14 265 * If baud rate > 1200, use 24 or ws_row 266 * Width is either 80 or ws_col; 267 */ 268 void 269 setscreensize(void) 270 { 271 struct termios tbuf; 272 struct winsize ws; 273 speed_t ospeed; 274 275 if (ioctl(1, TIOCGWINSZ, (char *) &ws) < 0) 276 ws.ws_col = ws.ws_row = 0; 277 if (tcgetattr(1, &tbuf) < 0) 278 ospeed = 9600; 279 else 280 ospeed = cfgetospeed(&tbuf); 281 if (ospeed < B1200) 282 screenheight = 9; 283 else if (ospeed == B1200) 284 screenheight = 14; 285 else if (ws.ws_row != 0) 286 screenheight = ws.ws_row; 287 else 288 screenheight = 24; 289 if ((realscreenheight = ws.ws_row) == 0) 290 realscreenheight = 24; 291 if ((screenwidth = ws.ws_col) == 0) 292 screenwidth = 80; 293 } 294 295 __dead void 296 usage(void) 297 { 298 299 fprintf(stderr, "usage: %s [-dEIinv] [-b list] [-c list] " 300 "[-s subject] to-addr ...\n", __progname); 301 fprintf(stderr, " %*s [-sendmail-options ...]\n", 302 (int)strlen(__progname), ""); 303 fprintf(stderr, " %s [-dEIiNnv] -f [file]\n", __progname); 304 fprintf(stderr, " %s [-dEIiNnv] [-u user]\n", __progname); 305 exit(1); 306 } 307