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>
24 #include <lib-ui/enter.h>
32 #include <imap/imap.h>
41 #include <lib-crypt/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 */
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 ((h->security & ENCRYPT) && !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;
214 buf = p_new(char, blen = STRING);
216 /* search the file "fp" */
218 if (pat->op == M_HEADER) {
219 if (!mutt_read_rfc822_line(fp, &buf, &blen))
221 } else if (fgets (buf, blen - 1, fp) == NULL)
222 break; /* don't loop forever */
223 if (patmatch (pat, buf) == 0) {
227 lng -= m_strlen(buf);
232 mx_close_message (&msg);
234 if (option (OPTTHOROUGHSRC)) {
243 int eat_regexp (pattern_t * pat, BUFFER * s, BUFFER * err)
250 if (mutt_extract_token (&buf, s, M_TOKEN_PATTERN | M_TOKEN_COMMENT) != 0 ||
252 snprintf (err->data, err->dsize, _("Error in expression: %s"), s->dptr);
257 snprintf (err->data, err->dsize, _("Empty expression"));
262 /* If there are no RE metacharacters, use simple search anyway */
263 if (!pat->stringmatch && !strpbrk (buf.data, "|[{.*+?^$"))
264 pat->stringmatch = 1;
267 if (pat->stringmatch) {
268 pat->str = m_strdup(buf.data);
271 pat->rx = p_new(regex_t, 1);
272 r = REGCOMP (pat->rx, buf.data, REG_NEWLINE | REG_NOSUB | mutt_which_case (buf.data));
275 regerror (r, pat->rx, err->data, err->dsize);
284 static int patmatch (const pattern_t* pat, const char* buf) {
285 if (pat->stringmatch)
286 return !strstr (buf, pat->str);
288 return regexec (pat->rx, buf, 0, NULL, 0);
291 int eat_range (pattern_t * pat, BUFFER * s, BUFFER * err __attribute__ ((unused)))
294 int do_exclusive = 0;
298 * If simple_search is set to "~m %s", the range will have double quotes
301 if (*s->dptr == '"') {
307 if ((*s->dptr != '-') && (*s->dptr != '<')) {
309 if (*s->dptr == '>') {
310 pat->max = M_MAXRANGE;
311 pat->min = strtol (s->dptr + 1, &tmp, 0) + 1; /* exclusive range */
314 pat->min = strtol (s->dptr, &tmp, 0);
315 if (toupper ((unsigned char) *tmp) == 'K') { /* is there a prefix? */
319 else if (toupper ((unsigned char) *tmp) == 'M') {
323 if (*s->dptr == '>') {
340 if (isdigit ((unsigned char) *tmp)) {
342 pat->max = strtol (tmp, &tmp, 0);
343 if (toupper ((unsigned char) *tmp) == 'K') {
347 else if (toupper ((unsigned char) *tmp) == 'M') {
355 pat->max = M_MAXRANGE;
357 if (skip_quote && *tmp == '"')
360 s->dptr = vskipspaces(tmp);
364 static const char *getDate (const char *s, struct tm *t, BUFFER * err)
367 time_t now = time (NULL);
368 struct tm *tm = localtime (&now);
370 t->tm_mday = strtol (s, &p, 10);
371 if (t->tm_mday < 1 || t->tm_mday > 31) {
372 snprintf (err->data, err->dsize, _("Invalid day of month: %s"), s);
376 /* fill in today's month and year */
377 t->tm_mon = tm->tm_mon;
378 t->tm_year = tm->tm_year;
382 t->tm_mon = strtol (p, &p, 10) - 1;
383 if (t->tm_mon < 0 || t->tm_mon > 11) {
384 snprintf (err->data, err->dsize, _("Invalid month: %s"), p);
388 t->tm_year = tm->tm_year;
392 t->tm_year = strtol (p, &p, 10);
393 if (t->tm_year < 70) /* year 2000+ */
395 else if (t->tm_year > 1900)
404 static const char *get_offset (struct tm *tm, const char *s, int sign)
407 int offset = strtol (s, &ps, 0);
409 if ((sign < 0 && offset > 0) || (sign > 0 && offset < 0))
414 tm->tm_year += offset;
417 tm->tm_mon += offset;
420 tm->tm_mday += 7 * offset;
423 tm->tm_mday += offset;
428 mutt_normalize_time (tm);
432 static void adjust_date_range (struct tm *min, struct tm *max)
434 if (min->tm_year > max->tm_year
435 || (min->tm_year == max->tm_year && min->tm_mon > max->tm_mon)
436 || (min->tm_year == max->tm_year && min->tm_mon == max->tm_mon
437 && min->tm_mday > max->tm_mday)) {
441 min->tm_year = max->tm_year;
445 min->tm_mon = max->tm_mon;
449 min->tm_mday = max->tm_mday;
452 min->tm_hour = min->tm_min = min->tm_sec = 0;
454 max->tm_min = max->tm_sec = 59;
458 static const char *parse_date_range (const char *pc, struct tm *min,
459 struct tm *max, int haveMin,
460 struct tm *baseMin, BUFFER * err)
462 int flag = M_PDR_NONE;
464 while (*pc && ((flag & M_PDR_DONE) == 0)) {
468 pc = vskipspaces(pc);
472 /* try a range of absolute date minus offset of Ndwmy */
473 pt = get_offset (min, pc, -1);
475 if (flag == M_PDR_NONE) { /* nothing yet and no offset parsed => absolute date? */
476 if (!getDate (pc, max, err))
477 flag |= (M_PDR_ABSOLUTE | M_PDR_ERRORDONE); /* done bad */
479 /* reestablish initial base minimum if not specified */
481 memcpy (min, baseMin, sizeof (struct tm));
482 flag |= (M_PDR_ABSOLUTE | M_PDR_DONE); /* done good */
486 flag |= M_PDR_ERRORDONE;
490 if (flag == M_PDR_NONE && !haveMin) { /* the very first "-3d" without a previous absolute date */
491 max->tm_year = min->tm_year;
492 max->tm_mon = min->tm_mon;
493 max->tm_mday = min->tm_mday;
500 { /* enlarge plusRange */
501 pt = get_offset (max, pc, 1);
503 flag |= M_PDR_ERRORDONE;
511 { /* enlarge window in both directions */
512 pt = get_offset (min, pc, -1);
514 flag |= M_PDR_ERRORDONE;
516 pc = get_offset (max, pc, 1);
517 flag |= M_PDR_WINDOW;
522 flag |= M_PDR_ERRORDONE;
524 pc = vskipspaces(pc);
526 if ((flag & M_PDR_ERROR) && !(flag & M_PDR_ABSOLUTE)) { /* getDate has its own error message, don't overwrite it here */
527 snprintf (err->data, err->dsize, _("Invalid relative date: %s"), pc - 1);
529 return ((flag & M_PDR_ERROR) ? NULL : pc);
532 static int eat_date (pattern_t * pat, BUFFER * s, BUFFER * err)
538 if (mutt_extract_token (&buffer, s, M_TOKEN_COMMENT | M_TOKEN_PATTERN) != 0
540 m_strcpy(err->data, err->dsize, _("error in expression"));
545 /* the `0' time is Jan 1, 1970 UTC, so in order to prevent a negative time
546 when doing timezone conversion, we use Jan 2, 1970 UTC as the base
553 /* Arbitrary year in the future. Don't set this too high
554 or mutt_mktime() returns something larger than will
555 fit in a time_t on some systems */
563 if (strchr ("<>=", buffer.data[0])) {
564 /* offset from current time
565 <3d less than three days ago
566 >3d more than three days ago
567 =3d exactly three days ago */
568 time_t now = time (NULL);
569 struct tm *tm = localtime (&now);
572 if (buffer.data[0] == '<') {
573 memcpy (&min, tm, sizeof (min));
577 memcpy (&max, tm, sizeof (max));
580 if (buffer.data[0] == '=')
584 tm->tm_min = tm->tm_sec = 59;
586 /* force negative offset */
587 get_offset (tm, buffer.data + 1, -1);
590 /* start at the beginning of the day in question */
591 memcpy (&min, &max, sizeof (max));
592 min.tm_hour = min.tm_sec = min.tm_min = 0;
596 const char *pc = buffer.data;
599 int untilNow = FALSE;
601 if (isdigit ((unsigned char) *pc)) {
602 /* mininum date specified */
603 if ((pc = getDate (pc, &min, err)) == NULL) {
604 p_delete(&buffer.data);
608 pc = vskipspaces(pc);
612 pt = skipspaces(pc + 1);
613 untilNow = (*pt == '\0');
617 if (!untilNow) { /* max date or relative range/window */
621 if (!haveMin) { /* save base minimum and set current date, e.g. for "-3d+1d" */
622 time_t now = time (NULL);
623 struct tm *tm = localtime (&now);
625 memcpy (&baseMin, &min, sizeof (baseMin));
626 memcpy (&min, tm, sizeof (min));
627 min.tm_hour = min.tm_sec = min.tm_min = 0;
630 /* preset max date for relative offsets,
631 if nothing follows we search for messages on a specific day */
632 max.tm_year = min.tm_year;
633 max.tm_mon = min.tm_mon;
634 max.tm_mday = min.tm_mday;
636 if (!parse_date_range (pc, &min, &max, haveMin, &baseMin, err)) { /* bail out on any parsing error */
637 p_delete(&buffer.data);
643 /* Since we allow two dates to be specified we'll have to adjust that. */
644 adjust_date_range (&min, &max);
646 pat->min = mutt_mktime (&min, 1);
647 pat->max = mutt_mktime (&max, 1);
649 p_delete(&buffer.data);
654 static struct pattern_flags *lookup_tag (char tag)
658 for (i = 0; Flags[i].tag; i++)
659 if (Flags[i].tag == tag)
664 static /* const */ char *find_matching_paren ( /* const */ char *s)
671 else if (*s == ')') {
680 void mutt_pattern_free (pattern_t ** pat)
694 mutt_pattern_free (&tmp->child);
699 pattern_t *mutt_pattern_comp ( /* const */ char *s, int flags, BUFFER * err)
701 pattern_t *curlist = NULL;
703 pattern_t *last = NULL;
707 int implicit = 1; /* used to detect logical AND operator */
708 struct pattern_flags *entry;
715 ps.dsize = m_strlen(s);
718 ps.dptr = vskipspaces(ps.dptr);
731 snprintf (err->data, err->dsize, _("error in pattern at: %s"),
736 /* A & B | C == (A & B) | C */
737 tmp = new_pattern ();
739 tmp->child = curlist;
755 if (implicit && or) {
756 /* A | B & C == (A | B) & C */
757 tmp = new_pattern ();
759 tmp->child = curlist;
765 tmp = new_pattern ();
767 tmp->alladdr = alladdr;
768 tmp->stringmatch = (*ps.dptr == '=') ? 1 : 0;
778 ps.dptr++; /* move past the ~ */
779 if ((entry = lookup_tag (*ps.dptr)) == NULL) {
780 snprintf (err->data, err->dsize, _("%c: invalid command"), *ps.dptr);
781 mutt_pattern_free (&curlist);
784 if (entry->class && (flags & entry->class) == 0) {
785 snprintf (err->data, err->dsize, _("%c: not supported in this mode"),
787 mutt_pattern_free (&curlist);
792 ps.dptr = vskipspaces(ps.dptr + 1);
794 if (entry->eat_arg) {
796 snprintf (err->data, err->dsize, _("missing parameter"));
797 mutt_pattern_free (&curlist);
800 if (entry->eat_arg (tmp, &ps, err) == -1) {
801 mutt_pattern_free (&curlist);
808 p = find_matching_paren (ps.dptr + 1);
810 snprintf (err->data, err->dsize, _("mismatched parenthesis: %s"),
812 mutt_pattern_free (&curlist);
815 /* compile the sub-expression */
816 buf = p_dupstr(ps.dptr + 1, p - ps.dptr - 1);
817 if ((tmp = mutt_pattern_comp (buf, flags, err)) == NULL) {
819 mutt_pattern_free (&curlist);
829 tmp->alladdr |= alladdr;
832 ps.dptr = p + 1; /* restore location */
835 snprintf (err->data, err->dsize, _("error in pattern at: %s"), ps.dptr);
836 mutt_pattern_free (&curlist);
841 m_strcpy(err->data, err->dsize, _("empty pattern"));
845 tmp = new_pattern ();
846 tmp->op = or ? M_OR : M_AND;
847 tmp->child = curlist;
854 perform_and (pattern_t * pat, pattern_exec_flag flags, CONTEXT * ctx,
857 for (; pat; pat = pat->next)
858 if (mutt_pattern_exec (pat, flags, ctx, hdr) <= 0)
864 perform_or (struct pattern_t *pat, pattern_exec_flag flags, CONTEXT * ctx,
867 for (; pat; pat = pat->next)
868 if (mutt_pattern_exec (pat, flags, ctx, hdr) > 0)
873 static int match_adrlist (pattern_t* pat, int match_personal, int alladdr __attribute__ ((unused)),
881 for (a = va_arg (ap, address_t *); a; a = a->next) {
883 ((a->mailbox && patmatch (pat, a->mailbox) == 0) ||
884 (match_personal && a->personal &&
885 patmatch (pat, a->personal) == 0))) {
887 return (!pat->alladdr); /* Found match, or non-match if alladdr */
892 return pat->alladdr; /* No matches, or all matches if alladdr */
895 static int match_reference (pattern_t* pat, LIST * refs)
897 for (; refs; refs = refs->next)
898 if (patmatch (pat, refs->data) == 0)
903 int mutt_is_list_recipient (int alladdr, address_t * a1, address_t * a2)
905 for (; a1; a1 = a1->next)
906 if (alladdr ^ mutt_is_subscribed_list (a1))
908 for (; a2; a2 = a2->next)
909 if (alladdr ^ mutt_is_subscribed_list (a2))
914 int mutt_is_list_cc (int alladdr, address_t * a1, address_t * a2)
916 for (; a1; a1 = a1->next)
917 if (alladdr ^ mutt_is_mail_list (a1))
919 for (; a2; a2 = a2->next)
920 if (alladdr ^ mutt_is_mail_list (a2))
925 static int match_user (int alladdr, address_t * a1, address_t * a2)
927 for (; a1; a1 = a1->next)
928 if (alladdr ^ mutt_addr_is_user (a1))
930 for (; a2; a2 = a2->next)
931 if (alladdr ^ mutt_addr_is_user (a2))
936 /* test if name is considered a real name, i.e. consists of at least 2
937 * space-separated words of which none may end in a dot
939 static int valid_realname (const char *name)
941 const char *p = name;
948 /* skip abbr. parts of names (e.g. 'J. User') */
956 M_MATCH_FULL_ADDRESS match both personal and machine address */
958 mutt_pattern_exec (struct pattern_t *pat, pattern_exec_flag flags,
959 CONTEXT * ctx, HEADER * h)
963 return (pat->not ^ (perform_and (pat->child, flags, ctx, h) > 0));
965 return (pat->not ^ (perform_or (pat->child, flags, ctx, h) > 0));
969 return (pat->not ^ h->expired);
971 return (pat->not ^ h->superseded);
973 return (pat->not ^ h->flagged);
975 return (pat->not ^ h->tagged);
977 return (pat->not ? h->old || h->read : !(h->old || h->read));
979 return (pat->not ? h->read : !h->read);
981 return (pat->not ^ h->replied);
983 return (pat->not ? (!h->old || h->read) : (h->old && !h->read));
985 return (pat->not ^ h->read);
987 return (pat->not ^ h->deleted);
989 return (pat->not ^ (h->msgno >= pat->min - 1 && (pat->max == M_MAXRANGE ||
994 not ^ (h->date_sent >= pat->min && h->date_sent <= pat->max));
995 case M_DATE_RECEIVED:
996 return (pat->not ^ (h->received >= pat->min && h->received <= pat->max));
1000 /* IMAP search sets h->matched at search compile time */
1001 if (ctx->magic == M_IMAP && pat->stringmatch)
1002 return (h->matched);
1003 return (pat->not ^ msg_search (ctx, pat, h->msgno));
1005 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
1006 pat->alladdr, 1, h->env->sender));
1008 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
1009 pat->alladdr, 1, h->env->from));
1011 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
1012 pat->alladdr, 1, h->env->to));
1014 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
1015 pat->alladdr, 1, h->env->cc));
1018 not ^ (h->env && h->env->subject
1019 && patmatch (pat, h->env->subject) == 0));
1022 not ^ (h->env && h->env->message_id
1023 && patmatch (pat, h->env->message_id) == 0));
1025 return (pat->not ^ (h->score >= pat->min && (pat->max == M_MAXRANGE ||
1026 h->score <= pat->max)));
1029 not ^ (h->content->length >= pat->min
1030 && (pat->max == M_MAXRANGE
1031 || h->content->length <= pat->max)));
1033 return (pat->not ^ match_reference (pat, h->env->references));
1037 && match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
1038 pat->alladdr, 4, h->env->from,
1039 h->env->sender, h->env->to,
1044 && match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
1045 pat->alladdr, 2, h->env->to,
1050 && mutt_is_list_cc (pat->alladdr, h->env->to,
1052 case M_SUBSCRIBED_LIST:
1055 && mutt_is_list_recipient (pat->alladdr, h->env->to,
1057 case M_PERSONAL_RECIP:
1060 && match_user (pat->alladdr, h->env->to, h->env->cc)));
1061 case M_PERSONAL_FROM:
1063 not ^ (h->env && match_user (pat->alladdr, h->env->from, NULL)));
1065 return (pat->not ^ (h->collapsed && h->num_hidden > 1));
1067 return (pat->not ^ ((h->security & SIGN) ? 1 : 0));
1068 case M_CRYPT_VERIFIED:
1069 return (pat->not ^ ((h->security & GOODSIGN) ? 1 : 0));
1070 case M_CRYPT_ENCRYPT:
1071 return (pat->not ^ ((h->security & ENCRYPT) ? 1 : 0));
1073 return (pat->not ^ ((h->security & APPLICATION_PGP)
1074 && (h->security & PGPKEY)));
1077 not ^ (h->env->x_label
1078 && patmatch (pat, h->env->x_label) == 0));
1081 not ^ (h->env->spam && h->env->spam->data
1082 && patmatch (pat, h->env->spam->data) == 0));
1084 return (pat->not ^ (h->thread && h->thread->duplicate_thread));
1090 if (h->content->parts)
1091 count = mutt_count_body_parts(h, 0);
1093 mutt_parse_mime_message(ctx, h);
1094 count = mutt_count_body_parts(h, 0);
1095 mutt_free_body(&h->content->parts);
1098 return (pat->not ^ (count >= pat->min && (pat->max == M_MAXRANGE ||
1099 count <= pat->max)));
1102 case M_UNREFERENCED:
1103 return (pat->not ^ (h->thread && !h->thread->child));
1105 return (pat->not ^ (h->content && h->content->type == TYPEMULTIPART));
1108 * we have a match if
1109 * - From: matches $alternates
1110 * - or we have an alias for current address
1111 * - or From: contains valid email address _and_ name has >= 2 fields
1114 not ^ (h->env && h->env->from && (mutt_addr_is_user (h->env->from)
1116 (alias_reverse_lookup
1117 (h->env->from) != NULL)
1118 || (h->env->from->personal
1119 && valid_realname (h->env->
1122 && h->env->from->mailbox)
1127 not ^ (h->env->newsgroups
1128 && patmatch (pat, h->env->newsgroups) == 0));
1131 mutt_error (_("error: unknown op %d (report this error)."), pat->op);
1135 static void quote_simple (char *tmp, ssize_t len, const char *p)
1140 while (*p && i < len - 3) {
1141 if (*p == '\\' || *p == '"')
1149 /* convert a simple search into a real request */
1150 void mutt_check_simple (char *s, size_t len, const char *simple)
1152 char tmp[LONG_STRING];
1154 /* XXX - is ascii_strcasecmp() right here, or should we use locale's
1158 if (!strchr (s, '~') && !strchr (s, '=')) { /* yup, so spoof a real request */
1159 /* convert old tokens into the new format */
1160 if (ascii_strcasecmp ("all", s) == 0 || !m_strcmp("^", s) || !m_strcmp(".", s)) /* ~A is more efficient */
1161 m_strcpy(s, len, "~A");
1162 else if (ascii_strcasecmp ("del", s) == 0)
1163 m_strcpy(s, len, "~D");
1164 else if (ascii_strcasecmp ("flag", s) == 0)
1165 m_strcpy(s, len, "~F");
1166 else if (ascii_strcasecmp ("new", s) == 0)
1167 m_strcpy(s, len, "~N");
1168 else if (ascii_strcasecmp ("old", s) == 0)
1169 m_strcpy(s, len, "~O");
1170 else if (ascii_strcasecmp ("repl", s) == 0)
1171 m_strcpy(s, len, "~Q");
1172 else if (ascii_strcasecmp ("read", s) == 0)
1173 m_strcpy(s, len, "~R");
1174 else if (ascii_strcasecmp ("tag", s) == 0)
1175 m_strcpy(s, len, "~T");
1176 else if (ascii_strcasecmp ("unread", s) == 0)
1177 m_strcpy(s, len, "~U");
1179 quote_simple (tmp, sizeof (tmp), s);
1180 mutt_expand_fmt (s, len, simple, tmp);
1185 int mutt_pattern_func (int op, char *prompt)
1188 char buf[LONG_STRING] = "", *simple, error[STRING];
1192 m_strcpy(buf, sizeof(buf), NONULL(Context->pattern));
1193 if (prompt || op != M_LIMIT)
1194 if (mutt_get_field (prompt, buf, sizeof (buf), M_PATTERN | M_CLEAR) != 0 || !buf[0])
1197 mutt_message _("Compiling search pattern...");
1199 simple = m_strdup(buf);
1200 mutt_check_simple (buf, sizeof (buf), NONULL (SimpleSearch));
1203 err.dsize = sizeof (error);
1204 if ((pat = mutt_pattern_comp (buf, M_FULL_MSG, &err)) == NULL) {
1206 mutt_error ("%s", err.data);
1210 if (Context->magic == M_IMAP && imap_search (Context, pat) < 0)
1213 mutt_message _("Executing command on matching messages...");
1215 #define THIS_BODY Context->hdrs[i]->content
1217 if (op == M_LIMIT) {
1218 Context->vcount = 0;
1220 Context->collapsed = 0;
1222 for (i = 0; i < Context->msgcount; i++) {
1223 /* new limit pattern implicitly uncollapses all threads */
1224 Context->hdrs[i]->virtual = -1;
1225 Context->hdrs[i]->limited = 0;
1226 Context->hdrs[i]->collapsed = 0;
1227 Context->hdrs[i]->num_hidden = 0;
1228 if (mutt_pattern_exec
1229 (pat, M_MATCH_FULL_ADDRESS, Context, Context->hdrs[i])) {
1230 Context->hdrs[i]->virtual = Context->vcount;
1231 Context->hdrs[i]->limited = 1;
1232 Context->v2r[Context->vcount] = i;
1234 Context->vsize += THIS_BODY->length + THIS_BODY->offset -
1235 THIS_BODY->hdr_offset;
1240 for (i = 0; i < Context->vcount; i++) {
1241 if (mutt_pattern_exec
1242 (pat, M_MATCH_FULL_ADDRESS, Context,
1243 Context->hdrs[Context->v2r[i]])) {
1246 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_PURGED,
1249 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_DELETE,
1254 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_TAG,
1264 mutt_clear_error ();
1266 if (op == M_LIMIT) {
1267 /* drop previous limit pattern */
1268 p_delete(&Context->pattern);
1269 if (Context->limit_pattern)
1270 mutt_pattern_free (&Context->limit_pattern);
1271 if (Context->msgcount && !Context->vcount) {
1272 mutt_error _("No messages matched criteria.");
1275 /* record new limit pattern, unless match all */
1276 if (m_strncmp(buf, "~A", 2) != 0) {
1277 Context->pattern = simple;
1278 simple = NULL; /* don't clobber it */
1279 Context->limit_pattern = mutt_pattern_comp (buf, M_FULL_MSG, &err);
1283 mutt_pattern_free (&pat);
1287 int mutt_search_command (int cur, int op)
1291 char temp[LONG_STRING];
1297 if (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE) {
1298 m_strcpy(buf, sizeof(buf), LastSearch);
1299 if (mutt_get_field ((op == OP_SEARCH) ? _("Search for: ") :
1300 _("Reverse search for: "), buf, sizeof (buf),
1301 M_CLEAR | M_PATTERN) != 0 || !buf[0])
1304 if (op == OP_SEARCH)
1305 unset_option (OPTSEARCHREVERSE);
1307 set_option (OPTSEARCHREVERSE);
1309 /* compare the *expanded* version of the search pattern in case
1310 $simple_search has changed while we were searching */
1311 m_strcpy(temp, sizeof(temp), buf);
1312 mutt_check_simple (temp, sizeof (temp), NONULL (SimpleSearch));
1314 if (!SearchPattern || m_strcmp(temp, LastSearchExpn)) {
1315 set_option (OPTSEARCHINVALID);
1316 m_strcpy(LastSearch, sizeof(LastSearch), buf);
1317 mutt_message _("Compiling search pattern...");
1319 mutt_pattern_free (&SearchPattern);
1321 err.dsize = sizeof (error);
1322 if ((SearchPattern =
1323 mutt_pattern_comp (temp, M_FULL_MSG, &err)) == NULL) {
1324 mutt_error ("%s", error);
1327 mutt_clear_error ();
1330 else if (!SearchPattern) {
1331 mutt_error _("No search pattern.");
1336 if (option (OPTSEARCHINVALID)) {
1337 for (i = 0; i < Context->msgcount; i++)
1338 Context->hdrs[i]->searched = 0;
1339 if (Context->magic == M_IMAP && imap_search (Context, SearchPattern) < 0)
1341 unset_option (OPTSEARCHINVALID);
1344 incr = (option (OPTSEARCHREVERSE)) ? -1 : 1;
1345 if (op == OP_SEARCH_OPPOSITE)
1348 for (i = cur + incr, j = 0; j != Context->vcount; j++) {
1349 if (i > Context->vcount - 1) {
1351 if (option (OPTWRAPSEARCH))
1352 mutt_message (_("Search wrapped to top."));
1355 mutt_message _("Search hit bottom without finding match");
1361 i = Context->vcount - 1;
1362 if (option (OPTWRAPSEARCH))
1363 mutt_message (_("Search wrapped to bottom."));
1366 mutt_message _("Search hit top without finding match");
1372 h = Context->hdrs[Context->v2r[i]];
1374 /* if we've already evaulated this message, use the cached value */
1379 /* remember that we've already searched this message */
1383 (SearchPattern, M_MATCH_FULL_ADDRESS, Context, h) > 0)))
1388 mutt_error _("Search interrupted.");
1397 mutt_error _("Not found.");