blob: 3f1b3ca78897ef1eb85dea266eea6afcc404a1db [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 Anderson1f038a02005-09-05 01:13:07 -040034my (@to,@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 Anderson30d08b32006-02-02 11:56:06 -050037my ($chain_reply_to, $smtp_server, $quiet) = (1, "localhost", 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 Anderson78488b22005-07-31 20:04:24 -040051 "chain-reply-to!" => \$chain_reply_to,
Ryan Anderson3342d852005-07-31 20:04:24 -040052 "smtp-server=s" => \$smtp_server,
Ryan Anderson1f038a02005-09-05 01:13:07 -040053 "compose" => \$compose,
Ryan Anderson30d08b32006-02-02 11:56:06 -050054 "quiet" => \$quiet,
Ryan Anderson83b24432005-07-31 04:17:25 -040055 );
56
57# Now, let's fill any that aren't set in with defaults:
58
59open(GITVAR,"-|","git-var","-l")
60 or die "Failed to open pipe from git-var: $!";
61
62my ($author,$committer);
63while(<GITVAR>) {
64 chomp;
65 my ($var,$data) = split /=/,$_,2;
66 my @fields = split /\s+/, $data;
67
68 my $ident = join(" ", @fields[0...(@fields-3)]);
69
70 if ($var eq 'GIT_AUTHOR_IDENT') {
71 $author = $ident;
72 } elsif ($var eq 'GIT_COMMITTER_IDENT') {
73 $committer = $ident;
74 }
75}
76close(GITVAR);
77
Ryan Anderson1f038a02005-09-05 01:13:07 -040078my $prompting = 0;
Ryan Anderson83b24432005-07-31 04:17:25 -040079if (!defined $from) {
80 $from = $author || $committer;
Ryan Anderson8037d1a2005-07-31 20:04:24 -040081 do {
82 $_ = $term->readline("Who should the emails appear to be from? ",
83 $from);
Ryan Andersonca9a7d62005-07-31 20:04:25 -040084 } while (!defined $_);
Ryan Anderson8037d1a2005-07-31 20:04:24 -040085
Ryan Anderson83b24432005-07-31 04:17:25 -040086 $from = $_;
87 print "Emails will be sent from: ", $from, "\n";
Ryan Anderson1f038a02005-09-05 01:13:07 -040088 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -040089}
90
91if (!@to) {
Ryan Anderson8037d1a2005-07-31 20:04:24 -040092 do {
Junio C Hamano5825e5b2005-07-31 23:05:16 -070093 $_ = $term->readline("Who should the emails be sent to? ",
Ryan Anderson8037d1a2005-07-31 20:04:24 -040094 "");
95 } while (!defined $_);
Ryan Anderson83b24432005-07-31 04:17:25 -040096 my $to = $_;
97 push @to, split /,/, $to;
Ryan Anderson1f038a02005-09-05 01:13:07 -040098 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -040099}
100
Ryan Anderson1f038a02005-09-05 01:13:07 -0400101if (!defined $initial_subject && $compose) {
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400102 do {
Junio C Hamano5825e5b2005-07-31 23:05:16 -0700103 $_ = $term->readline("What subject should the emails start with? ",
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400104 $initial_subject);
105 } while (!defined $_);
Ryan Anderson83b24432005-07-31 04:17:25 -0400106 $initial_subject = $_;
Ryan Anderson1f038a02005-09-05 01:13:07 -0400107 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -0400108}
109
Ryan Anderson1f038a02005-09-05 01:13:07 -0400110if (!defined $initial_reply_to && $prompting) {
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400111 do {
Ryan Anderson1f038a02005-09-05 01:13:07 -0400112 $_= $term->readline("Message-ID to be used as In-Reply-To for the first email? ",
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400113 $initial_reply_to);
114 } while (!defined $_);
115
Ryan Anderson83b24432005-07-31 04:17:25 -0400116 $initial_reply_to = $_;
Ryan Anderson78488b22005-07-31 20:04:24 -0400117 $initial_reply_to =~ s/(^\s+|\s+$)//g;
Ryan Anderson83b24432005-07-31 04:17:25 -0400118}
119
Ryan Anderson3342d852005-07-31 20:04:24 -0400120if (!defined $smtp_server) {
121 $smtp_server = "localhost";
122}
123
Ryan Anderson1f038a02005-09-05 01:13:07 -0400124if ($compose) {
125 # Note that this does not need to be secure, but we will make a small
126 # effort to have it be unique
127 open(C,">",$compose_filename)
128 or die "Failed to open for writing $compose_filename: $!";
Ryan Andersond366c702006-02-02 11:56:06 -0500129 print C "From $from # This line is ignored.\n";
Ryan Anderson1f038a02005-09-05 01:13:07 -0400130 printf C "Subject: %s\n\n", $initial_subject;
131 printf C <<EOT;
132GIT: Please enter your email below.
133GIT: Lines beginning in "GIT: " will be removed.
134GIT: Consider including an overall diffstat or table of contents
135GIT: for the patch you are writing.
136
137EOT
138 close(C);
139
140 my $editor = $ENV{EDITOR};
141 $editor = 'vi' unless defined $editor;
142 system($editor, $compose_filename);
143
144 open(C2,">",$compose_filename . ".final")
145 or die "Failed to open $compose_filename.final : " . $!;
146
147 open(C,"<",$compose_filename)
148 or die "Failed to open $compose_filename : " . $!;
149
150 while(<C>) {
151 next if m/^GIT: /;
152 print C2 $_;
153 }
154 close(C);
155 close(C2);
156
157 do {
158 $_ = $term->readline("Send this email? (y|n) ");
159 } while (!defined $_);
160
161 if (uc substr($_,0,1) ne 'Y') {
162 cleanup_compose_files();
163 exit(0);
164 }
165
166 @files = ($compose_filename . ".final");
167}
168
169
Ryan Anderson83b24432005-07-31 04:17:25 -0400170# Now that all the defaults are set, process the rest of the command line
171# arguments and collect up the files that need to be processed.
172for my $f (@ARGV) {
173 if (-d $f) {
174 opendir(DH,$f)
175 or die "Failed to opendir $f: $!";
176
Junio C Hamano5825e5b2005-07-31 23:05:16 -0700177 push @files, grep { -f $_ } map { +$f . "/" . $_ }
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400178 sort readdir(DH);
179
Ryan Anderson83b24432005-07-31 04:17:25 -0400180 } elsif (-f $f) {
181 push @files, $f;
182
183 } else {
184 print STDERR "Skipping $f - not found.\n";
185 }
186}
187
188if (@files) {
Ryan Anderson27184352006-02-05 20:13:52 -0500189 unless ($quiet) {
190 print $_,"\n" for (@files);
191 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400192} else {
193 print <<EOT;
Junio C Hamano215a7ad2005-09-07 17:26:23 -0700194git-send-email [options] <file | directory> [... file | directory ]
Ryan Anderson83b24432005-07-31 04:17:25 -0400195Options:
196 --from Specify the "From:" line of the email to be sent.
Ryan Anderson1f038a02005-09-05 01:13:07 -0400197
Junio C Hamano5825e5b2005-07-31 23:05:16 -0700198 --to Specify the primary "To:" line of the email.
Ryan Anderson1f038a02005-09-05 01:13:07 -0400199
200 --compose Use \$EDITOR to edit an introductory message for the
201 patch series.
202
Ryan Anderson83b24432005-07-31 04:17:25 -0400203 --subject Specify the initial "Subject:" line.
Ryan Anderson1f038a02005-09-05 01:13:07 -0400204 Only necessary if --compose is also set. If --compose
205 is not set, this will be prompted for.
206
Ryan Anderson83b24432005-07-31 04:17:25 -0400207 --in-reply-to Specify the first "In-Reply-To:" header line.
Ryan Anderson1f038a02005-09-05 01:13:07 -0400208 Only used if --compose is also set. If --compose is not
209 set, this will be prompted for.
210
Ryan Andersone2057352005-08-02 21:45:22 -0400211 --chain-reply-to If set, the replies will all be to the previous
Junio C Hamano5825e5b2005-07-31 23:05:16 -0700212 email sent, rather than to the first email sent.
213 Defaults to on.
Ryan Anderson1f038a02005-09-05 01:13:07 -0400214
Ryan Anderson3342d852005-07-31 20:04:24 -0400215 --smtp-server If set, specifies the outgoing SMTP server to use.
216 Defaults to localhost.
Ryan Anderson83b24432005-07-31 04:17:25 -0400217
Ryan Anderson27184352006-02-05 20:13:52 -0500218 --quiet Make git-send-email less verbose. One line per email should be
219 all that is output.
220
221
Ryan Anderson83b24432005-07-31 04:17:25 -0400222Error: Please specify a file or a directory on the command line.
223EOT
224 exit(1);
225}
226
227# Variables we set as part of the loop over files
228our ($message_id, $cc, %mail, $subject, $reply_to, $message);
229
230
231# Usually don't need to change anything below here.
232
233# we make a "fake" message id by taking the current number
234# of seconds since the beginning of Unix time and tacking on
235# a random number to the end, in case we are called quicker than
236# 1 second since the last time we were called.
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400237
238# We'll setup a template for the message id, using the "from" address:
239my $message_id_from = Email::Valid->address($from);
Ryan Andersone2057352005-08-02 21:45:22 -0400240my $message_id_template = "<%s-git-send-email-$message_id_from>";
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400241
Ryan Anderson83b24432005-07-31 04:17:25 -0400242sub make_message_id
243{
244 my $date = `date "+\%s"`;
245 chomp($date);
246 my $pseudo_rand = int (rand(4200));
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400247 $message_id = sprintf $message_id_template, "$date$pseudo_rand";
248 #print "new message id = $message_id\n"; # Was useful for debugging
Ryan Anderson83b24432005-07-31 04:17:25 -0400249}
250
251
252
253$cc = "";
254
255sub send_message
256{
Ryan Andersone2057352005-08-02 21:45:22 -0400257 my $to = join (", ", unique_email_list(@to));
Ryan Anderson83b24432005-07-31 04:17:25 -0400258
259 %mail = ( To => $to,
260 From => $from,
261 CC => $cc,
262 Subject => $subject,
263 Message => $message,
264 'Reply-to' => $from,
265 'In-Reply-To' => $reply_to,
266 'Message-ID' => $message_id,
Junio C Hamano215a7ad2005-09-07 17:26:23 -0700267 'X-Mailer' => "git-send-email",
Ryan Anderson83b24432005-07-31 04:17:25 -0400268 );
269
Ryan Anderson3342d852005-07-31 20:04:24 -0400270 $mail{smtp} = $smtp_server;
Ryan Anderson91332612005-07-31 20:04:24 -0400271 $mailcfg{mime} = 0;
Ryan Anderson83b24432005-07-31 04:17:25 -0400272
273 #print Data::Dumper->Dump([\%mail],[qw(*mail)]);
274
275 sendmail(%mail) or die $Mail::Sendmail::error;
276
Ryan Anderson27184352006-02-05 20:13:52 -0500277 if ($quiet) {
278 printf "Sent %s\n", $subject;
279 } else {
Ryan Anderson30d08b32006-02-02 11:56:06 -0500280 print "OK. Log says:\n", $Mail::Sendmail::log;
281 print "\n\n"
282 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400283}
284
285
286$reply_to = $initial_reply_to;
287make_message_id();
288$subject = $initial_subject;
289
290foreach my $t (@files) {
Ryan Anderson83b24432005-07-31 04:17:25 -0400291 open(F,"<",$t) or die "can't open file $t";
292
293 @cc = ();
294 my $found_mbox = 0;
295 my $header_done = 0;
296 $message = "";
297 while(<F>) {
298 if (!$header_done) {
299 $found_mbox = 1, next if (/^From /);
300 chomp;
301
302 if ($found_mbox) {
303 if (/^Subject:\s+(.*)$/) {
304 $subject = $1;
305
306 } elsif (/^(Cc|From):\s+(.*)$/) {
307 printf("(mbox) Adding cc: %s from line '%s'\n",
Ryan Anderson27184352006-02-05 20:13:52 -0500308 $2, $_) unless $quiet;
Ryan Anderson83b24432005-07-31 04:17:25 -0400309 push @cc, $2;
310 }
311
312 } else {
313 # In the traditional
314 # "send lots of email" format,
315 # line 1 = cc
316 # line 2 = subject
317 # So let's support that, too.
318 if (@cc == 0) {
319 printf("(non-mbox) Adding cc: %s from line '%s'\n",
Ryan Anderson27184352006-02-05 20:13:52 -0500320 $_, $_) unless $quiet;
Ryan Anderson83b24432005-07-31 04:17:25 -0400321
322 push @cc, $_;
323
324 } elsif (!defined $subject) {
325 $subject = $_;
326 }
327 }
Junio C Hamano5825e5b2005-07-31 23:05:16 -0700328
Ryan Anderson83b24432005-07-31 04:17:25 -0400329 # A whitespace line will terminate the headers
330 if (m/^\s*$/) {
331 $header_done = 1;
332 }
333 } else {
334 $message .= $_;
335 if (/^Signed-off-by: (.*)$/i) {
336 my $c = $1;
337 chomp $c;
338 push @cc, $c;
339 printf("(sob) Adding cc: %s from line '%s'\n",
Ryan Anderson27184352006-02-05 20:13:52 -0500340 $c, $_) unless $quiet;
Ryan Anderson83b24432005-07-31 04:17:25 -0400341 }
342 }
343 }
344 close F;
345
Ryan Andersone2057352005-08-02 21:45:22 -0400346 $cc = join(", ", unique_email_list(@cc));
Ryan Anderson83b24432005-07-31 04:17:25 -0400347
348 send_message();
349
350 # set up for the next message
Ryan Anderson78488b22005-07-31 20:04:24 -0400351 if ($chain_reply_to || length($reply_to) == 0) {
352 $reply_to = $message_id;
353 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400354 make_message_id();
Ryan Anderson83b24432005-07-31 04:17:25 -0400355}
Ryan Andersone2057352005-08-02 21:45:22 -0400356
Ryan Anderson1f038a02005-09-05 01:13:07 -0400357if ($compose) {
358 cleanup_compose_files();
359}
360
361sub cleanup_compose_files() {
362 unlink($compose_filename, $compose_filename . ".final");
363
364}
365
366
Ryan Andersone2057352005-08-02 21:45:22 -0400367
368sub unique_email_list(@) {
369 my %seen;
370 my @emails;
371
372 foreach my $entry (@_) {
373 my $clean = Email::Valid->address($entry);
374 next if $seen{$clean}++;
375 push @emails, $entry;
376 }
377 return @emails;
378}