2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)msgs.c 8.2 (Berkeley) 4/28/95";
44 static const char rcsid[] =
49 * msgs - a user bulletin board program
52 * msgs [fhlopq] [[-]number] to read messages
53 * msgs -s to place messages
54 * msgs -c [-days] to clean up the bulletin board
56 * prompt commands are:
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>
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 */
75 #include <sys/param.h>
93 #include "pathnames.h"
95 #define CMODE 0644 /* bounds file creation mode */
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] ?"
115 char fname[MAXPATHLEN];
116 char cmdbuf[MAXPATHLEN + MAXPATHLEN];
142 /* option initialization */
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));
166 int argc; char *argv[];
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;
173 struct stat buf; /* stat to check access of bounds */
177 setbuf(stdout, NULL);
179 setlocale(LC_ALL, "");
182 setuid(uid = getuid());
183 ruptible = (signal(SIGINT, SIG_IGN) == SIG_DFL);
185 signal(SIGINT, SIG_DFL);
189 if (isdigit(argv[0][0])) { /* starting message # */
190 rcfirst = atoi(argv[0]);
192 else if (isdigit(argv[0][1])) { /* backward offset */
193 rcback = atoi( &( argv[0][1] ) );
197 while (*ptr) switch (*ptr++) {
203 if (uid != SUPERUSER && uid != DAEMON) {
204 fprintf(stderr, "Sorry\n");
210 case 'f': /* silently */
214 case 'h': /* headers only */
218 case 'l': /* local msgs only */
222 case 'o': /* option to save last message */
226 case 'p': /* pipe thru 'more' during long msgs */
230 case 'q': /* query only */
234 case 's': /* sending TO msgs */
246 * determine current message bounds
248 snprintf(fname, sizeof(fname), "%s/%s", _PATH_MSGS, BOUNDS);
251 * Test access rights to the bounds file
252 * This can be a little tricky. if(send_msg), then
253 * we will create it. We assume that if(send_msg),
254 * then you have write permission there.
255 * Else, it better be there, or we bail.
257 if (send_msg != YES) {
258 if (stat(fname, &buf) < 0) {
260 err(errno, "%s", fname);
266 bounds = fopen(fname, "r");
268 if (bounds != NULL) {
269 fscanf(bounds, "%d %d\n", &firstmsg, &lastmsg);
271 blast = lastmsg; /* save upper bound */
275 keep = t - (rcback? rcback : NDAYS) DAYS;
277 if (clean || bounds == NULL) { /* relocate message bounds */
283 dirp = opendir(_PATH_MSGS);
285 err(errno, "%s", _PATH_MSGS);
290 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)){
291 register char *cp = dp->d_name;
296 if (dp->d_namlen == 0)
300 snprintf(inbuf, sizeof(inbuf), "%s/%s", _PATH_MSGS, cp);
303 i = i * 10 + *cp++ - '0';
305 continue; /* not a message! */
308 if (stat(inbuf, &stbuf) != 0)
310 if (stbuf.st_mtime < keep
311 && stbuf.st_mode&S_IWRITE) {
326 if (blast != 0) /* never lower the upper bound! */
328 firstmsg = lastmsg + 1;
330 else if (blast > lastmsg)
334 bounds = fopen(fname, "w");
336 err(errno, "%s", fname);
338 fprintf(bounds, "%d %d\n", firstmsg, lastmsg);
345 * Send mode - place msgs in _PATH_MSGS
347 bounds = fopen(fname, "w");
349 err(errno, "%s", fname);
351 nextmsg = lastmsg + 1;
352 snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, nextmsg);
353 newmsg = fopen(fname, "w");
355 err(errno, "%s", fname);
358 fprintf(bounds, "%d %d\n", firstmsg, nextmsg);
363 signal(SIGINT, onintr);
365 if (isatty(fileno(stdin))) {
366 ptr = getpwuid(uid)->pw_name;
367 printf("Message %d:\nFrom %s %sSubject: ",
368 nextmsg, ptr, ctime(&t));
370 fgets(inbuf, sizeof inbuf, stdin);
373 fprintf(newmsg, "From %s %sSubject: %s\n",
374 ptr, ctime(&t), inbuf);
375 blankline = seensubj = YES;
378 blankline = seensubj = NO;
380 fgets(inbuf, sizeof inbuf, stdin);
381 if (feof(stdin) || ferror(stdin))
383 blankline = (blankline || (inbuf[0] == '\n'));
384 seensubj = (seensubj || (!blankline && (strncmp(inbuf, "Subj", 4) == 0)));
385 fputs(inbuf, newmsg);
389 printf("NOTICE: Messages should have a Subject field!\n");
402 * prepare to display messages
404 totty = (isatty(fileno(stdout)) != 0);
405 use_pager = use_pager && totty;
407 snprintf(fname, sizeof(fname), "%s/%s", getenv("HOME"), MSGSRC);
408 msgsrc = fopen(fname, "r");
411 fscanf(msgsrc, "%d\n", &nextmsg);
413 if (nextmsg > lastmsg+1) {
414 printf("Warning: bounds have been reset (%d, %d)\n",
416 truncate(fname, (off_t)0);
420 rcfirst = nextmsg - rcback;
424 msgsrc = fopen(fname, "r+");
426 msgsrc = fopen(fname, "w");
428 err(errno, "%s", fname);
430 if (rcfirst > lastmsg+1) {
431 printf("Warning: the last message is number %d.\n",
435 if (rcfirst > firstmsg)
436 firstmsg = rcfirst; /* don't set below first msg */
440 fseek(msgsrc, 0L, 0);
441 fprintf(msgsrc, "%d\n", nextmsg);
448 if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1)
451 if (tgetent(inbuf, getenv("TERM")) <= 0
452 || (Lpp = tgetnum("li")) <= 0) {
458 Lpp -= 6; /* for headers, etc. */
464 signal(SIGINT, onintr);
469 for (msg = firstmsg; msg <= lastmsg; msg++) {
471 snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, msg);
472 newmsg = fopen(fname, "r");
476 gfrsub(newmsg); /* get From and Subject fields */
477 if (locomode && !local) {
482 if (qopt) { /* This has to be located here */
483 printf("There are new messages.\n");
487 if (already && !hdrs)
494 signal(SIGTSTP, onsusp);
495 (void) setjmp(tstpbuf);
499 printf("Message %d:\nFrom %s %s", msg, from, date);
503 printf("Subject: %s", subj);
512 && fgets(inbuf, sizeof inbuf, newmsg)
513 && inbuf[0] != '\n') {
514 fputs(inbuf, stdout);
519 lct = linecnt(newmsg);
521 printf("(%d%slines) ", lct, seensubj? " " : " more ");
530 * Ask user for command
533 ask(lct? MORE : (msg==lastmsg? NOMORE : NEXT));
537 signal(SIGTSTP, SIG_DFL);
548 printf("--Postponed--\n");
550 /* intentional fall-thru */
553 if (msg >= nextmsg) sep = "Flushed";
559 use_pager = (*in++ == 'p');
560 /* intentional fallthru */
575 prmesg(nlines + lct + (seensubj? 1 : 0));
580 printf("--%s--\n", sep);
582 if (msg >= nextmsg) {
584 fseek(msgsrc, 0L, 0);
585 fprintf(msgsrc, "%d\n", nextmsg);
595 * Make sure .rc file gets updated
597 if (--msg >= nextmsg) {
599 fseek(msgsrc, 0L, 0);
600 fprintf(msgsrc, "%d\n", nextmsg);
603 if (already && !quitit && lastcmd && totty) {
605 * save or reply to last message?
609 if (inbuf[0] == '-' || isdigit(inbuf[0]))
612 if (!(already || hush || qopt))
613 printf("No new messages.\n");
620 fprintf(stderr, "usage: msgs [fhlopq] [[-]number]\n");
631 if (use_pager && length > Lpp) {
632 signal(SIGPIPE, SIG_IGN);
633 signal(SIGQUIT, SIG_IGN);
634 if ((env_pager = getenv("PAGER")) == NULL) {
635 snprintf(cmdbuf, sizeof(cmdbuf), _PATH_PAGER, Lpp);
637 snprintf(cmdbuf, sizeof(cmdbuf), "%s", env_pager);
639 outf = popen(cmdbuf, "w");
643 setbuf(outf, (char *)NULL);
651 while (fgets(inbuf, sizeof inbuf, newmsg)) {
659 if (outf != stdout) {
661 signal(SIGPIPE, SIG_DFL);
662 signal(SIGQUIT, SIG_DFL);
668 /* force wait on output */
669 tcdrain(fileno(stdout));
676 signal(SIGINT, onintr);
690 fseek(newmsg, 0L, 2);
696 * We have just gotten a susp. Suspend and prepare to resume.
702 signal(SIGTSTP, SIG_DFL);
705 signal(SIGTSTP, onsusp);
714 off_t oldpos = ftell(f);
718 while (fgets(lbuf, sizeof lbuf, f))
730 sscanf(buf, "%d", &i);
731 sprintf(buf, "Goto %d", i);
744 printf("%s ", prompt);
747 (void) fgets(inbuf, sizeof inbuf, stdin);
748 if ((n = strlen(inbuf)) > 0 && inbuf[n - 1] == '\n')
754 * Handle 'mail' and 'save' here.
756 if ((inch = inbuf[0]) == 's' || inch == 'm') {
759 else if (isdigit(inbuf[1]))
760 cmsg = atoi(&inbuf[1]);
763 snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, cmsg);
765 oldpos = ftell(newmsg);
767 cpfrom = fopen(fname, "r");
769 printf("Message %d not found\n", cmsg);
777 for (n=0; in[n] > ' '; n++) { /* sizeof fname? */
783 strcpy(fname, "Messages");
784 fd = open(fname, O_RDWR|O_EXCL|O_CREAT|O_APPEND);
787 strcpy(fname, _PATH_TMP);
790 snprintf(cmdbuf, sizeof(cmdbuf), _PATH_MAIL,
795 if (fd == -1 || (cpto = fdopen(fd, "a")) == NULL) {
800 fseek(newmsg, oldpos, 0);
805 while ((n = fread(inbuf, 1, sizeof inbuf, cpfrom)))
806 fwrite(inbuf, 1, n, cpto);
810 fseek(newmsg, oldpos, 0); /* reposition current message */
812 printf("Message %d saved in \"%s\"\n", cmsg, fname);
829 seensubj = seenfrom = NO;
831 subj[0] = from[0] = date[0] = NULL;
834 * Is this a normal message?
836 if (fgets(inbuf, sizeof inbuf, infile)) {
837 if (strncmp(inbuf, "From", 4)==0) {
839 * expected form starts with From
842 frompos = ftell(infile);
846 count = sizeof(from) - 1;
847 while (*in && *in > ' ' && count-- > 0) {
848 if (*in == ':' || *in == '@' ||
855 if (*(in = nxtfld(in)))
856 strncpy(date, in, sizeof date);
864 * not the expected form
866 fseek(infile, 0L, 0);
877 * look for Subject line until EOF or a blank line
879 while (fgets(inbuf, sizeof inbuf, infile)
880 && !(blankline = (inbuf[0] == '\n'))) {
882 * extract Subject line
884 if (!seensubj && strncmp(inbuf, "Subj", 4)==0) {
886 frompos = ftell(infile);
887 strncpy(subj, nxtfld(inbuf), sizeof subj);
894 fseek(infile, frompos, 0);
898 * for possible use with Mail
900 strncpy(subj, "(No Subject)\n", sizeof subj);
907 if (*s) while (*s && !isspace(*s)) s++; /* skip over this field */
908 if (*s) while (*s && isspace(*s)) s++; /* find start of next field */