blob: 24859a7bc37b05909cf16554c102cadc842c13ff [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
Joshua Watt8dd9b3f2018-10-22 22:24:25 -050085 --smtp-auth <str> * Space-separated list of allowed AUTH mechanisms, or
86 "none" to disable authentication.
Jan Viktorin0f2e68b2015-08-12 01:39:44 +020087 This setting forces to use one of the listed mechanisms.
Joshua Watt8dd9b3f2018-10-22 22:24:25 -050088 --no-smtp-auth Disable SMTP authentication. Shorthand for
89 `--smtp-auth=none`
Jari Aaltof60812e2010-03-14 17:16:09 +020090 --smtp-debug <0|1> * Disable, enable Net::SMTP debug.
Michael Witten4ed62b02008-09-30 07:58:30 -050091
xiaoqiang zhao5453b832017-05-21 20:59:50 +080092 --batch-size <int> * send max <int> message per connection.
93 --relogin-delay <int> * delay <int> seconds between two successive login.
94 This option can only be used with --batch-size
95
Michael Witten4ed62b02008-09-30 07:58:30 -050096 Automating:
97 --identity <str> * Use the sendemail.<id> options.
Joe Perches6e74e072010-09-24 10:03:00 -070098 --to-cmd <str> * Email To: via `<str> \$patch_path`
Michael Witten4ed62b02008-09-30 07:58:30 -050099 --cc-cmd <str> * Email Cc: via `<str> \$patch_path`
Rasmus Villemoesef0cc1d2018-10-16 09:39:23 +0200100 --suppress-cc <str> * author, self, sob, cc, cccmd, body, bodycc, misc-by, all.
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300101 --[no-]cc-cover * Email Cc: addresses in the cover letter.
102 --[no-]to-cover * Email To: addresses in the cover letter.
Jay Soffian3531e272009-02-14 23:32:15 -0500103 --[no-]signed-off-by-cc * Send to Signed-off-by: addresses. Default on.
Michael Witten4ed62b02008-09-30 07:58:30 -0500104 --[no-]suppress-from * Send to self. Default off.
Junio C Hamano41fe87f2009-08-22 12:48:48 -0700105 --[no-]chain-reply-to * Chain In-Reply-To: fields. Default off.
Michael Witten4ed62b02008-09-30 07:58:30 -0500106 --[no-]thread * Use In-Reply-To: field. Default on.
107
108 Administering:
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500109 --confirm <str> * Confirm recipients before sending;
110 auto, cc, compose, always, or never.
Michael Witten4ed62b02008-09-30 07:58:30 -0500111 --quiet * Output one line of info per email.
112 --dry-run * Don't actually send the emails.
113 --[no-]validate * Perform patch sanity checks. Default on.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100114 --[no-]format-patch * understand any non optional arguments as
115 `git format-patch` ones.
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200116 --force * Send even if safety checks would prevent it.
Jeff Kingc764a0c2008-01-18 09:20:10 -0500117
Jacob Keller17b7a832015-11-19 14:52:11 -0800118 Information:
119 --dump-aliases * Dump configured aliases and exit.
120
Michael Coleman1b0baf12007-02-27 22:47:54 -0600121EOT
122 exit(1);
123}
124
Duy Nguyen13374982018-11-03 07:03:18 +0100125sub completion_helper {
126 print Git::command('format-patch', '--git-completion-helper');
127 exit(0);
128}
129
Eric Wong4bc87a22006-03-25 17:20:48 -0800130# most mail servers generate the Date: header, but not all...
Jakub Narebski6bdca892006-07-07 20:57:55 +0200131sub format_2822_time {
132 my ($time) = @_;
133 my @localtm = localtime($time);
134 my @gmttm = gmtime($time);
135 my $localmin = $localtm[1] + $localtm[2] * 60;
136 my $gmtmin = $gmttm[1] + $gmttm[2] * 60;
137 if ($localtm[0] != $gmttm[0]) {
Vasco Almeida46493102016-12-14 11:54:36 -0100138 die __("local zone differs from GMT by a non-minute interval\n");
Jakub Narebski6bdca892006-07-07 20:57:55 +0200139 }
140 if ((($gmttm[6] + 1) % 7) == $localtm[6]) {
141 $localmin += 1440;
142 } elsif ((($gmttm[6] - 1) % 7) == $localtm[6]) {
143 $localmin -= 1440;
144 } elsif ($gmttm[6] != $localtm[6]) {
Vasco Almeida46493102016-12-14 11:54:36 -0100145 die __("local time offset greater than or equal to 24 hours\n");
Jakub Narebski6bdca892006-07-07 20:57:55 +0200146 }
147 my $offset = $localmin - $gmtmin;
148 my $offhour = $offset / 60;
149 my $offmin = abs($offset % 60);
150 if (abs($offhour) >= 24) {
Vasco Almeida46493102016-12-14 11:54:36 -0100151 die __("local time offset greater than or equal to 24 hours\n");
Jakub Narebski6bdca892006-07-07 20:57:55 +0200152 }
153
154 return sprintf("%s, %2d %s %d %02d:%02d:%02d %s%02d%02d",
155 qw(Sun Mon Tue Wed Thu Fri Sat)[$localtm[6]],
156 $localtm[3],
157 qw(Jan Feb Mar Apr May Jun
158 Jul Aug Sep Oct Nov Dec)[$localtm[4]],
159 $localtm[5]+1900,
160 $localtm[2],
161 $localtm[1],
162 $localtm[0],
163 ($offset >= 0) ? '+' : '-',
164 abs($offhour),
165 $offmin,
166 );
167}
Eric Wong4bc87a22006-03-25 17:20:48 -0800168
Eric Wong567ffeb2006-03-25 16:47:12 -0800169my $have_email_valid = eval { require Email::Valid; 1 };
Eric Wong4bc87a22006-03-25 17:20:48 -0800170my $smtp;
Wincent Colaiuta5f5b6112007-11-21 13:35:05 +0100171my $auth;
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800172my $num_sent = 0;
Eric Wong4bc87a22006-03-25 17:20:48 -0800173
Роман Донченко11f70a72014-12-14 18:59:46 +0300174# Regexes for RFC 2047 productions.
175my $re_token = qr/[^][()<>@,;:\\"\/?.= \000-\037\177-\377]+/;
176my $re_encoded_text = qr/[^? \000-\037\177-\377]+/;
177my $re_encoded_word = qr/=\?($re_token)\?($re_token)\?($re_encoded_text)\?=/;
178
Ryan Anderson83b24432005-07-31 04:17:25 -0400179# Variables we fill in automatically, or via prompting:
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700180my (@to,$no_to,@initial_to,@cc,$no_cc,@initial_cc,@bcclist,$no_bcc,@xh,
Christian Ludwigd11c9432018-03-04 00:58:14 +0100181 $initial_in_reply_to,$reply_to,$initial_subject,@files,
Luis Henriquesac1596a2014-03-24 21:38:27 +0000182 $author,$sender,$smtp_authpass,$annotate,$use_xmailer,$compose,$time);
Ryan Anderson83b24432005-07-31 04:17:25 -0400183
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700184my $envelope_sender;
Ryan Anderson78488b22005-07-31 20:04:24 -0400185
Ryan Anderson91332612005-07-31 20:04:24 -0400186# Example reply to:
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100187#$initial_in_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
Ryan Anderson83b24432005-07-31 04:17:25 -0400188
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100189my $repo = eval { Git->repository() };
190my @repo = $repo ? ($repo) : ();
Junio C Hamano280242d2006-07-02 16:03:59 -0700191my $term = eval {
Jay Soffian0fb7fc72008-02-21 19:16:04 -0500192 $ENV{"GIT_SEND_EMAIL_NOTTY"}
193 ? new Term::ReadLine 'git-send-email', \*STDIN, \*STDOUT
194 : new Term::ReadLine 'git-send-email';
Junio C Hamano280242d2006-07-02 16:03:59 -0700195};
196if ($@) {
197 $term = new FakeTerm "$@: going non-interactive";
198}
Ryan Anderson83b24432005-07-31 04:17:25 -0400199
Adam Roben5483c712007-06-27 20:59:37 -0700200# Behavior modification variables
201my ($quiet, $dry_run) = (0, 0);
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100202my $format_patch;
Jay Soffianafe756c2009-02-23 13:51:37 -0500203my $compose_filename;
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200204my $force = 0;
Jacob Keller17b7a832015-11-19 14:52:11 -0800205my $dump_aliases = 0;
Adam Roben5483c712007-06-27 20:59:37 -0700206
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100207# Handle interactive edition of files.
208my $multiedit;
Michael J Gruber0ce142c2010-03-22 17:12:53 +0100209my $editor;
Jonathan Niederb4479f02009-10-30 20:42:34 -0500210
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100211sub do_edit {
Michael J Gruber0ce142c2010-03-22 17:12:53 +0100212 if (!defined($editor)) {
213 $editor = Git::command_oneline('var', 'GIT_EDITOR');
214 }
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100215 if (defined($multiedit) && !$multiedit) {
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100216 map {
217 system('sh', '-c', $editor.' "$@"', $editor, $_);
218 if (($? & 127) || ($? >> 8)) {
Vasco Almeida46493102016-12-14 11:54:36 -0100219 die(__("the editor exited uncleanly, aborting everything"));
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100220 }
221 } @_;
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100222 } else {
223 system('sh', '-c', $editor.' "$@"', $editor, @_);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100224 if (($? & 127) || ($? >> 8)) {
Vasco Almeida46493102016-12-14 11:54:36 -0100225 die(__("the editor exited uncleanly, aborting everything"));
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100226 }
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100227 }
228}
Adam Roben5483c712007-06-27 20:59:37 -0700229
230# Variables with corresponding config settings
Joe Perches6e74e072010-09-24 10:03:00 -0700231my ($thread, $chain_reply_to, $suppress_from, $signed_off_by_cc);
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300232my ($cover_cc, $cover_to);
Joe Perches6e74e072010-09-24 10:03:00 -0700233my ($to_cmd, $cc_cmd);
Pascal Obry052fbea2010-09-06 20:12:11 +0200234my ($smtp_server, $smtp_server_port, @smtp_server_options);
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -0700235my ($smtp_authuser, $smtp_encryption, $smtp_ssl_cert_path);
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800236my ($batch_size, $relogin_delay);
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200237my ($identity, $aliasfiletype, @alias_files, $smtp_domain, $smtp_auth);
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500238my ($validate, $confirm);
David Brown65648282007-12-25 19:56:29 -0800239my (@suppress_cc);
Thomas Rast3cae7e52010-06-17 22:10:39 +0200240my ($auto_8bit_encoding);
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200241my ($compose_encoding);
brian m. carlsone67a2282018-07-08 22:17:12 +0000242my $target_xfer_encoding = 'auto';
Adam Roben5483c712007-06-27 20:59:37 -0700243
Jari Aaltof60812e2010-03-14 17:16:09 +0200244my ($debug_net_smtp) = 0; # Net::SMTP, see send_message()
245
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900246my %config_bool_settings = (
Adam Roben5483c712007-06-27 20:59:37 -0700247 "thread" => [\$thread, 1],
Felipe Contrerasb99d22f2013-05-24 22:44:52 -0500248 "chainreplyto" => [\$chain_reply_to, 0],
David Brown65648282007-12-25 19:56:29 -0800249 "suppressfrom" => [\$suppress_from, undef],
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500250 "signedoffbycc" => [\$signed_off_by_cc, undef],
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300251 "cccover" => [\$cover_cc, undef],
252 "tocover" => [\$cover_to, undef],
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500253 "signedoffcc" => [\$signed_off_by_cc, undef], # Deprecated
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500254 "validate" => [\$validate, 1],
Felipe Contreras402596a2013-04-07 01:10:27 -0600255 "multiedit" => [\$multiedit, undef],
Luis Henriquesac1596a2014-03-24 21:38:27 +0000256 "annotate" => [\$annotate, undef],
257 "xmailer" => [\$use_xmailer, 1]
Adam Robene46f7a02007-06-26 15:48:30 -0700258);
259
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900260my %config_settings = (
261 "smtpserver" => \$smtp_server,
Junio C Hamano44b24762007-09-25 17:27:54 -0700262 "smtpserverport" => \$smtp_server_port,
Pascal Obry052fbea2010-09-06 20:12:11 +0200263 "smtpserveroption" => \@smtp_server_options,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900264 "smtpuser" => \$smtp_authuser,
265 "smtppass" => \$smtp_authpass,
Pascal Obrye1e91152010-09-06 20:12:09 +0200266 "smtpdomain" => \$smtp_domain,
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200267 "smtpauth" => \$smtp_auth,
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800268 "smtpbatchsize" => \$batch_size,
269 "smtprelogindelay" => \$relogin_delay,
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700270 "to" => \@initial_to,
Joe Perches6e74e072010-09-24 10:03:00 -0700271 "tocmd" => \$to_cmd,
Miklos Vajna5f8b9fc2008-04-27 14:14:58 +0200272 "cc" => \@initial_cc,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900273 "cccmd" => \$cc_cmd,
274 "aliasfiletype" => \$aliasfiletype,
275 "bcc" => \@bcclist,
David Brown65648282007-12-25 19:56:29 -0800276 "suppresscc" => \@suppress_cc,
Ask Bjørn Hansen9f7820a2008-06-07 00:33:42 -0700277 "envelopesender" => \$envelope_sender,
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500278 "confirm" => \$confirm,
Trent Piepho09caa242009-05-12 15:48:56 -0700279 "from" => \$sender,
Thomas Rast3cae7e52010-06-17 22:10:39 +0200280 "assume8bitencoding" => \$auto_8bit_encoding,
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200281 "composeencoding" => \$compose_encoding,
Paolo Bonzini8d814082014-11-25 15:00:27 +0100282 "transferencoding" => \$target_xfer_encoding,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900283);
Avi Kivity4a62d3f2007-03-11 19:19:44 +0200284
Cord Seelecec5dae2011-09-30 12:52:25 +0200285my %config_path_settings = (
286 "aliasesfile" => \@alias_files,
John Keeping6e07a3b2015-11-17 22:01:05 +0000287 "smtpsslcertpath" => \$smtp_ssl_cert_path,
Cord Seelecec5dae2011-09-30 12:52:25 +0200288);
289
Michael Witten87429972008-02-03 19:53:57 -0500290# Handle Uncouth Termination
291sub signal_handler {
292
293 # Make text normal
294 print color("reset"), "\n";
295
296 # SMTP password masked
297 system "stty echo";
298
299 # tmp files from --compose
Jay Soffianafe756c2009-02-23 13:51:37 -0500300 if (defined $compose_filename) {
301 if (-e $compose_filename) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100302 printf __("'%s' contains an intermediate version ".
303 "of the email you were composing.\n"),
304 $compose_filename;
Jay Soffianafe756c2009-02-23 13:51:37 -0500305 }
306 if (-e ($compose_filename . ".final")) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100307 printf __("'%s.final' contains the composed email.\n"),
308 $compose_filename;
Jay Soffianafe756c2009-02-23 13:51:37 -0500309 }
Michael Witten87429972008-02-03 19:53:57 -0500310 }
311
312 exit;
313};
314
315$SIG{TERM} = \&signal_handler;
316$SIG{INT} = \&signal_handler;
317
Ryan Anderson83b24432005-07-31 04:17:25 -0400318# Begin by accumulating all the variables (defined above), that we will end up
319# needing, first, from the command line:
320
Clemens Buchacherc5978242011-09-03 19:06:13 +0200321my $help;
Duy Nguyen13374982018-11-03 07:03:18 +0100322my $git_completion_helper;
Clemens Buchacherc5978242011-09-03 19:06:13 +0200323my $rc = GetOptions("h" => \$help,
Jacob Keller17b7a832015-11-19 14:52:11 -0800324 "dump-aliases" => \$dump_aliases);
325usage() unless $rc;
Vasco Almeida46493102016-12-14 11:54:36 -0100326die __("--dump-aliases incompatible with other options\n")
Jacob Keller17b7a832015-11-19 14:52:11 -0800327 if !$help and $dump_aliases and @ARGV;
328$rc = GetOptions(
Clemens Buchacherc5978242011-09-03 19:06:13 +0200329 "sender|from=s" => \$sender,
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100330 "in-reply-to=s" => \$initial_in_reply_to,
Christian Ludwigd11c9432018-03-04 00:58:14 +0100331 "reply-to=s" => \$reply_to,
Ryan Anderson83b24432005-07-31 04:17:25 -0400332 "subject=s" => \$initial_subject,
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700333 "to=s" => \@initial_to,
Joe Perches6e74e072010-09-24 10:03:00 -0700334 "to-cmd=s" => \$to_cmd,
Stephen Boydf434c082010-03-07 14:46:48 -0800335 "no-to" => \$no_to,
Ryan Andersonda140f82006-02-13 03:05:15 -0500336 "cc=s" => \@initial_cc,
Stephen Boydf434c082010-03-07 14:46:48 -0800337 "no-cc" => \$no_cc,
Ryan Anderson58063242006-05-29 12:30:13 -0700338 "bcc=s" => \@bcclist,
Stephen Boydf434c082010-03-07 14:46:48 -0800339 "no-bcc" => \$no_bcc,
Ryan Anderson78488b22005-07-31 20:04:24 -0400340 "chain-reply-to!" => \$chain_reply_to,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800341 "no-chain-reply-to" => sub {$chain_reply_to = 0},
Ryan Anderson3342d852005-07-31 20:04:24 -0400342 "smtp-server=s" => \$smtp_server,
Pascal Obry052fbea2010-09-06 20:12:11 +0200343 "smtp-server-option=s" => \@smtp_server_options,
Junio C Hamano44b24762007-09-25 17:27:54 -0700344 "smtp-server-port=s" => \$smtp_server_port,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900345 "smtp-user=s" => \$smtp_authuser,
Michael Witten2363d742008-02-03 19:53:56 -0500346 "smtp-pass:s" => \$smtp_authpass,
Thomas Rastf6bebd12008-06-25 21:42:43 +0200347 "smtp-ssl" => sub { $smtp_encryption = 'ssl' },
348 "smtp-encryption=s" => \$smtp_encryption,
Thomas Rast979e6522013-12-01 23:48:42 +0100349 "smtp-ssl-cert-path=s" => \$smtp_ssl_cert_path,
Jari Aaltof60812e2010-03-14 17:16:09 +0200350 "smtp-debug:i" => \$debug_net_smtp,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -0400351 "smtp-domain:s" => \$smtp_domain,
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200352 "smtp-auth=s" => \$smtp_auth,
Joshua Watt8dd9b3f2018-10-22 22:24:25 -0500353 "no-smtp-auth" => sub {$smtp_auth = 'none'},
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900354 "identity=s" => \$identity,
Felipe Contreras402596a2013-04-07 01:10:27 -0600355 "annotate!" => \$annotate,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800356 "no-annotate" => sub {$annotate = 0},
Ryan Anderson1f038a02005-09-05 01:13:07 -0400357 "compose" => \$compose,
Ryan Anderson30d08b32006-02-02 11:56:06 -0500358 "quiet" => \$quiet,
Joe Perches324a8bd2007-08-17 18:51:12 -0700359 "cc-cmd=s" => \$cc_cmd,
Adam Roben5483c712007-06-27 20:59:37 -0700360 "suppress-from!" => \$suppress_from,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800361 "no-suppress-from" => sub {$suppress_from = 0},
David Brown65648282007-12-25 19:56:29 -0800362 "suppress-cc=s" => \@suppress_cc,
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500363 "signed-off-cc|signed-off-by-cc!" => \$signed_off_by_cc,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800364 "no-signed-off-cc|no-signed-off-by-cc" => sub {$signed_off_by_cc = 0},
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300365 "cc-cover|cc-cover!" => \$cover_cc,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800366 "no-cc-cover" => sub {$cover_cc = 0},
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300367 "to-cover|to-cover!" => \$cover_to,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800368 "no-to-cover" => sub {$cover_to = 0},
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500369 "confirm=s" => \$confirm,
Matthew Wilcox61302592006-10-10 08:58:23 -0600370 "dry-run" => \$dry_run,
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700371 "envelope-sender=s" => \$envelope_sender,
Adam Roben5483c712007-06-27 20:59:37 -0700372 "thread!" => \$thread,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800373 "no-thread" => sub {$thread = 0},
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500374 "validate!" => \$validate,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800375 "no-validate" => sub {$validate = 0},
Paolo Bonzini8d814082014-11-25 15:00:27 +0100376 "transfer-encoding=s" => \$target_xfer_encoding,
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100377 "format-patch!" => \$format_patch,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800378 "no-format-patch" => sub {$format_patch = 0},
Thomas Rast3cae7e52010-06-17 22:10:39 +0200379 "8bit-encoding=s" => \$auto_8bit_encoding,
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200380 "compose-encoding=s" => \$compose_encoding,
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200381 "force" => \$force,
Luis Henriquesac1596a2014-03-24 21:38:27 +0000382 "xmailer!" => \$use_xmailer,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800383 "no-xmailer" => sub {$use_xmailer = 0},
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800384 "batch-size=i" => \$batch_size,
385 "relogin-delay=i" => \$relogin_delay,
Duy Nguyen13374982018-11-03 07:03:18 +0100386 "git-completion-helper" => \$git_completion_helper,
Ryan Anderson83b24432005-07-31 04:17:25 -0400387 );
388
Clemens Buchacherc5978242011-09-03 19:06:13 +0200389usage() if $help;
Duy Nguyen13374982018-11-03 07:03:18 +0100390completion_helper() if $git_completion_helper;
Michael Coleman1b0baf12007-02-27 22:47:54 -0600391unless ($rc) {
392 usage();
393}
394
Vasco Almeida46493102016-12-14 11:54:36 -0100395die __("Cannot run git format-patch from outside a repository\n")
Jay Soffianeed6ca72009-02-14 23:32:13 -0500396 if $format_patch and not $repo;
397
Stefan Beller9caa7062018-02-12 11:44:04 -0800398die __("`batch-size` and `relogin` must be specified together " .
399 "(via command-line or configuration option)\n")
400 if defined $relogin_delay and not defined $batch_size;
401
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900402# Now, let's fill any that aren't set in with defaults:
403
404sub read_config {
405 my ($prefix) = @_;
406
407 foreach my $setting (keys %config_bool_settings) {
408 my $target = $config_bool_settings{$setting}->[0];
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100409 $$target = Git::config_bool(@repo, "$prefix.$setting") unless (defined $$target);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900410 }
411
Cord Seelecec5dae2011-09-30 12:52:25 +0200412 foreach my $setting (keys %config_path_settings) {
Cord Seele463b0ea2011-10-14 22:53:31 +0200413 my $target = $config_path_settings{$setting};
414 if (ref($target) eq "ARRAY") {
415 unless (@$target) {
416 my @values = Git::config_path(@repo, "$prefix.$setting");
417 @$target = @values if (@values && defined $values[0]);
418 }
419 }
420 else {
421 $$target = Git::config_path(@repo, "$prefix.$setting") unless (defined $$target);
422 }
Cord Seelecec5dae2011-09-30 12:52:25 +0200423 }
424
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900425 foreach my $setting (keys %config_settings) {
426 my $target = $config_settings{$setting};
Stephen Boydf434c082010-03-07 14:46:48 -0800427 next if $setting eq "to" and defined $no_to;
428 next if $setting eq "cc" and defined $no_cc;
429 next if $setting eq "bcc" and defined $no_bcc;
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900430 if (ref($target) eq "ARRAY") {
431 unless (@$target) {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100432 my @values = Git::config(@repo, "$prefix.$setting");
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900433 @$target = @values if (@values && defined $values[0]);
434 }
435 }
436 else {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100437 $$target = Git::config(@repo, "$prefix.$setting") unless (defined $$target);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900438 }
439 }
Thomas Rastf6bebd12008-06-25 21:42:43 +0200440
441 if (!defined $smtp_encryption) {
442 my $enc = Git::config(@repo, "$prefix.smtpencryption");
443 if (defined $enc) {
444 $smtp_encryption = $enc;
445 } elsif (Git::config_bool(@repo, "$prefix.smtpssl")) {
446 $smtp_encryption = 'ssl';
447 }
448 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900449}
450
451# read configuration from [sendemail "$identity"], fall back on [sendemail]
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100452$identity = Git::config(@repo, "sendemail.identity") unless (defined $identity);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900453read_config("sendemail.$identity") if (defined $identity);
454read_config("sendemail");
455
456# fall back on builtin bool defaults
457foreach my $setting (values %config_bool_settings) {
458 ${$setting->[0]} = $setting->[1] unless (defined (${$setting->[0]}));
459}
460
Thomas Rastfa835cd2008-06-26 23:03:21 +0200461# 'default' encryption is none -- this only prevents a warning
462$smtp_encryption = '' unless (defined $smtp_encryption);
463
David Brown65648282007-12-25 19:56:29 -0800464# Set CC suppressions
465my(%suppress_cc);
466if (@suppress_cc) {
467 foreach my $entry (@suppress_cc) {
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700468 # Please update $__git_send_email_suppresscc_options
469 # in git-completion.bash when you add new options.
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100470 die sprintf(__("Unknown --suppress-cc field: '%s'\n"), $entry)
Rasmus Villemoesef0cc1d2018-10-16 09:39:23 +0200471 unless $entry =~ /^(?:all|cccmd|cc|author|self|sob|body|bodycc|misc-by)$/;
David Brown65648282007-12-25 19:56:29 -0800472 $suppress_cc{$entry} = 1;
473 }
474}
475
476if ($suppress_cc{'all'}) {
Rasmus Villemoesef0cc1d2018-10-16 09:39:23 +0200477 foreach my $entry (qw (cccmd cc author self sob body bodycc misc-by)) {
David Brown65648282007-12-25 19:56:29 -0800478 $suppress_cc{$entry} = 1;
479 }
480 delete $suppress_cc{'all'};
481}
482
483# If explicit old-style ones are specified, they trump --suppress-cc.
484$suppress_cc{'self'} = $suppress_from if defined $suppress_from;
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500485$suppress_cc{'sob'} = !$signed_off_by_cc if defined $signed_off_by_cc;
David Brown65648282007-12-25 19:56:29 -0800486
Jay Soffian3531e272009-02-14 23:32:15 -0500487if ($suppress_cc{'body'}) {
Rasmus Villemoesef0cc1d2018-10-16 09:39:23 +0200488 foreach my $entry (qw (sob bodycc misc-by)) {
Jay Soffian3531e272009-02-14 23:32:15 -0500489 $suppress_cc{$entry} = 1;
490 }
491 delete $suppress_cc{'body'};
492}
493
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500494# Set confirm's default value
495my $confirm_unconfigured = !defined $confirm;
496if ($confirm_unconfigured) {
497 $confirm = scalar %suppress_cc ? 'compose' : 'auto';
498};
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700499# Please update $__git_send_email_confirm_options in
500# git-completion.bash when you add new options.
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100501die sprintf(__("Unknown --confirm setting: '%s'\n"), $confirm)
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500502 unless $confirm =~ /^(?:auto|cc|compose|always|never)/;
503
David Brown65648282007-12-25 19:56:29 -0800504# Debugging, print out the suppressions.
505if (0) {
506 print "suppressions:\n";
507 foreach my $entry (keys %suppress_cc) {
508 printf " %-5s -> $suppress_cc{$entry}\n", $entry;
509 }
510}
511
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100512my ($repoauthor, $repocommitter);
513($repoauthor) = Git::ident_person(@repo, 'author');
514($repocommitter) = Git::ident_person(@repo, 'committer');
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900515
Jay Soffian50126992009-02-14 23:32:14 -0500516sub parse_address_line {
Matthieu Moybd869f62018-01-05 19:36:51 +0100517 return map { $_->format } Mail::Address->parse($_[0]);
Jay Soffian50126992009-02-14 23:32:14 -0500518}
519
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800520sub split_addrs {
Junio C Hamano2f0e7cb2008-12-21 01:57:59 -0800521 return quotewords('\s*,\s*', 1, @_);
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800522}
523
Eric Wong994d6c62006-05-14 19:13:44 -0700524my %aliases;
Eric Sunshine09f11572015-05-31 18:29:27 -0400525
526sub parse_sendmail_alias {
527 local $_ = shift;
528 if (/"/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100529 printf STDERR __("warning: sendmail alias with quotes is not supported: %s\n"), $_;
Eric Sunshine86b89842015-06-01 14:22:36 -0400530 } elsif (/:include:/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100531 printf STDERR __("warning: `:include:` not supported: %s\n"), $_;
Eric Sunshine86b89842015-06-01 14:22:36 -0400532 } elsif (/[\/|]/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100533 printf STDERR __("warning: `/file` or `|pipe` redirection not supported: %s\n"), $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400534 } elsif (/^(\S+?)\s*:\s*(.+)$/) {
535 my ($alias, $addr) = ($1, $2);
536 $aliases{$alias} = [ split_addrs($addr) ];
537 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100538 printf STDERR __("warning: sendmail line is not recognized: %s\n"), $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400539 }
540}
541
542sub parse_sendmail_aliases {
543 my $fh = shift;
Eric Sunshine2532dd02015-05-31 18:29:29 -0400544 my $s = '';
Eric Sunshine09f11572015-05-31 18:29:27 -0400545 while (<$fh>) {
Eric Sunshine2532dd02015-05-31 18:29:29 -0400546 chomp;
Eric Sunshine020be85f2015-05-31 18:29:28 -0400547 next if /^\s*$/ || /^\s*#/;
Eric Sunshine2532dd02015-05-31 18:29:29 -0400548 $s .= $_, next if $s =~ s/\\$// || s/^\s+//;
549 parse_sendmail_alias($s) if $s;
550 $s = $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400551 }
Eric Sunshine2532dd02015-05-31 18:29:29 -0400552 $s =~ s/\\$//; # silently tolerate stray '\' on last line
553 parse_sendmail_alias($s) if $s;
Eric Sunshine09f11572015-05-31 18:29:27 -0400554}
555
Eric Wong994d6c62006-05-14 19:13:44 -0700556my %parse_alias = (
557 # multiline formats can be supported in the future
558 mutt => sub { my $fh = shift; while (<$fh>) {
Felipe Contrerasffc01f92009-09-30 17:49:36 +0300559 if (/^\s*alias\s+(?:-group\s+\S+\s+)*(\S+)\s+(.*)$/) {
Eric Wong994d6c62006-05-14 19:13:44 -0700560 my ($alias, $addr) = ($1, $2);
561 $addr =~ s/#.*$//; # mutt allows # comments
Eric Wong2c510f22016-01-04 20:53:30 +0000562 # commas delimit multiple addresses
563 my @addr = split_addrs($addr);
564
565 # quotes may be escaped in the file,
566 # unescape them so we do not double-escape them later.
567 s/\\"/"/g foreach @addr;
568 $aliases{$alias} = \@addr
Eric Wong994d6c62006-05-14 19:13:44 -0700569 }}},
570 mailrc => sub { my $fh = shift; while (<$fh>) {
Jeff Kinga277d1e2016-03-17 19:58:22 -0400571 if (/^alias\s+(\S+)\s+(.*?)\s*$/) {
Eric Wong994d6c62006-05-14 19:13:44 -0700572 # spaces delimit multiple addresses
Eric W. Biedermanfe87c922009-05-20 19:45:53 -0700573 $aliases{$1} = [ quotewords('\s+', 0, $2) ];
Eric Wong994d6c62006-05-14 19:13:44 -0700574 }}},
Trent Piepho73c427e2008-11-25 18:55:00 -0800575 pine => sub { my $fh = shift; my $f='\t[^\t]*';
576 for (my $x = ''; defined($x); $x = $_) {
577 chomp $x;
578 $x .= $1 while(defined($_ = <$fh>) && /^ +(.*)$/);
579 $x =~ /^(\S+)$f\t\(?([^\t]+?)\)?(:?$f){0,2}$/ or next;
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800580 $aliases{$1} = [ split_addrs($2) ];
Trent Piepho73c427e2008-11-25 18:55:00 -0800581 }},
Bill Pemberton7613ea32009-04-22 09:41:29 -0400582 elm => sub { my $fh = shift;
583 while (<$fh>) {
584 if (/^(\S+)\s+=\s+[^=]+=\s(\S+)/) {
585 my ($alias, $addr) = ($1, $2);
586 $aliases{$alias} = [ split_addrs($addr) ];
587 }
588 } },
Eric Sunshine09f11572015-05-31 18:29:27 -0400589 sendmail => \&parse_sendmail_aliases,
Eric Wong994d6c62006-05-14 19:13:44 -0700590 gnus => sub { my $fh = shift; while (<$fh>) {
591 if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
592 $aliases{$1} = [ $2 ];
593 }}}
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700594 # Please update _git_config() in git-completion.bash when you
595 # add new MUAs.
Eric Wong994d6c62006-05-14 19:13:44 -0700596);
597
Petr Baudis3cb8caf2006-07-03 22:47:58 +0200598if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) {
Eric Wong994d6c62006-05-14 19:13:44 -0700599 foreach my $file (@alias_files) {
600 open my $fh, '<', $file or die "opening $file: $!\n";
601 $parse_alias{$aliasfiletype}->($fh);
602 close $fh;
603 }
604}
605
Jacob Keller17b7a832015-11-19 14:52:11 -0800606if ($dump_aliases) {
607 print "$_\n" for (sort keys %aliases);
608 exit(0);
609}
610
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700611# is_format_patch_arg($f) returns 0 if $f names a patch, or 1 if
612# $f is a revision list specification to be passed to format-patch.
613sub is_format_patch_arg {
Jay Soffianeed6ca72009-02-14 23:32:13 -0500614 return unless $repo;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100615 my $f = shift;
616 try {
617 $repo->command('rev-parse', '--verify', '--quiet', $f);
618 if (defined($format_patch)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100619 return $format_patch;
620 }
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100621 die sprintf(__ <<EOF, $f, $f);
622File '%s' exists but it could also be the range of commits
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100623to produce patches for. Please disambiguate by...
624
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100625 * Saying "./%s" if you mean a file; or
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100626 * Giving --format-patch option if you mean a range.
627EOF
628 } catch Git::Error::Command with {
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700629 # Not a valid revision. Treat it as a filename.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100630 return 0;
631 }
632}
633
Jeff Kingaa548922008-01-18 09:19:36 -0500634# Now that all the defaults are set, process the rest of the command line
635# arguments and collect up the files that need to be processed.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100636my @rev_list_opts;
Junio C Hamano69f4ce52008-11-30 22:38:20 -0800637while (defined(my $f = shift @ARGV)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100638 if ($f eq "--") {
639 push @rev_list_opts, "--", @ARGV;
640 @ARGV = ();
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700641 } elsif (-d $f and !is_format_patch_arg($f)) {
Ævar Arnfjörð Bjarmasonc6038162010-09-30 13:42:54 +0000642 opendir my $dh, $f
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100643 or die sprintf(__("Failed to opendir %s: %s"), $f, $!);
Jeff Kingaa548922008-01-18 09:19:36 -0500644
Ævar Arnfjörð Bjarmason89bf1ba2010-09-14 19:02:24 +0000645 push @files, grep { -f $_ } map { catfile($f, $_) }
Ævar Arnfjörð Bjarmasonc6038162010-09-30 13:42:54 +0000646 sort readdir $dh;
647 closedir $dh;
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700648 } elsif ((-f $f or -p $f) and !is_format_patch_arg($f)) {
Jeff Kingaa548922008-01-18 09:19:36 -0500649 push @files, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500650 } else {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100651 push @rev_list_opts, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500652 }
653}
654
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100655if (@rev_list_opts) {
Vasco Almeida46493102016-12-14 11:54:36 -0100656 die __("Cannot run git format-patch from outside a repository\n")
Jay Soffianeed6ca72009-02-14 23:32:13 -0500657 unless $repo;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100658 push @files, $repo->command('format-patch', '-o', tempdir(CLEANUP => 1), @rev_list_opts);
659}
660
Junio C Hamano531220b2016-03-17 22:40:05 -0700661@files = handle_backup_files(@files);
662
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500663if ($validate) {
Jeff Kingc764a0c2008-01-18 09:20:10 -0500664 foreach my $f (@files) {
Kevin Ballard300913b2008-06-25 15:44:40 -0700665 unless (-p $f) {
brian m. carlsonf2d06fb2018-07-08 22:17:11 +0000666 my $error = validate_patch($f, $target_xfer_encoding);
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100667 $error and die sprintf(__("fatal: %s: %s\nwarning: no patches were sent\n"),
668 $f, $error);
Kevin Ballard300913b2008-06-25 15:44:40 -0700669 }
Jeff Kingc764a0c2008-01-18 09:20:10 -0500670 }
Jeff King747bbff2008-01-18 09:19:48 -0500671}
672
Jeff Kingaa548922008-01-18 09:19:36 -0500673if (@files) {
674 unless ($quiet) {
675 print $_,"\n" for (@files);
676 }
677} else {
Vasco Almeida46493102016-12-14 11:54:36 -0100678 print STDERR __("\nNo patch files specified!\n\n");
Jeff Kingaa548922008-01-18 09:19:36 -0500679 usage();
680}
681
Ævar Arnfjörð Bjarmasonacf071b2010-09-30 13:42:57 +0000682sub get_patch_subject {
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100683 my $fn = shift;
684 open (my $fh, '<', $fn);
685 while (my $line = <$fh>) {
686 next unless ($line =~ /^Subject: (.*)$/);
687 close $fh;
688 return "GIT: $1\n";
689 }
690 close $fh;
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100691 die sprintf(__("No subject line in %s?"), $fn);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100692}
693
694if ($compose) {
695 # Note that this does not need to be secure, but we will make a small
696 # effort to have it be unique
Jay Soffianafe756c2009-02-23 13:51:37 -0500697 $compose_filename = ($repo ?
698 tempfile(".gitsendemail.msg.XXXXXX", DIR => $repo->repo_path()) :
699 tempfile(".gitsendemail.msg.XXXXXX", DIR => "."))[1];
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000700 open my $c, ">", $compose_filename
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100701 or die sprintf(__("Failed to open for writing %s: %s"), $compose_filename, $!);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100702
703
704 my $tpl_sender = $sender || $repoauthor || $repocommitter || '';
705 my $tpl_subject = $initial_subject || '';
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100706 my $tpl_in_reply_to = $initial_in_reply_to || '';
Christian Ludwigd11c9432018-03-04 00:58:14 +0100707 my $tpl_reply_to = $reply_to || '';
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100708
Vasco Almeida70aedfb2016-12-14 11:54:38 -0100709 print $c <<EOT1, Git::prefix_lines("GIT: ", __ <<EOT2), <<EOT3;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100710From $tpl_sender # This line is ignored.
Vasco Almeida70aedfb2016-12-14 11:54:38 -0100711EOT1
712Lines beginning in "GIT:" will be removed.
713Consider including an overall diffstat or table of contents
714for the patch you are writing.
715
716Clear the body content if you don't wish to send a summary.
717EOT2
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100718From: $tpl_sender
Christian Ludwigd11c9432018-03-04 00:58:14 +0100719Reply-To: $tpl_reply_to
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100720Subject: $tpl_subject
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100721In-Reply-To: $tpl_in_reply_to
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100722
Vasco Almeida70aedfb2016-12-14 11:54:38 -0100723EOT3
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100724 for my $f (@files) {
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000725 print $c get_patch_subject($f);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100726 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000727 close $c;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100728
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100729 if ($annotate) {
730 do_edit($compose_filename, @files);
731 } else {
732 do_edit($compose_filename);
733 }
734
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000735 open $c, "<", $compose_filename
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100736 or die sprintf(__("Failed to open %s: %s"), $compose_filename, $!);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100737
Krzysztof Mazur4a47a4d2012-10-22 14:41:48 +0200738 if (!defined $compose_encoding) {
739 $compose_encoding = "UTF-8";
740 }
Nathan Payreb6049542017-12-15 16:33:39 +0100741
742 my %parsed_email;
743 while (my $line = <$c>) {
744 next if $line =~ m/^GIT:/;
745 parse_header_line($line, \%parsed_email);
746 if ($line =~ /^$/) {
747 $parsed_email{'body'} = filter_body($c);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100748 }
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100749 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000750 close $c;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100751
Nathan Payreb6049542017-12-15 16:33:39 +0100752 open my $c2, ">", $compose_filename . ".final"
753 or die sprintf(__("Failed to open %s.final: %s"), $compose_filename, $!);
754
755
756 if ($parsed_email{'From'}) {
757 $sender = delete($parsed_email{'From'});
758 }
759 if ($parsed_email{'In-Reply-To'}) {
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100760 $initial_in_reply_to = delete($parsed_email{'In-Reply-To'});
Nathan Payreb6049542017-12-15 16:33:39 +0100761 }
Christian Ludwigd11c9432018-03-04 00:58:14 +0100762 if ($parsed_email{'Reply-To'}) {
763 $reply_to = delete($parsed_email{'Reply-To'});
Nathan Payreb6049542017-12-15 16:33:39 +0100764 }
765 if ($parsed_email{'Subject'}) {
766 $initial_subject = delete($parsed_email{'Subject'});
767 print $c2 "Subject: " .
768 quote_subject($initial_subject, $compose_encoding) .
769 "\n";
770 }
771
772 if ($parsed_email{'MIME-Version'}) {
773 print $c2 "MIME-Version: $parsed_email{'MIME-Version'}\n",
774 "Content-Type: $parsed_email{'Content-Type'};\n",
775 "Content-Transfer-Encoding: $parsed_email{'Content-Transfer-Encoding'}\n";
776 delete($parsed_email{'MIME-Version'});
777 delete($parsed_email{'Content-Type'});
778 delete($parsed_email{'Content-Transfer-Encoding'});
779 } elsif (file_has_nonascii($compose_filename)) {
780 my $content_type = (delete($parsed_email{'Content-Type'}) or
781 "text/plain; charset=$compose_encoding");
782 print $c2 "MIME-Version: 1.0\n",
783 "Content-Type: $content_type\n",
784 "Content-Transfer-Encoding: 8bit\n";
785 }
786 # Preserve unknown headers
787 foreach my $key (keys %parsed_email) {
788 next if $key eq 'body';
789 print $c2 "$key: $parsed_email{$key}";
790 }
791
792 if ($parsed_email{'body'}) {
793 print $c2 "\n$parsed_email{'body'}\n";
794 delete($parsed_email{'body'});
795 } else {
Vasco Almeida46493102016-12-14 11:54:36 -0100796 print __("Summary email is empty, skipping it\n");
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100797 $compose = -1;
798 }
Nathan Payreb6049542017-12-15 16:33:39 +0100799
800 close $c2;
801
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100802} elsif ($annotate) {
803 do_edit(@files);
804}
805
Jay Soffian6e182512009-03-28 21:39:10 -0400806sub ask {
807 my ($prompt, %arg) = @_;
Jay Soffian0da43a62009-04-04 23:23:21 -0400808 my $valid_re = $arg{valid_re};
Jay Soffian6e182512009-03-28 21:39:10 -0400809 my $default = $arg{default};
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700810 my $confirm_only = $arg{confirm_only};
Jay Soffian6e182512009-03-28 21:39:10 -0400811 my $resp;
812 my $i = 0;
Jay Soffian5906f542009-03-31 12:22:11 -0400813 return defined $default ? $default : undef
814 unless defined $term->IN and defined fileno($term->IN) and
815 defined $term->OUT and defined fileno($term->OUT);
Jay Soffian6e182512009-03-28 21:39:10 -0400816 while ($i++ < 10) {
817 $resp = $term->readline($prompt);
818 if (!defined $resp) { # EOF
819 print "\n";
820 return defined $default ? $default : undef;
821 }
822 if ($resp eq '' and defined $default) {
823 return $default;
824 }
Jay Soffian0da43a62009-04-04 23:23:21 -0400825 if (!defined $valid_re or $resp =~ /$valid_re/) {
Jay Soffian6e182512009-03-28 21:39:10 -0400826 return $resp;
827 }
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700828 if ($confirm_only) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100829 my $yesno = $term->readline(
830 # TRANSLATORS: please keep [y/N] as is.
831 sprintf(__("Are you sure you want to use <%s> [y/N]? "), $resp));
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700832 if (defined $yesno && $yesno =~ /y/i) {
833 return $resp;
834 }
835 }
Jay Soffian6e182512009-03-28 21:39:10 -0400836 }
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -0700837 return;
Jay Soffian6e182512009-03-28 21:39:10 -0400838}
839
Nathan Payreb6049542017-12-15 16:33:39 +0100840sub parse_header_line {
841 my $lines = shift;
842 my $parsed_line = shift;
843 my $addr_pat = join "|", qw(To Cc Bcc);
844
845 foreach (split(/\n/, $lines)) {
846 if (/^($addr_pat):\s*(.+)$/i) {
847 $parsed_line->{$1} = [ parse_address_line($2) ];
848 } elsif (/^([^:]*):\s*(.+)\s*$/i) {
849 $parsed_line->{$1} = $2;
850 }
851 }
852}
853
854sub filter_body {
855 my $c = shift;
856 my $body = "";
857 while (my $body_line = <$c>) {
858 if ($body_line !~ m/^GIT:/) {
859 $body .= $body_line;
860 }
861 }
862 return $body;
863}
864
865
Thomas Rast3cae7e52010-06-17 22:10:39 +0200866my %broken_encoding;
867
Ævar Arnfjörð Bjarmason1d50bfd2010-09-30 13:42:58 +0000868sub file_declares_8bit_cte {
Thomas Rast3cae7e52010-06-17 22:10:39 +0200869 my $fn = shift;
870 open (my $fh, '<', $fn);
871 while (my $line = <$fh>) {
872 last if ($line =~ /^$/);
873 return 1 if ($line =~ /^Content-Transfer-Encoding: .*8bit.*$/);
874 }
875 close $fh;
876 return 0;
877}
878
879foreach my $f (@files) {
880 next unless (body_or_subject_has_nonascii($f)
881 && !file_declares_8bit_cte($f));
882 $broken_encoding{$f} = 1;
883}
884
885if (!defined $auto_8bit_encoding && scalar %broken_encoding) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100886 print __("The following files are 8bit, but do not declare " .
887 "a Content-Transfer-Encoding.\n");
Thomas Rast3cae7e52010-06-17 22:10:39 +0200888 foreach my $f (sort keys %broken_encoding) {
889 print " $f\n";
890 }
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100891 $auto_8bit_encoding = ask(__("Which 8bit encoding should I declare [UTF-8]? "),
Junio C Hamano852a15d2015-02-13 12:20:25 -0800892 valid_re => qr/.{4}/, confirm_only => 1,
Thomas Rast3cae7e52010-06-17 22:10:39 +0200893 default => "UTF-8");
894}
895
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200896if (!$force) {
897 for my $f (@files) {
Ævar Arnfjörð Bjarmason0d290a42010-09-30 13:43:01 +0000898 if (get_patch_subject($f) =~ /\Q*** SUBJECT HERE ***\E/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100899 die sprintf(__("Refusing to send because the patch\n\t%s\n"
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200900 . "has the template subject '*** SUBJECT HERE ***'. "
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100901 . "Pass --force if you really want to send.\n"), $f);
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200902 }
903 }
904}
905
Remi Lespinetc46e27a2015-06-30 14:16:47 +0200906if (defined $sender) {
Remi Lespinetfa5b1aa2015-06-30 14:16:51 +0200907 $sender =~ s/^\s+|\s+$//g;
Remi Lespinetc46e27a2015-06-30 14:16:47 +0200908 ($sender) = expand_aliases($sender);
909} else {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100910 $sender = $repoauthor || $repocommitter || '';
Ryan Anderson83b24432005-07-31 04:17:25 -0400911}
912
Michael S. Tsirkinda187592013-06-05 21:11:00 +0300913# $sender could be an already sanitized address
914# (e.g. sendemail.from could be manually sanitized by user).
915# But it's a no-op to run sanitize_address on an already sanitized address.
916$sender = sanitize_address($sender);
917
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100918my $to_whom = __("To whom should the emails be sent (if anyone)?");
Felipe Contreras8cac13d2012-11-24 12:16:19 +0100919my $prompting = 0;
Junio C Hamano8796ff72010-10-26 22:02:03 -0700920if (!@initial_to && !defined $to_cmd) {
Junio C Hamano0d6b21e2016-04-24 12:31:44 -0700921 my $to = ask("$to_whom ",
Stephen Boyd61837492012-09-06 11:31:11 -0700922 default => "",
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700923 valid_re => qr/\@.*\./, confirm_only => 1);
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700924 push @initial_to, parse_address_line($to) if defined $to; # sanitized/validated later
Ryan Anderson1f038a02005-09-05 01:13:07 -0400925 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -0400926}
927
Eric Wong994d6c62006-05-14 19:13:44 -0700928sub expand_aliases {
Jeff King302e04e2009-07-23 07:09:29 -0400929 return map { expand_one_alias($_) } @_;
930}
931
932my %EXPANDED_ALIASES;
933sub expand_one_alias {
934 my $alias = shift;
935 if ($EXPANDED_ALIASES{$alias}) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100936 die sprintf(__("fatal: alias '%s' expands to itself\n"), $alias);
Jeff King302e04e2009-07-23 07:09:29 -0400937 }
938 local $EXPANDED_ALIASES{$alias} = 1;
939 return $aliases{$alias} ? expand_aliases(@{$aliases{$alias}}) : $alias;
Eric Wong994d6c62006-05-14 19:13:44 -0700940}
941
Remi Lespinetb5e112d2015-06-30 14:16:45 +0200942@initial_to = process_address_list(@initial_to);
943@initial_cc = process_address_list(@initial_cc);
944@bcclist = process_address_list(@bcclist);
Eric Wong994d6c62006-05-14 19:13:44 -0700945
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100946if ($thread && !defined $initial_in_reply_to && $prompting) {
947 $initial_in_reply_to = ask(
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100948 __("Message-ID to be used as In-Reply-To for the first email (if any)? "),
Stephen Boyd61837492012-09-06 11:31:11 -0700949 default => "",
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700950 valid_re => qr/\@.*\./, confirm_only => 1);
Ryan Anderson83b24432005-07-31 04:17:25 -0400951}
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100952if (defined $initial_in_reply_to) {
953 $initial_in_reply_to =~ s/^\s*<?//;
954 $initial_in_reply_to =~ s/>?\s*$//;
955 $initial_in_reply_to = "<$initial_in_reply_to>" if $initial_in_reply_to ne '';
Junio C Hamanoace9c2a2007-12-10 21:44:42 -0800956}
Mike Hommeyace72082007-12-09 18:17:28 +0100957
Christian Ludwigd11c9432018-03-04 00:58:14 +0100958if (defined $reply_to) {
959 $reply_to =~ s/^\s+|\s+$//g;
960 ($reply_to) = expand_aliases($reply_to);
961 $reply_to = sanitize_address($reply_to);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900962}
Eric Wongaca7ad72006-05-15 02:34:44 -0700963
964if (!defined $smtp_server) {
Florian Klink1ab2fd42017-11-28 01:49:04 +0100965 my @sendmail_paths = qw( /usr/sbin/sendmail /usr/lib/sendmail );
966 push @sendmail_paths, map {"$_/sendmail"} split /:/, $ENV{PATH};
967 foreach (@sendmail_paths) {
Eric Wongaca7ad72006-05-15 02:34:44 -0700968 if (-x $_) {
969 $smtp_server = $_;
970 last;
971 }
972 }
973 $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
Ryan Anderson3342d852005-07-31 20:04:24 -0400974}
975
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500976if ($compose && $compose > 0) {
977 @files = ($compose_filename . ".final", @files);
Ryan Anderson1f038a02005-09-05 01:13:07 -0400978}
979
Ryan Anderson83b24432005-07-31 04:17:25 -0400980# Variables we set as part of the loop over files
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100981our ($message_id, %mail, $subject, $in_reply_to, $references, $message,
Jay Soffiandc1460a2009-03-31 12:22:12 -0400982 $needs_confirm, $message_num, $ask_default);
Ryan Anderson83b24432005-07-31 04:17:25 -0400983
Eric Wong567ffeb2006-03-25 16:47:12 -0800984sub extract_valid_address {
985 my $address = shift;
Ævar Arnfjörð Bjarmason35b6ab92010-09-30 19:03:31 +0000986 my $local_part_regexp = qr/[^<>"\s@]+/;
987 my $domain_regexp = qr/[^.<>"\s@]+(?:\.[^.<>"\s@]+)+/;
Eric Wongdb3106b2006-05-15 02:41:01 -0700988
989 # check for a local address:
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700990 return $address if ($address =~ /^($local_part_regexp)$/);
Eric Wongdb3106b2006-05-15 02:41:01 -0700991
Uwe Kleine-König155197e2007-08-09 15:27:57 +0200992 $address =~ s/^\s*<(.*)>\s*$/$1/;
Eric Wong567ffeb2006-03-25 16:47:12 -0800993 if ($have_email_valid) {
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700994 return scalar Email::Valid->address($address);
Eric Wong567ffeb2006-03-25 16:47:12 -0800995 }
Krzysztof Mazur95c0d4b2012-11-22 19:12:09 +0100996
997 # less robust/correct than the monster regexp in Email::Valid,
998 # but still does a 99% job, and one less dependency
999 return $1 if $address =~ /($local_part_regexp\@$domain_regexp)/;
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -07001000 return;
Eric Wong567ffeb2006-03-25 16:47:12 -08001001}
Ryan Anderson83b24432005-07-31 04:17:25 -04001002
Krzysztof Mazure4312252012-11-22 19:12:10 +01001003sub extract_valid_address_or_die {
1004 my $address = shift;
1005 $address = extract_valid_address($address);
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001006 die sprintf(__("error: unable to extract a valid address from: %s\n"), $address)
Krzysztof Mazure4312252012-11-22 19:12:10 +01001007 if !$address;
1008 return $address;
1009}
1010
1011sub validate_address {
1012 my $address = shift;
Krzysztof Mazurd0e98102012-11-22 19:12:12 +01001013 while (!extract_valid_address($address)) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001014 printf STDERR __("error: unable to extract a valid address from: %s\n"), $address;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001015 # TRANSLATORS: Make sure to include [q] [d] [e] in your
1016 # translation. The program will only accept English input
1017 # at this point.
1018 $_ = ask(__("What to do with this address? ([q]uit|[d]rop|[e]dit): "),
Krzysztof Mazurd0e98102012-11-22 19:12:12 +01001019 valid_re => qr/^(?:quit|q|drop|d|edit|e)/i,
Krzysztof Mazur5c80afe2012-11-22 19:12:11 +01001020 default => 'q');
1021 if (/^d/i) {
1022 return undef;
1023 } elsif (/^q/i) {
1024 cleanup_compose_files();
1025 exit(0);
1026 }
Junio C Hamano0d6b21e2016-04-24 12:31:44 -07001027 $address = ask("$to_whom ",
Krzysztof Mazurd0e98102012-11-22 19:12:12 +01001028 default => "",
1029 valid_re => qr/\@.*\./, confirm_only => 1);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001030 }
1031 return $address;
1032}
1033
1034sub validate_address_list {
1035 return (grep { defined $_ }
1036 map { validate_address($_) } @_);
Ryan Anderson83b24432005-07-31 04:17:25 -04001037}
1038
1039# Usually don't need to change anything below here.
1040
1041# we make a "fake" message id by taking the current number
1042# of seconds since the beginning of Unix time and tacking on
1043# a random number to the end, in case we are called quicker than
1044# 1 second since the last time we were called.
Ryan Anderson8037d1a2005-07-31 20:04:24 -04001045
1046# We'll setup a template for the message id, using the "from" address:
Ryan Anderson8037d1a2005-07-31 20:04:24 -04001047
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001048my ($message_id_stamp, $message_id_serial);
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001049sub make_message_id {
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001050 my $uniq;
1051 if (!defined $message_id_stamp) {
Eric Wongf916ab02016-04-06 20:07:14 +00001052 $message_id_stamp = strftime("%Y%m%d%H%M%S.$$", gmtime(time));
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001053 $message_id_serial = 0;
1054 }
1055 $message_id_serial++;
1056 $uniq = "$message_id_stamp-$message_id_serial";
1057
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001058 my $du_part;
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001059 for ($sender, $repocommitter, $repoauthor) {
1060 $du_part = extract_valid_address(sanitize_address($_));
1061 last if (defined $du_part and $du_part ne '');
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001062 }
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001063 if (not defined $du_part or $du_part eq '') {
Ævar Arnfjörð Bjarmason529dd382010-09-30 13:43:08 +00001064 require Sys::Hostname;
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001065 $du_part = 'user@' . Sys::Hostname::hostname();
1066 }
Eric Wongf916ab02016-04-06 20:07:14 +00001067 my $message_id_template = "<%s-%s>";
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001068 $message_id = sprintf($message_id_template, $uniq, $du_part);
Ryan Anderson8037d1a2005-07-31 20:04:24 -04001069 #print "new message id = $message_id\n"; # Was useful for debugging
Ryan Anderson83b24432005-07-31 04:17:25 -04001070}
1071
1072
1073
Eric Wonga5370b12006-03-25 03:01:01 -08001074$time = time - scalar $#files;
Ryan Anderson83b24432005-07-31 04:17:25 -04001075
Jürgen Rühle374c5902007-01-10 13:36:39 -08001076sub unquote_rfc2047 {
1077 local ($_) = @_;
Роман Донченко11f70a72014-12-14 18:59:46 +03001078 my $charset;
Роман Донченкоab47e2a2014-12-14 18:59:47 +03001079 my $sep = qr/[ \t]+/;
1080 s{$re_encoded_word(?:$sep$re_encoded_word)*}{
1081 my @words = split $sep, $&;
1082 foreach (@words) {
1083 m/$re_encoded_word/;
1084 $charset = $1;
1085 my $encoding = $2;
1086 my $text = $3;
1087 if ($encoding eq 'q' || $encoding eq 'Q') {
1088 $_ = $text;
1089 s/_/ /g;
1090 s/=([0-9A-F]{2})/chr(hex($1))/egi;
1091 } else {
1092 # other encodings not supported yet
1093 }
Роман Донченко11f70a72014-12-14 18:59:46 +03001094 }
Роман Донченкоab47e2a2014-12-14 18:59:47 +03001095 join '', @words;
Thomas Rastb622d4d2012-07-30 21:25:40 +02001096 }eg;
Роман Донченко11f70a72014-12-14 18:59:46 +03001097 return wantarray ? ($_, $charset) : $_;
Jürgen Rühle374c5902007-01-10 13:36:39 -08001098}
1099
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001100sub quote_rfc2047 {
1101 local $_ = shift;
Brandon Caseyd1fff6f2009-06-06 20:12:31 -05001102 my $encoding = shift || 'UTF-8';
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001103 s/([^-a-zA-Z0-9!*+\/])/sprintf("=%02X", ord($1))/eg;
1104 s/(.*)/=\?$encoding\?q\?$1\?=/;
1105 return $_;
1106}
1107
Brandon Caseya3a82622009-06-07 19:25:58 -05001108sub is_rfc2047_quoted {
1109 my $s = shift;
Brandon Caseya3a82622009-06-07 19:25:58 -05001110 length($s) <= 75 &&
Роман Донченко11f70a72014-12-14 18:59:46 +03001111 $s =~ m/^(?:"[[:ascii:]]*"|$re_encoded_word)$/o;
Brandon Caseya3a82622009-06-07 19:25:58 -05001112}
1113
Krzysztof Mazurce547802012-10-24 23:08:26 +02001114sub subject_needs_rfc2047_quoting {
1115 my $s = shift;
1116
Krzysztof Mazurce1459f2012-10-24 23:28:29 +02001117 return ($s =~ /[^[:ascii:]]/) || ($s =~ /=\?/);
Krzysztof Mazurce547802012-10-24 23:08:26 +02001118}
1119
1120sub quote_subject {
1121 local $subject = shift;
1122 my $encoding = shift || 'UTF-8';
1123
1124 if (subject_needs_rfc2047_quoting($subject)) {
1125 return quote_rfc2047($subject, $encoding);
1126 }
1127 return $subject;
1128}
1129
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001130# use the simplest quoting being able to handle the recipient
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001131sub sanitize_address {
Robin H. Johnson732263d2007-04-25 19:37:19 -07001132 my ($recipient) = @_;
Krzysztof Mazur831a4882012-11-22 19:12:08 +01001133
1134 # remove garbage after email address
1135 $recipient =~ s/(.*>).*$/$1/;
1136
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001137 my ($recipient_name, $recipient_addr) = ($recipient =~ /^(.*?)\s*(<.*)/);
1138
1139 if (not $recipient_name) {
Ævar Arnfjörð Bjarmasonff483892010-09-30 13:43:02 +00001140 return $recipient;
Robin H. Johnson732263d2007-04-25 19:37:19 -07001141 }
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001142
1143 # if recipient_name is already quoted, do nothing
Brandon Caseya3a82622009-06-07 19:25:58 -05001144 if (is_rfc2047_quoted($recipient_name)) {
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001145 return $recipient;
1146 }
1147
Remi Lespinet1fe97032015-06-30 14:16:49 +02001148 # remove non-escaped quotes
1149 $recipient_name =~ s/(^|[^\\])"/$1/g;
1150
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001151 # rfc2047 is needed if a non-ascii char is included
1152 if ($recipient_name =~ /[^[:ascii:]]/) {
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001153 $recipient_name = quote_rfc2047($recipient_name);
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001154 }
1155
1156 # double quotes are needed if specials or CTLs are included
1157 elsif ($recipient_name =~ /[][()<>@,;:\\".\000-\037\177]/) {
Remi Lespinet1fe97032015-06-30 14:16:49 +02001158 $recipient_name =~ s/([\\\r])/\\$1/g;
Ævar Arnfjörð Bjarmasond5c7d692010-09-30 13:43:03 +00001159 $recipient_name = qq["$recipient_name"];
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001160 }
1161
1162 return "$recipient_name $recipient_addr";
1163
Robin H. Johnson732263d2007-04-25 19:37:19 -07001164}
1165
Matthieu Moycb2922f2017-08-23 12:21:01 +02001166sub strip_garbage_one_address {
1167 my ($addr) = @_;
1168 chomp $addr;
1169 if ($addr =~ /^(("[^"]*"|[^"<]*)? *<[^>]*>).*/) {
1170 # "Foo Bar" <foobar@example.com> [possibly garbage here]
1171 # Foo Bar <foobar@example.com> [possibly garbage here]
1172 return $1;
1173 }
1174 if ($addr =~ /^(<[^>]*>).*/) {
1175 # <foo@example.com> [possibly garbage here]
1176 # if garbage contains other addresses, they are ignored.
1177 return $1;
1178 }
1179 if ($addr =~ /^([^"#,\s]*)/) {
1180 # address without quoting: remove anything after the address
1181 return $1;
1182 }
1183 return $addr;
1184}
1185
Krzysztof Mazure4312252012-11-22 19:12:10 +01001186sub sanitize_address_list {
1187 return (map { sanitize_address($_) } @_);
1188}
1189
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001190sub process_address_list {
Remi Lespinetb1c8a112015-06-30 14:16:50 +02001191 my @addr_list = map { parse_address_line($_) } @_;
1192 @addr_list = expand_aliases(@addr_list);
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001193 @addr_list = sanitize_address_list(@addr_list);
1194 @addr_list = validate_address_list(@addr_list);
1195 return @addr_list;
1196}
1197
Jari Aalto134550f2010-03-14 17:16:45 +02001198# Returns the local Fully Qualified Domain Name (FQDN) if available.
1199#
1200# Tightly configured MTAa require that a caller sends a real DNS
1201# domain name that corresponds the IP address in the HELO/EHLO
1202# handshake. This is used to verify the connection and prevent
1203# spammers from trying to hide their identity. If the DNS and IP don't
1204# match, the receiveing MTA may deny the connection.
1205#
1206# Here is a deny example of Net::SMTP with the default "localhost.localdomain"
1207#
1208# Net::SMTP=GLOB(0x267ec28)>>> EHLO localhost.localdomain
1209# Net::SMTP=GLOB(0x267ec28)<<< 550 EHLO argument does not match calling host
1210#
1211# This maildomain*() code is based on ideas in Perl library Test::Reporter
1212# /usr/share/perl5/Test/Reporter/Mail/Util.pm ==> sub _maildomain ()
1213
Brian Gernhardt59a86302010-04-10 10:53:54 -04001214sub valid_fqdn {
1215 my $domain = shift;
Brandon Casey61ef5e92010-09-26 22:18:01 -05001216 return defined $domain && !($^O eq 'darwin' && $domain =~ /\.local$/) && $domain =~ /\./;
Brian Gernhardt59a86302010-04-10 10:53:54 -04001217}
1218
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001219sub maildomain_net {
Jari Aalto134550f2010-03-14 17:16:45 +02001220 my $maildomain;
1221
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001222 my $domain = Net::Domain::domainname();
1223 $maildomain = $domain if valid_fqdn($domain);
Jari Aalto134550f2010-03-14 17:16:45 +02001224
1225 return $maildomain;
1226}
1227
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001228sub maildomain_mta {
Jari Aalto134550f2010-03-14 17:16:45 +02001229 my $maildomain;
1230
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001231 for my $host (qw(mailhost localhost)) {
1232 my $smtp = Net::SMTP->new($host);
1233 if (defined $smtp) {
1234 my $domain = $smtp->domain;
1235 $smtp->quit;
Jari Aalto134550f2010-03-14 17:16:45 +02001236
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001237 $maildomain = $domain if valid_fqdn($domain);
Jari Aalto134550f2010-03-14 17:16:45 +02001238
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001239 last if $maildomain;
Jari Aalto134550f2010-03-14 17:16:45 +02001240 }
1241 }
1242
1243 return $maildomain;
1244}
1245
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001246sub maildomain {
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001247 return maildomain_net() || maildomain_mta() || 'localhost.localdomain';
Jari Aalto134550f2010-03-14 17:16:45 +02001248}
1249
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001250sub smtp_host_string {
1251 if (defined $smtp_server_port) {
1252 return "$smtp_server:$smtp_server_port";
1253 } else {
1254 return $smtp_server;
1255 }
1256}
1257
1258# Returns 1 if authentication succeeded or was not necessary
1259# (smtp_user was not specified), and 0 otherwise.
1260
1261sub smtp_auth_maybe {
Joshua Watt8dd9b3f2018-10-22 22:24:25 -05001262 if (!defined $smtp_authuser || $auth || (defined $smtp_auth && $smtp_auth eq "none")) {
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001263 return 1;
1264 }
1265
1266 # Workaround AUTH PLAIN/LOGIN interaction defect
1267 # with Authen::SASL::Cyrus
1268 eval {
1269 require Authen::SASL;
1270 Authen::SASL->import(qw(Perl));
1271 };
1272
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001273 # Check mechanism naming as defined in:
1274 # https://tools.ietf.org/html/rfc4422#page-8
Brian Norris904f6e72015-09-18 15:12:50 -07001275 if ($smtp_auth && $smtp_auth !~ /^(\b[A-Z0-9-_]{1,20}\s*)*$/) {
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001276 die "invalid smtp auth: '${smtp_auth}'";
1277 }
1278
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001279 # TODO: Authentication may fail not because credentials were
1280 # invalid but due to other reasons, in which we should not
1281 # reject credentials.
1282 $auth = Git::credential({
1283 'protocol' => 'smtp',
1284 'host' => smtp_host_string(),
1285 'username' => $smtp_authuser,
1286 # if there's no password, "git credential fill" will
1287 # give us one, otherwise it'll just pass this one.
1288 'password' => $smtp_authpass
1289 }, sub {
1290 my $cred = shift;
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001291
1292 if ($smtp_auth) {
1293 my $sasl = Authen::SASL->new(
1294 mechanism => $smtp_auth,
1295 callback => {
1296 user => $cred->{'username'},
1297 pass => $cred->{'password'},
1298 authname => $cred->{'username'},
1299 }
1300 );
1301
1302 return !!$smtp->auth($sasl);
1303 }
1304
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001305 return !!$smtp->auth($cred->{'username'}, $cred->{'password'});
1306 });
1307
1308 return $auth;
1309}
1310
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001311sub ssl_verify_params {
1312 eval {
1313 require IO::Socket::SSL;
1314 IO::Socket::SSL->import(qw/SSL_VERIFY_PEER SSL_VERIFY_NONE/);
1315 };
1316 if ($@) {
1317 print STDERR "Not using SSL_VERIFY_PEER due to out-of-date IO::Socket::SSL.\n";
1318 return;
1319 }
1320
1321 if (!defined $smtp_ssl_cert_path) {
Ruben Kerkhof01645b72014-01-15 21:31:11 +04001322 # use the OpenSSL defaults
1323 return (SSL_verify_mode => SSL_VERIFY_PEER());
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001324 }
1325
1326 if ($smtp_ssl_cert_path eq "") {
1327 return (SSL_verify_mode => SSL_VERIFY_NONE());
1328 } elsif (-d $smtp_ssl_cert_path) {
1329 return (SSL_verify_mode => SSL_VERIFY_PEER(),
1330 SSL_ca_path => $smtp_ssl_cert_path);
1331 } elsif (-f $smtp_ssl_cert_path) {
1332 return (SSL_verify_mode => SSL_VERIFY_PEER(),
1333 SSL_ca_file => $smtp_ssl_cert_path);
1334 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001335 die sprintf(__("CA path \"%s\" does not exist"), $smtp_ssl_cert_path);
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001336 }
1337}
1338
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001339sub file_name_is_absolute {
1340 my ($path) = @_;
1341
1342 # msys does not grok DOS drive-prefixes
1343 if ($^O eq 'msys') {
Junio C Hamanof24ecf52014-04-23 09:37:38 -07001344 return ($path =~ m#^/# || $path =~ m#^[a-zA-Z]\:#)
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001345 }
1346
1347 require File::Spec::Functions;
1348 return File::Spec::Functions::file_name_is_absolute($path);
1349}
1350
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001351# Prepares the email, then asks the user what to do.
1352#
1353# If the user chooses to send the email, it's sent and 1 is returned.
1354# If the user chooses not to send the email, 0 is returned.
1355# If the user decides they want to make further edits, -1 is returned and the
1356# caller is expected to call send_message again after the edits are performed.
1357#
1358# If an error occurs sending the email, this just dies.
Michael Witten15da1082009-04-13 13:23:51 -05001359
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001360sub send_message {
Eric Wong4bc87a22006-03-25 17:20:48 -08001361 my @recipients = unique_email_list(@to);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001362 @cc = (grep { my $cc = extract_valid_address_or_die($_);
Joe Perches83acaae2010-11-20 15:06:05 -08001363 not grep { $cc eq $_ || $_ =~ /<\Q${cc}\E>$/ } @recipients
Ask Bjørn Hansen7ac17522007-11-19 03:00:26 -08001364 }
Ask Bjørn Hansen7ac17522007-11-19 03:00:26 -08001365 @cc);
Eric Wong4bc87a22006-03-25 17:20:48 -08001366 my $to = join (",\n\t", @recipients);
Ryan Anderson58063242006-05-29 12:30:13 -07001367 @recipients = unique_email_list(@recipients,@cc,@bcclist);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001368 @recipients = (map { extract_valid_address_or_die($_) } @recipients);
Jakub Narebski6bdca892006-07-07 20:57:55 +02001369 my $date = format_2822_time($time++);
Martin Langhoffe923eff2006-05-03 09:44:36 +12001370 my $gitversion = '@@GIT_VERSION@@';
1371 if ($gitversion =~ m/..GIT_VERSION../) {
Petr Baudis3cb8caf2006-07-03 22:47:58 +02001372 $gitversion = Git::version();
Martin Langhoffe923eff2006-05-03 09:44:36 +12001373 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001374
Joe Perches02461e02009-10-08 10:03:26 -07001375 my $cc = join(",\n\t", unique_email_list(@cc));
Junio C Hamanof06a6a42007-04-16 16:51:47 -07001376 my $ccline = "";
1377 if ($cc ne '') {
1378 $ccline = "\nCc: $cc";
1379 }
Jeff King4f3d3702007-12-17 15:51:34 -05001380 make_message_id() unless defined($message_id);
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001381
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001382 my $header = "From: $sender
Junio C Hamanof06a6a42007-04-16 16:51:47 -07001383To: $to${ccline}
Eric Wong4bc87a22006-03-25 17:20:48 -08001384Subject: $subject
Eric Wong4bc87a22006-03-25 17:20:48 -08001385Date: $date
1386Message-Id: $message_id
Eric Wong4bc87a22006-03-25 17:20:48 -08001387";
Luis Henriquesac1596a2014-03-24 21:38:27 +00001388 if ($use_xmailer) {
1389 $header .= "X-Mailer: git-send-email $gitversion\n";
1390 }
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001391 if ($in_reply_to) {
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001392
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001393 $header .= "In-Reply-To: $in_reply_to\n";
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001394 $header .= "References: $references\n";
1395 }
Christian Ludwigd11c9432018-03-04 00:58:14 +01001396 if ($reply_to) {
1397 $header .= "Reply-To: $reply_to\n";
1398 }
Junio C Hamanoce91c2f2006-10-05 16:36:49 -07001399 if (@xh) {
1400 $header .= join("\n", @xh) . "\n";
1401 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001402
Robin H. Johnsonc38f0242007-04-25 19:37:20 -07001403 my @sendmail_parameters = ('-i', @recipients);
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001404 my $raw_from = $sender;
Felipe Contrerasc89e3242009-11-26 21:04:29 +02001405 if (defined $envelope_sender && $envelope_sender ne "auto") {
1406 $raw_from = $envelope_sender;
1407 }
Robin H. Johnsonf073a592007-04-25 19:37:22 -07001408 $raw_from = extract_valid_address($raw_from);
1409 unshift (@sendmail_parameters,
1410 '-f', $raw_from) if(defined $envelope_sender);
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001411
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001412 if ($needs_confirm && !$dry_run) {
1413 print "\n$header\n";
1414 if ($needs_confirm eq "inform") {
1415 $confirm_unconfigured = 0; # squelch this message for the rest of this run
Jay Soffian6e182512009-03-28 21:39:10 -04001416 $ask_default = "y"; # assume yes on EOF since user hasn't explicitly asked for confirmation
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001417 print __ <<EOF ;
1418 The Cc list above has been expanded by additional
1419 addresses found in the patch commit message. By default
1420 send-email prompts before sending whenever this occurs.
1421 This behavior is controlled by the sendemail.confirm
1422 configuration setting.
1423
1424 For additional information, run 'git send-email --help'.
1425 To retain the current behavior, but squelch this message,
1426 run 'git config --global sendemail.confirm auto'.
1427
1428EOF
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001429 }
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001430 # TRANSLATORS: Make sure to include [y] [n] [e] [q] [a] in your
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001431 # translation. The program will only accept English input
1432 # at this point.
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001433 $_ = ask(__("Send this email? ([y]es|[n]o|[e]dit|[q]uit|[a]ll): "),
1434 valid_re => qr/^(?:yes|y|no|n|edit|e|quit|q|all|a)/i,
Jay Soffian6e182512009-03-28 21:39:10 -04001435 default => $ask_default);
Vasco Almeida46493102016-12-14 11:54:36 -01001436 die __("Send this email reply required") unless defined $_;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001437 if (/^n/i) {
Michael Witten15da1082009-04-13 13:23:51 -05001438 return 0;
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001439 } elsif (/^e/i) {
1440 return -1;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001441 } elsif (/^q/i) {
1442 cleanup_compose_files();
1443 exit(0);
1444 } elsif (/^a/i) {
1445 $confirm = 'never';
1446 }
1447 }
1448
Pascal Obry052fbea2010-09-06 20:12:11 +02001449 unshift (@sendmail_parameters, @smtp_server_options);
1450
Matthew Wilcox61302592006-10-10 08:58:23 -06001451 if ($dry_run) {
1452 # We don't want to send the email.
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001453 } elsif (file_name_is_absolute($smtp_server)) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001454 my $pid = open my $sm, '|-';
1455 defined $pid or die $!;
1456 if (!$pid) {
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001457 exec($smtp_server, @sendmail_parameters) or die $!;
Eric Wongaca7ad72006-05-15 02:34:44 -07001458 }
1459 print $sm "$header\n$message";
Ævar Arnfjörð Bjarmason5e2c2ab2010-09-30 13:43:07 +00001460 close $sm or die $!;
Eric Wongaca7ad72006-05-15 02:34:44 -07001461 } else {
Junio C Hamano44b24762007-09-25 17:27:54 -07001462
1463 if (!defined $smtp_server) {
Vasco Almeida46493102016-12-14 11:54:36 -01001464 die __("The required SMTP server is not properly defined.")
Junio C Hamano44b24762007-09-25 17:27:54 -07001465 }
1466
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001467 require Net::SMTP;
Jonathan Niederbfbfc9a2017-05-31 17:17:43 -07001468 my $use_net_smtp_ssl = version->parse($Net::SMTP::VERSION) < version->parse("2.34");
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001469 $smtp_domain ||= maildomain();
1470
Thomas Rastf6bebd12008-06-25 21:42:43 +02001471 if ($smtp_encryption eq 'ssl') {
Junio C Hamano44b24762007-09-25 17:27:54 -07001472 $smtp_server_port ||= 465; # ssmtp
Thomas Rast5508f3e2013-12-01 23:48:43 +01001473 require IO::Socket::SSL;
John Keeping9d605242015-12-03 21:47:18 +00001474
1475 # Suppress "variable accessed once" warning.
1476 {
1477 no warnings 'once';
1478 $IO::Socket::SSL::DEBUG = 1;
1479 }
1480
Thomas Rast5508f3e2013-12-01 23:48:43 +01001481 # Net::SMTP::SSL->new() does not forward any SSL options
1482 IO::Socket::SSL::set_client_defaults(
1483 ssl_verify_params());
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001484
1485 if ($use_net_smtp_ssl) {
1486 require Net::SMTP::SSL;
1487 $smtp ||= Net::SMTP::SSL->new($smtp_server,
1488 Hello => $smtp_domain,
1489 Port => $smtp_server_port,
1490 Debug => $debug_net_smtp);
1491 }
1492 else {
1493 $smtp ||= Net::SMTP->new($smtp_server,
1494 Hello => $smtp_domain,
1495 Port => $smtp_server_port,
1496 Debug => $debug_net_smtp,
1497 SSL => 1);
1498 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001499 }
Jules Maselbas636f3d72018-07-14 10:58:48 +02001500 elsif (!$smtp) {
brian m. carlson1a741bf2013-07-04 22:04:52 +00001501 $smtp_server_port ||= 25;
1502 $smtp ||= Net::SMTP->new($smtp_server,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001503 Hello => $smtp_domain,
brian m. carlson1a741bf2013-07-04 22:04:52 +00001504 Debug => $debug_net_smtp,
1505 Port => $smtp_server_port);
Yakov Lernerfb3650e2009-09-25 15:10:21 -07001506 if ($smtp_encryption eq 'tls' && $smtp) {
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001507 if ($use_net_smtp_ssl) {
1508 $smtp->command('STARTTLS');
1509 $smtp->response();
1510 if ($smtp->code != 220) {
1511 die sprintf(__("Server does not support STARTTLS! %s"), $smtp->message);
1512 }
1513 require Net::SMTP::SSL;
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001514 $smtp = Net::SMTP::SSL->start_SSL($smtp,
1515 ssl_verify_params())
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001516 or die sprintf(__("STARTTLS failed! %s"), IO::Socket::SSL::errstr());
Thomas Rastf6bebd12008-06-25 21:42:43 +02001517 }
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001518 else {
1519 $smtp->starttls(ssl_verify_params())
1520 or die sprintf(__("STARTTLS failed! %s"), IO::Socket::SSL::errstr());
1521 }
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001522 # Send EHLO again to receive fresh
1523 # supported commands
1524 $smtp->hello($smtp_domain);
Thomas Rastf6bebd12008-06-25 21:42:43 +02001525 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001526 }
Junio C Hamano44b24762007-09-25 17:27:54 -07001527
1528 if (!$smtp) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001529 die __("Unable to initialize SMTP properly. Check config and use --smtp-debug."),
1530 " VALUES: server=$smtp_server ",
Jari Aaltoe5afb3a2010-03-14 17:15:33 +02001531 "encryption=$smtp_encryption ",
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001532 "hello=$smtp_domain",
Sylvain Rabota1dd7e12011-04-29 20:23:24 +02001533 defined $smtp_server_port ? " port=$smtp_server_port" : "";
Junio C Hamano44b24762007-09-25 17:27:54 -07001534 }
1535
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001536 smtp_auth_maybe or die $smtp->message;
Michael Witten2363d742008-02-03 19:53:56 -05001537
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001538 $smtp->mail( $raw_from ) or die $smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001539 $smtp->to( @recipients ) or die $smtp->message;
1540 $smtp->data or die $smtp->message;
Stefan Agnerf60c4832015-09-30 09:26:09 +02001541 $smtp->datasend("$header\n") or die $smtp->message;
1542 my @lines = split /^/, $message;
1543 foreach my $line (@lines) {
1544 $smtp->datasend("$line") or die $smtp->message;
1545 }
Eric Wongaca7ad72006-05-15 02:34:44 -07001546 $smtp->dataend() or die $smtp->message;
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001547 $smtp->code =~ /250|200/ or die sprintf(__("Failed to send %s\n"), $subject).$smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001548 }
Ryan Anderson27184352006-02-05 20:13:52 -05001549 if ($quiet) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001550 printf($dry_run ? __("Dry-Sent %s\n") : __("Sent %s\n"), $subject);
Ryan Anderson27184352006-02-05 20:13:52 -05001551 } else {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001552 print($dry_run ? __("Dry-OK. Log says:\n") : __("OK. Log says:\n"));
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001553 if (!file_name_is_absolute($smtp_server)) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001554 print "Server: $smtp_server\n";
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001555 print "MAIL FROM:<$raw_from>\n";
Joe Perches02461e02009-10-08 10:03:26 -07001556 foreach my $entry (@recipients) {
1557 print "RCPT TO:<$entry>\n";
1558 }
Eric Wongaca7ad72006-05-15 02:34:44 -07001559 } else {
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001560 print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001561 }
David D. Kilzerb7f30e02007-11-18 20:14:55 -08001562 print $header, "\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001563 if ($smtp) {
Vasco Almeida46493102016-12-14 11:54:36 -01001564 print __("Result: "), $smtp->code, ' ',
Eric Wongaca7ad72006-05-15 02:34:44 -07001565 ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
1566 } else {
Vasco Almeida46493102016-12-14 11:54:36 -01001567 print __("Result: OK\n");
Eric Wongaca7ad72006-05-15 02:34:44 -07001568 }
Ryan Anderson30d08b32006-02-02 11:56:06 -05001569 }
Michael Witten15da1082009-04-13 13:23:51 -05001570
1571 return 1;
Ryan Anderson83b24432005-07-31 04:17:25 -04001572}
1573
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001574$in_reply_to = $initial_in_reply_to;
1575$references = $initial_in_reply_to || '';
Ryan Anderson83b24432005-07-31 04:17:25 -04001576$subject = $initial_subject;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001577$message_num = 0;
Ryan Anderson83b24432005-07-31 04:17:25 -04001578
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001579# Prepares the email, prompts the user, sends it out
1580# Returns 0 if an edit was done and the function should be called again, or 1
1581# otherwise.
1582sub process_file {
1583 my ($t) = @_;
1584
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001585 open my $fh, "<", $t or die sprintf(__("can't open file %s"), $t);
Ryan Anderson83b24432005-07-31 04:17:25 -04001586
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001587 my $author = undef;
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001588 my $sauthor = undef;
Jeff King8291db62007-11-16 05:49:09 -05001589 my $author_encoding;
1590 my $has_content_type;
1591 my $body_encoding;
Paolo Bonzinibb294562014-11-25 15:00:26 +01001592 my $xfer_encoding;
1593 my $has_mime_version;
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001594 @to = ();
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001595 @cc = ();
Junio C Hamanoce91c2f2006-10-05 16:36:49 -07001596 @xh = ();
Junio C Hamanoe6b09642006-10-07 03:09:05 -07001597 my $input_format = undef;
Jay Soffian50126992009-02-14 23:32:14 -05001598 my @header = ();
Ryan Anderson83b24432005-07-31 04:17:25 -04001599 $message = "";
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001600 $message_num++;
Jay Soffian50126992009-02-14 23:32:14 -05001601 # First unfold multiline header fields
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001602 while(<$fh>) {
Jay Soffian50126992009-02-14 23:32:14 -05001603 last if /^\s*$/;
1604 if (/^\s+\S/ and @header) {
1605 chomp($header[$#header]);
1606 s/^\s+/ /;
1607 $header[$#header] .= $_;
1608 } else {
1609 push(@header, $_);
1610 }
1611 }
1612 # Now parse the header
1613 foreach(@header) {
1614 if (/^From /) {
1615 $input_format = 'mbox';
1616 next;
1617 }
1618 chomp;
1619 if (!defined $input_format && /^[-A-Za-z]+:\s/) {
1620 $input_format = 'mbox';
1621 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001622
Jay Soffian50126992009-02-14 23:32:14 -05001623 if (defined $input_format && $input_format eq 'mbox') {
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001624 if (/^Subject:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001625 $subject = $1;
1626 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001627 elsif (/^From:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001628 ($author, $author_encoding) = unquote_rfc2047($1);
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001629 $sauthor = sanitize_address($author);
Jay Soffian50126992009-02-14 23:32:14 -05001630 next if $suppress_cc{'author'};
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001631 next if $suppress_cc{'self'} and $sauthor eq $sender;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001632 printf(__("(mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001633 $1, $_) unless $quiet;
1634 push @cc, $1;
1635 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001636 elsif (/^To:\s+(.*)$/i) {
Stephen Boyd21802cd2010-09-29 00:26:44 -07001637 foreach my $addr (parse_address_line($1)) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001638 printf(__("(mbox) Adding to: %s from line '%s'\n"),
Stephen Boyd21802cd2010-09-29 00:26:44 -07001639 $addr, $_) unless $quiet;
Krzysztof Mazure4312252012-11-22 19:12:10 +01001640 push @to, $addr;
Stephen Boyd21802cd2010-09-29 00:26:44 -07001641 }
1642 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001643 elsif (/^Cc:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001644 foreach my $addr (parse_address_line($1)) {
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001645 my $qaddr = unquote_rfc2047($addr);
1646 my $saddr = sanitize_address($qaddr);
1647 if ($saddr eq $sender) {
David Brown65648282007-12-25 19:56:29 -08001648 next if ($suppress_cc{'self'});
David Brown65648282007-12-25 19:56:29 -08001649 } else {
1650 next if ($suppress_cc{'cc'});
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001651 }
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001652 printf(__("(mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001653 $addr, $_) unless $quiet;
1654 push @cc, $addr;
Ryan Anderson83b24432005-07-31 04:17:25 -04001655 }
1656 }
Jay Soffian50126992009-02-14 23:32:14 -05001657 elsif (/^Content-type:/i) {
1658 $has_content_type = 1;
1659 if (/charset="?([^ "]+)/) {
1660 $body_encoding = $1;
1661 }
1662 push @xh, $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001663 }
Paolo Bonzinibb294562014-11-25 15:00:26 +01001664 elsif (/^MIME-Version/i) {
1665 $has_mime_version = 1;
1666 push @xh, $_;
1667 }
Jay Soffian50126992009-02-14 23:32:14 -05001668 elsif (/^Message-Id: (.*)/i) {
1669 $message_id = $1;
1670 }
Paolo Bonzinibb294562014-11-25 15:00:26 +01001671 elsif (/^Content-Transfer-Encoding: (.*)/i) {
1672 $xfer_encoding = $1 if not defined $xfer_encoding;
1673 }
Stefan Agner256be1d2018-04-17 23:16:30 +02001674 elsif (/^In-Reply-To: (.*)/i) {
1675 $in_reply_to = $1;
1676 }
1677 elsif (/^References: (.*)/i) {
1678 $references = $1;
1679 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001680 elsif (!/^Date:\s/i && /^[-A-Za-z]+:\s+\S/) {
Jay Soffian50126992009-02-14 23:32:14 -05001681 push @xh, $_;
1682 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001683 } else {
Jay Soffian50126992009-02-14 23:32:14 -05001684 # In the traditional
1685 # "send lots of email" format,
1686 # line 1 = cc
1687 # line 2 = subject
1688 # So let's support that, too.
1689 $input_format = 'lots';
1690 if (@cc == 0 && !$suppress_cc{'cc'}) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001691 printf(__("(non-mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001692 $_, $_) unless $quiet;
1693 push @cc, $_;
1694 } elsif (!defined $subject) {
1695 $subject = $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001696 }
1697 }
1698 }
Jay Soffian50126992009-02-14 23:32:14 -05001699 # Now parse the message body
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001700 while(<$fh>) {
Jay Soffian50126992009-02-14 23:32:14 -05001701 $message .= $_;
Baruch Siach9c545812019-03-16 21:26:50 +02001702 if (/^([a-z][a-z-]*-by|Cc): (.*)/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001703 chomp;
Jay Soffian3531e272009-02-14 23:32:15 -05001704 my ($what, $c) = ($1, $2);
Matthieu Moycb2922f2017-08-23 12:21:01 +02001705 # strip garbage for the address we'll use:
1706 $c = strip_garbage_one_address($c);
1707 # sanitize a bit more to decide whether to suppress the address:
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001708 my $sc = sanitize_address($c);
1709 if ($sc eq $sender) {
Jay Soffian3531e272009-02-14 23:32:15 -05001710 next if ($suppress_cc{'self'});
1711 } else {
Rasmus Villemoesef0cc1d2018-10-16 09:39:23 +02001712 if ($what =~ /^Signed-off-by$/i) {
1713 next if $suppress_cc{'sob'};
1714 } elsif ($what =~ /-by$/i) {
1715 next if $suppress_cc{'misc-by'};
1716 } elsif ($what =~ /Cc/i) {
1717 next if $suppress_cc{'bodycc'};
1718 }
Jay Soffian3531e272009-02-14 23:32:15 -05001719 }
Rasmus Villemoesfb34fe62018-10-10 13:13:50 +02001720 if ($c !~ /.+@.+|<.+>/) {
1721 printf("(body) Ignoring %s from line '%s'\n",
1722 $what, $_) unless $quiet;
1723 next;
1724 }
Jay Soffian50126992009-02-14 23:32:14 -05001725 push @cc, $c;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001726 printf(__("(body) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001727 $c, $_) unless $quiet;
1728 }
1729 }
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001730 close $fh;
Joe Perches324a8bd2007-08-17 18:51:12 -07001731
Joe Perches6e74e072010-09-24 10:03:00 -07001732 push @to, recipients_cmd("to-cmd", "to", $to_cmd, $t)
1733 if defined $to_cmd;
1734 push @cc, recipients_cmd("cc-cmd", "cc", $cc_cmd, $t)
1735 if defined $cc_cmd && !$suppress_cc{'cccmd'};
Joe Perches324a8bd2007-08-17 18:51:12 -07001736
Thomas Rast3cae7e52010-06-17 22:10:39 +02001737 if ($broken_encoding{$t} && !$has_content_type) {
Paolo Bonzinibb294562014-11-25 15:00:26 +01001738 $xfer_encoding = '8bit' if not defined $xfer_encoding;
Thomas Rast3cae7e52010-06-17 22:10:39 +02001739 $has_content_type = 1;
Paolo Bonzinibb294562014-11-25 15:00:26 +01001740 push @xh, "Content-Type: text/plain; charset=$auto_8bit_encoding";
Thomas Rast3cae7e52010-06-17 22:10:39 +02001741 $body_encoding = $auto_8bit_encoding;
1742 }
1743
Krzysztof Mazurce547802012-10-24 23:08:26 +02001744 if ($broken_encoding{$t} && !is_rfc2047_quoted($subject)) {
1745 $subject = quote_subject($subject, $auto_8bit_encoding);
Thomas Rast3cae7e52010-06-17 22:10:39 +02001746 }
1747
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001748 if (defined $sauthor and $sauthor ne $sender) {
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001749 $message = "From: $author\n\n$message";
Jeff King8291db62007-11-16 05:49:09 -05001750 if (defined $author_encoding) {
1751 if ($has_content_type) {
1752 if ($body_encoding eq $author_encoding) {
1753 # ok, we already have the right encoding
1754 }
1755 else {
1756 # uh oh, we should re-encode
1757 }
1758 }
1759 else {
Paolo Bonzinibb294562014-11-25 15:00:26 +01001760 $xfer_encoding = '8bit' if not defined $xfer_encoding;
Thomas Rast3cae7e52010-06-17 22:10:39 +02001761 $has_content_type = 1;
Jeff King8291db62007-11-16 05:49:09 -05001762 push @xh,
Paolo Bonzinibb294562014-11-25 15:00:26 +01001763 "Content-Type: text/plain; charset=$author_encoding";
Jeff King8291db62007-11-16 05:49:09 -05001764 }
1765 }
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001766 }
brian m. carlsone67a2282018-07-08 22:17:12 +00001767 $xfer_encoding = '8bit' if not defined $xfer_encoding;
1768 ($message, $xfer_encoding) = apply_transfer_encoding(
1769 $message, $xfer_encoding, $target_xfer_encoding);
1770 push @xh, "Content-Transfer-Encoding: $xfer_encoding";
1771 unshift @xh, 'MIME-Version: 1.0' unless $has_mime_version;
Ryan Anderson83b24432005-07-31 04:17:25 -04001772
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001773 $needs_confirm = (
1774 $confirm eq "always" or
1775 ($confirm =~ /^(?:auto|cc)$/ && @cc) or
1776 ($confirm =~ /^(?:auto|compose)$/ && $compose && $message_num == 1));
1777 $needs_confirm = "inform" if ($needs_confirm && $confirm_unconfigured && @cc);
1778
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001779 @to = process_address_list(@to);
1780 @cc = process_address_list(@cc);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001781
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001782 @to = (@initial_to, @to);
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001783 @cc = (@initial_cc, @cc);
1784
Michael S. Tsirkinf515c902014-04-29 08:41:16 +03001785 if ($message_num == 1) {
1786 if (defined $cover_cc and $cover_cc) {
1787 @initial_cc = @cc;
1788 }
1789 if (defined $cover_to and $cover_to) {
1790 @initial_to = @to;
1791 }
1792 }
1793
Michael Witten15da1082009-04-13 13:23:51 -05001794 my $message_was_sent = send_message();
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001795 if ($message_was_sent == -1) {
1796 do_edit($t);
1797 return 0;
1798 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001799
1800 # set up for the next message
Junio C Hamano95a877a2009-06-12 09:23:43 -07001801 if ($thread && $message_was_sent &&
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001802 ($chain_reply_to || !defined $in_reply_to || length($in_reply_to) == 0 ||
Antonio Ospitedb54c8e2010-11-12 15:55:08 +01001803 $message_num == 1)) {
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001804 $in_reply_to = $message_id;
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001805 if (length $references > 0) {
YOSHIFUJI Hideaki / 吉藤英明a925b892007-04-06 08:50:24 +09001806 $references .= "\n $message_id";
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001807 } else {
1808 $references = "$message_id";
1809 }
Ryan Anderson78488b22005-07-31 20:04:24 -04001810 }
Jeff King4f3d3702007-12-17 15:51:34 -05001811 $message_id = undef;
xiaoqiang zhao5453b832017-05-21 20:59:50 +08001812 $num_sent++;
1813 if (defined $batch_size && $num_sent == $batch_size) {
1814 $num_sent = 0;
1815 $smtp->quit if defined $smtp;
1816 undef $smtp;
1817 undef $auth;
1818 sleep($relogin_delay) if defined $relogin_delay;
1819 }
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001820
1821 return 1;
1822}
1823
1824foreach my $t (@files) {
1825 while (!process_file($t)) {
1826 # user edited the file
1827 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001828}
Ryan Andersone2057352005-08-02 21:45:22 -04001829
Joe Perches6e74e072010-09-24 10:03:00 -07001830# Execute a command (e.g. $to_cmd) to get a list of email addresses
1831# and return a results array
1832sub recipients_cmd {
1833 my ($prefix, $what, $cmd, $file) = @_;
1834
Joe Perches6e74e072010-09-24 10:03:00 -07001835 my @addresses = ();
Ramkumar Ramachandraa47eab02013-03-31 18:40:42 -07001836 open my $fh, "-|", "$cmd \Q$file\E"
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001837 or die sprintf(__("(%s) Could not execute '%s'"), $prefix, $cmd);
Junio C Hamano7ebee442010-10-26 22:02:52 -07001838 while (my $address = <$fh>) {
Joe Perches6e74e072010-09-24 10:03:00 -07001839 $address =~ s/^\s*//g;
1840 $address =~ s/\s*$//g;
1841 $address = sanitize_address($address);
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001842 next if ($address eq $sender and $suppress_cc{'self'});
Joe Perches6e74e072010-09-24 10:03:00 -07001843 push @addresses, $address;
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001844 printf(__("(%s) Adding %s: %s from: '%s'\n"),
1845 $prefix, $what, $address, $cmd) unless $quiet;
Joe Perches6e74e072010-09-24 10:03:00 -07001846 }
Junio C Hamano7ebee442010-10-26 22:02:52 -07001847 close $fh
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001848 or die sprintf(__("(%s) failed to close pipe to '%s'"), $prefix, $cmd);
Joe Perches6e74e072010-09-24 10:03:00 -07001849 return @addresses;
1850}
1851
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001852cleanup_compose_files();
Ryan Anderson1f038a02005-09-05 01:13:07 -04001853
Ævar Arnfjörð Bjarmason4bf597e2010-09-30 13:43:00 +00001854sub cleanup_compose_files {
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001855 unlink($compose_filename, $compose_filename . ".final") if $compose;
Ryan Anderson1f038a02005-09-05 01:13:07 -04001856}
1857
Eric Wong4bc87a22006-03-25 17:20:48 -08001858$smtp->quit if $smtp;
Ryan Andersone2057352005-08-02 21:45:22 -04001859
Paolo Bonzini8d814082014-11-25 15:00:27 +01001860sub apply_transfer_encoding {
1861 my $message = shift;
1862 my $from = shift;
1863 my $to = shift;
1864
Aaron Lindsay3c88e462018-11-02 05:52:38 -04001865 return ($message, $to) if ($from eq $to and $from ne '7bit');
Paolo Bonzini8d814082014-11-25 15:00:27 +01001866
1867 require MIME::QuotedPrint;
1868 require MIME::Base64;
1869
1870 $message = MIME::QuotedPrint::decode($message)
1871 if ($from eq 'quoted-printable');
1872 $message = MIME::Base64::decode($message)
1873 if ($from eq 'base64');
1874
brian m. carlson74d76a12019-04-13 22:45:51 +00001875 $to = ($message =~ /(?:.{999,}|\r)/) ? 'quoted-printable' : '8bit'
brian m. carlson7a369872018-07-08 22:17:10 +00001876 if $to eq 'auto';
1877
Vasco Almeida46493102016-12-14 11:54:36 -01001878 die __("cannot send message as 7bit")
Paolo Bonzini8d814082014-11-25 15:00:27 +01001879 if ($to eq '7bit' and $message =~ /[^[:ascii:]]/);
brian m. carlson7a369872018-07-08 22:17:10 +00001880 return ($message, $to)
Paolo Bonzini8d814082014-11-25 15:00:27 +01001881 if ($to eq '7bit' or $to eq '8bit');
brian m. carlson7a369872018-07-08 22:17:10 +00001882 return (MIME::QuotedPrint::encode($message, "\n", 0), $to)
Paolo Bonzini8d814082014-11-25 15:00:27 +01001883 if ($to eq 'quoted-printable');
brian m. carlson7a369872018-07-08 22:17:10 +00001884 return (MIME::Base64::encode($message, "\n"), $to)
Paolo Bonzini8d814082014-11-25 15:00:27 +01001885 if ($to eq 'base64');
Vasco Almeida46493102016-12-14 11:54:36 -01001886 die __("invalid transfer encoding");
Paolo Bonzini8d814082014-11-25 15:00:27 +01001887}
1888
Ævar Arnfjörð Bjarmasonc438ea22010-09-30 13:42:59 +00001889sub unique_email_list {
Ryan Andersone2057352005-08-02 21:45:22 -04001890 my %seen;
1891 my @emails;
1892
1893 foreach my $entry (@_) {
Krzysztof Mazure4312252012-11-22 19:12:10 +01001894 my $clean = extract_valid_address_or_die($entry);
1895 $seen{$clean} ||= 0;
1896 next if $seen{$clean}++;
1897 push @emails, $entry;
Ryan Andersone2057352005-08-02 21:45:22 -04001898 }
1899 return @emails;
1900}
Jeff King747bbff2008-01-18 09:19:48 -05001901
1902sub validate_patch {
brian m. carlsonf2d06fb2018-07-08 22:17:11 +00001903 my ($fn, $xfer_encoding) = @_;
Jonathan Tan64896602017-05-12 15:38:26 -07001904
Jonathan Tan177409e2017-06-01 16:50:55 -07001905 if ($repo) {
1906 my $validate_hook = catfile(catdir($repo->repo_path(), 'hooks'),
1907 'sendemail-validate');
1908 my $hook_error;
1909 if (-x $validate_hook) {
1910 my $target = abs_path($fn);
1911 # The hook needs a correct cwd and GIT_DIR.
1912 my $cwd_save = cwd();
1913 chdir($repo->wc_path() or $repo->repo_path())
1914 or die("chdir: $!");
1915 local $ENV{"GIT_DIR"} = $repo->repo_path();
1916 $hook_error = "rejected by sendemail-validate hook"
1917 if system($validate_hook, $target);
1918 chdir($cwd_save) or die("chdir: $!");
1919 }
1920 return $hook_error if $hook_error;
Jonathan Tan64896602017-05-12 15:38:26 -07001921 }
Jonathan Tan64896602017-05-12 15:38:26 -07001922
brian m. carlsonf2d06fb2018-07-08 22:17:11 +00001923 # Any long lines will be automatically fixed if we use a suitable transfer
1924 # encoding.
1925 unless ($xfer_encoding =~ /^(?:auto|quoted-printable|base64)$/) {
1926 open(my $fh, '<', $fn)
1927 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
1928 while (my $line = <$fh>) {
1929 if (length($line) > 998) {
1930 return sprintf(__("%s: patch contains a line longer than 998 characters"), $.);
1931 }
Jeff King747bbff2008-01-18 09:19:48 -05001932 }
1933 }
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -07001934 return;
Jeff King747bbff2008-01-18 09:19:48 -05001935}
Jeff King0706bd12008-03-28 17:28:33 -04001936
Junio C Hamano531220b2016-03-17 22:40:05 -07001937sub handle_backup {
1938 my ($last, $lastlen, $file, $known_suffix) = @_;
1939 my ($suffix, $skip);
1940
1941 $skip = 0;
1942 if (defined $last &&
1943 ($lastlen < length($file)) &&
1944 (substr($file, 0, $lastlen) eq $last) &&
1945 ($suffix = substr($file, $lastlen)) !~ /^[a-z0-9]/i) {
1946 if (defined $known_suffix && $suffix eq $known_suffix) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001947 printf(__("Skipping %s with backup suffix '%s'.\n"), $file, $known_suffix);
Junio C Hamano531220b2016-03-17 22:40:05 -07001948 $skip = 1;
1949 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001950 # TRANSLATORS: please keep "[y|N]" as is.
1951 my $answer = ask(sprintf(__("Do you really want to send %s? [y|N]: "), $file),
Junio C Hamano531220b2016-03-17 22:40:05 -07001952 valid_re => qr/^(?:y|n)/i,
1953 default => 'n');
1954 $skip = ($answer ne 'y');
1955 if ($skip) {
1956 $known_suffix = $suffix;
1957 }
1958 }
1959 }
1960 return ($skip, $known_suffix);
1961}
1962
1963sub handle_backup_files {
1964 my @file = @_;
1965 my ($last, $lastlen, $known_suffix, $skip, @result);
1966 for my $file (@file) {
1967 ($skip, $known_suffix) = handle_backup($last, $lastlen,
1968 $file, $known_suffix);
1969 push @result, $file unless $skip;
1970 $last = $file;
1971 $lastlen = length($file);
1972 }
1973 return @result;
1974}
1975
Jeff King0706bd12008-03-28 17:28:33 -04001976sub file_has_nonascii {
1977 my $fn = shift;
1978 open(my $fh, '<', $fn)
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001979 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
Jeff King0706bd12008-03-28 17:28:33 -04001980 while (my $line = <$fh>) {
1981 return 1 if $line =~ /[^[:ascii:]]/;
1982 }
1983 return 0;
1984}
Thomas Rast3cae7e52010-06-17 22:10:39 +02001985
1986sub body_or_subject_has_nonascii {
1987 my $fn = shift;
1988 open(my $fh, '<', $fn)
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001989 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
Thomas Rast3cae7e52010-06-17 22:10:39 +02001990 while (my $line = <$fh>) {
1991 last if $line =~ /^$/;
1992 return 1 if $line =~ /^Subject.*[^[:ascii:]]/;
1993 }
1994 while (my $line = <$fh>) {
1995 return 1 if $line =~ /[^[:ascii:]]/;
1996 }
1997 return 0;
1998}