blob: b6af001419e11a6293c13892166f6c2ce9286b05 [file] [log] [blame]
Ævar Arnfjörð Bjarmason3328ace2010-09-24 20:00:53 +00001#!/usr/bin/perl
Ryan Anderson83b24432005-07-31 04:17:25 -04002#
Ryan Andersonf3d9f352005-07-31 20:04:24 -04003# Copyright 2002,2005 Greg Kroah-Hartman <greg@kroah.com>
4# Copyright 2005 Ryan Anderson <ryan@michonline.com>
Ryan Anderson83b24432005-07-31 04:17:25 -04005#
6# GPL v2 (See COPYING)
Junio C Hamano5825e5b2005-07-31 23:05:16 -07007#
Ryan Anderson83b24432005-07-31 04:17:25 -04008# Ported to support git "mbox" format files by Ryan Anderson <ryan@michonline.com>
9#
Ryan Andersonf3d9f352005-07-31 20:04:24 -040010# Sends a collection of emails to the given email addresses, disturbingly fast.
Junio C Hamano5825e5b2005-07-31 23:05:16 -070011#
Ryan Andersonf3d9f352005-07-31 20:04:24 -040012# Supports two formats:
13# 1. mbox format files (ignoring most headers and MIME formatting - this is designed for sending patches)
14# 2. The original format support by Greg's script:
Junio C Hamano5825e5b2005-07-31 23:05:16 -070015# first line of the message is who to CC,
Ryan Andersonf3d9f352005-07-31 20:04:24 -040016# and second line is the subject of the message.
Junio C Hamano5825e5b2005-07-31 23:05:16 -070017#
Ryan Anderson83b24432005-07-31 04:17:25 -040018
Ævar Arnfjörð Bjarmasond48b2842010-09-24 20:00:52 +000019use 5.008;
Ryan Anderson83b24432005-07-31 04:17:25 -040020use strict;
21use warnings;
Eric Wongf916ab02016-04-06 20:07:14 +000022use POSIX qw/strftime/;
Ryan Anderson83b24432005-07-31 04:17:25 -040023use Term::ReadLine;
Ryan Anderson83b24432005-07-31 04:17:25 -040024use Getopt::Long;
Wu Fengguang0e73b3e2008-12-19 16:10:10 +080025use Text::ParseWords;
Sean Estabrooks412876d2007-08-17 17:38:25 -040026use Term::ANSIColor;
Jay Soffianeed6ca72009-02-14 23:32:13 -050027use File::Temp qw/ tempdir tempfile /;
Jonathan Tan64896602017-05-12 15:38:26 -070028use File::Spec::Functions qw(catdir catfile);
Ævar Arnfjörð Bjarmason28654672018-03-03 15:38:13 +000029use Git::LoadCPAN::Error qw(:try);
Jonathan Tan64896602017-05-12 15:38:26 -070030use Cwd qw(abs_path cwd);
Petr Baudis3cb8caf2006-07-03 22:47:58 +020031use Git;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -010032use Git::I18N;
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +000033use Net::Domain ();
34use Net::SMTP ();
Ævar Arnfjörð Bjarmason28654672018-03-03 15:38:13 +000035use Git::LoadCPAN::Mail::Address;
Ryan Anderson83b24432005-07-31 04:17:25 -040036
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +010037Getopt::Long::Configure qw/ pass_through /;
38
Junio C Hamano280242d2006-07-02 16:03:59 -070039package FakeTerm;
40sub new {
41 my ($class, $reason) = @_;
42 return bless \$reason, shift;
43}
44sub readline {
45 my $self = shift;
46 die "Cannot use readline on FakeTerm: $$self";
47}
48package main;
49
Michael Coleman1b0baf12007-02-27 22:47:54 -060050
51sub usage {
52 print <<EOT;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +010053git send-email [options] <file | directory | rev-list options >
Jacob Keller17b7a832015-11-19 14:52:11 -080054git send-email --dump-aliases
Michael Witten4ed62b02008-09-30 07:58:30 -050055
56 Composing:
57 --from <str> * Email From:
Stephen Boydf434c082010-03-07 14:46:48 -080058 --[no-]to <str> * Email To:
59 --[no-]cc <str> * Email Cc:
60 --[no-]bcc <str> * Email Bcc:
Michael Witten4ed62b02008-09-30 07:58:30 -050061 --subject <str> * Email "Subject:"
Christian Ludwigd11c9432018-03-04 00:58:14 +010062 --reply-to <str> * Email "Reply-To:"
Michael Witten4ed62b02008-09-30 07:58:30 -050063 --in-reply-to <str> * Email "In-Reply-To:"
Luis Henriquesac1596a2014-03-24 21:38:27 +000064 --[no-]xmailer * Add "X-Mailer:" header (default).
Felipe Contreras402596a2013-04-07 01:10:27 -060065 --[no-]annotate * Review each patch that will be sent in an editor.
Michael Witten4ed62b02008-09-30 07:58:30 -050066 --compose * Open an editor for introduction.
Krzysztof Mazur62e00692012-10-10 01:02:56 +020067 --compose-encoding <str> * Encoding to assume for introduction.
Thomas Rast3cae7e52010-06-17 22:10:39 +020068 --8bit-encoding <str> * Encoding to assume 8bit mails if undeclared
Paolo Bonzini8d814082014-11-25 15:00:27 +010069 --transfer-encoding <str> * Transfer encoding to use (quoted-printable, 8bit, base64)
Michael Witten4ed62b02008-09-30 07:58:30 -050070
71 Sending:
72 --envelope-sender <str> * Email envelope sender.
73 --smtp-server <str:int> * Outgoing SMTP server to use. The port
74 is optional. Default 'localhost'.
Pascal Obry052fbea2010-09-06 20:12:11 +020075 --smtp-server-option <str> * Outgoing SMTP server option to use.
Michael Witten4ed62b02008-09-30 07:58:30 -050076 --smtp-server-port <int> * Outgoing SMTP server port.
77 --smtp-user <str> * Username for SMTP-AUTH.
78 --smtp-pass <str> * Password for SMTP-AUTH; not necessary.
79 --smtp-encryption <str> * tls or ssl; anything else disables.
80 --smtp-ssl * Deprecated. Use '--smtp-encryption ssl'.
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -070081 --smtp-ssl-cert-path <str> * Path to ca-certificates (either directory or file).
82 Pass an empty string to disable certificate
83 verification.
Jari Aalto134550f2010-03-14 17:16:45 +020084 --smtp-domain <str> * The domain name sent to HELO/EHLO handshake
Jan Viktorin0f2e68b2015-08-12 01:39:44 +020085 --smtp-auth <str> * Space-separated list of allowed AUTH mechanisms.
86 This setting forces to use one of the listed mechanisms.
Jari Aaltof60812e2010-03-14 17:16:09 +020087 --smtp-debug <0|1> * Disable, enable Net::SMTP debug.
Michael Witten4ed62b02008-09-30 07:58:30 -050088
xiaoqiang zhao5453b832017-05-21 20:59:50 +080089 --batch-size <int> * send max <int> message per connection.
90 --relogin-delay <int> * delay <int> seconds between two successive login.
91 This option can only be used with --batch-size
92
Michael Witten4ed62b02008-09-30 07:58:30 -050093 Automating:
94 --identity <str> * Use the sendemail.<id> options.
Joe Perches6e74e072010-09-24 10:03:00 -070095 --to-cmd <str> * Email To: via `<str> \$patch_path`
Michael Witten4ed62b02008-09-30 07:58:30 -050096 --cc-cmd <str> * Email Cc: via `<str> \$patch_path`
Jay Soffian3531e272009-02-14 23:32:15 -050097 --suppress-cc <str> * author, self, sob, cc, cccmd, body, bodycc, all.
Michael S. Tsirkinf515c902014-04-29 08:41:16 +030098 --[no-]cc-cover * Email Cc: addresses in the cover letter.
99 --[no-]to-cover * Email To: addresses in the cover letter.
Jay Soffian3531e272009-02-14 23:32:15 -0500100 --[no-]signed-off-by-cc * Send to Signed-off-by: addresses. Default on.
Michael Witten4ed62b02008-09-30 07:58:30 -0500101 --[no-]suppress-from * Send to self. Default off.
Junio C Hamano41fe87f2009-08-22 12:48:48 -0700102 --[no-]chain-reply-to * Chain In-Reply-To: fields. Default off.
Michael Witten4ed62b02008-09-30 07:58:30 -0500103 --[no-]thread * Use In-Reply-To: field. Default on.
104
105 Administering:
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500106 --confirm <str> * Confirm recipients before sending;
107 auto, cc, compose, always, or never.
Michael Witten4ed62b02008-09-30 07:58:30 -0500108 --quiet * Output one line of info per email.
109 --dry-run * Don't actually send the emails.
110 --[no-]validate * Perform patch sanity checks. Default on.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100111 --[no-]format-patch * understand any non optional arguments as
112 `git format-patch` ones.
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200113 --force * Send even if safety checks would prevent it.
Jeff Kingc764a0c2008-01-18 09:20:10 -0500114
Jacob Keller17b7a832015-11-19 14:52:11 -0800115 Information:
116 --dump-aliases * Dump configured aliases and exit.
117
Michael Coleman1b0baf12007-02-27 22:47:54 -0600118EOT
119 exit(1);
120}
121
Eric Wong4bc87a22006-03-25 17:20:48 -0800122# most mail servers generate the Date: header, but not all...
Jakub Narebski6bdca892006-07-07 20:57:55 +0200123sub format_2822_time {
124 my ($time) = @_;
125 my @localtm = localtime($time);
126 my @gmttm = gmtime($time);
127 my $localmin = $localtm[1] + $localtm[2] * 60;
128 my $gmtmin = $gmttm[1] + $gmttm[2] * 60;
129 if ($localtm[0] != $gmttm[0]) {
Vasco Almeida46493102016-12-14 11:54:36 -0100130 die __("local zone differs from GMT by a non-minute interval\n");
Jakub Narebski6bdca892006-07-07 20:57:55 +0200131 }
132 if ((($gmttm[6] + 1) % 7) == $localtm[6]) {
133 $localmin += 1440;
134 } elsif ((($gmttm[6] - 1) % 7) == $localtm[6]) {
135 $localmin -= 1440;
136 } elsif ($gmttm[6] != $localtm[6]) {
Vasco Almeida46493102016-12-14 11:54:36 -0100137 die __("local time offset greater than or equal to 24 hours\n");
Jakub Narebski6bdca892006-07-07 20:57:55 +0200138 }
139 my $offset = $localmin - $gmtmin;
140 my $offhour = $offset / 60;
141 my $offmin = abs($offset % 60);
142 if (abs($offhour) >= 24) {
Vasco Almeida46493102016-12-14 11:54:36 -0100143 die __("local time offset greater than or equal to 24 hours\n");
Jakub Narebski6bdca892006-07-07 20:57:55 +0200144 }
145
146 return sprintf("%s, %2d %s %d %02d:%02d:%02d %s%02d%02d",
147 qw(Sun Mon Tue Wed Thu Fri Sat)[$localtm[6]],
148 $localtm[3],
149 qw(Jan Feb Mar Apr May Jun
150 Jul Aug Sep Oct Nov Dec)[$localtm[4]],
151 $localtm[5]+1900,
152 $localtm[2],
153 $localtm[1],
154 $localtm[0],
155 ($offset >= 0) ? '+' : '-',
156 abs($offhour),
157 $offmin,
158 );
159}
Eric Wong4bc87a22006-03-25 17:20:48 -0800160
Eric Wong567ffeb2006-03-25 16:47:12 -0800161my $have_email_valid = eval { require Email::Valid; 1 };
Eric Wong4bc87a22006-03-25 17:20:48 -0800162my $smtp;
Wincent Colaiuta5f5b6112007-11-21 13:35:05 +0100163my $auth;
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800164my $num_sent = 0;
Eric Wong4bc87a22006-03-25 17:20:48 -0800165
Роман Донченко11f70a72014-12-14 18:59:46 +0300166# Regexes for RFC 2047 productions.
167my $re_token = qr/[^][()<>@,;:\\"\/?.= \000-\037\177-\377]+/;
168my $re_encoded_text = qr/[^? \000-\037\177-\377]+/;
169my $re_encoded_word = qr/=\?($re_token)\?($re_token)\?($re_encoded_text)\?=/;
170
Ryan Anderson83b24432005-07-31 04:17:25 -0400171# Variables we fill in automatically, or via prompting:
Ævar Arnfjörð Bjarmasone60f6d12019-05-09 13:48:29 +0200172my (@to,$no_to,@initial_to,@cc,$no_cc,@initial_cc,@initial_bcc,$no_bcc,@xh,
Christian Ludwigd11c9432018-03-04 00:58:14 +0100173 $initial_in_reply_to,$reply_to,$initial_subject,@files,
Luis Henriquesac1596a2014-03-24 21:38:27 +0000174 $author,$sender,$smtp_authpass,$annotate,$use_xmailer,$compose,$time);
Ryan Anderson83b24432005-07-31 04:17:25 -0400175
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700176my $envelope_sender;
Ryan Anderson78488b22005-07-31 20:04:24 -0400177
Ryan Anderson91332612005-07-31 20:04:24 -0400178# Example reply to:
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100179#$initial_in_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
Ryan Anderson83b24432005-07-31 04:17:25 -0400180
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100181my $repo = eval { Git->repository() };
182my @repo = $repo ? ($repo) : ();
Junio C Hamano280242d2006-07-02 16:03:59 -0700183my $term = eval {
Jay Soffian0fb7fc72008-02-21 19:16:04 -0500184 $ENV{"GIT_SEND_EMAIL_NOTTY"}
185 ? new Term::ReadLine 'git-send-email', \*STDIN, \*STDOUT
186 : new Term::ReadLine 'git-send-email';
Junio C Hamano280242d2006-07-02 16:03:59 -0700187};
188if ($@) {
189 $term = new FakeTerm "$@: going non-interactive";
190}
Ryan Anderson83b24432005-07-31 04:17:25 -0400191
Adam Roben5483c712007-06-27 20:59:37 -0700192# Behavior modification variables
193my ($quiet, $dry_run) = (0, 0);
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100194my $format_patch;
Jay Soffianafe756c2009-02-23 13:51:37 -0500195my $compose_filename;
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200196my $force = 0;
Jacob Keller17b7a832015-11-19 14:52:11 -0800197my $dump_aliases = 0;
Adam Roben5483c712007-06-27 20:59:37 -0700198
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100199# Handle interactive edition of files.
200my $multiedit;
Michael J Gruber0ce142c2010-03-22 17:12:53 +0100201my $editor;
Jonathan Niederb4479f02009-10-30 20:42:34 -0500202
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100203sub do_edit {
Michael J Gruber0ce142c2010-03-22 17:12:53 +0100204 if (!defined($editor)) {
205 $editor = Git::command_oneline('var', 'GIT_EDITOR');
206 }
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100207 if (defined($multiedit) && !$multiedit) {
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100208 map {
209 system('sh', '-c', $editor.' "$@"', $editor, $_);
210 if (($? & 127) || ($? >> 8)) {
Vasco Almeida46493102016-12-14 11:54:36 -0100211 die(__("the editor exited uncleanly, aborting everything"));
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100212 }
213 } @_;
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100214 } else {
215 system('sh', '-c', $editor.' "$@"', $editor, @_);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100216 if (($? & 127) || ($? >> 8)) {
Vasco Almeida46493102016-12-14 11:54:36 -0100217 die(__("the editor exited uncleanly, aborting everything"));
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100218 }
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100219 }
220}
Adam Roben5483c712007-06-27 20:59:37 -0700221
222# Variables with corresponding config settings
Joe Perches6e74e072010-09-24 10:03:00 -0700223my ($thread, $chain_reply_to, $suppress_from, $signed_off_by_cc);
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300224my ($cover_cc, $cover_to);
Joe Perches6e74e072010-09-24 10:03:00 -0700225my ($to_cmd, $cc_cmd);
Pascal Obry052fbea2010-09-06 20:12:11 +0200226my ($smtp_server, $smtp_server_port, @smtp_server_options);
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -0700227my ($smtp_authuser, $smtp_encryption, $smtp_ssl_cert_path);
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800228my ($batch_size, $relogin_delay);
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200229my ($identity, $aliasfiletype, @alias_files, $smtp_domain, $smtp_auth);
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500230my ($validate, $confirm);
David Brown65648282007-12-25 19:56:29 -0800231my (@suppress_cc);
Thomas Rast3cae7e52010-06-17 22:10:39 +0200232my ($auto_8bit_encoding);
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200233my ($compose_encoding);
brian m. carlsone67a2282018-07-08 22:17:12 +0000234my $target_xfer_encoding = 'auto';
Adam Roben5483c712007-06-27 20:59:37 -0700235
Jari Aaltof60812e2010-03-14 17:16:09 +0200236my ($debug_net_smtp) = 0; # Net::SMTP, see send_message()
237
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900238my %config_bool_settings = (
Adam Roben5483c712007-06-27 20:59:37 -0700239 "thread" => [\$thread, 1],
Felipe Contrerasb99d22f2013-05-24 22:44:52 -0500240 "chainreplyto" => [\$chain_reply_to, 0],
David Brown65648282007-12-25 19:56:29 -0800241 "suppressfrom" => [\$suppress_from, undef],
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500242 "signedoffbycc" => [\$signed_off_by_cc, undef],
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300243 "cccover" => [\$cover_cc, undef],
244 "tocover" => [\$cover_to, undef],
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500245 "signedoffcc" => [\$signed_off_by_cc, undef], # Deprecated
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500246 "validate" => [\$validate, 1],
Felipe Contreras402596a2013-04-07 01:10:27 -0600247 "multiedit" => [\$multiedit, undef],
Luis Henriquesac1596a2014-03-24 21:38:27 +0000248 "annotate" => [\$annotate, undef],
249 "xmailer" => [\$use_xmailer, 1]
Adam Robene46f7a02007-06-26 15:48:30 -0700250);
251
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900252my %config_settings = (
253 "smtpserver" => \$smtp_server,
Junio C Hamano44b24762007-09-25 17:27:54 -0700254 "smtpserverport" => \$smtp_server_port,
Pascal Obry052fbea2010-09-06 20:12:11 +0200255 "smtpserveroption" => \@smtp_server_options,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900256 "smtpuser" => \$smtp_authuser,
257 "smtppass" => \$smtp_authpass,
Pascal Obrye1e91152010-09-06 20:12:09 +0200258 "smtpdomain" => \$smtp_domain,
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200259 "smtpauth" => \$smtp_auth,
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800260 "smtpbatchsize" => \$batch_size,
261 "smtprelogindelay" => \$relogin_delay,
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700262 "to" => \@initial_to,
Joe Perches6e74e072010-09-24 10:03:00 -0700263 "tocmd" => \$to_cmd,
Miklos Vajna5f8b9fc2008-04-27 14:14:58 +0200264 "cc" => \@initial_cc,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900265 "cccmd" => \$cc_cmd,
266 "aliasfiletype" => \$aliasfiletype,
Ævar Arnfjörð Bjarmasone60f6d12019-05-09 13:48:29 +0200267 "bcc" => \@initial_bcc,
David Brown65648282007-12-25 19:56:29 -0800268 "suppresscc" => \@suppress_cc,
Ask Bjørn Hansen9f7820a2008-06-07 00:33:42 -0700269 "envelopesender" => \$envelope_sender,
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500270 "confirm" => \$confirm,
Trent Piepho09caa242009-05-12 15:48:56 -0700271 "from" => \$sender,
Thomas Rast3cae7e52010-06-17 22:10:39 +0200272 "assume8bitencoding" => \$auto_8bit_encoding,
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200273 "composeencoding" => \$compose_encoding,
Paolo Bonzini8d814082014-11-25 15:00:27 +0100274 "transferencoding" => \$target_xfer_encoding,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900275);
Avi Kivity4a62d3f2007-03-11 19:19:44 +0200276
Cord Seelecec5dae2011-09-30 12:52:25 +0200277my %config_path_settings = (
278 "aliasesfile" => \@alias_files,
John Keeping6e07a3b2015-11-17 22:01:05 +0000279 "smtpsslcertpath" => \$smtp_ssl_cert_path,
Cord Seelecec5dae2011-09-30 12:52:25 +0200280);
281
Michael Witten87429972008-02-03 19:53:57 -0500282# Handle Uncouth Termination
283sub signal_handler {
284
285 # Make text normal
286 print color("reset"), "\n";
287
288 # SMTP password masked
289 system "stty echo";
290
291 # tmp files from --compose
Jay Soffianafe756c2009-02-23 13:51:37 -0500292 if (defined $compose_filename) {
293 if (-e $compose_filename) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100294 printf __("'%s' contains an intermediate version ".
295 "of the email you were composing.\n"),
296 $compose_filename;
Jay Soffianafe756c2009-02-23 13:51:37 -0500297 }
298 if (-e ($compose_filename . ".final")) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100299 printf __("'%s.final' contains the composed email.\n"),
300 $compose_filename;
Jay Soffianafe756c2009-02-23 13:51:37 -0500301 }
Michael Witten87429972008-02-03 19:53:57 -0500302 }
303
304 exit;
305};
306
307$SIG{TERM} = \&signal_handler;
308$SIG{INT} = \&signal_handler;
309
Ævar Arnfjörð Bjarmasonc5735722019-05-09 13:48:28 +0200310# Read our sendemail.* config
311sub read_config {
312 my ($prefix) = @_;
313
314 foreach my $setting (keys %config_bool_settings) {
315 my $target = $config_bool_settings{$setting}->[0];
316 $$target = Git::config_bool(@repo, "$prefix.$setting") unless (defined $$target);
317 }
318
319 foreach my $setting (keys %config_path_settings) {
320 my $target = $config_path_settings{$setting};
321 if (ref($target) eq "ARRAY") {
322 unless (@$target) {
323 my @values = Git::config_path(@repo, "$prefix.$setting");
324 @$target = @values if (@values && defined $values[0]);
325 }
326 }
327 else {
328 $$target = Git::config_path(@repo, "$prefix.$setting") unless (defined $$target);
329 }
330 }
331
332 foreach my $setting (keys %config_settings) {
333 my $target = $config_settings{$setting};
334 next if $setting eq "to" and defined $no_to;
335 next if $setting eq "cc" and defined $no_cc;
336 next if $setting eq "bcc" and defined $no_bcc;
337 if (ref($target) eq "ARRAY") {
338 unless (@$target) {
339 my @values = Git::config(@repo, "$prefix.$setting");
340 @$target = @values if (@values && defined $values[0]);
341 }
342 }
343 else {
344 $$target = Git::config(@repo, "$prefix.$setting") unless (defined $$target);
345 }
346 }
347
348 if (!defined $smtp_encryption) {
349 my $enc = Git::config(@repo, "$prefix.smtpencryption");
350 if (defined $enc) {
351 $smtp_encryption = $enc;
352 } elsif (Git::config_bool(@repo, "$prefix.smtpssl")) {
353 $smtp_encryption = 'ssl';
354 }
355 }
356}
357
Ryan Anderson83b24432005-07-31 04:17:25 -0400358# Begin by accumulating all the variables (defined above), that we will end up
359# needing, first, from the command line:
360
Clemens Buchacherc5978242011-09-03 19:06:13 +0200361my $help;
362my $rc = GetOptions("h" => \$help,
Jacob Keller17b7a832015-11-19 14:52:11 -0800363 "dump-aliases" => \$dump_aliases);
364usage() unless $rc;
Vasco Almeida46493102016-12-14 11:54:36 -0100365die __("--dump-aliases incompatible with other options\n")
Jacob Keller17b7a832015-11-19 14:52:11 -0800366 if !$help and $dump_aliases and @ARGV;
367$rc = GetOptions(
Clemens Buchacherc5978242011-09-03 19:06:13 +0200368 "sender|from=s" => \$sender,
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100369 "in-reply-to=s" => \$initial_in_reply_to,
Christian Ludwigd11c9432018-03-04 00:58:14 +0100370 "reply-to=s" => \$reply_to,
Ryan Anderson83b24432005-07-31 04:17:25 -0400371 "subject=s" => \$initial_subject,
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700372 "to=s" => \@initial_to,
Joe Perches6e74e072010-09-24 10:03:00 -0700373 "to-cmd=s" => \$to_cmd,
Stephen Boydf434c082010-03-07 14:46:48 -0800374 "no-to" => \$no_to,
Ryan Andersonda140f82006-02-13 03:05:15 -0500375 "cc=s" => \@initial_cc,
Stephen Boydf434c082010-03-07 14:46:48 -0800376 "no-cc" => \$no_cc,
Ævar Arnfjörð Bjarmasone60f6d12019-05-09 13:48:29 +0200377 "bcc=s" => \@initial_bcc,
Stephen Boydf434c082010-03-07 14:46:48 -0800378 "no-bcc" => \$no_bcc,
Ryan Anderson78488b22005-07-31 20:04:24 -0400379 "chain-reply-to!" => \$chain_reply_to,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800380 "no-chain-reply-to" => sub {$chain_reply_to = 0},
Ryan Anderson3342d852005-07-31 20:04:24 -0400381 "smtp-server=s" => \$smtp_server,
Pascal Obry052fbea2010-09-06 20:12:11 +0200382 "smtp-server-option=s" => \@smtp_server_options,
Junio C Hamano44b24762007-09-25 17:27:54 -0700383 "smtp-server-port=s" => \$smtp_server_port,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900384 "smtp-user=s" => \$smtp_authuser,
Michael Witten2363d742008-02-03 19:53:56 -0500385 "smtp-pass:s" => \$smtp_authpass,
Thomas Rastf6bebd12008-06-25 21:42:43 +0200386 "smtp-ssl" => sub { $smtp_encryption = 'ssl' },
387 "smtp-encryption=s" => \$smtp_encryption,
Thomas Rast979e6522013-12-01 23:48:42 +0100388 "smtp-ssl-cert-path=s" => \$smtp_ssl_cert_path,
Jari Aaltof60812e2010-03-14 17:16:09 +0200389 "smtp-debug:i" => \$debug_net_smtp,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -0400390 "smtp-domain:s" => \$smtp_domain,
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200391 "smtp-auth=s" => \$smtp_auth,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900392 "identity=s" => \$identity,
Felipe Contreras402596a2013-04-07 01:10:27 -0600393 "annotate!" => \$annotate,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800394 "no-annotate" => sub {$annotate = 0},
Ryan Anderson1f038a02005-09-05 01:13:07 -0400395 "compose" => \$compose,
Ryan Anderson30d08b32006-02-02 11:56:06 -0500396 "quiet" => \$quiet,
Joe Perches324a8bd2007-08-17 18:51:12 -0700397 "cc-cmd=s" => \$cc_cmd,
Adam Roben5483c712007-06-27 20:59:37 -0700398 "suppress-from!" => \$suppress_from,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800399 "no-suppress-from" => sub {$suppress_from = 0},
David Brown65648282007-12-25 19:56:29 -0800400 "suppress-cc=s" => \@suppress_cc,
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500401 "signed-off-cc|signed-off-by-cc!" => \$signed_off_by_cc,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800402 "no-signed-off-cc|no-signed-off-by-cc" => sub {$signed_off_by_cc = 0},
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300403 "cc-cover|cc-cover!" => \$cover_cc,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800404 "no-cc-cover" => sub {$cover_cc = 0},
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300405 "to-cover|to-cover!" => \$cover_to,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800406 "no-to-cover" => sub {$cover_to = 0},
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500407 "confirm=s" => \$confirm,
Matthew Wilcox61302592006-10-10 08:58:23 -0600408 "dry-run" => \$dry_run,
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700409 "envelope-sender=s" => \$envelope_sender,
Adam Roben5483c712007-06-27 20:59:37 -0700410 "thread!" => \$thread,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800411 "no-thread" => sub {$thread = 0},
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500412 "validate!" => \$validate,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800413 "no-validate" => sub {$validate = 0},
Paolo Bonzini8d814082014-11-25 15:00:27 +0100414 "transfer-encoding=s" => \$target_xfer_encoding,
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100415 "format-patch!" => \$format_patch,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800416 "no-format-patch" => sub {$format_patch = 0},
Thomas Rast3cae7e52010-06-17 22:10:39 +0200417 "8bit-encoding=s" => \$auto_8bit_encoding,
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200418 "compose-encoding=s" => \$compose_encoding,
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200419 "force" => \$force,
Luis Henriquesac1596a2014-03-24 21:38:27 +0000420 "xmailer!" => \$use_xmailer,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800421 "no-xmailer" => sub {$use_xmailer = 0},
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800422 "batch-size=i" => \$batch_size,
423 "relogin-delay=i" => \$relogin_delay,
Ryan Anderson83b24432005-07-31 04:17:25 -0400424 );
425
Clemens Buchacherc5978242011-09-03 19:06:13 +0200426usage() if $help;
Michael Coleman1b0baf12007-02-27 22:47:54 -0600427unless ($rc) {
428 usage();
429}
430
Vasco Almeida46493102016-12-14 11:54:36 -0100431die __("Cannot run git format-patch from outside a repository\n")
Jay Soffianeed6ca72009-02-14 23:32:13 -0500432 if $format_patch and not $repo;
433
Stefan Beller9caa7062018-02-12 11:44:04 -0800434die __("`batch-size` and `relogin` must be specified together " .
435 "(via command-line or configuration option)\n")
436 if defined $relogin_delay and not defined $batch_size;
437
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900438# read configuration from [sendemail "$identity"], fall back on [sendemail]
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100439$identity = Git::config(@repo, "sendemail.identity") unless (defined $identity);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900440read_config("sendemail.$identity") if (defined $identity);
441read_config("sendemail");
442
443# fall back on builtin bool defaults
444foreach my $setting (values %config_bool_settings) {
445 ${$setting->[0]} = $setting->[1] unless (defined (${$setting->[0]}));
446}
447
Thomas Rastfa835cd2008-06-26 23:03:21 +0200448# 'default' encryption is none -- this only prevents a warning
449$smtp_encryption = '' unless (defined $smtp_encryption);
450
David Brown65648282007-12-25 19:56:29 -0800451# Set CC suppressions
452my(%suppress_cc);
453if (@suppress_cc) {
454 foreach my $entry (@suppress_cc) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100455 die sprintf(__("Unknown --suppress-cc field: '%s'\n"), $entry)
Ævar Arnfjörð Bjarmasone9bf7412010-09-30 13:43:04 +0000456 unless $entry =~ /^(?:all|cccmd|cc|author|self|sob|body|bodycc)$/;
David Brown65648282007-12-25 19:56:29 -0800457 $suppress_cc{$entry} = 1;
458 }
459}
460
461if ($suppress_cc{'all'}) {
Paolo Bonzinicb8a9bd2009-06-18 14:31:32 +0200462 foreach my $entry (qw (cccmd cc author self sob body bodycc)) {
David Brown65648282007-12-25 19:56:29 -0800463 $suppress_cc{$entry} = 1;
464 }
465 delete $suppress_cc{'all'};
466}
467
468# If explicit old-style ones are specified, they trump --suppress-cc.
469$suppress_cc{'self'} = $suppress_from if defined $suppress_from;
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500470$suppress_cc{'sob'} = !$signed_off_by_cc if defined $signed_off_by_cc;
David Brown65648282007-12-25 19:56:29 -0800471
Jay Soffian3531e272009-02-14 23:32:15 -0500472if ($suppress_cc{'body'}) {
473 foreach my $entry (qw (sob bodycc)) {
474 $suppress_cc{$entry} = 1;
475 }
476 delete $suppress_cc{'body'};
477}
478
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500479# Set confirm's default value
480my $confirm_unconfigured = !defined $confirm;
481if ($confirm_unconfigured) {
482 $confirm = scalar %suppress_cc ? 'compose' : 'auto';
483};
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100484die sprintf(__("Unknown --confirm setting: '%s'\n"), $confirm)
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500485 unless $confirm =~ /^(?:auto|cc|compose|always|never)/;
486
David Brown65648282007-12-25 19:56:29 -0800487# Debugging, print out the suppressions.
488if (0) {
489 print "suppressions:\n";
490 foreach my $entry (keys %suppress_cc) {
491 printf " %-5s -> $suppress_cc{$entry}\n", $entry;
492 }
493}
494
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100495my ($repoauthor, $repocommitter);
496($repoauthor) = Git::ident_person(@repo, 'author');
497($repocommitter) = Git::ident_person(@repo, 'committer');
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900498
Jay Soffian50126992009-02-14 23:32:14 -0500499sub parse_address_line {
Matthieu Moybd869f62018-01-05 19:36:51 +0100500 return map { $_->format } Mail::Address->parse($_[0]);
Jay Soffian50126992009-02-14 23:32:14 -0500501}
502
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800503sub split_addrs {
Junio C Hamano2f0e7cb2008-12-21 01:57:59 -0800504 return quotewords('\s*,\s*', 1, @_);
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800505}
506
Eric Wong994d6c62006-05-14 19:13:44 -0700507my %aliases;
Eric Sunshine09f11572015-05-31 18:29:27 -0400508
509sub parse_sendmail_alias {
510 local $_ = shift;
511 if (/"/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100512 printf STDERR __("warning: sendmail alias with quotes is not supported: %s\n"), $_;
Eric Sunshine86b89842015-06-01 14:22:36 -0400513 } elsif (/:include:/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100514 printf STDERR __("warning: `:include:` not supported: %s\n"), $_;
Eric Sunshine86b89842015-06-01 14:22:36 -0400515 } elsif (/[\/|]/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100516 printf STDERR __("warning: `/file` or `|pipe` redirection not supported: %s\n"), $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400517 } elsif (/^(\S+?)\s*:\s*(.+)$/) {
518 my ($alias, $addr) = ($1, $2);
519 $aliases{$alias} = [ split_addrs($addr) ];
520 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100521 printf STDERR __("warning: sendmail line is not recognized: %s\n"), $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400522 }
523}
524
525sub parse_sendmail_aliases {
526 my $fh = shift;
Eric Sunshine2532dd02015-05-31 18:29:29 -0400527 my $s = '';
Eric Sunshine09f11572015-05-31 18:29:27 -0400528 while (<$fh>) {
Eric Sunshine2532dd02015-05-31 18:29:29 -0400529 chomp;
Eric Sunshine020be85f2015-05-31 18:29:28 -0400530 next if /^\s*$/ || /^\s*#/;
Eric Sunshine2532dd02015-05-31 18:29:29 -0400531 $s .= $_, next if $s =~ s/\\$// || s/^\s+//;
532 parse_sendmail_alias($s) if $s;
533 $s = $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400534 }
Eric Sunshine2532dd02015-05-31 18:29:29 -0400535 $s =~ s/\\$//; # silently tolerate stray '\' on last line
536 parse_sendmail_alias($s) if $s;
Eric Sunshine09f11572015-05-31 18:29:27 -0400537}
538
Eric Wong994d6c62006-05-14 19:13:44 -0700539my %parse_alias = (
540 # multiline formats can be supported in the future
541 mutt => sub { my $fh = shift; while (<$fh>) {
Felipe Contrerasffc01f92009-09-30 17:49:36 +0300542 if (/^\s*alias\s+(?:-group\s+\S+\s+)*(\S+)\s+(.*)$/) {
Eric Wong994d6c62006-05-14 19:13:44 -0700543 my ($alias, $addr) = ($1, $2);
544 $addr =~ s/#.*$//; # mutt allows # comments
Eric Wong2c510f22016-01-04 20:53:30 +0000545 # commas delimit multiple addresses
546 my @addr = split_addrs($addr);
547
548 # quotes may be escaped in the file,
549 # unescape them so we do not double-escape them later.
550 s/\\"/"/g foreach @addr;
551 $aliases{$alias} = \@addr
Eric Wong994d6c62006-05-14 19:13:44 -0700552 }}},
553 mailrc => sub { my $fh = shift; while (<$fh>) {
Jeff Kinga277d1e2016-03-17 19:58:22 -0400554 if (/^alias\s+(\S+)\s+(.*?)\s*$/) {
Eric Wong994d6c62006-05-14 19:13:44 -0700555 # spaces delimit multiple addresses
Eric W. Biedermanfe87c922009-05-20 19:45:53 -0700556 $aliases{$1} = [ quotewords('\s+', 0, $2) ];
Eric Wong994d6c62006-05-14 19:13:44 -0700557 }}},
Trent Piepho73c427e2008-11-25 18:55:00 -0800558 pine => sub { my $fh = shift; my $f='\t[^\t]*';
559 for (my $x = ''; defined($x); $x = $_) {
560 chomp $x;
561 $x .= $1 while(defined($_ = <$fh>) && /^ +(.*)$/);
562 $x =~ /^(\S+)$f\t\(?([^\t]+?)\)?(:?$f){0,2}$/ or next;
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800563 $aliases{$1} = [ split_addrs($2) ];
Trent Piepho73c427e2008-11-25 18:55:00 -0800564 }},
Bill Pemberton7613ea32009-04-22 09:41:29 -0400565 elm => sub { my $fh = shift;
566 while (<$fh>) {
567 if (/^(\S+)\s+=\s+[^=]+=\s(\S+)/) {
568 my ($alias, $addr) = ($1, $2);
569 $aliases{$alias} = [ split_addrs($addr) ];
570 }
571 } },
Eric Sunshine09f11572015-05-31 18:29:27 -0400572 sendmail => \&parse_sendmail_aliases,
Eric Wong994d6c62006-05-14 19:13:44 -0700573 gnus => sub { my $fh = shift; while (<$fh>) {
574 if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
575 $aliases{$1} = [ $2 ];
576 }}}
577);
578
Petr Baudis3cb8caf2006-07-03 22:47:58 +0200579if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) {
Eric Wong994d6c62006-05-14 19:13:44 -0700580 foreach my $file (@alias_files) {
581 open my $fh, '<', $file or die "opening $file: $!\n";
582 $parse_alias{$aliasfiletype}->($fh);
583 close $fh;
584 }
585}
586
Jacob Keller17b7a832015-11-19 14:52:11 -0800587if ($dump_aliases) {
588 print "$_\n" for (sort keys %aliases);
589 exit(0);
590}
591
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700592# is_format_patch_arg($f) returns 0 if $f names a patch, or 1 if
593# $f is a revision list specification to be passed to format-patch.
594sub is_format_patch_arg {
Jay Soffianeed6ca72009-02-14 23:32:13 -0500595 return unless $repo;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100596 my $f = shift;
597 try {
598 $repo->command('rev-parse', '--verify', '--quiet', $f);
599 if (defined($format_patch)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100600 return $format_patch;
601 }
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100602 die sprintf(__ <<EOF, $f, $f);
603File '%s' exists but it could also be the range of commits
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100604to produce patches for. Please disambiguate by...
605
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100606 * Saying "./%s" if you mean a file; or
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100607 * Giving --format-patch option if you mean a range.
608EOF
609 } catch Git::Error::Command with {
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700610 # Not a valid revision. Treat it as a filename.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100611 return 0;
612 }
613}
614
Jeff Kingaa548922008-01-18 09:19:36 -0500615# Now that all the defaults are set, process the rest of the command line
616# arguments and collect up the files that need to be processed.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100617my @rev_list_opts;
Junio C Hamano69f4ce52008-11-30 22:38:20 -0800618while (defined(my $f = shift @ARGV)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100619 if ($f eq "--") {
620 push @rev_list_opts, "--", @ARGV;
621 @ARGV = ();
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700622 } elsif (-d $f and !is_format_patch_arg($f)) {
Ævar Arnfjörð Bjarmasonc6038162010-09-30 13:42:54 +0000623 opendir my $dh, $f
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100624 or die sprintf(__("Failed to opendir %s: %s"), $f, $!);
Jeff Kingaa548922008-01-18 09:19:36 -0500625
Ævar Arnfjörð Bjarmason89bf1ba2010-09-14 19:02:24 +0000626 push @files, grep { -f $_ } map { catfile($f, $_) }
Ævar Arnfjörð Bjarmasonc6038162010-09-30 13:42:54 +0000627 sort readdir $dh;
628 closedir $dh;
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700629 } elsif ((-f $f or -p $f) and !is_format_patch_arg($f)) {
Jeff Kingaa548922008-01-18 09:19:36 -0500630 push @files, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500631 } else {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100632 push @rev_list_opts, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500633 }
634}
635
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100636if (@rev_list_opts) {
Vasco Almeida46493102016-12-14 11:54:36 -0100637 die __("Cannot run git format-patch from outside a repository\n")
Jay Soffianeed6ca72009-02-14 23:32:13 -0500638 unless $repo;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100639 push @files, $repo->command('format-patch', '-o', tempdir(CLEANUP => 1), @rev_list_opts);
640}
641
Junio C Hamano531220b2016-03-17 22:40:05 -0700642@files = handle_backup_files(@files);
643
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500644if ($validate) {
Jeff Kingc764a0c2008-01-18 09:20:10 -0500645 foreach my $f (@files) {
Kevin Ballard300913b2008-06-25 15:44:40 -0700646 unless (-p $f) {
brian m. carlsonf2d06fb2018-07-08 22:17:11 +0000647 my $error = validate_patch($f, $target_xfer_encoding);
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100648 $error and die sprintf(__("fatal: %s: %s\nwarning: no patches were sent\n"),
649 $f, $error);
Kevin Ballard300913b2008-06-25 15:44:40 -0700650 }
Jeff Kingc764a0c2008-01-18 09:20:10 -0500651 }
Jeff King747bbff2008-01-18 09:19:48 -0500652}
653
Jeff Kingaa548922008-01-18 09:19:36 -0500654if (@files) {
655 unless ($quiet) {
656 print $_,"\n" for (@files);
657 }
658} else {
Vasco Almeida46493102016-12-14 11:54:36 -0100659 print STDERR __("\nNo patch files specified!\n\n");
Jeff Kingaa548922008-01-18 09:19:36 -0500660 usage();
661}
662
Ævar Arnfjörð Bjarmasonacf071b2010-09-30 13:42:57 +0000663sub get_patch_subject {
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100664 my $fn = shift;
665 open (my $fh, '<', $fn);
666 while (my $line = <$fh>) {
667 next unless ($line =~ /^Subject: (.*)$/);
668 close $fh;
669 return "GIT: $1\n";
670 }
671 close $fh;
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100672 die sprintf(__("No subject line in %s?"), $fn);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100673}
674
675if ($compose) {
676 # Note that this does not need to be secure, but we will make a small
677 # effort to have it be unique
Jay Soffianafe756c2009-02-23 13:51:37 -0500678 $compose_filename = ($repo ?
679 tempfile(".gitsendemail.msg.XXXXXX", DIR => $repo->repo_path()) :
680 tempfile(".gitsendemail.msg.XXXXXX", DIR => "."))[1];
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000681 open my $c, ">", $compose_filename
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100682 or die sprintf(__("Failed to open for writing %s: %s"), $compose_filename, $!);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100683
684
685 my $tpl_sender = $sender || $repoauthor || $repocommitter || '';
686 my $tpl_subject = $initial_subject || '';
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100687 my $tpl_in_reply_to = $initial_in_reply_to || '';
Christian Ludwigd11c9432018-03-04 00:58:14 +0100688 my $tpl_reply_to = $reply_to || '';
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100689
Vasco Almeida70aedfb2016-12-14 11:54:38 -0100690 print $c <<EOT1, Git::prefix_lines("GIT: ", __ <<EOT2), <<EOT3;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100691From $tpl_sender # This line is ignored.
Vasco Almeida70aedfb2016-12-14 11:54:38 -0100692EOT1
693Lines beginning in "GIT:" will be removed.
694Consider including an overall diffstat or table of contents
695for the patch you are writing.
696
697Clear the body content if you don't wish to send a summary.
698EOT2
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100699From: $tpl_sender
Christian Ludwigd11c9432018-03-04 00:58:14 +0100700Reply-To: $tpl_reply_to
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100701Subject: $tpl_subject
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100702In-Reply-To: $tpl_in_reply_to
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100703
Vasco Almeida70aedfb2016-12-14 11:54:38 -0100704EOT3
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100705 for my $f (@files) {
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000706 print $c get_patch_subject($f);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100707 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000708 close $c;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100709
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100710 if ($annotate) {
711 do_edit($compose_filename, @files);
712 } else {
713 do_edit($compose_filename);
714 }
715
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000716 open $c, "<", $compose_filename
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100717 or die sprintf(__("Failed to open %s: %s"), $compose_filename, $!);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100718
Krzysztof Mazur4a47a4d2012-10-22 14:41:48 +0200719 if (!defined $compose_encoding) {
720 $compose_encoding = "UTF-8";
721 }
Nathan Payreb6049542017-12-15 16:33:39 +0100722
723 my %parsed_email;
724 while (my $line = <$c>) {
725 next if $line =~ m/^GIT:/;
726 parse_header_line($line, \%parsed_email);
727 if ($line =~ /^$/) {
728 $parsed_email{'body'} = filter_body($c);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100729 }
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100730 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000731 close $c;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100732
Nathan Payreb6049542017-12-15 16:33:39 +0100733 open my $c2, ">", $compose_filename . ".final"
734 or die sprintf(__("Failed to open %s.final: %s"), $compose_filename, $!);
735
736
737 if ($parsed_email{'From'}) {
738 $sender = delete($parsed_email{'From'});
739 }
740 if ($parsed_email{'In-Reply-To'}) {
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100741 $initial_in_reply_to = delete($parsed_email{'In-Reply-To'});
Nathan Payreb6049542017-12-15 16:33:39 +0100742 }
Christian Ludwigd11c9432018-03-04 00:58:14 +0100743 if ($parsed_email{'Reply-To'}) {
744 $reply_to = delete($parsed_email{'Reply-To'});
Nathan Payreb6049542017-12-15 16:33:39 +0100745 }
746 if ($parsed_email{'Subject'}) {
747 $initial_subject = delete($parsed_email{'Subject'});
748 print $c2 "Subject: " .
749 quote_subject($initial_subject, $compose_encoding) .
750 "\n";
751 }
752
753 if ($parsed_email{'MIME-Version'}) {
754 print $c2 "MIME-Version: $parsed_email{'MIME-Version'}\n",
755 "Content-Type: $parsed_email{'Content-Type'};\n",
756 "Content-Transfer-Encoding: $parsed_email{'Content-Transfer-Encoding'}\n";
757 delete($parsed_email{'MIME-Version'});
758 delete($parsed_email{'Content-Type'});
759 delete($parsed_email{'Content-Transfer-Encoding'});
760 } elsif (file_has_nonascii($compose_filename)) {
761 my $content_type = (delete($parsed_email{'Content-Type'}) or
762 "text/plain; charset=$compose_encoding");
763 print $c2 "MIME-Version: 1.0\n",
764 "Content-Type: $content_type\n",
765 "Content-Transfer-Encoding: 8bit\n";
766 }
767 # Preserve unknown headers
768 foreach my $key (keys %parsed_email) {
769 next if $key eq 'body';
770 print $c2 "$key: $parsed_email{$key}";
771 }
772
773 if ($parsed_email{'body'}) {
774 print $c2 "\n$parsed_email{'body'}\n";
775 delete($parsed_email{'body'});
776 } else {
Vasco Almeida46493102016-12-14 11:54:36 -0100777 print __("Summary email is empty, skipping it\n");
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100778 $compose = -1;
779 }
Nathan Payreb6049542017-12-15 16:33:39 +0100780
781 close $c2;
782
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100783} elsif ($annotate) {
784 do_edit(@files);
785}
786
Jay Soffian6e182512009-03-28 21:39:10 -0400787sub ask {
788 my ($prompt, %arg) = @_;
Jay Soffian0da43a62009-04-04 23:23:21 -0400789 my $valid_re = $arg{valid_re};
Jay Soffian6e182512009-03-28 21:39:10 -0400790 my $default = $arg{default};
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700791 my $confirm_only = $arg{confirm_only};
Jay Soffian6e182512009-03-28 21:39:10 -0400792 my $resp;
793 my $i = 0;
Jay Soffian5906f542009-03-31 12:22:11 -0400794 return defined $default ? $default : undef
795 unless defined $term->IN and defined fileno($term->IN) and
796 defined $term->OUT and defined fileno($term->OUT);
Jay Soffian6e182512009-03-28 21:39:10 -0400797 while ($i++ < 10) {
798 $resp = $term->readline($prompt);
799 if (!defined $resp) { # EOF
800 print "\n";
801 return defined $default ? $default : undef;
802 }
803 if ($resp eq '' and defined $default) {
804 return $default;
805 }
Jay Soffian0da43a62009-04-04 23:23:21 -0400806 if (!defined $valid_re or $resp =~ /$valid_re/) {
Jay Soffian6e182512009-03-28 21:39:10 -0400807 return $resp;
808 }
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700809 if ($confirm_only) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100810 my $yesno = $term->readline(
811 # TRANSLATORS: please keep [y/N] as is.
812 sprintf(__("Are you sure you want to use <%s> [y/N]? "), $resp));
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700813 if (defined $yesno && $yesno =~ /y/i) {
814 return $resp;
815 }
816 }
Jay Soffian6e182512009-03-28 21:39:10 -0400817 }
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -0700818 return;
Jay Soffian6e182512009-03-28 21:39:10 -0400819}
820
Nathan Payreb6049542017-12-15 16:33:39 +0100821sub parse_header_line {
822 my $lines = shift;
823 my $parsed_line = shift;
824 my $addr_pat = join "|", qw(To Cc Bcc);
825
826 foreach (split(/\n/, $lines)) {
827 if (/^($addr_pat):\s*(.+)$/i) {
828 $parsed_line->{$1} = [ parse_address_line($2) ];
829 } elsif (/^([^:]*):\s*(.+)\s*$/i) {
830 $parsed_line->{$1} = $2;
831 }
832 }
833}
834
835sub filter_body {
836 my $c = shift;
837 my $body = "";
838 while (my $body_line = <$c>) {
839 if ($body_line !~ m/^GIT:/) {
840 $body .= $body_line;
841 }
842 }
843 return $body;
844}
845
846
Thomas Rast3cae7e52010-06-17 22:10:39 +0200847my %broken_encoding;
848
Ævar Arnfjörð Bjarmason1d50bfd2010-09-30 13:42:58 +0000849sub file_declares_8bit_cte {
Thomas Rast3cae7e52010-06-17 22:10:39 +0200850 my $fn = shift;
851 open (my $fh, '<', $fn);
852 while (my $line = <$fh>) {
853 last if ($line =~ /^$/);
854 return 1 if ($line =~ /^Content-Transfer-Encoding: .*8bit.*$/);
855 }
856 close $fh;
857 return 0;
858}
859
860foreach my $f (@files) {
861 next unless (body_or_subject_has_nonascii($f)
862 && !file_declares_8bit_cte($f));
863 $broken_encoding{$f} = 1;
864}
865
866if (!defined $auto_8bit_encoding && scalar %broken_encoding) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100867 print __("The following files are 8bit, but do not declare " .
868 "a Content-Transfer-Encoding.\n");
Thomas Rast3cae7e52010-06-17 22:10:39 +0200869 foreach my $f (sort keys %broken_encoding) {
870 print " $f\n";
871 }
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100872 $auto_8bit_encoding = ask(__("Which 8bit encoding should I declare [UTF-8]? "),
Junio C Hamano852a15d2015-02-13 12:20:25 -0800873 valid_re => qr/.{4}/, confirm_only => 1,
Thomas Rast3cae7e52010-06-17 22:10:39 +0200874 default => "UTF-8");
875}
876
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200877if (!$force) {
878 for my $f (@files) {
Ævar Arnfjörð Bjarmason0d290a42010-09-30 13:43:01 +0000879 if (get_patch_subject($f) =~ /\Q*** SUBJECT HERE ***\E/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100880 die sprintf(__("Refusing to send because the patch\n\t%s\n"
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200881 . "has the template subject '*** SUBJECT HERE ***'. "
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100882 . "Pass --force if you really want to send.\n"), $f);
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200883 }
884 }
885}
886
Remi Lespinetc46e27a2015-06-30 14:16:47 +0200887if (defined $sender) {
Remi Lespinetfa5b1aa2015-06-30 14:16:51 +0200888 $sender =~ s/^\s+|\s+$//g;
Remi Lespinetc46e27a2015-06-30 14:16:47 +0200889 ($sender) = expand_aliases($sender);
890} else {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100891 $sender = $repoauthor || $repocommitter || '';
Ryan Anderson83b24432005-07-31 04:17:25 -0400892}
893
Michael S. Tsirkinda187592013-06-05 21:11:00 +0300894# $sender could be an already sanitized address
895# (e.g. sendemail.from could be manually sanitized by user).
896# But it's a no-op to run sanitize_address on an already sanitized address.
897$sender = sanitize_address($sender);
898
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100899my $to_whom = __("To whom should the emails be sent (if anyone)?");
Felipe Contreras8cac13d2012-11-24 12:16:19 +0100900my $prompting = 0;
Junio C Hamano8796ff72010-10-26 22:02:03 -0700901if (!@initial_to && !defined $to_cmd) {
Junio C Hamano0d6b21e2016-04-24 12:31:44 -0700902 my $to = ask("$to_whom ",
Stephen Boyd61837492012-09-06 11:31:11 -0700903 default => "",
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700904 valid_re => qr/\@.*\./, confirm_only => 1);
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700905 push @initial_to, parse_address_line($to) if defined $to; # sanitized/validated later
Ryan Anderson1f038a02005-09-05 01:13:07 -0400906 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -0400907}
908
Eric Wong994d6c62006-05-14 19:13:44 -0700909sub expand_aliases {
Jeff King302e04e2009-07-23 07:09:29 -0400910 return map { expand_one_alias($_) } @_;
911}
912
913my %EXPANDED_ALIASES;
914sub expand_one_alias {
915 my $alias = shift;
916 if ($EXPANDED_ALIASES{$alias}) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100917 die sprintf(__("fatal: alias '%s' expands to itself\n"), $alias);
Jeff King302e04e2009-07-23 07:09:29 -0400918 }
919 local $EXPANDED_ALIASES{$alias} = 1;
920 return $aliases{$alias} ? expand_aliases(@{$aliases{$alias}}) : $alias;
Eric Wong994d6c62006-05-14 19:13:44 -0700921}
922
Remi Lespinetb5e112d2015-06-30 14:16:45 +0200923@initial_to = process_address_list(@initial_to);
924@initial_cc = process_address_list(@initial_cc);
Ævar Arnfjörð Bjarmasone60f6d12019-05-09 13:48:29 +0200925@initial_bcc = process_address_list(@initial_bcc);
Eric Wong994d6c62006-05-14 19:13:44 -0700926
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100927if ($thread && !defined $initial_in_reply_to && $prompting) {
928 $initial_in_reply_to = ask(
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100929 __("Message-ID to be used as In-Reply-To for the first email (if any)? "),
Stephen Boyd61837492012-09-06 11:31:11 -0700930 default => "",
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700931 valid_re => qr/\@.*\./, confirm_only => 1);
Ryan Anderson83b24432005-07-31 04:17:25 -0400932}
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100933if (defined $initial_in_reply_to) {
934 $initial_in_reply_to =~ s/^\s*<?//;
935 $initial_in_reply_to =~ s/>?\s*$//;
936 $initial_in_reply_to = "<$initial_in_reply_to>" if $initial_in_reply_to ne '';
Junio C Hamanoace9c2a2007-12-10 21:44:42 -0800937}
Mike Hommeyace72082007-12-09 18:17:28 +0100938
Christian Ludwigd11c9432018-03-04 00:58:14 +0100939if (defined $reply_to) {
940 $reply_to =~ s/^\s+|\s+$//g;
941 ($reply_to) = expand_aliases($reply_to);
942 $reply_to = sanitize_address($reply_to);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900943}
Eric Wongaca7ad72006-05-15 02:34:44 -0700944
945if (!defined $smtp_server) {
Florian Klink1ab2fd42017-11-28 01:49:04 +0100946 my @sendmail_paths = qw( /usr/sbin/sendmail /usr/lib/sendmail );
947 push @sendmail_paths, map {"$_/sendmail"} split /:/, $ENV{PATH};
948 foreach (@sendmail_paths) {
Eric Wongaca7ad72006-05-15 02:34:44 -0700949 if (-x $_) {
950 $smtp_server = $_;
951 last;
952 }
953 }
954 $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
Ryan Anderson3342d852005-07-31 20:04:24 -0400955}
956
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500957if ($compose && $compose > 0) {
958 @files = ($compose_filename . ".final", @files);
Ryan Anderson1f038a02005-09-05 01:13:07 -0400959}
960
Ryan Anderson83b24432005-07-31 04:17:25 -0400961# Variables we set as part of the loop over files
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100962our ($message_id, %mail, $subject, $in_reply_to, $references, $message,
Jay Soffiandc1460a2009-03-31 12:22:12 -0400963 $needs_confirm, $message_num, $ask_default);
Ryan Anderson83b24432005-07-31 04:17:25 -0400964
Eric Wong567ffeb2006-03-25 16:47:12 -0800965sub extract_valid_address {
966 my $address = shift;
Ævar Arnfjörð Bjarmason35b6ab92010-09-30 19:03:31 +0000967 my $local_part_regexp = qr/[^<>"\s@]+/;
968 my $domain_regexp = qr/[^.<>"\s@]+(?:\.[^.<>"\s@]+)+/;
Eric Wongdb3106b2006-05-15 02:41:01 -0700969
970 # check for a local address:
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700971 return $address if ($address =~ /^($local_part_regexp)$/);
Eric Wongdb3106b2006-05-15 02:41:01 -0700972
Uwe Kleine-König155197e2007-08-09 15:27:57 +0200973 $address =~ s/^\s*<(.*)>\s*$/$1/;
Eric Wong567ffeb2006-03-25 16:47:12 -0800974 if ($have_email_valid) {
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700975 return scalar Email::Valid->address($address);
Eric Wong567ffeb2006-03-25 16:47:12 -0800976 }
Krzysztof Mazur95c0d4b2012-11-22 19:12:09 +0100977
978 # less robust/correct than the monster regexp in Email::Valid,
979 # but still does a 99% job, and one less dependency
980 return $1 if $address =~ /($local_part_regexp\@$domain_regexp)/;
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -0700981 return;
Eric Wong567ffeb2006-03-25 16:47:12 -0800982}
Ryan Anderson83b24432005-07-31 04:17:25 -0400983
Krzysztof Mazure4312252012-11-22 19:12:10 +0100984sub extract_valid_address_or_die {
985 my $address = shift;
986 $address = extract_valid_address($address);
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100987 die sprintf(__("error: unable to extract a valid address from: %s\n"), $address)
Krzysztof Mazure4312252012-11-22 19:12:10 +0100988 if !$address;
989 return $address;
990}
991
992sub validate_address {
993 my $address = shift;
Krzysztof Mazurd0e98102012-11-22 19:12:12 +0100994 while (!extract_valid_address($address)) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100995 printf STDERR __("error: unable to extract a valid address from: %s\n"), $address;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100996 # TRANSLATORS: Make sure to include [q] [d] [e] in your
997 # translation. The program will only accept English input
998 # at this point.
999 $_ = ask(__("What to do with this address? ([q]uit|[d]rop|[e]dit): "),
Krzysztof Mazurd0e98102012-11-22 19:12:12 +01001000 valid_re => qr/^(?:quit|q|drop|d|edit|e)/i,
Krzysztof Mazur5c80afe2012-11-22 19:12:11 +01001001 default => 'q');
1002 if (/^d/i) {
1003 return undef;
1004 } elsif (/^q/i) {
1005 cleanup_compose_files();
1006 exit(0);
1007 }
Junio C Hamano0d6b21e2016-04-24 12:31:44 -07001008 $address = ask("$to_whom ",
Krzysztof Mazurd0e98102012-11-22 19:12:12 +01001009 default => "",
1010 valid_re => qr/\@.*\./, confirm_only => 1);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001011 }
1012 return $address;
1013}
1014
1015sub validate_address_list {
1016 return (grep { defined $_ }
1017 map { validate_address($_) } @_);
Ryan Anderson83b24432005-07-31 04:17:25 -04001018}
1019
1020# Usually don't need to change anything below here.
1021
1022# we make a "fake" message id by taking the current number
1023# of seconds since the beginning of Unix time and tacking on
1024# a random number to the end, in case we are called quicker than
1025# 1 second since the last time we were called.
Ryan Anderson8037d1a2005-07-31 20:04:24 -04001026
1027# We'll setup a template for the message id, using the "from" address:
Ryan Anderson8037d1a2005-07-31 20:04:24 -04001028
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001029my ($message_id_stamp, $message_id_serial);
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001030sub make_message_id {
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001031 my $uniq;
1032 if (!defined $message_id_stamp) {
Eric Wongf916ab02016-04-06 20:07:14 +00001033 $message_id_stamp = strftime("%Y%m%d%H%M%S.$$", gmtime(time));
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001034 $message_id_serial = 0;
1035 }
1036 $message_id_serial++;
1037 $uniq = "$message_id_stamp-$message_id_serial";
1038
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001039 my $du_part;
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001040 for ($sender, $repocommitter, $repoauthor) {
1041 $du_part = extract_valid_address(sanitize_address($_));
1042 last if (defined $du_part and $du_part ne '');
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001043 }
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001044 if (not defined $du_part or $du_part eq '') {
Ævar Arnfjörð Bjarmason529dd382010-09-30 13:43:08 +00001045 require Sys::Hostname;
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001046 $du_part = 'user@' . Sys::Hostname::hostname();
1047 }
Eric Wongf916ab02016-04-06 20:07:14 +00001048 my $message_id_template = "<%s-%s>";
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001049 $message_id = sprintf($message_id_template, $uniq, $du_part);
Ryan Anderson8037d1a2005-07-31 20:04:24 -04001050 #print "new message id = $message_id\n"; # Was useful for debugging
Ryan Anderson83b24432005-07-31 04:17:25 -04001051}
1052
1053
1054
Eric Wonga5370b12006-03-25 03:01:01 -08001055$time = time - scalar $#files;
Ryan Anderson83b24432005-07-31 04:17:25 -04001056
Jürgen Rühle374c5902007-01-10 13:36:39 -08001057sub unquote_rfc2047 {
1058 local ($_) = @_;
Роман Донченко11f70a72014-12-14 18:59:46 +03001059 my $charset;
Роман Донченкоab47e2a2014-12-14 18:59:47 +03001060 my $sep = qr/[ \t]+/;
1061 s{$re_encoded_word(?:$sep$re_encoded_word)*}{
1062 my @words = split $sep, $&;
1063 foreach (@words) {
1064 m/$re_encoded_word/;
1065 $charset = $1;
1066 my $encoding = $2;
1067 my $text = $3;
1068 if ($encoding eq 'q' || $encoding eq 'Q') {
1069 $_ = $text;
1070 s/_/ /g;
1071 s/=([0-9A-F]{2})/chr(hex($1))/egi;
1072 } else {
1073 # other encodings not supported yet
1074 }
Роман Донченко11f70a72014-12-14 18:59:46 +03001075 }
Роман Донченкоab47e2a2014-12-14 18:59:47 +03001076 join '', @words;
Thomas Rastb622d4d2012-07-30 21:25:40 +02001077 }eg;
Роман Донченко11f70a72014-12-14 18:59:46 +03001078 return wantarray ? ($_, $charset) : $_;
Jürgen Rühle374c5902007-01-10 13:36:39 -08001079}
1080
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001081sub quote_rfc2047 {
1082 local $_ = shift;
Brandon Caseyd1fff6f2009-06-06 20:12:31 -05001083 my $encoding = shift || 'UTF-8';
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001084 s/([^-a-zA-Z0-9!*+\/])/sprintf("=%02X", ord($1))/eg;
1085 s/(.*)/=\?$encoding\?q\?$1\?=/;
1086 return $_;
1087}
1088
Brandon Caseya3a82622009-06-07 19:25:58 -05001089sub is_rfc2047_quoted {
1090 my $s = shift;
Brandon Caseya3a82622009-06-07 19:25:58 -05001091 length($s) <= 75 &&
Роман Донченко11f70a72014-12-14 18:59:46 +03001092 $s =~ m/^(?:"[[:ascii:]]*"|$re_encoded_word)$/o;
Brandon Caseya3a82622009-06-07 19:25:58 -05001093}
1094
Krzysztof Mazurce547802012-10-24 23:08:26 +02001095sub subject_needs_rfc2047_quoting {
1096 my $s = shift;
1097
Krzysztof Mazurce1459f2012-10-24 23:28:29 +02001098 return ($s =~ /[^[:ascii:]]/) || ($s =~ /=\?/);
Krzysztof Mazurce547802012-10-24 23:08:26 +02001099}
1100
1101sub quote_subject {
1102 local $subject = shift;
1103 my $encoding = shift || 'UTF-8';
1104
1105 if (subject_needs_rfc2047_quoting($subject)) {
1106 return quote_rfc2047($subject, $encoding);
1107 }
1108 return $subject;
1109}
1110
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001111# use the simplest quoting being able to handle the recipient
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001112sub sanitize_address {
Robin H. Johnson732263d2007-04-25 19:37:19 -07001113 my ($recipient) = @_;
Krzysztof Mazur831a4882012-11-22 19:12:08 +01001114
1115 # remove garbage after email address
1116 $recipient =~ s/(.*>).*$/$1/;
1117
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001118 my ($recipient_name, $recipient_addr) = ($recipient =~ /^(.*?)\s*(<.*)/);
1119
1120 if (not $recipient_name) {
Ævar Arnfjörð Bjarmasonff483892010-09-30 13:43:02 +00001121 return $recipient;
Robin H. Johnson732263d2007-04-25 19:37:19 -07001122 }
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001123
1124 # if recipient_name is already quoted, do nothing
Brandon Caseya3a82622009-06-07 19:25:58 -05001125 if (is_rfc2047_quoted($recipient_name)) {
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001126 return $recipient;
1127 }
1128
Remi Lespinet1fe97032015-06-30 14:16:49 +02001129 # remove non-escaped quotes
1130 $recipient_name =~ s/(^|[^\\])"/$1/g;
1131
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001132 # rfc2047 is needed if a non-ascii char is included
1133 if ($recipient_name =~ /[^[:ascii:]]/) {
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001134 $recipient_name = quote_rfc2047($recipient_name);
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001135 }
1136
1137 # double quotes are needed if specials or CTLs are included
1138 elsif ($recipient_name =~ /[][()<>@,;:\\".\000-\037\177]/) {
Remi Lespinet1fe97032015-06-30 14:16:49 +02001139 $recipient_name =~ s/([\\\r])/\\$1/g;
Ævar Arnfjörð Bjarmasond5c7d692010-09-30 13:43:03 +00001140 $recipient_name = qq["$recipient_name"];
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001141 }
1142
1143 return "$recipient_name $recipient_addr";
1144
Robin H. Johnson732263d2007-04-25 19:37:19 -07001145}
1146
Matthieu Moycb2922f2017-08-23 12:21:01 +02001147sub strip_garbage_one_address {
1148 my ($addr) = @_;
1149 chomp $addr;
1150 if ($addr =~ /^(("[^"]*"|[^"<]*)? *<[^>]*>).*/) {
1151 # "Foo Bar" <foobar@example.com> [possibly garbage here]
1152 # Foo Bar <foobar@example.com> [possibly garbage here]
1153 return $1;
1154 }
1155 if ($addr =~ /^(<[^>]*>).*/) {
1156 # <foo@example.com> [possibly garbage here]
1157 # if garbage contains other addresses, they are ignored.
1158 return $1;
1159 }
1160 if ($addr =~ /^([^"#,\s]*)/) {
1161 # address without quoting: remove anything after the address
1162 return $1;
1163 }
1164 return $addr;
1165}
1166
Krzysztof Mazure4312252012-11-22 19:12:10 +01001167sub sanitize_address_list {
1168 return (map { sanitize_address($_) } @_);
1169}
1170
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001171sub process_address_list {
Remi Lespinetb1c8a112015-06-30 14:16:50 +02001172 my @addr_list = map { parse_address_line($_) } @_;
1173 @addr_list = expand_aliases(@addr_list);
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001174 @addr_list = sanitize_address_list(@addr_list);
1175 @addr_list = validate_address_list(@addr_list);
1176 return @addr_list;
1177}
1178
Jari Aalto134550f2010-03-14 17:16:45 +02001179# Returns the local Fully Qualified Domain Name (FQDN) if available.
1180#
1181# Tightly configured MTAa require that a caller sends a real DNS
1182# domain name that corresponds the IP address in the HELO/EHLO
1183# handshake. This is used to verify the connection and prevent
1184# spammers from trying to hide their identity. If the DNS and IP don't
1185# match, the receiveing MTA may deny the connection.
1186#
1187# Here is a deny example of Net::SMTP with the default "localhost.localdomain"
1188#
1189# Net::SMTP=GLOB(0x267ec28)>>> EHLO localhost.localdomain
1190# Net::SMTP=GLOB(0x267ec28)<<< 550 EHLO argument does not match calling host
1191#
1192# This maildomain*() code is based on ideas in Perl library Test::Reporter
1193# /usr/share/perl5/Test/Reporter/Mail/Util.pm ==> sub _maildomain ()
1194
Brian Gernhardt59a86302010-04-10 10:53:54 -04001195sub valid_fqdn {
1196 my $domain = shift;
Brandon Casey61ef5e92010-09-26 22:18:01 -05001197 return defined $domain && !($^O eq 'darwin' && $domain =~ /\.local$/) && $domain =~ /\./;
Brian Gernhardt59a86302010-04-10 10:53:54 -04001198}
1199
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001200sub maildomain_net {
Jari Aalto134550f2010-03-14 17:16:45 +02001201 my $maildomain;
1202
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001203 my $domain = Net::Domain::domainname();
1204 $maildomain = $domain if valid_fqdn($domain);
Jari Aalto134550f2010-03-14 17:16:45 +02001205
1206 return $maildomain;
1207}
1208
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001209sub maildomain_mta {
Jari Aalto134550f2010-03-14 17:16:45 +02001210 my $maildomain;
1211
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001212 for my $host (qw(mailhost localhost)) {
1213 my $smtp = Net::SMTP->new($host);
1214 if (defined $smtp) {
1215 my $domain = $smtp->domain;
1216 $smtp->quit;
Jari Aalto134550f2010-03-14 17:16:45 +02001217
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001218 $maildomain = $domain if valid_fqdn($domain);
Jari Aalto134550f2010-03-14 17:16:45 +02001219
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001220 last if $maildomain;
Jari Aalto134550f2010-03-14 17:16:45 +02001221 }
1222 }
1223
1224 return $maildomain;
1225}
1226
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001227sub maildomain {
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001228 return maildomain_net() || maildomain_mta() || 'localhost.localdomain';
Jari Aalto134550f2010-03-14 17:16:45 +02001229}
1230
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001231sub smtp_host_string {
1232 if (defined $smtp_server_port) {
1233 return "$smtp_server:$smtp_server_port";
1234 } else {
1235 return $smtp_server;
1236 }
1237}
1238
1239# Returns 1 if authentication succeeded or was not necessary
1240# (smtp_user was not specified), and 0 otherwise.
1241
1242sub smtp_auth_maybe {
1243 if (!defined $smtp_authuser || $auth) {
1244 return 1;
1245 }
1246
1247 # Workaround AUTH PLAIN/LOGIN interaction defect
1248 # with Authen::SASL::Cyrus
1249 eval {
1250 require Authen::SASL;
1251 Authen::SASL->import(qw(Perl));
1252 };
1253
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001254 # Check mechanism naming as defined in:
1255 # https://tools.ietf.org/html/rfc4422#page-8
Brian Norris904f6e72015-09-18 15:12:50 -07001256 if ($smtp_auth && $smtp_auth !~ /^(\b[A-Z0-9-_]{1,20}\s*)*$/) {
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001257 die "invalid smtp auth: '${smtp_auth}'";
1258 }
1259
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001260 # TODO: Authentication may fail not because credentials were
1261 # invalid but due to other reasons, in which we should not
1262 # reject credentials.
1263 $auth = Git::credential({
1264 'protocol' => 'smtp',
1265 'host' => smtp_host_string(),
1266 'username' => $smtp_authuser,
1267 # if there's no password, "git credential fill" will
1268 # give us one, otherwise it'll just pass this one.
1269 'password' => $smtp_authpass
1270 }, sub {
1271 my $cred = shift;
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001272
1273 if ($smtp_auth) {
1274 my $sasl = Authen::SASL->new(
1275 mechanism => $smtp_auth,
1276 callback => {
1277 user => $cred->{'username'},
1278 pass => $cred->{'password'},
1279 authname => $cred->{'username'},
1280 }
1281 );
1282
1283 return !!$smtp->auth($sasl);
1284 }
1285
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001286 return !!$smtp->auth($cred->{'username'}, $cred->{'password'});
1287 });
1288
1289 return $auth;
1290}
1291
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001292sub ssl_verify_params {
1293 eval {
1294 require IO::Socket::SSL;
1295 IO::Socket::SSL->import(qw/SSL_VERIFY_PEER SSL_VERIFY_NONE/);
1296 };
1297 if ($@) {
1298 print STDERR "Not using SSL_VERIFY_PEER due to out-of-date IO::Socket::SSL.\n";
1299 return;
1300 }
1301
1302 if (!defined $smtp_ssl_cert_path) {
Ruben Kerkhof01645b72014-01-15 21:31:11 +04001303 # use the OpenSSL defaults
1304 return (SSL_verify_mode => SSL_VERIFY_PEER());
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001305 }
1306
1307 if ($smtp_ssl_cert_path eq "") {
1308 return (SSL_verify_mode => SSL_VERIFY_NONE());
1309 } elsif (-d $smtp_ssl_cert_path) {
1310 return (SSL_verify_mode => SSL_VERIFY_PEER(),
1311 SSL_ca_path => $smtp_ssl_cert_path);
1312 } elsif (-f $smtp_ssl_cert_path) {
1313 return (SSL_verify_mode => SSL_VERIFY_PEER(),
1314 SSL_ca_file => $smtp_ssl_cert_path);
1315 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001316 die sprintf(__("CA path \"%s\" does not exist"), $smtp_ssl_cert_path);
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001317 }
1318}
1319
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001320sub file_name_is_absolute {
1321 my ($path) = @_;
1322
1323 # msys does not grok DOS drive-prefixes
1324 if ($^O eq 'msys') {
Junio C Hamanof24ecf52014-04-23 09:37:38 -07001325 return ($path =~ m#^/# || $path =~ m#^[a-zA-Z]\:#)
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001326 }
1327
1328 require File::Spec::Functions;
1329 return File::Spec::Functions::file_name_is_absolute($path);
1330}
1331
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001332# Prepares the email, then asks the user what to do.
1333#
1334# If the user chooses to send the email, it's sent and 1 is returned.
1335# If the user chooses not to send the email, 0 is returned.
1336# If the user decides they want to make further edits, -1 is returned and the
1337# caller is expected to call send_message again after the edits are performed.
1338#
1339# If an error occurs sending the email, this just dies.
Michael Witten15da1082009-04-13 13:23:51 -05001340
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001341sub send_message {
Eric Wong4bc87a22006-03-25 17:20:48 -08001342 my @recipients = unique_email_list(@to);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001343 @cc = (grep { my $cc = extract_valid_address_or_die($_);
Joe Perches83acaae2010-11-20 15:06:05 -08001344 not grep { $cc eq $_ || $_ =~ /<\Q${cc}\E>$/ } @recipients
Ask Bjørn Hansen7ac17522007-11-19 03:00:26 -08001345 }
Ask Bjørn Hansen7ac17522007-11-19 03:00:26 -08001346 @cc);
Eric Wong4bc87a22006-03-25 17:20:48 -08001347 my $to = join (",\n\t", @recipients);
Ævar Arnfjörð Bjarmasone60f6d12019-05-09 13:48:29 +02001348 @recipients = unique_email_list(@recipients,@cc,@initial_bcc);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001349 @recipients = (map { extract_valid_address_or_die($_) } @recipients);
Jakub Narebski6bdca892006-07-07 20:57:55 +02001350 my $date = format_2822_time($time++);
Martin Langhoffe923eff2006-05-03 09:44:36 +12001351 my $gitversion = '@@GIT_VERSION@@';
1352 if ($gitversion =~ m/..GIT_VERSION../) {
Petr Baudis3cb8caf2006-07-03 22:47:58 +02001353 $gitversion = Git::version();
Martin Langhoffe923eff2006-05-03 09:44:36 +12001354 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001355
Joe Perches02461e02009-10-08 10:03:26 -07001356 my $cc = join(",\n\t", unique_email_list(@cc));
Junio C Hamanof06a6a42007-04-16 16:51:47 -07001357 my $ccline = "";
1358 if ($cc ne '') {
1359 $ccline = "\nCc: $cc";
1360 }
Jeff King4f3d3702007-12-17 15:51:34 -05001361 make_message_id() unless defined($message_id);
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001362
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001363 my $header = "From: $sender
Junio C Hamanof06a6a42007-04-16 16:51:47 -07001364To: $to${ccline}
Eric Wong4bc87a22006-03-25 17:20:48 -08001365Subject: $subject
Eric Wong4bc87a22006-03-25 17:20:48 -08001366Date: $date
1367Message-Id: $message_id
Eric Wong4bc87a22006-03-25 17:20:48 -08001368";
Luis Henriquesac1596a2014-03-24 21:38:27 +00001369 if ($use_xmailer) {
1370 $header .= "X-Mailer: git-send-email $gitversion\n";
1371 }
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001372 if ($in_reply_to) {
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001373
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001374 $header .= "In-Reply-To: $in_reply_to\n";
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001375 $header .= "References: $references\n";
1376 }
Christian Ludwigd11c9432018-03-04 00:58:14 +01001377 if ($reply_to) {
1378 $header .= "Reply-To: $reply_to\n";
1379 }
Junio C Hamanoce91c2f2006-10-05 16:36:49 -07001380 if (@xh) {
1381 $header .= join("\n", @xh) . "\n";
1382 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001383
Robin H. Johnsonc38f0242007-04-25 19:37:20 -07001384 my @sendmail_parameters = ('-i', @recipients);
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001385 my $raw_from = $sender;
Felipe Contrerasc89e3242009-11-26 21:04:29 +02001386 if (defined $envelope_sender && $envelope_sender ne "auto") {
1387 $raw_from = $envelope_sender;
1388 }
Robin H. Johnsonf073a592007-04-25 19:37:22 -07001389 $raw_from = extract_valid_address($raw_from);
1390 unshift (@sendmail_parameters,
1391 '-f', $raw_from) if(defined $envelope_sender);
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001392
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001393 if ($needs_confirm && !$dry_run) {
1394 print "\n$header\n";
1395 if ($needs_confirm eq "inform") {
1396 $confirm_unconfigured = 0; # squelch this message for the rest of this run
Jay Soffian6e182512009-03-28 21:39:10 -04001397 $ask_default = "y"; # assume yes on EOF since user hasn't explicitly asked for confirmation
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001398 print __ <<EOF ;
1399 The Cc list above has been expanded by additional
1400 addresses found in the patch commit message. By default
1401 send-email prompts before sending whenever this occurs.
1402 This behavior is controlled by the sendemail.confirm
1403 configuration setting.
1404
1405 For additional information, run 'git send-email --help'.
1406 To retain the current behavior, but squelch this message,
1407 run 'git config --global sendemail.confirm auto'.
1408
1409EOF
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001410 }
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001411 # TRANSLATORS: Make sure to include [y] [n] [e] [q] [a] in your
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001412 # translation. The program will only accept English input
1413 # at this point.
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001414 $_ = ask(__("Send this email? ([y]es|[n]o|[e]dit|[q]uit|[a]ll): "),
1415 valid_re => qr/^(?:yes|y|no|n|edit|e|quit|q|all|a)/i,
Jay Soffian6e182512009-03-28 21:39:10 -04001416 default => $ask_default);
Vasco Almeida46493102016-12-14 11:54:36 -01001417 die __("Send this email reply required") unless defined $_;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001418 if (/^n/i) {
Michael Witten15da1082009-04-13 13:23:51 -05001419 return 0;
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001420 } elsif (/^e/i) {
1421 return -1;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001422 } elsif (/^q/i) {
1423 cleanup_compose_files();
1424 exit(0);
1425 } elsif (/^a/i) {
1426 $confirm = 'never';
1427 }
1428 }
1429
Pascal Obry052fbea2010-09-06 20:12:11 +02001430 unshift (@sendmail_parameters, @smtp_server_options);
1431
Matthew Wilcox61302592006-10-10 08:58:23 -06001432 if ($dry_run) {
1433 # We don't want to send the email.
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001434 } elsif (file_name_is_absolute($smtp_server)) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001435 my $pid = open my $sm, '|-';
1436 defined $pid or die $!;
1437 if (!$pid) {
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001438 exec($smtp_server, @sendmail_parameters) or die $!;
Eric Wongaca7ad72006-05-15 02:34:44 -07001439 }
1440 print $sm "$header\n$message";
Ævar Arnfjörð Bjarmason5e2c2ab2010-09-30 13:43:07 +00001441 close $sm or die $!;
Eric Wongaca7ad72006-05-15 02:34:44 -07001442 } else {
Junio C Hamano44b24762007-09-25 17:27:54 -07001443
1444 if (!defined $smtp_server) {
Vasco Almeida46493102016-12-14 11:54:36 -01001445 die __("The required SMTP server is not properly defined.")
Junio C Hamano44b24762007-09-25 17:27:54 -07001446 }
1447
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001448 require Net::SMTP;
Jonathan Niederbfbfc9a2017-05-31 17:17:43 -07001449 my $use_net_smtp_ssl = version->parse($Net::SMTP::VERSION) < version->parse("2.34");
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001450 $smtp_domain ||= maildomain();
1451
Thomas Rastf6bebd12008-06-25 21:42:43 +02001452 if ($smtp_encryption eq 'ssl') {
Junio C Hamano44b24762007-09-25 17:27:54 -07001453 $smtp_server_port ||= 465; # ssmtp
Thomas Rast5508f3e2013-12-01 23:48:43 +01001454 require IO::Socket::SSL;
John Keeping9d605242015-12-03 21:47:18 +00001455
1456 # Suppress "variable accessed once" warning.
1457 {
1458 no warnings 'once';
1459 $IO::Socket::SSL::DEBUG = 1;
1460 }
1461
Thomas Rast5508f3e2013-12-01 23:48:43 +01001462 # Net::SMTP::SSL->new() does not forward any SSL options
1463 IO::Socket::SSL::set_client_defaults(
1464 ssl_verify_params());
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001465
1466 if ($use_net_smtp_ssl) {
1467 require Net::SMTP::SSL;
1468 $smtp ||= Net::SMTP::SSL->new($smtp_server,
1469 Hello => $smtp_domain,
1470 Port => $smtp_server_port,
1471 Debug => $debug_net_smtp);
1472 }
1473 else {
1474 $smtp ||= Net::SMTP->new($smtp_server,
1475 Hello => $smtp_domain,
1476 Port => $smtp_server_port,
1477 Debug => $debug_net_smtp,
1478 SSL => 1);
1479 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001480 }
1481 else {
brian m. carlson1a741bf2013-07-04 22:04:52 +00001482 $smtp_server_port ||= 25;
1483 $smtp ||= Net::SMTP->new($smtp_server,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001484 Hello => $smtp_domain,
brian m. carlson1a741bf2013-07-04 22:04:52 +00001485 Debug => $debug_net_smtp,
1486 Port => $smtp_server_port);
Yakov Lernerfb3650e2009-09-25 15:10:21 -07001487 if ($smtp_encryption eq 'tls' && $smtp) {
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001488 if ($use_net_smtp_ssl) {
1489 $smtp->command('STARTTLS');
1490 $smtp->response();
1491 if ($smtp->code != 220) {
1492 die sprintf(__("Server does not support STARTTLS! %s"), $smtp->message);
1493 }
1494 require Net::SMTP::SSL;
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001495 $smtp = Net::SMTP::SSL->start_SSL($smtp,
1496 ssl_verify_params())
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001497 or die sprintf(__("STARTTLS failed! %s"), IO::Socket::SSL::errstr());
Thomas Rastf6bebd12008-06-25 21:42:43 +02001498 }
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001499 else {
1500 $smtp->starttls(ssl_verify_params())
1501 or die sprintf(__("STARTTLS failed! %s"), IO::Socket::SSL::errstr());
1502 }
1503 $smtp_encryption = '';
1504 # Send EHLO again to receive fresh
1505 # supported commands
1506 $smtp->hello($smtp_domain);
Thomas Rastf6bebd12008-06-25 21:42:43 +02001507 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001508 }
Junio C Hamano44b24762007-09-25 17:27:54 -07001509
1510 if (!$smtp) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001511 die __("Unable to initialize SMTP properly. Check config and use --smtp-debug."),
1512 " VALUES: server=$smtp_server ",
Jari Aaltoe5afb3a2010-03-14 17:15:33 +02001513 "encryption=$smtp_encryption ",
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001514 "hello=$smtp_domain",
Sylvain Rabota1dd7e12011-04-29 20:23:24 +02001515 defined $smtp_server_port ? " port=$smtp_server_port" : "";
Junio C Hamano44b24762007-09-25 17:27:54 -07001516 }
1517
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001518 smtp_auth_maybe or die $smtp->message;
Michael Witten2363d742008-02-03 19:53:56 -05001519
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001520 $smtp->mail( $raw_from ) or die $smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001521 $smtp->to( @recipients ) or die $smtp->message;
1522 $smtp->data or die $smtp->message;
Stefan Agnerf60c4832015-09-30 09:26:09 +02001523 $smtp->datasend("$header\n") or die $smtp->message;
1524 my @lines = split /^/, $message;
1525 foreach my $line (@lines) {
1526 $smtp->datasend("$line") or die $smtp->message;
1527 }
Eric Wongaca7ad72006-05-15 02:34:44 -07001528 $smtp->dataend() or die $smtp->message;
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001529 $smtp->code =~ /250|200/ or die sprintf(__("Failed to send %s\n"), $subject).$smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001530 }
Ryan Anderson27184352006-02-05 20:13:52 -05001531 if ($quiet) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001532 printf($dry_run ? __("Dry-Sent %s\n") : __("Sent %s\n"), $subject);
Ryan Anderson27184352006-02-05 20:13:52 -05001533 } else {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001534 print($dry_run ? __("Dry-OK. Log says:\n") : __("OK. Log says:\n"));
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001535 if (!file_name_is_absolute($smtp_server)) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001536 print "Server: $smtp_server\n";
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001537 print "MAIL FROM:<$raw_from>\n";
Joe Perches02461e02009-10-08 10:03:26 -07001538 foreach my $entry (@recipients) {
1539 print "RCPT TO:<$entry>\n";
1540 }
Eric Wongaca7ad72006-05-15 02:34:44 -07001541 } else {
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001542 print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001543 }
David D. Kilzerb7f30e02007-11-18 20:14:55 -08001544 print $header, "\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001545 if ($smtp) {
Vasco Almeida46493102016-12-14 11:54:36 -01001546 print __("Result: "), $smtp->code, ' ',
Eric Wongaca7ad72006-05-15 02:34:44 -07001547 ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
1548 } else {
Vasco Almeida46493102016-12-14 11:54:36 -01001549 print __("Result: OK\n");
Eric Wongaca7ad72006-05-15 02:34:44 -07001550 }
Ryan Anderson30d08b32006-02-02 11:56:06 -05001551 }
Michael Witten15da1082009-04-13 13:23:51 -05001552
1553 return 1;
Ryan Anderson83b24432005-07-31 04:17:25 -04001554}
1555
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001556$in_reply_to = $initial_in_reply_to;
1557$references = $initial_in_reply_to || '';
Ryan Anderson83b24432005-07-31 04:17:25 -04001558$subject = $initial_subject;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001559$message_num = 0;
Ryan Anderson83b24432005-07-31 04:17:25 -04001560
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001561# Prepares the email, prompts the user, sends it out
1562# Returns 0 if an edit was done and the function should be called again, or 1
1563# otherwise.
1564sub process_file {
1565 my ($t) = @_;
1566
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001567 open my $fh, "<", $t or die sprintf(__("can't open file %s"), $t);
Ryan Anderson83b24432005-07-31 04:17:25 -04001568
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001569 my $author = undef;
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001570 my $sauthor = undef;
Jeff King8291db62007-11-16 05:49:09 -05001571 my $author_encoding;
1572 my $has_content_type;
1573 my $body_encoding;
Paolo Bonzinibb294562014-11-25 15:00:26 +01001574 my $xfer_encoding;
1575 my $has_mime_version;
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001576 @to = ();
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001577 @cc = ();
Junio C Hamanoce91c2f2006-10-05 16:36:49 -07001578 @xh = ();
Junio C Hamanoe6b09642006-10-07 03:09:05 -07001579 my $input_format = undef;
Jay Soffian50126992009-02-14 23:32:14 -05001580 my @header = ();
Ryan Anderson83b24432005-07-31 04:17:25 -04001581 $message = "";
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001582 $message_num++;
Jay Soffian50126992009-02-14 23:32:14 -05001583 # First unfold multiline header fields
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001584 while(<$fh>) {
Jay Soffian50126992009-02-14 23:32:14 -05001585 last if /^\s*$/;
1586 if (/^\s+\S/ and @header) {
1587 chomp($header[$#header]);
1588 s/^\s+/ /;
1589 $header[$#header] .= $_;
1590 } else {
1591 push(@header, $_);
1592 }
1593 }
1594 # Now parse the header
1595 foreach(@header) {
1596 if (/^From /) {
1597 $input_format = 'mbox';
1598 next;
1599 }
1600 chomp;
1601 if (!defined $input_format && /^[-A-Za-z]+:\s/) {
1602 $input_format = 'mbox';
1603 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001604
Jay Soffian50126992009-02-14 23:32:14 -05001605 if (defined $input_format && $input_format eq 'mbox') {
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001606 if (/^Subject:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001607 $subject = $1;
1608 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001609 elsif (/^From:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001610 ($author, $author_encoding) = unquote_rfc2047($1);
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001611 $sauthor = sanitize_address($author);
Jay Soffian50126992009-02-14 23:32:14 -05001612 next if $suppress_cc{'author'};
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001613 next if $suppress_cc{'self'} and $sauthor eq $sender;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001614 printf(__("(mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001615 $1, $_) unless $quiet;
1616 push @cc, $1;
1617 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001618 elsif (/^To:\s+(.*)$/i) {
Stephen Boyd21802cd2010-09-29 00:26:44 -07001619 foreach my $addr (parse_address_line($1)) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001620 printf(__("(mbox) Adding to: %s from line '%s'\n"),
Stephen Boyd21802cd2010-09-29 00:26:44 -07001621 $addr, $_) unless $quiet;
Krzysztof Mazure4312252012-11-22 19:12:10 +01001622 push @to, $addr;
Stephen Boyd21802cd2010-09-29 00:26:44 -07001623 }
1624 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001625 elsif (/^Cc:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001626 foreach my $addr (parse_address_line($1)) {
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001627 my $qaddr = unquote_rfc2047($addr);
1628 my $saddr = sanitize_address($qaddr);
1629 if ($saddr eq $sender) {
David Brown65648282007-12-25 19:56:29 -08001630 next if ($suppress_cc{'self'});
David Brown65648282007-12-25 19:56:29 -08001631 } else {
1632 next if ($suppress_cc{'cc'});
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001633 }
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001634 printf(__("(mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001635 $addr, $_) unless $quiet;
1636 push @cc, $addr;
Ryan Anderson83b24432005-07-31 04:17:25 -04001637 }
1638 }
Jay Soffian50126992009-02-14 23:32:14 -05001639 elsif (/^Content-type:/i) {
1640 $has_content_type = 1;
1641 if (/charset="?([^ "]+)/) {
1642 $body_encoding = $1;
1643 }
1644 push @xh, $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001645 }
Paolo Bonzinibb294562014-11-25 15:00:26 +01001646 elsif (/^MIME-Version/i) {
1647 $has_mime_version = 1;
1648 push @xh, $_;
1649 }
Jay Soffian50126992009-02-14 23:32:14 -05001650 elsif (/^Message-Id: (.*)/i) {
1651 $message_id = $1;
1652 }
Paolo Bonzinibb294562014-11-25 15:00:26 +01001653 elsif (/^Content-Transfer-Encoding: (.*)/i) {
1654 $xfer_encoding = $1 if not defined $xfer_encoding;
1655 }
Stefan Agner256be1d2018-04-17 23:16:30 +02001656 elsif (/^In-Reply-To: (.*)/i) {
1657 $in_reply_to = $1;
1658 }
1659 elsif (/^References: (.*)/i) {
1660 $references = $1;
1661 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001662 elsif (!/^Date:\s/i && /^[-A-Za-z]+:\s+\S/) {
Jay Soffian50126992009-02-14 23:32:14 -05001663 push @xh, $_;
1664 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001665 } else {
Jay Soffian50126992009-02-14 23:32:14 -05001666 # In the traditional
1667 # "send lots of email" format,
1668 # line 1 = cc
1669 # line 2 = subject
1670 # So let's support that, too.
1671 $input_format = 'lots';
1672 if (@cc == 0 && !$suppress_cc{'cc'}) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001673 printf(__("(non-mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001674 $_, $_) unless $quiet;
1675 push @cc, $_;
1676 } elsif (!defined $subject) {
1677 $subject = $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001678 }
1679 }
1680 }
Jay Soffian50126992009-02-14 23:32:14 -05001681 # Now parse the message body
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001682 while(<$fh>) {
Jay Soffian50126992009-02-14 23:32:14 -05001683 $message .= $_;
Matthieu Moycb2922f2017-08-23 12:21:01 +02001684 if (/^(Signed-off-by|Cc): (.*)/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001685 chomp;
Jay Soffian3531e272009-02-14 23:32:15 -05001686 my ($what, $c) = ($1, $2);
Matthieu Moycb2922f2017-08-23 12:21:01 +02001687 # strip garbage for the address we'll use:
1688 $c = strip_garbage_one_address($c);
1689 # sanitize a bit more to decide whether to suppress the address:
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001690 my $sc = sanitize_address($c);
1691 if ($sc eq $sender) {
Jay Soffian3531e272009-02-14 23:32:15 -05001692 next if ($suppress_cc{'self'});
1693 } else {
1694 next if $suppress_cc{'sob'} and $what =~ /Signed-off-by/i;
1695 next if $suppress_cc{'bodycc'} and $what =~ /Cc/i;
1696 }
Jay Soffian50126992009-02-14 23:32:14 -05001697 push @cc, $c;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001698 printf(__("(body) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001699 $c, $_) unless $quiet;
1700 }
1701 }
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001702 close $fh;
Joe Perches324a8bd2007-08-17 18:51:12 -07001703
Joe Perches6e74e072010-09-24 10:03:00 -07001704 push @to, recipients_cmd("to-cmd", "to", $to_cmd, $t)
1705 if defined $to_cmd;
1706 push @cc, recipients_cmd("cc-cmd", "cc", $cc_cmd, $t)
1707 if defined $cc_cmd && !$suppress_cc{'cccmd'};
Joe Perches324a8bd2007-08-17 18:51:12 -07001708
Thomas Rast3cae7e52010-06-17 22:10:39 +02001709 if ($broken_encoding{$t} && !$has_content_type) {
Paolo Bonzinibb294562014-11-25 15:00:26 +01001710 $xfer_encoding = '8bit' if not defined $xfer_encoding;
Thomas Rast3cae7e52010-06-17 22:10:39 +02001711 $has_content_type = 1;
Paolo Bonzinibb294562014-11-25 15:00:26 +01001712 push @xh, "Content-Type: text/plain; charset=$auto_8bit_encoding";
Thomas Rast3cae7e52010-06-17 22:10:39 +02001713 $body_encoding = $auto_8bit_encoding;
1714 }
1715
Krzysztof Mazurce547802012-10-24 23:08:26 +02001716 if ($broken_encoding{$t} && !is_rfc2047_quoted($subject)) {
1717 $subject = quote_subject($subject, $auto_8bit_encoding);
Thomas Rast3cae7e52010-06-17 22:10:39 +02001718 }
1719
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001720 if (defined $sauthor and $sauthor ne $sender) {
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001721 $message = "From: $author\n\n$message";
Jeff King8291db62007-11-16 05:49:09 -05001722 if (defined $author_encoding) {
1723 if ($has_content_type) {
1724 if ($body_encoding eq $author_encoding) {
1725 # ok, we already have the right encoding
1726 }
1727 else {
1728 # uh oh, we should re-encode
1729 }
1730 }
1731 else {
Paolo Bonzinibb294562014-11-25 15:00:26 +01001732 $xfer_encoding = '8bit' if not defined $xfer_encoding;
Thomas Rast3cae7e52010-06-17 22:10:39 +02001733 $has_content_type = 1;
Jeff King8291db62007-11-16 05:49:09 -05001734 push @xh,
Paolo Bonzinibb294562014-11-25 15:00:26 +01001735 "Content-Type: text/plain; charset=$author_encoding";
Jeff King8291db62007-11-16 05:49:09 -05001736 }
1737 }
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001738 }
brian m. carlsone67a2282018-07-08 22:17:12 +00001739 $xfer_encoding = '8bit' if not defined $xfer_encoding;
1740 ($message, $xfer_encoding) = apply_transfer_encoding(
1741 $message, $xfer_encoding, $target_xfer_encoding);
1742 push @xh, "Content-Transfer-Encoding: $xfer_encoding";
1743 unshift @xh, 'MIME-Version: 1.0' unless $has_mime_version;
Ryan Anderson83b24432005-07-31 04:17:25 -04001744
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001745 $needs_confirm = (
1746 $confirm eq "always" or
1747 ($confirm =~ /^(?:auto|cc)$/ && @cc) or
1748 ($confirm =~ /^(?:auto|compose)$/ && $compose && $message_num == 1));
1749 $needs_confirm = "inform" if ($needs_confirm && $confirm_unconfigured && @cc);
1750
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001751 @to = process_address_list(@to);
1752 @cc = process_address_list(@cc);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001753
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001754 @to = (@initial_to, @to);
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001755 @cc = (@initial_cc, @cc);
1756
Michael S. Tsirkinf515c902014-04-29 08:41:16 +03001757 if ($message_num == 1) {
1758 if (defined $cover_cc and $cover_cc) {
1759 @initial_cc = @cc;
1760 }
1761 if (defined $cover_to and $cover_to) {
1762 @initial_to = @to;
1763 }
1764 }
1765
Michael Witten15da1082009-04-13 13:23:51 -05001766 my $message_was_sent = send_message();
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001767 if ($message_was_sent == -1) {
1768 do_edit($t);
1769 return 0;
1770 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001771
1772 # set up for the next message
Junio C Hamano95a877a2009-06-12 09:23:43 -07001773 if ($thread && $message_was_sent &&
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001774 ($chain_reply_to || !defined $in_reply_to || length($in_reply_to) == 0 ||
Antonio Ospitedb54c8e2010-11-12 15:55:08 +01001775 $message_num == 1)) {
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001776 $in_reply_to = $message_id;
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001777 if (length $references > 0) {
YOSHIFUJI Hideaki / 吉藤英明a925b892007-04-06 08:50:24 +09001778 $references .= "\n $message_id";
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001779 } else {
1780 $references = "$message_id";
1781 }
Ryan Anderson78488b22005-07-31 20:04:24 -04001782 }
Jeff King4f3d3702007-12-17 15:51:34 -05001783 $message_id = undef;
xiaoqiang zhao5453b832017-05-21 20:59:50 +08001784 $num_sent++;
1785 if (defined $batch_size && $num_sent == $batch_size) {
1786 $num_sent = 0;
1787 $smtp->quit if defined $smtp;
1788 undef $smtp;
1789 undef $auth;
1790 sleep($relogin_delay) if defined $relogin_delay;
1791 }
Drew DeVault04c4a4e2018-05-04 09:08:11 -04001792
1793 return 1;
1794}
1795
1796foreach my $t (@files) {
1797 while (!process_file($t)) {
1798 # user edited the file
1799 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001800}
Ryan Andersone2057352005-08-02 21:45:22 -04001801
Joe Perches6e74e072010-09-24 10:03:00 -07001802# Execute a command (e.g. $to_cmd) to get a list of email addresses
1803# and return a results array
1804sub recipients_cmd {
1805 my ($prefix, $what, $cmd, $file) = @_;
1806
Joe Perches6e74e072010-09-24 10:03:00 -07001807 my @addresses = ();
Ramkumar Ramachandraa47eab02013-03-31 18:40:42 -07001808 open my $fh, "-|", "$cmd \Q$file\E"
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001809 or die sprintf(__("(%s) Could not execute '%s'"), $prefix, $cmd);
Junio C Hamano7ebee442010-10-26 22:02:52 -07001810 while (my $address = <$fh>) {
Joe Perches6e74e072010-09-24 10:03:00 -07001811 $address =~ s/^\s*//g;
1812 $address =~ s/\s*$//g;
1813 $address = sanitize_address($address);
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001814 next if ($address eq $sender and $suppress_cc{'self'});
Joe Perches6e74e072010-09-24 10:03:00 -07001815 push @addresses, $address;
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001816 printf(__("(%s) Adding %s: %s from: '%s'\n"),
1817 $prefix, $what, $address, $cmd) unless $quiet;
Joe Perches6e74e072010-09-24 10:03:00 -07001818 }
Junio C Hamano7ebee442010-10-26 22:02:52 -07001819 close $fh
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001820 or die sprintf(__("(%s) failed to close pipe to '%s'"), $prefix, $cmd);
Joe Perches6e74e072010-09-24 10:03:00 -07001821 return @addresses;
1822}
1823
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001824cleanup_compose_files();
Ryan Anderson1f038a02005-09-05 01:13:07 -04001825
Ævar Arnfjörð Bjarmason4bf597e2010-09-30 13:43:00 +00001826sub cleanup_compose_files {
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001827 unlink($compose_filename, $compose_filename . ".final") if $compose;
Ryan Anderson1f038a02005-09-05 01:13:07 -04001828}
1829
Eric Wong4bc87a22006-03-25 17:20:48 -08001830$smtp->quit if $smtp;
Ryan Andersone2057352005-08-02 21:45:22 -04001831
Paolo Bonzini8d814082014-11-25 15:00:27 +01001832sub apply_transfer_encoding {
1833 my $message = shift;
1834 my $from = shift;
1835 my $to = shift;
1836
1837 return $message if ($from eq $to and $from ne '7bit');
1838
1839 require MIME::QuotedPrint;
1840 require MIME::Base64;
1841
1842 $message = MIME::QuotedPrint::decode($message)
1843 if ($from eq 'quoted-printable');
1844 $message = MIME::Base64::decode($message)
1845 if ($from eq 'base64');
1846
brian m. carlson7a369872018-07-08 22:17:10 +00001847 $to = ($message =~ /.{999,}/) ? 'quoted-printable' : '8bit'
1848 if $to eq 'auto';
1849
Vasco Almeida46493102016-12-14 11:54:36 -01001850 die __("cannot send message as 7bit")
Paolo Bonzini8d814082014-11-25 15:00:27 +01001851 if ($to eq '7bit' and $message =~ /[^[:ascii:]]/);
brian m. carlson7a369872018-07-08 22:17:10 +00001852 return ($message, $to)
Paolo Bonzini8d814082014-11-25 15:00:27 +01001853 if ($to eq '7bit' or $to eq '8bit');
brian m. carlson7a369872018-07-08 22:17:10 +00001854 return (MIME::QuotedPrint::encode($message, "\n", 0), $to)
Paolo Bonzini8d814082014-11-25 15:00:27 +01001855 if ($to eq 'quoted-printable');
brian m. carlson7a369872018-07-08 22:17:10 +00001856 return (MIME::Base64::encode($message, "\n"), $to)
Paolo Bonzini8d814082014-11-25 15:00:27 +01001857 if ($to eq 'base64');
Vasco Almeida46493102016-12-14 11:54:36 -01001858 die __("invalid transfer encoding");
Paolo Bonzini8d814082014-11-25 15:00:27 +01001859}
1860
Ævar Arnfjörð Bjarmasonc438ea22010-09-30 13:42:59 +00001861sub unique_email_list {
Ryan Andersone2057352005-08-02 21:45:22 -04001862 my %seen;
1863 my @emails;
1864
1865 foreach my $entry (@_) {
Krzysztof Mazure4312252012-11-22 19:12:10 +01001866 my $clean = extract_valid_address_or_die($entry);
1867 $seen{$clean} ||= 0;
1868 next if $seen{$clean}++;
1869 push @emails, $entry;
Ryan Andersone2057352005-08-02 21:45:22 -04001870 }
1871 return @emails;
1872}
Jeff King747bbff2008-01-18 09:19:48 -05001873
1874sub validate_patch {
brian m. carlsonf2d06fb2018-07-08 22:17:11 +00001875 my ($fn, $xfer_encoding) = @_;
Jonathan Tan64896602017-05-12 15:38:26 -07001876
Jonathan Tan177409e2017-06-01 16:50:55 -07001877 if ($repo) {
1878 my $validate_hook = catfile(catdir($repo->repo_path(), 'hooks'),
1879 'sendemail-validate');
1880 my $hook_error;
1881 if (-x $validate_hook) {
1882 my $target = abs_path($fn);
1883 # The hook needs a correct cwd and GIT_DIR.
1884 my $cwd_save = cwd();
1885 chdir($repo->wc_path() or $repo->repo_path())
1886 or die("chdir: $!");
1887 local $ENV{"GIT_DIR"} = $repo->repo_path();
1888 $hook_error = "rejected by sendemail-validate hook"
1889 if system($validate_hook, $target);
1890 chdir($cwd_save) or die("chdir: $!");
1891 }
1892 return $hook_error if $hook_error;
Jonathan Tan64896602017-05-12 15:38:26 -07001893 }
Jonathan Tan64896602017-05-12 15:38:26 -07001894
brian m. carlsonf2d06fb2018-07-08 22:17:11 +00001895 # Any long lines will be automatically fixed if we use a suitable transfer
1896 # encoding.
1897 unless ($xfer_encoding =~ /^(?:auto|quoted-printable|base64)$/) {
1898 open(my $fh, '<', $fn)
1899 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
1900 while (my $line = <$fh>) {
1901 if (length($line) > 998) {
1902 return sprintf(__("%s: patch contains a line longer than 998 characters"), $.);
1903 }
Jeff King747bbff2008-01-18 09:19:48 -05001904 }
1905 }
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -07001906 return;
Jeff King747bbff2008-01-18 09:19:48 -05001907}
Jeff King0706bd12008-03-28 17:28:33 -04001908
Junio C Hamano531220b2016-03-17 22:40:05 -07001909sub handle_backup {
1910 my ($last, $lastlen, $file, $known_suffix) = @_;
1911 my ($suffix, $skip);
1912
1913 $skip = 0;
1914 if (defined $last &&
1915 ($lastlen < length($file)) &&
1916 (substr($file, 0, $lastlen) eq $last) &&
1917 ($suffix = substr($file, $lastlen)) !~ /^[a-z0-9]/i) {
1918 if (defined $known_suffix && $suffix eq $known_suffix) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001919 printf(__("Skipping %s with backup suffix '%s'.\n"), $file, $known_suffix);
Junio C Hamano531220b2016-03-17 22:40:05 -07001920 $skip = 1;
1921 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001922 # TRANSLATORS: please keep "[y|N]" as is.
1923 my $answer = ask(sprintf(__("Do you really want to send %s? [y|N]: "), $file),
Junio C Hamano531220b2016-03-17 22:40:05 -07001924 valid_re => qr/^(?:y|n)/i,
1925 default => 'n');
1926 $skip = ($answer ne 'y');
1927 if ($skip) {
1928 $known_suffix = $suffix;
1929 }
1930 }
1931 }
1932 return ($skip, $known_suffix);
1933}
1934
1935sub handle_backup_files {
1936 my @file = @_;
1937 my ($last, $lastlen, $known_suffix, $skip, @result);
1938 for my $file (@file) {
1939 ($skip, $known_suffix) = handle_backup($last, $lastlen,
1940 $file, $known_suffix);
1941 push @result, $file unless $skip;
1942 $last = $file;
1943 $lastlen = length($file);
1944 }
1945 return @result;
1946}
1947
Jeff King0706bd12008-03-28 17:28:33 -04001948sub file_has_nonascii {
1949 my $fn = shift;
1950 open(my $fh, '<', $fn)
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001951 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
Jeff King0706bd12008-03-28 17:28:33 -04001952 while (my $line = <$fh>) {
1953 return 1 if $line =~ /[^[:ascii:]]/;
1954 }
1955 return 0;
1956}
Thomas Rast3cae7e52010-06-17 22:10:39 +02001957
1958sub body_or_subject_has_nonascii {
1959 my $fn = shift;
1960 open(my $fh, '<', $fn)
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001961 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
Thomas Rast3cae7e52010-06-17 22:10:39 +02001962 while (my $line = <$fh>) {
1963 last if $line =~ /^$/;
1964 return 1 if $line =~ /^Subject.*[^[:ascii:]]/;
1965 }
1966 while (my $line = <$fh>) {
1967 return 1 if $line =~ /[^[:ascii:]]/;
1968 }
1969 return 0;
1970}