From: Tom Russello Date: Tue, 07 Jun 2016 14:01:46 GMT Subject: [PATCH v3 4/6] send-email: create email parser subroutine Message-ID: <20160607140148.23242-5-tom.russello@grenoble-inp.org> In-Reply-To: <20160607140148.23242-1-tom.russello@grenoble-inp.org> We need a simple and generic way to parse an email file. Since it would be hard to include and maintain an external library, create an simple email parser subroutine to parse an email file. Signed-off-by: Samuel GROOT Signed-off-by: Tom RUSSELLO Signed-off-by: Matthieu MOY --- We chose to create our own simple email parser and only use it for the "quote email" feature to pave the way for the refactorization of the patch parser [0] that may come after our current school project. [0] * http://thread.gmane.org/gmane.comp.version-control.git/295752 git-send-email.perl | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/git-send-email.perl b/git-send-email.perl index 4822f41..db114ae 100755 --- a/git-send-email.perl +++ b/git-send-email.perl @@ -1750,3 +1750,31 @@ sub body_or_subject_has_nonascii { } return 0; } + +sub parse_email { + my %mail = (); + my $fh = shift; + my $last_header; + + # Unfold and parse multiline header fields + while (<$fh>) { + last if /^\s*$/; + s/\r\n|\n|\r//; + if (/^([^\s:]+):[\s]+(.*)$/) { + $last_header = lc($1); + @{$mail{$last_header}} = () + unless defined $mail{$last_header}; + push @{$mail{$last_header}}, $2; + } elsif (/^\s+\S/ and defined $last_header) { + s/^\s+/ /; + push @{$mail{$last_header}}, $_; + } else { + die("Mail format undefined !\n"); + } + } + + # Separate body from header + $mail{"body"} = [(<$fh>)]; + + return \%mail; +} -- 2.8.3