blob: 69587856df1706a2da42d6ce2a87c25a054e9eb6 [file] [log] [blame]
Ævar Arnfjörð Bjarmason3328ace2010-09-24 20:00:53 +00001#!/usr/bin/perl
Ryan Anderson83b24432005-07-31 04:17:25 -04002#
Ryan Andersonf3d9f352005-07-31 20:04:24 -04003# Copyright 2002,2005 Greg Kroah-Hartman <greg@kroah.com>
4# Copyright 2005 Ryan Anderson <ryan@michonline.com>
Ryan Anderson83b24432005-07-31 04:17:25 -04005#
6# GPL v2 (See COPYING)
Junio C Hamano5825e5b2005-07-31 23:05:16 -07007#
Ryan Anderson83b24432005-07-31 04:17:25 -04008# Ported to support git "mbox" format files by Ryan Anderson <ryan@michonline.com>
9#
Ryan Andersonf3d9f352005-07-31 20:04:24 -040010# Sends a collection of emails to the given email addresses, disturbingly fast.
Junio C Hamano5825e5b2005-07-31 23:05:16 -070011#
Ryan Andersonf3d9f352005-07-31 20:04:24 -040012# 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:
Junio C Hamano5825e5b2005-07-31 23:05:16 -070015# first line of the message is who to CC,
Ryan Andersonf3d9f352005-07-31 20:04:24 -040016# and second line is the subject of the message.
Junio C Hamano5825e5b2005-07-31 23:05:16 -070017#
Ryan Anderson83b24432005-07-31 04:17:25 -040018
Ævar Arnfjörð Bjarmasond48b2842010-09-24 20:00:52 +000019use 5.008;
Ryan Anderson83b24432005-07-31 04:17:25 -040020use strict;
21use warnings;
Eric Wongf916ab02016-04-06 20:07:14 +000022use POSIX qw/strftime/;
Ryan Anderson83b24432005-07-31 04:17:25 -040023use Term::ReadLine;
Ryan Anderson83b24432005-07-31 04:17:25 -040024use Getopt::Long;
Wu Fengguang0e73b3e2008-12-19 16:10:10 +080025use Text::ParseWords;
Sean Estabrooks412876d2007-08-17 17:38:25 -040026use Term::ANSIColor;
Jay Soffianeed6ca72009-02-14 23:32:13 -050027use File::Temp qw/ tempdir tempfile /;
Ævar Arnfjörð Bjarmason89bf1ba2010-09-14 19:02:24 +000028use File::Spec::Functions qw(catfile);
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +010029use Error qw(:try);
Petr Baudis3cb8caf2006-07-03 22:47:58 +020030use Git;
Ryan Anderson83b24432005-07-31 04:17:25 -040031
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +010032Getopt::Long::Configure qw/ pass_through /;
33
Junio C Hamano280242d2006-07-02 16:03:59 -070034package FakeTerm;
35sub new {
36 my ($class, $reason) = @_;
37 return bless \$reason, shift;
38}
39sub readline {
40 my $self = shift;
41 die "Cannot use readline on FakeTerm: $$self";
42}
43package main;
44
Michael Coleman1b0baf12007-02-27 22:47:54 -060045
46sub usage {
47 print <<EOT;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +010048git send-email [options] <file | directory | rev-list options >
Jacob Keller17b7a832015-11-19 14:52:11 -080049git send-email --dump-aliases
Michael Witten4ed62b02008-09-30 07:58:30 -050050
51 Composing:
52 --from <str> * Email From:
Stephen Boydf434c082010-03-07 14:46:48 -080053 --[no-]to <str> * Email To:
54 --[no-]cc <str> * Email Cc:
55 --[no-]bcc <str> * Email Bcc:
Michael Witten4ed62b02008-09-30 07:58:30 -050056 --subject <str> * Email "Subject:"
57 --in-reply-to <str> * Email "In-Reply-To:"
Luis Henriquesac1596a2014-03-24 21:38:27 +000058 --[no-]xmailer * Add "X-Mailer:" header (default).
Felipe Contreras402596a2013-04-07 01:10:27 -060059 --[no-]annotate * Review each patch that will be sent in an editor.
Michael Witten4ed62b02008-09-30 07:58:30 -050060 --compose * Open an editor for introduction.
Krzysztof Mazur62e00692012-10-10 01:02:56 +020061 --compose-encoding <str> * Encoding to assume for introduction.
Thomas Rast3cae7e52010-06-17 22:10:39 +020062 --8bit-encoding <str> * Encoding to assume 8bit mails if undeclared
Paolo Bonzini8d814082014-11-25 15:00:27 +010063 --transfer-encoding <str> * Transfer encoding to use (quoted-printable, 8bit, base64)
Michael Witten4ed62b02008-09-30 07:58:30 -050064
65 Sending:
66 --envelope-sender <str> * Email envelope sender.
67 --smtp-server <str:int> * Outgoing SMTP server to use. The port
68 is optional. Default 'localhost'.
Pascal Obry052fbea2010-09-06 20:12:11 +020069 --smtp-server-option <str> * Outgoing SMTP server option to use.
Michael Witten4ed62b02008-09-30 07:58:30 -050070 --smtp-server-port <int> * Outgoing SMTP server port.
71 --smtp-user <str> * Username for SMTP-AUTH.
72 --smtp-pass <str> * Password for SMTP-AUTH; not necessary.
73 --smtp-encryption <str> * tls or ssl; anything else disables.
74 --smtp-ssl * Deprecated. Use '--smtp-encryption ssl'.
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -070075 --smtp-ssl-cert-path <str> * Path to ca-certificates (either directory or file).
76 Pass an empty string to disable certificate
77 verification.
Jari Aalto134550f2010-03-14 17:16:45 +020078 --smtp-domain <str> * The domain name sent to HELO/EHLO handshake
Jan Viktorin0f2e68b2015-08-12 01:39:44 +020079 --smtp-auth <str> * Space-separated list of allowed AUTH mechanisms.
80 This setting forces to use one of the listed mechanisms.
Jari Aaltof60812e2010-03-14 17:16:09 +020081 --smtp-debug <0|1> * Disable, enable Net::SMTP debug.
Michael Witten4ed62b02008-09-30 07:58:30 -050082
83 Automating:
84 --identity <str> * Use the sendemail.<id> options.
Joe Perches6e74e072010-09-24 10:03:00 -070085 --to-cmd <str> * Email To: via `<str> \$patch_path`
Michael Witten4ed62b02008-09-30 07:58:30 -050086 --cc-cmd <str> * Email Cc: via `<str> \$patch_path`
Jay Soffian3531e272009-02-14 23:32:15 -050087 --suppress-cc <str> * author, self, sob, cc, cccmd, body, bodycc, all.
Michael S. Tsirkinf515c902014-04-29 08:41:16 +030088 --[no-]cc-cover * Email Cc: addresses in the cover letter.
89 --[no-]to-cover * Email To: addresses in the cover letter.
Jay Soffian3531e272009-02-14 23:32:15 -050090 --[no-]signed-off-by-cc * Send to Signed-off-by: addresses. Default on.
Michael Witten4ed62b02008-09-30 07:58:30 -050091 --[no-]suppress-from * Send to self. Default off.
Junio C Hamano41fe87f2009-08-22 12:48:48 -070092 --[no-]chain-reply-to * Chain In-Reply-To: fields. Default off.
Michael Witten4ed62b02008-09-30 07:58:30 -050093 --[no-]thread * Use In-Reply-To: field. Default on.
94
95 Administering:
Jay Soffianc1f2aa42009-03-02 23:52:18 -050096 --confirm <str> * Confirm recipients before sending;
97 auto, cc, compose, always, or never.
Michael Witten4ed62b02008-09-30 07:58:30 -050098 --quiet * Output one line of info per email.
99 --dry-run * Don't actually send the emails.
100 --[no-]validate * Perform patch sanity checks. Default on.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100101 --[no-]format-patch * understand any non optional arguments as
102 `git format-patch` ones.
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200103 --force * Send even if safety checks would prevent it.
Jeff Kingc764a0c2008-01-18 09:20:10 -0500104
Jacob Keller17b7a832015-11-19 14:52:11 -0800105 Information:
106 --dump-aliases * Dump configured aliases and exit.
107
Michael Coleman1b0baf12007-02-27 22:47:54 -0600108EOT
109 exit(1);
110}
111
Eric Wong4bc87a22006-03-25 17:20:48 -0800112# most mail servers generate the Date: header, but not all...
Jakub Narebski6bdca892006-07-07 20:57:55 +0200113sub format_2822_time {
114 my ($time) = @_;
115 my @localtm = localtime($time);
116 my @gmttm = gmtime($time);
117 my $localmin = $localtm[1] + $localtm[2] * 60;
118 my $gmtmin = $gmttm[1] + $gmttm[2] * 60;
119 if ($localtm[0] != $gmttm[0]) {
120 die "local zone differs from GMT by a non-minute interval\n";
121 }
122 if ((($gmttm[6] + 1) % 7) == $localtm[6]) {
123 $localmin += 1440;
124 } elsif ((($gmttm[6] - 1) % 7) == $localtm[6]) {
125 $localmin -= 1440;
126 } elsif ($gmttm[6] != $localtm[6]) {
127 die "local time offset greater than or equal to 24 hours\n";
128 }
129 my $offset = $localmin - $gmtmin;
130 my $offhour = $offset / 60;
131 my $offmin = abs($offset % 60);
132 if (abs($offhour) >= 24) {
133 die ("local time offset greater than or equal to 24 hours\n");
134 }
135
136 return sprintf("%s, %2d %s %d %02d:%02d:%02d %s%02d%02d",
137 qw(Sun Mon Tue Wed Thu Fri Sat)[$localtm[6]],
138 $localtm[3],
139 qw(Jan Feb Mar Apr May Jun
140 Jul Aug Sep Oct Nov Dec)[$localtm[4]],
141 $localtm[5]+1900,
142 $localtm[2],
143 $localtm[1],
144 $localtm[0],
145 ($offset >= 0) ? '+' : '-',
146 abs($offhour),
147 $offmin,
148 );
149}
Eric Wong4bc87a22006-03-25 17:20:48 -0800150
Eric Wong567ffeb2006-03-25 16:47:12 -0800151my $have_email_valid = eval { require Email::Valid; 1 };
Jay Soffian50126992009-02-14 23:32:14 -0500152my $have_mail_address = eval { require Mail::Address; 1 };
Eric Wong4bc87a22006-03-25 17:20:48 -0800153my $smtp;
Wincent Colaiuta5f5b6112007-11-21 13:35:05 +0100154my $auth;
Eric Wong4bc87a22006-03-25 17:20:48 -0800155
Роман Донченко11f70a72014-12-14 18:59:46 +0300156# Regexes for RFC 2047 productions.
157my $re_token = qr/[^][()<>@,;:\\"\/?.= \000-\037\177-\377]+/;
158my $re_encoded_text = qr/[^? \000-\037\177-\377]+/;
159my $re_encoded_word = qr/=\?($re_token)\?($re_token)\?($re_encoded_text)\?=/;
160
Ryan Anderson83b24432005-07-31 04:17:25 -0400161# Variables we fill in automatically, or via prompting:
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700162my (@to,$no_to,@initial_to,@cc,$no_cc,@initial_cc,@bcclist,$no_bcc,@xh,
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100163 $initial_reply_to,$initial_subject,@files,
Luis Henriquesac1596a2014-03-24 21:38:27 +0000164 $author,$sender,$smtp_authpass,$annotate,$use_xmailer,$compose,$time);
Ryan Anderson83b24432005-07-31 04:17:25 -0400165
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700166my $envelope_sender;
Ryan Anderson78488b22005-07-31 20:04:24 -0400167
Ryan Anderson91332612005-07-31 20:04:24 -0400168# Example reply to:
Ryan Anderson83b24432005-07-31 04:17:25 -0400169#$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
Ryan Anderson83b24432005-07-31 04:17:25 -0400170
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100171my $repo = eval { Git->repository() };
172my @repo = $repo ? ($repo) : ();
Junio C Hamano280242d2006-07-02 16:03:59 -0700173my $term = eval {
Jay Soffian0fb7fc72008-02-21 19:16:04 -0500174 $ENV{"GIT_SEND_EMAIL_NOTTY"}
175 ? new Term::ReadLine 'git-send-email', \*STDIN, \*STDOUT
176 : new Term::ReadLine 'git-send-email';
Junio C Hamano280242d2006-07-02 16:03:59 -0700177};
178if ($@) {
179 $term = new FakeTerm "$@: going non-interactive";
180}
Ryan Anderson83b24432005-07-31 04:17:25 -0400181
Adam Roben5483c712007-06-27 20:59:37 -0700182# Behavior modification variables
183my ($quiet, $dry_run) = (0, 0);
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100184my $format_patch;
Jay Soffianafe756c2009-02-23 13:51:37 -0500185my $compose_filename;
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200186my $force = 0;
Jacob Keller17b7a832015-11-19 14:52:11 -0800187my $dump_aliases = 0;
Adam Roben5483c712007-06-27 20:59:37 -0700188
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100189# Handle interactive edition of files.
190my $multiedit;
Michael J Gruber0ce142c2010-03-22 17:12:53 +0100191my $editor;
Jonathan Niederb4479f02009-10-30 20:42:34 -0500192
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100193sub do_edit {
Michael J Gruber0ce142c2010-03-22 17:12:53 +0100194 if (!defined($editor)) {
195 $editor = Git::command_oneline('var', 'GIT_EDITOR');
196 }
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100197 if (defined($multiedit) && !$multiedit) {
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100198 map {
199 system('sh', '-c', $editor.' "$@"', $editor, $_);
200 if (($? & 127) || ($? >> 8)) {
201 die("the editor exited uncleanly, aborting everything");
202 }
203 } @_;
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100204 } else {
205 system('sh', '-c', $editor.' "$@"', $editor, @_);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100206 if (($? & 127) || ($? >> 8)) {
207 die("the editor exited uncleanly, aborting everything");
208 }
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100209 }
210}
Adam Roben5483c712007-06-27 20:59:37 -0700211
212# Variables with corresponding config settings
Joe Perches6e74e072010-09-24 10:03:00 -0700213my ($thread, $chain_reply_to, $suppress_from, $signed_off_by_cc);
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300214my ($cover_cc, $cover_to);
Joe Perches6e74e072010-09-24 10:03:00 -0700215my ($to_cmd, $cc_cmd);
Pascal Obry052fbea2010-09-06 20:12:11 +0200216my ($smtp_server, $smtp_server_port, @smtp_server_options);
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -0700217my ($smtp_authuser, $smtp_encryption, $smtp_ssl_cert_path);
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200218my ($identity, $aliasfiletype, @alias_files, $smtp_domain, $smtp_auth);
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500219my ($validate, $confirm);
David Brown65648282007-12-25 19:56:29 -0800220my (@suppress_cc);
Thomas Rast3cae7e52010-06-17 22:10:39 +0200221my ($auto_8bit_encoding);
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200222my ($compose_encoding);
Paolo Bonzini8d814082014-11-25 15:00:27 +0100223my ($target_xfer_encoding);
Adam Roben5483c712007-06-27 20:59:37 -0700224
Jari Aaltof60812e2010-03-14 17:16:09 +0200225my ($debug_net_smtp) = 0; # Net::SMTP, see send_message()
226
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900227my %config_bool_settings = (
Adam Roben5483c712007-06-27 20:59:37 -0700228 "thread" => [\$thread, 1],
Felipe Contrerasb99d22f2013-05-24 22:44:52 -0500229 "chainreplyto" => [\$chain_reply_to, 0],
David Brown65648282007-12-25 19:56:29 -0800230 "suppressfrom" => [\$suppress_from, undef],
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500231 "signedoffbycc" => [\$signed_off_by_cc, undef],
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300232 "cccover" => [\$cover_cc, undef],
233 "tocover" => [\$cover_to, undef],
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500234 "signedoffcc" => [\$signed_off_by_cc, undef], # Deprecated
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500235 "validate" => [\$validate, 1],
Felipe Contreras402596a2013-04-07 01:10:27 -0600236 "multiedit" => [\$multiedit, undef],
Luis Henriquesac1596a2014-03-24 21:38:27 +0000237 "annotate" => [\$annotate, undef],
238 "xmailer" => [\$use_xmailer, 1]
Adam Robene46f7a02007-06-26 15:48:30 -0700239);
240
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900241my %config_settings = (
242 "smtpserver" => \$smtp_server,
Junio C Hamano44b24762007-09-25 17:27:54 -0700243 "smtpserverport" => \$smtp_server_port,
Pascal Obry052fbea2010-09-06 20:12:11 +0200244 "smtpserveroption" => \@smtp_server_options,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900245 "smtpuser" => \$smtp_authuser,
246 "smtppass" => \$smtp_authpass,
Pascal Obrye1e91152010-09-06 20:12:09 +0200247 "smtpdomain" => \$smtp_domain,
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200248 "smtpauth" => \$smtp_auth,
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700249 "to" => \@initial_to,
Joe Perches6e74e072010-09-24 10:03:00 -0700250 "tocmd" => \$to_cmd,
Miklos Vajna5f8b9fc2008-04-27 14:14:58 +0200251 "cc" => \@initial_cc,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900252 "cccmd" => \$cc_cmd,
253 "aliasfiletype" => \$aliasfiletype,
254 "bcc" => \@bcclist,
David Brown65648282007-12-25 19:56:29 -0800255 "suppresscc" => \@suppress_cc,
Ask Bjørn Hansen9f7820a2008-06-07 00:33:42 -0700256 "envelopesender" => \$envelope_sender,
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500257 "confirm" => \$confirm,
Trent Piepho09caa242009-05-12 15:48:56 -0700258 "from" => \$sender,
Thomas Rast3cae7e52010-06-17 22:10:39 +0200259 "assume8bitencoding" => \$auto_8bit_encoding,
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200260 "composeencoding" => \$compose_encoding,
Paolo Bonzini8d814082014-11-25 15:00:27 +0100261 "transferencoding" => \$target_xfer_encoding,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900262);
Avi Kivity4a62d3f2007-03-11 19:19:44 +0200263
Cord Seelecec5dae2011-09-30 12:52:25 +0200264my %config_path_settings = (
265 "aliasesfile" => \@alias_files,
John Keeping6e07a3b2015-11-17 22:01:05 +0000266 "smtpsslcertpath" => \$smtp_ssl_cert_path,
Cord Seelecec5dae2011-09-30 12:52:25 +0200267);
268
Michael Witten87429972008-02-03 19:53:57 -0500269# Handle Uncouth Termination
270sub signal_handler {
271
272 # Make text normal
273 print color("reset"), "\n";
274
275 # SMTP password masked
276 system "stty echo";
277
278 # tmp files from --compose
Jay Soffianafe756c2009-02-23 13:51:37 -0500279 if (defined $compose_filename) {
280 if (-e $compose_filename) {
281 print "'$compose_filename' contains an intermediate version of the email you were composing.\n";
282 }
283 if (-e ($compose_filename . ".final")) {
284 print "'$compose_filename.final' contains the composed email.\n"
285 }
Michael Witten87429972008-02-03 19:53:57 -0500286 }
287
288 exit;
289};
290
291$SIG{TERM} = \&signal_handler;
292$SIG{INT} = \&signal_handler;
293
Ryan Anderson83b24432005-07-31 04:17:25 -0400294# Begin by accumulating all the variables (defined above), that we will end up
295# needing, first, from the command line:
296
Clemens Buchacherc5978242011-09-03 19:06:13 +0200297my $help;
298my $rc = GetOptions("h" => \$help,
Jacob Keller17b7a832015-11-19 14:52:11 -0800299 "dump-aliases" => \$dump_aliases);
300usage() unless $rc;
301die "--dump-aliases incompatible with other options\n"
302 if !$help and $dump_aliases and @ARGV;
303$rc = GetOptions(
Clemens Buchacherc5978242011-09-03 19:06:13 +0200304 "sender|from=s" => \$sender,
Ryan Anderson83b24432005-07-31 04:17:25 -0400305 "in-reply-to=s" => \$initial_reply_to,
306 "subject=s" => \$initial_subject,
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700307 "to=s" => \@initial_to,
Joe Perches6e74e072010-09-24 10:03:00 -0700308 "to-cmd=s" => \$to_cmd,
Stephen Boydf434c082010-03-07 14:46:48 -0800309 "no-to" => \$no_to,
Ryan Andersonda140f82006-02-13 03:05:15 -0500310 "cc=s" => \@initial_cc,
Stephen Boydf434c082010-03-07 14:46:48 -0800311 "no-cc" => \$no_cc,
Ryan Anderson58063242006-05-29 12:30:13 -0700312 "bcc=s" => \@bcclist,
Stephen Boydf434c082010-03-07 14:46:48 -0800313 "no-bcc" => \$no_bcc,
Ryan Anderson78488b22005-07-31 20:04:24 -0400314 "chain-reply-to!" => \$chain_reply_to,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800315 "no-chain-reply-to" => sub {$chain_reply_to = 0},
Ryan Anderson3342d852005-07-31 20:04:24 -0400316 "smtp-server=s" => \$smtp_server,
Pascal Obry052fbea2010-09-06 20:12:11 +0200317 "smtp-server-option=s" => \@smtp_server_options,
Junio C Hamano44b24762007-09-25 17:27:54 -0700318 "smtp-server-port=s" => \$smtp_server_port,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900319 "smtp-user=s" => \$smtp_authuser,
Michael Witten2363d742008-02-03 19:53:56 -0500320 "smtp-pass:s" => \$smtp_authpass,
Thomas Rastf6bebd12008-06-25 21:42:43 +0200321 "smtp-ssl" => sub { $smtp_encryption = 'ssl' },
322 "smtp-encryption=s" => \$smtp_encryption,
Thomas Rast979e6522013-12-01 23:48:42 +0100323 "smtp-ssl-cert-path=s" => \$smtp_ssl_cert_path,
Jari Aaltof60812e2010-03-14 17:16:09 +0200324 "smtp-debug:i" => \$debug_net_smtp,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -0400325 "smtp-domain:s" => \$smtp_domain,
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200326 "smtp-auth=s" => \$smtp_auth,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900327 "identity=s" => \$identity,
Felipe Contreras402596a2013-04-07 01:10:27 -0600328 "annotate!" => \$annotate,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800329 "no-annotate" => sub {$annotate = 0},
Ryan Anderson1f038a02005-09-05 01:13:07 -0400330 "compose" => \$compose,
Ryan Anderson30d08b32006-02-02 11:56:06 -0500331 "quiet" => \$quiet,
Joe Perches324a8bd2007-08-17 18:51:12 -0700332 "cc-cmd=s" => \$cc_cmd,
Adam Roben5483c712007-06-27 20:59:37 -0700333 "suppress-from!" => \$suppress_from,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800334 "no-suppress-from" => sub {$suppress_from = 0},
David Brown65648282007-12-25 19:56:29 -0800335 "suppress-cc=s" => \@suppress_cc,
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500336 "signed-off-cc|signed-off-by-cc!" => \$signed_off_by_cc,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800337 "no-signed-off-cc|no-signed-off-by-cc" => sub {$signed_off_by_cc = 0},
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300338 "cc-cover|cc-cover!" => \$cover_cc,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800339 "no-cc-cover" => sub {$cover_cc = 0},
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300340 "to-cover|to-cover!" => \$cover_to,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800341 "no-to-cover" => sub {$cover_to = 0},
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500342 "confirm=s" => \$confirm,
Matthew Wilcox61302592006-10-10 08:58:23 -0600343 "dry-run" => \$dry_run,
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700344 "envelope-sender=s" => \$envelope_sender,
Adam Roben5483c712007-06-27 20:59:37 -0700345 "thread!" => \$thread,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800346 "no-thread" => sub {$thread = 0},
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500347 "validate!" => \$validate,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800348 "no-validate" => sub {$validate = 0},
Paolo Bonzini8d814082014-11-25 15:00:27 +0100349 "transfer-encoding=s" => \$target_xfer_encoding,
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100350 "format-patch!" => \$format_patch,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800351 "no-format-patch" => sub {$format_patch = 0},
Thomas Rast3cae7e52010-06-17 22:10:39 +0200352 "8bit-encoding=s" => \$auto_8bit_encoding,
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200353 "compose-encoding=s" => \$compose_encoding,
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200354 "force" => \$force,
Luis Henriquesac1596a2014-03-24 21:38:27 +0000355 "xmailer!" => \$use_xmailer,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800356 "no-xmailer" => sub {$use_xmailer = 0},
Ryan Anderson83b24432005-07-31 04:17:25 -0400357 );
358
Clemens Buchacherc5978242011-09-03 19:06:13 +0200359usage() if $help;
Michael Coleman1b0baf12007-02-27 22:47:54 -0600360unless ($rc) {
361 usage();
362}
363
Jay Soffianeed6ca72009-02-14 23:32:13 -0500364die "Cannot run git format-patch from outside a repository\n"
365 if $format_patch and not $repo;
366
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900367# Now, let's fill any that aren't set in with defaults:
368
369sub read_config {
370 my ($prefix) = @_;
371
372 foreach my $setting (keys %config_bool_settings) {
373 my $target = $config_bool_settings{$setting}->[0];
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100374 $$target = Git::config_bool(@repo, "$prefix.$setting") unless (defined $$target);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900375 }
376
Cord Seelecec5dae2011-09-30 12:52:25 +0200377 foreach my $setting (keys %config_path_settings) {
Cord Seele463b0ea2011-10-14 22:53:31 +0200378 my $target = $config_path_settings{$setting};
379 if (ref($target) eq "ARRAY") {
380 unless (@$target) {
381 my @values = Git::config_path(@repo, "$prefix.$setting");
382 @$target = @values if (@values && defined $values[0]);
383 }
384 }
385 else {
386 $$target = Git::config_path(@repo, "$prefix.$setting") unless (defined $$target);
387 }
Cord Seelecec5dae2011-09-30 12:52:25 +0200388 }
389
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900390 foreach my $setting (keys %config_settings) {
391 my $target = $config_settings{$setting};
Stephen Boydf434c082010-03-07 14:46:48 -0800392 next if $setting eq "to" and defined $no_to;
393 next if $setting eq "cc" and defined $no_cc;
394 next if $setting eq "bcc" and defined $no_bcc;
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900395 if (ref($target) eq "ARRAY") {
396 unless (@$target) {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100397 my @values = Git::config(@repo, "$prefix.$setting");
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900398 @$target = @values if (@values && defined $values[0]);
399 }
400 }
401 else {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100402 $$target = Git::config(@repo, "$prefix.$setting") unless (defined $$target);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900403 }
404 }
Thomas Rastf6bebd12008-06-25 21:42:43 +0200405
406 if (!defined $smtp_encryption) {
407 my $enc = Git::config(@repo, "$prefix.smtpencryption");
408 if (defined $enc) {
409 $smtp_encryption = $enc;
410 } elsif (Git::config_bool(@repo, "$prefix.smtpssl")) {
411 $smtp_encryption = 'ssl';
412 }
413 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900414}
415
416# read configuration from [sendemail "$identity"], fall back on [sendemail]
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100417$identity = Git::config(@repo, "sendemail.identity") unless (defined $identity);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900418read_config("sendemail.$identity") if (defined $identity);
419read_config("sendemail");
420
421# fall back on builtin bool defaults
422foreach my $setting (values %config_bool_settings) {
423 ${$setting->[0]} = $setting->[1] unless (defined (${$setting->[0]}));
424}
425
Thomas Rastfa835cd2008-06-26 23:03:21 +0200426# 'default' encryption is none -- this only prevents a warning
427$smtp_encryption = '' unless (defined $smtp_encryption);
428
David Brown65648282007-12-25 19:56:29 -0800429# Set CC suppressions
430my(%suppress_cc);
431if (@suppress_cc) {
432 foreach my $entry (@suppress_cc) {
433 die "Unknown --suppress-cc field: '$entry'\n"
Ævar Arnfjörð Bjarmasone9bf7412010-09-30 13:43:04 +0000434 unless $entry =~ /^(?:all|cccmd|cc|author|self|sob|body|bodycc)$/;
David Brown65648282007-12-25 19:56:29 -0800435 $suppress_cc{$entry} = 1;
436 }
437}
438
439if ($suppress_cc{'all'}) {
Paolo Bonzinicb8a9bd2009-06-18 14:31:32 +0200440 foreach my $entry (qw (cccmd cc author self sob body bodycc)) {
David Brown65648282007-12-25 19:56:29 -0800441 $suppress_cc{$entry} = 1;
442 }
443 delete $suppress_cc{'all'};
444}
445
446# If explicit old-style ones are specified, they trump --suppress-cc.
447$suppress_cc{'self'} = $suppress_from if defined $suppress_from;
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500448$suppress_cc{'sob'} = !$signed_off_by_cc if defined $signed_off_by_cc;
David Brown65648282007-12-25 19:56:29 -0800449
Jay Soffian3531e272009-02-14 23:32:15 -0500450if ($suppress_cc{'body'}) {
451 foreach my $entry (qw (sob bodycc)) {
452 $suppress_cc{$entry} = 1;
453 }
454 delete $suppress_cc{'body'};
455}
456
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500457# Set confirm's default value
458my $confirm_unconfigured = !defined $confirm;
459if ($confirm_unconfigured) {
460 $confirm = scalar %suppress_cc ? 'compose' : 'auto';
461};
462die "Unknown --confirm setting: '$confirm'\n"
463 unless $confirm =~ /^(?:auto|cc|compose|always|never)/;
464
David Brown65648282007-12-25 19:56:29 -0800465# Debugging, print out the suppressions.
466if (0) {
467 print "suppressions:\n";
468 foreach my $entry (keys %suppress_cc) {
469 printf " %-5s -> $suppress_cc{$entry}\n", $entry;
470 }
471}
472
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100473my ($repoauthor, $repocommitter);
474($repoauthor) = Git::ident_person(@repo, 'author');
475($repocommitter) = Git::ident_person(@repo, 'committer');
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900476
Jay Soffian50126992009-02-14 23:32:14 -0500477sub parse_address_line {
478 if ($have_mail_address) {
479 return map { $_->format } Mail::Address->parse($_[0]);
480 } else {
Remi Lespinet8d314d72015-07-07 15:38:15 +0200481 return Git::parse_mailboxes($_[0]);
Jay Soffian50126992009-02-14 23:32:14 -0500482 }
483}
484
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800485sub split_addrs {
Junio C Hamano2f0e7cb2008-12-21 01:57:59 -0800486 return quotewords('\s*,\s*', 1, @_);
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800487}
488
Eric Wong994d6c62006-05-14 19:13:44 -0700489my %aliases;
Eric Sunshine09f11572015-05-31 18:29:27 -0400490
491sub parse_sendmail_alias {
492 local $_ = shift;
493 if (/"/) {
494 print STDERR "warning: sendmail alias with quotes is not supported: $_\n";
Eric Sunshine86b89842015-06-01 14:22:36 -0400495 } elsif (/:include:/) {
496 print STDERR "warning: `:include:` not supported: $_\n";
497 } elsif (/[\/|]/) {
498 print STDERR "warning: `/file` or `|pipe` redirection not supported: $_\n";
Eric Sunshine09f11572015-05-31 18:29:27 -0400499 } elsif (/^(\S+?)\s*:\s*(.+)$/) {
500 my ($alias, $addr) = ($1, $2);
501 $aliases{$alias} = [ split_addrs($addr) ];
502 } else {
503 print STDERR "warning: sendmail line is not recognized: $_\n";
504 }
505}
506
507sub parse_sendmail_aliases {
508 my $fh = shift;
Eric Sunshine2532dd02015-05-31 18:29:29 -0400509 my $s = '';
Eric Sunshine09f11572015-05-31 18:29:27 -0400510 while (<$fh>) {
Eric Sunshine2532dd02015-05-31 18:29:29 -0400511 chomp;
Eric Sunshine020be85f2015-05-31 18:29:28 -0400512 next if /^\s*$/ || /^\s*#/;
Eric Sunshine2532dd02015-05-31 18:29:29 -0400513 $s .= $_, next if $s =~ s/\\$// || s/^\s+//;
514 parse_sendmail_alias($s) if $s;
515 $s = $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400516 }
Eric Sunshine2532dd02015-05-31 18:29:29 -0400517 $s =~ s/\\$//; # silently tolerate stray '\' on last line
518 parse_sendmail_alias($s) if $s;
Eric Sunshine09f11572015-05-31 18:29:27 -0400519}
520
Eric Wong994d6c62006-05-14 19:13:44 -0700521my %parse_alias = (
522 # multiline formats can be supported in the future
523 mutt => sub { my $fh = shift; while (<$fh>) {
Felipe Contrerasffc01f92009-09-30 17:49:36 +0300524 if (/^\s*alias\s+(?:-group\s+\S+\s+)*(\S+)\s+(.*)$/) {
Eric Wong994d6c62006-05-14 19:13:44 -0700525 my ($alias, $addr) = ($1, $2);
526 $addr =~ s/#.*$//; # mutt allows # comments
Eric Wong2c510f22016-01-04 20:53:30 +0000527 # commas delimit multiple addresses
528 my @addr = split_addrs($addr);
529
530 # quotes may be escaped in the file,
531 # unescape them so we do not double-escape them later.
532 s/\\"/"/g foreach @addr;
533 $aliases{$alias} = \@addr
Eric Wong994d6c62006-05-14 19:13:44 -0700534 }}},
535 mailrc => sub { my $fh = shift; while (<$fh>) {
Jeff Kinga277d1e2016-03-17 19:58:22 -0400536 if (/^alias\s+(\S+)\s+(.*?)\s*$/) {
Eric Wong994d6c62006-05-14 19:13:44 -0700537 # spaces delimit multiple addresses
Eric W. Biedermanfe87c922009-05-20 19:45:53 -0700538 $aliases{$1} = [ quotewords('\s+', 0, $2) ];
Eric Wong994d6c62006-05-14 19:13:44 -0700539 }}},
Trent Piepho73c427e2008-11-25 18:55:00 -0800540 pine => sub { my $fh = shift; my $f='\t[^\t]*';
541 for (my $x = ''; defined($x); $x = $_) {
542 chomp $x;
543 $x .= $1 while(defined($_ = <$fh>) && /^ +(.*)$/);
544 $x =~ /^(\S+)$f\t\(?([^\t]+?)\)?(:?$f){0,2}$/ or next;
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800545 $aliases{$1} = [ split_addrs($2) ];
Trent Piepho73c427e2008-11-25 18:55:00 -0800546 }},
Bill Pemberton7613ea32009-04-22 09:41:29 -0400547 elm => sub { my $fh = shift;
548 while (<$fh>) {
549 if (/^(\S+)\s+=\s+[^=]+=\s(\S+)/) {
550 my ($alias, $addr) = ($1, $2);
551 $aliases{$alias} = [ split_addrs($addr) ];
552 }
553 } },
Eric Sunshine09f11572015-05-31 18:29:27 -0400554 sendmail => \&parse_sendmail_aliases,
Eric Wong994d6c62006-05-14 19:13:44 -0700555 gnus => sub { my $fh = shift; while (<$fh>) {
556 if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
557 $aliases{$1} = [ $2 ];
558 }}}
559);
560
Petr Baudis3cb8caf2006-07-03 22:47:58 +0200561if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) {
Eric Wong994d6c62006-05-14 19:13:44 -0700562 foreach my $file (@alias_files) {
563 open my $fh, '<', $file or die "opening $file: $!\n";
564 $parse_alias{$aliasfiletype}->($fh);
565 close $fh;
566 }
567}
568
Jacob Keller17b7a832015-11-19 14:52:11 -0800569if ($dump_aliases) {
570 print "$_\n" for (sort keys %aliases);
571 exit(0);
572}
573
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700574# is_format_patch_arg($f) returns 0 if $f names a patch, or 1 if
575# $f is a revision list specification to be passed to format-patch.
576sub is_format_patch_arg {
Jay Soffianeed6ca72009-02-14 23:32:13 -0500577 return unless $repo;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100578 my $f = shift;
579 try {
580 $repo->command('rev-parse', '--verify', '--quiet', $f);
581 if (defined($format_patch)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100582 return $format_patch;
583 }
584 die(<<EOF);
585File '$f' exists but it could also be the range of commits
586to produce patches for. Please disambiguate by...
587
588 * Saying "./$f" if you mean a file; or
589 * Giving --format-patch option if you mean a range.
590EOF
591 } catch Git::Error::Command with {
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700592 # Not a valid revision. Treat it as a filename.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100593 return 0;
594 }
595}
596
Jeff Kingaa548922008-01-18 09:19:36 -0500597# Now that all the defaults are set, process the rest of the command line
598# arguments and collect up the files that need to be processed.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100599my @rev_list_opts;
Junio C Hamano69f4ce52008-11-30 22:38:20 -0800600while (defined(my $f = shift @ARGV)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100601 if ($f eq "--") {
602 push @rev_list_opts, "--", @ARGV;
603 @ARGV = ();
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700604 } elsif (-d $f and !is_format_patch_arg($f)) {
Ævar Arnfjörð Bjarmasonc6038162010-09-30 13:42:54 +0000605 opendir my $dh, $f
Jeff Kingaa548922008-01-18 09:19:36 -0500606 or die "Failed to opendir $f: $!";
607
Ævar Arnfjörð Bjarmason89bf1ba2010-09-14 19:02:24 +0000608 push @files, grep { -f $_ } map { catfile($f, $_) }
Ævar Arnfjörð Bjarmasonc6038162010-09-30 13:42:54 +0000609 sort readdir $dh;
610 closedir $dh;
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700611 } elsif ((-f $f or -p $f) and !is_format_patch_arg($f)) {
Jeff Kingaa548922008-01-18 09:19:36 -0500612 push @files, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500613 } else {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100614 push @rev_list_opts, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500615 }
616}
617
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100618if (@rev_list_opts) {
Jay Soffianeed6ca72009-02-14 23:32:13 -0500619 die "Cannot run git format-patch from outside a repository\n"
620 unless $repo;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100621 push @files, $repo->command('format-patch', '-o', tempdir(CLEANUP => 1), @rev_list_opts);
622}
623
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500624if ($validate) {
Jeff Kingc764a0c2008-01-18 09:20:10 -0500625 foreach my $f (@files) {
Kevin Ballard300913b2008-06-25 15:44:40 -0700626 unless (-p $f) {
627 my $error = validate_patch($f);
628 $error and die "fatal: $f: $error\nwarning: no patches were sent\n";
629 }
Jeff Kingc764a0c2008-01-18 09:20:10 -0500630 }
Jeff King747bbff2008-01-18 09:19:48 -0500631}
632
Jeff Kingaa548922008-01-18 09:19:36 -0500633if (@files) {
634 unless ($quiet) {
635 print $_,"\n" for (@files);
636 }
637} else {
638 print STDERR "\nNo patch files specified!\n\n";
639 usage();
640}
641
Ævar Arnfjörð Bjarmasonacf071b2010-09-30 13:42:57 +0000642sub get_patch_subject {
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100643 my $fn = shift;
644 open (my $fh, '<', $fn);
645 while (my $line = <$fh>) {
646 next unless ($line =~ /^Subject: (.*)$/);
647 close $fh;
648 return "GIT: $1\n";
649 }
650 close $fh;
651 die "No subject line in $fn ?";
652}
653
654if ($compose) {
655 # Note that this does not need to be secure, but we will make a small
656 # effort to have it be unique
Jay Soffianafe756c2009-02-23 13:51:37 -0500657 $compose_filename = ($repo ?
658 tempfile(".gitsendemail.msg.XXXXXX", DIR => $repo->repo_path()) :
659 tempfile(".gitsendemail.msg.XXXXXX", DIR => "."))[1];
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000660 open my $c, ">", $compose_filename
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100661 or die "Failed to open for writing $compose_filename: $!";
662
663
664 my $tpl_sender = $sender || $repoauthor || $repocommitter || '';
665 my $tpl_subject = $initial_subject || '';
666 my $tpl_reply_to = $initial_reply_to || '';
667
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000668 print $c <<EOT;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100669From $tpl_sender # This line is ignored.
Michael Witten40e6e8a2009-04-13 13:23:50 -0500670GIT: Lines beginning in "GIT:" will be removed.
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100671GIT: Consider including an overall diffstat or table of contents
672GIT: for the patch you are writing.
673GIT:
674GIT: Clear the body content if you don't wish to send a summary.
675From: $tpl_sender
676Subject: $tpl_subject
677In-Reply-To: $tpl_reply_to
678
679EOT
680 for my $f (@files) {
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000681 print $c get_patch_subject($f);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100682 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000683 close $c;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100684
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100685 if ($annotate) {
686 do_edit($compose_filename, @files);
687 } else {
688 do_edit($compose_filename);
689 }
690
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000691 open my $c2, ">", $compose_filename . ".final"
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100692 or die "Failed to open $compose_filename.final : " . $!;
693
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000694 open $c, "<", $compose_filename
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100695 or die "Failed to open $compose_filename : " . $!;
696
697 my $need_8bit_cte = file_has_nonascii($compose_filename);
698 my $in_body = 0;
699 my $summary_empty = 1;
Krzysztof Mazur4a47a4d2012-10-22 14:41:48 +0200700 if (!defined $compose_encoding) {
701 $compose_encoding = "UTF-8";
702 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000703 while(<$c>) {
Michael Witten40e6e8a2009-04-13 13:23:50 -0500704 next if m/^GIT:/;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100705 if ($in_body) {
706 $summary_empty = 0 unless (/^\n$/);
707 } elsif (/^\n$/) {
708 $in_body = 1;
709 if ($need_8bit_cte) {
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000710 print $c2 "MIME-Version: 1.0\n",
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100711 "Content-Type: text/plain; ",
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200712 "charset=$compose_encoding\n",
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100713 "Content-Transfer-Encoding: 8bit\n";
714 }
715 } elsif (/^MIME-Version:/i) {
716 $need_8bit_cte = 0;
717 } elsif (/^Subject:\s*(.+)\s*$/i) {
718 $initial_subject = $1;
719 my $subject = $initial_subject;
720 $_ = "Subject: " .
Krzysztof Mazurce547802012-10-24 23:08:26 +0200721 quote_subject($subject, $compose_encoding) .
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100722 "\n";
723 } elsif (/^In-Reply-To:\s*(.+)\s*$/i) {
724 $initial_reply_to = $1;
725 next;
726 } elsif (/^From:\s*(.+)\s*$/i) {
727 $sender = $1;
728 next;
729 } elsif (/^(?:To|Cc|Bcc):/i) {
730 print "To/Cc/Bcc fields are not interpreted yet, they have been ignored\n";
731 next;
732 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000733 print $c2 $_;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100734 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000735 close $c;
736 close $c2;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100737
738 if ($summary_empty) {
739 print "Summary email is empty, skipping it\n";
740 $compose = -1;
741 }
742} elsif ($annotate) {
743 do_edit(@files);
744}
745
Jay Soffian6e182512009-03-28 21:39:10 -0400746sub ask {
747 my ($prompt, %arg) = @_;
Jay Soffian0da43a62009-04-04 23:23:21 -0400748 my $valid_re = $arg{valid_re};
Jay Soffian6e182512009-03-28 21:39:10 -0400749 my $default = $arg{default};
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700750 my $confirm_only = $arg{confirm_only};
Jay Soffian6e182512009-03-28 21:39:10 -0400751 my $resp;
752 my $i = 0;
Jay Soffian5906f542009-03-31 12:22:11 -0400753 return defined $default ? $default : undef
754 unless defined $term->IN and defined fileno($term->IN) and
755 defined $term->OUT and defined fileno($term->OUT);
Jay Soffian6e182512009-03-28 21:39:10 -0400756 while ($i++ < 10) {
757 $resp = $term->readline($prompt);
758 if (!defined $resp) { # EOF
759 print "\n";
760 return defined $default ? $default : undef;
761 }
762 if ($resp eq '' and defined $default) {
763 return $default;
764 }
Jay Soffian0da43a62009-04-04 23:23:21 -0400765 if (!defined $valid_re or $resp =~ /$valid_re/) {
Jay Soffian6e182512009-03-28 21:39:10 -0400766 return $resp;
767 }
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700768 if ($confirm_only) {
769 my $yesno = $term->readline("Are you sure you want to use <$resp> [y/N]? ");
770 if (defined $yesno && $yesno =~ /y/i) {
771 return $resp;
772 }
773 }
Jay Soffian6e182512009-03-28 21:39:10 -0400774 }
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -0700775 return;
Jay Soffian6e182512009-03-28 21:39:10 -0400776}
777
Thomas Rast3cae7e52010-06-17 22:10:39 +0200778my %broken_encoding;
779
Ævar Arnfjörð Bjarmason1d50bfd2010-09-30 13:42:58 +0000780sub file_declares_8bit_cte {
Thomas Rast3cae7e52010-06-17 22:10:39 +0200781 my $fn = shift;
782 open (my $fh, '<', $fn);
783 while (my $line = <$fh>) {
784 last if ($line =~ /^$/);
785 return 1 if ($line =~ /^Content-Transfer-Encoding: .*8bit.*$/);
786 }
787 close $fh;
788 return 0;
789}
790
791foreach my $f (@files) {
792 next unless (body_or_subject_has_nonascii($f)
793 && !file_declares_8bit_cte($f));
794 $broken_encoding{$f} = 1;
795}
796
797if (!defined $auto_8bit_encoding && scalar %broken_encoding) {
798 print "The following files are 8bit, but do not declare " .
799 "a Content-Transfer-Encoding.\n";
800 foreach my $f (sort keys %broken_encoding) {
801 print " $f\n";
802 }
803 $auto_8bit_encoding = ask("Which 8bit encoding should I declare [UTF-8]? ",
Junio C Hamano852a15d2015-02-13 12:20:25 -0800804 valid_re => qr/.{4}/, confirm_only => 1,
Thomas Rast3cae7e52010-06-17 22:10:39 +0200805 default => "UTF-8");
806}
807
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200808if (!$force) {
809 for my $f (@files) {
Ævar Arnfjörð Bjarmason0d290a42010-09-30 13:43:01 +0000810 if (get_patch_subject($f) =~ /\Q*** SUBJECT HERE ***\E/) {
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200811 die "Refusing to send because the patch\n\t$f\n"
812 . "has the template subject '*** SUBJECT HERE ***'. "
813 . "Pass --force if you really want to send.\n";
814 }
815 }
816}
817
Remi Lespinetc46e27a2015-06-30 14:16:47 +0200818if (defined $sender) {
Remi Lespinetfa5b1aa2015-06-30 14:16:51 +0200819 $sender =~ s/^\s+|\s+$//g;
Remi Lespinetc46e27a2015-06-30 14:16:47 +0200820 ($sender) = expand_aliases($sender);
821} else {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100822 $sender = $repoauthor || $repocommitter || '';
Ryan Anderson83b24432005-07-31 04:17:25 -0400823}
824
Michael S. Tsirkinda187592013-06-05 21:11:00 +0300825# $sender could be an already sanitized address
826# (e.g. sendemail.from could be manually sanitized by user).
827# But it's a no-op to run sanitize_address on an already sanitized address.
828$sender = sanitize_address($sender);
829
Junio C Hamano0d6b21e2016-04-24 12:31:44 -0700830my $to_whom = "To whom should the emails be sent (if anyone)?";
Felipe Contreras8cac13d2012-11-24 12:16:19 +0100831my $prompting = 0;
Junio C Hamano8796ff72010-10-26 22:02:03 -0700832if (!@initial_to && !defined $to_cmd) {
Junio C Hamano0d6b21e2016-04-24 12:31:44 -0700833 my $to = ask("$to_whom ",
Stephen Boyd61837492012-09-06 11:31:11 -0700834 default => "",
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700835 valid_re => qr/\@.*\./, confirm_only => 1);
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700836 push @initial_to, parse_address_line($to) if defined $to; # sanitized/validated later
Ryan Anderson1f038a02005-09-05 01:13:07 -0400837 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -0400838}
839
Eric Wong994d6c62006-05-14 19:13:44 -0700840sub expand_aliases {
Jeff King302e04e2009-07-23 07:09:29 -0400841 return map { expand_one_alias($_) } @_;
842}
843
844my %EXPANDED_ALIASES;
845sub expand_one_alias {
846 my $alias = shift;
847 if ($EXPANDED_ALIASES{$alias}) {
848 die "fatal: alias '$alias' expands to itself\n";
849 }
850 local $EXPANDED_ALIASES{$alias} = 1;
851 return $aliases{$alias} ? expand_aliases(@{$aliases{$alias}}) : $alias;
Eric Wong994d6c62006-05-14 19:13:44 -0700852}
853
Remi Lespinetb5e112d2015-06-30 14:16:45 +0200854@initial_to = process_address_list(@initial_to);
855@initial_cc = process_address_list(@initial_cc);
856@bcclist = process_address_list(@bcclist);
Eric Wong994d6c62006-05-14 19:13:44 -0700857
Adam Roben5483c712007-06-27 20:59:37 -0700858if ($thread && !defined $initial_reply_to && $prompting) {
Jay Soffian6e182512009-03-28 21:39:10 -0400859 $initial_reply_to = ask(
Stephen Boyd61837492012-09-06 11:31:11 -0700860 "Message-ID to be used as In-Reply-To for the first email (if any)? ",
861 default => "",
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700862 valid_re => qr/\@.*\./, confirm_only => 1);
Ryan Anderson83b24432005-07-31 04:17:25 -0400863}
Jay Soffian1ca3d6e2008-02-20 00:55:07 -0500864if (defined $initial_reply_to) {
Jay Soffian0fb7fc72008-02-21 19:16:04 -0500865 $initial_reply_to =~ s/^\s*<?//;
866 $initial_reply_to =~ s/>?\s*$//;
867 $initial_reply_to = "<$initial_reply_to>" if $initial_reply_to ne '';
Junio C Hamanoace9c2a2007-12-10 21:44:42 -0800868}
Mike Hommeyace72082007-12-09 18:17:28 +0100869
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900870if (!defined $smtp_server) {
Eric Wongaca7ad72006-05-15 02:34:44 -0700871 foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) {
872 if (-x $_) {
873 $smtp_server = $_;
874 last;
875 }
876 }
877 $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
Ryan Anderson3342d852005-07-31 20:04:24 -0400878}
879
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500880if ($compose && $compose > 0) {
881 @files = ($compose_filename . ".final", @files);
Ryan Anderson1f038a02005-09-05 01:13:07 -0400882}
883
Ryan Anderson83b24432005-07-31 04:17:25 -0400884# Variables we set as part of the loop over files
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500885our ($message_id, %mail, $subject, $reply_to, $references, $message,
Jay Soffiandc1460a2009-03-31 12:22:12 -0400886 $needs_confirm, $message_num, $ask_default);
Ryan Anderson83b24432005-07-31 04:17:25 -0400887
Eric Wong567ffeb2006-03-25 16:47:12 -0800888sub extract_valid_address {
889 my $address = shift;
Ævar Arnfjörð Bjarmason35b6ab92010-09-30 19:03:31 +0000890 my $local_part_regexp = qr/[^<>"\s@]+/;
891 my $domain_regexp = qr/[^.<>"\s@]+(?:\.[^.<>"\s@]+)+/;
Eric Wongdb3106b2006-05-15 02:41:01 -0700892
893 # check for a local address:
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700894 return $address if ($address =~ /^($local_part_regexp)$/);
Eric Wongdb3106b2006-05-15 02:41:01 -0700895
Uwe Kleine-König155197e2007-08-09 15:27:57 +0200896 $address =~ s/^\s*<(.*)>\s*$/$1/;
Eric Wong567ffeb2006-03-25 16:47:12 -0800897 if ($have_email_valid) {
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700898 return scalar Email::Valid->address($address);
Eric Wong567ffeb2006-03-25 16:47:12 -0800899 }
Krzysztof Mazur95c0d4b2012-11-22 19:12:09 +0100900
901 # less robust/correct than the monster regexp in Email::Valid,
902 # but still does a 99% job, and one less dependency
903 return $1 if $address =~ /($local_part_regexp\@$domain_regexp)/;
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -0700904 return;
Eric Wong567ffeb2006-03-25 16:47:12 -0800905}
Ryan Anderson83b24432005-07-31 04:17:25 -0400906
Krzysztof Mazure4312252012-11-22 19:12:10 +0100907sub extract_valid_address_or_die {
908 my $address = shift;
909 $address = extract_valid_address($address);
910 die "error: unable to extract a valid address from: $address\n"
911 if !$address;
912 return $address;
913}
914
915sub validate_address {
916 my $address = shift;
Krzysztof Mazurd0e98102012-11-22 19:12:12 +0100917 while (!extract_valid_address($address)) {
Krzysztof Mazur5c80afe2012-11-22 19:12:11 +0100918 print STDERR "error: unable to extract a valid address from: $address\n";
Krzysztof Mazurd0e98102012-11-22 19:12:12 +0100919 $_ = ask("What to do with this address? ([q]uit|[d]rop|[e]dit): ",
920 valid_re => qr/^(?:quit|q|drop|d|edit|e)/i,
Krzysztof Mazur5c80afe2012-11-22 19:12:11 +0100921 default => 'q');
922 if (/^d/i) {
923 return undef;
924 } elsif (/^q/i) {
925 cleanup_compose_files();
926 exit(0);
927 }
Junio C Hamano0d6b21e2016-04-24 12:31:44 -0700928 $address = ask("$to_whom ",
Krzysztof Mazurd0e98102012-11-22 19:12:12 +0100929 default => "",
930 valid_re => qr/\@.*\./, confirm_only => 1);
Krzysztof Mazure4312252012-11-22 19:12:10 +0100931 }
932 return $address;
933}
934
935sub validate_address_list {
936 return (grep { defined $_ }
937 map { validate_address($_) } @_);
Ryan Anderson83b24432005-07-31 04:17:25 -0400938}
939
940# Usually don't need to change anything below here.
941
942# we make a "fake" message id by taking the current number
943# of seconds since the beginning of Unix time and tacking on
944# a random number to the end, in case we are called quicker than
945# 1 second since the last time we were called.
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400946
947# We'll setup a template for the message id, using the "from" address:
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400948
Junio C Hamanobe510cf2007-09-17 21:18:20 -0700949my ($message_id_stamp, $message_id_serial);
Brian Gernhardt68ce9332010-04-10 10:53:53 -0400950sub make_message_id {
Junio C Hamanobe510cf2007-09-17 21:18:20 -0700951 my $uniq;
952 if (!defined $message_id_stamp) {
Eric Wongf916ab02016-04-06 20:07:14 +0000953 $message_id_stamp = strftime("%Y%m%d%H%M%S.$$", gmtime(time));
Junio C Hamanobe510cf2007-09-17 21:18:20 -0700954 $message_id_serial = 0;
955 }
956 $message_id_serial++;
957 $uniq = "$message_id_stamp-$message_id_serial";
958
Junio C Hamanoaeb59322007-06-20 13:47:34 -0700959 my $du_part;
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200960 for ($sender, $repocommitter, $repoauthor) {
961 $du_part = extract_valid_address(sanitize_address($_));
962 last if (defined $du_part and $du_part ne '');
Junio C Hamanoaeb59322007-06-20 13:47:34 -0700963 }
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200964 if (not defined $du_part or $du_part eq '') {
Ævar Arnfjörð Bjarmason529dd382010-09-30 13:43:08 +0000965 require Sys::Hostname;
Junio C Hamanoaeb59322007-06-20 13:47:34 -0700966 $du_part = 'user@' . Sys::Hostname::hostname();
967 }
Eric Wongf916ab02016-04-06 20:07:14 +0000968 my $message_id_template = "<%s-%s>";
Junio C Hamanobe510cf2007-09-17 21:18:20 -0700969 $message_id = sprintf($message_id_template, $uniq, $du_part);
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400970 #print "new message id = $message_id\n"; # Was useful for debugging
Ryan Anderson83b24432005-07-31 04:17:25 -0400971}
972
973
974
Eric Wonga5370b12006-03-25 03:01:01 -0800975$time = time - scalar $#files;
Ryan Anderson83b24432005-07-31 04:17:25 -0400976
Jürgen Rühle374c5902007-01-10 13:36:39 -0800977sub unquote_rfc2047 {
978 local ($_) = @_;
Роман Донченко11f70a72014-12-14 18:59:46 +0300979 my $charset;
Роман Донченкоab47e2a2014-12-14 18:59:47 +0300980 my $sep = qr/[ \t]+/;
981 s{$re_encoded_word(?:$sep$re_encoded_word)*}{
982 my @words = split $sep, $&;
983 foreach (@words) {
984 m/$re_encoded_word/;
985 $charset = $1;
986 my $encoding = $2;
987 my $text = $3;
988 if ($encoding eq 'q' || $encoding eq 'Q') {
989 $_ = $text;
990 s/_/ /g;
991 s/=([0-9A-F]{2})/chr(hex($1))/egi;
992 } else {
993 # other encodings not supported yet
994 }
Роман Донченко11f70a72014-12-14 18:59:46 +0300995 }
Роман Донченкоab47e2a2014-12-14 18:59:47 +0300996 join '', @words;
Thomas Rastb622d4d2012-07-30 21:25:40 +0200997 }eg;
Роман Донченко11f70a72014-12-14 18:59:46 +0300998 return wantarray ? ($_, $charset) : $_;
Jürgen Rühle374c5902007-01-10 13:36:39 -0800999}
1000
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001001sub quote_rfc2047 {
1002 local $_ = shift;
Brandon Caseyd1fff6f2009-06-06 20:12:31 -05001003 my $encoding = shift || 'UTF-8';
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001004 s/([^-a-zA-Z0-9!*+\/])/sprintf("=%02X", ord($1))/eg;
1005 s/(.*)/=\?$encoding\?q\?$1\?=/;
1006 return $_;
1007}
1008
Brandon Caseya3a82622009-06-07 19:25:58 -05001009sub is_rfc2047_quoted {
1010 my $s = shift;
Brandon Caseya3a82622009-06-07 19:25:58 -05001011 length($s) <= 75 &&
Роман Донченко11f70a72014-12-14 18:59:46 +03001012 $s =~ m/^(?:"[[:ascii:]]*"|$re_encoded_word)$/o;
Brandon Caseya3a82622009-06-07 19:25:58 -05001013}
1014
Krzysztof Mazurce547802012-10-24 23:08:26 +02001015sub subject_needs_rfc2047_quoting {
1016 my $s = shift;
1017
Krzysztof Mazurce1459f2012-10-24 23:28:29 +02001018 return ($s =~ /[^[:ascii:]]/) || ($s =~ /=\?/);
Krzysztof Mazurce547802012-10-24 23:08:26 +02001019}
1020
1021sub quote_subject {
1022 local $subject = shift;
1023 my $encoding = shift || 'UTF-8';
1024
1025 if (subject_needs_rfc2047_quoting($subject)) {
1026 return quote_rfc2047($subject, $encoding);
1027 }
1028 return $subject;
1029}
1030
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001031# use the simplest quoting being able to handle the recipient
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001032sub sanitize_address {
Robin H. Johnson732263d2007-04-25 19:37:19 -07001033 my ($recipient) = @_;
Krzysztof Mazur831a4882012-11-22 19:12:08 +01001034
1035 # remove garbage after email address
1036 $recipient =~ s/(.*>).*$/$1/;
1037
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001038 my ($recipient_name, $recipient_addr) = ($recipient =~ /^(.*?)\s*(<.*)/);
1039
1040 if (not $recipient_name) {
Ævar Arnfjörð Bjarmasonff483892010-09-30 13:43:02 +00001041 return $recipient;
Robin H. Johnson732263d2007-04-25 19:37:19 -07001042 }
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001043
1044 # if recipient_name is already quoted, do nothing
Brandon Caseya3a82622009-06-07 19:25:58 -05001045 if (is_rfc2047_quoted($recipient_name)) {
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001046 return $recipient;
1047 }
1048
Remi Lespinet1fe97032015-06-30 14:16:49 +02001049 # remove non-escaped quotes
1050 $recipient_name =~ s/(^|[^\\])"/$1/g;
1051
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001052 # rfc2047 is needed if a non-ascii char is included
1053 if ($recipient_name =~ /[^[:ascii:]]/) {
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001054 $recipient_name = quote_rfc2047($recipient_name);
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001055 }
1056
1057 # double quotes are needed if specials or CTLs are included
1058 elsif ($recipient_name =~ /[][()<>@,;:\\".\000-\037\177]/) {
Remi Lespinet1fe97032015-06-30 14:16:49 +02001059 $recipient_name =~ s/([\\\r])/\\$1/g;
Ævar Arnfjörð Bjarmasond5c7d692010-09-30 13:43:03 +00001060 $recipient_name = qq["$recipient_name"];
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001061 }
1062
1063 return "$recipient_name $recipient_addr";
1064
Robin H. Johnson732263d2007-04-25 19:37:19 -07001065}
1066
Krzysztof Mazure4312252012-11-22 19:12:10 +01001067sub sanitize_address_list {
1068 return (map { sanitize_address($_) } @_);
1069}
1070
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001071sub process_address_list {
Remi Lespinetb1c8a112015-06-30 14:16:50 +02001072 my @addr_list = map { parse_address_line($_) } @_;
1073 @addr_list = expand_aliases(@addr_list);
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001074 @addr_list = sanitize_address_list(@addr_list);
1075 @addr_list = validate_address_list(@addr_list);
1076 return @addr_list;
1077}
1078
Jari Aalto134550f2010-03-14 17:16:45 +02001079# Returns the local Fully Qualified Domain Name (FQDN) if available.
1080#
1081# Tightly configured MTAa require that a caller sends a real DNS
1082# domain name that corresponds the IP address in the HELO/EHLO
1083# handshake. This is used to verify the connection and prevent
1084# spammers from trying to hide their identity. If the DNS and IP don't
1085# match, the receiveing MTA may deny the connection.
1086#
1087# Here is a deny example of Net::SMTP with the default "localhost.localdomain"
1088#
1089# Net::SMTP=GLOB(0x267ec28)>>> EHLO localhost.localdomain
1090# Net::SMTP=GLOB(0x267ec28)<<< 550 EHLO argument does not match calling host
1091#
1092# This maildomain*() code is based on ideas in Perl library Test::Reporter
1093# /usr/share/perl5/Test/Reporter/Mail/Util.pm ==> sub _maildomain ()
1094
Brian Gernhardt59a86302010-04-10 10:53:54 -04001095sub valid_fqdn {
1096 my $domain = shift;
Brandon Casey61ef5e92010-09-26 22:18:01 -05001097 return defined $domain && !($^O eq 'darwin' && $domain =~ /\.local$/) && $domain =~ /\./;
Brian Gernhardt59a86302010-04-10 10:53:54 -04001098}
1099
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001100sub maildomain_net {
Jari Aalto134550f2010-03-14 17:16:45 +02001101 my $maildomain;
1102
1103 if (eval { require Net::Domain; 1 }) {
1104 my $domain = Net::Domain::domainname();
Brian Gernhardt59a86302010-04-10 10:53:54 -04001105 $maildomain = $domain if valid_fqdn($domain);
Jari Aalto134550f2010-03-14 17:16:45 +02001106 }
1107
1108 return $maildomain;
1109}
1110
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001111sub maildomain_mta {
Jari Aalto134550f2010-03-14 17:16:45 +02001112 my $maildomain;
1113
1114 if (eval { require Net::SMTP; 1 }) {
1115 for my $host (qw(mailhost localhost)) {
1116 my $smtp = Net::SMTP->new($host);
1117 if (defined $smtp) {
1118 my $domain = $smtp->domain;
1119 $smtp->quit;
1120
Brian Gernhardt59a86302010-04-10 10:53:54 -04001121 $maildomain = $domain if valid_fqdn($domain);
Jari Aalto134550f2010-03-14 17:16:45 +02001122
1123 last if $maildomain;
1124 }
1125 }
1126 }
1127
1128 return $maildomain;
1129}
1130
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001131sub maildomain {
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001132 return maildomain_net() || maildomain_mta() || 'localhost.localdomain';
Jari Aalto134550f2010-03-14 17:16:45 +02001133}
1134
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001135sub smtp_host_string {
1136 if (defined $smtp_server_port) {
1137 return "$smtp_server:$smtp_server_port";
1138 } else {
1139 return $smtp_server;
1140 }
1141}
1142
1143# Returns 1 if authentication succeeded or was not necessary
1144# (smtp_user was not specified), and 0 otherwise.
1145
1146sub smtp_auth_maybe {
1147 if (!defined $smtp_authuser || $auth) {
1148 return 1;
1149 }
1150
1151 # Workaround AUTH PLAIN/LOGIN interaction defect
1152 # with Authen::SASL::Cyrus
1153 eval {
1154 require Authen::SASL;
1155 Authen::SASL->import(qw(Perl));
1156 };
1157
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001158 # Check mechanism naming as defined in:
1159 # https://tools.ietf.org/html/rfc4422#page-8
Brian Norris904f6e72015-09-18 15:12:50 -07001160 if ($smtp_auth && $smtp_auth !~ /^(\b[A-Z0-9-_]{1,20}\s*)*$/) {
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001161 die "invalid smtp auth: '${smtp_auth}'";
1162 }
1163
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001164 # TODO: Authentication may fail not because credentials were
1165 # invalid but due to other reasons, in which we should not
1166 # reject credentials.
1167 $auth = Git::credential({
1168 'protocol' => 'smtp',
1169 'host' => smtp_host_string(),
1170 'username' => $smtp_authuser,
1171 # if there's no password, "git credential fill" will
1172 # give us one, otherwise it'll just pass this one.
1173 'password' => $smtp_authpass
1174 }, sub {
1175 my $cred = shift;
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001176
1177 if ($smtp_auth) {
1178 my $sasl = Authen::SASL->new(
1179 mechanism => $smtp_auth,
1180 callback => {
1181 user => $cred->{'username'},
1182 pass => $cred->{'password'},
1183 authname => $cred->{'username'},
1184 }
1185 );
1186
1187 return !!$smtp->auth($sasl);
1188 }
1189
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001190 return !!$smtp->auth($cred->{'username'}, $cred->{'password'});
1191 });
1192
1193 return $auth;
1194}
1195
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001196sub ssl_verify_params {
1197 eval {
1198 require IO::Socket::SSL;
1199 IO::Socket::SSL->import(qw/SSL_VERIFY_PEER SSL_VERIFY_NONE/);
1200 };
1201 if ($@) {
1202 print STDERR "Not using SSL_VERIFY_PEER due to out-of-date IO::Socket::SSL.\n";
1203 return;
1204 }
1205
1206 if (!defined $smtp_ssl_cert_path) {
Ruben Kerkhof01645b72014-01-15 21:31:11 +04001207 # use the OpenSSL defaults
1208 return (SSL_verify_mode => SSL_VERIFY_PEER());
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001209 }
1210
1211 if ($smtp_ssl_cert_path eq "") {
1212 return (SSL_verify_mode => SSL_VERIFY_NONE());
1213 } elsif (-d $smtp_ssl_cert_path) {
1214 return (SSL_verify_mode => SSL_VERIFY_PEER(),
1215 SSL_ca_path => $smtp_ssl_cert_path);
1216 } elsif (-f $smtp_ssl_cert_path) {
1217 return (SSL_verify_mode => SSL_VERIFY_PEER(),
1218 SSL_ca_file => $smtp_ssl_cert_path);
1219 } else {
John Keepingc55d65f2015-11-24 23:31:40 +00001220 die "CA path \"$smtp_ssl_cert_path\" does not exist";
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001221 }
1222}
1223
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001224sub file_name_is_absolute {
1225 my ($path) = @_;
1226
1227 # msys does not grok DOS drive-prefixes
1228 if ($^O eq 'msys') {
Junio C Hamanof24ecf52014-04-23 09:37:38 -07001229 return ($path =~ m#^/# || $path =~ m#^[a-zA-Z]\:#)
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001230 }
1231
1232 require File::Spec::Functions;
1233 return File::Spec::Functions::file_name_is_absolute($path);
1234}
1235
Michael Witten15da1082009-04-13 13:23:51 -05001236# Returns 1 if the message was sent, and 0 otherwise.
Markus Heidelberga1b5b372009-06-12 12:51:42 +02001237# In actuality, the whole program dies when there
Michael Witten15da1082009-04-13 13:23:51 -05001238# is an error sending a message.
1239
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001240sub send_message {
Eric Wong4bc87a22006-03-25 17:20:48 -08001241 my @recipients = unique_email_list(@to);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001242 @cc = (grep { my $cc = extract_valid_address_or_die($_);
Joe Perches83acaae2010-11-20 15:06:05 -08001243 not grep { $cc eq $_ || $_ =~ /<\Q${cc}\E>$/ } @recipients
Ask Bjørn Hansen7ac17522007-11-19 03:00:26 -08001244 }
Ask Bjørn Hansen7ac17522007-11-19 03:00:26 -08001245 @cc);
Eric Wong4bc87a22006-03-25 17:20:48 -08001246 my $to = join (",\n\t", @recipients);
Ryan Anderson58063242006-05-29 12:30:13 -07001247 @recipients = unique_email_list(@recipients,@cc,@bcclist);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001248 @recipients = (map { extract_valid_address_or_die($_) } @recipients);
Jakub Narebski6bdca892006-07-07 20:57:55 +02001249 my $date = format_2822_time($time++);
Martin Langhoffe923eff2006-05-03 09:44:36 +12001250 my $gitversion = '@@GIT_VERSION@@';
1251 if ($gitversion =~ m/..GIT_VERSION../) {
Petr Baudis3cb8caf2006-07-03 22:47:58 +02001252 $gitversion = Git::version();
Martin Langhoffe923eff2006-05-03 09:44:36 +12001253 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001254
Joe Perches02461e02009-10-08 10:03:26 -07001255 my $cc = join(",\n\t", unique_email_list(@cc));
Junio C Hamanof06a6a42007-04-16 16:51:47 -07001256 my $ccline = "";
1257 if ($cc ne '') {
1258 $ccline = "\nCc: $cc";
1259 }
Jeff King4f3d3702007-12-17 15:51:34 -05001260 make_message_id() unless defined($message_id);
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001261
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001262 my $header = "From: $sender
Junio C Hamanof06a6a42007-04-16 16:51:47 -07001263To: $to${ccline}
Eric Wong4bc87a22006-03-25 17:20:48 -08001264Subject: $subject
Eric Wong4bc87a22006-03-25 17:20:48 -08001265Date: $date
1266Message-Id: $message_id
Eric Wong4bc87a22006-03-25 17:20:48 -08001267";
Luis Henriquesac1596a2014-03-24 21:38:27 +00001268 if ($use_xmailer) {
1269 $header .= "X-Mailer: git-send-email $gitversion\n";
1270 }
Thomas Rast3e0c4ff2009-03-01 23:45:41 +01001271 if ($reply_to) {
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001272
1273 $header .= "In-Reply-To: $reply_to\n";
1274 $header .= "References: $references\n";
1275 }
Junio C Hamanoce91c2f2006-10-05 16:36:49 -07001276 if (@xh) {
1277 $header .= join("\n", @xh) . "\n";
1278 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001279
Robin H. Johnsonc38f0242007-04-25 19:37:20 -07001280 my @sendmail_parameters = ('-i', @recipients);
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001281 my $raw_from = $sender;
Felipe Contrerasc89e3242009-11-26 21:04:29 +02001282 if (defined $envelope_sender && $envelope_sender ne "auto") {
1283 $raw_from = $envelope_sender;
1284 }
Robin H. Johnsonf073a592007-04-25 19:37:22 -07001285 $raw_from = extract_valid_address($raw_from);
1286 unshift (@sendmail_parameters,
1287 '-f', $raw_from) if(defined $envelope_sender);
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001288
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001289 if ($needs_confirm && !$dry_run) {
1290 print "\n$header\n";
1291 if ($needs_confirm eq "inform") {
1292 $confirm_unconfigured = 0; # squelch this message for the rest of this run
Jay Soffian6e182512009-03-28 21:39:10 -04001293 $ask_default = "y"; # assume yes on EOF since user hasn't explicitly asked for confirmation
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001294 print " The Cc list above has been expanded by additional\n";
1295 print " addresses found in the patch commit message. By default\n";
1296 print " send-email prompts before sending whenever this occurs.\n";
1297 print " This behavior is controlled by the sendemail.confirm\n";
1298 print " configuration setting.\n";
1299 print "\n";
1300 print " For additional information, run 'git send-email --help'.\n";
1301 print " To retain the current behavior, but squelch this message,\n";
1302 print " run 'git config --global sendemail.confirm auto'.\n\n";
1303 }
Jay Soffian6e182512009-03-28 21:39:10 -04001304 $_ = ask("Send this email? ([y]es|[n]o|[q]uit|[a]ll): ",
1305 valid_re => qr/^(?:yes|y|no|n|quit|q|all|a)/i,
1306 default => $ask_default);
1307 die "Send this email reply required" unless defined $_;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001308 if (/^n/i) {
Michael Witten15da1082009-04-13 13:23:51 -05001309 return 0;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001310 } elsif (/^q/i) {
1311 cleanup_compose_files();
1312 exit(0);
1313 } elsif (/^a/i) {
1314 $confirm = 'never';
1315 }
1316 }
1317
Pascal Obry052fbea2010-09-06 20:12:11 +02001318 unshift (@sendmail_parameters, @smtp_server_options);
1319
Matthew Wilcox61302592006-10-10 08:58:23 -06001320 if ($dry_run) {
1321 # We don't want to send the email.
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001322 } elsif (file_name_is_absolute($smtp_server)) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001323 my $pid = open my $sm, '|-';
1324 defined $pid or die $!;
1325 if (!$pid) {
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001326 exec($smtp_server, @sendmail_parameters) or die $!;
Eric Wongaca7ad72006-05-15 02:34:44 -07001327 }
1328 print $sm "$header\n$message";
Ævar Arnfjörð Bjarmason5e2c2ab2010-09-30 13:43:07 +00001329 close $sm or die $!;
Eric Wongaca7ad72006-05-15 02:34:44 -07001330 } else {
Junio C Hamano44b24762007-09-25 17:27:54 -07001331
1332 if (!defined $smtp_server) {
1333 die "The required SMTP server is not properly defined."
1334 }
1335
Thomas Rastf6bebd12008-06-25 21:42:43 +02001336 if ($smtp_encryption eq 'ssl') {
Junio C Hamano44b24762007-09-25 17:27:54 -07001337 $smtp_server_port ||= 465; # ssmtp
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001338 require Net::SMTP::SSL;
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001339 $smtp_domain ||= maildomain();
Thomas Rast5508f3e2013-12-01 23:48:43 +01001340 require IO::Socket::SSL;
John Keeping9d605242015-12-03 21:47:18 +00001341
1342 # Suppress "variable accessed once" warning.
1343 {
1344 no warnings 'once';
1345 $IO::Socket::SSL::DEBUG = 1;
1346 }
1347
Thomas Rast5508f3e2013-12-01 23:48:43 +01001348 # Net::SMTP::SSL->new() does not forward any SSL options
1349 IO::Socket::SSL::set_client_defaults(
1350 ssl_verify_params());
Jari Aalto134550f2010-03-14 17:16:45 +02001351 $smtp ||= Net::SMTP::SSL->new($smtp_server,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001352 Hello => $smtp_domain,
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001353 Port => $smtp_server_port,
Thomas Rast5508f3e2013-12-01 23:48:43 +01001354 Debug => $debug_net_smtp);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001355 }
1356 else {
1357 require Net::SMTP;
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001358 $smtp_domain ||= maildomain();
brian m. carlson1a741bf2013-07-04 22:04:52 +00001359 $smtp_server_port ||= 25;
1360 $smtp ||= Net::SMTP->new($smtp_server,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001361 Hello => $smtp_domain,
brian m. carlson1a741bf2013-07-04 22:04:52 +00001362 Debug => $debug_net_smtp,
1363 Port => $smtp_server_port);
Yakov Lernerfb3650e2009-09-25 15:10:21 -07001364 if ($smtp_encryption eq 'tls' && $smtp) {
Thomas Rastf6bebd12008-06-25 21:42:43 +02001365 require Net::SMTP::SSL;
1366 $smtp->command('STARTTLS');
1367 $smtp->response();
1368 if ($smtp->code == 220) {
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001369 $smtp = Net::SMTP::SSL->start_SSL($smtp,
1370 ssl_verify_params())
Brian M. Carlson6cb0c882013-09-08 20:54:34 +00001371 or die "STARTTLS failed! ".IO::Socket::SSL::errstr();
Thomas Rast6cbf8b02008-07-03 00:11:31 +02001372 $smtp_encryption = '';
Robert Shearman9d1ccf52008-07-09 22:39:40 +01001373 # Send EHLO again to receive fresh
1374 # supported commands
Matthew Daley155b9402011-10-15 04:44:52 -04001375 $smtp->hello($smtp_domain);
Thomas Rastf6bebd12008-06-25 21:42:43 +02001376 } else {
1377 die "Server does not support STARTTLS! ".$smtp->message;
1378 }
1379 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001380 }
Junio C Hamano44b24762007-09-25 17:27:54 -07001381
1382 if (!$smtp) {
Jari Aaltof60812e2010-03-14 17:16:09 +02001383 die "Unable to initialize SMTP properly. Check config and use --smtp-debug. ",
Jari Aaltoe5afb3a2010-03-14 17:15:33 +02001384 "VALUES: server=$smtp_server ",
1385 "encryption=$smtp_encryption ",
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001386 "hello=$smtp_domain",
Sylvain Rabota1dd7e12011-04-29 20:23:24 +02001387 defined $smtp_server_port ? " port=$smtp_server_port" : "";
Junio C Hamano44b24762007-09-25 17:27:54 -07001388 }
1389
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001390 smtp_auth_maybe or die $smtp->message;
Michael Witten2363d742008-02-03 19:53:56 -05001391
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001392 $smtp->mail( $raw_from ) or die $smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001393 $smtp->to( @recipients ) or die $smtp->message;
1394 $smtp->data or die $smtp->message;
Stefan Agnerf60c4832015-09-30 09:26:09 +02001395 $smtp->datasend("$header\n") or die $smtp->message;
1396 my @lines = split /^/, $message;
1397 foreach my $line (@lines) {
1398 $smtp->datasend("$line") or die $smtp->message;
1399 }
Eric Wongaca7ad72006-05-15 02:34:44 -07001400 $smtp->dataend() or die $smtp->message;
Michael Witten15da1082009-04-13 13:23:51 -05001401 $smtp->code =~ /250|200/ or die "Failed to send $subject\n".$smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001402 }
Ryan Anderson27184352006-02-05 20:13:52 -05001403 if ($quiet) {
Robin H. Johnson71c7da92007-04-25 19:37:16 -07001404 printf (($dry_run ? "Dry-" : "")."Sent %s\n", $subject);
Ryan Anderson27184352006-02-05 20:13:52 -05001405 } else {
David D. Kilzerb7f30e02007-11-18 20:14:55 -08001406 print (($dry_run ? "Dry-" : "")."OK. Log says:\n");
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001407 if (!file_name_is_absolute($smtp_server)) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001408 print "Server: $smtp_server\n";
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001409 print "MAIL FROM:<$raw_from>\n";
Joe Perches02461e02009-10-08 10:03:26 -07001410 foreach my $entry (@recipients) {
1411 print "RCPT TO:<$entry>\n";
1412 }
Eric Wongaca7ad72006-05-15 02:34:44 -07001413 } else {
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001414 print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001415 }
David D. Kilzerb7f30e02007-11-18 20:14:55 -08001416 print $header, "\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001417 if ($smtp) {
1418 print "Result: ", $smtp->code, ' ',
1419 ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
1420 } else {
1421 print "Result: OK\n";
1422 }
Ryan Anderson30d08b32006-02-02 11:56:06 -05001423 }
Michael Witten15da1082009-04-13 13:23:51 -05001424
1425 return 1;
Ryan Anderson83b24432005-07-31 04:17:25 -04001426}
1427
Ryan Anderson83b24432005-07-31 04:17:25 -04001428$reply_to = $initial_reply_to;
Junio C Hamano2186d562006-05-29 23:53:13 -07001429$references = $initial_reply_to || '';
Ryan Anderson83b24432005-07-31 04:17:25 -04001430$subject = $initial_subject;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001431$message_num = 0;
Ryan Anderson83b24432005-07-31 04:17:25 -04001432
1433foreach my $t (@files) {
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001434 open my $fh, "<", $t or die "can't open file $t";
Ryan Anderson83b24432005-07-31 04:17:25 -04001435
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001436 my $author = undef;
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001437 my $sauthor = undef;
Jeff King8291db62007-11-16 05:49:09 -05001438 my $author_encoding;
1439 my $has_content_type;
1440 my $body_encoding;
Paolo Bonzinibb294562014-11-25 15:00:26 +01001441 my $xfer_encoding;
1442 my $has_mime_version;
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001443 @to = ();
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001444 @cc = ();
Junio C Hamanoce91c2f2006-10-05 16:36:49 -07001445 @xh = ();
Junio C Hamanoe6b09642006-10-07 03:09:05 -07001446 my $input_format = undef;
Jay Soffian50126992009-02-14 23:32:14 -05001447 my @header = ();
Ryan Anderson83b24432005-07-31 04:17:25 -04001448 $message = "";
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001449 $message_num++;
Jay Soffian50126992009-02-14 23:32:14 -05001450 # First unfold multiline header fields
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001451 while(<$fh>) {
Jay Soffian50126992009-02-14 23:32:14 -05001452 last if /^\s*$/;
1453 if (/^\s+\S/ and @header) {
1454 chomp($header[$#header]);
1455 s/^\s+/ /;
1456 $header[$#header] .= $_;
1457 } else {
1458 push(@header, $_);
1459 }
1460 }
1461 # Now parse the header
1462 foreach(@header) {
1463 if (/^From /) {
1464 $input_format = 'mbox';
1465 next;
1466 }
1467 chomp;
1468 if (!defined $input_format && /^[-A-Za-z]+:\s/) {
1469 $input_format = 'mbox';
1470 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001471
Jay Soffian50126992009-02-14 23:32:14 -05001472 if (defined $input_format && $input_format eq 'mbox') {
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001473 if (/^Subject:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001474 $subject = $1;
1475 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001476 elsif (/^From:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001477 ($author, $author_encoding) = unquote_rfc2047($1);
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001478 $sauthor = sanitize_address($author);
Jay Soffian50126992009-02-14 23:32:14 -05001479 next if $suppress_cc{'author'};
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001480 next if $suppress_cc{'self'} and $sauthor eq $sender;
Jay Soffian50126992009-02-14 23:32:14 -05001481 printf("(mbox) Adding cc: %s from line '%s'\n",
1482 $1, $_) unless $quiet;
1483 push @cc, $1;
1484 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001485 elsif (/^To:\s+(.*)$/i) {
Stephen Boyd21802cd2010-09-29 00:26:44 -07001486 foreach my $addr (parse_address_line($1)) {
1487 printf("(mbox) Adding to: %s from line '%s'\n",
1488 $addr, $_) unless $quiet;
Krzysztof Mazure4312252012-11-22 19:12:10 +01001489 push @to, $addr;
Stephen Boyd21802cd2010-09-29 00:26:44 -07001490 }
1491 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001492 elsif (/^Cc:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001493 foreach my $addr (parse_address_line($1)) {
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001494 my $qaddr = unquote_rfc2047($addr);
1495 my $saddr = sanitize_address($qaddr);
1496 if ($saddr eq $sender) {
David Brown65648282007-12-25 19:56:29 -08001497 next if ($suppress_cc{'self'});
David Brown65648282007-12-25 19:56:29 -08001498 } else {
1499 next if ($suppress_cc{'cc'});
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001500 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001501 printf("(mbox) Adding cc: %s from line '%s'\n",
Jay Soffian50126992009-02-14 23:32:14 -05001502 $addr, $_) unless $quiet;
1503 push @cc, $addr;
Ryan Anderson83b24432005-07-31 04:17:25 -04001504 }
1505 }
Jay Soffian50126992009-02-14 23:32:14 -05001506 elsif (/^Content-type:/i) {
1507 $has_content_type = 1;
1508 if (/charset="?([^ "]+)/) {
1509 $body_encoding = $1;
1510 }
1511 push @xh, $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001512 }
Paolo Bonzinibb294562014-11-25 15:00:26 +01001513 elsif (/^MIME-Version/i) {
1514 $has_mime_version = 1;
1515 push @xh, $_;
1516 }
Jay Soffian50126992009-02-14 23:32:14 -05001517 elsif (/^Message-Id: (.*)/i) {
1518 $message_id = $1;
1519 }
Paolo Bonzinibb294562014-11-25 15:00:26 +01001520 elsif (/^Content-Transfer-Encoding: (.*)/i) {
1521 $xfer_encoding = $1 if not defined $xfer_encoding;
1522 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001523 elsif (!/^Date:\s/i && /^[-A-Za-z]+:\s+\S/) {
Jay Soffian50126992009-02-14 23:32:14 -05001524 push @xh, $_;
1525 }
1526
Ryan Anderson83b24432005-07-31 04:17:25 -04001527 } else {
Jay Soffian50126992009-02-14 23:32:14 -05001528 # In the traditional
1529 # "send lots of email" format,
1530 # line 1 = cc
1531 # line 2 = subject
1532 # So let's support that, too.
1533 $input_format = 'lots';
1534 if (@cc == 0 && !$suppress_cc{'cc'}) {
1535 printf("(non-mbox) Adding cc: %s from line '%s'\n",
1536 $_, $_) unless $quiet;
1537 push @cc, $_;
1538 } elsif (!defined $subject) {
1539 $subject = $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001540 }
1541 }
1542 }
Jay Soffian50126992009-02-14 23:32:14 -05001543 # Now parse the message body
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001544 while(<$fh>) {
Jay Soffian50126992009-02-14 23:32:14 -05001545 $message .= $_;
1546 if (/^(Signed-off-by|Cc): (.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001547 chomp;
Jay Soffian3531e272009-02-14 23:32:15 -05001548 my ($what, $c) = ($1, $2);
Jay Soffian50126992009-02-14 23:32:14 -05001549 chomp $c;
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001550 my $sc = sanitize_address($c);
1551 if ($sc eq $sender) {
Jay Soffian3531e272009-02-14 23:32:15 -05001552 next if ($suppress_cc{'self'});
1553 } else {
1554 next if $suppress_cc{'sob'} and $what =~ /Signed-off-by/i;
1555 next if $suppress_cc{'bodycc'} and $what =~ /Cc/i;
1556 }
Jay Soffian50126992009-02-14 23:32:14 -05001557 push @cc, $c;
Jay Soffian3531e272009-02-14 23:32:15 -05001558 printf("(body) Adding cc: %s from line '%s'\n",
Jay Soffian50126992009-02-14 23:32:14 -05001559 $c, $_) unless $quiet;
1560 }
1561 }
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001562 close $fh;
Joe Perches324a8bd2007-08-17 18:51:12 -07001563
Joe Perches6e74e072010-09-24 10:03:00 -07001564 push @to, recipients_cmd("to-cmd", "to", $to_cmd, $t)
1565 if defined $to_cmd;
1566 push @cc, recipients_cmd("cc-cmd", "cc", $cc_cmd, $t)
1567 if defined $cc_cmd && !$suppress_cc{'cccmd'};
Joe Perches324a8bd2007-08-17 18:51:12 -07001568
Thomas Rast3cae7e52010-06-17 22:10:39 +02001569 if ($broken_encoding{$t} && !$has_content_type) {
Paolo Bonzinibb294562014-11-25 15:00:26 +01001570 $xfer_encoding = '8bit' if not defined $xfer_encoding;
Thomas Rast3cae7e52010-06-17 22:10:39 +02001571 $has_content_type = 1;
Paolo Bonzinibb294562014-11-25 15:00:26 +01001572 push @xh, "Content-Type: text/plain; charset=$auto_8bit_encoding";
Thomas Rast3cae7e52010-06-17 22:10:39 +02001573 $body_encoding = $auto_8bit_encoding;
1574 }
1575
Krzysztof Mazurce547802012-10-24 23:08:26 +02001576 if ($broken_encoding{$t} && !is_rfc2047_quoted($subject)) {
1577 $subject = quote_subject($subject, $auto_8bit_encoding);
Thomas Rast3cae7e52010-06-17 22:10:39 +02001578 }
1579
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001580 if (defined $sauthor and $sauthor ne $sender) {
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001581 $message = "From: $author\n\n$message";
Jeff King8291db62007-11-16 05:49:09 -05001582 if (defined $author_encoding) {
1583 if ($has_content_type) {
1584 if ($body_encoding eq $author_encoding) {
1585 # ok, we already have the right encoding
1586 }
1587 else {
1588 # uh oh, we should re-encode
1589 }
1590 }
1591 else {
Paolo Bonzinibb294562014-11-25 15:00:26 +01001592 $xfer_encoding = '8bit' if not defined $xfer_encoding;
Thomas Rast3cae7e52010-06-17 22:10:39 +02001593 $has_content_type = 1;
Jeff King8291db62007-11-16 05:49:09 -05001594 push @xh,
Paolo Bonzinibb294562014-11-25 15:00:26 +01001595 "Content-Type: text/plain; charset=$author_encoding";
Jeff King8291db62007-11-16 05:49:09 -05001596 }
1597 }
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001598 }
Paolo Bonzini8d814082014-11-25 15:00:27 +01001599 if (defined $target_xfer_encoding) {
1600 $xfer_encoding = '8bit' if not defined $xfer_encoding;
1601 $message = apply_transfer_encoding(
1602 $message, $xfer_encoding, $target_xfer_encoding);
1603 $xfer_encoding = $target_xfer_encoding;
1604 }
Paolo Bonzinibb294562014-11-25 15:00:26 +01001605 if (defined $xfer_encoding) {
1606 push @xh, "Content-Transfer-Encoding: $xfer_encoding";
1607 }
1608 if (defined $xfer_encoding or $has_content_type) {
1609 unshift @xh, 'MIME-Version: 1.0' unless $has_mime_version;
1610 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001611
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001612 $needs_confirm = (
1613 $confirm eq "always" or
1614 ($confirm =~ /^(?:auto|cc)$/ && @cc) or
1615 ($confirm =~ /^(?:auto|compose)$/ && $compose && $message_num == 1));
1616 $needs_confirm = "inform" if ($needs_confirm && $confirm_unconfigured && @cc);
1617
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001618 @to = process_address_list(@to);
1619 @cc = process_address_list(@cc);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001620
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001621 @to = (@initial_to, @to);
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001622 @cc = (@initial_cc, @cc);
1623
Michael S. Tsirkinf515c902014-04-29 08:41:16 +03001624 if ($message_num == 1) {
1625 if (defined $cover_cc and $cover_cc) {
1626 @initial_cc = @cc;
1627 }
1628 if (defined $cover_to and $cover_to) {
1629 @initial_to = @to;
1630 }
1631 }
1632
Michael Witten15da1082009-04-13 13:23:51 -05001633 my $message_was_sent = send_message();
Ryan Anderson83b24432005-07-31 04:17:25 -04001634
1635 # set up for the next message
Junio C Hamano95a877a2009-06-12 09:23:43 -07001636 if ($thread && $message_was_sent &&
Felipe Contrerasb99d22f2013-05-24 22:44:52 -05001637 ($chain_reply_to || !defined $reply_to || length($reply_to) == 0 ||
Antonio Ospitedb54c8e2010-11-12 15:55:08 +01001638 $message_num == 1)) {
Ryan Anderson78488b22005-07-31 20:04:24 -04001639 $reply_to = $message_id;
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001640 if (length $references > 0) {
YOSHIFUJI Hideaki / 吉藤英明a925b892007-04-06 08:50:24 +09001641 $references .= "\n $message_id";
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001642 } else {
1643 $references = "$message_id";
1644 }
Ryan Anderson78488b22005-07-31 20:04:24 -04001645 }
Jeff King4f3d3702007-12-17 15:51:34 -05001646 $message_id = undef;
Ryan Anderson83b24432005-07-31 04:17:25 -04001647}
Ryan Andersone2057352005-08-02 21:45:22 -04001648
Joe Perches6e74e072010-09-24 10:03:00 -07001649# Execute a command (e.g. $to_cmd) to get a list of email addresses
1650# and return a results array
1651sub recipients_cmd {
1652 my ($prefix, $what, $cmd, $file) = @_;
1653
Joe Perches6e74e072010-09-24 10:03:00 -07001654 my @addresses = ();
Ramkumar Ramachandraa47eab02013-03-31 18:40:42 -07001655 open my $fh, "-|", "$cmd \Q$file\E"
Joe Perches6e74e072010-09-24 10:03:00 -07001656 or die "($prefix) Could not execute '$cmd'";
Junio C Hamano7ebee442010-10-26 22:02:52 -07001657 while (my $address = <$fh>) {
Joe Perches6e74e072010-09-24 10:03:00 -07001658 $address =~ s/^\s*//g;
1659 $address =~ s/\s*$//g;
1660 $address = sanitize_address($address);
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001661 next if ($address eq $sender and $suppress_cc{'self'});
Joe Perches6e74e072010-09-24 10:03:00 -07001662 push @addresses, $address;
1663 printf("($prefix) Adding %s: %s from: '%s'\n",
1664 $what, $address, $cmd) unless $quiet;
1665 }
Junio C Hamano7ebee442010-10-26 22:02:52 -07001666 close $fh
Joe Perches6e74e072010-09-24 10:03:00 -07001667 or die "($prefix) failed to close pipe to '$cmd'";
1668 return @addresses;
1669}
1670
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001671cleanup_compose_files();
Ryan Anderson1f038a02005-09-05 01:13:07 -04001672
Ævar Arnfjörð Bjarmason4bf597e2010-09-30 13:43:00 +00001673sub cleanup_compose_files {
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001674 unlink($compose_filename, $compose_filename . ".final") if $compose;
Ryan Anderson1f038a02005-09-05 01:13:07 -04001675}
1676
Eric Wong4bc87a22006-03-25 17:20:48 -08001677$smtp->quit if $smtp;
Ryan Andersone2057352005-08-02 21:45:22 -04001678
Paolo Bonzini8d814082014-11-25 15:00:27 +01001679sub apply_transfer_encoding {
1680 my $message = shift;
1681 my $from = shift;
1682 my $to = shift;
1683
1684 return $message if ($from eq $to and $from ne '7bit');
1685
1686 require MIME::QuotedPrint;
1687 require MIME::Base64;
1688
1689 $message = MIME::QuotedPrint::decode($message)
1690 if ($from eq 'quoted-printable');
1691 $message = MIME::Base64::decode($message)
1692 if ($from eq 'base64');
1693
1694 die "cannot send message as 7bit"
1695 if ($to eq '7bit' and $message =~ /[^[:ascii:]]/);
1696 return $message
1697 if ($to eq '7bit' or $to eq '8bit');
1698 return MIME::QuotedPrint::encode($message, "\n", 0)
1699 if ($to eq 'quoted-printable');
1700 return MIME::Base64::encode($message, "\n")
1701 if ($to eq 'base64');
1702 die "invalid transfer encoding";
1703}
1704
Ævar Arnfjörð Bjarmasonc438ea22010-09-30 13:42:59 +00001705sub unique_email_list {
Ryan Andersone2057352005-08-02 21:45:22 -04001706 my %seen;
1707 my @emails;
1708
1709 foreach my $entry (@_) {
Krzysztof Mazure4312252012-11-22 19:12:10 +01001710 my $clean = extract_valid_address_or_die($entry);
1711 $seen{$clean} ||= 0;
1712 next if $seen{$clean}++;
1713 push @emails, $entry;
Ryan Andersone2057352005-08-02 21:45:22 -04001714 }
1715 return @emails;
1716}
Jeff King747bbff2008-01-18 09:19:48 -05001717
1718sub validate_patch {
1719 my $fn = shift;
1720 open(my $fh, '<', $fn)
1721 or die "unable to open $fn: $!\n";
1722 while (my $line = <$fh>) {
1723 if (length($line) > 998) {
1724 return "$.: patch contains a line longer than 998 characters";
1725 }
1726 }
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -07001727 return;
Jeff King747bbff2008-01-18 09:19:48 -05001728}
Jeff King0706bd12008-03-28 17:28:33 -04001729
1730sub file_has_nonascii {
1731 my $fn = shift;
1732 open(my $fh, '<', $fn)
1733 or die "unable to open $fn: $!\n";
1734 while (my $line = <$fh>) {
1735 return 1 if $line =~ /[^[:ascii:]]/;
1736 }
1737 return 0;
1738}
Thomas Rast3cae7e52010-06-17 22:10:39 +02001739
1740sub body_or_subject_has_nonascii {
1741 my $fn = shift;
1742 open(my $fh, '<', $fn)
1743 or die "unable to open $fn: $!\n";
1744 while (my $line = <$fh>) {
1745 last if $line =~ /^$/;
1746 return 1 if $line =~ /^Subject.*[^[:ascii:]]/;
1747 }
1748 while (my $line = <$fh>) {
1749 return 1 if $line =~ /[^[:ascii:]]/;
1750 }
1751 return 0;
1752}