notmuch: Ignore files that don't look like email messages.
[notmuch] / message.c
index 03583c8d0e43f122215bdbcdd6e7c9ca92242a79..1a5994ff5a03f721b8ed0474a092328e73880db2 100644 (file)
--- a/message.c
+++ b/message.c
@@ -37,6 +37,8 @@ struct _notmuch_message {
     /* Header storage */
     int restrict_headers;
     GHashTable *headers;
+    int broken_headers;
+    int good_headers;
 
     /* Parsing state */
     char *line;
@@ -234,12 +236,21 @@ notmuch_message_get_header (notmuch_message_t *message,
        colon = strchr (message->line, ':');
 
        if (colon == NULL) {
-           fprintf (stderr, "Warning: Unexpected non-header line: %s\n",
-                    message->line);
+           message->broken_headers++;
+           /* A simple heuristic for giving up on things that just
+            * don't look like mail messages. */
+           if (message->broken_headers >= 10 &&
+               message->good_headers < 5)
+           {
+               message->parsing_finished = 1;
+               continue;
+           }
            NEXT_HEADER_LINE (NULL);
            continue;
        }
 
+       message->good_headers++;
+
        header = xstrndup (message->line, colon - message->line);
 
        if (message->restrict_headers &&