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