1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Message list handling.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2013 Steffen "Daode" Nurpmeso <sdaoden@users.sf.net>.
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 static char **add_to_namelist(char ***namelist
, size_t *nmlsize
,
48 char **np
, char *string
);
49 static int markall(char *buf
, int f
);
50 static int evalcol(int col
);
51 static int check(int mesg
, int f
);
52 static int scan(char **sp
);
53 static void regret(int token
);
54 static void scaninit(void);
55 static int matchsender(char *str
, int mesg
, int allnet
);
56 static int matchmid(char *id
, enum idfield idfield
, int mesg
);
57 static int matchsubj(char *str
, int mesg
);
58 static void unmark(int mesg
);
59 static int metamess(int meta
, int f
);
61 static size_t STRINGLEN
;
63 static int lexnumber
; /* Number of TNUMBER from scan() */
64 static char *lexstring
; /* String from TSTRING, scan() */
65 static int regretp
; /* Pointer to TOS of regret tokens */
66 static int regretstack
[REGDEP
]; /* Stack of regretted tokens */
67 static char *string_stack
[REGDEP
]; /* Stack of regretted strings */
68 static int numberstack
[REGDEP
]; /* Stack of regretted numbers */
69 static int threadflag
; /* mark entire threads */
72 * Convert the user string of message numbers and
73 * store the numbers into vector.
75 * Returns the count of messages picked up or -1 on error.
78 getmsglist(char *buf
, int *vector
, int flags
)
84 msglist_is_single
= FAL0
;
90 msglist_is_single
= TRU1
;
91 if (markall(buf
, flags
) < 0)
96 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
97 if (mp
->m_flag
& MMARK
) {
98 if ((mp
->m_flag
& MNEWEST
) == 0)
99 unmark(mp
- &message
[0] + 1);
106 if (mb
.mb_threaded
== 0) {
107 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
108 if (mp
->m_flag
& MMARK
)
109 *ip
++ = mp
- &message
[0] + 1;
111 for (mp
= threadroot
; mp
; mp
= next_in_thread(mp
))
112 if (mp
->m_flag
& MMARK
)
113 *ip
++ = mp
- &message
[0] + 1;
116 if ((size_t)(ip
- vector
) != 1)
117 msglist_is_single
= FAL0
;
122 * Mark all messages that the user wanted from the command
123 * line in the message structure. Return 0 on success, -1
128 * Bit values for colon modifiers.
131 #define CMNEW 01 /* New messages */
132 #define CMOLD 02 /* Old messages */
133 #define CMUNREAD 04 /* Unread messages */
134 #define CMDELETED 010 /* Deleted messages */
135 #define CMREAD 020 /* Read messages */
136 #define CMFLAG 040 /* Flagged messages */
137 #define CMANSWER 0100 /* Answered messages */
138 #define CMDRAFT 0200 /* Draft messages */
139 #define CMSPAM 0400 /* Spam messages */
142 * The following table describes the letters which can follow
143 * the colon and gives the corresponding modifier bit.
146 static struct coltab
{
147 char co_char
; /* What to find past : */
148 int co_bit
; /* Associated modifier bit */
149 int co_mask
; /* m_status bits to mask */
150 int co_equal
; /* ... must equal this */
152 { 'n', CMNEW
, MNEW
, MNEW
},
153 { 'o', CMOLD
, MNEW
, 0 },
154 { 'u', CMUNREAD
, MREAD
, 0 },
155 { 'd', CMDELETED
, MDELETED
, MDELETED
},
156 { 'r', CMREAD
, MREAD
, MREAD
},
157 { 'f', CMFLAG
, MFLAGGED
, MFLAGGED
},
158 { 'a', CMANSWER
, MANSWERED
, MANSWERED
},
159 { 't', CMDRAFT
, MDRAFTED
, MDRAFTED
},
160 { 's', CMSPAM
, MSPAM
, MSPAM
},
164 static int lastcolmod
;
167 add_to_namelist(char ***namelist
, size_t *nmlsize
, char **np
, char *string
)
171 if ((idx
= np
- *namelist
) >= *nmlsize
) {
172 *namelist
= srealloc(*namelist
, (*nmlsize
+= 8) * sizeof *np
);
173 np
= &(*namelist
)[idx
];
180 markall(char *buf
, int f
)
182 #define markall_ret(i) do { retval = i; goto out; } while (0);
185 struct message
*mp
, *mx
;
186 char **namelist
, *bufp
, *id
= NULL
, *cp
;
187 int tok
, beg
, mc
, star
, other
, valdot
, colmod
, colresult
, topen
, tback
;
189 enum idfield idfield
= ID_REFERENCES
;
194 lexstring
= ac_alloc(STRINGLEN
= 2 * strlen(buf
) + 1);
195 valdot
= dot
- &message
[0] + 1;
197 for (i
= 1; i
<= msgCount
; i
++) {
198 message
[i
-1].m_flag
&= ~MOLDMARK
;
199 if (message
[i
-1].m_flag
& MMARK
)
200 message
[i
-1].m_flag
|= MOLDMARK
;
205 namelist
= smalloc((nmlsize
= 8) * sizeof *namelist
);
218 while (tok
!= TEOL
) {
223 printf(tr(112, "No numbers mixed with *\n"));
229 if (check(lexnumber
, f
))
232 while (mb
.mb_threaded
? 1 : i
<= lexnumber
) {
233 if (!(message
[i
-1].m_flag
&MHIDDEN
) &&
235 (message
[i
-1].m_flag
&
238 if (mb
.mb_threaded
) {
241 mx
= next_in_thread(&message
[i
-1]);
263 msglist_is_single
= FAL0
;
266 "Non-numeric second argument\n"));
271 if (mb
.mb_threaded
) {
272 mx
= next_in_thread(&message
[i
-1]);
273 i
= mx
? mx
-message
+1 : msgCount
+1;
278 "Referencing beyond EOF\n"));
281 } while (message
[i
-1].m_flag
== MHIDDEN
||
282 (message
[i
-1].m_flag
& MDELETED
) !=
288 msglist_is_single
= FAL0
;
292 if (mb
.mb_threaded
) {
295 i
= mx
? mx
-message
+1 : 0;
300 "Referencing before 1\n"));
303 } while ((message
[i
-1].m_flag
& MHIDDEN
) ||
304 (message
[i
-1].m_flag
& MDELETED
)
311 msglist_is_single
= FAL0
;
314 "Non-numeric second argument\n"));
318 if (lexstring
[0] == ':') {
319 colresult
= evalcol(lexstring
[1]);
320 if (colresult
== 0) {
322 "Unknown colon modifier \"%s\"\n"),
329 np
= add_to_namelist(&namelist
, &nmlsize
,
330 np
, savestr(lexstring
));
334 msglist_is_single
= FAL0
;
335 if (imap_search(lexstring
, f
) == STOP
)
344 msglist_is_single
= FAL0
;
345 lexnumber
= metamess(lexstring
[0], f
);
351 msglist_is_single
= FAL0
;
353 for (i
= 1; i
<= msgCount
; i
++) {
354 if ((message
[i
-1].m_flag
& MHIDDEN
) ||
355 (message
[i
-1].m_flag
&MDELETED
)
358 if (message
[i
-1].m_flag
&MOLDMARK
)
364 msglist_is_single
= FAL0
;
367 "Can't mix \"*\" with anything\n"));
374 msglist_is_single
= FAL0
;
376 if (mb
.mb_type
== MB_IMAP
&& gotheaders
++ == 0)
377 imap_getheaders(1, msgCount
);
379 if (id
== NULL
&& (cp
= hfield1("in-reply-to", dot
))
382 idfield
= ID_IN_REPLY_TO
;
384 if (id
== NULL
&& (cp
= hfield1("references", dot
))
387 if ((enp
= extract(cp
, GREF
)) != NULL
) {
388 while (enp
->n_flink
!= NULL
)
390 id
= savestr(enp
->n_name
);
391 idfield
= ID_REFERENCES
;
396 "Cannot determine parent Message-ID of the current message\n"));
402 msglist_is_single
= FAL0
;
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
) ==
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
;
466 if (mb
.mb_type
== MB_IMAP
&& gotheaders
++ == 0)
467 imap_getheaders(1, msgCount
);
469 for (i
= 1; i
<= msgCount
; i
++) {
472 for (nq
= &namelist
[0]; *nq
!= NULL
; nq
++) {
474 if (matchsubj(*nq
, i
)) {
480 if (matchsender(*nq
, i
,
488 if (mc
== 0 && id
&& matchmid(id
, idfield
, i
))
495 * Make sure we got some decent messages.
499 for (i
= 1; i
<= msgCount
; i
++)
500 if (message
[i
-1].m_flag
& MMARK
) {
505 if (!inhook
&& np
> namelist
) {
507 "No applicable messages from {%s"),
509 for (nq
= &namelist
[1]; *nq
!= NULL
; nq
++)
510 printf(tr(121, ", %s"), *nq
);
511 printf(tr(122, "}\n"));
513 printf(tr(227, "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
++) {
529 mp
= &message
[i
- 1];
530 for (colp
= &coltab
[0]; colp
->co_char
; colp
++)
531 if ((colp
->co_bit
& colmod
) &&
532 ((mp
->m_flag
& colp
->co_mask
)
533 == (unsigned)colp
->co_equal
))
538 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
539 if (mp
->m_flag
& MMARK
)
541 if (mp
>= &message
[msgCount
]) {
545 printf(tr(123, "No messages satisfy"));
546 for (colp
= &coltab
[0]; colp
->co_char
; colp
++)
547 if (colp
->co_bit
& colmod
)
548 printf(" :%c", colp
->co_char
);
563 * Turn the character after a colon modifier into a bit
573 for (colp
= &coltab
[0]; colp
->co_char
; colp
++)
574 if (colp
->co_char
== col
)
575 return(colp
->co_bit
);
580 * Check the passed message number for legality and proper flags.
581 * If f is MDELETED, then either kind will do. Otherwise, the message
582 * has to be undeleted.
585 check(int mesg
, int f
)
589 if (mesg
< 1 || mesg
> msgCount
) {
590 printf(tr(124, "%d: Invalid message number\n"), mesg
);
593 mp
= &message
[mesg
-1];
594 if (mp
->m_flag
& MHIDDEN
|| (f
!= MDELETED
&&
595 (mp
->m_flag
& MDELETED
) != 0)) {
596 printf(tr(125, "%d: Inappropriate message\n"), mesg
);
603 * Scan out the list of string arguments, shell style
607 getrawlist(const char *line
, size_t linesize
, char **argv
, int argc
,
610 char c
, *cp2
, quotec
;
617 linebuf
= ac_alloc(linesize
+ 1);
619 for (; blankchar(*cp
& 0377); cp
++);
622 if (argn
>= argc
- 1) {
624 "Too many elements in the list; excess discarded.\n"));
629 while ((c
= *cp
) != '\0') {
631 if (quotec
!= '\0') {
636 } else if (c
== '\\')
643 case '0': case '1': case '2': case '3':
644 case '4': case '5': case '6': case '7':
646 if (*cp >= '0' && *cp <= '7')
647 c = c * 8 + *cp++ - '0';
648 if (*cp >= '0' && *cp <= '7')
649 c = c * 8 + *cp++ - '0';
672 if (cp
[-1]!=quotec
|| echolist
)
676 /*else if (c == '^') {
680 /\* null doesn't show up anyway *\/
681 else if ((c >= 'A' && c <= '_') ||
682 (c >= 'a' && c <= 'z'))
690 } else if (c
== '"' || c
== '\'') {
694 } else if (c
== '\\' && !echolist
) {
699 } else if (blankchar(c
& 0377))
705 argv
[argn
++] = savestr(linebuf
);
713 * scan out a single lexical item and return its token number,
714 * updating the string pointer passed **p. Also, store the value
715 * of the number or string scanned in lexnumber or lexstring as
716 * appropriate. In any event, store the scanned `thing' in lexstring.
741 int c
, level
, inquote
;
746 strncpy(lexstring
, string_stack
[regretp
], STRINGLEN
);
747 lexstring
[STRINGLEN
-1]='\0';
748 lexnumber
= numberstack
[regretp
];
749 return(regretstack
[regretp
--]);
756 * strip away leading white space.
763 * If no characters remain, we are at end of line,
773 * Select members of a message thread.
777 if (*cp
== '\0' || spacechar(*cp
&0377)) {
787 * If the leading character is a digit, scan
788 * the number and convert it on the fly.
789 * Return TNUMBER when done.
794 while (digitchar(c
)) {
795 lexnumber
= lexnumber
*10 + c
- '0';
805 * An IMAP SEARCH list. Note that TOPEN has always been included
806 * in singles[] in Mail and mailx. Thus although there is no formal
807 * definition for (LIST) lists, they do not collide with historical
808 * practice because a subject string (LIST) could never been matched
817 if ((c
= *cp
++&0377) == '\0') {
818 mtop
: fprintf(stderr
, "Missing \")\".\n");
821 if (inquote
&& c
== '\\') {
834 else if (spacechar(c
)) {
836 * Replace unquoted whitespace by single
837 * space characters, to make the string
838 * IMAP SEARCH conformant.
845 } while (c
!= ')' || level
> 0);
852 * Check for single character tokens; return such
856 for (lp
= &singles
[0]; lp
->l_char
!= 0; lp
++)
857 if (c
== lp
->l_char
) {
865 * We've got a string! Copy all the characters
866 * of the string into lexstring, until we see
867 * a null, space, or tab.
868 * If the lead character is a " or ', save it
869 * and scan until you get another.
873 if (c
== '\'' || c
== '"') {
878 if (quotec
== 0 && c
== '\\' && *cp
)
884 if (quotec
== 0 && blankchar(c
))
886 if ((size_t)(cp2
- lexstring
) < (size_t)STRINGLEN
- 1)
890 if (quotec
&& c
== 0) {
891 fprintf(stderr
, tr(127, "Missing %c\n"), quotec
);
900 * Unscan the named token by pushing it onto the regret stack.
905 if (++regretp
>= REGDEP
)
906 panic(tr(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
= hfield1("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
= hfieldX(str
, mp
);
1036 cp2
= hfield1("subject", mp
);
1042 mime_fromhdr(&in
, &out
, TD_ICONV
);
1043 i
= substr(out
.s
, cp
);
1049 * Mark the named message by setting its mark bit.
1052 mark(int mesg
, int f
)
1058 if (i
< 1 || i
> msgCount
)
1059 panic(tr(129, "Bad message number to mark"));
1060 if (mb
.mb_threaded
== 1 && threadflag
) {
1061 if ((message
[i
-1].m_flag
& MHIDDEN
) == 0) {
1062 if (f
== MDELETED
||
1063 (message
[i
-1].m_flag
&MDELETED
) == 0)
1064 message
[i
-1].m_flag
|= MMARK
;
1066 if (message
[i
-1].m_child
) {
1067 mp
= message
[i
-1].m_child
;
1068 mark(mp
-message
+1, f
);
1069 for (mp
= mp
->m_younger
; mp
; mp
= mp
->m_younger
)
1070 mark(mp
-message
+1, f
);
1073 message
[i
-1].m_flag
|= MMARK
;
1077 * Unmark the named message.
1085 if (i
< 1 || i
> msgCount
)
1086 panic(tr(130, "Bad message number to unmark"));
1087 message
[i
-1].m_flag
&= ~MMARK
;
1091 * Return the message number corresponding to the passed meta character.
1094 metamess(int meta
, int f
)
1103 * First 'good' message left.
1105 mp
= mb
.mb_threaded
? threadroot
: &message
[0];
1106 while (mp
< &message
[msgCount
]) {
1107 if (!(mp
->m_flag
& MHIDDEN
) &&
1108 (mp
->m_flag
& MDELETED
) == (unsigned)f
)
1109 return(mp
- &message
[0] + 1);
1110 if (mb
.mb_threaded
) {
1111 mp
= next_in_thread(mp
);
1118 printf(tr(131, "No applicable messages\n"));
1123 * Last 'good message left.
1125 mp
= mb
.mb_threaded
? this_in_thread(threadroot
, -1) :
1126 &message
[msgCount
-1];
1127 while (mp
>= &message
[0]) {
1128 if (!(mp
->m_flag
& MHIDDEN
) &&
1129 (mp
->m_flag
& MDELETED
) == (unsigned)f
)
1130 return(mp
- &message
[0] + 1);
1131 if (mb
.mb_threaded
) {
1132 mp
= prev_in_thread(mp
);
1139 printf(tr(132, "No applicable messages\n"));
1146 m
= dot
- &message
[0] + 1;
1147 if ((dot
->m_flag
& MHIDDEN
) ||
1148 (dot
->m_flag
& MDELETED
) != (unsigned)f
) {
1149 printf(tr(133, "%d: Inappropriate message\n"), m
);
1156 * Previously current message.
1158 if (prevdot
== NULL
) {
1159 printf(tr(228, "No previously current message\n"));
1162 m
= prevdot
- &message
[0] + 1;
1163 if ((prevdot
->m_flag
& MHIDDEN
) ||
1164 (prevdot
->m_flag
& MDELETED
) != (unsigned)f
) {
1165 printf(tr(133, "%d: Inappropriate message\n"), m
);
1171 printf(tr(134, "Unknown metachar (%c)\n"), c
);