3 # Copyright 2002,2005 Greg Kroah-Hartman <greg@kroah.com>
4 # Copyright 2005 Ryan Anderson <ryan@michonline.com>
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.
27 my ($class, $reason) = @_;
28 return bless \
$reason, shift;
32 die "Cannot use readline on FakeTerm: $$self";
36 # most mail servers generate the Date: header, but not all...
37 sub format_2822_time
{
39 my @localtm = localtime($time);
40 my @gmttm = gmtime($time);
41 my $localmin = $localtm[1] + $localtm[2] * 60;
42 my $gmtmin = $gmttm[1] + $gmttm[2] * 60;
43 if ($localtm[0] != $gmttm[0]) {
44 die "local zone differs from GMT by a non-minute interval\n";
46 if ((($gmttm[6] + 1) % 7) == $localtm[6]) {
48 } elsif ((($gmttm[6] - 1) % 7) == $localtm[6]) {
50 } elsif ($gmttm[6] != $localtm[6]) {
51 die "local time offset greater than or equal to 24 hours\n";
53 my $offset = $localmin - $gmtmin;
54 my $offhour = $offset / 60;
55 my $offmin = abs($offset % 60);
56 if (abs($offhour) >= 24) {
57 die ("local time offset greater than or equal to 24 hours\n");
60 return sprintf("%s, %2d %s %d %02d:%02d:%02d %s%02d%02d",
61 qw(Sun Mon Tue Wed Thu Fri Sat)[$localtm[6]],
63 qw(Jan Feb Mar Apr May Jun
64 Jul Aug Sep Oct Nov Dec)[$localtm[4]],
69 ($offset >= 0) ?
'+' : '-',
75 my $have_email_valid = eval { require Email
::Valid
; 1 };
78 sub unique_email_list
(@
);
79 sub cleanup_compose_files
();
81 # Constants (essentially)
82 my $compose_filename = ".msg.$$";
84 # Variables we fill in automatically, or via prompting:
85 my (@to,@cc,@initial_cc,@bcclist,
86 $initial_reply_to,$initial_subject,@files,$from,$compose,$time);
88 # Behavior modification variables
89 my ($chain_reply_to, $quiet, $suppress_from, $no_signed_off_cc) = (1, 0, 0, 0);
93 #$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
96 new Term
::ReadLine
'git-send-email';
99 $term = new FakeTerm
"$@: going non-interactive";
102 # Begin by accumulating all the variables (defined above), that we will end up
103 # needing, first, from the command line:
105 my $rc = GetOptions
("from=s" => \
$from,
106 "in-reply-to=s" => \
$initial_reply_to,
107 "subject=s" => \
$initial_subject,
109 "cc=s" => \
@initial_cc,
110 "bcc=s" => \
@bcclist,
111 "chain-reply-to!" => \
$chain_reply_to,
112 "smtp-server=s" => \
$smtp_server,
113 "compose" => \
$compose,
115 "suppress-from" => \
$suppress_from,
116 "no-signed-off-cc|no-signed-off-by-cc" => \
$no_signed_off_cc,
119 # Verify the user input
121 foreach my $entry (@to) {
122 die "Comma in --to entry: $entry'\n" unless $entry !~ m/,/;
125 foreach my $entry (@initial_cc) {
126 die "Comma in --cc entry: $entry'\n" unless $entry !~ m/,/;
129 foreach my $entry (@bcclist) {
130 die "Comma in --bcclist entry: $entry'\n" unless $entry !~ m/,/;
133 # Now, let's fill any that aren't set in with defaults:
138 my $pid = open($fh, '-|');
139 die "$!" unless defined $pid;
141 exec('git-var', $var) or die "$!";
144 close $fh or die "$!";
151 my $val = gitvar
($name);
152 my @field = split(/\s+/, $val);
153 return join(' ', @field[0...(@field-3)]);
156 my ($author) = gitvar_ident
('GIT_AUTHOR_IDENT');
157 my ($committer) = gitvar_ident
('GIT_COMMITTER_IDENT');
160 chomp(my @alias_files = `git-repo-config --get-all sendemail.aliasesfile`);
161 chomp(my $aliasfiletype = `git-repo-config sendemail.aliasfiletype`);
163 # multiline formats can be supported in the future
164 mutt
=> sub { my $fh = shift; while (<$fh>) {
165 if (/^alias\s+(\S+)\s+(.*)$/) {
166 my ($alias, $addr) = ($1, $2);
167 $addr =~ s/#.*$//; # mutt allows # comments
168 # commas delimit multiple addresses
169 $aliases{$alias} = [ split(/\s*,\s*/, $addr) ];
171 mailrc
=> sub { my $fh = shift; while (<$fh>) {
172 if (/^alias\s+(\S+)\s+(.*)$/) {
173 # spaces delimit multiple addresses
174 $aliases{$1} = [ split(/\s+/, $2) ];
176 pine
=> sub { my $fh = shift; while (<$fh>) {
177 if (/^(\S+)\s+(.*)$/) {
178 $aliases{$1} = [ split(/\s*,\s*/, $2) ];
180 gnus
=> sub { my $fh = shift; while (<$fh>) {
181 if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
182 $aliases{$1} = [ $2 ];
186 if (@alias_files && defined $parse_alias{$aliasfiletype}) {
187 foreach my $file (@alias_files) {
188 open my $fh, '<', $file or die "opening $file: $!\n";
189 $parse_alias{$aliasfiletype}->($fh);
195 if (!defined $from) {
196 $from = $author || $committer;
198 $_ = $term->readline("Who should the emails appear to be from? ",
200 } while (!defined $_);
203 print "Emails will be sent from: ", $from, "\n";
209 $_ = $term->readline("Who should the emails be sent to? ",
211 } while (!defined $_);
213 push @to, split /,/, $to;
222 @cur = map { $aliases{$_} ? @
{$aliases{$_}} : $_ } @last;
223 } while (join(',',@cur) ne join(',',@last));
227 @to = expand_aliases
(@to);
228 @initial_cc = expand_aliases
(@initial_cc);
229 @bcclist = expand_aliases
(@bcclist);
231 if (!defined $initial_subject && $compose) {
233 $_ = $term->readline("What subject should the emails start with? ",
235 } while (!defined $_);
236 $initial_subject = $_;
240 if (!defined $initial_reply_to && $prompting) {
242 $_= $term->readline("Message-ID to be used as In-Reply-To for the first email? ",
244 } while (!defined $_);
246 $initial_reply_to = $_;
247 $initial_reply_to =~ s/(^\s+|\s+$)//g;
251 foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) {
257 $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
261 # Note that this does not need to be secure, but we will make a small
262 # effort to have it be unique
263 open(C
,">",$compose_filename)
264 or die "Failed to open for writing $compose_filename: $!";
265 print C
"From $from # This line is ignored.\n";
266 printf C
"Subject: %s\n\n", $initial_subject;
268 GIT: Please enter your email below.
269 GIT: Lines beginning in "GIT: " will be removed.
270 GIT: Consider including an overall diffstat or table of contents
271 GIT: for the patch you are writing.
276 my $editor = $ENV{EDITOR
};
277 $editor = 'vi' unless defined $editor;
278 system($editor, $compose_filename);
280 open(C2
,">",$compose_filename . ".final")
281 or die "Failed to open $compose_filename.final : " . $!;
283 open(C
,"<",$compose_filename)
284 or die "Failed to open $compose_filename : " . $!;
294 $_ = $term->readline("Send this email? (y|n) ");
295 } while (!defined $_);
297 if (uc substr($_,0,1) ne 'Y') {
298 cleanup_compose_files
();
302 @files = ($compose_filename . ".final");
306 # Now that all the defaults are set, process the rest of the command line
307 # arguments and collect up the files that need to be processed.
311 or die "Failed to opendir $f: $!";
313 push @files, grep { -f
$_ } map { +$f . "/" . $_ }
320 print STDERR
"Skipping $f - not found.\n";
326 print $_,"\n" for (@files);
330 git-send-email [options] <file | directory> [... file | directory ]
332 --from Specify the "From:" line of the email to be sent.
334 --to Specify the primary "To:" line of the email.
336 --cc Specify an initial "Cc:" list for the entire series
339 --bcc Specify a list of email addresses that should be Bcc:
342 --compose Use \$EDITOR to edit an introductory message for the
345 --subject Specify the initial "Subject:" line.
346 Only necessary if --compose is also set. If --compose
347 is not set, this will be prompted for.
349 --in-reply-to Specify the first "In-Reply-To:" header line.
350 Only used if --compose is also set. If --compose is not
351 set, this will be prompted for.
353 --chain-reply-to If set, the replies will all be to the previous
354 email sent, rather than to the first email sent.
357 --no-signed-off-cc Suppress the automatic addition of email addresses
358 that appear in a Signed-off-by: line, to the cc: list.
359 Note: Using this option is not recommended.
361 --smtp-server If set, specifies the outgoing SMTP server to use.
362 Defaults to localhost.
364 --suppress-from Suppress sending emails to yourself if your address
365 appears in a From: line.
367 --quiet Make git-send-email less verbose. One line per email should be
370 Error: Please specify a file or a directory on the command line.
375 # Variables we set as part of the loop over files
376 our ($message_id, $cc, %mail, $subject, $reply_to, $references, $message);
378 sub extract_valid_address
{
380 my $local_part_regexp = '[^<>"\s@]+';
381 my $domain_regexp = '[^.<>"\s@]+(?:\.[^.<>"\s@]+)+';
383 # check for a local address:
384 return $address if ($address =~ /^($local_part_regexp)$/);
386 if ($have_email_valid) {
387 return scalar Email
::Valid
->address($address);
389 # less robust/correct than the monster regexp in Email::Valid,
390 # but still does a 99% job, and one less dependency
391 $address =~ /($local_part_regexp\@$domain_regexp)/;
396 # Usually don't need to change anything below here.
398 # we make a "fake" message id by taking the current number
399 # of seconds since the beginning of Unix time and tacking on
400 # a random number to the end, in case we are called quicker than
401 # 1 second since the last time we were called.
403 # We'll setup a template for the message id, using the "from" address:
404 my $message_id_from = extract_valid_address
($from);
405 my $message_id_template = "<%s-git-send-email-$message_id_from>";
410 my $pseudo_rand = int (rand(4200));
411 $message_id = sprintf $message_id_template, "$date$pseudo_rand";
412 #print "new message id = $message_id\n"; # Was useful for debugging
418 $time = time - scalar $#files;
422 my @recipients = unique_email_list
(@to);
423 my $to = join (",\n\t", @recipients);
424 @recipients = unique_email_list
(@recipients,@cc,@bcclist);
425 my $date = format_2822_time
($time++);
426 my $gitversion = '@@GIT_VERSION@@';
427 if ($gitversion =~ m/..GIT_VERSION../) {
428 $gitversion = `git --version`;
430 # keep only what's after the last space
431 $gitversion =~ s/^.* //;
434 my $header = "From: $from
440 Message-Id: $message_id
441 X-Mailer: git-send-email $gitversion
445 $header .= "In-Reply-To: $reply_to\n";
446 $header .= "References: $references\n";
449 if ($smtp_server =~ m
#^/#) {
450 my $pid = open my $sm, '|-';
451 defined $pid or die $!;
453 exec($smtp_server,'-i',
454 map { extract_valid_address
($_) }
455 @recipients) or die $!;
457 print $sm "$header\n$message";
461 $smtp ||= Net
::SMTP
->new( $smtp_server );
462 $smtp->mail( $from ) or die $smtp->message;
463 $smtp->to( @recipients ) or die $smtp->message;
464 $smtp->data or die $smtp->message;
465 $smtp->datasend("$header\n$message") or die $smtp->message;
466 $smtp->dataend() or die $smtp->message;
467 $smtp->ok or die "Failed to send $subject\n".$smtp->message;
470 printf "Sent %s\n", $subject;
472 print "OK. Log says:\nDate: $date\n";
474 print "Server: $smtp_server\n";
476 print "Sendmail: $smtp_server\n";
478 print "From: $from\nSubject: $subject\nCc: $cc\nTo: $to\n\n";
480 print "Result: ", $smtp->code, ' ',
481 ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
483 print "Result: OK\n";
488 $reply_to = $initial_reply_to;
489 $references = $initial_reply_to || '';
491 $subject = $initial_subject;
493 foreach my $t (@files) {
494 open(F
,"<",$t) or die "can't open file $t";
496 my $author_not_sender = undef;
503 $found_mbox = 1, next if (/^From /);
507 if (/^Subject:\s+(.*)$/) {
510 } elsif (/^(Cc|From):\s+(.*)$/) {
512 next if ($suppress_from);
515 $author_not_sender = $2;
517 printf("(mbox) Adding cc: %s from line '%s'\n",
518 $2, $_) unless $quiet;
524 # "send lots of email" format,
527 # So let's support that, too.
529 printf("(non-mbox) Adding cc: %s from line '%s'\n",
530 $_, $_) unless $quiet;
534 } elsif (!defined $subject) {
539 # A whitespace line will terminate the headers
545 if (/^Signed-off-by: (.*)$/i && !$no_signed_off_cc) {
549 printf("(sob) Adding cc: %s from line '%s'\n",
550 $c, $_) unless $quiet;
555 if (defined $author_not_sender) {
556 $message = "From: $author_not_sender\n\n$message";
559 $cc = join(", ", unique_email_list
(@cc));
563 # set up for the next message
564 if ($chain_reply_to || length($reply_to) == 0) {
565 $reply_to = $message_id;
566 if (length $references > 0) {
567 $references .= " $message_id";
569 $references = "$message_id";
576 cleanup_compose_files
();
579 sub cleanup_compose_files
() {
580 unlink($compose_filename, $compose_filename . ".final");
584 $smtp->quit if $smtp;
586 sub unique_email_list
(@
) {
590 foreach my $entry (@_) {
591 if (my $clean = extract_valid_address
($entry)) {
593 next if $seen{$clean}++;
594 push @emails, $entry;
596 print STDERR
"W: unable to extract a valid address",