blob: 5861e99a6eb2a16bb45e759645e124a66a50e107 [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
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100223# Handle interactive edition of files.
224my $multiedit;
Michael J Gruber0ce142c2010-03-22 17:12:53 +0100225my $editor;
Jonathan Niederb4479f02009-10-30 20:42:34 -0500226
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +0200227sub system_or_msg {
Emily Shaffera7555302021-12-22 04:59:38 +0100228 my ($args, $msg, $cmd_name) = @_;
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +0200229 system(@$args);
230 my $signalled = $? & 127;
231 my $exit_code = $? >> 8;
232 return unless $signalled or $exit_code;
233
Emily Shaffera7555302021-12-22 04:59:38 +0100234 my @sprintf_args = ($cmd_name ? $cmd_name : $args->[0], $exit_code);
Ævar Arnfjörð Bjarmason5b719b72021-05-25 01:14:24 +0200235 if (defined $msg) {
236 # Quiet the 'redundant' warning category, except we
237 # need to support down to Perl 5.8, so we can't do a
238 # "no warnings 'redundant'", since that category was
239 # introduced in perl 5.22, and asking for it will die
240 # on older perls.
241 no warnings;
242 return sprintf($msg, @sprintf_args);
243 }
Ævar Arnfjörð Bjarmasonea7811b2021-04-06 16:00:37 +0200244 return sprintf(__("fatal: command '%s' died with exit code %d"),
Ævar Arnfjörð Bjarmason5b719b72021-05-25 01:14:24 +0200245 @sprintf_args);
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +0200246}
247
248sub system_or_die {
249 my $msg = system_or_msg(@_);
250 die $msg if $msg;
251}
252
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100253sub do_edit {
Michael J Gruber0ce142c2010-03-22 17:12:53 +0100254 if (!defined($editor)) {
255 $editor = Git::command_oneline('var', 'GIT_EDITOR');
256 }
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +0200257 my $die_msg = __("the editor exited uncleanly, aborting everything");
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100258 if (defined($multiedit) && !$multiedit) {
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +0200259 system_or_die(['sh', '-c', $editor.' "$@"', $editor, $_], $die_msg) for @_;
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100260 } else {
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +0200261 system_or_die(['sh', '-c', $editor.' "$@"', $editor, @_], $die_msg);
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100262 }
263}
Adam Roben5483c712007-06-27 20:59:37 -0700264
265# Variables with corresponding config settings
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200266my ($suppress_from, $signed_off_by_cc);
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300267my ($cover_cc, $cover_to);
Joe Perches6e74e072010-09-24 10:03:00 -0700268my ($to_cmd, $cc_cmd);
Pascal Obry052fbea2010-09-06 20:12:11 +0200269my ($smtp_server, $smtp_server_port, @smtp_server_options);
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -0700270my ($smtp_authuser, $smtp_encryption, $smtp_ssl_cert_path);
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800271my ($batch_size, $relogin_delay);
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200272my ($identity, $aliasfiletype, @alias_files, $smtp_domain, $smtp_auth);
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200273my ($confirm);
David Brown65648282007-12-25 19:56:29 -0800274my (@suppress_cc);
Thomas Rast3cae7e52010-06-17 22:10:39 +0200275my ($auto_8bit_encoding);
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200276my ($compose_encoding);
Gregory Anderscd5b33f2021-05-14 09:15:53 -0600277my ($sendmail_cmd);
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200278# Variables with corresponding config settings & hardcoded defaults
279my ($debug_net_smtp) = 0; # Net::SMTP, see send_message()
280my $thread = 1;
281my $chain_reply_to = 0;
282my $use_xmailer = 1;
283my $validate = 1;
brian m. carlsone67a2282018-07-08 22:17:12 +0000284my $target_xfer_encoding = 'auto';
Drew DeVaultdd84e522020-07-23 20:44:32 -0400285my $forbid_sendmail_variables = 1;
Adam Roben5483c712007-06-27 20:59:37 -0700286
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900287my %config_bool_settings = (
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200288 "thread" => \$thread,
289 "chainreplyto" => \$chain_reply_to,
290 "suppressfrom" => \$suppress_from,
291 "signedoffbycc" => \$signed_off_by_cc,
292 "cccover" => \$cover_cc,
293 "tocover" => \$cover_to,
294 "signedoffcc" => \$signed_off_by_cc,
295 "validate" => \$validate,
296 "multiedit" => \$multiedit,
297 "annotate" => \$annotate,
298 "xmailer" => \$use_xmailer,
Drew DeVaultdd84e522020-07-23 20:44:32 -0400299 "forbidsendmailvariables" => \$forbid_sendmail_variables,
Adam Robene46f7a02007-06-26 15:48:30 -0700300);
301
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900302my %config_settings = (
Ævar Arnfjörð Bjarmason119974e2021-05-28 11:23:43 +0200303 "smtpencryption" => \$smtp_encryption,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900304 "smtpserver" => \$smtp_server,
Junio C Hamano44b24762007-09-25 17:27:54 -0700305 "smtpserverport" => \$smtp_server_port,
Pascal Obry052fbea2010-09-06 20:12:11 +0200306 "smtpserveroption" => \@smtp_server_options,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900307 "smtpuser" => \$smtp_authuser,
308 "smtppass" => \$smtp_authpass,
Pascal Obrye1e91152010-09-06 20:12:09 +0200309 "smtpdomain" => \$smtp_domain,
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200310 "smtpauth" => \$smtp_auth,
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800311 "smtpbatchsize" => \$batch_size,
312 "smtprelogindelay" => \$relogin_delay,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200313 "to" => \@config_to,
Joe Perches6e74e072010-09-24 10:03:00 -0700314 "tocmd" => \$to_cmd,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200315 "cc" => \@config_cc,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900316 "cccmd" => \$cc_cmd,
317 "aliasfiletype" => \$aliasfiletype,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200318 "bcc" => \@config_bcc,
David Brown65648282007-12-25 19:56:29 -0800319 "suppresscc" => \@suppress_cc,
Ask Bjørn Hansen9f7820a2008-06-07 00:33:42 -0700320 "envelopesender" => \$envelope_sender,
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500321 "confirm" => \$confirm,
Trent Piepho09caa242009-05-12 15:48:56 -0700322 "from" => \$sender,
Thomas Rast3cae7e52010-06-17 22:10:39 +0200323 "assume8bitencoding" => \$auto_8bit_encoding,
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200324 "composeencoding" => \$compose_encoding,
Paolo Bonzini8d814082014-11-25 15:00:27 +0100325 "transferencoding" => \$target_xfer_encoding,
Gregory Anderscd5b33f2021-05-14 09:15:53 -0600326 "sendmailcmd" => \$sendmail_cmd,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900327);
Avi Kivity4a62d3f2007-03-11 19:19:44 +0200328
Cord Seelecec5dae2011-09-30 12:52:25 +0200329my %config_path_settings = (
330 "aliasesfile" => \@alias_files,
John Keeping6e07a3b2015-11-17 22:01:05 +0000331 "smtpsslcertpath" => \$smtp_ssl_cert_path,
Cord Seelecec5dae2011-09-30 12:52:25 +0200332);
333
Michael Witten87429972008-02-03 19:53:57 -0500334# Handle Uncouth Termination
335sub signal_handler {
Michael Witten87429972008-02-03 19:53:57 -0500336 # Make text normal
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200337 require Term::ANSIColor;
338 print Term::ANSIColor::color("reset"), "\n";
Michael Witten87429972008-02-03 19:53:57 -0500339
340 # SMTP password masked
341 system "stty echo";
342
343 # tmp files from --compose
Jay Soffianafe756c2009-02-23 13:51:37 -0500344 if (defined $compose_filename) {
345 if (-e $compose_filename) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100346 printf __("'%s' contains an intermediate version ".
347 "of the email you were composing.\n"),
348 $compose_filename;
Jay Soffianafe756c2009-02-23 13:51:37 -0500349 }
350 if (-e ($compose_filename . ".final")) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100351 printf __("'%s.final' contains the composed email.\n"),
352 $compose_filename;
Jay Soffianafe756c2009-02-23 13:51:37 -0500353 }
Michael Witten87429972008-02-03 19:53:57 -0500354 }
355
356 exit;
357};
358
359$SIG{TERM} = \&signal_handler;
360$SIG{INT} = \&signal_handler;
361
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200362# Read our sendemail.* config
363sub read_config {
Ævar Arnfjörð Bjarmason9264d292021-05-28 11:23:45 +0200364 my ($known_keys, $configured, $prefix) = @_;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200365
366 foreach my $setting (keys %config_bool_settings) {
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200367 my $target = $config_bool_settings{$setting};
Ævar Arnfjörð Bjarmason9264d292021-05-28 11:23:45 +0200368 my $key = "$prefix.$setting";
369 next unless exists $known_keys->{$key};
Ævar Arnfjörð Bjarmasonc95e3a32021-05-28 11:23:51 +0200370 my $v = (@{$known_keys->{$key}} == 1 &&
371 (defined $known_keys->{$key}->[0] &&
372 $known_keys->{$key}->[0] =~ /^(?:true|false)$/s))
373 ? $known_keys->{$key}->[0] eq 'true'
374 : Git::config_bool(@repo, $key);
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200375 next unless defined $v;
376 next if $configured->{$setting}++;
377 $$target = $v;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200378 }
379
380 foreach my $setting (keys %config_path_settings) {
381 my $target = $config_path_settings{$setting};
Ævar Arnfjörð Bjarmason9264d292021-05-28 11:23:45 +0200382 my $key = "$prefix.$setting";
383 next unless exists $known_keys->{$key};
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200384 if (ref($target) eq "ARRAY") {
Ævar Arnfjörð Bjarmason9264d292021-05-28 11:23:45 +0200385 my @values = Git::config_path(@repo, $key);
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200386 next unless @values;
387 next if $configured->{$setting}++;
388 @$target = @values;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200389 }
390 else {
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200391 my $v = Git::config_path(@repo, "$prefix.$setting");
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200392 next unless defined $v;
393 next if $configured->{$setting}++;
394 $$target = $v;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200395 }
396 }
397
398 foreach my $setting (keys %config_settings) {
399 my $target = $config_settings{$setting};
Ævar Arnfjörð Bjarmason9264d292021-05-28 11:23:45 +0200400 my $key = "$prefix.$setting";
401 next unless exists $known_keys->{$key};
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200402 if (ref($target) eq "ARRAY") {
Ævar Arnfjörð Bjarmasonc95e3a32021-05-28 11:23:51 +0200403 my @values = @{$known_keys->{$key}};
404 @values = grep { defined } @values;
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200405 next if $configured->{$setting}++;
406 @$target = @values;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200407 }
408 else {
Ævar Arnfjörð Bjarmasonb996f842021-09-06 09:33:29 +0200409 my $v = $known_keys->{$key}->[-1];
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200410 next unless defined $v;
411 next if $configured->{$setting}++;
412 $$target = $v;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200413 }
414 }
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200415}
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200416
Ævar Arnfjörð Bjarmason2b110e92021-05-28 11:23:44 +0200417sub config_regexp {
418 my ($regex) = @_;
419 my @ret;
420 eval {
Ævar Arnfjörð Bjarmasonc95e3a32021-05-28 11:23:51 +0200421 my $ret = Git::command(
Ævar Arnfjörð Bjarmason2b110e92021-05-28 11:23:44 +0200422 'config',
Ævar Arnfjörð Bjarmasonc95e3a32021-05-28 11:23:51 +0200423 '--null',
Ævar Arnfjörð Bjarmason2b110e92021-05-28 11:23:44 +0200424 '--get-regexp',
425 $regex,
426 );
Ævar Arnfjörð Bjarmasonc95e3a32021-05-28 11:23:51 +0200427 @ret = map {
428 # We must always return ($k, $v) here, since
429 # empty config values will be just "key\0",
430 # not "key\nvalue\0".
431 my ($k, $v) = split /\n/, $_, 2;
432 ($k, $v);
433 } split /\0/, $ret;
Ævar Arnfjörð Bjarmason2b110e92021-05-28 11:23:44 +0200434 1;
435 } or do {
436 # If we have no keys we're OK, otherwise re-throw
437 die $@ if $@->value != 1;
438 };
439 return @ret;
440}
441
Ævar Arnfjörð Bjarmason9264d292021-05-28 11:23:45 +0200442# Save ourselves a lot of work of shelling out to 'git config' (it
443# parses 'bool' etc.) by only doing so for config keys that exist.
444my %known_config_keys;
445{
Ævar Arnfjörð Bjarmasonc95e3a32021-05-28 11:23:51 +0200446 my @kv = config_regexp("^sende?mail[.]");
447 while (my ($k, $v) = splice @kv, 0, 2) {
448 push @{$known_config_keys{$k}} => $v;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200449 }
450}
451
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200452# sendemail.identity yields to --identity. We must parse this
453# special-case first before the rest of the config is read.
Ævar Arnfjörð Bjarmason9264d292021-05-28 11:23:45 +0200454{
455 my $key = "sendemail.identity";
456 $identity = Git::config(@repo, $key) if exists $known_config_keys{$key};
457}
Thiago Perrotta2b7b7582021-10-25 17:27:06 -0400458my %identity_options = (
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200459 "identity=s" => \$identity,
460 "no-identity" => \$no_identity,
461);
Thiago Perrotta2b7b7582021-10-25 17:27:06 -0400462my $rc = GetOptions(%identity_options);
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200463usage() unless $rc;
464undef $identity if $no_identity;
465
466# Now we know enough to read the config
467{
468 my %configured;
Ævar Arnfjörð Bjarmason9264d292021-05-28 11:23:45 +0200469 read_config(\%known_config_keys, \%configured, "sendemail.$identity") if defined $identity;
470 read_config(\%known_config_keys, \%configured, "sendemail");
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200471}
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200472
Ryan Anderson83b24432005-07-31 04:17:25 -0400473# Begin by accumulating all the variables (defined above), that we will end up
474# needing, first, from the command line:
475
Clemens Buchacherc5978242011-09-03 19:06:13 +0200476my $help;
Duy Nguyen13374982018-11-03 07:03:18 +0100477my $git_completion_helper;
Thiago Perrotta2b7b7582021-10-25 17:27:06 -0400478my %dump_aliases_options = (
479 "h" => \$help,
480 "dump-aliases" => \$dump_aliases,
481);
482$rc = GetOptions(%dump_aliases_options);
Jacob Keller17b7a832015-11-19 14:52:11 -0800483usage() unless $rc;
Vasco Almeida46493102016-12-14 11:54:36 -0100484die __("--dump-aliases incompatible with other options\n")
Jacob Keller17b7a832015-11-19 14:52:11 -0800485 if !$help and $dump_aliases and @ARGV;
Thiago Perrotta2b7b7582021-10-25 17:27:06 -0400486my %options = (
Clemens Buchacherc5978242011-09-03 19:06:13 +0200487 "sender|from=s" => \$sender,
Thiago Perrotta2b7b7582021-10-25 17:27:06 -0400488 "in-reply-to=s" => \$initial_in_reply_to,
Christian Ludwigd11c9432018-03-04 00:58:14 +0100489 "reply-to=s" => \$reply_to,
Ryan Anderson83b24432005-07-31 04:17:25 -0400490 "subject=s" => \$initial_subject,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200491 "to=s" => \@getopt_to,
Joe Perches6e74e072010-09-24 10:03:00 -0700492 "to-cmd=s" => \$to_cmd,
Stephen Boydf434c082010-03-07 14:46:48 -0800493 "no-to" => \$no_to,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200494 "cc=s" => \@getopt_cc,
Stephen Boydf434c082010-03-07 14:46:48 -0800495 "no-cc" => \$no_cc,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200496 "bcc=s" => \@getopt_bcc,
Stephen Boydf434c082010-03-07 14:46:48 -0800497 "no-bcc" => \$no_bcc,
Ryan Anderson78488b22005-07-31 20:04:24 -0400498 "chain-reply-to!" => \$chain_reply_to,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800499 "no-chain-reply-to" => sub {$chain_reply_to = 0},
Gregory Anderscd5b33f2021-05-14 09:15:53 -0600500 "sendmail-cmd=s" => \$sendmail_cmd,
Ryan Anderson3342d852005-07-31 20:04:24 -0400501 "smtp-server=s" => \$smtp_server,
Pascal Obry052fbea2010-09-06 20:12:11 +0200502 "smtp-server-option=s" => \@smtp_server_options,
Junio C Hamano44b24762007-09-25 17:27:54 -0700503 "smtp-server-port=s" => \$smtp_server_port,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900504 "smtp-user=s" => \$smtp_authuser,
Michael Witten2363d742008-02-03 19:53:56 -0500505 "smtp-pass:s" => \$smtp_authpass,
Thomas Rastf6bebd12008-06-25 21:42:43 +0200506 "smtp-ssl" => sub { $smtp_encryption = 'ssl' },
507 "smtp-encryption=s" => \$smtp_encryption,
Thomas Rast979e6522013-12-01 23:48:42 +0100508 "smtp-ssl-cert-path=s" => \$smtp_ssl_cert_path,
Jari Aaltof60812e2010-03-14 17:16:09 +0200509 "smtp-debug:i" => \$debug_net_smtp,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -0400510 "smtp-domain:s" => \$smtp_domain,
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200511 "smtp-auth=s" => \$smtp_auth,
Joshua Watt8dd9b3f2018-10-22 22:24:25 -0500512 "no-smtp-auth" => sub {$smtp_auth = 'none'},
Felipe Contreras402596a2013-04-07 01:10:27 -0600513 "annotate!" => \$annotate,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800514 "no-annotate" => sub {$annotate = 0},
Ryan Anderson1f038a02005-09-05 01:13:07 -0400515 "compose" => \$compose,
Ryan Anderson30d08b32006-02-02 11:56:06 -0500516 "quiet" => \$quiet,
Joe Perches324a8bd2007-08-17 18:51:12 -0700517 "cc-cmd=s" => \$cc_cmd,
Adam Roben5483c712007-06-27 20:59:37 -0700518 "suppress-from!" => \$suppress_from,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800519 "no-suppress-from" => sub {$suppress_from = 0},
David Brown65648282007-12-25 19:56:29 -0800520 "suppress-cc=s" => \@suppress_cc,
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500521 "signed-off-cc|signed-off-by-cc!" => \$signed_off_by_cc,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800522 "no-signed-off-cc|no-signed-off-by-cc" => sub {$signed_off_by_cc = 0},
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300523 "cc-cover|cc-cover!" => \$cover_cc,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800524 "no-cc-cover" => sub {$cover_cc = 0},
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300525 "to-cover|to-cover!" => \$cover_to,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800526 "no-to-cover" => sub {$cover_to = 0},
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500527 "confirm=s" => \$confirm,
Matthew Wilcox61302592006-10-10 08:58:23 -0600528 "dry-run" => \$dry_run,
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700529 "envelope-sender=s" => \$envelope_sender,
Adam Roben5483c712007-06-27 20:59:37 -0700530 "thread!" => \$thread,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800531 "no-thread" => sub {$thread = 0},
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500532 "validate!" => \$validate,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800533 "no-validate" => sub {$validate = 0},
Paolo Bonzini8d814082014-11-25 15:00:27 +0100534 "transfer-encoding=s" => \$target_xfer_encoding,
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100535 "format-patch!" => \$format_patch,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800536 "no-format-patch" => sub {$format_patch = 0},
Thomas Rast3cae7e52010-06-17 22:10:39 +0200537 "8bit-encoding=s" => \$auto_8bit_encoding,
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200538 "compose-encoding=s" => \$compose_encoding,
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200539 "force" => \$force,
Luis Henriquesac1596a2014-03-24 21:38:27 +0000540 "xmailer!" => \$use_xmailer,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800541 "no-xmailer" => sub {$use_xmailer = 0},
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800542 "batch-size=i" => \$batch_size,
543 "relogin-delay=i" => \$relogin_delay,
Duy Nguyen13374982018-11-03 07:03:18 +0100544 "git-completion-helper" => \$git_completion_helper,
Thiago Perrotta2b7b7582021-10-25 17:27:06 -0400545);
546$rc = GetOptions(%options);
Ryan Anderson83b24432005-07-31 04:17:25 -0400547
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200548# Munge any "either config or getopt, not both" variables
549my @initial_to = @getopt_to ? @getopt_to : ($no_to ? () : @config_to);
550my @initial_cc = @getopt_cc ? @getopt_cc : ($no_cc ? () : @config_cc);
551my @initial_bcc = @getopt_bcc ? @getopt_bcc : ($no_bcc ? () : @config_bcc);
552
Clemens Buchacherc5978242011-09-03 19:06:13 +0200553usage() if $help;
Thiago Perrotta2b7b7582021-10-25 17:27:06 -0400554my %all_options = (%options, %dump_aliases_options, %identity_options);
555completion_helper(\%all_options) if $git_completion_helper;
Michael Coleman1b0baf12007-02-27 22:47:54 -0600556unless ($rc) {
557 usage();
558}
559
Ævar Arnfjörð Bjarmason9264d292021-05-28 11:23:45 +0200560if ($forbid_sendmail_variables && grep { /^sendmail/s } keys %known_config_keys) {
Drew DeVaultdd84e522020-07-23 20:44:32 -0400561 die __("fatal: found configuration options for 'sendmail'\n" .
562 "git-send-email is configured with the sendemail.* options - note the 'e'.\n" .
563 "Set sendemail.forbidSendmailVariables to false to disable this check.\n");
564}
565
Vasco Almeida46493102016-12-14 11:54:36 -0100566die __("Cannot run git format-patch from outside a repository\n")
Jay Soffianeed6ca72009-02-14 23:32:13 -0500567 if $format_patch and not $repo;
568
Stefan Beller9caa7062018-02-12 11:44:04 -0800569die __("`batch-size` and `relogin` must be specified together " .
570 "(via command-line or configuration option)\n")
571 if defined $relogin_delay and not defined $batch_size;
572
Thomas Rastfa835cd2008-06-26 23:03:21 +0200573# 'default' encryption is none -- this only prevents a warning
574$smtp_encryption = '' unless (defined $smtp_encryption);
575
David Brown65648282007-12-25 19:56:29 -0800576# Set CC suppressions
577my(%suppress_cc);
578if (@suppress_cc) {
579 foreach my $entry (@suppress_cc) {
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700580 # Please update $__git_send_email_suppresscc_options
581 # in git-completion.bash when you add new options.
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100582 die sprintf(__("Unknown --suppress-cc field: '%s'\n"), $entry)
Rasmus Villemoesef0cc1d2018-10-16 09:39:23 +0200583 unless $entry =~ /^(?:all|cccmd|cc|author|self|sob|body|bodycc|misc-by)$/;
David Brown65648282007-12-25 19:56:29 -0800584 $suppress_cc{$entry} = 1;
585 }
586}
587
588if ($suppress_cc{'all'}) {
Rasmus Villemoesef0cc1d2018-10-16 09:39:23 +0200589 foreach my $entry (qw (cccmd cc author self sob body bodycc misc-by)) {
David Brown65648282007-12-25 19:56:29 -0800590 $suppress_cc{$entry} = 1;
591 }
592 delete $suppress_cc{'all'};
593}
594
595# If explicit old-style ones are specified, they trump --suppress-cc.
596$suppress_cc{'self'} = $suppress_from if defined $suppress_from;
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500597$suppress_cc{'sob'} = !$signed_off_by_cc if defined $signed_off_by_cc;
David Brown65648282007-12-25 19:56:29 -0800598
Jay Soffian3531e272009-02-14 23:32:15 -0500599if ($suppress_cc{'body'}) {
Rasmus Villemoesef0cc1d2018-10-16 09:39:23 +0200600 foreach my $entry (qw (sob bodycc misc-by)) {
Jay Soffian3531e272009-02-14 23:32:15 -0500601 $suppress_cc{$entry} = 1;
602 }
603 delete $suppress_cc{'body'};
604}
605
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500606# Set confirm's default value
607my $confirm_unconfigured = !defined $confirm;
608if ($confirm_unconfigured) {
609 $confirm = scalar %suppress_cc ? 'compose' : 'auto';
610};
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700611# Please update $__git_send_email_confirm_options in
612# git-completion.bash when you add new options.
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100613die sprintf(__("Unknown --confirm setting: '%s'\n"), $confirm)
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500614 unless $confirm =~ /^(?:auto|cc|compose|always|never)/;
615
David Brown65648282007-12-25 19:56:29 -0800616# Debugging, print out the suppressions.
617if (0) {
618 print "suppressions:\n";
619 foreach my $entry (keys %suppress_cc) {
620 printf " %-5s -> $suppress_cc{$entry}\n", $entry;
621 }
622}
623
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100624my ($repoauthor, $repocommitter);
Ævar Arnfjörð Bjarmasonfef381e2021-05-28 11:23:46 +0200625{
626 my %cache;
627 my ($author, $committer);
628 my $common = sub {
629 my ($what) = @_;
630 return $cache{$what} if exists $cache{$what};
631 ($cache{$what}) = Git::ident_person(@repo, $what);
632 return $cache{$what};
633 };
634 $repoauthor = sub { $common->('author') };
635 $repocommitter = sub { $common->('committer') };
636}
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900637
Jay Soffian50126992009-02-14 23:32:14 -0500638sub parse_address_line {
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200639 require Git::LoadCPAN::Mail::Address;
Matthieu Moybd869f62018-01-05 19:36:51 +0100640 return map { $_->format } Mail::Address->parse($_[0]);
Jay Soffian50126992009-02-14 23:32:14 -0500641}
642
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800643sub split_addrs {
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200644 require Text::ParseWords;
645 return Text::ParseWords::quotewords('\s*,\s*', 1, @_);
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800646}
647
Eric Wong994d6c62006-05-14 19:13:44 -0700648my %aliases;
Eric Sunshine09f11572015-05-31 18:29:27 -0400649
650sub parse_sendmail_alias {
651 local $_ = shift;
652 if (/"/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100653 printf STDERR __("warning: sendmail alias with quotes is not supported: %s\n"), $_;
Eric Sunshine86b89842015-06-01 14:22:36 -0400654 } elsif (/:include:/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100655 printf STDERR __("warning: `:include:` not supported: %s\n"), $_;
Eric Sunshine86b89842015-06-01 14:22:36 -0400656 } elsif (/[\/|]/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100657 printf STDERR __("warning: `/file` or `|pipe` redirection not supported: %s\n"), $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400658 } elsif (/^(\S+?)\s*:\s*(.+)$/) {
659 my ($alias, $addr) = ($1, $2);
660 $aliases{$alias} = [ split_addrs($addr) ];
661 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100662 printf STDERR __("warning: sendmail line is not recognized: %s\n"), $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400663 }
664}
665
666sub parse_sendmail_aliases {
667 my $fh = shift;
Eric Sunshine2532dd02015-05-31 18:29:29 -0400668 my $s = '';
Eric Sunshine09f11572015-05-31 18:29:27 -0400669 while (<$fh>) {
Eric Sunshine2532dd02015-05-31 18:29:29 -0400670 chomp;
Eric Sunshine020be85f2015-05-31 18:29:28 -0400671 next if /^\s*$/ || /^\s*#/;
Eric Sunshine2532dd02015-05-31 18:29:29 -0400672 $s .= $_, next if $s =~ s/\\$// || s/^\s+//;
673 parse_sendmail_alias($s) if $s;
674 $s = $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400675 }
Eric Sunshine2532dd02015-05-31 18:29:29 -0400676 $s =~ s/\\$//; # silently tolerate stray '\' on last line
677 parse_sendmail_alias($s) if $s;
Eric Sunshine09f11572015-05-31 18:29:27 -0400678}
679
Eric Wong994d6c62006-05-14 19:13:44 -0700680my %parse_alias = (
681 # multiline formats can be supported in the future
682 mutt => sub { my $fh = shift; while (<$fh>) {
Felipe Contrerasffc01f92009-09-30 17:49:36 +0300683 if (/^\s*alias\s+(?:-group\s+\S+\s+)*(\S+)\s+(.*)$/) {
Eric Wong994d6c62006-05-14 19:13:44 -0700684 my ($alias, $addr) = ($1, $2);
685 $addr =~ s/#.*$//; # mutt allows # comments
Eric Wong2c510f22016-01-04 20:53:30 +0000686 # commas delimit multiple addresses
687 my @addr = split_addrs($addr);
688
689 # quotes may be escaped in the file,
690 # unescape them so we do not double-escape them later.
691 s/\\"/"/g foreach @addr;
692 $aliases{$alias} = \@addr
Eric Wong994d6c62006-05-14 19:13:44 -0700693 }}},
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200694 mailrc => sub { my $fh = shift; while (<$fh>) {
Jeff Kinga277d1e2016-03-17 19:58:22 -0400695 if (/^alias\s+(\S+)\s+(.*?)\s*$/) {
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200696 require Text::ParseWords;
Eric Wong994d6c62006-05-14 19:13:44 -0700697 # spaces delimit multiple addresses
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200698 $aliases{$1} = [ Text::ParseWords::quotewords('\s+', 0, $2) ];
Eric Wong994d6c62006-05-14 19:13:44 -0700699 }}},
Trent Piepho73c427e2008-11-25 18:55:00 -0800700 pine => sub { my $fh = shift; my $f='\t[^\t]*';
701 for (my $x = ''; defined($x); $x = $_) {
702 chomp $x;
703 $x .= $1 while(defined($_ = <$fh>) && /^ +(.*)$/);
704 $x =~ /^(\S+)$f\t\(?([^\t]+?)\)?(:?$f){0,2}$/ or next;
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800705 $aliases{$1} = [ split_addrs($2) ];
Trent Piepho73c427e2008-11-25 18:55:00 -0800706 }},
Bill Pemberton7613ea32009-04-22 09:41:29 -0400707 elm => sub { my $fh = shift;
708 while (<$fh>) {
709 if (/^(\S+)\s+=\s+[^=]+=\s(\S+)/) {
710 my ($alias, $addr) = ($1, $2);
711 $aliases{$alias} = [ split_addrs($addr) ];
712 }
713 } },
Eric Sunshine09f11572015-05-31 18:29:27 -0400714 sendmail => \&parse_sendmail_aliases,
Eric Wong994d6c62006-05-14 19:13:44 -0700715 gnus => sub { my $fh = shift; while (<$fh>) {
716 if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
717 $aliases{$1} = [ $2 ];
718 }}}
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700719 # Please update _git_config() in git-completion.bash when you
720 # add new MUAs.
Eric Wong994d6c62006-05-14 19:13:44 -0700721);
722
Petr Baudis3cb8caf2006-07-03 22:47:58 +0200723if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) {
Eric Wong994d6c62006-05-14 19:13:44 -0700724 foreach my $file (@alias_files) {
725 open my $fh, '<', $file or die "opening $file: $!\n";
726 $parse_alias{$aliasfiletype}->($fh);
727 close $fh;
728 }
729}
730
Jacob Keller17b7a832015-11-19 14:52:11 -0800731if ($dump_aliases) {
732 print "$_\n" for (sort keys %aliases);
733 exit(0);
734}
735
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700736# is_format_patch_arg($f) returns 0 if $f names a patch, or 1 if
737# $f is a revision list specification to be passed to format-patch.
738sub is_format_patch_arg {
Jay Soffianeed6ca72009-02-14 23:32:13 -0500739 return unless $repo;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100740 my $f = shift;
741 try {
742 $repo->command('rev-parse', '--verify', '--quiet', $f);
743 if (defined($format_patch)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100744 return $format_patch;
745 }
Ævar Arnfjörð Bjarmason4adbf382021-05-28 11:23:47 +0200746 die sprintf(__(<<EOF), $f, $f);
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100747File '%s' exists but it could also be the range of commits
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100748to produce patches for. Please disambiguate by...
749
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100750 * Saying "./%s" if you mean a file; or
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100751 * Giving --format-patch option if you mean a range.
752EOF
753 } catch Git::Error::Command with {
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700754 # Not a valid revision. Treat it as a filename.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100755 return 0;
756 }
757}
758
Jeff Kingaa548922008-01-18 09:19:36 -0500759# Now that all the defaults are set, process the rest of the command line
760# arguments and collect up the files that need to be processed.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100761my @rev_list_opts;
Junio C Hamano69f4ce52008-11-30 22:38:20 -0800762while (defined(my $f = shift @ARGV)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100763 if ($f eq "--") {
764 push @rev_list_opts, "--", @ARGV;
765 @ARGV = ();
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700766 } elsif (-d $f and !is_format_patch_arg($f)) {
Ævar Arnfjörð Bjarmasonc6038162010-09-30 13:42:54 +0000767 opendir my $dh, $f
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100768 or die sprintf(__("Failed to opendir %s: %s"), $f, $!);
Jeff Kingaa548922008-01-18 09:19:36 -0500769
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200770 require File::Spec;
771 push @files, grep { -f $_ } map { File::Spec->catfile($f, $_) }
Ævar Arnfjörð Bjarmasonc6038162010-09-30 13:42:54 +0000772 sort readdir $dh;
773 closedir $dh;
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700774 } elsif ((-f $f or -p $f) and !is_format_patch_arg($f)) {
Jeff Kingaa548922008-01-18 09:19:36 -0500775 push @files, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500776 } else {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100777 push @rev_list_opts, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500778 }
779}
780
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100781if (@rev_list_opts) {
Vasco Almeida46493102016-12-14 11:54:36 -0100782 die __("Cannot run git format-patch from outside a repository\n")
Jay Soffianeed6ca72009-02-14 23:32:13 -0500783 unless $repo;
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200784 require File::Temp;
785 push @files, $repo->command('format-patch', '-o', File::Temp::tempdir(CLEANUP => 1), @rev_list_opts);
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100786}
787
Junio C Hamano531220b2016-03-17 22:40:05 -0700788@files = handle_backup_files(@files);
789
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500790if ($validate) {
Jeff Kingc764a0c2008-01-18 09:20:10 -0500791 foreach my $f (@files) {
Kevin Ballard300913b2008-06-25 15:44:40 -0700792 unless (-p $f) {
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +0200793 validate_patch($f, $target_xfer_encoding);
Kevin Ballard300913b2008-06-25 15:44:40 -0700794 }
Jeff Kingc764a0c2008-01-18 09:20:10 -0500795 }
Jeff King747bbff2008-01-18 09:19:48 -0500796}
797
Jeff Kingaa548922008-01-18 09:19:36 -0500798if (@files) {
799 unless ($quiet) {
800 print $_,"\n" for (@files);
801 }
802} else {
Vasco Almeida46493102016-12-14 11:54:36 -0100803 print STDERR __("\nNo patch files specified!\n\n");
Jeff Kingaa548922008-01-18 09:19:36 -0500804 usage();
805}
806
Ævar Arnfjörð Bjarmasonacf071b2010-09-30 13:42:57 +0000807sub get_patch_subject {
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100808 my $fn = shift;
809 open (my $fh, '<', $fn);
810 while (my $line = <$fh>) {
811 next unless ($line =~ /^Subject: (.*)$/);
812 close $fh;
813 return "GIT: $1\n";
814 }
815 close $fh;
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100816 die sprintf(__("No subject line in %s?"), $fn);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100817}
818
819if ($compose) {
820 # Note that this does not need to be secure, but we will make a small
821 # effort to have it be unique
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200822 require File::Temp;
Jay Soffianafe756c2009-02-23 13:51:37 -0500823 $compose_filename = ($repo ?
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200824 File::Temp::tempfile(".gitsendemail.msg.XXXXXX", DIR => $repo->repo_path()) :
825 File::Temp::tempfile(".gitsendemail.msg.XXXXXX", DIR => "."))[1];
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000826 open my $c, ">", $compose_filename
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100827 or die sprintf(__("Failed to open for writing %s: %s"), $compose_filename, $!);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100828
829
Ævar Arnfjörð Bjarmasonfef381e2021-05-28 11:23:46 +0200830 my $tpl_sender = $sender || $repoauthor->() || $repocommitter->() || '';
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100831 my $tpl_subject = $initial_subject || '';
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100832 my $tpl_in_reply_to = $initial_in_reply_to || '';
Christian Ludwigd11c9432018-03-04 00:58:14 +0100833 my $tpl_reply_to = $reply_to || '';
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100834
Ævar Arnfjörð Bjarmason4adbf382021-05-28 11:23:47 +0200835 print $c <<EOT1, Git::prefix_lines("GIT: ", __(<<EOT2)), <<EOT3;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100836From $tpl_sender # This line is ignored.
Vasco Almeida70aedfb2016-12-14 11:54:38 -0100837EOT1
838Lines beginning in "GIT:" will be removed.
839Consider including an overall diffstat or table of contents
840for the patch you are writing.
841
842Clear the body content if you don't wish to send a summary.
843EOT2
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100844From: $tpl_sender
Christian Ludwigd11c9432018-03-04 00:58:14 +0100845Reply-To: $tpl_reply_to
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100846Subject: $tpl_subject
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100847In-Reply-To: $tpl_in_reply_to
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100848
Vasco Almeida70aedfb2016-12-14 11:54:38 -0100849EOT3
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100850 for my $f (@files) {
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000851 print $c get_patch_subject($f);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100852 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000853 close $c;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100854
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100855 if ($annotate) {
856 do_edit($compose_filename, @files);
857 } else {
858 do_edit($compose_filename);
859 }
860
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000861 open $c, "<", $compose_filename
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100862 or die sprintf(__("Failed to open %s: %s"), $compose_filename, $!);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100863
Krzysztof Mazur4a47a4d2012-10-22 14:41:48 +0200864 if (!defined $compose_encoding) {
865 $compose_encoding = "UTF-8";
866 }
Nathan Payreb6049542017-12-15 16:33:39 +0100867
868 my %parsed_email;
869 while (my $line = <$c>) {
870 next if $line =~ m/^GIT:/;
871 parse_header_line($line, \%parsed_email);
872 if ($line =~ /^$/) {
873 $parsed_email{'body'} = filter_body($c);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100874 }
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100875 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000876 close $c;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100877
Nathan Payreb6049542017-12-15 16:33:39 +0100878 open my $c2, ">", $compose_filename . ".final"
879 or die sprintf(__("Failed to open %s.final: %s"), $compose_filename, $!);
880
881
882 if ($parsed_email{'From'}) {
883 $sender = delete($parsed_email{'From'});
884 }
885 if ($parsed_email{'In-Reply-To'}) {
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100886 $initial_in_reply_to = delete($parsed_email{'In-Reply-To'});
Nathan Payreb6049542017-12-15 16:33:39 +0100887 }
Christian Ludwigd11c9432018-03-04 00:58:14 +0100888 if ($parsed_email{'Reply-To'}) {
889 $reply_to = delete($parsed_email{'Reply-To'});
Nathan Payreb6049542017-12-15 16:33:39 +0100890 }
891 if ($parsed_email{'Subject'}) {
892 $initial_subject = delete($parsed_email{'Subject'});
893 print $c2 "Subject: " .
894 quote_subject($initial_subject, $compose_encoding) .
895 "\n";
896 }
897
898 if ($parsed_email{'MIME-Version'}) {
899 print $c2 "MIME-Version: $parsed_email{'MIME-Version'}\n",
900 "Content-Type: $parsed_email{'Content-Type'};\n",
901 "Content-Transfer-Encoding: $parsed_email{'Content-Transfer-Encoding'}\n";
902 delete($parsed_email{'MIME-Version'});
903 delete($parsed_email{'Content-Type'});
904 delete($parsed_email{'Content-Transfer-Encoding'});
905 } elsif (file_has_nonascii($compose_filename)) {
906 my $content_type = (delete($parsed_email{'Content-Type'}) or
907 "text/plain; charset=$compose_encoding");
908 print $c2 "MIME-Version: 1.0\n",
909 "Content-Type: $content_type\n",
910 "Content-Transfer-Encoding: 8bit\n";
911 }
912 # Preserve unknown headers
913 foreach my $key (keys %parsed_email) {
914 next if $key eq 'body';
915 print $c2 "$key: $parsed_email{$key}";
916 }
917
918 if ($parsed_email{'body'}) {
919 print $c2 "\n$parsed_email{'body'}\n";
920 delete($parsed_email{'body'});
921 } else {
Vasco Almeida46493102016-12-14 11:54:36 -0100922 print __("Summary email is empty, skipping it\n");
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100923 $compose = -1;
924 }
Nathan Payreb6049542017-12-15 16:33:39 +0100925
926 close $c2;
927
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100928} elsif ($annotate) {
929 do_edit(@files);
930}
931
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200932sub term {
933 my $term = eval {
934 require Term::ReadLine;
935 $ENV{"GIT_SEND_EMAIL_NOTTY"}
936 ? Term::ReadLine->new('git-send-email', \*STDIN, \*STDOUT)
937 : Term::ReadLine->new('git-send-email');
938 };
939 if ($@) {
940 $term = FakeTerm->new("$@: going non-interactive");
941 }
942 return $term;
943}
944
Jay Soffian6e182512009-03-28 21:39:10 -0400945sub ask {
946 my ($prompt, %arg) = @_;
Jay Soffian0da43a62009-04-04 23:23:21 -0400947 my $valid_re = $arg{valid_re};
Jay Soffian6e182512009-03-28 21:39:10 -0400948 my $default = $arg{default};
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700949 my $confirm_only = $arg{confirm_only};
Jay Soffian6e182512009-03-28 21:39:10 -0400950 my $resp;
951 my $i = 0;
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200952 my $term = term();
Jay Soffian5906f542009-03-31 12:22:11 -0400953 return defined $default ? $default : undef
954 unless defined $term->IN and defined fileno($term->IN) and
955 defined $term->OUT and defined fileno($term->OUT);
Jay Soffian6e182512009-03-28 21:39:10 -0400956 while ($i++ < 10) {
957 $resp = $term->readline($prompt);
958 if (!defined $resp) { # EOF
959 print "\n";
960 return defined $default ? $default : undef;
961 }
962 if ($resp eq '' and defined $default) {
963 return $default;
964 }
Jay Soffian0da43a62009-04-04 23:23:21 -0400965 if (!defined $valid_re or $resp =~ /$valid_re/) {
Jay Soffian6e182512009-03-28 21:39:10 -0400966 return $resp;
967 }
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700968 if ($confirm_only) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100969 my $yesno = $term->readline(
970 # TRANSLATORS: please keep [y/N] as is.
971 sprintf(__("Are you sure you want to use <%s> [y/N]? "), $resp));
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700972 if (defined $yesno && $yesno =~ /y/i) {
973 return $resp;
974 }
975 }
Jay Soffian6e182512009-03-28 21:39:10 -0400976 }
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -0700977 return;
Jay Soffian6e182512009-03-28 21:39:10 -0400978}
979
Nathan Payreb6049542017-12-15 16:33:39 +0100980sub parse_header_line {
981 my $lines = shift;
982 my $parsed_line = shift;
983 my $addr_pat = join "|", qw(To Cc Bcc);
984
985 foreach (split(/\n/, $lines)) {
986 if (/^($addr_pat):\s*(.+)$/i) {
987 $parsed_line->{$1} = [ parse_address_line($2) ];
988 } elsif (/^([^:]*):\s*(.+)\s*$/i) {
989 $parsed_line->{$1} = $2;
990 }
991 }
992}
993
994sub filter_body {
995 my $c = shift;
996 my $body = "";
997 while (my $body_line = <$c>) {
998 if ($body_line !~ m/^GIT:/) {
999 $body .= $body_line;
1000 }
1001 }
1002 return $body;
1003}
1004
1005
Thomas Rast3cae7e52010-06-17 22:10:39 +02001006my %broken_encoding;
1007
Ævar Arnfjörð Bjarmason1d50bfd2010-09-30 13:42:58 +00001008sub file_declares_8bit_cte {
Thomas Rast3cae7e52010-06-17 22:10:39 +02001009 my $fn = shift;
1010 open (my $fh, '<', $fn);
1011 while (my $line = <$fh>) {
1012 last if ($line =~ /^$/);
1013 return 1 if ($line =~ /^Content-Transfer-Encoding: .*8bit.*$/);
1014 }
1015 close $fh;
1016 return 0;
1017}
1018
1019foreach my $f (@files) {
1020 next unless (body_or_subject_has_nonascii($f)
1021 && !file_declares_8bit_cte($f));
1022 $broken_encoding{$f} = 1;
1023}
1024
1025if (!defined $auto_8bit_encoding && scalar %broken_encoding) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001026 print __("The following files are 8bit, but do not declare " .
1027 "a Content-Transfer-Encoding.\n");
Thomas Rast3cae7e52010-06-17 22:10:39 +02001028 foreach my $f (sort keys %broken_encoding) {
1029 print " $f\n";
1030 }
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001031 $auto_8bit_encoding = ask(__("Which 8bit encoding should I declare [UTF-8]? "),
Junio C Hamano852a15d2015-02-13 12:20:25 -08001032 valid_re => qr/.{4}/, confirm_only => 1,
Thomas Rast3cae7e52010-06-17 22:10:39 +02001033 default => "UTF-8");
1034}
1035
Thomas Rasta03bc5b2009-06-08 23:34:12 +02001036if (!$force) {
1037 for my $f (@files) {
Ævar Arnfjörð Bjarmason0d290a42010-09-30 13:43:01 +00001038 if (get_patch_subject($f) =~ /\Q*** SUBJECT HERE ***\E/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001039 die sprintf(__("Refusing to send because the patch\n\t%s\n"
Thomas Rasta03bc5b2009-06-08 23:34:12 +02001040 . "has the template subject '*** SUBJECT HERE ***'. "
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001041 . "Pass --force if you really want to send.\n"), $f);
Thomas Rasta03bc5b2009-06-08 23:34:12 +02001042 }
1043 }
1044}
1045
Remi Lespinetc46e27a2015-06-30 14:16:47 +02001046if (defined $sender) {
Remi Lespinetfa5b1aa2015-06-30 14:16:51 +02001047 $sender =~ s/^\s+|\s+$//g;
Remi Lespinetc46e27a2015-06-30 14:16:47 +02001048 ($sender) = expand_aliases($sender);
1049} else {
Ævar Arnfjörð Bjarmasonfef381e2021-05-28 11:23:46 +02001050 $sender = $repoauthor->() || $repocommitter->() || '';
Ryan Anderson83b24432005-07-31 04:17:25 -04001051}
1052
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001053# $sender could be an already sanitized address
1054# (e.g. sendemail.from could be manually sanitized by user).
1055# But it's a no-op to run sanitize_address on an already sanitized address.
1056$sender = sanitize_address($sender);
1057
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001058my $to_whom = __("To whom should the emails be sent (if anyone)?");
Felipe Contreras8cac13d2012-11-24 12:16:19 +01001059my $prompting = 0;
Junio C Hamano8796ff72010-10-26 22:02:03 -07001060if (!@initial_to && !defined $to_cmd) {
Junio C Hamano0d6b21e2016-04-24 12:31:44 -07001061 my $to = ask("$to_whom ",
Stephen Boyd61837492012-09-06 11:31:11 -07001062 default => "",
Junio C Hamano51bbccf2012-08-14 15:15:53 -07001063 valid_re => qr/\@.*\./, confirm_only => 1);
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001064 push @initial_to, parse_address_line($to) if defined $to; # sanitized/validated later
Ryan Anderson1f038a02005-09-05 01:13:07 -04001065 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -04001066}
1067
Eric Wong994d6c62006-05-14 19:13:44 -07001068sub expand_aliases {
Jeff King302e04e2009-07-23 07:09:29 -04001069 return map { expand_one_alias($_) } @_;
1070}
1071
1072my %EXPANDED_ALIASES;
1073sub expand_one_alias {
1074 my $alias = shift;
1075 if ($EXPANDED_ALIASES{$alias}) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001076 die sprintf(__("fatal: alias '%s' expands to itself\n"), $alias);
Jeff King302e04e2009-07-23 07:09:29 -04001077 }
1078 local $EXPANDED_ALIASES{$alias} = 1;
1079 return $aliases{$alias} ? expand_aliases(@{$aliases{$alias}}) : $alias;
Eric Wong994d6c62006-05-14 19:13:44 -07001080}
1081
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001082@initial_to = process_address_list(@initial_to);
1083@initial_cc = process_address_list(@initial_cc);
Ævar Arnfjörð Bjarmasone60f6d12019-05-09 13:48:29 +02001084@initial_bcc = process_address_list(@initial_bcc);
Eric Wong994d6c62006-05-14 19:13:44 -07001085
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001086if ($thread && !defined $initial_in_reply_to && $prompting) {
1087 $initial_in_reply_to = ask(
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001088 __("Message-ID to be used as In-Reply-To for the first email (if any)? "),
Stephen Boyd61837492012-09-06 11:31:11 -07001089 default => "",
Junio C Hamano51bbccf2012-08-14 15:15:53 -07001090 valid_re => qr/\@.*\./, confirm_only => 1);
Ryan Anderson83b24432005-07-31 04:17:25 -04001091}
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001092if (defined $initial_in_reply_to) {
1093 $initial_in_reply_to =~ s/^\s*<?//;
1094 $initial_in_reply_to =~ s/>?\s*$//;
1095 $initial_in_reply_to = "<$initial_in_reply_to>" if $initial_in_reply_to ne '';
Junio C Hamanoace9c2a2007-12-10 21:44:42 -08001096}
Mike Hommeyace72082007-12-09 18:17:28 +01001097
Christian Ludwigd11c9432018-03-04 00:58:14 +01001098if (defined $reply_to) {
1099 $reply_to =~ s/^\s+|\s+$//g;
1100 ($reply_to) = expand_aliases($reply_to);
1101 $reply_to = sanitize_address($reply_to);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001102}
Eric Wongaca7ad72006-05-15 02:34:44 -07001103
Gregory Anderscd5b33f2021-05-14 09:15:53 -06001104if (!defined $sendmail_cmd && !defined $smtp_server) {
Florian Klink1ab2fd42017-11-28 01:49:04 +01001105 my @sendmail_paths = qw( /usr/sbin/sendmail /usr/lib/sendmail );
1106 push @sendmail_paths, map {"$_/sendmail"} split /:/, $ENV{PATH};
1107 foreach (@sendmail_paths) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001108 if (-x $_) {
Gregory Anderscd5b33f2021-05-14 09:15:53 -06001109 $sendmail_cmd = $_;
Eric Wongaca7ad72006-05-15 02:34:44 -07001110 last;
1111 }
1112 }
Gregory Anderscd5b33f2021-05-14 09:15:53 -06001113
1114 if (!defined $sendmail_cmd) {
1115 $smtp_server = 'localhost'; # could be 127.0.0.1, too... *shrug*
1116 }
Ryan Anderson3342d852005-07-31 20:04:24 -04001117}
1118
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001119if ($compose && $compose > 0) {
1120 @files = ($compose_filename . ".final", @files);
Ryan Anderson1f038a02005-09-05 01:13:07 -04001121}
1122
Ryan Anderson83b24432005-07-31 04:17:25 -04001123# Variables we set as part of the loop over files
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001124our ($message_id, %mail, $subject, $in_reply_to, $references, $message,
Jay Soffiandc1460a2009-03-31 12:22:12 -04001125 $needs_confirm, $message_num, $ask_default);
Ryan Anderson83b24432005-07-31 04:17:25 -04001126
Eric Wong567ffeb2006-03-25 16:47:12 -08001127sub extract_valid_address {
1128 my $address = shift;
Ævar Arnfjörð Bjarmason35b6ab92010-09-30 19:03:31 +00001129 my $local_part_regexp = qr/[^<>"\s@]+/;
1130 my $domain_regexp = qr/[^.<>"\s@]+(?:\.[^.<>"\s@]+)+/;
Eric Wongdb3106b2006-05-15 02:41:01 -07001131
1132 # check for a local address:
Junio C Hamanoad9c18f2006-06-06 00:05:56 -07001133 return $address if ($address =~ /^($local_part_regexp)$/);
Eric Wongdb3106b2006-05-15 02:41:01 -07001134
Uwe Kleine-König155197e2007-08-09 15:27:57 +02001135 $address =~ s/^\s*<(.*)>\s*$/$1/;
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +02001136 my $have_email_valid = eval { require Email::Valid; 1 };
Eric Wong567ffeb2006-03-25 16:47:12 -08001137 if ($have_email_valid) {
Junio C Hamanoad9c18f2006-06-06 00:05:56 -07001138 return scalar Email::Valid->address($address);
Eric Wong567ffeb2006-03-25 16:47:12 -08001139 }
Krzysztof Mazur95c0d4b2012-11-22 19:12:09 +01001140
1141 # less robust/correct than the monster regexp in Email::Valid,
1142 # but still does a 99% job, and one less dependency
1143 return $1 if $address =~ /($local_part_regexp\@$domain_regexp)/;
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -07001144 return;
Eric Wong567ffeb2006-03-25 16:47:12 -08001145}
Ryan Anderson83b24432005-07-31 04:17:25 -04001146
Krzysztof Mazure4312252012-11-22 19:12:10 +01001147sub extract_valid_address_or_die {
1148 my $address = shift;
1149 $address = extract_valid_address($address);
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001150 die sprintf(__("error: unable to extract a valid address from: %s\n"), $address)
Krzysztof Mazure4312252012-11-22 19:12:10 +01001151 if !$address;
1152 return $address;
1153}
1154
1155sub validate_address {
1156 my $address = shift;
Krzysztof Mazurd0e98102012-11-22 19:12:12 +01001157 while (!extract_valid_address($address)) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001158 printf STDERR __("error: unable to extract a valid address from: %s\n"), $address;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001159 # TRANSLATORS: Make sure to include [q] [d] [e] in your
1160 # translation. The program will only accept English input
1161 # at this point.
1162 $_ = ask(__("What to do with this address? ([q]uit|[d]rop|[e]dit): "),
Krzysztof Mazurd0e98102012-11-22 19:12:12 +01001163 valid_re => qr/^(?:quit|q|drop|d|edit|e)/i,
Krzysztof Mazur5c80afe2012-11-22 19:12:11 +01001164 default => 'q');
1165 if (/^d/i) {
1166 return undef;
1167 } elsif (/^q/i) {
1168 cleanup_compose_files();
1169 exit(0);
1170 }
Junio C Hamano0d6b21e2016-04-24 12:31:44 -07001171 $address = ask("$to_whom ",
Krzysztof Mazurd0e98102012-11-22 19:12:12 +01001172 default => "",
1173 valid_re => qr/\@.*\./, confirm_only => 1);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001174 }
1175 return $address;
1176}
1177
1178sub validate_address_list {
1179 return (grep { defined $_ }
1180 map { validate_address($_) } @_);
Ryan Anderson83b24432005-07-31 04:17:25 -04001181}
1182
1183# Usually don't need to change anything below here.
1184
1185# we make a "fake" message id by taking the current number
1186# of seconds since the beginning of Unix time and tacking on
1187# a random number to the end, in case we are called quicker than
1188# 1 second since the last time we were called.
Ryan Anderson8037d1a2005-07-31 20:04:24 -04001189
1190# We'll setup a template for the message id, using the "from" address:
Ryan Anderson8037d1a2005-07-31 20:04:24 -04001191
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001192my ($message_id_stamp, $message_id_serial);
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001193sub make_message_id {
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001194 my $uniq;
1195 if (!defined $message_id_stamp) {
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +02001196 require POSIX;
1197 $message_id_stamp = POSIX::strftime("%Y%m%d%H%M%S.$$", gmtime(time));
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001198 $message_id_serial = 0;
1199 }
1200 $message_id_serial++;
1201 $uniq = "$message_id_stamp-$message_id_serial";
1202
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001203 my $du_part;
Ævar Arnfjörð Bjarmasonfef381e2021-05-28 11:23:46 +02001204 for ($sender, $repocommitter->(), $repoauthor->()) {
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001205 $du_part = extract_valid_address(sanitize_address($_));
1206 last if (defined $du_part and $du_part ne '');
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001207 }
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001208 if (not defined $du_part or $du_part eq '') {
Ævar Arnfjörð Bjarmason529dd382010-09-30 13:43:08 +00001209 require Sys::Hostname;
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001210 $du_part = 'user@' . Sys::Hostname::hostname();
1211 }
Eric Wongf916ab02016-04-06 20:07:14 +00001212 my $message_id_template = "<%s-%s>";
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001213 $message_id = sprintf($message_id_template, $uniq, $du_part);
Ryan Anderson8037d1a2005-07-31 20:04:24 -04001214 #print "new message id = $message_id\n"; # Was useful for debugging
Ryan Anderson83b24432005-07-31 04:17:25 -04001215}
1216
1217
1218
Eric Wonga5370b12006-03-25 03:01:01 -08001219$time = time - scalar $#files;
Ryan Anderson83b24432005-07-31 04:17:25 -04001220
Jürgen Rühle374c5902007-01-10 13:36:39 -08001221sub unquote_rfc2047 {
1222 local ($_) = @_;
Роман Донченко11f70a72014-12-14 18:59:46 +03001223 my $charset;
Роман Донченкоab47e2a2014-12-14 18:59:47 +03001224 my $sep = qr/[ \t]+/;
1225 s{$re_encoded_word(?:$sep$re_encoded_word)*}{
1226 my @words = split $sep, $&;
1227 foreach (@words) {
1228 m/$re_encoded_word/;
1229 $charset = $1;
1230 my $encoding = $2;
1231 my $text = $3;
1232 if ($encoding eq 'q' || $encoding eq 'Q') {
1233 $_ = $text;
1234 s/_/ /g;
1235 s/=([0-9A-F]{2})/chr(hex($1))/egi;
1236 } else {
1237 # other encodings not supported yet
1238 }
Роман Донченко11f70a72014-12-14 18:59:46 +03001239 }
Роман Донченкоab47e2a2014-12-14 18:59:47 +03001240 join '', @words;
Thomas Rastb622d4d2012-07-30 21:25:40 +02001241 }eg;
Роман Донченко11f70a72014-12-14 18:59:46 +03001242 return wantarray ? ($_, $charset) : $_;
Jürgen Rühle374c5902007-01-10 13:36:39 -08001243}
1244
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001245sub quote_rfc2047 {
1246 local $_ = shift;
Brandon Caseyd1fff6f2009-06-06 20:12:31 -05001247 my $encoding = shift || 'UTF-8';
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001248 s/([^-a-zA-Z0-9!*+\/])/sprintf("=%02X", ord($1))/eg;
1249 s/(.*)/=\?$encoding\?q\?$1\?=/;
1250 return $_;
1251}
1252
Brandon Caseya3a82622009-06-07 19:25:58 -05001253sub is_rfc2047_quoted {
1254 my $s = shift;
Brandon Caseya3a82622009-06-07 19:25:58 -05001255 length($s) <= 75 &&
Роман Донченко11f70a72014-12-14 18:59:46 +03001256 $s =~ m/^(?:"[[:ascii:]]*"|$re_encoded_word)$/o;
Brandon Caseya3a82622009-06-07 19:25:58 -05001257}
1258
Krzysztof Mazurce547802012-10-24 23:08:26 +02001259sub subject_needs_rfc2047_quoting {
1260 my $s = shift;
1261
Krzysztof Mazurce1459f2012-10-24 23:28:29 +02001262 return ($s =~ /[^[:ascii:]]/) || ($s =~ /=\?/);
Krzysztof Mazurce547802012-10-24 23:08:26 +02001263}
1264
1265sub quote_subject {
1266 local $subject = shift;
1267 my $encoding = shift || 'UTF-8';
1268
1269 if (subject_needs_rfc2047_quoting($subject)) {
1270 return quote_rfc2047($subject, $encoding);
1271 }
1272 return $subject;
1273}
1274
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001275# use the simplest quoting being able to handle the recipient
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001276sub sanitize_address {
Robin H. Johnson732263d2007-04-25 19:37:19 -07001277 my ($recipient) = @_;
Krzysztof Mazur831a4882012-11-22 19:12:08 +01001278
1279 # remove garbage after email address
1280 $recipient =~ s/(.*>).*$/$1/;
1281
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001282 my ($recipient_name, $recipient_addr) = ($recipient =~ /^(.*?)\s*(<.*)/);
1283
1284 if (not $recipient_name) {
Ævar Arnfjörð Bjarmasonff483892010-09-30 13:43:02 +00001285 return $recipient;
Robin H. Johnson732263d2007-04-25 19:37:19 -07001286 }
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001287
1288 # if recipient_name is already quoted, do nothing
Brandon Caseya3a82622009-06-07 19:25:58 -05001289 if (is_rfc2047_quoted($recipient_name)) {
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001290 return $recipient;
1291 }
1292
Remi Lespinet1fe97032015-06-30 14:16:49 +02001293 # remove non-escaped quotes
1294 $recipient_name =~ s/(^|[^\\])"/$1/g;
1295
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001296 # rfc2047 is needed if a non-ascii char is included
1297 if ($recipient_name =~ /[^[:ascii:]]/) {
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001298 $recipient_name = quote_rfc2047($recipient_name);
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001299 }
1300
1301 # double quotes are needed if specials or CTLs are included
1302 elsif ($recipient_name =~ /[][()<>@,;:\\".\000-\037\177]/) {
Remi Lespinet1fe97032015-06-30 14:16:49 +02001303 $recipient_name =~ s/([\\\r])/\\$1/g;
Ævar Arnfjörð Bjarmasond5c7d692010-09-30 13:43:03 +00001304 $recipient_name = qq["$recipient_name"];
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001305 }
1306
1307 return "$recipient_name $recipient_addr";
1308
Robin H. Johnson732263d2007-04-25 19:37:19 -07001309}
1310
Matthieu Moycb2922f2017-08-23 12:21:01 +02001311sub strip_garbage_one_address {
1312 my ($addr) = @_;
1313 chomp $addr;
1314 if ($addr =~ /^(("[^"]*"|[^"<]*)? *<[^>]*>).*/) {
1315 # "Foo Bar" <foobar@example.com> [possibly garbage here]
1316 # Foo Bar <foobar@example.com> [possibly garbage here]
1317 return $1;
1318 }
1319 if ($addr =~ /^(<[^>]*>).*/) {
1320 # <foo@example.com> [possibly garbage here]
1321 # if garbage contains other addresses, they are ignored.
1322 return $1;
1323 }
1324 if ($addr =~ /^([^"#,\s]*)/) {
1325 # address without quoting: remove anything after the address
1326 return $1;
1327 }
1328 return $addr;
1329}
1330
Krzysztof Mazure4312252012-11-22 19:12:10 +01001331sub sanitize_address_list {
1332 return (map { sanitize_address($_) } @_);
1333}
1334
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001335sub process_address_list {
Remi Lespinetb1c8a112015-06-30 14:16:50 +02001336 my @addr_list = map { parse_address_line($_) } @_;
1337 @addr_list = expand_aliases(@addr_list);
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001338 @addr_list = sanitize_address_list(@addr_list);
1339 @addr_list = validate_address_list(@addr_list);
1340 return @addr_list;
1341}
1342
Jari Aalto134550f2010-03-14 17:16:45 +02001343# Returns the local Fully Qualified Domain Name (FQDN) if available.
1344#
1345# Tightly configured MTAa require that a caller sends a real DNS
1346# domain name that corresponds the IP address in the HELO/EHLO
1347# handshake. This is used to verify the connection and prevent
1348# spammers from trying to hide their identity. If the DNS and IP don't
Elijah Newren15beaaa2019-11-05 17:07:23 +00001349# match, the receiving MTA may deny the connection.
Jari Aalto134550f2010-03-14 17:16:45 +02001350#
1351# Here is a deny example of Net::SMTP with the default "localhost.localdomain"
1352#
1353# Net::SMTP=GLOB(0x267ec28)>>> EHLO localhost.localdomain
1354# Net::SMTP=GLOB(0x267ec28)<<< 550 EHLO argument does not match calling host
1355#
1356# This maildomain*() code is based on ideas in Perl library Test::Reporter
1357# /usr/share/perl5/Test/Reporter/Mail/Util.pm ==> sub _maildomain ()
1358
Brian Gernhardt59a86302010-04-10 10:53:54 -04001359sub valid_fqdn {
1360 my $domain = shift;
Brandon Casey61ef5e92010-09-26 22:18:01 -05001361 return defined $domain && !($^O eq 'darwin' && $domain =~ /\.local$/) && $domain =~ /\./;
Brian Gernhardt59a86302010-04-10 10:53:54 -04001362}
1363
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001364sub maildomain_net {
Jari Aalto134550f2010-03-14 17:16:45 +02001365 my $maildomain;
1366
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +02001367 require Net::Domain;
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001368 my $domain = Net::Domain::domainname();
1369 $maildomain = $domain if valid_fqdn($domain);
Jari Aalto134550f2010-03-14 17:16:45 +02001370
1371 return $maildomain;
1372}
1373
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001374sub maildomain_mta {
Jari Aalto134550f2010-03-14 17:16:45 +02001375 my $maildomain;
1376
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001377 for my $host (qw(mailhost localhost)) {
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +02001378 require Net::SMTP;
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001379 my $smtp = Net::SMTP->new($host);
1380 if (defined $smtp) {
1381 my $domain = $smtp->domain;
1382 $smtp->quit;
Jari Aalto134550f2010-03-14 17:16:45 +02001383
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001384 $maildomain = $domain if valid_fqdn($domain);
Jari Aalto134550f2010-03-14 17:16:45 +02001385
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001386 last if $maildomain;
Jari Aalto134550f2010-03-14 17:16:45 +02001387 }
1388 }
1389
1390 return $maildomain;
1391}
1392
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001393sub maildomain {
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001394 return maildomain_net() || maildomain_mta() || 'localhost.localdomain';
Jari Aalto134550f2010-03-14 17:16:45 +02001395}
1396
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001397sub smtp_host_string {
1398 if (defined $smtp_server_port) {
1399 return "$smtp_server:$smtp_server_port";
1400 } else {
1401 return $smtp_server;
1402 }
1403}
1404
1405# Returns 1 if authentication succeeded or was not necessary
1406# (smtp_user was not specified), and 0 otherwise.
1407
1408sub smtp_auth_maybe {
Joshua Watt8dd9b3f2018-10-22 22:24:25 -05001409 if (!defined $smtp_authuser || $auth || (defined $smtp_auth && $smtp_auth eq "none")) {
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001410 return 1;
1411 }
1412
1413 # Workaround AUTH PLAIN/LOGIN interaction defect
1414 # with Authen::SASL::Cyrus
1415 eval {
1416 require Authen::SASL;
1417 Authen::SASL->import(qw(Perl));
1418 };
1419
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001420 # Check mechanism naming as defined in:
1421 # https://tools.ietf.org/html/rfc4422#page-8
Brian Norris904f6e72015-09-18 15:12:50 -07001422 if ($smtp_auth && $smtp_auth !~ /^(\b[A-Z0-9-_]{1,20}\s*)*$/) {
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001423 die "invalid smtp auth: '${smtp_auth}'";
1424 }
1425
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001426 # TODO: Authentication may fail not because credentials were
1427 # invalid but due to other reasons, in which we should not
1428 # reject credentials.
1429 $auth = Git::credential({
1430 'protocol' => 'smtp',
1431 'host' => smtp_host_string(),
1432 'username' => $smtp_authuser,
1433 # if there's no password, "git credential fill" will
1434 # give us one, otherwise it'll just pass this one.
1435 'password' => $smtp_authpass
1436 }, sub {
1437 my $cred = shift;
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001438
1439 if ($smtp_auth) {
1440 my $sasl = Authen::SASL->new(
1441 mechanism => $smtp_auth,
1442 callback => {
1443 user => $cred->{'username'},
1444 pass => $cred->{'password'},
1445 authname => $cred->{'username'},
1446 }
1447 );
1448
1449 return !!$smtp->auth($sasl);
1450 }
1451
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001452 return !!$smtp->auth($cred->{'username'}, $cred->{'password'});
1453 });
1454
1455 return $auth;
1456}
1457
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001458sub ssl_verify_params {
1459 eval {
1460 require IO::Socket::SSL;
1461 IO::Socket::SSL->import(qw/SSL_VERIFY_PEER SSL_VERIFY_NONE/);
1462 };
1463 if ($@) {
1464 print STDERR "Not using SSL_VERIFY_PEER due to out-of-date IO::Socket::SSL.\n";
1465 return;
1466 }
1467
1468 if (!defined $smtp_ssl_cert_path) {
Ruben Kerkhof01645b72014-01-15 21:31:11 +04001469 # use the OpenSSL defaults
1470 return (SSL_verify_mode => SSL_VERIFY_PEER());
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001471 }
1472
1473 if ($smtp_ssl_cert_path eq "") {
1474 return (SSL_verify_mode => SSL_VERIFY_NONE());
1475 } elsif (-d $smtp_ssl_cert_path) {
1476 return (SSL_verify_mode => SSL_VERIFY_PEER(),
1477 SSL_ca_path => $smtp_ssl_cert_path);
1478 } elsif (-f $smtp_ssl_cert_path) {
1479 return (SSL_verify_mode => SSL_VERIFY_PEER(),
1480 SSL_ca_file => $smtp_ssl_cert_path);
1481 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001482 die sprintf(__("CA path \"%s\" does not exist"), $smtp_ssl_cert_path);
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001483 }
1484}
1485
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001486sub file_name_is_absolute {
1487 my ($path) = @_;
1488
1489 # msys does not grok DOS drive-prefixes
1490 if ($^O eq 'msys') {
Junio C Hamanof24ecf52014-04-23 09:37:38 -07001491 return ($path =~ m#^/# || $path =~ m#^[a-zA-Z]\:#)
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001492 }
1493
1494 require File::Spec::Functions;
1495 return File::Spec::Functions::file_name_is_absolute($path);
1496}
1497
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001498# Prepares the email, then asks the user what to do.
1499#
1500# If the user chooses to send the email, it's sent and 1 is returned.
1501# If the user chooses not to send the email, 0 is returned.
1502# If the user decides they want to make further edits, -1 is returned and the
1503# caller is expected to call send_message again after the edits are performed.
1504#
1505# If an error occurs sending the email, this just dies.
Michael Witten15da1082009-04-13 13:23:51 -05001506
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001507sub send_message {
Eric Wong4bc87a22006-03-25 17:20:48 -08001508 my @recipients = unique_email_list(@to);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001509 @cc = (grep { my $cc = extract_valid_address_or_die($_);
Joe Perches83acaae2010-11-20 15:06:05 -08001510 not grep { $cc eq $_ || $_ =~ /<\Q${cc}\E>$/ } @recipients
Ask Bjørn Hansen7ac17522007-11-19 03:00:26 -08001511 }
Ask Bjørn Hansen7ac17522007-11-19 03:00:26 -08001512 @cc);
Eric Wong4bc87a22006-03-25 17:20:48 -08001513 my $to = join (",\n\t", @recipients);
Ævar Arnfjörð Bjarmasone60f6d12019-05-09 13:48:29 +02001514 @recipients = unique_email_list(@recipients,@cc,@initial_bcc);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001515 @recipients = (map { extract_valid_address_or_die($_) } @recipients);
Jakub Narebski6bdca892006-07-07 20:57:55 +02001516 my $date = format_2822_time($time++);
Martin Langhoffe923eff2006-05-03 09:44:36 +12001517 my $gitversion = '@@GIT_VERSION@@';
1518 if ($gitversion =~ m/..GIT_VERSION../) {
Petr Baudis3cb8caf2006-07-03 22:47:58 +02001519 $gitversion = Git::version();
Martin Langhoffe923eff2006-05-03 09:44:36 +12001520 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001521
Joe Perches02461e02009-10-08 10:03:26 -07001522 my $cc = join(",\n\t", unique_email_list(@cc));
Junio C Hamanof06a6a42007-04-16 16:51:47 -07001523 my $ccline = "";
1524 if ($cc ne '') {
1525 $ccline = "\nCc: $cc";
1526 }
Jeff King4f3d3702007-12-17 15:51:34 -05001527 make_message_id() unless defined($message_id);
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001528
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001529 my $header = "From: $sender
Junio C Hamanof06a6a42007-04-16 16:51:47 -07001530To: $to${ccline}
Eric Wong4bc87a22006-03-25 17:20:48 -08001531Subject: $subject
Eric Wong4bc87a22006-03-25 17:20:48 -08001532Date: $date
1533Message-Id: $message_id
Eric Wong4bc87a22006-03-25 17:20:48 -08001534";
Luis Henriquesac1596a2014-03-24 21:38:27 +00001535 if ($use_xmailer) {
1536 $header .= "X-Mailer: git-send-email $gitversion\n";
1537 }
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001538 if ($in_reply_to) {
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001539
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001540 $header .= "In-Reply-To: $in_reply_to\n";
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001541 $header .= "References: $references\n";
1542 }
Christian Ludwigd11c9432018-03-04 00:58:14 +01001543 if ($reply_to) {
1544 $header .= "Reply-To: $reply_to\n";
1545 }
Junio C Hamanoce91c2f2006-10-05 16:36:49 -07001546 if (@xh) {
1547 $header .= join("\n", @xh) . "\n";
1548 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001549
Robin H. Johnsonc38f0242007-04-25 19:37:20 -07001550 my @sendmail_parameters = ('-i', @recipients);
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001551 my $raw_from = $sender;
Felipe Contrerasc89e3242009-11-26 21:04:29 +02001552 if (defined $envelope_sender && $envelope_sender ne "auto") {
1553 $raw_from = $envelope_sender;
1554 }
Robin H. Johnsonf073a592007-04-25 19:37:22 -07001555 $raw_from = extract_valid_address($raw_from);
1556 unshift (@sendmail_parameters,
1557 '-f', $raw_from) if(defined $envelope_sender);
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001558
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001559 if ($needs_confirm && !$dry_run) {
1560 print "\n$header\n";
1561 if ($needs_confirm eq "inform") {
1562 $confirm_unconfigured = 0; # squelch this message for the rest of this run
Jay Soffian6e182512009-03-28 21:39:10 -04001563 $ask_default = "y"; # assume yes on EOF since user hasn't explicitly asked for confirmation
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001564 print __ <<EOF ;
1565 The Cc list above has been expanded by additional
1566 addresses found in the patch commit message. By default
1567 send-email prompts before sending whenever this occurs.
1568 This behavior is controlled by the sendemail.confirm
1569 configuration setting.
1570
1571 For additional information, run 'git send-email --help'.
1572 To retain the current behavior, but squelch this message,
1573 run 'git config --global sendemail.confirm auto'.
1574
1575EOF
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001576 }
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001577 # TRANSLATORS: Make sure to include [y] [n] [e] [q] [a] in your
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001578 # translation. The program will only accept English input
1579 # at this point.
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001580 $_ = ask(__("Send this email? ([y]es|[n]o|[e]dit|[q]uit|[a]ll): "),
1581 valid_re => qr/^(?:yes|y|no|n|edit|e|quit|q|all|a)/i,
Jay Soffian6e182512009-03-28 21:39:10 -04001582 default => $ask_default);
Vasco Almeida46493102016-12-14 11:54:36 -01001583 die __("Send this email reply required") unless defined $_;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001584 if (/^n/i) {
Michael Witten15da1082009-04-13 13:23:51 -05001585 return 0;
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001586 } elsif (/^e/i) {
1587 return -1;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001588 } elsif (/^q/i) {
1589 cleanup_compose_files();
1590 exit(0);
1591 } elsif (/^a/i) {
1592 $confirm = 'never';
1593 }
1594 }
1595
Pascal Obry052fbea2010-09-06 20:12:11 +02001596 unshift (@sendmail_parameters, @smtp_server_options);
1597
Matthew Wilcox61302592006-10-10 08:58:23 -06001598 if ($dry_run) {
1599 # We don't want to send the email.
Gregory Anderscd5b33f2021-05-14 09:15:53 -06001600 } elsif (defined $sendmail_cmd || file_name_is_absolute($smtp_server)) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001601 my $pid = open my $sm, '|-';
1602 defined $pid or die $!;
1603 if (!$pid) {
Gregory Anderscd5b33f2021-05-14 09:15:53 -06001604 if (defined $sendmail_cmd) {
1605 exec ("sh", "-c", "$sendmail_cmd \"\$@\"", "-", @sendmail_parameters)
1606 or die $!;
1607 } else {
1608 exec ($smtp_server, @sendmail_parameters)
1609 or die $!;
1610 }
Eric Wongaca7ad72006-05-15 02:34:44 -07001611 }
1612 print $sm "$header\n$message";
Ævar Arnfjörð Bjarmason5e2c2ab2010-09-30 13:43:07 +00001613 close $sm or die $!;
Eric Wongaca7ad72006-05-15 02:34:44 -07001614 } else {
Junio C Hamano44b24762007-09-25 17:27:54 -07001615
1616 if (!defined $smtp_server) {
Vasco Almeida46493102016-12-14 11:54:36 -01001617 die __("The required SMTP server is not properly defined.")
Junio C Hamano44b24762007-09-25 17:27:54 -07001618 }
1619
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001620 require Net::SMTP;
Jonathan Niederbfbfc9a2017-05-31 17:17:43 -07001621 my $use_net_smtp_ssl = version->parse($Net::SMTP::VERSION) < version->parse("2.34");
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001622 $smtp_domain ||= maildomain();
1623
Thomas Rastf6bebd12008-06-25 21:42:43 +02001624 if ($smtp_encryption eq 'ssl') {
Junio C Hamano44b24762007-09-25 17:27:54 -07001625 $smtp_server_port ||= 465; # ssmtp
Thomas Rast5508f3e2013-12-01 23:48:43 +01001626 require IO::Socket::SSL;
John Keeping9d605242015-12-03 21:47:18 +00001627
1628 # Suppress "variable accessed once" warning.
1629 {
1630 no warnings 'once';
1631 $IO::Socket::SSL::DEBUG = 1;
1632 }
1633
Thomas Rast5508f3e2013-12-01 23:48:43 +01001634 # Net::SMTP::SSL->new() does not forward any SSL options
1635 IO::Socket::SSL::set_client_defaults(
1636 ssl_verify_params());
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001637
1638 if ($use_net_smtp_ssl) {
1639 require Net::SMTP::SSL;
1640 $smtp ||= Net::SMTP::SSL->new($smtp_server,
1641 Hello => $smtp_domain,
1642 Port => $smtp_server_port,
1643 Debug => $debug_net_smtp);
1644 }
1645 else {
1646 $smtp ||= Net::SMTP->new($smtp_server,
1647 Hello => $smtp_domain,
1648 Port => $smtp_server_port,
1649 Debug => $debug_net_smtp,
1650 SSL => 1);
1651 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001652 }
Jules Maselbas636f3d72018-07-14 10:58:48 +02001653 elsif (!$smtp) {
brian m. carlson1a741bf2013-07-04 22:04:52 +00001654 $smtp_server_port ||= 25;
1655 $smtp ||= Net::SMTP->new($smtp_server,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001656 Hello => $smtp_domain,
brian m. carlson1a741bf2013-07-04 22:04:52 +00001657 Debug => $debug_net_smtp,
1658 Port => $smtp_server_port);
Yakov Lernerfb3650e2009-09-25 15:10:21 -07001659 if ($smtp_encryption eq 'tls' && $smtp) {
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001660 if ($use_net_smtp_ssl) {
1661 $smtp->command('STARTTLS');
1662 $smtp->response();
1663 if ($smtp->code != 220) {
1664 die sprintf(__("Server does not support STARTTLS! %s"), $smtp->message);
1665 }
1666 require Net::SMTP::SSL;
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001667 $smtp = Net::SMTP::SSL->start_SSL($smtp,
1668 ssl_verify_params())
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001669 or die sprintf(__("STARTTLS failed! %s"), IO::Socket::SSL::errstr());
Thomas Rastf6bebd12008-06-25 21:42:43 +02001670 }
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001671 else {
1672 $smtp->starttls(ssl_verify_params())
1673 or die sprintf(__("STARTTLS failed! %s"), IO::Socket::SSL::errstr());
1674 }
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001675 # Send EHLO again to receive fresh
1676 # supported commands
1677 $smtp->hello($smtp_domain);
Thomas Rastf6bebd12008-06-25 21:42:43 +02001678 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001679 }
Junio C Hamano44b24762007-09-25 17:27:54 -07001680
1681 if (!$smtp) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001682 die __("Unable to initialize SMTP properly. Check config and use --smtp-debug."),
1683 " VALUES: server=$smtp_server ",
Jari Aaltoe5afb3a2010-03-14 17:15:33 +02001684 "encryption=$smtp_encryption ",
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001685 "hello=$smtp_domain",
Sylvain Rabota1dd7e12011-04-29 20:23:24 +02001686 defined $smtp_server_port ? " port=$smtp_server_port" : "";
Junio C Hamano44b24762007-09-25 17:27:54 -07001687 }
1688
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001689 smtp_auth_maybe or die $smtp->message;
Michael Witten2363d742008-02-03 19:53:56 -05001690
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001691 $smtp->mail( $raw_from ) or die $smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001692 $smtp->to( @recipients ) or die $smtp->message;
1693 $smtp->data or die $smtp->message;
Stefan Agnerf60c4832015-09-30 09:26:09 +02001694 $smtp->datasend("$header\n") or die $smtp->message;
1695 my @lines = split /^/, $message;
1696 foreach my $line (@lines) {
1697 $smtp->datasend("$line") or die $smtp->message;
1698 }
Eric Wongaca7ad72006-05-15 02:34:44 -07001699 $smtp->dataend() or die $smtp->message;
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001700 $smtp->code =~ /250|200/ or die sprintf(__("Failed to send %s\n"), $subject).$smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001701 }
Ryan Anderson27184352006-02-05 20:13:52 -05001702 if ($quiet) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001703 printf($dry_run ? __("Dry-Sent %s\n") : __("Sent %s\n"), $subject);
Ryan Anderson27184352006-02-05 20:13:52 -05001704 } else {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001705 print($dry_run ? __("Dry-OK. Log says:\n") : __("OK. Log says:\n"));
Gregory Anderscd5b33f2021-05-14 09:15:53 -06001706 if (!defined $sendmail_cmd && !file_name_is_absolute($smtp_server)) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001707 print "Server: $smtp_server\n";
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001708 print "MAIL FROM:<$raw_from>\n";
Joe Perches02461e02009-10-08 10:03:26 -07001709 foreach my $entry (@recipients) {
1710 print "RCPT TO:<$entry>\n";
1711 }
Eric Wongaca7ad72006-05-15 02:34:44 -07001712 } else {
Gregory Anderscd5b33f2021-05-14 09:15:53 -06001713 my $sm;
1714 if (defined $sendmail_cmd) {
1715 $sm = $sendmail_cmd;
1716 } else {
1717 $sm = $smtp_server;
1718 }
1719
1720 print "Sendmail: $sm ".join(' ',@sendmail_parameters)."\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001721 }
David D. Kilzerb7f30e02007-11-18 20:14:55 -08001722 print $header, "\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001723 if ($smtp) {
Vasco Almeida46493102016-12-14 11:54:36 -01001724 print __("Result: "), $smtp->code, ' ',
Eric Wongaca7ad72006-05-15 02:34:44 -07001725 ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
1726 } else {
Vasco Almeida46493102016-12-14 11:54:36 -01001727 print __("Result: OK\n");
Eric Wongaca7ad72006-05-15 02:34:44 -07001728 }
Ryan Anderson30d08b32006-02-02 11:56:06 -05001729 }
Michael Witten15da1082009-04-13 13:23:51 -05001730
1731 return 1;
Ryan Anderson83b24432005-07-31 04:17:25 -04001732}
1733
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001734$in_reply_to = $initial_in_reply_to;
1735$references = $initial_in_reply_to || '';
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001736$message_num = 0;
Ryan Anderson83b24432005-07-31 04:17:25 -04001737
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001738# Prepares the email, prompts the user, sends it out
1739# Returns 0 if an edit was done and the function should be called again, or 1
1740# otherwise.
1741sub process_file {
1742 my ($t) = @_;
1743
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001744 open my $fh, "<", $t or die sprintf(__("can't open file %s"), $t);
Ryan Anderson83b24432005-07-31 04:17:25 -04001745
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001746 my $author = undef;
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001747 my $sauthor = undef;
Jeff King8291db62007-11-16 05:49:09 -05001748 my $author_encoding;
1749 my $has_content_type;
1750 my $body_encoding;
Paolo Bonzinibb294562014-11-25 15:00:26 +01001751 my $xfer_encoding;
1752 my $has_mime_version;
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001753 @to = ();
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001754 @cc = ();
Junio C Hamanoce91c2f2006-10-05 16:36:49 -07001755 @xh = ();
Junio C Hamanoe6b09642006-10-07 03:09:05 -07001756 my $input_format = undef;
Jay Soffian50126992009-02-14 23:32:14 -05001757 my @header = ();
Marvin Häusere0821132021-08-30 15:30:01 +00001758 $subject = $initial_subject;
Ryan Anderson83b24432005-07-31 04:17:25 -04001759 $message = "";
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001760 $message_num++;
Jay Soffian50126992009-02-14 23:32:14 -05001761 # First unfold multiline header fields
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001762 while(<$fh>) {
Jay Soffian50126992009-02-14 23:32:14 -05001763 last if /^\s*$/;
1764 if (/^\s+\S/ and @header) {
1765 chomp($header[$#header]);
1766 s/^\s+/ /;
1767 $header[$#header] .= $_;
1768 } else {
1769 push(@header, $_);
1770 }
1771 }
1772 # Now parse the header
1773 foreach(@header) {
1774 if (/^From /) {
1775 $input_format = 'mbox';
1776 next;
1777 }
1778 chomp;
1779 if (!defined $input_format && /^[-A-Za-z]+:\s/) {
1780 $input_format = 'mbox';
1781 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001782
Jay Soffian50126992009-02-14 23:32:14 -05001783 if (defined $input_format && $input_format eq 'mbox') {
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001784 if (/^Subject:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001785 $subject = $1;
1786 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001787 elsif (/^From:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001788 ($author, $author_encoding) = unquote_rfc2047($1);
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001789 $sauthor = sanitize_address($author);
Jay Soffian50126992009-02-14 23:32:14 -05001790 next if $suppress_cc{'author'};
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001791 next if $suppress_cc{'self'} and $sauthor eq $sender;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001792 printf(__("(mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001793 $1, $_) unless $quiet;
1794 push @cc, $1;
1795 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001796 elsif (/^To:\s+(.*)$/i) {
Stephen Boyd21802cd2010-09-29 00:26:44 -07001797 foreach my $addr (parse_address_line($1)) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001798 printf(__("(mbox) Adding to: %s from line '%s'\n"),
Stephen Boyd21802cd2010-09-29 00:26:44 -07001799 $addr, $_) unless $quiet;
Krzysztof Mazure4312252012-11-22 19:12:10 +01001800 push @to, $addr;
Stephen Boyd21802cd2010-09-29 00:26:44 -07001801 }
1802 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001803 elsif (/^Cc:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001804 foreach my $addr (parse_address_line($1)) {
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001805 my $qaddr = unquote_rfc2047($addr);
1806 my $saddr = sanitize_address($qaddr);
1807 if ($saddr eq $sender) {
David Brown65648282007-12-25 19:56:29 -08001808 next if ($suppress_cc{'self'});
David Brown65648282007-12-25 19:56:29 -08001809 } else {
1810 next if ($suppress_cc{'cc'});
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001811 }
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001812 printf(__("(mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001813 $addr, $_) unless $quiet;
1814 push @cc, $addr;
Ryan Anderson83b24432005-07-31 04:17:25 -04001815 }
1816 }
Jay Soffian50126992009-02-14 23:32:14 -05001817 elsif (/^Content-type:/i) {
1818 $has_content_type = 1;
1819 if (/charset="?([^ "]+)/) {
1820 $body_encoding = $1;
1821 }
1822 push @xh, $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001823 }
Paolo Bonzinibb294562014-11-25 15:00:26 +01001824 elsif (/^MIME-Version/i) {
1825 $has_mime_version = 1;
1826 push @xh, $_;
1827 }
Jay Soffian50126992009-02-14 23:32:14 -05001828 elsif (/^Message-Id: (.*)/i) {
1829 $message_id = $1;
1830 }
Paolo Bonzinibb294562014-11-25 15:00:26 +01001831 elsif (/^Content-Transfer-Encoding: (.*)/i) {
1832 $xfer_encoding = $1 if not defined $xfer_encoding;
1833 }
Stefan Agner256be1d2018-04-17 23:16:30 +02001834 elsif (/^In-Reply-To: (.*)/i) {
Rafael Aquinif9f60d72020-06-29 10:11:04 -04001835 if (!$initial_in_reply_to || $thread) {
1836 $in_reply_to = $1;
1837 }
Stefan Agner256be1d2018-04-17 23:16:30 +02001838 }
1839 elsif (/^References: (.*)/i) {
Rafael Aquinif9f60d72020-06-29 10:11:04 -04001840 if (!$initial_in_reply_to || $thread) {
1841 $references = $1;
1842 }
Stefan Agner256be1d2018-04-17 23:16:30 +02001843 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001844 elsif (!/^Date:\s/i && /^[-A-Za-z]+:\s+\S/) {
Jay Soffian50126992009-02-14 23:32:14 -05001845 push @xh, $_;
1846 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001847 } else {
Jay Soffian50126992009-02-14 23:32:14 -05001848 # In the traditional
1849 # "send lots of email" format,
1850 # line 1 = cc
1851 # line 2 = subject
1852 # So let's support that, too.
1853 $input_format = 'lots';
1854 if (@cc == 0 && !$suppress_cc{'cc'}) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001855 printf(__("(non-mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001856 $_, $_) unless $quiet;
1857 push @cc, $_;
1858 } elsif (!defined $subject) {
1859 $subject = $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001860 }
1861 }
1862 }
Jay Soffian50126992009-02-14 23:32:14 -05001863 # Now parse the message body
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001864 while(<$fh>) {
Jay Soffian50126992009-02-14 23:32:14 -05001865 $message .= $_;
Baruch Siach9c545812019-03-16 21:26:50 +02001866 if (/^([a-z][a-z-]*-by|Cc): (.*)/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001867 chomp;
Jay Soffian3531e272009-02-14 23:32:15 -05001868 my ($what, $c) = ($1, $2);
Matthieu Moycb2922f2017-08-23 12:21:01 +02001869 # strip garbage for the address we'll use:
1870 $c = strip_garbage_one_address($c);
1871 # sanitize a bit more to decide whether to suppress the address:
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001872 my $sc = sanitize_address($c);
1873 if ($sc eq $sender) {
Jay Soffian3531e272009-02-14 23:32:15 -05001874 next if ($suppress_cc{'self'});
1875 } else {
Rasmus Villemoesef0cc1d2018-10-16 09:39:23 +02001876 if ($what =~ /^Signed-off-by$/i) {
1877 next if $suppress_cc{'sob'};
1878 } elsif ($what =~ /-by$/i) {
1879 next if $suppress_cc{'misc-by'};
1880 } elsif ($what =~ /Cc/i) {
1881 next if $suppress_cc{'bodycc'};
1882 }
Jay Soffian3531e272009-02-14 23:32:15 -05001883 }
Rasmus Villemoesfb34fe62018-10-10 13:13:50 +02001884 if ($c !~ /.+@.+|<.+>/) {
1885 printf("(body) Ignoring %s from line '%s'\n",
1886 $what, $_) unless $quiet;
1887 next;
1888 }
Jay Soffian50126992009-02-14 23:32:14 -05001889 push @cc, $c;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001890 printf(__("(body) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001891 $c, $_) unless $quiet;
1892 }
1893 }
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001894 close $fh;
Joe Perches324a8bd2007-08-17 18:51:12 -07001895
Joe Perches6e74e072010-09-24 10:03:00 -07001896 push @to, recipients_cmd("to-cmd", "to", $to_cmd, $t)
1897 if defined $to_cmd;
1898 push @cc, recipients_cmd("cc-cmd", "cc", $cc_cmd, $t)
1899 if defined $cc_cmd && !$suppress_cc{'cccmd'};
Joe Perches324a8bd2007-08-17 18:51:12 -07001900
Thomas Rast3cae7e52010-06-17 22:10:39 +02001901 if ($broken_encoding{$t} && !$has_content_type) {
Paolo Bonzinibb294562014-11-25 15:00:26 +01001902 $xfer_encoding = '8bit' if not defined $xfer_encoding;
Thomas Rast3cae7e52010-06-17 22:10:39 +02001903 $has_content_type = 1;
Paolo Bonzinibb294562014-11-25 15:00:26 +01001904 push @xh, "Content-Type: text/plain; charset=$auto_8bit_encoding";
Thomas Rast3cae7e52010-06-17 22:10:39 +02001905 $body_encoding = $auto_8bit_encoding;
1906 }
1907
Krzysztof Mazurce547802012-10-24 23:08:26 +02001908 if ($broken_encoding{$t} && !is_rfc2047_quoted($subject)) {
1909 $subject = quote_subject($subject, $auto_8bit_encoding);
Thomas Rast3cae7e52010-06-17 22:10:39 +02001910 }
1911
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001912 if (defined $sauthor and $sauthor ne $sender) {
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001913 $message = "From: $author\n\n$message";
Jeff King8291db62007-11-16 05:49:09 -05001914 if (defined $author_encoding) {
1915 if ($has_content_type) {
1916 if ($body_encoding eq $author_encoding) {
1917 # ok, we already have the right encoding
1918 }
1919 else {
1920 # uh oh, we should re-encode
1921 }
1922 }
1923 else {
Paolo Bonzinibb294562014-11-25 15:00:26 +01001924 $xfer_encoding = '8bit' if not defined $xfer_encoding;
Thomas Rast3cae7e52010-06-17 22:10:39 +02001925 $has_content_type = 1;
Jeff King8291db62007-11-16 05:49:09 -05001926 push @xh,
Paolo Bonzinibb294562014-11-25 15:00:26 +01001927 "Content-Type: text/plain; charset=$author_encoding";
Jeff King8291db62007-11-16 05:49:09 -05001928 }
1929 }
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001930 }
brian m. carlsone67a2282018-07-08 22:17:12 +00001931 $xfer_encoding = '8bit' if not defined $xfer_encoding;
1932 ($message, $xfer_encoding) = apply_transfer_encoding(
1933 $message, $xfer_encoding, $target_xfer_encoding);
1934 push @xh, "Content-Transfer-Encoding: $xfer_encoding";
1935 unshift @xh, 'MIME-Version: 1.0' unless $has_mime_version;
Ryan Anderson83b24432005-07-31 04:17:25 -04001936
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001937 $needs_confirm = (
1938 $confirm eq "always" or
1939 ($confirm =~ /^(?:auto|cc)$/ && @cc) or
1940 ($confirm =~ /^(?:auto|compose)$/ && $compose && $message_num == 1));
1941 $needs_confirm = "inform" if ($needs_confirm && $confirm_unconfigured && @cc);
1942
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001943 @to = process_address_list(@to);
1944 @cc = process_address_list(@cc);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001945
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001946 @to = (@initial_to, @to);
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001947 @cc = (@initial_cc, @cc);
1948
Michael S. Tsirkinf515c902014-04-29 08:41:16 +03001949 if ($message_num == 1) {
1950 if (defined $cover_cc and $cover_cc) {
1951 @initial_cc = @cc;
1952 }
1953 if (defined $cover_to and $cover_to) {
1954 @initial_to = @to;
1955 }
1956 }
1957
Michael Witten15da1082009-04-13 13:23:51 -05001958 my $message_was_sent = send_message();
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001959 if ($message_was_sent == -1) {
1960 do_edit($t);
1961 return 0;
1962 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001963
1964 # set up for the next message
Marvin Häusere0821132021-08-30 15:30:01 +00001965 if ($thread) {
1966 if ($message_was_sent &&
1967 ($chain_reply_to || !defined $in_reply_to || length($in_reply_to) == 0 ||
1968 $message_num == 1)) {
1969 $in_reply_to = $message_id;
1970 if (length $references > 0) {
1971 $references .= "\n $message_id";
1972 } else {
1973 $references = "$message_id";
1974 }
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001975 }
Marvin Häusere0821132021-08-30 15:30:01 +00001976 } elsif (!defined $initial_in_reply_to) {
1977 # --thread and --in-reply-to manage the "In-Reply-To" header and by
1978 # extension the "References" header. If these commands are not used, reset
1979 # the header values to their defaults.
1980 $in_reply_to = undef;
1981 $references = '';
Ryan Anderson78488b22005-07-31 20:04:24 -04001982 }
Jeff King4f3d3702007-12-17 15:51:34 -05001983 $message_id = undef;
xiaoqiang zhao5453b832017-05-21 20:59:50 +08001984 $num_sent++;
1985 if (defined $batch_size && $num_sent == $batch_size) {
1986 $num_sent = 0;
1987 $smtp->quit if defined $smtp;
1988 undef $smtp;
1989 undef $auth;
1990 sleep($relogin_delay) if defined $relogin_delay;
1991 }
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001992
1993 return 1;
1994}
1995
1996foreach my $t (@files) {
1997 while (!process_file($t)) {
1998 # user edited the file
1999 }
Ryan Anderson83b24432005-07-31 04:17:25 -04002000}
Ryan Andersone2057352005-08-02 21:45:22 -04002001
Joe Perches6e74e072010-09-24 10:03:00 -07002002# Execute a command (e.g. $to_cmd) to get a list of email addresses
2003# and return a results array
2004sub recipients_cmd {
2005 my ($prefix, $what, $cmd, $file) = @_;
2006
Joe Perches6e74e072010-09-24 10:03:00 -07002007 my @addresses = ();
Ramkumar Ramachandraa47eab02013-03-31 18:40:42 -07002008 open my $fh, "-|", "$cmd \Q$file\E"
Vasco Almeida3c5cd202016-12-14 11:54:37 -01002009 or die sprintf(__("(%s) Could not execute '%s'"), $prefix, $cmd);
Junio C Hamano7ebee442010-10-26 22:02:52 -07002010 while (my $address = <$fh>) {
Joe Perches6e74e072010-09-24 10:03:00 -07002011 $address =~ s/^\s*//g;
2012 $address =~ s/\s*$//g;
2013 $address = sanitize_address($address);
Michael S. Tsirkinda187592013-06-05 21:11:00 +03002014 next if ($address eq $sender and $suppress_cc{'self'});
Joe Perches6e74e072010-09-24 10:03:00 -07002015 push @addresses, $address;
Vasco Almeida3c5cd202016-12-14 11:54:37 -01002016 printf(__("(%s) Adding %s: %s from: '%s'\n"),
2017 $prefix, $what, $address, $cmd) unless $quiet;
Joe Perches6e74e072010-09-24 10:03:00 -07002018 }
Junio C Hamano7ebee442010-10-26 22:02:52 -07002019 close $fh
Vasco Almeida3c5cd202016-12-14 11:54:37 -01002020 or die sprintf(__("(%s) failed to close pipe to '%s'"), $prefix, $cmd);
Joe Perches6e74e072010-09-24 10:03:00 -07002021 return @addresses;
2022}
2023
Jay Soffianc1f2aa42009-03-02 23:52:18 -05002024cleanup_compose_files();
Ryan Anderson1f038a02005-09-05 01:13:07 -04002025
Ævar Arnfjörð Bjarmason4bf597e2010-09-30 13:43:00 +00002026sub cleanup_compose_files {
Jay Soffianc1f2aa42009-03-02 23:52:18 -05002027 unlink($compose_filename, $compose_filename . ".final") if $compose;
Ryan Anderson1f038a02005-09-05 01:13:07 -04002028}
2029
Eric Wong4bc87a22006-03-25 17:20:48 -08002030$smtp->quit if $smtp;
Ryan Andersone2057352005-08-02 21:45:22 -04002031
Paolo Bonzini8d814082014-11-25 15:00:27 +01002032sub apply_transfer_encoding {
2033 my $message = shift;
2034 my $from = shift;
2035 my $to = shift;
2036
Aaron Lindsay3c88e462018-11-02 05:52:38 -04002037 return ($message, $to) if ($from eq $to and $from ne '7bit');
Paolo Bonzini8d814082014-11-25 15:00:27 +01002038
2039 require MIME::QuotedPrint;
2040 require MIME::Base64;
2041
2042 $message = MIME::QuotedPrint::decode($message)
2043 if ($from eq 'quoted-printable');
2044 $message = MIME::Base64::decode($message)
2045 if ($from eq 'base64');
2046
brian m. carlson74d76a12019-04-13 22:45:51 +00002047 $to = ($message =~ /(?:.{999,}|\r)/) ? 'quoted-printable' : '8bit'
brian m. carlson7a369872018-07-08 22:17:10 +00002048 if $to eq 'auto';
2049
Vasco Almeida46493102016-12-14 11:54:36 -01002050 die __("cannot send message as 7bit")
Paolo Bonzini8d814082014-11-25 15:00:27 +01002051 if ($to eq '7bit' and $message =~ /[^[:ascii:]]/);
brian m. carlson7a369872018-07-08 22:17:10 +00002052 return ($message, $to)
Paolo Bonzini8d814082014-11-25 15:00:27 +01002053 if ($to eq '7bit' or $to eq '8bit');
brian m. carlson7a369872018-07-08 22:17:10 +00002054 return (MIME::QuotedPrint::encode($message, "\n", 0), $to)
Paolo Bonzini8d814082014-11-25 15:00:27 +01002055 if ($to eq 'quoted-printable');
brian m. carlson7a369872018-07-08 22:17:10 +00002056 return (MIME::Base64::encode($message, "\n"), $to)
Paolo Bonzini8d814082014-11-25 15:00:27 +01002057 if ($to eq 'base64');
Vasco Almeida46493102016-12-14 11:54:36 -01002058 die __("invalid transfer encoding");
Paolo Bonzini8d814082014-11-25 15:00:27 +01002059}
2060
Ævar Arnfjörð Bjarmasonc438ea22010-09-30 13:42:59 +00002061sub unique_email_list {
Ryan Andersone2057352005-08-02 21:45:22 -04002062 my %seen;
2063 my @emails;
2064
2065 foreach my $entry (@_) {
Krzysztof Mazure4312252012-11-22 19:12:10 +01002066 my $clean = extract_valid_address_or_die($entry);
2067 $seen{$clean} ||= 0;
2068 next if $seen{$clean}++;
2069 push @emails, $entry;
Ryan Andersone2057352005-08-02 21:45:22 -04002070 }
2071 return @emails;
2072}
Jeff King747bbff2008-01-18 09:19:48 -05002073
2074sub validate_patch {
brian m. carlsonf2d06fb2018-07-08 22:17:11 +00002075 my ($fn, $xfer_encoding) = @_;
Jonathan Tan64896602017-05-12 15:38:26 -07002076
Jonathan Tan177409e2017-06-01 16:50:55 -07002077 if ($repo) {
Emily Shaffera7555302021-12-22 04:59:38 +01002078 my $hook_name = 'sendemail-validate';
Ævar Arnfjörð Bjarmason7cbc0452021-05-26 13:21:07 +02002079 my $hooks_path = $repo->command_oneline('rev-parse', '--git-path', 'hooks');
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +02002080 require File::Spec;
Emily Shaffera7555302021-12-22 04:59:38 +01002081 my $validate_hook = File::Spec->catfile($hooks_path, $hook_name);
Jonathan Tan177409e2017-06-01 16:50:55 -07002082 my $hook_error;
2083 if (-x $validate_hook) {
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +02002084 require Cwd;
2085 my $target = Cwd::abs_path($fn);
Jonathan Tan177409e2017-06-01 16:50:55 -07002086 # The hook needs a correct cwd and GIT_DIR.
Ævar Arnfjörð Bjarmason17530b22021-05-28 11:23:52 +02002087 my $cwd_save = Cwd::getcwd();
Jonathan Tan177409e2017-06-01 16:50:55 -07002088 chdir($repo->wc_path() or $repo->repo_path())
2089 or die("chdir: $!");
2090 local $ENV{"GIT_DIR"} = $repo->repo_path();
Emily Shaffera7555302021-12-22 04:59:38 +01002091 my @cmd = ("git", "hook", "run", "--ignore-missing",
2092 $hook_name, "--");
2093 my @cmd_msg = (@cmd, "<patch>");
2094 my @cmd_run = (@cmd, $target);
2095 $hook_error = system_or_msg(\@cmd_run, undef, "@cmd_msg");
Jonathan Tan177409e2017-06-01 16:50:55 -07002096 chdir($cwd_save) or die("chdir: $!");
2097 }
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +02002098 if ($hook_error) {
Jean-Noël Avilaaf15f842022-04-11 19:23:30 +00002099 $hook_error = sprintf(
2100 __("fatal: %s: rejected by %s hook\n%s\nwarning: no patches were sent\n"),
2101 $fn, $hook_name, $hook_error);
Emily Shaffera7555302021-12-22 04:59:38 +01002102 die $hook_error;
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +02002103 }
Jonathan Tan64896602017-05-12 15:38:26 -07002104 }
Jonathan Tan64896602017-05-12 15:38:26 -07002105
brian m. carlsonf2d06fb2018-07-08 22:17:11 +00002106 # Any long lines will be automatically fixed if we use a suitable transfer
2107 # encoding.
2108 unless ($xfer_encoding =~ /^(?:auto|quoted-printable|base64)$/) {
2109 open(my $fh, '<', $fn)
2110 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
2111 while (my $line = <$fh>) {
2112 if (length($line) > 998) {
Ævar Arnfjörð Bjarmasonea7811b2021-04-06 16:00:37 +02002113 die sprintf(__("fatal: %s:%d is longer than 998 characters\n" .
2114 "warning: no patches were sent\n"), $fn, $.);
brian m. carlsonf2d06fb2018-07-08 22:17:11 +00002115 }
Jeff King747bbff2008-01-18 09:19:48 -05002116 }
2117 }
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -07002118 return;
Jeff King747bbff2008-01-18 09:19:48 -05002119}
Jeff King0706bd12008-03-28 17:28:33 -04002120
Junio C Hamano531220b2016-03-17 22:40:05 -07002121sub handle_backup {
2122 my ($last, $lastlen, $file, $known_suffix) = @_;
2123 my ($suffix, $skip);
2124
2125 $skip = 0;
2126 if (defined $last &&
2127 ($lastlen < length($file)) &&
2128 (substr($file, 0, $lastlen) eq $last) &&
2129 ($suffix = substr($file, $lastlen)) !~ /^[a-z0-9]/i) {
2130 if (defined $known_suffix && $suffix eq $known_suffix) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01002131 printf(__("Skipping %s with backup suffix '%s'.\n"), $file, $known_suffix);
Junio C Hamano531220b2016-03-17 22:40:05 -07002132 $skip = 1;
2133 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01002134 # TRANSLATORS: please keep "[y|N]" as is.
2135 my $answer = ask(sprintf(__("Do you really want to send %s? [y|N]: "), $file),
Junio C Hamano531220b2016-03-17 22:40:05 -07002136 valid_re => qr/^(?:y|n)/i,
2137 default => 'n');
2138 $skip = ($answer ne 'y');
2139 if ($skip) {
2140 $known_suffix = $suffix;
2141 }
2142 }
2143 }
2144 return ($skip, $known_suffix);
2145}
2146
2147sub handle_backup_files {
2148 my @file = @_;
2149 my ($last, $lastlen, $known_suffix, $skip, @result);
2150 for my $file (@file) {
2151 ($skip, $known_suffix) = handle_backup($last, $lastlen,
2152 $file, $known_suffix);
2153 push @result, $file unless $skip;
2154 $last = $file;
2155 $lastlen = length($file);
2156 }
2157 return @result;
2158}
2159
Jeff King0706bd12008-03-28 17:28:33 -04002160sub file_has_nonascii {
2161 my $fn = shift;
2162 open(my $fh, '<', $fn)
Vasco Almeida3c5cd202016-12-14 11:54:37 -01002163 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
Jeff King0706bd12008-03-28 17:28:33 -04002164 while (my $line = <$fh>) {
2165 return 1 if $line =~ /[^[:ascii:]]/;
2166 }
2167 return 0;
2168}
Thomas Rast3cae7e52010-06-17 22:10:39 +02002169
2170sub body_or_subject_has_nonascii {
2171 my $fn = shift;
2172 open(my $fh, '<', $fn)
Vasco Almeida3c5cd202016-12-14 11:54:37 -01002173 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
Thomas Rast3cae7e52010-06-17 22:10:39 +02002174 while (my $line = <$fh>) {
2175 last if $line =~ /^$/;
2176 return 1 if $line =~ /^Subject.*[^[:ascii:]]/;
2177 }
2178 while (my $line = <$fh>) {
2179 return 1 if $line =~ /[^[:ascii:]]/;
2180 }
2181 return 0;
2182}