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 for (i
= 0; _it_strings
[i
].s_string
; i
++) {
263 n
= strlen(_it_strings
[i
].s_string
);
264 if (!ascncasecmp(spec
, _it_strings
[i
].s_string
, n
) &&
265 (spacechar(spec
[n
]) || spec
[n
] == '\0' ||
266 spec
[n
] == '(' || spec
[n
] == ')')) {
267 _it_token
= _it_strings
[i
].s_token
;
269 while (spacechar(*spec
))
271 rv
= itsplit(spec
, xp
);
275 if (digitchar(*spec
)) {
276 _it_number
= strtoul(spec
, UNCONST(xp
), 10);
277 if (spacechar(**xp
) || **xp
== '\0' || **xp
== '(' || **xp
== ')') {
282 fprintf(stderr
, "Bad SEARCH criterion \"");
283 while (*spec
&& !spacechar(*spec
) && *spec
!= '(' && *spec
!= ')') {
284 putc(*spec
& 0377, stderr
);
287 fprintf(stderr
, "\": >>> %s <<<\n", around(*xp
));
296 itsplit(char const *spec
, char const **xp
)
313 rv
= itstring(&_it_args
[0], spec
, xp
);
324 if ((rv
= itstring(&_it_args
[0], spec
, xp
)) != OKAY
)
326 if ((t
= _read_imap_date(_it_args
[0])) == (time_t)-1) {
327 fprintf(stderr
, "Invalid date \"%s\": >>> %s <<<\n",
328 (char*)_it_args
[0], around(*xp
));
336 /* <field-name> <string> */
338 if ((rv
= itstring(&_it_args
[0], spec
, xp
)) != OKAY
)
341 if ((rv
= itstring(&_it_args
[1], spec
, xp
)) != OKAY
)
347 if ((rv
= itstring(&_it_args
[0], spec
, xp
)) != OKAY
)
349 if (!asccasecmp(_it_args
[0], "\\Seen"))
351 else if (!asccasecmp(_it_args
[0], "\\Deleted"))
352 _it_number
= MDELETED
;
353 else if (!asccasecmp(_it_args
[0], "\\Recent"))
355 else if (!asccasecmp(_it_args
[0], "\\Flagged"))
356 _it_number
= MFLAGGED
;
357 else if (!asccasecmp(_it_args
[0], "\\Answered"))
358 _it_number
= MANSWERED
;
359 else if (!asccasecmp(_it_args
[0], "\\Draft"))
367 if ((rv
= itstring(&_it_args
[0], spec
, xp
)) != OKAY
)
369 _it_number
= strtoul(_it_args
[0], &cp
, 10);
370 if (spacechar(*cp
) || *cp
== '\0')
372 fprintf(stderr
, "Invalid size: >>> %s <<<\n", around(*xp
));
378 "Searching for UIDs is not supported: >>> %s <<<\n", around(*xp
));
391 itstring(void **tp
, char const *spec
, char const **xp
)
398 while (spacechar(*spec
))
400 if (*spec
== '\0' || *spec
== '(' || *spec
== ')') {
401 fprintf(stderr
, "Missing string argument: >>> %s <<<\n",
402 around(&(*xp
)[spec
- *xp
]));
405 ap
= *tp
= salloc(strlen(spec
) + 1);
408 if (inquote
&& **xp
== '\\')
410 else if (**xp
== '"')
412 else if (!inquote
&& (spacechar(**xp
) || **xp
== '(' || **xp
== ')')) {
419 *tp
= imap_unquotestr(*tp
);
427 itexecute(struct mailbox
*mp
, struct message
*m
, size_t c
, struct itnode
*n
)
429 struct search_expr se
;
430 char *cp
, *line
= NULL
;
437 fprintf(stderr
, "Internal error: Empty node in SEARCH tree.\n");
442 switch (n
->n_token
) {
446 if (m
->m_time
== 0 && !(m
->m_flag
& MNOFROM
) &&
447 (ibuf
= setinput(mp
, m
, NEED_HEADER
)) != NULL
) {
448 if (readline_restart(ibuf
, &line
, &linesize
, 0) > 0)
449 m
->m_time
= unixtime(line
);
457 if ((cp
= hfield1("date", m
)) != NULL
)
458 m
->m_date
= rfctime(cp
);
464 switch (n
->n_token
) {
466 fprintf(stderr
, "Internal SEARCH error: Lost token %d\n", n
->n_token
);
470 rv
= itexecute(mp
, m
, c
, n
->n_x
) & itexecute(mp
, m
, c
, n
->n_y
);
473 rv
= UICMP(z
, c
, ==, n
->n_n
);
479 rv
= ((m
->m_flag
& MANSWERED
) != 0);
482 rv
= matchenvelope(m
, "bcc", n
->n_v
);
485 rv
= UICMP(z
, m
->m_time
, <, n
->n_n
);
488 se
.ss_sexpr
= n
->n_v
;
489 se
.ss_where
= "body";
490 rv
= message_match(m
, &se
, FAL0
);
493 rv
= matchenvelope(m
, "cc", n
->n_v
);
496 rv
= ((m
->m_flag
& MDELETED
) != 0);
499 rv
= ((m
->m_flag
& MDRAFTED
) != 0);
502 rv
= ((m
->m_flag
& MFLAGGED
) != 0);
505 rv
= matchenvelope(m
, "from", n
->n_v
);
508 rv
= matchfield(m
, n
->n_v
, n
->n_w
);
511 rv
= ((m
->m_flag
& n
->n_n
) != 0);
514 rv
= (m
->m_xsize
> n
->n_n
);
517 rv
= ((m
->m_flag
& (MNEW
| MREAD
)) == MNEW
);
520 rv
= !itexecute(mp
, m
, c
, n
->n_x
);
523 rv
= !(m
->m_flag
& MNEW
);
526 rv
= (UICMP(z
, m
->m_time
, >=, n
->n_n
) &&
527 UICMP(z
, m
->m_time
, <, n
->n_n
+ 86400));
530 rv
= itexecute(mp
, m
, c
, n
->n_x
) | itexecute(mp
, m
, c
, n
->n_y
);
533 rv
= ((m
->m_flag
& MNEW
) != 0);
536 rv
= ((m
->m_flag
& MREAD
) != 0);
539 rv
= UICMP(z
, m
->m_date
, <, n
->n_n
);
542 rv
= (UICMP(z
, m
->m_date
, >=, n
->n_n
) &&
543 UICMP(z
, m
->m_date
, <, n
->n_n
+ 86400));
546 rv
= UICMP(z
, m
->m_date
, >=, n
->n_n
);
549 rv
= UICMP(z
, m
->m_time
, >=, n
->n_n
);
552 rv
= UICMP(z
, m
->m_xsize
, <, n
->n_n
);
555 rv
= matchfield(m
, "subject", n
->n_v
);
558 se
.ss_sexpr
= n
->n_v
;
559 se
.ss_where
= "text";
560 rv
= message_match(m
, &se
, TRU1
);
563 rv
= matchenvelope(m
, "to", n
->n_v
);
566 rv
= !(m
->m_flag
& MANSWERED
);
569 rv
= !(m
->m_flag
& MDELETED
);
572 rv
= !(m
->m_flag
& MDRAFTED
);
575 rv
= !(m
->m_flag
& MFLAGGED
);
578 rv
= !(m
->m_flag
& n
->n_n
);
581 rv
= !(m
->m_flag
& MREAD
);
590 _read_imap_date(char const *cp
)
592 time_t t
= (time_t)-1;
593 int year
, month
, day
, i
, tzdiff
;
600 day
= strtol(cp
, &xp
, 10);
601 if (day
<= 0 || day
> 31 || *xp
++ != '-')
605 if (!ascncasecmp(xp
, month_names
[i
], 3))
607 if (month_names
[++i
][0] == '\0')
613 year
= strtol(&xp
[4], &yp
, 10);
614 if (year
< 1970 || year
> 2037 || yp
!= &xp
[8])
616 if (yp
[0] != '\0' && (yp
[1] != '"' || yp
[2] != '\0'))
618 if ((t
= combinetime(year
, month
, day
, 0, 0, 0)) == (time_t)-1)
620 tzdiff
= t
- mktime(gmtime(&t
));
621 tmptr
= localtime(&t
);
622 if (tmptr
->tm_isdst
> 0)
631 matchfield(struct message
*m
, char const *field
, const void *what
)
637 if ((in
.s
= hfieldX(field
, m
)) == NULL
)
641 mime_fromhdr(&in
, &out
, TD_ICONV
);
642 rv
= substr(out
.s
, what
);
650 matchenvelope(struct message
*m
, char const *field
, const void *what
)
657 if ((cp
= hfieldX(field
, m
)) == NULL
)
660 for (np
= lextract(cp
, GFULL
); np
!= NULL
; np
= np
->n_flink
) {
661 if (!substr(np
->n_name
, what
) && !substr(mkenvelope(np
), what
))
673 mkenvelope(struct name
*np
)
676 char *ep
, *realnam
= NULL
, *sourceaddr
= NULL
, *localpart
= NULL
,
677 *domainpart
= NULL
, *cp
, *rp
, *xp
, *ip
;
679 int level
= 0, hadphrase
= 0;
682 in
.s
= np
->n_fullname
;
684 mime_fromhdr(&in
, &out
, TD_ICONV
);
685 rp
= ip
= ac_alloc(strlen(out
.s
) + 1);
686 for (cp
= out
.s
; *cp
; cp
++) {
692 if (*cp
== '\\' && cp
[1])
698 while (cp
> out
.s
&& blankchar(cp
[-1]))
702 if (xp
< &cp
[-1] && *xp
== '"' && cp
[-1] == '"') {
713 if (hadphrase
++ == 0)
734 localpart
= savestr(np
->n_name
);
735 if ((cp
= strrchr(localpart
, '@')) != NULL
) {
740 ep
= salloc(epsize
= strlen(np
->n_fullname
) * 2 + 40);
741 snprintf(ep
, epsize
, "(%s %s %s %s)",
742 realnam
? imap_quotestr(realnam
) : "NIL",
743 sourceaddr
? imap_quotestr(sourceaddr
) : "NIL",
744 localpart
? imap_quotestr(localpart
) : "NIL",
745 domainpart
? imap_quotestr(domainpart
) : "NIL");
751 #define SURROUNDING 16
753 around(char const *cp
)
755 static char ab
[2 * SURROUNDING
+1];
760 for (i
= 0; i
< SURROUNDING
&& cp
> _it_begin
; ++i
)
762 for (i
= 0; i
< sizeof(ab
) - 1; ++i
)
770 imap_search(char const *spec
, int f
)
772 static char *lastspec
;
779 if (strcmp(spec
, "()")) {
780 if (lastspec
!= NULL
)
783 lastspec
= sbufdup(spec
, i
);
784 } else if (lastspec
== NULL
) {
785 fprintf(stderr
, tr(524, "No last SEARCH criteria available.\n"));
789 _it_begin
= lastspec
;
791 _it_need_headers
= FAL0
;
793 if ((rv
= imap_search1(spec
, f
) == OKAY
))
797 if (itparse(spec
, &xp
, 0) == STOP
)
799 if (_it_tree
== NULL
) {
805 if (mb
.mb_type
== MB_IMAP
&& _it_need_headers
)
806 imap_getheaders(1, msgCount
);
809 for (i
= 0; UICMP(z
, i
, <, msgCount
); ++i
) {
810 if (message
[i
].m_flag
& MHIDDEN
)
812 if (f
== MDELETED
|| !(message
[i
].m_flag
& MDELETED
)) {
813 size_t j
= (int)(i
+ 1);
814 if (itexecute(&mb
, &message
[i
], j
, _it_tree
))
826 #endif /* HAVE_IMAP_SEARCH */
828 /* vim:set fenc=utf-8:s-it-mode */