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 'v', M_COLLAPSED, 0, NULL}, {
84 'V', M_CRYPT_VERIFIED, 0, NULL},
87 'w', M_NEWSGROUPS, 0, eat_regexp},
90 'x', M_REFERENCE, 0, eat_regexp}, {
91 'y', M_XLABEL, 0, eat_regexp}, {
92 'z', M_SIZE, 0, eat_range}, {
93 '=', M_DUPLICATED, 0, NULL}, {
94 '$', M_UNREFERENCED, 0, NULL}, {
95 '*', M_REALNAME, 0, NULL}, {
99 static pattern_t *SearchPattern = NULL; /* current search pattern */
100 static char LastSearch[STRING] = { 0 }; /* last pattern searched for */
101 static char LastSearchExpn[LONG_STRING] = { 0 }; /* expanded version of
104 #define M_MAXRANGE -1
106 /* constants for parse_date_range() */
107 #define M_PDR_NONE 0x0000
108 #define M_PDR_MINUS 0x0001
109 #define M_PDR_PLUS 0x0002
110 #define M_PDR_WINDOW 0x0004
111 #define M_PDR_ABSOLUTE 0x0008
112 #define M_PDR_DONE 0x0010
113 #define M_PDR_ERROR 0x0100
114 #define M_PDR_ERRORDONE (M_PDR_ERROR | M_PDR_DONE)
117 int mutt_getvaluebychar (char ch, struct mapping_t *table)
121 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)
133 if (isalpha ((unsigned char) *s) && isupper ((unsigned char) *s))
134 return 0; /* case-sensitive */
137 return REG_ICASE; /* case-insensitive */
141 msg_search (CONTEXT * ctx, regex_t * rx, char *buf, size_t blen, int op,
144 char tempfile[_POSIX_PATH_MAX];
151 HEADER *h = ctx->hdrs[msgno];
153 if ((msg = mx_open_message (ctx, msgno)) != NULL) {
154 if (option (OPTTHOROUGHSRC)) {
155 /* decode the header / body */
156 memset (&s, 0, sizeof (s));
158 s.flags = M_CHARCONV;
159 mutt_mktemp (tempfile);
160 if ((s.fpout = safe_fopen (tempfile, "w+")) == NULL) {
161 mutt_perror (tempfile);
166 mutt_copy_header (msg->fp, h, s.fpout, CH_FROM | CH_DECODE, NULL);
168 if (op != M_HEADER) {
169 mutt_parse_mime_message (ctx, h);
171 if (WithCrypto && (h->security & ENCRYPT)
172 && !crypt_valid_passphrase (h->security)) {
173 mx_close_message (&msg);
181 fseek (msg->fp, h->offset, 0);
182 mutt_body_handler (h->content, &s);
188 fstat (fileno (fp), &st);
189 lng = (long) st.st_size;
192 /* raw header / body */
195 fseek (fp, h->offset, 0);
196 lng = h->content->offset - h->offset;
198 if (op != M_HEADER) {
200 fseek (fp, h->content->offset, 0);
201 lng += h->content->length;
205 /* search the file "fp" */
207 if (fgets (buf, blen - 1, fp) == NULL)
208 break; /* don't loop forever */
209 if (regexec (rx, buf, 0, NULL, 0) == 0) {
213 lng -= str_len (buf);
216 mx_close_message (&msg);
218 if (option (OPTTHOROUGHSRC)) {
227 int eat_regexp (pattern_t * pat, BUFFER * s, BUFFER * err)
232 memset (&buf, 0, sizeof (buf));
233 if (mutt_extract_token (&buf, s, M_TOKEN_PATTERN | M_TOKEN_COMMENT) != 0 ||
235 snprintf (err->data, err->dsize, _("Error in expression: %s"), s->dptr);
238 pat->rx = mem_malloc (sizeof (regex_t));
240 REGCOMP (pat->rx, buf.data,
241 REG_NEWLINE | REG_NOSUB | mutt_which_case (buf.data));
242 mem_free (&buf.data);
244 regerror (r, pat->rx, err->data, err->dsize);
252 int eat_range (pattern_t * pat, BUFFER * s, BUFFER * err)
255 int do_exclusive = 0;
259 * If simple_search is set to "~m %s", the range will have double quotes
262 if (*s->dptr == '"') {
268 if ((*s->dptr != '-') && (*s->dptr != '<')) {
270 if (*s->dptr == '>') {
271 pat->max = M_MAXRANGE;
272 pat->min = strtol (s->dptr + 1, &tmp, 0) + 1; /* exclusive range */
275 pat->min = strtol (s->dptr, &tmp, 0);
276 if (toupper ((unsigned char) *tmp) == 'K') { /* is there a prefix? */
280 else if (toupper ((unsigned char) *tmp) == 'M') {
284 if (*s->dptr == '>') {
301 if (isdigit ((unsigned char) *tmp)) {
303 pat->max = strtol (tmp, &tmp, 0);
304 if (toupper ((unsigned char) *tmp) == 'K') {
308 else if (toupper ((unsigned char) *tmp) == 'M') {
316 pat->max = M_MAXRANGE;
318 if (skip_quote && *tmp == '"')
326 static const char *getDate (const char *s, struct tm *t, BUFFER * err)
329 time_t now = time (NULL);
330 struct tm *tm = localtime (&now);
332 t->tm_mday = strtol (s, &p, 10);
333 if (t->tm_mday < 1 || t->tm_mday > 31) {
334 snprintf (err->data, err->dsize, _("Invalid day of month: %s"), s);
338 /* fill in today's month and year */
339 t->tm_mon = tm->tm_mon;
340 t->tm_year = tm->tm_year;
344 t->tm_mon = strtol (p, &p, 10) - 1;
345 if (t->tm_mon < 0 || t->tm_mon > 11) {
346 snprintf (err->data, err->dsize, _("Invalid month: %s"), p);
350 t->tm_year = tm->tm_year;
354 t->tm_year = strtol (p, &p, 10);
355 if (t->tm_year < 70) /* year 2000+ */
357 else if (t->tm_year > 1900)
366 static const char *get_offset (struct tm *tm, const char *s, int sign)
369 int offset = strtol (s, &ps, 0);
371 if ((sign < 0 && offset > 0) || (sign > 0 && offset < 0))
376 tm->tm_year += offset;
379 tm->tm_mon += offset;
382 tm->tm_mday += 7 * offset;
385 tm->tm_mday += offset;
390 mutt_normalize_time (tm);
394 static void adjust_date_range (struct tm *min, struct tm *max)
396 if (min->tm_year > max->tm_year
397 || (min->tm_year == max->tm_year && min->tm_mon > max->tm_mon)
398 || (min->tm_year == max->tm_year && min->tm_mon == max->tm_mon
399 && min->tm_mday > max->tm_mday)) {
403 min->tm_year = max->tm_year;
407 min->tm_mon = max->tm_mon;
411 min->tm_mday = max->tm_mday;
414 min->tm_hour = min->tm_min = min->tm_sec = 0;
416 max->tm_min = max->tm_sec = 59;
420 static const char *parse_date_range (const char *pc, struct tm *min,
421 struct tm *max, int haveMin,
422 struct tm *baseMin, BUFFER * err)
424 int flag = M_PDR_NONE;
426 while (*pc && ((flag & M_PDR_DONE) == 0)) {
434 /* try a range of absolute date minus offset of Ndwmy */
435 pt = get_offset (min, pc, -1);
437 if (flag == M_PDR_NONE) { /* nothing yet and no offset parsed => absolute date? */
438 if (!getDate (pc, max, err))
439 flag |= (M_PDR_ABSOLUTE | M_PDR_ERRORDONE); /* done bad */
441 /* reestablish initial base minimum if not specified */
443 memcpy (min, baseMin, sizeof (struct tm));
444 flag |= (M_PDR_ABSOLUTE | M_PDR_DONE); /* done good */
448 flag |= M_PDR_ERRORDONE;
452 if (flag == M_PDR_NONE && !haveMin) { /* the very first "-3d" without a previous absolute date */
453 max->tm_year = min->tm_year;
454 max->tm_mon = min->tm_mon;
455 max->tm_mday = min->tm_mday;
462 { /* enlarge plusRange */
463 pt = get_offset (max, pc, 1);
465 flag |= M_PDR_ERRORDONE;
473 { /* enlarge window in both directions */
474 pt = get_offset (min, pc, -1);
476 flag |= M_PDR_ERRORDONE;
478 pc = get_offset (max, pc, 1);
479 flag |= M_PDR_WINDOW;
484 flag |= M_PDR_ERRORDONE;
488 if ((flag & M_PDR_ERROR) && !(flag & M_PDR_ABSOLUTE)) { /* getDate has its own error message, don't overwrite it here */
489 snprintf (err->data, err->dsize, _("Invalid relative date: %s"), pc - 1);
491 return ((flag & M_PDR_ERROR) ? NULL : pc);
494 static int eat_date (pattern_t * pat, BUFFER * s, BUFFER * err)
499 memset (&buffer, 0, sizeof (buffer));
500 if (mutt_extract_token (&buffer, s, M_TOKEN_COMMENT | M_TOKEN_PATTERN) != 0
502 strfcpy (err->data, _("error in expression"), err->dsize);
506 memset (&min, 0, sizeof (min));
507 /* the `0' time is Jan 1, 1970 UTC, so in order to prevent a negative time
508 when doing timezone conversion, we use Jan 2, 1970 UTC as the base
513 memset (&max, 0, sizeof (max));
515 /* Arbitrary year in the future. Don't set this too high
516 or mutt_mktime() returns something larger than will
517 fit in a time_t on some systems */
525 if (strchr ("<>=", buffer.data[0])) {
526 /* offset from current time
527 <3d less than three days ago
528 >3d more than three days ago
529 =3d exactly three days ago */
530 time_t now = time (NULL);
531 struct tm *tm = localtime (&now);
534 if (buffer.data[0] == '<') {
535 memcpy (&min, tm, sizeof (min));
539 memcpy (&max, tm, sizeof (max));
542 if (buffer.data[0] == '=')
546 tm->tm_min = tm->tm_sec = 59;
548 /* force negative offset */
549 get_offset (tm, buffer.data + 1, -1);
552 /* start at the beginning of the day in question */
553 memcpy (&min, &max, sizeof (max));
554 min.tm_hour = min.tm_sec = min.tm_min = 0;
558 const char *pc = buffer.data;
561 int untilNow = FALSE;
563 if (isdigit ((unsigned char) *pc)) {
564 /* mininum date specified */
565 if ((pc = getDate (pc, &min, err)) == NULL) {
566 mem_free (&buffer.data);
572 const char *pt = pc + 1;
575 untilNow = (*pt == '\0');
579 if (!untilNow) { /* max date or relative range/window */
583 if (!haveMin) { /* save base minimum and set current date, e.g. for "-3d+1d" */
584 time_t now = time (NULL);
585 struct tm *tm = localtime (&now);
587 memcpy (&baseMin, &min, sizeof (baseMin));
588 memcpy (&min, tm, sizeof (min));
589 min.tm_hour = min.tm_sec = min.tm_min = 0;
592 /* preset max date for relative offsets,
593 if nothing follows we search for messages on a specific day */
594 max.tm_year = min.tm_year;
595 max.tm_mon = min.tm_mon;
596 max.tm_mday = min.tm_mday;
598 if (!parse_date_range (pc, &min, &max, haveMin, &baseMin, err)) { /* bail out on any parsing error */
599 mem_free (&buffer.data);
605 /* Since we allow two dates to be specified we'll have to adjust that. */
606 adjust_date_range (&min, &max);
608 pat->min = mutt_mktime (&min, 1);
609 pat->max = mutt_mktime (&max, 1);
611 mem_free (&buffer.data);
616 static struct pattern_flags *lookup_tag (char tag)
620 for (i = 0; Flags[i].tag; i++)
621 if (Flags[i].tag == tag)
626 static /* const */ char *find_matching_paren ( /* const */ char *s)
633 else if (*s == ')') {
642 void mutt_pattern_free (pattern_t ** pat)
655 mutt_pattern_free (&tmp->child);
660 pattern_t *mutt_pattern_comp ( /* const */ char *s, int flags, BUFFER * err)
662 pattern_t *curlist = NULL;
664 pattern_t *last = NULL;
668 int implicit = 1; /* used to detect logical AND operator */
669 struct pattern_flags *entry;
674 memset (&ps, 0, sizeof (ps));
676 ps.dsize = str_len (s);
692 snprintf (err->data, err->dsize, _("error in pattern at: %s"),
697 /* A & B | C == (A & B) | C */
698 tmp = new_pattern ();
700 tmp->child = curlist;
714 if (implicit && or) {
715 /* A | B & C == (A | B) & C */
716 tmp = new_pattern ();
718 tmp->child = curlist;
724 tmp = new_pattern ();
726 tmp->alladdr = alladdr;
736 ps.dptr++; /* move past the ~ */
737 if ((entry = lookup_tag (*ps.dptr)) == NULL) {
738 snprintf (err->data, err->dsize, _("%c: invalid command"), *ps.dptr);
739 mutt_pattern_free (&curlist);
742 if (entry->class && (flags & entry->class) == 0) {
743 snprintf (err->data, err->dsize, _("%c: not supported in this mode"),
745 mutt_pattern_free (&curlist);
750 ps.dptr++; /* eat the operator and any optional whitespace */
753 if (entry->eat_arg) {
755 snprintf (err->data, err->dsize, _("missing parameter"));
756 mutt_pattern_free (&curlist);
759 if (entry->eat_arg (tmp, &ps, err) == -1) {
760 mutt_pattern_free (&curlist);
767 p = find_matching_paren (ps.dptr + 1);
769 snprintf (err->data, err->dsize, _("mismatched parenthesis: %s"),
771 mutt_pattern_free (&curlist);
774 /* compile the sub-expression */
775 buf = str_substrdup (ps.dptr + 1, p);
776 if ((tmp = mutt_pattern_comp (buf, flags, err)) == NULL) {
778 mutt_pattern_free (&curlist);
788 tmp->alladdr |= alladdr;
791 ps.dptr = p + 1; /* restore location */
794 snprintf (err->data, err->dsize, _("error in pattern at: %s"), ps.dptr);
795 mutt_pattern_free (&curlist);
800 strfcpy (err->data, _("empty pattern"), err->dsize);
804 tmp = new_pattern ();
805 tmp->op = or ? M_OR : M_AND;
806 tmp->child = curlist;
813 perform_and (pattern_t * pat, pattern_exec_flag flags, CONTEXT * ctx,
816 for (; pat; pat = pat->next)
817 if (mutt_pattern_exec (pat, flags, ctx, hdr) <= 0)
823 perform_or (struct pattern_t *pat, pattern_exec_flag flags, CONTEXT * ctx,
826 for (; pat; pat = pat->next)
827 if (mutt_pattern_exec (pat, flags, ctx, hdr) > 0)
832 static int match_adrlist (regex_t * rx, int match_personal, int alladdr,
840 for (a = va_arg (ap, ADDRESS *); a; a = a->next) {
842 ((a->mailbox && regexec (rx, a->mailbox, 0, NULL, 0) == 0) ||
843 (match_personal && a->personal &&
844 regexec (rx, a->personal, 0, NULL, 0) == 0))) {
846 return (!alladdr); /* Found match, or non-match if alladdr */
851 return alladdr; /* No matches, or all matches if alladdr */
854 static int match_reference (regex_t * rx, LIST * refs)
856 for (; refs; refs = refs->next)
857 if (regexec (rx, refs->data, 0, NULL, 0) == 0)
862 int mutt_is_list_recipient (int alladdr, ADDRESS * a1, ADDRESS * a2)
864 for (; a1; a1 = a1->next)
865 if (alladdr ^ mutt_is_subscribed_list (a1))
867 for (; a2; a2 = a2->next)
868 if (alladdr ^ mutt_is_subscribed_list (a2))
873 int mutt_is_list_cc (int alladdr, ADDRESS * a1, ADDRESS * a2)
875 for (; a1; a1 = a1->next)
876 if (alladdr ^ mutt_is_mail_list (a1))
878 for (; a2; a2 = a2->next)
879 if (alladdr ^ mutt_is_mail_list (a2))
884 static int match_user (int alladdr, ADDRESS * a1, ADDRESS * a2)
886 for (; a1; a1 = a1->next)
887 if (alladdr ^ mutt_addr_is_user (a1))
889 for (; a2; a2 = a2->next)
890 if (alladdr ^ mutt_addr_is_user (a2))
895 /* test if name is considered a real name, i.e. consists of at least 2
896 * space-separated words of which none may end in a dot
898 static int valid_realname (const char *name)
900 const char *p = name;
907 /* skip abbr. parts of names (e.g. 'J. User') */
915 M_MATCH_FULL_ADDRESS match both personal and machine address */
917 mutt_pattern_exec (struct pattern_t *pat, pattern_exec_flag flags,
918 CONTEXT * ctx, HEADER * h)
924 return (pat->not ^ (perform_and (pat->child, flags, ctx, h) > 0));
926 return (pat->not ^ (perform_or (pat->child, flags, ctx, h) > 0));
930 return (pat->not ^ h->expired);
932 return (pat->not ^ h->superseded);
934 return (pat->not ^ h->flagged);
936 return (pat->not ^ h->tagged);
938 return (pat->not ? h->old || h->read : !(h->old || h->read));
940 return (pat->not ? h->read : !h->read);
942 return (pat->not ^ h->replied);
944 return (pat->not ? (!h->old || h->read) : (h->old && !h->read));
946 return (pat->not ^ h->read);
948 return (pat->not ^ h->deleted);
950 return (pat->not ^ (h->msgno >= pat->min - 1 && (pat->max == M_MAXRANGE ||
955 not ^ (h->date_sent >= pat->min && h->date_sent <= pat->max));
956 case M_DATE_RECEIVED:
957 return (pat->not ^ (h->received >= pat->min && h->received <= pat->max));
962 not ^ msg_search (ctx, pat->rx, buf, sizeof (buf), pat->op,
965 return (pat->not ^ match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
966 pat->alladdr, 1, h->env->sender));
968 return (pat->not ^ match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
969 pat->alladdr, 1, h->env->from));
971 return (pat->not ^ match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
972 pat->alladdr, 1, h->env->to));
974 return (pat->not ^ match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
975 pat->alladdr, 1, h->env->cc));
978 not ^ (h->env && h->env->subject
979 && regexec (pat->rx, h->env->subject, 0, NULL, 0) == 0));
982 not ^ (h->env && h->env->message_id
983 && regexec (pat->rx, h->env->message_id, 0, NULL,
986 return (pat->not ^ (h->score >= pat->min && (pat->max == M_MAXRANGE ||
987 h->score <= pat->max)));
990 not ^ (h->content->length >= pat->min
991 && (pat->max == M_MAXRANGE
992 || h->content->length <= pat->max)));
994 return (pat->not ^ match_reference (pat->rx, h->env->references));
998 && match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
999 pat->alladdr, 4, h->env->from,
1000 h->env->sender, h->env->to,
1005 && match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
1006 pat->alladdr, 2, h->env->to,
1011 && mutt_is_list_recipient (pat->alladdr, h->env->to,
1013 case M_PERSONAL_RECIP:
1016 && match_user (pat->alladdr, h->env->to, h->env->cc)));
1017 case M_PERSONAL_FROM:
1019 not ^ (h->env && match_user (pat->alladdr, h->env->from, NULL)));
1021 return (pat->not ^ (h->collapsed && h->num_hidden > 1));
1025 return (pat->not ^ ((h->security & SIGN) ? 1 : 0));
1026 case M_CRYPT_VERIFIED:
1029 return (pat->not ^ ((h->security & GOODSIGN) ? 1 : 0));
1030 case M_CRYPT_ENCRYPT:
1033 return (pat->not ^ ((h->security & ENCRYPT) ? 1 : 0));
1035 if (!(WithCrypto & APPLICATION_PGP))
1037 return (pat->not ^ ((h->security & APPLICATION_PGP)
1038 && (h->security & PGPKEY)));
1041 not ^ (h->env->x_label
1042 && regexec (pat->rx, h->env->x_label, 0, NULL, 0) == 0));
1045 not ^ (h->env->spam && h->env->spam->data
1046 && regexec (pat->rx, h->env->spam->data, 0, NULL,
1049 return (pat->not ^ (h->thread && h->thread->duplicate_thread));
1050 case M_UNREFERENCED:
1051 return (pat->not ^ (h->thread && !h->thread->child));
1053 return (pat->not ^ (h->content && h->content->type == TYPEMULTIPART));
1056 * we have a match if
1057 * - From: matches $alternates
1058 * - or we have an alias for current address
1059 * - or From: contains valid email address _and_ name has >= 2 fields
1062 not ^ (h->env && h->env->from && (mutt_addr_is_user (h->env->from)
1064 (alias_reverse_lookup
1065 (h->env->from) != NULL)
1066 || (h->env->from->personal
1067 && valid_realname (h->env->
1070 && h->env->from->mailbox)
1075 not ^ (h->env->newsgroups
1076 && regexec (pat->rx, h->env->newsgroups, 0, NULL,
1080 mutt_error (_("error: unknown op %d (report this error)."), pat->op);
1084 static void quote_simple (char *tmp, size_t len, const char *p)
1089 while (*p && i < len - 3) {
1090 if (*p == '\\' || *p == '"')
1098 /* convert a simple search into a real request */
1099 void mutt_check_simple (char *s, size_t len, const char *simple)
1101 char tmp[LONG_STRING];
1103 /* XXX - is ascii_strcasecmp() right here, or should we use locale's
1107 if (!strchr (s, '~')) { /* yup, so spoof a real request */
1108 /* convert old tokens into the new format */
1109 if (ascii_strcasecmp ("all", s) == 0 || !str_cmp ("^", s) || !str_cmp (".", s)) /* ~A is more efficient */
1110 strfcpy (s, "~A", len);
1111 else if (ascii_strcasecmp ("del", s) == 0)
1112 strfcpy (s, "~D", len);
1113 else if (ascii_strcasecmp ("flag", s) == 0)
1114 strfcpy (s, "~F", len);
1115 else if (ascii_strcasecmp ("new", s) == 0)
1116 strfcpy (s, "~N", len);
1117 else if (ascii_strcasecmp ("old", s) == 0)
1118 strfcpy (s, "~O", len);
1119 else if (ascii_strcasecmp ("repl", s) == 0)
1120 strfcpy (s, "~Q", len);
1121 else if (ascii_strcasecmp ("read", s) == 0)
1122 strfcpy (s, "~R", len);
1123 else if (ascii_strcasecmp ("tag", s) == 0)
1124 strfcpy (s, "~T", len);
1125 else if (ascii_strcasecmp ("unread", s) == 0)
1126 strfcpy (s, "~U", len);
1128 quote_simple (tmp, sizeof (tmp), s);
1129 mutt_expand_fmt (s, len, simple, tmp);
1134 int mutt_pattern_func (int op, char *prompt)
1137 char buf[LONG_STRING] = "", *simple, error[STRING];
1141 strfcpy (buf, NONULL (Context->pattern), sizeof (buf));
1142 if (prompt || op != M_LIMIT)
1143 if (mutt_get_field (prompt, buf, sizeof (buf), M_PATTERN | M_CLEAR) != 0)
1147 strncpy (buf, "~A", sizeof (buf));
1152 mutt_message _("Compiling search pattern...");
1154 simple = str_dup (buf);
1155 mutt_check_simple (buf, sizeof (buf), NONULL (SimpleSearch));
1158 err.dsize = sizeof (error);
1159 if ((pat = mutt_pattern_comp (buf, M_FULL_MSG, &err)) == NULL) {
1161 mutt_error ("%s", err.data);
1165 mutt_message _("Executing command on matching messages...");
1167 #define THIS_BODY Context->hdrs[i]->content
1169 if (op == M_LIMIT) {
1170 Context->vcount = 0;
1172 Context->collapsed = 0;
1174 for (i = 0; i < Context->msgcount; i++) {
1175 /* new limit pattern implicitly uncollapses all threads */
1176 Context->hdrs[i]->virtual = -1;
1177 Context->hdrs[i]->limited = 0;
1178 Context->hdrs[i]->collapsed = 0;
1179 Context->hdrs[i]->num_hidden = 0;
1180 if (mutt_pattern_exec
1181 (pat, M_MATCH_FULL_ADDRESS, Context, Context->hdrs[i])) {
1182 Context->hdrs[i]->virtual = Context->vcount;
1183 Context->hdrs[i]->limited = 1;
1184 Context->v2r[Context->vcount] = i;
1186 Context->vsize += THIS_BODY->length + THIS_BODY->offset -
1187 THIS_BODY->hdr_offset;
1192 for (i = 0; i < Context->vcount; i++) {
1193 if (mutt_pattern_exec
1194 (pat, M_MATCH_FULL_ADDRESS, Context,
1195 Context->hdrs[Context->v2r[i]])) {
1198 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_PURGED,
1201 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_DELETE,
1206 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_TAG,
1216 mutt_clear_error ();
1218 if (op == M_LIMIT) {
1219 mem_free (&Context->pattern);
1220 if (Context->limit_pattern)
1221 mutt_pattern_free (&Context->limit_pattern);
1222 if (!Context->vcount) {
1223 mutt_error _("No messages matched criteria.");
1226 Context->vcount = Context->msgcount;
1227 /* restore full display */
1228 for (i = 0; i < Context->msgcount; i++) {
1229 Context->hdrs[i]->virtual = i;
1230 Context->v2r[i] = i;
1234 else if (str_ncmp (buf, "~A", 2) != 0) {
1235 Context->pattern = simple;
1236 simple = NULL; /* don't clobber it */
1237 Context->limit_pattern = mutt_pattern_comp (buf, M_FULL_MSG, &err);
1241 mutt_pattern_free (&pat);
1245 int mutt_search_command (int cur, int op)
1249 char temp[LONG_STRING];
1255 if (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE) {
1256 strfcpy (buf, LastSearch, sizeof (buf));
1257 if (mutt_get_field ((op == OP_SEARCH) ? _("Search for: ") :
1258 _("Reverse search for: "), buf, sizeof (buf),
1259 M_CLEAR | M_PATTERN) != 0 || !buf[0])
1262 if (op == OP_SEARCH)
1263 unset_option (OPTSEARCHREVERSE);
1265 set_option (OPTSEARCHREVERSE);
1267 /* compare the *expanded* version of the search pattern in case
1268 $simple_search has changed while we were searching */
1269 strfcpy (temp, buf, sizeof (temp));
1270 mutt_check_simple (temp, sizeof (temp), NONULL (SimpleSearch));
1272 if (!SearchPattern || str_cmp (temp, LastSearchExpn)) {
1273 set_option (OPTSEARCHINVALID);
1274 strfcpy (LastSearch, buf, sizeof (LastSearch));
1275 mutt_message _("Compiling search pattern...");
1277 mutt_pattern_free (&SearchPattern);
1279 err.dsize = sizeof (error);
1280 if ((SearchPattern =
1281 mutt_pattern_comp (temp, M_FULL_MSG, &err)) == NULL) {
1282 mutt_error ("%s", error);
1285 mutt_clear_error ();
1288 else if (!SearchPattern) {
1289 mutt_error _("No search pattern.");
1294 if (option (OPTSEARCHINVALID)) {
1295 for (i = 0; i < Context->msgcount; i++)
1296 Context->hdrs[i]->searched = 0;
1297 unset_option (OPTSEARCHINVALID);
1300 incr = (option (OPTSEARCHREVERSE)) ? -1 : 1;
1301 if (op == OP_SEARCH_OPPOSITE)
1304 for (i = cur + incr, j = 0; j != Context->vcount; j++) {
1305 if (i > Context->vcount - 1) {
1307 if (option (OPTWRAPSEARCH))
1308 mutt_message (_("Search wrapped to top."));
1311 mutt_message _("Search hit bottom without finding match");
1317 i = Context->vcount - 1;
1318 if (option (OPTWRAPSEARCH))
1319 mutt_message (_("Search wrapped to bottom."));
1322 mutt_message _("Search hit top without finding match");
1328 h = Context->hdrs[Context->v2r[i]];
1330 /* if we've already evaulated this message, use the cached value */
1335 /* remember that we've already searched this message */
1339 (SearchPattern, M_MATCH_FULL_ADDRESS, Context, h) > 0)))
1344 mutt_error _("Search interrupted.");
1353 mutt_error _("Not found.");