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); 410 411}elsif(-f $for-p $f) { 412push@files,$f; 413 414}else{ 415print STDERR "Skipping$f- not found.\n"; 416} 417} 418 419if(!$no_validate) { 420foreachmy$f(@files) { 421unless(-p $f) { 422my$error= validate_patch($f); 423$errorand die"fatal:$f:$error\nwarning: no patches were sent\n"; 424} 425} 426} 427 428if(@files) { 429unless($quiet) { 430print$_,"\n"for(@files); 431} 432}else{ 433print STDERR "\nNo patch files specified!\n\n"; 434 usage(); 435} 436 437my$prompting=0; 438if(!defined$sender) { 439$sender=$repoauthor||$repocommitter||''; 440 441while(1) { 442$_=$term->readline("Who should the emails appear to be from? [$sender] "); 443last ifdefined$_; 444print"\n"; 445} 446 447$sender=$_if($_); 448print"Emails will be sent from: ",$sender,"\n"; 449$prompting++; 450} 451 452if(!@to) { 453 454 455while(1) { 456$_=$term->readline("Who should the emails be sent to? ",""); 457last ifdefined$_; 458print"\n"; 459} 460 461my$to=$_; 462push@to,split/,\s*/,$to; 463$prompting++; 464} 465 466sub expand_aliases { 467my@cur=@_; 468my@last; 469do{ 470@last=@cur; 471@cur=map{$aliases{$_} ? @{$aliases{$_}} :$_}@last; 472}while(join(',',@cur)ne join(',',@last)); 473return@cur; 474} 475 476@to= expand_aliases(@to); 477@to= (map{ sanitize_address($_) }@to); 478@initial_cc= expand_aliases(@initial_cc); 479@bcclist= expand_aliases(@bcclist); 480 481if(!defined$initial_subject&&$compose) { 482while(1) { 483$_=$term->readline("What subject should the initial email start with? ",$initial_subject); 484last ifdefined$_; 485print"\n"; 486} 487 488$initial_subject=$_; 489$prompting++; 490} 491 492if($thread&& !defined$initial_reply_to&&$prompting) { 493while(1) { 494$_=$term->readline("Message-ID to be used as In-Reply-To for the first email? ",$initial_reply_to); 495last ifdefined$_; 496print"\n"; 497} 498 499$initial_reply_to=$_; 500} 501if(defined$initial_reply_to) { 502$initial_reply_to=~s/^\s*<?//; 503$initial_reply_to=~s/>?\s*$//; 504$initial_reply_to="<$initial_reply_to>"if$initial_reply_tone''; 505} 506 507if(!defined$smtp_server) { 508foreach(qw( /usr/sbin/sendmail /usr/lib/sendmail )) { 509if(-x $_) { 510$smtp_server=$_; 511last; 512} 513} 514$smtp_server||='localhost';# could be 127.0.0.1, too... *shrug* 515} 516 517if($compose) { 518# Note that this does not need to be secure, but we will make a small 519# effort to have it be unique 520open(C,">",$compose_filename) 521or die"Failed to open for writing$compose_filename:$!"; 522print C "From$sender# This line is ignored.\n"; 523printf C "Subject:%s\n\n",$initial_subject; 524printf C <<EOT; 525GIT: Please enter your email below. 526GIT: Lines beginning in "GIT: " will be removed. 527GIT: Consider including an overall diffstat or table of contents 528GIT: for the patch you are writing. 529 530EOT 531close(C); 532 533my$editor=$ENV{GIT_EDITOR} || Git::config(@repo,"core.editor") ||$ENV{VISUAL} ||$ENV{EDITOR} ||"vi"; 534system('sh','-c',$editor.' "$@"',$editor,$compose_filename); 535 536open(C2,">",$compose_filename.".final") 537or die"Failed to open$compose_filename.final : ".$!; 538 539open(C,"<",$compose_filename) 540or die"Failed to open$compose_filename: ".$!; 541 542my$need_8bit_cte= file_has_nonascii($compose_filename); 543my$in_body=0; 544while(<C>) { 545next ifm/^GIT: /; 546if(!$in_body&&/^\n$/) { 547$in_body=1; 548if($need_8bit_cte) { 549print C2 "MIME-Version: 1.0\n", 550"Content-Type: text/plain; ", 551"charset=utf-8\n", 552"Content-Transfer-Encoding: 8bit\n"; 553} 554} 555if(!$in_body&&/^MIME-Version:/i) { 556$need_8bit_cte=0; 557} 558if(!$in_body&&/^Subject: ?(.*)/i) { 559my$subject=$1; 560$_="Subject: ". 561($subject=~/[^[:ascii:]]/? 562 quote_rfc2047($subject) : 563$subject) . 564"\n"; 565} 566print C2 $_; 567} 568close(C); 569close(C2); 570 571while(1) { 572$_=$term->readline("Send this email? (y|n) "); 573last ifdefined$_; 574print"\n"; 575} 576 577if(uc substr($_,0,1)ne'Y') { 578 cleanup_compose_files(); 579exit(0); 580} 581 582@files= ($compose_filename.".final",@files); 583} 584 585# Variables we set as part of the loop over files 586our($message_id,%mail,$subject,$reply_to,$references,$message); 587 588sub extract_valid_address { 589my$address=shift; 590my$local_part_regexp='[^<>"\s@]+'; 591my$domain_regexp='[^.<>"\s@]+(?:\.[^.<>"\s@]+)+'; 592 593# check for a local address: 594return$addressif($address=~/^($local_part_regexp)$/); 595 596$address=~s/^\s*<(.*)>\s*$/$1/; 597if($have_email_valid) { 598returnscalar Email::Valid->address($address); 599}else{ 600# less robust/correct than the monster regexp in Email::Valid, 601# but still does a 99% job, and one less dependency 602$address=~/($local_part_regexp\@$domain_regexp)/; 603return$1; 604} 605} 606 607# Usually don't need to change anything below here. 608 609# we make a "fake" message id by taking the current number 610# of seconds since the beginning of Unix time and tacking on 611# a random number to the end, in case we are called quicker than 612# 1 second since the last time we were called. 613 614# We'll setup a template for the message id, using the "from" address: 615 616my($message_id_stamp,$message_id_serial); 617sub make_message_id 618{ 619my$uniq; 620if(!defined$message_id_stamp) { 621$message_id_stamp=sprintf("%s-%s",time,$$); 622$message_id_serial=0; 623} 624$message_id_serial++; 625$uniq="$message_id_stamp-$message_id_serial"; 626 627my$du_part; 628for($sender,$repocommitter,$repoauthor) { 629$du_part= extract_valid_address(sanitize_address($_)); 630last if(defined$du_partand$du_partne''); 631} 632if(not defined$du_partor$du_parteq'') { 633use Sys::Hostname qw(); 634$du_part='user@'. Sys::Hostname::hostname(); 635} 636my$message_id_template="<%s-git-send-email-%s>"; 637$message_id=sprintf($message_id_template,$uniq,$du_part); 638#print "new message id = $message_id\n"; # Was useful for debugging 639} 640 641 642 643$time=time-scalar$#files; 644 645sub unquote_rfc2047 { 646local($_) =@_; 647my$encoding; 648if(s/=\?([^?]+)\?q\?(.*)\?=/$2/g) { 649$encoding=$1; 650s/_/ /g; 651s/=([0-9A-F]{2})/chr(hex($1))/eg; 652} 653returnwantarray? ($_,$encoding) :$_; 654} 655 656sub quote_rfc2047 { 657local$_=shift; 658my$encoding=shift||'utf-8'; 659s/([^-a-zA-Z0-9!*+\/])/sprintf("=%02X",ord($1))/eg; 660s/(.*)/=\?$encoding\?q\?$1\?=/; 661return$_; 662} 663 664# use the simplest quoting being able to handle the recipient 665sub sanitize_address 666{ 667my($recipient) =@_; 668my($recipient_name,$recipient_addr) = ($recipient=~/^(.*?)\s*(<.*)/); 669 670if(not$recipient_name) { 671return"$recipient"; 672} 673 674# if recipient_name is already quoted, do nothing 675if($recipient_name=~/^(".*"|=\?utf-8\?q\?.*\?=)$/) { 676return$recipient; 677} 678 679# rfc2047 is needed if a non-ascii char is included 680if($recipient_name=~/[^[:ascii:]]/) { 681$recipient_name= quote_rfc2047($recipient_name); 682} 683 684# double quotes are needed if specials or CTLs are included 685elsif($recipient_name=~/[][()<>@,;:\\".\000-\037\177]/) { 686$recipient_name=~s/(["\\\r])/\\$1/g; 687$recipient_name="\"$recipient_name\""; 688} 689 690return"$recipient_name$recipient_addr"; 691 692} 693 694sub send_message 695{ 696my@recipients= unique_email_list(@to); 697@cc= (grep{my$cc= extract_valid_address($_); 698not grep{$cceq$_}@recipients 699} 700map{ sanitize_address($_) } 701@cc); 702my$to=join(",\n\t",@recipients); 703@recipients= unique_email_list(@recipients,@cc,@bcclist); 704@recipients= (map{ extract_valid_address($_) }@recipients); 705my$date= format_2822_time($time++); 706my$gitversion='@@GIT_VERSION@@'; 707if($gitversion=~m/..GIT_VERSION../) { 708$gitversion= Git::version(); 709} 710 711my$cc=join(", ", unique_email_list(@cc)); 712my$ccline=""; 713if($ccne'') { 714$ccline="\nCc:$cc"; 715} 716my$sanitized_sender= sanitize_address($sender); 717 make_message_id()unlessdefined($message_id); 718 719my$header="From:$sanitized_sender 720To:$to${ccline} 721Subject:$subject 722Date:$date 723Message-Id:$message_id 724X-Mailer: git-send-email$gitversion 725"; 726if($thread&&$reply_to) { 727 728$header.="In-Reply-To:$reply_to\n"; 729$header.="References:$references\n"; 730} 731if(@xh) { 732$header.=join("\n",@xh) ."\n"; 733} 734 735my@sendmail_parameters= ('-i',@recipients); 736my$raw_from=$sanitized_sender; 737$raw_from=$envelope_senderif(defined$envelope_sender); 738$raw_from= extract_valid_address($raw_from); 739unshift(@sendmail_parameters, 740'-f',$raw_from)if(defined$envelope_sender); 741 742if($dry_run) { 743# We don't want to send the email. 744}elsif($smtp_server=~ m#^/#) { 745my$pid=open my$sm,'|-'; 746defined$pidor die$!; 747if(!$pid) { 748exec($smtp_server,@sendmail_parameters)or die$!; 749} 750print$sm"$header\n$message"; 751close$smor die$?; 752}else{ 753 754if(!defined$smtp_server) { 755die"The required SMTP server is not properly defined." 756} 757 758if($smtp_encryptioneq'ssl') { 759$smtp_server_port||=465;# ssmtp 760require Net::SMTP::SSL; 761$smtp||= Net::SMTP::SSL->new($smtp_server, Port =>$smtp_server_port); 762} 763else{ 764require Net::SMTP; 765$smtp||= Net::SMTP->new((defined$smtp_server_port) 766?"$smtp_server:$smtp_server_port" 767:$smtp_server); 768if($smtp_encryptioneq'tls') { 769require Net::SMTP::SSL; 770$smtp->command('STARTTLS'); 771$smtp->response(); 772if($smtp->code==220) { 773$smtp= Net::SMTP::SSL->start_SSL($smtp) 774or die"STARTTLS failed! ".$smtp->message; 775$smtp_encryption=''; 776}else{ 777die"Server does not support STARTTLS! ".$smtp->message; 778} 779} 780} 781 782if(!$smtp) { 783die"Unable to initialize SMTP properly. Is there something wrong with your config?"; 784} 785 786if(defined$smtp_authuser) { 787 788if(!defined$smtp_authpass) { 789 790system"stty -echo"; 791 792do{ 793print"Password: "; 794$_= <STDIN>; 795print"\n"; 796}while(!defined$_); 797 798chomp($smtp_authpass=$_); 799 800system"stty echo"; 801} 802 803$auth||=$smtp->auth($smtp_authuser,$smtp_authpass)or die$smtp->message; 804} 805 806$smtp->mail($raw_from)or die$smtp->message; 807$smtp->to(@recipients)or die$smtp->message; 808$smtp->dataor die$smtp->message; 809$smtp->datasend("$header\n$message")or die$smtp->message; 810$smtp->dataend()or die$smtp->message; 811$smtp->okor die"Failed to send$subject\n".$smtp->message; 812} 813if($quiet) { 814printf(($dry_run?"Dry-":"")."Sent%s\n",$subject); 815}else{ 816print(($dry_run?"Dry-":"")."OK. Log says:\n"); 817if($smtp_server!~ m#^/#) { 818print"Server:$smtp_server\n"; 819print"MAIL FROM:<$raw_from>\n"; 820print"RCPT TO:".join(',',(map{"<$_>"}@recipients))."\n"; 821}else{ 822print"Sendmail:$smtp_server".join(' ',@sendmail_parameters)."\n"; 823} 824print$header,"\n"; 825if($smtp) { 826print"Result: ",$smtp->code,' ', 827($smtp->message=~/\n([^\n]+\n)$/s),"\n"; 828}else{ 829print"Result: OK\n"; 830} 831} 832} 833 834$reply_to=$initial_reply_to; 835$references=$initial_reply_to||''; 836$subject=$initial_subject; 837 838foreachmy$t(@files) { 839open(F,"<",$t)or die"can't open file$t"; 840 841my$author=undef; 842my$author_encoding; 843my$has_content_type; 844my$body_encoding; 845@cc=@initial_cc; 846@xh= (); 847my$input_format=undef; 848my$header_done=0; 849$message=""; 850while(<F>) { 851if(!$header_done) { 852if(/^From /) { 853$input_format='mbox'; 854next; 855} 856chomp; 857if(!defined$input_format&&/^[-A-Za-z]+:\s/) { 858$input_format='mbox'; 859} 860 861if(defined$input_format&&$input_formateq'mbox') { 862if(/^Subject:\s+(.*)$/) { 863$subject=$1; 864 865}elsif(/^(Cc|From):\s+(.*)$/) { 866if(unquote_rfc2047($2)eq$sender) { 867next if($suppress_cc{'self'}); 868} 869elsif($1eq'From') { 870($author,$author_encoding) 871= unquote_rfc2047($2); 872next if($suppress_cc{'author'}); 873}else{ 874next if($suppress_cc{'cc'}); 875} 876printf("(mbox) Adding cc:%sfrom line '%s'\n", 877$2,$_)unless$quiet; 878push@cc,$2; 879} 880elsif(/^Content-type:/i) { 881$has_content_type=1; 882if(/charset="?[^ "]+/) { 883$body_encoding=$1; 884} 885push@xh,$_; 886} 887elsif(/^Message-Id: (.*)/i) { 888$message_id=$1; 889} 890elsif(!/^Date:\s/&&/^[-A-Za-z]+:\s+\S/) { 891push@xh,$_; 892} 893 894}else{ 895# In the traditional 896# "send lots of email" format, 897# line 1 = cc 898# line 2 = subject 899# So let's support that, too. 900$input_format='lots'; 901if(@cc==0&& !$suppress_cc{'cc'}) { 902printf("(non-mbox) Adding cc:%sfrom line '%s'\n", 903$_,$_)unless$quiet; 904 905push@cc,$_; 906 907}elsif(!defined$subject) { 908$subject=$_; 909} 910} 911 912# A whitespace line will terminate the headers 913if(m/^\s*$/) { 914$header_done=1; 915} 916}else{ 917$message.=$_; 918if(/^(Signed-off-by|Cc): (.*)$/i) { 919next if($suppress_cc{'sob'}); 920chomp; 921my$c=$2; 922chomp$c; 923next if($ceq$senderand$suppress_cc{'self'}); 924push@cc,$c; 925printf("(sob) Adding cc:%sfrom line '%s'\n", 926$c,$_)unless$quiet; 927} 928} 929} 930close F; 931 932if(defined$cc_cmd&& !$suppress_cc{'cccmd'}) { 933open(F,"$cc_cmd$t|") 934or die"(cc-cmd) Could not execute '$cc_cmd'"; 935while(<F>) { 936my$c=$_; 937$c=~s/^\s*//g; 938$c=~s/\n$//g; 939next if($ceq$senderand$suppress_from); 940push@cc,$c; 941printf("(cc-cmd) Adding cc:%sfrom: '%s'\n", 942$c,$cc_cmd)unless$quiet; 943} 944close F 945or die"(cc-cmd) failed to close pipe to '$cc_cmd'"; 946} 947 948if(defined$author) { 949$message="From:$author\n\n$message"; 950if(defined$author_encoding) { 951if($has_content_type) { 952if($body_encodingeq$author_encoding) { 953# ok, we already have the right encoding 954} 955else{ 956# uh oh, we should re-encode 957} 958} 959else{ 960push@xh, 961'MIME-Version: 1.0', 962"Content-Type: text/plain; charset=$author_encoding", 963'Content-Transfer-Encoding: 8bit'; 964} 965} 966} 967 968 send_message(); 969 970# set up for the next message 971if($chain_reply_to|| !defined$reply_to||length($reply_to) ==0) { 972$reply_to=$message_id; 973if(length$references>0) { 974$references.="\n$message_id"; 975}else{ 976$references="$message_id"; 977} 978} 979$message_id=undef; 980} 981 982if($compose) { 983 cleanup_compose_files(); 984} 985 986sub cleanup_compose_files() { 987unlink($compose_filename,$compose_filename.".final"); 988 989} 990 991$smtp->quitif$smtp; 992 993sub unique_email_list(@) { 994my%seen; 995my@emails; 996 997foreachmy$entry(@_) { 998if(my$clean= extract_valid_address($entry)) { 999$seen{$clean} ||=0;1000next if$seen{$clean}++;1001push@emails,$entry;1002}else{1003print STDERR "W: unable to extract a valid address",1004" from:$entry\n";1005}1006}1007return@emails;1008}10091010sub validate_patch {1011my$fn=shift;1012open(my$fh,'<',$fn)1013or die"unable to open$fn:$!\n";1014while(my$line= <$fh>) {1015if(length($line) >998) {1016return"$.: patch contains a line longer than 998 characters";1017}1018}1019returnundef;1020}10211022sub file_has_nonascii {1023my$fn=shift;1024open(my$fh,'<',$fn)1025or die"unable to open$fn:$!\n";1026while(my$line= <$fh>) {1027return1if$line=~/[^[:ascii:]]/;1028}1029return0;1030}