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-crypt/crypt.h>
14 #include <lib-ui/enter.h>
15 #include <lib-ui/curses.h>
16 #include <lib-mx/mx.h>
17 #include <imap/imap.h>
25 static int eat_regexp (pattern_t * pat, BUFFER *, BUFFER *);
26 static int eat_date (pattern_t * pat, BUFFER *, BUFFER *);
27 static int eat_range (pattern_t * pat, BUFFER *, BUFFER *);
28 static int patmatch (const pattern_t* pat, const char* buf);
30 static struct pattern_flags {
31 int tag; /* character used to represent this op */
32 int op; /* operation to perform */
34 int (*eat_arg)(pattern_t *, BUFFER *, BUFFER *);
36 {'A', M_ALL, 0, NULL},
37 {'b', M_BODY, M_FULL_MSG, eat_regexp},
38 {'B', M_WHOLE_MSG, M_FULL_MSG, eat_regexp},
39 {'c', M_CC, 0, eat_regexp},
40 {'C', M_RECIPIENT, 0, eat_regexp},
41 {'d', M_DATE, 0, eat_date},
42 {'D', M_DELETED, 0, NULL},
43 {'e', M_SENDER, 0, eat_regexp},
44 {'E', M_EXPIRED, 0, NULL},
45 {'f', M_FROM, 0, eat_regexp},
46 {'F', M_FLAG, 0, NULL},
47 {'g', M_CRYPT_SIGN, 0, NULL},
48 {'G', M_CRYPT_ENCRYPT, 0, NULL},
49 {'h', M_HEADER, M_FULL_MSG, eat_regexp},
50 {'H', M_HORMEL, 0, eat_regexp},
51 {'i', M_ID, 0, eat_regexp},
52 {'k', M_PGP_KEY, 0, NULL},
53 {'L', M_ADDRESS, 0, eat_regexp},
54 {'l', M_LIST, 0, NULL},
55 {'m', M_MESSAGE, 0, eat_range},
56 {'M', M_MULTIPART, 0, NULL},
57 {'n', M_SCORE, 0, eat_range},
58 {'N', M_NEW, 0, NULL},
59 {'O', M_OLD, 0, NULL},
60 {'p', M_PERSONAL_RECIP, 0, NULL},
61 {'P', M_PERSONAL_FROM, 0, NULL},
62 {'Q', M_REPLIED, 0, NULL},
63 {'R', M_READ, 0, NULL},
64 {'r', M_DATE_RECEIVED, 0, eat_date},
65 {'s', M_SUBJECT, 0, eat_regexp},
66 {'S', M_SUPERSEDED, 0, NULL},
67 {'T', M_TAG, 0, NULL},
68 {'t', M_TO, 0, eat_regexp},
69 {'U', M_UNREAD, 0, NULL},
70 {'u', M_SUBSCRIBED_LIST, 0, NULL},
71 {'v', M_COLLAPSED, 0, NULL},
72 {'V', M_CRYPT_VERIFIED, 0, NULL},
74 {'w', M_NEWSGROUPS, 0, eat_regexp},
76 {'x', M_REFERENCE, 0, eat_regexp},
77 {'X', M_MIMEATTACH, 0, eat_range},
78 {'y', M_XLABEL, 0, eat_regexp},
79 {'z', M_SIZE, 0, eat_range},
80 {'=', M_DUPLICATED, 0, NULL},
81 {'$', M_UNREFERENCED, 0, NULL},
82 {'*', M_REALNAME, 0, NULL},
86 static pattern_t *SearchPattern = NULL; /* current search pattern */
87 static char LastSearch[STRING] = { 0 }; /* last pattern searched for */
88 static char LastSearchExpn[LONG_STRING] = { 0 }; /* expanded version of
93 /* constants for parse_date_range() */
94 #define M_PDR_NONE 0x0000
95 #define M_PDR_MINUS 0x0001
96 #define M_PDR_PLUS 0x0002
97 #define M_PDR_WINDOW 0x0004
98 #define M_PDR_ABSOLUTE 0x0008
99 #define M_PDR_DONE 0x0010
100 #define M_PDR_ERROR 0x0100
101 #define M_PDR_ERRORDONE (M_PDR_ERROR | M_PDR_DONE)
103 void pattern_wipe(pattern_t *pat)
110 pattern_list_wipe(&pat->child);
113 /* if no uppercase letters are given, do a case-insensitive search */
114 int mutt_which_case (const char *s)
117 if (isalpha ((unsigned char) *s) && isupper ((unsigned char) *s))
118 return 0; /* case-sensitive */
121 return REG_ICASE; /* case-insensitive */
125 msg_search (CONTEXT *ctx, pattern_t* pat, int msgno)
127 char tempfile[_POSIX_PATH_MAX];
134 HEADER *h = ctx->hdrs[msgno];
138 if ((msg = mx_open_message (ctx, msgno)) != NULL) {
139 if (option (OPTTHOROUGHSRC)) {
140 /* decode the header / body */
143 s.flags = M_CHARCONV;
144 s.fpout = m_tempfile(tempfile, sizeof(tempfile), NONULL(Tempdir), NULL);
146 mutt_error(_("Could not create temporary file"));
150 if (pat->op != M_BODY)
151 mutt_copy_header (msg->fp, h, s.fpout, CH_FROM | CH_DECODE, NULL);
153 if (pat->op != M_HEADER) {
154 mutt_parse_mime_message (ctx, h);
156 if ((h->security & ENCRYPT) && !crypt_valid_passphrase (h->security))
158 mx_close_message (&msg);
166 fseeko (msg->fp, h->offset, 0);
167 mutt_body_handler (h->content, &s);
173 fstat (fileno (fp), &st);
174 lng = (long) st.st_size;
177 /* raw header / body */
179 if (pat->op != M_BODY) {
180 fseeko (fp, h->offset, 0);
181 lng = h->content->offset - h->offset;
183 if (pat->op != M_HEADER) {
184 if (pat->op == M_BODY)
185 fseeko (fp, h->content->offset, 0);
186 lng += h->content->length;
190 buf = p_new(char, blen = STRING);
192 /* search the file "fp" */
194 if (pat->op == M_HEADER) {
195 if (!mutt_read_rfc822_line(fp, &buf, &blen))
197 } else if (fgets (buf, blen - 1, fp) == NULL)
198 break; /* don't loop forever */
199 if (patmatch (pat, buf) == 0) {
203 lng -= m_strlen(buf);
208 mx_close_message (&msg);
210 if (option (OPTTHOROUGHSRC)) {
219 int eat_regexp (pattern_t * pat, BUFFER * s, BUFFER * err)
226 if (mutt_extract_token (&buf, s, M_TOKEN_PATTERN | M_TOKEN_COMMENT) != 0 ||
228 snprintf (err->data, err->dsize, _("Error in expression: %s"), s->dptr);
233 snprintf (err->data, err->dsize, _("Empty expression"));
237 if (pat->stringmatch) {
238 pat->str = m_strdup(buf.data);
241 pat->rx = p_new(regex_t, 1);
242 r = REGCOMP (pat->rx, buf.data, REG_NEWLINE | REG_NOSUB | mutt_which_case (buf.data));
245 regerror (r, pat->rx, err->data, err->dsize);
254 static int patmatch (const pattern_t* pat, const char* buf) {
255 if (pat->stringmatch)
256 return !strstr (buf, pat->str);
258 return regexec (pat->rx, buf, 0, NULL, 0);
261 int eat_range (pattern_t * pat, BUFFER * s, BUFFER * err __attribute__ ((unused)))
264 int do_exclusive = 0;
268 * If simple_search is set to "~m %s", the range will have double quotes
271 if (*s->dptr == '"') {
277 if ((*s->dptr != '-') && (*s->dptr != '<')) {
279 if (*s->dptr == '>') {
280 pat->max = M_MAXRANGE;
281 pat->min = strtol (s->dptr + 1, &tmp, 0) + 1; /* exclusive range */
284 pat->min = strtol (s->dptr, &tmp, 0);
285 if (toupper ((unsigned char) *tmp) == 'K') { /* is there a prefix? */
289 else if (toupper ((unsigned char) *tmp) == 'M') {
293 if (*s->dptr == '>') {
310 if (isdigit ((unsigned char) *tmp)) {
312 pat->max = strtol (tmp, &tmp, 0);
313 if (toupper ((unsigned char) *tmp) == 'K') {
317 else if (toupper ((unsigned char) *tmp) == 'M') {
325 pat->max = M_MAXRANGE;
327 if (skip_quote && *tmp == '"')
330 s->dptr = vskipspaces(tmp);
334 static const char *getDate (const char *s, struct tm *t, BUFFER * err)
337 time_t now = time (NULL);
338 struct tm *tm = localtime (&now);
340 t->tm_mday = strtol (s, &p, 10);
341 if (t->tm_mday < 1 || t->tm_mday > 31) {
342 snprintf (err->data, err->dsize, _("Invalid day of month: %s"), s);
346 /* fill in today's month and year */
347 t->tm_mon = tm->tm_mon;
348 t->tm_year = tm->tm_year;
352 t->tm_mon = strtol (p, &p, 10) - 1;
353 if (t->tm_mon < 0 || t->tm_mon > 11) {
354 snprintf (err->data, err->dsize, _("Invalid month: %s"), p);
358 t->tm_year = tm->tm_year;
362 t->tm_year = strtol (p, &p, 10);
363 if (t->tm_year < 70) /* year 2000+ */
365 else if (t->tm_year > 1900)
374 static const char *get_offset (struct tm *tm, const char *s, int sign)
377 int offset = strtol (s, &ps, 0);
379 if ((sign < 0 && offset > 0) || (sign > 0 && offset < 0))
384 tm->tm_year += offset;
387 tm->tm_mon += offset;
390 tm->tm_mday += 7 * offset;
393 tm->tm_mday += offset;
398 mutt_normalize_time (tm);
402 static void adjust_date_range (struct tm *min, struct tm *max)
404 if (min->tm_year > max->tm_year
405 || (min->tm_year == max->tm_year && min->tm_mon > max->tm_mon)
406 || (min->tm_year == max->tm_year && min->tm_mon == max->tm_mon
407 && min->tm_mday > max->tm_mday)) {
411 min->tm_year = max->tm_year;
415 min->tm_mon = max->tm_mon;
419 min->tm_mday = max->tm_mday;
422 min->tm_hour = min->tm_min = min->tm_sec = 0;
424 max->tm_min = max->tm_sec = 59;
428 static const char *parse_date_range (const char *pc, struct tm *min,
429 struct tm *max, int haveMin,
430 struct tm *baseMin, BUFFER * err)
432 int flag = M_PDR_NONE;
434 while (*pc && ((flag & M_PDR_DONE) == 0)) {
438 pc = vskipspaces(pc);
442 /* try a range of absolute date minus offset of Ndwmy */
443 pt = get_offset (min, pc, -1);
445 if (flag == M_PDR_NONE) { /* nothing yet and no offset parsed => absolute date? */
446 if (!getDate (pc, max, err))
447 flag |= (M_PDR_ABSOLUTE | M_PDR_ERRORDONE); /* done bad */
449 /* reestablish initial base minimum if not specified */
451 memcpy (min, baseMin, sizeof (struct tm));
452 flag |= (M_PDR_ABSOLUTE | M_PDR_DONE); /* done good */
456 flag |= M_PDR_ERRORDONE;
460 if (flag == M_PDR_NONE && !haveMin) { /* the very first "-3d" without a previous absolute date */
461 max->tm_year = min->tm_year;
462 max->tm_mon = min->tm_mon;
463 max->tm_mday = min->tm_mday;
470 { /* enlarge plusRange */
471 pt = get_offset (max, pc, 1);
473 flag |= M_PDR_ERRORDONE;
481 { /* enlarge window in both directions */
482 pt = get_offset (min, pc, -1);
484 flag |= M_PDR_ERRORDONE;
486 pc = get_offset (max, pc, 1);
487 flag |= M_PDR_WINDOW;
492 flag |= M_PDR_ERRORDONE;
494 pc = vskipspaces(pc);
496 if ((flag & M_PDR_ERROR) && !(flag & M_PDR_ABSOLUTE)) { /* getDate has its own error message, don't overwrite it here */
497 snprintf (err->data, err->dsize, _("Invalid relative date: %s"), pc - 1);
499 return ((flag & M_PDR_ERROR) ? NULL : pc);
502 static int eat_date (pattern_t * pat, BUFFER * s, BUFFER * err)
508 if (mutt_extract_token (&buffer, s, M_TOKEN_COMMENT | M_TOKEN_PATTERN) != 0
510 m_strcpy(err->data, err->dsize, _("error in expression"));
515 /* the `0' time is Jan 1, 1970 UTC, so in order to prevent a negative time
516 when doing timezone conversion, we use Jan 2, 1970 UTC as the base
523 /* Arbitrary year in the future. Don't set this too high
524 or mutt_mktime() returns something larger than will
525 fit in a time_t on some systems */
533 if (strchr ("<>=", buffer.data[0])) {
534 /* offset from current time
535 <3d less than three days ago
536 >3d more than three days ago
537 =3d exactly three days ago */
538 time_t now = time (NULL);
539 struct tm *tm = localtime (&now);
542 if (buffer.data[0] == '<') {
543 memcpy (&min, tm, sizeof (min));
547 memcpy (&max, tm, sizeof (max));
550 if (buffer.data[0] == '=')
554 tm->tm_min = tm->tm_sec = 59;
556 /* force negative offset */
557 get_offset (tm, buffer.data + 1, -1);
560 /* start at the beginning of the day in question */
561 memcpy (&min, &max, sizeof (max));
562 min.tm_hour = min.tm_sec = min.tm_min = 0;
566 const char *pc = buffer.data;
569 int untilNow = FALSE;
571 if (isdigit ((unsigned char) *pc)) {
572 /* mininum date specified */
573 if ((pc = getDate (pc, &min, err)) == NULL) {
574 p_delete(&buffer.data);
578 pc = vskipspaces(pc);
582 pt = skipspaces(pc + 1);
583 untilNow = (*pt == '\0');
587 if (!untilNow) { /* max date or relative range/window */
591 if (!haveMin) { /* save base minimum and set current date, e.g. for "-3d+1d" */
592 time_t now = time (NULL);
593 struct tm *tm = localtime (&now);
595 memcpy (&baseMin, &min, sizeof (baseMin));
596 memcpy (&min, tm, sizeof (min));
597 min.tm_hour = min.tm_sec = min.tm_min = 0;
600 /* preset max date for relative offsets,
601 if nothing follows we search for messages on a specific day */
602 max.tm_year = min.tm_year;
603 max.tm_mon = min.tm_mon;
604 max.tm_mday = min.tm_mday;
606 if (!parse_date_range (pc, &min, &max, haveMin, &baseMin, err)) { /* bail out on any parsing error */
607 p_delete(&buffer.data);
613 /* Since we allow two dates to be specified we'll have to adjust that. */
614 adjust_date_range (&min, &max);
616 pat->min = mutt_mktime (&min, 1);
617 pat->max = mutt_mktime (&max, 1);
619 p_delete(&buffer.data);
624 static const struct pattern_flags *lookup_tag (char tag)
628 for (i = 0; Flags[i].tag; i++)
629 if (Flags[i].tag == tag)
634 static /* const */ char *find_matching_paren ( /* const */ char *s)
641 else if (*s == ')') {
650 pattern_t *mutt_pattern_comp(const char *s, int flags, BUFFER *err)
652 pattern_t *curlist = NULL;
654 pattern_t *last = NULL;
658 int implicit = 1; /* used to detect logical AND operator */
659 const struct pattern_flags *entry;
666 ps.dsize = m_strlen(s);
669 ps.dptr = vskipspaces(ps.dptr);
682 snprintf (err->data, err->dsize, _("error in pattern at: %s"),
687 /* A & B | C == (A & B) | C */
690 tmp->child = curlist;
706 if (implicit && or) {
707 /* A | B & C == (A | B) & C */
710 tmp->child = curlist;
718 tmp->alladdr = alladdr;
719 tmp->stringmatch = (*ps.dptr == '=') ? 1 : 0;
729 ps.dptr++; /* move past the ~ */
730 if ((entry = lookup_tag (*ps.dptr)) == NULL) {
731 snprintf (err->data, err->dsize, _("%c: invalid command"), *ps.dptr);
732 pattern_list_wipe(&curlist);
735 if (entry->class && (flags & entry->class) == 0) {
736 snprintf (err->data, err->dsize, _("%c: not supported in this mode"),
738 pattern_list_wipe(&curlist);
743 ps.dptr = vskipspaces(ps.dptr + 1);
745 if (entry->eat_arg) {
747 snprintf (err->data, err->dsize, _("missing parameter"));
748 pattern_list_wipe(&curlist);
751 if (entry->eat_arg (tmp, &ps, err) == -1) {
752 pattern_list_wipe(&curlist);
759 p = find_matching_paren (ps.dptr + 1);
761 snprintf (err->data, err->dsize, _("mismatched parenthesis: %s"),
763 pattern_list_wipe(&curlist);
766 /* compile the sub-expression */
767 buf = p_dupstr(ps.dptr + 1, p - ps.dptr - 1);
768 if ((tmp = mutt_pattern_comp (buf, flags, err)) == NULL) {
770 pattern_list_wipe(&curlist);
780 tmp->alladdr |= alladdr;
783 ps.dptr = p + 1; /* restore location */
786 snprintf (err->data, err->dsize, _("error in pattern at: %s"), ps.dptr);
787 pattern_list_wipe(&curlist);
792 m_strcpy(err->data, err->dsize, _("empty pattern"));
797 tmp->op = or ? M_OR : M_AND;
798 tmp->child = curlist;
805 perform_and (pattern_t * pat, pattern_exec_flag flags, CONTEXT * ctx,
808 for (; pat; pat = pat->next)
809 if (mutt_pattern_exec (pat, flags, ctx, hdr) <= 0)
815 perform_or (struct pattern_t *pat, pattern_exec_flag flags, CONTEXT * ctx,
818 for (; pat; pat = pat->next)
819 if (mutt_pattern_exec (pat, flags, ctx, hdr) > 0)
824 static int match_adrlist (pattern_t* pat, int match_personal, int alladdr __attribute__ ((unused)),
832 for (a = va_arg (ap, address_t *); a; a = a->next) {
834 ((a->mailbox && patmatch (pat, a->mailbox) == 0) ||
835 (match_personal && a->personal &&
836 patmatch (pat, a->personal) == 0))) {
838 return (!pat->alladdr); /* Found match, or non-match if alladdr */
843 return pat->alladdr; /* No matches, or all matches if alladdr */
846 static int match_reference (pattern_t* pat, string_list_t * refs)
848 for (; refs; refs = refs->next)
849 if (patmatch (pat, refs->data) == 0)
854 int mutt_is_list_recipient (int alladdr, address_t * a1, address_t * a2)
856 for (; a1; a1 = a1->next)
857 if (alladdr ^ mutt_is_subscribed_list (a1))
859 for (; a2; a2 = a2->next)
860 if (alladdr ^ mutt_is_subscribed_list (a2))
865 int mutt_is_list_cc (int alladdr, address_t * a1, address_t * a2)
867 for (; a1; a1 = a1->next)
868 if (alladdr ^ mutt_is_mail_list (a1))
870 for (; a2; a2 = a2->next)
871 if (alladdr ^ mutt_is_mail_list (a2))
876 static int match_user (int alladdr, address_t * a1, address_t * a2)
878 for (; a1; a1 = a1->next)
879 if (alladdr ^ mutt_addr_is_user (a1))
881 for (; a2; a2 = a2->next)
882 if (alladdr ^ mutt_addr_is_user (a2))
887 /* test if name is considered a real name, i.e. consists of at least 2
888 * space-separated words of which none may end in a dot
890 static int valid_realname (const char *name)
892 const char *p = name;
899 /* skip abbr. parts of names (e.g. 'J. User') */
907 M_MATCH_FULL_ADDRESS match both personal and machine address */
909 mutt_pattern_exec (struct pattern_t *pat, pattern_exec_flag flags,
910 CONTEXT * ctx, HEADER * h)
914 return (pat->not ^ (perform_and (pat->child, flags, ctx, h) > 0));
916 return (pat->not ^ (perform_or (pat->child, flags, ctx, h) > 0));
920 return (pat->not ^ h->expired);
922 return (pat->not ^ h->superseded);
924 return (pat->not ^ h->flagged);
926 return (pat->not ^ h->tagged);
928 return (pat->not ? h->old || h->read : !(h->old || h->read));
930 return (pat->not ? h->read : !h->read);
932 return (pat->not ^ h->replied);
934 return (pat->not ? (!h->old || h->read) : (h->old && !h->read));
936 return (pat->not ^ h->read);
938 return (pat->not ^ h->deleted);
940 return (pat->not ^ (h->msgno >= pat->min - 1 && (pat->max == M_MAXRANGE ||
945 not ^ (h->date_sent >= pat->min && h->date_sent <= pat->max));
946 case M_DATE_RECEIVED:
947 return (pat->not ^ (h->received >= pat->min && h->received <= pat->max));
951 /* IMAP search sets h->matched at search compile time */
952 if (ctx->magic == M_IMAP && pat->stringmatch)
954 return (pat->not ^ msg_search (ctx, pat, h->msgno));
956 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
957 pat->alladdr, 1, h->env->sender));
959 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
960 pat->alladdr, 1, h->env->from));
962 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
963 pat->alladdr, 1, h->env->to));
965 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
966 pat->alladdr, 1, h->env->cc));
969 not ^ (h->env && h->env->subject
970 && patmatch (pat, h->env->subject) == 0));
973 not ^ (h->env && h->env->message_id
974 && patmatch (pat, h->env->message_id) == 0));
976 return (pat->not ^ (h->score >= pat->min && (pat->max == M_MAXRANGE ||
977 h->score <= pat->max)));
980 not ^ (h->content->length >= pat->min
981 && (pat->max == M_MAXRANGE
982 || h->content->length <= pat->max)));
984 return (pat->not ^ match_reference (pat, h->env->references));
988 && match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
989 pat->alladdr, 4, h->env->from,
990 h->env->sender, h->env->to,
995 && match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
996 pat->alladdr, 2, h->env->to,
1001 && mutt_is_list_cc (pat->alladdr, h->env->to,
1003 case M_SUBSCRIBED_LIST:
1006 && mutt_is_list_recipient (pat->alladdr, h->env->to,
1008 case M_PERSONAL_RECIP:
1011 && match_user (pat->alladdr, h->env->to, h->env->cc)));
1012 case M_PERSONAL_FROM:
1014 not ^ (h->env && match_user (pat->alladdr, h->env->from, NULL)));
1016 return (pat->not ^ (h->collapsed && h->num_hidden > 1));
1018 return (pat->not ^ ((h->security & SIGN) ? 1 : 0));
1019 case M_CRYPT_VERIFIED:
1020 return (pat->not ^ ((h->security & GOODSIGN) ? 1 : 0));
1021 case M_CRYPT_ENCRYPT:
1022 return (pat->not ^ ((h->security & ENCRYPT) ? 1 : 0));
1024 return (pat->not ^ ((h->security & APPLICATION_PGP)
1025 && (h->security & PGPKEY)));
1028 not ^ (h->env->x_label
1029 && patmatch (pat, h->env->x_label) == 0));
1032 not ^ (h->env->spam && h->env->spam->data
1033 && patmatch (pat, h->env->spam->data) == 0));
1035 return (pat->not ^ (h->thread && h->thread->duplicate_thread));
1041 if (h->content->parts)
1042 count = mutt_count_body_parts(h, 0);
1044 mutt_parse_mime_message(ctx, h);
1045 count = mutt_count_body_parts(h, 0);
1046 body_list_wipe(&h->content->parts);
1049 return (pat->not ^ (count >= pat->min && (pat->max == M_MAXRANGE ||
1050 count <= pat->max)));
1053 case M_UNREFERENCED:
1054 return (pat->not ^ (h->thread && !h->thread->child));
1056 return (pat->not ^ (h->content && h->content->type == TYPEMULTIPART));
1059 * we have a match if
1060 * - From: matches $alternates
1061 * - or we have an alias for current address
1062 * - or From: contains valid email address _and_ name has >= 2 fields
1064 return (h->env && h->env->from
1065 && (mutt_addr_is_user(h->env->from)
1066 || alias_reverse_lookup(h->env->from)
1067 || (h->env->from->personal
1068 && valid_realname(h->env-> from->personal)
1069 && h->env->from->mailbox)
1074 not ^ (h->env->newsgroups
1075 && patmatch (pat, h->env->newsgroups) == 0));
1078 mutt_error (_("error: unknown op %d (report this error)."), pat->op);
1082 static void quote_simple (char *tmp, ssize_t len, const char *p)
1087 while (*p && i < len - 3) {
1088 if (*p == '\\' || *p == '"')
1096 /* convert a simple search into a real request */
1097 void mutt_check_simple (char *s, ssize_t len, const char *simple)
1099 char tmp[LONG_STRING];
1101 /* XXX - is ascii_strcasecmp() right here, or should we use locale's
1105 if (!strchr (s, '~') && !strchr (s, '=')) { /* yup, so spoof a real request */
1106 /* convert old tokens into the new format */
1107 if (ascii_strcasecmp ("all", s) == 0 || !m_strcmp("^", s) || !m_strcmp(".", s)) /* ~A is more efficient */
1108 m_strcpy(s, len, "~A");
1109 else if (ascii_strcasecmp ("del", s) == 0)
1110 m_strcpy(s, len, "~D");
1111 else if (ascii_strcasecmp ("flag", s) == 0)
1112 m_strcpy(s, len, "~F");
1113 else if (ascii_strcasecmp ("new", s) == 0)
1114 m_strcpy(s, len, "~N");
1115 else if (ascii_strcasecmp ("old", s) == 0)
1116 m_strcpy(s, len, "~O");
1117 else if (ascii_strcasecmp ("repl", s) == 0)
1118 m_strcpy(s, len, "~Q");
1119 else if (ascii_strcasecmp ("read", s) == 0)
1120 m_strcpy(s, len, "~R");
1121 else if (ascii_strcasecmp ("tag", s) == 0)
1122 m_strcpy(s, len, "~T");
1123 else if (ascii_strcasecmp ("unread", s) == 0)
1124 m_strcpy(s, len, "~U");
1126 quote_simple (tmp, sizeof (tmp), s);
1127 m_file_fmt(s, len, simple, tmp);
1132 int mutt_pattern_func (int op, char *prompt)
1135 char buf[LONG_STRING] = "", *simple, error[STRING];
1139 m_strcpy(buf, sizeof(buf), NONULL(Context->pattern));
1140 if (prompt || op != M_LIMIT)
1141 if (mutt_get_field (prompt, buf, sizeof (buf), M_PATTERN | M_CLEAR) != 0 || !buf[0])
1144 mutt_message _("Compiling search pattern...");
1146 simple = m_strdup(buf);
1147 mutt_check_simple (buf, sizeof (buf), NONULL (SimpleSearch));
1150 err.dsize = sizeof (error);
1151 if ((pat = mutt_pattern_comp (buf, M_FULL_MSG, &err)) == NULL) {
1153 mutt_error ("%s", err.data);
1157 if (Context->magic == M_IMAP && imap_search (Context, pat) < 0)
1160 mutt_message _("Executing command on matching messages...");
1162 #define THIS_BODY Context->hdrs[i]->content
1164 if (op == M_LIMIT) {
1165 Context->vcount = 0;
1167 Context->collapsed = 0;
1169 for (i = 0; i < Context->msgcount; i++) {
1170 /* new limit pattern implicitly uncollapses all threads */
1171 Context->hdrs[i]->virtual = -1;
1172 Context->hdrs[i]->limited = 0;
1173 Context->hdrs[i]->collapsed = 0;
1174 Context->hdrs[i]->num_hidden = 0;
1175 if (mutt_pattern_exec
1176 (pat, M_MATCH_FULL_ADDRESS, Context, Context->hdrs[i])) {
1177 Context->hdrs[i]->virtual = Context->vcount;
1178 Context->hdrs[i]->limited = 1;
1179 Context->v2r[Context->vcount] = i;
1181 Context->vsize += THIS_BODY->length + THIS_BODY->offset -
1182 THIS_BODY->hdr_offset;
1187 for (i = 0; i < Context->vcount; i++) {
1188 if (mutt_pattern_exec
1189 (pat, M_MATCH_FULL_ADDRESS, Context,
1190 Context->hdrs[Context->v2r[i]])) {
1193 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_PURGED,
1196 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_DELETE,
1201 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_TAG,
1211 mutt_clear_error ();
1213 if (op == M_LIMIT) {
1214 /* drop previous limit pattern */
1215 p_delete(&Context->pattern);
1216 if (Context->limit_pattern)
1217 pattern_list_wipe(&Context->limit_pattern);
1218 if (Context->msgcount && !Context->vcount) {
1219 mutt_error _("No messages matched criteria.");
1222 /* record new limit pattern, unless match all */
1223 if (m_strncmp(buf, "~A", 2) != 0) {
1224 Context->pattern = simple;
1225 simple = NULL; /* don't clobber it */
1226 Context->limit_pattern = mutt_pattern_comp (buf, M_FULL_MSG, &err);
1230 pattern_list_wipe(&pat);
1234 int mutt_search_command (int cur, int op)
1238 char temp[LONG_STRING];
1244 if (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE) {
1245 m_strcpy(buf, sizeof(buf), LastSearch);
1246 if (mutt_get_field ((op == OP_SEARCH) ? _("Search for: ") :
1247 _("Reverse search for: "), buf, sizeof (buf),
1248 M_CLEAR | M_PATTERN) != 0 || !buf[0])
1251 if (op == OP_SEARCH)
1252 unset_option (OPTSEARCHREVERSE);
1254 set_option (OPTSEARCHREVERSE);
1256 /* compare the *expanded* version of the search pattern in case
1257 $simple_search has changed while we were searching */
1258 m_strcpy(temp, sizeof(temp), buf);
1259 mutt_check_simple (temp, sizeof (temp), NONULL (SimpleSearch));
1261 if (!SearchPattern || m_strcmp(temp, LastSearchExpn)) {
1262 set_option (OPTSEARCHINVALID);
1263 m_strcpy(LastSearch, sizeof(LastSearch), buf);
1264 mutt_message _("Compiling search pattern...");
1266 pattern_list_wipe(&SearchPattern);
1268 err.dsize = sizeof (error);
1269 if ((SearchPattern =
1270 mutt_pattern_comp (temp, M_FULL_MSG, &err)) == NULL) {
1271 mutt_error ("%s", error);
1274 mutt_clear_error ();
1277 else if (!SearchPattern) {
1278 mutt_error _("No search pattern.");
1283 if (option (OPTSEARCHINVALID)) {
1284 for (i = 0; i < Context->msgcount; i++)
1285 Context->hdrs[i]->searched = 0;
1286 if (Context->magic == M_IMAP && imap_search (Context, SearchPattern) < 0)
1288 unset_option (OPTSEARCHINVALID);
1291 incr = (option (OPTSEARCHREVERSE)) ? -1 : 1;
1292 if (op == OP_SEARCH_OPPOSITE)
1295 for (i = cur + incr, j = 0; j != Context->vcount; j++) {
1296 if (i > Context->vcount - 1) {
1298 if (option (OPTWRAPSEARCH))
1299 mutt_message (_("Search wrapped to top."));
1302 mutt_message _("Search hit bottom without finding match");
1308 i = Context->vcount - 1;
1309 if (option (OPTWRAPSEARCH))
1310 mutt_message (_("Search wrapped to bottom."));
1313 mutt_message _("Search hit top without finding match");
1319 h = Context->hdrs[Context->v2r[i]];
1321 /* if we've already evaulated this message, use the cached value */
1326 /* remember that we've already searched this message */
1330 (SearchPattern, M_MATCH_FULL_ADDRESS, Context, h) > 0)))
1335 mutt_error _("Search interrupted.");
1344 mutt_error _("Not found.");