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 *);
42 struct pattern_flags {
43 int tag; /* character used to represent this op */
44 int op; /* operation to perform */
46 int (*eat_arg) (pattern_t *, BUFFER *, BUFFER *);
49 'A', M_ALL, 0, NULL}, {
50 'b', M_BODY, M_FULL_MSG, eat_regexp}, {
51 'B', M_WHOLE_MSG, M_FULL_MSG, eat_regexp}, {
52 'c', M_CC, 0, eat_regexp}, {
53 'C', M_RECIPIENT, 0, eat_regexp}, {
54 'd', M_DATE, 0, eat_date}, {
55 'D', M_DELETED, 0, NULL}, {
56 'e', M_SENDER, 0, eat_regexp}, {
57 'E', M_EXPIRED, 0, NULL}, {
58 'f', M_FROM, 0, eat_regexp}, {
59 'F', M_FLAG, 0, NULL}, {
60 'g', M_CRYPT_SIGN, 0, NULL}, {
61 'G', M_CRYPT_ENCRYPT, 0, NULL}, {
62 'h', M_HEADER, M_FULL_MSG, eat_regexp}, {
63 'H', M_HORMEL, 0, eat_regexp}, {
64 'i', M_ID, 0, eat_regexp}, {
65 'k', M_PGP_KEY, 0, NULL}, {
66 'L', M_ADDRESS, 0, eat_regexp}, {
67 'l', M_LIST, 0, NULL}, {
68 'm', M_MESSAGE, 0, eat_range}, {
69 'n', M_SCORE, 0, eat_range}, {
70 'N', M_NEW, 0, NULL}, {
71 'O', M_OLD, 0, NULL}, {
72 'p', M_PERSONAL_RECIP, 0, NULL}, {
73 'P', M_PERSONAL_FROM, 0, NULL}, {
74 'Q', M_REPLIED, 0, NULL}, {
75 'R', M_READ, 0, NULL}, {
76 'r', M_DATE_RECEIVED, 0, eat_date}, {
77 's', M_SUBJECT, 0, eat_regexp}, {
78 'S', M_SUPERSEDED, 0, NULL}, {
79 'T', M_TAG, 0, NULL}, {
80 't', M_TO, 0, eat_regexp}, {
81 'U', M_UNREAD, 0, NULL}, {
82 'v', M_COLLAPSED, 0, NULL}, {
83 'V', M_CRYPT_VERIFIED, 0, NULL},
86 '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}, {
94 '*', M_REALNAME, 0, NULL}, {
98 static pattern_t *SearchPattern = NULL; /* current search pattern */
99 static char LastSearch[STRING] = { 0 }; /* last pattern searched for */
100 static char LastSearchExpn[LONG_STRING] = { 0 }; /* expanded version of
103 #define M_MAXRANGE -1
105 /* constants for parse_date_range() */
106 #define M_PDR_NONE 0x0000
107 #define M_PDR_MINUS 0x0001
108 #define M_PDR_PLUS 0x0002
109 #define M_PDR_WINDOW 0x0004
110 #define M_PDR_ABSOLUTE 0x0008
111 #define M_PDR_DONE 0x0010
112 #define M_PDR_ERROR 0x0100
113 #define M_PDR_ERRORDONE (M_PDR_ERROR | M_PDR_DONE)
116 int mutt_getvaluebychar (char ch, struct mapping_t *table)
120 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)
132 if (isalpha ((unsigned char) *s) && isupper ((unsigned char) *s))
133 return 0; /* case-sensitive */
136 return REG_ICASE; /* case-insensitive */
140 msg_search (CONTEXT * ctx, regex_t * rx, char *buf, size_t blen, int op,
143 char tempfile[_POSIX_PATH_MAX];
150 HEADER *h = ctx->hdrs[msgno];
152 if ((msg = mx_open_message (ctx, msgno)) != NULL) {
153 if (option (OPTTHOROUGHSRC)) {
154 /* decode the header / body */
155 memset (&s, 0, sizeof (s));
157 s.flags = M_CHARCONV;
158 mutt_mktemp (tempfile);
159 if ((s.fpout = safe_fopen (tempfile, "w+")) == NULL) {
160 mutt_perror (tempfile);
165 mutt_copy_header (msg->fp, h, s.fpout, CH_FROM | CH_DECODE, NULL);
167 if (op != M_HEADER) {
168 mutt_parse_mime_message (ctx, h);
170 if (WithCrypto && (h->security & ENCRYPT)
171 && !crypt_valid_passphrase (h->security)) {
172 mx_close_message (&msg);
180 fseek (msg->fp, h->offset, 0);
181 mutt_body_handler (h->content, &s);
187 fstat (fileno (fp), &st);
188 lng = (long) st.st_size;
191 /* raw header / body */
194 fseek (fp, h->offset, 0);
195 lng = h->content->offset - h->offset;
197 if (op != M_HEADER) {
199 fseek (fp, h->content->offset, 0);
200 lng += h->content->length;
204 /* search the file "fp" */
206 if (fgets (buf, blen - 1, fp) == NULL)
207 break; /* don't loop forever */
208 if (regexec (rx, buf, 0, NULL, 0) == 0) {
212 lng -= mutt_strlen (buf);
215 mx_close_message (&msg);
217 if (option (OPTTHOROUGHSRC)) {
226 int eat_regexp (pattern_t * pat, BUFFER * s, BUFFER * err)
231 memset (&buf, 0, sizeof (buf));
232 if (mutt_extract_token (&buf, s, M_TOKEN_PATTERN | M_TOKEN_COMMENT) != 0 ||
234 snprintf (err->data, err->dsize, _("Error in expression: %s"), s->dptr);
237 pat->rx = safe_malloc (sizeof (regex_t));
239 REGCOMP (pat->rx, buf.data,
240 REG_NEWLINE | REG_NOSUB | mutt_which_case (buf.data));
243 regerror (r, pat->rx, err->data, err->dsize);
251 int eat_range (pattern_t * pat, BUFFER * s, BUFFER * err)
254 int do_exclusive = 0;
258 * If simple_search is set to "~m %s", the range will have double quotes
261 if (*s->dptr == '"') {
267 if ((*s->dptr != '-') && (*s->dptr != '<')) {
269 if (*s->dptr == '>') {
270 pat->max = M_MAXRANGE;
271 pat->min = strtol (s->dptr + 1, &tmp, 0) + 1; /* exclusive range */
274 pat->min = strtol (s->dptr, &tmp, 0);
275 if (toupper ((unsigned char) *tmp) == 'K') { /* is there a prefix? */
279 else if (toupper ((unsigned char) *tmp) == 'M') {
283 if (*s->dptr == '>') {
300 if (isdigit ((unsigned char) *tmp)) {
302 pat->max = strtol (tmp, &tmp, 0);
303 if (toupper ((unsigned char) *tmp) == 'K') {
307 else if (toupper ((unsigned char) *tmp) == 'M') {
315 pat->max = M_MAXRANGE;
317 if (skip_quote && *tmp == '"')
325 static const char *getDate (const char *s, struct tm *t, BUFFER * err)
328 time_t now = time (NULL);
329 struct tm *tm = localtime (&now);
331 t->tm_mday = strtol (s, &p, 10);
332 if (t->tm_mday < 1 || t->tm_mday > 31) {
333 snprintf (err->data, err->dsize, _("Invalid day of month: %s"), s);
337 /* fill in today's month and year */
338 t->tm_mon = tm->tm_mon;
339 t->tm_year = tm->tm_year;
343 t->tm_mon = strtol (p, &p, 10) - 1;
344 if (t->tm_mon < 0 || t->tm_mon > 11) {
345 snprintf (err->data, err->dsize, _("Invalid month: %s"), p);
349 t->tm_year = tm->tm_year;
353 t->tm_year = strtol (p, &p, 10);
354 if (t->tm_year < 70) /* year 2000+ */
356 else if (t->tm_year > 1900)
365 static const char *get_offset (struct tm *tm, const char *s, int sign)
368 int offset = strtol (s, &ps, 0);
370 if ((sign < 0 && offset > 0) || (sign > 0 && offset < 0))
375 tm->tm_year += offset;
378 tm->tm_mon += offset;
381 tm->tm_mday += 7 * offset;
384 tm->tm_mday += offset;
389 mutt_normalize_time (tm);
393 static void adjust_date_range (struct tm *min, struct tm *max)
395 if (min->tm_year > max->tm_year
396 || (min->tm_year == max->tm_year && min->tm_mon > max->tm_mon)
397 || (min->tm_year == max->tm_year && min->tm_mon == max->tm_mon
398 && min->tm_mday > max->tm_mday)) {
402 min->tm_year = max->tm_year;
406 min->tm_mon = max->tm_mon;
410 min->tm_mday = max->tm_mday;
413 min->tm_hour = min->tm_min = min->tm_sec = 0;
415 max->tm_min = max->tm_sec = 59;
419 static const char *parse_date_range (const char *pc, struct tm *min,
420 struct tm *max, int haveMin,
421 struct tm *baseMin, BUFFER * err)
423 int flag = M_PDR_NONE;
425 while (*pc && ((flag & M_PDR_DONE) == 0)) {
433 /* try a range of absolute date minus offset of Ndwmy */
434 pt = get_offset (min, pc, -1);
436 if (flag == M_PDR_NONE) { /* nothing yet and no offset parsed => absolute date? */
437 if (!getDate (pc, max, err))
438 flag |= (M_PDR_ABSOLUTE | M_PDR_ERRORDONE); /* done bad */
440 /* reestablish initial base minimum if not specified */
442 memcpy (min, baseMin, sizeof (struct tm));
443 flag |= (M_PDR_ABSOLUTE | M_PDR_DONE); /* done good */
447 flag |= M_PDR_ERRORDONE;
451 if (flag == M_PDR_NONE && !haveMin) { /* the very first "-3d" without a previous absolute date */
452 max->tm_year = min->tm_year;
453 max->tm_mon = min->tm_mon;
454 max->tm_mday = min->tm_mday;
461 { /* enlarge plusRange */
462 pt = get_offset (max, pc, 1);
464 flag |= M_PDR_ERRORDONE;
472 { /* enlarge window in both directions */
473 pt = get_offset (min, pc, -1);
475 flag |= M_PDR_ERRORDONE;
477 pc = get_offset (max, pc, 1);
478 flag |= M_PDR_WINDOW;
483 flag |= M_PDR_ERRORDONE;
487 if ((flag & M_PDR_ERROR) && !(flag & M_PDR_ABSOLUTE)) { /* getDate has its own error message, don't overwrite it here */
488 snprintf (err->data, err->dsize, _("Invalid relative date: %s"), pc - 1);
490 return ((flag & M_PDR_ERROR) ? NULL : pc);
493 static int eat_date (pattern_t * pat, BUFFER * s, BUFFER * err)
498 memset (&buffer, 0, sizeof (buffer));
499 if (mutt_extract_token (&buffer, s, M_TOKEN_COMMENT | M_TOKEN_PATTERN) != 0
501 strfcpy (err->data, _("error in expression"), err->dsize);
505 memset (&min, 0, sizeof (min));
506 /* the `0' time is Jan 1, 1970 UTC, so in order to prevent a negative time
507 when doing timezone conversion, we use Jan 2, 1970 UTC as the base
512 memset (&max, 0, sizeof (max));
514 /* Arbitrary year in the future. Don't set this too high
515 or mutt_mktime() returns something larger than will
516 fit in a time_t on some systems */
524 if (strchr ("<>=", buffer.data[0])) {
525 /* offset from current time
526 <3d less than three days ago
527 >3d more than three days ago
528 =3d exactly three days ago */
529 time_t now = time (NULL);
530 struct tm *tm = localtime (&now);
533 if (buffer.data[0] == '<') {
534 memcpy (&min, tm, sizeof (min));
538 memcpy (&max, tm, sizeof (max));
541 if (buffer.data[0] == '=')
545 tm->tm_min = tm->tm_sec = 59;
547 /* force negative offset */
548 get_offset (tm, buffer.data + 1, -1);
551 /* start at the beginning of the day in question */
552 memcpy (&min, &max, sizeof (max));
553 min.tm_hour = min.tm_sec = min.tm_min = 0;
557 const char *pc = buffer.data;
560 int untilNow = FALSE;
562 if (isdigit ((unsigned char) *pc)) {
563 /* mininum date specified */
564 if ((pc = getDate (pc, &min, err)) == NULL) {
571 const char *pt = pc + 1;
574 untilNow = (*pt == '\0');
578 if (!untilNow) { /* max date or relative range/window */
582 if (!haveMin) { /* save base minimum and set current date, e.g. for "-3d+1d" */
583 time_t now = time (NULL);
584 struct tm *tm = localtime (&now);
586 memcpy (&baseMin, &min, sizeof (baseMin));
587 memcpy (&min, tm, sizeof (min));
588 min.tm_hour = min.tm_sec = min.tm_min = 0;
591 /* preset max date for relative offsets,
592 if nothing follows we search for messages on a specific day */
593 max.tm_year = min.tm_year;
594 max.tm_mon = min.tm_mon;
595 max.tm_mday = min.tm_mday;
597 if (!parse_date_range (pc, &min, &max, haveMin, &baseMin, err)) { /* bail out on any parsing error */
604 /* Since we allow two dates to be specified we'll have to adjust that. */
605 adjust_date_range (&min, &max);
607 pat->min = mutt_mktime (&min, 1);
608 pat->max = mutt_mktime (&max, 1);
615 static struct pattern_flags *lookup_tag (char tag)
619 for (i = 0; Flags[i].tag; i++)
620 if (Flags[i].tag == tag)
625 static /* const */ char *find_matching_paren ( /* const */ char *s)
632 else if (*s == ')') {
641 void mutt_pattern_free (pattern_t ** pat)
654 mutt_pattern_free (&tmp->child);
659 pattern_t *mutt_pattern_comp ( /* const */ char *s, int flags, BUFFER * err)
661 pattern_t *curlist = NULL;
663 pattern_t *last = NULL;
667 int implicit = 1; /* used to detect logical AND operator */
668 struct pattern_flags *entry;
673 memset (&ps, 0, sizeof (ps));
675 ps.dsize = mutt_strlen (s);
691 snprintf (err->data, err->dsize, _("error in pattern at: %s"),
696 /* A & B | C == (A & B) | C */
697 tmp = new_pattern ();
699 tmp->child = curlist;
713 if (implicit && or) {
714 /* A | B & C == (A | B) & C */
715 tmp = new_pattern ();
717 tmp->child = curlist;
723 tmp = new_pattern ();
725 tmp->alladdr = alladdr;
735 ps.dptr++; /* move past the ~ */
736 if ((entry = lookup_tag (*ps.dptr)) == NULL) {
737 snprintf (err->data, err->dsize, _("%c: invalid command"), *ps.dptr);
738 mutt_pattern_free (&curlist);
741 if (entry->class && (flags & entry->class) == 0) {
742 snprintf (err->data, err->dsize, _("%c: not supported in this mode"),
744 mutt_pattern_free (&curlist);
749 ps.dptr++; /* eat the operator and any optional whitespace */
752 if (entry->eat_arg) {
754 snprintf (err->data, err->dsize, _("missing parameter"));
755 mutt_pattern_free (&curlist);
758 if (entry->eat_arg (tmp, &ps, err) == -1) {
759 mutt_pattern_free (&curlist);
766 p = find_matching_paren (ps.dptr + 1);
768 snprintf (err->data, err->dsize, _("mismatched parenthesis: %s"),
770 mutt_pattern_free (&curlist);
773 /* compile the sub-expression */
774 buf = mutt_substrdup (ps.dptr + 1, p);
775 if ((tmp = mutt_pattern_comp (buf, flags, err)) == NULL) {
777 mutt_pattern_free (&curlist);
787 tmp->alladdr |= alladdr;
790 ps.dptr = p + 1; /* restore location */
793 snprintf (err->data, err->dsize, _("error in pattern at: %s"), ps.dptr);
794 mutt_pattern_free (&curlist);
799 strfcpy (err->data, _("empty pattern"), err->dsize);
803 tmp = new_pattern ();
804 tmp->op = or ? M_OR : M_AND;
805 tmp->child = curlist;
812 perform_and (pattern_t * pat, pattern_exec_flag flags, CONTEXT * ctx,
815 for (; pat; pat = pat->next)
816 if (mutt_pattern_exec (pat, flags, ctx, hdr) <= 0)
822 perform_or (struct pattern_t *pat, pattern_exec_flag flags, CONTEXT * ctx,
825 for (; pat; pat = pat->next)
826 if (mutt_pattern_exec (pat, flags, ctx, hdr) > 0)
831 static int match_adrlist (regex_t * rx, int match_personal, int alladdr,
839 for (a = va_arg (ap, ADDRESS *); a; a = a->next) {
841 ((a->mailbox && regexec (rx, a->mailbox, 0, NULL, 0) == 0) ||
842 (match_personal && a->personal &&
843 regexec (rx, a->personal, 0, NULL, 0) == 0))) {
845 return (!alladdr); /* Found match, or non-match if alladdr */
850 return alladdr; /* No matches, or all matches if alladdr */
853 static int match_reference (regex_t * rx, LIST * refs)
855 for (; refs; refs = refs->next)
856 if (regexec (rx, refs->data, 0, NULL, 0) == 0)
861 int mutt_is_list_recipient (int alladdr, ADDRESS * a1, ADDRESS * a2)
863 for (; a1; a1 = a1->next)
864 if (alladdr ^ mutt_is_subscribed_list (a1))
866 for (; a2; a2 = a2->next)
867 if (alladdr ^ mutt_is_subscribed_list (a2))
872 int mutt_is_list_cc (int alladdr, ADDRESS * a1, ADDRESS * a2)
874 for (; a1; a1 = a1->next)
875 if (alladdr ^ mutt_is_mail_list (a1))
877 for (; a2; a2 = a2->next)
878 if (alladdr ^ mutt_is_mail_list (a2))
883 static int match_user (int alladdr, ADDRESS * a1, ADDRESS * a2)
885 for (; a1; a1 = a1->next)
886 if (alladdr ^ mutt_addr_is_user (a1))
888 for (; a2; a2 = a2->next)
889 if (alladdr ^ mutt_addr_is_user (a2))
894 /* test if name is considered a real name, i.e. consists of at least 2
895 * space-separated words of which none may end in a dot
897 static int valid_realname (const char *name)
899 const char *p = name;
906 /* skip abbr. parts of names (e.g. 'J. User') */
914 M_MATCH_FULL_ADDRESS match both personal and machine address */
916 mutt_pattern_exec (struct pattern_t *pat, pattern_exec_flag flags,
917 CONTEXT * ctx, HEADER * h)
923 return (pat->not ^ (perform_and (pat->child, flags, ctx, h) > 0));
925 return (pat->not ^ (perform_or (pat->child, flags, ctx, h) > 0));
929 return (pat->not ^ h->expired);
931 return (pat->not ^ h->superseded);
933 return (pat->not ^ h->flagged);
935 return (pat->not ^ h->tagged);
937 return (pat->not ? h->old || h->read : !(h->old || h->read));
939 return (pat->not ? h->read : !h->read);
941 return (pat->not ^ h->replied);
943 return (pat->not ? (!h->old || h->read) : (h->old && !h->read));
945 return (pat->not ^ h->read);
947 return (pat->not ^ h->deleted);
949 return (pat->not ^ (h->msgno >= pat->min - 1 && (pat->max == M_MAXRANGE ||
954 not ^ (h->date_sent >= pat->min && h->date_sent <= pat->max));
955 case M_DATE_RECEIVED:
956 return (pat->not ^ (h->received >= pat->min && h->received <= pat->max));
961 not ^ msg_search (ctx, pat->rx, buf, sizeof (buf), pat->op,
964 return (pat->not ^ match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
965 pat->alladdr, 1, h->env->sender));
967 return (pat->not ^ match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
968 pat->alladdr, 1, h->env->from));
970 return (pat->not ^ match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
971 pat->alladdr, 1, h->env->to));
973 return (pat->not ^ match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
974 pat->alladdr, 1, h->env->cc));
977 not ^ (h->env && h->env->subject
978 && regexec (pat->rx, h->env->subject, 0, NULL, 0) == 0));
981 not ^ (h->env && h->env->message_id
982 && regexec (pat->rx, h->env->message_id, 0, NULL,
985 return (pat->not ^ (h->score >= pat->min && (pat->max == M_MAXRANGE ||
986 h->score <= pat->max)));
989 not ^ (h->content->length >= pat->min
990 && (pat->max == M_MAXRANGE
991 || h->content->length <= pat->max)));
993 return (pat->not ^ match_reference (pat->rx, h->env->references));
997 && match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
998 pat->alladdr, 4, h->env->from,
999 h->env->sender, h->env->to,
1004 && match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
1005 pat->alladdr, 2, h->env->to,
1010 && mutt_is_list_recipient (pat->alladdr, h->env->to,
1012 case M_PERSONAL_RECIP:
1015 && match_user (pat->alladdr, h->env->to, h->env->cc)));
1016 case M_PERSONAL_FROM:
1018 not ^ (h->env && match_user (pat->alladdr, h->env->from, NULL)));
1020 return (pat->not ^ (h->collapsed && h->num_hidden > 1));
1024 return (pat->not ^ ((h->security & SIGN) ? 1 : 0));
1025 case M_CRYPT_VERIFIED:
1028 return (pat->not ^ ((h->security & GOODSIGN) ? 1 : 0));
1029 case M_CRYPT_ENCRYPT:
1032 return (pat->not ^ ((h->security & ENCRYPT) ? 1 : 0));
1034 if (!(WithCrypto & APPLICATION_PGP))
1036 return (pat->not ^ ((h->security & APPLICATION_PGP)
1037 && (h->security & PGPKEY)));
1040 not ^ (h->env->x_label
1041 && regexec (pat->rx, h->env->x_label, 0, NULL, 0) == 0));
1044 not ^ (h->env->spam && h->env->spam->data
1045 && regexec (pat->rx, h->env->spam->data, 0, NULL,
1048 return (pat->not ^ (h->thread && h->thread->duplicate_thread));
1049 case M_UNREFERENCED:
1050 return (pat->not ^ (h->thread && !h->thread->child));
1053 * we have a match if
1054 * - From: matches $alternates
1055 * - or we have an alias for current address
1056 * - or From: contains valid email address _and_ name has >= 2 fields
1059 not ^ (h->env && h->env->from && (mutt_addr_is_user (h->env->from)
1061 (alias_reverse_lookup
1062 (h->env->from) != NULL)
1063 || (h->env->from->personal
1064 && valid_realname (h->env->
1067 && h->env->from->mailbox)
1072 not ^ (h->env->newsgroups
1073 && regexec (pat->rx, h->env->newsgroups, 0, NULL,
1077 mutt_error (_("error: unknown op %d (report this error)."), pat->op);
1081 static void quote_simple (char *tmp, size_t len, const char *p)
1086 while (*p && i < len - 3) {
1087 if (*p == '\\' || *p == '"')
1095 /* convert a simple search into a real request */
1096 void mutt_check_simple (char *s, size_t len, const char *simple)
1098 char tmp[LONG_STRING];
1100 /* XXX - is ascii_strcasecmp() right here, or should we use locale's
1104 if (!strchr (s, '~')) { /* yup, so spoof a real request */
1105 /* convert old tokens into the new format */
1106 if (ascii_strcasecmp ("all", s) == 0 || !mutt_strcmp ("^", s) || !mutt_strcmp (".", s)) /* ~A is more efficient */
1107 strfcpy (s, "~A", len);
1108 else if (ascii_strcasecmp ("del", s) == 0)
1109 strfcpy (s, "~D", len);
1110 else if (ascii_strcasecmp ("flag", s) == 0)
1111 strfcpy (s, "~F", len);
1112 else if (ascii_strcasecmp ("new", s) == 0)
1113 strfcpy (s, "~N", len);
1114 else if (ascii_strcasecmp ("old", s) == 0)
1115 strfcpy (s, "~O", len);
1116 else if (ascii_strcasecmp ("repl", s) == 0)
1117 strfcpy (s, "~Q", len);
1118 else if (ascii_strcasecmp ("read", s) == 0)
1119 strfcpy (s, "~R", len);
1120 else if (ascii_strcasecmp ("tag", s) == 0)
1121 strfcpy (s, "~T", len);
1122 else if (ascii_strcasecmp ("unread", s) == 0)
1123 strfcpy (s, "~U", len);
1125 quote_simple (tmp, sizeof (tmp), s);
1126 mutt_expand_fmt (s, len, simple, tmp);
1131 int mutt_pattern_func (int op, char *prompt)
1134 char buf[LONG_STRING] = "", *simple, error[STRING];
1138 strfcpy (buf, NONULL (Context->pattern), sizeof (buf));
1139 if (prompt || op != M_LIMIT)
1140 if (mutt_get_field (prompt, buf, sizeof (buf), M_PATTERN | M_CLEAR) != 0
1144 mutt_message _("Compiling search pattern...");
1146 simple = safe_strdup (buf);
1147 mutt_check_simple (buf, sizeof (buf), NONULL (SimpleSearch));
1150 err.dsize = sizeof (error);
1151 if ((pat = mutt_pattern_comp (buf, M_FULL_MSG, &err)) == NULL) {
1153 mutt_error ("%s", err.data);
1157 mutt_message _("Executing command on matching messages...");
1159 #define THIS_BODY Context->hdrs[i]->content
1161 if (op == M_LIMIT) {
1162 Context->vcount = 0;
1164 Context->collapsed = 0;
1166 for (i = 0; i < Context->msgcount; i++) {
1167 /* new limit pattern implicitly uncollapses all threads */
1168 Context->hdrs[i]->virtual = -1;
1169 Context->hdrs[i]->limited = 0;
1170 Context->hdrs[i]->collapsed = 0;
1171 Context->hdrs[i]->num_hidden = 0;
1172 if (mutt_pattern_exec
1173 (pat, M_MATCH_FULL_ADDRESS, Context, Context->hdrs[i])) {
1174 Context->hdrs[i]->virtual = Context->vcount;
1175 Context->hdrs[i]->limited = 1;
1176 Context->v2r[Context->vcount] = i;
1178 Context->vsize += THIS_BODY->length + THIS_BODY->offset -
1179 THIS_BODY->hdr_offset;
1184 for (i = 0; i < Context->vcount; i++) {
1185 if (mutt_pattern_exec
1186 (pat, M_MATCH_FULL_ADDRESS, Context,
1187 Context->hdrs[Context->v2r[i]])) {
1190 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_PURGED,
1193 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_DELETE,
1198 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_TAG,
1208 mutt_clear_error ();
1210 if (op == M_LIMIT) {
1211 FREE (&Context->pattern);
1212 if (Context->limit_pattern)
1213 mutt_pattern_free (&Context->limit_pattern);
1214 if (!Context->vcount) {
1215 mutt_error _("No messages matched criteria.");
1218 Context->vcount = Context->msgcount;
1219 /* restore full display */
1220 for (i = 0; i < Context->msgcount; i++) {
1221 Context->hdrs[i]->virtual = i;
1222 Context->v2r[i] = i;
1226 else if (mutt_strncmp (buf, "~A", 2) != 0) {
1227 Context->pattern = simple;
1228 simple = NULL; /* don't clobber it */
1229 Context->limit_pattern = mutt_pattern_comp (buf, M_FULL_MSG, &err);
1233 mutt_pattern_free (&pat);
1237 int mutt_search_command (int cur, int op)
1241 char temp[LONG_STRING];
1247 if (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE) {
1248 strfcpy (buf, LastSearch, sizeof (buf));
1249 if (mutt_get_field ((op == OP_SEARCH) ? _("Search for: ") :
1250 _("Reverse search for: "), buf, sizeof (buf),
1251 M_CLEAR | M_PATTERN) != 0 || !buf[0])
1254 if (op == OP_SEARCH)
1255 unset_option (OPTSEARCHREVERSE);
1257 set_option (OPTSEARCHREVERSE);
1259 /* compare the *expanded* version of the search pattern in case
1260 $simple_search has changed while we were searching */
1261 strfcpy (temp, buf, sizeof (temp));
1262 mutt_check_simple (temp, sizeof (temp), NONULL (SimpleSearch));
1264 if (!SearchPattern || mutt_strcmp (temp, LastSearchExpn)) {
1265 set_option (OPTSEARCHINVALID);
1266 strfcpy (LastSearch, buf, sizeof (LastSearch));
1267 mutt_message _("Compiling search pattern...");
1269 mutt_pattern_free (&SearchPattern);
1271 err.dsize = sizeof (error);
1272 if ((SearchPattern =
1273 mutt_pattern_comp (temp, M_FULL_MSG, &err)) == NULL) {
1274 mutt_error ("%s", error);
1277 mutt_clear_error ();
1280 else if (!SearchPattern) {
1281 mutt_error _("No search pattern.");
1286 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++) {
1297 if (i > Context->vcount - 1) {
1299 if (option (OPTWRAPSEARCH))
1300 mutt_message (_("Search wrapped to top."));
1303 mutt_message _("Search hit bottom without finding match");
1309 i = Context->vcount - 1;
1310 if (option (OPTWRAPSEARCH))
1311 mutt_message (_("Search wrapped to bottom."));
1314 mutt_message _("Search hit top without finding match");
1320 h = Context->hdrs[Context->v2r[i]];
1322 /* if we've already evaulated this message, use the cached value */
1327 /* remember that we've already searched this message */
1331 (SearchPattern, M_MATCH_FULL_ADDRESS, Context, h) > 0)))
1336 mutt_error _("Search interrupted.");
1345 mutt_error _("Not found.");