2 * S-nail - a mail user agent derived from Berkeley Mail.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 Steffen "Daode" Nurpmeso.
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * Mail -- a mail program
53 * Print the current active headings.
54 * Don't change dot if invoker didn't give an argument.
58 static void onpipe(int signo
);
59 static int dispc(struct message
*mp
, const char *a
);
60 static int scroll1(char *arg
, int onlynew
);
61 static void hprf(const char *fmt
, int mesg
, FILE *f
, int threaded
,
62 const char *attrlist
);
63 static int putindent(FILE *fp
, struct message
*mp
, int maxwidth
);
64 static int type1(int *msgvec
, int doign
, int page
, int pipe
, int decode
,
65 char *cmd
, off_t
*tstats
);
66 static int pipe1(char *str
, int doign
);
67 void brokpipe(int signo
);
75 if (cp
== NULL
|| *cp
== '\0')
76 cp
= value("bsdcompat") ? "more" : "pg";
84 int g
, k
, n
, mesg
, flag
= 0, lastg
= 1;
85 struct message
*mp
, *mq
, *lastmq
= NULL
;
87 enum mflag fl
= MNEW
|MFLAGGED
;
90 n
= msgvec
[0]; /* n == {-2, -1, 0}: called from scroll() */
98 if (mb
.mb_threaded
== 0) {
101 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
109 if ((n
> 0 && mp
== &message
[n
-1]) ||
110 (n
== 0 && g
== k
) ||
111 (n
== -2 && g
== k
+ size
&&
114 (mp
->m_flag
& fl
) != 0))
118 if (lastmq
&& (n
==-2 || (n
==-1 && mp
== &message
[msgCount
]))) {
124 mesg
= mp
- &message
[0];
125 if (dot
!= &message
[n
-1]) {
126 for (mq
= mp
; mq
< &message
[msgCount
]; mq
++)
132 if (mb
.mb_type
== MB_IMAP
)
133 imap_getheaders(mesg
+1, mesg
+ size
);
134 for (; mp
< &message
[msgCount
]; mp
++) {
140 printhead(mesg
, stdout
, 0);
142 } else { /* threaded */
145 for (mp
= threadroot
; mp
; mp
= next_in_thread(mp
))
146 if (visible(mp
) && (mp
->m_collapsed
<= 0 ||
147 mp
== &message
[n
-1])) {
154 if ((n
> 0 && mp
== &message
[n
-1]) ||
155 (n
== 0 && g
== k
) ||
156 (n
== -2 && g
== k
+ size
&&
159 (mp
->m_flag
& fl
) != 0))
163 if (lastmq
&& (n
==-2 || (n
==-1 && mp
==&message
[msgCount
]))) {
169 if (dot
!= &message
[n
-1]) {
170 for (mq
= mp
; mq
; mq
= next_in_thread(mq
))
171 if (visible(mq
) && mq
->m_collapsed
<= 0) {
177 if (visible(mp
) && (mp
->m_collapsed
<= 0 ||
178 mp
== &message
[n
-1])) {
181 printhead(mp
- &message
[0] + 1, stdout
,
184 mp
= next_in_thread(mp
);
188 printf(catgets(catd
, CATSET
, 6, "No more mail.\n"));
195 * Scroll to the next/previous screen
200 return scroll1(v
, 0);
206 return scroll1(v
, 1);
210 scroll1(char *arg
, int onlynew
)
215 cur
[0] = onlynew
? -1 : 0;
218 case '1': case '2': case '3': case '4': case '5':
219 case '6': case '7': case '8': case '9': case '0':
226 screen
= msgCount
/ size
;
232 screen
+= atoi(arg
+ 1);
234 if (screen
* size
> msgCount
) {
235 screen
= msgCount
/ size
;
236 printf(catgets(catd
, CATSET
, 7,
237 "On last screenful of messages\n"));
245 screen
-= atoi(arg
+ 1);
248 printf(catgets(catd
, CATSET
, 8,
249 "On first screenful of messages\n"));
256 printf(catgets(catd
, CATSET
, 9,
257 "Unrecognized scrolling command \"%s\"\n"), arg
);
260 return(headers(cur
));
264 * Compute screen size.
272 if ((cp
= value("screen")) != NULL
&& (s
= atoi(cp
)) > 0)
274 return scrnheight
- 4;
277 static sigjmp_buf pipejmp
;
284 siglongjmp(pipejmp
, 1);
288 * Print out the headlines for each message
289 * in the passed message list.
296 FILE *volatile obuf
= stdout
;
299 if (is_a_tty
[0] && is_a_tty
[1] && (cp
= value("crt")) != NULL
) {
300 for (n
= 0, ip
= msgvec
; *ip
; ip
++)
302 if (n
> (*cp
== '\0' ? screensize() : atoi(cp
)) + 3) {
304 if (sigsetjmp(pipejmp
, 1))
306 if ((obuf
= Popen(cp
, "w", NULL
, 1)) == NULL
) {
310 safe_signal(SIGPIPE
, onpipe
);
313 for (ip
= msgvec
; *ip
!= 0; ip
++)
314 printhead(*ip
, obuf
, mb
.mb_threaded
);
316 setdot(&message
[*ip
- 1]);
318 if (obuf
!= stdout
) {
319 safe_signal(SIGPIPE
, SIG_IGN
);
321 safe_signal(SIGPIPE
, dflpipe
);
327 dispc(struct message
*mp
, const char *a
)
334 if ((mp
->m_flag
& (MREAD
|MNEW
)) == MREAD
)
336 if ((mp
->m_flag
& (MREAD
|MNEW
)) == (MREAD
|MNEW
))
338 if (mp
->m_flag
& MANSWERED
)
340 if (mp
->m_flag
& MDRAFTED
)
342 if ((mp
->m_flag
& (MREAD
|MNEW
)) == MNEW
)
344 if ((mp
->m_flag
& (MREAD
|MNEW
)) == 0)
346 if (mp
->m_flag
& MJUNK
)
348 if (mp
->m_flag
& MSAVED
)
350 if (mp
->m_flag
& MPRESERVE
)
352 if (mp
->m_flag
& (MBOX
|MBOXED
))
354 if (mp
->m_flag
& MFLAGGED
)
356 if (mp
->m_flag
& MKILL
)
358 if (mb
.mb_threaded
== 1 && mp
->m_collapsed
> 0)
360 if (mb
.mb_threaded
== 1 && mp
->m_collapsed
< 0)
366 hprf(const char *fmt
, int mesg
, FILE *f
, int threaded
, const char *attrlist
)
368 struct message
*mp
= &message
[mesg
-1];
369 char *headline
= NULL
, *subjline
, *name
, *cp
, *pbuf
= NULL
;
376 int subjlen
= scrnwidth
, fromlen
, isto
= 0, isaddr
= 0;
379 if ((mp
->m_flag
& MNOFROM
) == 0) {
380 if ((ibuf
= setinput(&mb
, mp
, NEED_HEADER
)) == NULL
)
382 if ((headlen
= readline(ibuf
, &headline
, &headsize
)) < 0)
385 if ((subjline
= hfield1("subject", mp
)) == NULL
) {
390 in
.l
= strlen(subjline
);
391 mime_fromhdr(&in
, &out
, TD_ICONV
| TD_ISPR
);
394 if ((mp
->m_flag
& MNOFROM
) == 0) {
395 pbuf
= ac_alloc(headlen
+ 1);
396 parse(headline
, headlen
, &hl
, pbuf
);
398 hl
.l_from
= /*fakefrom(mp);*/NULL
;
400 hl
.l_date
= fakedate(mp
->m_time
);
402 if (value("datefield") && (cp
= hfield1("date", mp
)) != NULL
)
403 hl
.l_date
= fakedate(rfctime(cp
));
405 if ((name
= hfieldX("newsgroups", mp
)) == NULL
)
406 if ((name
= hfieldX("article-id", mp
)) == NULL
)
409 } else if (value("show-rcpt") == NULL
) {
412 if (value("showto") && name
&& is_myname(skin(name
))) {
413 if ((cp
= hfield1("to", mp
)) != NULL
) {
420 if ((name
= hfield1("to", mp
)) != NULL
)
428 if (value("showname"))
429 name
= realname(name
);
431 name
= prstr(skin(name
));
434 for (fp
= fmt
; *fp
; fp
++) {
438 } else if (*fp
== '+')
440 while (digitchar(*fp
&0377))
445 #if defined (HAVE_MBTOWC) && defined (HAVE_WCWIDTH)
446 if (mb_cur_max
> 1) {
448 if ((s
= mbtowc(&wc
, fp
, mb_cur_max
)) < 0)
451 if ((n
= wcwidth(wc
)) < 0)
455 #endif /* HAVE_MBTOWC && HAVE_WCWIDTH */
464 for (fp
= fmt
; *fp
; fp
++) {
472 } else if (*fp
== '+')
474 if (digitchar(*fp
&0377)) {
476 n
= 10*n
+ *fp
- '0';
477 while (fp
++, digitchar(*fp
&0377));
489 c
= dot
== mp
? *fp
&0377 : ' ';
494 c
= dispc(mp
, attrlist
);
502 for (i
=msgCount
; i
>999; i
/=10)
505 subjlen
-= fprintf(f
, "%*d", n
, mesg
);
513 fprintf(f
, "%s%s", isto
? "To " : "",
514 colalign(name
, fromlen
, 1));
520 subjlen
-= fprintf(f
, "%*.*s", n
, n
, hl
.l_date
);
526 subjlen
-= fprintf(f
, "%*ld", n
,
537 subjlen
-= fprintf(f
, "%*lu", n
,
542 subjlen
-= putindent(f
, mp
,
549 n
= n
>0 ? n
: subjlen
- 2;
552 if (subjline
!= NULL
&& n
>= 0) {
553 /* pretty pathetic */
554 fprintf(f
, B
? "\"%s\"" : "%s",
555 colalign(subjline
, n
, 0));
561 subjlen
-= fprintf(f
, "%*lu", n
, mp
->m_uid
);
566 subjlen
-= fprintf(f
, "%*u", n
, threaded
== 1 ?
573 for (i
=msgCount
; i
>999; i
/=10)
576 fprintf(f
, "%*ld", n
, threaded
?
577 mp
->m_threadpos
: mesg
);
583 subjlen
-= fprintf(f
, "%*g", n
, mp
->m_score
);
599 * Print out the indenting in threaded display.
602 putindent(FILE *fp
, struct message
*mp
, int maxwidth
)
608 int important
= MNEW
|MFLAGGED
;
610 if (mp
->m_level
== 0)
612 cs
= ac_alloc(mp
->m_level
);
613 us
= ac_alloc(mp
->m_level
* sizeof *us
);
615 if (mp
->m_younger
&& (unsigned)i
+ 1 == mp
->m_younger
->m_level
) {
616 if (mp
->m_parent
&& mp
->m_parent
->m_flag
& important
)
617 us
[i
] = mp
->m_flag
& important
? 0x2523 : 0x2520;
619 us
[i
] = mp
->m_flag
& important
? 0x251D : 0x251C;
622 if (mp
->m_parent
&& mp
->m_parent
->m_flag
& important
)
623 us
[i
] = mp
->m_flag
& important
? 0x2517 : 0x2516;
625 us
[i
] = mp
->m_flag
& important
? 0x2515 : 0x2514;
629 for (i
= mp
->m_level
- 2; i
>= 0; i
--) {
631 if ((unsigned)i
> mq
->m_level
- 1) {
637 mq
->m_parent
->m_flag
&important
)
648 for (indent
= 0; (unsigned)indent
< mp
->m_level
&& indent
< maxwidth
;
650 if (indent
< maxwidth
- 1)
651 putuc(us
[indent
], cs
[indent
] & 0377, fp
);
653 putuc(0x21B8, '^', fp
);
661 * Print out the header of a specific message.
662 * This is a slight improvement to the standard one.
665 printhead(int mesg
, FILE *f
, int threaded
)
667 int bsdflags
, bsdheadline
, sz
;
668 char *fmt
, attrlist
[30], *cp
;
670 bsdflags
= value("bsdcompat") != NULL
|| value("bsdflags") != NULL
||
671 getenv("SYSV3") != NULL
;
672 strcpy(attrlist
, bsdflags
? "NU *HMFATK+-J" : "NUROSPMFATK+-J");
673 if ((cp
= value("attrlist")) != NULL
) {
675 if (sz
> (int)sizeof attrlist
- 1)
676 sz
= (int)sizeof attrlist
- 1;
677 memcpy(attrlist
, cp
, sz
);
679 bsdheadline
= value("bsdcompat") != NULL
||
680 value("bsdheadline") != NULL
;
681 if ((fmt
= value("headline")) == NULL
)
683 "%>%a%m %20f %16d %3l/%-5o %i%S" :
684 "%>%a%m %18f %16d %4l/%-5o %i%s";
685 hprf(fmt
, mesg
, f
, threaded
, attrlist
);
689 * Print out the value of dot.
696 printf(catgets(catd
, CATSET
, 13, "%d\n"),
697 (int)(dot
- &message
[0] + 1));
702 * Print out all the possible commands.
708 extern const struct cmd cmdtab
[];
709 const struct cmd
*cp
;
713 printf(catgets(catd
, CATSET
, 14, "Commands are:\n"));
714 for (cc
= 0, cp
= cmdtab
; cp
->c_name
!= NULL
; cp
++) {
715 cc
+= strlen(cp
->c_name
) + 2;
718 cc
= strlen(cp
->c_name
) + 2;
720 if ((cp
+1)->c_name
!= NULL
)
721 printf(catgets(catd
, CATSET
, 15, "%s, "), cp
->c_name
);
723 printf("%s\n", cp
->c_name
);
729 * Type out the messages requested.
731 static sigjmp_buf pipestop
;
734 type1(int *msgvec
, int doign
, int page
, int pipe
, int decode
,
735 char *cmd
, off_t
*tstats
)
743 * Must be static to become excluded from sigsetjmp().
747 /* Avoid longjmp clobbering */
754 if (sigsetjmp(pipestop
, 1))
760 obuf
= Popen(cmd
, "w", cp
, 1);
765 safe_signal(SIGPIPE
, brokpipe
);
767 } else if (value("interactive") != NULL
&&
768 (page
|| (cp
= value("crt")) != NULL
)) {
771 for (ip
= msgvec
; *ip
&& ip
-msgvec
< msgCount
; ip
++) {
772 if ((message
[*ip
-1].m_have
& HAVE_BODY
) == 0) {
773 if ((get_body(&message
[*ip
- 1])) !=
777 nlines
+= message
[*ip
- 1].m_lines
;
780 if (page
|| nlines
> (*cp
? atoi(cp
) : realscreenheight
)) {
782 obuf
= Popen(cp
, "w", NULL
, 1);
787 safe_signal(SIGPIPE
, brokpipe
);
790 for (ip
= msgvec
; *ip
&& ip
- msgvec
< msgCount
; ip
++) {
791 mp
= &message
[*ip
- 1];
795 if (value("quiet") == NULL
)
796 fprintf(obuf
, catgets(catd
, CATSET
, 17,
797 "Message %2d:\n"), *ip
);
798 send(mp
, obuf
, doign
? ignore
: 0, NULL
,
799 pipe
&& value("piperaw") ? SEND_MBOX
:
801 doign
? SEND_TODISP
: SEND_TODISP_ALL
,
803 if (pipe
&& value("page")) {
807 tstats
[0] += mstats
[0];
808 tstats
[1] += mstats
[1];
812 if (obuf
!= stdout
) {
814 * Ignore SIGPIPE so it can't cause a duplicate close.
816 safe_signal(SIGPIPE
, SIG_IGN
);
818 safe_signal(SIGPIPE
, dflpipe
);
824 * Get the last, possibly quoted part of linebuf.
827 laststring(char *linebuf
, int *flag
, int strip
)
833 cp
= strlen(linebuf
) + linebuf
- 1;
836 * Strip away trailing blanks.
838 while (cp
> linebuf
&& whitechar(*cp
& 0377))
847 * Now search for the beginning of the command name.
850 if (quoted
== '\'' || quoted
== '\"') {
855 while (cp
> linebuf
) {
858 } else if (*(cp
- 1) != '\\') {
876 while (cp
> linebuf
&& !whitechar(*cp
& 0377))
878 if (whitechar(*cp
& 0377))
890 * Pipe the messages requested.
893 pipe1(char *str
, int doign
)
900 msgvec
= (int *)salloc((msgCount
+ 2) * sizeof *msgvec
);
901 if ((cmd
= laststring(str
, &f
, 1)) == NULL
) {
903 if (cmd
== NULL
|| *cmd
== '\0') {
904 fputs(catgets(catd
, CATSET
, 16,
905 "variable cmd not set\n"), stderr
);
910 *msgvec
= first(0, MMNORM
);
914 puts(catgets(catd
, CATSET
, 18, "No messages to pipe."));
918 } else if (getmsglist(str
, msgvec
, 0) < 0)
923 printf("No applicable messages.\n");
926 printf(catgets(catd
, CATSET
, 268, "Pipe to: \"%s\"\n"), cmd
);
927 stats
[0] = stats
[1] = 0;
928 if ((ret
= type1(msgvec
, doign
, 0, 1, 0, cmd
, stats
)) == 0) {
929 printf("\"%s\" ", cmd
);
931 printf("%lu", (long)stats
[0]);
933 printf(catgets(catd
, CATSET
, 27, "binary"));
934 printf("/%lu\n", (long)stats
[1]);
940 * Paginate messages, honor ignored fields.
946 return (type1(msgvec
, 1, 1, 0, 0, NULL
, NULL
));
950 * Paginate messages, even printing ignored fields.
957 return (type1(msgvec
, 0, 1, 0, 0, NULL
, NULL
));
961 * Type out messages, honor ignored fields.
968 return(type1(msgvec
, 1, 0, 0, 0, NULL
, NULL
));
972 * Type out messages, even printing ignored fields.
979 return(type1(msgvec
, 0, 0, 0, 0, NULL
, NULL
));
983 * Show MIME-encoded message text, including all fields.
990 return(type1(msgvec
, 0, 0, 0, 1, NULL
, NULL
));
994 * Pipe messages, honor ignored fields.
1000 return(pipe1(str
, 1));
1003 * Pipe messages, not respecting ignored fields.
1009 return(pipe1(str
, 0));
1013 * Respond to a broken pipe signal --
1014 * probably caused by quitting more.
1021 siglongjmp(pipestop
, 1);
1025 * Print the top so many lines of each desired message.
1026 * The number of lines is taken from the variable "toplines"
1027 * and defaults to 5.
1035 int c
, topl
, lines
, lineb
;
1036 char *valtop
, *linebuf
= NULL
;
1041 valtop
= value("toplines");
1042 if (valtop
!= NULL
) {
1043 topl
= atoi(valtop
);
1044 if (topl
< 0 || topl
> 10000)
1048 for (ip
= msgvec
; *ip
&& ip
-msgvec
< msgCount
; ip
++) {
1049 mp
= &message
[*ip
- 1];
1053 if (value("quiet") == NULL
)
1054 printf(catgets(catd
, CATSET
, 19,
1055 "Message %2d:\n"), *ip
);
1056 if (mp
->m_flag
& MNOFROM
)
1057 printf("From %s %s\n", fakefrom(mp
),
1058 fakedate(mp
->m_time
));
1059 if ((ibuf
= setinput(&mb
, mp
, NEED_BODY
)) == NULL
) /* XXX could use TOP */
1064 for (lines
= 0; lines
< c
&& lines
<= topl
; lines
++) {
1065 if (readline(ibuf
, &linebuf
, &linesize
) < 0)
1068 lineb
= blankline(linebuf
);
1077 * Touch all the given messages so that they will
1086 for (ip
= msgvec
; *ip
!= 0; ip
++) {
1087 setdot(&message
[*ip
-1]);
1088 dot
->m_flag
|= MTOUCH
;
1089 dot
->m_flag
&= ~MPRESERVE
;
1091 * POSIX interpretation necessary.
1099 * Make sure all passed messages get mboxed.
1107 for (ip
= msgvec
; *ip
!= 0; ip
++) {
1108 setdot(&message
[*ip
-1]);
1109 dot
->m_flag
|= MTOUCH
|MBOX
;
1110 dot
->m_flag
&= ~MPRESERVE
;
1112 * POSIX interpretation necessary.
1120 * List the folders the user currently has.
1126 char dirname
[PATHSIZE
];
1130 name
= expand(*argv
);
1131 else if (getfold(dirname
, sizeof dirname
) < 0) {
1132 printf(catgets(catd
, CATSET
, 20,
1133 "No value set for \"folder\"\n"));
1137 if (which_protocol(name
) == PROTO_IMAP
)
1138 imap_folders(name
, *argv
== NULL
);
1140 if ((cmd
= value("LISTER")) == NULL
)
1142 run_command(cmd
, 0, -1, -1, name
, NULL
, NULL
);