msgs.c revision 1.16 1 /* $NetBSD: msgs.c,v 1.16 2000/07/06 14:21:47 ad 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.16 2000/07/06 14:21:47 ad 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[MAXPATHLEN];
119 char cmdbuf[MAXPATHLEN + 16];
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 snprintf(fname, sizeof (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 snprintf(inbuf, sizeof (inbuf), "%s/%s",
299 _PATH_MSGS, cp);
300
301 while (isdigit((unsigned char)*cp))
302 i = i * 10 + *cp++ - '0';
303 if (*cp)
304 continue; /* not a message! */
305
306 if (clean) {
307 if (stat(inbuf, &stbuf) != 0)
308 continue;
309 if (stbuf.st_mtime < keep
310 && stbuf.st_mode&S_IWRITE) {
311 unlink(inbuf);
312 continue;
313 }
314 }
315
316 if (i > lastmsg)
317 lastmsg = i;
318 if (i < firstmsg)
319 firstmsg = i;
320 seenany = YES;
321 }
322 closedir(dirp);
323
324 if (!seenany) {
325 if (blast != 0) /* never lower the upper bound! */
326 lastmsg = blast;
327 firstmsg = lastmsg + 1;
328 }
329 else if (blast > lastmsg)
330 lastmsg = blast;
331
332 if (!send_msg) {
333 bounds = fopen(fname, "w");
334 if (bounds == NULL) {
335 perror(fname);
336 exit(errno);
337 }
338 chmod(fname, CMODE);
339 fprintf(bounds, "%d %d\n", firstmsg, lastmsg);
340 fclose(bounds);
341 }
342 }
343
344 if (send_msg) {
345 /*
346 * Send mode - place msgs in _PATH_MSGS
347 */
348 bounds = fopen(fname, "w");
349 if (bounds == NULL) {
350 perror(fname);
351 exit(errno);
352 }
353
354 nextmsg = lastmsg + 1;
355 snprintf(fname, sizeof (fname), "%s/%d", _PATH_MSGS, nextmsg);
356 newmsg = fopen(fname, "w");
357 if (newmsg == NULL) {
358 perror(fname);
359 exit(errno);
360 }
361 chmod(fname, 0644);
362
363 fprintf(bounds, "%d %d\n", firstmsg, nextmsg);
364 fclose(bounds);
365
366 sending = YES;
367 if (ruptible)
368 signal(SIGINT, onintr);
369
370 if (isatty(fileno(stdin))) {
371 pw = getpwuid(uid);
372 if (!pw) {
373 fprintf(stderr, "Who are you?\n");
374 exit(1);
375 }
376 printf("Message %d:\nFrom %s %sSubject: ",
377 nextmsg, pw->pw_name, ctime(&t));
378 fflush(stdout);
379 fgets(inbuf, sizeof inbuf, stdin);
380 putchar('\n');
381 fflush(stdout);
382 fprintf(newmsg, "From %s %sSubject: %s\n",
383 ptr, ctime(&t), inbuf);
384 blankline = seensubj = YES;
385 }
386 else
387 blankline = seensubj = NO;
388 for (;;) {
389 fgets(inbuf, sizeof inbuf, stdin);
390 if (feof(stdin) || ferror(stdin))
391 break;
392 blankline = (blankline || (inbuf[0] == '\n'));
393 seensubj = (seensubj || (!blankline && (strncmp(inbuf, "Subj", 4) == 0)));
394 fputs(inbuf, newmsg);
395 }
396 #ifdef OBJECT
397 if (!seensubj) {
398 printf("NOTICE: Messages should have a Subject field!\n");
399 #ifdef REJECT
400 unlink(fname);
401 #endif
402 exit(1);
403 }
404 #endif
405 exit(ferror(stdin));
406 }
407 if (clean)
408 exit(0);
409
410 /*
411 * prepare to display messages
412 */
413 totty = (isatty(fileno(stdout)) != 0);
414 use_pager = use_pager && totty;
415
416 snprintf(fname, sizeof (fname), "%s/%s", getenv("HOME"), MSGSRC);
417 msgsrc = fopen(fname, "r");
418 if (msgsrc) {
419 newrc = NO;
420 fscanf(msgsrc, "%d\n", &nextmsg);
421 fclose(msgsrc);
422 if (nextmsg > lastmsg+1) {
423 printf("Warning: bounds have been reset (%d, %d)\n",
424 firstmsg, lastmsg);
425 truncate(fname, (off_t)0);
426 newrc = YES;
427 }
428 else if (!rcfirst)
429 rcfirst = nextmsg - rcback;
430 }
431 else
432 newrc = YES;
433 msgsrc = fopen(fname, "r+");
434 if (msgsrc == NULL)
435 msgsrc = fopen(fname, "w");
436 if (msgsrc == NULL) {
437 perror(fname);
438 exit(errno);
439 }
440 if (rcfirst) {
441 if (rcfirst > lastmsg+1) {
442 printf("Warning: the last message is number %d.\n",
443 lastmsg);
444 rcfirst = nextmsg;
445 }
446 if (rcfirst > firstmsg)
447 firstmsg = rcfirst; /* don't set below first msg */
448 }
449 if (newrc) {
450 nextmsg = firstmsg;
451 fseek(msgsrc, 0L, 0);
452 fprintf(msgsrc, "%d\n", nextmsg);
453 fflush(msgsrc);
454 }
455
456 #ifdef V7
457 if (totty) {
458 struct winsize win;
459 if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1)
460 Lpp = win.ws_row;
461 if (Lpp <= 0) {
462 if (tgetent(inbuf, getenv("TERM")) <= 0
463 || (Lpp = tgetnum("li")) <= 0) {
464 Lpp = NLINES;
465 }
466 }
467 }
468 #endif
469 Lpp -= 6; /* for headers, etc. */
470
471 already = NO;
472 prevmsg = firstmsg;
473 printing = YES;
474 if (ruptible)
475 signal(SIGINT, onintr);
476
477 /*
478 * Main program loop
479 */
480 for (msg = firstmsg; msg <= lastmsg; msg++) {
481
482 snprintf(fname, sizeof (fname), "%s/%d", _PATH_MSGS, msg);
483 newmsg = fopen(fname, "r");
484 if (newmsg == NULL)
485 continue;
486
487 gfrsub(newmsg); /* get From and Subject fields */
488 if (locomode && !local) {
489 fclose(newmsg);
490 continue;
491 }
492
493 if (qopt) { /* This has to be located here */
494 printf("There are new messages.\n");
495 exit(0);
496 }
497
498 if (already && !hdrs)
499 putchar('\n');
500
501 /*
502 * Print header
503 */
504 if (totty)
505 signal(SIGTSTP, onsusp);
506 (void) setjmp(tstpbuf);
507 already = YES;
508 nlines = 2;
509 if (seenfrom) {
510 printf("Message %d:\nFrom %s %s", msg, from, date);
511 nlines++;
512 }
513 if (seensubj) {
514 printf("Subject: %s", subj);
515 nlines++;
516 }
517 else {
518 if (seenfrom) {
519 putchar('\n');
520 nlines++;
521 }
522 while (nlines < 6
523 && fgets(inbuf, sizeof inbuf, newmsg)
524 && inbuf[0] != '\n') {
525 fputs(inbuf, stdout);
526 nlines++;
527 }
528 }
529
530 lct = linecnt(newmsg);
531 if (lct)
532 printf("(%d%slines) ", lct, seensubj? " " : " more ");
533
534 if (hdrs) {
535 printf("\n-----\n");
536 fclose(newmsg);
537 continue;
538 }
539
540 /*
541 * Ask user for command
542 */
543 if (totty)
544 ask(lct? MORE : (msg==lastmsg? NOMORE : NEXT));
545 else
546 inbuf[0] = 'y';
547 if (totty)
548 signal(SIGTSTP, SIG_DFL);
549 cmnd:
550 in = inbuf;
551 switch (*in) {
552 case 'x':
553 case 'X':
554 exit(0);
555
556 case 'q':
557 case 'Q':
558 quitit = YES;
559 printf("--Postponed--\n");
560 exit(0);
561 /* intentional fall-thru */
562 case 'n':
563 case 'N':
564 if (msg >= nextmsg) sep = "Flushed";
565 prevmsg = msg;
566 break;
567
568 case 'p':
569 case 'P':
570 use_pager = (*in++ == 'p');
571 /* intentional fallthru */
572 case '\n':
573 case 'y':
574 default:
575 if (*in == '-') {
576 msg = prevmsg-1;
577 sep = "replay";
578 break;
579 }
580 if (isdigit((unsigned char)*in)) {
581 msg = next(in);
582 sep = in;
583 break;
584 }
585
586 prmesg(nlines + lct + (seensubj? 1 : 0));
587 prevmsg = msg;
588
589 }
590
591 printf("--%s--\n", sep);
592 sep = "-";
593 if (msg >= nextmsg) {
594 nextmsg = msg + 1;
595 fseek(msgsrc, 0L, 0);
596 fprintf(msgsrc, "%d\n", nextmsg);
597 fflush(msgsrc);
598 }
599 if (newmsg)
600 fclose(newmsg);
601 if (quitit)
602 break;
603 }
604
605 /*
606 * Make sure .rc file gets updated
607 */
608 if (--msg >= nextmsg) {
609 nextmsg = msg + 1;
610 fseek(msgsrc, 0L, 0);
611 fprintf(msgsrc, "%d\n", nextmsg);
612 fflush(msgsrc);
613 }
614 if (already && !quitit && lastcmd && totty) {
615 /*
616 * save or reply to last message?
617 */
618 msg = prevmsg;
619 ask(NOMORE);
620 if (inbuf[0] == '-' || isdigit((unsigned char)inbuf[0]))
621 goto cmnd;
622 }
623 if (!(already || hush || qopt))
624 printf("No new messages.\n");
625 exit(0);
626 }
627
628 void
629 prmesg(length)
630 int length;
631 {
632 FILE *outf;
633 char *env_pager;
634
635 if (use_pager && length > Lpp) {
636 signal(SIGPIPE, SIG_IGN);
637 signal(SIGQUIT, SIG_IGN);
638 if ((env_pager = getenv("PAGER")) == NULL ||
639 env_pager[0] == '\0') {
640 snprintf(cmdbuf, sizeof(cmdbuf), "%s -z%d",
641 _PATH_PAGER, Lpp);
642 } else {
643 strlcpy(cmdbuf, env_pager, sizeof (cmdbuf));
644 }
645 outf = popen(cmdbuf, "w");
646 if (!outf)
647 outf = stdout;
648 else
649 setbuf(outf, (char *)NULL);
650 }
651 else
652 outf = stdout;
653
654 if (seensubj)
655 putc('\n', outf);
656
657 while (fgets(inbuf, sizeof inbuf, newmsg)) {
658 fputs(inbuf, outf);
659 if (ferror(outf)) {
660 clearerr(outf);
661 break;
662 }
663 }
664
665 if (outf != stdout) {
666 pclose(outf);
667 signal(SIGPIPE, SIG_DFL);
668 signal(SIGQUIT, SIG_DFL);
669 }
670 else {
671 fflush(stdout);
672 }
673
674 /* trick to force wait on output */
675 tcdrain(fileno(stdout));
676 }
677
678 void
679 onintr(dummy)
680 int dummy;
681 {
682 signal(SIGINT, onintr);
683 if (mailing)
684 unlink(fname);
685 if (sending) {
686 unlink(fname);
687 puts("--Killed--");
688 exit(1);
689 }
690 if (printing) {
691 putchar('\n');
692 if (hdrs)
693 exit(0);
694 sep = "Interrupt";
695 if (newmsg)
696 fseek(newmsg, 0L, 2);
697 intrpflg = YES;
698 }
699 }
700
701 /*
702 * We have just gotten a susp. Suspend and prepare to resume.
703 */
704 void
705 onsusp(dummy)
706 int dummy;
707 {
708
709 signal(SIGTSTP, SIG_DFL);
710 sigsetmask(0);
711 kill(0, SIGTSTP);
712 signal(SIGTSTP, onsusp);
713 if (!mailing)
714 longjmp(tstpbuf, 0);
715 }
716
717 int
718 linecnt(f)
719 FILE *f;
720 {
721 off_t oldpos = ftell(f);
722 int l = 0;
723 char lbuf[BUFSIZ];
724
725 while (fgets(lbuf, sizeof lbuf, f))
726 l++;
727 clearerr(f);
728 fseek(f, oldpos, 0);
729 return (l);
730 }
731
732 int
733 next(buf)
734 char *buf;
735 {
736 int i;
737 sscanf(buf, "%d", &i);
738 snprintf(buf, sizeof (buf), "Goto %d", i);
739 return(--i);
740 }
741
742 void
743 ask(prompt)
744 char *prompt;
745 {
746 char inch;
747 int n, cmsg;
748 off_t oldpos;
749 FILE *cpfrom, *cpto;
750
751 printf("%s ", prompt);
752 fflush(stdout);
753 intrpflg = NO;
754 (void) fgets(inbuf, sizeof inbuf, stdin);
755 if ((n = strlen(inbuf)) > 0 && inbuf[n - 1] == '\n')
756 inbuf[n - 1] = '\0';
757 if (intrpflg)
758 inbuf[0] = 'x';
759
760 /*
761 * Handle 'mail' and 'save' here.
762 */
763 if (((inch = inbuf[0]) == 's' || inch == 'm') && !restricted) {
764 if (inbuf[1] == '-')
765 cmsg = prevmsg;
766 else if (isdigit((unsigned char)inbuf[1]))
767 cmsg = atoi(&inbuf[1]);
768 else
769 cmsg = msg;
770 snprintf(fname, sizeof (fname), "%s/%d", _PATH_MSGS, cmsg);
771
772 oldpos = ftell(newmsg);
773
774 cpfrom = fopen(fname, "r");
775 if (!cpfrom) {
776 printf("Message %d not found\n", cmsg);
777 ask (prompt);
778 return;
779 }
780
781 if (inch == 's') {
782 in = nxtfld(inbuf);
783 if (*in) {
784 for (n=0; in[n] > ' '; n++) { /* sizeof fname? */
785 fname[n] = in[n];
786 }
787 fname[n] = '\0';
788 }
789 else
790 strlcpy(fname, "Messages", sizeof(fname));
791 }
792 else {
793 int fd;
794
795 strlcpy(fname, _PATH_TMP, sizeof(fname));
796 fd = mkstemp(fname);
797 if (fd == -1)
798 err(1, "mkstemp failed");
799 close(fd);
800 snprintf(cmdbuf, sizeof (cmdbuf), _PATH_MAIL, fname);
801 mailing = YES;
802 }
803 cpto = fopen(fname, "a");
804 if (!cpto) {
805 perror(fname);
806 mailing = NO;
807 fseek(newmsg, oldpos, 0);
808 ask(prompt);
809 return;
810 }
811
812 while ((n = fread(inbuf, 1, sizeof inbuf, cpfrom)) != 0)
813 fwrite(inbuf, 1, n, cpto);
814
815 fclose(cpfrom);
816 fclose(cpto);
817 fseek(newmsg, oldpos, 0); /* reposition current message */
818 if (inch == 's')
819 printf("Message %d saved in \"%s\"\n", cmsg, fname);
820 else {
821 system(cmdbuf);
822 unlink(fname);
823 mailing = NO;
824 }
825 ask(prompt);
826 }
827 }
828
829 void
830 gfrsub(infile)
831 FILE *infile;
832 {
833 off_t frompos;
834
835 seensubj = seenfrom = NO;
836 local = YES;
837 subj[0] = from[0] = date[0] = 0;
838
839 /*
840 * Is this a normal message?
841 */
842 if (fgets(inbuf, sizeof inbuf, infile)) {
843 if (strncmp(inbuf, "From", 4)==0) {
844 /*
845 * expected form starts with From
846 */
847 seenfrom = YES;
848 frompos = ftell(infile);
849 ptr = from;
850 in = nxtfld(inbuf);
851 if (*in) while (*in && *in > ' ') {
852 if (*in == ':' || *in == '@' || *in == '!')
853 local = NO;
854 *ptr++ = *in++;
855 /* what about sizeof from ? */
856 }
857 *ptr = '\0';
858 if (*(in = nxtfld(in)))
859 strncpy(date, in, sizeof date);
860 else {
861 date[0] = '\n';
862 date[1] = '\0';
863 }
864 }
865 else {
866 /*
867 * not the expected form
868 */
869 fseek(infile, 0L, 0);
870 return;
871 }
872 }
873 else
874 /*
875 * empty file ?
876 */
877 return;
878
879 /*
880 * look for Subject line until EOF or a blank line
881 */
882 while (fgets(inbuf, sizeof inbuf, infile)
883 && !(blankline = (inbuf[0] == '\n'))) {
884 /*
885 * extract Subject line
886 */
887 if (!seensubj && strncmp(inbuf, "Subj", 4)==0) {
888 seensubj = YES;
889 frompos = ftell(infile);
890 strncpy(subj, nxtfld(inbuf), sizeof subj);
891 }
892 }
893 if (!blankline)
894 /*
895 * ran into EOF
896 */
897 fseek(infile, frompos, 0);
898
899 if (!seensubj)
900 /*
901 * for possible use with Mail
902 */
903 strncpy(subj, "(No Subject)\n", sizeof subj);
904 }
905
906 char *
907 nxtfld(s)
908 char *s;
909 {
910 if (*s) while (*s && *s > ' ') s++; /* skip over this field */
911 if (*s) while (*s && *s <= ' ') s++; /* find start of next field */
912 return (s);
913 }
914