blob: ecfa347b85bb6c3c5e6b1b3a1f83ac1acb069b66 [file] [log] [blame]
Ryan Anderson83b24432005-07-31 04:17:25 -04001#!/usr/bin/perl -w
Ryan Anderson83b24432005-07-31 04:17:25 -04002#
Ryan Andersonf3d9f352005-07-31 20:04:24 -04003# Copyright 2002,2005 Greg Kroah-Hartman <greg@kroah.com>
4# Copyright 2005 Ryan Anderson <ryan@michonline.com>
Ryan Anderson83b24432005-07-31 04:17:25 -04005#
6# GPL v2 (See COPYING)
Junio C Hamano5825e5b2005-07-31 23:05:16 -07007#
Ryan Anderson83b24432005-07-31 04:17:25 -04008# Ported to support git "mbox" format files by Ryan Anderson <ryan@michonline.com>
9#
Ryan Andersonf3d9f352005-07-31 20:04:24 -040010# Sends a collection of emails to the given email addresses, disturbingly fast.
Junio C Hamano5825e5b2005-07-31 23:05:16 -070011#
Ryan Andersonf3d9f352005-07-31 20:04:24 -040012# 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:
Junio C Hamano5825e5b2005-07-31 23:05:16 -070015# first line of the message is who to CC,
Ryan Andersonf3d9f352005-07-31 20:04:24 -040016# and second line is the subject of the message.
Junio C Hamano5825e5b2005-07-31 23:05:16 -070017#
Ryan Anderson83b24432005-07-31 04:17:25 -040018
19use strict;
20use warnings;
21use Term::ReadLine;
Ryan Anderson83b24432005-07-31 04:17:25 -040022use Getopt::Long;
23use Data::Dumper;
Eric Wong4bc87a22006-03-25 17:20:48 -080024use Net::SMTP;
Ryan Anderson83b24432005-07-31 04:17:25 -040025
Eric Wong4bc87a22006-03-25 17:20:48 -080026# most mail servers generate the Date: header, but not all...
27$ENV{LC_ALL} = 'C';
28use POSIX qw/strftime/;
29
Eric Wong567ffeb2006-03-25 16:47:12 -080030my $have_email_valid = eval { require Email::Valid; 1 };
Eric Wong4bc87a22006-03-25 17:20:48 -080031my $smtp;
32
Ryan Andersone2057352005-08-02 21:45:22 -040033sub unique_email_list(@);
Ryan Anderson1f038a02005-09-05 01:13:07 -040034sub cleanup_compose_files();
35
36# Constants (essentially)
37my $compose_filename = ".msg.$$";
Ryan Andersone2057352005-08-02 21:45:22 -040038
Ryan Anderson83b24432005-07-31 04:17:25 -040039# Variables we fill in automatically, or via prompting:
Eric Wonga5370b12006-03-25 03:01:01 -080040my (@to,@cc,@initial_cc,$initial_reply_to,$initial_subject,@files,$from,$compose,$time);
Ryan Anderson83b24432005-07-31 04:17:25 -040041
Ryan Anderson78488b22005-07-31 20:04:24 -040042# Behavior modification variables
Ryan Andersona985d592006-02-13 02:57:09 -050043my ($chain_reply_to, $smtp_server, $quiet, $suppress_from, $no_signed_off_cc) = (1, "localhost", 0, 0, 0);
Ryan Anderson78488b22005-07-31 20:04:24 -040044
Ryan Anderson91332612005-07-31 20:04:24 -040045# Example reply to:
Ryan Anderson83b24432005-07-31 04:17:25 -040046#$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
Ryan Anderson83b24432005-07-31 04:17:25 -040047
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,
Ryan Andersonda140f82006-02-13 03:05:15 -050057 "cc=s" => \@initial_cc,
Ryan Anderson78488b22005-07-31 20:04:24 -040058 "chain-reply-to!" => \$chain_reply_to,
Ryan Anderson3342d852005-07-31 20:04:24 -040059 "smtp-server=s" => \$smtp_server,
Ryan Anderson1f038a02005-09-05 01:13:07 -040060 "compose" => \$compose,
Ryan Anderson30d08b32006-02-02 11:56:06 -050061 "quiet" => \$quiet,
Ryan Andersona985d592006-02-13 02:57:09 -050062 "suppress-from" => \$suppress_from,
Eric Wong8e69b312006-03-03 01:28:48 -080063 "no-signed-off-cc|no-signed-off-by-cc" => \$no_signed_off_cc,
Ryan Anderson83b24432005-07-31 04:17:25 -040064 );
65
66# Now, let's fill any that aren't set in with defaults:
67
Junio C Hamanoe4159072006-02-20 14:23:51 -080068sub 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;
Ryan Anderson83b24432005-07-31 04:17:25 -040080}
Junio C Hamanoe4159072006-02-20 14:23:51 -080081
82sub gitvar_ident {
83 my ($name) = @_;
84 my $val = gitvar($name);
85 my @field = split(/\s+/, $val);
86 return join(' ', @field[0...(@field-3)]);
87}
88
89my ($author) = gitvar_ident('GIT_AUTHOR_IDENT');
90my ($committer) = gitvar_ident('GIT_COMMITTER_IDENT');
Ryan Anderson83b24432005-07-31 04:17:25 -040091
Ryan Anderson1f038a02005-09-05 01:13:07 -040092my $prompting = 0;
Ryan Anderson83b24432005-07-31 04:17:25 -040093if (!defined $from) {
94 $from = $author || $committer;
Ryan Anderson8037d1a2005-07-31 20:04:24 -040095 do {
96 $_ = $term->readline("Who should the emails appear to be from? ",
97 $from);
Ryan Andersonca9a7d62005-07-31 20:04:25 -040098 } while (!defined $_);
Ryan Anderson8037d1a2005-07-31 20:04:24 -040099
Ryan Anderson83b24432005-07-31 04:17:25 -0400100 $from = $_;
101 print "Emails will be sent from: ", $from, "\n";
Ryan Anderson1f038a02005-09-05 01:13:07 -0400102 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -0400103}
104
105if (!@to) {
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400106 do {
Junio C Hamano5825e5b2005-07-31 23:05:16 -0700107 $_ = $term->readline("Who should the emails be sent to? ",
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400108 "");
109 } while (!defined $_);
Ryan Anderson83b24432005-07-31 04:17:25 -0400110 my $to = $_;
111 push @to, split /,/, $to;
Ryan Anderson1f038a02005-09-05 01:13:07 -0400112 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -0400113}
114
Ryan Anderson1f038a02005-09-05 01:13:07 -0400115if (!defined $initial_subject && $compose) {
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400116 do {
Junio C Hamano5825e5b2005-07-31 23:05:16 -0700117 $_ = $term->readline("What subject should the emails start with? ",
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400118 $initial_subject);
119 } while (!defined $_);
Ryan Anderson83b24432005-07-31 04:17:25 -0400120 $initial_subject = $_;
Ryan Anderson1f038a02005-09-05 01:13:07 -0400121 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -0400122}
123
Ryan Anderson1f038a02005-09-05 01:13:07 -0400124if (!defined $initial_reply_to && $prompting) {
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400125 do {
Ryan Anderson1f038a02005-09-05 01:13:07 -0400126 $_= $term->readline("Message-ID to be used as In-Reply-To for the first email? ",
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400127 $initial_reply_to);
128 } while (!defined $_);
129
Ryan Anderson83b24432005-07-31 04:17:25 -0400130 $initial_reply_to = $_;
Ryan Anderson78488b22005-07-31 20:04:24 -0400131 $initial_reply_to =~ s/(^\s+|\s+$)//g;
Ryan Anderson83b24432005-07-31 04:17:25 -0400132}
133
Ryan Anderson3342d852005-07-31 20:04:24 -0400134if (!defined $smtp_server) {
135 $smtp_server = "localhost";
136}
137
Ryan Anderson1f038a02005-09-05 01:13:07 -0400138if ($compose) {
139 # Note that this does not need to be secure, but we will make a small
140 # effort to have it be unique
141 open(C,">",$compose_filename)
142 or die "Failed to open for writing $compose_filename: $!";
Ryan Andersond366c702006-02-02 11:56:06 -0500143 print C "From $from # This line is ignored.\n";
Ryan Anderson1f038a02005-09-05 01:13:07 -0400144 printf C "Subject: %s\n\n", $initial_subject;
145 printf C <<EOT;
146GIT: Please enter your email below.
147GIT: Lines beginning in "GIT: " will be removed.
148GIT: Consider including an overall diffstat or table of contents
149GIT: for the patch you are writing.
150
151EOT
152 close(C);
153
154 my $editor = $ENV{EDITOR};
155 $editor = 'vi' unless defined $editor;
156 system($editor, $compose_filename);
157
158 open(C2,">",$compose_filename . ".final")
159 or die "Failed to open $compose_filename.final : " . $!;
160
161 open(C,"<",$compose_filename)
162 or die "Failed to open $compose_filename : " . $!;
163
164 while(<C>) {
165 next if m/^GIT: /;
166 print C2 $_;
167 }
168 close(C);
169 close(C2);
170
171 do {
172 $_ = $term->readline("Send this email? (y|n) ");
173 } while (!defined $_);
174
175 if (uc substr($_,0,1) ne 'Y') {
176 cleanup_compose_files();
177 exit(0);
178 }
179
180 @files = ($compose_filename . ".final");
181}
182
183
Ryan Anderson83b24432005-07-31 04:17:25 -0400184# Now that all the defaults are set, process the rest of the command line
185# arguments and collect up the files that need to be processed.
186for my $f (@ARGV) {
187 if (-d $f) {
188 opendir(DH,$f)
189 or die "Failed to opendir $f: $!";
190
Junio C Hamano5825e5b2005-07-31 23:05:16 -0700191 push @files, grep { -f $_ } map { +$f . "/" . $_ }
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400192 sort readdir(DH);
193
Ryan Anderson83b24432005-07-31 04:17:25 -0400194 } elsif (-f $f) {
195 push @files, $f;
196
197 } else {
198 print STDERR "Skipping $f - not found.\n";
199 }
200}
201
202if (@files) {
Ryan Anderson27184352006-02-05 20:13:52 -0500203 unless ($quiet) {
204 print $_,"\n" for (@files);
205 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400206} else {
207 print <<EOT;
Junio C Hamano215a7ad2005-09-07 17:26:23 -0700208git-send-email [options] <file | directory> [... file | directory ]
Ryan Anderson83b24432005-07-31 04:17:25 -0400209Options:
210 --from Specify the "From:" line of the email to be sent.
Ryan Anderson1f038a02005-09-05 01:13:07 -0400211
Junio C Hamano5825e5b2005-07-31 23:05:16 -0700212 --to Specify the primary "To:" line of the email.
Ryan Anderson1f038a02005-09-05 01:13:07 -0400213
Ryan Andersonda140f82006-02-13 03:05:15 -0500214 --cc Specify an initial "Cc:" list for the entire series
215 of emails.
216
Ryan Anderson1f038a02005-09-05 01:13:07 -0400217 --compose Use \$EDITOR to edit an introductory message for the
218 patch series.
219
Ryan Anderson83b24432005-07-31 04:17:25 -0400220 --subject Specify the initial "Subject:" line.
Ryan Anderson1f038a02005-09-05 01:13:07 -0400221 Only necessary if --compose is also set. If --compose
222 is not set, this will be prompted for.
223
Ryan Anderson83b24432005-07-31 04:17:25 -0400224 --in-reply-to Specify the first "In-Reply-To:" header line.
Ryan Anderson1f038a02005-09-05 01:13:07 -0400225 Only used if --compose is also set. If --compose is not
226 set, this will be prompted for.
227
Ryan Andersone2057352005-08-02 21:45:22 -0400228 --chain-reply-to If set, the replies will all be to the previous
Junio C Hamano5825e5b2005-07-31 23:05:16 -0700229 email sent, rather than to the first email sent.
230 Defaults to on.
Ryan Anderson1f038a02005-09-05 01:13:07 -0400231
Ryan Andersona985d592006-02-13 02:57:09 -0500232 --no-signed-off-cc Suppress the automatic addition of email addresses
233 that appear in a Signed-off-by: line, to the cc: list.
234 Note: Using this option is not recommended.
235
Ryan Anderson3342d852005-07-31 20:04:24 -0400236 --smtp-server If set, specifies the outgoing SMTP server to use.
237 Defaults to localhost.
Ryan Anderson83b24432005-07-31 04:17:25 -0400238
Ryan Andersona985d592006-02-13 02:57:09 -0500239 --suppress-from Supress sending emails to yourself if your address
240 appears in a From: line.
241
Ryan Anderson27184352006-02-05 20:13:52 -0500242 --quiet Make git-send-email less verbose. One line per email should be
243 all that is output.
244
Ryan Anderson83b24432005-07-31 04:17:25 -0400245Error: Please specify a file or a directory on the command line.
246EOT
247 exit(1);
248}
249
250# Variables we set as part of the loop over files
251our ($message_id, $cc, %mail, $subject, $reply_to, $message);
252
Eric Wong567ffeb2006-03-25 16:47:12 -0800253sub extract_valid_address {
254 my $address = shift;
255 if ($have_email_valid) {
256 return Email::Valid->address($address);
257 } else {
258 # less robust/correct than the monster regexp in Email::Valid,
259 # but still does a 99% job, and one less dependency
260 return ($address =~ /([^\"<>\s]+@[^<>\s]+)/);
261 }
262}
Ryan Anderson83b24432005-07-31 04:17:25 -0400263
264# Usually don't need to change anything below here.
265
266# we make a "fake" message id by taking the current number
267# of seconds since the beginning of Unix time and tacking on
268# a random number to the end, in case we are called quicker than
269# 1 second since the last time we were called.
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400270
271# We'll setup a template for the message id, using the "from" address:
Eric Wong567ffeb2006-03-25 16:47:12 -0800272my $message_id_from = extract_valid_address($from);
Ryan Andersone2057352005-08-02 21:45:22 -0400273my $message_id_template = "<%s-git-send-email-$message_id_from>";
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400274
Ryan Anderson83b24432005-07-31 04:17:25 -0400275sub make_message_id
276{
Eric Wong72095d52006-03-25 02:43:31 -0800277 my $date = time;
Ryan Anderson83b24432005-07-31 04:17:25 -0400278 my $pseudo_rand = int (rand(4200));
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400279 $message_id = sprintf $message_id_template, "$date$pseudo_rand";
280 #print "new message id = $message_id\n"; # Was useful for debugging
Ryan Anderson83b24432005-07-31 04:17:25 -0400281}
282
283
284
285$cc = "";
Eric Wonga5370b12006-03-25 03:01:01 -0800286$time = time - scalar $#files;
Ryan Anderson83b24432005-07-31 04:17:25 -0400287
288sub send_message
289{
Eric Wong4bc87a22006-03-25 17:20:48 -0800290 my @recipients = unique_email_list(@to);
291 my $to = join (",\n\t", @recipients);
292 @recipients = unique_email_list(@recipients,@cc);
Eric Wonga5370b12006-03-25 03:01:01 -0800293 my $date = strftime('%a, %d %b %Y %H:%M:%S %z', localtime($time++));
Ryan Anderson83b24432005-07-31 04:17:25 -0400294
Eric Wong4bc87a22006-03-25 17:20:48 -0800295 my $header = "From: $from
296To: $to
297Cc: $cc
298Subject: $subject
299Reply-To: $from
300Date: $date
301Message-Id: $message_id
302X-Mailer: git-send-email @@GIT_VERSION@@
303";
304 $header .= "In-Reply-To: $reply_to\n" if $reply_to;
Ryan Anderson83b24432005-07-31 04:17:25 -0400305
Eric Wong4bc87a22006-03-25 17:20:48 -0800306 $smtp ||= Net::SMTP->new( $smtp_server );
307 $smtp->mail( $from ) or die $smtp->message;
308 $smtp->to( @recipients ) or die $smtp->message;
309 $smtp->data or die $smtp->message;
310 $smtp->datasend("$header\n$message") or die $smtp->message;
311 $smtp->dataend() or die $smtp->message;
312 $smtp->ok or die "Failed to send $subject\n".$smtp->message;
Ryan Anderson83b24432005-07-31 04:17:25 -0400313
Ryan Anderson27184352006-02-05 20:13:52 -0500314 if ($quiet) {
315 printf "Sent %s\n", $subject;
316 } else {
Eric Wong4bc87a22006-03-25 17:20:48 -0800317 print "OK. Log says:
318Date: $date
319Server: $smtp_server Port: 25
320From: $from
321Subject: $subject
322Cc: $cc
323To: $to
324
325Result: ", $smtp->code, ' ', ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
Ryan Anderson30d08b32006-02-02 11:56:06 -0500326 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400327}
328
Ryan Anderson83b24432005-07-31 04:17:25 -0400329$reply_to = $initial_reply_to;
330make_message_id();
331$subject = $initial_subject;
332
333foreach my $t (@files) {
Ryan Anderson83b24432005-07-31 04:17:25 -0400334 open(F,"<",$t) or die "can't open file $t";
335
Junio C Hamano8a8e6232006-03-23 23:43:52 -0800336 my $author_not_sender = undef;
Ryan Andersonda140f82006-02-13 03:05:15 -0500337 @cc = @initial_cc;
Ryan Anderson83b24432005-07-31 04:17:25 -0400338 my $found_mbox = 0;
339 my $header_done = 0;
340 $message = "";
341 while(<F>) {
342 if (!$header_done) {
343 $found_mbox = 1, next if (/^From /);
344 chomp;
345
346 if ($found_mbox) {
347 if (/^Subject:\s+(.*)$/) {
348 $subject = $1;
349
350 } elsif (/^(Cc|From):\s+(.*)$/) {
Junio C Hamano8a8e6232006-03-23 23:43:52 -0800351 if ($2 eq $from) {
352 next if ($suppress_from);
353 }
354 else {
355 $author_not_sender = $2;
356 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400357 printf("(mbox) Adding cc: %s from line '%s'\n",
Ryan Anderson27184352006-02-05 20:13:52 -0500358 $2, $_) unless $quiet;
Ryan Anderson83b24432005-07-31 04:17:25 -0400359 push @cc, $2;
360 }
361
362 } else {
363 # In the traditional
364 # "send lots of email" format,
365 # line 1 = cc
366 # line 2 = subject
367 # So let's support that, too.
368 if (@cc == 0) {
369 printf("(non-mbox) Adding cc: %s from line '%s'\n",
Ryan Anderson27184352006-02-05 20:13:52 -0500370 $_, $_) unless $quiet;
Ryan Anderson83b24432005-07-31 04:17:25 -0400371
372 push @cc, $_;
373
374 } elsif (!defined $subject) {
375 $subject = $_;
376 }
377 }
Junio C Hamano5825e5b2005-07-31 23:05:16 -0700378
Ryan Anderson83b24432005-07-31 04:17:25 -0400379 # A whitespace line will terminate the headers
380 if (m/^\s*$/) {
381 $header_done = 1;
382 }
383 } else {
384 $message .= $_;
Ryan Andersona985d592006-02-13 02:57:09 -0500385 if (/^Signed-off-by: (.*)$/i && !$no_signed_off_cc) {
Ryan Anderson83b24432005-07-31 04:17:25 -0400386 my $c = $1;
387 chomp $c;
388 push @cc, $c;
389 printf("(sob) Adding cc: %s from line '%s'\n",
Ryan Anderson27184352006-02-05 20:13:52 -0500390 $c, $_) unless $quiet;
Ryan Anderson83b24432005-07-31 04:17:25 -0400391 }
392 }
393 }
394 close F;
Junio C Hamano8a8e6232006-03-23 23:43:52 -0800395 if (defined $author_not_sender) {
396 $message = "From: $author_not_sender\n\n$message";
397 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400398
Ryan Andersone2057352005-08-02 21:45:22 -0400399 $cc = join(", ", unique_email_list(@cc));
Ryan Anderson83b24432005-07-31 04:17:25 -0400400
401 send_message();
402
403 # set up for the next message
Ryan Anderson78488b22005-07-31 20:04:24 -0400404 if ($chain_reply_to || length($reply_to) == 0) {
405 $reply_to = $message_id;
406 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400407 make_message_id();
Ryan Anderson83b24432005-07-31 04:17:25 -0400408}
Ryan Andersone2057352005-08-02 21:45:22 -0400409
Ryan Anderson1f038a02005-09-05 01:13:07 -0400410if ($compose) {
411 cleanup_compose_files();
412}
413
414sub cleanup_compose_files() {
415 unlink($compose_filename, $compose_filename . ".final");
416
417}
418
Eric Wong4bc87a22006-03-25 17:20:48 -0800419$smtp->quit if $smtp;
Ryan Andersone2057352005-08-02 21:45:22 -0400420
421sub unique_email_list(@) {
422 my %seen;
423 my @emails;
424
425 foreach my $entry (@_) {
Eric Wong567ffeb2006-03-25 16:47:12 -0800426 my $clean = extract_valid_address($entry);
Ryan Andersone2057352005-08-02 21:45:22 -0400427 next if $seen{$clean}++;
428 push @emails, $entry;
429 }
430 return @emails;
431}