[PATCH] mime_node_open: skip envelope from lines at the start of messages

David Bremner david at tethera.net
Fri Mar 9 05:56:26 PST 2012


From: David Bremner <bremner at debian.org>

Some MDAs such as procmail (in MH mode), and exim (doing local
delivery in some configurations of the appendfile transport) add a
line to the front of a message with "From " followed by envelope
sender.  Since this is not a proper RFC822 header field, gmime (at
least since version 2.6) refuses to parse it, unless in mbox mode.

This change reads the line of the file, and if they start with
"From ", pass the stream to gmime starting from the second line.

This makes mime_node_open more consistent with (but still stricter
than) the permissive behaviour of notmuch_file_get_header
(message-file.c), which allows a certain number of "broken_headers".

We avoid putting gmime into mbox mode in case of side effects; this
leaves the situation of mboxes accidentally indexed by notmuch the
same as before, namely "undefined behaviour".  Ideally they should at
least be warned by notmuch-new.  Although strict rfc822 adherence
would be one way to detect mboxes, it doesn't seem to fit with the
spirit or code of message-file.c.
---
 mime-node.c |   24 ++++++++++++++++++++++++
 1 files changed, 24 insertions(+), 0 deletions(-)

diff --git a/mime-node.c b/mime-node.c
index a95bdab..cf84423 100644
--- a/mime-node.c
+++ b/mime-node.c
@@ -72,6 +72,8 @@ mime_node_open (const void *ctx, notmuch_message_t *message,
     mime_node_context_t *mctx;
     mime_node_t *root;
     notmuch_status_t status;
+    char *first_line=NULL;
+    size_t first_line_size=0;
 
     root = talloc_zero (ctx, mime_node_t);
     if (root == NULL) {
@@ -96,6 +98,23 @@ mime_node_open (const void *ctx, notmuch_message_t *message,
 	goto DONE;
     }
 
+    if (getline(&first_line, &first_line_size, mctx->file) < 0) {
+	fprintf (stderr, "Failed to read first line from %s: %s\n",
+		 filename, strerror (errno));
+	status = NOTMUCH_STATUS_FILE_ERROR;
+	goto DONE;
+    }
+
+    if (strcmp (first_line, "From ") != 0) {
+	/* No envelope from line */
+	if (fseek (mctx->file, 0L, SEEK_SET)) {
+	    fprintf (stderr, "Failed to rewind %s: %s\n",
+		     filename, strerror (errno));
+	    status = NOTMUCH_STATUS_FILE_ERROR;
+	    goto DONE;
+	}
+    }
+
     mctx->stream = g_mime_stream_file_new (mctx->file);
     if (!mctx->stream) {
 	fprintf (stderr, "Out of memory.\n");
@@ -111,7 +130,9 @@ mime_node_open (const void *ctx, notmuch_message_t *message,
 	goto DONE;
     }
 
+    g_mime_parser_set_scan_from (mctx->parser, FALSE);
     mctx->mime_message = g_mime_parser_construct_message (mctx->parser);
+
     if (!mctx->mime_message) {
 	fprintf (stderr, "Failed to parse %s\n", filename);
 	status = NOTMUCH_STATUS_FILE_ERROR;
@@ -136,6 +157,9 @@ mime_node_open (const void *ctx, notmuch_message_t *message,
     return NOTMUCH_STATUS_SUCCESS;
 
 DONE:
+    if (first_line != NULL)
+	free (first_line);
+
     talloc_free (root);
     return status;
 }
-- 
1.7.9.1



More information about the notmuch mailing list