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 Term::ANSIColor; 25use Git; 26 27package FakeTerm; 28sub new { 29my($class,$reason) =@_; 30returnbless \$reason,shift; 31} 32subreadline{ 33my$self=shift; 34die"Cannot use readline on FakeTerm:$$self"; 35} 36package main; 37 38 39sub usage { 40print<<EOT; 41git send-email [options] <file | directory>... 42Options: 43 --from Specify the "From:" line of the email to be sent. 44 45 --to Specify the primary "To:" line of the email. 46 47 --cc Specify an initial "Cc:" list for the entire series 48 of emails. 49 50 --cc-cmd Specify a command to execute per file which adds 51 per file specific cc address entries 52 53 --bcc Specify a list of email addresses that should be Bcc: 54 on all the emails. 55 56 --compose Use \$GIT_EDITOR, core.editor, \$EDITOR, or \$VISUALto edit 57 an introductory message for the patch series. 58 59 --subject Specify the initial "Subject:" line. 60 Only necessary if --compose is also set. If --compose 61 is not set, this will be prompted for. 62 63 --in-reply-to Specify the first "In-Reply-To:" header line. 64 Only used if --compose is also set. If --compose is not 65 set, this will be prompted for. 66 67 --chain-reply-to If set, the replies will all be to the previous 68 email sent, rather than to the first email sent. 69 Defaults to on. 70 71 --signed-off-cc Automatically add email addresses that appear in 72 Signed-off-by: or Cc: lines to the cc: list. Defaults to on. 73 74 --identity The configuration identity, a subsection to prioritise over 75 the default section. 76 77 --smtp-server If set, specifies the outgoing SMTP server to use. 78 Defaults to localhost. Port number can be specified here with 79 hostname:port format or by using --smtp-server-port option. 80 81 --smtp-server-port Specify a port on the outgoing SMTP server to connect to. 82 83 --smtp-user The username for SMTP-AUTH. 84 85 --smtp-pass The password for SMTP-AUTH. 86 87 --smtp-encryption Specify 'tls' for STARTTLS encryption, or 'ssl' for SSL. 88 Any other value disables the feature. 89 90 --smtp-ssl Synonym for '--smtp-encryption=ssl'. Deprecated. 91 92 --suppress-cc Suppress the specified category of auto-CC. The category 93 can be one of 'author' for the patch author, 'self' to 94 avoid copying yourself, 'sob' for Signed-off-by lines, 95 'cccmd' for the output of the cccmd, or 'all' to suppress 96 all of these. 97 98 --suppress-from Suppress sending emails to yourself. Defaults to off. 99 100 --thread Specify that the "In-Reply-To:" header should be set on all 101 emails. Defaults to on. 102 103 --quiet Make git-send-email less verbose. One line per email 104 should be all that is output. 105 106 --dry-run Do everything except actually send the emails. 107 108 --envelope-sender Specify the envelope sender used to send the emails. 109 110 --no-validate Don't perform any sanity checks on patches. 111 112EOT 113exit(1); 114} 115 116# most mail servers generate the Date: header, but not all... 117sub format_2822_time { 118my($time) =@_; 119my@localtm=localtime($time); 120my@gmttm=gmtime($time); 121my$localmin=$localtm[1] +$localtm[2] *60; 122my$gmtmin=$gmttm[1] +$gmttm[2] *60; 123if($localtm[0] !=$gmttm[0]) { 124die"local zone differs from GMT by a non-minute interval\n"; 125} 126if((($gmttm[6] +1) %7) ==$localtm[6]) { 127$localmin+=1440; 128}elsif((($gmttm[6] -1) %7) ==$localtm[6]) { 129$localmin-=1440; 130}elsif($gmttm[6] !=$localtm[6]) { 131die"local time offset greater than or equal to 24 hours\n"; 132} 133my$offset=$localmin-$gmtmin; 134my$offhour=$offset/60; 135my$offmin=abs($offset%60); 136if(abs($offhour) >=24) { 137die("local time offset greater than or equal to 24 hours\n"); 138} 139 140returnsprintf("%s,%2d%s%d%02d:%02d:%02d%s%02d%02d", 141qw(Sun Mon Tue Wed Thu Fri Sat)[$localtm[6]], 142$localtm[3], 143qw(Jan Feb Mar Apr May Jun 144 Jul Aug Sep Oct Nov Dec)[$localtm[4]], 145$localtm[5]+1900, 146$localtm[2], 147$localtm[1], 148$localtm[0], 149($offset>=0) ?'+':'-', 150abs($offhour), 151$offmin, 152); 153} 154 155my$have_email_valid=eval{require Email::Valid;1}; 156my$smtp; 157my$auth; 158 159sub unique_email_list(@); 160sub cleanup_compose_files(); 161 162# Constants (essentially) 163my$compose_filename=".msg.$$"; 164 165# Variables we fill in automatically, or via prompting: 166my(@to,@cc,@initial_cc,@bcclist,@xh, 167$initial_reply_to,$initial_subject,@files,$author,$sender,$smtp_authpass,$compose,$time); 168 169my$envelope_sender; 170 171# Example reply to: 172#$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>'; 173 174my$repo=eval{ Git->repository() }; 175my@repo=$repo? ($repo) : (); 176my$term=eval{ 177$ENV{"GIT_SEND_EMAIL_NOTTY"} 178? new Term::ReadLine 'git-send-email', \*STDIN, \*STDOUT 179: new Term::ReadLine 'git-send-email'; 180}; 181if($@) { 182$term= new FakeTerm "$@: going non-interactive"; 183} 184 185# Behavior modification variables 186my($quiet,$dry_run) = (0,0); 187 188# Variables with corresponding config settings 189my($thread,$chain_reply_to,$suppress_from,$signed_off_cc,$cc_cmd); 190my($smtp_server,$smtp_server_port,$smtp_authuser,$smtp_encryption); 191my($identity,$aliasfiletype,@alias_files,@smtp_host_parts); 192my($no_validate); 193my(@suppress_cc); 194 195my%config_bool_settings= ( 196"thread"=> [\$thread,1], 197"chainreplyto"=> [\$chain_reply_to,1], 198"suppressfrom"=> [\$suppress_from,undef], 199"signedoffcc"=> [\$signed_off_cc,undef], 200); 201 202my%config_settings= ( 203"smtpserver"=> \$smtp_server, 204"smtpserverport"=> \$smtp_server_port, 205"smtpuser"=> \$smtp_authuser, 206"smtppass"=> \$smtp_authpass, 207"to"=> \@to, 208"cc"=> \@initial_cc, 209"cccmd"=> \$cc_cmd, 210"aliasfiletype"=> \$aliasfiletype, 211"bcc"=> \@bcclist, 212"aliasesfile"=> \@alias_files, 213"suppresscc"=> \@suppress_cc, 214"envelopesender"=> \$envelope_sender, 215); 216 217# Handle Uncouth Termination 218sub signal_handler { 219 220# Make text normal 221print color("reset"),"\n"; 222 223# SMTP password masked 224system"stty echo"; 225 226# tmp files from --compose 227if(-e $compose_filename) { 228print"'$compose_filename' contains an intermediate version of the email you were composing.\n"; 229} 230if(-e ($compose_filename.".final")) { 231print"'$compose_filename.final' contains the composed email.\n" 232} 233 234exit; 235}; 236 237$SIG{TERM} = \&signal_handler; 238$SIG{INT} = \&signal_handler; 239 240# Begin by accumulating all the variables (defined above), that we will end up 241# needing, first, from the command line: 242 243my$rc= GetOptions("sender|from=s"=> \$sender, 244"in-reply-to=s"=> \$initial_reply_to, 245"subject=s"=> \$initial_subject, 246"to=s"=> \@to, 247"cc=s"=> \@initial_cc, 248"bcc=s"=> \@bcclist, 249"chain-reply-to!"=> \$chain_reply_to, 250"smtp-server=s"=> \$smtp_server, 251"smtp-server-port=s"=> \$smtp_server_port, 252"smtp-user=s"=> \$smtp_authuser, 253"smtp-pass:s"=> \$smtp_authpass, 254"smtp-ssl"=>sub{$smtp_encryption='ssl'}, 255"smtp-encryption=s"=> \$smtp_encryption, 256"identity=s"=> \$identity, 257"compose"=> \$compose, 258"quiet"=> \$quiet, 259"cc-cmd=s"=> \$cc_cmd, 260"suppress-from!"=> \$suppress_from, 261"suppress-cc=s"=> \@suppress_cc, 262"signed-off-cc|signed-off-by-cc!"=> \$signed_off_cc, 263"dry-run"=> \$dry_run, 264"envelope-sender=s"=> \$envelope_sender, 265"thread!"=> \$thread, 266"no-validate"=> \$no_validate, 267); 268 269unless($rc) { 270 usage(); 271} 272 273# Now, let's fill any that aren't set in with defaults: 274 275sub read_config { 276my($prefix) =@_; 277 278foreachmy$setting(keys%config_bool_settings) { 279my$target=$config_bool_settings{$setting}->[0]; 280$$target= Git::config_bool(@repo,"$prefix.$setting")unless(defined$$target); 281} 282 283foreachmy$setting(keys%config_settings) { 284my$target=$config_settings{$setting}; 285if(ref($target)eq"ARRAY") { 286unless(@$target) { 287my@values= Git::config(@repo,"$prefix.$setting"); 288@$target=@valuesif(@values&&defined$values[0]); 289} 290} 291else{ 292$$target= Git::config(@repo,"$prefix.$setting")unless(defined$$target); 293} 294} 295 296if(!defined$smtp_encryption) { 297my$enc= Git::config(@repo,"$prefix.smtpencryption"); 298if(defined$enc) { 299$smtp_encryption=$enc; 300}elsif(Git::config_bool(@repo,"$prefix.smtpssl")) { 301$smtp_encryption='ssl'; 302} 303} 304} 305 306# read configuration from [sendemail "$identity"], fall back on [sendemail] 307$identity= Git::config(@repo,"sendemail.identity")unless(defined$identity); 308read_config("sendemail.$identity")if(defined$identity); 309read_config("sendemail"); 310 311# fall back on builtin bool defaults 312foreachmy$setting(values%config_bool_settings) { 313${$setting->[0]} =$setting->[1]unless(defined(${$setting->[0]})); 314} 315 316# 'default' encryption is none -- this only prevents a warning 317$smtp_encryption=''unless(defined$smtp_encryption); 318 319# Set CC suppressions 320my(%suppress_cc); 321if(@suppress_cc) { 322foreachmy$entry(@suppress_cc) { 323die"Unknown --suppress-cc field: '$entry'\n" 324unless$entry=~/^(all|cccmd|cc|author|self|sob)$/; 325$suppress_cc{$entry} =1; 326} 327} 328 329if($suppress_cc{'all'}) { 330foreachmy$entry(qw (ccmd cc author self sob)) { 331$suppress_cc{$entry} =1; 332} 333delete$suppress_cc{'all'}; 334} 335 336# If explicit old-style ones are specified, they trump --suppress-cc. 337$suppress_cc{'self'} =$suppress_fromifdefined$suppress_from; 338$suppress_cc{'sob'} = !$signed_off_ccifdefined$signed_off_cc; 339 340# Debugging, print out the suppressions. 341if(0) { 342print"suppressions:\n"; 343foreachmy$entry(keys%suppress_cc) { 344printf" %-5s ->$suppress_cc{$entry}\n",$entry; 345} 346} 347 348my($repoauthor,$repocommitter); 349($repoauthor) = Git::ident_person(@repo,'author'); 350($repocommitter) = Git::ident_person(@repo,'committer'); 351 352# Verify the user input 353 354foreachmy$entry(@to) { 355die"Comma in --to entry:$entry'\n"unless$entry!~m/,/; 356} 357 358foreachmy$entry(@initial_cc) { 359die"Comma in --cc entry:$entry'\n"unless$entry!~m/,/; 360} 361 362foreachmy$entry(@bcclist) { 363die"Comma in --bcclist entry:$entry'\n"unless$entry!~m/,/; 364} 365 366my%aliases; 367my%parse_alias= ( 368# multiline formats can be supported in the future 369 mutt =>sub{my$fh=shift;while(<$fh>) { 370if(/^\s*alias\s+(\S+)\s+(.*)$/) { 371my($alias,$addr) = ($1,$2); 372$addr=~s/#.*$//;# mutt allows # comments 373# commas delimit multiple addresses 374$aliases{$alias} = [split(/\s*,\s*/,$addr) ]; 375}}}, 376 mailrc =>sub{my$fh=shift;while(<$fh>) { 377if(/^alias\s+(\S+)\s+(.*)$/) { 378# spaces delimit multiple addresses 379$aliases{$1} = [split(/\s+/,$2) ]; 380}}}, 381 pine =>sub{my$fh=shift;while(<$fh>) { 382if(/^(\S+)\t.*\t(.*)$/) { 383$aliases{$1} = [split(/\s*,\s*/,$2) ]; 384}}}, 385 gnus =>sub{my$fh=shift;while(<$fh>) { 386if(/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) { 387$aliases{$1} = [$2]; 388}}} 389); 390 391if(@alias_filesand$aliasfiletypeand defined$parse_alias{$aliasfiletype}) { 392foreachmy$file(@alias_files) { 393open my$fh,'<',$fileor die"opening$file:$!\n"; 394$parse_alias{$aliasfiletype}->($fh); 395close$fh; 396} 397} 398 399($sender) = expand_aliases($sender)ifdefined$sender; 400 401# Now that all the defaults are set, process the rest of the command line 402# arguments and collect up the files that need to be processed. 403formy$f(@ARGV) { 404if(-d $f) { 405opendir(DH,$f) 406or die"Failed to opendir$f:$!"; 407 408push@files,grep{ -f $_}map{ +$f."/".$_} 409sort readdir(DH); 410closedir(DH); 411}elsif(-f $for-p $f) { 412push@files,$f; 413}else{ 414print STDERR "Skipping$f- not found.\n"; 415} 416} 417 418if(!$no_validate) { 419foreachmy$f(@files) { 420unless(-p $f) { 421my$error= validate_patch($f); 422$errorand die"fatal:$f:$error\nwarning: no patches were sent\n"; 423} 424} 425} 426 427if(@files) { 428unless($quiet) { 429print$_,"\n"for(@files); 430} 431}else{ 432print STDERR "\nNo patch files specified!\n\n"; 433 usage(); 434} 435 436my$prompting=0; 437if(!defined$sender) { 438$sender=$repoauthor||$repocommitter||''; 439 440while(1) { 441$_=$term->readline("Who should the emails appear to be from? [$sender] "); 442last ifdefined$_; 443print"\n"; 444} 445 446$sender=$_if($_); 447print"Emails will be sent from: ",$sender,"\n"; 448$prompting++; 449} 450 451if(!@to) { 452 453 454while(1) { 455$_=$term->readline("Who should the emails be sent to? ",""); 456last ifdefined$_; 457print"\n"; 458} 459 460my$to=$_; 461push@to,split/,\s*/,$to; 462$prompting++; 463} 464 465sub expand_aliases { 466my@cur=@_; 467my@last; 468do{ 469@last=@cur; 470@cur=map{$aliases{$_} ? @{$aliases{$_}} :$_}@last; 471}while(join(',',@cur)ne join(',',@last)); 472return@cur; 473} 474 475@to= expand_aliases(@to); 476@to= (map{ sanitize_address($_) }@to); 477@initial_cc= expand_aliases(@initial_cc); 478@bcclist= expand_aliases(@bcclist); 479 480if(!defined$initial_subject&&$compose) { 481while(1) { 482$_=$term->readline("What subject should the initial email start with? ",$initial_subject); 483last ifdefined$_; 484print"\n"; 485} 486 487$initial_subject=$_; 488$prompting++; 489} 490 491if($thread&& !defined$initial_reply_to&&$prompting) { 492while(1) { 493$_=$term->readline("Message-ID to be used as In-Reply-To for the first email? ",$initial_reply_to); 494last ifdefined$_; 495print"\n"; 496} 497 498$initial_reply_to=$_; 499} 500if(defined$initial_reply_to) { 501$initial_reply_to=~s/^\s*<?//; 502$initial_reply_to=~s/>?\s*$//; 503$initial_reply_to="<$initial_reply_to>"if$initial_reply_tone''; 504} 505 506if(!defined$smtp_server) { 507foreach(qw( /usr/sbin/sendmail /usr/lib/sendmail )) { 508if(-x $_) { 509$smtp_server=$_; 510last; 511} 512} 513$smtp_server||='localhost';# could be 127.0.0.1, too... *shrug* 514} 515 516if($compose) { 517# Note that this does not need to be secure, but we will make a small 518# effort to have it be unique 519open(C,">",$compose_filename) 520or die"Failed to open for writing$compose_filename:$!"; 521print C "From$sender# This line is ignored.\n"; 522printf C "Subject:%s\n\n",$initial_subject; 523printf C <<EOT; 524GIT: Please enter your email below. 525GIT: Lines beginning in "GIT: " will be removed. 526GIT: Consider including an overall diffstat or table of contents 527GIT: for the patch you are writing. 528 529EOT 530close(C); 531 532my$editor=$ENV{GIT_EDITOR} || Git::config(@repo,"core.editor") ||$ENV{VISUAL} ||$ENV{EDITOR} ||"vi"; 533system('sh','-c',$editor.' "$@"',$editor,$compose_filename); 534 535open(C2,">",$compose_filename.".final") 536or die"Failed to open$compose_filename.final : ".$!; 537 538open(C,"<",$compose_filename) 539or die"Failed to open$compose_filename: ".$!; 540 541my$need_8bit_cte= file_has_nonascii($compose_filename); 542my$in_body=0; 543while(<C>) { 544next ifm/^GIT: /; 545if(!$in_body&&/^\n$/) { 546$in_body=1; 547if($need_8bit_cte) { 548print C2 "MIME-Version: 1.0\n", 549"Content-Type: text/plain; ", 550"charset=utf-8\n", 551"Content-Transfer-Encoding: 8bit\n"; 552} 553} 554if(!$in_body&&/^MIME-Version:/i) { 555$need_8bit_cte=0; 556} 557if(!$in_body&&/^Subject: ?(.*)/i) { 558my$subject=$1; 559$_="Subject: ". 560($subject=~/[^[:ascii:]]/? 561 quote_rfc2047($subject) : 562$subject) . 563"\n"; 564} 565print C2 $_; 566} 567close(C); 568close(C2); 569 570while(1) { 571$_=$term->readline("Send this email? (y|n) "); 572last ifdefined$_; 573print"\n"; 574} 575 576if(uc substr($_,0,1)ne'Y') { 577 cleanup_compose_files(); 578exit(0); 579} 580 581@files= ($compose_filename.".final",@files); 582} 583 584# Variables we set as part of the loop over files 585our($message_id,%mail,$subject,$reply_to,$references,$message); 586 587sub extract_valid_address { 588my$address=shift; 589my$local_part_regexp='[^<>"\s@]+'; 590my$domain_regexp='[^.<>"\s@]+(?:\.[^.<>"\s@]+)+'; 591 592# check for a local address: 593return$addressif($address=~/^($local_part_regexp)$/); 594 595$address=~s/^\s*<(.*)>\s*$/$1/; 596if($have_email_valid) { 597returnscalar Email::Valid->address($address); 598}else{ 599# less robust/correct than the monster regexp in Email::Valid, 600# but still does a 99% job, and one less dependency 601$address=~/($local_part_regexp\@$domain_regexp)/; 602return$1; 603} 604} 605 606# Usually don't need to change anything below here. 607 608# we make a "fake" message id by taking the current number 609# of seconds since the beginning of Unix time and tacking on 610# a random number to the end, in case we are called quicker than 611# 1 second since the last time we were called. 612 613# We'll setup a template for the message id, using the "from" address: 614 615my($message_id_stamp,$message_id_serial); 616sub make_message_id 617{ 618my$uniq; 619if(!defined$message_id_stamp) { 620$message_id_stamp=sprintf("%s-%s",time,$$); 621$message_id_serial=0; 622} 623$message_id_serial++; 624$uniq="$message_id_stamp-$message_id_serial"; 625 626my$du_part; 627for($sender,$repocommitter,$repoauthor) { 628$du_part= extract_valid_address(sanitize_address($_)); 629last if(defined$du_partand$du_partne''); 630} 631if(not defined$du_partor$du_parteq'') { 632use Sys::Hostname qw(); 633$du_part='user@'. Sys::Hostname::hostname(); 634} 635my$message_id_template="<%s-git-send-email-%s>"; 636$message_id=sprintf($message_id_template,$uniq,$du_part); 637#print "new message id = $message_id\n"; # Was useful for debugging 638} 639 640 641 642$time=time-scalar$#files; 643 644sub unquote_rfc2047 { 645local($_) =@_; 646my$encoding; 647if(s/=\?([^?]+)\?q\?(.*)\?=/$2/g) { 648$encoding=$1; 649s/_/ /g; 650s/=([0-9A-F]{2})/chr(hex($1))/eg; 651} 652returnwantarray? ($_,$encoding) :$_; 653} 654 655sub quote_rfc2047 { 656local$_=shift; 657my$encoding=shift||'utf-8'; 658s/([^-a-zA-Z0-9!*+\/])/sprintf("=%02X",ord($1))/eg; 659s/(.*)/=\?$encoding\?q\?$1\?=/; 660return$_; 661} 662 663# use the simplest quoting being able to handle the recipient 664sub sanitize_address 665{ 666my($recipient) =@_; 667my($recipient_name,$recipient_addr) = ($recipient=~/^(.*?)\s*(<.*)/); 668 669if(not$recipient_name) { 670return"$recipient"; 671} 672 673# if recipient_name is already quoted, do nothing 674if($recipient_name=~/^(".*"|=\?utf-8\?q\?.*\?=)$/) { 675return$recipient; 676} 677 678# rfc2047 is needed if a non-ascii char is included 679if($recipient_name=~/[^[:ascii:]]/) { 680$recipient_name= quote_rfc2047($recipient_name); 681} 682 683# double quotes are needed if specials or CTLs are included 684elsif($recipient_name=~/[][()<>@,;:\\".\000-\037\177]/) { 685$recipient_name=~s/(["\\\r])/\\$1/g; 686$recipient_name="\"$recipient_name\""; 687} 688 689return"$recipient_name$recipient_addr"; 690 691} 692 693sub send_message 694{ 695my@recipients= unique_email_list(@to); 696@cc= (grep{my$cc= extract_valid_address($_); 697not grep{$cceq$_}@recipients 698} 699map{ sanitize_address($_) } 700@cc); 701my$to=join(",\n\t",@recipients); 702@recipients= unique_email_list(@recipients,@cc,@bcclist); 703@recipients= (map{ extract_valid_address($_) }@recipients); 704my$date= format_2822_time($time++); 705my$gitversion='@@GIT_VERSION@@'; 706if($gitversion=~m/..GIT_VERSION../) { 707$gitversion= Git::version(); 708} 709 710my$cc=join(", ", unique_email_list(@cc)); 711my$ccline=""; 712if($ccne'') { 713$ccline="\nCc:$cc"; 714} 715my$sanitized_sender= sanitize_address($sender); 716 make_message_id()unlessdefined($message_id); 717 718my$header="From:$sanitized_sender 719To:$to${ccline} 720Subject:$subject 721Date:$date 722Message-Id:$message_id 723X-Mailer: git-send-email$gitversion 724"; 725if($thread&&$reply_to) { 726 727$header.="In-Reply-To:$reply_to\n"; 728$header.="References:$references\n"; 729} 730if(@xh) { 731$header.=join("\n",@xh) ."\n"; 732} 733 734my@sendmail_parameters= ('-i',@recipients); 735my$raw_from=$sanitized_sender; 736$raw_from=$envelope_senderif(defined$envelope_sender); 737$raw_from= extract_valid_address($raw_from); 738unshift(@sendmail_parameters, 739'-f',$raw_from)if(defined$envelope_sender); 740 741if($dry_run) { 742# We don't want to send the email. 743}elsif($smtp_server=~ m#^/#) { 744my$pid=open my$sm,'|-'; 745defined$pidor die$!; 746if(!$pid) { 747exec($smtp_server,@sendmail_parameters)or die$!; 748} 749print$sm"$header\n$message"; 750close$smor die$?; 751}else{ 752 753if(!defined$smtp_server) { 754die"The required SMTP server is not properly defined." 755} 756 757if($smtp_encryptioneq'ssl') { 758$smtp_server_port||=465;# ssmtp 759require Net::SMTP::SSL; 760$smtp||= Net::SMTP::SSL->new($smtp_server, Port =>$smtp_server_port); 761} 762else{ 763require Net::SMTP; 764$smtp||= Net::SMTP->new((defined$smtp_server_port) 765?"$smtp_server:$smtp_server_port" 766:$smtp_server); 767if($smtp_encryptioneq'tls') { 768require Net::SMTP::SSL; 769$smtp->command('STARTTLS'); 770$smtp->response(); 771if($smtp->code==220) { 772$smtp= Net::SMTP::SSL->start_SSL($smtp) 773or die"STARTTLS failed! ".$smtp->message; 774$smtp_encryption=''; 775# Send EHLO again to receive fresh 776# supported commands 777$smtp->hello(); 778}else{ 779die"Server does not support STARTTLS! ".$smtp->message; 780} 781} 782} 783 784if(!$smtp) { 785die"Unable to initialize SMTP properly. Is there something wrong with your config?"; 786} 787 788if(defined$smtp_authuser) { 789 790if(!defined$smtp_authpass) { 791 792system"stty -echo"; 793 794do{ 795print"Password: "; 796$_= <STDIN>; 797print"\n"; 798}while(!defined$_); 799 800chomp($smtp_authpass=$_); 801 802system"stty echo"; 803} 804 805$auth||=$smtp->auth($smtp_authuser,$smtp_authpass)or die$smtp->message; 806} 807 808$smtp->mail($raw_from)or die$smtp->message; 809$smtp->to(@recipients)or die$smtp->message; 810$smtp->dataor die$smtp->message; 811$smtp->datasend("$header\n$message")or die$smtp->message; 812$smtp->dataend()or die$smtp->message; 813$smtp->okor die"Failed to send$subject\n".$smtp->message; 814} 815if($quiet) { 816printf(($dry_run?"Dry-":"")."Sent%s\n",$subject); 817}else{ 818print(($dry_run?"Dry-":"")."OK. Log says:\n"); 819if($smtp_server!~ m#^/#) { 820print"Server:$smtp_server\n"; 821print"MAIL FROM:<$raw_from>\n"; 822print"RCPT TO:".join(',',(map{"<$_>"}@recipients))."\n"; 823}else{ 824print"Sendmail:$smtp_server".join(' ',@sendmail_parameters)."\n"; 825} 826print$header,"\n"; 827if($smtp) { 828print"Result: ",$smtp->code,' ', 829($smtp->message=~/\n([^\n]+\n)$/s),"\n"; 830}else{ 831print"Result: OK\n"; 832} 833} 834} 835 836$reply_to=$initial_reply_to; 837$references=$initial_reply_to||''; 838$subject=$initial_subject; 839 840foreachmy$t(@files) { 841open(F,"<",$t)or die"can't open file$t"; 842 843my$author=undef; 844my$author_encoding; 845my$has_content_type; 846my$body_encoding; 847@cc=@initial_cc; 848@xh= (); 849my$input_format=undef; 850my$header_done=0; 851$message=""; 852while(<F>) { 853if(!$header_done) { 854if(/^From /) { 855$input_format='mbox'; 856next; 857} 858chomp; 859if(!defined$input_format&&/^[-A-Za-z]+:\s/) { 860$input_format='mbox'; 861} 862 863if(defined$input_format&&$input_formateq'mbox') { 864if(/^Subject:\s+(.*)$/) { 865$subject=$1; 866 867}elsif(/^(Cc|From):\s+(.*)$/) { 868if(unquote_rfc2047($2)eq$sender) { 869next if($suppress_cc{'self'}); 870} 871elsif($1eq'From') { 872($author,$author_encoding) 873= unquote_rfc2047($2); 874next if($suppress_cc{'author'}); 875}else{ 876next if($suppress_cc{'cc'}); 877} 878printf("(mbox) Adding cc:%sfrom line '%s'\n", 879$2,$_)unless$quiet; 880push@cc,$2; 881} 882elsif(/^Content-type:/i) { 883$has_content_type=1; 884if(/charset="?([^ "]+)/) { 885$body_encoding=$1; 886} 887push@xh,$_; 888} 889elsif(/^Message-Id: (.*)/i) { 890$message_id=$1; 891} 892elsif(!/^Date:\s/&&/^[-A-Za-z]+:\s+\S/) { 893push@xh,$_; 894} 895 896}else{ 897# In the traditional 898# "send lots of email" format, 899# line 1 = cc 900# line 2 = subject 901# So let's support that, too. 902$input_format='lots'; 903if(@cc==0&& !$suppress_cc{'cc'}) { 904printf("(non-mbox) Adding cc:%sfrom line '%s'\n", 905$_,$_)unless$quiet; 906 907push@cc,$_; 908 909}elsif(!defined$subject) { 910$subject=$_; 911} 912} 913 914# A whitespace line will terminate the headers 915if(m/^\s*$/) { 916$header_done=1; 917} 918}else{ 919$message.=$_; 920if(/^(Signed-off-by|Cc): (.*)$/i) { 921next if($suppress_cc{'sob'}); 922chomp; 923my$c=$2; 924chomp$c; 925next if($ceq$senderand$suppress_cc{'self'}); 926push@cc,$c; 927printf("(sob) Adding cc:%sfrom line '%s'\n", 928$c,$_)unless$quiet; 929} 930} 931} 932close F; 933 934if(defined$cc_cmd&& !$suppress_cc{'cccmd'}) { 935open(F,"$cc_cmd$t|") 936or die"(cc-cmd) Could not execute '$cc_cmd'"; 937while(<F>) { 938my$c=$_; 939$c=~s/^\s*//g; 940$c=~s/\n$//g; 941next if($ceq$senderand$suppress_from); 942push@cc,$c; 943printf("(cc-cmd) Adding cc:%sfrom: '%s'\n", 944$c,$cc_cmd)unless$quiet; 945} 946close F 947or die"(cc-cmd) failed to close pipe to '$cc_cmd'"; 948} 949 950if(defined$author) { 951$message="From:$author\n\n$message"; 952if(defined$author_encoding) { 953if($has_content_type) { 954if($body_encodingeq$author_encoding) { 955# ok, we already have the right encoding 956} 957else{ 958# uh oh, we should re-encode 959} 960} 961else{ 962push@xh, 963'MIME-Version: 1.0', 964"Content-Type: text/plain; charset=$author_encoding", 965'Content-Transfer-Encoding: 8bit'; 966} 967} 968} 969 970 send_message(); 971 972# set up for the next message 973if($chain_reply_to|| !defined$reply_to||length($reply_to) ==0) { 974$reply_to=$message_id; 975if(length$references>0) { 976$references.="\n$message_id"; 977}else{ 978$references="$message_id"; 979} 980} 981$message_id=undef; 982} 983 984if($compose) { 985 cleanup_compose_files(); 986} 987 988sub cleanup_compose_files() { 989unlink($compose_filename,$compose_filename.".final"); 990 991} 992 993$smtp->quitif$smtp; 994 995sub unique_email_list(@) { 996my%seen; 997my@emails; 998 999foreachmy$entry(@_) {1000if(my$clean= extract_valid_address($entry)) {1001$seen{$clean} ||=0;1002next if$seen{$clean}++;1003push@emails,$entry;1004}else{1005print STDERR "W: unable to extract a valid address",1006" from:$entry\n";1007}1008}1009return@emails;1010}10111012sub validate_patch {1013my$fn=shift;1014open(my$fh,'<',$fn)1015or die"unable to open$fn:$!\n";1016while(my$line= <$fh>) {1017if(length($line) >998) {1018return"$.: patch contains a line longer than 998 characters";1019}1020}1021returnundef;1022}10231024sub file_has_nonascii {1025my$fn=shift;1026open(my$fh,'<',$fn)1027or die"unable to open$fn:$!\n";1028while(my$line= <$fh>) {1029return1if$line=~/[^[:ascii:]]/;1030}1031return0;1032}