[PATCH] mime_node_open: skip envelope from lines at the start of messages
authorDavid Bremner <david@tethera.net>
Fri, 9 Mar 2012 13:56:26 +0000 (09:56 +2000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:45:21 +0000 (09:45 -0800)
e6/ef5f39490ed0502b031b044252b4de0b7ebdc5 [new file with mode: 0644]

diff --git a/e6/ef5f39490ed0502b031b044252b4de0b7ebdc5 b/e6/ef5f39490ed0502b031b044252b4de0b7ebdc5
new file mode 100644 (file)
index 0000000..142f760
--- /dev/null
@@ -0,0 +1,139 @@
+Return-Path: <bremner@tethera.net>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id 99CE6431FB6\r
+       for <notmuch@notmuchmail.org>; Fri,  9 Mar 2012 05:56:37 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
+       autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id CY6UqosvXWQ7 for <notmuch@notmuchmail.org>;\r
+       Fri,  9 Mar 2012 05:56:37 -0800 (PST)\r
+Received: from tesseract.cs.unb.ca (tesseract.cs.unb.ca [131.202.240.238])\r
+       (using TLSv1 with cipher AES256-SHA (256/256 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id F3179431FAE\r
+       for <notmuch@notmuchmail.org>; Fri,  9 Mar 2012 05:56:36 -0800 (PST)\r
+Received: from fctnnbsc30w-142166230117.dhcp-dynamic.fibreop.nb.bellaliant.net\r
+       ([142.166.230.117] helo=zancas.localnet)\r
+       by tesseract.cs.unb.ca with esmtpsa (TLS1.0:RSA_AES_256_CBC_SHA1:32)\r
+       (Exim 4.72) (envelope-from <bremner@tethera.net>)\r
+       id 1S60J9-00079K-Ul; Fri, 09 Mar 2012 09:56:36 -0400\r
+Received: from bremner by zancas.localnet with local (Exim 4.77)\r
+       (envelope-from <bremner@tethera.net>)\r
+       id 1S60J4-00069f-L9; Fri, 09 Mar 2012 09:56:30 -0400\r
+From: David Bremner <david@tethera.net>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH] mime_node_open: skip envelope from lines at the start of\r
+       messages\r
+Date: Fri,  9 Mar 2012 09:56:26 -0400\r
+Message-Id: <1331301387-23630-1-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 1.7.9.1\r
+In-Reply-To: <m2aa3qjbfo.fsf@guru.guru-group.fi>\r
+References: <m2aa3qjbfo.fsf@guru.guru-group.fi>\r
+X-Spam_bar: -\r
+Cc: David Bremner <bremner@debian.org>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Fri, 09 Mar 2012 13:56:37 -0000\r
+\r
+From: David Bremner <bremner@debian.org>\r
+\r
+Some MDAs such as procmail (in MH mode), and exim (doing local\r
+delivery in some configurations of the appendfile transport) add a\r
+line to the front of a message with "From " followed by envelope\r
+sender.  Since this is not a proper RFC822 header field, gmime (at\r
+least since version 2.6) refuses to parse it, unless in mbox mode.\r
+\r
+This change reads the line of the file, and if they start with\r
+"From ", pass the stream to gmime starting from the second line.\r
+\r
+This makes mime_node_open more consistent with (but still stricter\r
+than) the permissive behaviour of notmuch_file_get_header\r
+(message-file.c), which allows a certain number of "broken_headers".\r
+\r
+We avoid putting gmime into mbox mode in case of side effects; this\r
+leaves the situation of mboxes accidentally indexed by notmuch the\r
+same as before, namely "undefined behaviour".  Ideally they should at\r
+least be warned by notmuch-new.  Although strict rfc822 adherence\r
+would be one way to detect mboxes, it doesn't seem to fit with the\r
+spirit or code of message-file.c.\r
+---\r
+ mime-node.c |   24 ++++++++++++++++++++++++\r
+ 1 files changed, 24 insertions(+), 0 deletions(-)\r
+\r
+diff --git a/mime-node.c b/mime-node.c\r
+index a95bdab..cf84423 100644\r
+--- a/mime-node.c\r
++++ b/mime-node.c\r
+@@ -72,6 +72,8 @@ mime_node_open (const void *ctx, notmuch_message_t *message,\r
+     mime_node_context_t *mctx;\r
+     mime_node_t *root;\r
+     notmuch_status_t status;\r
++    char *first_line=NULL;\r
++    size_t first_line_size=0;\r
\r
+     root = talloc_zero (ctx, mime_node_t);\r
+     if (root == NULL) {\r
+@@ -96,6 +98,23 @@ mime_node_open (const void *ctx, notmuch_message_t *message,\r
+       goto DONE;\r
+     }\r
\r
++    if (getline(&first_line, &first_line_size, mctx->file) < 0) {\r
++      fprintf (stderr, "Failed to read first line from %s: %s\n",\r
++               filename, strerror (errno));\r
++      status = NOTMUCH_STATUS_FILE_ERROR;\r
++      goto DONE;\r
++    }\r
++\r
++    if (strcmp (first_line, "From ") != 0) {\r
++      /* No envelope from line */\r
++      if (fseek (mctx->file, 0L, SEEK_SET)) {\r
++          fprintf (stderr, "Failed to rewind %s: %s\n",\r
++                   filename, strerror (errno));\r
++          status = NOTMUCH_STATUS_FILE_ERROR;\r
++          goto DONE;\r
++      }\r
++    }\r
++\r
+     mctx->stream = g_mime_stream_file_new (mctx->file);\r
+     if (!mctx->stream) {\r
+       fprintf (stderr, "Out of memory.\n");\r
+@@ -111,7 +130,9 @@ mime_node_open (const void *ctx, notmuch_message_t *message,\r
+       goto DONE;\r
+     }\r
\r
++    g_mime_parser_set_scan_from (mctx->parser, FALSE);\r
+     mctx->mime_message = g_mime_parser_construct_message (mctx->parser);\r
++\r
+     if (!mctx->mime_message) {\r
+       fprintf (stderr, "Failed to parse %s\n", filename);\r
+       status = NOTMUCH_STATUS_FILE_ERROR;\r
+@@ -136,6 +157,9 @@ mime_node_open (const void *ctx, notmuch_message_t *message,\r
+     return NOTMUCH_STATUS_SUCCESS;\r
\r
+ DONE:\r
++    if (first_line != NULL)\r
++      free (first_line);\r
++\r
+     talloc_free (root);\r
+     return status;\r
+ }\r
+-- \r
+1.7.9.1\r
+\r