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 Git; 25 26package FakeTerm; 27sub new { 28my($class,$reason) =@_; 29returnbless \$reason,shift; 30} 31subreadline{ 32my$self=shift; 33die"Cannot use readline on FakeTerm:$$self"; 34} 35package main; 36 37 38sub usage { 39print<<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 \$EDITORto 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 67 --no-signed-off-cc Suppress the automatic addition of email addresses 68 that appear in Signed-off-by: or Cc: lines to the cc: 69 list. Note: Using this option is not recommended. 70 71 --smtp-server If set, specifies the outgoing SMTP server to use. 72 Defaults to localhost. 73 74 --suppress-from Suppress sending emails to yourself if your address 75 appears in a From: line. 76 77 --quiet Make git-send-email less verbose. One line per email 78 should be all that is output. 79 80EOT 81exit(1); 82} 83 84# most mail servers generate the Date: header, but not all... 85sub format_2822_time { 86my($time) =@_; 87my@localtm=localtime($time); 88my@gmttm=gmtime($time); 89my$localmin=$localtm[1] +$localtm[2] *60; 90my$gmtmin=$gmttm[1] +$gmttm[2] *60; 91if($localtm[0] !=$gmttm[0]) { 92die"local zone differs from GMT by a non-minute interval\n"; 93} 94if((($gmttm[6] +1) %7) ==$localtm[6]) { 95$localmin+=1440; 96}elsif((($gmttm[6] -1) %7) ==$localtm[6]) { 97$localmin-=1440; 98}elsif($gmttm[6] !=$localtm[6]) { 99die"local time offset greater than or equal to 24 hours\n"; 100} 101my$offset=$localmin-$gmtmin; 102my$offhour=$offset/60; 103my$offmin=abs($offset%60); 104if(abs($offhour) >=24) { 105die("local time offset greater than or equal to 24 hours\n"); 106} 107 108returnsprintf("%s,%2d%s%d%02d:%02d:%02d%s%02d%02d", 109qw(Sun Mon Tue Wed Thu Fri Sat)[$localtm[6]], 110$localtm[3], 111qw(Jan Feb Mar Apr May Jun 112 Jul Aug Sep Oct Nov Dec)[$localtm[4]], 113$localtm[5]+1900, 114$localtm[2], 115$localtm[1], 116$localtm[0], 117($offset>=0) ?'+':'-', 118abs($offhour), 119$offmin, 120); 121} 122 123my$have_email_valid=eval{require Email::Valid;1}; 124my$smtp; 125 126sub unique_email_list(@); 127sub cleanup_compose_files(); 128 129# Constants (essentially) 130my$compose_filename=".msg.$$"; 131 132# Variables we fill in automatically, or via prompting: 133my(@to,@cc,@initial_cc,@bcclist,@xh, 134$initial_reply_to,$initial_subject,@files,$from,$compose,$time); 135 136# Behavior modification variables 137my($chain_reply_to,$quiet,$suppress_from,$no_signed_off_cc, 138$dry_run) = (1,0,0,0,0); 139my$smtp_server; 140 141# Example reply to: 142#$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>'; 143 144my$repo= Git->repository(); 145my$term=eval{ 146 new Term::ReadLine 'git-send-email'; 147}; 148if($@) { 149$term= new FakeTerm "$@: going non-interactive"; 150} 151 152my$def_chain=$repo->config_boolean('sendemail.chainreplyto'); 153if($def_chainand$def_chaineq'false') { 154$chain_reply_to=0; 155} 156 157@bcclist=$repo->config('sendemail.bcc'); 158if(!@bcclistor!$bcclist[0]) { 159@bcclist= (); 160} 161 162# Begin by accumulating all the variables (defined above), that we will end up 163# needing, first, from the command line: 164 165my$rc= GetOptions("from=s"=> \$from, 166"in-reply-to=s"=> \$initial_reply_to, 167"subject=s"=> \$initial_subject, 168"to=s"=> \@to, 169"cc=s"=> \@initial_cc, 170"bcc=s"=> \@bcclist, 171"chain-reply-to!"=> \$chain_reply_to, 172"smtp-server=s"=> \$smtp_server, 173"compose"=> \$compose, 174"quiet"=> \$quiet, 175"suppress-from"=> \$suppress_from, 176"no-signed-off-cc|no-signed-off-by-cc"=> \$no_signed_off_cc, 177"dry-run"=> \$dry_run, 178); 179 180unless($rc) { 181 usage(); 182} 183 184# Verify the user input 185 186foreachmy$entry(@to) { 187die"Comma in --to entry:$entry'\n"unless$entry!~m/,/; 188} 189 190foreachmy$entry(@initial_cc) { 191die"Comma in --cc entry:$entry'\n"unless$entry!~m/,/; 192} 193 194foreachmy$entry(@bcclist) { 195die"Comma in --bcclist entry:$entry'\n"unless$entry!~m/,/; 196} 197 198# Now, let's fill any that aren't set in with defaults: 199 200my($author) =$repo->ident_person('author'); 201my($committer) =$repo->ident_person('committer'); 202 203my%aliases; 204my@alias_files=$repo->config('sendemail.aliasesfile'); 205my$aliasfiletype=$repo->config('sendemail.aliasfiletype'); 206my%parse_alias= ( 207# multiline formats can be supported in the future 208 mutt =>sub{my$fh=shift;while(<$fh>) { 209if(/^alias\s+(\S+)\s+(.*)$/) { 210my($alias,$addr) = ($1,$2); 211$addr=~s/#.*$//;# mutt allows # comments 212# commas delimit multiple addresses 213$aliases{$alias} = [split(/\s*,\s*/,$addr) ]; 214}}}, 215 mailrc =>sub{my$fh=shift;while(<$fh>) { 216if(/^alias\s+(\S+)\s+(.*)$/) { 217# spaces delimit multiple addresses 218$aliases{$1} = [split(/\s+/,$2) ]; 219}}}, 220 pine =>sub{my$fh=shift;while(<$fh>) { 221if(/^(\S+)\s+(.*)$/) { 222$aliases{$1} = [split(/\s*,\s*/,$2) ]; 223}}}, 224 gnus =>sub{my$fh=shift;while(<$fh>) { 225if(/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) { 226$aliases{$1} = [$2]; 227}}} 228); 229 230if(@alias_filesand$aliasfiletypeand defined$parse_alias{$aliasfiletype}) { 231foreachmy$file(@alias_files) { 232open my$fh,'<',$fileor die"opening$file:$!\n"; 233$parse_alias{$aliasfiletype}->($fh); 234close$fh; 235} 236} 237 238my$prompting=0; 239if(!defined$from) { 240$from=$author||$committer; 241do{ 242$_=$term->readline("Who should the emails appear to be from? [$from] "); 243}while(!defined$_); 244 245$from=$_if($_); 246print"Emails will be sent from: ",$from,"\n"; 247$prompting++; 248} 249 250if(!@to) { 251do{ 252$_=$term->readline("Who should the emails be sent to? ", 253""); 254}while(!defined$_); 255my$to=$_; 256push@to,split/,/,$to; 257$prompting++; 258} 259 260sub expand_aliases { 261my@cur=@_; 262my@last; 263do{ 264@last=@cur; 265@cur=map{$aliases{$_} ? @{$aliases{$_}} :$_}@last; 266}while(join(',',@cur)ne join(',',@last)); 267return@cur; 268} 269 270@to= expand_aliases(@to); 271@initial_cc= expand_aliases(@initial_cc); 272@bcclist= expand_aliases(@bcclist); 273 274if(!defined$initial_subject&&$compose) { 275do{ 276$_=$term->readline("What subject should the emails start with? ", 277$initial_subject); 278}while(!defined$_); 279$initial_subject=$_; 280$prompting++; 281} 282 283if(!defined$initial_reply_to&&$prompting) { 284do{ 285$_=$term->readline("Message-ID to be used as In-Reply-To for the first email? ", 286$initial_reply_to); 287}while(!defined$_); 288 289$initial_reply_to=$_; 290$initial_reply_to=~s/(^\s+|\s+$)//g; 291} 292 293if(!$smtp_server) { 294$smtp_server=$repo->config('sendemail.smtpserver'); 295} 296if(!$smtp_server) { 297foreach(qw( /usr/sbin/sendmail /usr/lib/sendmail )) { 298if(-x $_) { 299$smtp_server=$_; 300last; 301} 302} 303$smtp_server||='localhost';# could be 127.0.0.1, too... *shrug* 304} 305 306if($compose) { 307# Note that this does not need to be secure, but we will make a small 308# effort to have it be unique 309open(C,">",$compose_filename) 310or die"Failed to open for writing$compose_filename:$!"; 311print C "From$from# This line is ignored.\n"; 312printf C "Subject:%s\n\n",$initial_subject; 313printf C <<EOT; 314GIT: Please enter your email below. 315GIT: Lines beginning in "GIT: " will be removed. 316GIT: Consider including an overall diffstat or table of contents 317GIT: for the patch you are writing. 318 319EOT 320close(C); 321 322my$editor=$ENV{EDITOR}; 323$editor='vi'unlessdefined$editor; 324system($editor,$compose_filename); 325 326open(C2,">",$compose_filename.".final") 327or die"Failed to open$compose_filename.final : ".$!; 328 329open(C,"<",$compose_filename) 330or die"Failed to open$compose_filename: ".$!; 331 332while(<C>) { 333next ifm/^GIT: /; 334print C2 $_; 335} 336close(C); 337close(C2); 338 339do{ 340$_=$term->readline("Send this email? (y|n) "); 341}while(!defined$_); 342 343if(uc substr($_,0,1)ne'Y') { 344 cleanup_compose_files(); 345exit(0); 346} 347 348@files= ($compose_filename.".final"); 349} 350 351 352# Now that all the defaults are set, process the rest of the command line 353# arguments and collect up the files that need to be processed. 354formy$f(@ARGV) { 355if(-d $f) { 356opendir(DH,$f) 357or die"Failed to opendir$f:$!"; 358 359push@files,grep{ -f $_}map{ +$f."/".$_} 360sort readdir(DH); 361 362}elsif(-f $f) { 363push@files,$f; 364 365}else{ 366print STDERR "Skipping$f- not found.\n"; 367} 368} 369 370if(@files) { 371unless($quiet) { 372print$_,"\n"for(@files); 373} 374}else{ 375print STDERR "\nNo patch files specified!\n\n"; 376 usage(); 377} 378 379# Variables we set as part of the loop over files 380our($message_id,$cc,%mail,$subject,$reply_to,$references,$message); 381 382sub extract_valid_address { 383my$address=shift; 384my$local_part_regexp='[^<>"\s@]+'; 385my$domain_regexp='[^.<>"\s@]+(?:\.[^.<>"\s@]+)+'; 386 387# check for a local address: 388return$addressif($address=~/^($local_part_regexp)$/); 389 390if($have_email_valid) { 391returnscalar Email::Valid->address($address); 392}else{ 393# less robust/correct than the monster regexp in Email::Valid, 394# but still does a 99% job, and one less dependency 395$address=~/($local_part_regexp\@$domain_regexp)/; 396return$1; 397} 398} 399 400# Usually don't need to change anything below here. 401 402# we make a "fake" message id by taking the current number 403# of seconds since the beginning of Unix time and tacking on 404# a random number to the end, in case we are called quicker than 405# 1 second since the last time we were called. 406 407# We'll setup a template for the message id, using the "from" address: 408my$message_id_from= extract_valid_address($from); 409my$message_id_template="<%s-git-send-email-$message_id_from>"; 410 411sub make_message_id 412{ 413my$date=time; 414my$pseudo_rand=int(rand(4200)); 415$message_id=sprintf$message_id_template,"$date$pseudo_rand"; 416#print "new message id = $message_id\n"; # Was useful for debugging 417} 418 419 420 421$cc=""; 422$time=time-scalar$#files; 423 424sub unquote_rfc2047 { 425local($_) =@_; 426if(s/=\?utf-8\?q\?(.*)\?=/$1/g) { 427s/_/ /g; 428s/=([0-9A-F]{2})/chr(hex($1))/eg; 429} 430return"$_"; 431} 432 433sub send_message 434{ 435my@recipients= unique_email_list(@to); 436my$to=join(",\n\t",@recipients); 437@recipients= unique_email_list(@recipients,@cc,@bcclist); 438my$date= format_2822_time($time++); 439my$gitversion='@@GIT_VERSION@@'; 440if($gitversion=~m/..GIT_VERSION../) { 441$gitversion= Git::version(); 442} 443 444my($author_name) = ($from=~/^(.*?)\s+</); 445if($author_name&&$author_name=~/\./&&$author_name!~/^".*"$/) { 446my($name,$addr) = ($from=~/^(.*?)(\s+<.*)/); 447$from="\"$name\"$addr"; 448} 449my$header="From:$from 450To:$to 451Cc:$cc 452Subject:$subject 453Date:$date 454Message-Id:$message_id 455X-Mailer: git-send-email$gitversion 456"; 457if($reply_to) { 458 459$header.="In-Reply-To:$reply_to\n"; 460$header.="References:$references\n"; 461} 462if(@xh) { 463$header.=join("\n",@xh) ."\n"; 464} 465 466if($dry_run) { 467# We don't want to send the email. 468}elsif($smtp_server=~ m#^/#) { 469my$pid=open my$sm,'|-'; 470defined$pidor die$!; 471if(!$pid) { 472exec($smtp_server,'-i', 473map{ extract_valid_address($_) } 474@recipients)or die$!; 475} 476print$sm"$header\n$message"; 477close$smor die$?; 478}else{ 479require Net::SMTP; 480$smtp||= Net::SMTP->new($smtp_server); 481$smtp->mail($from)or die$smtp->message; 482$smtp->to(@recipients)or die$smtp->message; 483$smtp->dataor die$smtp->message; 484$smtp->datasend("$header\n$message")or die$smtp->message; 485$smtp->dataend()or die$smtp->message; 486$smtp->okor die"Failed to send$subject\n".$smtp->message; 487} 488if($quiet) { 489printf"Sent%s\n",$subject; 490}else{ 491print"OK. Log says:\nDate:$date\n"; 492if($smtp) { 493print"Server:$smtp_server\n"; 494}else{ 495print"Sendmail:$smtp_server\n"; 496} 497print"From:$from\nSubject:$subject\nCc:$cc\nTo:$to\n\n"; 498if($smtp) { 499print"Result: ",$smtp->code,' ', 500($smtp->message=~/\n([^\n]+\n)$/s),"\n"; 501}else{ 502print"Result: OK\n"; 503} 504} 505} 506 507$reply_to=$initial_reply_to; 508$references=$initial_reply_to||''; 509make_message_id(); 510$subject=$initial_subject; 511 512foreachmy$t(@files) { 513open(F,"<",$t)or die"can't open file$t"; 514 515my$author_not_sender=undef; 516@cc=@initial_cc; 517@xh= (); 518my$input_format=undef; 519my$header_done=0; 520$message=""; 521while(<F>) { 522if(!$header_done) { 523if(/^From /) { 524$input_format='mbox'; 525next; 526} 527chomp; 528if(!defined$input_format&&/^[-A-Za-z]+:\s/) { 529$input_format='mbox'; 530} 531 532if(defined$input_format&&$input_formateq'mbox') { 533if(/^Subject:\s+(.*)$/) { 534$subject=$1; 535 536}elsif(/^(Cc|From):\s+(.*)$/) { 537if($2eq$from) { 538next if($suppress_from); 539} 540elsif($1eq'From') { 541$author_not_sender=$2; 542} 543printf("(mbox) Adding cc:%sfrom line '%s'\n", 544$2,$_)unless$quiet; 545push@cc,$2; 546} 547elsif(!/^Date:\s/&&/^[-A-Za-z]+:\s+\S/) { 548push@xh,$_; 549} 550 551}else{ 552# In the traditional 553# "send lots of email" format, 554# line 1 = cc 555# line 2 = subject 556# So let's support that, too. 557$input_format='lots'; 558if(@cc==0) { 559printf("(non-mbox) Adding cc:%sfrom line '%s'\n", 560$_,$_)unless$quiet; 561 562push@cc,$_; 563 564}elsif(!defined$subject) { 565$subject=$_; 566} 567} 568 569# A whitespace line will terminate the headers 570if(m/^\s*$/) { 571$header_done=1; 572} 573}else{ 574$message.=$_; 575if(/^(Signed-off-by|Cc): (.*)$/i&& !$no_signed_off_cc) { 576my$c=$2; 577chomp$c; 578push@cc,$c; 579printf("(sob) Adding cc:%sfrom line '%s'\n", 580$c,$_)unless$quiet; 581} 582} 583} 584close F; 585if(defined$author_not_sender) { 586$author_not_sender= unquote_rfc2047($author_not_sender); 587$message="From:$author_not_sender\n\n$message"; 588} 589 590$cc=join(", ", unique_email_list(@cc)); 591 592 send_message(); 593 594# set up for the next message 595if($chain_reply_to|| !defined$reply_to||length($reply_to) ==0) { 596$reply_to=$message_id; 597if(length$references>0) { 598$references.="\n$message_id"; 599}else{ 600$references="$message_id"; 601} 602} 603 make_message_id(); 604} 605 606if($compose) { 607 cleanup_compose_files(); 608} 609 610sub cleanup_compose_files() { 611unlink($compose_filename,$compose_filename.".final"); 612 613} 614 615$smtp->quitif$smtp; 616 617sub unique_email_list(@) { 618my%seen; 619my@emails; 620 621foreachmy$entry(@_) { 622if(my$clean= extract_valid_address($entry)) { 623$seen{$clean} ||=0; 624next if$seen{$clean}++; 625push@emails,$entry; 626}else{ 627print STDERR "W: unable to extract a valid address", 628" from:$entry\n"; 629} 630} 631return@emails; 632}