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