more fixes
[apps/madmutt.git] / pattern.c
index d7b274f..c06f5f8 100644 (file)
--- a/pattern.c
+++ b/pattern.c
@@ -95,12 +95,13 @@ struct pattern_flags {
 #endif
   {
   'x', M_REFERENCE, 0, eat_regexp}, {
+  'X', M_MIMEATTACH, 0, eat_range}, {
   'y', M_XLABEL, 0, eat_regexp}, {
   'z', M_SIZE, 0, eat_range}, {
   '=', M_DUPLICATED, 0, NULL}, {
   '$', M_UNREFERENCED, 0, NULL}, {
   '*', M_REALNAME, 0, NULL}, {
-  0}
+  0, 0, 0, NULL}
 };
 
 static pattern_t *SearchPattern = NULL; /* current search pattern */
@@ -186,13 +187,13 @@ msg_search (CONTEXT *ctx, pattern_t* pat, int msgno)
           return (0);
         }
 
-        fseek (msg->fp, h->offset, 0);
+        fseeko (msg->fp, h->offset, 0);
         mutt_body_handler (h->content, &s);
       }
 
       fp = s.fpout;
       fflush (fp);
-      fseek (fp, 0, 0);
+      fseeko (fp, 0, 0);
       fstat (fileno (fp), &st);
       lng = (long) st.st_size;
     }
@@ -200,12 +201,12 @@ msg_search (CONTEXT *ctx, pattern_t* pat, int msgno)
       /* raw header / body */
       fp = msg->fp;
       if (pat->op != M_BODY) {
-        fseek (fp, h->offset, 0);
+        fseeko (fp, h->offset, 0);
         lng = h->content->offset - h->offset;
       }
       if (pat->op != M_HEADER) {
         if (pat->op == M_BODY)
-          fseek (fp, h->content->offset, 0);
+          fseeko (fp, h->content->offset, 0);
         lng += h->content->length;
       }
     }
@@ -218,7 +219,7 @@ msg_search (CONTEXT *ctx, pattern_t* pat, int msgno)
       if (pat->op == M_HEADER) {
         if (*(buf = mutt_read_rfc822_line (fp, buf, &blen)) == '\0')
           break;
-      } if (fgets (buf, blen - 1, fp) == NULL)
+      } else if (fgets (buf, blen - 1, fp) == NULL)
         break;                  /* don't loop forever */
       if (patmatch (pat, buf) == 0) {
         match = 1;
@@ -246,12 +247,18 @@ int eat_regexp (pattern_t * pat, BUFFER * s, BUFFER * err)
   int r;
 
   memset (&buf, 0, sizeof (buf));
+
   if (mutt_extract_token (&buf, s, M_TOKEN_PATTERN | M_TOKEN_COMMENT) != 0 ||
       !buf.data) {
     snprintf (err->data, err->dsize, _("Error in expression: %s"), s->dptr);
     return (-1);
   }
 
+  if (!*buf.data) {
+    snprintf (err->data, err->dsize, _("Empty expression"));
+    return (-1);
+  }
+
 #if 0
  /* If there are no RE metacharacters, use simple search anyway */
   if (!pat->stringmatch && !strpbrk (buf.data, "|[{.*+?^$"))
@@ -995,7 +1002,7 @@ mutt_pattern_exec (struct pattern_t *pat, pattern_exec_flag flags,
   case M_WHOLE_MSG:
 #ifdef USE_IMAP
     /* IMAP search sets h->matched at search compile time */
-    if (Context->magic == M_IMAP && pat->stringmatch)
+    if (ctx->magic == M_IMAP && pat->stringmatch)
       return (h->matched);
 #endif
     return (pat->not ^ msg_search (ctx, pat, h->msgno));
@@ -1088,6 +1095,23 @@ mutt_pattern_exec (struct pattern_t *pat, pattern_exec_flag flags,
                    && patmatch (pat, h->env->spam->data) == 0));
   case M_DUPLICATED:
     return (pat->not ^ (h->thread && h->thread->duplicate_thread));
+
+  case M_MIMEATTACH:
+    {
+      int count;
+      
+      if (h->content->parts)
+        count = mutt_count_body_parts(h, 0);
+      else {
+        mutt_parse_mime_message(ctx, h);
+        count = mutt_count_body_parts(h, 0);
+        mutt_free_body(&h->content->parts);
+      }
+      
+      return (pat->not ^ (count >= pat->min && (pat->max == M_MAXRANGE ||
+                                                count <= pat->max)));
+    }
+
   case M_UNREFERENCED:
     return (pat->not ^ (h->thread && !h->thread->child));
   case M_MULTIPART:
@@ -1180,14 +1204,8 @@ int mutt_pattern_func (int op, char *prompt)
 
   strfcpy (buf, NONULL (Context->pattern), sizeof (buf));
   if (prompt || op != M_LIMIT)
-    if (mutt_get_field (prompt, buf, sizeof (buf), M_PATTERN | M_CLEAR) != 0)
+    if (mutt_get_field (prompt, buf, sizeof (buf), M_PATTERN | M_CLEAR) != 0 || !buf[0])
       return (-1);
-  if (!buf[0]) {
-    if (op == M_LIMIT)
-      strncpy (buf, "~A", sizeof (buf));
-    else
-      return (-1);
-  }
 
   mutt_message _("Compiling search pattern...");