1 /* $NetBSD: msgs.c,v 1.19 2008/07/21 14:19:24 lukem Exp $ */
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
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)msgs.c 8.2 (Berkeley) 4/28/95";
42 __RCSID("$NetBSD: msgs.c,v 1.19 2008/07/21 14:19:24 lukem Exp $");
47 * msgs - a user bulletin board program
50 * msgs [fhlopqr] [[-]number] to read messages
51 * msgs -s to place messages
52 * msgs -c [-days] to clean up the bulletin board
54 * prompt commands are:
56 * n flush message, go to next message
57 * q flush message, quit
58 * p print message, turn on 'pipe thru more' mode
59 * P print message, turn off 'pipe thru more' mode
60 * - reprint last message
61 * s[-][<num>] [<filename>] save message
62 * m[-][<num>] mail with message in temp mbox
63 * x exit without flushing this message
64 * <num> print message number <num>
67 #define V7 /* will look for TERM in the environment */
68 #define OBJECT /* will object to messages without Subjects */
69 #define REJECT /* will reject messages without Subjects
70 (OBJECT must be defined also) */
71 /*#define UNBUFFERED */ /* use unbuffered output */
73 #include <sys/param.h>
74 #include <sys/ioctl.h>
92 #include "pathnames.h"
94 #define CMODE 0664 /* bounds file creation mode */
97 #define SUPERUSER 0 /* superuser uid */
98 #define DAEMON 1 /* daemon uid */
99 #define NLINES 24 /* default number of lines/crt screen */
100 #define NDAYS 21 /* default keep time for messages */
101 #define DAYS *24*60*60 /* seconds/day */
102 #define MSGSRC ".msgsrc" /* user's rc file */
103 #define BOUNDS "bounds" /* message bounds file */
104 #define NEXT "Next message? [yq]"
105 #define MORE "More? [ynq]"
106 #define NOMORE "(No more) [q] ?"
112 const char *sep
= "-";
114 char fname
[MAXPATHLEN
];
115 char cmdbuf
[MAXPATHLEN
+ 16];
132 bool restricted
= NO
;
142 void ask
__P((const char *));
143 void gfrsub
__P((FILE *));
144 int linecnt
__P((FILE *));
145 int main
__P((int, char *[]));
146 int next
__P((char *));
147 char *nxtfld
__P((char *));
148 void onintr
__P((int));
149 void onsusp
__P((int));
150 void prmesg
__P((int));
152 /* option initialization */
165 int argc
; char *argv
[];
168 int rcfirst
= 0; /* first message to print (from .rc) */
169 int rcback
= 0; /* amount to back off of rcfirst */
170 int firstmsg
, nextmsg
, lastmsg
= 0;
176 setbuf(stdout
, NULL
);
180 setuid(uid
= getuid());
181 ruptible
= (signal(SIGINT
, SIG_IGN
) == SIG_DFL
);
183 signal(SIGINT
, SIG_DFL
);
187 if (isdigit((unsigned char)argv
[0][0])) {/* starting message # */
188 rcfirst
= atoi(argv
[0]);
190 else if (isdigit((unsigned char)argv
[0][1])) {/* backward offset */
191 rcback
= atoi( &( argv
[0][1] ) );
195 while (*ptr
) switch (*ptr
++) {
201 if (uid
!= SUPERUSER
&& uid
!= DAEMON
) {
202 fprintf(stderr
, "Sorry\n");
208 case 'f': /* silently */
212 case 'h': /* headers only */
216 case 'l': /* local msgs only */
220 case 'o': /* option to save last message */
224 case 'p': /* pipe thru 'more' during long msgs */
228 case 'q': /* query only */
232 case 'r': /* restricted */
237 case 's': /* sending TO msgs */
243 "usage: msgs [cfhlopqrs] [[-]number]\n");
251 * determine current message bounds
253 snprintf(fname
, sizeof (fname
), "%s/%s", _PATH_MSGS
, BOUNDS
);
254 bounds
= fopen(fname
, "r");
256 if (bounds
!= NULL
) {
257 if (fscanf(bounds
, "%d %d\n", &firstmsg
, &lastmsg
) < 2)
258 firstmsg
= lastmsg
= 0;
260 blast
= lastmsg
; /* save upper bound */
264 keep
= t
- (rcback
? rcback
: NDAYS
) DAYS
;
266 if (clean
|| bounds
== NULL
) { /* relocate message bounds */
272 dirp
= opendir(_PATH_MSGS
);
282 for (dp
= readdir(dirp
); dp
!= NULL
; dp
= readdir(dirp
)){
283 char *cp
= dp
->d_name
;
289 if (dp
->d_namlen
== 0)
294 snprintf(inbuf
, sizeof (inbuf
), "%s/%s",
297 while (isdigit((unsigned char)*cp
))
298 i
= i
* 10 + *cp
++ - '0';
300 continue; /* not a message! */
303 if (stat(inbuf
, &stbuf
) != 0)
305 if (stbuf
.st_mtime
< keep
306 && stbuf
.st_mode
&S_IWRITE
) {
321 if (blast
!= 0) /* never lower the upper bound! */
323 firstmsg
= lastmsg
+ 1;
325 else if (blast
> lastmsg
)
329 bounds
= fopen(fname
, "w");
330 if (bounds
== NULL
) {
335 fprintf(bounds
, "%d %d\n", firstmsg
, lastmsg
);
342 * Send mode - place msgs in _PATH_MSGS
344 bounds
= fopen(fname
, "w");
345 if (bounds
== NULL
) {
350 nextmsg
= lastmsg
+ 1;
351 snprintf(fname
, sizeof (fname
), "%s/%d", _PATH_MSGS
, nextmsg
);
352 newmsg
= fopen(fname
, "w");
353 if (newmsg
== NULL
) {
359 fprintf(bounds
, "%d %d\n", firstmsg
, nextmsg
);
364 signal(SIGINT
, onintr
);
366 if (isatty(fileno(stdin
))) {
369 fprintf(stderr
, "Who are you?\n");
372 printf("Message %d:\nFrom %s %sSubject: ",
373 nextmsg
, pw
->pw_name
, ctime(&t
));
375 fgets(inbuf
, sizeof inbuf
, stdin
);
378 fprintf(newmsg
, "From %s %sSubject: %s\n",
379 ptr
, ctime(&t
), inbuf
);
380 blankline
= seensubj
= YES
;
383 blankline
= seensubj
= NO
;
385 fgets(inbuf
, sizeof inbuf
, stdin
);
386 if (feof(stdin
) || ferror(stdin
))
388 blankline
= (blankline
|| (inbuf
[0] == '\n'));
389 seensubj
= (seensubj
|| (!blankline
&& (strncmp(inbuf
, "Subj", 4) == 0)));
390 fputs(inbuf
, newmsg
);
394 printf("NOTICE: Messages should have a Subject field!\n");
407 * prepare to display messages
409 totty
= (isatty(fileno(stdout
)) != 0);
410 use_pager
= use_pager
&& totty
;
413 char *home
= getenv("HOME");
414 if(home
== NULL
|| *home
== '\0')
415 errx(1, "$HOME not set");
416 snprintf(fname
, sizeof (fname
), "%s/%s", home
, MSGSRC
);
418 msgsrc
= fopen(fname
, "r");
421 fscanf(msgsrc
, "%d\n", &nextmsg
);
423 if (nextmsg
> lastmsg
+1) {
424 printf("Warning: bounds have been reset (%d, %d)\n",
426 truncate(fname
, (off_t
)0);
430 rcfirst
= nextmsg
- rcback
;
434 msgsrc
= fopen(fname
, "r+");
436 msgsrc
= fopen(fname
, "w");
437 if (msgsrc
== NULL
) {
442 if (rcfirst
> lastmsg
+1) {
443 printf("Warning: the last message is number %d.\n",
447 if (rcfirst
> firstmsg
)
448 firstmsg
= rcfirst
; /* don't set below first msg */
452 fseek(msgsrc
, 0L, 0);
453 fprintf(msgsrc
, "%d\n", nextmsg
);
460 if (ioctl(fileno(stdout
), TIOCGWINSZ
, &win
) != -1)
463 if (tgetent(inbuf
, getenv("TERM")) <= 0
464 || (Lpp
= tgetnum("li")) <= 0) {
470 Lpp
-= 6; /* for headers, etc. */
476 signal(SIGINT
, onintr
);
481 for (msg
= firstmsg
; msg
<= lastmsg
; msg
++) {
483 snprintf(fname
, sizeof (fname
), "%s/%d", _PATH_MSGS
, msg
);
484 newmsg
= fopen(fname
, "r");
488 gfrsub(newmsg
); /* get From and Subject fields */
489 if (locomode
&& !local
) {
494 if (qopt
) { /* This has to be located here */
495 printf("There are new messages.\n");
499 if (already
&& !hdrs
)
506 signal(SIGTSTP
, onsusp
);
507 (void) setjmp(tstpbuf
);
511 printf("Message %d:\nFrom %s %s", msg
, from
, date
);
515 printf("Subject: %s", subj
);
524 && fgets(inbuf
, sizeof inbuf
, newmsg
)
525 && inbuf
[0] != '\n') {
526 fputs(inbuf
, stdout
);
531 lct
= linecnt(newmsg
);
533 printf("(%d%slines) ", lct
, seensubj
? " " : " more ");
542 * Ask user for command
545 ask(lct
? MORE
: (msg
==lastmsg
? NOMORE
: NEXT
));
549 signal(SIGTSTP
, SIG_DFL
);
560 printf("--Postponed--\n");
562 /* intentional fall-thru */
565 if (msg
>= nextmsg
) sep
= "Flushed";
571 use_pager
= (*in
++ == 'p');
572 /* intentional fallthru */
581 if (isdigit((unsigned char)*in
)) {
587 prmesg(nlines
+ lct
+ (seensubj
? 1 : 0));
592 printf("--%s--\n", sep
);
594 if (msg
>= nextmsg
) {
596 fseek(msgsrc
, 0L, 0);
597 fprintf(msgsrc
, "%d\n", nextmsg
);
607 * Make sure .rc file gets updated
609 if (--msg
>= nextmsg
) {
611 fseek(msgsrc
, 0L, 0);
612 fprintf(msgsrc
, "%d\n", nextmsg
);
615 if (already
&& !quitit
&& lastcmd
&& totty
) {
617 * save or reply to last message?
621 if (inbuf
[0] == '-' || isdigit((unsigned char)inbuf
[0]))
624 if (!(already
|| hush
|| qopt
))
625 printf("No new messages.\n");
636 if (use_pager
&& length
> Lpp
) {
637 signal(SIGPIPE
, SIG_IGN
);
638 signal(SIGQUIT
, SIG_IGN
);
639 if ((env_pager
= getenv("PAGER")) == NULL
||
640 env_pager
[0] == '\0') {
641 snprintf(cmdbuf
, sizeof(cmdbuf
), "%s -z%d",
644 strlcpy(cmdbuf
, env_pager
, sizeof (cmdbuf
));
646 outf
= popen(cmdbuf
, "w");
650 setbuf(outf
, (char *)NULL
);
658 while (fgets(inbuf
, sizeof inbuf
, newmsg
)) {
666 if (outf
!= stdout
) {
668 signal(SIGPIPE
, SIG_DFL
);
669 signal(SIGQUIT
, SIG_DFL
);
675 /* trick to force wait on output */
676 tcdrain(fileno(stdout
));
683 signal(SIGINT
, onintr
);
697 fseek(newmsg
, 0L, 2);
703 * We have just gotten a susp. Suspend and prepare to resume.
710 signal(SIGTSTP
, SIG_DFL
);
713 signal(SIGTSTP
, onsusp
);
722 off_t oldpos
= ftell(f
);
726 while (fgets(lbuf
, sizeof lbuf
, f
))
738 sscanf(buf
, "%d", &i
);
739 snprintf(buf
, sizeof (buf
), "Goto %d", i
);
752 printf("%s ", prompt
);
755 (void) fgets(inbuf
, sizeof inbuf
, stdin
);
756 if ((n
= strlen(inbuf
)) > 0 && inbuf
[n
- 1] == '\n')
762 * Handle 'mail' and 'save' here.
764 if (((inch
= inbuf
[0]) == 's' || inch
== 'm') && !restricted
) {
767 else if (isdigit((unsigned char)inbuf
[1]))
768 cmsg
= atoi(&inbuf
[1]);
771 snprintf(fname
, sizeof (fname
), "%s/%d", _PATH_MSGS
, cmsg
);
773 oldpos
= ftell(newmsg
);
775 cpfrom
= fopen(fname
, "r");
777 printf("Message %d not found\n", cmsg
);
785 for (n
=0; in
[n
] > ' '; n
++) { /* sizeof fname? */
791 strlcpy(fname
, "Messages", sizeof(fname
));
796 strlcpy(fname
, _PATH_TMP
, sizeof(fname
));
799 err(1, "mkstemp failed");
801 snprintf(cmdbuf
, sizeof (cmdbuf
), _PATH_MAIL
, fname
);
804 cpto
= fopen(fname
, "a");
808 fseek(newmsg
, oldpos
, 0);
813 while ((n
= fread(inbuf
, 1, sizeof inbuf
, cpfrom
)) != 0)
814 fwrite(inbuf
, 1, n
, cpto
);
818 fseek(newmsg
, oldpos
, 0); /* reposition current message */
820 printf("Message %d saved in \"%s\"\n", cmsg
, fname
);
836 seensubj
= seenfrom
= NO
;
838 subj
[0] = from
[0] = date
[0] = 0;
841 * Is this a normal message?
843 if (fgets(inbuf
, sizeof inbuf
, infile
)) {
844 if (strncmp(inbuf
, "From", 4)==0) {
846 * expected form starts with From
849 frompos
= ftell(infile
);
852 if (*in
) while (*in
&& *in
> ' ') {
853 if (*in
== ':' || *in
== '@' || *in
== '!')
856 /* what about sizeof from ? */
859 if (*(in
= nxtfld(in
)))
860 strncpy(date
, in
, sizeof date
);
868 * not the expected form
870 fseek(infile
, 0L, 0);
881 * look for Subject line until EOF or a blank line
883 while (fgets(inbuf
, sizeof inbuf
, infile
)
884 && !(blankline
= (inbuf
[0] == '\n'))) {
886 * extract Subject line
888 if (!seensubj
&& strncmp(inbuf
, "Subj", 4)==0) {
890 frompos
= ftell(infile
);
891 strncpy(subj
, nxtfld(inbuf
), sizeof subj
);
898 fseek(infile
, frompos
, 0);
902 * for possible use with Mail
904 strncpy(subj
, "(No Subject)\n", sizeof subj
);
911 if (*s
) while (*s
&& *s
> ' ') s
++; /* skip over this field */
912 if (*s
) while (*s
&& *s
<= ' ') s
++; /* find start of next field */