Code

git-mergetool: properly handle "git mergetool -- filename"
[git.git] / git-send-email.perl
index 385ff7c2197f02207bfa56e2a9b6ec557fbfe0cc..449d938ba97cf1db47049a2f514bea1ed220044b 100755 (executable)
@@ -20,6 +20,7 @@ use strict;
 use warnings;
 use Term::ReadLine;
 use Getopt::Long;
+use Text::ParseWords;
 use Data::Dumper;
 use Term::ANSIColor;
 use Git;
@@ -38,7 +39,7 @@ package main;
 
 sub usage {
        print <<EOT;
-git-send-email [options] <file | directory>...
+git send-email [options] <file | directory>...
 Options:
    --from         Specify the "From:" line of the email to be sent.
 
@@ -84,7 +85,10 @@ Options:
 
    --smtp-pass    The password for SMTP-AUTH.
 
-   --smtp-ssl     If set, connects to the SMTP server using SSL.
+   --smtp-encryption Specify 'tls' for STARTTLS encryption, or 'ssl' for SSL.
+                  Any other value disables the feature.
+
+   --smtp-ssl     Synonym for '--smtp-encryption=ssl'.  Deprecated.
 
    --suppress-cc  Suppress the specified category of auto-CC.  The category
                  can be one of 'author' for the patch author, 'self' to
@@ -184,7 +188,7 @@ my ($quiet, $dry_run) = (0, 0);
 
 # Variables with corresponding config settings
 my ($thread, $chain_reply_to, $suppress_from, $signed_off_cc, $cc_cmd);
-my ($smtp_server, $smtp_server_port, $smtp_authuser, $smtp_ssl);
+my ($smtp_server, $smtp_server_port, $smtp_authuser, $smtp_encryption);
 my ($identity, $aliasfiletype, @alias_files, @smtp_host_parts);
 my ($no_validate);
 my (@suppress_cc);
@@ -194,7 +198,6 @@ my %config_bool_settings = (
     "chainreplyto" => [\$chain_reply_to, 1],
     "suppressfrom" => [\$suppress_from, undef],
     "signedoffcc" => [\$signed_off_cc, undef],
-    "smtpssl" => [\$smtp_ssl, 0],
 );
 
 my %config_settings = (
@@ -249,7 +252,8 @@ my $rc = GetOptions("sender|from=s" => \$sender,
                    "smtp-server-port=s" => \$smtp_server_port,
                    "smtp-user=s" => \$smtp_authuser,
                    "smtp-pass:s" => \$smtp_authpass,
-                   "smtp-ssl!" => \$smtp_ssl,
+                   "smtp-ssl" => sub { $smtp_encryption = 'ssl' },
+                   "smtp-encryption=s" => \$smtp_encryption,
                    "identity=s" => \$identity,
                    "compose" => \$compose,
                    "quiet" => \$quiet,
@@ -289,6 +293,15 @@ sub read_config {
                        $$target = Git::config(@repo, "$prefix.$setting") unless (defined $$target);
                }
        }
+
+       if (!defined $smtp_encryption) {
+               my $enc = Git::config(@repo, "$prefix.smtpencryption");
+               if (defined $enc) {
+                       $smtp_encryption = $enc;
+               } elsif (Git::config_bool(@repo, "$prefix.smtpssl")) {
+                       $smtp_encryption = 'ssl';
+               }
+       }
 }
 
 # read configuration from [sendemail "$identity"], fall back on [sendemail]
@@ -301,6 +314,9 @@ foreach my $setting (values %config_bool_settings) {
        ${$setting->[0]} = $setting->[1] unless (defined (${$setting->[0]}));
 }
 
+# 'default' encryption is none -- this only prevents a warning
+$smtp_encryption = '' unless (defined $smtp_encryption);
+
 # Set CC suppressions
 my(%suppress_cc);
 if (@suppress_cc) {
@@ -348,6 +364,10 @@ foreach my $entry (@bcclist) {
        die "Comma in --bcclist entry: $entry'\n" unless $entry !~ m/,/;
 }
 
+sub split_addrs {
+       return parse_line('\s*,\s*', 1, @_);
+}
+
 my %aliases;
 my %parse_alias = (
        # multiline formats can be supported in the future
@@ -356,7 +376,7 @@ my %parse_alias = (
                        my ($alias, $addr) = ($1, $2);
                        $addr =~ s/#.*$//; # mutt allows # comments
                         # commas delimit multiple addresses
-                       $aliases{$alias} = [ split(/\s*,\s*/, $addr) ];
+                       $aliases{$alias} = [ split_addrs($addr) ];
                }}},
        mailrc => sub { my $fh = shift; while (<$fh>) {
                if (/^alias\s+(\S+)\s+(.*)$/) {
@@ -365,7 +385,7 @@ my %parse_alias = (
                }}},
        pine => sub { my $fh = shift; while (<$fh>) {
                if (/^(\S+)\t.*\t(.*)$/) {
-                       $aliases{$1} = [ split(/\s*,\s*/, $2) ];
+                       $aliases{$1} = [ split_addrs($2) ];
                }}},
        gnus => sub { my $fh = shift; while (<$fh>) {
                if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
@@ -392,10 +412,9 @@ for my $f (@ARGV) {
 
                push @files, grep { -f $_ } map { +$f . "/" . $_ }
                                sort readdir(DH);
-
-       } elsif (-f $f) {
+               closedir(DH);
+       } elsif (-f $f or -p $f) {
                push @files, $f;
-
        } else {
                print STDERR "Skipping $f - not found.\n";
        }
@@ -403,8 +422,10 @@ for my $f (@ARGV) {
 
 if (!$no_validate) {
        foreach my $f (@files) {
-               my $error = validate_patch($f);
-               $error and die "fatal: $f: $error\nwarning: no patches were sent\n";
+               unless (-p $f) {
+                       my $error = validate_patch($f);
+                       $error and die "fatal: $f: $error\nwarning: no patches were sent\n";
+               }
        }
 }
 
@@ -442,7 +463,7 @@ if (!@to) {
        }
 
        my $to = $_;
-       push @to, split /,\s*/, $to;
+       push @to, split_addrs($to);
        $prompting++;
 }
 
@@ -738,7 +759,7 @@ X-Mailer: git-send-email $gitversion
                        die "The required SMTP server is not properly defined."
                }
 
-               if ($smtp_ssl) {
+               if ($smtp_encryption eq 'ssl') {
                        $smtp_server_port ||= 465; # ssmtp
                        require Net::SMTP::SSL;
                        $smtp ||= Net::SMTP::SSL->new($smtp_server, Port => $smtp_server_port);
@@ -748,6 +769,21 @@ X-Mailer: git-send-email $gitversion
                        $smtp ||= Net::SMTP->new((defined $smtp_server_port)
                                                 ? "$smtp_server:$smtp_server_port"
                                                 : $smtp_server);
+                       if ($smtp_encryption eq 'tls') {
+                               require Net::SMTP::SSL;
+                               $smtp->command('STARTTLS');
+                               $smtp->response();
+                               if ($smtp->code == 220) {
+                                       $smtp = Net::SMTP::SSL->start_SSL($smtp)
+                                               or die "STARTTLS failed! ".$smtp->message;
+                                       $smtp_encryption = '';
+                                       # Send EHLO again to receive fresh
+                                       # supported commands
+                                       $smtp->hello();
+                               } else {
+                                       die "Server does not support STARTTLS! ".$smtp->message;
+                               }
+                       }
                }
 
                if (!$smtp) {