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