2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * msgs - a user bulletin board program
36 * msgs [fhlopq] [[-]number] to read messages
37 * msgs -s to place messages
38 * msgs -c [-days] to clean up the bulletin board
40 * prompt commands are:
42 * n flush message, go to next message
43 * q flush message, quit
44 * p print message, turn on 'pipe thru more' mode
45 * P print message, turn off 'pipe thru more' mode
46 * - reprint last message
47 * s[-][<num>] [<filename>] save message
48 * m[-][<num>] mail with message in temp mbox
49 * x exit without flushing this message
50 * <num> print message number <num>
53 #define V7 /* will look for TERM in the environment */
54 #define OBJECT /* will object to messages without Subjects */
55 /* #define REJECT */ /* will reject messages without Subjects
56 (OBJECT must be defined also) */
57 /* #define UNBUFFERED *//* use unbuffered output */
59 #include <sys/param.h>
78 #include "pathnames.h"
80 #define CMODE 0644 /* bounds file creation mode */
81 #define SUPERUSER 0 /* superuser uid */
82 #define DAEMON 1 /* daemon uid */
83 #define NLINES 24 /* default number of lines/crt screen */
84 #define NDAYS 21 /* default keep time for messages */
85 #define DAYS *24*60*60 /* seconds/day */
86 #define MSGSRC ".msgsrc" /* user's rc file */
87 #define BOUNDS "bounds" /* message bounds file */
88 #define NEXT "Next message? [yq]"
89 #define MORE "More? [ynq]"
90 #define NOMORE "(No more) [q] ?"
94 static const char *sep
= "-";
95 static char inbuf
[BUFSIZ
];
96 static char fname
[MAXPATHLEN
];
97 static char cmdbuf
[MAXPATHLEN
+ MAXPATHLEN
];
98 static char subj
[128];
99 static char from
[128];
100 static char date
[128];
104 static bool ruptible
;
106 static bool seenfrom
;
107 static bool seensubj
;
108 static bool blankline
;
109 static bool printing
= false;
110 static bool mailing
= false;
111 static bool quitit
= false;
112 static bool sending
= false;
113 static bool intrpflg
= false;
123 /* option initialization */
124 static bool hdrs
= false;
125 static bool qopt
= false;
126 static bool hush
= false;
127 static bool send_msg
= false;
128 static bool locomode
= false;
129 static bool use_pager
= false;
130 static bool clean
= false;
131 static bool lastcmd
= false;
132 static jmp_buf tstpbuf
;
134 static void ask(const char *);
135 static void gfrsub(FILE *);
136 static int linecnt(FILE *);
137 static int next(char *);
138 static char *nxtfld(char *);
139 static void onsusp(int);
140 static void onintr(int);
141 static void prmesg(int);
142 static void usage(void);
145 main(int argc
, char *argv
[])
148 int rcfirst
= 0; /* first message to print (from .rc) */
149 int rcback
= 0; /* amount to back off of rcfirst */
150 int firstmsg
= 0, nextmsg
= 0, lastmsg
= 0;
152 struct stat buf
; /* stat to check access of bounds */
157 setbuf(stdout
, NULL
);
159 setlocale(LC_ALL
, "");
162 if (setuid(uid
= getuid()) != 0)
163 err(1, "setuid failed");
164 ruptible
= (signal(SIGINT
, SIG_IGN
) == SIG_DFL
);
166 signal(SIGINT
, SIG_DFL
);
170 if (isdigit(argv
[0][0])) { /* starting message # */
171 rcfirst
= atoi(argv
[0]);
173 else if (isdigit(argv
[0][1])) { /* backward offset */
174 rcback
= atoi( &( argv
[0][1] ) );
178 while (*ptr
) switch (*ptr
++) {
184 if (uid
!= SUPERUSER
&& uid
!= DAEMON
)
186 "only the super-user can use the c flag");
190 case 'f': /* silently */
194 case 'h': /* headers only */
198 case 'l': /* local msgs only */
202 case 'o': /* option to save last message */
206 case 'p': /* pipe thru 'more' during long msgs */
210 case 'q': /* query only */
214 case 's': /* sending TO msgs */
226 * determine current message bounds
228 snprintf(fname
, sizeof(fname
), "%s/%s", _PATH_MSGS
, BOUNDS
);
231 * Test access rights to the bounds file
232 * This can be a little tricky. if(send_msg), then
233 * we will create it. We assume that if(send_msg),
234 * then you have write permission there.
235 * Else, it better be there, or we bail.
238 if (stat(fname
, &buf
) < 0) {
240 err(errno
, "%s", fname
);
246 bounds
= fopen(fname
, "r");
248 if (bounds
!= NULL
) {
249 fscanf(bounds
, "%d %d\n", &firstmsg
, &lastmsg
);
251 blast
= lastmsg
; /* save upper bound */
255 keep
= t
- (rcback
? rcback
: NDAYS
) DAYS
;
257 if (clean
|| bounds
== NULL
) { /* relocate message bounds */
260 bool seenany
= false;
263 dirp
= opendir(_PATH_MSGS
);
265 err(errno
, "%s", _PATH_MSGS
);
270 for (dp
= readdir(dirp
); dp
!= NULL
; dp
= readdir(dirp
)){
276 if (dp
->d_namlen
== 0)
280 snprintf(inbuf
, sizeof(inbuf
), "%s/%s", _PATH_MSGS
, cp
);
283 i
= i
* 10 + *cp
++ - '0';
285 continue; /* not a message! */
288 if (stat(inbuf
, &stbuf
) != 0)
290 if (stbuf
.st_mtime
< keep
291 && stbuf
.st_mode
&S_IWRITE
) {
306 if (blast
!= 0) /* never lower the upper bound! */
308 firstmsg
= lastmsg
+ 1;
310 else if (blast
> lastmsg
)
314 bounds
= fopen(fname
, "w");
316 err(errno
, "%s", fname
);
318 fprintf(bounds
, "%d %d\n", firstmsg
, lastmsg
);
325 * Send mode - place msgs in _PATH_MSGS
327 bounds
= fopen(fname
, "w");
329 err(errno
, "%s", fname
);
331 nextmsg
= lastmsg
+ 1;
332 snprintf(fname
, sizeof(fname
), "%s/%d", _PATH_MSGS
, nextmsg
);
333 newmsg
= fopen(fname
, "w");
335 err(errno
, "%s", fname
);
338 fprintf(bounds
, "%d %d\n", firstmsg
, nextmsg
);
343 signal(SIGINT
, onintr
);
345 if (isatty(fileno(stdin
))) {
346 ptr
= getpwuid(uid
)->pw_name
;
347 printf("Message %d:\nFrom %s %sSubject: ",
348 nextmsg
, ptr
, ctime(&t
));
350 fgets(inbuf
, sizeof inbuf
, stdin
);
353 fprintf(newmsg
, "From %s %sSubject: %s\n",
354 ptr
, ctime(&t
), inbuf
);
355 blankline
= seensubj
= true;
358 blankline
= seensubj
= false;
360 fgets(inbuf
, sizeof inbuf
, stdin
);
361 if (feof(stdin
) || ferror(stdin
))
363 blankline
= (blankline
|| (inbuf
[0] == '\n'));
364 seensubj
= (seensubj
|| (!blankline
&& (strncmp(inbuf
, "Subj", 4) == 0)));
365 fputs(inbuf
, newmsg
);
369 printf("NOTICE: Messages should have a Subject field!\n");
382 * prepare to display messages
384 totty
= (isatty(fileno(stdout
)) != 0);
385 use_pager
= use_pager
&& totty
;
387 if ((cp
= getenv("HOME")) == NULL
|| *cp
== '\0') {
388 fprintf(stderr
, "Error, no home directory!\n");
391 snprintf(fname
, sizeof(fname
), "%s/%s", cp
, MSGSRC
);
392 msgsrc
= fopen(fname
, "r");
395 fscanf(msgsrc
, "%d\n", &nextmsg
);
397 if (nextmsg
> lastmsg
+1) {
398 printf("Warning: bounds have been reset (%d, %d)\n",
400 truncate(fname
, (off_t
)0);
404 rcfirst
= nextmsg
- rcback
;
408 msgsrc
= fopen(fname
, "r+");
410 msgsrc
= fopen(fname
, "w");
412 err(errno
, "%s", fname
);
414 if (rcfirst
> lastmsg
+1) {
415 printf("Warning: the last message is number %d.\n",
419 if (rcfirst
> firstmsg
)
420 firstmsg
= rcfirst
; /* don't set below first msg */
425 fprintf(msgsrc
, "%d\n", nextmsg
);
432 if (ioctl(fileno(stdout
), TIOCGWINSZ
, &win
) != -1)
435 if (tgetent(inbuf
, getenv("TERM")) <= 0
436 || (Lpp
= tgetnum("li")) <= 0) {
442 Lpp
-= 6; /* for headers, etc. */
448 signal(SIGINT
, onintr
);
453 for (msg
= firstmsg
; msg
<= lastmsg
; msg
++) {
455 snprintf(fname
, sizeof(fname
), "%s/%d", _PATH_MSGS
, msg
);
456 newmsg
= fopen(fname
, "r");
460 gfrsub(newmsg
); /* get From and Subject fields */
461 if (locomode
&& !local
) {
466 if (qopt
) { /* This has to be located here */
467 printf("There are new messages.\n");
471 if (already
&& !hdrs
)
478 signal(SIGTSTP
, onsusp
);
479 (void) setjmp(tstpbuf
);
483 printf("Message %d:\nFrom %s %s", msg
, from
, date
);
487 printf("Subject: %s", subj
);
496 && fgets(inbuf
, sizeof inbuf
, newmsg
)
497 && inbuf
[0] != '\n') {
498 fputs(inbuf
, stdout
);
503 lct
= linecnt(newmsg
);
505 printf("(%d%sline%s) ", lct
, seensubj
? " " : " more ",
506 (lct
== 1) ? "" : "s");
515 * Ask user for command
518 ask(lct
? MORE
: (msg
==lastmsg
? NOMORE
: NEXT
));
522 signal(SIGTSTP
, SIG_DFL
);
536 printf("--Postponed--\n");
543 if (msg
>= nextmsg
) sep
= "Flushed";
550 use_pager
= (*in
++ == 'p');
567 prmesg(nlines
+ lct
+ (seensubj
? 1 : 0));
572 printf("--%s--\n", sep
);
574 if (msg
>= nextmsg
) {
577 fprintf(msgsrc
, "%d\n", nextmsg
);
587 * Make sure .rc file gets updated
589 if (--msg
>= nextmsg
) {
592 fprintf(msgsrc
, "%d\n", nextmsg
);
595 if (already
&& !quitit
&& lastcmd
&& totty
) {
597 * save or reply to last message?
601 if (inbuf
[0] == '-' || isdigit(inbuf
[0]))
604 if (!(already
|| hush
|| qopt
))
605 printf("No new messages.\n");
613 fprintf(stderr
, "usage: msgs [fhlopq] [[-]number]\n");
623 if (use_pager
&& length
> Lpp
) {
624 signal(SIGPIPE
, SIG_IGN
);
625 signal(SIGQUIT
, SIG_IGN
);
626 if ((env_pager
= getenv("PAGER")) == NULL
) {
627 snprintf(cmdbuf
, sizeof(cmdbuf
), _PATH_PAGER
, Lpp
);
629 snprintf(cmdbuf
, sizeof(cmdbuf
), "%s", env_pager
);
631 outf
= popen(cmdbuf
, "w");
635 setbuf(outf
, (char *)NULL
);
643 while (fgets(inbuf
, sizeof inbuf
, newmsg
)) {
651 if (outf
!= stdout
) {
653 signal(SIGPIPE
, SIG_DFL
);
654 signal(SIGQUIT
, SIG_DFL
);
660 /* force wait on output */
661 tcdrain(fileno(stdout
));
665 onintr(int unused __unused
)
667 signal(SIGINT
, onintr
);
681 fseeko(newmsg
, (off_t
)0, SEEK_END
);
687 * We have just gotten a susp. Suspend and prepare to resume.
690 onsusp(int unused __unused
)
692 signal(SIGTSTP
, SIG_DFL
);
695 signal(SIGTSTP
, onsusp
);
703 off_t oldpos
= ftello(f
);
707 while (fgets(lbuf
, sizeof lbuf
, f
))
710 fseeko(f
, oldpos
, SEEK_SET
);
718 sscanf(buf
, "%d", &i
);
719 sprintf(buf
, "Goto %d", i
);
724 ask(const char *prompt
)
731 printf("%s ", prompt
);
734 (void) fgets(inbuf
, sizeof inbuf
, stdin
);
735 if ((n
= strlen(inbuf
)) > 0 && inbuf
[n
- 1] == '\n')
741 * Handle 'mail' and 'save' here.
743 if ((inch
= inbuf
[0]) == 's' || inch
== 'm') {
746 else if (isdigit(inbuf
[1]))
747 cmsg
= atoi(&inbuf
[1]);
750 snprintf(fname
, sizeof(fname
), "%s/%d", _PATH_MSGS
, cmsg
);
752 oldpos
= ftello(newmsg
);
754 cpfrom
= fopen(fname
, "r");
756 printf("Message %d not found\n", cmsg
);
764 for (n
=0; in
[n
] > ' '; n
++) { /* sizeof fname? */
770 strcpy(fname
, "Messages");
771 fd
= open(fname
, O_RDWR
|O_EXCL
|O_CREAT
|O_APPEND
);
774 strcpy(fname
, _PATH_TMP
);
777 snprintf(cmdbuf
, sizeof(cmdbuf
), _PATH_MAIL
,
782 if (fd
== -1 || (cpto
= fdopen(fd
, "a")) == NULL
) {
787 fseeko(newmsg
, oldpos
, SEEK_SET
);
793 while ((n
= fread(inbuf
, 1, sizeof inbuf
, cpfrom
)))
794 fwrite(inbuf
, 1, n
, cpto
);
798 fseeko(newmsg
, oldpos
, SEEK_SET
);/* reposition current message */
800 printf("Message %d saved in \"%s\"\n", cmsg
, fname
);
816 seensubj
= seenfrom
= false;
818 subj
[0] = from
[0] = date
[0] = '\0';
821 * Is this a normal message?
823 if (fgets(inbuf
, sizeof inbuf
, infile
)) {
824 if (strncmp(inbuf
, "From", 4)==0) {
826 * expected form starts with From
829 frompos
= ftello(infile
);
833 count
= sizeof(from
) - 1;
834 while (*in
&& *in
> ' ' && count
-- > 0) {
835 if (*in
== ':' || *in
== '@' ||
842 if (*(in
= nxtfld(in
)))
843 strlcpy(date
, in
, sizeof date
);
851 * not the expected form
864 * look for Subject line until EOF or a blank line
866 while (fgets(inbuf
, sizeof inbuf
, infile
)
867 && !(blankline
= (inbuf
[0] == '\n'))) {
869 * extract Subject line
871 if (!seensubj
&& strncmp(inbuf
, "Subj", 4)==0) {
873 frompos
= ftello(infile
);
874 strlcpy(subj
, nxtfld(inbuf
), sizeof subj
);
881 fseeko(infile
, frompos
, SEEK_SET
);
885 * for possible use with Mail
887 strlcpy(subj
, "(No Subject)\n", sizeof subj
);
893 if (*s
) while (*s
&& !isspace(*s
)) s
++; /* skip over this field */
894 if (*s
) while (*s
&& isspace(*s
)) s
++; /* find start of next field */