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