2 * Copyright (C) 1996-2000,2006 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);
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 int mutt_getvaluebychar (char ch, struct mapping_t *table)
125 for (i = 0; table[i].name; i++)
127 if (ch == table[i].name[0])
128 return table[i].value;
134 /* if no uppercase letters are given, do a case-insensitive search */
135 int mutt_which_case (const char *s)
141 memset (&mb, 0, sizeof (mb));
143 for (; (l = mbrtowc (&w, s, MB_CUR_MAX, &mb)) != 0; s += l)
145 if (l == (size_t) -2)
146 continue; /* shift sequences */
147 if (l == (size_t) -1)
148 return 0; /* error; assume case-sensitive */
149 if (iswalpha ((wint_t) w) && iswupper ((wint_t) w))
150 return 0; /* case-sensitive */
153 return REG_ICASE; /* case-insensitive */
157 msg_search (CONTEXT *ctx, pattern_t* pat, int msgno)
159 char tempfile[_POSIX_PATH_MAX];
166 HEADER *h = ctx->hdrs[msgno];
170 if ((msg = mx_open_message (ctx, msgno)) != NULL)
172 if (option (OPTTHOROUGHSRC))
174 /* decode the header / body */
175 memset (&s, 0, sizeof (s));
177 s.flags = M_CHARCONV;
178 mutt_mktemp (tempfile);
179 if ((s.fpout = safe_fopen (tempfile, "w+")) == NULL)
181 mutt_perror (tempfile);
185 if (pat->op != M_BODY)
186 mutt_copy_header (msg->fp, h, s.fpout, CH_FROM | CH_DECODE, NULL);
188 if (pat->op != M_HEADER)
190 mutt_parse_mime_message (ctx, h);
192 if (WithCrypto && (h->security & ENCRYPT)
193 && !crypt_valid_passphrase(h->security))
195 mx_close_message (&msg);
204 fseeko (msg->fp, h->offset, 0);
205 mutt_body_handler (h->content, &s);
211 fstat (fileno (fp), &st);
212 lng = (long) st.st_size;
216 /* raw header / body */
218 if (pat->op != M_BODY)
220 fseeko (fp, h->offset, 0);
221 lng = h->content->offset - h->offset;
223 if (pat->op != M_HEADER)
225 if (pat->op == M_BODY)
226 fseeko (fp, h->content->offset, 0);
227 lng += h->content->length;
232 buf = safe_malloc (blen);
234 /* search the file "fp" */
237 if (pat->op == M_HEADER)
239 if (*(buf = mutt_read_rfc822_line (fp, buf, &blen)) == '\0')
242 else if (fgets (buf, blen - 1, fp) == NULL)
243 break; /* don't loop forever */
244 if (patmatch (pat, buf) == 0)
249 lng -= mutt_strlen (buf);
254 mx_close_message (&msg);
256 if (option (OPTTHOROUGHSRC))
266 int eat_regexp (pattern_t *pat, BUFFER *s, BUFFER *err)
271 memset (&buf, 0, sizeof (buf));
272 if (mutt_extract_token (&buf, s, M_TOKEN_PATTERN | M_TOKEN_COMMENT) != 0 ||
275 snprintf (err->data, err->dsize, _("Error in expression: %s"), s->dptr);
280 snprintf (err->data, err->dsize, _("Empty expression"));
285 /* If there are no RE metacharacters, use simple search anyway */
286 if (!pat->stringmatch && !strpbrk (buf.data, "|[{.*+?^$"))
287 pat->stringmatch = 1;
290 if (pat->stringmatch)
292 pat->p.str = safe_strdup (buf.data);
295 else if (pat->groupmatch)
297 pat->p.g = mutt_pattern_group (buf.data);
302 pat->p.rx = safe_malloc (sizeof (regex_t));
303 r = REGCOMP (pat->p.rx, buf.data, REG_NEWLINE | REG_NOSUB | mutt_which_case (buf.data));
307 regerror (r, pat->p.rx, err->data, err->dsize);
317 int eat_range (pattern_t *pat, BUFFER *s, BUFFER *err)
320 int do_exclusive = 0;
324 * If simple_search is set to "~m %s", the range will have double quotes
334 if ((*s->dptr != '-') && (*s->dptr != '<'))
339 pat->max = M_MAXRANGE;
340 pat->min = strtol (s->dptr + 1, &tmp, 0) + 1; /* exclusive range */
343 pat->min = strtol (s->dptr, &tmp, 0);
344 if (toupper ((unsigned char) *tmp) == 'K') /* is there a prefix? */
349 else if (toupper ((unsigned char) *tmp) == 'M')
374 if (isdigit ((unsigned char) *tmp))
377 pat->max = strtol (tmp, &tmp, 0);
378 if (toupper ((unsigned char) *tmp) == 'K')
383 else if (toupper ((unsigned char) *tmp) == 'M')
392 pat->max = M_MAXRANGE;
394 if (skip_quote && *tmp == '"')
402 static const char *getDate (const char *s, struct tm *t, BUFFER *err)
405 time_t now = time (NULL);
406 struct tm *tm = localtime (&now);
408 t->tm_mday = strtol (s, &p, 10);
409 if (t->tm_mday < 1 || t->tm_mday > 31)
411 snprintf (err->data, err->dsize, _("Invalid day of month: %s"), s);
416 /* fill in today's month and year */
417 t->tm_mon = tm->tm_mon;
418 t->tm_year = tm->tm_year;
422 t->tm_mon = strtol (p, &p, 10) - 1;
423 if (t->tm_mon < 0 || t->tm_mon > 11)
425 snprintf (err->data, err->dsize, _("Invalid month: %s"), p);
430 t->tm_year = tm->tm_year;
434 t->tm_year = strtol (p, &p, 10);
435 if (t->tm_year < 70) /* year 2000+ */
437 else if (t->tm_year > 1900)
446 static const char *get_offset (struct tm *tm, const char *s, int sign)
449 int offset = strtol (s, &ps, 0);
450 if ((sign < 0 && offset > 0) || (sign > 0 && offset < 0))
456 tm->tm_year += offset;
459 tm->tm_mon += offset;
462 tm->tm_mday += 7 * offset;
465 tm->tm_mday += offset;
470 mutt_normalize_time (tm);
474 static void adjust_date_range (struct tm *min, struct tm *max)
476 if (min->tm_year > max->tm_year
477 || (min->tm_year == max->tm_year && min->tm_mon > max->tm_mon)
478 || (min->tm_year == max->tm_year && min->tm_mon == max->tm_mon
479 && min->tm_mday > max->tm_mday))
484 min->tm_year = max->tm_year;
488 min->tm_mon = max->tm_mon;
492 min->tm_mday = max->tm_mday;
495 min->tm_hour = min->tm_min = min->tm_sec = 0;
497 max->tm_min = max->tm_sec = 59;
501 static const char * parse_date_range (const char* pc, struct tm *min,
502 struct tm *max, int haveMin, struct tm *baseMin, BUFFER *err)
504 int flag = M_PDR_NONE;
505 while (*pc && ((flag & M_PDR_DONE) == 0))
514 /* try a range of absolute date minus offset of Ndwmy */
515 pt = get_offset (min, pc, -1);
518 if (flag == M_PDR_NONE)
519 { /* nothing yet and no offset parsed => absolute date? */
520 if (!getDate (pc, max, err))
521 flag |= (M_PDR_ABSOLUTE | M_PDR_ERRORDONE); /* done bad */
524 /* reestablish initial base minimum if not specified */
526 memcpy (min, baseMin, sizeof(struct tm));
527 flag |= (M_PDR_ABSOLUTE | M_PDR_DONE); /* done good */
531 flag |= M_PDR_ERRORDONE;
536 if (flag == M_PDR_NONE && !haveMin)
537 { /* the very first "-3d" without a previous absolute date */
538 max->tm_year = min->tm_year;
539 max->tm_mon = min->tm_mon;
540 max->tm_mday = min->tm_mday;
547 { /* enlarge plusRange */
548 pt = get_offset (max, pc, 1);
550 flag |= M_PDR_ERRORDONE;
559 { /* enlarge window in both directions */
560 pt = get_offset (min, pc, -1);
562 flag |= M_PDR_ERRORDONE;
565 pc = get_offset (max, pc, 1);
566 flag |= M_PDR_WINDOW;
571 flag |= M_PDR_ERRORDONE;
575 if ((flag & M_PDR_ERROR) && !(flag & M_PDR_ABSOLUTE))
576 { /* getDate has its own error message, don't overwrite it here */
577 snprintf (err->data, err->dsize, _("Invalid relative date: %s"), pc-1);
579 return ((flag & M_PDR_ERROR) ? NULL : pc);
582 static int eat_date (pattern_t *pat, BUFFER *s, BUFFER *err)
587 memset (&buffer, 0, sizeof (buffer));
588 if (mutt_extract_token (&buffer, s, M_TOKEN_COMMENT | M_TOKEN_PATTERN) != 0
591 strfcpy (err->data, _("error in expression"), err->dsize);
595 memset (&min, 0, sizeof (min));
596 /* the `0' time is Jan 1, 1970 UTC, so in order to prevent a negative time
597 when doing timezone conversion, we use Jan 2, 1970 UTC as the base
602 memset (&max, 0, sizeof (max));
604 /* Arbitrary year in the future. Don't set this too high
605 or mutt_mktime() returns something larger than will
606 fit in a time_t on some systems */
614 if (strchr ("<>=", buffer.data[0]))
616 /* offset from current time
617 <3d less than three days ago
618 >3d more than three days ago
619 =3d exactly three days ago */
620 time_t now = time (NULL);
621 struct tm *tm = localtime (&now);
624 if (buffer.data[0] == '<')
626 memcpy (&min, tm, sizeof (min));
631 memcpy (&max, tm, sizeof (max));
634 if (buffer.data[0] == '=')
638 tm->tm_min = tm->tm_sec = 59;
640 /* force negative offset */
641 get_offset (tm, buffer.data + 1, -1);
645 /* start at the beginning of the day in question */
646 memcpy (&min, &max, sizeof (max));
647 min.tm_hour = min.tm_sec = min.tm_min = 0;
652 const char *pc = buffer.data;
655 int untilNow = FALSE;
656 if (isdigit ((unsigned char)*pc))
658 /* mininum date specified */
659 if ((pc = getDate (pc, &min, err)) == NULL)
668 const char *pt = pc + 1;
670 untilNow = (*pt == '\0');
675 { /* max date or relative range/window */
680 { /* save base minimum and set current date, e.g. for "-3d+1d" */
681 time_t now = time (NULL);
682 struct tm *tm = localtime (&now);
683 memcpy (&baseMin, &min, sizeof(baseMin));
684 memcpy (&min, tm, sizeof (min));
685 min.tm_hour = min.tm_sec = min.tm_min = 0;
688 /* preset max date for relative offsets,
689 if nothing follows we search for messages on a specific day */
690 max.tm_year = min.tm_year;
691 max.tm_mon = min.tm_mon;
692 max.tm_mday = min.tm_mday;
694 if (!parse_date_range (pc, &min, &max, haveMin, &baseMin, err))
695 { /* bail out on any parsing error */
702 /* Since we allow two dates to be specified we'll have to adjust that. */
703 adjust_date_range (&min, &max);
705 pat->min = mutt_mktime (&min, 1);
706 pat->max = mutt_mktime (&max, 1);
713 static int patmatch (const pattern_t* pat, const char* buf)
715 if (pat->stringmatch)
716 return !strstr (buf, pat->p.str);
717 else if (pat->groupmatch)
718 return !mutt_group_match (pat->p.g, buf);
720 return regexec (pat->p.rx, buf, 0, NULL, 0);
723 static struct pattern_flags *lookup_tag (char tag)
727 for (i = 0; Flags[i].tag; i++)
728 if (Flags[i].tag == tag)
733 static /* const */ char *find_matching_paren (/* const */ char *s)
751 void mutt_pattern_free (pattern_t **pat)
760 if (tmp->stringmatch)
762 else if (tmp->groupmatch)
771 mutt_pattern_free (&tmp->child);
776 pattern_t *mutt_pattern_comp (/* const */ char *s, int flags, BUFFER *err)
778 pattern_t *curlist = NULL;
779 pattern_t *tmp, *tmp2;
780 pattern_t *last = NULL;
784 int implicit = 1; /* used to detect logical AND operator */
785 struct pattern_flags *entry;
790 memset (&ps, 0, sizeof (ps));
792 ps.dsize = mutt_strlen (s);
812 snprintf (err->data, err->dsize, _("error in pattern at: %s"), ps.dptr);
817 /* A & B | C == (A & B) | C */
818 tmp = new_pattern ();
820 tmp->child = curlist;
836 if (*(ps.dptr + 1) == '(')
838 ps.dptr ++; /* skip ~ */
839 p = find_matching_paren (ps.dptr + 1);
842 snprintf (err->data, err->dsize, _("mismatched brackets: %s"), ps.dptr);
843 mutt_pattern_free (&curlist);
846 tmp = new_pattern ();
854 tmp->alladdr |= alladdr;
857 /* compile the sub-expression */
858 buf = mutt_substrdup (ps.dptr + 1, p);
859 if ((tmp2 = mutt_pattern_comp (buf, flags, err)) == NULL)
862 mutt_pattern_free (&curlist);
867 ps.dptr = p + 1; /* restore location */
872 /* A | B & C == (A | B) & C */
873 tmp = new_pattern ();
875 tmp->child = curlist;
881 tmp = new_pattern ();
883 tmp->alladdr = alladdr;
884 tmp->stringmatch = (*ps.dptr == '=') ? 1 : 0;
885 tmp->groupmatch = (*ps.dptr == '%') ? 1 : 0;
895 ps.dptr++; /* move past the ~ */
896 if ((entry = lookup_tag (*ps.dptr)) == NULL)
898 snprintf (err->data, err->dsize, _("%c: invalid pattern modifier"), *ps.dptr);
899 mutt_pattern_free (&curlist);
902 if (entry->class && (flags & entry->class) == 0)
904 snprintf (err->data, err->dsize, _("%c: not supported in this mode"), *ps.dptr);
905 mutt_pattern_free (&curlist);
910 ps.dptr++; /* eat the operator and any optional whitespace */
917 snprintf (err->data, err->dsize, _("missing parameter"));
918 mutt_pattern_free (&curlist);
921 if (entry->eat_arg (tmp, &ps, err) == -1)
923 mutt_pattern_free (&curlist);
930 p = find_matching_paren (ps.dptr + 1);
933 snprintf (err->data, err->dsize, _("mismatched parenthesis: %s"), ps.dptr);
934 mutt_pattern_free (&curlist);
937 /* compile the sub-expression */
938 buf = mutt_substrdup (ps.dptr + 1, p);
939 if ((tmp = mutt_pattern_comp (buf, flags, err)) == NULL)
942 mutt_pattern_free (&curlist);
952 tmp->alladdr |= alladdr;
955 ps.dptr = p + 1; /* restore location */
958 snprintf (err->data, err->dsize, _("error in pattern at: %s"), ps.dptr);
959 mutt_pattern_free (&curlist);
965 strfcpy (err->data, _("empty pattern"), err->dsize);
970 tmp = new_pattern ();
971 tmp->op = or ? M_OR : M_AND;
972 tmp->child = curlist;
979 perform_and (pattern_t *pat, pattern_exec_flag flags, CONTEXT *ctx, HEADER *hdr)
981 for (; pat; pat = pat->next)
982 if (mutt_pattern_exec (pat, flags, ctx, hdr) <= 0)
988 perform_or (struct pattern_t *pat, pattern_exec_flag flags, CONTEXT *ctx, HEADER *hdr)
990 for (; pat; pat = pat->next)
991 if (mutt_pattern_exec (pat, flags, ctx, hdr) > 0)
996 static int match_adrlist (pattern_t *pat, int match_personal, int n, ...)
1004 for (a = va_arg (ap, ADDRESS *) ; a ; a = a->next)
1006 if (pat->alladdr ^ ((a->mailbox && patmatch (pat, a->mailbox) == 0) ||
1007 (match_personal && a->personal && patmatch (pat, a->personal) == 0)))
1010 return (! pat->alladdr); /* Found match, or non-match if alladdr */
1015 return pat->alladdr; /* No matches, or all matches if alladdr */
1018 static int match_reference (pattern_t *pat, LIST *refs)
1020 for (; refs; refs = refs->next)
1021 if (patmatch (pat, refs->data) == 0)
1027 * Matches subscribed mailing lists
1029 int mutt_is_list_recipient (int alladdr, ADDRESS *a1, ADDRESS *a2)
1031 for (; a1 ; a1 = a1->next)
1032 if (alladdr ^ mutt_is_subscribed_list (a1))
1034 for (; a2 ; a2 = a2->next)
1035 if (alladdr ^ mutt_is_subscribed_list (a2))
1041 * Matches known mailing lists
1042 * The function name may seem a little bit misleading: It checks all
1043 * recipients in To and Cc for known mailing lists, subscribed or not.
1045 int mutt_is_list_cc (int alladdr, ADDRESS *a1, ADDRESS *a2)
1047 for (; a1 ; a1 = a1->next)
1048 if (alladdr ^ mutt_is_mail_list (a1))
1050 for (; a2 ; a2 = a2->next)
1051 if (alladdr ^ mutt_is_mail_list (a2))
1056 static int match_user (int alladdr, ADDRESS *a1, ADDRESS *a2)
1058 for (; a1 ; a1 = a1->next)
1059 if (alladdr ^ mutt_addr_is_user (a1))
1061 for (; a2 ; a2 = a2->next)
1062 if (alladdr ^ mutt_addr_is_user (a2))
1067 static int match_threadcomplete(struct pattern_t *pat, pattern_exec_flag flags, CONTEXT *ctx, THREAD *t,int left,int up,int right,int down)
1076 if(mutt_pattern_exec(pat, flags, ctx, h))
1079 if(up && (a=match_threadcomplete(pat, flags, ctx, t->parent,1,1,1,0)))
1081 if(right && t->parent && (a=match_threadcomplete(pat, flags, ctx, t->next,0,0,1,1)))
1083 if(left && t->parent && (a=match_threadcomplete(pat, flags, ctx, t->prev,1,0,0,1)))
1085 if(down && (a=match_threadcomplete(pat, flags, ctx, t->child,1,0,1,1)))
1091 M_MATCH_FULL_ADDRESS match both personal and machine address */
1093 mutt_pattern_exec (struct pattern_t *pat, pattern_exec_flag flags, CONTEXT *ctx, HEADER *h)
1098 return (pat->not ^ (perform_and (pat->child, flags, ctx, h) > 0));
1100 return (pat->not ^ (perform_or (pat->child, flags, ctx, h) > 0));
1102 return (pat->not ^ match_threadcomplete(pat->child, flags, ctx, h->thread, 1, 1, 1, 1));
1106 return (pat->not ^ h->expired);
1108 return (pat->not ^ h->superseded);
1110 return (pat->not ^ h->flagged);
1112 return (pat->not ^ h->tagged);
1114 return (pat->not ? h->old || h->read : !(h->old || h->read));
1116 return (pat->not ? h->read : !h->read);
1118 return (pat->not ^ h->replied);
1120 return (pat->not ? (!h->old || h->read) : (h->old && !h->read));
1122 return (pat->not ^ h->read);
1124 return (pat->not ^ h->deleted);
1126 return (pat->not ^ (h->msgno >= pat->min - 1 && (pat->max == M_MAXRANGE ||
1127 h->msgno <= pat->max - 1)));
1129 return (pat->not ^ (h->date_sent >= pat->min && h->date_sent <= pat->max));
1130 case M_DATE_RECEIVED:
1131 return (pat->not ^ (h->received >= pat->min && h->received <= pat->max));
1136 * ctx can be NULL in certain cases, such as when replying to a message from the attachment menu and
1137 * the user has a reply-hook using "~h" (bug #2190).
1142 /* IMAP search sets h->matched at search compile time */
1143 if (ctx->magic == M_IMAP && pat->stringmatch)
1144 return (h->matched);
1146 return (pat->not ^ msg_search (ctx, pat, h->msgno));
1148 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS, 1,
1151 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS, 1,
1154 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS, 1,
1157 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS, 1,
1160 return (pat->not ^ (h->env->subject && patmatch (pat, h->env->subject) == 0));
1162 return (pat->not ^ (h->env->message_id && patmatch (pat, h->env->message_id) == 0));
1164 return (pat->not ^ (h->score >= pat->min && (pat->max == M_MAXRANGE ||
1165 h->score <= pat->max)));
1167 return (pat->not ^ (h->content->length >= pat->min && (pat->max == M_MAXRANGE || h->content->length <= pat->max)));
1169 return (pat->not ^ match_reference (pat, h->env->references));
1171 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS, 4,
1172 h->env->from, h->env->sender,
1173 h->env->to, h->env->cc));
1175 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
1176 2, h->env->to, h->env->cc));
1177 case M_LIST: /* known list, subscribed or not */
1178 return (pat->not ^ mutt_is_list_cc (pat->alladdr, h->env->to, h->env->cc));
1179 case M_SUBSCRIBED_LIST:
1180 return (pat->not ^ mutt_is_list_recipient (pat->alladdr, h->env->to, h->env->cc));
1181 case M_PERSONAL_RECIP:
1182 return (pat->not ^ match_user (pat->alladdr, h->env->to, h->env->cc));
1183 case M_PERSONAL_FROM:
1184 return (pat->not ^ match_user (pat->alladdr, h->env->from, NULL));
1186 return (pat->not ^ (h->collapsed && h->num_hidden > 1));
1190 return (pat->not ^ ((h->security & SIGN) ? 1 : 0));
1191 case M_CRYPT_VERIFIED:
1194 return (pat->not ^ ((h->security & GOODSIGN) ? 1 : 0));
1195 case M_CRYPT_ENCRYPT:
1198 return (pat->not ^ ((h->security & ENCRYPT) ? 1 : 0));
1200 if (!(WithCrypto & APPLICATION_PGP))
1202 return (pat->not ^ ((h->security & APPLICATION_PGP) && (h->security & PGPKEY)));
1204 return (pat->not ^ (h->env->x_label && patmatch (pat, h->env->x_label) == 0));
1206 return (pat->not ^ (h->env->spam && h->env->spam->data && patmatch (pat, h->env->spam->data) == 0));
1208 return (pat->not ^ (h->thread && h->thread->duplicate_thread));
1211 int count = mutt_count_body_parts (ctx, h);
1212 return (pat->not ^ (count >= pat->min && (pat->max == M_MAXRANGE ||
1213 count <= pat->max)));
1215 case M_UNREFERENCED:
1216 return (pat->not ^ (h->thread && !h->thread->child));
1218 mutt_error (_("error: unknown op %d (report this error)."), pat->op);
1222 static void quote_simple(char *tmp, size_t len, const char *p)
1227 while (*p && i < len - 3)
1229 if (*p == '\\' || *p == '"')
1237 /* convert a simple search into a real request */
1238 void mutt_check_simple (char *s, size_t len, const char *simple)
1240 char tmp[LONG_STRING];
1244 for (p = s; p && *p; p++)
1246 if (*p == '\\' && *(p + 1))
1248 else if (*p == '~' || *p == '=' || *p == '%')
1255 /* XXX - is ascii_strcasecmp() right here, or should we use locale's
1259 if (do_simple) /* yup, so spoof a real request */
1261 /* convert old tokens into the new format */
1262 if (ascii_strcasecmp ("all", s) == 0 ||
1263 !mutt_strcmp ("^", s) || !mutt_strcmp (".", s)) /* ~A is more efficient */
1264 strfcpy (s, "~A", len);
1265 else if (ascii_strcasecmp ("del", s) == 0)
1266 strfcpy (s, "~D", len);
1267 else if (ascii_strcasecmp ("flag", s) == 0)
1268 strfcpy (s, "~F", len);
1269 else if (ascii_strcasecmp ("new", s) == 0)
1270 strfcpy (s, "~N", len);
1271 else if (ascii_strcasecmp ("old", s) == 0)
1272 strfcpy (s, "~O", len);
1273 else if (ascii_strcasecmp ("repl", s) == 0)
1274 strfcpy (s, "~Q", len);
1275 else if (ascii_strcasecmp ("read", s) == 0)
1276 strfcpy (s, "~R", len);
1277 else if (ascii_strcasecmp ("tag", s) == 0)
1278 strfcpy (s, "~T", len);
1279 else if (ascii_strcasecmp ("unread", s) == 0)
1280 strfcpy (s, "~U", len);
1283 quote_simple (tmp, sizeof(tmp), s);
1284 mutt_expand_fmt (s, len, simple, tmp);
1289 int mutt_pattern_func (int op, char *prompt)
1292 char buf[LONG_STRING] = "", *simple, error[STRING];
1295 progress_t progress;
1297 strfcpy (buf, NONULL (Context->pattern), sizeof (buf));
1298 if (mutt_get_field (prompt, buf, sizeof (buf), M_PATTERN | M_CLEAR) != 0 || !buf[0])
1301 mutt_message _("Compiling search pattern...");
1303 simple = safe_strdup (buf);
1304 mutt_check_simple (buf, sizeof (buf), NONULL (SimpleSearch));
1307 err.dsize = sizeof (error);
1308 if ((pat = mutt_pattern_comp (buf, M_FULL_MSG, &err)) == NULL)
1311 mutt_error ("%s", err.data);
1316 if (Context->magic == M_IMAP && imap_search (Context, pat) < 0)
1320 mutt_progress_init (&progress, _("Executing command on matching messages..."),
1321 M_PROGRESS_MSG, ReadInc,
1322 (op == M_LIMIT) ? Context->msgcount : Context->vcount);
1324 #define THIS_BODY Context->hdrs[i]->content
1328 Context->vcount = 0;
1330 Context->collapsed = 0;
1332 for (i = 0; i < Context->msgcount; i++)
1334 mutt_progress_update (&progress, i, -1);
1335 /* new limit pattern implicitly uncollapses all threads */
1336 Context->hdrs[i]->virtual = -1;
1337 Context->hdrs[i]->limited = 0;
1338 Context->hdrs[i]->collapsed = 0;
1339 Context->hdrs[i]->num_hidden = 0;
1340 if (mutt_pattern_exec (pat, M_MATCH_FULL_ADDRESS, Context, Context->hdrs[i]))
1342 Context->hdrs[i]->virtual = Context->vcount;
1343 Context->hdrs[i]->limited = 1;
1344 Context->v2r[Context->vcount] = i;
1346 Context->vsize+=THIS_BODY->length + THIS_BODY->offset -
1347 THIS_BODY->hdr_offset;
1353 for (i = 0; i < Context->vcount; i++)
1355 mutt_progress_update (&progress, i, -1);
1356 if (mutt_pattern_exec (pat, M_MATCH_FULL_ADDRESS, Context, Context->hdrs[Context->v2r[i]]))
1362 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_DELETE,
1367 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_TAG,
1377 mutt_clear_error ();
1381 /* drop previous limit pattern */
1382 FREE (&Context->pattern);
1383 if (Context->limit_pattern)
1384 mutt_pattern_free (&Context->limit_pattern);
1386 if (Context->msgcount && !Context->vcount)
1387 mutt_error _("No messages matched criteria.");
1389 /* record new limit pattern, unless match all */
1390 if (mutt_strcmp (buf, "~A") != 0)
1392 Context->pattern = simple;
1393 simple = NULL; /* don't clobber it */
1394 Context->limit_pattern = mutt_pattern_comp (buf, M_FULL_MSG, &err);
1398 mutt_pattern_free (&pat);
1402 int mutt_search_command (int cur, int op)
1406 char temp[LONG_STRING];
1411 progress_t progress;
1412 const char* msg = NULL;
1414 if (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE)
1416 strfcpy (buf, LastSearch, sizeof (buf));
1417 if (mutt_get_field ((op == OP_SEARCH) ? _("Search for: ") :
1418 _("Reverse search for: "), buf, sizeof (buf),
1419 M_CLEAR | M_PATTERN) != 0 || !buf[0])
1422 if (op == OP_SEARCH)
1423 unset_option (OPTSEARCHREVERSE);
1425 set_option (OPTSEARCHREVERSE);
1427 /* compare the *expanded* version of the search pattern in case
1428 $simple_search has changed while we were searching */
1429 strfcpy (temp, buf, sizeof (temp));
1430 mutt_check_simple (temp, sizeof (temp), NONULL (SimpleSearch));
1432 if (!SearchPattern || mutt_strcmp (temp, LastSearchExpn))
1434 set_option (OPTSEARCHINVALID);
1435 strfcpy (LastSearch, buf, sizeof (LastSearch));
1436 mutt_message _("Compiling search pattern...");
1437 mutt_pattern_free (&SearchPattern);
1439 err.dsize = sizeof (error);
1440 if ((SearchPattern = mutt_pattern_comp (temp, M_FULL_MSG, &err)) == NULL)
1442 mutt_error ("%s", error);
1445 mutt_clear_error ();
1448 else if (!SearchPattern)
1450 mutt_error _("No search pattern.");
1454 if (option (OPTSEARCHINVALID))
1456 for (i = 0; i < Context->msgcount; i++)
1457 Context->hdrs[i]->searched = 0;
1459 if (Context->magic == M_IMAP && imap_search (Context, SearchPattern) < 0)
1462 unset_option (OPTSEARCHINVALID);
1465 incr = (option (OPTSEARCHREVERSE)) ? -1 : 1;
1466 if (op == OP_SEARCH_OPPOSITE)
1469 mutt_progress_init (&progress, _("Searching..."), M_PROGRESS_MSG,
1470 ReadInc, Context->vcount);
1472 for (i = cur + incr, j = 0 ; j != Context->vcount; j++)
1474 mutt_progress_update (&progress, j, -1);
1475 if (i > Context->vcount - 1)
1478 if (option (OPTWRAPSEARCH))
1479 msg = _("Search wrapped to top.");
1482 mutt_message _("Search hit bottom without finding match");
1488 i = Context->vcount - 1;
1489 if (option (OPTWRAPSEARCH))
1490 msg = _("Search wrapped to bottom.");
1493 mutt_message _("Search hit top without finding match");
1498 h = Context->hdrs[Context->v2r[i]];
1501 /* if we've already evaulated this message, use the cached value */
1512 /* remember that we've already searched this message */
1514 if ((h->matched = (mutt_pattern_exec (SearchPattern, M_MATCH_FULL_ADDRESS, Context, h) > 0)))
1525 mutt_error _("Search interrupted.");
1533 mutt_error _("Not found.");