git-clone: split up long &&-command-chain and use a function for cleanup
[git/jnareb-git/bp-gitweb.git] / git-send-email.perl
blob87f59fa313f4483dc15846979e80e8a8aec3d1f5
1 #!/usr/bin/perl -w
3 # Copyright 2002,2005 Greg Kroah-Hartman <greg@kroah.com>
4 # Copyright 2005 Ryan Anderson <ryan@michonline.com>
6 # GPL v2 (See COPYING)
8 # Ported to support git "mbox" format files by Ryan Anderson <ryan@michonline.com>
10 # Sends a collection of emails to the given email addresses, disturbingly fast.
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.
19 use strict;
20 use warnings;
21 use Term::ReadLine;
22 use Getopt::Long;
23 use Data::Dumper;
24 use Git;
26 package FakeTerm;
27 sub new {
28 my ($class, $reason) = @_;
29 return bless \$reason, shift;
31 sub readline {
32 my $self = shift;
33 die "Cannot use readline on FakeTerm: $$self";
35 package main;
38 sub usage {
39 print <<EOT;
40 git-send-email [options] <file | directory>...
41 Options:
42 --from Specify the "From:" line of the email to be sent.
44 --to Specify the primary "To:" line of the email.
46 --cc Specify an initial "Cc:" list for the entire series
47 of emails.
49 --bcc Specify a list of email addresses that should be Bcc:
50 on all the emails.
52 --compose Use \$EDITOR to edit an introductory message for the
53 patch series.
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.
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.
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.
67 --signed-off-cc Automatically add email addresses that appear in
68 Signed-off-by: or Cc: lines to the cc: list. Defaults to on.
70 --smtp-server If set, specifies the outgoing SMTP server to use.
71 Defaults to localhost.
73 --suppress-from Suppress sending emails to yourself if your address
74 appears in a From: line. Defaults to off.
76 --thread Specify that the "In-Reply-To:" header should be set on all
77 emails. Defaults to on.
79 --quiet Make git-send-email less verbose. One line per email
80 should be all that is output.
82 --dry-run Do everything except actually send the emails.
84 --envelope-sender Specify the envelope sender used to send the emails.
86 EOT
87 exit(1);
90 # most mail servers generate the Date: header, but not all...
91 sub format_2822_time {
92 my ($time) = @_;
93 my @localtm = localtime($time);
94 my @gmttm = gmtime($time);
95 my $localmin = $localtm[1] + $localtm[2] * 60;
96 my $gmtmin = $gmttm[1] + $gmttm[2] * 60;
97 if ($localtm[0] != $gmttm[0]) {
98 die "local zone differs from GMT by a non-minute interval\n";
100 if ((($gmttm[6] + 1) % 7) == $localtm[6]) {
101 $localmin += 1440;
102 } elsif ((($gmttm[6] - 1) % 7) == $localtm[6]) {
103 $localmin -= 1440;
104 } elsif ($gmttm[6] != $localtm[6]) {
105 die "local time offset greater than or equal to 24 hours\n";
107 my $offset = $localmin - $gmtmin;
108 my $offhour = $offset / 60;
109 my $offmin = abs($offset % 60);
110 if (abs($offhour) >= 24) {
111 die ("local time offset greater than or equal to 24 hours\n");
114 return sprintf("%s, %2d %s %d %02d:%02d:%02d %s%02d%02d",
115 qw(Sun Mon Tue Wed Thu Fri Sat)[$localtm[6]],
116 $localtm[3],
117 qw(Jan Feb Mar Apr May Jun
118 Jul Aug Sep Oct Nov Dec)[$localtm[4]],
119 $localtm[5]+1900,
120 $localtm[2],
121 $localtm[1],
122 $localtm[0],
123 ($offset >= 0) ? '+' : '-',
124 abs($offhour),
125 $offmin,
129 my $have_email_valid = eval { require Email::Valid; 1 };
130 my $smtp;
132 sub unique_email_list(@);
133 sub cleanup_compose_files();
135 # Constants (essentially)
136 my $compose_filename = ".msg.$$";
138 # Variables we fill in automatically, or via prompting:
139 my (@to,@cc,@initial_cc,@bcclist,@xh,
140 $initial_reply_to,$initial_subject,@files,$from,$compose,$time);
142 my $smtp_server;
143 my $envelope_sender;
145 # Example reply to:
146 #$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
148 my $repo = Git->repository();
149 my $term = eval {
150 new Term::ReadLine 'git-send-email';
152 if ($@) {
153 $term = new FakeTerm "$@: going non-interactive";
156 # Behavior modification variables
157 my ($quiet, $dry_run) = (0, 0);
159 # Variables with corresponding config settings
160 my ($thread, $chain_reply_to, $suppress_from, $signed_off_cc);
162 my %config_settings = (
163 "thread" => [\$thread, 1],
164 "chainreplyto" => [\$chain_reply_to, 1],
165 "suppressfrom" => [\$suppress_from, 0],
166 "signedoffcc" => [\$signed_off_cc, 1],
169 foreach my $setting (keys %config_settings) {
170 my $config = $repo->config_bool("sendemail.$setting");
171 ${$config_settings{$setting}->[0]} = (defined $config) ? $config : $config_settings{$setting}->[1];
174 @bcclist = $repo->config('sendemail.bcc');
175 if (!@bcclist or !$bcclist[0]) {
176 @bcclist = ();
179 # Begin by accumulating all the variables (defined above), that we will end up
180 # needing, first, from the command line:
182 my $rc = GetOptions("from=s" => \$from,
183 "in-reply-to=s" => \$initial_reply_to,
184 "subject=s" => \$initial_subject,
185 "to=s" => \@to,
186 "cc=s" => \@initial_cc,
187 "bcc=s" => \@bcclist,
188 "chain-reply-to!" => \$chain_reply_to,
189 "smtp-server=s" => \$smtp_server,
190 "compose" => \$compose,
191 "quiet" => \$quiet,
192 "suppress-from!" => \$suppress_from,
193 "signed-off-cc|signed-off-by-cc!" => \$signed_off_cc,
194 "dry-run" => \$dry_run,
195 "envelope-sender=s" => \$envelope_sender,
196 "thread!" => \$thread,
199 unless ($rc) {
200 usage();
203 # Verify the user input
205 foreach my $entry (@to) {
206 die "Comma in --to entry: $entry'\n" unless $entry !~ m/,/;
209 foreach my $entry (@initial_cc) {
210 die "Comma in --cc entry: $entry'\n" unless $entry !~ m/,/;
213 foreach my $entry (@bcclist) {
214 die "Comma in --bcclist entry: $entry'\n" unless $entry !~ m/,/;
217 # Now, let's fill any that aren't set in with defaults:
219 my ($author) = $repo->ident_person('author');
220 my ($committer) = $repo->ident_person('committer');
222 my %aliases;
223 my @alias_files = $repo->config('sendemail.aliasesfile');
224 my $aliasfiletype = $repo->config('sendemail.aliasfiletype');
225 my %parse_alias = (
226 # multiline formats can be supported in the future
227 mutt => sub { my $fh = shift; while (<$fh>) {
228 if (/^\s*alias\s+(\S+)\s+(.*)$/) {
229 my ($alias, $addr) = ($1, $2);
230 $addr =~ s/#.*$//; # mutt allows # comments
231 # commas delimit multiple addresses
232 $aliases{$alias} = [ split(/\s*,\s*/, $addr) ];
233 }}},
234 mailrc => sub { my $fh = shift; while (<$fh>) {
235 if (/^alias\s+(\S+)\s+(.*)$/) {
236 # spaces delimit multiple addresses
237 $aliases{$1} = [ split(/\s+/, $2) ];
238 }}},
239 pine => sub { my $fh = shift; while (<$fh>) {
240 if (/^(\S+)\s+(.*)$/) {
241 $aliases{$1} = [ split(/\s*,\s*/, $2) ];
242 }}},
243 gnus => sub { my $fh = shift; while (<$fh>) {
244 if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
245 $aliases{$1} = [ $2 ];
249 if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) {
250 foreach my $file (@alias_files) {
251 open my $fh, '<', $file or die "opening $file: $!\n";
252 $parse_alias{$aliasfiletype}->($fh);
253 close $fh;
257 my $prompting = 0;
258 if (!defined $from) {
259 $from = $author || $committer;
260 do {
261 $_ = $term->readline("Who should the emails appear to be from? [$from] ");
262 } while (!defined $_);
264 $from = $_ if ($_);
265 print "Emails will be sent from: ", $from, "\n";
266 $prompting++;
269 if (!@to) {
270 do {
271 $_ = $term->readline("Who should the emails be sent to? ",
272 "");
273 } while (!defined $_);
274 my $to = $_;
275 push @to, split /,/, $to;
276 $prompting++;
279 sub expand_aliases {
280 my @cur = @_;
281 my @last;
282 do {
283 @last = @cur;
284 @cur = map { $aliases{$_} ? @{$aliases{$_}} : $_ } @last;
285 } while (join(',',@cur) ne join(',',@last));
286 return @cur;
289 @to = expand_aliases(@to);
290 @to = (map { sanitize_address_rfc822($_) } @to);
291 @initial_cc = expand_aliases(@initial_cc);
292 @bcclist = expand_aliases(@bcclist);
294 if (!defined $initial_subject && $compose) {
295 do {
296 $_ = $term->readline("What subject should the emails start with? ",
297 $initial_subject);
298 } while (!defined $_);
299 $initial_subject = $_;
300 $prompting++;
303 if ($thread && !defined $initial_reply_to && $prompting) {
304 do {
305 $_= $term->readline("Message-ID to be used as In-Reply-To for the first email? ",
306 $initial_reply_to);
307 } while (!defined $_);
309 $initial_reply_to = $_;
310 $initial_reply_to =~ s/(^\s+|\s+$)//g;
313 if (!$smtp_server) {
314 $smtp_server = $repo->config('sendemail.smtpserver');
316 if (!$smtp_server) {
317 foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) {
318 if (-x $_) {
319 $smtp_server = $_;
320 last;
323 $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
326 if ($compose) {
327 # Note that this does not need to be secure, but we will make a small
328 # effort to have it be unique
329 open(C,">",$compose_filename)
330 or die "Failed to open for writing $compose_filename: $!";
331 print C "From $from # This line is ignored.\n";
332 printf C "Subject: %s\n\n", $initial_subject;
333 printf C <<EOT;
334 GIT: Please enter your email below.
335 GIT: Lines beginning in "GIT: " will be removed.
336 GIT: Consider including an overall diffstat or table of contents
337 GIT: for the patch you are writing.
340 close(C);
342 my $editor = $ENV{EDITOR};
343 $editor = 'vi' unless defined $editor;
344 system($editor, $compose_filename);
346 open(C2,">",$compose_filename . ".final")
347 or die "Failed to open $compose_filename.final : " . $!;
349 open(C,"<",$compose_filename)
350 or die "Failed to open $compose_filename : " . $!;
352 while(<C>) {
353 next if m/^GIT: /;
354 print C2 $_;
356 close(C);
357 close(C2);
359 do {
360 $_ = $term->readline("Send this email? (y|n) ");
361 } while (!defined $_);
363 if (uc substr($_,0,1) ne 'Y') {
364 cleanup_compose_files();
365 exit(0);
368 @files = ($compose_filename . ".final");
372 # Now that all the defaults are set, process the rest of the command line
373 # arguments and collect up the files that need to be processed.
374 for my $f (@ARGV) {
375 if (-d $f) {
376 opendir(DH,$f)
377 or die "Failed to opendir $f: $!";
379 push @files, grep { -f $_ } map { +$f . "/" . $_ }
380 sort readdir(DH);
382 } elsif (-f $f) {
383 push @files, $f;
385 } else {
386 print STDERR "Skipping $f - not found.\n";
390 if (@files) {
391 unless ($quiet) {
392 print $_,"\n" for (@files);
394 } else {
395 print STDERR "\nNo patch files specified!\n\n";
396 usage();
399 # Variables we set as part of the loop over files
400 our ($message_id, %mail, $subject, $reply_to, $references, $message);
402 sub extract_valid_address {
403 my $address = shift;
404 my $local_part_regexp = '[^<>"\s@]+';
405 my $domain_regexp = '[^.<>"\s@]+(?:\.[^.<>"\s@]+)+';
407 # check for a local address:
408 return $address if ($address =~ /^($local_part_regexp)$/);
410 if ($have_email_valid) {
411 return scalar Email::Valid->address($address);
412 } else {
413 # less robust/correct than the monster regexp in Email::Valid,
414 # but still does a 99% job, and one less dependency
415 $address =~ /($local_part_regexp\@$domain_regexp)/;
416 return $1;
420 # Usually don't need to change anything below here.
422 # we make a "fake" message id by taking the current number
423 # of seconds since the beginning of Unix time and tacking on
424 # a random number to the end, in case we are called quicker than
425 # 1 second since the last time we were called.
427 # We'll setup a template for the message id, using the "from" address:
429 sub make_message_id
431 my $date = time;
432 my $pseudo_rand = int (rand(4200));
433 my $du_part;
434 for ($from, $committer, $author) {
435 $du_part = extract_valid_address($_);
436 last if ($du_part ne '');
438 if ($du_part eq '') {
439 use Sys::Hostname qw();
440 $du_part = 'user@' . Sys::Hostname::hostname();
442 my $message_id_template = "<%s-git-send-email-$du_part>";
443 $message_id = sprintf $message_id_template, "$date$pseudo_rand";
444 #print "new message id = $message_id\n"; # Was useful for debugging
449 $time = time - scalar $#files;
451 sub unquote_rfc2047 {
452 local ($_) = @_;
453 if (s/=\?utf-8\?q\?(.*)\?=/$1/g) {
454 s/_/ /g;
455 s/=([0-9A-F]{2})/chr(hex($1))/eg;
457 return "$_";
460 # If an address contains a . in the name portion, the name must be quoted.
461 sub sanitize_address_rfc822
463 my ($recipient) = @_;
464 my ($recipient_name) = ($recipient =~ /^(.*?)\s+</);
465 if ($recipient_name && $recipient_name =~ /\./ && $recipient_name !~ /^".*"$/) {
466 my ($name, $addr) = ($recipient =~ /^(.*?)(\s+<.*)/);
467 $recipient = "\"$name\"$addr";
469 return $recipient;
472 sub send_message
474 my @recipients = unique_email_list(@to);
475 @cc = (map { sanitize_address_rfc822($_) } @cc);
476 my $to = join (",\n\t", @recipients);
477 @recipients = unique_email_list(@recipients,@cc,@bcclist);
478 @recipients = (map { extract_valid_address($_) } @recipients);
479 my $date = format_2822_time($time++);
480 my $gitversion = '@@GIT_VERSION@@';
481 if ($gitversion =~ m/..GIT_VERSION../) {
482 $gitversion = Git::version();
485 my $cc = join(", ", unique_email_list(@cc));
486 my $ccline = "";
487 if ($cc ne '') {
488 $ccline = "\nCc: $cc";
490 $from = sanitize_address_rfc822($from);
491 make_message_id();
493 my $header = "From: $from
494 To: $to${ccline}
495 Subject: $subject
496 Date: $date
497 Message-Id: $message_id
498 X-Mailer: git-send-email $gitversion
500 if ($thread && $reply_to) {
502 $header .= "In-Reply-To: $reply_to\n";
503 $header .= "References: $references\n";
505 if (@xh) {
506 $header .= join("\n", @xh) . "\n";
509 my @sendmail_parameters = ('-i', @recipients);
510 my $raw_from = $from;
511 $raw_from = $envelope_sender if (defined $envelope_sender);
512 $raw_from = extract_valid_address($raw_from);
513 unshift (@sendmail_parameters,
514 '-f', $raw_from) if(defined $envelope_sender);
516 if ($dry_run) {
517 # We don't want to send the email.
518 } elsif ($smtp_server =~ m#^/#) {
519 my $pid = open my $sm, '|-';
520 defined $pid or die $!;
521 if (!$pid) {
522 exec($smtp_server, @sendmail_parameters) or die $!;
524 print $sm "$header\n$message";
525 close $sm or die $?;
526 } else {
527 require Net::SMTP;
528 $smtp ||= Net::SMTP->new( $smtp_server );
529 $smtp->mail( $raw_from ) or die $smtp->message;
530 $smtp->to( @recipients ) or die $smtp->message;
531 $smtp->data or die $smtp->message;
532 $smtp->datasend("$header\n$message") or die $smtp->message;
533 $smtp->dataend() or die $smtp->message;
534 $smtp->ok or die "Failed to send $subject\n".$smtp->message;
536 if ($quiet) {
537 printf (($dry_run ? "Dry-" : "")."Sent %s\n", $subject);
538 } else {
539 print (($dry_run ? "Dry-" : "")."OK. Log says:\nDate: $date\n");
540 if ($smtp_server !~ m#^/#) {
541 print "Server: $smtp_server\n";
542 print "MAIL FROM:<$raw_from>\n";
543 print "RCPT TO:".join(',',(map { "<$_>" } @recipients))."\n";
544 } else {
545 print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n";
547 print "From: $from\nSubject: $subject\nCc: $cc\nTo: $to\n\n";
548 if ($smtp) {
549 print "Result: ", $smtp->code, ' ',
550 ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
551 } else {
552 print "Result: OK\n";
557 $reply_to = $initial_reply_to;
558 $references = $initial_reply_to || '';
559 $subject = $initial_subject;
561 foreach my $t (@files) {
562 open(F,"<",$t) or die "can't open file $t";
564 my $author_not_sender = undef;
565 @cc = @initial_cc;
566 @xh = ();
567 my $input_format = undef;
568 my $header_done = 0;
569 $message = "";
570 while(<F>) {
571 if (!$header_done) {
572 if (/^From /) {
573 $input_format = 'mbox';
574 next;
576 chomp;
577 if (!defined $input_format && /^[-A-Za-z]+:\s/) {
578 $input_format = 'mbox';
581 if (defined $input_format && $input_format eq 'mbox') {
582 if (/^Subject:\s+(.*)$/) {
583 $subject = $1;
585 } elsif (/^(Cc|From):\s+(.*)$/) {
586 if (unquote_rfc2047($2) eq $from) {
587 $from = $2;
588 next if ($suppress_from);
590 elsif ($1 eq 'From') {
591 $author_not_sender = $2;
593 printf("(mbox) Adding cc: %s from line '%s'\n",
594 $2, $_) unless $quiet;
595 push @cc, $2;
597 elsif (!/^Date:\s/ && /^[-A-Za-z]+:\s+\S/) {
598 push @xh, $_;
601 } else {
602 # In the traditional
603 # "send lots of email" format,
604 # line 1 = cc
605 # line 2 = subject
606 # So let's support that, too.
607 $input_format = 'lots';
608 if (@cc == 0) {
609 printf("(non-mbox) Adding cc: %s from line '%s'\n",
610 $_, $_) unless $quiet;
612 push @cc, $_;
614 } elsif (!defined $subject) {
615 $subject = $_;
619 # A whitespace line will terminate the headers
620 if (m/^\s*$/) {
621 $header_done = 1;
623 } else {
624 $message .= $_;
625 if (/^(Signed-off-by|Cc): (.*)$/i && $signed_off_cc) {
626 my $c = $2;
627 chomp $c;
628 push @cc, $c;
629 printf("(sob) Adding cc: %s from line '%s'\n",
630 $c, $_) unless $quiet;
634 close F;
635 if (defined $author_not_sender) {
636 $author_not_sender = unquote_rfc2047($author_not_sender);
637 $message = "From: $author_not_sender\n\n$message";
641 send_message();
643 # set up for the next message
644 if ($chain_reply_to || !defined $reply_to || length($reply_to) == 0) {
645 $reply_to = $message_id;
646 if (length $references > 0) {
647 $references .= "\n $message_id";
648 } else {
649 $references = "$message_id";
654 if ($compose) {
655 cleanup_compose_files();
658 sub cleanup_compose_files() {
659 unlink($compose_filename, $compose_filename . ".final");
663 $smtp->quit if $smtp;
665 sub unique_email_list(@) {
666 my %seen;
667 my @emails;
669 foreach my $entry (@_) {
670 if (my $clean = extract_valid_address($entry)) {
671 $seen{$clean} ||= 0;
672 next if $seen{$clean}++;
673 push @emails, $entry;
674 } else {
675 print STDERR "W: unable to extract a valid address",
676 " from: $entry\n";
679 return @emails;