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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char copyright[] =
32 "@(#) Copyright (c) 1980, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)msgs.c 8.2 (Berkeley) 4/28/95";
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
46 * msgs - a user bulletin board program
49 * msgs [fhlopq] [[-]number] to read messages
50 * msgs -s to place messages
51 * msgs -c [-days] to clean up the bulletin board
53 * prompt commands are:
55 * n flush message, go to next message
56 * q flush message, quit
57 * p print message, turn on 'pipe thru more' mode
58 * P print message, turn off 'pipe thru more' mode
59 * - reprint last message
60 * s[-][<num>] [<filename>] save message
61 * m[-][<num>] mail with message in temp mbox
62 * x exit without flushing this message
63 * <num> print message number <num>
66 #define V7 /* will look for TERM in the environment */
67 #define OBJECT /* will object to messages without Subjects */
68 /* #define REJECT */ /* will reject messages without Subjects
69 (OBJECT must be defined also) */
70 /* #define UNBUFFERED *//* use unbuffered output */
72 #include <sys/param.h>
90 #include "pathnames.h"
92 #define CMODE 0644 /* bounds file creation mode */
95 #define SUPERUSER 0 /* superuser uid */
96 #define DAEMON 1 /* daemon uid */
97 #define NLINES 24 /* default number of lines/crt screen */
98 #define NDAYS 21 /* default keep time for messages */
99 #define DAYS *24*60*60 /* seconds/day */
100 #define MSGSRC ".msgsrc" /* user's rc file */
101 #define BOUNDS "bounds" /* message bounds file */
102 #define NEXT "Next message? [yq]"
103 #define MORE "More? [ynq]"
104 #define NOMORE "(No more) [q] ?"
110 static const char *sep = "-";
111 static char inbuf[BUFSIZ];
112 static char fname[MAXPATHLEN];
113 static char cmdbuf[MAXPATHLEN + MAXPATHLEN];
114 static char subj[128];
115 static char from[128];
116 static char date[128];
120 static bool ruptible;
122 static bool seenfrom;
123 static bool seensubj;
124 static bool blankline;
125 static bool printing = NO;
126 static bool mailing = NO;
127 static bool quitit = NO;
128 static bool sending = NO;
129 static bool intrpflg = NO;
139 /* option initialization */
140 static bool hdrs = NO;
141 static bool qopt = NO;
142 static bool hush = NO;
143 static bool send_msg = NO;
144 static bool locomode = NO;
145 static bool use_pager = NO;
146 static bool clean = NO;
147 static bool lastcmd = NO;
148 static jmp_buf tstpbuf;
150 static void ask(const char *);
151 static void gfrsub(FILE *);
152 static int linecnt(FILE *);
153 static int next(char *);
154 static char *nxtfld(char *);
155 static void onsusp(int);
156 static void onintr(int);
157 static void prmesg(int);
158 static void usage(void);
161 main(int argc, char *argv[])
164 int rcfirst = 0; /* first message to print (from .rc) */
165 int rcback = 0; /* amount to back off of rcfirst */
166 int firstmsg = 0, nextmsg = 0, lastmsg = 0;
168 struct stat buf; /* stat to check access of bounds */
173 setbuf(stdout, NULL);
175 setlocale(LC_ALL, "");
178 if (setuid(uid = getuid()) != 0)
179 err(1, "setuid failed");
180 ruptible = (signal(SIGINT, SIG_IGN) == SIG_DFL);
182 signal(SIGINT, SIG_DFL);
186 if (isdigit(argv[0][0])) { /* starting message # */
187 rcfirst = atoi(argv[0]);
189 else if (isdigit(argv[0][1])) { /* backward offset */
190 rcback = atoi( &( argv[0][1] ) );
194 while (*ptr) switch (*ptr++) {
200 if (uid != SUPERUSER && uid != DAEMON)
202 "only the super-user can use the c flag");
206 case 'f': /* silently */
210 case 'h': /* headers only */
214 case 'l': /* local msgs only */
218 case 'o': /* option to save last message */
222 case 'p': /* pipe thru 'more' during long msgs */
226 case 'q': /* query only */
230 case 's': /* sending TO msgs */
242 * determine current message bounds
244 snprintf(fname, sizeof(fname), "%s/%s", _PATH_MSGS, BOUNDS);
247 * Test access rights to the bounds file
248 * This can be a little tricky. if(send_msg), then
249 * we will create it. We assume that if(send_msg),
250 * then you have write permission there.
251 * Else, it better be there, or we bail.
253 if (send_msg != YES) {
254 if (stat(fname, &buf) < 0) {
256 err(errno, "%s", fname);
262 bounds = fopen(fname, "r");
264 if (bounds != NULL) {
265 fscanf(bounds, "%d %d\n", &firstmsg, &lastmsg);
267 blast = lastmsg; /* save upper bound */
271 keep = t - (rcback? rcback : NDAYS) DAYS;
273 if (clean || bounds == NULL) { /* relocate message bounds */
279 dirp = opendir(_PATH_MSGS);
281 err(errno, "%s", _PATH_MSGS);
286 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)){
292 if (dp->d_namlen == 0)
296 snprintf(inbuf, sizeof(inbuf), "%s/%s", _PATH_MSGS, cp);
299 i = i * 10 + *cp++ - '0';
301 continue; /* not a message! */
304 if (stat(inbuf, &stbuf) != 0)
306 if (stbuf.st_mtime < keep
307 && stbuf.st_mode&S_IWRITE) {
322 if (blast != 0) /* never lower the upper bound! */
324 firstmsg = lastmsg + 1;
326 else if (blast > lastmsg)
330 bounds = fopen(fname, "w");
332 err(errno, "%s", fname);
334 fprintf(bounds, "%d %d\n", firstmsg, lastmsg);
341 * Send mode - place msgs in _PATH_MSGS
343 bounds = fopen(fname, "w");
345 err(errno, "%s", fname);
347 nextmsg = lastmsg + 1;
348 snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, nextmsg);
349 newmsg = fopen(fname, "w");
351 err(errno, "%s", fname);
354 fprintf(bounds, "%d %d\n", firstmsg, nextmsg);
359 signal(SIGINT, onintr);
361 if (isatty(fileno(stdin))) {
362 ptr = getpwuid(uid)->pw_name;
363 printf("Message %d:\nFrom %s %sSubject: ",
364 nextmsg, ptr, ctime(&t));
366 fgets(inbuf, sizeof inbuf, stdin);
369 fprintf(newmsg, "From %s %sSubject: %s\n",
370 ptr, ctime(&t), inbuf);
371 blankline = seensubj = YES;
374 blankline = seensubj = NO;
376 fgets(inbuf, sizeof inbuf, stdin);
377 if (feof(stdin) || ferror(stdin))
379 blankline = (blankline || (inbuf[0] == '\n'));
380 seensubj = (seensubj || (!blankline && (strncmp(inbuf, "Subj", 4) == 0)));
381 fputs(inbuf, newmsg);
385 printf("NOTICE: Messages should have a Subject field!\n");
398 * prepare to display messages
400 totty = (isatty(fileno(stdout)) != 0);
401 use_pager = use_pager && totty;
403 if ((cp = getenv("HOME")) == NULL || *cp == '\0') {
404 fprintf(stderr, "Error, no home directory!\n");
407 snprintf(fname, sizeof(fname), "%s/%s", cp, 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 */
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%sline%s) ", lct, seensubj? " " : " more ",
522 (lct == 1) ? "" : "s");
531 * Ask user for command
534 ask(lct? MORE : (msg==lastmsg? NOMORE : NEXT));
538 signal(SIGTSTP, SIG_DFL);
552 printf("--Postponed--\n");
559 if (msg >= nextmsg) sep = "Flushed";
566 use_pager = (*in++ == 'p');
583 prmesg(nlines + lct + (seensubj? 1 : 0));
588 printf("--%s--\n", sep);
590 if (msg >= nextmsg) {
593 fprintf(msgsrc, "%d\n", nextmsg);
603 * Make sure .rc file gets updated
605 if (--msg >= nextmsg) {
608 fprintf(msgsrc, "%d\n", nextmsg);
611 if (already && !quitit && lastcmd && totty) {
613 * save or reply to last message?
617 if (inbuf[0] == '-' || isdigit(inbuf[0]))
620 if (!(already || hush || qopt))
621 printf("No new messages.\n");
629 fprintf(stderr, "usage: msgs [fhlopq] [[-]number]\n");
639 if (use_pager && length > Lpp) {
640 signal(SIGPIPE, SIG_IGN);
641 signal(SIGQUIT, SIG_IGN);
642 if ((env_pager = getenv("PAGER")) == NULL) {
643 snprintf(cmdbuf, sizeof(cmdbuf), _PATH_PAGER, Lpp);
645 snprintf(cmdbuf, sizeof(cmdbuf), "%s", env_pager);
647 outf = popen(cmdbuf, "w");
651 setbuf(outf, (char *)NULL);
659 while (fgets(inbuf, sizeof inbuf, newmsg)) {
667 if (outf != stdout) {
669 signal(SIGPIPE, SIG_DFL);
670 signal(SIGQUIT, SIG_DFL);
676 /* force wait on output */
677 tcdrain(fileno(stdout));
681 onintr(int unused __unused)
683 signal(SIGINT, onintr);
697 fseeko(newmsg, (off_t)0, SEEK_END);
703 * We have just gotten a susp. Suspend and prepare to resume.
706 onsusp(int unused __unused)
708 signal(SIGTSTP, SIG_DFL);
711 signal(SIGTSTP, onsusp);
719 off_t oldpos = ftello(f);
723 while (fgets(lbuf, sizeof lbuf, f))
726 fseeko(f, oldpos, SEEK_SET);
734 sscanf(buf, "%d", &i);
735 sprintf(buf, "Goto %d", i);
740 ask(const char *prompt)
747 printf("%s ", prompt);
750 (void) fgets(inbuf, sizeof inbuf, stdin);
751 if ((n = strlen(inbuf)) > 0 && inbuf[n - 1] == '\n')
757 * Handle 'mail' and 'save' here.
759 if ((inch = inbuf[0]) == 's' || inch == 'm') {
762 else if (isdigit(inbuf[1]))
763 cmsg = atoi(&inbuf[1]);
766 snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, cmsg);
768 oldpos = ftello(newmsg);
770 cpfrom = fopen(fname, "r");
772 printf("Message %d not found\n", cmsg);
780 for (n=0; in[n] > ' '; n++) { /* sizeof fname? */
786 strcpy(fname, "Messages");
787 fd = open(fname, O_RDWR|O_EXCL|O_CREAT|O_APPEND);
790 strcpy(fname, _PATH_TMP);
793 snprintf(cmdbuf, sizeof(cmdbuf), _PATH_MAIL,
798 if (fd == -1 || (cpto = fdopen(fd, "a")) == NULL) {
803 fseeko(newmsg, oldpos, SEEK_SET);
808 while ((n = fread(inbuf, 1, sizeof inbuf, cpfrom)))
809 fwrite(inbuf, 1, n, cpto);
813 fseeko(newmsg, oldpos, SEEK_SET);/* reposition current message */
815 printf("Message %d saved in \"%s\"\n", cmsg, fname);
831 seensubj = seenfrom = NO;
833 subj[0] = from[0] = date[0] = '\0';
836 * Is this a normal message?
838 if (fgets(inbuf, sizeof inbuf, infile)) {
839 if (strncmp(inbuf, "From", 4)==0) {
841 * expected form starts with From
844 frompos = ftello(infile);
848 count = sizeof(from) - 1;
849 while (*in && *in > ' ' && count-- > 0) {
850 if (*in == ':' || *in == '@' ||
857 if (*(in = nxtfld(in)))
858 strncpy(date, in, sizeof date);
866 * not the expected form
879 * look for Subject line until EOF or a blank line
881 while (fgets(inbuf, sizeof inbuf, infile)
882 && !(blankline = (inbuf[0] == '\n'))) {
884 * extract Subject line
886 if (!seensubj && strncmp(inbuf, "Subj", 4)==0) {
888 frompos = ftello(infile);
889 strncpy(subj, nxtfld(inbuf), sizeof subj);
896 fseeko(infile, frompos, SEEK_SET);
900 * for possible use with Mail
902 strncpy(subj, "(No Subject)\n", sizeof subj);
908 if (*s) while (*s && !isspace(*s)) s++; /* skip over this field */
909 if (*s) while (*s && isspace(*s)) s++; /* find start of next field */