2 * Copyright (C) 1996-2000,2006-7 Michael R. Elkins <me@mutt.org>, and others
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
36 #include "mutt_crypt.h"
37 #include "mutt_curses.h"
41 #include "imap/imap.h"
44 static int eat_regexp (pattern_t *pat, BUFFER *, BUFFER *);
45 static int eat_date (pattern_t *pat, BUFFER *, BUFFER *);
46 static int eat_range (pattern_t *pat, BUFFER *, BUFFER *);
47 static int patmatch (const pattern_t *pat, const char *buf);
49 static struct pattern_flags
51 int tag; /* character used to represent this op */
52 int op; /* operation to perform */
54 int (*eat_arg) (pattern_t *, BUFFER *, BUFFER *);
58 { 'A', M_ALL, 0, NULL },
59 { 'b', M_BODY, M_FULL_MSG, eat_regexp },
60 { 'B', M_WHOLE_MSG, M_FULL_MSG, eat_regexp },
61 { 'c', M_CC, 0, eat_regexp },
62 { 'C', M_RECIPIENT, 0, eat_regexp },
63 { 'd', M_DATE, 0, eat_date },
64 { 'D', M_DELETED, 0, NULL },
65 { 'e', M_SENDER, 0, eat_regexp },
66 { 'E', M_EXPIRED, 0, NULL },
67 { 'f', M_FROM, 0, eat_regexp },
68 { 'F', M_FLAG, 0, NULL },
69 { 'g', M_CRYPT_SIGN, 0, NULL },
70 { 'G', M_CRYPT_ENCRYPT, 0, NULL },
71 { 'h', M_HEADER, M_FULL_MSG, eat_regexp },
72 { 'H', M_HORMEL, 0, eat_regexp },
73 { 'i', M_ID, 0, eat_regexp },
74 { 'k', M_PGP_KEY, 0, NULL },
75 { 'l', M_LIST, 0, NULL },
76 { 'L', M_ADDRESS, 0, eat_regexp },
77 { 'm', M_MESSAGE, 0, eat_range },
78 { 'n', M_SCORE, 0, eat_range },
79 { 'N', M_NEW, 0, NULL },
80 { 'O', M_OLD, 0, NULL },
81 { 'p', M_PERSONAL_RECIP, 0, NULL },
82 { 'P', M_PERSONAL_FROM, 0, NULL },
83 { 'Q', M_REPLIED, 0, NULL },
84 { 'r', M_DATE_RECEIVED, 0, eat_date },
85 { 'R', M_READ, 0, NULL },
86 { 's', M_SUBJECT, 0, eat_regexp },
87 { 'S', M_SUPERSEDED, 0, NULL },
88 { 't', M_TO, 0, eat_regexp },
89 { 'T', M_TAG, 0, NULL },
90 { 'u', M_SUBSCRIBED_LIST, 0, NULL },
91 { 'U', M_UNREAD, 0, NULL },
92 { 'v', M_COLLAPSED, 0, NULL },
93 { 'V', M_CRYPT_VERIFIED, 0, NULL },
94 { 'x', M_REFERENCE, 0, eat_regexp },
95 { 'X', M_MIMEATTACH, 0, eat_range },
96 { 'y', M_XLABEL, 0, eat_regexp },
97 { 'z', M_SIZE, 0, eat_range },
98 { '=', M_DUPLICATED, 0, NULL },
99 { '$', M_UNREFERENCED, 0, NULL },
103 static pattern_t *SearchPattern = NULL; /* current search pattern */
104 static char LastSearch[STRING] = { 0 }; /* last pattern searched for */
105 static char LastSearchExpn[LONG_STRING] = { 0 }; /* expanded version of
108 #define M_MAXRANGE -1
110 /* constants for parse_date_range() */
111 #define M_PDR_NONE 0x0000
112 #define M_PDR_MINUS 0x0001
113 #define M_PDR_PLUS 0x0002
114 #define M_PDR_WINDOW 0x0004
115 #define M_PDR_ABSOLUTE 0x0008
116 #define M_PDR_DONE 0x0010
117 #define M_PDR_ERROR 0x0100
118 #define M_PDR_ERRORDONE (M_PDR_ERROR | M_PDR_DONE)
121 /* if no uppercase letters are given, do a case-insensitive search */
122 int mutt_which_case (const char *s)
128 memset (&mb, 0, sizeof (mb));
130 for (; (l = mbrtowc (&w, s, MB_CUR_MAX, &mb)) != 0; s += l)
132 if (l == (size_t) -2)
133 continue; /* shift sequences */
134 if (l == (size_t) -1)
135 return 0; /* error; assume case-sensitive */
136 if (iswalpha ((wint_t) w) && iswupper ((wint_t) w))
137 return 0; /* case-sensitive */
140 return REG_ICASE; /* case-insensitive */
144 msg_search (CONTEXT *ctx, pattern_t* pat, int msgno)
146 char tempfile[_POSIX_PATH_MAX];
153 HEADER *h = ctx->hdrs[msgno];
157 if ((msg = mx_open_message (ctx, msgno)) != NULL)
159 if (option (OPTTHOROUGHSRC))
161 /* decode the header / body */
162 memset (&s, 0, sizeof (s));
164 s.flags = M_CHARCONV;
165 mutt_mktemp (tempfile);
166 if ((s.fpout = safe_fopen (tempfile, "w+")) == NULL)
168 mutt_perror (tempfile);
172 if (pat->op != M_BODY)
173 mutt_copy_header (msg->fp, h, s.fpout, CH_FROM | CH_DECODE, NULL);
175 if (pat->op != M_HEADER)
177 mutt_parse_mime_message (ctx, h);
179 if (WithCrypto && (h->security & ENCRYPT)
180 && !crypt_valid_passphrase(h->security))
182 mx_close_message (&msg);
191 fseeko (msg->fp, h->offset, 0);
192 mutt_body_handler (h->content, &s);
198 fstat (fileno (fp), &st);
199 lng = (long) st.st_size;
203 /* raw header / body */
205 if (pat->op != M_BODY)
207 fseeko (fp, h->offset, 0);
208 lng = h->content->offset - h->offset;
210 if (pat->op != M_HEADER)
212 if (pat->op == M_BODY)
213 fseeko (fp, h->content->offset, 0);
214 lng += h->content->length;
219 buf = safe_malloc (blen);
221 /* search the file "fp" */
224 if (pat->op == M_HEADER)
226 if (*(buf = mutt_read_rfc822_line (fp, buf, &blen)) == '\0')
229 else if (fgets (buf, blen - 1, fp) == NULL)
230 break; /* don't loop forever */
231 if (patmatch (pat, buf) == 0)
236 lng -= mutt_strlen (buf);
241 mx_close_message (&msg);
243 if (option (OPTTHOROUGHSRC))
253 static int eat_regexp (pattern_t *pat, BUFFER *s, BUFFER *err)
258 memset (&buf, 0, sizeof (buf));
259 if (mutt_extract_token (&buf, s, M_TOKEN_PATTERN | M_TOKEN_COMMENT) != 0 ||
262 snprintf (err->data, err->dsize, _("Error in expression: %s"), s->dptr);
267 snprintf (err->data, err->dsize, _("Empty expression"));
272 /* If there are no RE metacharacters, use simple search anyway */
273 if (!pat->stringmatch && !strpbrk (buf.data, "|[{.*+?^$"))
274 pat->stringmatch = 1;
277 if (pat->stringmatch)
279 pat->p.str = safe_strdup (buf.data);
282 else if (pat->groupmatch)
284 pat->p.g = mutt_pattern_group (buf.data);
289 pat->p.rx = safe_malloc (sizeof (regex_t));
290 r = REGCOMP (pat->p.rx, buf.data, REG_NEWLINE | REG_NOSUB | mutt_which_case (buf.data));
294 regerror (r, pat->p.rx, err->data, err->dsize);
304 int eat_range (pattern_t *pat, BUFFER *s, BUFFER *err)
307 int do_exclusive = 0;
311 * If simple_search is set to "~m %s", the range will have double quotes
321 if ((*s->dptr != '-') && (*s->dptr != '<'))
326 pat->max = M_MAXRANGE;
327 pat->min = strtol (s->dptr + 1, &tmp, 0) + 1; /* exclusive range */
330 pat->min = strtol (s->dptr, &tmp, 0);
331 if (toupper ((unsigned char) *tmp) == 'K') /* is there a prefix? */
336 else if (toupper ((unsigned char) *tmp) == 'M')
361 if (isdigit ((unsigned char) *tmp))
364 pat->max = strtol (tmp, &tmp, 0);
365 if (toupper ((unsigned char) *tmp) == 'K')
370 else if (toupper ((unsigned char) *tmp) == 'M')
379 pat->max = M_MAXRANGE;
381 if (skip_quote && *tmp == '"')
389 static const char *getDate (const char *s, struct tm *t, BUFFER *err)
392 time_t now = time (NULL);
393 struct tm *tm = localtime (&now);
395 t->tm_mday = strtol (s, &p, 10);
396 if (t->tm_mday < 1 || t->tm_mday > 31)
398 snprintf (err->data, err->dsize, _("Invalid day of month: %s"), s);
403 /* fill in today's month and year */
404 t->tm_mon = tm->tm_mon;
405 t->tm_year = tm->tm_year;
409 t->tm_mon = strtol (p, &p, 10) - 1;
410 if (t->tm_mon < 0 || t->tm_mon > 11)
412 snprintf (err->data, err->dsize, _("Invalid month: %s"), p);
417 t->tm_year = tm->tm_year;
421 t->tm_year = strtol (p, &p, 10);
422 if (t->tm_year < 70) /* year 2000+ */
424 else if (t->tm_year > 1900)
433 static const char *get_offset (struct tm *tm, const char *s, int sign)
436 int offset = strtol (s, &ps, 0);
437 if ((sign < 0 && offset > 0) || (sign > 0 && offset < 0))
443 tm->tm_year += offset;
446 tm->tm_mon += offset;
449 tm->tm_mday += 7 * offset;
452 tm->tm_mday += offset;
457 mutt_normalize_time (tm);
461 static void adjust_date_range (struct tm *min, struct tm *max)
463 if (min->tm_year > max->tm_year
464 || (min->tm_year == max->tm_year && min->tm_mon > max->tm_mon)
465 || (min->tm_year == max->tm_year && min->tm_mon == max->tm_mon
466 && min->tm_mday > max->tm_mday))
471 min->tm_year = max->tm_year;
475 min->tm_mon = max->tm_mon;
479 min->tm_mday = max->tm_mday;
482 min->tm_hour = min->tm_min = min->tm_sec = 0;
484 max->tm_min = max->tm_sec = 59;
488 static const char * parse_date_range (const char* pc, struct tm *min,
489 struct tm *max, int haveMin, struct tm *baseMin, BUFFER *err)
491 int flag = M_PDR_NONE;
492 while (*pc && ((flag & M_PDR_DONE) == 0))
501 /* try a range of absolute date minus offset of Ndwmy */
502 pt = get_offset (min, pc, -1);
505 if (flag == M_PDR_NONE)
506 { /* nothing yet and no offset parsed => absolute date? */
507 if (!getDate (pc, max, err))
508 flag |= (M_PDR_ABSOLUTE | M_PDR_ERRORDONE); /* done bad */
511 /* reestablish initial base minimum if not specified */
513 memcpy (min, baseMin, sizeof(struct tm));
514 flag |= (M_PDR_ABSOLUTE | M_PDR_DONE); /* done good */
518 flag |= M_PDR_ERRORDONE;
523 if (flag == M_PDR_NONE && !haveMin)
524 { /* the very first "-3d" without a previous absolute date */
525 max->tm_year = min->tm_year;
526 max->tm_mon = min->tm_mon;
527 max->tm_mday = min->tm_mday;
534 { /* enlarge plusRange */
535 pt = get_offset (max, pc, 1);
537 flag |= M_PDR_ERRORDONE;
546 { /* enlarge window in both directions */
547 pt = get_offset (min, pc, -1);
549 flag |= M_PDR_ERRORDONE;
552 pc = get_offset (max, pc, 1);
553 flag |= M_PDR_WINDOW;
558 flag |= M_PDR_ERRORDONE;
562 if ((flag & M_PDR_ERROR) && !(flag & M_PDR_ABSOLUTE))
563 { /* getDate has its own error message, don't overwrite it here */
564 snprintf (err->data, err->dsize, _("Invalid relative date: %s"), pc-1);
566 return ((flag & M_PDR_ERROR) ? NULL : pc);
569 static int eat_date (pattern_t *pat, BUFFER *s, BUFFER *err)
574 memset (&buffer, 0, sizeof (buffer));
575 if (mutt_extract_token (&buffer, s, M_TOKEN_COMMENT | M_TOKEN_PATTERN) != 0
578 strfcpy (err->data, _("error in expression"), err->dsize);
582 memset (&min, 0, sizeof (min));
583 /* the `0' time is Jan 1, 1970 UTC, so in order to prevent a negative time
584 when doing timezone conversion, we use Jan 2, 1970 UTC as the base
589 memset (&max, 0, sizeof (max));
591 /* Arbitrary year in the future. Don't set this too high
592 or mutt_mktime() returns something larger than will
593 fit in a time_t on some systems */
601 if (strchr ("<>=", buffer.data[0]))
603 /* offset from current time
604 <3d less than three days ago
605 >3d more than three days ago
606 =3d exactly three days ago */
607 time_t now = time (NULL);
608 struct tm *tm = localtime (&now);
611 if (buffer.data[0] == '<')
613 memcpy (&min, tm, sizeof (min));
618 memcpy (&max, tm, sizeof (max));
621 if (buffer.data[0] == '=')
625 tm->tm_min = tm->tm_sec = 59;
627 /* force negative offset */
628 get_offset (tm, buffer.data + 1, -1);
632 /* start at the beginning of the day in question */
633 memcpy (&min, &max, sizeof (max));
634 min.tm_hour = min.tm_sec = min.tm_min = 0;
639 const char *pc = buffer.data;
642 int untilNow = FALSE;
643 if (isdigit ((unsigned char)*pc))
645 /* mininum date specified */
646 if ((pc = getDate (pc, &min, err)) == NULL)
655 const char *pt = pc + 1;
657 untilNow = (*pt == '\0');
662 { /* max date or relative range/window */
667 { /* save base minimum and set current date, e.g. for "-3d+1d" */
668 time_t now = time (NULL);
669 struct tm *tm = localtime (&now);
670 memcpy (&baseMin, &min, sizeof(baseMin));
671 memcpy (&min, tm, sizeof (min));
672 min.tm_hour = min.tm_sec = min.tm_min = 0;
675 /* preset max date for relative offsets,
676 if nothing follows we search for messages on a specific day */
677 max.tm_year = min.tm_year;
678 max.tm_mon = min.tm_mon;
679 max.tm_mday = min.tm_mday;
681 if (!parse_date_range (pc, &min, &max, haveMin, &baseMin, err))
682 { /* bail out on any parsing error */
689 /* Since we allow two dates to be specified we'll have to adjust that. */
690 adjust_date_range (&min, &max);
692 pat->min = mutt_mktime (&min, 1);
693 pat->max = mutt_mktime (&max, 1);
700 static int patmatch (const pattern_t* pat, const char* buf)
702 if (pat->stringmatch)
703 return !strstr (buf, pat->p.str);
704 else if (pat->groupmatch)
705 return !mutt_group_match (pat->p.g, buf);
707 return regexec (pat->p.rx, buf, 0, NULL, 0);
710 static struct pattern_flags *lookup_tag (char tag)
714 for (i = 0; Flags[i].tag; i++)
715 if (Flags[i].tag == tag)
720 static /* const */ char *find_matching_paren (/* const */ char *s)
738 void mutt_pattern_free (pattern_t **pat)
747 if (tmp->stringmatch)
749 else if (tmp->groupmatch)
758 mutt_pattern_free (&tmp->child);
763 pattern_t *mutt_pattern_comp (/* const */ char *s, int flags, BUFFER *err)
765 pattern_t *curlist = NULL;
766 pattern_t *tmp, *tmp2;
767 pattern_t *last = NULL;
771 int implicit = 1; /* used to detect logical AND operator */
772 struct pattern_flags *entry;
777 memset (&ps, 0, sizeof (ps));
779 ps.dsize = mutt_strlen (s);
799 snprintf (err->data, err->dsize, _("error in pattern at: %s"), ps.dptr);
804 /* A & B | C == (A & B) | C */
805 tmp = new_pattern ();
807 tmp->child = curlist;
823 if (*(ps.dptr + 1) == '(')
825 ps.dptr ++; /* skip ~ */
826 p = find_matching_paren (ps.dptr + 1);
829 snprintf (err->data, err->dsize, _("mismatched brackets: %s"), ps.dptr);
830 mutt_pattern_free (&curlist);
833 tmp = new_pattern ();
841 tmp->alladdr |= alladdr;
844 /* compile the sub-expression */
845 buf = mutt_substrdup (ps.dptr + 1, p);
846 if ((tmp2 = mutt_pattern_comp (buf, flags, err)) == NULL)
849 mutt_pattern_free (&curlist);
854 ps.dptr = p + 1; /* restore location */
859 /* A | B & C == (A | B) & C */
860 tmp = new_pattern ();
862 tmp->child = curlist;
868 tmp = new_pattern ();
870 tmp->alladdr = alladdr;
871 tmp->stringmatch = (*ps.dptr == '=') ? 1 : 0;
872 tmp->groupmatch = (*ps.dptr == '%') ? 1 : 0;
882 ps.dptr++; /* move past the ~ */
883 if ((entry = lookup_tag (*ps.dptr)) == NULL)
885 snprintf (err->data, err->dsize, _("%c: invalid pattern modifier"), *ps.dptr);
886 mutt_pattern_free (&curlist);
889 if (entry->class && (flags & entry->class) == 0)
891 snprintf (err->data, err->dsize, _("%c: not supported in this mode"), *ps.dptr);
892 mutt_pattern_free (&curlist);
897 ps.dptr++; /* eat the operator and any optional whitespace */
904 snprintf (err->data, err->dsize, _("missing parameter"));
905 mutt_pattern_free (&curlist);
908 if (entry->eat_arg (tmp, &ps, err) == -1)
910 mutt_pattern_free (&curlist);
917 p = find_matching_paren (ps.dptr + 1);
920 snprintf (err->data, err->dsize, _("mismatched parenthesis: %s"), ps.dptr);
921 mutt_pattern_free (&curlist);
924 /* compile the sub-expression */
925 buf = mutt_substrdup (ps.dptr + 1, p);
926 if ((tmp = mutt_pattern_comp (buf, flags, err)) == NULL)
929 mutt_pattern_free (&curlist);
939 tmp->alladdr |= alladdr;
942 ps.dptr = p + 1; /* restore location */
945 snprintf (err->data, err->dsize, _("error in pattern at: %s"), ps.dptr);
946 mutt_pattern_free (&curlist);
952 strfcpy (err->data, _("empty pattern"), err->dsize);
957 tmp = new_pattern ();
958 tmp->op = or ? M_OR : M_AND;
959 tmp->child = curlist;
966 perform_and (pattern_t *pat, pattern_exec_flag flags, CONTEXT *ctx, HEADER *hdr)
968 for (; pat; pat = pat->next)
969 if (mutt_pattern_exec (pat, flags, ctx, hdr) <= 0)
975 perform_or (struct pattern_t *pat, pattern_exec_flag flags, CONTEXT *ctx, HEADER *hdr)
977 for (; pat; pat = pat->next)
978 if (mutt_pattern_exec (pat, flags, ctx, hdr) > 0)
983 static int match_adrlist (pattern_t *pat, int match_personal, int n, ...)
991 for (a = va_arg (ap, ADDRESS *) ; a ; a = a->next)
993 if (pat->alladdr ^ ((a->mailbox && patmatch (pat, a->mailbox) == 0) ||
994 (match_personal && a->personal && patmatch (pat, a->personal) == 0)))
997 return (! pat->alladdr); /* Found match, or non-match if alladdr */
1002 return pat->alladdr; /* No matches, or all matches if alladdr */
1005 static int match_reference (pattern_t *pat, LIST *refs)
1007 for (; refs; refs = refs->next)
1008 if (patmatch (pat, refs->data) == 0)
1014 * Matches subscribed mailing lists
1016 int mutt_is_list_recipient (int alladdr, ADDRESS *a1, ADDRESS *a2)
1018 for (; a1 ; a1 = a1->next)
1019 if (alladdr ^ mutt_is_subscribed_list (a1))
1021 for (; a2 ; a2 = a2->next)
1022 if (alladdr ^ mutt_is_subscribed_list (a2))
1028 * Matches known mailing lists
1029 * The function name may seem a little bit misleading: It checks all
1030 * recipients in To and Cc for known mailing lists, subscribed or not.
1032 int mutt_is_list_cc (int alladdr, ADDRESS *a1, ADDRESS *a2)
1034 for (; a1 ; a1 = a1->next)
1035 if (alladdr ^ mutt_is_mail_list (a1))
1037 for (; a2 ; a2 = a2->next)
1038 if (alladdr ^ mutt_is_mail_list (a2))
1043 static int match_user (int alladdr, ADDRESS *a1, ADDRESS *a2)
1045 for (; a1 ; a1 = a1->next)
1046 if (alladdr ^ mutt_addr_is_user (a1))
1048 for (; a2 ; a2 = a2->next)
1049 if (alladdr ^ mutt_addr_is_user (a2))
1054 static int match_threadcomplete(struct pattern_t *pat, pattern_exec_flag flags, CONTEXT *ctx, THREAD *t,int left,int up,int right,int down)
1063 if(mutt_pattern_exec(pat, flags, ctx, h))
1066 if(up && (a=match_threadcomplete(pat, flags, ctx, t->parent,1,1,1,0)))
1068 if(right && t->parent && (a=match_threadcomplete(pat, flags, ctx, t->next,0,0,1,1)))
1070 if(left && t->parent && (a=match_threadcomplete(pat, flags, ctx, t->prev,1,0,0,1)))
1072 if(down && (a=match_threadcomplete(pat, flags, ctx, t->child,1,0,1,1)))
1078 M_MATCH_FULL_ADDRESS match both personal and machine address */
1080 mutt_pattern_exec (struct pattern_t *pat, pattern_exec_flag flags, CONTEXT *ctx, HEADER *h)
1085 return (pat->not ^ (perform_and (pat->child, flags, ctx, h) > 0));
1087 return (pat->not ^ (perform_or (pat->child, flags, ctx, h) > 0));
1089 return (pat->not ^ match_threadcomplete(pat->child, flags, ctx, h->thread, 1, 1, 1, 1));
1093 return (pat->not ^ h->expired);
1095 return (pat->not ^ h->superseded);
1097 return (pat->not ^ h->flagged);
1099 return (pat->not ^ h->tagged);
1101 return (pat->not ? h->old || h->read : !(h->old || h->read));
1103 return (pat->not ? h->read : !h->read);
1105 return (pat->not ^ h->replied);
1107 return (pat->not ? (!h->old || h->read) : (h->old && !h->read));
1109 return (pat->not ^ h->read);
1111 return (pat->not ^ h->deleted);
1113 return (pat->not ^ (h->msgno >= pat->min - 1 && (pat->max == M_MAXRANGE ||
1114 h->msgno <= pat->max - 1)));
1116 return (pat->not ^ (h->date_sent >= pat->min && h->date_sent <= pat->max));
1117 case M_DATE_RECEIVED:
1118 return (pat->not ^ (h->received >= pat->min && h->received <= pat->max));
1123 * ctx can be NULL in certain cases, such as when replying to a message from the attachment menu and
1124 * the user has a reply-hook using "~h" (bug #2190).
1129 /* IMAP search sets h->matched at search compile time */
1130 if (ctx->magic == M_IMAP && pat->stringmatch)
1131 return (h->matched);
1133 return (pat->not ^ msg_search (ctx, pat, h->msgno));
1135 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS, 1,
1138 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS, 1,
1141 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS, 1,
1144 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS, 1,
1147 return (pat->not ^ (h->env->subject && patmatch (pat, h->env->subject) == 0));
1149 return (pat->not ^ (h->env->message_id && patmatch (pat, h->env->message_id) == 0));
1151 return (pat->not ^ (h->score >= pat->min && (pat->max == M_MAXRANGE ||
1152 h->score <= pat->max)));
1154 return (pat->not ^ (h->content->length >= pat->min && (pat->max == M_MAXRANGE || h->content->length <= pat->max)));
1156 return (pat->not ^ match_reference (pat, h->env->references));
1158 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS, 4,
1159 h->env->from, h->env->sender,
1160 h->env->to, h->env->cc));
1162 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
1163 2, h->env->to, h->env->cc));
1164 case M_LIST: /* known list, subscribed or not */
1165 return (pat->not ^ mutt_is_list_cc (pat->alladdr, h->env->to, h->env->cc));
1166 case M_SUBSCRIBED_LIST:
1167 return (pat->not ^ mutt_is_list_recipient (pat->alladdr, h->env->to, h->env->cc));
1168 case M_PERSONAL_RECIP:
1169 return (pat->not ^ match_user (pat->alladdr, h->env->to, h->env->cc));
1170 case M_PERSONAL_FROM:
1171 return (pat->not ^ match_user (pat->alladdr, h->env->from, NULL));
1173 return (pat->not ^ (h->collapsed && h->num_hidden > 1));
1177 return (pat->not ^ ((h->security & SIGN) ? 1 : 0));
1178 case M_CRYPT_VERIFIED:
1181 return (pat->not ^ ((h->security & GOODSIGN) ? 1 : 0));
1182 case M_CRYPT_ENCRYPT:
1185 return (pat->not ^ ((h->security & ENCRYPT) ? 1 : 0));
1187 if (!(WithCrypto & APPLICATION_PGP))
1189 return (pat->not ^ ((h->security & APPLICATION_PGP) && (h->security & PGPKEY)));
1191 return (pat->not ^ (h->env->x_label && patmatch (pat, h->env->x_label) == 0));
1193 return (pat->not ^ (h->env->spam && h->env->spam->data && patmatch (pat, h->env->spam->data) == 0));
1195 return (pat->not ^ (h->thread && h->thread->duplicate_thread));
1198 int count = mutt_count_body_parts (ctx, h);
1199 return (pat->not ^ (count >= pat->min && (pat->max == M_MAXRANGE ||
1200 count <= pat->max)));
1202 case M_UNREFERENCED:
1203 return (pat->not ^ (h->thread && !h->thread->child));
1205 mutt_error (_("error: unknown op %d (report this error)."), pat->op);
1209 static void quote_simple(char *tmp, size_t len, const char *p)
1214 while (*p && i < len - 3)
1216 if (*p == '\\' || *p == '"')
1224 /* convert a simple search into a real request */
1225 void mutt_check_simple (char *s, size_t len, const char *simple)
1227 char tmp[LONG_STRING];
1231 for (p = s; p && *p; p++)
1233 if (*p == '\\' && *(p + 1))
1235 else if (*p == '~' || *p == '=' || *p == '%')
1242 /* XXX - is ascii_strcasecmp() right here, or should we use locale's
1246 if (do_simple) /* yup, so spoof a real request */
1248 /* convert old tokens into the new format */
1249 if (ascii_strcasecmp ("all", s) == 0 ||
1250 !mutt_strcmp ("^", s) || !mutt_strcmp (".", s)) /* ~A is more efficient */
1251 strfcpy (s, "~A", len);
1252 else if (ascii_strcasecmp ("del", s) == 0)
1253 strfcpy (s, "~D", len);
1254 else if (ascii_strcasecmp ("flag", s) == 0)
1255 strfcpy (s, "~F", len);
1256 else if (ascii_strcasecmp ("new", s) == 0)
1257 strfcpy (s, "~N", len);
1258 else if (ascii_strcasecmp ("old", s) == 0)
1259 strfcpy (s, "~O", len);
1260 else if (ascii_strcasecmp ("repl", s) == 0)
1261 strfcpy (s, "~Q", len);
1262 else if (ascii_strcasecmp ("read", s) == 0)
1263 strfcpy (s, "~R", len);
1264 else if (ascii_strcasecmp ("tag", s) == 0)
1265 strfcpy (s, "~T", len);
1266 else if (ascii_strcasecmp ("unread", s) == 0)
1267 strfcpy (s, "~U", len);
1270 quote_simple (tmp, sizeof(tmp), s);
1271 mutt_expand_fmt (s, len, simple, tmp);
1276 int mutt_pattern_func (int op, char *prompt)
1279 char buf[LONG_STRING] = "", *simple, error[STRING];
1282 progress_t progress;
1284 strfcpy (buf, NONULL (Context->pattern), sizeof (buf));
1285 if (mutt_get_field (prompt, buf, sizeof (buf), M_PATTERN | M_CLEAR) != 0 || !buf[0])
1288 mutt_message _("Compiling search pattern...");
1290 simple = safe_strdup (buf);
1291 mutt_check_simple (buf, sizeof (buf), NONULL (SimpleSearch));
1294 err.dsize = sizeof (error);
1295 if ((pat = mutt_pattern_comp (buf, M_FULL_MSG, &err)) == NULL)
1298 mutt_error ("%s", err.data);
1303 if (Context->magic == M_IMAP && imap_search (Context, pat) < 0)
1307 mutt_progress_init (&progress, _("Executing command on matching messages..."),
1308 M_PROGRESS_MSG, ReadInc,
1309 (op == M_LIMIT) ? Context->msgcount : Context->vcount);
1311 #define THIS_BODY Context->hdrs[i]->content
1315 Context->vcount = 0;
1317 Context->collapsed = 0;
1319 for (i = 0; i < Context->msgcount; i++)
1321 mutt_progress_update (&progress, i, -1);
1322 /* new limit pattern implicitly uncollapses all threads */
1323 Context->hdrs[i]->virtual = -1;
1324 Context->hdrs[i]->limited = 0;
1325 Context->hdrs[i]->collapsed = 0;
1326 Context->hdrs[i]->num_hidden = 0;
1327 if (mutt_pattern_exec (pat, M_MATCH_FULL_ADDRESS, Context, Context->hdrs[i]))
1329 Context->hdrs[i]->virtual = Context->vcount;
1330 Context->hdrs[i]->limited = 1;
1331 Context->v2r[Context->vcount] = i;
1333 Context->vsize+=THIS_BODY->length + THIS_BODY->offset -
1334 THIS_BODY->hdr_offset;
1340 for (i = 0; i < Context->vcount; i++)
1342 mutt_progress_update (&progress, i, -1);
1343 if (mutt_pattern_exec (pat, M_MATCH_FULL_ADDRESS, Context, Context->hdrs[Context->v2r[i]]))
1349 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_DELETE,
1354 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_TAG,
1364 mutt_clear_error ();
1368 /* drop previous limit pattern */
1369 FREE (&Context->pattern);
1370 if (Context->limit_pattern)
1371 mutt_pattern_free (&Context->limit_pattern);
1373 if (Context->msgcount && !Context->vcount)
1374 mutt_error _("No messages matched criteria.");
1376 /* record new limit pattern, unless match all */
1377 if (mutt_strcmp (buf, "~A") != 0)
1379 Context->pattern = simple;
1380 simple = NULL; /* don't clobber it */
1381 Context->limit_pattern = mutt_pattern_comp (buf, M_FULL_MSG, &err);
1385 mutt_pattern_free (&pat);
1389 int mutt_search_command (int cur, int op)
1393 char temp[LONG_STRING];
1398 progress_t progress;
1399 const char* msg = NULL;
1401 if (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE)
1403 strfcpy (buf, LastSearch, sizeof (buf));
1404 if (mutt_get_field ((op == OP_SEARCH) ? _("Search for: ") :
1405 _("Reverse search for: "), buf, sizeof (buf),
1406 M_CLEAR | M_PATTERN) != 0 || !buf[0])
1409 if (op == OP_SEARCH)
1410 unset_option (OPTSEARCHREVERSE);
1412 set_option (OPTSEARCHREVERSE);
1414 /* compare the *expanded* version of the search pattern in case
1415 $simple_search has changed while we were searching */
1416 strfcpy (temp, buf, sizeof (temp));
1417 mutt_check_simple (temp, sizeof (temp), NONULL (SimpleSearch));
1419 if (!SearchPattern || mutt_strcmp (temp, LastSearchExpn))
1421 set_option (OPTSEARCHINVALID);
1422 strfcpy (LastSearch, buf, sizeof (LastSearch));
1423 mutt_message _("Compiling search pattern...");
1424 mutt_pattern_free (&SearchPattern);
1426 err.dsize = sizeof (error);
1427 if ((SearchPattern = mutt_pattern_comp (temp, M_FULL_MSG, &err)) == NULL)
1429 mutt_error ("%s", error);
1432 mutt_clear_error ();
1435 else if (!SearchPattern)
1437 mutt_error _("No search pattern.");
1441 if (option (OPTSEARCHINVALID))
1443 for (i = 0; i < Context->msgcount; i++)
1444 Context->hdrs[i]->searched = 0;
1446 if (Context->magic == M_IMAP && imap_search (Context, SearchPattern) < 0)
1449 unset_option (OPTSEARCHINVALID);
1452 incr = (option (OPTSEARCHREVERSE)) ? -1 : 1;
1453 if (op == OP_SEARCH_OPPOSITE)
1456 mutt_progress_init (&progress, _("Searching..."), M_PROGRESS_MSG,
1457 ReadInc, Context->vcount);
1459 for (i = cur + incr, j = 0 ; j != Context->vcount; j++)
1461 mutt_progress_update (&progress, j, -1);
1462 if (i > Context->vcount - 1)
1465 if (option (OPTWRAPSEARCH))
1466 msg = _("Search wrapped to top.");
1469 mutt_message _("Search hit bottom without finding match");
1475 i = Context->vcount - 1;
1476 if (option (OPTWRAPSEARCH))
1477 msg = _("Search wrapped to bottom.");
1480 mutt_message _("Search hit top without finding match");
1485 h = Context->hdrs[Context->v2r[i]];
1488 /* if we've already evaulated this message, use the cached value */
1499 /* remember that we've already searched this message */
1501 if ((h->matched = (mutt_pattern_exec (SearchPattern, M_MATCH_FULL_ADDRESS, Context, h) > 0)))
1512 mutt_error _("Search interrupted.");
1520 mutt_error _("Not found.");