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 <samuel.gr...@grenoble-inp.org>
Signed-off-by: Tom RUSSELLO <tom.russe...@grenoble-inp.org>
Signed-off-by: Matthieu MOY <matthieu....@grenoble-inp.fr>
---
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

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to