quit()+descendants: add hold_sigs_on argument, let'em handle it
[s-mailx.git] / imap_search.c
blobcfa3b336b111a120590d8578eea9a99f8b448962
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 - 2016 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
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.
41 #undef n_FILE
42 #define n_FILE imap_search
44 #ifndef HAVE_AMALGAMATION
45 # include "nail.h"
46 #endif
48 EMPTY_FILE()
49 #ifdef HAVE_IMAP_SEARCH
51 enum itoken {
52 ITBAD, ITEOD, ITBOL, ITEOL, ITAND, ITSET, ITALL, ITANSWERED,
53 ITBCC, ITBEFORE, ITBODY,
54 ITCC,
55 ITDELETED, ITDRAFT,
56 ITFLAGGED, ITFROM,
57 ITHEADER,
58 ITKEYWORD,
59 ITLARGER,
60 ITNEW, ITNOT,
61 ITOLD, ITON, ITOR,
62 ITRECENT,
63 ITSEEN, ITSENTBEFORE, ITSENTON, ITSENTSINCE, ITSINCE, ITSMALLER,
64 ITSUBJECT,
65 ITTEXT, ITTO,
66 ITUID, ITUNANSWERED, ITUNDELETED, ITUNDRAFT, ITUNFLAGGED, ITUNKEYWORD,
67 ITUNSEEN
70 struct itlex {
71 char const *s_string;
72 enum itoken s_token;
75 struct itnode {
76 enum itoken n_token;
77 unsigned long n_n;
78 void *n_v;
79 void *n_w;
80 struct itnode *n_x;
81 struct itnode *n_y;
84 static struct itlex const _it_strings[] = {
85 { "ALL", ITALL },
86 { "ANSWERED", ITANSWERED },
87 { "BCC", ITBCC },
88 { "BEFORE", ITBEFORE },
89 { "BODY", ITBODY },
90 { "CC", ITCC },
91 { "DELETED", ITDELETED },
92 { "DRAFT", ITDRAFT },
93 { "FLAGGED", ITFLAGGED },
94 { "FROM", ITFROM },
95 { "HEADER", ITHEADER },
96 { "KEYWORD", ITKEYWORD },
97 { "LARGER", ITLARGER },
98 { "NEW", ITNEW },
99 { "NOT", ITNOT },
100 { "OLD", ITOLD },
101 { "ON", ITON },
102 { "OR", ITOR },
103 { "RECENT", ITRECENT },
104 { "SEEN", ITSEEN },
105 { "SENTBEFORE", ITSENTBEFORE },
106 { "SENTON", ITSENTON },
107 { "SENTSINCE", ITSENTSINCE },
108 { "SINCE", ITSINCE },
109 { "SMALLER", ITSMALLER },
110 { "SUBJECT", ITSUBJECT },
111 { "TEXT", ITTEXT },
112 { "TO", ITTO },
113 { "UID", ITUID },
114 { "UNANSWERED", ITUNANSWERED },
115 { "UNDELETED", ITUNDELETED },
116 { "UNDRAFT", ITUNDRAFT },
117 { "UNFLAGGED", ITUNFLAGGED },
118 { "UNKEYWORD", ITUNKEYWORD },
119 { "UNSEEN", ITUNSEEN },
120 { NULL, ITBAD }
123 static struct itnode *_it_tree;
124 static char *_it_begin;
125 static enum itoken _it_token;
126 static unsigned long _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,
142 void const *what);
143 static int matchenvelope(struct message *m, char const *field,
144 void const *what);
145 static char * mkenvelope(struct name *np);
146 static char const * around(char const *cp);
148 static enum okay
149 itparse(char const *spec, char const **xp, int sub)
151 int level = 0;
152 struct itnode n, *z, *ittree;
153 enum okay rv;
154 NYD_ENTER;
156 _it_tree = NULL;
157 while ((rv = itscan(spec, xp)) == OKAY && _it_token != ITBAD &&
158 _it_token != ITEOD) {
159 ittree = _it_tree;
160 memset(&n, 0, sizeof n);
161 spec = *xp;
162 switch (_it_token) {
163 case ITBOL:
164 ++level;
165 continue;
166 case ITEOL:
167 if (--level == 0)
168 goto jleave;
169 if (level < 0) {
170 if (sub > 0) {
171 --(*xp);
172 goto jleave;
174 n_err(_("Excess in )\n"));
175 rv = STOP;
176 goto jleave;
178 continue;
179 case ITNOT:
180 /* <search-key> */
181 n.n_token = ITNOT;
182 if ((rv = itparse(spec, xp, sub + 1)) == STOP)
183 goto jleave;
184 spec = *xp;
185 if ((n.n_x = _it_tree) == NULL) {
186 n_err(_("Criterion for NOT missing: >>> %s <<<\n"), around(*xp));
187 rv = STOP;
188 goto jleave;
190 _it_token = ITNOT;
191 break;
192 case ITOR:
193 /* <search-key1> <search-key2> */
194 n.n_token = ITOR;
195 if ((rv = itparse(spec, xp, sub + 1)) == STOP)
196 goto jleave;
197 if ((n.n_x = _it_tree) == NULL) {
198 n_err(_("First criterion for OR missing: >>> %s <<<\n"),
199 around(*xp));
200 rv = STOP;
201 goto jleave;
203 spec = *xp;
204 if ((rv = itparse(spec, xp, sub + 1)) == STOP)
205 goto jleave;
206 spec = *xp;
207 if ((n.n_y = _it_tree) == NULL) {
208 n_err(_("Second criterion for OR missing: >>> %s <<<\n"),
209 around(*xp));
210 rv = STOP;
211 goto jleave;
213 break;
214 default:
215 n.n_token = _it_token;
216 n.n_n = _it_number;
217 n.n_v = _it_args[0];
218 n.n_w = _it_args[1];
221 _it_tree = ittree;
222 if (_it_tree == NULL) {
223 _it_tree = salloc(sizeof *_it_tree);
224 *_it_tree = n;
225 } else {
226 z = _it_tree;
227 _it_tree = salloc(sizeof *_it_tree);
228 _it_tree->n_token = ITAND;
229 _it_tree->n_x = z;
230 _it_tree->n_y = salloc(sizeof *_it_tree->n_y);
231 *_it_tree->n_y = n;
233 if (sub && level == 0)
234 break;
236 jleave:
237 NYD_LEAVE;
238 return rv;
241 static enum okay
242 itscan(char const *spec, char const **xp)
244 int i, n;
245 enum okay rv = OKAY;
246 NYD_ENTER;
248 while (spacechar(*spec))
249 ++spec;
250 if (*spec == '(') {
251 *xp = &spec[1];
252 _it_token = ITBOL;
253 goto jleave;
255 if (*spec == ')') {
256 *xp = &spec[1];
257 _it_token = ITEOL;
258 goto jleave;
260 while (spacechar(*spec))
261 ++spec;
262 if (*spec == '\0') {
263 _it_token = ITEOD;
264 goto jleave;
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;
272 spec += n;
273 while (spacechar(*spec))
274 ++spec;
275 rv = itsplit(spec, xp);
276 goto jleave;
279 if (digitchar(*spec)) {
280 _it_number = strtoul(spec, n_UNCONST(xp), 10);
281 if (!__GO(**xp)) {
282 _it_token = ITSET;
283 goto jleave;
287 n_err(_("Bad SEARCH criterion: "));
288 for (i = 0; __GO(spec[i]); ++i)
290 n_err(_("%.*s: >>> %s <<<\n"), i, spec, around(*xp));
291 #undef __GO
293 _it_token = ITBAD;
294 rv = STOP;
295 jleave:
296 NYD_LEAVE;
297 return rv;
300 static enum okay
301 itsplit(char const *spec, char const **xp)
303 char *cp;
304 time_t t;
305 enum okay rv;
306 NYD_ENTER;
308 switch (_it_token) {
309 case ITBCC:
310 case ITBODY:
311 case ITCC:
312 case ITFROM:
313 case ITSUBJECT:
314 case ITTEXT:
315 case ITTO:
316 /* <string> */
317 ++_it_need_headers;
318 rv = itstring(_it_args, spec, xp);
319 break;
320 case ITSENTBEFORE:
321 case ITSENTON:
322 case ITSENTSINCE:
323 ++_it_need_headers;
324 /*FALLTHRU*/
325 case ITBEFORE:
326 case ITON:
327 case ITSINCE:
328 /* <date> */
329 if ((rv = itstring(_it_args, spec, xp)) != OKAY)
330 break;
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));
334 rv = STOP;
335 break;
337 _it_number = t;
338 rv = OKAY;
339 break;
340 case ITHEADER:
341 /* <field-name> <string> */
342 ++_it_need_headers;
343 if ((rv = itstring(_it_args, spec, xp)) != OKAY)
344 break;
345 spec = *xp;
346 if ((rv = itstring(_it_args + 1, spec, xp)) != OKAY)
347 break;
348 break;
349 case ITKEYWORD:
350 case ITUNKEYWORD:
351 /* <flag> */ /* TODO use table->flag map search instead */
352 if ((rv = itstring(_it_args, spec, xp)) != OKAY)
353 break;
354 if (!asccasecmp(_it_args[0], "\\Seen"))
355 _it_number = MREAD;
356 else if (!asccasecmp(_it_args[0], "\\Deleted"))
357 _it_number = MDELETED;
358 else if (!asccasecmp(_it_args[0], "\\Recent"))
359 _it_number = MNEW;
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"))
365 _it_number = MDRAFT;
366 else
367 _it_number = 0;
368 break;
369 case ITLARGER:
370 case ITSMALLER:
371 /* <n> */
372 if ((rv = itstring(_it_args, spec, xp)) != OKAY)
373 break;
374 _it_number = strtoul(_it_args[0], &cp, 10);
375 if (spacechar(*cp) || *cp == '\0')
376 break;
377 n_err(_("Invalid size: >>> %s <<<\n"), around(*xp));
378 rv = STOP;
379 break;
380 case ITUID:
381 /* <message set> */
382 n_err(_("Searching for UIDs is not supported: >>> %s <<<\n"),
383 around(*xp));
384 rv = STOP;
385 break;
386 default:
387 *xp = spec;
388 rv = OKAY;
389 break;
391 NYD_LEAVE;
392 return rv;
395 static enum okay
396 itstring(void **tp, char const *spec, char const **xp) /* XXX lesser derefs */
398 int inquote = 0;
399 char *ap;
400 enum okay rv = STOP;
401 NYD_ENTER;
403 while (spacechar(*spec))
404 ++spec;
405 if (*spec == '\0' || *spec == '(' || *spec == ')') {
406 n_err(_("Missing string argument: >>> %s <<<\n"),
407 around(&(*xp)[spec - *xp]));
408 goto jleave;
410 ap = *tp = salloc(strlen(spec) +1);
411 *xp = spec;
412 do {
413 if (inquote && **xp == '\\')
414 *ap++ = *(*xp)++;
415 else if (**xp == '"')
416 inquote = !inquote;
417 else if (!inquote && (spacechar(**xp) || **xp == '(' || **xp == ')')) {
418 *ap++ = '\0';
419 break;
421 *ap++ = **xp;
422 } while (*(*xp)++);
424 *tp = _imap_unquotestr(*tp);
425 rv = OKAY;
426 jleave:
427 NYD_LEAVE;
428 return rv;
431 static int
432 itexecute(struct mailbox *mp, struct message *m, size_t c, struct itnode *n)
434 struct search_expr se;
435 char *cp, *line = NULL; /* TODO line pool */
436 size_t linesize = 0;
437 FILE *ibuf;
438 int rv;
439 NYD_ENTER;
441 if (n == NULL) {
442 n_err(_("Internal error: Empty node in SEARCH tree\n"));
443 rv = 0;
444 goto jleave;
447 switch (n->n_token) {
448 case ITBEFORE:
449 case ITON:
450 case ITSINCE:
451 if (m->m_time == 0 && !(m->m_flag & MNOFROM) &&
452 (ibuf = setinput(mp, m, NEED_HEADER)) != NULL) {
453 if (readline_restart(ibuf, &line, &linesize, 0) > 0)
454 m->m_time = unixtime(line);
455 free(line);
457 break;
458 case ITSENTBEFORE:
459 case ITSENTON:
460 case ITSENTSINCE:
461 if (m->m_date == 0)
462 if ((cp = hfield1("date", m)) != NULL)
463 m->m_date = rfctime(cp);
464 break;
465 default:
466 break;
469 switch (n->n_token) {
470 default:
471 n_err(_("Internal SEARCH error: Lost token %d\n"), n->n_token);
472 rv = 0;
473 break;
474 case ITAND:
475 rv = itexecute(mp, m, c, n->n_x) & itexecute(mp, m, c, n->n_y);
476 break;
477 case ITSET:
478 rv = UICMP(z, c, ==, n->n_n);
479 break;
480 case ITALL:
481 rv = 1;
482 break;
483 case ITANSWERED:
484 rv = ((m->m_flag & MANSWERED) != 0);
485 break;
486 case ITBCC:
487 rv = matchenvelope(m, "bcc", n->n_v);
488 break;
489 case ITBEFORE:
490 rv = UICMP(z, m->m_time, <, n->n_n);
491 break;
492 case ITBODY:
493 se.ss_sexpr = n->n_v;
494 se.ss_where = "body";
495 rv = message_match(m, &se, FAL0);
496 break;
497 case ITCC:
498 rv = matchenvelope(m, "cc", n->n_v);
499 break;
500 case ITDELETED:
501 rv = ((m->m_flag & MDELETED) != 0);
502 break;
503 case ITDRAFT:
504 rv = ((m->m_flag & MDRAFTED) != 0);
505 break;
506 case ITFLAGGED:
507 rv = ((m->m_flag & MFLAGGED) != 0);
508 break;
509 case ITFROM:
510 rv = matchenvelope(m, "from", n->n_v);
511 break;
512 case ITHEADER:
513 rv = matchfield(m, n->n_v, n->n_w);
514 break;
515 case ITKEYWORD:
516 rv = ((m->m_flag & n->n_n) != 0);
517 break;
518 case ITLARGER:
519 rv = (m->m_xsize > n->n_n);
520 break;
521 case ITNEW:
522 rv = ((m->m_flag & (MNEW | MREAD)) == MNEW);
523 break;
524 case ITNOT:
525 rv = !itexecute(mp, m, c, n->n_x);
526 break;
527 case ITOLD:
528 rv = !(m->m_flag & MNEW);
529 break;
530 case ITON:
531 rv = (UICMP(z, m->m_time, >=, n->n_n) &&
532 UICMP(z, m->m_time, <, n->n_n + 86400));
533 break;
534 case ITOR:
535 rv = itexecute(mp, m, c, n->n_x) | itexecute(mp, m, c, n->n_y);
536 break;
537 case ITRECENT:
538 rv = ((m->m_flag & MNEW) != 0);
539 break;
540 case ITSEEN:
541 rv = ((m->m_flag & MREAD) != 0);
542 break;
543 case ITSENTBEFORE:
544 rv = UICMP(z, m->m_date, <, n->n_n);
545 break;
546 case ITSENTON:
547 rv = (UICMP(z, m->m_date, >=, n->n_n) &&
548 UICMP(z, m->m_date, <, n->n_n + 86400));
549 break;
550 case ITSENTSINCE:
551 rv = UICMP(z, m->m_date, >=, n->n_n);
552 break;
553 case ITSINCE:
554 rv = UICMP(z, m->m_time, >=, n->n_n);
555 break;
556 case ITSMALLER:
557 rv = UICMP(z, m->m_xsize, <, n->n_n);
558 break;
559 case ITSUBJECT:
560 rv = matchfield(m, "subject", n->n_v);
561 break;
562 case ITTEXT:
563 se.ss_sexpr = n->n_v;
564 se.ss_where = "text";
565 rv = message_match(m, &se, TRU1);
566 break;
567 case ITTO:
568 rv = matchenvelope(m, "to", n->n_v);
569 break;
570 case ITUNANSWERED:
571 rv = !(m->m_flag & MANSWERED);
572 break;
573 case ITUNDELETED:
574 rv = !(m->m_flag & MDELETED);
575 break;
576 case ITUNDRAFT:
577 rv = !(m->m_flag & MDRAFTED);
578 break;
579 case ITUNFLAGGED:
580 rv = !(m->m_flag & MFLAGGED);
581 break;
582 case ITUNKEYWORD:
583 rv = !(m->m_flag & n->n_n);
584 break;
585 case ITUNSEEN:
586 rv = !(m->m_flag & MREAD);
587 break;
589 jleave:
590 NYD_LEAVE;
591 return rv;
594 static time_t
595 _imap_read_date(char const *cp)
597 time_t t = (time_t)-1;
598 int year, month, day, i, tzdiff;
599 struct tm *tmptr;
600 char *xp, *yp;
601 NYD_ENTER;
603 if (*cp == '"')
604 ++cp;
605 day = strtol(cp, &xp, 10);
606 if (day <= 0 || day > 31 || *xp++ != '-')
607 goto jleave;
609 for (i = 0;;) {
610 if (!ascncasecmp(xp, month_names[i], 3))
611 break;
612 if (month_names[++i][0] == '\0')
613 goto jleave;
615 month = i + 1;
616 if (xp[3] != '-')
617 goto jleave;
618 year = strtol(xp + 4, &yp, 10);
619 if (year < 1970 || year > 2037 || PTRCMP(yp, !=, xp + 8))
620 goto jleave;
621 if (yp[0] != '\0' && (yp[1] != '"' || yp[2] != '\0'))
622 goto jleave;
623 if ((t = combinetime(year, month, day, 0, 0, 0)) == (time_t)-1)
624 goto jleave;
625 tzdiff = t - mktime(gmtime(&t));
626 tmptr = localtime(&t);
627 if (tmptr->tm_isdst > 0)
628 tzdiff += 3600;
629 t -= tzdiff;
630 jleave:
631 NYD_LEAVE;
632 return t;
635 static char *
636 _imap_quotestr(char const *s)
638 char *n, *np;
639 NYD2_ENTER;
641 np = n = salloc(2 * strlen(s) + 3);
642 *np++ = '"';
643 while (*s) {
644 if (*s == '"' || *s == '\\')
645 *np++ = '\\';
646 *np++ = *s++;
648 *np++ = '"';
649 *np = '\0';
650 NYD2_LEAVE;
651 return n;
654 static char *
655 _imap_unquotestr(char const *s)
657 char *n, *np;
658 NYD2_ENTER;
660 if (*s != '"') {
661 n = savestr(s);
662 goto jleave;
665 np = n = salloc(strlen(s) + 1);
666 while (*++s) {
667 if (*s == '\\')
668 s++;
669 else if (*s == '"')
670 break;
671 *np++ = *s;
673 *np = '\0';
674 jleave:
675 NYD2_LEAVE;
676 return n;
679 static bool_t
680 matchfield(struct message *m, char const *field, void const *what)
682 struct str in, out;
683 bool_t rv = FAL0;
684 NYD_ENTER;
686 if ((in.s = hfieldX(field, m)) == NULL)
687 goto jleave;
689 in.l = strlen(in.s);
690 mime_fromhdr(&in, &out, TD_ICONV);
691 rv = substr(out.s, what);
692 free(out.s);
693 jleave:
694 NYD_LEAVE;
695 return rv;
698 static int
699 matchenvelope(struct message *m, char const *field, void const *what)
701 struct name *np;
702 char *cp;
703 int rv = 0;
704 NYD_ENTER;
706 if ((cp = hfieldX(field, m)) == NULL)
707 goto jleave;
709 for (np = lextract(cp, GFULL); np != NULL; np = np->n_flink) {
710 if (!substr(np->n_name, what) && !substr(mkenvelope(np), what))
711 continue;
712 rv = 1;
713 break;
716 jleave:
717 NYD_LEAVE;
718 return rv;
721 static char *
722 mkenvelope(struct name *np)
724 size_t epsize;
725 char *ep, *realnam = NULL, *sourceaddr = NULL, *localpart = NULL,
726 *domainpart = NULL, *cp, *rp, *xp, *ip;
727 struct str in, out;
728 int level = 0;
729 bool_t hadphrase = FAL0;
730 NYD_ENTER;
732 in.s = np->n_fullname;
733 in.l = strlen(in.s);
734 mime_fromhdr(&in, &out, TD_ICONV);
735 rp = ip = ac_alloc(strlen(out.s) + 1);
736 for (cp = out.s; *cp; cp++) {
737 switch (*cp) {
738 case '"':
739 while (*cp) {
740 if (*++cp == '"')
741 break;
742 if (cp[0] == '\\' && cp[1] != '\0')
743 ++cp;
744 *rp++ = *cp;
746 break;
747 case '<':
748 while (cp > out.s && blankchar(cp[-1]))
749 --cp;
750 rp = ip;
751 xp = out.s;
752 if (PTRCMP(xp, <, cp - 1) && *xp == '"' && cp[-1] == '"') {
753 ++xp;
754 --cp;
756 while (xp < cp)
757 *rp++ = *xp++;
758 hadphrase = TRU1;
759 goto jdone;
760 case '(':
761 if (level++)
762 goto jdfl;
763 if (!hadphrase)
764 rp = ip;
765 hadphrase = TRU1;
766 break;
767 case ')':
768 if (--level)
769 goto jdfl;
770 break;
771 case '\\':
772 if (level && cp[1] != '\0')
773 cp++;
774 goto jdfl;
775 default:
776 jdfl:
777 *rp++ = *cp;
780 jdone:
781 *rp = '\0';
782 if (hadphrase)
783 realnam = ip;
784 free(out.s);
785 localpart = savestr(np->n_name);
786 if ((cp = strrchr(localpart, '@')) != NULL) {
787 *cp = '\0';
788 domainpart = cp + 1;
791 ep = salloc(epsize = strlen(np->n_fullname) * 2 + 40);
792 snprintf(ep, epsize, "(%s %s %s %s)",
793 realnam ? _imap_quotestr(realnam) : "NIL",
794 sourceaddr ? _imap_quotestr(sourceaddr) : "NIL",
795 localpart ? _imap_quotestr(localpart) : "NIL",
796 domainpart ? _imap_quotestr(domainpart) : "NIL");
797 ac_free(ip);
798 NYD_LEAVE;
799 return ep;
802 #define SURROUNDING 16
803 static char const *
804 around(char const *cp)
806 static char ab[2 * SURROUNDING +1];
808 size_t i;
809 NYD_ENTER;
811 for (i = 0; i < SURROUNDING && cp > _it_begin; ++i)
812 --cp;
813 for (i = 0; i < sizeof(ab) -1; ++i)
814 ab[i] = *cp++;
815 ab[i] = '\0';
816 NYD_LEAVE;
817 return ab;
820 FL ssize_t
821 imap_search(char const *spec, int f)
823 static char *lastspec;
825 char const *xp;
826 size_t i;
827 ssize_t rv;
828 NYD_ENTER;
830 if (strcmp(spec, "()")) {
831 if (lastspec != NULL)
832 free(lastspec);
833 i = strlen(spec);
834 lastspec = sbufdup(spec, i);
835 } else if (lastspec == NULL) {
836 n_err(_("No last SEARCH criteria available\n"));
837 rv = -1;
838 goto jleave;
840 spec =
841 _it_begin = lastspec;
843 _it_need_headers = FAL0;
844 #if 0
845 if ((rv = imap_search1(spec, f) == OKAY))
846 goto jleave;
847 #endif
848 if (itparse(spec, &xp, 0) == STOP){
849 rv = -1;
850 goto jleave;
853 rv = 0;
855 if (_it_tree == NULL)
856 goto jleave;
858 #if 0
859 if (mb.mb_type == MB_IMAP && _it_need_headers)
860 imap_getheaders(1, msgCount);
861 #endif
862 srelax_hold();
863 for (i = 0; UICMP(z, i, <, msgCount); ++i) {
864 if (message[i].m_flag & MHIDDEN)
865 continue;
866 if (f == MDELETED || !(message[i].m_flag & MDELETED)) {
867 size_t j = (int)(i + 1);
868 if (itexecute(&mb, message + i, j, _it_tree)){
869 mark((int)j, f);
870 ++rv;
872 srelax();
875 srelax_rele();
876 jleave:
877 NYD_LEAVE;
878 return rv;
880 #endif /* HAVE_IMAP_SEARCH */
882 /* s-it-mode */