blob: e1e9b1460ced5f660b32796890df7336bc3d01af [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;
22use Term::ReadLine;
Ryan Anderson83b24432005-07-31 04:17:25 -040023use Getopt::Long;
Wu Fengguang0e73b3e2008-12-19 16:10:10 +080024use Text::ParseWords;
Ryan Anderson83b24432005-07-31 04:17:25 -040025use Data::Dumper;
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;
Ryan Anderson83b24432005-07-31 04:17:25 -040031
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +010032Getopt::Long::Configure qw/ pass_through /;
33
Junio C Hamano280242d2006-07-02 16:03:59 -070034package FakeTerm;
35sub new {
36 my ($class, $reason) = @_;
37 return bless \$reason, shift;
38}
39sub readline {
40 my $self = shift;
41 die "Cannot use readline on FakeTerm: $$self";
42}
43package main;
44
Michael Coleman1b0baf12007-02-27 22:47:54 -060045
46sub usage {
47 print <<EOT;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +010048git send-email [options] <file | directory | rev-list options >
Michael Witten4ed62b02008-09-30 07:58:30 -050049
50 Composing:
51 --from <str> * Email From:
Stephen Boydf434c082010-03-07 14:46:48 -080052 --[no-]to <str> * Email To:
53 --[no-]cc <str> * Email Cc:
54 --[no-]bcc <str> * Email Bcc:
Michael Witten4ed62b02008-09-30 07:58:30 -050055 --subject <str> * Email "Subject:"
56 --in-reply-to <str> * Email "In-Reply-To:"
Luis Henriquesac1596a2014-03-24 21:38:27 +000057 --[no-]xmailer * Add "X-Mailer:" header (default).
Felipe Contreras402596a2013-04-07 01:10:27 -060058 --[no-]annotate * Review each patch that will be sent in an editor.
Michael Witten4ed62b02008-09-30 07:58:30 -050059 --compose * Open an editor for introduction.
Krzysztof Mazur62e00692012-10-10 01:02:56 +020060 --compose-encoding <str> * Encoding to assume for introduction.
Thomas Rast3cae7e52010-06-17 22:10:39 +020061 --8bit-encoding <str> * Encoding to assume 8bit mails if undeclared
Paolo Bonzini8d814082014-11-25 15:00:27 +010062 --transfer-encoding <str> * Transfer encoding to use (quoted-printable, 8bit, base64)
Michael Witten4ed62b02008-09-30 07:58:30 -050063
64 Sending:
65 --envelope-sender <str> * Email envelope sender.
66 --smtp-server <str:int> * Outgoing SMTP server to use. The port
67 is optional. Default 'localhost'.
Pascal Obry052fbea2010-09-06 20:12:11 +020068 --smtp-server-option <str> * Outgoing SMTP server option to use.
Michael Witten4ed62b02008-09-30 07:58:30 -050069 --smtp-server-port <int> * Outgoing SMTP server port.
70 --smtp-user <str> * Username for SMTP-AUTH.
71 --smtp-pass <str> * Password for SMTP-AUTH; not necessary.
72 --smtp-encryption <str> * tls or ssl; anything else disables.
73 --smtp-ssl * Deprecated. Use '--smtp-encryption ssl'.
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -070074 --smtp-ssl-cert-path <str> * Path to ca-certificates (either directory or file).
75 Pass an empty string to disable certificate
76 verification.
Jari Aalto134550f2010-03-14 17:16:45 +020077 --smtp-domain <str> * The domain name sent to HELO/EHLO handshake
Jari Aaltof60812e2010-03-14 17:16:09 +020078 --smtp-debug <0|1> * Disable, enable Net::SMTP debug.
Michael Witten4ed62b02008-09-30 07:58:30 -050079
80 Automating:
81 --identity <str> * Use the sendemail.<id> options.
Joe Perches6e74e072010-09-24 10:03:00 -070082 --to-cmd <str> * Email To: via `<str> \$patch_path`
Michael Witten4ed62b02008-09-30 07:58:30 -050083 --cc-cmd <str> * Email Cc: via `<str> \$patch_path`
Jay Soffian3531e272009-02-14 23:32:15 -050084 --suppress-cc <str> * author, self, sob, cc, cccmd, body, bodycc, all.
Michael S. Tsirkinf515c902014-04-29 08:41:16 +030085 --[no-]cc-cover * Email Cc: addresses in the cover letter.
86 --[no-]to-cover * Email To: addresses in the cover letter.
Jay Soffian3531e272009-02-14 23:32:15 -050087 --[no-]signed-off-by-cc * Send to Signed-off-by: addresses. Default on.
Michael Witten4ed62b02008-09-30 07:58:30 -050088 --[no-]suppress-from * Send to self. Default off.
Junio C Hamano41fe87f2009-08-22 12:48:48 -070089 --[no-]chain-reply-to * Chain In-Reply-To: fields. Default off.
Michael Witten4ed62b02008-09-30 07:58:30 -050090 --[no-]thread * Use In-Reply-To: field. Default on.
91
92 Administering:
Jay Soffianc1f2aa42009-03-02 23:52:18 -050093 --confirm <str> * Confirm recipients before sending;
94 auto, cc, compose, always, or never.
Michael Witten4ed62b02008-09-30 07:58:30 -050095 --quiet * Output one line of info per email.
96 --dry-run * Don't actually send the emails.
97 --[no-]validate * Perform patch sanity checks. Default on.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +010098 --[no-]format-patch * understand any non optional arguments as
99 `git format-patch` ones.
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200100 --force * Send even if safety checks would prevent it.
Jeff Kingc764a0c2008-01-18 09:20:10 -0500101
Michael Coleman1b0baf12007-02-27 22:47:54 -0600102EOT
103 exit(1);
104}
105
Eric Wong4bc87a22006-03-25 17:20:48 -0800106# most mail servers generate the Date: header, but not all...
Jakub Narebski6bdca892006-07-07 20:57:55 +0200107sub format_2822_time {
108 my ($time) = @_;
109 my @localtm = localtime($time);
110 my @gmttm = gmtime($time);
111 my $localmin = $localtm[1] + $localtm[2] * 60;
112 my $gmtmin = $gmttm[1] + $gmttm[2] * 60;
113 if ($localtm[0] != $gmttm[0]) {
114 die "local zone differs from GMT by a non-minute interval\n";
115 }
116 if ((($gmttm[6] + 1) % 7) == $localtm[6]) {
117 $localmin += 1440;
118 } elsif ((($gmttm[6] - 1) % 7) == $localtm[6]) {
119 $localmin -= 1440;
120 } elsif ($gmttm[6] != $localtm[6]) {
121 die "local time offset greater than or equal to 24 hours\n";
122 }
123 my $offset = $localmin - $gmtmin;
124 my $offhour = $offset / 60;
125 my $offmin = abs($offset % 60);
126 if (abs($offhour) >= 24) {
127 die ("local time offset greater than or equal to 24 hours\n");
128 }
129
130 return sprintf("%s, %2d %s %d %02d:%02d:%02d %s%02d%02d",
131 qw(Sun Mon Tue Wed Thu Fri Sat)[$localtm[6]],
132 $localtm[3],
133 qw(Jan Feb Mar Apr May Jun
134 Jul Aug Sep Oct Nov Dec)[$localtm[4]],
135 $localtm[5]+1900,
136 $localtm[2],
137 $localtm[1],
138 $localtm[0],
139 ($offset >= 0) ? '+' : '-',
140 abs($offhour),
141 $offmin,
142 );
143}
Eric Wong4bc87a22006-03-25 17:20:48 -0800144
Eric Wong567ffeb2006-03-25 16:47:12 -0800145my $have_email_valid = eval { require Email::Valid; 1 };
Jay Soffian50126992009-02-14 23:32:14 -0500146my $have_mail_address = eval { require Mail::Address; 1 };
Eric Wong4bc87a22006-03-25 17:20:48 -0800147my $smtp;
Wincent Colaiuta5f5b6112007-11-21 13:35:05 +0100148my $auth;
Eric Wong4bc87a22006-03-25 17:20:48 -0800149
Роман Донченко11f70a72014-12-14 18:59:46 +0300150# Regexes for RFC 2047 productions.
151my $re_token = qr/[^][()<>@,;:\\"\/?.= \000-\037\177-\377]+/;
152my $re_encoded_text = qr/[^? \000-\037\177-\377]+/;
153my $re_encoded_word = qr/=\?($re_token)\?($re_token)\?($re_encoded_text)\?=/;
154
Ryan Anderson83b24432005-07-31 04:17:25 -0400155# Variables we fill in automatically, or via prompting:
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700156my (@to,$no_to,@initial_to,@cc,$no_cc,@initial_cc,@bcclist,$no_bcc,@xh,
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100157 $initial_reply_to,$initial_subject,@files,
Luis Henriquesac1596a2014-03-24 21:38:27 +0000158 $author,$sender,$smtp_authpass,$annotate,$use_xmailer,$compose,$time);
Ryan Anderson83b24432005-07-31 04:17:25 -0400159
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700160my $envelope_sender;
Ryan Anderson78488b22005-07-31 20:04:24 -0400161
Ryan Anderson91332612005-07-31 20:04:24 -0400162# Example reply to:
Ryan Anderson83b24432005-07-31 04:17:25 -0400163#$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
Ryan Anderson83b24432005-07-31 04:17:25 -0400164
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100165my $repo = eval { Git->repository() };
166my @repo = $repo ? ($repo) : ();
Junio C Hamano280242d2006-07-02 16:03:59 -0700167my $term = eval {
Jay Soffian0fb7fc72008-02-21 19:16:04 -0500168 $ENV{"GIT_SEND_EMAIL_NOTTY"}
169 ? new Term::ReadLine 'git-send-email', \*STDIN, \*STDOUT
170 : new Term::ReadLine 'git-send-email';
Junio C Hamano280242d2006-07-02 16:03:59 -0700171};
172if ($@) {
173 $term = new FakeTerm "$@: going non-interactive";
174}
Ryan Anderson83b24432005-07-31 04:17:25 -0400175
Adam Roben5483c712007-06-27 20:59:37 -0700176# Behavior modification variables
177my ($quiet, $dry_run) = (0, 0);
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100178my $format_patch;
Jay Soffianafe756c2009-02-23 13:51:37 -0500179my $compose_filename;
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200180my $force = 0;
Adam Roben5483c712007-06-27 20:59:37 -0700181
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100182# Handle interactive edition of files.
183my $multiedit;
Michael J Gruber0ce142c2010-03-22 17:12:53 +0100184my $editor;
Jonathan Niederb4479f02009-10-30 20:42:34 -0500185
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100186sub do_edit {
Michael J Gruber0ce142c2010-03-22 17:12:53 +0100187 if (!defined($editor)) {
188 $editor = Git::command_oneline('var', 'GIT_EDITOR');
189 }
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100190 if (defined($multiedit) && !$multiedit) {
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100191 map {
192 system('sh', '-c', $editor.' "$@"', $editor, $_);
193 if (($? & 127) || ($? >> 8)) {
194 die("the editor exited uncleanly, aborting everything");
195 }
196 } @_;
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100197 } else {
198 system('sh', '-c', $editor.' "$@"', $editor, @_);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100199 if (($? & 127) || ($? >> 8)) {
200 die("the editor exited uncleanly, aborting everything");
201 }
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100202 }
203}
Adam Roben5483c712007-06-27 20:59:37 -0700204
205# Variables with corresponding config settings
Joe Perches6e74e072010-09-24 10:03:00 -0700206my ($thread, $chain_reply_to, $suppress_from, $signed_off_by_cc);
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300207my ($cover_cc, $cover_to);
Joe Perches6e74e072010-09-24 10:03:00 -0700208my ($to_cmd, $cc_cmd);
Pascal Obry052fbea2010-09-06 20:12:11 +0200209my ($smtp_server, $smtp_server_port, @smtp_server_options);
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -0700210my ($smtp_authuser, $smtp_encryption, $smtp_ssl_cert_path);
Pascal Obry1d02a002010-09-06 20:12:10 +0200211my ($identity, $aliasfiletype, @alias_files, $smtp_domain);
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500212my ($validate, $confirm);
David Brown65648282007-12-25 19:56:29 -0800213my (@suppress_cc);
Thomas Rast3cae7e52010-06-17 22:10:39 +0200214my ($auto_8bit_encoding);
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200215my ($compose_encoding);
Paolo Bonzini8d814082014-11-25 15:00:27 +0100216my ($target_xfer_encoding);
Adam Roben5483c712007-06-27 20:59:37 -0700217
Jari Aaltof60812e2010-03-14 17:16:09 +0200218my ($debug_net_smtp) = 0; # Net::SMTP, see send_message()
219
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900220my %config_bool_settings = (
Adam Roben5483c712007-06-27 20:59:37 -0700221 "thread" => [\$thread, 1],
Felipe Contrerasb99d22f2013-05-24 22:44:52 -0500222 "chainreplyto" => [\$chain_reply_to, 0],
David Brown65648282007-12-25 19:56:29 -0800223 "suppressfrom" => [\$suppress_from, undef],
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500224 "signedoffbycc" => [\$signed_off_by_cc, undef],
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300225 "cccover" => [\$cover_cc, undef],
226 "tocover" => [\$cover_to, undef],
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500227 "signedoffcc" => [\$signed_off_by_cc, undef], # Deprecated
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500228 "validate" => [\$validate, 1],
Felipe Contreras402596a2013-04-07 01:10:27 -0600229 "multiedit" => [\$multiedit, undef],
Luis Henriquesac1596a2014-03-24 21:38:27 +0000230 "annotate" => [\$annotate, undef],
231 "xmailer" => [\$use_xmailer, 1]
Adam Robene46f7a02007-06-26 15:48:30 -0700232);
233
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900234my %config_settings = (
235 "smtpserver" => \$smtp_server,
Junio C Hamano44b24762007-09-25 17:27:54 -0700236 "smtpserverport" => \$smtp_server_port,
Pascal Obry052fbea2010-09-06 20:12:11 +0200237 "smtpserveroption" => \@smtp_server_options,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900238 "smtpuser" => \$smtp_authuser,
239 "smtppass" => \$smtp_authpass,
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -0700240 "smtpsslcertpath" => \$smtp_ssl_cert_path,
Pascal Obrye1e91152010-09-06 20:12:09 +0200241 "smtpdomain" => \$smtp_domain,
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700242 "to" => \@initial_to,
Joe Perches6e74e072010-09-24 10:03:00 -0700243 "tocmd" => \$to_cmd,
Miklos Vajna5f8b9fc2008-04-27 14:14:58 +0200244 "cc" => \@initial_cc,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900245 "cccmd" => \$cc_cmd,
246 "aliasfiletype" => \$aliasfiletype,
247 "bcc" => \@bcclist,
David Brown65648282007-12-25 19:56:29 -0800248 "suppresscc" => \@suppress_cc,
Ask Bjørn Hansen9f7820a2008-06-07 00:33:42 -0700249 "envelopesender" => \$envelope_sender,
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500250 "confirm" => \$confirm,
Trent Piepho09caa242009-05-12 15:48:56 -0700251 "from" => \$sender,
Thomas Rast3cae7e52010-06-17 22:10:39 +0200252 "assume8bitencoding" => \$auto_8bit_encoding,
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200253 "composeencoding" => \$compose_encoding,
Paolo Bonzini8d814082014-11-25 15:00:27 +0100254 "transferencoding" => \$target_xfer_encoding,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900255);
Avi Kivity4a62d3f2007-03-11 19:19:44 +0200256
Cord Seelecec5dae2011-09-30 12:52:25 +0200257my %config_path_settings = (
258 "aliasesfile" => \@alias_files,
259);
260
Michael Witten87429972008-02-03 19:53:57 -0500261# Handle Uncouth Termination
262sub signal_handler {
263
264 # Make text normal
265 print color("reset"), "\n";
266
267 # SMTP password masked
268 system "stty echo";
269
270 # tmp files from --compose
Jay Soffianafe756c2009-02-23 13:51:37 -0500271 if (defined $compose_filename) {
272 if (-e $compose_filename) {
273 print "'$compose_filename' contains an intermediate version of the email you were composing.\n";
274 }
275 if (-e ($compose_filename . ".final")) {
276 print "'$compose_filename.final' contains the composed email.\n"
277 }
Michael Witten87429972008-02-03 19:53:57 -0500278 }
279
280 exit;
281};
282
283$SIG{TERM} = \&signal_handler;
284$SIG{INT} = \&signal_handler;
285
Ryan Anderson83b24432005-07-31 04:17:25 -0400286# Begin by accumulating all the variables (defined above), that we will end up
287# needing, first, from the command line:
288
Clemens Buchacherc5978242011-09-03 19:06:13 +0200289my $help;
290my $rc = GetOptions("h" => \$help,
291 "sender|from=s" => \$sender,
Ryan Anderson83b24432005-07-31 04:17:25 -0400292 "in-reply-to=s" => \$initial_reply_to,
293 "subject=s" => \$initial_subject,
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700294 "to=s" => \@initial_to,
Joe Perches6e74e072010-09-24 10:03:00 -0700295 "to-cmd=s" => \$to_cmd,
Stephen Boydf434c082010-03-07 14:46:48 -0800296 "no-to" => \$no_to,
Ryan Andersonda140f82006-02-13 03:05:15 -0500297 "cc=s" => \@initial_cc,
Stephen Boydf434c082010-03-07 14:46:48 -0800298 "no-cc" => \$no_cc,
Ryan Anderson58063242006-05-29 12:30:13 -0700299 "bcc=s" => \@bcclist,
Stephen Boydf434c082010-03-07 14:46:48 -0800300 "no-bcc" => \$no_bcc,
Ryan Anderson78488b22005-07-31 20:04:24 -0400301 "chain-reply-to!" => \$chain_reply_to,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800302 "no-chain-reply-to" => sub {$chain_reply_to = 0},
Ryan Anderson3342d852005-07-31 20:04:24 -0400303 "smtp-server=s" => \$smtp_server,
Pascal Obry052fbea2010-09-06 20:12:11 +0200304 "smtp-server-option=s" => \@smtp_server_options,
Junio C Hamano44b24762007-09-25 17:27:54 -0700305 "smtp-server-port=s" => \$smtp_server_port,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900306 "smtp-user=s" => \$smtp_authuser,
Michael Witten2363d742008-02-03 19:53:56 -0500307 "smtp-pass:s" => \$smtp_authpass,
Thomas Rastf6bebd12008-06-25 21:42:43 +0200308 "smtp-ssl" => sub { $smtp_encryption = 'ssl' },
309 "smtp-encryption=s" => \$smtp_encryption,
Thomas Rast979e6522013-12-01 23:48:42 +0100310 "smtp-ssl-cert-path=s" => \$smtp_ssl_cert_path,
Jari Aaltof60812e2010-03-14 17:16:09 +0200311 "smtp-debug:i" => \$debug_net_smtp,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -0400312 "smtp-domain:s" => \$smtp_domain,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900313 "identity=s" => \$identity,
Felipe Contreras402596a2013-04-07 01:10:27 -0600314 "annotate!" => \$annotate,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800315 "no-annotate" => sub {$annotate = 0},
Ryan Anderson1f038a02005-09-05 01:13:07 -0400316 "compose" => \$compose,
Ryan Anderson30d08b32006-02-02 11:56:06 -0500317 "quiet" => \$quiet,
Joe Perches324a8bd2007-08-17 18:51:12 -0700318 "cc-cmd=s" => \$cc_cmd,
Adam Roben5483c712007-06-27 20:59:37 -0700319 "suppress-from!" => \$suppress_from,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800320 "no-suppress-from" => sub {$suppress_from = 0},
David Brown65648282007-12-25 19:56:29 -0800321 "suppress-cc=s" => \@suppress_cc,
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500322 "signed-off-cc|signed-off-by-cc!" => \$signed_off_by_cc,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800323 "no-signed-off-cc|no-signed-off-by-cc" => sub {$signed_off_by_cc = 0},
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300324 "cc-cover|cc-cover!" => \$cover_cc,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800325 "no-cc-cover" => sub {$cover_cc = 0},
Michael S. Tsirkinf515c902014-04-29 08:41:16 +0300326 "to-cover|to-cover!" => \$cover_to,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800327 "no-to-cover" => sub {$cover_to = 0},
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500328 "confirm=s" => \$confirm,
Matthew Wilcox61302592006-10-10 08:58:23 -0600329 "dry-run" => \$dry_run,
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700330 "envelope-sender=s" => \$envelope_sender,
Adam Roben5483c712007-06-27 20:59:37 -0700331 "thread!" => \$thread,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800332 "no-thread" => sub {$thread = 0},
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500333 "validate!" => \$validate,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800334 "no-validate" => sub {$validate = 0},
Paolo Bonzini8d814082014-11-25 15:00:27 +0100335 "transfer-encoding=s" => \$target_xfer_encoding,
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100336 "format-patch!" => \$format_patch,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800337 "no-format-patch" => sub {$format_patch = 0},
Thomas Rast3cae7e52010-06-17 22:10:39 +0200338 "8bit-encoding=s" => \$auto_8bit_encoding,
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200339 "compose-encoding=s" => \$compose_encoding,
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200340 "force" => \$force,
Luis Henriquesac1596a2014-03-24 21:38:27 +0000341 "xmailer!" => \$use_xmailer,
Kyle J. McKayf4714942015-01-30 18:40:17 -0800342 "no-xmailer" => sub {$use_xmailer = 0},
Ryan Anderson83b24432005-07-31 04:17:25 -0400343 );
344
Clemens Buchacherc5978242011-09-03 19:06:13 +0200345usage() if $help;
Michael Coleman1b0baf12007-02-27 22:47:54 -0600346unless ($rc) {
347 usage();
348}
349
Jay Soffianeed6ca72009-02-14 23:32:13 -0500350die "Cannot run git format-patch from outside a repository\n"
351 if $format_patch and not $repo;
352
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900353# Now, let's fill any that aren't set in with defaults:
354
355sub read_config {
356 my ($prefix) = @_;
357
358 foreach my $setting (keys %config_bool_settings) {
359 my $target = $config_bool_settings{$setting}->[0];
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100360 $$target = Git::config_bool(@repo, "$prefix.$setting") unless (defined $$target);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900361 }
362
Cord Seelecec5dae2011-09-30 12:52:25 +0200363 foreach my $setting (keys %config_path_settings) {
Cord Seele463b0ea2011-10-14 22:53:31 +0200364 my $target = $config_path_settings{$setting};
365 if (ref($target) eq "ARRAY") {
366 unless (@$target) {
367 my @values = Git::config_path(@repo, "$prefix.$setting");
368 @$target = @values if (@values && defined $values[0]);
369 }
370 }
371 else {
372 $$target = Git::config_path(@repo, "$prefix.$setting") unless (defined $$target);
373 }
Cord Seelecec5dae2011-09-30 12:52:25 +0200374 }
375
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900376 foreach my $setting (keys %config_settings) {
377 my $target = $config_settings{$setting};
Stephen Boydf434c082010-03-07 14:46:48 -0800378 next if $setting eq "to" and defined $no_to;
379 next if $setting eq "cc" and defined $no_cc;
380 next if $setting eq "bcc" and defined $no_bcc;
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900381 if (ref($target) eq "ARRAY") {
382 unless (@$target) {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100383 my @values = Git::config(@repo, "$prefix.$setting");
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900384 @$target = @values if (@values && defined $values[0]);
385 }
386 }
387 else {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100388 $$target = Git::config(@repo, "$prefix.$setting") unless (defined $$target);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900389 }
390 }
Thomas Rastf6bebd12008-06-25 21:42:43 +0200391
392 if (!defined $smtp_encryption) {
393 my $enc = Git::config(@repo, "$prefix.smtpencryption");
394 if (defined $enc) {
395 $smtp_encryption = $enc;
396 } elsif (Git::config_bool(@repo, "$prefix.smtpssl")) {
397 $smtp_encryption = 'ssl';
398 }
399 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900400}
401
402# read configuration from [sendemail "$identity"], fall back on [sendemail]
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100403$identity = Git::config(@repo, "sendemail.identity") unless (defined $identity);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900404read_config("sendemail.$identity") if (defined $identity);
405read_config("sendemail");
406
407# fall back on builtin bool defaults
408foreach my $setting (values %config_bool_settings) {
409 ${$setting->[0]} = $setting->[1] unless (defined (${$setting->[0]}));
410}
411
Thomas Rastfa835cd2008-06-26 23:03:21 +0200412# 'default' encryption is none -- this only prevents a warning
413$smtp_encryption = '' unless (defined $smtp_encryption);
414
David Brown65648282007-12-25 19:56:29 -0800415# Set CC suppressions
416my(%suppress_cc);
417if (@suppress_cc) {
418 foreach my $entry (@suppress_cc) {
419 die "Unknown --suppress-cc field: '$entry'\n"
Ævar Arnfjörð Bjarmasone9bf7412010-09-30 13:43:04 +0000420 unless $entry =~ /^(?:all|cccmd|cc|author|self|sob|body|bodycc)$/;
David Brown65648282007-12-25 19:56:29 -0800421 $suppress_cc{$entry} = 1;
422 }
423}
424
425if ($suppress_cc{'all'}) {
Paolo Bonzinicb8a9bd2009-06-18 14:31:32 +0200426 foreach my $entry (qw (cccmd cc author self sob body bodycc)) {
David Brown65648282007-12-25 19:56:29 -0800427 $suppress_cc{$entry} = 1;
428 }
429 delete $suppress_cc{'all'};
430}
431
432# If explicit old-style ones are specified, they trump --suppress-cc.
433$suppress_cc{'self'} = $suppress_from if defined $suppress_from;
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500434$suppress_cc{'sob'} = !$signed_off_by_cc if defined $signed_off_by_cc;
David Brown65648282007-12-25 19:56:29 -0800435
Jay Soffian3531e272009-02-14 23:32:15 -0500436if ($suppress_cc{'body'}) {
437 foreach my $entry (qw (sob bodycc)) {
438 $suppress_cc{$entry} = 1;
439 }
440 delete $suppress_cc{'body'};
441}
442
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500443# Set confirm's default value
444my $confirm_unconfigured = !defined $confirm;
445if ($confirm_unconfigured) {
446 $confirm = scalar %suppress_cc ? 'compose' : 'auto';
447};
448die "Unknown --confirm setting: '$confirm'\n"
449 unless $confirm =~ /^(?:auto|cc|compose|always|never)/;
450
David Brown65648282007-12-25 19:56:29 -0800451# Debugging, print out the suppressions.
452if (0) {
453 print "suppressions:\n";
454 foreach my $entry (keys %suppress_cc) {
455 printf " %-5s -> $suppress_cc{$entry}\n", $entry;
456 }
457}
458
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100459my ($repoauthor, $repocommitter);
460($repoauthor) = Git::ident_person(@repo, 'author');
461($repocommitter) = Git::ident_person(@repo, 'committer');
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900462
Eric W. Biederman79ee5552006-06-21 07:17:31 -0600463# Verify the user input
464
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700465foreach my $entry (@initial_to) {
Eric W. Biederman79ee5552006-06-21 07:17:31 -0600466 die "Comma in --to entry: $entry'\n" unless $entry !~ m/,/;
467}
468
469foreach my $entry (@initial_cc) {
470 die "Comma in --cc entry: $entry'\n" unless $entry !~ m/,/;
471}
472
473foreach my $entry (@bcclist) {
474 die "Comma in --bcclist entry: $entry'\n" unless $entry !~ m/,/;
475}
476
Jay Soffian50126992009-02-14 23:32:14 -0500477sub parse_address_line {
478 if ($have_mail_address) {
479 return map { $_->format } Mail::Address->parse($_[0]);
480 } else {
481 return split_addrs($_[0]);
482 }
483}
484
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800485sub split_addrs {
Junio C Hamano2f0e7cb2008-12-21 01:57:59 -0800486 return quotewords('\s*,\s*', 1, @_);
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800487}
488
Eric Wong994d6c62006-05-14 19:13:44 -0700489my %aliases;
Eric Wong994d6c62006-05-14 19:13:44 -0700490my %parse_alias = (
491 # multiline formats can be supported in the future
492 mutt => sub { my $fh = shift; while (<$fh>) {
Felipe Contrerasffc01f92009-09-30 17:49:36 +0300493 if (/^\s*alias\s+(?:-group\s+\S+\s+)*(\S+)\s+(.*)$/) {
Eric Wong994d6c62006-05-14 19:13:44 -0700494 my ($alias, $addr) = ($1, $2);
495 $addr =~ s/#.*$//; # mutt allows # comments
496 # commas delimit multiple addresses
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800497 $aliases{$alias} = [ split_addrs($addr) ];
Eric Wong994d6c62006-05-14 19:13:44 -0700498 }}},
499 mailrc => sub { my $fh = shift; while (<$fh>) {
500 if (/^alias\s+(\S+)\s+(.*)$/) {
501 # spaces delimit multiple addresses
Eric W. Biedermanfe87c922009-05-20 19:45:53 -0700502 $aliases{$1} = [ quotewords('\s+', 0, $2) ];
Eric Wong994d6c62006-05-14 19:13:44 -0700503 }}},
Trent Piepho73c427e2008-11-25 18:55:00 -0800504 pine => sub { my $fh = shift; my $f='\t[^\t]*';
505 for (my $x = ''; defined($x); $x = $_) {
506 chomp $x;
507 $x .= $1 while(defined($_ = <$fh>) && /^ +(.*)$/);
508 $x =~ /^(\S+)$f\t\(?([^\t]+?)\)?(:?$f){0,2}$/ or next;
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800509 $aliases{$1} = [ split_addrs($2) ];
Trent Piepho73c427e2008-11-25 18:55:00 -0800510 }},
Bill Pemberton7613ea32009-04-22 09:41:29 -0400511 elm => sub { my $fh = shift;
512 while (<$fh>) {
513 if (/^(\S+)\s+=\s+[^=]+=\s(\S+)/) {
514 my ($alias, $addr) = ($1, $2);
515 $aliases{$alias} = [ split_addrs($addr) ];
516 }
517 } },
518
Eric Wong994d6c62006-05-14 19:13:44 -0700519 gnus => sub { my $fh = shift; while (<$fh>) {
520 if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
521 $aliases{$1} = [ $2 ];
522 }}}
523);
524
Petr Baudis3cb8caf2006-07-03 22:47:58 +0200525if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) {
Eric Wong994d6c62006-05-14 19:13:44 -0700526 foreach my $file (@alias_files) {
527 open my $fh, '<', $file or die "opening $file: $!\n";
528 $parse_alias{$aliasfiletype}->($fh);
529 close $fh;
530 }
531}
532
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200533($sender) = expand_aliases($sender) if defined $sender;
Michael Hendricksae740a52007-07-04 19:11:36 -0600534
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700535# is_format_patch_arg($f) returns 0 if $f names a patch, or 1 if
536# $f is a revision list specification to be passed to format-patch.
537sub is_format_patch_arg {
Jay Soffianeed6ca72009-02-14 23:32:13 -0500538 return unless $repo;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100539 my $f = shift;
540 try {
541 $repo->command('rev-parse', '--verify', '--quiet', $f);
542 if (defined($format_patch)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100543 return $format_patch;
544 }
545 die(<<EOF);
546File '$f' exists but it could also be the range of commits
547to produce patches for. Please disambiguate by...
548
549 * Saying "./$f" if you mean a file; or
550 * Giving --format-patch option if you mean a range.
551EOF
552 } catch Git::Error::Command with {
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700553 # Not a valid revision. Treat it as a filename.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100554 return 0;
555 }
556}
557
Jeff Kingaa548922008-01-18 09:19:36 -0500558# Now that all the defaults are set, process the rest of the command line
559# arguments and collect up the files that need to be processed.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100560my @rev_list_opts;
Junio C Hamano69f4ce52008-11-30 22:38:20 -0800561while (defined(my $f = shift @ARGV)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100562 if ($f eq "--") {
563 push @rev_list_opts, "--", @ARGV;
564 @ARGV = ();
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700565 } elsif (-d $f and !is_format_patch_arg($f)) {
Ævar Arnfjörð Bjarmasonc6038162010-09-30 13:42:54 +0000566 opendir my $dh, $f
Jeff Kingaa548922008-01-18 09:19:36 -0500567 or die "Failed to opendir $f: $!";
568
Ævar Arnfjörð Bjarmason89bf1ba2010-09-14 19:02:24 +0000569 push @files, grep { -f $_ } map { catfile($f, $_) }
Ævar Arnfjörð Bjarmasonc6038162010-09-30 13:42:54 +0000570 sort readdir $dh;
571 closedir $dh;
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700572 } elsif ((-f $f or -p $f) and !is_format_patch_arg($f)) {
Jeff Kingaa548922008-01-18 09:19:36 -0500573 push @files, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500574 } else {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100575 push @rev_list_opts, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500576 }
577}
578
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100579if (@rev_list_opts) {
Jay Soffianeed6ca72009-02-14 23:32:13 -0500580 die "Cannot run git format-patch from outside a repository\n"
581 unless $repo;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100582 push @files, $repo->command('format-patch', '-o', tempdir(CLEANUP => 1), @rev_list_opts);
583}
584
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500585if ($validate) {
Jeff Kingc764a0c2008-01-18 09:20:10 -0500586 foreach my $f (@files) {
Kevin Ballard300913b2008-06-25 15:44:40 -0700587 unless (-p $f) {
588 my $error = validate_patch($f);
589 $error and die "fatal: $f: $error\nwarning: no patches were sent\n";
590 }
Jeff Kingc764a0c2008-01-18 09:20:10 -0500591 }
Jeff King747bbff2008-01-18 09:19:48 -0500592}
593
Jeff Kingaa548922008-01-18 09:19:36 -0500594if (@files) {
595 unless ($quiet) {
596 print $_,"\n" for (@files);
597 }
598} else {
599 print STDERR "\nNo patch files specified!\n\n";
600 usage();
601}
602
Ævar Arnfjörð Bjarmasonacf071b2010-09-30 13:42:57 +0000603sub get_patch_subject {
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100604 my $fn = shift;
605 open (my $fh, '<', $fn);
606 while (my $line = <$fh>) {
607 next unless ($line =~ /^Subject: (.*)$/);
608 close $fh;
609 return "GIT: $1\n";
610 }
611 close $fh;
612 die "No subject line in $fn ?";
613}
614
615if ($compose) {
616 # Note that this does not need to be secure, but we will make a small
617 # effort to have it be unique
Jay Soffianafe756c2009-02-23 13:51:37 -0500618 $compose_filename = ($repo ?
619 tempfile(".gitsendemail.msg.XXXXXX", DIR => $repo->repo_path()) :
620 tempfile(".gitsendemail.msg.XXXXXX", DIR => "."))[1];
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000621 open my $c, ">", $compose_filename
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100622 or die "Failed to open for writing $compose_filename: $!";
623
624
625 my $tpl_sender = $sender || $repoauthor || $repocommitter || '';
626 my $tpl_subject = $initial_subject || '';
627 my $tpl_reply_to = $initial_reply_to || '';
628
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000629 print $c <<EOT;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100630From $tpl_sender # This line is ignored.
Michael Witten40e6e8a2009-04-13 13:23:50 -0500631GIT: Lines beginning in "GIT:" will be removed.
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100632GIT: Consider including an overall diffstat or table of contents
633GIT: for the patch you are writing.
634GIT:
635GIT: Clear the body content if you don't wish to send a summary.
636From: $tpl_sender
637Subject: $tpl_subject
638In-Reply-To: $tpl_reply_to
639
640EOT
641 for my $f (@files) {
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000642 print $c get_patch_subject($f);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100643 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000644 close $c;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100645
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100646 if ($annotate) {
647 do_edit($compose_filename, @files);
648 } else {
649 do_edit($compose_filename);
650 }
651
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000652 open my $c2, ">", $compose_filename . ".final"
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100653 or die "Failed to open $compose_filename.final : " . $!;
654
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000655 open $c, "<", $compose_filename
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100656 or die "Failed to open $compose_filename : " . $!;
657
658 my $need_8bit_cte = file_has_nonascii($compose_filename);
659 my $in_body = 0;
660 my $summary_empty = 1;
Krzysztof Mazur4a47a4d2012-10-22 14:41:48 +0200661 if (!defined $compose_encoding) {
662 $compose_encoding = "UTF-8";
663 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000664 while(<$c>) {
Michael Witten40e6e8a2009-04-13 13:23:50 -0500665 next if m/^GIT:/;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100666 if ($in_body) {
667 $summary_empty = 0 unless (/^\n$/);
668 } elsif (/^\n$/) {
669 $in_body = 1;
670 if ($need_8bit_cte) {
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000671 print $c2 "MIME-Version: 1.0\n",
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100672 "Content-Type: text/plain; ",
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200673 "charset=$compose_encoding\n",
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100674 "Content-Transfer-Encoding: 8bit\n";
675 }
676 } elsif (/^MIME-Version:/i) {
677 $need_8bit_cte = 0;
678 } elsif (/^Subject:\s*(.+)\s*$/i) {
679 $initial_subject = $1;
680 my $subject = $initial_subject;
681 $_ = "Subject: " .
Krzysztof Mazurce547802012-10-24 23:08:26 +0200682 quote_subject($subject, $compose_encoding) .
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100683 "\n";
684 } elsif (/^In-Reply-To:\s*(.+)\s*$/i) {
685 $initial_reply_to = $1;
686 next;
687 } elsif (/^From:\s*(.+)\s*$/i) {
688 $sender = $1;
689 next;
690 } elsif (/^(?:To|Cc|Bcc):/i) {
691 print "To/Cc/Bcc fields are not interpreted yet, they have been ignored\n";
692 next;
693 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000694 print $c2 $_;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100695 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000696 close $c;
697 close $c2;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100698
699 if ($summary_empty) {
700 print "Summary email is empty, skipping it\n";
701 $compose = -1;
702 }
703} elsif ($annotate) {
704 do_edit(@files);
705}
706
Jay Soffian6e182512009-03-28 21:39:10 -0400707sub ask {
708 my ($prompt, %arg) = @_;
Jay Soffian0da43a62009-04-04 23:23:21 -0400709 my $valid_re = $arg{valid_re};
Jay Soffian6e182512009-03-28 21:39:10 -0400710 my $default = $arg{default};
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700711 my $confirm_only = $arg{confirm_only};
Jay Soffian6e182512009-03-28 21:39:10 -0400712 my $resp;
713 my $i = 0;
Jay Soffian5906f542009-03-31 12:22:11 -0400714 return defined $default ? $default : undef
715 unless defined $term->IN and defined fileno($term->IN) and
716 defined $term->OUT and defined fileno($term->OUT);
Jay Soffian6e182512009-03-28 21:39:10 -0400717 while ($i++ < 10) {
718 $resp = $term->readline($prompt);
719 if (!defined $resp) { # EOF
720 print "\n";
721 return defined $default ? $default : undef;
722 }
723 if ($resp eq '' and defined $default) {
724 return $default;
725 }
Jay Soffian0da43a62009-04-04 23:23:21 -0400726 if (!defined $valid_re or $resp =~ /$valid_re/) {
Jay Soffian6e182512009-03-28 21:39:10 -0400727 return $resp;
728 }
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700729 if ($confirm_only) {
730 my $yesno = $term->readline("Are you sure you want to use <$resp> [y/N]? ");
731 if (defined $yesno && $yesno =~ /y/i) {
732 return $resp;
733 }
734 }
Jay Soffian6e182512009-03-28 21:39:10 -0400735 }
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -0700736 return;
Jay Soffian6e182512009-03-28 21:39:10 -0400737}
738
Thomas Rast3cae7e52010-06-17 22:10:39 +0200739my %broken_encoding;
740
Ævar Arnfjörð Bjarmason1d50bfd2010-09-30 13:42:58 +0000741sub file_declares_8bit_cte {
Thomas Rast3cae7e52010-06-17 22:10:39 +0200742 my $fn = shift;
743 open (my $fh, '<', $fn);
744 while (my $line = <$fh>) {
745 last if ($line =~ /^$/);
746 return 1 if ($line =~ /^Content-Transfer-Encoding: .*8bit.*$/);
747 }
748 close $fh;
749 return 0;
750}
751
752foreach my $f (@files) {
753 next unless (body_or_subject_has_nonascii($f)
754 && !file_declares_8bit_cte($f));
755 $broken_encoding{$f} = 1;
756}
757
758if (!defined $auto_8bit_encoding && scalar %broken_encoding) {
759 print "The following files are 8bit, but do not declare " .
760 "a Content-Transfer-Encoding.\n";
761 foreach my $f (sort keys %broken_encoding) {
762 print " $f\n";
763 }
764 $auto_8bit_encoding = ask("Which 8bit encoding should I declare [UTF-8]? ",
Junio C Hamano852a15d2015-02-13 12:20:25 -0800765 valid_re => qr/.{4}/, confirm_only => 1,
Thomas Rast3cae7e52010-06-17 22:10:39 +0200766 default => "UTF-8");
767}
768
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200769if (!$force) {
770 for my $f (@files) {
Ævar Arnfjörð Bjarmason0d290a42010-09-30 13:43:01 +0000771 if (get_patch_subject($f) =~ /\Q*** SUBJECT HERE ***\E/) {
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200772 die "Refusing to send because the patch\n\t$f\n"
773 . "has the template subject '*** SUBJECT HERE ***'. "
774 . "Pass --force if you really want to send.\n";
775 }
776 }
777}
778
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200779if (!defined $sender) {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100780 $sender = $repoauthor || $repocommitter || '';
Ryan Anderson83b24432005-07-31 04:17:25 -0400781}
782
Michael S. Tsirkinda187592013-06-05 21:11:00 +0300783# $sender could be an already sanitized address
784# (e.g. sendemail.from could be manually sanitized by user).
785# But it's a no-op to run sanitize_address on an already sanitized address.
786$sender = sanitize_address($sender);
787
Felipe Contreras8cac13d2012-11-24 12:16:19 +0100788my $prompting = 0;
Junio C Hamano8796ff72010-10-26 22:02:03 -0700789if (!@initial_to && !defined $to_cmd) {
Stephen Boyd61837492012-09-06 11:31:11 -0700790 my $to = ask("Who should the emails be sent to (if any)? ",
791 default => "",
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700792 valid_re => qr/\@.*\./, confirm_only => 1);
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700793 push @initial_to, parse_address_line($to) if defined $to; # sanitized/validated later
Ryan Anderson1f038a02005-09-05 01:13:07 -0400794 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -0400795}
796
Eric Wong994d6c62006-05-14 19:13:44 -0700797sub expand_aliases {
Jeff King302e04e2009-07-23 07:09:29 -0400798 return map { expand_one_alias($_) } @_;
799}
800
801my %EXPANDED_ALIASES;
802sub expand_one_alias {
803 my $alias = shift;
804 if ($EXPANDED_ALIASES{$alias}) {
805 die "fatal: alias '$alias' expands to itself\n";
806 }
807 local $EXPANDED_ALIASES{$alias} = 1;
808 return $aliases{$alias} ? expand_aliases(@{$aliases{$alias}}) : $alias;
Eric Wong994d6c62006-05-14 19:13:44 -0700809}
810
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700811@initial_to = expand_aliases(@initial_to);
Krzysztof Mazure4312252012-11-22 19:12:10 +0100812@initial_to = validate_address_list(sanitize_address_list(@initial_to));
Eric Wong994d6c62006-05-14 19:13:44 -0700813@initial_cc = expand_aliases(@initial_cc);
Krzysztof Mazure4312252012-11-22 19:12:10 +0100814@initial_cc = validate_address_list(sanitize_address_list(@initial_cc));
Ryan Anderson58063242006-05-29 12:30:13 -0700815@bcclist = expand_aliases(@bcclist);
Krzysztof Mazure4312252012-11-22 19:12:10 +0100816@bcclist = validate_address_list(sanitize_address_list(@bcclist));
Eric Wong994d6c62006-05-14 19:13:44 -0700817
Adam Roben5483c712007-06-27 20:59:37 -0700818if ($thread && !defined $initial_reply_to && $prompting) {
Jay Soffian6e182512009-03-28 21:39:10 -0400819 $initial_reply_to = ask(
Stephen Boyd61837492012-09-06 11:31:11 -0700820 "Message-ID to be used as In-Reply-To for the first email (if any)? ",
821 default => "",
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700822 valid_re => qr/\@.*\./, confirm_only => 1);
Ryan Anderson83b24432005-07-31 04:17:25 -0400823}
Jay Soffian1ca3d6e2008-02-20 00:55:07 -0500824if (defined $initial_reply_to) {
Jay Soffian0fb7fc72008-02-21 19:16:04 -0500825 $initial_reply_to =~ s/^\s*<?//;
826 $initial_reply_to =~ s/>?\s*$//;
827 $initial_reply_to = "<$initial_reply_to>" if $initial_reply_to ne '';
Junio C Hamanoace9c2a2007-12-10 21:44:42 -0800828}
Mike Hommeyace72082007-12-09 18:17:28 +0100829
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900830if (!defined $smtp_server) {
Eric Wongaca7ad72006-05-15 02:34:44 -0700831 foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) {
832 if (-x $_) {
833 $smtp_server = $_;
834 last;
835 }
836 }
837 $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
Ryan Anderson3342d852005-07-31 20:04:24 -0400838}
839
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500840if ($compose && $compose > 0) {
841 @files = ($compose_filename . ".final", @files);
Ryan Anderson1f038a02005-09-05 01:13:07 -0400842}
843
Ryan Anderson83b24432005-07-31 04:17:25 -0400844# Variables we set as part of the loop over files
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500845our ($message_id, %mail, $subject, $reply_to, $references, $message,
Jay Soffiandc1460a2009-03-31 12:22:12 -0400846 $needs_confirm, $message_num, $ask_default);
Ryan Anderson83b24432005-07-31 04:17:25 -0400847
Eric Wong567ffeb2006-03-25 16:47:12 -0800848sub extract_valid_address {
849 my $address = shift;
Ævar Arnfjörð Bjarmason35b6ab92010-09-30 19:03:31 +0000850 my $local_part_regexp = qr/[^<>"\s@]+/;
851 my $domain_regexp = qr/[^.<>"\s@]+(?:\.[^.<>"\s@]+)+/;
Eric Wongdb3106b2006-05-15 02:41:01 -0700852
853 # check for a local address:
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700854 return $address if ($address =~ /^($local_part_regexp)$/);
Eric Wongdb3106b2006-05-15 02:41:01 -0700855
Uwe Kleine-König155197e2007-08-09 15:27:57 +0200856 $address =~ s/^\s*<(.*)>\s*$/$1/;
Eric Wong567ffeb2006-03-25 16:47:12 -0800857 if ($have_email_valid) {
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700858 return scalar Email::Valid->address($address);
Eric Wong567ffeb2006-03-25 16:47:12 -0800859 }
Krzysztof Mazur95c0d4b2012-11-22 19:12:09 +0100860
861 # less robust/correct than the monster regexp in Email::Valid,
862 # but still does a 99% job, and one less dependency
863 return $1 if $address =~ /($local_part_regexp\@$domain_regexp)/;
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -0700864 return;
Eric Wong567ffeb2006-03-25 16:47:12 -0800865}
Ryan Anderson83b24432005-07-31 04:17:25 -0400866
Krzysztof Mazure4312252012-11-22 19:12:10 +0100867sub extract_valid_address_or_die {
868 my $address = shift;
869 $address = extract_valid_address($address);
870 die "error: unable to extract a valid address from: $address\n"
871 if !$address;
872 return $address;
873}
874
875sub validate_address {
876 my $address = shift;
Krzysztof Mazurd0e98102012-11-22 19:12:12 +0100877 while (!extract_valid_address($address)) {
Krzysztof Mazur5c80afe2012-11-22 19:12:11 +0100878 print STDERR "error: unable to extract a valid address from: $address\n";
Krzysztof Mazurd0e98102012-11-22 19:12:12 +0100879 $_ = ask("What to do with this address? ([q]uit|[d]rop|[e]dit): ",
880 valid_re => qr/^(?:quit|q|drop|d|edit|e)/i,
Krzysztof Mazur5c80afe2012-11-22 19:12:11 +0100881 default => 'q');
882 if (/^d/i) {
883 return undef;
884 } elsif (/^q/i) {
885 cleanup_compose_files();
886 exit(0);
887 }
Krzysztof Mazurd0e98102012-11-22 19:12:12 +0100888 $address = ask("Who should the email be sent to (if any)? ",
889 default => "",
890 valid_re => qr/\@.*\./, confirm_only => 1);
Krzysztof Mazure4312252012-11-22 19:12:10 +0100891 }
892 return $address;
893}
894
895sub validate_address_list {
896 return (grep { defined $_ }
897 map { validate_address($_) } @_);
Ryan Anderson83b24432005-07-31 04:17:25 -0400898}
899
900# Usually don't need to change anything below here.
901
902# we make a "fake" message id by taking the current number
903# of seconds since the beginning of Unix time and tacking on
904# a random number to the end, in case we are called quicker than
905# 1 second since the last time we were called.
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400906
907# We'll setup a template for the message id, using the "from" address:
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400908
Junio C Hamanobe510cf2007-09-17 21:18:20 -0700909my ($message_id_stamp, $message_id_serial);
Brian Gernhardt68ce9332010-04-10 10:53:53 -0400910sub make_message_id {
Junio C Hamanobe510cf2007-09-17 21:18:20 -0700911 my $uniq;
912 if (!defined $message_id_stamp) {
913 $message_id_stamp = sprintf("%s-%s", time, $$);
914 $message_id_serial = 0;
915 }
916 $message_id_serial++;
917 $uniq = "$message_id_stamp-$message_id_serial";
918
Junio C Hamanoaeb59322007-06-20 13:47:34 -0700919 my $du_part;
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200920 for ($sender, $repocommitter, $repoauthor) {
921 $du_part = extract_valid_address(sanitize_address($_));
922 last if (defined $du_part and $du_part ne '');
Junio C Hamanoaeb59322007-06-20 13:47:34 -0700923 }
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200924 if (not defined $du_part or $du_part eq '') {
Ævar Arnfjörð Bjarmason529dd382010-09-30 13:43:08 +0000925 require Sys::Hostname;
Junio C Hamanoaeb59322007-06-20 13:47:34 -0700926 $du_part = 'user@' . Sys::Hostname::hostname();
927 }
Junio C Hamanobe510cf2007-09-17 21:18:20 -0700928 my $message_id_template = "<%s-git-send-email-%s>";
929 $message_id = sprintf($message_id_template, $uniq, $du_part);
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400930 #print "new message id = $message_id\n"; # Was useful for debugging
Ryan Anderson83b24432005-07-31 04:17:25 -0400931}
932
933
934
Eric Wonga5370b12006-03-25 03:01:01 -0800935$time = time - scalar $#files;
Ryan Anderson83b24432005-07-31 04:17:25 -0400936
Jürgen Rühle374c5902007-01-10 13:36:39 -0800937sub unquote_rfc2047 {
938 local ($_) = @_;
Роман Донченко11f70a72014-12-14 18:59:46 +0300939 my $charset;
Роман Донченкоab47e2a2014-12-14 18:59:47 +0300940 my $sep = qr/[ \t]+/;
941 s{$re_encoded_word(?:$sep$re_encoded_word)*}{
942 my @words = split $sep, $&;
943 foreach (@words) {
944 m/$re_encoded_word/;
945 $charset = $1;
946 my $encoding = $2;
947 my $text = $3;
948 if ($encoding eq 'q' || $encoding eq 'Q') {
949 $_ = $text;
950 s/_/ /g;
951 s/=([0-9A-F]{2})/chr(hex($1))/egi;
952 } else {
953 # other encodings not supported yet
954 }
Роман Донченко11f70a72014-12-14 18:59:46 +0300955 }
Роман Донченкоab47e2a2014-12-14 18:59:47 +0300956 join '', @words;
Thomas Rastb622d4d2012-07-30 21:25:40 +0200957 }eg;
Роман Донченко11f70a72014-12-14 18:59:46 +0300958 return wantarray ? ($_, $charset) : $_;
Jürgen Rühle374c5902007-01-10 13:36:39 -0800959}
960
Jeff Kingd54eaaa2008-03-28 17:29:01 -0400961sub quote_rfc2047 {
962 local $_ = shift;
Brandon Caseyd1fff6f2009-06-06 20:12:31 -0500963 my $encoding = shift || 'UTF-8';
Jeff Kingd54eaaa2008-03-28 17:29:01 -0400964 s/([^-a-zA-Z0-9!*+\/])/sprintf("=%02X", ord($1))/eg;
965 s/(.*)/=\?$encoding\?q\?$1\?=/;
966 return $_;
967}
968
Brandon Caseya3a82622009-06-07 19:25:58 -0500969sub is_rfc2047_quoted {
970 my $s = shift;
Brandon Caseya3a82622009-06-07 19:25:58 -0500971 length($s) <= 75 &&
Роман Донченко11f70a72014-12-14 18:59:46 +0300972 $s =~ m/^(?:"[[:ascii:]]*"|$re_encoded_word)$/o;
Brandon Caseya3a82622009-06-07 19:25:58 -0500973}
974
Krzysztof Mazurce547802012-10-24 23:08:26 +0200975sub subject_needs_rfc2047_quoting {
976 my $s = shift;
977
Krzysztof Mazurce1459f2012-10-24 23:28:29 +0200978 return ($s =~ /[^[:ascii:]]/) || ($s =~ /=\?/);
Krzysztof Mazurce547802012-10-24 23:08:26 +0200979}
980
981sub quote_subject {
982 local $subject = shift;
983 my $encoding = shift || 'UTF-8';
984
985 if (subject_needs_rfc2047_quoting($subject)) {
986 return quote_rfc2047($subject, $encoding);
987 }
988 return $subject;
989}
990
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +0200991# use the simplest quoting being able to handle the recipient
Brian Gernhardt68ce9332010-04-10 10:53:53 -0400992sub sanitize_address {
Robin H. Johnson732263d2007-04-25 19:37:19 -0700993 my ($recipient) = @_;
Krzysztof Mazur831a4882012-11-22 19:12:08 +0100994
995 # remove garbage after email address
996 $recipient =~ s/(.*>).*$/$1/;
997
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +0200998 my ($recipient_name, $recipient_addr) = ($recipient =~ /^(.*?)\s*(<.*)/);
999
1000 if (not $recipient_name) {
Ævar Arnfjörð Bjarmasonff483892010-09-30 13:43:02 +00001001 return $recipient;
Robin H. Johnson732263d2007-04-25 19:37:19 -07001002 }
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001003
1004 # if recipient_name is already quoted, do nothing
Brandon Caseya3a82622009-06-07 19:25:58 -05001005 if (is_rfc2047_quoted($recipient_name)) {
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001006 return $recipient;
1007 }
1008
1009 # rfc2047 is needed if a non-ascii char is included
1010 if ($recipient_name =~ /[^[:ascii:]]/) {
Jay Soffiana61c0ff2009-03-31 12:22:14 -04001011 $recipient_name =~ s/^"(.*)"$/$1/;
Jeff Kingd54eaaa2008-03-28 17:29:01 -04001012 $recipient_name = quote_rfc2047($recipient_name);
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001013 }
1014
1015 # double quotes are needed if specials or CTLs are included
1016 elsif ($recipient_name =~ /[][()<>@,;:\\".\000-\037\177]/) {
Horst H. von Brand18023c22008-03-28 11:09:04 -03001017 $recipient_name =~ s/(["\\\r])/\\$1/g;
Ævar Arnfjörð Bjarmasond5c7d692010-09-30 13:43:03 +00001018 $recipient_name = qq["$recipient_name"];
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +02001019 }
1020
1021 return "$recipient_name $recipient_addr";
1022
Robin H. Johnson732263d2007-04-25 19:37:19 -07001023}
1024
Krzysztof Mazure4312252012-11-22 19:12:10 +01001025sub sanitize_address_list {
1026 return (map { sanitize_address($_) } @_);
1027}
1028
Jari Aalto134550f2010-03-14 17:16:45 +02001029# Returns the local Fully Qualified Domain Name (FQDN) if available.
1030#
1031# Tightly configured MTAa require that a caller sends a real DNS
1032# domain name that corresponds the IP address in the HELO/EHLO
1033# handshake. This is used to verify the connection and prevent
1034# spammers from trying to hide their identity. If the DNS and IP don't
1035# match, the receiveing MTA may deny the connection.
1036#
1037# Here is a deny example of Net::SMTP with the default "localhost.localdomain"
1038#
1039# Net::SMTP=GLOB(0x267ec28)>>> EHLO localhost.localdomain
1040# Net::SMTP=GLOB(0x267ec28)<<< 550 EHLO argument does not match calling host
1041#
1042# This maildomain*() code is based on ideas in Perl library Test::Reporter
1043# /usr/share/perl5/Test/Reporter/Mail/Util.pm ==> sub _maildomain ()
1044
Brian Gernhardt59a86302010-04-10 10:53:54 -04001045sub valid_fqdn {
1046 my $domain = shift;
Brandon Casey61ef5e92010-09-26 22:18:01 -05001047 return defined $domain && !($^O eq 'darwin' && $domain =~ /\.local$/) && $domain =~ /\./;
Brian Gernhardt59a86302010-04-10 10:53:54 -04001048}
1049
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001050sub maildomain_net {
Jari Aalto134550f2010-03-14 17:16:45 +02001051 my $maildomain;
1052
1053 if (eval { require Net::Domain; 1 }) {
1054 my $domain = Net::Domain::domainname();
Brian Gernhardt59a86302010-04-10 10:53:54 -04001055 $maildomain = $domain if valid_fqdn($domain);
Jari Aalto134550f2010-03-14 17:16:45 +02001056 }
1057
1058 return $maildomain;
1059}
1060
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001061sub maildomain_mta {
Jari Aalto134550f2010-03-14 17:16:45 +02001062 my $maildomain;
1063
1064 if (eval { require Net::SMTP; 1 }) {
1065 for my $host (qw(mailhost localhost)) {
1066 my $smtp = Net::SMTP->new($host);
1067 if (defined $smtp) {
1068 my $domain = $smtp->domain;
1069 $smtp->quit;
1070
Brian Gernhardt59a86302010-04-10 10:53:54 -04001071 $maildomain = $domain if valid_fqdn($domain);
Jari Aalto134550f2010-03-14 17:16:45 +02001072
1073 last if $maildomain;
1074 }
1075 }
1076 }
1077
1078 return $maildomain;
1079}
1080
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001081sub maildomain {
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001082 return maildomain_net() || maildomain_mta() || 'localhost.localdomain';
Jari Aalto134550f2010-03-14 17:16:45 +02001083}
1084
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001085sub smtp_host_string {
1086 if (defined $smtp_server_port) {
1087 return "$smtp_server:$smtp_server_port";
1088 } else {
1089 return $smtp_server;
1090 }
1091}
1092
1093# Returns 1 if authentication succeeded or was not necessary
1094# (smtp_user was not specified), and 0 otherwise.
1095
1096sub smtp_auth_maybe {
1097 if (!defined $smtp_authuser || $auth) {
1098 return 1;
1099 }
1100
1101 # Workaround AUTH PLAIN/LOGIN interaction defect
1102 # with Authen::SASL::Cyrus
1103 eval {
1104 require Authen::SASL;
1105 Authen::SASL->import(qw(Perl));
1106 };
1107
1108 # TODO: Authentication may fail not because credentials were
1109 # invalid but due to other reasons, in which we should not
1110 # reject credentials.
1111 $auth = Git::credential({
1112 'protocol' => 'smtp',
1113 'host' => smtp_host_string(),
1114 'username' => $smtp_authuser,
1115 # if there's no password, "git credential fill" will
1116 # give us one, otherwise it'll just pass this one.
1117 'password' => $smtp_authpass
1118 }, sub {
1119 my $cred = shift;
1120 return !!$smtp->auth($cred->{'username'}, $cred->{'password'});
1121 });
1122
1123 return $auth;
1124}
1125
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001126sub ssl_verify_params {
1127 eval {
1128 require IO::Socket::SSL;
1129 IO::Socket::SSL->import(qw/SSL_VERIFY_PEER SSL_VERIFY_NONE/);
1130 };
1131 if ($@) {
1132 print STDERR "Not using SSL_VERIFY_PEER due to out-of-date IO::Socket::SSL.\n";
1133 return;
1134 }
1135
1136 if (!defined $smtp_ssl_cert_path) {
Ruben Kerkhof01645b72014-01-15 21:31:11 +04001137 # use the OpenSSL defaults
1138 return (SSL_verify_mode => SSL_VERIFY_PEER());
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001139 }
1140
1141 if ($smtp_ssl_cert_path eq "") {
1142 return (SSL_verify_mode => SSL_VERIFY_NONE());
1143 } elsif (-d $smtp_ssl_cert_path) {
1144 return (SSL_verify_mode => SSL_VERIFY_PEER(),
1145 SSL_ca_path => $smtp_ssl_cert_path);
1146 } elsif (-f $smtp_ssl_cert_path) {
1147 return (SSL_verify_mode => SSL_VERIFY_PEER(),
1148 SSL_ca_file => $smtp_ssl_cert_path);
1149 } else {
1150 print STDERR "Not using SSL_VERIFY_PEER because the CA path does not exist.\n";
1151 return (SSL_verify_mode => SSL_VERIFY_NONE());
1152 }
1153}
1154
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001155sub file_name_is_absolute {
1156 my ($path) = @_;
1157
1158 # msys does not grok DOS drive-prefixes
1159 if ($^O eq 'msys') {
Junio C Hamanof24ecf52014-04-23 09:37:38 -07001160 return ($path =~ m#^/# || $path =~ m#^[a-zA-Z]\:#)
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001161 }
1162
1163 require File::Spec::Functions;
1164 return File::Spec::Functions::file_name_is_absolute($path);
1165}
1166
Michael Witten15da1082009-04-13 13:23:51 -05001167# Returns 1 if the message was sent, and 0 otherwise.
Markus Heidelberga1b5b372009-06-12 12:51:42 +02001168# In actuality, the whole program dies when there
Michael Witten15da1082009-04-13 13:23:51 -05001169# is an error sending a message.
1170
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001171sub send_message {
Eric Wong4bc87a22006-03-25 17:20:48 -08001172 my @recipients = unique_email_list(@to);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001173 @cc = (grep { my $cc = extract_valid_address_or_die($_);
Joe Perches83acaae2010-11-20 15:06:05 -08001174 not grep { $cc eq $_ || $_ =~ /<\Q${cc}\E>$/ } @recipients
Ask Bjørn Hansen7ac17522007-11-19 03:00:26 -08001175 }
Ask Bjørn Hansen7ac17522007-11-19 03:00:26 -08001176 @cc);
Eric Wong4bc87a22006-03-25 17:20:48 -08001177 my $to = join (",\n\t", @recipients);
Ryan Anderson58063242006-05-29 12:30:13 -07001178 @recipients = unique_email_list(@recipients,@cc,@bcclist);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001179 @recipients = (map { extract_valid_address_or_die($_) } @recipients);
Jakub Narebski6bdca892006-07-07 20:57:55 +02001180 my $date = format_2822_time($time++);
Martin Langhoffe923eff2006-05-03 09:44:36 +12001181 my $gitversion = '@@GIT_VERSION@@';
1182 if ($gitversion =~ m/..GIT_VERSION../) {
Petr Baudis3cb8caf2006-07-03 22:47:58 +02001183 $gitversion = Git::version();
Martin Langhoffe923eff2006-05-03 09:44:36 +12001184 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001185
Joe Perches02461e02009-10-08 10:03:26 -07001186 my $cc = join(",\n\t", unique_email_list(@cc));
Junio C Hamanof06a6a42007-04-16 16:51:47 -07001187 my $ccline = "";
1188 if ($cc ne '') {
1189 $ccline = "\nCc: $cc";
1190 }
Jeff King4f3d3702007-12-17 15:51:34 -05001191 make_message_id() unless defined($message_id);
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001192
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001193 my $header = "From: $sender
Junio C Hamanof06a6a42007-04-16 16:51:47 -07001194To: $to${ccline}
Eric Wong4bc87a22006-03-25 17:20:48 -08001195Subject: $subject
Eric Wong4bc87a22006-03-25 17:20:48 -08001196Date: $date
1197Message-Id: $message_id
Eric Wong4bc87a22006-03-25 17:20:48 -08001198";
Luis Henriquesac1596a2014-03-24 21:38:27 +00001199 if ($use_xmailer) {
1200 $header .= "X-Mailer: git-send-email $gitversion\n";
1201 }
Thomas Rast3e0c4ff2009-03-01 23:45:41 +01001202 if ($reply_to) {
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001203
1204 $header .= "In-Reply-To: $reply_to\n";
1205 $header .= "References: $references\n";
1206 }
Junio C Hamanoce91c2f2006-10-05 16:36:49 -07001207 if (@xh) {
1208 $header .= join("\n", @xh) . "\n";
1209 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001210
Robin H. Johnsonc38f0242007-04-25 19:37:20 -07001211 my @sendmail_parameters = ('-i', @recipients);
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001212 my $raw_from = $sender;
Felipe Contrerasc89e3242009-11-26 21:04:29 +02001213 if (defined $envelope_sender && $envelope_sender ne "auto") {
1214 $raw_from = $envelope_sender;
1215 }
Robin H. Johnsonf073a592007-04-25 19:37:22 -07001216 $raw_from = extract_valid_address($raw_from);
1217 unshift (@sendmail_parameters,
1218 '-f', $raw_from) if(defined $envelope_sender);
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001219
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001220 if ($needs_confirm && !$dry_run) {
1221 print "\n$header\n";
1222 if ($needs_confirm eq "inform") {
1223 $confirm_unconfigured = 0; # squelch this message for the rest of this run
Jay Soffian6e182512009-03-28 21:39:10 -04001224 $ask_default = "y"; # assume yes on EOF since user hasn't explicitly asked for confirmation
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001225 print " The Cc list above has been expanded by additional\n";
1226 print " addresses found in the patch commit message. By default\n";
1227 print " send-email prompts before sending whenever this occurs.\n";
1228 print " This behavior is controlled by the sendemail.confirm\n";
1229 print " configuration setting.\n";
1230 print "\n";
1231 print " For additional information, run 'git send-email --help'.\n";
1232 print " To retain the current behavior, but squelch this message,\n";
1233 print " run 'git config --global sendemail.confirm auto'.\n\n";
1234 }
Jay Soffian6e182512009-03-28 21:39:10 -04001235 $_ = ask("Send this email? ([y]es|[n]o|[q]uit|[a]ll): ",
1236 valid_re => qr/^(?:yes|y|no|n|quit|q|all|a)/i,
1237 default => $ask_default);
1238 die "Send this email reply required" unless defined $_;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001239 if (/^n/i) {
Michael Witten15da1082009-04-13 13:23:51 -05001240 return 0;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001241 } elsif (/^q/i) {
1242 cleanup_compose_files();
1243 exit(0);
1244 } elsif (/^a/i) {
1245 $confirm = 'never';
1246 }
1247 }
1248
Pascal Obry052fbea2010-09-06 20:12:11 +02001249 unshift (@sendmail_parameters, @smtp_server_options);
1250
Matthew Wilcox61302592006-10-10 08:58:23 -06001251 if ($dry_run) {
1252 # We don't want to send the email.
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001253 } elsif (file_name_is_absolute($smtp_server)) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001254 my $pid = open my $sm, '|-';
1255 defined $pid or die $!;
1256 if (!$pid) {
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001257 exec($smtp_server, @sendmail_parameters) or die $!;
Eric Wongaca7ad72006-05-15 02:34:44 -07001258 }
1259 print $sm "$header\n$message";
Ævar Arnfjörð Bjarmason5e2c2ab2010-09-30 13:43:07 +00001260 close $sm or die $!;
Eric Wongaca7ad72006-05-15 02:34:44 -07001261 } else {
Junio C Hamano44b24762007-09-25 17:27:54 -07001262
1263 if (!defined $smtp_server) {
1264 die "The required SMTP server is not properly defined."
1265 }
1266
Thomas Rastf6bebd12008-06-25 21:42:43 +02001267 if ($smtp_encryption eq 'ssl') {
Junio C Hamano44b24762007-09-25 17:27:54 -07001268 $smtp_server_port ||= 465; # ssmtp
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001269 require Net::SMTP::SSL;
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001270 $smtp_domain ||= maildomain();
Thomas Rast5508f3e2013-12-01 23:48:43 +01001271 require IO::Socket::SSL;
1272 # Net::SMTP::SSL->new() does not forward any SSL options
1273 IO::Socket::SSL::set_client_defaults(
1274 ssl_verify_params());
Jari Aalto134550f2010-03-14 17:16:45 +02001275 $smtp ||= Net::SMTP::SSL->new($smtp_server,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001276 Hello => $smtp_domain,
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001277 Port => $smtp_server_port,
Thomas Rast5508f3e2013-12-01 23:48:43 +01001278 Debug => $debug_net_smtp);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001279 }
1280 else {
1281 require Net::SMTP;
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001282 $smtp_domain ||= maildomain();
brian m. carlson1a741bf2013-07-04 22:04:52 +00001283 $smtp_server_port ||= 25;
1284 $smtp ||= Net::SMTP->new($smtp_server,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001285 Hello => $smtp_domain,
brian m. carlson1a741bf2013-07-04 22:04:52 +00001286 Debug => $debug_net_smtp,
1287 Port => $smtp_server_port);
Yakov Lernerfb3650e2009-09-25 15:10:21 -07001288 if ($smtp_encryption eq 'tls' && $smtp) {
Thomas Rastf6bebd12008-06-25 21:42:43 +02001289 require Net::SMTP::SSL;
1290 $smtp->command('STARTTLS');
1291 $smtp->response();
1292 if ($smtp->code == 220) {
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001293 $smtp = Net::SMTP::SSL->start_SSL($smtp,
1294 ssl_verify_params())
Brian M. Carlson6cb0c882013-09-08 20:54:34 +00001295 or die "STARTTLS failed! ".IO::Socket::SSL::errstr();
Thomas Rast6cbf8b02008-07-03 00:11:31 +02001296 $smtp_encryption = '';
Robert Shearman9d1ccf52008-07-09 22:39:40 +01001297 # Send EHLO again to receive fresh
1298 # supported commands
Matthew Daley155b9402011-10-15 04:44:52 -04001299 $smtp->hello($smtp_domain);
Thomas Rastf6bebd12008-06-25 21:42:43 +02001300 } else {
1301 die "Server does not support STARTTLS! ".$smtp->message;
1302 }
1303 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001304 }
Junio C Hamano44b24762007-09-25 17:27:54 -07001305
1306 if (!$smtp) {
Jari Aaltof60812e2010-03-14 17:16:09 +02001307 die "Unable to initialize SMTP properly. Check config and use --smtp-debug. ",
Jari Aaltoe5afb3a2010-03-14 17:15:33 +02001308 "VALUES: server=$smtp_server ",
1309 "encryption=$smtp_encryption ",
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001310 "hello=$smtp_domain",
Sylvain Rabota1dd7e12011-04-29 20:23:24 +02001311 defined $smtp_server_port ? " port=$smtp_server_port" : "";
Junio C Hamano44b24762007-09-25 17:27:54 -07001312 }
1313
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001314 smtp_auth_maybe or die $smtp->message;
Michael Witten2363d742008-02-03 19:53:56 -05001315
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001316 $smtp->mail( $raw_from ) or die $smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001317 $smtp->to( @recipients ) or die $smtp->message;
1318 $smtp->data or die $smtp->message;
1319 $smtp->datasend("$header\n$message") or die $smtp->message;
1320 $smtp->dataend() or die $smtp->message;
Michael Witten15da1082009-04-13 13:23:51 -05001321 $smtp->code =~ /250|200/ or die "Failed to send $subject\n".$smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001322 }
Ryan Anderson27184352006-02-05 20:13:52 -05001323 if ($quiet) {
Robin H. Johnson71c7da92007-04-25 19:37:16 -07001324 printf (($dry_run ? "Dry-" : "")."Sent %s\n", $subject);
Ryan Anderson27184352006-02-05 20:13:52 -05001325 } else {
David D. Kilzerb7f30e02007-11-18 20:14:55 -08001326 print (($dry_run ? "Dry-" : "")."OK. Log says:\n");
Erik Faye-Lundcb005c12014-04-16 10:08:18 +02001327 if (!file_name_is_absolute($smtp_server)) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001328 print "Server: $smtp_server\n";
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001329 print "MAIL FROM:<$raw_from>\n";
Joe Perches02461e02009-10-08 10:03:26 -07001330 foreach my $entry (@recipients) {
1331 print "RCPT TO:<$entry>\n";
1332 }
Eric Wongaca7ad72006-05-15 02:34:44 -07001333 } else {
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001334 print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001335 }
David D. Kilzerb7f30e02007-11-18 20:14:55 -08001336 print $header, "\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001337 if ($smtp) {
1338 print "Result: ", $smtp->code, ' ',
1339 ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
1340 } else {
1341 print "Result: OK\n";
1342 }
Ryan Anderson30d08b32006-02-02 11:56:06 -05001343 }
Michael Witten15da1082009-04-13 13:23:51 -05001344
1345 return 1;
Ryan Anderson83b24432005-07-31 04:17:25 -04001346}
1347
Ryan Anderson83b24432005-07-31 04:17:25 -04001348$reply_to = $initial_reply_to;
Junio C Hamano2186d562006-05-29 23:53:13 -07001349$references = $initial_reply_to || '';
Ryan Anderson83b24432005-07-31 04:17:25 -04001350$subject = $initial_subject;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001351$message_num = 0;
Ryan Anderson83b24432005-07-31 04:17:25 -04001352
1353foreach my $t (@files) {
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001354 open my $fh, "<", $t or die "can't open file $t";
Ryan Anderson83b24432005-07-31 04:17:25 -04001355
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001356 my $author = undef;
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001357 my $sauthor = undef;
Jeff King8291db62007-11-16 05:49:09 -05001358 my $author_encoding;
1359 my $has_content_type;
1360 my $body_encoding;
Paolo Bonzinibb294562014-11-25 15:00:26 +01001361 my $xfer_encoding;
1362 my $has_mime_version;
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001363 @to = ();
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001364 @cc = ();
Junio C Hamanoce91c2f2006-10-05 16:36:49 -07001365 @xh = ();
Junio C Hamanoe6b09642006-10-07 03:09:05 -07001366 my $input_format = undef;
Jay Soffian50126992009-02-14 23:32:14 -05001367 my @header = ();
Ryan Anderson83b24432005-07-31 04:17:25 -04001368 $message = "";
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001369 $message_num++;
Jay Soffian50126992009-02-14 23:32:14 -05001370 # First unfold multiline header fields
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001371 while(<$fh>) {
Jay Soffian50126992009-02-14 23:32:14 -05001372 last if /^\s*$/;
1373 if (/^\s+\S/ and @header) {
1374 chomp($header[$#header]);
1375 s/^\s+/ /;
1376 $header[$#header] .= $_;
1377 } else {
1378 push(@header, $_);
1379 }
1380 }
1381 # Now parse the header
1382 foreach(@header) {
1383 if (/^From /) {
1384 $input_format = 'mbox';
1385 next;
1386 }
1387 chomp;
1388 if (!defined $input_format && /^[-A-Za-z]+:\s/) {
1389 $input_format = 'mbox';
1390 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001391
Jay Soffian50126992009-02-14 23:32:14 -05001392 if (defined $input_format && $input_format eq 'mbox') {
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001393 if (/^Subject:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001394 $subject = $1;
1395 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001396 elsif (/^From:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001397 ($author, $author_encoding) = unquote_rfc2047($1);
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001398 $sauthor = sanitize_address($author);
Jay Soffian50126992009-02-14 23:32:14 -05001399 next if $suppress_cc{'author'};
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001400 next if $suppress_cc{'self'} and $sauthor eq $sender;
Jay Soffian50126992009-02-14 23:32:14 -05001401 printf("(mbox) Adding cc: %s from line '%s'\n",
1402 $1, $_) unless $quiet;
1403 push @cc, $1;
1404 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001405 elsif (/^To:\s+(.*)$/i) {
Stephen Boyd21802cd2010-09-29 00:26:44 -07001406 foreach my $addr (parse_address_line($1)) {
1407 printf("(mbox) Adding to: %s from line '%s'\n",
1408 $addr, $_) unless $quiet;
Krzysztof Mazure4312252012-11-22 19:12:10 +01001409 push @to, $addr;
Stephen Boyd21802cd2010-09-29 00:26:44 -07001410 }
1411 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001412 elsif (/^Cc:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001413 foreach my $addr (parse_address_line($1)) {
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001414 my $qaddr = unquote_rfc2047($addr);
1415 my $saddr = sanitize_address($qaddr);
1416 if ($saddr eq $sender) {
David Brown65648282007-12-25 19:56:29 -08001417 next if ($suppress_cc{'self'});
David Brown65648282007-12-25 19:56:29 -08001418 } else {
1419 next if ($suppress_cc{'cc'});
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001420 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001421 printf("(mbox) Adding cc: %s from line '%s'\n",
Jay Soffian50126992009-02-14 23:32:14 -05001422 $addr, $_) unless $quiet;
1423 push @cc, $addr;
Ryan Anderson83b24432005-07-31 04:17:25 -04001424 }
1425 }
Jay Soffian50126992009-02-14 23:32:14 -05001426 elsif (/^Content-type:/i) {
1427 $has_content_type = 1;
1428 if (/charset="?([^ "]+)/) {
1429 $body_encoding = $1;
1430 }
1431 push @xh, $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001432 }
Paolo Bonzinibb294562014-11-25 15:00:26 +01001433 elsif (/^MIME-Version/i) {
1434 $has_mime_version = 1;
1435 push @xh, $_;
1436 }
Jay Soffian50126992009-02-14 23:32:14 -05001437 elsif (/^Message-Id: (.*)/i) {
1438 $message_id = $1;
1439 }
Paolo Bonzinibb294562014-11-25 15:00:26 +01001440 elsif (/^Content-Transfer-Encoding: (.*)/i) {
1441 $xfer_encoding = $1 if not defined $xfer_encoding;
1442 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001443 elsif (!/^Date:\s/i && /^[-A-Za-z]+:\s+\S/) {
Jay Soffian50126992009-02-14 23:32:14 -05001444 push @xh, $_;
1445 }
1446
Ryan Anderson83b24432005-07-31 04:17:25 -04001447 } else {
Jay Soffian50126992009-02-14 23:32:14 -05001448 # In the traditional
1449 # "send lots of email" format,
1450 # line 1 = cc
1451 # line 2 = subject
1452 # So let's support that, too.
1453 $input_format = 'lots';
1454 if (@cc == 0 && !$suppress_cc{'cc'}) {
1455 printf("(non-mbox) Adding cc: %s from line '%s'\n",
1456 $_, $_) unless $quiet;
1457 push @cc, $_;
1458 } elsif (!defined $subject) {
1459 $subject = $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001460 }
1461 }
1462 }
Jay Soffian50126992009-02-14 23:32:14 -05001463 # Now parse the message body
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001464 while(<$fh>) {
Jay Soffian50126992009-02-14 23:32:14 -05001465 $message .= $_;
1466 if (/^(Signed-off-by|Cc): (.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001467 chomp;
Jay Soffian3531e272009-02-14 23:32:15 -05001468 my ($what, $c) = ($1, $2);
Jay Soffian50126992009-02-14 23:32:14 -05001469 chomp $c;
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001470 my $sc = sanitize_address($c);
1471 if ($sc eq $sender) {
Jay Soffian3531e272009-02-14 23:32:15 -05001472 next if ($suppress_cc{'self'});
1473 } else {
1474 next if $suppress_cc{'sob'} and $what =~ /Signed-off-by/i;
1475 next if $suppress_cc{'bodycc'} and $what =~ /Cc/i;
1476 }
Jay Soffian50126992009-02-14 23:32:14 -05001477 push @cc, $c;
Jay Soffian3531e272009-02-14 23:32:15 -05001478 printf("(body) Adding cc: %s from line '%s'\n",
Jay Soffian50126992009-02-14 23:32:14 -05001479 $c, $_) unless $quiet;
1480 }
1481 }
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001482 close $fh;
Joe Perches324a8bd2007-08-17 18:51:12 -07001483
Joe Perches6e74e072010-09-24 10:03:00 -07001484 push @to, recipients_cmd("to-cmd", "to", $to_cmd, $t)
1485 if defined $to_cmd;
1486 push @cc, recipients_cmd("cc-cmd", "cc", $cc_cmd, $t)
1487 if defined $cc_cmd && !$suppress_cc{'cccmd'};
Joe Perches324a8bd2007-08-17 18:51:12 -07001488
Thomas Rast3cae7e52010-06-17 22:10:39 +02001489 if ($broken_encoding{$t} && !$has_content_type) {
Paolo Bonzinibb294562014-11-25 15:00:26 +01001490 $xfer_encoding = '8bit' if not defined $xfer_encoding;
Thomas Rast3cae7e52010-06-17 22:10:39 +02001491 $has_content_type = 1;
Paolo Bonzinibb294562014-11-25 15:00:26 +01001492 push @xh, "Content-Type: text/plain; charset=$auto_8bit_encoding";
Thomas Rast3cae7e52010-06-17 22:10:39 +02001493 $body_encoding = $auto_8bit_encoding;
1494 }
1495
Krzysztof Mazurce547802012-10-24 23:08:26 +02001496 if ($broken_encoding{$t} && !is_rfc2047_quoted($subject)) {
1497 $subject = quote_subject($subject, $auto_8bit_encoding);
Thomas Rast3cae7e52010-06-17 22:10:39 +02001498 }
1499
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001500 if (defined $sauthor and $sauthor ne $sender) {
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001501 $message = "From: $author\n\n$message";
Jeff King8291db62007-11-16 05:49:09 -05001502 if (defined $author_encoding) {
1503 if ($has_content_type) {
1504 if ($body_encoding eq $author_encoding) {
1505 # ok, we already have the right encoding
1506 }
1507 else {
1508 # uh oh, we should re-encode
1509 }
1510 }
1511 else {
Paolo Bonzinibb294562014-11-25 15:00:26 +01001512 $xfer_encoding = '8bit' if not defined $xfer_encoding;
Thomas Rast3cae7e52010-06-17 22:10:39 +02001513 $has_content_type = 1;
Jeff King8291db62007-11-16 05:49:09 -05001514 push @xh,
Paolo Bonzinibb294562014-11-25 15:00:26 +01001515 "Content-Type: text/plain; charset=$author_encoding";
Jeff King8291db62007-11-16 05:49:09 -05001516 }
1517 }
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001518 }
Paolo Bonzini8d814082014-11-25 15:00:27 +01001519 if (defined $target_xfer_encoding) {
1520 $xfer_encoding = '8bit' if not defined $xfer_encoding;
1521 $message = apply_transfer_encoding(
1522 $message, $xfer_encoding, $target_xfer_encoding);
1523 $xfer_encoding = $target_xfer_encoding;
1524 }
Paolo Bonzinibb294562014-11-25 15:00:26 +01001525 if (defined $xfer_encoding) {
1526 push @xh, "Content-Transfer-Encoding: $xfer_encoding";
1527 }
1528 if (defined $xfer_encoding or $has_content_type) {
1529 unshift @xh, 'MIME-Version: 1.0' unless $has_mime_version;
1530 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001531
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001532 $needs_confirm = (
1533 $confirm eq "always" or
1534 ($confirm =~ /^(?:auto|cc)$/ && @cc) or
1535 ($confirm =~ /^(?:auto|compose)$/ && $compose && $message_num == 1));
1536 $needs_confirm = "inform" if ($needs_confirm && $confirm_unconfigured && @cc);
1537
Krzysztof Mazure4312252012-11-22 19:12:10 +01001538 @to = validate_address_list(sanitize_address_list(@to));
1539 @cc = validate_address_list(sanitize_address_list(@cc));
1540
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001541 @to = (@initial_to, @to);
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001542 @cc = (@initial_cc, @cc);
1543
Michael S. Tsirkinf515c902014-04-29 08:41:16 +03001544 if ($message_num == 1) {
1545 if (defined $cover_cc and $cover_cc) {
1546 @initial_cc = @cc;
1547 }
1548 if (defined $cover_to and $cover_to) {
1549 @initial_to = @to;
1550 }
1551 }
1552
Michael Witten15da1082009-04-13 13:23:51 -05001553 my $message_was_sent = send_message();
Ryan Anderson83b24432005-07-31 04:17:25 -04001554
1555 # set up for the next message
Junio C Hamano95a877a2009-06-12 09:23:43 -07001556 if ($thread && $message_was_sent &&
Felipe Contrerasb99d22f2013-05-24 22:44:52 -05001557 ($chain_reply_to || !defined $reply_to || length($reply_to) == 0 ||
Antonio Ospitedb54c8e2010-11-12 15:55:08 +01001558 $message_num == 1)) {
Ryan Anderson78488b22005-07-31 20:04:24 -04001559 $reply_to = $message_id;
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001560 if (length $references > 0) {
YOSHIFUJI Hideaki / 吉藤英明a925b892007-04-06 08:50:24 +09001561 $references .= "\n $message_id";
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001562 } else {
1563 $references = "$message_id";
1564 }
Ryan Anderson78488b22005-07-31 20:04:24 -04001565 }
Jeff King4f3d3702007-12-17 15:51:34 -05001566 $message_id = undef;
Ryan Anderson83b24432005-07-31 04:17:25 -04001567}
Ryan Andersone2057352005-08-02 21:45:22 -04001568
Joe Perches6e74e072010-09-24 10:03:00 -07001569# Execute a command (e.g. $to_cmd) to get a list of email addresses
1570# and return a results array
1571sub recipients_cmd {
1572 my ($prefix, $what, $cmd, $file) = @_;
1573
Joe Perches6e74e072010-09-24 10:03:00 -07001574 my @addresses = ();
Ramkumar Ramachandraa47eab02013-03-31 18:40:42 -07001575 open my $fh, "-|", "$cmd \Q$file\E"
Joe Perches6e74e072010-09-24 10:03:00 -07001576 or die "($prefix) Could not execute '$cmd'";
Junio C Hamano7ebee442010-10-26 22:02:52 -07001577 while (my $address = <$fh>) {
Joe Perches6e74e072010-09-24 10:03:00 -07001578 $address =~ s/^\s*//g;
1579 $address =~ s/\s*$//g;
1580 $address = sanitize_address($address);
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001581 next if ($address eq $sender and $suppress_cc{'self'});
Joe Perches6e74e072010-09-24 10:03:00 -07001582 push @addresses, $address;
1583 printf("($prefix) Adding %s: %s from: '%s'\n",
1584 $what, $address, $cmd) unless $quiet;
1585 }
Junio C Hamano7ebee442010-10-26 22:02:52 -07001586 close $fh
Joe Perches6e74e072010-09-24 10:03:00 -07001587 or die "($prefix) failed to close pipe to '$cmd'";
1588 return @addresses;
1589}
1590
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001591cleanup_compose_files();
Ryan Anderson1f038a02005-09-05 01:13:07 -04001592
Ævar Arnfjörð Bjarmason4bf597e2010-09-30 13:43:00 +00001593sub cleanup_compose_files {
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001594 unlink($compose_filename, $compose_filename . ".final") if $compose;
Ryan Anderson1f038a02005-09-05 01:13:07 -04001595}
1596
Eric Wong4bc87a22006-03-25 17:20:48 -08001597$smtp->quit if $smtp;
Ryan Andersone2057352005-08-02 21:45:22 -04001598
Paolo Bonzini8d814082014-11-25 15:00:27 +01001599sub apply_transfer_encoding {
1600 my $message = shift;
1601 my $from = shift;
1602 my $to = shift;
1603
1604 return $message if ($from eq $to and $from ne '7bit');
1605
1606 require MIME::QuotedPrint;
1607 require MIME::Base64;
1608
1609 $message = MIME::QuotedPrint::decode($message)
1610 if ($from eq 'quoted-printable');
1611 $message = MIME::Base64::decode($message)
1612 if ($from eq 'base64');
1613
1614 die "cannot send message as 7bit"
1615 if ($to eq '7bit' and $message =~ /[^[:ascii:]]/);
1616 return $message
1617 if ($to eq '7bit' or $to eq '8bit');
1618 return MIME::QuotedPrint::encode($message, "\n", 0)
1619 if ($to eq 'quoted-printable');
1620 return MIME::Base64::encode($message, "\n")
1621 if ($to eq 'base64');
1622 die "invalid transfer encoding";
1623}
1624
Ævar Arnfjörð Bjarmasonc438ea22010-09-30 13:42:59 +00001625sub unique_email_list {
Ryan Andersone2057352005-08-02 21:45:22 -04001626 my %seen;
1627 my @emails;
1628
1629 foreach my $entry (@_) {
Krzysztof Mazure4312252012-11-22 19:12:10 +01001630 my $clean = extract_valid_address_or_die($entry);
1631 $seen{$clean} ||= 0;
1632 next if $seen{$clean}++;
1633 push @emails, $entry;
Ryan Andersone2057352005-08-02 21:45:22 -04001634 }
1635 return @emails;
1636}
Jeff King747bbff2008-01-18 09:19:48 -05001637
1638sub validate_patch {
1639 my $fn = shift;
1640 open(my $fh, '<', $fn)
1641 or die "unable to open $fn: $!\n";
1642 while (my $line = <$fh>) {
1643 if (length($line) > 998) {
1644 return "$.: patch contains a line longer than 998 characters";
1645 }
1646 }
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -07001647 return;
Jeff King747bbff2008-01-18 09:19:48 -05001648}
Jeff King0706bd12008-03-28 17:28:33 -04001649
1650sub file_has_nonascii {
1651 my $fn = shift;
1652 open(my $fh, '<', $fn)
1653 or die "unable to open $fn: $!\n";
1654 while (my $line = <$fh>) {
1655 return 1 if $line =~ /[^[:ascii:]]/;
1656 }
1657 return 0;
1658}
Thomas Rast3cae7e52010-06-17 22:10:39 +02001659
1660sub body_or_subject_has_nonascii {
1661 my $fn = shift;
1662 open(my $fh, '<', $fn)
1663 or die "unable to open $fn: $!\n";
1664 while (my $line = <$fh>) {
1665 last if $line =~ /^$/;
1666 return 1 if $line =~ /^Subject.*[^[:ascii:]]/;
1667 }
1668 while (my $line = <$fh>) {
1669 return 1 if $line =~ /[^[:ascii:]]/;
1670 }
1671 return 0;
1672}