2 * Heirloom mailx - 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
42 static char sccsid
[] = "@(#)list.c 2.62 (gritter) 12/11/08";
51 #endif /* HAVE_WCTYPE_H */
54 * Mail -- a mail program
56 * Message list handling.
64 static char **add_to_namelist(char ***namelist
, size_t *nmlsize
,
65 char **np
, char *string
);
66 static int markall(char *buf
, int f
);
67 static int evalcol(int col
);
68 static int check(int mesg
, int f
);
69 static int scan(char **sp
);
70 static void regret(int token
);
71 static void scaninit(void);
72 static int matchsender(char *str
, int mesg
, int allnet
);
73 static int matchmid(char *id
, enum idfield idfield
, int mesg
);
74 static int matchsubj(char *str
, int mesg
);
75 static void unmark(int mesg
);
76 static int metamess(int meta
, int f
);
78 static size_t STRINGLEN
;
80 static int lexnumber
; /* Number of TNUMBER from scan() */
81 static char *lexstring
; /* String from TSTRING, scan() */
82 static int regretp
; /* Pointer to TOS of regret tokens */
83 static int regretstack
[REGDEP
]; /* Stack of regretted tokens */
84 static char *string_stack
[REGDEP
]; /* Stack of regretted strings */
85 static int numberstack
[REGDEP
]; /* Stack of regretted numbers */
86 static int threadflag
; /* mark entire threads */
89 * Convert the user string of message numbers and
90 * store the numbers into vector.
92 * Returns the count of messages picked up or -1 on error.
95 getmsglist(char *buf
, int *vector
, int flags
)
105 if (markall(buf
, flags
) < 0)
110 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
111 if (mp
->m_flag
& MMARK
) {
112 if ((mp
->m_flag
& MNEWEST
) == 0)
113 unmark(mp
- &message
[0] + 1);
120 if (mb
.mb_threaded
== 0) {
121 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
122 if (mp
->m_flag
& MMARK
)
123 *ip
++ = mp
- &message
[0] + 1;
125 for (mp
= threadroot
; mp
; mp
= next_in_thread(mp
))
126 if (mp
->m_flag
& MMARK
)
127 *ip
++ = mp
- &message
[0] + 1;
134 * Mark all messages that the user wanted from the command
135 * line in the message structure. Return 0 on success, -1
140 * Bit values for colon modifiers.
143 #define CMNEW 01 /* New messages */
144 #define CMOLD 02 /* Old messages */
145 #define CMUNREAD 04 /* Unread messages */
146 #define CMDELETED 010 /* Deleted messages */
147 #define CMREAD 020 /* Read messages */
148 #define CMFLAG 040 /* Flagged messages */
149 #define CMANSWER 0100 /* Answered messages */
150 #define CMDRAFT 0200 /* Draft messages */
151 #define CMKILL 0400 /* Killed messages */
152 #define CMJUNK 01000 /* Junk messages */
155 * The following table describes the letters which can follow
156 * the colon and gives the corresponding modifier bit.
159 static struct coltab
{
160 char co_char
; /* What to find past : */
161 int co_bit
; /* Associated modifier bit */
162 int co_mask
; /* m_status bits to mask */
163 int co_equal
; /* ... must equal this */
165 { 'n', CMNEW
, MNEW
, MNEW
},
166 { 'o', CMOLD
, MNEW
, 0 },
167 { 'u', CMUNREAD
, MREAD
, 0 },
168 { 'd', CMDELETED
, MDELETED
, MDELETED
},
169 { 'r', CMREAD
, MREAD
, MREAD
},
170 { 'f', CMFLAG
, MFLAGGED
, MFLAGGED
},
171 { 'a', CMANSWER
, MANSWERED
, MANSWERED
},
172 { 't', CMDRAFT
, MDRAFTED
, MDRAFTED
},
173 { 'k', CMKILL
, MKILL
, MKILL
},
174 { 'j', CMJUNK
, MJUNK
, MJUNK
},
178 static int lastcolmod
;
181 add_to_namelist(char ***namelist
, size_t *nmlsize
, char **np
, char *string
)
185 if ((idx
= np
- *namelist
) >= *nmlsize
) {
186 *namelist
= srealloc(*namelist
, (*nmlsize
+= 8) * sizeof *np
);
187 np
= &(*namelist
)[idx
];
193 #define markall_ret(i) { \
195 ac_free(lexstring); \
200 markall(char *buf
, int f
)
203 int i
, retval
, gotheaders
;
204 struct message
*mp
, *mx
;
205 char **namelist
, *bufp
, *id
= NULL
, *cp
;
206 int tok
, beg
, mc
, star
, other
, valdot
, colmod
, colresult
, topen
, tback
;
208 enum idfield idfield
= ID_REFERENCES
;
210 lexstring
= ac_alloc(STRINGLEN
= 2 * strlen(buf
) + 1);
211 valdot
= dot
- &message
[0] + 1;
213 for (i
= 1; i
<= msgCount
; i
++) {
214 message
[i
-1].m_flag
&= ~MOLDMARK
;
215 if (message
[i
-1].m_flag
& MMARK
)
216 message
[i
-1].m_flag
|= MOLDMARK
;
221 namelist
= smalloc((nmlsize
= 8) * sizeof *namelist
);
231 while (tok
!= TEOL
) {
236 printf(catgets(catd
, CATSET
, 112,
237 "No numbers mixed with *\n"));
243 if (check(lexnumber
, f
))
246 while (mb
.mb_threaded
? 1 : i
<= lexnumber
) {
247 if (!(message
[i
-1].m_flag
&MHIDDEN
) &&
249 (message
[i
-1].m_flag
&
252 if (mb
.mb_threaded
) {
255 mx
= next_in_thread(&message
[i
-1]);
278 printf(catgets(catd
, CATSET
, 113,
279 "Non-numeric second argument\n"));
284 if (mb
.mb_threaded
) {
285 mx
= next_in_thread(&message
[i
-1]);
286 i
= mx
? mx
-message
+1 : msgCount
+1;
290 printf(catgets(catd
, CATSET
, 114,
291 "Referencing beyond EOF\n"));
294 } while (message
[i
-1].m_flag
== MHIDDEN
||
295 (message
[i
-1].m_flag
& MDELETED
) !=
297 (message
[i
-1].m_flag
& MKILL
));
305 if (mb
.mb_threaded
) {
308 i
= mx
? mx
-message
+1 : 0;
312 printf(catgets(catd
, CATSET
,
314 "Referencing before 1\n"));
317 } while ((message
[i
-1].m_flag
& MHIDDEN
) ||
318 (message
[i
-1].m_flag
& MDELETED
)
320 (message
[i
-1].m_flag
& MKILL
));
327 printf(catgets(catd
, CATSET
, 116,
328 "Non-numeric second argument\n"));
332 if (lexstring
[0] == ':') {
333 colresult
= evalcol(lexstring
[1]);
334 if (colresult
== 0) {
335 printf(catgets(catd
, CATSET
, 117,
336 "Unknown colon modifier \"%s\"\n"),
343 np
= add_to_namelist(&namelist
, &nmlsize
,
344 np
, savestr(lexstring
));
348 if (imap_search(lexstring
, f
) == STOP
)
357 lexnumber
= metamess(lexstring
[0], f
);
364 for (i
= 1; i
<= msgCount
; i
++) {
365 if ((message
[i
-1].m_flag
& MHIDDEN
) ||
366 (message
[i
-1].m_flag
&MDELETED
)
369 if (message
[i
-1].m_flag
&MOLDMARK
)
376 printf(catgets(catd
, CATSET
, 118,
377 "Can't mix \"*\" with anything\n"));
384 if (mb
.mb_type
== MB_IMAP
&& gotheaders
++ == 0)
385 imap_getheaders(1, msgCount
);
386 if (id
== NULL
&& (cp
= hfield("in-reply-to", dot
))
389 idfield
= ID_IN_REPLY_TO
;
391 if (id
== NULL
&& (cp
= hfield("references", dot
))
394 if ((np
= extract(cp
, GREF
)) != NULL
) {
395 while (np
->n_flink
!= NULL
)
397 id
= savestr(np
->n_name
);
398 idfield
= ID_REFERENCES
;
402 printf(catgets(catd
, CATSET
, 227,
403 "Cannot determine parent Message-ID of the current message\n"));
415 np
= add_to_namelist(&namelist
, &nmlsize
, np
, NULL
);
419 for (i
= 0; i
< msgCount
; i
++) {
420 if (!(message
[i
].m_flag
& MHIDDEN
) &&
421 (message
[i
].m_flag
& MDELETED
) ==
429 printf(catgets(catd
, CATSET
, 119,
430 "No applicable messages.\n"));
436 if ((topen
|| tback
) && mc
== 0) {
437 for (i
= 0; i
< msgCount
; i
++)
438 if (message
[i
].m_flag
& MMARK
)
443 "No previously marked messages.\n" :
444 "No messages satisfy (criteria).\n");
450 * If no numbers were given, mark all of the messages,
451 * so that we can unmark any whose sender was not selected
452 * if any user names were given.
455 if ((np
> namelist
|| colmod
!= 0 || id
) && mc
== 0)
456 for (i
= 1; i
<= msgCount
; i
++) {
457 if (!(message
[i
-1].m_flag
& MHIDDEN
) &&
458 (message
[i
-1].m_flag
& MDELETED
) ==
464 * If any names were given, go through and eliminate any
465 * messages whose senders were not requested.
468 if (np
> namelist
|| id
) {
469 int allnet
= value("allnet") != NULL
;
471 if (mb
.mb_type
== MB_IMAP
&& gotheaders
++ == 0)
472 imap_getheaders(1, msgCount
);
473 for (i
= 1; i
<= msgCount
; i
++) {
476 for (nq
= &namelist
[0]; *nq
!= NULL
; nq
++) {
478 if (matchsubj(*nq
, i
)) {
484 if (matchsender(*nq
, i
,
492 if (mc
== 0 && id
&& matchmid(id
, idfield
, i
))
499 * Make sure we got some decent messages.
503 for (i
= 1; i
<= msgCount
; i
++)
504 if (message
[i
-1].m_flag
& MMARK
) {
509 if (!inhook
&& np
> namelist
) {
510 printf(catgets(catd
, CATSET
, 120,
511 "No applicable messages from {%s"),
513 for (nq
= &namelist
[1]; *nq
!= NULL
; nq
++)
514 printf(catgets(catd
, CATSET
, 121,
516 printf(catgets(catd
, CATSET
, 122, "}\n"));
518 printf(catgets(catd
, CATSET
, 227,
519 "Parent message not found\n"));
526 * If any colon modifiers were given, go through and
527 * unmark any messages which do not satisfy the modifiers.
531 for (i
= 1; i
<= msgCount
; i
++) {
534 mp
= &message
[i
- 1];
535 for (colp
= &coltab
[0]; colp
->co_char
; colp
++)
536 if (colp
->co_bit
& colmod
)
537 if ((mp
->m_flag
& colp
->co_mask
)
538 != (unsigned)colp
->co_equal
)
542 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
543 if (mp
->m_flag
& MMARK
)
545 if (mp
>= &message
[msgCount
]) {
549 printf(catgets(catd
, CATSET
, 123,
550 "No messages satisfy"));
551 for (colp
= &coltab
[0]; colp
->co_char
; colp
++)
552 if (colp
->co_bit
& colmod
)
553 printf(" :%c", colp
->co_char
);
566 * Turn the character after a colon modifier into a bit
576 for (colp
= &coltab
[0]; colp
->co_char
; colp
++)
577 if (colp
->co_char
== col
)
578 return(colp
->co_bit
);
583 * Check the passed message number for legality and proper flags.
584 * If f is MDELETED, then either kind will do. Otherwise, the message
585 * has to be undeleted.
588 check(int mesg
, int f
)
592 if (mesg
< 1 || mesg
> msgCount
) {
593 printf(catgets(catd
, CATSET
, 124,
594 "%d: Invalid message number\n"), mesg
);
597 mp
= &message
[mesg
-1];
598 if (mp
->m_flag
& MHIDDEN
|| (f
!= MDELETED
&&
599 (mp
->m_flag
& MDELETED
) != 0)) {
600 printf(catgets(catd
, CATSET
, 125,
601 "%d: Inappropriate message\n"), mesg
);
608 * Scan out the list of string arguments, shell style
612 getrawlist(const char *line
, size_t linesize
, char **argv
, int argc
,
615 char c
, *cp2
, quotec
;
622 linebuf
= ac_alloc(linesize
+ 1);
624 for (; blankchar(*cp
& 0377); cp
++);
627 if (argn
>= argc
- 1) {
628 printf(catgets(catd
, CATSET
, 126,
629 "Too many elements in the list; excess discarded.\n"));
634 while ((c
= *cp
) != '\0') {
636 if (quotec
!= '\0') {
641 } else if (c
== '\\')
648 case '0': case '1': case '2': case '3':
649 case '4': case '5': case '6': case '7':
651 if (*cp >= '0' && *cp <= '7')
652 c = c * 8 + *cp++ - '0';
653 if (*cp >= '0' && *cp <= '7')
654 c = c * 8 + *cp++ - '0';
677 if (cp
[-1]!=quotec
|| echolist
)
681 /*else if (c == '^') {
685 /\* null doesn't show up anyway *\/
686 else if ((c >= 'A' && c <= '_') ||
687 (c >= 'a' && c <= 'z'))
695 } else if (c
== '"' || c
== '\'') {
699 } else if (c
== '\\' && !echolist
) {
704 } else if (blankchar(c
& 0377))
710 argv
[argn
++] = savestr(linebuf
);
718 * scan out a single lexical item and return its token number,
719 * updating the string pointer passed **p. Also, store the value
720 * of the number or string scanned in lexnumber or lexstring as
721 * appropriate. In any event, store the scanned `thing' in lexstring.
746 int c
, level
, inquote
;
751 strncpy(lexstring
, string_stack
[regretp
], STRINGLEN
);
752 lexstring
[STRINGLEN
-1]='\0';
753 lexnumber
= numberstack
[regretp
];
754 return(regretstack
[regretp
--]);
761 * strip away leading white space.
768 * If no characters remain, we are at end of line,
778 * Select members of a message thread.
782 if (*cp
== '\0' || spacechar(*cp
&0377)) {
792 * If the leading character is a digit, scan
793 * the number and convert it on the fly.
794 * Return TNUMBER when done.
799 while (digitchar(c
)) {
800 lexnumber
= lexnumber
*10 + c
- '0';
810 * An IMAP SEARCH list. Note that TOPEN has always been included
811 * in singles[] in Mail and mailx. Thus although there is no formal
812 * definition for (LIST) lists, they do not collide with historical
813 * practice because a subject string (LIST) could never been matched
822 if ((c
= *cp
++&0377) == '\0') {
823 mtop
: fprintf(stderr
, "Missing \")\".\n");
826 if (inquote
&& c
== '\\') {
839 else if (spacechar(c
)) {
841 * Replace unquoted whitespace by single
842 * space characters, to make the string
843 * IMAP SEARCH conformant.
850 } while (c
!= ')' || level
> 0);
857 * Check for single character tokens; return such
861 for (lp
= &singles
[0]; lp
->l_char
!= 0; lp
++)
862 if (c
== lp
->l_char
) {
870 * We've got a string! Copy all the characters
871 * of the string into lexstring, until we see
872 * a null, space, or tab.
873 * If the lead character is a " or ', save it
874 * and scan until you get another.
878 if (c
== '\'' || c
== '"') {
883 if (quotec
== 0 && c
== '\\' && *cp
)
889 if (quotec
== 0 && blankchar(c
))
891 if ((size_t)(cp2
- lexstring
) < (size_t)STRINGLEN
- 1)
895 if (quotec
&& c
== 0) {
896 fprintf(stderr
, catgets(catd
, CATSET
, 127,
897 "Missing %c\n"), quotec
);
906 * Unscan the named token by pushing it onto the regret stack.
911 if (++regretp
>= REGDEP
)
912 panic(catgets(catd
, CATSET
, 128, "Too many regrets"));
913 regretstack
[regretp
] = token
;
914 lexstring
[STRINGLEN
-1] = '\0';
915 string_stack
[regretp
] = savestr(lexstring
);
916 numberstack
[regretp
] = lexnumber
;
920 * Reset all the scanner global variables.
930 * Find the first message whose flags & m == f and return
931 * its message number.
942 for (mp
= dot
; mb
.mb_threaded
? mp
!= NULL
: mp
< &message
[msgCount
];
943 mb
.mb_threaded
? mp
= next_in_thread(mp
) : mp
++) {
944 if (!(mp
->m_flag
& MHIDDEN
) && (mp
->m_flag
& m
) == (unsigned)f
)
945 return mp
- message
+ 1;
947 if (dot
> &message
[0]) {
948 for (mp
= dot
-1; mb
.mb_threaded
?
949 mp
!= NULL
: mp
>= &message
[0];
951 mp
= prev_in_thread(mp
) : mp
--) {
952 if (! (mp
->m_flag
& MHIDDEN
) &&
953 (mp
->m_flag
& m
) == (unsigned)f
)
954 return mp
- message
+ 1;
961 * See if the passed name sent the passed message number. Return true
965 matchsender(char *str
, int mesg
, int allnet
)
968 char *cp
= nameof(&message
[mesg
- 1], 0);
971 if ((*cp
== '@' || *cp
== '\0') &&
972 (*str
== '@' || *str
== '\0'))
976 } while (cp
++, *str
++ != '\0');
979 return !strcmp(str
, (value("showname") ? realname
: skin
)
980 (name1(&message
[mesg
- 1], 0)));
984 matchmid(char *id
, enum idfield idfield
, int mesg
)
989 if ((cp
= hfield("message-id", &message
[mesg
- 1])) != NULL
) {
992 return msgidcmp(id
, cp
) == 0;
994 if ((np
= extract(id
, GREF
)) != NULL
)
996 if (msgidcmp(np
->n_name
, cp
) == 0)
998 } while ((np
= np
->n_flink
) != NULL
);
1006 * See if the given string matches inside the subject field of the
1007 * given message. For the purpose of the scan, we ignore case differences.
1008 * If it does, return true. The string search argument is assumed to
1009 * have the form "/search-string." If it is of the form "/," we use the
1010 * previous search string.
1013 static char lastscan
[128];
1016 matchsubj(char *str
, int mesg
)
1024 if (strlen(str
) == 0) {
1027 strncpy(lastscan
, str
, sizeof lastscan
);
1028 lastscan
[sizeof lastscan
- 1]='\0';
1030 mp
= &message
[mesg
-1];
1033 * Now look, ignoring case, for the word in the string.
1036 if (value("searchheaders") && (cp
= strchr(str
, ':'))) {
1038 cp2
= hfield(str
, mp
);
1043 cp2
= hfield("subject", mp
);
1049 mime_fromhdr(&in
, &out
, TD_ICONV
);
1050 i
= substr(out
.s
, cp
);
1056 * Mark the named message by setting its mark bit.
1059 mark(int mesg
, int f
)
1065 if (i
< 1 || i
> msgCount
)
1066 panic(catgets(catd
, CATSET
, 129, "Bad message number to mark"));
1067 if (mb
.mb_threaded
== 1 && threadflag
) {
1068 if ((message
[i
-1].m_flag
& MHIDDEN
) == 0) {
1069 if (f
== MDELETED
||
1070 (message
[i
-1].m_flag
&MDELETED
) == 0)
1071 message
[i
-1].m_flag
|= MMARK
;
1073 if (message
[i
-1].m_child
) {
1074 mp
= message
[i
-1].m_child
;
1075 mark(mp
-message
+1, f
);
1076 for (mp
= mp
->m_younger
; mp
; mp
= mp
->m_younger
)
1077 mark(mp
-message
+1, f
);
1080 message
[i
-1].m_flag
|= MMARK
;
1084 * Unmark the named message.
1092 if (i
< 1 || i
> msgCount
)
1093 panic(catgets(catd
, CATSET
, 130,
1094 "Bad message number to unmark"));
1095 message
[i
-1].m_flag
&= ~MMARK
;
1099 * Return the message number corresponding to the passed meta character.
1102 metamess(int meta
, int f
)
1111 * First 'good' message left.
1113 mp
= mb
.mb_threaded
? threadroot
: &message
[0];
1114 while (mp
< &message
[msgCount
]) {
1115 if (!(mp
->m_flag
& (MHIDDEN
|MKILL
)) &&
1116 (mp
->m_flag
& MDELETED
) == (unsigned)f
)
1117 return(mp
- &message
[0] + 1);
1118 if (mb
.mb_threaded
) {
1119 mp
= next_in_thread(mp
);
1126 printf(catgets(catd
, CATSET
, 131,
1127 "No applicable messages\n"));
1132 * Last 'good message left.
1134 mp
= mb
.mb_threaded
? this_in_thread(threadroot
, -1) :
1135 &message
[msgCount
-1];
1136 while (mp
>= &message
[0]) {
1137 if (!(mp
->m_flag
& (MHIDDEN
|MKILL
)) &&
1138 (mp
->m_flag
& MDELETED
) == (unsigned)f
)
1139 return(mp
- &message
[0] + 1);
1140 if (mb
.mb_threaded
) {
1141 mp
= prev_in_thread(mp
);
1148 printf(catgets(catd
, CATSET
, 132,
1149 "No applicable messages\n"));
1156 m
= dot
- &message
[0] + 1;
1157 if ((dot
->m_flag
& MHIDDEN
) ||
1158 (dot
->m_flag
& MDELETED
) != (unsigned)f
) {
1159 printf(catgets(catd
, CATSET
, 133,
1160 "%d: Inappropriate message\n"), m
);
1167 * Previously current message.
1169 if (prevdot
== NULL
) {
1170 printf(catgets(catd
, CATSET
, 228,
1171 "No previously current message\n"));
1174 m
= prevdot
- &message
[0] + 1;
1175 if ((prevdot
->m_flag
& MHIDDEN
) ||
1176 (prevdot
->m_flag
& MDELETED
) != (unsigned)f
) {
1177 printf(catgets(catd
, CATSET
, 133,
1178 "%d: Inappropriate message\n"), m
);
1184 printf(catgets(catd
, CATSET
, 134,
1185 "Unknown metachar (%c)\n"), c
);