2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>, and others
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
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 'M', M_MULTIPART, 0, NULL}, {
70 'n', M_SCORE, 0, eat_range}, {
71 'N', M_NEW, 0, NULL}, {
72 'O', M_OLD, 0, NULL}, {
73 'p', M_PERSONAL_RECIP, 0, NULL}, {
74 'P', M_PERSONAL_FROM, 0, NULL}, {
75 'Q', M_REPLIED, 0, NULL}, {
76 'R', M_READ, 0, NULL}, {
77 'r', M_DATE_RECEIVED, 0, eat_date}, {
78 's', M_SUBJECT, 0, eat_regexp}, {
79 'S', M_SUPERSEDED, 0, NULL}, {
80 'T', M_TAG, 0, NULL}, {
81 't', M_TO, 0, eat_regexp}, {
82 'U', M_UNREAD, 0, NULL}, {
83 'u', M_SUBSCRIBED_LIST, 0, NULL}, {
84 'v', M_COLLAPSED, 0, NULL}, {
85 'V', M_CRYPT_VERIFIED, 0, NULL},
88 'w', M_NEWSGROUPS, 0, eat_regexp},
91 'x', M_REFERENCE, 0, eat_regexp}, {
92 'y', M_XLABEL, 0, eat_regexp}, {
93 'z', M_SIZE, 0, eat_range}, {
94 '=', M_DUPLICATED, 0, NULL}, {
95 '$', M_UNREFERENCED, 0, NULL}, {
96 '*', M_REALNAME, 0, NULL}, {
100 static pattern_t *SearchPattern = NULL; /* current search pattern */
101 static char LastSearch[STRING] = { 0 }; /* last pattern searched for */
102 static char LastSearchExpn[LONG_STRING] = { 0 }; /* expanded version of
105 #define M_MAXRANGE -1
107 /* constants for parse_date_range() */
108 #define M_PDR_NONE 0x0000
109 #define M_PDR_MINUS 0x0001
110 #define M_PDR_PLUS 0x0002
111 #define M_PDR_WINDOW 0x0004
112 #define M_PDR_ABSOLUTE 0x0008
113 #define M_PDR_DONE 0x0010
114 #define M_PDR_ERROR 0x0100
115 #define M_PDR_ERRORDONE (M_PDR_ERROR | M_PDR_DONE)
118 int mutt_getvaluebychar (char ch, struct mapping_t *table)
122 for (i = 0; table[i].name; i++) {
123 if (ch == table[i].name[0])
124 return table[i].value;
130 /* if no uppercase letters are given, do a case-insensitive search */
131 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,
145 char tempfile[_POSIX_PATH_MAX];
152 HEADER *h = ctx->hdrs[msgno];
154 if ((msg = mx_open_message (ctx, msgno)) != NULL) {
155 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) {
162 mutt_perror (tempfile);
167 mutt_copy_header (msg->fp, h, s.fpout, CH_FROM | CH_DECODE, NULL);
169 if (op != M_HEADER) {
170 mutt_parse_mime_message (ctx, h);
172 if (WithCrypto && (h->security & ENCRYPT)
173 && !crypt_valid_passphrase (h->security)) {
174 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;
193 /* raw header / body */
196 fseek (fp, h->offset, 0);
197 lng = h->content->offset - h->offset;
199 if (op != M_HEADER) {
201 fseek (fp, h->content->offset, 0);
202 lng += h->content->length;
206 /* search the file "fp" */
208 if (fgets (buf, blen - 1, fp) == NULL)
209 break; /* don't loop forever */
210 if (regexec (rx, buf, 0, NULL, 0) == 0) {
214 lng -= str_len (buf);
217 mx_close_message (&msg);
219 if (option (OPTTHOROUGHSRC)) {
228 int eat_regexp (pattern_t * pat, BUFFER * s, BUFFER * err)
233 memset (&buf, 0, sizeof (buf));
234 if (mutt_extract_token (&buf, s, M_TOKEN_PATTERN | M_TOKEN_COMMENT) != 0 ||
236 snprintf (err->data, err->dsize, _("Error in expression: %s"), s->dptr);
239 pat->rx = mem_malloc (sizeof (regex_t));
241 REGCOMP (pat->rx, buf.data,
242 REG_NEWLINE | REG_NOSUB | mutt_which_case (buf.data));
243 mem_free (&buf.data);
245 regerror (r, pat->rx, err->data, err->dsize);
253 int eat_range (pattern_t * pat, BUFFER * s, BUFFER * err)
256 int do_exclusive = 0;
260 * If simple_search is set to "~m %s", the range will have double quotes
263 if (*s->dptr == '"') {
269 if ((*s->dptr != '-') && (*s->dptr != '<')) {
271 if (*s->dptr == '>') {
272 pat->max = M_MAXRANGE;
273 pat->min = strtol (s->dptr + 1, &tmp, 0) + 1; /* exclusive range */
276 pat->min = strtol (s->dptr, &tmp, 0);
277 if (toupper ((unsigned char) *tmp) == 'K') { /* is there a prefix? */
281 else if (toupper ((unsigned char) *tmp) == 'M') {
285 if (*s->dptr == '>') {
302 if (isdigit ((unsigned char) *tmp)) {
304 pat->max = strtol (tmp, &tmp, 0);
305 if (toupper ((unsigned char) *tmp) == 'K') {
309 else if (toupper ((unsigned char) *tmp) == 'M') {
317 pat->max = M_MAXRANGE;
319 if (skip_quote && *tmp == '"')
327 static const char *getDate (const char *s, struct tm *t, BUFFER * err)
330 time_t now = time (NULL);
331 struct tm *tm = localtime (&now);
333 t->tm_mday = strtol (s, &p, 10);
334 if (t->tm_mday < 1 || t->tm_mday > 31) {
335 snprintf (err->data, err->dsize, _("Invalid day of month: %s"), s);
339 /* fill in today's month and year */
340 t->tm_mon = tm->tm_mon;
341 t->tm_year = tm->tm_year;
345 t->tm_mon = strtol (p, &p, 10) - 1;
346 if (t->tm_mon < 0 || t->tm_mon > 11) {
347 snprintf (err->data, err->dsize, _("Invalid month: %s"), p);
351 t->tm_year = tm->tm_year;
355 t->tm_year = strtol (p, &p, 10);
356 if (t->tm_year < 70) /* year 2000+ */
358 else if (t->tm_year > 1900)
367 static const char *get_offset (struct tm *tm, const char *s, int sign)
370 int offset = strtol (s, &ps, 0);
372 if ((sign < 0 && offset > 0) || (sign > 0 && offset < 0))
377 tm->tm_year += offset;
380 tm->tm_mon += offset;
383 tm->tm_mday += 7 * offset;
386 tm->tm_mday += offset;
391 mutt_normalize_time (tm);
395 static void adjust_date_range (struct tm *min, struct tm *max)
397 if (min->tm_year > max->tm_year
398 || (min->tm_year == max->tm_year && min->tm_mon > max->tm_mon)
399 || (min->tm_year == max->tm_year && min->tm_mon == max->tm_mon
400 && min->tm_mday > max->tm_mday)) {
404 min->tm_year = max->tm_year;
408 min->tm_mon = max->tm_mon;
412 min->tm_mday = max->tm_mday;
415 min->tm_hour = min->tm_min = min->tm_sec = 0;
417 max->tm_min = max->tm_sec = 59;
421 static const char *parse_date_range (const char *pc, struct tm *min,
422 struct tm *max, int haveMin,
423 struct tm *baseMin, BUFFER * err)
425 int flag = M_PDR_NONE;
427 while (*pc && ((flag & M_PDR_DONE) == 0)) {
435 /* try a range of absolute date minus offset of Ndwmy */
436 pt = get_offset (min, pc, -1);
438 if (flag == M_PDR_NONE) { /* nothing yet and no offset parsed => absolute date? */
439 if (!getDate (pc, max, err))
440 flag |= (M_PDR_ABSOLUTE | M_PDR_ERRORDONE); /* done bad */
442 /* reestablish initial base minimum if not specified */
444 memcpy (min, baseMin, sizeof (struct tm));
445 flag |= (M_PDR_ABSOLUTE | M_PDR_DONE); /* done good */
449 flag |= M_PDR_ERRORDONE;
453 if (flag == M_PDR_NONE && !haveMin) { /* the very first "-3d" without a previous absolute date */
454 max->tm_year = min->tm_year;
455 max->tm_mon = min->tm_mon;
456 max->tm_mday = min->tm_mday;
463 { /* enlarge plusRange */
464 pt = get_offset (max, pc, 1);
466 flag |= M_PDR_ERRORDONE;
474 { /* enlarge window in both directions */
475 pt = get_offset (min, pc, -1);
477 flag |= M_PDR_ERRORDONE;
479 pc = get_offset (max, pc, 1);
480 flag |= M_PDR_WINDOW;
485 flag |= M_PDR_ERRORDONE;
489 if ((flag & M_PDR_ERROR) && !(flag & M_PDR_ABSOLUTE)) { /* getDate has its own error message, don't overwrite it here */
490 snprintf (err->data, err->dsize, _("Invalid relative date: %s"), pc - 1);
492 return ((flag & M_PDR_ERROR) ? NULL : pc);
495 static int eat_date (pattern_t * pat, BUFFER * s, BUFFER * err)
500 memset (&buffer, 0, sizeof (buffer));
501 if (mutt_extract_token (&buffer, s, M_TOKEN_COMMENT | M_TOKEN_PATTERN) != 0
503 strfcpy (err->data, _("error in expression"), err->dsize);
507 memset (&min, 0, sizeof (min));
508 /* the `0' time is Jan 1, 1970 UTC, so in order to prevent a negative time
509 when doing timezone conversion, we use Jan 2, 1970 UTC as the base
514 memset (&max, 0, sizeof (max));
516 /* Arbitrary year in the future. Don't set this too high
517 or mutt_mktime() returns something larger than will
518 fit in a time_t on some systems */
526 if (strchr ("<>=", buffer.data[0])) {
527 /* offset from current time
528 <3d less than three days ago
529 >3d more than three days ago
530 =3d exactly three days ago */
531 time_t now = time (NULL);
532 struct tm *tm = localtime (&now);
535 if (buffer.data[0] == '<') {
536 memcpy (&min, tm, sizeof (min));
540 memcpy (&max, tm, sizeof (max));
543 if (buffer.data[0] == '=')
547 tm->tm_min = tm->tm_sec = 59;
549 /* force negative offset */
550 get_offset (tm, buffer.data + 1, -1);
553 /* start at the beginning of the day in question */
554 memcpy (&min, &max, sizeof (max));
555 min.tm_hour = min.tm_sec = min.tm_min = 0;
559 const char *pc = buffer.data;
562 int untilNow = FALSE;
564 if (isdigit ((unsigned char) *pc)) {
565 /* mininum date specified */
566 if ((pc = getDate (pc, &min, err)) == NULL) {
567 mem_free (&buffer.data);
573 const char *pt = pc + 1;
576 untilNow = (*pt == '\0');
580 if (!untilNow) { /* max date or relative range/window */
584 if (!haveMin) { /* save base minimum and set current date, e.g. for "-3d+1d" */
585 time_t now = time (NULL);
586 struct tm *tm = localtime (&now);
588 memcpy (&baseMin, &min, sizeof (baseMin));
589 memcpy (&min, tm, sizeof (min));
590 min.tm_hour = min.tm_sec = min.tm_min = 0;
593 /* preset max date for relative offsets,
594 if nothing follows we search for messages on a specific day */
595 max.tm_year = min.tm_year;
596 max.tm_mon = min.tm_mon;
597 max.tm_mday = min.tm_mday;
599 if (!parse_date_range (pc, &min, &max, haveMin, &baseMin, err)) { /* bail out on any parsing error */
600 mem_free (&buffer.data);
606 /* Since we allow two dates to be specified we'll have to adjust that. */
607 adjust_date_range (&min, &max);
609 pat->min = mutt_mktime (&min, 1);
610 pat->max = mutt_mktime (&max, 1);
612 mem_free (&buffer.data);
617 static struct pattern_flags *lookup_tag (char tag)
621 for (i = 0; Flags[i].tag; i++)
622 if (Flags[i].tag == tag)
627 static /* const */ char *find_matching_paren ( /* const */ char *s)
634 else if (*s == ')') {
643 void mutt_pattern_free (pattern_t ** pat)
656 mutt_pattern_free (&tmp->child);
661 pattern_t *mutt_pattern_comp ( /* const */ char *s, int flags, BUFFER * err)
663 pattern_t *curlist = NULL;
665 pattern_t *last = NULL;
669 int implicit = 1; /* used to detect logical AND operator */
670 struct pattern_flags *entry;
675 memset (&ps, 0, sizeof (ps));
677 ps.dsize = str_len (s);
693 snprintf (err->data, err->dsize, _("error in pattern at: %s"),
698 /* A & B | C == (A & B) | C */
699 tmp = new_pattern ();
701 tmp->child = curlist;
715 if (implicit && or) {
716 /* A | B & C == (A | B) & C */
717 tmp = new_pattern ();
719 tmp->child = curlist;
725 tmp = new_pattern ();
727 tmp->alladdr = alladdr;
737 ps.dptr++; /* move past the ~ */
738 if ((entry = lookup_tag (*ps.dptr)) == NULL) {
739 snprintf (err->data, err->dsize, _("%c: invalid command"), *ps.dptr);
740 mutt_pattern_free (&curlist);
743 if (entry->class && (flags & entry->class) == 0) {
744 snprintf (err->data, err->dsize, _("%c: not supported in this mode"),
746 mutt_pattern_free (&curlist);
751 ps.dptr++; /* eat the operator and any optional whitespace */
754 if (entry->eat_arg) {
756 snprintf (err->data, err->dsize, _("missing parameter"));
757 mutt_pattern_free (&curlist);
760 if (entry->eat_arg (tmp, &ps, err) == -1) {
761 mutt_pattern_free (&curlist);
768 p = find_matching_paren (ps.dptr + 1);
770 snprintf (err->data, err->dsize, _("mismatched parenthesis: %s"),
772 mutt_pattern_free (&curlist);
775 /* compile the sub-expression */
776 buf = str_substrdup (ps.dptr + 1, p);
777 if ((tmp = mutt_pattern_comp (buf, flags, err)) == NULL) {
779 mutt_pattern_free (&curlist);
789 tmp->alladdr |= alladdr;
792 ps.dptr = p + 1; /* restore location */
795 snprintf (err->data, err->dsize, _("error in pattern at: %s"), ps.dptr);
796 mutt_pattern_free (&curlist);
801 strfcpy (err->data, _("empty pattern"), err->dsize);
805 tmp = new_pattern ();
806 tmp->op = or ? M_OR : M_AND;
807 tmp->child = curlist;
814 perform_and (pattern_t * pat, pattern_exec_flag flags, CONTEXT * ctx,
817 for (; pat; pat = pat->next)
818 if (mutt_pattern_exec (pat, flags, ctx, hdr) <= 0)
824 perform_or (struct pattern_t *pat, pattern_exec_flag flags, CONTEXT * ctx,
827 for (; pat; pat = pat->next)
828 if (mutt_pattern_exec (pat, flags, ctx, hdr) > 0)
833 static int match_adrlist (regex_t * rx, int match_personal, int alladdr,
841 for (a = va_arg (ap, ADDRESS *); a; a = a->next) {
843 ((a->mailbox && regexec (rx, a->mailbox, 0, NULL, 0) == 0) ||
844 (match_personal && a->personal &&
845 regexec (rx, a->personal, 0, NULL, 0) == 0))) {
847 return (!alladdr); /* Found match, or non-match if alladdr */
852 return alladdr; /* No matches, or all matches if alladdr */
855 static int match_reference (regex_t * rx, LIST * refs)
857 for (; refs; refs = refs->next)
858 if (regexec (rx, refs->data, 0, NULL, 0) == 0)
863 int mutt_is_list_recipient (int alladdr, ADDRESS * a1, ADDRESS * a2)
865 for (; a1; a1 = a1->next)
866 if (alladdr ^ mutt_is_subscribed_list (a1))
868 for (; a2; a2 = a2->next)
869 if (alladdr ^ mutt_is_subscribed_list (a2))
874 int mutt_is_list_cc (int alladdr, ADDRESS * a1, ADDRESS * a2)
876 for (; a1; a1 = a1->next)
877 if (alladdr ^ mutt_is_mail_list (a1))
879 for (; a2; a2 = a2->next)
880 if (alladdr ^ mutt_is_mail_list (a2))
885 static int match_user (int alladdr, ADDRESS * a1, ADDRESS * a2)
887 for (; a1; a1 = a1->next)
888 if (alladdr ^ mutt_addr_is_user (a1))
890 for (; a2; a2 = a2->next)
891 if (alladdr ^ mutt_addr_is_user (a2))
896 /* test if name is considered a real name, i.e. consists of at least 2
897 * space-separated words of which none may end in a dot
899 static int valid_realname (const char *name)
901 const char *p = name;
908 /* skip abbr. parts of names (e.g. 'J. User') */
916 M_MATCH_FULL_ADDRESS match both personal and machine address */
918 mutt_pattern_exec (struct pattern_t *pat, pattern_exec_flag flags,
919 CONTEXT * ctx, HEADER * h)
925 return (pat->not ^ (perform_and (pat->child, flags, ctx, h) > 0));
927 return (pat->not ^ (perform_or (pat->child, flags, ctx, h) > 0));
931 return (pat->not ^ h->expired);
933 return (pat->not ^ h->superseded);
935 return (pat->not ^ h->flagged);
937 return (pat->not ^ h->tagged);
939 return (pat->not ? h->old || h->read : !(h->old || h->read));
941 return (pat->not ? h->read : !h->read);
943 return (pat->not ^ h->replied);
945 return (pat->not ? (!h->old || h->read) : (h->old && !h->read));
947 return (pat->not ^ h->read);
949 return (pat->not ^ h->deleted);
951 return (pat->not ^ (h->msgno >= pat->min - 1 && (pat->max == M_MAXRANGE ||
956 not ^ (h->date_sent >= pat->min && h->date_sent <= pat->max));
957 case M_DATE_RECEIVED:
958 return (pat->not ^ (h->received >= pat->min && h->received <= pat->max));
963 not ^ msg_search (ctx, pat->rx, buf, sizeof (buf), pat->op,
966 return (pat->not ^ match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
967 pat->alladdr, 1, h->env->sender));
969 return (pat->not ^ match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
970 pat->alladdr, 1, h->env->from));
972 return (pat->not ^ match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
973 pat->alladdr, 1, h->env->to));
975 return (pat->not ^ match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
976 pat->alladdr, 1, h->env->cc));
979 not ^ (h->env && h->env->subject
980 && regexec (pat->rx, h->env->subject, 0, NULL, 0) == 0));
983 not ^ (h->env && h->env->message_id
984 && regexec (pat->rx, h->env->message_id, 0, NULL,
987 return (pat->not ^ (h->score >= pat->min && (pat->max == M_MAXRANGE ||
988 h->score <= pat->max)));
991 not ^ (h->content->length >= pat->min
992 && (pat->max == M_MAXRANGE
993 || h->content->length <= pat->max)));
995 return (pat->not ^ match_reference (pat->rx, h->env->references));
999 && match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
1000 pat->alladdr, 4, h->env->from,
1001 h->env->sender, h->env->to,
1006 && match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
1007 pat->alladdr, 2, h->env->to,
1012 && mutt_is_list_cc (pat->alladdr, h->env->to,
1014 case M_SUBSCRIBED_LIST:
1017 && mutt_is_list_recipient (pat->alladdr, h->env->to,
1019 case M_PERSONAL_RECIP:
1022 && match_user (pat->alladdr, h->env->to, h->env->cc)));
1023 case M_PERSONAL_FROM:
1025 not ^ (h->env && match_user (pat->alladdr, h->env->from, NULL)));
1027 return (pat->not ^ (h->collapsed && h->num_hidden > 1));
1031 return (pat->not ^ ((h->security & SIGN) ? 1 : 0));
1032 case M_CRYPT_VERIFIED:
1035 return (pat->not ^ ((h->security & GOODSIGN) ? 1 : 0));
1036 case M_CRYPT_ENCRYPT:
1039 return (pat->not ^ ((h->security & ENCRYPT) ? 1 : 0));
1041 if (!(WithCrypto & APPLICATION_PGP))
1043 return (pat->not ^ ((h->security & APPLICATION_PGP)
1044 && (h->security & PGPKEY)));
1047 not ^ (h->env->x_label
1048 && regexec (pat->rx, h->env->x_label, 0, NULL, 0) == 0));
1051 not ^ (h->env->spam && h->env->spam->data
1052 && regexec (pat->rx, h->env->spam->data, 0, NULL,
1055 return (pat->not ^ (h->thread && h->thread->duplicate_thread));
1056 case M_UNREFERENCED:
1057 return (pat->not ^ (h->thread && !h->thread->child));
1059 return (pat->not ^ (h->content && h->content->type == TYPEMULTIPART));
1062 * we have a match if
1063 * - From: matches $alternates
1064 * - or we have an alias for current address
1065 * - or From: contains valid email address _and_ name has >= 2 fields
1068 not ^ (h->env && h->env->from && (mutt_addr_is_user (h->env->from)
1070 (alias_reverse_lookup
1071 (h->env->from) != NULL)
1072 || (h->env->from->personal
1073 && valid_realname (h->env->
1076 && h->env->from->mailbox)
1081 not ^ (h->env->newsgroups
1082 && regexec (pat->rx, h->env->newsgroups, 0, NULL,
1086 mutt_error (_("error: unknown op %d (report this error)."), pat->op);
1090 static void quote_simple (char *tmp, size_t len, const char *p)
1095 while (*p && i < len - 3) {
1096 if (*p == '\\' || *p == '"')
1104 /* convert a simple search into a real request */
1105 void mutt_check_simple (char *s, size_t len, const char *simple)
1107 char tmp[LONG_STRING];
1109 /* XXX - is ascii_strcasecmp() right here, or should we use locale's
1113 if (!strchr (s, '~')) { /* yup, so spoof a real request */
1114 /* convert old tokens into the new format */
1115 if (ascii_strcasecmp ("all", s) == 0 || !str_cmp ("^", s) || !str_cmp (".", s)) /* ~A is more efficient */
1116 strfcpy (s, "~A", len);
1117 else if (ascii_strcasecmp ("del", s) == 0)
1118 strfcpy (s, "~D", len);
1119 else if (ascii_strcasecmp ("flag", s) == 0)
1120 strfcpy (s, "~F", len);
1121 else if (ascii_strcasecmp ("new", s) == 0)
1122 strfcpy (s, "~N", len);
1123 else if (ascii_strcasecmp ("old", s) == 0)
1124 strfcpy (s, "~O", len);
1125 else if (ascii_strcasecmp ("repl", s) == 0)
1126 strfcpy (s, "~Q", len);
1127 else if (ascii_strcasecmp ("read", s) == 0)
1128 strfcpy (s, "~R", len);
1129 else if (ascii_strcasecmp ("tag", s) == 0)
1130 strfcpy (s, "~T", len);
1131 else if (ascii_strcasecmp ("unread", s) == 0)
1132 strfcpy (s, "~U", len);
1134 quote_simple (tmp, sizeof (tmp), s);
1135 mutt_expand_fmt (s, len, simple, tmp);
1140 int mutt_pattern_func (int op, char *prompt)
1143 char buf[LONG_STRING] = "", *simple, error[STRING];
1147 strfcpy (buf, NONULL (Context->pattern), sizeof (buf));
1148 if (prompt || op != M_LIMIT)
1149 if (mutt_get_field (prompt, buf, sizeof (buf), M_PATTERN | M_CLEAR) != 0)
1153 strncpy (buf, "~A", sizeof (buf));
1158 mutt_message _("Compiling search pattern...");
1160 simple = str_dup (buf);
1161 mutt_check_simple (buf, sizeof (buf), NONULL (SimpleSearch));
1164 err.dsize = sizeof (error);
1165 if ((pat = mutt_pattern_comp (buf, M_FULL_MSG, &err)) == NULL) {
1167 mutt_error ("%s", err.data);
1171 mutt_message _("Executing command on matching messages...");
1173 #define THIS_BODY Context->hdrs[i]->content
1175 if (op == M_LIMIT) {
1176 Context->vcount = 0;
1178 Context->collapsed = 0;
1180 for (i = 0; i < Context->msgcount; i++) {
1181 /* new limit pattern implicitly uncollapses all threads */
1182 Context->hdrs[i]->virtual = -1;
1183 Context->hdrs[i]->limited = 0;
1184 Context->hdrs[i]->collapsed = 0;
1185 Context->hdrs[i]->num_hidden = 0;
1186 if (mutt_pattern_exec
1187 (pat, M_MATCH_FULL_ADDRESS, Context, Context->hdrs[i])) {
1188 Context->hdrs[i]->virtual = Context->vcount;
1189 Context->hdrs[i]->limited = 1;
1190 Context->v2r[Context->vcount] = i;
1192 Context->vsize += THIS_BODY->length + THIS_BODY->offset -
1193 THIS_BODY->hdr_offset;
1198 for (i = 0; i < Context->vcount; i++) {
1199 if (mutt_pattern_exec
1200 (pat, M_MATCH_FULL_ADDRESS, Context,
1201 Context->hdrs[Context->v2r[i]])) {
1204 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_PURGED,
1207 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_DELETE,
1212 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_TAG,
1222 mutt_clear_error ();
1224 if (op == M_LIMIT) {
1225 mem_free (&Context->pattern);
1226 if (Context->limit_pattern)
1227 mutt_pattern_free (&Context->limit_pattern);
1228 if (!Context->vcount) {
1229 mutt_error _("No messages matched criteria.");
1232 Context->vcount = Context->msgcount;
1233 /* restore full display */
1234 for (i = 0; i < Context->msgcount; i++) {
1235 Context->hdrs[i]->virtual = i;
1236 Context->v2r[i] = i;
1240 else if (str_ncmp (buf, "~A", 2) != 0) {
1241 Context->pattern = simple;
1242 simple = NULL; /* don't clobber it */
1243 Context->limit_pattern = mutt_pattern_comp (buf, M_FULL_MSG, &err);
1247 mutt_pattern_free (&pat);
1251 int mutt_search_command (int cur, int op)
1255 char temp[LONG_STRING];
1261 if (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE) {
1262 strfcpy (buf, LastSearch, sizeof (buf));
1263 if (mutt_get_field ((op == OP_SEARCH) ? _("Search for: ") :
1264 _("Reverse search for: "), buf, sizeof (buf),
1265 M_CLEAR | M_PATTERN) != 0 || !buf[0])
1268 if (op == OP_SEARCH)
1269 unset_option (OPTSEARCHREVERSE);
1271 set_option (OPTSEARCHREVERSE);
1273 /* compare the *expanded* version of the search pattern in case
1274 $simple_search has changed while we were searching */
1275 strfcpy (temp, buf, sizeof (temp));
1276 mutt_check_simple (temp, sizeof (temp), NONULL (SimpleSearch));
1278 if (!SearchPattern || str_cmp (temp, LastSearchExpn)) {
1279 set_option (OPTSEARCHINVALID);
1280 strfcpy (LastSearch, buf, sizeof (LastSearch));
1281 mutt_message _("Compiling search pattern...");
1283 mutt_pattern_free (&SearchPattern);
1285 err.dsize = sizeof (error);
1286 if ((SearchPattern =
1287 mutt_pattern_comp (temp, M_FULL_MSG, &err)) == NULL) {
1288 mutt_error ("%s", error);
1291 mutt_clear_error ();
1294 else if (!SearchPattern) {
1295 mutt_error _("No search pattern.");
1300 if (option (OPTSEARCHINVALID)) {
1301 for (i = 0; i < Context->msgcount; i++)
1302 Context->hdrs[i]->searched = 0;
1303 unset_option (OPTSEARCHINVALID);
1306 incr = (option (OPTSEARCHREVERSE)) ? -1 : 1;
1307 if (op == OP_SEARCH_OPPOSITE)
1310 for (i = cur + incr, j = 0; j != Context->vcount; j++) {
1311 if (i > Context->vcount - 1) {
1313 if (option (OPTWRAPSEARCH))
1314 mutt_message (_("Search wrapped to top."));
1317 mutt_message _("Search hit bottom without finding match");
1323 i = Context->vcount - 1;
1324 if (option (OPTWRAPSEARCH))
1325 mutt_message (_("Search wrapped to bottom."));
1328 mutt_message _("Search hit top without finding match");
1334 h = Context->hdrs[Context->v2r[i]];
1336 /* if we've already evaulated this message, use the cached value */
1341 /* remember that we've already searched this message */
1345 (SearchPattern, M_MATCH_FULL_ADDRESS, Context, h) > 0)))
1350 mutt_error _("Search interrupted.");
1359 mutt_error _("Not found.");