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"
42 #include "imap/imap.h"
45 static int eat_regexp (pattern_t *pat, BUFFER *, BUFFER *);
46 static int eat_date (pattern_t *pat, BUFFER *, BUFFER *);
47 static int eat_range (pattern_t *pat, BUFFER *, BUFFER *);
48 static int patmatch (const pattern_t *pat, const char *buf);
50 static struct pattern_flags
52 int tag; /* character used to represent this op */
53 int op; /* operation to perform */
55 int (*eat_arg) (pattern_t *, BUFFER *, BUFFER *);
59 { 'A', M_ALL, 0, NULL },
60 { 'b', M_BODY, M_FULL_MSG, eat_regexp },
61 { 'B', M_WHOLE_MSG, M_FULL_MSG, eat_regexp },
62 { 'c', M_CC, 0, eat_regexp },
63 { 'C', M_RECIPIENT, 0, eat_regexp },
64 { 'd', M_DATE, 0, eat_date },
65 { 'D', M_DELETED, 0, NULL },
66 { 'e', M_SENDER, 0, eat_regexp },
67 { 'E', M_EXPIRED, 0, NULL },
68 { 'f', M_FROM, 0, eat_regexp },
69 { 'F', M_FLAG, 0, NULL },
70 { 'g', M_CRYPT_SIGN, 0, NULL },
71 { 'G', M_CRYPT_ENCRYPT, 0, NULL },
72 { 'h', M_HEADER, M_FULL_MSG, eat_regexp },
73 { 'H', M_HORMEL, 0, eat_regexp },
74 { 'i', M_ID, 0, eat_regexp },
75 { 'k', M_PGP_KEY, 0, NULL },
76 { 'l', M_LIST, 0, NULL },
77 { 'L', M_ADDRESS, 0, eat_regexp },
78 { 'm', M_MESSAGE, 0, eat_range },
79 { 'n', M_SCORE, 0, eat_range },
80 { 'N', M_NEW, 0, NULL },
81 { 'O', M_OLD, 0, NULL },
82 { 'p', M_PERSONAL_RECIP, 0, NULL },
83 { 'P', M_PERSONAL_FROM, 0, NULL },
84 { 'Q', M_REPLIED, 0, NULL },
85 { 'r', M_DATE_RECEIVED, 0, eat_date },
86 { 'R', M_READ, 0, NULL },
87 { 's', M_SUBJECT, 0, eat_regexp },
88 { 'S', M_SUPERSEDED, 0, NULL },
89 { 't', M_TO, 0, eat_regexp },
90 { 'T', M_TAG, 0, NULL },
91 { 'u', M_SUBSCRIBED_LIST, 0, NULL },
92 { 'U', M_UNREAD, 0, NULL },
93 { 'v', M_COLLAPSED, 0, NULL },
94 { 'V', M_CRYPT_VERIFIED, 0, NULL },
95 { 'x', M_REFERENCE, 0, eat_regexp },
96 { 'X', M_MIMEATTACH, 0, eat_range },
97 { 'y', M_XLABEL, 0, eat_regexp },
98 { 'z', M_SIZE, 0, eat_range },
99 { '=', M_DUPLICATED, 0, NULL },
100 { '$', M_UNREFERENCED, 0, NULL },
104 static pattern_t *SearchPattern = NULL; /* current search pattern */
105 static char LastSearch[STRING] = { 0 }; /* last pattern searched for */
106 static char LastSearchExpn[LONG_STRING] = { 0 }; /* expanded version of
109 #define M_MAXRANGE -1
111 /* constants for parse_date_range() */
112 #define M_PDR_NONE 0x0000
113 #define M_PDR_MINUS 0x0001
114 #define M_PDR_PLUS 0x0002
115 #define M_PDR_WINDOW 0x0004
116 #define M_PDR_ABSOLUTE 0x0008
117 #define M_PDR_DONE 0x0010
118 #define M_PDR_ERROR 0x0100
119 #define M_PDR_ERRORDONE (M_PDR_ERROR | M_PDR_DONE)
122 /* if no uppercase letters are given, do a case-insensitive search */
123 int mutt_which_case (const char *s)
129 memset (&mb, 0, sizeof (mb));
131 for (; (l = mbrtowc (&w, s, MB_CUR_MAX, &mb)) != 0; s += l)
133 if (l == (size_t) -2)
134 continue; /* shift sequences */
135 if (l == (size_t) -1)
136 return 0; /* error; assume case-sensitive */
137 if (iswalpha ((wint_t) w) && iswupper ((wint_t) w))
138 return 0; /* case-sensitive */
141 return REG_ICASE; /* case-insensitive */
145 msg_search (CONTEXT *ctx, pattern_t* pat, int msgno)
147 char tempfile[_POSIX_PATH_MAX];
154 HEADER *h = ctx->hdrs[msgno];
158 if ((msg = mx_open_message (ctx, msgno)) != NULL)
160 if (option (OPTTHOROUGHSRC))
162 /* decode the header / body */
163 memset (&s, 0, sizeof (s));
165 s.flags = M_CHARCONV;
166 mutt_mktemp (tempfile, sizeof (tempfile));
167 if ((s.fpout = safe_fopen (tempfile, "w+")) == NULL)
169 mutt_perror (tempfile);
173 if (pat->op != M_BODY)
174 mutt_copy_header (msg->fp, h, s.fpout, CH_FROM | CH_DECODE, NULL);
176 if (pat->op != M_HEADER)
178 mutt_parse_mime_message (ctx, h);
180 if (WithCrypto && (h->security & ENCRYPT)
181 && !crypt_valid_passphrase(h->security))
183 mx_close_message (&msg);
186 safe_fclose (&s.fpout);
192 fseeko (msg->fp, h->offset, 0);
193 mutt_body_handler (h->content, &s);
199 fstat (fileno (fp), &st);
200 lng = (long) st.st_size;
204 /* raw header / body */
206 if (pat->op != M_BODY)
208 fseeko (fp, h->offset, 0);
209 lng = h->content->offset - h->offset;
211 if (pat->op != M_HEADER)
213 if (pat->op == M_BODY)
214 fseeko (fp, h->content->offset, 0);
215 lng += h->content->length;
220 buf = safe_malloc (blen);
222 /* search the file "fp" */
225 if (pat->op == M_HEADER)
227 if (*(buf = mutt_read_rfc822_line (fp, buf, &blen)) == '\0')
230 else if (fgets (buf, blen - 1, fp) == NULL)
231 break; /* don't loop forever */
232 if (patmatch (pat, buf) == 0)
237 lng -= mutt_strlen (buf);
242 mx_close_message (&msg);
244 if (option (OPTTHOROUGHSRC))
254 static int eat_regexp (pattern_t *pat, BUFFER *s, BUFFER *err)
260 memset (&buf, 0, sizeof (buf));
261 if (mutt_extract_token (&buf, s, M_TOKEN_PATTERN | M_TOKEN_COMMENT) != 0 ||
264 snprintf (err->data, err->dsize, _("Error in expression: %s"), s->dptr);
269 snprintf (err->data, err->dsize, _("Empty expression"));
274 /* If there are no RE metacharacters, use simple search anyway */
275 if (!pat->stringmatch && !strpbrk (buf.data, "|[{.*+?^$"))
276 pat->stringmatch = 1;
279 if (pat->stringmatch)
281 pat->p.str = safe_strdup (buf.data);
282 pat->ign_case = mutt_which_case (buf.data) == REG_ICASE;
285 else if (pat->groupmatch)
287 pat->p.g = mutt_pattern_group (buf.data);
292 pat->p.rx = safe_malloc (sizeof (regex_t));
293 r = REGCOMP (pat->p.rx, buf.data, REG_NEWLINE | REG_NOSUB | mutt_which_case (buf.data));
296 regerror (r, pat->p.rx, errmsg, sizeof (errmsg));
297 mutt_buffer_printf (err, "'%s': %s", buf.data, errmsg);
308 int eat_range (pattern_t *pat, BUFFER *s, BUFFER *err)
311 int do_exclusive = 0;
315 * If simple_search is set to "~m %s", the range will have double quotes
325 if ((*s->dptr != '-') && (*s->dptr != '<'))
330 pat->max = M_MAXRANGE;
331 pat->min = strtol (s->dptr + 1, &tmp, 0) + 1; /* exclusive range */
334 pat->min = strtol (s->dptr, &tmp, 0);
335 if (toupper ((unsigned char) *tmp) == 'K') /* is there a prefix? */
340 else if (toupper ((unsigned char) *tmp) == 'M')
365 if (isdigit ((unsigned char) *tmp))
368 pat->max = strtol (tmp, &tmp, 0);
369 if (toupper ((unsigned char) *tmp) == 'K')
374 else if (toupper ((unsigned char) *tmp) == 'M')
383 pat->max = M_MAXRANGE;
385 if (skip_quote && *tmp == '"')
393 static const char *getDate (const char *s, struct tm *t, BUFFER *err)
396 time_t now = time (NULL);
397 struct tm *tm = localtime (&now);
399 t->tm_mday = strtol (s, &p, 10);
400 if (t->tm_mday < 1 || t->tm_mday > 31)
402 snprintf (err->data, err->dsize, _("Invalid day of month: %s"), s);
407 /* fill in today's month and year */
408 t->tm_mon = tm->tm_mon;
409 t->tm_year = tm->tm_year;
413 t->tm_mon = strtol (p, &p, 10) - 1;
414 if (t->tm_mon < 0 || t->tm_mon > 11)
416 snprintf (err->data, err->dsize, _("Invalid month: %s"), p);
421 t->tm_year = tm->tm_year;
425 t->tm_year = strtol (p, &p, 10);
426 if (t->tm_year < 70) /* year 2000+ */
428 else if (t->tm_year > 1900)
437 static const char *get_offset (struct tm *tm, const char *s, int sign)
440 int offset = strtol (s, &ps, 0);
441 if ((sign < 0 && offset > 0) || (sign > 0 && offset < 0))
447 tm->tm_year += offset;
450 tm->tm_mon += offset;
453 tm->tm_mday += 7 * offset;
456 tm->tm_mday += offset;
461 mutt_normalize_time (tm);
465 static void adjust_date_range (struct tm *min, struct tm *max)
467 if (min->tm_year > max->tm_year
468 || (min->tm_year == max->tm_year && min->tm_mon > max->tm_mon)
469 || (min->tm_year == max->tm_year && min->tm_mon == max->tm_mon
470 && min->tm_mday > max->tm_mday))
475 min->tm_year = max->tm_year;
479 min->tm_mon = max->tm_mon;
483 min->tm_mday = max->tm_mday;
486 min->tm_hour = min->tm_min = min->tm_sec = 0;
488 max->tm_min = max->tm_sec = 59;
492 static const char * parse_date_range (const char* pc, struct tm *min,
493 struct tm *max, int haveMin, struct tm *baseMin, BUFFER *err)
495 int flag = M_PDR_NONE;
496 while (*pc && ((flag & M_PDR_DONE) == 0))
505 /* try a range of absolute date minus offset of Ndwmy */
506 pt = get_offset (min, pc, -1);
509 if (flag == M_PDR_NONE)
510 { /* nothing yet and no offset parsed => absolute date? */
511 if (!getDate (pc, max, err))
512 flag |= (M_PDR_ABSOLUTE | M_PDR_ERRORDONE); /* done bad */
515 /* reestablish initial base minimum if not specified */
517 memcpy (min, baseMin, sizeof(struct tm));
518 flag |= (M_PDR_ABSOLUTE | M_PDR_DONE); /* done good */
522 flag |= M_PDR_ERRORDONE;
527 if (flag == M_PDR_NONE && !haveMin)
528 { /* the very first "-3d" without a previous absolute date */
529 max->tm_year = min->tm_year;
530 max->tm_mon = min->tm_mon;
531 max->tm_mday = min->tm_mday;
538 { /* enlarge plusRange */
539 pt = get_offset (max, pc, 1);
541 flag |= M_PDR_ERRORDONE;
550 { /* enlarge window in both directions */
551 pt = get_offset (min, pc, -1);
553 flag |= M_PDR_ERRORDONE;
556 pc = get_offset (max, pc, 1);
557 flag |= M_PDR_WINDOW;
562 flag |= M_PDR_ERRORDONE;
566 if ((flag & M_PDR_ERROR) && !(flag & M_PDR_ABSOLUTE))
567 { /* getDate has its own error message, don't overwrite it here */
568 snprintf (err->data, err->dsize, _("Invalid relative date: %s"), pc-1);
570 return ((flag & M_PDR_ERROR) ? NULL : pc);
573 static int eat_date (pattern_t *pat, BUFFER *s, BUFFER *err)
578 memset (&buffer, 0, sizeof (buffer));
579 if (mutt_extract_token (&buffer, s, M_TOKEN_COMMENT | M_TOKEN_PATTERN) != 0
582 strfcpy (err->data, _("error in expression"), err->dsize);
586 memset (&min, 0, sizeof (min));
587 /* the `0' time is Jan 1, 1970 UTC, so in order to prevent a negative time
588 when doing timezone conversion, we use Jan 2, 1970 UTC as the base
593 memset (&max, 0, sizeof (max));
595 /* Arbitrary year in the future. Don't set this too high
596 or mutt_mktime() returns something larger than will
597 fit in a time_t on some systems */
605 if (strchr ("<>=", buffer.data[0]))
607 /* offset from current time
608 <3d less than three days ago
609 >3d more than three days ago
610 =3d exactly three days ago */
611 time_t now = time (NULL);
612 struct tm *tm = localtime (&now);
615 if (buffer.data[0] == '<')
617 memcpy (&min, tm, sizeof (min));
622 memcpy (&max, tm, sizeof (max));
625 if (buffer.data[0] == '=')
629 tm->tm_min = tm->tm_sec = 59;
631 /* force negative offset */
632 get_offset (tm, buffer.data + 1, -1);
636 /* start at the beginning of the day in question */
637 memcpy (&min, &max, sizeof (max));
638 min.tm_hour = min.tm_sec = min.tm_min = 0;
643 const char *pc = buffer.data;
646 int untilNow = FALSE;
647 if (isdigit ((unsigned char)*pc))
649 /* mininum date specified */
650 if ((pc = getDate (pc, &min, err)) == NULL)
659 const char *pt = pc + 1;
661 untilNow = (*pt == '\0');
666 { /* max date or relative range/window */
671 { /* save base minimum and set current date, e.g. for "-3d+1d" */
672 time_t now = time (NULL);
673 struct tm *tm = localtime (&now);
674 memcpy (&baseMin, &min, sizeof(baseMin));
675 memcpy (&min, tm, sizeof (min));
676 min.tm_hour = min.tm_sec = min.tm_min = 0;
679 /* preset max date for relative offsets,
680 if nothing follows we search for messages on a specific day */
681 max.tm_year = min.tm_year;
682 max.tm_mon = min.tm_mon;
683 max.tm_mday = min.tm_mday;
685 if (!parse_date_range (pc, &min, &max, haveMin, &baseMin, err))
686 { /* bail out on any parsing error */
693 /* Since we allow two dates to be specified we'll have to adjust that. */
694 adjust_date_range (&min, &max);
696 pat->min = mutt_mktime (&min, 1);
697 pat->max = mutt_mktime (&max, 1);
704 static int patmatch (const pattern_t* pat, const char* buf)
706 if (pat->stringmatch)
707 return pat->ign_case ? !strcasestr (buf, pat->p.str) :
708 !strstr (buf, pat->p.str);
709 else if (pat->groupmatch)
710 return !mutt_group_match (pat->p.g, buf);
712 return regexec (pat->p.rx, buf, 0, NULL, 0);
715 static struct pattern_flags *lookup_tag (char tag)
719 for (i = 0; Flags[i].tag; i++)
720 if (Flags[i].tag == tag)
725 static /* const */ char *find_matching_paren (/* const */ char *s)
743 void mutt_pattern_free (pattern_t **pat)
752 if (tmp->stringmatch)
754 else if (tmp->groupmatch)
763 mutt_pattern_free (&tmp->child);
768 pattern_t *mutt_pattern_comp (/* const */ char *s, int flags, BUFFER *err)
770 pattern_t *curlist = NULL;
771 pattern_t *tmp, *tmp2;
772 pattern_t *last = NULL;
776 int implicit = 1; /* used to detect logical AND operator */
777 struct pattern_flags *entry;
782 memset (&ps, 0, sizeof (ps));
784 ps.dsize = mutt_strlen (s);
804 snprintf (err->data, err->dsize, _("error in pattern at: %s"), ps.dptr);
809 /* A & B | C == (A & B) | C */
810 tmp = new_pattern ();
812 tmp->child = curlist;
830 snprintf (err->data, err->dsize, _("missing pattern: %s"), ps.dptr);
831 mutt_pattern_free (&curlist);
834 if (*(ps.dptr + 1) == '(')
836 ps.dptr ++; /* skip ~ */
837 p = find_matching_paren (ps.dptr + 1);
840 snprintf (err->data, err->dsize, _("mismatched brackets: %s"), ps.dptr);
841 mutt_pattern_free (&curlist);
844 tmp = new_pattern ();
852 tmp->alladdr |= alladdr;
855 /* compile the sub-expression */
856 buf = mutt_substrdup (ps.dptr + 1, p);
857 if ((tmp2 = mutt_pattern_comp (buf, flags, err)) == NULL)
860 mutt_pattern_free (&curlist);
865 ps.dptr = p + 1; /* restore location */
870 /* A | B & C == (A | B) & C */
871 tmp = new_pattern ();
873 tmp->child = curlist;
879 tmp = new_pattern ();
881 tmp->alladdr = alladdr;
882 tmp->stringmatch = (*ps.dptr == '=') ? 1 : 0;
883 tmp->groupmatch = (*ps.dptr == '%') ? 1 : 0;
893 ps.dptr++; /* move past the ~ */
894 if ((entry = lookup_tag (*ps.dptr)) == NULL)
896 snprintf (err->data, err->dsize, _("%c: invalid pattern modifier"), *ps.dptr);
897 mutt_pattern_free (&curlist);
900 if (entry->class && (flags & entry->class) == 0)
902 snprintf (err->data, err->dsize, _("%c: not supported in this mode"), *ps.dptr);
903 mutt_pattern_free (&curlist);
908 ps.dptr++; /* eat the operator and any optional whitespace */
915 snprintf (err->data, err->dsize, _("missing parameter"));
916 mutt_pattern_free (&curlist);
919 if (entry->eat_arg (tmp, &ps, err) == -1)
921 mutt_pattern_free (&curlist);
928 p = find_matching_paren (ps.dptr + 1);
931 snprintf (err->data, err->dsize, _("mismatched parenthesis: %s"), ps.dptr);
932 mutt_pattern_free (&curlist);
935 /* compile the sub-expression */
936 buf = mutt_substrdup (ps.dptr + 1, p);
937 if ((tmp = mutt_pattern_comp (buf, flags, err)) == NULL)
940 mutt_pattern_free (&curlist);
950 tmp->alladdr |= alladdr;
953 ps.dptr = p + 1; /* restore location */
956 snprintf (err->data, err->dsize, _("error in pattern at: %s"), ps.dptr);
957 mutt_pattern_free (&curlist);
963 strfcpy (err->data, _("empty pattern"), err->dsize);
968 tmp = new_pattern ();
969 tmp->op = or ? M_OR : M_AND;
970 tmp->child = curlist;
977 perform_and (pattern_t *pat, pattern_exec_flag flags, CONTEXT *ctx, HEADER *hdr)
979 for (; pat; pat = pat->next)
980 if (mutt_pattern_exec (pat, flags, ctx, hdr) <= 0)
986 perform_or (struct pattern_t *pat, pattern_exec_flag flags, CONTEXT *ctx, HEADER *hdr)
988 for (; pat; pat = pat->next)
989 if (mutt_pattern_exec (pat, flags, ctx, hdr) > 0)
994 static int match_adrlist (pattern_t *pat, int match_personal, int n, ...)
1002 for (a = va_arg (ap, ADDRESS *) ; a ; a = a->next)
1004 if (pat->alladdr ^ ((a->mailbox && patmatch (pat, a->mailbox) == 0) ||
1005 (match_personal && a->personal && patmatch (pat, a->personal) == 0)))
1008 return (! pat->alladdr); /* Found match, or non-match if alladdr */
1013 return pat->alladdr; /* No matches, or all matches if alladdr */
1016 static int match_reference (pattern_t *pat, LIST *refs)
1018 for (; refs; refs = refs->next)
1019 if (patmatch (pat, refs->data) == 0)
1025 * Matches subscribed mailing lists
1027 int mutt_is_list_recipient (int alladdr, ADDRESS *a1, ADDRESS *a2)
1029 for (; a1 ; a1 = a1->next)
1030 if (alladdr ^ mutt_is_subscribed_list (a1))
1032 for (; a2 ; a2 = a2->next)
1033 if (alladdr ^ mutt_is_subscribed_list (a2))
1039 * Matches known mailing lists
1040 * The function name may seem a little bit misleading: It checks all
1041 * recipients in To and Cc for known mailing lists, subscribed or not.
1043 int mutt_is_list_cc (int alladdr, ADDRESS *a1, ADDRESS *a2)
1045 for (; a1 ; a1 = a1->next)
1046 if (alladdr ^ mutt_is_mail_list (a1))
1048 for (; a2 ; a2 = a2->next)
1049 if (alladdr ^ mutt_is_mail_list (a2))
1054 static int match_user (int alladdr, ADDRESS *a1, ADDRESS *a2)
1056 for (; a1 ; a1 = a1->next)
1057 if (alladdr ^ mutt_addr_is_user (a1))
1059 for (; a2 ; a2 = a2->next)
1060 if (alladdr ^ mutt_addr_is_user (a2))
1065 static int match_threadcomplete(struct pattern_t *pat, pattern_exec_flag flags, CONTEXT *ctx, THREAD *t,int left,int up,int right,int down)
1074 if(mutt_pattern_exec(pat, flags, ctx, h))
1077 if(up && (a=match_threadcomplete(pat, flags, ctx, t->parent,1,1,1,0)))
1079 if(right && t->parent && (a=match_threadcomplete(pat, flags, ctx, t->next,0,0,1,1)))
1081 if(left && t->parent && (a=match_threadcomplete(pat, flags, ctx, t->prev,1,0,0,1)))
1083 if(down && (a=match_threadcomplete(pat, flags, ctx, t->child,1,0,1,1)))
1089 M_MATCH_FULL_ADDRESS match both personal and machine address */
1091 mutt_pattern_exec (struct pattern_t *pat, pattern_exec_flag flags, CONTEXT *ctx, HEADER *h)
1096 return (pat->not ^ (perform_and (pat->child, flags, ctx, h) > 0));
1098 return (pat->not ^ (perform_or (pat->child, flags, ctx, h) > 0));
1100 return (pat->not ^ match_threadcomplete(pat->child, flags, ctx, h->thread, 1, 1, 1, 1));
1104 return (pat->not ^ h->expired);
1106 return (pat->not ^ h->superseded);
1108 return (pat->not ^ h->flagged);
1110 return (pat->not ^ h->tagged);
1112 return (pat->not ? h->old || h->read : !(h->old || h->read));
1114 return (pat->not ? h->read : !h->read);
1116 return (pat->not ^ h->replied);
1118 return (pat->not ? (!h->old || h->read) : (h->old && !h->read));
1120 return (pat->not ^ h->read);
1122 return (pat->not ^ h->deleted);
1124 return (pat->not ^ (h->msgno >= pat->min - 1 && (pat->max == M_MAXRANGE ||
1125 h->msgno <= pat->max - 1)));
1127 return (pat->not ^ (h->date_sent >= pat->min && h->date_sent <= pat->max));
1128 case M_DATE_RECEIVED:
1129 return (pat->not ^ (h->received >= pat->min && h->received <= pat->max));
1134 * ctx can be NULL in certain cases, such as when replying to a message from the attachment menu and
1135 * the user has a reply-hook using "~h" (bug #2190).
1140 /* IMAP search sets h->matched at search compile time */
1141 if (ctx->magic == M_IMAP && pat->stringmatch)
1142 return (h->matched);
1144 return (pat->not ^ msg_search (ctx, pat, h->msgno));
1146 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS, 1,
1149 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS, 1,
1152 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS, 1,
1155 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS, 1,
1158 return (pat->not ^ (h->env->subject && patmatch (pat, h->env->subject) == 0));
1160 return (pat->not ^ (h->env->message_id && patmatch (pat, h->env->message_id) == 0));
1162 return (pat->not ^ (h->score >= pat->min && (pat->max == M_MAXRANGE ||
1163 h->score <= pat->max)));
1165 return (pat->not ^ (h->content->length >= pat->min && (pat->max == M_MAXRANGE || h->content->length <= pat->max)));
1167 return (pat->not ^ (match_reference (pat, h->env->references) ||
1168 match_reference (pat, h->env->in_reply_to)));
1170 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS, 4,
1171 h->env->from, h->env->sender,
1172 h->env->to, h->env->cc));
1174 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
1175 2, h->env->to, h->env->cc));
1176 case M_LIST: /* known list, subscribed or not */
1177 return (pat->not ^ mutt_is_list_cc (pat->alladdr, h->env->to, h->env->cc));
1178 case M_SUBSCRIBED_LIST:
1179 return (pat->not ^ mutt_is_list_recipient (pat->alladdr, h->env->to, h->env->cc));
1180 case M_PERSONAL_RECIP:
1181 return (pat->not ^ match_user (pat->alladdr, h->env->to, h->env->cc));
1182 case M_PERSONAL_FROM:
1183 return (pat->not ^ match_user (pat->alladdr, h->env->from, NULL));
1185 return (pat->not ^ (h->collapsed && h->num_hidden > 1));
1189 return (pat->not ^ ((h->security & SIGN) ? 1 : 0));
1190 case M_CRYPT_VERIFIED:
1193 return (pat->not ^ ((h->security & GOODSIGN) ? 1 : 0));
1194 case M_CRYPT_ENCRYPT:
1197 return (pat->not ^ ((h->security & ENCRYPT) ? 1 : 0));
1199 if (!(WithCrypto & APPLICATION_PGP))
1201 return (pat->not ^ ((h->security & APPLICATION_PGP) && (h->security & PGPKEY)));
1203 return (pat->not ^ (h->env->x_label && patmatch (pat, h->env->x_label) == 0));
1205 return (pat->not ^ (h->env->spam && h->env->spam->data && patmatch (pat, h->env->spam->data) == 0));
1207 return (pat->not ^ (h->thread && h->thread->duplicate_thread));
1210 int count = mutt_count_body_parts (ctx, h);
1211 return (pat->not ^ (count >= pat->min && (pat->max == M_MAXRANGE ||
1212 count <= pat->max)));
1214 case M_UNREFERENCED:
1215 return (pat->not ^ (h->thread && !h->thread->child));
1217 mutt_error (_("error: unknown op %d (report this error)."), pat->op);
1221 static void quote_simple(char *tmp, size_t len, const char *p)
1226 while (*p && i < len - 3)
1228 if (*p == '\\' || *p == '"')
1236 /* convert a simple search into a real request */
1237 void mutt_check_simple (char *s, size_t len, const char *simple)
1239 char tmp[LONG_STRING];
1243 for (p = s; p && *p; p++)
1245 if (*p == '\\' && *(p + 1))
1247 else if (*p == '~' || *p == '=' || *p == '%')
1254 /* XXX - is ascii_strcasecmp() right here, or should we use locale's
1258 if (do_simple) /* yup, so spoof a real request */
1260 /* convert old tokens into the new format */
1261 if (ascii_strcasecmp ("all", s) == 0 ||
1262 !mutt_strcmp ("^", s) || !mutt_strcmp (".", s)) /* ~A is more efficient */
1263 strfcpy (s, "~A", len);
1264 else if (ascii_strcasecmp ("del", s) == 0)
1265 strfcpy (s, "~D", len);
1266 else if (ascii_strcasecmp ("flag", s) == 0)
1267 strfcpy (s, "~F", len);
1268 else if (ascii_strcasecmp ("new", s) == 0)
1269 strfcpy (s, "~N", len);
1270 else if (ascii_strcasecmp ("old", s) == 0)
1271 strfcpy (s, "~O", len);
1272 else if (ascii_strcasecmp ("repl", s) == 0)
1273 strfcpy (s, "~Q", len);
1274 else if (ascii_strcasecmp ("read", s) == 0)
1275 strfcpy (s, "~R", len);
1276 else if (ascii_strcasecmp ("tag", s) == 0)
1277 strfcpy (s, "~T", len);
1278 else if (ascii_strcasecmp ("unread", s) == 0)
1279 strfcpy (s, "~U", len);
1282 quote_simple (tmp, sizeof(tmp), s);
1283 mutt_expand_fmt (s, len, simple, tmp);
1288 int mutt_pattern_func (int op, char *prompt)
1291 char buf[LONG_STRING] = "", *simple, error[STRING];
1294 progress_t progress;
1296 strfcpy (buf, NONULL (Context->pattern), sizeof (buf));
1297 if (mutt_get_field (prompt, buf, sizeof (buf), M_PATTERN | M_CLEAR) != 0 || !buf[0])
1300 mutt_message _("Compiling search pattern...");
1302 simple = safe_strdup (buf);
1303 mutt_check_simple (buf, sizeof (buf), NONULL (SimpleSearch));
1305 memset (&err, 0, sizeof(err));
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];
1410 progress_t progress;
1411 const char* msg = NULL;
1413 if (!*LastSearch || (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE))
1415 strfcpy (buf, *LastSearch ? LastSearch : "", sizeof (buf));
1416 if (mutt_get_field ((op == OP_SEARCH || op == OP_SEARCH_NEXT) ?
1417 _("Search for: ") : _("Reverse search for: "),
1419 M_CLEAR | M_PATTERN) != 0 || !buf[0])
1422 if (op == OP_SEARCH || op == OP_SEARCH_NEXT)
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))
1435 memset(&err, 0, sizeof(err));
1436 set_option (OPTSEARCHINVALID);
1437 strfcpy (LastSearch, buf, sizeof (LastSearch));
1438 mutt_message _("Compiling search pattern...");
1439 mutt_pattern_free (&SearchPattern);
1441 err.dsize = sizeof (error);
1442 if ((SearchPattern = mutt_pattern_comp (temp, M_FULL_MSG, &err)) == NULL)
1444 mutt_error ("%s", error);
1445 LastSearch[0] = '\0';
1448 mutt_clear_error ();
1452 if (option (OPTSEARCHINVALID))
1454 for (i = 0; i < Context->msgcount; i++)
1455 Context->hdrs[i]->searched = 0;
1457 if (Context->magic == M_IMAP && imap_search (Context, SearchPattern) < 0)
1460 unset_option (OPTSEARCHINVALID);
1463 incr = (option (OPTSEARCHREVERSE)) ? -1 : 1;
1464 if (op == OP_SEARCH_OPPOSITE)
1467 mutt_progress_init (&progress, _("Searching..."), M_PROGRESS_MSG,
1468 ReadInc, Context->vcount);
1470 for (i = cur + incr, j = 0 ; j != Context->vcount; j++)
1472 mutt_progress_update (&progress, j, -1);
1473 if (i > Context->vcount - 1)
1476 if (option (OPTWRAPSEARCH))
1477 msg = _("Search wrapped to top.");
1480 mutt_message _("Search hit bottom without finding match");
1486 i = Context->vcount - 1;
1487 if (option (OPTWRAPSEARCH))
1488 msg = _("Search wrapped to bottom.");
1491 mutt_message _("Search hit top without finding match");
1496 h = Context->hdrs[Context->v2r[i]];
1499 /* if we've already evaulated this message, use the cached value */
1510 /* remember that we've already searched this message */
1512 if ((h->matched = (mutt_pattern_exec (SearchPattern, M_MATCH_FULL_ADDRESS, Context, h) > 0)))
1523 mutt_error _("Search interrupted.");
1531 mutt_error _("Not found.");