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 'y', M_XLABEL, 0, eat_regexp}, {
99 'z', M_SIZE, 0, eat_range}, {
100 '=', M_DUPLICATED, 0, NULL}, {
101 '$', M_UNREFERENCED, 0, NULL}, {
102 '*', M_REALNAME, 0, NULL}, {
106 static pattern_t *SearchPattern = NULL; /* current search pattern */
107 static char LastSearch[STRING] = { 0 }; /* last pattern searched for */
108 static char LastSearchExpn[LONG_STRING] = { 0 }; /* expanded version of
111 #define M_MAXRANGE -1
113 /* constants for parse_date_range() */
114 #define M_PDR_NONE 0x0000
115 #define M_PDR_MINUS 0x0001
116 #define M_PDR_PLUS 0x0002
117 #define M_PDR_WINDOW 0x0004
118 #define M_PDR_ABSOLUTE 0x0008
119 #define M_PDR_DONE 0x0010
120 #define M_PDR_ERROR 0x0100
121 #define M_PDR_ERRORDONE (M_PDR_ERROR | M_PDR_DONE)
124 int mutt_getvaluebychar (char ch, struct mapping_t *table)
128 for (i = 0; table[i].name; i++) {
129 if (ch == table[i].name[0])
130 return table[i].value;
136 /* if no uppercase letters are given, do a case-insensitive search */
137 int mutt_which_case (const char *s)
140 if (isalpha ((unsigned char) *s) && isupper ((unsigned char) *s))
141 return 0; /* case-sensitive */
144 return REG_ICASE; /* case-insensitive */
148 msg_search (CONTEXT *ctx, pattern_t* pat, int msgno)
150 char tempfile[_POSIX_PATH_MAX];
157 HEADER *h = ctx->hdrs[msgno];
161 if ((msg = mx_open_message (ctx, msgno)) != NULL) {
162 if (option (OPTTHOROUGHSRC)) {
163 /* decode the header / body */
164 memset (&s, 0, sizeof (s));
166 s.flags = M_CHARCONV;
167 mutt_mktemp (tempfile);
168 if ((s.fpout = safe_fopen (tempfile, "w+")) == NULL) {
169 mutt_perror (tempfile);
173 if (pat->op != M_BODY)
174 mutt_copy_header (msg->fp, h, s.fpout, CH_FROM | CH_DECODE, NULL);
176 if (pat->op != M_HEADER) {
177 mutt_parse_mime_message (ctx, h);
179 if (WithCrypto && (h->security & ENCRYPT)
180 && !crypt_valid_passphrase (h->security)) {
181 mx_close_message (&msg);
189 fseek (msg->fp, h->offset, 0);
190 mutt_body_handler (h->content, &s);
196 fstat (fileno (fp), &st);
197 lng = (long) st.st_size;
200 /* raw header / body */
202 if (pat->op != M_BODY) {
203 fseek (fp, h->offset, 0);
204 lng = h->content->offset - h->offset;
206 if (pat->op != M_HEADER) {
207 if (pat->op == M_BODY)
208 fseek (fp, h->content->offset, 0);
209 lng += h->content->length;
214 buf = mem_malloc (blen);
216 /* search the file "fp" */
218 if (pat->op == M_HEADER) {
219 if (*(buf = mutt_read_rfc822_line (fp, buf, &blen)) == '\0')
221 } else if (fgets (buf, blen - 1, fp) == NULL)
222 break; /* don't loop forever */
223 if (patmatch (pat, buf) == 0) {
227 lng -= str_len (buf);
232 mx_close_message (&msg);
234 if (option (OPTTHOROUGHSRC)) {
243 int eat_regexp (pattern_t * pat, BUFFER * s, BUFFER * err)
248 memset (&buf, 0, sizeof (buf));
249 if (mutt_extract_token (&buf, s, M_TOKEN_PATTERN | M_TOKEN_COMMENT) != 0 ||
251 snprintf (err->data, err->dsize, _("Error in expression: %s"), s->dptr);
256 /* If there are no RE metacharacters, use simple search anyway */
257 if (!pat->stringmatch && !strpbrk (buf.data, "|[{.*+?^$"))
258 pat->stringmatch = 1;
261 if (pat->stringmatch) {
262 pat->str = str_dup (buf.data);
263 mem_free (&buf.data);
265 pat->rx = mem_malloc (sizeof (regex_t));
266 r = REGCOMP (pat->rx, buf.data, REG_NEWLINE | REG_NOSUB | mutt_which_case (buf.data));
267 mem_free (&buf.data);
269 regerror (r, pat->rx, err->data, err->dsize);
278 static int patmatch (const pattern_t* pat, const char* buf) {
279 if (pat->stringmatch)
280 return !strstr (buf, pat->str);
282 return regexec (pat->rx, buf, 0, NULL, 0);
285 int eat_range (pattern_t * pat, BUFFER * s, BUFFER * err)
288 int do_exclusive = 0;
292 * If simple_search is set to "~m %s", the range will have double quotes
295 if (*s->dptr == '"') {
301 if ((*s->dptr != '-') && (*s->dptr != '<')) {
303 if (*s->dptr == '>') {
304 pat->max = M_MAXRANGE;
305 pat->min = strtol (s->dptr + 1, &tmp, 0) + 1; /* exclusive range */
308 pat->min = strtol (s->dptr, &tmp, 0);
309 if (toupper ((unsigned char) *tmp) == 'K') { /* is there a prefix? */
313 else if (toupper ((unsigned char) *tmp) == 'M') {
317 if (*s->dptr == '>') {
334 if (isdigit ((unsigned char) *tmp)) {
336 pat->max = strtol (tmp, &tmp, 0);
337 if (toupper ((unsigned char) *tmp) == 'K') {
341 else if (toupper ((unsigned char) *tmp) == 'M') {
349 pat->max = M_MAXRANGE;
351 if (skip_quote && *tmp == '"')
359 static const char *getDate (const char *s, struct tm *t, BUFFER * err)
362 time_t now = time (NULL);
363 struct tm *tm = localtime (&now);
365 t->tm_mday = strtol (s, &p, 10);
366 if (t->tm_mday < 1 || t->tm_mday > 31) {
367 snprintf (err->data, err->dsize, _("Invalid day of month: %s"), s);
371 /* fill in today's month and year */
372 t->tm_mon = tm->tm_mon;
373 t->tm_year = tm->tm_year;
377 t->tm_mon = strtol (p, &p, 10) - 1;
378 if (t->tm_mon < 0 || t->tm_mon > 11) {
379 snprintf (err->data, err->dsize, _("Invalid month: %s"), p);
383 t->tm_year = tm->tm_year;
387 t->tm_year = strtol (p, &p, 10);
388 if (t->tm_year < 70) /* year 2000+ */
390 else if (t->tm_year > 1900)
399 static const char *get_offset (struct tm *tm, const char *s, int sign)
402 int offset = strtol (s, &ps, 0);
404 if ((sign < 0 && offset > 0) || (sign > 0 && offset < 0))
409 tm->tm_year += offset;
412 tm->tm_mon += offset;
415 tm->tm_mday += 7 * offset;
418 tm->tm_mday += offset;
423 mutt_normalize_time (tm);
427 static void adjust_date_range (struct tm *min, struct tm *max)
429 if (min->tm_year > max->tm_year
430 || (min->tm_year == max->tm_year && min->tm_mon > max->tm_mon)
431 || (min->tm_year == max->tm_year && min->tm_mon == max->tm_mon
432 && min->tm_mday > max->tm_mday)) {
436 min->tm_year = max->tm_year;
440 min->tm_mon = max->tm_mon;
444 min->tm_mday = max->tm_mday;
447 min->tm_hour = min->tm_min = min->tm_sec = 0;
449 max->tm_min = max->tm_sec = 59;
453 static const char *parse_date_range (const char *pc, struct tm *min,
454 struct tm *max, int haveMin,
455 struct tm *baseMin, BUFFER * err)
457 int flag = M_PDR_NONE;
459 while (*pc && ((flag & M_PDR_DONE) == 0)) {
467 /* try a range of absolute date minus offset of Ndwmy */
468 pt = get_offset (min, pc, -1);
470 if (flag == M_PDR_NONE) { /* nothing yet and no offset parsed => absolute date? */
471 if (!getDate (pc, max, err))
472 flag |= (M_PDR_ABSOLUTE | M_PDR_ERRORDONE); /* done bad */
474 /* reestablish initial base minimum if not specified */
476 memcpy (min, baseMin, sizeof (struct tm));
477 flag |= (M_PDR_ABSOLUTE | M_PDR_DONE); /* done good */
481 flag |= M_PDR_ERRORDONE;
485 if (flag == M_PDR_NONE && !haveMin) { /* the very first "-3d" without a previous absolute date */
486 max->tm_year = min->tm_year;
487 max->tm_mon = min->tm_mon;
488 max->tm_mday = min->tm_mday;
495 { /* enlarge plusRange */
496 pt = get_offset (max, pc, 1);
498 flag |= M_PDR_ERRORDONE;
506 { /* enlarge window in both directions */
507 pt = get_offset (min, pc, -1);
509 flag |= M_PDR_ERRORDONE;
511 pc = get_offset (max, pc, 1);
512 flag |= M_PDR_WINDOW;
517 flag |= M_PDR_ERRORDONE;
521 if ((flag & M_PDR_ERROR) && !(flag & M_PDR_ABSOLUTE)) { /* getDate has its own error message, don't overwrite it here */
522 snprintf (err->data, err->dsize, _("Invalid relative date: %s"), pc - 1);
524 return ((flag & M_PDR_ERROR) ? NULL : pc);
527 static int eat_date (pattern_t * pat, BUFFER * s, BUFFER * err)
532 memset (&buffer, 0, sizeof (buffer));
533 if (mutt_extract_token (&buffer, s, M_TOKEN_COMMENT | M_TOKEN_PATTERN) != 0
535 strfcpy (err->data, _("error in expression"), err->dsize);
539 memset (&min, 0, sizeof (min));
540 /* the `0' time is Jan 1, 1970 UTC, so in order to prevent a negative time
541 when doing timezone conversion, we use Jan 2, 1970 UTC as the base
546 memset (&max, 0, sizeof (max));
548 /* Arbitrary year in the future. Don't set this too high
549 or mutt_mktime() returns something larger than will
550 fit in a time_t on some systems */
558 if (strchr ("<>=", buffer.data[0])) {
559 /* offset from current time
560 <3d less than three days ago
561 >3d more than three days ago
562 =3d exactly three days ago */
563 time_t now = time (NULL);
564 struct tm *tm = localtime (&now);
567 if (buffer.data[0] == '<') {
568 memcpy (&min, tm, sizeof (min));
572 memcpy (&max, tm, sizeof (max));
575 if (buffer.data[0] == '=')
579 tm->tm_min = tm->tm_sec = 59;
581 /* force negative offset */
582 get_offset (tm, buffer.data + 1, -1);
585 /* start at the beginning of the day in question */
586 memcpy (&min, &max, sizeof (max));
587 min.tm_hour = min.tm_sec = min.tm_min = 0;
591 const char *pc = buffer.data;
594 int untilNow = FALSE;
596 if (isdigit ((unsigned char) *pc)) {
597 /* mininum date specified */
598 if ((pc = getDate (pc, &min, err)) == NULL) {
599 mem_free (&buffer.data);
605 const char *pt = pc + 1;
608 untilNow = (*pt == '\0');
612 if (!untilNow) { /* max date or relative range/window */
616 if (!haveMin) { /* save base minimum and set current date, e.g. for "-3d+1d" */
617 time_t now = time (NULL);
618 struct tm *tm = localtime (&now);
620 memcpy (&baseMin, &min, sizeof (baseMin));
621 memcpy (&min, tm, sizeof (min));
622 min.tm_hour = min.tm_sec = min.tm_min = 0;
625 /* preset max date for relative offsets,
626 if nothing follows we search for messages on a specific day */
627 max.tm_year = min.tm_year;
628 max.tm_mon = min.tm_mon;
629 max.tm_mday = min.tm_mday;
631 if (!parse_date_range (pc, &min, &max, haveMin, &baseMin, err)) { /* bail out on any parsing error */
632 mem_free (&buffer.data);
638 /* Since we allow two dates to be specified we'll have to adjust that. */
639 adjust_date_range (&min, &max);
641 pat->min = mutt_mktime (&min, 1);
642 pat->max = mutt_mktime (&max, 1);
644 mem_free (&buffer.data);
649 static struct pattern_flags *lookup_tag (char tag)
653 for (i = 0; Flags[i].tag; i++)
654 if (Flags[i].tag == tag)
659 static /* const */ char *find_matching_paren ( /* const */ char *s)
666 else if (*s == ')') {
675 void mutt_pattern_free (pattern_t ** pat)
687 mem_free (&tmp->str);
689 mutt_pattern_free (&tmp->child);
694 pattern_t *mutt_pattern_comp ( /* const */ char *s, int flags, BUFFER * err)
696 pattern_t *curlist = NULL;
698 pattern_t *last = NULL;
702 int implicit = 1; /* used to detect logical AND operator */
703 struct pattern_flags *entry;
708 memset (&ps, 0, sizeof (ps));
710 ps.dsize = str_len (s);
726 snprintf (err->data, err->dsize, _("error in pattern at: %s"),
731 /* A & B | C == (A & B) | C */
732 tmp = new_pattern ();
734 tmp->child = curlist;
750 if (implicit && or) {
751 /* A | B & C == (A | B) & C */
752 tmp = new_pattern ();
754 tmp->child = curlist;
760 tmp = new_pattern ();
762 tmp->alladdr = alladdr;
763 tmp->stringmatch = (*ps.dptr == '=') ? 1 : 0;
773 ps.dptr++; /* move past the ~ */
774 if ((entry = lookup_tag (*ps.dptr)) == NULL) {
775 snprintf (err->data, err->dsize, _("%c: invalid command"), *ps.dptr);
776 mutt_pattern_free (&curlist);
779 if (entry->class && (flags & entry->class) == 0) {
780 snprintf (err->data, err->dsize, _("%c: not supported in this mode"),
782 mutt_pattern_free (&curlist);
787 ps.dptr++; /* eat the operator and any optional whitespace */
790 if (entry->eat_arg) {
792 snprintf (err->data, err->dsize, _("missing parameter"));
793 mutt_pattern_free (&curlist);
796 if (entry->eat_arg (tmp, &ps, err) == -1) {
797 mutt_pattern_free (&curlist);
804 p = find_matching_paren (ps.dptr + 1);
806 snprintf (err->data, err->dsize, _("mismatched parenthesis: %s"),
808 mutt_pattern_free (&curlist);
811 /* compile the sub-expression */
812 buf = str_substrdup (ps.dptr + 1, p);
813 if ((tmp = mutt_pattern_comp (buf, flags, err)) == NULL) {
815 mutt_pattern_free (&curlist);
825 tmp->alladdr |= alladdr;
828 ps.dptr = p + 1; /* restore location */
831 snprintf (err->data, err->dsize, _("error in pattern at: %s"), ps.dptr);
832 mutt_pattern_free (&curlist);
837 strfcpy (err->data, _("empty pattern"), err->dsize);
841 tmp = new_pattern ();
842 tmp->op = or ? M_OR : M_AND;
843 tmp->child = curlist;
850 perform_and (pattern_t * pat, pattern_exec_flag flags, CONTEXT * ctx,
853 for (; pat; pat = pat->next)
854 if (mutt_pattern_exec (pat, flags, ctx, hdr) <= 0)
860 perform_or (struct pattern_t *pat, pattern_exec_flag flags, CONTEXT * ctx,
863 for (; pat; pat = pat->next)
864 if (mutt_pattern_exec (pat, flags, ctx, hdr) > 0)
869 static int match_adrlist (pattern_t* pat, int match_personal, int alladdr,
877 for (a = va_arg (ap, ADDRESS *); a; a = a->next) {
879 ((a->mailbox && patmatch (pat, a->mailbox) == 0) ||
880 (match_personal && a->personal &&
881 patmatch (pat, a->personal) == 0))) {
883 return (!pat->alladdr); /* Found match, or non-match if alladdr */
888 return pat->alladdr; /* No matches, or all matches if alladdr */
891 static int match_reference (pattern_t* pat, LIST * refs)
893 for (; refs; refs = refs->next)
894 if (patmatch (pat, refs->data) == 0)
899 int mutt_is_list_recipient (int alladdr, ADDRESS * a1, ADDRESS * a2)
901 for (; a1; a1 = a1->next)
902 if (alladdr ^ mutt_is_subscribed_list (a1))
904 for (; a2; a2 = a2->next)
905 if (alladdr ^ mutt_is_subscribed_list (a2))
910 int mutt_is_list_cc (int alladdr, ADDRESS * a1, ADDRESS * a2)
912 for (; a1; a1 = a1->next)
913 if (alladdr ^ mutt_is_mail_list (a1))
915 for (; a2; a2 = a2->next)
916 if (alladdr ^ mutt_is_mail_list (a2))
921 static int match_user (int alladdr, ADDRESS * a1, ADDRESS * a2)
923 for (; a1; a1 = a1->next)
924 if (alladdr ^ mutt_addr_is_user (a1))
926 for (; a2; a2 = a2->next)
927 if (alladdr ^ mutt_addr_is_user (a2))
932 /* test if name is considered a real name, i.e. consists of at least 2
933 * space-separated words of which none may end in a dot
935 static int valid_realname (const char *name)
937 const char *p = name;
944 /* skip abbr. parts of names (e.g. 'J. User') */
952 M_MATCH_FULL_ADDRESS match both personal and machine address */
954 mutt_pattern_exec (struct pattern_t *pat, pattern_exec_flag flags,
955 CONTEXT * ctx, HEADER * h)
959 return (pat->not ^ (perform_and (pat->child, flags, ctx, h) > 0));
961 return (pat->not ^ (perform_or (pat->child, flags, ctx, h) > 0));
965 return (pat->not ^ h->expired);
967 return (pat->not ^ h->superseded);
969 return (pat->not ^ h->flagged);
971 return (pat->not ^ h->tagged);
973 return (pat->not ? h->old || h->read : !(h->old || h->read));
975 return (pat->not ? h->read : !h->read);
977 return (pat->not ^ h->replied);
979 return (pat->not ? (!h->old || h->read) : (h->old && !h->read));
981 return (pat->not ^ h->read);
983 return (pat->not ^ h->deleted);
985 return (pat->not ^ (h->msgno >= pat->min - 1 && (pat->max == M_MAXRANGE ||
990 not ^ (h->date_sent >= pat->min && h->date_sent <= pat->max));
991 case M_DATE_RECEIVED:
992 return (pat->not ^ (h->received >= pat->min && h->received <= pat->max));
997 /* IMAP search sets h->matched at search compile time */
998 if (Context->magic == M_IMAP && pat->stringmatch)
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));
1067 return (pat->not ^ ((h->security & SIGN) ? 1 : 0));
1068 case M_CRYPT_VERIFIED:
1071 return (pat->not ^ ((h->security & GOODSIGN) ? 1 : 0));
1072 case M_CRYPT_ENCRYPT:
1075 return (pat->not ^ ((h->security & ENCRYPT) ? 1 : 0));
1077 if (!(WithCrypto & APPLICATION_PGP))
1079 return (pat->not ^ ((h->security & APPLICATION_PGP)
1080 && (h->security & PGPKEY)));
1083 not ^ (h->env->x_label
1084 && patmatch (pat, h->env->x_label) == 0));
1087 not ^ (h->env->spam && h->env->spam->data
1088 && patmatch (pat, h->env->spam->data) == 0));
1090 return (pat->not ^ (h->thread && h->thread->duplicate_thread));
1091 case M_UNREFERENCED:
1092 return (pat->not ^ (h->thread && !h->thread->child));
1094 return (pat->not ^ (h->content && h->content->type == TYPEMULTIPART));
1097 * we have a match if
1098 * - From: matches $alternates
1099 * - or we have an alias for current address
1100 * - or From: contains valid email address _and_ name has >= 2 fields
1103 not ^ (h->env && h->env->from && (mutt_addr_is_user (h->env->from)
1105 (alias_reverse_lookup
1106 (h->env->from) != NULL)
1107 || (h->env->from->personal
1108 && valid_realname (h->env->
1111 && h->env->from->mailbox)
1116 not ^ (h->env->newsgroups
1117 && patmatch (pat, h->env->newsgroups) == 0));
1120 mutt_error (_("error: unknown op %d (report this error)."), pat->op);
1124 static void quote_simple (char *tmp, size_t len, const char *p)
1129 while (*p && i < len - 3) {
1130 if (*p == '\\' || *p == '"')
1138 /* convert a simple search into a real request */
1139 void mutt_check_simple (char *s, size_t len, const char *simple)
1141 char tmp[LONG_STRING];
1143 /* XXX - is ascii_strcasecmp() right here, or should we use locale's
1147 if (!strchr (s, '~') && !strchr (s, '=')) { /* yup, so spoof a real request */
1148 /* convert old tokens into the new format */
1149 if (ascii_strcasecmp ("all", s) == 0 || !str_cmp ("^", s) || !str_cmp (".", s)) /* ~A is more efficient */
1150 strfcpy (s, "~A", len);
1151 else if (ascii_strcasecmp ("del", s) == 0)
1152 strfcpy (s, "~D", len);
1153 else if (ascii_strcasecmp ("flag", s) == 0)
1154 strfcpy (s, "~F", len);
1155 else if (ascii_strcasecmp ("new", s) == 0)
1156 strfcpy (s, "~N", len);
1157 else if (ascii_strcasecmp ("old", s) == 0)
1158 strfcpy (s, "~O", len);
1159 else if (ascii_strcasecmp ("repl", s) == 0)
1160 strfcpy (s, "~Q", len);
1161 else if (ascii_strcasecmp ("read", s) == 0)
1162 strfcpy (s, "~R", len);
1163 else if (ascii_strcasecmp ("tag", s) == 0)
1164 strfcpy (s, "~T", len);
1165 else if (ascii_strcasecmp ("unread", s) == 0)
1166 strfcpy (s, "~U", len);
1168 quote_simple (tmp, sizeof (tmp), s);
1169 mutt_expand_fmt (s, len, simple, tmp);
1174 int mutt_pattern_func (int op, char *prompt)
1177 char buf[LONG_STRING] = "", *simple, error[STRING];
1181 strfcpy (buf, NONULL (Context->pattern), sizeof (buf));
1182 if (prompt || op != M_LIMIT)
1183 if (mutt_get_field (prompt, buf, sizeof (buf), M_PATTERN | M_CLEAR) != 0)
1187 strncpy (buf, "~A", sizeof (buf));
1192 mutt_message _("Compiling search pattern...");
1194 simple = str_dup (buf);
1195 mutt_check_simple (buf, sizeof (buf), NONULL (SimpleSearch));
1198 err.dsize = sizeof (error);
1199 if ((pat = mutt_pattern_comp (buf, M_FULL_MSG, &err)) == NULL) {
1201 mutt_error ("%s", err.data);
1206 if (Context->magic == M_IMAP && imap_search (Context, pat) < 0)
1210 mutt_message _("Executing command on matching messages...");
1212 #define THIS_BODY Context->hdrs[i]->content
1214 if (op == M_LIMIT) {
1215 Context->vcount = 0;
1217 Context->collapsed = 0;
1219 for (i = 0; i < Context->msgcount; i++) {
1220 /* new limit pattern implicitly uncollapses all threads */
1221 Context->hdrs[i]->virtual = -1;
1222 Context->hdrs[i]->limited = 0;
1223 Context->hdrs[i]->collapsed = 0;
1224 Context->hdrs[i]->num_hidden = 0;
1225 if (mutt_pattern_exec
1226 (pat, M_MATCH_FULL_ADDRESS, Context, Context->hdrs[i])) {
1227 Context->hdrs[i]->virtual = Context->vcount;
1228 Context->hdrs[i]->limited = 1;
1229 Context->v2r[Context->vcount] = i;
1231 Context->vsize += THIS_BODY->length + THIS_BODY->offset -
1232 THIS_BODY->hdr_offset;
1237 for (i = 0; i < Context->vcount; i++) {
1238 if (mutt_pattern_exec
1239 (pat, M_MATCH_FULL_ADDRESS, Context,
1240 Context->hdrs[Context->v2r[i]])) {
1243 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_PURGED,
1246 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_DELETE,
1251 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_TAG,
1261 mutt_clear_error ();
1263 if (op == M_LIMIT) {
1264 /* drop previous limit pattern */
1265 mem_free (&Context->pattern);
1266 if (Context->limit_pattern)
1267 mutt_pattern_free (&Context->limit_pattern);
1268 if (Context->msgcount && !Context->vcount) {
1269 mutt_error _("No messages matched criteria.");
1272 /* record new limit pattern, unless match all */
1273 if (str_ncmp (buf, "~A", 2) != 0) {
1274 Context->pattern = simple;
1275 simple = NULL; /* don't clobber it */
1276 Context->limit_pattern = mutt_pattern_comp (buf, M_FULL_MSG, &err);
1280 mutt_pattern_free (&pat);
1284 int mutt_search_command (int cur, int op)
1288 char temp[LONG_STRING];
1294 if (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE) {
1295 strfcpy (buf, LastSearch, sizeof (buf));
1296 if (mutt_get_field ((op == OP_SEARCH) ? _("Search for: ") :
1297 _("Reverse search for: "), buf, sizeof (buf),
1298 M_CLEAR | M_PATTERN) != 0 || !buf[0])
1301 if (op == OP_SEARCH)
1302 unset_option (OPTSEARCHREVERSE);
1304 set_option (OPTSEARCHREVERSE);
1306 /* compare the *expanded* version of the search pattern in case
1307 $simple_search has changed while we were searching */
1308 strfcpy (temp, buf, sizeof (temp));
1309 mutt_check_simple (temp, sizeof (temp), NONULL (SimpleSearch));
1311 if (!SearchPattern || str_cmp (temp, LastSearchExpn)) {
1312 set_option (OPTSEARCHINVALID);
1313 strfcpy (LastSearch, buf, sizeof (LastSearch));
1314 mutt_message _("Compiling search pattern...");
1316 mutt_pattern_free (&SearchPattern);
1318 err.dsize = sizeof (error);
1319 if ((SearchPattern =
1320 mutt_pattern_comp (temp, M_FULL_MSG, &err)) == NULL) {
1321 mutt_error ("%s", error);
1324 mutt_clear_error ();
1327 else if (!SearchPattern) {
1328 mutt_error _("No search pattern.");
1333 if (option (OPTSEARCHINVALID)) {
1334 for (i = 0; i < Context->msgcount; i++)
1335 Context->hdrs[i]->searched = 0;
1337 if (Context->magic == M_IMAP && imap_search (Context, SearchPattern) < 0)
1340 unset_option (OPTSEARCHINVALID);
1343 incr = (option (OPTSEARCHREVERSE)) ? -1 : 1;
1344 if (op == OP_SEARCH_OPPOSITE)
1347 for (i = cur + incr, j = 0; j != Context->vcount; j++) {
1348 if (i > Context->vcount - 1) {
1350 if (option (OPTWRAPSEARCH))
1351 mutt_message (_("Search wrapped to top."));
1354 mutt_message _("Search hit bottom without finding match");
1360 i = Context->vcount - 1;
1361 if (option (OPTWRAPSEARCH))
1362 mutt_message (_("Search wrapped to bottom."));
1365 mutt_message _("Search hit top without finding match");
1371 h = Context->hdrs[Context->v2r[i]];
1373 /* if we've already evaulated this message, use the cached value */
1378 /* remember that we've already searched this message */
1382 (SearchPattern, M_MATCH_FULL_ADDRESS, Context, h) > 0)))
1387 mutt_error _("Search interrupted.");
1396 mutt_error _("Not found.");