blob: dc95656f75c1c4a066665b91d31b387936998961 [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:
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200180my (@to,@cc,@xh,$envelope_sender,
Christian Ludwigd11c9432018-03-04 00:58:14 +0100181 $initial_in_reply_to,$reply_to,$initial_subject,@files,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200182 $author,$sender,$smtp_authpass,$annotate,$compose,$time);
183# Things we either get from config, *or* are overridden on the
184# command-line.
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200185my ($no_cc, $no_to, $no_bcc, $no_identity);
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200186my (@config_to, @getopt_to);
187my (@config_cc, @getopt_cc);
188my (@config_bcc, @getopt_bcc);
Ryan Anderson78488b22005-07-31 20:04:24 -0400189
Ryan Anderson91332612005-07-31 20:04:24 -0400190# Example reply to:
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100191#$initial_in_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
Ryan Anderson83b24432005-07-31 04:17:25 -0400192
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100193my $repo = eval { Git->repository() };
194my @repo = $repo ? ($repo) : ();
Junio C Hamano280242d2006-07-02 16:03:59 -0700195my $term = eval {
Jay Soffian0fb7fc72008-02-21 19:16:04 -0500196 $ENV{"GIT_SEND_EMAIL_NOTTY"}
197 ? new Term::ReadLine 'git-send-email', \*STDIN, \*STDOUT
198 : new Term::ReadLine 'git-send-email';
Junio C Hamano280242d2006-07-02 16:03:59 -0700199};
200if ($@) {
201 $term = new FakeTerm "$@: going non-interactive";
202}
Ryan Anderson83b24432005-07-31 04:17:25 -0400203
Adam Roben5483c712007-06-27 20:59:37 -0700204# Behavior modification variables
205my ($quiet, $dry_run) = (0, 0);
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100206my $format_patch;
Jay Soffianafe756c2009-02-23 13:51:37 -0500207my $compose_filename;
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200208my $force = 0;
Jacob Keller17b7a832015-11-19 14:52:11 -0800209my $dump_aliases = 0;
Adam Roben5483c712007-06-27 20:59:37 -0700210
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100211# Handle interactive edition of files.
212my $multiedit;
Michael J Gruber0ce142c2010-03-22 17:12:53 +0100213my $editor;
Jonathan Niederb4479f02009-10-30 20:42:34 -0500214
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100215sub do_edit {
Michael J Gruber0ce142c2010-03-22 17:12:53 +0100216 if (!defined($editor)) {
217 $editor = Git::command_oneline('var', 'GIT_EDITOR');
218 }
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100219 if (defined($multiedit) && !$multiedit) {
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100220 map {
221 system('sh', '-c', $editor.' "$@"', $editor, $_);
222 if (($? & 127) || ($? >> 8)) {
Vasco Almeida46493102016-12-14 11:54:36 -0100223 die(__("the editor exited uncleanly, aborting everything"));
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100224 }
225 } @_;
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100226 } else {
227 system('sh', '-c', $editor.' "$@"', $editor, @_);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100228 if (($? & 127) || ($? >> 8)) {
Vasco Almeida46493102016-12-14 11:54:36 -0100229 die(__("the editor exited uncleanly, aborting everything"));
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100230 }
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100231 }
232}
Adam Roben5483c712007-06-27 20:59:37 -0700233
234# Variables with corresponding config settings
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200235my ($suppress_from, $signed_off_by_cc);
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300236my ($cover_cc, $cover_to);
Joe Perches6e74e072010-09-24 10:03:00 -0700237my ($to_cmd, $cc_cmd);
Pascal Obry052fbea2010-09-06 20:12:11 +0200238my ($smtp_server, $smtp_server_port, @smtp_server_options);
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -0700239my ($smtp_authuser, $smtp_encryption, $smtp_ssl_cert_path);
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800240my ($batch_size, $relogin_delay);
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200241my ($identity, $aliasfiletype, @alias_files, $smtp_domain, $smtp_auth);
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200242my ($confirm);
David Brown65648282007-12-25 19:56:29 -0800243my (@suppress_cc);
Thomas Rast3cae7e52010-06-17 22:10:39 +0200244my ($auto_8bit_encoding);
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200245my ($compose_encoding);
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200246# Variables with corresponding config settings & hardcoded defaults
247my ($debug_net_smtp) = 0; # Net::SMTP, see send_message()
248my $thread = 1;
249my $chain_reply_to = 0;
250my $use_xmailer = 1;
251my $validate = 1;
brian m. carlsone67a2282018-07-08 22:17:12 +0000252my $target_xfer_encoding = 'auto';
Adam Roben5483c712007-06-27 20:59:37 -0700253
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900254my %config_bool_settings = (
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200255 "thread" => \$thread,
256 "chainreplyto" => \$chain_reply_to,
257 "suppressfrom" => \$suppress_from,
258 "signedoffbycc" => \$signed_off_by_cc,
259 "cccover" => \$cover_cc,
260 "tocover" => \$cover_to,
261 "signedoffcc" => \$signed_off_by_cc,
262 "validate" => \$validate,
263 "multiedit" => \$multiedit,
264 "annotate" => \$annotate,
265 "xmailer" => \$use_xmailer,
Adam Robene46f7a02007-06-26 15:48:30 -0700266);
267
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900268my %config_settings = (
269 "smtpserver" => \$smtp_server,
Junio C Hamano44b24762007-09-25 17:27:54 -0700270 "smtpserverport" => \$smtp_server_port,
Pascal Obry052fbea2010-09-06 20:12:11 +0200271 "smtpserveroption" => \@smtp_server_options,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900272 "smtpuser" => \$smtp_authuser,
273 "smtppass" => \$smtp_authpass,
Pascal Obrye1e91152010-09-06 20:12:09 +0200274 "smtpdomain" => \$smtp_domain,
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200275 "smtpauth" => \$smtp_auth,
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800276 "smtpbatchsize" => \$batch_size,
277 "smtprelogindelay" => \$relogin_delay,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200278 "to" => \@config_to,
Joe Perches6e74e072010-09-24 10:03:00 -0700279 "tocmd" => \$to_cmd,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200280 "cc" => \@config_cc,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900281 "cccmd" => \$cc_cmd,
282 "aliasfiletype" => \$aliasfiletype,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200283 "bcc" => \@config_bcc,
David Brown65648282007-12-25 19:56:29 -0800284 "suppresscc" => \@suppress_cc,
Ask Bjørn Hansen9f7820a2008-06-07 00:33:42 -0700285 "envelopesender" => \$envelope_sender,
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500286 "confirm" => \$confirm,
Trent Piepho09caa242009-05-12 15:48:56 -0700287 "from" => \$sender,
Thomas Rast3cae7e52010-06-17 22:10:39 +0200288 "assume8bitencoding" => \$auto_8bit_encoding,
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200289 "composeencoding" => \$compose_encoding,
Paolo Bonzini8d814082014-11-25 15:00:27 +0100290 "transferencoding" => \$target_xfer_encoding,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900291);
Avi Kivity4a62d3f2007-03-11 19:19:44 +0200292
Cord Seelecec5dae2011-09-30 12:52:25 +0200293my %config_path_settings = (
294 "aliasesfile" => \@alias_files,
John Keeping6e07a3b2015-11-17 22:01:05 +0000295 "smtpsslcertpath" => \$smtp_ssl_cert_path,
Cord Seelecec5dae2011-09-30 12:52:25 +0200296);
297
Michael Witten87429972008-02-03 19:53:57 -0500298# Handle Uncouth Termination
299sub signal_handler {
300
301 # Make text normal
302 print color("reset"), "\n";
303
304 # SMTP password masked
305 system "stty echo";
306
307 # tmp files from --compose
Jay Soffianafe756c2009-02-23 13:51:37 -0500308 if (defined $compose_filename) {
309 if (-e $compose_filename) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100310 printf __("'%s' contains an intermediate version ".
311 "of the email you were composing.\n"),
312 $compose_filename;
Jay Soffianafe756c2009-02-23 13:51:37 -0500313 }
314 if (-e ($compose_filename . ".final")) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100315 printf __("'%s.final' contains the composed email.\n"),
316 $compose_filename;
Jay Soffianafe756c2009-02-23 13:51:37 -0500317 }
Michael Witten87429972008-02-03 19:53:57 -0500318 }
319
320 exit;
321};
322
323$SIG{TERM} = \&signal_handler;
324$SIG{INT} = \&signal_handler;
325
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200326# Read our sendemail.* config
327sub read_config {
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200328 my ($configured, $prefix) = @_;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200329
330 foreach my $setting (keys %config_bool_settings) {
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200331 my $target = $config_bool_settings{$setting};
332 my $v = Git::config_bool(@repo, "$prefix.$setting");
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200333 next unless defined $v;
334 next if $configured->{$setting}++;
335 $$target = $v;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200336 }
337
338 foreach my $setting (keys %config_path_settings) {
339 my $target = $config_path_settings{$setting};
340 if (ref($target) eq "ARRAY") {
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200341 my @values = Git::config_path(@repo, "$prefix.$setting");
342 next unless @values;
343 next if $configured->{$setting}++;
344 @$target = @values;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200345 }
346 else {
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200347 my $v = Git::config_path(@repo, "$prefix.$setting");
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200348 next unless defined $v;
349 next if $configured->{$setting}++;
350 $$target = $v;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200351 }
352 }
353
354 foreach my $setting (keys %config_settings) {
355 my $target = $config_settings{$setting};
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200356 if (ref($target) eq "ARRAY") {
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200357 my @values = Git::config(@repo, "$prefix.$setting");
358 next unless @values;
359 next if $configured->{$setting}++;
360 @$target = @values;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200361 }
362 else {
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200363 my $v = Git::config(@repo, "$prefix.$setting");
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200364 next unless defined $v;
365 next if $configured->{$setting}++;
366 $$target = $v;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200367 }
368 }
369
370 if (!defined $smtp_encryption) {
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200371 my $setting = "$prefix.smtpencryption";
372 my $enc = Git::config(@repo, $setting);
373 return unless defined $enc;
374 return if $configured->{$setting}++;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200375 if (defined $enc) {
376 $smtp_encryption = $enc;
377 } elsif (Git::config_bool(@repo, "$prefix.smtpssl")) {
378 $smtp_encryption = 'ssl';
379 }
380 }
381}
382
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200383# sendemail.identity yields to --identity. We must parse this
384# special-case first before the rest of the config is read.
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200385$identity = Git::config(@repo, "sendemail.identity");
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200386my $rc = GetOptions(
387 "identity=s" => \$identity,
388 "no-identity" => \$no_identity,
389);
390usage() unless $rc;
391undef $identity if $no_identity;
392
393# Now we know enough to read the config
394{
395 my %configured;
396 read_config(\%configured, "sendemail.$identity") if defined $identity;
397 read_config(\%configured, "sendemail");
398}
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200399
Ryan Anderson83b24432005-07-31 04:17:25 -0400400# Begin by accumulating all the variables (defined above), that we will end up
401# needing, first, from the command line:
402
Clemens Buchacherc5978242011-09-03 19:06:13 +0200403my $help;
Duy Nguyen13374982018-11-03 07:03:18 +0100404my $git_completion_helper;
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200405$rc = GetOptions("h" => \$help,
406 "dump-aliases" => \$dump_aliases);
Jacob Keller17b7a832015-11-19 14:52:11 -0800407usage() unless $rc;
Vasco Almeida46493102016-12-14 11:54:36 -0100408die __("--dump-aliases incompatible with other options\n")
Jacob Keller17b7a832015-11-19 14:52:11 -0800409 if !$help and $dump_aliases and @ARGV;
410$rc = GetOptions(
Clemens Buchacherc5978242011-09-03 19:06:13 +0200411 "sender|from=s" => \$sender,
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100412 "in-reply-to=s" => \$initial_in_reply_to,
Christian Ludwigd11c9432018-03-04 00:58:14 +0100413 "reply-to=s" => \$reply_to,
Ryan Anderson83b24432005-07-31 04:17:25 -0400414 "subject=s" => \$initial_subject,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200415 "to=s" => \@getopt_to,
Joe Perches6e74e072010-09-24 10:03:00 -0700416 "to-cmd=s" => \$to_cmd,
Stephen Boydf434c082010-03-07 14:46:48 -0800417 "no-to" => \$no_to,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200418 "cc=s" => \@getopt_cc,
Stephen Boydf434c082010-03-07 14:46:48 -0800419 "no-cc" => \$no_cc,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200420 "bcc=s" => \@getopt_bcc,
Stephen Boydf434c082010-03-07 14:46:48 -0800421 "no-bcc" => \$no_bcc,
Ryan Anderson78488b22005-07-31 20:04:24 -0400422 "chain-reply-to!" => \$chain_reply_to,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800423 "no-chain-reply-to" => sub {$chain_reply_to = 0},
Ryan Anderson3342d852005-07-31 20:04:24 -0400424 "smtp-server=s" => \$smtp_server,
Pascal Obry052fbea2010-09-06 20:12:11 +0200425 "smtp-server-option=s" => \@smtp_server_options,
Junio C Hamano44b24762007-09-25 17:27:54 -0700426 "smtp-server-port=s" => \$smtp_server_port,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900427 "smtp-user=s" => \$smtp_authuser,
Michael Witten2363d742008-02-03 19:53:56 -0500428 "smtp-pass:s" => \$smtp_authpass,
Thomas Rastf6bebd12008-06-25 21:42:43 +0200429 "smtp-ssl" => sub { $smtp_encryption = 'ssl' },
430 "smtp-encryption=s" => \$smtp_encryption,
Thomas Rast979e6522013-12-01 23:48:42 +0100431 "smtp-ssl-cert-path=s" => \$smtp_ssl_cert_path,
Jari Aaltof60812e2010-03-14 17:16:09 +0200432 "smtp-debug:i" => \$debug_net_smtp,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -0400433 "smtp-domain:s" => \$smtp_domain,
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200434 "smtp-auth=s" => \$smtp_auth,
Joshua Watt8dd9b3f2018-10-22 22:24:25 -0500435 "no-smtp-auth" => sub {$smtp_auth = 'none'},
Felipe Contreras402596a2013-04-07 01:10:27 -0600436 "annotate!" => \$annotate,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800437 "no-annotate" => sub {$annotate = 0},
Ryan Anderson1f038a02005-09-05 01:13:07 -0400438 "compose" => \$compose,
Ryan Anderson30d08b32006-02-02 11:56:06 -0500439 "quiet" => \$quiet,
Joe Perches324a8bd2007-08-17 18:51:12 -0700440 "cc-cmd=s" => \$cc_cmd,
Adam Roben5483c712007-06-27 20:59:37 -0700441 "suppress-from!" => \$suppress_from,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800442 "no-suppress-from" => sub {$suppress_from = 0},
David Brown65648282007-12-25 19:56:29 -0800443 "suppress-cc=s" => \@suppress_cc,
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500444 "signed-off-cc|signed-off-by-cc!" => \$signed_off_by_cc,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800445 "no-signed-off-cc|no-signed-off-by-cc" => sub {$signed_off_by_cc = 0},
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300446 "cc-cover|cc-cover!" => \$cover_cc,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800447 "no-cc-cover" => sub {$cover_cc = 0},
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300448 "to-cover|to-cover!" => \$cover_to,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800449 "no-to-cover" => sub {$cover_to = 0},
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500450 "confirm=s" => \$confirm,
Matthew Wilcox61302592006-10-10 08:58:23 -0600451 "dry-run" => \$dry_run,
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700452 "envelope-sender=s" => \$envelope_sender,
Adam Roben5483c712007-06-27 20:59:37 -0700453 "thread!" => \$thread,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800454 "no-thread" => sub {$thread = 0},
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500455 "validate!" => \$validate,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800456 "no-validate" => sub {$validate = 0},
Paolo Bonzini8d814082014-11-25 15:00:27 +0100457 "transfer-encoding=s" => \$target_xfer_encoding,
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100458 "format-patch!" => \$format_patch,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800459 "no-format-patch" => sub {$format_patch = 0},
Thomas Rast3cae7e52010-06-17 22:10:39 +0200460 "8bit-encoding=s" => \$auto_8bit_encoding,
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200461 "compose-encoding=s" => \$compose_encoding,
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200462 "force" => \$force,
Luis Henriquesac1596a2014-03-24 21:38:27 +0000463 "xmailer!" => \$use_xmailer,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800464 "no-xmailer" => sub {$use_xmailer = 0},
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800465 "batch-size=i" => \$batch_size,
466 "relogin-delay=i" => \$relogin_delay,
Duy Nguyen13374982018-11-03 07:03:18 +0100467 "git-completion-helper" => \$git_completion_helper,
Ryan Anderson83b24432005-07-31 04:17:25 -0400468 );
469
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200470# Munge any "either config or getopt, not both" variables
471my @initial_to = @getopt_to ? @getopt_to : ($no_to ? () : @config_to);
472my @initial_cc = @getopt_cc ? @getopt_cc : ($no_cc ? () : @config_cc);
473my @initial_bcc = @getopt_bcc ? @getopt_bcc : ($no_bcc ? () : @config_bcc);
474
Clemens Buchacherc5978242011-09-03 19:06:13 +0200475usage() if $help;
Duy Nguyen13374982018-11-03 07:03:18 +0100476completion_helper() if $git_completion_helper;
Michael Coleman1b0baf12007-02-27 22:47:54 -0600477unless ($rc) {
478 usage();
479}
480
Vasco Almeida46493102016-12-14 11:54:36 -0100481die __("Cannot run git format-patch from outside a repository\n")
Jay Soffianeed6ca72009-02-14 23:32:13 -0500482 if $format_patch and not $repo;
483
Stefan Beller9caa7062018-02-12 11:44:04 -0800484die __("`batch-size` and `relogin` must be specified together " .
485 "(via command-line or configuration option)\n")
486 if defined $relogin_delay and not defined $batch_size;
487
Thomas Rastfa835cd2008-06-26 23:03:21 +0200488# 'default' encryption is none -- this only prevents a warning
489$smtp_encryption = '' unless (defined $smtp_encryption);
490
David Brown65648282007-12-25 19:56:29 -0800491# Set CC suppressions
492my(%suppress_cc);
493if (@suppress_cc) {
494 foreach my $entry (@suppress_cc) {
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700495 # Please update $__git_send_email_suppresscc_options
496 # in git-completion.bash when you add new options.
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100497 die sprintf(__("Unknown --suppress-cc field: '%s'\n"), $entry)
Rasmus Villemoesef0cc1d2018-10-16 09:39:23 +0200498 unless $entry =~ /^(?:all|cccmd|cc|author|self|sob|body|bodycc|misc-by)$/;
David Brown65648282007-12-25 19:56:29 -0800499 $suppress_cc{$entry} = 1;
500 }
501}
502
503if ($suppress_cc{'all'}) {
Rasmus Villemoesef0cc1d2018-10-16 09:39:23 +0200504 foreach my $entry (qw (cccmd cc author self sob body bodycc misc-by)) {
David Brown65648282007-12-25 19:56:29 -0800505 $suppress_cc{$entry} = 1;
506 }
507 delete $suppress_cc{'all'};
508}
509
510# If explicit old-style ones are specified, they trump --suppress-cc.
511$suppress_cc{'self'} = $suppress_from if defined $suppress_from;
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500512$suppress_cc{'sob'} = !$signed_off_by_cc if defined $signed_off_by_cc;
David Brown65648282007-12-25 19:56:29 -0800513
Jay Soffian3531e272009-02-14 23:32:15 -0500514if ($suppress_cc{'body'}) {
Rasmus Villemoesef0cc1d2018-10-16 09:39:23 +0200515 foreach my $entry (qw (sob bodycc misc-by)) {
Jay Soffian3531e272009-02-14 23:32:15 -0500516 $suppress_cc{$entry} = 1;
517 }
518 delete $suppress_cc{'body'};
519}
520
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500521# Set confirm's default value
522my $confirm_unconfigured = !defined $confirm;
523if ($confirm_unconfigured) {
524 $confirm = scalar %suppress_cc ? 'compose' : 'auto';
525};
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700526# Please update $__git_send_email_confirm_options in
527# git-completion.bash when you add new options.
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100528die sprintf(__("Unknown --confirm setting: '%s'\n"), $confirm)
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500529 unless $confirm =~ /^(?:auto|cc|compose|always|never)/;
530
David Brown65648282007-12-25 19:56:29 -0800531# Debugging, print out the suppressions.
532if (0) {
533 print "suppressions:\n";
534 foreach my $entry (keys %suppress_cc) {
535 printf " %-5s -> $suppress_cc{$entry}\n", $entry;
536 }
537}
538
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100539my ($repoauthor, $repocommitter);
540($repoauthor) = Git::ident_person(@repo, 'author');
541($repocommitter) = Git::ident_person(@repo, 'committer');
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900542
Jay Soffian50126992009-02-14 23:32:14 -0500543sub parse_address_line {
Matthieu Moybd869f62018-01-05 19:36:51 +0100544 return map { $_->format } Mail::Address->parse($_[0]);
Jay Soffian50126992009-02-14 23:32:14 -0500545}
546
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800547sub split_addrs {
Junio C Hamano2f0e7cb2008-12-21 01:57:59 -0800548 return quotewords('\s*,\s*', 1, @_);
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800549}
550
Eric Wong994d6c62006-05-14 19:13:44 -0700551my %aliases;
Eric Sunshine09f11572015-05-31 18:29:27 -0400552
553sub parse_sendmail_alias {
554 local $_ = shift;
555 if (/"/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100556 printf STDERR __("warning: sendmail alias with quotes is not supported: %s\n"), $_;
Eric Sunshine86b89842015-06-01 14:22:36 -0400557 } elsif (/:include:/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100558 printf STDERR __("warning: `:include:` not supported: %s\n"), $_;
Eric Sunshine86b89842015-06-01 14:22:36 -0400559 } elsif (/[\/|]/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100560 printf STDERR __("warning: `/file` or `|pipe` redirection not supported: %s\n"), $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400561 } elsif (/^(\S+?)\s*:\s*(.+)$/) {
562 my ($alias, $addr) = ($1, $2);
563 $aliases{$alias} = [ split_addrs($addr) ];
564 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100565 printf STDERR __("warning: sendmail line is not recognized: %s\n"), $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400566 }
567}
568
569sub parse_sendmail_aliases {
570 my $fh = shift;
Eric Sunshine2532dd02015-05-31 18:29:29 -0400571 my $s = '';
Eric Sunshine09f11572015-05-31 18:29:27 -0400572 while (<$fh>) {
Eric Sunshine2532dd02015-05-31 18:29:29 -0400573 chomp;
Eric Sunshine020be85f2015-05-31 18:29:28 -0400574 next if /^\s*$/ || /^\s*#/;
Eric Sunshine2532dd02015-05-31 18:29:29 -0400575 $s .= $_, next if $s =~ s/\\$// || s/^\s+//;
576 parse_sendmail_alias($s) if $s;
577 $s = $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400578 }
Eric Sunshine2532dd02015-05-31 18:29:29 -0400579 $s =~ s/\\$//; # silently tolerate stray '\' on last line
580 parse_sendmail_alias($s) if $s;
Eric Sunshine09f11572015-05-31 18:29:27 -0400581}
582
Eric Wong994d6c62006-05-14 19:13:44 -0700583my %parse_alias = (
584 # multiline formats can be supported in the future
585 mutt => sub { my $fh = shift; while (<$fh>) {
Felipe Contrerasffc01f92009-09-30 17:49:36 +0300586 if (/^\s*alias\s+(?:-group\s+\S+\s+)*(\S+)\s+(.*)$/) {
Eric Wong994d6c62006-05-14 19:13:44 -0700587 my ($alias, $addr) = ($1, $2);
588 $addr =~ s/#.*$//; # mutt allows # comments
Eric Wong2c510f22016-01-04 20:53:30 +0000589 # commas delimit multiple addresses
590 my @addr = split_addrs($addr);
591
592 # quotes may be escaped in the file,
593 # unescape them so we do not double-escape them later.
594 s/\\"/"/g foreach @addr;
595 $aliases{$alias} = \@addr
Eric Wong994d6c62006-05-14 19:13:44 -0700596 }}},
597 mailrc => sub { my $fh = shift; while (<$fh>) {
Jeff Kinga277d1e2016-03-17 19:58:22 -0400598 if (/^alias\s+(\S+)\s+(.*?)\s*$/) {
Eric Wong994d6c62006-05-14 19:13:44 -0700599 # spaces delimit multiple addresses
Eric W. Biedermanfe87c922009-05-20 19:45:53 -0700600 $aliases{$1} = [ quotewords('\s+', 0, $2) ];
Eric Wong994d6c62006-05-14 19:13:44 -0700601 }}},
Trent Piepho73c427e2008-11-25 18:55:00 -0800602 pine => sub { my $fh = shift; my $f='\t[^\t]*';
603 for (my $x = ''; defined($x); $x = $_) {
604 chomp $x;
605 $x .= $1 while(defined($_ = <$fh>) && /^ +(.*)$/);
606 $x =~ /^(\S+)$f\t\(?([^\t]+?)\)?(:?$f){0,2}$/ or next;
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800607 $aliases{$1} = [ split_addrs($2) ];
Trent Piepho73c427e2008-11-25 18:55:00 -0800608 }},
Bill Pemberton7613ea32009-04-22 09:41:29 -0400609 elm => sub { my $fh = shift;
610 while (<$fh>) {
611 if (/^(\S+)\s+=\s+[^=]+=\s(\S+)/) {
612 my ($alias, $addr) = ($1, $2);
613 $aliases{$alias} = [ split_addrs($addr) ];
614 }
615 } },
Eric Sunshine09f11572015-05-31 18:29:27 -0400616 sendmail => \&parse_sendmail_aliases,
Eric Wong994d6c62006-05-14 19:13:44 -0700617 gnus => sub { my $fh = shift; while (<$fh>) {
618 if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
619 $aliases{$1} = [ $2 ];
620 }}}
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700621 # Please update _git_config() in git-completion.bash when you
622 # add new MUAs.
Eric Wong994d6c62006-05-14 19:13:44 -0700623);
624
Petr Baudis3cb8caf2006-07-03 22:47:58 +0200625if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) {
Eric Wong994d6c62006-05-14 19:13:44 -0700626 foreach my $file (@alias_files) {
627 open my $fh, '<', $file or die "opening $file: $!\n";
628 $parse_alias{$aliasfiletype}->($fh);
629 close $fh;
630 }
631}
632
Jacob Keller17b7a832015-11-19 14:52:11 -0800633if ($dump_aliases) {
634 print "$_\n" for (sort keys %aliases);
635 exit(0);
636}
637
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700638# is_format_patch_arg($f) returns 0 if $f names a patch, or 1 if
639# $f is a revision list specification to be passed to format-patch.
640sub is_format_patch_arg {
Jay Soffianeed6ca72009-02-14 23:32:13 -0500641 return unless $repo;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100642 my $f = shift;
643 try {
644 $repo->command('rev-parse', '--verify', '--quiet', $f);
645 if (defined($format_patch)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100646 return $format_patch;
647 }
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100648 die sprintf(__ <<EOF, $f, $f);
649File '%s' exists but it could also be the range of commits
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100650to produce patches for. Please disambiguate by...
651
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100652 * Saying "./%s" if you mean a file; or
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100653 * Giving --format-patch option if you mean a range.
654EOF
655 } catch Git::Error::Command with {
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700656 # Not a valid revision. Treat it as a filename.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100657 return 0;
658 }
659}
660
Jeff Kingaa548922008-01-18 09:19:36 -0500661# Now that all the defaults are set, process the rest of the command line
662# arguments and collect up the files that need to be processed.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100663my @rev_list_opts;
Junio C Hamano69f4ce52008-11-30 22:38:20 -0800664while (defined(my $f = shift @ARGV)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100665 if ($f eq "--") {
666 push @rev_list_opts, "--", @ARGV;
667 @ARGV = ();
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700668 } elsif (-d $f and !is_format_patch_arg($f)) {
Ævar Arnfjörð Bjarmasonc6038162010-09-30 13:42:54 +0000669 opendir my $dh, $f
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100670 or die sprintf(__("Failed to opendir %s: %s"), $f, $!);
Jeff Kingaa548922008-01-18 09:19:36 -0500671
Ævar Arnfjörð Bjarmason89bf1ba2010-09-14 19:02:24 +0000672 push @files, grep { -f $_ } map { catfile($f, $_) }
Ævar Arnfjörð Bjarmasonc6038162010-09-30 13:42:54 +0000673 sort readdir $dh;
674 closedir $dh;
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700675 } elsif ((-f $f or -p $f) and !is_format_patch_arg($f)) {
Jeff Kingaa548922008-01-18 09:19:36 -0500676 push @files, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500677 } else {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100678 push @rev_list_opts, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500679 }
680}
681
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100682if (@rev_list_opts) {
Vasco Almeida46493102016-12-14 11:54:36 -0100683 die __("Cannot run git format-patch from outside a repository\n")
Jay Soffianeed6ca72009-02-14 23:32:13 -0500684 unless $repo;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100685 push @files, $repo->command('format-patch', '-o', tempdir(CLEANUP => 1), @rev_list_opts);
686}
687
Junio C Hamano531220b2016-03-17 22:40:05 -0700688@files = handle_backup_files(@files);
689
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500690if ($validate) {
Jeff Kingc764a0c2008-01-18 09:20:10 -0500691 foreach my $f (@files) {
Kevin Ballard300913b2008-06-25 15:44:40 -0700692 unless (-p $f) {
brian m. carlsonf2d06fb2018-07-08 22:17:11 +0000693 my $error = validate_patch($f, $target_xfer_encoding);
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100694 $error and die sprintf(__("fatal: %s: %s\nwarning: no patches were sent\n"),
695 $f, $error);
Kevin Ballard300913b2008-06-25 15:44:40 -0700696 }
Jeff Kingc764a0c2008-01-18 09:20:10 -0500697 }
Jeff King747bbff2008-01-18 09:19:48 -0500698}
699
Jeff Kingaa548922008-01-18 09:19:36 -0500700if (@files) {
701 unless ($quiet) {
702 print $_,"\n" for (@files);
703 }
704} else {
Vasco Almeida46493102016-12-14 11:54:36 -0100705 print STDERR __("\nNo patch files specified!\n\n");
Jeff Kingaa548922008-01-18 09:19:36 -0500706 usage();
707}
708
Ævar Arnfjörð Bjarmasonacf071b2010-09-30 13:42:57 +0000709sub get_patch_subject {
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100710 my $fn = shift;
711 open (my $fh, '<', $fn);
712 while (my $line = <$fh>) {
713 next unless ($line =~ /^Subject: (.*)$/);
714 close $fh;
715 return "GIT: $1\n";
716 }
717 close $fh;
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100718 die sprintf(__("No subject line in %s?"), $fn);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100719}
720
721if ($compose) {
722 # Note that this does not need to be secure, but we will make a small
723 # effort to have it be unique
Jay Soffianafe756c2009-02-23 13:51:37 -0500724 $compose_filename = ($repo ?
725 tempfile(".gitsendemail.msg.XXXXXX", DIR => $repo->repo_path()) :
726 tempfile(".gitsendemail.msg.XXXXXX", DIR => "."))[1];
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000727 open my $c, ">", $compose_filename
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100728 or die sprintf(__("Failed to open for writing %s: %s"), $compose_filename, $!);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100729
730
731 my $tpl_sender = $sender || $repoauthor || $repocommitter || '';
732 my $tpl_subject = $initial_subject || '';
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100733 my $tpl_in_reply_to = $initial_in_reply_to || '';
Christian Ludwigd11c9432018-03-04 00:58:14 +0100734 my $tpl_reply_to = $reply_to || '';
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100735
Vasco Almeida70aedfb2016-12-14 11:54:38 -0100736 print $c <<EOT1, Git::prefix_lines("GIT: ", __ <<EOT2), <<EOT3;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100737From $tpl_sender # This line is ignored.
Vasco Almeida70aedfb2016-12-14 11:54:38 -0100738EOT1
739Lines beginning in "GIT:" will be removed.
740Consider including an overall diffstat or table of contents
741for the patch you are writing.
742
743Clear the body content if you don't wish to send a summary.
744EOT2
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100745From: $tpl_sender
Christian Ludwigd11c9432018-03-04 00:58:14 +0100746Reply-To: $tpl_reply_to
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100747Subject: $tpl_subject
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100748In-Reply-To: $tpl_in_reply_to
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100749
Vasco Almeida70aedfb2016-12-14 11:54:38 -0100750EOT3
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100751 for my $f (@files) {
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000752 print $c get_patch_subject($f);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100753 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000754 close $c;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100755
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100756 if ($annotate) {
757 do_edit($compose_filename, @files);
758 } else {
759 do_edit($compose_filename);
760 }
761
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000762 open $c, "<", $compose_filename
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100763 or die sprintf(__("Failed to open %s: %s"), $compose_filename, $!);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100764
Krzysztof Mazur4a47a4d2012-10-22 14:41:48 +0200765 if (!defined $compose_encoding) {
766 $compose_encoding = "UTF-8";
767 }
Nathan Payreb6049542017-12-15 16:33:39 +0100768
769 my %parsed_email;
770 while (my $line = <$c>) {
771 next if $line =~ m/^GIT:/;
772 parse_header_line($line, \%parsed_email);
773 if ($line =~ /^$/) {
774 $parsed_email{'body'} = filter_body($c);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100775 }
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100776 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000777 close $c;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100778
Nathan Payreb6049542017-12-15 16:33:39 +0100779 open my $c2, ">", $compose_filename . ".final"
780 or die sprintf(__("Failed to open %s.final: %s"), $compose_filename, $!);
781
782
783 if ($parsed_email{'From'}) {
784 $sender = delete($parsed_email{'From'});
785 }
786 if ($parsed_email{'In-Reply-To'}) {
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100787 $initial_in_reply_to = delete($parsed_email{'In-Reply-To'});
Nathan Payreb6049542017-12-15 16:33:39 +0100788 }
Christian Ludwigd11c9432018-03-04 00:58:14 +0100789 if ($parsed_email{'Reply-To'}) {
790 $reply_to = delete($parsed_email{'Reply-To'});
Nathan Payreb6049542017-12-15 16:33:39 +0100791 }
792 if ($parsed_email{'Subject'}) {
793 $initial_subject = delete($parsed_email{'Subject'});
794 print $c2 "Subject: " .
795 quote_subject($initial_subject, $compose_encoding) .
796 "\n";
797 }
798
799 if ($parsed_email{'MIME-Version'}) {
800 print $c2 "MIME-Version: $parsed_email{'MIME-Version'}\n",
801 "Content-Type: $parsed_email{'Content-Type'};\n",
802 "Content-Transfer-Encoding: $parsed_email{'Content-Transfer-Encoding'}\n";
803 delete($parsed_email{'MIME-Version'});
804 delete($parsed_email{'Content-Type'});
805 delete($parsed_email{'Content-Transfer-Encoding'});
806 } elsif (file_has_nonascii($compose_filename)) {
807 my $content_type = (delete($parsed_email{'Content-Type'}) or
808 "text/plain; charset=$compose_encoding");
809 print $c2 "MIME-Version: 1.0\n",
810 "Content-Type: $content_type\n",
811 "Content-Transfer-Encoding: 8bit\n";
812 }
813 # Preserve unknown headers
814 foreach my $key (keys %parsed_email) {
815 next if $key eq 'body';
816 print $c2 "$key: $parsed_email{$key}";
817 }
818
819 if ($parsed_email{'body'}) {
820 print $c2 "\n$parsed_email{'body'}\n";
821 delete($parsed_email{'body'});
822 } else {
Vasco Almeida46493102016-12-14 11:54:36 -0100823 print __("Summary email is empty, skipping it\n");
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100824 $compose = -1;
825 }
Nathan Payreb6049542017-12-15 16:33:39 +0100826
827 close $c2;
828
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100829} elsif ($annotate) {
830 do_edit(@files);
831}
832
Jay Soffian6e182512009-03-28 21:39:10 -0400833sub ask {
834 my ($prompt, %arg) = @_;
Jay Soffian0da43a62009-04-04 23:23:21 -0400835 my $valid_re = $arg{valid_re};
Jay Soffian6e182512009-03-28 21:39:10 -0400836 my $default = $arg{default};
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700837 my $confirm_only = $arg{confirm_only};
Jay Soffian6e182512009-03-28 21:39:10 -0400838 my $resp;
839 my $i = 0;
Jay Soffian5906f542009-03-31 12:22:11 -0400840 return defined $default ? $default : undef
841 unless defined $term->IN and defined fileno($term->IN) and
842 defined $term->OUT and defined fileno($term->OUT);
Jay Soffian6e182512009-03-28 21:39:10 -0400843 while ($i++ < 10) {
844 $resp = $term->readline($prompt);
845 if (!defined $resp) { # EOF
846 print "\n";
847 return defined $default ? $default : undef;
848 }
849 if ($resp eq '' and defined $default) {
850 return $default;
851 }
Jay Soffian0da43a62009-04-04 23:23:21 -0400852 if (!defined $valid_re or $resp =~ /$valid_re/) {
Jay Soffian6e182512009-03-28 21:39:10 -0400853 return $resp;
854 }
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700855 if ($confirm_only) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100856 my $yesno = $term->readline(
857 # TRANSLATORS: please keep [y/N] as is.
858 sprintf(__("Are you sure you want to use <%s> [y/N]? "), $resp));
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700859 if (defined $yesno && $yesno =~ /y/i) {
860 return $resp;
861 }
862 }
Jay Soffian6e182512009-03-28 21:39:10 -0400863 }
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -0700864 return;
Jay Soffian6e182512009-03-28 21:39:10 -0400865}
866
Nathan Payreb6049542017-12-15 16:33:39 +0100867sub parse_header_line {
868 my $lines = shift;
869 my $parsed_line = shift;
870 my $addr_pat = join "|", qw(To Cc Bcc);
871
872 foreach (split(/\n/, $lines)) {
873 if (/^($addr_pat):\s*(.+)$/i) {
874 $parsed_line->{$1} = [ parse_address_line($2) ];
875 } elsif (/^([^:]*):\s*(.+)\s*$/i) {
876 $parsed_line->{$1} = $2;
877 }
878 }
879}
880
881sub filter_body {
882 my $c = shift;
883 my $body = "";
884 while (my $body_line = <$c>) {
885 if ($body_line !~ m/^GIT:/) {
886 $body .= $body_line;
887 }
888 }
889 return $body;
890}
891
892
Thomas Rast3cae7e52010-06-17 22:10:39 +0200893my %broken_encoding;
894
Ævar Arnfjörð Bjarmason1d50bfd2010-09-30 13:42:58 +0000895sub file_declares_8bit_cte {
Thomas Rast3cae7e52010-06-17 22:10:39 +0200896 my $fn = shift;
897 open (my $fh, '<', $fn);
898 while (my $line = <$fh>) {
899 last if ($line =~ /^$/);
900 return 1 if ($line =~ /^Content-Transfer-Encoding: .*8bit.*$/);
901 }
902 close $fh;
903 return 0;
904}
905
906foreach my $f (@files) {
907 next unless (body_or_subject_has_nonascii($f)
908 && !file_declares_8bit_cte($f));
909 $broken_encoding{$f} = 1;
910}
911
912if (!defined $auto_8bit_encoding && scalar %broken_encoding) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100913 print __("The following files are 8bit, but do not declare " .
914 "a Content-Transfer-Encoding.\n");
Thomas Rast3cae7e52010-06-17 22:10:39 +0200915 foreach my $f (sort keys %broken_encoding) {
916 print " $f\n";
917 }
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100918 $auto_8bit_encoding = ask(__("Which 8bit encoding should I declare [UTF-8]? "),
Junio C Hamano852a15d2015-02-13 12:20:25 -0800919 valid_re => qr/.{4}/, confirm_only => 1,
Thomas Rast3cae7e52010-06-17 22:10:39 +0200920 default => "UTF-8");
921}
922
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200923if (!$force) {
924 for my $f (@files) {
Ævar Arnfjörð Bjarmason0d290a42010-09-30 13:43:01 +0000925 if (get_patch_subject($f) =~ /\Q*** SUBJECT HERE ***\E/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100926 die sprintf(__("Refusing to send because the patch\n\t%s\n"
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200927 . "has the template subject '*** SUBJECT HERE ***'. "
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100928 . "Pass --force if you really want to send.\n"), $f);
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200929 }
930 }
931}
932
Remi Lespinetc46e27a2015-06-30 14:16:47 +0200933if (defined $sender) {
Remi Lespinetfa5b1aa2015-06-30 14:16:51 +0200934 $sender =~ s/^\s+|\s+$//g;
Remi Lespinetc46e27a2015-06-30 14:16:47 +0200935 ($sender) = expand_aliases($sender);
936} else {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100937 $sender = $repoauthor || $repocommitter || '';
Ryan Anderson83b24432005-07-31 04:17:25 -0400938}
939
Michael S. Tsirkinda187592013-06-05 21:11:00 +0300940# $sender could be an already sanitized address
941# (e.g. sendemail.from could be manually sanitized by user).
942# But it's a no-op to run sanitize_address on an already sanitized address.
943$sender = sanitize_address($sender);
944
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100945my $to_whom = __("To whom should the emails be sent (if anyone)?");
Felipe Contreras8cac13d2012-11-24 12:16:19 +0100946my $prompting = 0;
Junio C Hamano8796ff72010-10-26 22:02:03 -0700947if (!@initial_to && !defined $to_cmd) {
Junio C Hamano0d6b21e2016-04-24 12:31:44 -0700948 my $to = ask("$to_whom ",
Stephen Boyd61837492012-09-06 11:31:11 -0700949 default => "",
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700950 valid_re => qr/\@.*\./, confirm_only => 1);
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700951 push @initial_to, parse_address_line($to) if defined $to; # sanitized/validated later
Ryan Anderson1f038a02005-09-05 01:13:07 -0400952 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -0400953}
954
Eric Wong994d6c62006-05-14 19:13:44 -0700955sub expand_aliases {
Jeff King302e04e2009-07-23 07:09:29 -0400956 return map { expand_one_alias($_) } @_;
957}
958
959my %EXPANDED_ALIASES;
960sub expand_one_alias {
961 my $alias = shift;
962 if ($EXPANDED_ALIASES{$alias}) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100963 die sprintf(__("fatal: alias '%s' expands to itself\n"), $alias);
Jeff King302e04e2009-07-23 07:09:29 -0400964 }
965 local $EXPANDED_ALIASES{$alias} = 1;
966 return $aliases{$alias} ? expand_aliases(@{$aliases{$alias}}) : $alias;
Eric Wong994d6c62006-05-14 19:13:44 -0700967}
968
Remi Lespinetb5e112d2015-06-30 14:16:45 +0200969@initial_to = process_address_list(@initial_to);
970@initial_cc = process_address_list(@initial_cc);
Ævar Arnfjörð Bjarmasone60f6d12019-05-09 13:48:29 +0200971@initial_bcc = process_address_list(@initial_bcc);
Eric Wong994d6c62006-05-14 19:13:44 -0700972
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100973if ($thread && !defined $initial_in_reply_to && $prompting) {
974 $initial_in_reply_to = ask(
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100975 __("Message-ID to be used as In-Reply-To for the first email (if any)? "),
Stephen Boyd61837492012-09-06 11:31:11 -0700976 default => "",
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700977 valid_re => qr/\@.*\./, confirm_only => 1);
Ryan Anderson83b24432005-07-31 04:17:25 -0400978}
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100979if (defined $initial_in_reply_to) {
980 $initial_in_reply_to =~ s/^\s*<?//;
981 $initial_in_reply_to =~ s/>?\s*$//;
982 $initial_in_reply_to = "<$initial_in_reply_to>" if $initial_in_reply_to ne '';
Junio C Hamanoace9c2a2007-12-10 21:44:42 -0800983}
Mike Hommeyace72082007-12-09 18:17:28 +0100984
Christian Ludwigd11c9432018-03-04 00:58:14 +0100985if (defined $reply_to) {
986 $reply_to =~ s/^\s+|\s+$//g;
987 ($reply_to) = expand_aliases($reply_to);
988 $reply_to = sanitize_address($reply_to);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900989}
Eric Wongaca7ad72006-05-15 02:34:44 -0700990
991if (!defined $smtp_server) {
Florian Klink1ab2fd42017-11-28 01:49:04 +0100992 my @sendmail_paths = qw( /usr/sbin/sendmail /usr/lib/sendmail );
993 push @sendmail_paths, map {"$_/sendmail"} split /:/, $ENV{PATH};
994 foreach (@sendmail_paths) {
Eric Wongaca7ad72006-05-15 02:34:44 -0700995 if (-x $_) {
996 $smtp_server = $_;
997 last;
998 }
999 }
1000 $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
Ryan Anderson3342d852005-07-31 20:04:24 -04001001}
1002
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001003if ($compose && $compose > 0) {
1004 @files = ($compose_filename . ".final", @files);
Ryan Anderson1f038a02005-09-05 01:13:07 -04001005}
1006
Ryan Anderson83b24432005-07-31 04:17:25 -04001007# Variables we set as part of the loop over files
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001008our ($message_id, %mail, $subject, $in_reply_to, $references, $message,
Jay Soffiandc1460a2009-03-31 12:22:12 -04001009 $needs_confirm, $message_num, $ask_default);
Ryan Anderson83b24432005-07-31 04:17:25 -04001010
Eric Wong567ffeb2006-03-25 16:47:12 -08001011sub extract_valid_address {
1012 my $address = shift;
Ævar Arnfjörð Bjarmason35b6ab92010-09-30 19:03:31 +00001013 my $local_part_regexp = qr/[^<>"\s@]+/;
1014 my $domain_regexp = qr/[^.<>"\s@]+(?:\.[^.<>"\s@]+)+/;
Eric Wongdb3106b2006-05-15 02:41:01 -07001015
1016 # check for a local address:
Junio C Hamanoad9c18f2006-06-06 00:05:56 -07001017 return $address if ($address =~ /^($local_part_regexp)$/);
Eric Wongdb3106b2006-05-15 02:41:01 -07001018
Uwe Kleine-König155197e2007-08-09 15:27:57 +02001019 $address =~ s/^\s*<(.*)>\s*$/$1/;
Eric Wong567ffeb2006-03-25 16:47:12 -08001020 if ($have_email_valid) {
Junio C Hamanoad9c18f2006-06-06 00:05:56 -07001021 return scalar Email::Valid->address($address);
Eric Wong567ffeb2006-03-25 16:47:12 -08001022 }
Krzysztof Mazur95c0d4b2012-11-22 19:12:09 +01001023
1024 # less robust/correct than the monster regexp in Email::Valid,
1025 # but still does a 99% job, and one less dependency
1026 return $1 if $address =~ /($local_part_regexp\@$domain_regexp)/;
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -07001027 return;
Eric Wong567ffeb2006-03-25 16:47:12 -08001028}
Ryan Anderson83b24432005-07-31 04:17:25 -04001029
Krzysztof Mazure4312252012-11-22 19:12:10 +01001030sub extract_valid_address_or_die {
1031 my $address = shift;
1032 $address = extract_valid_address($address);
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001033 die sprintf(__("error: unable to extract a valid address from: %s\n"), $address)
Krzysztof Mazure4312252012-11-22 19:12:10 +01001034 if !$address;
1035 return $address;
1036}
1037
1038sub validate_address {
1039 my $address = shift;
Krzysztof Mazurd0e98102012-11-22 19:12:12 +01001040 while (!extract_valid_address($address)) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001041 printf STDERR __("error: unable to extract a valid address from: %s\n"), $address;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001042 # TRANSLATORS: Make sure to include [q] [d] [e] in your
1043 # translation. The program will only accept English input
1044 # at this point.
1045 $_ = ask(__("What to do with this address? ([q]uit|[d]rop|[e]dit): "),
Krzysztof Mazurd0e98102012-11-22 19:12:12 +01001046 valid_re => qr/^(?:quit|q|drop|d|edit|e)/i,
Krzysztof Mazur5c80afe2012-11-22 19:12:11 +01001047 default => 'q');
1048 if (/^d/i) {
1049 return undef;
1050 } elsif (/^q/i) {
1051 cleanup_compose_files();
1052 exit(0);
1053 }
Junio C Hamano0d6b21e2016-04-24 12:31:44 -07001054 $address = ask("$to_whom ",
Krzysztof Mazurd0e98102012-11-22 19:12:12 +01001055 default => "",
1056 valid_re => qr/\@.*\./, confirm_only => 1);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001057 }
1058 return $address;
1059}
1060
1061sub validate_address_list {
1062 return (grep { defined $_ }
1063 map { validate_address($_) } @_);
Ryan Anderson83b24432005-07-31 04:17:25 -04001064}
1065
1066# Usually don't need to change anything below here.
1067
1068# we make a "fake" message id by taking the current number
1069# of seconds since the beginning of Unix time and tacking on
1070# a random number to the end, in case we are called quicker than
1071# 1 second since the last time we were called.
Ryan Anderson8037d1a2005-07-31 20:04:24 -04001072
1073# We'll setup a template for the message id, using the "from" address:
Ryan Anderson8037d1a2005-07-31 20:04:24 -04001074
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001075my ($message_id_stamp, $message_id_serial);
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001076sub make_message_id {
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001077 my $uniq;
1078 if (!defined $message_id_stamp) {
Eric Wongf916ab02016-04-06 20:07:14 +00001079 $message_id_stamp = strftime("%Y%m%d%H%M%S.$$", gmtime(time));
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001080 $message_id_serial = 0;
1081 }
1082 $message_id_serial++;
1083 $uniq = "$message_id_stamp-$message_id_serial";
1084
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001085 my $du_part;
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001086 for ($sender, $repocommitter, $repoauthor) {
1087 $du_part = extract_valid_address(sanitize_address($_));
1088 last if (defined $du_part and $du_part ne '');
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001089 }
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001090 if (not defined $du_part or $du_part eq '') {
Ævar Arnfjörð Bjarmason529dd382010-09-30 13:43:08 +00001091 require Sys::Hostname;
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001092 $du_part = 'user@' . Sys::Hostname::hostname();
1093 }
Eric Wongf916ab02016-04-06 20:07:14 +00001094 my $message_id_template = "<%s-%s>";
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001095 $message_id = sprintf($message_id_template, $uniq, $du_part);
Ryan Anderson8037d1a2005-07-31 20:04:24 -04001096 #print "new message id = $message_id\n"; # Was useful for debugging
Ryan Anderson83b24432005-07-31 04:17:25 -04001097}
1098
1099
1100
Eric Wonga5370b12006-03-25 03:01:01 -08001101$time = time - scalar $#files;
Ryan Anderson83b24432005-07-31 04:17:25 -04001102
Jürgen Rühle374c5902007-01-10 13:36:39 -08001103sub unquote_rfc2047 {
1104 local ($_) = @_;
Роман Донченко11f70a72014-12-14 18:59:46 +03001105 my $charset;
Роман Донченкоab47e2a2014-12-14 18:59:47 +03001106 my $sep = qr/[ \t]+/;
1107 s{$re_encoded_word(?:$sep$re_encoded_word)*}{
1108 my @words = split $sep, $&;
1109 foreach (@words) {
1110 m/$re_encoded_word/;
1111 $charset = $1;
1112 my $encoding = $2;
1113 my $text = $3;
1114 if ($encoding eq 'q' || $encoding eq 'Q') {
1115 $_ = $text;
1116 s/_/ /g;
1117 s/=([0-9A-F]{2})/chr(hex($1))/egi;
1118 } else {
1119 # other encodings not supported yet
1120 }
Роман Донченко11f70a72014-12-14 18:59:46 +03001121 }
Роман Донченкоab47e2a2014-12-14 18:59:47 +03001122 join '', @words;
Thomas Rastb622d4d2012-07-30 21:25:40 +02001123 }eg;
Роман Донченко11f70a72014-12-14 18:59:46 +03001124 return wantarray ? ($_, $charset) : $_;
Jürgen Rühle374c5902007-01-10 13:36:39 -08001125}
1126
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001127sub quote_rfc2047 {
1128 local $_ = shift;
Brandon Caseyd1fff6f2009-06-06 20:12:31 -05001129 my $encoding = shift || 'UTF-8';
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001130 s/([^-a-zA-Z0-9!*+\/])/sprintf("=%02X", ord($1))/eg;
1131 s/(.*)/=\?$encoding\?q\?$1\?=/;
1132 return $_;
1133}
1134
Brandon Caseya3a82622009-06-07 19:25:58 -05001135sub is_rfc2047_quoted {
1136 my $s = shift;
Brandon Caseya3a82622009-06-07 19:25:58 -05001137 length($s) <= 75 &&
Роман Донченко11f70a72014-12-14 18:59:46 +03001138 $s =~ m/^(?:"[[:ascii:]]*"|$re_encoded_word)$/o;
Brandon Caseya3a82622009-06-07 19:25:58 -05001139}
1140
Krzysztof Mazurce547802012-10-24 23:08:26 +02001141sub subject_needs_rfc2047_quoting {
1142 my $s = shift;
1143
Krzysztof Mazurce1459f2012-10-24 23:28:29 +02001144 return ($s =~ /[^[:ascii:]]/) || ($s =~ /=\?/);
Krzysztof Mazurce547802012-10-24 23:08:26 +02001145}
1146
1147sub quote_subject {
1148 local $subject = shift;
1149 my $encoding = shift || 'UTF-8';
1150
1151 if (subject_needs_rfc2047_quoting($subject)) {
1152 return quote_rfc2047($subject, $encoding);
1153 }
1154 return $subject;
1155}
1156
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001157# use the simplest quoting being able to handle the recipient
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001158sub sanitize_address {
Robin H. Johnson732263d2007-04-25 19:37:19 -07001159 my ($recipient) = @_;
Krzysztof Mazur831a4882012-11-22 19:12:08 +01001160
1161 # remove garbage after email address
1162 $recipient =~ s/(.*>).*$/$1/;
1163
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001164 my ($recipient_name, $recipient_addr) = ($recipient =~ /^(.*?)\s*(<.*)/);
1165
1166 if (not $recipient_name) {
Ævar Arnfjörð Bjarmasonff483892010-09-30 13:43:02 +00001167 return $recipient;
Robin H. Johnson732263d2007-04-25 19:37:19 -07001168 }
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001169
1170 # if recipient_name is already quoted, do nothing
Brandon Caseya3a82622009-06-07 19:25:58 -05001171 if (is_rfc2047_quoted($recipient_name)) {
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001172 return $recipient;
1173 }
1174
Remi Lespinet1fe97032015-06-30 14:16:49 +02001175 # remove non-escaped quotes
1176 $recipient_name =~ s/(^|[^\\])"/$1/g;
1177
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001178 # rfc2047 is needed if a non-ascii char is included
1179 if ($recipient_name =~ /[^[:ascii:]]/) {
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001180 $recipient_name = quote_rfc2047($recipient_name);
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001181 }
1182
1183 # double quotes are needed if specials or CTLs are included
1184 elsif ($recipient_name =~ /[][()<>@,;:\\".\000-\037\177]/) {
Remi Lespinet1fe97032015-06-30 14:16:49 +02001185 $recipient_name =~ s/([\\\r])/\\$1/g;
Ævar Arnfjörð Bjarmasond5c7d692010-09-30 13:43:03 +00001186 $recipient_name = qq["$recipient_name"];
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001187 }
1188
1189 return "$recipient_name $recipient_addr";
1190
Robin H. Johnson732263d2007-04-25 19:37:19 -07001191}
1192
Matthieu Moycb2922f2017-08-23 12:21:01 +02001193sub strip_garbage_one_address {
1194 my ($addr) = @_;
1195 chomp $addr;
1196 if ($addr =~ /^(("[^"]*"|[^"<]*)? *<[^>]*>).*/) {
1197 # "Foo Bar" <foobar@example.com> [possibly garbage here]
1198 # Foo Bar <foobar@example.com> [possibly garbage here]
1199 return $1;
1200 }
1201 if ($addr =~ /^(<[^>]*>).*/) {
1202 # <foo@example.com> [possibly garbage here]
1203 # if garbage contains other addresses, they are ignored.
1204 return $1;
1205 }
1206 if ($addr =~ /^([^"#,\s]*)/) {
1207 # address without quoting: remove anything after the address
1208 return $1;
1209 }
1210 return $addr;
1211}
1212
Krzysztof Mazure4312252012-11-22 19:12:10 +01001213sub sanitize_address_list {
1214 return (map { sanitize_address($_) } @_);
1215}
1216
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001217sub process_address_list {
Remi Lespinetb1c8a112015-06-30 14:16:50 +02001218 my @addr_list = map { parse_address_line($_) } @_;
1219 @addr_list = expand_aliases(@addr_list);
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001220 @addr_list = sanitize_address_list(@addr_list);
1221 @addr_list = validate_address_list(@addr_list);
1222 return @addr_list;
1223}
1224
Jari Aalto134550f2010-03-14 17:16:45 +02001225# Returns the local Fully Qualified Domain Name (FQDN) if available.
1226#
1227# Tightly configured MTAa require that a caller sends a real DNS
1228# domain name that corresponds the IP address in the HELO/EHLO
1229# handshake. This is used to verify the connection and prevent
1230# spammers from trying to hide their identity. If the DNS and IP don't
Elijah Newren15beaaa2019-11-05 17:07:23 +00001231# match, the receiving MTA may deny the connection.
Jari Aalto134550f2010-03-14 17:16:45 +02001232#
1233# Here is a deny example of Net::SMTP with the default "localhost.localdomain"
1234#
1235# Net::SMTP=GLOB(0x267ec28)>>> EHLO localhost.localdomain
1236# Net::SMTP=GLOB(0x267ec28)<<< 550 EHLO argument does not match calling host
1237#
1238# This maildomain*() code is based on ideas in Perl library Test::Reporter
1239# /usr/share/perl5/Test/Reporter/Mail/Util.pm ==> sub _maildomain ()
1240
Brian Gernhardt59a86302010-04-10 10:53:54 -04001241sub valid_fqdn {
1242 my $domain = shift;
Brandon Casey61ef5e92010-09-26 22:18:01 -05001243 return defined $domain && !($^O eq 'darwin' && $domain =~ /\.local$/) && $domain =~ /\./;
Brian Gernhardt59a86302010-04-10 10:53:54 -04001244}
1245
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001246sub maildomain_net {
Jari Aalto134550f2010-03-14 17:16:45 +02001247 my $maildomain;
1248
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001249 my $domain = Net::Domain::domainname();
1250 $maildomain = $domain if valid_fqdn($domain);
Jari Aalto134550f2010-03-14 17:16:45 +02001251
1252 return $maildomain;
1253}
1254
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001255sub maildomain_mta {
Jari Aalto134550f2010-03-14 17:16:45 +02001256 my $maildomain;
1257
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001258 for my $host (qw(mailhost localhost)) {
1259 my $smtp = Net::SMTP->new($host);
1260 if (defined $smtp) {
1261 my $domain = $smtp->domain;
1262 $smtp->quit;
Jari Aalto134550f2010-03-14 17:16:45 +02001263
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001264 $maildomain = $domain if valid_fqdn($domain);
Jari Aalto134550f2010-03-14 17:16:45 +02001265
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001266 last if $maildomain;
Jari Aalto134550f2010-03-14 17:16:45 +02001267 }
1268 }
1269
1270 return $maildomain;
1271}
1272
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001273sub maildomain {
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001274 return maildomain_net() || maildomain_mta() || 'localhost.localdomain';
Jari Aalto134550f2010-03-14 17:16:45 +02001275}
1276
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001277sub smtp_host_string {
1278 if (defined $smtp_server_port) {
1279 return "$smtp_server:$smtp_server_port";
1280 } else {
1281 return $smtp_server;
1282 }
1283}
1284
1285# Returns 1 if authentication succeeded or was not necessary
1286# (smtp_user was not specified), and 0 otherwise.
1287
1288sub smtp_auth_maybe {
Joshua Watt8dd9b3f2018-10-22 22:24:25 -05001289 if (!defined $smtp_authuser || $auth || (defined $smtp_auth && $smtp_auth eq "none")) {
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001290 return 1;
1291 }
1292
1293 # Workaround AUTH PLAIN/LOGIN interaction defect
1294 # with Authen::SASL::Cyrus
1295 eval {
1296 require Authen::SASL;
1297 Authen::SASL->import(qw(Perl));
1298 };
1299
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001300 # Check mechanism naming as defined in:
1301 # https://tools.ietf.org/html/rfc4422#page-8
Brian Norris904f6e72015-09-18 15:12:50 -07001302 if ($smtp_auth && $smtp_auth !~ /^(\b[A-Z0-9-_]{1,20}\s*)*$/) {
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001303 die "invalid smtp auth: '${smtp_auth}'";
1304 }
1305
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001306 # TODO: Authentication may fail not because credentials were
1307 # invalid but due to other reasons, in which we should not
1308 # reject credentials.
1309 $auth = Git::credential({
1310 'protocol' => 'smtp',
1311 'host' => smtp_host_string(),
1312 'username' => $smtp_authuser,
1313 # if there's no password, "git credential fill" will
1314 # give us one, otherwise it'll just pass this one.
1315 'password' => $smtp_authpass
1316 }, sub {
1317 my $cred = shift;
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001318
1319 if ($smtp_auth) {
1320 my $sasl = Authen::SASL->new(
1321 mechanism => $smtp_auth,
1322 callback => {
1323 user => $cred->{'username'},
1324 pass => $cred->{'password'},
1325 authname => $cred->{'username'},
1326 }
1327 );
1328
1329 return !!$smtp->auth($sasl);
1330 }
1331
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001332 return !!$smtp->auth($cred->{'username'}, $cred->{'password'});
1333 });
1334
1335 return $auth;
1336}
1337
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001338sub ssl_verify_params {
1339 eval {
1340 require IO::Socket::SSL;
1341 IO::Socket::SSL->import(qw/SSL_VERIFY_PEER SSL_VERIFY_NONE/);
1342 };
1343 if ($@) {
1344 print STDERR "Not using SSL_VERIFY_PEER due to out-of-date IO::Socket::SSL.\n";
1345 return;
1346 }
1347
1348 if (!defined $smtp_ssl_cert_path) {
Ruben Kerkhof01645b72014-01-15 21:31:11 +04001349 # use the OpenSSL defaults
1350 return (SSL_verify_mode => SSL_VERIFY_PEER());
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001351 }
1352
1353 if ($smtp_ssl_cert_path eq "") {
1354 return (SSL_verify_mode => SSL_VERIFY_NONE());
1355 } elsif (-d $smtp_ssl_cert_path) {
1356 return (SSL_verify_mode => SSL_VERIFY_PEER(),
1357 SSL_ca_path => $smtp_ssl_cert_path);
1358 } elsif (-f $smtp_ssl_cert_path) {
1359 return (SSL_verify_mode => SSL_VERIFY_PEER(),
1360 SSL_ca_file => $smtp_ssl_cert_path);
1361 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001362 die sprintf(__("CA path \"%s\" does not exist"), $smtp_ssl_cert_path);
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001363 }
1364}
1365
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001366sub file_name_is_absolute {
1367 my ($path) = @_;
1368
1369 # msys does not grok DOS drive-prefixes
1370 if ($^O eq 'msys') {
Junio C Hamanof24ecf52014-04-23 09:37:38 -07001371 return ($path =~ m#^/# || $path =~ m#^[a-zA-Z]\:#)
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001372 }
1373
1374 require File::Spec::Functions;
1375 return File::Spec::Functions::file_name_is_absolute($path);
1376}
1377
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001378# Prepares the email, then asks the user what to do.
1379#
1380# If the user chooses to send the email, it's sent and 1 is returned.
1381# If the user chooses not to send the email, 0 is returned.
1382# If the user decides they want to make further edits, -1 is returned and the
1383# caller is expected to call send_message again after the edits are performed.
1384#
1385# If an error occurs sending the email, this just dies.
Michael Witten15da1082009-04-13 13:23:51 -05001386
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001387sub send_message {
Eric Wong4bc87a22006-03-25 17:20:48 -08001388 my @recipients = unique_email_list(@to);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001389 @cc = (grep { my $cc = extract_valid_address_or_die($_);
Joe Perches83acaae2010-11-20 15:06:05 -08001390 not grep { $cc eq $_ || $_ =~ /<\Q${cc}\E>$/ } @recipients
Ask Bjørn Hansen7ac17522007-11-19 03:00:26 -08001391 }
Ask Bjørn Hansen7ac17522007-11-19 03:00:26 -08001392 @cc);
Eric Wong4bc87a22006-03-25 17:20:48 -08001393 my $to = join (",\n\t", @recipients);
Ævar Arnfjörð Bjarmasone60f6d12019-05-09 13:48:29 +02001394 @recipients = unique_email_list(@recipients,@cc,@initial_bcc);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001395 @recipients = (map { extract_valid_address_or_die($_) } @recipients);
Jakub Narebski6bdca892006-07-07 20:57:55 +02001396 my $date = format_2822_time($time++);
Martin Langhoffe923eff2006-05-03 09:44:36 +12001397 my $gitversion = '@@GIT_VERSION@@';
1398 if ($gitversion =~ m/..GIT_VERSION../) {
Petr Baudis3cb8caf2006-07-03 22:47:58 +02001399 $gitversion = Git::version();
Martin Langhoffe923eff2006-05-03 09:44:36 +12001400 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001401
Joe Perches02461e02009-10-08 10:03:26 -07001402 my $cc = join(",\n\t", unique_email_list(@cc));
Junio C Hamanof06a6a42007-04-16 16:51:47 -07001403 my $ccline = "";
1404 if ($cc ne '') {
1405 $ccline = "\nCc: $cc";
1406 }
Jeff King4f3d3702007-12-17 15:51:34 -05001407 make_message_id() unless defined($message_id);
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001408
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001409 my $header = "From: $sender
Junio C Hamanof06a6a42007-04-16 16:51:47 -07001410To: $to${ccline}
Eric Wong4bc87a22006-03-25 17:20:48 -08001411Subject: $subject
Eric Wong4bc87a22006-03-25 17:20:48 -08001412Date: $date
1413Message-Id: $message_id
Eric Wong4bc87a22006-03-25 17:20:48 -08001414";
Luis Henriquesac1596a2014-03-24 21:38:27 +00001415 if ($use_xmailer) {
1416 $header .= "X-Mailer: git-send-email $gitversion\n";
1417 }
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001418 if ($in_reply_to) {
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001419
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001420 $header .= "In-Reply-To: $in_reply_to\n";
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001421 $header .= "References: $references\n";
1422 }
Christian Ludwigd11c9432018-03-04 00:58:14 +01001423 if ($reply_to) {
1424 $header .= "Reply-To: $reply_to\n";
1425 }
Junio C Hamanoce91c2f2006-10-05 16:36:49 -07001426 if (@xh) {
1427 $header .= join("\n", @xh) . "\n";
1428 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001429
Robin H. Johnsonc38f0242007-04-25 19:37:20 -07001430 my @sendmail_parameters = ('-i', @recipients);
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001431 my $raw_from = $sender;
Felipe Contrerasc89e3242009-11-26 21:04:29 +02001432 if (defined $envelope_sender && $envelope_sender ne "auto") {
1433 $raw_from = $envelope_sender;
1434 }
Robin H. Johnsonf073a592007-04-25 19:37:22 -07001435 $raw_from = extract_valid_address($raw_from);
1436 unshift (@sendmail_parameters,
1437 '-f', $raw_from) if(defined $envelope_sender);
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001438
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001439 if ($needs_confirm && !$dry_run) {
1440 print "\n$header\n";
1441 if ($needs_confirm eq "inform") {
1442 $confirm_unconfigured = 0; # squelch this message for the rest of this run
Jay Soffian6e182512009-03-28 21:39:10 -04001443 $ask_default = "y"; # assume yes on EOF since user hasn't explicitly asked for confirmation
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001444 print __ <<EOF ;
1445 The Cc list above has been expanded by additional
1446 addresses found in the patch commit message. By default
1447 send-email prompts before sending whenever this occurs.
1448 This behavior is controlled by the sendemail.confirm
1449 configuration setting.
1450
1451 For additional information, run 'git send-email --help'.
1452 To retain the current behavior, but squelch this message,
1453 run 'git config --global sendemail.confirm auto'.
1454
1455EOF
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001456 }
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001457 # TRANSLATORS: Make sure to include [y] [n] [e] [q] [a] in your
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001458 # translation. The program will only accept English input
1459 # at this point.
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001460 $_ = ask(__("Send this email? ([y]es|[n]o|[e]dit|[q]uit|[a]ll): "),
1461 valid_re => qr/^(?:yes|y|no|n|edit|e|quit|q|all|a)/i,
Jay Soffian6e182512009-03-28 21:39:10 -04001462 default => $ask_default);
Vasco Almeida46493102016-12-14 11:54:36 -01001463 die __("Send this email reply required") unless defined $_;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001464 if (/^n/i) {
Michael Witten15da1082009-04-13 13:23:51 -05001465 return 0;
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001466 } elsif (/^e/i) {
1467 return -1;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001468 } elsif (/^q/i) {
1469 cleanup_compose_files();
1470 exit(0);
1471 } elsif (/^a/i) {
1472 $confirm = 'never';
1473 }
1474 }
1475
Pascal Obry052fbea2010-09-06 20:12:11 +02001476 unshift (@sendmail_parameters, @smtp_server_options);
1477
Matthew Wilcox61302592006-10-10 08:58:23 -06001478 if ($dry_run) {
1479 # We don't want to send the email.
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001480 } elsif (file_name_is_absolute($smtp_server)) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001481 my $pid = open my $sm, '|-';
1482 defined $pid or die $!;
1483 if (!$pid) {
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001484 exec($smtp_server, @sendmail_parameters) or die $!;
Eric Wongaca7ad72006-05-15 02:34:44 -07001485 }
1486 print $sm "$header\n$message";
Ævar Arnfjörð Bjarmason5e2c2ab2010-09-30 13:43:07 +00001487 close $sm or die $!;
Eric Wongaca7ad72006-05-15 02:34:44 -07001488 } else {
Junio C Hamano44b24762007-09-25 17:27:54 -07001489
1490 if (!defined $smtp_server) {
Vasco Almeida46493102016-12-14 11:54:36 -01001491 die __("The required SMTP server is not properly defined.")
Junio C Hamano44b24762007-09-25 17:27:54 -07001492 }
1493
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001494 require Net::SMTP;
Jonathan Niederbfbfc9a2017-05-31 17:17:43 -07001495 my $use_net_smtp_ssl = version->parse($Net::SMTP::VERSION) < version->parse("2.34");
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001496 $smtp_domain ||= maildomain();
1497
Thomas Rastf6bebd12008-06-25 21:42:43 +02001498 if ($smtp_encryption eq 'ssl') {
Junio C Hamano44b24762007-09-25 17:27:54 -07001499 $smtp_server_port ||= 465; # ssmtp
Thomas Rast5508f3e2013-12-01 23:48:43 +01001500 require IO::Socket::SSL;
John Keeping9d605242015-12-03 21:47:18 +00001501
1502 # Suppress "variable accessed once" warning.
1503 {
1504 no warnings 'once';
1505 $IO::Socket::SSL::DEBUG = 1;
1506 }
1507
Thomas Rast5508f3e2013-12-01 23:48:43 +01001508 # Net::SMTP::SSL->new() does not forward any SSL options
1509 IO::Socket::SSL::set_client_defaults(
1510 ssl_verify_params());
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001511
1512 if ($use_net_smtp_ssl) {
1513 require Net::SMTP::SSL;
1514 $smtp ||= Net::SMTP::SSL->new($smtp_server,
1515 Hello => $smtp_domain,
1516 Port => $smtp_server_port,
1517 Debug => $debug_net_smtp);
1518 }
1519 else {
1520 $smtp ||= Net::SMTP->new($smtp_server,
1521 Hello => $smtp_domain,
1522 Port => $smtp_server_port,
1523 Debug => $debug_net_smtp,
1524 SSL => 1);
1525 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001526 }
Jules Maselbas636f3d72018-07-14 10:58:48 +02001527 elsif (!$smtp) {
brian m. carlson1a741bf2013-07-04 22:04:52 +00001528 $smtp_server_port ||= 25;
1529 $smtp ||= Net::SMTP->new($smtp_server,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001530 Hello => $smtp_domain,
brian m. carlson1a741bf2013-07-04 22:04:52 +00001531 Debug => $debug_net_smtp,
1532 Port => $smtp_server_port);
Yakov Lernerfb3650e2009-09-25 15:10:21 -07001533 if ($smtp_encryption eq 'tls' && $smtp) {
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001534 if ($use_net_smtp_ssl) {
1535 $smtp->command('STARTTLS');
1536 $smtp->response();
1537 if ($smtp->code != 220) {
1538 die sprintf(__("Server does not support STARTTLS! %s"), $smtp->message);
1539 }
1540 require Net::SMTP::SSL;
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001541 $smtp = Net::SMTP::SSL->start_SSL($smtp,
1542 ssl_verify_params())
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001543 or die sprintf(__("STARTTLS failed! %s"), IO::Socket::SSL::errstr());
Thomas Rastf6bebd12008-06-25 21:42:43 +02001544 }
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001545 else {
1546 $smtp->starttls(ssl_verify_params())
1547 or die sprintf(__("STARTTLS failed! %s"), IO::Socket::SSL::errstr());
1548 }
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001549 # Send EHLO again to receive fresh
1550 # supported commands
1551 $smtp->hello($smtp_domain);
Thomas Rastf6bebd12008-06-25 21:42:43 +02001552 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001553 }
Junio C Hamano44b24762007-09-25 17:27:54 -07001554
1555 if (!$smtp) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001556 die __("Unable to initialize SMTP properly. Check config and use --smtp-debug."),
1557 " VALUES: server=$smtp_server ",
Jari Aaltoe5afb3a2010-03-14 17:15:33 +02001558 "encryption=$smtp_encryption ",
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001559 "hello=$smtp_domain",
Sylvain Rabota1dd7e12011-04-29 20:23:24 +02001560 defined $smtp_server_port ? " port=$smtp_server_port" : "";
Junio C Hamano44b24762007-09-25 17:27:54 -07001561 }
1562
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001563 smtp_auth_maybe or die $smtp->message;
Michael Witten2363d742008-02-03 19:53:56 -05001564
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001565 $smtp->mail( $raw_from ) or die $smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001566 $smtp->to( @recipients ) or die $smtp->message;
1567 $smtp->data or die $smtp->message;
Stefan Agnerf60c4832015-09-30 09:26:09 +02001568 $smtp->datasend("$header\n") or die $smtp->message;
1569 my @lines = split /^/, $message;
1570 foreach my $line (@lines) {
1571 $smtp->datasend("$line") or die $smtp->message;
1572 }
Eric Wongaca7ad72006-05-15 02:34:44 -07001573 $smtp->dataend() or die $smtp->message;
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001574 $smtp->code =~ /250|200/ or die sprintf(__("Failed to send %s\n"), $subject).$smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001575 }
Ryan Anderson27184352006-02-05 20:13:52 -05001576 if ($quiet) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001577 printf($dry_run ? __("Dry-Sent %s\n") : __("Sent %s\n"), $subject);
Ryan Anderson27184352006-02-05 20:13:52 -05001578 } else {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001579 print($dry_run ? __("Dry-OK. Log says:\n") : __("OK. Log says:\n"));
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001580 if (!file_name_is_absolute($smtp_server)) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001581 print "Server: $smtp_server\n";
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001582 print "MAIL FROM:<$raw_from>\n";
Joe Perches02461e02009-10-08 10:03:26 -07001583 foreach my $entry (@recipients) {
1584 print "RCPT TO:<$entry>\n";
1585 }
Eric Wongaca7ad72006-05-15 02:34:44 -07001586 } else {
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001587 print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001588 }
David D. Kilzerb7f30e02007-11-18 20:14:55 -08001589 print $header, "\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001590 if ($smtp) {
Vasco Almeida46493102016-12-14 11:54:36 -01001591 print __("Result: "), $smtp->code, ' ',
Eric Wongaca7ad72006-05-15 02:34:44 -07001592 ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
1593 } else {
Vasco Almeida46493102016-12-14 11:54:36 -01001594 print __("Result: OK\n");
Eric Wongaca7ad72006-05-15 02:34:44 -07001595 }
Ryan Anderson30d08b32006-02-02 11:56:06 -05001596 }
Michael Witten15da1082009-04-13 13:23:51 -05001597
1598 return 1;
Ryan Anderson83b24432005-07-31 04:17:25 -04001599}
1600
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001601$in_reply_to = $initial_in_reply_to;
1602$references = $initial_in_reply_to || '';
Ryan Anderson83b24432005-07-31 04:17:25 -04001603$subject = $initial_subject;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001604$message_num = 0;
Ryan Anderson83b24432005-07-31 04:17:25 -04001605
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001606# Prepares the email, prompts the user, sends it out
1607# Returns 0 if an edit was done and the function should be called again, or 1
1608# otherwise.
1609sub process_file {
1610 my ($t) = @_;
1611
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001612 open my $fh, "<", $t or die sprintf(__("can't open file %s"), $t);
Ryan Anderson83b24432005-07-31 04:17:25 -04001613
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001614 my $author = undef;
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001615 my $sauthor = undef;
Jeff King8291db62007-11-16 05:49:09 -05001616 my $author_encoding;
1617 my $has_content_type;
1618 my $body_encoding;
Paolo Bonzinibb294562014-11-25 15:00:26 +01001619 my $xfer_encoding;
1620 my $has_mime_version;
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001621 @to = ();
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001622 @cc = ();
Junio C Hamanoce91c2f2006-10-05 16:36:49 -07001623 @xh = ();
Junio C Hamanoe6b09642006-10-07 03:09:05 -07001624 my $input_format = undef;
Jay Soffian50126992009-02-14 23:32:14 -05001625 my @header = ();
Ryan Anderson83b24432005-07-31 04:17:25 -04001626 $message = "";
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001627 $message_num++;
Jay Soffian50126992009-02-14 23:32:14 -05001628 # First unfold multiline header fields
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001629 while(<$fh>) {
Jay Soffian50126992009-02-14 23:32:14 -05001630 last if /^\s*$/;
1631 if (/^\s+\S/ and @header) {
1632 chomp($header[$#header]);
1633 s/^\s+/ /;
1634 $header[$#header] .= $_;
1635 } else {
1636 push(@header, $_);
1637 }
1638 }
1639 # Now parse the header
1640 foreach(@header) {
1641 if (/^From /) {
1642 $input_format = 'mbox';
1643 next;
1644 }
1645 chomp;
1646 if (!defined $input_format && /^[-A-Za-z]+:\s/) {
1647 $input_format = 'mbox';
1648 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001649
Jay Soffian50126992009-02-14 23:32:14 -05001650 if (defined $input_format && $input_format eq 'mbox') {
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001651 if (/^Subject:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001652 $subject = $1;
1653 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001654 elsif (/^From:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001655 ($author, $author_encoding) = unquote_rfc2047($1);
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001656 $sauthor = sanitize_address($author);
Jay Soffian50126992009-02-14 23:32:14 -05001657 next if $suppress_cc{'author'};
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001658 next if $suppress_cc{'self'} and $sauthor eq $sender;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001659 printf(__("(mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001660 $1, $_) unless $quiet;
1661 push @cc, $1;
1662 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001663 elsif (/^To:\s+(.*)$/i) {
Stephen Boyd21802cd2010-09-29 00:26:44 -07001664 foreach my $addr (parse_address_line($1)) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001665 printf(__("(mbox) Adding to: %s from line '%s'\n"),
Stephen Boyd21802cd2010-09-29 00:26:44 -07001666 $addr, $_) unless $quiet;
Krzysztof Mazure4312252012-11-22 19:12:10 +01001667 push @to, $addr;
Stephen Boyd21802cd2010-09-29 00:26:44 -07001668 }
1669 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001670 elsif (/^Cc:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001671 foreach my $addr (parse_address_line($1)) {
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001672 my $qaddr = unquote_rfc2047($addr);
1673 my $saddr = sanitize_address($qaddr);
1674 if ($saddr eq $sender) {
David Brown65648282007-12-25 19:56:29 -08001675 next if ($suppress_cc{'self'});
David Brown65648282007-12-25 19:56:29 -08001676 } else {
1677 next if ($suppress_cc{'cc'});
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001678 }
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001679 printf(__("(mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001680 $addr, $_) unless $quiet;
1681 push @cc, $addr;
Ryan Anderson83b24432005-07-31 04:17:25 -04001682 }
1683 }
Jay Soffian50126992009-02-14 23:32:14 -05001684 elsif (/^Content-type:/i) {
1685 $has_content_type = 1;
1686 if (/charset="?([^ "]+)/) {
1687 $body_encoding = $1;
1688 }
1689 push @xh, $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001690 }
Paolo Bonzinibb294562014-11-25 15:00:26 +01001691 elsif (/^MIME-Version/i) {
1692 $has_mime_version = 1;
1693 push @xh, $_;
1694 }
Jay Soffian50126992009-02-14 23:32:14 -05001695 elsif (/^Message-Id: (.*)/i) {
1696 $message_id = $1;
1697 }
Paolo Bonzinibb294562014-11-25 15:00:26 +01001698 elsif (/^Content-Transfer-Encoding: (.*)/i) {
1699 $xfer_encoding = $1 if not defined $xfer_encoding;
1700 }
Stefan Agner256be1d2018-04-17 23:16:30 +02001701 elsif (/^In-Reply-To: (.*)/i) {
1702 $in_reply_to = $1;
1703 }
1704 elsif (/^References: (.*)/i) {
1705 $references = $1;
1706 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001707 elsif (!/^Date:\s/i && /^[-A-Za-z]+:\s+\S/) {
Jay Soffian50126992009-02-14 23:32:14 -05001708 push @xh, $_;
1709 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001710 } else {
Jay Soffian50126992009-02-14 23:32:14 -05001711 # In the traditional
1712 # "send lots of email" format,
1713 # line 1 = cc
1714 # line 2 = subject
1715 # So let's support that, too.
1716 $input_format = 'lots';
1717 if (@cc == 0 && !$suppress_cc{'cc'}) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001718 printf(__("(non-mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001719 $_, $_) unless $quiet;
1720 push @cc, $_;
1721 } elsif (!defined $subject) {
1722 $subject = $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001723 }
1724 }
1725 }
Jay Soffian50126992009-02-14 23:32:14 -05001726 # Now parse the message body
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001727 while(<$fh>) {
Jay Soffian50126992009-02-14 23:32:14 -05001728 $message .= $_;
Baruch Siach9c545812019-03-16 21:26:50 +02001729 if (/^([a-z][a-z-]*-by|Cc): (.*)/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001730 chomp;
Jay Soffian3531e272009-02-14 23:32:15 -05001731 my ($what, $c) = ($1, $2);
Matthieu Moycb2922f2017-08-23 12:21:01 +02001732 # strip garbage for the address we'll use:
1733 $c = strip_garbage_one_address($c);
1734 # sanitize a bit more to decide whether to suppress the address:
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001735 my $sc = sanitize_address($c);
1736 if ($sc eq $sender) {
Jay Soffian3531e272009-02-14 23:32:15 -05001737 next if ($suppress_cc{'self'});
1738 } else {
Rasmus Villemoesef0cc1d2018-10-16 09:39:23 +02001739 if ($what =~ /^Signed-off-by$/i) {
1740 next if $suppress_cc{'sob'};
1741 } elsif ($what =~ /-by$/i) {
1742 next if $suppress_cc{'misc-by'};
1743 } elsif ($what =~ /Cc/i) {
1744 next if $suppress_cc{'bodycc'};
1745 }
Jay Soffian3531e272009-02-14 23:32:15 -05001746 }
Rasmus Villemoesfb34fe62018-10-10 13:13:50 +02001747 if ($c !~ /.+@.+|<.+>/) {
1748 printf("(body) Ignoring %s from line '%s'\n",
1749 $what, $_) unless $quiet;
1750 next;
1751 }
Jay Soffian50126992009-02-14 23:32:14 -05001752 push @cc, $c;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001753 printf(__("(body) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001754 $c, $_) unless $quiet;
1755 }
1756 }
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001757 close $fh;
Joe Perches324a8bd2007-08-17 18:51:12 -07001758
Joe Perches6e74e072010-09-24 10:03:00 -07001759 push @to, recipients_cmd("to-cmd", "to", $to_cmd, $t)
1760 if defined $to_cmd;
1761 push @cc, recipients_cmd("cc-cmd", "cc", $cc_cmd, $t)
1762 if defined $cc_cmd && !$suppress_cc{'cccmd'};
Joe Perches324a8bd2007-08-17 18:51:12 -07001763
Thomas Rast3cae7e52010-06-17 22:10:39 +02001764 if ($broken_encoding{$t} && !$has_content_type) {
Paolo Bonzinibb294562014-11-25 15:00:26 +01001765 $xfer_encoding = '8bit' if not defined $xfer_encoding;
Thomas Rast3cae7e52010-06-17 22:10:39 +02001766 $has_content_type = 1;
Paolo Bonzinibb294562014-11-25 15:00:26 +01001767 push @xh, "Content-Type: text/plain; charset=$auto_8bit_encoding";
Thomas Rast3cae7e52010-06-17 22:10:39 +02001768 $body_encoding = $auto_8bit_encoding;
1769 }
1770
Krzysztof Mazurce547802012-10-24 23:08:26 +02001771 if ($broken_encoding{$t} && !is_rfc2047_quoted($subject)) {
1772 $subject = quote_subject($subject, $auto_8bit_encoding);
Thomas Rast3cae7e52010-06-17 22:10:39 +02001773 }
1774
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001775 if (defined $sauthor and $sauthor ne $sender) {
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001776 $message = "From: $author\n\n$message";
Jeff King8291db62007-11-16 05:49:09 -05001777 if (defined $author_encoding) {
1778 if ($has_content_type) {
1779 if ($body_encoding eq $author_encoding) {
1780 # ok, we already have the right encoding
1781 }
1782 else {
1783 # uh oh, we should re-encode
1784 }
1785 }
1786 else {
Paolo Bonzinibb294562014-11-25 15:00:26 +01001787 $xfer_encoding = '8bit' if not defined $xfer_encoding;
Thomas Rast3cae7e52010-06-17 22:10:39 +02001788 $has_content_type = 1;
Jeff King8291db62007-11-16 05:49:09 -05001789 push @xh,
Paolo Bonzinibb294562014-11-25 15:00:26 +01001790 "Content-Type: text/plain; charset=$author_encoding";
Jeff King8291db62007-11-16 05:49:09 -05001791 }
1792 }
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001793 }
brian m. carlsone67a2282018-07-08 22:17:12 +00001794 $xfer_encoding = '8bit' if not defined $xfer_encoding;
1795 ($message, $xfer_encoding) = apply_transfer_encoding(
1796 $message, $xfer_encoding, $target_xfer_encoding);
1797 push @xh, "Content-Transfer-Encoding: $xfer_encoding";
1798 unshift @xh, 'MIME-Version: 1.0' unless $has_mime_version;
Ryan Anderson83b24432005-07-31 04:17:25 -04001799
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001800 $needs_confirm = (
1801 $confirm eq "always" or
1802 ($confirm =~ /^(?:auto|cc)$/ && @cc) or
1803 ($confirm =~ /^(?:auto|compose)$/ && $compose && $message_num == 1));
1804 $needs_confirm = "inform" if ($needs_confirm && $confirm_unconfigured && @cc);
1805
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001806 @to = process_address_list(@to);
1807 @cc = process_address_list(@cc);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001808
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001809 @to = (@initial_to, @to);
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001810 @cc = (@initial_cc, @cc);
1811
Michael S. Tsirkinf515c902014-04-29 08:41:16 +03001812 if ($message_num == 1) {
1813 if (defined $cover_cc and $cover_cc) {
1814 @initial_cc = @cc;
1815 }
1816 if (defined $cover_to and $cover_to) {
1817 @initial_to = @to;
1818 }
1819 }
1820
Michael Witten15da1082009-04-13 13:23:51 -05001821 my $message_was_sent = send_message();
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001822 if ($message_was_sent == -1) {
1823 do_edit($t);
1824 return 0;
1825 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001826
1827 # set up for the next message
Junio C Hamano95a877a2009-06-12 09:23:43 -07001828 if ($thread && $message_was_sent &&
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001829 ($chain_reply_to || !defined $in_reply_to || length($in_reply_to) == 0 ||
Antonio Ospitedb54c8e2010-11-12 15:55:08 +01001830 $message_num == 1)) {
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001831 $in_reply_to = $message_id;
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001832 if (length $references > 0) {
YOSHIFUJI Hideaki / 吉藤英明a925b892007-04-06 08:50:24 +09001833 $references .= "\n $message_id";
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001834 } else {
1835 $references = "$message_id";
1836 }
Ryan Anderson78488b22005-07-31 20:04:24 -04001837 }
Jeff King4f3d3702007-12-17 15:51:34 -05001838 $message_id = undef;
xiaoqiang zhao5453b832017-05-21 20:59:50 +08001839 $num_sent++;
1840 if (defined $batch_size && $num_sent == $batch_size) {
1841 $num_sent = 0;
1842 $smtp->quit if defined $smtp;
1843 undef $smtp;
1844 undef $auth;
1845 sleep($relogin_delay) if defined $relogin_delay;
1846 }
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001847
1848 return 1;
1849}
1850
1851foreach my $t (@files) {
1852 while (!process_file($t)) {
1853 # user edited the file
1854 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001855}
Ryan Andersone2057352005-08-02 21:45:22 -04001856
Joe Perches6e74e072010-09-24 10:03:00 -07001857# Execute a command (e.g. $to_cmd) to get a list of email addresses
1858# and return a results array
1859sub recipients_cmd {
1860 my ($prefix, $what, $cmd, $file) = @_;
1861
Joe Perches6e74e072010-09-24 10:03:00 -07001862 my @addresses = ();
Ramkumar Ramachandraa47eab02013-03-31 18:40:42 -07001863 open my $fh, "-|", "$cmd \Q$file\E"
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001864 or die sprintf(__("(%s) Could not execute '%s'"), $prefix, $cmd);
Junio C Hamano7ebee442010-10-26 22:02:52 -07001865 while (my $address = <$fh>) {
Joe Perches6e74e072010-09-24 10:03:00 -07001866 $address =~ s/^\s*//g;
1867 $address =~ s/\s*$//g;
1868 $address = sanitize_address($address);
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001869 next if ($address eq $sender and $suppress_cc{'self'});
Joe Perches6e74e072010-09-24 10:03:00 -07001870 push @addresses, $address;
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001871 printf(__("(%s) Adding %s: %s from: '%s'\n"),
1872 $prefix, $what, $address, $cmd) unless $quiet;
Joe Perches6e74e072010-09-24 10:03:00 -07001873 }
Junio C Hamano7ebee442010-10-26 22:02:52 -07001874 close $fh
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001875 or die sprintf(__("(%s) failed to close pipe to '%s'"), $prefix, $cmd);
Joe Perches6e74e072010-09-24 10:03:00 -07001876 return @addresses;
1877}
1878
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001879cleanup_compose_files();
Ryan Anderson1f038a02005-09-05 01:13:07 -04001880
Ævar Arnfjörð Bjarmason4bf597e2010-09-30 13:43:00 +00001881sub cleanup_compose_files {
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001882 unlink($compose_filename, $compose_filename . ".final") if $compose;
Ryan Anderson1f038a02005-09-05 01:13:07 -04001883}
1884
Eric Wong4bc87a22006-03-25 17:20:48 -08001885$smtp->quit if $smtp;
Ryan Andersone2057352005-08-02 21:45:22 -04001886
Paolo Bonzini8d814082014-11-25 15:00:27 +01001887sub apply_transfer_encoding {
1888 my $message = shift;
1889 my $from = shift;
1890 my $to = shift;
1891
Aaron Lindsay3c88e462018-11-02 05:52:38 -04001892 return ($message, $to) if ($from eq $to and $from ne '7bit');
Paolo Bonzini8d814082014-11-25 15:00:27 +01001893
1894 require MIME::QuotedPrint;
1895 require MIME::Base64;
1896
1897 $message = MIME::QuotedPrint::decode($message)
1898 if ($from eq 'quoted-printable');
1899 $message = MIME::Base64::decode($message)
1900 if ($from eq 'base64');
1901
brian m. carlson74d76a12019-04-13 22:45:51 +00001902 $to = ($message =~ /(?:.{999,}|\r)/) ? 'quoted-printable' : '8bit'
brian m. carlson7a369872018-07-08 22:17:10 +00001903 if $to eq 'auto';
1904
Vasco Almeida46493102016-12-14 11:54:36 -01001905 die __("cannot send message as 7bit")
Paolo Bonzini8d814082014-11-25 15:00:27 +01001906 if ($to eq '7bit' and $message =~ /[^[:ascii:]]/);
brian m. carlson7a369872018-07-08 22:17:10 +00001907 return ($message, $to)
Paolo Bonzini8d814082014-11-25 15:00:27 +01001908 if ($to eq '7bit' or $to eq '8bit');
brian m. carlson7a369872018-07-08 22:17:10 +00001909 return (MIME::QuotedPrint::encode($message, "\n", 0), $to)
Paolo Bonzini8d814082014-11-25 15:00:27 +01001910 if ($to eq 'quoted-printable');
brian m. carlson7a369872018-07-08 22:17:10 +00001911 return (MIME::Base64::encode($message, "\n"), $to)
Paolo Bonzini8d814082014-11-25 15:00:27 +01001912 if ($to eq 'base64');
Vasco Almeida46493102016-12-14 11:54:36 -01001913 die __("invalid transfer encoding");
Paolo Bonzini8d814082014-11-25 15:00:27 +01001914}
1915
Ævar Arnfjörð Bjarmasonc438ea22010-09-30 13:42:59 +00001916sub unique_email_list {
Ryan Andersone2057352005-08-02 21:45:22 -04001917 my %seen;
1918 my @emails;
1919
1920 foreach my $entry (@_) {
Krzysztof Mazure4312252012-11-22 19:12:10 +01001921 my $clean = extract_valid_address_or_die($entry);
1922 $seen{$clean} ||= 0;
1923 next if $seen{$clean}++;
1924 push @emails, $entry;
Ryan Andersone2057352005-08-02 21:45:22 -04001925 }
1926 return @emails;
1927}
Jeff King747bbff2008-01-18 09:19:48 -05001928
1929sub validate_patch {
brian m. carlsonf2d06fb2018-07-08 22:17:11 +00001930 my ($fn, $xfer_encoding) = @_;
Jonathan Tan64896602017-05-12 15:38:26 -07001931
Jonathan Tan177409e2017-06-01 16:50:55 -07001932 if ($repo) {
1933 my $validate_hook = catfile(catdir($repo->repo_path(), 'hooks'),
1934 'sendemail-validate');
1935 my $hook_error;
1936 if (-x $validate_hook) {
1937 my $target = abs_path($fn);
1938 # The hook needs a correct cwd and GIT_DIR.
1939 my $cwd_save = cwd();
1940 chdir($repo->wc_path() or $repo->repo_path())
1941 or die("chdir: $!");
1942 local $ENV{"GIT_DIR"} = $repo->repo_path();
1943 $hook_error = "rejected by sendemail-validate hook"
1944 if system($validate_hook, $target);
1945 chdir($cwd_save) or die("chdir: $!");
1946 }
1947 return $hook_error if $hook_error;
Jonathan Tan64896602017-05-12 15:38:26 -07001948 }
Jonathan Tan64896602017-05-12 15:38:26 -07001949
brian m. carlsonf2d06fb2018-07-08 22:17:11 +00001950 # Any long lines will be automatically fixed if we use a suitable transfer
1951 # encoding.
1952 unless ($xfer_encoding =~ /^(?:auto|quoted-printable|base64)$/) {
1953 open(my $fh, '<', $fn)
1954 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
1955 while (my $line = <$fh>) {
1956 if (length($line) > 998) {
1957 return sprintf(__("%s: patch contains a line longer than 998 characters"), $.);
1958 }
Jeff King747bbff2008-01-18 09:19:48 -05001959 }
1960 }
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -07001961 return;
Jeff King747bbff2008-01-18 09:19:48 -05001962}
Jeff King0706bd12008-03-28 17:28:33 -04001963
Junio C Hamano531220b2016-03-17 22:40:05 -07001964sub handle_backup {
1965 my ($last, $lastlen, $file, $known_suffix) = @_;
1966 my ($suffix, $skip);
1967
1968 $skip = 0;
1969 if (defined $last &&
1970 ($lastlen < length($file)) &&
1971 (substr($file, 0, $lastlen) eq $last) &&
1972 ($suffix = substr($file, $lastlen)) !~ /^[a-z0-9]/i) {
1973 if (defined $known_suffix && $suffix eq $known_suffix) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001974 printf(__("Skipping %s with backup suffix '%s'.\n"), $file, $known_suffix);
Junio C Hamano531220b2016-03-17 22:40:05 -07001975 $skip = 1;
1976 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001977 # TRANSLATORS: please keep "[y|N]" as is.
1978 my $answer = ask(sprintf(__("Do you really want to send %s? [y|N]: "), $file),
Junio C Hamano531220b2016-03-17 22:40:05 -07001979 valid_re => qr/^(?:y|n)/i,
1980 default => 'n');
1981 $skip = ($answer ne 'y');
1982 if ($skip) {
1983 $known_suffix = $suffix;
1984 }
1985 }
1986 }
1987 return ($skip, $known_suffix);
1988}
1989
1990sub handle_backup_files {
1991 my @file = @_;
1992 my ($last, $lastlen, $known_suffix, $skip, @result);
1993 for my $file (@file) {
1994 ($skip, $known_suffix) = handle_backup($last, $lastlen,
1995 $file, $known_suffix);
1996 push @result, $file unless $skip;
1997 $last = $file;
1998 $lastlen = length($file);
1999 }
2000 return @result;
2001}
2002
Jeff King0706bd12008-03-28 17:28:33 -04002003sub file_has_nonascii {
2004 my $fn = shift;
2005 open(my $fh, '<', $fn)
Vasco Almeida3c5cd202016-12-14 11:54:37 -01002006 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
Jeff King0706bd12008-03-28 17:28:33 -04002007 while (my $line = <$fh>) {
2008 return 1 if $line =~ /[^[:ascii:]]/;
2009 }
2010 return 0;
2011}
Thomas Rast3cae7e52010-06-17 22:10:39 +02002012
2013sub body_or_subject_has_nonascii {
2014 my $fn = shift;
2015 open(my $fh, '<', $fn)
Vasco Almeida3c5cd202016-12-14 11:54:37 -01002016 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
Thomas Rast3cae7e52010-06-17 22:10:39 +02002017 while (my $line = <$fh>) {
2018 last if $line =~ /^$/;
2019 return 1 if $line =~ /^Subject.*[^[:ascii:]]/;
2020 }
2021 while (my $line = <$fh>) {
2022 return 1 if $line =~ /[^[:ascii:]]/;
2023 }
2024 return 0;
2025}