daemon: Set SO_REUSEADDR on listening sockets.
[git/jrn.git] / git-send-email.perl
blob2977b9adebff3180f156327622a9a11067e01696
1 #!/usr/bin/perl -w
3 # Copyright 2002,2005 Greg Kroah-Hartman <greg@kroah.com>
4 # Copyright 2005 Ryan Anderson <ryan@michonline.com>
6 # GPL v2 (See COPYING)
8 # Ported to support git "mbox" format files by Ryan Anderson <ryan@michonline.com>
10 # Sends a collection of emails to the given email addresses, disturbingly fast.
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.
19 use strict;
20 use warnings;
21 use Term::ReadLine;
22 use Mail::Sendmail qw(sendmail %mailcfg);
23 use Getopt::Long;
24 use Data::Dumper;
25 use Email::Valid;
27 sub unique_email_list(@);
28 sub cleanup_compose_files();
30 # Constants (essentially)
31 my $compose_filename = ".msg.$$";
33 # Variables we fill in automatically, or via prompting:
34 my (@to,@cc,$initial_reply_to,$initial_subject,@files,$from,$compose);
36 # Behavior modification variables
37 my ($chain_reply_to, $smtp_server, $quiet) = (1, "localhost", 0);
39 # Example reply to:
40 #$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
42 my $term = new Term::ReadLine 'git-send-email';
44 # Begin by accumulating all the variables (defined above), that we will end up
45 # needing, first, from the command line:
47 my $rc = GetOptions("from=s" => \$from,
48 "in-reply-to=s" => \$initial_reply_to,
49 "subject=s" => \$initial_subject,
50 "to=s" => \@to,
51 "chain-reply-to!" => \$chain_reply_to,
52 "smtp-server=s" => \$smtp_server,
53 "compose" => \$compose,
54 "quiet" => \$quiet,
57 # Now, let's fill any that aren't set in with defaults:
59 open(GITVAR,"-|","git-var","-l")
60 or die "Failed to open pipe from git-var: $!";
62 my ($author,$committer);
63 while(<GITVAR>) {
64 chomp;
65 my ($var,$data) = split /=/,$_,2;
66 my @fields = split /\s+/, $data;
68 my $ident = join(" ", @fields[0...(@fields-3)]);
70 if ($var eq 'GIT_AUTHOR_IDENT') {
71 $author = $ident;
72 } elsif ($var eq 'GIT_COMMITTER_IDENT') {
73 $committer = $ident;
76 close(GITVAR);
78 my $prompting = 0;
79 if (!defined $from) {
80 $from = $author || $committer;
81 do {
82 $_ = $term->readline("Who should the emails appear to be from? ",
83 $from);
84 } while (!defined $_);
86 $from = $_;
87 print "Emails will be sent from: ", $from, "\n";
88 $prompting++;
91 if (!@to) {
92 do {
93 $_ = $term->readline("Who should the emails be sent to? ",
94 "");
95 } while (!defined $_);
96 my $to = $_;
97 push @to, split /,/, $to;
98 $prompting++;
101 if (!defined $initial_subject && $compose) {
102 do {
103 $_ = $term->readline("What subject should the emails start with? ",
104 $initial_subject);
105 } while (!defined $_);
106 $initial_subject = $_;
107 $prompting++;
110 if (!defined $initial_reply_to && $prompting) {
111 do {
112 $_= $term->readline("Message-ID to be used as In-Reply-To for the first email? ",
113 $initial_reply_to);
114 } while (!defined $_);
116 $initial_reply_to = $_;
117 $initial_reply_to =~ s/(^\s+|\s+$)//g;
120 if (!defined $smtp_server) {
121 $smtp_server = "localhost";
124 if ($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: $!";
129 print C "From $from # This line is ignored.\n";
130 printf C "Subject: %s\n\n", $initial_subject;
131 printf C <<EOT;
132 GIT: Please enter your email below.
133 GIT: Lines beginning in "GIT: " will be removed.
134 GIT: Consider including an overall diffstat or table of contents
135 GIT: for the patch you are writing.
138 close(C);
140 my $editor = $ENV{EDITOR};
141 $editor = 'vi' unless defined $editor;
142 system($editor, $compose_filename);
144 open(C2,">",$compose_filename . ".final")
145 or die "Failed to open $compose_filename.final : " . $!;
147 open(C,"<",$compose_filename)
148 or die "Failed to open $compose_filename : " . $!;
150 while(<C>) {
151 next if m/^GIT: /;
152 print C2 $_;
154 close(C);
155 close(C2);
157 do {
158 $_ = $term->readline("Send this email? (y|n) ");
159 } while (!defined $_);
161 if (uc substr($_,0,1) ne 'Y') {
162 cleanup_compose_files();
163 exit(0);
166 @files = ($compose_filename . ".final");
170 # 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.
172 for my $f (@ARGV) {
173 if (-d $f) {
174 opendir(DH,$f)
175 or die "Failed to opendir $f: $!";
177 push @files, grep { -f $_ } map { +$f . "/" . $_ }
178 sort readdir(DH);
180 } elsif (-f $f) {
181 push @files, $f;
183 } else {
184 print STDERR "Skipping $f - not found.\n";
188 if (@files) {
189 print $_,"\n" for @files;
190 } else {
191 print <<EOT;
192 git-send-email [options] <file | directory> [... file | directory ]
193 Options:
194 --from Specify the "From:" line of the email to be sent.
196 --to Specify the primary "To:" line of the email.
198 --compose Use \$EDITOR to edit an introductory message for the
199 patch series.
201 --subject Specify the initial "Subject:" line.
202 Only necessary if --compose is also set. If --compose
203 is not set, this will be prompted for.
205 --in-reply-to Specify the first "In-Reply-To:" header line.
206 Only used if --compose is also set. If --compose is not
207 set, this will be prompted for.
209 --chain-reply-to If set, the replies will all be to the previous
210 email sent, rather than to the first email sent.
211 Defaults to on.
213 --smtp-server If set, specifies the outgoing SMTP server to use.
214 Defaults to localhost.
216 Error: Please specify a file or a directory on the command line.
218 exit(1);
221 # Variables we set as part of the loop over files
222 our ($message_id, $cc, %mail, $subject, $reply_to, $message);
225 # Usually don't need to change anything below here.
227 # we make a "fake" message id by taking the current number
228 # of seconds since the beginning of Unix time and tacking on
229 # a random number to the end, in case we are called quicker than
230 # 1 second since the last time we were called.
232 # We'll setup a template for the message id, using the "from" address:
233 my $message_id_from = Email::Valid->address($from);
234 my $message_id_template = "<%s-git-send-email-$message_id_from>";
236 sub make_message_id
238 my $date = `date "+\%s"`;
239 chomp($date);
240 my $pseudo_rand = int (rand(4200));
241 $message_id = sprintf $message_id_template, "$date$pseudo_rand";
242 #print "new message id = $message_id\n"; # Was useful for debugging
247 $cc = "";
249 sub send_message
251 my $to = join (", ", unique_email_list(@to));
253 %mail = ( To => $to,
254 From => $from,
255 CC => $cc,
256 Subject => $subject,
257 Message => $message,
258 'Reply-to' => $from,
259 'In-Reply-To' => $reply_to,
260 'Message-ID' => $message_id,
261 'X-Mailer' => "git-send-email",
264 $mail{smtp} = $smtp_server;
265 $mailcfg{mime} = 0;
267 #print Data::Dumper->Dump([\%mail],[qw(*mail)]);
269 sendmail(%mail) or die $Mail::Sendmail::error;
271 unless ($quiet) {
272 print "OK. Log says:\n", $Mail::Sendmail::log;
273 print "\n\n"
278 $reply_to = $initial_reply_to;
279 make_message_id();
280 $subject = $initial_subject;
282 foreach my $t (@files) {
283 my $F = $t;
284 open(F,"<",$t) or die "can't open file $t";
286 @cc = ();
287 my $found_mbox = 0;
288 my $header_done = 0;
289 $message = "";
290 while(<F>) {
291 if (!$header_done) {
292 $found_mbox = 1, next if (/^From /);
293 chomp;
295 if ($found_mbox) {
296 if (/^Subject:\s+(.*)$/) {
297 $subject = $1;
299 } elsif (/^(Cc|From):\s+(.*)$/) {
300 printf("(mbox) Adding cc: %s from line '%s'\n",
301 $2, $_);
302 push @cc, $2;
305 } else {
306 # In the traditional
307 # "send lots of email" format,
308 # line 1 = cc
309 # line 2 = subject
310 # So let's support that, too.
311 if (@cc == 0) {
312 printf("(non-mbox) Adding cc: %s from line '%s'\n",
313 $_, $_);
315 push @cc, $_;
317 } elsif (!defined $subject) {
318 $subject = $_;
322 # A whitespace line will terminate the headers
323 if (m/^\s*$/) {
324 $header_done = 1;
326 } else {
327 $message .= $_;
328 if (/^Signed-off-by: (.*)$/i) {
329 my $c = $1;
330 chomp $c;
331 push @cc, $c;
332 printf("(sob) Adding cc: %s from line '%s'\n",
333 $c, $_);
337 close F;
339 $cc = join(", ", unique_email_list(@cc));
341 send_message();
343 # set up for the next message
344 if ($chain_reply_to || length($reply_to) == 0) {
345 $reply_to = $message_id;
347 make_message_id();
350 if ($compose) {
351 cleanup_compose_files();
354 sub cleanup_compose_files() {
355 unlink($compose_filename, $compose_filename . ".final");
361 sub unique_email_list(@) {
362 my %seen;
363 my @emails;
365 foreach my $entry (@_) {
366 my $clean = Email::Valid->address($entry);
367 next if $seen{$clean}++;
368 push @emails, $entry;
370 return @emails;