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.
21 #include <lib-lib/lib-lib.h>
23 #include <lib-mime/mime.h>
25 #include <lib-ui/enter.h>
26 #include <lib-ui/curses.h>
35 #include <imap/imap.h>
37 #include <lib-crypt/crypt.h>
39 static int eat_regexp (pattern_t * pat, BUFFER *, BUFFER *);
40 static int eat_date (pattern_t * pat, BUFFER *, BUFFER *);
41 static int eat_range (pattern_t * pat, BUFFER *, BUFFER *);
42 static int patmatch (const pattern_t* pat, const char* buf);
44 struct pattern_flags {
45 int tag; /* character used to represent this op */
46 int op; /* operation to perform */
48 int (*eat_arg)(pattern_t *, BUFFER *, BUFFER *);
50 {'A', M_ALL, 0, NULL},
51 {'b', M_BODY, M_FULL_MSG, eat_regexp},
52 {'B', M_WHOLE_MSG, M_FULL_MSG, eat_regexp},
53 {'c', M_CC, 0, eat_regexp},
54 {'C', M_RECIPIENT, 0, eat_regexp},
55 {'d', M_DATE, 0, eat_date},
56 {'D', M_DELETED, 0, NULL},
57 {'e', M_SENDER, 0, eat_regexp},
58 {'E', M_EXPIRED, 0, NULL},
59 {'f', M_FROM, 0, eat_regexp},
60 {'F', M_FLAG, 0, NULL},
61 {'g', M_CRYPT_SIGN, 0, NULL},
62 {'G', M_CRYPT_ENCRYPT, 0, NULL},
63 {'h', M_HEADER, M_FULL_MSG, eat_regexp},
64 {'H', M_HORMEL, 0, eat_regexp},
65 {'i', M_ID, 0, eat_regexp},
66 {'k', M_PGP_KEY, 0, NULL},
67 {'L', M_ADDRESS, 0, eat_regexp},
68 {'l', M_LIST, 0, NULL},
69 {'m', M_MESSAGE, 0, eat_range},
70 {'M', M_MULTIPART, 0, NULL},
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 {'u', M_SUBSCRIBED_LIST, 0, NULL},
85 {'v', M_COLLAPSED, 0, NULL},
86 {'V', M_CRYPT_VERIFIED, 0, NULL},
88 {'w', M_NEWSGROUPS, 0, eat_regexp},
90 {'x', M_REFERENCE, 0, eat_regexp},
91 {'X', M_MIMEATTACH, 0, eat_range},
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)
117 /* if no uppercase letters are given, do a case-insensitive search */
118 int mutt_which_case (const char *s)
121 if (isalpha ((unsigned char) *s) && isupper ((unsigned char) *s))
122 return 0; /* case-sensitive */
125 return REG_ICASE; /* case-insensitive */
129 msg_search (CONTEXT *ctx, pattern_t* pat, int msgno)
131 char tempfile[_POSIX_PATH_MAX];
138 HEADER *h = ctx->hdrs[msgno];
142 if ((msg = mx_open_message (ctx, msgno)) != NULL) {
143 if (option (OPTTHOROUGHSRC)) {
144 /* decode the header / body */
147 s.flags = M_CHARCONV;
148 mutt_mktemp (tempfile);
149 if ((s.fpout = safe_fopen (tempfile, "w+")) == NULL) {
150 mutt_perror (tempfile);
154 if (pat->op != M_BODY)
155 mutt_copy_header (msg->fp, h, s.fpout, CH_FROM | CH_DECODE, NULL);
157 if (pat->op != M_HEADER) {
158 mutt_parse_mime_message (ctx, h);
160 if ((h->security & ENCRYPT) && !crypt_valid_passphrase (h->security))
162 mx_close_message (&msg);
170 fseeko (msg->fp, h->offset, 0);
171 mutt_body_handler (h->content, &s);
177 fstat (fileno (fp), &st);
178 lng = (long) st.st_size;
181 /* raw header / body */
183 if (pat->op != M_BODY) {
184 fseeko (fp, h->offset, 0);
185 lng = h->content->offset - h->offset;
187 if (pat->op != M_HEADER) {
188 if (pat->op == M_BODY)
189 fseeko (fp, h->content->offset, 0);
190 lng += h->content->length;
194 buf = p_new(char, blen = STRING);
196 /* search the file "fp" */
198 if (pat->op == M_HEADER) {
199 if (!mutt_read_rfc822_line(fp, &buf, &blen))
201 } else if (fgets (buf, blen - 1, fp) == NULL)
202 break; /* don't loop forever */
203 if (patmatch (pat, buf) == 0) {
207 lng -= m_strlen(buf);
212 mx_close_message (&msg);
214 if (option (OPTTHOROUGHSRC)) {
223 int eat_regexp (pattern_t * pat, BUFFER * s, BUFFER * err)
230 if (mutt_extract_token (&buf, s, M_TOKEN_PATTERN | M_TOKEN_COMMENT) != 0 ||
232 snprintf (err->data, err->dsize, _("Error in expression: %s"), s->dptr);
237 snprintf (err->data, err->dsize, _("Empty expression"));
242 /* If there are no RE metacharacters, use simple search anyway */
243 if (!pat->stringmatch && !strpbrk (buf.data, "|[{.*+?^$"))
244 pat->stringmatch = 1;
247 if (pat->stringmatch) {
248 pat->str = m_strdup(buf.data);
251 pat->rx = p_new(regex_t, 1);
252 r = REGCOMP (pat->rx, buf.data, REG_NEWLINE | REG_NOSUB | mutt_which_case (buf.data));
255 regerror (r, pat->rx, err->data, err->dsize);
264 static int patmatch (const pattern_t* pat, const char* buf) {
265 if (pat->stringmatch)
266 return !strstr (buf, pat->str);
268 return regexec (pat->rx, buf, 0, NULL, 0);
271 int eat_range (pattern_t * pat, BUFFER * s, BUFFER * err __attribute__ ((unused)))
274 int do_exclusive = 0;
278 * If simple_search is set to "~m %s", the range will have double quotes
281 if (*s->dptr == '"') {
287 if ((*s->dptr != '-') && (*s->dptr != '<')) {
289 if (*s->dptr == '>') {
290 pat->max = M_MAXRANGE;
291 pat->min = strtol (s->dptr + 1, &tmp, 0) + 1; /* exclusive range */
294 pat->min = strtol (s->dptr, &tmp, 0);
295 if (toupper ((unsigned char) *tmp) == 'K') { /* is there a prefix? */
299 else if (toupper ((unsigned char) *tmp) == 'M') {
303 if (*s->dptr == '>') {
320 if (isdigit ((unsigned char) *tmp)) {
322 pat->max = strtol (tmp, &tmp, 0);
323 if (toupper ((unsigned char) *tmp) == 'K') {
327 else if (toupper ((unsigned char) *tmp) == 'M') {
335 pat->max = M_MAXRANGE;
337 if (skip_quote && *tmp == '"')
340 s->dptr = vskipspaces(tmp);
344 static const char *getDate (const char *s, struct tm *t, BUFFER * err)
347 time_t now = time (NULL);
348 struct tm *tm = localtime (&now);
350 t->tm_mday = strtol (s, &p, 10);
351 if (t->tm_mday < 1 || t->tm_mday > 31) {
352 snprintf (err->data, err->dsize, _("Invalid day of month: %s"), s);
356 /* fill in today's month and year */
357 t->tm_mon = tm->tm_mon;
358 t->tm_year = tm->tm_year;
362 t->tm_mon = strtol (p, &p, 10) - 1;
363 if (t->tm_mon < 0 || t->tm_mon > 11) {
364 snprintf (err->data, err->dsize, _("Invalid month: %s"), p);
368 t->tm_year = tm->tm_year;
372 t->tm_year = strtol (p, &p, 10);
373 if (t->tm_year < 70) /* year 2000+ */
375 else if (t->tm_year > 1900)
384 static const char *get_offset (struct tm *tm, const char *s, int sign)
387 int offset = strtol (s, &ps, 0);
389 if ((sign < 0 && offset > 0) || (sign > 0 && offset < 0))
394 tm->tm_year += offset;
397 tm->tm_mon += offset;
400 tm->tm_mday += 7 * offset;
403 tm->tm_mday += offset;
408 mutt_normalize_time (tm);
412 static void adjust_date_range (struct tm *min, struct tm *max)
414 if (min->tm_year > max->tm_year
415 || (min->tm_year == max->tm_year && min->tm_mon > max->tm_mon)
416 || (min->tm_year == max->tm_year && min->tm_mon == max->tm_mon
417 && min->tm_mday > max->tm_mday)) {
421 min->tm_year = max->tm_year;
425 min->tm_mon = max->tm_mon;
429 min->tm_mday = max->tm_mday;
432 min->tm_hour = min->tm_min = min->tm_sec = 0;
434 max->tm_min = max->tm_sec = 59;
438 static const char *parse_date_range (const char *pc, struct tm *min,
439 struct tm *max, int haveMin,
440 struct tm *baseMin, BUFFER * err)
442 int flag = M_PDR_NONE;
444 while (*pc && ((flag & M_PDR_DONE) == 0)) {
448 pc = vskipspaces(pc);
452 /* try a range of absolute date minus offset of Ndwmy */
453 pt = get_offset (min, pc, -1);
455 if (flag == M_PDR_NONE) { /* nothing yet and no offset parsed => absolute date? */
456 if (!getDate (pc, max, err))
457 flag |= (M_PDR_ABSOLUTE | M_PDR_ERRORDONE); /* done bad */
459 /* reestablish initial base minimum if not specified */
461 memcpy (min, baseMin, sizeof (struct tm));
462 flag |= (M_PDR_ABSOLUTE | M_PDR_DONE); /* done good */
466 flag |= M_PDR_ERRORDONE;
470 if (flag == M_PDR_NONE && !haveMin) { /* the very first "-3d" without a previous absolute date */
471 max->tm_year = min->tm_year;
472 max->tm_mon = min->tm_mon;
473 max->tm_mday = min->tm_mday;
480 { /* enlarge plusRange */
481 pt = get_offset (max, pc, 1);
483 flag |= M_PDR_ERRORDONE;
491 { /* enlarge window in both directions */
492 pt = get_offset (min, pc, -1);
494 flag |= M_PDR_ERRORDONE;
496 pc = get_offset (max, pc, 1);
497 flag |= M_PDR_WINDOW;
502 flag |= M_PDR_ERRORDONE;
504 pc = vskipspaces(pc);
506 if ((flag & M_PDR_ERROR) && !(flag & M_PDR_ABSOLUTE)) { /* getDate has its own error message, don't overwrite it here */
507 snprintf (err->data, err->dsize, _("Invalid relative date: %s"), pc - 1);
509 return ((flag & M_PDR_ERROR) ? NULL : pc);
512 static int eat_date (pattern_t * pat, BUFFER * s, BUFFER * err)
518 if (mutt_extract_token (&buffer, s, M_TOKEN_COMMENT | M_TOKEN_PATTERN) != 0
520 m_strcpy(err->data, err->dsize, _("error in expression"));
525 /* the `0' time is Jan 1, 1970 UTC, so in order to prevent a negative time
526 when doing timezone conversion, we use Jan 2, 1970 UTC as the base
533 /* Arbitrary year in the future. Don't set this too high
534 or mutt_mktime() returns something larger than will
535 fit in a time_t on some systems */
543 if (strchr ("<>=", buffer.data[0])) {
544 /* offset from current time
545 <3d less than three days ago
546 >3d more than three days ago
547 =3d exactly three days ago */
548 time_t now = time (NULL);
549 struct tm *tm = localtime (&now);
552 if (buffer.data[0] == '<') {
553 memcpy (&min, tm, sizeof (min));
557 memcpy (&max, tm, sizeof (max));
560 if (buffer.data[0] == '=')
564 tm->tm_min = tm->tm_sec = 59;
566 /* force negative offset */
567 get_offset (tm, buffer.data + 1, -1);
570 /* start at the beginning of the day in question */
571 memcpy (&min, &max, sizeof (max));
572 min.tm_hour = min.tm_sec = min.tm_min = 0;
576 const char *pc = buffer.data;
579 int untilNow = FALSE;
581 if (isdigit ((unsigned char) *pc)) {
582 /* mininum date specified */
583 if ((pc = getDate (pc, &min, err)) == NULL) {
584 p_delete(&buffer.data);
588 pc = vskipspaces(pc);
592 pt = skipspaces(pc + 1);
593 untilNow = (*pt == '\0');
597 if (!untilNow) { /* max date or relative range/window */
601 if (!haveMin) { /* save base minimum and set current date, e.g. for "-3d+1d" */
602 time_t now = time (NULL);
603 struct tm *tm = localtime (&now);
605 memcpy (&baseMin, &min, sizeof (baseMin));
606 memcpy (&min, tm, sizeof (min));
607 min.tm_hour = min.tm_sec = min.tm_min = 0;
610 /* preset max date for relative offsets,
611 if nothing follows we search for messages on a specific day */
612 max.tm_year = min.tm_year;
613 max.tm_mon = min.tm_mon;
614 max.tm_mday = min.tm_mday;
616 if (!parse_date_range (pc, &min, &max, haveMin, &baseMin, err)) { /* bail out on any parsing error */
617 p_delete(&buffer.data);
623 /* Since we allow two dates to be specified we'll have to adjust that. */
624 adjust_date_range (&min, &max);
626 pat->min = mutt_mktime (&min, 1);
627 pat->max = mutt_mktime (&max, 1);
629 p_delete(&buffer.data);
634 static struct pattern_flags *lookup_tag (char tag)
638 for (i = 0; Flags[i].tag; i++)
639 if (Flags[i].tag == tag)
644 static /* const */ char *find_matching_paren ( /* const */ char *s)
651 else if (*s == ')') {
660 void mutt_pattern_free (pattern_t ** pat)
674 mutt_pattern_free (&tmp->child);
679 pattern_t *mutt_pattern_comp ( /* const */ char *s, int flags, BUFFER * err)
681 pattern_t *curlist = NULL;
683 pattern_t *last = NULL;
687 int implicit = 1; /* used to detect logical AND operator */
688 struct pattern_flags *entry;
695 ps.dsize = m_strlen(s);
698 ps.dptr = vskipspaces(ps.dptr);
711 snprintf (err->data, err->dsize, _("error in pattern at: %s"),
716 /* A & B | C == (A & B) | C */
717 tmp = new_pattern ();
719 tmp->child = curlist;
735 if (implicit && or) {
736 /* A | B & C == (A | B) & C */
737 tmp = new_pattern ();
739 tmp->child = curlist;
745 tmp = new_pattern ();
747 tmp->alladdr = alladdr;
748 tmp->stringmatch = (*ps.dptr == '=') ? 1 : 0;
758 ps.dptr++; /* move past the ~ */
759 if ((entry = lookup_tag (*ps.dptr)) == NULL) {
760 snprintf (err->data, err->dsize, _("%c: invalid command"), *ps.dptr);
761 mutt_pattern_free (&curlist);
764 if (entry->class && (flags & entry->class) == 0) {
765 snprintf (err->data, err->dsize, _("%c: not supported in this mode"),
767 mutt_pattern_free (&curlist);
772 ps.dptr = vskipspaces(ps.dptr + 1);
774 if (entry->eat_arg) {
776 snprintf (err->data, err->dsize, _("missing parameter"));
777 mutt_pattern_free (&curlist);
780 if (entry->eat_arg (tmp, &ps, err) == -1) {
781 mutt_pattern_free (&curlist);
788 p = find_matching_paren (ps.dptr + 1);
790 snprintf (err->data, err->dsize, _("mismatched parenthesis: %s"),
792 mutt_pattern_free (&curlist);
795 /* compile the sub-expression */
796 buf = p_dupstr(ps.dptr + 1, p - ps.dptr - 1);
797 if ((tmp = mutt_pattern_comp (buf, flags, err)) == NULL) {
799 mutt_pattern_free (&curlist);
809 tmp->alladdr |= alladdr;
812 ps.dptr = p + 1; /* restore location */
815 snprintf (err->data, err->dsize, _("error in pattern at: %s"), ps.dptr);
816 mutt_pattern_free (&curlist);
821 m_strcpy(err->data, err->dsize, _("empty pattern"));
825 tmp = new_pattern ();
826 tmp->op = or ? M_OR : M_AND;
827 tmp->child = curlist;
834 perform_and (pattern_t * pat, pattern_exec_flag flags, CONTEXT * ctx,
837 for (; pat; pat = pat->next)
838 if (mutt_pattern_exec (pat, flags, ctx, hdr) <= 0)
844 perform_or (struct pattern_t *pat, pattern_exec_flag flags, CONTEXT * ctx,
847 for (; pat; pat = pat->next)
848 if (mutt_pattern_exec (pat, flags, ctx, hdr) > 0)
853 static int match_adrlist (pattern_t* pat, int match_personal, int alladdr __attribute__ ((unused)),
861 for (a = va_arg (ap, address_t *); a; a = a->next) {
863 ((a->mailbox && patmatch (pat, a->mailbox) == 0) ||
864 (match_personal && a->personal &&
865 patmatch (pat, a->personal) == 0))) {
867 return (!pat->alladdr); /* Found match, or non-match if alladdr */
872 return pat->alladdr; /* No matches, or all matches if alladdr */
875 static int match_reference (pattern_t* pat, string_list_t * refs)
877 for (; refs; refs = refs->next)
878 if (patmatch (pat, refs->data) == 0)
883 int mutt_is_list_recipient (int alladdr, address_t * a1, address_t * a2)
885 for (; a1; a1 = a1->next)
886 if (alladdr ^ mutt_is_subscribed_list (a1))
888 for (; a2; a2 = a2->next)
889 if (alladdr ^ mutt_is_subscribed_list (a2))
894 int mutt_is_list_cc (int alladdr, address_t * a1, address_t * a2)
896 for (; a1; a1 = a1->next)
897 if (alladdr ^ mutt_is_mail_list (a1))
899 for (; a2; a2 = a2->next)
900 if (alladdr ^ mutt_is_mail_list (a2))
905 static int match_user (int alladdr, address_t * a1, address_t * a2)
907 for (; a1; a1 = a1->next)
908 if (alladdr ^ mutt_addr_is_user (a1))
910 for (; a2; a2 = a2->next)
911 if (alladdr ^ mutt_addr_is_user (a2))
916 /* test if name is considered a real name, i.e. consists of at least 2
917 * space-separated words of which none may end in a dot
919 static int valid_realname (const char *name)
921 const char *p = name;
928 /* skip abbr. parts of names (e.g. 'J. User') */
936 M_MATCH_FULL_ADDRESS match both personal and machine address */
938 mutt_pattern_exec (struct pattern_t *pat, pattern_exec_flag flags,
939 CONTEXT * ctx, HEADER * h)
943 return (pat->not ^ (perform_and (pat->child, flags, ctx, h) > 0));
945 return (pat->not ^ (perform_or (pat->child, flags, ctx, h) > 0));
949 return (pat->not ^ h->expired);
951 return (pat->not ^ h->superseded);
953 return (pat->not ^ h->flagged);
955 return (pat->not ^ h->tagged);
957 return (pat->not ? h->old || h->read : !(h->old || h->read));
959 return (pat->not ? h->read : !h->read);
961 return (pat->not ^ h->replied);
963 return (pat->not ? (!h->old || h->read) : (h->old && !h->read));
965 return (pat->not ^ h->read);
967 return (pat->not ^ h->deleted);
969 return (pat->not ^ (h->msgno >= pat->min - 1 && (pat->max == M_MAXRANGE ||
974 not ^ (h->date_sent >= pat->min && h->date_sent <= pat->max));
975 case M_DATE_RECEIVED:
976 return (pat->not ^ (h->received >= pat->min && h->received <= pat->max));
980 /* IMAP search sets h->matched at search compile time */
981 if (ctx->magic == M_IMAP && pat->stringmatch)
983 return (pat->not ^ msg_search (ctx, pat, h->msgno));
985 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
986 pat->alladdr, 1, h->env->sender));
988 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
989 pat->alladdr, 1, h->env->from));
991 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
992 pat->alladdr, 1, h->env->to));
994 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
995 pat->alladdr, 1, h->env->cc));
998 not ^ (h->env && h->env->subject
999 && patmatch (pat, h->env->subject) == 0));
1002 not ^ (h->env && h->env->message_id
1003 && patmatch (pat, h->env->message_id) == 0));
1005 return (pat->not ^ (h->score >= pat->min && (pat->max == M_MAXRANGE ||
1006 h->score <= pat->max)));
1009 not ^ (h->content->length >= pat->min
1010 && (pat->max == M_MAXRANGE
1011 || h->content->length <= pat->max)));
1013 return (pat->not ^ match_reference (pat, h->env->references));
1017 && match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
1018 pat->alladdr, 4, h->env->from,
1019 h->env->sender, h->env->to,
1024 && match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
1025 pat->alladdr, 2, h->env->to,
1030 && mutt_is_list_cc (pat->alladdr, h->env->to,
1032 case M_SUBSCRIBED_LIST:
1035 && mutt_is_list_recipient (pat->alladdr, h->env->to,
1037 case M_PERSONAL_RECIP:
1040 && match_user (pat->alladdr, h->env->to, h->env->cc)));
1041 case M_PERSONAL_FROM:
1043 not ^ (h->env && match_user (pat->alladdr, h->env->from, NULL)));
1045 return (pat->not ^ (h->collapsed && h->num_hidden > 1));
1047 return (pat->not ^ ((h->security & SIGN) ? 1 : 0));
1048 case M_CRYPT_VERIFIED:
1049 return (pat->not ^ ((h->security & GOODSIGN) ? 1 : 0));
1050 case M_CRYPT_ENCRYPT:
1051 return (pat->not ^ ((h->security & ENCRYPT) ? 1 : 0));
1053 return (pat->not ^ ((h->security & APPLICATION_PGP)
1054 && (h->security & PGPKEY)));
1057 not ^ (h->env->x_label
1058 && patmatch (pat, h->env->x_label) == 0));
1061 not ^ (h->env->spam && h->env->spam->data
1062 && patmatch (pat, h->env->spam->data) == 0));
1064 return (pat->not ^ (h->thread && h->thread->duplicate_thread));
1070 if (h->content->parts)
1071 count = mutt_count_body_parts(h, 0);
1073 mutt_parse_mime_message(ctx, h);
1074 count = mutt_count_body_parts(h, 0);
1075 body_list_wipe(&h->content->parts);
1078 return (pat->not ^ (count >= pat->min && (pat->max == M_MAXRANGE ||
1079 count <= pat->max)));
1082 case M_UNREFERENCED:
1083 return (pat->not ^ (h->thread && !h->thread->child));
1085 return (pat->not ^ (h->content && h->content->type == TYPEMULTIPART));
1088 * we have a match if
1089 * - From: matches $alternates
1090 * - or we have an alias for current address
1091 * - or From: contains valid email address _and_ name has >= 2 fields
1093 return (h->env && h->env->from
1094 && (mutt_addr_is_user(h->env->from)
1095 || alias_reverse_lookup(h->env->from)
1096 || (h->env->from->personal
1097 && valid_realname(h->env-> from->personal)
1098 && h->env->from->mailbox)
1103 not ^ (h->env->newsgroups
1104 && patmatch (pat, h->env->newsgroups) == 0));
1107 mutt_error (_("error: unknown op %d (report this error)."), pat->op);
1111 static void quote_simple (char *tmp, ssize_t len, const char *p)
1116 while (*p && i < len - 3) {
1117 if (*p == '\\' || *p == '"')
1125 /* convert a simple search into a real request */
1126 void mutt_check_simple (char *s, ssize_t len, const char *simple)
1128 char tmp[LONG_STRING];
1130 /* XXX - is ascii_strcasecmp() right here, or should we use locale's
1134 if (!strchr (s, '~') && !strchr (s, '=')) { /* yup, so spoof a real request */
1135 /* convert old tokens into the new format */
1136 if (ascii_strcasecmp ("all", s) == 0 || !m_strcmp("^", s) || !m_strcmp(".", s)) /* ~A is more efficient */
1137 m_strcpy(s, len, "~A");
1138 else if (ascii_strcasecmp ("del", s) == 0)
1139 m_strcpy(s, len, "~D");
1140 else if (ascii_strcasecmp ("flag", s) == 0)
1141 m_strcpy(s, len, "~F");
1142 else if (ascii_strcasecmp ("new", s) == 0)
1143 m_strcpy(s, len, "~N");
1144 else if (ascii_strcasecmp ("old", s) == 0)
1145 m_strcpy(s, len, "~O");
1146 else if (ascii_strcasecmp ("repl", s) == 0)
1147 m_strcpy(s, len, "~Q");
1148 else if (ascii_strcasecmp ("read", s) == 0)
1149 m_strcpy(s, len, "~R");
1150 else if (ascii_strcasecmp ("tag", s) == 0)
1151 m_strcpy(s, len, "~T");
1152 else if (ascii_strcasecmp ("unread", s) == 0)
1153 m_strcpy(s, len, "~U");
1155 quote_simple (tmp, sizeof (tmp), s);
1156 mutt_expand_fmt (s, len, simple, tmp);
1161 int mutt_pattern_func (int op, char *prompt)
1164 char buf[LONG_STRING] = "", *simple, error[STRING];
1168 m_strcpy(buf, sizeof(buf), NONULL(Context->pattern));
1169 if (prompt || op != M_LIMIT)
1170 if (mutt_get_field (prompt, buf, sizeof (buf), M_PATTERN | M_CLEAR) != 0 || !buf[0])
1173 mutt_message _("Compiling search pattern...");
1175 simple = m_strdup(buf);
1176 mutt_check_simple (buf, sizeof (buf), NONULL (SimpleSearch));
1179 err.dsize = sizeof (error);
1180 if ((pat = mutt_pattern_comp (buf, M_FULL_MSG, &err)) == NULL) {
1182 mutt_error ("%s", err.data);
1186 if (Context->magic == M_IMAP && imap_search (Context, pat) < 0)
1189 mutt_message _("Executing command on matching messages...");
1191 #define THIS_BODY Context->hdrs[i]->content
1193 if (op == M_LIMIT) {
1194 Context->vcount = 0;
1196 Context->collapsed = 0;
1198 for (i = 0; i < Context->msgcount; i++) {
1199 /* new limit pattern implicitly uncollapses all threads */
1200 Context->hdrs[i]->virtual = -1;
1201 Context->hdrs[i]->limited = 0;
1202 Context->hdrs[i]->collapsed = 0;
1203 Context->hdrs[i]->num_hidden = 0;
1204 if (mutt_pattern_exec
1205 (pat, M_MATCH_FULL_ADDRESS, Context, Context->hdrs[i])) {
1206 Context->hdrs[i]->virtual = Context->vcount;
1207 Context->hdrs[i]->limited = 1;
1208 Context->v2r[Context->vcount] = i;
1210 Context->vsize += THIS_BODY->length + THIS_BODY->offset -
1211 THIS_BODY->hdr_offset;
1216 for (i = 0; i < Context->vcount; i++) {
1217 if (mutt_pattern_exec
1218 (pat, M_MATCH_FULL_ADDRESS, Context,
1219 Context->hdrs[Context->v2r[i]])) {
1222 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_PURGED,
1225 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_DELETE,
1230 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_TAG,
1240 mutt_clear_error ();
1242 if (op == M_LIMIT) {
1243 /* drop previous limit pattern */
1244 p_delete(&Context->pattern);
1245 if (Context->limit_pattern)
1246 mutt_pattern_free (&Context->limit_pattern);
1247 if (Context->msgcount && !Context->vcount) {
1248 mutt_error _("No messages matched criteria.");
1251 /* record new limit pattern, unless match all */
1252 if (m_strncmp(buf, "~A", 2) != 0) {
1253 Context->pattern = simple;
1254 simple = NULL; /* don't clobber it */
1255 Context->limit_pattern = mutt_pattern_comp (buf, M_FULL_MSG, &err);
1259 mutt_pattern_free (&pat);
1263 int mutt_search_command (int cur, int op)
1267 char temp[LONG_STRING];
1273 if (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE) {
1274 m_strcpy(buf, sizeof(buf), LastSearch);
1275 if (mutt_get_field ((op == OP_SEARCH) ? _("Search for: ") :
1276 _("Reverse search for: "), buf, sizeof (buf),
1277 M_CLEAR | M_PATTERN) != 0 || !buf[0])
1280 if (op == OP_SEARCH)
1281 unset_option (OPTSEARCHREVERSE);
1283 set_option (OPTSEARCHREVERSE);
1285 /* compare the *expanded* version of the search pattern in case
1286 $simple_search has changed while we were searching */
1287 m_strcpy(temp, sizeof(temp), buf);
1288 mutt_check_simple (temp, sizeof (temp), NONULL (SimpleSearch));
1290 if (!SearchPattern || m_strcmp(temp, LastSearchExpn)) {
1291 set_option (OPTSEARCHINVALID);
1292 m_strcpy(LastSearch, sizeof(LastSearch), buf);
1293 mutt_message _("Compiling search pattern...");
1295 mutt_pattern_free (&SearchPattern);
1297 err.dsize = sizeof (error);
1298 if ((SearchPattern =
1299 mutt_pattern_comp (temp, M_FULL_MSG, &err)) == NULL) {
1300 mutt_error ("%s", error);
1303 mutt_clear_error ();
1306 else if (!SearchPattern) {
1307 mutt_error _("No search pattern.");
1312 if (option (OPTSEARCHINVALID)) {
1313 for (i = 0; i < Context->msgcount; i++)
1314 Context->hdrs[i]->searched = 0;
1315 if (Context->magic == M_IMAP && imap_search (Context, SearchPattern) < 0)
1317 unset_option (OPTSEARCHINVALID);
1320 incr = (option (OPTSEARCHREVERSE)) ? -1 : 1;
1321 if (op == OP_SEARCH_OPPOSITE)
1324 for (i = cur + incr, j = 0; j != Context->vcount; j++) {
1325 if (i > Context->vcount - 1) {
1327 if (option (OPTWRAPSEARCH))
1328 mutt_message (_("Search wrapped to top."));
1331 mutt_message _("Search hit bottom without finding match");
1337 i = Context->vcount - 1;
1338 if (option (OPTWRAPSEARCH))
1339 mutt_message (_("Search wrapped to bottom."));
1342 mutt_message _("Search hit top without finding match");
1348 h = Context->hdrs[Context->v2r[i]];
1350 /* if we've already evaulated this message, use the cached value */
1355 /* remember that we've already searched this message */
1359 (SearchPattern, M_MATCH_FULL_ADDRESS, Context, h) > 0)))
1364 mutt_error _("Search interrupted.");
1373 mutt_error _("Not found.");