blob: a454020f0174f7bf881aebaab1d848cada54a28d [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
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +0200215sub system_or_msg {
216 my ($args, $msg) = @_;
217 system(@$args);
218 my $signalled = $? & 127;
219 my $exit_code = $? >> 8;
220 return unless $signalled or $exit_code;
221
Ævar Arnfjörð Bjarmasonea7811b2021-04-06 16:00:37 +0200222 return sprintf(__("fatal: command '%s' died with exit code %d"),
223 $args->[0], $exit_code);
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +0200224}
225
226sub system_or_die {
227 my $msg = system_or_msg(@_);
228 die $msg if $msg;
229}
230
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100231sub do_edit {
Michael J Gruber0ce142c2010-03-22 17:12:53 +0100232 if (!defined($editor)) {
233 $editor = Git::command_oneline('var', 'GIT_EDITOR');
234 }
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +0200235 my $die_msg = __("the editor exited uncleanly, aborting everything");
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100236 if (defined($multiedit) && !$multiedit) {
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +0200237 system_or_die(['sh', '-c', $editor.' "$@"', $editor, $_], $die_msg) for @_;
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100238 } else {
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +0200239 system_or_die(['sh', '-c', $editor.' "$@"', $editor, @_], $die_msg);
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100240 }
241}
Adam Roben5483c712007-06-27 20:59:37 -0700242
243# Variables with corresponding config settings
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200244my ($suppress_from, $signed_off_by_cc);
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300245my ($cover_cc, $cover_to);
Joe Perches6e74e072010-09-24 10:03:00 -0700246my ($to_cmd, $cc_cmd);
Pascal Obry052fbea2010-09-06 20:12:11 +0200247my ($smtp_server, $smtp_server_port, @smtp_server_options);
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -0700248my ($smtp_authuser, $smtp_encryption, $smtp_ssl_cert_path);
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800249my ($batch_size, $relogin_delay);
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200250my ($identity, $aliasfiletype, @alias_files, $smtp_domain, $smtp_auth);
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200251my ($confirm);
David Brown65648282007-12-25 19:56:29 -0800252my (@suppress_cc);
Thomas Rast3cae7e52010-06-17 22:10:39 +0200253my ($auto_8bit_encoding);
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200254my ($compose_encoding);
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200255# Variables with corresponding config settings & hardcoded defaults
256my ($debug_net_smtp) = 0; # Net::SMTP, see send_message()
257my $thread = 1;
258my $chain_reply_to = 0;
259my $use_xmailer = 1;
260my $validate = 1;
brian m. carlsone67a2282018-07-08 22:17:12 +0000261my $target_xfer_encoding = 'auto';
Drew DeVaultdd84e522020-07-23 20:44:32 -0400262my $forbid_sendmail_variables = 1;
Adam Roben5483c712007-06-27 20:59:37 -0700263
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900264my %config_bool_settings = (
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200265 "thread" => \$thread,
266 "chainreplyto" => \$chain_reply_to,
267 "suppressfrom" => \$suppress_from,
268 "signedoffbycc" => \$signed_off_by_cc,
269 "cccover" => \$cover_cc,
270 "tocover" => \$cover_to,
271 "signedoffcc" => \$signed_off_by_cc,
272 "validate" => \$validate,
273 "multiedit" => \$multiedit,
274 "annotate" => \$annotate,
275 "xmailer" => \$use_xmailer,
Drew DeVaultdd84e522020-07-23 20:44:32 -0400276 "forbidsendmailvariables" => \$forbid_sendmail_variables,
Adam Robene46f7a02007-06-26 15:48:30 -0700277);
278
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900279my %config_settings = (
280 "smtpserver" => \$smtp_server,
Junio C Hamano44b24762007-09-25 17:27:54 -0700281 "smtpserverport" => \$smtp_server_port,
Pascal Obry052fbea2010-09-06 20:12:11 +0200282 "smtpserveroption" => \@smtp_server_options,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900283 "smtpuser" => \$smtp_authuser,
284 "smtppass" => \$smtp_authpass,
Pascal Obrye1e91152010-09-06 20:12:09 +0200285 "smtpdomain" => \$smtp_domain,
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200286 "smtpauth" => \$smtp_auth,
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800287 "smtpbatchsize" => \$batch_size,
288 "smtprelogindelay" => \$relogin_delay,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200289 "to" => \@config_to,
Joe Perches6e74e072010-09-24 10:03:00 -0700290 "tocmd" => \$to_cmd,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200291 "cc" => \@config_cc,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900292 "cccmd" => \$cc_cmd,
293 "aliasfiletype" => \$aliasfiletype,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200294 "bcc" => \@config_bcc,
David Brown65648282007-12-25 19:56:29 -0800295 "suppresscc" => \@suppress_cc,
Ask Bjørn Hansen9f7820a2008-06-07 00:33:42 -0700296 "envelopesender" => \$envelope_sender,
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500297 "confirm" => \$confirm,
Trent Piepho09caa242009-05-12 15:48:56 -0700298 "from" => \$sender,
Thomas Rast3cae7e52010-06-17 22:10:39 +0200299 "assume8bitencoding" => \$auto_8bit_encoding,
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200300 "composeencoding" => \$compose_encoding,
Paolo Bonzini8d814082014-11-25 15:00:27 +0100301 "transferencoding" => \$target_xfer_encoding,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900302);
Avi Kivity4a62d3f2007-03-11 19:19:44 +0200303
Cord Seelecec5dae2011-09-30 12:52:25 +0200304my %config_path_settings = (
305 "aliasesfile" => \@alias_files,
John Keeping6e07a3b2015-11-17 22:01:05 +0000306 "smtpsslcertpath" => \$smtp_ssl_cert_path,
Cord Seelecec5dae2011-09-30 12:52:25 +0200307);
308
Michael Witten87429972008-02-03 19:53:57 -0500309# Handle Uncouth Termination
310sub signal_handler {
311
312 # Make text normal
313 print color("reset"), "\n";
314
315 # SMTP password masked
316 system "stty echo";
317
318 # tmp files from --compose
Jay Soffianafe756c2009-02-23 13:51:37 -0500319 if (defined $compose_filename) {
320 if (-e $compose_filename) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100321 printf __("'%s' contains an intermediate version ".
322 "of the email you were composing.\n"),
323 $compose_filename;
Jay Soffianafe756c2009-02-23 13:51:37 -0500324 }
325 if (-e ($compose_filename . ".final")) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100326 printf __("'%s.final' contains the composed email.\n"),
327 $compose_filename;
Jay Soffianafe756c2009-02-23 13:51:37 -0500328 }
Michael Witten87429972008-02-03 19:53:57 -0500329 }
330
331 exit;
332};
333
334$SIG{TERM} = \&signal_handler;
335$SIG{INT} = \&signal_handler;
336
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200337# Read our sendemail.* config
338sub read_config {
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200339 my ($configured, $prefix) = @_;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200340
341 foreach my $setting (keys %config_bool_settings) {
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200342 my $target = $config_bool_settings{$setting};
343 my $v = Git::config_bool(@repo, "$prefix.$setting");
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200344 next unless defined $v;
345 next if $configured->{$setting}++;
346 $$target = $v;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200347 }
348
349 foreach my $setting (keys %config_path_settings) {
350 my $target = $config_path_settings{$setting};
351 if (ref($target) eq "ARRAY") {
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200352 my @values = Git::config_path(@repo, "$prefix.$setting");
353 next unless @values;
354 next if $configured->{$setting}++;
355 @$target = @values;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200356 }
357 else {
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200358 my $v = Git::config_path(@repo, "$prefix.$setting");
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200359 next unless defined $v;
360 next if $configured->{$setting}++;
361 $$target = $v;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200362 }
363 }
364
365 foreach my $setting (keys %config_settings) {
366 my $target = $config_settings{$setting};
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200367 if (ref($target) eq "ARRAY") {
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200368 my @values = Git::config(@repo, "$prefix.$setting");
369 next unless @values;
370 next if $configured->{$setting}++;
371 @$target = @values;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200372 }
373 else {
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200374 my $v = Git::config(@repo, "$prefix.$setting");
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200375 next unless defined $v;
376 next if $configured->{$setting}++;
377 $$target = $v;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200378 }
379 }
380
381 if (!defined $smtp_encryption) {
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200382 my $setting = "$prefix.smtpencryption";
383 my $enc = Git::config(@repo, $setting);
384 return unless defined $enc;
385 return if $configured->{$setting}++;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200386 if (defined $enc) {
387 $smtp_encryption = $enc;
388 } elsif (Git::config_bool(@repo, "$prefix.smtpssl")) {
389 $smtp_encryption = 'ssl';
390 }
391 }
392}
393
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200394# sendemail.identity yields to --identity. We must parse this
395# special-case first before the rest of the config is read.
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200396$identity = Git::config(@repo, "sendemail.identity");
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200397my $rc = GetOptions(
398 "identity=s" => \$identity,
399 "no-identity" => \$no_identity,
400);
401usage() unless $rc;
402undef $identity if $no_identity;
403
404# Now we know enough to read the config
405{
406 my %configured;
407 read_config(\%configured, "sendemail.$identity") if defined $identity;
408 read_config(\%configured, "sendemail");
409}
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200410
Ryan Anderson83b24432005-07-31 04:17:25 -0400411# Begin by accumulating all the variables (defined above), that we will end up
412# needing, first, from the command line:
413
Clemens Buchacherc5978242011-09-03 19:06:13 +0200414my $help;
Duy Nguyen13374982018-11-03 07:03:18 +0100415my $git_completion_helper;
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200416$rc = GetOptions("h" => \$help,
417 "dump-aliases" => \$dump_aliases);
Jacob Keller17b7a832015-11-19 14:52:11 -0800418usage() unless $rc;
Vasco Almeida46493102016-12-14 11:54:36 -0100419die __("--dump-aliases incompatible with other options\n")
Jacob Keller17b7a832015-11-19 14:52:11 -0800420 if !$help and $dump_aliases and @ARGV;
421$rc = GetOptions(
Clemens Buchacherc5978242011-09-03 19:06:13 +0200422 "sender|from=s" => \$sender,
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100423 "in-reply-to=s" => \$initial_in_reply_to,
Christian Ludwigd11c9432018-03-04 00:58:14 +0100424 "reply-to=s" => \$reply_to,
Ryan Anderson83b24432005-07-31 04:17:25 -0400425 "subject=s" => \$initial_subject,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200426 "to=s" => \@getopt_to,
Joe Perches6e74e072010-09-24 10:03:00 -0700427 "to-cmd=s" => \$to_cmd,
Stephen Boydf434c082010-03-07 14:46:48 -0800428 "no-to" => \$no_to,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200429 "cc=s" => \@getopt_cc,
Stephen Boydf434c082010-03-07 14:46:48 -0800430 "no-cc" => \$no_cc,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200431 "bcc=s" => \@getopt_bcc,
Stephen Boydf434c082010-03-07 14:46:48 -0800432 "no-bcc" => \$no_bcc,
Ryan Anderson78488b22005-07-31 20:04:24 -0400433 "chain-reply-to!" => \$chain_reply_to,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800434 "no-chain-reply-to" => sub {$chain_reply_to = 0},
Ryan Anderson3342d852005-07-31 20:04:24 -0400435 "smtp-server=s" => \$smtp_server,
Pascal Obry052fbea2010-09-06 20:12:11 +0200436 "smtp-server-option=s" => \@smtp_server_options,
Junio C Hamano44b24762007-09-25 17:27:54 -0700437 "smtp-server-port=s" => \$smtp_server_port,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900438 "smtp-user=s" => \$smtp_authuser,
Michael Witten2363d742008-02-03 19:53:56 -0500439 "smtp-pass:s" => \$smtp_authpass,
Thomas Rastf6bebd12008-06-25 21:42:43 +0200440 "smtp-ssl" => sub { $smtp_encryption = 'ssl' },
441 "smtp-encryption=s" => \$smtp_encryption,
Thomas Rast979e6522013-12-01 23:48:42 +0100442 "smtp-ssl-cert-path=s" => \$smtp_ssl_cert_path,
Jari Aaltof60812e2010-03-14 17:16:09 +0200443 "smtp-debug:i" => \$debug_net_smtp,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -0400444 "smtp-domain:s" => \$smtp_domain,
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200445 "smtp-auth=s" => \$smtp_auth,
Joshua Watt8dd9b3f2018-10-22 22:24:25 -0500446 "no-smtp-auth" => sub {$smtp_auth = 'none'},
Felipe Contreras402596a2013-04-07 01:10:27 -0600447 "annotate!" => \$annotate,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800448 "no-annotate" => sub {$annotate = 0},
Ryan Anderson1f038a02005-09-05 01:13:07 -0400449 "compose" => \$compose,
Ryan Anderson30d08b32006-02-02 11:56:06 -0500450 "quiet" => \$quiet,
Joe Perches324a8bd2007-08-17 18:51:12 -0700451 "cc-cmd=s" => \$cc_cmd,
Adam Roben5483c712007-06-27 20:59:37 -0700452 "suppress-from!" => \$suppress_from,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800453 "no-suppress-from" => sub {$suppress_from = 0},
David Brown65648282007-12-25 19:56:29 -0800454 "suppress-cc=s" => \@suppress_cc,
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500455 "signed-off-cc|signed-off-by-cc!" => \$signed_off_by_cc,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800456 "no-signed-off-cc|no-signed-off-by-cc" => sub {$signed_off_by_cc = 0},
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300457 "cc-cover|cc-cover!" => \$cover_cc,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800458 "no-cc-cover" => sub {$cover_cc = 0},
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300459 "to-cover|to-cover!" => \$cover_to,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800460 "no-to-cover" => sub {$cover_to = 0},
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500461 "confirm=s" => \$confirm,
Matthew Wilcox61302592006-10-10 08:58:23 -0600462 "dry-run" => \$dry_run,
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700463 "envelope-sender=s" => \$envelope_sender,
Adam Roben5483c712007-06-27 20:59:37 -0700464 "thread!" => \$thread,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800465 "no-thread" => sub {$thread = 0},
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500466 "validate!" => \$validate,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800467 "no-validate" => sub {$validate = 0},
Paolo Bonzini8d814082014-11-25 15:00:27 +0100468 "transfer-encoding=s" => \$target_xfer_encoding,
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100469 "format-patch!" => \$format_patch,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800470 "no-format-patch" => sub {$format_patch = 0},
Thomas Rast3cae7e52010-06-17 22:10:39 +0200471 "8bit-encoding=s" => \$auto_8bit_encoding,
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200472 "compose-encoding=s" => \$compose_encoding,
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200473 "force" => \$force,
Luis Henriquesac1596a2014-03-24 21:38:27 +0000474 "xmailer!" => \$use_xmailer,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800475 "no-xmailer" => sub {$use_xmailer = 0},
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800476 "batch-size=i" => \$batch_size,
477 "relogin-delay=i" => \$relogin_delay,
Duy Nguyen13374982018-11-03 07:03:18 +0100478 "git-completion-helper" => \$git_completion_helper,
Ryan Anderson83b24432005-07-31 04:17:25 -0400479 );
480
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200481# Munge any "either config or getopt, not both" variables
482my @initial_to = @getopt_to ? @getopt_to : ($no_to ? () : @config_to);
483my @initial_cc = @getopt_cc ? @getopt_cc : ($no_cc ? () : @config_cc);
484my @initial_bcc = @getopt_bcc ? @getopt_bcc : ($no_bcc ? () : @config_bcc);
485
Clemens Buchacherc5978242011-09-03 19:06:13 +0200486usage() if $help;
Duy Nguyen13374982018-11-03 07:03:18 +0100487completion_helper() if $git_completion_helper;
Michael Coleman1b0baf12007-02-27 22:47:54 -0600488unless ($rc) {
489 usage();
490}
491
Drew DeVaultdd84e522020-07-23 20:44:32 -0400492if ($forbid_sendmail_variables && (scalar Git::config_regexp("^sendmail[.]")) != 0) {
493 die __("fatal: found configuration options for 'sendmail'\n" .
494 "git-send-email is configured with the sendemail.* options - note the 'e'.\n" .
495 "Set sendemail.forbidSendmailVariables to false to disable this check.\n");
496}
497
Vasco Almeida46493102016-12-14 11:54:36 -0100498die __("Cannot run git format-patch from outside a repository\n")
Jay Soffianeed6ca72009-02-14 23:32:13 -0500499 if $format_patch and not $repo;
500
Stefan Beller9caa7062018-02-12 11:44:04 -0800501die __("`batch-size` and `relogin` must be specified together " .
502 "(via command-line or configuration option)\n")
503 if defined $relogin_delay and not defined $batch_size;
504
Thomas Rastfa835cd2008-06-26 23:03:21 +0200505# 'default' encryption is none -- this only prevents a warning
506$smtp_encryption = '' unless (defined $smtp_encryption);
507
David Brown65648282007-12-25 19:56:29 -0800508# Set CC suppressions
509my(%suppress_cc);
510if (@suppress_cc) {
511 foreach my $entry (@suppress_cc) {
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700512 # Please update $__git_send_email_suppresscc_options
513 # in git-completion.bash when you add new options.
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100514 die sprintf(__("Unknown --suppress-cc field: '%s'\n"), $entry)
Rasmus Villemoesef0cc1d2018-10-16 09:39:23 +0200515 unless $entry =~ /^(?:all|cccmd|cc|author|self|sob|body|bodycc|misc-by)$/;
David Brown65648282007-12-25 19:56:29 -0800516 $suppress_cc{$entry} = 1;
517 }
518}
519
520if ($suppress_cc{'all'}) {
Rasmus Villemoesef0cc1d2018-10-16 09:39:23 +0200521 foreach my $entry (qw (cccmd cc author self sob body bodycc misc-by)) {
David Brown65648282007-12-25 19:56:29 -0800522 $suppress_cc{$entry} = 1;
523 }
524 delete $suppress_cc{'all'};
525}
526
527# If explicit old-style ones are specified, they trump --suppress-cc.
528$suppress_cc{'self'} = $suppress_from if defined $suppress_from;
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500529$suppress_cc{'sob'} = !$signed_off_by_cc if defined $signed_off_by_cc;
David Brown65648282007-12-25 19:56:29 -0800530
Jay Soffian3531e272009-02-14 23:32:15 -0500531if ($suppress_cc{'body'}) {
Rasmus Villemoesef0cc1d2018-10-16 09:39:23 +0200532 foreach my $entry (qw (sob bodycc misc-by)) {
Jay Soffian3531e272009-02-14 23:32:15 -0500533 $suppress_cc{$entry} = 1;
534 }
535 delete $suppress_cc{'body'};
536}
537
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500538# Set confirm's default value
539my $confirm_unconfigured = !defined $confirm;
540if ($confirm_unconfigured) {
541 $confirm = scalar %suppress_cc ? 'compose' : 'auto';
542};
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700543# Please update $__git_send_email_confirm_options in
544# git-completion.bash when you add new options.
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100545die sprintf(__("Unknown --confirm setting: '%s'\n"), $confirm)
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500546 unless $confirm =~ /^(?:auto|cc|compose|always|never)/;
547
David Brown65648282007-12-25 19:56:29 -0800548# Debugging, print out the suppressions.
549if (0) {
550 print "suppressions:\n";
551 foreach my $entry (keys %suppress_cc) {
552 printf " %-5s -> $suppress_cc{$entry}\n", $entry;
553 }
554}
555
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100556my ($repoauthor, $repocommitter);
557($repoauthor) = Git::ident_person(@repo, 'author');
558($repocommitter) = Git::ident_person(@repo, 'committer');
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900559
Jay Soffian50126992009-02-14 23:32:14 -0500560sub parse_address_line {
Matthieu Moybd869f62018-01-05 19:36:51 +0100561 return map { $_->format } Mail::Address->parse($_[0]);
Jay Soffian50126992009-02-14 23:32:14 -0500562}
563
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800564sub split_addrs {
Junio C Hamano2f0e7cb2008-12-21 01:57:59 -0800565 return quotewords('\s*,\s*', 1, @_);
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800566}
567
Eric Wong994d6c62006-05-14 19:13:44 -0700568my %aliases;
Eric Sunshine09f11572015-05-31 18:29:27 -0400569
570sub parse_sendmail_alias {
571 local $_ = shift;
572 if (/"/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100573 printf STDERR __("warning: sendmail alias with quotes is not supported: %s\n"), $_;
Eric Sunshine86b89842015-06-01 14:22:36 -0400574 } elsif (/:include:/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100575 printf STDERR __("warning: `:include:` not supported: %s\n"), $_;
Eric Sunshine86b89842015-06-01 14:22:36 -0400576 } elsif (/[\/|]/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100577 printf STDERR __("warning: `/file` or `|pipe` redirection not supported: %s\n"), $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400578 } elsif (/^(\S+?)\s*:\s*(.+)$/) {
579 my ($alias, $addr) = ($1, $2);
580 $aliases{$alias} = [ split_addrs($addr) ];
581 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100582 printf STDERR __("warning: sendmail line is not recognized: %s\n"), $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400583 }
584}
585
586sub parse_sendmail_aliases {
587 my $fh = shift;
Eric Sunshine2532dd02015-05-31 18:29:29 -0400588 my $s = '';
Eric Sunshine09f11572015-05-31 18:29:27 -0400589 while (<$fh>) {
Eric Sunshine2532dd02015-05-31 18:29:29 -0400590 chomp;
Eric Sunshine020be85f2015-05-31 18:29:28 -0400591 next if /^\s*$/ || /^\s*#/;
Eric Sunshine2532dd02015-05-31 18:29:29 -0400592 $s .= $_, next if $s =~ s/\\$// || s/^\s+//;
593 parse_sendmail_alias($s) if $s;
594 $s = $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400595 }
Eric Sunshine2532dd02015-05-31 18:29:29 -0400596 $s =~ s/\\$//; # silently tolerate stray '\' on last line
597 parse_sendmail_alias($s) if $s;
Eric Sunshine09f11572015-05-31 18:29:27 -0400598}
599
Eric Wong994d6c62006-05-14 19:13:44 -0700600my %parse_alias = (
601 # multiline formats can be supported in the future
602 mutt => sub { my $fh = shift; while (<$fh>) {
Felipe Contrerasffc01f92009-09-30 17:49:36 +0300603 if (/^\s*alias\s+(?:-group\s+\S+\s+)*(\S+)\s+(.*)$/) {
Eric Wong994d6c62006-05-14 19:13:44 -0700604 my ($alias, $addr) = ($1, $2);
605 $addr =~ s/#.*$//; # mutt allows # comments
Eric Wong2c510f22016-01-04 20:53:30 +0000606 # commas delimit multiple addresses
607 my @addr = split_addrs($addr);
608
609 # quotes may be escaped in the file,
610 # unescape them so we do not double-escape them later.
611 s/\\"/"/g foreach @addr;
612 $aliases{$alias} = \@addr
Eric Wong994d6c62006-05-14 19:13:44 -0700613 }}},
614 mailrc => sub { my $fh = shift; while (<$fh>) {
Jeff Kinga277d1e2016-03-17 19:58:22 -0400615 if (/^alias\s+(\S+)\s+(.*?)\s*$/) {
Eric Wong994d6c62006-05-14 19:13:44 -0700616 # spaces delimit multiple addresses
Eric W. Biedermanfe87c922009-05-20 19:45:53 -0700617 $aliases{$1} = [ quotewords('\s+', 0, $2) ];
Eric Wong994d6c62006-05-14 19:13:44 -0700618 }}},
Trent Piepho73c427e2008-11-25 18:55:00 -0800619 pine => sub { my $fh = shift; my $f='\t[^\t]*';
620 for (my $x = ''; defined($x); $x = $_) {
621 chomp $x;
622 $x .= $1 while(defined($_ = <$fh>) && /^ +(.*)$/);
623 $x =~ /^(\S+)$f\t\(?([^\t]+?)\)?(:?$f){0,2}$/ or next;
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800624 $aliases{$1} = [ split_addrs($2) ];
Trent Piepho73c427e2008-11-25 18:55:00 -0800625 }},
Bill Pemberton7613ea32009-04-22 09:41:29 -0400626 elm => sub { my $fh = shift;
627 while (<$fh>) {
628 if (/^(\S+)\s+=\s+[^=]+=\s(\S+)/) {
629 my ($alias, $addr) = ($1, $2);
630 $aliases{$alias} = [ split_addrs($addr) ];
631 }
632 } },
Eric Sunshine09f11572015-05-31 18:29:27 -0400633 sendmail => \&parse_sendmail_aliases,
Eric Wong994d6c62006-05-14 19:13:44 -0700634 gnus => sub { my $fh = shift; while (<$fh>) {
635 if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
636 $aliases{$1} = [ $2 ];
637 }}}
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700638 # Please update _git_config() in git-completion.bash when you
639 # add new MUAs.
Eric Wong994d6c62006-05-14 19:13:44 -0700640);
641
Petr Baudis3cb8caf2006-07-03 22:47:58 +0200642if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) {
Eric Wong994d6c62006-05-14 19:13:44 -0700643 foreach my $file (@alias_files) {
644 open my $fh, '<', $file or die "opening $file: $!\n";
645 $parse_alias{$aliasfiletype}->($fh);
646 close $fh;
647 }
648}
649
Jacob Keller17b7a832015-11-19 14:52:11 -0800650if ($dump_aliases) {
651 print "$_\n" for (sort keys %aliases);
652 exit(0);
653}
654
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700655# is_format_patch_arg($f) returns 0 if $f names a patch, or 1 if
656# $f is a revision list specification to be passed to format-patch.
657sub is_format_patch_arg {
Jay Soffianeed6ca72009-02-14 23:32:13 -0500658 return unless $repo;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100659 my $f = shift;
660 try {
661 $repo->command('rev-parse', '--verify', '--quiet', $f);
662 if (defined($format_patch)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100663 return $format_patch;
664 }
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100665 die sprintf(__ <<EOF, $f, $f);
666File '%s' exists but it could also be the range of commits
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100667to produce patches for. Please disambiguate by...
668
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100669 * Saying "./%s" if you mean a file; or
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100670 * Giving --format-patch option if you mean a range.
671EOF
672 } catch Git::Error::Command with {
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700673 # Not a valid revision. Treat it as a filename.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100674 return 0;
675 }
676}
677
Jeff Kingaa548922008-01-18 09:19:36 -0500678# Now that all the defaults are set, process the rest of the command line
679# arguments and collect up the files that need to be processed.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100680my @rev_list_opts;
Junio C Hamano69f4ce52008-11-30 22:38:20 -0800681while (defined(my $f = shift @ARGV)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100682 if ($f eq "--") {
683 push @rev_list_opts, "--", @ARGV;
684 @ARGV = ();
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700685 } elsif (-d $f and !is_format_patch_arg($f)) {
Ævar Arnfjörð Bjarmasonc6038162010-09-30 13:42:54 +0000686 opendir my $dh, $f
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100687 or die sprintf(__("Failed to opendir %s: %s"), $f, $!);
Jeff Kingaa548922008-01-18 09:19:36 -0500688
Ævar Arnfjörð Bjarmason89bf1ba2010-09-14 19:02:24 +0000689 push @files, grep { -f $_ } map { catfile($f, $_) }
Ævar Arnfjörð Bjarmasonc6038162010-09-30 13:42:54 +0000690 sort readdir $dh;
691 closedir $dh;
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700692 } elsif ((-f $f or -p $f) and !is_format_patch_arg($f)) {
Jeff Kingaa548922008-01-18 09:19:36 -0500693 push @files, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500694 } else {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100695 push @rev_list_opts, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500696 }
697}
698
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100699if (@rev_list_opts) {
Vasco Almeida46493102016-12-14 11:54:36 -0100700 die __("Cannot run git format-patch from outside a repository\n")
Jay Soffianeed6ca72009-02-14 23:32:13 -0500701 unless $repo;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100702 push @files, $repo->command('format-patch', '-o', tempdir(CLEANUP => 1), @rev_list_opts);
703}
704
Junio C Hamano531220b2016-03-17 22:40:05 -0700705@files = handle_backup_files(@files);
706
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500707if ($validate) {
Jeff Kingc764a0c2008-01-18 09:20:10 -0500708 foreach my $f (@files) {
Kevin Ballard300913b2008-06-25 15:44:40 -0700709 unless (-p $f) {
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +0200710 validate_patch($f, $target_xfer_encoding);
Kevin Ballard300913b2008-06-25 15:44:40 -0700711 }
Jeff Kingc764a0c2008-01-18 09:20:10 -0500712 }
Jeff King747bbff2008-01-18 09:19:48 -0500713}
714
Jeff Kingaa548922008-01-18 09:19:36 -0500715if (@files) {
716 unless ($quiet) {
717 print $_,"\n" for (@files);
718 }
719} else {
Vasco Almeida46493102016-12-14 11:54:36 -0100720 print STDERR __("\nNo patch files specified!\n\n");
Jeff Kingaa548922008-01-18 09:19:36 -0500721 usage();
722}
723
Ævar Arnfjörð Bjarmasonacf071b2010-09-30 13:42:57 +0000724sub get_patch_subject {
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100725 my $fn = shift;
726 open (my $fh, '<', $fn);
727 while (my $line = <$fh>) {
728 next unless ($line =~ /^Subject: (.*)$/);
729 close $fh;
730 return "GIT: $1\n";
731 }
732 close $fh;
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100733 die sprintf(__("No subject line in %s?"), $fn);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100734}
735
736if ($compose) {
737 # Note that this does not need to be secure, but we will make a small
738 # effort to have it be unique
Jay Soffianafe756c2009-02-23 13:51:37 -0500739 $compose_filename = ($repo ?
740 tempfile(".gitsendemail.msg.XXXXXX", DIR => $repo->repo_path()) :
741 tempfile(".gitsendemail.msg.XXXXXX", DIR => "."))[1];
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000742 open my $c, ">", $compose_filename
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100743 or die sprintf(__("Failed to open for writing %s: %s"), $compose_filename, $!);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100744
745
746 my $tpl_sender = $sender || $repoauthor || $repocommitter || '';
747 my $tpl_subject = $initial_subject || '';
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100748 my $tpl_in_reply_to = $initial_in_reply_to || '';
Christian Ludwigd11c9432018-03-04 00:58:14 +0100749 my $tpl_reply_to = $reply_to || '';
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100750
Vasco Almeida70aedfb2016-12-14 11:54:38 -0100751 print $c <<EOT1, Git::prefix_lines("GIT: ", __ <<EOT2), <<EOT3;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100752From $tpl_sender # This line is ignored.
Vasco Almeida70aedfb2016-12-14 11:54:38 -0100753EOT1
754Lines beginning in "GIT:" will be removed.
755Consider including an overall diffstat or table of contents
756for the patch you are writing.
757
758Clear the body content if you don't wish to send a summary.
759EOT2
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100760From: $tpl_sender
Christian Ludwigd11c9432018-03-04 00:58:14 +0100761Reply-To: $tpl_reply_to
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100762Subject: $tpl_subject
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100763In-Reply-To: $tpl_in_reply_to
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100764
Vasco Almeida70aedfb2016-12-14 11:54:38 -0100765EOT3
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100766 for my $f (@files) {
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000767 print $c get_patch_subject($f);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100768 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000769 close $c;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100770
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100771 if ($annotate) {
772 do_edit($compose_filename, @files);
773 } else {
774 do_edit($compose_filename);
775 }
776
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000777 open $c, "<", $compose_filename
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100778 or die sprintf(__("Failed to open %s: %s"), $compose_filename, $!);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100779
Krzysztof Mazur4a47a4d2012-10-22 14:41:48 +0200780 if (!defined $compose_encoding) {
781 $compose_encoding = "UTF-8";
782 }
Nathan Payreb6049542017-12-15 16:33:39 +0100783
784 my %parsed_email;
785 while (my $line = <$c>) {
786 next if $line =~ m/^GIT:/;
787 parse_header_line($line, \%parsed_email);
788 if ($line =~ /^$/) {
789 $parsed_email{'body'} = filter_body($c);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100790 }
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100791 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000792 close $c;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100793
Nathan Payreb6049542017-12-15 16:33:39 +0100794 open my $c2, ">", $compose_filename . ".final"
795 or die sprintf(__("Failed to open %s.final: %s"), $compose_filename, $!);
796
797
798 if ($parsed_email{'From'}) {
799 $sender = delete($parsed_email{'From'});
800 }
801 if ($parsed_email{'In-Reply-To'}) {
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100802 $initial_in_reply_to = delete($parsed_email{'In-Reply-To'});
Nathan Payreb6049542017-12-15 16:33:39 +0100803 }
Christian Ludwigd11c9432018-03-04 00:58:14 +0100804 if ($parsed_email{'Reply-To'}) {
805 $reply_to = delete($parsed_email{'Reply-To'});
Nathan Payreb6049542017-12-15 16:33:39 +0100806 }
807 if ($parsed_email{'Subject'}) {
808 $initial_subject = delete($parsed_email{'Subject'});
809 print $c2 "Subject: " .
810 quote_subject($initial_subject, $compose_encoding) .
811 "\n";
812 }
813
814 if ($parsed_email{'MIME-Version'}) {
815 print $c2 "MIME-Version: $parsed_email{'MIME-Version'}\n",
816 "Content-Type: $parsed_email{'Content-Type'};\n",
817 "Content-Transfer-Encoding: $parsed_email{'Content-Transfer-Encoding'}\n";
818 delete($parsed_email{'MIME-Version'});
819 delete($parsed_email{'Content-Type'});
820 delete($parsed_email{'Content-Transfer-Encoding'});
821 } elsif (file_has_nonascii($compose_filename)) {
822 my $content_type = (delete($parsed_email{'Content-Type'}) or
823 "text/plain; charset=$compose_encoding");
824 print $c2 "MIME-Version: 1.0\n",
825 "Content-Type: $content_type\n",
826 "Content-Transfer-Encoding: 8bit\n";
827 }
828 # Preserve unknown headers
829 foreach my $key (keys %parsed_email) {
830 next if $key eq 'body';
831 print $c2 "$key: $parsed_email{$key}";
832 }
833
834 if ($parsed_email{'body'}) {
835 print $c2 "\n$parsed_email{'body'}\n";
836 delete($parsed_email{'body'});
837 } else {
Vasco Almeida46493102016-12-14 11:54:36 -0100838 print __("Summary email is empty, skipping it\n");
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100839 $compose = -1;
840 }
Nathan Payreb6049542017-12-15 16:33:39 +0100841
842 close $c2;
843
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100844} elsif ($annotate) {
845 do_edit(@files);
846}
847
Jay Soffian6e182512009-03-28 21:39:10 -0400848sub ask {
849 my ($prompt, %arg) = @_;
Jay Soffian0da43a62009-04-04 23:23:21 -0400850 my $valid_re = $arg{valid_re};
Jay Soffian6e182512009-03-28 21:39:10 -0400851 my $default = $arg{default};
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700852 my $confirm_only = $arg{confirm_only};
Jay Soffian6e182512009-03-28 21:39:10 -0400853 my $resp;
854 my $i = 0;
Jay Soffian5906f542009-03-31 12:22:11 -0400855 return defined $default ? $default : undef
856 unless defined $term->IN and defined fileno($term->IN) and
857 defined $term->OUT and defined fileno($term->OUT);
Jay Soffian6e182512009-03-28 21:39:10 -0400858 while ($i++ < 10) {
859 $resp = $term->readline($prompt);
860 if (!defined $resp) { # EOF
861 print "\n";
862 return defined $default ? $default : undef;
863 }
864 if ($resp eq '' and defined $default) {
865 return $default;
866 }
Jay Soffian0da43a62009-04-04 23:23:21 -0400867 if (!defined $valid_re or $resp =~ /$valid_re/) {
Jay Soffian6e182512009-03-28 21:39:10 -0400868 return $resp;
869 }
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700870 if ($confirm_only) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100871 my $yesno = $term->readline(
872 # TRANSLATORS: please keep [y/N] as is.
873 sprintf(__("Are you sure you want to use <%s> [y/N]? "), $resp));
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700874 if (defined $yesno && $yesno =~ /y/i) {
875 return $resp;
876 }
877 }
Jay Soffian6e182512009-03-28 21:39:10 -0400878 }
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -0700879 return;
Jay Soffian6e182512009-03-28 21:39:10 -0400880}
881
Nathan Payreb6049542017-12-15 16:33:39 +0100882sub parse_header_line {
883 my $lines = shift;
884 my $parsed_line = shift;
885 my $addr_pat = join "|", qw(To Cc Bcc);
886
887 foreach (split(/\n/, $lines)) {
888 if (/^($addr_pat):\s*(.+)$/i) {
889 $parsed_line->{$1} = [ parse_address_line($2) ];
890 } elsif (/^([^:]*):\s*(.+)\s*$/i) {
891 $parsed_line->{$1} = $2;
892 }
893 }
894}
895
896sub filter_body {
897 my $c = shift;
898 my $body = "";
899 while (my $body_line = <$c>) {
900 if ($body_line !~ m/^GIT:/) {
901 $body .= $body_line;
902 }
903 }
904 return $body;
905}
906
907
Thomas Rast3cae7e52010-06-17 22:10:39 +0200908my %broken_encoding;
909
Ævar Arnfjörð Bjarmason1d50bfd2010-09-30 13:42:58 +0000910sub file_declares_8bit_cte {
Thomas Rast3cae7e52010-06-17 22:10:39 +0200911 my $fn = shift;
912 open (my $fh, '<', $fn);
913 while (my $line = <$fh>) {
914 last if ($line =~ /^$/);
915 return 1 if ($line =~ /^Content-Transfer-Encoding: .*8bit.*$/);
916 }
917 close $fh;
918 return 0;
919}
920
921foreach my $f (@files) {
922 next unless (body_or_subject_has_nonascii($f)
923 && !file_declares_8bit_cte($f));
924 $broken_encoding{$f} = 1;
925}
926
927if (!defined $auto_8bit_encoding && scalar %broken_encoding) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100928 print __("The following files are 8bit, but do not declare " .
929 "a Content-Transfer-Encoding.\n");
Thomas Rast3cae7e52010-06-17 22:10:39 +0200930 foreach my $f (sort keys %broken_encoding) {
931 print " $f\n";
932 }
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100933 $auto_8bit_encoding = ask(__("Which 8bit encoding should I declare [UTF-8]? "),
Junio C Hamano852a15d2015-02-13 12:20:25 -0800934 valid_re => qr/.{4}/, confirm_only => 1,
Thomas Rast3cae7e52010-06-17 22:10:39 +0200935 default => "UTF-8");
936}
937
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200938if (!$force) {
939 for my $f (@files) {
Ævar Arnfjörð Bjarmason0d290a42010-09-30 13:43:01 +0000940 if (get_patch_subject($f) =~ /\Q*** SUBJECT HERE ***\E/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100941 die sprintf(__("Refusing to send because the patch\n\t%s\n"
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200942 . "has the template subject '*** SUBJECT HERE ***'. "
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100943 . "Pass --force if you really want to send.\n"), $f);
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200944 }
945 }
946}
947
Remi Lespinetc46e27a2015-06-30 14:16:47 +0200948if (defined $sender) {
Remi Lespinetfa5b1aa2015-06-30 14:16:51 +0200949 $sender =~ s/^\s+|\s+$//g;
Remi Lespinetc46e27a2015-06-30 14:16:47 +0200950 ($sender) = expand_aliases($sender);
951} else {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100952 $sender = $repoauthor || $repocommitter || '';
Ryan Anderson83b24432005-07-31 04:17:25 -0400953}
954
Michael S. Tsirkinda187592013-06-05 21:11:00 +0300955# $sender could be an already sanitized address
956# (e.g. sendemail.from could be manually sanitized by user).
957# But it's a no-op to run sanitize_address on an already sanitized address.
958$sender = sanitize_address($sender);
959
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100960my $to_whom = __("To whom should the emails be sent (if anyone)?");
Felipe Contreras8cac13d2012-11-24 12:16:19 +0100961my $prompting = 0;
Junio C Hamano8796ff72010-10-26 22:02:03 -0700962if (!@initial_to && !defined $to_cmd) {
Junio C Hamano0d6b21e2016-04-24 12:31:44 -0700963 my $to = ask("$to_whom ",
Stephen Boyd61837492012-09-06 11:31:11 -0700964 default => "",
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700965 valid_re => qr/\@.*\./, confirm_only => 1);
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700966 push @initial_to, parse_address_line($to) if defined $to; # sanitized/validated later
Ryan Anderson1f038a02005-09-05 01:13:07 -0400967 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -0400968}
969
Eric Wong994d6c62006-05-14 19:13:44 -0700970sub expand_aliases {
Jeff King302e04e2009-07-23 07:09:29 -0400971 return map { expand_one_alias($_) } @_;
972}
973
974my %EXPANDED_ALIASES;
975sub expand_one_alias {
976 my $alias = shift;
977 if ($EXPANDED_ALIASES{$alias}) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100978 die sprintf(__("fatal: alias '%s' expands to itself\n"), $alias);
Jeff King302e04e2009-07-23 07:09:29 -0400979 }
980 local $EXPANDED_ALIASES{$alias} = 1;
981 return $aliases{$alias} ? expand_aliases(@{$aliases{$alias}}) : $alias;
Eric Wong994d6c62006-05-14 19:13:44 -0700982}
983
Remi Lespinetb5e112d2015-06-30 14:16:45 +0200984@initial_to = process_address_list(@initial_to);
985@initial_cc = process_address_list(@initial_cc);
Ævar Arnfjörð Bjarmasone60f6d12019-05-09 13:48:29 +0200986@initial_bcc = process_address_list(@initial_bcc);
Eric Wong994d6c62006-05-14 19:13:44 -0700987
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100988if ($thread && !defined $initial_in_reply_to && $prompting) {
989 $initial_in_reply_to = ask(
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100990 __("Message-ID to be used as In-Reply-To for the first email (if any)? "),
Stephen Boyd61837492012-09-06 11:31:11 -0700991 default => "",
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700992 valid_re => qr/\@.*\./, confirm_only => 1);
Ryan Anderson83b24432005-07-31 04:17:25 -0400993}
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100994if (defined $initial_in_reply_to) {
995 $initial_in_reply_to =~ s/^\s*<?//;
996 $initial_in_reply_to =~ s/>?\s*$//;
997 $initial_in_reply_to = "<$initial_in_reply_to>" if $initial_in_reply_to ne '';
Junio C Hamanoace9c2a2007-12-10 21:44:42 -0800998}
Mike Hommeyace72082007-12-09 18:17:28 +0100999
Christian Ludwigd11c9432018-03-04 00:58:14 +01001000if (defined $reply_to) {
1001 $reply_to =~ s/^\s+|\s+$//g;
1002 ($reply_to) = expand_aliases($reply_to);
1003 $reply_to = sanitize_address($reply_to);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001004}
Eric Wongaca7ad72006-05-15 02:34:44 -07001005
1006if (!defined $smtp_server) {
Florian Klink1ab2fd42017-11-28 01:49:04 +01001007 my @sendmail_paths = qw( /usr/sbin/sendmail /usr/lib/sendmail );
1008 push @sendmail_paths, map {"$_/sendmail"} split /:/, $ENV{PATH};
1009 foreach (@sendmail_paths) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001010 if (-x $_) {
1011 $smtp_server = $_;
1012 last;
1013 }
1014 }
1015 $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
Ryan Anderson3342d852005-07-31 20:04:24 -04001016}
1017
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001018if ($compose && $compose > 0) {
1019 @files = ($compose_filename . ".final", @files);
Ryan Anderson1f038a02005-09-05 01:13:07 -04001020}
1021
Ryan Anderson83b24432005-07-31 04:17:25 -04001022# Variables we set as part of the loop over files
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001023our ($message_id, %mail, $subject, $in_reply_to, $references, $message,
Jay Soffiandc1460a2009-03-31 12:22:12 -04001024 $needs_confirm, $message_num, $ask_default);
Ryan Anderson83b24432005-07-31 04:17:25 -04001025
Eric Wong567ffeb2006-03-25 16:47:12 -08001026sub extract_valid_address {
1027 my $address = shift;
Ævar Arnfjörð Bjarmason35b6ab92010-09-30 19:03:31 +00001028 my $local_part_regexp = qr/[^<>"\s@]+/;
1029 my $domain_regexp = qr/[^.<>"\s@]+(?:\.[^.<>"\s@]+)+/;
Eric Wongdb3106b2006-05-15 02:41:01 -07001030
1031 # check for a local address:
Junio C Hamanoad9c18f2006-06-06 00:05:56 -07001032 return $address if ($address =~ /^($local_part_regexp)$/);
Eric Wongdb3106b2006-05-15 02:41:01 -07001033
Uwe Kleine-König155197e2007-08-09 15:27:57 +02001034 $address =~ s/^\s*<(.*)>\s*$/$1/;
Eric Wong567ffeb2006-03-25 16:47:12 -08001035 if ($have_email_valid) {
Junio C Hamanoad9c18f2006-06-06 00:05:56 -07001036 return scalar Email::Valid->address($address);
Eric Wong567ffeb2006-03-25 16:47:12 -08001037 }
Krzysztof Mazur95c0d4b2012-11-22 19:12:09 +01001038
1039 # less robust/correct than the monster regexp in Email::Valid,
1040 # but still does a 99% job, and one less dependency
1041 return $1 if $address =~ /($local_part_regexp\@$domain_regexp)/;
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -07001042 return;
Eric Wong567ffeb2006-03-25 16:47:12 -08001043}
Ryan Anderson83b24432005-07-31 04:17:25 -04001044
Krzysztof Mazure4312252012-11-22 19:12:10 +01001045sub extract_valid_address_or_die {
1046 my $address = shift;
1047 $address = extract_valid_address($address);
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001048 die sprintf(__("error: unable to extract a valid address from: %s\n"), $address)
Krzysztof Mazure4312252012-11-22 19:12:10 +01001049 if !$address;
1050 return $address;
1051}
1052
1053sub validate_address {
1054 my $address = shift;
Krzysztof Mazurd0e98102012-11-22 19:12:12 +01001055 while (!extract_valid_address($address)) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001056 printf STDERR __("error: unable to extract a valid address from: %s\n"), $address;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001057 # TRANSLATORS: Make sure to include [q] [d] [e] in your
1058 # translation. The program will only accept English input
1059 # at this point.
1060 $_ = ask(__("What to do with this address? ([q]uit|[d]rop|[e]dit): "),
Krzysztof Mazurd0e98102012-11-22 19:12:12 +01001061 valid_re => qr/^(?:quit|q|drop|d|edit|e)/i,
Krzysztof Mazur5c80afe2012-11-22 19:12:11 +01001062 default => 'q');
1063 if (/^d/i) {
1064 return undef;
1065 } elsif (/^q/i) {
1066 cleanup_compose_files();
1067 exit(0);
1068 }
Junio C Hamano0d6b21e2016-04-24 12:31:44 -07001069 $address = ask("$to_whom ",
Krzysztof Mazurd0e98102012-11-22 19:12:12 +01001070 default => "",
1071 valid_re => qr/\@.*\./, confirm_only => 1);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001072 }
1073 return $address;
1074}
1075
1076sub validate_address_list {
1077 return (grep { defined $_ }
1078 map { validate_address($_) } @_);
Ryan Anderson83b24432005-07-31 04:17:25 -04001079}
1080
1081# Usually don't need to change anything below here.
1082
1083# we make a "fake" message id by taking the current number
1084# of seconds since the beginning of Unix time and tacking on
1085# a random number to the end, in case we are called quicker than
1086# 1 second since the last time we were called.
Ryan Anderson8037d1a2005-07-31 20:04:24 -04001087
1088# We'll setup a template for the message id, using the "from" address:
Ryan Anderson8037d1a2005-07-31 20:04:24 -04001089
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001090my ($message_id_stamp, $message_id_serial);
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001091sub make_message_id {
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001092 my $uniq;
1093 if (!defined $message_id_stamp) {
Eric Wongf916ab02016-04-06 20:07:14 +00001094 $message_id_stamp = strftime("%Y%m%d%H%M%S.$$", gmtime(time));
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001095 $message_id_serial = 0;
1096 }
1097 $message_id_serial++;
1098 $uniq = "$message_id_stamp-$message_id_serial";
1099
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001100 my $du_part;
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001101 for ($sender, $repocommitter, $repoauthor) {
1102 $du_part = extract_valid_address(sanitize_address($_));
1103 last if (defined $du_part and $du_part ne '');
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001104 }
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001105 if (not defined $du_part or $du_part eq '') {
Ævar Arnfjörð Bjarmason529dd382010-09-30 13:43:08 +00001106 require Sys::Hostname;
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001107 $du_part = 'user@' . Sys::Hostname::hostname();
1108 }
Eric Wongf916ab02016-04-06 20:07:14 +00001109 my $message_id_template = "<%s-%s>";
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001110 $message_id = sprintf($message_id_template, $uniq, $du_part);
Ryan Anderson8037d1a2005-07-31 20:04:24 -04001111 #print "new message id = $message_id\n"; # Was useful for debugging
Ryan Anderson83b24432005-07-31 04:17:25 -04001112}
1113
1114
1115
Eric Wonga5370b12006-03-25 03:01:01 -08001116$time = time - scalar $#files;
Ryan Anderson83b24432005-07-31 04:17:25 -04001117
Jürgen Rühle374c5902007-01-10 13:36:39 -08001118sub unquote_rfc2047 {
1119 local ($_) = @_;
Роман Донченко11f70a72014-12-14 18:59:46 +03001120 my $charset;
Роман Донченкоab47e2a2014-12-14 18:59:47 +03001121 my $sep = qr/[ \t]+/;
1122 s{$re_encoded_word(?:$sep$re_encoded_word)*}{
1123 my @words = split $sep, $&;
1124 foreach (@words) {
1125 m/$re_encoded_word/;
1126 $charset = $1;
1127 my $encoding = $2;
1128 my $text = $3;
1129 if ($encoding eq 'q' || $encoding eq 'Q') {
1130 $_ = $text;
1131 s/_/ /g;
1132 s/=([0-9A-F]{2})/chr(hex($1))/egi;
1133 } else {
1134 # other encodings not supported yet
1135 }
Роман Донченко11f70a72014-12-14 18:59:46 +03001136 }
Роман Донченкоab47e2a2014-12-14 18:59:47 +03001137 join '', @words;
Thomas Rastb622d4d2012-07-30 21:25:40 +02001138 }eg;
Роман Донченко11f70a72014-12-14 18:59:46 +03001139 return wantarray ? ($_, $charset) : $_;
Jürgen Rühle374c5902007-01-10 13:36:39 -08001140}
1141
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001142sub quote_rfc2047 {
1143 local $_ = shift;
Brandon Caseyd1fff6f2009-06-06 20:12:31 -05001144 my $encoding = shift || 'UTF-8';
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001145 s/([^-a-zA-Z0-9!*+\/])/sprintf("=%02X", ord($1))/eg;
1146 s/(.*)/=\?$encoding\?q\?$1\?=/;
1147 return $_;
1148}
1149
Brandon Caseya3a82622009-06-07 19:25:58 -05001150sub is_rfc2047_quoted {
1151 my $s = shift;
Brandon Caseya3a82622009-06-07 19:25:58 -05001152 length($s) <= 75 &&
Роман Донченко11f70a72014-12-14 18:59:46 +03001153 $s =~ m/^(?:"[[:ascii:]]*"|$re_encoded_word)$/o;
Brandon Caseya3a82622009-06-07 19:25:58 -05001154}
1155
Krzysztof Mazurce547802012-10-24 23:08:26 +02001156sub subject_needs_rfc2047_quoting {
1157 my $s = shift;
1158
Krzysztof Mazurce1459f2012-10-24 23:28:29 +02001159 return ($s =~ /[^[:ascii:]]/) || ($s =~ /=\?/);
Krzysztof Mazurce547802012-10-24 23:08:26 +02001160}
1161
1162sub quote_subject {
1163 local $subject = shift;
1164 my $encoding = shift || 'UTF-8';
1165
1166 if (subject_needs_rfc2047_quoting($subject)) {
1167 return quote_rfc2047($subject, $encoding);
1168 }
1169 return $subject;
1170}
1171
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001172# use the simplest quoting being able to handle the recipient
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001173sub sanitize_address {
Robin H. Johnson732263d2007-04-25 19:37:19 -07001174 my ($recipient) = @_;
Krzysztof Mazur831a4882012-11-22 19:12:08 +01001175
1176 # remove garbage after email address
1177 $recipient =~ s/(.*>).*$/$1/;
1178
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001179 my ($recipient_name, $recipient_addr) = ($recipient =~ /^(.*?)\s*(<.*)/);
1180
1181 if (not $recipient_name) {
Ævar Arnfjörð Bjarmasonff483892010-09-30 13:43:02 +00001182 return $recipient;
Robin H. Johnson732263d2007-04-25 19:37:19 -07001183 }
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001184
1185 # if recipient_name is already quoted, do nothing
Brandon Caseya3a82622009-06-07 19:25:58 -05001186 if (is_rfc2047_quoted($recipient_name)) {
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001187 return $recipient;
1188 }
1189
Remi Lespinet1fe97032015-06-30 14:16:49 +02001190 # remove non-escaped quotes
1191 $recipient_name =~ s/(^|[^\\])"/$1/g;
1192
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001193 # rfc2047 is needed if a non-ascii char is included
1194 if ($recipient_name =~ /[^[:ascii:]]/) {
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001195 $recipient_name = quote_rfc2047($recipient_name);
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001196 }
1197
1198 # double quotes are needed if specials or CTLs are included
1199 elsif ($recipient_name =~ /[][()<>@,;:\\".\000-\037\177]/) {
Remi Lespinet1fe97032015-06-30 14:16:49 +02001200 $recipient_name =~ s/([\\\r])/\\$1/g;
Ævar Arnfjörð Bjarmasond5c7d692010-09-30 13:43:03 +00001201 $recipient_name = qq["$recipient_name"];
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001202 }
1203
1204 return "$recipient_name $recipient_addr";
1205
Robin H. Johnson732263d2007-04-25 19:37:19 -07001206}
1207
Matthieu Moycb2922f2017-08-23 12:21:01 +02001208sub strip_garbage_one_address {
1209 my ($addr) = @_;
1210 chomp $addr;
1211 if ($addr =~ /^(("[^"]*"|[^"<]*)? *<[^>]*>).*/) {
1212 # "Foo Bar" <foobar@example.com> [possibly garbage here]
1213 # Foo Bar <foobar@example.com> [possibly garbage here]
1214 return $1;
1215 }
1216 if ($addr =~ /^(<[^>]*>).*/) {
1217 # <foo@example.com> [possibly garbage here]
1218 # if garbage contains other addresses, they are ignored.
1219 return $1;
1220 }
1221 if ($addr =~ /^([^"#,\s]*)/) {
1222 # address without quoting: remove anything after the address
1223 return $1;
1224 }
1225 return $addr;
1226}
1227
Krzysztof Mazure4312252012-11-22 19:12:10 +01001228sub sanitize_address_list {
1229 return (map { sanitize_address($_) } @_);
1230}
1231
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001232sub process_address_list {
Remi Lespinetb1c8a112015-06-30 14:16:50 +02001233 my @addr_list = map { parse_address_line($_) } @_;
1234 @addr_list = expand_aliases(@addr_list);
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001235 @addr_list = sanitize_address_list(@addr_list);
1236 @addr_list = validate_address_list(@addr_list);
1237 return @addr_list;
1238}
1239
Jari Aalto134550f2010-03-14 17:16:45 +02001240# Returns the local Fully Qualified Domain Name (FQDN) if available.
1241#
1242# Tightly configured MTAa require that a caller sends a real DNS
1243# domain name that corresponds the IP address in the HELO/EHLO
1244# handshake. This is used to verify the connection and prevent
1245# spammers from trying to hide their identity. If the DNS and IP don't
Elijah Newren15beaaa2019-11-05 17:07:23 +00001246# match, the receiving MTA may deny the connection.
Jari Aalto134550f2010-03-14 17:16:45 +02001247#
1248# Here is a deny example of Net::SMTP with the default "localhost.localdomain"
1249#
1250# Net::SMTP=GLOB(0x267ec28)>>> EHLO localhost.localdomain
1251# Net::SMTP=GLOB(0x267ec28)<<< 550 EHLO argument does not match calling host
1252#
1253# This maildomain*() code is based on ideas in Perl library Test::Reporter
1254# /usr/share/perl5/Test/Reporter/Mail/Util.pm ==> sub _maildomain ()
1255
Brian Gernhardt59a86302010-04-10 10:53:54 -04001256sub valid_fqdn {
1257 my $domain = shift;
Brandon Casey61ef5e92010-09-26 22:18:01 -05001258 return defined $domain && !($^O eq 'darwin' && $domain =~ /\.local$/) && $domain =~ /\./;
Brian Gernhardt59a86302010-04-10 10:53:54 -04001259}
1260
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001261sub maildomain_net {
Jari Aalto134550f2010-03-14 17:16:45 +02001262 my $maildomain;
1263
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001264 my $domain = Net::Domain::domainname();
1265 $maildomain = $domain if valid_fqdn($domain);
Jari Aalto134550f2010-03-14 17:16:45 +02001266
1267 return $maildomain;
1268}
1269
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001270sub maildomain_mta {
Jari Aalto134550f2010-03-14 17:16:45 +02001271 my $maildomain;
1272
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001273 for my $host (qw(mailhost localhost)) {
1274 my $smtp = Net::SMTP->new($host);
1275 if (defined $smtp) {
1276 my $domain = $smtp->domain;
1277 $smtp->quit;
Jari Aalto134550f2010-03-14 17:16:45 +02001278
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001279 $maildomain = $domain if valid_fqdn($domain);
Jari Aalto134550f2010-03-14 17:16:45 +02001280
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001281 last if $maildomain;
Jari Aalto134550f2010-03-14 17:16:45 +02001282 }
1283 }
1284
1285 return $maildomain;
1286}
1287
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001288sub maildomain {
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001289 return maildomain_net() || maildomain_mta() || 'localhost.localdomain';
Jari Aalto134550f2010-03-14 17:16:45 +02001290}
1291
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001292sub smtp_host_string {
1293 if (defined $smtp_server_port) {
1294 return "$smtp_server:$smtp_server_port";
1295 } else {
1296 return $smtp_server;
1297 }
1298}
1299
1300# Returns 1 if authentication succeeded or was not necessary
1301# (smtp_user was not specified), and 0 otherwise.
1302
1303sub smtp_auth_maybe {
Joshua Watt8dd9b3f2018-10-22 22:24:25 -05001304 if (!defined $smtp_authuser || $auth || (defined $smtp_auth && $smtp_auth eq "none")) {
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001305 return 1;
1306 }
1307
1308 # Workaround AUTH PLAIN/LOGIN interaction defect
1309 # with Authen::SASL::Cyrus
1310 eval {
1311 require Authen::SASL;
1312 Authen::SASL->import(qw(Perl));
1313 };
1314
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001315 # Check mechanism naming as defined in:
1316 # https://tools.ietf.org/html/rfc4422#page-8
Brian Norris904f6e72015-09-18 15:12:50 -07001317 if ($smtp_auth && $smtp_auth !~ /^(\b[A-Z0-9-_]{1,20}\s*)*$/) {
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001318 die "invalid smtp auth: '${smtp_auth}'";
1319 }
1320
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001321 # TODO: Authentication may fail not because credentials were
1322 # invalid but due to other reasons, in which we should not
1323 # reject credentials.
1324 $auth = Git::credential({
1325 'protocol' => 'smtp',
1326 'host' => smtp_host_string(),
1327 'username' => $smtp_authuser,
1328 # if there's no password, "git credential fill" will
1329 # give us one, otherwise it'll just pass this one.
1330 'password' => $smtp_authpass
1331 }, sub {
1332 my $cred = shift;
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001333
1334 if ($smtp_auth) {
1335 my $sasl = Authen::SASL->new(
1336 mechanism => $smtp_auth,
1337 callback => {
1338 user => $cred->{'username'},
1339 pass => $cred->{'password'},
1340 authname => $cred->{'username'},
1341 }
1342 );
1343
1344 return !!$smtp->auth($sasl);
1345 }
1346
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001347 return !!$smtp->auth($cred->{'username'}, $cred->{'password'});
1348 });
1349
1350 return $auth;
1351}
1352
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001353sub ssl_verify_params {
1354 eval {
1355 require IO::Socket::SSL;
1356 IO::Socket::SSL->import(qw/SSL_VERIFY_PEER SSL_VERIFY_NONE/);
1357 };
1358 if ($@) {
1359 print STDERR "Not using SSL_VERIFY_PEER due to out-of-date IO::Socket::SSL.\n";
1360 return;
1361 }
1362
1363 if (!defined $smtp_ssl_cert_path) {
Ruben Kerkhof01645b72014-01-15 21:31:11 +04001364 # use the OpenSSL defaults
1365 return (SSL_verify_mode => SSL_VERIFY_PEER());
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001366 }
1367
1368 if ($smtp_ssl_cert_path eq "") {
1369 return (SSL_verify_mode => SSL_VERIFY_NONE());
1370 } elsif (-d $smtp_ssl_cert_path) {
1371 return (SSL_verify_mode => SSL_VERIFY_PEER(),
1372 SSL_ca_path => $smtp_ssl_cert_path);
1373 } elsif (-f $smtp_ssl_cert_path) {
1374 return (SSL_verify_mode => SSL_VERIFY_PEER(),
1375 SSL_ca_file => $smtp_ssl_cert_path);
1376 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001377 die sprintf(__("CA path \"%s\" does not exist"), $smtp_ssl_cert_path);
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001378 }
1379}
1380
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001381sub file_name_is_absolute {
1382 my ($path) = @_;
1383
1384 # msys does not grok DOS drive-prefixes
1385 if ($^O eq 'msys') {
Junio C Hamanof24ecf52014-04-23 09:37:38 -07001386 return ($path =~ m#^/# || $path =~ m#^[a-zA-Z]\:#)
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001387 }
1388
1389 require File::Spec::Functions;
1390 return File::Spec::Functions::file_name_is_absolute($path);
1391}
1392
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001393# Prepares the email, then asks the user what to do.
1394#
1395# If the user chooses to send the email, it's sent and 1 is returned.
1396# If the user chooses not to send the email, 0 is returned.
1397# If the user decides they want to make further edits, -1 is returned and the
1398# caller is expected to call send_message again after the edits are performed.
1399#
1400# If an error occurs sending the email, this just dies.
Michael Witten15da1082009-04-13 13:23:51 -05001401
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001402sub send_message {
Eric Wong4bc87a22006-03-25 17:20:48 -08001403 my @recipients = unique_email_list(@to);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001404 @cc = (grep { my $cc = extract_valid_address_or_die($_);
Joe Perches83acaae2010-11-20 15:06:05 -08001405 not grep { $cc eq $_ || $_ =~ /<\Q${cc}\E>$/ } @recipients
Ask Bjørn Hansen7ac17522007-11-19 03:00:26 -08001406 }
Ask Bjørn Hansen7ac17522007-11-19 03:00:26 -08001407 @cc);
Eric Wong4bc87a22006-03-25 17:20:48 -08001408 my $to = join (",\n\t", @recipients);
Ævar Arnfjörð Bjarmasone60f6d12019-05-09 13:48:29 +02001409 @recipients = unique_email_list(@recipients,@cc,@initial_bcc);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001410 @recipients = (map { extract_valid_address_or_die($_) } @recipients);
Jakub Narebski6bdca892006-07-07 20:57:55 +02001411 my $date = format_2822_time($time++);
Martin Langhoffe923eff2006-05-03 09:44:36 +12001412 my $gitversion = '@@GIT_VERSION@@';
1413 if ($gitversion =~ m/..GIT_VERSION../) {
Petr Baudis3cb8caf2006-07-03 22:47:58 +02001414 $gitversion = Git::version();
Martin Langhoffe923eff2006-05-03 09:44:36 +12001415 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001416
Joe Perches02461e02009-10-08 10:03:26 -07001417 my $cc = join(",\n\t", unique_email_list(@cc));
Junio C Hamanof06a6a42007-04-16 16:51:47 -07001418 my $ccline = "";
1419 if ($cc ne '') {
1420 $ccline = "\nCc: $cc";
1421 }
Jeff King4f3d3702007-12-17 15:51:34 -05001422 make_message_id() unless defined($message_id);
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001423
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001424 my $header = "From: $sender
Junio C Hamanof06a6a42007-04-16 16:51:47 -07001425To: $to${ccline}
Eric Wong4bc87a22006-03-25 17:20:48 -08001426Subject: $subject
Eric Wong4bc87a22006-03-25 17:20:48 -08001427Date: $date
1428Message-Id: $message_id
Eric Wong4bc87a22006-03-25 17:20:48 -08001429";
Luis Henriquesac1596a2014-03-24 21:38:27 +00001430 if ($use_xmailer) {
1431 $header .= "X-Mailer: git-send-email $gitversion\n";
1432 }
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001433 if ($in_reply_to) {
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001434
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001435 $header .= "In-Reply-To: $in_reply_to\n";
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001436 $header .= "References: $references\n";
1437 }
Christian Ludwigd11c9432018-03-04 00:58:14 +01001438 if ($reply_to) {
1439 $header .= "Reply-To: $reply_to\n";
1440 }
Junio C Hamanoce91c2f2006-10-05 16:36:49 -07001441 if (@xh) {
1442 $header .= join("\n", @xh) . "\n";
1443 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001444
Robin H. Johnsonc38f0242007-04-25 19:37:20 -07001445 my @sendmail_parameters = ('-i', @recipients);
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001446 my $raw_from = $sender;
Felipe Contrerasc89e3242009-11-26 21:04:29 +02001447 if (defined $envelope_sender && $envelope_sender ne "auto") {
1448 $raw_from = $envelope_sender;
1449 }
Robin H. Johnsonf073a592007-04-25 19:37:22 -07001450 $raw_from = extract_valid_address($raw_from);
1451 unshift (@sendmail_parameters,
1452 '-f', $raw_from) if(defined $envelope_sender);
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001453
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001454 if ($needs_confirm && !$dry_run) {
1455 print "\n$header\n";
1456 if ($needs_confirm eq "inform") {
1457 $confirm_unconfigured = 0; # squelch this message for the rest of this run
Jay Soffian6e182512009-03-28 21:39:10 -04001458 $ask_default = "y"; # assume yes on EOF since user hasn't explicitly asked for confirmation
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001459 print __ <<EOF ;
1460 The Cc list above has been expanded by additional
1461 addresses found in the patch commit message. By default
1462 send-email prompts before sending whenever this occurs.
1463 This behavior is controlled by the sendemail.confirm
1464 configuration setting.
1465
1466 For additional information, run 'git send-email --help'.
1467 To retain the current behavior, but squelch this message,
1468 run 'git config --global sendemail.confirm auto'.
1469
1470EOF
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001471 }
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001472 # TRANSLATORS: Make sure to include [y] [n] [e] [q] [a] in your
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001473 # translation. The program will only accept English input
1474 # at this point.
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001475 $_ = ask(__("Send this email? ([y]es|[n]o|[e]dit|[q]uit|[a]ll): "),
1476 valid_re => qr/^(?:yes|y|no|n|edit|e|quit|q|all|a)/i,
Jay Soffian6e182512009-03-28 21:39:10 -04001477 default => $ask_default);
Vasco Almeida46493102016-12-14 11:54:36 -01001478 die __("Send this email reply required") unless defined $_;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001479 if (/^n/i) {
Michael Witten15da1082009-04-13 13:23:51 -05001480 return 0;
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001481 } elsif (/^e/i) {
1482 return -1;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001483 } elsif (/^q/i) {
1484 cleanup_compose_files();
1485 exit(0);
1486 } elsif (/^a/i) {
1487 $confirm = 'never';
1488 }
1489 }
1490
Pascal Obry052fbea2010-09-06 20:12:11 +02001491 unshift (@sendmail_parameters, @smtp_server_options);
1492
Matthew Wilcox61302592006-10-10 08:58:23 -06001493 if ($dry_run) {
1494 # We don't want to send the email.
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001495 } elsif (file_name_is_absolute($smtp_server)) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001496 my $pid = open my $sm, '|-';
1497 defined $pid or die $!;
1498 if (!$pid) {
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001499 exec($smtp_server, @sendmail_parameters) or die $!;
Eric Wongaca7ad72006-05-15 02:34:44 -07001500 }
1501 print $sm "$header\n$message";
Ævar Arnfjörð Bjarmason5e2c2ab2010-09-30 13:43:07 +00001502 close $sm or die $!;
Eric Wongaca7ad72006-05-15 02:34:44 -07001503 } else {
Junio C Hamano44b24762007-09-25 17:27:54 -07001504
1505 if (!defined $smtp_server) {
Vasco Almeida46493102016-12-14 11:54:36 -01001506 die __("The required SMTP server is not properly defined.")
Junio C Hamano44b24762007-09-25 17:27:54 -07001507 }
1508
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001509 require Net::SMTP;
Jonathan Niederbfbfc9a2017-05-31 17:17:43 -07001510 my $use_net_smtp_ssl = version->parse($Net::SMTP::VERSION) < version->parse("2.34");
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001511 $smtp_domain ||= maildomain();
1512
Thomas Rastf6bebd12008-06-25 21:42:43 +02001513 if ($smtp_encryption eq 'ssl') {
Junio C Hamano44b24762007-09-25 17:27:54 -07001514 $smtp_server_port ||= 465; # ssmtp
Thomas Rast5508f3e2013-12-01 23:48:43 +01001515 require IO::Socket::SSL;
John Keeping9d605242015-12-03 21:47:18 +00001516
1517 # Suppress "variable accessed once" warning.
1518 {
1519 no warnings 'once';
1520 $IO::Socket::SSL::DEBUG = 1;
1521 }
1522
Thomas Rast5508f3e2013-12-01 23:48:43 +01001523 # Net::SMTP::SSL->new() does not forward any SSL options
1524 IO::Socket::SSL::set_client_defaults(
1525 ssl_verify_params());
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001526
1527 if ($use_net_smtp_ssl) {
1528 require Net::SMTP::SSL;
1529 $smtp ||= Net::SMTP::SSL->new($smtp_server,
1530 Hello => $smtp_domain,
1531 Port => $smtp_server_port,
1532 Debug => $debug_net_smtp);
1533 }
1534 else {
1535 $smtp ||= Net::SMTP->new($smtp_server,
1536 Hello => $smtp_domain,
1537 Port => $smtp_server_port,
1538 Debug => $debug_net_smtp,
1539 SSL => 1);
1540 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001541 }
Jules Maselbas636f3d72018-07-14 10:58:48 +02001542 elsif (!$smtp) {
brian m. carlson1a741bf2013-07-04 22:04:52 +00001543 $smtp_server_port ||= 25;
1544 $smtp ||= Net::SMTP->new($smtp_server,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001545 Hello => $smtp_domain,
brian m. carlson1a741bf2013-07-04 22:04:52 +00001546 Debug => $debug_net_smtp,
1547 Port => $smtp_server_port);
Yakov Lernerfb3650e2009-09-25 15:10:21 -07001548 if ($smtp_encryption eq 'tls' && $smtp) {
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001549 if ($use_net_smtp_ssl) {
1550 $smtp->command('STARTTLS');
1551 $smtp->response();
1552 if ($smtp->code != 220) {
1553 die sprintf(__("Server does not support STARTTLS! %s"), $smtp->message);
1554 }
1555 require Net::SMTP::SSL;
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001556 $smtp = Net::SMTP::SSL->start_SSL($smtp,
1557 ssl_verify_params())
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001558 or die sprintf(__("STARTTLS failed! %s"), IO::Socket::SSL::errstr());
Thomas Rastf6bebd12008-06-25 21:42:43 +02001559 }
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001560 else {
1561 $smtp->starttls(ssl_verify_params())
1562 or die sprintf(__("STARTTLS failed! %s"), IO::Socket::SSL::errstr());
1563 }
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001564 # Send EHLO again to receive fresh
1565 # supported commands
1566 $smtp->hello($smtp_domain);
Thomas Rastf6bebd12008-06-25 21:42:43 +02001567 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001568 }
Junio C Hamano44b24762007-09-25 17:27:54 -07001569
1570 if (!$smtp) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001571 die __("Unable to initialize SMTP properly. Check config and use --smtp-debug."),
1572 " VALUES: server=$smtp_server ",
Jari Aaltoe5afb3a2010-03-14 17:15:33 +02001573 "encryption=$smtp_encryption ",
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001574 "hello=$smtp_domain",
Sylvain Rabota1dd7e12011-04-29 20:23:24 +02001575 defined $smtp_server_port ? " port=$smtp_server_port" : "";
Junio C Hamano44b24762007-09-25 17:27:54 -07001576 }
1577
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001578 smtp_auth_maybe or die $smtp->message;
Michael Witten2363d742008-02-03 19:53:56 -05001579
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001580 $smtp->mail( $raw_from ) or die $smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001581 $smtp->to( @recipients ) or die $smtp->message;
1582 $smtp->data or die $smtp->message;
Stefan Agnerf60c4832015-09-30 09:26:09 +02001583 $smtp->datasend("$header\n") or die $smtp->message;
1584 my @lines = split /^/, $message;
1585 foreach my $line (@lines) {
1586 $smtp->datasend("$line") or die $smtp->message;
1587 }
Eric Wongaca7ad72006-05-15 02:34:44 -07001588 $smtp->dataend() or die $smtp->message;
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001589 $smtp->code =~ /250|200/ or die sprintf(__("Failed to send %s\n"), $subject).$smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001590 }
Ryan Anderson27184352006-02-05 20:13:52 -05001591 if ($quiet) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001592 printf($dry_run ? __("Dry-Sent %s\n") : __("Sent %s\n"), $subject);
Ryan Anderson27184352006-02-05 20:13:52 -05001593 } else {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001594 print($dry_run ? __("Dry-OK. Log says:\n") : __("OK. Log says:\n"));
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001595 if (!file_name_is_absolute($smtp_server)) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001596 print "Server: $smtp_server\n";
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001597 print "MAIL FROM:<$raw_from>\n";
Joe Perches02461e02009-10-08 10:03:26 -07001598 foreach my $entry (@recipients) {
1599 print "RCPT TO:<$entry>\n";
1600 }
Eric Wongaca7ad72006-05-15 02:34:44 -07001601 } else {
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001602 print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001603 }
David D. Kilzerb7f30e02007-11-18 20:14:55 -08001604 print $header, "\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001605 if ($smtp) {
Vasco Almeida46493102016-12-14 11:54:36 -01001606 print __("Result: "), $smtp->code, ' ',
Eric Wongaca7ad72006-05-15 02:34:44 -07001607 ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
1608 } else {
Vasco Almeida46493102016-12-14 11:54:36 -01001609 print __("Result: OK\n");
Eric Wongaca7ad72006-05-15 02:34:44 -07001610 }
Ryan Anderson30d08b32006-02-02 11:56:06 -05001611 }
Michael Witten15da1082009-04-13 13:23:51 -05001612
1613 return 1;
Ryan Anderson83b24432005-07-31 04:17:25 -04001614}
1615
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001616$in_reply_to = $initial_in_reply_to;
1617$references = $initial_in_reply_to || '';
Ryan Anderson83b24432005-07-31 04:17:25 -04001618$subject = $initial_subject;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001619$message_num = 0;
Ryan Anderson83b24432005-07-31 04:17:25 -04001620
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001621# Prepares the email, prompts the user, sends it out
1622# Returns 0 if an edit was done and the function should be called again, or 1
1623# otherwise.
1624sub process_file {
1625 my ($t) = @_;
1626
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001627 open my $fh, "<", $t or die sprintf(__("can't open file %s"), $t);
Ryan Anderson83b24432005-07-31 04:17:25 -04001628
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001629 my $author = undef;
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001630 my $sauthor = undef;
Jeff King8291db62007-11-16 05:49:09 -05001631 my $author_encoding;
1632 my $has_content_type;
1633 my $body_encoding;
Paolo Bonzinibb294562014-11-25 15:00:26 +01001634 my $xfer_encoding;
1635 my $has_mime_version;
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001636 @to = ();
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001637 @cc = ();
Junio C Hamanoce91c2f2006-10-05 16:36:49 -07001638 @xh = ();
Junio C Hamanoe6b09642006-10-07 03:09:05 -07001639 my $input_format = undef;
Jay Soffian50126992009-02-14 23:32:14 -05001640 my @header = ();
Ryan Anderson83b24432005-07-31 04:17:25 -04001641 $message = "";
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001642 $message_num++;
Jay Soffian50126992009-02-14 23:32:14 -05001643 # First unfold multiline header fields
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001644 while(<$fh>) {
Jay Soffian50126992009-02-14 23:32:14 -05001645 last if /^\s*$/;
1646 if (/^\s+\S/ and @header) {
1647 chomp($header[$#header]);
1648 s/^\s+/ /;
1649 $header[$#header] .= $_;
1650 } else {
1651 push(@header, $_);
1652 }
1653 }
1654 # Now parse the header
1655 foreach(@header) {
1656 if (/^From /) {
1657 $input_format = 'mbox';
1658 next;
1659 }
1660 chomp;
1661 if (!defined $input_format && /^[-A-Za-z]+:\s/) {
1662 $input_format = 'mbox';
1663 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001664
Jay Soffian50126992009-02-14 23:32:14 -05001665 if (defined $input_format && $input_format eq 'mbox') {
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001666 if (/^Subject:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001667 $subject = $1;
1668 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001669 elsif (/^From:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001670 ($author, $author_encoding) = unquote_rfc2047($1);
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001671 $sauthor = sanitize_address($author);
Jay Soffian50126992009-02-14 23:32:14 -05001672 next if $suppress_cc{'author'};
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001673 next if $suppress_cc{'self'} and $sauthor eq $sender;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001674 printf(__("(mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001675 $1, $_) unless $quiet;
1676 push @cc, $1;
1677 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001678 elsif (/^To:\s+(.*)$/i) {
Stephen Boyd21802cd2010-09-29 00:26:44 -07001679 foreach my $addr (parse_address_line($1)) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001680 printf(__("(mbox) Adding to: %s from line '%s'\n"),
Stephen Boyd21802cd2010-09-29 00:26:44 -07001681 $addr, $_) unless $quiet;
Krzysztof Mazure4312252012-11-22 19:12:10 +01001682 push @to, $addr;
Stephen Boyd21802cd2010-09-29 00:26:44 -07001683 }
1684 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001685 elsif (/^Cc:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001686 foreach my $addr (parse_address_line($1)) {
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001687 my $qaddr = unquote_rfc2047($addr);
1688 my $saddr = sanitize_address($qaddr);
1689 if ($saddr eq $sender) {
David Brown65648282007-12-25 19:56:29 -08001690 next if ($suppress_cc{'self'});
David Brown65648282007-12-25 19:56:29 -08001691 } else {
1692 next if ($suppress_cc{'cc'});
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001693 }
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001694 printf(__("(mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001695 $addr, $_) unless $quiet;
1696 push @cc, $addr;
Ryan Anderson83b24432005-07-31 04:17:25 -04001697 }
1698 }
Jay Soffian50126992009-02-14 23:32:14 -05001699 elsif (/^Content-type:/i) {
1700 $has_content_type = 1;
1701 if (/charset="?([^ "]+)/) {
1702 $body_encoding = $1;
1703 }
1704 push @xh, $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001705 }
Paolo Bonzinibb294562014-11-25 15:00:26 +01001706 elsif (/^MIME-Version/i) {
1707 $has_mime_version = 1;
1708 push @xh, $_;
1709 }
Jay Soffian50126992009-02-14 23:32:14 -05001710 elsif (/^Message-Id: (.*)/i) {
1711 $message_id = $1;
1712 }
Paolo Bonzinibb294562014-11-25 15:00:26 +01001713 elsif (/^Content-Transfer-Encoding: (.*)/i) {
1714 $xfer_encoding = $1 if not defined $xfer_encoding;
1715 }
Stefan Agner256be1d2018-04-17 23:16:30 +02001716 elsif (/^In-Reply-To: (.*)/i) {
Rafael Aquinif9f60d72020-06-29 10:11:04 -04001717 if (!$initial_in_reply_to || $thread) {
1718 $in_reply_to = $1;
1719 }
Stefan Agner256be1d2018-04-17 23:16:30 +02001720 }
1721 elsif (/^References: (.*)/i) {
Rafael Aquinif9f60d72020-06-29 10:11:04 -04001722 if (!$initial_in_reply_to || $thread) {
1723 $references = $1;
1724 }
Stefan Agner256be1d2018-04-17 23:16:30 +02001725 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001726 elsif (!/^Date:\s/i && /^[-A-Za-z]+:\s+\S/) {
Jay Soffian50126992009-02-14 23:32:14 -05001727 push @xh, $_;
1728 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001729 } else {
Jay Soffian50126992009-02-14 23:32:14 -05001730 # In the traditional
1731 # "send lots of email" format,
1732 # line 1 = cc
1733 # line 2 = subject
1734 # So let's support that, too.
1735 $input_format = 'lots';
1736 if (@cc == 0 && !$suppress_cc{'cc'}) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001737 printf(__("(non-mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001738 $_, $_) unless $quiet;
1739 push @cc, $_;
1740 } elsif (!defined $subject) {
1741 $subject = $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001742 }
1743 }
1744 }
Jay Soffian50126992009-02-14 23:32:14 -05001745 # Now parse the message body
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001746 while(<$fh>) {
Jay Soffian50126992009-02-14 23:32:14 -05001747 $message .= $_;
Baruch Siach9c545812019-03-16 21:26:50 +02001748 if (/^([a-z][a-z-]*-by|Cc): (.*)/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001749 chomp;
Jay Soffian3531e272009-02-14 23:32:15 -05001750 my ($what, $c) = ($1, $2);
Matthieu Moycb2922f2017-08-23 12:21:01 +02001751 # strip garbage for the address we'll use:
1752 $c = strip_garbage_one_address($c);
1753 # sanitize a bit more to decide whether to suppress the address:
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001754 my $sc = sanitize_address($c);
1755 if ($sc eq $sender) {
Jay Soffian3531e272009-02-14 23:32:15 -05001756 next if ($suppress_cc{'self'});
1757 } else {
Rasmus Villemoesef0cc1d2018-10-16 09:39:23 +02001758 if ($what =~ /^Signed-off-by$/i) {
1759 next if $suppress_cc{'sob'};
1760 } elsif ($what =~ /-by$/i) {
1761 next if $suppress_cc{'misc-by'};
1762 } elsif ($what =~ /Cc/i) {
1763 next if $suppress_cc{'bodycc'};
1764 }
Jay Soffian3531e272009-02-14 23:32:15 -05001765 }
Rasmus Villemoesfb34fe62018-10-10 13:13:50 +02001766 if ($c !~ /.+@.+|<.+>/) {
1767 printf("(body) Ignoring %s from line '%s'\n",
1768 $what, $_) unless $quiet;
1769 next;
1770 }
Jay Soffian50126992009-02-14 23:32:14 -05001771 push @cc, $c;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001772 printf(__("(body) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001773 $c, $_) unless $quiet;
1774 }
1775 }
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001776 close $fh;
Joe Perches324a8bd2007-08-17 18:51:12 -07001777
Joe Perches6e74e072010-09-24 10:03:00 -07001778 push @to, recipients_cmd("to-cmd", "to", $to_cmd, $t)
1779 if defined $to_cmd;
1780 push @cc, recipients_cmd("cc-cmd", "cc", $cc_cmd, $t)
1781 if defined $cc_cmd && !$suppress_cc{'cccmd'};
Joe Perches324a8bd2007-08-17 18:51:12 -07001782
Thomas Rast3cae7e52010-06-17 22:10:39 +02001783 if ($broken_encoding{$t} && !$has_content_type) {
Paolo Bonzinibb294562014-11-25 15:00:26 +01001784 $xfer_encoding = '8bit' if not defined $xfer_encoding;
Thomas Rast3cae7e52010-06-17 22:10:39 +02001785 $has_content_type = 1;
Paolo Bonzinibb294562014-11-25 15:00:26 +01001786 push @xh, "Content-Type: text/plain; charset=$auto_8bit_encoding";
Thomas Rast3cae7e52010-06-17 22:10:39 +02001787 $body_encoding = $auto_8bit_encoding;
1788 }
1789
Krzysztof Mazurce547802012-10-24 23:08:26 +02001790 if ($broken_encoding{$t} && !is_rfc2047_quoted($subject)) {
1791 $subject = quote_subject($subject, $auto_8bit_encoding);
Thomas Rast3cae7e52010-06-17 22:10:39 +02001792 }
1793
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001794 if (defined $sauthor and $sauthor ne $sender) {
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001795 $message = "From: $author\n\n$message";
Jeff King8291db62007-11-16 05:49:09 -05001796 if (defined $author_encoding) {
1797 if ($has_content_type) {
1798 if ($body_encoding eq $author_encoding) {
1799 # ok, we already have the right encoding
1800 }
1801 else {
1802 # uh oh, we should re-encode
1803 }
1804 }
1805 else {
Paolo Bonzinibb294562014-11-25 15:00:26 +01001806 $xfer_encoding = '8bit' if not defined $xfer_encoding;
Thomas Rast3cae7e52010-06-17 22:10:39 +02001807 $has_content_type = 1;
Jeff King8291db62007-11-16 05:49:09 -05001808 push @xh,
Paolo Bonzinibb294562014-11-25 15:00:26 +01001809 "Content-Type: text/plain; charset=$author_encoding";
Jeff King8291db62007-11-16 05:49:09 -05001810 }
1811 }
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001812 }
brian m. carlsone67a2282018-07-08 22:17:12 +00001813 $xfer_encoding = '8bit' if not defined $xfer_encoding;
1814 ($message, $xfer_encoding) = apply_transfer_encoding(
1815 $message, $xfer_encoding, $target_xfer_encoding);
1816 push @xh, "Content-Transfer-Encoding: $xfer_encoding";
1817 unshift @xh, 'MIME-Version: 1.0' unless $has_mime_version;
Ryan Anderson83b24432005-07-31 04:17:25 -04001818
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001819 $needs_confirm = (
1820 $confirm eq "always" or
1821 ($confirm =~ /^(?:auto|cc)$/ && @cc) or
1822 ($confirm =~ /^(?:auto|compose)$/ && $compose && $message_num == 1));
1823 $needs_confirm = "inform" if ($needs_confirm && $confirm_unconfigured && @cc);
1824
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001825 @to = process_address_list(@to);
1826 @cc = process_address_list(@cc);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001827
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001828 @to = (@initial_to, @to);
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001829 @cc = (@initial_cc, @cc);
1830
Michael S. Tsirkinf515c902014-04-29 08:41:16 +03001831 if ($message_num == 1) {
1832 if (defined $cover_cc and $cover_cc) {
1833 @initial_cc = @cc;
1834 }
1835 if (defined $cover_to and $cover_to) {
1836 @initial_to = @to;
1837 }
1838 }
1839
Michael Witten15da1082009-04-13 13:23:51 -05001840 my $message_was_sent = send_message();
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001841 if ($message_was_sent == -1) {
1842 do_edit($t);
1843 return 0;
1844 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001845
1846 # set up for the next message
Junio C Hamano95a877a2009-06-12 09:23:43 -07001847 if ($thread && $message_was_sent &&
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001848 ($chain_reply_to || !defined $in_reply_to || length($in_reply_to) == 0 ||
Antonio Ospitedb54c8e2010-11-12 15:55:08 +01001849 $message_num == 1)) {
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001850 $in_reply_to = $message_id;
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001851 if (length $references > 0) {
YOSHIFUJI Hideaki / 吉藤英明a925b892007-04-06 08:50:24 +09001852 $references .= "\n $message_id";
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001853 } else {
1854 $references = "$message_id";
1855 }
Ryan Anderson78488b22005-07-31 20:04:24 -04001856 }
Jeff King4f3d3702007-12-17 15:51:34 -05001857 $message_id = undef;
xiaoqiang zhao5453b832017-05-21 20:59:50 +08001858 $num_sent++;
1859 if (defined $batch_size && $num_sent == $batch_size) {
1860 $num_sent = 0;
1861 $smtp->quit if defined $smtp;
1862 undef $smtp;
1863 undef $auth;
1864 sleep($relogin_delay) if defined $relogin_delay;
1865 }
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001866
1867 return 1;
1868}
1869
1870foreach my $t (@files) {
1871 while (!process_file($t)) {
1872 # user edited the file
1873 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001874}
Ryan Andersone2057352005-08-02 21:45:22 -04001875
Joe Perches6e74e072010-09-24 10:03:00 -07001876# Execute a command (e.g. $to_cmd) to get a list of email addresses
1877# and return a results array
1878sub recipients_cmd {
1879 my ($prefix, $what, $cmd, $file) = @_;
1880
Joe Perches6e74e072010-09-24 10:03:00 -07001881 my @addresses = ();
Ramkumar Ramachandraa47eab02013-03-31 18:40:42 -07001882 open my $fh, "-|", "$cmd \Q$file\E"
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001883 or die sprintf(__("(%s) Could not execute '%s'"), $prefix, $cmd);
Junio C Hamano7ebee442010-10-26 22:02:52 -07001884 while (my $address = <$fh>) {
Joe Perches6e74e072010-09-24 10:03:00 -07001885 $address =~ s/^\s*//g;
1886 $address =~ s/\s*$//g;
1887 $address = sanitize_address($address);
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001888 next if ($address eq $sender and $suppress_cc{'self'});
Joe Perches6e74e072010-09-24 10:03:00 -07001889 push @addresses, $address;
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001890 printf(__("(%s) Adding %s: %s from: '%s'\n"),
1891 $prefix, $what, $address, $cmd) unless $quiet;
Joe Perches6e74e072010-09-24 10:03:00 -07001892 }
Junio C Hamano7ebee442010-10-26 22:02:52 -07001893 close $fh
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001894 or die sprintf(__("(%s) failed to close pipe to '%s'"), $prefix, $cmd);
Joe Perches6e74e072010-09-24 10:03:00 -07001895 return @addresses;
1896}
1897
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001898cleanup_compose_files();
Ryan Anderson1f038a02005-09-05 01:13:07 -04001899
Ævar Arnfjörð Bjarmason4bf597e2010-09-30 13:43:00 +00001900sub cleanup_compose_files {
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001901 unlink($compose_filename, $compose_filename . ".final") if $compose;
Ryan Anderson1f038a02005-09-05 01:13:07 -04001902}
1903
Eric Wong4bc87a22006-03-25 17:20:48 -08001904$smtp->quit if $smtp;
Ryan Andersone2057352005-08-02 21:45:22 -04001905
Paolo Bonzini8d814082014-11-25 15:00:27 +01001906sub apply_transfer_encoding {
1907 my $message = shift;
1908 my $from = shift;
1909 my $to = shift;
1910
Aaron Lindsay3c88e462018-11-02 05:52:38 -04001911 return ($message, $to) if ($from eq $to and $from ne '7bit');
Paolo Bonzini8d814082014-11-25 15:00:27 +01001912
1913 require MIME::QuotedPrint;
1914 require MIME::Base64;
1915
1916 $message = MIME::QuotedPrint::decode($message)
1917 if ($from eq 'quoted-printable');
1918 $message = MIME::Base64::decode($message)
1919 if ($from eq 'base64');
1920
brian m. carlson74d76a12019-04-13 22:45:51 +00001921 $to = ($message =~ /(?:.{999,}|\r)/) ? 'quoted-printable' : '8bit'
brian m. carlson7a369872018-07-08 22:17:10 +00001922 if $to eq 'auto';
1923
Vasco Almeida46493102016-12-14 11:54:36 -01001924 die __("cannot send message as 7bit")
Paolo Bonzini8d814082014-11-25 15:00:27 +01001925 if ($to eq '7bit' and $message =~ /[^[:ascii:]]/);
brian m. carlson7a369872018-07-08 22:17:10 +00001926 return ($message, $to)
Paolo Bonzini8d814082014-11-25 15:00:27 +01001927 if ($to eq '7bit' or $to eq '8bit');
brian m. carlson7a369872018-07-08 22:17:10 +00001928 return (MIME::QuotedPrint::encode($message, "\n", 0), $to)
Paolo Bonzini8d814082014-11-25 15:00:27 +01001929 if ($to eq 'quoted-printable');
brian m. carlson7a369872018-07-08 22:17:10 +00001930 return (MIME::Base64::encode($message, "\n"), $to)
Paolo Bonzini8d814082014-11-25 15:00:27 +01001931 if ($to eq 'base64');
Vasco Almeida46493102016-12-14 11:54:36 -01001932 die __("invalid transfer encoding");
Paolo Bonzini8d814082014-11-25 15:00:27 +01001933}
1934
Ævar Arnfjörð Bjarmasonc438ea22010-09-30 13:42:59 +00001935sub unique_email_list {
Ryan Andersone2057352005-08-02 21:45:22 -04001936 my %seen;
1937 my @emails;
1938
1939 foreach my $entry (@_) {
Krzysztof Mazure4312252012-11-22 19:12:10 +01001940 my $clean = extract_valid_address_or_die($entry);
1941 $seen{$clean} ||= 0;
1942 next if $seen{$clean}++;
1943 push @emails, $entry;
Ryan Andersone2057352005-08-02 21:45:22 -04001944 }
1945 return @emails;
1946}
Jeff King747bbff2008-01-18 09:19:48 -05001947
1948sub validate_patch {
brian m. carlsonf2d06fb2018-07-08 22:17:11 +00001949 my ($fn, $xfer_encoding) = @_;
Jonathan Tan64896602017-05-12 15:38:26 -07001950
Jonathan Tan177409e2017-06-01 16:50:55 -07001951 if ($repo) {
Ævar Arnfjörð Bjarmason7cbc0452021-05-26 13:21:07 +02001952 my $hooks_path = $repo->command_oneline('rev-parse', '--git-path', 'hooks');
1953 my $validate_hook = catfile($hooks_path,
Jonathan Tan177409e2017-06-01 16:50:55 -07001954 'sendemail-validate');
1955 my $hook_error;
1956 if (-x $validate_hook) {
1957 my $target = abs_path($fn);
1958 # The hook needs a correct cwd and GIT_DIR.
1959 my $cwd_save = cwd();
1960 chdir($repo->wc_path() or $repo->repo_path())
1961 or die("chdir: $!");
1962 local $ENV{"GIT_DIR"} = $repo->repo_path();
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +02001963 $hook_error = system_or_msg([$validate_hook, $target]);
Jonathan Tan177409e2017-06-01 16:50:55 -07001964 chdir($cwd_save) or die("chdir: $!");
1965 }
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +02001966 if ($hook_error) {
1967 die sprintf(__("fatal: %s: rejected by sendemail-validate hook\n" .
Ævar Arnfjörð Bjarmasonea7811b2021-04-06 16:00:37 +02001968 "%s\n" .
1969 "warning: no patches were sent\n"), $fn, $hook_error);
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +02001970 }
Jonathan Tan64896602017-05-12 15:38:26 -07001971 }
Jonathan Tan64896602017-05-12 15:38:26 -07001972
brian m. carlsonf2d06fb2018-07-08 22:17:11 +00001973 # Any long lines will be automatically fixed if we use a suitable transfer
1974 # encoding.
1975 unless ($xfer_encoding =~ /^(?:auto|quoted-printable|base64)$/) {
1976 open(my $fh, '<', $fn)
1977 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
1978 while (my $line = <$fh>) {
1979 if (length($line) > 998) {
Ævar Arnfjörð Bjarmasonea7811b2021-04-06 16:00:37 +02001980 die sprintf(__("fatal: %s:%d is longer than 998 characters\n" .
1981 "warning: no patches were sent\n"), $fn, $.);
brian m. carlsonf2d06fb2018-07-08 22:17:11 +00001982 }
Jeff King747bbff2008-01-18 09:19:48 -05001983 }
1984 }
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -07001985 return;
Jeff King747bbff2008-01-18 09:19:48 -05001986}
Jeff King0706bd12008-03-28 17:28:33 -04001987
Junio C Hamano531220b2016-03-17 22:40:05 -07001988sub handle_backup {
1989 my ($last, $lastlen, $file, $known_suffix) = @_;
1990 my ($suffix, $skip);
1991
1992 $skip = 0;
1993 if (defined $last &&
1994 ($lastlen < length($file)) &&
1995 (substr($file, 0, $lastlen) eq $last) &&
1996 ($suffix = substr($file, $lastlen)) !~ /^[a-z0-9]/i) {
1997 if (defined $known_suffix && $suffix eq $known_suffix) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001998 printf(__("Skipping %s with backup suffix '%s'.\n"), $file, $known_suffix);
Junio C Hamano531220b2016-03-17 22:40:05 -07001999 $skip = 1;
2000 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01002001 # TRANSLATORS: please keep "[y|N]" as is.
2002 my $answer = ask(sprintf(__("Do you really want to send %s? [y|N]: "), $file),
Junio C Hamano531220b2016-03-17 22:40:05 -07002003 valid_re => qr/^(?:y|n)/i,
2004 default => 'n');
2005 $skip = ($answer ne 'y');
2006 if ($skip) {
2007 $known_suffix = $suffix;
2008 }
2009 }
2010 }
2011 return ($skip, $known_suffix);
2012}
2013
2014sub handle_backup_files {
2015 my @file = @_;
2016 my ($last, $lastlen, $known_suffix, $skip, @result);
2017 for my $file (@file) {
2018 ($skip, $known_suffix) = handle_backup($last, $lastlen,
2019 $file, $known_suffix);
2020 push @result, $file unless $skip;
2021 $last = $file;
2022 $lastlen = length($file);
2023 }
2024 return @result;
2025}
2026
Jeff King0706bd12008-03-28 17:28:33 -04002027sub file_has_nonascii {
2028 my $fn = shift;
2029 open(my $fh, '<', $fn)
Vasco Almeida3c5cd202016-12-14 11:54:37 -01002030 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
Jeff King0706bd12008-03-28 17:28:33 -04002031 while (my $line = <$fh>) {
2032 return 1 if $line =~ /[^[:ascii:]]/;
2033 }
2034 return 0;
2035}
Thomas Rast3cae7e52010-06-17 22:10:39 +02002036
2037sub body_or_subject_has_nonascii {
2038 my $fn = shift;
2039 open(my $fh, '<', $fn)
Vasco Almeida3c5cd202016-12-14 11:54:37 -01002040 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
Thomas Rast3cae7e52010-06-17 22:10:39 +02002041 while (my $line = <$fh>) {
2042 last if $line =~ /^$/;
2043 return 1 if $line =~ /^Subject.*[^[:ascii:]]/;
2044 }
2045 while (my $line = <$fh>) {
2046 return 1 if $line =~ /[^[:ascii:]]/;
2047 }
2048 return 0;
2049}