git-send-email.perlon commit Merge branch 'js/commit-format' (8ab3e18)
   1#!/usr/bin/perl -w
   2#
   3# Copyright 2002,2005 Greg Kroah-Hartman <greg@kroah.com>
   4# Copyright 2005 Ryan Anderson <ryan@michonline.com>
   5#
   6# GPL v2 (See COPYING)
   7#
   8# Ported to support git "mbox" format files by Ryan Anderson <ryan@michonline.com>
   9#
  10# Sends a collection of emails to the given email addresses, disturbingly fast.
  11#
  12# Supports two formats:
  13# 1. mbox format files (ignoring most headers and MIME formatting - this is designed for sending patches)
  14# 2. The original format support by Greg's script:
  15#    first line of the message is who to CC,
  16#    and second line is the subject of the message.
  17#
  18
  19use strict;
  20use warnings;
  21use Term::ReadLine;
  22use Getopt::Long;
  23use Data::Dumper;
  24use Git;
  25
  26package FakeTerm;
  27sub new {
  28        my ($class, $reason) = @_;
  29        return bless \$reason, shift;
  30}
  31sub readline {
  32        my $self = shift;
  33        die "Cannot use readline on FakeTerm: $$self";
  34}
  35package main;
  36
  37
  38sub usage {
  39        print <<EOT;
  40git-send-email [options] <file | directory>...
  41Options:
  42   --from         Specify the "From:" line of the email to be sent.
  43
  44   --to           Specify the primary "To:" line of the email.
  45
  46   --cc           Specify an initial "Cc:" list for the entire series
  47                  of emails.
  48
  49   --bcc          Specify a list of email addresses that should be Bcc:
  50                  on all the emails.
  51
  52   --compose      Use \$EDITOR to edit an introductory message for the
  53                  patch series.
  54
  55   --subject      Specify the initial "Subject:" line.
  56                  Only necessary if --compose is also set.  If --compose
  57                  is not set, this will be prompted for.
  58
  59   --in-reply-to  Specify the first "In-Reply-To:" header line.
  60                  Only used if --compose is also set.  If --compose is not
  61                  set, this will be prompted for.
  62
  63   --chain-reply-to If set, the replies will all be to the previous
  64                  email sent, rather than to the first email sent.
  65                  Defaults to on.
  66
  67   --no-signed-off-cc Suppress the automatic addition of email addresses
  68                 that appear in a Signed-off-by: line, to the cc: list.
  69                 Note: Using this option is not recommended.
  70
  71   --smtp-server  If set, specifies the outgoing SMTP server to use.
  72                  Defaults to localhost.
  73
  74   --suppress-from Suppress sending emails to yourself if your address
  75                  appears in a From: line.
  76
  77   --quiet        Make git-send-email less verbose.  One line per email
  78                  should be all that is output.
  79
  80EOT
  81        exit(1);
  82}
  83
  84# most mail servers generate the Date: header, but not all...
  85sub format_2822_time {
  86        my ($time) = @_;
  87        my @localtm = localtime($time);
  88        my @gmttm = gmtime($time);
  89        my $localmin = $localtm[1] + $localtm[2] * 60;
  90        my $gmtmin = $gmttm[1] + $gmttm[2] * 60;
  91        if ($localtm[0] != $gmttm[0]) {
  92                die "local zone differs from GMT by a non-minute interval\n";
  93        }
  94        if ((($gmttm[6] + 1) % 7) == $localtm[6]) {
  95                $localmin += 1440;
  96        } elsif ((($gmttm[6] - 1) % 7) == $localtm[6]) {
  97                $localmin -= 1440;
  98        } elsif ($gmttm[6] != $localtm[6]) {
  99                die "local time offset greater than or equal to 24 hours\n";
 100        }
 101        my $offset = $localmin - $gmtmin;
 102        my $offhour = $offset / 60;
 103        my $offmin = abs($offset % 60);
 104        if (abs($offhour) >= 24) {
 105                die ("local time offset greater than or equal to 24 hours\n");
 106        }
 107
 108        return sprintf("%s, %2d %s %d %02d:%02d:%02d %s%02d%02d",
 109                       qw(Sun Mon Tue Wed Thu Fri Sat)[$localtm[6]],
 110                       $localtm[3],
 111                       qw(Jan Feb Mar Apr May Jun
 112                          Jul Aug Sep Oct Nov Dec)[$localtm[4]],
 113                       $localtm[5]+1900,
 114                       $localtm[2],
 115                       $localtm[1],
 116                       $localtm[0],
 117                       ($offset >= 0) ? '+' : '-',
 118                       abs($offhour),
 119                       $offmin,
 120                       );
 121}
 122
 123my $have_email_valid = eval { require Email::Valid; 1 };
 124my $smtp;
 125
 126sub unique_email_list(@);
 127sub cleanup_compose_files();
 128
 129# Constants (essentially)
 130my $compose_filename = ".msg.$$";
 131
 132# Variables we fill in automatically, or via prompting:
 133my (@to,@cc,@initial_cc,@bcclist,@xh,
 134        $initial_reply_to,$initial_subject,@files,$from,$compose,$time);
 135
 136# Behavior modification variables
 137my ($chain_reply_to, $quiet, $suppress_from, $no_signed_off_cc,
 138        $dry_run) = (1, 0, 0, 0, 0);
 139my $smtp_server;
 140
 141# Example reply to:
 142#$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
 143
 144my $repo = Git->repository();
 145my $term = eval {
 146        new Term::ReadLine 'git-send-email';
 147};
 148if ($@) {
 149        $term = new FakeTerm "$@: going non-interactive";
 150}
 151
 152# Begin by accumulating all the variables (defined above), that we will end up
 153# needing, first, from the command line:
 154
 155my $rc = GetOptions("from=s" => \$from,
 156                    "in-reply-to=s" => \$initial_reply_to,
 157                    "subject=s" => \$initial_subject,
 158                    "to=s" => \@to,
 159                    "cc=s" => \@initial_cc,
 160                    "bcc=s" => \@bcclist,
 161                    "chain-reply-to!" => \$chain_reply_to,
 162                    "smtp-server=s" => \$smtp_server,
 163                    "compose" => \$compose,
 164                    "quiet" => \$quiet,
 165                    "suppress-from" => \$suppress_from,
 166                    "no-signed-off-cc|no-signed-off-by-cc" => \$no_signed_off_cc,
 167                    "dry-run" => \$dry_run,
 168         );
 169
 170unless ($rc) {
 171    usage();
 172}
 173
 174# Verify the user input
 175
 176foreach my $entry (@to) {
 177        die "Comma in --to entry: $entry'\n" unless $entry !~ m/,/;
 178}
 179
 180foreach my $entry (@initial_cc) {
 181        die "Comma in --cc entry: $entry'\n" unless $entry !~ m/,/;
 182}
 183
 184foreach my $entry (@bcclist) {
 185        die "Comma in --bcclist entry: $entry'\n" unless $entry !~ m/,/;
 186}
 187
 188# Now, let's fill any that aren't set in with defaults:
 189
 190my ($author) = $repo->ident_person('author');
 191my ($committer) = $repo->ident_person('committer');
 192
 193my %aliases;
 194my @alias_files = $repo->config('sendemail.aliasesfile');
 195my $aliasfiletype = $repo->config('sendemail.aliasfiletype');
 196my %parse_alias = (
 197        # multiline formats can be supported in the future
 198        mutt => sub { my $fh = shift; while (<$fh>) {
 199                if (/^alias\s+(\S+)\s+(.*)$/) {
 200                        my ($alias, $addr) = ($1, $2);
 201                        $addr =~ s/#.*$//; # mutt allows # comments
 202                         # commas delimit multiple addresses
 203                        $aliases{$alias} = [ split(/\s*,\s*/, $addr) ];
 204                }}},
 205        mailrc => sub { my $fh = shift; while (<$fh>) {
 206                if (/^alias\s+(\S+)\s+(.*)$/) {
 207                        # spaces delimit multiple addresses
 208                        $aliases{$1} = [ split(/\s+/, $2) ];
 209                }}},
 210        pine => sub { my $fh = shift; while (<$fh>) {
 211                if (/^(\S+)\s+(.*)$/) {
 212                        $aliases{$1} = [ split(/\s*,\s*/, $2) ];
 213                }}},
 214        gnus => sub { my $fh = shift; while (<$fh>) {
 215                if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
 216                        $aliases{$1} = [ $2 ];
 217                }}}
 218);
 219
 220if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) {
 221        foreach my $file (@alias_files) {
 222                open my $fh, '<', $file or die "opening $file: $!\n";
 223                $parse_alias{$aliasfiletype}->($fh);
 224                close $fh;
 225        }
 226}
 227
 228my $prompting = 0;
 229if (!defined $from) {
 230        $from = $author || $committer;
 231        do {
 232                $_ = $term->readline("Who should the emails appear to be from? [$from] ");
 233        } while (!defined $_);
 234
 235        $from = $_ if ($_);
 236        print "Emails will be sent from: ", $from, "\n";
 237        $prompting++;
 238}
 239
 240if (!@to) {
 241        do {
 242                $_ = $term->readline("Who should the emails be sent to? ",
 243                                "");
 244        } while (!defined $_);
 245        my $to = $_;
 246        push @to, split /,/, $to;
 247        $prompting++;
 248}
 249
 250sub expand_aliases {
 251        my @cur = @_;
 252        my @last;
 253        do {
 254                @last = @cur;
 255                @cur = map { $aliases{$_} ? @{$aliases{$_}} : $_ } @last;
 256        } while (join(',',@cur) ne join(',',@last));
 257        return @cur;
 258}
 259
 260@to = expand_aliases(@to);
 261@initial_cc = expand_aliases(@initial_cc);
 262@bcclist = expand_aliases(@bcclist);
 263
 264if (!defined $initial_subject && $compose) {
 265        do {
 266                $_ = $term->readline("What subject should the emails start with? ",
 267                        $initial_subject);
 268        } while (!defined $_);
 269        $initial_subject = $_;
 270        $prompting++;
 271}
 272
 273if (!defined $initial_reply_to && $prompting) {
 274        do {
 275                $_= $term->readline("Message-ID to be used as In-Reply-To for the first email? ",
 276                        $initial_reply_to);
 277        } while (!defined $_);
 278
 279        $initial_reply_to = $_;
 280        $initial_reply_to =~ s/(^\s+|\s+$)//g;
 281}
 282
 283if (!$smtp_server) {
 284        $smtp_server = $repo->config('sendemail.smtpserver');
 285}
 286if (!$smtp_server) {
 287        foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) {
 288                if (-x $_) {
 289                        $smtp_server = $_;
 290                        last;
 291                }
 292        }
 293        $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
 294}
 295
 296if ($compose) {
 297        # Note that this does not need to be secure, but we will make a small
 298        # effort to have it be unique
 299        open(C,">",$compose_filename)
 300                or die "Failed to open for writing $compose_filename: $!";
 301        print C "From $from # This line is ignored.\n";
 302        printf C "Subject: %s\n\n", $initial_subject;
 303        printf C <<EOT;
 304GIT: Please enter your email below.
 305GIT: Lines beginning in "GIT: " will be removed.
 306GIT: Consider including an overall diffstat or table of contents
 307GIT: for the patch you are writing.
 308
 309EOT
 310        close(C);
 311
 312        my $editor = $ENV{EDITOR};
 313        $editor = 'vi' unless defined $editor;
 314        system($editor, $compose_filename);
 315
 316        open(C2,">",$compose_filename . ".final")
 317                or die "Failed to open $compose_filename.final : " . $!;
 318
 319        open(C,"<",$compose_filename)
 320                or die "Failed to open $compose_filename : " . $!;
 321
 322        while(<C>) {
 323                next if m/^GIT: /;
 324                print C2 $_;
 325        }
 326        close(C);
 327        close(C2);
 328
 329        do {
 330                $_ = $term->readline("Send this email? (y|n) ");
 331        } while (!defined $_);
 332
 333        if (uc substr($_,0,1) ne 'Y') {
 334                cleanup_compose_files();
 335                exit(0);
 336        }
 337
 338        @files = ($compose_filename . ".final");
 339}
 340
 341
 342# Now that all the defaults are set, process the rest of the command line
 343# arguments and collect up the files that need to be processed.
 344for my $f (@ARGV) {
 345        if (-d $f) {
 346                opendir(DH,$f)
 347                        or die "Failed to opendir $f: $!";
 348
 349                push @files, grep { -f $_ } map { +$f . "/" . $_ }
 350                                sort readdir(DH);
 351
 352        } elsif (-f $f) {
 353                push @files, $f;
 354
 355        } else {
 356                print STDERR "Skipping $f - not found.\n";
 357        }
 358}
 359
 360if (@files) {
 361        unless ($quiet) {
 362                print $_,"\n" for (@files);
 363        }
 364} else {
 365        print STDERR "\nNo patch files specified!\n\n";
 366        usage();
 367}
 368
 369# Variables we set as part of the loop over files
 370our ($message_id, $cc, %mail, $subject, $reply_to, $references, $message);
 371
 372sub extract_valid_address {
 373        my $address = shift;
 374        my $local_part_regexp = '[^<>"\s@]+';
 375        my $domain_regexp = '[^.<>"\s@]+(?:\.[^.<>"\s@]+)+';
 376
 377        # check for a local address:
 378        return $address if ($address =~ /^($local_part_regexp)$/);
 379
 380        if ($have_email_valid) {
 381                return scalar Email::Valid->address($address);
 382        } else {
 383                # less robust/correct than the monster regexp in Email::Valid,
 384                # but still does a 99% job, and one less dependency
 385                $address =~ /($local_part_regexp\@$domain_regexp)/;
 386                return $1;
 387        }
 388}
 389
 390# Usually don't need to change anything below here.
 391
 392# we make a "fake" message id by taking the current number
 393# of seconds since the beginning of Unix time and tacking on
 394# a random number to the end, in case we are called quicker than
 395# 1 second since the last time we were called.
 396
 397# We'll setup a template for the message id, using the "from" address:
 398my $message_id_from = extract_valid_address($from);
 399my $message_id_template = "<%s-git-send-email-$message_id_from>";
 400
 401sub make_message_id
 402{
 403        my $date = time;
 404        my $pseudo_rand = int (rand(4200));
 405        $message_id = sprintf $message_id_template, "$date$pseudo_rand";
 406        #print "new message id = $message_id\n"; # Was useful for debugging
 407}
 408
 409
 410
 411$cc = "";
 412$time = time - scalar $#files;
 413
 414sub unquote_rfc2047 {
 415        local ($_) = @_;
 416        if (s/=\?utf-8\?q\?(.*)\?=/$1/g) {
 417                s/_/ /g;
 418                s/=([0-9A-F]{2})/chr(hex($1))/eg;
 419        }
 420        return "$_";
 421}
 422
 423sub send_message
 424{
 425        my @recipients = unique_email_list(@to);
 426        my $to = join (",\n\t", @recipients);
 427        @recipients = unique_email_list(@recipients,@cc,@bcclist);
 428        my $date = format_2822_time($time++);
 429        my $gitversion = '@@GIT_VERSION@@';
 430        if ($gitversion =~ m/..GIT_VERSION../) {
 431            $gitversion = Git::version();
 432        }
 433
 434        my ($author_name) = ($from =~ /^(.*?)\s+</);
 435        if ($author_name && $author_name =~ /\./ && $author_name !~ /^".*"$/) {
 436                my ($name, $addr) = ($from =~ /^(.*?)(\s+<.*)/);
 437                $from = "\"$name\"$addr";
 438        }
 439        my $header = "From: $from
 440To: $to
 441Cc: $cc
 442Subject: $subject
 443Date: $date
 444Message-Id: $message_id
 445X-Mailer: git-send-email $gitversion
 446";
 447        if ($reply_to) {
 448
 449                $header .= "In-Reply-To: $reply_to\n";
 450                $header .= "References: $references\n";
 451        }
 452        if (@xh) {
 453                $header .= join("\n", @xh) . "\n";
 454        }
 455
 456        if ($dry_run) {
 457                # We don't want to send the email.
 458        } elsif ($smtp_server =~ m#^/#) {
 459                my $pid = open my $sm, '|-';
 460                defined $pid or die $!;
 461                if (!$pid) {
 462                        exec($smtp_server,'-i',
 463                             map { extract_valid_address($_) }
 464                             @recipients) or die $!;
 465                }
 466                print $sm "$header\n$message";
 467                close $sm or die $?;
 468        } else {
 469                require Net::SMTP;
 470                $smtp ||= Net::SMTP->new( $smtp_server );
 471                $smtp->mail( $from ) or die $smtp->message;
 472                $smtp->to( @recipients ) or die $smtp->message;
 473                $smtp->data or die $smtp->message;
 474                $smtp->datasend("$header\n$message") or die $smtp->message;
 475                $smtp->dataend() or die $smtp->message;
 476                $smtp->ok or die "Failed to send $subject\n".$smtp->message;
 477        }
 478        if ($quiet) {
 479                printf "Sent %s\n", $subject;
 480        } else {
 481                print "OK. Log says:\nDate: $date\n";
 482                if ($smtp) {
 483                        print "Server: $smtp_server\n";
 484                } else {
 485                        print "Sendmail: $smtp_server\n";
 486                }
 487                print "From: $from\nSubject: $subject\nCc: $cc\nTo: $to\n\n";
 488                if ($smtp) {
 489                        print "Result: ", $smtp->code, ' ',
 490                                ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
 491                } else {
 492                        print "Result: OK\n";
 493                }
 494        }
 495}
 496
 497$reply_to = $initial_reply_to;
 498$references = $initial_reply_to || '';
 499make_message_id();
 500$subject = $initial_subject;
 501
 502foreach my $t (@files) {
 503        open(F,"<",$t) or die "can't open file $t";
 504
 505        my $author_not_sender = undef;
 506        @cc = @initial_cc;
 507        @xh = ();
 508        my $input_format = undef;
 509        my $header_done = 0;
 510        $message = "";
 511        while(<F>) {
 512                if (!$header_done) {
 513                        if (/^From /) {
 514                                $input_format = 'mbox';
 515                                next;
 516                        }
 517                        chomp;
 518                        if (!defined $input_format && /^[-A-Za-z]+:\s/) {
 519                                $input_format = 'mbox';
 520                        }
 521
 522                        if (defined $input_format && $input_format eq 'mbox') {
 523                                if (/^Subject:\s+(.*)$/) {
 524                                        $subject = $1;
 525
 526                                } elsif (/^(Cc|From):\s+(.*)$/) {
 527                                        if ($2 eq $from) {
 528                                                next if ($suppress_from);
 529                                        }
 530                                        elsif ($1 eq 'From') {
 531                                                $author_not_sender = $2;
 532                                        }
 533                                        printf("(mbox) Adding cc: %s from line '%s'\n",
 534                                                $2, $_) unless $quiet;
 535                                        push @cc, $2;
 536                                }
 537                                elsif (!/^Date:\s/ && /^[-A-Za-z]+:\s+\S/) {
 538                                        push @xh, $_;
 539                                }
 540
 541                        } else {
 542                                # In the traditional
 543                                # "send lots of email" format,
 544                                # line 1 = cc
 545                                # line 2 = subject
 546                                # So let's support that, too.
 547                                $input_format = 'lots';
 548                                if (@cc == 0) {
 549                                        printf("(non-mbox) Adding cc: %s from line '%s'\n",
 550                                                $_, $_) unless $quiet;
 551
 552                                        push @cc, $_;
 553
 554                                } elsif (!defined $subject) {
 555                                        $subject = $_;
 556                                }
 557                        }
 558
 559                        # A whitespace line will terminate the headers
 560                        if (m/^\s*$/) {
 561                                $header_done = 1;
 562                        }
 563                } else {
 564                        $message .=  $_;
 565                        if (/^Signed-off-by: (.*)$/i && !$no_signed_off_cc) {
 566                                my $c = $1;
 567                                chomp $c;
 568                                push @cc, $c;
 569                                printf("(sob) Adding cc: %s from line '%s'\n",
 570                                        $c, $_) unless $quiet;
 571                        }
 572                }
 573        }
 574        close F;
 575        if (defined $author_not_sender) {
 576                $author_not_sender = unquote_rfc2047($author_not_sender);
 577                $message = "From: $author_not_sender\n\n$message";
 578        }
 579
 580        $cc = join(", ", unique_email_list(@cc));
 581
 582        send_message();
 583
 584        # set up for the next message
 585        if ($chain_reply_to || !defined $reply_to || length($reply_to) == 0) {
 586                $reply_to = $message_id;
 587                if (length $references > 0) {
 588                        $references .= " $message_id";
 589                } else {
 590                        $references = "$message_id";
 591                }
 592        }
 593        make_message_id();
 594}
 595
 596if ($compose) {
 597        cleanup_compose_files();
 598}
 599
 600sub cleanup_compose_files() {
 601        unlink($compose_filename, $compose_filename . ".final");
 602
 603}
 604
 605$smtp->quit if $smtp;
 606
 607sub unique_email_list(@) {
 608        my %seen;
 609        my @emails;
 610
 611        foreach my $entry (@_) {
 612                if (my $clean = extract_valid_address($entry)) {
 613                        $seen{$clean} ||= 0;
 614                        next if $seen{$clean}++;
 615                        push @emails, $entry;
 616                } else {
 617                        print STDERR "W: unable to extract a valid address",
 618                                        " from: $entry\n";
 619                }
 620        }
 621        return @emails;
 622}