blob: e65d969d0bb257f5c95ba7fe955a3788d4b98a32 [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;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +010043git send-email [options] <file | directory | rev-list options >
Jacob Keller17b7a832015-11-19 14:52:11 -080044git send-email --dump-aliases
Michael Witten4ed62b02008-09-30 07:58:30 -050045
46 Composing:
47 --from <str> * Email From:
Stephen Boydf434c082010-03-07 14:46:48 -080048 --[no-]to <str> * Email To:
49 --[no-]cc <str> * Email Cc:
50 --[no-]bcc <str> * Email Bcc:
Michael Witten4ed62b02008-09-30 07:58:30 -050051 --subject <str> * Email "Subject:"
Christian Ludwigd11c9432018-03-04 00:58:14 +010052 --reply-to <str> * Email "Reply-To:"
Michael Witten4ed62b02008-09-30 07:58:30 -050053 --in-reply-to <str> * Email "In-Reply-To:"
Luis Henriquesac1596a2014-03-24 21:38:27 +000054 --[no-]xmailer * Add "X-Mailer:" header (default).
Felipe Contreras402596a2013-04-07 01:10:27 -060055 --[no-]annotate * Review each patch that will be sent in an editor.
Michael Witten4ed62b02008-09-30 07:58:30 -050056 --compose * Open an editor for introduction.
Krzysztof Mazur62e00692012-10-10 01:02:56 +020057 --compose-encoding <str> * Encoding to assume for introduction.
Thomas Rast3cae7e52010-06-17 22:10:39 +020058 --8bit-encoding <str> * Encoding to assume 8bit mails if undeclared
Paolo Bonzini8d814082014-11-25 15:00:27 +010059 --transfer-encoding <str> * Transfer encoding to use (quoted-printable, 8bit, base64)
Michael Witten4ed62b02008-09-30 07:58:30 -050060
61 Sending:
62 --envelope-sender <str> * Email envelope sender.
Gregory Anderscd5b33f2021-05-14 09:15:53 -060063 --sendmail-cmd <str> * Command to run to send email.
Michael Witten4ed62b02008-09-30 07:58:30 -050064 --smtp-server <str:int> * Outgoing SMTP server to use. The port
65 is optional. Default 'localhost'.
Pascal Obry052fbea2010-09-06 20:12:11 +020066 --smtp-server-option <str> * Outgoing SMTP server option to use.
Michael Witten4ed62b02008-09-30 07:58:30 -050067 --smtp-server-port <int> * Outgoing SMTP server port.
68 --smtp-user <str> * Username for SMTP-AUTH.
69 --smtp-pass <str> * Password for SMTP-AUTH; not necessary.
70 --smtp-encryption <str> * tls or ssl; anything else disables.
71 --smtp-ssl * Deprecated. Use '--smtp-encryption ssl'.
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -070072 --smtp-ssl-cert-path <str> * Path to ca-certificates (either directory or file).
73 Pass an empty string to disable certificate
74 verification.
Jari Aalto134550f2010-03-14 17:16:45 +020075 --smtp-domain <str> * The domain name sent to HELO/EHLO handshake
Joshua Watt8dd9b3f2018-10-22 22:24:25 -050076 --smtp-auth <str> * Space-separated list of allowed AUTH mechanisms, or
77 "none" to disable authentication.
Jan Viktorin0f2e68b2015-08-12 01:39:44 +020078 This setting forces to use one of the listed mechanisms.
Joshua Watt8dd9b3f2018-10-22 22:24:25 -050079 --no-smtp-auth Disable SMTP authentication. Shorthand for
80 `--smtp-auth=none`
Jari Aaltof60812e2010-03-14 17:16:09 +020081 --smtp-debug <0|1> * Disable, enable Net::SMTP debug.
Michael Witten4ed62b02008-09-30 07:58:30 -050082
xiaoqiang zhao5453b832017-05-21 20:59:50 +080083 --batch-size <int> * send max <int> message per connection.
84 --relogin-delay <int> * delay <int> seconds between two successive login.
85 This option can only be used with --batch-size
86
Michael Witten4ed62b02008-09-30 07:58:30 -050087 Automating:
88 --identity <str> * Use the sendemail.<id> options.
Joe Perches6e74e072010-09-24 10:03:00 -070089 --to-cmd <str> * Email To: via `<str> \$patch_path`
Michael Witten4ed62b02008-09-30 07:58:30 -050090 --cc-cmd <str> * Email Cc: via `<str> \$patch_path`
Rasmus Villemoesef0cc1d2018-10-16 09:39:23 +020091 --suppress-cc <str> * author, self, sob, cc, cccmd, body, bodycc, misc-by, all.
Michael S. Tsirkinf515c902014-04-29 08:41:16 +030092 --[no-]cc-cover * Email Cc: addresses in the cover letter.
93 --[no-]to-cover * Email To: addresses in the cover letter.
Jay Soffian3531e272009-02-14 23:32:15 -050094 --[no-]signed-off-by-cc * Send to Signed-off-by: addresses. Default on.
Michael Witten4ed62b02008-09-30 07:58:30 -050095 --[no-]suppress-from * Send to self. Default off.
Junio C Hamano41fe87f2009-08-22 12:48:48 -070096 --[no-]chain-reply-to * Chain In-Reply-To: fields. Default off.
Michael Witten4ed62b02008-09-30 07:58:30 -050097 --[no-]thread * Use In-Reply-To: field. Default on.
98
99 Administering:
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500100 --confirm <str> * Confirm recipients before sending;
101 auto, cc, compose, always, or never.
Michael Witten4ed62b02008-09-30 07:58:30 -0500102 --quiet * Output one line of info per email.
103 --dry-run * Don't actually send the emails.
104 --[no-]validate * Perform patch sanity checks. Default on.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100105 --[no-]format-patch * understand any non optional arguments as
106 `git format-patch` ones.
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200107 --force * Send even if safety checks would prevent it.
Jeff Kingc764a0c2008-01-18 09:20:10 -0500108
Jacob Keller17b7a832015-11-19 14:52:11 -0800109 Information:
110 --dump-aliases * Dump configured aliases and exit.
111
Michael Coleman1b0baf12007-02-27 22:47:54 -0600112EOT
113 exit(1);
114}
115
Duy Nguyen13374982018-11-03 07:03:18 +0100116sub completion_helper {
117 print Git::command('format-patch', '--git-completion-helper');
118 exit(0);
119}
120
Eric Wong4bc87a22006-03-25 17:20:48 -0800121# most mail servers generate the Date: header, but not all...
Jakub Narebski6bdca892006-07-07 20:57:55 +0200122sub format_2822_time {
123 my ($time) = @_;
124 my @localtm = localtime($time);
125 my @gmttm = gmtime($time);
126 my $localmin = $localtm[1] + $localtm[2] * 60;
127 my $gmtmin = $gmttm[1] + $gmttm[2] * 60;
128 if ($localtm[0] != $gmttm[0]) {
Vasco Almeida46493102016-12-14 11:54:36 -0100129 die __("local zone differs from GMT by a non-minute interval\n");
Jakub Narebski6bdca892006-07-07 20:57:55 +0200130 }
131 if ((($gmttm[6] + 1) % 7) == $localtm[6]) {
132 $localmin += 1440;
133 } elsif ((($gmttm[6] - 1) % 7) == $localtm[6]) {
134 $localmin -= 1440;
135 } elsif ($gmttm[6] != $localtm[6]) {
Vasco Almeida46493102016-12-14 11:54:36 -0100136 die __("local time offset greater than or equal to 24 hours\n");
Jakub Narebski6bdca892006-07-07 20:57:55 +0200137 }
138 my $offset = $localmin - $gmtmin;
139 my $offhour = $offset / 60;
140 my $offmin = abs($offset % 60);
141 if (abs($offhour) >= 24) {
Vasco Almeida46493102016-12-14 11:54:36 -0100142 die __("local time offset greater than or equal to 24 hours\n");
Jakub Narebski6bdca892006-07-07 20:57:55 +0200143 }
144
145 return sprintf("%s, %2d %s %d %02d:%02d:%02d %s%02d%02d",
146 qw(Sun Mon Tue Wed Thu Fri Sat)[$localtm[6]],
147 $localtm[3],
148 qw(Jan Feb Mar Apr May Jun
149 Jul Aug Sep Oct Nov Dec)[$localtm[4]],
150 $localtm[5]+1900,
151 $localtm[2],
152 $localtm[1],
153 $localtm[0],
154 ($offset >= 0) ? '+' : '-',
155 abs($offhour),
156 $offmin,
157 );
158}
Eric Wong4bc87a22006-03-25 17:20:48 -0800159
Eric Wong4bc87a22006-03-25 17:20:48 -0800160my $smtp;
Wincent Colaiuta5f5b6112007-11-21 13:35:05 +0100161my $auth;
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800162my $num_sent = 0;
Eric Wong4bc87a22006-03-25 17:20:48 -0800163
Роман Донченко11f70a72014-12-14 18:59:46 +0300164# Regexes for RFC 2047 productions.
165my $re_token = qr/[^][()<>@,;:\\"\/?.= \000-\037\177-\377]+/;
166my $re_encoded_text = qr/[^? \000-\037\177-\377]+/;
167my $re_encoded_word = qr/=\?($re_token)\?($re_token)\?($re_encoded_text)\?=/;
168
Ryan Anderson83b24432005-07-31 04:17:25 -0400169# Variables we fill in automatically, or via prompting:
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200170my (@to,@cc,@xh,$envelope_sender,
Christian Ludwigd11c9432018-03-04 00:58:14 +0100171 $initial_in_reply_to,$reply_to,$initial_subject,@files,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200172 $author,$sender,$smtp_authpass,$annotate,$compose,$time);
173# Things we either get from config, *or* are overridden on the
174# command-line.
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200175my ($no_cc, $no_to, $no_bcc, $no_identity);
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200176my (@config_to, @getopt_to);
177my (@config_cc, @getopt_cc);
178my (@config_bcc, @getopt_bcc);
Ryan Anderson78488b22005-07-31 20:04:24 -0400179
Ryan Anderson91332612005-07-31 20:04:24 -0400180# Example reply to:
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100181#$initial_in_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
Ryan Anderson83b24432005-07-31 04:17:25 -0400182
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100183my $repo = eval { Git->repository() };
184my @repo = $repo ? ($repo) : ();
Ryan Anderson83b24432005-07-31 04:17:25 -0400185
Adam Roben5483c712007-06-27 20:59:37 -0700186# Behavior modification variables
187my ($quiet, $dry_run) = (0, 0);
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100188my $format_patch;
Jay Soffianafe756c2009-02-23 13:51:37 -0500189my $compose_filename;
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200190my $force = 0;
Jacob Keller17b7a832015-11-19 14:52:11 -0800191my $dump_aliases = 0;
Adam Roben5483c712007-06-27 20:59:37 -0700192
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100193# Handle interactive edition of files.
194my $multiedit;
Michael J Gruber0ce142c2010-03-22 17:12:53 +0100195my $editor;
Jonathan Niederb4479f02009-10-30 20:42:34 -0500196
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +0200197sub system_or_msg {
198 my ($args, $msg) = @_;
199 system(@$args);
200 my $signalled = $? & 127;
201 my $exit_code = $? >> 8;
202 return unless $signalled or $exit_code;
203
Ævar Arnfjörð Bjarmason5b719b72021-05-25 01:14:24 +0200204 my @sprintf_args = ($args->[0], $exit_code);
205 if (defined $msg) {
206 # Quiet the 'redundant' warning category, except we
207 # need to support down to Perl 5.8, so we can't do a
208 # "no warnings 'redundant'", since that category was
209 # introduced in perl 5.22, and asking for it will die
210 # on older perls.
211 no warnings;
212 return sprintf($msg, @sprintf_args);
213 }
Ævar Arnfjörð Bjarmasonea7811b2021-04-06 16:00:37 +0200214 return sprintf(__("fatal: command '%s' died with exit code %d"),
Ævar Arnfjörð Bjarmason5b719b72021-05-25 01:14:24 +0200215 @sprintf_args);
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +0200216}
217
218sub system_or_die {
219 my $msg = system_or_msg(@_);
220 die $msg if $msg;
221}
222
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100223sub do_edit {
Michael J Gruber0ce142c2010-03-22 17:12:53 +0100224 if (!defined($editor)) {
225 $editor = Git::command_oneline('var', 'GIT_EDITOR');
226 }
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +0200227 my $die_msg = __("the editor exited uncleanly, aborting everything");
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100228 if (defined($multiedit) && !$multiedit) {
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +0200229 system_or_die(['sh', '-c', $editor.' "$@"', $editor, $_], $die_msg) for @_;
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100230 } else {
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +0200231 system_or_die(['sh', '-c', $editor.' "$@"', $editor, @_], $die_msg);
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100232 }
233}
Adam Roben5483c712007-06-27 20:59:37 -0700234
235# Variables with corresponding config settings
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200236my ($suppress_from, $signed_off_by_cc);
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300237my ($cover_cc, $cover_to);
Joe Perches6e74e072010-09-24 10:03:00 -0700238my ($to_cmd, $cc_cmd);
Pascal Obry052fbea2010-09-06 20:12:11 +0200239my ($smtp_server, $smtp_server_port, @smtp_server_options);
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -0700240my ($smtp_authuser, $smtp_encryption, $smtp_ssl_cert_path);
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800241my ($batch_size, $relogin_delay);
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200242my ($identity, $aliasfiletype, @alias_files, $smtp_domain, $smtp_auth);
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200243my ($confirm);
David Brown65648282007-12-25 19:56:29 -0800244my (@suppress_cc);
Thomas Rast3cae7e52010-06-17 22:10:39 +0200245my ($auto_8bit_encoding);
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200246my ($compose_encoding);
Gregory Anderscd5b33f2021-05-14 09:15:53 -0600247my ($sendmail_cmd);
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200248# Variables with corresponding config settings & hardcoded defaults
249my ($debug_net_smtp) = 0; # Net::SMTP, see send_message()
250my $thread = 1;
251my $chain_reply_to = 0;
252my $use_xmailer = 1;
253my $validate = 1;
brian m. carlsone67a2282018-07-08 22:17:12 +0000254my $target_xfer_encoding = 'auto';
Drew DeVaultdd84e522020-07-23 20:44:32 -0400255my $forbid_sendmail_variables = 1;
Adam Roben5483c712007-06-27 20:59:37 -0700256
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900257my %config_bool_settings = (
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200258 "thread" => \$thread,
259 "chainreplyto" => \$chain_reply_to,
260 "suppressfrom" => \$suppress_from,
261 "signedoffbycc" => \$signed_off_by_cc,
262 "cccover" => \$cover_cc,
263 "tocover" => \$cover_to,
264 "signedoffcc" => \$signed_off_by_cc,
265 "validate" => \$validate,
266 "multiedit" => \$multiedit,
267 "annotate" => \$annotate,
268 "xmailer" => \$use_xmailer,
Drew DeVaultdd84e522020-07-23 20:44:32 -0400269 "forbidsendmailvariables" => \$forbid_sendmail_variables,
Adam Robene46f7a02007-06-26 15:48:30 -0700270);
271
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900272my %config_settings = (
Ævar Arnfjörð Bjarmason119974e2021-05-28 11:23:43 +0200273 "smtpencryption" => \$smtp_encryption,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900274 "smtpserver" => \$smtp_server,
Junio C Hamano44b24762007-09-25 17:27:54 -0700275 "smtpserverport" => \$smtp_server_port,
Pascal Obry052fbea2010-09-06 20:12:11 +0200276 "smtpserveroption" => \@smtp_server_options,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900277 "smtpuser" => \$smtp_authuser,
278 "smtppass" => \$smtp_authpass,
Pascal Obrye1e91152010-09-06 20:12:09 +0200279 "smtpdomain" => \$smtp_domain,
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200280 "smtpauth" => \$smtp_auth,
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800281 "smtpbatchsize" => \$batch_size,
282 "smtprelogindelay" => \$relogin_delay,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200283 "to" => \@config_to,
Joe Perches6e74e072010-09-24 10:03:00 -0700284 "tocmd" => \$to_cmd,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200285 "cc" => \@config_cc,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900286 "cccmd" => \$cc_cmd,
287 "aliasfiletype" => \$aliasfiletype,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200288 "bcc" => \@config_bcc,
David Brown65648282007-12-25 19:56:29 -0800289 "suppresscc" => \@suppress_cc,
Ask Bjørn Hansen9f7820a2008-06-07 00:33:42 -0700290 "envelopesender" => \$envelope_sender,
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500291 "confirm" => \$confirm,
Trent Piepho09caa242009-05-12 15:48:56 -0700292 "from" => \$sender,
Thomas Rast3cae7e52010-06-17 22:10:39 +0200293 "assume8bitencoding" => \$auto_8bit_encoding,
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200294 "composeencoding" => \$compose_encoding,
Paolo Bonzini8d814082014-11-25 15:00:27 +0100295 "transferencoding" => \$target_xfer_encoding,
Gregory Anderscd5b33f2021-05-14 09:15:53 -0600296 "sendmailcmd" => \$sendmail_cmd,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900297);
Avi Kivity4a62d3f2007-03-11 19:19:44 +0200298
Cord Seelecec5dae2011-09-30 12:52:25 +0200299my %config_path_settings = (
300 "aliasesfile" => \@alias_files,
John Keeping6e07a3b2015-11-17 22:01:05 +0000301 "smtpsslcertpath" => \$smtp_ssl_cert_path,
Cord Seelecec5dae2011-09-30 12:52:25 +0200302);
303
Michael Witten87429972008-02-03 19:53:57 -0500304# Handle Uncouth Termination
305sub signal_handler {
Michael Witten87429972008-02-03 19:53:57 -0500306 # Make text normal
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200307 require Term::ANSIColor;
308 print Term::ANSIColor::color("reset"), "\n";
Michael Witten87429972008-02-03 19:53:57 -0500309
310 # SMTP password masked
311 system "stty echo";
312
313 # tmp files from --compose
Jay Soffianafe756c2009-02-23 13:51:37 -0500314 if (defined $compose_filename) {
315 if (-e $compose_filename) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100316 printf __("'%s' contains an intermediate version ".
317 "of the email you were composing.\n"),
318 $compose_filename;
Jay Soffianafe756c2009-02-23 13:51:37 -0500319 }
320 if (-e ($compose_filename . ".final")) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100321 printf __("'%s.final' contains the composed email.\n"),
322 $compose_filename;
Jay Soffianafe756c2009-02-23 13:51:37 -0500323 }
Michael Witten87429972008-02-03 19:53:57 -0500324 }
325
326 exit;
327};
328
329$SIG{TERM} = \&signal_handler;
330$SIG{INT} = \&signal_handler;
331
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200332# Read our sendemail.* config
333sub read_config {
Ævar Arnfjörð Bjarmason9264d292021-05-28 11:23:45 +0200334 my ($known_keys, $configured, $prefix) = @_;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200335
336 foreach my $setting (keys %config_bool_settings) {
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200337 my $target = $config_bool_settings{$setting};
Ævar Arnfjörð Bjarmason9264d292021-05-28 11:23:45 +0200338 my $key = "$prefix.$setting";
339 next unless exists $known_keys->{$key};
Ævar Arnfjörð Bjarmasonc95e3a32021-05-28 11:23:51 +0200340 my $v = (@{$known_keys->{$key}} == 1 &&
341 (defined $known_keys->{$key}->[0] &&
342 $known_keys->{$key}->[0] =~ /^(?:true|false)$/s))
343 ? $known_keys->{$key}->[0] eq 'true'
344 : Git::config_bool(@repo, $key);
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200345 next unless defined $v;
346 next if $configured->{$setting}++;
347 $$target = $v;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200348 }
349
350 foreach my $setting (keys %config_path_settings) {
351 my $target = $config_path_settings{$setting};
Ævar Arnfjörð Bjarmason9264d292021-05-28 11:23:45 +0200352 my $key = "$prefix.$setting";
353 next unless exists $known_keys->{$key};
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200354 if (ref($target) eq "ARRAY") {
Ævar Arnfjörð Bjarmason9264d292021-05-28 11:23:45 +0200355 my @values = Git::config_path(@repo, $key);
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200356 next unless @values;
357 next if $configured->{$setting}++;
358 @$target = @values;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200359 }
360 else {
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200361 my $v = Git::config_path(@repo, "$prefix.$setting");
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200362 next unless defined $v;
363 next if $configured->{$setting}++;
364 $$target = $v;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200365 }
366 }
367
368 foreach my $setting (keys %config_settings) {
369 my $target = $config_settings{$setting};
Ævar Arnfjörð Bjarmason9264d292021-05-28 11:23:45 +0200370 my $key = "$prefix.$setting";
371 next unless exists $known_keys->{$key};
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200372 if (ref($target) eq "ARRAY") {
Ævar Arnfjörð Bjarmasonc95e3a32021-05-28 11:23:51 +0200373 my @values = @{$known_keys->{$key}};
374 @values = grep { defined } @values;
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200375 next if $configured->{$setting}++;
376 @$target = @values;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200377 }
378 else {
Ævar Arnfjörð Bjarmasonc95e3a32021-05-28 11:23:51 +0200379 my $v = $known_keys->{$key}->[0];
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200380 next unless defined $v;
381 next if $configured->{$setting}++;
382 $$target = $v;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200383 }
384 }
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200385}
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200386
Ævar Arnfjörð Bjarmason2b110e92021-05-28 11:23:44 +0200387sub config_regexp {
388 my ($regex) = @_;
389 my @ret;
390 eval {
Ævar Arnfjörð Bjarmasonc95e3a32021-05-28 11:23:51 +0200391 my $ret = Git::command(
Ævar Arnfjörð Bjarmason2b110e92021-05-28 11:23:44 +0200392 'config',
Ævar Arnfjörð Bjarmasonc95e3a32021-05-28 11:23:51 +0200393 '--null',
Ævar Arnfjörð Bjarmason2b110e92021-05-28 11:23:44 +0200394 '--get-regexp',
395 $regex,
396 );
Ævar Arnfjörð Bjarmasonc95e3a32021-05-28 11:23:51 +0200397 @ret = map {
398 # We must always return ($k, $v) here, since
399 # empty config values will be just "key\0",
400 # not "key\nvalue\0".
401 my ($k, $v) = split /\n/, $_, 2;
402 ($k, $v);
403 } split /\0/, $ret;
Ævar Arnfjörð Bjarmason2b110e92021-05-28 11:23:44 +0200404 1;
405 } or do {
406 # If we have no keys we're OK, otherwise re-throw
407 die $@ if $@->value != 1;
408 };
409 return @ret;
410}
411
Ævar Arnfjörð Bjarmason9264d292021-05-28 11:23:45 +0200412# Save ourselves a lot of work of shelling out to 'git config' (it
413# parses 'bool' etc.) by only doing so for config keys that exist.
414my %known_config_keys;
415{
Ævar Arnfjörð Bjarmasonc95e3a32021-05-28 11:23:51 +0200416 my @kv = config_regexp("^sende?mail[.]");
417 while (my ($k, $v) = splice @kv, 0, 2) {
418 push @{$known_config_keys{$k}} => $v;
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200419 }
420}
421
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200422# sendemail.identity yields to --identity. We must parse this
423# special-case first before the rest of the config is read.
Ævar Arnfjörð Bjarmason9264d292021-05-28 11:23:45 +0200424{
425 my $key = "sendemail.identity";
426 $identity = Git::config(@repo, $key) if exists $known_config_keys{$key};
427}
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200428my $rc = GetOptions(
429 "identity=s" => \$identity,
430 "no-identity" => \$no_identity,
431);
432usage() unless $rc;
433undef $identity if $no_identity;
434
435# Now we know enough to read the config
436{
437 my %configured;
Ævar Arnfjörð Bjarmason9264d292021-05-28 11:23:45 +0200438 read_config(\%known_config_keys, \%configured, "sendemail.$identity") if defined $identity;
439 read_config(\%known_config_keys, \%configured, "sendemail");
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200440}
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200441
Ryan Anderson83b24432005-07-31 04:17:25 -0400442# Begin by accumulating all the variables (defined above), that we will end up
443# needing, first, from the command line:
444
Clemens Buchacherc5978242011-09-03 19:06:13 +0200445my $help;
Duy Nguyen13374982018-11-03 07:03:18 +0100446my $git_completion_helper;
Ævar Arnfjörð Bjarmason3ff15042019-05-17 21:55:44 +0200447$rc = GetOptions("h" => \$help,
448 "dump-aliases" => \$dump_aliases);
Jacob Keller17b7a832015-11-19 14:52:11 -0800449usage() unless $rc;
Vasco Almeida46493102016-12-14 11:54:36 -0100450die __("--dump-aliases incompatible with other options\n")
Jacob Keller17b7a832015-11-19 14:52:11 -0800451 if !$help and $dump_aliases and @ARGV;
452$rc = GetOptions(
Clemens Buchacherc5978242011-09-03 19:06:13 +0200453 "sender|from=s" => \$sender,
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100454 "in-reply-to=s" => \$initial_in_reply_to,
Christian Ludwigd11c9432018-03-04 00:58:14 +0100455 "reply-to=s" => \$reply_to,
Ryan Anderson83b24432005-07-31 04:17:25 -0400456 "subject=s" => \$initial_subject,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200457 "to=s" => \@getopt_to,
Joe Perches6e74e072010-09-24 10:03:00 -0700458 "to-cmd=s" => \$to_cmd,
Stephen Boydf434c082010-03-07 14:46:48 -0800459 "no-to" => \$no_to,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200460 "cc=s" => \@getopt_cc,
Stephen Boydf434c082010-03-07 14:46:48 -0800461 "no-cc" => \$no_cc,
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200462 "bcc=s" => \@getopt_bcc,
Stephen Boydf434c082010-03-07 14:46:48 -0800463 "no-bcc" => \$no_bcc,
Ryan Anderson78488b22005-07-31 20:04:24 -0400464 "chain-reply-to!" => \$chain_reply_to,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800465 "no-chain-reply-to" => sub {$chain_reply_to = 0},
Gregory Anderscd5b33f2021-05-14 09:15:53 -0600466 "sendmail-cmd=s" => \$sendmail_cmd,
Ryan Anderson3342d852005-07-31 20:04:24 -0400467 "smtp-server=s" => \$smtp_server,
Pascal Obry052fbea2010-09-06 20:12:11 +0200468 "smtp-server-option=s" => \@smtp_server_options,
Junio C Hamano44b24762007-09-25 17:27:54 -0700469 "smtp-server-port=s" => \$smtp_server_port,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900470 "smtp-user=s" => \$smtp_authuser,
Michael Witten2363d742008-02-03 19:53:56 -0500471 "smtp-pass:s" => \$smtp_authpass,
Thomas Rastf6bebd12008-06-25 21:42:43 +0200472 "smtp-ssl" => sub { $smtp_encryption = 'ssl' },
473 "smtp-encryption=s" => \$smtp_encryption,
Thomas Rast979e6522013-12-01 23:48:42 +0100474 "smtp-ssl-cert-path=s" => \$smtp_ssl_cert_path,
Jari Aaltof60812e2010-03-14 17:16:09 +0200475 "smtp-debug:i" => \$debug_net_smtp,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -0400476 "smtp-domain:s" => \$smtp_domain,
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200477 "smtp-auth=s" => \$smtp_auth,
Joshua Watt8dd9b3f2018-10-22 22:24:25 -0500478 "no-smtp-auth" => sub {$smtp_auth = 'none'},
Felipe Contreras402596a2013-04-07 01:10:27 -0600479 "annotate!" => \$annotate,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800480 "no-annotate" => sub {$annotate = 0},
Ryan Anderson1f038a02005-09-05 01:13:07 -0400481 "compose" => \$compose,
Ryan Anderson30d08b32006-02-02 11:56:06 -0500482 "quiet" => \$quiet,
Joe Perches324a8bd2007-08-17 18:51:12 -0700483 "cc-cmd=s" => \$cc_cmd,
Adam Roben5483c712007-06-27 20:59:37 -0700484 "suppress-from!" => \$suppress_from,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800485 "no-suppress-from" => sub {$suppress_from = 0},
David Brown65648282007-12-25 19:56:29 -0800486 "suppress-cc=s" => \@suppress_cc,
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500487 "signed-off-cc|signed-off-by-cc!" => \$signed_off_by_cc,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800488 "no-signed-off-cc|no-signed-off-by-cc" => sub {$signed_off_by_cc = 0},
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300489 "cc-cover|cc-cover!" => \$cover_cc,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800490 "no-cc-cover" => sub {$cover_cc = 0},
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300491 "to-cover|to-cover!" => \$cover_to,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800492 "no-to-cover" => sub {$cover_to = 0},
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500493 "confirm=s" => \$confirm,
Matthew Wilcox61302592006-10-10 08:58:23 -0600494 "dry-run" => \$dry_run,
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700495 "envelope-sender=s" => \$envelope_sender,
Adam Roben5483c712007-06-27 20:59:37 -0700496 "thread!" => \$thread,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800497 "no-thread" => sub {$thread = 0},
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500498 "validate!" => \$validate,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800499 "no-validate" => sub {$validate = 0},
Paolo Bonzini8d814082014-11-25 15:00:27 +0100500 "transfer-encoding=s" => \$target_xfer_encoding,
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100501 "format-patch!" => \$format_patch,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800502 "no-format-patch" => sub {$format_patch = 0},
Thomas Rast3cae7e52010-06-17 22:10:39 +0200503 "8bit-encoding=s" => \$auto_8bit_encoding,
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200504 "compose-encoding=s" => \$compose_encoding,
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200505 "force" => \$force,
Luis Henriquesac1596a2014-03-24 21:38:27 +0000506 "xmailer!" => \$use_xmailer,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800507 "no-xmailer" => sub {$use_xmailer = 0},
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800508 "batch-size=i" => \$batch_size,
509 "relogin-delay=i" => \$relogin_delay,
Duy Nguyen13374982018-11-03 07:03:18 +0100510 "git-completion-helper" => \$git_completion_helper,
Ryan Anderson83b24432005-07-31 04:17:25 -0400511 );
512
Ævar Arnfjörð Bjarmason3494dfd2019-05-09 13:48:30 +0200513# Munge any "either config or getopt, not both" variables
514my @initial_to = @getopt_to ? @getopt_to : ($no_to ? () : @config_to);
515my @initial_cc = @getopt_cc ? @getopt_cc : ($no_cc ? () : @config_cc);
516my @initial_bcc = @getopt_bcc ? @getopt_bcc : ($no_bcc ? () : @config_bcc);
517
Clemens Buchacherc5978242011-09-03 19:06:13 +0200518usage() if $help;
Duy Nguyen13374982018-11-03 07:03:18 +0100519completion_helper() if $git_completion_helper;
Michael Coleman1b0baf12007-02-27 22:47:54 -0600520unless ($rc) {
521 usage();
522}
523
Ævar Arnfjörð Bjarmason9264d292021-05-28 11:23:45 +0200524if ($forbid_sendmail_variables && grep { /^sendmail/s } keys %known_config_keys) {
Drew DeVaultdd84e522020-07-23 20:44:32 -0400525 die __("fatal: found configuration options for 'sendmail'\n" .
526 "git-send-email is configured with the sendemail.* options - note the 'e'.\n" .
527 "Set sendemail.forbidSendmailVariables to false to disable this check.\n");
528}
529
Vasco Almeida46493102016-12-14 11:54:36 -0100530die __("Cannot run git format-patch from outside a repository\n")
Jay Soffianeed6ca72009-02-14 23:32:13 -0500531 if $format_patch and not $repo;
532
Stefan Beller9caa7062018-02-12 11:44:04 -0800533die __("`batch-size` and `relogin` must be specified together " .
534 "(via command-line or configuration option)\n")
535 if defined $relogin_delay and not defined $batch_size;
536
Thomas Rastfa835cd2008-06-26 23:03:21 +0200537# 'default' encryption is none -- this only prevents a warning
538$smtp_encryption = '' unless (defined $smtp_encryption);
539
David Brown65648282007-12-25 19:56:29 -0800540# Set CC suppressions
541my(%suppress_cc);
542if (@suppress_cc) {
543 foreach my $entry (@suppress_cc) {
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700544 # Please update $__git_send_email_suppresscc_options
545 # in git-completion.bash when you add new options.
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100546 die sprintf(__("Unknown --suppress-cc field: '%s'\n"), $entry)
Rasmus Villemoesef0cc1d2018-10-16 09:39:23 +0200547 unless $entry =~ /^(?:all|cccmd|cc|author|self|sob|body|bodycc|misc-by)$/;
David Brown65648282007-12-25 19:56:29 -0800548 $suppress_cc{$entry} = 1;
549 }
550}
551
552if ($suppress_cc{'all'}) {
Rasmus Villemoesef0cc1d2018-10-16 09:39:23 +0200553 foreach my $entry (qw (cccmd cc author self sob body bodycc misc-by)) {
David Brown65648282007-12-25 19:56:29 -0800554 $suppress_cc{$entry} = 1;
555 }
556 delete $suppress_cc{'all'};
557}
558
559# If explicit old-style ones are specified, they trump --suppress-cc.
560$suppress_cc{'self'} = $suppress_from if defined $suppress_from;
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500561$suppress_cc{'sob'} = !$signed_off_by_cc if defined $signed_off_by_cc;
David Brown65648282007-12-25 19:56:29 -0800562
Jay Soffian3531e272009-02-14 23:32:15 -0500563if ($suppress_cc{'body'}) {
Rasmus Villemoesef0cc1d2018-10-16 09:39:23 +0200564 foreach my $entry (qw (sob bodycc misc-by)) {
Jay Soffian3531e272009-02-14 23:32:15 -0500565 $suppress_cc{$entry} = 1;
566 }
567 delete $suppress_cc{'body'};
568}
569
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500570# Set confirm's default value
571my $confirm_unconfigured = !defined $confirm;
572if ($confirm_unconfigured) {
573 $confirm = scalar %suppress_cc ? 'compose' : 'auto';
574};
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700575# Please update $__git_send_email_confirm_options in
576# git-completion.bash when you add new options.
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100577die sprintf(__("Unknown --confirm setting: '%s'\n"), $confirm)
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500578 unless $confirm =~ /^(?:auto|cc|compose|always|never)/;
579
David Brown65648282007-12-25 19:56:29 -0800580# Debugging, print out the suppressions.
581if (0) {
582 print "suppressions:\n";
583 foreach my $entry (keys %suppress_cc) {
584 printf " %-5s -> $suppress_cc{$entry}\n", $entry;
585 }
586}
587
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100588my ($repoauthor, $repocommitter);
Ævar Arnfjörð Bjarmasonfef381e2021-05-28 11:23:46 +0200589{
590 my %cache;
591 my ($author, $committer);
592 my $common = sub {
593 my ($what) = @_;
594 return $cache{$what} if exists $cache{$what};
595 ($cache{$what}) = Git::ident_person(@repo, $what);
596 return $cache{$what};
597 };
598 $repoauthor = sub { $common->('author') };
599 $repocommitter = sub { $common->('committer') };
600}
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900601
Jay Soffian50126992009-02-14 23:32:14 -0500602sub parse_address_line {
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200603 require Git::LoadCPAN::Mail::Address;
Matthieu Moybd869f62018-01-05 19:36:51 +0100604 return map { $_->format } Mail::Address->parse($_[0]);
Jay Soffian50126992009-02-14 23:32:14 -0500605}
606
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800607sub split_addrs {
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200608 require Text::ParseWords;
609 return Text::ParseWords::quotewords('\s*,\s*', 1, @_);
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800610}
611
Eric Wong994d6c62006-05-14 19:13:44 -0700612my %aliases;
Eric Sunshine09f11572015-05-31 18:29:27 -0400613
614sub parse_sendmail_alias {
615 local $_ = shift;
616 if (/"/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100617 printf STDERR __("warning: sendmail alias with quotes is not supported: %s\n"), $_;
Eric Sunshine86b89842015-06-01 14:22:36 -0400618 } elsif (/:include:/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100619 printf STDERR __("warning: `:include:` not supported: %s\n"), $_;
Eric Sunshine86b89842015-06-01 14:22:36 -0400620 } elsif (/[\/|]/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100621 printf STDERR __("warning: `/file` or `|pipe` redirection not supported: %s\n"), $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400622 } elsif (/^(\S+?)\s*:\s*(.+)$/) {
623 my ($alias, $addr) = ($1, $2);
624 $aliases{$alias} = [ split_addrs($addr) ];
625 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100626 printf STDERR __("warning: sendmail line is not recognized: %s\n"), $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400627 }
628}
629
630sub parse_sendmail_aliases {
631 my $fh = shift;
Eric Sunshine2532dd02015-05-31 18:29:29 -0400632 my $s = '';
Eric Sunshine09f11572015-05-31 18:29:27 -0400633 while (<$fh>) {
Eric Sunshine2532dd02015-05-31 18:29:29 -0400634 chomp;
Eric Sunshine020be85f2015-05-31 18:29:28 -0400635 next if /^\s*$/ || /^\s*#/;
Eric Sunshine2532dd02015-05-31 18:29:29 -0400636 $s .= $_, next if $s =~ s/\\$// || s/^\s+//;
637 parse_sendmail_alias($s) if $s;
638 $s = $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400639 }
Eric Sunshine2532dd02015-05-31 18:29:29 -0400640 $s =~ s/\\$//; # silently tolerate stray '\' on last line
641 parse_sendmail_alias($s) if $s;
Eric Sunshine09f11572015-05-31 18:29:27 -0400642}
643
Eric Wong994d6c62006-05-14 19:13:44 -0700644my %parse_alias = (
645 # multiline formats can be supported in the future
646 mutt => sub { my $fh = shift; while (<$fh>) {
Felipe Contrerasffc01f92009-09-30 17:49:36 +0300647 if (/^\s*alias\s+(?:-group\s+\S+\s+)*(\S+)\s+(.*)$/) {
Eric Wong994d6c62006-05-14 19:13:44 -0700648 my ($alias, $addr) = ($1, $2);
649 $addr =~ s/#.*$//; # mutt allows # comments
Eric Wong2c510f22016-01-04 20:53:30 +0000650 # commas delimit multiple addresses
651 my @addr = split_addrs($addr);
652
653 # quotes may be escaped in the file,
654 # unescape them so we do not double-escape them later.
655 s/\\"/"/g foreach @addr;
656 $aliases{$alias} = \@addr
Eric Wong994d6c62006-05-14 19:13:44 -0700657 }}},
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200658 mailrc => sub { my $fh = shift; while (<$fh>) {
Jeff Kinga277d1e2016-03-17 19:58:22 -0400659 if (/^alias\s+(\S+)\s+(.*?)\s*$/) {
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200660 require Text::ParseWords;
Eric Wong994d6c62006-05-14 19:13:44 -0700661 # spaces delimit multiple addresses
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200662 $aliases{$1} = [ Text::ParseWords::quotewords('\s+', 0, $2) ];
Eric Wong994d6c62006-05-14 19:13:44 -0700663 }}},
Trent Piepho73c427e2008-11-25 18:55:00 -0800664 pine => sub { my $fh = shift; my $f='\t[^\t]*';
665 for (my $x = ''; defined($x); $x = $_) {
666 chomp $x;
667 $x .= $1 while(defined($_ = <$fh>) && /^ +(.*)$/);
668 $x =~ /^(\S+)$f\t\(?([^\t]+?)\)?(:?$f){0,2}$/ or next;
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800669 $aliases{$1} = [ split_addrs($2) ];
Trent Piepho73c427e2008-11-25 18:55:00 -0800670 }},
Bill Pemberton7613ea32009-04-22 09:41:29 -0400671 elm => sub { my $fh = shift;
672 while (<$fh>) {
673 if (/^(\S+)\s+=\s+[^=]+=\s(\S+)/) {
674 my ($alias, $addr) = ($1, $2);
675 $aliases{$alias} = [ split_addrs($addr) ];
676 }
677 } },
Eric Sunshine09f11572015-05-31 18:29:27 -0400678 sendmail => \&parse_sendmail_aliases,
Eric Wong994d6c62006-05-14 19:13:44 -0700679 gnus => sub { my $fh = shift; while (<$fh>) {
680 if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
681 $aliases{$1} = [ $2 ];
682 }}}
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700683 # Please update _git_config() in git-completion.bash when you
684 # add new MUAs.
Eric Wong994d6c62006-05-14 19:13:44 -0700685);
686
Petr Baudis3cb8caf2006-07-03 22:47:58 +0200687if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) {
Eric Wong994d6c62006-05-14 19:13:44 -0700688 foreach my $file (@alias_files) {
689 open my $fh, '<', $file or die "opening $file: $!\n";
690 $parse_alias{$aliasfiletype}->($fh);
691 close $fh;
692 }
693}
694
Jacob Keller17b7a832015-11-19 14:52:11 -0800695if ($dump_aliases) {
696 print "$_\n" for (sort keys %aliases);
697 exit(0);
698}
699
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700700# is_format_patch_arg($f) returns 0 if $f names a patch, or 1 if
701# $f is a revision list specification to be passed to format-patch.
702sub is_format_patch_arg {
Jay Soffianeed6ca72009-02-14 23:32:13 -0500703 return unless $repo;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100704 my $f = shift;
705 try {
706 $repo->command('rev-parse', '--verify', '--quiet', $f);
707 if (defined($format_patch)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100708 return $format_patch;
709 }
Ævar Arnfjörð Bjarmason4adbf382021-05-28 11:23:47 +0200710 die sprintf(__(<<EOF), $f, $f);
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100711File '%s' exists but it could also be the range of commits
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100712to produce patches for. Please disambiguate by...
713
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100714 * Saying "./%s" if you mean a file; or
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100715 * Giving --format-patch option if you mean a range.
716EOF
717 } catch Git::Error::Command with {
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700718 # Not a valid revision. Treat it as a filename.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100719 return 0;
720 }
721}
722
Jeff Kingaa548922008-01-18 09:19:36 -0500723# Now that all the defaults are set, process the rest of the command line
724# arguments and collect up the files that need to be processed.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100725my @rev_list_opts;
Junio C Hamano69f4ce52008-11-30 22:38:20 -0800726while (defined(my $f = shift @ARGV)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100727 if ($f eq "--") {
728 push @rev_list_opts, "--", @ARGV;
729 @ARGV = ();
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700730 } elsif (-d $f and !is_format_patch_arg($f)) {
Ævar Arnfjörð Bjarmasonc6038162010-09-30 13:42:54 +0000731 opendir my $dh, $f
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100732 or die sprintf(__("Failed to opendir %s: %s"), $f, $!);
Jeff Kingaa548922008-01-18 09:19:36 -0500733
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200734 require File::Spec;
735 push @files, grep { -f $_ } map { File::Spec->catfile($f, $_) }
Ævar Arnfjörð Bjarmasonc6038162010-09-30 13:42:54 +0000736 sort readdir $dh;
737 closedir $dh;
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700738 } elsif ((-f $f or -p $f) and !is_format_patch_arg($f)) {
Jeff Kingaa548922008-01-18 09:19:36 -0500739 push @files, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500740 } else {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100741 push @rev_list_opts, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500742 }
743}
744
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100745if (@rev_list_opts) {
Vasco Almeida46493102016-12-14 11:54:36 -0100746 die __("Cannot run git format-patch from outside a repository\n")
Jay Soffianeed6ca72009-02-14 23:32:13 -0500747 unless $repo;
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200748 require File::Temp;
749 push @files, $repo->command('format-patch', '-o', File::Temp::tempdir(CLEANUP => 1), @rev_list_opts);
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100750}
751
Junio C Hamano531220b2016-03-17 22:40:05 -0700752@files = handle_backup_files(@files);
753
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500754if ($validate) {
Jeff Kingc764a0c2008-01-18 09:20:10 -0500755 foreach my $f (@files) {
Kevin Ballard300913b2008-06-25 15:44:40 -0700756 unless (-p $f) {
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +0200757 validate_patch($f, $target_xfer_encoding);
Kevin Ballard300913b2008-06-25 15:44:40 -0700758 }
Jeff Kingc764a0c2008-01-18 09:20:10 -0500759 }
Jeff King747bbff2008-01-18 09:19:48 -0500760}
761
Jeff Kingaa548922008-01-18 09:19:36 -0500762if (@files) {
763 unless ($quiet) {
764 print $_,"\n" for (@files);
765 }
766} else {
Vasco Almeida46493102016-12-14 11:54:36 -0100767 print STDERR __("\nNo patch files specified!\n\n");
Jeff Kingaa548922008-01-18 09:19:36 -0500768 usage();
769}
770
Ævar Arnfjörð Bjarmasonacf071b2010-09-30 13:42:57 +0000771sub get_patch_subject {
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100772 my $fn = shift;
773 open (my $fh, '<', $fn);
774 while (my $line = <$fh>) {
775 next unless ($line =~ /^Subject: (.*)$/);
776 close $fh;
777 return "GIT: $1\n";
778 }
779 close $fh;
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100780 die sprintf(__("No subject line in %s?"), $fn);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100781}
782
783if ($compose) {
784 # Note that this does not need to be secure, but we will make a small
785 # effort to have it be unique
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200786 require File::Temp;
Jay Soffianafe756c2009-02-23 13:51:37 -0500787 $compose_filename = ($repo ?
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200788 File::Temp::tempfile(".gitsendemail.msg.XXXXXX", DIR => $repo->repo_path()) :
789 File::Temp::tempfile(".gitsendemail.msg.XXXXXX", DIR => "."))[1];
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000790 open my $c, ">", $compose_filename
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100791 or die sprintf(__("Failed to open for writing %s: %s"), $compose_filename, $!);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100792
793
Ævar Arnfjörð Bjarmasonfef381e2021-05-28 11:23:46 +0200794 my $tpl_sender = $sender || $repoauthor->() || $repocommitter->() || '';
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100795 my $tpl_subject = $initial_subject || '';
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100796 my $tpl_in_reply_to = $initial_in_reply_to || '';
Christian Ludwigd11c9432018-03-04 00:58:14 +0100797 my $tpl_reply_to = $reply_to || '';
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100798
Ævar Arnfjörð Bjarmason4adbf382021-05-28 11:23:47 +0200799 print $c <<EOT1, Git::prefix_lines("GIT: ", __(<<EOT2)), <<EOT3;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100800From $tpl_sender # This line is ignored.
Vasco Almeida70aedfb2016-12-14 11:54:38 -0100801EOT1
802Lines beginning in "GIT:" will be removed.
803Consider including an overall diffstat or table of contents
804for the patch you are writing.
805
806Clear the body content if you don't wish to send a summary.
807EOT2
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100808From: $tpl_sender
Christian Ludwigd11c9432018-03-04 00:58:14 +0100809Reply-To: $tpl_reply_to
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100810Subject: $tpl_subject
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100811In-Reply-To: $tpl_in_reply_to
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100812
Vasco Almeida70aedfb2016-12-14 11:54:38 -0100813EOT3
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100814 for my $f (@files) {
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000815 print $c get_patch_subject($f);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100816 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000817 close $c;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100818
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100819 if ($annotate) {
820 do_edit($compose_filename, @files);
821 } else {
822 do_edit($compose_filename);
823 }
824
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000825 open $c, "<", $compose_filename
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100826 or die sprintf(__("Failed to open %s: %s"), $compose_filename, $!);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100827
Krzysztof Mazur4a47a4d2012-10-22 14:41:48 +0200828 if (!defined $compose_encoding) {
829 $compose_encoding = "UTF-8";
830 }
Nathan Payreb6049542017-12-15 16:33:39 +0100831
832 my %parsed_email;
833 while (my $line = <$c>) {
834 next if $line =~ m/^GIT:/;
835 parse_header_line($line, \%parsed_email);
836 if ($line =~ /^$/) {
837 $parsed_email{'body'} = filter_body($c);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100838 }
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100839 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000840 close $c;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100841
Nathan Payreb6049542017-12-15 16:33:39 +0100842 open my $c2, ">", $compose_filename . ".final"
843 or die sprintf(__("Failed to open %s.final: %s"), $compose_filename, $!);
844
845
846 if ($parsed_email{'From'}) {
847 $sender = delete($parsed_email{'From'});
848 }
849 if ($parsed_email{'In-Reply-To'}) {
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100850 $initial_in_reply_to = delete($parsed_email{'In-Reply-To'});
Nathan Payreb6049542017-12-15 16:33:39 +0100851 }
Christian Ludwigd11c9432018-03-04 00:58:14 +0100852 if ($parsed_email{'Reply-To'}) {
853 $reply_to = delete($parsed_email{'Reply-To'});
Nathan Payreb6049542017-12-15 16:33:39 +0100854 }
855 if ($parsed_email{'Subject'}) {
856 $initial_subject = delete($parsed_email{'Subject'});
857 print $c2 "Subject: " .
858 quote_subject($initial_subject, $compose_encoding) .
859 "\n";
860 }
861
862 if ($parsed_email{'MIME-Version'}) {
863 print $c2 "MIME-Version: $parsed_email{'MIME-Version'}\n",
864 "Content-Type: $parsed_email{'Content-Type'};\n",
865 "Content-Transfer-Encoding: $parsed_email{'Content-Transfer-Encoding'}\n";
866 delete($parsed_email{'MIME-Version'});
867 delete($parsed_email{'Content-Type'});
868 delete($parsed_email{'Content-Transfer-Encoding'});
869 } elsif (file_has_nonascii($compose_filename)) {
870 my $content_type = (delete($parsed_email{'Content-Type'}) or
871 "text/plain; charset=$compose_encoding");
872 print $c2 "MIME-Version: 1.0\n",
873 "Content-Type: $content_type\n",
874 "Content-Transfer-Encoding: 8bit\n";
875 }
876 # Preserve unknown headers
877 foreach my $key (keys %parsed_email) {
878 next if $key eq 'body';
879 print $c2 "$key: $parsed_email{$key}";
880 }
881
882 if ($parsed_email{'body'}) {
883 print $c2 "\n$parsed_email{'body'}\n";
884 delete($parsed_email{'body'});
885 } else {
Vasco Almeida46493102016-12-14 11:54:36 -0100886 print __("Summary email is empty, skipping it\n");
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100887 $compose = -1;
888 }
Nathan Payreb6049542017-12-15 16:33:39 +0100889
890 close $c2;
891
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100892} elsif ($annotate) {
893 do_edit(@files);
894}
895
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200896sub term {
897 my $term = eval {
898 require Term::ReadLine;
899 $ENV{"GIT_SEND_EMAIL_NOTTY"}
900 ? Term::ReadLine->new('git-send-email', \*STDIN, \*STDOUT)
901 : Term::ReadLine->new('git-send-email');
902 };
903 if ($@) {
904 $term = FakeTerm->new("$@: going non-interactive");
905 }
906 return $term;
907}
908
Jay Soffian6e182512009-03-28 21:39:10 -0400909sub ask {
910 my ($prompt, %arg) = @_;
Jay Soffian0da43a62009-04-04 23:23:21 -0400911 my $valid_re = $arg{valid_re};
Jay Soffian6e182512009-03-28 21:39:10 -0400912 my $default = $arg{default};
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700913 my $confirm_only = $arg{confirm_only};
Jay Soffian6e182512009-03-28 21:39:10 -0400914 my $resp;
915 my $i = 0;
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +0200916 my $term = term();
Jay Soffian5906f542009-03-31 12:22:11 -0400917 return defined $default ? $default : undef
918 unless defined $term->IN and defined fileno($term->IN) and
919 defined $term->OUT and defined fileno($term->OUT);
Jay Soffian6e182512009-03-28 21:39:10 -0400920 while ($i++ < 10) {
921 $resp = $term->readline($prompt);
922 if (!defined $resp) { # EOF
923 print "\n";
924 return defined $default ? $default : undef;
925 }
926 if ($resp eq '' and defined $default) {
927 return $default;
928 }
Jay Soffian0da43a62009-04-04 23:23:21 -0400929 if (!defined $valid_re or $resp =~ /$valid_re/) {
Jay Soffian6e182512009-03-28 21:39:10 -0400930 return $resp;
931 }
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700932 if ($confirm_only) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100933 my $yesno = $term->readline(
934 # TRANSLATORS: please keep [y/N] as is.
935 sprintf(__("Are you sure you want to use <%s> [y/N]? "), $resp));
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700936 if (defined $yesno && $yesno =~ /y/i) {
937 return $resp;
938 }
939 }
Jay Soffian6e182512009-03-28 21:39:10 -0400940 }
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -0700941 return;
Jay Soffian6e182512009-03-28 21:39:10 -0400942}
943
Nathan Payreb6049542017-12-15 16:33:39 +0100944sub parse_header_line {
945 my $lines = shift;
946 my $parsed_line = shift;
947 my $addr_pat = join "|", qw(To Cc Bcc);
948
949 foreach (split(/\n/, $lines)) {
950 if (/^($addr_pat):\s*(.+)$/i) {
951 $parsed_line->{$1} = [ parse_address_line($2) ];
952 } elsif (/^([^:]*):\s*(.+)\s*$/i) {
953 $parsed_line->{$1} = $2;
954 }
955 }
956}
957
958sub filter_body {
959 my $c = shift;
960 my $body = "";
961 while (my $body_line = <$c>) {
962 if ($body_line !~ m/^GIT:/) {
963 $body .= $body_line;
964 }
965 }
966 return $body;
967}
968
969
Thomas Rast3cae7e52010-06-17 22:10:39 +0200970my %broken_encoding;
971
Ævar Arnfjörð Bjarmason1d50bfd2010-09-30 13:42:58 +0000972sub file_declares_8bit_cte {
Thomas Rast3cae7e52010-06-17 22:10:39 +0200973 my $fn = shift;
974 open (my $fh, '<', $fn);
975 while (my $line = <$fh>) {
976 last if ($line =~ /^$/);
977 return 1 if ($line =~ /^Content-Transfer-Encoding: .*8bit.*$/);
978 }
979 close $fh;
980 return 0;
981}
982
983foreach my $f (@files) {
984 next unless (body_or_subject_has_nonascii($f)
985 && !file_declares_8bit_cte($f));
986 $broken_encoding{$f} = 1;
987}
988
989if (!defined $auto_8bit_encoding && scalar %broken_encoding) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100990 print __("The following files are 8bit, but do not declare " .
991 "a Content-Transfer-Encoding.\n");
Thomas Rast3cae7e52010-06-17 22:10:39 +0200992 foreach my $f (sort keys %broken_encoding) {
993 print " $f\n";
994 }
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100995 $auto_8bit_encoding = ask(__("Which 8bit encoding should I declare [UTF-8]? "),
Junio C Hamano852a15d2015-02-13 12:20:25 -0800996 valid_re => qr/.{4}/, confirm_only => 1,
Thomas Rast3cae7e52010-06-17 22:10:39 +0200997 default => "UTF-8");
998}
999
Thomas Rasta03bc5b2009-06-08 23:34:12 +02001000if (!$force) {
1001 for my $f (@files) {
Ævar Arnfjörð Bjarmason0d290a42010-09-30 13:43:01 +00001002 if (get_patch_subject($f) =~ /\Q*** SUBJECT HERE ***\E/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001003 die sprintf(__("Refusing to send because the patch\n\t%s\n"
Thomas Rasta03bc5b2009-06-08 23:34:12 +02001004 . "has the template subject '*** SUBJECT HERE ***'. "
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001005 . "Pass --force if you really want to send.\n"), $f);
Thomas Rasta03bc5b2009-06-08 23:34:12 +02001006 }
1007 }
1008}
1009
Remi Lespinetc46e27a2015-06-30 14:16:47 +02001010if (defined $sender) {
Remi Lespinetfa5b1aa2015-06-30 14:16:51 +02001011 $sender =~ s/^\s+|\s+$//g;
Remi Lespinetc46e27a2015-06-30 14:16:47 +02001012 ($sender) = expand_aliases($sender);
1013} else {
Ævar Arnfjörð Bjarmasonfef381e2021-05-28 11:23:46 +02001014 $sender = $repoauthor->() || $repocommitter->() || '';
Ryan Anderson83b24432005-07-31 04:17:25 -04001015}
1016
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001017# $sender could be an already sanitized address
1018# (e.g. sendemail.from could be manually sanitized by user).
1019# But it's a no-op to run sanitize_address on an already sanitized address.
1020$sender = sanitize_address($sender);
1021
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001022my $to_whom = __("To whom should the emails be sent (if anyone)?");
Felipe Contreras8cac13d2012-11-24 12:16:19 +01001023my $prompting = 0;
Junio C Hamano8796ff72010-10-26 22:02:03 -07001024if (!@initial_to && !defined $to_cmd) {
Junio C Hamano0d6b21e2016-04-24 12:31:44 -07001025 my $to = ask("$to_whom ",
Stephen Boyd61837492012-09-06 11:31:11 -07001026 default => "",
Junio C Hamano51bbccf2012-08-14 15:15:53 -07001027 valid_re => qr/\@.*\./, confirm_only => 1);
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001028 push @initial_to, parse_address_line($to) if defined $to; # sanitized/validated later
Ryan Anderson1f038a02005-09-05 01:13:07 -04001029 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -04001030}
1031
Eric Wong994d6c62006-05-14 19:13:44 -07001032sub expand_aliases {
Jeff King302e04e2009-07-23 07:09:29 -04001033 return map { expand_one_alias($_) } @_;
1034}
1035
1036my %EXPANDED_ALIASES;
1037sub expand_one_alias {
1038 my $alias = shift;
1039 if ($EXPANDED_ALIASES{$alias}) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001040 die sprintf(__("fatal: alias '%s' expands to itself\n"), $alias);
Jeff King302e04e2009-07-23 07:09:29 -04001041 }
1042 local $EXPANDED_ALIASES{$alias} = 1;
1043 return $aliases{$alias} ? expand_aliases(@{$aliases{$alias}}) : $alias;
Eric Wong994d6c62006-05-14 19:13:44 -07001044}
1045
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001046@initial_to = process_address_list(@initial_to);
1047@initial_cc = process_address_list(@initial_cc);
Ævar Arnfjörð Bjarmasone60f6d12019-05-09 13:48:29 +02001048@initial_bcc = process_address_list(@initial_bcc);
Eric Wong994d6c62006-05-14 19:13:44 -07001049
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001050if ($thread && !defined $initial_in_reply_to && $prompting) {
1051 $initial_in_reply_to = ask(
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001052 __("Message-ID to be used as In-Reply-To for the first email (if any)? "),
Stephen Boyd61837492012-09-06 11:31:11 -07001053 default => "",
Junio C Hamano51bbccf2012-08-14 15:15:53 -07001054 valid_re => qr/\@.*\./, confirm_only => 1);
Ryan Anderson83b24432005-07-31 04:17:25 -04001055}
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001056if (defined $initial_in_reply_to) {
1057 $initial_in_reply_to =~ s/^\s*<?//;
1058 $initial_in_reply_to =~ s/>?\s*$//;
1059 $initial_in_reply_to = "<$initial_in_reply_to>" if $initial_in_reply_to ne '';
Junio C Hamanoace9c2a2007-12-10 21:44:42 -08001060}
Mike Hommeyace72082007-12-09 18:17:28 +01001061
Christian Ludwigd11c9432018-03-04 00:58:14 +01001062if (defined $reply_to) {
1063 $reply_to =~ s/^\s+|\s+$//g;
1064 ($reply_to) = expand_aliases($reply_to);
1065 $reply_to = sanitize_address($reply_to);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001066}
Eric Wongaca7ad72006-05-15 02:34:44 -07001067
Gregory Anderscd5b33f2021-05-14 09:15:53 -06001068if (!defined $sendmail_cmd && !defined $smtp_server) {
Florian Klink1ab2fd42017-11-28 01:49:04 +01001069 my @sendmail_paths = qw( /usr/sbin/sendmail /usr/lib/sendmail );
1070 push @sendmail_paths, map {"$_/sendmail"} split /:/, $ENV{PATH};
1071 foreach (@sendmail_paths) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001072 if (-x $_) {
Gregory Anderscd5b33f2021-05-14 09:15:53 -06001073 $sendmail_cmd = $_;
Eric Wongaca7ad72006-05-15 02:34:44 -07001074 last;
1075 }
1076 }
Gregory Anderscd5b33f2021-05-14 09:15:53 -06001077
1078 if (!defined $sendmail_cmd) {
1079 $smtp_server = 'localhost'; # could be 127.0.0.1, too... *shrug*
1080 }
Ryan Anderson3342d852005-07-31 20:04:24 -04001081}
1082
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001083if ($compose && $compose > 0) {
1084 @files = ($compose_filename . ".final", @files);
Ryan Anderson1f038a02005-09-05 01:13:07 -04001085}
1086
Ryan Anderson83b24432005-07-31 04:17:25 -04001087# Variables we set as part of the loop over files
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001088our ($message_id, %mail, $subject, $in_reply_to, $references, $message,
Jay Soffiandc1460a2009-03-31 12:22:12 -04001089 $needs_confirm, $message_num, $ask_default);
Ryan Anderson83b24432005-07-31 04:17:25 -04001090
Eric Wong567ffeb2006-03-25 16:47:12 -08001091sub extract_valid_address {
1092 my $address = shift;
Ævar Arnfjörð Bjarmason35b6ab92010-09-30 19:03:31 +00001093 my $local_part_regexp = qr/[^<>"\s@]+/;
1094 my $domain_regexp = qr/[^.<>"\s@]+(?:\.[^.<>"\s@]+)+/;
Eric Wongdb3106b2006-05-15 02:41:01 -07001095
1096 # check for a local address:
Junio C Hamanoad9c18f2006-06-06 00:05:56 -07001097 return $address if ($address =~ /^($local_part_regexp)$/);
Eric Wongdb3106b2006-05-15 02:41:01 -07001098
Uwe Kleine-König155197e2007-08-09 15:27:57 +02001099 $address =~ s/^\s*<(.*)>\s*$/$1/;
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +02001100 my $have_email_valid = eval { require Email::Valid; 1 };
Eric Wong567ffeb2006-03-25 16:47:12 -08001101 if ($have_email_valid) {
Junio C Hamanoad9c18f2006-06-06 00:05:56 -07001102 return scalar Email::Valid->address($address);
Eric Wong567ffeb2006-03-25 16:47:12 -08001103 }
Krzysztof Mazur95c0d4b2012-11-22 19:12:09 +01001104
1105 # less robust/correct than the monster regexp in Email::Valid,
1106 # but still does a 99% job, and one less dependency
1107 return $1 if $address =~ /($local_part_regexp\@$domain_regexp)/;
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -07001108 return;
Eric Wong567ffeb2006-03-25 16:47:12 -08001109}
Ryan Anderson83b24432005-07-31 04:17:25 -04001110
Krzysztof Mazure4312252012-11-22 19:12:10 +01001111sub extract_valid_address_or_die {
1112 my $address = shift;
1113 $address = extract_valid_address($address);
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001114 die sprintf(__("error: unable to extract a valid address from: %s\n"), $address)
Krzysztof Mazure4312252012-11-22 19:12:10 +01001115 if !$address;
1116 return $address;
1117}
1118
1119sub validate_address {
1120 my $address = shift;
Krzysztof Mazurd0e98102012-11-22 19:12:12 +01001121 while (!extract_valid_address($address)) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001122 printf STDERR __("error: unable to extract a valid address from: %s\n"), $address;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001123 # TRANSLATORS: Make sure to include [q] [d] [e] in your
1124 # translation. The program will only accept English input
1125 # at this point.
1126 $_ = ask(__("What to do with this address? ([q]uit|[d]rop|[e]dit): "),
Krzysztof Mazurd0e98102012-11-22 19:12:12 +01001127 valid_re => qr/^(?:quit|q|drop|d|edit|e)/i,
Krzysztof Mazur5c80afe2012-11-22 19:12:11 +01001128 default => 'q');
1129 if (/^d/i) {
1130 return undef;
1131 } elsif (/^q/i) {
1132 cleanup_compose_files();
1133 exit(0);
1134 }
Junio C Hamano0d6b21e2016-04-24 12:31:44 -07001135 $address = ask("$to_whom ",
Krzysztof Mazurd0e98102012-11-22 19:12:12 +01001136 default => "",
1137 valid_re => qr/\@.*\./, confirm_only => 1);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001138 }
1139 return $address;
1140}
1141
1142sub validate_address_list {
1143 return (grep { defined $_ }
1144 map { validate_address($_) } @_);
Ryan Anderson83b24432005-07-31 04:17:25 -04001145}
1146
1147# Usually don't need to change anything below here.
1148
1149# we make a "fake" message id by taking the current number
1150# of seconds since the beginning of Unix time and tacking on
1151# a random number to the end, in case we are called quicker than
1152# 1 second since the last time we were called.
Ryan Anderson8037d1a2005-07-31 20:04:24 -04001153
1154# We'll setup a template for the message id, using the "from" address:
Ryan Anderson8037d1a2005-07-31 20:04:24 -04001155
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001156my ($message_id_stamp, $message_id_serial);
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001157sub make_message_id {
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001158 my $uniq;
1159 if (!defined $message_id_stamp) {
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +02001160 require POSIX;
1161 $message_id_stamp = POSIX::strftime("%Y%m%d%H%M%S.$$", gmtime(time));
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001162 $message_id_serial = 0;
1163 }
1164 $message_id_serial++;
1165 $uniq = "$message_id_stamp-$message_id_serial";
1166
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001167 my $du_part;
Ævar Arnfjörð Bjarmasonfef381e2021-05-28 11:23:46 +02001168 for ($sender, $repocommitter->(), $repoauthor->()) {
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001169 $du_part = extract_valid_address(sanitize_address($_));
1170 last if (defined $du_part and $du_part ne '');
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001171 }
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001172 if (not defined $du_part or $du_part eq '') {
Ævar Arnfjörð Bjarmason529dd382010-09-30 13:43:08 +00001173 require Sys::Hostname;
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001174 $du_part = 'user@' . Sys::Hostname::hostname();
1175 }
Eric Wongf916ab02016-04-06 20:07:14 +00001176 my $message_id_template = "<%s-%s>";
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001177 $message_id = sprintf($message_id_template, $uniq, $du_part);
Ryan Anderson8037d1a2005-07-31 20:04:24 -04001178 #print "new message id = $message_id\n"; # Was useful for debugging
Ryan Anderson83b24432005-07-31 04:17:25 -04001179}
1180
1181
1182
Eric Wonga5370b12006-03-25 03:01:01 -08001183$time = time - scalar $#files;
Ryan Anderson83b24432005-07-31 04:17:25 -04001184
Jürgen Rühle374c5902007-01-10 13:36:39 -08001185sub unquote_rfc2047 {
1186 local ($_) = @_;
Роман Донченко11f70a72014-12-14 18:59:46 +03001187 my $charset;
Роман Донченкоab47e2a2014-12-14 18:59:47 +03001188 my $sep = qr/[ \t]+/;
1189 s{$re_encoded_word(?:$sep$re_encoded_word)*}{
1190 my @words = split $sep, $&;
1191 foreach (@words) {
1192 m/$re_encoded_word/;
1193 $charset = $1;
1194 my $encoding = $2;
1195 my $text = $3;
1196 if ($encoding eq 'q' || $encoding eq 'Q') {
1197 $_ = $text;
1198 s/_/ /g;
1199 s/=([0-9A-F]{2})/chr(hex($1))/egi;
1200 } else {
1201 # other encodings not supported yet
1202 }
Роман Донченко11f70a72014-12-14 18:59:46 +03001203 }
Роман Донченкоab47e2a2014-12-14 18:59:47 +03001204 join '', @words;
Thomas Rastb622d4d2012-07-30 21:25:40 +02001205 }eg;
Роман Донченко11f70a72014-12-14 18:59:46 +03001206 return wantarray ? ($_, $charset) : $_;
Jürgen Rühle374c5902007-01-10 13:36:39 -08001207}
1208
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001209sub quote_rfc2047 {
1210 local $_ = shift;
Brandon Caseyd1fff6f2009-06-06 20:12:31 -05001211 my $encoding = shift || 'UTF-8';
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001212 s/([^-a-zA-Z0-9!*+\/])/sprintf("=%02X", ord($1))/eg;
1213 s/(.*)/=\?$encoding\?q\?$1\?=/;
1214 return $_;
1215}
1216
Brandon Caseya3a82622009-06-07 19:25:58 -05001217sub is_rfc2047_quoted {
1218 my $s = shift;
Brandon Caseya3a82622009-06-07 19:25:58 -05001219 length($s) <= 75 &&
Роман Донченко11f70a72014-12-14 18:59:46 +03001220 $s =~ m/^(?:"[[:ascii:]]*"|$re_encoded_word)$/o;
Brandon Caseya3a82622009-06-07 19:25:58 -05001221}
1222
Krzysztof Mazurce547802012-10-24 23:08:26 +02001223sub subject_needs_rfc2047_quoting {
1224 my $s = shift;
1225
Krzysztof Mazurce1459f2012-10-24 23:28:29 +02001226 return ($s =~ /[^[:ascii:]]/) || ($s =~ /=\?/);
Krzysztof Mazurce547802012-10-24 23:08:26 +02001227}
1228
1229sub quote_subject {
1230 local $subject = shift;
1231 my $encoding = shift || 'UTF-8';
1232
1233 if (subject_needs_rfc2047_quoting($subject)) {
1234 return quote_rfc2047($subject, $encoding);
1235 }
1236 return $subject;
1237}
1238
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001239# use the simplest quoting being able to handle the recipient
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001240sub sanitize_address {
Robin H. Johnson732263d2007-04-25 19:37:19 -07001241 my ($recipient) = @_;
Krzysztof Mazur831a4882012-11-22 19:12:08 +01001242
1243 # remove garbage after email address
1244 $recipient =~ s/(.*>).*$/$1/;
1245
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001246 my ($recipient_name, $recipient_addr) = ($recipient =~ /^(.*?)\s*(<.*)/);
1247
1248 if (not $recipient_name) {
Ævar Arnfjörð Bjarmasonff483892010-09-30 13:43:02 +00001249 return $recipient;
Robin H. Johnson732263d2007-04-25 19:37:19 -07001250 }
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001251
1252 # if recipient_name is already quoted, do nothing
Brandon Caseya3a82622009-06-07 19:25:58 -05001253 if (is_rfc2047_quoted($recipient_name)) {
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001254 return $recipient;
1255 }
1256
Remi Lespinet1fe97032015-06-30 14:16:49 +02001257 # remove non-escaped quotes
1258 $recipient_name =~ s/(^|[^\\])"/$1/g;
1259
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001260 # rfc2047 is needed if a non-ascii char is included
1261 if ($recipient_name =~ /[^[:ascii:]]/) {
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001262 $recipient_name = quote_rfc2047($recipient_name);
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001263 }
1264
1265 # double quotes are needed if specials or CTLs are included
1266 elsif ($recipient_name =~ /[][()<>@,;:\\".\000-\037\177]/) {
Remi Lespinet1fe97032015-06-30 14:16:49 +02001267 $recipient_name =~ s/([\\\r])/\\$1/g;
Ævar Arnfjörð Bjarmasond5c7d692010-09-30 13:43:03 +00001268 $recipient_name = qq["$recipient_name"];
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001269 }
1270
1271 return "$recipient_name $recipient_addr";
1272
Robin H. Johnson732263d2007-04-25 19:37:19 -07001273}
1274
Matthieu Moycb2922f2017-08-23 12:21:01 +02001275sub strip_garbage_one_address {
1276 my ($addr) = @_;
1277 chomp $addr;
1278 if ($addr =~ /^(("[^"]*"|[^"<]*)? *<[^>]*>).*/) {
1279 # "Foo Bar" <foobar@example.com> [possibly garbage here]
1280 # Foo Bar <foobar@example.com> [possibly garbage here]
1281 return $1;
1282 }
1283 if ($addr =~ /^(<[^>]*>).*/) {
1284 # <foo@example.com> [possibly garbage here]
1285 # if garbage contains other addresses, they are ignored.
1286 return $1;
1287 }
1288 if ($addr =~ /^([^"#,\s]*)/) {
1289 # address without quoting: remove anything after the address
1290 return $1;
1291 }
1292 return $addr;
1293}
1294
Krzysztof Mazure4312252012-11-22 19:12:10 +01001295sub sanitize_address_list {
1296 return (map { sanitize_address($_) } @_);
1297}
1298
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001299sub process_address_list {
Remi Lespinetb1c8a112015-06-30 14:16:50 +02001300 my @addr_list = map { parse_address_line($_) } @_;
1301 @addr_list = expand_aliases(@addr_list);
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001302 @addr_list = sanitize_address_list(@addr_list);
1303 @addr_list = validate_address_list(@addr_list);
1304 return @addr_list;
1305}
1306
Jari Aalto134550f2010-03-14 17:16:45 +02001307# Returns the local Fully Qualified Domain Name (FQDN) if available.
1308#
1309# Tightly configured MTAa require that a caller sends a real DNS
1310# domain name that corresponds the IP address in the HELO/EHLO
1311# handshake. This is used to verify the connection and prevent
1312# spammers from trying to hide their identity. If the DNS and IP don't
Elijah Newren15beaaa2019-11-05 17:07:23 +00001313# match, the receiving MTA may deny the connection.
Jari Aalto134550f2010-03-14 17:16:45 +02001314#
1315# Here is a deny example of Net::SMTP with the default "localhost.localdomain"
1316#
1317# Net::SMTP=GLOB(0x267ec28)>>> EHLO localhost.localdomain
1318# Net::SMTP=GLOB(0x267ec28)<<< 550 EHLO argument does not match calling host
1319#
1320# This maildomain*() code is based on ideas in Perl library Test::Reporter
1321# /usr/share/perl5/Test/Reporter/Mail/Util.pm ==> sub _maildomain ()
1322
Brian Gernhardt59a86302010-04-10 10:53:54 -04001323sub valid_fqdn {
1324 my $domain = shift;
Brandon Casey61ef5e92010-09-26 22:18:01 -05001325 return defined $domain && !($^O eq 'darwin' && $domain =~ /\.local$/) && $domain =~ /\./;
Brian Gernhardt59a86302010-04-10 10:53:54 -04001326}
1327
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001328sub maildomain_net {
Jari Aalto134550f2010-03-14 17:16:45 +02001329 my $maildomain;
1330
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +02001331 require Net::Domain;
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001332 my $domain = Net::Domain::domainname();
1333 $maildomain = $domain if valid_fqdn($domain);
Jari Aalto134550f2010-03-14 17:16:45 +02001334
1335 return $maildomain;
1336}
1337
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001338sub maildomain_mta {
Jari Aalto134550f2010-03-14 17:16:45 +02001339 my $maildomain;
1340
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001341 for my $host (qw(mailhost localhost)) {
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +02001342 require Net::SMTP;
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001343 my $smtp = Net::SMTP->new($host);
1344 if (defined $smtp) {
1345 my $domain = $smtp->domain;
1346 $smtp->quit;
Jari Aalto134550f2010-03-14 17:16:45 +02001347
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001348 $maildomain = $domain if valid_fqdn($domain);
Jari Aalto134550f2010-03-14 17:16:45 +02001349
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001350 last if $maildomain;
Jari Aalto134550f2010-03-14 17:16:45 +02001351 }
1352 }
1353
1354 return $maildomain;
1355}
1356
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001357sub maildomain {
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001358 return maildomain_net() || maildomain_mta() || 'localhost.localdomain';
Jari Aalto134550f2010-03-14 17:16:45 +02001359}
1360
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001361sub smtp_host_string {
1362 if (defined $smtp_server_port) {
1363 return "$smtp_server:$smtp_server_port";
1364 } else {
1365 return $smtp_server;
1366 }
1367}
1368
1369# Returns 1 if authentication succeeded or was not necessary
1370# (smtp_user was not specified), and 0 otherwise.
1371
1372sub smtp_auth_maybe {
Joshua Watt8dd9b3f2018-10-22 22:24:25 -05001373 if (!defined $smtp_authuser || $auth || (defined $smtp_auth && $smtp_auth eq "none")) {
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001374 return 1;
1375 }
1376
1377 # Workaround AUTH PLAIN/LOGIN interaction defect
1378 # with Authen::SASL::Cyrus
1379 eval {
1380 require Authen::SASL;
1381 Authen::SASL->import(qw(Perl));
1382 };
1383
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001384 # Check mechanism naming as defined in:
1385 # https://tools.ietf.org/html/rfc4422#page-8
Brian Norris904f6e72015-09-18 15:12:50 -07001386 if ($smtp_auth && $smtp_auth !~ /^(\b[A-Z0-9-_]{1,20}\s*)*$/) {
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001387 die "invalid smtp auth: '${smtp_auth}'";
1388 }
1389
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001390 # TODO: Authentication may fail not because credentials were
1391 # invalid but due to other reasons, in which we should not
1392 # reject credentials.
1393 $auth = Git::credential({
1394 'protocol' => 'smtp',
1395 'host' => smtp_host_string(),
1396 'username' => $smtp_authuser,
1397 # if there's no password, "git credential fill" will
1398 # give us one, otherwise it'll just pass this one.
1399 'password' => $smtp_authpass
1400 }, sub {
1401 my $cred = shift;
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001402
1403 if ($smtp_auth) {
1404 my $sasl = Authen::SASL->new(
1405 mechanism => $smtp_auth,
1406 callback => {
1407 user => $cred->{'username'},
1408 pass => $cred->{'password'},
1409 authname => $cred->{'username'},
1410 }
1411 );
1412
1413 return !!$smtp->auth($sasl);
1414 }
1415
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001416 return !!$smtp->auth($cred->{'username'}, $cred->{'password'});
1417 });
1418
1419 return $auth;
1420}
1421
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001422sub ssl_verify_params {
1423 eval {
1424 require IO::Socket::SSL;
1425 IO::Socket::SSL->import(qw/SSL_VERIFY_PEER SSL_VERIFY_NONE/);
1426 };
1427 if ($@) {
1428 print STDERR "Not using SSL_VERIFY_PEER due to out-of-date IO::Socket::SSL.\n";
1429 return;
1430 }
1431
1432 if (!defined $smtp_ssl_cert_path) {
Ruben Kerkhof01645b72014-01-15 21:31:11 +04001433 # use the OpenSSL defaults
1434 return (SSL_verify_mode => SSL_VERIFY_PEER());
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001435 }
1436
1437 if ($smtp_ssl_cert_path eq "") {
1438 return (SSL_verify_mode => SSL_VERIFY_NONE());
1439 } elsif (-d $smtp_ssl_cert_path) {
1440 return (SSL_verify_mode => SSL_VERIFY_PEER(),
1441 SSL_ca_path => $smtp_ssl_cert_path);
1442 } elsif (-f $smtp_ssl_cert_path) {
1443 return (SSL_verify_mode => SSL_VERIFY_PEER(),
1444 SSL_ca_file => $smtp_ssl_cert_path);
1445 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001446 die sprintf(__("CA path \"%s\" does not exist"), $smtp_ssl_cert_path);
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001447 }
1448}
1449
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001450sub file_name_is_absolute {
1451 my ($path) = @_;
1452
1453 # msys does not grok DOS drive-prefixes
1454 if ($^O eq 'msys') {
Junio C Hamanof24ecf52014-04-23 09:37:38 -07001455 return ($path =~ m#^/# || $path =~ m#^[a-zA-Z]\:#)
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001456 }
1457
1458 require File::Spec::Functions;
1459 return File::Spec::Functions::file_name_is_absolute($path);
1460}
1461
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001462# Prepares the email, then asks the user what to do.
1463#
1464# If the user chooses to send the email, it's sent and 1 is returned.
1465# If the user chooses not to send the email, 0 is returned.
1466# If the user decides they want to make further edits, -1 is returned and the
1467# caller is expected to call send_message again after the edits are performed.
1468#
1469# If an error occurs sending the email, this just dies.
Michael Witten15da1082009-04-13 13:23:51 -05001470
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001471sub send_message {
Eric Wong4bc87a22006-03-25 17:20:48 -08001472 my @recipients = unique_email_list(@to);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001473 @cc = (grep { my $cc = extract_valid_address_or_die($_);
Joe Perches83acaae2010-11-20 15:06:05 -08001474 not grep { $cc eq $_ || $_ =~ /<\Q${cc}\E>$/ } @recipients
Ask Bjørn Hansen7ac17522007-11-19 03:00:26 -08001475 }
Ask Bjørn Hansen7ac17522007-11-19 03:00:26 -08001476 @cc);
Eric Wong4bc87a22006-03-25 17:20:48 -08001477 my $to = join (",\n\t", @recipients);
Ævar Arnfjörð Bjarmasone60f6d12019-05-09 13:48:29 +02001478 @recipients = unique_email_list(@recipients,@cc,@initial_bcc);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001479 @recipients = (map { extract_valid_address_or_die($_) } @recipients);
Jakub Narebski6bdca892006-07-07 20:57:55 +02001480 my $date = format_2822_time($time++);
Martin Langhoffe923eff2006-05-03 09:44:36 +12001481 my $gitversion = '@@GIT_VERSION@@';
1482 if ($gitversion =~ m/..GIT_VERSION../) {
Petr Baudis3cb8caf2006-07-03 22:47:58 +02001483 $gitversion = Git::version();
Martin Langhoffe923eff2006-05-03 09:44:36 +12001484 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001485
Joe Perches02461e02009-10-08 10:03:26 -07001486 my $cc = join(",\n\t", unique_email_list(@cc));
Junio C Hamanof06a6a42007-04-16 16:51:47 -07001487 my $ccline = "";
1488 if ($cc ne '') {
1489 $ccline = "\nCc: $cc";
1490 }
Jeff King4f3d3702007-12-17 15:51:34 -05001491 make_message_id() unless defined($message_id);
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001492
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001493 my $header = "From: $sender
Junio C Hamanof06a6a42007-04-16 16:51:47 -07001494To: $to${ccline}
Eric Wong4bc87a22006-03-25 17:20:48 -08001495Subject: $subject
Eric Wong4bc87a22006-03-25 17:20:48 -08001496Date: $date
1497Message-Id: $message_id
Eric Wong4bc87a22006-03-25 17:20:48 -08001498";
Luis Henriquesac1596a2014-03-24 21:38:27 +00001499 if ($use_xmailer) {
1500 $header .= "X-Mailer: git-send-email $gitversion\n";
1501 }
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001502 if ($in_reply_to) {
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001503
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001504 $header .= "In-Reply-To: $in_reply_to\n";
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001505 $header .= "References: $references\n";
1506 }
Christian Ludwigd11c9432018-03-04 00:58:14 +01001507 if ($reply_to) {
1508 $header .= "Reply-To: $reply_to\n";
1509 }
Junio C Hamanoce91c2f2006-10-05 16:36:49 -07001510 if (@xh) {
1511 $header .= join("\n", @xh) . "\n";
1512 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001513
Robin H. Johnsonc38f0242007-04-25 19:37:20 -07001514 my @sendmail_parameters = ('-i', @recipients);
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001515 my $raw_from = $sender;
Felipe Contrerasc89e3242009-11-26 21:04:29 +02001516 if (defined $envelope_sender && $envelope_sender ne "auto") {
1517 $raw_from = $envelope_sender;
1518 }
Robin H. Johnsonf073a592007-04-25 19:37:22 -07001519 $raw_from = extract_valid_address($raw_from);
1520 unshift (@sendmail_parameters,
1521 '-f', $raw_from) if(defined $envelope_sender);
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001522
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001523 if ($needs_confirm && !$dry_run) {
1524 print "\n$header\n";
1525 if ($needs_confirm eq "inform") {
1526 $confirm_unconfigured = 0; # squelch this message for the rest of this run
Jay Soffian6e182512009-03-28 21:39:10 -04001527 $ask_default = "y"; # assume yes on EOF since user hasn't explicitly asked for confirmation
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001528 print __ <<EOF ;
1529 The Cc list above has been expanded by additional
1530 addresses found in the patch commit message. By default
1531 send-email prompts before sending whenever this occurs.
1532 This behavior is controlled by the sendemail.confirm
1533 configuration setting.
1534
1535 For additional information, run 'git send-email --help'.
1536 To retain the current behavior, but squelch this message,
1537 run 'git config --global sendemail.confirm auto'.
1538
1539EOF
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001540 }
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001541 # TRANSLATORS: Make sure to include [y] [n] [e] [q] [a] in your
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001542 # translation. The program will only accept English input
1543 # at this point.
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001544 $_ = ask(__("Send this email? ([y]es|[n]o|[e]dit|[q]uit|[a]ll): "),
1545 valid_re => qr/^(?:yes|y|no|n|edit|e|quit|q|all|a)/i,
Jay Soffian6e182512009-03-28 21:39:10 -04001546 default => $ask_default);
Vasco Almeida46493102016-12-14 11:54:36 -01001547 die __("Send this email reply required") unless defined $_;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001548 if (/^n/i) {
Michael Witten15da1082009-04-13 13:23:51 -05001549 return 0;
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001550 } elsif (/^e/i) {
1551 return -1;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001552 } elsif (/^q/i) {
1553 cleanup_compose_files();
1554 exit(0);
1555 } elsif (/^a/i) {
1556 $confirm = 'never';
1557 }
1558 }
1559
Pascal Obry052fbea2010-09-06 20:12:11 +02001560 unshift (@sendmail_parameters, @smtp_server_options);
1561
Matthew Wilcox61302592006-10-10 08:58:23 -06001562 if ($dry_run) {
1563 # We don't want to send the email.
Gregory Anderscd5b33f2021-05-14 09:15:53 -06001564 } elsif (defined $sendmail_cmd || file_name_is_absolute($smtp_server)) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001565 my $pid = open my $sm, '|-';
1566 defined $pid or die $!;
1567 if (!$pid) {
Gregory Anderscd5b33f2021-05-14 09:15:53 -06001568 if (defined $sendmail_cmd) {
1569 exec ("sh", "-c", "$sendmail_cmd \"\$@\"", "-", @sendmail_parameters)
1570 or die $!;
1571 } else {
1572 exec ($smtp_server, @sendmail_parameters)
1573 or die $!;
1574 }
Eric Wongaca7ad72006-05-15 02:34:44 -07001575 }
1576 print $sm "$header\n$message";
Ævar Arnfjörð Bjarmason5e2c2ab2010-09-30 13:43:07 +00001577 close $sm or die $!;
Eric Wongaca7ad72006-05-15 02:34:44 -07001578 } else {
Junio C Hamano44b24762007-09-25 17:27:54 -07001579
1580 if (!defined $smtp_server) {
Vasco Almeida46493102016-12-14 11:54:36 -01001581 die __("The required SMTP server is not properly defined.")
Junio C Hamano44b24762007-09-25 17:27:54 -07001582 }
1583
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001584 require Net::SMTP;
Jonathan Niederbfbfc9a2017-05-31 17:17:43 -07001585 my $use_net_smtp_ssl = version->parse($Net::SMTP::VERSION) < version->parse("2.34");
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001586 $smtp_domain ||= maildomain();
1587
Thomas Rastf6bebd12008-06-25 21:42:43 +02001588 if ($smtp_encryption eq 'ssl') {
Junio C Hamano44b24762007-09-25 17:27:54 -07001589 $smtp_server_port ||= 465; # ssmtp
Thomas Rast5508f3e2013-12-01 23:48:43 +01001590 require IO::Socket::SSL;
John Keeping9d605242015-12-03 21:47:18 +00001591
1592 # Suppress "variable accessed once" warning.
1593 {
1594 no warnings 'once';
1595 $IO::Socket::SSL::DEBUG = 1;
1596 }
1597
Thomas Rast5508f3e2013-12-01 23:48:43 +01001598 # Net::SMTP::SSL->new() does not forward any SSL options
1599 IO::Socket::SSL::set_client_defaults(
1600 ssl_verify_params());
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001601
1602 if ($use_net_smtp_ssl) {
1603 require Net::SMTP::SSL;
1604 $smtp ||= Net::SMTP::SSL->new($smtp_server,
1605 Hello => $smtp_domain,
1606 Port => $smtp_server_port,
1607 Debug => $debug_net_smtp);
1608 }
1609 else {
1610 $smtp ||= Net::SMTP->new($smtp_server,
1611 Hello => $smtp_domain,
1612 Port => $smtp_server_port,
1613 Debug => $debug_net_smtp,
1614 SSL => 1);
1615 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001616 }
Jules Maselbas636f3d72018-07-14 10:58:48 +02001617 elsif (!$smtp) {
brian m. carlson1a741bf2013-07-04 22:04:52 +00001618 $smtp_server_port ||= 25;
1619 $smtp ||= Net::SMTP->new($smtp_server,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001620 Hello => $smtp_domain,
brian m. carlson1a741bf2013-07-04 22:04:52 +00001621 Debug => $debug_net_smtp,
1622 Port => $smtp_server_port);
Yakov Lernerfb3650e2009-09-25 15:10:21 -07001623 if ($smtp_encryption eq 'tls' && $smtp) {
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001624 if ($use_net_smtp_ssl) {
1625 $smtp->command('STARTTLS');
1626 $smtp->response();
1627 if ($smtp->code != 220) {
1628 die sprintf(__("Server does not support STARTTLS! %s"), $smtp->message);
1629 }
1630 require Net::SMTP::SSL;
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001631 $smtp = Net::SMTP::SSL->start_SSL($smtp,
1632 ssl_verify_params())
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001633 or die sprintf(__("STARTTLS failed! %s"), IO::Socket::SSL::errstr());
Thomas Rastf6bebd12008-06-25 21:42:43 +02001634 }
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001635 else {
1636 $smtp->starttls(ssl_verify_params())
1637 or die sprintf(__("STARTTLS failed! %s"), IO::Socket::SSL::errstr());
1638 }
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001639 # Send EHLO again to receive fresh
1640 # supported commands
1641 $smtp->hello($smtp_domain);
Thomas Rastf6bebd12008-06-25 21:42:43 +02001642 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001643 }
Junio C Hamano44b24762007-09-25 17:27:54 -07001644
1645 if (!$smtp) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001646 die __("Unable to initialize SMTP properly. Check config and use --smtp-debug."),
1647 " VALUES: server=$smtp_server ",
Jari Aaltoe5afb3a2010-03-14 17:15:33 +02001648 "encryption=$smtp_encryption ",
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001649 "hello=$smtp_domain",
Sylvain Rabota1dd7e12011-04-29 20:23:24 +02001650 defined $smtp_server_port ? " port=$smtp_server_port" : "";
Junio C Hamano44b24762007-09-25 17:27:54 -07001651 }
1652
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001653 smtp_auth_maybe or die $smtp->message;
Michael Witten2363d742008-02-03 19:53:56 -05001654
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001655 $smtp->mail( $raw_from ) or die $smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001656 $smtp->to( @recipients ) or die $smtp->message;
1657 $smtp->data or die $smtp->message;
Stefan Agnerf60c4832015-09-30 09:26:09 +02001658 $smtp->datasend("$header\n") or die $smtp->message;
1659 my @lines = split /^/, $message;
1660 foreach my $line (@lines) {
1661 $smtp->datasend("$line") or die $smtp->message;
1662 }
Eric Wongaca7ad72006-05-15 02:34:44 -07001663 $smtp->dataend() or die $smtp->message;
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001664 $smtp->code =~ /250|200/ or die sprintf(__("Failed to send %s\n"), $subject).$smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001665 }
Ryan Anderson27184352006-02-05 20:13:52 -05001666 if ($quiet) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001667 printf($dry_run ? __("Dry-Sent %s\n") : __("Sent %s\n"), $subject);
Ryan Anderson27184352006-02-05 20:13:52 -05001668 } else {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001669 print($dry_run ? __("Dry-OK. Log says:\n") : __("OK. Log says:\n"));
Gregory Anderscd5b33f2021-05-14 09:15:53 -06001670 if (!defined $sendmail_cmd && !file_name_is_absolute($smtp_server)) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001671 print "Server: $smtp_server\n";
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001672 print "MAIL FROM:<$raw_from>\n";
Joe Perches02461e02009-10-08 10:03:26 -07001673 foreach my $entry (@recipients) {
1674 print "RCPT TO:<$entry>\n";
1675 }
Eric Wongaca7ad72006-05-15 02:34:44 -07001676 } else {
Gregory Anderscd5b33f2021-05-14 09:15:53 -06001677 my $sm;
1678 if (defined $sendmail_cmd) {
1679 $sm = $sendmail_cmd;
1680 } else {
1681 $sm = $smtp_server;
1682 }
1683
1684 print "Sendmail: $sm ".join(' ',@sendmail_parameters)."\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001685 }
David D. Kilzerb7f30e02007-11-18 20:14:55 -08001686 print $header, "\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001687 if ($smtp) {
Vasco Almeida46493102016-12-14 11:54:36 -01001688 print __("Result: "), $smtp->code, ' ',
Eric Wongaca7ad72006-05-15 02:34:44 -07001689 ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
1690 } else {
Vasco Almeida46493102016-12-14 11:54:36 -01001691 print __("Result: OK\n");
Eric Wongaca7ad72006-05-15 02:34:44 -07001692 }
Ryan Anderson30d08b32006-02-02 11:56:06 -05001693 }
Michael Witten15da1082009-04-13 13:23:51 -05001694
1695 return 1;
Ryan Anderson83b24432005-07-31 04:17:25 -04001696}
1697
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001698$in_reply_to = $initial_in_reply_to;
1699$references = $initial_in_reply_to || '';
Ryan Anderson83b24432005-07-31 04:17:25 -04001700$subject = $initial_subject;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001701$message_num = 0;
Ryan Anderson83b24432005-07-31 04:17:25 -04001702
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001703# Prepares the email, prompts the user, sends it out
1704# Returns 0 if an edit was done and the function should be called again, or 1
1705# otherwise.
1706sub process_file {
1707 my ($t) = @_;
1708
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001709 open my $fh, "<", $t or die sprintf(__("can't open file %s"), $t);
Ryan Anderson83b24432005-07-31 04:17:25 -04001710
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001711 my $author = undef;
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001712 my $sauthor = undef;
Jeff King8291db62007-11-16 05:49:09 -05001713 my $author_encoding;
1714 my $has_content_type;
1715 my $body_encoding;
Paolo Bonzinibb294562014-11-25 15:00:26 +01001716 my $xfer_encoding;
1717 my $has_mime_version;
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001718 @to = ();
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001719 @cc = ();
Junio C Hamanoce91c2f2006-10-05 16:36:49 -07001720 @xh = ();
Junio C Hamanoe6b09642006-10-07 03:09:05 -07001721 my $input_format = undef;
Jay Soffian50126992009-02-14 23:32:14 -05001722 my @header = ();
Ryan Anderson83b24432005-07-31 04:17:25 -04001723 $message = "";
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001724 $message_num++;
Jay Soffian50126992009-02-14 23:32:14 -05001725 # First unfold multiline header fields
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001726 while(<$fh>) {
Jay Soffian50126992009-02-14 23:32:14 -05001727 last if /^\s*$/;
1728 if (/^\s+\S/ and @header) {
1729 chomp($header[$#header]);
1730 s/^\s+/ /;
1731 $header[$#header] .= $_;
1732 } else {
1733 push(@header, $_);
1734 }
1735 }
1736 # Now parse the header
1737 foreach(@header) {
1738 if (/^From /) {
1739 $input_format = 'mbox';
1740 next;
1741 }
1742 chomp;
1743 if (!defined $input_format && /^[-A-Za-z]+:\s/) {
1744 $input_format = 'mbox';
1745 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001746
Jay Soffian50126992009-02-14 23:32:14 -05001747 if (defined $input_format && $input_format eq 'mbox') {
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001748 if (/^Subject:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001749 $subject = $1;
1750 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001751 elsif (/^From:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001752 ($author, $author_encoding) = unquote_rfc2047($1);
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001753 $sauthor = sanitize_address($author);
Jay Soffian50126992009-02-14 23:32:14 -05001754 next if $suppress_cc{'author'};
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001755 next if $suppress_cc{'self'} and $sauthor eq $sender;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001756 printf(__("(mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001757 $1, $_) unless $quiet;
1758 push @cc, $1;
1759 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001760 elsif (/^To:\s+(.*)$/i) {
Stephen Boyd21802cd2010-09-29 00:26:44 -07001761 foreach my $addr (parse_address_line($1)) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001762 printf(__("(mbox) Adding to: %s from line '%s'\n"),
Stephen Boyd21802cd2010-09-29 00:26:44 -07001763 $addr, $_) unless $quiet;
Krzysztof Mazure4312252012-11-22 19:12:10 +01001764 push @to, $addr;
Stephen Boyd21802cd2010-09-29 00:26:44 -07001765 }
1766 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001767 elsif (/^Cc:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001768 foreach my $addr (parse_address_line($1)) {
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001769 my $qaddr = unquote_rfc2047($addr);
1770 my $saddr = sanitize_address($qaddr);
1771 if ($saddr eq $sender) {
David Brown65648282007-12-25 19:56:29 -08001772 next if ($suppress_cc{'self'});
David Brown65648282007-12-25 19:56:29 -08001773 } else {
1774 next if ($suppress_cc{'cc'});
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001775 }
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001776 printf(__("(mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001777 $addr, $_) unless $quiet;
1778 push @cc, $addr;
Ryan Anderson83b24432005-07-31 04:17:25 -04001779 }
1780 }
Jay Soffian50126992009-02-14 23:32:14 -05001781 elsif (/^Content-type:/i) {
1782 $has_content_type = 1;
1783 if (/charset="?([^ "]+)/) {
1784 $body_encoding = $1;
1785 }
1786 push @xh, $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001787 }
Paolo Bonzinibb294562014-11-25 15:00:26 +01001788 elsif (/^MIME-Version/i) {
1789 $has_mime_version = 1;
1790 push @xh, $_;
1791 }
Jay Soffian50126992009-02-14 23:32:14 -05001792 elsif (/^Message-Id: (.*)/i) {
1793 $message_id = $1;
1794 }
Paolo Bonzinibb294562014-11-25 15:00:26 +01001795 elsif (/^Content-Transfer-Encoding: (.*)/i) {
1796 $xfer_encoding = $1 if not defined $xfer_encoding;
1797 }
Stefan Agner256be1d2018-04-17 23:16:30 +02001798 elsif (/^In-Reply-To: (.*)/i) {
Rafael Aquinif9f60d72020-06-29 10:11:04 -04001799 if (!$initial_in_reply_to || $thread) {
1800 $in_reply_to = $1;
1801 }
Stefan Agner256be1d2018-04-17 23:16:30 +02001802 }
1803 elsif (/^References: (.*)/i) {
Rafael Aquinif9f60d72020-06-29 10:11:04 -04001804 if (!$initial_in_reply_to || $thread) {
1805 $references = $1;
1806 }
Stefan Agner256be1d2018-04-17 23:16:30 +02001807 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001808 elsif (!/^Date:\s/i && /^[-A-Za-z]+:\s+\S/) {
Jay Soffian50126992009-02-14 23:32:14 -05001809 push @xh, $_;
1810 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001811 } else {
Jay Soffian50126992009-02-14 23:32:14 -05001812 # In the traditional
1813 # "send lots of email" format,
1814 # line 1 = cc
1815 # line 2 = subject
1816 # So let's support that, too.
1817 $input_format = 'lots';
1818 if (@cc == 0 && !$suppress_cc{'cc'}) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001819 printf(__("(non-mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001820 $_, $_) unless $quiet;
1821 push @cc, $_;
1822 } elsif (!defined $subject) {
1823 $subject = $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001824 }
1825 }
1826 }
Jay Soffian50126992009-02-14 23:32:14 -05001827 # Now parse the message body
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001828 while(<$fh>) {
Jay Soffian50126992009-02-14 23:32:14 -05001829 $message .= $_;
Baruch Siach9c545812019-03-16 21:26:50 +02001830 if (/^([a-z][a-z-]*-by|Cc): (.*)/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001831 chomp;
Jay Soffian3531e272009-02-14 23:32:15 -05001832 my ($what, $c) = ($1, $2);
Matthieu Moycb2922f2017-08-23 12:21:01 +02001833 # strip garbage for the address we'll use:
1834 $c = strip_garbage_one_address($c);
1835 # sanitize a bit more to decide whether to suppress the address:
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001836 my $sc = sanitize_address($c);
1837 if ($sc eq $sender) {
Jay Soffian3531e272009-02-14 23:32:15 -05001838 next if ($suppress_cc{'self'});
1839 } else {
Rasmus Villemoesef0cc1d2018-10-16 09:39:23 +02001840 if ($what =~ /^Signed-off-by$/i) {
1841 next if $suppress_cc{'sob'};
1842 } elsif ($what =~ /-by$/i) {
1843 next if $suppress_cc{'misc-by'};
1844 } elsif ($what =~ /Cc/i) {
1845 next if $suppress_cc{'bodycc'};
1846 }
Jay Soffian3531e272009-02-14 23:32:15 -05001847 }
Rasmus Villemoesfb34fe62018-10-10 13:13:50 +02001848 if ($c !~ /.+@.+|<.+>/) {
1849 printf("(body) Ignoring %s from line '%s'\n",
1850 $what, $_) unless $quiet;
1851 next;
1852 }
Jay Soffian50126992009-02-14 23:32:14 -05001853 push @cc, $c;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001854 printf(__("(body) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001855 $c, $_) unless $quiet;
1856 }
1857 }
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001858 close $fh;
Joe Perches324a8bd2007-08-17 18:51:12 -07001859
Joe Perches6e74e072010-09-24 10:03:00 -07001860 push @to, recipients_cmd("to-cmd", "to", $to_cmd, $t)
1861 if defined $to_cmd;
1862 push @cc, recipients_cmd("cc-cmd", "cc", $cc_cmd, $t)
1863 if defined $cc_cmd && !$suppress_cc{'cccmd'};
Joe Perches324a8bd2007-08-17 18:51:12 -07001864
Thomas Rast3cae7e52010-06-17 22:10:39 +02001865 if ($broken_encoding{$t} && !$has_content_type) {
Paolo Bonzinibb294562014-11-25 15:00:26 +01001866 $xfer_encoding = '8bit' if not defined $xfer_encoding;
Thomas Rast3cae7e52010-06-17 22:10:39 +02001867 $has_content_type = 1;
Paolo Bonzinibb294562014-11-25 15:00:26 +01001868 push @xh, "Content-Type: text/plain; charset=$auto_8bit_encoding";
Thomas Rast3cae7e52010-06-17 22:10:39 +02001869 $body_encoding = $auto_8bit_encoding;
1870 }
1871
Krzysztof Mazurce547802012-10-24 23:08:26 +02001872 if ($broken_encoding{$t} && !is_rfc2047_quoted($subject)) {
1873 $subject = quote_subject($subject, $auto_8bit_encoding);
Thomas Rast3cae7e52010-06-17 22:10:39 +02001874 }
1875
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001876 if (defined $sauthor and $sauthor ne $sender) {
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001877 $message = "From: $author\n\n$message";
Jeff King8291db62007-11-16 05:49:09 -05001878 if (defined $author_encoding) {
1879 if ($has_content_type) {
1880 if ($body_encoding eq $author_encoding) {
1881 # ok, we already have the right encoding
1882 }
1883 else {
1884 # uh oh, we should re-encode
1885 }
1886 }
1887 else {
Paolo Bonzinibb294562014-11-25 15:00:26 +01001888 $xfer_encoding = '8bit' if not defined $xfer_encoding;
Thomas Rast3cae7e52010-06-17 22:10:39 +02001889 $has_content_type = 1;
Jeff King8291db62007-11-16 05:49:09 -05001890 push @xh,
Paolo Bonzinibb294562014-11-25 15:00:26 +01001891 "Content-Type: text/plain; charset=$author_encoding";
Jeff King8291db62007-11-16 05:49:09 -05001892 }
1893 }
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001894 }
brian m. carlsone67a2282018-07-08 22:17:12 +00001895 $xfer_encoding = '8bit' if not defined $xfer_encoding;
1896 ($message, $xfer_encoding) = apply_transfer_encoding(
1897 $message, $xfer_encoding, $target_xfer_encoding);
1898 push @xh, "Content-Transfer-Encoding: $xfer_encoding";
1899 unshift @xh, 'MIME-Version: 1.0' unless $has_mime_version;
Ryan Anderson83b24432005-07-31 04:17:25 -04001900
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001901 $needs_confirm = (
1902 $confirm eq "always" or
1903 ($confirm =~ /^(?:auto|cc)$/ && @cc) or
1904 ($confirm =~ /^(?:auto|compose)$/ && $compose && $message_num == 1));
1905 $needs_confirm = "inform" if ($needs_confirm && $confirm_unconfigured && @cc);
1906
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001907 @to = process_address_list(@to);
1908 @cc = process_address_list(@cc);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001909
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001910 @to = (@initial_to, @to);
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001911 @cc = (@initial_cc, @cc);
1912
Michael S. Tsirkinf515c902014-04-29 08:41:16 +03001913 if ($message_num == 1) {
1914 if (defined $cover_cc and $cover_cc) {
1915 @initial_cc = @cc;
1916 }
1917 if (defined $cover_to and $cover_to) {
1918 @initial_to = @to;
1919 }
1920 }
1921
Michael Witten15da1082009-04-13 13:23:51 -05001922 my $message_was_sent = send_message();
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001923 if ($message_was_sent == -1) {
1924 do_edit($t);
1925 return 0;
1926 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001927
1928 # set up for the next message
Junio C Hamano95a877a2009-06-12 09:23:43 -07001929 if ($thread && $message_was_sent &&
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001930 ($chain_reply_to || !defined $in_reply_to || length($in_reply_to) == 0 ||
Antonio Ospitedb54c8e2010-11-12 15:55:08 +01001931 $message_num == 1)) {
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001932 $in_reply_to = $message_id;
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001933 if (length $references > 0) {
YOSHIFUJI Hideaki / 吉藤英明a925b892007-04-06 08:50:24 +09001934 $references .= "\n $message_id";
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001935 } else {
1936 $references = "$message_id";
1937 }
Ryan Anderson78488b22005-07-31 20:04:24 -04001938 }
Jeff King4f3d3702007-12-17 15:51:34 -05001939 $message_id = undef;
xiaoqiang zhao5453b832017-05-21 20:59:50 +08001940 $num_sent++;
1941 if (defined $batch_size && $num_sent == $batch_size) {
1942 $num_sent = 0;
1943 $smtp->quit if defined $smtp;
1944 undef $smtp;
1945 undef $auth;
1946 sleep($relogin_delay) if defined $relogin_delay;
1947 }
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001948
1949 return 1;
1950}
1951
1952foreach my $t (@files) {
1953 while (!process_file($t)) {
1954 # user edited the file
1955 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001956}
Ryan Andersone2057352005-08-02 21:45:22 -04001957
Joe Perches6e74e072010-09-24 10:03:00 -07001958# Execute a command (e.g. $to_cmd) to get a list of email addresses
1959# and return a results array
1960sub recipients_cmd {
1961 my ($prefix, $what, $cmd, $file) = @_;
1962
Joe Perches6e74e072010-09-24 10:03:00 -07001963 my @addresses = ();
Ramkumar Ramachandraa47eab02013-03-31 18:40:42 -07001964 open my $fh, "-|", "$cmd \Q$file\E"
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001965 or die sprintf(__("(%s) Could not execute '%s'"), $prefix, $cmd);
Junio C Hamano7ebee442010-10-26 22:02:52 -07001966 while (my $address = <$fh>) {
Joe Perches6e74e072010-09-24 10:03:00 -07001967 $address =~ s/^\s*//g;
1968 $address =~ s/\s*$//g;
1969 $address = sanitize_address($address);
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001970 next if ($address eq $sender and $suppress_cc{'self'});
Joe Perches6e74e072010-09-24 10:03:00 -07001971 push @addresses, $address;
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001972 printf(__("(%s) Adding %s: %s from: '%s'\n"),
1973 $prefix, $what, $address, $cmd) unless $quiet;
Joe Perches6e74e072010-09-24 10:03:00 -07001974 }
Junio C Hamano7ebee442010-10-26 22:02:52 -07001975 close $fh
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001976 or die sprintf(__("(%s) failed to close pipe to '%s'"), $prefix, $cmd);
Joe Perches6e74e072010-09-24 10:03:00 -07001977 return @addresses;
1978}
1979
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001980cleanup_compose_files();
Ryan Anderson1f038a02005-09-05 01:13:07 -04001981
Ævar Arnfjörð Bjarmason4bf597e2010-09-30 13:43:00 +00001982sub cleanup_compose_files {
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001983 unlink($compose_filename, $compose_filename . ".final") if $compose;
Ryan Anderson1f038a02005-09-05 01:13:07 -04001984}
1985
Eric Wong4bc87a22006-03-25 17:20:48 -08001986$smtp->quit if $smtp;
Ryan Andersone2057352005-08-02 21:45:22 -04001987
Paolo Bonzini8d814082014-11-25 15:00:27 +01001988sub apply_transfer_encoding {
1989 my $message = shift;
1990 my $from = shift;
1991 my $to = shift;
1992
Aaron Lindsay3c88e462018-11-02 05:52:38 -04001993 return ($message, $to) if ($from eq $to and $from ne '7bit');
Paolo Bonzini8d814082014-11-25 15:00:27 +01001994
1995 require MIME::QuotedPrint;
1996 require MIME::Base64;
1997
1998 $message = MIME::QuotedPrint::decode($message)
1999 if ($from eq 'quoted-printable');
2000 $message = MIME::Base64::decode($message)
2001 if ($from eq 'base64');
2002
brian m. carlson74d76a12019-04-13 22:45:51 +00002003 $to = ($message =~ /(?:.{999,}|\r)/) ? 'quoted-printable' : '8bit'
brian m. carlson7a369872018-07-08 22:17:10 +00002004 if $to eq 'auto';
2005
Vasco Almeida46493102016-12-14 11:54:36 -01002006 die __("cannot send message as 7bit")
Paolo Bonzini8d814082014-11-25 15:00:27 +01002007 if ($to eq '7bit' and $message =~ /[^[:ascii:]]/);
brian m. carlson7a369872018-07-08 22:17:10 +00002008 return ($message, $to)
Paolo Bonzini8d814082014-11-25 15:00:27 +01002009 if ($to eq '7bit' or $to eq '8bit');
brian m. carlson7a369872018-07-08 22:17:10 +00002010 return (MIME::QuotedPrint::encode($message, "\n", 0), $to)
Paolo Bonzini8d814082014-11-25 15:00:27 +01002011 if ($to eq 'quoted-printable');
brian m. carlson7a369872018-07-08 22:17:10 +00002012 return (MIME::Base64::encode($message, "\n"), $to)
Paolo Bonzini8d814082014-11-25 15:00:27 +01002013 if ($to eq 'base64');
Vasco Almeida46493102016-12-14 11:54:36 -01002014 die __("invalid transfer encoding");
Paolo Bonzini8d814082014-11-25 15:00:27 +01002015}
2016
Ævar Arnfjörð Bjarmasonc438ea22010-09-30 13:42:59 +00002017sub unique_email_list {
Ryan Andersone2057352005-08-02 21:45:22 -04002018 my %seen;
2019 my @emails;
2020
2021 foreach my $entry (@_) {
Krzysztof Mazure4312252012-11-22 19:12:10 +01002022 my $clean = extract_valid_address_or_die($entry);
2023 $seen{$clean} ||= 0;
2024 next if $seen{$clean}++;
2025 push @emails, $entry;
Ryan Andersone2057352005-08-02 21:45:22 -04002026 }
2027 return @emails;
2028}
Jeff King747bbff2008-01-18 09:19:48 -05002029
2030sub validate_patch {
brian m. carlsonf2d06fb2018-07-08 22:17:11 +00002031 my ($fn, $xfer_encoding) = @_;
Jonathan Tan64896602017-05-12 15:38:26 -07002032
Jonathan Tan177409e2017-06-01 16:50:55 -07002033 if ($repo) {
Ævar Arnfjörð Bjarmason7cbc0452021-05-26 13:21:07 +02002034 my $hooks_path = $repo->command_oneline('rev-parse', '--git-path', 'hooks');
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +02002035 require File::Spec;
2036 my $validate_hook = File::Spec->catfile($hooks_path,
Jonathan Tan177409e2017-06-01 16:50:55 -07002037 'sendemail-validate');
2038 my $hook_error;
2039 if (-x $validate_hook) {
Ævar Arnfjörð Bjarmasonf4dc9432021-05-28 11:23:49 +02002040 require Cwd;
2041 my $target = Cwd::abs_path($fn);
Jonathan Tan177409e2017-06-01 16:50:55 -07002042 # The hook needs a correct cwd and GIT_DIR.
Ævar Arnfjörð Bjarmason17530b22021-05-28 11:23:52 +02002043 my $cwd_save = Cwd::getcwd();
Jonathan Tan177409e2017-06-01 16:50:55 -07002044 chdir($repo->wc_path() or $repo->repo_path())
2045 or die("chdir: $!");
2046 local $ENV{"GIT_DIR"} = $repo->repo_path();
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +02002047 $hook_error = system_or_msg([$validate_hook, $target]);
Jonathan Tan177409e2017-06-01 16:50:55 -07002048 chdir($cwd_save) or die("chdir: $!");
2049 }
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +02002050 if ($hook_error) {
2051 die sprintf(__("fatal: %s: rejected by sendemail-validate hook\n" .
Ævar Arnfjörð Bjarmasonea7811b2021-04-06 16:00:37 +02002052 "%s\n" .
2053 "warning: no patches were sent\n"), $fn, $hook_error);
Ævar Arnfjörð Bjarmasond21616c2021-04-06 16:00:36 +02002054 }
Jonathan Tan64896602017-05-12 15:38:26 -07002055 }
Jonathan Tan64896602017-05-12 15:38:26 -07002056
brian m. carlsonf2d06fb2018-07-08 22:17:11 +00002057 # Any long lines will be automatically fixed if we use a suitable transfer
2058 # encoding.
2059 unless ($xfer_encoding =~ /^(?:auto|quoted-printable|base64)$/) {
2060 open(my $fh, '<', $fn)
2061 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
2062 while (my $line = <$fh>) {
2063 if (length($line) > 998) {
Ævar Arnfjörð Bjarmasonea7811b2021-04-06 16:00:37 +02002064 die sprintf(__("fatal: %s:%d is longer than 998 characters\n" .
2065 "warning: no patches were sent\n"), $fn, $.);
brian m. carlsonf2d06fb2018-07-08 22:17:11 +00002066 }
Jeff King747bbff2008-01-18 09:19:48 -05002067 }
2068 }
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -07002069 return;
Jeff King747bbff2008-01-18 09:19:48 -05002070}
Jeff King0706bd12008-03-28 17:28:33 -04002071
Junio C Hamano531220b2016-03-17 22:40:05 -07002072sub handle_backup {
2073 my ($last, $lastlen, $file, $known_suffix) = @_;
2074 my ($suffix, $skip);
2075
2076 $skip = 0;
2077 if (defined $last &&
2078 ($lastlen < length($file)) &&
2079 (substr($file, 0, $lastlen) eq $last) &&
2080 ($suffix = substr($file, $lastlen)) !~ /^[a-z0-9]/i) {
2081 if (defined $known_suffix && $suffix eq $known_suffix) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01002082 printf(__("Skipping %s with backup suffix '%s'.\n"), $file, $known_suffix);
Junio C Hamano531220b2016-03-17 22:40:05 -07002083 $skip = 1;
2084 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01002085 # TRANSLATORS: please keep "[y|N]" as is.
2086 my $answer = ask(sprintf(__("Do you really want to send %s? [y|N]: "), $file),
Junio C Hamano531220b2016-03-17 22:40:05 -07002087 valid_re => qr/^(?:y|n)/i,
2088 default => 'n');
2089 $skip = ($answer ne 'y');
2090 if ($skip) {
2091 $known_suffix = $suffix;
2092 }
2093 }
2094 }
2095 return ($skip, $known_suffix);
2096}
2097
2098sub handle_backup_files {
2099 my @file = @_;
2100 my ($last, $lastlen, $known_suffix, $skip, @result);
2101 for my $file (@file) {
2102 ($skip, $known_suffix) = handle_backup($last, $lastlen,
2103 $file, $known_suffix);
2104 push @result, $file unless $skip;
2105 $last = $file;
2106 $lastlen = length($file);
2107 }
2108 return @result;
2109}
2110
Jeff King0706bd12008-03-28 17:28:33 -04002111sub file_has_nonascii {
2112 my $fn = shift;
2113 open(my $fh, '<', $fn)
Vasco Almeida3c5cd202016-12-14 11:54:37 -01002114 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
Jeff King0706bd12008-03-28 17:28:33 -04002115 while (my $line = <$fh>) {
2116 return 1 if $line =~ /[^[:ascii:]]/;
2117 }
2118 return 0;
2119}
Thomas Rast3cae7e52010-06-17 22:10:39 +02002120
2121sub body_or_subject_has_nonascii {
2122 my $fn = shift;
2123 open(my $fh, '<', $fn)
Vasco Almeida3c5cd202016-12-14 11:54:37 -01002124 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
Thomas Rast3cae7e52010-06-17 22:10:39 +02002125 while (my $line = <$fh>) {
2126 last if $line =~ /^$/;
2127 return 1 if $line =~ /^Subject.*[^[:ascii:]]/;
2128 }
2129 while (my $line = <$fh>) {
2130 return 1 if $line =~ /[^[:ascii:]]/;
2131 }
2132 return 0;
2133}