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
45 #endif /* HAVE_WCTYPE_H */
48 * Mail -- a mail program
50 * Message list handling.
58 static char **add_to_namelist(char ***namelist
, size_t *nmlsize
,
59 char **np
, char *string
);
60 static int markall(char *buf
, int f
);
61 static int evalcol(int col
);
62 static int check(int mesg
, int f
);
63 static int scan(char **sp
);
64 static void regret(int token
);
65 static void scaninit(void);
66 static int matchsender(char *str
, int mesg
, int allnet
);
67 static int matchmid(char *id
, enum idfield idfield
, int mesg
);
68 static int matchsubj(char *str
, int mesg
);
69 static void unmark(int mesg
);
70 static int metamess(int meta
, int f
);
72 static size_t STRINGLEN
;
74 static int lexnumber
; /* Number of TNUMBER from scan() */
75 static char *lexstring
; /* String from TSTRING, scan() */
76 static int regretp
; /* Pointer to TOS of regret tokens */
77 static int regretstack
[REGDEP
]; /* Stack of regretted tokens */
78 static char *string_stack
[REGDEP
]; /* Stack of regretted strings */
79 static int numberstack
[REGDEP
]; /* Stack of regretted numbers */
80 static int threadflag
; /* mark entire threads */
83 * Convert the user string of message numbers and
84 * store the numbers into vector.
86 * Returns the count of messages picked up or -1 on error.
89 getmsglist(char *buf
, int *vector
, int flags
)
99 if (markall(buf
, flags
) < 0)
104 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
105 if (mp
->m_flag
& MMARK
) {
106 if ((mp
->m_flag
& MNEWEST
) == 0)
107 unmark(mp
- &message
[0] + 1);
114 if (mb
.mb_threaded
== 0) {
115 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
116 if (mp
->m_flag
& MMARK
)
117 *ip
++ = mp
- &message
[0] + 1;
119 for (mp
= threadroot
; mp
; mp
= next_in_thread(mp
))
120 if (mp
->m_flag
& MMARK
)
121 *ip
++ = mp
- &message
[0] + 1;
128 * Mark all messages that the user wanted from the command
129 * line in the message structure. Return 0 on success, -1
134 * Bit values for colon modifiers.
137 #define CMNEW 01 /* New messages */
138 #define CMOLD 02 /* Old messages */
139 #define CMUNREAD 04 /* Unread messages */
140 #define CMDELETED 010 /* Deleted messages */
141 #define CMREAD 020 /* Read messages */
142 #define CMFLAG 040 /* Flagged messages */
143 #define CMANSWER 0100 /* Answered messages */
144 #define CMDRAFT 0200 /* Draft messages */
145 #define CMKILL 0400 /* Killed messages */
146 #define CMJUNK 01000 /* Junk messages */
149 * The following table describes the letters which can follow
150 * the colon and gives the corresponding modifier bit.
153 static struct coltab
{
154 char co_char
; /* What to find past : */
155 int co_bit
; /* Associated modifier bit */
156 int co_mask
; /* m_status bits to mask */
157 int co_equal
; /* ... must equal this */
159 { 'n', CMNEW
, MNEW
, MNEW
},
160 { 'o', CMOLD
, MNEW
, 0 },
161 { 'u', CMUNREAD
, MREAD
, 0 },
162 { 'd', CMDELETED
, MDELETED
, MDELETED
},
163 { 'r', CMREAD
, MREAD
, MREAD
},
164 { 'f', CMFLAG
, MFLAGGED
, MFLAGGED
},
165 { 'a', CMANSWER
, MANSWERED
, MANSWERED
},
166 { 't', CMDRAFT
, MDRAFTED
, MDRAFTED
},
167 { 'k', CMKILL
, MKILL
, MKILL
},
168 { 'j', CMJUNK
, MJUNK
, MJUNK
},
172 static int lastcolmod
;
175 add_to_namelist(char ***namelist
, size_t *nmlsize
, char **np
, char *string
)
179 if ((idx
= np
- *namelist
) >= *nmlsize
) {
180 *namelist
= srealloc(*namelist
, (*nmlsize
+= 8) * sizeof *np
);
181 np
= &(*namelist
)[idx
];
187 #define markall_ret(i) { \
189 ac_free(lexstring); \
194 markall(char *buf
, int f
)
197 int i
, retval
, gotheaders
;
198 struct message
*mp
, *mx
;
199 char **namelist
, *bufp
, *id
= NULL
, *cp
;
200 int tok
, beg
, mc
, star
, other
, valdot
, colmod
, colresult
, topen
, tback
;
202 enum idfield idfield
= ID_REFERENCES
;
204 lexstring
= ac_alloc(STRINGLEN
= 2 * strlen(buf
) + 1);
205 valdot
= dot
- &message
[0] + 1;
207 for (i
= 1; i
<= msgCount
; i
++) {
208 message
[i
-1].m_flag
&= ~MOLDMARK
;
209 if (message
[i
-1].m_flag
& MMARK
)
210 message
[i
-1].m_flag
|= MOLDMARK
;
215 namelist
= smalloc((nmlsize
= 8) * sizeof *namelist
);
225 while (tok
!= TEOL
) {
230 printf(catgets(catd
, CATSET
, 112,
231 "No numbers mixed with *\n"));
237 if (check(lexnumber
, f
))
240 while (mb
.mb_threaded
? 1 : i
<= lexnumber
) {
241 if (!(message
[i
-1].m_flag
&MHIDDEN
) &&
243 (message
[i
-1].m_flag
&
246 if (mb
.mb_threaded
) {
249 mx
= next_in_thread(&message
[i
-1]);
272 printf(catgets(catd
, CATSET
, 113,
273 "Non-numeric second argument\n"));
278 if (mb
.mb_threaded
) {
279 mx
= next_in_thread(&message
[i
-1]);
280 i
= mx
? mx
-message
+1 : msgCount
+1;
284 printf(catgets(catd
, CATSET
, 114,
285 "Referencing beyond EOF\n"));
288 } while (message
[i
-1].m_flag
== MHIDDEN
||
289 (message
[i
-1].m_flag
& MDELETED
) !=
291 (message
[i
-1].m_flag
& MKILL
));
299 if (mb
.mb_threaded
) {
302 i
= mx
? mx
-message
+1 : 0;
306 printf(catgets(catd
, CATSET
,
308 "Referencing before 1\n"));
311 } while ((message
[i
-1].m_flag
& MHIDDEN
) ||
312 (message
[i
-1].m_flag
& MDELETED
)
314 (message
[i
-1].m_flag
& MKILL
));
321 printf(catgets(catd
, CATSET
, 116,
322 "Non-numeric second argument\n"));
326 if (lexstring
[0] == ':') {
327 colresult
= evalcol(lexstring
[1]);
328 if (colresult
== 0) {
329 printf(catgets(catd
, CATSET
, 117,
330 "Unknown colon modifier \"%s\"\n"),
337 np
= add_to_namelist(&namelist
, &nmlsize
,
338 np
, savestr(lexstring
));
342 if (imap_search(lexstring
, f
) == STOP
)
351 lexnumber
= metamess(lexstring
[0], f
);
358 for (i
= 1; i
<= msgCount
; i
++) {
359 if ((message
[i
-1].m_flag
& MHIDDEN
) ||
360 (message
[i
-1].m_flag
&MDELETED
)
363 if (message
[i
-1].m_flag
&MOLDMARK
)
370 printf(catgets(catd
, CATSET
, 118,
371 "Can't mix \"*\" with anything\n"));
378 if (mb
.mb_type
== MB_IMAP
&& gotheaders
++ == 0)
379 imap_getheaders(1, msgCount
);
380 if (id
== NULL
&& (cp
= hfield("in-reply-to", dot
))
383 idfield
= ID_IN_REPLY_TO
;
385 if (id
== NULL
&& (cp
= hfield("references", dot
))
388 if ((np
= extract(cp
, GREF
)) != NULL
) {
389 while (np
->n_flink
!= NULL
)
391 id
= savestr(np
->n_name
);
392 idfield
= ID_REFERENCES
;
396 printf(catgets(catd
, CATSET
, 227,
397 "Cannot determine parent Message-ID of the current message\n"));
409 np
= add_to_namelist(&namelist
, &nmlsize
, np
, NULL
);
413 for (i
= 0; i
< msgCount
; i
++) {
414 if (!(message
[i
].m_flag
& MHIDDEN
) &&
415 (message
[i
].m_flag
& MDELETED
) ==
423 printf(catgets(catd
, CATSET
, 119,
424 "No applicable messages.\n"));
430 if ((topen
|| tback
) && mc
== 0) {
431 for (i
= 0; i
< msgCount
; i
++)
432 if (message
[i
].m_flag
& MMARK
)
437 "No previously marked messages.\n" :
438 "No messages satisfy (criteria).\n");
444 * If no numbers were given, mark all of the messages,
445 * so that we can unmark any whose sender was not selected
446 * if any user names were given.
449 if ((np
> namelist
|| colmod
!= 0 || id
) && mc
== 0)
450 for (i
= 1; i
<= msgCount
; i
++) {
451 if (!(message
[i
-1].m_flag
& MHIDDEN
) &&
452 (message
[i
-1].m_flag
& MDELETED
) ==
458 * If any names were given, go through and eliminate any
459 * messages whose senders were not requested.
462 if (np
> namelist
|| id
) {
463 int allnet
= value("allnet") != NULL
;
465 if (mb
.mb_type
== MB_IMAP
&& gotheaders
++ == 0)
466 imap_getheaders(1, msgCount
);
467 for (i
= 1; i
<= msgCount
; i
++) {
470 for (nq
= &namelist
[0]; *nq
!= NULL
; nq
++) {
472 if (matchsubj(*nq
, i
)) {
478 if (matchsender(*nq
, i
,
486 if (mc
== 0 && id
&& matchmid(id
, idfield
, i
))
493 * Make sure we got some decent messages.
497 for (i
= 1; i
<= msgCount
; i
++)
498 if (message
[i
-1].m_flag
& MMARK
) {
503 if (!inhook
&& np
> namelist
) {
504 printf(catgets(catd
, CATSET
, 120,
505 "No applicable messages from {%s"),
507 for (nq
= &namelist
[1]; *nq
!= NULL
; nq
++)
508 printf(catgets(catd
, CATSET
, 121,
510 printf(catgets(catd
, CATSET
, 122, "}\n"));
512 printf(catgets(catd
, CATSET
, 227,
513 "Parent message not found\n"));
520 * If any colon modifiers were given, go through and
521 * unmark any messages which do not satisfy the modifiers.
525 for (i
= 1; i
<= msgCount
; i
++) {
528 mp
= &message
[i
- 1];
529 for (colp
= &coltab
[0]; colp
->co_char
; colp
++)
530 if (colp
->co_bit
& colmod
)
531 if ((mp
->m_flag
& colp
->co_mask
)
532 != (unsigned)colp
->co_equal
)
536 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
537 if (mp
->m_flag
& MMARK
)
539 if (mp
>= &message
[msgCount
]) {
543 printf(catgets(catd
, CATSET
, 123,
544 "No messages satisfy"));
545 for (colp
= &coltab
[0]; colp
->co_char
; colp
++)
546 if (colp
->co_bit
& colmod
)
547 printf(" :%c", colp
->co_char
);
560 * Turn the character after a colon modifier into a bit
570 for (colp
= &coltab
[0]; colp
->co_char
; colp
++)
571 if (colp
->co_char
== col
)
572 return(colp
->co_bit
);
577 * Check the passed message number for legality and proper flags.
578 * If f is MDELETED, then either kind will do. Otherwise, the message
579 * has to be undeleted.
582 check(int mesg
, int f
)
586 if (mesg
< 1 || mesg
> msgCount
) {
587 printf(catgets(catd
, CATSET
, 124,
588 "%d: Invalid message number\n"), mesg
);
591 mp
= &message
[mesg
-1];
592 if (mp
->m_flag
& MHIDDEN
|| (f
!= MDELETED
&&
593 (mp
->m_flag
& MDELETED
) != 0)) {
594 printf(catgets(catd
, CATSET
, 125,
595 "%d: Inappropriate message\n"), mesg
);
602 * Scan out the list of string arguments, shell style
606 getrawlist(const char *line
, size_t linesize
, char **argv
, int argc
,
609 char c
, *cp2
, quotec
;
616 linebuf
= ac_alloc(linesize
+ 1);
618 for (; blankchar(*cp
& 0377); cp
++);
621 if (argn
>= argc
- 1) {
622 printf(catgets(catd
, CATSET
, 126,
623 "Too many elements in the list; excess discarded.\n"));
628 while ((c
= *cp
) != '\0') {
630 if (quotec
!= '\0') {
635 } else if (c
== '\\')
642 case '0': case '1': case '2': case '3':
643 case '4': case '5': case '6': case '7':
645 if (*cp >= '0' && *cp <= '7')
646 c = c * 8 + *cp++ - '0';
647 if (*cp >= '0' && *cp <= '7')
648 c = c * 8 + *cp++ - '0';
671 if (cp
[-1]!=quotec
|| echolist
)
675 /*else if (c == '^') {
679 /\* null doesn't show up anyway *\/
680 else if ((c >= 'A' && c <= '_') ||
681 (c >= 'a' && c <= 'z'))
689 } else if (c
== '"' || c
== '\'') {
693 } else if (c
== '\\' && !echolist
) {
698 } else if (blankchar(c
& 0377))
704 argv
[argn
++] = savestr(linebuf
);
712 * scan out a single lexical item and return its token number,
713 * updating the string pointer passed **p. Also, store the value
714 * of the number or string scanned in lexnumber or lexstring as
715 * appropriate. In any event, store the scanned `thing' in lexstring.
740 int c
, level
, inquote
;
745 strncpy(lexstring
, string_stack
[regretp
], STRINGLEN
);
746 lexstring
[STRINGLEN
-1]='\0';
747 lexnumber
= numberstack
[regretp
];
748 return(regretstack
[regretp
--]);
755 * strip away leading white space.
762 * If no characters remain, we are at end of line,
772 * Select members of a message thread.
776 if (*cp
== '\0' || spacechar(*cp
&0377)) {
786 * If the leading character is a digit, scan
787 * the number and convert it on the fly.
788 * Return TNUMBER when done.
793 while (digitchar(c
)) {
794 lexnumber
= lexnumber
*10 + c
- '0';
804 * An IMAP SEARCH list. Note that TOPEN has always been included
805 * in singles[] in Mail and mailx. Thus although there is no formal
806 * definition for (LIST) lists, they do not collide with historical
807 * practice because a subject string (LIST) could never been matched
816 if ((c
= *cp
++&0377) == '\0') {
817 mtop
: fprintf(stderr
, "Missing \")\".\n");
820 if (inquote
&& c
== '\\') {
833 else if (spacechar(c
)) {
835 * Replace unquoted whitespace by single
836 * space characters, to make the string
837 * IMAP SEARCH conformant.
844 } while (c
!= ')' || level
> 0);
851 * Check for single character tokens; return such
855 for (lp
= &singles
[0]; lp
->l_char
!= 0; lp
++)
856 if (c
== lp
->l_char
) {
864 * We've got a string! Copy all the characters
865 * of the string into lexstring, until we see
866 * a null, space, or tab.
867 * If the lead character is a " or ', save it
868 * and scan until you get another.
872 if (c
== '\'' || c
== '"') {
877 if (quotec
== 0 && c
== '\\' && *cp
)
883 if (quotec
== 0 && blankchar(c
))
885 if ((size_t)(cp2
- lexstring
) < (size_t)STRINGLEN
- 1)
889 if (quotec
&& c
== 0) {
890 fprintf(stderr
, catgets(catd
, CATSET
, 127,
891 "Missing %c\n"), quotec
);
900 * Unscan the named token by pushing it onto the regret stack.
905 if (++regretp
>= REGDEP
)
906 panic(catgets(catd
, CATSET
, 128, "Too many regrets"));
907 regretstack
[regretp
] = token
;
908 lexstring
[STRINGLEN
-1] = '\0';
909 string_stack
[regretp
] = savestr(lexstring
);
910 numberstack
[regretp
] = lexnumber
;
914 * Reset all the scanner global variables.
924 * Find the first message whose flags & m == f and return
925 * its message number.
936 for (mp
= dot
; mb
.mb_threaded
? mp
!= NULL
: mp
< &message
[msgCount
];
937 mb
.mb_threaded
? mp
= next_in_thread(mp
) : mp
++) {
938 if (!(mp
->m_flag
& MHIDDEN
) && (mp
->m_flag
& m
) == (unsigned)f
)
939 return mp
- message
+ 1;
941 if (dot
> &message
[0]) {
942 for (mp
= dot
-1; mb
.mb_threaded
?
943 mp
!= NULL
: mp
>= &message
[0];
945 mp
= prev_in_thread(mp
) : mp
--) {
946 if (! (mp
->m_flag
& MHIDDEN
) &&
947 (mp
->m_flag
& m
) == (unsigned)f
)
948 return mp
- message
+ 1;
955 * See if the passed name sent the passed message number. Return true
959 matchsender(char *str
, int mesg
, int allnet
)
962 char *cp
= nameof(&message
[mesg
- 1], 0);
965 if ((*cp
== '@' || *cp
== '\0') &&
966 (*str
== '@' || *str
== '\0'))
970 } while (cp
++, *str
++ != '\0');
973 return !strcmp(str
, (value("showname") ? realname
: skin
)
974 (name1(&message
[mesg
- 1], 0)));
978 matchmid(char *id
, enum idfield idfield
, int mesg
)
983 if ((cp
= hfield("message-id", &message
[mesg
- 1])) != NULL
) {
986 return msgidcmp(id
, cp
) == 0;
988 if ((np
= extract(id
, GREF
)) != NULL
)
990 if (msgidcmp(np
->n_name
, cp
) == 0)
992 } while ((np
= np
->n_flink
) != NULL
);
1000 * See if the given string matches inside the subject field of the
1001 * given message. For the purpose of the scan, we ignore case differences.
1002 * If it does, return true. The string search argument is assumed to
1003 * have the form "/search-string." If it is of the form "/," we use the
1004 * previous search string.
1007 static char lastscan
[128];
1010 matchsubj(char *str
, int mesg
)
1018 if (strlen(str
) == 0) {
1021 strncpy(lastscan
, str
, sizeof lastscan
);
1022 lastscan
[sizeof lastscan
- 1]='\0';
1024 mp
= &message
[mesg
-1];
1027 * Now look, ignoring case, for the word in the string.
1030 if (value("searchheaders") && (cp
= strchr(str
, ':'))) {
1032 cp2
= hfield(str
, mp
);
1037 cp2
= hfield("subject", mp
);
1043 mime_fromhdr(&in
, &out
, TD_ICONV
);
1044 i
= substr(out
.s
, cp
);
1050 * Mark the named message by setting its mark bit.
1053 mark(int mesg
, int f
)
1059 if (i
< 1 || i
> msgCount
)
1060 panic(catgets(catd
, CATSET
, 129, "Bad message number to mark"));
1061 if (mb
.mb_threaded
== 1 && threadflag
) {
1062 if ((message
[i
-1].m_flag
& MHIDDEN
) == 0) {
1063 if (f
== MDELETED
||
1064 (message
[i
-1].m_flag
&MDELETED
) == 0)
1065 message
[i
-1].m_flag
|= MMARK
;
1067 if (message
[i
-1].m_child
) {
1068 mp
= message
[i
-1].m_child
;
1069 mark(mp
-message
+1, f
);
1070 for (mp
= mp
->m_younger
; mp
; mp
= mp
->m_younger
)
1071 mark(mp
-message
+1, f
);
1074 message
[i
-1].m_flag
|= MMARK
;
1078 * Unmark the named message.
1086 if (i
< 1 || i
> msgCount
)
1087 panic(catgets(catd
, CATSET
, 130,
1088 "Bad message number to unmark"));
1089 message
[i
-1].m_flag
&= ~MMARK
;
1093 * Return the message number corresponding to the passed meta character.
1096 metamess(int meta
, int f
)
1105 * First 'good' message left.
1107 mp
= mb
.mb_threaded
? threadroot
: &message
[0];
1108 while (mp
< &message
[msgCount
]) {
1109 if (!(mp
->m_flag
& (MHIDDEN
|MKILL
)) &&
1110 (mp
->m_flag
& MDELETED
) == (unsigned)f
)
1111 return(mp
- &message
[0] + 1);
1112 if (mb
.mb_threaded
) {
1113 mp
= next_in_thread(mp
);
1120 printf(catgets(catd
, CATSET
, 131,
1121 "No applicable messages\n"));
1126 * Last 'good message left.
1128 mp
= mb
.mb_threaded
? this_in_thread(threadroot
, -1) :
1129 &message
[msgCount
-1];
1130 while (mp
>= &message
[0]) {
1131 if (!(mp
->m_flag
& (MHIDDEN
|MKILL
)) &&
1132 (mp
->m_flag
& MDELETED
) == (unsigned)f
)
1133 return(mp
- &message
[0] + 1);
1134 if (mb
.mb_threaded
) {
1135 mp
= prev_in_thread(mp
);
1142 printf(catgets(catd
, CATSET
, 132,
1143 "No applicable messages\n"));
1150 m
= dot
- &message
[0] + 1;
1151 if ((dot
->m_flag
& MHIDDEN
) ||
1152 (dot
->m_flag
& MDELETED
) != (unsigned)f
) {
1153 printf(catgets(catd
, CATSET
, 133,
1154 "%d: Inappropriate message\n"), m
);
1161 * Previously current message.
1163 if (prevdot
== NULL
) {
1164 printf(catgets(catd
, CATSET
, 228,
1165 "No previously current message\n"));
1168 m
= prevdot
- &message
[0] + 1;
1169 if ((prevdot
->m_flag
& MHIDDEN
) ||
1170 (prevdot
->m_flag
& MDELETED
) != (unsigned)f
) {
1171 printf(catgets(catd
, CATSET
, 133,
1172 "%d: Inappropriate message\n"), m
);
1178 printf(catgets(catd
, CATSET
, 134,
1179 "Unknown metachar (%c)\n"), c
);