colour_table_create(): auto-enable $TERMs named "color" (Gavin Troy)
[s-mailx.git] / imap_search.c
blobf9b41dcc7c4fd06a3b05f76d6466996bb7f2c784
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>.
8 */
9 /*
10 * Copyright (c) 2004
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
15 * are met:
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
39 * SUCH DAMAGE.
42 #ifndef HAVE_AMALGAMATION
43 # include "nail.h"
44 #endif
46 EMPTY_FILE(imap_search)
47 #ifdef HAVE_IMAP_SEARCH
49 enum itoken {
50 ITBAD, ITEOD, ITBOL, ITEOL, ITAND, ITSET, ITALL, ITANSWERED,
51 ITBCC, ITBEFORE, ITBODY,
52 ITCC,
53 ITDELETED, ITDRAFT,
54 ITFLAGGED, ITFROM,
55 ITHEADER,
56 ITKEYWORD,
57 ITLARGER,
58 ITNEW, ITNOT,
59 ITOLD, ITON, ITOR,
60 ITRECENT,
61 ITSEEN, ITSENTBEFORE, ITSENTON, ITSENTSINCE, ITSINCE, ITSMALLER,
62 ITSUBJECT,
63 ITTEXT, ITTO,
64 ITUID, ITUNANSWERED, ITUNDELETED, ITUNDRAFT, ITUNFLAGGED, ITUNKEYWORD,
65 ITUNSEEN
68 struct itlex {
69 char const *s_string;
70 enum itoken s_token;
73 struct itnode {
74 enum itoken n_token;
75 unsigned long n_n;
76 void *n_v;
77 void *n_w;
78 struct itnode *n_x;
79 struct itnode *n_y;
82 static struct itlex const _it_strings[] = {
83 { "ALL", ITALL },
84 { "ANSWERED", ITANSWERED },
85 { "BCC", ITBCC },
86 { "BEFORE", ITBEFORE },
87 { "BODY", ITBODY },
88 { "CC", ITCC },
89 { "DELETED", ITDELETED },
90 { "DRAFT", ITDRAFT },
91 { "FLAGGED", ITFLAGGED },
92 { "FROM", ITFROM },
93 { "HEADER", ITHEADER },
94 { "KEYWORD", ITKEYWORD },
95 { "LARGER", ITLARGER },
96 { "NEW", ITNEW },
97 { "NOT", ITNOT },
98 { "OLD", ITOLD },
99 { "ON", ITON },
100 { "OR", ITOR },
101 { "RECENT", ITRECENT },
102 { "SEEN", ITSEEN },
103 { "SENTBEFORE", ITSENTBEFORE },
104 { "SENTON", ITSENTON },
105 { "SENTSINCE", ITSENTSINCE },
106 { "SINCE", ITSINCE },
107 { "SMALLER", ITSMALLER },
108 { "SUBJECT", ITSUBJECT },
109 { "TEXT", ITTEXT },
110 { "TO", ITTO },
111 { "UID", ITUID },
112 { "UNANSWERED", ITUNANSWERED },
113 { "UNDELETED", ITUNDELETED },
114 { "UNDRAFT", ITUNDRAFT },
115 { "UNFLAGGED", ITUNFLAGGED },
116 { "UNKEYWORD", ITUNKEYWORD },
117 { "UNSEEN", ITUNSEEN },
118 { NULL, ITBAD }
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,
136 void const *what);
137 static int matchenvelope(struct message *m, char const *field,
138 void const *what);
139 static char * mkenvelope(struct name *np);
140 static char const * around(char const *cp);
142 static enum okay
143 itparse(char const *spec, char const **xp, int sub)
145 int level = 0;
146 struct itnode n, *z, *ittree;
147 enum okay rv;
148 NYD_ENTER;
150 _it_tree = NULL;
151 while ((rv = itscan(spec, xp)) == OKAY && _it_token != ITBAD &&
152 _it_token != ITEOD) {
153 ittree = _it_tree;
154 memset(&n, 0, sizeof n);
155 spec = *xp;
156 switch (_it_token) {
157 case ITBOL:
158 ++level;
159 continue;
160 case ITEOL:
161 if (--level == 0)
162 goto jleave;
163 if (level < 0) {
164 if (sub > 0) {
165 --(*xp);
166 goto jleave;
168 fprintf(stderr, "Excess in \")\".\n");
169 rv = STOP;
170 goto jleave;
172 continue;
173 case ITNOT:
174 /* <search-key> */
175 n.n_token = ITNOT;
176 if ((rv = itparse(spec, xp, sub + 1)) == STOP)
177 goto jleave;
178 spec = *xp;
179 if ((n.n_x = _it_tree) == NULL) {
180 fprintf(stderr, "Criterion for NOT missing: >>> %s <<<\n",
181 around(*xp));
182 rv = STOP;
183 goto jleave;
185 _it_token = ITNOT;
186 break;
187 case ITOR:
188 /* <search-key1> <search-key2> */
189 n.n_token = ITOR;
190 if ((rv = itparse(spec, xp, sub + 1)) == STOP)
191 goto jleave;
192 if ((n.n_x = _it_tree) == NULL) {
193 fprintf(stderr, "First criterion for OR missing: >>> %s <<<\n",
194 around(*xp));
195 rv = STOP;
196 goto jleave;
198 spec = *xp;
199 if ((rv = itparse(spec, xp, sub + 1)) == STOP)
200 goto jleave;
201 spec = *xp;
202 if ((n.n_y = _it_tree) == NULL) {
203 fprintf(stderr, "Second criterion for OR missing: >>> %s <<<\n",
204 around(*xp));
205 rv = STOP;
206 goto jleave;
208 break;
209 default:
210 n.n_token = _it_token;
211 n.n_n = _it_number;
212 n.n_v = _it_args[0];
213 n.n_w = _it_args[1];
216 _it_tree = ittree;
217 if (_it_tree == NULL) {
218 _it_tree = salloc(sizeof *_it_tree);
219 *_it_tree = n;
220 } else {
221 z = _it_tree;
222 _it_tree = salloc(sizeof *_it_tree);
223 _it_tree->n_token = ITAND;
224 _it_tree->n_x = z;
225 _it_tree->n_y = salloc(sizeof *_it_tree->n_y);
226 *_it_tree->n_y = n;
228 if (sub && level == 0)
229 break;
231 jleave:
232 NYD_LEAVE;
233 return rv;
236 static enum okay
237 itscan(char const *spec, char const **xp)
239 int i, n;
240 enum okay rv = OKAY;
241 NYD_ENTER;
243 while (spacechar(*spec))
244 ++spec;
245 if (*spec == '(') {
246 *xp = &spec[1];
247 _it_token = ITBOL;
248 goto jleave;
250 if (*spec == ')') {
251 *xp = &spec[1];
252 _it_token = ITEOL;
253 goto jleave;
255 while (spacechar(*spec))
256 ++spec;
257 if (*spec == '\0') {
258 _it_token = ITEOD;
259 goto jleave;
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;
267 spec += n;
268 while (spacechar(*spec))
269 ++spec;
270 rv = itsplit(spec, xp);
271 goto jleave;
274 if (digitchar(*spec)) {
275 _it_number = strtoul(spec, UNCONST(xp), 10);
276 if (!__GO(**xp)) {
277 _it_token = ITSET;
278 goto jleave;
281 fprintf(stderr, "Bad SEARCH criterion \"");
282 while (__GO(*spec)) {
283 putc(*spec & 0377, stderr);
284 ++spec;
286 #undef __GO
288 fprintf(stderr, "\": >>> %s <<<\n", around(*xp));
289 _it_token = ITBAD;
290 rv = STOP;
291 jleave:
292 NYD_LEAVE;
293 return rv;
296 static enum okay
297 itsplit(char const *spec, char const **xp)
299 char *cp;
300 time_t t;
301 enum okay rv;
302 NYD_ENTER;
304 switch (_it_token) {
305 case ITBCC:
306 case ITBODY:
307 case ITCC:
308 case ITFROM:
309 case ITSUBJECT:
310 case ITTEXT:
311 case ITTO:
312 /* <string> */
313 ++_it_need_headers;
314 rv = itstring(_it_args, spec, xp);
315 break;
316 case ITSENTBEFORE:
317 case ITSENTON:
318 case ITSENTSINCE:
319 ++_it_need_headers;
320 /*FALLTHRU*/
321 case ITBEFORE:
322 case ITON:
323 case ITSINCE:
324 /* <date> */
325 if ((rv = itstring(_it_args, spec, xp)) != OKAY)
326 break;
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));
330 rv = STOP;
331 break;
333 _it_number = t;
334 rv = OKAY;
335 break;
336 case ITHEADER:
337 /* <field-name> <string> */
338 ++_it_need_headers;
339 if ((rv = itstring(_it_args, spec, xp)) != OKAY)
340 break;
341 spec = *xp;
342 if ((rv = itstring(_it_args + 1, spec, xp)) != OKAY)
343 break;
344 break;
345 case ITKEYWORD:
346 case ITUNKEYWORD:
347 /* <flag> */ /* TODO use table->flag map search instead */
348 if ((rv = itstring(_it_args, spec, xp)) != OKAY)
349 break;
350 if (!asccasecmp(_it_args[0], "\\Seen"))
351 _it_number = MREAD;
352 else if (!asccasecmp(_it_args[0], "\\Deleted"))
353 _it_number = MDELETED;
354 else if (!asccasecmp(_it_args[0], "\\Recent"))
355 _it_number = MNEW;
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"))
361 _it_number = MDRAFT;
362 else
363 _it_number = 0;
364 break;
365 case ITLARGER:
366 case ITSMALLER:
367 /* <n> */
368 if ((rv = itstring(_it_args, spec, xp)) != OKAY)
369 break;
370 _it_number = strtoul(_it_args[0], &cp, 10);
371 if (spacechar(*cp) || *cp == '\0')
372 break;
373 fprintf(stderr, "Invalid size: >>> %s <<<\n", around(*xp));
374 rv = STOP;
375 break;
376 case ITUID:
377 /* <message set> */
378 fprintf(stderr,
379 "Searching for UIDs is not supported: >>> %s <<<\n", around(*xp));
380 rv = STOP;
381 break;
382 default:
383 *xp = spec;
384 rv = OKAY;
385 break;
387 NYD_LEAVE;
388 return rv;
391 static enum okay
392 itstring(void **tp, char const *spec, char const **xp) /* XXX lesser derefs */
394 int inquote = 0;
395 char *ap;
396 enum okay rv = STOP;
397 NYD_ENTER;
399 while (spacechar(*spec))
400 ++spec;
401 if (*spec == '\0' || *spec == '(' || *spec == ')') {
402 fprintf(stderr, "Missing string argument: >>> %s <<<\n",
403 around(&(*xp)[spec - *xp]));
404 goto jleave;
406 ap = *tp = salloc(strlen(spec) +1);
407 *xp = spec;
408 do {
409 if (inquote && **xp == '\\')
410 *ap++ = *(*xp)++;
411 else if (**xp == '"')
412 inquote = !inquote;
413 else if (!inquote && (spacechar(**xp) || **xp == '(' || **xp == ')')) {
414 *ap++ = '\0';
415 break;
417 *ap++ = **xp;
418 } while (*(*xp)++);
420 *tp = imap_unquotestr(*tp);
421 rv = OKAY;
422 jleave:
423 NYD_LEAVE;
424 return rv;
427 static int
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 */
432 size_t linesize = 0;
433 FILE *ibuf;
434 int rv;
435 NYD_ENTER;
437 if (n == NULL) {
438 fprintf(stderr, "Internal error: Empty node in SEARCH tree.\n");
439 rv = 0;
440 goto jleave;
443 switch (n->n_token) {
444 case ITBEFORE:
445 case ITON:
446 case ITSINCE:
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);
451 free(line);
453 break;
454 case ITSENTBEFORE:
455 case ITSENTON:
456 case ITSENTSINCE:
457 if (m->m_date == 0)
458 if ((cp = hfield1("date", m)) != NULL)
459 m->m_date = rfctime(cp);
460 break;
461 default:
462 break;
465 switch (n->n_token) {
466 default:
467 fprintf(stderr, "Internal SEARCH error: Lost token %d\n", n->n_token);
468 rv = 0;
469 break;
470 case ITAND:
471 rv = itexecute(mp, m, c, n->n_x) & itexecute(mp, m, c, n->n_y);
472 break;
473 case ITSET:
474 rv = UICMP(z, c, ==, n->n_n);
475 break;
476 case ITALL:
477 rv = 1;
478 break;
479 case ITANSWERED:
480 rv = ((m->m_flag & MANSWERED) != 0);
481 break;
482 case ITBCC:
483 rv = matchenvelope(m, "bcc", n->n_v);
484 break;
485 case ITBEFORE:
486 rv = UICMP(z, m->m_time, <, n->n_n);
487 break;
488 case ITBODY:
489 se.ss_sexpr = n->n_v;
490 se.ss_where = "body";
491 rv = message_match(m, &se, FAL0);
492 break;
493 case ITCC:
494 rv = matchenvelope(m, "cc", n->n_v);
495 break;
496 case ITDELETED:
497 rv = ((m->m_flag & MDELETED) != 0);
498 break;
499 case ITDRAFT:
500 rv = ((m->m_flag & MDRAFTED) != 0);
501 break;
502 case ITFLAGGED:
503 rv = ((m->m_flag & MFLAGGED) != 0);
504 break;
505 case ITFROM:
506 rv = matchenvelope(m, "from", n->n_v);
507 break;
508 case ITHEADER:
509 rv = matchfield(m, n->n_v, n->n_w);
510 break;
511 case ITKEYWORD:
512 rv = ((m->m_flag & n->n_n) != 0);
513 break;
514 case ITLARGER:
515 rv = (m->m_xsize > n->n_n);
516 break;
517 case ITNEW:
518 rv = ((m->m_flag & (MNEW | MREAD)) == MNEW);
519 break;
520 case ITNOT:
521 rv = !itexecute(mp, m, c, n->n_x);
522 break;
523 case ITOLD:
524 rv = !(m->m_flag & MNEW);
525 break;
526 case ITON:
527 rv = (UICMP(z, m->m_time, >=, n->n_n) &&
528 UICMP(z, m->m_time, <, n->n_n + 86400));
529 break;
530 case ITOR:
531 rv = itexecute(mp, m, c, n->n_x) | itexecute(mp, m, c, n->n_y);
532 break;
533 case ITRECENT:
534 rv = ((m->m_flag & MNEW) != 0);
535 break;
536 case ITSEEN:
537 rv = ((m->m_flag & MREAD) != 0);
538 break;
539 case ITSENTBEFORE:
540 rv = UICMP(z, m->m_date, <, n->n_n);
541 break;
542 case ITSENTON:
543 rv = (UICMP(z, m->m_date, >=, n->n_n) &&
544 UICMP(z, m->m_date, <, n->n_n + 86400));
545 break;
546 case ITSENTSINCE:
547 rv = UICMP(z, m->m_date, >=, n->n_n);
548 break;
549 case ITSINCE:
550 rv = UICMP(z, m->m_time, >=, n->n_n);
551 break;
552 case ITSMALLER:
553 rv = UICMP(z, m->m_xsize, <, n->n_n);
554 break;
555 case ITSUBJECT:
556 rv = matchfield(m, "subject", n->n_v);
557 break;
558 case ITTEXT:
559 se.ss_sexpr = n->n_v;
560 se.ss_where = "text";
561 rv = message_match(m, &se, TRU1);
562 break;
563 case ITTO:
564 rv = matchenvelope(m, "to", n->n_v);
565 break;
566 case ITUNANSWERED:
567 rv = !(m->m_flag & MANSWERED);
568 break;
569 case ITUNDELETED:
570 rv = !(m->m_flag & MDELETED);
571 break;
572 case ITUNDRAFT:
573 rv = !(m->m_flag & MDRAFTED);
574 break;
575 case ITUNFLAGGED:
576 rv = !(m->m_flag & MFLAGGED);
577 break;
578 case ITUNKEYWORD:
579 rv = !(m->m_flag & n->n_n);
580 break;
581 case ITUNSEEN:
582 rv = !(m->m_flag & MREAD);
583 break;
585 jleave:
586 NYD_LEAVE;
587 return rv;
590 static time_t
591 _read_imap_date(char const *cp)
593 time_t t = (time_t)-1;
594 int year, month, day, i, tzdiff;
595 struct tm *tmptr;
596 char *xp, *yp;
597 NYD_ENTER;
599 if (*cp == '"')
600 ++cp;
601 day = strtol(cp, &xp, 10);
602 if (day <= 0 || day > 31 || *xp++ != '-')
603 goto jleave;
605 for (i = 0;;) {
606 if (!ascncasecmp(xp, month_names[i], 3))
607 break;
608 if (month_names[++i][0] == '\0')
609 goto jleave;
611 month = i + 1;
612 if (xp[3] != '-')
613 goto jleave;
614 year = strtol(xp + 4, &yp, 10);
615 if (year < 1970 || year > 2037 || PTRCMP(yp, !=, xp + 8))
616 goto jleave;
617 if (yp[0] != '\0' && (yp[1] != '"' || yp[2] != '\0'))
618 goto jleave;
619 if ((t = combinetime(year, month, day, 0, 0, 0)) == (time_t)-1)
620 goto jleave;
621 tzdiff = t - mktime(gmtime(&t));
622 tmptr = localtime(&t);
623 if (tmptr->tm_isdst > 0)
624 tzdiff += 3600;
625 t -= tzdiff;
626 jleave:
627 NYD_LEAVE;
628 return t;
631 static bool_t
632 matchfield(struct message *m, char const *field, void const *what)
634 struct str in, out;
635 bool_t rv = FAL0;
636 NYD_ENTER;
638 if ((in.s = hfieldX(field, m)) == NULL)
639 goto jleave;
641 in.l = strlen(in.s);
642 mime_fromhdr(&in, &out, TD_ICONV);
643 rv = substr(out.s, what);
644 free(out.s);
645 jleave:
646 NYD_LEAVE;
647 return rv;
650 static int
651 matchenvelope(struct message *m, char const *field, void const *what)
653 struct name *np;
654 char *cp;
655 int rv = 0;
656 NYD_ENTER;
658 if ((cp = hfieldX(field, m)) == NULL)
659 goto jleave;
661 for (np = lextract(cp, GFULL); np != NULL; np = np->n_flink) {
662 if (!substr(np->n_name, what) && !substr(mkenvelope(np), what))
663 continue;
664 rv = 1;
665 break;
668 jleave:
669 NYD_LEAVE;
670 return rv;
673 static char *
674 mkenvelope(struct name *np)
676 size_t epsize;
677 char *ep, *realnam = NULL, *sourceaddr = NULL, *localpart = NULL,
678 *domainpart = NULL, *cp, *rp, *xp, *ip;
679 struct str in, out;
680 int level = 0;
681 bool_t hadphrase = FAL0;
682 NYD_ENTER;
684 in.s = np->n_fullname;
685 in.l = strlen(in.s);
686 mime_fromhdr(&in, &out, TD_ICONV);
687 rp = ip = ac_alloc(strlen(out.s) + 1);
688 for (cp = out.s; *cp; cp++) {
689 switch (*cp) {
690 case '"':
691 while (*cp) {
692 if (*++cp == '"')
693 break;
694 if (cp[0] == '\\' && cp[1] != '\0')
695 ++cp;
696 *rp++ = *cp;
698 break;
699 case '<':
700 while (cp > out.s && blankchar(cp[-1]))
701 --cp;
702 rp = ip;
703 xp = out.s;
704 if (PTRCMP(xp, <, cp - 1) && *xp == '"' && cp[-1] == '"') {
705 ++xp;
706 --cp;
708 while (xp < cp)
709 *rp++ = *xp++;
710 hadphrase = TRU1;
711 goto jdone;
712 case '(':
713 if (level++)
714 goto jdfl;
715 if (!hadphrase)
716 rp = ip;
717 hadphrase = TRU1;
718 break;
719 case ')':
720 if (--level)
721 goto jdfl;
722 break;
723 case '\\':
724 if (level && cp[1] != '\0')
725 cp++;
726 goto jdfl;
727 default:
728 jdfl:
729 *rp++ = *cp;
732 jdone:
733 *rp = '\0';
734 if (hadphrase)
735 realnam = ip;
736 free(out.s);
737 localpart = savestr(np->n_name);
738 if ((cp = strrchr(localpart, '@')) != NULL) {
739 *cp = '\0';
740 domainpart = cp + 1;
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");
749 ac_free(ip);
750 NYD_LEAVE;
751 return ep;
754 #define SURROUNDING 16
755 static char const *
756 around(char const *cp)
758 static char ab[2 * SURROUNDING +1];
760 size_t i;
761 NYD_ENTER;
763 for (i = 0; i < SURROUNDING && cp > _it_begin; ++i)
764 --cp;
765 for (i = 0; i < sizeof(ab) -1; ++i)
766 ab[i] = *cp++;
767 ab[i] = '\0';
768 NYD_LEAVE;
769 return ab;
772 FL enum okay
773 imap_search(char const *spec, int f)
775 static char *lastspec;
777 char const *xp;
778 size_t i;
779 enum okay rv = STOP;
780 NYD_ENTER;
782 if (strcmp(spec, "()")) {
783 if (lastspec != NULL)
784 free(lastspec);
785 i = strlen(spec);
786 lastspec = sbufdup(spec, i);
787 } else if (lastspec == NULL) {
788 fprintf(stderr, tr(524, "No last SEARCH criteria available.\n"));
789 goto jleave;
791 spec =
792 _it_begin = lastspec;
794 _it_need_headers = FAL0;
795 #ifdef HAVE_IMAP
796 if ((rv = imap_search1(spec, f) == OKAY))
797 goto jleave;
798 #endif
800 if (itparse(spec, &xp, 0) == STOP)
801 goto jleave;
802 if (_it_tree == NULL) {
803 rv = OKAY;
804 goto jleave;
807 #ifdef HAVE_IMAP
808 if (mb.mb_type == MB_IMAP && _it_need_headers)
809 imap_getheaders(1, msgCount);
810 #endif
811 srelax_hold();
812 for (i = 0; UICMP(z, i, <, msgCount); ++i) {
813 if (message[i].m_flag & MHIDDEN)
814 continue;
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))
818 mark((int)j, f);
819 srelax();
822 srelax_rele();
824 rv = OKAY;
825 jleave:
826 NYD_LEAVE;
827 return rv;
829 #endif /* HAVE_IMAP_SEARCH */
831 /* vim:set fenc=utf-8:s-it-mode */