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 #
19 use strict;
20 use warnings;
21 use Term::ReadLine;
22 use Getopt::Long;
23 use Data::Dumper;
24 use Net::SMTP;
25 use Email::Valid;
27 # most mail servers generate the Date: header, but not all...
28 $ENV{LC_ALL} = 'C';
29 use POSIX qw/strftime/;
31 my $smtp;
33 sub unique_email_list(@);
34 sub cleanup_compose_files();
36 # Constants (essentially)
37 my $compose_filename = ".msg.$$";
39 # Variables we fill in automatically, or via prompting:
40 my (@to,@cc,@initial_cc,$initial_reply_to,$initial_subject,@files,$from,$compose,$time);
42 # Behavior modification variables
43 my ($chain_reply_to, $smtp_server, $quiet, $suppress_from, $no_signed_off_cc) = (1, "localhost", 0, 0, 0);
45 # Example reply to:
46 #$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
48 my $term = new Term::ReadLine 'git-send-email';
50 # Begin by accumulating all the variables (defined above), that we will end up
51 # needing, first, from the command line:
53 my $rc = GetOptions("from=s" => \$from,
54 "in-reply-to=s" => \$initial_reply_to,
55 "subject=s" => \$initial_subject,
56 "to=s" => \@to,
57 "cc=s" => \@initial_cc,
58 "chain-reply-to!" => \$chain_reply_to,
59 "smtp-server=s" => \$smtp_server,
60 "compose" => \$compose,
61 "quiet" => \$quiet,
62 "suppress-from" => \$suppress_from,
63 "no-signed-off-cc|no-signed-off-by-cc" => \$no_signed_off_cc,
64 );
66 # Now, let's fill any that aren't set in with defaults:
68 sub 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;
80 }
82 sub gitvar_ident {
83 my ($name) = @_;
84 my $val = gitvar($name);
85 my @field = split(/\s+/, $val);
86 return join(' ', @field[0...(@field-3)]);
87 }
89 my ($author) = gitvar_ident('GIT_AUTHOR_IDENT');
90 my ($committer) = gitvar_ident('GIT_COMMITTER_IDENT');
92 my $prompting = 0;
93 if (!defined $from) {
94 $from = $author || $committer;
95 do {
96 $_ = $term->readline("Who should the emails appear to be from? ",
97 $from);
98 } while (!defined $_);
100 $from = $_;
101 print "Emails will be sent from: ", $from, "\n";
102 $prompting++;
103 }
105 if (!@to) {
106 do {
107 $_ = $term->readline("Who should the emails be sent to? ",
108 "");
109 } while (!defined $_);
110 my $to = $_;
111 push @to, split /,/, $to;
112 $prompting++;
113 }
115 if (!defined $initial_subject && $compose) {
116 do {
117 $_ = $term->readline("What subject should the emails start with? ",
118 $initial_subject);
119 } while (!defined $_);
120 $initial_subject = $_;
121 $prompting++;
122 }
124 if (!defined $initial_reply_to && $prompting) {
125 do {
126 $_= $term->readline("Message-ID to be used as In-Reply-To for the first email? ",
127 $initial_reply_to);
128 } while (!defined $_);
130 $initial_reply_to = $_;
131 $initial_reply_to =~ s/(^\s+|\s+$)//g;
132 }
134 if (!defined $smtp_server) {
135 $smtp_server = "localhost";
136 }
138 if ($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: $!";
143 print C "From $from # This line is ignored.\n";
144 printf C "Subject: %s\n\n", $initial_subject;
145 printf C <<EOT;
146 GIT: Please enter your email below.
147 GIT: Lines beginning in "GIT: " will be removed.
148 GIT: Consider including an overall diffstat or table of contents
149 GIT: for the patch you are writing.
151 EOT
152 close(C);
154 my $editor = $ENV{EDITOR};
155 $editor = 'vi' unless defined $editor;
156 system($editor, $compose_filename);
158 open(C2,">",$compose_filename . ".final")
159 or die "Failed to open $compose_filename.final : " . $!;
161 open(C,"<",$compose_filename)
162 or die "Failed to open $compose_filename : " . $!;
164 while(<C>) {
165 next if m/^GIT: /;
166 print C2 $_;
167 }
168 close(C);
169 close(C2);
171 do {
172 $_ = $term->readline("Send this email? (y|n) ");
173 } while (!defined $_);
175 if (uc substr($_,0,1) ne 'Y') {
176 cleanup_compose_files();
177 exit(0);
178 }
180 @files = ($compose_filename . ".final");
181 }
184 # 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.
186 for my $f (@ARGV) {
187 if (-d $f) {
188 opendir(DH,$f)
189 or die "Failed to opendir $f: $!";
191 push @files, grep { -f $_ } map { +$f . "/" . $_ }
192 sort readdir(DH);
194 } elsif (-f $f) {
195 push @files, $f;
197 } else {
198 print STDERR "Skipping $f - not found.\n";
199 }
200 }
202 if (@files) {
203 unless ($quiet) {
204 print $_,"\n" for (@files);
205 }
206 } else {
207 print <<EOT;
208 git-send-email [options] <file | directory> [... file | directory ]
209 Options:
210 --from Specify the "From:" line of the email to be sent.
212 --to Specify the primary "To:" line of the email.
214 --cc Specify an initial "Cc:" list for the entire series
215 of emails.
217 --compose Use \$EDITOR to edit an introductory message for the
218 patch series.
220 --subject Specify the initial "Subject:" line.
221 Only necessary if --compose is also set. If --compose
222 is not set, this will be prompted for.
224 --in-reply-to Specify the first "In-Reply-To:" header line.
225 Only used if --compose is also set. If --compose is not
226 set, this will be prompted for.
228 --chain-reply-to If set, the replies will all be to the previous
229 email sent, rather than to the first email sent.
230 Defaults to on.
232 --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.
236 --smtp-server If set, specifies the outgoing SMTP server to use.
237 Defaults to localhost.
239 --suppress-from Supress sending emails to yourself if your address
240 appears in a From: line.
242 --quiet Make git-send-email less verbose. One line per email should be
243 all that is output.
245 Error: Please specify a file or a directory on the command line.
246 EOT
247 exit(1);
248 }
250 # Variables we set as part of the loop over files
251 our ($message_id, $cc, %mail, $subject, $reply_to, $message);
254 # Usually don't need to change anything below here.
256 # we make a "fake" message id by taking the current number
257 # of seconds since the beginning of Unix time and tacking on
258 # a random number to the end, in case we are called quicker than
259 # 1 second since the last time we were called.
261 # We'll setup a template for the message id, using the "from" address:
262 my $message_id_from = Email::Valid->address($from);
263 my $message_id_template = "<%s-git-send-email-$message_id_from>";
265 sub make_message_id
266 {
267 my $date = time;
268 my $pseudo_rand = int (rand(4200));
269 $message_id = sprintf $message_id_template, "$date$pseudo_rand";
270 #print "new message id = $message_id\n"; # Was useful for debugging
271 }
275 $cc = "";
276 $time = time - scalar $#files;
278 sub send_message
279 {
280 my @recipients = unique_email_list(@to);
281 my $to = join (",\n\t", @recipients);
282 @recipients = unique_email_list(@recipients,@cc);
283 my $date = strftime('%a, %d %b %Y %H:%M:%S %z', localtime($time++));
285 my $header = "From: $from
286 To: $to
287 Cc: $cc
288 Subject: $subject
289 Reply-To: $from
290 Date: $date
291 Message-Id: $message_id
292 X-Mailer: git-send-email @@GIT_VERSION@@
293 ";
294 $header .= "In-Reply-To: $reply_to\n" if $reply_to;
296 $smtp ||= Net::SMTP->new( $smtp_server );
297 $smtp->mail( $from ) or die $smtp->message;
298 $smtp->to( @recipients ) or die $smtp->message;
299 $smtp->data or die $smtp->message;
300 $smtp->datasend("$header\n$message") or die $smtp->message;
301 $smtp->dataend() or die $smtp->message;
302 $smtp->ok or die "Failed to send $subject\n".$smtp->message;
304 if ($quiet) {
305 printf "Sent %s\n", $subject;
306 } else {
307 print "OK. Log says:
308 Date: $date
309 Server: $smtp_server Port: 25
310 From: $from
311 Subject: $subject
312 Cc: $cc
313 To: $to
315 Result: ", $smtp->code, ' ', ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
316 }
317 }
319 $reply_to = $initial_reply_to;
320 make_message_id();
321 $subject = $initial_subject;
323 foreach my $t (@files) {
324 open(F,"<",$t) or die "can't open file $t";
326 my $author_not_sender = undef;
327 @cc = @initial_cc;
328 my $found_mbox = 0;
329 my $header_done = 0;
330 $message = "";
331 while(<F>) {
332 if (!$header_done) {
333 $found_mbox = 1, next if (/^From /);
334 chomp;
336 if ($found_mbox) {
337 if (/^Subject:\s+(.*)$/) {
338 $subject = $1;
340 } elsif (/^(Cc|From):\s+(.*)$/) {
341 if ($2 eq $from) {
342 next if ($suppress_from);
343 }
344 else {
345 $author_not_sender = $2;
346 }
347 printf("(mbox) Adding cc: %s from line '%s'\n",
348 $2, $_) unless $quiet;
349 push @cc, $2;
350 }
352 } else {
353 # In the traditional
354 # "send lots of email" format,
355 # line 1 = cc
356 # line 2 = subject
357 # So let's support that, too.
358 if (@cc == 0) {
359 printf("(non-mbox) Adding cc: %s from line '%s'\n",
360 $_, $_) unless $quiet;
362 push @cc, $_;
364 } elsif (!defined $subject) {
365 $subject = $_;
366 }
367 }
369 # A whitespace line will terminate the headers
370 if (m/^\s*$/) {
371 $header_done = 1;
372 }
373 } else {
374 $message .= $_;
375 if (/^Signed-off-by: (.*)$/i && !$no_signed_off_cc) {
376 my $c = $1;
377 chomp $c;
378 push @cc, $c;
379 printf("(sob) Adding cc: %s from line '%s'\n",
380 $c, $_) unless $quiet;
381 }
382 }
383 }
384 close F;
385 if (defined $author_not_sender) {
386 $message = "From: $author_not_sender\n\n$message";
387 }
389 $cc = join(", ", unique_email_list(@cc));
391 send_message();
393 # set up for the next message
394 if ($chain_reply_to || length($reply_to) == 0) {
395 $reply_to = $message_id;
396 }
397 make_message_id();
398 }
400 if ($compose) {
401 cleanup_compose_files();
402 }
404 sub cleanup_compose_files() {
405 unlink($compose_filename, $compose_filename . ".final");
407 }
409 $smtp->quit if $smtp;
411 sub unique_email_list(@) {
412 my %seen;
413 my @emails;
415 foreach my $entry (@_) {
416 my $clean = Email::Valid->address($entry);
417 next if $seen{$clean}++;
418 push @emails, $entry;
419 }
420 return @emails;
421 }