blob: eea0a517f71b66f861db38f70fac0593f8c8cea4 [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 /;
Ævar Arnfjörð Bjarmason89bf1ba2010-09-14 19:02:24 +000028use File::Spec::Functions qw(catfile);
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +010029use Error qw(:try);
Petr Baudis3cb8caf2006-07-03 22:47:58 +020030use Git;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -010031use Git::I18N;
Ryan Anderson83b24432005-07-31 04:17:25 -040032
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +010033Getopt::Long::Configure qw/ pass_through /;
34
Junio C Hamano280242d2006-07-02 16:03:59 -070035package FakeTerm;
36sub new {
37 my ($class, $reason) = @_;
38 return bless \$reason, shift;
39}
40sub readline {
41 my $self = shift;
42 die "Cannot use readline on FakeTerm: $$self";
43}
44package main;
45
Michael Coleman1b0baf12007-02-27 22:47:54 -060046
47sub usage {
48 print <<EOT;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +010049git send-email [options] <file | directory | rev-list options >
Jacob Keller17b7a832015-11-19 14:52:11 -080050git send-email --dump-aliases
Michael Witten4ed62b02008-09-30 07:58:30 -050051
52 Composing:
53 --from <str> * Email From:
Stephen Boydf434c082010-03-07 14:46:48 -080054 --[no-]to <str> * Email To:
55 --[no-]cc <str> * Email Cc:
56 --[no-]bcc <str> * Email Bcc:
Michael Witten4ed62b02008-09-30 07:58:30 -050057 --subject <str> * Email "Subject:"
58 --in-reply-to <str> * Email "In-Reply-To:"
Luis Henriquesac1596a2014-03-24 21:38:27 +000059 --[no-]xmailer * Add "X-Mailer:" header (default).
Felipe Contreras402596a2013-04-07 01:10:27 -060060 --[no-]annotate * Review each patch that will be sent in an editor.
Michael Witten4ed62b02008-09-30 07:58:30 -050061 --compose * Open an editor for introduction.
Krzysztof Mazur62e00692012-10-10 01:02:56 +020062 --compose-encoding <str> * Encoding to assume for introduction.
Thomas Rast3cae7e52010-06-17 22:10:39 +020063 --8bit-encoding <str> * Encoding to assume 8bit mails if undeclared
Paolo Bonzini8d814082014-11-25 15:00:27 +010064 --transfer-encoding <str> * Transfer encoding to use (quoted-printable, 8bit, base64)
Michael Witten4ed62b02008-09-30 07:58:30 -050065
66 Sending:
67 --envelope-sender <str> * Email envelope sender.
68 --smtp-server <str:int> * Outgoing SMTP server to use. The port
69 is optional. Default 'localhost'.
Pascal Obry052fbea2010-09-06 20:12:11 +020070 --smtp-server-option <str> * Outgoing SMTP server option to use.
Michael Witten4ed62b02008-09-30 07:58:30 -050071 --smtp-server-port <int> * Outgoing SMTP server port.
72 --smtp-user <str> * Username for SMTP-AUTH.
73 --smtp-pass <str> * Password for SMTP-AUTH; not necessary.
74 --smtp-encryption <str> * tls or ssl; anything else disables.
75 --smtp-ssl * Deprecated. Use '--smtp-encryption ssl'.
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -070076 --smtp-ssl-cert-path <str> * Path to ca-certificates (either directory or file).
77 Pass an empty string to disable certificate
78 verification.
Jari Aalto134550f2010-03-14 17:16:45 +020079 --smtp-domain <str> * The domain name sent to HELO/EHLO handshake
Jan Viktorin0f2e68b2015-08-12 01:39:44 +020080 --smtp-auth <str> * Space-separated list of allowed AUTH mechanisms.
81 This setting forces to use one of the listed mechanisms.
Jari Aaltof60812e2010-03-14 17:16:09 +020082 --smtp-debug <0|1> * Disable, enable Net::SMTP debug.
Michael Witten4ed62b02008-09-30 07:58:30 -050083
84 Automating:
85 --identity <str> * Use the sendemail.<id> options.
Joe Perches6e74e072010-09-24 10:03:00 -070086 --to-cmd <str> * Email To: via `<str> \$patch_path`
Michael Witten4ed62b02008-09-30 07:58:30 -050087 --cc-cmd <str> * Email Cc: via `<str> \$patch_path`
Jay Soffian3531e272009-02-14 23:32:15 -050088 --suppress-cc <str> * author, self, sob, cc, cccmd, body, bodycc, all.
Michael S. Tsirkinf515c902014-04-29 08:41:16 +030089 --[no-]cc-cover * Email Cc: addresses in the cover letter.
90 --[no-]to-cover * Email To: addresses in the cover letter.
Jay Soffian3531e272009-02-14 23:32:15 -050091 --[no-]signed-off-by-cc * Send to Signed-off-by: addresses. Default on.
Michael Witten4ed62b02008-09-30 07:58:30 -050092 --[no-]suppress-from * Send to self. Default off.
Junio C Hamano41fe87f2009-08-22 12:48:48 -070093 --[no-]chain-reply-to * Chain In-Reply-To: fields. Default off.
Michael Witten4ed62b02008-09-30 07:58:30 -050094 --[no-]thread * Use In-Reply-To: field. Default on.
95
96 Administering:
Jay Soffianc1f2aa42009-03-02 23:52:18 -050097 --confirm <str> * Confirm recipients before sending;
98 auto, cc, compose, always, or never.
Michael Witten4ed62b02008-09-30 07:58:30 -050099 --quiet * Output one line of info per email.
100 --dry-run * Don't actually send the emails.
101 --[no-]validate * Perform patch sanity checks. Default on.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100102 --[no-]format-patch * understand any non optional arguments as
103 `git format-patch` ones.
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200104 --force * Send even if safety checks would prevent it.
Jeff Kingc764a0c2008-01-18 09:20:10 -0500105
Jacob Keller17b7a832015-11-19 14:52:11 -0800106 Information:
107 --dump-aliases * Dump configured aliases and exit.
108
Michael Coleman1b0baf12007-02-27 22:47:54 -0600109EOT
110 exit(1);
111}
112
Eric Wong4bc87a22006-03-25 17:20:48 -0800113# most mail servers generate the Date: header, but not all...
Jakub Narebski6bdca892006-07-07 20:57:55 +0200114sub format_2822_time {
115 my ($time) = @_;
116 my @localtm = localtime($time);
117 my @gmttm = gmtime($time);
118 my $localmin = $localtm[1] + $localtm[2] * 60;
119 my $gmtmin = $gmttm[1] + $gmttm[2] * 60;
120 if ($localtm[0] != $gmttm[0]) {
Vasco Almeida46493102016-12-14 11:54:36 -0100121 die __("local zone differs from GMT by a non-minute interval\n");
Jakub Narebski6bdca892006-07-07 20:57:55 +0200122 }
123 if ((($gmttm[6] + 1) % 7) == $localtm[6]) {
124 $localmin += 1440;
125 } elsif ((($gmttm[6] - 1) % 7) == $localtm[6]) {
126 $localmin -= 1440;
127 } elsif ($gmttm[6] != $localtm[6]) {
Vasco Almeida46493102016-12-14 11:54:36 -0100128 die __("local time offset greater than or equal to 24 hours\n");
Jakub Narebski6bdca892006-07-07 20:57:55 +0200129 }
130 my $offset = $localmin - $gmtmin;
131 my $offhour = $offset / 60;
132 my $offmin = abs($offset % 60);
133 if (abs($offhour) >= 24) {
Vasco Almeida46493102016-12-14 11:54:36 -0100134 die __("local time offset greater than or equal to 24 hours\n");
Jakub Narebski6bdca892006-07-07 20:57:55 +0200135 }
136
137 return sprintf("%s, %2d %s %d %02d:%02d:%02d %s%02d%02d",
138 qw(Sun Mon Tue Wed Thu Fri Sat)[$localtm[6]],
139 $localtm[3],
140 qw(Jan Feb Mar Apr May Jun
141 Jul Aug Sep Oct Nov Dec)[$localtm[4]],
142 $localtm[5]+1900,
143 $localtm[2],
144 $localtm[1],
145 $localtm[0],
146 ($offset >= 0) ? '+' : '-',
147 abs($offhour),
148 $offmin,
149 );
150}
Eric Wong4bc87a22006-03-25 17:20:48 -0800151
Eric Wong567ffeb2006-03-25 16:47:12 -0800152my $have_email_valid = eval { require Email::Valid; 1 };
Jay Soffian50126992009-02-14 23:32:14 -0500153my $have_mail_address = eval { require Mail::Address; 1 };
Eric Wong4bc87a22006-03-25 17:20:48 -0800154my $smtp;
Wincent Colaiuta5f5b6112007-11-21 13:35:05 +0100155my $auth;
Eric Wong4bc87a22006-03-25 17:20:48 -0800156
Роман Донченко11f70a72014-12-14 18:59:46 +0300157# Regexes for RFC 2047 productions.
158my $re_token = qr/[^][()<>@,;:\\"\/?.= \000-\037\177-\377]+/;
159my $re_encoded_text = qr/[^? \000-\037\177-\377]+/;
160my $re_encoded_word = qr/=\?($re_token)\?($re_token)\?($re_encoded_text)\?=/;
161
Ryan Anderson83b24432005-07-31 04:17:25 -0400162# Variables we fill in automatically, or via prompting:
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700163my (@to,$no_to,@initial_to,@cc,$no_cc,@initial_cc,@bcclist,$no_bcc,@xh,
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100164 $initial_reply_to,$initial_subject,@files,
Luis Henriquesac1596a2014-03-24 21:38:27 +0000165 $author,$sender,$smtp_authpass,$annotate,$use_xmailer,$compose,$time);
Ryan Anderson83b24432005-07-31 04:17:25 -0400166
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700167my $envelope_sender;
Ryan Anderson78488b22005-07-31 20:04:24 -0400168
Ryan Anderson91332612005-07-31 20:04:24 -0400169# Example reply to:
Ryan Anderson83b24432005-07-31 04:17:25 -0400170#$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
Ryan Anderson83b24432005-07-31 04:17:25 -0400171
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100172my $repo = eval { Git->repository() };
173my @repo = $repo ? ($repo) : ();
Junio C Hamano280242d2006-07-02 16:03:59 -0700174my $term = eval {
Jay Soffian0fb7fc72008-02-21 19:16:04 -0500175 $ENV{"GIT_SEND_EMAIL_NOTTY"}
176 ? new Term::ReadLine 'git-send-email', \*STDIN, \*STDOUT
177 : new Term::ReadLine 'git-send-email';
Junio C Hamano280242d2006-07-02 16:03:59 -0700178};
179if ($@) {
180 $term = new FakeTerm "$@: going non-interactive";
181}
Ryan Anderson83b24432005-07-31 04:17:25 -0400182
Adam Roben5483c712007-06-27 20:59:37 -0700183# Behavior modification variables
184my ($quiet, $dry_run) = (0, 0);
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100185my $format_patch;
Jay Soffianafe756c2009-02-23 13:51:37 -0500186my $compose_filename;
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200187my $force = 0;
Jacob Keller17b7a832015-11-19 14:52:11 -0800188my $dump_aliases = 0;
Adam Roben5483c712007-06-27 20:59:37 -0700189
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100190# Handle interactive edition of files.
191my $multiedit;
Michael J Gruber0ce142c2010-03-22 17:12:53 +0100192my $editor;
Jonathan Niederb4479f02009-10-30 20:42:34 -0500193
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100194sub do_edit {
Michael J Gruber0ce142c2010-03-22 17:12:53 +0100195 if (!defined($editor)) {
196 $editor = Git::command_oneline('var', 'GIT_EDITOR');
197 }
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100198 if (defined($multiedit) && !$multiedit) {
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100199 map {
200 system('sh', '-c', $editor.' "$@"', $editor, $_);
201 if (($? & 127) || ($? >> 8)) {
Vasco Almeida46493102016-12-14 11:54:36 -0100202 die(__("the editor exited uncleanly, aborting everything"));
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100203 }
204 } @_;
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100205 } else {
206 system('sh', '-c', $editor.' "$@"', $editor, @_);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100207 if (($? & 127) || ($? >> 8)) {
Vasco Almeida46493102016-12-14 11:54:36 -0100208 die(__("the editor exited uncleanly, aborting everything"));
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100209 }
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100210 }
211}
Adam Roben5483c712007-06-27 20:59:37 -0700212
213# Variables with corresponding config settings
Joe Perches6e74e072010-09-24 10:03:00 -0700214my ($thread, $chain_reply_to, $suppress_from, $signed_off_by_cc);
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300215my ($cover_cc, $cover_to);
Joe Perches6e74e072010-09-24 10:03:00 -0700216my ($to_cmd, $cc_cmd);
Pascal Obry052fbea2010-09-06 20:12:11 +0200217my ($smtp_server, $smtp_server_port, @smtp_server_options);
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -0700218my ($smtp_authuser, $smtp_encryption, $smtp_ssl_cert_path);
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200219my ($identity, $aliasfiletype, @alias_files, $smtp_domain, $smtp_auth);
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500220my ($validate, $confirm);
David Brown65648282007-12-25 19:56:29 -0800221my (@suppress_cc);
Thomas Rast3cae7e52010-06-17 22:10:39 +0200222my ($auto_8bit_encoding);
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200223my ($compose_encoding);
Paolo Bonzini8d814082014-11-25 15:00:27 +0100224my ($target_xfer_encoding);
Adam Roben5483c712007-06-27 20:59:37 -0700225
Jari Aaltof60812e2010-03-14 17:16:09 +0200226my ($debug_net_smtp) = 0; # Net::SMTP, see send_message()
227
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900228my %config_bool_settings = (
Adam Roben5483c712007-06-27 20:59:37 -0700229 "thread" => [\$thread, 1],
Felipe Contrerasb99d22f2013-05-24 22:44:52 -0500230 "chainreplyto" => [\$chain_reply_to, 0],
David Brown65648282007-12-25 19:56:29 -0800231 "suppressfrom" => [\$suppress_from, undef],
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500232 "signedoffbycc" => [\$signed_off_by_cc, undef],
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300233 "cccover" => [\$cover_cc, undef],
234 "tocover" => [\$cover_to, undef],
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500235 "signedoffcc" => [\$signed_off_by_cc, undef], # Deprecated
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500236 "validate" => [\$validate, 1],
Felipe Contreras402596a2013-04-07 01:10:27 -0600237 "multiedit" => [\$multiedit, undef],
Luis Henriquesac1596a2014-03-24 21:38:27 +0000238 "annotate" => [\$annotate, undef],
239 "xmailer" => [\$use_xmailer, 1]
Adam Robene46f7a02007-06-26 15:48:30 -0700240);
241
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900242my %config_settings = (
243 "smtpserver" => \$smtp_server,
Junio C Hamano44b24762007-09-25 17:27:54 -0700244 "smtpserverport" => \$smtp_server_port,
Pascal Obry052fbea2010-09-06 20:12:11 +0200245 "smtpserveroption" => \@smtp_server_options,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900246 "smtpuser" => \$smtp_authuser,
247 "smtppass" => \$smtp_authpass,
Pascal Obrye1e91152010-09-06 20:12:09 +0200248 "smtpdomain" => \$smtp_domain,
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200249 "smtpauth" => \$smtp_auth,
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700250 "to" => \@initial_to,
Joe Perches6e74e072010-09-24 10:03:00 -0700251 "tocmd" => \$to_cmd,
Miklos Vajna5f8b9fc2008-04-27 14:14:58 +0200252 "cc" => \@initial_cc,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900253 "cccmd" => \$cc_cmd,
254 "aliasfiletype" => \$aliasfiletype,
255 "bcc" => \@bcclist,
David Brown65648282007-12-25 19:56:29 -0800256 "suppresscc" => \@suppress_cc,
Ask Bjørn Hansen9f7820a2008-06-07 00:33:42 -0700257 "envelopesender" => \$envelope_sender,
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500258 "confirm" => \$confirm,
Trent Piepho09caa242009-05-12 15:48:56 -0700259 "from" => \$sender,
Thomas Rast3cae7e52010-06-17 22:10:39 +0200260 "assume8bitencoding" => \$auto_8bit_encoding,
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200261 "composeencoding" => \$compose_encoding,
Paolo Bonzini8d814082014-11-25 15:00:27 +0100262 "transferencoding" => \$target_xfer_encoding,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900263);
Avi Kivity4a62d3f2007-03-11 19:19:44 +0200264
Cord Seelecec5dae2011-09-30 12:52:25 +0200265my %config_path_settings = (
266 "aliasesfile" => \@alias_files,
John Keeping6e07a3b2015-11-17 22:01:05 +0000267 "smtpsslcertpath" => \$smtp_ssl_cert_path,
Cord Seelecec5dae2011-09-30 12:52:25 +0200268);
269
Michael Witten87429972008-02-03 19:53:57 -0500270# Handle Uncouth Termination
271sub signal_handler {
272
273 # Make text normal
274 print color("reset"), "\n";
275
276 # SMTP password masked
277 system "stty echo";
278
279 # tmp files from --compose
Jay Soffianafe756c2009-02-23 13:51:37 -0500280 if (defined $compose_filename) {
281 if (-e $compose_filename) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100282 printf __("'%s' contains an intermediate version ".
283 "of the email you were composing.\n"),
284 $compose_filename;
Jay Soffianafe756c2009-02-23 13:51:37 -0500285 }
286 if (-e ($compose_filename . ".final")) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100287 printf __("'%s.final' contains the composed email.\n"),
288 $compose_filename;
Jay Soffianafe756c2009-02-23 13:51:37 -0500289 }
Michael Witten87429972008-02-03 19:53:57 -0500290 }
291
292 exit;
293};
294
295$SIG{TERM} = \&signal_handler;
296$SIG{INT} = \&signal_handler;
297
Ryan Anderson83b24432005-07-31 04:17:25 -0400298# Begin by accumulating all the variables (defined above), that we will end up
299# needing, first, from the command line:
300
Clemens Buchacherc5978242011-09-03 19:06:13 +0200301my $help;
302my $rc = GetOptions("h" => \$help,
Jacob Keller17b7a832015-11-19 14:52:11 -0800303 "dump-aliases" => \$dump_aliases);
304usage() unless $rc;
Vasco Almeida46493102016-12-14 11:54:36 -0100305die __("--dump-aliases incompatible with other options\n")
Jacob Keller17b7a832015-11-19 14:52:11 -0800306 if !$help and $dump_aliases and @ARGV;
307$rc = GetOptions(
Clemens Buchacherc5978242011-09-03 19:06:13 +0200308 "sender|from=s" => \$sender,
Ryan Anderson83b24432005-07-31 04:17:25 -0400309 "in-reply-to=s" => \$initial_reply_to,
310 "subject=s" => \$initial_subject,
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700311 "to=s" => \@initial_to,
Joe Perches6e74e072010-09-24 10:03:00 -0700312 "to-cmd=s" => \$to_cmd,
Stephen Boydf434c082010-03-07 14:46:48 -0800313 "no-to" => \$no_to,
Ryan Andersonda140f82006-02-13 03:05:15 -0500314 "cc=s" => \@initial_cc,
Stephen Boydf434c082010-03-07 14:46:48 -0800315 "no-cc" => \$no_cc,
Ryan Anderson58063242006-05-29 12:30:13 -0700316 "bcc=s" => \@bcclist,
Stephen Boydf434c082010-03-07 14:46:48 -0800317 "no-bcc" => \$no_bcc,
Ryan Anderson78488b22005-07-31 20:04:24 -0400318 "chain-reply-to!" => \$chain_reply_to,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800319 "no-chain-reply-to" => sub {$chain_reply_to = 0},
Ryan Anderson3342d852005-07-31 20:04:24 -0400320 "smtp-server=s" => \$smtp_server,
Pascal Obry052fbea2010-09-06 20:12:11 +0200321 "smtp-server-option=s" => \@smtp_server_options,
Junio C Hamano44b24762007-09-25 17:27:54 -0700322 "smtp-server-port=s" => \$smtp_server_port,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900323 "smtp-user=s" => \$smtp_authuser,
Michael Witten2363d742008-02-03 19:53:56 -0500324 "smtp-pass:s" => \$smtp_authpass,
Thomas Rastf6bebd12008-06-25 21:42:43 +0200325 "smtp-ssl" => sub { $smtp_encryption = 'ssl' },
326 "smtp-encryption=s" => \$smtp_encryption,
Thomas Rast979e6522013-12-01 23:48:42 +0100327 "smtp-ssl-cert-path=s" => \$smtp_ssl_cert_path,
Jari Aaltof60812e2010-03-14 17:16:09 +0200328 "smtp-debug:i" => \$debug_net_smtp,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -0400329 "smtp-domain:s" => \$smtp_domain,
Jan Viktorin0f2e68b2015-08-12 01:39:44 +0200330 "smtp-auth=s" => \$smtp_auth,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900331 "identity=s" => \$identity,
Felipe Contreras402596a2013-04-07 01:10:27 -0600332 "annotate!" => \$annotate,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800333 "no-annotate" => sub {$annotate = 0},
Ryan Anderson1f038a02005-09-05 01:13:07 -0400334 "compose" => \$compose,
Ryan Anderson30d08b32006-02-02 11:56:06 -0500335 "quiet" => \$quiet,
Joe Perches324a8bd2007-08-17 18:51:12 -0700336 "cc-cmd=s" => \$cc_cmd,
Adam Roben5483c712007-06-27 20:59:37 -0700337 "suppress-from!" => \$suppress_from,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800338 "no-suppress-from" => sub {$suppress_from = 0},
David Brown65648282007-12-25 19:56:29 -0800339 "suppress-cc=s" => \@suppress_cc,
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500340 "signed-off-cc|signed-off-by-cc!" => \$signed_off_by_cc,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800341 "no-signed-off-cc|no-signed-off-by-cc" => sub {$signed_off_by_cc = 0},
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300342 "cc-cover|cc-cover!" => \$cover_cc,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800343 "no-cc-cover" => sub {$cover_cc = 0},
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300344 "to-cover|to-cover!" => \$cover_to,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800345 "no-to-cover" => sub {$cover_to = 0},
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500346 "confirm=s" => \$confirm,
Matthew Wilcox61302592006-10-10 08:58:23 -0600347 "dry-run" => \$dry_run,
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700348 "envelope-sender=s" => \$envelope_sender,
Adam Roben5483c712007-06-27 20:59:37 -0700349 "thread!" => \$thread,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800350 "no-thread" => sub {$thread = 0},
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500351 "validate!" => \$validate,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800352 "no-validate" => sub {$validate = 0},
Paolo Bonzini8d814082014-11-25 15:00:27 +0100353 "transfer-encoding=s" => \$target_xfer_encoding,
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100354 "format-patch!" => \$format_patch,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800355 "no-format-patch" => sub {$format_patch = 0},
Thomas Rast3cae7e52010-06-17 22:10:39 +0200356 "8bit-encoding=s" => \$auto_8bit_encoding,
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200357 "compose-encoding=s" => \$compose_encoding,
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200358 "force" => \$force,
Luis Henriquesac1596a2014-03-24 21:38:27 +0000359 "xmailer!" => \$use_xmailer,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800360 "no-xmailer" => sub {$use_xmailer = 0},
Ryan Anderson83b24432005-07-31 04:17:25 -0400361 );
362
Clemens Buchacherc5978242011-09-03 19:06:13 +0200363usage() if $help;
Michael Coleman1b0baf12007-02-27 22:47:54 -0600364unless ($rc) {
365 usage();
366}
367
Vasco Almeida46493102016-12-14 11:54:36 -0100368die __("Cannot run git format-patch from outside a repository\n")
Jay Soffianeed6ca72009-02-14 23:32:13 -0500369 if $format_patch and not $repo;
370
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900371# Now, let's fill any that aren't set in with defaults:
372
373sub read_config {
374 my ($prefix) = @_;
375
376 foreach my $setting (keys %config_bool_settings) {
377 my $target = $config_bool_settings{$setting}->[0];
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100378 $$target = Git::config_bool(@repo, "$prefix.$setting") unless (defined $$target);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900379 }
380
Cord Seelecec5dae2011-09-30 12:52:25 +0200381 foreach my $setting (keys %config_path_settings) {
Cord Seele463b0ea2011-10-14 22:53:31 +0200382 my $target = $config_path_settings{$setting};
383 if (ref($target) eq "ARRAY") {
384 unless (@$target) {
385 my @values = Git::config_path(@repo, "$prefix.$setting");
386 @$target = @values if (@values && defined $values[0]);
387 }
388 }
389 else {
390 $$target = Git::config_path(@repo, "$prefix.$setting") unless (defined $$target);
391 }
Cord Seelecec5dae2011-09-30 12:52:25 +0200392 }
393
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900394 foreach my $setting (keys %config_settings) {
395 my $target = $config_settings{$setting};
Stephen Boydf434c082010-03-07 14:46:48 -0800396 next if $setting eq "to" and defined $no_to;
397 next if $setting eq "cc" and defined $no_cc;
398 next if $setting eq "bcc" and defined $no_bcc;
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900399 if (ref($target) eq "ARRAY") {
400 unless (@$target) {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100401 my @values = Git::config(@repo, "$prefix.$setting");
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900402 @$target = @values if (@values && defined $values[0]);
403 }
404 }
405 else {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100406 $$target = Git::config(@repo, "$prefix.$setting") unless (defined $$target);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900407 }
408 }
Thomas Rastf6bebd12008-06-25 21:42:43 +0200409
410 if (!defined $smtp_encryption) {
411 my $enc = Git::config(@repo, "$prefix.smtpencryption");
412 if (defined $enc) {
413 $smtp_encryption = $enc;
414 } elsif (Git::config_bool(@repo, "$prefix.smtpssl")) {
415 $smtp_encryption = 'ssl';
416 }
417 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900418}
419
420# read configuration from [sendemail "$identity"], fall back on [sendemail]
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100421$identity = Git::config(@repo, "sendemail.identity") unless (defined $identity);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900422read_config("sendemail.$identity") if (defined $identity);
423read_config("sendemail");
424
425# fall back on builtin bool defaults
426foreach my $setting (values %config_bool_settings) {
427 ${$setting->[0]} = $setting->[1] unless (defined (${$setting->[0]}));
428}
429
Thomas Rastfa835cd2008-06-26 23:03:21 +0200430# 'default' encryption is none -- this only prevents a warning
431$smtp_encryption = '' unless (defined $smtp_encryption);
432
David Brown65648282007-12-25 19:56:29 -0800433# Set CC suppressions
434my(%suppress_cc);
435if (@suppress_cc) {
436 foreach my $entry (@suppress_cc) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100437 die sprintf(__("Unknown --suppress-cc field: '%s'\n"), $entry)
Ævar Arnfjörð Bjarmasone9bf7412010-09-30 13:43:04 +0000438 unless $entry =~ /^(?:all|cccmd|cc|author|self|sob|body|bodycc)$/;
David Brown65648282007-12-25 19:56:29 -0800439 $suppress_cc{$entry} = 1;
440 }
441}
442
443if ($suppress_cc{'all'}) {
Paolo Bonzinicb8a9bd2009-06-18 14:31:32 +0200444 foreach my $entry (qw (cccmd cc author self sob body bodycc)) {
David Brown65648282007-12-25 19:56:29 -0800445 $suppress_cc{$entry} = 1;
446 }
447 delete $suppress_cc{'all'};
448}
449
450# If explicit old-style ones are specified, they trump --suppress-cc.
451$suppress_cc{'self'} = $suppress_from if defined $suppress_from;
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500452$suppress_cc{'sob'} = !$signed_off_by_cc if defined $signed_off_by_cc;
David Brown65648282007-12-25 19:56:29 -0800453
Jay Soffian3531e272009-02-14 23:32:15 -0500454if ($suppress_cc{'body'}) {
455 foreach my $entry (qw (sob bodycc)) {
456 $suppress_cc{$entry} = 1;
457 }
458 delete $suppress_cc{'body'};
459}
460
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500461# Set confirm's default value
462my $confirm_unconfigured = !defined $confirm;
463if ($confirm_unconfigured) {
464 $confirm = scalar %suppress_cc ? 'compose' : 'auto';
465};
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100466die sprintf(__("Unknown --confirm setting: '%s'\n"), $confirm)
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500467 unless $confirm =~ /^(?:auto|cc|compose|always|never)/;
468
David Brown65648282007-12-25 19:56:29 -0800469# Debugging, print out the suppressions.
470if (0) {
471 print "suppressions:\n";
472 foreach my $entry (keys %suppress_cc) {
473 printf " %-5s -> $suppress_cc{$entry}\n", $entry;
474 }
475}
476
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100477my ($repoauthor, $repocommitter);
478($repoauthor) = Git::ident_person(@repo, 'author');
479($repocommitter) = Git::ident_person(@repo, 'committer');
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900480
Jay Soffian50126992009-02-14 23:32:14 -0500481sub parse_address_line {
482 if ($have_mail_address) {
483 return map { $_->format } Mail::Address->parse($_[0]);
484 } else {
Remi Lespinet8d314d72015-07-07 15:38:15 +0200485 return Git::parse_mailboxes($_[0]);
Jay Soffian50126992009-02-14 23:32:14 -0500486 }
487}
488
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800489sub split_addrs {
Junio C Hamano2f0e7cb2008-12-21 01:57:59 -0800490 return quotewords('\s*,\s*', 1, @_);
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800491}
492
Eric Wong994d6c62006-05-14 19:13:44 -0700493my %aliases;
Eric Sunshine09f11572015-05-31 18:29:27 -0400494
495sub parse_sendmail_alias {
496 local $_ = shift;
497 if (/"/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100498 printf STDERR __("warning: sendmail alias with quotes is not supported: %s\n"), $_;
Eric Sunshine86b89842015-06-01 14:22:36 -0400499 } elsif (/:include:/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100500 printf STDERR __("warning: `:include:` not supported: %s\n"), $_;
Eric Sunshine86b89842015-06-01 14:22:36 -0400501 } elsif (/[\/|]/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100502 printf STDERR __("warning: `/file` or `|pipe` redirection not supported: %s\n"), $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400503 } elsif (/^(\S+?)\s*:\s*(.+)$/) {
504 my ($alias, $addr) = ($1, $2);
505 $aliases{$alias} = [ split_addrs($addr) ];
506 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100507 printf STDERR __("warning: sendmail line is not recognized: %s\n"), $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400508 }
509}
510
511sub parse_sendmail_aliases {
512 my $fh = shift;
Eric Sunshine2532dd02015-05-31 18:29:29 -0400513 my $s = '';
Eric Sunshine09f11572015-05-31 18:29:27 -0400514 while (<$fh>) {
Eric Sunshine2532dd02015-05-31 18:29:29 -0400515 chomp;
Eric Sunshine020be85f2015-05-31 18:29:28 -0400516 next if /^\s*$/ || /^\s*#/;
Eric Sunshine2532dd02015-05-31 18:29:29 -0400517 $s .= $_, next if $s =~ s/\\$// || s/^\s+//;
518 parse_sendmail_alias($s) if $s;
519 $s = $_;
Eric Sunshine09f11572015-05-31 18:29:27 -0400520 }
Eric Sunshine2532dd02015-05-31 18:29:29 -0400521 $s =~ s/\\$//; # silently tolerate stray '\' on last line
522 parse_sendmail_alias($s) if $s;
Eric Sunshine09f11572015-05-31 18:29:27 -0400523}
524
Eric Wong994d6c62006-05-14 19:13:44 -0700525my %parse_alias = (
526 # multiline formats can be supported in the future
527 mutt => sub { my $fh = shift; while (<$fh>) {
Felipe Contrerasffc01f92009-09-30 17:49:36 +0300528 if (/^\s*alias\s+(?:-group\s+\S+\s+)*(\S+)\s+(.*)$/) {
Eric Wong994d6c62006-05-14 19:13:44 -0700529 my ($alias, $addr) = ($1, $2);
530 $addr =~ s/#.*$//; # mutt allows # comments
Eric Wong2c510f22016-01-04 20:53:30 +0000531 # commas delimit multiple addresses
532 my @addr = split_addrs($addr);
533
534 # quotes may be escaped in the file,
535 # unescape them so we do not double-escape them later.
536 s/\\"/"/g foreach @addr;
537 $aliases{$alias} = \@addr
Eric Wong994d6c62006-05-14 19:13:44 -0700538 }}},
539 mailrc => sub { my $fh = shift; while (<$fh>) {
Jeff Kinga277d1e2016-03-17 19:58:22 -0400540 if (/^alias\s+(\S+)\s+(.*?)\s*$/) {
Eric Wong994d6c62006-05-14 19:13:44 -0700541 # spaces delimit multiple addresses
Eric W. Biedermanfe87c922009-05-20 19:45:53 -0700542 $aliases{$1} = [ quotewords('\s+', 0, $2) ];
Eric Wong994d6c62006-05-14 19:13:44 -0700543 }}},
Trent Piepho73c427e2008-11-25 18:55:00 -0800544 pine => sub { my $fh = shift; my $f='\t[^\t]*';
545 for (my $x = ''; defined($x); $x = $_) {
546 chomp $x;
547 $x .= $1 while(defined($_ = <$fh>) && /^ +(.*)$/);
548 $x =~ /^(\S+)$f\t\(?([^\t]+?)\)?(:?$f){0,2}$/ or next;
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800549 $aliases{$1} = [ split_addrs($2) ];
Trent Piepho73c427e2008-11-25 18:55:00 -0800550 }},
Bill Pemberton7613ea32009-04-22 09:41:29 -0400551 elm => sub { my $fh = shift;
552 while (<$fh>) {
553 if (/^(\S+)\s+=\s+[^=]+=\s(\S+)/) {
554 my ($alias, $addr) = ($1, $2);
555 $aliases{$alias} = [ split_addrs($addr) ];
556 }
557 } },
Eric Sunshine09f11572015-05-31 18:29:27 -0400558 sendmail => \&parse_sendmail_aliases,
Eric Wong994d6c62006-05-14 19:13:44 -0700559 gnus => sub { my $fh = shift; while (<$fh>) {
560 if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
561 $aliases{$1} = [ $2 ];
562 }}}
563);
564
Petr Baudis3cb8caf2006-07-03 22:47:58 +0200565if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) {
Eric Wong994d6c62006-05-14 19:13:44 -0700566 foreach my $file (@alias_files) {
567 open my $fh, '<', $file or die "opening $file: $!\n";
568 $parse_alias{$aliasfiletype}->($fh);
569 close $fh;
570 }
571}
572
Jacob Keller17b7a832015-11-19 14:52:11 -0800573if ($dump_aliases) {
574 print "$_\n" for (sort keys %aliases);
575 exit(0);
576}
577
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700578# is_format_patch_arg($f) returns 0 if $f names a patch, or 1 if
579# $f is a revision list specification to be passed to format-patch.
580sub is_format_patch_arg {
Jay Soffianeed6ca72009-02-14 23:32:13 -0500581 return unless $repo;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100582 my $f = shift;
583 try {
584 $repo->command('rev-parse', '--verify', '--quiet', $f);
585 if (defined($format_patch)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100586 return $format_patch;
587 }
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100588 die sprintf(__ <<EOF, $f, $f);
589File '%s' exists but it could also be the range of commits
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100590to produce patches for. Please disambiguate by...
591
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100592 * Saying "./%s" if you mean a file; or
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100593 * Giving --format-patch option if you mean a range.
594EOF
595 } catch Git::Error::Command with {
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700596 # Not a valid revision. Treat it as a filename.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100597 return 0;
598 }
599}
600
Jeff Kingaa548922008-01-18 09:19:36 -0500601# Now that all the defaults are set, process the rest of the command line
602# arguments and collect up the files that need to be processed.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100603my @rev_list_opts;
Junio C Hamano69f4ce52008-11-30 22:38:20 -0800604while (defined(my $f = shift @ARGV)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100605 if ($f eq "--") {
606 push @rev_list_opts, "--", @ARGV;
607 @ARGV = ();
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700608 } elsif (-d $f and !is_format_patch_arg($f)) {
Ævar Arnfjörð Bjarmasonc6038162010-09-30 13:42:54 +0000609 opendir my $dh, $f
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100610 or die sprintf(__("Failed to opendir %s: %s"), $f, $!);
Jeff Kingaa548922008-01-18 09:19:36 -0500611
Ævar Arnfjörð Bjarmason89bf1ba2010-09-14 19:02:24 +0000612 push @files, grep { -f $_ } map { catfile($f, $_) }
Ævar Arnfjörð Bjarmasonc6038162010-09-30 13:42:54 +0000613 sort readdir $dh;
614 closedir $dh;
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700615 } elsif ((-f $f or -p $f) and !is_format_patch_arg($f)) {
Jeff Kingaa548922008-01-18 09:19:36 -0500616 push @files, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500617 } else {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100618 push @rev_list_opts, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500619 }
620}
621
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100622if (@rev_list_opts) {
Vasco Almeida46493102016-12-14 11:54:36 -0100623 die __("Cannot run git format-patch from outside a repository\n")
Jay Soffianeed6ca72009-02-14 23:32:13 -0500624 unless $repo;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100625 push @files, $repo->command('format-patch', '-o', tempdir(CLEANUP => 1), @rev_list_opts);
626}
627
Junio C Hamano531220b2016-03-17 22:40:05 -0700628@files = handle_backup_files(@files);
629
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500630if ($validate) {
Jeff Kingc764a0c2008-01-18 09:20:10 -0500631 foreach my $f (@files) {
Kevin Ballard300913b2008-06-25 15:44:40 -0700632 unless (-p $f) {
633 my $error = validate_patch($f);
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100634 $error and die sprintf(__("fatal: %s: %s\nwarning: no patches were sent\n"),
635 $f, $error);
Kevin Ballard300913b2008-06-25 15:44:40 -0700636 }
Jeff Kingc764a0c2008-01-18 09:20:10 -0500637 }
Jeff King747bbff2008-01-18 09:19:48 -0500638}
639
Jeff Kingaa548922008-01-18 09:19:36 -0500640if (@files) {
641 unless ($quiet) {
642 print $_,"\n" for (@files);
643 }
644} else {
Vasco Almeida46493102016-12-14 11:54:36 -0100645 print STDERR __("\nNo patch files specified!\n\n");
Jeff Kingaa548922008-01-18 09:19:36 -0500646 usage();
647}
648
Ævar Arnfjörð Bjarmasonacf071b2010-09-30 13:42:57 +0000649sub get_patch_subject {
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100650 my $fn = shift;
651 open (my $fh, '<', $fn);
652 while (my $line = <$fh>) {
653 next unless ($line =~ /^Subject: (.*)$/);
654 close $fh;
655 return "GIT: $1\n";
656 }
657 close $fh;
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100658 die sprintf(__("No subject line in %s?"), $fn);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100659}
660
661if ($compose) {
662 # Note that this does not need to be secure, but we will make a small
663 # effort to have it be unique
Jay Soffianafe756c2009-02-23 13:51:37 -0500664 $compose_filename = ($repo ?
665 tempfile(".gitsendemail.msg.XXXXXX", DIR => $repo->repo_path()) :
666 tempfile(".gitsendemail.msg.XXXXXX", DIR => "."))[1];
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000667 open my $c, ">", $compose_filename
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100668 or die sprintf(__("Failed to open for writing %s: %s"), $compose_filename, $!);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100669
670
671 my $tpl_sender = $sender || $repoauthor || $repocommitter || '';
672 my $tpl_subject = $initial_subject || '';
673 my $tpl_reply_to = $initial_reply_to || '';
674
Vasco Almeida70aedfb2016-12-14 11:54:38 -0100675 print $c <<EOT1, Git::prefix_lines("GIT: ", __ <<EOT2), <<EOT3;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100676From $tpl_sender # This line is ignored.
Vasco Almeida70aedfb2016-12-14 11:54:38 -0100677EOT1
678Lines beginning in "GIT:" will be removed.
679Consider including an overall diffstat or table of contents
680for the patch you are writing.
681
682Clear the body content if you don't wish to send a summary.
683EOT2
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100684From: $tpl_sender
685Subject: $tpl_subject
686In-Reply-To: $tpl_reply_to
687
Vasco Almeida70aedfb2016-12-14 11:54:38 -0100688EOT3
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100689 for my $f (@files) {
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000690 print $c get_patch_subject($f);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100691 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000692 close $c;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100693
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100694 if ($annotate) {
695 do_edit($compose_filename, @files);
696 } else {
697 do_edit($compose_filename);
698 }
699
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000700 open my $c2, ">", $compose_filename . ".final"
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100701 or die sprintf(__("Failed to open %s.final: %s"), $compose_filename, $!);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100702
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000703 open $c, "<", $compose_filename
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100704 or die sprintf(__("Failed to open %s: %s"), $compose_filename, $!);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100705
706 my $need_8bit_cte = file_has_nonascii($compose_filename);
707 my $in_body = 0;
708 my $summary_empty = 1;
Krzysztof Mazur4a47a4d2012-10-22 14:41:48 +0200709 if (!defined $compose_encoding) {
710 $compose_encoding = "UTF-8";
711 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000712 while(<$c>) {
Michael Witten40e6e8a2009-04-13 13:23:50 -0500713 next if m/^GIT:/;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100714 if ($in_body) {
715 $summary_empty = 0 unless (/^\n$/);
716 } elsif (/^\n$/) {
717 $in_body = 1;
718 if ($need_8bit_cte) {
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000719 print $c2 "MIME-Version: 1.0\n",
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100720 "Content-Type: text/plain; ",
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200721 "charset=$compose_encoding\n",
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100722 "Content-Transfer-Encoding: 8bit\n";
723 }
724 } elsif (/^MIME-Version:/i) {
725 $need_8bit_cte = 0;
726 } elsif (/^Subject:\s*(.+)\s*$/i) {
727 $initial_subject = $1;
728 my $subject = $initial_subject;
729 $_ = "Subject: " .
Krzysztof Mazurce547802012-10-24 23:08:26 +0200730 quote_subject($subject, $compose_encoding) .
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100731 "\n";
732 } elsif (/^In-Reply-To:\s*(.+)\s*$/i) {
733 $initial_reply_to = $1;
734 next;
735 } elsif (/^From:\s*(.+)\s*$/i) {
736 $sender = $1;
737 next;
738 } elsif (/^(?:To|Cc|Bcc):/i) {
Vasco Almeida46493102016-12-14 11:54:36 -0100739 print __("To/Cc/Bcc fields are not interpreted yet, they have been ignored\n");
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100740 next;
741 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000742 print $c2 $_;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100743 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000744 close $c;
745 close $c2;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100746
747 if ($summary_empty) {
Vasco Almeida46493102016-12-14 11:54:36 -0100748 print __("Summary email is empty, skipping it\n");
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100749 $compose = -1;
750 }
751} elsif ($annotate) {
752 do_edit(@files);
753}
754
Jay Soffian6e182512009-03-28 21:39:10 -0400755sub ask {
756 my ($prompt, %arg) = @_;
Jay Soffian0da43a62009-04-04 23:23:21 -0400757 my $valid_re = $arg{valid_re};
Jay Soffian6e182512009-03-28 21:39:10 -0400758 my $default = $arg{default};
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700759 my $confirm_only = $arg{confirm_only};
Jay Soffian6e182512009-03-28 21:39:10 -0400760 my $resp;
761 my $i = 0;
Jay Soffian5906f542009-03-31 12:22:11 -0400762 return defined $default ? $default : undef
763 unless defined $term->IN and defined fileno($term->IN) and
764 defined $term->OUT and defined fileno($term->OUT);
Jay Soffian6e182512009-03-28 21:39:10 -0400765 while ($i++ < 10) {
766 $resp = $term->readline($prompt);
767 if (!defined $resp) { # EOF
768 print "\n";
769 return defined $default ? $default : undef;
770 }
771 if ($resp eq '' and defined $default) {
772 return $default;
773 }
Jay Soffian0da43a62009-04-04 23:23:21 -0400774 if (!defined $valid_re or $resp =~ /$valid_re/) {
Jay Soffian6e182512009-03-28 21:39:10 -0400775 return $resp;
776 }
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700777 if ($confirm_only) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100778 my $yesno = $term->readline(
779 # TRANSLATORS: please keep [y/N] as is.
780 sprintf(__("Are you sure you want to use <%s> [y/N]? "), $resp));
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700781 if (defined $yesno && $yesno =~ /y/i) {
782 return $resp;
783 }
784 }
Jay Soffian6e182512009-03-28 21:39:10 -0400785 }
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -0700786 return;
Jay Soffian6e182512009-03-28 21:39:10 -0400787}
788
Thomas Rast3cae7e52010-06-17 22:10:39 +0200789my %broken_encoding;
790
Ævar Arnfjörð Bjarmason1d50bfd2010-09-30 13:42:58 +0000791sub file_declares_8bit_cte {
Thomas Rast3cae7e52010-06-17 22:10:39 +0200792 my $fn = shift;
793 open (my $fh, '<', $fn);
794 while (my $line = <$fh>) {
795 last if ($line =~ /^$/);
796 return 1 if ($line =~ /^Content-Transfer-Encoding: .*8bit.*$/);
797 }
798 close $fh;
799 return 0;
800}
801
802foreach my $f (@files) {
803 next unless (body_or_subject_has_nonascii($f)
804 && !file_declares_8bit_cte($f));
805 $broken_encoding{$f} = 1;
806}
807
808if (!defined $auto_8bit_encoding && scalar %broken_encoding) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100809 print __("The following files are 8bit, but do not declare " .
810 "a Content-Transfer-Encoding.\n");
Thomas Rast3cae7e52010-06-17 22:10:39 +0200811 foreach my $f (sort keys %broken_encoding) {
812 print " $f\n";
813 }
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100814 $auto_8bit_encoding = ask(__("Which 8bit encoding should I declare [UTF-8]? "),
Junio C Hamano852a15d2015-02-13 12:20:25 -0800815 valid_re => qr/.{4}/, confirm_only => 1,
Thomas Rast3cae7e52010-06-17 22:10:39 +0200816 default => "UTF-8");
817}
818
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200819if (!$force) {
820 for my $f (@files) {
Ævar Arnfjörð Bjarmason0d290a42010-09-30 13:43:01 +0000821 if (get_patch_subject($f) =~ /\Q*** SUBJECT HERE ***\E/) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100822 die sprintf(__("Refusing to send because the patch\n\t%s\n"
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200823 . "has the template subject '*** SUBJECT HERE ***'. "
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100824 . "Pass --force if you really want to send.\n"), $f);
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200825 }
826 }
827}
828
Remi Lespinetc46e27a2015-06-30 14:16:47 +0200829if (defined $sender) {
Remi Lespinetfa5b1aa2015-06-30 14:16:51 +0200830 $sender =~ s/^\s+|\s+$//g;
Remi Lespinetc46e27a2015-06-30 14:16:47 +0200831 ($sender) = expand_aliases($sender);
832} else {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100833 $sender = $repoauthor || $repocommitter || '';
Ryan Anderson83b24432005-07-31 04:17:25 -0400834}
835
Michael S. Tsirkinda187592013-06-05 21:11:00 +0300836# $sender could be an already sanitized address
837# (e.g. sendemail.from could be manually sanitized by user).
838# But it's a no-op to run sanitize_address on an already sanitized address.
839$sender = sanitize_address($sender);
840
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100841my $to_whom = __("To whom should the emails be sent (if anyone)?");
Felipe Contreras8cac13d2012-11-24 12:16:19 +0100842my $prompting = 0;
Junio C Hamano8796ff72010-10-26 22:02:03 -0700843if (!@initial_to && !defined $to_cmd) {
Junio C Hamano0d6b21e2016-04-24 12:31:44 -0700844 my $to = ask("$to_whom ",
Stephen Boyd61837492012-09-06 11:31:11 -0700845 default => "",
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700846 valid_re => qr/\@.*\./, confirm_only => 1);
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700847 push @initial_to, parse_address_line($to) if defined $to; # sanitized/validated later
Ryan Anderson1f038a02005-09-05 01:13:07 -0400848 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -0400849}
850
Eric Wong994d6c62006-05-14 19:13:44 -0700851sub expand_aliases {
Jeff King302e04e2009-07-23 07:09:29 -0400852 return map { expand_one_alias($_) } @_;
853}
854
855my %EXPANDED_ALIASES;
856sub expand_one_alias {
857 my $alias = shift;
858 if ($EXPANDED_ALIASES{$alias}) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100859 die sprintf(__("fatal: alias '%s' expands to itself\n"), $alias);
Jeff King302e04e2009-07-23 07:09:29 -0400860 }
861 local $EXPANDED_ALIASES{$alias} = 1;
862 return $aliases{$alias} ? expand_aliases(@{$aliases{$alias}}) : $alias;
Eric Wong994d6c62006-05-14 19:13:44 -0700863}
864
Remi Lespinetb5e112d2015-06-30 14:16:45 +0200865@initial_to = process_address_list(@initial_to);
866@initial_cc = process_address_list(@initial_cc);
867@bcclist = process_address_list(@bcclist);
Eric Wong994d6c62006-05-14 19:13:44 -0700868
Adam Roben5483c712007-06-27 20:59:37 -0700869if ($thread && !defined $initial_reply_to && $prompting) {
Jay Soffian6e182512009-03-28 21:39:10 -0400870 $initial_reply_to = ask(
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100871 __("Message-ID to be used as In-Reply-To for the first email (if any)? "),
Stephen Boyd61837492012-09-06 11:31:11 -0700872 default => "",
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700873 valid_re => qr/\@.*\./, confirm_only => 1);
Ryan Anderson83b24432005-07-31 04:17:25 -0400874}
Jay Soffian1ca3d6e2008-02-20 00:55:07 -0500875if (defined $initial_reply_to) {
Jay Soffian0fb7fc72008-02-21 19:16:04 -0500876 $initial_reply_to =~ s/^\s*<?//;
877 $initial_reply_to =~ s/>?\s*$//;
878 $initial_reply_to = "<$initial_reply_to>" if $initial_reply_to ne '';
Junio C Hamanoace9c2a2007-12-10 21:44:42 -0800879}
Mike Hommeyace72082007-12-09 18:17:28 +0100880
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900881if (!defined $smtp_server) {
Eric Wongaca7ad72006-05-15 02:34:44 -0700882 foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) {
883 if (-x $_) {
884 $smtp_server = $_;
885 last;
886 }
887 }
888 $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
Ryan Anderson3342d852005-07-31 20:04:24 -0400889}
890
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500891if ($compose && $compose > 0) {
892 @files = ($compose_filename . ".final", @files);
Ryan Anderson1f038a02005-09-05 01:13:07 -0400893}
894
Ryan Anderson83b24432005-07-31 04:17:25 -0400895# Variables we set as part of the loop over files
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500896our ($message_id, %mail, $subject, $reply_to, $references, $message,
Jay Soffiandc1460a2009-03-31 12:22:12 -0400897 $needs_confirm, $message_num, $ask_default);
Ryan Anderson83b24432005-07-31 04:17:25 -0400898
Eric Wong567ffeb2006-03-25 16:47:12 -0800899sub extract_valid_address {
900 my $address = shift;
Ævar Arnfjörð Bjarmason35b6ab92010-09-30 19:03:31 +0000901 my $local_part_regexp = qr/[^<>"\s@]+/;
902 my $domain_regexp = qr/[^.<>"\s@]+(?:\.[^.<>"\s@]+)+/;
Eric Wongdb3106b2006-05-15 02:41:01 -0700903
904 # check for a local address:
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700905 return $address if ($address =~ /^($local_part_regexp)$/);
Eric Wongdb3106b2006-05-15 02:41:01 -0700906
Uwe Kleine-König155197e2007-08-09 15:27:57 +0200907 $address =~ s/^\s*<(.*)>\s*$/$1/;
Eric Wong567ffeb2006-03-25 16:47:12 -0800908 if ($have_email_valid) {
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700909 return scalar Email::Valid->address($address);
Eric Wong567ffeb2006-03-25 16:47:12 -0800910 }
Krzysztof Mazur95c0d4b2012-11-22 19:12:09 +0100911
912 # less robust/correct than the monster regexp in Email::Valid,
913 # but still does a 99% job, and one less dependency
914 return $1 if $address =~ /($local_part_regexp\@$domain_regexp)/;
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -0700915 return;
Eric Wong567ffeb2006-03-25 16:47:12 -0800916}
Ryan Anderson83b24432005-07-31 04:17:25 -0400917
Krzysztof Mazure4312252012-11-22 19:12:10 +0100918sub extract_valid_address_or_die {
919 my $address = shift;
920 $address = extract_valid_address($address);
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100921 die sprintf(__("error: unable to extract a valid address from: %s\n"), $address)
Krzysztof Mazure4312252012-11-22 19:12:10 +0100922 if !$address;
923 return $address;
924}
925
926sub validate_address {
927 my $address = shift;
Krzysztof Mazurd0e98102012-11-22 19:12:12 +0100928 while (!extract_valid_address($address)) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -0100929 printf STDERR __("error: unable to extract a valid address from: %s\n"), $address;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -0100930 # TRANSLATORS: Make sure to include [q] [d] [e] in your
931 # translation. The program will only accept English input
932 # at this point.
933 $_ = ask(__("What to do with this address? ([q]uit|[d]rop|[e]dit): "),
Krzysztof Mazurd0e98102012-11-22 19:12:12 +0100934 valid_re => qr/^(?:quit|q|drop|d|edit|e)/i,
Krzysztof Mazur5c80afe2012-11-22 19:12:11 +0100935 default => 'q');
936 if (/^d/i) {
937 return undef;
938 } elsif (/^q/i) {
939 cleanup_compose_files();
940 exit(0);
941 }
Junio C Hamano0d6b21e2016-04-24 12:31:44 -0700942 $address = ask("$to_whom ",
Krzysztof Mazurd0e98102012-11-22 19:12:12 +0100943 default => "",
944 valid_re => qr/\@.*\./, confirm_only => 1);
Krzysztof Mazure4312252012-11-22 19:12:10 +0100945 }
946 return $address;
947}
948
949sub validate_address_list {
950 return (grep { defined $_ }
951 map { validate_address($_) } @_);
Ryan Anderson83b24432005-07-31 04:17:25 -0400952}
953
954# Usually don't need to change anything below here.
955
956# we make a "fake" message id by taking the current number
957# of seconds since the beginning of Unix time and tacking on
958# a random number to the end, in case we are called quicker than
959# 1 second since the last time we were called.
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400960
961# We'll setup a template for the message id, using the "from" address:
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400962
Junio C Hamanobe510cf2007-09-17 21:18:20 -0700963my ($message_id_stamp, $message_id_serial);
Brian Gernhardt68ce9332010-04-10 10:53:53 -0400964sub make_message_id {
Junio C Hamanobe510cf2007-09-17 21:18:20 -0700965 my $uniq;
966 if (!defined $message_id_stamp) {
Eric Wongf916ab02016-04-06 20:07:14 +0000967 $message_id_stamp = strftime("%Y%m%d%H%M%S.$$", gmtime(time));
Junio C Hamanobe510cf2007-09-17 21:18:20 -0700968 $message_id_serial = 0;
969 }
970 $message_id_serial++;
971 $uniq = "$message_id_stamp-$message_id_serial";
972
Junio C Hamanoaeb59322007-06-20 13:47:34 -0700973 my $du_part;
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200974 for ($sender, $repocommitter, $repoauthor) {
975 $du_part = extract_valid_address(sanitize_address($_));
976 last if (defined $du_part and $du_part ne '');
Junio C Hamanoaeb59322007-06-20 13:47:34 -0700977 }
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200978 if (not defined $du_part or $du_part eq '') {
Ævar Arnfjörð Bjarmason529dd382010-09-30 13:43:08 +0000979 require Sys::Hostname;
Junio C Hamanoaeb59322007-06-20 13:47:34 -0700980 $du_part = 'user@' . Sys::Hostname::hostname();
981 }
Eric Wongf916ab02016-04-06 20:07:14 +0000982 my $message_id_template = "<%s-%s>";
Junio C Hamanobe510cf2007-09-17 21:18:20 -0700983 $message_id = sprintf($message_id_template, $uniq, $du_part);
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400984 #print "new message id = $message_id\n"; # Was useful for debugging
Ryan Anderson83b24432005-07-31 04:17:25 -0400985}
986
987
988
Eric Wonga5370b12006-03-25 03:01:01 -0800989$time = time - scalar $#files;
Ryan Anderson83b24432005-07-31 04:17:25 -0400990
Jürgen Rühle374c5902007-01-10 13:36:39 -0800991sub unquote_rfc2047 {
992 local ($_) = @_;
Роман Донченко11f70a72014-12-14 18:59:46 +0300993 my $charset;
Роман Донченкоab47e2a2014-12-14 18:59:47 +0300994 my $sep = qr/[ \t]+/;
995 s{$re_encoded_word(?:$sep$re_encoded_word)*}{
996 my @words = split $sep, $&;
997 foreach (@words) {
998 m/$re_encoded_word/;
999 $charset = $1;
1000 my $encoding = $2;
1001 my $text = $3;
1002 if ($encoding eq 'q' || $encoding eq 'Q') {
1003 $_ = $text;
1004 s/_/ /g;
1005 s/=([0-9A-F]{2})/chr(hex($1))/egi;
1006 } else {
1007 # other encodings not supported yet
1008 }
Роман Донченко11f70a72014-12-14 18:59:46 +03001009 }
Роман Донченкоab47e2a2014-12-14 18:59:47 +03001010 join '', @words;
Thomas Rastb622d4d2012-07-30 21:25:40 +02001011 }eg;
Роман Донченко11f70a72014-12-14 18:59:46 +03001012 return wantarray ? ($_, $charset) : $_;
Jürgen Rühle374c5902007-01-10 13:36:39 -08001013}
1014
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001015sub quote_rfc2047 {
1016 local $_ = shift;
Brandon Caseyd1fff6f2009-06-06 20:12:31 -05001017 my $encoding = shift || 'UTF-8';
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001018 s/([^-a-zA-Z0-9!*+\/])/sprintf("=%02X", ord($1))/eg;
1019 s/(.*)/=\?$encoding\?q\?$1\?=/;
1020 return $_;
1021}
1022
Brandon Caseya3a82622009-06-07 19:25:58 -05001023sub is_rfc2047_quoted {
1024 my $s = shift;
Brandon Caseya3a82622009-06-07 19:25:58 -05001025 length($s) <= 75 &&
Роман Донченко11f70a72014-12-14 18:59:46 +03001026 $s =~ m/^(?:"[[:ascii:]]*"|$re_encoded_word)$/o;
Brandon Caseya3a82622009-06-07 19:25:58 -05001027}
1028
Krzysztof Mazurce547802012-10-24 23:08:26 +02001029sub subject_needs_rfc2047_quoting {
1030 my $s = shift;
1031
Krzysztof Mazurce1459f2012-10-24 23:28:29 +02001032 return ($s =~ /[^[:ascii:]]/) || ($s =~ /=\?/);
Krzysztof Mazurce547802012-10-24 23:08:26 +02001033}
1034
1035sub quote_subject {
1036 local $subject = shift;
1037 my $encoding = shift || 'UTF-8';
1038
1039 if (subject_needs_rfc2047_quoting($subject)) {
1040 return quote_rfc2047($subject, $encoding);
1041 }
1042 return $subject;
1043}
1044
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001045# use the simplest quoting being able to handle the recipient
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001046sub sanitize_address {
Robin H. Johnson732263d2007-04-25 19:37:19 -07001047 my ($recipient) = @_;
Krzysztof Mazur831a4882012-11-22 19:12:08 +01001048
1049 # remove garbage after email address
1050 $recipient =~ s/(.*>).*$/$1/;
1051
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001052 my ($recipient_name, $recipient_addr) = ($recipient =~ /^(.*?)\s*(<.*)/);
1053
1054 if (not $recipient_name) {
Ævar Arnfjörð Bjarmasonff483892010-09-30 13:43:02 +00001055 return $recipient;
Robin H. Johnson732263d2007-04-25 19:37:19 -07001056 }
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001057
1058 # if recipient_name is already quoted, do nothing
Brandon Caseya3a82622009-06-07 19:25:58 -05001059 if (is_rfc2047_quoted($recipient_name)) {
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001060 return $recipient;
1061 }
1062
Remi Lespinet1fe97032015-06-30 14:16:49 +02001063 # remove non-escaped quotes
1064 $recipient_name =~ s/(^|[^\\])"/$1/g;
1065
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001066 # rfc2047 is needed if a non-ascii char is included
1067 if ($recipient_name =~ /[^[:ascii:]]/) {
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001068 $recipient_name = quote_rfc2047($recipient_name);
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001069 }
1070
1071 # double quotes are needed if specials or CTLs are included
1072 elsif ($recipient_name =~ /[][()<>@,;:\\".\000-\037\177]/) {
Remi Lespinet1fe97032015-06-30 14:16:49 +02001073 $recipient_name =~ s/([\\\r])/\\$1/g;
Ævar Arnfjörð Bjarmasond5c7d692010-09-30 13:43:03 +00001074 $recipient_name = qq["$recipient_name"];
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001075 }
1076
1077 return "$recipient_name $recipient_addr";
1078
Robin H. Johnson732263d2007-04-25 19:37:19 -07001079}
1080
Krzysztof Mazure4312252012-11-22 19:12:10 +01001081sub sanitize_address_list {
1082 return (map { sanitize_address($_) } @_);
1083}
1084
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001085sub process_address_list {
Remi Lespinetb1c8a112015-06-30 14:16:50 +02001086 my @addr_list = map { parse_address_line($_) } @_;
1087 @addr_list = expand_aliases(@addr_list);
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001088 @addr_list = sanitize_address_list(@addr_list);
1089 @addr_list = validate_address_list(@addr_list);
1090 return @addr_list;
1091}
1092
Jari Aalto134550f2010-03-14 17:16:45 +02001093# Returns the local Fully Qualified Domain Name (FQDN) if available.
1094#
1095# Tightly configured MTAa require that a caller sends a real DNS
1096# domain name that corresponds the IP address in the HELO/EHLO
1097# handshake. This is used to verify the connection and prevent
1098# spammers from trying to hide their identity. If the DNS and IP don't
1099# match, the receiveing MTA may deny the connection.
1100#
1101# Here is a deny example of Net::SMTP with the default "localhost.localdomain"
1102#
1103# Net::SMTP=GLOB(0x267ec28)>>> EHLO localhost.localdomain
1104# Net::SMTP=GLOB(0x267ec28)<<< 550 EHLO argument does not match calling host
1105#
1106# This maildomain*() code is based on ideas in Perl library Test::Reporter
1107# /usr/share/perl5/Test/Reporter/Mail/Util.pm ==> sub _maildomain ()
1108
Brian Gernhardt59a86302010-04-10 10:53:54 -04001109sub valid_fqdn {
1110 my $domain = shift;
Brandon Casey61ef5e92010-09-26 22:18:01 -05001111 return defined $domain && !($^O eq 'darwin' && $domain =~ /\.local$/) && $domain =~ /\./;
Brian Gernhardt59a86302010-04-10 10:53:54 -04001112}
1113
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001114sub maildomain_net {
Jari Aalto134550f2010-03-14 17:16:45 +02001115 my $maildomain;
1116
1117 if (eval { require Net::Domain; 1 }) {
1118 my $domain = Net::Domain::domainname();
Brian Gernhardt59a86302010-04-10 10:53:54 -04001119 $maildomain = $domain if valid_fqdn($domain);
Jari Aalto134550f2010-03-14 17:16:45 +02001120 }
1121
1122 return $maildomain;
1123}
1124
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001125sub maildomain_mta {
Jari Aalto134550f2010-03-14 17:16:45 +02001126 my $maildomain;
1127
1128 if (eval { require Net::SMTP; 1 }) {
1129 for my $host (qw(mailhost localhost)) {
1130 my $smtp = Net::SMTP->new($host);
1131 if (defined $smtp) {
1132 my $domain = $smtp->domain;
1133 $smtp->quit;
1134
Brian Gernhardt59a86302010-04-10 10:53:54 -04001135 $maildomain = $domain if valid_fqdn($domain);
Jari Aalto134550f2010-03-14 17:16:45 +02001136
1137 last if $maildomain;
1138 }
1139 }
1140 }
1141
1142 return $maildomain;
1143}
1144
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001145sub maildomain {
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001146 return maildomain_net() || maildomain_mta() || 'localhost.localdomain';
Jari Aalto134550f2010-03-14 17:16:45 +02001147}
1148
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001149sub smtp_host_string {
1150 if (defined $smtp_server_port) {
1151 return "$smtp_server:$smtp_server_port";
1152 } else {
1153 return $smtp_server;
1154 }
1155}
1156
1157# Returns 1 if authentication succeeded or was not necessary
1158# (smtp_user was not specified), and 0 otherwise.
1159
1160sub smtp_auth_maybe {
1161 if (!defined $smtp_authuser || $auth) {
1162 return 1;
1163 }
1164
1165 # Workaround AUTH PLAIN/LOGIN interaction defect
1166 # with Authen::SASL::Cyrus
1167 eval {
1168 require Authen::SASL;
1169 Authen::SASL->import(qw(Perl));
1170 };
1171
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001172 # Check mechanism naming as defined in:
1173 # https://tools.ietf.org/html/rfc4422#page-8
Brian Norris904f6e72015-09-18 15:12:50 -07001174 if ($smtp_auth && $smtp_auth !~ /^(\b[A-Z0-9-_]{1,20}\s*)*$/) {
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001175 die "invalid smtp auth: '${smtp_auth}'";
1176 }
1177
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001178 # TODO: Authentication may fail not because credentials were
1179 # invalid but due to other reasons, in which we should not
1180 # reject credentials.
1181 $auth = Git::credential({
1182 'protocol' => 'smtp',
1183 'host' => smtp_host_string(),
1184 'username' => $smtp_authuser,
1185 # if there's no password, "git credential fill" will
1186 # give us one, otherwise it'll just pass this one.
1187 'password' => $smtp_authpass
1188 }, sub {
1189 my $cred = shift;
Jan Viktorin0f2e68b2015-08-12 01:39:44 +02001190
1191 if ($smtp_auth) {
1192 my $sasl = Authen::SASL->new(
1193 mechanism => $smtp_auth,
1194 callback => {
1195 user => $cred->{'username'},
1196 pass => $cred->{'password'},
1197 authname => $cred->{'username'},
1198 }
1199 );
1200
1201 return !!$smtp->auth($sasl);
1202 }
1203
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001204 return !!$smtp->auth($cred->{'username'}, $cred->{'password'});
1205 });
1206
1207 return $auth;
1208}
1209
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001210sub ssl_verify_params {
1211 eval {
1212 require IO::Socket::SSL;
1213 IO::Socket::SSL->import(qw/SSL_VERIFY_PEER SSL_VERIFY_NONE/);
1214 };
1215 if ($@) {
1216 print STDERR "Not using SSL_VERIFY_PEER due to out-of-date IO::Socket::SSL.\n";
1217 return;
1218 }
1219
1220 if (!defined $smtp_ssl_cert_path) {
Ruben Kerkhof01645b72014-01-15 21:31:11 +04001221 # use the OpenSSL defaults
1222 return (SSL_verify_mode => SSL_VERIFY_PEER());
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001223 }
1224
1225 if ($smtp_ssl_cert_path eq "") {
1226 return (SSL_verify_mode => SSL_VERIFY_NONE());
1227 } elsif (-d $smtp_ssl_cert_path) {
1228 return (SSL_verify_mode => SSL_VERIFY_PEER(),
1229 SSL_ca_path => $smtp_ssl_cert_path);
1230 } elsif (-f $smtp_ssl_cert_path) {
1231 return (SSL_verify_mode => SSL_VERIFY_PEER(),
1232 SSL_ca_file => $smtp_ssl_cert_path);
1233 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001234 die sprintf(__("CA path \"%s\" does not exist"), $smtp_ssl_cert_path);
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001235 }
1236}
1237
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001238sub file_name_is_absolute {
1239 my ($path) = @_;
1240
1241 # msys does not grok DOS drive-prefixes
1242 if ($^O eq 'msys') {
Junio C Hamanof24ecf52014-04-23 09:37:38 -07001243 return ($path =~ m#^/# || $path =~ m#^[a-zA-Z]\:#)
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001244 }
1245
1246 require File::Spec::Functions;
1247 return File::Spec::Functions::file_name_is_absolute($path);
1248}
1249
Michael Witten15da1082009-04-13 13:23:51 -05001250# Returns 1 if the message was sent, and 0 otherwise.
Markus Heidelberga1b5b372009-06-12 12:51:42 +02001251# In actuality, the whole program dies when there
Michael Witten15da1082009-04-13 13:23:51 -05001252# is an error sending a message.
1253
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001254sub send_message {
Eric Wong4bc87a22006-03-25 17:20:48 -08001255 my @recipients = unique_email_list(@to);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001256 @cc = (grep { my $cc = extract_valid_address_or_die($_);
Joe Perches83acaae2010-11-20 15:06:05 -08001257 not grep { $cc eq $_ || $_ =~ /<\Q${cc}\E>$/ } @recipients
Ask Bjørn Hansen7ac17522007-11-19 03:00:26 -08001258 }
Ask Bjørn Hansen7ac17522007-11-19 03:00:26 -08001259 @cc);
Eric Wong4bc87a22006-03-25 17:20:48 -08001260 my $to = join (",\n\t", @recipients);
Ryan Anderson58063242006-05-29 12:30:13 -07001261 @recipients = unique_email_list(@recipients,@cc,@bcclist);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001262 @recipients = (map { extract_valid_address_or_die($_) } @recipients);
Jakub Narebski6bdca892006-07-07 20:57:55 +02001263 my $date = format_2822_time($time++);
Martin Langhoffe923eff2006-05-03 09:44:36 +12001264 my $gitversion = '@@GIT_VERSION@@';
1265 if ($gitversion =~ m/..GIT_VERSION../) {
Petr Baudis3cb8caf2006-07-03 22:47:58 +02001266 $gitversion = Git::version();
Martin Langhoffe923eff2006-05-03 09:44:36 +12001267 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001268
Joe Perches02461e02009-10-08 10:03:26 -07001269 my $cc = join(",\n\t", unique_email_list(@cc));
Junio C Hamanof06a6a42007-04-16 16:51:47 -07001270 my $ccline = "";
1271 if ($cc ne '') {
1272 $ccline = "\nCc: $cc";
1273 }
Jeff King4f3d3702007-12-17 15:51:34 -05001274 make_message_id() unless defined($message_id);
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001275
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001276 my $header = "From: $sender
Junio C Hamanof06a6a42007-04-16 16:51:47 -07001277To: $to${ccline}
Eric Wong4bc87a22006-03-25 17:20:48 -08001278Subject: $subject
Eric Wong4bc87a22006-03-25 17:20:48 -08001279Date: $date
1280Message-Id: $message_id
Eric Wong4bc87a22006-03-25 17:20:48 -08001281";
Luis Henriquesac1596a2014-03-24 21:38:27 +00001282 if ($use_xmailer) {
1283 $header .= "X-Mailer: git-send-email $gitversion\n";
1284 }
Thomas Rast3e0c4ff2009-03-01 23:45:41 +01001285 if ($reply_to) {
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001286
1287 $header .= "In-Reply-To: $reply_to\n";
1288 $header .= "References: $references\n";
1289 }
Junio C Hamanoce91c2f2006-10-05 16:36:49 -07001290 if (@xh) {
1291 $header .= join("\n", @xh) . "\n";
1292 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001293
Robin H. Johnsonc38f0242007-04-25 19:37:20 -07001294 my @sendmail_parameters = ('-i', @recipients);
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001295 my $raw_from = $sender;
Felipe Contrerasc89e3242009-11-26 21:04:29 +02001296 if (defined $envelope_sender && $envelope_sender ne "auto") {
1297 $raw_from = $envelope_sender;
1298 }
Robin H. Johnsonf073a592007-04-25 19:37:22 -07001299 $raw_from = extract_valid_address($raw_from);
1300 unshift (@sendmail_parameters,
1301 '-f', $raw_from) if(defined $envelope_sender);
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001302
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001303 if ($needs_confirm && !$dry_run) {
1304 print "\n$header\n";
1305 if ($needs_confirm eq "inform") {
1306 $confirm_unconfigured = 0; # squelch this message for the rest of this run
Jay Soffian6e182512009-03-28 21:39:10 -04001307 $ask_default = "y"; # assume yes on EOF since user hasn't explicitly asked for confirmation
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001308 print __ <<EOF ;
1309 The Cc list above has been expanded by additional
1310 addresses found in the patch commit message. By default
1311 send-email prompts before sending whenever this occurs.
1312 This behavior is controlled by the sendemail.confirm
1313 configuration setting.
1314
1315 For additional information, run 'git send-email --help'.
1316 To retain the current behavior, but squelch this message,
1317 run 'git config --global sendemail.confirm auto'.
1318
1319EOF
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001320 }
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001321 # TRANSLATORS: Make sure to include [y] [n] [q] [a] in your
1322 # translation. The program will only accept English input
1323 # at this point.
1324 $_ = ask(__("Send this email? ([y]es|[n]o|[q]uit|[a]ll): "),
Jay Soffian6e182512009-03-28 21:39:10 -04001325 valid_re => qr/^(?:yes|y|no|n|quit|q|all|a)/i,
1326 default => $ask_default);
Vasco Almeida46493102016-12-14 11:54:36 -01001327 die __("Send this email reply required") unless defined $_;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001328 if (/^n/i) {
Michael Witten15da1082009-04-13 13:23:51 -05001329 return 0;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001330 } elsif (/^q/i) {
1331 cleanup_compose_files();
1332 exit(0);
1333 } elsif (/^a/i) {
1334 $confirm = 'never';
1335 }
1336 }
1337
Pascal Obry052fbea2010-09-06 20:12:11 +02001338 unshift (@sendmail_parameters, @smtp_server_options);
1339
Matthew Wilcox61302592006-10-10 08:58:23 -06001340 if ($dry_run) {
1341 # We don't want to send the email.
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001342 } elsif (file_name_is_absolute($smtp_server)) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001343 my $pid = open my $sm, '|-';
1344 defined $pid or die $!;
1345 if (!$pid) {
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001346 exec($smtp_server, @sendmail_parameters) or die $!;
Eric Wongaca7ad72006-05-15 02:34:44 -07001347 }
1348 print $sm "$header\n$message";
Ævar Arnfjörð Bjarmason5e2c2ab2010-09-30 13:43:07 +00001349 close $sm or die $!;
Eric Wongaca7ad72006-05-15 02:34:44 -07001350 } else {
Junio C Hamano44b24762007-09-25 17:27:54 -07001351
1352 if (!defined $smtp_server) {
Vasco Almeida46493102016-12-14 11:54:36 -01001353 die __("The required SMTP server is not properly defined.")
Junio C Hamano44b24762007-09-25 17:27:54 -07001354 }
1355
Thomas Rastf6bebd12008-06-25 21:42:43 +02001356 if ($smtp_encryption eq 'ssl') {
Junio C Hamano44b24762007-09-25 17:27:54 -07001357 $smtp_server_port ||= 465; # ssmtp
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001358 require Net::SMTP::SSL;
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001359 $smtp_domain ||= maildomain();
Thomas Rast5508f3e2013-12-01 23:48:43 +01001360 require IO::Socket::SSL;
John Keeping9d605242015-12-03 21:47:18 +00001361
1362 # Suppress "variable accessed once" warning.
1363 {
1364 no warnings 'once';
1365 $IO::Socket::SSL::DEBUG = 1;
1366 }
1367
Thomas Rast5508f3e2013-12-01 23:48:43 +01001368 # Net::SMTP::SSL->new() does not forward any SSL options
1369 IO::Socket::SSL::set_client_defaults(
1370 ssl_verify_params());
Jari Aalto134550f2010-03-14 17:16:45 +02001371 $smtp ||= Net::SMTP::SSL->new($smtp_server,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001372 Hello => $smtp_domain,
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001373 Port => $smtp_server_port,
Thomas Rast5508f3e2013-12-01 23:48:43 +01001374 Debug => $debug_net_smtp);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001375 }
1376 else {
1377 require Net::SMTP;
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001378 $smtp_domain ||= maildomain();
brian m. carlson1a741bf2013-07-04 22:04:52 +00001379 $smtp_server_port ||= 25;
1380 $smtp ||= Net::SMTP->new($smtp_server,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001381 Hello => $smtp_domain,
brian m. carlson1a741bf2013-07-04 22:04:52 +00001382 Debug => $debug_net_smtp,
1383 Port => $smtp_server_port);
Yakov Lernerfb3650e2009-09-25 15:10:21 -07001384 if ($smtp_encryption eq 'tls' && $smtp) {
Thomas Rastf6bebd12008-06-25 21:42:43 +02001385 require Net::SMTP::SSL;
1386 $smtp->command('STARTTLS');
1387 $smtp->response();
1388 if ($smtp->code == 220) {
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001389 $smtp = Net::SMTP::SSL->start_SSL($smtp,
1390 ssl_verify_params())
Brian M. Carlson6cb0c882013-09-08 20:54:34 +00001391 or die "STARTTLS failed! ".IO::Socket::SSL::errstr();
Thomas Rast6cbf8b02008-07-03 00:11:31 +02001392 $smtp_encryption = '';
Robert Shearman9d1ccf52008-07-09 22:39:40 +01001393 # Send EHLO again to receive fresh
1394 # supported commands
Matthew Daley155b9402011-10-15 04:44:52 -04001395 $smtp->hello($smtp_domain);
Thomas Rastf6bebd12008-06-25 21:42:43 +02001396 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001397 die sprintf(__("Server does not support STARTTLS! %s"), $smtp->message);
Thomas Rastf6bebd12008-06-25 21:42:43 +02001398 }
1399 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001400 }
Junio C Hamano44b24762007-09-25 17:27:54 -07001401
1402 if (!$smtp) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001403 die __("Unable to initialize SMTP properly. Check config and use --smtp-debug."),
1404 " VALUES: server=$smtp_server ",
Jari Aaltoe5afb3a2010-03-14 17:15:33 +02001405 "encryption=$smtp_encryption ",
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001406 "hello=$smtp_domain",
Sylvain Rabota1dd7e12011-04-29 20:23:24 +02001407 defined $smtp_server_port ? " port=$smtp_server_port" : "";
Junio C Hamano44b24762007-09-25 17:27:54 -07001408 }
1409
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001410 smtp_auth_maybe or die $smtp->message;
Michael Witten2363d742008-02-03 19:53:56 -05001411
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001412 $smtp->mail( $raw_from ) or die $smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001413 $smtp->to( @recipients ) or die $smtp->message;
1414 $smtp->data or die $smtp->message;
Stefan Agnerf60c4832015-09-30 09:26:09 +02001415 $smtp->datasend("$header\n") or die $smtp->message;
1416 my @lines = split /^/, $message;
1417 foreach my $line (@lines) {
1418 $smtp->datasend("$line") or die $smtp->message;
1419 }
Eric Wongaca7ad72006-05-15 02:34:44 -07001420 $smtp->dataend() or die $smtp->message;
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001421 $smtp->code =~ /250|200/ or die sprintf(__("Failed to send %s\n"), $subject).$smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001422 }
Ryan Anderson27184352006-02-05 20:13:52 -05001423 if ($quiet) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001424 printf($dry_run ? __("Dry-Sent %s\n") : __("Sent %s\n"), $subject);
Ryan Anderson27184352006-02-05 20:13:52 -05001425 } else {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001426 print($dry_run ? __("Dry-OK. Log says:\n") : __("OK. Log says:\n"));
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001427 if (!file_name_is_absolute($smtp_server)) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001428 print "Server: $smtp_server\n";
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001429 print "MAIL FROM:<$raw_from>\n";
Joe Perches02461e02009-10-08 10:03:26 -07001430 foreach my $entry (@recipients) {
1431 print "RCPT TO:<$entry>\n";
1432 }
Eric Wongaca7ad72006-05-15 02:34:44 -07001433 } else {
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001434 print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001435 }
David D. Kilzerb7f30e02007-11-18 20:14:55 -08001436 print $header, "\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001437 if ($smtp) {
Vasco Almeida46493102016-12-14 11:54:36 -01001438 print __("Result: "), $smtp->code, ' ',
Eric Wongaca7ad72006-05-15 02:34:44 -07001439 ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
1440 } else {
Vasco Almeida46493102016-12-14 11:54:36 -01001441 print __("Result: OK\n");
Eric Wongaca7ad72006-05-15 02:34:44 -07001442 }
Ryan Anderson30d08b32006-02-02 11:56:06 -05001443 }
Michael Witten15da1082009-04-13 13:23:51 -05001444
1445 return 1;
Ryan Anderson83b24432005-07-31 04:17:25 -04001446}
1447
Ryan Anderson83b24432005-07-31 04:17:25 -04001448$reply_to = $initial_reply_to;
Junio C Hamano2186d562006-05-29 23:53:13 -07001449$references = $initial_reply_to || '';
Ryan Anderson83b24432005-07-31 04:17:25 -04001450$subject = $initial_subject;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001451$message_num = 0;
Ryan Anderson83b24432005-07-31 04:17:25 -04001452
1453foreach my $t (@files) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001454 open my $fh, "<", $t or die sprintf(__("can't open file %s"), $t);
Ryan Anderson83b24432005-07-31 04:17:25 -04001455
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001456 my $author = undef;
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001457 my $sauthor = undef;
Jeff King8291db62007-11-16 05:49:09 -05001458 my $author_encoding;
1459 my $has_content_type;
1460 my $body_encoding;
Paolo Bonzinibb294562014-11-25 15:00:26 +01001461 my $xfer_encoding;
1462 my $has_mime_version;
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001463 @to = ();
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001464 @cc = ();
Junio C Hamanoce91c2f2006-10-05 16:36:49 -07001465 @xh = ();
Junio C Hamanoe6b09642006-10-07 03:09:05 -07001466 my $input_format = undef;
Jay Soffian50126992009-02-14 23:32:14 -05001467 my @header = ();
Ryan Anderson83b24432005-07-31 04:17:25 -04001468 $message = "";
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001469 $message_num++;
Jay Soffian50126992009-02-14 23:32:14 -05001470 # First unfold multiline header fields
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001471 while(<$fh>) {
Jay Soffian50126992009-02-14 23:32:14 -05001472 last if /^\s*$/;
1473 if (/^\s+\S/ and @header) {
1474 chomp($header[$#header]);
1475 s/^\s+/ /;
1476 $header[$#header] .= $_;
1477 } else {
1478 push(@header, $_);
1479 }
1480 }
1481 # Now parse the header
1482 foreach(@header) {
1483 if (/^From /) {
1484 $input_format = 'mbox';
1485 next;
1486 }
1487 chomp;
1488 if (!defined $input_format && /^[-A-Za-z]+:\s/) {
1489 $input_format = 'mbox';
1490 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001491
Jay Soffian50126992009-02-14 23:32:14 -05001492 if (defined $input_format && $input_format eq 'mbox') {
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001493 if (/^Subject:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001494 $subject = $1;
1495 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001496 elsif (/^From:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001497 ($author, $author_encoding) = unquote_rfc2047($1);
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001498 $sauthor = sanitize_address($author);
Jay Soffian50126992009-02-14 23:32:14 -05001499 next if $suppress_cc{'author'};
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001500 next if $suppress_cc{'self'} and $sauthor eq $sender;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001501 printf(__("(mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001502 $1, $_) unless $quiet;
1503 push @cc, $1;
1504 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001505 elsif (/^To:\s+(.*)$/i) {
Stephen Boyd21802cd2010-09-29 00:26:44 -07001506 foreach my $addr (parse_address_line($1)) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001507 printf(__("(mbox) Adding to: %s from line '%s'\n"),
Stephen Boyd21802cd2010-09-29 00:26:44 -07001508 $addr, $_) unless $quiet;
Krzysztof Mazure4312252012-11-22 19:12:10 +01001509 push @to, $addr;
Stephen Boyd21802cd2010-09-29 00:26:44 -07001510 }
1511 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001512 elsif (/^Cc:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001513 foreach my $addr (parse_address_line($1)) {
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001514 my $qaddr = unquote_rfc2047($addr);
1515 my $saddr = sanitize_address($qaddr);
1516 if ($saddr eq $sender) {
David Brown65648282007-12-25 19:56:29 -08001517 next if ($suppress_cc{'self'});
David Brown65648282007-12-25 19:56:29 -08001518 } else {
1519 next if ($suppress_cc{'cc'});
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001520 }
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001521 printf(__("(mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001522 $addr, $_) unless $quiet;
1523 push @cc, $addr;
Ryan Anderson83b24432005-07-31 04:17:25 -04001524 }
1525 }
Jay Soffian50126992009-02-14 23:32:14 -05001526 elsif (/^Content-type:/i) {
1527 $has_content_type = 1;
1528 if (/charset="?([^ "]+)/) {
1529 $body_encoding = $1;
1530 }
1531 push @xh, $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001532 }
Paolo Bonzinibb294562014-11-25 15:00:26 +01001533 elsif (/^MIME-Version/i) {
1534 $has_mime_version = 1;
1535 push @xh, $_;
1536 }
Jay Soffian50126992009-02-14 23:32:14 -05001537 elsif (/^Message-Id: (.*)/i) {
1538 $message_id = $1;
1539 }
Paolo Bonzinibb294562014-11-25 15:00:26 +01001540 elsif (/^Content-Transfer-Encoding: (.*)/i) {
1541 $xfer_encoding = $1 if not defined $xfer_encoding;
1542 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001543 elsif (!/^Date:\s/i && /^[-A-Za-z]+:\s+\S/) {
Jay Soffian50126992009-02-14 23:32:14 -05001544 push @xh, $_;
1545 }
1546
Ryan Anderson83b24432005-07-31 04:17:25 -04001547 } else {
Jay Soffian50126992009-02-14 23:32:14 -05001548 # In the traditional
1549 # "send lots of email" format,
1550 # line 1 = cc
1551 # line 2 = subject
1552 # So let's support that, too.
1553 $input_format = 'lots';
1554 if (@cc == 0 && !$suppress_cc{'cc'}) {
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001555 printf(__("(non-mbox) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001556 $_, $_) unless $quiet;
1557 push @cc, $_;
1558 } elsif (!defined $subject) {
1559 $subject = $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001560 }
1561 }
1562 }
Jay Soffian50126992009-02-14 23:32:14 -05001563 # Now parse the message body
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001564 while(<$fh>) {
Jay Soffian50126992009-02-14 23:32:14 -05001565 $message .= $_;
Johan Hovold9d334392017-02-20 12:44:06 +01001566 if (/^(Signed-off-by|Cc): ([^>]*>?)/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001567 chomp;
Jay Soffian3531e272009-02-14 23:32:15 -05001568 my ($what, $c) = ($1, $2);
Jay Soffian50126992009-02-14 23:32:14 -05001569 chomp $c;
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001570 my $sc = sanitize_address($c);
1571 if ($sc eq $sender) {
Jay Soffian3531e272009-02-14 23:32:15 -05001572 next if ($suppress_cc{'self'});
1573 } else {
1574 next if $suppress_cc{'sob'} and $what =~ /Signed-off-by/i;
1575 next if $suppress_cc{'bodycc'} and $what =~ /Cc/i;
1576 }
Jay Soffian50126992009-02-14 23:32:14 -05001577 push @cc, $c;
Vasco Almeidaa4dde4c2016-12-14 11:54:35 -01001578 printf(__("(body) Adding cc: %s from line '%s'\n"),
Jay Soffian50126992009-02-14 23:32:14 -05001579 $c, $_) unless $quiet;
1580 }
1581 }
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001582 close $fh;
Joe Perches324a8bd2007-08-17 18:51:12 -07001583
Joe Perches6e74e072010-09-24 10:03:00 -07001584 push @to, recipients_cmd("to-cmd", "to", $to_cmd, $t)
1585 if defined $to_cmd;
1586 push @cc, recipients_cmd("cc-cmd", "cc", $cc_cmd, $t)
1587 if defined $cc_cmd && !$suppress_cc{'cccmd'};
Joe Perches324a8bd2007-08-17 18:51:12 -07001588
Thomas Rast3cae7e52010-06-17 22:10:39 +02001589 if ($broken_encoding{$t} && !$has_content_type) {
Paolo Bonzinibb294562014-11-25 15:00:26 +01001590 $xfer_encoding = '8bit' if not defined $xfer_encoding;
Thomas Rast3cae7e52010-06-17 22:10:39 +02001591 $has_content_type = 1;
Paolo Bonzinibb294562014-11-25 15:00:26 +01001592 push @xh, "Content-Type: text/plain; charset=$auto_8bit_encoding";
Thomas Rast3cae7e52010-06-17 22:10:39 +02001593 $body_encoding = $auto_8bit_encoding;
1594 }
1595
Krzysztof Mazurce547802012-10-24 23:08:26 +02001596 if ($broken_encoding{$t} && !is_rfc2047_quoted($subject)) {
1597 $subject = quote_subject($subject, $auto_8bit_encoding);
Thomas Rast3cae7e52010-06-17 22:10:39 +02001598 }
1599
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001600 if (defined $sauthor and $sauthor ne $sender) {
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001601 $message = "From: $author\n\n$message";
Jeff King8291db62007-11-16 05:49:09 -05001602 if (defined $author_encoding) {
1603 if ($has_content_type) {
1604 if ($body_encoding eq $author_encoding) {
1605 # ok, we already have the right encoding
1606 }
1607 else {
1608 # uh oh, we should re-encode
1609 }
1610 }
1611 else {
Paolo Bonzinibb294562014-11-25 15:00:26 +01001612 $xfer_encoding = '8bit' if not defined $xfer_encoding;
Thomas Rast3cae7e52010-06-17 22:10:39 +02001613 $has_content_type = 1;
Jeff King8291db62007-11-16 05:49:09 -05001614 push @xh,
Paolo Bonzinibb294562014-11-25 15:00:26 +01001615 "Content-Type: text/plain; charset=$author_encoding";
Jeff King8291db62007-11-16 05:49:09 -05001616 }
1617 }
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001618 }
Paolo Bonzini8d814082014-11-25 15:00:27 +01001619 if (defined $target_xfer_encoding) {
1620 $xfer_encoding = '8bit' if not defined $xfer_encoding;
1621 $message = apply_transfer_encoding(
1622 $message, $xfer_encoding, $target_xfer_encoding);
1623 $xfer_encoding = $target_xfer_encoding;
1624 }
Paolo Bonzinibb294562014-11-25 15:00:26 +01001625 if (defined $xfer_encoding) {
1626 push @xh, "Content-Transfer-Encoding: $xfer_encoding";
1627 }
1628 if (defined $xfer_encoding or $has_content_type) {
1629 unshift @xh, 'MIME-Version: 1.0' unless $has_mime_version;
1630 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001631
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001632 $needs_confirm = (
1633 $confirm eq "always" or
1634 ($confirm =~ /^(?:auto|cc)$/ && @cc) or
1635 ($confirm =~ /^(?:auto|compose)$/ && $compose && $message_num == 1));
1636 $needs_confirm = "inform" if ($needs_confirm && $confirm_unconfigured && @cc);
1637
Remi Lespinetb5e112d2015-06-30 14:16:45 +02001638 @to = process_address_list(@to);
1639 @cc = process_address_list(@cc);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001640
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001641 @to = (@initial_to, @to);
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001642 @cc = (@initial_cc, @cc);
1643
Michael S. Tsirkinf515c902014-04-29 08:41:16 +03001644 if ($message_num == 1) {
1645 if (defined $cover_cc and $cover_cc) {
1646 @initial_cc = @cc;
1647 }
1648 if (defined $cover_to and $cover_to) {
1649 @initial_to = @to;
1650 }
1651 }
1652
Michael Witten15da1082009-04-13 13:23:51 -05001653 my $message_was_sent = send_message();
Ryan Anderson83b24432005-07-31 04:17:25 -04001654
1655 # set up for the next message
Junio C Hamano95a877a2009-06-12 09:23:43 -07001656 if ($thread && $message_was_sent &&
Felipe Contrerasb99d22f2013-05-24 22:44:52 -05001657 ($chain_reply_to || !defined $reply_to || length($reply_to) == 0 ||
Antonio Ospitedb54c8e2010-11-12 15:55:08 +01001658 $message_num == 1)) {
Ryan Anderson78488b22005-07-31 20:04:24 -04001659 $reply_to = $message_id;
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001660 if (length $references > 0) {
YOSHIFUJI Hideaki / 吉藤英明a925b892007-04-06 08:50:24 +09001661 $references .= "\n $message_id";
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001662 } else {
1663 $references = "$message_id";
1664 }
Ryan Anderson78488b22005-07-31 20:04:24 -04001665 }
Jeff King4f3d3702007-12-17 15:51:34 -05001666 $message_id = undef;
Ryan Anderson83b24432005-07-31 04:17:25 -04001667}
Ryan Andersone2057352005-08-02 21:45:22 -04001668
Joe Perches6e74e072010-09-24 10:03:00 -07001669# Execute a command (e.g. $to_cmd) to get a list of email addresses
1670# and return a results array
1671sub recipients_cmd {
1672 my ($prefix, $what, $cmd, $file) = @_;
1673
Joe Perches6e74e072010-09-24 10:03:00 -07001674 my @addresses = ();
Ramkumar Ramachandraa47eab02013-03-31 18:40:42 -07001675 open my $fh, "-|", "$cmd \Q$file\E"
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001676 or die sprintf(__("(%s) Could not execute '%s'"), $prefix, $cmd);
Junio C Hamano7ebee442010-10-26 22:02:52 -07001677 while (my $address = <$fh>) {
Joe Perches6e74e072010-09-24 10:03:00 -07001678 $address =~ s/^\s*//g;
1679 $address =~ s/\s*$//g;
1680 $address = sanitize_address($address);
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001681 next if ($address eq $sender and $suppress_cc{'self'});
Joe Perches6e74e072010-09-24 10:03:00 -07001682 push @addresses, $address;
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001683 printf(__("(%s) Adding %s: %s from: '%s'\n"),
1684 $prefix, $what, $address, $cmd) unless $quiet;
Joe Perches6e74e072010-09-24 10:03:00 -07001685 }
Junio C Hamano7ebee442010-10-26 22:02:52 -07001686 close $fh
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001687 or die sprintf(__("(%s) failed to close pipe to '%s'"), $prefix, $cmd);
Joe Perches6e74e072010-09-24 10:03:00 -07001688 return @addresses;
1689}
1690
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001691cleanup_compose_files();
Ryan Anderson1f038a02005-09-05 01:13:07 -04001692
Ævar Arnfjörð Bjarmason4bf597e2010-09-30 13:43:00 +00001693sub cleanup_compose_files {
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001694 unlink($compose_filename, $compose_filename . ".final") if $compose;
Ryan Anderson1f038a02005-09-05 01:13:07 -04001695}
1696
Eric Wong4bc87a22006-03-25 17:20:48 -08001697$smtp->quit if $smtp;
Ryan Andersone2057352005-08-02 21:45:22 -04001698
Paolo Bonzini8d814082014-11-25 15:00:27 +01001699sub apply_transfer_encoding {
1700 my $message = shift;
1701 my $from = shift;
1702 my $to = shift;
1703
1704 return $message if ($from eq $to and $from ne '7bit');
1705
1706 require MIME::QuotedPrint;
1707 require MIME::Base64;
1708
1709 $message = MIME::QuotedPrint::decode($message)
1710 if ($from eq 'quoted-printable');
1711 $message = MIME::Base64::decode($message)
1712 if ($from eq 'base64');
1713
Vasco Almeida46493102016-12-14 11:54:36 -01001714 die __("cannot send message as 7bit")
Paolo Bonzini8d814082014-11-25 15:00:27 +01001715 if ($to eq '7bit' and $message =~ /[^[:ascii:]]/);
1716 return $message
1717 if ($to eq '7bit' or $to eq '8bit');
1718 return MIME::QuotedPrint::encode($message, "\n", 0)
1719 if ($to eq 'quoted-printable');
1720 return MIME::Base64::encode($message, "\n")
1721 if ($to eq 'base64');
Vasco Almeida46493102016-12-14 11:54:36 -01001722 die __("invalid transfer encoding");
Paolo Bonzini8d814082014-11-25 15:00:27 +01001723}
1724
Ævar Arnfjörð Bjarmasonc438ea22010-09-30 13:42:59 +00001725sub unique_email_list {
Ryan Andersone2057352005-08-02 21:45:22 -04001726 my %seen;
1727 my @emails;
1728
1729 foreach my $entry (@_) {
Krzysztof Mazure4312252012-11-22 19:12:10 +01001730 my $clean = extract_valid_address_or_die($entry);
1731 $seen{$clean} ||= 0;
1732 next if $seen{$clean}++;
1733 push @emails, $entry;
Ryan Andersone2057352005-08-02 21:45:22 -04001734 }
1735 return @emails;
1736}
Jeff King747bbff2008-01-18 09:19:48 -05001737
1738sub validate_patch {
1739 my $fn = shift;
1740 open(my $fh, '<', $fn)
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001741 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
Jeff King747bbff2008-01-18 09:19:48 -05001742 while (my $line = <$fh>) {
1743 if (length($line) > 998) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001744 return sprintf(__("%s: patch contains a line longer than 998 characters"), $.);
Jeff King747bbff2008-01-18 09:19:48 -05001745 }
1746 }
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -07001747 return;
Jeff King747bbff2008-01-18 09:19:48 -05001748}
Jeff King0706bd12008-03-28 17:28:33 -04001749
Junio C Hamano531220b2016-03-17 22:40:05 -07001750sub handle_backup {
1751 my ($last, $lastlen, $file, $known_suffix) = @_;
1752 my ($suffix, $skip);
1753
1754 $skip = 0;
1755 if (defined $last &&
1756 ($lastlen < length($file)) &&
1757 (substr($file, 0, $lastlen) eq $last) &&
1758 ($suffix = substr($file, $lastlen)) !~ /^[a-z0-9]/i) {
1759 if (defined $known_suffix && $suffix eq $known_suffix) {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001760 printf(__("Skipping %s with backup suffix '%s'.\n"), $file, $known_suffix);
Junio C Hamano531220b2016-03-17 22:40:05 -07001761 $skip = 1;
1762 } else {
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001763 # TRANSLATORS: please keep "[y|N]" as is.
1764 my $answer = ask(sprintf(__("Do you really want to send %s? [y|N]: "), $file),
Junio C Hamano531220b2016-03-17 22:40:05 -07001765 valid_re => qr/^(?:y|n)/i,
1766 default => 'n');
1767 $skip = ($answer ne 'y');
1768 if ($skip) {
1769 $known_suffix = $suffix;
1770 }
1771 }
1772 }
1773 return ($skip, $known_suffix);
1774}
1775
1776sub handle_backup_files {
1777 my @file = @_;
1778 my ($last, $lastlen, $known_suffix, $skip, @result);
1779 for my $file (@file) {
1780 ($skip, $known_suffix) = handle_backup($last, $lastlen,
1781 $file, $known_suffix);
1782 push @result, $file unless $skip;
1783 $last = $file;
1784 $lastlen = length($file);
1785 }
1786 return @result;
1787}
1788
Jeff King0706bd12008-03-28 17:28:33 -04001789sub file_has_nonascii {
1790 my $fn = shift;
1791 open(my $fh, '<', $fn)
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001792 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
Jeff King0706bd12008-03-28 17:28:33 -04001793 while (my $line = <$fh>) {
1794 return 1 if $line =~ /[^[:ascii:]]/;
1795 }
1796 return 0;
1797}
Thomas Rast3cae7e52010-06-17 22:10:39 +02001798
1799sub body_or_subject_has_nonascii {
1800 my $fn = shift;
1801 open(my $fh, '<', $fn)
Vasco Almeida3c5cd202016-12-14 11:54:37 -01001802 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
Thomas Rast3cae7e52010-06-17 22:10:39 +02001803 while (my $line = <$fh>) {
1804 last if $line =~ /^$/;
1805 return 1 if $line =~ /^Subject.*[^[:ascii:]]/;
1806 }
1807 while (my $line = <$fh>) {
1808 return 1 if $line =~ /[^[:ascii:]]/;
1809 }
1810 return 0;
1811}