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