Code

Don't dereference a strdup-returned NULL
[git.git] / git-send-email.perl
index 5e38a97cb62703f10b242111376ca26ee5888944..7c0c90bd21bbb009de81aa315bed1c947a32c423 100755 (executable)
@@ -79,6 +79,8 @@ Options:
 
    --dry-run     Do everything except actually send the emails.
 
+   --envelope-sender   Specify the envelope sender used to send the emails.
+
 EOT
        exit(1);
 }
@@ -139,6 +141,7 @@ my (@to,@cc,@initial_cc,@bcclist,@xh,
 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,8 +154,8 @@ if ($@) {
        $term = new FakeTerm "$@: going non-interactive";
 }
 
-my $def_chain = $repo->config_boolean('sendemail.chainreplyto');
-if ($def_chain and $def_chain eq 'false') {
+my $def_chain = $repo->config_bool('sendemail.chainreplyto');
+if (defined $def_chain and not $def_chain) {
     $chain_reply_to = 0;
 }
 
@@ -177,6 +180,7 @@ my $rc = GetOptions("from=s" => \$from,
                    "suppress-from" => \$suppress_from,
                    "no-signed-off-cc|no-signed-off-by-cc" => \$no_signed_off_cc,
                    "dry-run" => \$dry_run,
+                   "envelope-sender=s" => \$envelope_sender,
         );
 
 unless ($rc) {
@@ -208,7 +212,7 @@ my $aliasfiletype = $repo->config('sendemail.aliasfiletype');
 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 +274,7 @@ sub expand_aliases {
 }
 
 @to = expand_aliases(@to);
+@to = (map { sanitize_address_rfc822($_) } @to);
 @initial_cc = expand_aliases(@initial_cc);
 @bcclist = expand_aliases(@bcclist);
 
@@ -379,7 +384,7 @@ if (@files) {
 }
 
 # Variables we set as part of the loop over files
-our ($message_id, $cc, %mail, $subject, $reply_to, $references, $message);
+our ($message_id, %mail, $subject, $reply_to, $references, $message);
 
 sub extract_valid_address {
        my $address = shift;
@@ -420,7 +425,6 @@ sub make_message_id
 
 
 
-$cc = "";
 $time = time - scalar $#files;
 
 sub unquote_rfc2047 {
@@ -432,25 +436,39 @@ sub unquote_rfc2047 {
        return "$_";
 }
 
+# If an address contains a . in the name portion, the name must be quoted.
+sub sanitize_address_rfc822
+{
+       my ($recipient) = @_;
+       my ($recipient_name) = ($recipient =~ /^(.*?)\s+</);
+       if ($recipient_name && $recipient_name =~ /\./ && $recipient_name !~ /^".*"$/) {
+               my ($name, $addr) = ($recipient =~ /^(.*?)(\s+<.*)/);
+               $recipient = "\"$name\"$addr";
+       }
+       return $recipient;
+}
+
 sub send_message
 {
        my @recipients = unique_email_list(@to);
+       @cc = (map { sanitize_address_rfc822($_) } @cc);
        my $to = join (",\n\t", @recipients);
        @recipients = unique_email_list(@recipients,@cc,@bcclist);
+       @recipients = (map { extract_valid_address($_) } @recipients);
        my $date = format_2822_time($time++);
        my $gitversion = '@@GIT_VERSION@@';
        if ($gitversion =~ m/..GIT_VERSION../) {
            $gitversion = Git::version();
        }
 
-       my ($author_name) = ($from =~ /^(.*?)\s+</);
-       if ($author_name && $author_name =~ /\./ && $author_name !~ /^".*"$/) {
-               my ($name, $addr) = ($from =~ /^(.*?)(\s+<.*)/);
-               $from = "\"$name\"$addr";
+       my $cc = join(", ", unique_email_list(@cc));
+       my $ccline = "";
+       if ($cc ne '') {
+               $ccline = "\nCc: $cc";
        }
+       $from = sanitize_address_rfc822($from);
        my $header = "From: $from
-To: $to
-Cc: $cc
+To: $to${ccline}
 Subject: $subject
 Date: $date
 Message-Id: $message_id
@@ -465,22 +483,27 @@ X-Mailer: git-send-email $gitversion
                $header .= join("\n", @xh) . "\n";
        }
 
+       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.
        } elsif ($smtp_server =~ m#^/#) {
                my $pid = open my $sm, '|-';
                defined $pid or die $!;
                if (!$pid) {
-                       exec($smtp_server,'-i',
-                            map { extract_valid_address($_) }
-                            @recipients) or die $!;
+                       exec($smtp_server, @sendmail_parameters) or die $!;
                }
                print $sm "$header\n$message";
                close $sm or die $?;
        } 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;
@@ -488,13 +511,15 @@ X-Mailer: git-send-email $gitversion
                $smtp->ok or die "Failed to send $subject\n".$smtp->message;
        }
        if ($quiet) {
-               printf "Sent %s\n", $subject;
+               printf (($dry_run ? "Dry-" : "")."Sent %s\n", $subject);
        } else {
-               print "OK. Log says:\nDate: $date\n";
-               if ($smtp) {
+               print (($dry_run ? "Dry-" : "")."OK. Log says:\nDate: $date\n");
+               if ($smtp_server !~ m#^/#) {
                        print "Server: $smtp_server\n";
+                       print "MAIL FROM:<$raw_from>\n";
+                       print "RCPT TO:".join(',',(map { "<$_>" } @recipients))."\n";
                } else {
-                       print "Sendmail: $smtp_server\n";
+                       print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n";
                }
                print "From: $from\nSubject: $subject\nCc: $cc\nTo: $to\n\n";
                if ($smtp) {
@@ -536,7 +561,8 @@ foreach my $t (@files) {
                                        $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') {
@@ -589,7 +615,6 @@ foreach my $t (@files) {
                $message = "From: $author_not_sender\n\n$message";
        }
 
-       $cc = join(", ", unique_email_list(@cc));
 
        send_message();