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 --signed-off-cc Automatically add email addresses that appear in 68 Signed-off-by: or Cc: lines to the cc: list. Defaults to on. 69 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 74 appears in a From: line. Defaults to off. 75 76 --thread Specify that the "In-Reply-To:" header should be set on all 77 emails. Defaults to on. 78 79 --quiet Make git-send-email less verbose. One line per email 80 should be all that is output. 81 82 --dry-run Do everything except actually send the emails. 83 84 --envelope-sender Specify the envelope sender used to send the emails. 85 86EOT 87exit(1); 88} 89 90# most mail servers generate the Date: header, but not all... 91sub format_2822_time { 92my($time) =@_; 93my@localtm=localtime($time); 94my@gmttm=gmtime($time); 95my$localmin=$localtm[1] +$localtm[2] *60; 96my$gmtmin=$gmttm[1] +$gmttm[2] *60; 97if($localtm[0] !=$gmttm[0]) { 98die"local zone differs from GMT by a non-minute interval\n"; 99} 100if((($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]) { 105die"local time offset greater than or equal to 24 hours\n"; 106} 107my$offset=$localmin-$gmtmin; 108my$offhour=$offset/60; 109my$offmin=abs($offset%60); 110if(abs($offhour) >=24) { 111die("local time offset greater than or equal to 24 hours\n"); 112} 113 114returnsprintf("%s,%2d%s%d%02d:%02d:%02d%s%02d%02d", 115qw(Sun Mon Tue Wed Thu Fri Sat)[$localtm[6]], 116$localtm[3], 117qw(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) ?'+':'-', 124abs($offhour), 125$offmin, 126); 127} 128 129my$have_email_valid=eval{require Email::Valid;1}; 130my$smtp; 131 132sub unique_email_list(@); 133sub cleanup_compose_files(); 134 135# Constants (essentially) 136my$compose_filename=".msg.$$"; 137 138# Variables we fill in automatically, or via prompting: 139my(@to,@cc,@initial_cc,@bcclist,@xh, 140$initial_reply_to,$initial_subject,@files,$from,$compose,$time); 141 142my$smtp_server; 143my$envelope_sender; 144 145# Example reply to: 146#$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>'; 147 148my$repo= Git->repository(); 149my$term=eval{ 150 new Term::ReadLine 'git-send-email'; 151}; 152if($@) { 153$term= new FakeTerm "$@: going non-interactive"; 154} 155 156# 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 162my%config_settings= ( 163"thread"=> [\$thread,1], 164"chainreplyto"=> [\$chain_reply_to,1], 165"suppressfrom"=> [\$suppress_from,0], 166"signedoffcc"=> [\$signed_off_cc,1], 167); 168 169foreachmy$setting(keys%config_settings) { 170my$config=$repo->config_bool("sendemail.$setting"); 171${$config_settings{$setting}->[0]} = (defined$config) ?$config:$config_settings{$setting}->[1]; 172} 173 174@bcclist=$repo->config('sendemail.bcc'); 175if(!@bcclistor!$bcclist[0]) { 176@bcclist= (); 177} 178 179# 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, 186"cc=s"=> \@initial_cc, 187"bcc=s"=> \@bcclist, 188"chain-reply-to!"=> \$chain_reply_to, 189"smtp-server=s"=> \$smtp_server, 190"compose"=> \$compose, 191"quiet"=> \$quiet, 192"suppress-from!"=> \$suppress_from, 193"signed-off-cc|signed-off-by-cc!"=> \$signed_off_cc, 194"dry-run"=> \$dry_run, 195"envelope-sender=s"=> \$envelope_sender, 196"thread!"=> \$thread, 197); 198 199unless($rc) { 200 usage(); 201} 202 203# Verify the user input 204 205foreachmy$entry(@to) { 206die"Comma in --to entry:$entry'\n"unless$entry!~m/,/; 207} 208 209foreachmy$entry(@initial_cc) { 210die"Comma in --cc entry:$entry'\n"unless$entry!~m/,/; 211} 212 213foreachmy$entry(@bcclist) { 214die"Comma in --bcclist entry:$entry'\n"unless$entry!~m/,/; 215} 216 217# Now, let's fill any that aren't set in with defaults: 218 219my($author) =$repo->ident_person('author'); 220my($committer) =$repo->ident_person('committer'); 221 222my%aliases; 223my@alias_files=$repo->config('sendemail.aliasesfile'); 224my$aliasfiletype=$repo->config('sendemail.aliasfiletype'); 225my%parse_alias= ( 226# multiline formats can be supported in the future 227 mutt =>sub{my$fh=shift;while(<$fh>) { 228if(/^\s*alias\s+(\S+)\s+(.*)$/) { 229my($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>) { 235if(/^alias\s+(\S+)\s+(.*)$/) { 236# spaces delimit multiple addresses 237$aliases{$1} = [split(/\s+/,$2) ]; 238}}}, 239 pine =>sub{my$fh=shift;while(<$fh>) { 240if(/^(\S+)\s+(.*)$/) { 241$aliases{$1} = [split(/\s*,\s*/,$2) ]; 242}}}, 243 gnus =>sub{my$fh=shift;while(<$fh>) { 244if(/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) { 245$aliases{$1} = [$2]; 246}}} 247); 248 249if(@alias_filesand$aliasfiletypeand defined$parse_alias{$aliasfiletype}) { 250foreachmy$file(@alias_files) { 251open my$fh,'<',$fileor die"opening$file:$!\n"; 252$parse_alias{$aliasfiletype}->($fh); 253close$fh; 254} 255} 256 257my$prompting=0; 258if(!defined$from) { 259$from=$author||$committer; 260do{ 261$_=$term->readline("Who should the emails appear to be from? [$from] "); 262}while(!defined$_); 263 264$from=$_if($_); 265print"Emails will be sent from: ",$from,"\n"; 266$prompting++; 267} 268 269if(!@to) { 270do{ 271$_=$term->readline("Who should the emails be sent to? ", 272""); 273}while(!defined$_); 274my$to=$_; 275push@to,split/,/,$to; 276$prompting++; 277} 278 279sub expand_aliases { 280my@cur=@_; 281my@last; 282do{ 283@last=@cur; 284@cur=map{$aliases{$_} ? @{$aliases{$_}} :$_}@last; 285}while(join(',',@cur)ne join(',',@last)); 286return@cur; 287} 288 289@to= expand_aliases(@to); 290@to= (map{ sanitize_address_rfc822($_) }@to); 291@initial_cc= expand_aliases(@initial_cc); 292@bcclist= expand_aliases(@bcclist); 293 294if(!defined$initial_subject&&$compose) { 295do{ 296$_=$term->readline("What subject should the emails start with? ", 297$initial_subject); 298}while(!defined$_); 299$initial_subject=$_; 300$prompting++; 301} 302 303if($thread&& !defined$initial_reply_to&&$prompting) { 304do{ 305$_=$term->readline("Message-ID to be used as In-Reply-To for the first email? ", 306$initial_reply_to); 307}while(!defined$_); 308 309$initial_reply_to=$_; 310$initial_reply_to=~s/(^\s+|\s+$)//g; 311} 312 313if(!$smtp_server) { 314$smtp_server=$repo->config('sendemail.smtpserver'); 315} 316if(!$smtp_server) { 317foreach(qw( /usr/sbin/sendmail /usr/lib/sendmail )) { 318if(-x $_) { 319$smtp_server=$_; 320last; 321} 322} 323$smtp_server||='localhost';# could be 127.0.0.1, too... *shrug* 324} 325 326if($compose) { 327# Note that this does not need to be secure, but we will make a small 328# effort to have it be unique 329open(C,">",$compose_filename) 330or die"Failed to open for writing$compose_filename:$!"; 331print C "From$from# This line is ignored.\n"; 332printf C "Subject:%s\n\n",$initial_subject; 333printf 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 340close(C); 341 342my$editor=$ENV{EDITOR}; 343$editor='vi'unlessdefined$editor; 344system($editor,$compose_filename); 345 346open(C2,">",$compose_filename.".final") 347or die"Failed to open$compose_filename.final : ".$!; 348 349open(C,"<",$compose_filename) 350or die"Failed to open$compose_filename: ".$!; 351 352while(<C>) { 353next ifm/^GIT: /; 354print C2 $_; 355} 356close(C); 357close(C2); 358 359do{ 360$_=$term->readline("Send this email? (y|n) "); 361}while(!defined$_); 362 363if(uc substr($_,0,1)ne'Y') { 364 cleanup_compose_files(); 365exit(0); 366} 367 368@files= ($compose_filename.".final"); 369} 370 371 372# 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. 374formy$f(@ARGV) { 375if(-d $f) { 376opendir(DH,$f) 377or die"Failed to opendir$f:$!"; 378 379push@files,grep{ -f $_}map{ +$f."/".$_} 380sort readdir(DH); 381 382}elsif(-f $f) { 383push@files,$f; 384 385}else{ 386print STDERR "Skipping$f- not found.\n"; 387} 388} 389 390if(@files) { 391unless($quiet) { 392print$_,"\n"for(@files); 393} 394}else{ 395print STDERR "\nNo patch files specified!\n\n"; 396 usage(); 397} 398 399# Variables we set as part of the loop over files 400our($message_id,%mail,$subject,$reply_to,$references,$message); 401 402sub extract_valid_address { 403my$address=shift; 404my$local_part_regexp='[^<>"\s@]+'; 405my$domain_regexp='[^.<>"\s@]+(?:\.[^.<>"\s@]+)+'; 406 407# check for a local address: 408return$addressif($address=~/^($local_part_regexp)$/); 409 410if($have_email_valid) { 411returnscalar Email::Valid->address($address); 412}else{ 413# less robust/correct than the monster regexp in Email::Valid, 414# but still does a 99% job, and one less dependency 415$address=~/($local_part_regexp\@$domain_regexp)/; 416return$1; 417} 418} 419 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. 426 427# We'll setup a template for the message id, using the "from" address: 428 429sub make_message_id 430{ 431my$date=time; 432my$pseudo_rand=int(rand(4200)); 433my$du_part; 434for($from,$committer,$author) { 435$du_part= extract_valid_address($_); 436last if($du_partne''); 437} 438if($du_parteq'') { 439use Sys::Hostname qw(); 440$du_part='user@'. Sys::Hostname::hostname(); 441} 442my$message_id_template="<%s-git-send-email-$du_part>"; 443$message_id=sprintf$message_id_template,"$date$pseudo_rand"; 444#print "new message id = $message_id\n"; # Was useful for debugging 445} 446 447 448 449$time=time-scalar$#files; 450 451sub unquote_rfc2047 { 452local($_) =@_; 453if(s/=\?utf-8\?q\?(.*)\?=/$1/g) { 454s/_/ /g; 455s/=([0-9A-F]{2})/chr(hex($1))/eg; 456} 457return"$_"; 458} 459 460# If an address contains a . in the name portion, the name must be quoted. 461sub sanitize_address_rfc822 462{ 463my($recipient) =@_; 464my($recipient_name) = ($recipient=~/^(.*?)\s+</); 465if($recipient_name&&$recipient_name=~/\./&&$recipient_name!~/^".*"$/) { 466my($name,$addr) = ($recipient=~/^(.*?)(\s+<.*)/); 467$recipient="\"$name\"$addr"; 468} 469return$recipient; 470} 471 472sub send_message 473{ 474my@recipients= unique_email_list(@to); 475@cc= (map{ sanitize_address_rfc822($_) }@cc); 476my$to=join(",\n\t",@recipients); 477@recipients= unique_email_list(@recipients,@cc,@bcclist); 478@recipients= (map{ extract_valid_address($_) }@recipients); 479my$date= format_2822_time($time++); 480my$gitversion='@@GIT_VERSION@@'; 481if($gitversion=~m/..GIT_VERSION../) { 482$gitversion= Git::version(); 483} 484 485my$cc=join(", ", unique_email_list(@cc)); 486my$ccline=""; 487if($ccne'') { 488$ccline="\nCc:$cc"; 489} 490$from= sanitize_address_rfc822($from); 491 make_message_id(); 492 493my$header="From:$from 494To:$to${ccline} 495Subject:$subject 496Date:$date 497Message-Id:$message_id 498X-Mailer: git-send-email$gitversion 499"; 500if($thread&&$reply_to) { 501 502$header.="In-Reply-To:$reply_to\n"; 503$header.="References:$references\n"; 504} 505if(@xh) { 506$header.=join("\n",@xh) ."\n"; 507} 508 509my@sendmail_parameters= ('-i',@recipients); 510my$raw_from=$from; 511$raw_from=$envelope_senderif(defined$envelope_sender); 512$raw_from= extract_valid_address($raw_from); 513unshift(@sendmail_parameters, 514'-f',$raw_from)if(defined$envelope_sender); 515 516if($dry_run) { 517# We don't want to send the email. 518}elsif($smtp_server=~ m#^/#) { 519my$pid=open my$sm,'|-'; 520defined$pidor die$!; 521if(!$pid) { 522exec($smtp_server,@sendmail_parameters)or die$!; 523} 524print$sm"$header\n$message"; 525close$smor die$?; 526}else{ 527require Net::SMTP; 528$smtp||= Net::SMTP->new($smtp_server); 529$smtp->mail($raw_from)or die$smtp->message; 530$smtp->to(@recipients)or die$smtp->message; 531$smtp->dataor die$smtp->message; 532$smtp->datasend("$header\n$message")or die$smtp->message; 533$smtp->dataend()or die$smtp->message; 534$smtp->okor die"Failed to send$subject\n".$smtp->message; 535} 536if($quiet) { 537printf(($dry_run?"Dry-":"")."Sent%s\n",$subject); 538}else{ 539print(($dry_run?"Dry-":"")."OK. Log says:\nDate:$date\n"); 540if($smtp_server!~ m#^/#) { 541print"Server:$smtp_server\n"; 542print"MAIL FROM:<$raw_from>\n"; 543print"RCPT TO:".join(',',(map{"<$_>"}@recipients))."\n"; 544}else{ 545print"Sendmail:$smtp_server".join(' ',@sendmail_parameters)."\n"; 546} 547print"From:$from\nSubject:$subject\nCc:$cc\nTo:$to\n\n"; 548if($smtp) { 549print"Result: ",$smtp->code,' ', 550($smtp->message=~/\n([^\n]+\n)$/s),"\n"; 551}else{ 552print"Result: OK\n"; 553} 554} 555} 556 557$reply_to=$initial_reply_to; 558$references=$initial_reply_to||''; 559$subject=$initial_subject; 560 561foreachmy$t(@files) { 562open(F,"<",$t)or die"can't open file$t"; 563 564my$author_not_sender=undef; 565@cc=@initial_cc; 566@xh= (); 567my$input_format=undef; 568my$header_done=0; 569$message=""; 570while(<F>) { 571if(!$header_done) { 572if(/^From /) { 573$input_format='mbox'; 574next; 575} 576chomp; 577if(!defined$input_format&&/^[-A-Za-z]+:\s/) { 578$input_format='mbox'; 579} 580 581if(defined$input_format&&$input_formateq'mbox') { 582if(/^Subject:\s+(.*)$/) { 583$subject=$1; 584 585}elsif(/^(Cc|From):\s+(.*)$/) { 586if(unquote_rfc2047($2)eq$from) { 587$from=$2; 588next if($suppress_from); 589} 590elsif($1eq'From') { 591$author_not_sender=$2; 592} 593printf("(mbox) Adding cc:%sfrom line '%s'\n", 594$2,$_)unless$quiet; 595push@cc,$2; 596} 597elsif(!/^Date:\s/&&/^[-A-Za-z]+:\s+\S/) { 598push@xh,$_; 599} 600 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. 607$input_format='lots'; 608if(@cc==0) { 609printf("(non-mbox) Adding cc:%sfrom line '%s'\n", 610$_,$_)unless$quiet; 611 612push@cc,$_; 613 614}elsif(!defined$subject) { 615$subject=$_; 616} 617} 618 619# A whitespace line will terminate the headers 620if(m/^\s*$/) { 621$header_done=1; 622} 623}else{ 624$message.=$_; 625if(/^(Signed-off-by|Cc): (.*)$/i&&$signed_off_cc) { 626my$c=$2; 627chomp$c; 628push@cc,$c; 629printf("(sob) Adding cc:%sfrom line '%s'\n", 630$c,$_)unless$quiet; 631} 632} 633} 634close F; 635if(defined$author_not_sender) { 636$author_not_sender= unquote_rfc2047($author_not_sender); 637$message="From:$author_not_sender\n\n$message"; 638} 639 640 641 send_message(); 642 643# set up for the next message 644if($chain_reply_to|| !defined$reply_to||length($reply_to) ==0) { 645$reply_to=$message_id; 646if(length$references>0) { 647$references.="\n$message_id"; 648}else{ 649$references="$message_id"; 650} 651} 652} 653 654if($compose) { 655 cleanup_compose_files(); 656} 657 658sub cleanup_compose_files() { 659unlink($compose_filename,$compose_filename.".final"); 660 661} 662 663$smtp->quitif$smtp; 664 665sub unique_email_list(@) { 666my%seen; 667my@emails; 668 669foreachmy$entry(@_) { 670if(my$clean= extract_valid_address($entry)) { 671$seen{$clean} ||=0; 672next if$seen{$clean}++; 673push@emails,$entry; 674}else{ 675print STDERR "W: unable to extract a valid address", 676" from:$entry\n"; 677} 678} 679return@emails; 680}