Skip to content

Commit fbc615b

Browse files
committed
Merge branch 'np/send-email-header-parsing'
Code refactoring. * np/send-email-header-parsing: send-email: extract email-parsing code into a subroutine
2 parents 7fb6aef + b604954 commit fbc615b

File tree

1 file changed

+77
-38
lines changed

1 file changed

+77
-38
lines changed

git-send-email.perl

Lines changed: 77 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -708,57 +708,70 @@ sub get_patch_subject {
708708
do_edit($compose_filename);
709709
}
710710
711-
open my $c2, ">", $compose_filename . ".final"
712-
or die sprintf(__("Failed to open %s.final: %s"), $compose_filename, $!);
713-
714711
open $c, "<", $compose_filename
715712
or die sprintf(__("Failed to open %s: %s"), $compose_filename, $!);
716713
717-
my $need_8bit_cte = file_has_nonascii($compose_filename);
718-
my $in_body = 0;
719-
my $summary_empty = 1;
720714
if (!defined $compose_encoding) {
721715
$compose_encoding = "UTF-8";
722716
}
723-
while(<$c>) {
724-
next if m/^GIT:/;
725-
if ($in_body) {
726-
$summary_empty = 0 unless (/^\n$/);
727-
} elsif (/^\n$/) {
728-
$in_body = 1;
729-
if ($need_8bit_cte) {
730-
print $c2 "MIME-Version: 1.0\n",
731-
"Content-Type: text/plain; ",
732-
"charset=$compose_encoding\n",
733-
"Content-Transfer-Encoding: 8bit\n";
734-
}
735-
} elsif (/^MIME-Version:/i) {
736-
$need_8bit_cte = 0;
737-
} elsif (/^Subject:\s*(.+)\s*$/i) {
738-
$initial_subject = $1;
739-
my $subject = $initial_subject;
740-
$_ = "Subject: " .
741-
quote_subject($subject, $compose_encoding) .
742-
"\n";
743-
} elsif (/^In-Reply-To:\s*(.+)\s*$/i) {
744-
$initial_reply_to = $1;
745-
next;
746-
} elsif (/^From:\s*(.+)\s*$/i) {
747-
$sender = $1;
748-
next;
749-
} elsif (/^(?:To|Cc|Bcc):/i) {
750-
print __("To/Cc/Bcc fields are not interpreted yet, they have been ignored\n");
751-
next;
717+
718+
my %parsed_email;
719+
while (my $line = <$c>) {
720+
next if $line =~ m/^GIT:/;
721+
parse_header_line($line, \%parsed_email);
722+
if ($line =~ /^$/) {
723+
$parsed_email{'body'} = filter_body($c);
752724
}
753-
print $c2 $_;
754725
}
755726
close $c;
756-
close $c2;
757727
758-
if ($summary_empty) {
728+
open my $c2, ">", $compose_filename . ".final"
729+
or die sprintf(__("Failed to open %s.final: %s"), $compose_filename, $!);
730+
731+
732+
if ($parsed_email{'From'}) {
733+
$sender = delete($parsed_email{'From'});
734+
}
735+
if ($parsed_email{'In-Reply-To'}) {
736+
$initial_reply_to = delete($parsed_email{'In-Reply-To'});
737+
}
738+
if ($parsed_email{'Subject'}) {
739+
$initial_subject = delete($parsed_email{'Subject'});
740+
print $c2 "Subject: " .
741+
quote_subject($initial_subject, $compose_encoding) .
742+
"\n";
743+
}
744+
745+
if ($parsed_email{'MIME-Version'}) {
746+
print $c2 "MIME-Version: $parsed_email{'MIME-Version'}\n",
747+
"Content-Type: $parsed_email{'Content-Type'};\n",
748+
"Content-Transfer-Encoding: $parsed_email{'Content-Transfer-Encoding'}\n";
749+
delete($parsed_email{'MIME-Version'});
750+
delete($parsed_email{'Content-Type'});
751+
delete($parsed_email{'Content-Transfer-Encoding'});
752+
} elsif (file_has_nonascii($compose_filename)) {
753+
my $content_type = (delete($parsed_email{'Content-Type'}) or
754+
"text/plain; charset=$compose_encoding");
755+
print $c2 "MIME-Version: 1.0\n",
756+
"Content-Type: $content_type\n",
757+
"Content-Transfer-Encoding: 8bit\n";
758+
}
759+
# Preserve unknown headers
760+
foreach my $key (keys %parsed_email) {
761+
next if $key eq 'body';
762+
print $c2 "$key: $parsed_email{$key}";
763+
}
764+
765+
if ($parsed_email{'body'}) {
766+
print $c2 "\n$parsed_email{'body'}\n";
767+
delete($parsed_email{'body'});
768+
} else {
759769
print __("Summary email is empty, skipping it\n");
760770
$compose = -1;
761771
}
772+
773+
close $c2;
774+
762775
} elsif ($annotate) {
763776
do_edit(@files);
764777
}
@@ -797,6 +810,32 @@ sub ask {
797810
return;
798811
}
799812
813+
sub parse_header_line {
814+
my $lines = shift;
815+
my $parsed_line = shift;
816+
my $addr_pat = join "|", qw(To Cc Bcc);
817+
818+
foreach (split(/\n/, $lines)) {
819+
if (/^($addr_pat):\s*(.+)$/i) {
820+
$parsed_line->{$1} = [ parse_address_line($2) ];
821+
} elsif (/^([^:]*):\s*(.+)\s*$/i) {
822+
$parsed_line->{$1} = $2;
823+
}
824+
}
825+
}
826+
827+
sub filter_body {
828+
my $c = shift;
829+
my $body = "";
830+
while (my $body_line = <$c>) {
831+
if ($body_line !~ m/^GIT:/) {
832+
$body .= $body_line;
833+
}
834+
}
835+
return $body;
836+
}
837+
838+
800839
my %broken_encoding;
801840
802841
sub file_declares_8bit_cte {

0 commit comments

Comments
 (0)