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