[PATCH] mime_node_open: skip envelope from lines at the start of messages
David Bremner
david at tethera.net
Fri Mar 9 05:31:59 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 | 21 +++++++++++++++++++++
1 files changed, 21 insertions(+), 0 deletions(-)
diff --git a/mime-node.c b/mime-node.c
index a95bdab..8939147 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 from_buf[6]; /* From space NULL */
+ int is_mbox = 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 (fread (from_buf, 1, 5, mctx->file) != 5) {
+ fprintf (stderr, "Failed to read 5 bytes from %s: %s\n",
+ filename, strerror (errno));
+ status = NOTMUCH_STATUS_FILE_ERROR;
+ goto DONE;
+ }
+ from_buf[5] = '\0';
+
+ 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;
+ }
+
+ is_mbox = (strcmp (from_buf, "From ") == 0);
+
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, is_mbox);
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;
--
1.7.9.1
More information about the notmuch
mailing list