blob: 13b85dddd186b5b769fb8e9b88896b1f89ef7935 [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 Anderson91332612005-07-31 20:04:24 -040022use Mail::Sendmail qw(sendmail %mailcfg);
Ryan Anderson83b24432005-07-31 04:17:25 -040023use Getopt::Long;
24use Data::Dumper;
25use Email::Valid;
26
Ryan Andersone2057352005-08-02 21:45:22 -040027sub unique_email_list(@);
Ryan Anderson1f038a02005-09-05 01:13:07 -040028sub cleanup_compose_files();
29
30# Constants (essentially)
31my $compose_filename = ".msg.$$";
Ryan Andersone2057352005-08-02 21:45:22 -040032
Ryan Anderson83b24432005-07-31 04:17:25 -040033# Variables we fill in automatically, or via prompting:
Ryan Andersonda140f82006-02-13 03:05:15 -050034my (@to,@cc,@initial_cc,$initial_reply_to,$initial_subject,@files,$from,$compose);
Ryan Anderson83b24432005-07-31 04:17:25 -040035
Ryan Anderson78488b22005-07-31 20:04:24 -040036# Behavior modification variables
Ryan Andersona985d592006-02-13 02:57:09 -050037my ($chain_reply_to, $smtp_server, $quiet, $suppress_from, $no_signed_off_cc) = (1, "localhost", 0, 0, 0);
Ryan Anderson78488b22005-07-31 20:04:24 -040038
Ryan Anderson91332612005-07-31 20:04:24 -040039# Example reply to:
Ryan Anderson83b24432005-07-31 04:17:25 -040040#$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
Ryan Anderson83b24432005-07-31 04:17:25 -040041
42my $term = new Term::ReadLine 'git-send-email';
43
44# Begin by accumulating all the variables (defined above), that we will end up
45# needing, first, from the command line:
46
47my $rc = GetOptions("from=s" => \$from,
48 "in-reply-to=s" => \$initial_reply_to,
49 "subject=s" => \$initial_subject,
50 "to=s" => \@to,
Ryan Andersonda140f82006-02-13 03:05:15 -050051 "cc=s" => \@initial_cc,
Ryan Anderson78488b22005-07-31 20:04:24 -040052 "chain-reply-to!" => \$chain_reply_to,
Ryan Anderson3342d852005-07-31 20:04:24 -040053 "smtp-server=s" => \$smtp_server,
Ryan Anderson1f038a02005-09-05 01:13:07 -040054 "compose" => \$compose,
Ryan Anderson30d08b32006-02-02 11:56:06 -050055 "quiet" => \$quiet,
Ryan Andersona985d592006-02-13 02:57:09 -050056 "suppress-from" => \$suppress_from,
57 "no-signed-off-cc" => \$no_signed_off_cc,
Ryan Anderson83b24432005-07-31 04:17:25 -040058 );
59
60# Now, let's fill any that aren't set in with defaults:
61
62open(GITVAR,"-|","git-var","-l")
63 or die "Failed to open pipe from git-var: $!";
64
65my ($author,$committer);
66while(<GITVAR>) {
67 chomp;
68 my ($var,$data) = split /=/,$_,2;
69 my @fields = split /\s+/, $data;
70
71 my $ident = join(" ", @fields[0...(@fields-3)]);
72
73 if ($var eq 'GIT_AUTHOR_IDENT') {
74 $author = $ident;
75 } elsif ($var eq 'GIT_COMMITTER_IDENT') {
76 $committer = $ident;
77 }
78}
79close(GITVAR);
80
Ryan Anderson1f038a02005-09-05 01:13:07 -040081my $prompting = 0;
Ryan Anderson83b24432005-07-31 04:17:25 -040082if (!defined $from) {
83 $from = $author || $committer;
Ryan Anderson8037d1a2005-07-31 20:04:24 -040084 do {
85 $_ = $term->readline("Who should the emails appear to be from? ",
86 $from);
Ryan Andersonca9a7d62005-07-31 20:04:25 -040087 } while (!defined $_);
Ryan Anderson8037d1a2005-07-31 20:04:24 -040088
Ryan Anderson83b24432005-07-31 04:17:25 -040089 $from = $_;
90 print "Emails will be sent from: ", $from, "\n";
Ryan Anderson1f038a02005-09-05 01:13:07 -040091 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -040092}
93
94if (!@to) {
Ryan Anderson8037d1a2005-07-31 20:04:24 -040095 do {
Junio C Hamano5825e5b2005-07-31 23:05:16 -070096 $_ = $term->readline("Who should the emails be sent to? ",
Ryan Anderson8037d1a2005-07-31 20:04:24 -040097 "");
98 } while (!defined $_);
Ryan Anderson83b24432005-07-31 04:17:25 -040099 my $to = $_;
100 push @to, split /,/, $to;
Ryan Anderson1f038a02005-09-05 01:13:07 -0400101 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -0400102}
103
Ryan Anderson1f038a02005-09-05 01:13:07 -0400104if (!defined $initial_subject && $compose) {
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400105 do {
Junio C Hamano5825e5b2005-07-31 23:05:16 -0700106 $_ = $term->readline("What subject should the emails start with? ",
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400107 $initial_subject);
108 } while (!defined $_);
Ryan Anderson83b24432005-07-31 04:17:25 -0400109 $initial_subject = $_;
Ryan Anderson1f038a02005-09-05 01:13:07 -0400110 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -0400111}
112
Ryan Anderson1f038a02005-09-05 01:13:07 -0400113if (!defined $initial_reply_to && $prompting) {
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400114 do {
Ryan Anderson1f038a02005-09-05 01:13:07 -0400115 $_= $term->readline("Message-ID to be used as In-Reply-To for the first email? ",
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400116 $initial_reply_to);
117 } while (!defined $_);
118
Ryan Anderson83b24432005-07-31 04:17:25 -0400119 $initial_reply_to = $_;
Ryan Anderson78488b22005-07-31 20:04:24 -0400120 $initial_reply_to =~ s/(^\s+|\s+$)//g;
Ryan Anderson83b24432005-07-31 04:17:25 -0400121}
122
Ryan Anderson3342d852005-07-31 20:04:24 -0400123if (!defined $smtp_server) {
124 $smtp_server = "localhost";
125}
126
Ryan Anderson1f038a02005-09-05 01:13:07 -0400127if ($compose) {
128 # Note that this does not need to be secure, but we will make a small
129 # effort to have it be unique
130 open(C,">",$compose_filename)
131 or die "Failed to open for writing $compose_filename: $!";
Ryan Andersond366c702006-02-02 11:56:06 -0500132 print C "From $from # This line is ignored.\n";
Ryan Anderson1f038a02005-09-05 01:13:07 -0400133 printf C "Subject: %s\n\n", $initial_subject;
134 printf C <<EOT;
135GIT: Please enter your email below.
136GIT: Lines beginning in "GIT: " will be removed.
137GIT: Consider including an overall diffstat or table of contents
138GIT: for the patch you are writing.
139
140EOT
141 close(C);
142
143 my $editor = $ENV{EDITOR};
144 $editor = 'vi' unless defined $editor;
145 system($editor, $compose_filename);
146
147 open(C2,">",$compose_filename . ".final")
148 or die "Failed to open $compose_filename.final : " . $!;
149
150 open(C,"<",$compose_filename)
151 or die "Failed to open $compose_filename : " . $!;
152
153 while(<C>) {
154 next if m/^GIT: /;
155 print C2 $_;
156 }
157 close(C);
158 close(C2);
159
160 do {
161 $_ = $term->readline("Send this email? (y|n) ");
162 } while (!defined $_);
163
164 if (uc substr($_,0,1) ne 'Y') {
165 cleanup_compose_files();
166 exit(0);
167 }
168
169 @files = ($compose_filename . ".final");
170}
171
172
Ryan Anderson83b24432005-07-31 04:17:25 -0400173# Now that all the defaults are set, process the rest of the command line
174# arguments and collect up the files that need to be processed.
175for my $f (@ARGV) {
176 if (-d $f) {
177 opendir(DH,$f)
178 or die "Failed to opendir $f: $!";
179
Junio C Hamano5825e5b2005-07-31 23:05:16 -0700180 push @files, grep { -f $_ } map { +$f . "/" . $_ }
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400181 sort readdir(DH);
182
Ryan Anderson83b24432005-07-31 04:17:25 -0400183 } elsif (-f $f) {
184 push @files, $f;
185
186 } else {
187 print STDERR "Skipping $f - not found.\n";
188 }
189}
190
191if (@files) {
Ryan Anderson27184352006-02-05 20:13:52 -0500192 unless ($quiet) {
193 print $_,"\n" for (@files);
194 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400195} else {
196 print <<EOT;
Junio C Hamano215a7ad2005-09-07 17:26:23 -0700197git-send-email [options] <file | directory> [... file | directory ]
Ryan Anderson83b24432005-07-31 04:17:25 -0400198Options:
199 --from Specify the "From:" line of the email to be sent.
Ryan Anderson1f038a02005-09-05 01:13:07 -0400200
Junio C Hamano5825e5b2005-07-31 23:05:16 -0700201 --to Specify the primary "To:" line of the email.
Ryan Anderson1f038a02005-09-05 01:13:07 -0400202
Ryan Andersonda140f82006-02-13 03:05:15 -0500203 --cc Specify an initial "Cc:" list for the entire series
204 of emails.
205
Ryan Anderson1f038a02005-09-05 01:13:07 -0400206 --compose Use \$EDITOR to edit an introductory message for the
207 patch series.
208
Ryan Anderson83b24432005-07-31 04:17:25 -0400209 --subject Specify the initial "Subject:" line.
Ryan Anderson1f038a02005-09-05 01:13:07 -0400210 Only necessary if --compose is also set. If --compose
211 is not set, this will be prompted for.
212
Ryan Anderson83b24432005-07-31 04:17:25 -0400213 --in-reply-to Specify the first "In-Reply-To:" header line.
Ryan Anderson1f038a02005-09-05 01:13:07 -0400214 Only used if --compose is also set. If --compose is not
215 set, this will be prompted for.
216
Ryan Andersone2057352005-08-02 21:45:22 -0400217 --chain-reply-to If set, the replies will all be to the previous
Junio C Hamano5825e5b2005-07-31 23:05:16 -0700218 email sent, rather than to the first email sent.
219 Defaults to on.
Ryan Anderson1f038a02005-09-05 01:13:07 -0400220
Ryan Andersona985d592006-02-13 02:57:09 -0500221 --no-signed-off-cc Suppress the automatic addition of email addresses
222 that appear in a Signed-off-by: line, to the cc: list.
223 Note: Using this option is not recommended.
224
Ryan Anderson3342d852005-07-31 20:04:24 -0400225 --smtp-server If set, specifies the outgoing SMTP server to use.
226 Defaults to localhost.
Ryan Anderson83b24432005-07-31 04:17:25 -0400227
Ryan Andersona985d592006-02-13 02:57:09 -0500228 --suppress-from Supress sending emails to yourself if your address
229 appears in a From: line.
230
Ryan Anderson27184352006-02-05 20:13:52 -0500231 --quiet Make git-send-email less verbose. One line per email should be
232 all that is output.
233
Ryan Anderson83b24432005-07-31 04:17:25 -0400234Error: Please specify a file or a directory on the command line.
235EOT
236 exit(1);
237}
238
239# Variables we set as part of the loop over files
240our ($message_id, $cc, %mail, $subject, $reply_to, $message);
241
242
243# Usually don't need to change anything below here.
244
245# we make a "fake" message id by taking the current number
246# of seconds since the beginning of Unix time and tacking on
247# a random number to the end, in case we are called quicker than
248# 1 second since the last time we were called.
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400249
250# We'll setup a template for the message id, using the "from" address:
251my $message_id_from = Email::Valid->address($from);
Ryan Andersone2057352005-08-02 21:45:22 -0400252my $message_id_template = "<%s-git-send-email-$message_id_from>";
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400253
Ryan Anderson83b24432005-07-31 04:17:25 -0400254sub make_message_id
255{
256 my $date = `date "+\%s"`;
257 chomp($date);
258 my $pseudo_rand = int (rand(4200));
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400259 $message_id = sprintf $message_id_template, "$date$pseudo_rand";
260 #print "new message id = $message_id\n"; # Was useful for debugging
Ryan Anderson83b24432005-07-31 04:17:25 -0400261}
262
263
264
265$cc = "";
266
267sub send_message
268{
Ryan Andersone2057352005-08-02 21:45:22 -0400269 my $to = join (", ", unique_email_list(@to));
Ryan Anderson83b24432005-07-31 04:17:25 -0400270
271 %mail = ( To => $to,
272 From => $from,
273 CC => $cc,
274 Subject => $subject,
275 Message => $message,
276 'Reply-to' => $from,
277 'In-Reply-To' => $reply_to,
278 'Message-ID' => $message_id,
Junio C Hamano215a7ad2005-09-07 17:26:23 -0700279 'X-Mailer' => "git-send-email",
Ryan Anderson83b24432005-07-31 04:17:25 -0400280 );
281
Ryan Anderson3342d852005-07-31 20:04:24 -0400282 $mail{smtp} = $smtp_server;
Ryan Anderson91332612005-07-31 20:04:24 -0400283 $mailcfg{mime} = 0;
Ryan Anderson83b24432005-07-31 04:17:25 -0400284
285 #print Data::Dumper->Dump([\%mail],[qw(*mail)]);
286
287 sendmail(%mail) or die $Mail::Sendmail::error;
288
Ryan Anderson27184352006-02-05 20:13:52 -0500289 if ($quiet) {
290 printf "Sent %s\n", $subject;
291 } else {
Ryan Anderson30d08b32006-02-02 11:56:06 -0500292 print "OK. Log says:\n", $Mail::Sendmail::log;
293 print "\n\n"
294 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400295}
296
297
298$reply_to = $initial_reply_to;
299make_message_id();
300$subject = $initial_subject;
301
302foreach my $t (@files) {
Ryan Anderson83b24432005-07-31 04:17:25 -0400303 open(F,"<",$t) or die "can't open file $t";
304
Ryan Andersonda140f82006-02-13 03:05:15 -0500305 @cc = @initial_cc;
Ryan Anderson83b24432005-07-31 04:17:25 -0400306 my $found_mbox = 0;
307 my $header_done = 0;
308 $message = "";
309 while(<F>) {
310 if (!$header_done) {
311 $found_mbox = 1, next if (/^From /);
312 chomp;
313
314 if ($found_mbox) {
315 if (/^Subject:\s+(.*)$/) {
316 $subject = $1;
317
318 } elsif (/^(Cc|From):\s+(.*)$/) {
Ryan Andersona985d592006-02-13 02:57:09 -0500319 next if ($2 eq $from && $suppress_from);
Ryan Anderson83b24432005-07-31 04:17:25 -0400320 printf("(mbox) Adding cc: %s from line '%s'\n",
Ryan Anderson27184352006-02-05 20:13:52 -0500321 $2, $_) unless $quiet;
Ryan Anderson83b24432005-07-31 04:17:25 -0400322 push @cc, $2;
323 }
324
325 } else {
326 # In the traditional
327 # "send lots of email" format,
328 # line 1 = cc
329 # line 2 = subject
330 # So let's support that, too.
331 if (@cc == 0) {
332 printf("(non-mbox) Adding cc: %s from line '%s'\n",
Ryan Anderson27184352006-02-05 20:13:52 -0500333 $_, $_) unless $quiet;
Ryan Anderson83b24432005-07-31 04:17:25 -0400334
335 push @cc, $_;
336
337 } elsif (!defined $subject) {
338 $subject = $_;
339 }
340 }
Junio C Hamano5825e5b2005-07-31 23:05:16 -0700341
Ryan Anderson83b24432005-07-31 04:17:25 -0400342 # A whitespace line will terminate the headers
343 if (m/^\s*$/) {
344 $header_done = 1;
345 }
346 } else {
347 $message .= $_;
Ryan Andersona985d592006-02-13 02:57:09 -0500348 if (/^Signed-off-by: (.*)$/i && !$no_signed_off_cc) {
Ryan Anderson83b24432005-07-31 04:17:25 -0400349 my $c = $1;
350 chomp $c;
351 push @cc, $c;
352 printf("(sob) Adding cc: %s from line '%s'\n",
Ryan Anderson27184352006-02-05 20:13:52 -0500353 $c, $_) unless $quiet;
Ryan Anderson83b24432005-07-31 04:17:25 -0400354 }
355 }
356 }
357 close F;
358
Ryan Andersone2057352005-08-02 21:45:22 -0400359 $cc = join(", ", unique_email_list(@cc));
Ryan Anderson83b24432005-07-31 04:17:25 -0400360
361 send_message();
362
363 # set up for the next message
Ryan Anderson78488b22005-07-31 20:04:24 -0400364 if ($chain_reply_to || length($reply_to) == 0) {
365 $reply_to = $message_id;
366 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400367 make_message_id();
Ryan Anderson83b24432005-07-31 04:17:25 -0400368}
Ryan Andersone2057352005-08-02 21:45:22 -0400369
Ryan Anderson1f038a02005-09-05 01:13:07 -0400370if ($compose) {
371 cleanup_compose_files();
372}
373
374sub cleanup_compose_files() {
375 unlink($compose_filename, $compose_filename . ".final");
376
377}
378
379
Ryan Andersone2057352005-08-02 21:45:22 -0400380
381sub unique_email_list(@) {
382 my %seen;
383 my @emails;
384
385 foreach my $entry (@_) {
386 my $clean = Email::Valid->address($entry);
387 next if $seen{$clean}++;
388 push @emails, $entry;
389 }
390 return @emails;
391}