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