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 bool_t
_matchmsg(struct message
*m
, const void *what
,
142 static char const * around(char const *cp
);
145 itparse(char const *spec
, char const **xp
, int sub
)
148 struct itnode n
, *z
, *ittree
;
153 while ((rv
= itscan(spec
, xp
)) == OKAY
&& _it_token
!= ITBAD
&&
154 _it_token
!= ITEOD
) {
156 memset(&n
, 0, sizeof n
);
170 fprintf(stderr
, "Excess in \")\".\n");
178 if ((rv
= itparse(spec
, xp
, sub
+ 1)) == STOP
)
181 if ((n
.n_x
= _it_tree
) == NULL
) {
182 fprintf(stderr
, "Criterion for NOT missing: >>> %s <<<\n",
190 /* <search-key1> <search-key2> */
192 if ((rv
= itparse(spec
, xp
, sub
+ 1)) == STOP
)
194 if ((n
.n_x
= _it_tree
) == NULL
) {
195 fprintf(stderr
, "First criterion for OR missing: >>> %s <<<\n",
201 if ((rv
= itparse(spec
, xp
, sub
+ 1)) == STOP
)
204 if ((n
.n_y
= _it_tree
) == NULL
) {
205 fprintf(stderr
, "Second criterion for OR missing: >>> %s <<<\n",
212 n
.n_token
= _it_token
;
219 if (_it_tree
== NULL
) {
220 _it_tree
= salloc(sizeof *_it_tree
);
224 _it_tree
= salloc(sizeof *_it_tree
);
225 _it_tree
->n_token
= ITAND
;
227 _it_tree
->n_y
= salloc(sizeof *_it_tree
->n_y
);
230 if (sub
&& level
== 0)
239 itscan(char const *spec
, char const **xp
)
245 while (spacechar(*spec
))
257 while (spacechar(*spec
))
264 for (i
= 0; _it_strings
[i
].s_string
; i
++) {
265 n
= strlen(_it_strings
[i
].s_string
);
266 if (ascncasecmp(spec
, _it_strings
[i
].s_string
, n
) == 0 &&
267 (spacechar(spec
[n
]) || spec
[n
] == '\0' ||
268 spec
[n
] == '(' || spec
[n
] == ')')) {
269 _it_token
= _it_strings
[i
].s_token
;
271 while (spacechar(*spec
))
273 rv
= itsplit(spec
, xp
);
277 if (digitchar(*spec
)) {
278 _it_number
= strtoul(spec
, UNCONST(xp
), 10);
279 if (spacechar(**xp
) || **xp
== '\0' || **xp
== '(' || **xp
== ')') {
284 fprintf(stderr
, "Bad SEARCH criterion \"");
285 while (*spec
&& !spacechar(*spec
) && *spec
!= '(' && *spec
!= ')') {
286 putc(*spec
& 0377, stderr
);
289 fprintf(stderr
, "\": >>> %s <<<\n", around(*xp
));
298 itsplit(char const *spec
, char const **xp
)
315 rv
= itstring(&_it_args
[0], spec
, xp
);
326 if ((rv
= itstring(&_it_args
[0], spec
, xp
)) != OKAY
)
328 if ((t
= _read_imap_date(_it_args
[0])) == (time_t)-1) {
329 fprintf(stderr
, "Invalid date \"%s\": >>> %s <<<\n",
330 (char*)_it_args
[0], around(*xp
));
338 /* <field-name> <string> */
340 if ((rv
= itstring(&_it_args
[0], spec
, xp
)) != OKAY
)
343 if ((rv
= itstring(&_it_args
[1], spec
, xp
)) != OKAY
)
349 if ((rv
= itstring(&_it_args
[0], spec
, xp
)) != OKAY
)
351 if (asccasecmp(_it_args
[0], "\\Seen") == 0)
353 else if (asccasecmp(_it_args
[0], "\\Deleted") == 0)
354 _it_number
= MDELETED
;
355 else if (asccasecmp(_it_args
[0], "\\Recent") == 0)
357 else if (asccasecmp(_it_args
[0], "\\Flagged") == 0)
358 _it_number
= MFLAGGED
;
359 else if (asccasecmp(_it_args
[0], "\\Answered") == 0)
360 _it_number
= MANSWERED
;
361 else if (asccasecmp(_it_args
[0], "\\Draft") == 0)
369 if ((rv
= itstring(&_it_args
[0], spec
, xp
)) != OKAY
)
371 _it_number
= strtoul(_it_args
[0], &cp
, 10);
372 if (spacechar(*cp
) || *cp
== '\0')
374 fprintf(stderr
, "Invalid size: >>> %s <<<\n", around(*xp
));
380 "Searching for UIDs is not supported: >>> %s <<<\n", around(*xp
));
393 itstring(void **tp
, char const *spec
, char const **xp
)
400 while (spacechar(*spec
))
402 if (*spec
== '\0' || *spec
== '(' || *spec
== ')') {
403 fprintf(stderr
, "Missing string argument: >>> %s <<<\n",
404 around(&(*xp
)[spec
- *xp
]));
407 ap
= *tp
= salloc(strlen(spec
) + 1);
410 if (inquote
&& **xp
== '\\')
412 else if (**xp
== '"')
414 else if (!inquote
&& (spacechar(**xp
) || **xp
== '(' || **xp
== ')')) {
421 *tp
= imap_unquotestr(*tp
);
429 itexecute(struct mailbox
*mp
, struct message
*m
, size_t c
, struct itnode
*n
)
431 char *cp
, *line
= NULL
;
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 rv
= _matchmsg(m
, n
->n_v
, 0);
492 rv
= matchenvelope(m
, "cc", n
->n_v
);
495 rv
= ((m
->m_flag
& MDELETED
) != 0);
498 rv
= ((m
->m_flag
& MDRAFTED
) != 0);
501 rv
= ((m
->m_flag
& MFLAGGED
) != 0);
504 rv
= matchenvelope(m
, "from", n
->n_v
);
507 rv
= matchfield(m
, n
->n_v
, n
->n_w
);
510 rv
= ((m
->m_flag
& n
->n_n
) != 0);
513 rv
= (m
->m_xsize
> n
->n_n
);
516 rv
= ((m
->m_flag
& (MNEW
| MREAD
)) == MNEW
);
519 rv
= !itexecute(mp
, m
, c
, n
->n_x
);
522 rv
= !(m
->m_flag
& MNEW
);
525 rv
= (UICMP(z
, m
->m_time
, >=, n
->n_n
) &&
526 UICMP(z
, m
->m_time
, <, n
->n_n
+ 86400));
529 rv
= itexecute(mp
, m
, c
, n
->n_x
) | itexecute(mp
, m
, c
, n
->n_y
);
532 rv
= ((m
->m_flag
& MNEW
) != 0);
535 rv
= ((m
->m_flag
& MREAD
) != 0);
538 rv
= UICMP(z
, m
->m_date
, <, n
->n_n
);
541 rv
= (UICMP(z
, m
->m_date
, >=, n
->n_n
) &&
542 UICMP(z
, m
->m_date
, <, n
->n_n
+ 86400));
545 rv
= UICMP(z
, m
->m_date
, >=, n
->n_n
);
548 rv
= UICMP(z
, m
->m_time
, >=, n
->n_n
);
551 rv
= UICMP(z
, m
->m_xsize
, <, n
->n_n
);
554 rv
= matchfield(m
, "subject", n
->n_v
);
557 rv
= _matchmsg(m
, n
->n_v
, 1);
560 rv
= matchenvelope(m
, "to", n
->n_v
);
563 rv
= !(m
->m_flag
& MANSWERED
);
566 rv
= !(m
->m_flag
& MDELETED
);
569 rv
= !(m
->m_flag
& MDRAFTED
);
572 rv
= !(m
->m_flag
& MFLAGGED
);
575 rv
= !(m
->m_flag
& n
->n_n
);
578 rv
= !(m
->m_flag
& MREAD
);
587 _read_imap_date(char const *cp
)
589 time_t t
= (time_t)-1;
590 int year
, month
, day
, i
, tzdiff
;
597 day
= strtol(cp
, &xp
, 10);
598 if (day
<= 0 || day
> 31 || *xp
++ != '-')
602 if (ascncasecmp(xp
, month_names
[i
], 3) == 0)
604 if (month_names
[++i
][0] == '\0')
610 year
= strtol(&xp
[4], &yp
, 10);
611 if (year
< 1970 || year
> 2037 || yp
!= &xp
[8])
613 if (yp
[0] != '\0' && (yp
[1] != '"' || yp
[2] != '\0'))
615 if ((t
= combinetime(year
, month
, day
, 0, 0, 0)) == (time_t)-1)
617 tzdiff
= t
- mktime(gmtime(&t
));
618 tmptr
= localtime(&t
);
619 if (tmptr
->tm_isdst
> 0)
628 matchfield(struct message
*m
, char const *field
, const void *what
)
634 if ((in
.s
= hfieldX(field
, m
)) == NULL
)
638 mime_fromhdr(&in
, &out
, TD_ICONV
);
639 rv
= substr(out
.s
, what
);
647 matchenvelope(struct message
*m
, char const *field
, const void *what
)
654 if ((cp
= hfieldX(field
, m
)) == NULL
)
657 for (np
= lextract(cp
, GFULL
); np
!= NULL
; np
= np
->n_flink
) {
658 if (!substr(np
->n_name
, what
) && !substr(mkenvelope(np
), what
))
670 mkenvelope(struct name
*np
)
673 char *ep
, *realnam
= NULL
, *sourceaddr
= NULL
, *localpart
= NULL
,
674 *domainpart
= NULL
, *cp
, *rp
, *xp
, *ip
;
676 int level
= 0, hadphrase
= 0;
679 in
.s
= np
->n_fullname
;
681 mime_fromhdr(&in
, &out
, TD_ICONV
);
682 rp
= ip
= ac_alloc(strlen(out
.s
) + 1);
683 for (cp
= out
.s
; *cp
; cp
++) {
689 if (*cp
== '\\' && cp
[1])
695 while (cp
> out
.s
&& blankchar(cp
[-1]))
699 if (xp
< &cp
[-1] && *xp
== '"' && cp
[-1] == '"') {
710 if (hadphrase
++ == 0)
731 localpart
= savestr(np
->n_name
);
732 if ((cp
= strrchr(localpart
, '@')) != NULL
) {
737 ep
= salloc(epsize
= strlen(np
->n_fullname
) * 2 + 40);
738 snprintf(ep
, epsize
, "(%s %s %s %s)",
739 realnam
? imap_quotestr(realnam
) : "NIL",
740 sourceaddr
? imap_quotestr(sourceaddr
) : "NIL",
741 localpart
? imap_quotestr(localpart
) : "NIL",
742 domainpart
? imap_quotestr(domainpart
) : "NIL");
749 _matchmsg(struct message
*m
, const void *what
, int withheader
)
752 size_t linesize
= 0, cnt
;
757 if ((fp
= Ftmp(NULL
, "imasrch", OF_RDWR
| OF_UNLINK
| OF_REGISTER
, 0600)) ==
760 if (sendmp(m
, fp
, NULL
, NULL
, SEND_TOSRCH
, NULL
) < 0)
767 while (fgetline(&line
, &linesize
, &cnt
, NULL
, fp
, 0))
771 while (fgetline(&line
, &linesize
, &cnt
, NULL
, fp
, 0)) {
772 if (!substr(line
, what
))
787 #define SURROUNDING 16
789 around(char const *cp
)
791 static char ab
[2 * SURROUNDING
+1];
796 for (i
= 0; i
< SURROUNDING
&& cp
> _it_begin
; ++i
)
798 for (i
= 0; i
< sizeof(ab
) - 1; ++i
)
806 imap_search(char const *spec
, int f
)
808 static char *lastspec
;
815 if (strcmp(spec
, "()")) {
816 if (lastspec
!= NULL
)
819 lastspec
= sbufdup(spec
, i
);
820 } else if (lastspec
== NULL
) {
821 fprintf(stderr
, tr(524, "No last SEARCH criteria available.\n"));
825 _it_begin
= lastspec
;
827 _it_need_headers
= FAL0
;
829 if ((rv
= imap_search1(spec
, f
) == OKAY
))
833 if (itparse(spec
, &xp
, 0) == STOP
)
835 if (_it_tree
== NULL
) {
841 if (mb
.mb_type
== MB_IMAP
&& _it_need_headers
)
842 imap_getheaders(1, msgCount
);
845 for (i
= 0; UICMP(z
, i
, <, msgCount
); ++i
) {
846 if (message
[i
].m_flag
& MHIDDEN
)
848 if (f
== MDELETED
|| !(message
[i
].m_flag
& MDELETED
)) {
849 size_t j
= (int)(i
+ 1);
850 if (itexecute(&mb
, &message
[i
], j
, _it_tree
))
862 #endif /* HAVE_IMAP_SEARCH */
864 /* vim:set fenc=utf-8:s-it-mode */