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"));
229 if (pat->stringmatch) {
230 pat->str = m_strdup(buf.data);
233 pat->rx = p_new(regex_t, 1);
234 r = REGCOMP (pat->rx, buf.data, REG_NEWLINE | REG_NOSUB | mutt_which_case (buf.data));
237 regerror (r, pat->rx, err->data, err->dsize);
246 static int patmatch (const pattern_t* pat, const char* buf) {
247 if (pat->stringmatch)
248 return !strstr (buf, pat->str);
250 return regexec (pat->rx, buf, 0, NULL, 0);
253 int eat_range (pattern_t * pat, BUFFER * s, BUFFER * err __attribute__ ((unused)))
256 int do_exclusive = 0;
260 * If simple_search is set to "~m %s", the range will have double quotes
263 if (*s->dptr == '"') {
269 if ((*s->dptr != '-') && (*s->dptr != '<')) {
271 if (*s->dptr == '>') {
272 pat->max = M_MAXRANGE;
273 pat->min = strtol (s->dptr + 1, &tmp, 0) + 1; /* exclusive range */
276 pat->min = strtol (s->dptr, &tmp, 0);
277 if (toupper ((unsigned char) *tmp) == 'K') { /* is there a prefix? */
281 else if (toupper ((unsigned char) *tmp) == 'M') {
285 if (*s->dptr == '>') {
302 if (isdigit ((unsigned char) *tmp)) {
304 pat->max = strtol (tmp, &tmp, 0);
305 if (toupper ((unsigned char) *tmp) == 'K') {
309 else if (toupper ((unsigned char) *tmp) == 'M') {
317 pat->max = M_MAXRANGE;
319 if (skip_quote && *tmp == '"')
322 s->dptr = vskipspaces(tmp);
326 static const char *getDate (const char *s, struct tm *t, BUFFER * err)
329 time_t now = time (NULL);
330 struct tm *tm = localtime (&now);
332 t->tm_mday = strtol (s, &p, 10);
333 if (t->tm_mday < 1 || t->tm_mday > 31) {
334 snprintf (err->data, err->dsize, _("Invalid day of month: %s"), s);
338 /* fill in today's month and year */
339 t->tm_mon = tm->tm_mon;
340 t->tm_year = tm->tm_year;
344 t->tm_mon = strtol (p, &p, 10) - 1;
345 if (t->tm_mon < 0 || t->tm_mon > 11) {
346 snprintf (err->data, err->dsize, _("Invalid month: %s"), p);
350 t->tm_year = tm->tm_year;
354 t->tm_year = strtol (p, &p, 10);
355 if (t->tm_year < 70) /* year 2000+ */
357 else if (t->tm_year > 1900)
366 static const char *get_offset (struct tm *tm, const char *s, int sign)
369 int offset = strtol (s, &ps, 0);
371 if ((sign < 0 && offset > 0) || (sign > 0 && offset < 0))
376 tm->tm_year += offset;
379 tm->tm_mon += offset;
382 tm->tm_mday += 7 * offset;
385 tm->tm_mday += offset;
390 mutt_normalize_time (tm);
394 static void adjust_date_range (struct tm *min, struct tm *max)
396 if (min->tm_year > max->tm_year
397 || (min->tm_year == max->tm_year && min->tm_mon > max->tm_mon)
398 || (min->tm_year == max->tm_year && min->tm_mon == max->tm_mon
399 && min->tm_mday > max->tm_mday)) {
403 min->tm_year = max->tm_year;
407 min->tm_mon = max->tm_mon;
411 min->tm_mday = max->tm_mday;
414 min->tm_hour = min->tm_min = min->tm_sec = 0;
416 max->tm_min = max->tm_sec = 59;
420 static const char *parse_date_range (const char *pc, struct tm *min,
421 struct tm *max, int haveMin,
422 struct tm *baseMin, BUFFER * err)
424 int flag = M_PDR_NONE;
426 while (*pc && ((flag & M_PDR_DONE) == 0)) {
430 pc = vskipspaces(pc);
434 /* try a range of absolute date minus offset of Ndwmy */
435 pt = get_offset (min, pc, -1);
437 if (flag == M_PDR_NONE) { /* nothing yet and no offset parsed => absolute date? */
438 if (!getDate (pc, max, err))
439 flag |= (M_PDR_ABSOLUTE | M_PDR_ERRORDONE); /* done bad */
441 /* reestablish initial base minimum if not specified */
443 memcpy (min, baseMin, sizeof (struct tm));
444 flag |= (M_PDR_ABSOLUTE | M_PDR_DONE); /* done good */
448 flag |= M_PDR_ERRORDONE;
452 if (flag == M_PDR_NONE && !haveMin) { /* the very first "-3d" without a previous absolute date */
453 max->tm_year = min->tm_year;
454 max->tm_mon = min->tm_mon;
455 max->tm_mday = min->tm_mday;
462 { /* enlarge plusRange */
463 pt = get_offset (max, pc, 1);
465 flag |= M_PDR_ERRORDONE;
473 { /* enlarge window in both directions */
474 pt = get_offset (min, pc, -1);
476 flag |= M_PDR_ERRORDONE;
478 pc = get_offset (max, pc, 1);
479 flag |= M_PDR_WINDOW;
484 flag |= M_PDR_ERRORDONE;
486 pc = vskipspaces(pc);
488 if ((flag & M_PDR_ERROR) && !(flag & M_PDR_ABSOLUTE)) { /* getDate has its own error message, don't overwrite it here */
489 snprintf (err->data, err->dsize, _("Invalid relative date: %s"), pc - 1);
491 return ((flag & M_PDR_ERROR) ? NULL : pc);
494 static int eat_date (pattern_t * pat, BUFFER * s, BUFFER * err)
500 if (mutt_extract_token (&buffer, s, M_TOKEN_COMMENT | M_TOKEN_PATTERN) != 0
502 m_strcpy(err->data, err->dsize, _("error in expression"));
507 /* the `0' time is Jan 1, 1970 UTC, so in order to prevent a negative time
508 when doing timezone conversion, we use Jan 2, 1970 UTC as the base
515 /* Arbitrary year in the future. Don't set this too high
516 or mutt_mktime() returns something larger than will
517 fit in a time_t on some systems */
525 if (strchr ("<>=", buffer.data[0])) {
526 /* offset from current time
527 <3d less than three days ago
528 >3d more than three days ago
529 =3d exactly three days ago */
530 time_t now = time (NULL);
531 struct tm *tm = localtime (&now);
534 if (buffer.data[0] == '<') {
535 memcpy (&min, tm, sizeof (min));
539 memcpy (&max, tm, sizeof (max));
542 if (buffer.data[0] == '=')
546 tm->tm_min = tm->tm_sec = 59;
548 /* force negative offset */
549 get_offset (tm, buffer.data + 1, -1);
552 /* start at the beginning of the day in question */
553 memcpy (&min, &max, sizeof (max));
554 min.tm_hour = min.tm_sec = min.tm_min = 0;
558 const char *pc = buffer.data;
561 int untilNow = FALSE;
563 if (isdigit ((unsigned char) *pc)) {
564 /* mininum date specified */
565 if ((pc = getDate (pc, &min, err)) == NULL) {
566 p_delete(&buffer.data);
570 pc = vskipspaces(pc);
574 pt = skipspaces(pc + 1);
575 untilNow = (*pt == '\0');
579 if (!untilNow) { /* max date or relative range/window */
583 if (!haveMin) { /* save base minimum and set current date, e.g. for "-3d+1d" */
584 time_t now = time (NULL);
585 struct tm *tm = localtime (&now);
587 memcpy (&baseMin, &min, sizeof (baseMin));
588 memcpy (&min, tm, sizeof (min));
589 min.tm_hour = min.tm_sec = min.tm_min = 0;
592 /* preset max date for relative offsets,
593 if nothing follows we search for messages on a specific day */
594 max.tm_year = min.tm_year;
595 max.tm_mon = min.tm_mon;
596 max.tm_mday = min.tm_mday;
598 if (!parse_date_range (pc, &min, &max, haveMin, &baseMin, err)) { /* bail out on any parsing error */
599 p_delete(&buffer.data);
605 /* Since we allow two dates to be specified we'll have to adjust that. */
606 adjust_date_range (&min, &max);
608 pat->min = mutt_mktime (&min, 1);
609 pat->max = mutt_mktime (&max, 1);
611 p_delete(&buffer.data);
616 static struct pattern_flags *lookup_tag (char tag)
620 for (i = 0; Flags[i].tag; i++)
621 if (Flags[i].tag == tag)
626 static /* const */ char *find_matching_paren ( /* const */ char *s)
633 else if (*s == ')') {
642 void mutt_pattern_free (pattern_t ** pat)
656 mutt_pattern_free (&tmp->child);
661 pattern_t *mutt_pattern_comp ( /* const */ char *s, int flags, BUFFER * err)
663 pattern_t *curlist = NULL;
665 pattern_t *last = NULL;
669 int implicit = 1; /* used to detect logical AND operator */
670 struct pattern_flags *entry;
677 ps.dsize = m_strlen(s);
680 ps.dptr = vskipspaces(ps.dptr);
693 snprintf (err->data, err->dsize, _("error in pattern at: %s"),
698 /* A & B | C == (A & B) | C */
699 tmp = new_pattern ();
701 tmp->child = curlist;
717 if (implicit && or) {
718 /* A | B & C == (A | B) & C */
719 tmp = new_pattern ();
721 tmp->child = curlist;
727 tmp = new_pattern ();
729 tmp->alladdr = alladdr;
730 tmp->stringmatch = (*ps.dptr == '=') ? 1 : 0;
740 ps.dptr++; /* move past the ~ */
741 if ((entry = lookup_tag (*ps.dptr)) == NULL) {
742 snprintf (err->data, err->dsize, _("%c: invalid command"), *ps.dptr);
743 mutt_pattern_free (&curlist);
746 if (entry->class && (flags & entry->class) == 0) {
747 snprintf (err->data, err->dsize, _("%c: not supported in this mode"),
749 mutt_pattern_free (&curlist);
754 ps.dptr = vskipspaces(ps.dptr + 1);
756 if (entry->eat_arg) {
758 snprintf (err->data, err->dsize, _("missing parameter"));
759 mutt_pattern_free (&curlist);
762 if (entry->eat_arg (tmp, &ps, err) == -1) {
763 mutt_pattern_free (&curlist);
770 p = find_matching_paren (ps.dptr + 1);
772 snprintf (err->data, err->dsize, _("mismatched parenthesis: %s"),
774 mutt_pattern_free (&curlist);
777 /* compile the sub-expression */
778 buf = p_dupstr(ps.dptr + 1, p - ps.dptr - 1);
779 if ((tmp = mutt_pattern_comp (buf, flags, err)) == NULL) {
781 mutt_pattern_free (&curlist);
791 tmp->alladdr |= alladdr;
794 ps.dptr = p + 1; /* restore location */
797 snprintf (err->data, err->dsize, _("error in pattern at: %s"), ps.dptr);
798 mutt_pattern_free (&curlist);
803 m_strcpy(err->data, err->dsize, _("empty pattern"));
807 tmp = new_pattern ();
808 tmp->op = or ? M_OR : M_AND;
809 tmp->child = curlist;
816 perform_and (pattern_t * pat, pattern_exec_flag flags, CONTEXT * ctx,
819 for (; pat; pat = pat->next)
820 if (mutt_pattern_exec (pat, flags, ctx, hdr) <= 0)
826 perform_or (struct pattern_t *pat, pattern_exec_flag flags, CONTEXT * ctx,
829 for (; pat; pat = pat->next)
830 if (mutt_pattern_exec (pat, flags, ctx, hdr) > 0)
835 static int match_adrlist (pattern_t* pat, int match_personal, int alladdr __attribute__ ((unused)),
843 for (a = va_arg (ap, address_t *); a; a = a->next) {
845 ((a->mailbox && patmatch (pat, a->mailbox) == 0) ||
846 (match_personal && a->personal &&
847 patmatch (pat, a->personal) == 0))) {
849 return (!pat->alladdr); /* Found match, or non-match if alladdr */
854 return pat->alladdr; /* No matches, or all matches if alladdr */
857 static int match_reference (pattern_t* pat, string_list_t * refs)
859 for (; refs; refs = refs->next)
860 if (patmatch (pat, refs->data) == 0)
865 int mutt_is_list_recipient (int alladdr, address_t * a1, address_t * a2)
867 for (; a1; a1 = a1->next)
868 if (alladdr ^ mutt_is_subscribed_list (a1))
870 for (; a2; a2 = a2->next)
871 if (alladdr ^ mutt_is_subscribed_list (a2))
876 int mutt_is_list_cc (int alladdr, address_t * a1, address_t * a2)
878 for (; a1; a1 = a1->next)
879 if (alladdr ^ mutt_is_mail_list (a1))
881 for (; a2; a2 = a2->next)
882 if (alladdr ^ mutt_is_mail_list (a2))
887 static int match_user (int alladdr, address_t * a1, address_t * a2)
889 for (; a1; a1 = a1->next)
890 if (alladdr ^ mutt_addr_is_user (a1))
892 for (; a2; a2 = a2->next)
893 if (alladdr ^ mutt_addr_is_user (a2))
898 /* test if name is considered a real name, i.e. consists of at least 2
899 * space-separated words of which none may end in a dot
901 static int valid_realname (const char *name)
903 const char *p = name;
910 /* skip abbr. parts of names (e.g. 'J. User') */
918 M_MATCH_FULL_ADDRESS match both personal and machine address */
920 mutt_pattern_exec (struct pattern_t *pat, pattern_exec_flag flags,
921 CONTEXT * ctx, HEADER * h)
925 return (pat->not ^ (perform_and (pat->child, flags, ctx, h) > 0));
927 return (pat->not ^ (perform_or (pat->child, flags, ctx, h) > 0));
931 return (pat->not ^ h->expired);
933 return (pat->not ^ h->superseded);
935 return (pat->not ^ h->flagged);
937 return (pat->not ^ h->tagged);
939 return (pat->not ? h->old || h->read : !(h->old || h->read));
941 return (pat->not ? h->read : !h->read);
943 return (pat->not ^ h->replied);
945 return (pat->not ? (!h->old || h->read) : (h->old && !h->read));
947 return (pat->not ^ h->read);
949 return (pat->not ^ h->deleted);
951 return (pat->not ^ (h->msgno >= pat->min - 1 && (pat->max == M_MAXRANGE ||
956 not ^ (h->date_sent >= pat->min && h->date_sent <= pat->max));
957 case M_DATE_RECEIVED:
958 return (pat->not ^ (h->received >= pat->min && h->received <= pat->max));
962 /* IMAP search sets h->matched at search compile time */
963 if (ctx->magic == M_IMAP && pat->stringmatch)
965 return (pat->not ^ msg_search (ctx, pat, h->msgno));
967 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
968 pat->alladdr, 1, h->env->sender));
970 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
971 pat->alladdr, 1, h->env->from));
973 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
974 pat->alladdr, 1, h->env->to));
976 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
977 pat->alladdr, 1, h->env->cc));
980 not ^ (h->env && h->env->subject
981 && patmatch (pat, h->env->subject) == 0));
984 not ^ (h->env && h->env->message_id
985 && patmatch (pat, h->env->message_id) == 0));
987 return (pat->not ^ (h->score >= pat->min && (pat->max == M_MAXRANGE ||
988 h->score <= pat->max)));
991 not ^ (h->content->length >= pat->min
992 && (pat->max == M_MAXRANGE
993 || h->content->length <= pat->max)));
995 return (pat->not ^ match_reference (pat, h->env->references));
999 && match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
1000 pat->alladdr, 4, h->env->from,
1001 h->env->sender, h->env->to,
1006 && match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
1007 pat->alladdr, 2, h->env->to,
1012 && mutt_is_list_cc (pat->alladdr, h->env->to,
1014 case M_SUBSCRIBED_LIST:
1017 && mutt_is_list_recipient (pat->alladdr, h->env->to,
1019 case M_PERSONAL_RECIP:
1022 && match_user (pat->alladdr, h->env->to, h->env->cc)));
1023 case M_PERSONAL_FROM:
1025 not ^ (h->env && match_user (pat->alladdr, h->env->from, NULL)));
1027 return (pat->not ^ (h->collapsed && h->num_hidden > 1));
1029 return (pat->not ^ ((h->security & SIGN) ? 1 : 0));
1030 case M_CRYPT_VERIFIED:
1031 return (pat->not ^ ((h->security & GOODSIGN) ? 1 : 0));
1032 case M_CRYPT_ENCRYPT:
1033 return (pat->not ^ ((h->security & ENCRYPT) ? 1 : 0));
1035 return (pat->not ^ ((h->security & APPLICATION_PGP)
1036 && (h->security & PGPKEY)));
1039 not ^ (h->env->x_label
1040 && patmatch (pat, h->env->x_label) == 0));
1043 not ^ (h->env->spam && h->env->spam->data
1044 && patmatch (pat, h->env->spam->data) == 0));
1046 return (pat->not ^ (h->thread && h->thread->duplicate_thread));
1052 if (h->content->parts)
1053 count = mutt_count_body_parts(h, 0);
1055 mutt_parse_mime_message(ctx, h);
1056 count = mutt_count_body_parts(h, 0);
1057 body_list_wipe(&h->content->parts);
1060 return (pat->not ^ (count >= pat->min && (pat->max == M_MAXRANGE ||
1061 count <= pat->max)));
1064 case M_UNREFERENCED:
1065 return (pat->not ^ (h->thread && !h->thread->child));
1067 return (pat->not ^ (h->content && h->content->type == TYPEMULTIPART));
1070 * we have a match if
1071 * - From: matches $alternates
1072 * - or we have an alias for current address
1073 * - or From: contains valid email address _and_ name has >= 2 fields
1075 return (h->env && h->env->from
1076 && (mutt_addr_is_user(h->env->from)
1077 || alias_reverse_lookup(h->env->from)
1078 || (h->env->from->personal
1079 && valid_realname(h->env-> from->personal)
1080 && h->env->from->mailbox)
1085 not ^ (h->env->newsgroups
1086 && patmatch (pat, h->env->newsgroups) == 0));
1089 mutt_error (_("error: unknown op %d (report this error)."), pat->op);
1093 static void quote_simple (char *tmp, ssize_t len, const char *p)
1098 while (*p && i < len - 3) {
1099 if (*p == '\\' || *p == '"')
1107 /* convert a simple search into a real request */
1108 void mutt_check_simple (char *s, ssize_t len, const char *simple)
1110 char tmp[LONG_STRING];
1112 /* XXX - is ascii_strcasecmp() right here, or should we use locale's
1116 if (!strchr (s, '~') && !strchr (s, '=')) { /* yup, so spoof a real request */
1117 /* convert old tokens into the new format */
1118 if (ascii_strcasecmp ("all", s) == 0 || !m_strcmp("^", s) || !m_strcmp(".", s)) /* ~A is more efficient */
1119 m_strcpy(s, len, "~A");
1120 else if (ascii_strcasecmp ("del", s) == 0)
1121 m_strcpy(s, len, "~D");
1122 else if (ascii_strcasecmp ("flag", s) == 0)
1123 m_strcpy(s, len, "~F");
1124 else if (ascii_strcasecmp ("new", s) == 0)
1125 m_strcpy(s, len, "~N");
1126 else if (ascii_strcasecmp ("old", s) == 0)
1127 m_strcpy(s, len, "~O");
1128 else if (ascii_strcasecmp ("repl", s) == 0)
1129 m_strcpy(s, len, "~Q");
1130 else if (ascii_strcasecmp ("read", s) == 0)
1131 m_strcpy(s, len, "~R");
1132 else if (ascii_strcasecmp ("tag", s) == 0)
1133 m_strcpy(s, len, "~T");
1134 else if (ascii_strcasecmp ("unread", s) == 0)
1135 m_strcpy(s, len, "~U");
1137 quote_simple (tmp, sizeof (tmp), s);
1138 m_file_fmt(s, len, simple, tmp);
1143 int mutt_pattern_func (int op, char *prompt)
1146 char buf[LONG_STRING] = "", *simple, error[STRING];
1150 m_strcpy(buf, sizeof(buf), NONULL(Context->pattern));
1151 if (prompt || op != M_LIMIT)
1152 if (mutt_get_field (prompt, buf, sizeof (buf), M_PATTERN | M_CLEAR) != 0 || !buf[0])
1155 mutt_message _("Compiling search pattern...");
1157 simple = m_strdup(buf);
1158 mutt_check_simple (buf, sizeof (buf), NONULL (SimpleSearch));
1161 err.dsize = sizeof (error);
1162 if ((pat = mutt_pattern_comp (buf, M_FULL_MSG, &err)) == NULL) {
1164 mutt_error ("%s", err.data);
1168 if (Context->magic == M_IMAP && imap_search (Context, pat) < 0)
1171 mutt_message _("Executing command on matching messages...");
1173 #define THIS_BODY Context->hdrs[i]->content
1175 if (op == M_LIMIT) {
1176 Context->vcount = 0;
1178 Context->collapsed = 0;
1180 for (i = 0; i < Context->msgcount; i++) {
1181 /* new limit pattern implicitly uncollapses all threads */
1182 Context->hdrs[i]->virtual = -1;
1183 Context->hdrs[i]->limited = 0;
1184 Context->hdrs[i]->collapsed = 0;
1185 Context->hdrs[i]->num_hidden = 0;
1186 if (mutt_pattern_exec
1187 (pat, M_MATCH_FULL_ADDRESS, Context, Context->hdrs[i])) {
1188 Context->hdrs[i]->virtual = Context->vcount;
1189 Context->hdrs[i]->limited = 1;
1190 Context->v2r[Context->vcount] = i;
1192 Context->vsize += THIS_BODY->length + THIS_BODY->offset -
1193 THIS_BODY->hdr_offset;
1198 for (i = 0; i < Context->vcount; i++) {
1199 if (mutt_pattern_exec
1200 (pat, M_MATCH_FULL_ADDRESS, Context,
1201 Context->hdrs[Context->v2r[i]])) {
1204 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_PURGED,
1207 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_DELETE,
1212 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_TAG,
1222 mutt_clear_error ();
1224 if (op == M_LIMIT) {
1225 /* drop previous limit pattern */
1226 p_delete(&Context->pattern);
1227 if (Context->limit_pattern)
1228 mutt_pattern_free (&Context->limit_pattern);
1229 if (Context->msgcount && !Context->vcount) {
1230 mutt_error _("No messages matched criteria.");
1233 /* record new limit pattern, unless match all */
1234 if (m_strncmp(buf, "~A", 2) != 0) {
1235 Context->pattern = simple;
1236 simple = NULL; /* don't clobber it */
1237 Context->limit_pattern = mutt_pattern_comp (buf, M_FULL_MSG, &err);
1241 mutt_pattern_free (&pat);
1245 int mutt_search_command (int cur, int op)
1249 char temp[LONG_STRING];
1255 if (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE) {
1256 m_strcpy(buf, sizeof(buf), LastSearch);
1257 if (mutt_get_field ((op == OP_SEARCH) ? _("Search for: ") :
1258 _("Reverse search for: "), buf, sizeof (buf),
1259 M_CLEAR | M_PATTERN) != 0 || !buf[0])
1262 if (op == OP_SEARCH)
1263 unset_option (OPTSEARCHREVERSE);
1265 set_option (OPTSEARCHREVERSE);
1267 /* compare the *expanded* version of the search pattern in case
1268 $simple_search has changed while we were searching */
1269 m_strcpy(temp, sizeof(temp), buf);
1270 mutt_check_simple (temp, sizeof (temp), NONULL (SimpleSearch));
1272 if (!SearchPattern || m_strcmp(temp, LastSearchExpn)) {
1273 set_option (OPTSEARCHINVALID);
1274 m_strcpy(LastSearch, sizeof(LastSearch), buf);
1275 mutt_message _("Compiling search pattern...");
1277 mutt_pattern_free (&SearchPattern);
1279 err.dsize = sizeof (error);
1280 if ((SearchPattern =
1281 mutt_pattern_comp (temp, M_FULL_MSG, &err)) == NULL) {
1282 mutt_error ("%s", error);
1285 mutt_clear_error ();
1288 else if (!SearchPattern) {
1289 mutt_error _("No search pattern.");
1294 if (option (OPTSEARCHINVALID)) {
1295 for (i = 0; i < Context->msgcount; i++)
1296 Context->hdrs[i]->searched = 0;
1297 if (Context->magic == M_IMAP && imap_search (Context, SearchPattern) < 0)
1299 unset_option (OPTSEARCHINVALID);
1302 incr = (option (OPTSEARCHREVERSE)) ? -1 : 1;
1303 if (op == OP_SEARCH_OPPOSITE)
1306 for (i = cur + incr, j = 0; j != Context->vcount; j++) {
1307 if (i > Context->vcount - 1) {
1309 if (option (OPTWRAPSEARCH))
1310 mutt_message (_("Search wrapped to top."));
1313 mutt_message _("Search hit bottom without finding match");
1319 i = Context->vcount - 1;
1320 if (option (OPTWRAPSEARCH))
1321 mutt_message (_("Search wrapped to bottom."));
1324 mutt_message _("Search hit top without finding match");
1330 h = Context->hdrs[Context->v2r[i]];
1332 /* if we've already evaulated this message, use the cached value */
1337 /* remember that we've already searched this message */
1341 (SearchPattern, M_MATCH_FULL_ADDRESS, Context, h) > 0)))
1346 mutt_error _("Search interrupted.");
1355 mutt_error _("Not found.");