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