From 4b8e4bfff812ace05d2119dd052445a3baa9d89c Mon Sep 17 00:00:00 2001 From: David Bremner Date: Sun, 11 Mar 2012 09:25:31 +2000 Subject: [PATCH] [PATCH] mime_node_open: skip envelope from lines at the start of messages --- a2/b269d6b3519ecc0cea66f39e39a863333f3582 | 141 ++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 a2/b269d6b3519ecc0cea66f39e39a863333f3582 diff --git a/a2/b269d6b3519ecc0cea66f39e39a863333f3582 b/a2/b269d6b3519ecc0cea66f39e39a863333f3582 new file mode 100644 index 000000000..2cd736141 --- /dev/null +++ b/a2/b269d6b3519ecc0cea66f39e39a863333f3582 @@ -0,0 +1,141 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by olra.theworths.org (Postfix) with ESMTP id 52961431FBC + for ; Sat, 10 Mar 2012 05:25:49 -0800 (PST) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: 0 +X-Spam-Level: +X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none] + autolearn=disabled +Received: from olra.theworths.org ([127.0.0.1]) + by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id Ot5Yw95sqGED for ; + Sat, 10 Mar 2012 05:25:47 -0800 (PST) +Received: from tesseract.cs.unb.ca (tesseract.cs.unb.ca [131.202.240.238]) + (using TLSv1 with cipher AES256-SHA (256/256 bits)) + (No client certificate requested) + by olra.theworths.org (Postfix) with ESMTPS id 2F43F431FAE + for ; Sat, 10 Mar 2012 05:25:47 -0800 (PST) +Received: from fctnnbsc30w-142166230117.dhcp-dynamic.fibreop.nb.bellaliant.net + ([142.166.230.117] helo=zancas.localnet) + by tesseract.cs.unb.ca with esmtpsa (TLS1.0:RSA_AES_256_CBC_SHA1:32) + (Exim 4.72) (envelope-from ) + id 1S6MIr-0007z7-Lq; Sat, 10 Mar 2012 09:25:46 -0400 +Received: from bremner by zancas.localnet with local (Exim 4.77) + (envelope-from ) + id 1S6MIm-0000Qc-CP; Sat, 10 Mar 2012 09:25:40 -0400 +From: David Bremner +To: notmuch@notmuchmail.org +Subject: [PATCH] mime_node_open: skip envelope from lines at the start of + messages +Date: Sat, 10 Mar 2012 09:25:31 -0400 +Message-Id: <1331385931-1610-1-git-send-email-david@tethera.net> +X-Mailer: git-send-email 1.7.9.1 +In-Reply-To: <4F5A2DB1.7040301@fifthhorseman.net> +References: <4F5A2DB1.7040301@fifthhorseman.net> +X-Spam_bar: - +Cc: David Bremner +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.13 +Precedence: list +List-Id: "Use and development of the notmuch mail system." + +List-Unsubscribe: , + +List-Archive: +List-Post: +List-Help: +List-Subscribe: , + +X-List-Received-Date: Sat, 10 Mar 2012 13:25:49 -0000 + +From: David Bremner + +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. +--- +This version fixes a few formatting issues, and one bug (the strncmp). + + mime-node.c | 24 ++++++++++++++++++++++++ + 1 files changed, 24 insertions(+), 0 deletions(-) + +diff --git a/mime-node.c b/mime-node.c +index a95bdab..25e9d11 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 (strncmp (first_line, "From ", 5) != 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 + -- 2.26.2