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] ?"
113 const char *sep = "-";
115 char fname[MAXPATHLEN];
116 char cmdbuf[MAXPATHLEN + MAXPATHLEN];
142 /* option initialization */
154 void ask __P((const char *));
155 void gfrsub __P((FILE *));
156 int linecnt __P((FILE *));
157 int main __P((int, char *[]));
158 int next __P((char *));
159 char *nxtfld __P((unsigned char *));
160 void onsusp __P((int));
161 void onintr __P((int));
162 void prmesg __P((int));
163 static void usage __P((void));
167 int argc; char *argv[];
170 int rcfirst = 0; /* first message to print (from .rc) */
171 int rcback = 0; /* amount to back off of rcfirst */
172 int firstmsg = 0, nextmsg = 0, lastmsg = 0;
174 struct stat buf; /* stat to check access of bounds */
178 setbuf(stdout, NULL);
180 setlocale(LC_ALL, "");
183 setuid(uid = getuid());
184 ruptible = (signal(SIGINT, SIG_IGN) == SIG_DFL);
186 signal(SIGINT, SIG_DFL);
190 if (isdigit(argv[0][0])) { /* starting message # */
191 rcfirst = atoi(argv[0]);
193 else if (isdigit(argv[0][1])) { /* backward offset */
194 rcback = atoi( &( argv[0][1] ) );
198 while (*ptr) switch (*ptr++) {
204 if (uid != SUPERUSER && uid != DAEMON) {
205 fprintf(stderr, "Sorry\n");
211 case 'f': /* silently */
215 case 'h': /* headers only */
219 case 'l': /* local msgs only */
223 case 'o': /* option to save last message */
227 case 'p': /* pipe thru 'more' during long msgs */
231 case 'q': /* query only */
235 case 's': /* sending TO msgs */
247 * determine current message bounds
249 snprintf(fname, sizeof(fname), "%s/%s", _PATH_MSGS, BOUNDS);
252 * Test access rights to the bounds file
253 * This can be a little tricky. if(send_msg), then
254 * we will create it. We assume that if(send_msg),
255 * then you have write permission there.
256 * Else, it better be there, or we bail.
258 if (send_msg != YES) {
259 if (stat(fname, &buf) < 0) {
261 err(errno, "%s", fname);
267 bounds = fopen(fname, "r");
269 if (bounds != NULL) {
270 fscanf(bounds, "%d %d\n", &firstmsg, &lastmsg);
272 blast = lastmsg; /* save upper bound */
276 keep = t - (rcback? rcback : NDAYS) DAYS;
278 if (clean || bounds == NULL) { /* relocate message bounds */
284 dirp = opendir(_PATH_MSGS);
286 err(errno, "%s", _PATH_MSGS);
291 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)){
292 register char *cp = dp->d_name;
297 if (dp->d_namlen == 0)
301 snprintf(inbuf, sizeof(inbuf), "%s/%s", _PATH_MSGS, cp);
304 i = i * 10 + *cp++ - '0';
306 continue; /* not a message! */
309 if (stat(inbuf, &stbuf) != 0)
311 if (stbuf.st_mtime < keep
312 && stbuf.st_mode&S_IWRITE) {
327 if (blast != 0) /* never lower the upper bound! */
329 firstmsg = lastmsg + 1;
331 else if (blast > lastmsg)
335 bounds = fopen(fname, "w");
337 err(errno, "%s", fname);
339 fprintf(bounds, "%d %d\n", firstmsg, lastmsg);
346 * Send mode - place msgs in _PATH_MSGS
348 bounds = fopen(fname, "w");
350 err(errno, "%s", fname);
352 nextmsg = lastmsg + 1;
353 snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, nextmsg);
354 newmsg = fopen(fname, "w");
356 err(errno, "%s", fname);
359 fprintf(bounds, "%d %d\n", firstmsg, nextmsg);
364 signal(SIGINT, onintr);
366 if (isatty(fileno(stdin))) {
367 ptr = getpwuid(uid)->pw_name;
368 printf("Message %d:\nFrom %s %sSubject: ",
369 nextmsg, ptr, ctime(&t));
371 fgets(inbuf, sizeof inbuf, stdin);
374 fprintf(newmsg, "From %s %sSubject: %s\n",
375 ptr, ctime(&t), inbuf);
376 blankline = seensubj = YES;
379 blankline = seensubj = NO;
381 fgets(inbuf, sizeof inbuf, stdin);
382 if (feof(stdin) || ferror(stdin))
384 blankline = (blankline || (inbuf[0] == '\n'));
385 seensubj = (seensubj || (!blankline && (strncmp(inbuf, "Subj", 4) == 0)));
386 fputs(inbuf, newmsg);
390 printf("NOTICE: Messages should have a Subject field!\n");
403 * prepare to display messages
405 totty = (isatty(fileno(stdout)) != 0);
406 use_pager = use_pager && totty;
408 snprintf(fname, sizeof(fname), "%s/%s", getenv("HOME"), MSGSRC);
409 msgsrc = fopen(fname, "r");
412 fscanf(msgsrc, "%d\n", &nextmsg);
414 if (nextmsg > lastmsg+1) {
415 printf("Warning: bounds have been reset (%d, %d)\n",
417 truncate(fname, (off_t)0);
421 rcfirst = nextmsg - rcback;
425 msgsrc = fopen(fname, "r+");
427 msgsrc = fopen(fname, "w");
429 err(errno, "%s", fname);
431 if (rcfirst > lastmsg+1) {
432 printf("Warning: the last message is number %d.\n",
436 if (rcfirst > firstmsg)
437 firstmsg = rcfirst; /* don't set below first msg */
441 fseek(msgsrc, 0L, 0);
442 fprintf(msgsrc, "%d\n", nextmsg);
449 if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1)
452 if (tgetent(inbuf, getenv("TERM")) <= 0
453 || (Lpp = tgetnum(__DECONST(char *, "li"))) <= 0) {
459 Lpp -= 6; /* for headers, etc. */
465 signal(SIGINT, onintr);
470 for (msg = firstmsg; msg <= lastmsg; msg++) {
472 snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, msg);
473 newmsg = fopen(fname, "r");
477 gfrsub(newmsg); /* get From and Subject fields */
478 if (locomode && !local) {
483 if (qopt) { /* This has to be located here */
484 printf("There are new messages.\n");
488 if (already && !hdrs)
495 signal(SIGTSTP, onsusp);
496 (void) setjmp(tstpbuf);
500 printf("Message %d:\nFrom %s %s", msg, from, date);
504 printf("Subject: %s", subj);
513 && fgets(inbuf, sizeof inbuf, newmsg)
514 && inbuf[0] != '\n') {
515 fputs(inbuf, stdout);
520 lct = linecnt(newmsg);
522 printf("(%d%slines) ", lct, seensubj? " " : " more ");
531 * Ask user for command
534 ask(lct? MORE : (msg==lastmsg? NOMORE : NEXT));
538 signal(SIGTSTP, SIG_DFL);
549 printf("--Postponed--\n");
551 /* intentional fall-thru */
554 if (msg >= nextmsg) sep = "Flushed";
560 use_pager = (*in++ == 'p');
561 /* intentional fallthru */
576 prmesg(nlines + lct + (seensubj? 1 : 0));
581 printf("--%s--\n", sep);
583 if (msg >= nextmsg) {
585 fseek(msgsrc, 0L, 0);
586 fprintf(msgsrc, "%d\n", nextmsg);
596 * Make sure .rc file gets updated
598 if (--msg >= nextmsg) {
600 fseek(msgsrc, 0L, 0);
601 fprintf(msgsrc, "%d\n", nextmsg);
604 if (already && !quitit && lastcmd && totty) {
606 * save or reply to last message?
610 if (inbuf[0] == '-' || isdigit(inbuf[0]))
613 if (!(already || hush || qopt))
614 printf("No new messages.\n");
621 fprintf(stderr, "usage: msgs [fhlopq] [[-]number]\n");
632 if (use_pager && length > Lpp) {
633 signal(SIGPIPE, SIG_IGN);
634 signal(SIGQUIT, SIG_IGN);
635 if ((env_pager = getenv("PAGER")) == NULL) {
636 snprintf(cmdbuf, sizeof(cmdbuf), _PATH_PAGER, Lpp);
638 snprintf(cmdbuf, sizeof(cmdbuf), "%s", env_pager);
640 outf = popen(cmdbuf, "w");
644 setbuf(outf, (char *)NULL);
652 while (fgets(inbuf, sizeof inbuf, newmsg)) {
660 if (outf != stdout) {
662 signal(SIGPIPE, SIG_DFL);
663 signal(SIGQUIT, SIG_DFL);
669 /* force wait on output */
670 tcdrain(fileno(stdout));
677 signal(SIGINT, onintr);
691 fseek(newmsg, 0L, 2);
697 * We have just gotten a susp. Suspend and prepare to resume.
703 signal(SIGTSTP, SIG_DFL);
706 signal(SIGTSTP, onsusp);
715 off_t oldpos = ftell(f);
719 while (fgets(lbuf, sizeof lbuf, f))
731 sscanf(buf, "%d", &i);
732 sprintf(buf, "Goto %d", i);
745 printf("%s ", prompt);
748 (void) fgets(inbuf, sizeof inbuf, stdin);
749 if ((n = strlen(inbuf)) > 0 && inbuf[n - 1] == '\n')
755 * Handle 'mail' and 'save' here.
757 if ((inch = inbuf[0]) == 's' || inch == 'm') {
760 else if (isdigit(inbuf[1]))
761 cmsg = atoi(&inbuf[1]);
764 snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, cmsg);
766 oldpos = ftell(newmsg);
768 cpfrom = fopen(fname, "r");
770 printf("Message %d not found\n", cmsg);
778 for (n=0; in[n] > ' '; n++) { /* sizeof fname? */
784 strcpy(fname, "Messages");
785 fd = open(fname, O_RDWR|O_EXCL|O_CREAT|O_APPEND);
788 strcpy(fname, _PATH_TMP);
791 snprintf(cmdbuf, sizeof(cmdbuf), _PATH_MAIL,
796 if (fd == -1 || (cpto = fdopen(fd, "a")) == NULL) {
801 fseek(newmsg, oldpos, 0);
806 while ((n = fread(inbuf, 1, sizeof inbuf, cpfrom)))
807 fwrite(inbuf, 1, n, cpto);
811 fseek(newmsg, oldpos, 0); /* reposition current message */
813 printf("Message %d saved in \"%s\"\n", cmsg, fname);
830 seensubj = seenfrom = NO;
832 subj[0] = from[0] = date[0] = NULL;
835 * Is this a normal message?
837 if (fgets(inbuf, sizeof inbuf, infile)) {
838 if (strncmp(inbuf, "From", 4)==0) {
840 * expected form starts with From
843 frompos = ftell(infile);
847 count = sizeof(from) - 1;
848 while (*in && *in > ' ' && count-- > 0) {
849 if (*in == ':' || *in == '@' ||
856 if (*(in = nxtfld(in)))
857 strncpy(date, in, sizeof date);
865 * not the expected form
867 fseek(infile, 0L, 0);
878 * look for Subject line until EOF or a blank line
880 while (fgets(inbuf, sizeof inbuf, infile)
881 && !(blankline = (inbuf[0] == '\n'))) {
883 * extract Subject line
885 if (!seensubj && strncmp(inbuf, "Subj", 4)==0) {
887 frompos = ftell(infile);
888 strncpy(subj, nxtfld(inbuf), sizeof subj);
895 fseek(infile, frompos, 0);
899 * for possible use with Mail
901 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 */