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 [1] that may come after our current school project.

[1] * http://thread.gmane.org/gmane.comp.version-control.git/295752

 perl/Git.pm | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git a/perl/Git.pm b/perl/Git.pm
index ce7e4e8..1af4805 100644
--- a/perl/Git.pm
+++ b/perl/Git.pm
@@ -865,6 +865,40 @@ sub ident_person {
        return "$ident[0] <$ident[1]>";
 }
 
+=item parse_email
+
+Return a hash of email fields extracted from a file handler.
+
+=cut
+
+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;
+}
+
 =item parse_mailboxes
 
 Return an array of mailboxes extracted from a string.
-- 
2.8.2.537.gb153d2a

--
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