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 - 2014 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
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 #ifndef HAVE_AMALGAMATION
46 EMPTY_FILE(imap_search
)
47 #ifdef HAVE_IMAP_SEARCH
50 ITBAD
, ITEOD
, ITBOL
, ITEOL
, ITAND
, ITSET
, ITALL
, ITANSWERED
,
51 ITBCC
, ITBEFORE
, ITBODY
,
61 ITSEEN
, ITSENTBEFORE
, ITSENTON
, ITSENTSINCE
, ITSINCE
, ITSMALLER
,
64 ITUID
, ITUNANSWERED
, ITUNDELETED
, ITUNDRAFT
, ITUNFLAGGED
, ITUNKEYWORD
,
82 static struct itlex
const _it_strings
[] = {
84 { "ANSWERED", ITANSWERED
},
86 { "BEFORE", ITBEFORE
},
89 { "DELETED", ITDELETED
},
91 { "FLAGGED", ITFLAGGED
},
93 { "HEADER", ITHEADER
},
94 { "KEYWORD", ITKEYWORD
},
95 { "LARGER", ITLARGER
},
101 { "RECENT", ITRECENT
},
103 { "SENTBEFORE", ITSENTBEFORE
},
104 { "SENTON", ITSENTON
},
105 { "SENTSINCE", ITSENTSINCE
},
106 { "SINCE", ITSINCE
},
107 { "SMALLER", ITSMALLER
},
108 { "SUBJECT", ITSUBJECT
},
112 { "UNANSWERED", ITUNANSWERED
},
113 { "UNDELETED", ITUNDELETED
},
114 { "UNDRAFT", ITUNDRAFT
},
115 { "UNFLAGGED", ITUNFLAGGED
},
116 { "UNKEYWORD", ITUNKEYWORD
},
117 { "UNSEEN", ITUNSEEN
},
121 static struct itnode
*_it_tree
;
122 static char *_it_begin
;
123 static enum itoken _it_token
;
124 static unsigned long _it_number
;
125 static void *_it_args
[2];
126 static size_t _it_need_headers
;
128 static enum okay
itparse(char const *spec
, char const **xp
, int sub
);
129 static enum okay
itscan(char const *spec
, char const **xp
);
130 static enum okay
itsplit(char const *spec
, char const **xp
);
131 static enum okay
itstring(void **tp
, char const *spec
, char const **xp
);
132 static int itexecute(struct mailbox
*mp
, struct message
*m
,
133 size_t c
, struct itnode
*n
);
134 static time_t _read_imap_date(char const *cp
);
135 static bool_t
matchfield(struct message
*m
, char const *field
,
137 static int matchenvelope(struct message
*m
, char const *field
,
139 static char * mkenvelope(struct name
*np
);
140 static char const * around(char const *cp
);
143 itparse(char const *spec
, char const **xp
, int sub
)
146 struct itnode n
, *z
, *ittree
;
151 while ((rv
= itscan(spec
, xp
)) == OKAY
&& _it_token
!= ITBAD
&&
152 _it_token
!= ITEOD
) {
154 memset(&n
, 0, sizeof n
);
168 fprintf(stderr
, "Excess in \")\".\n");
176 if ((rv
= itparse(spec
, xp
, sub
+ 1)) == STOP
)
179 if ((n
.n_x
= _it_tree
) == NULL
) {
180 fprintf(stderr
, "Criterion for NOT missing: >>> %s <<<\n",
188 /* <search-key1> <search-key2> */
190 if ((rv
= itparse(spec
, xp
, sub
+ 1)) == STOP
)
192 if ((n
.n_x
= _it_tree
) == NULL
) {
193 fprintf(stderr
, "First criterion for OR missing: >>> %s <<<\n",
199 if ((rv
= itparse(spec
, xp
, sub
+ 1)) == STOP
)
202 if ((n
.n_y
= _it_tree
) == NULL
) {
203 fprintf(stderr
, "Second criterion for OR missing: >>> %s <<<\n",
210 n
.n_token
= _it_token
;
217 if (_it_tree
== NULL
) {
218 _it_tree
= salloc(sizeof *_it_tree
);
222 _it_tree
= salloc(sizeof *_it_tree
);
223 _it_tree
->n_token
= ITAND
;
225 _it_tree
->n_y
= salloc(sizeof *_it_tree
->n_y
);
228 if (sub
&& level
== 0)
237 itscan(char const *spec
, char const **xp
)
243 while (spacechar(*spec
))
255 while (spacechar(*spec
))
262 #define __GO(C) ((C) != '\0' && (C) != '(' && (C) != ')' && !spacechar(C))
263 for (i
= 0; _it_strings
[i
].s_string
!= NULL
; ++i
) {
264 n
= strlen(_it_strings
[i
].s_string
);
265 if (!ascncasecmp(spec
, _it_strings
[i
].s_string
, n
) && !__GO(spec
[n
])) {
266 _it_token
= _it_strings
[i
].s_token
;
268 while (spacechar(*spec
))
270 rv
= itsplit(spec
, xp
);
274 if (digitchar(*spec
)) {
275 _it_number
= strtoul(spec
, UNCONST(xp
), 10);
281 fprintf(stderr
, "Bad SEARCH criterion \"");
282 while (__GO(*spec
)) {
283 putc(*spec
& 0377, stderr
);
288 fprintf(stderr
, "\": >>> %s <<<\n", around(*xp
));
297 itsplit(char const *spec
, char const **xp
)
314 rv
= itstring(_it_args
, spec
, xp
);
325 if ((rv
= itstring(_it_args
, spec
, xp
)) != OKAY
)
327 if ((t
= _read_imap_date(_it_args
[0])) == (time_t)-1) {
328 fprintf(stderr
, "Invalid date \"%s\": >>> %s <<<\n",
329 (char*)_it_args
[0], around(*xp
));
337 /* <field-name> <string> */
339 if ((rv
= itstring(_it_args
, spec
, xp
)) != OKAY
)
342 if ((rv
= itstring(_it_args
+ 1, spec
, xp
)) != OKAY
)
347 /* <flag> */ /* TODO use table->flag map search instead */
348 if ((rv
= itstring(_it_args
, spec
, xp
)) != OKAY
)
350 if (!asccasecmp(_it_args
[0], "\\Seen"))
352 else if (!asccasecmp(_it_args
[0], "\\Deleted"))
353 _it_number
= MDELETED
;
354 else if (!asccasecmp(_it_args
[0], "\\Recent"))
356 else if (!asccasecmp(_it_args
[0], "\\Flagged"))
357 _it_number
= MFLAGGED
;
358 else if (!asccasecmp(_it_args
[0], "\\Answered"))
359 _it_number
= MANSWERED
;
360 else if (!asccasecmp(_it_args
[0], "\\Draft"))
368 if ((rv
= itstring(_it_args
, spec
, xp
)) != OKAY
)
370 _it_number
= strtoul(_it_args
[0], &cp
, 10);
371 if (spacechar(*cp
) || *cp
== '\0')
373 fprintf(stderr
, "Invalid size: >>> %s <<<\n", around(*xp
));
379 "Searching for UIDs is not supported: >>> %s <<<\n", around(*xp
));
392 itstring(void **tp
, char const *spec
, char const **xp
) /* XXX lesser derefs */
399 while (spacechar(*spec
))
401 if (*spec
== '\0' || *spec
== '(' || *spec
== ')') {
402 fprintf(stderr
, "Missing string argument: >>> %s <<<\n",
403 around(&(*xp
)[spec
- *xp
]));
406 ap
= *tp
= salloc(strlen(spec
) +1);
409 if (inquote
&& **xp
== '\\')
411 else if (**xp
== '"')
413 else if (!inquote
&& (spacechar(**xp
) || **xp
== '(' || **xp
== ')')) {
420 *tp
= imap_unquotestr(*tp
);
428 itexecute(struct mailbox
*mp
, struct message
*m
, size_t c
, struct itnode
*n
)
430 struct search_expr se
;
431 char *cp
, *line
= NULL
; /* TODO line pool */
438 fprintf(stderr
, "Internal error: Empty node in SEARCH tree.\n");
443 switch (n
->n_token
) {
447 if (m
->m_time
== 0 && !(m
->m_flag
& MNOFROM
) &&
448 (ibuf
= setinput(mp
, m
, NEED_HEADER
)) != NULL
) {
449 if (readline_restart(ibuf
, &line
, &linesize
, 0) > 0)
450 m
->m_time
= unixtime(line
);
458 if ((cp
= hfield1("date", m
)) != NULL
)
459 m
->m_date
= rfctime(cp
);
465 switch (n
->n_token
) {
467 fprintf(stderr
, "Internal SEARCH error: Lost token %d\n", n
->n_token
);
471 rv
= itexecute(mp
, m
, c
, n
->n_x
) & itexecute(mp
, m
, c
, n
->n_y
);
474 rv
= UICMP(z
, c
, ==, n
->n_n
);
480 rv
= ((m
->m_flag
& MANSWERED
) != 0);
483 rv
= matchenvelope(m
, "bcc", n
->n_v
);
486 rv
= UICMP(z
, m
->m_time
, <, n
->n_n
);
489 se
.ss_sexpr
= n
->n_v
;
490 se
.ss_where
= "body";
491 rv
= message_match(m
, &se
, FAL0
);
494 rv
= matchenvelope(m
, "cc", n
->n_v
);
497 rv
= ((m
->m_flag
& MDELETED
) != 0);
500 rv
= ((m
->m_flag
& MDRAFTED
) != 0);
503 rv
= ((m
->m_flag
& MFLAGGED
) != 0);
506 rv
= matchenvelope(m
, "from", n
->n_v
);
509 rv
= matchfield(m
, n
->n_v
, n
->n_w
);
512 rv
= ((m
->m_flag
& n
->n_n
) != 0);
515 rv
= (m
->m_xsize
> n
->n_n
);
518 rv
= ((m
->m_flag
& (MNEW
| MREAD
)) == MNEW
);
521 rv
= !itexecute(mp
, m
, c
, n
->n_x
);
524 rv
= !(m
->m_flag
& MNEW
);
527 rv
= (UICMP(z
, m
->m_time
, >=, n
->n_n
) &&
528 UICMP(z
, m
->m_time
, <, n
->n_n
+ 86400));
531 rv
= itexecute(mp
, m
, c
, n
->n_x
) | itexecute(mp
, m
, c
, n
->n_y
);
534 rv
= ((m
->m_flag
& MNEW
) != 0);
537 rv
= ((m
->m_flag
& MREAD
) != 0);
540 rv
= UICMP(z
, m
->m_date
, <, n
->n_n
);
543 rv
= (UICMP(z
, m
->m_date
, >=, n
->n_n
) &&
544 UICMP(z
, m
->m_date
, <, n
->n_n
+ 86400));
547 rv
= UICMP(z
, m
->m_date
, >=, n
->n_n
);
550 rv
= UICMP(z
, m
->m_time
, >=, n
->n_n
);
553 rv
= UICMP(z
, m
->m_xsize
, <, n
->n_n
);
556 rv
= matchfield(m
, "subject", n
->n_v
);
559 se
.ss_sexpr
= n
->n_v
;
560 se
.ss_where
= "text";
561 rv
= message_match(m
, &se
, TRU1
);
564 rv
= matchenvelope(m
, "to", n
->n_v
);
567 rv
= !(m
->m_flag
& MANSWERED
);
570 rv
= !(m
->m_flag
& MDELETED
);
573 rv
= !(m
->m_flag
& MDRAFTED
);
576 rv
= !(m
->m_flag
& MFLAGGED
);
579 rv
= !(m
->m_flag
& n
->n_n
);
582 rv
= !(m
->m_flag
& MREAD
);
591 _read_imap_date(char const *cp
)
593 time_t t
= (time_t)-1;
594 int year
, month
, day
, i
, tzdiff
;
601 day
= strtol(cp
, &xp
, 10);
602 if (day
<= 0 || day
> 31 || *xp
++ != '-')
606 if (!ascncasecmp(xp
, month_names
[i
], 3))
608 if (month_names
[++i
][0] == '\0')
614 year
= strtol(xp
+ 4, &yp
, 10);
615 if (year
< 1970 || year
> 2037 || PTRCMP(yp
, !=, xp
+ 8))
617 if (yp
[0] != '\0' && (yp
[1] != '"' || yp
[2] != '\0'))
619 if ((t
= combinetime(year
, month
, day
, 0, 0, 0)) == (time_t)-1)
621 tzdiff
= t
- mktime(gmtime(&t
));
622 tmptr
= localtime(&t
);
623 if (tmptr
->tm_isdst
> 0)
632 matchfield(struct message
*m
, char const *field
, void const *what
)
638 if ((in
.s
= hfieldX(field
, m
)) == NULL
)
642 mime_fromhdr(&in
, &out
, TD_ICONV
);
643 rv
= substr(out
.s
, what
);
651 matchenvelope(struct message
*m
, char const *field
, void const *what
)
658 if ((cp
= hfieldX(field
, m
)) == NULL
)
661 for (np
= lextract(cp
, GFULL
); np
!= NULL
; np
= np
->n_flink
) {
662 if (!substr(np
->n_name
, what
) && !substr(mkenvelope(np
), what
))
674 mkenvelope(struct name
*np
)
677 char *ep
, *realnam
= NULL
, *sourceaddr
= NULL
, *localpart
= NULL
,
678 *domainpart
= NULL
, *cp
, *rp
, *xp
, *ip
;
681 bool_t hadphrase
= FAL0
;
684 in
.s
= np
->n_fullname
;
686 mime_fromhdr(&in
, &out
, TD_ICONV
);
687 rp
= ip
= ac_alloc(strlen(out
.s
) + 1);
688 for (cp
= out
.s
; *cp
; cp
++) {
694 if (cp
[0] == '\\' && cp
[1] != '\0')
700 while (cp
> out
.s
&& blankchar(cp
[-1]))
704 if (PTRCMP(xp
, <, cp
- 1) && *xp
== '"' && cp
[-1] == '"') {
724 if (level
&& cp
[1] != '\0')
737 localpart
= savestr(np
->n_name
);
738 if ((cp
= strrchr(localpart
, '@')) != NULL
) {
743 ep
= salloc(epsize
= strlen(np
->n_fullname
) * 2 + 40);
744 snprintf(ep
, epsize
, "(%s %s %s %s)",
745 realnam
? imap_quotestr(realnam
) : "NIL",
746 sourceaddr
? imap_quotestr(sourceaddr
) : "NIL",
747 localpart
? imap_quotestr(localpart
) : "NIL",
748 domainpart
? imap_quotestr(domainpart
) : "NIL");
754 #define SURROUNDING 16
756 around(char const *cp
)
758 static char ab
[2 * SURROUNDING
+1];
763 for (i
= 0; i
< SURROUNDING
&& cp
> _it_begin
; ++i
)
765 for (i
= 0; i
< sizeof(ab
) -1; ++i
)
773 imap_search(char const *spec
, int f
)
775 static char *lastspec
;
782 if (strcmp(spec
, "()")) {
783 if (lastspec
!= NULL
)
786 lastspec
= sbufdup(spec
, i
);
787 } else if (lastspec
== NULL
) {
788 fprintf(stderr
, tr(524, "No last SEARCH criteria available.\n"));
792 _it_begin
= lastspec
;
794 _it_need_headers
= FAL0
;
796 if ((rv
= imap_search1(spec
, f
) == OKAY
))
800 if (itparse(spec
, &xp
, 0) == STOP
)
802 if (_it_tree
== NULL
) {
808 if (mb
.mb_type
== MB_IMAP
&& _it_need_headers
)
809 imap_getheaders(1, msgCount
);
812 for (i
= 0; UICMP(z
, i
, <, msgCount
); ++i
) {
813 if (message
[i
].m_flag
& MHIDDEN
)
815 if (f
== MDELETED
|| !(message
[i
].m_flag
& MDELETED
)) {
816 size_t j
= (int)(i
+ 1);
817 if (itexecute(&mb
, message
+ i
, j
, _it_tree
))
829 #endif /* HAVE_IMAP_SEARCH */
831 /* vim:set fenc=utf-8:s-it-mode */