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/mem.h>
22 #include <lib-lib/ascii.h>
23 #include <lib-lib/str.h>
24 #include <lib-lib/macros.h>
25 #include <lib-lib/file.h>
26 #include <lib-lib/buffer.h>
27 #include <lib-lib/mapping.h>
29 #include <lib-mime/mime.h>
31 #include <lib-ui/enter.h>
40 #include <imap/imap.h>
42 #include <lib-crypt/crypt.h>
44 static int eat_regexp (pattern_t * pat, BUFFER *, BUFFER *);
45 static int eat_date (pattern_t * pat, BUFFER *, BUFFER *);
46 static int eat_range (pattern_t * pat, BUFFER *, BUFFER *);
47 static int patmatch (const pattern_t* pat, const char* buf);
49 struct pattern_flags {
50 int tag; /* character used to represent this op */
51 int op; /* operation to perform */
53 int (*eat_arg)(pattern_t *, BUFFER *, BUFFER *);
55 {'A', M_ALL, 0, NULL},
56 {'b', M_BODY, M_FULL_MSG, eat_regexp},
57 {'B', M_WHOLE_MSG, M_FULL_MSG, eat_regexp},
58 {'c', M_CC, 0, eat_regexp},
59 {'C', M_RECIPIENT, 0, eat_regexp},
60 {'d', M_DATE, 0, eat_date},
61 {'D', M_DELETED, 0, NULL},
62 {'e', M_SENDER, 0, eat_regexp},
63 {'E', M_EXPIRED, 0, NULL},
64 {'f', M_FROM, 0, eat_regexp},
65 {'F', M_FLAG, 0, NULL},
66 {'g', M_CRYPT_SIGN, 0, NULL},
67 {'G', M_CRYPT_ENCRYPT, 0, NULL},
68 {'h', M_HEADER, M_FULL_MSG, eat_regexp},
69 {'H', M_HORMEL, 0, eat_regexp},
70 {'i', M_ID, 0, eat_regexp},
71 {'k', M_PGP_KEY, 0, NULL},
72 {'L', M_ADDRESS, 0, eat_regexp},
73 {'l', M_LIST, 0, NULL},
74 {'m', M_MESSAGE, 0, eat_range},
75 {'M', M_MULTIPART, 0, NULL},
76 {'n', M_SCORE, 0, eat_range},
77 {'N', M_NEW, 0, NULL},
78 {'O', M_OLD, 0, NULL},
79 {'p', M_PERSONAL_RECIP, 0, NULL},
80 {'P', M_PERSONAL_FROM, 0, NULL},
81 {'Q', M_REPLIED, 0, NULL},
82 {'R', M_READ, 0, NULL},
83 {'r', M_DATE_RECEIVED, 0, eat_date},
84 {'s', M_SUBJECT, 0, eat_regexp},
85 {'S', M_SUPERSEDED, 0, NULL},
86 {'T', M_TAG, 0, NULL},
87 {'t', M_TO, 0, eat_regexp},
88 {'U', M_UNREAD, 0, NULL},
89 {'u', M_SUBSCRIBED_LIST, 0, NULL},
90 {'v', M_COLLAPSED, 0, NULL},
91 {'V', M_CRYPT_VERIFIED, 0, NULL},
93 {'w', M_NEWSGROUPS, 0, eat_regexp},
95 {'x', M_REFERENCE, 0, eat_regexp},
96 {'X', M_MIMEATTACH, 0, eat_range},
97 {'y', M_XLABEL, 0, eat_regexp},
98 {'z', M_SIZE, 0, eat_range},
99 {'=', M_DUPLICATED, 0, NULL},
100 {'$', M_UNREFERENCED, 0, NULL},
101 {'*', M_REALNAME, 0, NULL},
105 static pattern_t *SearchPattern = NULL; /* current search pattern */
106 static char LastSearch[STRING] = { 0 }; /* last pattern searched for */
107 static char LastSearchExpn[LONG_STRING] = { 0 }; /* expanded version of
110 #define M_MAXRANGE -1
112 /* constants for parse_date_range() */
113 #define M_PDR_NONE 0x0000
114 #define M_PDR_MINUS 0x0001
115 #define M_PDR_PLUS 0x0002
116 #define M_PDR_WINDOW 0x0004
117 #define M_PDR_ABSOLUTE 0x0008
118 #define M_PDR_DONE 0x0010
119 #define M_PDR_ERROR 0x0100
120 #define M_PDR_ERRORDONE (M_PDR_ERROR | M_PDR_DONE)
123 int mutt_getvaluebychar (char ch, struct mapping_t *table)
127 for (i = 0; table[i].name; i++) {
128 if (ch == table[i].name[0])
129 return table[i].value;
135 /* if no uppercase letters are given, do a case-insensitive search */
136 int mutt_which_case (const char *s)
139 if (isalpha ((unsigned char) *s) && isupper ((unsigned char) *s))
140 return 0; /* case-sensitive */
143 return REG_ICASE; /* case-insensitive */
147 msg_search (CONTEXT *ctx, pattern_t* pat, int msgno)
149 char tempfile[_POSIX_PATH_MAX];
156 HEADER *h = ctx->hdrs[msgno];
160 if ((msg = mx_open_message (ctx, msgno)) != NULL) {
161 if (option (OPTTHOROUGHSRC)) {
162 /* decode the header / body */
165 s.flags = M_CHARCONV;
166 mutt_mktemp (tempfile);
167 if ((s.fpout = safe_fopen (tempfile, "w+")) == NULL) {
168 mutt_perror (tempfile);
172 if (pat->op != M_BODY)
173 mutt_copy_header (msg->fp, h, s.fpout, CH_FROM | CH_DECODE, NULL);
175 if (pat->op != M_HEADER) {
176 mutt_parse_mime_message (ctx, h);
178 if ((h->security & ENCRYPT) && !crypt_valid_passphrase (h->security))
180 mx_close_message (&msg);
188 fseeko (msg->fp, h->offset, 0);
189 mutt_body_handler (h->content, &s);
195 fstat (fileno (fp), &st);
196 lng = (long) st.st_size;
199 /* raw header / body */
201 if (pat->op != M_BODY) {
202 fseeko (fp, h->offset, 0);
203 lng = h->content->offset - h->offset;
205 if (pat->op != M_HEADER) {
206 if (pat->op == M_BODY)
207 fseeko (fp, h->content->offset, 0);
208 lng += h->content->length;
212 buf = p_new(char, blen = STRING);
214 /* search the file "fp" */
216 if (pat->op == M_HEADER) {
217 if (!mutt_read_rfc822_line(fp, &buf, &blen))
219 } else if (fgets (buf, blen - 1, fp) == NULL)
220 break; /* don't loop forever */
221 if (patmatch (pat, buf) == 0) {
225 lng -= m_strlen(buf);
230 mx_close_message (&msg);
232 if (option (OPTTHOROUGHSRC)) {
241 int eat_regexp (pattern_t * pat, BUFFER * s, BUFFER * err)
248 if (mutt_extract_token (&buf, s, M_TOKEN_PATTERN | M_TOKEN_COMMENT) != 0 ||
250 snprintf (err->data, err->dsize, _("Error in expression: %s"), s->dptr);
255 snprintf (err->data, err->dsize, _("Empty expression"));
260 /* If there are no RE metacharacters, use simple search anyway */
261 if (!pat->stringmatch && !strpbrk (buf.data, "|[{.*+?^$"))
262 pat->stringmatch = 1;
265 if (pat->stringmatch) {
266 pat->str = m_strdup(buf.data);
269 pat->rx = p_new(regex_t, 1);
270 r = REGCOMP (pat->rx, buf.data, REG_NEWLINE | REG_NOSUB | mutt_which_case (buf.data));
273 regerror (r, pat->rx, err->data, err->dsize);
282 static int patmatch (const pattern_t* pat, const char* buf) {
283 if (pat->stringmatch)
284 return !strstr (buf, pat->str);
286 return regexec (pat->rx, buf, 0, NULL, 0);
289 int eat_range (pattern_t * pat, BUFFER * s, BUFFER * err __attribute__ ((unused)))
292 int do_exclusive = 0;
296 * If simple_search is set to "~m %s", the range will have double quotes
299 if (*s->dptr == '"') {
305 if ((*s->dptr != '-') && (*s->dptr != '<')) {
307 if (*s->dptr == '>') {
308 pat->max = M_MAXRANGE;
309 pat->min = strtol (s->dptr + 1, &tmp, 0) + 1; /* exclusive range */
312 pat->min = strtol (s->dptr, &tmp, 0);
313 if (toupper ((unsigned char) *tmp) == 'K') { /* is there a prefix? */
317 else if (toupper ((unsigned char) *tmp) == 'M') {
321 if (*s->dptr == '>') {
338 if (isdigit ((unsigned char) *tmp)) {
340 pat->max = strtol (tmp, &tmp, 0);
341 if (toupper ((unsigned char) *tmp) == 'K') {
345 else if (toupper ((unsigned char) *tmp) == 'M') {
353 pat->max = M_MAXRANGE;
355 if (skip_quote && *tmp == '"')
358 s->dptr = vskipspaces(tmp);
362 static const char *getDate (const char *s, struct tm *t, BUFFER * err)
365 time_t now = time (NULL);
366 struct tm *tm = localtime (&now);
368 t->tm_mday = strtol (s, &p, 10);
369 if (t->tm_mday < 1 || t->tm_mday > 31) {
370 snprintf (err->data, err->dsize, _("Invalid day of month: %s"), s);
374 /* fill in today's month and year */
375 t->tm_mon = tm->tm_mon;
376 t->tm_year = tm->tm_year;
380 t->tm_mon = strtol (p, &p, 10) - 1;
381 if (t->tm_mon < 0 || t->tm_mon > 11) {
382 snprintf (err->data, err->dsize, _("Invalid month: %s"), p);
386 t->tm_year = tm->tm_year;
390 t->tm_year = strtol (p, &p, 10);
391 if (t->tm_year < 70) /* year 2000+ */
393 else if (t->tm_year > 1900)
402 static const char *get_offset (struct tm *tm, const char *s, int sign)
405 int offset = strtol (s, &ps, 0);
407 if ((sign < 0 && offset > 0) || (sign > 0 && offset < 0))
412 tm->tm_year += offset;
415 tm->tm_mon += offset;
418 tm->tm_mday += 7 * offset;
421 tm->tm_mday += offset;
426 mutt_normalize_time (tm);
430 static void adjust_date_range (struct tm *min, struct tm *max)
432 if (min->tm_year > max->tm_year
433 || (min->tm_year == max->tm_year && min->tm_mon > max->tm_mon)
434 || (min->tm_year == max->tm_year && min->tm_mon == max->tm_mon
435 && min->tm_mday > max->tm_mday)) {
439 min->tm_year = max->tm_year;
443 min->tm_mon = max->tm_mon;
447 min->tm_mday = max->tm_mday;
450 min->tm_hour = min->tm_min = min->tm_sec = 0;
452 max->tm_min = max->tm_sec = 59;
456 static const char *parse_date_range (const char *pc, struct tm *min,
457 struct tm *max, int haveMin,
458 struct tm *baseMin, BUFFER * err)
460 int flag = M_PDR_NONE;
462 while (*pc && ((flag & M_PDR_DONE) == 0)) {
466 pc = vskipspaces(pc);
470 /* try a range of absolute date minus offset of Ndwmy */
471 pt = get_offset (min, pc, -1);
473 if (flag == M_PDR_NONE) { /* nothing yet and no offset parsed => absolute date? */
474 if (!getDate (pc, max, err))
475 flag |= (M_PDR_ABSOLUTE | M_PDR_ERRORDONE); /* done bad */
477 /* reestablish initial base minimum if not specified */
479 memcpy (min, baseMin, sizeof (struct tm));
480 flag |= (M_PDR_ABSOLUTE | M_PDR_DONE); /* done good */
484 flag |= M_PDR_ERRORDONE;
488 if (flag == M_PDR_NONE && !haveMin) { /* the very first "-3d" without a previous absolute date */
489 max->tm_year = min->tm_year;
490 max->tm_mon = min->tm_mon;
491 max->tm_mday = min->tm_mday;
498 { /* enlarge plusRange */
499 pt = get_offset (max, pc, 1);
501 flag |= M_PDR_ERRORDONE;
509 { /* enlarge window in both directions */
510 pt = get_offset (min, pc, -1);
512 flag |= M_PDR_ERRORDONE;
514 pc = get_offset (max, pc, 1);
515 flag |= M_PDR_WINDOW;
520 flag |= M_PDR_ERRORDONE;
522 pc = vskipspaces(pc);
524 if ((flag & M_PDR_ERROR) && !(flag & M_PDR_ABSOLUTE)) { /* getDate has its own error message, don't overwrite it here */
525 snprintf (err->data, err->dsize, _("Invalid relative date: %s"), pc - 1);
527 return ((flag & M_PDR_ERROR) ? NULL : pc);
530 static int eat_date (pattern_t * pat, BUFFER * s, BUFFER * err)
536 if (mutt_extract_token (&buffer, s, M_TOKEN_COMMENT | M_TOKEN_PATTERN) != 0
538 m_strcpy(err->data, err->dsize, _("error in expression"));
543 /* the `0' time is Jan 1, 1970 UTC, so in order to prevent a negative time
544 when doing timezone conversion, we use Jan 2, 1970 UTC as the base
551 /* Arbitrary year in the future. Don't set this too high
552 or mutt_mktime() returns something larger than will
553 fit in a time_t on some systems */
561 if (strchr ("<>=", buffer.data[0])) {
562 /* offset from current time
563 <3d less than three days ago
564 >3d more than three days ago
565 =3d exactly three days ago */
566 time_t now = time (NULL);
567 struct tm *tm = localtime (&now);
570 if (buffer.data[0] == '<') {
571 memcpy (&min, tm, sizeof (min));
575 memcpy (&max, tm, sizeof (max));
578 if (buffer.data[0] == '=')
582 tm->tm_min = tm->tm_sec = 59;
584 /* force negative offset */
585 get_offset (tm, buffer.data + 1, -1);
588 /* start at the beginning of the day in question */
589 memcpy (&min, &max, sizeof (max));
590 min.tm_hour = min.tm_sec = min.tm_min = 0;
594 const char *pc = buffer.data;
597 int untilNow = FALSE;
599 if (isdigit ((unsigned char) *pc)) {
600 /* mininum date specified */
601 if ((pc = getDate (pc, &min, err)) == NULL) {
602 p_delete(&buffer.data);
606 pc = vskipspaces(pc);
610 pt = skipspaces(pc + 1);
611 untilNow = (*pt == '\0');
615 if (!untilNow) { /* max date or relative range/window */
619 if (!haveMin) { /* save base minimum and set current date, e.g. for "-3d+1d" */
620 time_t now = time (NULL);
621 struct tm *tm = localtime (&now);
623 memcpy (&baseMin, &min, sizeof (baseMin));
624 memcpy (&min, tm, sizeof (min));
625 min.tm_hour = min.tm_sec = min.tm_min = 0;
628 /* preset max date for relative offsets,
629 if nothing follows we search for messages on a specific day */
630 max.tm_year = min.tm_year;
631 max.tm_mon = min.tm_mon;
632 max.tm_mday = min.tm_mday;
634 if (!parse_date_range (pc, &min, &max, haveMin, &baseMin, err)) { /* bail out on any parsing error */
635 p_delete(&buffer.data);
641 /* Since we allow two dates to be specified we'll have to adjust that. */
642 adjust_date_range (&min, &max);
644 pat->min = mutt_mktime (&min, 1);
645 pat->max = mutt_mktime (&max, 1);
647 p_delete(&buffer.data);
652 static struct pattern_flags *lookup_tag (char tag)
656 for (i = 0; Flags[i].tag; i++)
657 if (Flags[i].tag == tag)
662 static /* const */ char *find_matching_paren ( /* const */ char *s)
669 else if (*s == ')') {
678 void mutt_pattern_free (pattern_t ** pat)
692 mutt_pattern_free (&tmp->child);
697 pattern_t *mutt_pattern_comp ( /* const */ char *s, int flags, BUFFER * err)
699 pattern_t *curlist = NULL;
701 pattern_t *last = NULL;
705 int implicit = 1; /* used to detect logical AND operator */
706 struct pattern_flags *entry;
713 ps.dsize = m_strlen(s);
716 ps.dptr = vskipspaces(ps.dptr);
729 snprintf (err->data, err->dsize, _("error in pattern at: %s"),
734 /* A & B | C == (A & B) | C */
735 tmp = new_pattern ();
737 tmp->child = curlist;
753 if (implicit && or) {
754 /* A | B & C == (A | B) & C */
755 tmp = new_pattern ();
757 tmp->child = curlist;
763 tmp = new_pattern ();
765 tmp->alladdr = alladdr;
766 tmp->stringmatch = (*ps.dptr == '=') ? 1 : 0;
776 ps.dptr++; /* move past the ~ */
777 if ((entry = lookup_tag (*ps.dptr)) == NULL) {
778 snprintf (err->data, err->dsize, _("%c: invalid command"), *ps.dptr);
779 mutt_pattern_free (&curlist);
782 if (entry->class && (flags & entry->class) == 0) {
783 snprintf (err->data, err->dsize, _("%c: not supported in this mode"),
785 mutt_pattern_free (&curlist);
790 ps.dptr = vskipspaces(ps.dptr + 1);
792 if (entry->eat_arg) {
794 snprintf (err->data, err->dsize, _("missing parameter"));
795 mutt_pattern_free (&curlist);
798 if (entry->eat_arg (tmp, &ps, err) == -1) {
799 mutt_pattern_free (&curlist);
806 p = find_matching_paren (ps.dptr + 1);
808 snprintf (err->data, err->dsize, _("mismatched parenthesis: %s"),
810 mutt_pattern_free (&curlist);
813 /* compile the sub-expression */
814 buf = p_dupstr(ps.dptr + 1, p - ps.dptr - 1);
815 if ((tmp = mutt_pattern_comp (buf, flags, err)) == NULL) {
817 mutt_pattern_free (&curlist);
827 tmp->alladdr |= alladdr;
830 ps.dptr = p + 1; /* restore location */
833 snprintf (err->data, err->dsize, _("error in pattern at: %s"), ps.dptr);
834 mutt_pattern_free (&curlist);
839 m_strcpy(err->data, err->dsize, _("empty pattern"));
843 tmp = new_pattern ();
844 tmp->op = or ? M_OR : M_AND;
845 tmp->child = curlist;
852 perform_and (pattern_t * pat, pattern_exec_flag flags, CONTEXT * ctx,
855 for (; pat; pat = pat->next)
856 if (mutt_pattern_exec (pat, flags, ctx, hdr) <= 0)
862 perform_or (struct pattern_t *pat, pattern_exec_flag flags, CONTEXT * ctx,
865 for (; pat; pat = pat->next)
866 if (mutt_pattern_exec (pat, flags, ctx, hdr) > 0)
871 static int match_adrlist (pattern_t* pat, int match_personal, int alladdr __attribute__ ((unused)),
879 for (a = va_arg (ap, address_t *); a; a = a->next) {
881 ((a->mailbox && patmatch (pat, a->mailbox) == 0) ||
882 (match_personal && a->personal &&
883 patmatch (pat, a->personal) == 0))) {
885 return (!pat->alladdr); /* Found match, or non-match if alladdr */
890 return pat->alladdr; /* No matches, or all matches if alladdr */
893 static int match_reference (pattern_t* pat, string_list_t * refs)
895 for (; refs; refs = refs->next)
896 if (patmatch (pat, refs->data) == 0)
901 int mutt_is_list_recipient (int alladdr, address_t * a1, address_t * a2)
903 for (; a1; a1 = a1->next)
904 if (alladdr ^ mutt_is_subscribed_list (a1))
906 for (; a2; a2 = a2->next)
907 if (alladdr ^ mutt_is_subscribed_list (a2))
912 int mutt_is_list_cc (int alladdr, address_t * a1, address_t * a2)
914 for (; a1; a1 = a1->next)
915 if (alladdr ^ mutt_is_mail_list (a1))
917 for (; a2; a2 = a2->next)
918 if (alladdr ^ mutt_is_mail_list (a2))
923 static int match_user (int alladdr, address_t * a1, address_t * a2)
925 for (; a1; a1 = a1->next)
926 if (alladdr ^ mutt_addr_is_user (a1))
928 for (; a2; a2 = a2->next)
929 if (alladdr ^ mutt_addr_is_user (a2))
934 /* test if name is considered a real name, i.e. consists of at least 2
935 * space-separated words of which none may end in a dot
937 static int valid_realname (const char *name)
939 const char *p = name;
946 /* skip abbr. parts of names (e.g. 'J. User') */
954 M_MATCH_FULL_ADDRESS match both personal and machine address */
956 mutt_pattern_exec (struct pattern_t *pat, pattern_exec_flag flags,
957 CONTEXT * ctx, HEADER * h)
961 return (pat->not ^ (perform_and (pat->child, flags, ctx, h) > 0));
963 return (pat->not ^ (perform_or (pat->child, flags, ctx, h) > 0));
967 return (pat->not ^ h->expired);
969 return (pat->not ^ h->superseded);
971 return (pat->not ^ h->flagged);
973 return (pat->not ^ h->tagged);
975 return (pat->not ? h->old || h->read : !(h->old || h->read));
977 return (pat->not ? h->read : !h->read);
979 return (pat->not ^ h->replied);
981 return (pat->not ? (!h->old || h->read) : (h->old && !h->read));
983 return (pat->not ^ h->read);
985 return (pat->not ^ h->deleted);
987 return (pat->not ^ (h->msgno >= pat->min - 1 && (pat->max == M_MAXRANGE ||
992 not ^ (h->date_sent >= pat->min && h->date_sent <= pat->max));
993 case M_DATE_RECEIVED:
994 return (pat->not ^ (h->received >= pat->min && h->received <= pat->max));
998 /* IMAP search sets h->matched at search compile time */
999 if (ctx->magic == M_IMAP && pat->stringmatch)
1000 return (h->matched);
1001 return (pat->not ^ msg_search (ctx, pat, h->msgno));
1003 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
1004 pat->alladdr, 1, h->env->sender));
1006 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
1007 pat->alladdr, 1, h->env->from));
1009 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
1010 pat->alladdr, 1, h->env->to));
1012 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
1013 pat->alladdr, 1, h->env->cc));
1016 not ^ (h->env && h->env->subject
1017 && patmatch (pat, h->env->subject) == 0));
1020 not ^ (h->env && h->env->message_id
1021 && patmatch (pat, h->env->message_id) == 0));
1023 return (pat->not ^ (h->score >= pat->min && (pat->max == M_MAXRANGE ||
1024 h->score <= pat->max)));
1027 not ^ (h->content->length >= pat->min
1028 && (pat->max == M_MAXRANGE
1029 || h->content->length <= pat->max)));
1031 return (pat->not ^ match_reference (pat, h->env->references));
1035 && match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
1036 pat->alladdr, 4, h->env->from,
1037 h->env->sender, h->env->to,
1042 && match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
1043 pat->alladdr, 2, h->env->to,
1048 && mutt_is_list_cc (pat->alladdr, h->env->to,
1050 case M_SUBSCRIBED_LIST:
1053 && mutt_is_list_recipient (pat->alladdr, h->env->to,
1055 case M_PERSONAL_RECIP:
1058 && match_user (pat->alladdr, h->env->to, h->env->cc)));
1059 case M_PERSONAL_FROM:
1061 not ^ (h->env && match_user (pat->alladdr, h->env->from, NULL)));
1063 return (pat->not ^ (h->collapsed && h->num_hidden > 1));
1065 return (pat->not ^ ((h->security & SIGN) ? 1 : 0));
1066 case M_CRYPT_VERIFIED:
1067 return (pat->not ^ ((h->security & GOODSIGN) ? 1 : 0));
1068 case M_CRYPT_ENCRYPT:
1069 return (pat->not ^ ((h->security & ENCRYPT) ? 1 : 0));
1071 return (pat->not ^ ((h->security & APPLICATION_PGP)
1072 && (h->security & PGPKEY)));
1075 not ^ (h->env->x_label
1076 && patmatch (pat, h->env->x_label) == 0));
1079 not ^ (h->env->spam && h->env->spam->data
1080 && patmatch (pat, h->env->spam->data) == 0));
1082 return (pat->not ^ (h->thread && h->thread->duplicate_thread));
1088 if (h->content->parts)
1089 count = mutt_count_body_parts(h, 0);
1091 mutt_parse_mime_message(ctx, h);
1092 count = mutt_count_body_parts(h, 0);
1093 mutt_free_body(&h->content->parts);
1096 return (pat->not ^ (count >= pat->min && (pat->max == M_MAXRANGE ||
1097 count <= pat->max)));
1100 case M_UNREFERENCED:
1101 return (pat->not ^ (h->thread && !h->thread->child));
1103 return (pat->not ^ (h->content && h->content->type == TYPEMULTIPART));
1106 * we have a match if
1107 * - From: matches $alternates
1108 * - or we have an alias for current address
1109 * - or From: contains valid email address _and_ name has >= 2 fields
1111 return (h->env && h->env->from
1112 && (mutt_addr_is_user(h->env->from)
1113 || alias_reverse_lookup(h->env->from)
1114 || (h->env->from->personal
1115 && valid_realname(h->env-> from->personal)
1116 && h->env->from->mailbox)
1121 not ^ (h->env->newsgroups
1122 && patmatch (pat, h->env->newsgroups) == 0));
1125 mutt_error (_("error: unknown op %d (report this error)."), pat->op);
1129 static void quote_simple (char *tmp, ssize_t len, const char *p)
1134 while (*p && i < len - 3) {
1135 if (*p == '\\' || *p == '"')
1143 /* convert a simple search into a real request */
1144 void mutt_check_simple (char *s, ssize_t len, const char *simple)
1146 char tmp[LONG_STRING];
1148 /* XXX - is ascii_strcasecmp() right here, or should we use locale's
1152 if (!strchr (s, '~') && !strchr (s, '=')) { /* yup, so spoof a real request */
1153 /* convert old tokens into the new format */
1154 if (ascii_strcasecmp ("all", s) == 0 || !m_strcmp("^", s) || !m_strcmp(".", s)) /* ~A is more efficient */
1155 m_strcpy(s, len, "~A");
1156 else if (ascii_strcasecmp ("del", s) == 0)
1157 m_strcpy(s, len, "~D");
1158 else if (ascii_strcasecmp ("flag", s) == 0)
1159 m_strcpy(s, len, "~F");
1160 else if (ascii_strcasecmp ("new", s) == 0)
1161 m_strcpy(s, len, "~N");
1162 else if (ascii_strcasecmp ("old", s) == 0)
1163 m_strcpy(s, len, "~O");
1164 else if (ascii_strcasecmp ("repl", s) == 0)
1165 m_strcpy(s, len, "~Q");
1166 else if (ascii_strcasecmp ("read", s) == 0)
1167 m_strcpy(s, len, "~R");
1168 else if (ascii_strcasecmp ("tag", s) == 0)
1169 m_strcpy(s, len, "~T");
1170 else if (ascii_strcasecmp ("unread", s) == 0)
1171 m_strcpy(s, len, "~U");
1173 quote_simple (tmp, sizeof (tmp), s);
1174 mutt_expand_fmt (s, len, simple, tmp);
1179 int mutt_pattern_func (int op, char *prompt)
1182 char buf[LONG_STRING] = "", *simple, error[STRING];
1186 m_strcpy(buf, sizeof(buf), NONULL(Context->pattern));
1187 if (prompt || op != M_LIMIT)
1188 if (mutt_get_field (prompt, buf, sizeof (buf), M_PATTERN | M_CLEAR) != 0 || !buf[0])
1191 mutt_message _("Compiling search pattern...");
1193 simple = m_strdup(buf);
1194 mutt_check_simple (buf, sizeof (buf), NONULL (SimpleSearch));
1197 err.dsize = sizeof (error);
1198 if ((pat = mutt_pattern_comp (buf, M_FULL_MSG, &err)) == NULL) {
1200 mutt_error ("%s", err.data);
1204 if (Context->magic == M_IMAP && imap_search (Context, pat) < 0)
1207 mutt_message _("Executing command on matching messages...");
1209 #define THIS_BODY Context->hdrs[i]->content
1211 if (op == M_LIMIT) {
1212 Context->vcount = 0;
1214 Context->collapsed = 0;
1216 for (i = 0; i < Context->msgcount; i++) {
1217 /* new limit pattern implicitly uncollapses all threads */
1218 Context->hdrs[i]->virtual = -1;
1219 Context->hdrs[i]->limited = 0;
1220 Context->hdrs[i]->collapsed = 0;
1221 Context->hdrs[i]->num_hidden = 0;
1222 if (mutt_pattern_exec
1223 (pat, M_MATCH_FULL_ADDRESS, Context, Context->hdrs[i])) {
1224 Context->hdrs[i]->virtual = Context->vcount;
1225 Context->hdrs[i]->limited = 1;
1226 Context->v2r[Context->vcount] = i;
1228 Context->vsize += THIS_BODY->length + THIS_BODY->offset -
1229 THIS_BODY->hdr_offset;
1234 for (i = 0; i < Context->vcount; i++) {
1235 if (mutt_pattern_exec
1236 (pat, M_MATCH_FULL_ADDRESS, Context,
1237 Context->hdrs[Context->v2r[i]])) {
1240 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_PURGED,
1243 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_DELETE,
1248 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_TAG,
1258 mutt_clear_error ();
1260 if (op == M_LIMIT) {
1261 /* drop previous limit pattern */
1262 p_delete(&Context->pattern);
1263 if (Context->limit_pattern)
1264 mutt_pattern_free (&Context->limit_pattern);
1265 if (Context->msgcount && !Context->vcount) {
1266 mutt_error _("No messages matched criteria.");
1269 /* record new limit pattern, unless match all */
1270 if (m_strncmp(buf, "~A", 2) != 0) {
1271 Context->pattern = simple;
1272 simple = NULL; /* don't clobber it */
1273 Context->limit_pattern = mutt_pattern_comp (buf, M_FULL_MSG, &err);
1277 mutt_pattern_free (&pat);
1281 int mutt_search_command (int cur, int op)
1285 char temp[LONG_STRING];
1291 if (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE) {
1292 m_strcpy(buf, sizeof(buf), LastSearch);
1293 if (mutt_get_field ((op == OP_SEARCH) ? _("Search for: ") :
1294 _("Reverse search for: "), buf, sizeof (buf),
1295 M_CLEAR | M_PATTERN) != 0 || !buf[0])
1298 if (op == OP_SEARCH)
1299 unset_option (OPTSEARCHREVERSE);
1301 set_option (OPTSEARCHREVERSE);
1303 /* compare the *expanded* version of the search pattern in case
1304 $simple_search has changed while we were searching */
1305 m_strcpy(temp, sizeof(temp), buf);
1306 mutt_check_simple (temp, sizeof (temp), NONULL (SimpleSearch));
1308 if (!SearchPattern || m_strcmp(temp, LastSearchExpn)) {
1309 set_option (OPTSEARCHINVALID);
1310 m_strcpy(LastSearch, sizeof(LastSearch), buf);
1311 mutt_message _("Compiling search pattern...");
1313 mutt_pattern_free (&SearchPattern);
1315 err.dsize = sizeof (error);
1316 if ((SearchPattern =
1317 mutt_pattern_comp (temp, M_FULL_MSG, &err)) == NULL) {
1318 mutt_error ("%s", error);
1321 mutt_clear_error ();
1324 else if (!SearchPattern) {
1325 mutt_error _("No search pattern.");
1330 if (option (OPTSEARCHINVALID)) {
1331 for (i = 0; i < Context->msgcount; i++)
1332 Context->hdrs[i]->searched = 0;
1333 if (Context->magic == M_IMAP && imap_search (Context, SearchPattern) < 0)
1335 unset_option (OPTSEARCHINVALID);
1338 incr = (option (OPTSEARCHREVERSE)) ? -1 : 1;
1339 if (op == OP_SEARCH_OPPOSITE)
1342 for (i = cur + incr, j = 0; j != Context->vcount; j++) {
1343 if (i > Context->vcount - 1) {
1345 if (option (OPTWRAPSEARCH))
1346 mutt_message (_("Search wrapped to top."));
1349 mutt_message _("Search hit bottom without finding match");
1355 i = Context->vcount - 1;
1356 if (option (OPTWRAPSEARCH))
1357 mutt_message (_("Search wrapped to bottom."));
1360 mutt_message _("Search hit top without finding match");
1366 h = Context->hdrs[Context->v2r[i]];
1368 /* if we've already evaulated this message, use the cached value */
1373 /* remember that we've already searched this message */
1377 (SearchPattern, M_MATCH_FULL_ADDRESS, Context, h) > 0)))
1382 mutt_error _("Search interrupted.");
1391 mutt_error _("Not found.");