| 1 | #ifndef lint |
| 2 | static char sccsid[] = "@(#)main.c 2.13 (Berkeley) %G%"; |
| 3 | #endif |
| 4 | |
| 5 | #include "rcv.h" |
| 6 | #include <sys/stat.h> |
| 7 | |
| 8 | /* |
| 9 | * Mail -- a mail program |
| 10 | * |
| 11 | * Startup -- interface with user. |
| 12 | */ |
| 13 | |
| 14 | jmp_buf hdrjmp; |
| 15 | |
| 16 | /* |
| 17 | * Find out who the user is, copy his mail file (if exists) into |
| 18 | * /tmp/Rxxxxx and set up the message pointers. Then, print out the |
| 19 | * message headers and read user commands. |
| 20 | * |
| 21 | * Command line syntax: |
| 22 | * Mail [ -i ] [ -r address ] [ -h number ] [ -f [ name ] ] |
| 23 | * or: |
| 24 | * Mail [ -i ] [ -r address ] [ -h number ] people ... |
| 25 | */ |
| 26 | |
| 27 | main(argc, argv) |
| 28 | char **argv; |
| 29 | { |
| 30 | register char *ef; |
| 31 | register int i, argp; |
| 32 | int mustsend, uflag, hdrstop(), (*prevint)(), f; |
| 33 | FILE *ibuf, *ftat; |
| 34 | struct sgttyb tbuf; |
| 35 | |
| 36 | #ifdef signal |
| 37 | Siginit(); |
| 38 | #endif |
| 39 | |
| 40 | /* |
| 41 | * Set up a reasonable environment. We clobber the last |
| 42 | * element of argument list for compatibility with version 6, |
| 43 | * figure out whether we are being run interactively, set up |
| 44 | * all the temporary files, buffer standard output, and so forth. |
| 45 | */ |
| 46 | |
| 47 | uflag = 0; |
| 48 | argv[argc] = (char *) -1; |
| 49 | #ifdef GETHOST |
| 50 | inithost(); |
| 51 | #endif GETHOST |
| 52 | mypid = getpid(); |
| 53 | intty = isatty(0); |
| 54 | outtty = isatty(1); |
| 55 | if (outtty) { |
| 56 | gtty(1, &tbuf); |
| 57 | baud = tbuf.sg_ospeed; |
| 58 | } |
| 59 | else |
| 60 | baud = B9600; |
| 61 | image = -1; |
| 62 | |
| 63 | /* |
| 64 | * Now, determine how we are being used. |
| 65 | * We successively pick off instances of -r, -h, -f, and -i. |
| 66 | * If called as "rmail" we note this fact for letter sending. |
| 67 | * If there is anything left, it is the base of the list |
| 68 | * of users to mail to. Argp will be set to point to the |
| 69 | * first of these users. |
| 70 | */ |
| 71 | |
| 72 | ef = NOSTR; |
| 73 | argp = -1; |
| 74 | mustsend = 0; |
| 75 | if (argc > 0 && **argv == 'r') |
| 76 | rmail++; |
| 77 | for (i = 1; i < argc; i++) { |
| 78 | |
| 79 | /* |
| 80 | * If current argument is not a flag, then the |
| 81 | * rest of the arguments must be recipients. |
| 82 | */ |
| 83 | |
| 84 | if (*argv[i] != '-') { |
| 85 | argp = i; |
| 86 | break; |
| 87 | } |
| 88 | switch (argv[i][1]) { |
| 89 | case 'r': |
| 90 | /* |
| 91 | * Next argument is address to be sent along |
| 92 | * to the mailer. |
| 93 | */ |
| 94 | if (i >= argc - 1) { |
| 95 | fprintf(stderr, "Address required after -r\n"); |
| 96 | exit(1); |
| 97 | } |
| 98 | mustsend++; |
| 99 | rflag = argv[i+1]; |
| 100 | i++; |
| 101 | break; |
| 102 | |
| 103 | case 'T': |
| 104 | /* |
| 105 | * Next argument is temp file to write which |
| 106 | * articles have been read/deleted for netnews. |
| 107 | */ |
| 108 | if (i >= argc - 1) { |
| 109 | fprintf(stderr, "Name required after -T\n"); |
| 110 | exit(1); |
| 111 | } |
| 112 | Tflag = argv[i+1]; |
| 113 | if ((f = creat(Tflag, 0600)) < 0) { |
| 114 | perror(Tflag); |
| 115 | exit(1); |
| 116 | } |
| 117 | close(f); |
| 118 | i++; |
| 119 | break; |
| 120 | |
| 121 | case 'u': |
| 122 | /* |
| 123 | * Next argument is person to pretend to be. |
| 124 | */ |
| 125 | uflag++; |
| 126 | if (i >= argc - 1) { |
| 127 | fprintf(stderr, "Missing user name for -u\n"); |
| 128 | exit(1); |
| 129 | } |
| 130 | strcpy(myname, argv[i+1]); |
| 131 | i++; |
| 132 | break; |
| 133 | |
| 134 | case 'i': |
| 135 | /* |
| 136 | * User wants to ignore interrupts. |
| 137 | * Set the variable "ignore" |
| 138 | */ |
| 139 | assign("ignore", ""); |
| 140 | break; |
| 141 | |
| 142 | case 'd': |
| 143 | debug++; |
| 144 | break; |
| 145 | |
| 146 | case 'h': |
| 147 | /* |
| 148 | * Specified sequence number for network. |
| 149 | * This is the number of "hops" made so |
| 150 | * far (count of times message has been |
| 151 | * forwarded) to help avoid infinite mail loops. |
| 152 | */ |
| 153 | if (i >= argc - 1) { |
| 154 | fprintf(stderr, "Number required for -h\n"); |
| 155 | exit(1); |
| 156 | } |
| 157 | mustsend++; |
| 158 | hflag = atoi(argv[i+1]); |
| 159 | if (hflag == 0) { |
| 160 | fprintf(stderr, "-h needs non-zero number\n"); |
| 161 | exit(1); |
| 162 | } |
| 163 | i++; |
| 164 | break; |
| 165 | |
| 166 | case 's': |
| 167 | /* |
| 168 | * Give a subject field for sending from |
| 169 | * non terminal |
| 170 | */ |
| 171 | if (i >= argc - 1) { |
| 172 | fprintf(stderr, "Subject req'd for -s\n"); |
| 173 | exit(1); |
| 174 | } |
| 175 | mustsend++; |
| 176 | sflag = argv[i+1]; |
| 177 | i++; |
| 178 | break; |
| 179 | |
| 180 | case 'f': |
| 181 | /* |
| 182 | * User is specifying file to "edit" with Mail, |
| 183 | * as opposed to reading system mailbox. |
| 184 | * If no argument is given after -f, we read his |
| 185 | * mbox file in his home directory. |
| 186 | */ |
| 187 | if (i >= argc - 1) |
| 188 | ef = mbox; |
| 189 | else |
| 190 | ef = argv[i + 1]; |
| 191 | i++; |
| 192 | break; |
| 193 | |
| 194 | case 'n': |
| 195 | /* |
| 196 | * User doesn't want to source /usr/lib/Mail.rc |
| 197 | */ |
| 198 | nosrc++; |
| 199 | break; |
| 200 | |
| 201 | case 'N': |
| 202 | /* |
| 203 | * Avoid initial header printing. |
| 204 | */ |
| 205 | noheader++; |
| 206 | break; |
| 207 | |
| 208 | case 'v': |
| 209 | /* |
| 210 | * Send mailer verbose flag |
| 211 | */ |
| 212 | assign("verbose", ""); |
| 213 | break; |
| 214 | |
| 215 | default: |
| 216 | fprintf(stderr, "Unknown flag: %s\n", argv[i]); |
| 217 | exit(1); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * Check for inconsistent arguments. |
| 223 | */ |
| 224 | |
| 225 | if (ef != NOSTR && argp != -1) { |
| 226 | fprintf(stderr, "Cannot give -f and people to send to.\n"); |
| 227 | exit(1); |
| 228 | } |
| 229 | if (mustsend && argp == -1) { |
| 230 | fprintf(stderr, "The flags you gave make no sense since you're not sending mail.\n"); |
| 231 | exit(1); |
| 232 | } |
| 233 | tinit(); |
| 234 | input = stdin; |
| 235 | rcvmode = argp == -1; |
| 236 | if (!nosrc) |
| 237 | load(MASTER); |
| 238 | load(mailrc); |
| 239 | if (argp != -1) { |
| 240 | mail(&argv[argp]); |
| 241 | |
| 242 | /* |
| 243 | * why wait? |
| 244 | */ |
| 245 | |
| 246 | exit(senderr); |
| 247 | } |
| 248 | |
| 249 | /* |
| 250 | * Ok, we are reading mail. |
| 251 | * Decide whether we are editing a mailbox or reading |
| 252 | * the system mailbox, and open up the right stuff. |
| 253 | */ |
| 254 | |
| 255 | if (ef != NOSTR) { |
| 256 | char *ename; |
| 257 | |
| 258 | edit++; |
| 259 | ename = expand(ef); |
| 260 | if (ename != ef) { |
| 261 | ef = (char *) calloc(1, strlen(ename) + 1); |
| 262 | strcpy(ef, ename); |
| 263 | } |
| 264 | editfile = ef; |
| 265 | strcpy(mailname, ef); |
| 266 | } |
| 267 | if (setfile(mailname, edit) < 0) { |
| 268 | if (edit) |
| 269 | perror(mailname); |
| 270 | else |
| 271 | fprintf(stderr, "No mail for %s\n", myname); |
| 272 | exit(1); |
| 273 | } |
| 274 | if (!edit && !noheader && value("noheader") == NOSTR) { |
| 275 | if (setjmp(hdrjmp) == 0) { |
| 276 | if ((prevint = sigset(SIGINT, SIG_IGN)) != SIG_IGN) |
| 277 | sigset(SIGINT, hdrstop); |
| 278 | announce(!0); |
| 279 | fflush(stdout); |
| 280 | sigset(SIGINT, prevint); |
| 281 | } |
| 282 | } |
| 283 | if (edit) |
| 284 | newfileinfo(); |
| 285 | if (!edit && msgCount == 0) { |
| 286 | printf("No mail\n"); |
| 287 | fflush(stdout); |
| 288 | exit(0); |
| 289 | } |
| 290 | commands(); |
| 291 | if (!edit) { |
| 292 | sigset(SIGHUP, SIG_IGN); |
| 293 | sigset(SIGINT, SIG_IGN); |
| 294 | sigset(SIGQUIT, SIG_IGN); |
| 295 | quit(); |
| 296 | } |
| 297 | exit(0); |
| 298 | } |
| 299 | |
| 300 | /* |
| 301 | * Interrupt printing of the headers. |
| 302 | */ |
| 303 | hdrstop() |
| 304 | { |
| 305 | |
| 306 | clrbuf(stdout); |
| 307 | printf("\nInterrupt\n"); |
| 308 | fflush(stdout); |
| 309 | longjmp(hdrjmp, 1); |
| 310 | } |