1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Client-side implementation of the IMAP SEARCH command. This is used
3 *@ for folders not located on IMAP servers, or for IMAP servers that do
4 *@ not implement the SEARCH command.
6 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
7 * Copyright (c) 2012 - 2018 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
11 * Gunnar Ritter. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by Gunnar Ritter
24 * and his contributors.
25 * 4. Neither the name of Gunnar Ritter nor the names of his contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY GUNNAR RITTER AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL GUNNAR RITTER OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 #define n_FILE imap_search
44 #ifndef HAVE_AMALGAMATION
49 #ifdef HAVE_IMAP_SEARCH
52 ITBAD
, ITEOD
, ITBOL
, ITEOL
, ITAND
, ITSET
, ITALL
, ITANSWERED
,
53 ITBCC
, ITBEFORE
, ITBODY
,
63 ITSEEN
, ITSENTBEFORE
, ITSENTON
, ITSENTSINCE
, ITSINCE
, ITSMALLER
,
66 ITUID
, ITUNANSWERED
, ITUNDELETED
, ITUNDRAFT
, ITUNFLAGGED
, ITUNKEYWORD
,
84 static struct itlex
const _it_strings
[] = {
86 { "ANSWERED", ITANSWERED
},
88 { "BEFORE", ITBEFORE
},
91 { "DELETED", ITDELETED
},
93 { "FLAGGED", ITFLAGGED
},
95 { "HEADER", ITHEADER
},
96 { "KEYWORD", ITKEYWORD
},
97 { "LARGER", ITLARGER
},
103 { "RECENT", ITRECENT
},
105 { "SENTBEFORE", ITSENTBEFORE
},
106 { "SENTON", ITSENTON
},
107 { "SENTSINCE", ITSENTSINCE
},
108 { "SINCE", ITSINCE
},
109 { "SMALLER", ITSMALLER
},
110 { "SUBJECT", ITSUBJECT
},
114 { "UNANSWERED", ITUNANSWERED
},
115 { "UNDELETED", ITUNDELETED
},
116 { "UNDRAFT", ITUNDRAFT
},
117 { "UNFLAGGED", ITUNFLAGGED
},
118 { "UNKEYWORD", ITUNKEYWORD
},
119 { "UNSEEN", ITUNSEEN
},
123 static struct itnode
*_it_tree
;
124 static char *_it_begin
;
125 static enum itoken _it_token
;
126 static uiz_t _it_number
;
127 static void *_it_args
[2];
128 static size_t _it_need_headers
;
130 static enum okay
itparse(char const *spec
, char const **xp
, int sub
);
131 static enum okay
itscan(char const *spec
, char const **xp
);
132 static enum okay
itsplit(char const *spec
, char const **xp
);
133 static enum okay
itstring(void **tp
, char const *spec
, char const **xp
);
134 static int itexecute(struct mailbox
*mp
, struct message
*m
,
135 size_t c
, struct itnode
*n
);
137 static time_t _imap_read_date(char const *cp
);
138 static char * _imap_quotestr(char const *s
);
139 static char * _imap_unquotestr(char const *s
);
141 static bool_t
matchfield(struct message
*m
, char const *field
,
143 static int matchenvelope(struct message
*m
, char const *field
,
145 static char * mkenvelope(struct name
*np
);
146 static char const * around(char const *cp
);
149 itparse(char const *spec
, char const **xp
, int sub
)
152 struct itnode n
, *z
, *ittree
;
157 while ((rv
= itscan(spec
, xp
)) == OKAY
&& _it_token
!= ITBAD
&&
158 _it_token
!= ITEOD
) {
160 memset(&n
, 0, sizeof n
);
174 n_err(_("Excess in )\n"));
182 if ((rv
= itparse(spec
, xp
, sub
+ 1)) == STOP
)
185 if ((n
.n_x
= _it_tree
) == NULL
) {
186 n_err(_("Criterion for NOT missing: >>> %s <<<\n"), around(*xp
));
193 /* <search-key1> <search-key2> */
195 if ((rv
= itparse(spec
, xp
, sub
+ 1)) == STOP
)
197 if ((n
.n_x
= _it_tree
) == NULL
) {
198 n_err(_("First criterion for OR missing: >>> %s <<<\n"),
204 if ((rv
= itparse(spec
, xp
, sub
+ 1)) == STOP
)
207 if ((n
.n_y
= _it_tree
) == NULL
) {
208 n_err(_("Second criterion for OR missing: >>> %s <<<\n"),
215 n
.n_token
= _it_token
;
222 if (_it_tree
== NULL
) {
223 _it_tree
= salloc(sizeof *_it_tree
);
227 _it_tree
= salloc(sizeof *_it_tree
);
228 _it_tree
->n_token
= ITAND
;
230 _it_tree
->n_y
= salloc(sizeof *_it_tree
->n_y
);
233 if (sub
&& level
== 0)
242 itscan(char const *spec
, char const **xp
)
248 while (spacechar(*spec
))
260 while (spacechar(*spec
))
267 #define __GO(C) ((C) != '\0' && (C) != '(' && (C) != ')' && !spacechar(C))
268 for (i
= 0; _it_strings
[i
].s_string
!= NULL
; ++i
) {
269 n
= strlen(_it_strings
[i
].s_string
);
270 if (!ascncasecmp(spec
, _it_strings
[i
].s_string
, n
) && !__GO(spec
[n
])) {
271 _it_token
= _it_strings
[i
].s_token
;
273 while (spacechar(*spec
))
275 rv
= itsplit(spec
, xp
);
279 if (digitchar(*spec
)) {
280 n_idec_uiz_cp(&_it_number
, spec
, 10, xp
);
287 n_err(_("Bad SEARCH criterion: "));
288 for (i
= 0; __GO(spec
[i
]); ++i
)
290 n_err(_("%.*s: >>> %s <<<\n"), i
, spec
, around(*xp
));
301 itsplit(char const *spec
, char const **xp
)
318 rv
= itstring(_it_args
, spec
, xp
);
329 if ((rv
= itstring(_it_args
, spec
, xp
)) != OKAY
)
331 if ((t
= _imap_read_date(_it_args
[0])) == (time_t)-1) {
332 n_err(_("Invalid date %s: >>> %s <<<\n"),
333 (char*)_it_args
[0], around(*xp
));
341 /* <field-name> <string> */
343 if ((rv
= itstring(_it_args
, spec
, xp
)) != OKAY
)
346 if ((rv
= itstring(_it_args
+ 1, spec
, xp
)) != OKAY
)
351 /* <flag> */ /* TODO use table->flag map search instead */
352 if ((rv
= itstring(_it_args
, spec
, xp
)) != OKAY
)
354 if (!asccasecmp(_it_args
[0], "\\Seen"))
356 else if (!asccasecmp(_it_args
[0], "\\Deleted"))
357 _it_number
= MDELETED
;
358 else if (!asccasecmp(_it_args
[0], "\\Recent"))
360 else if (!asccasecmp(_it_args
[0], "\\Flagged"))
361 _it_number
= MFLAGGED
;
362 else if (!asccasecmp(_it_args
[0], "\\Answered"))
363 _it_number
= MANSWERED
;
364 else if (!asccasecmp(_it_args
[0], "\\Draft"))
372 if ((rv
= itstring(_it_args
, spec
, xp
)) != OKAY
)
375 n_idec_uiz_cp(&_it_number
, _it_args
[0], 10, &cp
);
377 if (spacechar(*cp
) || *cp
== '\0')
379 n_err(_("Invalid size: >>> %s <<<\n"), around(*xp
));
384 n_err(_("Searching for UIDs is not supported: >>> %s <<<\n"),
398 itstring(void **tp
, char const *spec
, char const **xp
) /* XXX lesser derefs */
405 while (spacechar(*spec
))
407 if (*spec
== '\0' || *spec
== '(' || *spec
== ')') {
408 n_err(_("Missing string argument: >>> %s <<<\n"),
409 around(&(*xp
)[spec
- *xp
]));
412 ap
= *tp
= salloc(strlen(spec
) +1);
415 if (inquote
&& **xp
== '\\')
417 else if (**xp
== '"')
419 else if (!inquote
&& (spacechar(**xp
) || **xp
== '(' || **xp
== ')')) {
426 *tp
= _imap_unquotestr(*tp
);
434 itexecute(struct mailbox
*mp
, struct message
*m
, size_t c
, struct itnode
*n
)
436 struct search_expr se
;
437 char *cp
, *line
= NULL
; /* TODO line pool */
444 n_err(_("Internal error: Empty node in SEARCH tree\n"));
449 switch (n
->n_token
) {
453 if (m
->m_time
== 0 && !(m
->m_flag
& MNOFROM
) &&
454 (ibuf
= setinput(mp
, m
, NEED_HEADER
)) != NULL
) {
455 if (readline_restart(ibuf
, &line
, &linesize
, 0) > 0)
456 m
->m_time
= unixtime(line
);
464 if ((cp
= hfield1("date", m
)) != NULL
)
465 m
->m_date
= rfctime(cp
);
471 switch (n
->n_token
) {
473 n_err(_("Internal SEARCH error: Lost token %d\n"), n
->n_token
);
477 rv
= itexecute(mp
, m
, c
, n
->n_x
) & itexecute(mp
, m
, c
, n
->n_y
);
486 rv
= ((m
->m_flag
& MANSWERED
) != 0);
489 rv
= matchenvelope(m
, "bcc", n
->n_v
);
492 rv
= UICMP(z
, m
->m_time
, <, n
->n_n
);
495 memset(&se
, 0, sizeof se
);
496 se
.ss_field
= "body";
498 rv
= message_match(m
, &se
, FAL0
);
501 rv
= matchenvelope(m
, "cc", n
->n_v
);
504 rv
= ((m
->m_flag
& MDELETED
) != 0);
507 rv
= ((m
->m_flag
& MDRAFTED
) != 0);
510 rv
= ((m
->m_flag
& MFLAGGED
) != 0);
513 rv
= matchenvelope(m
, "from", n
->n_v
);
516 rv
= matchfield(m
, n
->n_v
, n
->n_w
);
519 rv
= ((m
->m_flag
& n
->n_n
) != 0);
522 rv
= (m
->m_xsize
> n
->n_n
);
525 rv
= ((m
->m_flag
& (MNEW
| MREAD
)) == MNEW
);
528 rv
= !itexecute(mp
, m
, c
, n
->n_x
);
531 rv
= !(m
->m_flag
& MNEW
);
534 rv
= (UICMP(z
, m
->m_time
, >=, n
->n_n
) &&
535 UICMP(z
, m
->m_time
, <, n
->n_n
+ 86400));
538 rv
= itexecute(mp
, m
, c
, n
->n_x
) | itexecute(mp
, m
, c
, n
->n_y
);
541 rv
= ((m
->m_flag
& MNEW
) != 0);
544 rv
= ((m
->m_flag
& MREAD
) != 0);
547 rv
= UICMP(z
, m
->m_date
, <, n
->n_n
);
550 rv
= (UICMP(z
, m
->m_date
, >=, n
->n_n
) &&
551 UICMP(z
, m
->m_date
, <, n
->n_n
+ 86400));
554 rv
= UICMP(z
, m
->m_date
, >=, n
->n_n
);
557 rv
= UICMP(z
, m
->m_time
, >=, n
->n_n
);
560 rv
= UICMP(z
, m
->m_xsize
, <, n
->n_n
);
563 rv
= matchfield(m
, "subject", n
->n_v
);
566 memset(&se
, 0, sizeof se
);
567 se
.ss_field
= "text";
569 rv
= message_match(m
, &se
, TRU1
);
572 rv
= matchenvelope(m
, "to", n
->n_v
);
575 rv
= !(m
->m_flag
& MANSWERED
);
578 rv
= !(m
->m_flag
& MDELETED
);
581 rv
= !(m
->m_flag
& MDRAFTED
);
584 rv
= !(m
->m_flag
& MFLAGGED
);
587 rv
= !(m
->m_flag
& n
->n_n
);
590 rv
= !(m
->m_flag
& MREAD
);
599 _imap_read_date(char const *cp
)
602 si32_t year
, month
, day
, i
, tzdiff
;
609 n_idec_si32_cp(&day
, cp
, 10, &xp
);
610 if (day
<= 0 || day
> 31 || *xp
++ != '-')
614 if (!ascncasecmp(xp
, n_month_names
[i
], 3))
616 if (n_month_names
[++i
][0] == '\0')
622 n_idec_si32_cp(&year
, &xp
[4], 10, &yp
);
623 if (year
< 1970 || year
> 2037 || PTRCMP(yp
, !=, xp
+ 8))
625 if (yp
[0] != '\0' && (yp
[1] != '"' || yp
[2] != '\0'))
627 if ((t
= combinetime(year
, month
, day
, 0, 0, 0)) == (time_t)-1)
629 if((t2
= mktime(gmtime(&t
))) == (time_t)-1)
632 if((tmptr
= localtime(&t
)) == NULL
)
634 if (tmptr
->tm_isdst
> 0)
646 _imap_quotestr(char const *s
)
651 np
= n
= salloc(2 * strlen(s
) + 3);
654 if (*s
== '"' || *s
== '\\')
665 _imap_unquotestr(char const *s
)
675 np
= n
= salloc(strlen(s
) + 1);
690 matchfield(struct message
*m
, char const *field
, void const *what
)
696 if ((in
.s
= hfieldX(field
, m
)) == NULL
)
700 mime_fromhdr(&in
, &out
, TD_ICONV
);
701 rv
= substr(out
.s
, what
);
709 matchenvelope(struct message
*m
, char const *field
, void const *what
)
716 if ((cp
= hfieldX(field
, m
)) == NULL
)
719 for (np
= lextract(cp
, GFULL
); np
!= NULL
; np
= np
->n_flink
) {
720 if (!substr(np
->n_name
, what
) && !substr(mkenvelope(np
), what
))
732 mkenvelope(struct name
*np
)
735 char *ep
, *realnam
= NULL
, /**sourceaddr = NULL,*/ *localpart
,
736 *domainpart
, *cp
, *rp
, *xp
, *ip
;
739 bool_t hadphrase
= FAL0
;
742 in
.s
= np
->n_fullname
;
744 mime_fromhdr(&in
, &out
, TD_ICONV
);
745 rp
= ip
= ac_alloc(strlen(out
.s
) + 1);
746 for (cp
= out
.s
; *cp
; cp
++) {
752 if (cp
[0] == '\\' && cp
[1] != '\0')
758 while (cp
> out
.s
&& blankchar(cp
[-1]))
762 if (PTRCMP(xp
, <, cp
- 1) && *xp
== '"' && cp
[-1] == '"') {
782 if (level
&& cp
[1] != '\0')
795 localpart
= savestr(np
->n_name
);
796 if ((cp
= strrchr(localpart
, '@')) != NULL
) {
802 ep
= n_autorec_alloc(epsize
= strlen(np
->n_fullname
) * 2 + 40);
803 snprintf(ep
, epsize
, "(%s %s %s %s)",
804 realnam
? _imap_quotestr(realnam
) : "NIL",
805 /*sourceaddr ? _imap_quotestr(sourceaddr) :*/ "NIL",
806 _imap_quotestr(localpart
),
807 domainpart
? _imap_quotestr(domainpart
) : "NIL");
813 #define SURROUNDING 16
815 around(char const *cp
)
817 static char ab
[2 * SURROUNDING
+1];
822 for (i
= 0; i
< SURROUNDING
&& cp
> _it_begin
; ++i
)
824 for (i
= 0; i
< sizeof(ab
) -1; ++i
)
832 imap_search(char const *spec
, int f
)
834 static char *lastspec
;
841 if (strcmp(spec
, "()")) {
842 if (lastspec
!= NULL
)
845 lastspec
= sbufdup(spec
, i
);
846 } else if (lastspec
== NULL
) {
847 n_err(_("No last SEARCH criteria available\n"));
852 _it_begin
= lastspec
;
854 _it_need_headers
= FAL0
;
856 if ((rv
= imap_search1(spec
, f
) == OKAY
))
859 if (itparse(spec
, &xp
, 0) == STOP
){
866 if (_it_tree
== NULL
)
870 if (mb
.mb_type
== MB_IMAP
&& _it_need_headers
)
871 imap_getheaders(1, msgCount
);
874 for (i
= 0; UICMP(z
, i
, <, msgCount
); ++i
) {
875 if (message
[i
].m_flag
& MHIDDEN
)
877 if (f
== MDELETED
|| !(message
[i
].m_flag
& MDELETED
)) {
878 size_t j
= (int)(i
+ 1);
879 if (itexecute(&mb
, message
+ i
, j
, _it_tree
)){
891 #endif /* HAVE_IMAP_SEARCH */