blob: f0417f64e7513ad085c5227500569f23905f03e4 [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;
Ryan Anderson83b24432005-07-31 04:17:25 -040033
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +010034Getopt::Long::Configure qw/ pass_through /;
35
Junio C Hamano280242d2006-07-02 16:03:59 -070036package FakeTerm;
37sub new {
38 my ($class, $reason) = @_;
39 return bless \$reason, shift;
40}
41sub readline {
42 my $self = shift;
43 die "Cannot use readline on FakeTerm: $$self";
44}
45package main;
46
Michael Coleman1b0baf12007-02-27 22:47:54 -060047
48sub usage {
49 print <<EOT;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +010050git send-email [options] <file | directory | rev-list options >
Jacob Keller17b7a832015-11-19 14:52:11 -080051git send-email --dump-aliases
Michael Witten4ed62b02008-09-30 07:58:30 -050052
53 Composing:
54 --from <str> * Email From:
Stephen Boydf434c082010-03-07 14:46:48 -080055 --[no-]to <str> * Email To:
56 --[no-]cc <str> * Email Cc:
57 --[no-]bcc <str> * Email Bcc:
Michael Witten4ed62b02008-09-30 07:58:30 -050058 --subject <str> * Email "Subject:"
59 --in-reply-to <str> * Email "In-Reply-To:"
Luis Henriquesac1596a2014-03-24 21:38:27 +000060 --[no-]xmailer * Add "X-Mailer:" header (default).
Felipe Contreras402596a2013-04-07 01:10:27 -060061 --[no-]annotate * Review each patch that will be sent in an editor.
Michael Witten4ed62b02008-09-30 07:58:30 -050062 --compose * Open an editor for introduction.
Krzysztof Mazur62e00692012-10-10 01:02:56 +020063 --compose-encoding <str> * Encoding to assume for introduction.
Thomas Rast3cae7e52010-06-17 22:10:39 +020064 --8bit-encoding <str> * Encoding to assume 8bit mails if undeclared
Paolo Bonzini8d814082014-11-25 15:00:27 +010065 --transfer-encoding <str> * Transfer encoding to use (quoted-printable, 8bit, base64)
Michael Witten4ed62b02008-09-30 07:58:30 -050066
67 Sending:
68 --envelope-sender <str> * Email envelope sender.
69 --smtp-server <str:int> * Outgoing SMTP server to use. The port
70 is optional. Default 'localhost'.
Pascal Obry052fbea2010-09-06 20:12:11 +020071 --smtp-server-option <str> * Outgoing SMTP server option to use.
Michael Witten4ed62b02008-09-30 07:58:30 -050072 --smtp-server-port <int> * Outgoing SMTP server port.
73 --smtp-user <str> * Username for SMTP-AUTH.
74 --smtp-pass <str> * Password for SMTP-AUTH; not necessary.
75 --smtp-encryption <str> * tls or ssl; anything else disables.
76 --smtp-ssl * Deprecated. Use '--smtp-encryption ssl'.
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -070077 --smtp-ssl-cert-path <str> * Path to ca-certificates (either directory or file).
78 Pass an empty string to disable certificate
79 verification.
Jari Aalto134550f2010-03-14 17:16:45 +020080 --smtp-domain <str> * The domain name sent to HELO/EHLO handshake
Jan Viktorin0f2e68b2015-08-12 01:39:44 +020081 --smtp-auth <str> * Space-separated list of allowed AUTH mechanisms.
82 This setting forces to use one of the listed mechanisms.
Jari Aaltof60812e2010-03-14 17:16:09 +020083 --smtp-debug <0|1> * Disable, enable Net::SMTP debug.
Michael Witten4ed62b02008-09-30 07:58:30 -050084
85 Automating:
86 --identity <str> * Use the sendemail.<id> options.
Joe Perches6e74e072010-09-24 10:03:00 -070087 --to-cmd <str> * Email To: via `<str> \$patch_path`
Michael Witten4ed62b02008-09-30 07:58:30 -050088 --cc-cmd <str> * Email Cc: via `<str> \$patch_path`
Jay Soffian3531e272009-02-14 23:32:15 -050089 --suppress-cc <str> * author, self, sob, cc, cccmd, body, bodycc, all.
Michael S. Tsirkinf515c902014-04-29 08:41:16 +030090 --[no-]cc-cover * Email Cc: addresses in the cover letter.
91 --[no-]to-cover * Email To: addresses in the cover letter.
Jay Soffian3531e272009-02-14 23:32:15 -050092 --[no-]signed-off-by-cc * Send to Signed-off-by: addresses. Default on.
Michael Witten4ed62b02008-09-30 07:58:30 -050093 --[no-]suppress-from * Send to self. Default off.
Junio C Hamano41fe87f2009-08-22 12:48:48 -070094 --[no-]chain-reply-to * Chain In-Reply-To: fields. Default off.
Michael Witten4ed62b02008-09-30 07:58:30 -050095 --[no-]thread * Use In-Reply-To: field. Default on.
96
97 Administering:
Jay Soffianc1f2aa42009-03-02 23:52:18 -050098 --confirm <str> * Confirm recipients before sending;
99 auto, cc, compose, always, or never.
Michael Witten4ed62b02008-09-30 07:58:30 -0500100 --quiet * Output one line of info per email.
101 --dry-run * Don't actually send the emails.
102 --[no-]validate * Perform patch sanity checks. Default on.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100103 --[no-]format-patch * understand any non optional arguments as
104 `git format-patch` ones.
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200105 --force * Send even if safety checks would prevent it.
Jeff Kingc764a0c2008-01-18 09:20:10 -0500106
Jacob Keller17b7a832015-11-19 14:52:11 -0800107 Information:
108 --dump-aliases * Dump configured aliases and exit.
109
Michael Coleman1b0baf12007-02-27 22:47:54 -0600110EOT
111 exit(1);
112}
113
Eric Wong4bc87a22006-03-25 17:20:48 -0800114# most mail servers generate the Date: header, but not all...
Jakub Narebski6bdca892006-07-07 20:57:55 +0200115sub format_2822_time {
116 my ($time) = @_;
117 my @localtm = localtime($time);
118 my @gmttm = gmtime($time);
119 my $localmin = $localtm[1] + $localtm[2] * 60;
120 my $gmtmin = $gmttm[1] + $gmttm[2] * 60;
121 if ($localtm[0] != $gmttm[0]) {
Vasco Almeida46493102016-12-14 11:54:36 -0100122 die __("local zone differs from GMT by a non-minute interval\n");
Jakub Narebski6bdca892006-07-07 20:57:55 +0200123 }
124 if ((($gmttm[6] + 1) % 7) == $localtm[6]) {
125 $localmin += 1440;
126 } elsif ((($gmttm[6] - 1) % 7) == $localtm[6]) {
127 $localmin -= 1440;
128 } elsif ($gmttm[6] != $localtm[6]) {
Vasco Almeida46493102016-12-14 11:54:36 -0100129 die __("local time offset greater than or equal to 24 hours\n");
Jakub Narebski6bdca892006-07-07 20:57:55 +0200130 }
131 my $offset = $localmin - $gmtmin;
132 my $offhour = $offset / 60;
133 my $offmin = abs($offset % 60);
134 if (abs($offhour) >= 24) {
Vasco Almeida46493102016-12-14 11:54:36 -0100135 die __("local time offset greater than or equal to 24 hours\n");
Jakub Narebski6bdca892006-07-07 20:57:55 +0200136 }
137
138 return sprintf("%s, %2d %s %d %02d:%02d:%02d %s%02d%02d",
139 qw(Sun Mon Tue Wed Thu Fri Sat)[$localtm[6]],
140 $localtm[3],
141 qw(Jan Feb Mar Apr May Jun
142 Jul Aug Sep Oct Nov Dec)[$localtm[4]],
143 $localtm[5]+1900,
144 $localtm[2],
145 $localtm[1],
146 $localtm[0],
147 ($offset >= 0) ? '+' : '-',
148 abs($offhour),
149 $offmin,
150 );
151}
Eric Wong4bc87a22006-03-25 17:20:48 -0800152
Eric Wong567ffeb2006-03-25 16:47:12 -0800153my $have_email_valid = eval { require Email::Valid; 1 };
Jay Soffian50126992009-02-14 23:32:14 -0500154my $have_mail_address = eval { require Mail::Address; 1 };
Eric Wong4bc87a22006-03-25 17:20:48 -0800155my $smtp;
Wincent Colaiuta5f5b6112007-11-21 13:35:05 +0100156my $auth;
Eric Wong4bc87a22006-03-25 17:20:48 -0800157
Роман Донченко11f70a72014-12-14 18:59:46 +0300158# Regexes for RFC 2047 productions.
159my $re_token = qr/[^][()<>@,;:\\"\/?.= \000-\037\177-\377]+/;
160my $re_encoded_text = qr/[^? \000-\037\177-\377]+/;
161my $re_encoded_word = qr/=\?($re_token)\?($re_token)\?($re_encoded_text)\?=/;
162
Ryan Anderson83b24432005-07-31 04:17:25 -0400163# Variables we fill in automatically, or via prompting:
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700164my (@to,$no_to,@initial_to,@cc,$no_cc,@initial_cc,@bcclist,$no_bcc,@xh,
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100165 $initial_reply_to,$initial_subject,@files,
Luis Henriquesac1596a2014-03-24 21:38:27 +0000166 $author,$sender,$smtp_authpass,$annotate,$use_xmailer,$compose,$time);
Ryan Anderson83b24432005-07-31 04:17:25 -0400167
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700168my $envelope_sender;
Ryan Anderson78488b22005-07-31 20:04:24 -0400169
Ryan Anderson91332612005-07-31 20:04:24 -0400170# Example reply to:
Ryan Anderson83b24432005-07-31 04:17:25 -0400171#$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
Ryan Anderson83b24432005-07-31 04:17:25 -0400172
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100173my $repo = eval { Git->repository() };
174my @repo = $repo ? ($repo) : ();
Junio C Hamano280242d2006-07-02 16:03:59 -0700175my $term = eval {
Jay Soffian0fb7fc72008-02-21 19:16:04 -0500176 $ENV{"GIT_SEND_EMAIL_NOTTY"}
177 ? new Term::ReadLine 'git-send-email', \*STDIN, \*STDOUT
178 : new Term::ReadLine 'git-send-email';
Junio C Hamano280242d2006-07-02 16:03:59 -0700179};
180if ($@) {
181 $term = new FakeTerm "$@: going non-interactive";
182}
Ryan Anderson83b24432005-07-31 04:17:25 -0400183
Adam Roben5483c712007-06-27 20:59:37 -0700184# Behavior modification variables
185my ($quiet, $dry_run) = (0, 0);
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100186my $format_patch;
Jay Soffianafe756c2009-02-23 13:51:37 -0500187my $compose_filename;
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200188my $force = 0;
Jacob Keller17b7a832015-11-19 14:52:11 -0800189my $dump_aliases = 0;
Adam Roben5483c712007-06-27 20:59:37 -0700190
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100191# Handle interactive edition of files.
192my $multiedit;
Michael J Gruber0ce142c2010-03-22 17:12:53 +0100193my $editor;
Jonathan Niederb4479f02009-10-30 20:42:34 -0500194
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100195sub do_edit {
Michael J Gruber0ce142c2010-03-22 17:12:53 +0100196 if (!defined($editor)) {
197 $editor = Git::command_oneline('var', 'GIT_EDITOR');
198 }
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100199 if (defined($multiedit) && !$multiedit) {
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100200 map {
201 system('sh', '-c', $editor.' "$@"', $editor, $_);
202 if (($? & 127) || ($? >> 8)) {
Vasco Almeida46493102016-12-14 11:54:36 -0100203 die(__("the editor exited uncleanly, aborting everything"));
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100204 }
205 } @_;
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100206 } else {
207 system('sh', '-c', $editor.' "$@"', $editor, @_);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100208 if (($? & 127) || ($? >> 8)) {
Vasco Almeida46493102016-12-14 11:54:36 -0100209 die(__("the editor exited uncleanly, aborting everything"));
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100210 }
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100211 }
212}
Adam Roben5483c712007-06-27 20:59:37 -0700213
214# Variables with corresponding config settings
Joe Perches6e74e072010-09-24 10:03:00 -0700215my ($thread, $chain_reply_to, $suppress_from, $signed_off_by_cc);
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300216my ($cover_cc, $cover_to);
Joe Perches6e74e072010-09-24 10:03:00 -0700217my ($to_cmd, $cc_cmd);
Pascal Obry052fbea2010-09-06 20:12:11 +0200218my ($smtp_server, $smtp_server_port, @smtp_server_options);
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -0700219my ($smtp_authuser, $smtp_encryption, $smtp_ssl_cert_path);
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200220my ($identity, $aliasfiletype, @alias_files, $smtp_domain, $smtp_auth);
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500221my ($validate, $confirm);
David Brown65648282007-12-25 19:56:29 -0800222my (@suppress_cc);
Thomas Rast3cae7e52010-06-17 22:10:39 +0200223my ($auto_8bit_encoding);
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200224my ($compose_encoding);
Paolo Bonzini8d814082014-11-25 15:00:27 +0100225my ($target_xfer_encoding);
Adam Roben5483c712007-06-27 20:59:37 -0700226
Jari Aaltof60812e2010-03-14 17:16:09 +0200227my ($debug_net_smtp) = 0; # Net::SMTP, see send_message()
228
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900229my %config_bool_settings = (
Adam Roben5483c712007-06-27 20:59:37 -0700230 "thread" => [\$thread, 1],
Felipe Contrerasb99d22f2013-05-24 22:44:52 -0500231 "chainreplyto" => [\$chain_reply_to, 0],
David Brown65648282007-12-25 19:56:29 -0800232 "suppressfrom" => [\$suppress_from, undef],
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500233 "signedoffbycc" => [\$signed_off_by_cc, undef],
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300234 "cccover" => [\$cover_cc, undef],
235 "tocover" => [\$cover_to, undef],
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500236 "signedoffcc" => [\$signed_off_by_cc, undef], # Deprecated
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500237 "validate" => [\$validate, 1],
Felipe Contreras402596a2013-04-07 01:10:27 -0600238 "multiedit" => [\$multiedit, undef],
Luis Henriquesac1596a2014-03-24 21:38:27 +0000239 "annotate" => [\$annotate, undef],
240 "xmailer" => [\$use_xmailer, 1]
Adam Robene46f7a02007-06-26 15:48:30 -0700241);
242
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900243my %config_settings = (
244 "smtpserver" => \$smtp_server,
Junio C Hamano44b24762007-09-25 17:27:54 -0700245 "smtpserverport" => \$smtp_server_port,
Pascal Obry052fbea2010-09-06 20:12:11 +0200246 "smtpserveroption" => \@smtp_server_options,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900247 "smtpuser" => \$smtp_authuser,
248 "smtppass" => \$smtp_authpass,
Pascal Obrye1e91152010-09-06 20:12:09 +0200249 "smtpdomain" => \$smtp_domain,
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200250 "smtpauth" => \$smtp_auth,
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700251 "to" => \@initial_to,
Joe Perches6e74e072010-09-24 10:03:00 -0700252 "tocmd" => \$to_cmd,
Miklos Vajna5f8b9fc2008-04-27 14:14:58 +0200253 "cc" => \@initial_cc,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900254 "cccmd" => \$cc_cmd,
255 "aliasfiletype" => \$aliasfiletype,
256 "bcc" => \@bcclist,
David Brown65648282007-12-25 19:56:29 -0800257 "suppresscc" => \@suppress_cc,
Ask Bjørn Hansen9f7820a2008-06-07 00:33:42 -0700258 "envelopesender" => \$envelope_sender,
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500259 "confirm" => \$confirm,
Trent Piepho09caa242009-05-12 15:48:56 -0700260 "from" => \$sender,
Thomas Rast3cae7e52010-06-17 22:10:39 +0200261 "assume8bitencoding" => \$auto_8bit_encoding,
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200262 "composeencoding" => \$compose_encoding,
Paolo Bonzini8d814082014-11-25 15:00:27 +0100263 "transferencoding" => \$target_xfer_encoding,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900264);
Avi Kivity4a62d3f2007-03-11 19:19:44 +0200265
Cord Seelecec5dae2011-09-30 12:52:25 +0200266my %config_path_settings = (
267 "aliasesfile" => \@alias_files,
John Keeping6e07a3b2015-11-17 22:01:05 +0000268 "smtpsslcertpath" => \$smtp_ssl_cert_path,
Cord Seelecec5dae2011-09-30 12:52:25 +0200269);
270
Michael Witten87429972008-02-03 19:53:57 -0500271# Handle Uncouth Termination
272sub signal_handler {
273
274 # Make text normal
275 print color("reset"), "\n";
276
277 # SMTP password masked
278 system "stty echo";
279
280 # tmp files from --compose
Jay Soffianafe756c2009-02-23 13:51:37 -0500281 if (defined $compose_filename) {
282 if (-e $compose_filename) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100283 printf __("'%s' contains an intermediate version ".
284 "of the email you were composing.\n"),
285 $compose_filename;
Jay Soffianafe756c2009-02-23 13:51:37 -0500286 }
287 if (-e ($compose_filename . ".final")) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100288 printf __("'%s.final' contains the composed email.\n"),
289 $compose_filename;
Jay Soffianafe756c2009-02-23 13:51:37 -0500290 }
Michael Witten87429972008-02-03 19:53:57 -0500291 }
292
293 exit;
294};
295
296$SIG{TERM} = \&signal_handler;
297$SIG{INT} = \&signal_handler;
298
Ryan Anderson83b24432005-07-31 04:17:25 -0400299# Begin by accumulating all the variables (defined above), that we will end up
300# needing, first, from the command line:
301
Clemens Buchacherc5978242011-09-03 19:06:13 +0200302my $help;
303my $rc = GetOptions("h" => \$help,
Jacob Keller17b7a832015-11-19 14:52:11 -0800304 "dump-aliases" => \$dump_aliases);
305usage() unless $rc;
Vasco Almeida46493102016-12-14 11:54:36 -0100306die __("--dump-aliases incompatible with other options\n")
Jacob Keller17b7a832015-11-19 14:52:11 -0800307 if !$help and $dump_aliases and @ARGV;
308$rc = GetOptions(
Clemens Buchacherc5978242011-09-03 19:06:13 +0200309 "sender|from=s" => \$sender,
Ryan Anderson83b24432005-07-31 04:17:25 -0400310 "in-reply-to=s" => \$initial_reply_to,
311 "subject=s" => \$initial_subject,
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700312 "to=s" => \@initial_to,
Joe Perches6e74e072010-09-24 10:03:00 -0700313 "to-cmd=s" => \$to_cmd,
Stephen Boydf434c082010-03-07 14:46:48 -0800314 "no-to" => \$no_to,
Ryan Andersonda140f82006-02-13 03:05:15 -0500315 "cc=s" => \@initial_cc,
Stephen Boydf434c082010-03-07 14:46:48 -0800316 "no-cc" => \$no_cc,
Ryan Anderson58063242006-05-29 12:30:13 -0700317 "bcc=s" => \@bcclist,
Stephen Boydf434c082010-03-07 14:46:48 -0800318 "no-bcc" => \$no_bcc,
Ryan Anderson78488b22005-07-31 20:04:24 -0400319 "chain-reply-to!" => \$chain_reply_to,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800320 "no-chain-reply-to" => sub {$chain_reply_to = 0},
Ryan Anderson3342d852005-07-31 20:04:24 -0400321 "smtp-server=s" => \$smtp_server,
Pascal Obry052fbea2010-09-06 20:12:11 +0200322 "smtp-server-option=s" => \@smtp_server_options,
Junio C Hamano44b24762007-09-25 17:27:54 -0700323 "smtp-server-port=s" => \$smtp_server_port,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900324 "smtp-user=s" => \$smtp_authuser,
Michael Witten2363d742008-02-03 19:53:56 -0500325 "smtp-pass:s" => \$smtp_authpass,
Thomas Rastf6bebd12008-06-25 21:42:43 +0200326 "smtp-ssl" => sub { $smtp_encryption = 'ssl' },
327 "smtp-encryption=s" => \$smtp_encryption,
Thomas Rast979e6522013-12-01 23:48:42 +0100328 "smtp-ssl-cert-path=s" => \$smtp_ssl_cert_path,
Jari Aaltof60812e2010-03-14 17:16:09 +0200329 "smtp-debug:i" => \$debug_net_smtp,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -0400330 "smtp-domain:s" => \$smtp_domain,
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200331 "smtp-auth=s" => \$smtp_auth,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900332 "identity=s" => \$identity,
Felipe Contreras402596a2013-04-07 01:10:27 -0600333 "annotate!" => \$annotate,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800334 "no-annotate" => sub {$annotate = 0},
Ryan Anderson1f038a02005-09-05 01:13:07 -0400335 "compose" => \$compose,
Ryan Anderson30d08b32006-02-02 11:56:06 -0500336 "quiet" => \$quiet,
Joe Perches324a8bd2007-08-17 18:51:12 -0700337 "cc-cmd=s" => \$cc_cmd,
Adam Roben5483c712007-06-27 20:59:37 -0700338 "suppress-from!" => \$suppress_from,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800339 "no-suppress-from" => sub {$suppress_from = 0},
David Brown65648282007-12-25 19:56:29 -0800340 "suppress-cc=s" => \@suppress_cc,
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500341 "signed-off-cc|signed-off-by-cc!" => \$signed_off_by_cc,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800342 "no-signed-off-cc|no-signed-off-by-cc" => sub {$signed_off_by_cc = 0},
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300343 "cc-cover|cc-cover!" => \$cover_cc,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800344 "no-cc-cover" => sub {$cover_cc = 0},
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300345 "to-cover|to-cover!" => \$cover_to,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800346 "no-to-cover" => sub {$cover_to = 0},
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500347 "confirm=s" => \$confirm,
Matthew Wilcox61302592006-10-10 08:58:23 -0600348 "dry-run" => \$dry_run,
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700349 "envelope-sender=s" => \$envelope_sender,
Adam Roben5483c712007-06-27 20:59:37 -0700350 "thread!" => \$thread,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800351 "no-thread" => sub {$thread = 0},
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500352 "validate!" => \$validate,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800353 "no-validate" => sub {$validate = 0},
Paolo Bonzini8d814082014-11-25 15:00:27 +0100354 "transfer-encoding=s" => \$target_xfer_encoding,
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100355 "format-patch!" => \$format_patch,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800356 "no-format-patch" => sub {$format_patch = 0},
Thomas Rast3cae7e52010-06-17 22:10:39 +0200357 "8bit-encoding=s" => \$auto_8bit_encoding,
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200358 "compose-encoding=s" => \$compose_encoding,
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200359 "force" => \$force,
Luis Henriquesac1596a2014-03-24 21:38:27 +0000360 "xmailer!" => \$use_xmailer,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800361 "no-xmailer" => sub {$use_xmailer = 0},
Ryan Anderson83b24432005-07-31 04:17:25 -0400362 );
363
Clemens Buchacherc5978242011-09-03 19:06:13 +0200364usage() if $help;
Michael Coleman1b0baf12007-02-27 22:47:54 -0600365unless ($rc) {
366 usage();
367}
368
Vasco Almeida46493102016-12-14 11:54:36 -0100369die __("Cannot run git format-patch from outside a repository\n")
Jay Soffianeed6ca72009-02-14 23:32:13 -0500370 if $format_patch and not $repo;
371
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900372# Now, let's fill any that aren't set in with defaults:
373
374sub read_config {
375 my ($prefix) = @_;
376
377 foreach my $setting (keys %config_bool_settings) {
378 my $target = $config_bool_settings{$setting}->[0];
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100379 $$target = Git::config_bool(@repo, "$prefix.$setting") unless (defined $$target);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900380 }
381
Cord Seelecec5dae2011-09-30 12:52:25 +0200382 foreach my $setting (keys %config_path_settings) {
Cord Seele463b0ea2011-10-14 22:53:31 +0200383 my $target = $config_path_settings{$setting};
384 if (ref($target) eq "ARRAY") {
385 unless (@$target) {
386 my @values = Git::config_path(@repo, "$prefix.$setting");
387 @$target = @values if (@values && defined $values[0]);
388 }
389 }
390 else {
391 $$target = Git::config_path(@repo, "$prefix.$setting") unless (defined $$target);
392 }
Cord Seelecec5dae2011-09-30 12:52:25 +0200393 }
394
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900395 foreach my $setting (keys %config_settings) {
396 my $target = $config_settings{$setting};
Stephen Boydf434c082010-03-07 14:46:48 -0800397 next if $setting eq "to" and defined $no_to;
398 next if $setting eq "cc" and defined $no_cc;
399 next if $setting eq "bcc" and defined $no_bcc;
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900400 if (ref($target) eq "ARRAY") {
401 unless (@$target) {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100402 my @values = Git::config(@repo, "$prefix.$setting");
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900403 @$target = @values if (@values && defined $values[0]);
404 }
405 }
406 else {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100407 $$target = Git::config(@repo, "$prefix.$setting") unless (defined $$target);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900408 }
409 }
Thomas Rastf6bebd12008-06-25 21:42:43 +0200410
411 if (!defined $smtp_encryption) {
412 my $enc = Git::config(@repo, "$prefix.smtpencryption");
413 if (defined $enc) {
414 $smtp_encryption = $enc;
415 } elsif (Git::config_bool(@repo, "$prefix.smtpssl")) {
416 $smtp_encryption = 'ssl';
417 }
418 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900419}
420
421# read configuration from [sendemail "$identity"], fall back on [sendemail]
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100422$identity = Git::config(@repo, "sendemail.identity") unless (defined $identity);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900423read_config("sendemail.$identity") if (defined $identity);
424read_config("sendemail");
425
426# fall back on builtin bool defaults
427foreach my $setting (values %config_bool_settings) {
428 ${$setting->[0]} = $setting->[1] unless (defined (${$setting->[0]}));
429}
430
Thomas Rastfa835cd2008-06-26 23:03:21 +0200431# 'default' encryption is none -- this only prevents a warning
432$smtp_encryption = '' unless (defined $smtp_encryption);
433
David Brown65648282007-12-25 19:56:29 -0800434# Set CC suppressions
435my(%suppress_cc);
436if (@suppress_cc) {
437 foreach my $entry (@suppress_cc) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100438 die sprintf(__("Unknown --suppress-cc field: '%s'\n"), $entry)
Ævar Arnfjörð Bjarmasone9bf7412010-09-30 13:43:04 +0000439 unless $entry =~ /^(?:all|cccmd|cc|author|self|sob|body|bodycc)$/;
David Brown65648282007-12-25 19:56:29 -0800440 $suppress_cc{$entry} = 1;
441 }
442}
443
444if ($suppress_cc{'all'}) {
Paolo Bonzinicb8a9bd2009-06-18 14:31:32 +0200445 foreach my $entry (qw (cccmd cc author self sob body bodycc)) {
David Brown65648282007-12-25 19:56:29 -0800446 $suppress_cc{$entry} = 1;
447 }
448 delete $suppress_cc{'all'};
449}
450
451# If explicit old-style ones are specified, they trump --suppress-cc.
452$suppress_cc{'self'} = $suppress_from if defined $suppress_from;
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500453$suppress_cc{'sob'} = !$signed_off_by_cc if defined $signed_off_by_cc;
David Brown65648282007-12-25 19:56:29 -0800454
Jay Soffian3531e272009-02-14 23:32:15 -0500455if ($suppress_cc{'body'}) {
456 foreach my $entry (qw (sob bodycc)) {
457 $suppress_cc{$entry} = 1;
458 }
459 delete $suppress_cc{'body'};
460}
461
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500462# Set confirm's default value
463my $confirm_unconfigured = !defined $confirm;
464if ($confirm_unconfigured) {
465 $confirm = scalar %suppress_cc ? 'compose' : 'auto';
466};
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100467die sprintf(__("Unknown --confirm setting: '%s'\n"), $confirm)
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500468 unless $confirm =~ /^(?:auto|cc|compose|always|never)/;
469
David Brown65648282007-12-25 19:56:29 -0800470# Debugging, print out the suppressions.
471if (0) {
472 print "suppressions:\n";
473 foreach my $entry (keys %suppress_cc) {
474 printf " %-5s -> $suppress_cc{$entry}\n", $entry;
475 }
476}
477
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100478my ($repoauthor, $repocommitter);
479($repoauthor) = Git::ident_person(@repo, 'author');
480($repocommitter) = Git::ident_person(@repo, 'committer');
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900481
Jay Soffian50126992009-02-14 23:32:14 -0500482sub parse_address_line {
483 if ($have_mail_address) {
484 return map { $_->format } Mail::Address->parse($_[0]);
485 } else {
Remi Lespinet8d314d72015-07-07 15:38:15 +0200486 return Git::parse_mailboxes($_[0]);
Jay Soffian50126992009-02-14 23:32:14 -0500487 }
488}
489
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800490sub split_addrs {
Junio C Hamano2f0e7cb2008-12-21 01:57:59 -0800491 return quotewords('\s*,\s*', 1, @_);
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800492}
493
Eric Wong994d6c62006-05-14 19:13:44 -0700494my %aliases;
Eric Sunshine09f11572015-05-31 18:29:27 -0400495
496sub parse_sendmail_alias {
497 local $_ = shift;
498 if (/"/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100499 printf STDERR __("warning: sendmail alias with quotes is not supported: %s\n"), $_;
Eric Sunshine86b89842015-06-01 14:22:36 -0400500 } elsif (/:include:/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100501 printf STDERR __("warning: `:include:` not supported: %s\n"), $_;
Eric Sunshine86b89842015-06-01 14:22:36 -0400502 } elsif (/[\/|]/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100503 printf STDERR __("warning: `/file` or `|pipe` redirection not supported: %s\n"), $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400504 } elsif (/^(\S+?)\s*:\s*(.+)$/) {
505 my ($alias, $addr) = ($1, $2);
506 $aliases{$alias} = [ split_addrs($addr) ];
507 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100508 printf STDERR __("warning: sendmail line is not recognized: %s\n"), $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400509 }
510}
511
512sub parse_sendmail_aliases {
513 my $fh = shift;
Eric Sunshine2532dd02015-05-31 18:29:29 -0400514 my $s = '';
Eric Sunshine09f11572015-05-31 18:29:27 -0400515 while (<$fh>) {
Eric Sunshine2532dd02015-05-31 18:29:29 -0400516 chomp;
Eric Sunshine020be85f2015-05-31 18:29:28 -0400517 next if /^\s*$/ || /^\s*#/;
Eric Sunshine2532dd02015-05-31 18:29:29 -0400518 $s .= $_, next if $s =~ s/\\$// || s/^\s+//;
519 parse_sendmail_alias($s) if $s;
520 $s = $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400521 }
Eric Sunshine2532dd02015-05-31 18:29:29 -0400522 $s =~ s/\\$//; # silently tolerate stray '\' on last line
523 parse_sendmail_alias($s) if $s;
Eric Sunshine09f11572015-05-31 18:29:27 -0400524}
525
Eric Wong994d6c62006-05-14 19:13:44 -0700526my %parse_alias = (
527 # multiline formats can be supported in the future
528 mutt => sub { my $fh = shift; while (<$fh>) {
Felipe Contrerasffc01f92009-09-30 17:49:36 +0300529 if (/^\s*alias\s+(?:-group\s+\S+\s+)*(\S+)\s+(.*)$/) {
Eric Wong994d6c62006-05-14 19:13:44 -0700530 my ($alias, $addr) = ($1, $2);
531 $addr =~ s/#.*$//; # mutt allows # comments
Eric Wong2c510f22016-01-04 20:53:30 +0000532 # commas delimit multiple addresses
533 my @addr = split_addrs($addr);
534
535 # quotes may be escaped in the file,
536 # unescape them so we do not double-escape them later.
537 s/\\"/"/g foreach @addr;
538 $aliases{$alias} = \@addr
Eric Wong994d6c62006-05-14 19:13:44 -0700539 }}},
540 mailrc => sub { my $fh = shift; while (<$fh>) {
Jeff Kinga277d1e2016-03-17 19:58:22 -0400541 if (/^alias\s+(\S+)\s+(.*?)\s*$/) {
Eric Wong994d6c62006-05-14 19:13:44 -0700542 # spaces delimit multiple addresses
Eric W. Biedermanfe87c922009-05-20 19:45:53 -0700543 $aliases{$1} = [ quotewords('\s+', 0, $2) ];
Eric Wong994d6c62006-05-14 19:13:44 -0700544 }}},
Trent Piepho73c427e2008-11-25 18:55:00 -0800545 pine => sub { my $fh = shift; my $f='\t[^\t]*';
546 for (my $x = ''; defined($x); $x = $_) {
547 chomp $x;
548 $x .= $1 while(defined($_ = <$fh>) && /^ +(.*)$/);
549 $x =~ /^(\S+)$f\t\(?([^\t]+?)\)?(:?$f){0,2}$/ or next;
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800550 $aliases{$1} = [ split_addrs($2) ];
Trent Piepho73c427e2008-11-25 18:55:00 -0800551 }},
Bill Pemberton7613ea32009-04-22 09:41:29 -0400552 elm => sub { my $fh = shift;
553 while (<$fh>) {
554 if (/^(\S+)\s+=\s+[^=]+=\s(\S+)/) {
555 my ($alias, $addr) = ($1, $2);
556 $aliases{$alias} = [ split_addrs($addr) ];
557 }
558 } },
Eric Sunshine09f11572015-05-31 18:29:27 -0400559 sendmail => \&parse_sendmail_aliases,
Eric Wong994d6c62006-05-14 19:13:44 -0700560 gnus => sub { my $fh = shift; while (<$fh>) {
561 if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
562 $aliases{$1} = [ $2 ];
563 }}}
564);
565
Petr Baudis3cb8caf2006-07-03 22:47:58 +0200566if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) {
Eric Wong994d6c62006-05-14 19:13:44 -0700567 foreach my $file (@alias_files) {
568 open my $fh, '<', $file or die "opening $file: $!\n";
569 $parse_alias{$aliasfiletype}->($fh);
570 close $fh;
571 }
572}
573
Jacob Keller17b7a832015-11-19 14:52:11 -0800574if ($dump_aliases) {
575 print "$_\n" for (sort keys %aliases);
576 exit(0);
577}
578
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700579# is_format_patch_arg($f) returns 0 if $f names a patch, or 1 if
580# $f is a revision list specification to be passed to format-patch.
581sub is_format_patch_arg {
Jay Soffianeed6ca72009-02-14 23:32:13 -0500582 return unless $repo;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100583 my $f = shift;
584 try {
585 $repo->command('rev-parse', '--verify', '--quiet', $f);
586 if (defined($format_patch)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100587 return $format_patch;
588 }
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100589 die sprintf(__ <<EOF, $f, $f);
590File '%s' exists but it could also be the range of commits
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100591to produce patches for. Please disambiguate by...
592
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100593 * Saying "./%s" if you mean a file; or
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100594 * Giving --format-patch option if you mean a range.
595EOF
596 } catch Git::Error::Command with {
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700597 # Not a valid revision. Treat it as a filename.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100598 return 0;
599 }
600}
601
Jeff Kingaa548922008-01-18 09:19:36 -0500602# Now that all the defaults are set, process the rest of the command line
603# arguments and collect up the files that need to be processed.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100604my @rev_list_opts;
Junio C Hamano69f4ce52008-11-30 22:38:20 -0800605while (defined(my $f = shift @ARGV)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100606 if ($f eq "--") {
607 push @rev_list_opts, "--", @ARGV;
608 @ARGV = ();
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700609 } elsif (-d $f and !is_format_patch_arg($f)) {
Ævar Arnfjörð Bjarmasonc6038162010-09-30 13:42:54 +0000610 opendir my $dh, $f
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100611 or die sprintf(__("Failed to opendir %s: %s"), $f, $!);
Jeff Kingaa548922008-01-18 09:19:36 -0500612
Ævar Arnfjörð Bjarmason89bf1ba2010-09-14 19:02:24 +0000613 push @files, grep { -f $_ } map { catfile($f, $_) }
Ævar Arnfjörð Bjarmasonc6038162010-09-30 13:42:54 +0000614 sort readdir $dh;
615 closedir $dh;
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700616 } elsif ((-f $f or -p $f) and !is_format_patch_arg($f)) {
Jeff Kingaa548922008-01-18 09:19:36 -0500617 push @files, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500618 } else {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100619 push @rev_list_opts, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500620 }
621}
622
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100623if (@rev_list_opts) {
Vasco Almeida46493102016-12-14 11:54:36 -0100624 die __("Cannot run git format-patch from outside a repository\n")
Jay Soffianeed6ca72009-02-14 23:32:13 -0500625 unless $repo;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100626 push @files, $repo->command('format-patch', '-o', tempdir(CLEANUP => 1), @rev_list_opts);
627}
628
Junio C Hamano531220b2016-03-17 22:40:05 -0700629@files = handle_backup_files(@files);
630
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500631if ($validate) {
Jeff Kingc764a0c2008-01-18 09:20:10 -0500632 foreach my $f (@files) {
Kevin Ballard300913b2008-06-25 15:44:40 -0700633 unless (-p $f) {
634 my $error = validate_patch($f);
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100635 $error and die sprintf(__("fatal: %s: %s\nwarning: no patches were sent\n"),
636 $f, $error);
Kevin Ballard300913b2008-06-25 15:44:40 -0700637 }
Jeff Kingc764a0c2008-01-18 09:20:10 -0500638 }
Jeff King747bbff2008-01-18 09:19:48 -0500639}
640
Jeff Kingaa548922008-01-18 09:19:36 -0500641if (@files) {
642 unless ($quiet) {
643 print $_,"\n" for (@files);
644 }
645} else {
Vasco Almeida46493102016-12-14 11:54:36 -0100646 print STDERR __("\nNo patch files specified!\n\n");
Jeff Kingaa548922008-01-18 09:19:36 -0500647 usage();
648}
649
Ævar Arnfjörð Bjarmasonacf071b2010-09-30 13:42:57 +0000650sub get_patch_subject {
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100651 my $fn = shift;
652 open (my $fh, '<', $fn);
653 while (my $line = <$fh>) {
654 next unless ($line =~ /^Subject: (.*)$/);
655 close $fh;
656 return "GIT: $1\n";
657 }
658 close $fh;
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100659 die sprintf(__("No subject line in %s?"), $fn);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100660}
661
662if ($compose) {
663 # Note that this does not need to be secure, but we will make a small
664 # effort to have it be unique
Jay Soffianafe756c2009-02-23 13:51:37 -0500665 $compose_filename = ($repo ?
666 tempfile(".gitsendemail.msg.XXXXXX", DIR => $repo->repo_path()) :
667 tempfile(".gitsendemail.msg.XXXXXX", DIR => "."))[1];
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000668 open my $c, ">", $compose_filename
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100669 or die sprintf(__("Failed to open for writing %s: %s"), $compose_filename, $!);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100670
671
672 my $tpl_sender = $sender || $repoauthor || $repocommitter || '';
673 my $tpl_subject = $initial_subject || '';
674 my $tpl_reply_to = $initial_reply_to || '';
675
Vasco Almeida70aedfb2016-12-14 11:54:38 -0100676 print $c <<EOT1, Git::prefix_lines("GIT: ", __ <<EOT2), <<EOT3;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100677From $tpl_sender # This line is ignored.
Vasco Almeida70aedfb2016-12-14 11:54:38 -0100678EOT1
679Lines beginning in "GIT:" will be removed.
680Consider including an overall diffstat or table of contents
681for the patch you are writing.
682
683Clear the body content if you don't wish to send a summary.
684EOT2
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100685From: $tpl_sender
686Subject: $tpl_subject
687In-Reply-To: $tpl_reply_to
688
Vasco Almeida70aedfb2016-12-14 11:54:38 -0100689EOT3
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100690 for my $f (@files) {
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000691 print $c get_patch_subject($f);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100692 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000693 close $c;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100694
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100695 if ($annotate) {
696 do_edit($compose_filename, @files);
697 } else {
698 do_edit($compose_filename);
699 }
700
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000701 open my $c2, ">", $compose_filename . ".final"
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100702 or die sprintf(__("Failed to open %s.final: %s"), $compose_filename, $!);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100703
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000704 open $c, "<", $compose_filename
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100705 or die sprintf(__("Failed to open %s: %s"), $compose_filename, $!);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100706
707 my $need_8bit_cte = file_has_nonascii($compose_filename);
708 my $in_body = 0;
709 my $summary_empty = 1;
Krzysztof Mazur4a47a4d2012-10-22 14:41:48 +0200710 if (!defined $compose_encoding) {
711 $compose_encoding = "UTF-8";
712 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000713 while(<$c>) {
Michael Witten40e6e8a2009-04-13 13:23:50 -0500714 next if m/^GIT:/;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100715 if ($in_body) {
716 $summary_empty = 0 unless (/^\n$/);
717 } elsif (/^\n$/) {
718 $in_body = 1;
719 if ($need_8bit_cte) {
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000720 print $c2 "MIME-Version: 1.0\n",
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100721 "Content-Type: text/plain; ",
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200722 "charset=$compose_encoding\n",
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100723 "Content-Transfer-Encoding: 8bit\n";
724 }
725 } elsif (/^MIME-Version:/i) {
726 $need_8bit_cte = 0;
727 } elsif (/^Subject:\s*(.+)\s*$/i) {
728 $initial_subject = $1;
729 my $subject = $initial_subject;
730 $_ = "Subject: " .
Krzysztof Mazurce547802012-10-24 23:08:26 +0200731 quote_subject($subject, $compose_encoding) .
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100732 "\n";
733 } elsif (/^In-Reply-To:\s*(.+)\s*$/i) {
734 $initial_reply_to = $1;
735 next;
736 } elsif (/^From:\s*(.+)\s*$/i) {
737 $sender = $1;
738 next;
739 } elsif (/^(?:To|Cc|Bcc):/i) {
Vasco Almeida46493102016-12-14 11:54:36 -0100740 print __("To/Cc/Bcc fields are not interpreted yet, they have been ignored\n");
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100741 next;
742 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000743 print $c2 $_;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100744 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000745 close $c;
746 close $c2;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100747
748 if ($summary_empty) {
Vasco Almeida46493102016-12-14 11:54:36 -0100749 print __("Summary email is empty, skipping it\n");
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100750 $compose = -1;
751 }
752} elsif ($annotate) {
753 do_edit(@files);
754}
755
Jay Soffian6e182512009-03-28 21:39:10 -0400756sub ask {
757 my ($prompt, %arg) = @_;
Jay Soffian0da43a62009-04-04 23:23:21 -0400758 my $valid_re = $arg{valid_re};
Jay Soffian6e182512009-03-28 21:39:10 -0400759 my $default = $arg{default};
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700760 my $confirm_only = $arg{confirm_only};
Jay Soffian6e182512009-03-28 21:39:10 -0400761 my $resp;
762 my $i = 0;
Jay Soffian5906f542009-03-31 12:22:11 -0400763 return defined $default ? $default : undef
764 unless defined $term->IN and defined fileno($term->IN) and
765 defined $term->OUT and defined fileno($term->OUT);
Jay Soffian6e182512009-03-28 21:39:10 -0400766 while ($i++ < 10) {
767 $resp = $term->readline($prompt);
768 if (!defined $resp) { # EOF
769 print "\n";
770 return defined $default ? $default : undef;
771 }
772 if ($resp eq '' and defined $default) {
773 return $default;
774 }
Jay Soffian0da43a62009-04-04 23:23:21 -0400775 if (!defined $valid_re or $resp =~ /$valid_re/) {
Jay Soffian6e182512009-03-28 21:39:10 -0400776 return $resp;
777 }
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700778 if ($confirm_only) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100779 my $yesno = $term->readline(
780 # TRANSLATORS: please keep [y/N] as is.
781 sprintf(__("Are you sure you want to use <%s> [y/N]? "), $resp));
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700782 if (defined $yesno && $yesno =~ /y/i) {
783 return $resp;
784 }
785 }
Jay Soffian6e182512009-03-28 21:39:10 -0400786 }
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -0700787 return;
Jay Soffian6e182512009-03-28 21:39:10 -0400788}
789
Thomas Rast3cae7e52010-06-17 22:10:39 +0200790my %broken_encoding;
791
Ævar Arnfjörð Bjarmason1d50bfd2010-09-30 13:42:58 +0000792sub file_declares_8bit_cte {
Thomas Rast3cae7e52010-06-17 22:10:39 +0200793 my $fn = shift;
794 open (my $fh, '<', $fn);
795 while (my $line = <$fh>) {
796 last if ($line =~ /^$/);
797 return 1 if ($line =~ /^Content-Transfer-Encoding: .*8bit.*$/);
798 }
799 close $fh;
800 return 0;
801}
802
803foreach my $f (@files) {
804 next unless (body_or_subject_has_nonascii($f)
805 && !file_declares_8bit_cte($f));
806 $broken_encoding{$f} = 1;
807}
808
809if (!defined $auto_8bit_encoding && scalar %broken_encoding) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100810 print __("The following files are 8bit, but do not declare " .
811 "a Content-Transfer-Encoding.\n");
Thomas Rast3cae7e52010-06-17 22:10:39 +0200812 foreach my $f (sort keys %broken_encoding) {
813 print " $f\n";
814 }
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100815 $auto_8bit_encoding = ask(__("Which 8bit encoding should I declare [UTF-8]? "),
Junio C Hamano852a15d2015-02-13 12:20:25 -0800816 valid_re => qr/.{4}/, confirm_only => 1,
Thomas Rast3cae7e52010-06-17 22:10:39 +0200817 default => "UTF-8");
818}
819
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200820if (!$force) {
821 for my $f (@files) {
Ævar Arnfjörð Bjarmason0d290a42010-09-30 13:43:01 +0000822 if (get_patch_subject($f) =~ /\Q*** SUBJECT HERE ***\E/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100823 die sprintf(__("Refusing to send because the patch\n\t%s\n"
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200824 . "has the template subject '*** SUBJECT HERE ***'. "
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100825 . "Pass --force if you really want to send.\n"), $f);
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200826 }
827 }
828}
829
Remi Lespinetc46e27a2015-06-30 14:16:47 +0200830if (defined $sender) {
Remi Lespinetfa5b1aa2015-06-30 14:16:51 +0200831 $sender =~ s/^\s+|\s+$//g;
Remi Lespinetc46e27a2015-06-30 14:16:47 +0200832 ($sender) = expand_aliases($sender);
833} else {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100834 $sender = $repoauthor || $repocommitter || '';
Ryan Anderson83b24432005-07-31 04:17:25 -0400835}
836
Michael S. Tsirkinda187592013-06-05 21:11:00 +0300837# $sender could be an already sanitized address
838# (e.g. sendemail.from could be manually sanitized by user).
839# But it's a no-op to run sanitize_address on an already sanitized address.
840$sender = sanitize_address($sender);
841
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100842my $to_whom = __("To whom should the emails be sent (if anyone)?");
Felipe Contreras8cac13d2012-11-24 12:16:19 +0100843my $prompting = 0;
Junio C Hamano8796ff72010-10-26 22:02:03 -0700844if (!@initial_to && !defined $to_cmd) {
Junio C Hamano0d6b21e2016-04-24 12:31:44 -0700845 my $to = ask("$to_whom ",
Stephen Boyd61837492012-09-06 11:31:11 -0700846 default => "",
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700847 valid_re => qr/\@.*\./, confirm_only => 1);
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700848 push @initial_to, parse_address_line($to) if defined $to; # sanitized/validated later
Ryan Anderson1f038a02005-09-05 01:13:07 -0400849 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -0400850}
851
Eric Wong994d6c62006-05-14 19:13:44 -0700852sub expand_aliases {
Jeff King302e04e2009-07-23 07:09:29 -0400853 return map { expand_one_alias($_) } @_;
854}
855
856my %EXPANDED_ALIASES;
857sub expand_one_alias {
858 my $alias = shift;
859 if ($EXPANDED_ALIASES{$alias}) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100860 die sprintf(__("fatal: alias '%s' expands to itself\n"), $alias);
Jeff King302e04e2009-07-23 07:09:29 -0400861 }
862 local $EXPANDED_ALIASES{$alias} = 1;
863 return $aliases{$alias} ? expand_aliases(@{$aliases{$alias}}) : $alias;
Eric Wong994d6c62006-05-14 19:13:44 -0700864}
865
Remi Lespinetb5e112d2015-06-30 14:16:45 +0200866@initial_to = process_address_list(@initial_to);
867@initial_cc = process_address_list(@initial_cc);
868@bcclist = process_address_list(@bcclist);
Eric Wong994d6c62006-05-14 19:13:44 -0700869
Adam Roben5483c712007-06-27 20:59:37 -0700870if ($thread && !defined $initial_reply_to && $prompting) {
Jay Soffian6e182512009-03-28 21:39:10 -0400871 $initial_reply_to = ask(
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100872 __("Message-ID to be used as In-Reply-To for the first email (if any)? "),
Stephen Boyd61837492012-09-06 11:31:11 -0700873 default => "",
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700874 valid_re => qr/\@.*\./, confirm_only => 1);
Ryan Anderson83b24432005-07-31 04:17:25 -0400875}
Jay Soffian1ca3d6e2008-02-20 00:55:07 -0500876if (defined $initial_reply_to) {
Jay Soffian0fb7fc72008-02-21 19:16:04 -0500877 $initial_reply_to =~ s/^\s*<?//;
878 $initial_reply_to =~ s/>?\s*$//;
879 $initial_reply_to = "<$initial_reply_to>" if $initial_reply_to ne '';
Junio C Hamanoace9c2a2007-12-10 21:44:42 -0800880}
Mike Hommeyace72082007-12-09 18:17:28 +0100881
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900882if (!defined $smtp_server) {
Eric Wongaca7ad72006-05-15 02:34:44 -0700883 foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) {
884 if (-x $_) {
885 $smtp_server = $_;
886 last;
887 }
888 }
889 $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
Ryan Anderson3342d852005-07-31 20:04:24 -0400890}
891
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500892if ($compose && $compose > 0) {
893 @files = ($compose_filename . ".final", @files);
Ryan Anderson1f038a02005-09-05 01:13:07 -0400894}
895
Ryan Anderson83b24432005-07-31 04:17:25 -0400896# Variables we set as part of the loop over files
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500897our ($message_id, %mail, $subject, $reply_to, $references, $message,
Jay Soffiandc1460a2009-03-31 12:22:12 -0400898 $needs_confirm, $message_num, $ask_default);
Ryan Anderson83b24432005-07-31 04:17:25 -0400899
Eric Wong567ffeb2006-03-25 16:47:12 -0800900sub extract_valid_address {
901 my $address = shift;
Ævar Arnfjörð Bjarmason35b6ab92010-09-30 19:03:31 +0000902 my $local_part_regexp = qr/[^<>"\s@]+/;
903 my $domain_regexp = qr/[^.<>"\s@]+(?:\.[^.<>"\s@]+)+/;
Eric Wongdb3106b2006-05-15 02:41:01 -0700904
905 # check for a local address:
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700906 return $address if ($address =~ /^($local_part_regexp)$/);
Eric Wongdb3106b2006-05-15 02:41:01 -0700907
Uwe Kleine-König155197e2007-08-09 15:27:57 +0200908 $address =~ s/^\s*<(.*)>\s*$/$1/;
Eric Wong567ffeb2006-03-25 16:47:12 -0800909 if ($have_email_valid) {
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700910 return scalar Email::Valid->address($address);
Eric Wong567ffeb2006-03-25 16:47:12 -0800911 }
Krzysztof Mazur95c0d4b2012-11-22 19:12:09 +0100912
913 # less robust/correct than the monster regexp in Email::Valid,
914 # but still does a 99% job, and one less dependency
915 return $1 if $address =~ /($local_part_regexp\@$domain_regexp)/;
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -0700916 return;
Eric Wong567ffeb2006-03-25 16:47:12 -0800917}
Ryan Anderson83b24432005-07-31 04:17:25 -0400918
Krzysztof Mazure4312252012-11-22 19:12:10 +0100919sub extract_valid_address_or_die {
920 my $address = shift;
921 $address = extract_valid_address($address);
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100922 die sprintf(__("error: unable to extract a valid address from: %s\n"), $address)
Krzysztof Mazure4312252012-11-22 19:12:10 +0100923 if !$address;
924 return $address;
925}
926
927sub validate_address {
928 my $address = shift;
Krzysztof Mazurd0e98102012-11-22 19:12:12 +0100929 while (!extract_valid_address($address)) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100930 printf STDERR __("error: unable to extract a valid address from: %s\n"), $address;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100931 # TRANSLATORS: Make sure to include [q] [d] [e] in your
932 # translation. The program will only accept English input
933 # at this point.
934 $_ = ask(__("What to do with this address? ([q]uit|[d]rop|[e]dit): "),
Krzysztof Mazurd0e98102012-11-22 19:12:12 +0100935 valid_re => qr/^(?:quit|q|drop|d|edit|e)/i,
Krzysztof Mazur5c80afe2012-11-22 19:12:11 +0100936 default => 'q');
937 if (/^d/i) {
938 return undef;
939 } elsif (/^q/i) {
940 cleanup_compose_files();
941 exit(0);
942 }
Junio C Hamano0d6b21e2016-04-24 12:31:44 -0700943 $address = ask("$to_whom ",
Krzysztof Mazurd0e98102012-11-22 19:12:12 +0100944 default => "",
945 valid_re => qr/\@.*\./, confirm_only => 1);
Krzysztof Mazure4312252012-11-22 19:12:10 +0100946 }
947 return $address;
948}
949
950sub validate_address_list {
951 return (grep { defined $_ }
952 map { validate_address($_) } @_);
Ryan Anderson83b24432005-07-31 04:17:25 -0400953}
954
955# Usually don't need to change anything below here.
956
957# we make a "fake" message id by taking the current number
958# of seconds since the beginning of Unix time and tacking on
959# a random number to the end, in case we are called quicker than
960# 1 second since the last time we were called.
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400961
962# We'll setup a template for the message id, using the "from" address:
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400963
Junio C Hamanobe510cf2007-09-17 21:18:20 -0700964my ($message_id_stamp, $message_id_serial);
Brian Gernhardt68ce9332010-04-10 10:53:53 -0400965sub make_message_id {
Junio C Hamanobe510cf2007-09-17 21:18:20 -0700966 my $uniq;
967 if (!defined $message_id_stamp) {
Eric Wongf916ab02016-04-06 20:07:14 +0000968 $message_id_stamp = strftime("%Y%m%d%H%M%S.$$", gmtime(time));
Junio C Hamanobe510cf2007-09-17 21:18:20 -0700969 $message_id_serial = 0;
970 }
971 $message_id_serial++;
972 $uniq = "$message_id_stamp-$message_id_serial";
973
Junio C Hamanoaeb59322007-06-20 13:47:34 -0700974 my $du_part;
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200975 for ($sender, $repocommitter, $repoauthor) {
976 $du_part = extract_valid_address(sanitize_address($_));
977 last if (defined $du_part and $du_part ne '');
Junio C Hamanoaeb59322007-06-20 13:47:34 -0700978 }
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200979 if (not defined $du_part or $du_part eq '') {
Ævar Arnfjörð Bjarmason529dd382010-09-30 13:43:08 +0000980 require Sys::Hostname;
Junio C Hamanoaeb59322007-06-20 13:47:34 -0700981 $du_part = 'user@' . Sys::Hostname::hostname();
982 }
Eric Wongf916ab02016-04-06 20:07:14 +0000983 my $message_id_template = "<%s-%s>";
Junio C Hamanobe510cf2007-09-17 21:18:20 -0700984 $message_id = sprintf($message_id_template, $uniq, $du_part);
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400985 #print "new message id = $message_id\n"; # Was useful for debugging
Ryan Anderson83b24432005-07-31 04:17:25 -0400986}
987
988
989
Eric Wonga5370b12006-03-25 03:01:01 -0800990$time = time - scalar $#files;
Ryan Anderson83b24432005-07-31 04:17:25 -0400991
Jürgen Rühle374c5902007-01-10 13:36:39 -0800992sub unquote_rfc2047 {
993 local ($_) = @_;
Роман Донченко11f70a72014-12-14 18:59:46 +0300994 my $charset;
Роман Донченкоab47e2a2014-12-14 18:59:47 +0300995 my $sep = qr/[ \t]+/;
996 s{$re_encoded_word(?:$sep$re_encoded_word)*}{
997 my @words = split $sep, $&;
998 foreach (@words) {
999 m/$re_encoded_word/;
1000 $charset = $1;
1001 my $encoding = $2;
1002 my $text = $3;
1003 if ($encoding eq 'q' || $encoding eq 'Q') {
1004 $_ = $text;
1005 s/_/ /g;
1006 s/=([0-9A-F]{2})/chr(hex($1))/egi;
1007 } else {
1008 # other encodings not supported yet
1009 }
Роман Донченко11f70a72014-12-14 18:59:46 +03001010 }
Роман Донченкоab47e2a2014-12-14 18:59:47 +03001011 join '', @words;
Thomas Rastb622d4d2012-07-30 21:25:40 +02001012 }eg;
Роман Донченко11f70a72014-12-14 18:59:46 +03001013 return wantarray ? ($_, $charset) : $_;
Jürgen Rühle374c5902007-01-10 13:36:39 -08001014}
1015
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001016sub quote_rfc2047 {
1017 local $_ = shift;
Brandon Caseyd1fff6f2009-06-06 20:12:31 -05001018 my $encoding = shift || 'UTF-8';
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001019 s/([^-a-zA-Z0-9!*+\/])/sprintf("=%02X", ord($1))/eg;
1020 s/(.*)/=\?$encoding\?q\?$1\?=/;
1021 return $_;
1022}
1023
Brandon Caseya3a82622009-06-07 19:25:58 -05001024sub is_rfc2047_quoted {
1025 my $s = shift;
Brandon Caseya3a82622009-06-07 19:25:58 -05001026 length($s) <= 75 &&
Роман Донченко11f70a72014-12-14 18:59:46 +03001027 $s =~ m/^(?:"[[:ascii:]]*"|$re_encoded_word)$/o;
Brandon Caseya3a82622009-06-07 19:25:58 -05001028}
1029
Krzysztof Mazurce547802012-10-24 23:08:26 +02001030sub subject_needs_rfc2047_quoting {
1031 my $s = shift;
1032
Krzysztof Mazurce1459f2012-10-24 23:28:29 +02001033 return ($s =~ /[^[:ascii:]]/) || ($s =~ /=\?/);
Krzysztof Mazurce547802012-10-24 23:08:26 +02001034}
1035
1036sub quote_subject {
1037 local $subject = shift;
1038 my $encoding = shift || 'UTF-8';
1039
1040 if (subject_needs_rfc2047_quoting($subject)) {
1041 return quote_rfc2047($subject, $encoding);
1042 }
1043 return $subject;
1044}
1045
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001046# use the simplest quoting being able to handle the recipient
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001047sub sanitize_address {
Robin H. Johnson732263d2007-04-25 19:37:19 -07001048 my ($recipient) = @_;
Krzysztof Mazur831a4882012-11-22 19:12:08 +01001049
1050 # remove garbage after email address
1051 $recipient =~ s/(.*>).*$/$1/;
1052
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001053 my ($recipient_name, $recipient_addr) = ($recipient =~ /^(.*?)\s*(<.*)/);
1054
1055 if (not $recipient_name) {
Ævar Arnfjörð Bjarmasonff483892010-09-30 13:43:02 +00001056 return $recipient;
Robin H. Johnson732263d2007-04-25 19:37:19 -07001057 }
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001058
1059 # if recipient_name is already quoted, do nothing
Brandon Caseya3a82622009-06-07 19:25:58 -05001060 if (is_rfc2047_quoted($recipient_name)) {
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001061 return $recipient;
1062 }
1063
Remi Lespinet1fe97032015-06-30 14:16:49 +02001064 # remove non-escaped quotes
1065 $recipient_name =~ s/(^|[^\\])"/$1/g;
1066
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001067 # rfc2047 is needed if a non-ascii char is included
1068 if ($recipient_name =~ /[^[:ascii:]]/) {
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001069 $recipient_name = quote_rfc2047($recipient_name);
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001070 }
1071
1072 # double quotes are needed if specials or CTLs are included
1073 elsif ($recipient_name =~ /[][()<>@,;:\\".\000-\037\177]/) {
Remi Lespinet1fe97032015-06-30 14:16:49 +02001074 $recipient_name =~ s/([\\\r])/\\$1/g;
Ævar Arnfjörð Bjarmasond5c7d692010-09-30 13:43:03 +00001075 $recipient_name = qq["$recipient_name"];
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001076 }
1077
1078 return "$recipient_name $recipient_addr";
1079
Robin H. Johnson732263d2007-04-25 19:37:19 -07001080}
1081
Krzysztof Mazure4312252012-11-22 19:12:10 +01001082sub sanitize_address_list {
1083 return (map { sanitize_address($_) } @_);
1084}
1085
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001086sub process_address_list {
Remi Lespinetb1c8a112015-06-30 14:16:50 +02001087 my @addr_list = map { parse_address_line($_) } @_;
1088 @addr_list = expand_aliases(@addr_list);
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001089 @addr_list = sanitize_address_list(@addr_list);
1090 @addr_list = validate_address_list(@addr_list);
1091 return @addr_list;
1092}
1093
Jari Aalto134550f2010-03-14 17:16:45 +02001094# Returns the local Fully Qualified Domain Name (FQDN) if available.
1095#
1096# Tightly configured MTAa require that a caller sends a real DNS
1097# domain name that corresponds the IP address in the HELO/EHLO
1098# handshake. This is used to verify the connection and prevent
1099# spammers from trying to hide their identity. If the DNS and IP don't
1100# match, the receiveing MTA may deny the connection.
1101#
1102# Here is a deny example of Net::SMTP with the default "localhost.localdomain"
1103#
1104# Net::SMTP=GLOB(0x267ec28)>>> EHLO localhost.localdomain
1105# Net::SMTP=GLOB(0x267ec28)<<< 550 EHLO argument does not match calling host
1106#
1107# This maildomain*() code is based on ideas in Perl library Test::Reporter
1108# /usr/share/perl5/Test/Reporter/Mail/Util.pm ==> sub _maildomain ()
1109
Brian Gernhardt59a86302010-04-10 10:53:54 -04001110sub valid_fqdn {
1111 my $domain = shift;
Brandon Casey61ef5e92010-09-26 22:18:01 -05001112 return defined $domain && !($^O eq 'darwin' && $domain =~ /\.local$/) && $domain =~ /\./;
Brian Gernhardt59a86302010-04-10 10:53:54 -04001113}
1114
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001115sub maildomain_net {
Jari Aalto134550f2010-03-14 17:16:45 +02001116 my $maildomain;
1117
1118 if (eval { require Net::Domain; 1 }) {
1119 my $domain = Net::Domain::domainname();
Brian Gernhardt59a86302010-04-10 10:53:54 -04001120 $maildomain = $domain if valid_fqdn($domain);
Jari Aalto134550f2010-03-14 17:16:45 +02001121 }
1122
1123 return $maildomain;
1124}
1125
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001126sub maildomain_mta {
Jari Aalto134550f2010-03-14 17:16:45 +02001127 my $maildomain;
1128
1129 if (eval { require Net::SMTP; 1 }) {
1130 for my $host (qw(mailhost localhost)) {
1131 my $smtp = Net::SMTP->new($host);
1132 if (defined $smtp) {
1133 my $domain = $smtp->domain;
1134 $smtp->quit;
1135
Brian Gernhardt59a86302010-04-10 10:53:54 -04001136 $maildomain = $domain if valid_fqdn($domain);
Jari Aalto134550f2010-03-14 17:16:45 +02001137
1138 last if $maildomain;
1139 }
1140 }
1141 }
1142
1143 return $maildomain;
1144}
1145
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001146sub maildomain {
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001147 return maildomain_net() || maildomain_mta() || 'localhost.localdomain';
Jari Aalto134550f2010-03-14 17:16:45 +02001148}
1149
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001150sub smtp_host_string {
1151 if (defined $smtp_server_port) {
1152 return "$smtp_server:$smtp_server_port";
1153 } else {
1154 return $smtp_server;
1155 }
1156}
1157
1158# Returns 1 if authentication succeeded or was not necessary
1159# (smtp_user was not specified), and 0 otherwise.
1160
1161sub smtp_auth_maybe {
1162 if (!defined $smtp_authuser || $auth) {
1163 return 1;
1164 }
1165
1166 # Workaround AUTH PLAIN/LOGIN interaction defect
1167 # with Authen::SASL::Cyrus
1168 eval {
1169 require Authen::SASL;
1170 Authen::SASL->import(qw(Perl));
1171 };
1172
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001173 # Check mechanism naming as defined in:
1174 # https://tools.ietf.org/html/rfc4422#page-8
Brian Norris904f6e72015-09-18 15:12:50 -07001175 if ($smtp_auth && $smtp_auth !~ /^(\b[A-Z0-9-_]{1,20}\s*)*$/) {
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001176 die "invalid smtp auth: '${smtp_auth}'";
1177 }
1178
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001179 # TODO: Authentication may fail not because credentials were
1180 # invalid but due to other reasons, in which we should not
1181 # reject credentials.
1182 $auth = Git::credential({
1183 'protocol' => 'smtp',
1184 'host' => smtp_host_string(),
1185 'username' => $smtp_authuser,
1186 # if there's no password, "git credential fill" will
1187 # give us one, otherwise it'll just pass this one.
1188 'password' => $smtp_authpass
1189 }, sub {
1190 my $cred = shift;
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001191
1192 if ($smtp_auth) {
1193 my $sasl = Authen::SASL->new(
1194 mechanism => $smtp_auth,
1195 callback => {
1196 user => $cred->{'username'},
1197 pass => $cred->{'password'},
1198 authname => $cred->{'username'},
1199 }
1200 );
1201
1202 return !!$smtp->auth($sasl);
1203 }
1204
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001205 return !!$smtp->auth($cred->{'username'}, $cred->{'password'});
1206 });
1207
1208 return $auth;
1209}
1210
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001211sub ssl_verify_params {
1212 eval {
1213 require IO::Socket::SSL;
1214 IO::Socket::SSL->import(qw/SSL_VERIFY_PEER SSL_VERIFY_NONE/);
1215 };
1216 if ($@) {
1217 print STDERR "Not using SSL_VERIFY_PEER due to out-of-date IO::Socket::SSL.\n";
1218 return;
1219 }
1220
1221 if (!defined $smtp_ssl_cert_path) {
Ruben Kerkhof01645b72014-01-15 21:31:11 +04001222 # use the OpenSSL defaults
1223 return (SSL_verify_mode => SSL_VERIFY_PEER());
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001224 }
1225
1226 if ($smtp_ssl_cert_path eq "") {
1227 return (SSL_verify_mode => SSL_VERIFY_NONE());
1228 } elsif (-d $smtp_ssl_cert_path) {
1229 return (SSL_verify_mode => SSL_VERIFY_PEER(),
1230 SSL_ca_path => $smtp_ssl_cert_path);
1231 } elsif (-f $smtp_ssl_cert_path) {
1232 return (SSL_verify_mode => SSL_VERIFY_PEER(),
1233 SSL_ca_file => $smtp_ssl_cert_path);
1234 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001235 die sprintf(__("CA path \"%s\" does not exist"), $smtp_ssl_cert_path);
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001236 }
1237}
1238
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001239sub file_name_is_absolute {
1240 my ($path) = @_;
1241
1242 # msys does not grok DOS drive-prefixes
1243 if ($^O eq 'msys') {
Junio C Hamanof24ecf52014-04-23 09:37:38 -07001244 return ($path =~ m#^/# || $path =~ m#^[a-zA-Z]\:#)
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001245 }
1246
1247 require File::Spec::Functions;
1248 return File::Spec::Functions::file_name_is_absolute($path);
1249}
1250
Michael Witten15da1082009-04-13 13:23:51 -05001251# Returns 1 if the message was sent, and 0 otherwise.
Markus Heidelberga1b5b372009-06-12 12:51:42 +02001252# In actuality, the whole program dies when there
Michael Witten15da1082009-04-13 13:23:51 -05001253# is an error sending a message.
1254
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001255sub send_message {
Eric Wong4bc87a22006-03-25 17:20:48 -08001256 my @recipients = unique_email_list(@to);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001257 @cc = (grep { my $cc = extract_valid_address_or_die($_);
Joe Perches83acaae2010-11-20 15:06:05 -08001258 not grep { $cc eq $_ || $_ =~ /<\Q${cc}\E>$/ } @recipients
Ask Bjørn Hansen7ac17522007-11-19 03:00:26 -08001259 }
Ask Bjørn Hansen7ac17522007-11-19 03:00:26 -08001260 @cc);
Eric Wong4bc87a22006-03-25 17:20:48 -08001261 my $to = join (",\n\t", @recipients);
Ryan Anderson58063242006-05-29 12:30:13 -07001262 @recipients = unique_email_list(@recipients,@cc,@bcclist);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001263 @recipients = (map { extract_valid_address_or_die($_) } @recipients);
Jakub Narebski6bdca892006-07-07 20:57:55 +02001264 my $date = format_2822_time($time++);
Martin Langhoffe923eff2006-05-03 09:44:36 +12001265 my $gitversion = '@@GIT_VERSION@@';
1266 if ($gitversion =~ m/..GIT_VERSION../) {
Petr Baudis3cb8caf2006-07-03 22:47:58 +02001267 $gitversion = Git::version();
Martin Langhoffe923eff2006-05-03 09:44:36 +12001268 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001269
Joe Perches02461e02009-10-08 10:03:26 -07001270 my $cc = join(",\n\t", unique_email_list(@cc));
Junio C Hamanof06a6a42007-04-16 16:51:47 -07001271 my $ccline = "";
1272 if ($cc ne '') {
1273 $ccline = "\nCc: $cc";
1274 }
Jeff King4f3d3702007-12-17 15:51:34 -05001275 make_message_id() unless defined($message_id);
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001276
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001277 my $header = "From: $sender
Junio C Hamanof06a6a42007-04-16 16:51:47 -07001278To: $to${ccline}
Eric Wong4bc87a22006-03-25 17:20:48 -08001279Subject: $subject
Eric Wong4bc87a22006-03-25 17:20:48 -08001280Date: $date
1281Message-Id: $message_id
Eric Wong4bc87a22006-03-25 17:20:48 -08001282";
Luis Henriquesac1596a2014-03-24 21:38:27 +00001283 if ($use_xmailer) {
1284 $header .= "X-Mailer: git-send-email $gitversion\n";
1285 }
Thomas Rast3e0c4ff2009-03-01 23:45:41 +01001286 if ($reply_to) {
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001287
1288 $header .= "In-Reply-To: $reply_to\n";
1289 $header .= "References: $references\n";
1290 }
Junio C Hamanoce91c2f2006-10-05 16:36:49 -07001291 if (@xh) {
1292 $header .= join("\n", @xh) . "\n";
1293 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001294
Robin H. Johnsonc38f0242007-04-25 19:37:20 -07001295 my @sendmail_parameters = ('-i', @recipients);
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001296 my $raw_from = $sender;
Felipe Contrerasc89e3242009-11-26 21:04:29 +02001297 if (defined $envelope_sender && $envelope_sender ne "auto") {
1298 $raw_from = $envelope_sender;
1299 }
Robin H. Johnsonf073a592007-04-25 19:37:22 -07001300 $raw_from = extract_valid_address($raw_from);
1301 unshift (@sendmail_parameters,
1302 '-f', $raw_from) if(defined $envelope_sender);
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001303
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001304 if ($needs_confirm && !$dry_run) {
1305 print "\n$header\n";
1306 if ($needs_confirm eq "inform") {
1307 $confirm_unconfigured = 0; # squelch this message for the rest of this run
Jay Soffian6e182512009-03-28 21:39:10 -04001308 $ask_default = "y"; # assume yes on EOF since user hasn't explicitly asked for confirmation
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001309 print __ <<EOF ;
1310 The Cc list above has been expanded by additional
1311 addresses found in the patch commit message. By default
1312 send-email prompts before sending whenever this occurs.
1313 This behavior is controlled by the sendemail.confirm
1314 configuration setting.
1315
1316 For additional information, run 'git send-email --help'.
1317 To retain the current behavior, but squelch this message,
1318 run 'git config --global sendemail.confirm auto'.
1319
1320EOF
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001321 }
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001322 # TRANSLATORS: Make sure to include [y] [n] [q] [a] in your
1323 # translation. The program will only accept English input
1324 # at this point.
1325 $_ = ask(__("Send this email? ([y]es|[n]o|[q]uit|[a]ll): "),
Jay Soffian6e182512009-03-28 21:39:10 -04001326 valid_re => qr/^(?:yes|y|no|n|quit|q|all|a)/i,
1327 default => $ask_default);
Vasco Almeida46493102016-12-14 11:54:36 -01001328 die __("Send this email reply required") unless defined $_;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001329 if (/^n/i) {
Michael Witten15da1082009-04-13 13:23:51 -05001330 return 0;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001331 } elsif (/^q/i) {
1332 cleanup_compose_files();
1333 exit(0);
1334 } elsif (/^a/i) {
1335 $confirm = 'never';
1336 }
1337 }
1338
Pascal Obry052fbea2010-09-06 20:12:11 +02001339 unshift (@sendmail_parameters, @smtp_server_options);
1340
Matthew Wilcox61302592006-10-10 08:58:23 -06001341 if ($dry_run) {
1342 # We don't want to send the email.
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001343 } elsif (file_name_is_absolute($smtp_server)) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001344 my $pid = open my $sm, '|-';
1345 defined $pid or die $!;
1346 if (!$pid) {
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001347 exec($smtp_server, @sendmail_parameters) or die $!;
Eric Wongaca7ad72006-05-15 02:34:44 -07001348 }
1349 print $sm "$header\n$message";
Ævar Arnfjörð Bjarmason5e2c2ab2010-09-30 13:43:07 +00001350 close $sm or die $!;
Eric Wongaca7ad72006-05-15 02:34:44 -07001351 } else {
Junio C Hamano44b24762007-09-25 17:27:54 -07001352
1353 if (!defined $smtp_server) {
Vasco Almeida46493102016-12-14 11:54:36 -01001354 die __("The required SMTP server is not properly defined.")
Junio C Hamano44b24762007-09-25 17:27:54 -07001355 }
1356
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001357 require Net::SMTP;
1358 my $use_net_smtp_ssl = version->parse($Net::SMTP::VERSION) < version->parse("1.28");
1359 $smtp_domain ||= maildomain();
1360
Thomas Rastf6bebd12008-06-25 21:42:43 +02001361 if ($smtp_encryption eq 'ssl') {
Junio C Hamano44b24762007-09-25 17:27:54 -07001362 $smtp_server_port ||= 465; # ssmtp
Thomas Rast5508f3e2013-12-01 23:48:43 +01001363 require IO::Socket::SSL;
John Keeping9d605242015-12-03 21:47:18 +00001364
1365 # Suppress "variable accessed once" warning.
1366 {
1367 no warnings 'once';
1368 $IO::Socket::SSL::DEBUG = 1;
1369 }
1370
Thomas Rast5508f3e2013-12-01 23:48:43 +01001371 # Net::SMTP::SSL->new() does not forward any SSL options
1372 IO::Socket::SSL::set_client_defaults(
1373 ssl_verify_params());
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001374
1375 if ($use_net_smtp_ssl) {
1376 require Net::SMTP::SSL;
1377 $smtp ||= Net::SMTP::SSL->new($smtp_server,
1378 Hello => $smtp_domain,
1379 Port => $smtp_server_port,
1380 Debug => $debug_net_smtp);
1381 }
1382 else {
1383 $smtp ||= Net::SMTP->new($smtp_server,
1384 Hello => $smtp_domain,
1385 Port => $smtp_server_port,
1386 Debug => $debug_net_smtp,
1387 SSL => 1);
1388 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001389 }
1390 else {
brian m. carlson1a741bf2013-07-04 22:04:52 +00001391 $smtp_server_port ||= 25;
1392 $smtp ||= Net::SMTP->new($smtp_server,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001393 Hello => $smtp_domain,
brian m. carlson1a741bf2013-07-04 22:04:52 +00001394 Debug => $debug_net_smtp,
1395 Port => $smtp_server_port);
Yakov Lernerfb3650e2009-09-25 15:10:21 -07001396 if ($smtp_encryption eq 'tls' && $smtp) {
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001397 if ($use_net_smtp_ssl) {
1398 $smtp->command('STARTTLS');
1399 $smtp->response();
1400 if ($smtp->code != 220) {
1401 die sprintf(__("Server does not support STARTTLS! %s"), $smtp->message);
1402 }
1403 require Net::SMTP::SSL;
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001404 $smtp = Net::SMTP::SSL->start_SSL($smtp,
1405 ssl_verify_params())
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001406 or die sprintf(__("STARTTLS failed! %s"), IO::Socket::SSL::errstr());
Thomas Rastf6bebd12008-06-25 21:42:43 +02001407 }
Dennis Kaarsemaker0ead0002017-03-24 22:37:32 +01001408 else {
1409 $smtp->starttls(ssl_verify_params())
1410 or die sprintf(__("STARTTLS failed! %s"), IO::Socket::SSL::errstr());
1411 }
1412 $smtp_encryption = '';
1413 # Send EHLO again to receive fresh
1414 # supported commands
1415 $smtp->hello($smtp_domain);
Thomas Rastf6bebd12008-06-25 21:42:43 +02001416 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001417 }
Junio C Hamano44b24762007-09-25 17:27:54 -07001418
1419 if (!$smtp) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001420 die __("Unable to initialize SMTP properly. Check config and use --smtp-debug."),
1421 " VALUES: server=$smtp_server ",
Jari Aaltoe5afb3a2010-03-14 17:15:33 +02001422 "encryption=$smtp_encryption ",
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001423 "hello=$smtp_domain",
Sylvain Rabota1dd7e12011-04-29 20:23:24 +02001424 defined $smtp_server_port ? " port=$smtp_server_port" : "";
Junio C Hamano44b24762007-09-25 17:27:54 -07001425 }
1426
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001427 smtp_auth_maybe or die $smtp->message;
Michael Witten2363d742008-02-03 19:53:56 -05001428
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001429 $smtp->mail( $raw_from ) or die $smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001430 $smtp->to( @recipients ) or die $smtp->message;
1431 $smtp->data or die $smtp->message;
Stefan Agnerf60c4832015-09-30 09:26:09 +02001432 $smtp->datasend("$header\n") or die $smtp->message;
1433 my @lines = split /^/, $message;
1434 foreach my $line (@lines) {
1435 $smtp->datasend("$line") or die $smtp->message;
1436 }
Eric Wongaca7ad72006-05-15 02:34:44 -07001437 $smtp->dataend() or die $smtp->message;
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001438 $smtp->code =~ /250|200/ or die sprintf(__("Failed to send %s\n"), $subject).$smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001439 }
Ryan Anderson27184352006-02-05 20:13:52 -05001440 if ($quiet) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001441 printf($dry_run ? __("Dry-Sent %s\n") : __("Sent %s\n"), $subject);
Ryan Anderson27184352006-02-05 20:13:52 -05001442 } else {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001443 print($dry_run ? __("Dry-OK. Log says:\n") : __("OK. Log says:\n"));
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001444 if (!file_name_is_absolute($smtp_server)) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001445 print "Server: $smtp_server\n";
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001446 print "MAIL FROM:<$raw_from>\n";
Joe Perches02461e02009-10-08 10:03:26 -07001447 foreach my $entry (@recipients) {
1448 print "RCPT TO:<$entry>\n";
1449 }
Eric Wongaca7ad72006-05-15 02:34:44 -07001450 } else {
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001451 print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001452 }
David D. Kilzerb7f30e02007-11-18 20:14:55 -08001453 print $header, "\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001454 if ($smtp) {
Vasco Almeida46493102016-12-14 11:54:36 -01001455 print __("Result: "), $smtp->code, ' ',
Eric Wongaca7ad72006-05-15 02:34:44 -07001456 ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
1457 } else {
Vasco Almeida46493102016-12-14 11:54:36 -01001458 print __("Result: OK\n");
Eric Wongaca7ad72006-05-15 02:34:44 -07001459 }
Ryan Anderson30d08b32006-02-02 11:56:06 -05001460 }
Michael Witten15da1082009-04-13 13:23:51 -05001461
1462 return 1;
Ryan Anderson83b24432005-07-31 04:17:25 -04001463}
1464
Ryan Anderson83b24432005-07-31 04:17:25 -04001465$reply_to = $initial_reply_to;
Junio C Hamano2186d562006-05-29 23:53:13 -07001466$references = $initial_reply_to || '';
Ryan Anderson83b24432005-07-31 04:17:25 -04001467$subject = $initial_subject;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001468$message_num = 0;
Ryan Anderson83b24432005-07-31 04:17:25 -04001469
1470foreach my $t (@files) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001471 open my $fh, "<", $t or die sprintf(__("can't open file %s"), $t);
Ryan Anderson83b24432005-07-31 04:17:25 -04001472
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001473 my $author = undef;
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001474 my $sauthor = undef;
Jeff King8291db62007-11-16 05:49:09 -05001475 my $author_encoding;
1476 my $has_content_type;
1477 my $body_encoding;
Paolo Bonzinibb294562014-11-25 15:00:26 +01001478 my $xfer_encoding;
1479 my $has_mime_version;
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001480 @to = ();
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001481 @cc = ();
Junio C Hamanoce91c2f2006-10-05 16:36:49 -07001482 @xh = ();
Junio C Hamanoe6b09642006-10-07 03:09:05 -07001483 my $input_format = undef;
Jay Soffian50126992009-02-14 23:32:14 -05001484 my @header = ();
Ryan Anderson83b24432005-07-31 04:17:25 -04001485 $message = "";
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001486 $message_num++;
Jay Soffian50126992009-02-14 23:32:14 -05001487 # First unfold multiline header fields
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001488 while(<$fh>) {
Jay Soffian50126992009-02-14 23:32:14 -05001489 last if /^\s*$/;
1490 if (/^\s+\S/ and @header) {
1491 chomp($header[$#header]);
1492 s/^\s+/ /;
1493 $header[$#header] .= $_;
1494 } else {
1495 push(@header, $_);
1496 }
1497 }
1498 # Now parse the header
1499 foreach(@header) {
1500 if (/^From /) {
1501 $input_format = 'mbox';
1502 next;
1503 }
1504 chomp;
1505 if (!defined $input_format && /^[-A-Za-z]+:\s/) {
1506 $input_format = 'mbox';
1507 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001508
Jay Soffian50126992009-02-14 23:32:14 -05001509 if (defined $input_format && $input_format eq 'mbox') {
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001510 if (/^Subject:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001511 $subject = $1;
1512 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001513 elsif (/^From:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001514 ($author, $author_encoding) = unquote_rfc2047($1);
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001515 $sauthor = sanitize_address($author);
Jay Soffian50126992009-02-14 23:32:14 -05001516 next if $suppress_cc{'author'};
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001517 next if $suppress_cc{'self'} and $sauthor eq $sender;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001518 printf(__("(mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001519 $1, $_) unless $quiet;
1520 push @cc, $1;
1521 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001522 elsif (/^To:\s+(.*)$/i) {
Stephen Boyd21802cd2010-09-29 00:26:44 -07001523 foreach my $addr (parse_address_line($1)) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001524 printf(__("(mbox) Adding to: %s from line '%s'\n"),
Stephen Boyd21802cd2010-09-29 00:26:44 -07001525 $addr, $_) unless $quiet;
Krzysztof Mazure4312252012-11-22 19:12:10 +01001526 push @to, $addr;
Stephen Boyd21802cd2010-09-29 00:26:44 -07001527 }
1528 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001529 elsif (/^Cc:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001530 foreach my $addr (parse_address_line($1)) {
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001531 my $qaddr = unquote_rfc2047($addr);
1532 my $saddr = sanitize_address($qaddr);
1533 if ($saddr eq $sender) {
David Brown65648282007-12-25 19:56:29 -08001534 next if ($suppress_cc{'self'});
David Brown65648282007-12-25 19:56:29 -08001535 } else {
1536 next if ($suppress_cc{'cc'});
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001537 }
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001538 printf(__("(mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001539 $addr, $_) unless $quiet;
1540 push @cc, $addr;
Ryan Anderson83b24432005-07-31 04:17:25 -04001541 }
1542 }
Jay Soffian50126992009-02-14 23:32:14 -05001543 elsif (/^Content-type:/i) {
1544 $has_content_type = 1;
1545 if (/charset="?([^ "]+)/) {
1546 $body_encoding = $1;
1547 }
1548 push @xh, $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001549 }
Paolo Bonzinibb294562014-11-25 15:00:26 +01001550 elsif (/^MIME-Version/i) {
1551 $has_mime_version = 1;
1552 push @xh, $_;
1553 }
Jay Soffian50126992009-02-14 23:32:14 -05001554 elsif (/^Message-Id: (.*)/i) {
1555 $message_id = $1;
1556 }
Paolo Bonzinibb294562014-11-25 15:00:26 +01001557 elsif (/^Content-Transfer-Encoding: (.*)/i) {
1558 $xfer_encoding = $1 if not defined $xfer_encoding;
1559 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001560 elsif (!/^Date:\s/i && /^[-A-Za-z]+:\s+\S/) {
Jay Soffian50126992009-02-14 23:32:14 -05001561 push @xh, $_;
1562 }
1563
Ryan Anderson83b24432005-07-31 04:17:25 -04001564 } else {
Jay Soffian50126992009-02-14 23:32:14 -05001565 # In the traditional
1566 # "send lots of email" format,
1567 # line 1 = cc
1568 # line 2 = subject
1569 # So let's support that, too.
1570 $input_format = 'lots';
1571 if (@cc == 0 && !$suppress_cc{'cc'}) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001572 printf(__("(non-mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001573 $_, $_) unless $quiet;
1574 push @cc, $_;
1575 } elsif (!defined $subject) {
1576 $subject = $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001577 }
1578 }
1579 }
Jay Soffian50126992009-02-14 23:32:14 -05001580 # Now parse the message body
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001581 while(<$fh>) {
Jay Soffian50126992009-02-14 23:32:14 -05001582 $message .= $_;
Johan Hovold9d334392017-02-20 12:44:06 +01001583 if (/^(Signed-off-by|Cc): ([^>]*>?)/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001584 chomp;
Jay Soffian3531e272009-02-14 23:32:15 -05001585 my ($what, $c) = ($1, $2);
Jay Soffian50126992009-02-14 23:32:14 -05001586 chomp $c;
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001587 my $sc = sanitize_address($c);
1588 if ($sc eq $sender) {
Jay Soffian3531e272009-02-14 23:32:15 -05001589 next if ($suppress_cc{'self'});
1590 } else {
1591 next if $suppress_cc{'sob'} and $what =~ /Signed-off-by/i;
1592 next if $suppress_cc{'bodycc'} and $what =~ /Cc/i;
1593 }
Jay Soffian50126992009-02-14 23:32:14 -05001594 push @cc, $c;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001595 printf(__("(body) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001596 $c, $_) unless $quiet;
1597 }
1598 }
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001599 close $fh;
Joe Perches324a8bd2007-08-17 18:51:12 -07001600
Joe Perches6e74e072010-09-24 10:03:00 -07001601 push @to, recipients_cmd("to-cmd", "to", $to_cmd, $t)
1602 if defined $to_cmd;
1603 push @cc, recipients_cmd("cc-cmd", "cc", $cc_cmd, $t)
1604 if defined $cc_cmd && !$suppress_cc{'cccmd'};
Joe Perches324a8bd2007-08-17 18:51:12 -07001605
Thomas Rast3cae7e52010-06-17 22:10:39 +02001606 if ($broken_encoding{$t} && !$has_content_type) {
Paolo Bonzinibb294562014-11-25 15:00:26 +01001607 $xfer_encoding = '8bit' if not defined $xfer_encoding;
Thomas Rast3cae7e52010-06-17 22:10:39 +02001608 $has_content_type = 1;
Paolo Bonzinibb294562014-11-25 15:00:26 +01001609 push @xh, "Content-Type: text/plain; charset=$auto_8bit_encoding";
Thomas Rast3cae7e52010-06-17 22:10:39 +02001610 $body_encoding = $auto_8bit_encoding;
1611 }
1612
Krzysztof Mazurce547802012-10-24 23:08:26 +02001613 if ($broken_encoding{$t} && !is_rfc2047_quoted($subject)) {
1614 $subject = quote_subject($subject, $auto_8bit_encoding);
Thomas Rast3cae7e52010-06-17 22:10:39 +02001615 }
1616
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001617 if (defined $sauthor and $sauthor ne $sender) {
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001618 $message = "From: $author\n\n$message";
Jeff King8291db62007-11-16 05:49:09 -05001619 if (defined $author_encoding) {
1620 if ($has_content_type) {
1621 if ($body_encoding eq $author_encoding) {
1622 # ok, we already have the right encoding
1623 }
1624 else {
1625 # uh oh, we should re-encode
1626 }
1627 }
1628 else {
Paolo Bonzinibb294562014-11-25 15:00:26 +01001629 $xfer_encoding = '8bit' if not defined $xfer_encoding;
Thomas Rast3cae7e52010-06-17 22:10:39 +02001630 $has_content_type = 1;
Jeff King8291db62007-11-16 05:49:09 -05001631 push @xh,
Paolo Bonzinibb294562014-11-25 15:00:26 +01001632 "Content-Type: text/plain; charset=$author_encoding";
Jeff King8291db62007-11-16 05:49:09 -05001633 }
1634 }
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001635 }
Paolo Bonzini8d814082014-11-25 15:00:27 +01001636 if (defined $target_xfer_encoding) {
1637 $xfer_encoding = '8bit' if not defined $xfer_encoding;
1638 $message = apply_transfer_encoding(
1639 $message, $xfer_encoding, $target_xfer_encoding);
1640 $xfer_encoding = $target_xfer_encoding;
1641 }
Paolo Bonzinibb294562014-11-25 15:00:26 +01001642 if (defined $xfer_encoding) {
1643 push @xh, "Content-Transfer-Encoding: $xfer_encoding";
1644 }
1645 if (defined $xfer_encoding or $has_content_type) {
1646 unshift @xh, 'MIME-Version: 1.0' unless $has_mime_version;
1647 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001648
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001649 $needs_confirm = (
1650 $confirm eq "always" or
1651 ($confirm =~ /^(?:auto|cc)$/ && @cc) or
1652 ($confirm =~ /^(?:auto|compose)$/ && $compose && $message_num == 1));
1653 $needs_confirm = "inform" if ($needs_confirm && $confirm_unconfigured && @cc);
1654
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001655 @to = process_address_list(@to);
1656 @cc = process_address_list(@cc);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001657
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001658 @to = (@initial_to, @to);
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001659 @cc = (@initial_cc, @cc);
1660
Michael S. Tsirkinf515c902014-04-29 08:41:16 +03001661 if ($message_num == 1) {
1662 if (defined $cover_cc and $cover_cc) {
1663 @initial_cc = @cc;
1664 }
1665 if (defined $cover_to and $cover_to) {
1666 @initial_to = @to;
1667 }
1668 }
1669
Michael Witten15da1082009-04-13 13:23:51 -05001670 my $message_was_sent = send_message();
Ryan Anderson83b24432005-07-31 04:17:25 -04001671
1672 # set up for the next message
Junio C Hamano95a877a2009-06-12 09:23:43 -07001673 if ($thread && $message_was_sent &&
Felipe Contrerasb99d22f2013-05-24 22:44:52 -05001674 ($chain_reply_to || !defined $reply_to || length($reply_to) == 0 ||
Antonio Ospitedb54c8e2010-11-12 15:55:08 +01001675 $message_num == 1)) {
Ryan Anderson78488b22005-07-31 20:04:24 -04001676 $reply_to = $message_id;
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001677 if (length $references > 0) {
YOSHIFUJI Hideaki / 吉藤英明a925b892007-04-06 08:50:24 +09001678 $references .= "\n $message_id";
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001679 } else {
1680 $references = "$message_id";
1681 }
Ryan Anderson78488b22005-07-31 20:04:24 -04001682 }
Jeff King4f3d3702007-12-17 15:51:34 -05001683 $message_id = undef;
Ryan Anderson83b24432005-07-31 04:17:25 -04001684}
Ryan Andersone2057352005-08-02 21:45:22 -04001685
Joe Perches6e74e072010-09-24 10:03:00 -07001686# Execute a command (e.g. $to_cmd) to get a list of email addresses
1687# and return a results array
1688sub recipients_cmd {
1689 my ($prefix, $what, $cmd, $file) = @_;
1690
Joe Perches6e74e072010-09-24 10:03:00 -07001691 my @addresses = ();
Ramkumar Ramachandraa47eab02013-03-31 18:40:42 -07001692 open my $fh, "-|", "$cmd \Q$file\E"
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001693 or die sprintf(__("(%s) Could not execute '%s'"), $prefix, $cmd);
Junio C Hamano7ebee442010-10-26 22:02:52 -07001694 while (my $address = <$fh>) {
Joe Perches6e74e072010-09-24 10:03:00 -07001695 $address =~ s/^\s*//g;
1696 $address =~ s/\s*$//g;
1697 $address = sanitize_address($address);
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001698 next if ($address eq $sender and $suppress_cc{'self'});
Joe Perches6e74e072010-09-24 10:03:00 -07001699 push @addresses, $address;
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001700 printf(__("(%s) Adding %s: %s from: '%s'\n"),
1701 $prefix, $what, $address, $cmd) unless $quiet;
Joe Perches6e74e072010-09-24 10:03:00 -07001702 }
Junio C Hamano7ebee442010-10-26 22:02:52 -07001703 close $fh
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001704 or die sprintf(__("(%s) failed to close pipe to '%s'"), $prefix, $cmd);
Joe Perches6e74e072010-09-24 10:03:00 -07001705 return @addresses;
1706}
1707
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001708cleanup_compose_files();
Ryan Anderson1f038a02005-09-05 01:13:07 -04001709
Ævar Arnfjörð Bjarmason4bf597e2010-09-30 13:43:00 +00001710sub cleanup_compose_files {
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001711 unlink($compose_filename, $compose_filename . ".final") if $compose;
Ryan Anderson1f038a02005-09-05 01:13:07 -04001712}
1713
Eric Wong4bc87a22006-03-25 17:20:48 -08001714$smtp->quit if $smtp;
Ryan Andersone2057352005-08-02 21:45:22 -04001715
Paolo Bonzini8d814082014-11-25 15:00:27 +01001716sub apply_transfer_encoding {
1717 my $message = shift;
1718 my $from = shift;
1719 my $to = shift;
1720
1721 return $message if ($from eq $to and $from ne '7bit');
1722
1723 require MIME::QuotedPrint;
1724 require MIME::Base64;
1725
1726 $message = MIME::QuotedPrint::decode($message)
1727 if ($from eq 'quoted-printable');
1728 $message = MIME::Base64::decode($message)
1729 if ($from eq 'base64');
1730
Vasco Almeida46493102016-12-14 11:54:36 -01001731 die __("cannot send message as 7bit")
Paolo Bonzini8d814082014-11-25 15:00:27 +01001732 if ($to eq '7bit' and $message =~ /[^[:ascii:]]/);
1733 return $message
1734 if ($to eq '7bit' or $to eq '8bit');
1735 return MIME::QuotedPrint::encode($message, "\n", 0)
1736 if ($to eq 'quoted-printable');
1737 return MIME::Base64::encode($message, "\n")
1738 if ($to eq 'base64');
Vasco Almeida46493102016-12-14 11:54:36 -01001739 die __("invalid transfer encoding");
Paolo Bonzini8d814082014-11-25 15:00:27 +01001740}
1741
Ævar Arnfjörð Bjarmasonc438ea22010-09-30 13:42:59 +00001742sub unique_email_list {
Ryan Andersone2057352005-08-02 21:45:22 -04001743 my %seen;
1744 my @emails;
1745
1746 foreach my $entry (@_) {
Krzysztof Mazure4312252012-11-22 19:12:10 +01001747 my $clean = extract_valid_address_or_die($entry);
1748 $seen{$clean} ||= 0;
1749 next if $seen{$clean}++;
1750 push @emails, $entry;
Ryan Andersone2057352005-08-02 21:45:22 -04001751 }
1752 return @emails;
1753}
Jeff King747bbff2008-01-18 09:19:48 -05001754
1755sub validate_patch {
1756 my $fn = shift;
Jonathan Tan64896602017-05-12 15:38:26 -07001757
1758 my $validate_hook = catfile(catdir($repo->repo_path(), 'hooks'),
1759 'sendemail-validate');
1760 my $hook_error;
1761 if (-x $validate_hook) {
1762 my $target = abs_path($fn);
1763 # The hook needs a correct cwd and GIT_DIR.
1764 my $cwd_save = cwd();
1765 chdir($repo->wc_path() or $repo->repo_path())
1766 or die("chdir: $!");
1767 local $ENV{"GIT_DIR"} = $repo->repo_path();
1768 $hook_error = "rejected by sendemail-validate hook"
1769 if system($validate_hook, $target);
1770 chdir($cwd_save) or die("chdir: $!");
1771 }
1772 return $hook_error if $hook_error;
1773
Jeff King747bbff2008-01-18 09:19:48 -05001774 open(my $fh, '<', $fn)
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001775 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
Jeff King747bbff2008-01-18 09:19:48 -05001776 while (my $line = <$fh>) {
1777 if (length($line) > 998) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001778 return sprintf(__("%s: patch contains a line longer than 998 characters"), $.);
Jeff King747bbff2008-01-18 09:19:48 -05001779 }
1780 }
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -07001781 return;
Jeff King747bbff2008-01-18 09:19:48 -05001782}
Jeff King0706bd12008-03-28 17:28:33 -04001783
Junio C Hamano531220b2016-03-17 22:40:05 -07001784sub handle_backup {
1785 my ($last, $lastlen, $file, $known_suffix) = @_;
1786 my ($suffix, $skip);
1787
1788 $skip = 0;
1789 if (defined $last &&
1790 ($lastlen < length($file)) &&
1791 (substr($file, 0, $lastlen) eq $last) &&
1792 ($suffix = substr($file, $lastlen)) !~ /^[a-z0-9]/i) {
1793 if (defined $known_suffix && $suffix eq $known_suffix) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001794 printf(__("Skipping %s with backup suffix '%s'.\n"), $file, $known_suffix);
Junio C Hamano531220b2016-03-17 22:40:05 -07001795 $skip = 1;
1796 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001797 # TRANSLATORS: please keep "[y|N]" as is.
1798 my $answer = ask(sprintf(__("Do you really want to send %s? [y|N]: "), $file),
Junio C Hamano531220b2016-03-17 22:40:05 -07001799 valid_re => qr/^(?:y|n)/i,
1800 default => 'n');
1801 $skip = ($answer ne 'y');
1802 if ($skip) {
1803 $known_suffix = $suffix;
1804 }
1805 }
1806 }
1807 return ($skip, $known_suffix);
1808}
1809
1810sub handle_backup_files {
1811 my @file = @_;
1812 my ($last, $lastlen, $known_suffix, $skip, @result);
1813 for my $file (@file) {
1814 ($skip, $known_suffix) = handle_backup($last, $lastlen,
1815 $file, $known_suffix);
1816 push @result, $file unless $skip;
1817 $last = $file;
1818 $lastlen = length($file);
1819 }
1820 return @result;
1821}
1822
Jeff King0706bd12008-03-28 17:28:33 -04001823sub file_has_nonascii {
1824 my $fn = shift;
1825 open(my $fh, '<', $fn)
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001826 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
Jeff King0706bd12008-03-28 17:28:33 -04001827 while (my $line = <$fh>) {
1828 return 1 if $line =~ /[^[:ascii:]]/;
1829 }
1830 return 0;
1831}
Thomas Rast3cae7e52010-06-17 22:10:39 +02001832
1833sub body_or_subject_has_nonascii {
1834 my $fn = shift;
1835 open(my $fh, '<', $fn)
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001836 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
Thomas Rast3cae7e52010-06-17 22:10:39 +02001837 while (my $line = <$fh>) {
1838 last if $line =~ /^$/;
1839 return 1 if $line =~ /^Subject.*[^[:ascii:]]/;
1840 }
1841 while (my $line = <$fh>) {
1842 return 1 if $line =~ /[^[:ascii:]]/;
1843 }
1844 return 0;
1845}