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