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