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.
32 #include "mutt_crypt.h"
34 static int eat_regexp (pattern_t *pat, BUFFER *, BUFFER *);
35 static int eat_date (pattern_t *pat, BUFFER *, BUFFER *);
36 static int eat_range (pattern_t *pat, BUFFER *, BUFFER *);
40 int tag; /* character used to represent this op */
41 int op; /* operation to perform */
43 int (*eat_arg) (pattern_t *, BUFFER *, BUFFER *);
47 { 'A', M_ALL, 0, NULL },
48 { 'b', M_BODY, M_FULL_MSG, eat_regexp },
49 { 'B', M_WHOLE_MSG, M_FULL_MSG, eat_regexp },
50 { 'c', M_CC, 0, eat_regexp },
51 { 'C', M_RECIPIENT, 0, eat_regexp },
52 { 'd', M_DATE, 0, eat_date },
53 { 'D', M_DELETED, 0, NULL },
54 { 'e', M_SENDER, 0, eat_regexp },
55 { 'E', M_EXPIRED, 0, NULL },
56 { 'f', M_FROM, 0, eat_regexp },
57 { 'F', M_FLAG, 0, NULL },
58 { 'g', M_CRYPT_SIGN, 0, NULL },
59 { 'G', M_CRYPT_ENCRYPT, 0, NULL },
60 { 'h', M_HEADER, M_FULL_MSG, eat_regexp },
61 { 'H', M_HORMEL, 0, eat_regexp },
62 { 'i', M_ID, 0, eat_regexp },
63 { 'k', M_PGP_KEY, 0, NULL },
64 { 'L', M_ADDRESS, 0, eat_regexp },
65 { 'l', M_LIST, 0, NULL },
66 { 'm', M_MESSAGE, 0, eat_range },
67 { 'n', M_SCORE, 0, eat_range },
68 { 'N', M_NEW, 0, NULL },
69 { 'O', M_OLD, 0, NULL },
70 { 'p', M_PERSONAL_RECIP, 0, NULL },
71 { 'P', M_PERSONAL_FROM, 0, NULL },
72 { 'Q', M_REPLIED, 0, NULL },
73 { 'R', M_READ, 0, NULL },
74 { 'r', M_DATE_RECEIVED, 0, eat_date },
75 { 's', M_SUBJECT, 0, eat_regexp },
76 { 'S', M_SUPERSEDED, 0, NULL },
77 { 'T', M_TAG, 0, NULL },
78 { 't', M_TO, 0, eat_regexp },
79 { 'U', M_UNREAD, 0, NULL },
80 { 'v', M_COLLAPSED, 0, NULL },
81 { 'V', M_CRYPT_VERIFIED, 0, NULL },
83 { 'w', M_NEWSGROUPS, 0, eat_regexp },
85 { 'x', M_REFERENCE, 0, eat_regexp },
86 { 'y', M_XLABEL, 0, eat_regexp },
87 { 'z', M_SIZE, 0, eat_range },
88 { '=', M_DUPLICATED, 0, NULL },
89 { '$', M_UNREFERENCED, 0, NULL },
93 static pattern_t *SearchPattern = NULL; /* current search pattern */
94 static char LastSearch[STRING] = { 0 }; /* last pattern searched for */
95 static char LastSearchExpn[LONG_STRING] = { 0 }; /* expanded version of
100 /* constants for parse_date_range() */
101 #define M_PDR_NONE 0x0000
102 #define M_PDR_MINUS 0x0001
103 #define M_PDR_PLUS 0x0002
104 #define M_PDR_WINDOW 0x0004
105 #define M_PDR_ABSOLUTE 0x0008
106 #define M_PDR_DONE 0x0010
107 #define M_PDR_ERROR 0x0100
108 #define M_PDR_ERRORDONE (M_PDR_ERROR | M_PDR_DONE)
111 int mutt_getvaluebychar (char ch, struct mapping_t *table)
115 for (i = 0; table[i].name; i++)
117 if (ch == table[i].name[0])
118 return table[i].value;
124 /* if no uppercase letters are given, do a case-insensitive search */
125 int mutt_which_case (const char *s)
129 if (isalpha ((unsigned char) *s) && isupper ((unsigned char) *s))
130 return 0; /* case-sensitive */
133 return REG_ICASE; /* case-insensitive */
137 msg_search (CONTEXT *ctx, regex_t *rx, char *buf, size_t blen, int op, int msgno)
139 char tempfile[_POSIX_PATH_MAX];
146 HEADER *h = ctx->hdrs[msgno];
148 if ((msg = mx_open_message (ctx, msgno)) != NULL)
150 if (option (OPTTHOROUGHSRC))
152 /* decode the header / body */
153 memset (&s, 0, sizeof (s));
155 s.flags = M_CHARCONV;
156 mutt_mktemp (tempfile);
157 if ((s.fpout = safe_fopen (tempfile, "w+")) == NULL)
159 mutt_perror (tempfile);
164 mutt_copy_header (msg->fp, h, s.fpout, CH_FROM | CH_DECODE, NULL);
168 mutt_parse_mime_message (ctx, h);
170 if (WithCrypto && (h->security & ENCRYPT)
171 && !crypt_valid_passphrase(h->security))
173 mx_close_message (&msg);
182 fseek (msg->fp, h->offset, 0);
183 mutt_body_handler (h->content, &s);
189 fstat (fileno (fp), &st);
190 lng = (long) st.st_size;
194 /* raw header / body */
198 fseek (fp, h->offset, 0);
199 lng = h->content->offset - h->offset;
204 fseek (fp, h->content->offset, 0);
205 lng += h->content->length;
209 /* search the file "fp" */
212 if (fgets (buf, blen - 1, fp) == NULL)
213 break; /* don't loop forever */
214 if (regexec (rx, buf, 0, NULL, 0) == 0)
219 lng -= mutt_strlen (buf);
222 mx_close_message (&msg);
224 if (option (OPTTHOROUGHSRC))
234 int eat_regexp (pattern_t *pat, BUFFER *s, BUFFER *err)
239 memset (&buf, 0, sizeof (buf));
240 if (mutt_extract_token (&buf, s, M_TOKEN_PATTERN | M_TOKEN_COMMENT) != 0 ||
243 snprintf (err->data, err->dsize, _("Error in expression: %s"), s->dptr);
246 pat->rx = safe_malloc (sizeof (regex_t));
247 r = REGCOMP (pat->rx, buf.data, REG_NEWLINE | REG_NOSUB | mutt_which_case (buf.data));
251 regerror (r, pat->rx, err->data, err->dsize);
259 int eat_range (pattern_t *pat, BUFFER *s, BUFFER *err)
262 int do_exclusive = 0;
266 * If simple_search is set to "~m %s", the range will have double quotes
276 if ((*s->dptr != '-') && (*s->dptr != '<'))
281 pat->max = M_MAXRANGE;
282 pat->min = strtol (s->dptr + 1, &tmp, 0) + 1; /* exclusive range */
285 pat->min = strtol (s->dptr, &tmp, 0);
286 if (toupper ((unsigned char) *tmp) == 'K') /* is there a prefix? */
291 else if (toupper ((unsigned char) *tmp) == 'M')
316 if (isdigit ((unsigned char) *tmp))
319 pat->max = strtol (tmp, &tmp, 0);
320 if (toupper ((unsigned char) *tmp) == 'K')
325 else if (toupper ((unsigned char) *tmp) == 'M')
334 pat->max = M_MAXRANGE;
336 if (skip_quote && *tmp == '"')
344 static const char *getDate (const char *s, struct tm *t, BUFFER *err)
347 time_t now = time (NULL);
348 struct tm *tm = localtime (&now);
350 t->tm_mday = strtol (s, &p, 10);
351 if (t->tm_mday < 1 || t->tm_mday > 31)
353 snprintf (err->data, err->dsize, _("Invalid day of month: %s"), s);
358 /* fill in today's month and year */
359 t->tm_mon = tm->tm_mon;
360 t->tm_year = tm->tm_year;
364 t->tm_mon = strtol (p, &p, 10) - 1;
365 if (t->tm_mon < 0 || t->tm_mon > 11)
367 snprintf (err->data, err->dsize, _("Invalid month: %s"), p);
372 t->tm_year = tm->tm_year;
376 t->tm_year = strtol (p, &p, 10);
377 if (t->tm_year < 70) /* year 2000+ */
379 else if (t->tm_year > 1900)
388 static const char *get_offset (struct tm *tm, const char *s, int sign)
391 int offset = strtol (s, &ps, 0);
392 if ((sign < 0 && offset > 0) || (sign > 0 && offset < 0))
398 tm->tm_year += offset;
401 tm->tm_mon += offset;
404 tm->tm_mday += 7 * offset;
407 tm->tm_mday += offset;
412 mutt_normalize_time (tm);
416 static void adjust_date_range (struct tm *min, struct tm *max)
418 if (min->tm_year > max->tm_year
419 || (min->tm_year == max->tm_year && min->tm_mon > max->tm_mon)
420 || (min->tm_year == max->tm_year && min->tm_mon == max->tm_mon
421 && min->tm_mday > max->tm_mday))
426 min->tm_year = max->tm_year;
430 min->tm_mon = max->tm_mon;
434 min->tm_mday = max->tm_mday;
437 min->tm_hour = min->tm_min = min->tm_sec = 0;
439 max->tm_min = max->tm_sec = 59;
443 static const char * parse_date_range (const char* pc, struct tm *min,
444 struct tm *max, int haveMin, struct tm *baseMin, BUFFER *err)
446 int flag = M_PDR_NONE;
447 while (*pc && ((flag & M_PDR_DONE) == 0))
456 /* try a range of absolute date minus offset of Ndwmy */
457 pt = get_offset (min, pc, -1);
460 if (flag == M_PDR_NONE)
461 { /* nothing yet and no offset parsed => absolute date? */
462 if (!getDate (pc, max, err))
463 flag |= (M_PDR_ABSOLUTE | M_PDR_ERRORDONE); /* done bad */
466 /* reestablish initial base minimum if not specified */
468 memcpy (min, baseMin, sizeof(struct tm));
469 flag |= (M_PDR_ABSOLUTE | M_PDR_DONE); /* done good */
473 flag |= M_PDR_ERRORDONE;
478 if (flag == M_PDR_NONE && !haveMin)
479 { /* the very first "-3d" without a previous absolute date */
480 max->tm_year = min->tm_year;
481 max->tm_mon = min->tm_mon;
482 max->tm_mday = min->tm_mday;
489 { /* enlarge plusRange */
490 pt = get_offset (max, pc, 1);
492 flag |= M_PDR_ERRORDONE;
501 { /* enlarge window in both directions */
502 pt = get_offset (min, pc, -1);
504 flag |= M_PDR_ERRORDONE;
507 pc = get_offset (max, pc, 1);
508 flag |= M_PDR_WINDOW;
513 flag |= M_PDR_ERRORDONE;
517 if ((flag & M_PDR_ERROR) && !(flag & M_PDR_ABSOLUTE))
518 { /* getDate has its own error message, don't overwrite it here */
519 snprintf (err->data, err->dsize, _("Invalid relative date: %s"), pc-1);
521 return ((flag & M_PDR_ERROR) ? NULL : pc);
524 static int eat_date (pattern_t *pat, BUFFER *s, BUFFER *err)
529 memset (&buffer, 0, sizeof (buffer));
530 if (mutt_extract_token (&buffer, s, M_TOKEN_COMMENT | M_TOKEN_PATTERN) != 0
533 strfcpy (err->data, _("error in expression"), err->dsize);
537 memset (&min, 0, sizeof (min));
538 /* the `0' time is Jan 1, 1970 UTC, so in order to prevent a negative time
539 when doing timezone conversion, we use Jan 2, 1970 UTC as the base
544 memset (&max, 0, sizeof (max));
546 /* Arbitrary year in the future. Don't set this too high
547 or mutt_mktime() returns something larger than will
548 fit in a time_t on some systems */
556 if (strchr ("<>=", buffer.data[0]))
558 /* offset from current time
559 <3d less than three days ago
560 >3d more than three days ago
561 =3d exactly three days ago */
562 time_t now = time (NULL);
563 struct tm *tm = localtime (&now);
566 if (buffer.data[0] == '<')
568 memcpy (&min, tm, sizeof (min));
573 memcpy (&max, tm, sizeof (max));
576 if (buffer.data[0] == '=')
580 tm->tm_min = tm->tm_sec = 59;
582 /* force negative offset */
583 get_offset (tm, buffer.data + 1, -1);
587 /* start at the beginning of the day in question */
588 memcpy (&min, &max, sizeof (max));
589 min.tm_hour = min.tm_sec = min.tm_min = 0;
594 const char *pc = buffer.data;
597 int untilNow = FALSE;
598 if (isdigit ((unsigned char)*pc))
600 /* mininum date specified */
601 if ((pc = getDate (pc, &min, err)) == NULL)
610 const char *pt = pc + 1;
612 untilNow = (*pt == '\0');
617 { /* max date or relative range/window */
622 { /* save base minimum and set current date, e.g. for "-3d+1d" */
623 time_t now = time (NULL);
624 struct tm *tm = localtime (&now);
625 memcpy (&baseMin, &min, sizeof(baseMin));
626 memcpy (&min, tm, sizeof (min));
627 min.tm_hour = min.tm_sec = min.tm_min = 0;
630 /* preset max date for relative offsets,
631 if nothing follows we search for messages on a specific day */
632 max.tm_year = min.tm_year;
633 max.tm_mon = min.tm_mon;
634 max.tm_mday = min.tm_mday;
636 if (!parse_date_range (pc, &min, &max, haveMin, &baseMin, err))
637 { /* bail out on any parsing error */
644 /* Since we allow two dates to be specified we'll have to adjust that. */
645 adjust_date_range (&min, &max);
647 pat->min = mutt_mktime (&min, 1);
648 pat->max = mutt_mktime (&max, 1);
655 static struct pattern_flags *lookup_tag (char tag)
659 for (i = 0; Flags[i].tag; i++)
660 if (Flags[i].tag == tag)
665 static /* const */ char *find_matching_paren (/* const */ char *s)
683 void mutt_pattern_free (pattern_t **pat)
698 mutt_pattern_free (&tmp->child);
703 pattern_t *mutt_pattern_comp (/* const */ char *s, int flags, BUFFER *err)
705 pattern_t *curlist = NULL;
707 pattern_t *last = NULL;
711 int implicit = 1; /* used to detect logical AND operator */
712 struct pattern_flags *entry;
717 memset (&ps, 0, sizeof (ps));
719 ps.dsize = mutt_strlen (s);
739 snprintf (err->data, err->dsize, _("error in pattern at: %s"), ps.dptr);
744 /* A & B | C == (A & B) | C */
745 tmp = new_pattern ();
747 tmp->child = curlist;
763 /* A | B & C == (A | B) & C */
764 tmp = new_pattern ();
766 tmp->child = curlist;
772 tmp = new_pattern ();
774 tmp->alladdr = alladdr;
784 ps.dptr++; /* move past the ~ */
785 if ((entry = lookup_tag (*ps.dptr)) == NULL)
787 snprintf (err->data, err->dsize, _("%c: invalid command"), *ps.dptr);
788 mutt_pattern_free (&curlist);
791 if (entry->class && (flags & entry->class) == 0)
793 snprintf (err->data, err->dsize, _("%c: not supported in this mode"), *ps.dptr);
794 mutt_pattern_free (&curlist);
799 ps.dptr++; /* eat the operator and any optional whitespace */
806 snprintf (err->data, err->dsize, _("missing parameter"));
807 mutt_pattern_free (&curlist);
810 if (entry->eat_arg (tmp, &ps, err) == -1)
812 mutt_pattern_free (&curlist);
819 p = find_matching_paren (ps.dptr + 1);
822 snprintf (err->data, err->dsize, _("mismatched parenthesis: %s"), ps.dptr);
823 mutt_pattern_free (&curlist);
826 /* compile the sub-expression */
827 buf = mutt_substrdup (ps.dptr + 1, p);
828 if ((tmp = mutt_pattern_comp (buf, flags, err)) == NULL)
831 mutt_pattern_free (&curlist);
841 tmp->alladdr |= alladdr;
844 ps.dptr = p + 1; /* restore location */
847 snprintf (err->data, err->dsize, _("error in pattern at: %s"), ps.dptr);
848 mutt_pattern_free (&curlist);
854 strfcpy (err->data, _("empty pattern"), err->dsize);
859 tmp = new_pattern ();
860 tmp->op = or ? M_OR : M_AND;
861 tmp->child = curlist;
868 perform_and (pattern_t *pat, pattern_exec_flag flags, CONTEXT *ctx, HEADER *hdr)
870 for (; pat; pat = pat->next)
871 if (mutt_pattern_exec (pat, flags, ctx, hdr) <= 0)
877 perform_or (struct pattern_t *pat, pattern_exec_flag flags, CONTEXT *ctx, HEADER *hdr)
879 for (; pat; pat = pat->next)
880 if (mutt_pattern_exec (pat, flags, ctx, hdr) > 0)
885 static int match_adrlist (regex_t *rx, int match_personal, int alladdr,
894 for (a = va_arg (ap, ADDRESS *) ; a ; a = a->next)
897 ((a->mailbox && regexec (rx, a->mailbox, 0, NULL, 0) == 0) ||
898 (match_personal && a->personal &&
899 regexec (rx, a->personal, 0, NULL, 0) == 0)))
902 return (! alladdr); /* Found match, or non-match if alladdr */
907 return alladdr; /* No matches, or all matches if alladdr */
910 static int match_reference (regex_t *rx, LIST *refs)
912 for (; refs; refs = refs->next)
913 if (regexec (rx, refs->data, 0, NULL, 0) == 0)
918 int mutt_is_list_recipient (int alladdr, ADDRESS *a1, ADDRESS *a2)
920 for (; a1 ; a1 = a1->next)
921 if (alladdr ^ mutt_is_subscribed_list (a1))
923 for (; a2 ; a2 = a2->next)
924 if (alladdr ^ mutt_is_subscribed_list (a2))
929 int mutt_is_list_cc (int alladdr, ADDRESS *a1, ADDRESS *a2)
931 for (; a1 ; a1 = a1->next)
932 if (alladdr ^ mutt_is_mail_list (a1))
934 for (; a2 ; a2 = a2->next)
935 if (alladdr ^ mutt_is_mail_list (a2))
940 static int match_user (int alladdr, ADDRESS *a1, ADDRESS *a2)
942 for (; a1 ; a1 = a1->next)
943 if (alladdr ^ mutt_addr_is_user (a1))
945 for (; a2 ; a2 = a2->next)
946 if (alladdr ^ mutt_addr_is_user (a2))
952 M_MATCH_FULL_ADDRESS match both personal and machine address */
954 mutt_pattern_exec (struct pattern_t *pat, pattern_exec_flag flags, CONTEXT *ctx, HEADER *h)
961 return (pat->not ^ (perform_and (pat->child, flags, ctx, h) > 0));
963 return (pat->not ^ (perform_or (pat->child, flags, ctx, h) > 0));
967 return (pat->not ^ h->expired);
969 return (pat->not ^ h->superseded);
971 return (pat->not ^ h->flagged);
973 return (pat->not ^ h->tagged);
975 return (pat->not ? h->old || h->read : !(h->old || h->read));
977 return (pat->not ? h->read : !h->read);
979 return (pat->not ^ h->replied);
981 return (pat->not ? (!h->old || h->read) : (h->old && !h->read));
983 return (pat->not ^ h->read);
985 return (pat->not ^ h->deleted);
987 return (pat->not ^ (h->msgno >= pat->min - 1 && (pat->max == M_MAXRANGE ||
988 h->msgno <= pat->max - 1)));
990 return (pat->not ^ (h->date_sent >= pat->min && h->date_sent <= pat->max));
991 case M_DATE_RECEIVED:
992 return (pat->not ^ (h->received >= pat->min && h->received <= pat->max));
996 return (pat->not ^ msg_search (ctx, pat->rx, buf, sizeof (buf), pat->op, h->msgno));
998 return (pat->not ^ match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
999 pat->alladdr, 1, h->env->sender));
1001 return (pat->not ^ match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
1002 pat->alladdr, 1, h->env->from));
1004 return (pat->not ^ match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
1005 pat->alladdr, 1, h->env->to));
1007 return (pat->not ^ match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
1008 pat->alladdr, 1, h->env->cc));
1010 return (pat->not ^ (h->env->subject && regexec (pat->rx, h->env->subject, 0, NULL, 0) == 0));
1012 return (pat->not ^ (h->env->message_id && regexec (pat->rx, h->env->message_id, 0, NULL, 0) == 0));
1014 return (pat->not ^ (h->score >= pat->min && (pat->max == M_MAXRANGE ||
1015 h->score <= pat->max)));
1017 return (pat->not ^ (h->content->length >= pat->min && (pat->max == M_MAXRANGE || h->content->length <= pat->max)));
1019 return (pat->not ^ match_reference (pat->rx, h->env->references));
1021 return (pat->not ^ match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
1022 pat->alladdr, 4, h->env->from,
1023 h->env->sender, h->env->to, h->env->cc));
1025 return (pat->not ^ match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
1026 pat->alladdr, 2, h->env->to, h->env->cc));
1028 return (pat->not ^ mutt_is_list_recipient (pat->alladdr, h->env->to, h->env->cc));
1029 case M_PERSONAL_RECIP:
1030 return (pat->not ^ match_user (pat->alladdr, h->env->to, h->env->cc));
1031 case M_PERSONAL_FROM:
1032 return (pat->not ^ match_user (pat->alladdr, h->env->from, NULL));
1034 return (pat->not ^ (h->collapsed && h->num_hidden > 1));
1038 return (pat->not ^ ((h->security & SIGN) ? 1 : 0));
1039 case M_CRYPT_VERIFIED:
1042 return (pat->not ^ ((h->security & GOODSIGN) ? 1 : 0));
1043 case M_CRYPT_ENCRYPT:
1046 return (pat->not ^ ((h->security & ENCRYPT) ? 1 : 0));
1048 if (!(WithCrypto & APPLICATION_PGP))
1050 return (pat->not ^ ((h->security & APPLICATION_PGP) && (h->security & PGPKEY)));
1052 return (pat->not ^ (h->env->x_label && regexec (pat->rx, h->env->x_label, 0, NULL, 0) == 0));
1054 return (pat->not ^ (h->env->spam && h->env->spam->data && regexec (pat->rx, h->env->spam->data, 0, NULL, 0) == 0));
1056 return (pat->not ^ (h->thread && h->thread->duplicate_thread));
1057 case M_UNREFERENCED:
1058 return (pat->not ^ (h->thread && !h->thread->child));
1061 return (pat->not ^ (h->env->newsgroups && regexec (pat->rx, h->env->newsgroups, 0, NULL, 0) == 0));
1064 mutt_error (_("error: unknown op %d (report this error)."), pat->op);
1068 static void quote_simple(char *tmp, size_t len, const char *p)
1073 while (*p && i < len - 3)
1075 if (*p == '\\' || *p == '"')
1083 /* convert a simple search into a real request */
1084 void mutt_check_simple (char *s, size_t len, const char *simple)
1086 char tmp[LONG_STRING];
1088 /* XXX - is ascii_strcasecmp() right here, or should we use locale's
1092 if (!strchr (s, '~')) /* yup, so spoof a real request */
1094 /* convert old tokens into the new format */
1095 if (ascii_strcasecmp ("all", s) == 0 ||
1096 !mutt_strcmp ("^", s) || !mutt_strcmp (".", s)) /* ~A is more efficient */
1097 strfcpy (s, "~A", len);
1098 else if (ascii_strcasecmp ("del", s) == 0)
1099 strfcpy (s, "~D", len);
1100 else if (ascii_strcasecmp ("flag", s) == 0)
1101 strfcpy (s, "~F", len);
1102 else if (ascii_strcasecmp ("new", s) == 0)
1103 strfcpy (s, "~N", len);
1104 else if (ascii_strcasecmp ("old", s) == 0)
1105 strfcpy (s, "~O", len);
1106 else if (ascii_strcasecmp ("repl", s) == 0)
1107 strfcpy (s, "~Q", len);
1108 else if (ascii_strcasecmp ("read", s) == 0)
1109 strfcpy (s, "~R", len);
1110 else if (ascii_strcasecmp ("tag", s) == 0)
1111 strfcpy (s, "~T", len);
1112 else if (ascii_strcasecmp ("unread", s) == 0)
1113 strfcpy (s, "~U", len);
1116 quote_simple (tmp, sizeof(tmp), s);
1117 mutt_expand_fmt (s, len, simple, tmp);
1122 int mutt_pattern_func (int op, char *prompt)
1125 char buf[LONG_STRING] = "", *simple, error[STRING];
1129 strfcpy (buf, NONULL (Context->pattern), sizeof (buf));
1130 if (prompt || op != M_LIMIT)
1131 if (mutt_get_field (prompt, buf, sizeof (buf), M_PATTERN | M_CLEAR) != 0 || !buf[0])
1134 mutt_message _("Compiling search pattern...");
1136 simple = safe_strdup (buf);
1137 mutt_check_simple (buf, sizeof (buf), NONULL (SimpleSearch));
1140 err.dsize = sizeof (error);
1141 if ((pat = mutt_pattern_comp (buf, M_FULL_MSG, &err)) == NULL)
1144 mutt_error ("%s", err.data);
1148 mutt_message _("Executing command on matching messages...");
1150 #define THIS_BODY Context->hdrs[i]->content
1154 Context->vcount = 0;
1156 Context->collapsed = 0;
1158 for (i = 0; i < Context->msgcount; i++)
1160 /* new limit pattern implicitly uncollapses all threads */
1161 Context->hdrs[i]->virtual = -1;
1162 Context->hdrs[i]->limited = 0;
1163 Context->hdrs[i]->collapsed = 0;
1164 Context->hdrs[i]->num_hidden = 0;
1165 if (mutt_pattern_exec (pat, M_MATCH_FULL_ADDRESS, Context, Context->hdrs[i]))
1167 Context->hdrs[i]->virtual = Context->vcount;
1168 Context->hdrs[i]->limited = 1;
1169 Context->v2r[Context->vcount] = i;
1171 Context->vsize+=THIS_BODY->length + THIS_BODY->offset -
1172 THIS_BODY->hdr_offset;
1178 for (i = 0; i < Context->vcount; i++)
1180 if (mutt_pattern_exec (pat, M_MATCH_FULL_ADDRESS, Context, Context->hdrs[Context->v2r[i]]))
1185 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_PURGED,
1188 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_DELETE,
1193 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_TAG,
1203 mutt_clear_error ();
1207 FREE (&Context->pattern);
1208 if (Context->limit_pattern)
1209 mutt_pattern_free (&Context->limit_pattern);
1210 if (!Context->vcount)
1212 mutt_error _("No messages matched criteria.");
1214 Context->vcount = Context->msgcount;
1215 /* restore full display */
1216 for (i = 0; i < Context->msgcount; i++)
1218 Context->hdrs[i]->virtual = i;
1219 Context->v2r[i] = i;
1223 else if (mutt_strncmp (buf, "~A", 2) != 0)
1225 Context->pattern = simple;
1226 simple = NULL; /* don't clobber it */
1227 Context->limit_pattern = mutt_pattern_comp (buf, M_FULL_MSG, &err);
1231 mutt_pattern_free (&pat);
1235 int mutt_search_command (int cur, int op)
1239 char temp[LONG_STRING];
1245 if (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE)
1247 strfcpy (buf, LastSearch, sizeof (buf));
1248 if (mutt_get_field ((op == OP_SEARCH) ? _("Search for: ") :
1249 _("Reverse search for: "), buf, sizeof (buf),
1250 M_CLEAR | M_PATTERN) != 0 || !buf[0])
1253 if (op == OP_SEARCH)
1254 unset_option (OPTSEARCHREVERSE);
1256 set_option (OPTSEARCHREVERSE);
1258 /* compare the *expanded* version of the search pattern in case
1259 $simple_search has changed while we were searching */
1260 strfcpy (temp, buf, sizeof (temp));
1261 mutt_check_simple (temp, sizeof (temp), NONULL (SimpleSearch));
1263 if (!SearchPattern || mutt_strcmp (temp, LastSearchExpn))
1265 set_option (OPTSEARCHINVALID);
1266 strfcpy (LastSearch, buf, sizeof (LastSearch));
1267 mutt_message _("Compiling search pattern...");
1268 mutt_pattern_free (&SearchPattern);
1270 err.dsize = sizeof (error);
1271 if ((SearchPattern = mutt_pattern_comp (temp, M_FULL_MSG, &err)) == NULL)
1273 mutt_error ("%s", error);
1276 mutt_clear_error ();
1279 else if (!SearchPattern)
1281 mutt_error _("No search pattern.");
1285 if (option (OPTSEARCHINVALID))
1287 for (i = 0; i < Context->msgcount; i++)
1288 Context->hdrs[i]->searched = 0;
1289 unset_option (OPTSEARCHINVALID);
1292 incr = (option (OPTSEARCHREVERSE)) ? -1 : 1;
1293 if (op == OP_SEARCH_OPPOSITE)
1296 for (i = cur + incr, j = 0 ; j != Context->vcount; j++)
1298 if (i > Context->vcount - 1)
1301 if (option (OPTWRAPSEARCH))
1302 mutt_message _("Search wrapped to top.");
1305 mutt_message _("Search hit bottom without finding match");
1311 i = Context->vcount - 1;
1312 if (option (OPTWRAPSEARCH))
1313 mutt_message _("Search wrapped to bottom.");
1316 mutt_message _("Search hit top without finding match");
1321 h = Context->hdrs[Context->v2r[i]];
1324 /* if we've already evaulated this message, use the cached value */
1330 /* remember that we've already searched this message */
1332 if ((h->matched = (mutt_pattern_exec (SearchPattern, M_MATCH_FULL_ADDRESS, Context, h) > 0)))
1338 mutt_error _("Search interrupted.");
1346 mutt_error _("Not found.");