blob: f0be4b4560f7a47a1d3f7e42ee0d879fb95028a7 [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
Todd Zullingerd13a73e2023-11-16 14:30:10 -050019use 5.008001;
Ryan Anderson83b24432005-07-31 04:17:25 -040020use strict;
Ævar Arnfjörð Bjarmasonecc4ee92021-05-28 11:23:40 +020021use warnings $ENV{GIT_PERL_FATAL_WARNINGS} ? qw(FATAL all) : ();
Ryan Anderson83b24432005-07-31 04:17:25 -040022use Getopt::Long;
Ævar Arnfjörð Bjarmason28654672018-03-03 15:38:13 +000023use Git::LoadCPAN::Error qw(:try);
Petr Baudis3cb8caf2006-07-03 22:47:58 +020024use Git;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -010025use Git::I18N;
Ryan Anderson83b24432005-07-31 04:17:25 -040026
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +010027Getopt::Long::Configure qw/ pass_through /;
28
Michael Coleman1b0baf12007-02-27 22:47:54 -060029sub usage {
30 print <<EOT;
Todd Zullinger46edab52023-11-15 12:39:44 -050031git send-email [<options>] <file|directory>
32git send-email [<options>] <format-patch options>
Jacob Keller17b7a832015-11-19 14:52:11 -080033git send-email --dump-aliases
Michael Witten4ed62b02008-09-30 07:58:30 -050034
35 Composing:
36 --from <str> * Email From:
Stephen Boydf434c082010-03-07 14:46:48 -080037 --[no-]to <str> * Email To:
38 --[no-]cc <str> * Email Cc:
39 --[no-]bcc <str> * Email Bcc:
Michael Witten4ed62b02008-09-30 07:58:30 -050040 --subject <str> * Email "Subject:"
Christian Ludwigd11c9432018-03-04 00:58:14 +010041 --reply-to <str> * Email "Reply-To:"
Michael Witten4ed62b02008-09-30 07:58:30 -050042 --in-reply-to <str> * Email "In-Reply-To:"
Luis Henriquesac1596a2014-03-24 21:38:27 +000043 --[no-]xmailer * Add "X-Mailer:" header (default).
Felipe Contreras402596a2013-04-07 01:10:27 -060044 --[no-]annotate * Review each patch that will be sent in an editor.
Michael Witten4ed62b02008-09-30 07:58:30 -050045 --compose * Open an editor for introduction.
Krzysztof Mazur62e00692012-10-10 01:02:56 +020046 --compose-encoding <str> * Encoding to assume for introduction.
Thomas Rast3cae7e52010-06-17 22:10:39 +020047 --8bit-encoding <str> * Encoding to assume 8bit mails if undeclared
Paolo Bonzini8d814082014-11-25 15:00:27 +010048 --transfer-encoding <str> * Transfer encoding to use (quoted-printable, 8bit, base64)
Michael Witten4ed62b02008-09-30 07:58:30 -050049
50 Sending:
51 --envelope-sender <str> * Email envelope sender.
Gregory Anderscd5b33f2021-05-14 09:15:53 -060052 --sendmail-cmd <str> * Command to run to send email.
Michael Witten4ed62b02008-09-30 07:58:30 -050053 --smtp-server <str:int> * Outgoing SMTP server to use. The port
54 is optional. Default 'localhost'.
Pascal Obry052fbea2010-09-06 20:12:11 +020055 --smtp-server-option <str> * Outgoing SMTP server option to use.
Michael Witten4ed62b02008-09-30 07:58:30 -050056 --smtp-server-port <int> * Outgoing SMTP server port.
57 --smtp-user <str> * Username for SMTP-AUTH.
58 --smtp-pass <str> * Password for SMTP-AUTH; not necessary.
59 --smtp-encryption <str> * tls or ssl; anything else disables.
60 --smtp-ssl * Deprecated. Use '--smtp-encryption ssl'.
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -070061 --smtp-ssl-cert-path <str> * Path to ca-certificates (either directory or file).
62 Pass an empty string to disable certificate
63 verification.
Jari Aalto134550f2010-03-14 17:16:45 +020064 --smtp-domain <str> * The domain name sent to HELO/EHLO handshake
Joshua Watt8dd9b3f2018-10-22 22:24:25 -050065 --smtp-auth <str> * Space-separated list of allowed AUTH mechanisms, or
66 "none" to disable authentication.
Jan Viktorin0f2e68b2015-08-12 01:39:44 +020067 This setting forces to use one of the listed mechanisms.
Joshua Watt8dd9b3f2018-10-22 22:24:25 -050068 --no-smtp-auth Disable SMTP authentication. Shorthand for
69 `--smtp-auth=none`
Jari Aaltof60812e2010-03-14 17:16:09 +020070 --smtp-debug <0|1> * Disable, enable Net::SMTP debug.
Michael Witten4ed62b02008-09-30 07:58:30 -050071
xiaoqiang zhao5453b832017-05-21 20:59:50 +080072 --batch-size <int> * send max <int> message per connection.
73 --relogin-delay <int> * delay <int> seconds between two successive login.
74 This option can only be used with --batch-size
75
Michael Witten4ed62b02008-09-30 07:58:30 -050076 Automating:
77 --identity <str> * Use the sendemail.<id> options.
Maxim Cournoyerba921062023-05-01 10:38:47 -040078 --to-cmd <str> * Email To: via `<str> \$patch_path`.
79 --cc-cmd <str> * Email Cc: via `<str> \$patch_path`.
80 --header-cmd <str> * Add headers via `<str> \$patch_path`.
81 --no-header-cmd * Disable any header command in use.
Rasmus Villemoesef0cc1d2018-10-16 09:39:23 +020082 --suppress-cc <str> * author, self, sob, cc, cccmd, body, bodycc, misc-by, all.
Michael S. Tsirkinf515c902014-04-29 08:41:16 +030083 --[no-]cc-cover * Email Cc: addresses in the cover letter.
84 --[no-]to-cover * Email To: addresses in the cover letter.
Jay Soffian3531e272009-02-14 23:32:15 -050085 --[no-]signed-off-by-cc * Send to Signed-off-by: addresses. Default on.
Michael Witten4ed62b02008-09-30 07:58:30 -050086 --[no-]suppress-from * Send to self. Default off.
Junio C Hamano41fe87f2009-08-22 12:48:48 -070087 --[no-]chain-reply-to * Chain In-Reply-To: fields. Default off.
Michael Witten4ed62b02008-09-30 07:58:30 -050088 --[no-]thread * Use In-Reply-To: field. Default on.
89
90 Administering:
Jay Soffianc1f2aa42009-03-02 23:52:18 -050091 --confirm <str> * Confirm recipients before sending;
92 auto, cc, compose, always, or never.
Michael Witten4ed62b02008-09-30 07:58:30 -050093 --quiet * Output one line of info per email.
94 --dry-run * Don't actually send the emails.
95 --[no-]validate * Perform patch sanity checks. Default on.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +010096 --[no-]format-patch * understand any non optional arguments as
97 `git format-patch` ones.
Thomas Rasta03bc5b2009-06-08 23:34:12 +020098 --force * Send even if safety checks would prevent it.
Jeff Kingc764a0c2008-01-18 09:20:10 -050099
Jacob Keller17b7a832015-11-19 14:52:11 -0800100 Information:
101 --dump-aliases * Dump configured aliases and exit.
102
Michael Coleman1b0baf12007-02-27 22:47:54 -0600103EOT
104 exit(1);
105}
106
Thiago Perrotta2b7b7582021-10-25 17:27:06 -0400107sub uniq {
108 my %seen;
109 grep !$seen{$_}++, @_;
110}
111
Duy Nguyen13374982018-11-03 07:03:18 +0100112sub completion_helper {
Thiago Perrotta2b7b7582021-10-25 17:27:06 -0400113 my ($original_opts) = @_;
114 my %not_for_completion = (
115 "git-completion-helper" => undef,
116 "h" => undef,
117 );
118 my @send_email_opts = ();
119
120 foreach my $key (keys %$original_opts) {
121 unless (exists $not_for_completion{$key}) {
Todd Zullinger6ff658c2023-11-16 14:30:11 -0500122 my $negatable = ($key =~ s/!$//);
Thiago Perrotta2b7b7582021-10-25 17:27:06 -0400123
124 if ($key =~ /[:=][si]$/) {
125 $key =~ s/[:=][si]$//;
126 push (@send_email_opts, "--$_=") foreach (split (/\|/, $key));
127 } else {
128 push (@send_email_opts, "--$_") foreach (split (/\|/, $key));
Todd Zullinger6ff658c2023-11-16 14:30:11 -0500129 if ($negatable) {
130 push (@send_email_opts, "--no-$_") foreach (split (/\|/, $key));
131 }
Thiago Perrotta2b7b7582021-10-25 17:27:06 -0400132 }
133 }
134 }
135
136 my @format_patch_opts = split(/ /, Git::command('format-patch', '--git-completion-helper'));
137 my @opts = (@send_email_opts, @format_patch_opts);
138 @opts = uniq (grep !/^$/, @opts);
139 # There's an implicit '\n' here already, no need to add an explicit one.
140 print "@opts";
141 exit(0);
Duy Nguyen13374982018-11-03 07:03:18 +0100142}
143
Eric Wong4bc87a22006-03-25 17:20:48 -0800144# most mail servers generate the Date: header, but not all...
Jakub Narebski6bdca892006-07-07 20:57:55 +0200145sub format_2822_time {
146 my ($time) = @_;
147 my @localtm = localtime($time);
148 my @gmttm = gmtime($time);
149 my $localmin = $localtm[1] + $localtm[2] * 60;
150 my $gmtmin = $gmttm[1] + $gmttm[2] * 60;
151 if ($localtm[0] != $gmttm[0]) {
Vasco Almeida46493102016-12-14 11:54:36 -0100152 die __("local zone differs from GMT by a non-minute interval\n");
Jakub Narebski6bdca892006-07-07 20:57:55 +0200153 }
154 if ((($gmttm[6] + 1) % 7) == $localtm[6]) {
155 $localmin += 1440;
156 } elsif ((($gmttm[6] - 1) % 7) == $localtm[6]) {
157 $localmin -= 1440;
158 } elsif ($gmttm[6] != $localtm[6]) {
Vasco Almeida46493102016-12-14 11:54:36 -0100159 die __("local time offset greater than or equal to 24 hours\n");
Jakub Narebski6bdca892006-07-07 20:57:55 +0200160 }
161 my $offset = $localmin - $gmtmin;
162 my $offhour = $offset / 60;
163 my $offmin = abs($offset % 60);
164 if (abs($offhour) >= 24) {
Vasco Almeida46493102016-12-14 11:54:36 -0100165 die __("local time offset greater than or equal to 24 hours\n");
Jakub Narebski6bdca892006-07-07 20:57:55 +0200166 }
167
168 return sprintf("%s, %2d %s %d %02d:%02d:%02d %s%02d%02d",
169 qw(Sun Mon Tue Wed Thu Fri Sat)[$localtm[6]],
170 $localtm[3],
171 qw(Jan Feb Mar Apr May Jun
172 Jul Aug Sep Oct Nov Dec)[$localtm[4]],
173 $localtm[5]+1900,
174 $localtm[2],
175 $localtm[1],
176 $localtm[0],
177 ($offset >= 0) ? '+' : '-',
178 abs($offhour),
179 $offmin,
180 );
181}
Eric Wong4bc87a22006-03-25 17:20:48 -0800182
183my $smtp;
Wincent Colaiuta5f5b6112007-11-21 13:35:05 +0100184my $auth;
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800185my $num_sent = 0;
Eric Wong4bc87a22006-03-25 17:20:48 -0800186
Роман Донченко11f70a72014-12-14 18:59:46 +0300187# Regexes for RFC 2047 productions.
188my $re_token = qr/[^][()<>@,;:\\"\/?.= \000-\037\177-\377]+/;
189my $re_encoded_text = qr/[^? \000-\037\177-\377]+/;
190my $re_encoded_word = qr/=\?($re_token)\?($re_token)\?($re_encoded_text)\?=/;
191
Ryan Anderson83b24432005-07-31 04:17:25 -0400192# Variables we fill in automatically, or via prompting:
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200193my (@to,@cc,@xh,$envelope_sender,
Christian Ludwigd11c9432018-03-04 00:58:14 +0100194 $initial_in_reply_to,$reply_to,$initial_subject,@files,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200195 $author,$sender,$smtp_authpass,$annotate,$compose,$time);
196# Things we either get from config, *or* are overridden on the
197# command-line.
Maxim Cournoyerba921062023-05-01 10:38:47 -0400198my ($no_cc, $no_to, $no_bcc, $no_identity, $no_header_cmd);
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200199my (@config_to, @getopt_to);
200my (@config_cc, @getopt_cc);
201my (@config_bcc, @getopt_bcc);
Ryan Anderson78488b22005-07-31 20:04:24 -0400202
Ryan Anderson91332612005-07-31 20:04:24 -0400203# Example reply to:
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100204#$initial_in_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
Ryan Anderson83b24432005-07-31 04:17:25 -0400205
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100206my $repo = eval { Git->repository() };
207my @repo = $repo ? ($repo) : ();
Ryan Anderson83b24432005-07-31 04:17:25 -0400208
Adam Roben5483c712007-06-27 20:59:37 -0700209# Behavior modification variables
210my ($quiet, $dry_run) = (0, 0);
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100211my $format_patch;
Jay Soffianafe756c2009-02-23 13:51:37 -0500212my $compose_filename;
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200213my $force = 0;
Jacob Keller17b7a832015-11-19 14:52:11 -0800214my $dump_aliases = 0;
Adam Roben5483c712007-06-27 20:59:37 -0700215
Kyle Meyer8774aa52022-11-26 15:21:23 -0500216# Variables to prevent short format-patch options from being captured
217# as abbreviated send-email options
218my $reroll_count;
219
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100220# Handle interactive edition of files.
221my $multiedit;
Michael J Gruber0ce142c2010-03-22 17:12:53 +0100222my $editor;
Jonathan Niederb4479f02009-10-30 20:42:34 -0500223
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +0200224sub system_or_msg {
Emily Shaffera7555302021-12-22 04:59:38 +0100225 my ($args, $msg, $cmd_name) = @_;
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +0200226 system(@$args);
227 my $signalled = $? & 127;
228 my $exit_code = $? >> 8;
229 return unless $signalled or $exit_code;
230
Emily Shaffera7555302021-12-22 04:59:38 +0100231 my @sprintf_args = ($cmd_name ? $cmd_name : $args->[0], $exit_code);
Ævar Arnfjörð Bjarmason5b719b72021-05-25 01:14:24 +0200232 if (defined $msg) {
233 # Quiet the 'redundant' warning category, except we
Todd Zullingerd13a73e2023-11-16 14:30:10 -0500234 # need to support down to Perl 5.8.1, so we can't do a
Ævar Arnfjörð Bjarmason5b719b72021-05-25 01:14:24 +0200235 # "no warnings 'redundant'", since that category was
236 # introduced in perl 5.22, and asking for it will die
237 # on older perls.
238 no warnings;
239 return sprintf($msg, @sprintf_args);
240 }
Ævar Arnfjörð Bjarmasonea7811b2021-04-06 16:00:37 +0200241 return sprintf(__("fatal: command '%s' died with exit code %d"),
Ævar Arnfjörð Bjarmason5b719b72021-05-25 01:14:24 +0200242 @sprintf_args);
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +0200243}
244
245sub system_or_die {
246 my $msg = system_or_msg(@_);
247 die $msg if $msg;
248}
249
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100250sub do_edit {
Michael J Gruber0ce142c2010-03-22 17:12:53 +0100251 if (!defined($editor)) {
252 $editor = Git::command_oneline('var', 'GIT_EDITOR');
253 }
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +0200254 my $die_msg = __("the editor exited uncleanly, aborting everything");
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100255 if (defined($multiedit) && !$multiedit) {
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +0200256 system_or_die(['sh', '-c', $editor.' "$@"', $editor, $_], $die_msg) for @_;
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100257 } else {
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +0200258 system_or_die(['sh', '-c', $editor.' "$@"', $editor, @_], $die_msg);
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100259 }
260}
Adam Roben5483c712007-06-27 20:59:37 -0700261
262# Variables with corresponding config settings
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200263my ($suppress_from, $signed_off_by_cc);
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300264my ($cover_cc, $cover_to);
Maxim Cournoyerba921062023-05-01 10:38:47 -0400265my ($to_cmd, $cc_cmd, $header_cmd);
Pascal Obry052fbea2010-09-06 20:12:11 +0200266my ($smtp_server, $smtp_server_port, @smtp_server_options);
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -0700267my ($smtp_authuser, $smtp_encryption, $smtp_ssl_cert_path);
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800268my ($batch_size, $relogin_delay);
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200269my ($identity, $aliasfiletype, @alias_files, $smtp_domain, $smtp_auth);
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200270my ($confirm);
David Brown65648282007-12-25 19:56:29 -0800271my (@suppress_cc);
Thomas Rast3cae7e52010-06-17 22:10:39 +0200272my ($auto_8bit_encoding);
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200273my ($compose_encoding);
Gregory Anderscd5b33f2021-05-14 09:15:53 -0600274my ($sendmail_cmd);
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200275# Variables with corresponding config settings & hardcoded defaults
276my ($debug_net_smtp) = 0; # Net::SMTP, see send_message()
277my $thread = 1;
278my $chain_reply_to = 0;
279my $use_xmailer = 1;
280my $validate = 1;
brian m. carlsone67a2282018-07-08 22:17:12 +0000281my $target_xfer_encoding = 'auto';
Drew DeVaultdd84e522020-07-23 20:44:32 -0400282my $forbid_sendmail_variables = 1;
Adam Roben5483c712007-06-27 20:59:37 -0700283
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900284my %config_bool_settings = (
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200285 "thread" => \$thread,
286 "chainreplyto" => \$chain_reply_to,
287 "suppressfrom" => \$suppress_from,
288 "signedoffbycc" => \$signed_off_by_cc,
289 "cccover" => \$cover_cc,
290 "tocover" => \$cover_to,
291 "signedoffcc" => \$signed_off_by_cc,
292 "validate" => \$validate,
293 "multiedit" => \$multiedit,
294 "annotate" => \$annotate,
295 "xmailer" => \$use_xmailer,
Drew DeVaultdd84e522020-07-23 20:44:32 -0400296 "forbidsendmailvariables" => \$forbid_sendmail_variables,
Adam Robene46f7a02007-06-26 15:48:30 -0700297);
298
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900299my %config_settings = (
Ævar Arnfjörð Bjarmason119974e2021-05-28 11:23:43 +0200300 "smtpencryption" => \$smtp_encryption,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900301 "smtpserver" => \$smtp_server,
Junio C Hamano44b24762007-09-25 17:27:54 -0700302 "smtpserverport" => \$smtp_server_port,
Pascal Obry052fbea2010-09-06 20:12:11 +0200303 "smtpserveroption" => \@smtp_server_options,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900304 "smtpuser" => \$smtp_authuser,
305 "smtppass" => \$smtp_authpass,
Pascal Obrye1e91152010-09-06 20:12:09 +0200306 "smtpdomain" => \$smtp_domain,
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200307 "smtpauth" => \$smtp_auth,
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800308 "smtpbatchsize" => \$batch_size,
309 "smtprelogindelay" => \$relogin_delay,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200310 "to" => \@config_to,
Joe Perches6e74e072010-09-24 10:03:00 -0700311 "tocmd" => \$to_cmd,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200312 "cc" => \@config_cc,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900313 "cccmd" => \$cc_cmd,
Maxim Cournoyerba921062023-05-01 10:38:47 -0400314 "headercmd" => \$header_cmd,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900315 "aliasfiletype" => \$aliasfiletype,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200316 "bcc" => \@config_bcc,
David Brown65648282007-12-25 19:56:29 -0800317 "suppresscc" => \@suppress_cc,
Ask Bjørn Hansen9f7820a2008-06-07 00:33:42 -0700318 "envelopesender" => \$envelope_sender,
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500319 "confirm" => \$confirm,
Trent Piepho09caa242009-05-12 15:48:56 -0700320 "from" => \$sender,
Thomas Rast3cae7e52010-06-17 22:10:39 +0200321 "assume8bitencoding" => \$auto_8bit_encoding,
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200322 "composeencoding" => \$compose_encoding,
Paolo Bonzini8d814082014-11-25 15:00:27 +0100323 "transferencoding" => \$target_xfer_encoding,
Gregory Anderscd5b33f2021-05-14 09:15:53 -0600324 "sendmailcmd" => \$sendmail_cmd,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900325);
Avi Kivity4a62d3f2007-03-11 19:19:44 +0200326
Cord Seelecec5dae2011-09-30 12:52:25 +0200327my %config_path_settings = (
328 "aliasesfile" => \@alias_files,
John Keeping6e07a3b2015-11-17 22:01:05 +0000329 "smtpsslcertpath" => \$smtp_ssl_cert_path,
Cord Seelecec5dae2011-09-30 12:52:25 +0200330);
331
Michael Witten87429972008-02-03 19:53:57 -0500332# Handle Uncouth Termination
333sub signal_handler {
Michael Witten87429972008-02-03 19:53:57 -0500334 # Make text normal
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200335 require Term::ANSIColor;
336 print Term::ANSIColor::color("reset"), "\n";
Michael Witten87429972008-02-03 19:53:57 -0500337
338 # SMTP password masked
339 system "stty echo";
340
341 # tmp files from --compose
Jay Soffianafe756c2009-02-23 13:51:37 -0500342 if (defined $compose_filename) {
343 if (-e $compose_filename) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100344 printf __("'%s' contains an intermediate version ".
345 "of the email you were composing.\n"),
346 $compose_filename;
Jay Soffianafe756c2009-02-23 13:51:37 -0500347 }
348 if (-e ($compose_filename . ".final")) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100349 printf __("'%s.final' contains the composed email.\n"),
350 $compose_filename;
Jay Soffianafe756c2009-02-23 13:51:37 -0500351 }
Michael Witten87429972008-02-03 19:53:57 -0500352 }
353
354 exit;
355};
356
357$SIG{TERM} = \&signal_handler;
358$SIG{INT} = \&signal_handler;
359
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200360# Read our sendemail.* config
361sub read_config {
Ævar Arnfjörð Bjarmason9264d292021-05-28 11:23:45 +0200362 my ($known_keys, $configured, $prefix) = @_;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200363
364 foreach my $setting (keys %config_bool_settings) {
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200365 my $target = $config_bool_settings{$setting};
Ævar Arnfjörð Bjarmason9264d292021-05-28 11:23:45 +0200366 my $key = "$prefix.$setting";
367 next unless exists $known_keys->{$key};
Ævar Arnfjörð Bjarmasonc95e3a32021-05-28 11:23:51 +0200368 my $v = (@{$known_keys->{$key}} == 1 &&
369 (defined $known_keys->{$key}->[0] &&
370 $known_keys->{$key}->[0] =~ /^(?:true|false)$/s))
371 ? $known_keys->{$key}->[0] eq 'true'
372 : Git::config_bool(@repo, $key);
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200373 next unless defined $v;
374 next if $configured->{$setting}++;
375 $$target = $v;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200376 }
377
378 foreach my $setting (keys %config_path_settings) {
379 my $target = $config_path_settings{$setting};
Ævar Arnfjörð Bjarmason9264d292021-05-28 11:23:45 +0200380 my $key = "$prefix.$setting";
381 next unless exists $known_keys->{$key};
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200382 if (ref($target) eq "ARRAY") {
Ævar Arnfjörð Bjarmason9264d292021-05-28 11:23:45 +0200383 my @values = Git::config_path(@repo, $key);
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200384 next unless @values;
385 next if $configured->{$setting}++;
386 @$target = @values;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200387 }
388 else {
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200389 my $v = Git::config_path(@repo, "$prefix.$setting");
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200390 next unless defined $v;
391 next if $configured->{$setting}++;
392 $$target = $v;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200393 }
394 }
395
396 foreach my $setting (keys %config_settings) {
397 my $target = $config_settings{$setting};
Ævar Arnfjörð Bjarmason9264d292021-05-28 11:23:45 +0200398 my $key = "$prefix.$setting";
399 next unless exists $known_keys->{$key};
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200400 if (ref($target) eq "ARRAY") {
Ævar Arnfjörð Bjarmasonc95e3a32021-05-28 11:23:51 +0200401 my @values = @{$known_keys->{$key}};
402 @values = grep { defined } @values;
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200403 next if $configured->{$setting}++;
404 @$target = @values;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200405 }
406 else {
Ævar Arnfjörð Bjarmasonb996f842021-09-06 09:33:29 +0200407 my $v = $known_keys->{$key}->[-1];
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200408 next unless defined $v;
409 next if $configured->{$setting}++;
410 $$target = $v;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200411 }
412 }
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200413}
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200414
Ævar Arnfjörð Bjarmason2b110e92021-05-28 11:23:44 +0200415sub config_regexp {
416 my ($regex) = @_;
417 my @ret;
418 eval {
Ævar Arnfjörð Bjarmasonc95e3a32021-05-28 11:23:51 +0200419 my $ret = Git::command(
Ævar Arnfjörð Bjarmason2b110e92021-05-28 11:23:44 +0200420 'config',
Ævar Arnfjörð Bjarmasonc95e3a32021-05-28 11:23:51 +0200421 '--null',
Ævar Arnfjörð Bjarmason2b110e92021-05-28 11:23:44 +0200422 '--get-regexp',
423 $regex,
424 );
Ævar Arnfjörð Bjarmasonc95e3a32021-05-28 11:23:51 +0200425 @ret = map {
426 # We must always return ($k, $v) here, since
427 # empty config values will be just "key\0",
428 # not "key\nvalue\0".
429 my ($k, $v) = split /\n/, $_, 2;
430 ($k, $v);
431 } split /\0/, $ret;
Ævar Arnfjörð Bjarmason2b110e92021-05-28 11:23:44 +0200432 1;
433 } or do {
434 # If we have no keys we're OK, otherwise re-throw
435 die $@ if $@->value != 1;
436 };
437 return @ret;
438}
439
Ævar Arnfjörð Bjarmason9264d292021-05-28 11:23:45 +0200440# Save ourselves a lot of work of shelling out to 'git config' (it
441# parses 'bool' etc.) by only doing so for config keys that exist.
442my %known_config_keys;
443{
Ævar Arnfjörð Bjarmasonc95e3a32021-05-28 11:23:51 +0200444 my @kv = config_regexp("^sende?mail[.]");
445 while (my ($k, $v) = splice @kv, 0, 2) {
446 push @{$known_config_keys{$k}} => $v;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200447 }
448}
449
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200450# sendemail.identity yields to --identity. We must parse this
451# special-case first before the rest of the config is read.
Ævar Arnfjörð Bjarmason9264d292021-05-28 11:23:45 +0200452{
453 my $key = "sendemail.identity";
454 $identity = Git::config(@repo, $key) if exists $known_config_keys{$key};
455}
Thiago Perrotta2b7b7582021-10-25 17:27:06 -0400456my %identity_options = (
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200457 "identity=s" => \$identity,
458 "no-identity" => \$no_identity,
459);
Thiago Perrotta2b7b7582021-10-25 17:27:06 -0400460my $rc = GetOptions(%identity_options);
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200461usage() unless $rc;
462undef $identity if $no_identity;
463
464# Now we know enough to read the config
465{
466 my %configured;
Ævar Arnfjörð Bjarmason9264d292021-05-28 11:23:45 +0200467 read_config(\%known_config_keys, \%configured, "sendemail.$identity") if defined $identity;
468 read_config(\%known_config_keys, \%configured, "sendemail");
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200469}
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200470
Ryan Anderson83b24432005-07-31 04:17:25 -0400471# Begin by accumulating all the variables (defined above), that we will end up
472# needing, first, from the command line:
473
Clemens Buchacherc5978242011-09-03 19:06:13 +0200474my $help;
Duy Nguyen13374982018-11-03 07:03:18 +0100475my $git_completion_helper;
Thiago Perrotta2b7b7582021-10-25 17:27:06 -0400476my %dump_aliases_options = (
477 "h" => \$help,
478 "dump-aliases" => \$dump_aliases,
479);
480$rc = GetOptions(%dump_aliases_options);
Jacob Keller17b7a832015-11-19 14:52:11 -0800481usage() unless $rc;
Vasco Almeida46493102016-12-14 11:54:36 -0100482die __("--dump-aliases incompatible with other options\n")
Jacob Keller17b7a832015-11-19 14:52:11 -0800483 if !$help and $dump_aliases and @ARGV;
Thiago Perrotta2b7b7582021-10-25 17:27:06 -0400484my %options = (
Clemens Buchacherc5978242011-09-03 19:06:13 +0200485 "sender|from=s" => \$sender,
Thiago Perrotta2b7b7582021-10-25 17:27:06 -0400486 "in-reply-to=s" => \$initial_in_reply_to,
Christian Ludwigd11c9432018-03-04 00:58:14 +0100487 "reply-to=s" => \$reply_to,
Ryan Anderson83b24432005-07-31 04:17:25 -0400488 "subject=s" => \$initial_subject,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200489 "to=s" => \@getopt_to,
Joe Perches6e74e072010-09-24 10:03:00 -0700490 "to-cmd=s" => \$to_cmd,
Stephen Boydf434c082010-03-07 14:46:48 -0800491 "no-to" => \$no_to,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200492 "cc=s" => \@getopt_cc,
Stephen Boydf434c082010-03-07 14:46:48 -0800493 "no-cc" => \$no_cc,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200494 "bcc=s" => \@getopt_bcc,
Stephen Boydf434c082010-03-07 14:46:48 -0800495 "no-bcc" => \$no_bcc,
Ryan Anderson78488b22005-07-31 20:04:24 -0400496 "chain-reply-to!" => \$chain_reply_to,
Gregory Anderscd5b33f2021-05-14 09:15:53 -0600497 "sendmail-cmd=s" => \$sendmail_cmd,
Ryan Anderson3342d852005-07-31 20:04:24 -0400498 "smtp-server=s" => \$smtp_server,
Pascal Obry052fbea2010-09-06 20:12:11 +0200499 "smtp-server-option=s" => \@smtp_server_options,
Junio C Hamano44b24762007-09-25 17:27:54 -0700500 "smtp-server-port=s" => \$smtp_server_port,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900501 "smtp-user=s" => \$smtp_authuser,
Michael Witten2363d742008-02-03 19:53:56 -0500502 "smtp-pass:s" => \$smtp_authpass,
Thomas Rastf6bebd12008-06-25 21:42:43 +0200503 "smtp-ssl" => sub { $smtp_encryption = 'ssl' },
504 "smtp-encryption=s" => \$smtp_encryption,
Thomas Rast979e6522013-12-01 23:48:42 +0100505 "smtp-ssl-cert-path=s" => \$smtp_ssl_cert_path,
Jari Aaltof60812e2010-03-14 17:16:09 +0200506 "smtp-debug:i" => \$debug_net_smtp,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -0400507 "smtp-domain:s" => \$smtp_domain,
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200508 "smtp-auth=s" => \$smtp_auth,
Joshua Watt8dd9b3f2018-10-22 22:24:25 -0500509 "no-smtp-auth" => sub {$smtp_auth = 'none'},
Felipe Contreras402596a2013-04-07 01:10:27 -0600510 "annotate!" => \$annotate,
Ryan Anderson1f038a02005-09-05 01:13:07 -0400511 "compose" => \$compose,
Ryan Anderson30d08b32006-02-02 11:56:06 -0500512 "quiet" => \$quiet,
Joe Perches324a8bd2007-08-17 18:51:12 -0700513 "cc-cmd=s" => \$cc_cmd,
Maxim Cournoyerba921062023-05-01 10:38:47 -0400514 "header-cmd=s" => \$header_cmd,
515 "no-header-cmd" => \$no_header_cmd,
Adam Roben5483c712007-06-27 20:59:37 -0700516 "suppress-from!" => \$suppress_from,
David Brown65648282007-12-25 19:56:29 -0800517 "suppress-cc=s" => \@suppress_cc,
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500518 "signed-off-cc|signed-off-by-cc!" => \$signed_off_by_cc,
Todd Zullinger6ff658c2023-11-16 14:30:11 -0500519 "cc-cover!" => \$cover_cc,
520 "to-cover!" => \$cover_to,
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500521 "confirm=s" => \$confirm,
Matthew Wilcox61302592006-10-10 08:58:23 -0600522 "dry-run" => \$dry_run,
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700523 "envelope-sender=s" => \$envelope_sender,
Adam Roben5483c712007-06-27 20:59:37 -0700524 "thread!" => \$thread,
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500525 "validate!" => \$validate,
Paolo Bonzini8d814082014-11-25 15:00:27 +0100526 "transfer-encoding=s" => \$target_xfer_encoding,
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100527 "format-patch!" => \$format_patch,
Thomas Rast3cae7e52010-06-17 22:10:39 +0200528 "8bit-encoding=s" => \$auto_8bit_encoding,
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200529 "compose-encoding=s" => \$compose_encoding,
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200530 "force" => \$force,
Luis Henriquesac1596a2014-03-24 21:38:27 +0000531 "xmailer!" => \$use_xmailer,
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800532 "batch-size=i" => \$batch_size,
533 "relogin-delay=i" => \$relogin_delay,
Duy Nguyen13374982018-11-03 07:03:18 +0100534 "git-completion-helper" => \$git_completion_helper,
Kyle Meyer8774aa52022-11-26 15:21:23 -0500535 "v=s" => \$reroll_count,
Thiago Perrotta2b7b7582021-10-25 17:27:06 -0400536);
537$rc = GetOptions(%options);
Ryan Anderson83b24432005-07-31 04:17:25 -0400538
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200539# Munge any "either config or getopt, not both" variables
540my @initial_to = @getopt_to ? @getopt_to : ($no_to ? () : @config_to);
541my @initial_cc = @getopt_cc ? @getopt_cc : ($no_cc ? () : @config_cc);
542my @initial_bcc = @getopt_bcc ? @getopt_bcc : ($no_bcc ? () : @config_bcc);
543
Clemens Buchacherc5978242011-09-03 19:06:13 +0200544usage() if $help;
Thiago Perrotta2b7b7582021-10-25 17:27:06 -0400545my %all_options = (%options, %dump_aliases_options, %identity_options);
546completion_helper(\%all_options) if $git_completion_helper;
Michael Coleman1b0baf12007-02-27 22:47:54 -0600547unless ($rc) {
548 usage();
549}
550
Ævar Arnfjörð Bjarmason9264d292021-05-28 11:23:45 +0200551if ($forbid_sendmail_variables && grep { /^sendmail/s } keys %known_config_keys) {
Drew DeVaultdd84e522020-07-23 20:44:32 -0400552 die __("fatal: found configuration options for 'sendmail'\n" .
553 "git-send-email is configured with the sendemail.* options - note the 'e'.\n" .
554 "Set sendemail.forbidSendmailVariables to false to disable this check.\n");
555}
556
Vasco Almeida46493102016-12-14 11:54:36 -0100557die __("Cannot run git format-patch from outside a repository\n")
Jay Soffianeed6ca72009-02-14 23:32:13 -0500558 if $format_patch and not $repo;
559
Stefan Beller9caa7062018-02-12 11:44:04 -0800560die __("`batch-size` and `relogin` must be specified together " .
561 "(via command-line or configuration option)\n")
562 if defined $relogin_delay and not defined $batch_size;
563
Thomas Rastfa835cd2008-06-26 23:03:21 +0200564# 'default' encryption is none -- this only prevents a warning
565$smtp_encryption = '' unless (defined $smtp_encryption);
566
David Brown65648282007-12-25 19:56:29 -0800567# Set CC suppressions
568my(%suppress_cc);
569if (@suppress_cc) {
570 foreach my $entry (@suppress_cc) {
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700571 # Please update $__git_send_email_suppresscc_options
572 # in git-completion.bash when you add new options.
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100573 die sprintf(__("Unknown --suppress-cc field: '%s'\n"), $entry)
Rasmus Villemoesef0cc1d2018-10-16 09:39:23 +0200574 unless $entry =~ /^(?:all|cccmd|cc|author|self|sob|body|bodycc|misc-by)$/;
David Brown65648282007-12-25 19:56:29 -0800575 $suppress_cc{$entry} = 1;
576 }
577}
578
579if ($suppress_cc{'all'}) {
Rasmus Villemoesef0cc1d2018-10-16 09:39:23 +0200580 foreach my $entry (qw (cccmd cc author self sob body bodycc misc-by)) {
David Brown65648282007-12-25 19:56:29 -0800581 $suppress_cc{$entry} = 1;
582 }
583 delete $suppress_cc{'all'};
584}
585
586# If explicit old-style ones are specified, they trump --suppress-cc.
587$suppress_cc{'self'} = $suppress_from if defined $suppress_from;
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500588$suppress_cc{'sob'} = !$signed_off_by_cc if defined $signed_off_by_cc;
David Brown65648282007-12-25 19:56:29 -0800589
Jay Soffian3531e272009-02-14 23:32:15 -0500590if ($suppress_cc{'body'}) {
Rasmus Villemoesef0cc1d2018-10-16 09:39:23 +0200591 foreach my $entry (qw (sob bodycc misc-by)) {
Jay Soffian3531e272009-02-14 23:32:15 -0500592 $suppress_cc{$entry} = 1;
593 }
594 delete $suppress_cc{'body'};
595}
596
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500597# Set confirm's default value
598my $confirm_unconfigured = !defined $confirm;
599if ($confirm_unconfigured) {
600 $confirm = scalar %suppress_cc ? 'compose' : 'auto';
601};
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700602# Please update $__git_send_email_confirm_options in
603# git-completion.bash when you add new options.
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100604die sprintf(__("Unknown --confirm setting: '%s'\n"), $confirm)
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500605 unless $confirm =~ /^(?:auto|cc|compose|always|never)/;
606
David Brown65648282007-12-25 19:56:29 -0800607# Debugging, print out the suppressions.
608if (0) {
609 print "suppressions:\n";
610 foreach my $entry (keys %suppress_cc) {
611 printf " %-5s -> $suppress_cc{$entry}\n", $entry;
612 }
613}
614
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100615my ($repoauthor, $repocommitter);
Ævar Arnfjörð Bjarmasonfef381e2021-05-28 11:23:46 +0200616{
617 my %cache;
618 my ($author, $committer);
619 my $common = sub {
620 my ($what) = @_;
621 return $cache{$what} if exists $cache{$what};
622 ($cache{$what}) = Git::ident_person(@repo, $what);
623 return $cache{$what};
624 };
625 $repoauthor = sub { $common->('author') };
626 $repocommitter = sub { $common->('committer') };
627}
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900628
Jay Soffian50126992009-02-14 23:32:14 -0500629sub parse_address_line {
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200630 require Git::LoadCPAN::Mail::Address;
Matthieu Moybd869f62018-01-05 19:36:51 +0100631 return map { $_->format } Mail::Address->parse($_[0]);
Jay Soffian50126992009-02-14 23:32:14 -0500632}
633
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800634sub split_addrs {
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200635 require Text::ParseWords;
636 return Text::ParseWords::quotewords('\s*,\s*', 1, @_);
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800637}
638
Eric Wong994d6c62006-05-14 19:13:44 -0700639my %aliases;
Eric Sunshine09f11572015-05-31 18:29:27 -0400640
641sub parse_sendmail_alias {
642 local $_ = shift;
643 if (/"/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100644 printf STDERR __("warning: sendmail alias with quotes is not supported: %s\n"), $_;
Eric Sunshine86b89842015-06-01 14:22:36 -0400645 } elsif (/:include:/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100646 printf STDERR __("warning: `:include:` not supported: %s\n"), $_;
Eric Sunshine86b89842015-06-01 14:22:36 -0400647 } elsif (/[\/|]/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100648 printf STDERR __("warning: `/file` or `|pipe` redirection not supported: %s\n"), $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400649 } elsif (/^(\S+?)\s*:\s*(.+)$/) {
650 my ($alias, $addr) = ($1, $2);
651 $aliases{$alias} = [ split_addrs($addr) ];
652 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100653 printf STDERR __("warning: sendmail line is not recognized: %s\n"), $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400654 }
655}
656
657sub parse_sendmail_aliases {
658 my $fh = shift;
Eric Sunshine2532dd02015-05-31 18:29:29 -0400659 my $s = '';
Eric Sunshine09f11572015-05-31 18:29:27 -0400660 while (<$fh>) {
Eric Sunshine2532dd02015-05-31 18:29:29 -0400661 chomp;
Eric Sunshine020be85f2015-05-31 18:29:28 -0400662 next if /^\s*$/ || /^\s*#/;
Eric Sunshine2532dd02015-05-31 18:29:29 -0400663 $s .= $_, next if $s =~ s/\\$// || s/^\s+//;
664 parse_sendmail_alias($s) if $s;
665 $s = $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400666 }
Eric Sunshine2532dd02015-05-31 18:29:29 -0400667 $s =~ s/\\$//; # silently tolerate stray '\' on last line
668 parse_sendmail_alias($s) if $s;
Eric Sunshine09f11572015-05-31 18:29:27 -0400669}
670
Eric Wong994d6c62006-05-14 19:13:44 -0700671my %parse_alias = (
672 # multiline formats can be supported in the future
673 mutt => sub { my $fh = shift; while (<$fh>) {
Felipe Contrerasffc01f92009-09-30 17:49:36 +0300674 if (/^\s*alias\s+(?:-group\s+\S+\s+)*(\S+)\s+(.*)$/) {
Eric Wong994d6c62006-05-14 19:13:44 -0700675 my ($alias, $addr) = ($1, $2);
676 $addr =~ s/#.*$//; # mutt allows # comments
Eric Wong2c510f22016-01-04 20:53:30 +0000677 # commas delimit multiple addresses
678 my @addr = split_addrs($addr);
679
680 # quotes may be escaped in the file,
681 # unescape them so we do not double-escape them later.
682 s/\\"/"/g foreach @addr;
683 $aliases{$alias} = \@addr
Eric Wong994d6c62006-05-14 19:13:44 -0700684 }}},
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200685 mailrc => sub { my $fh = shift; while (<$fh>) {
Jeff Kinga277d1e2016-03-17 19:58:22 -0400686 if (/^alias\s+(\S+)\s+(.*?)\s*$/) {
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200687 require Text::ParseWords;
Eric Wong994d6c62006-05-14 19:13:44 -0700688 # spaces delimit multiple addresses
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200689 $aliases{$1} = [ Text::ParseWords::quotewords('\s+', 0, $2) ];
Eric Wong994d6c62006-05-14 19:13:44 -0700690 }}},
Trent Piepho73c427e2008-11-25 18:55:00 -0800691 pine => sub { my $fh = shift; my $f='\t[^\t]*';
692 for (my $x = ''; defined($x); $x = $_) {
693 chomp $x;
694 $x .= $1 while(defined($_ = <$fh>) && /^ +(.*)$/);
695 $x =~ /^(\S+)$f\t\(?([^\t]+?)\)?(:?$f){0,2}$/ or next;
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800696 $aliases{$1} = [ split_addrs($2) ];
Trent Piepho73c427e2008-11-25 18:55:00 -0800697 }},
Bill Pemberton7613ea32009-04-22 09:41:29 -0400698 elm => sub { my $fh = shift;
699 while (<$fh>) {
700 if (/^(\S+)\s+=\s+[^=]+=\s(\S+)/) {
701 my ($alias, $addr) = ($1, $2);
702 $aliases{$alias} = [ split_addrs($addr) ];
703 }
704 } },
Eric Sunshine09f11572015-05-31 18:29:27 -0400705 sendmail => \&parse_sendmail_aliases,
Eric Wong994d6c62006-05-14 19:13:44 -0700706 gnus => sub { my $fh = shift; while (<$fh>) {
707 if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
708 $aliases{$1} = [ $2 ];
709 }}}
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700710 # Please update _git_config() in git-completion.bash when you
711 # add new MUAs.
Eric Wong994d6c62006-05-14 19:13:44 -0700712);
713
Petr Baudis3cb8caf2006-07-03 22:47:58 +0200714if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) {
Eric Wong994d6c62006-05-14 19:13:44 -0700715 foreach my $file (@alias_files) {
716 open my $fh, '<', $file or die "opening $file: $!\n";
717 $parse_alias{$aliasfiletype}->($fh);
718 close $fh;
719 }
720}
721
Jacob Keller17b7a832015-11-19 14:52:11 -0800722if ($dump_aliases) {
723 print "$_\n" for (sort keys %aliases);
724 exit(0);
725}
726
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700727# is_format_patch_arg($f) returns 0 if $f names a patch, or 1 if
728# $f is a revision list specification to be passed to format-patch.
729sub is_format_patch_arg {
Jay Soffianeed6ca72009-02-14 23:32:13 -0500730 return unless $repo;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100731 my $f = shift;
732 try {
733 $repo->command('rev-parse', '--verify', '--quiet', $f);
734 if (defined($format_patch)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100735 return $format_patch;
736 }
Ævar Arnfjörð Bjarmason4adbf382021-05-28 11:23:47 +0200737 die sprintf(__(<<EOF), $f, $f);
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100738File '%s' exists but it could also be the range of commits
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100739to produce patches for. Please disambiguate by...
740
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100741 * Saying "./%s" if you mean a file; or
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100742 * Giving --format-patch option if you mean a range.
743EOF
744 } catch Git::Error::Command with {
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700745 # Not a valid revision. Treat it as a filename.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100746 return 0;
747 }
748}
749
Jeff Kingaa548922008-01-18 09:19:36 -0500750# Now that all the defaults are set, process the rest of the command line
751# arguments and collect up the files that need to be processed.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100752my @rev_list_opts;
Junio C Hamano69f4ce52008-11-30 22:38:20 -0800753while (defined(my $f = shift @ARGV)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100754 if ($f eq "--") {
755 push @rev_list_opts, "--", @ARGV;
756 @ARGV = ();
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700757 } elsif (-d $f and !is_format_patch_arg($f)) {
Ævar Arnfjörð Bjarmasonc6038162010-09-30 13:42:54 +0000758 opendir my $dh, $f
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100759 or die sprintf(__("Failed to opendir %s: %s"), $f, $!);
Jeff Kingaa548922008-01-18 09:19:36 -0500760
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200761 require File::Spec;
762 push @files, grep { -f $_ } map { File::Spec->catfile($f, $_) }
Ævar Arnfjörð Bjarmasonc6038162010-09-30 13:42:54 +0000763 sort readdir $dh;
764 closedir $dh;
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700765 } elsif ((-f $f or -p $f) and !is_format_patch_arg($f)) {
Jeff Kingaa548922008-01-18 09:19:36 -0500766 push @files, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500767 } else {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100768 push @rev_list_opts, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500769 }
770}
771
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100772if (@rev_list_opts) {
Vasco Almeida46493102016-12-14 11:54:36 -0100773 die __("Cannot run git format-patch from outside a repository\n")
Jay Soffianeed6ca72009-02-14 23:32:13 -0500774 unless $repo;
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200775 require File::Temp;
Kyle Meyer8774aa52022-11-26 15:21:23 -0500776 push @files, $repo->command('format-patch', '-o', File::Temp::tempdir(CLEANUP => 1),
777 defined $reroll_count ? ('-v', $reroll_count) : (),
778 @rev_list_opts);
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100779}
780
Michael Strawbridgea8022c52023-04-19 16:27:03 -0400781if (defined $sender) {
782 $sender =~ s/^\s+|\s+$//g;
783 ($sender) = expand_aliases($sender);
784} else {
785 $sender = $repoauthor->() || $repocommitter->() || '';
786}
787
788# $sender could be an already sanitized address
789# (e.g. sendemail.from could be manually sanitized by user).
790# But it's a no-op to run sanitize_address on an already sanitized address.
791$sender = sanitize_address($sender);
792
793$time = time - scalar $#files;
Junio C Hamano531220b2016-03-17 22:40:05 -0700794
Michael Strawbridgea8022c52023-04-19 16:27:03 -0400795@files = handle_backup_files(@files);
796
Jeff Kingaa548922008-01-18 09:19:36 -0500797if (@files) {
798 unless ($quiet) {
799 print $_,"\n" for (@files);
800 }
801} else {
Vasco Almeida46493102016-12-14 11:54:36 -0100802 print STDERR __("\nNo patch files specified!\n\n");
Jeff Kingaa548922008-01-18 09:19:36 -0500803 usage();
804}
805
Ævar Arnfjörð Bjarmasonacf071b2010-09-30 13:42:57 +0000806sub get_patch_subject {
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100807 my $fn = shift;
808 open (my $fh, '<', $fn);
809 while (my $line = <$fh>) {
810 next unless ($line =~ /^Subject: (.*)$/);
811 close $fh;
812 return "GIT: $1\n";
813 }
814 close $fh;
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100815 die sprintf(__("No subject line in %s?"), $fn);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100816}
817
818if ($compose) {
819 # Note that this does not need to be secure, but we will make a small
820 # effort to have it be unique
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200821 require File::Temp;
Jay Soffianafe756c2009-02-23 13:51:37 -0500822 $compose_filename = ($repo ?
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200823 File::Temp::tempfile(".gitsendemail.msg.XXXXXX", DIR => $repo->repo_path()) :
824 File::Temp::tempfile(".gitsendemail.msg.XXXXXX", DIR => "."))[1];
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000825 open my $c, ">", $compose_filename
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100826 or die sprintf(__("Failed to open for writing %s: %s"), $compose_filename, $!);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100827
828
Ævar Arnfjörð Bjarmasonfef381e2021-05-28 11:23:46 +0200829 my $tpl_sender = $sender || $repoauthor->() || $repocommitter->() || '';
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100830 my $tpl_subject = $initial_subject || '';
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100831 my $tpl_in_reply_to = $initial_in_reply_to || '';
Christian Ludwigd11c9432018-03-04 00:58:14 +0100832 my $tpl_reply_to = $reply_to || '';
Jeff King3ec61672023-10-20 06:15:24 -0400833 my $tpl_to = join(',', @initial_to);
834 my $tpl_cc = join(',', @initial_cc);
835 my $tpl_bcc = join(', ', @initial_bcc);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100836
Ævar Arnfjörð Bjarmason4adbf382021-05-28 11:23:47 +0200837 print $c <<EOT1, Git::prefix_lines("GIT: ", __(<<EOT2)), <<EOT3;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100838From $tpl_sender # This line is ignored.
Vasco Almeida70aedfb2016-12-14 11:54:38 -0100839EOT1
840Lines beginning in "GIT:" will be removed.
841Consider including an overall diffstat or table of contents
842for the patch you are writing.
843
844Clear the body content if you don't wish to send a summary.
845EOT2
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100846From: $tpl_sender
Jeff King3ec61672023-10-20 06:15:24 -0400847To: $tpl_to
848Cc: $tpl_cc
849Bcc: $tpl_bcc
Christian Ludwigd11c9432018-03-04 00:58:14 +0100850Reply-To: $tpl_reply_to
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100851Subject: $tpl_subject
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100852In-Reply-To: $tpl_in_reply_to
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100853
Vasco Almeida70aedfb2016-12-14 11:54:38 -0100854EOT3
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100855 for my $f (@files) {
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000856 print $c get_patch_subject($f);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100857 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000858 close $c;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100859
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100860 if ($annotate) {
861 do_edit($compose_filename, @files);
862 } else {
863 do_edit($compose_filename);
864 }
865
Jeff King637e8942023-10-20 06:13:10 -0400866 open my $c2, ">", $compose_filename . ".final"
867 or die sprintf(__("Failed to open %s.final: %s"), $compose_filename, $!);
868
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000869 open $c, "<", $compose_filename
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100870 or die sprintf(__("Failed to open %s: %s"), $compose_filename, $!);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100871
Jeff King637e8942023-10-20 06:13:10 -0400872 my $need_8bit_cte = file_has_nonascii($compose_filename);
873 my $in_body = 0;
874 my $summary_empty = 1;
Krzysztof Mazur4a47a4d2012-10-22 14:41:48 +0200875 if (!defined $compose_encoding) {
876 $compose_encoding = "UTF-8";
877 }
Jeff King637e8942023-10-20 06:13:10 -0400878 while(<$c>) {
879 next if m/^GIT:/;
880 if ($in_body) {
881 $summary_empty = 0 unless (/^\n$/);
882 } elsif (/^\n$/) {
883 $in_body = 1;
884 if ($need_8bit_cte) {
885 print $c2 "MIME-Version: 1.0\n",
886 "Content-Type: text/plain; ",
887 "charset=$compose_encoding\n",
888 "Content-Transfer-Encoding: 8bit\n";
889 }
890 } elsif (/^MIME-Version:/i) {
891 $need_8bit_cte = 0;
892 } elsif (/^Subject:\s*(.+)\s*$/i) {
893 $initial_subject = $1;
894 my $subject = $initial_subject;
895 $_ = "Subject: " .
896 quote_subject($subject, $compose_encoding) .
897 "\n";
898 } elsif (/^In-Reply-To:\s*(.+)\s*$/i) {
899 $initial_in_reply_to = $1;
900 next;
901 } elsif (/^Reply-To:\s*(.+)\s*$/i) {
902 $reply_to = $1;
903 } elsif (/^From:\s*(.+)\s*$/i) {
904 $sender = $1;
905 next;
Jeff King3ec61672023-10-20 06:15:24 -0400906 } elsif (/^To:\s*(.+)\s*$/i) {
907 @initial_to = parse_address_line($1);
908 next;
909 } elsif (/^Cc:\s*(.+)\s*$/i) {
910 @initial_cc = parse_address_line($1);
911 next;
912 } elsif (/^Bcc:/i) {
913 @initial_bcc = parse_address_line($1);
Jeff King637e8942023-10-20 06:13:10 -0400914 next;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100915 }
Jeff King637e8942023-10-20 06:13:10 -0400916 print $c2 $_;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100917 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000918 close $c;
Jeff King637e8942023-10-20 06:13:10 -0400919 close $c2;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100920
Jeff King637e8942023-10-20 06:13:10 -0400921 if ($summary_empty) {
Vasco Almeida46493102016-12-14 11:54:36 -0100922 print __("Summary email is empty, skipping it\n");
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100923 $compose = -1;
924 }
925} elsif ($annotate) {
926 do_edit(@files);
927}
928
Jeff Kingc0167262023-08-08 14:15:31 -0400929{
930 # Only instantiate one $term per program run, since some
931 # Term::ReadLine providers refuse to create a second instance.
932 my $term;
933 sub term {
934 require Term::ReadLine;
935 if (!defined $term) {
936 $term = $ENV{"GIT_SEND_EMAIL_NOTTY"}
937 ? Term::ReadLine->new('git-send-email', \*STDIN, \*STDOUT)
938 : Term::ReadLine->new('git-send-email');
939 }
940 return $term;
941 }
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200942}
943
Jay Soffian6e182512009-03-28 21:39:10 -0400944sub ask {
945 my ($prompt, %arg) = @_;
Jay Soffian0da43a62009-04-04 23:23:21 -0400946 my $valid_re = $arg{valid_re};
Jay Soffian6e182512009-03-28 21:39:10 -0400947 my $default = $arg{default};
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700948 my $confirm_only = $arg{confirm_only};
Jay Soffian6e182512009-03-28 21:39:10 -0400949 my $resp;
950 my $i = 0;
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200951 my $term = term();
Jay Soffian5906f542009-03-31 12:22:11 -0400952 return defined $default ? $default : undef
953 unless defined $term->IN and defined fileno($term->IN) and
954 defined $term->OUT and defined fileno($term->OUT);
Jay Soffian6e182512009-03-28 21:39:10 -0400955 while ($i++ < 10) {
956 $resp = $term->readline($prompt);
957 if (!defined $resp) { # EOF
958 print "\n";
959 return defined $default ? $default : undef;
960 }
961 if ($resp eq '' and defined $default) {
962 return $default;
963 }
Jay Soffian0da43a62009-04-04 23:23:21 -0400964 if (!defined $valid_re or $resp =~ /$valid_re/) {
Jay Soffian6e182512009-03-28 21:39:10 -0400965 return $resp;
966 }
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700967 if ($confirm_only) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100968 my $yesno = $term->readline(
969 # TRANSLATORS: please keep [y/N] as is.
970 sprintf(__("Are you sure you want to use <%s> [y/N]? "), $resp));
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700971 if (defined $yesno && $yesno =~ /y/i) {
972 return $resp;
973 }
974 }
Jay Soffian6e182512009-03-28 21:39:10 -0400975 }
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -0700976 return;
Jay Soffian6e182512009-03-28 21:39:10 -0400977}
978
Thomas Rast3cae7e52010-06-17 22:10:39 +0200979my %broken_encoding;
980
Ævar Arnfjörð Bjarmason1d50bfd2010-09-30 13:42:58 +0000981sub file_declares_8bit_cte {
Thomas Rast3cae7e52010-06-17 22:10:39 +0200982 my $fn = shift;
983 open (my $fh, '<', $fn);
984 while (my $line = <$fh>) {
985 last if ($line =~ /^$/);
986 return 1 if ($line =~ /^Content-Transfer-Encoding: .*8bit.*$/);
987 }
988 close $fh;
989 return 0;
990}
991
992foreach my $f (@files) {
993 next unless (body_or_subject_has_nonascii($f)
994 && !file_declares_8bit_cte($f));
995 $broken_encoding{$f} = 1;
996}
997
998if (!defined $auto_8bit_encoding && scalar %broken_encoding) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100999 print __("The following files are 8bit, but do not declare " .
1000 "a Content-Transfer-Encoding.\n");
Thomas Rast3cae7e52010-06-17 22:10:39 +02001001 foreach my $f (sort keys %broken_encoding) {
1002 print " $f\n";
1003 }
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001004 $auto_8bit_encoding = ask(__("Which 8bit encoding should I declare [UTF-8]? "),
Junio C Hamano852a15d2015-02-13 12:20:25 -08001005 valid_re => qr/.{4}/, confirm_only => 1,
Thomas Rast3cae7e52010-06-17 22:10:39 +02001006 default => "UTF-8");
1007}
1008
Thomas Rasta03bc5b2009-06-08 23:34:12 +02001009if (!$force) {
1010 for my $f (@files) {
Ævar Arnfjörð Bjarmason0d290a42010-09-30 13:43:01 +00001011 if (get_patch_subject($f) =~ /\Q*** SUBJECT HERE ***\E/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001012 die sprintf(__("Refusing to send because the patch\n\t%s\n"
Thomas Rasta03bc5b2009-06-08 23:34:12 +02001013 . "has the template subject '*** SUBJECT HERE ***'. "
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001014 . "Pass --force if you really want to send.\n"), $f);
Thomas Rasta03bc5b2009-06-08 23:34:12 +02001015 }
1016 }
1017}
1018
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001019my $to_whom = __("To whom should the emails be sent (if anyone)?");
Felipe Contreras8cac13d2012-11-24 12:16:19 +01001020my $prompting = 0;
Junio C Hamano8796ff72010-10-26 22:02:03 -07001021if (!@initial_to && !defined $to_cmd) {
Junio C Hamano0d6b21e2016-04-24 12:31:44 -07001022 my $to = ask("$to_whom ",
Stephen Boyd61837492012-09-06 11:31:11 -07001023 default => "",
Junio C Hamano51bbccf2012-08-14 15:15:53 -07001024 valid_re => qr/\@.*\./, confirm_only => 1);
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001025 push @initial_to, parse_address_line($to) if defined $to; # sanitized/validated later
Ryan Anderson1f038a02005-09-05 01:13:07 -04001026 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -04001027}
1028
Eric Wong994d6c62006-05-14 19:13:44 -07001029sub expand_aliases {
Jeff King302e04e2009-07-23 07:09:29 -04001030 return map { expand_one_alias($_) } @_;
1031}
1032
1033my %EXPANDED_ALIASES;
1034sub expand_one_alias {
1035 my $alias = shift;
1036 if ($EXPANDED_ALIASES{$alias}) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001037 die sprintf(__("fatal: alias '%s' expands to itself\n"), $alias);
Jeff King302e04e2009-07-23 07:09:29 -04001038 }
1039 local $EXPANDED_ALIASES{$alias} = 1;
1040 return $aliases{$alias} ? expand_aliases(@{$aliases{$alias}}) : $alias;
Eric Wong994d6c62006-05-14 19:13:44 -07001041}
1042
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001043@initial_to = process_address_list(@initial_to);
1044@initial_cc = process_address_list(@initial_cc);
Ævar Arnfjörð Bjarmasone60f6d12019-05-09 13:48:29 +02001045@initial_bcc = process_address_list(@initial_bcc);
Eric Wong994d6c62006-05-14 19:13:44 -07001046
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001047if ($thread && !defined $initial_in_reply_to && $prompting) {
1048 $initial_in_reply_to = ask(
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001049 __("Message-ID to be used as In-Reply-To for the first email (if any)? "),
Stephen Boyd61837492012-09-06 11:31:11 -07001050 default => "",
Junio C Hamano51bbccf2012-08-14 15:15:53 -07001051 valid_re => qr/\@.*\./, confirm_only => 1);
Ryan Anderson83b24432005-07-31 04:17:25 -04001052}
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001053if (defined $initial_in_reply_to) {
1054 $initial_in_reply_to =~ s/^\s*<?//;
1055 $initial_in_reply_to =~ s/>?\s*$//;
1056 $initial_in_reply_to = "<$initial_in_reply_to>" if $initial_in_reply_to ne '';
Junio C Hamanoace9c2a2007-12-10 21:44:42 -08001057}
Mike Hommeyace72082007-12-09 18:17:28 +01001058
Christian Ludwigd11c9432018-03-04 00:58:14 +01001059if (defined $reply_to) {
1060 $reply_to =~ s/^\s+|\s+$//g;
1061 ($reply_to) = expand_aliases($reply_to);
1062 $reply_to = sanitize_address($reply_to);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001063}
Eric Wongaca7ad72006-05-15 02:34:44 -07001064
Gregory Anderscd5b33f2021-05-14 09:15:53 -06001065if (!defined $sendmail_cmd && !defined $smtp_server) {
Florian Klink1ab2fd42017-11-28 01:49:04 +01001066 my @sendmail_paths = qw( /usr/sbin/sendmail /usr/lib/sendmail );
1067 push @sendmail_paths, map {"$_/sendmail"} split /:/, $ENV{PATH};
1068 foreach (@sendmail_paths) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001069 if (-x $_) {
Gregory Anderscd5b33f2021-05-14 09:15:53 -06001070 $sendmail_cmd = $_;
Eric Wongaca7ad72006-05-15 02:34:44 -07001071 last;
1072 }
1073 }
Gregory Anderscd5b33f2021-05-14 09:15:53 -06001074
1075 if (!defined $sendmail_cmd) {
1076 $smtp_server = 'localhost'; # could be 127.0.0.1, too... *shrug*
1077 }
Ryan Anderson3342d852005-07-31 20:04:24 -04001078}
1079
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001080if ($compose && $compose > 0) {
1081 @files = ($compose_filename . ".final", @files);
Ryan Anderson1f038a02005-09-05 01:13:07 -04001082}
1083
Ryan Anderson83b24432005-07-31 04:17:25 -04001084# Variables we set as part of the loop over files
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001085our ($message_id, %mail, $subject, $in_reply_to, $references, $message,
Jay Soffiandc1460a2009-03-31 12:22:12 -04001086 $needs_confirm, $message_num, $ask_default);
Ryan Anderson83b24432005-07-31 04:17:25 -04001087
Eric Wong567ffeb2006-03-25 16:47:12 -08001088sub extract_valid_address {
1089 my $address = shift;
Ævar Arnfjörð Bjarmason35b6ab92010-09-30 19:03:31 +00001090 my $local_part_regexp = qr/[^<>"\s@]+/;
1091 my $domain_regexp = qr/[^.<>"\s@]+(?:\.[^.<>"\s@]+)+/;
Eric Wongdb3106b2006-05-15 02:41:01 -07001092
1093 # check for a local address:
Junio C Hamanoad9c18f2006-06-06 00:05:56 -07001094 return $address if ($address =~ /^($local_part_regexp)$/);
Eric Wongdb3106b2006-05-15 02:41:01 -07001095
Uwe Kleine-König155197e2007-08-09 15:27:57 +02001096 $address =~ s/^\s*<(.*)>\s*$/$1/;
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +02001097 my $have_email_valid = eval { require Email::Valid; 1 };
Eric Wong567ffeb2006-03-25 16:47:12 -08001098 if ($have_email_valid) {
Junio C Hamanoad9c18f2006-06-06 00:05:56 -07001099 return scalar Email::Valid->address($address);
Eric Wong567ffeb2006-03-25 16:47:12 -08001100 }
Krzysztof Mazur95c0d4b2012-11-22 19:12:09 +01001101
1102 # less robust/correct than the monster regexp in Email::Valid,
1103 # but still does a 99% job, and one less dependency
1104 return $1 if $address =~ /($local_part_regexp\@$domain_regexp)/;
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -07001105 return;
Eric Wong567ffeb2006-03-25 16:47:12 -08001106}
Ryan Anderson83b24432005-07-31 04:17:25 -04001107
Krzysztof Mazure4312252012-11-22 19:12:10 +01001108sub extract_valid_address_or_die {
1109 my $address = shift;
Taylor Blau12288cc2023-09-18 12:35:53 -04001110 my $valid_address = extract_valid_address($address);
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001111 die sprintf(__("error: unable to extract a valid address from: %s\n"), $address)
Taylor Blau12288cc2023-09-18 12:35:53 -04001112 if !$valid_address;
1113 return $valid_address;
Krzysztof Mazure4312252012-11-22 19:12:10 +01001114}
1115
1116sub validate_address {
1117 my $address = shift;
Krzysztof Mazurd0e98102012-11-22 19:12:12 +01001118 while (!extract_valid_address($address)) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001119 printf STDERR __("error: unable to extract a valid address from: %s\n"), $address;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001120 # TRANSLATORS: Make sure to include [q] [d] [e] in your
1121 # translation. The program will only accept English input
1122 # at this point.
1123 $_ = ask(__("What to do with this address? ([q]uit|[d]rop|[e]dit): "),
Krzysztof Mazurd0e98102012-11-22 19:12:12 +01001124 valid_re => qr/^(?:quit|q|drop|d|edit|e)/i,
Krzysztof Mazur5c80afe2012-11-22 19:12:11 +01001125 default => 'q');
1126 if (/^d/i) {
1127 return undef;
1128 } elsif (/^q/i) {
1129 cleanup_compose_files();
1130 exit(0);
1131 }
Junio C Hamano0d6b21e2016-04-24 12:31:44 -07001132 $address = ask("$to_whom ",
Krzysztof Mazurd0e98102012-11-22 19:12:12 +01001133 default => "",
1134 valid_re => qr/\@.*\./, confirm_only => 1);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001135 }
1136 return $address;
1137}
1138
1139sub validate_address_list {
1140 return (grep { defined $_ }
1141 map { validate_address($_) } @_);
Ryan Anderson83b24432005-07-31 04:17:25 -04001142}
1143
1144# Usually don't need to change anything below here.
1145
1146# we make a "fake" message id by taking the current number
1147# of seconds since the beginning of Unix time and tacking on
1148# a random number to the end, in case we are called quicker than
1149# 1 second since the last time we were called.
Ryan Anderson8037d1a2005-07-31 20:04:24 -04001150
1151# We'll setup a template for the message id, using the "from" address:
Ryan Anderson8037d1a2005-07-31 20:04:24 -04001152
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001153my ($message_id_stamp, $message_id_serial);
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001154sub make_message_id {
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001155 my $uniq;
1156 if (!defined $message_id_stamp) {
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +02001157 require POSIX;
1158 $message_id_stamp = POSIX::strftime("%Y%m%d%H%M%S.$$", gmtime(time));
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001159 $message_id_serial = 0;
1160 }
1161 $message_id_serial++;
1162 $uniq = "$message_id_stamp-$message_id_serial";
1163
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001164 my $du_part;
Ævar Arnfjörð Bjarmasonfef381e2021-05-28 11:23:46 +02001165 for ($sender, $repocommitter->(), $repoauthor->()) {
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001166 $du_part = extract_valid_address(sanitize_address($_));
1167 last if (defined $du_part and $du_part ne '');
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001168 }
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001169 if (not defined $du_part or $du_part eq '') {
Ævar Arnfjörð Bjarmason529dd382010-09-30 13:43:08 +00001170 require Sys::Hostname;
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001171 $du_part = 'user@' . Sys::Hostname::hostname();
1172 }
Eric Wongf916ab02016-04-06 20:07:14 +00001173 my $message_id_template = "<%s-%s>";
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001174 $message_id = sprintf($message_id_template, $uniq, $du_part);
Ryan Anderson8037d1a2005-07-31 20:04:24 -04001175 #print "new message id = $message_id\n"; # Was useful for debugging
Ryan Anderson83b24432005-07-31 04:17:25 -04001176}
1177
Jürgen Rühle374c5902007-01-10 13:36:39 -08001178sub unquote_rfc2047 {
1179 local ($_) = @_;
Роман Донченко11f70a72014-12-14 18:59:46 +03001180 my $charset;
Роман Донченкоab47e2a2014-12-14 18:59:47 +03001181 my $sep = qr/[ \t]+/;
1182 s{$re_encoded_word(?:$sep$re_encoded_word)*}{
1183 my @words = split $sep, $&;
1184 foreach (@words) {
1185 m/$re_encoded_word/;
1186 $charset = $1;
1187 my $encoding = $2;
1188 my $text = $3;
1189 if ($encoding eq 'q' || $encoding eq 'Q') {
1190 $_ = $text;
1191 s/_/ /g;
1192 s/=([0-9A-F]{2})/chr(hex($1))/egi;
1193 } else {
1194 # other encodings not supported yet
1195 }
Роман Донченко11f70a72014-12-14 18:59:46 +03001196 }
Роман Донченкоab47e2a2014-12-14 18:59:47 +03001197 join '', @words;
Thomas Rastb622d4d2012-07-30 21:25:40 +02001198 }eg;
Роман Донченко11f70a72014-12-14 18:59:46 +03001199 return wantarray ? ($_, $charset) : $_;
Jürgen Rühle374c5902007-01-10 13:36:39 -08001200}
1201
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001202sub quote_rfc2047 {
1203 local $_ = shift;
Brandon Caseyd1fff6f2009-06-06 20:12:31 -05001204 my $encoding = shift || 'UTF-8';
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001205 s/([^-a-zA-Z0-9!*+\/])/sprintf("=%02X", ord($1))/eg;
1206 s/(.*)/=\?$encoding\?q\?$1\?=/;
1207 return $_;
1208}
1209
Brandon Caseya3a82622009-06-07 19:25:58 -05001210sub is_rfc2047_quoted {
1211 my $s = shift;
Brandon Caseya3a82622009-06-07 19:25:58 -05001212 length($s) <= 75 &&
Роман Донченко11f70a72014-12-14 18:59:46 +03001213 $s =~ m/^(?:"[[:ascii:]]*"|$re_encoded_word)$/o;
Brandon Caseya3a82622009-06-07 19:25:58 -05001214}
1215
Krzysztof Mazurce547802012-10-24 23:08:26 +02001216sub subject_needs_rfc2047_quoting {
1217 my $s = shift;
1218
Krzysztof Mazurce1459f2012-10-24 23:28:29 +02001219 return ($s =~ /[^[:ascii:]]/) || ($s =~ /=\?/);
Krzysztof Mazurce547802012-10-24 23:08:26 +02001220}
1221
1222sub quote_subject {
1223 local $subject = shift;
1224 my $encoding = shift || 'UTF-8';
1225
1226 if (subject_needs_rfc2047_quoting($subject)) {
1227 return quote_rfc2047($subject, $encoding);
1228 }
1229 return $subject;
1230}
1231
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001232# use the simplest quoting being able to handle the recipient
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001233sub sanitize_address {
Robin H. Johnson732263d2007-04-25 19:37:19 -07001234 my ($recipient) = @_;
Krzysztof Mazur831a4882012-11-22 19:12:08 +01001235
1236 # remove garbage after email address
1237 $recipient =~ s/(.*>).*$/$1/;
1238
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001239 my ($recipient_name, $recipient_addr) = ($recipient =~ /^(.*?)\s*(<.*)/);
1240
1241 if (not $recipient_name) {
Ævar Arnfjörð Bjarmasonff483892010-09-30 13:43:02 +00001242 return $recipient;
Robin H. Johnson732263d2007-04-25 19:37:19 -07001243 }
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001244
1245 # if recipient_name is already quoted, do nothing
Brandon Caseya3a82622009-06-07 19:25:58 -05001246 if (is_rfc2047_quoted($recipient_name)) {
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001247 return $recipient;
1248 }
1249
Remi Lespinet1fe97032015-06-30 14:16:49 +02001250 # remove non-escaped quotes
1251 $recipient_name =~ s/(^|[^\\])"/$1/g;
1252
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001253 # rfc2047 is needed if a non-ascii char is included
1254 if ($recipient_name =~ /[^[:ascii:]]/) {
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001255 $recipient_name = quote_rfc2047($recipient_name);
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001256 }
1257
1258 # double quotes are needed if specials or CTLs are included
1259 elsif ($recipient_name =~ /[][()<>@,;:\\".\000-\037\177]/) {
Remi Lespinet1fe97032015-06-30 14:16:49 +02001260 $recipient_name =~ s/([\\\r])/\\$1/g;
Ævar Arnfjörð Bjarmasond5c7d692010-09-30 13:43:03 +00001261 $recipient_name = qq["$recipient_name"];
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001262 }
1263
1264 return "$recipient_name $recipient_addr";
1265
Robin H. Johnson732263d2007-04-25 19:37:19 -07001266}
1267
Matthieu Moycb2922f2017-08-23 12:21:01 +02001268sub strip_garbage_one_address {
1269 my ($addr) = @_;
1270 chomp $addr;
1271 if ($addr =~ /^(("[^"]*"|[^"<]*)? *<[^>]*>).*/) {
1272 # "Foo Bar" <foobar@example.com> [possibly garbage here]
1273 # Foo Bar <foobar@example.com> [possibly garbage here]
1274 return $1;
1275 }
1276 if ($addr =~ /^(<[^>]*>).*/) {
1277 # <foo@example.com> [possibly garbage here]
1278 # if garbage contains other addresses, they are ignored.
1279 return $1;
1280 }
1281 if ($addr =~ /^([^"#,\s]*)/) {
1282 # address without quoting: remove anything after the address
1283 return $1;
1284 }
1285 return $addr;
1286}
1287
Krzysztof Mazure4312252012-11-22 19:12:10 +01001288sub sanitize_address_list {
1289 return (map { sanitize_address($_) } @_);
1290}
1291
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001292sub process_address_list {
Remi Lespinetb1c8a112015-06-30 14:16:50 +02001293 my @addr_list = map { parse_address_line($_) } @_;
1294 @addr_list = expand_aliases(@addr_list);
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001295 @addr_list = sanitize_address_list(@addr_list);
1296 @addr_list = validate_address_list(@addr_list);
1297 return @addr_list;
1298}
1299
Jari Aalto134550f2010-03-14 17:16:45 +02001300# Returns the local Fully Qualified Domain Name (FQDN) if available.
1301#
1302# Tightly configured MTAa require that a caller sends a real DNS
1303# domain name that corresponds the IP address in the HELO/EHLO
1304# handshake. This is used to verify the connection and prevent
1305# spammers from trying to hide their identity. If the DNS and IP don't
Elijah Newren15beaaa2019-11-05 17:07:23 +00001306# match, the receiving MTA may deny the connection.
Jari Aalto134550f2010-03-14 17:16:45 +02001307#
1308# Here is a deny example of Net::SMTP with the default "localhost.localdomain"
1309#
1310# Net::SMTP=GLOB(0x267ec28)>>> EHLO localhost.localdomain
1311# Net::SMTP=GLOB(0x267ec28)<<< 550 EHLO argument does not match calling host
1312#
1313# This maildomain*() code is based on ideas in Perl library Test::Reporter
1314# /usr/share/perl5/Test/Reporter/Mail/Util.pm ==> sub _maildomain ()
1315
Brian Gernhardt59a86302010-04-10 10:53:54 -04001316sub valid_fqdn {
1317 my $domain = shift;
Brandon Casey61ef5e92010-09-26 22:18:01 -05001318 return defined $domain && !($^O eq 'darwin' && $domain =~ /\.local$/) && $domain =~ /\./;
Brian Gernhardt59a86302010-04-10 10:53:54 -04001319}
1320
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001321sub maildomain_net {
Jari Aalto134550f2010-03-14 17:16:45 +02001322 my $maildomain;
1323
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +02001324 require Net::Domain;
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001325 my $domain = Net::Domain::domainname();
1326 $maildomain = $domain if valid_fqdn($domain);
Jari Aalto134550f2010-03-14 17:16:45 +02001327
1328 return $maildomain;
1329}
1330
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001331sub maildomain_mta {
Jari Aalto134550f2010-03-14 17:16:45 +02001332 my $maildomain;
1333
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001334 for my $host (qw(mailhost localhost)) {
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +02001335 require Net::SMTP;
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001336 my $smtp = Net::SMTP->new($host);
1337 if (defined $smtp) {
1338 my $domain = $smtp->domain;
1339 $smtp->quit;
Jari Aalto134550f2010-03-14 17:16:45 +02001340
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001341 $maildomain = $domain if valid_fqdn($domain);
Jari Aalto134550f2010-03-14 17:16:45 +02001342
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001343 last if $maildomain;
Jari Aalto134550f2010-03-14 17:16:45 +02001344 }
1345 }
1346
1347 return $maildomain;
1348}
1349
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001350sub maildomain {
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001351 return maildomain_net() || maildomain_mta() || 'localhost.localdomain';
Jari Aalto134550f2010-03-14 17:16:45 +02001352}
1353
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001354sub smtp_host_string {
1355 if (defined $smtp_server_port) {
1356 return "$smtp_server:$smtp_server_port";
1357 } else {
1358 return $smtp_server;
1359 }
1360}
1361
1362# Returns 1 if authentication succeeded or was not necessary
1363# (smtp_user was not specified), and 0 otherwise.
1364
1365sub smtp_auth_maybe {
Joshua Watt8dd9b3f2018-10-22 22:24:25 -05001366 if (!defined $smtp_authuser || $auth || (defined $smtp_auth && $smtp_auth eq "none")) {
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001367 return 1;
1368 }
1369
1370 # Workaround AUTH PLAIN/LOGIN interaction defect
1371 # with Authen::SASL::Cyrus
1372 eval {
1373 require Authen::SASL;
1374 Authen::SASL->import(qw(Perl));
1375 };
1376
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001377 # Check mechanism naming as defined in:
1378 # https://tools.ietf.org/html/rfc4422#page-8
Brian Norris904f6e72015-09-18 15:12:50 -07001379 if ($smtp_auth && $smtp_auth !~ /^(\b[A-Z0-9-_]{1,20}\s*)*$/) {
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001380 die "invalid smtp auth: '${smtp_auth}'";
1381 }
1382
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001383 # TODO: Authentication may fail not because credentials were
1384 # invalid but due to other reasons, in which we should not
1385 # reject credentials.
1386 $auth = Git::credential({
1387 'protocol' => 'smtp',
1388 'host' => smtp_host_string(),
1389 'username' => $smtp_authuser,
1390 # if there's no password, "git credential fill" will
1391 # give us one, otherwise it'll just pass this one.
1392 'password' => $smtp_authpass
1393 }, sub {
1394 my $cred = shift;
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001395
1396 if ($smtp_auth) {
1397 my $sasl = Authen::SASL->new(
1398 mechanism => $smtp_auth,
1399 callback => {
1400 user => $cred->{'username'},
1401 pass => $cred->{'password'},
1402 authname => $cred->{'username'},
1403 }
1404 );
1405
1406 return !!$smtp->auth($sasl);
1407 }
1408
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001409 return !!$smtp->auth($cred->{'username'}, $cred->{'password'});
1410 });
1411
1412 return $auth;
1413}
1414
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001415sub ssl_verify_params {
1416 eval {
1417 require IO::Socket::SSL;
1418 IO::Socket::SSL->import(qw/SSL_VERIFY_PEER SSL_VERIFY_NONE/);
1419 };
1420 if ($@) {
1421 print STDERR "Not using SSL_VERIFY_PEER due to out-of-date IO::Socket::SSL.\n";
1422 return;
1423 }
1424
1425 if (!defined $smtp_ssl_cert_path) {
Ruben Kerkhof01645b72014-01-15 21:31:11 +04001426 # use the OpenSSL defaults
1427 return (SSL_verify_mode => SSL_VERIFY_PEER());
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001428 }
1429
1430 if ($smtp_ssl_cert_path eq "") {
1431 return (SSL_verify_mode => SSL_VERIFY_NONE());
1432 } elsif (-d $smtp_ssl_cert_path) {
1433 return (SSL_verify_mode => SSL_VERIFY_PEER(),
1434 SSL_ca_path => $smtp_ssl_cert_path);
1435 } elsif (-f $smtp_ssl_cert_path) {
1436 return (SSL_verify_mode => SSL_VERIFY_PEER(),
1437 SSL_ca_file => $smtp_ssl_cert_path);
1438 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001439 die sprintf(__("CA path \"%s\" does not exist"), $smtp_ssl_cert_path);
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001440 }
1441}
1442
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001443sub file_name_is_absolute {
1444 my ($path) = @_;
1445
1446 # msys does not grok DOS drive-prefixes
1447 if ($^O eq 'msys') {
Junio C Hamanof24ecf52014-04-23 09:37:38 -07001448 return ($path =~ m#^/# || $path =~ m#^[a-zA-Z]\:#)
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001449 }
1450
1451 require File::Spec::Functions;
1452 return File::Spec::Functions::file_name_is_absolute($path);
1453}
1454
Michael Strawbridge56addda2023-04-19 16:27:02 -04001455sub gen_header {
Eric Wong4bc87a22006-03-25 17:20:48 -08001456 my @recipients = unique_email_list(@to);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001457 @cc = (grep { my $cc = extract_valid_address_or_die($_);
Joe Perches83acaae2010-11-20 15:06:05 -08001458 not grep { $cc eq $_ || $_ =~ /<\Q${cc}\E>$/ } @recipients
Ask Bjørn Hansen7ac17522007-11-19 03:00:26 -08001459 }
Ask Bjørn Hansen7ac17522007-11-19 03:00:26 -08001460 @cc);
Eric Wong4bc87a22006-03-25 17:20:48 -08001461 my $to = join (",\n\t", @recipients);
Ævar Arnfjörð Bjarmasone60f6d12019-05-09 13:48:29 +02001462 @recipients = unique_email_list(@recipients,@cc,@initial_bcc);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001463 @recipients = (map { extract_valid_address_or_die($_) } @recipients);
Jakub Narebski6bdca892006-07-07 20:57:55 +02001464 my $date = format_2822_time($time++);
Martin Langhoffe923eff2006-05-03 09:44:36 +12001465 my $gitversion = '@@GIT_VERSION@@';
1466 if ($gitversion =~ m/..GIT_VERSION../) {
Petr Baudis3cb8caf2006-07-03 22:47:58 +02001467 $gitversion = Git::version();
Martin Langhoffe923eff2006-05-03 09:44:36 +12001468 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001469
Joe Perches02461e02009-10-08 10:03:26 -07001470 my $cc = join(",\n\t", unique_email_list(@cc));
Junio C Hamanof06a6a42007-04-16 16:51:47 -07001471 my $ccline = "";
1472 if ($cc ne '') {
1473 $ccline = "\nCc: $cc";
1474 }
Jeff King4f3d3702007-12-17 15:51:34 -05001475 make_message_id() unless defined($message_id);
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001476
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001477 my $header = "From: $sender
Junio C Hamanof06a6a42007-04-16 16:51:47 -07001478To: $to${ccline}
Eric Wong4bc87a22006-03-25 17:20:48 -08001479Subject: $subject
Eric Wong4bc87a22006-03-25 17:20:48 -08001480Date: $date
Junio C Hamanoba4324c2022-12-16 10:47:19 +09001481Message-ID: $message_id
Eric Wong4bc87a22006-03-25 17:20:48 -08001482";
Luis Henriquesac1596a2014-03-24 21:38:27 +00001483 if ($use_xmailer) {
1484 $header .= "X-Mailer: git-send-email $gitversion\n";
1485 }
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001486 if ($in_reply_to) {
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001487
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001488 $header .= "In-Reply-To: $in_reply_to\n";
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001489 $header .= "References: $references\n";
1490 }
Christian Ludwigd11c9432018-03-04 00:58:14 +01001491 if ($reply_to) {
1492 $header .= "Reply-To: $reply_to\n";
1493 }
Junio C Hamanoce91c2f2006-10-05 16:36:49 -07001494 if (@xh) {
1495 $header .= join("\n", @xh) . "\n";
1496 }
Michael Strawbridge56addda2023-04-19 16:27:02 -04001497 my $recipients_ref = \@recipients;
1498 return ($recipients_ref, $to, $date, $gitversion, $cc, $ccline, $header);
1499}
1500
1501# Prepares the email, then asks the user what to do.
1502#
1503# If the user chooses to send the email, it's sent and 1 is returned.
1504# If the user chooses not to send the email, 0 is returned.
1505# If the user decides they want to make further edits, -1 is returned and the
1506# caller is expected to call send_message again after the edits are performed.
1507#
1508# If an error occurs sending the email, this just dies.
1509
1510sub send_message {
1511 my ($recipients_ref, $to, $date, $gitversion, $cc, $ccline, $header) = gen_header();
1512 my @recipients = @$recipients_ref;
Ryan Anderson83b24432005-07-31 04:17:25 -04001513
Robin H. Johnsonc38f0242007-04-25 19:37:20 -07001514 my @sendmail_parameters = ('-i', @recipients);
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001515 my $raw_from = $sender;
Felipe Contrerasc89e3242009-11-26 21:04:29 +02001516 if (defined $envelope_sender && $envelope_sender ne "auto") {
1517 $raw_from = $envelope_sender;
1518 }
Robin H. Johnsonf073a592007-04-25 19:37:22 -07001519 $raw_from = extract_valid_address($raw_from);
1520 unshift (@sendmail_parameters,
1521 '-f', $raw_from) if(defined $envelope_sender);
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001522
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001523 if ($needs_confirm && !$dry_run) {
1524 print "\n$header\n";
1525 if ($needs_confirm eq "inform") {
1526 $confirm_unconfigured = 0; # squelch this message for the rest of this run
Jay Soffian6e182512009-03-28 21:39:10 -04001527 $ask_default = "y"; # assume yes on EOF since user hasn't explicitly asked for confirmation
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001528 print __ <<EOF ;
1529 The Cc list above has been expanded by additional
1530 addresses found in the patch commit message. By default
1531 send-email prompts before sending whenever this occurs.
1532 This behavior is controlled by the sendemail.confirm
1533 configuration setting.
1534
1535 For additional information, run 'git send-email --help'.
1536 To retain the current behavior, but squelch this message,
1537 run 'git config --global sendemail.confirm auto'.
1538
1539EOF
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001540 }
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001541 # TRANSLATORS: Make sure to include [y] [n] [e] [q] [a] in your
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001542 # translation. The program will only accept English input
1543 # at this point.
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001544 $_ = ask(__("Send this email? ([y]es|[n]o|[e]dit|[q]uit|[a]ll): "),
1545 valid_re => qr/^(?:yes|y|no|n|edit|e|quit|q|all|a)/i,
Jay Soffian6e182512009-03-28 21:39:10 -04001546 default => $ask_default);
Vasco Almeida46493102016-12-14 11:54:36 -01001547 die __("Send this email reply required") unless defined $_;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001548 if (/^n/i) {
Michael Witten15da1082009-04-13 13:23:51 -05001549 return 0;
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001550 } elsif (/^e/i) {
1551 return -1;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001552 } elsif (/^q/i) {
1553 cleanup_compose_files();
1554 exit(0);
1555 } elsif (/^a/i) {
1556 $confirm = 'never';
1557 }
1558 }
1559
Pascal Obry052fbea2010-09-06 20:12:11 +02001560 unshift (@sendmail_parameters, @smtp_server_options);
1561
Matthew Wilcox61302592006-10-10 08:58:23 -06001562 if ($dry_run) {
1563 # We don't want to send the email.
Gregory Anderscd5b33f2021-05-14 09:15:53 -06001564 } elsif (defined $sendmail_cmd || file_name_is_absolute($smtp_server)) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001565 my $pid = open my $sm, '|-';
1566 defined $pid or die $!;
1567 if (!$pid) {
Gregory Anderscd5b33f2021-05-14 09:15:53 -06001568 if (defined $sendmail_cmd) {
1569 exec ("sh", "-c", "$sendmail_cmd \"\$@\"", "-", @sendmail_parameters)
1570 or die $!;
1571 } else {
1572 exec ($smtp_server, @sendmail_parameters)
1573 or die $!;
1574 }
Eric Wongaca7ad72006-05-15 02:34:44 -07001575 }
1576 print $sm "$header\n$message";
Ævar Arnfjörð Bjarmason5e2c2ab2010-09-30 13:43:07 +00001577 close $sm or die $!;
Eric Wongaca7ad72006-05-15 02:34:44 -07001578 } else {
Junio C Hamano44b24762007-09-25 17:27:54 -07001579
1580 if (!defined $smtp_server) {
Vasco Almeida46493102016-12-14 11:54:36 -01001581 die __("The required SMTP server is not properly defined.")
Junio C Hamano44b24762007-09-25 17:27:54 -07001582 }
1583
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001584 require Net::SMTP;
Jonathan Niederbfbfc9a2017-05-31 17:17:43 -07001585 my $use_net_smtp_ssl = version->parse($Net::SMTP::VERSION) < version->parse("2.34");
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001586 $smtp_domain ||= maildomain();
1587
Thomas Rastf6bebd12008-06-25 21:42:43 +02001588 if ($smtp_encryption eq 'ssl') {
Junio C Hamano44b24762007-09-25 17:27:54 -07001589 $smtp_server_port ||= 465; # ssmtp
Thomas Rast5508f3e2013-12-01 23:48:43 +01001590 require IO::Socket::SSL;
John Keeping9d605242015-12-03 21:47:18 +00001591
1592 # Suppress "variable accessed once" warning.
1593 {
1594 no warnings 'once';
1595 $IO::Socket::SSL::DEBUG = 1;
1596 }
1597
Thomas Rast5508f3e2013-12-01 23:48:43 +01001598 # Net::SMTP::SSL->new() does not forward any SSL options
1599 IO::Socket::SSL::set_client_defaults(
1600 ssl_verify_params());
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001601
1602 if ($use_net_smtp_ssl) {
1603 require Net::SMTP::SSL;
1604 $smtp ||= Net::SMTP::SSL->new($smtp_server,
1605 Hello => $smtp_domain,
1606 Port => $smtp_server_port,
1607 Debug => $debug_net_smtp);
1608 }
1609 else {
1610 $smtp ||= Net::SMTP->new($smtp_server,
1611 Hello => $smtp_domain,
1612 Port => $smtp_server_port,
1613 Debug => $debug_net_smtp,
1614 SSL => 1);
1615 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001616 }
Jules Maselbas636f3d72018-07-14 10:58:48 +02001617 elsif (!$smtp) {
brian m. carlson1a741bf2013-07-04 22:04:52 +00001618 $smtp_server_port ||= 25;
1619 $smtp ||= Net::SMTP->new($smtp_server,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001620 Hello => $smtp_domain,
brian m. carlson1a741bf2013-07-04 22:04:52 +00001621 Debug => $debug_net_smtp,
1622 Port => $smtp_server_port);
Yakov Lernerfb3650e2009-09-25 15:10:21 -07001623 if ($smtp_encryption eq 'tls' && $smtp) {
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001624 if ($use_net_smtp_ssl) {
1625 $smtp->command('STARTTLS');
1626 $smtp->response();
1627 if ($smtp->code != 220) {
1628 die sprintf(__("Server does not support STARTTLS! %s"), $smtp->message);
1629 }
1630 require Net::SMTP::SSL;
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001631 $smtp = Net::SMTP::SSL->start_SSL($smtp,
1632 ssl_verify_params())
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001633 or die sprintf(__("STARTTLS failed! %s"), IO::Socket::SSL::errstr());
Thomas Rastf6bebd12008-06-25 21:42:43 +02001634 }
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001635 else {
1636 $smtp->starttls(ssl_verify_params())
1637 or die sprintf(__("STARTTLS failed! %s"), IO::Socket::SSL::errstr());
1638 }
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001639 # Send EHLO again to receive fresh
1640 # supported commands
1641 $smtp->hello($smtp_domain);
Thomas Rastf6bebd12008-06-25 21:42:43 +02001642 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001643 }
Junio C Hamano44b24762007-09-25 17:27:54 -07001644
1645 if (!$smtp) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001646 die __("Unable to initialize SMTP properly. Check config and use --smtp-debug."),
1647 " VALUES: server=$smtp_server ",
Jari Aaltoe5afb3a2010-03-14 17:15:33 +02001648 "encryption=$smtp_encryption ",
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001649 "hello=$smtp_domain",
Sylvain Rabota1dd7e12011-04-29 20:23:24 +02001650 defined $smtp_server_port ? " port=$smtp_server_port" : "";
Junio C Hamano44b24762007-09-25 17:27:54 -07001651 }
1652
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001653 smtp_auth_maybe or die $smtp->message;
Michael Witten2363d742008-02-03 19:53:56 -05001654
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001655 $smtp->mail( $raw_from ) or die $smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001656 $smtp->to( @recipients ) or die $smtp->message;
1657 $smtp->data or die $smtp->message;
Stefan Agnerf60c4832015-09-30 09:26:09 +02001658 $smtp->datasend("$header\n") or die $smtp->message;
1659 my @lines = split /^/, $message;
1660 foreach my $line (@lines) {
1661 $smtp->datasend("$line") or die $smtp->message;
1662 }
Eric Wongaca7ad72006-05-15 02:34:44 -07001663 $smtp->dataend() or die $smtp->message;
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001664 $smtp->code =~ /250|200/ or die sprintf(__("Failed to send %s\n"), $subject).$smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001665 }
Ryan Anderson27184352006-02-05 20:13:52 -05001666 if ($quiet) {
Dragan Simicc02dc382024-04-10 09:01:29 +02001667 printf($dry_run ? __("Dry-Sent %s") : __("Sent %s"), $subject);
1668 print "\n";
Ryan Anderson27184352006-02-05 20:13:52 -05001669 } else {
Dragan Simicc02dc382024-04-10 09:01:29 +02001670 print($dry_run ? __("Dry-OK. Log says:") : __("OK. Log says:"));
1671 print "\n";
Gregory Anderscd5b33f2021-05-14 09:15:53 -06001672 if (!defined $sendmail_cmd && !file_name_is_absolute($smtp_server)) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001673 print "Server: $smtp_server\n";
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001674 print "MAIL FROM:<$raw_from>\n";
Joe Perches02461e02009-10-08 10:03:26 -07001675 foreach my $entry (@recipients) {
1676 print "RCPT TO:<$entry>\n";
1677 }
Eric Wongaca7ad72006-05-15 02:34:44 -07001678 } else {
Gregory Anderscd5b33f2021-05-14 09:15:53 -06001679 my $sm;
1680 if (defined $sendmail_cmd) {
1681 $sm = $sendmail_cmd;
1682 } else {
1683 $sm = $smtp_server;
1684 }
1685
1686 print "Sendmail: $sm ".join(' ',@sendmail_parameters)."\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001687 }
David D. Kilzerb7f30e02007-11-18 20:14:55 -08001688 print $header, "\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001689 if ($smtp) {
Vasco Almeida46493102016-12-14 11:54:36 -01001690 print __("Result: "), $smtp->code, ' ',
Dragan Simicc02dc382024-04-10 09:01:29 +02001691 ($smtp->message =~ /\n([^\n]+\n)$/s);
Eric Wongaca7ad72006-05-15 02:34:44 -07001692 } else {
Dragan Simicc02dc382024-04-10 09:01:29 +02001693 print __("Result: OK");
Eric Wongaca7ad72006-05-15 02:34:44 -07001694 }
Dragan Simicc02dc382024-04-10 09:01:29 +02001695 print "\n";
Ryan Anderson30d08b32006-02-02 11:56:06 -05001696 }
Michael Witten15da1082009-04-13 13:23:51 -05001697
1698 return 1;
Ryan Anderson83b24432005-07-31 04:17:25 -04001699}
1700
Michael Strawbridge56addda2023-04-19 16:27:02 -04001701sub pre_process_file {
1702 my ($t, $quiet) = @_;
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001703
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001704 open my $fh, "<", $t or die sprintf(__("can't open file %s"), $t);
Ryan Anderson83b24432005-07-31 04:17:25 -04001705
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001706 my $author = undef;
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001707 my $sauthor = undef;
Jeff King8291db62007-11-16 05:49:09 -05001708 my $author_encoding;
1709 my $has_content_type;
1710 my $body_encoding;
Paolo Bonzinibb294562014-11-25 15:00:26 +01001711 my $xfer_encoding;
1712 my $has_mime_version;
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001713 @to = ();
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001714 @cc = ();
Junio C Hamanoce91c2f2006-10-05 16:36:49 -07001715 @xh = ();
Junio C Hamanoe6b09642006-10-07 03:09:05 -07001716 my $input_format = undef;
Jay Soffian50126992009-02-14 23:32:14 -05001717 my @header = ();
Marvin Häusere0821132021-08-30 15:30:01 +00001718 $subject = $initial_subject;
Ryan Anderson83b24432005-07-31 04:17:25 -04001719 $message = "";
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001720 $message_num++;
Junio C Hamano3ece9bf2023-05-17 14:10:39 -07001721 undef $message_id;
Maxim Cournoyerba921062023-05-01 10:38:47 -04001722 # Retrieve and unfold header fields.
1723 my @header_lines = ();
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001724 while(<$fh>) {
Jay Soffian50126992009-02-14 23:32:14 -05001725 last if /^\s*$/;
Maxim Cournoyerba921062023-05-01 10:38:47 -04001726 push(@header_lines, $_);
1727 }
1728 @header = unfold_headers(@header_lines);
1729 # Add computed headers, if applicable.
1730 unless ($no_header_cmd || ! $header_cmd) {
1731 push @header, invoke_header_cmd($header_cmd, $t);
Jay Soffian50126992009-02-14 23:32:14 -05001732 }
1733 # Now parse the header
1734 foreach(@header) {
1735 if (/^From /) {
1736 $input_format = 'mbox';
1737 next;
1738 }
1739 chomp;
1740 if (!defined $input_format && /^[-A-Za-z]+:\s/) {
1741 $input_format = 'mbox';
1742 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001743
Jay Soffian50126992009-02-14 23:32:14 -05001744 if (defined $input_format && $input_format eq 'mbox') {
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001745 if (/^Subject:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001746 $subject = $1;
1747 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001748 elsif (/^From:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001749 ($author, $author_encoding) = unquote_rfc2047($1);
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001750 $sauthor = sanitize_address($author);
Jay Soffian50126992009-02-14 23:32:14 -05001751 next if $suppress_cc{'author'};
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001752 next if $suppress_cc{'self'} and $sauthor eq $sender;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001753 printf(__("(mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001754 $1, $_) unless $quiet;
1755 push @cc, $1;
1756 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001757 elsif (/^To:\s+(.*)$/i) {
Stephen Boyd21802cd2010-09-29 00:26:44 -07001758 foreach my $addr (parse_address_line($1)) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001759 printf(__("(mbox) Adding to: %s from line '%s'\n"),
Stephen Boyd21802cd2010-09-29 00:26:44 -07001760 $addr, $_) unless $quiet;
Krzysztof Mazure4312252012-11-22 19:12:10 +01001761 push @to, $addr;
Stephen Boyd21802cd2010-09-29 00:26:44 -07001762 }
1763 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001764 elsif (/^Cc:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001765 foreach my $addr (parse_address_line($1)) {
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001766 my $qaddr = unquote_rfc2047($addr);
1767 my $saddr = sanitize_address($qaddr);
1768 if ($saddr eq $sender) {
David Brown65648282007-12-25 19:56:29 -08001769 next if ($suppress_cc{'self'});
David Brown65648282007-12-25 19:56:29 -08001770 } else {
1771 next if ($suppress_cc{'cc'});
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001772 }
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001773 printf(__("(mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001774 $addr, $_) unless $quiet;
1775 push @cc, $addr;
Ryan Anderson83b24432005-07-31 04:17:25 -04001776 }
1777 }
Jay Soffian50126992009-02-14 23:32:14 -05001778 elsif (/^Content-type:/i) {
1779 $has_content_type = 1;
1780 if (/charset="?([^ "]+)/) {
1781 $body_encoding = $1;
1782 }
1783 push @xh, $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001784 }
Paolo Bonzinibb294562014-11-25 15:00:26 +01001785 elsif (/^MIME-Version/i) {
1786 $has_mime_version = 1;
1787 push @xh, $_;
1788 }
Junio C Hamanoba4324c2022-12-16 10:47:19 +09001789 elsif (/^Message-ID: (.*)/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001790 $message_id = $1;
1791 }
Paolo Bonzinibb294562014-11-25 15:00:26 +01001792 elsif (/^Content-Transfer-Encoding: (.*)/i) {
1793 $xfer_encoding = $1 if not defined $xfer_encoding;
1794 }
Stefan Agner256be1d2018-04-17 23:16:30 +02001795 elsif (/^In-Reply-To: (.*)/i) {
Rafael Aquinif9f60d72020-06-29 10:11:04 -04001796 if (!$initial_in_reply_to || $thread) {
1797 $in_reply_to = $1;
1798 }
Stefan Agner256be1d2018-04-17 23:16:30 +02001799 }
1800 elsif (/^References: (.*)/i) {
Rafael Aquinif9f60d72020-06-29 10:11:04 -04001801 if (!$initial_in_reply_to || $thread) {
1802 $references = $1;
1803 }
Stefan Agner256be1d2018-04-17 23:16:30 +02001804 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001805 elsif (!/^Date:\s/i && /^[-A-Za-z]+:\s+\S/) {
Jay Soffian50126992009-02-14 23:32:14 -05001806 push @xh, $_;
1807 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001808 } else {
Jay Soffian50126992009-02-14 23:32:14 -05001809 # In the traditional
1810 # "send lots of email" format,
1811 # line 1 = cc
1812 # line 2 = subject
1813 # So let's support that, too.
1814 $input_format = 'lots';
1815 if (@cc == 0 && !$suppress_cc{'cc'}) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001816 printf(__("(non-mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001817 $_, $_) unless $quiet;
1818 push @cc, $_;
1819 } elsif (!defined $subject) {
1820 $subject = $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001821 }
1822 }
1823 }
Jay Soffian50126992009-02-14 23:32:14 -05001824 # Now parse the message body
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001825 while(<$fh>) {
Jay Soffian50126992009-02-14 23:32:14 -05001826 $message .= $_;
Baruch Siach9c545812019-03-16 21:26:50 +02001827 if (/^([a-z][a-z-]*-by|Cc): (.*)/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001828 chomp;
Jay Soffian3531e272009-02-14 23:32:15 -05001829 my ($what, $c) = ($1, $2);
Matthieu Moycb2922f2017-08-23 12:21:01 +02001830 # strip garbage for the address we'll use:
1831 $c = strip_garbage_one_address($c);
1832 # sanitize a bit more to decide whether to suppress the address:
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001833 my $sc = sanitize_address($c);
1834 if ($sc eq $sender) {
Jay Soffian3531e272009-02-14 23:32:15 -05001835 next if ($suppress_cc{'self'});
1836 } else {
Rasmus Villemoesef0cc1d2018-10-16 09:39:23 +02001837 if ($what =~ /^Signed-off-by$/i) {
1838 next if $suppress_cc{'sob'};
1839 } elsif ($what =~ /-by$/i) {
1840 next if $suppress_cc{'misc-by'};
1841 } elsif ($what =~ /Cc/i) {
1842 next if $suppress_cc{'bodycc'};
1843 }
Jay Soffian3531e272009-02-14 23:32:15 -05001844 }
Rasmus Villemoesfb34fe62018-10-10 13:13:50 +02001845 if ($c !~ /.+@.+|<.+>/) {
1846 printf("(body) Ignoring %s from line '%s'\n",
1847 $what, $_) unless $quiet;
1848 next;
1849 }
Jay Soffian50126992009-02-14 23:32:14 -05001850 push @cc, $c;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001851 printf(__("(body) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001852 $c, $_) unless $quiet;
1853 }
1854 }
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001855 close $fh;
Joe Perches324a8bd2007-08-17 18:51:12 -07001856
Michael Strawbridge56addda2023-04-19 16:27:02 -04001857 push @to, recipients_cmd("to-cmd", "to", $to_cmd, $t, $quiet)
Joe Perches6e74e072010-09-24 10:03:00 -07001858 if defined $to_cmd;
Michael Strawbridge56addda2023-04-19 16:27:02 -04001859 push @cc, recipients_cmd("cc-cmd", "cc", $cc_cmd, $t, $quiet)
Joe Perches6e74e072010-09-24 10:03:00 -07001860 if defined $cc_cmd && !$suppress_cc{'cccmd'};
Joe Perches324a8bd2007-08-17 18:51:12 -07001861
Thomas Rast3cae7e52010-06-17 22:10:39 +02001862 if ($broken_encoding{$t} && !$has_content_type) {
Paolo Bonzinibb294562014-11-25 15:00:26 +01001863 $xfer_encoding = '8bit' if not defined $xfer_encoding;
Thomas Rast3cae7e52010-06-17 22:10:39 +02001864 $has_content_type = 1;
Paolo Bonzinibb294562014-11-25 15:00:26 +01001865 push @xh, "Content-Type: text/plain; charset=$auto_8bit_encoding";
Thomas Rast3cae7e52010-06-17 22:10:39 +02001866 $body_encoding = $auto_8bit_encoding;
1867 }
1868
Krzysztof Mazurce547802012-10-24 23:08:26 +02001869 if ($broken_encoding{$t} && !is_rfc2047_quoted($subject)) {
1870 $subject = quote_subject($subject, $auto_8bit_encoding);
Thomas Rast3cae7e52010-06-17 22:10:39 +02001871 }
1872
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001873 if (defined $sauthor and $sauthor ne $sender) {
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001874 $message = "From: $author\n\n$message";
Jeff King8291db62007-11-16 05:49:09 -05001875 if (defined $author_encoding) {
1876 if ($has_content_type) {
1877 if ($body_encoding eq $author_encoding) {
1878 # ok, we already have the right encoding
1879 }
1880 else {
1881 # uh oh, we should re-encode
1882 }
1883 }
1884 else {
Paolo Bonzinibb294562014-11-25 15:00:26 +01001885 $xfer_encoding = '8bit' if not defined $xfer_encoding;
Thomas Rast3cae7e52010-06-17 22:10:39 +02001886 $has_content_type = 1;
Jeff King8291db62007-11-16 05:49:09 -05001887 push @xh,
Paolo Bonzinibb294562014-11-25 15:00:26 +01001888 "Content-Type: text/plain; charset=$author_encoding";
Jeff King8291db62007-11-16 05:49:09 -05001889 }
1890 }
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001891 }
brian m. carlsone67a2282018-07-08 22:17:12 +00001892 $xfer_encoding = '8bit' if not defined $xfer_encoding;
1893 ($message, $xfer_encoding) = apply_transfer_encoding(
1894 $message, $xfer_encoding, $target_xfer_encoding);
1895 push @xh, "Content-Transfer-Encoding: $xfer_encoding";
1896 unshift @xh, 'MIME-Version: 1.0' unless $has_mime_version;
Ryan Anderson83b24432005-07-31 04:17:25 -04001897
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001898 $needs_confirm = (
1899 $confirm eq "always" or
1900 ($confirm =~ /^(?:auto|cc)$/ && @cc) or
1901 ($confirm =~ /^(?:auto|compose)$/ && $compose && $message_num == 1));
1902 $needs_confirm = "inform" if ($needs_confirm && $confirm_unconfigured && @cc);
1903
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001904 @to = process_address_list(@to);
1905 @cc = process_address_list(@cc);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001906
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001907 @to = (@initial_to, @to);
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001908 @cc = (@initial_cc, @cc);
1909
Michael S. Tsirkinf515c902014-04-29 08:41:16 +03001910 if ($message_num == 1) {
1911 if (defined $cover_cc and $cover_cc) {
1912 @initial_cc = @cc;
1913 }
1914 if (defined $cover_to and $cover_to) {
1915 @initial_to = @to;
1916 }
1917 }
Michael Strawbridge56addda2023-04-19 16:27:02 -04001918}
1919
1920# Prepares the email, prompts the user, and sends it out
1921# Returns 0 if an edit was done and the function should be called again, or 1
1922# on the email being successfully sent out.
1923sub process_file {
1924 my ($t) = @_;
1925
1926 pre_process_file($t, $quiet);
Michael S. Tsirkinf515c902014-04-29 08:41:16 +03001927
Michael Witten15da1082009-04-13 13:23:51 -05001928 my $message_was_sent = send_message();
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001929 if ($message_was_sent == -1) {
1930 do_edit($t);
1931 return 0;
1932 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001933
1934 # set up for the next message
Marvin Häusere0821132021-08-30 15:30:01 +00001935 if ($thread) {
1936 if ($message_was_sent &&
1937 ($chain_reply_to || !defined $in_reply_to || length($in_reply_to) == 0 ||
1938 $message_num == 1)) {
1939 $in_reply_to = $message_id;
1940 if (length $references > 0) {
1941 $references .= "\n $message_id";
1942 } else {
1943 $references = "$message_id";
1944 }
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001945 }
Marvin Häusere0821132021-08-30 15:30:01 +00001946 } elsif (!defined $initial_in_reply_to) {
1947 # --thread and --in-reply-to manage the "In-Reply-To" header and by
1948 # extension the "References" header. If these commands are not used, reset
1949 # the header values to their defaults.
1950 $in_reply_to = undef;
1951 $references = '';
Ryan Anderson78488b22005-07-31 20:04:24 -04001952 }
Jeff King4f3d3702007-12-17 15:51:34 -05001953 $message_id = undef;
xiaoqiang zhao5453b832017-05-21 20:59:50 +08001954 $num_sent++;
1955 if (defined $batch_size && $num_sent == $batch_size) {
1956 $num_sent = 0;
1957 $smtp->quit if defined $smtp;
1958 undef $smtp;
1959 undef $auth;
1960 sleep($relogin_delay) if defined $relogin_delay;
1961 }
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001962
1963 return 1;
1964}
1965
Michael Strawbridge0d864702023-10-25 14:51:29 -04001966sub initialize_modified_loop_vars {
1967 $in_reply_to = $initial_in_reply_to;
1968 $references = $initial_in_reply_to || '';
1969 $message_num = 0;
1970}
1971
1972if ($validate) {
1973 # FIFOs can only be read once, exclude them from validation.
1974 my @real_files = ();
1975 foreach my $f (@files) {
1976 unless (-p $f) {
1977 push(@real_files, $f);
1978 }
1979 }
1980
1981 # Run the loop once again to avoid gaps in the counter due to FIFO
1982 # arguments provided by the user.
1983 my $num = 1;
1984 my $num_files = scalar @real_files;
1985 $ENV{GIT_SENDEMAIL_FILE_TOTAL} = "$num_files";
1986 initialize_modified_loop_vars();
1987 foreach my $r (@real_files) {
1988 $ENV{GIT_SENDEMAIL_FILE_COUNTER} = "$num";
1989 pre_process_file($r, 1);
1990 validate_patch($r, $target_xfer_encoding);
1991 $num += 1;
1992 }
1993 delete $ENV{GIT_SENDEMAIL_FILE_COUNTER};
1994 delete $ENV{GIT_SENDEMAIL_FILE_TOTAL};
1995}
1996
1997initialize_modified_loop_vars();
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001998foreach my $t (@files) {
1999 while (!process_file($t)) {
2000 # user edited the file
2001 }
Ryan Anderson83b24432005-07-31 04:17:25 -04002002}
Ryan Andersone2057352005-08-02 21:45:22 -04002003
Maxim Cournoyer3a7a18a2023-05-01 10:38:48 -04002004# Execute a command and return its output lines as an array. Blank
2005# lines which do not appear at the end of the output are reported as
2006# errors.
Maxim Cournoyer03056ce2023-05-01 10:38:46 -04002007sub execute_cmd {
2008 my ($prefix, $cmd, $file) = @_;
2009 my @lines = ();
Maxim Cournoyer3a7a18a2023-05-01 10:38:48 -04002010 my $seen_blank_line = 0;
Maxim Cournoyer03056ce2023-05-01 10:38:46 -04002011 open my $fh, "-|", "$cmd \Q$file\E"
2012 or die sprintf(__("(%s) Could not execute '%s'"), $prefix, $cmd);
2013 while (my $line = <$fh>) {
Maxim Cournoyer3a7a18a2023-05-01 10:38:48 -04002014 die sprintf(__("(%s) Malformed output from '%s'"), $prefix, $cmd)
2015 if $seen_blank_line;
2016 if ($line =~ /^$/) {
2017 $seen_blank_line = $line =~ /^$/;
2018 next;
2019 }
Maxim Cournoyer03056ce2023-05-01 10:38:46 -04002020 push @lines, $line;
2021 }
2022 close $fh
2023 or die sprintf(__("(%s) failed to close pipe to '%s'"), $prefix, $cmd);
2024 return @lines;
2025}
2026
Maxim Cournoyerba921062023-05-01 10:38:47 -04002027# Process headers lines, unfolding multiline headers as defined by RFC
2028# 2822.
2029sub unfold_headers {
2030 my @headers;
2031 foreach(@_) {
2032 last if /^\s*$/;
2033 if (/^\s+\S/ and @headers) {
2034 chomp($headers[$#headers]);
2035 s/^\s+/ /;
2036 $headers[$#headers] .= $_;
2037 } else {
2038 push(@headers, $_);
2039 }
2040 }
2041 return @headers;
2042}
2043
2044# Invoke the provided CMD with FILE as an argument, which should
2045# output RFC 2822 email headers. Fold multiline headers and return the
2046# headers as an array.
2047sub invoke_header_cmd {
2048 my ($cmd, $file) = @_;
2049 my @lines = execute_cmd("header-cmd", $header_cmd, $file);
2050 return unfold_headers(@lines);
2051}
2052
Joe Perches6e74e072010-09-24 10:03:00 -07002053# Execute a command (e.g. $to_cmd) to get a list of email addresses
2054# and return a results array
2055sub recipients_cmd {
Michael Strawbridge56addda2023-04-19 16:27:02 -04002056 my ($prefix, $what, $cmd, $file, $quiet) = @_;
Maxim Cournoyer03056ce2023-05-01 10:38:46 -04002057 my @lines = ();
Joe Perches6e74e072010-09-24 10:03:00 -07002058 my @addresses = ();
Junio C Hamano64477d22023-05-15 13:59:03 -07002059
Maxim Cournoyer03056ce2023-05-01 10:38:46 -04002060 @lines = execute_cmd($prefix, $cmd, $file);
2061 for my $address (@lines) {
Joe Perches6e74e072010-09-24 10:03:00 -07002062 $address =~ s/^\s*//g;
2063 $address =~ s/\s*$//g;
2064 $address = sanitize_address($address);
Michael S. Tsirkinda187592013-06-05 21:11:00 +03002065 next if ($address eq $sender and $suppress_cc{'self'});
Joe Perches6e74e072010-09-24 10:03:00 -07002066 push @addresses, $address;
Vasco Almeida3c5cd202016-12-14 11:54:37 -01002067 printf(__("(%s) Adding %s: %s from: '%s'\n"),
2068 $prefix, $what, $address, $cmd) unless $quiet;
Joe Perches6e74e072010-09-24 10:03:00 -07002069 }
Joe Perches6e74e072010-09-24 10:03:00 -07002070 return @addresses;
2071}
2072
Jay Soffianc1f2aa42009-03-02 23:52:18 -05002073cleanup_compose_files();
Ryan Anderson1f038a02005-09-05 01:13:07 -04002074
Ævar Arnfjörð Bjarmason4bf597e2010-09-30 13:43:00 +00002075sub cleanup_compose_files {
Jay Soffianc1f2aa42009-03-02 23:52:18 -05002076 unlink($compose_filename, $compose_filename . ".final") if $compose;
Ryan Anderson1f038a02005-09-05 01:13:07 -04002077}
2078
Eric Wong4bc87a22006-03-25 17:20:48 -08002079$smtp->quit if $smtp;
Ryan Andersone2057352005-08-02 21:45:22 -04002080
Paolo Bonzini8d814082014-11-25 15:00:27 +01002081sub apply_transfer_encoding {
2082 my $message = shift;
2083 my $from = shift;
2084 my $to = shift;
2085
Aaron Lindsay3c88e462018-11-02 05:52:38 -04002086 return ($message, $to) if ($from eq $to and $from ne '7bit');
Paolo Bonzini8d814082014-11-25 15:00:27 +01002087
2088 require MIME::QuotedPrint;
2089 require MIME::Base64;
2090
2091 $message = MIME::QuotedPrint::decode($message)
2092 if ($from eq 'quoted-printable');
2093 $message = MIME::Base64::decode($message)
2094 if ($from eq 'base64');
2095
brian m. carlson74d76a12019-04-13 22:45:51 +00002096 $to = ($message =~ /(?:.{999,}|\r)/) ? 'quoted-printable' : '8bit'
brian m. carlson7a369872018-07-08 22:17:10 +00002097 if $to eq 'auto';
2098
Vasco Almeida46493102016-12-14 11:54:36 -01002099 die __("cannot send message as 7bit")
Paolo Bonzini8d814082014-11-25 15:00:27 +01002100 if ($to eq '7bit' and $message =~ /[^[:ascii:]]/);
brian m. carlson7a369872018-07-08 22:17:10 +00002101 return ($message, $to)
Paolo Bonzini8d814082014-11-25 15:00:27 +01002102 if ($to eq '7bit' or $to eq '8bit');
brian m. carlson7a369872018-07-08 22:17:10 +00002103 return (MIME::QuotedPrint::encode($message, "\n", 0), $to)
Paolo Bonzini8d814082014-11-25 15:00:27 +01002104 if ($to eq 'quoted-printable');
brian m. carlson7a369872018-07-08 22:17:10 +00002105 return (MIME::Base64::encode($message, "\n"), $to)
Paolo Bonzini8d814082014-11-25 15:00:27 +01002106 if ($to eq 'base64');
Vasco Almeida46493102016-12-14 11:54:36 -01002107 die __("invalid transfer encoding");
Paolo Bonzini8d814082014-11-25 15:00:27 +01002108}
2109
Ævar Arnfjörð Bjarmasonc438ea22010-09-30 13:42:59 +00002110sub unique_email_list {
Ryan Andersone2057352005-08-02 21:45:22 -04002111 my %seen;
2112 my @emails;
2113
2114 foreach my $entry (@_) {
Krzysztof Mazure4312252012-11-22 19:12:10 +01002115 my $clean = extract_valid_address_or_die($entry);
2116 $seen{$clean} ||= 0;
2117 next if $seen{$clean}++;
2118 push @emails, $entry;
Ryan Andersone2057352005-08-02 21:45:22 -04002119 }
2120 return @emails;
2121}
Jeff King747bbff2008-01-18 09:19:48 -05002122
2123sub validate_patch {
brian m. carlsonf2d06fb2018-07-08 22:17:11 +00002124 my ($fn, $xfer_encoding) = @_;
Jonathan Tan64896602017-05-12 15:38:26 -07002125
Jonathan Tan177409e2017-06-01 16:50:55 -07002126 if ($repo) {
Emily Shaffera7555302021-12-22 04:59:38 +01002127 my $hook_name = 'sendemail-validate';
Ævar Arnfjörð Bjarmason7cbc0452021-05-26 13:21:07 +02002128 my $hooks_path = $repo->command_oneline('rev-parse', '--git-path', 'hooks');
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +02002129 require File::Spec;
Emily Shaffera7555302021-12-22 04:59:38 +01002130 my $validate_hook = File::Spec->catfile($hooks_path, $hook_name);
Jonathan Tan177409e2017-06-01 16:50:55 -07002131 my $hook_error;
2132 if (-x $validate_hook) {
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +02002133 require Cwd;
2134 my $target = Cwd::abs_path($fn);
Jonathan Tan177409e2017-06-01 16:50:55 -07002135 # The hook needs a correct cwd and GIT_DIR.
Ævar Arnfjörð Bjarmason17530b22021-05-28 11:23:52 +02002136 my $cwd_save = Cwd::getcwd();
Jonathan Tan177409e2017-06-01 16:50:55 -07002137 chdir($repo->wc_path() or $repo->repo_path())
2138 or die("chdir: $!");
2139 local $ENV{"GIT_DIR"} = $repo->repo_path();
Michael Strawbridgea8022c52023-04-19 16:27:03 -04002140
2141 my ($recipients_ref, $to, $date, $gitversion, $cc, $ccline, $header) = gen_header();
2142
2143 require File::Temp;
2144 my ($header_filehandle, $header_filename) = File::Temp::tempfile(
2145 TEMPLATE => ".gitsendemail.header.XXXXXX",
2146 DIR => $repo->repo_path(),
2147 UNLINK => 1,
2148 );
2149 print $header_filehandle $header;
2150
Emily Shaffera7555302021-12-22 04:59:38 +01002151 my @cmd = ("git", "hook", "run", "--ignore-missing",
2152 $hook_name, "--");
Michael Strawbridgea8022c52023-04-19 16:27:03 -04002153 my @cmd_msg = (@cmd, "<patch>", "<header>");
2154 my @cmd_run = (@cmd, $target, $header_filename);
Emily Shaffera7555302021-12-22 04:59:38 +01002155 $hook_error = system_or_msg(\@cmd_run, undef, "@cmd_msg");
Jonathan Tan177409e2017-06-01 16:50:55 -07002156 chdir($cwd_save) or die("chdir: $!");
2157 }
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +02002158 if ($hook_error) {
Jean-Noël Avilaaf15f842022-04-11 19:23:30 +00002159 $hook_error = sprintf(
2160 __("fatal: %s: rejected by %s hook\n%s\nwarning: no patches were sent\n"),
2161 $fn, $hook_name, $hook_error);
Emily Shaffera7555302021-12-22 04:59:38 +01002162 die $hook_error;
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +02002163 }
Jonathan Tan64896602017-05-12 15:38:26 -07002164 }
Jonathan Tan64896602017-05-12 15:38:26 -07002165
brian m. carlsonf2d06fb2018-07-08 22:17:11 +00002166 # Any long lines will be automatically fixed if we use a suitable transfer
2167 # encoding.
2168 unless ($xfer_encoding =~ /^(?:auto|quoted-printable|base64)$/) {
2169 open(my $fh, '<', $fn)
2170 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
2171 while (my $line = <$fh>) {
2172 if (length($line) > 998) {
Ævar Arnfjörð Bjarmasonea7811b2021-04-06 16:00:37 +02002173 die sprintf(__("fatal: %s:%d is longer than 998 characters\n" .
2174 "warning: no patches were sent\n"), $fn, $.);
brian m. carlsonf2d06fb2018-07-08 22:17:11 +00002175 }
Jeff King747bbff2008-01-18 09:19:48 -05002176 }
2177 }
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -07002178 return;
Jeff King747bbff2008-01-18 09:19:48 -05002179}
Jeff King0706bd12008-03-28 17:28:33 -04002180
Junio C Hamano531220b2016-03-17 22:40:05 -07002181sub handle_backup {
2182 my ($last, $lastlen, $file, $known_suffix) = @_;
2183 my ($suffix, $skip);
2184
2185 $skip = 0;
2186 if (defined $last &&
2187 ($lastlen < length($file)) &&
2188 (substr($file, 0, $lastlen) eq $last) &&
2189 ($suffix = substr($file, $lastlen)) !~ /^[a-z0-9]/i) {
2190 if (defined $known_suffix && $suffix eq $known_suffix) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01002191 printf(__("Skipping %s with backup suffix '%s'.\n"), $file, $known_suffix);
Junio C Hamano531220b2016-03-17 22:40:05 -07002192 $skip = 1;
2193 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01002194 # TRANSLATORS: please keep "[y|N]" as is.
2195 my $answer = ask(sprintf(__("Do you really want to send %s? [y|N]: "), $file),
Junio C Hamano531220b2016-03-17 22:40:05 -07002196 valid_re => qr/^(?:y|n)/i,
2197 default => 'n');
2198 $skip = ($answer ne 'y');
2199 if ($skip) {
2200 $known_suffix = $suffix;
2201 }
2202 }
2203 }
2204 return ($skip, $known_suffix);
2205}
2206
2207sub handle_backup_files {
2208 my @file = @_;
2209 my ($last, $lastlen, $known_suffix, $skip, @result);
2210 for my $file (@file) {
2211 ($skip, $known_suffix) = handle_backup($last, $lastlen,
2212 $file, $known_suffix);
2213 push @result, $file unless $skip;
2214 $last = $file;
2215 $lastlen = length($file);
2216 }
2217 return @result;
2218}
2219
Jeff King0706bd12008-03-28 17:28:33 -04002220sub file_has_nonascii {
2221 my $fn = shift;
2222 open(my $fh, '<', $fn)
Vasco Almeida3c5cd202016-12-14 11:54:37 -01002223 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
Jeff King0706bd12008-03-28 17:28:33 -04002224 while (my $line = <$fh>) {
2225 return 1 if $line =~ /[^[:ascii:]]/;
2226 }
2227 return 0;
2228}
Thomas Rast3cae7e52010-06-17 22:10:39 +02002229
2230sub body_or_subject_has_nonascii {
2231 my $fn = shift;
2232 open(my $fh, '<', $fn)
Vasco Almeida3c5cd202016-12-14 11:54:37 -01002233 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
Thomas Rast3cae7e52010-06-17 22:10:39 +02002234 while (my $line = <$fh>) {
2235 last if $line =~ /^$/;
2236 return 1 if $line =~ /^Subject.*[^[:ascii:]]/;
2237 }
2238 while (my $line = <$fh>) {
2239 return 1 if $line =~ /[^[:ascii:]]/;
2240 }
2241 return 0;
2242}