Merge branch 'maint'
[gitweb.git] / git-send-email.perl
index b6022929042e0b03f395c553e93c6f47a35c6990..87f59fa313f4483dc15846979e80e8a8aec3d1f5 100755 (executable)
@@ -64,21 +64,25 @@ sub usage {
                   email sent, rather than to the first email sent.
                   Defaults to on.
 
-   --no-signed-off-cc Suppress the automatic addition of email addresses
-                 that appear in Signed-off-by: or Cc: lines to the cc:
-                 list.  Note: Using this option is not recommended.
+   --signed-off-cc Automatically add email addresses that appear in
+                 Signed-off-by: or Cc: lines to the cc: list. Defaults to on.
 
    --smtp-server  If set, specifies the outgoing SMTP server to use.
                   Defaults to localhost.
 
    --suppress-from Suppress sending emails to yourself if your address
-                  appears in a From: line.
+                  appears in a From: line. Defaults to off.
+
+   --thread       Specify that the "In-Reply-To:" header should be set on all
+                  emails. Defaults to on.
 
    --quiet       Make git-send-email less verbose.  One line per email
                   should be all that is output.
 
    --dry-run     Do everything except actually send the emails.
 
+   --envelope-sender   Specify the envelope sender used to send the emails.
+
 EOT
        exit(1);
 }
