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 - 2017 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 se
.ss_sexpr
= n
->n_v
;
496 se
.ss_where
= "body";
497 rv
= message_match(m
, &se
, FAL0
);
500 rv
= matchenvelope(m
, "cc", n
->n_v
);
503 rv
= ((m
->m_flag
& MDELETED
) != 0);
506 rv
= ((m
->m_flag
& MDRAFTED
) != 0);
509 rv
= ((m
->m_flag
& MFLAGGED
) != 0);
512 rv
= matchenvelope(m
, "from", n
->n_v
);
515 rv
= matchfield(m
, n
->n_v
, n
->n_w
);
518 rv
= ((m
->m_flag
& n
->n_n
) != 0);
521 rv
= (m
->m_xsize
> n
->n_n
);
524 rv
= ((m
->m_flag
& (MNEW
| MREAD
)) == MNEW
);
527 rv
= !itexecute(mp
, m
, c
, n
->n_x
);
530 rv
= !(m
->m_flag
& MNEW
);
533 rv
= (UICMP(z
, m
->m_time
, >=, n
->n_n
) &&
534 UICMP(z
, m
->m_time
, <, n
->n_n
+ 86400));
537 rv
= itexecute(mp
, m
, c
, n
->n_x
) | itexecute(mp
, m
, c
, n
->n_y
);
540 rv
= ((m
->m_flag
& MNEW
) != 0);
543 rv
= ((m
->m_flag
& MREAD
) != 0);
546 rv
= UICMP(z
, m
->m_date
, <, n
->n_n
);
549 rv
= (UICMP(z
, m
->m_date
, >=, n
->n_n
) &&
550 UICMP(z
, m
->m_date
, <, n
->n_n
+ 86400));
553 rv
= UICMP(z
, m
->m_date
, >=, n
->n_n
);
556 rv
= UICMP(z
, m
->m_time
, >=, n
->n_n
);
559 rv
= UICMP(z
, m
->m_xsize
, <, n
->n_n
);
562 rv
= matchfield(m
, "subject", n
->n_v
);
565 se
.ss_sexpr
= n
->n_v
;
566 se
.ss_where
= "text";
567 rv
= message_match(m
, &se
, TRU1
);
570 rv
= matchenvelope(m
, "to", n
->n_v
);
573 rv
= !(m
->m_flag
& MANSWERED
);
576 rv
= !(m
->m_flag
& MDELETED
);
579 rv
= !(m
->m_flag
& MDRAFTED
);
582 rv
= !(m
->m_flag
& MFLAGGED
);
585 rv
= !(m
->m_flag
& n
->n_n
);
588 rv
= !(m
->m_flag
& MREAD
);
597 _imap_read_date(char const *cp
)
599 time_t t
= (time_t)-1;
600 si32_t year
, month
, day
, i
, tzdiff
;
607 n_idec_si32_cp(&day
, cp
, 10, &xp
);
608 if (day
<= 0 || day
> 31 || *xp
++ != '-')
612 if (!ascncasecmp(xp
, n_month_names
[i
], 3))
614 if (n_month_names
[++i
][0] == '\0')
620 n_idec_si32_cp(&year
, &xp
[4], 10, &yp
);
621 if (year
< 1970 || year
> 2037 || PTRCMP(yp
, !=, xp
+ 8))
623 if (yp
[0] != '\0' && (yp
[1] != '"' || yp
[2] != '\0'))
625 if ((t
= combinetime(year
, month
, day
, 0, 0, 0)) == (time_t)-1)
627 tzdiff
= t
- mktime(gmtime(&t
));
628 tmptr
= localtime(&t
);
629 if (tmptr
->tm_isdst
> 0)
638 _imap_quotestr(char const *s
)
643 np
= n
= salloc(2 * strlen(s
) + 3);
646 if (*s
== '"' || *s
== '\\')
657 _imap_unquotestr(char const *s
)
667 np
= n
= salloc(strlen(s
) + 1);
682 matchfield(struct message
*m
, char const *field
, void const *what
)
688 if ((in
.s
= hfieldX(field
, m
)) == NULL
)
692 mime_fromhdr(&in
, &out
, TD_ICONV
);
693 rv
= substr(out
.s
, what
);
701 matchenvelope(struct message
*m
, char const *field
, void const *what
)
708 if ((cp
= hfieldX(field
, m
)) == NULL
)
711 for (np
= lextract(cp
, GFULL
); np
!= NULL
; np
= np
->n_flink
) {
712 if (!substr(np
->n_name
, what
) && !substr(mkenvelope(np
), what
))
724 mkenvelope(struct name
*np
)
727 char *ep
, *realnam
= NULL
, *sourceaddr
= NULL
, *localpart
= NULL
,
728 *domainpart
= NULL
, *cp
, *rp
, *xp
, *ip
;
731 bool_t hadphrase
= FAL0
;
734 in
.s
= np
->n_fullname
;
736 mime_fromhdr(&in
, &out
, TD_ICONV
);
737 rp
= ip
= ac_alloc(strlen(out
.s
) + 1);
738 for (cp
= out
.s
; *cp
; cp
++) {
744 if (cp
[0] == '\\' && cp
[1] != '\0')
750 while (cp
> out
.s
&& blankchar(cp
[-1]))
754 if (PTRCMP(xp
, <, cp
- 1) && *xp
== '"' && cp
[-1] == '"') {
774 if (level
&& cp
[1] != '\0')
787 localpart
= savestr(np
->n_name
);
788 if ((cp
= strrchr(localpart
, '@')) != NULL
) {
793 ep
= n_autorec_alloc(epsize
= strlen(np
->n_fullname
) * 2 + 40);
794 snprintf(ep
, epsize
, "(%s %s %s %s)",
795 realnam
? _imap_quotestr(realnam
) : "NIL",
796 sourceaddr
? _imap_quotestr(sourceaddr
) : "NIL",
797 localpart
? _imap_quotestr(localpart
) : "NIL",
798 domainpart
? _imap_quotestr(domainpart
) : "NIL");
804 #define SURROUNDING 16
806 around(char const *cp
)
808 static char ab
[2 * SURROUNDING
+1];
813 for (i
= 0; i
< SURROUNDING
&& cp
> _it_begin
; ++i
)
815 for (i
= 0; i
< sizeof(ab
) -1; ++i
)
823 imap_search(char const *spec
, int f
)
825 static char *lastspec
;
832 if (strcmp(spec
, "()")) {
833 if (lastspec
!= NULL
)
836 lastspec
= sbufdup(spec
, i
);
837 } else if (lastspec
== NULL
) {
838 n_err(_("No last SEARCH criteria available\n"));
843 _it_begin
= lastspec
;
845 _it_need_headers
= FAL0
;
847 if ((rv
= imap_search1(spec
, f
) == OKAY
))
850 if (itparse(spec
, &xp
, 0) == STOP
){
857 if (_it_tree
== NULL
)
861 if (mb
.mb_type
== MB_IMAP
&& _it_need_headers
)
862 imap_getheaders(1, msgCount
);
865 for (i
= 0; UICMP(z
, i
, <, msgCount
); ++i
) {
866 if (message
[i
].m_flag
& MHIDDEN
)
868 if (f
== MDELETED
|| !(message
[i
].m_flag
& MDELETED
)) {
869 size_t j
= (int)(i
+ 1);
870 if (itexecute(&mb
, message
+ i
, j
, _it_tree
)){
882 #endif /* HAVE_IMAP_SEARCH */