2 * Copyright (C) 1996-2000 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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
36 #include "mutt_crypt.h"
38 static int eat_regexp (pattern_t *pat, BUFFER *, BUFFER *);
39 static int eat_date (pattern_t *pat, BUFFER *, BUFFER *);
40 static int eat_range (pattern_t *pat, BUFFER *, BUFFER *);
44 int tag; /* character used to represent this op */
45 int op; /* operation to perform */
47 int (*eat_arg) (pattern_t *, BUFFER *, BUFFER *);
51 { 'A', M_ALL, 0, NULL },
52 { 'b', M_BODY, M_FULL_MSG, eat_regexp },
53 { 'B', M_WHOLE_MSG, M_FULL_MSG, eat_regexp },
54 { 'c', M_CC, 0, eat_regexp },
55 { 'C', M_RECIPIENT, 0, eat_regexp },
56 { 'd', M_DATE, 0, eat_date },
57 { 'D', M_DELETED, 0, NULL },
58 { 'e', M_SENDER, 0, eat_regexp },
59 { 'E', M_EXPIRED, 0, NULL },
60 { 'f', M_FROM, 0, eat_regexp },
61 { 'F', M_FLAG, 0, NULL },
62 { 'g', M_CRYPT_SIGN, 0, NULL },
63 { 'G', M_CRYPT_ENCRYPT, 0, NULL },
64 { 'h', M_HEADER, M_FULL_MSG, eat_regexp },
65 { 'H', M_HORMEL, 0, eat_regexp },
66 { 'i', M_ID, 0, eat_regexp },
67 { 'k', M_PGP_KEY, 0, NULL },
68 { 'L', M_ADDRESS, 0, eat_regexp },
69 { 'l', M_LIST, 0, NULL },
70 { 'm', M_MESSAGE, 0, eat_range },
71 { 'n', M_SCORE, 0, eat_range },
72 { 'N', M_NEW, 0, NULL },
73 { 'O', M_OLD, 0, NULL },
74 { 'p', M_PERSONAL_RECIP, 0, NULL },
75 { 'P', M_PERSONAL_FROM, 0, NULL },
76 { 'Q', M_REPLIED, 0, NULL },
77 { 'R', M_READ, 0, NULL },
78 { 'r', M_DATE_RECEIVED, 0, eat_date },
79 { 's', M_SUBJECT, 0, eat_regexp },
80 { 'S', M_SUPERSEDED, 0, NULL },
81 { 'T', M_TAG, 0, NULL },
82 { 't', M_TO, 0, eat_regexp },
83 { 'U', M_UNREAD, 0, NULL },
84 { 'v', M_COLLAPSED, 0, NULL },
85 { 'V', M_CRYPT_VERIFIED, 0, NULL },
87 { 'w', M_NEWSGROUPS, 0, eat_regexp },
89 { 'x', M_REFERENCE, 0, eat_regexp },
90 { 'y', M_XLABEL, 0, eat_regexp },
91 { 'z', M_SIZE, 0, eat_range },
92 { '=', M_DUPLICATED, 0, NULL },
93 { '$', M_UNREFERENCED, 0, NULL },
97 static pattern_t *SearchPattern = NULL; /* current search pattern */
98 static char LastSearch[STRING] = { 0 }; /* last pattern searched for */
99 static char LastSearchExpn[LONG_STRING] = { 0 }; /* expanded version of
102 #define M_MAXRANGE -1
104 /* constants for parse_date_range() */
105 #define M_PDR_NONE 0x0000
106 #define M_PDR_MINUS 0x0001
107 #define M_PDR_PLUS 0x0002
108 #define M_PDR_WINDOW 0x0004
109 #define M_PDR_ABSOLUTE 0x0008
110 #define M_PDR_DONE 0x0010
111 #define M_PDR_ERROR 0x0100
112 #define M_PDR_ERRORDONE (M_PDR_ERROR | M_PDR_DONE)
115 int mutt_getvaluebychar (char ch, struct mapping_t *table)
119 for (i = 0; table[i].name; i++)
121 if (ch == table[i].name[0])
122 return table[i].value;
128 /* if no uppercase letters are given, do a case-insensitive search */
129 int mutt_which_case (const char *s)
133 if (isalpha ((unsigned char) *s) && isupper ((unsigned char) *s))
134 return 0; /* case-sensitive */
137 return REG_ICASE; /* case-insensitive */
141 msg_search (CONTEXT *ctx, regex_t *rx, char *buf, size_t blen, int op, int msgno)
143 char tempfile[_POSIX_PATH_MAX];
150 HEADER *h = ctx->hdrs[msgno];
152 if ((msg = mx_open_message (ctx, msgno)) != NULL)
154 if (option (OPTTHOROUGHSRC))
156 /* decode the header / body */
157 memset (&s, 0, sizeof (s));
159 s.flags = M_CHARCONV;
160 mutt_mktemp (tempfile);
161 if ((s.fpout = safe_fopen (tempfile, "w+")) == NULL)
163 mutt_perror (tempfile);
168 mutt_copy_header (msg->fp, h, s.fpout, CH_FROM | CH_DECODE, NULL);
172 mutt_parse_mime_message (ctx, h);
174 if (WithCrypto && (h->security & ENCRYPT)
175 && !crypt_valid_passphrase(h->security))
177 mx_close_message (&msg);
186 fseek (msg->fp, h->offset, 0);
187 mutt_body_handler (h->content, &s);
193 fstat (fileno (fp), &st);
194 lng = (long) st.st_size;
198 /* raw header / body */
202 fseek (fp, h->offset, 0);
203 lng = h->content->offset - h->offset;
208 fseek (fp, h->content->offset, 0);
209 lng += h->content->length;
213 /* search the file "fp" */
216 if (fgets (buf, blen - 1, fp) == NULL)
217 break; /* don't loop forever */
218 if (regexec (rx, buf, 0, NULL, 0) == 0)
223 lng -= mutt_strlen (buf);
226 mx_close_message (&msg);
228 if (option (OPTTHOROUGHSRC))
238 int eat_regexp (pattern_t *pat, BUFFER *s, BUFFER *err)
243 memset (&buf, 0, sizeof (buf));
244 if (mutt_extract_token (&buf, s, M_TOKEN_PATTERN | M_TOKEN_COMMENT) != 0 ||
247 snprintf (err->data, err->dsize, _("Error in expression: %s"), s->dptr);
250 pat->rx = safe_malloc (sizeof (regex_t));
251 r = REGCOMP (pat->rx, buf.data, REG_NEWLINE | REG_NOSUB | mutt_which_case (buf.data));
255 regerror (r, pat->rx, err->data, err->dsize);
263 int eat_range (pattern_t *pat, BUFFER *s, BUFFER *err)
266 int do_exclusive = 0;
270 * If simple_search is set to "~m %s", the range will have double quotes
280 if ((*s->dptr != '-') && (*s->dptr != '<'))
285 pat->max = M_MAXRANGE;
286 pat->min = strtol (s->dptr + 1, &tmp, 0) + 1; /* exclusive range */
289 pat->min = strtol (s->dptr, &tmp, 0);
290 if (toupper ((unsigned char) *tmp) == 'K') /* is there a prefix? */
295 else if (toupper ((unsigned char) *tmp) == 'M')
320 if (isdigit ((unsigned char) *tmp))
323 pat->max = strtol (tmp, &tmp, 0);
324 if (toupper ((unsigned char) *tmp) == 'K')
329 else if (toupper ((unsigned char) *tmp) == 'M')
338 pat->max = M_MAXRANGE;
340 if (skip_quote && *tmp == '"')
348 static const char *getDate (const char *s, struct tm *t, BUFFER *err)
351 time_t now = time (NULL);
352 struct tm *tm = localtime (&now);
354 t->tm_mday = strtol (s, &p, 10);
355 if (t->tm_mday < 1 || t->tm_mday > 31)
357 snprintf (err->data, err->dsize, _("Invalid day of month: %s"), s);
362 /* fill in today's month and year */
363 t->tm_mon = tm->tm_mon;
364 t->tm_year = tm->tm_year;
368 t->tm_mon = strtol (p, &p, 10) - 1;
369 if (t->tm_mon < 0 || t->tm_mon > 11)
371 snprintf (err->data, err->dsize, _("Invalid month: %s"), p);
376 t->tm_year = tm->tm_year;
380 t->tm_year = strtol (p, &p, 10);
381 if (t->tm_year < 70) /* year 2000+ */
383 else if (t->tm_year > 1900)
392 static const char *get_offset (struct tm *tm, const char *s, int sign)
395 int offset = strtol (s, &ps, 0);
396 if ((sign < 0 && offset > 0) || (sign > 0 && offset < 0))
402 tm->tm_year += offset;
405 tm->tm_mon += offset;
408 tm->tm_mday += 7 * offset;
411 tm->tm_mday += offset;
416 mutt_normalize_time (tm);
420 static void adjust_date_range (struct tm *min, struct tm *max)
422 if (min->tm_year > max->tm_year
423 || (min->tm_year == max->tm_year && min->tm_mon > max->tm_mon)
424 || (min->tm_year == max->tm_year && min->tm_mon == max->tm_mon
425 && min->tm_mday > max->tm_mday))
430 min->tm_year = max->tm_year;
434 min->tm_mon = max->tm_mon;
438 min->tm_mday = max->tm_mday;
441 min->tm_hour = min->tm_min = min->tm_sec = 0;
443 max->tm_min = max->tm_sec = 59;
447 static const char * parse_date_range (const char* pc, struct tm *min,
448 struct tm *max, int haveMin, struct tm *baseMin, BUFFER *err)
450 int flag = M_PDR_NONE;
451 while (*pc && ((flag & M_PDR_DONE) == 0))
460 /* try a range of absolute date minus offset of Ndwmy */
461 pt = get_offset (min, pc, -1);
464 if (flag == M_PDR_NONE)
465 { /* nothing yet and no offset parsed => absolute date? */
466 if (!getDate (pc, max, err))
467 flag |= (M_PDR_ABSOLUTE | M_PDR_ERRORDONE); /* done bad */
470 /* reestablish initial base minimum if not specified */
472 memcpy (min, baseMin, sizeof(struct tm));
473 flag |= (M_PDR_ABSOLUTE | M_PDR_DONE); /* done good */
477 flag |= M_PDR_ERRORDONE;
482 if (flag == M_PDR_NONE && !haveMin)
483 { /* the very first "-3d" without a previous absolute date */
484 max->tm_year = min->tm_year;
485 max->tm_mon = min->tm_mon;
486 max->tm_mday = min->tm_mday;
493 { /* enlarge plusRange */
494 pt = get_offset (max, pc, 1);
496 flag |= M_PDR_ERRORDONE;
505 { /* enlarge window in both directions */
506 pt = get_offset (min, pc, -1);
508 flag |= M_PDR_ERRORDONE;
511 pc = get_offset (max, pc, 1);
512 flag |= M_PDR_WINDOW;
517 flag |= M_PDR_ERRORDONE;
521 if ((flag & M_PDR_ERROR) && !(flag & M_PDR_ABSOLUTE))
522 { /* getDate has its own error message, don't overwrite it here */
523 snprintf (err->data, err->dsize, _("Invalid relative date: %s"), pc-1);
525 return ((flag & M_PDR_ERROR) ? NULL : pc);
528 static int eat_date (pattern_t *pat, BUFFER *s, BUFFER *err)
533 memset (&buffer, 0, sizeof (buffer));
534 if (mutt_extract_token (&buffer, s, M_TOKEN_COMMENT | M_TOKEN_PATTERN) != 0
537 strfcpy (err->data, _("error in expression"), err->dsize);
541 memset (&min, 0, sizeof (min));
542 /* the `0' time is Jan 1, 1970 UTC, so in order to prevent a negative time
543 when doing timezone conversion, we use Jan 2, 1970 UTC as the base
548 memset (&max, 0, sizeof (max));
550 /* Arbitrary year in the future. Don't set this too high
551 or mutt_mktime() returns something larger than will
552 fit in a time_t on some systems */
560 if (strchr ("<>=", buffer.data[0]))
562 /* offset from current time
563 <3d less than three days ago
564 >3d more than three days ago
565 =3d exactly three days ago */
566 time_t now = time (NULL);
567 struct tm *tm = localtime (&now);
570 if (buffer.data[0] == '<')
572 memcpy (&min, tm, sizeof (min));
577 memcpy (&max, tm, sizeof (max));
580 if (buffer.data[0] == '=')
584 tm->tm_min = tm->tm_sec = 59;
586 /* force negative offset */
587 get_offset (tm, buffer.data + 1, -1);
591 /* start at the beginning of the day in question */
592 memcpy (&min, &max, sizeof (max));
593 min.tm_hour = min.tm_sec = min.tm_min = 0;
598 const char *pc = buffer.data;
601 int untilNow = FALSE;
602 if (isdigit ((unsigned char)*pc))
604 /* mininum date specified */
605 if ((pc = getDate (pc, &min, err)) == NULL)
614 const char *pt = pc + 1;
616 untilNow = (*pt == '\0');
621 { /* max date or relative range/window */
626 { /* save base minimum and set current date, e.g. for "-3d+1d" */
627 time_t now = time (NULL);
628 struct tm *tm = localtime (&now);
629 memcpy (&baseMin, &min, sizeof(baseMin));
630 memcpy (&min, tm, sizeof (min));
631 min.tm_hour = min.tm_sec = min.tm_min = 0;
634 /* preset max date for relative offsets,
635 if nothing follows we search for messages on a specific day */
636 max.tm_year = min.tm_year;
637 max.tm_mon = min.tm_mon;
638 max.tm_mday = min.tm_mday;
640 if (!parse_date_range (pc, &min, &max, haveMin, &baseMin, err))
641 { /* bail out on any parsing error */
648 /* Since we allow two dates to be specified we'll have to adjust that. */
649 adjust_date_range (&min, &max);
651 pat->min = mutt_mktime (&min, 1);
652 pat->max = mutt_mktime (&max, 1);
659 static struct pattern_flags *lookup_tag (char tag)
663 for (i = 0; Flags[i].tag; i++)
664 if (Flags[i].tag == tag)
669 static /* const */ char *find_matching_paren (/* const */ char *s)
687 void mutt_pattern_free (pattern_t **pat)
702 mutt_pattern_free (&tmp->child);
707 pattern_t *mutt_pattern_comp (/* const */ char *s, int flags, BUFFER *err)
709 pattern_t *curlist = NULL;
711 pattern_t *last = NULL;
715 int implicit = 1; /* used to detect logical AND operator */
716 struct pattern_flags *entry;
721 memset (&ps, 0, sizeof (ps));
723 ps.dsize = mutt_strlen (s);
743 snprintf (err->data, err->dsize, _("error in pattern at: %s"), ps.dptr);
748 /* A & B | C == (A & B) | C */
749 tmp = new_pattern ();
751 tmp->child = curlist;
767 /* A | B & C == (A | B) & C */
768 tmp = new_pattern ();
770 tmp->child = curlist;
776 tmp = new_pattern ();
778 tmp->alladdr = alladdr;
788 ps.dptr++; /* move past the ~ */
789 if ((entry = lookup_tag (*ps.dptr)) == NULL)
791 snprintf (err->data, err->dsize, _("%c: invalid command"), *ps.dptr);
792 mutt_pattern_free (&curlist);
795 if (entry->class && (flags & entry->class) == 0)
797 snprintf (err->data, err->dsize, _("%c: not supported in this mode"), *ps.dptr);
798 mutt_pattern_free (&curlist);
803 ps.dptr++; /* eat the operator and any optional whitespace */
810 snprintf (err->data, err->dsize, _("missing parameter"));
811 mutt_pattern_free (&curlist);
814 if (entry->eat_arg (tmp, &ps, err) == -1)
816 mutt_pattern_free (&curlist);
823 p = find_matching_paren (ps.dptr + 1);
826 snprintf (err->data, err->dsize, _("mismatched parenthesis: %s"), ps.dptr);
827 mutt_pattern_free (&curlist);
830 /* compile the sub-expression */
831 buf = mutt_substrdup (ps.dptr + 1, p);
832 if ((tmp = mutt_pattern_comp (buf, flags, err)) == NULL)
835 mutt_pattern_free (&curlist);
845 tmp->alladdr |= alladdr;
848 ps.dptr = p + 1; /* restore location */
851 snprintf (err->data, err->dsize, _("error in pattern at: %s"), ps.dptr);
852 mutt_pattern_free (&curlist);
858 strfcpy (err->data, _("empty pattern"), err->dsize);
863 tmp = new_pattern ();
864 tmp->op = or ? M_OR : M_AND;
865 tmp->child = curlist;
872 perform_and (pattern_t *pat, pattern_exec_flag flags, CONTEXT *ctx, HEADER *hdr)
874 for (; pat; pat = pat->next)
875 if (mutt_pattern_exec (pat, flags, ctx, hdr) <= 0)
881 perform_or (struct pattern_t *pat, pattern_exec_flag flags, CONTEXT *ctx, HEADER *hdr)
883 for (; pat; pat = pat->next)
884 if (mutt_pattern_exec (pat, flags, ctx, hdr) > 0)
889 static int match_adrlist (regex_t *rx, int match_personal, int alladdr,
898 for (a = va_arg (ap, ADDRESS *) ; a ; a = a->next)
901 ((a->mailbox && regexec (rx, a->mailbox, 0, NULL, 0) == 0) ||
902 (match_personal && a->personal &&
903 regexec (rx, a->personal, 0, NULL, 0) == 0)))
906 return (! alladdr); /* Found match, or non-match if alladdr */
911 return alladdr; /* No matches, or all matches if alladdr */
914 static int match_reference (regex_t *rx, LIST *refs)
916 for (; refs; refs = refs->next)
917 if (regexec (rx, refs->data, 0, NULL, 0) == 0)
922 int mutt_is_list_recipient (int alladdr, ADDRESS *a1, ADDRESS *a2)
924 for (; a1 ; a1 = a1->next)
925 if (alladdr ^ mutt_is_subscribed_list (a1))
927 for (; a2 ; a2 = a2->next)
928 if (alladdr ^ mutt_is_subscribed_list (a2))
933 int mutt_is_list_cc (int alladdr, ADDRESS *a1, ADDRESS *a2)
935 for (; a1 ; a1 = a1->next)
936 if (alladdr ^ mutt_is_mail_list (a1))
938 for (; a2 ; a2 = a2->next)
939 if (alladdr ^ mutt_is_mail_list (a2))
944 static int match_user (int alladdr, ADDRESS *a1, ADDRESS *a2)
946 for (; a1 ; a1 = a1->next)
947 if (alladdr ^ mutt_addr_is_user (a1))
949 for (; a2 ; a2 = a2->next)
950 if (alladdr ^ mutt_addr_is_user (a2))
956 M_MATCH_FULL_ADDRESS match both personal and machine address */
958 mutt_pattern_exec (struct pattern_t *pat, pattern_exec_flag flags, CONTEXT *ctx, HEADER *h)
965 return (pat->not ^ (perform_and (pat->child, flags, ctx, h) > 0));
967 return (pat->not ^ (perform_or (pat->child, flags, ctx, h) > 0));
971 return (pat->not ^ h->expired);
973 return (pat->not ^ h->superseded);
975 return (pat->not ^ h->flagged);
977 return (pat->not ^ h->tagged);
979 return (pat->not ? h->old || h->read : !(h->old || h->read));
981 return (pat->not ? h->read : !h->read);
983 return (pat->not ^ h->replied);
985 return (pat->not ? (!h->old || h->read) : (h->old && !h->read));
987 return (pat->not ^ h->read);
989 return (pat->not ^ h->deleted);
991 return (pat->not ^ (h->msgno >= pat->min - 1 && (pat->max == M_MAXRANGE ||
992 h->msgno <= pat->max - 1)));
994 return (pat->not ^ (h->date_sent >= pat->min && h->date_sent <= pat->max));
995 case M_DATE_RECEIVED:
996 return (pat->not ^ (h->received >= pat->min && h->received <= pat->max));
1000 return (pat->not ^ msg_search (ctx, pat->rx, buf, sizeof (buf), pat->op, h->msgno));
1002 return (pat->not ^ match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
1003 pat->alladdr, 1, h->env->sender));
1005 return (pat->not ^ match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
1006 pat->alladdr, 1, h->env->from));
1008 return (pat->not ^ match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
1009 pat->alladdr, 1, h->env->to));
1011 return (pat->not ^ match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
1012 pat->alladdr, 1, h->env->cc));
1014 return (pat->not ^ (h->env && h->env->subject && regexec (pat->rx, h->env->subject, 0, NULL, 0) == 0));
1016 return (pat->not ^ (h->env && h->env->message_id && regexec (pat->rx, h->env->message_id, 0, NULL, 0) == 0));
1018 return (pat->not ^ (h->score >= pat->min && (pat->max == M_MAXRANGE ||
1019 h->score <= pat->max)));
1021 return (pat->not ^ (h->content->length >= pat->min && (pat->max == M_MAXRANGE || h->content->length <= pat->max)));
1023 return (pat->not ^ match_reference (pat->rx, h->env->references));
1025 return (pat->not ^ (h->env && match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
1026 pat->alladdr, 4, h->env->from,
1027 h->env->sender, h->env->to, h->env->cc)));
1029 return (pat->not ^ (h->env && match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
1030 pat->alladdr, 2, h->env->to, h->env->cc)));
1032 return (pat->not ^ (h->env && mutt_is_list_recipient (pat->alladdr, h->env->to, h->env->cc)));
1033 case M_PERSONAL_RECIP:
1034 return (pat->not ^ (h->env && match_user (pat->alladdr, h->env->to, h->env->cc)));
1035 case M_PERSONAL_FROM:
1036 return (pat->not ^ (h->env && match_user (pat->alladdr, h->env->from, NULL)));
1038 return (pat->not ^ (h->collapsed && h->num_hidden > 1));
1042 return (pat->not ^ ((h->security & SIGN) ? 1 : 0));
1043 case M_CRYPT_VERIFIED:
1046 return (pat->not ^ ((h->security & GOODSIGN) ? 1 : 0));
1047 case M_CRYPT_ENCRYPT:
1050 return (pat->not ^ ((h->security & ENCRYPT) ? 1 : 0));
1052 if (!(WithCrypto & APPLICATION_PGP))
1054 return (pat->not ^ ((h->security & APPLICATION_PGP) && (h->security & PGPKEY)));
1056 return (pat->not ^ (h->env->x_label && regexec (pat->rx, h->env->x_label, 0, NULL, 0) == 0));
1058 return (pat->not ^ (h->env->spam && h->env->spam->data && regexec (pat->rx, h->env->spam->data, 0, NULL, 0) == 0));
1060 return (pat->not ^ (h->thread && h->thread->duplicate_thread));
1061 case M_UNREFERENCED:
1062 return (pat->not ^ (h->thread && !h->thread->child));
1065 return (pat->not ^ (h->env->newsgroups && regexec (pat->rx, h->env->newsgroups, 0, NULL, 0) == 0));
1068 mutt_error (_("error: unknown op %d (report this error)."), pat->op);
1072 static void quote_simple(char *tmp, size_t len, const char *p)
1077 while (*p && i < len - 3)
1079 if (*p == '\\' || *p == '"')
1087 /* convert a simple search into a real request */
1088 void mutt_check_simple (char *s, size_t len, const char *simple)
1090 char tmp[LONG_STRING];
1092 /* XXX - is ascii_strcasecmp() right here, or should we use locale's
1096 if (!strchr (s, '~')) /* yup, so spoof a real request */
1098 /* convert old tokens into the new format */
1099 if (ascii_strcasecmp ("all", s) == 0 ||
1100 !mutt_strcmp ("^", s) || !mutt_strcmp (".", s)) /* ~A is more efficient */
1101 strfcpy (s, "~A", len);
1102 else if (ascii_strcasecmp ("del", s) == 0)
1103 strfcpy (s, "~D", len);
1104 else if (ascii_strcasecmp ("flag", s) == 0)
1105 strfcpy (s, "~F", len);
1106 else if (ascii_strcasecmp ("new", s) == 0)
1107 strfcpy (s, "~N", len);
1108 else if (ascii_strcasecmp ("old", s) == 0)
1109 strfcpy (s, "~O", len);
1110 else if (ascii_strcasecmp ("repl", s) == 0)
1111 strfcpy (s, "~Q", len);
1112 else if (ascii_strcasecmp ("read", s) == 0)
1113 strfcpy (s, "~R", len);
1114 else if (ascii_strcasecmp ("tag", s) == 0)
1115 strfcpy (s, "~T", len);
1116 else if (ascii_strcasecmp ("unread", s) == 0)
1117 strfcpy (s, "~U", len);
1120 quote_simple (tmp, sizeof(tmp), s);
1121 mutt_expand_fmt (s, len, simple, tmp);
1126 int mutt_pattern_func (int op, char *prompt)
1129 char buf[LONG_STRING] = "", *simple, error[STRING];
1133 strfcpy (buf, NONULL (Context->pattern), sizeof (buf));
1134 if (prompt || op != M_LIMIT)
1135 if (mutt_get_field (prompt, buf, sizeof (buf), M_PATTERN | M_CLEAR) != 0 || !buf[0])
1138 mutt_message _("Compiling search pattern...");
1140 simple = safe_strdup (buf);
1141 mutt_check_simple (buf, sizeof (buf), NONULL (SimpleSearch));
1144 err.dsize = sizeof (error);
1145 if ((pat = mutt_pattern_comp (buf, M_FULL_MSG, &err)) == NULL)
1148 mutt_error ("%s", err.data);
1152 mutt_message _("Executing command on matching messages...");
1154 #define THIS_BODY Context->hdrs[i]->content
1158 Context->vcount = 0;
1160 Context->collapsed = 0;
1162 for (i = 0; i < Context->msgcount; i++)
1164 /* new limit pattern implicitly uncollapses all threads */
1165 Context->hdrs[i]->virtual = -1;
1166 Context->hdrs[i]->limited = 0;
1167 Context->hdrs[i]->collapsed = 0;
1168 Context->hdrs[i]->num_hidden = 0;
1169 if (mutt_pattern_exec (pat, M_MATCH_FULL_ADDRESS, Context, Context->hdrs[i]))
1171 Context->hdrs[i]->virtual = Context->vcount;
1172 Context->hdrs[i]->limited = 1;
1173 Context->v2r[Context->vcount] = i;
1175 Context->vsize+=THIS_BODY->length + THIS_BODY->offset -
1176 THIS_BODY->hdr_offset;
1182 for (i = 0; i < Context->vcount; i++)
1184 if (mutt_pattern_exec (pat, M_MATCH_FULL_ADDRESS, Context, Context->hdrs[Context->v2r[i]]))
1189 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_PURGED,
1192 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_DELETE,
1197 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_TAG,
1207 mutt_clear_error ();
1211 FREE (&Context->pattern);
1212 if (Context->limit_pattern)
1213 mutt_pattern_free (&Context->limit_pattern);
1214 if (!Context->vcount)
1216 mutt_error _("No messages matched criteria.");
1218 Context->vcount = Context->msgcount;
1219 /* restore full display */
1220 for (i = 0; i < Context->msgcount; i++)
1222 Context->hdrs[i]->virtual = i;
1223 Context->v2r[i] = i;
1227 else if (mutt_strncmp (buf, "~A", 2) != 0)
1229 Context->pattern = simple;
1230 simple = NULL; /* don't clobber it */
1231 Context->limit_pattern = mutt_pattern_comp (buf, M_FULL_MSG, &err);
1235 mutt_pattern_free (&pat);
1239 int mutt_search_command (int cur, int op)
1243 char temp[LONG_STRING];
1249 if (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE)
1251 strfcpy (buf, LastSearch, sizeof (buf));
1252 if (mutt_get_field ((op == OP_SEARCH) ? _("Search for: ") :
1253 _("Reverse search for: "), buf, sizeof (buf),
1254 M_CLEAR | M_PATTERN) != 0 || !buf[0])
1257 if (op == OP_SEARCH)
1258 unset_option (OPTSEARCHREVERSE);
1260 set_option (OPTSEARCHREVERSE);
1262 /* compare the *expanded* version of the search pattern in case
1263 $simple_search has changed while we were searching */
1264 strfcpy (temp, buf, sizeof (temp));
1265 mutt_check_simple (temp, sizeof (temp), NONULL (SimpleSearch));
1267 if (!SearchPattern || mutt_strcmp (temp, LastSearchExpn))
1269 set_option (OPTSEARCHINVALID);
1270 strfcpy (LastSearch, buf, sizeof (LastSearch));
1271 mutt_message _("Compiling search pattern...");
1272 mutt_pattern_free (&SearchPattern);
1274 err.dsize = sizeof (error);
1275 if ((SearchPattern = mutt_pattern_comp (temp, M_FULL_MSG, &err)) == NULL)
1277 mutt_error ("%s", error);
1280 mutt_clear_error ();
1283 else if (!SearchPattern)
1285 mutt_error _("No search pattern.");
1289 if (option (OPTSEARCHINVALID))
1291 for (i = 0; i < Context->msgcount; i++)
1292 Context->hdrs[i]->searched = 0;
1293 unset_option (OPTSEARCHINVALID);
1296 incr = (option (OPTSEARCHREVERSE)) ? -1 : 1;
1297 if (op == OP_SEARCH_OPPOSITE)
1300 for (i = cur + incr, j = 0 ; j != Context->vcount; j++)
1302 if (i > Context->vcount - 1)
1305 if (option (OPTWRAPSEARCH))
1306 mutt_message _("Search wrapped to top.");
1309 mutt_message _("Search hit bottom without finding match");
1315 i = Context->vcount - 1;
1316 if (option (OPTWRAPSEARCH))
1317 mutt_message _("Search wrapped to bottom.");
1320 mutt_message _("Search hit top without finding match");
1325 h = Context->hdrs[Context->v2r[i]];
1328 /* if we've already evaulated this message, use the cached value */
1334 /* remember that we've already searched this message */
1336 if ((h->matched = (mutt_pattern_exec (SearchPattern, M_MATCH_FULL_ADDRESS, Context, h) > 0)))
1342 mutt_error _("Search interrupted.");
1350 mutt_error _("Not found.");