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.
14 #include <lib-lib/mem.h>
15 #include <lib-lib/ascii.h>
16 #include <lib-lib/str.h>
17 #include <lib-lib/macros.h>
18 #include <lib-lib/file.h>
19 #include <lib-lib/buffer.h>
20 #include <lib-lib/mapping.h>
22 #include <lib-mime/mime.h>
34 #include "imap/imap.h"
44 #include "mutt_crypt.h"
46 static int eat_regexp (pattern_t * pat, BUFFER *, BUFFER *);
47 static int eat_date (pattern_t * pat, BUFFER *, BUFFER *);
48 static int eat_range (pattern_t * pat, BUFFER *, BUFFER *);
49 static int patmatch (const pattern_t* pat, const char* buf);
51 struct pattern_flags {
52 int tag; /* character used to represent this op */
53 int op; /* operation to perform */
55 int (*eat_arg) (pattern_t *, BUFFER *, BUFFER *);
58 'A', M_ALL, 0, NULL}, {
59 'b', M_BODY, M_FULL_MSG, eat_regexp}, {
60 'B', M_WHOLE_MSG, M_FULL_MSG, eat_regexp}, {
61 'c', M_CC, 0, eat_regexp}, {
62 'C', M_RECIPIENT, 0, eat_regexp}, {
63 'd', M_DATE, 0, eat_date}, {
64 'D', M_DELETED, 0, NULL}, {
65 'e', M_SENDER, 0, eat_regexp}, {
66 'E', M_EXPIRED, 0, NULL}, {
67 'f', M_FROM, 0, eat_regexp}, {
68 'F', M_FLAG, 0, NULL}, {
69 'g', M_CRYPT_SIGN, 0, NULL}, {
70 'G', M_CRYPT_ENCRYPT, 0, NULL}, {
71 'h', M_HEADER, M_FULL_MSG, eat_regexp}, {
72 'H', M_HORMEL, 0, eat_regexp}, {
73 'i', M_ID, 0, eat_regexp}, {
74 'k', M_PGP_KEY, 0, NULL}, {
75 'L', M_ADDRESS, 0, eat_regexp}, {
76 'l', M_LIST, 0, NULL}, {
77 'm', M_MESSAGE, 0, eat_range}, {
78 'M', M_MULTIPART, 0, NULL}, {
79 'n', M_SCORE, 0, eat_range}, {
80 'N', M_NEW, 0, NULL}, {
81 'O', M_OLD, 0, NULL}, {
82 'p', M_PERSONAL_RECIP, 0, NULL}, {
83 'P', M_PERSONAL_FROM, 0, NULL}, {
84 'Q', M_REPLIED, 0, NULL}, {
85 'R', M_READ, 0, NULL}, {
86 'r', M_DATE_RECEIVED, 0, eat_date}, {
87 's', M_SUBJECT, 0, eat_regexp}, {
88 'S', M_SUPERSEDED, 0, NULL}, {
89 'T', M_TAG, 0, NULL}, {
90 't', M_TO, 0, eat_regexp}, {
91 'U', M_UNREAD, 0, NULL}, {
92 'u', M_SUBSCRIBED_LIST, 0, NULL}, {
93 'v', M_COLLAPSED, 0, NULL}, {
94 'V', M_CRYPT_VERIFIED, 0, NULL},
97 'w', M_NEWSGROUPS, 0, eat_regexp},
100 'x', M_REFERENCE, 0, eat_regexp}, {
101 'X', M_MIMEATTACH, 0, eat_range}, {
102 'y', M_XLABEL, 0, eat_regexp}, {
103 'z', M_SIZE, 0, eat_range}, {
104 '=', M_DUPLICATED, 0, NULL}, {
105 '$', M_UNREFERENCED, 0, NULL}, {
106 '*', M_REALNAME, 0, NULL}, {
110 static pattern_t *SearchPattern = NULL; /* current search pattern */
111 static char LastSearch[STRING] = { 0 }; /* last pattern searched for */
112 static char LastSearchExpn[LONG_STRING] = { 0 }; /* expanded version of
115 #define M_MAXRANGE -1
117 /* constants for parse_date_range() */
118 #define M_PDR_NONE 0x0000
119 #define M_PDR_MINUS 0x0001
120 #define M_PDR_PLUS 0x0002
121 #define M_PDR_WINDOW 0x0004
122 #define M_PDR_ABSOLUTE 0x0008
123 #define M_PDR_DONE 0x0010
124 #define M_PDR_ERROR 0x0100
125 #define M_PDR_ERRORDONE (M_PDR_ERROR | M_PDR_DONE)
128 int mutt_getvaluebychar (char ch, struct mapping_t *table)
132 for (i = 0; table[i].name; i++) {
133 if (ch == table[i].name[0])
134 return table[i].value;
140 /* if no uppercase letters are given, do a case-insensitive search */
141 int mutt_which_case (const char *s)
144 if (isalpha ((unsigned char) *s) && isupper ((unsigned char) *s))
145 return 0; /* case-sensitive */
148 return REG_ICASE; /* case-insensitive */
152 msg_search (CONTEXT *ctx, pattern_t* pat, int msgno)
154 char tempfile[_POSIX_PATH_MAX];
161 HEADER *h = ctx->hdrs[msgno];
165 if ((msg = mx_open_message (ctx, msgno)) != NULL) {
166 if (option (OPTTHOROUGHSRC)) {
167 /* decode the header / body */
170 s.flags = M_CHARCONV;
171 mutt_mktemp (tempfile);
172 if ((s.fpout = safe_fopen (tempfile, "w+")) == NULL) {
173 mutt_perror (tempfile);
177 if (pat->op != M_BODY)
178 mutt_copy_header (msg->fp, h, s.fpout, CH_FROM | CH_DECODE, NULL);
180 if (pat->op != M_HEADER) {
181 mutt_parse_mime_message (ctx, h);
183 if ((h->security & ENCRYPT) && !crypt_valid_passphrase (h->security))
185 mx_close_message (&msg);
193 fseeko (msg->fp, h->offset, 0);
194 mutt_body_handler (h->content, &s);
200 fstat (fileno (fp), &st);
201 lng = (long) st.st_size;
204 /* raw header / body */
206 if (pat->op != M_BODY) {
207 fseeko (fp, h->offset, 0);
208 lng = h->content->offset - h->offset;
210 if (pat->op != M_HEADER) {
211 if (pat->op == M_BODY)
212 fseeko (fp, h->content->offset, 0);
213 lng += h->content->length;
217 buf = p_new(char, blen = STRING);
219 /* search the file "fp" */
221 if (pat->op == M_HEADER) {
222 if (*(buf = mutt_read_rfc822_line (fp, buf, &blen)) == '\0')
224 } else if (fgets (buf, blen - 1, fp) == NULL)
225 break; /* don't loop forever */
226 if (patmatch (pat, buf) == 0) {
230 lng -= m_strlen(buf);
235 mx_close_message (&msg);
237 if (option (OPTTHOROUGHSRC)) {
246 int eat_regexp (pattern_t * pat, BUFFER * s, BUFFER * err)
253 if (mutt_extract_token (&buf, s, M_TOKEN_PATTERN | M_TOKEN_COMMENT) != 0 ||
255 snprintf (err->data, err->dsize, _("Error in expression: %s"), s->dptr);
260 snprintf (err->data, err->dsize, _("Empty expression"));
265 /* If there are no RE metacharacters, use simple search anyway */
266 if (!pat->stringmatch && !strpbrk (buf.data, "|[{.*+?^$"))
267 pat->stringmatch = 1;
270 if (pat->stringmatch) {
271 pat->str = m_strdup(buf.data);
274 pat->rx = p_new(regex_t, 1);
275 r = REGCOMP (pat->rx, buf.data, REG_NEWLINE | REG_NOSUB | mutt_which_case (buf.data));
278 regerror (r, pat->rx, err->data, err->dsize);
287 static int patmatch (const pattern_t* pat, const char* buf) {
288 if (pat->stringmatch)
289 return !strstr (buf, pat->str);
291 return regexec (pat->rx, buf, 0, NULL, 0);
294 int eat_range (pattern_t * pat, BUFFER * s, BUFFER * err)
297 int do_exclusive = 0;
301 * If simple_search is set to "~m %s", the range will have double quotes
304 if (*s->dptr == '"') {
310 if ((*s->dptr != '-') && (*s->dptr != '<')) {
312 if (*s->dptr == '>') {
313 pat->max = M_MAXRANGE;
314 pat->min = strtol (s->dptr + 1, &tmp, 0) + 1; /* exclusive range */
317 pat->min = strtol (s->dptr, &tmp, 0);
318 if (toupper ((unsigned char) *tmp) == 'K') { /* is there a prefix? */
322 else if (toupper ((unsigned char) *tmp) == 'M') {
326 if (*s->dptr == '>') {
343 if (isdigit ((unsigned char) *tmp)) {
345 pat->max = strtol (tmp, &tmp, 0);
346 if (toupper ((unsigned char) *tmp) == 'K') {
350 else if (toupper ((unsigned char) *tmp) == 'M') {
358 pat->max = M_MAXRANGE;
360 if (skip_quote && *tmp == '"')
363 s->dptr = vskipspaces(tmp);
367 static const char *getDate (const char *s, struct tm *t, BUFFER * err)
370 time_t now = time (NULL);
371 struct tm *tm = localtime (&now);
373 t->tm_mday = strtol (s, &p, 10);
374 if (t->tm_mday < 1 || t->tm_mday > 31) {
375 snprintf (err->data, err->dsize, _("Invalid day of month: %s"), s);
379 /* fill in today's month and year */
380 t->tm_mon = tm->tm_mon;
381 t->tm_year = tm->tm_year;
385 t->tm_mon = strtol (p, &p, 10) - 1;
386 if (t->tm_mon < 0 || t->tm_mon > 11) {
387 snprintf (err->data, err->dsize, _("Invalid month: %s"), p);
391 t->tm_year = tm->tm_year;
395 t->tm_year = strtol (p, &p, 10);
396 if (t->tm_year < 70) /* year 2000+ */
398 else if (t->tm_year > 1900)
407 static const char *get_offset (struct tm *tm, const char *s, int sign)
410 int offset = strtol (s, &ps, 0);
412 if ((sign < 0 && offset > 0) || (sign > 0 && offset < 0))
417 tm->tm_year += offset;
420 tm->tm_mon += offset;
423 tm->tm_mday += 7 * offset;
426 tm->tm_mday += offset;
431 mutt_normalize_time (tm);
435 static void adjust_date_range (struct tm *min, struct tm *max)
437 if (min->tm_year > max->tm_year
438 || (min->tm_year == max->tm_year && min->tm_mon > max->tm_mon)
439 || (min->tm_year == max->tm_year && min->tm_mon == max->tm_mon
440 && min->tm_mday > max->tm_mday)) {
444 min->tm_year = max->tm_year;
448 min->tm_mon = max->tm_mon;
452 min->tm_mday = max->tm_mday;
455 min->tm_hour = min->tm_min = min->tm_sec = 0;
457 max->tm_min = max->tm_sec = 59;
461 static const char *parse_date_range (const char *pc, struct tm *min,
462 struct tm *max, int haveMin,
463 struct tm *baseMin, BUFFER * err)
465 int flag = M_PDR_NONE;
467 while (*pc && ((flag & M_PDR_DONE) == 0)) {
471 pc = vskipspaces(pc);
475 /* try a range of absolute date minus offset of Ndwmy */
476 pt = get_offset (min, pc, -1);
478 if (flag == M_PDR_NONE) { /* nothing yet and no offset parsed => absolute date? */
479 if (!getDate (pc, max, err))
480 flag |= (M_PDR_ABSOLUTE | M_PDR_ERRORDONE); /* done bad */
482 /* reestablish initial base minimum if not specified */
484 memcpy (min, baseMin, sizeof (struct tm));
485 flag |= (M_PDR_ABSOLUTE | M_PDR_DONE); /* done good */
489 flag |= M_PDR_ERRORDONE;
493 if (flag == M_PDR_NONE && !haveMin) { /* the very first "-3d" without a previous absolute date */
494 max->tm_year = min->tm_year;
495 max->tm_mon = min->tm_mon;
496 max->tm_mday = min->tm_mday;
503 { /* enlarge plusRange */
504 pt = get_offset (max, pc, 1);
506 flag |= M_PDR_ERRORDONE;
514 { /* enlarge window in both directions */
515 pt = get_offset (min, pc, -1);
517 flag |= M_PDR_ERRORDONE;
519 pc = get_offset (max, pc, 1);
520 flag |= M_PDR_WINDOW;
525 flag |= M_PDR_ERRORDONE;
527 pc = vskipspaces(pc);
529 if ((flag & M_PDR_ERROR) && !(flag & M_PDR_ABSOLUTE)) { /* getDate has its own error message, don't overwrite it here */
530 snprintf (err->data, err->dsize, _("Invalid relative date: %s"), pc - 1);
532 return ((flag & M_PDR_ERROR) ? NULL : pc);
535 static int eat_date (pattern_t * pat, BUFFER * s, BUFFER * err)
541 if (mutt_extract_token (&buffer, s, M_TOKEN_COMMENT | M_TOKEN_PATTERN) != 0
543 m_strcpy(err->data, err->dsize, _("error in expression"));
548 /* the `0' time is Jan 1, 1970 UTC, so in order to prevent a negative time
549 when doing timezone conversion, we use Jan 2, 1970 UTC as the base
556 /* Arbitrary year in the future. Don't set this too high
557 or mutt_mktime() returns something larger than will
558 fit in a time_t on some systems */
566 if (strchr ("<>=", buffer.data[0])) {
567 /* offset from current time
568 <3d less than three days ago
569 >3d more than three days ago
570 =3d exactly three days ago */
571 time_t now = time (NULL);
572 struct tm *tm = localtime (&now);
575 if (buffer.data[0] == '<') {
576 memcpy (&min, tm, sizeof (min));
580 memcpy (&max, tm, sizeof (max));
583 if (buffer.data[0] == '=')
587 tm->tm_min = tm->tm_sec = 59;
589 /* force negative offset */
590 get_offset (tm, buffer.data + 1, -1);
593 /* start at the beginning of the day in question */
594 memcpy (&min, &max, sizeof (max));
595 min.tm_hour = min.tm_sec = min.tm_min = 0;
599 const char *pc = buffer.data;
602 int untilNow = FALSE;
604 if (isdigit ((unsigned char) *pc)) {
605 /* mininum date specified */
606 if ((pc = getDate (pc, &min, err)) == NULL) {
607 p_delete(&buffer.data);
611 pc = vskipspaces(pc);
615 pt = skipspaces(pc + 1);
616 untilNow = (*pt == '\0');
620 if (!untilNow) { /* max date or relative range/window */
624 if (!haveMin) { /* save base minimum and set current date, e.g. for "-3d+1d" */
625 time_t now = time (NULL);
626 struct tm *tm = localtime (&now);
628 memcpy (&baseMin, &min, sizeof (baseMin));
629 memcpy (&min, tm, sizeof (min));
630 min.tm_hour = min.tm_sec = min.tm_min = 0;
633 /* preset max date for relative offsets,
634 if nothing follows we search for messages on a specific day */
635 max.tm_year = min.tm_year;
636 max.tm_mon = min.tm_mon;
637 max.tm_mday = min.tm_mday;
639 if (!parse_date_range (pc, &min, &max, haveMin, &baseMin, err)) { /* bail out on any parsing error */
640 p_delete(&buffer.data);
646 /* Since we allow two dates to be specified we'll have to adjust that. */
647 adjust_date_range (&min, &max);
649 pat->min = mutt_mktime (&min, 1);
650 pat->max = mutt_mktime (&max, 1);
652 p_delete(&buffer.data);
657 static struct pattern_flags *lookup_tag (char tag)
661 for (i = 0; Flags[i].tag; i++)
662 if (Flags[i].tag == tag)
667 static /* const */ char *find_matching_paren ( /* const */ char *s)
674 else if (*s == ')') {
683 void mutt_pattern_free (pattern_t ** pat)
697 mutt_pattern_free (&tmp->child);
702 pattern_t *mutt_pattern_comp ( /* const */ char *s, int flags, BUFFER * err)
704 pattern_t *curlist = NULL;
706 pattern_t *last = NULL;
710 int implicit = 1; /* used to detect logical AND operator */
711 struct pattern_flags *entry;
718 ps.dsize = m_strlen(s);
721 ps.dptr = vskipspaces(ps.dptr);
734 snprintf (err->data, err->dsize, _("error in pattern at: %s"),
739 /* A & B | C == (A & B) | C */
740 tmp = new_pattern ();
742 tmp->child = curlist;
758 if (implicit && or) {
759 /* A | B & C == (A | B) & C */
760 tmp = new_pattern ();
762 tmp->child = curlist;
768 tmp = new_pattern ();
770 tmp->alladdr = alladdr;
771 tmp->stringmatch = (*ps.dptr == '=') ? 1 : 0;
781 ps.dptr++; /* move past the ~ */
782 if ((entry = lookup_tag (*ps.dptr)) == NULL) {
783 snprintf (err->data, err->dsize, _("%c: invalid command"), *ps.dptr);
784 mutt_pattern_free (&curlist);
787 if (entry->class && (flags & entry->class) == 0) {
788 snprintf (err->data, err->dsize, _("%c: not supported in this mode"),
790 mutt_pattern_free (&curlist);
795 ps.dptr = vskipspaces(ps.dptr + 1);
797 if (entry->eat_arg) {
799 snprintf (err->data, err->dsize, _("missing parameter"));
800 mutt_pattern_free (&curlist);
803 if (entry->eat_arg (tmp, &ps, err) == -1) {
804 mutt_pattern_free (&curlist);
811 p = find_matching_paren (ps.dptr + 1);
813 snprintf (err->data, err->dsize, _("mismatched parenthesis: %s"),
815 mutt_pattern_free (&curlist);
818 /* compile the sub-expression */
819 buf = p_dupstr(ps.dptr + 1, p - ps.dptr - 1);
820 if ((tmp = mutt_pattern_comp (buf, flags, err)) == NULL) {
822 mutt_pattern_free (&curlist);
832 tmp->alladdr |= alladdr;
835 ps.dptr = p + 1; /* restore location */
838 snprintf (err->data, err->dsize, _("error in pattern at: %s"), ps.dptr);
839 mutt_pattern_free (&curlist);
844 m_strcpy(err->data, err->dsize, _("empty pattern"));
848 tmp = new_pattern ();
849 tmp->op = or ? M_OR : M_AND;
850 tmp->child = curlist;
857 perform_and (pattern_t * pat, pattern_exec_flag flags, CONTEXT * ctx,
860 for (; pat; pat = pat->next)
861 if (mutt_pattern_exec (pat, flags, ctx, hdr) <= 0)
867 perform_or (struct pattern_t *pat, pattern_exec_flag flags, CONTEXT * ctx,
870 for (; pat; pat = pat->next)
871 if (mutt_pattern_exec (pat, flags, ctx, hdr) > 0)
876 static int match_adrlist (pattern_t* pat, int match_personal, int alladdr,
884 for (a = va_arg (ap, address_t *); a; a = a->next) {
886 ((a->mailbox && patmatch (pat, a->mailbox) == 0) ||
887 (match_personal && a->personal &&
888 patmatch (pat, a->personal) == 0))) {
890 return (!pat->alladdr); /* Found match, or non-match if alladdr */
895 return pat->alladdr; /* No matches, or all matches if alladdr */
898 static int match_reference (pattern_t* pat, LIST * refs)
900 for (; refs; refs = refs->next)
901 if (patmatch (pat, refs->data) == 0)
906 int mutt_is_list_recipient (int alladdr, address_t * a1, address_t * a2)
908 for (; a1; a1 = a1->next)
909 if (alladdr ^ mutt_is_subscribed_list (a1))
911 for (; a2; a2 = a2->next)
912 if (alladdr ^ mutt_is_subscribed_list (a2))
917 int mutt_is_list_cc (int alladdr, address_t * a1, address_t * a2)
919 for (; a1; a1 = a1->next)
920 if (alladdr ^ mutt_is_mail_list (a1))
922 for (; a2; a2 = a2->next)
923 if (alladdr ^ mutt_is_mail_list (a2))
928 static int match_user (int alladdr, address_t * a1, address_t * a2)
930 for (; a1; a1 = a1->next)
931 if (alladdr ^ mutt_addr_is_user (a1))
933 for (; a2; a2 = a2->next)
934 if (alladdr ^ mutt_addr_is_user (a2))
939 /* test if name is considered a real name, i.e. consists of at least 2
940 * space-separated words of which none may end in a dot
942 static int valid_realname (const char *name)
944 const char *p = name;
951 /* skip abbr. parts of names (e.g. 'J. User') */
959 M_MATCH_FULL_ADDRESS match both personal and machine address */
961 mutt_pattern_exec (struct pattern_t *pat, pattern_exec_flag flags,
962 CONTEXT * ctx, HEADER * h)
966 return (pat->not ^ (perform_and (pat->child, flags, ctx, h) > 0));
968 return (pat->not ^ (perform_or (pat->child, flags, ctx, h) > 0));
972 return (pat->not ^ h->expired);
974 return (pat->not ^ h->superseded);
976 return (pat->not ^ h->flagged);
978 return (pat->not ^ h->tagged);
980 return (pat->not ? h->old || h->read : !(h->old || h->read));
982 return (pat->not ? h->read : !h->read);
984 return (pat->not ^ h->replied);
986 return (pat->not ? (!h->old || h->read) : (h->old && !h->read));
988 return (pat->not ^ h->read);
990 return (pat->not ^ h->deleted);
992 return (pat->not ^ (h->msgno >= pat->min - 1 && (pat->max == M_MAXRANGE ||
997 not ^ (h->date_sent >= pat->min && h->date_sent <= pat->max));
998 case M_DATE_RECEIVED:
999 return (pat->not ^ (h->received >= pat->min && h->received <= pat->max));
1004 /* IMAP search sets h->matched at search compile time */
1005 if (ctx->magic == M_IMAP && pat->stringmatch)
1006 return (h->matched);
1008 return (pat->not ^ msg_search (ctx, pat, h->msgno));
1010 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
1011 pat->alladdr, 1, h->env->sender));
1013 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
1014 pat->alladdr, 1, h->env->from));
1016 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
1017 pat->alladdr, 1, h->env->to));
1019 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
1020 pat->alladdr, 1, h->env->cc));
1023 not ^ (h->env && h->env->subject
1024 && patmatch (pat, h->env->subject) == 0));
1027 not ^ (h->env && h->env->message_id
1028 && patmatch (pat, h->env->message_id) == 0));
1030 return (pat->not ^ (h->score >= pat->min && (pat->max == M_MAXRANGE ||
1031 h->score <= pat->max)));
1034 not ^ (h->content->length >= pat->min
1035 && (pat->max == M_MAXRANGE
1036 || h->content->length <= pat->max)));
1038 return (pat->not ^ match_reference (pat, h->env->references));
1042 && match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
1043 pat->alladdr, 4, h->env->from,
1044 h->env->sender, h->env->to,
1049 && match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
1050 pat->alladdr, 2, h->env->to,
1055 && mutt_is_list_cc (pat->alladdr, h->env->to,
1057 case M_SUBSCRIBED_LIST:
1060 && mutt_is_list_recipient (pat->alladdr, h->env->to,
1062 case M_PERSONAL_RECIP:
1065 && match_user (pat->alladdr, h->env->to, h->env->cc)));
1066 case M_PERSONAL_FROM:
1068 not ^ (h->env && match_user (pat->alladdr, h->env->from, NULL)));
1070 return (pat->not ^ (h->collapsed && h->num_hidden > 1));
1072 return (pat->not ^ ((h->security & SIGN) ? 1 : 0));
1073 case M_CRYPT_VERIFIED:
1074 return (pat->not ^ ((h->security & GOODSIGN) ? 1 : 0));
1075 case M_CRYPT_ENCRYPT:
1076 return (pat->not ^ ((h->security & ENCRYPT) ? 1 : 0));
1078 return (pat->not ^ ((h->security & APPLICATION_PGP)
1079 && (h->security & PGPKEY)));
1082 not ^ (h->env->x_label
1083 && patmatch (pat, h->env->x_label) == 0));
1086 not ^ (h->env->spam && h->env->spam->data
1087 && patmatch (pat, h->env->spam->data) == 0));
1089 return (pat->not ^ (h->thread && h->thread->duplicate_thread));
1095 if (h->content->parts)
1096 count = mutt_count_body_parts(h, 0);
1098 mutt_parse_mime_message(ctx, h);
1099 count = mutt_count_body_parts(h, 0);
1100 mutt_free_body(&h->content->parts);
1103 return (pat->not ^ (count >= pat->min && (pat->max == M_MAXRANGE ||
1104 count <= pat->max)));
1107 case M_UNREFERENCED:
1108 return (pat->not ^ (h->thread && !h->thread->child));
1110 return (pat->not ^ (h->content && h->content->type == TYPEMULTIPART));
1113 * we have a match if
1114 * - From: matches $alternates
1115 * - or we have an alias for current address
1116 * - or From: contains valid email address _and_ name has >= 2 fields
1119 not ^ (h->env && h->env->from && (mutt_addr_is_user (h->env->from)
1121 (alias_reverse_lookup
1122 (h->env->from) != NULL)
1123 || (h->env->from->personal
1124 && valid_realname (h->env->
1127 && h->env->from->mailbox)
1132 not ^ (h->env->newsgroups
1133 && patmatch (pat, h->env->newsgroups) == 0));
1136 mutt_error (_("error: unknown op %d (report this error)."), pat->op);
1140 static void quote_simple (char *tmp, size_t len, const char *p)
1145 while (*p && i < len - 3) {
1146 if (*p == '\\' || *p == '"')
1154 /* convert a simple search into a real request */
1155 void mutt_check_simple (char *s, size_t len, const char *simple)
1157 char tmp[LONG_STRING];
1159 /* XXX - is ascii_strcasecmp() right here, or should we use locale's
1163 if (!strchr (s, '~') && !strchr (s, '=')) { /* yup, so spoof a real request */
1164 /* convert old tokens into the new format */
1165 if (ascii_strcasecmp ("all", s) == 0 || !m_strcmp("^", s) || !m_strcmp(".", s)) /* ~A is more efficient */
1166 m_strcpy(s, len, "~A");
1167 else if (ascii_strcasecmp ("del", s) == 0)
1168 m_strcpy(s, len, "~D");
1169 else if (ascii_strcasecmp ("flag", s) == 0)
1170 m_strcpy(s, len, "~F");
1171 else if (ascii_strcasecmp ("new", s) == 0)
1172 m_strcpy(s, len, "~N");
1173 else if (ascii_strcasecmp ("old", s) == 0)
1174 m_strcpy(s, len, "~O");
1175 else if (ascii_strcasecmp ("repl", s) == 0)
1176 m_strcpy(s, len, "~Q");
1177 else if (ascii_strcasecmp ("read", s) == 0)
1178 m_strcpy(s, len, "~R");
1179 else if (ascii_strcasecmp ("tag", s) == 0)
1180 m_strcpy(s, len, "~T");
1181 else if (ascii_strcasecmp ("unread", s) == 0)
1182 m_strcpy(s, len, "~U");
1184 quote_simple (tmp, sizeof (tmp), s);
1185 mutt_expand_fmt (s, len, simple, tmp);
1190 int mutt_pattern_func (int op, char *prompt)
1193 char buf[LONG_STRING] = "", *simple, error[STRING];
1197 m_strcpy(buf, sizeof(buf), NONULL(Context->pattern));
1198 if (prompt || op != M_LIMIT)
1199 if (mutt_get_field (prompt, buf, sizeof (buf), M_PATTERN | M_CLEAR) != 0 || !buf[0])
1202 mutt_message _("Compiling search pattern...");
1204 simple = m_strdup(buf);
1205 mutt_check_simple (buf, sizeof (buf), NONULL (SimpleSearch));
1208 err.dsize = sizeof (error);
1209 if ((pat = mutt_pattern_comp (buf, M_FULL_MSG, &err)) == NULL) {
1211 mutt_error ("%s", err.data);
1216 if (Context->magic == M_IMAP && imap_search (Context, pat) < 0)
1220 mutt_message _("Executing command on matching messages...");
1222 #define THIS_BODY Context->hdrs[i]->content
1224 if (op == M_LIMIT) {
1225 Context->vcount = 0;
1227 Context->collapsed = 0;
1229 for (i = 0; i < Context->msgcount; i++) {
1230 /* new limit pattern implicitly uncollapses all threads */
1231 Context->hdrs[i]->virtual = -1;
1232 Context->hdrs[i]->limited = 0;
1233 Context->hdrs[i]->collapsed = 0;
1234 Context->hdrs[i]->num_hidden = 0;
1235 if (mutt_pattern_exec
1236 (pat, M_MATCH_FULL_ADDRESS, Context, Context->hdrs[i])) {
1237 Context->hdrs[i]->virtual = Context->vcount;
1238 Context->hdrs[i]->limited = 1;
1239 Context->v2r[Context->vcount] = i;
1241 Context->vsize += THIS_BODY->length + THIS_BODY->offset -
1242 THIS_BODY->hdr_offset;
1247 for (i = 0; i < Context->vcount; i++) {
1248 if (mutt_pattern_exec
1249 (pat, M_MATCH_FULL_ADDRESS, Context,
1250 Context->hdrs[Context->v2r[i]])) {
1253 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_PURGED,
1256 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_DELETE,
1261 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_TAG,
1271 mutt_clear_error ();
1273 if (op == M_LIMIT) {
1274 /* drop previous limit pattern */
1275 p_delete(&Context->pattern);
1276 if (Context->limit_pattern)
1277 mutt_pattern_free (&Context->limit_pattern);
1278 if (Context->msgcount && !Context->vcount) {
1279 mutt_error _("No messages matched criteria.");
1282 /* record new limit pattern, unless match all */
1283 if (m_strncmp(buf, "~A", 2) != 0) {
1284 Context->pattern = simple;
1285 simple = NULL; /* don't clobber it */
1286 Context->limit_pattern = mutt_pattern_comp (buf, M_FULL_MSG, &err);
1290 mutt_pattern_free (&pat);
1294 int mutt_search_command (int cur, int op)
1298 char temp[LONG_STRING];
1304 if (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE) {
1305 m_strcpy(buf, sizeof(buf), LastSearch);
1306 if (mutt_get_field ((op == OP_SEARCH) ? _("Search for: ") :
1307 _("Reverse search for: "), buf, sizeof (buf),
1308 M_CLEAR | M_PATTERN) != 0 || !buf[0])
1311 if (op == OP_SEARCH)
1312 unset_option (OPTSEARCHREVERSE);
1314 set_option (OPTSEARCHREVERSE);
1316 /* compare the *expanded* version of the search pattern in case
1317 $simple_search has changed while we were searching */
1318 m_strcpy(temp, sizeof(temp), buf);
1319 mutt_check_simple (temp, sizeof (temp), NONULL (SimpleSearch));
1321 if (!SearchPattern || m_strcmp(temp, LastSearchExpn)) {
1322 set_option (OPTSEARCHINVALID);
1323 m_strcpy(LastSearch, sizeof(LastSearch), buf);
1324 mutt_message _("Compiling search pattern...");
1326 mutt_pattern_free (&SearchPattern);
1328 err.dsize = sizeof (error);
1329 if ((SearchPattern =
1330 mutt_pattern_comp (temp, M_FULL_MSG, &err)) == NULL) {
1331 mutt_error ("%s", error);
1334 mutt_clear_error ();
1337 else if (!SearchPattern) {
1338 mutt_error _("No search pattern.");
1343 if (option (OPTSEARCHINVALID)) {
1344 for (i = 0; i < Context->msgcount; i++)
1345 Context->hdrs[i]->searched = 0;
1347 if (Context->magic == M_IMAP && imap_search (Context, SearchPattern) < 0)
1350 unset_option (OPTSEARCHINVALID);
1353 incr = (option (OPTSEARCHREVERSE)) ? -1 : 1;
1354 if (op == OP_SEARCH_OPPOSITE)
1357 for (i = cur + incr, j = 0; j != Context->vcount; j++) {
1358 if (i > Context->vcount - 1) {
1360 if (option (OPTWRAPSEARCH))
1361 mutt_message (_("Search wrapped to top."));
1364 mutt_message _("Search hit bottom without finding match");
1370 i = Context->vcount - 1;
1371 if (option (OPTWRAPSEARCH))
1372 mutt_message (_("Search wrapped to bottom."));
1375 mutt_message _("Search hit top without finding match");
1381 h = Context->hdrs[Context->v2r[i]];
1383 /* if we've already evaulated this message, use the cached value */
1388 /* remember that we've already searched this message */
1392 (SearchPattern, M_MATCH_FULL_ADDRESS, Context, h) > 0)))
1397 mutt_error _("Search interrupted.");
1406 mutt_error _("Not found.");