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
54 static char **add_to_namelist(char ***namelist
, size_t *nmlsize
,
55 char **np
, char *string
);
56 static int markall(char *buf
, int f
);
57 static int evalcol(int col
);
58 static int check(int mesg
, int f
);
59 static int scan(char **sp
);
60 static void regret(int token
);
61 static void scaninit(void);
62 static int matchsender(char *str
, int mesg
, int allnet
);
63 static int matchmid(char *id
, enum idfield idfield
, int mesg
);
64 static int matchsubj(char *str
, int mesg
);
65 static void unmark(int mesg
);
66 static int metamess(int meta
, int f
);
68 static size_t STRINGLEN
;
70 static int lexnumber
; /* Number of TNUMBER from scan() */
71 static char *lexstring
; /* String from TSTRING, scan() */
72 static int regretp
; /* Pointer to TOS of regret tokens */
73 static int regretstack
[REGDEP
]; /* Stack of regretted tokens */
74 static char *string_stack
[REGDEP
]; /* Stack of regretted strings */
75 static int numberstack
[REGDEP
]; /* Stack of regretted numbers */
76 static int threadflag
; /* mark entire threads */
79 * Convert the user string of message numbers and
80 * store the numbers into vector.
82 * Returns the count of messages picked up or -1 on error.
85 getmsglist(char *buf
, int *vector
, int flags
)
91 msglist_is_single
= FAL0
;
97 msglist_is_single
= TRU1
;
98 if (markall(buf
, flags
) < 0)
103 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
104 if (mp
->m_flag
& MMARK
) {
105 if ((mp
->m_flag
& MNEWEST
) == 0)
106 unmark(mp
- &message
[0] + 1);
113 if (mb
.mb_threaded
== 0) {
114 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
115 if (mp
->m_flag
& MMARK
)
116 *ip
++ = mp
- &message
[0] + 1;
118 for (mp
= threadroot
; mp
; mp
= next_in_thread(mp
))
119 if (mp
->m_flag
& MMARK
)
120 *ip
++ = mp
- &message
[0] + 1;
123 if ((size_t)(ip
- vector
) != 1)
124 msglist_is_single
= FAL0
;
129 * Mark all messages that the user wanted from the command
130 * line in the message structure. Return 0 on success, -1
135 * Bit values for colon modifiers.
138 #define CMNEW 01 /* New messages */
139 #define CMOLD 02 /* Old messages */
140 #define CMUNREAD 04 /* Unread messages */
141 #define CMDELETED 010 /* Deleted messages */
142 #define CMREAD 020 /* Read messages */
143 #define CMFLAG 040 /* Flagged messages */
144 #define CMANSWER 0100 /* Answered messages */
145 #define CMDRAFT 0200 /* Draft messages */
146 #define CMSPAM 0400 /* Spam 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 { 's', CMSPAM
, MSPAM
, MSPAM
},
171 static int lastcolmod
;
174 add_to_namelist(char ***namelist
, size_t *nmlsize
, char **np
, char *string
)
178 if ((idx
= np
- *namelist
) >= *nmlsize
) {
179 *namelist
= srealloc(*namelist
, (*nmlsize
+= 8) * sizeof *np
);
180 np
= &(*namelist
)[idx
];
187 markall(char *buf
, int f
)
189 #define markall_ret(i) do { retval = i; goto out; } while (0);
192 struct message
*mp
, *mx
;
193 char **namelist
, *bufp
, *id
= NULL
, *cp
;
194 int tok
, beg
, mc
, star
, other
, valdot
, colmod
, colresult
, topen
, tback
;
196 enum idfield idfield
= ID_REFERENCES
;
201 lexstring
= ac_alloc(STRINGLEN
= 2 * strlen(buf
) + 1);
202 valdot
= dot
- &message
[0] + 1;
204 for (i
= 1; i
<= msgCount
; i
++) {
205 message
[i
-1].m_flag
&= ~MOLDMARK
;
206 if (message
[i
-1].m_flag
& MMARK
)
207 message
[i
-1].m_flag
|= MOLDMARK
;
212 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]);
271 msglist_is_single
= FAL0
;
273 printf(catgets(catd
, CATSET
, 113,
274 "Non-numeric second argument\n"));
279 if (mb
.mb_threaded
) {
280 mx
= next_in_thread(&message
[i
-1]);
281 i
= mx
? mx
-message
+1 : msgCount
+1;
285 printf(catgets(catd
, CATSET
, 114,
286 "Referencing beyond EOF\n"));
289 } while (message
[i
-1].m_flag
== MHIDDEN
||
290 (message
[i
-1].m_flag
& MDELETED
) !=
296 msglist_is_single
= FAL0
;
300 if (mb
.mb_threaded
) {
303 i
= mx
? mx
-message
+1 : 0;
307 printf(catgets(catd
, CATSET
,
309 "Referencing before 1\n"));
312 } while ((message
[i
-1].m_flag
& MHIDDEN
) ||
313 (message
[i
-1].m_flag
& MDELETED
)
320 msglist_is_single
= FAL0
;
322 printf(catgets(catd
, CATSET
, 116,
323 "Non-numeric second argument\n"));
327 if (lexstring
[0] == ':') {
328 colresult
= evalcol(lexstring
[1]);
329 if (colresult
== 0) {
330 printf(catgets(catd
, CATSET
, 117,
331 "Unknown colon modifier \"%s\"\n"),
338 np
= add_to_namelist(&namelist
, &nmlsize
,
339 np
, savestr(lexstring
));
343 msglist_is_single
= FAL0
;
344 if (imap_search(lexstring
, f
) == STOP
)
353 msglist_is_single
= FAL0
;
354 lexnumber
= metamess(lexstring
[0], f
);
360 msglist_is_single
= FAL0
;
362 for (i
= 1; i
<= msgCount
; i
++) {
363 if ((message
[i
-1].m_flag
& MHIDDEN
) ||
364 (message
[i
-1].m_flag
&MDELETED
)
367 if (message
[i
-1].m_flag
&MOLDMARK
)
373 msglist_is_single
= FAL0
;
375 printf(catgets(catd
, CATSET
, 118,
376 "Can't mix \"*\" with anything\n"));
383 msglist_is_single
= FAL0
;
385 if (mb
.mb_type
== MB_IMAP
&& gotheaders
++ == 0)
386 imap_getheaders(1, msgCount
);
388 if (id
== NULL
&& (cp
= hfield1("in-reply-to", dot
))
391 idfield
= ID_IN_REPLY_TO
;
393 if (id
== NULL
&& (cp
= hfield1("references", dot
))
396 if ((enp
= extract(cp
, GREF
)) != NULL
) {
397 while (enp
->n_flink
!= NULL
)
399 id
= savestr(enp
->n_name
);
400 idfield
= ID_REFERENCES
;
404 printf(catgets(catd
, CATSET
, 227,
405 "Cannot determine parent Message-ID of the current message\n"));
411 msglist_is_single
= FAL0
;
418 np
= add_to_namelist(&namelist
, &nmlsize
, np
, NULL
);
422 for (i
= 0; i
< msgCount
; i
++) {
423 if (!(message
[i
].m_flag
& MHIDDEN
) &&
424 (message
[i
].m_flag
& MDELETED
) ==
432 printf(catgets(catd
, CATSET
, 119,
433 "No applicable messages.\n"));
439 if ((topen
|| tback
) && mc
== 0) {
440 for (i
= 0; i
< msgCount
; i
++)
441 if (message
[i
].m_flag
& MMARK
)
446 "No previously marked messages.\n" :
447 "No messages satisfy (criteria).\n");
453 * If no numbers were given, mark all of the messages,
454 * so that we can unmark any whose sender was not selected
455 * if any user names were given.
458 if ((np
> namelist
|| colmod
!= 0 || id
) && mc
== 0)
459 for (i
= 1; i
<= msgCount
; i
++) {
460 if (!(message
[i
-1].m_flag
& MHIDDEN
) &&
461 (message
[i
-1].m_flag
& MDELETED
) ==
467 * If any names were given, go through and eliminate any
468 * messages whose senders were not requested.
471 if (np
> namelist
|| id
) {
472 int allnet
= value("allnet") != NULL
;
475 if (mb
.mb_type
== MB_IMAP
&& gotheaders
++ == 0)
476 imap_getheaders(1, msgCount
);
478 for (i
= 1; i
<= msgCount
; i
++) {
481 for (nq
= &namelist
[0]; *nq
!= NULL
; nq
++) {
483 if (matchsubj(*nq
, i
)) {
489 if (matchsender(*nq
, i
,
497 if (mc
== 0 && id
&& matchmid(id
, idfield
, i
))
504 * Make sure we got some decent messages.
508 for (i
= 1; i
<= msgCount
; i
++)
509 if (message
[i
-1].m_flag
& MMARK
) {
514 if (!inhook
&& np
> namelist
) {
515 printf(catgets(catd
, CATSET
, 120,
516 "No applicable messages from {%s"),
518 for (nq
= &namelist
[1]; *nq
!= NULL
; nq
++)
519 printf(catgets(catd
, CATSET
, 121,
521 printf(catgets(catd
, CATSET
, 122, "}\n"));
523 printf(catgets(catd
, CATSET
, 227,
524 "Parent message not found\n"));
531 * If any colon modifiers were given, go through and
532 * unmark any messages which do not satisfy the modifiers.
536 for (i
= 1; i
<= msgCount
; i
++) {
540 mp
= &message
[i
- 1];
541 for (colp
= &coltab
[0]; colp
->co_char
; colp
++)
542 if ((colp
->co_bit
& colmod
) &&
543 ((mp
->m_flag
& colp
->co_mask
)
544 == (unsigned)colp
->co_equal
))
549 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
550 if (mp
->m_flag
& MMARK
)
552 if (mp
>= &message
[msgCount
]) {
556 printf(tr(123, "No messages satisfy"));
557 for (colp
= &coltab
[0]; colp
->co_char
; colp
++)
558 if (colp
->co_bit
& colmod
)
559 printf(" :%c", colp
->co_char
);
574 * Turn the character after a colon modifier into a bit
584 for (colp
= &coltab
[0]; colp
->co_char
; colp
++)
585 if (colp
->co_char
== col
)
586 return(colp
->co_bit
);
591 * Check the passed message number for legality and proper flags.
592 * If f is MDELETED, then either kind will do. Otherwise, the message
593 * has to be undeleted.
596 check(int mesg
, int f
)
600 if (mesg
< 1 || mesg
> msgCount
) {
601 printf(catgets(catd
, CATSET
, 124,
602 "%d: Invalid message number\n"), mesg
);
605 mp
= &message
[mesg
-1];
606 if (mp
->m_flag
& MHIDDEN
|| (f
!= MDELETED
&&
607 (mp
->m_flag
& MDELETED
) != 0)) {
608 printf(catgets(catd
, CATSET
, 125,
609 "%d: Inappropriate message\n"), mesg
);
616 * Scan out the list of string arguments, shell style
620 getrawlist(const char *line
, size_t linesize
, char **argv
, int argc
,
623 char c
, *cp2
, quotec
;
630 linebuf
= ac_alloc(linesize
+ 1);
632 for (; blankchar(*cp
& 0377); cp
++);
635 if (argn
>= argc
- 1) {
636 printf(catgets(catd
, CATSET
, 126,
637 "Too many elements in the list; excess discarded.\n"));
642 while ((c
= *cp
) != '\0') {
644 if (quotec
!= '\0') {
649 } else if (c
== '\\')
656 case '0': case '1': case '2': case '3':
657 case '4': case '5': case '6': case '7':
659 if (*cp >= '0' && *cp <= '7')
660 c = c * 8 + *cp++ - '0';
661 if (*cp >= '0' && *cp <= '7')
662 c = c * 8 + *cp++ - '0';
685 if (cp
[-1]!=quotec
|| echolist
)
689 /*else if (c == '^') {
693 /\* null doesn't show up anyway *\/
694 else if ((c >= 'A' && c <= '_') ||
695 (c >= 'a' && c <= 'z'))
703 } else if (c
== '"' || c
== '\'') {
707 } else if (c
== '\\' && !echolist
) {
712 } else if (blankchar(c
& 0377))
718 argv
[argn
++] = savestr(linebuf
);
726 * scan out a single lexical item and return its token number,
727 * updating the string pointer passed **p. Also, store the value
728 * of the number or string scanned in lexnumber or lexstring as
729 * appropriate. In any event, store the scanned `thing' in lexstring.
754 int c
, level
, inquote
;
759 strncpy(lexstring
, string_stack
[regretp
], STRINGLEN
);
760 lexstring
[STRINGLEN
-1]='\0';
761 lexnumber
= numberstack
[regretp
];
762 return(regretstack
[regretp
--]);
769 * strip away leading white space.
776 * If no characters remain, we are at end of line,
786 * Select members of a message thread.
790 if (*cp
== '\0' || spacechar(*cp
&0377)) {
800 * If the leading character is a digit, scan
801 * the number and convert it on the fly.
802 * Return TNUMBER when done.
807 while (digitchar(c
)) {
808 lexnumber
= lexnumber
*10 + c
- '0';
818 * An IMAP SEARCH list. Note that TOPEN has always been included
819 * in singles[] in Mail and mailx. Thus although there is no formal
820 * definition for (LIST) lists, they do not collide with historical
821 * practice because a subject string (LIST) could never been matched
830 if ((c
= *cp
++&0377) == '\0') {
831 mtop
: fprintf(stderr
, "Missing \")\".\n");
834 if (inquote
&& c
== '\\') {
847 else if (spacechar(c
)) {
849 * Replace unquoted whitespace by single
850 * space characters, to make the string
851 * IMAP SEARCH conformant.
858 } while (c
!= ')' || level
> 0);
865 * Check for single character tokens; return such
869 for (lp
= &singles
[0]; lp
->l_char
!= 0; lp
++)
870 if (c
== lp
->l_char
) {
878 * We've got a string! Copy all the characters
879 * of the string into lexstring, until we see
880 * a null, space, or tab.
881 * If the lead character is a " or ', save it
882 * and scan until you get another.
886 if (c
== '\'' || c
== '"') {
891 if (quotec
== 0 && c
== '\\' && *cp
)
897 if (quotec
== 0 && blankchar(c
))
899 if ((size_t)(cp2
- lexstring
) < (size_t)STRINGLEN
- 1)
903 if (quotec
&& c
== 0) {
904 fprintf(stderr
, catgets(catd
, CATSET
, 127,
905 "Missing %c\n"), quotec
);
914 * Unscan the named token by pushing it onto the regret stack.
919 if (++regretp
>= REGDEP
)
920 panic(catgets(catd
, CATSET
, 128, "Too many regrets"));
921 regretstack
[regretp
] = token
;
922 lexstring
[STRINGLEN
-1] = '\0';
923 string_stack
[regretp
] = savestr(lexstring
);
924 numberstack
[regretp
] = lexnumber
;
928 * Reset all the scanner global variables.
938 * Find the first message whose flags & m == f and return
939 * its message number.
950 for (mp
= dot
; mb
.mb_threaded
? mp
!= NULL
: mp
< &message
[msgCount
];
951 mb
.mb_threaded
? mp
= next_in_thread(mp
) : mp
++) {
952 if (!(mp
->m_flag
& MHIDDEN
) && (mp
->m_flag
& m
) == (unsigned)f
)
953 return mp
- message
+ 1;
955 if (dot
> &message
[0]) {
956 for (mp
= dot
-1; mb
.mb_threaded
?
957 mp
!= NULL
: mp
>= &message
[0];
959 mp
= prev_in_thread(mp
) : mp
--) {
960 if (! (mp
->m_flag
& MHIDDEN
) &&
961 (mp
->m_flag
& m
) == (unsigned)f
)
962 return mp
- message
+ 1;
969 * See if the passed name sent the passed message number. Return true
973 matchsender(char *str
, int mesg
, int allnet
)
976 char *cp
= nameof(&message
[mesg
- 1], 0);
979 if ((*cp
== '@' || *cp
== '\0') &&
980 (*str
== '@' || *str
== '\0'))
984 } while (cp
++, *str
++ != '\0');
987 return !strcmp(str
, (value("showname") ? realname
: skin
)
988 (name1(&message
[mesg
- 1], 0)));
992 matchmid(char *id
, enum idfield idfield
, int mesg
)
997 if ((cp
= hfield1("message-id", &message
[mesg
- 1])) != NULL
) {
1000 return msgidcmp(id
, cp
) == 0;
1001 case ID_IN_REPLY_TO
:
1002 if ((np
= extract(id
, GREF
)) != NULL
)
1004 if (msgidcmp(np
->n_name
, cp
) == 0)
1006 } while ((np
= np
->n_flink
) != NULL
);
1014 * See if the given string matches inside the subject field of the
1015 * given message. For the purpose of the scan, we ignore case differences.
1016 * If it does, return true. The string search argument is assumed to
1017 * have the form "/search-string." If it is of the form "/," we use the
1018 * previous search string.
1021 static char lastscan
[128];
1024 matchsubj(char *str
, int mesg
)
1032 if (strlen(str
) == 0) {
1035 strncpy(lastscan
, str
, sizeof lastscan
);
1036 lastscan
[sizeof lastscan
- 1]='\0';
1038 mp
= &message
[mesg
-1];
1041 * Now look, ignoring case, for the word in the string.
1044 if (value("searchheaders") && (cp
= strchr(str
, ':'))) {
1046 cp2
= hfieldX(str
, mp
);
1050 cp2
= hfield1("subject", mp
);
1056 mime_fromhdr(&in
, &out
, TD_ICONV
);
1057 i
= substr(out
.s
, cp
);
1063 * Mark the named message by setting its mark bit.
1066 mark(int mesg
, int f
)
1072 if (i
< 1 || i
> msgCount
)
1073 panic(catgets(catd
, CATSET
, 129, "Bad message number to mark"));
1074 if (mb
.mb_threaded
== 1 && threadflag
) {
1075 if ((message
[i
-1].m_flag
& MHIDDEN
) == 0) {
1076 if (f
== MDELETED
||
1077 (message
[i
-1].m_flag
&MDELETED
) == 0)
1078 message
[i
-1].m_flag
|= MMARK
;
1080 if (message
[i
-1].m_child
) {
1081 mp
= message
[i
-1].m_child
;
1082 mark(mp
-message
+1, f
);
1083 for (mp
= mp
->m_younger
; mp
; mp
= mp
->m_younger
)
1084 mark(mp
-message
+1, f
);
1087 message
[i
-1].m_flag
|= MMARK
;
1091 * Unmark the named message.
1099 if (i
< 1 || i
> msgCount
)
1100 panic(catgets(catd
, CATSET
, 130,
1101 "Bad message number to unmark"));
1102 message
[i
-1].m_flag
&= ~MMARK
;
1106 * Return the message number corresponding to the passed meta character.
1109 metamess(int meta
, int f
)
1118 * First 'good' message left.
1120 mp
= mb
.mb_threaded
? threadroot
: &message
[0];
1121 while (mp
< &message
[msgCount
]) {
1122 if (!(mp
->m_flag
& MHIDDEN
) &&
1123 (mp
->m_flag
& MDELETED
) == (unsigned)f
)
1124 return(mp
- &message
[0] + 1);
1125 if (mb
.mb_threaded
) {
1126 mp
= next_in_thread(mp
);
1133 printf(catgets(catd
, CATSET
, 131,
1134 "No applicable messages\n"));
1139 * Last 'good message left.
1141 mp
= mb
.mb_threaded
? this_in_thread(threadroot
, -1) :
1142 &message
[msgCount
-1];
1143 while (mp
>= &message
[0]) {
1144 if (!(mp
->m_flag
& MHIDDEN
) &&
1145 (mp
->m_flag
& MDELETED
) == (unsigned)f
)
1146 return(mp
- &message
[0] + 1);
1147 if (mb
.mb_threaded
) {
1148 mp
= prev_in_thread(mp
);
1155 printf(catgets(catd
, CATSET
, 132,
1156 "No applicable messages\n"));
1163 m
= dot
- &message
[0] + 1;
1164 if ((dot
->m_flag
& MHIDDEN
) ||
1165 (dot
->m_flag
& MDELETED
) != (unsigned)f
) {
1166 printf(catgets(catd
, CATSET
, 133,
1167 "%d: Inappropriate message\n"), m
);
1174 * Previously current message.
1176 if (prevdot
== NULL
) {
1177 printf(catgets(catd
, CATSET
, 228,
1178 "No previously current message\n"));
1181 m
= prevdot
- &message
[0] + 1;
1182 if ((prevdot
->m_flag
& MHIDDEN
) ||
1183 (prevdot
->m_flag
& MDELETED
) != (unsigned)f
) {
1184 printf(catgets(catd
, CATSET
, 133,
1185 "%d: Inappropriate message\n"), m
);
1191 printf(catgets(catd
, CATSET
, 134,
1192 "Unknown metachar (%c)\n"), c
);