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. 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
29 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)msgs.c 8.2 (Berkeley) 4/28/95
31 * $FreeBSD: src/usr.bin/msgs/msgs.c,v 1.15.2.2 2003/02/11 21:31:56 mike Exp $
32 * $DragonFly: src/usr.bin/msgs/msgs.c,v 1.7 2007/11/25 18:10:07 swildner Exp $
36 * msgs - a user bulletin board program
39 * msgs [fhlopq] [[-]number] to read messages
40 * msgs -s to place messages
41 * msgs -c [-days] to clean up the bulletin board
43 * prompt commands are:
45 * n flush message, go to next message
46 * q flush message, quit
47 * p print message, turn on 'pipe thru more' mode
48 * P print message, turn off 'pipe thru more' mode
49 * - reprint last message
50 * s[-][<num>] [<filename>] save message
51 * m[-][<num>] mail with message in temp mbox
52 * x exit without flushing this message
53 * <num> print message number <num>
56 #define V7 /* will look for TERM in the environment */
57 #define OBJECT /* will object to messages without Subjects */
58 /* #define REJECT */ /* will reject messages without Subjects
59 (OBJECT must be defined also) */
60 /* #define UNBUFFERED *//* use unbuffered output */
62 #include <sys/param.h>
80 #include "pathnames.h"
82 #define CMODE 0644 /* bounds file creation mode */
85 #define SUPERUSER 0 /* superuser uid */
86 #define DAEMON 1 /* daemon uid */
87 #define NLINES 24 /* default number of lines/crt screen */
88 #define NDAYS 21 /* default keep time for messages */
89 #define DAYS *24*60*60 /* seconds/day */
90 #define MSGSRC ".msgsrc" /* user's rc file */
91 #define BOUNDS "bounds" /* message bounds file */
92 #define NEXT "Next message? [yq]"
93 #define MORE "More? [ynq]"
94 #define NOMORE "(No more) [q] ?"
100 const char *sep
= "-";
102 char fname
[MAXPATHLEN
];
103 char cmdbuf
[MAXPATHLEN
+ MAXPATHLEN
];
129 /* option initialization */
141 void ask(const char *);
145 char *nxtfld(unsigned char *);
149 static void usage(void);
152 main(int argc
, char **argv
)
155 int rcfirst
= 0; /* first message to print (from .rc) */
156 int rcback
= 0; /* amount to back off of rcfirst */
157 int firstmsg
= 0, nextmsg
= 0, lastmsg
= 0;
159 struct stat buf
; /* stat to check access of bounds */
163 setbuf(stdout
, NULL
);
165 setlocale(LC_ALL
, "");
168 setuid(uid
= getuid());
169 ruptible
= (signal(SIGINT
, SIG_IGN
) == SIG_DFL
);
171 signal(SIGINT
, SIG_DFL
);
175 if (isdigit(argv
[0][0])) { /* starting message # */
176 rcfirst
= atoi(argv
[0]);
178 else if (isdigit(argv
[0][1])) { /* backward offset */
179 rcback
= atoi( &( argv
[0][1] ) );
183 while (*ptr
) switch (*ptr
++) {
189 if (uid
!= SUPERUSER
&& uid
!= DAEMON
) {
190 fprintf(stderr
, "Sorry\n");
196 case 'f': /* silently */
200 case 'h': /* headers only */
204 case 'l': /* local msgs only */
208 case 'o': /* option to save last message */
212 case 'p': /* pipe thru 'more' during long msgs */
216 case 'q': /* query only */
220 case 's': /* sending TO msgs */
232 * determine current message bounds
234 snprintf(fname
, sizeof(fname
), "%s/%s", _PATH_MSGS
, BOUNDS
);
237 * Test access rights to the bounds file
238 * This can be a little tricky. if(send_msg), then
239 * we will create it. We assume that if(send_msg),
240 * then you have write permission there.
241 * Else, it better be there, or we bail.
243 if (send_msg
!= YES
) {
244 if (stat(fname
, &buf
) < 0) {
246 err(errno
, "%s", fname
);
252 bounds
= fopen(fname
, "r");
254 if (bounds
!= NULL
) {
255 fscanf(bounds
, "%d %d\n", &firstmsg
, &lastmsg
);
257 blast
= lastmsg
; /* save upper bound */
261 keep
= t
- (rcback
? rcback
: NDAYS
) DAYS
;
263 if (clean
|| bounds
== NULL
) { /* relocate message bounds */
269 dirp
= opendir(_PATH_MSGS
);
271 err(errno
, "%s", _PATH_MSGS
);
276 for (dp
= readdir(dirp
); dp
!= NULL
; dp
= readdir(dirp
)){
277 char *cp
= dp
->d_name
;
284 snprintf(inbuf
, sizeof(inbuf
), "%s/%s", _PATH_MSGS
, cp
);
287 i
= i
* 10 + *cp
++ - '0';
289 continue; /* not a message! */
292 if (stat(inbuf
, &stbuf
) != 0)
294 if (stbuf
.st_mtime
< keep
295 && stbuf
.st_mode
&S_IWRITE
) {
310 if (blast
!= 0) /* never lower the upper bound! */
312 firstmsg
= lastmsg
+ 1;
314 else if (blast
> lastmsg
)
318 bounds
= fopen(fname
, "w");
320 err(errno
, "%s", fname
);
322 fprintf(bounds
, "%d %d\n", firstmsg
, lastmsg
);
329 * Send mode - place msgs in _PATH_MSGS
331 bounds
= fopen(fname
, "w");
333 err(errno
, "%s", fname
);
335 nextmsg
= lastmsg
+ 1;
336 snprintf(fname
, sizeof(fname
), "%s/%d", _PATH_MSGS
, nextmsg
);
337 newmsg
= fopen(fname
, "w");
339 err(errno
, "%s", fname
);
342 fprintf(bounds
, "%d %d\n", firstmsg
, nextmsg
);
347 signal(SIGINT
, onintr
);
349 if (isatty(fileno(stdin
))) {
350 ptr
= getpwuid(uid
)->pw_name
;
351 printf("Message %d:\nFrom %s %sSubject: ",
352 nextmsg
, ptr
, ctime(&t
));
354 fgets(inbuf
, sizeof inbuf
, stdin
);
357 fprintf(newmsg
, "From %s %sSubject: %s\n",
358 ptr
, ctime(&t
), inbuf
);
359 blankline
= seensubj
= YES
;
362 blankline
= seensubj
= NO
;
364 fgets(inbuf
, sizeof inbuf
, stdin
);
365 if (feof(stdin
) || ferror(stdin
))
367 blankline
= (blankline
|| (inbuf
[0] == '\n'));
368 seensubj
= (seensubj
|| (!blankline
&& (strncmp(inbuf
, "Subj", 4) == 0)));
369 fputs(inbuf
, newmsg
);
373 printf("NOTICE: Messages should have a Subject field!\n");
386 * prepare to display messages
388 totty
= (isatty(fileno(stdout
)) != 0);
389 use_pager
= use_pager
&& totty
;
391 snprintf(fname
, sizeof(fname
), "%s/%s", getenv("HOME"), 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 */
424 fseek(msgsrc
, 0L, 0);
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
);
533 printf("--Postponed--\n");
535 /* intentional fall-thru */
538 if (msg
>= nextmsg
) sep
= "Flushed";
544 use_pager
= (*in
++ == 'p');
545 /* intentional fallthru */
560 prmesg(nlines
+ lct
+ (seensubj
? 1 : 0));
565 printf("--%s--\n", sep
);
567 if (msg
>= nextmsg
) {
569 fseek(msgsrc
, 0L, 0);
570 fprintf(msgsrc
, "%d\n", nextmsg
);
580 * Make sure .rc file gets updated
582 if (--msg
>= nextmsg
) {
584 fseek(msgsrc
, 0L, 0);
585 fprintf(msgsrc
, "%d\n", nextmsg
);
588 if (already
&& !quitit
&& lastcmd
&& totty
) {
590 * save or reply to last message?
594 if (inbuf
[0] == '-' || isdigit(inbuf
[0]))
597 if (!(already
|| hush
|| qopt
))
598 printf("No new messages.\n");
605 fprintf(stderr
, "usage: msgs [fhlopq] [[-]number]\n");
615 if (use_pager
&& length
> Lpp
) {
616 signal(SIGPIPE
, SIG_IGN
);
617 signal(SIGQUIT
, SIG_IGN
);
618 if ((env_pager
= getenv("PAGER")) == NULL
) {
619 snprintf(cmdbuf
, sizeof(cmdbuf
), _PATH_PAGER
, Lpp
);
621 snprintf(cmdbuf
, sizeof(cmdbuf
), "%s", env_pager
);
623 outf
= popen(cmdbuf
, "w");
635 while (fgets(inbuf
, sizeof inbuf
, newmsg
)) {
643 if (outf
!= stdout
) {
645 signal(SIGPIPE
, SIG_DFL
);
646 signal(SIGQUIT
, SIG_DFL
);
652 /* force wait on output */
653 tcdrain(fileno(stdout
));
657 onintr(int unused __unused
)
659 signal(SIGINT
, onintr
);
673 fseek(newmsg
, 0L, 2);
679 * We have just gotten a susp. Suspend and prepare to resume.
682 onsusp(int unused __unused
)
684 signal(SIGTSTP
, SIG_DFL
);
687 signal(SIGTSTP
, onsusp
);
695 off_t oldpos
= ftell(f
);
699 while (fgets(lbuf
, sizeof lbuf
, f
))
710 sscanf(buf
, "%d", &i
);
711 sprintf(buf
, "Goto %d", i
);
716 ask(const char *prompt
)
723 printf("%s ", prompt
);
726 (void) fgets(inbuf
, sizeof inbuf
, stdin
);
727 if ((n
= strlen(inbuf
)) > 0 && inbuf
[n
- 1] == '\n')
733 * Handle 'mail' and 'save' here.
735 if ((inch
= inbuf
[0]) == 's' || inch
== 'm') {
738 else if (isdigit(inbuf
[1]))
739 cmsg
= atoi(&inbuf
[1]);
742 snprintf(fname
, sizeof(fname
), "%s/%d", _PATH_MSGS
, cmsg
);
744 oldpos
= ftell(newmsg
);
746 cpfrom
= fopen(fname
, "r");
748 printf("Message %d not found\n", cmsg
);
756 for (n
=0; in
[n
] > ' '; n
++) { /* sizeof fname? */
762 strcpy(fname
, "Messages");
763 fd
= open(fname
, O_RDWR
|O_EXCL
|O_CREAT
|O_APPEND
);
766 strcpy(fname
, _PATH_TMP
);
769 snprintf(cmdbuf
, sizeof(cmdbuf
), _PATH_MAIL
,
774 if (fd
== -1 || (cpto
= fdopen(fd
, "a")) == NULL
) {
779 fseek(newmsg
, oldpos
, 0);
784 while ((n
= fread(inbuf
, 1, sizeof inbuf
, cpfrom
)))
785 fwrite(inbuf
, 1, n
, cpto
);
789 fseek(newmsg
, oldpos
, 0); /* reposition current message */
791 printf("Message %d saved in \"%s\"\n", cmsg
, fname
);
807 seensubj
= seenfrom
= NO
;
809 subj
[0] = from
[0] = date
[0] = '\0';
812 * Is this a normal message?
814 if (fgets(inbuf
, sizeof inbuf
, infile
)) {
815 if (strncmp(inbuf
, "From", 4)==0) {
817 * expected form starts with From
820 frompos
= ftell(infile
);
824 count
= sizeof(from
) - 1;
825 while (*in
&& *in
> ' ' && count
-- > 0) {
826 if (*in
== ':' || *in
== '@' ||
833 if (*(in
= nxtfld(in
)))
834 strncpy(date
, in
, sizeof date
);
842 * not the expected form
844 fseek(infile
, 0L, 0);
855 * look for Subject line until EOF or a blank line
857 while (fgets(inbuf
, sizeof inbuf
, infile
)
858 && !(blankline
= (inbuf
[0] == '\n'))) {
860 * extract Subject line
862 if (!seensubj
&& strncmp(inbuf
, "Subj", 4)==0) {
864 frompos
= ftell(infile
);
865 strncpy(subj
, nxtfld(inbuf
), sizeof subj
);
872 fseek(infile
, frompos
, 0);
876 * for possible use with Mail
878 strncpy(subj
, "(No Subject)\n", sizeof subj
);
882 nxtfld(unsigned char *s
)
884 if (*s
) while (*s
&& !isspace(*s
)) s
++; /* skip over this field */
885 if (*s
) while (*s
&& isspace(*s
)) s
++; /* find start of next field */