]> git.ipfire.org Git - thirdparty/git.git/blame - git-send-email.perl
cvsimport: Improve formating consistency
[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
1b0baf14
MC
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
abec100c
BF
68 that appear in Signed-off-by: or Cc: lines to the cc:
69 list. Note: Using this option is not recommended.
1b0baf14
MC
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
4bc87a28 84# most mail servers generate the Date: header, but not all...
6bdca890
JN
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}
4bc87a28 122
567ffeb7 123my $have_email_valid = eval { require Email::Valid; 1 };
4bc87a28
EW
124my $smtp;
125
e205735d 126sub unique_email_list(@);
1f038a0c
RA
127sub cleanup_compose_files();
128
129# Constants (essentially)
130my $compose_filename = ".msg.$$";
e205735d 131
83b24437 132# Variables we fill in automatically, or via prompting:
ce91c2f6 133my (@to,@cc,@initial_cc,@bcclist,@xh,
58063245 134 $initial_reply_to,$initial_subject,@files,$from,$compose,$time);
83b24437 135
78488b2c 136# Behavior modification variables
6130259c
MW
137my ($chain_reply_to, $quiet, $suppress_from, $no_signed_off_cc,
138 $dry_run) = (1, 0, 0, 0, 0);
aca7ad76 139my $smtp_server;
78488b2c 140
9133261f 141# Example reply to:
83b24437 142#$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
83b24437 143
3cb8caf7 144my $repo = Git->repository();
280242d1
JH
145my $term = eval {
146 new Term::ReadLine 'git-send-email';
147};
148if ($@) {
149 $term = new FakeTerm "$@: going non-interactive";
150}
83b24437 151
4a62d3f5
AK
152my $def_chain = $repo->config_boolean('sendemail.chainreplyto');
153if ($def_chain and $def_chain eq 'false') {
154 $chain_reply_to = 0;
155}
156
157@bcclist = $repo->config('sendemail.bcc');
158if (!@bcclist or !$bcclist[0]) {
159 @bcclist = ();
160}
161
83b24437
RA
162# Begin by accumulating all the variables (defined above), that we will end up
163# needing, first, from the command line:
164
165my $rc = GetOptions("from=s" => \$from,
166 "in-reply-to=s" => \$initial_reply_to,
167 "subject=s" => \$initial_subject,
168 "to=s" => \@to,
da140f8b 169 "cc=s" => \@initial_cc,
58063245 170 "bcc=s" => \@bcclist,
78488b2c 171 "chain-reply-to!" => \$chain_reply_to,
3342d850 172 "smtp-server=s" => \$smtp_server,
1f038a0c 173 "compose" => \$compose,
30d08b34 174 "quiet" => \$quiet,
a985d595 175 "suppress-from" => \$suppress_from,
8e69b31e 176 "no-signed-off-cc|no-signed-off-by-cc" => \$no_signed_off_cc,
6130259c 177 "dry-run" => \$dry_run,
83b24437
RA
178 );
179
1b0baf14
MC
180unless ($rc) {
181 usage();
182}
183
79ee555b
EB
184# Verify the user input
185
186foreach my $entry (@to) {
187 die "Comma in --to entry: $entry'\n" unless $entry !~ m/,/;
188}
189
190foreach my $entry (@initial_cc) {
191 die "Comma in --cc entry: $entry'\n" unless $entry !~ m/,/;
192}
193
194foreach my $entry (@bcclist) {
195 die "Comma in --bcclist entry: $entry'\n" unless $entry !~ m/,/;
196}
197
83b24437
RA
198# Now, let's fill any that aren't set in with defaults:
199
c7a30e56
PB
200my ($author) = $repo->ident_person('author');
201my ($committer) = $repo->ident_person('committer');
83b24437 202
994d6c66 203my %aliases;
3cb8caf7
PB
204my @alias_files = $repo->config('sendemail.aliasesfile');
205my $aliasfiletype = $repo->config('sendemail.aliasfiletype');
994d6c66
EW
206my %parse_alias = (
207 # multiline formats can be supported in the future
208 mutt => sub { my $fh = shift; while (<$fh>) {
209 if (/^alias\s+(\S+)\s+(.*)$/) {
210 my ($alias, $addr) = ($1, $2);
211 $addr =~ s/#.*$//; # mutt allows # comments
212 # commas delimit multiple addresses
213 $aliases{$alias} = [ split(/\s*,\s*/, $addr) ];
214 }}},
215 mailrc => sub { my $fh = shift; while (<$fh>) {
216 if (/^alias\s+(\S+)\s+(.*)$/) {
217 # spaces delimit multiple addresses
218 $aliases{$1} = [ split(/\s+/, $2) ];
219 }}},
220 pine => sub { my $fh = shift; while (<$fh>) {
221 if (/^(\S+)\s+(.*)$/) {
222 $aliases{$1} = [ split(/\s*,\s*/, $2) ];
223 }}},
224 gnus => sub { my $fh = shift; while (<$fh>) {
225 if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
226 $aliases{$1} = [ $2 ];
227 }}}
228);
229
3cb8caf7 230if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) {
994d6c66
EW
231 foreach my $file (@alias_files) {
232 open my $fh, '<', $file or die "opening $file: $!\n";
233 $parse_alias{$aliasfiletype}->($fh);
234 close $fh;
235 }
236}
237
1f038a0c 238my $prompting = 0;
83b24437
RA
239if (!defined $from) {
240 $from = $author || $committer;
8037d1a3 241 do {
2ef58055 242 $_ = $term->readline("Who should the emails appear to be from? [$from] ");
ca9a7d65 243 } while (!defined $_);
8037d1a3 244
2ef58055 245 $from = $_ if ($_);
83b24437 246 print "Emails will be sent from: ", $from, "\n";
1f038a0c 247 $prompting++;
83b24437
RA
248}
249
250if (!@to) {
8037d1a3 251 do {
5825e5b2 252 $_ = $term->readline("Who should the emails be sent to? ",
8037d1a3
RA
253 "");
254 } while (!defined $_);
83b24437
RA
255 my $to = $_;
256 push @to, split /,/, $to;
1f038a0c 257 $prompting++;
83b24437
RA
258}
259
994d6c66
EW
260sub expand_aliases {
261 my @cur = @_;
262 my @last;
263 do {
264 @last = @cur;
265 @cur = map { $aliases{$_} ? @{$aliases{$_}} : $_ } @last;
266 } while (join(',',@cur) ne join(',',@last));
267 return @cur;
268}
269
270@to = expand_aliases(@to);
271@initial_cc = expand_aliases(@initial_cc);
58063245 272@bcclist = expand_aliases(@bcclist);
994d6c66 273
1f038a0c 274if (!defined $initial_subject && $compose) {
8037d1a3 275 do {
5825e5b2 276 $_ = $term->readline("What subject should the emails start with? ",
8037d1a3
RA
277 $initial_subject);
278 } while (!defined $_);
83b24437 279 $initial_subject = $_;
1f038a0c 280 $prompting++;
83b24437
RA
281}
282
1f038a0c 283if (!defined $initial_reply_to && $prompting) {
8037d1a3 284 do {
1f038a0c 285 $_= $term->readline("Message-ID to be used as In-Reply-To for the first email? ",
8037d1a3
RA
286 $initial_reply_to);
287 } while (!defined $_);
288
83b24437 289 $initial_reply_to = $_;
78488b2c 290 $initial_reply_to =~ s/(^\s+|\s+$)//g;
83b24437
RA
291}
292
6dcfa306
SV
293if (!$smtp_server) {
294 $smtp_server = $repo->config('sendemail.smtpserver');
295}
aca7ad76
EW
296if (!$smtp_server) {
297 foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) {
298 if (-x $_) {
299 $smtp_server = $_;
300 last;
301 }
302 }
303 $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
3342d850
RA
304}
305
1f038a0c
RA
306if ($compose) {
307 # Note that this does not need to be secure, but we will make a small
308 # effort to have it be unique
309 open(C,">",$compose_filename)
310 or die "Failed to open for writing $compose_filename: $!";
d366c703 311 print C "From $from # This line is ignored.\n";
1f038a0c
RA
312 printf C "Subject: %s\n\n", $initial_subject;
313 printf C <<EOT;
314GIT: Please enter your email below.
315GIT: Lines beginning in "GIT: " will be removed.
316GIT: Consider including an overall diffstat or table of contents
317GIT: for the patch you are writing.
318
319EOT
320 close(C);
321
322 my $editor = $ENV{EDITOR};
323 $editor = 'vi' unless defined $editor;
324 system($editor, $compose_filename);
325
326 open(C2,">",$compose_filename . ".final")
327 or die "Failed to open $compose_filename.final : " . $!;
328
329 open(C,"<",$compose_filename)
330 or die "Failed to open $compose_filename : " . $!;
331
332 while(<C>) {
333 next if m/^GIT: /;
334 print C2 $_;
335 }
336 close(C);
337 close(C2);
338
339 do {
340 $_ = $term->readline("Send this email? (y|n) ");
341 } while (!defined $_);
342
343 if (uc substr($_,0,1) ne 'Y') {
344 cleanup_compose_files();
345 exit(0);
346 }
347
348 @files = ($compose_filename . ".final");
349}
350
351
83b24437
RA
352# Now that all the defaults are set, process the rest of the command line
353# arguments and collect up the files that need to be processed.
354for my $f (@ARGV) {
355 if (-d $f) {
356 opendir(DH,$f)
357 or die "Failed to opendir $f: $!";
358
5825e5b2 359 push @files, grep { -f $_ } map { +$f . "/" . $_ }
8037d1a3
RA
360 sort readdir(DH);
361
83b24437
RA
362 } elsif (-f $f) {
363 push @files, $f;
364
365 } else {
366 print STDERR "Skipping $f - not found.\n";
367 }
368}
369
370if (@files) {
2718435b
RA
371 unless ($quiet) {
372 print $_,"\n" for (@files);
373 }
83b24437 374} else {
1b0baf14
MC
375 print STDERR "\nNo patch files specified!\n\n";
376 usage();
83b24437
RA
377}
378
379# Variables we set as part of the loop over files
7ccf7927 380our ($message_id, $cc, %mail, $subject, $reply_to, $references, $message);
83b24437 381
567ffeb7
EW
382sub extract_valid_address {
383 my $address = shift;
ad9c18f5 384 my $local_part_regexp = '[^<>"\s@]+';
09302e17 385 my $domain_regexp = '[^.<>"\s@]+(?:\.[^.<>"\s@]+)+';
db3106b2
EW
386
387 # check for a local address:
ad9c18f5 388 return $address if ($address =~ /^($local_part_regexp)$/);
db3106b2 389
567ffeb7 390 if ($have_email_valid) {
ad9c18f5 391 return scalar Email::Valid->address($address);
567ffeb7
EW
392 } else {
393 # less robust/correct than the monster regexp in Email::Valid,
394 # but still does a 99% job, and one less dependency
ad9c18f5 395 $address =~ /($local_part_regexp\@$domain_regexp)/;
e96fd305 396 return $1;
567ffeb7
EW
397 }
398}
83b24437
RA
399
400# Usually don't need to change anything below here.
401
402# we make a "fake" message id by taking the current number
403# of seconds since the beginning of Unix time and tacking on
404# a random number to the end, in case we are called quicker than
405# 1 second since the last time we were called.
8037d1a3
RA
406
407# We'll setup a template for the message id, using the "from" address:
567ffeb7 408my $message_id_from = extract_valid_address($from);
e205735d 409my $message_id_template = "<%s-git-send-email-$message_id_from>";
8037d1a3 410
83b24437
RA
411sub make_message_id
412{
72095d5c 413 my $date = time;
83b24437 414 my $pseudo_rand = int (rand(4200));
8037d1a3
RA
415 $message_id = sprintf $message_id_template, "$date$pseudo_rand";
416 #print "new message id = $message_id\n"; # Was useful for debugging
83b24437
RA
417}
418
419
420
421$cc = "";
a5370b16 422$time = time - scalar $#files;
83b24437 423
374c5905
JR
424sub unquote_rfc2047 {
425 local ($_) = @_;
426 if (s/=\?utf-8\?q\?(.*)\?=/$1/g) {
427 s/_/ /g;
428 s/=([0-9A-F]{2})/chr(hex($1))/eg;
429 }
3740b04f 430 return "$_";
374c5905
JR
431}
432
83b24437
RA
433sub send_message
434{
4bc87a28
EW
435 my @recipients = unique_email_list(@to);
436 my $to = join (",\n\t", @recipients);
58063245 437 @recipients = unique_email_list(@recipients,@cc,@bcclist);
6bdca890 438 my $date = format_2822_time($time++);
e923effb
ML
439 my $gitversion = '@@GIT_VERSION@@';
440 if ($gitversion =~ m/..GIT_VERSION../) {
3cb8caf7 441 $gitversion = Git::version();
e923effb 442 }
4bc87a28 443
7a2a0d21 444 my ($author_name) = ($from =~ /^(.*?)\s+</);
7768e27e 445 if ($author_name && $author_name =~ /\./ && $author_name !~ /^".*"$/) {
7a2a0d21
JH
446 my ($name, $addr) = ($from =~ /^(.*?)(\s+<.*)/);
447 $from = "\"$name\"$addr";
448 }
4bc87a28
EW
449 my $header = "From: $from
450To: $to
451Cc: $cc
452Subject: $subject
4bc87a28
EW
453Date: $date
454Message-Id: $message_id
e923effb 455X-Mailer: git-send-email $gitversion
4bc87a28 456";
7ccf7927
RA
457 if ($reply_to) {
458
459 $header .= "In-Reply-To: $reply_to\n";
460 $header .= "References: $references\n";
461 }
ce91c2f6
JH
462 if (@xh) {
463 $header .= join("\n", @xh) . "\n";
464 }
4bc87a28 465
6130259c
MW
466 if ($dry_run) {
467 # We don't want to send the email.
468 } elsif ($smtp_server =~ m#^/#) {
aca7ad76
EW
469 my $pid = open my $sm, '|-';
470 defined $pid or die $!;
471 if (!$pid) {
2186d566 472 exec($smtp_server,'-i',
ad9c18f5 473 map { extract_valid_address($_) }
2186d566 474 @recipients) or die $!;
aca7ad76
EW
475 }
476 print $sm "$header\n$message";
477 close $sm or die $?;
478 } else {
87840620 479 require Net::SMTP;
aca7ad76
EW
480 $smtp ||= Net::SMTP->new( $smtp_server );
481 $smtp->mail( $from ) or die $smtp->message;
482 $smtp->to( @recipients ) or die $smtp->message;
483 $smtp->data or die $smtp->message;
484 $smtp->datasend("$header\n$message") or die $smtp->message;
485 $smtp->dataend() or die $smtp->message;
486 $smtp->ok or die "Failed to send $subject\n".$smtp->message;
487 }
2718435b
RA
488 if ($quiet) {
489 printf "Sent %s\n", $subject;
490 } else {
aca7ad76
EW
491 print "OK. Log says:\nDate: $date\n";
492 if ($smtp) {
493 print "Server: $smtp_server\n";
494 } else {
495 print "Sendmail: $smtp_server\n";
496 }
497 print "From: $from\nSubject: $subject\nCc: $cc\nTo: $to\n\n";
498 if ($smtp) {
499 print "Result: ", $smtp->code, ' ',
500 ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
501 } else {
502 print "Result: OK\n";
503 }
30d08b34 504 }
83b24437
RA
505}
506
83b24437 507$reply_to = $initial_reply_to;
2186d566 508$references = $initial_reply_to || '';
83b24437
RA
509make_message_id();
510$subject = $initial_subject;
511
512foreach my $t (@files) {
83b24437
RA
513 open(F,"<",$t) or die "can't open file $t";
514
8a8e6235 515 my $author_not_sender = undef;
da140f8b 516 @cc = @initial_cc;
ce91c2f6 517 @xh = ();
e6b0964a 518 my $input_format = undef;
83b24437
RA
519 my $header_done = 0;
520 $message = "";
521 while(<F>) {
522 if (!$header_done) {
e6b0964a
JH
523 if (/^From /) {
524 $input_format = 'mbox';
525 next;
526 }
83b24437 527 chomp;
e6b0964a
JH
528 if (!defined $input_format && /^[-A-Za-z]+:\s/) {
529 $input_format = 'mbox';
530 }
83b24437 531
e6b0964a 532 if (defined $input_format && $input_format eq 'mbox') {
83b24437
RA
533 if (/^Subject:\s+(.*)$/) {
534 $subject = $1;
535
536 } elsif (/^(Cc|From):\s+(.*)$/) {
8a8e6235
JH
537 if ($2 eq $from) {
538 next if ($suppress_from);
539 }
68d42c41 540 elsif ($1 eq 'From') {
8a8e6235
JH
541 $author_not_sender = $2;
542 }
83b24437 543 printf("(mbox) Adding cc: %s from line '%s'\n",
2718435b 544 $2, $_) unless $quiet;
83b24437
RA
545 push @cc, $2;
546 }
1d6a003a 547 elsif (!/^Date:\s/ && /^[-A-Za-z]+:\s+\S/) {
ce91c2f6
JH
548 push @xh, $_;
549 }
83b24437
RA
550
551 } else {
552 # In the traditional
553 # "send lots of email" format,
554 # line 1 = cc
555 # line 2 = subject
556 # So let's support that, too.
e6b0964a 557 $input_format = 'lots';
83b24437
RA
558 if (@cc == 0) {
559 printf("(non-mbox) Adding cc: %s from line '%s'\n",
2718435b 560 $_, $_) unless $quiet;
83b24437
RA
561
562 push @cc, $_;
563
564 } elsif (!defined $subject) {
565 $subject = $_;
566 }
567 }
5825e5b2 568
83b24437
RA
569 # A whitespace line will terminate the headers
570 if (m/^\s*$/) {
571 $header_done = 1;
572 }
573 } else {
574 $message .= $_;
abec100c
BF
575 if (/^(Signed-off-by|Cc): (.*)$/i && !$no_signed_off_cc) {
576 my $c = $2;
83b24437
RA
577 chomp $c;
578 push @cc, $c;
579 printf("(sob) Adding cc: %s from line '%s'\n",
2718435b 580 $c, $_) unless $quiet;
83b24437
RA
581 }
582 }
583 }
584 close F;
8a8e6235 585 if (defined $author_not_sender) {
374c5905 586 $author_not_sender = unquote_rfc2047($author_not_sender);
8a8e6235
JH
587 $message = "From: $author_not_sender\n\n$message";
588 }
83b24437 589
e205735d 590 $cc = join(", ", unique_email_list(@cc));
83b24437
RA
591
592 send_message();
593
594 # set up for the next message
bc108f63 595 if ($chain_reply_to || !defined $reply_to || length($reply_to) == 0) {
78488b2c 596 $reply_to = $message_id;
7ccf7927
RA
597 if (length $references > 0) {
598 $references .= " $message_id";
599 } else {
600 $references = "$message_id";
601 }
78488b2c 602 }
83b24437 603 make_message_id();
83b24437 604}
e205735d 605
1f038a0c
RA
606if ($compose) {
607 cleanup_compose_files();
608}
609
610sub cleanup_compose_files() {
611 unlink($compose_filename, $compose_filename . ".final");
612
613}
614
4bc87a28 615$smtp->quit if $smtp;
e205735d
RA
616
617sub unique_email_list(@) {
618 my %seen;
619 my @emails;
620
621 foreach my $entry (@_) {
db3106b2
EW
622 if (my $clean = extract_valid_address($entry)) {
623 $seen{$clean} ||= 0;
624 next if $seen{$clean}++;
625 push @emails, $entry;
626 } else {
627 print STDERR "W: unable to extract a valid address",
628 " from: $entry\n";
629 }
e205735d
RA
630 }
631 return @emails;
632}