blob: 87f59fa313f4483dc15846979e80e8a8aec3d1f5 [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 Anderson83b24432005-07-31 04:17:25 -040022use Getopt::Long;
23use Data::Dumper;
Petr Baudis3cb8caf2006-07-03 22:47:58 +020024use Git;
Ryan Anderson83b24432005-07-31 04:17:25 -040025
Junio C Hamano280242d2006-07-02 16:03:59 -070026package FakeTerm;
27sub new {
28 my ($class, $reason) = @_;
29 return bless \$reason, shift;
30}
31sub readline {
32 my $self = shift;
33 die "Cannot use readline on FakeTerm: $$self";
34}
35package main;
36
Michael Coleman1b0baf12007-02-27 22:47:54 -060037
38sub usage {
39 print <<EOT;
40git-send-email [options] <file | directory>...
41Options:
42 --from Specify the "From:" line of the email to be sent.
43
44 --to Specify the primary "To:" line of the email.
45
46 --cc Specify an initial "Cc:" list for the entire series
47 of emails.
48
49 --bcc Specify a list of email addresses that should be Bcc:
50 on all the emails.
51
52 --compose Use \$EDITOR to edit an introductory message for the
53 patch series.
54
55 --subject Specify the initial "Subject:" line.
56 Only necessary if --compose is also set. If --compose
57 is not set, this will be prompted for.
58
59 --in-reply-to Specify the first "In-Reply-To:" header line.
60 Only used if --compose is also set. If --compose is not
61 set, this will be prompted for.
62
63 --chain-reply-to If set, the replies will all be to the previous
64 email sent, rather than to the first email sent.
65 Defaults to on.
66
Adam Roben5483c712007-06-27 20:59:37 -070067 --signed-off-cc Automatically add email addresses that appear in
68 Signed-off-by: or Cc: lines to the cc: list. Defaults to on.
Michael Coleman1b0baf12007-02-27 22:47:54 -060069
70 --smtp-server If set, specifies the outgoing SMTP server to use.
71 Defaults to localhost.
72
73 --suppress-from Suppress sending emails to yourself if your address
Adam Roben5483c712007-06-27 20:59:37 -070074 appears in a From: line. Defaults to off.
Michael Coleman1b0baf12007-02-27 22:47:54 -060075
Adam Roben5483c712007-06-27 20:59:37 -070076 --thread Specify that the "In-Reply-To:" header should be set on all
Adam Robene46f7a02007-06-26 15:48:30 -070077 emails. Defaults to on.
78
Michael Coleman1b0baf12007-02-27 22:47:54 -060079 --quiet Make git-send-email less verbose. One line per email
80 should be all that is output.
81
Robin H. Johnson238cc632007-04-25 19:37:15 -070082 --dry-run Do everything except actually send the emails.
83
Robin H. Johnsonf073a592007-04-25 19:37:22 -070084 --envelope-sender Specify the envelope sender used to send the emails.
85
Michael Coleman1b0baf12007-02-27 22:47:54 -060086EOT
87 exit(1);
88}
89
Eric Wong4bc87a22006-03-25 17:20:48 -080090# most mail servers generate the Date: header, but not all...
Jakub Narebski6bdca892006-07-07 20:57:55 +020091sub format_2822_time {
92 my ($time) = @_;
93 my @localtm = localtime($time);
94 my @gmttm = gmtime($time);
95 my $localmin = $localtm[1] + $localtm[2] * 60;
96 my $gmtmin = $gmttm[1] + $gmttm[2] * 60;
97 if ($localtm[0] != $gmttm[0]) {
98 die "local zone differs from GMT by a non-minute interval\n";
99 }
100 if ((($gmttm[6] + 1) % 7) == $localtm[6]) {
101 $localmin += 1440;
102 } elsif ((($gmttm[6] - 1) % 7) == $localtm[6]) {
103 $localmin -= 1440;
104 } elsif ($gmttm[6] != $localtm[6]) {
105 die "local time offset greater than or equal to 24 hours\n";
106 }
107 my $offset = $localmin - $gmtmin;
108 my $offhour = $offset / 60;
109 my $offmin = abs($offset % 60);
110 if (abs($offhour) >= 24) {
111 die ("local time offset greater than or equal to 24 hours\n");
112 }
113
114 return sprintf("%s, %2d %s %d %02d:%02d:%02d %s%02d%02d",
115 qw(Sun Mon Tue Wed Thu Fri Sat)[$localtm[6]],
116 $localtm[3],
117 qw(Jan Feb Mar Apr May Jun
118 Jul Aug Sep Oct Nov Dec)[$localtm[4]],
119 $localtm[5]+1900,
120 $localtm[2],
121 $localtm[1],
122 $localtm[0],
123 ($offset >= 0) ? '+' : '-',
124 abs($offhour),
125 $offmin,
126 );
127}
Eric Wong4bc87a22006-03-25 17:20:48 -0800128
Eric Wong567ffeb2006-03-25 16:47:12 -0800129my $have_email_valid = eval { require Email::Valid; 1 };
Eric Wong4bc87a22006-03-25 17:20:48 -0800130my $smtp;
131
Ryan Andersone2057352005-08-02 21:45:22 -0400132sub unique_email_list(@);
Ryan Anderson1f038a02005-09-05 01:13:07 -0400133sub cleanup_compose_files();
134
135# Constants (essentially)
136my $compose_filename = ".msg.$$";
Ryan Andersone2057352005-08-02 21:45:22 -0400137
Ryan Anderson83b24432005-07-31 04:17:25 -0400138# Variables we fill in automatically, or via prompting:
Junio C Hamanoce91c2f2006-10-05 16:36:49 -0700139my (@to,@cc,@initial_cc,@bcclist,@xh,
Ryan Anderson58063242006-05-29 12:30:13 -0700140 $initial_reply_to,$initial_subject,@files,$from,$compose,$time);
Ryan Anderson83b24432005-07-31 04:17:25 -0400141
Eric Wongaca7ad72006-05-15 02:34:44 -0700142my $smtp_server;
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700143my $envelope_sender;
Ryan Anderson78488b22005-07-31 20:04:24 -0400144
Ryan Anderson91332612005-07-31 20:04:24 -0400145# Example reply to:
Ryan Anderson83b24432005-07-31 04:17:25 -0400146#$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
Ryan Anderson83b24432005-07-31 04:17:25 -0400147
Petr Baudis3cb8caf2006-07-03 22:47:58 +0200148my $repo = Git->repository();
Junio C Hamano280242d2006-07-02 16:03:59 -0700149my $term = eval {
150 new Term::ReadLine 'git-send-email';
151};
152if ($@) {
153 $term = new FakeTerm "$@: going non-interactive";
154}
Ryan Anderson83b24432005-07-31 04:17:25 -0400155
Adam Roben5483c712007-06-27 20:59:37 -0700156# Behavior modification variables
157my ($quiet, $dry_run) = (0, 0);
158
159# Variables with corresponding config settings
160my ($thread, $chain_reply_to, $suppress_from, $signed_off_cc);
161
Adam Robene46f7a02007-06-26 15:48:30 -0700162my %config_settings = (
Adam Roben5483c712007-06-27 20:59:37 -0700163 "thread" => [\$thread, 1],
164 "chainreplyto" => [\$chain_reply_to, 1],
165 "suppressfrom" => [\$suppress_from, 0],
166 "signedoffcc" => [\$signed_off_cc, 1],
Adam Robene46f7a02007-06-26 15:48:30 -0700167);
168
169foreach my $setting (keys %config_settings) {
Adam Roben5483c712007-06-27 20:59:37 -0700170 my $config = $repo->config_bool("sendemail.$setting");
171 ${$config_settings{$setting}->[0]} = (defined $config) ? $config : $config_settings{$setting}->[1];
Avi Kivity4a62d3f2007-03-11 19:19:44 +0200172}
173
174@bcclist = $repo->config('sendemail.bcc');
175if (!@bcclist or !$bcclist[0]) {
176 @bcclist = ();
177}
178
Ryan Anderson83b24432005-07-31 04:17:25 -0400179# Begin by accumulating all the variables (defined above), that we will end up
180# needing, first, from the command line:
181
182my $rc = GetOptions("from=s" => \$from,
183 "in-reply-to=s" => \$initial_reply_to,
184 "subject=s" => \$initial_subject,
185 "to=s" => \@to,
Ryan Andersonda140f82006-02-13 03:05:15 -0500186 "cc=s" => \@initial_cc,
Ryan Anderson58063242006-05-29 12:30:13 -0700187 "bcc=s" => \@bcclist,
Ryan Anderson78488b22005-07-31 20:04:24 -0400188 "chain-reply-to!" => \$chain_reply_to,
Ryan Anderson3342d852005-07-31 20:04:24 -0400189 "smtp-server=s" => \$smtp_server,
Ryan Anderson1f038a02005-09-05 01:13:07 -0400190 "compose" => \$compose,
Ryan Anderson30d08b32006-02-02 11:56:06 -0500191 "quiet" => \$quiet,
Adam Roben5483c712007-06-27 20:59:37 -0700192 "suppress-from!" => \$suppress_from,
193 "signed-off-cc|signed-off-by-cc!" => \$signed_off_cc,
Matthew Wilcox61302592006-10-10 08:58:23 -0600194 "dry-run" => \$dry_run,
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700195 "envelope-sender=s" => \$envelope_sender,
Adam Roben5483c712007-06-27 20:59:37 -0700196 "thread!" => \$thread,
Ryan Anderson83b24432005-07-31 04:17:25 -0400197 );
198
Michael Coleman1b0baf12007-02-27 22:47:54 -0600199unless ($rc) {
200 usage();
201}
202
Eric W. Biederman79ee5552006-06-21 07:17:31 -0600203# Verify the user input
204
205foreach my $entry (@to) {
206 die "Comma in --to entry: $entry'\n" unless $entry !~ m/,/;
207}
208
209foreach my $entry (@initial_cc) {
210 die "Comma in --cc entry: $entry'\n" unless $entry !~ m/,/;
211}
212
213foreach my $entry (@bcclist) {
214 die "Comma in --bcclist entry: $entry'\n" unless $entry !~ m/,/;
215}
216
Ryan Anderson83b24432005-07-31 04:17:25 -0400217# Now, let's fill any that aren't set in with defaults:
218
Petr Baudisc7a30e52006-07-03 22:48:01 +0200219my ($author) = $repo->ident_person('author');
220my ($committer) = $repo->ident_person('committer');
Ryan Anderson83b24432005-07-31 04:17:25 -0400221
Eric Wong994d6c62006-05-14 19:13:44 -0700222my %aliases;
Petr Baudis3cb8caf2006-07-03 22:47:58 +0200223my @alias_files = $repo->config('sendemail.aliasesfile');
224my $aliasfiletype = $repo->config('sendemail.aliasfiletype');
Eric Wong994d6c62006-05-14 19:13:44 -0700225my %parse_alias = (
226 # multiline formats can be supported in the future
227 mutt => sub { my $fh = shift; while (<$fh>) {
Michael Hendricks504ceab2007-05-16 23:15:16 -0600228 if (/^\s*alias\s+(\S+)\s+(.*)$/) {
Eric Wong994d6c62006-05-14 19:13:44 -0700229 my ($alias, $addr) = ($1, $2);
230 $addr =~ s/#.*$//; # mutt allows # comments
231 # commas delimit multiple addresses
232 $aliases{$alias} = [ split(/\s*,\s*/, $addr) ];
233 }}},
234 mailrc => sub { my $fh = shift; while (<$fh>) {
235 if (/^alias\s+(\S+)\s+(.*)$/) {
236 # spaces delimit multiple addresses
237 $aliases{$1} = [ split(/\s+/, $2) ];
238 }}},
239 pine => sub { my $fh = shift; while (<$fh>) {
240 if (/^(\S+)\s+(.*)$/) {
241 $aliases{$1} = [ split(/\s*,\s*/, $2) ];
242 }}},
243 gnus => sub { my $fh = shift; while (<$fh>) {
244 if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
245 $aliases{$1} = [ $2 ];
246 }}}
247);
248
Petr Baudis3cb8caf2006-07-03 22:47:58 +0200249if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) {
Eric Wong994d6c62006-05-14 19:13:44 -0700250 foreach my $file (@alias_files) {
251 open my $fh, '<', $file or die "opening $file: $!\n";
252 $parse_alias{$aliasfiletype}->($fh);
253 close $fh;
254 }
255}
256
Ryan Anderson1f038a02005-09-05 01:13:07 -0400257my $prompting = 0;
Ryan Anderson83b24432005-07-31 04:17:25 -0400258if (!defined $from) {
259 $from = $author || $committer;
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400260 do {
Quy Tonthat2ef58052006-12-28 01:16:21 +1100261 $_ = $term->readline("Who should the emails appear to be from? [$from] ");
Ryan Andersonca9a7d62005-07-31 20:04:25 -0400262 } while (!defined $_);
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400263
Quy Tonthat2ef58052006-12-28 01:16:21 +1100264 $from = $_ if ($_);
Ryan Anderson83b24432005-07-31 04:17:25 -0400265 print "Emails will be sent from: ", $from, "\n";
Ryan Anderson1f038a02005-09-05 01:13:07 -0400266 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -0400267}
268
269if (!@to) {
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400270 do {
Junio C Hamano5825e5b2005-07-31 23:05:16 -0700271 $_ = $term->readline("Who should the emails be sent to? ",
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400272 "");
273 } while (!defined $_);
Ryan Anderson83b24432005-07-31 04:17:25 -0400274 my $to = $_;
275 push @to, split /,/, $to;
Ryan Anderson1f038a02005-09-05 01:13:07 -0400276 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -0400277}
278
Eric Wong994d6c62006-05-14 19:13:44 -0700279sub expand_aliases {
280 my @cur = @_;
281 my @last;
282 do {
283 @last = @cur;
284 @cur = map { $aliases{$_} ? @{$aliases{$_}} : $_ } @last;
285 } while (join(',',@cur) ne join(',',@last));
286 return @cur;
287}
288
289@to = expand_aliases(@to);
Robin H. Johnsonbf7af112007-04-25 21:53:22 -0700290@to = (map { sanitize_address_rfc822($_) } @to);
Eric Wong994d6c62006-05-14 19:13:44 -0700291@initial_cc = expand_aliases(@initial_cc);
Ryan Anderson58063242006-05-29 12:30:13 -0700292@bcclist = expand_aliases(@bcclist);
Eric Wong994d6c62006-05-14 19:13:44 -0700293
Ryan Anderson1f038a02005-09-05 01:13:07 -0400294if (!defined $initial_subject && $compose) {
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400295 do {
Junio C Hamano5825e5b2005-07-31 23:05:16 -0700296 $_ = $term->readline("What subject should the emails start with? ",
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400297 $initial_subject);
298 } while (!defined $_);
Ryan Anderson83b24432005-07-31 04:17:25 -0400299 $initial_subject = $_;
Ryan Anderson1f038a02005-09-05 01:13:07 -0400300 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -0400301}
302
Adam Roben5483c712007-06-27 20:59:37 -0700303if ($thread && !defined $initial_reply_to && $prompting) {
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400304 do {
Ryan Anderson1f038a02005-09-05 01:13:07 -0400305 $_= $term->readline("Message-ID to be used as In-Reply-To for the first email? ",
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400306 $initial_reply_to);
307 } while (!defined $_);
308
Ryan Anderson83b24432005-07-31 04:17:25 -0400309 $initial_reply_to = $_;
Ryan Anderson78488b22005-07-31 20:04:24 -0400310 $initial_reply_to =~ s/(^\s+|\s+$)//g;
Ryan Anderson83b24432005-07-31 04:17:25 -0400311}
312
Eric Wongaca7ad72006-05-15 02:34:44 -0700313if (!$smtp_server) {
Sergey Vlasov6dcfa302006-10-29 22:31:39 +0300314 $smtp_server = $repo->config('sendemail.smtpserver');
315}
316if (!$smtp_server) {
Eric Wongaca7ad72006-05-15 02:34:44 -0700317 foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) {
318 if (-x $_) {
319 $smtp_server = $_;
320 last;
321 }
322 }
323 $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
Ryan Anderson3342d852005-07-31 20:04:24 -0400324}
325
Ryan Anderson1f038a02005-09-05 01:13:07 -0400326if ($compose) {
327 # Note that this does not need to be secure, but we will make a small
328 # effort to have it be unique
329 open(C,">",$compose_filename)
330 or die "Failed to open for writing $compose_filename: $!";
Ryan Andersond366c702006-02-02 11:56:06 -0500331 print C "From $from # This line is ignored.\n";
Ryan Anderson1f038a02005-09-05 01:13:07 -0400332 printf C "Subject: %s\n\n", $initial_subject;
333 printf C <<EOT;
334GIT: Please enter your email below.
335GIT: Lines beginning in "GIT: " will be removed.
336GIT: Consider including an overall diffstat or table of contents
337GIT: for the patch you are writing.
338
339EOT
340 close(C);
341
342 my $editor = $ENV{EDITOR};
343 $editor = 'vi' unless defined $editor;
344 system($editor, $compose_filename);
345
346 open(C2,">",$compose_filename . ".final")
347 or die "Failed to open $compose_filename.final : " . $!;
348
349 open(C,"<",$compose_filename)
350 or die "Failed to open $compose_filename : " . $!;
351
352 while(<C>) {
353 next if m/^GIT: /;
354 print C2 $_;
355 }
356 close(C);
357 close(C2);
358
359 do {
360 $_ = $term->readline("Send this email? (y|n) ");
361 } while (!defined $_);
362
363 if (uc substr($_,0,1) ne 'Y') {
364 cleanup_compose_files();
365 exit(0);
366 }
367
368 @files = ($compose_filename . ".final");
369}
370
371
Ryan Anderson83b24432005-07-31 04:17:25 -0400372# Now that all the defaults are set, process the rest of the command line
373# arguments and collect up the files that need to be processed.
374for my $f (@ARGV) {
375 if (-d $f) {
376 opendir(DH,$f)
377 or die "Failed to opendir $f: $!";
378
Junio C Hamano5825e5b2005-07-31 23:05:16 -0700379 push @files, grep { -f $_ } map { +$f . "/" . $_ }
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400380 sort readdir(DH);
381
Ryan Anderson83b24432005-07-31 04:17:25 -0400382 } elsif (-f $f) {
383 push @files, $f;
384
385 } else {
386 print STDERR "Skipping $f - not found.\n";
387 }
388}
389
390if (@files) {
Ryan Anderson27184352006-02-05 20:13:52 -0500391 unless ($quiet) {
392 print $_,"\n" for (@files);
393 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400394} else {
Michael Coleman1b0baf12007-02-27 22:47:54 -0600395 print STDERR "\nNo patch files specified!\n\n";
396 usage();
Ryan Anderson83b24432005-07-31 04:17:25 -0400397}
398
399# Variables we set as part of the loop over files
Robin H. Johnsonaf068d22007-04-25 19:37:18 -0700400our ($message_id, %mail, $subject, $reply_to, $references, $message);
Ryan Anderson83b24432005-07-31 04:17:25 -0400401
Eric Wong567ffeb2006-03-25 16:47:12 -0800402sub extract_valid_address {
403 my $address = shift;
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700404 my $local_part_regexp = '[^<>"\s@]+';
Junio C Hamano09302e12006-06-06 14:12:46 -0700405 my $domain_regexp = '[^.<>"\s@]+(?:\.[^.<>"\s@]+)+';
Eric Wongdb3106b2006-05-15 02:41:01 -0700406
407 # check for a local address:
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700408 return $address if ($address =~ /^($local_part_regexp)$/);
Eric Wongdb3106b2006-05-15 02:41:01 -0700409
Eric Wong567ffeb2006-03-25 16:47:12 -0800410 if ($have_email_valid) {
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700411 return scalar Email::Valid->address($address);
Eric Wong567ffeb2006-03-25 16:47:12 -0800412 } else {
413 # less robust/correct than the monster regexp in Email::Valid,
414 # but still does a 99% job, and one less dependency
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700415 $address =~ /($local_part_regexp\@$domain_regexp)/;
Horst H. von Brande96fd302006-06-03 13:11:48 -0400416 return $1;
Eric Wong567ffeb2006-03-25 16:47:12 -0800417 }
418}
Ryan Anderson83b24432005-07-31 04:17:25 -0400419
420# Usually don't need to change anything below here.
421
422# we make a "fake" message id by taking the current number
423# of seconds since the beginning of Unix time and tacking on
424# a random number to the end, in case we are called quicker than
425# 1 second since the last time we were called.
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400426
427# We'll setup a template for the message id, using the "from" address:
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400428
Ryan Anderson83b24432005-07-31 04:17:25 -0400429sub make_message_id
430{
Eric Wong72095d52006-03-25 02:43:31 -0800431 my $date = time;
Ryan Anderson83b24432005-07-31 04:17:25 -0400432 my $pseudo_rand = int (rand(4200));
Junio C Hamanoaeb59322007-06-20 13:47:34 -0700433 my $du_part;
434 for ($from, $committer, $author) {
435 $du_part = extract_valid_address($_);
436 last if ($du_part ne '');
437 }
438 if ($du_part eq '') {
439 use Sys::Hostname qw();
440 $du_part = 'user@' . Sys::Hostname::hostname();
441 }
442 my $message_id_template = "<%s-git-send-email-$du_part>";
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400443 $message_id = sprintf $message_id_template, "$date$pseudo_rand";
444 #print "new message id = $message_id\n"; # Was useful for debugging
Ryan Anderson83b24432005-07-31 04:17:25 -0400445}
446
447
448
Eric Wonga5370b12006-03-25 03:01:01 -0800449$time = time - scalar $#files;
Ryan Anderson83b24432005-07-31 04:17:25 -0400450
Jürgen Rühle374c5902007-01-10 13:36:39 -0800451sub unquote_rfc2047 {
452 local ($_) = @_;
453 if (s/=\?utf-8\?q\?(.*)\?=/$1/g) {
454 s/_/ /g;
455 s/=([0-9A-F]{2})/chr(hex($1))/eg;
456 }
Junio C Hamano3740b042007-01-30 02:22:37 -0800457 return "$_";
Jürgen Rühle374c5902007-01-10 13:36:39 -0800458}
459
Robin H. Johnson732263d2007-04-25 19:37:19 -0700460# If an address contains a . in the name portion, the name must be quoted.
461sub sanitize_address_rfc822
462{
463 my ($recipient) = @_;
464 my ($recipient_name) = ($recipient =~ /^(.*?)\s+</);
465 if ($recipient_name && $recipient_name =~ /\./ && $recipient_name !~ /^".*"$/) {
466 my ($name, $addr) = ($recipient =~ /^(.*?)(\s+<.*)/);
467 $recipient = "\"$name\"$addr";
468 }
469 return $recipient;
470}
471
Ryan Anderson83b24432005-07-31 04:17:25 -0400472sub send_message
473{
Eric Wong4bc87a22006-03-25 17:20:48 -0800474 my @recipients = unique_email_list(@to);
Robin H. Johnson732263d2007-04-25 19:37:19 -0700475 @cc = (map { sanitize_address_rfc822($_) } @cc);
Eric Wong4bc87a22006-03-25 17:20:48 -0800476 my $to = join (",\n\t", @recipients);
Ryan Anderson58063242006-05-29 12:30:13 -0700477 @recipients = unique_email_list(@recipients,@cc,@bcclist);
Robin H. Johnsonc38f0242007-04-25 19:37:20 -0700478 @recipients = (map { extract_valid_address($_) } @recipients);
Jakub Narebski6bdca892006-07-07 20:57:55 +0200479 my $date = format_2822_time($time++);
Martin Langhoffe923eff2006-05-03 09:44:36 +1200480 my $gitversion = '@@GIT_VERSION@@';
481 if ($gitversion =~ m/..GIT_VERSION../) {
Petr Baudis3cb8caf2006-07-03 22:47:58 +0200482 $gitversion = Git::version();
Martin Langhoffe923eff2006-05-03 09:44:36 +1200483 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400484
Robin H. Johnsonaf068d22007-04-25 19:37:18 -0700485 my $cc = join(", ", unique_email_list(@cc));
Junio C Hamanof06a6a42007-04-16 16:51:47 -0700486 my $ccline = "";
487 if ($cc ne '') {
488 $ccline = "\nCc: $cc";
489 }
Robin H. Johnson732263d2007-04-25 19:37:19 -0700490 $from = sanitize_address_rfc822($from);
Junio C Hamanoaeb59322007-06-20 13:47:34 -0700491 make_message_id();
492
Eric Wong4bc87a22006-03-25 17:20:48 -0800493 my $header = "From: $from
Junio C Hamanof06a6a42007-04-16 16:51:47 -0700494To: $to${ccline}
Eric Wong4bc87a22006-03-25 17:20:48 -0800495Subject: $subject
Eric Wong4bc87a22006-03-25 17:20:48 -0800496Date: $date
497Message-Id: $message_id
Martin Langhoffe923eff2006-05-03 09:44:36 +1200498X-Mailer: git-send-email $gitversion
Eric Wong4bc87a22006-03-25 17:20:48 -0800499";
Adam Roben5483c712007-06-27 20:59:37 -0700500 if ($thread && $reply_to) {
Ryan Anderson7ccf7922006-05-29 12:30:12 -0700501
502 $header .= "In-Reply-To: $reply_to\n";
503 $header .= "References: $references\n";
504 }
Junio C Hamanoce91c2f2006-10-05 16:36:49 -0700505 if (@xh) {
506 $header .= join("\n", @xh) . "\n";
507 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400508
Robin H. Johnsonc38f0242007-04-25 19:37:20 -0700509 my @sendmail_parameters = ('-i', @recipients);
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700510 my $raw_from = $from;
511 $raw_from = $envelope_sender if (defined $envelope_sender);
512 $raw_from = extract_valid_address($raw_from);
513 unshift (@sendmail_parameters,
514 '-f', $raw_from) if(defined $envelope_sender);
Robin H. Johnson8e3d4362007-04-25 19:37:17 -0700515
Matthew Wilcox61302592006-10-10 08:58:23 -0600516 if ($dry_run) {
517 # We don't want to send the email.
518 } elsif ($smtp_server =~ m#^/#) {
Eric Wongaca7ad72006-05-15 02:34:44 -0700519 my $pid = open my $sm, '|-';
520 defined $pid or die $!;
521 if (!$pid) {
Robin H. Johnson8e3d4362007-04-25 19:37:17 -0700522 exec($smtp_server, @sendmail_parameters) or die $!;
Eric Wongaca7ad72006-05-15 02:34:44 -0700523 }
524 print $sm "$header\n$message";
525 close $sm or die $?;
526 } else {
Johannes Schindelin87840622006-06-01 00:55:47 +0200527 require Net::SMTP;
Eric Wongaca7ad72006-05-15 02:34:44 -0700528 $smtp ||= Net::SMTP->new( $smtp_server );
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -0700529 $smtp->mail( $raw_from ) or die $smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -0700530 $smtp->to( @recipients ) or die $smtp->message;
531 $smtp->data or die $smtp->message;
532 $smtp->datasend("$header\n$message") or die $smtp->message;
533 $smtp->dataend() or die $smtp->message;
534 $smtp->ok or die "Failed to send $subject\n".$smtp->message;
535 }
Ryan Anderson27184352006-02-05 20:13:52 -0500536 if ($quiet) {
Robin H. Johnson71c7da92007-04-25 19:37:16 -0700537 printf (($dry_run ? "Dry-" : "")."Sent %s\n", $subject);
Ryan Anderson27184352006-02-05 20:13:52 -0500538 } else {
Robin H. Johnson71c7da92007-04-25 19:37:16 -0700539 print (($dry_run ? "Dry-" : "")."OK. Log says:\nDate: $date\n");
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -0700540 if ($smtp_server !~ m#^/#) {
Eric Wongaca7ad72006-05-15 02:34:44 -0700541 print "Server: $smtp_server\n";
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -0700542 print "MAIL FROM:<$raw_from>\n";
543 print "RCPT TO:".join(',',(map { "<$_>" } @recipients))."\n";
Eric Wongaca7ad72006-05-15 02:34:44 -0700544 } else {
Robin H. Johnson8e3d4362007-04-25 19:37:17 -0700545 print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n";
Eric Wongaca7ad72006-05-15 02:34:44 -0700546 }
547 print "From: $from\nSubject: $subject\nCc: $cc\nTo: $to\n\n";
548 if ($smtp) {
549 print "Result: ", $smtp->code, ' ',
550 ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
551 } else {
552 print "Result: OK\n";
553 }
Ryan Anderson30d08b32006-02-02 11:56:06 -0500554 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400555}
556
Ryan Anderson83b24432005-07-31 04:17:25 -0400557$reply_to = $initial_reply_to;
Junio C Hamano2186d562006-05-29 23:53:13 -0700558$references = $initial_reply_to || '';
Ryan Anderson83b24432005-07-31 04:17:25 -0400559$subject = $initial_subject;
560
561foreach my $t (@files) {
Ryan Anderson83b24432005-07-31 04:17:25 -0400562 open(F,"<",$t) or die "can't open file $t";
563
Junio C Hamano8a8e6232006-03-23 23:43:52 -0800564 my $author_not_sender = undef;
Ryan Andersonda140f82006-02-13 03:05:15 -0500565 @cc = @initial_cc;
Junio C Hamanoce91c2f2006-10-05 16:36:49 -0700566 @xh = ();
Junio C Hamanoe6b09642006-10-07 03:09:05 -0700567 my $input_format = undef;
Ryan Anderson83b24432005-07-31 04:17:25 -0400568 my $header_done = 0;
569 $message = "";
570 while(<F>) {
571 if (!$header_done) {
Junio C Hamanoe6b09642006-10-07 03:09:05 -0700572 if (/^From /) {
573 $input_format = 'mbox';
574 next;
575 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400576 chomp;
Junio C Hamanoe6b09642006-10-07 03:09:05 -0700577 if (!defined $input_format && /^[-A-Za-z]+:\s/) {
578 $input_format = 'mbox';
579 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400580
Junio C Hamanoe6b09642006-10-07 03:09:05 -0700581 if (defined $input_format && $input_format eq 'mbox') {
Ryan Anderson83b24432005-07-31 04:17:25 -0400582 if (/^Subject:\s+(.*)$/) {
583 $subject = $1;
584
585 } elsif (/^(Cc|From):\s+(.*)$/) {
Kristian Høgsberg2cf69cf2007-06-11 13:04:40 -0400586 if (unquote_rfc2047($2) eq $from) {
587 $from = $2;
Junio C Hamano8a8e6232006-03-23 23:43:52 -0800588 next if ($suppress_from);
589 }
Haavard Skinnemoen68d42c42006-08-23 03:02:59 -0700590 elsif ($1 eq 'From') {
Junio C Hamano8a8e6232006-03-23 23:43:52 -0800591 $author_not_sender = $2;
592 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400593 printf("(mbox) Adding cc: %s from line '%s'\n",
Ryan Anderson27184352006-02-05 20:13:52 -0500594 $2, $_) unless $quiet;
Ryan Anderson83b24432005-07-31 04:17:25 -0400595 push @cc, $2;
596 }
Eric Wong1d6a0032006-10-23 00:46:37 -0700597 elsif (!/^Date:\s/ && /^[-A-Za-z]+:\s+\S/) {
Junio C Hamanoce91c2f2006-10-05 16:36:49 -0700598 push @xh, $_;
599 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400600
601 } else {
602 # In the traditional
603 # "send lots of email" format,
604 # line 1 = cc
605 # line 2 = subject
606 # So let's support that, too.
Junio C Hamanoe6b09642006-10-07 03:09:05 -0700607 $input_format = 'lots';
Ryan Anderson83b24432005-07-31 04:17:25 -0400608 if (@cc == 0) {
609 printf("(non-mbox) Adding cc: %s from line '%s'\n",
Ryan Anderson27184352006-02-05 20:13:52 -0500610 $_, $_) unless $quiet;
Ryan Anderson83b24432005-07-31 04:17:25 -0400611
612 push @cc, $_;
613
614 } elsif (!defined $subject) {
615 $subject = $_;
616 }
617 }
Junio C Hamano5825e5b2005-07-31 23:05:16 -0700618
Ryan Anderson83b24432005-07-31 04:17:25 -0400619 # A whitespace line will terminate the headers
620 if (m/^\s*$/) {
621 $header_done = 1;
622 }
623 } else {
624 $message .= $_;
Adam Roben5483c712007-06-27 20:59:37 -0700625 if (/^(Signed-off-by|Cc): (.*)$/i && $signed_off_cc) {
J. Bruce Fieldsabec1002007-03-18 21:37:53 -0400626 my $c = $2;
Ryan Anderson83b24432005-07-31 04:17:25 -0400627 chomp $c;
628 push @cc, $c;
629 printf("(sob) Adding cc: %s from line '%s'\n",
Ryan Anderson27184352006-02-05 20:13:52 -0500630 $c, $_) unless $quiet;
Ryan Anderson83b24432005-07-31 04:17:25 -0400631 }
632 }
633 }
634 close F;
Junio C Hamano8a8e6232006-03-23 23:43:52 -0800635 if (defined $author_not_sender) {
Jürgen Rühle374c5902007-01-10 13:36:39 -0800636 $author_not_sender = unquote_rfc2047($author_not_sender);
Junio C Hamano8a8e6232006-03-23 23:43:52 -0800637 $message = "From: $author_not_sender\n\n$message";
638 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400639
Ryan Anderson83b24432005-07-31 04:17:25 -0400640
641 send_message();
642
643 # set up for the next message
Junio C Hamanobc108f62006-10-05 16:36:15 -0700644 if ($chain_reply_to || !defined $reply_to || length($reply_to) == 0) {
Ryan Anderson78488b22005-07-31 20:04:24 -0400645 $reply_to = $message_id;
Ryan Anderson7ccf7922006-05-29 12:30:12 -0700646 if (length $references > 0) {
YOSHIFUJI Hideaki / 吉藤英明a925b892007-04-06 08:50:24 +0900647 $references .= "\n $message_id";
Ryan Anderson7ccf7922006-05-29 12:30:12 -0700648 } else {
649 $references = "$message_id";
650 }
Ryan Anderson78488b22005-07-31 20:04:24 -0400651 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400652}
Ryan Andersone2057352005-08-02 21:45:22 -0400653
Ryan Anderson1f038a02005-09-05 01:13:07 -0400654if ($compose) {
655 cleanup_compose_files();
656}
657
658sub cleanup_compose_files() {
659 unlink($compose_filename, $compose_filename . ".final");
660
661}
662
Eric Wong4bc87a22006-03-25 17:20:48 -0800663$smtp->quit if $smtp;
Ryan Andersone2057352005-08-02 21:45:22 -0400664
665sub unique_email_list(@) {
666 my %seen;
667 my @emails;
668
669 foreach my $entry (@_) {
Eric Wongdb3106b2006-05-15 02:41:01 -0700670 if (my $clean = extract_valid_address($entry)) {
671 $seen{$clean} ||= 0;
672 next if $seen{$clean}++;
673 push @emails, $entry;
674 } else {
675 print STDERR "W: unable to extract a valid address",
676 " from: $entry\n";
677 }
Ryan Andersone2057352005-08-02 21:45:22 -0400678 }
679 return @emails;
680}