blob: 2fa7818ca9a8ac7363d17aef17b864f7361b3eb8 [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:
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700172my (@to,$no_to,@initial_to,@cc,$no_cc,@initial_cc,@bcclist,$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);
Paolo Bonzini8d814082014-11-25 15:00:27 +0100234my ($target_xfer_encoding);
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,
267 "bcc" => \@bcclist,
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
Ryan Anderson83b24432005-07-31 04:17:25 -0400310# Begin by accumulating all the variables (defined above), that we will end up
311# needing, first, from the command line:
312
Clemens Buchacherc5978242011-09-03 19:06:13 +0200313my $help;
314my $rc = GetOptions("h" => \$help,
Jacob Keller17b7a832015-11-19 14:52:11 -0800315 "dump-aliases" => \$dump_aliases);
316usage() unless $rc;
Vasco Almeida46493102016-12-14 11:54:36 -0100317die __("--dump-aliases incompatible with other options\n")
Jacob Keller17b7a832015-11-19 14:52:11 -0800318 if !$help and $dump_aliases and @ARGV;
319$rc = GetOptions(
Clemens Buchacherc5978242011-09-03 19:06:13 +0200320 "sender|from=s" => \$sender,
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100321 "in-reply-to=s" => \$initial_in_reply_to,
Christian Ludwigd11c9432018-03-04 00:58:14 +0100322 "reply-to=s" => \$reply_to,
Ryan Anderson83b24432005-07-31 04:17:25 -0400323 "subject=s" => \$initial_subject,
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700324 "to=s" => \@initial_to,
Joe Perches6e74e072010-09-24 10:03:00 -0700325 "to-cmd=s" => \$to_cmd,
Stephen Boydf434c082010-03-07 14:46:48 -0800326 "no-to" => \$no_to,
Ryan Andersonda140f82006-02-13 03:05:15 -0500327 "cc=s" => \@initial_cc,
Stephen Boydf434c082010-03-07 14:46:48 -0800328 "no-cc" => \$no_cc,
Ryan Anderson58063242006-05-29 12:30:13 -0700329 "bcc=s" => \@bcclist,
Stephen Boydf434c082010-03-07 14:46:48 -0800330 "no-bcc" => \$no_bcc,
Ryan Anderson78488b22005-07-31 20:04:24 -0400331 "chain-reply-to!" => \$chain_reply_to,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800332 "no-chain-reply-to" => sub {$chain_reply_to = 0},
Ryan Anderson3342d852005-07-31 20:04:24 -0400333 "smtp-server=s" => \$smtp_server,
Pascal Obry052fbea2010-09-06 20:12:11 +0200334 "smtp-server-option=s" => \@smtp_server_options,
Junio C Hamano44b24762007-09-25 17:27:54 -0700335 "smtp-server-port=s" => \$smtp_server_port,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900336 "smtp-user=s" => \$smtp_authuser,
Michael Witten2363d742008-02-03 19:53:56 -0500337 "smtp-pass:s" => \$smtp_authpass,
Thomas Rastf6bebd12008-06-25 21:42:43 +0200338 "smtp-ssl" => sub { $smtp_encryption = 'ssl' },
339 "smtp-encryption=s" => \$smtp_encryption,
Thomas Rast979e6522013-12-01 23:48:42 +0100340 "smtp-ssl-cert-path=s" => \$smtp_ssl_cert_path,
Jari Aaltof60812e2010-03-14 17:16:09 +0200341 "smtp-debug:i" => \$debug_net_smtp,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -0400342 "smtp-domain:s" => \$smtp_domain,
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200343 "smtp-auth=s" => \$smtp_auth,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900344 "identity=s" => \$identity,
Felipe Contreras402596a2013-04-07 01:10:27 -0600345 "annotate!" => \$annotate,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800346 "no-annotate" => sub {$annotate = 0},
Ryan Anderson1f038a02005-09-05 01:13:07 -0400347 "compose" => \$compose,
Ryan Anderson30d08b32006-02-02 11:56:06 -0500348 "quiet" => \$quiet,
Joe Perches324a8bd2007-08-17 18:51:12 -0700349 "cc-cmd=s" => \$cc_cmd,
Adam Roben5483c712007-06-27 20:59:37 -0700350 "suppress-from!" => \$suppress_from,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800351 "no-suppress-from" => sub {$suppress_from = 0},
David Brown65648282007-12-25 19:56:29 -0800352 "suppress-cc=s" => \@suppress_cc,
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500353 "signed-off-cc|signed-off-by-cc!" => \$signed_off_by_cc,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800354 "no-signed-off-cc|no-signed-off-by-cc" => sub {$signed_off_by_cc = 0},
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300355 "cc-cover|cc-cover!" => \$cover_cc,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800356 "no-cc-cover" => sub {$cover_cc = 0},
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300357 "to-cover|to-cover!" => \$cover_to,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800358 "no-to-cover" => sub {$cover_to = 0},
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500359 "confirm=s" => \$confirm,
Matthew Wilcox61302592006-10-10 08:58:23 -0600360 "dry-run" => \$dry_run,
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700361 "envelope-sender=s" => \$envelope_sender,
Adam Roben5483c712007-06-27 20:59:37 -0700362 "thread!" => \$thread,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800363 "no-thread" => sub {$thread = 0},
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500364 "validate!" => \$validate,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800365 "no-validate" => sub {$validate = 0},
Paolo Bonzini8d814082014-11-25 15:00:27 +0100366 "transfer-encoding=s" => \$target_xfer_encoding,
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100367 "format-patch!" => \$format_patch,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800368 "no-format-patch" => sub {$format_patch = 0},
Thomas Rast3cae7e52010-06-17 22:10:39 +0200369 "8bit-encoding=s" => \$auto_8bit_encoding,
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200370 "compose-encoding=s" => \$compose_encoding,
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200371 "force" => \$force,
Luis Henriquesac1596a2014-03-24 21:38:27 +0000372 "xmailer!" => \$use_xmailer,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800373 "no-xmailer" => sub {$use_xmailer = 0},
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800374 "batch-size=i" => \$batch_size,
375 "relogin-delay=i" => \$relogin_delay,
Ryan Anderson83b24432005-07-31 04:17:25 -0400376 );
377
Clemens Buchacherc5978242011-09-03 19:06:13 +0200378usage() if $help;
Michael Coleman1b0baf12007-02-27 22:47:54 -0600379unless ($rc) {
380 usage();
381}
382
Vasco Almeida46493102016-12-14 11:54:36 -0100383die __("Cannot run git format-patch from outside a repository\n")
Jay Soffianeed6ca72009-02-14 23:32:13 -0500384 if $format_patch and not $repo;
385
Stefan Beller9caa7062018-02-12 11:44:04 -0800386die __("`batch-size` and `relogin` must be specified together " .
387 "(via command-line or configuration option)\n")
388 if defined $relogin_delay and not defined $batch_size;
389
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900390# Now, let's fill any that aren't set in with defaults:
391
392sub read_config {
393 my ($prefix) = @_;
394
395 foreach my $setting (keys %config_bool_settings) {
396 my $target = $config_bool_settings{$setting}->[0];
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100397 $$target = Git::config_bool(@repo, "$prefix.$setting") unless (defined $$target);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900398 }
399
Cord Seelecec5dae2011-09-30 12:52:25 +0200400 foreach my $setting (keys %config_path_settings) {
Cord Seele463b0ea2011-10-14 22:53:31 +0200401 my $target = $config_path_settings{$setting};
402 if (ref($target) eq "ARRAY") {
403 unless (@$target) {
404 my @values = Git::config_path(@repo, "$prefix.$setting");
405 @$target = @values if (@values && defined $values[0]);
406 }
407 }
408 else {
409 $$target = Git::config_path(@repo, "$prefix.$setting") unless (defined $$target);
410 }
Cord Seelecec5dae2011-09-30 12:52:25 +0200411 }
412
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900413 foreach my $setting (keys %config_settings) {
414 my $target = $config_settings{$setting};
Stephen Boydf434c082010-03-07 14:46:48 -0800415 next if $setting eq "to" and defined $no_to;
416 next if $setting eq "cc" and defined $no_cc;
417 next if $setting eq "bcc" and defined $no_bcc;
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900418 if (ref($target) eq "ARRAY") {
419 unless (@$target) {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100420 my @values = Git::config(@repo, "$prefix.$setting");
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900421 @$target = @values if (@values && defined $values[0]);
422 }
423 }
424 else {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100425 $$target = Git::config(@repo, "$prefix.$setting") unless (defined $$target);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900426 }
427 }
Thomas Rastf6bebd12008-06-25 21:42:43 +0200428
429 if (!defined $smtp_encryption) {
430 my $enc = Git::config(@repo, "$prefix.smtpencryption");
431 if (defined $enc) {
432 $smtp_encryption = $enc;
433 } elsif (Git::config_bool(@repo, "$prefix.smtpssl")) {
434 $smtp_encryption = 'ssl';
435 }
436 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900437}
438
439# read configuration from [sendemail "$identity"], fall back on [sendemail]
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100440$identity = Git::config(@repo, "sendemail.identity") unless (defined $identity);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900441read_config("sendemail.$identity") if (defined $identity);
442read_config("sendemail");
443
444# fall back on builtin bool defaults
445foreach my $setting (values %config_bool_settings) {
446 ${$setting->[0]} = $setting->[1] unless (defined (${$setting->[0]}));
447}
448
Thomas Rastfa835cd2008-06-26 23:03:21 +0200449# 'default' encryption is none -- this only prevents a warning
450$smtp_encryption = '' unless (defined $smtp_encryption);
451
David Brown65648282007-12-25 19:56:29 -0800452# Set CC suppressions
453my(%suppress_cc);
454if (@suppress_cc) {
455 foreach my $entry (@suppress_cc) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100456 die sprintf(__("Unknown --suppress-cc field: '%s'\n"), $entry)
Ævar Arnfjörð Bjarmasone9bf7412010-09-30 13:43:04 +0000457 unless $entry =~ /^(?:all|cccmd|cc|author|self|sob|body|bodycc)$/;
David Brown65648282007-12-25 19:56:29 -0800458 $suppress_cc{$entry} = 1;
459 }
460}
461
462if ($suppress_cc{'all'}) {
Paolo Bonzinicb8a9bd2009-06-18 14:31:32 +0200463 foreach my $entry (qw (cccmd cc author self sob body bodycc)) {
David Brown65648282007-12-25 19:56:29 -0800464 $suppress_cc{$entry} = 1;
465 }
466 delete $suppress_cc{'all'};
467}
468
469# If explicit old-style ones are specified, they trump --suppress-cc.
470$suppress_cc{'self'} = $suppress_from if defined $suppress_from;
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500471$suppress_cc{'sob'} = !$signed_off_by_cc if defined $signed_off_by_cc;
David Brown65648282007-12-25 19:56:29 -0800472
Jay Soffian3531e272009-02-14 23:32:15 -0500473if ($suppress_cc{'body'}) {
474 foreach my $entry (qw (sob bodycc)) {
475 $suppress_cc{$entry} = 1;
476 }
477 delete $suppress_cc{'body'};
478}
479
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500480# Set confirm's default value
481my $confirm_unconfigured = !defined $confirm;
482if ($confirm_unconfigured) {
483 $confirm = scalar %suppress_cc ? 'compose' : 'auto';
484};
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100485die sprintf(__("Unknown --confirm setting: '%s'\n"), $confirm)
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500486 unless $confirm =~ /^(?:auto|cc|compose|always|never)/;
487
David Brown65648282007-12-25 19:56:29 -0800488# Debugging, print out the suppressions.
489if (0) {
490 print "suppressions:\n";
491 foreach my $entry (keys %suppress_cc) {
492 printf " %-5s -> $suppress_cc{$entry}\n", $entry;
493 }
494}
495
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100496my ($repoauthor, $repocommitter);
497($repoauthor) = Git::ident_person(@repo, 'author');
498($repocommitter) = Git::ident_person(@repo, 'committer');
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900499
Jay Soffian50126992009-02-14 23:32:14 -0500500sub parse_address_line {
Matthieu Moybd869f62018-01-05 19:36:51 +0100501 return map { $_->format } Mail::Address->parse($_[0]);
Jay Soffian50126992009-02-14 23:32:14 -0500502}
503
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800504sub split_addrs {
Junio C Hamano2f0e7cb2008-12-21 01:57:59 -0800505 return quotewords('\s*,\s*', 1, @_);
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800506}
507
Eric Wong994d6c62006-05-14 19:13:44 -0700508my %aliases;
Eric Sunshine09f11572015-05-31 18:29:27 -0400509
510sub parse_sendmail_alias {
511 local $_ = shift;
512 if (/"/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100513 printf STDERR __("warning: sendmail alias with quotes is not supported: %s\n"), $_;
Eric Sunshine86b89842015-06-01 14:22:36 -0400514 } elsif (/:include:/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100515 printf STDERR __("warning: `:include:` not supported: %s\n"), $_;
Eric Sunshine86b89842015-06-01 14:22:36 -0400516 } elsif (/[\/|]/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100517 printf STDERR __("warning: `/file` or `|pipe` redirection not supported: %s\n"), $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400518 } elsif (/^(\S+?)\s*:\s*(.+)$/) {
519 my ($alias, $addr) = ($1, $2);
520 $aliases{$alias} = [ split_addrs($addr) ];
521 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100522 printf STDERR __("warning: sendmail line is not recognized: %s\n"), $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400523 }
524}
525
526sub parse_sendmail_aliases {
527 my $fh = shift;
Eric Sunshine2532dd02015-05-31 18:29:29 -0400528 my $s = '';
Eric Sunshine09f11572015-05-31 18:29:27 -0400529 while (<$fh>) {
Eric Sunshine2532dd02015-05-31 18:29:29 -0400530 chomp;
Eric Sunshine020be85f2015-05-31 18:29:28 -0400531 next if /^\s*$/ || /^\s*#/;
Eric Sunshine2532dd02015-05-31 18:29:29 -0400532 $s .= $_, next if $s =~ s/\\$// || s/^\s+//;
533 parse_sendmail_alias($s) if $s;
534 $s = $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400535 }
Eric Sunshine2532dd02015-05-31 18:29:29 -0400536 $s =~ s/\\$//; # silently tolerate stray '\' on last line
537 parse_sendmail_alias($s) if $s;
Eric Sunshine09f11572015-05-31 18:29:27 -0400538}
539
Eric Wong994d6c62006-05-14 19:13:44 -0700540my %parse_alias = (
541 # multiline formats can be supported in the future
542 mutt => sub { my $fh = shift; while (<$fh>) {
Felipe Contrerasffc01f92009-09-30 17:49:36 +0300543 if (/^\s*alias\s+(?:-group\s+\S+\s+)*(\S+)\s+(.*)$/) {
Eric Wong994d6c62006-05-14 19:13:44 -0700544 my ($alias, $addr) = ($1, $2);
545 $addr =~ s/#.*$//; # mutt allows # comments
Eric Wong2c510f22016-01-04 20:53:30 +0000546 # commas delimit multiple addresses
547 my @addr = split_addrs($addr);
548
549 # quotes may be escaped in the file,
550 # unescape them so we do not double-escape them later.
551 s/\\"/"/g foreach @addr;
552 $aliases{$alias} = \@addr
Eric Wong994d6c62006-05-14 19:13:44 -0700553 }}},
554 mailrc => sub { my $fh = shift; while (<$fh>) {
Jeff Kinga277d1e2016-03-17 19:58:22 -0400555 if (/^alias\s+(\S+)\s+(.*?)\s*$/) {
Eric Wong994d6c62006-05-14 19:13:44 -0700556 # spaces delimit multiple addresses
Eric W. Biedermanfe87c922009-05-20 19:45:53 -0700557 $aliases{$1} = [ quotewords('\s+', 0, $2) ];
Eric Wong994d6c62006-05-14 19:13:44 -0700558 }}},
Trent Piepho73c427e2008-11-25 18:55:00 -0800559 pine => sub { my $fh = shift; my $f='\t[^\t]*';
560 for (my $x = ''; defined($x); $x = $_) {
561 chomp $x;
562 $x .= $1 while(defined($_ = <$fh>) && /^ +(.*)$/);
563 $x =~ /^(\S+)$f\t\(?([^\t]+?)\)?(:?$f){0,2}$/ or next;
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800564 $aliases{$1} = [ split_addrs($2) ];
Trent Piepho73c427e2008-11-25 18:55:00 -0800565 }},
Bill Pemberton7613ea32009-04-22 09:41:29 -0400566 elm => sub { my $fh = shift;
567 while (<$fh>) {
568 if (/^(\S+)\s+=\s+[^=]+=\s(\S+)/) {
569 my ($alias, $addr) = ($1, $2);
570 $aliases{$alias} = [ split_addrs($addr) ];
571 }
572 } },
Eric Sunshine09f11572015-05-31 18:29:27 -0400573 sendmail => \&parse_sendmail_aliases,
Eric Wong994d6c62006-05-14 19:13:44 -0700574 gnus => sub { my $fh = shift; while (<$fh>) {
575 if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
576 $aliases{$1} = [ $2 ];
577 }}}
578);
579
Petr Baudis3cb8caf2006-07-03 22:47:58 +0200580if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) {
Eric Wong994d6c62006-05-14 19:13:44 -0700581 foreach my $file (@alias_files) {
582 open my $fh, '<', $file or die "opening $file: $!\n";
583 $parse_alias{$aliasfiletype}->($fh);
584 close $fh;
585 }
586}
587
Jacob Keller17b7a832015-11-19 14:52:11 -0800588if ($dump_aliases) {
589 print "$_\n" for (sort keys %aliases);
590 exit(0);
591}
592
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700593# is_format_patch_arg($f) returns 0 if $f names a patch, or 1 if
594# $f is a revision list specification to be passed to format-patch.
595sub is_format_patch_arg {
Jay Soffianeed6ca72009-02-14 23:32:13 -0500596 return unless $repo;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100597 my $f = shift;
598 try {
599 $repo->command('rev-parse', '--verify', '--quiet', $f);
600 if (defined($format_patch)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100601 return $format_patch;
602 }
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100603 die sprintf(__ <<EOF, $f, $f);
604File '%s' exists but it could also be the range of commits
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100605to produce patches for. Please disambiguate by...
606
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100607 * Saying "./%s" if you mean a file; or
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100608 * Giving --format-patch option if you mean a range.
609EOF
610 } catch Git::Error::Command with {
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700611 # Not a valid revision. Treat it as a filename.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100612 return 0;
613 }
614}
615
Jeff Kingaa548922008-01-18 09:19:36 -0500616# Now that all the defaults are set, process the rest of the command line
617# arguments and collect up the files that need to be processed.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100618my @rev_list_opts;
Junio C Hamano69f4ce52008-11-30 22:38:20 -0800619while (defined(my $f = shift @ARGV)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100620 if ($f eq "--") {
621 push @rev_list_opts, "--", @ARGV;
622 @ARGV = ();
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700623 } elsif (-d $f and !is_format_patch_arg($f)) {
Ævar Arnfjörð Bjarmasonc6038162010-09-30 13:42:54 +0000624 opendir my $dh, $f
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100625 or die sprintf(__("Failed to opendir %s: %s"), $f, $!);
Jeff Kingaa548922008-01-18 09:19:36 -0500626
Ævar Arnfjörð Bjarmason89bf1ba2010-09-14 19:02:24 +0000627 push @files, grep { -f $_ } map { catfile($f, $_) }
Ævar Arnfjörð Bjarmasonc6038162010-09-30 13:42:54 +0000628 sort readdir $dh;
629 closedir $dh;
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700630 } elsif ((-f $f or -p $f) and !is_format_patch_arg($f)) {
Jeff Kingaa548922008-01-18 09:19:36 -0500631 push @files, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500632 } else {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100633 push @rev_list_opts, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500634 }
635}
636
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100637if (@rev_list_opts) {
Vasco Almeida46493102016-12-14 11:54:36 -0100638 die __("Cannot run git format-patch from outside a repository\n")
Jay Soffianeed6ca72009-02-14 23:32:13 -0500639 unless $repo;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100640 push @files, $repo->command('format-patch', '-o', tempdir(CLEANUP => 1), @rev_list_opts);
641}
642
Junio C Hamano531220b2016-03-17 22:40:05 -0700643@files = handle_backup_files(@files);
644
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500645if ($validate) {
Jeff Kingc764a0c2008-01-18 09:20:10 -0500646 foreach my $f (@files) {
Kevin Ballard300913b2008-06-25 15:44:40 -0700647 unless (-p $f) {
648 my $error = validate_patch($f);
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100649 $error and die sprintf(__("fatal: %s: %s\nwarning: no patches were sent\n"),
650 $f, $error);
Kevin Ballard300913b2008-06-25 15:44:40 -0700651 }
Jeff Kingc764a0c2008-01-18 09:20:10 -0500652 }
Jeff King747bbff2008-01-18 09:19:48 -0500653}
654
Jeff Kingaa548922008-01-18 09:19:36 -0500655if (@files) {
656 unless ($quiet) {
657 print $_,"\n" for (@files);
658 }
659} else {
Vasco Almeida46493102016-12-14 11:54:36 -0100660 print STDERR __("\nNo patch files specified!\n\n");
Jeff Kingaa548922008-01-18 09:19:36 -0500661 usage();
662}
663
Ævar Arnfjörð Bjarmasonacf071b2010-09-30 13:42:57 +0000664sub get_patch_subject {
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100665 my $fn = shift;
666 open (my $fh, '<', $fn);
667 while (my $line = <$fh>) {
668 next unless ($line =~ /^Subject: (.*)$/);
669 close $fh;
670 return "GIT: $1\n";
671 }
672 close $fh;
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100673 die sprintf(__("No subject line in %s?"), $fn);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100674}
675
676if ($compose) {
677 # Note that this does not need to be secure, but we will make a small
678 # effort to have it be unique
Jay Soffianafe756c2009-02-23 13:51:37 -0500679 $compose_filename = ($repo ?
680 tempfile(".gitsendemail.msg.XXXXXX", DIR => $repo->repo_path()) :
681 tempfile(".gitsendemail.msg.XXXXXX", DIR => "."))[1];
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000682 open my $c, ">", $compose_filename
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100683 or die sprintf(__("Failed to open for writing %s: %s"), $compose_filename, $!);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100684
685
686 my $tpl_sender = $sender || $repoauthor || $repocommitter || '';
687 my $tpl_subject = $initial_subject || '';
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100688 my $tpl_in_reply_to = $initial_in_reply_to || '';
Christian Ludwigd11c9432018-03-04 00:58:14 +0100689 my $tpl_reply_to = $reply_to || '';
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100690
Vasco Almeida70aedfb2016-12-14 11:54:38 -0100691 print $c <<EOT1, Git::prefix_lines("GIT: ", __ <<EOT2), <<EOT3;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100692From $tpl_sender # This line is ignored.
Vasco Almeida70aedfb2016-12-14 11:54:38 -0100693EOT1
694Lines beginning in "GIT:" will be removed.
695Consider including an overall diffstat or table of contents
696for the patch you are writing.
697
698Clear the body content if you don't wish to send a summary.
699EOT2
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100700From: $tpl_sender
Christian Ludwigd11c9432018-03-04 00:58:14 +0100701Reply-To: $tpl_reply_to
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100702Subject: $tpl_subject
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100703In-Reply-To: $tpl_in_reply_to
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100704
Vasco Almeida70aedfb2016-12-14 11:54:38 -0100705EOT3
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100706 for my $f (@files) {
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000707 print $c get_patch_subject($f);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100708 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000709 close $c;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100710
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100711 if ($annotate) {
712 do_edit($compose_filename, @files);
713 } else {
714 do_edit($compose_filename);
715 }
716
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000717 open $c, "<", $compose_filename
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100718 or die sprintf(__("Failed to open %s: %s"), $compose_filename, $!);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100719
Krzysztof Mazur4a47a4d2012-10-22 14:41:48 +0200720 if (!defined $compose_encoding) {
721 $compose_encoding = "UTF-8";
722 }
Nathan Payreb6049542017-12-15 16:33:39 +0100723
724 my %parsed_email;
725 while (my $line = <$c>) {
726 next if $line =~ m/^GIT:/;
727 parse_header_line($line, \%parsed_email);
728 if ($line =~ /^$/) {
729 $parsed_email{'body'} = filter_body($c);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100730 }
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100731 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000732 close $c;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100733
Nathan Payreb6049542017-12-15 16:33:39 +0100734 open my $c2, ">", $compose_filename . ".final"
735 or die sprintf(__("Failed to open %s.final: %s"), $compose_filename, $!);
736
737
738 if ($parsed_email{'From'}) {
739 $sender = delete($parsed_email{'From'});
740 }
741 if ($parsed_email{'In-Reply-To'}) {
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100742 $initial_in_reply_to = delete($parsed_email{'In-Reply-To'});
Nathan Payreb6049542017-12-15 16:33:39 +0100743 }
Christian Ludwigd11c9432018-03-04 00:58:14 +0100744 if ($parsed_email{'Reply-To'}) {
745 $reply_to = delete($parsed_email{'Reply-To'});
Nathan Payreb6049542017-12-15 16:33:39 +0100746 }
747 if ($parsed_email{'Subject'}) {
748 $initial_subject = delete($parsed_email{'Subject'});
749 print $c2 "Subject: " .
750 quote_subject($initial_subject, $compose_encoding) .
751 "\n";
752 }
753
754 if ($parsed_email{'MIME-Version'}) {
755 print $c2 "MIME-Version: $parsed_email{'MIME-Version'}\n",
756 "Content-Type: $parsed_email{'Content-Type'};\n",
757 "Content-Transfer-Encoding: $parsed_email{'Content-Transfer-Encoding'}\n";
758 delete($parsed_email{'MIME-Version'});
759 delete($parsed_email{'Content-Type'});
760 delete($parsed_email{'Content-Transfer-Encoding'});
761 } elsif (file_has_nonascii($compose_filename)) {
762 my $content_type = (delete($parsed_email{'Content-Type'}) or
763 "text/plain; charset=$compose_encoding");
764 print $c2 "MIME-Version: 1.0\n",
765 "Content-Type: $content_type\n",
766 "Content-Transfer-Encoding: 8bit\n";
767 }
768 # Preserve unknown headers
769 foreach my $key (keys %parsed_email) {
770 next if $key eq 'body';
771 print $c2 "$key: $parsed_email{$key}";
772 }
773
774 if ($parsed_email{'body'}) {
775 print $c2 "\n$parsed_email{'body'}\n";
776 delete($parsed_email{'body'});
777 } else {
Vasco Almeida46493102016-12-14 11:54:36 -0100778 print __("Summary email is empty, skipping it\n");
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100779 $compose = -1;
780 }
Nathan Payreb6049542017-12-15 16:33:39 +0100781
782 close $c2;
783
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100784} elsif ($annotate) {
785 do_edit(@files);
786}
787
Jay Soffian6e182512009-03-28 21:39:10 -0400788sub ask {
789 my ($prompt, %arg) = @_;
Jay Soffian0da43a62009-04-04 23:23:21 -0400790 my $valid_re = $arg{valid_re};
Jay Soffian6e182512009-03-28 21:39:10 -0400791 my $default = $arg{default};
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700792 my $confirm_only = $arg{confirm_only};
Jay Soffian6e182512009-03-28 21:39:10 -0400793 my $resp;
794 my $i = 0;
Jay Soffian5906f542009-03-31 12:22:11 -0400795 return defined $default ? $default : undef
796 unless defined $term->IN and defined fileno($term->IN) and
797 defined $term->OUT and defined fileno($term->OUT);
Jay Soffian6e182512009-03-28 21:39:10 -0400798 while ($i++ < 10) {
799 $resp = $term->readline($prompt);
800 if (!defined $resp) { # EOF
801 print "\n";
802 return defined $default ? $default : undef;
803 }
804 if ($resp eq '' and defined $default) {
805 return $default;
806 }
Jay Soffian0da43a62009-04-04 23:23:21 -0400807 if (!defined $valid_re or $resp =~ /$valid_re/) {
Jay Soffian6e182512009-03-28 21:39:10 -0400808 return $resp;
809 }
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700810 if ($confirm_only) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100811 my $yesno = $term->readline(
812 # TRANSLATORS: please keep [y/N] as is.
813 sprintf(__("Are you sure you want to use <%s> [y/N]? "), $resp));
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700814 if (defined $yesno && $yesno =~ /y/i) {
815 return $resp;
816 }
817 }
Jay Soffian6e182512009-03-28 21:39:10 -0400818 }
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -0700819 return;
Jay Soffian6e182512009-03-28 21:39:10 -0400820}
821
Nathan Payreb6049542017-12-15 16:33:39 +0100822sub parse_header_line {
823 my $lines = shift;
824 my $parsed_line = shift;
825 my $addr_pat = join "|", qw(To Cc Bcc);
826
827 foreach (split(/\n/, $lines)) {
828 if (/^($addr_pat):\s*(.+)$/i) {
829 $parsed_line->{$1} = [ parse_address_line($2) ];
830 } elsif (/^([^:]*):\s*(.+)\s*$/i) {
831 $parsed_line->{$1} = $2;
832 }
833 }
834}
835
836sub filter_body {
837 my $c = shift;
838 my $body = "";
839 while (my $body_line = <$c>) {
840 if ($body_line !~ m/^GIT:/) {
841 $body .= $body_line;
842 }
843 }
844 return $body;
845}
846
847
Thomas Rast3cae7e52010-06-17 22:10:39 +0200848my %broken_encoding;
849
Ævar Arnfjörð Bjarmason1d50bfd2010-09-30 13:42:58 +0000850sub file_declares_8bit_cte {
Thomas Rast3cae7e52010-06-17 22:10:39 +0200851 my $fn = shift;
852 open (my $fh, '<', $fn);
853 while (my $line = <$fh>) {
854 last if ($line =~ /^$/);
855 return 1 if ($line =~ /^Content-Transfer-Encoding: .*8bit.*$/);
856 }
857 close $fh;
858 return 0;
859}
860
861foreach my $f (@files) {
862 next unless (body_or_subject_has_nonascii($f)
863 && !file_declares_8bit_cte($f));
864 $broken_encoding{$f} = 1;
865}
866
867if (!defined $auto_8bit_encoding && scalar %broken_encoding) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100868 print __("The following files are 8bit, but do not declare " .
869 "a Content-Transfer-Encoding.\n");
Thomas Rast3cae7e52010-06-17 22:10:39 +0200870 foreach my $f (sort keys %broken_encoding) {
871 print " $f\n";
872 }
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100873 $auto_8bit_encoding = ask(__("Which 8bit encoding should I declare [UTF-8]? "),
Junio C Hamano852a15d2015-02-13 12:20:25 -0800874 valid_re => qr/.{4}/, confirm_only => 1,
Thomas Rast3cae7e52010-06-17 22:10:39 +0200875 default => "UTF-8");
876}
877
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200878if (!$force) {
879 for my $f (@files) {
Ævar Arnfjörð Bjarmason0d290a42010-09-30 13:43:01 +0000880 if (get_patch_subject($f) =~ /\Q*** SUBJECT HERE ***\E/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100881 die sprintf(__("Refusing to send because the patch\n\t%s\n"
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200882 . "has the template subject '*** SUBJECT HERE ***'. "
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100883 . "Pass --force if you really want to send.\n"), $f);
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200884 }
885 }
886}
887
Remi Lespinetc46e27a2015-06-30 14:16:47 +0200888if (defined $sender) {
Remi Lespinetfa5b1aa2015-06-30 14:16:51 +0200889 $sender =~ s/^\s+|\s+$//g;
Remi Lespinetc46e27a2015-06-30 14:16:47 +0200890 ($sender) = expand_aliases($sender);
891} else {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100892 $sender = $repoauthor || $repocommitter || '';
Ryan Anderson83b24432005-07-31 04:17:25 -0400893}
894
Michael S. Tsirkinda187592013-06-05 21:11:00 +0300895# $sender could be an already sanitized address
896# (e.g. sendemail.from could be manually sanitized by user).
897# But it's a no-op to run sanitize_address on an already sanitized address.
898$sender = sanitize_address($sender);
899
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100900my $to_whom = __("To whom should the emails be sent (if anyone)?");
Felipe Contreras8cac13d2012-11-24 12:16:19 +0100901my $prompting = 0;
Junio C Hamano8796ff72010-10-26 22:02:03 -0700902if (!@initial_to && !defined $to_cmd) {
Junio C Hamano0d6b21e2016-04-24 12:31:44 -0700903 my $to = ask("$to_whom ",
Stephen Boyd61837492012-09-06 11:31:11 -0700904 default => "",
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700905 valid_re => qr/\@.*\./, confirm_only => 1);
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700906 push @initial_to, parse_address_line($to) if defined $to; # sanitized/validated later
Ryan Anderson1f038a02005-09-05 01:13:07 -0400907 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -0400908}
909
Eric Wong994d6c62006-05-14 19:13:44 -0700910sub expand_aliases {
Jeff King302e04e2009-07-23 07:09:29 -0400911 return map { expand_one_alias($_) } @_;
912}
913
914my %EXPANDED_ALIASES;
915sub expand_one_alias {
916 my $alias = shift;
917 if ($EXPANDED_ALIASES{$alias}) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100918 die sprintf(__("fatal: alias '%s' expands to itself\n"), $alias);
Jeff King302e04e2009-07-23 07:09:29 -0400919 }
920 local $EXPANDED_ALIASES{$alias} = 1;
921 return $aliases{$alias} ? expand_aliases(@{$aliases{$alias}}) : $alias;
Eric Wong994d6c62006-05-14 19:13:44 -0700922}
923
Remi Lespinetb5e112d2015-06-30 14:16:45 +0200924@initial_to = process_address_list(@initial_to);
925@initial_cc = process_address_list(@initial_cc);
926@bcclist = process_address_list(@bcclist);
Eric Wong994d6c62006-05-14 19:13:44 -0700927
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100928if ($thread && !defined $initial_in_reply_to && $prompting) {
929 $initial_in_reply_to = ask(
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100930 __("Message-ID to be used as In-Reply-To for the first email (if any)? "),
Stephen Boyd61837492012-09-06 11:31:11 -0700931 default => "",
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700932 valid_re => qr/\@.*\./, confirm_only => 1);
Ryan Anderson83b24432005-07-31 04:17:25 -0400933}
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100934if (defined $initial_in_reply_to) {
935 $initial_in_reply_to =~ s/^\s*<?//;
936 $initial_in_reply_to =~ s/>?\s*$//;
937 $initial_in_reply_to = "<$initial_in_reply_to>" if $initial_in_reply_to ne '';
Junio C Hamanoace9c2a2007-12-10 21:44:42 -0800938}
Mike Hommeyace72082007-12-09 18:17:28 +0100939
Christian Ludwigd11c9432018-03-04 00:58:14 +0100940if (defined $reply_to) {
941 $reply_to =~ s/^\s+|\s+$//g;
942 ($reply_to) = expand_aliases($reply_to);
943 $reply_to = sanitize_address($reply_to);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900944}
Eric Wongaca7ad72006-05-15 02:34:44 -0700945
946if (!defined $smtp_server) {
Florian Klink1ab2fd42017-11-28 01:49:04 +0100947 my @sendmail_paths = qw( /usr/sbin/sendmail /usr/lib/sendmail );
948 push @sendmail_paths, map {"$_/sendmail"} split /:/, $ENV{PATH};
949 foreach (@sendmail_paths) {
Eric Wongaca7ad72006-05-15 02:34:44 -0700950 if (-x $_) {
951 $smtp_server = $_;
952 last;
953 }
954 }
955 $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
Ryan Anderson3342d852005-07-31 20:04:24 -0400956}
957
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500958if ($compose && $compose > 0) {
959 @files = ($compose_filename . ".final", @files);
Ryan Anderson1f038a02005-09-05 01:13:07 -0400960}
961
Ryan Anderson83b24432005-07-31 04:17:25 -0400962# Variables we set as part of the loop over files
Christian Ludwig15dc3b92018-03-04 00:58:13 +0100963our ($message_id, %mail, $subject, $in_reply_to, $references, $message,
Jay Soffiandc1460a2009-03-31 12:22:12 -0400964 $needs_confirm, $message_num, $ask_default);
Ryan Anderson83b24432005-07-31 04:17:25 -0400965
Eric Wong567ffeb2006-03-25 16:47:12 -0800966sub extract_valid_address {
967 my $address = shift;
Ævar Arnfjörð Bjarmason35b6ab92010-09-30 19:03:31 +0000968 my $local_part_regexp = qr/[^<>"\s@]+/;
969 my $domain_regexp = qr/[^.<>"\s@]+(?:\.[^.<>"\s@]+)+/;
Eric Wongdb3106b2006-05-15 02:41:01 -0700970
971 # check for a local address:
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700972 return $address if ($address =~ /^($local_part_regexp)$/);
Eric Wongdb3106b2006-05-15 02:41:01 -0700973
Uwe Kleine-König155197e2007-08-09 15:27:57 +0200974 $address =~ s/^\s*<(.*)>\s*$/$1/;
Eric Wong567ffeb2006-03-25 16:47:12 -0800975 if ($have_email_valid) {
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700976 return scalar Email::Valid->address($address);
Eric Wong567ffeb2006-03-25 16:47:12 -0800977 }
Krzysztof Mazur95c0d4b2012-11-22 19:12:09 +0100978
979 # less robust/correct than the monster regexp in Email::Valid,
980 # but still does a 99% job, and one less dependency
981 return $1 if $address =~ /($local_part_regexp\@$domain_regexp)/;
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -0700982 return;
Eric Wong567ffeb2006-03-25 16:47:12 -0800983}
Ryan Anderson83b24432005-07-31 04:17:25 -0400984
Krzysztof Mazure4312252012-11-22 19:12:10 +0100985sub extract_valid_address_or_die {
986 my $address = shift;
987 $address = extract_valid_address($address);
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100988 die sprintf(__("error: unable to extract a valid address from: %s\n"), $address)
Krzysztof Mazure4312252012-11-22 19:12:10 +0100989 if !$address;
990 return $address;
991}
992
993sub validate_address {
994 my $address = shift;
Krzysztof Mazurd0e98102012-11-22 19:12:12 +0100995 while (!extract_valid_address($address)) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100996 printf STDERR __("error: unable to extract a valid address from: %s\n"), $address;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100997 # TRANSLATORS: Make sure to include [q] [d] [e] in your
998 # translation. The program will only accept English input
999 # at this point.
1000 $_ = ask(__("What to do with this address? ([q]uit|[d]rop|[e]dit): "),
Krzysztof Mazurd0e98102012-11-22 19:12:12 +01001001 valid_re => qr/^(?:quit|q|drop|d|edit|e)/i,
Krzysztof Mazur5c80afe2012-11-22 19:12:11 +01001002 default => 'q');
1003 if (/^d/i) {
1004 return undef;
1005 } elsif (/^q/i) {
1006 cleanup_compose_files();
1007 exit(0);
1008 }
Junio C Hamano0d6b21e2016-04-24 12:31:44 -07001009 $address = ask("$to_whom ",
Krzysztof Mazurd0e98102012-11-22 19:12:12 +01001010 default => "",
1011 valid_re => qr/\@.*\./, confirm_only => 1);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001012 }
1013 return $address;
1014}
1015
1016sub validate_address_list {
1017 return (grep { defined $_ }
1018 map { validate_address($_) } @_);
Ryan Anderson83b24432005-07-31 04:17:25 -04001019}
1020
1021# Usually don't need to change anything below here.
1022
1023# we make a "fake" message id by taking the current number
1024# of seconds since the beginning of Unix time and tacking on
1025# a random number to the end, in case we are called quicker than
1026# 1 second since the last time we were called.
Ryan Anderson8037d1a2005-07-31 20:04:24 -04001027
1028# We'll setup a template for the message id, using the "from" address:
Ryan Anderson8037d1a2005-07-31 20:04:24 -04001029
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001030my ($message_id_stamp, $message_id_serial);
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001031sub make_message_id {
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001032 my $uniq;
1033 if (!defined $message_id_stamp) {
Eric Wongf916ab02016-04-06 20:07:14 +00001034 $message_id_stamp = strftime("%Y%m%d%H%M%S.$$", gmtime(time));
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001035 $message_id_serial = 0;
1036 }
1037 $message_id_serial++;
1038 $uniq = "$message_id_stamp-$message_id_serial";
1039
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001040 my $du_part;
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001041 for ($sender, $repocommitter, $repoauthor) {
1042 $du_part = extract_valid_address(sanitize_address($_));
1043 last if (defined $du_part and $du_part ne '');
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001044 }
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001045 if (not defined $du_part or $du_part eq '') {
Ævar Arnfjörð Bjarmason529dd382010-09-30 13:43:08 +00001046 require Sys::Hostname;
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001047 $du_part = 'user@' . Sys::Hostname::hostname();
1048 }
Eric Wongf916ab02016-04-06 20:07:14 +00001049 my $message_id_template = "<%s-%s>";
Junio C Hamanobe510cf2007-09-17 21:18:20 -07001050 $message_id = sprintf($message_id_template, $uniq, $du_part);
Ryan Anderson8037d1a2005-07-31 20:04:24 -04001051 #print "new message id = $message_id\n"; # Was useful for debugging
Ryan Anderson83b24432005-07-31 04:17:25 -04001052}
1053
1054
1055
Eric Wonga5370b12006-03-25 03:01:01 -08001056$time = time - scalar $#files;
Ryan Anderson83b24432005-07-31 04:17:25 -04001057
Jürgen Rühle374c5902007-01-10 13:36:39 -08001058sub unquote_rfc2047 {
1059 local ($_) = @_;
Роман Донченко11f70a72014-12-14 18:59:46 +03001060 my $charset;
Роман Донченкоab47e2a2014-12-14 18:59:47 +03001061 my $sep = qr/[ \t]+/;
1062 s{$re_encoded_word(?:$sep$re_encoded_word)*}{
1063 my @words = split $sep, $&;
1064 foreach (@words) {
1065 m/$re_encoded_word/;
1066 $charset = $1;
1067 my $encoding = $2;
1068 my $text = $3;
1069 if ($encoding eq 'q' || $encoding eq 'Q') {
1070 $_ = $text;
1071 s/_/ /g;
1072 s/=([0-9A-F]{2})/chr(hex($1))/egi;
1073 } else {
1074 # other encodings not supported yet
1075 }
Роман Донченко11f70a72014-12-14 18:59:46 +03001076 }
Роман Донченкоab47e2a2014-12-14 18:59:47 +03001077 join '', @words;
Thomas Rastb622d4d2012-07-30 21:25:40 +02001078 }eg;
Роман Донченко11f70a72014-12-14 18:59:46 +03001079 return wantarray ? ($_, $charset) : $_;
Jürgen Rühle374c5902007-01-10 13:36:39 -08001080}
1081
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001082sub quote_rfc2047 {
1083 local $_ = shift;
Brandon Caseyd1fff6f2009-06-06 20:12:31 -05001084 my $encoding = shift || 'UTF-8';
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001085 s/([^-a-zA-Z0-9!*+\/])/sprintf("=%02X", ord($1))/eg;
1086 s/(.*)/=\?$encoding\?q\?$1\?=/;
1087 return $_;
1088}
1089
Brandon Caseya3a82622009-06-07 19:25:58 -05001090sub is_rfc2047_quoted {
1091 my $s = shift;
Brandon Caseya3a82622009-06-07 19:25:58 -05001092 length($s) <= 75 &&
Роман Донченко11f70a72014-12-14 18:59:46 +03001093 $s =~ m/^(?:"[[:ascii:]]*"|$re_encoded_word)$/o;
Brandon Caseya3a82622009-06-07 19:25:58 -05001094}
1095
Krzysztof Mazurce547802012-10-24 23:08:26 +02001096sub subject_needs_rfc2047_quoting {
1097 my $s = shift;
1098
Krzysztof Mazurce1459f2012-10-24 23:28:29 +02001099 return ($s =~ /[^[:ascii:]]/) || ($s =~ /=\?/);
Krzysztof Mazurce547802012-10-24 23:08:26 +02001100}
1101
1102sub quote_subject {
1103 local $subject = shift;
1104 my $encoding = shift || 'UTF-8';
1105
1106 if (subject_needs_rfc2047_quoting($subject)) {
1107 return quote_rfc2047($subject, $encoding);
1108 }
1109 return $subject;
1110}
1111
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001112# use the simplest quoting being able to handle the recipient
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001113sub sanitize_address {
Robin H. Johnson732263d2007-04-25 19:37:19 -07001114 my ($recipient) = @_;
Krzysztof Mazur831a4882012-11-22 19:12:08 +01001115
1116 # remove garbage after email address
1117 $recipient =~ s/(.*>).*$/$1/;
1118
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001119 my ($recipient_name, $recipient_addr) = ($recipient =~ /^(.*?)\s*(<.*)/);
1120
1121 if (not $recipient_name) {
Ævar Arnfjörð Bjarmasonff483892010-09-30 13:43:02 +00001122 return $recipient;
Robin H. Johnson732263d2007-04-25 19:37:19 -07001123 }
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001124
1125 # if recipient_name is already quoted, do nothing
Brandon Caseya3a82622009-06-07 19:25:58 -05001126 if (is_rfc2047_quoted($recipient_name)) {
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001127 return $recipient;
1128 }
1129
Remi Lespinet1fe97032015-06-30 14:16:49 +02001130 # remove non-escaped quotes
1131 $recipient_name =~ s/(^|[^\\])"/$1/g;
1132
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001133 # rfc2047 is needed if a non-ascii char is included
1134 if ($recipient_name =~ /[^[:ascii:]]/) {
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001135 $recipient_name = quote_rfc2047($recipient_name);
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001136 }
1137
1138 # double quotes are needed if specials or CTLs are included
1139 elsif ($recipient_name =~ /[][()<>@,;:\\".\000-\037\177]/) {
Remi Lespinet1fe97032015-06-30 14:16:49 +02001140 $recipient_name =~ s/([\\\r])/\\$1/g;
Ævar Arnfjörð Bjarmasond5c7d692010-09-30 13:43:03 +00001141 $recipient_name = qq["$recipient_name"];
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001142 }
1143
1144 return "$recipient_name $recipient_addr";
1145
Robin H. Johnson732263d2007-04-25 19:37:19 -07001146}
1147
Matthieu Moycb2922f2017-08-23 12:21:01 +02001148sub strip_garbage_one_address {
1149 my ($addr) = @_;
1150 chomp $addr;
1151 if ($addr =~ /^(("[^"]*"|[^"<]*)? *<[^>]*>).*/) {
1152 # "Foo Bar" <foobar@example.com> [possibly garbage here]
1153 # Foo Bar <foobar@example.com> [possibly garbage here]
1154 return $1;
1155 }
1156 if ($addr =~ /^(<[^>]*>).*/) {
1157 # <foo@example.com> [possibly garbage here]
1158 # if garbage contains other addresses, they are ignored.
1159 return $1;
1160 }
1161 if ($addr =~ /^([^"#,\s]*)/) {
1162 # address without quoting: remove anything after the address
1163 return $1;
1164 }
1165 return $addr;
1166}
1167
Krzysztof Mazure4312252012-11-22 19:12:10 +01001168sub sanitize_address_list {
1169 return (map { sanitize_address($_) } @_);
1170}
1171
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001172sub process_address_list {
Remi Lespinetb1c8a112015-06-30 14:16:50 +02001173 my @addr_list = map { parse_address_line($_) } @_;
1174 @addr_list = expand_aliases(@addr_list);
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001175 @addr_list = sanitize_address_list(@addr_list);
1176 @addr_list = validate_address_list(@addr_list);
1177 return @addr_list;
1178}
1179
Jari Aalto134550f2010-03-14 17:16:45 +02001180# Returns the local Fully Qualified Domain Name (FQDN) if available.
1181#
1182# Tightly configured MTAa require that a caller sends a real DNS
1183# domain name that corresponds the IP address in the HELO/EHLO
1184# handshake. This is used to verify the connection and prevent
1185# spammers from trying to hide their identity. If the DNS and IP don't
1186# match, the receiveing MTA may deny the connection.
1187#
1188# Here is a deny example of Net::SMTP with the default "localhost.localdomain"
1189#
1190# Net::SMTP=GLOB(0x267ec28)>>> EHLO localhost.localdomain
1191# Net::SMTP=GLOB(0x267ec28)<<< 550 EHLO argument does not match calling host
1192#
1193# This maildomain*() code is based on ideas in Perl library Test::Reporter
1194# /usr/share/perl5/Test/Reporter/Mail/Util.pm ==> sub _maildomain ()
1195
Brian Gernhardt59a86302010-04-10 10:53:54 -04001196sub valid_fqdn {
1197 my $domain = shift;
Brandon Casey61ef5e92010-09-26 22:18:01 -05001198 return defined $domain && !($^O eq 'darwin' && $domain =~ /\.local$/) && $domain =~ /\./;
Brian Gernhardt59a86302010-04-10 10:53:54 -04001199}
1200
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001201sub maildomain_net {
Jari Aalto134550f2010-03-14 17:16:45 +02001202 my $maildomain;
1203
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001204 my $domain = Net::Domain::domainname();
1205 $maildomain = $domain if valid_fqdn($domain);
Jari Aalto134550f2010-03-14 17:16:45 +02001206
1207 return $maildomain;
1208}
1209
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001210sub maildomain_mta {
Jari Aalto134550f2010-03-14 17:16:45 +02001211 my $maildomain;
1212
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001213 for my $host (qw(mailhost localhost)) {
1214 my $smtp = Net::SMTP->new($host);
1215 if (defined $smtp) {
1216 my $domain = $smtp->domain;
1217 $smtp->quit;
Jari Aalto134550f2010-03-14 17:16:45 +02001218
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001219 $maildomain = $domain if valid_fqdn($domain);
Jari Aalto134550f2010-03-14 17:16:45 +02001220
Ævar Arnfjörð Bjarmason1046c112018-03-03 15:38:10 +00001221 last if $maildomain;
Jari Aalto134550f2010-03-14 17:16:45 +02001222 }
1223 }
1224
1225 return $maildomain;
1226}
1227
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001228sub maildomain {
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001229 return maildomain_net() || maildomain_mta() || 'localhost.localdomain';
Jari Aalto134550f2010-03-14 17:16:45 +02001230}
1231
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001232sub smtp_host_string {
1233 if (defined $smtp_server_port) {
1234 return "$smtp_server:$smtp_server_port";
1235 } else {
1236 return $smtp_server;
1237 }
1238}
1239
1240# Returns 1 if authentication succeeded or was not necessary
1241# (smtp_user was not specified), and 0 otherwise.
1242
1243sub smtp_auth_maybe {
1244 if (!defined $smtp_authuser || $auth) {
1245 return 1;
1246 }
1247
1248 # Workaround AUTH PLAIN/LOGIN interaction defect
1249 # with Authen::SASL::Cyrus
1250 eval {
1251 require Authen::SASL;
1252 Authen::SASL->import(qw(Perl));
1253 };
1254
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001255 # Check mechanism naming as defined in:
1256 # https://tools.ietf.org/html/rfc4422#page-8
Brian Norris904f6e72015-09-18 15:12:50 -07001257 if ($smtp_auth && $smtp_auth !~ /^(\b[A-Z0-9-_]{1,20}\s*)*$/) {
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001258 die "invalid smtp auth: '${smtp_auth}'";
1259 }
1260
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001261 # TODO: Authentication may fail not because credentials were
1262 # invalid but due to other reasons, in which we should not
1263 # reject credentials.
1264 $auth = Git::credential({
1265 'protocol' => 'smtp',
1266 'host' => smtp_host_string(),
1267 'username' => $smtp_authuser,
1268 # if there's no password, "git credential fill" will
1269 # give us one, otherwise it'll just pass this one.
1270 'password' => $smtp_authpass
1271 }, sub {
1272 my $cred = shift;
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001273
1274 if ($smtp_auth) {
1275 my $sasl = Authen::SASL->new(
1276 mechanism => $smtp_auth,
1277 callback => {
1278 user => $cred->{'username'},
1279 pass => $cred->{'password'},
1280 authname => $cred->{'username'},
1281 }
1282 );
1283
1284 return !!$smtp->auth($sasl);
1285 }
1286
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001287 return !!$smtp->auth($cred->{'username'}, $cred->{'password'});
1288 });
1289
1290 return $auth;
1291}
1292
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001293sub ssl_verify_params {
1294 eval {
1295 require IO::Socket::SSL;
1296 IO::Socket::SSL->import(qw/SSL_VERIFY_PEER SSL_VERIFY_NONE/);
1297 };
1298 if ($@) {
1299 print STDERR "Not using SSL_VERIFY_PEER due to out-of-date IO::Socket::SSL.\n";
1300 return;
1301 }
1302
1303 if (!defined $smtp_ssl_cert_path) {
Ruben Kerkhof01645b72014-01-15 21:31:11 +04001304 # use the OpenSSL defaults
1305 return (SSL_verify_mode => SSL_VERIFY_PEER());
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001306 }
1307
1308 if ($smtp_ssl_cert_path eq "") {
1309 return (SSL_verify_mode => SSL_VERIFY_NONE());
1310 } elsif (-d $smtp_ssl_cert_path) {
1311 return (SSL_verify_mode => SSL_VERIFY_PEER(),
1312 SSL_ca_path => $smtp_ssl_cert_path);
1313 } elsif (-f $smtp_ssl_cert_path) {
1314 return (SSL_verify_mode => SSL_VERIFY_PEER(),
1315 SSL_ca_file => $smtp_ssl_cert_path);
1316 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001317 die sprintf(__("CA path \"%s\" does not exist"), $smtp_ssl_cert_path);
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001318 }
1319}
1320
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001321sub file_name_is_absolute {
1322 my ($path) = @_;
1323
1324 # msys does not grok DOS drive-prefixes
1325 if ($^O eq 'msys') {
Junio C Hamanof24ecf52014-04-23 09:37:38 -07001326 return ($path =~ m#^/# || $path =~ m#^[a-zA-Z]\:#)
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001327 }
1328
1329 require File::Spec::Functions;
1330 return File::Spec::Functions::file_name_is_absolute($path);
1331}
1332
Michael Witten15da1082009-04-13 13:23:51 -05001333# Returns 1 if the message was sent, and 0 otherwise.
Markus Heidelberga1b5b372009-06-12 12:51:42 +02001334# In actuality, the whole program dies when there
Michael Witten15da1082009-04-13 13:23:51 -05001335# is an error sending a message.
1336
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001337sub send_message {
Eric Wong4bc87a22006-03-25 17:20:48 -08001338 my @recipients = unique_email_list(@to);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001339 @cc = (grep { my $cc = extract_valid_address_or_die($_);
Joe Perches83acaae2010-11-20 15:06:05 -08001340 not grep { $cc eq $_ || $_ =~ /<\Q${cc}\E>$/ } @recipients
Ask Bjørn Hansen7ac17522007-11-19 03:00:26 -08001341 }
Ask Bjørn Hansen7ac17522007-11-19 03:00:26 -08001342 @cc);
Eric Wong4bc87a22006-03-25 17:20:48 -08001343 my $to = join (",\n\t", @recipients);
Ryan Anderson58063242006-05-29 12:30:13 -07001344 @recipients = unique_email_list(@recipients,@cc,@bcclist);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001345 @recipients = (map { extract_valid_address_or_die($_) } @recipients);
Jakub Narebski6bdca892006-07-07 20:57:55 +02001346 my $date = format_2822_time($time++);
Martin Langhoffe923eff2006-05-03 09:44:36 +12001347 my $gitversion = '@@GIT_VERSION@@';
1348 if ($gitversion =~ m/..GIT_VERSION../) {
Petr Baudis3cb8caf2006-07-03 22:47:58 +02001349 $gitversion = Git::version();
Martin Langhoffe923eff2006-05-03 09:44:36 +12001350 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001351
Joe Perches02461e02009-10-08 10:03:26 -07001352 my $cc = join(",\n\t", unique_email_list(@cc));
Junio C Hamanof06a6a42007-04-16 16:51:47 -07001353 my $ccline = "";
1354 if ($cc ne '') {
1355 $ccline = "\nCc: $cc";
1356 }
Jeff King4f3d3702007-12-17 15:51:34 -05001357 make_message_id() unless defined($message_id);
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001358
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001359 my $header = "From: $sender
Junio C Hamanof06a6a42007-04-16 16:51:47 -07001360To: $to${ccline}
Eric Wong4bc87a22006-03-25 17:20:48 -08001361Subject: $subject
Eric Wong4bc87a22006-03-25 17:20:48 -08001362Date: $date
1363Message-Id: $message_id
Eric Wong4bc87a22006-03-25 17:20:48 -08001364";
Luis Henriquesac1596a2014-03-24 21:38:27 +00001365 if ($use_xmailer) {
1366 $header .= "X-Mailer: git-send-email $gitversion\n";
1367 }
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001368 if ($in_reply_to) {
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001369
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001370 $header .= "In-Reply-To: $in_reply_to\n";
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001371 $header .= "References: $references\n";
1372 }
Christian Ludwigd11c9432018-03-04 00:58:14 +01001373 if ($reply_to) {
1374 $header .= "Reply-To: $reply_to\n";
1375 }
Junio C Hamanoce91c2f2006-10-05 16:36:49 -07001376 if (@xh) {
1377 $header .= join("\n", @xh) . "\n";
1378 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001379
Robin H. Johnsonc38f0242007-04-25 19:37:20 -07001380 my @sendmail_parameters = ('-i', @recipients);
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001381 my $raw_from = $sender;
Felipe Contrerasc89e3242009-11-26 21:04:29 +02001382 if (defined $envelope_sender && $envelope_sender ne "auto") {
1383 $raw_from = $envelope_sender;
1384 }
Robin H. Johnsonf073a592007-04-25 19:37:22 -07001385 $raw_from = extract_valid_address($raw_from);
1386 unshift (@sendmail_parameters,
1387 '-f', $raw_from) if(defined $envelope_sender);
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001388
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001389 if ($needs_confirm && !$dry_run) {
1390 print "\n$header\n";
1391 if ($needs_confirm eq "inform") {
1392 $confirm_unconfigured = 0; # squelch this message for the rest of this run
Jay Soffian6e182512009-03-28 21:39:10 -04001393 $ask_default = "y"; # assume yes on EOF since user hasn't explicitly asked for confirmation
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001394 print __ <<EOF ;
1395 The Cc list above has been expanded by additional
1396 addresses found in the patch commit message. By default
1397 send-email prompts before sending whenever this occurs.
1398 This behavior is controlled by the sendemail.confirm
1399 configuration setting.
1400
1401 For additional information, run 'git send-email --help'.
1402 To retain the current behavior, but squelch this message,
1403 run 'git config --global sendemail.confirm auto'.
1404
1405EOF
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001406 }
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001407 # TRANSLATORS: Make sure to include [y] [n] [q] [a] in your
1408 # translation. The program will only accept English input
1409 # at this point.
1410 $_ = ask(__("Send this email? ([y]es|[n]o|[q]uit|[a]ll): "),
Jay Soffian6e182512009-03-28 21:39:10 -04001411 valid_re => qr/^(?:yes|y|no|n|quit|q|all|a)/i,
1412 default => $ask_default);
Vasco Almeida46493102016-12-14 11:54:36 -01001413 die __("Send this email reply required") unless defined $_;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001414 if (/^n/i) {
Michael Witten15da1082009-04-13 13:23:51 -05001415 return 0;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001416 } elsif (/^q/i) {
1417 cleanup_compose_files();
1418 exit(0);
1419 } elsif (/^a/i) {
1420 $confirm = 'never';
1421 }
1422 }
1423
Pascal Obry052fbea2010-09-06 20:12:11 +02001424 unshift (@sendmail_parameters, @smtp_server_options);
1425
Matthew Wilcox61302592006-10-10 08:58:23 -06001426 if ($dry_run) {
1427 # We don't want to send the email.
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001428 } elsif (file_name_is_absolute($smtp_server)) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001429 my $pid = open my $sm, '|-';
1430 defined $pid or die $!;
1431 if (!$pid) {
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001432 exec($smtp_server, @sendmail_parameters) or die $!;
Eric Wongaca7ad72006-05-15 02:34:44 -07001433 }
1434 print $sm "$header\n$message";
Ævar Arnfjörð Bjarmason5e2c2ab2010-09-30 13:43:07 +00001435 close $sm or die $!;
Eric Wongaca7ad72006-05-15 02:34:44 -07001436 } else {
Junio C Hamano44b24762007-09-25 17:27:54 -07001437
1438 if (!defined $smtp_server) {
Vasco Almeida46493102016-12-14 11:54:36 -01001439 die __("The required SMTP server is not properly defined.")
Junio C Hamano44b24762007-09-25 17:27:54 -07001440 }
1441
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001442 require Net::SMTP;
Jonathan Niederbfbfc9a2017-05-31 17:17:43 -07001443 my $use_net_smtp_ssl = version->parse($Net::SMTP::VERSION) < version->parse("2.34");
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001444 $smtp_domain ||= maildomain();
1445
Thomas Rastf6bebd12008-06-25 21:42:43 +02001446 if ($smtp_encryption eq 'ssl') {
Junio C Hamano44b24762007-09-25 17:27:54 -07001447 $smtp_server_port ||= 465; # ssmtp
Thomas Rast5508f3e2013-12-01 23:48:43 +01001448 require IO::Socket::SSL;
John Keeping9d605242015-12-03 21:47:18 +00001449
1450 # Suppress "variable accessed once" warning.
1451 {
1452 no warnings 'once';
1453 $IO::Socket::SSL::DEBUG = 1;
1454 }
1455
Thomas Rast5508f3e2013-12-01 23:48:43 +01001456 # Net::SMTP::SSL->new() does not forward any SSL options
1457 IO::Socket::SSL::set_client_defaults(
1458 ssl_verify_params());
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001459
1460 if ($use_net_smtp_ssl) {
1461 require Net::SMTP::SSL;
1462 $smtp ||= Net::SMTP::SSL->new($smtp_server,
1463 Hello => $smtp_domain,
1464 Port => $smtp_server_port,
1465 Debug => $debug_net_smtp);
1466 }
1467 else {
1468 $smtp ||= Net::SMTP->new($smtp_server,
1469 Hello => $smtp_domain,
1470 Port => $smtp_server_port,
1471 Debug => $debug_net_smtp,
1472 SSL => 1);
1473 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001474 }
1475 else {
brian m. carlson1a741bf2013-07-04 22:04:52 +00001476 $smtp_server_port ||= 25;
1477 $smtp ||= Net::SMTP->new($smtp_server,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001478 Hello => $smtp_domain,
brian m. carlson1a741bf2013-07-04 22:04:52 +00001479 Debug => $debug_net_smtp,
1480 Port => $smtp_server_port);
Yakov Lernerfb3650e2009-09-25 15:10:21 -07001481 if ($smtp_encryption eq 'tls' && $smtp) {
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001482 if ($use_net_smtp_ssl) {
1483 $smtp->command('STARTTLS');
1484 $smtp->response();
1485 if ($smtp->code != 220) {
1486 die sprintf(__("Server does not support STARTTLS! %s"), $smtp->message);
1487 }
1488 require Net::SMTP::SSL;
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001489 $smtp = Net::SMTP::SSL->start_SSL($smtp,
1490 ssl_verify_params())
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001491 or die sprintf(__("STARTTLS failed! %s"), IO::Socket::SSL::errstr());
Thomas Rastf6bebd12008-06-25 21:42:43 +02001492 }
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001493 else {
1494 $smtp->starttls(ssl_verify_params())
1495 or die sprintf(__("STARTTLS failed! %s"), IO::Socket::SSL::errstr());
1496 }
1497 $smtp_encryption = '';
1498 # Send EHLO again to receive fresh
1499 # supported commands
1500 $smtp->hello($smtp_domain);
Thomas Rastf6bebd12008-06-25 21:42:43 +02001501 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001502 }
Junio C Hamano44b24762007-09-25 17:27:54 -07001503
1504 if (!$smtp) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001505 die __("Unable to initialize SMTP properly. Check config and use --smtp-debug."),
1506 " VALUES: server=$smtp_server ",
Jari Aaltoe5afb3a2010-03-14 17:15:33 +02001507 "encryption=$smtp_encryption ",
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001508 "hello=$smtp_domain",
Sylvain Rabota1dd7e12011-04-29 20:23:24 +02001509 defined $smtp_server_port ? " port=$smtp_server_port" : "";
Junio C Hamano44b24762007-09-25 17:27:54 -07001510 }
1511
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001512 smtp_auth_maybe or die $smtp->message;
Michael Witten2363d742008-02-03 19:53:56 -05001513
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001514 $smtp->mail( $raw_from ) or die $smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001515 $smtp->to( @recipients ) or die $smtp->message;
1516 $smtp->data or die $smtp->message;
Stefan Agnerf60c4832015-09-30 09:26:09 +02001517 $smtp->datasend("$header\n") or die $smtp->message;
1518 my @lines = split /^/, $message;
1519 foreach my $line (@lines) {
1520 $smtp->datasend("$line") or die $smtp->message;
1521 }
Eric Wongaca7ad72006-05-15 02:34:44 -07001522 $smtp->dataend() or die $smtp->message;
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001523 $smtp->code =~ /250|200/ or die sprintf(__("Failed to send %s\n"), $subject).$smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001524 }
Ryan Anderson27184352006-02-05 20:13:52 -05001525 if ($quiet) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001526 printf($dry_run ? __("Dry-Sent %s\n") : __("Sent %s\n"), $subject);
Ryan Anderson27184352006-02-05 20:13:52 -05001527 } else {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001528 print($dry_run ? __("Dry-OK. Log says:\n") : __("OK. Log says:\n"));
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001529 if (!file_name_is_absolute($smtp_server)) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001530 print "Server: $smtp_server\n";
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001531 print "MAIL FROM:<$raw_from>\n";
Joe Perches02461e02009-10-08 10:03:26 -07001532 foreach my $entry (@recipients) {
1533 print "RCPT TO:<$entry>\n";
1534 }
Eric Wongaca7ad72006-05-15 02:34:44 -07001535 } else {
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001536 print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001537 }
David D. Kilzerb7f30e02007-11-18 20:14:55 -08001538 print $header, "\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001539 if ($smtp) {
Vasco Almeida46493102016-12-14 11:54:36 -01001540 print __("Result: "), $smtp->code, ' ',
Eric Wongaca7ad72006-05-15 02:34:44 -07001541 ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
1542 } else {
Vasco Almeida46493102016-12-14 11:54:36 -01001543 print __("Result: OK\n");
Eric Wongaca7ad72006-05-15 02:34:44 -07001544 }
Ryan Anderson30d08b32006-02-02 11:56:06 -05001545 }
Michael Witten15da1082009-04-13 13:23:51 -05001546
1547 return 1;
Ryan Anderson83b24432005-07-31 04:17:25 -04001548}
1549
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001550$in_reply_to = $initial_in_reply_to;
1551$references = $initial_in_reply_to || '';
Ryan Anderson83b24432005-07-31 04:17:25 -04001552$subject = $initial_subject;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001553$message_num = 0;
Ryan Anderson83b24432005-07-31 04:17:25 -04001554
1555foreach my $t (@files) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001556 open my $fh, "<", $t or die sprintf(__("can't open file %s"), $t);
Ryan Anderson83b24432005-07-31 04:17:25 -04001557
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001558 my $author = undef;
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001559 my $sauthor = undef;
Jeff King8291db62007-11-16 05:49:09 -05001560 my $author_encoding;
1561 my $has_content_type;
1562 my $body_encoding;
Paolo Bonzinibb294562014-11-25 15:00:26 +01001563 my $xfer_encoding;
1564 my $has_mime_version;
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001565 @to = ();
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001566 @cc = ();
Junio C Hamanoce91c2f2006-10-05 16:36:49 -07001567 @xh = ();
Junio C Hamanoe6b09642006-10-07 03:09:05 -07001568 my $input_format = undef;
Jay Soffian50126992009-02-14 23:32:14 -05001569 my @header = ();
Ryan Anderson83b24432005-07-31 04:17:25 -04001570 $message = "";
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001571 $message_num++;
Jay Soffian50126992009-02-14 23:32:14 -05001572 # First unfold multiline header fields
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001573 while(<$fh>) {
Jay Soffian50126992009-02-14 23:32:14 -05001574 last if /^\s*$/;
1575 if (/^\s+\S/ and @header) {
1576 chomp($header[$#header]);
1577 s/^\s+/ /;
1578 $header[$#header] .= $_;
1579 } else {
1580 push(@header, $_);
1581 }
1582 }
1583 # Now parse the header
1584 foreach(@header) {
1585 if (/^From /) {
1586 $input_format = 'mbox';
1587 next;
1588 }
1589 chomp;
1590 if (!defined $input_format && /^[-A-Za-z]+:\s/) {
1591 $input_format = 'mbox';
1592 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001593
Jay Soffian50126992009-02-14 23:32:14 -05001594 if (defined $input_format && $input_format eq 'mbox') {
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001595 if (/^Subject:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001596 $subject = $1;
1597 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001598 elsif (/^From:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001599 ($author, $author_encoding) = unquote_rfc2047($1);
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001600 $sauthor = sanitize_address($author);
Jay Soffian50126992009-02-14 23:32:14 -05001601 next if $suppress_cc{'author'};
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001602 next if $suppress_cc{'self'} and $sauthor eq $sender;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001603 printf(__("(mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001604 $1, $_) unless $quiet;
1605 push @cc, $1;
1606 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001607 elsif (/^To:\s+(.*)$/i) {
Stephen Boyd21802cd2010-09-29 00:26:44 -07001608 foreach my $addr (parse_address_line($1)) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001609 printf(__("(mbox) Adding to: %s from line '%s'\n"),
Stephen Boyd21802cd2010-09-29 00:26:44 -07001610 $addr, $_) unless $quiet;
Krzysztof Mazure4312252012-11-22 19:12:10 +01001611 push @to, $addr;
Stephen Boyd21802cd2010-09-29 00:26:44 -07001612 }
1613 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001614 elsif (/^Cc:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001615 foreach my $addr (parse_address_line($1)) {
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001616 my $qaddr = unquote_rfc2047($addr);
1617 my $saddr = sanitize_address($qaddr);
1618 if ($saddr eq $sender) {
David Brown65648282007-12-25 19:56:29 -08001619 next if ($suppress_cc{'self'});
David Brown65648282007-12-25 19:56:29 -08001620 } else {
1621 next if ($suppress_cc{'cc'});
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001622 }
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001623 printf(__("(mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001624 $addr, $_) unless $quiet;
1625 push @cc, $addr;
Ryan Anderson83b24432005-07-31 04:17:25 -04001626 }
1627 }
Jay Soffian50126992009-02-14 23:32:14 -05001628 elsif (/^Content-type:/i) {
1629 $has_content_type = 1;
1630 if (/charset="?([^ "]+)/) {
1631 $body_encoding = $1;
1632 }
1633 push @xh, $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001634 }
Paolo Bonzinibb294562014-11-25 15:00:26 +01001635 elsif (/^MIME-Version/i) {
1636 $has_mime_version = 1;
1637 push @xh, $_;
1638 }
Jay Soffian50126992009-02-14 23:32:14 -05001639 elsif (/^Message-Id: (.*)/i) {
1640 $message_id = $1;
1641 }
Paolo Bonzinibb294562014-11-25 15:00:26 +01001642 elsif (/^Content-Transfer-Encoding: (.*)/i) {
1643 $xfer_encoding = $1 if not defined $xfer_encoding;
1644 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001645 elsif (!/^Date:\s/i && /^[-A-Za-z]+:\s+\S/) {
Jay Soffian50126992009-02-14 23:32:14 -05001646 push @xh, $_;
1647 }
1648
Ryan Anderson83b24432005-07-31 04:17:25 -04001649 } else {
Jay Soffian50126992009-02-14 23:32:14 -05001650 # In the traditional
1651 # "send lots of email" format,
1652 # line 1 = cc
1653 # line 2 = subject
1654 # So let's support that, too.
1655 $input_format = 'lots';
1656 if (@cc == 0 && !$suppress_cc{'cc'}) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001657 printf(__("(non-mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001658 $_, $_) unless $quiet;
1659 push @cc, $_;
1660 } elsif (!defined $subject) {
1661 $subject = $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001662 }
1663 }
1664 }
Jay Soffian50126992009-02-14 23:32:14 -05001665 # Now parse the message body
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001666 while(<$fh>) {
Jay Soffian50126992009-02-14 23:32:14 -05001667 $message .= $_;
Matthieu Moycb2922f2017-08-23 12:21:01 +02001668 if (/^(Signed-off-by|Cc): (.*)/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001669 chomp;
Jay Soffian3531e272009-02-14 23:32:15 -05001670 my ($what, $c) = ($1, $2);
Matthieu Moycb2922f2017-08-23 12:21:01 +02001671 # strip garbage for the address we'll use:
1672 $c = strip_garbage_one_address($c);
1673 # sanitize a bit more to decide whether to suppress the address:
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001674 my $sc = sanitize_address($c);
1675 if ($sc eq $sender) {
Jay Soffian3531e272009-02-14 23:32:15 -05001676 next if ($suppress_cc{'self'});
1677 } else {
1678 next if $suppress_cc{'sob'} and $what =~ /Signed-off-by/i;
1679 next if $suppress_cc{'bodycc'} and $what =~ /Cc/i;
1680 }
Jay Soffian50126992009-02-14 23:32:14 -05001681 push @cc, $c;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001682 printf(__("(body) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001683 $c, $_) unless $quiet;
1684 }
1685 }
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001686 close $fh;
Joe Perches324a8bd2007-08-17 18:51:12 -07001687
Joe Perches6e74e072010-09-24 10:03:00 -07001688 push @to, recipients_cmd("to-cmd", "to", $to_cmd, $t)
1689 if defined $to_cmd;
1690 push @cc, recipients_cmd("cc-cmd", "cc", $cc_cmd, $t)
1691 if defined $cc_cmd && !$suppress_cc{'cccmd'};
Joe Perches324a8bd2007-08-17 18:51:12 -07001692
Thomas Rast3cae7e52010-06-17 22:10:39 +02001693 if ($broken_encoding{$t} && !$has_content_type) {
Paolo Bonzinibb294562014-11-25 15:00:26 +01001694 $xfer_encoding = '8bit' if not defined $xfer_encoding;
Thomas Rast3cae7e52010-06-17 22:10:39 +02001695 $has_content_type = 1;
Paolo Bonzinibb294562014-11-25 15:00:26 +01001696 push @xh, "Content-Type: text/plain; charset=$auto_8bit_encoding";
Thomas Rast3cae7e52010-06-17 22:10:39 +02001697 $body_encoding = $auto_8bit_encoding;
1698 }
1699
Krzysztof Mazurce547802012-10-24 23:08:26 +02001700 if ($broken_encoding{$t} && !is_rfc2047_quoted($subject)) {
1701 $subject = quote_subject($subject, $auto_8bit_encoding);
Thomas Rast3cae7e52010-06-17 22:10:39 +02001702 }
1703
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001704 if (defined $sauthor and $sauthor ne $sender) {
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001705 $message = "From: $author\n\n$message";
Jeff King8291db62007-11-16 05:49:09 -05001706 if (defined $author_encoding) {
1707 if ($has_content_type) {
1708 if ($body_encoding eq $author_encoding) {
1709 # ok, we already have the right encoding
1710 }
1711 else {
1712 # uh oh, we should re-encode
1713 }
1714 }
1715 else {
Paolo Bonzinibb294562014-11-25 15:00:26 +01001716 $xfer_encoding = '8bit' if not defined $xfer_encoding;
Thomas Rast3cae7e52010-06-17 22:10:39 +02001717 $has_content_type = 1;
Jeff King8291db62007-11-16 05:49:09 -05001718 push @xh,
Paolo Bonzinibb294562014-11-25 15:00:26 +01001719 "Content-Type: text/plain; charset=$author_encoding";
Jeff King8291db62007-11-16 05:49:09 -05001720 }
1721 }
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001722 }
Paolo Bonzini8d814082014-11-25 15:00:27 +01001723 if (defined $target_xfer_encoding) {
1724 $xfer_encoding = '8bit' if not defined $xfer_encoding;
1725 $message = apply_transfer_encoding(
1726 $message, $xfer_encoding, $target_xfer_encoding);
1727 $xfer_encoding = $target_xfer_encoding;
1728 }
Paolo Bonzinibb294562014-11-25 15:00:26 +01001729 if (defined $xfer_encoding) {
1730 push @xh, "Content-Transfer-Encoding: $xfer_encoding";
1731 }
1732 if (defined $xfer_encoding or $has_content_type) {
1733 unshift @xh, 'MIME-Version: 1.0' unless $has_mime_version;
1734 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001735
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001736 $needs_confirm = (
1737 $confirm eq "always" or
1738 ($confirm =~ /^(?:auto|cc)$/ && @cc) or
1739 ($confirm =~ /^(?:auto|compose)$/ && $compose && $message_num == 1));
1740 $needs_confirm = "inform" if ($needs_confirm && $confirm_unconfigured && @cc);
1741
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001742 @to = process_address_list(@to);
1743 @cc = process_address_list(@cc);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001744
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001745 @to = (@initial_to, @to);
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001746 @cc = (@initial_cc, @cc);
1747
Michael S. Tsirkinf515c902014-04-29 08:41:16 +03001748 if ($message_num == 1) {
1749 if (defined $cover_cc and $cover_cc) {
1750 @initial_cc = @cc;
1751 }
1752 if (defined $cover_to and $cover_to) {
1753 @initial_to = @to;
1754 }
1755 }
1756
Michael Witten15da1082009-04-13 13:23:51 -05001757 my $message_was_sent = send_message();
Ryan Anderson83b24432005-07-31 04:17:25 -04001758
1759 # set up for the next message
Junio C Hamano95a877a2009-06-12 09:23:43 -07001760 if ($thread && $message_was_sent &&
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001761 ($chain_reply_to || !defined $in_reply_to || length($in_reply_to) == 0 ||
Antonio Ospitedb54c8e2010-11-12 15:55:08 +01001762 $message_num == 1)) {
Christian Ludwig15dc3b92018-03-04 00:58:13 +01001763 $in_reply_to = $message_id;
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001764 if (length $references > 0) {
YOSHIFUJI Hideaki / 吉藤英明a925b892007-04-06 08:50:24 +09001765 $references .= "\n $message_id";
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001766 } else {
1767 $references = "$message_id";
1768 }
Ryan Anderson78488b22005-07-31 20:04:24 -04001769 }
Jeff King4f3d3702007-12-17 15:51:34 -05001770 $message_id = undef;
xiaoqiang zhao5453b832017-05-21 20:59:50 +08001771 $num_sent++;
1772 if (defined $batch_size && $num_sent == $batch_size) {
1773 $num_sent = 0;
1774 $smtp->quit if defined $smtp;
1775 undef $smtp;
1776 undef $auth;
1777 sleep($relogin_delay) if defined $relogin_delay;
1778 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001779}
Ryan Andersone2057352005-08-02 21:45:22 -04001780
Joe Perches6e74e072010-09-24 10:03:00 -07001781# Execute a command (e.g. $to_cmd) to get a list of email addresses
1782# and return a results array
1783sub recipients_cmd {
1784 my ($prefix, $what, $cmd, $file) = @_;
1785
Joe Perches6e74e072010-09-24 10:03:00 -07001786 my @addresses = ();
Ramkumar Ramachandraa47eab02013-03-31 18:40:42 -07001787 open my $fh, "-|", "$cmd \Q$file\E"
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001788 or die sprintf(__("(%s) Could not execute '%s'"), $prefix, $cmd);
Junio C Hamano7ebee442010-10-26 22:02:52 -07001789 while (my $address = <$fh>) {
Joe Perches6e74e072010-09-24 10:03:00 -07001790 $address =~ s/^\s*//g;
1791 $address =~ s/\s*$//g;
1792 $address = sanitize_address($address);
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001793 next if ($address eq $sender and $suppress_cc{'self'});
Joe Perches6e74e072010-09-24 10:03:00 -07001794 push @addresses, $address;
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001795 printf(__("(%s) Adding %s: %s from: '%s'\n"),
1796 $prefix, $what, $address, $cmd) unless $quiet;
Joe Perches6e74e072010-09-24 10:03:00 -07001797 }
Junio C Hamano7ebee442010-10-26 22:02:52 -07001798 close $fh
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001799 or die sprintf(__("(%s) failed to close pipe to '%s'"), $prefix, $cmd);
Joe Perches6e74e072010-09-24 10:03:00 -07001800 return @addresses;
1801}
1802
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001803cleanup_compose_files();
Ryan Anderson1f038a02005-09-05 01:13:07 -04001804
Ævar Arnfjörð Bjarmason4bf597e2010-09-30 13:43:00 +00001805sub cleanup_compose_files {
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001806 unlink($compose_filename, $compose_filename . ".final") if $compose;
Ryan Anderson1f038a02005-09-05 01:13:07 -04001807}
1808
Eric Wong4bc87a22006-03-25 17:20:48 -08001809$smtp->quit if $smtp;
Ryan Andersone2057352005-08-02 21:45:22 -04001810
Paolo Bonzini8d814082014-11-25 15:00:27 +01001811sub apply_transfer_encoding {
1812 my $message = shift;
1813 my $from = shift;
1814 my $to = shift;
1815
1816 return $message if ($from eq $to and $from ne '7bit');
1817
1818 require MIME::QuotedPrint;
1819 require MIME::Base64;
1820
1821 $message = MIME::QuotedPrint::decode($message)
1822 if ($from eq 'quoted-printable');
1823 $message = MIME::Base64::decode($message)
1824 if ($from eq 'base64');
1825
Vasco Almeida46493102016-12-14 11:54:36 -01001826 die __("cannot send message as 7bit")
Paolo Bonzini8d814082014-11-25 15:00:27 +01001827 if ($to eq '7bit' and $message =~ /[^[:ascii:]]/);
1828 return $message
1829 if ($to eq '7bit' or $to eq '8bit');
1830 return MIME::QuotedPrint::encode($message, "\n", 0)
1831 if ($to eq 'quoted-printable');
1832 return MIME::Base64::encode($message, "\n")
1833 if ($to eq 'base64');
Vasco Almeida46493102016-12-14 11:54:36 -01001834 die __("invalid transfer encoding");
Paolo Bonzini8d814082014-11-25 15:00:27 +01001835}
1836
Ævar Arnfjörð Bjarmasonc438ea22010-09-30 13:42:59 +00001837sub unique_email_list {
Ryan Andersone2057352005-08-02 21:45:22 -04001838 my %seen;
1839 my @emails;
1840
1841 foreach my $entry (@_) {
Krzysztof Mazure4312252012-11-22 19:12:10 +01001842 my $clean = extract_valid_address_or_die($entry);
1843 $seen{$clean} ||= 0;
1844 next if $seen{$clean}++;
1845 push @emails, $entry;
Ryan Andersone2057352005-08-02 21:45:22 -04001846 }
1847 return @emails;
1848}
Jeff King747bbff2008-01-18 09:19:48 -05001849
1850sub validate_patch {
1851 my $fn = shift;
Jonathan Tan64896602017-05-12 15:38:26 -07001852
Jonathan Tan177409e2017-06-01 16:50:55 -07001853 if ($repo) {
1854 my $validate_hook = catfile(catdir($repo->repo_path(), 'hooks'),
1855 'sendemail-validate');
1856 my $hook_error;
1857 if (-x $validate_hook) {
1858 my $target = abs_path($fn);
1859 # The hook needs a correct cwd and GIT_DIR.
1860 my $cwd_save = cwd();
1861 chdir($repo->wc_path() or $repo->repo_path())
1862 or die("chdir: $!");
1863 local $ENV{"GIT_DIR"} = $repo->repo_path();
1864 $hook_error = "rejected by sendemail-validate hook"
1865 if system($validate_hook, $target);
1866 chdir($cwd_save) or die("chdir: $!");
1867 }
1868 return $hook_error if $hook_error;
Jonathan Tan64896602017-05-12 15:38:26 -07001869 }
Jonathan Tan64896602017-05-12 15:38:26 -07001870
Jeff King747bbff2008-01-18 09:19:48 -05001871 open(my $fh, '<', $fn)
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001872 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
Jeff King747bbff2008-01-18 09:19:48 -05001873 while (my $line = <$fh>) {
1874 if (length($line) > 998) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001875 return sprintf(__("%s: patch contains a line longer than 998 characters"), $.);
Jeff King747bbff2008-01-18 09:19:48 -05001876 }
1877 }
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -07001878 return;
Jeff King747bbff2008-01-18 09:19:48 -05001879}
Jeff King0706bd12008-03-28 17:28:33 -04001880
Junio C Hamano531220b2016-03-17 22:40:05 -07001881sub handle_backup {
1882 my ($last, $lastlen, $file, $known_suffix) = @_;
1883 my ($suffix, $skip);
1884
1885 $skip = 0;
1886 if (defined $last &&
1887 ($lastlen < length($file)) &&
1888 (substr($file, 0, $lastlen) eq $last) &&
1889 ($suffix = substr($file, $lastlen)) !~ /^[a-z0-9]/i) {
1890 if (defined $known_suffix && $suffix eq $known_suffix) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001891 printf(__("Skipping %s with backup suffix '%s'.\n"), $file, $known_suffix);
Junio C Hamano531220b2016-03-17 22:40:05 -07001892 $skip = 1;
1893 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001894 # TRANSLATORS: please keep "[y|N]" as is.
1895 my $answer = ask(sprintf(__("Do you really want to send %s? [y|N]: "), $file),
Junio C Hamano531220b2016-03-17 22:40:05 -07001896 valid_re => qr/^(?:y|n)/i,
1897 default => 'n');
1898 $skip = ($answer ne 'y');
1899 if ($skip) {
1900 $known_suffix = $suffix;
1901 }
1902 }
1903 }
1904 return ($skip, $known_suffix);
1905}
1906
1907sub handle_backup_files {
1908 my @file = @_;
1909 my ($last, $lastlen, $known_suffix, $skip, @result);
1910 for my $file (@file) {
1911 ($skip, $known_suffix) = handle_backup($last, $lastlen,
1912 $file, $known_suffix);
1913 push @result, $file unless $skip;
1914 $last = $file;
1915 $lastlen = length($file);
1916 }
1917 return @result;
1918}
1919
Jeff King0706bd12008-03-28 17:28:33 -04001920sub file_has_nonascii {
1921 my $fn = shift;
1922 open(my $fh, '<', $fn)
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001923 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
Jeff King0706bd12008-03-28 17:28:33 -04001924 while (my $line = <$fh>) {
1925 return 1 if $line =~ /[^[:ascii:]]/;
1926 }
1927 return 0;
1928}
Thomas Rast3cae7e52010-06-17 22:10:39 +02001929
1930sub body_or_subject_has_nonascii {
1931 my $fn = shift;
1932 open(my $fh, '<', $fn)
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001933 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
Thomas Rast3cae7e52010-06-17 22:10:39 +02001934 while (my $line = <$fh>) {
1935 last if $line =~ /^$/;
1936 return 1 if $line =~ /^Subject.*[^[:ascii:]]/;
1937 }
1938 while (my $line = <$fh>) {
1939 return 1 if $line =~ /[^[:ascii:]]/;
1940 }
1941 return 0;
1942}