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.
10 #include <lib-lib/lib-lib.h>
12 #include <lib-mime/mime.h>
13 #include <lib-ui/enter.h>
14 #include <lib-ui/curses.h>
15 #include <lib-mx/mx.h>
23 #include <imap/imap.h>
25 #include <lib-crypt/crypt.h>
27 static int eat_regexp (pattern_t * pat, BUFFER *, BUFFER *);
28 static int eat_date (pattern_t * pat, BUFFER *, BUFFER *);
29 static int eat_range (pattern_t * pat, BUFFER *, BUFFER *);
30 static int patmatch (const pattern_t* pat, const char* buf);
32 struct pattern_flags {
33 int tag; /* character used to represent this op */
34 int op; /* operation to perform */
36 int (*eat_arg)(pattern_t *, BUFFER *, BUFFER *);
38 {'A', M_ALL, 0, NULL},
39 {'b', M_BODY, M_FULL_MSG, eat_regexp},
40 {'B', M_WHOLE_MSG, M_FULL_MSG, eat_regexp},
41 {'c', M_CC, 0, eat_regexp},
42 {'C', M_RECIPIENT, 0, eat_regexp},
43 {'d', M_DATE, 0, eat_date},
44 {'D', M_DELETED, 0, NULL},
45 {'e', M_SENDER, 0, eat_regexp},
46 {'E', M_EXPIRED, 0, NULL},
47 {'f', M_FROM, 0, eat_regexp},
48 {'F', M_FLAG, 0, NULL},
49 {'g', M_CRYPT_SIGN, 0, NULL},
50 {'G', M_CRYPT_ENCRYPT, 0, NULL},
51 {'h', M_HEADER, M_FULL_MSG, eat_regexp},
52 {'H', M_HORMEL, 0, eat_regexp},
53 {'i', M_ID, 0, eat_regexp},
54 {'k', M_PGP_KEY, 0, NULL},
55 {'L', M_ADDRESS, 0, eat_regexp},
56 {'l', M_LIST, 0, NULL},
57 {'m', M_MESSAGE, 0, eat_range},
58 {'M', M_MULTIPART, 0, NULL},
59 {'n', M_SCORE, 0, eat_range},
60 {'N', M_NEW, 0, NULL},
61 {'O', M_OLD, 0, NULL},
62 {'p', M_PERSONAL_RECIP, 0, NULL},
63 {'P', M_PERSONAL_FROM, 0, NULL},
64 {'Q', M_REPLIED, 0, NULL},
65 {'R', M_READ, 0, NULL},
66 {'r', M_DATE_RECEIVED, 0, eat_date},
67 {'s', M_SUBJECT, 0, eat_regexp},
68 {'S', M_SUPERSEDED, 0, NULL},
69 {'T', M_TAG, 0, NULL},
70 {'t', M_TO, 0, eat_regexp},
71 {'U', M_UNREAD, 0, NULL},
72 {'u', M_SUBSCRIBED_LIST, 0, NULL},
73 {'v', M_COLLAPSED, 0, NULL},
74 {'V', M_CRYPT_VERIFIED, 0, NULL},
76 {'w', M_NEWSGROUPS, 0, eat_regexp},
78 {'x', M_REFERENCE, 0, eat_regexp},
79 {'X', M_MIMEATTACH, 0, eat_range},
80 {'y', M_XLABEL, 0, eat_regexp},
81 {'z', M_SIZE, 0, eat_range},
82 {'=', M_DUPLICATED, 0, NULL},
83 {'$', M_UNREFERENCED, 0, NULL},
84 {'*', M_REALNAME, 0, NULL},
88 static pattern_t *SearchPattern = NULL; /* current search pattern */
89 static char LastSearch[STRING] = { 0 }; /* last pattern searched for */
90 static char LastSearchExpn[LONG_STRING] = { 0 }; /* expanded version of
95 /* constants for parse_date_range() */
96 #define M_PDR_NONE 0x0000
97 #define M_PDR_MINUS 0x0001
98 #define M_PDR_PLUS 0x0002
99 #define M_PDR_WINDOW 0x0004
100 #define M_PDR_ABSOLUTE 0x0008
101 #define M_PDR_DONE 0x0010
102 #define M_PDR_ERROR 0x0100
103 #define M_PDR_ERRORDONE (M_PDR_ERROR | M_PDR_DONE)
105 /* if no uppercase letters are given, do a case-insensitive search */
106 int mutt_which_case (const char *s)
109 if (isalpha ((unsigned char) *s) && isupper ((unsigned char) *s))
110 return 0; /* case-sensitive */
113 return REG_ICASE; /* case-insensitive */
117 msg_search (CONTEXT *ctx, pattern_t* pat, int msgno)
119 char tempfile[_POSIX_PATH_MAX];
126 HEADER *h = ctx->hdrs[msgno];
130 if ((msg = mx_open_message (ctx, msgno)) != NULL) {
131 if (option (OPTTHOROUGHSRC)) {
132 /* decode the header / body */
135 s.flags = M_CHARCONV;
136 s.fpout = m_tempfile(tempfile, sizeof(tempfile), NONULL(Tempdir), NULL);
138 mutt_error(_("Could not create temporary file"));
142 if (pat->op != M_BODY)
143 mutt_copy_header (msg->fp, h, s.fpout, CH_FROM | CH_DECODE, NULL);
145 if (pat->op != M_HEADER) {
146 mutt_parse_mime_message (ctx, h);
148 if ((h->security & ENCRYPT) && !crypt_valid_passphrase (h->security))
150 mx_close_message (&msg);
158 fseeko (msg->fp, h->offset, 0);
159 mutt_body_handler (h->content, &s);
165 fstat (fileno (fp), &st);
166 lng = (long) st.st_size;
169 /* raw header / body */
171 if (pat->op != M_BODY) {
172 fseeko (fp, h->offset, 0);
173 lng = h->content->offset - h->offset;
175 if (pat->op != M_HEADER) {
176 if (pat->op == M_BODY)
177 fseeko (fp, h->content->offset, 0);
178 lng += h->content->length;
182 buf = p_new(char, blen = STRING);
184 /* search the file "fp" */
186 if (pat->op == M_HEADER) {
187 if (!mutt_read_rfc822_line(fp, &buf, &blen))
189 } else if (fgets (buf, blen - 1, fp) == NULL)
190 break; /* don't loop forever */
191 if (patmatch (pat, buf) == 0) {
195 lng -= m_strlen(buf);
200 mx_close_message (&msg);
202 if (option (OPTTHOROUGHSRC)) {
211 int eat_regexp (pattern_t * pat, BUFFER * s, BUFFER * err)
218 if (mutt_extract_token (&buf, s, M_TOKEN_PATTERN | M_TOKEN_COMMENT) != 0 ||
220 snprintf (err->data, err->dsize, _("Error in expression: %s"), s->dptr);
225 snprintf (err->data, err->dsize, _("Empty expression"));
230 /* If there are no RE metacharacters, use simple search anyway */
231 if (!pat->stringmatch && !strpbrk (buf.data, "|[{.*+?^$"))
232 pat->stringmatch = 1;
235 if (pat->stringmatch) {
236 pat->str = m_strdup(buf.data);
239 pat->rx = p_new(regex_t, 1);
240 r = REGCOMP (pat->rx, buf.data, REG_NEWLINE | REG_NOSUB | mutt_which_case (buf.data));
243 regerror (r, pat->rx, err->data, err->dsize);
252 static int patmatch (const pattern_t* pat, const char* buf) {
253 if (pat->stringmatch)
254 return !strstr (buf, pat->str);
256 return regexec (pat->rx, buf, 0, NULL, 0);
259 int eat_range (pattern_t * pat, BUFFER * s, BUFFER * err __attribute__ ((unused)))
262 int do_exclusive = 0;
266 * If simple_search is set to "~m %s", the range will have double quotes
269 if (*s->dptr == '"') {
275 if ((*s->dptr != '-') && (*s->dptr != '<')) {
277 if (*s->dptr == '>') {
278 pat->max = M_MAXRANGE;
279 pat->min = strtol (s->dptr + 1, &tmp, 0) + 1; /* exclusive range */
282 pat->min = strtol (s->dptr, &tmp, 0);
283 if (toupper ((unsigned char) *tmp) == 'K') { /* is there a prefix? */
287 else if (toupper ((unsigned char) *tmp) == 'M') {
291 if (*s->dptr == '>') {
308 if (isdigit ((unsigned char) *tmp)) {
310 pat->max = strtol (tmp, &tmp, 0);
311 if (toupper ((unsigned char) *tmp) == 'K') {
315 else if (toupper ((unsigned char) *tmp) == 'M') {
323 pat->max = M_MAXRANGE;
325 if (skip_quote && *tmp == '"')
328 s->dptr = vskipspaces(tmp);
332 static const char *getDate (const char *s, struct tm *t, BUFFER * err)
335 time_t now = time (NULL);
336 struct tm *tm = localtime (&now);
338 t->tm_mday = strtol (s, &p, 10);
339 if (t->tm_mday < 1 || t->tm_mday > 31) {
340 snprintf (err->data, err->dsize, _("Invalid day of month: %s"), s);
344 /* fill in today's month and year */
345 t->tm_mon = tm->tm_mon;
346 t->tm_year = tm->tm_year;
350 t->tm_mon = strtol (p, &p, 10) - 1;
351 if (t->tm_mon < 0 || t->tm_mon > 11) {
352 snprintf (err->data, err->dsize, _("Invalid month: %s"), p);
356 t->tm_year = tm->tm_year;
360 t->tm_year = strtol (p, &p, 10);
361 if (t->tm_year < 70) /* year 2000+ */
363 else if (t->tm_year > 1900)
372 static const char *get_offset (struct tm *tm, const char *s, int sign)
375 int offset = strtol (s, &ps, 0);
377 if ((sign < 0 && offset > 0) || (sign > 0 && offset < 0))
382 tm->tm_year += offset;
385 tm->tm_mon += offset;
388 tm->tm_mday += 7 * offset;
391 tm->tm_mday += offset;
396 mutt_normalize_time (tm);
400 static void adjust_date_range (struct tm *min, struct tm *max)
402 if (min->tm_year > max->tm_year
403 || (min->tm_year == max->tm_year && min->tm_mon > max->tm_mon)
404 || (min->tm_year == max->tm_year && min->tm_mon == max->tm_mon
405 && min->tm_mday > max->tm_mday)) {
409 min->tm_year = max->tm_year;
413 min->tm_mon = max->tm_mon;
417 min->tm_mday = max->tm_mday;
420 min->tm_hour = min->tm_min = min->tm_sec = 0;
422 max->tm_min = max->tm_sec = 59;
426 static const char *parse_date_range (const char *pc, struct tm *min,
427 struct tm *max, int haveMin,
428 struct tm *baseMin, BUFFER * err)
430 int flag = M_PDR_NONE;
432 while (*pc && ((flag & M_PDR_DONE) == 0)) {
436 pc = vskipspaces(pc);
440 /* try a range of absolute date minus offset of Ndwmy */
441 pt = get_offset (min, pc, -1);
443 if (flag == M_PDR_NONE) { /* nothing yet and no offset parsed => absolute date? */
444 if (!getDate (pc, max, err))
445 flag |= (M_PDR_ABSOLUTE | M_PDR_ERRORDONE); /* done bad */
447 /* reestablish initial base minimum if not specified */
449 memcpy (min, baseMin, sizeof (struct tm));
450 flag |= (M_PDR_ABSOLUTE | M_PDR_DONE); /* done good */
454 flag |= M_PDR_ERRORDONE;
458 if (flag == M_PDR_NONE && !haveMin) { /* the very first "-3d" without a previous absolute date */
459 max->tm_year = min->tm_year;
460 max->tm_mon = min->tm_mon;
461 max->tm_mday = min->tm_mday;
468 { /* enlarge plusRange */
469 pt = get_offset (max, pc, 1);
471 flag |= M_PDR_ERRORDONE;
479 { /* enlarge window in both directions */
480 pt = get_offset (min, pc, -1);
482 flag |= M_PDR_ERRORDONE;
484 pc = get_offset (max, pc, 1);
485 flag |= M_PDR_WINDOW;
490 flag |= M_PDR_ERRORDONE;
492 pc = vskipspaces(pc);
494 if ((flag & M_PDR_ERROR) && !(flag & M_PDR_ABSOLUTE)) { /* getDate has its own error message, don't overwrite it here */
495 snprintf (err->data, err->dsize, _("Invalid relative date: %s"), pc - 1);
497 return ((flag & M_PDR_ERROR) ? NULL : pc);
500 static int eat_date (pattern_t * pat, BUFFER * s, BUFFER * err)
506 if (mutt_extract_token (&buffer, s, M_TOKEN_COMMENT | M_TOKEN_PATTERN) != 0
508 m_strcpy(err->data, err->dsize, _("error in expression"));
513 /* the `0' time is Jan 1, 1970 UTC, so in order to prevent a negative time
514 when doing timezone conversion, we use Jan 2, 1970 UTC as the base
521 /* Arbitrary year in the future. Don't set this too high
522 or mutt_mktime() returns something larger than will
523 fit in a time_t on some systems */
531 if (strchr ("<>=", buffer.data[0])) {
532 /* offset from current time
533 <3d less than three days ago
534 >3d more than three days ago
535 =3d exactly three days ago */
536 time_t now = time (NULL);
537 struct tm *tm = localtime (&now);
540 if (buffer.data[0] == '<') {
541 memcpy (&min, tm, sizeof (min));
545 memcpy (&max, tm, sizeof (max));
548 if (buffer.data[0] == '=')
552 tm->tm_min = tm->tm_sec = 59;
554 /* force negative offset */
555 get_offset (tm, buffer.data + 1, -1);
558 /* start at the beginning of the day in question */
559 memcpy (&min, &max, sizeof (max));
560 min.tm_hour = min.tm_sec = min.tm_min = 0;
564 const char *pc = buffer.data;
567 int untilNow = FALSE;
569 if (isdigit ((unsigned char) *pc)) {
570 /* mininum date specified */
571 if ((pc = getDate (pc, &min, err)) == NULL) {
572 p_delete(&buffer.data);
576 pc = vskipspaces(pc);
580 pt = skipspaces(pc + 1);
581 untilNow = (*pt == '\0');
585 if (!untilNow) { /* max date or relative range/window */
589 if (!haveMin) { /* save base minimum and set current date, e.g. for "-3d+1d" */
590 time_t now = time (NULL);
591 struct tm *tm = localtime (&now);
593 memcpy (&baseMin, &min, sizeof (baseMin));
594 memcpy (&min, tm, sizeof (min));
595 min.tm_hour = min.tm_sec = min.tm_min = 0;
598 /* preset max date for relative offsets,
599 if nothing follows we search for messages on a specific day */
600 max.tm_year = min.tm_year;
601 max.tm_mon = min.tm_mon;
602 max.tm_mday = min.tm_mday;
604 if (!parse_date_range (pc, &min, &max, haveMin, &baseMin, err)) { /* bail out on any parsing error */
605 p_delete(&buffer.data);
611 /* Since we allow two dates to be specified we'll have to adjust that. */
612 adjust_date_range (&min, &max);
614 pat->min = mutt_mktime (&min, 1);
615 pat->max = mutt_mktime (&max, 1);
617 p_delete(&buffer.data);
622 static struct pattern_flags *lookup_tag (char tag)
626 for (i = 0; Flags[i].tag; i++)
627 if (Flags[i].tag == tag)
632 static /* const */ char *find_matching_paren ( /* const */ char *s)
639 else if (*s == ')') {
648 void mutt_pattern_free (pattern_t ** pat)
662 mutt_pattern_free (&tmp->child);
667 pattern_t *mutt_pattern_comp ( /* const */ char *s, int flags, BUFFER * err)
669 pattern_t *curlist = NULL;
671 pattern_t *last = NULL;
675 int implicit = 1; /* used to detect logical AND operator */
676 struct pattern_flags *entry;
683 ps.dsize = m_strlen(s);
686 ps.dptr = vskipspaces(ps.dptr);
699 snprintf (err->data, err->dsize, _("error in pattern at: %s"),
704 /* A & B | C == (A & B) | C */
705 tmp = new_pattern ();
707 tmp->child = curlist;
723 if (implicit && or) {
724 /* A | B & C == (A | B) & C */
725 tmp = new_pattern ();
727 tmp->child = curlist;
733 tmp = new_pattern ();
735 tmp->alladdr = alladdr;
736 tmp->stringmatch = (*ps.dptr == '=') ? 1 : 0;
746 ps.dptr++; /* move past the ~ */
747 if ((entry = lookup_tag (*ps.dptr)) == NULL) {
748 snprintf (err->data, err->dsize, _("%c: invalid command"), *ps.dptr);
749 mutt_pattern_free (&curlist);
752 if (entry->class && (flags & entry->class) == 0) {
753 snprintf (err->data, err->dsize, _("%c: not supported in this mode"),
755 mutt_pattern_free (&curlist);
760 ps.dptr = vskipspaces(ps.dptr + 1);
762 if (entry->eat_arg) {
764 snprintf (err->data, err->dsize, _("missing parameter"));
765 mutt_pattern_free (&curlist);
768 if (entry->eat_arg (tmp, &ps, err) == -1) {
769 mutt_pattern_free (&curlist);
776 p = find_matching_paren (ps.dptr + 1);
778 snprintf (err->data, err->dsize, _("mismatched parenthesis: %s"),
780 mutt_pattern_free (&curlist);
783 /* compile the sub-expression */
784 buf = p_dupstr(ps.dptr + 1, p - ps.dptr - 1);
785 if ((tmp = mutt_pattern_comp (buf, flags, err)) == NULL) {
787 mutt_pattern_free (&curlist);
797 tmp->alladdr |= alladdr;
800 ps.dptr = p + 1; /* restore location */
803 snprintf (err->data, err->dsize, _("error in pattern at: %s"), ps.dptr);
804 mutt_pattern_free (&curlist);
809 m_strcpy(err->data, err->dsize, _("empty pattern"));
813 tmp = new_pattern ();
814 tmp->op = or ? M_OR : M_AND;
815 tmp->child = curlist;
822 perform_and (pattern_t * pat, pattern_exec_flag flags, CONTEXT * ctx,
825 for (; pat; pat = pat->next)
826 if (mutt_pattern_exec (pat, flags, ctx, hdr) <= 0)
832 perform_or (struct pattern_t *pat, pattern_exec_flag flags, CONTEXT * ctx,
835 for (; pat; pat = pat->next)
836 if (mutt_pattern_exec (pat, flags, ctx, hdr) > 0)
841 static int match_adrlist (pattern_t* pat, int match_personal, int alladdr __attribute__ ((unused)),
849 for (a = va_arg (ap, address_t *); a; a = a->next) {
851 ((a->mailbox && patmatch (pat, a->mailbox) == 0) ||
852 (match_personal && a->personal &&
853 patmatch (pat, a->personal) == 0))) {
855 return (!pat->alladdr); /* Found match, or non-match if alladdr */
860 return pat->alladdr; /* No matches, or all matches if alladdr */
863 static int match_reference (pattern_t* pat, string_list_t * refs)
865 for (; refs; refs = refs->next)
866 if (patmatch (pat, refs->data) == 0)
871 int mutt_is_list_recipient (int alladdr, address_t * a1, address_t * a2)
873 for (; a1; a1 = a1->next)
874 if (alladdr ^ mutt_is_subscribed_list (a1))
876 for (; a2; a2 = a2->next)
877 if (alladdr ^ mutt_is_subscribed_list (a2))
882 int mutt_is_list_cc (int alladdr, address_t * a1, address_t * a2)
884 for (; a1; a1 = a1->next)
885 if (alladdr ^ mutt_is_mail_list (a1))
887 for (; a2; a2 = a2->next)
888 if (alladdr ^ mutt_is_mail_list (a2))
893 static int match_user (int alladdr, address_t * a1, address_t * a2)
895 for (; a1; a1 = a1->next)
896 if (alladdr ^ mutt_addr_is_user (a1))
898 for (; a2; a2 = a2->next)
899 if (alladdr ^ mutt_addr_is_user (a2))
904 /* test if name is considered a real name, i.e. consists of at least 2
905 * space-separated words of which none may end in a dot
907 static int valid_realname (const char *name)
909 const char *p = name;
916 /* skip abbr. parts of names (e.g. 'J. User') */
924 M_MATCH_FULL_ADDRESS match both personal and machine address */
926 mutt_pattern_exec (struct pattern_t *pat, pattern_exec_flag flags,
927 CONTEXT * ctx, HEADER * h)
931 return (pat->not ^ (perform_and (pat->child, flags, ctx, h) > 0));
933 return (pat->not ^ (perform_or (pat->child, flags, ctx, h) > 0));
937 return (pat->not ^ h->expired);
939 return (pat->not ^ h->superseded);
941 return (pat->not ^ h->flagged);
943 return (pat->not ^ h->tagged);
945 return (pat->not ? h->old || h->read : !(h->old || h->read));
947 return (pat->not ? h->read : !h->read);
949 return (pat->not ^ h->replied);
951 return (pat->not ? (!h->old || h->read) : (h->old && !h->read));
953 return (pat->not ^ h->read);
955 return (pat->not ^ h->deleted);
957 return (pat->not ^ (h->msgno >= pat->min - 1 && (pat->max == M_MAXRANGE ||
962 not ^ (h->date_sent >= pat->min && h->date_sent <= pat->max));
963 case M_DATE_RECEIVED:
964 return (pat->not ^ (h->received >= pat->min && h->received <= pat->max));
968 /* IMAP search sets h->matched at search compile time */
969 if (ctx->magic == M_IMAP && pat->stringmatch)
971 return (pat->not ^ msg_search (ctx, pat, h->msgno));
973 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
974 pat->alladdr, 1, h->env->sender));
976 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
977 pat->alladdr, 1, h->env->from));
979 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
980 pat->alladdr, 1, h->env->to));
982 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
983 pat->alladdr, 1, h->env->cc));
986 not ^ (h->env && h->env->subject
987 && patmatch (pat, h->env->subject) == 0));
990 not ^ (h->env && h->env->message_id
991 && patmatch (pat, h->env->message_id) == 0));
993 return (pat->not ^ (h->score >= pat->min && (pat->max == M_MAXRANGE ||
994 h->score <= pat->max)));
997 not ^ (h->content->length >= pat->min
998 && (pat->max == M_MAXRANGE
999 || h->content->length <= pat->max)));
1001 return (pat->not ^ match_reference (pat, h->env->references));
1005 && match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
1006 pat->alladdr, 4, h->env->from,
1007 h->env->sender, h->env->to,
1012 && match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
1013 pat->alladdr, 2, h->env->to,
1018 && mutt_is_list_cc (pat->alladdr, h->env->to,
1020 case M_SUBSCRIBED_LIST:
1023 && mutt_is_list_recipient (pat->alladdr, h->env->to,
1025 case M_PERSONAL_RECIP:
1028 && match_user (pat->alladdr, h->env->to, h->env->cc)));
1029 case M_PERSONAL_FROM:
1031 not ^ (h->env && match_user (pat->alladdr, h->env->from, NULL)));
1033 return (pat->not ^ (h->collapsed && h->num_hidden > 1));
1035 return (pat->not ^ ((h->security & SIGN) ? 1 : 0));
1036 case M_CRYPT_VERIFIED:
1037 return (pat->not ^ ((h->security & GOODSIGN) ? 1 : 0));
1038 case M_CRYPT_ENCRYPT:
1039 return (pat->not ^ ((h->security & ENCRYPT) ? 1 : 0));
1041 return (pat->not ^ ((h->security & APPLICATION_PGP)
1042 && (h->security & PGPKEY)));
1045 not ^ (h->env->x_label
1046 && patmatch (pat, h->env->x_label) == 0));
1049 not ^ (h->env->spam && h->env->spam->data
1050 && patmatch (pat, h->env->spam->data) == 0));
1052 return (pat->not ^ (h->thread && h->thread->duplicate_thread));
1058 if (h->content->parts)
1059 count = mutt_count_body_parts(h, 0);
1061 mutt_parse_mime_message(ctx, h);
1062 count = mutt_count_body_parts(h, 0);
1063 body_list_wipe(&h->content->parts);
1066 return (pat->not ^ (count >= pat->min && (pat->max == M_MAXRANGE ||
1067 count <= pat->max)));
1070 case M_UNREFERENCED:
1071 return (pat->not ^ (h->thread && !h->thread->child));
1073 return (pat->not ^ (h->content && h->content->type == TYPEMULTIPART));
1076 * we have a match if
1077 * - From: matches $alternates
1078 * - or we have an alias for current address
1079 * - or From: contains valid email address _and_ name has >= 2 fields
1081 return (h->env && h->env->from
1082 && (mutt_addr_is_user(h->env->from)
1083 || alias_reverse_lookup(h->env->from)
1084 || (h->env->from->personal
1085 && valid_realname(h->env-> from->personal)
1086 && h->env->from->mailbox)
1091 not ^ (h->env->newsgroups
1092 && patmatch (pat, h->env->newsgroups) == 0));
1095 mutt_error (_("error: unknown op %d (report this error)."), pat->op);
1099 static void quote_simple (char *tmp, ssize_t len, const char *p)
1104 while (*p && i < len - 3) {
1105 if (*p == '\\' || *p == '"')
1113 /* convert a simple search into a real request */
1114 void mutt_check_simple (char *s, ssize_t len, const char *simple)
1116 char tmp[LONG_STRING];
1118 /* XXX - is ascii_strcasecmp() right here, or should we use locale's
1122 if (!strchr (s, '~') && !strchr (s, '=')) { /* yup, so spoof a real request */
1123 /* convert old tokens into the new format */
1124 if (ascii_strcasecmp ("all", s) == 0 || !m_strcmp("^", s) || !m_strcmp(".", s)) /* ~A is more efficient */
1125 m_strcpy(s, len, "~A");
1126 else if (ascii_strcasecmp ("del", s) == 0)
1127 m_strcpy(s, len, "~D");
1128 else if (ascii_strcasecmp ("flag", s) == 0)
1129 m_strcpy(s, len, "~F");
1130 else if (ascii_strcasecmp ("new", s) == 0)
1131 m_strcpy(s, len, "~N");
1132 else if (ascii_strcasecmp ("old", s) == 0)
1133 m_strcpy(s, len, "~O");
1134 else if (ascii_strcasecmp ("repl", s) == 0)
1135 m_strcpy(s, len, "~Q");
1136 else if (ascii_strcasecmp ("read", s) == 0)
1137 m_strcpy(s, len, "~R");
1138 else if (ascii_strcasecmp ("tag", s) == 0)
1139 m_strcpy(s, len, "~T");
1140 else if (ascii_strcasecmp ("unread", s) == 0)
1141 m_strcpy(s, len, "~U");
1143 quote_simple (tmp, sizeof (tmp), s);
1144 m_file_fmt(s, len, simple, tmp);
1149 int mutt_pattern_func (int op, char *prompt)
1152 char buf[LONG_STRING] = "", *simple, error[STRING];
1156 m_strcpy(buf, sizeof(buf), NONULL(Context->pattern));
1157 if (prompt || op != M_LIMIT)
1158 if (mutt_get_field (prompt, buf, sizeof (buf), M_PATTERN | M_CLEAR) != 0 || !buf[0])
1161 mutt_message _("Compiling search pattern...");
1163 simple = m_strdup(buf);
1164 mutt_check_simple (buf, sizeof (buf), NONULL (SimpleSearch));
1167 err.dsize = sizeof (error);
1168 if ((pat = mutt_pattern_comp (buf, M_FULL_MSG, &err)) == NULL) {
1170 mutt_error ("%s", err.data);
1174 if (Context->magic == M_IMAP && imap_search (Context, pat) < 0)
1177 mutt_message _("Executing command on matching messages...");
1179 #define THIS_BODY Context->hdrs[i]->content
1181 if (op == M_LIMIT) {
1182 Context->vcount = 0;
1184 Context->collapsed = 0;
1186 for (i = 0; i < Context->msgcount; i++) {
1187 /* new limit pattern implicitly uncollapses all threads */
1188 Context->hdrs[i]->virtual = -1;
1189 Context->hdrs[i]->limited = 0;
1190 Context->hdrs[i]->collapsed = 0;
1191 Context->hdrs[i]->num_hidden = 0;
1192 if (mutt_pattern_exec
1193 (pat, M_MATCH_FULL_ADDRESS, Context, Context->hdrs[i])) {
1194 Context->hdrs[i]->virtual = Context->vcount;
1195 Context->hdrs[i]->limited = 1;
1196 Context->v2r[Context->vcount] = i;
1198 Context->vsize += THIS_BODY->length + THIS_BODY->offset -
1199 THIS_BODY->hdr_offset;
1204 for (i = 0; i < Context->vcount; i++) {
1205 if (mutt_pattern_exec
1206 (pat, M_MATCH_FULL_ADDRESS, Context,
1207 Context->hdrs[Context->v2r[i]])) {
1210 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_PURGED,
1213 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_DELETE,
1218 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_TAG,
1228 mutt_clear_error ();
1230 if (op == M_LIMIT) {
1231 /* drop previous limit pattern */
1232 p_delete(&Context->pattern);
1233 if (Context->limit_pattern)
1234 mutt_pattern_free (&Context->limit_pattern);
1235 if (Context->msgcount && !Context->vcount) {
1236 mutt_error _("No messages matched criteria.");
1239 /* record new limit pattern, unless match all */
1240 if (m_strncmp(buf, "~A", 2) != 0) {
1241 Context->pattern = simple;
1242 simple = NULL; /* don't clobber it */
1243 Context->limit_pattern = mutt_pattern_comp (buf, M_FULL_MSG, &err);
1247 mutt_pattern_free (&pat);
1251 int mutt_search_command (int cur, int op)
1255 char temp[LONG_STRING];
1261 if (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE) {
1262 m_strcpy(buf, sizeof(buf), LastSearch);
1263 if (mutt_get_field ((op == OP_SEARCH) ? _("Search for: ") :
1264 _("Reverse search for: "), buf, sizeof (buf),
1265 M_CLEAR | M_PATTERN) != 0 || !buf[0])
1268 if (op == OP_SEARCH)
1269 unset_option (OPTSEARCHREVERSE);
1271 set_option (OPTSEARCHREVERSE);
1273 /* compare the *expanded* version of the search pattern in case
1274 $simple_search has changed while we were searching */
1275 m_strcpy(temp, sizeof(temp), buf);
1276 mutt_check_simple (temp, sizeof (temp), NONULL (SimpleSearch));
1278 if (!SearchPattern || m_strcmp(temp, LastSearchExpn)) {
1279 set_option (OPTSEARCHINVALID);
1280 m_strcpy(LastSearch, sizeof(LastSearch), buf);
1281 mutt_message _("Compiling search pattern...");
1283 mutt_pattern_free (&SearchPattern);
1285 err.dsize = sizeof (error);
1286 if ((SearchPattern =
1287 mutt_pattern_comp (temp, M_FULL_MSG, &err)) == NULL) {
1288 mutt_error ("%s", error);
1291 mutt_clear_error ();
1294 else if (!SearchPattern) {
1295 mutt_error _("No search pattern.");
1300 if (option (OPTSEARCHINVALID)) {
1301 for (i = 0; i < Context->msgcount; i++)
1302 Context->hdrs[i]->searched = 0;
1303 if (Context->magic == M_IMAP && imap_search (Context, SearchPattern) < 0)
1305 unset_option (OPTSEARCHINVALID);
1308 incr = (option (OPTSEARCHREVERSE)) ? -1 : 1;
1309 if (op == OP_SEARCH_OPPOSITE)
1312 for (i = cur + incr, j = 0; j != Context->vcount; j++) {
1313 if (i > Context->vcount - 1) {
1315 if (option (OPTWRAPSEARCH))
1316 mutt_message (_("Search wrapped to top."));
1319 mutt_message _("Search hit bottom without finding match");
1325 i = Context->vcount - 1;
1326 if (option (OPTWRAPSEARCH))
1327 mutt_message (_("Search wrapped to bottom."));
1330 mutt_message _("Search hit top without finding match");
1336 h = Context->hdrs[Context->v2r[i]];
1338 /* if we've already evaulated this message, use the cached value */
1343 /* remember that we've already searched this message */
1347 (SearchPattern, M_MATCH_FULL_ADDRESS, Context, h) > 0)))
1352 mutt_error _("Search interrupted.");
1361 mutt_error _("Not found.");