parse_address_line had not the same behavior whether the user had
Mail::Address or not. Teach parse_address_line to behave like
Mail::Address.

When the user input is correct, this implementation behaves
exactly like Mail::Address except when there are quotes
inside the name:

  "Jane Do"e <j...@example.com>

In this case the result of parse_address_line is:

  With M::A : "Jane Do" e <j...@example.com>
  Without   : "Jane Do e" <j...@example.com>

When the user input is not correct, the behavior is also mostly
the same.

Unlike Mail::Address, this doesn't parse groups and recursive
commentaries.

Signed-off-by: Remi Lespinet <remi.lespi...@ensimag.grenoble-inp.fr>
---
 git-send-email.perl | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 52 insertions(+), 2 deletions(-)

diff --git a/git-send-email.perl b/git-send-email.perl
index a0cd7ff..a1f6c18 100755
--- a/git-send-email.perl
+++ b/git-send-email.perl
@@ -477,9 +477,59 @@ foreach my $entry (@bcclist) {
 sub parse_address_line {
        if ($have_mail_address) {
                return map { $_->format } Mail::Address->parse($_[0]);
-       } else {
-               return split_addrs($_[0]);
        }
+
+       my $commentrgx=qr/\((?:[^)]*)\)/;
+       my $quotergx=qr/"(?:[^\"\\]|\\.)*"/;
+       my $wordrgx=qr/(?:[^]["\s()<>:;@\\,.]|\\.)+/;
+       my $tokenrgx = qr/(?:$quotergx|$wordrgx|$commentrgx|\S)/;
+
+       my @tokens = map { $_ =~ /\s*($tokenrgx)\s*/g } @_;
+       push @tokens, ",";
+
+       my (@addr_list, @phrase, @address, @comment, @buffer) = ();
+       foreach my $token (@tokens) {
+           if ($token =~ /^[,;]$/) {
+               if (@address) {
+                   push @address, @buffer;
+               } else {
+                   push @phrase, @buffer;
+               }
+
+               my $str_phrase = join ' ', @phrase;
+               my $str_address = join '', @address;
+               my $str_comment = join ' ', @comment;
+
+               if ($str_phrase =~ /[][()<>:;@\\,.\000-\037\177]/) {
+                   $str_phrase =~ s/(^|[^\\])"/$1/g;
+                   $str_phrase = qq["$str_phrase"];
+               }
+
+               if ($str_address ne "" && $str_phrase ne "") {
+                   $str_address = qq[<$str_address>];
+               }
+
+               my $str_mailbox = "$str_phrase $str_address $str_comment";
+               $str_mailbox =~ s/^\s*|\s*$//g;
+               push @addr_list, $str_mailbox if ($str_mailbox);
+
+               @phrase = @address = @comment = @buffer = ();
+           } elsif ($token =~ /^\(/) {
+               push @comment, $token;
+           } elsif ($token eq "<") {
+               push @phrase, (splice @address), (splice @buffer);
+           } elsif ($token eq ">") {
+               push @address, (splice @buffer);
+           } elsif ($token eq "@") {
+               push @address, (splice @buffer), "@";
+           } elsif ($token eq ".") {
+               push @address, (splice @buffer), ".";
+           } else {
+               push @buffer, $token;
+           }
+       }
+
+       return @addr_list;
 }
 
 sub split_addrs {
-- 
1.9.1

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