unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: David Bremner <david@tethera.net>
To: notmuch@notmuchmail.org
Cc: David Bremner <bremner@debian.org>
Subject: [PATCH] mime_node_open: skip envelope from lines at the start of messages
Date: Fri,  9 Mar 2012 09:56:26 -0400	[thread overview]
Message-ID: <1331301387-23630-1-git-send-email-david@tethera.net> (raw)
In-Reply-To: <m2aa3qjbfo.fsf@guru.guru-group.fi>

From: David Bremner <bremner@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

  parent reply	other threads:[~2012-03-09 13:56 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-03-01 21:39 Internal error on line 296 of mime-node.c David Bremner
2012-03-01 21:57 ` Austin Clements
2012-03-02  2:35   ` David Bremner
2012-03-02  2:35     ` [PATCH 1/2] test: utility function to add a pre-generated message to the database David Bremner
2012-03-02  2:35     ` [PATCH 2/2] test: add new test file for mime parsing David Bremner
2012-03-06 18:26 ` [PATCH] Handle errors in mime_node_open Austin Clements
2012-03-06 22:04   ` Parsing regression with gmime-2.6? David Bremner
2012-03-08 15:35     ` [WIP PATCH] debugging gmime-2.6 fail David Bremner
2012-03-08 18:08       ` David Bremner
2012-03-08 18:27     ` Parsing regression with gmime-2.6? Jameson Graef Rollins
2012-03-08 20:30     ` Daniel Kahn Gillmor
2012-03-08 21:32       ` Jameson Graef Rollins
2012-03-08 21:40         ` Daniel Kahn Gillmor
2012-03-08 21:59           ` Jameson Graef Rollins
2012-03-08 21:48       ` [PATCH] mime_node_open: check if the file is in mbox format, and inform gmime David Bremner
2012-03-08 22:05         ` Jameson Graef Rollins
2012-03-09 10:50         ` Tomi Ollila
2012-03-09 13:31           ` [PATCH] mime_node_open: skip envelope from lines at the start of messages David Bremner
2012-03-09 13:56           ` David Bremner [this message]
2012-03-09 16:20             ` Daniel Kahn Gillmor
2012-03-10 13:25               ` David Bremner
2012-03-10 14:45                 ` Tomi Ollila
2012-03-11  1:51   ` [PATCH] Handle errors in mime_node_open David Bremner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1331301387-23630-1-git-send-email-david@tethera.net \
    --to=david@tethera.net \
    --cc=bremner@debian.org \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).