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# 18 19use strict; 20use warnings; 21use Term::ReadLine; 22use Getopt::Long; 23use Data::Dumper; 24 25# most mail servers generate the Date: header, but not all... 26$ENV{LC_ALL} ='C'; 27use POSIX qw/strftime/; 28 29my$have_email_valid=eval{require Email::Valid;1}; 30my$smtp; 31 32sub unique_email_list(@); 33sub cleanup_compose_files(); 34 35# Constants (essentially) 36my$compose_filename=".msg.$$"; 37 38# Variables we fill in automatically, or via prompting: 39my(@to,@cc,@initial_cc,@bcclist, 40$initial_reply_to,$initial_subject,@files,$from,$compose,$time); 41 42# Behavior modification variables 43my($chain_reply_to,$quiet,$suppress_from,$no_signed_off_cc) = (1,0,0,0); 44my$smtp_server; 45 46# Example reply to: 47#$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>'; 48 49my$term= new Term::ReadLine 'git-send-email'; 50 51# Begin by accumulating all the variables (defined above), that we will end up 52# needing, first, from the command line: 53 54my$rc= GetOptions("from=s"=> \$from, 55"in-reply-to=s"=> \$initial_reply_to, 56"subject=s"=> \$initial_subject, 57"to=s"=> \@to, 58"cc=s"=> \@initial_cc, 59"bcc=s"=> \@bcclist, 60"chain-reply-to!"=> \$chain_reply_to, 61"smtp-server=s"=> \$smtp_server, 62"compose"=> \$compose, 63"quiet"=> \$quiet, 64"suppress-from"=> \$suppress_from, 65"no-signed-off-cc|no-signed-off-by-cc"=> \$no_signed_off_cc, 66); 67 68# Now, let's fill any that aren't set in with defaults: 69 70sub gitvar { 71my($var) =@_; 72my$fh; 73my$pid=open($fh,'-|'); 74die"$!"unlessdefined$pid; 75if(!$pid) { 76exec('git-var',$var)or die"$!"; 77} 78my($val) = <$fh>; 79close$fhor die"$!"; 80chomp($val); 81return$val; 82} 83 84sub gitvar_ident { 85my($name) =@_; 86my$val= gitvar($name); 87my@field=split(/\s+/,$val); 88returnjoin(' ',@field[0...(@field-3)]); 89} 90 91my($author) = gitvar_ident('GIT_AUTHOR_IDENT'); 92my($committer) = gitvar_ident('GIT_COMMITTER_IDENT'); 93 94my%aliases; 95chomp(my@alias_files=`git-repo-config --get-all sendemail.aliasesfile`); 96chomp(my$aliasfiletype=`git-repo-config sendemail.aliasfiletype`); 97my%parse_alias= ( 98# multiline formats can be supported in the future 99 mutt =>sub{my$fh=shift;while(<$fh>) { 100if(/^alias\s+(\S+)\s+(.*)$/) { 101my($alias,$addr) = ($1,$2); 102$addr=~s/#.*$//;# mutt allows # comments 103# commas delimit multiple addresses 104$aliases{$alias} = [split(/\s*,\s*/,$addr) ]; 105}}}, 106 mailrc =>sub{my$fh=shift;while(<$fh>) { 107if(/^alias\s+(\S+)\s+(.*)$/) { 108# spaces delimit multiple addresses 109$aliases{$1} = [split(/\s+/,$2) ]; 110}}}, 111 pine =>sub{my$fh=shift;while(<$fh>) { 112if(/^(\S+)\s+(.*)$/) { 113$aliases{$1} = [split(/\s*,\s*/,$2) ]; 114}}}, 115 gnus =>sub{my$fh=shift;while(<$fh>) { 116if(/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) { 117$aliases{$1} = [$2]; 118}}} 119); 120 121if(@alias_files&&defined$parse_alias{$aliasfiletype}) { 122foreachmy$file(@alias_files) { 123open my$fh,'<',$fileor die"opening$file:$!\n"; 124$parse_alias{$aliasfiletype}->($fh); 125close$fh; 126} 127} 128 129my$prompting=0; 130if(!defined$from) { 131$from=$author||$committer; 132do{ 133$_=$term->readline("Who should the emails appear to be from? ", 134$from); 135}while(!defined$_); 136 137$from=$_; 138print"Emails will be sent from: ",$from,"\n"; 139$prompting++; 140} 141 142if(!@to) { 143do{ 144$_=$term->readline("Who should the emails be sent to? ", 145""); 146}while(!defined$_); 147my$to=$_; 148push@to,split/,/,$to; 149$prompting++; 150} 151 152sub expand_aliases { 153my@cur=@_; 154my@last; 155do{ 156@last=@cur; 157@cur=map{$aliases{$_} ? @{$aliases{$_}} :$_}@last; 158}while(join(',',@cur)ne join(',',@last)); 159return@cur; 160} 161 162@to= expand_aliases(@to); 163@initial_cc= expand_aliases(@initial_cc); 164@bcclist= expand_aliases(@bcclist); 165 166if(!defined$initial_subject&&$compose) { 167do{ 168$_=$term->readline("What subject should the emails start with? ", 169$initial_subject); 170}while(!defined$_); 171$initial_subject=$_; 172$prompting++; 173} 174 175if(!defined$initial_reply_to&&$prompting) { 176do{ 177$_=$term->readline("Message-ID to be used as In-Reply-To for the first email? ", 178$initial_reply_to); 179}while(!defined$_); 180 181$initial_reply_to=$_; 182$initial_reply_to=~s/(^\s+|\s+$)//g; 183} 184 185if(!$smtp_server) { 186foreach(qw( /usr/sbin/sendmail /usr/lib/sendmail )) { 187if(-x $_) { 188$smtp_server=$_; 189last; 190} 191} 192$smtp_server||='localhost';# could be 127.0.0.1, too... *shrug* 193} 194 195if($compose) { 196# Note that this does not need to be secure, but we will make a small 197# effort to have it be unique 198open(C,">",$compose_filename) 199or die"Failed to open for writing$compose_filename:$!"; 200print C "From$from# This line is ignored.\n"; 201printf C "Subject:%s\n\n",$initial_subject; 202printf C <<EOT; 203GIT: Please enter your email below. 204GIT: Lines beginning in "GIT: " will be removed. 205GIT: Consider including an overall diffstat or table of contents 206GIT: for the patch you are writing. 207 208EOT 209close(C); 210 211my$editor=$ENV{EDITOR}; 212$editor='vi'unlessdefined$editor; 213system($editor,$compose_filename); 214 215open(C2,">",$compose_filename.".final") 216or die"Failed to open$compose_filename.final : ".$!; 217 218open(C,"<",$compose_filename) 219or die"Failed to open$compose_filename: ".$!; 220 221while(<C>) { 222next ifm/^GIT: /; 223print C2 $_; 224} 225close(C); 226close(C2); 227 228do{ 229$_=$term->readline("Send this email? (y|n) "); 230}while(!defined$_); 231 232if(uc substr($_,0,1)ne'Y') { 233 cleanup_compose_files(); 234exit(0); 235} 236 237@files= ($compose_filename.".final"); 238} 239 240 241# Now that all the defaults are set, process the rest of the command line 242# arguments and collect up the files that need to be processed. 243formy$f(@ARGV) { 244if(-d $f) { 245opendir(DH,$f) 246or die"Failed to opendir$f:$!"; 247 248push@files,grep{ -f $_}map{ +$f."/".$_} 249sort readdir(DH); 250 251}elsif(-f $f) { 252push@files,$f; 253 254}else{ 255print STDERR "Skipping$f- not found.\n"; 256} 257} 258 259if(@files) { 260unless($quiet) { 261print$_,"\n"for(@files); 262} 263}else{ 264print<<EOT; 265git-send-email [options] <file | directory> [... file | directory ] 266Options: 267 --from Specify the "From:" line of the email to be sent. 268 269 --to Specify the primary "To:" line of the email. 270 271 --cc Specify an initial "Cc:" list for the entire series 272 of emails. 273 274 --bcc Specify a list of email addresses that should be Bcc: 275 on all the emails. 276 277 --compose Use \$EDITORto edit an introductory message for the 278 patch series. 279 280 --subject Specify the initial "Subject:" line. 281 Only necessary if --compose is also set. If --compose 282 is not set, this will be prompted for. 283 284 --in-reply-to Specify the first "In-Reply-To:" header line. 285 Only used if --compose is also set. If --compose is not 286 set, this will be prompted for. 287 288 --chain-reply-to If set, the replies will all be to the previous 289 email sent, rather than to the first email sent. 290 Defaults to on. 291 292 --no-signed-off-cc Suppress the automatic addition of email addresses 293 that appear in a Signed-off-by: line, to the cc: list. 294 Note: Using this option is not recommended. 295 296 --smtp-server If set, specifies the outgoing SMTP server to use. 297 Defaults to localhost. 298 299 --suppress-from Supress sending emails to yourself if your address 300 appears in a From: line. 301 302 --quiet Make git-send-email less verbose. One line per email should be 303 all that is output. 304 305Error: Please specify a file or a directory on the command line. 306EOT 307exit(1); 308} 309 310# Variables we set as part of the loop over files 311our($message_id,$cc,%mail,$subject,$reply_to,$references,$message); 312 313sub extract_valid_address { 314my$address=shift; 315 316# check for a local address: 317return$addressif($address=~/^([\w\-.]+)$/); 318 319if($have_email_valid) { 320return Email::Valid->address($address); 321}else{ 322# less robust/correct than the monster regexp in Email::Valid, 323# but still does a 99% job, and one less dependency 324$address=~/([\w\-.]+@[\w\-.]+)/; 325return$1; 326} 327} 328 329# Usually don't need to change anything below here. 330 331# we make a "fake" message id by taking the current number 332# of seconds since the beginning of Unix time and tacking on 333# a random number to the end, in case we are called quicker than 334# 1 second since the last time we were called. 335 336# We'll setup a template for the message id, using the "from" address: 337my$message_id_from= extract_valid_address($from); 338my$message_id_template="<%s-git-send-email-$message_id_from>"; 339 340sub make_message_id 341{ 342my$date=time; 343my$pseudo_rand=int(rand(4200)); 344$message_id=sprintf$message_id_template,"$date$pseudo_rand"; 345#print "new message id = $message_id\n"; # Was useful for debugging 346} 347 348 349 350$cc=""; 351$time=time-scalar$#files; 352 353sub send_message 354{ 355my@recipients= unique_email_list(@to); 356my$to=join(",\n\t",@recipients); 357@recipients= unique_email_list(@recipients,@cc,@bcclist); 358my$date= strftime('%a,%d%b%Y%H:%M:%S%z',localtime($time++)); 359my$gitversion='@@GIT_VERSION@@'; 360if($gitversion=~m/..GIT_VERSION../) { 361$gitversion=`git --version`; 362chomp$gitversion; 363# keep only what's after the last space 364$gitversion=~s/^.* //; 365} 366 367my$header="From:$from 368To:$to 369Cc:$cc 370Subject:$subject 371Reply-To:$from 372Date:$date 373Message-Id:$message_id 374X-Mailer: git-send-email$gitversion 375"; 376if($reply_to) { 377 378$header.="In-Reply-To:$reply_to\n"; 379$header.="References:$references\n"; 380} 381 382if($smtp_server=~ m#^/#) { 383my$pid=open my$sm,'|-'; 384defined$pidor die$!; 385if(!$pid) { 386exec($smtp_server,'-i', 387map{scalar extract_valid_address($_) } 388@recipients)or die$!; 389} 390print$sm"$header\n$message"; 391close$smor die$?; 392}else{ 393require Net::SMTP; 394$smtp||= Net::SMTP->new($smtp_server); 395$smtp->mail($from)or die$smtp->message; 396$smtp->to(@recipients)or die$smtp->message; 397$smtp->dataor die$smtp->message; 398$smtp->datasend("$header\n$message")or die$smtp->message; 399$smtp->dataend()or die$smtp->message; 400$smtp->okor die"Failed to send$subject\n".$smtp->message; 401} 402if($quiet) { 403printf"Sent%s\n",$subject; 404}else{ 405print"OK. Log says:\nDate:$date\n"; 406if($smtp) { 407print"Server:$smtp_server\n"; 408}else{ 409print"Sendmail:$smtp_server\n"; 410} 411print"From:$from\nSubject:$subject\nCc:$cc\nTo:$to\n\n"; 412if($smtp) { 413print"Result: ",$smtp->code,' ', 414($smtp->message=~/\n([^\n]+\n)$/s),"\n"; 415}else{ 416print"Result: OK\n"; 417} 418} 419} 420 421$reply_to=$initial_reply_to; 422$references=$initial_reply_to||''; 423make_message_id(); 424$subject=$initial_subject; 425 426foreachmy$t(@files) { 427open(F,"<",$t)or die"can't open file$t"; 428 429my$author_not_sender=undef; 430@cc=@initial_cc; 431my$found_mbox=0; 432my$header_done=0; 433$message=""; 434while(<F>) { 435if(!$header_done) { 436$found_mbox=1,next if(/^From /); 437chomp; 438 439if($found_mbox) { 440if(/^Subject:\s+(.*)$/) { 441$subject=$1; 442 443}elsif(/^(Cc|From):\s+(.*)$/) { 444if($2eq$from) { 445next if($suppress_from); 446} 447else{ 448$author_not_sender=$2; 449} 450printf("(mbox) Adding cc:%sfrom line '%s'\n", 451$2,$_)unless$quiet; 452push@cc,$2; 453} 454 455}else{ 456# In the traditional 457# "send lots of email" format, 458# line 1 = cc 459# line 2 = subject 460# So let's support that, too. 461if(@cc==0) { 462printf("(non-mbox) Adding cc:%sfrom line '%s'\n", 463$_,$_)unless$quiet; 464 465push@cc,$_; 466 467}elsif(!defined$subject) { 468$subject=$_; 469} 470} 471 472# A whitespace line will terminate the headers 473if(m/^\s*$/) { 474$header_done=1; 475} 476}else{ 477$message.=$_; 478if(/^Signed-off-by: (.*)$/i&& !$no_signed_off_cc) { 479my$c=$1; 480chomp$c; 481push@cc,$c; 482printf("(sob) Adding cc:%sfrom line '%s'\n", 483$c,$_)unless$quiet; 484} 485} 486} 487close F; 488if(defined$author_not_sender) { 489$message="From:$author_not_sender\n\n$message"; 490} 491 492$cc=join(", ", unique_email_list(@cc)); 493 494 send_message(); 495 496# set up for the next message 497if($chain_reply_to||length($reply_to) ==0) { 498$reply_to=$message_id; 499if(length$references>0) { 500$references.="$message_id"; 501}else{ 502$references="$message_id"; 503} 504} 505 make_message_id(); 506} 507 508if($compose) { 509 cleanup_compose_files(); 510} 511 512sub cleanup_compose_files() { 513unlink($compose_filename,$compose_filename.".final"); 514 515} 516 517$smtp->quitif$smtp; 518 519sub unique_email_list(@) { 520my%seen; 521my@emails; 522 523foreachmy$entry(@_) { 524if(my$clean= extract_valid_address($entry)) { 525$seen{$clean} ||=0; 526next if$seen{$clean}++; 527push@emails,$entry; 528}else{ 529print STDERR "W: unable to extract a valid address", 530" from:$entry\n"; 531} 532} 533return@emails; 534}