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.
31 #include "imap/imap.h"
41 #include "mutt_crypt.h"
43 static int eat_regexp (pattern_t * pat, BUFFER *, BUFFER *);
44 static int eat_date (pattern_t * pat, BUFFER *, BUFFER *);
45 static int eat_range (pattern_t * pat, BUFFER *, BUFFER *);
46 static int patmatch (const pattern_t* pat, const char* buf);
48 struct pattern_flags {
49 int tag; /* character used to represent this op */
50 int op; /* operation to perform */
52 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},
94 'w', M_NEWSGROUPS, 0, eat_regexp},
97 'x', M_REFERENCE, 0, eat_regexp}, {
98 'X', M_MIMEATTACH, 0, eat_range}, {
99 'y', M_XLABEL, 0, eat_regexp}, {
100 'z', M_SIZE, 0, eat_range}, {
101 '=', M_DUPLICATED, 0, NULL}, {
102 '$', M_UNREFERENCED, 0, NULL}, {
103 '*', M_REALNAME, 0, NULL}, {
107 static pattern_t *SearchPattern = NULL; /* current search pattern */
108 static char LastSearch[STRING] = { 0 }; /* last pattern searched for */
109 static char LastSearchExpn[LONG_STRING] = { 0 }; /* expanded version of
112 #define M_MAXRANGE -1
114 /* constants for parse_date_range() */
115 #define M_PDR_NONE 0x0000
116 #define M_PDR_MINUS 0x0001
117 #define M_PDR_PLUS 0x0002
118 #define M_PDR_WINDOW 0x0004
119 #define M_PDR_ABSOLUTE 0x0008
120 #define M_PDR_DONE 0x0010
121 #define M_PDR_ERROR 0x0100
122 #define M_PDR_ERRORDONE (M_PDR_ERROR | M_PDR_DONE)
125 int mutt_getvaluebychar (char ch, struct mapping_t *table)
129 for (i = 0; table[i].name; i++) {
130 if (ch == table[i].name[0])
131 return table[i].value;
137 /* if no uppercase letters are given, do a case-insensitive search */
138 int mutt_which_case (const char *s)
141 if (isalpha ((unsigned char) *s) && isupper ((unsigned char) *s))
142 return 0; /* case-sensitive */
145 return REG_ICASE; /* case-insensitive */
149 msg_search (CONTEXT *ctx, pattern_t* pat, int msgno)
151 char tempfile[_POSIX_PATH_MAX];
158 HEADER *h = ctx->hdrs[msgno];
162 if ((msg = mx_open_message (ctx, msgno)) != NULL) {
163 if (option (OPTTHOROUGHSRC)) {
164 /* decode the header / body */
165 memset (&s, 0, sizeof (s));
167 s.flags = M_CHARCONV;
168 mutt_mktemp (tempfile);
169 if ((s.fpout = safe_fopen (tempfile, "w+")) == NULL) {
170 mutt_perror (tempfile);
174 if (pat->op != M_BODY)
175 mutt_copy_header (msg->fp, h, s.fpout, CH_FROM | CH_DECODE, NULL);
177 if (pat->op != M_HEADER) {
178 mutt_parse_mime_message (ctx, h);
180 if (WithCrypto && (h->security & ENCRYPT)
181 && !crypt_valid_passphrase (h->security)) {
182 mx_close_message (&msg);
190 fseeko (msg->fp, h->offset, 0);
191 mutt_body_handler (h->content, &s);
197 fstat (fileno (fp), &st);
198 lng = (long) st.st_size;
201 /* raw header / body */
203 if (pat->op != M_BODY) {
204 fseeko (fp, h->offset, 0);
205 lng = h->content->offset - h->offset;
207 if (pat->op != M_HEADER) {
208 if (pat->op == M_BODY)
209 fseeko (fp, h->content->offset, 0);
210 lng += h->content->length;
215 buf = mem_malloc (blen);
217 /* search the file "fp" */
219 if (pat->op == M_HEADER) {
220 if (*(buf = mutt_read_rfc822_line (fp, buf, &blen)) == '\0')
222 } else if (fgets (buf, blen - 1, fp) == NULL)
223 break; /* don't loop forever */
224 if (patmatch (pat, buf) == 0) {
228 lng -= str_len (buf);
233 mx_close_message (&msg);
235 if (option (OPTTHOROUGHSRC)) {
244 int eat_regexp (pattern_t * pat, BUFFER * s, BUFFER * err)
249 memset (&buf, 0, sizeof (buf));
251 if (mutt_extract_token (&buf, s, M_TOKEN_PATTERN | M_TOKEN_COMMENT) != 0 ||
253 snprintf (err->data, err->dsize, _("Error in expression: %s"), s->dptr);
258 snprintf (err->data, err->dsize, _("Empty expression"));
263 /* If there are no RE metacharacters, use simple search anyway */
264 if (!pat->stringmatch && !strpbrk (buf.data, "|[{.*+?^$"))
265 pat->stringmatch = 1;
268 if (pat->stringmatch) {
269 pat->str = str_dup (buf.data);
270 mem_free (&buf.data);
272 pat->rx = mem_malloc (sizeof (regex_t));
273 r = REGCOMP (pat->rx, buf.data, REG_NEWLINE | REG_NOSUB | mutt_which_case (buf.data));
274 mem_free (&buf.data);
276 regerror (r, pat->rx, err->data, err->dsize);
285 static int patmatch (const pattern_t* pat, const char* buf) {
286 if (pat->stringmatch)
287 return !strstr (buf, pat->str);
289 return regexec (pat->rx, buf, 0, NULL, 0);
292 int eat_range (pattern_t * pat, BUFFER * s, BUFFER * err)
295 int do_exclusive = 0;
299 * If simple_search is set to "~m %s", the range will have double quotes
302 if (*s->dptr == '"') {
308 if ((*s->dptr != '-') && (*s->dptr != '<')) {
310 if (*s->dptr == '>') {
311 pat->max = M_MAXRANGE;
312 pat->min = strtol (s->dptr + 1, &tmp, 0) + 1; /* exclusive range */
315 pat->min = strtol (s->dptr, &tmp, 0);
316 if (toupper ((unsigned char) *tmp) == 'K') { /* is there a prefix? */
320 else if (toupper ((unsigned char) *tmp) == 'M') {
324 if (*s->dptr == '>') {
341 if (isdigit ((unsigned char) *tmp)) {
343 pat->max = strtol (tmp, &tmp, 0);
344 if (toupper ((unsigned char) *tmp) == 'K') {
348 else if (toupper ((unsigned char) *tmp) == 'M') {
356 pat->max = M_MAXRANGE;
358 if (skip_quote && *tmp == '"')
366 static const char *getDate (const char *s, struct tm *t, BUFFER * err)
369 time_t now = time (NULL);
370 struct tm *tm = localtime (&now);
372 t->tm_mday = strtol (s, &p, 10);
373 if (t->tm_mday < 1 || t->tm_mday > 31) {
374 snprintf (err->data, err->dsize, _("Invalid day of month: %s"), s);
378 /* fill in today's month and year */
379 t->tm_mon = tm->tm_mon;
380 t->tm_year = tm->tm_year;
384 t->tm_mon = strtol (p, &p, 10) - 1;
385 if (t->tm_mon < 0 || t->tm_mon > 11) {
386 snprintf (err->data, err->dsize, _("Invalid month: %s"), p);
390 t->tm_year = tm->tm_year;
394 t->tm_year = strtol (p, &p, 10);
395 if (t->tm_year < 70) /* year 2000+ */
397 else if (t->tm_year > 1900)
406 static const char *get_offset (struct tm *tm, const char *s, int sign)
409 int offset = strtol (s, &ps, 0);
411 if ((sign < 0 && offset > 0) || (sign > 0 && offset < 0))
416 tm->tm_year += offset;
419 tm->tm_mon += offset;
422 tm->tm_mday += 7 * offset;
425 tm->tm_mday += offset;
430 mutt_normalize_time (tm);
434 static void adjust_date_range (struct tm *min, struct tm *max)
436 if (min->tm_year > max->tm_year
437 || (min->tm_year == max->tm_year && min->tm_mon > max->tm_mon)
438 || (min->tm_year == max->tm_year && min->tm_mon == max->tm_mon
439 && min->tm_mday > max->tm_mday)) {
443 min->tm_year = max->tm_year;
447 min->tm_mon = max->tm_mon;
451 min->tm_mday = max->tm_mday;
454 min->tm_hour = min->tm_min = min->tm_sec = 0;
456 max->tm_min = max->tm_sec = 59;
460 static const char *parse_date_range (const char *pc, struct tm *min,
461 struct tm *max, int haveMin,
462 struct tm *baseMin, BUFFER * err)
464 int flag = M_PDR_NONE;
466 while (*pc && ((flag & M_PDR_DONE) == 0)) {
474 /* try a range of absolute date minus offset of Ndwmy */
475 pt = get_offset (min, pc, -1);
477 if (flag == M_PDR_NONE) { /* nothing yet and no offset parsed => absolute date? */
478 if (!getDate (pc, max, err))
479 flag |= (M_PDR_ABSOLUTE | M_PDR_ERRORDONE); /* done bad */
481 /* reestablish initial base minimum if not specified */
483 memcpy (min, baseMin, sizeof (struct tm));
484 flag |= (M_PDR_ABSOLUTE | M_PDR_DONE); /* done good */
488 flag |= M_PDR_ERRORDONE;
492 if (flag == M_PDR_NONE && !haveMin) { /* the very first "-3d" without a previous absolute date */
493 max->tm_year = min->tm_year;
494 max->tm_mon = min->tm_mon;
495 max->tm_mday = min->tm_mday;
502 { /* enlarge plusRange */
503 pt = get_offset (max, pc, 1);
505 flag |= M_PDR_ERRORDONE;
513 { /* enlarge window in both directions */
514 pt = get_offset (min, pc, -1);
516 flag |= M_PDR_ERRORDONE;
518 pc = get_offset (max, pc, 1);
519 flag |= M_PDR_WINDOW;
524 flag |= M_PDR_ERRORDONE;
528 if ((flag & M_PDR_ERROR) && !(flag & M_PDR_ABSOLUTE)) { /* getDate has its own error message, don't overwrite it here */
529 snprintf (err->data, err->dsize, _("Invalid relative date: %s"), pc - 1);
531 return ((flag & M_PDR_ERROR) ? NULL : pc);
534 static int eat_date (pattern_t * pat, BUFFER * s, BUFFER * err)
539 memset (&buffer, 0, sizeof (buffer));
540 if (mutt_extract_token (&buffer, s, M_TOKEN_COMMENT | M_TOKEN_PATTERN) != 0
542 strfcpy (err->data, _("error in expression"), err->dsize);
546 memset (&min, 0, sizeof (min));
547 /* the `0' time is Jan 1, 1970 UTC, so in order to prevent a negative time
548 when doing timezone conversion, we use Jan 2, 1970 UTC as the base
553 memset (&max, 0, sizeof (max));
555 /* Arbitrary year in the future. Don't set this too high
556 or mutt_mktime() returns something larger than will
557 fit in a time_t on some systems */
565 if (strchr ("<>=", buffer.data[0])) {
566 /* offset from current time
567 <3d less than three days ago
568 >3d more than three days ago
569 =3d exactly three days ago */
570 time_t now = time (NULL);
571 struct tm *tm = localtime (&now);
574 if (buffer.data[0] == '<') {
575 memcpy (&min, tm, sizeof (min));
579 memcpy (&max, tm, sizeof (max));
582 if (buffer.data[0] == '=')
586 tm->tm_min = tm->tm_sec = 59;
588 /* force negative offset */
589 get_offset (tm, buffer.data + 1, -1);
592 /* start at the beginning of the day in question */
593 memcpy (&min, &max, sizeof (max));
594 min.tm_hour = min.tm_sec = min.tm_min = 0;
598 const char *pc = buffer.data;
601 int untilNow = FALSE;
603 if (isdigit ((unsigned char) *pc)) {
604 /* mininum date specified */
605 if ((pc = getDate (pc, &min, err)) == NULL) {
606 mem_free (&buffer.data);
612 const char *pt = pc + 1;
615 untilNow = (*pt == '\0');
619 if (!untilNow) { /* max date or relative range/window */
623 if (!haveMin) { /* save base minimum and set current date, e.g. for "-3d+1d" */
624 time_t now = time (NULL);
625 struct tm *tm = localtime (&now);
627 memcpy (&baseMin, &min, sizeof (baseMin));
628 memcpy (&min, tm, sizeof (min));
629 min.tm_hour = min.tm_sec = min.tm_min = 0;
632 /* preset max date for relative offsets,
633 if nothing follows we search for messages on a specific day */
634 max.tm_year = min.tm_year;
635 max.tm_mon = min.tm_mon;
636 max.tm_mday = min.tm_mday;
638 if (!parse_date_range (pc, &min, &max, haveMin, &baseMin, err)) { /* bail out on any parsing error */
639 mem_free (&buffer.data);
645 /* Since we allow two dates to be specified we'll have to adjust that. */
646 adjust_date_range (&min, &max);
648 pat->min = mutt_mktime (&min, 1);
649 pat->max = mutt_mktime (&max, 1);
651 mem_free (&buffer.data);
656 static struct pattern_flags *lookup_tag (char tag)
660 for (i = 0; Flags[i].tag; i++)
661 if (Flags[i].tag == tag)
666 static /* const */ char *find_matching_paren ( /* const */ char *s)
673 else if (*s == ')') {
682 void mutt_pattern_free (pattern_t ** pat)
694 mem_free (&tmp->str);
696 mutt_pattern_free (&tmp->child);
701 pattern_t *mutt_pattern_comp ( /* const */ char *s, int flags, BUFFER * err)
703 pattern_t *curlist = NULL;
705 pattern_t *last = NULL;
709 int implicit = 1; /* used to detect logical AND operator */
710 struct pattern_flags *entry;
715 memset (&ps, 0, sizeof (ps));
717 ps.dsize = str_len (s);
733 snprintf (err->data, err->dsize, _("error in pattern at: %s"),
738 /* A & B | C == (A & B) | C */
739 tmp = new_pattern ();
741 tmp->child = curlist;
757 if (implicit && or) {
758 /* A | B & C == (A | B) & C */
759 tmp = new_pattern ();
761 tmp->child = curlist;
767 tmp = new_pattern ();
769 tmp->alladdr = alladdr;
770 tmp->stringmatch = (*ps.dptr == '=') ? 1 : 0;
780 ps.dptr++; /* move past the ~ */
781 if ((entry = lookup_tag (*ps.dptr)) == NULL) {
782 snprintf (err->data, err->dsize, _("%c: invalid command"), *ps.dptr);
783 mutt_pattern_free (&curlist);
786 if (entry->class && (flags & entry->class) == 0) {
787 snprintf (err->data, err->dsize, _("%c: not supported in this mode"),
789 mutt_pattern_free (&curlist);
794 ps.dptr++; /* eat the operator and any optional whitespace */
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 = str_substrdup (ps.dptr + 1, p);
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 strfcpy (err->data, _("empty pattern"), err->dsize);
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 *); 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 * a1, ADDRESS * 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 * a1, ADDRESS * 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 * a1, ADDRESS * 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));
1074 return (pat->not ^ ((h->security & SIGN) ? 1 : 0));
1075 case M_CRYPT_VERIFIED:
1078 return (pat->not ^ ((h->security & GOODSIGN) ? 1 : 0));
1079 case M_CRYPT_ENCRYPT:
1082 return (pat->not ^ ((h->security & ENCRYPT) ? 1 : 0));
1084 if (!(WithCrypto & APPLICATION_PGP))
1086 return (pat->not ^ ((h->security & APPLICATION_PGP)
1087 && (h->security & PGPKEY)));
1090 not ^ (h->env->x_label
1091 && patmatch (pat, h->env->x_label) == 0));
1094 not ^ (h->env->spam && h->env->spam->data
1095 && patmatch (pat, h->env->spam->data) == 0));
1097 return (pat->not ^ (h->thread && h->thread->duplicate_thread));
1103 if (h->content->parts)
1104 count = mutt_count_body_parts(h, 0);
1106 mutt_parse_mime_message(ctx, h);
1107 count = mutt_count_body_parts(h, 0);
1108 mutt_free_body(&h->content->parts);
1111 return (pat->not ^ (count >= pat->min && (pat->max == M_MAXRANGE ||
1112 count <= pat->max)));
1115 case M_UNREFERENCED:
1116 return (pat->not ^ (h->thread && !h->thread->child));
1118 return (pat->not ^ (h->content && h->content->type == TYPEMULTIPART));
1121 * we have a match if
1122 * - From: matches $alternates
1123 * - or we have an alias for current address
1124 * - or From: contains valid email address _and_ name has >= 2 fields
1127 not ^ (h->env && h->env->from && (mutt_addr_is_user (h->env->from)
1129 (alias_reverse_lookup
1130 (h->env->from) != NULL)
1131 || (h->env->from->personal
1132 && valid_realname (h->env->
1135 && h->env->from->mailbox)
1140 not ^ (h->env->newsgroups
1141 && patmatch (pat, h->env->newsgroups) == 0));
1144 mutt_error (_("error: unknown op %d (report this error)."), pat->op);
1148 static void quote_simple (char *tmp, size_t len, const char *p)
1153 while (*p && i < len - 3) {
1154 if (*p == '\\' || *p == '"')
1162 /* convert a simple search into a real request */
1163 void mutt_check_simple (char *s, size_t len, const char *simple)
1165 char tmp[LONG_STRING];
1167 /* XXX - is ascii_strcasecmp() right here, or should we use locale's
1171 if (!strchr (s, '~') && !strchr (s, '=')) { /* yup, so spoof a real request */
1172 /* convert old tokens into the new format */
1173 if (ascii_strcasecmp ("all", s) == 0 || !str_cmp ("^", s) || !str_cmp (".", s)) /* ~A is more efficient */
1174 strfcpy (s, "~A", len);
1175 else if (ascii_strcasecmp ("del", s) == 0)
1176 strfcpy (s, "~D", len);
1177 else if (ascii_strcasecmp ("flag", s) == 0)
1178 strfcpy (s, "~F", len);
1179 else if (ascii_strcasecmp ("new", s) == 0)
1180 strfcpy (s, "~N", len);
1181 else if (ascii_strcasecmp ("old", s) == 0)
1182 strfcpy (s, "~O", len);
1183 else if (ascii_strcasecmp ("repl", s) == 0)
1184 strfcpy (s, "~Q", len);
1185 else if (ascii_strcasecmp ("read", s) == 0)
1186 strfcpy (s, "~R", len);
1187 else if (ascii_strcasecmp ("tag", s) == 0)
1188 strfcpy (s, "~T", len);
1189 else if (ascii_strcasecmp ("unread", s) == 0)
1190 strfcpy (s, "~U", len);
1192 quote_simple (tmp, sizeof (tmp), s);
1193 mutt_expand_fmt (s, len, simple, tmp);
1198 int mutt_pattern_func (int op, char *prompt)
1201 char buf[LONG_STRING] = "", *simple, error[STRING];
1205 strfcpy (buf, NONULL (Context->pattern), sizeof (buf));
1206 if (prompt || op != M_LIMIT)
1207 if (mutt_get_field (prompt, buf, sizeof (buf), M_PATTERN | M_CLEAR) != 0 || !buf[0])
1210 mutt_message _("Compiling search pattern...");
1212 simple = str_dup (buf);
1213 mutt_check_simple (buf, sizeof (buf), NONULL (SimpleSearch));
1216 err.dsize = sizeof (error);
1217 if ((pat = mutt_pattern_comp (buf, M_FULL_MSG, &err)) == NULL) {
1219 mutt_error ("%s", err.data);
1224 if (Context->magic == M_IMAP && imap_search (Context, pat) < 0)
1228 mutt_message _("Executing command on matching messages...");
1230 #define THIS_BODY Context->hdrs[i]->content
1232 if (op == M_LIMIT) {
1233 Context->vcount = 0;
1235 Context->collapsed = 0;
1237 for (i = 0; i < Context->msgcount; i++) {
1238 /* new limit pattern implicitly uncollapses all threads */
1239 Context->hdrs[i]->virtual = -1;
1240 Context->hdrs[i]->limited = 0;
1241 Context->hdrs[i]->collapsed = 0;
1242 Context->hdrs[i]->num_hidden = 0;
1243 if (mutt_pattern_exec
1244 (pat, M_MATCH_FULL_ADDRESS, Context, Context->hdrs[i])) {
1245 Context->hdrs[i]->virtual = Context->vcount;
1246 Context->hdrs[i]->limited = 1;
1247 Context->v2r[Context->vcount] = i;
1249 Context->vsize += THIS_BODY->length + THIS_BODY->offset -
1250 THIS_BODY->hdr_offset;
1255 for (i = 0; i < Context->vcount; i++) {
1256 if (mutt_pattern_exec
1257 (pat, M_MATCH_FULL_ADDRESS, Context,
1258 Context->hdrs[Context->v2r[i]])) {
1261 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_PURGED,
1264 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_DELETE,
1269 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_TAG,
1279 mutt_clear_error ();
1281 if (op == M_LIMIT) {
1282 /* drop previous limit pattern */
1283 mem_free (&Context->pattern);
1284 if (Context->limit_pattern)
1285 mutt_pattern_free (&Context->limit_pattern);
1286 if (Context->msgcount && !Context->vcount) {
1287 mutt_error _("No messages matched criteria.");
1290 /* record new limit pattern, unless match all */
1291 if (str_ncmp (buf, "~A", 2) != 0) {
1292 Context->pattern = simple;
1293 simple = NULL; /* don't clobber it */
1294 Context->limit_pattern = mutt_pattern_comp (buf, M_FULL_MSG, &err);
1298 mutt_pattern_free (&pat);
1302 int mutt_search_command (int cur, int op)
1306 char temp[LONG_STRING];
1312 if (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE) {
1313 strfcpy (buf, LastSearch, sizeof (buf));
1314 if (mutt_get_field ((op == OP_SEARCH) ? _("Search for: ") :
1315 _("Reverse search for: "), buf, sizeof (buf),
1316 M_CLEAR | M_PATTERN) != 0 || !buf[0])
1319 if (op == OP_SEARCH)
1320 unset_option (OPTSEARCHREVERSE);
1322 set_option (OPTSEARCHREVERSE);
1324 /* compare the *expanded* version of the search pattern in case
1325 $simple_search has changed while we were searching */
1326 strfcpy (temp, buf, sizeof (temp));
1327 mutt_check_simple (temp, sizeof (temp), NONULL (SimpleSearch));
1329 if (!SearchPattern || str_cmp (temp, LastSearchExpn)) {
1330 set_option (OPTSEARCHINVALID);
1331 strfcpy (LastSearch, buf, sizeof (LastSearch));
1332 mutt_message _("Compiling search pattern...");
1334 mutt_pattern_free (&SearchPattern);
1336 err.dsize = sizeof (error);
1337 if ((SearchPattern =
1338 mutt_pattern_comp (temp, M_FULL_MSG, &err)) == NULL) {
1339 mutt_error ("%s", error);
1342 mutt_clear_error ();
1345 else if (!SearchPattern) {
1346 mutt_error _("No search pattern.");
1351 if (option (OPTSEARCHINVALID)) {
1352 for (i = 0; i < Context->msgcount; i++)
1353 Context->hdrs[i]->searched = 0;
1355 if (Context->magic == M_IMAP && imap_search (Context, SearchPattern) < 0)
1358 unset_option (OPTSEARCHINVALID);
1361 incr = (option (OPTSEARCHREVERSE)) ? -1 : 1;
1362 if (op == OP_SEARCH_OPPOSITE)
1365 for (i = cur + incr, j = 0; j != Context->vcount; j++) {
1366 if (i > Context->vcount - 1) {
1368 if (option (OPTWRAPSEARCH))
1369 mutt_message (_("Search wrapped to top."));
1372 mutt_message _("Search hit bottom without finding match");
1378 i = Context->vcount - 1;
1379 if (option (OPTWRAPSEARCH))
1380 mutt_message (_("Search wrapped to bottom."));
1383 mutt_message _("Search hit top without finding match");
1389 h = Context->hdrs[Context->v2r[i]];
1391 /* if we've already evaulated this message, use the cached value */
1396 /* remember that we've already searched this message */
1400 (SearchPattern, M_MATCH_FULL_ADDRESS, Context, h) > 0)))
1405 mutt_error _("Search interrupted.");
1414 mutt_error _("Not found.");