]> asedeno.scripts.mit.edu Git - git.git/blobdiff - git-send-email.perl
Merge branch 'ds/checkout-upper'
[git.git] / git-send-email.perl
index d8319d45fb275c783511232c7b0047a517973e03..fd0a4ad0c24dfedd2fea278733d8f6cff59b2eb5 100755 (executable)
@@ -77,7 +77,10 @@ Options:
                   the default section.
 
    --smtp-server  If set, specifies the outgoing SMTP server to use.
-                  Defaults to localhost.
+                  Defaults to localhost.  Port number can be specified here with
+                  hostname:port format or by using --smtp-server-port option.
+
+   --smtp-server-port Specify a port on the outgoing SMTP server to connect to.
 
    --smtp-user    The username for SMTP-AUTH.
 
@@ -85,8 +88,7 @@ Options:
 
    --smtp-ssl     If set, connects to the SMTP server using SSL.
 
-   --suppress-from Suppress sending emails to yourself if your address
-                  appears in a From: line. Defaults to off.
+   --suppress-from Suppress sending emails to yourself. Defaults to off.
 
    --thread       Specify that the "In-Reply-To:" header should be set on all
                   emails. Defaults to on.
@@ -172,8 +174,8 @@ 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_authuser, $smtp_authpass, $smtp_ssl);
-my ($identity, $aliasfiletype, @alias_files);
+my ($smtp_server, $smtp_server_port, $smtp_authuser, $smtp_authpass, $smtp_ssl);
+my ($identity, $aliasfiletype, @alias_files, @smtp_host_parts);
 
 my %config_bool_settings = (
     "thread" => [\$thread, 1],
@@ -185,8 +187,10 @@ my %config_bool_settings = (
 
 my %config_settings = (
     "smtpserver" => \$smtp_server,
+    "smtpserverport" => \$smtp_server_port,
     "smtpuser" => \$smtp_authuser,
     "smtppass" => \$smtp_authpass,
+    "to" => \@to,
     "cccmd" => \$cc_cmd,
     "aliasfiletype" => \$aliasfiletype,
     "bcc" => \@bcclist,
@@ -204,6 +208,7 @@ my $rc = GetOptions("sender|from=s" => \$sender,
                    "bcc=s" => \@bcclist,
                    "chain-reply-to!" => \$chain_reply_to,
                    "smtp-server=s" => \$smtp_server,
+                   "smtp-server-port=s" => \$smtp_server_port,
                    "smtp-user=s" => \$smtp_authuser,
                    "smtp-pass=s" => \$smtp_authpass,
                    "smtp-ssl!" => \$smtp_ssl,
@@ -347,7 +352,7 @@ sub expand_aliases {
 
 if (!defined $initial_subject && $compose) {
        do {
-               $_ = $term->readline("What subject should the emails start with? ",
+               $_ = $term->readline("What subject should the initial email start with? ",
                        $initial_subject);
        } while (!defined $_);
        $initial_subject = $_;
@@ -478,10 +483,17 @@ sub extract_valid_address {
 
 # We'll setup a template for the message id, using the "from" address:
 
+my ($message_id_stamp, $message_id_serial);
 sub make_message_id
 {
-       my $date = time;
-       my $pseudo_rand = int (rand(4200));
+       my $uniq;
+       if (!defined $message_id_stamp) {
+               $message_id_stamp = sprintf("%s-%s", time, $$);
+               $message_id_serial = 0;
+       }
+       $message_id_serial++;
+       $uniq = "$message_id_stamp-$message_id_serial";
+
        my $du_part;
        for ($sender, $repocommitter, $repoauthor) {
                $du_part = extract_valid_address(sanitize_address($_));
@@ -491,8 +503,8 @@ sub make_message_id
                use Sys::Hostname qw();
                $du_part = 'user@' . Sys::Hostname::hostname();
        }
-       my $message_id_template = "<%s-git-send-email-$du_part>";
-       $message_id = sprintf $message_id_template, "$date$pseudo_rand";
+       my $message_id_template = "<%s-git-send-email-%s>";
+       $message_id = sprintf($message_id_template, $uniq, $du_part);
        #print "new message id = $message_id\n"; # Was useful for debugging
 }
 
@@ -502,11 +514,13 @@ $time = time - scalar $#files;
 
 sub unquote_rfc2047 {
        local ($_) = @_;
-       if (s/=\?utf-8\?q\?(.*)\?=/$1/g) {
+       my $encoding;
+       if (s/=\?([^?]+)\?q\?(.*)\?=/$2/g) {
+               $encoding = $1;
                s/_/ /g;
                s/=([0-9A-F]{2})/chr(hex($1))/eg;
        }
-       return "$_";
+       return wantarray ? ($_, $encoding) : $_;
 }
 
 # use the simplest quoting being able to handle the recipient
@@ -595,16 +609,30 @@ X-Mailer: git-send-email $gitversion
                print $sm "$header\n$message";
                close $sm or die $?;
        } else {
+
+               if (!defined $smtp_server) {
+                       die "The required SMTP server is not properly defined."
+               }
+
                if ($smtp_ssl) {
+                       $smtp_server_port ||= 465; # ssmtp
                        require Net::SMTP::SSL;
-                       $smtp ||= Net::SMTP::SSL->new( $smtp_server, Port => 465 );
+                       $smtp ||= Net::SMTP::SSL->new($smtp_server, Port => $smtp_server_port);
                }
                else {
                        require Net::SMTP;
-                       $smtp ||= Net::SMTP->new( $smtp_server );
+                       $smtp ||= Net::SMTP->new((defined $smtp_server_port)
+                                                ? "$smtp_server:$smtp_server_port"
+                                                : $smtp_server);
+               }
+
+               if (!$smtp) {
+                       die "Unable to initialize SMTP properly.  Is there something wrong with your config?";
+               }
+
+               if ((defined $smtp_authuser) && (defined $smtp_authpass)) {
+                       $smtp->auth( $smtp_authuser, $smtp_authpass ) or die $smtp->message;
                }
-               $smtp->auth( $smtp_authuser, $smtp_authpass )
-                       or die $smtp->message if (defined $smtp_authuser);
                $smtp->mail( $raw_from ) or die $smtp->message;
                $smtp->to( @recipients ) or die $smtp->message;
                $smtp->data or die $smtp->message;
@@ -641,6 +669,9 @@ foreach my $t (@files) {
        open(F,"<",$t) or die "can't open file $t";
 
        my $author = undef;
+       my $author_encoding;
+       my $has_content_type;
+       my $body_encoding;
        @cc = @initial_cc;
        @xh = ();
        my $input_format = undef;
@@ -666,12 +697,20 @@ foreach my $t (@files) {
                                                next if ($suppress_from);
                                        }
                                        elsif ($1 eq 'From') {
-                                               $author = unquote_rfc2047($2);
+                                               ($author, $author_encoding)
+                                                 = unquote_rfc2047($2);
                                        }
                                        printf("(mbox) Adding cc: %s from line '%s'\n",
                                                $2, $_) unless $quiet;
                                        push @cc, $2;
                                }
+                               elsif (/^Content-type:/i) {
+                                       $has_content_type = 1;
+                                       if (/charset="?[^ "]+/) {
+                                               $body_encoding = $1;
+                                       }
+                                       push @xh, $_;
+                               }
                                elsif (!/^Date:\s/ && /^[-A-Za-z]+:\s+\S/) {
                                        push @xh, $_;
                                }
@@ -703,6 +742,7 @@ foreach my $t (@files) {
                        if (/^(Signed-off-by|Cc): (.*)$/i && $signed_off_cc) {
                                my $c = $2;
                                chomp $c;
+                               next if ($c eq $sender and $suppress_from);
                                push @cc, $c;
                                printf("(sob) Adding cc: %s from line '%s'\n",
                                        $c, $_) unless $quiet;
@@ -718,6 +758,7 @@ foreach my $t (@files) {
                        my $c = $_;
                        $c =~ s/^\s*//g;
                        $c =~ s/\n$//g;
+                       next if ($c eq $sender and $suppress_from);
                        push @cc, $c;
                        printf("(cc-cmd) Adding cc: %s from: '%s'\n",
                                $c, $cc_cmd) unless $quiet;
@@ -728,6 +769,21 @@ foreach my $t (@files) {
 
        if (defined $author) {
                $message = "From: $author\n\n$message";
+               if (defined $author_encoding) {
+                       if ($has_content_type) {
+                               if ($body_encoding eq $author_encoding) {
+                                       # ok, we already have the right encoding
+                               }
+                               else {
+                                       # uh oh, we should re-encode
+                               }
+                       }
+                       else {
+                               push @xh,
+                                 'MIME-Version: 1.0',
+                                 "Content-Type: text/plain; charset=$author_encoding";
+                       }
+               }
        }
 
        send_message();