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