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 - 2016 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 unsigned long _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 _it_number
= strtoul(spec
, n_UNCONST(xp
), 10);
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
)
374 _it_number
= strtoul(_it_args
[0], &cp
, 10);
375 if (spacechar(*cp
) || *cp
== '\0')
377 n_err(_("Invalid size: >>> %s <<<\n"), around(*xp
));
382 n_err(_("Searching for UIDs is not supported: >>> %s <<<\n"),
396 itstring(void **tp
, char const *spec
, char const **xp
) /* XXX lesser derefs */
403 while (spacechar(*spec
))
405 if (*spec
== '\0' || *spec
== '(' || *spec
== ')') {
406 n_err(_("Missing string argument: >>> %s <<<\n"),
407 around(&(*xp
)[spec
- *xp
]));
410 ap
= *tp
= salloc(strlen(spec
) +1);
413 if (inquote
&& **xp
== '\\')
415 else if (**xp
== '"')
417 else if (!inquote
&& (spacechar(**xp
) || **xp
== '(' || **xp
== ')')) {
424 *tp
= _imap_unquotestr(*tp
);
432 itexecute(struct mailbox
*mp
, struct message
*m
, size_t c
, struct itnode
*n
)
434 struct search_expr se
;
435 char *cp
, *line
= NULL
; /* TODO line pool */
442 n_err(_("Internal error: Empty node in SEARCH tree\n"));
447 switch (n
->n_token
) {
451 if (m
->m_time
== 0 && !(m
->m_flag
& MNOFROM
) &&
452 (ibuf
= setinput(mp
, m
, NEED_HEADER
)) != NULL
) {
453 if (readline_restart(ibuf
, &line
, &linesize
, 0) > 0)
454 m
->m_time
= unixtime(line
);
462 if ((cp
= hfield1("date", m
)) != NULL
)
463 m
->m_date
= rfctime(cp
);
469 switch (n
->n_token
) {
471 n_err(_("Internal SEARCH error: Lost token %d\n"), n
->n_token
);
475 rv
= itexecute(mp
, m
, c
, n
->n_x
) & itexecute(mp
, m
, c
, n
->n_y
);
478 rv
= UICMP(z
, c
, ==, n
->n_n
);
484 rv
= ((m
->m_flag
& MANSWERED
) != 0);
487 rv
= matchenvelope(m
, "bcc", n
->n_v
);
490 rv
= UICMP(z
, m
->m_time
, <, n
->n_n
);
493 se
.ss_sexpr
= n
->n_v
;
494 se
.ss_where
= "body";
495 rv
= message_match(m
, &se
, FAL0
);
498 rv
= matchenvelope(m
, "cc", n
->n_v
);
501 rv
= ((m
->m_flag
& MDELETED
) != 0);
504 rv
= ((m
->m_flag
& MDRAFTED
) != 0);
507 rv
= ((m
->m_flag
& MFLAGGED
) != 0);
510 rv
= matchenvelope(m
, "from", n
->n_v
);
513 rv
= matchfield(m
, n
->n_v
, n
->n_w
);
516 rv
= ((m
->m_flag
& n
->n_n
) != 0);
519 rv
= (m
->m_xsize
> n
->n_n
);
522 rv
= ((m
->m_flag
& (MNEW
| MREAD
)) == MNEW
);
525 rv
= !itexecute(mp
, m
, c
, n
->n_x
);
528 rv
= !(m
->m_flag
& MNEW
);
531 rv
= (UICMP(z
, m
->m_time
, >=, n
->n_n
) &&
532 UICMP(z
, m
->m_time
, <, n
->n_n
+ 86400));
535 rv
= itexecute(mp
, m
, c
, n
->n_x
) | itexecute(mp
, m
, c
, n
->n_y
);
538 rv
= ((m
->m_flag
& MNEW
) != 0);
541 rv
= ((m
->m_flag
& MREAD
) != 0);
544 rv
= UICMP(z
, m
->m_date
, <, n
->n_n
);
547 rv
= (UICMP(z
, m
->m_date
, >=, n
->n_n
) &&
548 UICMP(z
, m
->m_date
, <, n
->n_n
+ 86400));
551 rv
= UICMP(z
, m
->m_date
, >=, n
->n_n
);
554 rv
= UICMP(z
, m
->m_time
, >=, n
->n_n
);
557 rv
= UICMP(z
, m
->m_xsize
, <, n
->n_n
);
560 rv
= matchfield(m
, "subject", n
->n_v
);
563 se
.ss_sexpr
= n
->n_v
;
564 se
.ss_where
= "text";
565 rv
= message_match(m
, &se
, TRU1
);
568 rv
= matchenvelope(m
, "to", n
->n_v
);
571 rv
= !(m
->m_flag
& MANSWERED
);
574 rv
= !(m
->m_flag
& MDELETED
);
577 rv
= !(m
->m_flag
& MDRAFTED
);
580 rv
= !(m
->m_flag
& MFLAGGED
);
583 rv
= !(m
->m_flag
& n
->n_n
);
586 rv
= !(m
->m_flag
& MREAD
);
595 _imap_read_date(char const *cp
)
597 time_t t
= (time_t)-1;
598 int year
, month
, day
, i
, tzdiff
;
605 day
= strtol(cp
, &xp
, 10);
606 if (day
<= 0 || day
> 31 || *xp
++ != '-')
610 if (!ascncasecmp(xp
, month_names
[i
], 3))
612 if (month_names
[++i
][0] == '\0')
618 year
= strtol(xp
+ 4, &yp
, 10);
619 if (year
< 1970 || year
> 2037 || PTRCMP(yp
, !=, xp
+ 8))
621 if (yp
[0] != '\0' && (yp
[1] != '"' || yp
[2] != '\0'))
623 if ((t
= combinetime(year
, month
, day
, 0, 0, 0)) == (time_t)-1)
625 tzdiff
= t
- mktime(gmtime(&t
));
626 tmptr
= localtime(&t
);
627 if (tmptr
->tm_isdst
> 0)
636 _imap_quotestr(char const *s
)
641 np
= n
= salloc(2 * strlen(s
) + 3);
644 if (*s
== '"' || *s
== '\\')
655 _imap_unquotestr(char const *s
)
665 np
= n
= salloc(strlen(s
) + 1);
680 matchfield(struct message
*m
, char const *field
, void const *what
)
686 if ((in
.s
= hfieldX(field
, m
)) == NULL
)
690 mime_fromhdr(&in
, &out
, TD_ICONV
);
691 rv
= substr(out
.s
, what
);
699 matchenvelope(struct message
*m
, char const *field
, void const *what
)
706 if ((cp
= hfieldX(field
, m
)) == NULL
)
709 for (np
= lextract(cp
, GFULL
); np
!= NULL
; np
= np
->n_flink
) {
710 if (!substr(np
->n_name
, what
) && !substr(mkenvelope(np
), what
))
722 mkenvelope(struct name
*np
)
725 char *ep
, *realnam
= NULL
, *sourceaddr
= NULL
, *localpart
= NULL
,
726 *domainpart
= NULL
, *cp
, *rp
, *xp
, *ip
;
729 bool_t hadphrase
= FAL0
;
732 in
.s
= np
->n_fullname
;
734 mime_fromhdr(&in
, &out
, TD_ICONV
);
735 rp
= ip
= ac_alloc(strlen(out
.s
) + 1);
736 for (cp
= out
.s
; *cp
; cp
++) {
742 if (cp
[0] == '\\' && cp
[1] != '\0')
748 while (cp
> out
.s
&& blankchar(cp
[-1]))
752 if (PTRCMP(xp
, <, cp
- 1) && *xp
== '"' && cp
[-1] == '"') {
772 if (level
&& cp
[1] != '\0')
785 localpart
= savestr(np
->n_name
);
786 if ((cp
= strrchr(localpart
, '@')) != NULL
) {
791 ep
= salloc(epsize
= strlen(np
->n_fullname
) * 2 + 40);
792 snprintf(ep
, epsize
, "(%s %s %s %s)",
793 realnam
? _imap_quotestr(realnam
) : "NIL",
794 sourceaddr
? _imap_quotestr(sourceaddr
) : "NIL",
795 localpart
? _imap_quotestr(localpart
) : "NIL",
796 domainpart
? _imap_quotestr(domainpart
) : "NIL");
802 #define SURROUNDING 16
804 around(char const *cp
)
806 static char ab
[2 * SURROUNDING
+1];
811 for (i
= 0; i
< SURROUNDING
&& cp
> _it_begin
; ++i
)
813 for (i
= 0; i
< sizeof(ab
) -1; ++i
)
821 imap_search(char const *spec
, int f
)
823 static char *lastspec
;
830 if (strcmp(spec
, "()")) {
831 if (lastspec
!= NULL
)
834 lastspec
= sbufdup(spec
, i
);
835 } else if (lastspec
== NULL
) {
836 n_err(_("No last SEARCH criteria available\n"));
841 _it_begin
= lastspec
;
843 _it_need_headers
= FAL0
;
845 if ((rv
= imap_search1(spec
, f
) == OKAY
))
848 if (itparse(spec
, &xp
, 0) == STOP
){
855 if (_it_tree
== NULL
)
859 if (mb
.mb_type
== MB_IMAP
&& _it_need_headers
)
860 imap_getheaders(1, msgCount
);
863 for (i
= 0; UICMP(z
, i
, <, msgCount
); ++i
) {
864 if (message
[i
].m_flag
& MHIDDEN
)
866 if (f
== MDELETED
|| !(message
[i
].m_flag
& MDELETED
)) {
867 size_t j
= (int)(i
+ 1);
868 if (itexecute(&mb
, message
+ i
, j
, _it_tree
)){
880 #endif /* HAVE_IMAP_SEARCH */