]> git.ipfire.org Git - thirdparty/git.git/blame - git-send-email.perl
send-email: use built-in time() instead of /bin/date '+%s'
[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;
9133261f 22use Mail::Sendmail qw(sendmail %mailcfg);
83b24437
RA
23use Getopt::Long;
24use Data::Dumper;
25use Email::Valid;
26
e205735d 27sub unique_email_list(@);
1f038a0c
RA
28sub cleanup_compose_files();
29
30# Constants (essentially)
31my $compose_filename = ".msg.$$";
e205735d 32
83b24437 33# Variables we fill in automatically, or via prompting:
da140f8b 34my (@to,@cc,@initial_cc,$initial_reply_to,$initial_subject,@files,$from,$compose);
83b24437 35
78488b2c 36# Behavior modification variables
a985d595 37my ($chain_reply_to, $smtp_server, $quiet, $suppress_from, $no_signed_off_cc) = (1, "localhost", 0, 0, 0);
78488b2c 38
9133261f 39# Example reply to:
83b24437 40#$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
83b24437
RA
41
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,
da140f8b 51 "cc=s" => \@initial_cc,
78488b2c 52 "chain-reply-to!" => \$chain_reply_to,
3342d850 53 "smtp-server=s" => \$smtp_server,
1f038a0c 54 "compose" => \$compose,
30d08b34 55 "quiet" => \$quiet,
a985d595 56 "suppress-from" => \$suppress_from,
8e69b31e 57 "no-signed-off-cc|no-signed-off-by-cc" => \$no_signed_off_cc,
83b24437
RA
58 );
59
60# Now, let's fill any that aren't set in with defaults:
61
e415907d
JH
62sub gitvar {
63 my ($var) = @_;
64 my $fh;
65 my $pid = open($fh, '-|');
66 die "$!" unless defined $pid;
67 if (!$pid) {
68 exec('git-var', $var) or die "$!";
69 }
70 my ($val) = <$fh>;
71 close $fh or die "$!";
72 chomp($val);
73 return $val;
74}
83b24437 75
e415907d
JH
76sub gitvar_ident {
77 my ($name) = @_;
78 my $val = gitvar($name);
79 my @field = split(/\s+/, $val);
80 return join(' ', @field[0...(@field-3)]);
83b24437 81}
e415907d
JH
82
83my ($author) = gitvar_ident('GIT_AUTHOR_IDENT');
84my ($committer) = gitvar_ident('GIT_COMMITTER_IDENT');
83b24437 85
1f038a0c 86my $prompting = 0;
83b24437
RA
87if (!defined $from) {
88 $from = $author || $committer;
8037d1a3
RA
89 do {
90 $_ = $term->readline("Who should the emails appear to be from? ",
91 $from);
ca9a7d65 92 } while (!defined $_);
8037d1a3 93
83b24437
RA
94 $from = $_;
95 print "Emails will be sent from: ", $from, "\n";
1f038a0c 96 $prompting++;
83b24437
RA
97}
98
99if (!@to) {
8037d1a3 100 do {
5825e5b2 101 $_ = $term->readline("Who should the emails be sent to? ",
8037d1a3
RA
102 "");
103 } while (!defined $_);
83b24437
RA
104 my $to = $_;
105 push @to, split /,/, $to;
1f038a0c 106 $prompting++;
83b24437
RA
107}
108
1f038a0c 109if (!defined $initial_subject && $compose) {
8037d1a3 110 do {
5825e5b2 111 $_ = $term->readline("What subject should the emails start with? ",
8037d1a3
RA
112 $initial_subject);
113 } while (!defined $_);
83b24437 114 $initial_subject = $_;
1f038a0c 115 $prompting++;
83b24437
RA
116}
117
1f038a0c 118if (!defined $initial_reply_to && $prompting) {
8037d1a3 119 do {
1f038a0c 120 $_= $term->readline("Message-ID to be used as In-Reply-To for the first email? ",
8037d1a3
RA
121 $initial_reply_to);
122 } while (!defined $_);
123
83b24437 124 $initial_reply_to = $_;
78488b2c 125 $initial_reply_to =~ s/(^\s+|\s+$)//g;
83b24437
RA
126}
127
3342d850
RA
128if (!defined $smtp_server) {
129 $smtp_server = "localhost";
130}
131
1f038a0c
RA
132if ($compose) {
133 # Note that this does not need to be secure, but we will make a small
134 # effort to have it be unique
135 open(C,">",$compose_filename)
136 or die "Failed to open for writing $compose_filename: $!";
d366c703 137 print C "From $from # This line is ignored.\n";
1f038a0c
RA
138 printf C "Subject: %s\n\n", $initial_subject;
139 printf C <<EOT;
140GIT: Please enter your email below.
141GIT: Lines beginning in "GIT: " will be removed.
142GIT: Consider including an overall diffstat or table of contents
143GIT: for the patch you are writing.
144
145EOT
146 close(C);
147
148 my $editor = $ENV{EDITOR};
149 $editor = 'vi' unless defined $editor;
150 system($editor, $compose_filename);
151
152 open(C2,">",$compose_filename . ".final")
153 or die "Failed to open $compose_filename.final : " . $!;
154
155 open(C,"<",$compose_filename)
156 or die "Failed to open $compose_filename : " . $!;
157
158 while(<C>) {
159 next if m/^GIT: /;
160 print C2 $_;
161 }
162 close(C);
163 close(C2);
164
165 do {
166 $_ = $term->readline("Send this email? (y|n) ");
167 } while (!defined $_);
168
169 if (uc substr($_,0,1) ne 'Y') {
170 cleanup_compose_files();
171 exit(0);
172 }
173
174 @files = ($compose_filename . ".final");
175}
176
177
83b24437
RA
178# Now that all the defaults are set, process the rest of the command line
179# arguments and collect up the files that need to be processed.
180for my $f (@ARGV) {
181 if (-d $f) {
182 opendir(DH,$f)
183 or die "Failed to opendir $f: $!";
184
5825e5b2 185 push @files, grep { -f $_ } map { +$f . "/" . $_ }
8037d1a3
RA
186 sort readdir(DH);
187
83b24437
RA
188 } elsif (-f $f) {
189 push @files, $f;
190
191 } else {
192 print STDERR "Skipping $f - not found.\n";
193 }
194}
195
196if (@files) {
2718435b
RA
197 unless ($quiet) {
198 print $_,"\n" for (@files);
199 }
83b24437
RA
200} else {
201 print <<EOT;
215a7ad1 202git-send-email [options] <file | directory> [... file | directory ]
83b24437
RA
203Options:
204 --from Specify the "From:" line of the email to be sent.
1f038a0c 205
5825e5b2 206 --to Specify the primary "To:" line of the email.
1f038a0c 207
da140f8b
RA
208 --cc Specify an initial "Cc:" list for the entire series
209 of emails.
210
1f038a0c
RA
211 --compose Use \$EDITOR to edit an introductory message for the
212 patch series.
213
83b24437 214 --subject Specify the initial "Subject:" line.
1f038a0c
RA
215 Only necessary if --compose is also set. If --compose
216 is not set, this will be prompted for.
217
83b24437 218 --in-reply-to Specify the first "In-Reply-To:" header line.
1f038a0c
RA
219 Only used if --compose is also set. If --compose is not
220 set, this will be prompted for.
221
e205735d 222 --chain-reply-to If set, the replies will all be to the previous
5825e5b2
JH
223 email sent, rather than to the first email sent.
224 Defaults to on.
1f038a0c 225
a985d595
RA
226 --no-signed-off-cc Suppress the automatic addition of email addresses
227 that appear in a Signed-off-by: line, to the cc: list.
228 Note: Using this option is not recommended.
229
3342d850
RA
230 --smtp-server If set, specifies the outgoing SMTP server to use.
231 Defaults to localhost.
83b24437 232
a985d595
RA
233 --suppress-from Supress sending emails to yourself if your address
234 appears in a From: line.
235
2718435b
RA
236 --quiet Make git-send-email less verbose. One line per email should be
237 all that is output.
238
83b24437
RA
239Error: Please specify a file or a directory on the command line.
240EOT
241 exit(1);
242}
243
244# Variables we set as part of the loop over files
245our ($message_id, $cc, %mail, $subject, $reply_to, $message);
246
247
248# Usually don't need to change anything below here.
249
250# we make a "fake" message id by taking the current number
251# of seconds since the beginning of Unix time and tacking on
252# a random number to the end, in case we are called quicker than
253# 1 second since the last time we were called.
8037d1a3
RA
254
255# We'll setup a template for the message id, using the "from" address:
256my $message_id_from = Email::Valid->address($from);
e205735d 257my $message_id_template = "<%s-git-send-email-$message_id_from>";
8037d1a3 258
83b24437
RA
259sub make_message_id
260{
72095d5c 261 my $date = time;
83b24437 262 my $pseudo_rand = int (rand(4200));
8037d1a3
RA
263 $message_id = sprintf $message_id_template, "$date$pseudo_rand";
264 #print "new message id = $message_id\n"; # Was useful for debugging
83b24437
RA
265}
266
267
268
269$cc = "";
270
271sub send_message
272{
e205735d 273 my $to = join (", ", unique_email_list(@to));
83b24437
RA
274
275 %mail = ( To => $to,
276 From => $from,
277 CC => $cc,
278 Subject => $subject,
279 Message => $message,
280 'Reply-to' => $from,
281 'In-Reply-To' => $reply_to,
282 'Message-ID' => $message_id,
215a7ad1 283 'X-Mailer' => "git-send-email",
83b24437
RA
284 );
285
3342d850 286 $mail{smtp} = $smtp_server;
9133261f 287 $mailcfg{mime} = 0;
83b24437
RA
288
289 #print Data::Dumper->Dump([\%mail],[qw(*mail)]);
290
291 sendmail(%mail) or die $Mail::Sendmail::error;
292
2718435b
RA
293 if ($quiet) {
294 printf "Sent %s\n", $subject;
295 } else {
30d08b34
RA
296 print "OK. Log says:\n", $Mail::Sendmail::log;
297 print "\n\n"
298 }
83b24437
RA
299}
300
301
302$reply_to = $initial_reply_to;
303make_message_id();
304$subject = $initial_subject;
305
306foreach my $t (@files) {
83b24437
RA
307 open(F,"<",$t) or die "can't open file $t";
308
8a8e6235 309 my $author_not_sender = undef;
da140f8b 310 @cc = @initial_cc;
83b24437
RA
311 my $found_mbox = 0;
312 my $header_done = 0;
313 $message = "";
314 while(<F>) {
315 if (!$header_done) {
316 $found_mbox = 1, next if (/^From /);
317 chomp;
318
319 if ($found_mbox) {
320 if (/^Subject:\s+(.*)$/) {
321 $subject = $1;
322
323 } elsif (/^(Cc|From):\s+(.*)$/) {
8a8e6235
JH
324 if ($2 eq $from) {
325 next if ($suppress_from);
326 }
327 else {
328 $author_not_sender = $2;
329 }
83b24437 330 printf("(mbox) Adding cc: %s from line '%s'\n",
2718435b 331 $2, $_) unless $quiet;
83b24437
RA
332 push @cc, $2;
333 }
334
335 } else {
336 # In the traditional
337 # "send lots of email" format,
338 # line 1 = cc
339 # line 2 = subject
340 # So let's support that, too.
341 if (@cc == 0) {
342 printf("(non-mbox) Adding cc: %s from line '%s'\n",
2718435b 343 $_, $_) unless $quiet;
83b24437
RA
344
345 push @cc, $_;
346
347 } elsif (!defined $subject) {
348 $subject = $_;
349 }
350 }
5825e5b2 351
83b24437
RA
352 # A whitespace line will terminate the headers
353 if (m/^\s*$/) {
354 $header_done = 1;
355 }
356 } else {
357 $message .= $_;
a985d595 358 if (/^Signed-off-by: (.*)$/i && !$no_signed_off_cc) {
83b24437
RA
359 my $c = $1;
360 chomp $c;
361 push @cc, $c;
362 printf("(sob) Adding cc: %s from line '%s'\n",
2718435b 363 $c, $_) unless $quiet;
83b24437
RA
364 }
365 }
366 }
367 close F;
8a8e6235
JH
368 if (defined $author_not_sender) {
369 $message = "From: $author_not_sender\n\n$message";
370 }
83b24437 371
e205735d 372 $cc = join(", ", unique_email_list(@cc));
83b24437
RA
373
374 send_message();
375
376 # set up for the next message
78488b2c
RA
377 if ($chain_reply_to || length($reply_to) == 0) {
378 $reply_to = $message_id;
379 }
83b24437 380 make_message_id();
83b24437 381}
e205735d 382
1f038a0c
RA
383if ($compose) {
384 cleanup_compose_files();
385}
386
387sub cleanup_compose_files() {
388 unlink($compose_filename, $compose_filename . ".final");
389
390}
391
392
e205735d
RA
393
394sub unique_email_list(@) {
395 my %seen;
396 my @emails;
397
398 foreach my $entry (@_) {
399 my $clean = Email::Valid->address($entry);
400 next if $seen{$clean}++;
401 push @emails, $entry;
402 }
403 return @emails;
404}