blob: 340b5c848294f6d58329b282fe5df72b8f5bbeed [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);
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +010029use 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;
Matthieu Moybd869f62018-01-05 19:36:51 +010033use Git::Mail::Address;
Ryan Anderson83b24432005-07-31 04:17:25 -040034
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +010035Getopt::Long::Configure qw/ pass_through /;
36
Junio C Hamano280242d2006-07-02 16:03:59 -070037package FakeTerm;
38sub new {
39 my ($class, $reason) = @_;
40 return bless \$reason, shift;
41}
42sub readline {
43 my $self = shift;
44 die "Cannot use readline on FakeTerm: $$self";
45}
46package main;
47
Michael Coleman1b0baf12007-02-27 22:47:54 -060048
49sub usage {
50 print <<EOT;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +010051git send-email [options] <file | directory | rev-list options >
Jacob Keller17b7a832015-11-19 14:52:11 -080052git send-email --dump-aliases
Michael Witten4ed62b02008-09-30 07:58:30 -050053
54 Composing:
55 --from <str> * Email From:
Stephen Boydf434c082010-03-07 14:46:48 -080056 --[no-]to <str> * Email To:
57 --[no-]cc <str> * Email Cc:
58 --[no-]bcc <str> * Email Bcc:
Michael Witten4ed62b02008-09-30 07:58:30 -050059 --subject <str> * Email "Subject:"
60 --in-reply-to <str> * Email "In-Reply-To:"
Luis Henriquesac1596a2014-03-24 21:38:27 +000061 --[no-]xmailer * Add "X-Mailer:" header (default).
Felipe Contreras402596a2013-04-07 01:10:27 -060062 --[no-]annotate * Review each patch that will be sent in an editor.
Michael Witten4ed62b02008-09-30 07:58:30 -050063 --compose * Open an editor for introduction.
Krzysztof Mazur62e00692012-10-10 01:02:56 +020064 --compose-encoding <str> * Encoding to assume for introduction.
Thomas Rast3cae7e52010-06-17 22:10:39 +020065 --8bit-encoding <str> * Encoding to assume 8bit mails if undeclared
Paolo Bonzini8d814082014-11-25 15:00:27 +010066 --transfer-encoding <str> * Transfer encoding to use (quoted-printable, 8bit, base64)
Michael Witten4ed62b02008-09-30 07:58:30 -050067
68 Sending:
69 --envelope-sender <str> * Email envelope sender.
70 --smtp-server <str:int> * Outgoing SMTP server to use. The port
71 is optional. Default 'localhost'.
Pascal Obry052fbea2010-09-06 20:12:11 +020072 --smtp-server-option <str> * Outgoing SMTP server option to use.
Michael Witten4ed62b02008-09-30 07:58:30 -050073 --smtp-server-port <int> * Outgoing SMTP server port.
74 --smtp-user <str> * Username for SMTP-AUTH.
75 --smtp-pass <str> * Password for SMTP-AUTH; not necessary.
76 --smtp-encryption <str> * tls or ssl; anything else disables.
77 --smtp-ssl * Deprecated. Use '--smtp-encryption ssl'.
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -070078 --smtp-ssl-cert-path <str> * Path to ca-certificates (either directory or file).
79 Pass an empty string to disable certificate
80 verification.
Jari Aalto134550f2010-03-14 17:16:45 +020081 --smtp-domain <str> * The domain name sent to HELO/EHLO handshake
Jan Viktorin0f2e68b2015-08-12 01:39:44 +020082 --smtp-auth <str> * Space-separated list of allowed AUTH mechanisms.
83 This setting forces to use one of the listed mechanisms.
Jari Aaltof60812e2010-03-14 17:16:09 +020084 --smtp-debug <0|1> * Disable, enable Net::SMTP debug.
Michael Witten4ed62b02008-09-30 07:58:30 -050085
xiaoqiang zhao5453b832017-05-21 20:59:50 +080086 --batch-size <int> * send max <int> message per connection.
87 --relogin-delay <int> * delay <int> seconds between two successive login.
88 This option can only be used with --batch-size
89
Michael Witten4ed62b02008-09-30 07:58:30 -050090 Automating:
91 --identity <str> * Use the sendemail.<id> options.
Joe Perches6e74e072010-09-24 10:03:00 -070092 --to-cmd <str> * Email To: via `<str> \$patch_path`
Michael Witten4ed62b02008-09-30 07:58:30 -050093 --cc-cmd <str> * Email Cc: via `<str> \$patch_path`
Jay Soffian3531e272009-02-14 23:32:15 -050094 --suppress-cc <str> * author, self, sob, cc, cccmd, body, bodycc, all.
Michael S. Tsirkinf515c902014-04-29 08:41:16 +030095 --[no-]cc-cover * Email Cc: addresses in the cover letter.
96 --[no-]to-cover * Email To: addresses in the cover letter.
Jay Soffian3531e272009-02-14 23:32:15 -050097 --[no-]signed-off-by-cc * Send to Signed-off-by: addresses. Default on.
Michael Witten4ed62b02008-09-30 07:58:30 -050098 --[no-]suppress-from * Send to self. Default off.
Junio C Hamano41fe87f2009-08-22 12:48:48 -070099 --[no-]chain-reply-to * Chain In-Reply-To: fields. Default off.
Michael Witten4ed62b02008-09-30 07:58:30 -0500100 --[no-]thread * Use In-Reply-To: field. Default on.
101
102 Administering:
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500103 --confirm <str> * Confirm recipients before sending;
104 auto, cc, compose, always, or never.
Michael Witten4ed62b02008-09-30 07:58:30 -0500105 --quiet * Output one line of info per email.
106 --dry-run * Don't actually send the emails.
107 --[no-]validate * Perform patch sanity checks. Default on.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100108 --[no-]format-patch * understand any non optional arguments as
109 `git format-patch` ones.
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200110 --force * Send even if safety checks would prevent it.
Jeff Kingc764a0c2008-01-18 09:20:10 -0500111
Jacob Keller17b7a832015-11-19 14:52:11 -0800112 Information:
113 --dump-aliases * Dump configured aliases and exit.
114
Michael Coleman1b0baf12007-02-27 22:47:54 -0600115EOT
116 exit(1);
117}
118
Eric Wong4bc87a22006-03-25 17:20:48 -0800119# most mail servers generate the Date: header, but not all...
Jakub Narebski6bdca892006-07-07 20:57:55 +0200120sub format_2822_time {
121 my ($time) = @_;
122 my @localtm = localtime($time);
123 my @gmttm = gmtime($time);
124 my $localmin = $localtm[1] + $localtm[2] * 60;
125 my $gmtmin = $gmttm[1] + $gmttm[2] * 60;
126 if ($localtm[0] != $gmttm[0]) {
Vasco Almeida46493102016-12-14 11:54:36 -0100127 die __("local zone differs from GMT by a non-minute interval\n");
Jakub Narebski6bdca892006-07-07 20:57:55 +0200128 }
129 if ((($gmttm[6] + 1) % 7) == $localtm[6]) {
130 $localmin += 1440;
131 } elsif ((($gmttm[6] - 1) % 7) == $localtm[6]) {
132 $localmin -= 1440;
133 } elsif ($gmttm[6] != $localtm[6]) {
Vasco Almeida46493102016-12-14 11:54:36 -0100134 die __("local time offset greater than or equal to 24 hours\n");
Jakub Narebski6bdca892006-07-07 20:57:55 +0200135 }
136 my $offset = $localmin - $gmtmin;
137 my $offhour = $offset / 60;
138 my $offmin = abs($offset % 60);
139 if (abs($offhour) >= 24) {
Vasco Almeida46493102016-12-14 11:54:36 -0100140 die __("local time offset greater than or equal to 24 hours\n");
Jakub Narebski6bdca892006-07-07 20:57:55 +0200141 }
142
143 return sprintf("%s, %2d %s %d %02d:%02d:%02d %s%02d%02d",
144 qw(Sun Mon Tue Wed Thu Fri Sat)[$localtm[6]],
145 $localtm[3],
146 qw(Jan Feb Mar Apr May Jun
147 Jul Aug Sep Oct Nov Dec)[$localtm[4]],
148 $localtm[5]+1900,
149 $localtm[2],
150 $localtm[1],
151 $localtm[0],
152 ($offset >= 0) ? '+' : '-',
153 abs($offhour),
154 $offmin,
155 );
156}
Eric Wong4bc87a22006-03-25 17:20:48 -0800157
Eric Wong567ffeb2006-03-25 16:47:12 -0800158my $have_email_valid = eval { require Email::Valid; 1 };
Eric Wong4bc87a22006-03-25 17:20:48 -0800159my $smtp;
Wincent Colaiuta5f5b6112007-11-21 13:35:05 +0100160my $auth;
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800161my $num_sent = 0;
Eric Wong4bc87a22006-03-25 17:20:48 -0800162
Роман Донченко11f70a72014-12-14 18:59:46 +0300163# Regexes for RFC 2047 productions.
164my $re_token = qr/[^][()<>@,;:\\"\/?.= \000-\037\177-\377]+/;
165my $re_encoded_text = qr/[^? \000-\037\177-\377]+/;
166my $re_encoded_word = qr/=\?($re_token)\?($re_token)\?($re_encoded_text)\?=/;
167
Ryan Anderson83b24432005-07-31 04:17:25 -0400168# Variables we fill in automatically, or via prompting:
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700169my (@to,$no_to,@initial_to,@cc,$no_cc,@initial_cc,@bcclist,$no_bcc,@xh,
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100170 $initial_reply_to,$initial_subject,@files,
Luis Henriquesac1596a2014-03-24 21:38:27 +0000171 $author,$sender,$smtp_authpass,$annotate,$use_xmailer,$compose,$time);
Ryan Anderson83b24432005-07-31 04:17:25 -0400172
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700173my $envelope_sender;
Ryan Anderson78488b22005-07-31 20:04:24 -0400174
Ryan Anderson91332612005-07-31 20:04:24 -0400175# Example reply to:
Ryan Anderson83b24432005-07-31 04:17:25 -0400176#$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
Ryan Anderson83b24432005-07-31 04:17:25 -0400177
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100178my $repo = eval { Git->repository() };
179my @repo = $repo ? ($repo) : ();
Junio C Hamano280242d2006-07-02 16:03:59 -0700180my $term = eval {
Jay Soffian0fb7fc72008-02-21 19:16:04 -0500181 $ENV{"GIT_SEND_EMAIL_NOTTY"}
182 ? new Term::ReadLine 'git-send-email', \*STDIN, \*STDOUT
183 : new Term::ReadLine 'git-send-email';
Junio C Hamano280242d2006-07-02 16:03:59 -0700184};
185if ($@) {
186 $term = new FakeTerm "$@: going non-interactive";
187}
Ryan Anderson83b24432005-07-31 04:17:25 -0400188
Adam Roben5483c712007-06-27 20:59:37 -0700189# Behavior modification variables
190my ($quiet, $dry_run) = (0, 0);
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100191my $format_patch;
Jay Soffianafe756c2009-02-23 13:51:37 -0500192my $compose_filename;
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200193my $force = 0;
Jacob Keller17b7a832015-11-19 14:52:11 -0800194my $dump_aliases = 0;
Adam Roben5483c712007-06-27 20:59:37 -0700195
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100196# Handle interactive edition of files.
197my $multiedit;
Michael J Gruber0ce142c2010-03-22 17:12:53 +0100198my $editor;
Jonathan Niederb4479f02009-10-30 20:42:34 -0500199
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100200sub do_edit {
Michael J Gruber0ce142c2010-03-22 17:12:53 +0100201 if (!defined($editor)) {
202 $editor = Git::command_oneline('var', 'GIT_EDITOR');
203 }
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100204 if (defined($multiedit) && !$multiedit) {
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100205 map {
206 system('sh', '-c', $editor.' "$@"', $editor, $_);
207 if (($? & 127) || ($? >> 8)) {
Vasco Almeida46493102016-12-14 11:54:36 -0100208 die(__("the editor exited uncleanly, aborting everything"));
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100209 }
210 } @_;
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100211 } else {
212 system('sh', '-c', $editor.' "$@"', $editor, @_);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100213 if (($? & 127) || ($? >> 8)) {
Vasco Almeida46493102016-12-14 11:54:36 -0100214 die(__("the editor exited uncleanly, aborting everything"));
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100215 }
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100216 }
217}
Adam Roben5483c712007-06-27 20:59:37 -0700218
219# Variables with corresponding config settings
Joe Perches6e74e072010-09-24 10:03:00 -0700220my ($thread, $chain_reply_to, $suppress_from, $signed_off_by_cc);
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300221my ($cover_cc, $cover_to);
Joe Perches6e74e072010-09-24 10:03:00 -0700222my ($to_cmd, $cc_cmd);
Pascal Obry052fbea2010-09-06 20:12:11 +0200223my ($smtp_server, $smtp_server_port, @smtp_server_options);
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -0700224my ($smtp_authuser, $smtp_encryption, $smtp_ssl_cert_path);
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800225my ($batch_size, $relogin_delay);
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200226my ($identity, $aliasfiletype, @alias_files, $smtp_domain, $smtp_auth);
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500227my ($validate, $confirm);
David Brown65648282007-12-25 19:56:29 -0800228my (@suppress_cc);
Thomas Rast3cae7e52010-06-17 22:10:39 +0200229my ($auto_8bit_encoding);
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200230my ($compose_encoding);
Paolo Bonzini8d814082014-11-25 15:00:27 +0100231my ($target_xfer_encoding);
Adam Roben5483c712007-06-27 20:59:37 -0700232
Jari Aaltof60812e2010-03-14 17:16:09 +0200233my ($debug_net_smtp) = 0; # Net::SMTP, see send_message()
234
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900235my %config_bool_settings = (
Adam Roben5483c712007-06-27 20:59:37 -0700236 "thread" => [\$thread, 1],
Felipe Contrerasb99d22f2013-05-24 22:44:52 -0500237 "chainreplyto" => [\$chain_reply_to, 0],
David Brown65648282007-12-25 19:56:29 -0800238 "suppressfrom" => [\$suppress_from, undef],
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500239 "signedoffbycc" => [\$signed_off_by_cc, undef],
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300240 "cccover" => [\$cover_cc, undef],
241 "tocover" => [\$cover_to, undef],
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500242 "signedoffcc" => [\$signed_off_by_cc, undef], # Deprecated
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500243 "validate" => [\$validate, 1],
Felipe Contreras402596a2013-04-07 01:10:27 -0600244 "multiedit" => [\$multiedit, undef],
Luis Henriquesac1596a2014-03-24 21:38:27 +0000245 "annotate" => [\$annotate, undef],
246 "xmailer" => [\$use_xmailer, 1]
Adam Robene46f7a02007-06-26 15:48:30 -0700247);
248
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900249my %config_settings = (
250 "smtpserver" => \$smtp_server,
Junio C Hamano44b24762007-09-25 17:27:54 -0700251 "smtpserverport" => \$smtp_server_port,
Pascal Obry052fbea2010-09-06 20:12:11 +0200252 "smtpserveroption" => \@smtp_server_options,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900253 "smtpuser" => \$smtp_authuser,
254 "smtppass" => \$smtp_authpass,
Pascal Obrye1e91152010-09-06 20:12:09 +0200255 "smtpdomain" => \$smtp_domain,
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200256 "smtpauth" => \$smtp_auth,
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800257 "smtpbatchsize" => \$batch_size,
258 "smtprelogindelay" => \$relogin_delay,
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700259 "to" => \@initial_to,
Joe Perches6e74e072010-09-24 10:03:00 -0700260 "tocmd" => \$to_cmd,
Miklos Vajna5f8b9fc2008-04-27 14:14:58 +0200261 "cc" => \@initial_cc,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900262 "cccmd" => \$cc_cmd,
263 "aliasfiletype" => \$aliasfiletype,
264 "bcc" => \@bcclist,
David Brown65648282007-12-25 19:56:29 -0800265 "suppresscc" => \@suppress_cc,
Ask Bjørn Hansen9f7820a2008-06-07 00:33:42 -0700266 "envelopesender" => \$envelope_sender,
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500267 "confirm" => \$confirm,
Trent Piepho09caa242009-05-12 15:48:56 -0700268 "from" => \$sender,
Thomas Rast3cae7e52010-06-17 22:10:39 +0200269 "assume8bitencoding" => \$auto_8bit_encoding,
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200270 "composeencoding" => \$compose_encoding,
Paolo Bonzini8d814082014-11-25 15:00:27 +0100271 "transferencoding" => \$target_xfer_encoding,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900272);
Avi Kivity4a62d3f2007-03-11 19:19:44 +0200273
Cord Seelecec5dae2011-09-30 12:52:25 +0200274my %config_path_settings = (
275 "aliasesfile" => \@alias_files,
John Keeping6e07a3b2015-11-17 22:01:05 +0000276 "smtpsslcertpath" => \$smtp_ssl_cert_path,
Cord Seelecec5dae2011-09-30 12:52:25 +0200277);
278
Michael Witten87429972008-02-03 19:53:57 -0500279# Handle Uncouth Termination
280sub signal_handler {
281
282 # Make text normal
283 print color("reset"), "\n";
284
285 # SMTP password masked
286 system "stty echo";
287
288 # tmp files from --compose
Jay Soffianafe756c2009-02-23 13:51:37 -0500289 if (defined $compose_filename) {
290 if (-e $compose_filename) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100291 printf __("'%s' contains an intermediate version ".
292 "of the email you were composing.\n"),
293 $compose_filename;
Jay Soffianafe756c2009-02-23 13:51:37 -0500294 }
295 if (-e ($compose_filename . ".final")) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100296 printf __("'%s.final' contains the composed email.\n"),
297 $compose_filename;
Jay Soffianafe756c2009-02-23 13:51:37 -0500298 }
Michael Witten87429972008-02-03 19:53:57 -0500299 }
300
301 exit;
302};
303
304$SIG{TERM} = \&signal_handler;
305$SIG{INT} = \&signal_handler;
306
Ryan Anderson83b24432005-07-31 04:17:25 -0400307# Begin by accumulating all the variables (defined above), that we will end up
308# needing, first, from the command line:
309
Clemens Buchacherc5978242011-09-03 19:06:13 +0200310my $help;
311my $rc = GetOptions("h" => \$help,
Jacob Keller17b7a832015-11-19 14:52:11 -0800312 "dump-aliases" => \$dump_aliases);
313usage() unless $rc;
Vasco Almeida46493102016-12-14 11:54:36 -0100314die __("--dump-aliases incompatible with other options\n")
Jacob Keller17b7a832015-11-19 14:52:11 -0800315 if !$help and $dump_aliases and @ARGV;
316$rc = GetOptions(
Clemens Buchacherc5978242011-09-03 19:06:13 +0200317 "sender|from=s" => \$sender,
Ryan Anderson83b24432005-07-31 04:17:25 -0400318 "in-reply-to=s" => \$initial_reply_to,
319 "subject=s" => \$initial_subject,
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700320 "to=s" => \@initial_to,
Joe Perches6e74e072010-09-24 10:03:00 -0700321 "to-cmd=s" => \$to_cmd,
Stephen Boydf434c082010-03-07 14:46:48 -0800322 "no-to" => \$no_to,
Ryan Andersonda140f82006-02-13 03:05:15 -0500323 "cc=s" => \@initial_cc,
Stephen Boydf434c082010-03-07 14:46:48 -0800324 "no-cc" => \$no_cc,
Ryan Anderson58063242006-05-29 12:30:13 -0700325 "bcc=s" => \@bcclist,
Stephen Boydf434c082010-03-07 14:46:48 -0800326 "no-bcc" => \$no_bcc,
Ryan Anderson78488b22005-07-31 20:04:24 -0400327 "chain-reply-to!" => \$chain_reply_to,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800328 "no-chain-reply-to" => sub {$chain_reply_to = 0},
Ryan Anderson3342d852005-07-31 20:04:24 -0400329 "smtp-server=s" => \$smtp_server,
Pascal Obry052fbea2010-09-06 20:12:11 +0200330 "smtp-server-option=s" => \@smtp_server_options,
Junio C Hamano44b24762007-09-25 17:27:54 -0700331 "smtp-server-port=s" => \$smtp_server_port,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900332 "smtp-user=s" => \$smtp_authuser,
Michael Witten2363d742008-02-03 19:53:56 -0500333 "smtp-pass:s" => \$smtp_authpass,
Thomas Rastf6bebd12008-06-25 21:42:43 +0200334 "smtp-ssl" => sub { $smtp_encryption = 'ssl' },
335 "smtp-encryption=s" => \$smtp_encryption,
Thomas Rast979e6522013-12-01 23:48:42 +0100336 "smtp-ssl-cert-path=s" => \$smtp_ssl_cert_path,
Jari Aaltof60812e2010-03-14 17:16:09 +0200337 "smtp-debug:i" => \$debug_net_smtp,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -0400338 "smtp-domain:s" => \$smtp_domain,
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200339 "smtp-auth=s" => \$smtp_auth,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900340 "identity=s" => \$identity,
Felipe Contreras402596a2013-04-07 01:10:27 -0600341 "annotate!" => \$annotate,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800342 "no-annotate" => sub {$annotate = 0},
Ryan Anderson1f038a02005-09-05 01:13:07 -0400343 "compose" => \$compose,
Ryan Anderson30d08b32006-02-02 11:56:06 -0500344 "quiet" => \$quiet,
Joe Perches324a8bd2007-08-17 18:51:12 -0700345 "cc-cmd=s" => \$cc_cmd,
Adam Roben5483c712007-06-27 20:59:37 -0700346 "suppress-from!" => \$suppress_from,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800347 "no-suppress-from" => sub {$suppress_from = 0},
David Brown65648282007-12-25 19:56:29 -0800348 "suppress-cc=s" => \@suppress_cc,
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500349 "signed-off-cc|signed-off-by-cc!" => \$signed_off_by_cc,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800350 "no-signed-off-cc|no-signed-off-by-cc" => sub {$signed_off_by_cc = 0},
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300351 "cc-cover|cc-cover!" => \$cover_cc,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800352 "no-cc-cover" => sub {$cover_cc = 0},
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300353 "to-cover|to-cover!" => \$cover_to,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800354 "no-to-cover" => sub {$cover_to = 0},
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500355 "confirm=s" => \$confirm,
Matthew Wilcox61302592006-10-10 08:58:23 -0600356 "dry-run" => \$dry_run,
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700357 "envelope-sender=s" => \$envelope_sender,
Adam Roben5483c712007-06-27 20:59:37 -0700358 "thread!" => \$thread,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800359 "no-thread" => sub {$thread = 0},
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500360 "validate!" => \$validate,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800361 "no-validate" => sub {$validate = 0},
Paolo Bonzini8d814082014-11-25 15:00:27 +0100362 "transfer-encoding=s" => \$target_xfer_encoding,
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100363 "format-patch!" => \$format_patch,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800364 "no-format-patch" => sub {$format_patch = 0},
Thomas Rast3cae7e52010-06-17 22:10:39 +0200365 "8bit-encoding=s" => \$auto_8bit_encoding,
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200366 "compose-encoding=s" => \$compose_encoding,
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200367 "force" => \$force,
Luis Henriquesac1596a2014-03-24 21:38:27 +0000368 "xmailer!" => \$use_xmailer,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800369 "no-xmailer" => sub {$use_xmailer = 0},
xiaoqiang zhao5453b832017-05-21 20:59:50 +0800370 "batch-size=i" => \$batch_size,
371 "relogin-delay=i" => \$relogin_delay,
Ryan Anderson83b24432005-07-31 04:17:25 -0400372 );
373
Clemens Buchacherc5978242011-09-03 19:06:13 +0200374usage() if $help;
Michael Coleman1b0baf12007-02-27 22:47:54 -0600375unless ($rc) {
376 usage();
377}
378
Vasco Almeida46493102016-12-14 11:54:36 -0100379die __("Cannot run git format-patch from outside a repository\n")
Jay Soffianeed6ca72009-02-14 23:32:13 -0500380 if $format_patch and not $repo;
381
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900382# Now, let's fill any that aren't set in with defaults:
383
384sub read_config {
385 my ($prefix) = @_;
386
387 foreach my $setting (keys %config_bool_settings) {
388 my $target = $config_bool_settings{$setting}->[0];
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100389 $$target = Git::config_bool(@repo, "$prefix.$setting") unless (defined $$target);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900390 }
391
Cord Seelecec5dae2011-09-30 12:52:25 +0200392 foreach my $setting (keys %config_path_settings) {
Cord Seele463b0ea2011-10-14 22:53:31 +0200393 my $target = $config_path_settings{$setting};
394 if (ref($target) eq "ARRAY") {
395 unless (@$target) {
396 my @values = Git::config_path(@repo, "$prefix.$setting");
397 @$target = @values if (@values && defined $values[0]);
398 }
399 }
400 else {
401 $$target = Git::config_path(@repo, "$prefix.$setting") unless (defined $$target);
402 }
Cord Seelecec5dae2011-09-30 12:52:25 +0200403 }
404
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900405 foreach my $setting (keys %config_settings) {
406 my $target = $config_settings{$setting};
Stephen Boydf434c082010-03-07 14:46:48 -0800407 next if $setting eq "to" and defined $no_to;
408 next if $setting eq "cc" and defined $no_cc;
409 next if $setting eq "bcc" and defined $no_bcc;
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900410 if (ref($target) eq "ARRAY") {
411 unless (@$target) {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100412 my @values = Git::config(@repo, "$prefix.$setting");
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900413 @$target = @values if (@values && defined $values[0]);
414 }
415 }
416 else {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100417 $$target = Git::config(@repo, "$prefix.$setting") unless (defined $$target);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900418 }
419 }
Thomas Rastf6bebd12008-06-25 21:42:43 +0200420
421 if (!defined $smtp_encryption) {
422 my $enc = Git::config(@repo, "$prefix.smtpencryption");
423 if (defined $enc) {
424 $smtp_encryption = $enc;
425 } elsif (Git::config_bool(@repo, "$prefix.smtpssl")) {
426 $smtp_encryption = 'ssl';
427 }
428 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900429}
430
431# read configuration from [sendemail "$identity"], fall back on [sendemail]
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100432$identity = Git::config(@repo, "sendemail.identity") unless (defined $identity);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900433read_config("sendemail.$identity") if (defined $identity);
434read_config("sendemail");
435
436# fall back on builtin bool defaults
437foreach my $setting (values %config_bool_settings) {
438 ${$setting->[0]} = $setting->[1] unless (defined (${$setting->[0]}));
439}
440
Thomas Rastfa835cd2008-06-26 23:03:21 +0200441# 'default' encryption is none -- this only prevents a warning
442$smtp_encryption = '' unless (defined $smtp_encryption);
443
David Brown65648282007-12-25 19:56:29 -0800444# Set CC suppressions
445my(%suppress_cc);
446if (@suppress_cc) {
447 foreach my $entry (@suppress_cc) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100448 die sprintf(__("Unknown --suppress-cc field: '%s'\n"), $entry)
Ævar Arnfjörð Bjarmasone9bf7412010-09-30 13:43:04 +0000449 unless $entry =~ /^(?:all|cccmd|cc|author|self|sob|body|bodycc)$/;
David Brown65648282007-12-25 19:56:29 -0800450 $suppress_cc{$entry} = 1;
451 }
452}
453
454if ($suppress_cc{'all'}) {
Paolo Bonzinicb8a9bd2009-06-18 14:31:32 +0200455 foreach my $entry (qw (cccmd cc author self sob body bodycc)) {
David Brown65648282007-12-25 19:56:29 -0800456 $suppress_cc{$entry} = 1;
457 }
458 delete $suppress_cc{'all'};
459}
460
461# If explicit old-style ones are specified, they trump --suppress-cc.
462$suppress_cc{'self'} = $suppress_from if defined $suppress_from;
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500463$suppress_cc{'sob'} = !$signed_off_by_cc if defined $signed_off_by_cc;
David Brown65648282007-12-25 19:56:29 -0800464
Jay Soffian3531e272009-02-14 23:32:15 -0500465if ($suppress_cc{'body'}) {
466 foreach my $entry (qw (sob bodycc)) {
467 $suppress_cc{$entry} = 1;
468 }
469 delete $suppress_cc{'body'};
470}
471
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500472# Set confirm's default value
473my $confirm_unconfigured = !defined $confirm;
474if ($confirm_unconfigured) {
475 $confirm = scalar %suppress_cc ? 'compose' : 'auto';
476};
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100477die sprintf(__("Unknown --confirm setting: '%s'\n"), $confirm)
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500478 unless $confirm =~ /^(?:auto|cc|compose|always|never)/;
479
David Brown65648282007-12-25 19:56:29 -0800480# Debugging, print out the suppressions.
481if (0) {
482 print "suppressions:\n";
483 foreach my $entry (keys %suppress_cc) {
484 printf " %-5s -> $suppress_cc{$entry}\n", $entry;
485 }
486}
487
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100488my ($repoauthor, $repocommitter);
489($repoauthor) = Git::ident_person(@repo, 'author');
490($repocommitter) = Git::ident_person(@repo, 'committer');
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900491
Jay Soffian50126992009-02-14 23:32:14 -0500492sub parse_address_line {
Matthieu Moybd869f62018-01-05 19:36:51 +0100493 return map { $_->format } Mail::Address->parse($_[0]);
Jay Soffian50126992009-02-14 23:32:14 -0500494}
495
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800496sub split_addrs {
Junio C Hamano2f0e7cb2008-12-21 01:57:59 -0800497 return quotewords('\s*,\s*', 1, @_);
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800498}
499
Eric Wong994d6c62006-05-14 19:13:44 -0700500my %aliases;
Eric Sunshine09f11572015-05-31 18:29:27 -0400501
502sub parse_sendmail_alias {
503 local $_ = shift;
504 if (/"/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100505 printf STDERR __("warning: sendmail alias with quotes is not supported: %s\n"), $_;
Eric Sunshine86b89842015-06-01 14:22:36 -0400506 } elsif (/:include:/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100507 printf STDERR __("warning: `:include:` not supported: %s\n"), $_;
Eric Sunshine86b89842015-06-01 14:22:36 -0400508 } elsif (/[\/|]/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100509 printf STDERR __("warning: `/file` or `|pipe` redirection not supported: %s\n"), $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400510 } elsif (/^(\S+?)\s*:\s*(.+)$/) {
511 my ($alias, $addr) = ($1, $2);
512 $aliases{$alias} = [ split_addrs($addr) ];
513 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100514 printf STDERR __("warning: sendmail line is not recognized: %s\n"), $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400515 }
516}
517
518sub parse_sendmail_aliases {
519 my $fh = shift;
Eric Sunshine2532dd02015-05-31 18:29:29 -0400520 my $s = '';
Eric Sunshine09f11572015-05-31 18:29:27 -0400521 while (<$fh>) {
Eric Sunshine2532dd02015-05-31 18:29:29 -0400522 chomp;
Eric Sunshine020be85f2015-05-31 18:29:28 -0400523 next if /^\s*$/ || /^\s*#/;
Eric Sunshine2532dd02015-05-31 18:29:29 -0400524 $s .= $_, next if $s =~ s/\\$// || s/^\s+//;
525 parse_sendmail_alias($s) if $s;
526 $s = $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400527 }
Eric Sunshine2532dd02015-05-31 18:29:29 -0400528 $s =~ s/\\$//; # silently tolerate stray '\' on last line
529 parse_sendmail_alias($s) if $s;
Eric Sunshine09f11572015-05-31 18:29:27 -0400530}
531
Eric Wong994d6c62006-05-14 19:13:44 -0700532my %parse_alias = (
533 # multiline formats can be supported in the future
534 mutt => sub { my $fh = shift; while (<$fh>) {
Felipe Contrerasffc01f92009-09-30 17:49:36 +0300535 if (/^\s*alias\s+(?:-group\s+\S+\s+)*(\S+)\s+(.*)$/) {
Eric Wong994d6c62006-05-14 19:13:44 -0700536 my ($alias, $addr) = ($1, $2);
537 $addr =~ s/#.*$//; # mutt allows # comments
Eric Wong2c510f22016-01-04 20:53:30 +0000538 # commas delimit multiple addresses
539 my @addr = split_addrs($addr);
540
541 # quotes may be escaped in the file,
542 # unescape them so we do not double-escape them later.
543 s/\\"/"/g foreach @addr;
544 $aliases{$alias} = \@addr
Eric Wong994d6c62006-05-14 19:13:44 -0700545 }}},
546 mailrc => sub { my $fh = shift; while (<$fh>) {
Jeff Kinga277d1e2016-03-17 19:58:22 -0400547 if (/^alias\s+(\S+)\s+(.*?)\s*$/) {
Eric Wong994d6c62006-05-14 19:13:44 -0700548 # spaces delimit multiple addresses
Eric W. Biedermanfe87c922009-05-20 19:45:53 -0700549 $aliases{$1} = [ quotewords('\s+', 0, $2) ];
Eric Wong994d6c62006-05-14 19:13:44 -0700550 }}},
Trent Piepho73c427e2008-11-25 18:55:00 -0800551 pine => sub { my $fh = shift; my $f='\t[^\t]*';
552 for (my $x = ''; defined($x); $x = $_) {
553 chomp $x;
554 $x .= $1 while(defined($_ = <$fh>) && /^ +(.*)$/);
555 $x =~ /^(\S+)$f\t\(?([^\t]+?)\)?(:?$f){0,2}$/ or next;
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800556 $aliases{$1} = [ split_addrs($2) ];
Trent Piepho73c427e2008-11-25 18:55:00 -0800557 }},
Bill Pemberton7613ea32009-04-22 09:41:29 -0400558 elm => sub { my $fh = shift;
559 while (<$fh>) {
560 if (/^(\S+)\s+=\s+[^=]+=\s(\S+)/) {
561 my ($alias, $addr) = ($1, $2);
562 $aliases{$alias} = [ split_addrs($addr) ];
563 }
564 } },
Eric Sunshine09f11572015-05-31 18:29:27 -0400565 sendmail => \&parse_sendmail_aliases,
Eric Wong994d6c62006-05-14 19:13:44 -0700566 gnus => sub { my $fh = shift; while (<$fh>) {
567 if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
568 $aliases{$1} = [ $2 ];
569 }}}
570);
571
Petr Baudis3cb8caf2006-07-03 22:47:58 +0200572if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) {
Eric Wong994d6c62006-05-14 19:13:44 -0700573 foreach my $file (@alias_files) {
574 open my $fh, '<', $file or die "opening $file: $!\n";
575 $parse_alias{$aliasfiletype}->($fh);
576 close $fh;
577 }
578}
579
Jacob Keller17b7a832015-11-19 14:52:11 -0800580if ($dump_aliases) {
581 print "$_\n" for (sort keys %aliases);
582 exit(0);
583}
584
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700585# is_format_patch_arg($f) returns 0 if $f names a patch, or 1 if
586# $f is a revision list specification to be passed to format-patch.
587sub is_format_patch_arg {
Jay Soffianeed6ca72009-02-14 23:32:13 -0500588 return unless $repo;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100589 my $f = shift;
590 try {
591 $repo->command('rev-parse', '--verify', '--quiet', $f);
592 if (defined($format_patch)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100593 return $format_patch;
594 }
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100595 die sprintf(__ <<EOF, $f, $f);
596File '%s' exists but it could also be the range of commits
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100597to produce patches for. Please disambiguate by...
598
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100599 * Saying "./%s" if you mean a file; or
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100600 * Giving --format-patch option if you mean a range.
601EOF
602 } catch Git::Error::Command with {
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700603 # Not a valid revision. Treat it as a filename.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100604 return 0;
605 }
606}
607
Jeff Kingaa548922008-01-18 09:19:36 -0500608# Now that all the defaults are set, process the rest of the command line
609# arguments and collect up the files that need to be processed.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100610my @rev_list_opts;
Junio C Hamano69f4ce52008-11-30 22:38:20 -0800611while (defined(my $f = shift @ARGV)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100612 if ($f eq "--") {
613 push @rev_list_opts, "--", @ARGV;
614 @ARGV = ();
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700615 } elsif (-d $f and !is_format_patch_arg($f)) {
Ævar Arnfjörð Bjarmasonc6038162010-09-30 13:42:54 +0000616 opendir my $dh, $f
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100617 or die sprintf(__("Failed to opendir %s: %s"), $f, $!);
Jeff Kingaa548922008-01-18 09:19:36 -0500618
Ævar Arnfjörð Bjarmason89bf1ba2010-09-14 19:02:24 +0000619 push @files, grep { -f $_ } map { catfile($f, $_) }
Ævar Arnfjörð Bjarmasonc6038162010-09-30 13:42:54 +0000620 sort readdir $dh;
621 closedir $dh;
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700622 } elsif ((-f $f or -p $f) and !is_format_patch_arg($f)) {
Jeff Kingaa548922008-01-18 09:19:36 -0500623 push @files, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500624 } else {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100625 push @rev_list_opts, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500626 }
627}
628
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100629if (@rev_list_opts) {
Vasco Almeida46493102016-12-14 11:54:36 -0100630 die __("Cannot run git format-patch from outside a repository\n")
Jay Soffianeed6ca72009-02-14 23:32:13 -0500631 unless $repo;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100632 push @files, $repo->command('format-patch', '-o', tempdir(CLEANUP => 1), @rev_list_opts);
633}
634
Junio C Hamano531220b2016-03-17 22:40:05 -0700635@files = handle_backup_files(@files);
636
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500637if ($validate) {
Jeff Kingc764a0c2008-01-18 09:20:10 -0500638 foreach my $f (@files) {
Kevin Ballard300913b2008-06-25 15:44:40 -0700639 unless (-p $f) {
640 my $error = validate_patch($f);
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100641 $error and die sprintf(__("fatal: %s: %s\nwarning: no patches were sent\n"),
642 $f, $error);
Kevin Ballard300913b2008-06-25 15:44:40 -0700643 }
Jeff Kingc764a0c2008-01-18 09:20:10 -0500644 }
Jeff King747bbff2008-01-18 09:19:48 -0500645}
646
Jeff Kingaa548922008-01-18 09:19:36 -0500647if (@files) {
648 unless ($quiet) {
649 print $_,"\n" for (@files);
650 }
651} else {
Vasco Almeida46493102016-12-14 11:54:36 -0100652 print STDERR __("\nNo patch files specified!\n\n");
Jeff Kingaa548922008-01-18 09:19:36 -0500653 usage();
654}
655
Ævar Arnfjörð Bjarmasonacf071b2010-09-30 13:42:57 +0000656sub get_patch_subject {
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100657 my $fn = shift;
658 open (my $fh, '<', $fn);
659 while (my $line = <$fh>) {
660 next unless ($line =~ /^Subject: (.*)$/);
661 close $fh;
662 return "GIT: $1\n";
663 }
664 close $fh;
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100665 die sprintf(__("No subject line in %s?"), $fn);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100666}
667
668if ($compose) {
669 # Note that this does not need to be secure, but we will make a small
670 # effort to have it be unique
Jay Soffianafe756c2009-02-23 13:51:37 -0500671 $compose_filename = ($repo ?
672 tempfile(".gitsendemail.msg.XXXXXX", DIR => $repo->repo_path()) :
673 tempfile(".gitsendemail.msg.XXXXXX", DIR => "."))[1];
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000674 open my $c, ">", $compose_filename
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100675 or die sprintf(__("Failed to open for writing %s: %s"), $compose_filename, $!);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100676
677
678 my $tpl_sender = $sender || $repoauthor || $repocommitter || '';
679 my $tpl_subject = $initial_subject || '';
680 my $tpl_reply_to = $initial_reply_to || '';
681
Vasco Almeida70aedfb2016-12-14 11:54:38 -0100682 print $c <<EOT1, Git::prefix_lines("GIT: ", __ <<EOT2), <<EOT3;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100683From $tpl_sender # This line is ignored.
Vasco Almeida70aedfb2016-12-14 11:54:38 -0100684EOT1
685Lines beginning in "GIT:" will be removed.
686Consider including an overall diffstat or table of contents
687for the patch you are writing.
688
689Clear the body content if you don't wish to send a summary.
690EOT2
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100691From: $tpl_sender
692Subject: $tpl_subject
693In-Reply-To: $tpl_reply_to
694
Vasco Almeida70aedfb2016-12-14 11:54:38 -0100695EOT3
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100696 for my $f (@files) {
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000697 print $c get_patch_subject($f);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100698 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000699 close $c;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100700
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100701 if ($annotate) {
702 do_edit($compose_filename, @files);
703 } else {
704 do_edit($compose_filename);
705 }
706
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000707 open my $c2, ">", $compose_filename . ".final"
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100708 or die sprintf(__("Failed to open %s.final: %s"), $compose_filename, $!);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100709
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000710 open $c, "<", $compose_filename
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100711 or die sprintf(__("Failed to open %s: %s"), $compose_filename, $!);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100712
713 my $need_8bit_cte = file_has_nonascii($compose_filename);
714 my $in_body = 0;
715 my $summary_empty = 1;
Krzysztof Mazur4a47a4d2012-10-22 14:41:48 +0200716 if (!defined $compose_encoding) {
717 $compose_encoding = "UTF-8";
718 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000719 while(<$c>) {
Michael Witten40e6e8a2009-04-13 13:23:50 -0500720 next if m/^GIT:/;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100721 if ($in_body) {
722 $summary_empty = 0 unless (/^\n$/);
723 } elsif (/^\n$/) {
724 $in_body = 1;
725 if ($need_8bit_cte) {
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000726 print $c2 "MIME-Version: 1.0\n",
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100727 "Content-Type: text/plain; ",
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200728 "charset=$compose_encoding\n",
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100729 "Content-Transfer-Encoding: 8bit\n";
730 }
731 } elsif (/^MIME-Version:/i) {
732 $need_8bit_cte = 0;
733 } elsif (/^Subject:\s*(.+)\s*$/i) {
734 $initial_subject = $1;
735 my $subject = $initial_subject;
736 $_ = "Subject: " .
Krzysztof Mazurce547802012-10-24 23:08:26 +0200737 quote_subject($subject, $compose_encoding) .
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100738 "\n";
739 } elsif (/^In-Reply-To:\s*(.+)\s*$/i) {
740 $initial_reply_to = $1;
741 next;
742 } elsif (/^From:\s*(.+)\s*$/i) {
743 $sender = $1;
744 next;
745 } elsif (/^(?:To|Cc|Bcc):/i) {
Vasco Almeida46493102016-12-14 11:54:36 -0100746 print __("To/Cc/Bcc fields are not interpreted yet, they have been ignored\n");
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100747 next;
748 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000749 print $c2 $_;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100750 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000751 close $c;
752 close $c2;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100753
754 if ($summary_empty) {
Vasco Almeida46493102016-12-14 11:54:36 -0100755 print __("Summary email is empty, skipping it\n");
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100756 $compose = -1;
757 }
758} elsif ($annotate) {
759 do_edit(@files);
760}
761
Jay Soffian6e182512009-03-28 21:39:10 -0400762sub ask {
763 my ($prompt, %arg) = @_;
Jay Soffian0da43a62009-04-04 23:23:21 -0400764 my $valid_re = $arg{valid_re};
Jay Soffian6e182512009-03-28 21:39:10 -0400765 my $default = $arg{default};
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700766 my $confirm_only = $arg{confirm_only};
Jay Soffian6e182512009-03-28 21:39:10 -0400767 my $resp;
768 my $i = 0;
Jay Soffian5906f542009-03-31 12:22:11 -0400769 return defined $default ? $default : undef
770 unless defined $term->IN and defined fileno($term->IN) and
771 defined $term->OUT and defined fileno($term->OUT);
Jay Soffian6e182512009-03-28 21:39:10 -0400772 while ($i++ < 10) {
773 $resp = $term->readline($prompt);
774 if (!defined $resp) { # EOF
775 print "\n";
776 return defined $default ? $default : undef;
777 }
778 if ($resp eq '' and defined $default) {
779 return $default;
780 }
Jay Soffian0da43a62009-04-04 23:23:21 -0400781 if (!defined $valid_re or $resp =~ /$valid_re/) {
Jay Soffian6e182512009-03-28 21:39:10 -0400782 return $resp;
783 }
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700784 if ($confirm_only) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100785 my $yesno = $term->readline(
786 # TRANSLATORS: please keep [y/N] as is.
787 sprintf(__("Are you sure you want to use <%s> [y/N]? "), $resp));
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700788 if (defined $yesno && $yesno =~ /y/i) {
789 return $resp;
790 }
791 }
Jay Soffian6e182512009-03-28 21:39:10 -0400792 }
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -0700793 return;
Jay Soffian6e182512009-03-28 21:39:10 -0400794}
795
Thomas Rast3cae7e52010-06-17 22:10:39 +0200796my %broken_encoding;
797
Ævar Arnfjörð Bjarmason1d50bfd2010-09-30 13:42:58 +0000798sub file_declares_8bit_cte {
Thomas Rast3cae7e52010-06-17 22:10:39 +0200799 my $fn = shift;
800 open (my $fh, '<', $fn);
801 while (my $line = <$fh>) {
802 last if ($line =~ /^$/);
803 return 1 if ($line =~ /^Content-Transfer-Encoding: .*8bit.*$/);
804 }
805 close $fh;
806 return 0;
807}
808
809foreach my $f (@files) {
810 next unless (body_or_subject_has_nonascii($f)
811 && !file_declares_8bit_cte($f));
812 $broken_encoding{$f} = 1;
813}
814
815if (!defined $auto_8bit_encoding && scalar %broken_encoding) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100816 print __("The following files are 8bit, but do not declare " .
817 "a Content-Transfer-Encoding.\n");
Thomas Rast3cae7e52010-06-17 22:10:39 +0200818 foreach my $f (sort keys %broken_encoding) {
819 print " $f\n";
820 }
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100821 $auto_8bit_encoding = ask(__("Which 8bit encoding should I declare [UTF-8]? "),
Junio C Hamano852a15d2015-02-13 12:20:25 -0800822 valid_re => qr/.{4}/, confirm_only => 1,
Thomas Rast3cae7e52010-06-17 22:10:39 +0200823 default => "UTF-8");
824}
825
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200826if (!$force) {
827 for my $f (@files) {
Ævar Arnfjörð Bjarmason0d290a42010-09-30 13:43:01 +0000828 if (get_patch_subject($f) =~ /\Q*** SUBJECT HERE ***\E/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100829 die sprintf(__("Refusing to send because the patch\n\t%s\n"
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200830 . "has the template subject '*** SUBJECT HERE ***'. "
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100831 . "Pass --force if you really want to send.\n"), $f);
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200832 }
833 }
834}
835
Remi Lespinetc46e27a2015-06-30 14:16:47 +0200836if (defined $sender) {
Remi Lespinetfa5b1aa2015-06-30 14:16:51 +0200837 $sender =~ s/^\s+|\s+$//g;
Remi Lespinetc46e27a2015-06-30 14:16:47 +0200838 ($sender) = expand_aliases($sender);
839} else {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100840 $sender = $repoauthor || $repocommitter || '';
Ryan Anderson83b24432005-07-31 04:17:25 -0400841}
842
Michael S. Tsirkinda187592013-06-05 21:11:00 +0300843# $sender could be an already sanitized address
844# (e.g. sendemail.from could be manually sanitized by user).
845# But it's a no-op to run sanitize_address on an already sanitized address.
846$sender = sanitize_address($sender);
847
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100848my $to_whom = __("To whom should the emails be sent (if anyone)?");
Felipe Contreras8cac13d2012-11-24 12:16:19 +0100849my $prompting = 0;
Junio C Hamano8796ff72010-10-26 22:02:03 -0700850if (!@initial_to && !defined $to_cmd) {
Junio C Hamano0d6b21e2016-04-24 12:31:44 -0700851 my $to = ask("$to_whom ",
Stephen Boyd61837492012-09-06 11:31:11 -0700852 default => "",
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700853 valid_re => qr/\@.*\./, confirm_only => 1);
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700854 push @initial_to, parse_address_line($to) if defined $to; # sanitized/validated later
Ryan Anderson1f038a02005-09-05 01:13:07 -0400855 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -0400856}
857
Eric Wong994d6c62006-05-14 19:13:44 -0700858sub expand_aliases {
Jeff King302e04e2009-07-23 07:09:29 -0400859 return map { expand_one_alias($_) } @_;
860}
861
862my %EXPANDED_ALIASES;
863sub expand_one_alias {
864 my $alias = shift;
865 if ($EXPANDED_ALIASES{$alias}) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100866 die sprintf(__("fatal: alias '%s' expands to itself\n"), $alias);
Jeff King302e04e2009-07-23 07:09:29 -0400867 }
868 local $EXPANDED_ALIASES{$alias} = 1;
869 return $aliases{$alias} ? expand_aliases(@{$aliases{$alias}}) : $alias;
Eric Wong994d6c62006-05-14 19:13:44 -0700870}
871
Remi Lespinetb5e112d2015-06-30 14:16:45 +0200872@initial_to = process_address_list(@initial_to);
873@initial_cc = process_address_list(@initial_cc);
874@bcclist = process_address_list(@bcclist);
Eric Wong994d6c62006-05-14 19:13:44 -0700875
Adam Roben5483c712007-06-27 20:59:37 -0700876if ($thread && !defined $initial_reply_to && $prompting) {
Jay Soffian6e182512009-03-28 21:39:10 -0400877 $initial_reply_to = ask(
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100878 __("Message-ID to be used as In-Reply-To for the first email (if any)? "),
Stephen Boyd61837492012-09-06 11:31:11 -0700879 default => "",
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700880 valid_re => qr/\@.*\./, confirm_only => 1);
Ryan Anderson83b24432005-07-31 04:17:25 -0400881}
Jay Soffian1ca3d6e2008-02-20 00:55:07 -0500882if (defined $initial_reply_to) {
Jay Soffian0fb7fc72008-02-21 19:16:04 -0500883 $initial_reply_to =~ s/^\s*<?//;
884 $initial_reply_to =~ s/>?\s*$//;
885 $initial_reply_to = "<$initial_reply_to>" if $initial_reply_to ne '';
Junio C Hamanoace9c2a2007-12-10 21:44:42 -0800886}
Mike Hommeyace72082007-12-09 18:17:28 +0100887
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900888if (!defined $smtp_server) {
Florian Klink1ab2fd42017-11-28 01:49:04 +0100889 my @sendmail_paths = qw( /usr/sbin/sendmail /usr/lib/sendmail );
890 push @sendmail_paths, map {"$_/sendmail"} split /:/, $ENV{PATH};
891 foreach (@sendmail_paths) {
Eric Wongaca7ad72006-05-15 02:34:44 -0700892 if (-x $_) {
893 $smtp_server = $_;
894 last;
895 }
896 }
897 $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
Ryan Anderson3342d852005-07-31 20:04:24 -0400898}
899
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500900if ($compose && $compose > 0) {
901 @files = ($compose_filename . ".final", @files);
Ryan Anderson1f038a02005-09-05 01:13:07 -0400902}
903
Ryan Anderson83b24432005-07-31 04:17:25 -0400904# Variables we set as part of the loop over files
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500905our ($message_id, %mail, $subject, $reply_to, $references, $message,
Jay Soffiandc1460a2009-03-31 12:22:12 -0400906 $needs_confirm, $message_num, $ask_default);
Ryan Anderson83b24432005-07-31 04:17:25 -0400907
Eric Wong567ffeb2006-03-25 16:47:12 -0800908sub extract_valid_address {
909 my $address = shift;
Ævar Arnfjörð Bjarmason35b6ab92010-09-30 19:03:31 +0000910 my $local_part_regexp = qr/[^<>"\s@]+/;
911 my $domain_regexp = qr/[^.<>"\s@]+(?:\.[^.<>"\s@]+)+/;
Eric Wongdb3106b2006-05-15 02:41:01 -0700912
913 # check for a local address:
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700914 return $address if ($address =~ /^($local_part_regexp)$/);
Eric Wongdb3106b2006-05-15 02:41:01 -0700915
Uwe Kleine-König155197e2007-08-09 15:27:57 +0200916 $address =~ s/^\s*<(.*)>\s*$/$1/;
Eric Wong567ffeb2006-03-25 16:47:12 -0800917 if ($have_email_valid) {
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700918 return scalar Email::Valid->address($address);
Eric Wong567ffeb2006-03-25 16:47:12 -0800919 }
Krzysztof Mazur95c0d4b2012-11-22 19:12:09 +0100920
921 # less robust/correct than the monster regexp in Email::Valid,
922 # but still does a 99% job, and one less dependency
923 return $1 if $address =~ /($local_part_regexp\@$domain_regexp)/;
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -0700924 return;
Eric Wong567ffeb2006-03-25 16:47:12 -0800925}
Ryan Anderson83b24432005-07-31 04:17:25 -0400926
Krzysztof Mazure4312252012-11-22 19:12:10 +0100927sub extract_valid_address_or_die {
928 my $address = shift;
929 $address = extract_valid_address($address);
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100930 die sprintf(__("error: unable to extract a valid address from: %s\n"), $address)
Krzysztof Mazure4312252012-11-22 19:12:10 +0100931 if !$address;
932 return $address;
933}
934
935sub validate_address {
936 my $address = shift;
Krzysztof Mazurd0e98102012-11-22 19:12:12 +0100937 while (!extract_valid_address($address)) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100938 printf STDERR __("error: unable to extract a valid address from: %s\n"), $address;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100939 # TRANSLATORS: Make sure to include [q] [d] [e] in your
940 # translation. The program will only accept English input
941 # at this point.
942 $_ = ask(__("What to do with this address? ([q]uit|[d]rop|[e]dit): "),
Krzysztof Mazurd0e98102012-11-22 19:12:12 +0100943 valid_re => qr/^(?:quit|q|drop|d|edit|e)/i,
Krzysztof Mazur5c80afe2012-11-22 19:12:11 +0100944 default => 'q');
945 if (/^d/i) {
946 return undef;
947 } elsif (/^q/i) {
948 cleanup_compose_files();
949 exit(0);
950 }
Junio C Hamano0d6b21e2016-04-24 12:31:44 -0700951 $address = ask("$to_whom ",
Krzysztof Mazurd0e98102012-11-22 19:12:12 +0100952 default => "",
953 valid_re => qr/\@.*\./, confirm_only => 1);
Krzysztof Mazure4312252012-11-22 19:12:10 +0100954 }
955 return $address;
956}
957
958sub validate_address_list {
959 return (grep { defined $_ }
960 map { validate_address($_) } @_);
Ryan Anderson83b24432005-07-31 04:17:25 -0400961}
962
963# Usually don't need to change anything below here.
964
965# we make a "fake" message id by taking the current number
966# of seconds since the beginning of Unix time and tacking on
967# a random number to the end, in case we are called quicker than
968# 1 second since the last time we were called.
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400969
970# We'll setup a template for the message id, using the "from" address:
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400971
Junio C Hamanobe510cf2007-09-17 21:18:20 -0700972my ($message_id_stamp, $message_id_serial);
Brian Gernhardt68ce9332010-04-10 10:53:53 -0400973sub make_message_id {
Junio C Hamanobe510cf2007-09-17 21:18:20 -0700974 my $uniq;
975 if (!defined $message_id_stamp) {
Eric Wongf916ab02016-04-06 20:07:14 +0000976 $message_id_stamp = strftime("%Y%m%d%H%M%S.$$", gmtime(time));
Junio C Hamanobe510cf2007-09-17 21:18:20 -0700977 $message_id_serial = 0;
978 }
979 $message_id_serial++;
980 $uniq = "$message_id_stamp-$message_id_serial";
981
Junio C Hamanoaeb59322007-06-20 13:47:34 -0700982 my $du_part;
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200983 for ($sender, $repocommitter, $repoauthor) {
984 $du_part = extract_valid_address(sanitize_address($_));
985 last if (defined $du_part and $du_part ne '');
Junio C Hamanoaeb59322007-06-20 13:47:34 -0700986 }
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200987 if (not defined $du_part or $du_part eq '') {
Ævar Arnfjörð Bjarmason529dd382010-09-30 13:43:08 +0000988 require Sys::Hostname;
Junio C Hamanoaeb59322007-06-20 13:47:34 -0700989 $du_part = 'user@' . Sys::Hostname::hostname();
990 }
Eric Wongf916ab02016-04-06 20:07:14 +0000991 my $message_id_template = "<%s-%s>";
Junio C Hamanobe510cf2007-09-17 21:18:20 -0700992 $message_id = sprintf($message_id_template, $uniq, $du_part);
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400993 #print "new message id = $message_id\n"; # Was useful for debugging
Ryan Anderson83b24432005-07-31 04:17:25 -0400994}
995
996
997
Eric Wonga5370b12006-03-25 03:01:01 -0800998$time = time - scalar $#files;
Ryan Anderson83b24432005-07-31 04:17:25 -0400999
Jürgen Rühle374c5902007-01-10 13:36:39 -08001000sub unquote_rfc2047 {
1001 local ($_) = @_;
Роман Донченко11f70a72014-12-14 18:59:46 +03001002 my $charset;
Роман Донченкоab47e2a2014-12-14 18:59:47 +03001003 my $sep = qr/[ \t]+/;
1004 s{$re_encoded_word(?:$sep$re_encoded_word)*}{
1005 my @words = split $sep, $&;
1006 foreach (@words) {
1007 m/$re_encoded_word/;
1008 $charset = $1;
1009 my $encoding = $2;
1010 my $text = $3;
1011 if ($encoding eq 'q' || $encoding eq 'Q') {
1012 $_ = $text;
1013 s/_/ /g;
1014 s/=([0-9A-F]{2})/chr(hex($1))/egi;
1015 } else {
1016 # other encodings not supported yet
1017 }
Роман Донченко11f70a72014-12-14 18:59:46 +03001018 }
Роман Донченкоab47e2a2014-12-14 18:59:47 +03001019 join '', @words;
Thomas Rastb622d4d2012-07-30 21:25:40 +02001020 }eg;
Роман Донченко11f70a72014-12-14 18:59:46 +03001021 return wantarray ? ($_, $charset) : $_;
Jürgen Rühle374c5902007-01-10 13:36:39 -08001022}
1023
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001024sub quote_rfc2047 {
1025 local $_ = shift;
Brandon Caseyd1fff6f2009-06-06 20:12:31 -05001026 my $encoding = shift || 'UTF-8';
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001027 s/([^-a-zA-Z0-9!*+\/])/sprintf("=%02X", ord($1))/eg;
1028 s/(.*)/=\?$encoding\?q\?$1\?=/;
1029 return $_;
1030}
1031
Brandon Caseya3a82622009-06-07 19:25:58 -05001032sub is_rfc2047_quoted {
1033 my $s = shift;
Brandon Caseya3a82622009-06-07 19:25:58 -05001034 length($s) <= 75 &&
Роман Донченко11f70a72014-12-14 18:59:46 +03001035 $s =~ m/^(?:"[[:ascii:]]*"|$re_encoded_word)$/o;
Brandon Caseya3a82622009-06-07 19:25:58 -05001036}
1037
Krzysztof Mazurce547802012-10-24 23:08:26 +02001038sub subject_needs_rfc2047_quoting {
1039 my $s = shift;
1040
Krzysztof Mazurce1459f2012-10-24 23:28:29 +02001041 return ($s =~ /[^[:ascii:]]/) || ($s =~ /=\?/);
Krzysztof Mazurce547802012-10-24 23:08:26 +02001042}
1043
1044sub quote_subject {
1045 local $subject = shift;
1046 my $encoding = shift || 'UTF-8';
1047
1048 if (subject_needs_rfc2047_quoting($subject)) {
1049 return quote_rfc2047($subject, $encoding);
1050 }
1051 return $subject;
1052}
1053
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001054# use the simplest quoting being able to handle the recipient
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001055sub sanitize_address {
Robin H. Johnson732263d2007-04-25 19:37:19 -07001056 my ($recipient) = @_;
Krzysztof Mazur831a4882012-11-22 19:12:08 +01001057
1058 # remove garbage after email address
1059 $recipient =~ s/(.*>).*$/$1/;
1060
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001061 my ($recipient_name, $recipient_addr) = ($recipient =~ /^(.*?)\s*(<.*)/);
1062
1063 if (not $recipient_name) {
Ævar Arnfjörð Bjarmasonff483892010-09-30 13:43:02 +00001064 return $recipient;
Robin H. Johnson732263d2007-04-25 19:37:19 -07001065 }
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001066
1067 # if recipient_name is already quoted, do nothing
Brandon Caseya3a82622009-06-07 19:25:58 -05001068 if (is_rfc2047_quoted($recipient_name)) {
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001069 return $recipient;
1070 }
1071
Remi Lespinet1fe97032015-06-30 14:16:49 +02001072 # remove non-escaped quotes
1073 $recipient_name =~ s/(^|[^\\])"/$1/g;
1074
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001075 # rfc2047 is needed if a non-ascii char is included
1076 if ($recipient_name =~ /[^[:ascii:]]/) {
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001077 $recipient_name = quote_rfc2047($recipient_name);
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001078 }
1079
1080 # double quotes are needed if specials or CTLs are included
1081 elsif ($recipient_name =~ /[][()<>@,;:\\".\000-\037\177]/) {
Remi Lespinet1fe97032015-06-30 14:16:49 +02001082 $recipient_name =~ s/([\\\r])/\\$1/g;
Ævar Arnfjörð Bjarmasond5c7d692010-09-30 13:43:03 +00001083 $recipient_name = qq["$recipient_name"];
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001084 }
1085
1086 return "$recipient_name $recipient_addr";
1087
Robin H. Johnson732263d2007-04-25 19:37:19 -07001088}
1089
Matthieu Moycb2922f2017-08-23 12:21:01 +02001090sub strip_garbage_one_address {
1091 my ($addr) = @_;
1092 chomp $addr;
1093 if ($addr =~ /^(("[^"]*"|[^"<]*)? *<[^>]*>).*/) {
1094 # "Foo Bar" <foobar@example.com> [possibly garbage here]
1095 # Foo Bar <foobar@example.com> [possibly garbage here]
1096 return $1;
1097 }
1098 if ($addr =~ /^(<[^>]*>).*/) {
1099 # <foo@example.com> [possibly garbage here]
1100 # if garbage contains other addresses, they are ignored.
1101 return $1;
1102 }
1103 if ($addr =~ /^([^"#,\s]*)/) {
1104 # address without quoting: remove anything after the address
1105 return $1;
1106 }
1107 return $addr;
1108}
1109
Krzysztof Mazure4312252012-11-22 19:12:10 +01001110sub sanitize_address_list {
1111 return (map { sanitize_address($_) } @_);
1112}
1113
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001114sub process_address_list {
Remi Lespinetb1c8a112015-06-30 14:16:50 +02001115 my @addr_list = map { parse_address_line($_) } @_;
1116 @addr_list = expand_aliases(@addr_list);
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001117 @addr_list = sanitize_address_list(@addr_list);
1118 @addr_list = validate_address_list(@addr_list);
1119 return @addr_list;
1120}
1121
Jari Aalto134550f2010-03-14 17:16:45 +02001122# Returns the local Fully Qualified Domain Name (FQDN) if available.
1123#
1124# Tightly configured MTAa require that a caller sends a real DNS
1125# domain name that corresponds the IP address in the HELO/EHLO
1126# handshake. This is used to verify the connection and prevent
1127# spammers from trying to hide their identity. If the DNS and IP don't
1128# match, the receiveing MTA may deny the connection.
1129#
1130# Here is a deny example of Net::SMTP with the default "localhost.localdomain"
1131#
1132# Net::SMTP=GLOB(0x267ec28)>>> EHLO localhost.localdomain
1133# Net::SMTP=GLOB(0x267ec28)<<< 550 EHLO argument does not match calling host
1134#
1135# This maildomain*() code is based on ideas in Perl library Test::Reporter
1136# /usr/share/perl5/Test/Reporter/Mail/Util.pm ==> sub _maildomain ()
1137
Brian Gernhardt59a86302010-04-10 10:53:54 -04001138sub valid_fqdn {
1139 my $domain = shift;
Brandon Casey61ef5e92010-09-26 22:18:01 -05001140 return defined $domain && !($^O eq 'darwin' && $domain =~ /\.local$/) && $domain =~ /\./;
Brian Gernhardt59a86302010-04-10 10:53:54 -04001141}
1142
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001143sub maildomain_net {
Jari Aalto134550f2010-03-14 17:16:45 +02001144 my $maildomain;
1145
1146 if (eval { require Net::Domain; 1 }) {
1147 my $domain = Net::Domain::domainname();
Brian Gernhardt59a86302010-04-10 10:53:54 -04001148 $maildomain = $domain if valid_fqdn($domain);
Jari Aalto134550f2010-03-14 17:16:45 +02001149 }
1150
1151 return $maildomain;
1152}
1153
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001154sub maildomain_mta {
Jari Aalto134550f2010-03-14 17:16:45 +02001155 my $maildomain;
1156
1157 if (eval { require Net::SMTP; 1 }) {
1158 for my $host (qw(mailhost localhost)) {
1159 my $smtp = Net::SMTP->new($host);
1160 if (defined $smtp) {
1161 my $domain = $smtp->domain;
1162 $smtp->quit;
1163
Brian Gernhardt59a86302010-04-10 10:53:54 -04001164 $maildomain = $domain if valid_fqdn($domain);
Jari Aalto134550f2010-03-14 17:16:45 +02001165
1166 last if $maildomain;
1167 }
1168 }
1169 }
1170
1171 return $maildomain;
1172}
1173
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001174sub maildomain {
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001175 return maildomain_net() || maildomain_mta() || 'localhost.localdomain';
Jari Aalto134550f2010-03-14 17:16:45 +02001176}
1177
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001178sub smtp_host_string {
1179 if (defined $smtp_server_port) {
1180 return "$smtp_server:$smtp_server_port";
1181 } else {
1182 return $smtp_server;
1183 }
1184}
1185
1186# Returns 1 if authentication succeeded or was not necessary
1187# (smtp_user was not specified), and 0 otherwise.
1188
1189sub smtp_auth_maybe {
1190 if (!defined $smtp_authuser || $auth) {
1191 return 1;
1192 }
1193
1194 # Workaround AUTH PLAIN/LOGIN interaction defect
1195 # with Authen::SASL::Cyrus
1196 eval {
1197 require Authen::SASL;
1198 Authen::SASL->import(qw(Perl));
1199 };
1200
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001201 # Check mechanism naming as defined in:
1202 # https://tools.ietf.org/html/rfc4422#page-8
Brian Norris904f6e72015-09-18 15:12:50 -07001203 if ($smtp_auth && $smtp_auth !~ /^(\b[A-Z0-9-_]{1,20}\s*)*$/) {
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001204 die "invalid smtp auth: '${smtp_auth}'";
1205 }
1206
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001207 # TODO: Authentication may fail not because credentials were
1208 # invalid but due to other reasons, in which we should not
1209 # reject credentials.
1210 $auth = Git::credential({
1211 'protocol' => 'smtp',
1212 'host' => smtp_host_string(),
1213 'username' => $smtp_authuser,
1214 # if there's no password, "git credential fill" will
1215 # give us one, otherwise it'll just pass this one.
1216 'password' => $smtp_authpass
1217 }, sub {
1218 my $cred = shift;
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001219
1220 if ($smtp_auth) {
1221 my $sasl = Authen::SASL->new(
1222 mechanism => $smtp_auth,
1223 callback => {
1224 user => $cred->{'username'},
1225 pass => $cred->{'password'},
1226 authname => $cred->{'username'},
1227 }
1228 );
1229
1230 return !!$smtp->auth($sasl);
1231 }
1232
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001233 return !!$smtp->auth($cred->{'username'}, $cred->{'password'});
1234 });
1235
1236 return $auth;
1237}
1238
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001239sub ssl_verify_params {
1240 eval {
1241 require IO::Socket::SSL;
1242 IO::Socket::SSL->import(qw/SSL_VERIFY_PEER SSL_VERIFY_NONE/);
1243 };
1244 if ($@) {
1245 print STDERR "Not using SSL_VERIFY_PEER due to out-of-date IO::Socket::SSL.\n";
1246 return;
1247 }
1248
1249 if (!defined $smtp_ssl_cert_path) {
Ruben Kerkhof01645b72014-01-15 21:31:11 +04001250 # use the OpenSSL defaults
1251 return (SSL_verify_mode => SSL_VERIFY_PEER());
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001252 }
1253
1254 if ($smtp_ssl_cert_path eq "") {
1255 return (SSL_verify_mode => SSL_VERIFY_NONE());
1256 } elsif (-d $smtp_ssl_cert_path) {
1257 return (SSL_verify_mode => SSL_VERIFY_PEER(),
1258 SSL_ca_path => $smtp_ssl_cert_path);
1259 } elsif (-f $smtp_ssl_cert_path) {
1260 return (SSL_verify_mode => SSL_VERIFY_PEER(),
1261 SSL_ca_file => $smtp_ssl_cert_path);
1262 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001263 die sprintf(__("CA path \"%s\" does not exist"), $smtp_ssl_cert_path);
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001264 }
1265}
1266
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001267sub file_name_is_absolute {
1268 my ($path) = @_;
1269
1270 # msys does not grok DOS drive-prefixes
1271 if ($^O eq 'msys') {
Junio C Hamanof24ecf52014-04-23 09:37:38 -07001272 return ($path =~ m#^/# || $path =~ m#^[a-zA-Z]\:#)
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001273 }
1274
1275 require File::Spec::Functions;
1276 return File::Spec::Functions::file_name_is_absolute($path);
1277}
1278
Michael Witten15da1082009-04-13 13:23:51 -05001279# Returns 1 if the message was sent, and 0 otherwise.
Markus Heidelberga1b5b372009-06-12 12:51:42 +02001280# In actuality, the whole program dies when there
Michael Witten15da1082009-04-13 13:23:51 -05001281# is an error sending a message.
1282
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001283sub send_message {
Eric Wong4bc87a22006-03-25 17:20:48 -08001284 my @recipients = unique_email_list(@to);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001285 @cc = (grep { my $cc = extract_valid_address_or_die($_);
Joe Perches83acaae2010-11-20 15:06:05 -08001286 not grep { $cc eq $_ || $_ =~ /<\Q${cc}\E>$/ } @recipients
Ask Bjørn Hansen7ac17522007-11-19 03:00:26 -08001287 }
Ask Bjørn Hansen7ac17522007-11-19 03:00:26 -08001288 @cc);
Eric Wong4bc87a22006-03-25 17:20:48 -08001289 my $to = join (",\n\t", @recipients);
Ryan Anderson58063242006-05-29 12:30:13 -07001290 @recipients = unique_email_list(@recipients,@cc,@bcclist);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001291 @recipients = (map { extract_valid_address_or_die($_) } @recipients);
Jakub Narebski6bdca892006-07-07 20:57:55 +02001292 my $date = format_2822_time($time++);
Martin Langhoffe923eff2006-05-03 09:44:36 +12001293 my $gitversion = '@@GIT_VERSION@@';
1294 if ($gitversion =~ m/..GIT_VERSION../) {
Petr Baudis3cb8caf2006-07-03 22:47:58 +02001295 $gitversion = Git::version();
Martin Langhoffe923eff2006-05-03 09:44:36 +12001296 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001297
Joe Perches02461e02009-10-08 10:03:26 -07001298 my $cc = join(",\n\t", unique_email_list(@cc));
Junio C Hamanof06a6a42007-04-16 16:51:47 -07001299 my $ccline = "";
1300 if ($cc ne '') {
1301 $ccline = "\nCc: $cc";
1302 }
Jeff King4f3d3702007-12-17 15:51:34 -05001303 make_message_id() unless defined($message_id);
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001304
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001305 my $header = "From: $sender
Junio C Hamanof06a6a42007-04-16 16:51:47 -07001306To: $to${ccline}
Eric Wong4bc87a22006-03-25 17:20:48 -08001307Subject: $subject
Eric Wong4bc87a22006-03-25 17:20:48 -08001308Date: $date
1309Message-Id: $message_id
Eric Wong4bc87a22006-03-25 17:20:48 -08001310";
Luis Henriquesac1596a2014-03-24 21:38:27 +00001311 if ($use_xmailer) {
1312 $header .= "X-Mailer: git-send-email $gitversion\n";
1313 }
Thomas Rast3e0c4ff2009-03-01 23:45:41 +01001314 if ($reply_to) {
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001315
1316 $header .= "In-Reply-To: $reply_to\n";
1317 $header .= "References: $references\n";
1318 }
Junio C Hamanoce91c2f2006-10-05 16:36:49 -07001319 if (@xh) {
1320 $header .= join("\n", @xh) . "\n";
1321 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001322
Robin H. Johnsonc38f0242007-04-25 19:37:20 -07001323 my @sendmail_parameters = ('-i', @recipients);
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001324 my $raw_from = $sender;
Felipe Contrerasc89e3242009-11-26 21:04:29 +02001325 if (defined $envelope_sender && $envelope_sender ne "auto") {
1326 $raw_from = $envelope_sender;
1327 }
Robin H. Johnsonf073a592007-04-25 19:37:22 -07001328 $raw_from = extract_valid_address($raw_from);
1329 unshift (@sendmail_parameters,
1330 '-f', $raw_from) if(defined $envelope_sender);
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001331
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001332 if ($needs_confirm && !$dry_run) {
1333 print "\n$header\n";
1334 if ($needs_confirm eq "inform") {
1335 $confirm_unconfigured = 0; # squelch this message for the rest of this run
Jay Soffian6e182512009-03-28 21:39:10 -04001336 $ask_default = "y"; # assume yes on EOF since user hasn't explicitly asked for confirmation
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001337 print __ <<EOF ;
1338 The Cc list above has been expanded by additional
1339 addresses found in the patch commit message. By default
1340 send-email prompts before sending whenever this occurs.
1341 This behavior is controlled by the sendemail.confirm
1342 configuration setting.
1343
1344 For additional information, run 'git send-email --help'.
1345 To retain the current behavior, but squelch this message,
1346 run 'git config --global sendemail.confirm auto'.
1347
1348EOF
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001349 }
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001350 # TRANSLATORS: Make sure to include [y] [n] [q] [a] in your
1351 # translation. The program will only accept English input
1352 # at this point.
1353 $_ = ask(__("Send this email? ([y]es|[n]o|[q]uit|[a]ll): "),
Jay Soffian6e182512009-03-28 21:39:10 -04001354 valid_re => qr/^(?:yes|y|no|n|quit|q|all|a)/i,
1355 default => $ask_default);
Vasco Almeida46493102016-12-14 11:54:36 -01001356 die __("Send this email reply required") unless defined $_;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001357 if (/^n/i) {
Michael Witten15da1082009-04-13 13:23:51 -05001358 return 0;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001359 } elsif (/^q/i) {
1360 cleanup_compose_files();
1361 exit(0);
1362 } elsif (/^a/i) {
1363 $confirm = 'never';
1364 }
1365 }
1366
Pascal Obry052fbea2010-09-06 20:12:11 +02001367 unshift (@sendmail_parameters, @smtp_server_options);
1368
Matthew Wilcox61302592006-10-10 08:58:23 -06001369 if ($dry_run) {
1370 # We don't want to send the email.
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001371 } elsif (file_name_is_absolute($smtp_server)) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001372 my $pid = open my $sm, '|-';
1373 defined $pid or die $!;
1374 if (!$pid) {
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001375 exec($smtp_server, @sendmail_parameters) or die $!;
Eric Wongaca7ad72006-05-15 02:34:44 -07001376 }
1377 print $sm "$header\n$message";
Ævar Arnfjörð Bjarmason5e2c2ab2010-09-30 13:43:07 +00001378 close $sm or die $!;
Eric Wongaca7ad72006-05-15 02:34:44 -07001379 } else {
Junio C Hamano44b24762007-09-25 17:27:54 -07001380
1381 if (!defined $smtp_server) {
Vasco Almeida46493102016-12-14 11:54:36 -01001382 die __("The required SMTP server is not properly defined.")
Junio C Hamano44b24762007-09-25 17:27:54 -07001383 }
1384
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001385 require Net::SMTP;
Jonathan Niederbfbfc9a2017-05-31 17:17:43 -07001386 my $use_net_smtp_ssl = version->parse($Net::SMTP::VERSION) < version->parse("2.34");
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001387 $smtp_domain ||= maildomain();
1388
Thomas Rastf6bebd12008-06-25 21:42:43 +02001389 if ($smtp_encryption eq 'ssl') {
Junio C Hamano44b24762007-09-25 17:27:54 -07001390 $smtp_server_port ||= 465; # ssmtp
Thomas Rast5508f3e2013-12-01 23:48:43 +01001391 require IO::Socket::SSL;
John Keeping9d605242015-12-03 21:47:18 +00001392
1393 # Suppress "variable accessed once" warning.
1394 {
1395 no warnings 'once';
1396 $IO::Socket::SSL::DEBUG = 1;
1397 }
1398
Thomas Rast5508f3e2013-12-01 23:48:43 +01001399 # Net::SMTP::SSL->new() does not forward any SSL options
1400 IO::Socket::SSL::set_client_defaults(
1401 ssl_verify_params());
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001402
1403 if ($use_net_smtp_ssl) {
1404 require Net::SMTP::SSL;
1405 $smtp ||= Net::SMTP::SSL->new($smtp_server,
1406 Hello => $smtp_domain,
1407 Port => $smtp_server_port,
1408 Debug => $debug_net_smtp);
1409 }
1410 else {
1411 $smtp ||= Net::SMTP->new($smtp_server,
1412 Hello => $smtp_domain,
1413 Port => $smtp_server_port,
1414 Debug => $debug_net_smtp,
1415 SSL => 1);
1416 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001417 }
1418 else {
brian m. carlson1a741bf2013-07-04 22:04:52 +00001419 $smtp_server_port ||= 25;
1420 $smtp ||= Net::SMTP->new($smtp_server,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001421 Hello => $smtp_domain,
brian m. carlson1a741bf2013-07-04 22:04:52 +00001422 Debug => $debug_net_smtp,
1423 Port => $smtp_server_port);
Yakov Lernerfb3650e2009-09-25 15:10:21 -07001424 if ($smtp_encryption eq 'tls' && $smtp) {
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001425 if ($use_net_smtp_ssl) {
1426 $smtp->command('STARTTLS');
1427 $smtp->response();
1428 if ($smtp->code != 220) {
1429 die sprintf(__("Server does not support STARTTLS! %s"), $smtp->message);
1430 }
1431 require Net::SMTP::SSL;
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001432 $smtp = Net::SMTP::SSL->start_SSL($smtp,
1433 ssl_verify_params())
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001434 or die sprintf(__("STARTTLS failed! %s"), IO::Socket::SSL::errstr());
Thomas Rastf6bebd12008-06-25 21:42:43 +02001435 }
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001436 else {
1437 $smtp->starttls(ssl_verify_params())
1438 or die sprintf(__("STARTTLS failed! %s"), IO::Socket::SSL::errstr());
1439 }
1440 $smtp_encryption = '';
1441 # Send EHLO again to receive fresh
1442 # supported commands
1443 $smtp->hello($smtp_domain);
Thomas Rastf6bebd12008-06-25 21:42:43 +02001444 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001445 }
Junio C Hamano44b24762007-09-25 17:27:54 -07001446
1447 if (!$smtp) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001448 die __("Unable to initialize SMTP properly. Check config and use --smtp-debug."),
1449 " VALUES: server=$smtp_server ",
Jari Aaltoe5afb3a2010-03-14 17:15:33 +02001450 "encryption=$smtp_encryption ",
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001451 "hello=$smtp_domain",
Sylvain Rabota1dd7e12011-04-29 20:23:24 +02001452 defined $smtp_server_port ? " port=$smtp_server_port" : "";
Junio C Hamano44b24762007-09-25 17:27:54 -07001453 }
1454
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001455 smtp_auth_maybe or die $smtp->message;
Michael Witten2363d742008-02-03 19:53:56 -05001456
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001457 $smtp->mail( $raw_from ) or die $smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001458 $smtp->to( @recipients ) or die $smtp->message;
1459 $smtp->data or die $smtp->message;
Stefan Agnerf60c4832015-09-30 09:26:09 +02001460 $smtp->datasend("$header\n") or die $smtp->message;
1461 my @lines = split /^/, $message;
1462 foreach my $line (@lines) {
1463 $smtp->datasend("$line") or die $smtp->message;
1464 }
Eric Wongaca7ad72006-05-15 02:34:44 -07001465 $smtp->dataend() or die $smtp->message;
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001466 $smtp->code =~ /250|200/ or die sprintf(__("Failed to send %s\n"), $subject).$smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001467 }
Ryan Anderson27184352006-02-05 20:13:52 -05001468 if ($quiet) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001469 printf($dry_run ? __("Dry-Sent %s\n") : __("Sent %s\n"), $subject);
Ryan Anderson27184352006-02-05 20:13:52 -05001470 } else {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001471 print($dry_run ? __("Dry-OK. Log says:\n") : __("OK. Log says:\n"));
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001472 if (!file_name_is_absolute($smtp_server)) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001473 print "Server: $smtp_server\n";
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001474 print "MAIL FROM:<$raw_from>\n";
Joe Perches02461e02009-10-08 10:03:26 -07001475 foreach my $entry (@recipients) {
1476 print "RCPT TO:<$entry>\n";
1477 }
Eric Wongaca7ad72006-05-15 02:34:44 -07001478 } else {
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001479 print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001480 }
David D. Kilzerb7f30e02007-11-18 20:14:55 -08001481 print $header, "\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001482 if ($smtp) {
Vasco Almeida46493102016-12-14 11:54:36 -01001483 print __("Result: "), $smtp->code, ' ',
Eric Wongaca7ad72006-05-15 02:34:44 -07001484 ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
1485 } else {
Vasco Almeida46493102016-12-14 11:54:36 -01001486 print __("Result: OK\n");
Eric Wongaca7ad72006-05-15 02:34:44 -07001487 }
Ryan Anderson30d08b32006-02-02 11:56:06 -05001488 }
Michael Witten15da1082009-04-13 13:23:51 -05001489
1490 return 1;
Ryan Anderson83b24432005-07-31 04:17:25 -04001491}
1492
Ryan Anderson83b24432005-07-31 04:17:25 -04001493$reply_to = $initial_reply_to;
Junio C Hamano2186d562006-05-29 23:53:13 -07001494$references = $initial_reply_to || '';
Ryan Anderson83b24432005-07-31 04:17:25 -04001495$subject = $initial_subject;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001496$message_num = 0;
Ryan Anderson83b24432005-07-31 04:17:25 -04001497
1498foreach my $t (@files) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001499 open my $fh, "<", $t or die sprintf(__("can't open file %s"), $t);
Ryan Anderson83b24432005-07-31 04:17:25 -04001500
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001501 my $author = undef;
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001502 my $sauthor = undef;
Jeff King8291db62007-11-16 05:49:09 -05001503 my $author_encoding;
1504 my $has_content_type;
1505 my $body_encoding;
Paolo Bonzinibb294562014-11-25 15:00:26 +01001506 my $xfer_encoding;
1507 my $has_mime_version;
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001508 @to = ();
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001509 @cc = ();
Junio C Hamanoce91c2f2006-10-05 16:36:49 -07001510 @xh = ();
Junio C Hamanoe6b09642006-10-07 03:09:05 -07001511 my $input_format = undef;
Jay Soffian50126992009-02-14 23:32:14 -05001512 my @header = ();
Ryan Anderson83b24432005-07-31 04:17:25 -04001513 $message = "";
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001514 $message_num++;
Jay Soffian50126992009-02-14 23:32:14 -05001515 # First unfold multiline header fields
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001516 while(<$fh>) {
Jay Soffian50126992009-02-14 23:32:14 -05001517 last if /^\s*$/;
1518 if (/^\s+\S/ and @header) {
1519 chomp($header[$#header]);
1520 s/^\s+/ /;
1521 $header[$#header] .= $_;
1522 } else {
1523 push(@header, $_);
1524 }
1525 }
1526 # Now parse the header
1527 foreach(@header) {
1528 if (/^From /) {
1529 $input_format = 'mbox';
1530 next;
1531 }
1532 chomp;
1533 if (!defined $input_format && /^[-A-Za-z]+:\s/) {
1534 $input_format = 'mbox';
1535 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001536
Jay Soffian50126992009-02-14 23:32:14 -05001537 if (defined $input_format && $input_format eq 'mbox') {
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001538 if (/^Subject:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001539 $subject = $1;
1540 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001541 elsif (/^From:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001542 ($author, $author_encoding) = unquote_rfc2047($1);
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001543 $sauthor = sanitize_address($author);
Jay Soffian50126992009-02-14 23:32:14 -05001544 next if $suppress_cc{'author'};
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001545 next if $suppress_cc{'self'} and $sauthor eq $sender;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001546 printf(__("(mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001547 $1, $_) unless $quiet;
1548 push @cc, $1;
1549 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001550 elsif (/^To:\s+(.*)$/i) {
Stephen Boyd21802cd2010-09-29 00:26:44 -07001551 foreach my $addr (parse_address_line($1)) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001552 printf(__("(mbox) Adding to: %s from line '%s'\n"),
Stephen Boyd21802cd2010-09-29 00:26:44 -07001553 $addr, $_) unless $quiet;
Krzysztof Mazure4312252012-11-22 19:12:10 +01001554 push @to, $addr;
Stephen Boyd21802cd2010-09-29 00:26:44 -07001555 }
1556 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001557 elsif (/^Cc:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001558 foreach my $addr (parse_address_line($1)) {
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001559 my $qaddr = unquote_rfc2047($addr);
1560 my $saddr = sanitize_address($qaddr);
1561 if ($saddr eq $sender) {
David Brown65648282007-12-25 19:56:29 -08001562 next if ($suppress_cc{'self'});
David Brown65648282007-12-25 19:56:29 -08001563 } else {
1564 next if ($suppress_cc{'cc'});
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001565 }
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001566 printf(__("(mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001567 $addr, $_) unless $quiet;
1568 push @cc, $addr;
Ryan Anderson83b24432005-07-31 04:17:25 -04001569 }
1570 }
Jay Soffian50126992009-02-14 23:32:14 -05001571 elsif (/^Content-type:/i) {
1572 $has_content_type = 1;
1573 if (/charset="?([^ "]+)/) {
1574 $body_encoding = $1;
1575 }
1576 push @xh, $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001577 }
Paolo Bonzinibb294562014-11-25 15:00:26 +01001578 elsif (/^MIME-Version/i) {
1579 $has_mime_version = 1;
1580 push @xh, $_;
1581 }
Jay Soffian50126992009-02-14 23:32:14 -05001582 elsif (/^Message-Id: (.*)/i) {
1583 $message_id = $1;
1584 }
Paolo Bonzinibb294562014-11-25 15:00:26 +01001585 elsif (/^Content-Transfer-Encoding: (.*)/i) {
1586 $xfer_encoding = $1 if not defined $xfer_encoding;
1587 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001588 elsif (!/^Date:\s/i && /^[-A-Za-z]+:\s+\S/) {
Jay Soffian50126992009-02-14 23:32:14 -05001589 push @xh, $_;
1590 }
1591
Ryan Anderson83b24432005-07-31 04:17:25 -04001592 } else {
Jay Soffian50126992009-02-14 23:32:14 -05001593 # In the traditional
1594 # "send lots of email" format,
1595 # line 1 = cc
1596 # line 2 = subject
1597 # So let's support that, too.
1598 $input_format = 'lots';
1599 if (@cc == 0 && !$suppress_cc{'cc'}) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001600 printf(__("(non-mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001601 $_, $_) unless $quiet;
1602 push @cc, $_;
1603 } elsif (!defined $subject) {
1604 $subject = $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001605 }
1606 }
1607 }
Jay Soffian50126992009-02-14 23:32:14 -05001608 # Now parse the message body
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001609 while(<$fh>) {
Jay Soffian50126992009-02-14 23:32:14 -05001610 $message .= $_;
Matthieu Moycb2922f2017-08-23 12:21:01 +02001611 if (/^(Signed-off-by|Cc): (.*)/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001612 chomp;
Jay Soffian3531e272009-02-14 23:32:15 -05001613 my ($what, $c) = ($1, $2);
Matthieu Moycb2922f2017-08-23 12:21:01 +02001614 # strip garbage for the address we'll use:
1615 $c = strip_garbage_one_address($c);
1616 # sanitize a bit more to decide whether to suppress the address:
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001617 my $sc = sanitize_address($c);
1618 if ($sc eq $sender) {
Jay Soffian3531e272009-02-14 23:32:15 -05001619 next if ($suppress_cc{'self'});
1620 } else {
1621 next if $suppress_cc{'sob'} and $what =~ /Signed-off-by/i;
1622 next if $suppress_cc{'bodycc'} and $what =~ /Cc/i;
1623 }
Jay Soffian50126992009-02-14 23:32:14 -05001624 push @cc, $c;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001625 printf(__("(body) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001626 $c, $_) unless $quiet;
1627 }
1628 }
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001629 close $fh;
Joe Perches324a8bd2007-08-17 18:51:12 -07001630
Joe Perches6e74e072010-09-24 10:03:00 -07001631 push @to, recipients_cmd("to-cmd", "to", $to_cmd, $t)
1632 if defined $to_cmd;
1633 push @cc, recipients_cmd("cc-cmd", "cc", $cc_cmd, $t)
1634 if defined $cc_cmd && !$suppress_cc{'cccmd'};
Joe Perches324a8bd2007-08-17 18:51:12 -07001635
Thomas Rast3cae7e52010-06-17 22:10:39 +02001636 if ($broken_encoding{$t} && !$has_content_type) {
Paolo Bonzinibb294562014-11-25 15:00:26 +01001637 $xfer_encoding = '8bit' if not defined $xfer_encoding;
Thomas Rast3cae7e52010-06-17 22:10:39 +02001638 $has_content_type = 1;
Paolo Bonzinibb294562014-11-25 15:00:26 +01001639 push @xh, "Content-Type: text/plain; charset=$auto_8bit_encoding";
Thomas Rast3cae7e52010-06-17 22:10:39 +02001640 $body_encoding = $auto_8bit_encoding;
1641 }
1642
Krzysztof Mazurce547802012-10-24 23:08:26 +02001643 if ($broken_encoding{$t} && !is_rfc2047_quoted($subject)) {
1644 $subject = quote_subject($subject, $auto_8bit_encoding);
Thomas Rast3cae7e52010-06-17 22:10:39 +02001645 }
1646
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001647 if (defined $sauthor and $sauthor ne $sender) {
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001648 $message = "From: $author\n\n$message";
Jeff King8291db62007-11-16 05:49:09 -05001649 if (defined $author_encoding) {
1650 if ($has_content_type) {
1651 if ($body_encoding eq $author_encoding) {
1652 # ok, we already have the right encoding
1653 }
1654 else {
1655 # uh oh, we should re-encode
1656 }
1657 }
1658 else {
Paolo Bonzinibb294562014-11-25 15:00:26 +01001659 $xfer_encoding = '8bit' if not defined $xfer_encoding;
Thomas Rast3cae7e52010-06-17 22:10:39 +02001660 $has_content_type = 1;
Jeff King8291db62007-11-16 05:49:09 -05001661 push @xh,
Paolo Bonzinibb294562014-11-25 15:00:26 +01001662 "Content-Type: text/plain; charset=$author_encoding";
Jeff King8291db62007-11-16 05:49:09 -05001663 }
1664 }
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001665 }
Paolo Bonzini8d814082014-11-25 15:00:27 +01001666 if (defined $target_xfer_encoding) {
1667 $xfer_encoding = '8bit' if not defined $xfer_encoding;
1668 $message = apply_transfer_encoding(
1669 $message, $xfer_encoding, $target_xfer_encoding);
1670 $xfer_encoding = $target_xfer_encoding;
1671 }
Paolo Bonzinibb294562014-11-25 15:00:26 +01001672 if (defined $xfer_encoding) {
1673 push @xh, "Content-Transfer-Encoding: $xfer_encoding";
1674 }
1675 if (defined $xfer_encoding or $has_content_type) {
1676 unshift @xh, 'MIME-Version: 1.0' unless $has_mime_version;
1677 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001678
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001679 $needs_confirm = (
1680 $confirm eq "always" or
1681 ($confirm =~ /^(?:auto|cc)$/ && @cc) or
1682 ($confirm =~ /^(?:auto|compose)$/ && $compose && $message_num == 1));
1683 $needs_confirm = "inform" if ($needs_confirm && $confirm_unconfigured && @cc);
1684
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001685 @to = process_address_list(@to);
1686 @cc = process_address_list(@cc);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001687
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001688 @to = (@initial_to, @to);
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001689 @cc = (@initial_cc, @cc);
1690
Michael S. Tsirkinf515c902014-04-29 08:41:16 +03001691 if ($message_num == 1) {
1692 if (defined $cover_cc and $cover_cc) {
1693 @initial_cc = @cc;
1694 }
1695 if (defined $cover_to and $cover_to) {
1696 @initial_to = @to;
1697 }
1698 }
1699
Michael Witten15da1082009-04-13 13:23:51 -05001700 my $message_was_sent = send_message();
Ryan Anderson83b24432005-07-31 04:17:25 -04001701
1702 # set up for the next message
Junio C Hamano95a877a2009-06-12 09:23:43 -07001703 if ($thread && $message_was_sent &&
Felipe Contrerasb99d22f2013-05-24 22:44:52 -05001704 ($chain_reply_to || !defined $reply_to || length($reply_to) == 0 ||
Antonio Ospitedb54c8e2010-11-12 15:55:08 +01001705 $message_num == 1)) {
Ryan Anderson78488b22005-07-31 20:04:24 -04001706 $reply_to = $message_id;
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001707 if (length $references > 0) {
YOSHIFUJI Hideaki / 吉藤英明a925b892007-04-06 08:50:24 +09001708 $references .= "\n $message_id";
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001709 } else {
1710 $references = "$message_id";
1711 }
Ryan Anderson78488b22005-07-31 20:04:24 -04001712 }
Jeff King4f3d3702007-12-17 15:51:34 -05001713 $message_id = undef;
xiaoqiang zhao5453b832017-05-21 20:59:50 +08001714 $num_sent++;
1715 if (defined $batch_size && $num_sent == $batch_size) {
1716 $num_sent = 0;
1717 $smtp->quit if defined $smtp;
1718 undef $smtp;
1719 undef $auth;
1720 sleep($relogin_delay) if defined $relogin_delay;
1721 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001722}
Ryan Andersone2057352005-08-02 21:45:22 -04001723
Joe Perches6e74e072010-09-24 10:03:00 -07001724# Execute a command (e.g. $to_cmd) to get a list of email addresses
1725# and return a results array
1726sub recipients_cmd {
1727 my ($prefix, $what, $cmd, $file) = @_;
1728
Joe Perches6e74e072010-09-24 10:03:00 -07001729 my @addresses = ();
Ramkumar Ramachandraa47eab02013-03-31 18:40:42 -07001730 open my $fh, "-|", "$cmd \Q$file\E"
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001731 or die sprintf(__("(%s) Could not execute '%s'"), $prefix, $cmd);
Junio C Hamano7ebee442010-10-26 22:02:52 -07001732 while (my $address = <$fh>) {
Joe Perches6e74e072010-09-24 10:03:00 -07001733 $address =~ s/^\s*//g;
1734 $address =~ s/\s*$//g;
1735 $address = sanitize_address($address);
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001736 next if ($address eq $sender and $suppress_cc{'self'});
Joe Perches6e74e072010-09-24 10:03:00 -07001737 push @addresses, $address;
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001738 printf(__("(%s) Adding %s: %s from: '%s'\n"),
1739 $prefix, $what, $address, $cmd) unless $quiet;
Joe Perches6e74e072010-09-24 10:03:00 -07001740 }
Junio C Hamano7ebee442010-10-26 22:02:52 -07001741 close $fh
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001742 or die sprintf(__("(%s) failed to close pipe to '%s'"), $prefix, $cmd);
Joe Perches6e74e072010-09-24 10:03:00 -07001743 return @addresses;
1744}
1745
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001746cleanup_compose_files();
Ryan Anderson1f038a02005-09-05 01:13:07 -04001747
Ævar Arnfjörð Bjarmason4bf597e2010-09-30 13:43:00 +00001748sub cleanup_compose_files {
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001749 unlink($compose_filename, $compose_filename . ".final") if $compose;
Ryan Anderson1f038a02005-09-05 01:13:07 -04001750}
1751
Eric Wong4bc87a22006-03-25 17:20:48 -08001752$smtp->quit if $smtp;
Ryan Andersone2057352005-08-02 21:45:22 -04001753
Paolo Bonzini8d814082014-11-25 15:00:27 +01001754sub apply_transfer_encoding {
1755 my $message = shift;
1756 my $from = shift;
1757 my $to = shift;
1758
1759 return $message if ($from eq $to and $from ne '7bit');
1760
1761 require MIME::QuotedPrint;
1762 require MIME::Base64;
1763
1764 $message = MIME::QuotedPrint::decode($message)
1765 if ($from eq 'quoted-printable');
1766 $message = MIME::Base64::decode($message)
1767 if ($from eq 'base64');
1768
Vasco Almeida46493102016-12-14 11:54:36 -01001769 die __("cannot send message as 7bit")
Paolo Bonzini8d814082014-11-25 15:00:27 +01001770 if ($to eq '7bit' and $message =~ /[^[:ascii:]]/);
1771 return $message
1772 if ($to eq '7bit' or $to eq '8bit');
1773 return MIME::QuotedPrint::encode($message, "\n", 0)
1774 if ($to eq 'quoted-printable');
1775 return MIME::Base64::encode($message, "\n")
1776 if ($to eq 'base64');
Vasco Almeida46493102016-12-14 11:54:36 -01001777 die __("invalid transfer encoding");
Paolo Bonzini8d814082014-11-25 15:00:27 +01001778}
1779
Ævar Arnfjörð Bjarmasonc438ea22010-09-30 13:42:59 +00001780sub unique_email_list {
Ryan Andersone2057352005-08-02 21:45:22 -04001781 my %seen;
1782 my @emails;
1783
1784 foreach my $entry (@_) {
Krzysztof Mazure4312252012-11-22 19:12:10 +01001785 my $clean = extract_valid_address_or_die($entry);
1786 $seen{$clean} ||= 0;
1787 next if $seen{$clean}++;
1788 push @emails, $entry;
Ryan Andersone2057352005-08-02 21:45:22 -04001789 }
1790 return @emails;
1791}
Jeff King747bbff2008-01-18 09:19:48 -05001792
1793sub validate_patch {
1794 my $fn = shift;
Jonathan Tan64896602017-05-12 15:38:26 -07001795
Jonathan Tan177409e2017-06-01 16:50:55 -07001796 if ($repo) {
1797 my $validate_hook = catfile(catdir($repo->repo_path(), 'hooks'),
1798 'sendemail-validate');
1799 my $hook_error;
1800 if (-x $validate_hook) {
1801 my $target = abs_path($fn);
1802 # The hook needs a correct cwd and GIT_DIR.
1803 my $cwd_save = cwd();
1804 chdir($repo->wc_path() or $repo->repo_path())
1805 or die("chdir: $!");
1806 local $ENV{"GIT_DIR"} = $repo->repo_path();
1807 $hook_error = "rejected by sendemail-validate hook"
1808 if system($validate_hook, $target);
1809 chdir($cwd_save) or die("chdir: $!");
1810 }
1811 return $hook_error if $hook_error;
Jonathan Tan64896602017-05-12 15:38:26 -07001812 }
Jonathan Tan64896602017-05-12 15:38:26 -07001813
Jeff King747bbff2008-01-18 09:19:48 -05001814 open(my $fh, '<', $fn)
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001815 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
Jeff King747bbff2008-01-18 09:19:48 -05001816 while (my $line = <$fh>) {
1817 if (length($line) > 998) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001818 return sprintf(__("%s: patch contains a line longer than 998 characters"), $.);
Jeff King747bbff2008-01-18 09:19:48 -05001819 }
1820 }
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -07001821 return;
Jeff King747bbff2008-01-18 09:19:48 -05001822}
Jeff King0706bd12008-03-28 17:28:33 -04001823
Junio C Hamano531220b2016-03-17 22:40:05 -07001824sub handle_backup {
1825 my ($last, $lastlen, $file, $known_suffix) = @_;
1826 my ($suffix, $skip);
1827
1828 $skip = 0;
1829 if (defined $last &&
1830 ($lastlen < length($file)) &&
1831 (substr($file, 0, $lastlen) eq $last) &&
1832 ($suffix = substr($file, $lastlen)) !~ /^[a-z0-9]/i) {
1833 if (defined $known_suffix && $suffix eq $known_suffix) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001834 printf(__("Skipping %s with backup suffix '%s'.\n"), $file, $known_suffix);
Junio C Hamano531220b2016-03-17 22:40:05 -07001835 $skip = 1;
1836 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001837 # TRANSLATORS: please keep "[y|N]" as is.
1838 my $answer = ask(sprintf(__("Do you really want to send %s? [y|N]: "), $file),
Junio C Hamano531220b2016-03-17 22:40:05 -07001839 valid_re => qr/^(?:y|n)/i,
1840 default => 'n');
1841 $skip = ($answer ne 'y');
1842 if ($skip) {
1843 $known_suffix = $suffix;
1844 }
1845 }
1846 }
1847 return ($skip, $known_suffix);
1848}
1849
1850sub handle_backup_files {
1851 my @file = @_;
1852 my ($last, $lastlen, $known_suffix, $skip, @result);
1853 for my $file (@file) {
1854 ($skip, $known_suffix) = handle_backup($last, $lastlen,
1855 $file, $known_suffix);
1856 push @result, $file unless $skip;
1857 $last = $file;
1858 $lastlen = length($file);
1859 }
1860 return @result;
1861}
1862
Jeff King0706bd12008-03-28 17:28:33 -04001863sub file_has_nonascii {
1864 my $fn = shift;
1865 open(my $fh, '<', $fn)
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001866 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
Jeff King0706bd12008-03-28 17:28:33 -04001867 while (my $line = <$fh>) {
1868 return 1 if $line =~ /[^[:ascii:]]/;
1869 }
1870 return 0;
1871}
Thomas Rast3cae7e52010-06-17 22:10:39 +02001872
1873sub body_or_subject_has_nonascii {
1874 my $fn = shift;
1875 open(my $fh, '<', $fn)
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001876 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
Thomas Rast3cae7e52010-06-17 22:10:39 +02001877 while (my $line = <$fh>) {
1878 last if $line =~ /^$/;
1879 return 1 if $line =~ /^Subject.*[^[:ascii:]]/;
1880 }
1881 while (my $line = <$fh>) {
1882 return 1 if $line =~ /[^[:ascii:]]/;
1883 }
1884 return 0;
1885}