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
33 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)msgs.c 8.2 (Berkeley) 4/28/95
35 * $FreeBSD: src/usr.bin/msgs/msgs.c,v 1.15.2.2 2003/02/11 21:31:56 mike Exp $
36 * $DragonFly: src/usr.bin/msgs/msgs.c,v 1.7 2007/11/25 18:10:07 swildner Exp $
40 * msgs - a user bulletin board program
43 * msgs [fhlopq] [[-]number] to read messages
44 * msgs -s to place messages
45 * msgs -c [-days] to clean up the bulletin board
47 * prompt commands are:
49 * n flush message, go to next message
50 * q flush message, quit
51 * p print message, turn on 'pipe thru more' mode
52 * P print message, turn off 'pipe thru more' mode
53 * - reprint last message
54 * s[-][<num>] [<filename>] save message
55 * m[-][<num>] mail with message in temp mbox
56 * x exit without flushing this message
57 * <num> print message number <num>
60 #define V7 /* will look for TERM in the environment */
61 #define OBJECT /* will object to messages without Subjects */
62 /* #define REJECT */ /* will reject messages without Subjects
63 (OBJECT must be defined also) */
64 /* #define UNBUFFERED *//* use unbuffered output */
66 #include <sys/param.h>
84 #include "pathnames.h"
86 #define CMODE 0644 /* bounds file creation mode */
89 #define SUPERUSER 0 /* superuser uid */
90 #define DAEMON 1 /* daemon uid */
91 #define NLINES 24 /* default number of lines/crt screen */
92 #define NDAYS 21 /* default keep time for messages */
93 #define DAYS *24*60*60 /* seconds/day */
94 #define MSGSRC ".msgsrc" /* user's rc file */
95 #define BOUNDS "bounds" /* message bounds file */
96 #define NEXT "Next message? [yq]"
97 #define MORE "More? [ynq]"
98 #define NOMORE "(No more) [q] ?"
104 const char *sep
= "-";
106 char fname
[MAXPATHLEN
];
107 char cmdbuf
[MAXPATHLEN
+ MAXPATHLEN
];
133 /* option initialization */
145 void ask(const char *);
149 char *nxtfld(unsigned char *);
153 static void usage(void);
156 main(int argc
, char **argv
)
159 int rcfirst
= 0; /* first message to print (from .rc) */
160 int rcback
= 0; /* amount to back off of rcfirst */
161 int firstmsg
= 0, nextmsg
= 0, lastmsg
= 0;
163 struct stat buf
; /* stat to check access of bounds */
167 setbuf(stdout
, NULL
);
169 setlocale(LC_ALL
, "");
172 setuid(uid
= getuid());
173 ruptible
= (signal(SIGINT
, SIG_IGN
) == SIG_DFL
);
175 signal(SIGINT
, SIG_DFL
);
179 if (isdigit(argv
[0][0])) { /* starting message # */
180 rcfirst
= atoi(argv
[0]);
182 else if (isdigit(argv
[0][1])) { /* backward offset */
183 rcback
= atoi( &( argv
[0][1] ) );
187 while (*ptr
) switch (*ptr
++) {
193 if (uid
!= SUPERUSER
&& uid
!= DAEMON
) {
194 fprintf(stderr
, "Sorry\n");
200 case 'f': /* silently */
204 case 'h': /* headers only */
208 case 'l': /* local msgs only */
212 case 'o': /* option to save last message */
216 case 'p': /* pipe thru 'more' during long msgs */
220 case 'q': /* query only */
224 case 's': /* sending TO msgs */
236 * determine current message bounds
238 snprintf(fname
, sizeof(fname
), "%s/%s", _PATH_MSGS
, BOUNDS
);
241 * Test access rights to the bounds file
242 * This can be a little tricky. if(send_msg), then
243 * we will create it. We assume that if(send_msg),
244 * then you have write permission there.
245 * Else, it better be there, or we bail.
247 if (send_msg
!= YES
) {
248 if (stat(fname
, &buf
) < 0) {
250 err(errno
, "%s", fname
);
256 bounds
= fopen(fname
, "r");
258 if (bounds
!= NULL
) {
259 fscanf(bounds
, "%d %d\n", &firstmsg
, &lastmsg
);
261 blast
= lastmsg
; /* save upper bound */
265 keep
= t
- (rcback
? rcback
: NDAYS
) DAYS
;
267 if (clean
|| bounds
== NULL
) { /* relocate message bounds */
273 dirp
= opendir(_PATH_MSGS
);
275 err(errno
, "%s", _PATH_MSGS
);
280 for (dp
= readdir(dirp
); dp
!= NULL
; dp
= readdir(dirp
)){
281 char *cp
= dp
->d_name
;
288 snprintf(inbuf
, sizeof(inbuf
), "%s/%s", _PATH_MSGS
, cp
);
291 i
= i
* 10 + *cp
++ - '0';
293 continue; /* not a message! */
296 if (stat(inbuf
, &stbuf
) != 0)
298 if (stbuf
.st_mtime
< keep
299 && stbuf
.st_mode
&S_IWRITE
) {
314 if (blast
!= 0) /* never lower the upper bound! */
316 firstmsg
= lastmsg
+ 1;
318 else if (blast
> lastmsg
)
322 bounds
= fopen(fname
, "w");
324 err(errno
, "%s", fname
);
326 fprintf(bounds
, "%d %d\n", firstmsg
, lastmsg
);
333 * Send mode - place msgs in _PATH_MSGS
335 bounds
= fopen(fname
, "w");
337 err(errno
, "%s", fname
);
339 nextmsg
= lastmsg
+ 1;
340 snprintf(fname
, sizeof(fname
), "%s/%d", _PATH_MSGS
, nextmsg
);
341 newmsg
= fopen(fname
, "w");
343 err(errno
, "%s", fname
);
346 fprintf(bounds
, "%d %d\n", firstmsg
, nextmsg
);
351 signal(SIGINT
, onintr
);
353 if (isatty(fileno(stdin
))) {
354 ptr
= getpwuid(uid
)->pw_name
;
355 printf("Message %d:\nFrom %s %sSubject: ",
356 nextmsg
, ptr
, ctime(&t
));
358 fgets(inbuf
, sizeof inbuf
, stdin
);
361 fprintf(newmsg
, "From %s %sSubject: %s\n",
362 ptr
, ctime(&t
), inbuf
);
363 blankline
= seensubj
= YES
;
366 blankline
= seensubj
= NO
;
368 fgets(inbuf
, sizeof inbuf
, stdin
);
369 if (feof(stdin
) || ferror(stdin
))
371 blankline
= (blankline
|| (inbuf
[0] == '\n'));
372 seensubj
= (seensubj
|| (!blankline
&& (strncmp(inbuf
, "Subj", 4) == 0)));
373 fputs(inbuf
, newmsg
);
377 printf("NOTICE: Messages should have a Subject field!\n");
390 * prepare to display messages
392 totty
= (isatty(fileno(stdout
)) != 0);
393 use_pager
= use_pager
&& totty
;
395 snprintf(fname
, sizeof(fname
), "%s/%s", getenv("HOME"), MSGSRC
);
396 msgsrc
= fopen(fname
, "r");
399 fscanf(msgsrc
, "%d\n", &nextmsg
);
401 if (nextmsg
> lastmsg
+1) {
402 printf("Warning: bounds have been reset (%d, %d)\n",
404 truncate(fname
, (off_t
)0);
408 rcfirst
= nextmsg
- rcback
;
412 msgsrc
= fopen(fname
, "r+");
414 msgsrc
= fopen(fname
, "w");
416 err(errno
, "%s", fname
);
418 if (rcfirst
> lastmsg
+1) {
419 printf("Warning: the last message is number %d.\n",
423 if (rcfirst
> firstmsg
)
424 firstmsg
= rcfirst
; /* don't set below first msg */
428 fseek(msgsrc
, 0L, 0);
429 fprintf(msgsrc
, "%d\n", nextmsg
);
436 if (ioctl(fileno(stdout
), TIOCGWINSZ
, &win
) != -1)
439 if (tgetent(inbuf
, getenv("TERM")) <= 0
440 || (Lpp
= tgetnum("li")) <= 0) {
446 Lpp
-= 6; /* for headers, etc. */
452 signal(SIGINT
, onintr
);
457 for (msg
= firstmsg
; msg
<= lastmsg
; msg
++) {
459 snprintf(fname
, sizeof(fname
), "%s/%d", _PATH_MSGS
, msg
);
460 newmsg
= fopen(fname
, "r");
464 gfrsub(newmsg
); /* get From and Subject fields */
465 if (locomode
&& !local
) {
470 if (qopt
) { /* This has to be located here */
471 printf("There are new messages.\n");
475 if (already
&& !hdrs
)
482 signal(SIGTSTP
, onsusp
);
483 (void) setjmp(tstpbuf
);
487 printf("Message %d:\nFrom %s %s", msg
, from
, date
);
491 printf("Subject: %s", subj
);
500 && fgets(inbuf
, sizeof inbuf
, newmsg
)
501 && inbuf
[0] != '\n') {
502 fputs(inbuf
, stdout
);
507 lct
= linecnt(newmsg
);
509 printf("(%d%sline%s) ", lct
, seensubj
? " " : " more ",
510 (lct
== 1) ? "" : "s");
519 * Ask user for command
522 ask(lct
? MORE
: (msg
==lastmsg
? NOMORE
: NEXT
));
526 signal(SIGTSTP
, SIG_DFL
);
537 printf("--Postponed--\n");
539 /* intentional fall-thru */
542 if (msg
>= nextmsg
) sep
= "Flushed";
548 use_pager
= (*in
++ == 'p');
549 /* intentional fallthru */
564 prmesg(nlines
+ lct
+ (seensubj
? 1 : 0));
569 printf("--%s--\n", sep
);
571 if (msg
>= nextmsg
) {
573 fseek(msgsrc
, 0L, 0);
574 fprintf(msgsrc
, "%d\n", nextmsg
);
584 * Make sure .rc file gets updated
586 if (--msg
>= nextmsg
) {
588 fseek(msgsrc
, 0L, 0);
589 fprintf(msgsrc
, "%d\n", nextmsg
);
592 if (already
&& !quitit
&& lastcmd
&& totty
) {
594 * save or reply to last message?
598 if (inbuf
[0] == '-' || isdigit(inbuf
[0]))
601 if (!(already
|| hush
|| qopt
))
602 printf("No new messages.\n");
609 fprintf(stderr
, "usage: msgs [fhlopq] [[-]number]\n");
619 if (use_pager
&& length
> Lpp
) {
620 signal(SIGPIPE
, SIG_IGN
);
621 signal(SIGQUIT
, SIG_IGN
);
622 if ((env_pager
= getenv("PAGER")) == NULL
) {
623 snprintf(cmdbuf
, sizeof(cmdbuf
), _PATH_PAGER
, Lpp
);
625 snprintf(cmdbuf
, sizeof(cmdbuf
), "%s", env_pager
);
627 outf
= popen(cmdbuf
, "w");
639 while (fgets(inbuf
, sizeof inbuf
, newmsg
)) {
647 if (outf
!= stdout
) {
649 signal(SIGPIPE
, SIG_DFL
);
650 signal(SIGQUIT
, SIG_DFL
);
656 /* force wait on output */
657 tcdrain(fileno(stdout
));
661 onintr(int unused __unused
)
663 signal(SIGINT
, onintr
);
677 fseek(newmsg
, 0L, 2);
683 * We have just gotten a susp. Suspend and prepare to resume.
686 onsusp(int unused __unused
)
688 signal(SIGTSTP
, SIG_DFL
);
691 signal(SIGTSTP
, onsusp
);
699 off_t oldpos
= ftell(f
);
703 while (fgets(lbuf
, sizeof lbuf
, f
))
714 sscanf(buf
, "%d", &i
);
715 sprintf(buf
, "Goto %d", i
);
720 ask(const char *prompt
)
727 printf("%s ", prompt
);
730 (void) fgets(inbuf
, sizeof inbuf
, stdin
);
731 if ((n
= strlen(inbuf
)) > 0 && inbuf
[n
- 1] == '\n')
737 * Handle 'mail' and 'save' here.
739 if ((inch
= inbuf
[0]) == 's' || inch
== 'm') {
742 else if (isdigit(inbuf
[1]))
743 cmsg
= atoi(&inbuf
[1]);
746 snprintf(fname
, sizeof(fname
), "%s/%d", _PATH_MSGS
, cmsg
);
748 oldpos
= ftell(newmsg
);
750 cpfrom
= fopen(fname
, "r");
752 printf("Message %d not found\n", cmsg
);
760 for (n
=0; in
[n
] > ' '; n
++) { /* sizeof fname? */
766 strcpy(fname
, "Messages");
767 fd
= open(fname
, O_RDWR
|O_EXCL
|O_CREAT
|O_APPEND
);
770 strcpy(fname
, _PATH_TMP
);
773 snprintf(cmdbuf
, sizeof(cmdbuf
), _PATH_MAIL
,
778 if (fd
== -1 || (cpto
= fdopen(fd
, "a")) == NULL
) {
783 fseek(newmsg
, oldpos
, 0);
788 while ((n
= fread(inbuf
, 1, sizeof inbuf
, cpfrom
)))
789 fwrite(inbuf
, 1, n
, cpto
);
793 fseek(newmsg
, oldpos
, 0); /* reposition current message */
795 printf("Message %d saved in \"%s\"\n", cmsg
, fname
);
811 seensubj
= seenfrom
= NO
;
813 subj
[0] = from
[0] = date
[0] = '\0';
816 * Is this a normal message?
818 if (fgets(inbuf
, sizeof inbuf
, infile
)) {
819 if (strncmp(inbuf
, "From", 4)==0) {
821 * expected form starts with From
824 frompos
= ftell(infile
);
828 count
= sizeof(from
) - 1;
829 while (*in
&& *in
> ' ' && count
-- > 0) {
830 if (*in
== ':' || *in
== '@' ||
837 if (*(in
= nxtfld(in
)))
838 strncpy(date
, in
, sizeof date
);
846 * not the expected form
848 fseek(infile
, 0L, 0);
859 * look for Subject line until EOF or a blank line
861 while (fgets(inbuf
, sizeof inbuf
, infile
)
862 && !(blankline
= (inbuf
[0] == '\n'))) {
864 * extract Subject line
866 if (!seensubj
&& strncmp(inbuf
, "Subj", 4)==0) {
868 frompos
= ftell(infile
);
869 strncpy(subj
, nxtfld(inbuf
), sizeof subj
);
876 fseek(infile
, frompos
, 0);
880 * for possible use with Mail
882 strncpy(subj
, "(No Subject)\n", sizeof subj
);
886 nxtfld(unsigned char *s
)
888 if (*s
) while (*s
&& !isspace(*s
)) s
++; /* skip over this field */
889 if (*s
) while (*s
&& isspace(*s
)) s
++; /* find start of next field */