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/lib-ui.h>
15 #include <lib-mx/mx.h>
16 #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(mod_core.tmpdir), 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);
155 fseeko (msg->fp, h->offset, 0);
156 mutt_body_handler (h->content, &s);
162 fstat (fileno (fp), &st);
163 lng = (long) st.st_size;
166 /* raw header / body */
168 if (pat->op != M_BODY) {
169 fseeko (fp, h->offset, 0);
170 lng = h->content->offset - h->offset;
172 if (pat->op != M_HEADER) {
173 if (pat->op == M_BODY)
174 fseeko (fp, h->content->offset, 0);
175 lng += h->content->length;
179 buf = p_new(char, blen = STRING);
181 /* search the file "fp" */
183 if (pat->op == M_HEADER) {
184 if (!mutt_read_rfc822_line(fp, &buf, &blen))
186 } else if (fgets (buf, blen - 1, fp) == NULL)
187 break; /* don't loop forever */
188 if (patmatch (pat, buf) == 0) {
192 lng -= m_strlen(buf);
197 mx_close_message (&msg);
199 if (option (OPTTHOROUGHSRC)) {
208 int eat_regexp (pattern_t * pat, BUFFER * s, BUFFER * err)
215 if (mutt_extract_token (&buf, s, M_TOKEN_PATTERN | M_TOKEN_COMMENT) != 0 ||
217 snprintf (err->data, err->dsize, _("Error in expression: %s"), s->dptr);
222 snprintf (err->data, err->dsize, _("Empty expression"));
226 if (pat->stringmatch) {
227 pat->str = m_strdup(buf.data);
230 pat->rx = p_new(regex_t, 1);
231 r = REGCOMP (pat->rx, buf.data, REG_NEWLINE | REG_NOSUB | mutt_which_case (buf.data));
234 regerror (r, pat->rx, err->data, err->dsize);
243 static int patmatch (const pattern_t* pat, const char* buf) {
244 if (pat->stringmatch)
245 return !strstr (buf, pat->str);
247 return regexec (pat->rx, buf, 0, NULL, 0);
250 int eat_range (pattern_t * pat, BUFFER * s, BUFFER * err __attribute__ ((unused)))
253 int do_exclusive = 0;
257 * If simple_search is set to "~m %s", the range will have double quotes
260 if (*s->dptr == '"') {
266 if ((*s->dptr != '-') && (*s->dptr != '<')) {
268 if (*s->dptr == '>') {
269 pat->max = M_MAXRANGE;
270 pat->min = strtol (s->dptr + 1, &tmp, 0) + 1; /* exclusive range */
273 pat->min = strtol (s->dptr, &tmp, 0);
274 if (toupper ((unsigned char) *tmp) == 'K') { /* is there a prefix? */
278 else if (toupper ((unsigned char) *tmp) == 'M') {
282 if (*s->dptr == '>') {
299 if (isdigit ((unsigned char) *tmp)) {
301 pat->max = strtol (tmp, &tmp, 0);
302 if (toupper ((unsigned char) *tmp) == 'K') {
306 else if (toupper ((unsigned char) *tmp) == 'M') {
314 pat->max = M_MAXRANGE;
316 if (skip_quote && *tmp == '"')
319 s->dptr = vskipspaces(tmp);
323 static const char *getDate (const char *s, struct tm *t, BUFFER * err)
326 time_t now = time (NULL);
327 struct tm *tm = localtime (&now);
329 t->tm_mday = strtol (s, &p, 10);
330 if (t->tm_mday < 1 || t->tm_mday > 31) {
331 snprintf (err->data, err->dsize, _("Invalid day of month: %s"), s);
335 /* fill in today's month and year */
336 t->tm_mon = tm->tm_mon;
337 t->tm_year = tm->tm_year;
341 t->tm_mon = strtol (p, &p, 10) - 1;
342 if (t->tm_mon < 0 || t->tm_mon > 11) {
343 snprintf (err->data, err->dsize, _("Invalid month: %s"), p);
347 t->tm_year = tm->tm_year;
351 t->tm_year = strtol (p, &p, 10);
352 if (t->tm_year < 70) /* year 2000+ */
354 else if (t->tm_year > 1900)
363 static const char *get_offset (struct tm *tm, const char *s, int sign)
366 int offset = strtol (s, &ps, 0);
368 if ((sign < 0 && offset > 0) || (sign > 0 && offset < 0))
373 tm->tm_year += offset;
376 tm->tm_mon += offset;
379 tm->tm_mday += 7 * offset;
382 tm->tm_mday += offset;
387 mutt_normalize_time (tm);
391 static void adjust_date_range (struct tm *min, struct tm *max)
393 if (min->tm_year > max->tm_year
394 || (min->tm_year == max->tm_year && min->tm_mon > max->tm_mon)
395 || (min->tm_year == max->tm_year && min->tm_mon == max->tm_mon
396 && min->tm_mday > max->tm_mday)) {
400 min->tm_year = max->tm_year;
404 min->tm_mon = max->tm_mon;
408 min->tm_mday = max->tm_mday;
411 min->tm_hour = min->tm_min = min->tm_sec = 0;
413 max->tm_min = max->tm_sec = 59;
417 static const char *parse_date_range (const char *pc, struct tm *min,
418 struct tm *max, int haveMin,
419 struct tm *baseMin, BUFFER * err)
421 int flag = M_PDR_NONE;
423 while (*pc && ((flag & M_PDR_DONE) == 0)) {
427 pc = vskipspaces(pc);
431 /* try a range of absolute date minus offset of Ndwmy */
432 pt = get_offset (min, pc, -1);
434 if (flag == M_PDR_NONE) { /* nothing yet and no offset parsed => absolute date? */
435 if (!getDate (pc, max, err))
436 flag |= (M_PDR_ABSOLUTE | M_PDR_ERRORDONE); /* done bad */
438 /* reestablish initial base minimum if not specified */
440 memcpy (min, baseMin, sizeof (struct tm));
441 flag |= (M_PDR_ABSOLUTE | M_PDR_DONE); /* done good */
445 flag |= M_PDR_ERRORDONE;
449 if (flag == M_PDR_NONE && !haveMin) { /* the very first "-3d" without a previous absolute date */
450 max->tm_year = min->tm_year;
451 max->tm_mon = min->tm_mon;
452 max->tm_mday = min->tm_mday;
459 { /* enlarge plusRange */
460 pt = get_offset (max, pc, 1);
462 flag |= M_PDR_ERRORDONE;
470 { /* enlarge window in both directions */
471 pt = get_offset (min, pc, -1);
473 flag |= M_PDR_ERRORDONE;
475 pc = get_offset (max, pc, 1);
476 flag |= M_PDR_WINDOW;
481 flag |= M_PDR_ERRORDONE;
483 pc = vskipspaces(pc);
485 if ((flag & M_PDR_ERROR) && !(flag & M_PDR_ABSOLUTE)) { /* getDate has its own error message, don't overwrite it here */
486 snprintf (err->data, err->dsize, _("Invalid relative date: %s"), pc - 1);
488 return ((flag & M_PDR_ERROR) ? NULL : pc);
491 static int eat_date (pattern_t * pat, BUFFER * s, BUFFER * err)
497 if (mutt_extract_token (&buffer, s, M_TOKEN_COMMENT | M_TOKEN_PATTERN) != 0
499 m_strcpy(err->data, err->dsize, _("error in expression"));
504 /* the `0' time is Jan 1, 1970 UTC, so in order to prevent a negative time
505 when doing timezone conversion, we use Jan 2, 1970 UTC as the base
512 /* Arbitrary year in the future. Don't set this too high
513 or mutt_mktime() returns something larger than will
514 fit in a time_t on some systems */
522 if (strchr ("<>=", buffer.data[0])) {
523 /* offset from current time
524 <3d less than three days ago
525 >3d more than three days ago
526 =3d exactly three days ago */
527 time_t now = time (NULL);
528 struct tm *tm = localtime (&now);
531 if (buffer.data[0] == '<') {
532 memcpy (&min, tm, sizeof (min));
536 memcpy (&max, tm, sizeof (max));
539 if (buffer.data[0] == '=')
543 tm->tm_min = tm->tm_sec = 59;
545 /* force negative offset */
546 get_offset (tm, buffer.data + 1, -1);
549 /* start at the beginning of the day in question */
550 memcpy (&min, &max, sizeof (max));
551 min.tm_hour = min.tm_sec = min.tm_min = 0;
555 const char *pc = buffer.data;
558 int untilNow = FALSE;
560 if (isdigit ((unsigned char) *pc)) {
561 /* mininum date specified */
562 if ((pc = getDate (pc, &min, err)) == NULL) {
563 p_delete(&buffer.data);
567 pc = vskipspaces(pc);
571 pt = skipspaces(pc + 1);
572 untilNow = (*pt == '\0');
576 if (!untilNow) { /* max date or relative range/window */
580 if (!haveMin) { /* save base minimum and set current date, e.g. for "-3d+1d" */
581 time_t now = time (NULL);
582 struct tm *tm = localtime (&now);
584 memcpy (&baseMin, &min, sizeof (baseMin));
585 memcpy (&min, tm, sizeof (min));
586 min.tm_hour = min.tm_sec = min.tm_min = 0;
589 /* preset max date for relative offsets,
590 if nothing follows we search for messages on a specific day */
591 max.tm_year = min.tm_year;
592 max.tm_mon = min.tm_mon;
593 max.tm_mday = min.tm_mday;
595 if (!parse_date_range (pc, &min, &max, haveMin, &baseMin, err)) { /* bail out on any parsing error */
596 p_delete(&buffer.data);
602 /* Since we allow two dates to be specified we'll have to adjust that. */
603 adjust_date_range (&min, &max);
605 pat->min = mutt_mktime (&min, 1);
606 pat->max = mutt_mktime (&max, 1);
608 p_delete(&buffer.data);
613 static const struct pattern_flags *lookup_tag (char tag)
617 for (i = 0; Flags[i].tag; i++)
618 if (Flags[i].tag == tag)
623 static const char *find_matching_paren (const char *s)
628 level += (*s == '(') - (*s == ')');
635 pattern_t *mutt_pattern_comp(const char *s, int flags, BUFFER *err)
637 pattern_t *curlist = NULL;
639 pattern_t *last = NULL;
643 int implicit = 1; /* used to detect logical AND operator */
644 const struct pattern_flags *entry;
651 ps.dsize = m_strlen(s);
654 ps.dptr = vskipspaces(ps.dptr);
667 snprintf (err->data, err->dsize, _("error in pattern at: %s"),
672 /* A & B | C == (A & B) | C */
675 tmp->child = curlist;
691 if (implicit && or) {
692 /* A | B & C == (A | B) & C */
695 tmp->child = curlist;
703 tmp->alladdr = alladdr;
704 tmp->stringmatch = (*ps.dptr == '=') ? 1 : 0;
714 ps.dptr++; /* move past the ~ */
715 if ((entry = lookup_tag (*ps.dptr)) == NULL) {
716 snprintf (err->data, err->dsize, _("%c: invalid command"), *ps.dptr);
717 pattern_list_wipe(&curlist);
720 if (entry->class && (flags & entry->class) == 0) {
721 snprintf (err->data, err->dsize, _("%c: not supported in this mode"),
723 pattern_list_wipe(&curlist);
728 ps.dptr = vskipspaces(ps.dptr + 1);
730 if (entry->eat_arg) {
732 snprintf (err->data, err->dsize, _("missing parameter"));
733 pattern_list_wipe(&curlist);
736 if (entry->eat_arg (tmp, &ps, err) == -1) {
737 pattern_list_wipe(&curlist);
744 p = find_matching_paren (ps.dptr + 1);
746 snprintf (err->data, err->dsize, _("mismatched parenthesis: %s"),
748 pattern_list_wipe(&curlist);
751 /* compile the sub-expression */
752 buf = p_dupstr(ps.dptr + 1, p - ps.dptr - 1);
753 if ((tmp = mutt_pattern_comp (buf, flags, err)) == NULL) {
755 pattern_list_wipe(&curlist);
765 tmp->alladdr |= alladdr;
768 ps.dptr = p + 1; /* restore location */
771 snprintf (err->data, err->dsize, _("error in pattern at: %s"), ps.dptr);
772 pattern_list_wipe(&curlist);
777 m_strcpy(err->data, err->dsize, _("empty pattern"));
782 tmp->op = or ? M_OR : M_AND;
783 tmp->child = curlist;
790 perform_and (pattern_t * pat, pattern_exec_flag flags, CONTEXT * ctx,
793 for (; pat; pat = pat->next)
794 if (mutt_pattern_exec (pat, flags, ctx, hdr) <= 0)
800 perform_or (struct pattern_t *pat, pattern_exec_flag flags, CONTEXT * ctx,
803 for (; pat; pat = pat->next)
804 if (mutt_pattern_exec (pat, flags, ctx, hdr) > 0)
809 static int match_adrlist (pattern_t* pat, int match_personal, int alladdr __attribute__ ((unused)),
817 for (a = va_arg (ap, address_t *); a; a = a->next) {
819 ((a->mailbox && patmatch (pat, a->mailbox) == 0) ||
820 (match_personal && a->personal &&
821 patmatch (pat, a->personal) == 0))) {
823 return (!pat->alladdr); /* Found match, or non-match if alladdr */
828 return pat->alladdr; /* No matches, or all matches if alladdr */
831 static int match_reference (pattern_t* pat, string_list_t * refs)
833 for (; refs; refs = refs->next)
834 if (patmatch (pat, refs->data) == 0)
839 int mutt_is_list_recipient (int alladdr, address_t * a1, address_t * a2)
841 for (; a1; a1 = a1->next)
842 if (alladdr ^ mutt_is_subscribed_list (a1))
844 for (; a2; a2 = a2->next)
845 if (alladdr ^ mutt_is_subscribed_list (a2))
850 int mutt_is_list_cc (int alladdr, address_t * a1, address_t * a2)
852 for (; a1; a1 = a1->next)
853 if (alladdr ^ mutt_is_mail_list (a1))
855 for (; a2; a2 = a2->next)
856 if (alladdr ^ mutt_is_mail_list (a2))
861 static int match_user (int alladdr, address_t * a1, address_t * a2)
863 for (; a1; a1 = a1->next)
864 if (alladdr ^ mutt_addr_is_user (a1))
866 for (; a2; a2 = a2->next)
867 if (alladdr ^ mutt_addr_is_user (a2))
872 /* test if name is considered a real name, i.e. consists of at least 2
873 * space-separated words of which none may end in a dot
875 static int valid_realname (const char *name)
877 const char *p = name;
884 /* skip abbr. parts of names (e.g. 'J. User') */
892 M_MATCH_FULL_ADDRESS match both personal and machine address */
894 mutt_pattern_exec (struct pattern_t *pat, pattern_exec_flag flags,
895 CONTEXT * ctx, HEADER * h)
899 return (pat->not ^ (perform_and (pat->child, flags, ctx, h) > 0));
901 return (pat->not ^ (perform_or (pat->child, flags, ctx, h) > 0));
905 return (pat->not ^ h->expired);
907 return (pat->not ^ h->superseded);
909 return (pat->not ^ h->flagged);
911 return (pat->not ^ h->tagged);
913 return (pat->not ? h->old || h->read : !(h->old || h->read));
915 return (pat->not ? h->read : !h->read);
917 return (pat->not ^ h->replied);
919 return (pat->not ? (!h->old || h->read) : (h->old && !h->read));
921 return (pat->not ^ h->read);
923 return (pat->not ^ h->deleted);
925 return (pat->not ^ (h->msgno >= pat->min - 1 && (pat->max == M_MAXRANGE ||
930 not ^ (h->date_sent >= pat->min && h->date_sent <= pat->max));
931 case M_DATE_RECEIVED:
932 return (pat->not ^ (h->received >= pat->min && h->received <= pat->max));
936 /* IMAP search sets h->matched at search compile time */
937 if (ctx->magic == M_IMAP && pat->stringmatch)
939 return (pat->not ^ msg_search (ctx, pat, h->msgno));
941 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
942 pat->alladdr, 1, h->env->sender));
944 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
945 pat->alladdr, 1, h->env->from));
947 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
948 pat->alladdr, 1, h->env->to));
950 return (pat->not ^ match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
951 pat->alladdr, 1, h->env->cc));
954 not ^ (h->env && h->env->subject
955 && patmatch (pat, h->env->subject) == 0));
958 not ^ (h->env && h->env->message_id
959 && patmatch (pat, h->env->message_id) == 0));
961 return (pat->not ^ (h->score >= pat->min && (pat->max == M_MAXRANGE ||
962 h->score <= pat->max)));
965 not ^ (h->content->length >= pat->min
966 && (pat->max == M_MAXRANGE
967 || h->content->length <= pat->max)));
969 return (pat->not ^ match_reference (pat, h->env->references));
973 && match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
974 pat->alladdr, 4, h->env->from,
975 h->env->sender, h->env->to,
980 && match_adrlist (pat, flags & M_MATCH_FULL_ADDRESS,
981 pat->alladdr, 2, h->env->to,
986 && mutt_is_list_cc (pat->alladdr, h->env->to,
988 case M_SUBSCRIBED_LIST:
991 && mutt_is_list_recipient (pat->alladdr, h->env->to,
993 case M_PERSONAL_RECIP:
996 && match_user (pat->alladdr, h->env->to, h->env->cc)));
997 case M_PERSONAL_FROM:
999 not ^ (h->env && match_user (pat->alladdr, h->env->from, NULL)));
1001 return (pat->not ^ (h->collapsed && h->num_hidden > 1));
1003 return (pat->not ^ ((h->security & SIGN) ? 1 : 0));
1004 case M_CRYPT_VERIFIED:
1005 return (pat->not ^ ((h->security & GOODSIGN) ? 1 : 0));
1006 case M_CRYPT_ENCRYPT:
1007 return (pat->not ^ ((h->security & ENCRYPT) ? 1 : 0));
1009 return (pat->not ^ ((h->security & APPLICATION_PGP)
1010 && (h->security & PGPKEY)));
1013 not ^ (h->env->x_label
1014 && patmatch (pat, h->env->x_label) == 0));
1017 not ^ (h->env->spam && h->env->spam->data
1018 && patmatch (pat, h->env->spam->data) == 0));
1020 return (pat->not ^ (h->thread && h->thread->duplicate_thread));
1026 if (h->content->parts)
1027 count = mutt_count_body_parts(h, 0);
1029 mutt_parse_mime_message(ctx, h);
1030 count = mutt_count_body_parts(h, 0);
1031 body_list_wipe(&h->content->parts);
1034 return (pat->not ^ (count >= pat->min && (pat->max == M_MAXRANGE ||
1035 count <= pat->max)));
1038 case M_UNREFERENCED:
1039 return (pat->not ^ (h->thread && !h->thread->child));
1041 return (pat->not ^ (h->content && h->content->type == TYPEMULTIPART));
1044 * we have a match if
1045 * - From: matches $alternates
1046 * - or we have an alias for current address
1047 * - or From: contains valid email address _and_ name has >= 2 fields
1049 return (h->env && h->env->from
1050 && (mutt_addr_is_user(h->env->from)
1051 || alias_reverse_lookup(h->env->from)
1052 || (h->env->from->personal
1053 && valid_realname(h->env-> from->personal)
1054 && h->env->from->mailbox)
1059 not ^ (h->env->newsgroups
1060 && patmatch (pat, h->env->newsgroups) == 0));
1063 mutt_error (_("error: unknown op %d (report this error)."), pat->op);
1067 static void quote_simple (char *tmp, ssize_t len, const char *p)
1072 while (*p && i < len - 3) {
1073 if (*p == '\\' || *p == '"')
1081 /* convert a simple search into a real request */
1082 void mutt_check_simple (char *s, ssize_t len, const char *simple)
1084 char tmp[LONG_STRING];
1086 /* XXX - is ascii_strcasecmp() right here, or should we use locale's
1090 if (!strchr (s, '~') && !strchr (s, '=')) { /* yup, so spoof a real request */
1091 /* convert old tokens into the new format */
1092 if (ascii_strcasecmp ("all", s) == 0 || !m_strcmp("^", s) || !m_strcmp(".", s)) /* ~A is more efficient */
1093 m_strcpy(s, len, "~A");
1094 else if (ascii_strcasecmp ("del", s) == 0)
1095 m_strcpy(s, len, "~D");
1096 else if (ascii_strcasecmp ("flag", s) == 0)
1097 m_strcpy(s, len, "~F");
1098 else if (ascii_strcasecmp ("new", s) == 0)
1099 m_strcpy(s, len, "~N");
1100 else if (ascii_strcasecmp ("old", s) == 0)
1101 m_strcpy(s, len, "~O");
1102 else if (ascii_strcasecmp ("repl", s) == 0)
1103 m_strcpy(s, len, "~Q");
1104 else if (ascii_strcasecmp ("read", s) == 0)
1105 m_strcpy(s, len, "~R");
1106 else if (ascii_strcasecmp ("tag", s) == 0)
1107 m_strcpy(s, len, "~T");
1108 else if (ascii_strcasecmp ("unread", s) == 0)
1109 m_strcpy(s, len, "~U");
1111 quote_simple (tmp, sizeof (tmp), s);
1112 m_file_fmt(s, len, simple, tmp);
1117 int mutt_pattern_func (int op, const char *prompt)
1120 char buf[LONG_STRING] = "", *simple, error[STRING];
1124 m_strcpy(buf, sizeof(buf), NONULL(Context->pattern));
1125 if (prompt || op != M_LIMIT)
1126 if (mutt_get_field (prompt, buf, sizeof (buf), M_PATTERN | M_CLEAR) != 0 || !buf[0])
1129 mutt_message _("Compiling search pattern...");
1131 simple = m_strdup(buf);
1132 mutt_check_simple (buf, sizeof (buf), NONULL (SimpleSearch));
1135 err.dsize = sizeof (error);
1136 if ((pat = mutt_pattern_comp (buf, M_FULL_MSG, &err)) == NULL) {
1138 mutt_error ("%s", err.data);
1142 if (Context->magic == M_IMAP && imap_search (Context, pat) < 0)
1145 mutt_message _("Executing command on matching messages...");
1147 #define THIS_BODY Context->hdrs[i]->content
1149 if (op == M_LIMIT) {
1150 Context->vcount = 0;
1152 Context->collapsed = 0;
1154 for (i = 0; i < Context->msgcount; i++) {
1155 /* new limit pattern implicitly uncollapses all threads */
1156 Context->hdrs[i]->virtual = -1;
1157 Context->hdrs[i]->limited = 0;
1158 Context->hdrs[i]->collapsed = 0;
1159 Context->hdrs[i]->num_hidden = 0;
1160 if (mutt_pattern_exec
1161 (pat, M_MATCH_FULL_ADDRESS, Context, Context->hdrs[i])) {
1162 Context->hdrs[i]->virtual = Context->vcount;
1163 Context->hdrs[i]->limited = 1;
1164 Context->v2r[Context->vcount] = i;
1166 Context->vsize += THIS_BODY->length + THIS_BODY->offset -
1167 THIS_BODY->hdr_offset;
1172 for (i = 0; i < Context->vcount; i++) {
1173 if (mutt_pattern_exec
1174 (pat, M_MATCH_FULL_ADDRESS, Context,
1175 Context->hdrs[Context->v2r[i]])) {
1178 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_PURGED,
1181 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_DELETE,
1186 mutt_set_flag (Context, Context->hdrs[Context->v2r[i]], M_TAG,
1196 mutt_clear_error ();
1198 if (op == M_LIMIT) {
1199 /* drop previous limit pattern */
1200 p_delete(&Context->pattern);
1201 if (Context->limit_pattern)
1202 pattern_list_wipe(&Context->limit_pattern);
1203 if (Context->msgcount && !Context->vcount) {
1204 mutt_error _("No messages matched criteria.");
1207 /* record new limit pattern, unless match all */
1208 if (m_strncmp(buf, "~A", 2) != 0) {
1209 Context->pattern = simple;
1210 simple = NULL; /* don't clobber it */
1211 Context->limit_pattern = mutt_pattern_comp (buf, M_FULL_MSG, &err);
1215 pattern_list_wipe(&pat);
1219 int mutt_search_command (int cur, int op)
1223 char temp[LONG_STRING];
1229 if (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE) {
1230 m_strcpy(buf, sizeof(buf), LastSearch);
1231 if (mutt_get_field ((op == OP_SEARCH) ? _("Search for: ") :
1232 _("Reverse search for: "), buf, sizeof (buf),
1233 M_CLEAR | M_PATTERN) != 0 || !buf[0])
1236 if (op == OP_SEARCH)
1237 unset_option (OPTSEARCHREVERSE);
1239 set_option (OPTSEARCHREVERSE);
1241 /* compare the *expanded* version of the search pattern in case
1242 $simple_search has changed while we were searching */
1243 m_strcpy(temp, sizeof(temp), buf);
1244 mutt_check_simple (temp, sizeof (temp), NONULL (SimpleSearch));
1246 if (!SearchPattern || m_strcmp(temp, LastSearchExpn)) {
1247 set_option (OPTSEARCHINVALID);
1248 m_strcpy(LastSearch, sizeof(LastSearch), buf);
1249 mutt_message _("Compiling search pattern...");
1251 pattern_list_wipe(&SearchPattern);
1253 err.dsize = sizeof (error);
1254 if ((SearchPattern =
1255 mutt_pattern_comp (temp, M_FULL_MSG, &err)) == NULL) {
1256 mutt_error ("%s", error);
1259 mutt_clear_error ();
1262 else if (!SearchPattern) {
1263 mutt_error _("No search pattern.");
1268 if (option (OPTSEARCHINVALID)) {
1269 for (i = 0; i < Context->msgcount; i++)
1270 Context->hdrs[i]->searched = 0;
1271 if (Context->magic == M_IMAP && imap_search (Context, SearchPattern) < 0)
1273 unset_option (OPTSEARCHINVALID);
1276 incr = (option (OPTSEARCHREVERSE)) ? -1 : 1;
1277 if (op == OP_SEARCH_OPPOSITE)
1280 for (i = cur + incr, j = 0; j != Context->vcount; j++) {
1281 if (i > Context->vcount - 1) {
1283 if (option (OPTWRAPSEARCH))
1284 mutt_message (_("Search wrapped to top."));
1287 mutt_message _("Search hit bottom without finding match");
1293 i = Context->vcount - 1;
1294 if (option (OPTWRAPSEARCH))
1295 mutt_message (_("Search wrapped to bottom."));
1298 mutt_message _("Search hit top without finding match");
1304 h = Context->hdrs[Context->v2r[i]];
1306 /* if we've already evaulated this message, use the cached value */
1311 /* remember that we've already searched this message */
1315 (SearchPattern, M_MATCH_FULL_ADDRESS, Context, h) > 0)))
1320 mutt_error _("Search interrupted.");
1329 mutt_error _("Not found.");