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