Merge branch 'dk/send-email-avoid-net-smtp-ssl-when-able'
authorJunio C Hamano <gitster@pobox.com>
Tue, 30 May 2017 02:16:45 +0000 (11:16 +0900)
committerJunio C Hamano <gitster@pobox.com>
Tue, 30 May 2017 02:16:45 +0000 (11:16 +0900)
"git send-email" now uses Net::SMTP::SSL, which is obsolete, only
when needed. Recent versions of Net::SMTP can do TLS natively.

* dk/send-email-avoid-net-smtp-ssl-when-able:
send-email: Net::SMTP::SSL is obsolete, use only when necessary

1  2 
git-send-email.perl
diff --combined git-send-email.perl
index c314cc2b51e363defcd216391df1ef6dd5897f16,0d90439d9a8e5f701f96afbd19883060a516a341..f0417f64e7513ad085c5227500569f23905f03e4
@@@ -25,9 -25,8 +25,9 @@@ use Getopt::Long
  use Text::ParseWords;
  use Term::ANSIColor;
  use File::Temp qw/ tempdir tempfile /;
 -use File::Spec::Functions qw(catfile);
 +use File::Spec::Functions qw(catdir catfile);
  use Error qw(:try);
 +use Cwd qw(abs_path cwd);
  use Git;
  use Git::I18N;
  
                        die __("The required SMTP server is not properly defined.")
                }
  
+               require Net::SMTP;
+               my $use_net_smtp_ssl = version->parse($Net::SMTP::VERSION) < version->parse("1.28");
+               $smtp_domain ||= maildomain();
                if ($smtp_encryption eq 'ssl') {
                        $smtp_server_port ||= 465; # ssmtp
-                       require Net::SMTP::SSL;
-                       $smtp_domain ||= maildomain();
                        require IO::Socket::SSL;
  
                        # Suppress "variable accessed once" warning.
                        # Net::SMTP::SSL->new() does not forward any SSL options
                        IO::Socket::SSL::set_client_defaults(
                                ssl_verify_params());
-                       $smtp ||= Net::SMTP::SSL->new($smtp_server,
-                                                     Hello => $smtp_domain,
-                                                     Port => $smtp_server_port,
-                                                     Debug => $debug_net_smtp);
+                       if ($use_net_smtp_ssl) {
+                               require Net::SMTP::SSL;
+                               $smtp ||= Net::SMTP::SSL->new($smtp_server,
+                                                             Hello => $smtp_domain,
+                                                             Port => $smtp_server_port,
+                                                             Debug => $debug_net_smtp);
+                       }
+                       else {
+                               $smtp ||= Net::SMTP->new($smtp_server,
+                                                        Hello => $smtp_domain,
+                                                        Port => $smtp_server_port,
+                                                        Debug => $debug_net_smtp,
+                                                        SSL => 1);
+                       }
                }
                else {
-                       require Net::SMTP;
-                       $smtp_domain ||= maildomain();
                        $smtp_server_port ||= 25;
                        $smtp ||= Net::SMTP->new($smtp_server,
                                                 Hello => $smtp_domain,
                                                 Debug => $debug_net_smtp,
                                                 Port => $smtp_server_port);
                        if ($smtp_encryption eq 'tls' && $smtp) {
-                               require Net::SMTP::SSL;
-                               $smtp->command('STARTTLS');
-                               $smtp->response();
-                               if ($smtp->code == 220) {
+                               if ($use_net_smtp_ssl) {
+                                       $smtp->command('STARTTLS');
+                                       $smtp->response();
+                                       if ($smtp->code != 220) {
+                                               die sprintf(__("Server does not support STARTTLS! %s"), $smtp->message);
+                                       }
+                                       require Net::SMTP::SSL;
                                        $smtp = Net::SMTP::SSL->start_SSL($smtp,
                                                                          ssl_verify_params())
-                                               or die "STARTTLS failed! ".IO::Socket::SSL::errstr();
-                                       $smtp_encryption = '';
-                                       # Send EHLO again to receive fresh
-                                       # supported commands
-                                       $smtp->hello($smtp_domain);
-                               } else {
-                                       die sprintf(__("Server does not support STARTTLS! %s"), $smtp->message);
+                                               or die sprintf(__("STARTTLS failed! %s"), IO::Socket::SSL::errstr());
+                               }
+                               else {
+                                       $smtp->starttls(ssl_verify_params())
+                                               or die sprintf(__("STARTTLS failed! %s"), IO::Socket::SSL::errstr());
                                }
+                               $smtp_encryption = '';
+                               # Send EHLO again to receive fresh
+                               # supported commands
+                               $smtp->hello($smtp_domain);
                        }
                }
  
@@@ -1738,23 -1753,6 +1754,23 @@@ sub unique_email_list 
  
  sub validate_patch {
        my $fn = shift;
 +
 +      my $validate_hook = catfile(catdir($repo->repo_path(), 'hooks'),
 +                                  'sendemail-validate');
 +      my $hook_error;
 +      if (-x $validate_hook) {
 +              my $target = abs_path($fn);
 +              # The hook needs a correct cwd and GIT_DIR.
 +              my $cwd_save = cwd();
 +              chdir($repo->wc_path() or $repo->repo_path())
 +                      or die("chdir: $!");
 +              local $ENV{"GIT_DIR"} = $repo->repo_path();
 +              $hook_error = "rejected by sendemail-validate hook"
 +                      if system($validate_hook, $target);
 +              chdir($cwd_save) or die("chdir: $!");
 +      }
 +      return $hook_error if $hook_error;
 +
        open(my $fh, '<', $fn)
                or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
        while (my $line = <$fh>) {