@@ -135,10 +139,8 @@ sub format_2822_time {
 my (@to,@cc,@initial_cc,@bcclist,@xh,
        $initial_reply_to,$initial_subject,@files,$from,$compose,$time);
 
-# Behavior modification variables
-my ($chain_reply_to, $quiet, $suppress_from, $no_signed_off_cc,
-       $dry_run) = (1, 0, 0, 0, 0);
 my $smtp_server;
+my $envelope_sender;
 
 # Example reply to:
 #$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
@@ -151,9 +153,22 @@ sub format_2822_time {
        $term = new FakeTerm "$@: going non-interactive";
 }
 
-my $def_chain = $repo->config_boolean('sendemail.chainreplyto');
-if ($def_chain and $def_chain eq 'false') {
-    $chain_reply_to = 0;
+# Behavior modification variables
+my ($quiet, $dry_run) = (0, 0);
+
+# Variables with corresponding config settings
+my ($thread, $chain_reply_to, $suppress_from, $signed_off_cc);
+
+my %config_settings = (
+    "thread" => [\$thread, 1],
+    "chainreplyto" => [\$chain_reply_to, 1],
+    "suppressfrom" => [\$suppress_from, 0],
+    "signedoffcc" => [\$signed_off_cc, 1],
+);
+
+foreach my $setting (keys %config_settings) {
+    my $config = $repo->config_bool("sendemail.$setting");
+    ${$config_settings{$setting}->[0]} = (defined $config) ? $config : $config_settings{$setting}->[1];
 }
 
 @bcclist = $repo->config('sendemail.bcc');
@@ -174,9 +189,11 @@ sub format_2822_time {
                    "smtp-server=s" => \$smtp_server,
                    "compose" => \$compose,
                    "quiet" => \$quiet,
-                   "suppress-from" => \$suppress_from,
-                   "no-signed-off-cc|no-signed-off-by-cc" => \$no_signed_off_cc,
+                   "suppress-from!" => \$suppress_from,
+                   "signed-off-cc|signed-off-by-cc!" => \$signed_off_cc,
                    "dry-run" => \$dry_run,
+                   "envelope-sender=s" => \$envelope_sender,
+                   "thread!" => \$thread,
         );
 
 unless ($rc) {
@@ -208,7 +225,7 @@ sub format_2822_time {
 my %parse_alias = (
        # multiline formats can be supported in the future
        mutt => sub { my $fh = shift; while (<$fh>) {
-               if (/^alias\s+(\S+)\s+(.*)$/) {
+               if (/^\s*alias\s+(\S+)\s+(.*)$/) {
                        my ($alias, $addr) = ($1, $2);
                        $addr =~ s/#.*$//; # mutt allows # comments
                         # commas delimit multiple addresses
@@ -270,6 +287,7 @@ sub expand_aliases {
 }
 
 @to = expand_aliases(@to);
+@to = (map { sanitize_address_rfc822($_) } @to);
 @initial_cc = expand_aliases(@initial_cc);
 @bcclist = expand_aliases(@bcclist);
 
@@ -282,7 +300,7 @@ sub expand_aliases {
        $prompting++;
 }
 
-if (!defined $initial_reply_to && $prompting) {
+if ($thread && !defined $initial_reply_to && $prompting) {
        do {
                $_= $term->readline("Message-ID to be used as In-Reply-To for the first email? ",
                        $initial_reply_to);
@@ -407,13 +425,21 @@ sub extract_valid_address {
 # 1 second since the last time we were called.
 
 # We'll setup a template for the message id, using the "from" address:
-my $message_id_from = extract_valid_address($from);
-my $message_id_template = "<%s-git-send-email-$message_id_from>";
 
 sub make_message_id
 {
        my $date = time;
        my $pseudo_rand = int (rand(4200));
+       my $du_part;
+       for ($from, $committer, $author) {
+               $du_part = extract_valid_address($_);
+               last if ($du_part ne '');
+       }
+       if ($du_part eq '') {
+               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";
        #print "new message id = $message_id\n"; # Was useful for debugging
 }
@@ -457,16 +483,21 @@ sub send_message
        }
 
        my $cc = join(", ", unique_email_list(@cc));
+       my $ccline = "";
+       if ($cc ne '') {
+               $ccline = "\nCc: $cc";
+       }
        $from = sanitize_address_rfc822($from);
+       make_message_id();
+
        my $header = "From: $from
-To: $to
-Cc: $cc
+To: $to${ccline}
 Subject: $subject
 Date: $date
 Message-Id: $message_id
 X-Mailer: git-send-email $gitversion
 ";
-       if ($reply_to) {
+       if ($thread && $reply_to) {
 
                $header .= "In-Reply-To: $reply_to\n";
                $header .= "References: $references\n";
@@ -476,6 +507,11 @@ sub send_message
        }
 
        my @sendmail_parameters = ('-i', @recipients);
+       my $raw_from = $from;
+       $raw_from = $envelope_sender if (defined $envelope_sender);
+       $raw_from = extract_valid_address($raw_from);
+       unshift (@sendmail_parameters,
+                       '-f', $raw_from) if(defined $envelope_sender);
 
        if ($dry_run) {
                # We don't want to send the email.
@@ -490,7 +526,7 @@ sub send_message
        } else {
                require Net::SMTP;
                $smtp ||= Net::SMTP->new( $smtp_server );
-               $smtp->mail( $from ) or die $smtp->message;
+               $smtp->mail( $raw_from ) or die $smtp->message;
                $smtp->to( @recipients ) or die $smtp->message;
                $smtp->data or die $smtp->message;
                $smtp->datasend("$header\n$message") or die $smtp->message;
@@ -501,10 +537,10 @@ sub send_message
                printf (($dry_run ? "Dry-" : "")."Sent %s\n", $subject);
        } else {
                print (($dry_run ? "Dry-" : "")."OK. Log says:\nDate: $date\n");
-               if ($smtp) {
+               if ($smtp_server !~ m#^/#) {
                        print "Server: $smtp_server\n";
-                       print "MAIL FROM: $from\n";
-                       print "RCPT TO: ".join(',',@recipients)."\n";
+                       print "MAIL FROM:<$raw_from>\n";
+                       print "RCPT TO:".join(',',(map { "<$_>" } @recipients))."\n";
                } else {
                        print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n";
                }
@@ -520,7 +556,6 @@ sub send_message
 
 $reply_to = $initial_reply_to;
 $references = $initial_reply_to || '';
-make_message_id();
 $subject = $initial_subject;
 
 foreach my $t (@files) {
@@ -548,7 +583,8 @@ sub send_message
                                        $subject = $1;
 
                                } elsif (/^(Cc|From):\s+(.*)$/) {
-                                       if ($2 eq $from) {
+                                       if (unquote_rfc2047($2) eq $from) {
+                                               $from = $2;
                                                next if ($suppress_from);
                                        }
                                        elsif ($1 eq 'From') {
@@ -586,7 +622,7 @@ sub send_message
                        }
                } else {
                        $message .=  $_;
-                       if (/^(Signed-off-by|Cc): (.*)$/i && !$no_signed_off_cc) {
+                       if (/^(Signed-off-by|Cc): (.*)$/i && $signed_off_cc) {
                                my $c = $2;
                                chomp $c;
                                push @cc, $c;
@@ -613,7 +649,6 @@ sub send_message
                        $references = "$message_id";
                }
        }
-       make_message_id();
 }
 
 if ($compose) {