blob: 10c450ef68902c7dfcc2289acf72319f38f37346 [file] [log] [blame]
Ævar Arnfjörð Bjarmason3328ace2010-09-24 20:00:53 +00001#!/usr/bin/perl
Ryan Anderson83b24432005-07-31 04:17:25 -04002#
Ryan Andersonf3d9f352005-07-31 20:04:24 -04003# Copyright 2002,2005 Greg Kroah-Hartman <greg@kroah.com>
4# Copyright 2005 Ryan Anderson <ryan@michonline.com>
Ryan Anderson83b24432005-07-31 04:17:25 -04005#
6# GPL v2 (See COPYING)
Junio C Hamano5825e5b2005-07-31 23:05:16 -07007#
Ryan Anderson83b24432005-07-31 04:17:25 -04008# Ported to support git "mbox" format files by Ryan Anderson <ryan@michonline.com>
9#
Ryan Andersonf3d9f352005-07-31 20:04:24 -040010# Sends a collection of emails to the given email addresses, disturbingly fast.
Junio C Hamano5825e5b2005-07-31 23:05:16 -070011#
Ryan Andersonf3d9f352005-07-31 20:04:24 -040012# Supports two formats:
13# 1. mbox format files (ignoring most headers and MIME formatting - this is designed for sending patches)
14# 2. The original format support by Greg's script:
Junio C Hamano5825e5b2005-07-31 23:05:16 -070015# first line of the message is who to CC,
Ryan Andersonf3d9f352005-07-31 20:04:24 -040016# and second line is the subject of the message.
Junio C Hamano5825e5b2005-07-31 23:05:16 -070017#
Ryan Anderson83b24432005-07-31 04:17:25 -040018
Ævar Arnfjörð Bjarmasond48b2842010-09-24 20:00:52 +000019use 5.008;
Ryan Anderson83b24432005-07-31 04:17:25 -040020use strict;
Æ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
Junio C Hamano280242d2006-07-02 16:03:59 -070029package FakeTerm;
30sub new {
31 my ($class, $reason) = @_;
32 return bless \$reason, shift;
33}
34sub readline {
35 my $self = shift;
36 die "Cannot use readline on FakeTerm: $$self";
37}
38package main;
39
Michael Coleman1b0baf12007-02-27 22:47:54 -060040
41sub usage {
42 print <<EOT;
Thiago Perrottaa2ce6082021-10-25 17:27:07 -040043git send-email' [<options>] <file|directory>
44git send-email' [<options>] <format-patch options>
Jacob Keller17b7a832015-11-19 14:52:11 -080045git send-email --dump-aliases
Michael Witten4ed62b02008-09-30 07:58:30 -050046
47 Composing:
48 --from <str> * Email From:
Stephen Boydf434c082010-03-07 14:46:48 -080049 --[no-]to <str> * Email To:
50 --[no-]cc <str> * Email Cc:
51 --[no-]bcc <str> * Email Bcc:
Michael Witten4ed62b02008-09-30 07:58:30 -050052 --subject <str> * Email "Subject:"
Christian Ludwigd11c9432018-03-04 00:58:14 +010053 --reply-to <str> * Email "Reply-To:"
Michael Witten4ed62b02008-09-30 07:58:30 -050054 --in-reply-to <str> * Email "In-Reply-To:"
Luis Henriquesac1596a2014-03-24 21:38:27 +000055 --[no-]xmailer * Add "X-Mailer:" header (default).
Felipe Contreras402596a2013-04-07 01:10:27 -060056 --[no-]annotate * Review each patch that will be sent in an editor.
Michael Witten4ed62b02008-09-30 07:58:30 -050057 --compose * Open an editor for introduction.
Krzysztof Mazur62e00692012-10-10 01:02:56 +020058 --compose-encoding <str> * Encoding to assume for introduction.
Thomas Rast3cae7e52010-06-17 22:10:39 +020059 --8bit-encoding <str> * Encoding to assume 8bit mails if undeclared
Paolo Bonzini8d814082014-11-25 15:00:27 +010060 --transfer-encoding <str> * Transfer encoding to use (quoted-printable, 8bit, base64)
Michael Witten4ed62b02008-09-30 07:58:30 -050061
62 Sending:
63 --envelope-sender <str> * Email envelope sender.
Gregory Anderscd5b33f2021-05-14 09:15:53 -060064 --sendmail-cmd <str> * Command to run to send email.
Michael Witten4ed62b02008-09-30 07:58:30 -050065 --smtp-server <str:int> * Outgoing SMTP server to use. The port
66 is optional. Default 'localhost'.
Pascal Obry052fbea2010-09-06 20:12:11 +020067 --smtp-server-option <str> * Outgoing SMTP server option to use.
Michael Witten4ed62b02008-09-30 07:58:30 -050068 --smtp-server-port <int> * Outgoing SMTP server port.
69 --smtp-user <str> * Username for SMTP-AUTH.
70 --smtp-pass <str> * Password for SMTP-AUTH; not necessary.
71 --smtp-encryption <str> * tls or ssl; anything else disables.
72 --smtp-ssl * Deprecated. Use '--smtp-encryption ssl'.
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -070073 --smtp-ssl-cert-path <str> * Path to ca-certificates (either directory or file).
74 Pass an empty string to disable certificate
75 verification.
Jari Aalto134550f2010-03-14 17:16:45 +020076 --smtp-domain <str> * The domain name sent to HELO/EHLO handshake
Joshua Watt8dd9b3f2018-10-22 22:24:25 -050077 --smtp-auth <str> * Space-separated list of allowed AUTH mechanisms, or
78 "none" to disable authentication.
Jan Viktorin0f2e68b2015-08-12 01:39:44 +020079 This setting forces to use one of the listed mechanisms.
Joshua Watt8dd9b3f2018-10-22 22:24:25 -050080 --no-smtp-auth Disable SMTP authentication. Shorthand for
81 `--smtp-auth=none`
Jari Aaltof60812e2010-03-14 17:16:09 +020082 --smtp-debug <0|1> * Disable, enable Net::SMTP debug.
Michael Witten4ed62b02008-09-30 07:58:30 -050083
xiaoqiang zhao5453b832017-05-21 20:59:50 +080084 --batch-size <int> * send max <int> message per connection.
85 --relogin-delay <int> * delay <int> seconds between two successive login.
86 This option can only be used with --batch-size
87
Michael Witten4ed62b02008-09-30 07:58:30 -050088 Automating:
89 --identity <str> * Use the sendemail.<id> options.
Joe Perches6e74e072010-09-24 10:03:00 -070090 --to-cmd <str> * Email To: via `<str> \$patch_path`
Michael Witten4ed62b02008-09-30 07:58:30 -050091 --cc-cmd <str> * Email Cc: via `<str> \$patch_path`
Rasmus Villemoesef0cc1d2018-10-16 09:39:23 +020092 --suppress-cc <str> * author, self, sob, cc, cccmd, body, bodycc, misc-by, all.
Michael S. Tsirkinf515c902014-04-29 08:41:16 +030093 --[no-]cc-cover * Email Cc: addresses in the cover letter.
94 --[no-]to-cover * Email To: addresses in the cover letter.
Jay Soffian3531e272009-02-14 23:32:15 -050095 --[no-]signed-off-by-cc * Send to Signed-off-by: addresses. Default on.
Michael Witten4ed62b02008-09-30 07:58:30 -050096 --[no-]suppress-from * Send to self. Default off.
Junio C Hamano41fe87f2009-08-22 12:48:48 -070097 --[no-]chain-reply-to * Chain In-Reply-To: fields. Default off.
Michael Witten4ed62b02008-09-30 07:58:30 -050098 --[no-]thread * Use In-Reply-To: field. Default on.
99
100 Administering:
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500101 --confirm <str> * Confirm recipients before sending;
102 auto, cc, compose, always, or never.
Michael Witten4ed62b02008-09-30 07:58:30 -0500103 --quiet * Output one line of info per email.
104 --dry-run * Don't actually send the emails.
105 --[no-]validate * Perform patch sanity checks. Default on.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100106 --[no-]format-patch * understand any non optional arguments as
107 `git format-patch` ones.
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200108 --force * Send even if safety checks would prevent it.
Jeff Kingc764a0c2008-01-18 09:20:10 -0500109
Jacob Keller17b7a832015-11-19 14:52:11 -0800110 Information:
111 --dump-aliases * Dump configured aliases and exit.
112
Michael Coleman1b0baf12007-02-27 22:47:54 -0600113EOT
114 exit(1);
115}
116
Thiago Perrotta2b7b7582021-10-25 17:27:06 -0400117sub uniq {
118 my %seen;
119 grep !$seen{$_}++, @_;
120}
121
Duy Nguyen13374982018-11-03 07:03:18 +0100122sub completion_helper {
Thiago Perrotta2b7b7582021-10-25 17:27:06 -0400123 my ($original_opts) = @_;
124 my %not_for_completion = (
125 "git-completion-helper" => undef,
126 "h" => undef,
127 );
128 my @send_email_opts = ();
129
130 foreach my $key (keys %$original_opts) {
131 unless (exists $not_for_completion{$key}) {
132 $key =~ s/!$//;
133
134 if ($key =~ /[:=][si]$/) {
135 $key =~ s/[:=][si]$//;
136 push (@send_email_opts, "--$_=") foreach (split (/\|/, $key));
137 } else {
138 push (@send_email_opts, "--$_") foreach (split (/\|/, $key));
139 }
140 }
141 }
142
143 my @format_patch_opts = split(/ /, Git::command('format-patch', '--git-completion-helper'));
144 my @opts = (@send_email_opts, @format_patch_opts);
145 @opts = uniq (grep !/^$/, @opts);
146 # There's an implicit '\n' here already, no need to add an explicit one.
147 print "@opts";
148 exit(0);
Duy Nguyen13374982018-11-03 07:03:18 +0100149}
150
Eric Wong4bc87a22006-03-25 17:20:48 -0800151# most mail servers generate the Date: header, but not all...
Jakub Narebski6bdca892006-07-07 20:57:55 +0200152sub format_2822_time {
153 my ($time) = @_;
154 my @localtm = localtime($time);
155 my @gmttm = gmtime($time);
156 my $localmin = $localtm[1] + $localtm[2] * 60;
157 my $gmtmin = $gmttm[1] + $gmttm[2] * 60;
158 if ($localtm[0] != $gmttm[0]) {
Vasco Almeida46493102016-12-14 11:54:36 -0100159 die __("local zone differs from GMT by a non-minute interval\n");
Jakub Narebski6bdca892006-07-07 20:57:55 +0200160 }
161 if ((($gmttm[6] + 1) % 7) == $localtm[6]) {
162 $localmin += 1440;
163 } elsif ((($gmttm[6] - 1) % 7) == $localtm[6]) {
164 $localmin -= 1440;
165 } elsif ($gmttm[6] != $localtm[6]) {
Vasco Almeida46493102016-12-14 11:54:36 -0100166 die __("local time offset greater than or equal to 24 hours\n");
Jakub Narebski6bdca892006-07-07 20:57:55 +0200167 }
168 my $offset = $localmin - $gmtmin;
169 my $offhour = $offset / 60;
170 my $offmin = abs($offset % 60);
171 if (abs($offhour) >= 24) {
Vasco Almeida46493102016-12-14 11:54:36 -0100172 die __("local time offset greater than or equal to 24 hours\n");
Jakub Narebski6bdca892006-07-07 20:57:55 +0200173 }
174
175 return sprintf("%s, %2d %s %d %02d:%02d:%02d %s%02d%02d",
176 qw(Sun Mon Tue Wed Thu Fri Sat)[$localtm[6]],
177 $localtm[3],
178 qw(Jan Feb Mar Apr May Jun
179 Jul Aug Sep Oct Nov Dec)[$localtm[4]],
180 $localtm[5]+1900,
181 $localtm[2],
182 $localtm[1],
183 $localtm[0],
184 ($offset >= 0) ? '+' : '-',
185 abs($offhour),
186 $offmin,
187 );
188}
Eric Wong4bc87a22006-03-25 17:20:48 -0800189
190my $smtp;
Wincent Colaiuta5f5b6112007-11-21 13:35:05 +0100191my $auth;
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800192my $num_sent = 0;
Eric Wong4bc87a22006-03-25 17:20:48 -0800193
Роман Донченко11f70a72014-12-14 18:59:46 +0300194# Regexes for RFC 2047 productions.
195my $re_token = qr/[^][()<>@,;:\\"\/?.= \000-\037\177-\377]+/;
196my $re_encoded_text = qr/[^? \000-\037\177-\377]+/;
197my $re_encoded_word = qr/=\?($re_token)\?($re_token)\?($re_encoded_text)\?=/;
198
Ryan Anderson83b24432005-07-31 04:17:25 -0400199# Variables we fill in automatically, or via prompting:
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200200my (@to,@cc,@xh,$envelope_sender,
Christian Ludwigd11c9432018-03-04 00:58:14 +0100201 $initial_in_reply_to,$reply_to,$initial_subject,@files,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200202 $author,$sender,$smtp_authpass,$annotate,$compose,$time);
203# Things we either get from config, *or* are overridden on the
204# command-line.
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200205my ($no_cc, $no_to, $no_bcc, $no_identity);
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200206my (@config_to, @getopt_to);
207my (@config_cc, @getopt_cc);
208my (@config_bcc, @getopt_bcc);
Ryan Anderson78488b22005-07-31 20:04:24 -0400209
Ryan Anderson91332612005-07-31 20:04:24 -0400210# Example reply to:
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100211#$initial_in_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
Ryan Anderson83b24432005-07-31 04:17:25 -0400212
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100213my $repo = eval { Git->repository() };
214my @repo = $repo ? ($repo) : ();
Ryan Anderson83b24432005-07-31 04:17:25 -0400215
Adam Roben5483c712007-06-27 20:59:37 -0700216# Behavior modification variables
217my ($quiet, $dry_run) = (0, 0);
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100218my $format_patch;
Jay Soffianafe756c2009-02-23 13:51:37 -0500219my $compose_filename;
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200220my $force = 0;
Jacob Keller17b7a832015-11-19 14:52:11 -0800221my $dump_aliases = 0;
Adam Roben5483c712007-06-27 20:59:37 -0700222
Kyle Meyer8774aa52022-11-26 15:21:23 -0500223# Variables to prevent short format-patch options from being captured
224# as abbreviated send-email options
225my $reroll_count;
226
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100227# Handle interactive edition of files.
228my $multiedit;
Michael J Gruber0ce142c2010-03-22 17:12:53 +0100229my $editor;
Jonathan Niederb4479f02009-10-30 20:42:34 -0500230
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +0200231sub system_or_msg {
Emily Shaffera7555302021-12-22 04:59:38 +0100232 my ($args, $msg, $cmd_name) = @_;
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +0200233 system(@$args);
234 my $signalled = $? & 127;
235 my $exit_code = $? >> 8;
236 return unless $signalled or $exit_code;
237
Emily Shaffera7555302021-12-22 04:59:38 +0100238 my @sprintf_args = ($cmd_name ? $cmd_name : $args->[0], $exit_code);
Ævar Arnfjörð Bjarmason5b719b72021-05-25 01:14:24 +0200239 if (defined $msg) {
240 # Quiet the 'redundant' warning category, except we
241 # need to support down to Perl 5.8, so we can't do a
242 # "no warnings 'redundant'", since that category was
243 # introduced in perl 5.22, and asking for it will die
244 # on older perls.
245 no warnings;
246 return sprintf($msg, @sprintf_args);
247 }
Ævar Arnfjörð Bjarmasonea7811b2021-04-06 16:00:37 +0200248 return sprintf(__("fatal: command '%s' died with exit code %d"),
Ævar Arnfjörð Bjarmason5b719b72021-05-25 01:14:24 +0200249 @sprintf_args);
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +0200250}
251
252sub system_or_die {
253 my $msg = system_or_msg(@_);
254 die $msg if $msg;
255}
256
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100257sub do_edit {
Michael J Gruber0ce142c2010-03-22 17:12:53 +0100258 if (!defined($editor)) {
259 $editor = Git::command_oneline('var', 'GIT_EDITOR');
260 }
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +0200261 my $die_msg = __("the editor exited uncleanly, aborting everything");
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100262 if (defined($multiedit) && !$multiedit) {
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +0200263 system_or_die(['sh', '-c', $editor.' "$@"', $editor, $_], $die_msg) for @_;
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100264 } else {
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +0200265 system_or_die(['sh', '-c', $editor.' "$@"', $editor, @_], $die_msg);
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100266 }
267}
Adam Roben5483c712007-06-27 20:59:37 -0700268
269# Variables with corresponding config settings
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200270my ($suppress_from, $signed_off_by_cc);
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300271my ($cover_cc, $cover_to);
Joe Perches6e74e072010-09-24 10:03:00 -0700272my ($to_cmd, $cc_cmd);
Pascal Obry052fbea2010-09-06 20:12:11 +0200273my ($smtp_server, $smtp_server_port, @smtp_server_options);
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -0700274my ($smtp_authuser, $smtp_encryption, $smtp_ssl_cert_path);
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800275my ($batch_size, $relogin_delay);
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200276my ($identity, $aliasfiletype, @alias_files, $smtp_domain, $smtp_auth);
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200277my ($confirm);
David Brown65648282007-12-25 19:56:29 -0800278my (@suppress_cc);
Thomas Rast3cae7e52010-06-17 22:10:39 +0200279my ($auto_8bit_encoding);
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200280my ($compose_encoding);
Gregory Anderscd5b33f2021-05-14 09:15:53 -0600281my ($sendmail_cmd);
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200282# Variables with corresponding config settings & hardcoded defaults
283my ($debug_net_smtp) = 0; # Net::SMTP, see send_message()
284my $thread = 1;
285my $chain_reply_to = 0;
286my $use_xmailer = 1;
287my $validate = 1;
brian m. carlsone67a2282018-07-08 22:17:12 +0000288my $target_xfer_encoding = 'auto';
Drew DeVaultdd84e522020-07-23 20:44:32 -0400289my $forbid_sendmail_variables = 1;
Adam Roben5483c712007-06-27 20:59:37 -0700290
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900291my %config_bool_settings = (
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200292 "thread" => \$thread,
293 "chainreplyto" => \$chain_reply_to,
294 "suppressfrom" => \$suppress_from,
295 "signedoffbycc" => \$signed_off_by_cc,
296 "cccover" => \$cover_cc,
297 "tocover" => \$cover_to,
298 "signedoffcc" => \$signed_off_by_cc,
299 "validate" => \$validate,
300 "multiedit" => \$multiedit,
301 "annotate" => \$annotate,
302 "xmailer" => \$use_xmailer,
Drew DeVaultdd84e522020-07-23 20:44:32 -0400303 "forbidsendmailvariables" => \$forbid_sendmail_variables,
Adam Robene46f7a02007-06-26 15:48:30 -0700304);
305
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900306my %config_settings = (
Ævar Arnfjörð Bjarmason119974e2021-05-28 11:23:43 +0200307 "smtpencryption" => \$smtp_encryption,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900308 "smtpserver" => \$smtp_server,
Junio C Hamano44b24762007-09-25 17:27:54 -0700309 "smtpserverport" => \$smtp_server_port,
Pascal Obry052fbea2010-09-06 20:12:11 +0200310 "smtpserveroption" => \@smtp_server_options,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900311 "smtpuser" => \$smtp_authuser,
312 "smtppass" => \$smtp_authpass,
Pascal Obrye1e91152010-09-06 20:12:09 +0200313 "smtpdomain" => \$smtp_domain,
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200314 "smtpauth" => \$smtp_auth,
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800315 "smtpbatchsize" => \$batch_size,
316 "smtprelogindelay" => \$relogin_delay,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200317 "to" => \@config_to,
Joe Perches6e74e072010-09-24 10:03:00 -0700318 "tocmd" => \$to_cmd,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200319 "cc" => \@config_cc,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900320 "cccmd" => \$cc_cmd,
321 "aliasfiletype" => \$aliasfiletype,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200322 "bcc" => \@config_bcc,
David Brown65648282007-12-25 19:56:29 -0800323 "suppresscc" => \@suppress_cc,
Ask Bjørn Hansen9f7820a2008-06-07 00:33:42 -0700324 "envelopesender" => \$envelope_sender,
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500325 "confirm" => \$confirm,
Trent Piepho09caa242009-05-12 15:48:56 -0700326 "from" => \$sender,
Thomas Rast3cae7e52010-06-17 22:10:39 +0200327 "assume8bitencoding" => \$auto_8bit_encoding,
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200328 "composeencoding" => \$compose_encoding,
Paolo Bonzini8d814082014-11-25 15:00:27 +0100329 "transferencoding" => \$target_xfer_encoding,
Gregory Anderscd5b33f2021-05-14 09:15:53 -0600330 "sendmailcmd" => \$sendmail_cmd,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900331);
Avi Kivity4a62d3f2007-03-11 19:19:44 +0200332
Cord Seelecec5dae2011-09-30 12:52:25 +0200333my %config_path_settings = (
334 "aliasesfile" => \@alias_files,
John Keeping6e07a3b2015-11-17 22:01:05 +0000335 "smtpsslcertpath" => \$smtp_ssl_cert_path,
Cord Seelecec5dae2011-09-30 12:52:25 +0200336);
337
Michael Witten87429972008-02-03 19:53:57 -0500338# Handle Uncouth Termination
339sub signal_handler {
Michael Witten87429972008-02-03 19:53:57 -0500340 # Make text normal
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200341 require Term::ANSIColor;
342 print Term::ANSIColor::color("reset"), "\n";
Michael Witten87429972008-02-03 19:53:57 -0500343
344 # SMTP password masked
345 system "stty echo";
346
347 # tmp files from --compose
Jay Soffianafe756c2009-02-23 13:51:37 -0500348 if (defined $compose_filename) {
349 if (-e $compose_filename) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100350 printf __("'%s' contains an intermediate version ".
351 "of the email you were composing.\n"),
352 $compose_filename;
Jay Soffianafe756c2009-02-23 13:51:37 -0500353 }
354 if (-e ($compose_filename . ".final")) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100355 printf __("'%s.final' contains the composed email.\n"),
356 $compose_filename;
Jay Soffianafe756c2009-02-23 13:51:37 -0500357 }
Michael Witten87429972008-02-03 19:53:57 -0500358 }
359
360 exit;
361};
362
363$SIG{TERM} = \&signal_handler;
364$SIG{INT} = \&signal_handler;
365
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200366# Read our sendemail.* config
367sub read_config {
Ævar Arnfjörð Bjarmason9264d292021-05-28 11:23:45 +0200368 my ($known_keys, $configured, $prefix) = @_;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200369
370 foreach my $setting (keys %config_bool_settings) {
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200371 my $target = $config_bool_settings{$setting};
Ævar Arnfjörð Bjarmason9264d292021-05-28 11:23:45 +0200372 my $key = "$prefix.$setting";
373 next unless exists $known_keys->{$key};
Ævar Arnfjörð Bjarmasonc95e3a32021-05-28 11:23:51 +0200374 my $v = (@{$known_keys->{$key}} == 1 &&
375 (defined $known_keys->{$key}->[0] &&
376 $known_keys->{$key}->[0] =~ /^(?:true|false)$/s))
377 ? $known_keys->{$key}->[0] eq 'true'
378 : Git::config_bool(@repo, $key);
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200379 next unless defined $v;
380 next if $configured->{$setting}++;
381 $$target = $v;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200382 }
383
384 foreach my $setting (keys %config_path_settings) {
385 my $target = $config_path_settings{$setting};
Ævar Arnfjörð Bjarmason9264d292021-05-28 11:23:45 +0200386 my $key = "$prefix.$setting";
387 next unless exists $known_keys->{$key};
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200388 if (ref($target) eq "ARRAY") {
Ævar Arnfjörð Bjarmason9264d292021-05-28 11:23:45 +0200389 my @values = Git::config_path(@repo, $key);
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200390 next unless @values;
391 next if $configured->{$setting}++;
392 @$target = @values;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200393 }
394 else {
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200395 my $v = Git::config_path(@repo, "$prefix.$setting");
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200396 next unless defined $v;
397 next if $configured->{$setting}++;
398 $$target = $v;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200399 }
400 }
401
402 foreach my $setting (keys %config_settings) {
403 my $target = $config_settings{$setting};
Ævar Arnfjörð Bjarmason9264d292021-05-28 11:23:45 +0200404 my $key = "$prefix.$setting";
405 next unless exists $known_keys->{$key};
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200406 if (ref($target) eq "ARRAY") {
Ævar Arnfjörð Bjarmasonc95e3a32021-05-28 11:23:51 +0200407 my @values = @{$known_keys->{$key}};
408 @values = grep { defined } @values;
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200409 next if $configured->{$setting}++;
410 @$target = @values;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200411 }
412 else {
Ævar Arnfjörð Bjarmasonb996f842021-09-06 09:33:29 +0200413 my $v = $known_keys->{$key}->[-1];
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200414 next unless defined $v;
415 next if $configured->{$setting}++;
416 $$target = $v;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200417 }
418 }
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200419}
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200420
Ævar Arnfjörð Bjarmason2b110e92021-05-28 11:23:44 +0200421sub config_regexp {
422 my ($regex) = @_;
423 my @ret;
424 eval {
Ævar Arnfjörð Bjarmasonc95e3a32021-05-28 11:23:51 +0200425 my $ret = Git::command(
Ævar Arnfjörð Bjarmason2b110e92021-05-28 11:23:44 +0200426 'config',
Ævar Arnfjörð Bjarmasonc95e3a32021-05-28 11:23:51 +0200427 '--null',
Ævar Arnfjörð Bjarmason2b110e92021-05-28 11:23:44 +0200428 '--get-regexp',
429 $regex,
430 );
Ævar Arnfjörð Bjarmasonc95e3a32021-05-28 11:23:51 +0200431 @ret = map {
432 # We must always return ($k, $v) here, since
433 # empty config values will be just "key\0",
434 # not "key\nvalue\0".
435 my ($k, $v) = split /\n/, $_, 2;
436 ($k, $v);
437 } split /\0/, $ret;
Ævar Arnfjörð Bjarmason2b110e92021-05-28 11:23:44 +0200438 1;
439 } or do {
440 # If we have no keys we're OK, otherwise re-throw
441 die $@ if $@->value != 1;
442 };
443 return @ret;
444}
445
Ævar Arnfjörð Bjarmason9264d292021-05-28 11:23:45 +0200446# Save ourselves a lot of work of shelling out to 'git config' (it
447# parses 'bool' etc.) by only doing so for config keys that exist.
448my %known_config_keys;
449{
Ævar Arnfjörð Bjarmasonc95e3a32021-05-28 11:23:51 +0200450 my @kv = config_regexp("^sende?mail[.]");
451 while (my ($k, $v) = splice @kv, 0, 2) {
452 push @{$known_config_keys{$k}} => $v;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200453 }
454}
455
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200456# sendemail.identity yields to --identity. We must parse this
457# special-case first before the rest of the config is read.
Ævar Arnfjörð Bjarmason9264d292021-05-28 11:23:45 +0200458{
459 my $key = "sendemail.identity";
460 $identity = Git::config(@repo, $key) if exists $known_config_keys{$key};
461}
Thiago Perrotta2b7b7582021-10-25 17:27:06 -0400462my %identity_options = (
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200463 "identity=s" => \$identity,
464 "no-identity" => \$no_identity,
465);
Thiago Perrotta2b7b7582021-10-25 17:27:06 -0400466my $rc = GetOptions(%identity_options);
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200467usage() unless $rc;
468undef $identity if $no_identity;
469
470# Now we know enough to read the config
471{
472 my %configured;
Ævar Arnfjörð Bjarmason9264d292021-05-28 11:23:45 +0200473 read_config(\%known_config_keys, \%configured, "sendemail.$identity") if defined $identity;
474 read_config(\%known_config_keys, \%configured, "sendemail");
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200475}
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200476
Ryan Anderson83b24432005-07-31 04:17:25 -0400477# Begin by accumulating all the variables (defined above), that we will end up
478# needing, first, from the command line:
479
Clemens Buchacherc5978242011-09-03 19:06:13 +0200480my $help;
Duy Nguyen13374982018-11-03 07:03:18 +0100481my $git_completion_helper;
Thiago Perrotta2b7b7582021-10-25 17:27:06 -0400482my %dump_aliases_options = (
483 "h" => \$help,
484 "dump-aliases" => \$dump_aliases,
485);
486$rc = GetOptions(%dump_aliases_options);
Jacob Keller17b7a832015-11-19 14:52:11 -0800487usage() unless $rc;
Vasco Almeida46493102016-12-14 11:54:36 -0100488die __("--dump-aliases incompatible with other options\n")
Jacob Keller17b7a832015-11-19 14:52:11 -0800489 if !$help and $dump_aliases and @ARGV;
Thiago Perrotta2b7b7582021-10-25 17:27:06 -0400490my %options = (
Clemens Buchacherc5978242011-09-03 19:06:13 +0200491 "sender|from=s" => \$sender,
Thiago Perrotta2b7b7582021-10-25 17:27:06 -0400492 "in-reply-to=s" => \$initial_in_reply_to,
Christian Ludwigd11c9432018-03-04 00:58:14 +0100493 "reply-to=s" => \$reply_to,
Ryan Anderson83b24432005-07-31 04:17:25 -0400494 "subject=s" => \$initial_subject,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200495 "to=s" => \@getopt_to,
Joe Perches6e74e072010-09-24 10:03:00 -0700496 "to-cmd=s" => \$to_cmd,
Stephen Boydf434c082010-03-07 14:46:48 -0800497 "no-to" => \$no_to,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200498 "cc=s" => \@getopt_cc,
Stephen Boydf434c082010-03-07 14:46:48 -0800499 "no-cc" => \$no_cc,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200500 "bcc=s" => \@getopt_bcc,
Stephen Boydf434c082010-03-07 14:46:48 -0800501 "no-bcc" => \$no_bcc,
Ryan Anderson78488b22005-07-31 20:04:24 -0400502 "chain-reply-to!" => \$chain_reply_to,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800503 "no-chain-reply-to" => sub {$chain_reply_to = 0},
Gregory Anderscd5b33f2021-05-14 09:15:53 -0600504 "sendmail-cmd=s" => \$sendmail_cmd,
Ryan Anderson3342d852005-07-31 20:04:24 -0400505 "smtp-server=s" => \$smtp_server,
Pascal Obry052fbea2010-09-06 20:12:11 +0200506 "smtp-server-option=s" => \@smtp_server_options,
Junio C Hamano44b24762007-09-25 17:27:54 -0700507 "smtp-server-port=s" => \$smtp_server_port,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900508 "smtp-user=s" => \$smtp_authuser,
Michael Witten2363d742008-02-03 19:53:56 -0500509 "smtp-pass:s" => \$smtp_authpass,
Thomas Rastf6bebd12008-06-25 21:42:43 +0200510 "smtp-ssl" => sub { $smtp_encryption = 'ssl' },
511 "smtp-encryption=s" => \$smtp_encryption,
Thomas Rast979e6522013-12-01 23:48:42 +0100512 "smtp-ssl-cert-path=s" => \$smtp_ssl_cert_path,
Jari Aaltof60812e2010-03-14 17:16:09 +0200513 "smtp-debug:i" => \$debug_net_smtp,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -0400514 "smtp-domain:s" => \$smtp_domain,
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200515 "smtp-auth=s" => \$smtp_auth,
Joshua Watt8dd9b3f2018-10-22 22:24:25 -0500516 "no-smtp-auth" => sub {$smtp_auth = 'none'},
Felipe Contreras402596a2013-04-07 01:10:27 -0600517 "annotate!" => \$annotate,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800518 "no-annotate" => sub {$annotate = 0},
Ryan Anderson1f038a02005-09-05 01:13:07 -0400519 "compose" => \$compose,
Ryan Anderson30d08b32006-02-02 11:56:06 -0500520 "quiet" => \$quiet,
Joe Perches324a8bd2007-08-17 18:51:12 -0700521 "cc-cmd=s" => \$cc_cmd,
Adam Roben5483c712007-06-27 20:59:37 -0700522 "suppress-from!" => \$suppress_from,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800523 "no-suppress-from" => sub {$suppress_from = 0},
David Brown65648282007-12-25 19:56:29 -0800524 "suppress-cc=s" => \@suppress_cc,
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500525 "signed-off-cc|signed-off-by-cc!" => \$signed_off_by_cc,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800526 "no-signed-off-cc|no-signed-off-by-cc" => sub {$signed_off_by_cc = 0},
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300527 "cc-cover|cc-cover!" => \$cover_cc,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800528 "no-cc-cover" => sub {$cover_cc = 0},
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300529 "to-cover|to-cover!" => \$cover_to,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800530 "no-to-cover" => sub {$cover_to = 0},
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500531 "confirm=s" => \$confirm,
Matthew Wilcox61302592006-10-10 08:58:23 -0600532 "dry-run" => \$dry_run,
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700533 "envelope-sender=s" => \$envelope_sender,
Adam Roben5483c712007-06-27 20:59:37 -0700534 "thread!" => \$thread,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800535 "no-thread" => sub {$thread = 0},
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500536 "validate!" => \$validate,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800537 "no-validate" => sub {$validate = 0},
Paolo Bonzini8d814082014-11-25 15:00:27 +0100538 "transfer-encoding=s" => \$target_xfer_encoding,
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100539 "format-patch!" => \$format_patch,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800540 "no-format-patch" => sub {$format_patch = 0},
Thomas Rast3cae7e52010-06-17 22:10:39 +0200541 "8bit-encoding=s" => \$auto_8bit_encoding,
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200542 "compose-encoding=s" => \$compose_encoding,
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200543 "force" => \$force,
Luis Henriquesac1596a2014-03-24 21:38:27 +0000544 "xmailer!" => \$use_xmailer,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800545 "no-xmailer" => sub {$use_xmailer = 0},
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800546 "batch-size=i" => \$batch_size,
547 "relogin-delay=i" => \$relogin_delay,
Duy Nguyen13374982018-11-03 07:03:18 +0100548 "git-completion-helper" => \$git_completion_helper,
Kyle Meyer8774aa52022-11-26 15:21:23 -0500549 "v=s" => \$reroll_count,
Thiago Perrotta2b7b7582021-10-25 17:27:06 -0400550);
551$rc = GetOptions(%options);
Ryan Anderson83b24432005-07-31 04:17:25 -0400552
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200553# Munge any "either config or getopt, not both" variables
554my @initial_to = @getopt_to ? @getopt_to : ($no_to ? () : @config_to);
555my @initial_cc = @getopt_cc ? @getopt_cc : ($no_cc ? () : @config_cc);
556my @initial_bcc = @getopt_bcc ? @getopt_bcc : ($no_bcc ? () : @config_bcc);
557
Clemens Buchacherc5978242011-09-03 19:06:13 +0200558usage() if $help;
Thiago Perrotta2b7b7582021-10-25 17:27:06 -0400559my %all_options = (%options, %dump_aliases_options, %identity_options);
560completion_helper(\%all_options) if $git_completion_helper;
Michael Coleman1b0baf12007-02-27 22:47:54 -0600561unless ($rc) {
562 usage();
563}
564
Ævar Arnfjörð Bjarmason9264d292021-05-28 11:23:45 +0200565if ($forbid_sendmail_variables && grep { /^sendmail/s } keys %known_config_keys) {
Drew DeVaultdd84e522020-07-23 20:44:32 -0400566 die __("fatal: found configuration options for 'sendmail'\n" .
567 "git-send-email is configured with the sendemail.* options - note the 'e'.\n" .
568 "Set sendemail.forbidSendmailVariables to false to disable this check.\n");
569}
570
Vasco Almeida46493102016-12-14 11:54:36 -0100571die __("Cannot run git format-patch from outside a repository\n")
Jay Soffianeed6ca72009-02-14 23:32:13 -0500572 if $format_patch and not $repo;
573
Stefan Beller9caa7062018-02-12 11:44:04 -0800574die __("`batch-size` and `relogin` must be specified together " .
575 "(via command-line or configuration option)\n")
576 if defined $relogin_delay and not defined $batch_size;
577
Thomas Rastfa835cd2008-06-26 23:03:21 +0200578# 'default' encryption is none -- this only prevents a warning
579$smtp_encryption = '' unless (defined $smtp_encryption);
580
David Brown65648282007-12-25 19:56:29 -0800581# Set CC suppressions
582my(%suppress_cc);
583if (@suppress_cc) {
584 foreach my $entry (@suppress_cc) {
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700585 # Please update $__git_send_email_suppresscc_options
586 # in git-completion.bash when you add new options.
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100587 die sprintf(__("Unknown --suppress-cc field: '%s'\n"), $entry)
Rasmus Villemoesef0cc1d2018-10-16 09:39:23 +0200588 unless $entry =~ /^(?:all|cccmd|cc|author|self|sob|body|bodycc|misc-by)$/;
David Brown65648282007-12-25 19:56:29 -0800589 $suppress_cc{$entry} = 1;
590 }
591}
592
593if ($suppress_cc{'all'}) {
Rasmus Villemoesef0cc1d2018-10-16 09:39:23 +0200594 foreach my $entry (qw (cccmd cc author self sob body bodycc misc-by)) {
David Brown65648282007-12-25 19:56:29 -0800595 $suppress_cc{$entry} = 1;
596 }
597 delete $suppress_cc{'all'};
598}
599
600# If explicit old-style ones are specified, they trump --suppress-cc.
601$suppress_cc{'self'} = $suppress_from if defined $suppress_from;
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500602$suppress_cc{'sob'} = !$signed_off_by_cc if defined $signed_off_by_cc;
David Brown65648282007-12-25 19:56:29 -0800603
Jay Soffian3531e272009-02-14 23:32:15 -0500604if ($suppress_cc{'body'}) {
Rasmus Villemoesef0cc1d2018-10-16 09:39:23 +0200605 foreach my $entry (qw (sob bodycc misc-by)) {
Jay Soffian3531e272009-02-14 23:32:15 -0500606 $suppress_cc{$entry} = 1;
607 }
608 delete $suppress_cc{'body'};
609}
610
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500611# Set confirm's default value
612my $confirm_unconfigured = !defined $confirm;
613if ($confirm_unconfigured) {
614 $confirm = scalar %suppress_cc ? 'compose' : 'auto';
615};
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700616# Please update $__git_send_email_confirm_options in
617# git-completion.bash when you add new options.
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100618die sprintf(__("Unknown --confirm setting: '%s'\n"), $confirm)
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500619 unless $confirm =~ /^(?:auto|cc|compose|always|never)/;
620
David Brown65648282007-12-25 19:56:29 -0800621# Debugging, print out the suppressions.
622if (0) {
623 print "suppressions:\n";
624 foreach my $entry (keys %suppress_cc) {
625 printf " %-5s -> $suppress_cc{$entry}\n", $entry;
626 }
627}
628
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100629my ($repoauthor, $repocommitter);
Ævar Arnfjörð Bjarmasonfef381e2021-05-28 11:23:46 +0200630{
631 my %cache;
632 my ($author, $committer);
633 my $common = sub {
634 my ($what) = @_;
635 return $cache{$what} if exists $cache{$what};
636 ($cache{$what}) = Git::ident_person(@repo, $what);
637 return $cache{$what};
638 };
639 $repoauthor = sub { $common->('author') };
640 $repocommitter = sub { $common->('committer') };
641}
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900642
Jay Soffian50126992009-02-14 23:32:14 -0500643sub parse_address_line {
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200644 require Git::LoadCPAN::Mail::Address;
Matthieu Moybd869f62018-01-05 19:36:51 +0100645 return map { $_->format } Mail::Address->parse($_[0]);
Jay Soffian50126992009-02-14 23:32:14 -0500646}
647
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800648sub split_addrs {
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200649 require Text::ParseWords;
650 return Text::ParseWords::quotewords('\s*,\s*', 1, @_);
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800651}
652
Eric Wong994d6c62006-05-14 19:13:44 -0700653my %aliases;
Eric Sunshine09f11572015-05-31 18:29:27 -0400654
655sub parse_sendmail_alias {
656 local $_ = shift;
657 if (/"/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100658 printf STDERR __("warning: sendmail alias with quotes is not supported: %s\n"), $_;
Eric Sunshine86b89842015-06-01 14:22:36 -0400659 } elsif (/:include:/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100660 printf STDERR __("warning: `:include:` not supported: %s\n"), $_;
Eric Sunshine86b89842015-06-01 14:22:36 -0400661 } elsif (/[\/|]/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100662 printf STDERR __("warning: `/file` or `|pipe` redirection not supported: %s\n"), $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400663 } elsif (/^(\S+?)\s*:\s*(.+)$/) {
664 my ($alias, $addr) = ($1, $2);
665 $aliases{$alias} = [ split_addrs($addr) ];
666 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100667 printf STDERR __("warning: sendmail line is not recognized: %s\n"), $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400668 }
669}
670
671sub parse_sendmail_aliases {
672 my $fh = shift;
Eric Sunshine2532dd02015-05-31 18:29:29 -0400673 my $s = '';
Eric Sunshine09f11572015-05-31 18:29:27 -0400674 while (<$fh>) {
Eric Sunshine2532dd02015-05-31 18:29:29 -0400675 chomp;
Eric Sunshine020be85f2015-05-31 18:29:28 -0400676 next if /^\s*$/ || /^\s*#/;
Eric Sunshine2532dd02015-05-31 18:29:29 -0400677 $s .= $_, next if $s =~ s/\\$// || s/^\s+//;
678 parse_sendmail_alias($s) if $s;
679 $s = $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400680 }
Eric Sunshine2532dd02015-05-31 18:29:29 -0400681 $s =~ s/\\$//; # silently tolerate stray '\' on last line
682 parse_sendmail_alias($s) if $s;
Eric Sunshine09f11572015-05-31 18:29:27 -0400683}
684
Eric Wong994d6c62006-05-14 19:13:44 -0700685my %parse_alias = (
686 # multiline formats can be supported in the future
687 mutt => sub { my $fh = shift; while (<$fh>) {
Felipe Contrerasffc01f92009-09-30 17:49:36 +0300688 if (/^\s*alias\s+(?:-group\s+\S+\s+)*(\S+)\s+(.*)$/) {
Eric Wong994d6c62006-05-14 19:13:44 -0700689 my ($alias, $addr) = ($1, $2);
690 $addr =~ s/#.*$//; # mutt allows # comments
Eric Wong2c510f22016-01-04 20:53:30 +0000691 # commas delimit multiple addresses
692 my @addr = split_addrs($addr);
693
694 # quotes may be escaped in the file,
695 # unescape them so we do not double-escape them later.
696 s/\\"/"/g foreach @addr;
697 $aliases{$alias} = \@addr
Eric Wong994d6c62006-05-14 19:13:44 -0700698 }}},
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200699 mailrc => sub { my $fh = shift; while (<$fh>) {
Jeff Kinga277d1e2016-03-17 19:58:22 -0400700 if (/^alias\s+(\S+)\s+(.*?)\s*$/) {
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200701 require Text::ParseWords;
Eric Wong994d6c62006-05-14 19:13:44 -0700702 # spaces delimit multiple addresses
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200703 $aliases{$1} = [ Text::ParseWords::quotewords('\s+', 0, $2) ];
Eric Wong994d6c62006-05-14 19:13:44 -0700704 }}},
Trent Piepho73c427e2008-11-25 18:55:00 -0800705 pine => sub { my $fh = shift; my $f='\t[^\t]*';
706 for (my $x = ''; defined($x); $x = $_) {
707 chomp $x;
708 $x .= $1 while(defined($_ = <$fh>) && /^ +(.*)$/);
709 $x =~ /^(\S+)$f\t\(?([^\t]+?)\)?(:?$f){0,2}$/ or next;
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800710 $aliases{$1} = [ split_addrs($2) ];
Trent Piepho73c427e2008-11-25 18:55:00 -0800711 }},
Bill Pemberton7613ea32009-04-22 09:41:29 -0400712 elm => sub { my $fh = shift;
713 while (<$fh>) {
714 if (/^(\S+)\s+=\s+[^=]+=\s(\S+)/) {
715 my ($alias, $addr) = ($1, $2);
716 $aliases{$alias} = [ split_addrs($addr) ];
717 }
718 } },
Eric Sunshine09f11572015-05-31 18:29:27 -0400719 sendmail => \&parse_sendmail_aliases,
Eric Wong994d6c62006-05-14 19:13:44 -0700720 gnus => sub { my $fh = shift; while (<$fh>) {
721 if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
722 $aliases{$1} = [ $2 ];
723 }}}
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700724 # Please update _git_config() in git-completion.bash when you
725 # add new MUAs.
Eric Wong994d6c62006-05-14 19:13:44 -0700726);
727
Petr Baudis3cb8caf2006-07-03 22:47:58 +0200728if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) {
Eric Wong994d6c62006-05-14 19:13:44 -0700729 foreach my $file (@alias_files) {
730 open my $fh, '<', $file or die "opening $file: $!\n";
731 $parse_alias{$aliasfiletype}->($fh);
732 close $fh;
733 }
734}
735
Jacob Keller17b7a832015-11-19 14:52:11 -0800736if ($dump_aliases) {
737 print "$_\n" for (sort keys %aliases);
738 exit(0);
739}
740
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700741# is_format_patch_arg($f) returns 0 if $f names a patch, or 1 if
742# $f is a revision list specification to be passed to format-patch.
743sub is_format_patch_arg {
Jay Soffianeed6ca72009-02-14 23:32:13 -0500744 return unless $repo;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100745 my $f = shift;
746 try {
747 $repo->command('rev-parse', '--verify', '--quiet', $f);
748 if (defined($format_patch)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100749 return $format_patch;
750 }
Ævar Arnfjörð Bjarmason4adbf382021-05-28 11:23:47 +0200751 die sprintf(__(<<EOF), $f, $f);
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100752File '%s' exists but it could also be the range of commits
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100753to produce patches for. Please disambiguate by...
754
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100755 * Saying "./%s" if you mean a file; or
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100756 * Giving --format-patch option if you mean a range.
757EOF
758 } catch Git::Error::Command with {
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700759 # Not a valid revision. Treat it as a filename.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100760 return 0;
761 }
762}
763
Jeff Kingaa548922008-01-18 09:19:36 -0500764# Now that all the defaults are set, process the rest of the command line
765# arguments and collect up the files that need to be processed.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100766my @rev_list_opts;
Junio C Hamano69f4ce52008-11-30 22:38:20 -0800767while (defined(my $f = shift @ARGV)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100768 if ($f eq "--") {
769 push @rev_list_opts, "--", @ARGV;
770 @ARGV = ();
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700771 } elsif (-d $f and !is_format_patch_arg($f)) {
Ævar Arnfjörð Bjarmasonc6038162010-09-30 13:42:54 +0000772 opendir my $dh, $f
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100773 or die sprintf(__("Failed to opendir %s: %s"), $f, $!);
Jeff Kingaa548922008-01-18 09:19:36 -0500774
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200775 require File::Spec;
776 push @files, grep { -f $_ } map { File::Spec->catfile($f, $_) }
Ævar Arnfjörð Bjarmasonc6038162010-09-30 13:42:54 +0000777 sort readdir $dh;
778 closedir $dh;
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700779 } elsif ((-f $f or -p $f) and !is_format_patch_arg($f)) {
Jeff Kingaa548922008-01-18 09:19:36 -0500780 push @files, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500781 } else {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100782 push @rev_list_opts, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500783 }
784}
785
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100786if (@rev_list_opts) {
Vasco Almeida46493102016-12-14 11:54:36 -0100787 die __("Cannot run git format-patch from outside a repository\n")
Jay Soffianeed6ca72009-02-14 23:32:13 -0500788 unless $repo;
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200789 require File::Temp;
Kyle Meyer8774aa52022-11-26 15:21:23 -0500790 push @files, $repo->command('format-patch', '-o', File::Temp::tempdir(CLEANUP => 1),
791 defined $reroll_count ? ('-v', $reroll_count) : (),
792 @rev_list_opts);
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100793}
794
Michael Strawbridgea8022c52023-04-19 16:27:03 -0400795if (defined $sender) {
796 $sender =~ s/^\s+|\s+$//g;
797 ($sender) = expand_aliases($sender);
798} else {
799 $sender = $repoauthor->() || $repocommitter->() || '';
800}
801
802# $sender could be an already sanitized address
803# (e.g. sendemail.from could be manually sanitized by user).
804# But it's a no-op to run sanitize_address on an already sanitized address.
805$sender = sanitize_address($sender);
806
807$time = time - scalar $#files;
Junio C Hamano531220b2016-03-17 22:40:05 -0700808
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500809if ($validate) {
Jeff Kingc764a0c2008-01-18 09:20:10 -0500810 foreach my $f (@files) {
Kevin Ballard300913b2008-06-25 15:44:40 -0700811 unless (-p $f) {
Michael Strawbridgea8022c52023-04-19 16:27:03 -0400812 pre_process_file($f, 1);
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +0200813 validate_patch($f, $target_xfer_encoding);
Kevin Ballard300913b2008-06-25 15:44:40 -0700814 }
Jeff Kingc764a0c2008-01-18 09:20:10 -0500815 }
Jeff King747bbff2008-01-18 09:19:48 -0500816}
817
Michael Strawbridgea8022c52023-04-19 16:27:03 -0400818@files = handle_backup_files(@files);
819
Jeff Kingaa548922008-01-18 09:19:36 -0500820if (@files) {
821 unless ($quiet) {
822 print $_,"\n" for (@files);
823 }
824} else {
Vasco Almeida46493102016-12-14 11:54:36 -0100825 print STDERR __("\nNo patch files specified!\n\n");
Jeff Kingaa548922008-01-18 09:19:36 -0500826 usage();
827}
828
Ævar Arnfjörð Bjarmasonacf071b2010-09-30 13:42:57 +0000829sub get_patch_subject {
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100830 my $fn = shift;
831 open (my $fh, '<', $fn);
832 while (my $line = <$fh>) {
833 next unless ($line =~ /^Subject: (.*)$/);
834 close $fh;
835 return "GIT: $1\n";
836 }
837 close $fh;
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100838 die sprintf(__("No subject line in %s?"), $fn);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100839}
840
841if ($compose) {
842 # Note that this does not need to be secure, but we will make a small
843 # effort to have it be unique
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200844 require File::Temp;
Jay Soffianafe756c2009-02-23 13:51:37 -0500845 $compose_filename = ($repo ?
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200846 File::Temp::tempfile(".gitsendemail.msg.XXXXXX", DIR => $repo->repo_path()) :
847 File::Temp::tempfile(".gitsendemail.msg.XXXXXX", DIR => "."))[1];
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000848 open my $c, ">", $compose_filename
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100849 or die sprintf(__("Failed to open for writing %s: %s"), $compose_filename, $!);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100850
851
Ævar Arnfjörð Bjarmasonfef381e2021-05-28 11:23:46 +0200852 my $tpl_sender = $sender || $repoauthor->() || $repocommitter->() || '';
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100853 my $tpl_subject = $initial_subject || '';
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100854 my $tpl_in_reply_to = $initial_in_reply_to || '';
Christian Ludwigd11c9432018-03-04 00:58:14 +0100855 my $tpl_reply_to = $reply_to || '';
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100856
Ævar Arnfjörð Bjarmason4adbf382021-05-28 11:23:47 +0200857 print $c <<EOT1, Git::prefix_lines("GIT: ", __(<<EOT2)), <<EOT3;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100858From $tpl_sender # This line is ignored.
Vasco Almeida70aedfb2016-12-14 11:54:38 -0100859EOT1
860Lines beginning in "GIT:" will be removed.
861Consider including an overall diffstat or table of contents
862for the patch you are writing.
863
864Clear the body content if you don't wish to send a summary.
865EOT2
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100866From: $tpl_sender
Christian Ludwigd11c9432018-03-04 00:58:14 +0100867Reply-To: $tpl_reply_to
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100868Subject: $tpl_subject
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100869In-Reply-To: $tpl_in_reply_to
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100870
Vasco Almeida70aedfb2016-12-14 11:54:38 -0100871EOT3
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100872 for my $f (@files) {
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000873 print $c get_patch_subject($f);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100874 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000875 close $c;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100876
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100877 if ($annotate) {
878 do_edit($compose_filename, @files);
879 } else {
880 do_edit($compose_filename);
881 }
882
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000883 open $c, "<", $compose_filename
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100884 or die sprintf(__("Failed to open %s: %s"), $compose_filename, $!);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100885
Krzysztof Mazur4a47a4d2012-10-22 14:41:48 +0200886 if (!defined $compose_encoding) {
887 $compose_encoding = "UTF-8";
888 }
Nathan Payreb6049542017-12-15 16:33:39 +0100889
890 my %parsed_email;
891 while (my $line = <$c>) {
892 next if $line =~ m/^GIT:/;
893 parse_header_line($line, \%parsed_email);
894 if ($line =~ /^$/) {
895 $parsed_email{'body'} = filter_body($c);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100896 }
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100897 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000898 close $c;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100899
Nathan Payreb6049542017-12-15 16:33:39 +0100900 open my $c2, ">", $compose_filename . ".final"
901 or die sprintf(__("Failed to open %s.final: %s"), $compose_filename, $!);
902
903
904 if ($parsed_email{'From'}) {
905 $sender = delete($parsed_email{'From'});
906 }
907 if ($parsed_email{'In-Reply-To'}) {
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100908 $initial_in_reply_to = delete($parsed_email{'In-Reply-To'});
Nathan Payreb6049542017-12-15 16:33:39 +0100909 }
Christian Ludwigd11c9432018-03-04 00:58:14 +0100910 if ($parsed_email{'Reply-To'}) {
911 $reply_to = delete($parsed_email{'Reply-To'});
Nathan Payreb6049542017-12-15 16:33:39 +0100912 }
913 if ($parsed_email{'Subject'}) {
914 $initial_subject = delete($parsed_email{'Subject'});
915 print $c2 "Subject: " .
916 quote_subject($initial_subject, $compose_encoding) .
917 "\n";
918 }
919
920 if ($parsed_email{'MIME-Version'}) {
921 print $c2 "MIME-Version: $parsed_email{'MIME-Version'}\n",
922 "Content-Type: $parsed_email{'Content-Type'};\n",
923 "Content-Transfer-Encoding: $parsed_email{'Content-Transfer-Encoding'}\n";
924 delete($parsed_email{'MIME-Version'});
925 delete($parsed_email{'Content-Type'});
926 delete($parsed_email{'Content-Transfer-Encoding'});
927 } elsif (file_has_nonascii($compose_filename)) {
928 my $content_type = (delete($parsed_email{'Content-Type'}) or
929 "text/plain; charset=$compose_encoding");
930 print $c2 "MIME-Version: 1.0\n",
931 "Content-Type: $content_type\n",
932 "Content-Transfer-Encoding: 8bit\n";
933 }
934 # Preserve unknown headers
935 foreach my $key (keys %parsed_email) {
936 next if $key eq 'body';
937 print $c2 "$key: $parsed_email{$key}";
938 }
939
940 if ($parsed_email{'body'}) {
941 print $c2 "\n$parsed_email{'body'}\n";
942 delete($parsed_email{'body'});
943 } else {
Vasco Almeida46493102016-12-14 11:54:36 -0100944 print __("Summary email is empty, skipping it\n");
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100945 $compose = -1;
946 }
Nathan Payreb6049542017-12-15 16:33:39 +0100947
948 close $c2;
949
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100950} elsif ($annotate) {
951 do_edit(@files);
952}
953
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200954sub term {
955 my $term = eval {
956 require Term::ReadLine;
957 $ENV{"GIT_SEND_EMAIL_NOTTY"}
958 ? Term::ReadLine->new('git-send-email', \*STDIN, \*STDOUT)
959 : Term::ReadLine->new('git-send-email');
960 };
961 if ($@) {
962 $term = FakeTerm->new("$@: going non-interactive");
963 }
964 return $term;
965}
966
Jay Soffian6e182512009-03-28 21:39:10 -0400967sub ask {
968 my ($prompt, %arg) = @_;
Jay Soffian0da43a62009-04-04 23:23:21 -0400969 my $valid_re = $arg{valid_re};
Jay Soffian6e182512009-03-28 21:39:10 -0400970 my $default = $arg{default};
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700971 my $confirm_only = $arg{confirm_only};
Jay Soffian6e182512009-03-28 21:39:10 -0400972 my $resp;
973 my $i = 0;
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200974 my $term = term();
Jay Soffian5906f542009-03-31 12:22:11 -0400975 return defined $default ? $default : undef
976 unless defined $term->IN and defined fileno($term->IN) and
977 defined $term->OUT and defined fileno($term->OUT);
Jay Soffian6e182512009-03-28 21:39:10 -0400978 while ($i++ < 10) {
979 $resp = $term->readline($prompt);
980 if (!defined $resp) { # EOF
981 print "\n";
982 return defined $default ? $default : undef;
983 }
984 if ($resp eq '' and defined $default) {
985 return $default;
986 }
Jay Soffian0da43a62009-04-04 23:23:21 -0400987 if (!defined $valid_re or $resp =~ /$valid_re/) {
Jay Soffian6e182512009-03-28 21:39:10 -0400988 return $resp;
989 }
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700990 if ($confirm_only) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100991 my $yesno = $term->readline(
992 # TRANSLATORS: please keep [y/N] as is.
993 sprintf(__("Are you sure you want to use <%s> [y/N]? "), $resp));
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700994 if (defined $yesno && $yesno =~ /y/i) {
995 return $resp;
996 }
997 }
Jay Soffian6e182512009-03-28 21:39:10 -0400998 }
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -0700999 return;
Jay Soffian6e182512009-03-28 21:39:10 -04001000}
1001
Nathan Payreb6049542017-12-15 16:33:39 +01001002sub parse_header_line {
1003 my $lines = shift;
1004 my $parsed_line = shift;
1005 my $addr_pat = join "|", qw(To Cc Bcc);
1006
1007 foreach (split(/\n/, $lines)) {
1008 if (/^($addr_pat):\s*(.+)$/i) {
1009 $parsed_line->{$1} = [ parse_address_line($2) ];
1010 } elsif (/^([^:]*):\s*(.+)\s*$/i) {
1011 $parsed_line->{$1} = $2;
1012 }
1013 }
1014}
1015
1016sub filter_body {
1017 my $c = shift;
1018 my $body = "";
1019 while (my $body_line = <$c>) {
1020 if ($body_line !~ m/^GIT:/) {
1021 $body .= $body_line;
1022 }
1023 }
1024 return $body;
1025}
1026
1027
Thomas Rast3cae7e52010-06-17 22:10:39 +02001028my %broken_encoding;
1029
Ævar Arnfjörð Bjarmason1d50bfd2010-09-30 13:42:58 +00001030sub file_declares_8bit_cte {
Thomas Rast3cae7e52010-06-17 22:10:39 +02001031 my $fn = shift;
1032 open (my $fh, '<', $fn);
1033 while (my $line = <$fh>) {
1034 last if ($line =~ /^$/);
1035 return 1 if ($line =~ /^Content-Transfer-Encoding: .*8bit.*$/);
1036 }
1037 close $fh;
1038 return 0;
1039}
1040
1041foreach my $f (@files) {
1042 next unless (body_or_subject_has_nonascii($f)
1043 && !file_declares_8bit_cte($f));
1044 $broken_encoding{$f} = 1;
1045}
1046
1047if (!defined $auto_8bit_encoding && scalar %broken_encoding) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001048 print __("The following files are 8bit, but do not declare " .
1049 "a Content-Transfer-Encoding.\n");
Thomas Rast3cae7e52010-06-17 22:10:39 +02001050 foreach my $f (sort keys %broken_encoding) {
1051 print " $f\n";
1052 }
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001053 $auto_8bit_encoding = ask(__("Which 8bit encoding should I declare [UTF-8]? "),
Junio C Hamano852a15d2015-02-13 12:20:25 -08001054 valid_re => qr/.{4}/, confirm_only => 1,
Thomas Rast3cae7e52010-06-17 22:10:39 +02001055 default => "UTF-8");
1056}
1057
Thomas Rasta03bc5b2009-06-08 23:34:12 +02001058if (!$force) {
1059 for my $f (@files) {
Ævar Arnfjörð Bjarmason0d290a42010-09-30 13:43:01 +00001060 if (get_patch_subject($f) =~ /\Q*** SUBJECT HERE ***\E/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001061 die sprintf(__("Refusing to send because the patch\n\t%s\n"
Thomas Rasta03bc5b2009-06-08 23:34:12 +02001062 . "has the template subject '*** SUBJECT HERE ***'. "
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001063 . "Pass --force if you really want to send.\n"), $f);
Thomas Rasta03bc5b2009-06-08 23:34:12 +02001064 }
1065 }
1066}
1067
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001068my $to_whom = __("To whom should the emails be sent (if anyone)?");
Felipe Contreras8cac13d2012-11-24 12:16:19 +01001069my $prompting = 0;
Junio C Hamano8796ff72010-10-26 22:02:03 -07001070if (!@initial_to && !defined $to_cmd) {
Junio C Hamano0d6b21e2016-04-24 12:31:44 -07001071 my $to = ask("$to_whom ",
Stephen Boyd61837492012-09-06 11:31:11 -07001072 default => "",
Junio C Hamano51bbccf2012-08-14 15:15:53 -07001073 valid_re => qr/\@.*\./, confirm_only => 1);
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001074 push @initial_to, parse_address_line($to) if defined $to; # sanitized/validated later
Ryan Anderson1f038a02005-09-05 01:13:07 -04001075 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -04001076}
1077
Eric Wong994d6c62006-05-14 19:13:44 -07001078sub expand_aliases {
Jeff King302e04e2009-07-23 07:09:29 -04001079 return map { expand_one_alias($_) } @_;
1080}
1081
1082my %EXPANDED_ALIASES;
1083sub expand_one_alias {
1084 my $alias = shift;
1085 if ($EXPANDED_ALIASES{$alias}) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001086 die sprintf(__("fatal: alias '%s' expands to itself\n"), $alias);
Jeff King302e04e2009-07-23 07:09:29 -04001087 }
1088 local $EXPANDED_ALIASES{$alias} = 1;
1089 return $aliases{$alias} ? expand_aliases(@{$aliases{$alias}}) : $alias;
Eric Wong994d6c62006-05-14 19:13:44 -07001090}
1091
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001092@initial_to = process_address_list(@initial_to);
1093@initial_cc = process_address_list(@initial_cc);
Ævar Arnfjörð Bjarmasone60f6d12019-05-09 13:48:29 +02001094@initial_bcc = process_address_list(@initial_bcc);
Eric Wong994d6c62006-05-14 19:13:44 -07001095
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001096if ($thread && !defined $initial_in_reply_to && $prompting) {
1097 $initial_in_reply_to = ask(
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001098 __("Message-ID to be used as In-Reply-To for the first email (if any)? "),
Stephen Boyd61837492012-09-06 11:31:11 -07001099 default => "",
Junio C Hamano51bbccf2012-08-14 15:15:53 -07001100 valid_re => qr/\@.*\./, confirm_only => 1);
Ryan Anderson83b24432005-07-31 04:17:25 -04001101}
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001102if (defined $initial_in_reply_to) {
1103 $initial_in_reply_to =~ s/^\s*<?//;
1104 $initial_in_reply_to =~ s/>?\s*$//;
1105 $initial_in_reply_to = "<$initial_in_reply_to>" if $initial_in_reply_to ne '';
Junio C Hamanoace9c2a2007-12-10 21:44:42 -08001106}
Mike Hommeyace72082007-12-09 18:17:28 +01001107
Christian Ludwigd11c9432018-03-04 00:58:14 +01001108if (defined $reply_to) {
1109 $reply_to =~ s/^\s+|\s+$//g;
1110 ($reply_to) = expand_aliases($reply_to);
1111 $reply_to = sanitize_address($reply_to);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001112}
Eric Wongaca7ad72006-05-15 02:34:44 -07001113
Gregory Anderscd5b33f2021-05-14 09:15:53 -06001114if (!defined $sendmail_cmd && !defined $smtp_server) {
Florian Klink1ab2fd42017-11-28 01:49:04 +01001115 my @sendmail_paths = qw( /usr/sbin/sendmail /usr/lib/sendmail );
1116 push @sendmail_paths, map {"$_/sendmail"} split /:/, $ENV{PATH};
1117 foreach (@sendmail_paths) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001118 if (-x $_) {
Gregory Anderscd5b33f2021-05-14 09:15:53 -06001119 $sendmail_cmd = $_;
Eric Wongaca7ad72006-05-15 02:34:44 -07001120 last;
1121 }
1122 }
Gregory Anderscd5b33f2021-05-14 09:15:53 -06001123
1124 if (!defined $sendmail_cmd) {
1125 $smtp_server = 'localhost'; # could be 127.0.0.1, too... *shrug*
1126 }
Ryan Anderson3342d852005-07-31 20:04:24 -04001127}
1128
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001129if ($compose && $compose > 0) {
1130 @files = ($compose_filename . ".final", @files);
Ryan Anderson1f038a02005-09-05 01:13:07 -04001131}
1132
Ryan Anderson83b24432005-07-31 04:17:25 -04001133# Variables we set as part of the loop over files
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001134our ($message_id, %mail, $subject, $in_reply_to, $references, $message,
Jay Soffiandc1460a2009-03-31 12:22:12 -04001135 $needs_confirm, $message_num, $ask_default);
Ryan Anderson83b24432005-07-31 04:17:25 -04001136
Eric Wong567ffeb2006-03-25 16:47:12 -08001137sub extract_valid_address {
1138 my $address = shift;
Ævar Arnfjörð Bjarmason35b6ab92010-09-30 19:03:31 +00001139 my $local_part_regexp = qr/[^<>"\s@]+/;
1140 my $domain_regexp = qr/[^.<>"\s@]+(?:\.[^.<>"\s@]+)+/;
Eric Wongdb3106b2006-05-15 02:41:01 -07001141
1142 # check for a local address:
Junio C Hamanoad9c18f2006-06-06 00:05:56 -07001143 return $address if ($address =~ /^($local_part_regexp)$/);
Eric Wongdb3106b2006-05-15 02:41:01 -07001144
Uwe Kleine-König155197e2007-08-09 15:27:57 +02001145 $address =~ s/^\s*<(.*)>\s*$/$1/;
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +02001146 my $have_email_valid = eval { require Email::Valid; 1 };
Eric Wong567ffeb2006-03-25 16:47:12 -08001147 if ($have_email_valid) {
Junio C Hamanoad9c18f2006-06-06 00:05:56 -07001148 return scalar Email::Valid->address($address);
Eric Wong567ffeb2006-03-25 16:47:12 -08001149 }
Krzysztof Mazur95c0d4b2012-11-22 19:12:09 +01001150
1151 # less robust/correct than the monster regexp in Email::Valid,
1152 # but still does a 99% job, and one less dependency
1153 return $1 if $address =~ /($local_part_regexp\@$domain_regexp)/;
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -07001154 return;
Eric Wong567ffeb2006-03-25 16:47:12 -08001155}
Ryan Anderson83b24432005-07-31 04:17:25 -04001156
Krzysztof Mazure4312252012-11-22 19:12:10 +01001157sub extract_valid_address_or_die {
1158 my $address = shift;
1159 $address = extract_valid_address($address);
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001160 die sprintf(__("error: unable to extract a valid address from: %s\n"), $address)
Krzysztof Mazure4312252012-11-22 19:12:10 +01001161 if !$address;
1162 return $address;
1163}
1164
1165sub validate_address {
1166 my $address = shift;
Krzysztof Mazurd0e98102012-11-22 19:12:12 +01001167 while (!extract_valid_address($address)) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001168 printf STDERR __("error: unable to extract a valid address from: %s\n"), $address;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001169 # TRANSLATORS: Make sure to include [q] [d] [e] in your
1170 # translation. The program will only accept English input
1171 # at this point.
1172 $_ = ask(__("What to do with this address? ([q]uit|[d]rop|[e]dit): "),
Krzysztof Mazurd0e98102012-11-22 19:12:12 +01001173 valid_re => qr/^(?:quit|q|drop|d|edit|e)/i,
Krzysztof Mazur5c80afe2012-11-22 19:12:11 +01001174 default => 'q');
1175 if (/^d/i) {
1176 return undef;
1177 } elsif (/^q/i) {
1178 cleanup_compose_files();
1179 exit(0);
1180 }
Junio C Hamano0d6b21e2016-04-24 12:31:44 -07001181 $address = ask("$to_whom ",
Krzysztof Mazurd0e98102012-11-22 19:12:12 +01001182 default => "",
1183 valid_re => qr/\@.*\./, confirm_only => 1);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001184 }
1185 return $address;
1186}
1187
1188sub validate_address_list {
1189 return (grep { defined $_ }
1190 map { validate_address($_) } @_);
Ryan Anderson83b24432005-07-31 04:17:25 -04001191}
1192
1193# Usually don't need to change anything below here.
1194
1195# we make a "fake" message id by taking the current number
1196# of seconds since the beginning of Unix time and tacking on
1197# a random number to the end, in case we are called quicker than
1198# 1 second since the last time we were called.
Ryan Anderson8037d1a2005-07-31 20:04:24 -04001199
1200# We'll setup a template for the message id, using the "from" address:
Ryan Anderson8037d1a2005-07-31 20:04:24 -04001201
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001202my ($message_id_stamp, $message_id_serial);
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001203sub make_message_id {
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001204 my $uniq;
1205 if (!defined $message_id_stamp) {
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +02001206 require POSIX;
1207 $message_id_stamp = POSIX::strftime("%Y%m%d%H%M%S.$$", gmtime(time));
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001208 $message_id_serial = 0;
1209 }
1210 $message_id_serial++;
1211 $uniq = "$message_id_stamp-$message_id_serial";
1212
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001213 my $du_part;
Ævar Arnfjörð Bjarmasonfef381e2021-05-28 11:23:46 +02001214 for ($sender, $repocommitter->(), $repoauthor->()) {
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001215 $du_part = extract_valid_address(sanitize_address($_));
1216 last if (defined $du_part and $du_part ne '');
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001217 }
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001218 if (not defined $du_part or $du_part eq '') {
Ævar Arnfjörð Bjarmason529dd382010-09-30 13:43:08 +00001219 require Sys::Hostname;
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001220 $du_part = 'user@' . Sys::Hostname::hostname();
1221 }
Eric Wongf916ab02016-04-06 20:07:14 +00001222 my $message_id_template = "<%s-%s>";
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001223 $message_id = sprintf($message_id_template, $uniq, $du_part);
Ryan Anderson8037d1a2005-07-31 20:04:24 -04001224 #print "new message id = $message_id\n"; # Was useful for debugging
Ryan Anderson83b24432005-07-31 04:17:25 -04001225}
1226
Jürgen Rühle374c5902007-01-10 13:36:39 -08001227sub unquote_rfc2047 {
1228 local ($_) = @_;
Роман Донченко11f70a72014-12-14 18:59:46 +03001229 my $charset;
Роман Донченкоab47e2a2014-12-14 18:59:47 +03001230 my $sep = qr/[ \t]+/;
1231 s{$re_encoded_word(?:$sep$re_encoded_word)*}{
1232 my @words = split $sep, $&;
1233 foreach (@words) {
1234 m/$re_encoded_word/;
1235 $charset = $1;
1236 my $encoding = $2;
1237 my $text = $3;
1238 if ($encoding eq 'q' || $encoding eq 'Q') {
1239 $_ = $text;
1240 s/_/ /g;
1241 s/=([0-9A-F]{2})/chr(hex($1))/egi;
1242 } else {
1243 # other encodings not supported yet
1244 }
Роман Донченко11f70a72014-12-14 18:59:46 +03001245 }
Роман Донченкоab47e2a2014-12-14 18:59:47 +03001246 join '', @words;
Thomas Rastb622d4d2012-07-30 21:25:40 +02001247 }eg;
Роман Донченко11f70a72014-12-14 18:59:46 +03001248 return wantarray ? ($_, $charset) : $_;
Jürgen Rühle374c5902007-01-10 13:36:39 -08001249}
1250
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001251sub quote_rfc2047 {
1252 local $_ = shift;
Brandon Caseyd1fff6f2009-06-06 20:12:31 -05001253 my $encoding = shift || 'UTF-8';
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001254 s/([^-a-zA-Z0-9!*+\/])/sprintf("=%02X", ord($1))/eg;
1255 s/(.*)/=\?$encoding\?q\?$1\?=/;
1256 return $_;
1257}
1258
Brandon Caseya3a82622009-06-07 19:25:58 -05001259sub is_rfc2047_quoted {
1260 my $s = shift;
Brandon Caseya3a82622009-06-07 19:25:58 -05001261 length($s) <= 75 &&
Роман Донченко11f70a72014-12-14 18:59:46 +03001262 $s =~ m/^(?:"[[:ascii:]]*"|$re_encoded_word)$/o;
Brandon Caseya3a82622009-06-07 19:25:58 -05001263}
1264
Krzysztof Mazurce547802012-10-24 23:08:26 +02001265sub subject_needs_rfc2047_quoting {
1266 my $s = shift;
1267
Krzysztof Mazurce1459f2012-10-24 23:28:29 +02001268 return ($s =~ /[^[:ascii:]]/) || ($s =~ /=\?/);
Krzysztof Mazurce547802012-10-24 23:08:26 +02001269}
1270
1271sub quote_subject {
1272 local $subject = shift;
1273 my $encoding = shift || 'UTF-8';
1274
1275 if (subject_needs_rfc2047_quoting($subject)) {
1276 return quote_rfc2047($subject, $encoding);
1277 }
1278 return $subject;
1279}
1280
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001281# use the simplest quoting being able to handle the recipient
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001282sub sanitize_address {
Robin H. Johnson732263d2007-04-25 19:37:19 -07001283 my ($recipient) = @_;
Krzysztof Mazur831a4882012-11-22 19:12:08 +01001284
1285 # remove garbage after email address
1286 $recipient =~ s/(.*>).*$/$1/;
1287
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001288 my ($recipient_name, $recipient_addr) = ($recipient =~ /^(.*?)\s*(<.*)/);
1289
1290 if (not $recipient_name) {
Ævar Arnfjörð Bjarmasonff483892010-09-30 13:43:02 +00001291 return $recipient;
Robin H. Johnson732263d2007-04-25 19:37:19 -07001292 }
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001293
1294 # if recipient_name is already quoted, do nothing
Brandon Caseya3a82622009-06-07 19:25:58 -05001295 if (is_rfc2047_quoted($recipient_name)) {
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001296 return $recipient;
1297 }
1298
Remi Lespinet1fe97032015-06-30 14:16:49 +02001299 # remove non-escaped quotes
1300 $recipient_name =~ s/(^|[^\\])"/$1/g;
1301
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001302 # rfc2047 is needed if a non-ascii char is included
1303 if ($recipient_name =~ /[^[:ascii:]]/) {
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001304 $recipient_name = quote_rfc2047($recipient_name);
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001305 }
1306
1307 # double quotes are needed if specials or CTLs are included
1308 elsif ($recipient_name =~ /[][()<>@,;:\\".\000-\037\177]/) {
Remi Lespinet1fe97032015-06-30 14:16:49 +02001309 $recipient_name =~ s/([\\\r])/\\$1/g;
Ævar Arnfjörð Bjarmasond5c7d692010-09-30 13:43:03 +00001310 $recipient_name = qq["$recipient_name"];
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001311 }
1312
1313 return "$recipient_name $recipient_addr";
1314
Robin H. Johnson732263d2007-04-25 19:37:19 -07001315}
1316
Matthieu Moycb2922f2017-08-23 12:21:01 +02001317sub strip_garbage_one_address {
1318 my ($addr) = @_;
1319 chomp $addr;
1320 if ($addr =~ /^(("[^"]*"|[^"<]*)? *<[^>]*>).*/) {
1321 # "Foo Bar" <foobar@example.com> [possibly garbage here]
1322 # Foo Bar <foobar@example.com> [possibly garbage here]
1323 return $1;
1324 }
1325 if ($addr =~ /^(<[^>]*>).*/) {
1326 # <foo@example.com> [possibly garbage here]
1327 # if garbage contains other addresses, they are ignored.
1328 return $1;
1329 }
1330 if ($addr =~ /^([^"#,\s]*)/) {
1331 # address without quoting: remove anything after the address
1332 return $1;
1333 }
1334 return $addr;
1335}
1336
Krzysztof Mazure4312252012-11-22 19:12:10 +01001337sub sanitize_address_list {
1338 return (map { sanitize_address($_) } @_);
1339}
1340
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001341sub process_address_list {
Remi Lespinetb1c8a112015-06-30 14:16:50 +02001342 my @addr_list = map { parse_address_line($_) } @_;
1343 @addr_list = expand_aliases(@addr_list);
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001344 @addr_list = sanitize_address_list(@addr_list);
1345 @addr_list = validate_address_list(@addr_list);
1346 return @addr_list;
1347}
1348
Jari Aalto134550f2010-03-14 17:16:45 +02001349# Returns the local Fully Qualified Domain Name (FQDN) if available.
1350#
1351# Tightly configured MTAa require that a caller sends a real DNS
1352# domain name that corresponds the IP address in the HELO/EHLO
1353# handshake. This is used to verify the connection and prevent
1354# spammers from trying to hide their identity. If the DNS and IP don't
Elijah Newren15beaaa2019-11-05 17:07:23 +00001355# match, the receiving MTA may deny the connection.
Jari Aalto134550f2010-03-14 17:16:45 +02001356#
1357# Here is a deny example of Net::SMTP with the default "localhost.localdomain"
1358#
1359# Net::SMTP=GLOB(0x267ec28)>>> EHLO localhost.localdomain
1360# Net::SMTP=GLOB(0x267ec28)<<< 550 EHLO argument does not match calling host
1361#
1362# This maildomain*() code is based on ideas in Perl library Test::Reporter
1363# /usr/share/perl5/Test/Reporter/Mail/Util.pm ==> sub _maildomain ()
1364
Brian Gernhardt59a86302010-04-10 10:53:54 -04001365sub valid_fqdn {
1366 my $domain = shift;
Brandon Casey61ef5e92010-09-26 22:18:01 -05001367 return defined $domain && !($^O eq 'darwin' && $domain =~ /\.local$/) && $domain =~ /\./;
Brian Gernhardt59a86302010-04-10 10:53:54 -04001368}
1369
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001370sub maildomain_net {
Jari Aalto134550f2010-03-14 17:16:45 +02001371 my $maildomain;
1372
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +02001373 require Net::Domain;
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001374 my $domain = Net::Domain::domainname();
1375 $maildomain = $domain if valid_fqdn($domain);
Jari Aalto134550f2010-03-14 17:16:45 +02001376
1377 return $maildomain;
1378}
1379
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001380sub maildomain_mta {
Jari Aalto134550f2010-03-14 17:16:45 +02001381 my $maildomain;
1382
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001383 for my $host (qw(mailhost localhost)) {
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +02001384 require Net::SMTP;
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001385 my $smtp = Net::SMTP->new($host);
1386 if (defined $smtp) {
1387 my $domain = $smtp->domain;
1388 $smtp->quit;
Jari Aalto134550f2010-03-14 17:16:45 +02001389
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001390 $maildomain = $domain if valid_fqdn($domain);
Jari Aalto134550f2010-03-14 17:16:45 +02001391
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001392 last if $maildomain;
Jari Aalto134550f2010-03-14 17:16:45 +02001393 }
1394 }
1395
1396 return $maildomain;
1397}
1398
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001399sub maildomain {
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001400 return maildomain_net() || maildomain_mta() || 'localhost.localdomain';
Jari Aalto134550f2010-03-14 17:16:45 +02001401}
1402
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001403sub smtp_host_string {
1404 if (defined $smtp_server_port) {
1405 return "$smtp_server:$smtp_server_port";
1406 } else {
1407 return $smtp_server;
1408 }
1409}
1410
1411# Returns 1 if authentication succeeded or was not necessary
1412# (smtp_user was not specified), and 0 otherwise.
1413
1414sub smtp_auth_maybe {
Joshua Watt8dd9b3f2018-10-22 22:24:25 -05001415 if (!defined $smtp_authuser || $auth || (defined $smtp_auth && $smtp_auth eq "none")) {
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001416 return 1;
1417 }
1418
1419 # Workaround AUTH PLAIN/LOGIN interaction defect
1420 # with Authen::SASL::Cyrus
1421 eval {
1422 require Authen::SASL;
1423 Authen::SASL->import(qw(Perl));
1424 };
1425
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001426 # Check mechanism naming as defined in:
1427 # https://tools.ietf.org/html/rfc4422#page-8
Brian Norris904f6e72015-09-18 15:12:50 -07001428 if ($smtp_auth && $smtp_auth !~ /^(\b[A-Z0-9-_]{1,20}\s*)*$/) {
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001429 die "invalid smtp auth: '${smtp_auth}'";
1430 }
1431
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001432 # TODO: Authentication may fail not because credentials were
1433 # invalid but due to other reasons, in which we should not
1434 # reject credentials.
1435 $auth = Git::credential({
1436 'protocol' => 'smtp',
1437 'host' => smtp_host_string(),
1438 'username' => $smtp_authuser,
1439 # if there's no password, "git credential fill" will
1440 # give us one, otherwise it'll just pass this one.
1441 'password' => $smtp_authpass
1442 }, sub {
1443 my $cred = shift;
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001444
1445 if ($smtp_auth) {
1446 my $sasl = Authen::SASL->new(
1447 mechanism => $smtp_auth,
1448 callback => {
1449 user => $cred->{'username'},
1450 pass => $cred->{'password'},
1451 authname => $cred->{'username'},
1452 }
1453 );
1454
1455 return !!$smtp->auth($sasl);
1456 }
1457
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001458 return !!$smtp->auth($cred->{'username'}, $cred->{'password'});
1459 });
1460
1461 return $auth;
1462}
1463
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001464sub ssl_verify_params {
1465 eval {
1466 require IO::Socket::SSL;
1467 IO::Socket::SSL->import(qw/SSL_VERIFY_PEER SSL_VERIFY_NONE/);
1468 };
1469 if ($@) {
1470 print STDERR "Not using SSL_VERIFY_PEER due to out-of-date IO::Socket::SSL.\n";
1471 return;
1472 }
1473
1474 if (!defined $smtp_ssl_cert_path) {
Ruben Kerkhof01645b72014-01-15 21:31:11 +04001475 # use the OpenSSL defaults
1476 return (SSL_verify_mode => SSL_VERIFY_PEER());
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001477 }
1478
1479 if ($smtp_ssl_cert_path eq "") {
1480 return (SSL_verify_mode => SSL_VERIFY_NONE());
1481 } elsif (-d $smtp_ssl_cert_path) {
1482 return (SSL_verify_mode => SSL_VERIFY_PEER(),
1483 SSL_ca_path => $smtp_ssl_cert_path);
1484 } elsif (-f $smtp_ssl_cert_path) {
1485 return (SSL_verify_mode => SSL_VERIFY_PEER(),
1486 SSL_ca_file => $smtp_ssl_cert_path);
1487 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001488 die sprintf(__("CA path \"%s\" does not exist"), $smtp_ssl_cert_path);
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001489 }
1490}
1491
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001492sub file_name_is_absolute {
1493 my ($path) = @_;
1494
1495 # msys does not grok DOS drive-prefixes
1496 if ($^O eq 'msys') {
Junio C Hamanof24ecf52014-04-23 09:37:38 -07001497 return ($path =~ m#^/# || $path =~ m#^[a-zA-Z]\:#)
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001498 }
1499
1500 require File::Spec::Functions;
1501 return File::Spec::Functions::file_name_is_absolute($path);
1502}
1503
Michael Strawbridge56addda2023-04-19 16:27:02 -04001504sub gen_header {
Eric Wong4bc87a22006-03-25 17:20:48 -08001505 my @recipients = unique_email_list(@to);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001506 @cc = (grep { my $cc = extract_valid_address_or_die($_);
Joe Perches83acaae2010-11-20 15:06:05 -08001507 not grep { $cc eq $_ || $_ =~ /<\Q${cc}\E>$/ } @recipients
Ask Bjørn Hansen7ac17522007-11-19 03:00:26 -08001508 }
Ask Bjørn Hansen7ac17522007-11-19 03:00:26 -08001509 @cc);
Eric Wong4bc87a22006-03-25 17:20:48 -08001510 my $to = join (",\n\t", @recipients);
Ævar Arnfjörð Bjarmasone60f6d12019-05-09 13:48:29 +02001511 @recipients = unique_email_list(@recipients,@cc,@initial_bcc);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001512 @recipients = (map { extract_valid_address_or_die($_) } @recipients);
Jakub Narebski6bdca892006-07-07 20:57:55 +02001513 my $date = format_2822_time($time++);
Martin Langhoffe923eff2006-05-03 09:44:36 +12001514 my $gitversion = '@@GIT_VERSION@@';
1515 if ($gitversion =~ m/..GIT_VERSION../) {
Petr Baudis3cb8caf2006-07-03 22:47:58 +02001516 $gitversion = Git::version();
Martin Langhoffe923eff2006-05-03 09:44:36 +12001517 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001518
Joe Perches02461e02009-10-08 10:03:26 -07001519 my $cc = join(",\n\t", unique_email_list(@cc));
Junio C Hamanof06a6a42007-04-16 16:51:47 -07001520 my $ccline = "";
1521 if ($cc ne '') {
1522 $ccline = "\nCc: $cc";
1523 }
Jeff King4f3d3702007-12-17 15:51:34 -05001524 make_message_id() unless defined($message_id);
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001525
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001526 my $header = "From: $sender
Junio C Hamanof06a6a42007-04-16 16:51:47 -07001527To: $to${ccline}
Eric Wong4bc87a22006-03-25 17:20:48 -08001528Subject: $subject
Eric Wong4bc87a22006-03-25 17:20:48 -08001529Date: $date
Junio C Hamanoba4324c2022-12-16 10:47:19 +09001530Message-ID: $message_id
Eric Wong4bc87a22006-03-25 17:20:48 -08001531";
Luis Henriquesac1596a2014-03-24 21:38:27 +00001532 if ($use_xmailer) {
1533 $header .= "X-Mailer: git-send-email $gitversion\n";
1534 }
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001535 if ($in_reply_to) {
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001536
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001537 $header .= "In-Reply-To: $in_reply_to\n";
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001538 $header .= "References: $references\n";
1539 }
Christian Ludwigd11c9432018-03-04 00:58:14 +01001540 if ($reply_to) {
1541 $header .= "Reply-To: $reply_to\n";
1542 }
Junio C Hamanoce91c2f2006-10-05 16:36:49 -07001543 if (@xh) {
1544 $header .= join("\n", @xh) . "\n";
1545 }
Michael Strawbridge56addda2023-04-19 16:27:02 -04001546 my $recipients_ref = \@recipients;
1547 return ($recipients_ref, $to, $date, $gitversion, $cc, $ccline, $header);
1548}
1549
1550# Prepares the email, then asks the user what to do.
1551#
1552# If the user chooses to send the email, it's sent and 1 is returned.
1553# If the user chooses not to send the email, 0 is returned.
1554# If the user decides they want to make further edits, -1 is returned and the
1555# caller is expected to call send_message again after the edits are performed.
1556#
1557# If an error occurs sending the email, this just dies.
1558
1559sub send_message {
1560 my ($recipients_ref, $to, $date, $gitversion, $cc, $ccline, $header) = gen_header();
1561 my @recipients = @$recipients_ref;
Ryan Anderson83b24432005-07-31 04:17:25 -04001562
Robin H. Johnsonc38f0242007-04-25 19:37:20 -07001563 my @sendmail_parameters = ('-i', @recipients);
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001564 my $raw_from = $sender;
Felipe Contrerasc89e3242009-11-26 21:04:29 +02001565 if (defined $envelope_sender && $envelope_sender ne "auto") {
1566 $raw_from = $envelope_sender;
1567 }
Robin H. Johnsonf073a592007-04-25 19:37:22 -07001568 $raw_from = extract_valid_address($raw_from);
1569 unshift (@sendmail_parameters,
1570 '-f', $raw_from) if(defined $envelope_sender);
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001571
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001572 if ($needs_confirm && !$dry_run) {
1573 print "\n$header\n";
1574 if ($needs_confirm eq "inform") {
1575 $confirm_unconfigured = 0; # squelch this message for the rest of this run
Jay Soffian6e182512009-03-28 21:39:10 -04001576 $ask_default = "y"; # assume yes on EOF since user hasn't explicitly asked for confirmation
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001577 print __ <<EOF ;
1578 The Cc list above has been expanded by additional
1579 addresses found in the patch commit message. By default
1580 send-email prompts before sending whenever this occurs.
1581 This behavior is controlled by the sendemail.confirm
1582 configuration setting.
1583
1584 For additional information, run 'git send-email --help'.
1585 To retain the current behavior, but squelch this message,
1586 run 'git config --global sendemail.confirm auto'.
1587
1588EOF
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001589 }
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001590 # TRANSLATORS: Make sure to include [y] [n] [e] [q] [a] in your
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001591 # translation. The program will only accept English input
1592 # at this point.
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001593 $_ = ask(__("Send this email? ([y]es|[n]o|[e]dit|[q]uit|[a]ll): "),
1594 valid_re => qr/^(?:yes|y|no|n|edit|e|quit|q|all|a)/i,
Jay Soffian6e182512009-03-28 21:39:10 -04001595 default => $ask_default);
Vasco Almeida46493102016-12-14 11:54:36 -01001596 die __("Send this email reply required") unless defined $_;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001597 if (/^n/i) {
Michael Witten15da1082009-04-13 13:23:51 -05001598 return 0;
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001599 } elsif (/^e/i) {
1600 return -1;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001601 } elsif (/^q/i) {
1602 cleanup_compose_files();
1603 exit(0);
1604 } elsif (/^a/i) {
1605 $confirm = 'never';
1606 }
1607 }
1608
Pascal Obry052fbea2010-09-06 20:12:11 +02001609 unshift (@sendmail_parameters, @smtp_server_options);
1610
Matthew Wilcox61302592006-10-10 08:58:23 -06001611 if ($dry_run) {
1612 # We don't want to send the email.
Gregory Anderscd5b33f2021-05-14 09:15:53 -06001613 } elsif (defined $sendmail_cmd || file_name_is_absolute($smtp_server)) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001614 my $pid = open my $sm, '|-';
1615 defined $pid or die $!;
1616 if (!$pid) {
Gregory Anderscd5b33f2021-05-14 09:15:53 -06001617 if (defined $sendmail_cmd) {
1618 exec ("sh", "-c", "$sendmail_cmd \"\$@\"", "-", @sendmail_parameters)
1619 or die $!;
1620 } else {
1621 exec ($smtp_server, @sendmail_parameters)
1622 or die $!;
1623 }
Eric Wongaca7ad72006-05-15 02:34:44 -07001624 }
1625 print $sm "$header\n$message";
Ævar Arnfjörð Bjarmason5e2c2ab2010-09-30 13:43:07 +00001626 close $sm or die $!;
Eric Wongaca7ad72006-05-15 02:34:44 -07001627 } else {
Junio C Hamano44b24762007-09-25 17:27:54 -07001628
1629 if (!defined $smtp_server) {
Vasco Almeida46493102016-12-14 11:54:36 -01001630 die __("The required SMTP server is not properly defined.")
Junio C Hamano44b24762007-09-25 17:27:54 -07001631 }
1632
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001633 require Net::SMTP;
Jonathan Niederbfbfc9a2017-05-31 17:17:43 -07001634 my $use_net_smtp_ssl = version->parse($Net::SMTP::VERSION) < version->parse("2.34");
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001635 $smtp_domain ||= maildomain();
1636
Thomas Rastf6bebd12008-06-25 21:42:43 +02001637 if ($smtp_encryption eq 'ssl') {
Junio C Hamano44b24762007-09-25 17:27:54 -07001638 $smtp_server_port ||= 465; # ssmtp
Thomas Rast5508f3e2013-12-01 23:48:43 +01001639 require IO::Socket::SSL;
John Keeping9d605242015-12-03 21:47:18 +00001640
1641 # Suppress "variable accessed once" warning.
1642 {
1643 no warnings 'once';
1644 $IO::Socket::SSL::DEBUG = 1;
1645 }
1646
Thomas Rast5508f3e2013-12-01 23:48:43 +01001647 # Net::SMTP::SSL->new() does not forward any SSL options
1648 IO::Socket::SSL::set_client_defaults(
1649 ssl_verify_params());
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001650
1651 if ($use_net_smtp_ssl) {
1652 require Net::SMTP::SSL;
1653 $smtp ||= Net::SMTP::SSL->new($smtp_server,
1654 Hello => $smtp_domain,
1655 Port => $smtp_server_port,
1656 Debug => $debug_net_smtp);
1657 }
1658 else {
1659 $smtp ||= Net::SMTP->new($smtp_server,
1660 Hello => $smtp_domain,
1661 Port => $smtp_server_port,
1662 Debug => $debug_net_smtp,
1663 SSL => 1);
1664 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001665 }
Jules Maselbas636f3d72018-07-14 10:58:48 +02001666 elsif (!$smtp) {
brian m. carlson1a741bf2013-07-04 22:04:52 +00001667 $smtp_server_port ||= 25;
1668 $smtp ||= Net::SMTP->new($smtp_server,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001669 Hello => $smtp_domain,
brian m. carlson1a741bf2013-07-04 22:04:52 +00001670 Debug => $debug_net_smtp,
1671 Port => $smtp_server_port);
Yakov Lernerfb3650e2009-09-25 15:10:21 -07001672 if ($smtp_encryption eq 'tls' && $smtp) {
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001673 if ($use_net_smtp_ssl) {
1674 $smtp->command('STARTTLS');
1675 $smtp->response();
1676 if ($smtp->code != 220) {
1677 die sprintf(__("Server does not support STARTTLS! %s"), $smtp->message);
1678 }
1679 require Net::SMTP::SSL;
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001680 $smtp = Net::SMTP::SSL->start_SSL($smtp,
1681 ssl_verify_params())
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001682 or die sprintf(__("STARTTLS failed! %s"), IO::Socket::SSL::errstr());
Thomas Rastf6bebd12008-06-25 21:42:43 +02001683 }
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001684 else {
1685 $smtp->starttls(ssl_verify_params())
1686 or die sprintf(__("STARTTLS failed! %s"), IO::Socket::SSL::errstr());
1687 }
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001688 # Send EHLO again to receive fresh
1689 # supported commands
1690 $smtp->hello($smtp_domain);
Thomas Rastf6bebd12008-06-25 21:42:43 +02001691 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001692 }
Junio C Hamano44b24762007-09-25 17:27:54 -07001693
1694 if (!$smtp) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001695 die __("Unable to initialize SMTP properly. Check config and use --smtp-debug."),
1696 " VALUES: server=$smtp_server ",
Jari Aaltoe5afb3a2010-03-14 17:15:33 +02001697 "encryption=$smtp_encryption ",
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001698 "hello=$smtp_domain",
Sylvain Rabota1dd7e12011-04-29 20:23:24 +02001699 defined $smtp_server_port ? " port=$smtp_server_port" : "";
Junio C Hamano44b24762007-09-25 17:27:54 -07001700 }
1701
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001702 smtp_auth_maybe or die $smtp->message;
Michael Witten2363d742008-02-03 19:53:56 -05001703
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001704 $smtp->mail( $raw_from ) or die $smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001705 $smtp->to( @recipients ) or die $smtp->message;
1706 $smtp->data or die $smtp->message;
Stefan Agnerf60c4832015-09-30 09:26:09 +02001707 $smtp->datasend("$header\n") or die $smtp->message;
1708 my @lines = split /^/, $message;
1709 foreach my $line (@lines) {
1710 $smtp->datasend("$line") or die $smtp->message;
1711 }
Eric Wongaca7ad72006-05-15 02:34:44 -07001712 $smtp->dataend() or die $smtp->message;
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001713 $smtp->code =~ /250|200/ or die sprintf(__("Failed to send %s\n"), $subject).$smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001714 }
Ryan Anderson27184352006-02-05 20:13:52 -05001715 if ($quiet) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001716 printf($dry_run ? __("Dry-Sent %s\n") : __("Sent %s\n"), $subject);
Ryan Anderson27184352006-02-05 20:13:52 -05001717 } else {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001718 print($dry_run ? __("Dry-OK. Log says:\n") : __("OK. Log says:\n"));
Gregory Anderscd5b33f2021-05-14 09:15:53 -06001719 if (!defined $sendmail_cmd && !file_name_is_absolute($smtp_server)) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001720 print "Server: $smtp_server\n";
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001721 print "MAIL FROM:<$raw_from>\n";
Joe Perches02461e02009-10-08 10:03:26 -07001722 foreach my $entry (@recipients) {
1723 print "RCPT TO:<$entry>\n";
1724 }
Eric Wongaca7ad72006-05-15 02:34:44 -07001725 } else {
Gregory Anderscd5b33f2021-05-14 09:15:53 -06001726 my $sm;
1727 if (defined $sendmail_cmd) {
1728 $sm = $sendmail_cmd;
1729 } else {
1730 $sm = $smtp_server;
1731 }
1732
1733 print "Sendmail: $sm ".join(' ',@sendmail_parameters)."\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001734 }
David D. Kilzerb7f30e02007-11-18 20:14:55 -08001735 print $header, "\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001736 if ($smtp) {
Vasco Almeida46493102016-12-14 11:54:36 -01001737 print __("Result: "), $smtp->code, ' ',
Eric Wongaca7ad72006-05-15 02:34:44 -07001738 ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
1739 } else {
Vasco Almeida46493102016-12-14 11:54:36 -01001740 print __("Result: OK\n");
Eric Wongaca7ad72006-05-15 02:34:44 -07001741 }
Ryan Anderson30d08b32006-02-02 11:56:06 -05001742 }
Michael Witten15da1082009-04-13 13:23:51 -05001743
1744 return 1;
Ryan Anderson83b24432005-07-31 04:17:25 -04001745}
1746
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001747$in_reply_to = $initial_in_reply_to;
1748$references = $initial_in_reply_to || '';
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001749$message_num = 0;
Ryan Anderson83b24432005-07-31 04:17:25 -04001750
Michael Strawbridge56addda2023-04-19 16:27:02 -04001751sub pre_process_file {
1752 my ($t, $quiet) = @_;
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001753
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001754 open my $fh, "<", $t or die sprintf(__("can't open file %s"), $t);
Ryan Anderson83b24432005-07-31 04:17:25 -04001755
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001756 my $author = undef;
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001757 my $sauthor = undef;
Jeff King8291db62007-11-16 05:49:09 -05001758 my $author_encoding;
1759 my $has_content_type;
1760 my $body_encoding;
Paolo Bonzinibb294562014-11-25 15:00:26 +01001761 my $xfer_encoding;
1762 my $has_mime_version;
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001763 @to = ();
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001764 @cc = ();
Junio C Hamanoce91c2f2006-10-05 16:36:49 -07001765 @xh = ();
Junio C Hamanoe6b09642006-10-07 03:09:05 -07001766 my $input_format = undef;
Jay Soffian50126992009-02-14 23:32:14 -05001767 my @header = ();
Marvin Häusere0821132021-08-30 15:30:01 +00001768 $subject = $initial_subject;
Ryan Anderson83b24432005-07-31 04:17:25 -04001769 $message = "";
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001770 $message_num++;
Jay Soffian50126992009-02-14 23:32:14 -05001771 # First unfold multiline header fields
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001772 while(<$fh>) {
Jay Soffian50126992009-02-14 23:32:14 -05001773 last if /^\s*$/;
1774 if (/^\s+\S/ and @header) {
1775 chomp($header[$#header]);
1776 s/^\s+/ /;
1777 $header[$#header] .= $_;
1778 } else {
1779 push(@header, $_);
1780 }
1781 }
1782 # Now parse the header
1783 foreach(@header) {
1784 if (/^From /) {
1785 $input_format = 'mbox';
1786 next;
1787 }
1788 chomp;
1789 if (!defined $input_format && /^[-A-Za-z]+:\s/) {
1790 $input_format = 'mbox';
1791 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001792
Jay Soffian50126992009-02-14 23:32:14 -05001793 if (defined $input_format && $input_format eq 'mbox') {
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001794 if (/^Subject:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001795 $subject = $1;
1796 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001797 elsif (/^From:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001798 ($author, $author_encoding) = unquote_rfc2047($1);
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001799 $sauthor = sanitize_address($author);
Jay Soffian50126992009-02-14 23:32:14 -05001800 next if $suppress_cc{'author'};
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001801 next if $suppress_cc{'self'} and $sauthor eq $sender;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001802 printf(__("(mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001803 $1, $_) unless $quiet;
1804 push @cc, $1;
1805 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001806 elsif (/^To:\s+(.*)$/i) {
Stephen Boyd21802cd2010-09-29 00:26:44 -07001807 foreach my $addr (parse_address_line($1)) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001808 printf(__("(mbox) Adding to: %s from line '%s'\n"),
Stephen Boyd21802cd2010-09-29 00:26:44 -07001809 $addr, $_) unless $quiet;
Krzysztof Mazure4312252012-11-22 19:12:10 +01001810 push @to, $addr;
Stephen Boyd21802cd2010-09-29 00:26:44 -07001811 }
1812 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001813 elsif (/^Cc:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001814 foreach my $addr (parse_address_line($1)) {
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001815 my $qaddr = unquote_rfc2047($addr);
1816 my $saddr = sanitize_address($qaddr);
1817 if ($saddr eq $sender) {
David Brown65648282007-12-25 19:56:29 -08001818 next if ($suppress_cc{'self'});
David Brown65648282007-12-25 19:56:29 -08001819 } else {
1820 next if ($suppress_cc{'cc'});
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001821 }
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001822 printf(__("(mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001823 $addr, $_) unless $quiet;
1824 push @cc, $addr;
Ryan Anderson83b24432005-07-31 04:17:25 -04001825 }
1826 }
Jay Soffian50126992009-02-14 23:32:14 -05001827 elsif (/^Content-type:/i) {
1828 $has_content_type = 1;
1829 if (/charset="?([^ "]+)/) {
1830 $body_encoding = $1;
1831 }
1832 push @xh, $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001833 }
Paolo Bonzinibb294562014-11-25 15:00:26 +01001834 elsif (/^MIME-Version/i) {
1835 $has_mime_version = 1;
1836 push @xh, $_;
1837 }
Junio C Hamanoba4324c2022-12-16 10:47:19 +09001838 elsif (/^Message-ID: (.*)/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001839 $message_id = $1;
1840 }
Paolo Bonzinibb294562014-11-25 15:00:26 +01001841 elsif (/^Content-Transfer-Encoding: (.*)/i) {
1842 $xfer_encoding = $1 if not defined $xfer_encoding;
1843 }
Stefan Agner256be1d2018-04-17 23:16:30 +02001844 elsif (/^In-Reply-To: (.*)/i) {
Rafael Aquinif9f60d72020-06-29 10:11:04 -04001845 if (!$initial_in_reply_to || $thread) {
1846 $in_reply_to = $1;
1847 }
Stefan Agner256be1d2018-04-17 23:16:30 +02001848 }
1849 elsif (/^References: (.*)/i) {
Rafael Aquinif9f60d72020-06-29 10:11:04 -04001850 if (!$initial_in_reply_to || $thread) {
1851 $references = $1;
1852 }
Stefan Agner256be1d2018-04-17 23:16:30 +02001853 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001854 elsif (!/^Date:\s/i && /^[-A-Za-z]+:\s+\S/) {
Jay Soffian50126992009-02-14 23:32:14 -05001855 push @xh, $_;
1856 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001857 } else {
Jay Soffian50126992009-02-14 23:32:14 -05001858 # In the traditional
1859 # "send lots of email" format,
1860 # line 1 = cc
1861 # line 2 = subject
1862 # So let's support that, too.
1863 $input_format = 'lots';
1864 if (@cc == 0 && !$suppress_cc{'cc'}) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001865 printf(__("(non-mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001866 $_, $_) unless $quiet;
1867 push @cc, $_;
1868 } elsif (!defined $subject) {
1869 $subject = $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001870 }
1871 }
1872 }
Jay Soffian50126992009-02-14 23:32:14 -05001873 # Now parse the message body
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001874 while(<$fh>) {
Jay Soffian50126992009-02-14 23:32:14 -05001875 $message .= $_;
Baruch Siach9c545812019-03-16 21:26:50 +02001876 if (/^([a-z][a-z-]*-by|Cc): (.*)/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001877 chomp;
Jay Soffian3531e272009-02-14 23:32:15 -05001878 my ($what, $c) = ($1, $2);
Matthieu Moycb2922f2017-08-23 12:21:01 +02001879 # strip garbage for the address we'll use:
1880 $c = strip_garbage_one_address($c);
1881 # sanitize a bit more to decide whether to suppress the address:
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001882 my $sc = sanitize_address($c);
1883 if ($sc eq $sender) {
Jay Soffian3531e272009-02-14 23:32:15 -05001884 next if ($suppress_cc{'self'});
1885 } else {
Rasmus Villemoesef0cc1d2018-10-16 09:39:23 +02001886 if ($what =~ /^Signed-off-by$/i) {
1887 next if $suppress_cc{'sob'};
1888 } elsif ($what =~ /-by$/i) {
1889 next if $suppress_cc{'misc-by'};
1890 } elsif ($what =~ /Cc/i) {
1891 next if $suppress_cc{'bodycc'};
1892 }
Jay Soffian3531e272009-02-14 23:32:15 -05001893 }
Rasmus Villemoesfb34fe62018-10-10 13:13:50 +02001894 if ($c !~ /.+@.+|<.+>/) {
1895 printf("(body) Ignoring %s from line '%s'\n",
1896 $what, $_) unless $quiet;
1897 next;
1898 }
Jay Soffian50126992009-02-14 23:32:14 -05001899 push @cc, $c;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001900 printf(__("(body) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001901 $c, $_) unless $quiet;
1902 }
1903 }
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001904 close $fh;
Joe Perches324a8bd2007-08-17 18:51:12 -07001905
Michael Strawbridge56addda2023-04-19 16:27:02 -04001906 push @to, recipients_cmd("to-cmd", "to", $to_cmd, $t, $quiet)
Joe Perches6e74e072010-09-24 10:03:00 -07001907 if defined $to_cmd;
Michael Strawbridge56addda2023-04-19 16:27:02 -04001908 push @cc, recipients_cmd("cc-cmd", "cc", $cc_cmd, $t, $quiet)
Joe Perches6e74e072010-09-24 10:03:00 -07001909 if defined $cc_cmd && !$suppress_cc{'cccmd'};
Joe Perches324a8bd2007-08-17 18:51:12 -07001910
Thomas Rast3cae7e52010-06-17 22:10:39 +02001911 if ($broken_encoding{$t} && !$has_content_type) {
Paolo Bonzinibb294562014-11-25 15:00:26 +01001912 $xfer_encoding = '8bit' if not defined $xfer_encoding;
Thomas Rast3cae7e52010-06-17 22:10:39 +02001913 $has_content_type = 1;
Paolo Bonzinibb294562014-11-25 15:00:26 +01001914 push @xh, "Content-Type: text/plain; charset=$auto_8bit_encoding";
Thomas Rast3cae7e52010-06-17 22:10:39 +02001915 $body_encoding = $auto_8bit_encoding;
1916 }
1917
Krzysztof Mazurce547802012-10-24 23:08:26 +02001918 if ($broken_encoding{$t} && !is_rfc2047_quoted($subject)) {
1919 $subject = quote_subject($subject, $auto_8bit_encoding);
Thomas Rast3cae7e52010-06-17 22:10:39 +02001920 }
1921
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001922 if (defined $sauthor and $sauthor ne $sender) {
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001923 $message = "From: $author\n\n$message";
Jeff King8291db62007-11-16 05:49:09 -05001924 if (defined $author_encoding) {
1925 if ($has_content_type) {
1926 if ($body_encoding eq $author_encoding) {
1927 # ok, we already have the right encoding
1928 }
1929 else {
1930 # uh oh, we should re-encode
1931 }
1932 }
1933 else {
Paolo Bonzinibb294562014-11-25 15:00:26 +01001934 $xfer_encoding = '8bit' if not defined $xfer_encoding;
Thomas Rast3cae7e52010-06-17 22:10:39 +02001935 $has_content_type = 1;
Jeff King8291db62007-11-16 05:49:09 -05001936 push @xh,
Paolo Bonzinibb294562014-11-25 15:00:26 +01001937 "Content-Type: text/plain; charset=$author_encoding";
Jeff King8291db62007-11-16 05:49:09 -05001938 }
1939 }
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001940 }
brian m. carlsone67a2282018-07-08 22:17:12 +00001941 $xfer_encoding = '8bit' if not defined $xfer_encoding;
1942 ($message, $xfer_encoding) = apply_transfer_encoding(
1943 $message, $xfer_encoding, $target_xfer_encoding);
1944 push @xh, "Content-Transfer-Encoding: $xfer_encoding";
1945 unshift @xh, 'MIME-Version: 1.0' unless $has_mime_version;
Ryan Anderson83b24432005-07-31 04:17:25 -04001946
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001947 $needs_confirm = (
1948 $confirm eq "always" or
1949 ($confirm =~ /^(?:auto|cc)$/ && @cc) or
1950 ($confirm =~ /^(?:auto|compose)$/ && $compose && $message_num == 1));
1951 $needs_confirm = "inform" if ($needs_confirm && $confirm_unconfigured && @cc);
1952
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001953 @to = process_address_list(@to);
1954 @cc = process_address_list(@cc);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001955
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001956 @to = (@initial_to, @to);
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001957 @cc = (@initial_cc, @cc);
1958
Michael S. Tsirkinf515c902014-04-29 08:41:16 +03001959 if ($message_num == 1) {
1960 if (defined $cover_cc and $cover_cc) {
1961 @initial_cc = @cc;
1962 }
1963 if (defined $cover_to and $cover_to) {
1964 @initial_to = @to;
1965 }
1966 }
Michael Strawbridge56addda2023-04-19 16:27:02 -04001967}
1968
1969# Prepares the email, prompts the user, and sends it out
1970# Returns 0 if an edit was done and the function should be called again, or 1
1971# on the email being successfully sent out.
1972sub process_file {
1973 my ($t) = @_;
1974
1975 pre_process_file($t, $quiet);
Michael S. Tsirkinf515c902014-04-29 08:41:16 +03001976
Michael Witten15da1082009-04-13 13:23:51 -05001977 my $message_was_sent = send_message();
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001978 if ($message_was_sent == -1) {
1979 do_edit($t);
1980 return 0;
1981 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001982
1983 # set up for the next message
Marvin Häusere0821132021-08-30 15:30:01 +00001984 if ($thread) {
1985 if ($message_was_sent &&
1986 ($chain_reply_to || !defined $in_reply_to || length($in_reply_to) == 0 ||
1987 $message_num == 1)) {
1988 $in_reply_to = $message_id;
1989 if (length $references > 0) {
1990 $references .= "\n $message_id";
1991 } else {
1992 $references = "$message_id";
1993 }
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001994 }
Marvin Häusere0821132021-08-30 15:30:01 +00001995 } elsif (!defined $initial_in_reply_to) {
1996 # --thread and --in-reply-to manage the "In-Reply-To" header and by
1997 # extension the "References" header. If these commands are not used, reset
1998 # the header values to their defaults.
1999 $in_reply_to = undef;
2000 $references = '';
Ryan Anderson78488b22005-07-31 20:04:24 -04002001 }
Jeff King4f3d3702007-12-17 15:51:34 -05002002 $message_id = undef;
xiaoqiang zhao5453b832017-05-21 20:59:50 +08002003 $num_sent++;
2004 if (defined $batch_size && $num_sent == $batch_size) {
2005 $num_sent = 0;
2006 $smtp->quit if defined $smtp;
2007 undef $smtp;
2008 undef $auth;
2009 sleep($relogin_delay) if defined $relogin_delay;
2010 }
Drew DeVault04c4a4e2018-05-04 09:08:11 -04002011
2012 return 1;
2013}
2014
2015foreach my $t (@files) {
2016 while (!process_file($t)) {
2017 # user edited the file
2018 }
Ryan Anderson83b24432005-07-31 04:17:25 -04002019}
Ryan Andersone2057352005-08-02 21:45:22 -04002020
Joe Perches6e74e072010-09-24 10:03:00 -07002021# Execute a command (e.g. $to_cmd) to get a list of email addresses
2022# and return a results array
2023sub recipients_cmd {
Michael Strawbridge56addda2023-04-19 16:27:02 -04002024 my ($prefix, $what, $cmd, $file, $quiet) = @_;
Joe Perches6e74e072010-09-24 10:03:00 -07002025
Joe Perches6e74e072010-09-24 10:03:00 -07002026 my @addresses = ();
Ramkumar Ramachandraa47eab02013-03-31 18:40:42 -07002027 open my $fh, "-|", "$cmd \Q$file\E"
Vasco Almeida3c5cd202016-12-14 11:54:37 -01002028 or die sprintf(__("(%s) Could not execute '%s'"), $prefix, $cmd);
Junio C Hamano7ebee442010-10-26 22:02:52 -07002029 while (my $address = <$fh>) {
Joe Perches6e74e072010-09-24 10:03:00 -07002030 $address =~ s/^\s*//g;
2031 $address =~ s/\s*$//g;
2032 $address = sanitize_address($address);
Michael S. Tsirkinda187592013-06-05 21:11:00 +03002033 next if ($address eq $sender and $suppress_cc{'self'});
Joe Perches6e74e072010-09-24 10:03:00 -07002034 push @addresses, $address;
Vasco Almeida3c5cd202016-12-14 11:54:37 -01002035 printf(__("(%s) Adding %s: %s from: '%s'\n"),
2036 $prefix, $what, $address, $cmd) unless $quiet;
Joe Perches6e74e072010-09-24 10:03:00 -07002037 }
Junio C Hamano7ebee442010-10-26 22:02:52 -07002038 close $fh
Vasco Almeida3c5cd202016-12-14 11:54:37 -01002039 or die sprintf(__("(%s) failed to close pipe to '%s'"), $prefix, $cmd);
Joe Perches6e74e072010-09-24 10:03:00 -07002040 return @addresses;
2041}
2042
Jay Soffianc1f2aa42009-03-02 23:52:18 -05002043cleanup_compose_files();
Ryan Anderson1f038a02005-09-05 01:13:07 -04002044
Ævar Arnfjörð Bjarmason4bf597e2010-09-30 13:43:00 +00002045sub cleanup_compose_files {
Jay Soffianc1f2aa42009-03-02 23:52:18 -05002046 unlink($compose_filename, $compose_filename . ".final") if $compose;
Ryan Anderson1f038a02005-09-05 01:13:07 -04002047}
2048
Eric Wong4bc87a22006-03-25 17:20:48 -08002049$smtp->quit if $smtp;
Ryan Andersone2057352005-08-02 21:45:22 -04002050
Paolo Bonzini8d814082014-11-25 15:00:27 +01002051sub apply_transfer_encoding {
2052 my $message = shift;
2053 my $from = shift;
2054 my $to = shift;
2055
Aaron Lindsay3c88e462018-11-02 05:52:38 -04002056 return ($message, $to) if ($from eq $to and $from ne '7bit');
Paolo Bonzini8d814082014-11-25 15:00:27 +01002057
2058 require MIME::QuotedPrint;
2059 require MIME::Base64;
2060
2061 $message = MIME::QuotedPrint::decode($message)
2062 if ($from eq 'quoted-printable');
2063 $message = MIME::Base64::decode($message)
2064 if ($from eq 'base64');
2065
brian m. carlson74d76a12019-04-13 22:45:51 +00002066 $to = ($message =~ /(?:.{999,}|\r)/) ? 'quoted-printable' : '8bit'
brian m. carlson7a369872018-07-08 22:17:10 +00002067 if $to eq 'auto';
2068
Vasco Almeida46493102016-12-14 11:54:36 -01002069 die __("cannot send message as 7bit")
Paolo Bonzini8d814082014-11-25 15:00:27 +01002070 if ($to eq '7bit' and $message =~ /[^[:ascii:]]/);
brian m. carlson7a369872018-07-08 22:17:10 +00002071 return ($message, $to)
Paolo Bonzini8d814082014-11-25 15:00:27 +01002072 if ($to eq '7bit' or $to eq '8bit');
brian m. carlson7a369872018-07-08 22:17:10 +00002073 return (MIME::QuotedPrint::encode($message, "\n", 0), $to)
Paolo Bonzini8d814082014-11-25 15:00:27 +01002074 if ($to eq 'quoted-printable');
brian m. carlson7a369872018-07-08 22:17:10 +00002075 return (MIME::Base64::encode($message, "\n"), $to)
Paolo Bonzini8d814082014-11-25 15:00:27 +01002076 if ($to eq 'base64');
Vasco Almeida46493102016-12-14 11:54:36 -01002077 die __("invalid transfer encoding");
Paolo Bonzini8d814082014-11-25 15:00:27 +01002078}
2079
Ævar Arnfjörð Bjarmasonc438ea22010-09-30 13:42:59 +00002080sub unique_email_list {
Ryan Andersone2057352005-08-02 21:45:22 -04002081 my %seen;
2082 my @emails;
2083
2084 foreach my $entry (@_) {
Krzysztof Mazure4312252012-11-22 19:12:10 +01002085 my $clean = extract_valid_address_or_die($entry);
2086 $seen{$clean} ||= 0;
2087 next if $seen{$clean}++;
2088 push @emails, $entry;
Ryan Andersone2057352005-08-02 21:45:22 -04002089 }
2090 return @emails;
2091}
Jeff King747bbff2008-01-18 09:19:48 -05002092
2093sub validate_patch {
brian m. carlsonf2d06fb2018-07-08 22:17:11 +00002094 my ($fn, $xfer_encoding) = @_;
Jonathan Tan64896602017-05-12 15:38:26 -07002095
Jonathan Tan177409e2017-06-01 16:50:55 -07002096 if ($repo) {
Emily Shaffera7555302021-12-22 04:59:38 +01002097 my $hook_name = 'sendemail-validate';
Ævar Arnfjörð Bjarmason7cbc0452021-05-26 13:21:07 +02002098 my $hooks_path = $repo->command_oneline('rev-parse', '--git-path', 'hooks');
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +02002099 require File::Spec;
Emily Shaffera7555302021-12-22 04:59:38 +01002100 my $validate_hook = File::Spec->catfile($hooks_path, $hook_name);
Jonathan Tan177409e2017-06-01 16:50:55 -07002101 my $hook_error;
2102 if (-x $validate_hook) {
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +02002103 require Cwd;
2104 my $target = Cwd::abs_path($fn);
Jonathan Tan177409e2017-06-01 16:50:55 -07002105 # The hook needs a correct cwd and GIT_DIR.
Ævar Arnfjörð Bjarmason17530b22021-05-28 11:23:52 +02002106 my $cwd_save = Cwd::getcwd();
Jonathan Tan177409e2017-06-01 16:50:55 -07002107 chdir($repo->wc_path() or $repo->repo_path())
2108 or die("chdir: $!");
2109 local $ENV{"GIT_DIR"} = $repo->repo_path();
Michael Strawbridgea8022c52023-04-19 16:27:03 -04002110
2111 my ($recipients_ref, $to, $date, $gitversion, $cc, $ccline, $header) = gen_header();
2112
2113 require File::Temp;
2114 my ($header_filehandle, $header_filename) = File::Temp::tempfile(
2115 TEMPLATE => ".gitsendemail.header.XXXXXX",
2116 DIR => $repo->repo_path(),
2117 UNLINK => 1,
2118 );
2119 print $header_filehandle $header;
2120
Emily Shaffera7555302021-12-22 04:59:38 +01002121 my @cmd = ("git", "hook", "run", "--ignore-missing",
2122 $hook_name, "--");
Michael Strawbridgea8022c52023-04-19 16:27:03 -04002123 my @cmd_msg = (@cmd, "<patch>", "<header>");
2124 my @cmd_run = (@cmd, $target, $header_filename);
Emily Shaffera7555302021-12-22 04:59:38 +01002125 $hook_error = system_or_msg(\@cmd_run, undef, "@cmd_msg");
Jonathan Tan177409e2017-06-01 16:50:55 -07002126 chdir($cwd_save) or die("chdir: $!");
2127 }
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +02002128 if ($hook_error) {
Jean-Noël Avilaaf15f842022-04-11 19:23:30 +00002129 $hook_error = sprintf(
2130 __("fatal: %s: rejected by %s hook\n%s\nwarning: no patches were sent\n"),
2131 $fn, $hook_name, $hook_error);
Emily Shaffera7555302021-12-22 04:59:38 +01002132 die $hook_error;
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +02002133 }
Jonathan Tan64896602017-05-12 15:38:26 -07002134 }
Jonathan Tan64896602017-05-12 15:38:26 -07002135
brian m. carlsonf2d06fb2018-07-08 22:17:11 +00002136 # Any long lines will be automatically fixed if we use a suitable transfer
2137 # encoding.
2138 unless ($xfer_encoding =~ /^(?:auto|quoted-printable|base64)$/) {
2139 open(my $fh, '<', $fn)
2140 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
2141 while (my $line = <$fh>) {
2142 if (length($line) > 998) {
Ævar Arnfjörð Bjarmasonea7811b2021-04-06 16:00:37 +02002143 die sprintf(__("fatal: %s:%d is longer than 998 characters\n" .
2144 "warning: no patches were sent\n"), $fn, $.);
brian m. carlsonf2d06fb2018-07-08 22:17:11 +00002145 }
Jeff King747bbff2008-01-18 09:19:48 -05002146 }
2147 }
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -07002148 return;
Jeff King747bbff2008-01-18 09:19:48 -05002149}
Jeff King0706bd12008-03-28 17:28:33 -04002150
Junio C Hamano531220b2016-03-17 22:40:05 -07002151sub handle_backup {
2152 my ($last, $lastlen, $file, $known_suffix) = @_;
2153 my ($suffix, $skip);
2154
2155 $skip = 0;
2156 if (defined $last &&
2157 ($lastlen < length($file)) &&
2158 (substr($file, 0, $lastlen) eq $last) &&
2159 ($suffix = substr($file, $lastlen)) !~ /^[a-z0-9]/i) {
2160 if (defined $known_suffix && $suffix eq $known_suffix) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01002161 printf(__("Skipping %s with backup suffix '%s'.\n"), $file, $known_suffix);
Junio C Hamano531220b2016-03-17 22:40:05 -07002162 $skip = 1;
2163 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01002164 # TRANSLATORS: please keep "[y|N]" as is.
2165 my $answer = ask(sprintf(__("Do you really want to send %s? [y|N]: "), $file),
Junio C Hamano531220b2016-03-17 22:40:05 -07002166 valid_re => qr/^(?:y|n)/i,
2167 default => 'n');
2168 $skip = ($answer ne 'y');
2169 if ($skip) {
2170 $known_suffix = $suffix;
2171 }
2172 }
2173 }
2174 return ($skip, $known_suffix);
2175}
2176
2177sub handle_backup_files {
2178 my @file = @_;
2179 my ($last, $lastlen, $known_suffix, $skip, @result);
2180 for my $file (@file) {
2181 ($skip, $known_suffix) = handle_backup($last, $lastlen,
2182 $file, $known_suffix);
2183 push @result, $file unless $skip;
2184 $last = $file;
2185 $lastlen = length($file);
2186 }
2187 return @result;
2188}
2189
Jeff King0706bd12008-03-28 17:28:33 -04002190sub file_has_nonascii {
2191 my $fn = shift;
2192 open(my $fh, '<', $fn)
Vasco Almeida3c5cd202016-12-14 11:54:37 -01002193 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
Jeff King0706bd12008-03-28 17:28:33 -04002194 while (my $line = <$fh>) {
2195 return 1 if $line =~ /[^[:ascii:]]/;
2196 }
2197 return 0;
2198}
Thomas Rast3cae7e52010-06-17 22:10:39 +02002199
2200sub body_or_subject_has_nonascii {
2201 my $fn = shift;
2202 open(my $fh, '<', $fn)
Vasco Almeida3c5cd202016-12-14 11:54:37 -01002203 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
Thomas Rast3cae7e52010-06-17 22:10:39 +02002204 while (my $line = <$fh>) {
2205 last if $line =~ /^$/;
2206 return 1 if $line =~ /^Subject.*[^[:ascii:]]/;
2207 }
2208 while (my $line = <$fh>) {
2209 return 1 if $line =~ /[^[:ascii:]]/;
2210 }
2211 return 0;
2212}