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 const 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[] = "@(#)msgs.c 8.2 (Berkeley) 4/28/95"; 43 #endif 44 static const char rcsid[] = 45 "$Id: msgs.c,v 1.12 1998/07/09 14:06:54 ghelmer Exp $"; 46 #endif /* not lint */ 47 48 /* 49 * msgs - a user bulletin board program 50 * 51 * usage: 52 * msgs [fhlopq] [[-]number] to read messages 53 * msgs -s to place messages 54 * msgs -c [-days] to clean up the bulletin board 55 * 56 * prompt commands are: 57 * y print message 58 * n flush message, go to next message 59 * q flush message, quit 60 * p print message, turn on 'pipe thru more' mode 61 * P print message, turn off 'pipe thru more' mode 62 * - reprint last message 63 * s[-][<num>] [<filename>] save message 64 * m[-][<num>] mail with message in temp mbox 65 * x exit without flushing this message 66 * <num> print message number <num> 67 */ 68 69 #define V7 /* will look for TERM in the environment */ 70 #define OBJECT /* will object to messages without Subjects */ 71 /* #define REJECT */ /* will reject messages without Subjects 72 (OBJECT must be defined also) */ 73 /* #define UNBUFFERED *//* use unbuffered output */ 74 75 #include <sys/param.h> 76 #include <sys/stat.h> 77 #include <ctype.h> 78 #include <dirent.h> 79 #include <err.h> 80 #include <errno.h> 81 #include <fcntl.h> 82 #include <locale.h> 83 #include <pwd.h> 84 #include <setjmp.h> 85 #include <termcap.h> 86 #include <termios.h> 87 #include <signal.h> 88 #include <stdio.h> 89 #include <stdlib.h> 90 #include <string.h> 91 #include <time.h> 92 #include <unistd.h> 93 #include "pathnames.h" 94 95 #define CMODE 0644 /* bounds file creation mode */ 96 #define NO 0 97 #define YES 1 98 #define SUPERUSER 0 /* superuser uid */ 99 #define DAEMON 1 /* daemon uid */ 100 #define NLINES 24 /* default number of lines/crt screen */ 101 #define NDAYS 21 /* default keep time for messages */ 102 #define DAYS *24*60*60 /* seconds/day */ 103 #define MSGSRC ".msgsrc" /* user's rc file */ 104 #define BOUNDS "bounds" /* message bounds file */ 105 #define NEXT "Next message? [yq]" 106 #define MORE "More? [ynq]" 107 #define NOMORE "(No more) [q] ?" 108 109 typedef char bool; 110 111 FILE *msgsrc; 112 FILE *newmsg; 113 char *sep = "-"; 114 char inbuf[BUFSIZ]; 115 char fname[MAXPATHLEN]; 116 char cmdbuf[MAXPATHLEN + MAXPATHLEN]; 117 char subj[128]; 118 char from[128]; 119 char date[128]; 120 char *ptr; 121 char *in; 122 bool local; 123 bool ruptible; 124 bool totty; 125 bool seenfrom; 126 bool seensubj; 127 bool blankline; 128 bool printing = NO; 129 bool mailing = NO; 130 bool quitit = NO; 131 bool sending = NO; 132 bool intrpflg = NO; 133 int uid; 134 int msg; 135 int prevmsg; 136 int lct; 137 int nlines; 138 int Lpp = 0; 139 time_t t; 140 time_t keep; 141 142 /* option initialization */ 143 bool hdrs = NO; 144 bool qopt = NO; 145 bool hush = NO; 146 bool send_msg = NO; 147 bool locomode = NO; 148 bool use_pager = NO; 149 bool clean = NO; 150 bool lastcmd = NO; 151 jmp_buf tstpbuf; 152 153 154 void ask __P((char *)); 155 void gfrsub __P((FILE *)); 156 int linecnt __P((FILE *)); 157 int next __P((char *)); 158 char *nxtfld __P((unsigned char *)); 159 void onsusp __P((int)); 160 void onintr __P((int)); 161 void prmesg __P((int)); 162 static void usage __P((void)); 163 164 int 165 main(argc, argv) 166 int argc; char *argv[]; 167 { 168 bool newrc, already; 169 int rcfirst = 0; /* first message to print (from .rc) */ 170 int rcback = 0; /* amount to back off of rcfirst */ 171 int firstmsg = 0, nextmsg = 0, lastmsg = 0; 172 int blast = 0; 173 struct stat buf; /* stat to check access of bounds */ 174 FILE *bounds; 175 176 #ifdef UNBUFFERED 177 setbuf(stdout, NULL); 178 #endif 179 setlocale(LC_ALL, ""); 180 181 time(&t); 182 setuid(uid = getuid()); 183 ruptible = (signal(SIGINT, SIG_IGN) == SIG_DFL); 184 if (ruptible) 185 signal(SIGINT, SIG_DFL); 186 187 argc--, argv++; 188 while (argc > 0) { 189 if (isdigit(argv[0][0])) { /* starting message # */ 190 rcfirst = atoi(argv[0]); 191 } 192 else if (isdigit(argv[0][1])) { /* backward offset */ 193 rcback = atoi( &( argv[0][1] ) ); 194 } 195 else { 196 ptr = *argv; 197 while (*ptr) switch (*ptr++) { 198 199 case '-': 200 break; 201 202 case 'c': 203 if (uid != SUPERUSER && uid != DAEMON) { 204 fprintf(stderr, "Sorry\n"); 205 exit(1); 206 } 207 clean = YES; 208 break; 209 210 case 'f': /* silently */ 211 hush = YES; 212 break; 213 214 case 'h': /* headers only */ 215 hdrs = YES; 216 break; 217 218 case 'l': /* local msgs only */ 219 locomode = YES; 220 break; 221 222 case 'o': /* option to save last message */ 223 lastcmd = YES; 224 break; 225 226 case 'p': /* pipe thru 'more' during long msgs */ 227 use_pager = YES; 228 break; 229 230 case 'q': /* query only */ 231 qopt = YES; 232 break; 233 234 case 's': /* sending TO msgs */ 235 send_msg = YES; 236 break; 237 238 default: 239 usage(); 240 } 241 } 242 argc--, argv++; 243 } 244 245 /* 246 * determine current message bounds 247 */ 248 snprintf(fname, sizeof(fname), "%s/%s", _PATH_MSGS, BOUNDS); 249 if (stat(fname, &buf) < 0) 250 err(errno, "%s", fname); 251 bounds = fopen(fname, "r"); 252 253 if (bounds != NULL) { 254 fscanf(bounds, "%d %d\n", &firstmsg, &lastmsg); 255 fclose(bounds); 256 blast = lastmsg; /* save upper bound */ 257 } 258 259 if (clean) 260 keep = t - (rcback? rcback : NDAYS) DAYS; 261 262 if (clean || bounds == NULL) { /* relocate message bounds */ 263 struct dirent *dp; 264 struct stat stbuf; 265 bool seenany = NO; 266 DIR *dirp; 267 268 dirp = opendir(_PATH_MSGS); 269 if (dirp == NULL) 270 err(errno, "%s", _PATH_MSGS); 271 272 firstmsg = 32767; 273 lastmsg = 0; 274 275 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)){ 276 register char *cp = dp->d_name; 277 register int i = 0; 278 279 if (dp->d_ino == 0) 280 continue; 281 if (dp->d_namlen == 0) 282 continue; 283 284 if (clean) 285 snprintf(inbuf, sizeof(inbuf), "%s/%s", _PATH_MSGS, cp); 286 287 while (isdigit(*cp)) 288 i = i * 10 + *cp++ - '0'; 289 if (*cp) 290 continue; /* not a message! */ 291 292 if (clean) { 293 if (stat(inbuf, &stbuf) != 0) 294 continue; 295 if (stbuf.st_mtime < keep 296 && stbuf.st_mode&S_IWRITE) { 297 unlink(inbuf); 298 continue; 299 } 300 } 301 302 if (i > lastmsg) 303 lastmsg = i; 304 if (i < firstmsg) 305 firstmsg = i; 306 seenany = YES; 307 } 308 closedir(dirp); 309 310 if (!seenany) { 311 if (blast != 0) /* never lower the upper bound! */ 312 lastmsg = blast; 313 firstmsg = lastmsg + 1; 314 } 315 else if (blast > lastmsg) 316 lastmsg = blast; 317 318 if (!send_msg) { 319 bounds = fopen(fname, "w"); 320 if (bounds == NULL) 321 err(errno, "%s", fname); 322 chmod(fname, CMODE); 323 fprintf(bounds, "%d %d\n", firstmsg, lastmsg); 324 fclose(bounds); 325 } 326 } 327 328 if (send_msg) { 329 /* 330 * Send mode - place msgs in _PATH_MSGS 331 */ 332 bounds = fopen(fname, "w"); 333 if (bounds == NULL) 334 err(errno, "%s", fname); 335 336 nextmsg = lastmsg + 1; 337 snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, nextmsg); 338 newmsg = fopen(fname, "w"); 339 if (newmsg == NULL) 340 err(errno, "%s", fname); 341 chmod(fname, CMODE); 342 343 fprintf(bounds, "%d %d\n", firstmsg, nextmsg); 344 fclose(bounds); 345 346 sending = YES; 347 if (ruptible) 348 signal(SIGINT, onintr); 349 350 if (isatty(fileno(stdin))) { 351 ptr = getpwuid(uid)->pw_name; 352 printf("Message %d:\nFrom %s %sSubject: ", 353 nextmsg, ptr, ctime(&t)); 354 fflush(stdout); 355 fgets(inbuf, sizeof inbuf, stdin); 356 putchar('\n'); 357 fflush(stdout); 358 fprintf(newmsg, "From %s %sSubject: %s\n", 359 ptr, ctime(&t), inbuf); 360 blankline = seensubj = YES; 361 } 362 else 363 blankline = seensubj = NO; 364 for (;;) { 365 fgets(inbuf, sizeof inbuf, stdin); 366 if (feof(stdin) || ferror(stdin)) 367 break; 368 blankline = (blankline || (inbuf[0] == '\n')); 369 seensubj = (seensubj || (!blankline && (strncmp(inbuf, "Subj", 4) == 0))); 370 fputs(inbuf, newmsg); 371 } 372 #ifdef OBJECT 373 if (!seensubj) { 374 printf("NOTICE: Messages should have a Subject field!\n"); 375 #ifdef REJECT 376 unlink(fname); 377 #endif 378 exit(1); 379 } 380 #endif 381 exit(ferror(stdin)); 382 } 383 if (clean) 384 exit(0); 385 386 /* 387 * prepare to display messages 388 */ 389 totty = (isatty(fileno(stdout)) != 0); 390 use_pager = use_pager && totty; 391 392 snprintf(fname, sizeof(fname), "%s/%s", getenv("HOME"), MSGSRC); 393 msgsrc = fopen(fname, "r"); 394 if (msgsrc) { 395 newrc = NO; 396 fscanf(msgsrc, "%d\n", &nextmsg); 397 fclose(msgsrc); 398 if (nextmsg > lastmsg+1) { 399 printf("Warning: bounds have been reset (%d, %d)\n", 400 firstmsg, lastmsg); 401 truncate(fname, (off_t)0); 402 newrc = YES; 403 } 404 else if (!rcfirst) 405 rcfirst = nextmsg - rcback; 406 } 407 else 408 newrc = YES; 409 msgsrc = fopen(fname, "r+"); 410 if (msgsrc == NULL) 411 msgsrc = fopen(fname, "w"); 412 if (msgsrc == NULL) 413 err(errno, "%s", fname); 414 if (rcfirst) { 415 if (rcfirst > lastmsg+1) { 416 printf("Warning: the last message is number %d.\n", 417 lastmsg); 418 rcfirst = nextmsg; 419 } 420 if (rcfirst > firstmsg) 421 firstmsg = rcfirst; /* don't set below first msg */ 422 } 423 if (newrc) { 424 nextmsg = firstmsg; 425 fseek(msgsrc, 0L, 0); 426 fprintf(msgsrc, "%d\n", nextmsg); 427 fflush(msgsrc); 428 } 429 430 #ifdef V7 431 if (totty) { 432 struct winsize win; 433 if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1) 434 Lpp = win.ws_row; 435 if (Lpp <= 0) { 436 if (tgetent(inbuf, getenv("TERM")) <= 0 437 || (Lpp = tgetnum("li")) <= 0) { 438 Lpp = NLINES; 439 } 440 } 441 } 442 #endif 443 Lpp -= 6; /* for headers, etc. */ 444 445 already = NO; 446 prevmsg = firstmsg; 447 printing = YES; 448 if (ruptible) 449 signal(SIGINT, onintr); 450 451 /* 452 * Main program loop 453 */ 454 for (msg = firstmsg; msg <= lastmsg; msg++) { 455 456 snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, msg); 457 newmsg = fopen(fname, "r"); 458 if (newmsg == NULL) 459 continue; 460 461 gfrsub(newmsg); /* get From and Subject fields */ 462 if (locomode && !local) { 463 fclose(newmsg); 464 continue; 465 } 466 467 if (qopt) { /* This has to be located here */ 468 printf("There are new messages.\n"); 469 exit(0); 470 } 471 472 if (already && !hdrs) 473 putchar('\n'); 474 475 /* 476 * Print header 477 */ 478 if (totty) 479 signal(SIGTSTP, onsusp); 480 (void) setjmp(tstpbuf); 481 already = YES; 482 nlines = 2; 483 if (seenfrom) { 484 printf("Message %d:\nFrom %s %s", msg, from, date); 485 nlines++; 486 } 487 if (seensubj) { 488 printf("Subject: %s", subj); 489 nlines++; 490 } 491 else { 492 if (seenfrom) { 493 putchar('\n'); 494 nlines++; 495 } 496 while (nlines < 6 497 && fgets(inbuf, sizeof inbuf, newmsg) 498 && inbuf[0] != '\n') { 499 fputs(inbuf, stdout); 500 nlines++; 501 } 502 } 503 504 lct = linecnt(newmsg); 505 if (lct) 506 printf("(%d%slines) ", lct, seensubj? " " : " more "); 507 508 if (hdrs) { 509 printf("\n-----\n"); 510 fclose(newmsg); 511 continue; 512 } 513 514 /* 515 * Ask user for command 516 */ 517 if (totty) 518 ask(lct? MORE : (msg==lastmsg? NOMORE : NEXT)); 519 else 520 inbuf[0] = 'y'; 521 if (totty) 522 signal(SIGTSTP, SIG_DFL); 523 cmnd: 524 in = inbuf; 525 switch (*in) { 526 case 'x': 527 case 'X': 528 exit(0); 529 530 case 'q': 531 case 'Q': 532 quitit = YES; 533 printf("--Postponed--\n"); 534 exit(0); 535 /* intentional fall-thru */ 536 case 'n': 537 case 'N': 538 if (msg >= nextmsg) sep = "Flushed"; 539 prevmsg = msg; 540 break; 541 542 case 'p': 543 case 'P': 544 use_pager = (*in++ == 'p'); 545 /* intentional fallthru */ 546 case '\n': 547 case 'y': 548 default: 549 if (*in == '-') { 550 msg = prevmsg-1; 551 sep = "replay"; 552 break; 553 } 554 if (isdigit(*in)) { 555 msg = next(in); 556 sep = in; 557 break; 558 } 559 560 prmesg(nlines + lct + (seensubj? 1 : 0)); 561 prevmsg = msg; 562 563 } 564 565 printf("--%s--\n", sep); 566 sep = "-"; 567 if (msg >= nextmsg) { 568 nextmsg = msg + 1; 569 fseek(msgsrc, 0L, 0); 570 fprintf(msgsrc, "%d\n", nextmsg); 571 fflush(msgsrc); 572 } 573 if (newmsg) 574 fclose(newmsg); 575 if (quitit) 576 break; 577 } 578 579 /* 580 * Make sure .rc file gets updated 581 */ 582 if (--msg >= nextmsg) { 583 nextmsg = msg + 1; 584 fseek(msgsrc, 0L, 0); 585 fprintf(msgsrc, "%d\n", nextmsg); 586 fflush(msgsrc); 587 } 588 if (already && !quitit && lastcmd && totty) { 589 /* 590 * save or reply to last message? 591 */ 592 msg = prevmsg; 593 ask(NOMORE); 594 if (inbuf[0] == '-' || isdigit(inbuf[0])) 595 goto cmnd; 596 } 597 if (!(already || hush || qopt)) 598 printf("No new messages.\n"); 599 exit(0); 600 } 601 602 static void 603 usage() 604 { 605 fprintf(stderr, "usage: msgs [fhlopq] [[-]number]\n"); 606 exit(1); 607 } 608 609 void 610 prmesg(length) 611 int length; 612 { 613 FILE *outf; 614 char *env_pager; 615 616 if (use_pager && length > Lpp) { 617 signal(SIGPIPE, SIG_IGN); 618 signal(SIGQUIT, SIG_IGN); 619 if ((env_pager = getenv("PAGER")) == NULL) { 620 snprintf(cmdbuf, sizeof(cmdbuf), _PATH_PAGER, Lpp); 621 } else { 622 snprintf(cmdbuf, sizeof(cmdbuf), env_pager); 623 } 624 outf = popen(cmdbuf, "w"); 625 if (!outf) 626 outf = stdout; 627 else 628 setbuf(outf, (char *)NULL); 629 } 630 else 631 outf = stdout; 632 633 if (seensubj) 634 putc('\n', outf); 635 636 while (fgets(inbuf, sizeof inbuf, newmsg)) { 637 fputs(inbuf, outf); 638 if (ferror(outf)) { 639 clearerr(outf); 640 break; 641 } 642 } 643 644 if (outf != stdout) { 645 pclose(outf); 646 signal(SIGPIPE, SIG_DFL); 647 signal(SIGQUIT, SIG_DFL); 648 } 649 else { 650 fflush(stdout); 651 } 652 653 /* force wait on output */ 654 tcdrain(fileno(stdout)); 655 } 656 657 void 658 onintr(unused) 659 int unused; 660 { 661 signal(SIGINT, onintr); 662 if (mailing) 663 unlink(fname); 664 if (sending) { 665 unlink(fname); 666 puts("--Killed--"); 667 exit(1); 668 } 669 if (printing) { 670 putchar('\n'); 671 if (hdrs) 672 exit(0); 673 sep = "Interrupt"; 674 if (newmsg) 675 fseek(newmsg, 0L, 2); 676 intrpflg = YES; 677 } 678 } 679 680 /* 681 * We have just gotten a susp. Suspend and prepare to resume. 682 */ 683 void 684 onsusp(unused) 685 int unused; 686 { 687 signal(SIGTSTP, SIG_DFL); 688 sigsetmask(0); 689 kill(0, SIGTSTP); 690 signal(SIGTSTP, onsusp); 691 if (!mailing) 692 longjmp(tstpbuf, 0); 693 } 694 695 int 696 linecnt(f) 697 FILE *f; 698 { 699 off_t oldpos = ftell(f); 700 int l = 0; 701 char lbuf[BUFSIZ]; 702 703 while (fgets(lbuf, sizeof lbuf, f)) 704 l++; 705 clearerr(f); 706 fseek(f, oldpos, 0); 707 return (l); 708 } 709 710 int 711 next(buf) 712 char *buf; 713 { 714 int i; 715 sscanf(buf, "%d", &i); 716 sprintf(buf, "Goto %d", i); 717 return(--i); 718 } 719 720 void 721 ask(prompt) 722 char *prompt; 723 { 724 char inch; 725 int n, cmsg, fd; 726 off_t oldpos; 727 FILE *cpfrom, *cpto; 728 729 printf("%s ", prompt); 730 fflush(stdout); 731 intrpflg = NO; 732 (void) fgets(inbuf, sizeof inbuf, stdin); 733 if ((n = strlen(inbuf)) > 0 && inbuf[n - 1] == '\n') 734 inbuf[n - 1] = '\0'; 735 if (intrpflg) 736 inbuf[0] = 'x'; 737 738 /* 739 * Handle 'mail' and 'save' here. 740 */ 741 if ((inch = inbuf[0]) == 's' || inch == 'm') { 742 if (inbuf[1] == '-') 743 cmsg = prevmsg; 744 else if (isdigit(inbuf[1])) 745 cmsg = atoi(&inbuf[1]); 746 else 747 cmsg = msg; 748 snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, cmsg); 749 750 oldpos = ftell(newmsg); 751 752 cpfrom = fopen(fname, "r"); 753 if (!cpfrom) { 754 printf("Message %d not found\n", cmsg); 755 ask (prompt); 756 return; 757 } 758 759 if (inch == 's') { 760 in = nxtfld(inbuf); 761 if (*in) { 762 for (n=0; in[n] > ' '; n++) { /* sizeof fname? */ 763 fname[n] = in[n]; 764 } 765 fname[n] = NULL; 766 } 767 else 768 strcpy(fname, "Messages"); 769 fd = open(fname, O_RDWR|O_EXCL|O_CREAT|O_APPEND); 770 } 771 else { 772 strcpy(fname, _PATH_TMP); 773 fd = mkstemp(fname); 774 if (fd != -1) { 775 snprintf(cmdbuf, sizeof(cmdbuf), _PATH_MAIL, 776 fname); 777 mailing = YES; 778 } 779 } 780 if (fd == -1 || (cpto = fdopen(fd, "a")) == NULL) { 781 if (fd != -1) 782 close(fd); 783 warn("%s", fname); 784 mailing = NO; 785 fseek(newmsg, oldpos, 0); 786 ask(prompt); 787 return; 788 } 789 790 while ((n = fread(inbuf, 1, sizeof inbuf, cpfrom))) 791 fwrite(inbuf, 1, n, cpto); 792 793 fclose(cpfrom); 794 fclose(cpto); 795 fseek(newmsg, oldpos, 0); /* reposition current message */ 796 if (inch == 's') 797 printf("Message %d saved in \"%s\"\n", cmsg, fname); 798 else { 799 system(cmdbuf); 800 unlink(fname); 801 mailing = NO; 802 } 803 ask(prompt); 804 } 805 } 806 807 void 808 gfrsub(infile) 809 FILE *infile; 810 { 811 off_t frompos; 812 int count; 813 814 seensubj = seenfrom = NO; 815 local = YES; 816 subj[0] = from[0] = date[0] = NULL; 817 818 /* 819 * Is this a normal message? 820 */ 821 if (fgets(inbuf, sizeof inbuf, infile)) { 822 if (strncmp(inbuf, "From", 4)==0) { 823 /* 824 * expected form starts with From 825 */ 826 seenfrom = YES; 827 frompos = ftell(infile); 828 ptr = from; 829 in = nxtfld(inbuf); 830 if (*in) { 831 count = sizeof(from) - 1; 832 while (*in && *in > ' ' && count-- > 0) { 833 if (*in == ':' || *in == '@' || 834 *in == '!') 835 local = NO; 836 *ptr++ = *in++; 837 } 838 } 839 *ptr = NULL; 840 if (*(in = nxtfld(in))) 841 strncpy(date, in, sizeof date); 842 else { 843 date[0] = '\n'; 844 date[1] = NULL; 845 } 846 } 847 else { 848 /* 849 * not the expected form 850 */ 851 fseek(infile, 0L, 0); 852 return; 853 } 854 } 855 else 856 /* 857 * empty file ? 858 */ 859 return; 860 861 /* 862 * look for Subject line until EOF or a blank line 863 */ 864 while (fgets(inbuf, sizeof inbuf, infile) 865 && !(blankline = (inbuf[0] == '\n'))) { 866 /* 867 * extract Subject line 868 */ 869 if (!seensubj && strncmp(inbuf, "Subj", 4)==0) { 870 seensubj = YES; 871 frompos = ftell(infile); 872 strncpy(subj, nxtfld(inbuf), sizeof subj); 873 } 874 } 875 if (!blankline) 876 /* 877 * ran into EOF 878 */ 879 fseek(infile, frompos, 0); 880 881 if (!seensubj) 882 /* 883 * for possible use with Mail 884 */ 885 strncpy(subj, "(No Subject)\n", sizeof subj); 886 } 887 888 char * 889 nxtfld(s) 890 unsigned char *s; 891 { 892 if (*s) while (*s && !isspace(*s)) s++; /* skip over this field */ 893 if (*s) while (*s && isspace(*s)) s++; /* find start of next field */ 894 return (s); 895 } 896