blob: f4c07908d2737203031bdcab7f4f30404e64d4e5 [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 /;
Jonathan Tan64896602017-05-12 15:38:26 -070028use File::Spec::Functions qw(catdir catfile);
Ævar Arnfjörð Bjarmason28654672018-03-03 15:38:13 +000029use Git::LoadCPAN::Error qw(:try);
Jonathan Tan64896602017-05-12 15:38:26 -070030use Cwd qw(abs_path cwd);
Petr Baudis3cb8caf2006-07-03 22:47:58 +020031use Git;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -010032use Git::I18N;
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +000033use Net::Domain ();
34use Net::SMTP ();
Ævar Arnfjörð Bjarmason28654672018-03-03 15:38:13 +000035use Git::LoadCPAN::Mail::Address;
Ryan Anderson83b24432005-07-31 04:17:25 -040036
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +010037Getopt::Long::Configure qw/ pass_through /;
38
Junio C Hamano280242d2006-07-02 16:03:59 -070039package FakeTerm;
40sub new {
41 my ($class, $reason) = @_;
42 return bless \$reason, shift;
43}
44sub readline {
45 my $self = shift;
46 die "Cannot use readline on FakeTerm: $$self";
47}
48package main;
49
Michael Coleman1b0baf12007-02-27 22:47:54 -060050
51sub usage {
52 print <<EOT;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +010053git send-email [options] <file | directory | rev-list options >
Jacob Keller17b7a832015-11-19 14:52:11 -080054git send-email --dump-aliases
Michael Witten4ed62b02008-09-30 07:58:30 -050055
56 Composing:
57 --from <str> * Email From:
Stephen Boydf434c082010-03-07 14:46:48 -080058 --[no-]to <str> * Email To:
59 --[no-]cc <str> * Email Cc:
60 --[no-]bcc <str> * Email Bcc:
Michael Witten4ed62b02008-09-30 07:58:30 -050061 --subject <str> * Email "Subject:"
Christian Ludwigd11c9432018-03-04 00:58:14 +010062 --reply-to <str> * Email "Reply-To:"
Michael Witten4ed62b02008-09-30 07:58:30 -050063 --in-reply-to <str> * Email "In-Reply-To:"
Luis Henriquesac1596a2014-03-24 21:38:27 +000064 --[no-]xmailer * Add "X-Mailer:" header (default).
Felipe Contreras402596a2013-04-07 01:10:27 -060065 --[no-]annotate * Review each patch that will be sent in an editor.
Michael Witten4ed62b02008-09-30 07:58:30 -050066 --compose * Open an editor for introduction.
Krzysztof Mazur62e00692012-10-10 01:02:56 +020067 --compose-encoding <str> * Encoding to assume for introduction.
Thomas Rast3cae7e52010-06-17 22:10:39 +020068 --8bit-encoding <str> * Encoding to assume 8bit mails if undeclared
Paolo Bonzini8d814082014-11-25 15:00:27 +010069 --transfer-encoding <str> * Transfer encoding to use (quoted-printable, 8bit, base64)
Michael Witten4ed62b02008-09-30 07:58:30 -050070
71 Sending:
72 --envelope-sender <str> * Email envelope sender.
73 --smtp-server <str:int> * Outgoing SMTP server to use. The port
74 is optional. Default 'localhost'.
Pascal Obry052fbea2010-09-06 20:12:11 +020075 --smtp-server-option <str> * Outgoing SMTP server option to use.
Michael Witten4ed62b02008-09-30 07:58:30 -050076 --smtp-server-port <int> * Outgoing SMTP server port.
77 --smtp-user <str> * Username for SMTP-AUTH.
78 --smtp-pass <str> * Password for SMTP-AUTH; not necessary.
79 --smtp-encryption <str> * tls or ssl; anything else disables.
80 --smtp-ssl * Deprecated. Use '--smtp-encryption ssl'.
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -070081 --smtp-ssl-cert-path <str> * Path to ca-certificates (either directory or file).
82 Pass an empty string to disable certificate
83 verification.
Jari Aalto134550f2010-03-14 17:16:45 +020084 --smtp-domain <str> * The domain name sent to HELO/EHLO handshake
Jan Viktorin0f2e68b2015-08-12 01:39:44 +020085 --smtp-auth <str> * Space-separated list of allowed AUTH mechanisms.
86 This setting forces to use one of the listed mechanisms.
Jari Aaltof60812e2010-03-14 17:16:09 +020087 --smtp-debug <0|1> * Disable, enable Net::SMTP debug.
Michael Witten4ed62b02008-09-30 07:58:30 -050088
xiaoqiang zhao5453b832017-05-21 20:59:50 +080089 --batch-size <int> * send max <int> message per connection.
90 --relogin-delay <int> * delay <int> seconds between two successive login.
91 This option can only be used with --batch-size
92
Michael Witten4ed62b02008-09-30 07:58:30 -050093 Automating:
94 --identity <str> * Use the sendemail.<id> options.
Joe Perches6e74e072010-09-24 10:03:00 -070095 --to-cmd <str> * Email To: via `<str> \$patch_path`
Michael Witten4ed62b02008-09-30 07:58:30 -050096 --cc-cmd <str> * Email Cc: via `<str> \$patch_path`
Jay Soffian3531e272009-02-14 23:32:15 -050097 --suppress-cc <str> * author, self, sob, cc, cccmd, body, bodycc, all.
Michael S. Tsirkinf515c902014-04-29 08:41:16 +030098 --[no-]cc-cover * Email Cc: addresses in the cover letter.
99 --[no-]to-cover * Email To: addresses in the cover letter.
Jay Soffian3531e272009-02-14 23:32:15 -0500100 --[no-]signed-off-by-cc * Send to Signed-off-by: addresses. Default on.
Michael Witten4ed62b02008-09-30 07:58:30 -0500101 --[no-]suppress-from * Send to self. Default off.
Junio C Hamano41fe87f2009-08-22 12:48:48 -0700102 --[no-]chain-reply-to * Chain In-Reply-To: fields. Default off.
Michael Witten4ed62b02008-09-30 07:58:30 -0500103 --[no-]thread * Use In-Reply-To: field. Default on.
104
105 Administering:
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500106 --confirm <str> * Confirm recipients before sending;
107 auto, cc, compose, always, or never.
Michael Witten4ed62b02008-09-30 07:58:30 -0500108 --quiet * Output one line of info per email.
109 --dry-run * Don't actually send the emails.
110 --[no-]validate * Perform patch sanity checks. Default on.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100111 --[no-]format-patch * understand any non optional arguments as
112 `git format-patch` ones.
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200113 --force * Send even if safety checks would prevent it.
Jeff Kingc764a0c2008-01-18 09:20:10 -0500114
Jacob Keller17b7a832015-11-19 14:52:11 -0800115 Information:
116 --dump-aliases * Dump configured aliases and exit.
117
Michael Coleman1b0baf12007-02-27 22:47:54 -0600118EOT
119 exit(1);
120}
121
Eric Wong4bc87a22006-03-25 17:20:48 -0800122# most mail servers generate the Date: header, but not all...
Jakub Narebski6bdca892006-07-07 20:57:55 +0200123sub format_2822_time {
124 my ($time) = @_;
125 my @localtm = localtime($time);
126 my @gmttm = gmtime($time);
127 my $localmin = $localtm[1] + $localtm[2] * 60;
128 my $gmtmin = $gmttm[1] + $gmttm[2] * 60;
129 if ($localtm[0] != $gmttm[0]) {
Vasco Almeida46493102016-12-14 11:54:36 -0100130 die __("local zone differs from GMT by a non-minute interval\n");
Jakub Narebski6bdca892006-07-07 20:57:55 +0200131 }
132 if ((($gmttm[6] + 1) % 7) == $localtm[6]) {
133 $localmin += 1440;
134 } elsif ((($gmttm[6] - 1) % 7) == $localtm[6]) {
135 $localmin -= 1440;
136 } elsif ($gmttm[6] != $localtm[6]) {
Vasco Almeida46493102016-12-14 11:54:36 -0100137 die __("local time offset greater than or equal to 24 hours\n");
Jakub Narebski6bdca892006-07-07 20:57:55 +0200138 }
139 my $offset = $localmin - $gmtmin;
140 my $offhour = $offset / 60;
141 my $offmin = abs($offset % 60);
142 if (abs($offhour) >= 24) {
Vasco Almeida46493102016-12-14 11:54:36 -0100143 die __("local time offset greater than or equal to 24 hours\n");
Jakub Narebski6bdca892006-07-07 20:57:55 +0200144 }
145
146 return sprintf("%s, %2d %s %d %02d:%02d:%02d %s%02d%02d",
147 qw(Sun Mon Tue Wed Thu Fri Sat)[$localtm[6]],
148 $localtm[3],
149 qw(Jan Feb Mar Apr May Jun
150 Jul Aug Sep Oct Nov Dec)[$localtm[4]],
151 $localtm[5]+1900,
152 $localtm[2],
153 $localtm[1],
154 $localtm[0],
155 ($offset >= 0) ? '+' : '-',
156 abs($offhour),
157 $offmin,
158 );
159}
Eric Wong4bc87a22006-03-25 17:20:48 -0800160
Eric Wong567ffeb2006-03-25 16:47:12 -0800161my $have_email_valid = eval { require Email::Valid; 1 };
Eric Wong4bc87a22006-03-25 17:20:48 -0800162my $smtp;
Wincent Colaiuta5f5b6112007-11-21 13:35:05 +0100163my $auth;
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800164my $num_sent = 0;
Eric Wong4bc87a22006-03-25 17:20:48 -0800165
Роман Донченко11f70a72014-12-14 18:59:46 +0300166# Regexes for RFC 2047 productions.
167my $re_token = qr/[^][()<>@,;:\\"\/?.= \000-\037\177-\377]+/;
168my $re_encoded_text = qr/[^? \000-\037\177-\377]+/;
169my $re_encoded_word = qr/=\?($re_token)\?($re_token)\?($re_encoded_text)\?=/;
170
Ryan Anderson83b24432005-07-31 04:17:25 -0400171# Variables we fill in automatically, or via prompting:
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700172my (@to,$no_to,@initial_to,@cc,$no_cc,@initial_cc,@bcclist,$no_bcc,@xh,
Christian Ludwigd11c9432018-03-04 00:58:14 +0100173 $initial_in_reply_to,$reply_to,$initial_subject,@files,
Luis Henriquesac1596a2014-03-24 21:38:27 +0000174 $author,$sender,$smtp_authpass,$annotate,$use_xmailer,$compose,$time);
Ryan Anderson83b24432005-07-31 04:17:25 -0400175
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700176my $envelope_sender;
Ryan Anderson78488b22005-07-31 20:04:24 -0400177
Ryan Anderson91332612005-07-31 20:04:24 -0400178# Example reply to:
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100179#$initial_in_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
Ryan Anderson83b24432005-07-31 04:17:25 -0400180
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100181my $repo = eval { Git->repository() };
182my @repo = $repo ? ($repo) : ();
Junio C Hamano280242d2006-07-02 16:03:59 -0700183my $term = eval {
Jay Soffian0fb7fc72008-02-21 19:16:04 -0500184 $ENV{"GIT_SEND_EMAIL_NOTTY"}
185 ? new Term::ReadLine 'git-send-email', \*STDIN, \*STDOUT
186 : new Term::ReadLine 'git-send-email';
Junio C Hamano280242d2006-07-02 16:03:59 -0700187};
188if ($@) {
189 $term = new FakeTerm "$@: going non-interactive";
190}
Ryan Anderson83b24432005-07-31 04:17:25 -0400191
Adam Roben5483c712007-06-27 20:59:37 -0700192# Behavior modification variables
193my ($quiet, $dry_run) = (0, 0);
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100194my $format_patch;
Jay Soffianafe756c2009-02-23 13:51:37 -0500195my $compose_filename;
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200196my $force = 0;
Jacob Keller17b7a832015-11-19 14:52:11 -0800197my $dump_aliases = 0;
Adam Roben5483c712007-06-27 20:59:37 -0700198
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100199# Handle interactive edition of files.
200my $multiedit;
Michael J Gruber0ce142c2010-03-22 17:12:53 +0100201my $editor;
Jonathan Niederb4479f02009-10-30 20:42:34 -0500202
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100203sub do_edit {
Michael J Gruber0ce142c2010-03-22 17:12:53 +0100204 if (!defined($editor)) {
205 $editor = Git::command_oneline('var', 'GIT_EDITOR');
206 }
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100207 if (defined($multiedit) && !$multiedit) {
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100208 map {
209 system('sh', '-c', $editor.' "$@"', $editor, $_);
210 if (($? & 127) || ($? >> 8)) {
Vasco Almeida46493102016-12-14 11:54:36 -0100211 die(__("the editor exited uncleanly, aborting everything"));
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100212 }
213 } @_;
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100214 } else {
215 system('sh', '-c', $editor.' "$@"', $editor, @_);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100216 if (($? & 127) || ($? >> 8)) {
Vasco Almeida46493102016-12-14 11:54:36 -0100217 die(__("the editor exited uncleanly, aborting everything"));
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100218 }
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100219 }
220}
Adam Roben5483c712007-06-27 20:59:37 -0700221
222# Variables with corresponding config settings
Joe Perches6e74e072010-09-24 10:03:00 -0700223my ($thread, $chain_reply_to, $suppress_from, $signed_off_by_cc);
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300224my ($cover_cc, $cover_to);
Joe Perches6e74e072010-09-24 10:03:00 -0700225my ($to_cmd, $cc_cmd);
Pascal Obry052fbea2010-09-06 20:12:11 +0200226my ($smtp_server, $smtp_server_port, @smtp_server_options);
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -0700227my ($smtp_authuser, $smtp_encryption, $smtp_ssl_cert_path);
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800228my ($batch_size, $relogin_delay);
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200229my ($identity, $aliasfiletype, @alias_files, $smtp_domain, $smtp_auth);
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500230my ($validate, $confirm);
David Brown65648282007-12-25 19:56:29 -0800231my (@suppress_cc);
Thomas Rast3cae7e52010-06-17 22:10:39 +0200232my ($auto_8bit_encoding);
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200233my ($compose_encoding);
brian m. carlsone67a2282018-07-08 22:17:12 +0000234my $target_xfer_encoding = 'auto';
Adam Roben5483c712007-06-27 20:59:37 -0700235
Jari Aaltof60812e2010-03-14 17:16:09 +0200236my ($debug_net_smtp) = 0; # Net::SMTP, see send_message()
237
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900238my %config_bool_settings = (
Adam Roben5483c712007-06-27 20:59:37 -0700239 "thread" => [\$thread, 1],
Felipe Contrerasb99d22f2013-05-24 22:44:52 -0500240 "chainreplyto" => [\$chain_reply_to, 0],
David Brown65648282007-12-25 19:56:29 -0800241 "suppressfrom" => [\$suppress_from, undef],
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500242 "signedoffbycc" => [\$signed_off_by_cc, undef],
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300243 "cccover" => [\$cover_cc, undef],
244 "tocover" => [\$cover_to, undef],
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500245 "signedoffcc" => [\$signed_off_by_cc, undef], # Deprecated
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500246 "validate" => [\$validate, 1],
Felipe Contreras402596a2013-04-07 01:10:27 -0600247 "multiedit" => [\$multiedit, undef],
Luis Henriquesac1596a2014-03-24 21:38:27 +0000248 "annotate" => [\$annotate, undef],
249 "xmailer" => [\$use_xmailer, 1]
Adam Robene46f7a02007-06-26 15:48:30 -0700250);
251
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900252my %config_settings = (
253 "smtpserver" => \$smtp_server,
Junio C Hamano44b24762007-09-25 17:27:54 -0700254 "smtpserverport" => \$smtp_server_port,
Pascal Obry052fbea2010-09-06 20:12:11 +0200255 "smtpserveroption" => \@smtp_server_options,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900256 "smtpuser" => \$smtp_authuser,
257 "smtppass" => \$smtp_authpass,
Pascal Obrye1e91152010-09-06 20:12:09 +0200258 "smtpdomain" => \$smtp_domain,
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200259 "smtpauth" => \$smtp_auth,
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800260 "smtpbatchsize" => \$batch_size,
261 "smtprelogindelay" => \$relogin_delay,
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700262 "to" => \@initial_to,
Joe Perches6e74e072010-09-24 10:03:00 -0700263 "tocmd" => \$to_cmd,
Miklos Vajna5f8b9fc2008-04-27 14:14:58 +0200264 "cc" => \@initial_cc,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900265 "cccmd" => \$cc_cmd,
266 "aliasfiletype" => \$aliasfiletype,
267 "bcc" => \@bcclist,
David Brown65648282007-12-25 19:56:29 -0800268 "suppresscc" => \@suppress_cc,
Ask Bjørn Hansen9f7820a2008-06-07 00:33:42 -0700269 "envelopesender" => \$envelope_sender,
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500270 "confirm" => \$confirm,
Trent Piepho09caa242009-05-12 15:48:56 -0700271 "from" => \$sender,
Thomas Rast3cae7e52010-06-17 22:10:39 +0200272 "assume8bitencoding" => \$auto_8bit_encoding,
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200273 "composeencoding" => \$compose_encoding,
Paolo Bonzini8d814082014-11-25 15:00:27 +0100274 "transferencoding" => \$target_xfer_encoding,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900275);
Avi Kivity4a62d3f2007-03-11 19:19:44 +0200276
Cord Seelecec5dae2011-09-30 12:52:25 +0200277my %config_path_settings = (
278 "aliasesfile" => \@alias_files,
John Keeping6e07a3b2015-11-17 22:01:05 +0000279 "smtpsslcertpath" => \$smtp_ssl_cert_path,
Cord Seelecec5dae2011-09-30 12:52:25 +0200280);
281
Michael Witten87429972008-02-03 19:53:57 -0500282# Handle Uncouth Termination
283sub signal_handler {
284
285 # Make text normal
286 print color("reset"), "\n";
287
288 # SMTP password masked
289 system "stty echo";
290
291 # tmp files from --compose
Jay Soffianafe756c2009-02-23 13:51:37 -0500292 if (defined $compose_filename) {
293 if (-e $compose_filename) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100294 printf __("'%s' contains an intermediate version ".
295 "of the email you were composing.\n"),
296 $compose_filename;
Jay Soffianafe756c2009-02-23 13:51:37 -0500297 }
298 if (-e ($compose_filename . ".final")) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100299 printf __("'%s.final' contains the composed email.\n"),
300 $compose_filename;
Jay Soffianafe756c2009-02-23 13:51:37 -0500301 }
Michael Witten87429972008-02-03 19:53:57 -0500302 }
303
304 exit;
305};
306
307$SIG{TERM} = \&signal_handler;
308$SIG{INT} = \&signal_handler;
309
Ryan Anderson83b24432005-07-31 04:17:25 -0400310# Begin by accumulating all the variables (defined above), that we will end up
311# needing, first, from the command line:
312
Clemens Buchacherc5978242011-09-03 19:06:13 +0200313my $help;
314my $rc = GetOptions("h" => \$help,
Jacob Keller17b7a832015-11-19 14:52:11 -0800315 "dump-aliases" => \$dump_aliases);
316usage() unless $rc;
Vasco Almeida46493102016-12-14 11:54:36 -0100317die __("--dump-aliases incompatible with other options\n")
Jacob Keller17b7a832015-11-19 14:52:11 -0800318 if !$help and $dump_aliases and @ARGV;
319$rc = GetOptions(
Clemens Buchacherc5978242011-09-03 19:06:13 +0200320 "sender|from=s" => \$sender,
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100321 "in-reply-to=s" => \$initial_in_reply_to,
Christian Ludwigd11c9432018-03-04 00:58:14 +0100322 "reply-to=s" => \$reply_to,
Ryan Anderson83b24432005-07-31 04:17:25 -0400323 "subject=s" => \$initial_subject,
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700324 "to=s" => \@initial_to,
Joe Perches6e74e072010-09-24 10:03:00 -0700325 "to-cmd=s" => \$to_cmd,
Stephen Boydf434c082010-03-07 14:46:48 -0800326 "no-to" => \$no_to,
Ryan Andersonda140f82006-02-13 03:05:15 -0500327 "cc=s" => \@initial_cc,
Stephen Boydf434c082010-03-07 14:46:48 -0800328 "no-cc" => \$no_cc,
Ryan Anderson58063242006-05-29 12:30:13 -0700329 "bcc=s" => \@bcclist,
Stephen Boydf434c082010-03-07 14:46:48 -0800330 "no-bcc" => \$no_bcc,
Ryan Anderson78488b22005-07-31 20:04:24 -0400331 "chain-reply-to!" => \$chain_reply_to,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800332 "no-chain-reply-to" => sub {$chain_reply_to = 0},
Ryan Anderson3342d852005-07-31 20:04:24 -0400333 "smtp-server=s" => \$smtp_server,
Pascal Obry052fbea2010-09-06 20:12:11 +0200334 "smtp-server-option=s" => \@smtp_server_options,
Junio C Hamano44b24762007-09-25 17:27:54 -0700335 "smtp-server-port=s" => \$smtp_server_port,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900336 "smtp-user=s" => \$smtp_authuser,
Michael Witten2363d742008-02-03 19:53:56 -0500337 "smtp-pass:s" => \$smtp_authpass,
Thomas Rastf6bebd12008-06-25 21:42:43 +0200338 "smtp-ssl" => sub { $smtp_encryption = 'ssl' },
339 "smtp-encryption=s" => \$smtp_encryption,
Thomas Rast979e6522013-12-01 23:48:42 +0100340 "smtp-ssl-cert-path=s" => \$smtp_ssl_cert_path,
Jari Aaltof60812e2010-03-14 17:16:09 +0200341 "smtp-debug:i" => \$debug_net_smtp,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -0400342 "smtp-domain:s" => \$smtp_domain,
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200343 "smtp-auth=s" => \$smtp_auth,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900344 "identity=s" => \$identity,
Felipe Contreras402596a2013-04-07 01:10:27 -0600345 "annotate!" => \$annotate,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800346 "no-annotate" => sub {$annotate = 0},
Ryan Anderson1f038a02005-09-05 01:13:07 -0400347 "compose" => \$compose,
Ryan Anderson30d08b32006-02-02 11:56:06 -0500348 "quiet" => \$quiet,
Joe Perches324a8bd2007-08-17 18:51:12 -0700349 "cc-cmd=s" => \$cc_cmd,
Adam Roben5483c712007-06-27 20:59:37 -0700350 "suppress-from!" => \$suppress_from,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800351 "no-suppress-from" => sub {$suppress_from = 0},
David Brown65648282007-12-25 19:56:29 -0800352 "suppress-cc=s" => \@suppress_cc,
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500353 "signed-off-cc|signed-off-by-cc!" => \$signed_off_by_cc,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800354 "no-signed-off-cc|no-signed-off-by-cc" => sub {$signed_off_by_cc = 0},
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300355 "cc-cover|cc-cover!" => \$cover_cc,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800356 "no-cc-cover" => sub {$cover_cc = 0},
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300357 "to-cover|to-cover!" => \$cover_to,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800358 "no-to-cover" => sub {$cover_to = 0},
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500359 "confirm=s" => \$confirm,
Matthew Wilcox61302592006-10-10 08:58:23 -0600360 "dry-run" => \$dry_run,
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700361 "envelope-sender=s" => \$envelope_sender,
Adam Roben5483c712007-06-27 20:59:37 -0700362 "thread!" => \$thread,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800363 "no-thread" => sub {$thread = 0},
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500364 "validate!" => \$validate,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800365 "no-validate" => sub {$validate = 0},
Paolo Bonzini8d814082014-11-25 15:00:27 +0100366 "transfer-encoding=s" => \$target_xfer_encoding,
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100367 "format-patch!" => \$format_patch,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800368 "no-format-patch" => sub {$format_patch = 0},
Thomas Rast3cae7e52010-06-17 22:10:39 +0200369 "8bit-encoding=s" => \$auto_8bit_encoding,
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200370 "compose-encoding=s" => \$compose_encoding,
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200371 "force" => \$force,
Luis Henriquesac1596a2014-03-24 21:38:27 +0000372 "xmailer!" => \$use_xmailer,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800373 "no-xmailer" => sub {$use_xmailer = 0},
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800374 "batch-size=i" => \$batch_size,
375 "relogin-delay=i" => \$relogin_delay,
Ryan Anderson83b24432005-07-31 04:17:25 -0400376 );
377
Clemens Buchacherc5978242011-09-03 19:06:13 +0200378usage() if $help;
Michael Coleman1b0baf12007-02-27 22:47:54 -0600379unless ($rc) {
380 usage();
381}
382
Vasco Almeida46493102016-12-14 11:54:36 -0100383die __("Cannot run git format-patch from outside a repository\n")
Jay Soffianeed6ca72009-02-14 23:32:13 -0500384 if $format_patch and not $repo;
385
Stefan Beller9caa7062018-02-12 11:44:04 -0800386die __("`batch-size` and `relogin` must be specified together " .
387 "(via command-line or configuration option)\n")
388 if defined $relogin_delay and not defined $batch_size;
389
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900390# Now, let's fill any that aren't set in with defaults:
391
392sub read_config {
393 my ($prefix) = @_;
394
395 foreach my $setting (keys %config_bool_settings) {
396 my $target = $config_bool_settings{$setting}->[0];
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100397 $$target = Git::config_bool(@repo, "$prefix.$setting") unless (defined $$target);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900398 }
399
Cord Seelecec5dae2011-09-30 12:52:25 +0200400 foreach my $setting (keys %config_path_settings) {
Cord Seele463b0ea2011-10-14 22:53:31 +0200401 my $target = $config_path_settings{$setting};
402 if (ref($target) eq "ARRAY") {
403 unless (@$target) {
404 my @values = Git::config_path(@repo, "$prefix.$setting");
405 @$target = @values if (@values && defined $values[0]);
406 }
407 }
408 else {
409 $$target = Git::config_path(@repo, "$prefix.$setting") unless (defined $$target);
410 }
Cord Seelecec5dae2011-09-30 12:52:25 +0200411 }
412
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900413 foreach my $setting (keys %config_settings) {
414 my $target = $config_settings{$setting};
Stephen Boydf434c082010-03-07 14:46:48 -0800415 next if $setting eq "to" and defined $no_to;
416 next if $setting eq "cc" and defined $no_cc;
417 next if $setting eq "bcc" and defined $no_bcc;
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900418 if (ref($target) eq "ARRAY") {
419 unless (@$target) {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100420 my @values = Git::config(@repo, "$prefix.$setting");
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900421 @$target = @values if (@values && defined $values[0]);
422 }
423 }
424 else {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100425 $$target = Git::config(@repo, "$prefix.$setting") unless (defined $$target);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900426 }
427 }
Thomas Rastf6bebd12008-06-25 21:42:43 +0200428
429 if (!defined $smtp_encryption) {
430 my $enc = Git::config(@repo, "$prefix.smtpencryption");
431 if (defined $enc) {
432 $smtp_encryption = $enc;
433 } elsif (Git::config_bool(@repo, "$prefix.smtpssl")) {
434 $smtp_encryption = 'ssl';
435 }
436 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900437}
438
439# read configuration from [sendemail "$identity"], fall back on [sendemail]
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100440$identity = Git::config(@repo, "sendemail.identity") unless (defined $identity);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900441read_config("sendemail.$identity") if (defined $identity);
442read_config("sendemail");
443
444# fall back on builtin bool defaults
445foreach my $setting (values %config_bool_settings) {
446 ${$setting->[0]} = $setting->[1] unless (defined (${$setting->[0]}));
447}
448
Thomas Rastfa835cd2008-06-26 23:03:21 +0200449# 'default' encryption is none -- this only prevents a warning
450$smtp_encryption = '' unless (defined $smtp_encryption);
451
David Brown65648282007-12-25 19:56:29 -0800452# Set CC suppressions
453my(%suppress_cc);
454if (@suppress_cc) {
455 foreach my $entry (@suppress_cc) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100456 die sprintf(__("Unknown --suppress-cc field: '%s'\n"), $entry)
Ævar Arnfjörð Bjarmasone9bf7412010-09-30 13:43:04 +0000457 unless $entry =~ /^(?:all|cccmd|cc|author|self|sob|body|bodycc)$/;
David Brown65648282007-12-25 19:56:29 -0800458 $suppress_cc{$entry} = 1;
459 }
460}
461
462if ($suppress_cc{'all'}) {
Paolo Bonzinicb8a9bd2009-06-18 14:31:32 +0200463 foreach my $entry (qw (cccmd cc author self sob body bodycc)) {
David Brown65648282007-12-25 19:56:29 -0800464 $suppress_cc{$entry} = 1;
465 }
466 delete $suppress_cc{'all'};
467}
468
469# If explicit old-style ones are specified, they trump --suppress-cc.
470$suppress_cc{'self'} = $suppress_from if defined $suppress_from;
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500471$suppress_cc{'sob'} = !$signed_off_by_cc if defined $signed_off_by_cc;
David Brown65648282007-12-25 19:56:29 -0800472
Jay Soffian3531e272009-02-14 23:32:15 -0500473if ($suppress_cc{'body'}) {
474 foreach my $entry (qw (sob bodycc)) {
475 $suppress_cc{$entry} = 1;
476 }
477 delete $suppress_cc{'body'};
478}
479
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500480# Set confirm's default value
481my $confirm_unconfigured = !defined $confirm;
482if ($confirm_unconfigured) {
483 $confirm = scalar %suppress_cc ? 'compose' : 'auto';
484};
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100485die sprintf(__("Unknown --confirm setting: '%s'\n"), $confirm)
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500486 unless $confirm =~ /^(?:auto|cc|compose|always|never)/;
487
David Brown65648282007-12-25 19:56:29 -0800488# Debugging, print out the suppressions.
489if (0) {
490 print "suppressions:\n";
491 foreach my $entry (keys %suppress_cc) {
492 printf " %-5s -> $suppress_cc{$entry}\n", $entry;
493 }
494}
495
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100496my ($repoauthor, $repocommitter);
497($repoauthor) = Git::ident_person(@repo, 'author');
498($repocommitter) = Git::ident_person(@repo, 'committer');
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900499
Jay Soffian50126992009-02-14 23:32:14 -0500500sub parse_address_line {
Matthieu Moybd869f62018-01-05 19:36:51 +0100501 return map { $_->format } Mail::Address->parse($_[0]);
Jay Soffian50126992009-02-14 23:32:14 -0500502}
503
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800504sub split_addrs {
Junio C Hamano2f0e7cb2008-12-21 01:57:59 -0800505 return quotewords('\s*,\s*', 1, @_);
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800506}
507
Eric Wong994d6c62006-05-14 19:13:44 -0700508my %aliases;
Eric Sunshine09f11572015-05-31 18:29:27 -0400509
510sub parse_sendmail_alias {
511 local $_ = shift;
512 if (/"/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100513 printf STDERR __("warning: sendmail alias with quotes is not supported: %s\n"), $_;
Eric Sunshine86b89842015-06-01 14:22:36 -0400514 } elsif (/:include:/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100515 printf STDERR __("warning: `:include:` not supported: %s\n"), $_;
Eric Sunshine86b89842015-06-01 14:22:36 -0400516 } elsif (/[\/|]/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100517 printf STDERR __("warning: `/file` or `|pipe` redirection not supported: %s\n"), $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400518 } elsif (/^(\S+?)\s*:\s*(.+)$/) {
519 my ($alias, $addr) = ($1, $2);
520 $aliases{$alias} = [ split_addrs($addr) ];
521 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100522 printf STDERR __("warning: sendmail line is not recognized: %s\n"), $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400523 }
524}
525
526sub parse_sendmail_aliases {
527 my $fh = shift;
Eric Sunshine2532dd02015-05-31 18:29:29 -0400528 my $s = '';
Eric Sunshine09f11572015-05-31 18:29:27 -0400529 while (<$fh>) {
Eric Sunshine2532dd02015-05-31 18:29:29 -0400530 chomp;
Eric Sunshine020be85f2015-05-31 18:29:28 -0400531 next if /^\s*$/ || /^\s*#/;
Eric Sunshine2532dd02015-05-31 18:29:29 -0400532 $s .= $_, next if $s =~ s/\\$// || s/^\s+//;
533 parse_sendmail_alias($s) if $s;
534 $s = $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400535 }
Eric Sunshine2532dd02015-05-31 18:29:29 -0400536 $s =~ s/\\$//; # silently tolerate stray '\' on last line
537 parse_sendmail_alias($s) if $s;
Eric Sunshine09f11572015-05-31 18:29:27 -0400538}
539
Eric Wong994d6c62006-05-14 19:13:44 -0700540my %parse_alias = (
541 # multiline formats can be supported in the future
542 mutt => sub { my $fh = shift; while (<$fh>) {
Felipe Contrerasffc01f92009-09-30 17:49:36 +0300543 if (/^\s*alias\s+(?:-group\s+\S+\s+)*(\S+)\s+(.*)$/) {
Eric Wong994d6c62006-05-14 19:13:44 -0700544 my ($alias, $addr) = ($1, $2);
545 $addr =~ s/#.*$//; # mutt allows # comments
Eric Wong2c510f22016-01-04 20:53:30 +0000546 # commas delimit multiple addresses
547 my @addr = split_addrs($addr);
548
549 # quotes may be escaped in the file,
550 # unescape them so we do not double-escape them later.
551 s/\\"/"/g foreach @addr;
552 $aliases{$alias} = \@addr
Eric Wong994d6c62006-05-14 19:13:44 -0700553 }}},
554 mailrc => sub { my $fh = shift; while (<$fh>) {
Jeff Kinga277d1e2016-03-17 19:58:22 -0400555 if (/^alias\s+(\S+)\s+(.*?)\s*$/) {
Eric Wong994d6c62006-05-14 19:13:44 -0700556 # spaces delimit multiple addresses
Eric W. Biedermanfe87c922009-05-20 19:45:53 -0700557 $aliases{$1} = [ quotewords('\s+', 0, $2) ];
Eric Wong994d6c62006-05-14 19:13:44 -0700558 }}},
Trent Piepho73c427e2008-11-25 18:55:00 -0800559 pine => sub { my $fh = shift; my $f='\t[^\t]*';
560 for (my $x = ''; defined($x); $x = $_) {
561 chomp $x;
562 $x .= $1 while(defined($_ = <$fh>) && /^ +(.*)$/);
563 $x =~ /^(\S+)$f\t\(?([^\t]+?)\)?(:?$f){0,2}$/ or next;
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800564 $aliases{$1} = [ split_addrs($2) ];
Trent Piepho73c427e2008-11-25 18:55:00 -0800565 }},
Bill Pemberton7613ea32009-04-22 09:41:29 -0400566 elm => sub { my $fh = shift;
567 while (<$fh>) {
568 if (/^(\S+)\s+=\s+[^=]+=\s(\S+)/) {
569 my ($alias, $addr) = ($1, $2);
570 $aliases{$alias} = [ split_addrs($addr) ];
571 }
572 } },
Eric Sunshine09f11572015-05-31 18:29:27 -0400573 sendmail => \&parse_sendmail_aliases,
Eric Wong994d6c62006-05-14 19:13:44 -0700574 gnus => sub { my $fh = shift; while (<$fh>) {
575 if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
576 $aliases{$1} = [ $2 ];
577 }}}
578);
579
Petr Baudis3cb8caf2006-07-03 22:47:58 +0200580if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) {
Eric Wong994d6c62006-05-14 19:13:44 -0700581 foreach my $file (@alias_files) {
582 open my $fh, '<', $file or die "opening $file: $!\n";
583 $parse_alias{$aliasfiletype}->($fh);
584 close $fh;
585 }
586}
587
Jacob Keller17b7a832015-11-19 14:52:11 -0800588if ($dump_aliases) {
589 print "$_\n" for (sort keys %aliases);
590 exit(0);
591}
592
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700593# is_format_patch_arg($f) returns 0 if $f names a patch, or 1 if
594# $f is a revision list specification to be passed to format-patch.
595sub is_format_patch_arg {
Jay Soffianeed6ca72009-02-14 23:32:13 -0500596 return unless $repo;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100597 my $f = shift;
598 try {
599 $repo->command('rev-parse', '--verify', '--quiet', $f);
600 if (defined($format_patch)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100601 return $format_patch;
602 }
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100603 die sprintf(__ <<EOF, $f, $f);
604File '%s' exists but it could also be the range of commits
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100605to produce patches for. Please disambiguate by...
606
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100607 * Saying "./%s" if you mean a file; or
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100608 * Giving --format-patch option if you mean a range.
609EOF
610 } catch Git::Error::Command with {
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700611 # Not a valid revision. Treat it as a filename.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100612 return 0;
613 }
614}
615
Jeff Kingaa548922008-01-18 09:19:36 -0500616# Now that all the defaults are set, process the rest of the command line
617# arguments and collect up the files that need to be processed.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100618my @rev_list_opts;
Junio C Hamano69f4ce52008-11-30 22:38:20 -0800619while (defined(my $f = shift @ARGV)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100620 if ($f eq "--") {
621 push @rev_list_opts, "--", @ARGV;
622 @ARGV = ();
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700623 } elsif (-d $f and !is_format_patch_arg($f)) {
Ævar Arnfjörð Bjarmasonc6038162010-09-30 13:42:54 +0000624 opendir my $dh, $f
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100625 or die sprintf(__("Failed to opendir %s: %s"), $f, $!);
Jeff Kingaa548922008-01-18 09:19:36 -0500626
Ævar Arnfjörð Bjarmason89bf1ba2010-09-14 19:02:24 +0000627 push @files, grep { -f $_ } map { catfile($f, $_) }
Ævar Arnfjörð Bjarmasonc6038162010-09-30 13:42:54 +0000628 sort readdir $dh;
629 closedir $dh;
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700630 } elsif ((-f $f or -p $f) and !is_format_patch_arg($f)) {
Jeff Kingaa548922008-01-18 09:19:36 -0500631 push @files, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500632 } else {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100633 push @rev_list_opts, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500634 }
635}
636
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100637if (@rev_list_opts) {
Vasco Almeida46493102016-12-14 11:54:36 -0100638 die __("Cannot run git format-patch from outside a repository\n")
Jay Soffianeed6ca72009-02-14 23:32:13 -0500639 unless $repo;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100640 push @files, $repo->command('format-patch', '-o', tempdir(CLEANUP => 1), @rev_list_opts);
641}
642
Junio C Hamano531220b2016-03-17 22:40:05 -0700643@files = handle_backup_files(@files);
644
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500645if ($validate) {
Jeff Kingc764a0c2008-01-18 09:20:10 -0500646 foreach my $f (@files) {
Kevin Ballard300913b2008-06-25 15:44:40 -0700647 unless (-p $f) {
brian m. carlsonf2d06fb2018-07-08 22:17:11 +0000648 my $error = validate_patch($f, $target_xfer_encoding);
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100649 $error and die sprintf(__("fatal: %s: %s\nwarning: no patches were sent\n"),
650 $f, $error);
Kevin Ballard300913b2008-06-25 15:44:40 -0700651 }
Jeff Kingc764a0c2008-01-18 09:20:10 -0500652 }
Jeff King747bbff2008-01-18 09:19:48 -0500653}
654
Jeff Kingaa548922008-01-18 09:19:36 -0500655if (@files) {
656 unless ($quiet) {
657 print $_,"\n" for (@files);
658 }
659} else {
Vasco Almeida46493102016-12-14 11:54:36 -0100660 print STDERR __("\nNo patch files specified!\n\n");
Jeff Kingaa548922008-01-18 09:19:36 -0500661 usage();
662}
663
Ævar Arnfjörð Bjarmasonacf071b2010-09-30 13:42:57 +0000664sub get_patch_subject {
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100665 my $fn = shift;
666 open (my $fh, '<', $fn);
667 while (my $line = <$fh>) {
668 next unless ($line =~ /^Subject: (.*)$/);
669 close $fh;
670 return "GIT: $1\n";
671 }
672 close $fh;
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100673 die sprintf(__("No subject line in %s?"), $fn);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100674}
675
676if ($compose) {
677 # Note that this does not need to be secure, but we will make a small
678 # effort to have it be unique
Jay Soffianafe756c2009-02-23 13:51:37 -0500679 $compose_filename = ($repo ?
680 tempfile(".gitsendemail.msg.XXXXXX", DIR => $repo->repo_path()) :
681 tempfile(".gitsendemail.msg.XXXXXX", DIR => "."))[1];
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000682 open my $c, ">", $compose_filename
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100683 or die sprintf(__("Failed to open for writing %s: %s"), $compose_filename, $!);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100684
685
686 my $tpl_sender = $sender || $repoauthor || $repocommitter || '';
687 my $tpl_subject = $initial_subject || '';
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100688 my $tpl_in_reply_to = $initial_in_reply_to || '';
Christian Ludwigd11c9432018-03-04 00:58:14 +0100689 my $tpl_reply_to = $reply_to || '';
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100690
Vasco Almeida70aedfb2016-12-14 11:54:38 -0100691 print $c <<EOT1, Git::prefix_lines("GIT: ", __ <<EOT2), <<EOT3;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100692From $tpl_sender # This line is ignored.
Vasco Almeida70aedfb2016-12-14 11:54:38 -0100693EOT1
694Lines beginning in "GIT:" will be removed.
695Consider including an overall diffstat or table of contents
696for the patch you are writing.
697
698Clear the body content if you don't wish to send a summary.
699EOT2
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100700From: $tpl_sender
Christian Ludwigd11c9432018-03-04 00:58:14 +0100701Reply-To: $tpl_reply_to
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100702Subject: $tpl_subject
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100703In-Reply-To: $tpl_in_reply_to
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100704
Vasco Almeida70aedfb2016-12-14 11:54:38 -0100705EOT3
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100706 for my $f (@files) {
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000707 print $c get_patch_subject($f);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100708 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000709 close $c;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100710
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100711 if ($annotate) {
712 do_edit($compose_filename, @files);
713 } else {
714 do_edit($compose_filename);
715 }
716
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000717 open $c, "<", $compose_filename
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100718 or die sprintf(__("Failed to open %s: %s"), $compose_filename, $!);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100719
Krzysztof Mazur4a47a4d2012-10-22 14:41:48 +0200720 if (!defined $compose_encoding) {
721 $compose_encoding = "UTF-8";
722 }
Nathan Payreb6049542017-12-15 16:33:39 +0100723
724 my %parsed_email;
725 while (my $line = <$c>) {
726 next if $line =~ m/^GIT:/;
727 parse_header_line($line, \%parsed_email);
728 if ($line =~ /^$/) {
729 $parsed_email{'body'} = filter_body($c);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100730 }
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100731 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000732 close $c;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100733
Nathan Payreb6049542017-12-15 16:33:39 +0100734 open my $c2, ">", $compose_filename . ".final"
735 or die sprintf(__("Failed to open %s.final: %s"), $compose_filename, $!);
736
737
738 if ($parsed_email{'From'}) {
739 $sender = delete($parsed_email{'From'});
740 }
741 if ($parsed_email{'In-Reply-To'}) {
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100742 $initial_in_reply_to = delete($parsed_email{'In-Reply-To'});
Nathan Payreb6049542017-12-15 16:33:39 +0100743 }
Christian Ludwigd11c9432018-03-04 00:58:14 +0100744 if ($parsed_email{'Reply-To'}) {
745 $reply_to = delete($parsed_email{'Reply-To'});
Nathan Payreb6049542017-12-15 16:33:39 +0100746 }
747 if ($parsed_email{'Subject'}) {
748 $initial_subject = delete($parsed_email{'Subject'});
749 print $c2 "Subject: " .
750 quote_subject($initial_subject, $compose_encoding) .
751 "\n";
752 }
753
754 if ($parsed_email{'MIME-Version'}) {
755 print $c2 "MIME-Version: $parsed_email{'MIME-Version'}\n",
756 "Content-Type: $parsed_email{'Content-Type'};\n",
757 "Content-Transfer-Encoding: $parsed_email{'Content-Transfer-Encoding'}\n";
758 delete($parsed_email{'MIME-Version'});
759 delete($parsed_email{'Content-Type'});
760 delete($parsed_email{'Content-Transfer-Encoding'});
761 } elsif (file_has_nonascii($compose_filename)) {
762 my $content_type = (delete($parsed_email{'Content-Type'}) or
763 "text/plain; charset=$compose_encoding");
764 print $c2 "MIME-Version: 1.0\n",
765 "Content-Type: $content_type\n",
766 "Content-Transfer-Encoding: 8bit\n";
767 }
768 # Preserve unknown headers
769 foreach my $key (keys %parsed_email) {
770 next if $key eq 'body';
771 print $c2 "$key: $parsed_email{$key}";
772 }
773
774 if ($parsed_email{'body'}) {
775 print $c2 "\n$parsed_email{'body'}\n";
776 delete($parsed_email{'body'});
777 } else {
Vasco Almeida46493102016-12-14 11:54:36 -0100778 print __("Summary email is empty, skipping it\n");
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100779 $compose = -1;
780 }
Nathan Payreb6049542017-12-15 16:33:39 +0100781
782 close $c2;
783
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100784} elsif ($annotate) {
785 do_edit(@files);
786}
787
Jay Soffian6e182512009-03-28 21:39:10 -0400788sub ask {
789 my ($prompt, %arg) = @_;
Jay Soffian0da43a62009-04-04 23:23:21 -0400790 my $valid_re = $arg{valid_re};
Jay Soffian6e182512009-03-28 21:39:10 -0400791 my $default = $arg{default};
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700792 my $confirm_only = $arg{confirm_only};
Jay Soffian6e182512009-03-28 21:39:10 -0400793 my $resp;
794 my $i = 0;
Jay Soffian5906f542009-03-31 12:22:11 -0400795 return defined $default ? $default : undef
796 unless defined $term->IN and defined fileno($term->IN) and
797 defined $term->OUT and defined fileno($term->OUT);
Jay Soffian6e182512009-03-28 21:39:10 -0400798 while ($i++ < 10) {
799 $resp = $term->readline($prompt);
800 if (!defined $resp) { # EOF
801 print "\n";
802 return defined $default ? $default : undef;
803 }
804 if ($resp eq '' and defined $default) {
805 return $default;
806 }
Jay Soffian0da43a62009-04-04 23:23:21 -0400807 if (!defined $valid_re or $resp =~ /$valid_re/) {
Jay Soffian6e182512009-03-28 21:39:10 -0400808 return $resp;
809 }
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700810 if ($confirm_only) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100811 my $yesno = $term->readline(
812 # TRANSLATORS: please keep [y/N] as is.
813 sprintf(__("Are you sure you want to use <%s> [y/N]? "), $resp));
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700814 if (defined $yesno && $yesno =~ /y/i) {
815 return $resp;
816 }
817 }
Jay Soffian6e182512009-03-28 21:39:10 -0400818 }
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -0700819 return;
Jay Soffian6e182512009-03-28 21:39:10 -0400820}
821
Nathan Payreb6049542017-12-15 16:33:39 +0100822sub parse_header_line {
823 my $lines = shift;
824 my $parsed_line = shift;
825 my $addr_pat = join "|", qw(To Cc Bcc);
826
827 foreach (split(/\n/, $lines)) {
828 if (/^($addr_pat):\s*(.+)$/i) {
829 $parsed_line->{$1} = [ parse_address_line($2) ];
830 } elsif (/^([^:]*):\s*(.+)\s*$/i) {
831 $parsed_line->{$1} = $2;
832 }
833 }
834}
835
836sub filter_body {
837 my $c = shift;
838 my $body = "";
839 while (my $body_line = <$c>) {
840 if ($body_line !~ m/^GIT:/) {
841 $body .= $body_line;
842 }
843 }
844 return $body;
845}
846
847
Thomas Rast3cae7e52010-06-17 22:10:39 +0200848my %broken_encoding;
849
Ævar Arnfjörð Bjarmason1d50bfd2010-09-30 13:42:58 +0000850sub file_declares_8bit_cte {
Thomas Rast3cae7e52010-06-17 22:10:39 +0200851 my $fn = shift;
852 open (my $fh, '<', $fn);
853 while (my $line = <$fh>) {
854 last if ($line =~ /^$/);
855 return 1 if ($line =~ /^Content-Transfer-Encoding: .*8bit.*$/);
856 }
857 close $fh;
858 return 0;
859}
860
861foreach my $f (@files) {
862 next unless (body_or_subject_has_nonascii($f)
863 && !file_declares_8bit_cte($f));
864 $broken_encoding{$f} = 1;
865}
866
867if (!defined $auto_8bit_encoding && scalar %broken_encoding) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100868 print __("The following files are 8bit, but do not declare " .
869 "a Content-Transfer-Encoding.\n");
Thomas Rast3cae7e52010-06-17 22:10:39 +0200870 foreach my $f (sort keys %broken_encoding) {
871 print " $f\n";
872 }
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100873 $auto_8bit_encoding = ask(__("Which 8bit encoding should I declare [UTF-8]? "),
Junio C Hamano852a15d2015-02-13 12:20:25 -0800874 valid_re => qr/.{4}/, confirm_only => 1,
Thomas Rast3cae7e52010-06-17 22:10:39 +0200875 default => "UTF-8");
876}
877
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200878if (!$force) {
879 for my $f (@files) {
Ævar Arnfjörð Bjarmason0d290a42010-09-30 13:43:01 +0000880 if (get_patch_subject($f) =~ /\Q*** SUBJECT HERE ***\E/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100881 die sprintf(__("Refusing to send because the patch\n\t%s\n"
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200882 . "has the template subject '*** SUBJECT HERE ***'. "
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100883 . "Pass --force if you really want to send.\n"), $f);
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200884 }
885 }
886}
887
Remi Lespinetc46e27a2015-06-30 14:16:47 +0200888if (defined $sender) {
Remi Lespinetfa5b1aa2015-06-30 14:16:51 +0200889 $sender =~ s/^\s+|\s+$//g;
Remi Lespinetc46e27a2015-06-30 14:16:47 +0200890 ($sender) = expand_aliases($sender);
891} else {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100892 $sender = $repoauthor || $repocommitter || '';
Ryan Anderson83b24432005-07-31 04:17:25 -0400893}
894
Michael S. Tsirkinda187592013-06-05 21:11:00 +0300895# $sender could be an already sanitized address
896# (e.g. sendemail.from could be manually sanitized by user).
897# But it's a no-op to run sanitize_address on an already sanitized address.
898$sender = sanitize_address($sender);
899
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100900my $to_whom = __("To whom should the emails be sent (if anyone)?");
Felipe Contreras8cac13d2012-11-24 12:16:19 +0100901my $prompting = 0;
Junio C Hamano8796ff72010-10-26 22:02:03 -0700902if (!@initial_to && !defined $to_cmd) {
Junio C Hamano0d6b21e2016-04-24 12:31:44 -0700903 my $to = ask("$to_whom ",
Stephen Boyd61837492012-09-06 11:31:11 -0700904 default => "",
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700905 valid_re => qr/\@.*\./, confirm_only => 1);
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700906 push @initial_to, parse_address_line($to) if defined $to; # sanitized/validated later
Ryan Anderson1f038a02005-09-05 01:13:07 -0400907 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -0400908}
909
Eric Wong994d6c62006-05-14 19:13:44 -0700910sub expand_aliases {
Jeff King302e04e2009-07-23 07:09:29 -0400911 return map { expand_one_alias($_) } @_;
912}
913
914my %EXPANDED_ALIASES;
915sub expand_one_alias {
916 my $alias = shift;
917 if ($EXPANDED_ALIASES{$alias}) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100918 die sprintf(__("fatal: alias '%s' expands to itself\n"), $alias);
Jeff King302e04e2009-07-23 07:09:29 -0400919 }
920 local $EXPANDED_ALIASES{$alias} = 1;
921 return $aliases{$alias} ? expand_aliases(@{$aliases{$alias}}) : $alias;
Eric Wong994d6c62006-05-14 19:13:44 -0700922}
923
Remi Lespinetb5e112d2015-06-30 14:16:45 +0200924@initial_to = process_address_list(@initial_to);
925@initial_cc = process_address_list(@initial_cc);
926@bcclist = process_address_list(@bcclist);
Eric Wong994d6c62006-05-14 19:13:44 -0700927
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100928if ($thread && !defined $initial_in_reply_to && $prompting) {
929 $initial_in_reply_to = ask(
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100930 __("Message-ID to be used as In-Reply-To for the first email (if any)? "),
Stephen Boyd61837492012-09-06 11:31:11 -0700931 default => "",
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700932 valid_re => qr/\@.*\./, confirm_only => 1);
Ryan Anderson83b24432005-07-31 04:17:25 -0400933}
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100934if (defined $initial_in_reply_to) {
935 $initial_in_reply_to =~ s/^\s*<?//;
936 $initial_in_reply_to =~ s/>?\s*$//;
937 $initial_in_reply_to = "<$initial_in_reply_to>" if $initial_in_reply_to ne '';
Junio C Hamanoace9c2a2007-12-10 21:44:42 -0800938}
Mike Hommeyace72082007-12-09 18:17:28 +0100939
Christian Ludwigd11c9432018-03-04 00:58:14 +0100940if (defined $reply_to) {
941 $reply_to =~ s/^\s+|\s+$//g;
942 ($reply_to) = expand_aliases($reply_to);
943 $reply_to = sanitize_address($reply_to);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900944}
Eric Wongaca7ad72006-05-15 02:34:44 -0700945
946if (!defined $smtp_server) {
Florian Klink1ab2fd42017-11-28 01:49:04 +0100947 my @sendmail_paths = qw( /usr/sbin/sendmail /usr/lib/sendmail );
948 push @sendmail_paths, map {"$_/sendmail"} split /:/, $ENV{PATH};
949 foreach (@sendmail_paths) {
Eric Wongaca7ad72006-05-15 02:34:44 -0700950 if (-x $_) {
951 $smtp_server = $_;
952 last;
953 }
954 }
955 $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
Ryan Anderson3342d852005-07-31 20:04:24 -0400956}
957
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500958if ($compose && $compose > 0) {
959 @files = ($compose_filename . ".final", @files);
Ryan Anderson1f038a02005-09-05 01:13:07 -0400960}
961
Ryan Anderson83b24432005-07-31 04:17:25 -0400962# Variables we set as part of the loop over files
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100963our ($message_id, %mail, $subject, $in_reply_to, $references, $message,
Jay Soffiandc1460a2009-03-31 12:22:12 -0400964 $needs_confirm, $message_num, $ask_default);
Ryan Anderson83b24432005-07-31 04:17:25 -0400965
Eric Wong567ffeb2006-03-25 16:47:12 -0800966sub extract_valid_address {
967 my $address = shift;
Ævar Arnfjörð Bjarmason35b6ab92010-09-30 19:03:31 +0000968 my $local_part_regexp = qr/[^<>"\s@]+/;
969 my $domain_regexp = qr/[^.<>"\s@]+(?:\.[^.<>"\s@]+)+/;
Eric Wongdb3106b2006-05-15 02:41:01 -0700970
971 # check for a local address:
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700972 return $address if ($address =~ /^($local_part_regexp)$/);
Eric Wongdb3106b2006-05-15 02:41:01 -0700973
Uwe Kleine-König155197e2007-08-09 15:27:57 +0200974 $address =~ s/^\s*<(.*)>\s*$/$1/;
Eric Wong567ffeb2006-03-25 16:47:12 -0800975 if ($have_email_valid) {
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700976 return scalar Email::Valid->address($address);
Eric Wong567ffeb2006-03-25 16:47:12 -0800977 }
Krzysztof Mazur95c0d4b2012-11-22 19:12:09 +0100978
979 # less robust/correct than the monster regexp in Email::Valid,
980 # but still does a 99% job, and one less dependency
981 return $1 if $address =~ /($local_part_regexp\@$domain_regexp)/;
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -0700982 return;
Eric Wong567ffeb2006-03-25 16:47:12 -0800983}
Ryan Anderson83b24432005-07-31 04:17:25 -0400984
Krzysztof Mazure4312252012-11-22 19:12:10 +0100985sub extract_valid_address_or_die {
986 my $address = shift;
987 $address = extract_valid_address($address);
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100988 die sprintf(__("error: unable to extract a valid address from: %s\n"), $address)
Krzysztof Mazure4312252012-11-22 19:12:10 +0100989 if !$address;
990 return $address;
991}
992
993sub validate_address {
994 my $address = shift;
Krzysztof Mazurd0e98102012-11-22 19:12:12 +0100995 while (!extract_valid_address($address)) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100996 printf STDERR __("error: unable to extract a valid address from: %s\n"), $address;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100997 # TRANSLATORS: Make sure to include [q] [d] [e] in your
998 # translation. The program will only accept English input
999 # at this point.
1000 $_ = ask(__("What to do with this address? ([q]uit|[d]rop|[e]dit): "),
Krzysztof Mazurd0e98102012-11-22 19:12:12 +01001001 valid_re => qr/^(?:quit|q|drop|d|edit|e)/i,
Krzysztof Mazur5c80afe2012-11-22 19:12:11 +01001002 default => 'q');
1003 if (/^d/i) {
1004 return undef;
1005 } elsif (/^q/i) {
1006 cleanup_compose_files();
1007 exit(0);
1008 }
Junio C Hamano0d6b21e2016-04-24 12:31:44 -07001009 $address = ask("$to_whom ",
Krzysztof Mazurd0e98102012-11-22 19:12:12 +01001010 default => "",
1011 valid_re => qr/\@.*\./, confirm_only => 1);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001012 }
1013 return $address;
1014}
1015
1016sub validate_address_list {
1017 return (grep { defined $_ }
1018 map { validate_address($_) } @_);
Ryan Anderson83b24432005-07-31 04:17:25 -04001019}
1020
1021# Usually don't need to change anything below here.
1022
1023# we make a "fake" message id by taking the current number
1024# of seconds since the beginning of Unix time and tacking on
1025# a random number to the end, in case we are called quicker than
1026# 1 second since the last time we were called.
Ryan Anderson8037d1a2005-07-31 20:04:24 -04001027
1028# We'll setup a template for the message id, using the "from" address:
Ryan Anderson8037d1a2005-07-31 20:04:24 -04001029
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001030my ($message_id_stamp, $message_id_serial);
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001031sub make_message_id {
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001032 my $uniq;
1033 if (!defined $message_id_stamp) {
Eric Wongf916ab02016-04-06 20:07:14 +00001034 $message_id_stamp = strftime("%Y%m%d%H%M%S.$$", gmtime(time));
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001035 $message_id_serial = 0;
1036 }
1037 $message_id_serial++;
1038 $uniq = "$message_id_stamp-$message_id_serial";
1039
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001040 my $du_part;
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001041 for ($sender, $repocommitter, $repoauthor) {
1042 $du_part = extract_valid_address(sanitize_address($_));
1043 last if (defined $du_part and $du_part ne '');
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001044 }
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001045 if (not defined $du_part or $du_part eq '') {
Ævar Arnfjörð Bjarmason529dd382010-09-30 13:43:08 +00001046 require Sys::Hostname;
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001047 $du_part = 'user@' . Sys::Hostname::hostname();
1048 }
Eric Wongf916ab02016-04-06 20:07:14 +00001049 my $message_id_template = "<%s-%s>";
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001050 $message_id = sprintf($message_id_template, $uniq, $du_part);
Ryan Anderson8037d1a2005-07-31 20:04:24 -04001051 #print "new message id = $message_id\n"; # Was useful for debugging
Ryan Anderson83b24432005-07-31 04:17:25 -04001052}
1053
1054
1055
Eric Wonga5370b12006-03-25 03:01:01 -08001056$time = time - scalar $#files;
Ryan Anderson83b24432005-07-31 04:17:25 -04001057
Jürgen Rühle374c5902007-01-10 13:36:39 -08001058sub unquote_rfc2047 {
1059 local ($_) = @_;
Роман Донченко11f70a72014-12-14 18:59:46 +03001060 my $charset;
Роман Донченкоab47e2a2014-12-14 18:59:47 +03001061 my $sep = qr/[ \t]+/;
1062 s{$re_encoded_word(?:$sep$re_encoded_word)*}{
1063 my @words = split $sep, $&;
1064 foreach (@words) {
1065 m/$re_encoded_word/;
1066 $charset = $1;
1067 my $encoding = $2;
1068 my $text = $3;
1069 if ($encoding eq 'q' || $encoding eq 'Q') {
1070 $_ = $text;
1071 s/_/ /g;
1072 s/=([0-9A-F]{2})/chr(hex($1))/egi;
1073 } else {
1074 # other encodings not supported yet
1075 }
Роман Донченко11f70a72014-12-14 18:59:46 +03001076 }
Роман Донченкоab47e2a2014-12-14 18:59:47 +03001077 join '', @words;
Thomas Rastb622d4d2012-07-30 21:25:40 +02001078 }eg;
Роман Донченко11f70a72014-12-14 18:59:46 +03001079 return wantarray ? ($_, $charset) : $_;
Jürgen Rühle374c5902007-01-10 13:36:39 -08001080}
1081
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001082sub quote_rfc2047 {
1083 local $_ = shift;
Brandon Caseyd1fff6f2009-06-06 20:12:31 -05001084 my $encoding = shift || 'UTF-8';
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001085 s/([^-a-zA-Z0-9!*+\/])/sprintf("=%02X", ord($1))/eg;
1086 s/(.*)/=\?$encoding\?q\?$1\?=/;
1087 return $_;
1088}
1089
Brandon Caseya3a82622009-06-07 19:25:58 -05001090sub is_rfc2047_quoted {
1091 my $s = shift;
Brandon Caseya3a82622009-06-07 19:25:58 -05001092 length($s) <= 75 &&
Роман Донченко11f70a72014-12-14 18:59:46 +03001093 $s =~ m/^(?:"[[:ascii:]]*"|$re_encoded_word)$/o;
Brandon Caseya3a82622009-06-07 19:25:58 -05001094}
1095
Krzysztof Mazurce547802012-10-24 23:08:26 +02001096sub subject_needs_rfc2047_quoting {
1097 my $s = shift;
1098
Krzysztof Mazurce1459f2012-10-24 23:28:29 +02001099 return ($s =~ /[^[:ascii:]]/) || ($s =~ /=\?/);
Krzysztof Mazurce547802012-10-24 23:08:26 +02001100}
1101
1102sub quote_subject {
1103 local $subject = shift;
1104 my $encoding = shift || 'UTF-8';
1105
1106 if (subject_needs_rfc2047_quoting($subject)) {
1107 return quote_rfc2047($subject, $encoding);
1108 }
1109 return $subject;
1110}
1111
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001112# use the simplest quoting being able to handle the recipient
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001113sub sanitize_address {
Robin H. Johnson732263d2007-04-25 19:37:19 -07001114 my ($recipient) = @_;
Krzysztof Mazur831a4882012-11-22 19:12:08 +01001115
1116 # remove garbage after email address
1117 $recipient =~ s/(.*>).*$/$1/;
1118
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001119 my ($recipient_name, $recipient_addr) = ($recipient =~ /^(.*?)\s*(<.*)/);
1120
1121 if (not $recipient_name) {
Ævar Arnfjörð Bjarmasonff483892010-09-30 13:43:02 +00001122 return $recipient;
Robin H. Johnson732263d2007-04-25 19:37:19 -07001123 }
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001124
1125 # if recipient_name is already quoted, do nothing
Brandon Caseya3a82622009-06-07 19:25:58 -05001126 if (is_rfc2047_quoted($recipient_name)) {
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001127 return $recipient;
1128 }
1129
Remi Lespinet1fe97032015-06-30 14:16:49 +02001130 # remove non-escaped quotes
1131 $recipient_name =~ s/(^|[^\\])"/$1/g;
1132
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001133 # rfc2047 is needed if a non-ascii char is included
1134 if ($recipient_name =~ /[^[:ascii:]]/) {
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001135 $recipient_name = quote_rfc2047($recipient_name);
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001136 }
1137
1138 # double quotes are needed if specials or CTLs are included
1139 elsif ($recipient_name =~ /[][()<>@,;:\\".\000-\037\177]/) {
Remi Lespinet1fe97032015-06-30 14:16:49 +02001140 $recipient_name =~ s/([\\\r])/\\$1/g;
Ævar Arnfjörð Bjarmasond5c7d692010-09-30 13:43:03 +00001141 $recipient_name = qq["$recipient_name"];
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001142 }
1143
1144 return "$recipient_name $recipient_addr";
1145
Robin H. Johnson732263d2007-04-25 19:37:19 -07001146}
1147
Matthieu Moycb2922f2017-08-23 12:21:01 +02001148sub strip_garbage_one_address {
1149 my ($addr) = @_;
1150 chomp $addr;
1151 if ($addr =~ /^(("[^"]*"|[^"<]*)? *<[^>]*>).*/) {
1152 # "Foo Bar" <foobar@example.com> [possibly garbage here]
1153 # Foo Bar <foobar@example.com> [possibly garbage here]
1154 return $1;
1155 }
1156 if ($addr =~ /^(<[^>]*>).*/) {
1157 # <foo@example.com> [possibly garbage here]
1158 # if garbage contains other addresses, they are ignored.
1159 return $1;
1160 }
1161 if ($addr =~ /^([^"#,\s]*)/) {
1162 # address without quoting: remove anything after the address
1163 return $1;
1164 }
1165 return $addr;
1166}
1167
Krzysztof Mazure4312252012-11-22 19:12:10 +01001168sub sanitize_address_list {
1169 return (map { sanitize_address($_) } @_);
1170}
1171
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001172sub process_address_list {
Remi Lespinetb1c8a112015-06-30 14:16:50 +02001173 my @addr_list = map { parse_address_line($_) } @_;
1174 @addr_list = expand_aliases(@addr_list);
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001175 @addr_list = sanitize_address_list(@addr_list);
1176 @addr_list = validate_address_list(@addr_list);
1177 return @addr_list;
1178}
1179
Jari Aalto134550f2010-03-14 17:16:45 +02001180# Returns the local Fully Qualified Domain Name (FQDN) if available.
1181#
1182# Tightly configured MTAa require that a caller sends a real DNS
1183# domain name that corresponds the IP address in the HELO/EHLO
1184# handshake. This is used to verify the connection and prevent
1185# spammers from trying to hide their identity. If the DNS and IP don't
1186# match, the receiveing MTA may deny the connection.
1187#
1188# Here is a deny example of Net::SMTP with the default "localhost.localdomain"
1189#
1190# Net::SMTP=GLOB(0x267ec28)>>> EHLO localhost.localdomain
1191# Net::SMTP=GLOB(0x267ec28)<<< 550 EHLO argument does not match calling host
1192#
1193# This maildomain*() code is based on ideas in Perl library Test::Reporter
1194# /usr/share/perl5/Test/Reporter/Mail/Util.pm ==> sub _maildomain ()
1195
Brian Gernhardt59a86302010-04-10 10:53:54 -04001196sub valid_fqdn {
1197 my $domain = shift;
Brandon Casey61ef5e92010-09-26 22:18:01 -05001198 return defined $domain && !($^O eq 'darwin' && $domain =~ /\.local$/) && $domain =~ /\./;
Brian Gernhardt59a86302010-04-10 10:53:54 -04001199}
1200
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001201sub maildomain_net {
Jari Aalto134550f2010-03-14 17:16:45 +02001202 my $maildomain;
1203
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001204 my $domain = Net::Domain::domainname();
1205 $maildomain = $domain if valid_fqdn($domain);
Jari Aalto134550f2010-03-14 17:16:45 +02001206
1207 return $maildomain;
1208}
1209
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001210sub maildomain_mta {
Jari Aalto134550f2010-03-14 17:16:45 +02001211 my $maildomain;
1212
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001213 for my $host (qw(mailhost localhost)) {
1214 my $smtp = Net::SMTP->new($host);
1215 if (defined $smtp) {
1216 my $domain = $smtp->domain;
1217 $smtp->quit;
Jari Aalto134550f2010-03-14 17:16:45 +02001218
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001219 $maildomain = $domain if valid_fqdn($domain);
Jari Aalto134550f2010-03-14 17:16:45 +02001220
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001221 last if $maildomain;
Jari Aalto134550f2010-03-14 17:16:45 +02001222 }
1223 }
1224
1225 return $maildomain;
1226}
1227
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001228sub maildomain {
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001229 return maildomain_net() || maildomain_mta() || 'localhost.localdomain';
Jari Aalto134550f2010-03-14 17:16:45 +02001230}
1231
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001232sub smtp_host_string {
1233 if (defined $smtp_server_port) {
1234 return "$smtp_server:$smtp_server_port";
1235 } else {
1236 return $smtp_server;
1237 }
1238}
1239
1240# Returns 1 if authentication succeeded or was not necessary
1241# (smtp_user was not specified), and 0 otherwise.
1242
1243sub smtp_auth_maybe {
1244 if (!defined $smtp_authuser || $auth) {
1245 return 1;
1246 }
1247
1248 # Workaround AUTH PLAIN/LOGIN interaction defect
1249 # with Authen::SASL::Cyrus
1250 eval {
1251 require Authen::SASL;
1252 Authen::SASL->import(qw(Perl));
1253 };
1254
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001255 # Check mechanism naming as defined in:
1256 # https://tools.ietf.org/html/rfc4422#page-8
Brian Norris904f6e72015-09-18 15:12:50 -07001257 if ($smtp_auth && $smtp_auth !~ /^(\b[A-Z0-9-_]{1,20}\s*)*$/) {
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001258 die "invalid smtp auth: '${smtp_auth}'";
1259 }
1260
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001261 # TODO: Authentication may fail not because credentials were
1262 # invalid but due to other reasons, in which we should not
1263 # reject credentials.
1264 $auth = Git::credential({
1265 'protocol' => 'smtp',
1266 'host' => smtp_host_string(),
1267 'username' => $smtp_authuser,
1268 # if there's no password, "git credential fill" will
1269 # give us one, otherwise it'll just pass this one.
1270 'password' => $smtp_authpass
1271 }, sub {
1272 my $cred = shift;
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001273
1274 if ($smtp_auth) {
1275 my $sasl = Authen::SASL->new(
1276 mechanism => $smtp_auth,
1277 callback => {
1278 user => $cred->{'username'},
1279 pass => $cred->{'password'},
1280 authname => $cred->{'username'},
1281 }
1282 );
1283
1284 return !!$smtp->auth($sasl);
1285 }
1286
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001287 return !!$smtp->auth($cred->{'username'}, $cred->{'password'});
1288 });
1289
1290 return $auth;
1291}
1292
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001293sub ssl_verify_params {
1294 eval {
1295 require IO::Socket::SSL;
1296 IO::Socket::SSL->import(qw/SSL_VERIFY_PEER SSL_VERIFY_NONE/);
1297 };
1298 if ($@) {
1299 print STDERR "Not using SSL_VERIFY_PEER due to out-of-date IO::Socket::SSL.\n";
1300 return;
1301 }
1302
1303 if (!defined $smtp_ssl_cert_path) {
Ruben Kerkhof01645b72014-01-15 21:31:11 +04001304 # use the OpenSSL defaults
1305 return (SSL_verify_mode => SSL_VERIFY_PEER());
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001306 }
1307
1308 if ($smtp_ssl_cert_path eq "") {
1309 return (SSL_verify_mode => SSL_VERIFY_NONE());
1310 } elsif (-d $smtp_ssl_cert_path) {
1311 return (SSL_verify_mode => SSL_VERIFY_PEER(),
1312 SSL_ca_path => $smtp_ssl_cert_path);
1313 } elsif (-f $smtp_ssl_cert_path) {
1314 return (SSL_verify_mode => SSL_VERIFY_PEER(),
1315 SSL_ca_file => $smtp_ssl_cert_path);
1316 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001317 die sprintf(__("CA path \"%s\" does not exist"), $smtp_ssl_cert_path);
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001318 }
1319}
1320
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001321sub file_name_is_absolute {
1322 my ($path) = @_;
1323
1324 # msys does not grok DOS drive-prefixes
1325 if ($^O eq 'msys') {
Junio C Hamanof24ecf52014-04-23 09:37:38 -07001326 return ($path =~ m#^/# || $path =~ m#^[a-zA-Z]\:#)
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001327 }
1328
1329 require File::Spec::Functions;
1330 return File::Spec::Functions::file_name_is_absolute($path);
1331}
1332
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001333# Prepares the email, then asks the user what to do.
1334#
1335# If the user chooses to send the email, it's sent and 1 is returned.
1336# If the user chooses not to send the email, 0 is returned.
1337# If the user decides they want to make further edits, -1 is returned and the
1338# caller is expected to call send_message again after the edits are performed.
1339#
1340# If an error occurs sending the email, this just dies.
Michael Witten15da1082009-04-13 13:23:51 -05001341
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001342sub send_message {
Eric Wong4bc87a22006-03-25 17:20:48 -08001343 my @recipients = unique_email_list(@to);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001344 @cc = (grep { my $cc = extract_valid_address_or_die($_);
Joe Perches83acaae2010-11-20 15:06:05 -08001345 not grep { $cc eq $_ || $_ =~ /<\Q${cc}\E>$/ } @recipients
Ask Bjørn Hansen7ac17522007-11-19 03:00:26 -08001346 }
Ask Bjørn Hansen7ac17522007-11-19 03:00:26 -08001347 @cc);
Eric Wong4bc87a22006-03-25 17:20:48 -08001348 my $to = join (",\n\t", @recipients);
Ryan Anderson58063242006-05-29 12:30:13 -07001349 @recipients = unique_email_list(@recipients,@cc,@bcclist);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001350 @recipients = (map { extract_valid_address_or_die($_) } @recipients);
Jakub Narebski6bdca892006-07-07 20:57:55 +02001351 my $date = format_2822_time($time++);
Martin Langhoffe923eff2006-05-03 09:44:36 +12001352 my $gitversion = '@@GIT_VERSION@@';
1353 if ($gitversion =~ m/..GIT_VERSION../) {
Petr Baudis3cb8caf2006-07-03 22:47:58 +02001354 $gitversion = Git::version();
Martin Langhoffe923eff2006-05-03 09:44:36 +12001355 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001356
Joe Perches02461e02009-10-08 10:03:26 -07001357 my $cc = join(",\n\t", unique_email_list(@cc));
Junio C Hamanof06a6a42007-04-16 16:51:47 -07001358 my $ccline = "";
1359 if ($cc ne '') {
1360 $ccline = "\nCc: $cc";
1361 }
Jeff King4f3d3702007-12-17 15:51:34 -05001362 make_message_id() unless defined($message_id);
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001363
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001364 my $header = "From: $sender
Junio C Hamanof06a6a42007-04-16 16:51:47 -07001365To: $to${ccline}
Eric Wong4bc87a22006-03-25 17:20:48 -08001366Subject: $subject
Eric Wong4bc87a22006-03-25 17:20:48 -08001367Date: $date
1368Message-Id: $message_id
Eric Wong4bc87a22006-03-25 17:20:48 -08001369";
Luis Henriquesac1596a2014-03-24 21:38:27 +00001370 if ($use_xmailer) {
1371 $header .= "X-Mailer: git-send-email $gitversion\n";
1372 }
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001373 if ($in_reply_to) {
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001374
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001375 $header .= "In-Reply-To: $in_reply_to\n";
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001376 $header .= "References: $references\n";
1377 }
Christian Ludwigd11c9432018-03-04 00:58:14 +01001378 if ($reply_to) {
1379 $header .= "Reply-To: $reply_to\n";
1380 }
Junio C Hamanoce91c2f2006-10-05 16:36:49 -07001381 if (@xh) {
1382 $header .= join("\n", @xh) . "\n";
1383 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001384
Robin H. Johnsonc38f0242007-04-25 19:37:20 -07001385 my @sendmail_parameters = ('-i', @recipients);
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001386 my $raw_from = $sender;
Felipe Contrerasc89e3242009-11-26 21:04:29 +02001387 if (defined $envelope_sender && $envelope_sender ne "auto") {
1388 $raw_from = $envelope_sender;
1389 }
Robin H. Johnsonf073a592007-04-25 19:37:22 -07001390 $raw_from = extract_valid_address($raw_from);
1391 unshift (@sendmail_parameters,
1392 '-f', $raw_from) if(defined $envelope_sender);
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001393
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001394 if ($needs_confirm && !$dry_run) {
1395 print "\n$header\n";
1396 if ($needs_confirm eq "inform") {
1397 $confirm_unconfigured = 0; # squelch this message for the rest of this run
Jay Soffian6e182512009-03-28 21:39:10 -04001398 $ask_default = "y"; # assume yes on EOF since user hasn't explicitly asked for confirmation
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001399 print __ <<EOF ;
1400 The Cc list above has been expanded by additional
1401 addresses found in the patch commit message. By default
1402 send-email prompts before sending whenever this occurs.
1403 This behavior is controlled by the sendemail.confirm
1404 configuration setting.
1405
1406 For additional information, run 'git send-email --help'.
1407 To retain the current behavior, but squelch this message,
1408 run 'git config --global sendemail.confirm auto'.
1409
1410EOF
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001411 }
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001412 # TRANSLATORS: Make sure to include [y] [n] [e] [q] [a] in your
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001413 # translation. The program will only accept English input
1414 # at this point.
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001415 $_ = ask(__("Send this email? ([y]es|[n]o|[e]dit|[q]uit|[a]ll): "),
1416 valid_re => qr/^(?:yes|y|no|n|edit|e|quit|q|all|a)/i,
Jay Soffian6e182512009-03-28 21:39:10 -04001417 default => $ask_default);
Vasco Almeida46493102016-12-14 11:54:36 -01001418 die __("Send this email reply required") unless defined $_;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001419 if (/^n/i) {
Michael Witten15da1082009-04-13 13:23:51 -05001420 return 0;
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001421 } elsif (/^e/i) {
1422 return -1;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001423 } elsif (/^q/i) {
1424 cleanup_compose_files();
1425 exit(0);
1426 } elsif (/^a/i) {
1427 $confirm = 'never';
1428 }
1429 }
1430
Pascal Obry052fbea2010-09-06 20:12:11 +02001431 unshift (@sendmail_parameters, @smtp_server_options);
1432
Matthew Wilcox61302592006-10-10 08:58:23 -06001433 if ($dry_run) {
1434 # We don't want to send the email.
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001435 } elsif (file_name_is_absolute($smtp_server)) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001436 my $pid = open my $sm, '|-';
1437 defined $pid or die $!;
1438 if (!$pid) {
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001439 exec($smtp_server, @sendmail_parameters) or die $!;
Eric Wongaca7ad72006-05-15 02:34:44 -07001440 }
1441 print $sm "$header\n$message";
Ævar Arnfjörð Bjarmason5e2c2ab2010-09-30 13:43:07 +00001442 close $sm or die $!;
Eric Wongaca7ad72006-05-15 02:34:44 -07001443 } else {
Junio C Hamano44b24762007-09-25 17:27:54 -07001444
1445 if (!defined $smtp_server) {
Vasco Almeida46493102016-12-14 11:54:36 -01001446 die __("The required SMTP server is not properly defined.")
Junio C Hamano44b24762007-09-25 17:27:54 -07001447 }
1448
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001449 require Net::SMTP;
Jonathan Niederbfbfc9a2017-05-31 17:17:43 -07001450 my $use_net_smtp_ssl = version->parse($Net::SMTP::VERSION) < version->parse("2.34");
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001451 $smtp_domain ||= maildomain();
1452
Thomas Rastf6bebd12008-06-25 21:42:43 +02001453 if ($smtp_encryption eq 'ssl') {
Junio C Hamano44b24762007-09-25 17:27:54 -07001454 $smtp_server_port ||= 465; # ssmtp
Thomas Rast5508f3e2013-12-01 23:48:43 +01001455 require IO::Socket::SSL;
John Keeping9d605242015-12-03 21:47:18 +00001456
1457 # Suppress "variable accessed once" warning.
1458 {
1459 no warnings 'once';
1460 $IO::Socket::SSL::DEBUG = 1;
1461 }
1462
Thomas Rast5508f3e2013-12-01 23:48:43 +01001463 # Net::SMTP::SSL->new() does not forward any SSL options
1464 IO::Socket::SSL::set_client_defaults(
1465 ssl_verify_params());
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001466
1467 if ($use_net_smtp_ssl) {
1468 require Net::SMTP::SSL;
1469 $smtp ||= Net::SMTP::SSL->new($smtp_server,
1470 Hello => $smtp_domain,
1471 Port => $smtp_server_port,
1472 Debug => $debug_net_smtp);
1473 }
1474 else {
1475 $smtp ||= Net::SMTP->new($smtp_server,
1476 Hello => $smtp_domain,
1477 Port => $smtp_server_port,
1478 Debug => $debug_net_smtp,
1479 SSL => 1);
1480 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001481 }
1482 else {
brian m. carlson1a741bf2013-07-04 22:04:52 +00001483 $smtp_server_port ||= 25;
1484 $smtp ||= Net::SMTP->new($smtp_server,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001485 Hello => $smtp_domain,
brian m. carlson1a741bf2013-07-04 22:04:52 +00001486 Debug => $debug_net_smtp,
1487 Port => $smtp_server_port);
Yakov Lernerfb3650e2009-09-25 15:10:21 -07001488 if ($smtp_encryption eq 'tls' && $smtp) {
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001489 if ($use_net_smtp_ssl) {
1490 $smtp->command('STARTTLS');
1491 $smtp->response();
1492 if ($smtp->code != 220) {
1493 die sprintf(__("Server does not support STARTTLS! %s"), $smtp->message);
1494 }
1495 require Net::SMTP::SSL;
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001496 $smtp = Net::SMTP::SSL->start_SSL($smtp,
1497 ssl_verify_params())
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001498 or die sprintf(__("STARTTLS failed! %s"), IO::Socket::SSL::errstr());
Thomas Rastf6bebd12008-06-25 21:42:43 +02001499 }
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001500 else {
1501 $smtp->starttls(ssl_verify_params())
1502 or die sprintf(__("STARTTLS failed! %s"), IO::Socket::SSL::errstr());
1503 }
1504 $smtp_encryption = '';
1505 # Send EHLO again to receive fresh
1506 # supported commands
1507 $smtp->hello($smtp_domain);
Thomas Rastf6bebd12008-06-25 21:42:43 +02001508 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001509 }
Junio C Hamano44b24762007-09-25 17:27:54 -07001510
1511 if (!$smtp) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001512 die __("Unable to initialize SMTP properly. Check config and use --smtp-debug."),
1513 " VALUES: server=$smtp_server ",
Jari Aaltoe5afb3a2010-03-14 17:15:33 +02001514 "encryption=$smtp_encryption ",
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001515 "hello=$smtp_domain",
Sylvain Rabota1dd7e12011-04-29 20:23:24 +02001516 defined $smtp_server_port ? " port=$smtp_server_port" : "";
Junio C Hamano44b24762007-09-25 17:27:54 -07001517 }
1518
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001519 smtp_auth_maybe or die $smtp->message;
Michael Witten2363d742008-02-03 19:53:56 -05001520
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001521 $smtp->mail( $raw_from ) or die $smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001522 $smtp->to( @recipients ) or die $smtp->message;
1523 $smtp->data or die $smtp->message;
Stefan Agnerf60c4832015-09-30 09:26:09 +02001524 $smtp->datasend("$header\n") or die $smtp->message;
1525 my @lines = split /^/, $message;
1526 foreach my $line (@lines) {
1527 $smtp->datasend("$line") or die $smtp->message;
1528 }
Eric Wongaca7ad72006-05-15 02:34:44 -07001529 $smtp->dataend() or die $smtp->message;
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001530 $smtp->code =~ /250|200/ or die sprintf(__("Failed to send %s\n"), $subject).$smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001531 }
Ryan Anderson27184352006-02-05 20:13:52 -05001532 if ($quiet) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001533 printf($dry_run ? __("Dry-Sent %s\n") : __("Sent %s\n"), $subject);
Ryan Anderson27184352006-02-05 20:13:52 -05001534 } else {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001535 print($dry_run ? __("Dry-OK. Log says:\n") : __("OK. Log says:\n"));
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001536 if (!file_name_is_absolute($smtp_server)) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001537 print "Server: $smtp_server\n";
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001538 print "MAIL FROM:<$raw_from>\n";
Joe Perches02461e02009-10-08 10:03:26 -07001539 foreach my $entry (@recipients) {
1540 print "RCPT TO:<$entry>\n";
1541 }
Eric Wongaca7ad72006-05-15 02:34:44 -07001542 } else {
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001543 print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001544 }
David D. Kilzerb7f30e02007-11-18 20:14:55 -08001545 print $header, "\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001546 if ($smtp) {
Vasco Almeida46493102016-12-14 11:54:36 -01001547 print __("Result: "), $smtp->code, ' ',
Eric Wongaca7ad72006-05-15 02:34:44 -07001548 ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
1549 } else {
Vasco Almeida46493102016-12-14 11:54:36 -01001550 print __("Result: OK\n");
Eric Wongaca7ad72006-05-15 02:34:44 -07001551 }
Ryan Anderson30d08b32006-02-02 11:56:06 -05001552 }
Michael Witten15da1082009-04-13 13:23:51 -05001553
1554 return 1;
Ryan Anderson83b24432005-07-31 04:17:25 -04001555}
1556
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001557$in_reply_to = $initial_in_reply_to;
1558$references = $initial_in_reply_to || '';
Ryan Anderson83b24432005-07-31 04:17:25 -04001559$subject = $initial_subject;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001560$message_num = 0;
Ryan Anderson83b24432005-07-31 04:17:25 -04001561
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001562# Prepares the email, prompts the user, sends it out
1563# Returns 0 if an edit was done and the function should be called again, or 1
1564# otherwise.
1565sub process_file {
1566 my ($t) = @_;
1567
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001568 open my $fh, "<", $t or die sprintf(__("can't open file %s"), $t);
Ryan Anderson83b24432005-07-31 04:17:25 -04001569
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001570 my $author = undef;
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001571 my $sauthor = undef;
Jeff King8291db62007-11-16 05:49:09 -05001572 my $author_encoding;
1573 my $has_content_type;
1574 my $body_encoding;
Paolo Bonzinibb294562014-11-25 15:00:26 +01001575 my $xfer_encoding;
1576 my $has_mime_version;
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001577 @to = ();
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001578 @cc = ();
Junio C Hamanoce91c2f2006-10-05 16:36:49 -07001579 @xh = ();
Junio C Hamanoe6b09642006-10-07 03:09:05 -07001580 my $input_format = undef;
Jay Soffian50126992009-02-14 23:32:14 -05001581 my @header = ();
Ryan Anderson83b24432005-07-31 04:17:25 -04001582 $message = "";
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001583 $message_num++;
Jay Soffian50126992009-02-14 23:32:14 -05001584 # First unfold multiline header fields
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001585 while(<$fh>) {
Jay Soffian50126992009-02-14 23:32:14 -05001586 last if /^\s*$/;
1587 if (/^\s+\S/ and @header) {
1588 chomp($header[$#header]);
1589 s/^\s+/ /;
1590 $header[$#header] .= $_;
1591 } else {
1592 push(@header, $_);
1593 }
1594 }
1595 # Now parse the header
1596 foreach(@header) {
1597 if (/^From /) {
1598 $input_format = 'mbox';
1599 next;
1600 }
1601 chomp;
1602 if (!defined $input_format && /^[-A-Za-z]+:\s/) {
1603 $input_format = 'mbox';
1604 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001605
Jay Soffian50126992009-02-14 23:32:14 -05001606 if (defined $input_format && $input_format eq 'mbox') {
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001607 if (/^Subject:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001608 $subject = $1;
1609 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001610 elsif (/^From:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001611 ($author, $author_encoding) = unquote_rfc2047($1);
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001612 $sauthor = sanitize_address($author);
Jay Soffian50126992009-02-14 23:32:14 -05001613 next if $suppress_cc{'author'};
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001614 next if $suppress_cc{'self'} and $sauthor eq $sender;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001615 printf(__("(mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001616 $1, $_) unless $quiet;
1617 push @cc, $1;
1618 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001619 elsif (/^To:\s+(.*)$/i) {
Stephen Boyd21802cd2010-09-29 00:26:44 -07001620 foreach my $addr (parse_address_line($1)) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001621 printf(__("(mbox) Adding to: %s from line '%s'\n"),
Stephen Boyd21802cd2010-09-29 00:26:44 -07001622 $addr, $_) unless $quiet;
Krzysztof Mazure4312252012-11-22 19:12:10 +01001623 push @to, $addr;
Stephen Boyd21802cd2010-09-29 00:26:44 -07001624 }
1625 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001626 elsif (/^Cc:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001627 foreach my $addr (parse_address_line($1)) {
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001628 my $qaddr = unquote_rfc2047($addr);
1629 my $saddr = sanitize_address($qaddr);
1630 if ($saddr eq $sender) {
David Brown65648282007-12-25 19:56:29 -08001631 next if ($suppress_cc{'self'});
David Brown65648282007-12-25 19:56:29 -08001632 } else {
1633 next if ($suppress_cc{'cc'});
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001634 }
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001635 printf(__("(mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001636 $addr, $_) unless $quiet;
1637 push @cc, $addr;
Ryan Anderson83b24432005-07-31 04:17:25 -04001638 }
1639 }
Jay Soffian50126992009-02-14 23:32:14 -05001640 elsif (/^Content-type:/i) {
1641 $has_content_type = 1;
1642 if (/charset="?([^ "]+)/) {
1643 $body_encoding = $1;
1644 }
1645 push @xh, $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001646 }
Paolo Bonzinibb294562014-11-25 15:00:26 +01001647 elsif (/^MIME-Version/i) {
1648 $has_mime_version = 1;
1649 push @xh, $_;
1650 }
Jay Soffian50126992009-02-14 23:32:14 -05001651 elsif (/^Message-Id: (.*)/i) {
1652 $message_id = $1;
1653 }
Paolo Bonzinibb294562014-11-25 15:00:26 +01001654 elsif (/^Content-Transfer-Encoding: (.*)/i) {
1655 $xfer_encoding = $1 if not defined $xfer_encoding;
1656 }
Stefan Agner256be1d2018-04-17 23:16:30 +02001657 elsif (/^In-Reply-To: (.*)/i) {
1658 $in_reply_to = $1;
1659 }
1660 elsif (/^References: (.*)/i) {
1661 $references = $1;
1662 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001663 elsif (!/^Date:\s/i && /^[-A-Za-z]+:\s+\S/) {
Jay Soffian50126992009-02-14 23:32:14 -05001664 push @xh, $_;
1665 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001666 } else {
Jay Soffian50126992009-02-14 23:32:14 -05001667 # In the traditional
1668 # "send lots of email" format,
1669 # line 1 = cc
1670 # line 2 = subject
1671 # So let's support that, too.
1672 $input_format = 'lots';
1673 if (@cc == 0 && !$suppress_cc{'cc'}) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001674 printf(__("(non-mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001675 $_, $_) unless $quiet;
1676 push @cc, $_;
1677 } elsif (!defined $subject) {
1678 $subject = $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001679 }
1680 }
1681 }
Jay Soffian50126992009-02-14 23:32:14 -05001682 # Now parse the message body
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001683 while(<$fh>) {
Jay Soffian50126992009-02-14 23:32:14 -05001684 $message .= $_;
Matthieu Moycb2922f2017-08-23 12:21:01 +02001685 if (/^(Signed-off-by|Cc): (.*)/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001686 chomp;
Jay Soffian3531e272009-02-14 23:32:15 -05001687 my ($what, $c) = ($1, $2);
Matthieu Moycb2922f2017-08-23 12:21:01 +02001688 # strip garbage for the address we'll use:
1689 $c = strip_garbage_one_address($c);
1690 # sanitize a bit more to decide whether to suppress the address:
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001691 my $sc = sanitize_address($c);
1692 if ($sc eq $sender) {
Jay Soffian3531e272009-02-14 23:32:15 -05001693 next if ($suppress_cc{'self'});
1694 } else {
1695 next if $suppress_cc{'sob'} and $what =~ /Signed-off-by/i;
1696 next if $suppress_cc{'bodycc'} and $what =~ /Cc/i;
1697 }
Jay Soffian50126992009-02-14 23:32:14 -05001698 push @cc, $c;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001699 printf(__("(body) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001700 $c, $_) unless $quiet;
1701 }
1702 }
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001703 close $fh;
Joe Perches324a8bd2007-08-17 18:51:12 -07001704
Joe Perches6e74e072010-09-24 10:03:00 -07001705 push @to, recipients_cmd("to-cmd", "to", $to_cmd, $t)
1706 if defined $to_cmd;
1707 push @cc, recipients_cmd("cc-cmd", "cc", $cc_cmd, $t)
1708 if defined $cc_cmd && !$suppress_cc{'cccmd'};
Joe Perches324a8bd2007-08-17 18:51:12 -07001709
Thomas Rast3cae7e52010-06-17 22:10:39 +02001710 if ($broken_encoding{$t} && !$has_content_type) {
Paolo Bonzinibb294562014-11-25 15:00:26 +01001711 $xfer_encoding = '8bit' if not defined $xfer_encoding;
Thomas Rast3cae7e52010-06-17 22:10:39 +02001712 $has_content_type = 1;
Paolo Bonzinibb294562014-11-25 15:00:26 +01001713 push @xh, "Content-Type: text/plain; charset=$auto_8bit_encoding";
Thomas Rast3cae7e52010-06-17 22:10:39 +02001714 $body_encoding = $auto_8bit_encoding;
1715 }
1716
Krzysztof Mazurce547802012-10-24 23:08:26 +02001717 if ($broken_encoding{$t} && !is_rfc2047_quoted($subject)) {
1718 $subject = quote_subject($subject, $auto_8bit_encoding);
Thomas Rast3cae7e52010-06-17 22:10:39 +02001719 }
1720
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001721 if (defined $sauthor and $sauthor ne $sender) {
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001722 $message = "From: $author\n\n$message";
Jeff King8291db62007-11-16 05:49:09 -05001723 if (defined $author_encoding) {
1724 if ($has_content_type) {
1725 if ($body_encoding eq $author_encoding) {
1726 # ok, we already have the right encoding
1727 }
1728 else {
1729 # uh oh, we should re-encode
1730 }
1731 }
1732 else {
Paolo Bonzinibb294562014-11-25 15:00:26 +01001733 $xfer_encoding = '8bit' if not defined $xfer_encoding;
Thomas Rast3cae7e52010-06-17 22:10:39 +02001734 $has_content_type = 1;
Jeff King8291db62007-11-16 05:49:09 -05001735 push @xh,
Paolo Bonzinibb294562014-11-25 15:00:26 +01001736 "Content-Type: text/plain; charset=$author_encoding";
Jeff King8291db62007-11-16 05:49:09 -05001737 }
1738 }
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001739 }
brian m. carlsone67a2282018-07-08 22:17:12 +00001740 $xfer_encoding = '8bit' if not defined $xfer_encoding;
1741 ($message, $xfer_encoding) = apply_transfer_encoding(
1742 $message, $xfer_encoding, $target_xfer_encoding);
1743 push @xh, "Content-Transfer-Encoding: $xfer_encoding";
1744 unshift @xh, 'MIME-Version: 1.0' unless $has_mime_version;
Ryan Anderson83b24432005-07-31 04:17:25 -04001745
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001746 $needs_confirm = (
1747 $confirm eq "always" or
1748 ($confirm =~ /^(?:auto|cc)$/ && @cc) or
1749 ($confirm =~ /^(?:auto|compose)$/ && $compose && $message_num == 1));
1750 $needs_confirm = "inform" if ($needs_confirm && $confirm_unconfigured && @cc);
1751
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001752 @to = process_address_list(@to);
1753 @cc = process_address_list(@cc);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001754
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001755 @to = (@initial_to, @to);
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001756 @cc = (@initial_cc, @cc);
1757
Michael S. Tsirkinf515c902014-04-29 08:41:16 +03001758 if ($message_num == 1) {
1759 if (defined $cover_cc and $cover_cc) {
1760 @initial_cc = @cc;
1761 }
1762 if (defined $cover_to and $cover_to) {
1763 @initial_to = @to;
1764 }
1765 }
1766
Michael Witten15da1082009-04-13 13:23:51 -05001767 my $message_was_sent = send_message();
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001768 if ($message_was_sent == -1) {
1769 do_edit($t);
1770 return 0;
1771 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001772
1773 # set up for the next message
Junio C Hamano95a877a2009-06-12 09:23:43 -07001774 if ($thread && $message_was_sent &&
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001775 ($chain_reply_to || !defined $in_reply_to || length($in_reply_to) == 0 ||
Antonio Ospitedb54c8e2010-11-12 15:55:08 +01001776 $message_num == 1)) {
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001777 $in_reply_to = $message_id;
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001778 if (length $references > 0) {
YOSHIFUJI Hideaki / 吉藤英明a925b892007-04-06 08:50:24 +09001779 $references .= "\n $message_id";
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001780 } else {
1781 $references = "$message_id";
1782 }
Ryan Anderson78488b22005-07-31 20:04:24 -04001783 }
Jeff King4f3d3702007-12-17 15:51:34 -05001784 $message_id = undef;
xiaoqiang zhao5453b832017-05-21 20:59:50 +08001785 $num_sent++;
1786 if (defined $batch_size && $num_sent == $batch_size) {
1787 $num_sent = 0;
1788 $smtp->quit if defined $smtp;
1789 undef $smtp;
1790 undef $auth;
1791 sleep($relogin_delay) if defined $relogin_delay;
1792 }
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001793
1794 return 1;
1795}
1796
1797foreach my $t (@files) {
1798 while (!process_file($t)) {
1799 # user edited the file
1800 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001801}
Ryan Andersone2057352005-08-02 21:45:22 -04001802
Joe Perches6e74e072010-09-24 10:03:00 -07001803# Execute a command (e.g. $to_cmd) to get a list of email addresses
1804# and return a results array
1805sub recipients_cmd {
1806 my ($prefix, $what, $cmd, $file) = @_;
1807
Joe Perches6e74e072010-09-24 10:03:00 -07001808 my @addresses = ();
Ramkumar Ramachandraa47eab02013-03-31 18:40:42 -07001809 open my $fh, "-|", "$cmd \Q$file\E"
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001810 or die sprintf(__("(%s) Could not execute '%s'"), $prefix, $cmd);
Junio C Hamano7ebee442010-10-26 22:02:52 -07001811 while (my $address = <$fh>) {
Joe Perches6e74e072010-09-24 10:03:00 -07001812 $address =~ s/^\s*//g;
1813 $address =~ s/\s*$//g;
1814 $address = sanitize_address($address);
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001815 next if ($address eq $sender and $suppress_cc{'self'});
Joe Perches6e74e072010-09-24 10:03:00 -07001816 push @addresses, $address;
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001817 printf(__("(%s) Adding %s: %s from: '%s'\n"),
1818 $prefix, $what, $address, $cmd) unless $quiet;
Joe Perches6e74e072010-09-24 10:03:00 -07001819 }
Junio C Hamano7ebee442010-10-26 22:02:52 -07001820 close $fh
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001821 or die sprintf(__("(%s) failed to close pipe to '%s'"), $prefix, $cmd);
Joe Perches6e74e072010-09-24 10:03:00 -07001822 return @addresses;
1823}
1824
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001825cleanup_compose_files();
Ryan Anderson1f038a02005-09-05 01:13:07 -04001826
Ævar Arnfjörð Bjarmason4bf597e2010-09-30 13:43:00 +00001827sub cleanup_compose_files {
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001828 unlink($compose_filename, $compose_filename . ".final") if $compose;
Ryan Anderson1f038a02005-09-05 01:13:07 -04001829}
1830
Eric Wong4bc87a22006-03-25 17:20:48 -08001831$smtp->quit if $smtp;
Ryan Andersone2057352005-08-02 21:45:22 -04001832
Paolo Bonzini8d814082014-11-25 15:00:27 +01001833sub apply_transfer_encoding {
1834 my $message = shift;
1835 my $from = shift;
1836 my $to = shift;
1837
1838 return $message if ($from eq $to and $from ne '7bit');
1839
1840 require MIME::QuotedPrint;
1841 require MIME::Base64;
1842
1843 $message = MIME::QuotedPrint::decode($message)
1844 if ($from eq 'quoted-printable');
1845 $message = MIME::Base64::decode($message)
1846 if ($from eq 'base64');
1847
brian m. carlson7a369872018-07-08 22:17:10 +00001848 $to = ($message =~ /.{999,}/) ? 'quoted-printable' : '8bit'
1849 if $to eq 'auto';
1850
Vasco Almeida46493102016-12-14 11:54:36 -01001851 die __("cannot send message as 7bit")
Paolo Bonzini8d814082014-11-25 15:00:27 +01001852 if ($to eq '7bit' and $message =~ /[^[:ascii:]]/);
brian m. carlson7a369872018-07-08 22:17:10 +00001853 return ($message, $to)
Paolo Bonzini8d814082014-11-25 15:00:27 +01001854 if ($to eq '7bit' or $to eq '8bit');
brian m. carlson7a369872018-07-08 22:17:10 +00001855 return (MIME::QuotedPrint::encode($message, "\n", 0), $to)
Paolo Bonzini8d814082014-11-25 15:00:27 +01001856 if ($to eq 'quoted-printable');
brian m. carlson7a369872018-07-08 22:17:10 +00001857 return (MIME::Base64::encode($message, "\n"), $to)
Paolo Bonzini8d814082014-11-25 15:00:27 +01001858 if ($to eq 'base64');
Vasco Almeida46493102016-12-14 11:54:36 -01001859 die __("invalid transfer encoding");
Paolo Bonzini8d814082014-11-25 15:00:27 +01001860}
1861
Ævar Arnfjörð Bjarmasonc438ea22010-09-30 13:42:59 +00001862sub unique_email_list {
Ryan Andersone2057352005-08-02 21:45:22 -04001863 my %seen;
1864 my @emails;
1865
1866 foreach my $entry (@_) {
Krzysztof Mazure4312252012-11-22 19:12:10 +01001867 my $clean = extract_valid_address_or_die($entry);
1868 $seen{$clean} ||= 0;
1869 next if $seen{$clean}++;
1870 push @emails, $entry;
Ryan Andersone2057352005-08-02 21:45:22 -04001871 }
1872 return @emails;
1873}
Jeff King747bbff2008-01-18 09:19:48 -05001874
1875sub validate_patch {
brian m. carlsonf2d06fb2018-07-08 22:17:11 +00001876 my ($fn, $xfer_encoding) = @_;
Jonathan Tan64896602017-05-12 15:38:26 -07001877
Jonathan Tan177409e2017-06-01 16:50:55 -07001878 if ($repo) {
1879 my $validate_hook = catfile(catdir($repo->repo_path(), 'hooks'),
1880 'sendemail-validate');
1881 my $hook_error;
1882 if (-x $validate_hook) {
1883 my $target = abs_path($fn);
1884 # The hook needs a correct cwd and GIT_DIR.
1885 my $cwd_save = cwd();
1886 chdir($repo->wc_path() or $repo->repo_path())
1887 or die("chdir: $!");
1888 local $ENV{"GIT_DIR"} = $repo->repo_path();
1889 $hook_error = "rejected by sendemail-validate hook"
1890 if system($validate_hook, $target);
1891 chdir($cwd_save) or die("chdir: $!");
1892 }
1893 return $hook_error if $hook_error;
Jonathan Tan64896602017-05-12 15:38:26 -07001894 }
Jonathan Tan64896602017-05-12 15:38:26 -07001895
brian m. carlsonf2d06fb2018-07-08 22:17:11 +00001896 # Any long lines will be automatically fixed if we use a suitable transfer
1897 # encoding.
1898 unless ($xfer_encoding =~ /^(?:auto|quoted-printable|base64)$/) {
1899 open(my $fh, '<', $fn)
1900 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
1901 while (my $line = <$fh>) {
1902 if (length($line) > 998) {
1903 return sprintf(__("%s: patch contains a line longer than 998 characters"), $.);
1904 }
Jeff King747bbff2008-01-18 09:19:48 -05001905 }
1906 }
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -07001907 return;
Jeff King747bbff2008-01-18 09:19:48 -05001908}
Jeff King0706bd12008-03-28 17:28:33 -04001909
Junio C Hamano531220b2016-03-17 22:40:05 -07001910sub handle_backup {
1911 my ($last, $lastlen, $file, $known_suffix) = @_;
1912 my ($suffix, $skip);
1913
1914 $skip = 0;
1915 if (defined $last &&
1916 ($lastlen < length($file)) &&
1917 (substr($file, 0, $lastlen) eq $last) &&
1918 ($suffix = substr($file, $lastlen)) !~ /^[a-z0-9]/i) {
1919 if (defined $known_suffix && $suffix eq $known_suffix) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001920 printf(__("Skipping %s with backup suffix '%s'.\n"), $file, $known_suffix);
Junio C Hamano531220b2016-03-17 22:40:05 -07001921 $skip = 1;
1922 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001923 # TRANSLATORS: please keep "[y|N]" as is.
1924 my $answer = ask(sprintf(__("Do you really want to send %s? [y|N]: "), $file),
Junio C Hamano531220b2016-03-17 22:40:05 -07001925 valid_re => qr/^(?:y|n)/i,
1926 default => 'n');
1927 $skip = ($answer ne 'y');
1928 if ($skip) {
1929 $known_suffix = $suffix;
1930 }
1931 }
1932 }
1933 return ($skip, $known_suffix);
1934}
1935
1936sub handle_backup_files {
1937 my @file = @_;
1938 my ($last, $lastlen, $known_suffix, $skip, @result);
1939 for my $file (@file) {
1940 ($skip, $known_suffix) = handle_backup($last, $lastlen,
1941 $file, $known_suffix);
1942 push @result, $file unless $skip;
1943 $last = $file;
1944 $lastlen = length($file);
1945 }
1946 return @result;
1947}
1948
Jeff King0706bd12008-03-28 17:28:33 -04001949sub file_has_nonascii {
1950 my $fn = shift;
1951 open(my $fh, '<', $fn)
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001952 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
Jeff King0706bd12008-03-28 17:28:33 -04001953 while (my $line = <$fh>) {
1954 return 1 if $line =~ /[^[:ascii:]]/;
1955 }
1956 return 0;
1957}
Thomas Rast3cae7e52010-06-17 22:10:39 +02001958
1959sub body_or_subject_has_nonascii {
1960 my $fn = shift;
1961 open(my $fh, '<', $fn)
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001962 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
Thomas Rast3cae7e52010-06-17 22:10:39 +02001963 while (my $line = <$fh>) {
1964 last if $line =~ /^$/;
1965 return 1 if $line =~ /^Subject.*[^[:ascii:]]/;
1966 }
1967 while (my $line = <$fh>) {
1968 return 1 if $line =~ /[^[:ascii:]]/;
1969 }
1970 return 0;
1971}