1 # 2 3 #include "rcv.h" 4 #include <sys/stat.h> 5 6 /* 7 * Mail -- a mail program 8 * 9 * Startup -- interface with user. 10 */ 11 12 static char *SccsId = "@(#)main.c 2.1 07/01/81"; 13 14 /* 15 * Find out who the user is, copy his mail file (if exists) into 16 * /tmp/Rxxxxx and set up the message pointers. Then, print out the 17 * message headers and read user commands. 18 * 19 * Command line syntax: 20 * Mail [ -i ] [ -r address ] [ -h number ] [ -f [ name ] ] 21 * or: 22 * Mail [ -i ] [ -r address ] [ -h number ] people ... 23 */ 24 25 main(argc, argv) 26 char **argv; 27 { 28 register char *ef; 29 register int i, argp; 30 int mustsend, uflag; 31 FILE *ibuf, *ftat; 32 extern char _sobuf[]; 33 34 #ifdef signal 35 Siginit(); 36 #endif 37 38 /* 39 * Set up a reasonable environment. We clobber the last 40 * element of argument list for compatibility with version 6, 41 * figure out whether we are being run interactively, set up 42 * all the temporary files, buffer standard output, and so forth. 43 */ 44 45 uflag = 0; 46 argv[argc] = (char *) -1; 47 mypid = getpid(); 48 intty = isatty(0); 49 outtty = isatty(1); 50 image = -1; 51 setbuf(stdout, _sobuf); 52 53 /* 54 * Now, determine how we are being used. 55 * We successively pick off instances of -r, -h, -f, and -i. 56 * If called as "rmail" we note this fact for letter sending. 57 * If there is anything left, it is the base of the list 58 * of users to mail to. Argp will be set to point to the 59 * first of these users. 60 */ 61 62 ef = NOSTR; 63 argp = -1; 64 mustsend = 0; 65 if (argc > 0 && **argv == 'r') 66 rmail++; 67 for (i = 1; i < argc; i++) { 68 69 /* 70 * If current argument is not a flag, then the 71 * rest of the arguments must be recipients. 72 */ 73 74 if (*argv[i] != '-') { 75 argp = i; 76 break; 77 } 78 switch (argv[i][1]) { 79 case 'r': 80 /* 81 * Next argument is address to be sent along 82 * to the mailer. 83 */ 84 if (i >= argc - 1) { 85 fprintf(stderr, "Address required after -r\n"); 86 exit(1); 87 } 88 mustsend++; 89 rflag = argv[i+1]; 90 i++; 91 break; 92 93 case 'u': 94 /* 95 * Next argument is person to pretend to be. 96 */ 97 uflag++; 98 if (i >= argc - 1) { 99 fprintf(stderr, "You obviously dont know what you're doing\n"); 100 exit(1); 101 } 102 strcpy(myname, argv[i+1]); 103 i++; 104 break; 105 106 case 'i': 107 /* 108 * User wants to ignore interrupts. 109 * Set the variable "ignore" 110 */ 111 assign("ignore", ""); 112 break; 113 114 case 'd': 115 debug++; 116 break; 117 118 case 'h': 119 /* 120 * Specified sequence number for network. 121 * This is the number of "hops" made so 122 * far (count of times message has been 123 * forwarded) to help avoid infinite mail loops. 124 */ 125 if (i >= argc - 1) { 126 fprintf(stderr, "Number required for -h\n"); 127 exit(1); 128 } 129 mustsend++; 130 hflag = atoi(argv[i+1]); 131 if (hflag == 0) { 132 fprintf(stderr, "-h needs non-zero number\n"); 133 exit(1); 134 } 135 i++; 136 break; 137 138 case 's': 139 /* 140 * Give a subject field for sending from 141 * non terminal 142 */ 143 if (i >= argc - 1) { 144 fprintf(stderr, "Subject req'd for -s\n"); 145 exit(1); 146 } 147 mustsend++; 148 sflag = argv[i+1]; 149 i++; 150 break; 151 152 case 'f': 153 /* 154 * User is specifying file to "edit" with Mail, 155 * as opposed to reading system mailbox. 156 * If no argument is given after -f, we read his 157 * mbox file in his home directory. 158 */ 159 if (i >= argc - 1) 160 ef = mbox; 161 else 162 ef = argv[i + 1]; 163 i++; 164 break; 165 166 case 'n': 167 /* 168 * User doesn't want to source /usr/lib/Mail.rc 169 */ 170 nosrc++; 171 break; 172 173 case 'N': 174 /* 175 * Avoid initial header printing. 176 */ 177 noheader++; 178 break; 179 180 default: 181 fprintf(stderr, "Unknown flag: %s\n", argv[i]); 182 exit(1); 183 } 184 } 185 186 /* 187 * Check for inconsistent arguments. 188 */ 189 190 if (rflag != NOSTR && strcmp(rflag, "daemon") == 0) { 191 ftat = fopen("/crp/kas/gotcha", "a"); 192 if (ftat != NULL) { 193 fprintf(ftat, "user daemon, real uid %d\n", getuid()); 194 fclose(ftat); 195 } 196 } 197 if (ef != NOSTR && argp != -1) { 198 fprintf(stderr, "Cannot give -f and people to send to.\n"); 199 exit(1); 200 } 201 if (mustsend && argp == -1) { 202 fprintf(stderr, "The flags you gave make no sense since you're not sending mail.\n"); 203 exit(1); 204 } 205 tinit(); 206 if (argp != -1) { 207 commands(); 208 mail(&argv[argp]); 209 210 /* 211 * why wait? 212 */ 213 214 exit(senderr); 215 } 216 217 /* 218 * Ok, we are reading mail. 219 * Decide whether we are editing a mailbox or reading 220 * the system mailbox, and open up the right stuff. 221 */ 222 223 rcvmode++; 224 if (ef != NOSTR) { 225 edit++; 226 editfile = mailname = ef; 227 } 228 if (setfile(mailname, edit) < 0) 229 exit(1); 230 commands(); 231 if (!edit) { 232 sigset(SIGHUP, SIG_IGN); 233 sigset(SIGINT, SIG_IGN); 234 sigset(SIGQUIT, SIG_IGN); 235 quit(); 236 } 237 exit(0); 238 } 239