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.
35 #include "mutt_crypt.h"
37 static int eat_regexp (pattern_t * pat, BUFFER *, BUFFER *);
38 static int eat_date (pattern_t * pat, BUFFER *, BUFFER *);
39 static int eat_range (pattern_t * pat, BUFFER *, BUFFER *);
41 struct pattern_flags {
42 int tag; /* character used to represent this op */
43 int op; /* operation to perform */
45 int (*eat_arg) (pattern_t *, BUFFER *, BUFFER *);
48 'A', M_ALL, 0, NULL}, {
49 'b', M_BODY, M_FULL_MSG, eat_regexp}, {
50 'B', M_WHOLE_MSG, M_FULL_MSG, eat_regexp}, {
51 'c', M_CC, 0, eat_regexp}, {
52 'C', M_RECIPIENT, 0, eat_regexp}, {
53 'd', M_DATE, 0, eat_date}, {
54 'D', M_DELETED, 0, NULL}, {
55 'e', M_SENDER, 0, eat_regexp}, {
56 'E', M_EXPIRED, 0, NULL}, {
57 'f', M_FROM, 0, eat_regexp}, {
58 'F', M_FLAG, 0, NULL}, {
59 'g', M_CRYPT_SIGN, 0, NULL}, {
60 'G', M_CRYPT_ENCRYPT, 0, NULL}, {
61 'h', M_HEADER, M_FULL_MSG, eat_regexp}, {
62 'H', M_HORMEL, 0, eat_regexp}, {
63 'i', M_ID, 0, eat_regexp}, {
64 'k', M_PGP_KEY, 0, NULL}, {
65 'L', M_ADDRESS, 0, eat_regexp}, {
66 'l', M_LIST, 0, NULL}, {
67 'm', M_MESSAGE, 0, eat_range}, {
68 'n', M_SCORE, 0, eat_range}, {
69 'N', M_NEW, 0, NULL}, {
70 'O', M_OLD, 0, NULL}, {
71 'p', M_PERSONAL_RECIP, 0, NULL}, {
72 'P', M_PERSONAL_FROM, 0, NULL}, {
73 'Q', M_REPLIED, 0, NULL}, {
74 'R', M_READ, 0, NULL}, {
75 'r', M_DATE_RECEIVED, 0, eat_date}, {
76 's', M_SUBJECT, 0, eat_regexp}, {
77 'S', M_SUPERSEDED, 0, NULL}, {
78 'T', M_TAG, 0, NULL}, {
79 't', M_TO, 0, eat_regexp}, {
80 'U', M_UNREAD, 0, NULL}, {
81 'v', M_COLLAPSED, 0, NULL}, {
82 'V', M_CRYPT_VERIFIED, 0, NULL},
85 'w', M_NEWSGROUPS, 0, eat_regexp},
88 'x', M_REFERENCE, 0, eat_regexp}, {
89 'y', M_XLABEL, 0, eat_regexp}, {
90 'z', M_SIZE, 0, eat_range}, {
91 '=', M_DUPLICATED, 0, NULL}, {
92 '$', M_UNREFERENCED, 0, NULL}, {
93 '*', M_REALNAME, 0, NULL}, {
97 static pattern_t *SearchPattern = NULL; /* current search pattern */
98 static char LastSearch[STRING] = { 0 }; /* last pattern searched for */
99 static char LastSearchExpn[LONG_STRING] = { 0 }; /* expanded version of
102 #define M_MAXRANGE -1
104 /* constants for parse_date_range() */
105 #define M_PDR_NONE 0x0000
106 #define M_PDR_MINUS 0x0001
107 #define M_PDR_PLUS 0x0002
108 #define M_PDR_WINDOW 0x0004
109 #define M_PDR_ABSOLUTE 0x0008
110 #define M_PDR_DONE 0x0010
111 #define M_PDR_ERROR 0x0100
112 #define M_PDR_ERRORDONE (M_PDR_ERROR | M_PDR_DONE)
115 int mutt_getvaluebychar (char ch, struct mapping_t *table)
119 for (i = 0; table[i].name; i++) {
120 if (ch == table[i].name[0])
121 return table[i].value;
127 /* if no uppercase letters are given, do a case-insensitive search */
128 int mutt_which_case (const char *s)
131 if (isalpha ((unsigned char) *s) && isupper ((unsigned char) *s))
132 return 0; /* case-sensitive */
135 return REG_ICASE; /* case-insensitive */
139 msg_search (CONTEXT * ctx, regex_t * rx, char *buf, size_t blen, int op,
142 char tempfile[_POSIX_PATH_MAX];
149 HEADER *h = ctx->hdrs[msgno];
151 if ((msg = mx_open_message (ctx, msgno)) != NULL) {
152 if (option (OPTTHOROUGHSRC)) {
153 /* decode the header / body */
154 memset (&s, 0, sizeof (s));
156 s.flags = M_CHARCONV;
157 mutt_mktemp (tempfile);
158 if ((s.fpout = safe_fopen (tempfile, "w+")) == NULL) {
159 mutt_perror (tempfile);
164 mutt_copy_header (msg->fp, h, s.fpout, CH_FROM | CH_DECODE, NULL);
166 if (op != M_HEADER) {
167 mutt_parse_mime_message (ctx, h);
169 if (WithCrypto && (h->security & ENCRYPT)
170 && !crypt_valid_passphrase (h->security)) {
171 mx_close_message (&msg);
179 fseek (msg->fp, h->offset, 0);
180 mutt_body_handler (h->content, &s);
186 fstat (fileno (fp), &st);
187 lng = (long) st.st_size;
190 /* raw header / body */
193 fseek (fp, h->offset, 0);
194 lng = h->content->offset - h->offset;
196 if (op != M_HEADER) {
198 fseek (fp, h->content->offset, 0);
199 lng += h->content->length;
203 /* search the file "fp" */
205 if (fgets (buf, blen - 1, fp) == NULL)
206 break; /* don't loop forever */
207 if (regexec (rx, buf, 0, NULL, 0) == 0) {
211 lng -= str_len (buf);
214 mx_close_message (&msg);
216 if (option (OPTTHOROUGHSRC)) {
225 int eat_regexp (pattern_t * pat, BUFFER * s, BUFFER * err)
230 memset (&buf, 0, sizeof (buf));
231 if (mutt_extract_token (&buf, s, M_TOKEN_PATTERN | M_TOKEN_COMMENT) != 0 ||
233 snprintf (err->data, err->dsize, _("Error in expression: %s"), s->dptr);
236 pat->rx = mem_malloc (sizeof (regex_t));
238 REGCOMP (pat->rx, buf.data,
239 REG_NEWLINE | REG_NOSUB | mutt_which_case (buf.data));
240 mem_free (&buf.data);
242 regerror (r, pat->rx, err->data, err->dsize);
250 int eat_range (pattern_t * pat, BUFFER * s, BUFFER * err)
253 int do_exclusive = 0;
257 * If simple_search is set to "~m %s", the range will have double quotes
260 if (*s->dptr == '"') {
266 if ((*s->dptr != '-') && (*s->dptr != '<')) {
268 if (*s->dptr == '>') {
269 pat->max = M_MAXRANGE;
270 pat->min = strtol (s->dptr + 1, &tmp, 0) + 1; /* exclusive range */
273 pat->min = strtol (s->dptr, &tmp, 0);
274 if (toupper ((unsigned char) *tmp) == 'K') { /* is there a prefix? */
278 else if (toupper ((unsigned char) *tmp) == 'M') {
282 if (*s->dptr == '>') {
299 if (isdigit ((unsigned char) *tmp)) {
301 pat->max = strtol (tmp, &tmp, 0);
302 if (toupper ((unsigned char) *tmp) == 'K') {
306 else if (toupper ((unsigned char) *tmp) == 'M') {
314 pat->max = M_MAXRANGE;
316 if (skip_quote && *tmp == '"')
324 static const char *getDate (const char *s, struct tm *t, BUFFER * err)
327 time_t now = time (NULL);
328 struct tm *tm = localtime (&now);
330 t->tm_mday = strtol (s, &p, 10);
331 if (t->tm_mday < 1 || t->tm_mday > 31) {
332 snprintf (err->data, err->dsize, _("Invalid day of month: %s"), s);
336 /* fill in today's month and year */
337 t->tm_mon = tm->tm_mon;
338 t->tm_year = tm->tm_year;
342 t->tm_mon = strtol (p, &p, 10) - 1;
343 if (t->tm_mon < 0 || t->tm_mon > 11) {
344 snprintf (err->data, err->dsize, _("Invalid month: %s"), p);
348 t->tm_year = tm->tm_year;
352 t->tm_year = strtol (p, &p, 10);
353 if (t->tm_year < 70) /* year 2000+ */
355 else if (t->tm_year > 1900)
364 static const char *get_offset (struct tm *tm, const char *s, int sign)
367 int offset = strtol (s, &ps, 0);
369 if ((sign < 0 && offset > 0) || (sign > 0 && offset < 0))
374 tm->tm_year += offset;
377 tm->tm_mon += offset;
380 tm->tm_mday += 7 * offset;
383 tm->tm_mday += offset;
388 mutt_normalize_time (tm);
392 static void adjust_date_range (struct tm *min, struct tm *max)
394 if (min->tm_year > max->tm_year
395 || (min->tm_year == max->tm_year && min->tm_mon > max->tm_mon)
396 || (min->tm_year == max->tm_year && min->tm_mon == max->tm_mon
397 && min->tm_mday > max->tm_mday)) {
401 min->tm_year = max->tm_year;
405 min->tm_mon = max->tm_mon;
409 min->tm_mday = max->tm_mday;
412 min->tm_hour = min->tm_min = min->tm_sec = 0;
414 max->tm_min = max->tm_sec = 59;
418 static const char *parse_date_range (const char *pc, struct tm *min,
419 struct tm *max, int haveMin,
420 struct tm *baseMin, BUFFER * err)
422 int flag = M_PDR_NONE;
424 while (*pc && ((flag & M_PDR_DONE) == 0)) {
432 /* try a range of absolute date minus offset of Ndwmy */
433 pt = get_offset (min, pc, -1);
435 if (flag == M_PDR_NONE) { /* nothing yet and no offset parsed => absolute date? */
436 if (!getDate (pc, max, err))
437 flag |= (M_PDR_ABSOLUTE | M_PDR_ERRORDONE); /* done bad */
439 /* reestablish initial base minimum if not specified */
441 memcpy (min, baseMin, sizeof (struct tm));
442 flag |= (M_PDR_ABSOLUTE | M_PDR_DONE); /* done good */
446 flag |= M_PDR_ERRORDONE;
450 if (flag == M_PDR_NONE && !haveMin) { /* the very first "-3d" without a previous absolute date */
451 max->tm_year = min->tm_year;
452 max->tm_mon = min->tm_mon;
453 max->tm_mday = min->tm_mday;
460 { /* enlarge plusRange */
461 pt = get_offset (max, pc, 1);
463 flag |= M_PDR_ERRORDONE;
471 { /* enlarge window in both directions */
472 pt = get_offset (min, pc, -1);
474 flag |= M_PDR_ERRORDONE;
476 pc = get_offset (max, pc, 1);
477 flag |= M_PDR_WINDOW;
482 flag |= M_PDR_ERRORDONE;
486 if ((flag & M_PDR_ERROR) && !(flag & M_PDR_ABSOLUTE)) { /* getDate has its own error message, don't overwrite it here */
487 snprintf (err->data, err->dsize, _("Invalid relative date: %s"), pc - 1);
489 return ((flag & M_PDR_ERROR) ? NULL : pc);
492 static int eat_date (pattern_t * pat, BUFFER * s, BUFFER * err)
497 memset (&buffer, 0, sizeof (buffer));
498 if (mutt_extract_token (&buffer, s, M_TOKEN_COMMENT | M_TOKEN_PATTERN) != 0
500 strfcpy (err->data, _("error in expression"), err->dsize);
504 memset (&min, 0, sizeof (min));
505 /* the `0' time is Jan 1, 1970 UTC, so in order to prevent a negative time
506 when doing timezone conversion, we use Jan 2, 1970 UTC as the base
511 memset (&max, 0, sizeof (max));
513 /* Arbitrary year in the future. Don't set this too high
514 or mutt_mktime() returns something larger than will
515 fit in a time_t on some systems */
523 if (strchr ("<>=", buffer.data[0])) {
524 /* offset from current time
525 <3d less than three days ago
526 >3d more than three days ago
527 =3d exactly three days ago */
528 time_t now = time (NULL);
529 struct tm *tm = localtime (&now);
532 if (buffer.data[0] == '<') {
533 memcpy (&min, tm, sizeof (min));
537 memcpy (&max, tm, sizeof (max));
540 if (buffer.data[0] == '=')
544 tm->tm_min = tm->tm_sec = 59;
546 /* force negative offset */
547 get_offset (tm, buffer.data + 1, -1);
550 /* start at the beginning of the day in question */
551 memcpy (&min, &max, sizeof (max));
552 min.tm_hour = min.tm_sec = min.tm_min = 0;
556 const char *pc = buffer.data;
559 int untilNow = FALSE;
561 if (isdigit ((unsigned char) *pc)) {
562 /* mininum date specified */
563 if ((pc = getDate (pc, &min, err)) == NULL) {
564 mem_free (&buffer.data);
570 const char *pt = pc + 1;
573 untilNow = (*pt == '\0');
577 if (!untilNow) { /* max date or relative range/window */
581 if (!haveMin) { /* save base minimum and set current date, e.g. for "-3d+1d" */
582 time_t now = time (NULL);
583 struct tm *tm = localtime (&now);
585 memcpy (&baseMin, &min, sizeof (baseMin));
586 memcpy (&min, tm, sizeof (min));
587 min.tm_hour = min.tm_sec = min.tm_min = 0;
590 /* preset max date for relative offsets,
591 if nothing follows we search for messages on a specific day */
592 max.tm_year = min.tm_year;
593 max.tm_mon = min.tm_mon;
594 max.tm_mday = min.tm_mday;
596 if (!parse_date_range (pc, &min, &max, haveMin, &baseMin, err)) { /* bail out on any parsing error */
597 mem_free (&buffer.data);
603 /* Since we allow two dates to be specified we'll have to adjust that. */
604 adjust_date_range (&min, &max);
606 pat->min = mutt_mktime (&min, 1);
607 pat->max = mutt_mktime (&max, 1);
609 mem_free (&buffer.data);
614 static struct pattern_flags *lookup_tag (char tag)
618 for (i = 0; Flags[i].tag; i++)
619 if (Flags[i].tag == tag)
624 static /* const */ char *find_matching_paren ( /* const */ char *s)
631 else if (*s == ')') {
640 void mutt_pattern_free (pattern_t ** pat)
653 mutt_pattern_free (&tmp->child);
658 pattern_t *mutt_pattern_comp ( /* const */ char *s, int flags, BUFFER * err)
660 pattern_t *curlist = NULL;
662 pattern_t *last = NULL;
666 int implicit = 1; /* used to detect logical AND operator */
667 struct pattern_flags *entry;
672 memset (&ps, 0, sizeof (ps));
674 ps.dsize = str_len (s);
690 snprintf (err->data, err->dsize, _("error in pattern at: %s"),
695 /* A & B | C == (A & B) | C */
696 tmp = new_pattern ();
698 tmp->child = curlist;
712 if (implicit && or) {
713 /* A | B & C == (A | B) & C */
714 tmp = new_pattern ();
716 tmp->child = curlist;
722 tmp = new_pattern ();
724 tmp->alladdr = alladdr;
734 ps.dptr++; /* move past the ~ */
735 if ((entry = lookup_tag (*ps.dptr)) == NULL) {
736 snprintf (err->data, err->dsize, _("%c: invalid command"), *ps.dptr);
737 mutt_pattern_free (&curlist);
740 if (entry->class && (flags & entry->class) == 0) {
741 snprintf (err->data, err->dsize, _("%c: not supported in this mode"),
743 mutt_pattern_free (&curlist);
748 ps.dptr++; /* eat the operator and any optional whitespace */
751 if (entry->eat_arg) {
753 snprintf (err->data, err->dsize, _("missing parameter"));
754 mutt_pattern_free (&curlist);
757 if (entry->eat_arg (tmp, &ps, err) == -1) {
758 mutt_pattern_free (&curlist);
765 p = find_matching_paren (ps.dptr + 1);
767 snprintf (err->data, err->dsize, _("mismatched parenthesis: %s"),
769 mutt_pattern_free (&curlist);
772 /* compile the sub-expression */
773 buf = str_substrdup (ps.dptr + 1, p);
774 if ((tmp = mutt_pattern_comp (buf, flags, err)) == NULL) {
776 mutt_pattern_free (&curlist);
786 tmp->alladdr |= alladdr;
789 ps.dptr = p + 1; /* restore location */
792 snprintf (err->data, err->dsize, _("error in pattern at: %s"), ps.dptr);
793 mutt_pattern_free (&curlist);
798 strfcpy (err->data, _("empty pattern"), err->dsize);
802 tmp = new_pattern ();
803 tmp->op = or ? M_OR : M_AND;
804 tmp->child = curlist;
811 perform_and (pattern_t * pat, pattern_exec_flag flags, CONTEXT * ctx,
814 for (; pat; pat = pat->next)
815 if (mutt_pattern_exec (pat, flags, ctx, hdr) <= 0)
821 perform_or (struct pattern_t *pat, pattern_exec_flag flags, CONTEXT * ctx,
824 for (; pat; pat = pat->next)
825 if (mutt_pattern_exec (pat, flags, ctx, hdr) > 0)
830 static int match_adrlist (regex_t * rx, int match_personal, int alladdr,
838 for (a = va_arg (ap, ADDRESS *); a; a = a->next) {
840 ((a->mailbox && regexec (rx, a->mailbox, 0, NULL, 0) == 0) ||
841 (match_personal && a->personal &&
842 regexec (rx, a->personal, 0, NULL, 0) == 0))) {
844 return (!alladdr); /* Found match, or non-match if alladdr */
849 return alladdr; /* No matches, or all matches if alladdr */
852 static int match_reference (regex_t * rx, LIST * refs)
854 for (; refs; refs = refs->next)
855 if (regexec (rx, refs->data, 0, NULL, 0) == 0)
860 int mutt_is_list_recipient (int alladdr, ADDRESS * a1, ADDRESS * a2)
862 for (; a1; a1 = a1->next)
863 if (alladdr ^ mutt_is_subscribed_list (a1))
865 for (; a2; a2 = a2->next)
866 if (alladdr ^ mutt_is_subscribed_list (a2))
871 int mutt_is_list_cc (int alladdr, ADDRESS * a1, ADDRESS * a2)
873 for (; a1; a1 = a1->next)
874 if (alladdr ^ mutt_is_mail_list (a1))
876 for (; a2; a2 = a2->next)
877 if (alladdr ^ mutt_is_mail_list (a2))
882 static int match_user (int alladdr, ADDRESS * a1, ADDRESS * a2)
884 for (; a1; a1 = a1->next)
885 if (alladdr ^ mutt_addr_is_user (a1))
887 for (; a2; a2 = a2->next)
888 if (alladdr ^ mutt_addr_is_user (a2))
893 /* test if name is considered a real name, i.e. consists of at least 2
894 * space-separated words of which none may end in a dot
896 static int valid_realname (const char *name)
898 const char *p = name;
905 /* skip abbr. parts of names (e.g. 'J. User') */
913 M_MATCH_FULL_ADDRESS match both personal and machine address */
915 mutt_pattern_exec (struct pattern_t *pat, pattern_exec_flag flags,
916 CONTEXT * ctx, HEADER * h)
922 return (pat->not ^ (perform_and (pat->child, flags, ctx, h) > 0));
924 return (pat->not ^ (perform_or (pat->child, flags, ctx, h) > 0));
928 return (pat->not ^ h->expired);
930 return (pat->not ^ h->superseded);
932 return (pat->not ^ h->flagged);
934 return (pat->not ^ h->tagged);
936 return (pat->not ? h->old || h->read : !(h->old || h->read));
938 return (pat->not ? h->read : !h->read);
940 return (pat->not ^ h->replied);
942 return (pat->not ? (!h->old || h->read) : (h->old && !h->read));
944 return (pat->not ^ h->read);
946 return (pat->not ^ h->deleted);
948 return (pat->not ^ (h->msgno >= pat->min - 1 && (pat->max == M_MAXRANGE ||
953 not ^ (h->date_sent >= pat->min && h->date_sent <= pat->max));
954 case M_DATE_RECEIVED:
955 return (pat->not ^ (h->received >= pat->min && h->received <= pat->max));
960 not ^ msg_search (ctx, pat->rx, buf, sizeof (buf), pat->op,
963 return (pat->not ^ match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
964 pat->alladdr, 1, h->env->sender));
966 return (pat->not ^ match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
967 pat->alladdr, 1, h->env->from));
969 return (pat->not ^ match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
970 pat->alladdr, 1, h->env->to));
972 return (pat->not ^ match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
973 pat->alladdr, 1, h->env->cc));
976 not ^ (h->env && h->env->subject
977 && regexec (pat->rx, h->env->subject, 0, NULL, 0) == 0));
980 not ^ (h->env && h->env->message_id
981 && regexec (pat->rx, h->env->message_id, 0, NULL,
984 return (pat->not ^ (h->score >= pat->min && (pat->max == M_MAXRANGE ||
985 h->score <= pat->max)));
988 not ^ (h->content->length >= pat->min
989 && (pat->max == M_MAXRANGE
990 || h->content->length <= pat->max)));
992 return (pat->not ^ match_reference (pat->rx, h->env->references));
996 && match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
997 pat->alladdr, 4, h->env->from,
998 h->env->sender, h->env->to,
1003 && match_adrlist (pat->rx, flags & M_MATCH_FULL_ADDRESS,
1004 pat->alladdr, 2, h->env->to,
1009 && mutt_is_list_recipient (pat->alladdr, h->env->to,
1011 case M_PERSONAL_RECIP:
1014 && match_user (pat->alladdr, h->env->to, h->env->cc)));
1015 case M_PERSONAL_FROM:
1017 not ^ (h->env && match_user (pat->alladdr, h->env->from, NULL)));
1019 return (pat->not ^ (h->collapsed && h->num_hidden > 1));
1023 return (pat->not ^ ((h->security & SIGN) ? 1 : 0));
1024 case M_CRYPT_VERIFIED:
1027 return (pat->not ^ ((h->security & GOODSIGN) ? 1 : 0));
1028 case M_CRYPT_ENCRYPT:
1031 return (pat->not ^ ((h->security & ENCRYPT) ? 1 : 0));
1033 if (!(WithCrypto & APPLICATION_PGP))
1035 return (pat->not ^ ((h->security & APPLICATION_PGP)
1036 && (h->security & PGPKEY)));
1039 not ^ (h->env->x_label
1040 && regexec (pat->rx, h->env->x_label, 0, NULL, 0) == 0));
1043 not ^ (h->env->spam && h->env->spam->data
1044 && regexec (pat->rx, h->env->spam->data, 0, NULL,
1047 return (pat->not ^ (h->thread && h->thread->duplicate_thread));
1048 case M_UNREFERENCED:
1049 return (pat->not ^ (h->thread && !h->thread->child));
1052 * we have a match if
1053 * - From: matches $alternates
1054 * - or we have an alias for current address
1055 * - or From: contains valid email address _and_ name has >= 2 fields
1058 not ^ (h->env && h->env->from && (mutt_addr_is_user (h->env->from)
1060 (alias_reverse_lookup
1061 (h->env->from) != NULL)
1062 || (h->env->from->personal
1063 && valid_realname (h->env->
1066 && h->env->from->mailbox)
1071 not ^ (h->env->newsgroups
1072 && regexec (pat->rx, h->env->newsgroups, 0, NULL,
1076 mutt_error (_("error: unknown op %d (report this error)."), pat->op);
1080 static void quote_simple (char *tmp, size_t len, const char *p)
1085 while (*p && i < len - 3) {
1086 if (*p == '\\' || *p == '"')
1094 /* convert a simple search into a real request */
1095 void mutt_check_simple (char *s, size_t len, const char *simple)
1097 char tmp[LONG_STRING];
1099 /* XXX - is ascii_strcasecmp() right here, or should we use locale's
1103 if (!strchr (s, '~')) { /* yup, so spoof a real request */
1104 /* convert old tokens into the new format */
1105 if (ascii_strcasecmp ("all", s) == 0 || !str_cmp ("^", s) || !str_cmp (".", s)) /* ~A is more efficient */
1106 strfcpy (s, "~A", len);
1107 else if (ascii_strcasecmp ("del", s) == 0)
1108 strfcpy (s, "~D", len);
1109 else if (ascii_strcasecmp ("flag", s) == 0)
1110 strfcpy (s, "~F", len);
1111 else if (ascii_strcasecmp ("new", s) == 0)
1112 strfcpy (s, "~N", len);
1113 else if (ascii_strcasecmp ("old", s) == 0)
1114 strfcpy (s, "~O", len);
1115 else if (ascii_strcasecmp ("repl", s) == 0)
1116 strfcpy (s, "~Q", len);
1117 else if (ascii_strcasecmp ("read", s) == 0)
1118 strfcpy (s, "~R", len);
1119 else if (ascii_strcasecmp ("tag", s) == 0)
1120 strfcpy (s, "~T", len);
1121 else if (ascii_strcasecmp ("unread", s) == 0)
1122 strfcpy (s, "~U", len);
1124 quote_simple (tmp, sizeof (tmp), s);
1125 mutt_expand_fmt (s, len, simple, tmp);
1130 int mutt_pattern_func (int op, char *prompt)
1133 char buf[LONG_STRING] = "", *simple, error[STRING];
1137 strfcpy (buf, NONULL (Context->pattern), sizeof (buf));
1138 if (prompt || op != M_LIMIT)
1139 if (mutt_get_field (prompt, buf, sizeof (buf), M_PATTERN | M_CLEAR) != 0)
1143 strncpy (buf, "~A", sizeof (buf));
1148 mutt_message _("Compiling search pattern...");
1150 simple = str_dup (buf);
1151 mutt_check_simple (buf, sizeof (buf), NONULL (SimpleSearch));
1154 err.dsize = sizeof (error);
1155 if ((pat = mutt_pattern_comp (buf, M_FULL_MSG, &err)) == NULL) {
1157 mutt_error ("%s", err.data);
1161 mutt_message _("Executing command on matching messages...");
1163 #define THIS_BODY Context->hdrs[i]->content
1165 if (op == M_LIMIT) {
1166 Context->vcount = 0;
1168 Context->collapsed = 0;
1170 for (i = 0; i < Context->msgcount; i++) {
1171 /* new limit pattern implicitly uncollapses all threads */
1172 Context->hdrs[i]->virtual = -1;
1173 Context->hdrs[i]->limited = 0;
1174 Context->hdrs[i]->collapsed = 0;
1175 Context->hdrs[i]->num_hidden = 0;
1176 if (mutt_pattern_exec
1177 (pat, M_MATCH_FULL_ADDRESS, Context, Context->hdrs[i])) {
1178 Context->hdrs[i]->virtual = Context->vcount;
1179 Context->hdrs[i]->limited = 1;
1180 Context->v2r[Context->vcount] = i;
1182 Context->vsize += THIS_BODY->length + THIS_BODY->offset -
1183 THIS_BODY->hdr_offset;
1188 for (i = 0; i < Context->vcount; i++) {
1189 if (mutt_pattern_exec
1190 (pat, M_MATCH_FULL_ADDRESS, Context,
1191 Context->hdrs[Context->v2r[i]])) {
1194 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_PURGED,
1197 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_DELETE,
1202 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_TAG,
1212 mutt_clear_error ();
1214 if (op == M_LIMIT) {
1215 mem_free (&Context->pattern);
1216 if (Context->limit_pattern)
1217 mutt_pattern_free (&Context->limit_pattern);
1218 if (!Context->vcount) {
1219 mutt_error _("No messages matched criteria.");
1222 Context->vcount = Context->msgcount;
1223 /* restore full display */
1224 for (i = 0; i < Context->msgcount; i++) {
1225 Context->hdrs[i]->virtual = i;
1226 Context->v2r[i] = i;
1230 else if (str_ncmp (buf, "~A", 2) != 0) {
1231 Context->pattern = simple;
1232 simple = NULL; /* don't clobber it */
1233 Context->limit_pattern = mutt_pattern_comp (buf, M_FULL_MSG, &err);
1237 mutt_pattern_free (&pat);
1241 int mutt_search_command (int cur, int op)
1245 char temp[LONG_STRING];
1251 if (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE) {
1252 strfcpy (buf, LastSearch, sizeof (buf));
1253 if (mutt_get_field ((op == OP_SEARCH) ? _("Search for: ") :
1254 _("Reverse search for: "), buf, sizeof (buf),
1255 M_CLEAR | M_PATTERN) != 0 || !buf[0])
1258 if (op == OP_SEARCH)
1259 unset_option (OPTSEARCHREVERSE);
1261 set_option (OPTSEARCHREVERSE);
1263 /* compare the *expanded* version of the search pattern in case
1264 $simple_search has changed while we were searching */
1265 strfcpy (temp, buf, sizeof (temp));
1266 mutt_check_simple (temp, sizeof (temp), NONULL (SimpleSearch));
1268 if (!SearchPattern || str_cmp (temp, LastSearchExpn)) {
1269 set_option (OPTSEARCHINVALID);
1270 strfcpy (LastSearch, buf, sizeof (LastSearch));
1271 mutt_message _("Compiling search pattern...");
1273 mutt_pattern_free (&SearchPattern);
1275 err.dsize = sizeof (error);
1276 if ((SearchPattern =
1277 mutt_pattern_comp (temp, M_FULL_MSG, &err)) == NULL) {
1278 mutt_error ("%s", error);
1281 mutt_clear_error ();
1284 else if (!SearchPattern) {
1285 mutt_error _("No search pattern.");
1290 if (option (OPTSEARCHINVALID)) {
1291 for (i = 0; i < Context->msgcount; i++)
1292 Context->hdrs[i]->searched = 0;
1293 unset_option (OPTSEARCHINVALID);
1296 incr = (option (OPTSEARCHREVERSE)) ? -1 : 1;
1297 if (op == OP_SEARCH_OPPOSITE)
1300 for (i = cur + incr, j = 0; j != Context->vcount; j++) {
1301 if (i > Context->vcount - 1) {
1303 if (option (OPTWRAPSEARCH))
1304 mutt_message (_("Search wrapped to top."));
1307 mutt_message _("Search hit bottom without finding match");
1313 i = Context->vcount - 1;
1314 if (option (OPTWRAPSEARCH))
1315 mutt_message (_("Search wrapped to bottom."));
1318 mutt_message _("Search hit top without finding match");
1324 h = Context->hdrs[Context->v2r[i]];
1326 /* if we've already evaulated this message, use the cached value */
1331 /* remember that we've already searched this message */
1335 (SearchPattern, M_MATCH_FULL_ADDRESS, Context, h) > 0)))
1340 mutt_error _("Search interrupted.");
1349 mutt_error _("Not found.");