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
40 #ifndef HAVE_AMALGAMATION
49 static char **add_to_namelist(char ***namelist
, size_t *nmlsize
,
50 char **np
, char *string
);
51 static int markall(char *buf
, int f
);
52 static int evalcol(int col
);
53 static int check(int mesg
, int f
);
54 static int scan(char **sp
);
55 static void regret(int token
);
56 static void scaninit(void);
57 static int matchsender(char *str
, int mesg
, int allnet
);
58 static int matchmid(char *id
, enum idfield idfield
, int mesg
);
59 static int matchsubj(char *str
, int mesg
);
60 static void unmark(int mesg
);
61 static int metamess(int meta
, int f
);
63 static size_t STRINGLEN
;
65 static int lexnumber
; /* Number of TNUMBER from scan() */
66 static char *lexstring
; /* String from TSTRING, scan() */
67 static int regretp
; /* Pointer to TOS of regret tokens */
68 static int regretstack
[REGDEP
]; /* Stack of regretted tokens */
69 static char *string_stack
[REGDEP
]; /* Stack of regretted strings */
70 static int numberstack
[REGDEP
]; /* Stack of regretted numbers */
71 static int threadflag
; /* mark entire threads */
74 * Convert the user string of message numbers and
75 * store the numbers into vector.
77 * Returns the count of messages picked up or -1 on error.
80 getmsglist(char *buf
, int *vector
, int flags
)
86 msglist_is_single
= FAL0
;
92 msglist_is_single
= TRU1
;
93 if (markall(buf
, flags
) < 0)
98 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
99 if (mp
->m_flag
& MMARK
) {
100 if ((mp
->m_flag
& MNEWEST
) == 0)
101 unmark(mp
- &message
[0] + 1);
109 if (mb
.mb_threaded
== 0) {
110 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
111 if (mp
->m_flag
& MMARK
)
112 *ip
++ = mp
- &message
[0] + 1;
114 for (mp
= threadroot
; mp
; mp
= next_in_thread(mp
))
115 if (mp
->m_flag
& MMARK
)
116 *ip
++ = mp
- &message
[0] + 1;
119 if ((size_t)(ip
- vector
) != 1)
120 msglist_is_single
= FAL0
;
125 * Mark all messages that the user wanted from the command
126 * line in the message structure. Return 0 on success, -1
131 * Bit values for colon modifiers.
134 #define CMNEW 01 /* New messages */
135 #define CMOLD 02 /* Old messages */
136 #define CMUNREAD 04 /* Unread messages */
137 #define CMDELETED 010 /* Deleted messages */
138 #define CMREAD 020 /* Read messages */
139 #define CMFLAG 040 /* Flagged messages */
140 #define CMANSWER 0100 /* Answered messages */
141 #define CMDRAFT 0200 /* Draft messages */
142 #define CMSPAM 0400 /* Spam messages */
145 * The following table describes the letters which can follow
146 * the colon and gives the corresponding modifier bit.
149 static struct coltab
{
150 char co_char
; /* What to find past : */
151 int co_bit
; /* Associated modifier bit */
152 int co_mask
; /* m_status bits to mask */
153 int co_equal
; /* ... must equal this */
155 { 'n', CMNEW
, MNEW
, MNEW
},
156 { 'o', CMOLD
, MNEW
, 0 },
157 { 'u', CMUNREAD
, MREAD
, 0 },
158 { 'd', CMDELETED
, MDELETED
, MDELETED
},
159 { 'r', CMREAD
, MREAD
, MREAD
},
160 { 'f', CMFLAG
, MFLAGGED
, MFLAGGED
},
161 { 'a', CMANSWER
, MANSWERED
, MANSWERED
},
162 { 't', CMDRAFT
, MDRAFTED
, MDRAFTED
},
163 { 's', CMSPAM
, MSPAM
, MSPAM
},
167 static int lastcolmod
;
170 add_to_namelist(char ***namelist
, size_t *nmlsize
, char **np
, char *string
)
174 if ((idx
= np
- *namelist
) >= *nmlsize
) {
175 *namelist
= srealloc(*namelist
, (*nmlsize
+= 8) * sizeof *np
);
176 np
= &(*namelist
)[idx
];
183 markall(char *buf
, int f
)
185 #define markall_ret(i) do { retval = i; goto out; } while (0);
188 struct message
*mp
, *mx
;
189 char **namelist
, *bufp
, *id
= NULL
, *cp
;
190 int tok
, beg
, mc
, star
, other
, valdot
, colmod
, colresult
, topen
, tback
;
192 enum idfield idfield
= ID_REFERENCES
;
197 lexstring
= ac_alloc(STRINGLEN
= 2 * strlen(buf
) + 1);
198 valdot
= dot
- &message
[0] + 1;
200 for (i
= 1; i
<= msgCount
; i
++) {
201 message
[i
-1].m_flag
&= ~MOLDMARK
;
202 if (message
[i
-1].m_flag
& MMARK
)
203 message
[i
-1].m_flag
|= MOLDMARK
;
208 namelist
= smalloc((nmlsize
= 8) * sizeof *namelist
);
221 while (tok
!= TEOL
) {
226 printf(tr(112, "No numbers mixed with *\n"));
232 if (check(lexnumber
, f
))
235 while (mb
.mb_threaded
? 1 : i
<= lexnumber
) {
236 if (!(message
[i
-1].m_flag
&MHIDDEN
) &&
238 (message
[i
-1].m_flag
&
241 if (mb
.mb_threaded
) {
244 mx
= next_in_thread(&message
[i
-1]);
266 msglist_is_single
= FAL0
;
269 "Non-numeric second argument\n"));
274 if (mb
.mb_threaded
) {
275 mx
= next_in_thread(&message
[i
-1]);
276 i
= mx
? mx
-message
+1 : msgCount
+1;
281 "Referencing beyond EOF\n"));
284 } while (message
[i
-1].m_flag
== MHIDDEN
||
285 (message
[i
-1].m_flag
& MDELETED
) !=
291 msglist_is_single
= FAL0
;
295 if (mb
.mb_threaded
) {
298 i
= mx
? mx
-message
+1 : 0;
303 "Referencing before 1\n"));
306 } while ((message
[i
-1].m_flag
& MHIDDEN
) ||
307 (message
[i
-1].m_flag
& MDELETED
)
314 msglist_is_single
= FAL0
;
317 "Non-numeric second argument\n"));
321 if (lexstring
[0] == ':') {
322 colresult
= evalcol(lexstring
[1]);
323 if (colresult
== 0) {
325 "Unknown colon modifier \"%s\"\n"),
332 np
= add_to_namelist(&namelist
, &nmlsize
,
333 np
, savestr(lexstring
));
337 msglist_is_single
= FAL0
;
338 if (imap_search(lexstring
, f
) == STOP
)
347 msglist_is_single
= FAL0
;
348 lexnumber
= metamess(lexstring
[0], f
);
354 msglist_is_single
= FAL0
;
356 for (i
= 1; i
<= msgCount
; i
++) {
357 if ((message
[i
-1].m_flag
& MHIDDEN
) ||
358 (message
[i
-1].m_flag
&MDELETED
)
361 if (message
[i
-1].m_flag
&MOLDMARK
)
367 msglist_is_single
= FAL0
;
370 "Can't mix \"*\" with anything\n"));
377 msglist_is_single
= FAL0
;
379 if (mb
.mb_type
== MB_IMAP
&& gotheaders
++ == 0)
380 imap_getheaders(1, msgCount
);
382 if (id
== NULL
&& (cp
= hfield1("in-reply-to", dot
))
385 idfield
= ID_IN_REPLY_TO
;
387 if (id
== NULL
&& (cp
= hfield1("references", dot
))
390 if ((enp
= extract(cp
, GREF
)) != NULL
) {
391 while (enp
->n_flink
!= NULL
)
393 id
= savestr(enp
->n_name
);
394 idfield
= ID_REFERENCES
;
399 "Cannot determine parent Message-ID of the current message\n"));
405 msglist_is_single
= FAL0
;
412 np
= add_to_namelist(&namelist
, &nmlsize
, np
, NULL
);
416 for (i
= 0; i
< msgCount
; i
++) {
417 if (!(message
[i
].m_flag
& MHIDDEN
) &&
418 (message
[i
].m_flag
& MDELETED
) ==
427 "No applicable messages.\n"));
433 if ((topen
|| tback
) && mc
== 0) {
434 for (i
= 0; i
< msgCount
; i
++)
435 if (message
[i
].m_flag
& MMARK
)
440 "No previously marked messages.\n" :
441 "No messages satisfy (criteria).\n");
447 * If no numbers were given, mark all of the messages,
448 * so that we can unmark any whose sender was not selected
449 * if any user names were given.
452 if ((np
> namelist
|| colmod
!= 0 || id
) && mc
== 0)
453 for (i
= 1; i
<= msgCount
; i
++) {
454 if (!(message
[i
-1].m_flag
& MHIDDEN
) &&
455 (message
[i
-1].m_flag
& MDELETED
) ==
461 * If any names were given, go through and eliminate any
462 * messages whose senders were not requested.
465 if (np
> namelist
|| id
) {
466 int allnet
= value("allnet") != NULL
;
469 if (mb
.mb_type
== MB_IMAP
&& gotheaders
++ == 0)
470 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
))
501 * Make sure we got some decent messages.
505 for (i
= 1; i
<= msgCount
; i
++)
506 if (message
[i
-1].m_flag
& MMARK
) {
511 if (!inhook
&& np
> namelist
) {
513 "No applicable messages from {%s"),
515 for (nq
= &namelist
[1]; *nq
!= NULL
; nq
++)
516 printf(tr(121, ", %s"), *nq
);
517 printf(tr(122, "}\n"));
519 printf(tr(227, "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
++) {
535 mp
= &message
[i
- 1];
536 for (colp
= &coltab
[0]; colp
->co_char
; colp
++)
537 if ((colp
->co_bit
& colmod
) &&
538 ((mp
->m_flag
& colp
->co_mask
)
539 == (unsigned)colp
->co_equal
))
544 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
545 if (mp
->m_flag
& MMARK
)
547 if (mp
>= &message
[msgCount
]) {
551 printf(tr(123, "No messages satisfy"));
552 for (colp
= &coltab
[0]; colp
->co_char
; colp
++)
553 if (colp
->co_bit
& colmod
)
554 printf(" :%c", colp
->co_char
);
569 * Turn the character after a colon modifier into a bit
579 for (colp
= &coltab
[0]; colp
->co_char
; colp
++)
580 if (colp
->co_char
== col
)
581 return(colp
->co_bit
);
586 * Check the passed message number for legality and proper flags.
587 * If f is MDELETED, then either kind will do. Otherwise, the message
588 * has to be undeleted.
591 check(int mesg
, int f
)
595 if (mesg
< 1 || mesg
> msgCount
) {
596 printf(tr(124, "%d: Invalid message number\n"), mesg
);
599 mp
= &message
[mesg
-1];
600 if (mp
->m_flag
& MHIDDEN
|| (f
!= MDELETED
&&
601 (mp
->m_flag
& MDELETED
) != 0)) {
602 printf(tr(125, "%d: Inappropriate message\n"), mesg
);
609 * Scan out the list of string arguments, shell style
613 getrawlist(const char *line
, size_t linesize
, char **argv
, int argc
,
616 char c
, *cp2
, quotec
;
623 linebuf
= ac_alloc(linesize
+ 1);
625 for (; blankchar(*cp
& 0377); cp
++);
628 if (argn
>= argc
- 1) {
630 "Too many elements in the list; excess discarded.\n"));
635 while ((c
= *cp
) != '\0') {
637 if (quotec
!= '\0') {
642 } else if (c
== '\\')
649 case '0': case '1': case '2': case '3':
650 case '4': case '5': case '6': case '7':
652 if (*cp >= '0' && *cp <= '7')
653 c = c * 8 + *cp++ - '0';
654 if (*cp >= '0' && *cp <= '7')
655 c = c * 8 + *cp++ - '0';
678 if (cp
[-1]!=quotec
|| echolist
)
682 /*else if (c == '^') {
686 /\* null doesn't show up anyway *\/
687 else if ((c >= 'A' && c <= '_') ||
688 (c >= 'a' && c <= 'z'))
696 } else if (c
== '"' || c
== '\'') {
700 } else if (c
== '\\' && !echolist
) {
705 } else if (blankchar(c
& 0377))
711 argv
[argn
++] = savestr(linebuf
);
719 * scan out a single lexical item and return its token number,
720 * updating the string pointer passed **p. Also, store the value
721 * of the number or string scanned in lexnumber or lexstring as
722 * appropriate. In any event, store the scanned `thing' in lexstring.
752 strncpy(lexstring
, string_stack
[regretp
], STRINGLEN
);
753 lexstring
[STRINGLEN
-1]='\0';
754 lexnumber
= numberstack
[regretp
];
755 return(regretstack
[regretp
--]);
762 * strip away leading white space.
769 * If no characters remain, we are at end of line,
779 * Select members of a message thread.
783 if (*cp
== '\0' || spacechar(*cp
&0377)) {
793 * If the leading character is a digit, scan
794 * the number and convert it on the fly.
795 * Return TNUMBER when done.
800 while (digitchar(c
)) {
801 lexnumber
= lexnumber
*10 + c
- '0';
811 * An IMAP SEARCH list. Note that TOPEN has always been included
812 * in singles[] in Mail and mailx. Thus although there is no formal
813 * definition for (LIST) lists, they do not collide with historical
814 * practice because a subject string (LIST) could never been matched
823 if ((c
= *cp
++&0377) == '\0') {
824 mtop
: fprintf(stderr
, "Missing \")\".\n");
827 if (inquote
&& c
== '\\') {
840 else if (spacechar(c
)) {
842 * Replace unquoted whitespace by single
843 * space characters, to make the string
844 * IMAP SEARCH conformant.
851 } while (c
!= ')' || level
> 0);
858 * Check for single character tokens; return such
862 for (lp
= &singles
[0]; lp
->l_char
!= 0; lp
++)
863 if (c
== lp
->l_char
) {
871 * We've got a string! Copy all the characters
872 * of the string into lexstring, until we see
873 * a null, space, or tab.
874 * If the lead character is a " or ', save it
875 * and scan until you get another.
879 if (c
== '\'' || c
== '"') {
884 if (quotec
== 0 && c
== '\\' && *cp
)
890 if (quotec
== 0 && blankchar(c
))
892 if ((size_t)(cp2
- lexstring
) < (size_t)STRINGLEN
- 1)
896 if (quotec
&& c
== 0) {
897 fprintf(stderr
, tr(127, "Missing %c\n"), quotec
);
906 * Unscan the named token by pushing it onto the regret stack.
911 if (++regretp
>= REGDEP
)
912 panic(tr(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
= hfield1("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
= hfieldX(str
, mp
);
1042 cp2
= hfield1("subject", mp
);
1048 mime_fromhdr(&in
, &out
, TD_ICONV
);
1049 i
= substr(out
.s
, cp
);
1055 * Mark the named message by setting its mark bit.
1058 mark(int mesg
, int f
)
1064 if (i
< 1 || i
> msgCount
)
1065 panic(tr(129, "Bad message number to mark"));
1066 if (mb
.mb_threaded
== 1 && threadflag
) {
1067 if ((message
[i
-1].m_flag
& MHIDDEN
) == 0) {
1068 if (f
== MDELETED
||
1069 (message
[i
-1].m_flag
&MDELETED
) == 0)
1070 message
[i
-1].m_flag
|= MMARK
;
1072 if (message
[i
-1].m_child
) {
1073 mp
= message
[i
-1].m_child
;
1074 mark(mp
-message
+1, f
);
1075 for (mp
= mp
->m_younger
; mp
; mp
= mp
->m_younger
)
1076 mark(mp
-message
+1, f
);
1079 message
[i
-1].m_flag
|= MMARK
;
1083 * Unmark the named message.
1091 if (i
< 1 || i
> (ui32_t
)msgCount
)
1092 panic(tr(130, "Bad message number to unmark"));
1093 message
[i
-1].m_flag
&= ~MMARK
;
1097 * Return the message number corresponding to the passed meta character.
1100 metamess(int meta
, int f
)
1109 * First 'good' message left.
1111 mp
= mb
.mb_threaded
? threadroot
: &message
[0];
1112 while (mp
< &message
[msgCount
]) {
1113 if (!(mp
->m_flag
& MHIDDEN
) &&
1114 (mp
->m_flag
& MDELETED
) == (unsigned)f
)
1115 return(mp
- &message
[0] + 1);
1116 if (mb
.mb_threaded
) {
1117 mp
= next_in_thread(mp
);
1124 printf(tr(131, "No applicable messages\n"));
1129 * Last 'good message left.
1131 mp
= mb
.mb_threaded
? this_in_thread(threadroot
, -1) :
1132 &message
[msgCount
-1];
1133 while (mp
>= &message
[0]) {
1134 if (!(mp
->m_flag
& MHIDDEN
) &&
1135 (mp
->m_flag
& MDELETED
) == (unsigned)f
)
1136 return(mp
- &message
[0] + 1);
1137 if (mb
.mb_threaded
) {
1138 mp
= prev_in_thread(mp
);
1145 printf(tr(132, "No applicable messages\n"));
1152 m
= dot
- &message
[0] + 1;
1153 if ((dot
->m_flag
& MHIDDEN
) ||
1154 (dot
->m_flag
& MDELETED
) != (unsigned)f
) {
1155 printf(tr(133, "%d: Inappropriate message\n"), m
);
1162 * Previously current message.
1164 if (prevdot
== NULL
) {
1165 printf(tr(228, "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(tr(133, "%d: Inappropriate message\n"), m
);
1177 printf(tr(134, "Unknown metachar (%c)\n"), c
);