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 },
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++)
122 if (ch == table[i].name[0])
123 return table[i].value;
129 /* if no uppercase letters are given, do a case-insensitive search */
130 int mutt_which_case (const char *s)
134 if (isalpha ((unsigned char) *s) && isupper ((unsigned char) *s))
135 return 0; /* case-sensitive */
138 return REG_ICASE; /* case-insensitive */
142 msg_search (CONTEXT *ctx, regex_t *rx, char *buf, size_t blen, int op, int msgno)
144 char tempfile[_POSIX_PATH_MAX];
151 HEADER *h = ctx->hdrs[msgno];
153 if ((msg = mx_open_message (ctx, msgno)) != NULL)
155 if (option (OPTTHOROUGHSRC))
157 /* decode the header / body */
158 memset (&s, 0, sizeof (s));
160 s.flags = M_CHARCONV;
161 mutt_mktemp (tempfile);
162 if ((s.fpout = safe_fopen (tempfile, "w+")) == NULL)
164 mutt_perror (tempfile);
169 mutt_copy_header (msg->fp, h, s.fpout, CH_FROM | CH_DECODE, NULL);
173 mutt_parse_mime_message (ctx, h);
175 if (WithCrypto && (h->security & ENCRYPT)
176 && !crypt_valid_passphrase(h->security))
178 mx_close_message (&msg);
187 fseek (msg->fp, h->offset, 0);
188 mutt_body_handler (h->content, &s);
194 fstat (fileno (fp), &st);
195 lng = (long) st.st_size;
199 /* raw header / body */
203 fseek (fp, h->offset, 0);
204 lng = h->content->offset - h->offset;
209 fseek (fp, h->content->offset, 0);
210 lng += h->content->length;
214 /* search the file "fp" */
217 if (fgets (buf, blen - 1, fp) == NULL)
218 break; /* don't loop forever */
219 if (regexec (rx, buf, 0, NULL, 0) == 0)
224 lng -= mutt_strlen (buf);
227 mx_close_message (&msg);
229 if (option (OPTTHOROUGHSRC))
239 int eat_regexp (pattern_t *pat, BUFFER *s, BUFFER *err)
244 memset (&buf, 0, sizeof (buf));
245 if (mutt_extract_token (&buf, s, M_TOKEN_PATTERN | M_TOKEN_COMMENT) != 0 ||
248 snprintf (err->data, err->dsize, _("Error in expression: %s"), s->dptr);
251 pat->rx = safe_malloc (sizeof (regex_t));
252 r = REGCOMP (pat->rx, buf.data, REG_NEWLINE | REG_NOSUB | mutt_which_case (buf.data));
256 regerror (r, pat->rx, err->data, err->dsize);
264 int eat_range (pattern_t *pat, BUFFER *s, BUFFER *err)
267 int do_exclusive = 0;
271 * If simple_search is set to "~m %s", the range will have double quotes
281 if ((*s->dptr != '-') && (*s->dptr != '<'))
286 pat->max = M_MAXRANGE;
287 pat->min = strtol (s->dptr + 1, &tmp, 0) + 1; /* exclusive range */
290 pat->min = strtol (s->dptr, &tmp, 0);
291 if (toupper ((unsigned char) *tmp) == 'K') /* is there a prefix? */
296 else if (toupper ((unsigned char) *tmp) == 'M')
321 if (isdigit ((unsigned char) *tmp))
324 pat->max = strtol (tmp, &tmp, 0);
325 if (toupper ((unsigned char) *tmp) == 'K')
330 else if (toupper ((unsigned char) *tmp) == 'M')
339 pat->max = M_MAXRANGE;
341 if (skip_quote && *tmp == '"')
349 static const char *getDate (const char *s, struct tm *t, BUFFER *err)
352 time_t now = time (NULL);
353 struct tm *tm = localtime (&now);
355 t->tm_mday = strtol (s, &p, 10);
356 if (t->tm_mday < 1 || t->tm_mday > 31)
358 snprintf (err->data, err->dsize, _("Invalid day of month: %s"), s);
363 /* fill in today's month and year */
364 t->tm_mon = tm->tm_mon;
365 t->tm_year = tm->tm_year;
369 t->tm_mon = strtol (p, &p, 10) - 1;
370 if (t->tm_mon < 0 || t->tm_mon > 11)
372 snprintf (err->data, err->dsize, _("Invalid month: %s"), p);
377 t->tm_year = tm->tm_year;
381 t->tm_year = strtol (p, &p, 10);
382 if (t->tm_year < 70) /* year 2000+ */
384 else if (t->tm_year > 1900)
393 static const char *get_offset (struct tm *tm, const char *s, int sign)
396 int offset = strtol (s, &ps, 0);
397 if ((sign < 0 && offset > 0) || (sign > 0 && offset < 0))
403 tm->tm_year += offset;
406 tm->tm_mon += offset;
409 tm->tm_mday += 7 * offset;
412 tm->tm_mday += offset;
417 mutt_normalize_time (tm);
421 static void adjust_date_range (struct tm *min, struct tm *max)
423 if (min->tm_year > max->tm_year
424 || (min->tm_year == max->tm_year && min->tm_mon > max->tm_mon)
425 || (min->tm_year == max->tm_year && min->tm_mon == max->tm_mon
426 && min->tm_mday > max->tm_mday))
431 min->tm_year = max->tm_year;
435 min->tm_mon = max->tm_mon;
439 min->tm_mday = max->tm_mday;
442 min->tm_hour = min->tm_min = min->tm_sec = 0;
444 max->tm_min = max->tm_sec = 59;
448 static const char * parse_date_range (const char* pc, struct tm *min,
449 struct tm *max, int haveMin, struct tm *baseMin, BUFFER *err)
451 int flag = M_PDR_NONE;
452 while (*pc && ((flag & M_PDR_DONE) == 0))
461 /* try a range of absolute date minus offset of Ndwmy */
462 pt = get_offset (min, pc, -1);
465 if (flag == M_PDR_NONE)
466 { /* nothing yet and no offset parsed => absolute date? */
467 if (!getDate (pc, max, err))
468 flag |= (M_PDR_ABSOLUTE | M_PDR_ERRORDONE); /* done bad */
471 /* reestablish initial base minimum if not specified */
473 memcpy (min, baseMin, sizeof(struct tm));
474 flag |= (M_PDR_ABSOLUTE | M_PDR_DONE); /* done good */
478 flag |= M_PDR_ERRORDONE;
483 if (flag == M_PDR_NONE && !haveMin)
484 { /* the very first "-3d" without a previous absolute date */
485 max->tm_year = min->tm_year;
486 max->tm_mon = min->tm_mon;
487 max->tm_mday = min->tm_mday;
494 { /* enlarge plusRange */
495 pt = get_offset (max, pc, 1);
497 flag |= M_PDR_ERRORDONE;
506 { /* enlarge window in both directions */
507 pt = get_offset (min, pc, -1);
509 flag |= M_PDR_ERRORDONE;
512 pc = get_offset (max, pc, 1);
513 flag |= M_PDR_WINDOW;
518 flag |= M_PDR_ERRORDONE;
522 if ((flag & M_PDR_ERROR) && !(flag & M_PDR_ABSOLUTE))
523 { /* getDate has its own error message, don't overwrite it here */
524 snprintf (err->data, err->dsize, _("Invalid relative date: %s"), pc-1);
526 return ((flag & M_PDR_ERROR) ? NULL : pc);
529 static int eat_date (pattern_t *pat, BUFFER *s, BUFFER *err)
534 memset (&buffer, 0, sizeof (buffer));
535 if (mutt_extract_token (&buffer, s, M_TOKEN_COMMENT | M_TOKEN_PATTERN) != 0
538 strfcpy (err->data, _("error in expression"), err->dsize);
542 memset (&min, 0, sizeof (min));
543 /* the `0' time is Jan 1, 1970 UTC, so in order to prevent a negative time
544 when doing timezone conversion, we use Jan 2, 1970 UTC as the base
549 memset (&max, 0, sizeof (max));
551 /* Arbitrary year in the future. Don't set this too high
552 or mutt_mktime() returns something larger than will
553 fit in a time_t on some systems */
561 if (strchr ("<>=", buffer.data[0]))
563 /* offset from current time
564 <3d less than three days ago
565 >3d more than three days ago
566 =3d exactly three days ago */
567 time_t now = time (NULL);
568 struct tm *tm = localtime (&now);
571 if (buffer.data[0] == '<')
573 memcpy (&min, tm, sizeof (min));
578 memcpy (&max, tm, sizeof (max));
581 if (buffer.data[0] == '=')
585 tm->tm_min = tm->tm_sec = 59;
587 /* force negative offset */
588 get_offset (tm, buffer.data + 1, -1);
592 /* start at the beginning of the day in question */
593 memcpy (&min, &max, sizeof (max));
594 min.tm_hour = min.tm_sec = min.tm_min = 0;
599 const char *pc = buffer.data;
602 int untilNow = FALSE;
603 if (isdigit ((unsigned char)*pc))
605 /* mininum date specified */
606 if ((pc = getDate (pc, &min, err)) == NULL)
615 const char *pt = pc + 1;
617 untilNow = (*pt == '\0');
622 { /* max date or relative range/window */
627 { /* save base minimum and set current date, e.g. for "-3d+1d" */
628 time_t now = time (NULL);
629 struct tm *tm = localtime (&now);
630 memcpy (&baseMin, &min, sizeof(baseMin));
631 memcpy (&min, tm, sizeof (min));
632 min.tm_hour = min.tm_sec = min.tm_min = 0;
635 /* preset max date for relative offsets,
636 if nothing follows we search for messages on a specific day */
637 max.tm_year = min.tm_year;
638 max.tm_mon = min.tm_mon;
639 max.tm_mday = min.tm_mday;
641 if (!parse_date_range (pc, &min, &max, haveMin, &baseMin, err))
642 { /* bail out on any parsing error */
649 /* Since we allow two dates to be specified we'll have to adjust that. */
650 adjust_date_range (&min, &max);
652 pat->min = mutt_mktime (&min, 1);
653 pat->max = mutt_mktime (&max, 1);
660 static struct pattern_flags *lookup_tag (char tag)
664 for (i = 0; Flags[i].tag; i++)
665 if (Flags[i].tag == tag)
670 static /* const */ char *find_matching_paren (/* const */ char *s)
688 void mutt_pattern_free (pattern_t **pat)
703 mutt_pattern_free (&tmp->child);
708 pattern_t *mutt_pattern_comp (/* const */ char *s, int flags, BUFFER *err)
710 pattern_t *curlist = NULL;
712 pattern_t *last = NULL;
716 int implicit = 1; /* used to detect logical AND operator */
717 struct pattern_flags *entry;
722 memset (&ps, 0, sizeof (ps));
724 ps.dsize = mutt_strlen (s);
744 snprintf (err->data, err->dsize, _("error in pattern at: %s"), ps.dptr);
749 /* A & B | C == (A & B) | C */
750 tmp = new_pattern ();
752 tmp->child = curlist;
768 /* A | B & C == (A | B) & C */
769 tmp = new_pattern ();
771 tmp->child = curlist;
777 tmp = new_pattern ();
779 tmp->alladdr = alladdr;
789 ps.dptr++; /* move past the ~ */
790 if ((entry = lookup_tag (*ps.dptr)) == NULL)
792 snprintf (err->data, err->dsize, _("%c: invalid command"), *ps.dptr);
793 mutt_pattern_free (&curlist);
796 if (entry->class && (flags & entry->class) == 0)
798 snprintf (err->data, err->dsize, _("%c: not supported in this mode"), *ps.dptr);
799 mutt_pattern_free (&curlist);
804 ps.dptr++; /* eat the operator and any optional whitespace */
811 snprintf (err->data, err->dsize, _("missing parameter"));
812 mutt_pattern_free (&curlist);
815 if (entry->eat_arg (tmp, &ps, err) == -1)
817 mutt_pattern_free (&curlist);
824 p = find_matching_paren (ps.dptr + 1);
827 snprintf (err->data, err->dsize, _("mismatched parenthesis: %s"), ps.dptr);
828 mutt_pattern_free (&curlist);
831 /* compile the sub-expression */
832 buf = mutt_substrdup (ps.dptr + 1, p);
833 if ((tmp = mutt_pattern_comp (buf, flags, err)) == NULL)
836 mutt_pattern_free (&curlist);
846 tmp->alladdr |= alladdr;
849 ps.dptr = p + 1; /* restore location */
852 snprintf (err->data, err->dsize, _("error in pattern at: %s"), ps.dptr);
853 mutt_pattern_free (&curlist);
859 strfcpy (err->data, _("empty pattern"), err->dsize);
864 tmp = new_pattern ();
865 tmp->op = or ? M_OR : M_AND;
866 tmp->child = curlist;
873 perform_and (pattern_t *pat, pattern_exec_flag flags, CONTEXT *ctx, HEADER *hdr)
875 for (; pat; pat = pat->next)
876 if (mutt_pattern_exec (pat, flags, ctx, hdr) <= 0)
882 perform_or (struct pattern_t *pat, pattern_exec_flag flags, CONTEXT *ctx, HEADER *hdr)
884 for (; pat; pat = pat->next)
885 if (mutt_pattern_exec (pat, flags, ctx, hdr) > 0)
890 static int match_adrlist (regex_t *rx, int match_personal, int alladdr,
899 for (a = va_arg (ap, ADDRESS *) ; a ; a = a->next)
902 ((a->mailbox && regexec (rx, a->mailbox, 0, NULL, 0) == 0) ||
903 (match_personal && a->personal &&
904 regexec (rx, a->personal, 0, NULL, 0) == 0)))
907 return (! alladdr); /* Found match, or non-match if alladdr */
912 return alladdr; /* No matches, or all matches if alladdr */
915 static int match_reference (regex_t *rx, LIST *refs)
917 for (; refs; refs = refs->next)
918 if (regexec (rx, refs->data, 0, NULL, 0) == 0)
923 int mutt_is_list_recipient (int alladdr, ADDRESS *a1, ADDRESS *a2)
925 for (; a1 ; a1 = a1->next)
926 if (alladdr ^ mutt_is_subscribed_list (a1))
928 for (; a2 ; a2 = a2->next)
929 if (alladdr ^ mutt_is_subscribed_list (a2))
934 int mutt_is_list_cc (int alladdr, ADDRESS *a1, ADDRESS *a2)
936 for (; a1 ; a1 = a1->next)
937 if (alladdr ^ mutt_is_mail_list (a1))
939 for (; a2 ; a2 = a2->next)
940 if (alladdr ^ mutt_is_mail_list (a2))
945 static int match_user (int alladdr, ADDRESS *a1, ADDRESS *a2)
947 for (; a1 ; a1 = a1->next)
948 if (alladdr ^ mutt_addr_is_user (a1))
950 for (; a2 ; a2 = a2->next)
951 if (alladdr ^ mutt_addr_is_user (a2))
956 /* test if name is considered a real name, i.e. consists of at least 2
957 * space-separated words of which none may end in a dot
959 static int valid_realname (const char* name)
961 const char* p = name;
968 /* skip abbr. parts of names (e.g. 'J. User') */
976 M_MATCH_FULL_ADDRESS match both personal and machine address */
978 mutt_pattern_exec (struct pattern_t *pat, pattern_exec_flag flags, CONTEXT *ctx, HEADER *h)
985 return (pat->not ^ (perform_and (pat->child, flags, ctx, h) > 0));
987 return (pat->not ^ (perform_or (pat->child, flags, ctx, h) > 0));
991 return (pat->not ^ h->expired);
993 return (pat->not ^ h->superseded);
995 return (pat->not ^ h->flagged);
997 return (pat->not ^ h->tagged);
999 return (pat->not ? h->old || h->read : !(h->old || h->read));
1001 return (pat->not ? h->read : !h->read);
1003 return (pat->not ^ h->replied);
1005 return (pat->not ? (!h->old || h->read) : (h->old && !h->read));
1007 return (pat->not ^ h->read);
1009 return (pat->not ^ h->deleted);
1011 return (pat->not ^ (h->msgno >= pat->min - 1 && (pat->max == M_MAXRANGE ||
1012 h->msgno <= pat->max - 1)));
1014 return (pat->not ^ (h->date_sent >= pat->min && h->date_sent <= pat->max));
1015 case M_DATE_RECEIVED:
1016 return (pat->not ^ (h->received >= pat->min && h->received <= pat->max));
1020 return (pat->not ^ msg_search (ctx, pat->rx, buf, sizeof (buf), pat->op, h->msgno));
1022 return (pat->not ^ match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
1023 pat->alladdr, 1, h->env->sender));
1025 return (pat->not ^ match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
1026 pat->alladdr, 1, h->env->from));
1028 return (pat->not ^ match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
1029 pat->alladdr, 1, h->env->to));
1031 return (pat->not ^ match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
1032 pat->alladdr, 1, h->env->cc));
1034 return (pat->not ^ (h->env && h->env->subject && regexec (pat->rx, h->env->subject, 0, NULL, 0) == 0));
1036 return (pat->not ^ (h->env && h->env->message_id && regexec (pat->rx, h->env->message_id, 0, NULL, 0) == 0));
1038 return (pat->not ^ (h->score >= pat->min && (pat->max == M_MAXRANGE ||
1039 h->score <= pat->max)));
1041 return (pat->not ^ (h->content->length >= pat->min && (pat->max == M_MAXRANGE || h->content->length <= pat->max)));
1043 return (pat->not ^ match_reference (pat->rx, h->env->references));
1045 return (pat->not ^ (h->env && match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
1046 pat->alladdr, 4, h->env->from,
1047 h->env->sender, h->env->to, h->env->cc)));
1049 return (pat->not ^ (h->env && match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
1050 pat->alladdr, 2, h->env->to, h->env->cc)));
1052 return (pat->not ^ (h->env && mutt_is_list_recipient (pat->alladdr, h->env->to, h->env->cc)));
1053 case M_PERSONAL_RECIP:
1054 return (pat->not ^ (h->env && match_user (pat->alladdr, h->env->to, h->env->cc)));
1055 case M_PERSONAL_FROM:
1056 return (pat->not ^ (h->env && match_user (pat->alladdr, h->env->from, NULL)));
1058 return (pat->not ^ (h->collapsed && h->num_hidden > 1));
1062 return (pat->not ^ ((h->security & SIGN) ? 1 : 0));
1063 case M_CRYPT_VERIFIED:
1066 return (pat->not ^ ((h->security & GOODSIGN) ? 1 : 0));
1067 case M_CRYPT_ENCRYPT:
1070 return (pat->not ^ ((h->security & ENCRYPT) ? 1 : 0));
1072 if (!(WithCrypto & APPLICATION_PGP))
1074 return (pat->not ^ ((h->security & APPLICATION_PGP) && (h->security & PGPKEY)));
1076 return (pat->not ^ (h->env->x_label && regexec (pat->rx, h->env->x_label, 0, NULL, 0) == 0));
1078 return (pat->not ^ (h->env->spam && h->env->spam->data && regexec (pat->rx, h->env->spam->data, 0, NULL, 0) == 0));
1080 return (pat->not ^ (h->thread && h->thread->duplicate_thread));
1081 case M_UNREFERENCED:
1082 return (pat->not ^ (h->thread && !h->thread->child));
1085 * we have a match if
1086 * - From: matches $alternates
1087 * - or we have an alias for current address
1088 * - or From: contains valid email address _and_ name has >= 2 fields
1090 return (pat->not ^ (h->env && h->env->from && (
1091 mutt_addr_is_user (h->env->from) ||
1092 (alias_reverse_lookup (h->env->from) != NULL) ||
1093 (h->env->from->personal && valid_realname (h->env->from->personal) && h->env->from->mailbox)
1097 return (pat->not ^ (h->env->newsgroups && regexec (pat->rx, h->env->newsgroups, 0, NULL, 0) == 0));
1100 mutt_error (_("error: unknown op %d (report this error)."), pat->op);
1104 static void quote_simple(char *tmp, size_t len, const char *p)
1109 while (*p && i < len - 3)
1111 if (*p == '\\' || *p == '"')
1119 /* convert a simple search into a real request */
1120 void mutt_check_simple (char *s, size_t len, const char *simple)
1122 char tmp[LONG_STRING];
1124 /* XXX - is ascii_strcasecmp() right here, or should we use locale's
1128 if (!strchr (s, '~')) /* yup, so spoof a real request */
1130 /* convert old tokens into the new format */
1131 if (ascii_strcasecmp ("all", s) == 0 ||
1132 !mutt_strcmp ("^", s) || !mutt_strcmp (".", s)) /* ~A is more efficient */
1133 strfcpy (s, "~A", len);
1134 else if (ascii_strcasecmp ("del", s) == 0)
1135 strfcpy (s, "~D", len);
1136 else if (ascii_strcasecmp ("flag", s) == 0)
1137 strfcpy (s, "~F", len);
1138 else if (ascii_strcasecmp ("new", s) == 0)
1139 strfcpy (s, "~N", len);
1140 else if (ascii_strcasecmp ("old", s) == 0)
1141 strfcpy (s, "~O", len);
1142 else if (ascii_strcasecmp ("repl", s) == 0)
1143 strfcpy (s, "~Q", len);
1144 else if (ascii_strcasecmp ("read", s) == 0)
1145 strfcpy (s, "~R", len);
1146 else if (ascii_strcasecmp ("tag", s) == 0)
1147 strfcpy (s, "~T", len);
1148 else if (ascii_strcasecmp ("unread", s) == 0)
1149 strfcpy (s, "~U", len);
1152 quote_simple (tmp, sizeof(tmp), s);
1153 mutt_expand_fmt (s, len, simple, tmp);
1158 int mutt_pattern_func (int op, char *prompt)
1161 char buf[LONG_STRING] = "", *simple, error[STRING];
1165 strfcpy (buf, NONULL (Context->pattern), sizeof (buf));
1166 if (prompt || op != M_LIMIT)
1167 if (mutt_get_field (prompt, buf, sizeof (buf), M_PATTERN | M_CLEAR) != 0 || !buf[0])
1170 mutt_message _("Compiling search pattern...");
1172 simple = safe_strdup (buf);
1173 mutt_check_simple (buf, sizeof (buf), NONULL (SimpleSearch));
1176 err.dsize = sizeof (error);
1177 if ((pat = mutt_pattern_comp (buf, M_FULL_MSG, &err)) == NULL)
1180 mutt_error ("%s", err.data);
1184 mutt_message _("Executing command on matching messages...");
1186 #define THIS_BODY Context->hdrs[i]->content
1190 Context->vcount = 0;
1192 Context->collapsed = 0;
1194 for (i = 0; i < Context->msgcount; i++)
1196 /* new limit pattern implicitly uncollapses all threads */
1197 Context->hdrs[i]->virtual = -1;
1198 Context->hdrs[i]->limited = 0;
1199 Context->hdrs[i]->collapsed = 0;
1200 Context->hdrs[i]->num_hidden = 0;
1201 if (mutt_pattern_exec (pat, M_MATCH_FULL_ADDRESS, Context, Context->hdrs[i]))
1203 Context->hdrs[i]->virtual = Context->vcount;
1204 Context->hdrs[i]->limited = 1;
1205 Context->v2r[Context->vcount] = i;
1207 Context->vsize+=THIS_BODY->length + THIS_BODY->offset -
1208 THIS_BODY->hdr_offset;
1214 for (i = 0; i < Context->vcount; i++)
1216 if (mutt_pattern_exec (pat, M_MATCH_FULL_ADDRESS, Context, Context->hdrs[Context->v2r[i]]))
1221 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_PURGED,
1224 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_DELETE,
1229 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_TAG,
1239 mutt_clear_error ();
1243 FREE (&Context->pattern);
1244 if (Context->limit_pattern)
1245 mutt_pattern_free (&Context->limit_pattern);
1246 if (!Context->vcount)
1248 mutt_error _("No messages matched criteria.");
1250 Context->vcount = Context->msgcount;
1251 /* restore full display */
1252 for (i = 0; i < Context->msgcount; i++)
1254 Context->hdrs[i]->virtual = i;
1255 Context->v2r[i] = i;
1259 else if (mutt_strncmp (buf, "~A", 2) != 0)
1261 Context->pattern = simple;
1262 simple = NULL; /* don't clobber it */
1263 Context->limit_pattern = mutt_pattern_comp (buf, M_FULL_MSG, &err);
1267 mutt_pattern_free (&pat);
1271 int mutt_search_command (int cur, int op)
1275 char temp[LONG_STRING];
1281 if (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE)
1283 strfcpy (buf, LastSearch, sizeof (buf));
1284 if (mutt_get_field ((op == OP_SEARCH) ? _("Search for: ") :
1285 _("Reverse search for: "), buf, sizeof (buf),
1286 M_CLEAR | M_PATTERN) != 0 || !buf[0])
1289 if (op == OP_SEARCH)
1290 unset_option (OPTSEARCHREVERSE);
1292 set_option (OPTSEARCHREVERSE);
1294 /* compare the *expanded* version of the search pattern in case
1295 $simple_search has changed while we were searching */
1296 strfcpy (temp, buf, sizeof (temp));
1297 mutt_check_simple (temp, sizeof (temp), NONULL (SimpleSearch));
1299 if (!SearchPattern || mutt_strcmp (temp, LastSearchExpn))
1301 set_option (OPTSEARCHINVALID);
1302 strfcpy (LastSearch, buf, sizeof (LastSearch));
1303 mutt_message _("Compiling search pattern...");
1304 mutt_pattern_free (&SearchPattern);
1306 err.dsize = sizeof (error);
1307 if ((SearchPattern = mutt_pattern_comp (temp, M_FULL_MSG, &err)) == NULL)
1309 mutt_error ("%s", error);
1312 mutt_clear_error ();
1315 else if (!SearchPattern)
1317 mutt_error _("No search pattern.");
1321 if (option (OPTSEARCHINVALID))
1323 for (i = 0; i < Context->msgcount; i++)
1324 Context->hdrs[i]->searched = 0;
1325 unset_option (OPTSEARCHINVALID);
1328 incr = (option (OPTSEARCHREVERSE)) ? -1 : 1;
1329 if (op == OP_SEARCH_OPPOSITE)
1332 for (i = cur + incr, j = 0 ; j != Context->vcount; j++)
1334 if (i > Context->vcount - 1)
1337 if (option (OPTWRAPSEARCH))
1338 mutt_message _("Search wrapped to top.");
1341 mutt_message _("Search hit bottom without finding match");
1347 i = Context->vcount - 1;
1348 if (option (OPTWRAPSEARCH))
1349 mutt_message _("Search wrapped to bottom.");
1352 mutt_message _("Search hit top without finding match");
1357 h = Context->hdrs[Context->v2r[i]];
1360 /* if we've already evaulated this message, use the cached value */
1366 /* remember that we've already searched this message */
1368 if ((h->matched = (mutt_pattern_exec (SearchPattern, M_MATCH_FULL_ADDRESS, Context, h) > 0)))
1374 mutt_error _("Search interrupted.");
1382 mutt_error _("Not found.");