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);
185 safe_fclose (&s.fpout);
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);
280 pat->ign_case = mutt_which_case (buf.data) == REG_ICASE;
283 else if (pat->groupmatch)
285 pat->p.g = mutt_pattern_group (buf.data);
290 pat->p.rx = safe_malloc (sizeof (regex_t));
291 r = REGCOMP (pat->p.rx, buf.data, REG_NEWLINE | REG_NOSUB | mutt_which_case (buf.data));
295 regerror (r, pat->p.rx, err->data, err->dsize);
305 int eat_range (pattern_t *pat, BUFFER *s, BUFFER *err)
308 int do_exclusive = 0;
312 * If simple_search is set to "~m %s", the range will have double quotes
322 if ((*s->dptr != '-') && (*s->dptr != '<'))
327 pat->max = M_MAXRANGE;
328 pat->min = strtol (s->dptr + 1, &tmp, 0) + 1; /* exclusive range */
331 pat->min = strtol (s->dptr, &tmp, 0);
332 if (toupper ((unsigned char) *tmp) == 'K') /* is there a prefix? */
337 else if (toupper ((unsigned char) *tmp) == 'M')
362 if (isdigit ((unsigned char) *tmp))
365 pat->max = strtol (tmp, &tmp, 0);
366 if (toupper ((unsigned char) *tmp) == 'K')
371 else if (toupper ((unsigned char) *tmp) == 'M')
380 pat->max = M_MAXRANGE;
382 if (skip_quote && *tmp == '"')
390 static const char *getDate (const char *s, struct tm *t, BUFFER *err)
393 time_t now = time (NULL);
394 struct tm *tm = localtime (&now);
396 t->tm_mday = strtol (s, &p, 10);
397 if (t->tm_mday < 1 || t->tm_mday > 31)
399 snprintf (err->data, err->dsize, _("Invalid day of month: %s"), s);
404 /* fill in today's month and year */
405 t->tm_mon = tm->tm_mon;
406 t->tm_year = tm->tm_year;
410 t->tm_mon = strtol (p, &p, 10) - 1;
411 if (t->tm_mon < 0 || t->tm_mon > 11)
413 snprintf (err->data, err->dsize, _("Invalid month: %s"), p);
418 t->tm_year = tm->tm_year;
422 t->tm_year = strtol (p, &p, 10);
423 if (t->tm_year < 70) /* year 2000+ */
425 else if (t->tm_year > 1900)
434 static const char *get_offset (struct tm *tm, const char *s, int sign)
437 int offset = strtol (s, &ps, 0);
438 if ((sign < 0 && offset > 0) || (sign > 0 && offset < 0))
444 tm->tm_year += offset;
447 tm->tm_mon += offset;
450 tm->tm_mday += 7 * offset;
453 tm->tm_mday += offset;
458 mutt_normalize_time (tm);
462 static void adjust_date_range (struct tm *min, struct tm *max)
464 if (min->tm_year > max->tm_year
465 || (min->tm_year == max->tm_year && min->tm_mon > max->tm_mon)
466 || (min->tm_year == max->tm_year && min->tm_mon == max->tm_mon
467 && min->tm_mday > max->tm_mday))
472 min->tm_year = max->tm_year;
476 min->tm_mon = max->tm_mon;
480 min->tm_mday = max->tm_mday;
483 min->tm_hour = min->tm_min = min->tm_sec = 0;
485 max->tm_min = max->tm_sec = 59;
489 static const char * parse_date_range (const char* pc, struct tm *min,
490 struct tm *max, int haveMin, struct tm *baseMin, BUFFER *err)
492 int flag = M_PDR_NONE;
493 while (*pc && ((flag & M_PDR_DONE) == 0))
502 /* try a range of absolute date minus offset of Ndwmy */
503 pt = get_offset (min, pc, -1);
506 if (flag == M_PDR_NONE)
507 { /* nothing yet and no offset parsed => absolute date? */
508 if (!getDate (pc, max, err))
509 flag |= (M_PDR_ABSOLUTE | M_PDR_ERRORDONE); /* done bad */
512 /* reestablish initial base minimum if not specified */
514 memcpy (min, baseMin, sizeof(struct tm));
515 flag |= (M_PDR_ABSOLUTE | M_PDR_DONE); /* done good */
519 flag |= M_PDR_ERRORDONE;
524 if (flag == M_PDR_NONE && !haveMin)
525 { /* the very first "-3d" without a previous absolute date */
526 max->tm_year = min->tm_year;
527 max->tm_mon = min->tm_mon;
528 max->tm_mday = min->tm_mday;
535 { /* enlarge plusRange */
536 pt = get_offset (max, pc, 1);
538 flag |= M_PDR_ERRORDONE;
547 { /* enlarge window in both directions */
548 pt = get_offset (min, pc, -1);
550 flag |= M_PDR_ERRORDONE;
553 pc = get_offset (max, pc, 1);
554 flag |= M_PDR_WINDOW;
559 flag |= M_PDR_ERRORDONE;
563 if ((flag & M_PDR_ERROR) && !(flag & M_PDR_ABSOLUTE))
564 { /* getDate has its own error message, don't overwrite it here */
565 snprintf (err->data, err->dsize, _("Invalid relative date: %s"), pc-1);
567 return ((flag & M_PDR_ERROR) ? NULL : pc);
570 static int eat_date (pattern_t *pat, BUFFER *s, BUFFER *err)
575 memset (&buffer, 0, sizeof (buffer));
576 if (mutt_extract_token (&buffer, s, M_TOKEN_COMMENT | M_TOKEN_PATTERN) != 0
579 strfcpy (err->data, _("error in expression"), err->dsize);
583 memset (&min, 0, sizeof (min));
584 /* the `0' time is Jan 1, 1970 UTC, so in order to prevent a negative time
585 when doing timezone conversion, we use Jan 2, 1970 UTC as the base
590 memset (&max, 0, sizeof (max));
592 /* Arbitrary year in the future. Don't set this too high
593 or mutt_mktime() returns something larger than will
594 fit in a time_t on some systems */
602 if (strchr ("<>=", buffer.data[0]))
604 /* offset from current time
605 <3d less than three days ago
606 >3d more than three days ago
607 =3d exactly three days ago */
608 time_t now = time (NULL);
609 struct tm *tm = localtime (&now);
612 if (buffer.data[0] == '<')
614 memcpy (&min, tm, sizeof (min));
619 memcpy (&max, tm, sizeof (max));
622 if (buffer.data[0] == '=')
626 tm->tm_min = tm->tm_sec = 59;
628 /* force negative offset */
629 get_offset (tm, buffer.data + 1, -1);
633 /* start at the beginning of the day in question */
634 memcpy (&min, &max, sizeof (max));
635 min.tm_hour = min.tm_sec = min.tm_min = 0;
640 const char *pc = buffer.data;
643 int untilNow = FALSE;
644 if (isdigit ((unsigned char)*pc))
646 /* mininum date specified */
647 if ((pc = getDate (pc, &min, err)) == NULL)
656 const char *pt = pc + 1;
658 untilNow = (*pt == '\0');
663 { /* max date or relative range/window */
668 { /* save base minimum and set current date, e.g. for "-3d+1d" */
669 time_t now = time (NULL);
670 struct tm *tm = localtime (&now);
671 memcpy (&baseMin, &min, sizeof(baseMin));
672 memcpy (&min, tm, sizeof (min));
673 min.tm_hour = min.tm_sec = min.tm_min = 0;
676 /* preset max date for relative offsets,
677 if nothing follows we search for messages on a specific day */
678 max.tm_year = min.tm_year;
679 max.tm_mon = min.tm_mon;
680 max.tm_mday = min.tm_mday;
682 if (!parse_date_range (pc, &min, &max, haveMin, &baseMin, err))
683 { /* bail out on any parsing error */
690 /* Since we allow two dates to be specified we'll have to adjust that. */
691 adjust_date_range (&min, &max);
693 pat->min = mutt_mktime (&min, 1);
694 pat->max = mutt_mktime (&max, 1);
701 static int patmatch (const pattern_t* pat, const char* buf)
703 if (pat->stringmatch)
704 return pat->ign_case ? !strcasestr (buf, pat->p.str) :
705 !strstr (buf, pat->p.str);
706 else if (pat->groupmatch)
707 return !mutt_group_match (pat->p.g, buf);
709 return regexec (pat->p.rx, buf, 0, NULL, 0);
712 static struct pattern_flags *lookup_tag (char tag)
716 for (i = 0; Flags[i].tag; i++)
717 if (Flags[i].tag == tag)
722 static /* const */ char *find_matching_paren (/* const */ char *s)
740 void mutt_pattern_free (pattern_t **pat)
749 if (tmp->stringmatch)
751 else if (tmp->groupmatch)
760 mutt_pattern_free (&tmp->child);
765 pattern_t *mutt_pattern_comp (/* const */ char *s, int flags, BUFFER *err)
767 pattern_t *curlist = NULL;
768 pattern_t *tmp, *tmp2;
769 pattern_t *last = NULL;
773 int implicit = 1; /* used to detect logical AND operator */
774 struct pattern_flags *entry;
779 memset (&ps, 0, sizeof (ps));
781 ps.dsize = mutt_strlen (s);
801 snprintf (err->data, err->dsize, _("error in pattern at: %s"), ps.dptr);
806 /* A & B | C == (A & B) | C */
807 tmp = new_pattern ();
809 tmp->child = curlist;
825 if (*(ps.dptr + 1) == '(')
827 ps.dptr ++; /* skip ~ */
828 p = find_matching_paren (ps.dptr + 1);
831 snprintf (err->data, err->dsize, _("mismatched brackets: %s"), ps.dptr);
832 mutt_pattern_free (&curlist);
835 tmp = new_pattern ();
843 tmp->alladdr |= alladdr;
846 /* compile the sub-expression */
847 buf = mutt_substrdup (ps.dptr + 1, p);
848 if ((tmp2 = mutt_pattern_comp (buf, flags, err)) == NULL)
851 mutt_pattern_free (&curlist);
856 ps.dptr = p + 1; /* restore location */
861 /* A | B & C == (A | B) & C */
862 tmp = new_pattern ();
864 tmp->child = curlist;
870 tmp = new_pattern ();
872 tmp->alladdr = alladdr;
873 tmp->stringmatch = (*ps.dptr == '=') ? 1 : 0;
874 tmp->groupmatch = (*ps.dptr == '%') ? 1 : 0;
884 ps.dptr++; /* move past the ~ */
885 if ((entry = lookup_tag (*ps.dptr)) == NULL)
887 snprintf (err->data, err->dsize, _("%c: invalid pattern modifier"), *ps.dptr);
888 mutt_pattern_free (&curlist);
891 if (entry->class && (flags & entry->class) == 0)
893 snprintf (err->data, err->dsize, _("%c: not supported in this mode"), *ps.dptr);
894 mutt_pattern_free (&curlist);
899 ps.dptr++; /* eat the operator and any optional whitespace */
906 snprintf (err->data, err->dsize, _("missing parameter"));
907 mutt_pattern_free (&curlist);
910 if (entry->eat_arg (tmp, &ps, err) == -1)
912 mutt_pattern_free (&curlist);
919 p = find_matching_paren (ps.dptr + 1);
922 snprintf (err->data, err->dsize, _("mismatched parenthesis: %s"), ps.dptr);
923 mutt_pattern_free (&curlist);
926 /* compile the sub-expression */
927 buf = mutt_substrdup (ps.dptr + 1, p);
928 if ((tmp = mutt_pattern_comp (buf, flags, err)) == NULL)
931 mutt_pattern_free (&curlist);
941 tmp->alladdr |= alladdr;
944 ps.dptr = p + 1; /* restore location */
947 snprintf (err->data, err->dsize, _("error in pattern at: %s"), ps.dptr);
948 mutt_pattern_free (&curlist);
954 strfcpy (err->data, _("empty pattern"), err->dsize);
959 tmp = new_pattern ();
960 tmp->op = or ? M_OR : M_AND;
961 tmp->child = curlist;
968 perform_and (pattern_t *pat, pattern_exec_flag flags, CONTEXT *ctx, HEADER *hdr)
970 for (; pat; pat = pat->next)
971 if (mutt_pattern_exec (pat, flags, ctx, hdr) <= 0)
977 perform_or (struct 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)
985 static int match_adrlist (pattern_t *pat, int match_personal, int n, ...)
993 for (a = va_arg (ap, ADDRESS *) ; a ; a = a->next)
995 if (pat->alladdr ^ ((a->mailbox && patmatch (pat, a->mailbox) == 0) ||
996 (match_personal && a->personal && patmatch (pat, a->personal) == 0)))
999 return (! pat->alladdr); /* Found match, or non-match if alladdr */
1004 return pat->alladdr; /* No matches, or all matches if alladdr */
1007 static int match_reference (pattern_t *pat, LIST *refs)
1009 for (; refs; refs = refs->next)
1010 if (patmatch (pat, refs->data) == 0)
1016 * Matches subscribed mailing lists
1018 int mutt_is_list_recipient (int alladdr, ADDRESS *a1, ADDRESS *a2)
1020 for (; a1 ; a1 = a1->next)
1021 if (alladdr ^ mutt_is_subscribed_list (a1))
1023 for (; a2 ; a2 = a2->next)
1024 if (alladdr ^ mutt_is_subscribed_list (a2))
1030 * Matches known mailing lists
1031 * The function name may seem a little bit misleading: It checks all
1032 * recipients in To and Cc for known mailing lists, subscribed or not.
1034 int mutt_is_list_cc (int alladdr, ADDRESS *a1, ADDRESS *a2)
1036 for (; a1 ; a1 = a1->next)
1037 if (alladdr ^ mutt_is_mail_list (a1))
1039 for (; a2 ; a2 = a2->next)
1040 if (alladdr ^ mutt_is_mail_list (a2))
1045 static int match_user (int alladdr, ADDRESS *a1, ADDRESS *a2)
1047 for (; a1 ; a1 = a1->next)
1048 if (alladdr ^ mutt_addr_is_user (a1))
1050 for (; a2 ; a2 = a2->next)
1051 if (alladdr ^ mutt_addr_is_user (a2))
1056 static int match_threadcomplete(struct pattern_t *pat, pattern_exec_flag flags, CONTEXT *ctx, THREAD *t,int left,int up,int right,int down)
1065 if(mutt_pattern_exec(pat, flags, ctx, h))
1068 if(up && (a=match_threadcomplete(pat, flags, ctx, t->parent,1,1,1,0)))
1070 if(right && t->parent && (a=match_threadcomplete(pat, flags, ctx, t->next,0,0,1,1)))
1072 if(left && t->parent && (a=match_threadcomplete(pat, flags, ctx, t->prev,1,0,0,1)))
1074 if(down && (a=match_threadcomplete(pat, flags, ctx, t->child,1,0,1,1)))
1080 M_MATCH_FULL_ADDRESS match both personal and machine address */
1082 mutt_pattern_exec (struct pattern_t *pat, pattern_exec_flag flags, CONTEXT *ctx, HEADER *h)
1087 return (pat->not ^ (perform_and (pat->child, flags, ctx, h) > 0));
1089 return (pat->not ^ (perform_or (pat->child, flags, ctx, h) > 0));
1091 return (pat->not ^ match_threadcomplete(pat->child, flags, ctx, h->thread, 1, 1, 1, 1));
1095 return (pat->not ^ h->expired);
1097 return (pat->not ^ h->superseded);
1099 return (pat->not ^ h->flagged);
1101 return (pat->not ^ h->tagged);
1103 return (pat->not ? h->old || h->read : !(h->old || h->read));
1105 return (pat->not ? h->read : !h->read);
1107 return (pat->not ^ h->replied);
1109 return (pat->not ? (!h->old || h->read) : (h->old && !h->read));
1111 return (pat->not ^ h->read);
1113 return (pat->not ^ h->deleted);
1115 return (pat->not ^ (h->msgno >= pat->min - 1 && (pat->max == M_MAXRANGE ||
1116 h->msgno <= pat->max - 1)));
1118 return (pat->not ^ (h->date_sent >= pat->min && h->date_sent <= pat->max));
1119 case M_DATE_RECEIVED:
1120 return (pat->not ^ (h->received >= pat->min && h->received <= pat->max));
1125 * ctx can be NULL in certain cases, such as when replying to a message from the attachment menu and
1126 * the user has a reply-hook using "~h" (bug #2190).
1131 /* IMAP search sets h->matched at search compile time */
1132 if (ctx->magic == M_IMAP && pat->stringmatch)
1133 return (h->matched);
1135 return (pat->not ^ msg_search (ctx, pat, h->msgno));
1137 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS, 1,
1140 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS, 1,
1143 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS, 1,
1146 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS, 1,
1149 return (pat->not ^ (h->env->subject && patmatch (pat, h->env->subject) == 0));
1151 return (pat->not ^ (h->env->message_id && patmatch (pat, h->env->message_id) == 0));
1153 return (pat->not ^ (h->score >= pat->min && (pat->max == M_MAXRANGE ||
1154 h->score <= pat->max)));
1156 return (pat->not ^ (h->content->length >= pat->min && (pat->max == M_MAXRANGE || h->content->length <= pat->max)));
1158 return (pat->not ^ (match_reference (pat, h->env->references) ||
1159 match_reference (pat, h->env->in_reply_to)));
1161 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS, 4,
1162 h->env->from, h->env->sender,
1163 h->env->to, h->env->cc));
1165 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
1166 2, h->env->to, h->env->cc));
1167 case M_LIST: /* known list, subscribed or not */
1168 return (pat->not ^ mutt_is_list_cc (pat->alladdr, h->env->to, h->env->cc));
1169 case M_SUBSCRIBED_LIST:
1170 return (pat->not ^ mutt_is_list_recipient (pat->alladdr, h->env->to, h->env->cc));
1171 case M_PERSONAL_RECIP:
1172 return (pat->not ^ match_user (pat->alladdr, h->env->to, h->env->cc));
1173 case M_PERSONAL_FROM:
1174 return (pat->not ^ match_user (pat->alladdr, h->env->from, NULL));
1176 return (pat->not ^ (h->collapsed && h->num_hidden > 1));
1180 return (pat->not ^ ((h->security & SIGN) ? 1 : 0));
1181 case M_CRYPT_VERIFIED:
1184 return (pat->not ^ ((h->security & GOODSIGN) ? 1 : 0));
1185 case M_CRYPT_ENCRYPT:
1188 return (pat->not ^ ((h->security & ENCRYPT) ? 1 : 0));
1190 if (!(WithCrypto & APPLICATION_PGP))
1192 return (pat->not ^ ((h->security & APPLICATION_PGP) && (h->security & PGPKEY)));
1194 return (pat->not ^ (h->env->x_label && patmatch (pat, h->env->x_label) == 0));
1196 return (pat->not ^ (h->env->spam && h->env->spam->data && patmatch (pat, h->env->spam->data) == 0));
1198 return (pat->not ^ (h->thread && h->thread->duplicate_thread));
1201 int count = mutt_count_body_parts (ctx, h);
1202 return (pat->not ^ (count >= pat->min && (pat->max == M_MAXRANGE ||
1203 count <= pat->max)));
1205 case M_UNREFERENCED:
1206 return (pat->not ^ (h->thread && !h->thread->child));
1208 mutt_error (_("error: unknown op %d (report this error)."), pat->op);
1212 static void quote_simple(char *tmp, size_t len, const char *p)
1217 while (*p && i < len - 3)
1219 if (*p == '\\' || *p == '"')
1227 /* convert a simple search into a real request */
1228 void mutt_check_simple (char *s, size_t len, const char *simple)
1230 char tmp[LONG_STRING];
1234 for (p = s; p && *p; p++)
1236 if (*p == '\\' && *(p + 1))
1238 else if (*p == '~' || *p == '=' || *p == '%')
1245 /* XXX - is ascii_strcasecmp() right here, or should we use locale's
1249 if (do_simple) /* yup, so spoof a real request */
1251 /* convert old tokens into the new format */
1252 if (ascii_strcasecmp ("all", s) == 0 ||
1253 !mutt_strcmp ("^", s) || !mutt_strcmp (".", s)) /* ~A is more efficient */
1254 strfcpy (s, "~A", len);
1255 else if (ascii_strcasecmp ("del", s) == 0)
1256 strfcpy (s, "~D", len);
1257 else if (ascii_strcasecmp ("flag", s) == 0)
1258 strfcpy (s, "~F", len);
1259 else if (ascii_strcasecmp ("new", s) == 0)
1260 strfcpy (s, "~N", len);
1261 else if (ascii_strcasecmp ("old", s) == 0)
1262 strfcpy (s, "~O", len);
1263 else if (ascii_strcasecmp ("repl", s) == 0)
1264 strfcpy (s, "~Q", len);
1265 else if (ascii_strcasecmp ("read", s) == 0)
1266 strfcpy (s, "~R", len);
1267 else if (ascii_strcasecmp ("tag", s) == 0)
1268 strfcpy (s, "~T", len);
1269 else if (ascii_strcasecmp ("unread", s) == 0)
1270 strfcpy (s, "~U", len);
1273 quote_simple (tmp, sizeof(tmp), s);
1274 mutt_expand_fmt (s, len, simple, tmp);
1279 int mutt_pattern_func (int op, char *prompt)
1282 char buf[LONG_STRING] = "", *simple, error[STRING];
1285 progress_t progress;
1287 strfcpy (buf, NONULL (Context->pattern), sizeof (buf));
1288 if (mutt_get_field (prompt, buf, sizeof (buf), M_PATTERN | M_CLEAR) != 0 || !buf[0])
1291 mutt_message _("Compiling search pattern...");
1293 simple = safe_strdup (buf);
1294 mutt_check_simple (buf, sizeof (buf), NONULL (SimpleSearch));
1297 err.dsize = sizeof (error);
1298 if ((pat = mutt_pattern_comp (buf, M_FULL_MSG, &err)) == NULL)
1301 mutt_error ("%s", err.data);
1306 if (Context->magic == M_IMAP && imap_search (Context, pat) < 0)
1310 mutt_progress_init (&progress, _("Executing command on matching messages..."),
1311 M_PROGRESS_MSG, ReadInc,
1312 (op == M_LIMIT) ? Context->msgcount : Context->vcount);
1314 #define THIS_BODY Context->hdrs[i]->content
1318 Context->vcount = 0;
1320 Context->collapsed = 0;
1322 for (i = 0; i < Context->msgcount; i++)
1324 mutt_progress_update (&progress, i, -1);
1325 /* new limit pattern implicitly uncollapses all threads */
1326 Context->hdrs[i]->virtual = -1;
1327 Context->hdrs[i]->limited = 0;
1328 Context->hdrs[i]->collapsed = 0;
1329 Context->hdrs[i]->num_hidden = 0;
1330 if (mutt_pattern_exec (pat, M_MATCH_FULL_ADDRESS, Context, Context->hdrs[i]))
1332 Context->hdrs[i]->virtual = Context->vcount;
1333 Context->hdrs[i]->limited = 1;
1334 Context->v2r[Context->vcount] = i;
1336 Context->vsize+=THIS_BODY->length + THIS_BODY->offset -
1337 THIS_BODY->hdr_offset;
1343 for (i = 0; i < Context->vcount; i++)
1345 mutt_progress_update (&progress, i, -1);
1346 if (mutt_pattern_exec (pat, M_MATCH_FULL_ADDRESS, Context, Context->hdrs[Context->v2r[i]]))
1352 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_DELETE,
1357 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_TAG,
1367 mutt_clear_error ();
1371 /* drop previous limit pattern */
1372 FREE (&Context->pattern);
1373 if (Context->limit_pattern)
1374 mutt_pattern_free (&Context->limit_pattern);
1376 if (Context->msgcount && !Context->vcount)
1377 mutt_error _("No messages matched criteria.");
1379 /* record new limit pattern, unless match all */
1380 if (mutt_strcmp (buf, "~A") != 0)
1382 Context->pattern = simple;
1383 simple = NULL; /* don't clobber it */
1384 Context->limit_pattern = mutt_pattern_comp (buf, M_FULL_MSG, &err);
1388 mutt_pattern_free (&pat);
1392 int mutt_search_command (int cur, int op)
1396 char temp[LONG_STRING];
1401 progress_t progress;
1402 const char* msg = NULL;
1404 if (!*LastSearch || (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE))
1406 strfcpy (buf, *LastSearch ? LastSearch : "", sizeof (buf));
1407 if (mutt_get_field ((op == OP_SEARCH || op == OP_SEARCH_NEXT) ?
1408 _("Search for: ") : _("Reverse search for: "),
1410 M_CLEAR | M_PATTERN) != 0 || !buf[0])
1413 if (op == OP_SEARCH || op == OP_SEARCH_NEXT)
1414 unset_option (OPTSEARCHREVERSE);
1416 set_option (OPTSEARCHREVERSE);
1418 /* compare the *expanded* version of the search pattern in case
1419 $simple_search has changed while we were searching */
1420 strfcpy (temp, buf, sizeof (temp));
1421 mutt_check_simple (temp, sizeof (temp), NONULL (SimpleSearch));
1423 if (!SearchPattern || mutt_strcmp (temp, LastSearchExpn))
1425 set_option (OPTSEARCHINVALID);
1426 strfcpy (LastSearch, buf, sizeof (LastSearch));
1427 mutt_message _("Compiling search pattern...");
1428 mutt_pattern_free (&SearchPattern);
1430 err.dsize = sizeof (error);
1431 if ((SearchPattern = mutt_pattern_comp (temp, M_FULL_MSG, &err)) == NULL)
1433 mutt_error ("%s", error);
1436 mutt_clear_error ();
1440 if (option (OPTSEARCHINVALID))
1442 for (i = 0; i < Context->msgcount; i++)
1443 Context->hdrs[i]->searched = 0;
1445 if (Context->magic == M_IMAP && imap_search (Context, SearchPattern) < 0)
1448 unset_option (OPTSEARCHINVALID);
1451 incr = (option (OPTSEARCHREVERSE)) ? -1 : 1;
1452 if (op == OP_SEARCH_OPPOSITE)
1455 mutt_progress_init (&progress, _("Searching..."), M_PROGRESS_MSG,
1456 ReadInc, Context->vcount);
1458 for (i = cur + incr, j = 0 ; j != Context->vcount; j++)
1460 mutt_progress_update (&progress, j, -1);
1461 if (i > Context->vcount - 1)
1464 if (option (OPTWRAPSEARCH))
1465 msg = _("Search wrapped to top.");
1468 mutt_message _("Search hit bottom without finding match");
1474 i = Context->vcount - 1;
1475 if (option (OPTWRAPSEARCH))
1476 msg = _("Search wrapped to bottom.");
1479 mutt_message _("Search hit top without finding match");
1484 h = Context->hdrs[Context->v2r[i]];
1487 /* if we've already evaulated this message, use the cached value */
1498 /* remember that we've already searched this message */
1500 if ((h->matched = (mutt_pattern_exec (SearchPattern, M_MATCH_FULL_ADDRESS, Context, h) > 0)))
1511 mutt_error _("Search interrupted.");
1519 mutt_error _("Not found.");