blob: 671762b93031e66cba2ca837ddf71ed765f7a204 [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:"
Felipe Contreras402596a2013-04-07 01:10:27 -060057 --[no-]annotate * Review each patch that will be sent in an editor.
Michael Witten4ed62b02008-09-30 07:58:30 -050058 --compose * Open an editor for introduction.
Krzysztof Mazur62e00692012-10-10 01:02:56 +020059 --compose-encoding <str> * Encoding to assume for introduction.
Thomas Rast3cae7e52010-06-17 22:10:39 +020060 --8bit-encoding <str> * Encoding to assume 8bit mails if undeclared
Michael Witten4ed62b02008-09-30 07:58:30 -050061
62 Sending:
63 --envelope-sender <str> * Email envelope sender.
64 --smtp-server <str:int> * Outgoing SMTP server to use. The port
65 is optional. Default 'localhost'.
Pascal Obry052fbea2010-09-06 20:12:11 +020066 --smtp-server-option <str> * Outgoing SMTP server option to use.
Michael Witten4ed62b02008-09-30 07:58:30 -050067 --smtp-server-port <int> * Outgoing SMTP server port.
68 --smtp-user <str> * Username for SMTP-AUTH.
69 --smtp-pass <str> * Password for SMTP-AUTH; not necessary.
70 --smtp-encryption <str> * tls or ssl; anything else disables.
71 --smtp-ssl * Deprecated. Use '--smtp-encryption ssl'.
Jari Aalto134550f2010-03-14 17:16:45 +020072 --smtp-domain <str> * The domain name sent to HELO/EHLO handshake
Jari Aaltof60812e2010-03-14 17:16:09 +020073 --smtp-debug <0|1> * Disable, enable Net::SMTP debug.
Michael Witten4ed62b02008-09-30 07:58:30 -050074
75 Automating:
76 --identity <str> * Use the sendemail.<id> options.
Joe Perches6e74e072010-09-24 10:03:00 -070077 --to-cmd <str> * Email To: via `<str> \$patch_path`
Michael Witten4ed62b02008-09-30 07:58:30 -050078 --cc-cmd <str> * Email Cc: via `<str> \$patch_path`
Jay Soffian3531e272009-02-14 23:32:15 -050079 --suppress-cc <str> * author, self, sob, cc, cccmd, body, bodycc, all.
80 --[no-]signed-off-by-cc * Send to Signed-off-by: addresses. Default on.
Michael Witten4ed62b02008-09-30 07:58:30 -050081 --[no-]suppress-from * Send to self. Default off.
Junio C Hamano41fe87f2009-08-22 12:48:48 -070082 --[no-]chain-reply-to * Chain In-Reply-To: fields. Default off.
Michael Witten4ed62b02008-09-30 07:58:30 -050083 --[no-]thread * Use In-Reply-To: field. Default on.
84
85 Administering:
Jay Soffianc1f2aa42009-03-02 23:52:18 -050086 --confirm <str> * Confirm recipients before sending;
87 auto, cc, compose, always, or never.
Michael Witten4ed62b02008-09-30 07:58:30 -050088 --quiet * Output one line of info per email.
89 --dry-run * Don't actually send the emails.
90 --[no-]validate * Perform patch sanity checks. Default on.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +010091 --[no-]format-patch * understand any non optional arguments as
92 `git format-patch` ones.
Thomas Rasta03bc5b2009-06-08 23:34:12 +020093 --force * Send even if safety checks would prevent it.
Jeff Kingc764a0c2008-01-18 09:20:10 -050094
Michael Coleman1b0baf12007-02-27 22:47:54 -060095EOT
96 exit(1);
97}
98
Eric Wong4bc87a22006-03-25 17:20:48 -080099# most mail servers generate the Date: header, but not all...
Jakub Narebski6bdca892006-07-07 20:57:55 +0200100sub format_2822_time {
101 my ($time) = @_;
102 my @localtm = localtime($time);
103 my @gmttm = gmtime($time);
104 my $localmin = $localtm[1] + $localtm[2] * 60;
105 my $gmtmin = $gmttm[1] + $gmttm[2] * 60;
106 if ($localtm[0] != $gmttm[0]) {
107 die "local zone differs from GMT by a non-minute interval\n";
108 }
109 if ((($gmttm[6] + 1) % 7) == $localtm[6]) {
110 $localmin += 1440;
111 } elsif ((($gmttm[6] - 1) % 7) == $localtm[6]) {
112 $localmin -= 1440;
113 } elsif ($gmttm[6] != $localtm[6]) {
114 die "local time offset greater than or equal to 24 hours\n";
115 }
116 my $offset = $localmin - $gmtmin;
117 my $offhour = $offset / 60;
118 my $offmin = abs($offset % 60);
119 if (abs($offhour) >= 24) {
120 die ("local time offset greater than or equal to 24 hours\n");
121 }
122
123 return sprintf("%s, %2d %s %d %02d:%02d:%02d %s%02d%02d",
124 qw(Sun Mon Tue Wed Thu Fri Sat)[$localtm[6]],
125 $localtm[3],
126 qw(Jan Feb Mar Apr May Jun
127 Jul Aug Sep Oct Nov Dec)[$localtm[4]],
128 $localtm[5]+1900,
129 $localtm[2],
130 $localtm[1],
131 $localtm[0],
132 ($offset >= 0) ? '+' : '-',
133 abs($offhour),
134 $offmin,
135 );
136}
Eric Wong4bc87a22006-03-25 17:20:48 -0800137
Eric Wong567ffeb2006-03-25 16:47:12 -0800138my $have_email_valid = eval { require Email::Valid; 1 };
Jay Soffian50126992009-02-14 23:32:14 -0500139my $have_mail_address = eval { require Mail::Address; 1 };
Eric Wong4bc87a22006-03-25 17:20:48 -0800140my $smtp;
Wincent Colaiuta5f5b6112007-11-21 13:35:05 +0100141my $auth;
Eric Wong4bc87a22006-03-25 17:20:48 -0800142
Ryan Anderson83b24432005-07-31 04:17:25 -0400143# Variables we fill in automatically, or via prompting:
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700144my (@to,$no_to,@initial_to,@cc,$no_cc,@initial_cc,@bcclist,$no_bcc,@xh,
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100145 $initial_reply_to,$initial_subject,@files,
146 $author,$sender,$smtp_authpass,$annotate,$compose,$time);
Ryan Anderson83b24432005-07-31 04:17:25 -0400147
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700148my $envelope_sender;
Ryan Anderson78488b22005-07-31 20:04:24 -0400149
Ryan Anderson91332612005-07-31 20:04:24 -0400150# Example reply to:
Ryan Anderson83b24432005-07-31 04:17:25 -0400151#$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
Ryan Anderson83b24432005-07-31 04:17:25 -0400152
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100153my $repo = eval { Git->repository() };
154my @repo = $repo ? ($repo) : ();
Junio C Hamano280242d2006-07-02 16:03:59 -0700155my $term = eval {
Jay Soffian0fb7fc72008-02-21 19:16:04 -0500156 $ENV{"GIT_SEND_EMAIL_NOTTY"}
157 ? new Term::ReadLine 'git-send-email', \*STDIN, \*STDOUT
158 : new Term::ReadLine 'git-send-email';
Junio C Hamano280242d2006-07-02 16:03:59 -0700159};
160if ($@) {
161 $term = new FakeTerm "$@: going non-interactive";
162}
Ryan Anderson83b24432005-07-31 04:17:25 -0400163
Adam Roben5483c712007-06-27 20:59:37 -0700164# Behavior modification variables
165my ($quiet, $dry_run) = (0, 0);
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100166my $format_patch;
Jay Soffianafe756c2009-02-23 13:51:37 -0500167my $compose_filename;
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200168my $force = 0;
Adam Roben5483c712007-06-27 20:59:37 -0700169
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100170# Handle interactive edition of files.
171my $multiedit;
Michael J Gruber0ce142c2010-03-22 17:12:53 +0100172my $editor;
Jonathan Niederb4479f02009-10-30 20:42:34 -0500173
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100174sub do_edit {
Michael J Gruber0ce142c2010-03-22 17:12:53 +0100175 if (!defined($editor)) {
176 $editor = Git::command_oneline('var', 'GIT_EDITOR');
177 }
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100178 if (defined($multiedit) && !$multiedit) {
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100179 map {
180 system('sh', '-c', $editor.' "$@"', $editor, $_);
181 if (($? & 127) || ($? >> 8)) {
182 die("the editor exited uncleanly, aborting everything");
183 }
184 } @_;
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100185 } else {
186 system('sh', '-c', $editor.' "$@"', $editor, @_);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100187 if (($? & 127) || ($? >> 8)) {
188 die("the editor exited uncleanly, aborting everything");
189 }
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100190 }
191}
Adam Roben5483c712007-06-27 20:59:37 -0700192
193# Variables with corresponding config settings
Joe Perches6e74e072010-09-24 10:03:00 -0700194my ($thread, $chain_reply_to, $suppress_from, $signed_off_by_cc);
195my ($to_cmd, $cc_cmd);
Pascal Obry052fbea2010-09-06 20:12:11 +0200196my ($smtp_server, $smtp_server_port, @smtp_server_options);
197my ($smtp_authuser, $smtp_encryption);
Pascal Obry1d02a002010-09-06 20:12:10 +0200198my ($identity, $aliasfiletype, @alias_files, $smtp_domain);
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500199my ($validate, $confirm);
David Brown65648282007-12-25 19:56:29 -0800200my (@suppress_cc);
Thomas Rast3cae7e52010-06-17 22:10:39 +0200201my ($auto_8bit_encoding);
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200202my ($compose_encoding);
Adam Roben5483c712007-06-27 20:59:37 -0700203
Jari Aaltof60812e2010-03-14 17:16:09 +0200204my ($debug_net_smtp) = 0; # Net::SMTP, see send_message()
205
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900206my %config_bool_settings = (
Adam Roben5483c712007-06-27 20:59:37 -0700207 "thread" => [\$thread, 1],
Felipe Contrerasb99d22f2013-05-24 22:44:52 -0500208 "chainreplyto" => [\$chain_reply_to, 0],
David Brown65648282007-12-25 19:56:29 -0800209 "suppressfrom" => [\$suppress_from, undef],
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500210 "signedoffbycc" => [\$signed_off_by_cc, undef],
211 "signedoffcc" => [\$signed_off_by_cc, undef], # Deprecated
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500212 "validate" => [\$validate, 1],
Felipe Contreras402596a2013-04-07 01:10:27 -0600213 "multiedit" => [\$multiedit, undef],
214 "annotate" => [\$annotate, undef]
Adam Robene46f7a02007-06-26 15:48:30 -0700215);
216
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900217my %config_settings = (
218 "smtpserver" => \$smtp_server,
Junio C Hamano44b24762007-09-25 17:27:54 -0700219 "smtpserverport" => \$smtp_server_port,
Pascal Obry052fbea2010-09-06 20:12:11 +0200220 "smtpserveroption" => \@smtp_server_options,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900221 "smtpuser" => \$smtp_authuser,
222 "smtppass" => \$smtp_authpass,
Pascal Obrye1e91152010-09-06 20:12:09 +0200223 "smtpdomain" => \$smtp_domain,
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700224 "to" => \@initial_to,
Joe Perches6e74e072010-09-24 10:03:00 -0700225 "tocmd" => \$to_cmd,
Miklos Vajna5f8b9fc2008-04-27 14:14:58 +0200226 "cc" => \@initial_cc,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900227 "cccmd" => \$cc_cmd,
228 "aliasfiletype" => \$aliasfiletype,
229 "bcc" => \@bcclist,
David Brown65648282007-12-25 19:56:29 -0800230 "suppresscc" => \@suppress_cc,
Ask Bjørn Hansen9f7820a2008-06-07 00:33:42 -0700231 "envelopesender" => \$envelope_sender,
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500232 "confirm" => \$confirm,
Trent Piepho09caa242009-05-12 15:48:56 -0700233 "from" => \$sender,
Thomas Rast3cae7e52010-06-17 22:10:39 +0200234 "assume8bitencoding" => \$auto_8bit_encoding,
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200235 "composeencoding" => \$compose_encoding,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900236);
Avi Kivity4a62d3f2007-03-11 19:19:44 +0200237
Cord Seelecec5dae2011-09-30 12:52:25 +0200238my %config_path_settings = (
239 "aliasesfile" => \@alias_files,
240);
241
Michael Witten87429972008-02-03 19:53:57 -0500242# Handle Uncouth Termination
243sub signal_handler {
244
245 # Make text normal
246 print color("reset"), "\n";
247
248 # SMTP password masked
249 system "stty echo";
250
251 # tmp files from --compose
Jay Soffianafe756c2009-02-23 13:51:37 -0500252 if (defined $compose_filename) {
253 if (-e $compose_filename) {
254 print "'$compose_filename' contains an intermediate version of the email you were composing.\n";
255 }
256 if (-e ($compose_filename . ".final")) {
257 print "'$compose_filename.final' contains the composed email.\n"
258 }
Michael Witten87429972008-02-03 19:53:57 -0500259 }
260
261 exit;
262};
263
264$SIG{TERM} = \&signal_handler;
265$SIG{INT} = \&signal_handler;
266
Ryan Anderson83b24432005-07-31 04:17:25 -0400267# Begin by accumulating all the variables (defined above), that we will end up
268# needing, first, from the command line:
269
Clemens Buchacherc5978242011-09-03 19:06:13 +0200270my $help;
271my $rc = GetOptions("h" => \$help,
272 "sender|from=s" => \$sender,
Ryan Anderson83b24432005-07-31 04:17:25 -0400273 "in-reply-to=s" => \$initial_reply_to,
274 "subject=s" => \$initial_subject,
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700275 "to=s" => \@initial_to,
Joe Perches6e74e072010-09-24 10:03:00 -0700276 "to-cmd=s" => \$to_cmd,
Stephen Boydf434c082010-03-07 14:46:48 -0800277 "no-to" => \$no_to,
Ryan Andersonda140f82006-02-13 03:05:15 -0500278 "cc=s" => \@initial_cc,
Stephen Boydf434c082010-03-07 14:46:48 -0800279 "no-cc" => \$no_cc,
Ryan Anderson58063242006-05-29 12:30:13 -0700280 "bcc=s" => \@bcclist,
Stephen Boydf434c082010-03-07 14:46:48 -0800281 "no-bcc" => \$no_bcc,
Ryan Anderson78488b22005-07-31 20:04:24 -0400282 "chain-reply-to!" => \$chain_reply_to,
Ryan Anderson3342d852005-07-31 20:04:24 -0400283 "smtp-server=s" => \$smtp_server,
Pascal Obry052fbea2010-09-06 20:12:11 +0200284 "smtp-server-option=s" => \@smtp_server_options,
Junio C Hamano44b24762007-09-25 17:27:54 -0700285 "smtp-server-port=s" => \$smtp_server_port,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900286 "smtp-user=s" => \$smtp_authuser,
Michael Witten2363d742008-02-03 19:53:56 -0500287 "smtp-pass:s" => \$smtp_authpass,
Thomas Rastf6bebd12008-06-25 21:42:43 +0200288 "smtp-ssl" => sub { $smtp_encryption = 'ssl' },
289 "smtp-encryption=s" => \$smtp_encryption,
Jari Aaltof60812e2010-03-14 17:16:09 +0200290 "smtp-debug:i" => \$debug_net_smtp,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -0400291 "smtp-domain:s" => \$smtp_domain,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900292 "identity=s" => \$identity,
Felipe Contreras402596a2013-04-07 01:10:27 -0600293 "annotate!" => \$annotate,
Ryan Anderson1f038a02005-09-05 01:13:07 -0400294 "compose" => \$compose,
Ryan Anderson30d08b32006-02-02 11:56:06 -0500295 "quiet" => \$quiet,
Joe Perches324a8bd2007-08-17 18:51:12 -0700296 "cc-cmd=s" => \$cc_cmd,
Adam Roben5483c712007-06-27 20:59:37 -0700297 "suppress-from!" => \$suppress_from,
David Brown65648282007-12-25 19:56:29 -0800298 "suppress-cc=s" => \@suppress_cc,
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500299 "signed-off-cc|signed-off-by-cc!" => \$signed_off_by_cc,
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500300 "confirm=s" => \$confirm,
Matthew Wilcox61302592006-10-10 08:58:23 -0600301 "dry-run" => \$dry_run,
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700302 "envelope-sender=s" => \$envelope_sender,
Adam Roben5483c712007-06-27 20:59:37 -0700303 "thread!" => \$thread,
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500304 "validate!" => \$validate,
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100305 "format-patch!" => \$format_patch,
Thomas Rast3cae7e52010-06-17 22:10:39 +0200306 "8bit-encoding=s" => \$auto_8bit_encoding,
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200307 "compose-encoding=s" => \$compose_encoding,
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200308 "force" => \$force,
Ryan Anderson83b24432005-07-31 04:17:25 -0400309 );
310
Clemens Buchacherc5978242011-09-03 19:06:13 +0200311usage() if $help;
Michael Coleman1b0baf12007-02-27 22:47:54 -0600312unless ($rc) {
313 usage();
314}
315
Jay Soffianeed6ca72009-02-14 23:32:13 -0500316die "Cannot run git format-patch from outside a repository\n"
317 if $format_patch and not $repo;
318
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900319# Now, let's fill any that aren't set in with defaults:
320
321sub read_config {
322 my ($prefix) = @_;
323
324 foreach my $setting (keys %config_bool_settings) {
325 my $target = $config_bool_settings{$setting}->[0];
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100326 $$target = Git::config_bool(@repo, "$prefix.$setting") unless (defined $$target);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900327 }
328
Cord Seelecec5dae2011-09-30 12:52:25 +0200329 foreach my $setting (keys %config_path_settings) {
Cord Seele463b0ea2011-10-14 22:53:31 +0200330 my $target = $config_path_settings{$setting};
331 if (ref($target) eq "ARRAY") {
332 unless (@$target) {
333 my @values = Git::config_path(@repo, "$prefix.$setting");
334 @$target = @values if (@values && defined $values[0]);
335 }
336 }
337 else {
338 $$target = Git::config_path(@repo, "$prefix.$setting") unless (defined $$target);
339 }
Cord Seelecec5dae2011-09-30 12:52:25 +0200340 }
341
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900342 foreach my $setting (keys %config_settings) {
343 my $target = $config_settings{$setting};
Stephen Boydf434c082010-03-07 14:46:48 -0800344 next if $setting eq "to" and defined $no_to;
345 next if $setting eq "cc" and defined $no_cc;
346 next if $setting eq "bcc" and defined $no_bcc;
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900347 if (ref($target) eq "ARRAY") {
348 unless (@$target) {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100349 my @values = Git::config(@repo, "$prefix.$setting");
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900350 @$target = @values if (@values && defined $values[0]);
351 }
352 }
353 else {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100354 $$target = Git::config(@repo, "$prefix.$setting") unless (defined $$target);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900355 }
356 }
Thomas Rastf6bebd12008-06-25 21:42:43 +0200357
358 if (!defined $smtp_encryption) {
359 my $enc = Git::config(@repo, "$prefix.smtpencryption");
360 if (defined $enc) {
361 $smtp_encryption = $enc;
362 } elsif (Git::config_bool(@repo, "$prefix.smtpssl")) {
363 $smtp_encryption = 'ssl';
364 }
365 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900366}
367
368# read configuration from [sendemail "$identity"], fall back on [sendemail]
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100369$identity = Git::config(@repo, "sendemail.identity") unless (defined $identity);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900370read_config("sendemail.$identity") if (defined $identity);
371read_config("sendemail");
372
373# fall back on builtin bool defaults
374foreach my $setting (values %config_bool_settings) {
375 ${$setting->[0]} = $setting->[1] unless (defined (${$setting->[0]}));
376}
377
Thomas Rastfa835cd2008-06-26 23:03:21 +0200378# 'default' encryption is none -- this only prevents a warning
379$smtp_encryption = '' unless (defined $smtp_encryption);
380
David Brown65648282007-12-25 19:56:29 -0800381# Set CC suppressions
382my(%suppress_cc);
383if (@suppress_cc) {
384 foreach my $entry (@suppress_cc) {
385 die "Unknown --suppress-cc field: '$entry'\n"
Ævar Arnfjörð Bjarmasone9bf7412010-09-30 13:43:04 +0000386 unless $entry =~ /^(?:all|cccmd|cc|author|self|sob|body|bodycc)$/;
David Brown65648282007-12-25 19:56:29 -0800387 $suppress_cc{$entry} = 1;
388 }
389}
390
391if ($suppress_cc{'all'}) {
Paolo Bonzinicb8a9bd2009-06-18 14:31:32 +0200392 foreach my $entry (qw (cccmd cc author self sob body bodycc)) {
David Brown65648282007-12-25 19:56:29 -0800393 $suppress_cc{$entry} = 1;
394 }
395 delete $suppress_cc{'all'};
396}
397
398# If explicit old-style ones are specified, they trump --suppress-cc.
399$suppress_cc{'self'} = $suppress_from if defined $suppress_from;
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500400$suppress_cc{'sob'} = !$signed_off_by_cc if defined $signed_off_by_cc;
David Brown65648282007-12-25 19:56:29 -0800401
Jay Soffian3531e272009-02-14 23:32:15 -0500402if ($suppress_cc{'body'}) {
403 foreach my $entry (qw (sob bodycc)) {
404 $suppress_cc{$entry} = 1;
405 }
406 delete $suppress_cc{'body'};
407}
408
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500409# Set confirm's default value
410my $confirm_unconfigured = !defined $confirm;
411if ($confirm_unconfigured) {
412 $confirm = scalar %suppress_cc ? 'compose' : 'auto';
413};
414die "Unknown --confirm setting: '$confirm'\n"
415 unless $confirm =~ /^(?:auto|cc|compose|always|never)/;
416
David Brown65648282007-12-25 19:56:29 -0800417# Debugging, print out the suppressions.
418if (0) {
419 print "suppressions:\n";
420 foreach my $entry (keys %suppress_cc) {
421 printf " %-5s -> $suppress_cc{$entry}\n", $entry;
422 }
423}
424
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100425my ($repoauthor, $repocommitter);
426($repoauthor) = Git::ident_person(@repo, 'author');
427($repocommitter) = Git::ident_person(@repo, 'committer');
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900428
Eric W. Biederman79ee5552006-06-21 07:17:31 -0600429# Verify the user input
430
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700431foreach my $entry (@initial_to) {
Eric W. Biederman79ee5552006-06-21 07:17:31 -0600432 die "Comma in --to entry: $entry'\n" unless $entry !~ m/,/;
433}
434
435foreach my $entry (@initial_cc) {
436 die "Comma in --cc entry: $entry'\n" unless $entry !~ m/,/;
437}
438
439foreach my $entry (@bcclist) {
440 die "Comma in --bcclist entry: $entry'\n" unless $entry !~ m/,/;
441}
442
Jay Soffian50126992009-02-14 23:32:14 -0500443sub parse_address_line {
444 if ($have_mail_address) {
445 return map { $_->format } Mail::Address->parse($_[0]);
446 } else {
447 return split_addrs($_[0]);
448 }
449}
450
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800451sub split_addrs {
Junio C Hamano2f0e7cb2008-12-21 01:57:59 -0800452 return quotewords('\s*,\s*', 1, @_);
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800453}
454
Eric Wong994d6c62006-05-14 19:13:44 -0700455my %aliases;
Eric Wong994d6c62006-05-14 19:13:44 -0700456my %parse_alias = (
457 # multiline formats can be supported in the future
458 mutt => sub { my $fh = shift; while (<$fh>) {
Felipe Contrerasffc01f92009-09-30 17:49:36 +0300459 if (/^\s*alias\s+(?:-group\s+\S+\s+)*(\S+)\s+(.*)$/) {
Eric Wong994d6c62006-05-14 19:13:44 -0700460 my ($alias, $addr) = ($1, $2);
461 $addr =~ s/#.*$//; # mutt allows # comments
462 # commas delimit multiple addresses
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800463 $aliases{$alias} = [ split_addrs($addr) ];
Eric Wong994d6c62006-05-14 19:13:44 -0700464 }}},
465 mailrc => sub { my $fh = shift; while (<$fh>) {
466 if (/^alias\s+(\S+)\s+(.*)$/) {
467 # spaces delimit multiple addresses
Eric W. Biedermanfe87c922009-05-20 19:45:53 -0700468 $aliases{$1} = [ quotewords('\s+', 0, $2) ];
Eric Wong994d6c62006-05-14 19:13:44 -0700469 }}},
Trent Piepho73c427e2008-11-25 18:55:00 -0800470 pine => sub { my $fh = shift; my $f='\t[^\t]*';
471 for (my $x = ''; defined($x); $x = $_) {
472 chomp $x;
473 $x .= $1 while(defined($_ = <$fh>) && /^ +(.*)$/);
474 $x =~ /^(\S+)$f\t\(?([^\t]+?)\)?(:?$f){0,2}$/ or next;
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800475 $aliases{$1} = [ split_addrs($2) ];
Trent Piepho73c427e2008-11-25 18:55:00 -0800476 }},
Bill Pemberton7613ea32009-04-22 09:41:29 -0400477 elm => sub { my $fh = shift;
478 while (<$fh>) {
479 if (/^(\S+)\s+=\s+[^=]+=\s(\S+)/) {
480 my ($alias, $addr) = ($1, $2);
481 $aliases{$alias} = [ split_addrs($addr) ];
482 }
483 } },
484
Eric Wong994d6c62006-05-14 19:13:44 -0700485 gnus => sub { my $fh = shift; while (<$fh>) {
486 if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
487 $aliases{$1} = [ $2 ];
488 }}}
489);
490
Petr Baudis3cb8caf2006-07-03 22:47:58 +0200491if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) {
Eric Wong994d6c62006-05-14 19:13:44 -0700492 foreach my $file (@alias_files) {
493 open my $fh, '<', $file or die "opening $file: $!\n";
494 $parse_alias{$aliasfiletype}->($fh);
495 close $fh;
496 }
497}
498
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200499($sender) = expand_aliases($sender) if defined $sender;
Michael Hendricksae740a52007-07-04 19:11:36 -0600500
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700501# is_format_patch_arg($f) returns 0 if $f names a patch, or 1 if
502# $f is a revision list specification to be passed to format-patch.
503sub is_format_patch_arg {
Jay Soffianeed6ca72009-02-14 23:32:13 -0500504 return unless $repo;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100505 my $f = shift;
506 try {
507 $repo->command('rev-parse', '--verify', '--quiet', $f);
508 if (defined($format_patch)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100509 return $format_patch;
510 }
511 die(<<EOF);
512File '$f' exists but it could also be the range of commits
513to produce patches for. Please disambiguate by...
514
515 * Saying "./$f" if you mean a file; or
516 * Giving --format-patch option if you mean a range.
517EOF
518 } catch Git::Error::Command with {
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700519 # Not a valid revision. Treat it as a filename.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100520 return 0;
521 }
522}
523
Jeff Kingaa548922008-01-18 09:19:36 -0500524# Now that all the defaults are set, process the rest of the command line
525# arguments and collect up the files that need to be processed.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100526my @rev_list_opts;
Junio C Hamano69f4ce52008-11-30 22:38:20 -0800527while (defined(my $f = shift @ARGV)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100528 if ($f eq "--") {
529 push @rev_list_opts, "--", @ARGV;
530 @ARGV = ();
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700531 } elsif (-d $f and !is_format_patch_arg($f)) {
Ævar Arnfjörð Bjarmasonc6038162010-09-30 13:42:54 +0000532 opendir my $dh, $f
Jeff Kingaa548922008-01-18 09:19:36 -0500533 or die "Failed to opendir $f: $!";
534
Ævar Arnfjörð Bjarmason89bf1ba2010-09-14 19:02:24 +0000535 push @files, grep { -f $_ } map { catfile($f, $_) }
Ævar Arnfjörð Bjarmasonc6038162010-09-30 13:42:54 +0000536 sort readdir $dh;
537 closedir $dh;
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700538 } elsif ((-f $f or -p $f) and !is_format_patch_arg($f)) {
Jeff Kingaa548922008-01-18 09:19:36 -0500539 push @files, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500540 } else {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100541 push @rev_list_opts, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500542 }
543}
544
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100545if (@rev_list_opts) {
Jay Soffianeed6ca72009-02-14 23:32:13 -0500546 die "Cannot run git format-patch from outside a repository\n"
547 unless $repo;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100548 push @files, $repo->command('format-patch', '-o', tempdir(CLEANUP => 1), @rev_list_opts);
549}
550
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500551if ($validate) {
Jeff Kingc764a0c2008-01-18 09:20:10 -0500552 foreach my $f (@files) {
Kevin Ballard300913b2008-06-25 15:44:40 -0700553 unless (-p $f) {
554 my $error = validate_patch($f);
555 $error and die "fatal: $f: $error\nwarning: no patches were sent\n";
556 }
Jeff Kingc764a0c2008-01-18 09:20:10 -0500557 }
Jeff King747bbff2008-01-18 09:19:48 -0500558}
559
Jeff Kingaa548922008-01-18 09:19:36 -0500560if (@files) {
561 unless ($quiet) {
562 print $_,"\n" for (@files);
563 }
564} else {
565 print STDERR "\nNo patch files specified!\n\n";
566 usage();
567}
568
Ævar Arnfjörð Bjarmasonacf071b2010-09-30 13:42:57 +0000569sub get_patch_subject {
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100570 my $fn = shift;
571 open (my $fh, '<', $fn);
572 while (my $line = <$fh>) {
573 next unless ($line =~ /^Subject: (.*)$/);
574 close $fh;
575 return "GIT: $1\n";
576 }
577 close $fh;
578 die "No subject line in $fn ?";
579}
580
581if ($compose) {
582 # Note that this does not need to be secure, but we will make a small
583 # effort to have it be unique
Jay Soffianafe756c2009-02-23 13:51:37 -0500584 $compose_filename = ($repo ?
585 tempfile(".gitsendemail.msg.XXXXXX", DIR => $repo->repo_path()) :
586 tempfile(".gitsendemail.msg.XXXXXX", DIR => "."))[1];
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000587 open my $c, ">", $compose_filename
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100588 or die "Failed to open for writing $compose_filename: $!";
589
590
591 my $tpl_sender = $sender || $repoauthor || $repocommitter || '';
592 my $tpl_subject = $initial_subject || '';
593 my $tpl_reply_to = $initial_reply_to || '';
594
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000595 print $c <<EOT;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100596From $tpl_sender # This line is ignored.
Michael Witten40e6e8a2009-04-13 13:23:50 -0500597GIT: Lines beginning in "GIT:" will be removed.
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100598GIT: Consider including an overall diffstat or table of contents
599GIT: for the patch you are writing.
600GIT:
601GIT: Clear the body content if you don't wish to send a summary.
602From: $tpl_sender
603Subject: $tpl_subject
604In-Reply-To: $tpl_reply_to
605
606EOT
607 for my $f (@files) {
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000608 print $c get_patch_subject($f);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100609 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000610 close $c;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100611
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100612 if ($annotate) {
613 do_edit($compose_filename, @files);
614 } else {
615 do_edit($compose_filename);
616 }
617
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000618 open my $c2, ">", $compose_filename . ".final"
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100619 or die "Failed to open $compose_filename.final : " . $!;
620
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000621 open $c, "<", $compose_filename
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100622 or die "Failed to open $compose_filename : " . $!;
623
624 my $need_8bit_cte = file_has_nonascii($compose_filename);
625 my $in_body = 0;
626 my $summary_empty = 1;
Krzysztof Mazur4a47a4d2012-10-22 14:41:48 +0200627 if (!defined $compose_encoding) {
628 $compose_encoding = "UTF-8";
629 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000630 while(<$c>) {
Michael Witten40e6e8a2009-04-13 13:23:50 -0500631 next if m/^GIT:/;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100632 if ($in_body) {
633 $summary_empty = 0 unless (/^\n$/);
634 } elsif (/^\n$/) {
635 $in_body = 1;
636 if ($need_8bit_cte) {
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000637 print $c2 "MIME-Version: 1.0\n",
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100638 "Content-Type: text/plain; ",
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200639 "charset=$compose_encoding\n",
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100640 "Content-Transfer-Encoding: 8bit\n";
641 }
642 } elsif (/^MIME-Version:/i) {
643 $need_8bit_cte = 0;
644 } elsif (/^Subject:\s*(.+)\s*$/i) {
645 $initial_subject = $1;
646 my $subject = $initial_subject;
647 $_ = "Subject: " .
Krzysztof Mazurce547802012-10-24 23:08:26 +0200648 quote_subject($subject, $compose_encoding) .
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100649 "\n";
650 } elsif (/^In-Reply-To:\s*(.+)\s*$/i) {
651 $initial_reply_to = $1;
652 next;
653 } elsif (/^From:\s*(.+)\s*$/i) {
654 $sender = $1;
655 next;
656 } elsif (/^(?:To|Cc|Bcc):/i) {
657 print "To/Cc/Bcc fields are not interpreted yet, they have been ignored\n";
658 next;
659 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000660 print $c2 $_;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100661 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000662 close $c;
663 close $c2;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100664
665 if ($summary_empty) {
666 print "Summary email is empty, skipping it\n";
667 $compose = -1;
668 }
669} elsif ($annotate) {
670 do_edit(@files);
671}
672
Jay Soffian6e182512009-03-28 21:39:10 -0400673sub ask {
674 my ($prompt, %arg) = @_;
Jay Soffian0da43a62009-04-04 23:23:21 -0400675 my $valid_re = $arg{valid_re};
Jay Soffian6e182512009-03-28 21:39:10 -0400676 my $default = $arg{default};
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700677 my $confirm_only = $arg{confirm_only};
Jay Soffian6e182512009-03-28 21:39:10 -0400678 my $resp;
679 my $i = 0;
Jay Soffian5906f542009-03-31 12:22:11 -0400680 return defined $default ? $default : undef
681 unless defined $term->IN and defined fileno($term->IN) and
682 defined $term->OUT and defined fileno($term->OUT);
Jay Soffian6e182512009-03-28 21:39:10 -0400683 while ($i++ < 10) {
684 $resp = $term->readline($prompt);
685 if (!defined $resp) { # EOF
686 print "\n";
687 return defined $default ? $default : undef;
688 }
689 if ($resp eq '' and defined $default) {
690 return $default;
691 }
Jay Soffian0da43a62009-04-04 23:23:21 -0400692 if (!defined $valid_re or $resp =~ /$valid_re/) {
Jay Soffian6e182512009-03-28 21:39:10 -0400693 return $resp;
694 }
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700695 if ($confirm_only) {
696 my $yesno = $term->readline("Are you sure you want to use <$resp> [y/N]? ");
697 if (defined $yesno && $yesno =~ /y/i) {
698 return $resp;
699 }
700 }
Jay Soffian6e182512009-03-28 21:39:10 -0400701 }
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -0700702 return;
Jay Soffian6e182512009-03-28 21:39:10 -0400703}
704
Thomas Rast3cae7e52010-06-17 22:10:39 +0200705my %broken_encoding;
706
Ævar Arnfjörð Bjarmason1d50bfd2010-09-30 13:42:58 +0000707sub file_declares_8bit_cte {
Thomas Rast3cae7e52010-06-17 22:10:39 +0200708 my $fn = shift;
709 open (my $fh, '<', $fn);
710 while (my $line = <$fh>) {
711 last if ($line =~ /^$/);
712 return 1 if ($line =~ /^Content-Transfer-Encoding: .*8bit.*$/);
713 }
714 close $fh;
715 return 0;
716}
717
718foreach my $f (@files) {
719 next unless (body_or_subject_has_nonascii($f)
720 && !file_declares_8bit_cte($f));
721 $broken_encoding{$f} = 1;
722}
723
724if (!defined $auto_8bit_encoding && scalar %broken_encoding) {
725 print "The following files are 8bit, but do not declare " .
726 "a Content-Transfer-Encoding.\n";
727 foreach my $f (sort keys %broken_encoding) {
728 print " $f\n";
729 }
730 $auto_8bit_encoding = ask("Which 8bit encoding should I declare [UTF-8]? ",
731 default => "UTF-8");
732}
733
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200734if (!$force) {
735 for my $f (@files) {
Ævar Arnfjörð Bjarmason0d290a42010-09-30 13:43:01 +0000736 if (get_patch_subject($f) =~ /\Q*** SUBJECT HERE ***\E/) {
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200737 die "Refusing to send because the patch\n\t$f\n"
738 . "has the template subject '*** SUBJECT HERE ***'. "
739 . "Pass --force if you really want to send.\n";
740 }
741 }
742}
743
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200744if (!defined $sender) {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100745 $sender = $repoauthor || $repocommitter || '';
Ryan Anderson83b24432005-07-31 04:17:25 -0400746}
747
Michael S. Tsirkinda187592013-06-05 21:11:00 +0300748# $sender could be an already sanitized address
749# (e.g. sendemail.from could be manually sanitized by user).
750# But it's a no-op to run sanitize_address on an already sanitized address.
751$sender = sanitize_address($sender);
752
Felipe Contreras8cac13d2012-11-24 12:16:19 +0100753my $prompting = 0;
Junio C Hamano8796ff72010-10-26 22:02:03 -0700754if (!@initial_to && !defined $to_cmd) {
Stephen Boyd61837492012-09-06 11:31:11 -0700755 my $to = ask("Who should the emails be sent to (if any)? ",
756 default => "",
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700757 valid_re => qr/\@.*\./, confirm_only => 1);
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700758 push @initial_to, parse_address_line($to) if defined $to; # sanitized/validated later
Ryan Anderson1f038a02005-09-05 01:13:07 -0400759 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -0400760}
761
Eric Wong994d6c62006-05-14 19:13:44 -0700762sub expand_aliases {
Jeff King302e04e2009-07-23 07:09:29 -0400763 return map { expand_one_alias($_) } @_;
764}
765
766my %EXPANDED_ALIASES;
767sub expand_one_alias {
768 my $alias = shift;
769 if ($EXPANDED_ALIASES{$alias}) {
770 die "fatal: alias '$alias' expands to itself\n";
771 }
772 local $EXPANDED_ALIASES{$alias} = 1;
773 return $aliases{$alias} ? expand_aliases(@{$aliases{$alias}}) : $alias;
Eric Wong994d6c62006-05-14 19:13:44 -0700774}
775
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700776@initial_to = expand_aliases(@initial_to);
Krzysztof Mazure4312252012-11-22 19:12:10 +0100777@initial_to = validate_address_list(sanitize_address_list(@initial_to));
Eric Wong994d6c62006-05-14 19:13:44 -0700778@initial_cc = expand_aliases(@initial_cc);
Krzysztof Mazure4312252012-11-22 19:12:10 +0100779@initial_cc = validate_address_list(sanitize_address_list(@initial_cc));
Ryan Anderson58063242006-05-29 12:30:13 -0700780@bcclist = expand_aliases(@bcclist);
Krzysztof Mazure4312252012-11-22 19:12:10 +0100781@bcclist = validate_address_list(sanitize_address_list(@bcclist));
Eric Wong994d6c62006-05-14 19:13:44 -0700782
Adam Roben5483c712007-06-27 20:59:37 -0700783if ($thread && !defined $initial_reply_to && $prompting) {
Jay Soffian6e182512009-03-28 21:39:10 -0400784 $initial_reply_to = ask(
Stephen Boyd61837492012-09-06 11:31:11 -0700785 "Message-ID to be used as In-Reply-To for the first email (if any)? ",
786 default => "",
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700787 valid_re => qr/\@.*\./, confirm_only => 1);
Ryan Anderson83b24432005-07-31 04:17:25 -0400788}
Jay Soffian1ca3d6e2008-02-20 00:55:07 -0500789if (defined $initial_reply_to) {
Jay Soffian0fb7fc72008-02-21 19:16:04 -0500790 $initial_reply_to =~ s/^\s*<?//;
791 $initial_reply_to =~ s/>?\s*$//;
792 $initial_reply_to = "<$initial_reply_to>" if $initial_reply_to ne '';
Junio C Hamanoace9c2a2007-12-10 21:44:42 -0800793}
Mike Hommeyace72082007-12-09 18:17:28 +0100794
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900795if (!defined $smtp_server) {
Eric Wongaca7ad72006-05-15 02:34:44 -0700796 foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) {
797 if (-x $_) {
798 $smtp_server = $_;
799 last;
800 }
801 }
802 $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
Ryan Anderson3342d852005-07-31 20:04:24 -0400803}
804
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500805if ($compose && $compose > 0) {
806 @files = ($compose_filename . ".final", @files);
Ryan Anderson1f038a02005-09-05 01:13:07 -0400807}
808
Ryan Anderson83b24432005-07-31 04:17:25 -0400809# Variables we set as part of the loop over files
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500810our ($message_id, %mail, $subject, $reply_to, $references, $message,
Jay Soffiandc1460a2009-03-31 12:22:12 -0400811 $needs_confirm, $message_num, $ask_default);
Ryan Anderson83b24432005-07-31 04:17:25 -0400812
Eric Wong567ffeb2006-03-25 16:47:12 -0800813sub extract_valid_address {
814 my $address = shift;
Ævar Arnfjörð Bjarmason35b6ab92010-09-30 19:03:31 +0000815 my $local_part_regexp = qr/[^<>"\s@]+/;
816 my $domain_regexp = qr/[^.<>"\s@]+(?:\.[^.<>"\s@]+)+/;
Eric Wongdb3106b2006-05-15 02:41:01 -0700817
818 # check for a local address:
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700819 return $address if ($address =~ /^($local_part_regexp)$/);
Eric Wongdb3106b2006-05-15 02:41:01 -0700820
Uwe Kleine-König155197e2007-08-09 15:27:57 +0200821 $address =~ s/^\s*<(.*)>\s*$/$1/;
Eric Wong567ffeb2006-03-25 16:47:12 -0800822 if ($have_email_valid) {
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700823 return scalar Email::Valid->address($address);
Eric Wong567ffeb2006-03-25 16:47:12 -0800824 }
Krzysztof Mazur95c0d4b2012-11-22 19:12:09 +0100825
826 # less robust/correct than the monster regexp in Email::Valid,
827 # but still does a 99% job, and one less dependency
828 return $1 if $address =~ /($local_part_regexp\@$domain_regexp)/;
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -0700829 return;
Eric Wong567ffeb2006-03-25 16:47:12 -0800830}
Ryan Anderson83b24432005-07-31 04:17:25 -0400831
Krzysztof Mazure4312252012-11-22 19:12:10 +0100832sub extract_valid_address_or_die {
833 my $address = shift;
834 $address = extract_valid_address($address);
835 die "error: unable to extract a valid address from: $address\n"
836 if !$address;
837 return $address;
838}
839
840sub validate_address {
841 my $address = shift;
Krzysztof Mazurd0e98102012-11-22 19:12:12 +0100842 while (!extract_valid_address($address)) {
Krzysztof Mazur5c80afe2012-11-22 19:12:11 +0100843 print STDERR "error: unable to extract a valid address from: $address\n";
Krzysztof Mazurd0e98102012-11-22 19:12:12 +0100844 $_ = ask("What to do with this address? ([q]uit|[d]rop|[e]dit): ",
845 valid_re => qr/^(?:quit|q|drop|d|edit|e)/i,
Krzysztof Mazur5c80afe2012-11-22 19:12:11 +0100846 default => 'q');
847 if (/^d/i) {
848 return undef;
849 } elsif (/^q/i) {
850 cleanup_compose_files();
851 exit(0);
852 }
Krzysztof Mazurd0e98102012-11-22 19:12:12 +0100853 $address = ask("Who should the email be sent to (if any)? ",
854 default => "",
855 valid_re => qr/\@.*\./, confirm_only => 1);
Krzysztof Mazure4312252012-11-22 19:12:10 +0100856 }
857 return $address;
858}
859
860sub validate_address_list {
861 return (grep { defined $_ }
862 map { validate_address($_) } @_);
Ryan Anderson83b24432005-07-31 04:17:25 -0400863}
864
865# Usually don't need to change anything below here.
866
867# we make a "fake" message id by taking the current number
868# of seconds since the beginning of Unix time and tacking on
869# a random number to the end, in case we are called quicker than
870# 1 second since the last time we were called.
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400871
872# We'll setup a template for the message id, using the "from" address:
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400873
Junio C Hamanobe510cf2007-09-17 21:18:20 -0700874my ($message_id_stamp, $message_id_serial);
Brian Gernhardt68ce9332010-04-10 10:53:53 -0400875sub make_message_id {
Junio C Hamanobe510cf2007-09-17 21:18:20 -0700876 my $uniq;
877 if (!defined $message_id_stamp) {
878 $message_id_stamp = sprintf("%s-%s", time, $$);
879 $message_id_serial = 0;
880 }
881 $message_id_serial++;
882 $uniq = "$message_id_stamp-$message_id_serial";
883
Junio C Hamanoaeb59322007-06-20 13:47:34 -0700884 my $du_part;
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200885 for ($sender, $repocommitter, $repoauthor) {
886 $du_part = extract_valid_address(sanitize_address($_));
887 last if (defined $du_part and $du_part ne '');
Junio C Hamanoaeb59322007-06-20 13:47:34 -0700888 }
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200889 if (not defined $du_part or $du_part eq '') {
Ævar Arnfjörð Bjarmason529dd382010-09-30 13:43:08 +0000890 require Sys::Hostname;
Junio C Hamanoaeb59322007-06-20 13:47:34 -0700891 $du_part = 'user@' . Sys::Hostname::hostname();
892 }
Junio C Hamanobe510cf2007-09-17 21:18:20 -0700893 my $message_id_template = "<%s-git-send-email-%s>";
894 $message_id = sprintf($message_id_template, $uniq, $du_part);
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400895 #print "new message id = $message_id\n"; # Was useful for debugging
Ryan Anderson83b24432005-07-31 04:17:25 -0400896}
897
898
899
Eric Wonga5370b12006-03-25 03:01:01 -0800900$time = time - scalar $#files;
Ryan Anderson83b24432005-07-31 04:17:25 -0400901
Jürgen Rühle374c5902007-01-10 13:36:39 -0800902sub unquote_rfc2047 {
903 local ($_) = @_;
Jeff King8291db62007-11-16 05:49:09 -0500904 my $encoding;
Thomas Rastb622d4d2012-07-30 21:25:40 +0200905 s{=\?([^?]+)\?q\?(.*?)\?=}{
Jeff King8291db62007-11-16 05:49:09 -0500906 $encoding = $1;
Thomas Rastb622d4d2012-07-30 21:25:40 +0200907 my $e = $2;
908 $e =~ s/_/ /g;
909 $e =~ s/=([0-9A-F]{2})/chr(hex($1))/eg;
910 $e;
911 }eg;
Jeff King8291db62007-11-16 05:49:09 -0500912 return wantarray ? ($_, $encoding) : $_;
Jürgen Rühle374c5902007-01-10 13:36:39 -0800913}
914
Jeff Kingd54eaaa2008-03-28 17:29:01 -0400915sub quote_rfc2047 {
916 local $_ = shift;
Brandon Caseyd1fff6f2009-06-06 20:12:31 -0500917 my $encoding = shift || 'UTF-8';
Jeff Kingd54eaaa2008-03-28 17:29:01 -0400918 s/([^-a-zA-Z0-9!*+\/])/sprintf("=%02X", ord($1))/eg;
919 s/(.*)/=\?$encoding\?q\?$1\?=/;
920 return $_;
921}
922
Brandon Caseya3a82622009-06-07 19:25:58 -0500923sub is_rfc2047_quoted {
924 my $s = shift;
Ævar Arnfjörð Bjarmason49f73852010-09-30 13:43:05 +0000925 my $token = qr/[^][()<>@,;:"\/?.= \000-\037\177-\377]+/;
926 my $encoded_text = qr/[!->@-~]+/;
Brandon Caseya3a82622009-06-07 19:25:58 -0500927 length($s) <= 75 &&
928 $s =~ m/^(?:"[[:ascii:]]*"|=\?$token\?$token\?$encoded_text\?=)$/o;
929}
930
Krzysztof Mazurce547802012-10-24 23:08:26 +0200931sub subject_needs_rfc2047_quoting {
932 my $s = shift;
933
Krzysztof Mazurce1459f2012-10-24 23:28:29 +0200934 return ($s =~ /[^[:ascii:]]/) || ($s =~ /=\?/);
Krzysztof Mazurce547802012-10-24 23:08:26 +0200935}
936
937sub quote_subject {
938 local $subject = shift;
939 my $encoding = shift || 'UTF-8';
940
941 if (subject_needs_rfc2047_quoting($subject)) {
942 return quote_rfc2047($subject, $encoding);
943 }
944 return $subject;
945}
946
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +0200947# use the simplest quoting being able to handle the recipient
Brian Gernhardt68ce9332010-04-10 10:53:53 -0400948sub sanitize_address {
Robin H. Johnson732263d2007-04-25 19:37:19 -0700949 my ($recipient) = @_;
Krzysztof Mazur831a4882012-11-22 19:12:08 +0100950
951 # remove garbage after email address
952 $recipient =~ s/(.*>).*$/$1/;
953
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +0200954 my ($recipient_name, $recipient_addr) = ($recipient =~ /^(.*?)\s*(<.*)/);
955
956 if (not $recipient_name) {
Ævar Arnfjörð Bjarmasonff483892010-09-30 13:43:02 +0000957 return $recipient;
Robin H. Johnson732263d2007-04-25 19:37:19 -0700958 }
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +0200959
960 # if recipient_name is already quoted, do nothing
Brandon Caseya3a82622009-06-07 19:25:58 -0500961 if (is_rfc2047_quoted($recipient_name)) {
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +0200962 return $recipient;
963 }
964
965 # rfc2047 is needed if a non-ascii char is included
966 if ($recipient_name =~ /[^[:ascii:]]/) {
Jay Soffiana61c0ff2009-03-31 12:22:14 -0400967 $recipient_name =~ s/^"(.*)"$/$1/;
Jeff Kingd54eaaa2008-03-28 17:29:01 -0400968 $recipient_name = quote_rfc2047($recipient_name);
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +0200969 }
970
971 # double quotes are needed if specials or CTLs are included
972 elsif ($recipient_name =~ /[][()<>@,;:\\".\000-\037\177]/) {
Horst H. von Brand18023c22008-03-28 11:09:04 -0300973 $recipient_name =~ s/(["\\\r])/\\$1/g;
Ævar Arnfjörð Bjarmasond5c7d692010-09-30 13:43:03 +0000974 $recipient_name = qq["$recipient_name"];
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +0200975 }
976
977 return "$recipient_name $recipient_addr";
978
Robin H. Johnson732263d2007-04-25 19:37:19 -0700979}
980
Krzysztof Mazure4312252012-11-22 19:12:10 +0100981sub sanitize_address_list {
982 return (map { sanitize_address($_) } @_);
983}
984
Jari Aalto134550f2010-03-14 17:16:45 +0200985# Returns the local Fully Qualified Domain Name (FQDN) if available.
986#
987# Tightly configured MTAa require that a caller sends a real DNS
988# domain name that corresponds the IP address in the HELO/EHLO
989# handshake. This is used to verify the connection and prevent
990# spammers from trying to hide their identity. If the DNS and IP don't
991# match, the receiveing MTA may deny the connection.
992#
993# Here is a deny example of Net::SMTP with the default "localhost.localdomain"
994#
995# Net::SMTP=GLOB(0x267ec28)>>> EHLO localhost.localdomain
996# Net::SMTP=GLOB(0x267ec28)<<< 550 EHLO argument does not match calling host
997#
998# This maildomain*() code is based on ideas in Perl library Test::Reporter
999# /usr/share/perl5/Test/Reporter/Mail/Util.pm ==> sub _maildomain ()
1000
Brian Gernhardt59a86302010-04-10 10:53:54 -04001001sub valid_fqdn {
1002 my $domain = shift;
Brandon Casey61ef5e92010-09-26 22:18:01 -05001003 return defined $domain && !($^O eq 'darwin' && $domain =~ /\.local$/) && $domain =~ /\./;
Brian Gernhardt59a86302010-04-10 10:53:54 -04001004}
1005
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001006sub maildomain_net {
Jari Aalto134550f2010-03-14 17:16:45 +02001007 my $maildomain;
1008
1009 if (eval { require Net::Domain; 1 }) {
1010 my $domain = Net::Domain::domainname();
Brian Gernhardt59a86302010-04-10 10:53:54 -04001011 $maildomain = $domain if valid_fqdn($domain);
Jari Aalto134550f2010-03-14 17:16:45 +02001012 }
1013
1014 return $maildomain;
1015}
1016
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001017sub maildomain_mta {
Jari Aalto134550f2010-03-14 17:16:45 +02001018 my $maildomain;
1019
1020 if (eval { require Net::SMTP; 1 }) {
1021 for my $host (qw(mailhost localhost)) {
1022 my $smtp = Net::SMTP->new($host);
1023 if (defined $smtp) {
1024 my $domain = $smtp->domain;
1025 $smtp->quit;
1026
Brian Gernhardt59a86302010-04-10 10:53:54 -04001027 $maildomain = $domain if valid_fqdn($domain);
Jari Aalto134550f2010-03-14 17:16:45 +02001028
1029 last if $maildomain;
1030 }
1031 }
1032 }
1033
1034 return $maildomain;
1035}
1036
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001037sub maildomain {
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001038 return maildomain_net() || maildomain_mta() || 'localhost.localdomain';
Jari Aalto134550f2010-03-14 17:16:45 +02001039}
1040
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001041sub smtp_host_string {
1042 if (defined $smtp_server_port) {
1043 return "$smtp_server:$smtp_server_port";
1044 } else {
1045 return $smtp_server;
1046 }
1047}
1048
1049# Returns 1 if authentication succeeded or was not necessary
1050# (smtp_user was not specified), and 0 otherwise.
1051
1052sub smtp_auth_maybe {
1053 if (!defined $smtp_authuser || $auth) {
1054 return 1;
1055 }
1056
1057 # Workaround AUTH PLAIN/LOGIN interaction defect
1058 # with Authen::SASL::Cyrus
1059 eval {
1060 require Authen::SASL;
1061 Authen::SASL->import(qw(Perl));
1062 };
1063
1064 # TODO: Authentication may fail not because credentials were
1065 # invalid but due to other reasons, in which we should not
1066 # reject credentials.
1067 $auth = Git::credential({
1068 'protocol' => 'smtp',
1069 'host' => smtp_host_string(),
1070 'username' => $smtp_authuser,
1071 # if there's no password, "git credential fill" will
1072 # give us one, otherwise it'll just pass this one.
1073 'password' => $smtp_authpass
1074 }, sub {
1075 my $cred = shift;
1076 return !!$smtp->auth($cred->{'username'}, $cred->{'password'});
1077 });
1078
1079 return $auth;
1080}
1081
Michael Witten15da1082009-04-13 13:23:51 -05001082# Returns 1 if the message was sent, and 0 otherwise.
Markus Heidelberga1b5b372009-06-12 12:51:42 +02001083# In actuality, the whole program dies when there
Michael Witten15da1082009-04-13 13:23:51 -05001084# is an error sending a message.
1085
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001086sub send_message {
Eric Wong4bc87a22006-03-25 17:20:48 -08001087 my @recipients = unique_email_list(@to);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001088 @cc = (grep { my $cc = extract_valid_address_or_die($_);
Joe Perches83acaae2010-11-20 15:06:05 -08001089 not grep { $cc eq $_ || $_ =~ /<\Q${cc}\E>$/ } @recipients
Ask Bjørn Hansen7ac17522007-11-19 03:00:26 -08001090 }
Ask Bjørn Hansen7ac17522007-11-19 03:00:26 -08001091 @cc);
Eric Wong4bc87a22006-03-25 17:20:48 -08001092 my $to = join (",\n\t", @recipients);
Ryan Anderson58063242006-05-29 12:30:13 -07001093 @recipients = unique_email_list(@recipients,@cc,@bcclist);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001094 @recipients = (map { extract_valid_address_or_die($_) } @recipients);
Jakub Narebski6bdca892006-07-07 20:57:55 +02001095 my $date = format_2822_time($time++);
Martin Langhoffe923eff2006-05-03 09:44:36 +12001096 my $gitversion = '@@GIT_VERSION@@';
1097 if ($gitversion =~ m/..GIT_VERSION../) {
Petr Baudis3cb8caf2006-07-03 22:47:58 +02001098 $gitversion = Git::version();
Martin Langhoffe923eff2006-05-03 09:44:36 +12001099 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001100
Joe Perches02461e02009-10-08 10:03:26 -07001101 my $cc = join(",\n\t", unique_email_list(@cc));
Junio C Hamanof06a6a42007-04-16 16:51:47 -07001102 my $ccline = "";
1103 if ($cc ne '') {
1104 $ccline = "\nCc: $cc";
1105 }
Jeff King4f3d3702007-12-17 15:51:34 -05001106 make_message_id() unless defined($message_id);
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001107
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001108 my $header = "From: $sender
Junio C Hamanof06a6a42007-04-16 16:51:47 -07001109To: $to${ccline}
Eric Wong4bc87a22006-03-25 17:20:48 -08001110Subject: $subject
Eric Wong4bc87a22006-03-25 17:20:48 -08001111Date: $date
1112Message-Id: $message_id
Martin Langhoffe923eff2006-05-03 09:44:36 +12001113X-Mailer: git-send-email $gitversion
Eric Wong4bc87a22006-03-25 17:20:48 -08001114";
Thomas Rast3e0c4ff2009-03-01 23:45:41 +01001115 if ($reply_to) {
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001116
1117 $header .= "In-Reply-To: $reply_to\n";
1118 $header .= "References: $references\n";
1119 }
Junio C Hamanoce91c2f2006-10-05 16:36:49 -07001120 if (@xh) {
1121 $header .= join("\n", @xh) . "\n";
1122 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001123
Robin H. Johnsonc38f0242007-04-25 19:37:20 -07001124 my @sendmail_parameters = ('-i', @recipients);
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001125 my $raw_from = $sender;
Felipe Contrerasc89e3242009-11-26 21:04:29 +02001126 if (defined $envelope_sender && $envelope_sender ne "auto") {
1127 $raw_from = $envelope_sender;
1128 }
Robin H. Johnsonf073a592007-04-25 19:37:22 -07001129 $raw_from = extract_valid_address($raw_from);
1130 unshift (@sendmail_parameters,
1131 '-f', $raw_from) if(defined $envelope_sender);
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001132
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001133 if ($needs_confirm && !$dry_run) {
1134 print "\n$header\n";
1135 if ($needs_confirm eq "inform") {
1136 $confirm_unconfigured = 0; # squelch this message for the rest of this run
Jay Soffian6e182512009-03-28 21:39:10 -04001137 $ask_default = "y"; # assume yes on EOF since user hasn't explicitly asked for confirmation
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001138 print " The Cc list above has been expanded by additional\n";
1139 print " addresses found in the patch commit message. By default\n";
1140 print " send-email prompts before sending whenever this occurs.\n";
1141 print " This behavior is controlled by the sendemail.confirm\n";
1142 print " configuration setting.\n";
1143 print "\n";
1144 print " For additional information, run 'git send-email --help'.\n";
1145 print " To retain the current behavior, but squelch this message,\n";
1146 print " run 'git config --global sendemail.confirm auto'.\n\n";
1147 }
Jay Soffian6e182512009-03-28 21:39:10 -04001148 $_ = ask("Send this email? ([y]es|[n]o|[q]uit|[a]ll): ",
1149 valid_re => qr/^(?:yes|y|no|n|quit|q|all|a)/i,
1150 default => $ask_default);
1151 die "Send this email reply required" unless defined $_;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001152 if (/^n/i) {
Michael Witten15da1082009-04-13 13:23:51 -05001153 return 0;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001154 } elsif (/^q/i) {
1155 cleanup_compose_files();
1156 exit(0);
1157 } elsif (/^a/i) {
1158 $confirm = 'never';
1159 }
1160 }
1161
Pascal Obry052fbea2010-09-06 20:12:11 +02001162 unshift (@sendmail_parameters, @smtp_server_options);
1163
Matthew Wilcox61302592006-10-10 08:58:23 -06001164 if ($dry_run) {
1165 # We don't want to send the email.
1166 } elsif ($smtp_server =~ m#^/#) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001167 my $pid = open my $sm, '|-';
1168 defined $pid or die $!;
1169 if (!$pid) {
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001170 exec($smtp_server, @sendmail_parameters) or die $!;
Eric Wongaca7ad72006-05-15 02:34:44 -07001171 }
1172 print $sm "$header\n$message";
Ævar Arnfjörð Bjarmason5e2c2ab2010-09-30 13:43:07 +00001173 close $sm or die $!;
Eric Wongaca7ad72006-05-15 02:34:44 -07001174 } else {
Junio C Hamano44b24762007-09-25 17:27:54 -07001175
1176 if (!defined $smtp_server) {
1177 die "The required SMTP server is not properly defined."
1178 }
1179
Thomas Rastf6bebd12008-06-25 21:42:43 +02001180 if ($smtp_encryption eq 'ssl') {
Junio C Hamano44b24762007-09-25 17:27:54 -07001181 $smtp_server_port ||= 465; # ssmtp
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001182 require Net::SMTP::SSL;
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001183 $smtp_domain ||= maildomain();
Jari Aalto134550f2010-03-14 17:16:45 +02001184 $smtp ||= Net::SMTP::SSL->new($smtp_server,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001185 Hello => $smtp_domain,
Jari Aalto134550f2010-03-14 17:16:45 +02001186 Port => $smtp_server_port);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001187 }
1188 else {
1189 require Net::SMTP;
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001190 $smtp_domain ||= maildomain();
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001191 $smtp ||= Net::SMTP->new(smtp_host_string(),
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001192 Hello => $smtp_domain,
Jari Aaltof60812e2010-03-14 17:16:09 +02001193 Debug => $debug_net_smtp);
Yakov Lernerfb3650e2009-09-25 15:10:21 -07001194 if ($smtp_encryption eq 'tls' && $smtp) {
Thomas Rastf6bebd12008-06-25 21:42:43 +02001195 require Net::SMTP::SSL;
1196 $smtp->command('STARTTLS');
1197 $smtp->response();
1198 if ($smtp->code == 220) {
1199 $smtp = Net::SMTP::SSL->start_SSL($smtp)
1200 or die "STARTTLS failed! ".$smtp->message;
Thomas Rast6cbf8b02008-07-03 00:11:31 +02001201 $smtp_encryption = '';
Robert Shearman9d1ccf52008-07-09 22:39:40 +01001202 # Send EHLO again to receive fresh
1203 # supported commands
Matthew Daley155b9402011-10-15 04:44:52 -04001204 $smtp->hello($smtp_domain);
Thomas Rastf6bebd12008-06-25 21:42:43 +02001205 } else {
1206 die "Server does not support STARTTLS! ".$smtp->message;
1207 }
1208 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001209 }
Junio C Hamano44b24762007-09-25 17:27:54 -07001210
1211 if (!$smtp) {
Jari Aaltof60812e2010-03-14 17:16:09 +02001212 die "Unable to initialize SMTP properly. Check config and use --smtp-debug. ",
Jari Aaltoe5afb3a2010-03-14 17:15:33 +02001213 "VALUES: server=$smtp_server ",
1214 "encryption=$smtp_encryption ",
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001215 "hello=$smtp_domain",
Sylvain Rabota1dd7e12011-04-29 20:23:24 +02001216 defined $smtp_server_port ? " port=$smtp_server_port" : "";
Junio C Hamano44b24762007-09-25 17:27:54 -07001217 }
1218
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001219 smtp_auth_maybe or die $smtp->message;
Michael Witten2363d742008-02-03 19:53:56 -05001220
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001221 $smtp->mail( $raw_from ) or die $smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001222 $smtp->to( @recipients ) or die $smtp->message;
1223 $smtp->data or die $smtp->message;
1224 $smtp->datasend("$header\n$message") or die $smtp->message;
1225 $smtp->dataend() or die $smtp->message;
Michael Witten15da1082009-04-13 13:23:51 -05001226 $smtp->code =~ /250|200/ or die "Failed to send $subject\n".$smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001227 }
Ryan Anderson27184352006-02-05 20:13:52 -05001228 if ($quiet) {
Robin H. Johnson71c7da92007-04-25 19:37:16 -07001229 printf (($dry_run ? "Dry-" : "")."Sent %s\n", $subject);
Ryan Anderson27184352006-02-05 20:13:52 -05001230 } else {
David D. Kilzerb7f30e02007-11-18 20:14:55 -08001231 print (($dry_run ? "Dry-" : "")."OK. Log says:\n");
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001232 if ($smtp_server !~ m#^/#) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001233 print "Server: $smtp_server\n";
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001234 print "MAIL FROM:<$raw_from>\n";
Joe Perches02461e02009-10-08 10:03:26 -07001235 foreach my $entry (@recipients) {
1236 print "RCPT TO:<$entry>\n";
1237 }
Eric Wongaca7ad72006-05-15 02:34:44 -07001238 } else {
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001239 print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001240 }
David D. Kilzerb7f30e02007-11-18 20:14:55 -08001241 print $header, "\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001242 if ($smtp) {
1243 print "Result: ", $smtp->code, ' ',
1244 ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
1245 } else {
1246 print "Result: OK\n";
1247 }
Ryan Anderson30d08b32006-02-02 11:56:06 -05001248 }
Michael Witten15da1082009-04-13 13:23:51 -05001249
1250 return 1;
Ryan Anderson83b24432005-07-31 04:17:25 -04001251}
1252
Ryan Anderson83b24432005-07-31 04:17:25 -04001253$reply_to = $initial_reply_to;
Junio C Hamano2186d562006-05-29 23:53:13 -07001254$references = $initial_reply_to || '';
Ryan Anderson83b24432005-07-31 04:17:25 -04001255$subject = $initial_subject;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001256$message_num = 0;
Ryan Anderson83b24432005-07-31 04:17:25 -04001257
1258foreach my $t (@files) {
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001259 open my $fh, "<", $t or die "can't open file $t";
Ryan Anderson83b24432005-07-31 04:17:25 -04001260
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001261 my $author = undef;
Jeff King8291db62007-11-16 05:49:09 -05001262 my $author_encoding;
1263 my $has_content_type;
1264 my $body_encoding;
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001265 @to = ();
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001266 @cc = ();
Junio C Hamanoce91c2f2006-10-05 16:36:49 -07001267 @xh = ();
Junio C Hamanoe6b09642006-10-07 03:09:05 -07001268 my $input_format = undef;
Jay Soffian50126992009-02-14 23:32:14 -05001269 my @header = ();
Ryan Anderson83b24432005-07-31 04:17:25 -04001270 $message = "";
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001271 $message_num++;
Jay Soffian50126992009-02-14 23:32:14 -05001272 # First unfold multiline header fields
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001273 while(<$fh>) {
Jay Soffian50126992009-02-14 23:32:14 -05001274 last if /^\s*$/;
1275 if (/^\s+\S/ and @header) {
1276 chomp($header[$#header]);
1277 s/^\s+/ /;
1278 $header[$#header] .= $_;
1279 } else {
1280 push(@header, $_);
1281 }
1282 }
1283 # Now parse the header
1284 foreach(@header) {
1285 if (/^From /) {
1286 $input_format = 'mbox';
1287 next;
1288 }
1289 chomp;
1290 if (!defined $input_format && /^[-A-Za-z]+:\s/) {
1291 $input_format = 'mbox';
1292 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001293
Jay Soffian50126992009-02-14 23:32:14 -05001294 if (defined $input_format && $input_format eq 'mbox') {
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001295 if (/^Subject:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001296 $subject = $1;
1297 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001298 elsif (/^From:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001299 ($author, $author_encoding) = unquote_rfc2047($1);
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001300 my $sauthor = sanitize_address($author);
Jay Soffian50126992009-02-14 23:32:14 -05001301 next if $suppress_cc{'author'};
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001302 next if $suppress_cc{'self'} and $sauthor eq $sender;
Jay Soffian50126992009-02-14 23:32:14 -05001303 printf("(mbox) Adding cc: %s from line '%s'\n",
1304 $1, $_) unless $quiet;
1305 push @cc, $1;
1306 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001307 elsif (/^To:\s+(.*)$/i) {
Stephen Boyd21802cd2010-09-29 00:26:44 -07001308 foreach my $addr (parse_address_line($1)) {
1309 printf("(mbox) Adding to: %s from line '%s'\n",
1310 $addr, $_) unless $quiet;
Krzysztof Mazure4312252012-11-22 19:12:10 +01001311 push @to, $addr;
Stephen Boyd21802cd2010-09-29 00:26:44 -07001312 }
1313 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001314 elsif (/^Cc:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001315 foreach my $addr (parse_address_line($1)) {
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001316 my $qaddr = unquote_rfc2047($addr);
1317 my $saddr = sanitize_address($qaddr);
1318 if ($saddr eq $sender) {
David Brown65648282007-12-25 19:56:29 -08001319 next if ($suppress_cc{'self'});
David Brown65648282007-12-25 19:56:29 -08001320 } else {
1321 next if ($suppress_cc{'cc'});
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001322 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001323 printf("(mbox) Adding cc: %s from line '%s'\n",
Jay Soffian50126992009-02-14 23:32:14 -05001324 $addr, $_) unless $quiet;
1325 push @cc, $addr;
Ryan Anderson83b24432005-07-31 04:17:25 -04001326 }
1327 }
Jay Soffian50126992009-02-14 23:32:14 -05001328 elsif (/^Content-type:/i) {
1329 $has_content_type = 1;
1330 if (/charset="?([^ "]+)/) {
1331 $body_encoding = $1;
1332 }
1333 push @xh, $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001334 }
Jay Soffian50126992009-02-14 23:32:14 -05001335 elsif (/^Message-Id: (.*)/i) {
1336 $message_id = $1;
1337 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001338 elsif (!/^Date:\s/i && /^[-A-Za-z]+:\s+\S/) {
Jay Soffian50126992009-02-14 23:32:14 -05001339 push @xh, $_;
1340 }
1341
Ryan Anderson83b24432005-07-31 04:17:25 -04001342 } else {
Jay Soffian50126992009-02-14 23:32:14 -05001343 # In the traditional
1344 # "send lots of email" format,
1345 # line 1 = cc
1346 # line 2 = subject
1347 # So let's support that, too.
1348 $input_format = 'lots';
1349 if (@cc == 0 && !$suppress_cc{'cc'}) {
1350 printf("(non-mbox) Adding cc: %s from line '%s'\n",
1351 $_, $_) unless $quiet;
1352 push @cc, $_;
1353 } elsif (!defined $subject) {
1354 $subject = $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001355 }
1356 }
1357 }
Jay Soffian50126992009-02-14 23:32:14 -05001358 # Now parse the message body
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001359 while(<$fh>) {
Jay Soffian50126992009-02-14 23:32:14 -05001360 $message .= $_;
1361 if (/^(Signed-off-by|Cc): (.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001362 chomp;
Jay Soffian3531e272009-02-14 23:32:15 -05001363 my ($what, $c) = ($1, $2);
Jay Soffian50126992009-02-14 23:32:14 -05001364 chomp $c;
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001365 my $sc = sanitize_address($c);
1366 if ($sc eq $sender) {
Jay Soffian3531e272009-02-14 23:32:15 -05001367 next if ($suppress_cc{'self'});
1368 } else {
1369 next if $suppress_cc{'sob'} and $what =~ /Signed-off-by/i;
1370 next if $suppress_cc{'bodycc'} and $what =~ /Cc/i;
1371 }
Jay Soffian50126992009-02-14 23:32:14 -05001372 push @cc, $c;
Jay Soffian3531e272009-02-14 23:32:15 -05001373 printf("(body) Adding cc: %s from line '%s'\n",
Jay Soffian50126992009-02-14 23:32:14 -05001374 $c, $_) unless $quiet;
1375 }
1376 }
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001377 close $fh;
Joe Perches324a8bd2007-08-17 18:51:12 -07001378
Joe Perches6e74e072010-09-24 10:03:00 -07001379 push @to, recipients_cmd("to-cmd", "to", $to_cmd, $t)
1380 if defined $to_cmd;
1381 push @cc, recipients_cmd("cc-cmd", "cc", $cc_cmd, $t)
1382 if defined $cc_cmd && !$suppress_cc{'cccmd'};
Joe Perches324a8bd2007-08-17 18:51:12 -07001383
Thomas Rast3cae7e52010-06-17 22:10:39 +02001384 if ($broken_encoding{$t} && !$has_content_type) {
1385 $has_content_type = 1;
1386 push @xh, "MIME-Version: 1.0",
1387 "Content-Type: text/plain; charset=$auto_8bit_encoding",
1388 "Content-Transfer-Encoding: 8bit";
1389 $body_encoding = $auto_8bit_encoding;
1390 }
1391
Krzysztof Mazurce547802012-10-24 23:08:26 +02001392 if ($broken_encoding{$t} && !is_rfc2047_quoted($subject)) {
1393 $subject = quote_subject($subject, $auto_8bit_encoding);
Thomas Rast3cae7e52010-06-17 22:10:39 +02001394 }
1395
Jay Soffian50126992009-02-14 23:32:14 -05001396 if (defined $author and $author ne $sender) {
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001397 $message = "From: $author\n\n$message";
Jeff King8291db62007-11-16 05:49:09 -05001398 if (defined $author_encoding) {
1399 if ($has_content_type) {
1400 if ($body_encoding eq $author_encoding) {
1401 # ok, we already have the right encoding
1402 }
1403 else {
1404 # uh oh, we should re-encode
1405 }
1406 }
1407 else {
Thomas Rast3cae7e52010-06-17 22:10:39 +02001408 $has_content_type = 1;
Jeff King8291db62007-11-16 05:49:09 -05001409 push @xh,
1410 'MIME-Version: 1.0',
Jeff King8641ee32007-11-20 07:54:04 -05001411 "Content-Type: text/plain; charset=$author_encoding",
1412 'Content-Transfer-Encoding: 8bit';
Jeff King8291db62007-11-16 05:49:09 -05001413 }
1414 }
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001415 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001416
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001417 $needs_confirm = (
1418 $confirm eq "always" or
1419 ($confirm =~ /^(?:auto|cc)$/ && @cc) or
1420 ($confirm =~ /^(?:auto|compose)$/ && $compose && $message_num == 1));
1421 $needs_confirm = "inform" if ($needs_confirm && $confirm_unconfigured && @cc);
1422
Krzysztof Mazure4312252012-11-22 19:12:10 +01001423 @to = validate_address_list(sanitize_address_list(@to));
1424 @cc = validate_address_list(sanitize_address_list(@cc));
1425
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001426 @to = (@initial_to, @to);
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001427 @cc = (@initial_cc, @cc);
1428
Michael Witten15da1082009-04-13 13:23:51 -05001429 my $message_was_sent = send_message();
Ryan Anderson83b24432005-07-31 04:17:25 -04001430
1431 # set up for the next message
Junio C Hamano95a877a2009-06-12 09:23:43 -07001432 if ($thread && $message_was_sent &&
Felipe Contrerasb99d22f2013-05-24 22:44:52 -05001433 ($chain_reply_to || !defined $reply_to || length($reply_to) == 0 ||
Antonio Ospitedb54c8e2010-11-12 15:55:08 +01001434 $message_num == 1)) {
Ryan Anderson78488b22005-07-31 20:04:24 -04001435 $reply_to = $message_id;
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001436 if (length $references > 0) {
YOSHIFUJI Hideaki / 吉藤英明a925b892007-04-06 08:50:24 +09001437 $references .= "\n $message_id";
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001438 } else {
1439 $references = "$message_id";
1440 }
Ryan Anderson78488b22005-07-31 20:04:24 -04001441 }
Jeff King4f3d3702007-12-17 15:51:34 -05001442 $message_id = undef;
Ryan Anderson83b24432005-07-31 04:17:25 -04001443}
Ryan Andersone2057352005-08-02 21:45:22 -04001444
Joe Perches6e74e072010-09-24 10:03:00 -07001445# Execute a command (e.g. $to_cmd) to get a list of email addresses
1446# and return a results array
1447sub recipients_cmd {
1448 my ($prefix, $what, $cmd, $file) = @_;
1449
Joe Perches6e74e072010-09-24 10:03:00 -07001450 my @addresses = ();
Ramkumar Ramachandraa47eab02013-03-31 18:40:42 -07001451 open my $fh, "-|", "$cmd \Q$file\E"
Joe Perches6e74e072010-09-24 10:03:00 -07001452 or die "($prefix) Could not execute '$cmd'";
Junio C Hamano7ebee442010-10-26 22:02:52 -07001453 while (my $address = <$fh>) {
Joe Perches6e74e072010-09-24 10:03:00 -07001454 $address =~ s/^\s*//g;
1455 $address =~ s/\s*$//g;
1456 $address = sanitize_address($address);
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001457 next if ($address eq $sender and $suppress_cc{'self'});
Joe Perches6e74e072010-09-24 10:03:00 -07001458 push @addresses, $address;
1459 printf("($prefix) Adding %s: %s from: '%s'\n",
1460 $what, $address, $cmd) unless $quiet;
1461 }
Junio C Hamano7ebee442010-10-26 22:02:52 -07001462 close $fh
Joe Perches6e74e072010-09-24 10:03:00 -07001463 or die "($prefix) failed to close pipe to '$cmd'";
1464 return @addresses;
1465}
1466
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001467cleanup_compose_files();
Ryan Anderson1f038a02005-09-05 01:13:07 -04001468
Ævar Arnfjörð Bjarmason4bf597e2010-09-30 13:43:00 +00001469sub cleanup_compose_files {
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001470 unlink($compose_filename, $compose_filename . ".final") if $compose;
Ryan Anderson1f038a02005-09-05 01:13:07 -04001471}
1472
Eric Wong4bc87a22006-03-25 17:20:48 -08001473$smtp->quit if $smtp;
Ryan Andersone2057352005-08-02 21:45:22 -04001474
Ævar Arnfjörð Bjarmasonc438ea22010-09-30 13:42:59 +00001475sub unique_email_list {
Ryan Andersone2057352005-08-02 21:45:22 -04001476 my %seen;
1477 my @emails;
1478
1479 foreach my $entry (@_) {
Krzysztof Mazure4312252012-11-22 19:12:10 +01001480 my $clean = extract_valid_address_or_die($entry);
1481 $seen{$clean} ||= 0;
1482 next if $seen{$clean}++;
1483 push @emails, $entry;
Ryan Andersone2057352005-08-02 21:45:22 -04001484 }
1485 return @emails;
1486}
Jeff King747bbff2008-01-18 09:19:48 -05001487
1488sub validate_patch {
1489 my $fn = shift;
1490 open(my $fh, '<', $fn)
1491 or die "unable to open $fn: $!\n";
1492 while (my $line = <$fh>) {
1493 if (length($line) > 998) {
1494 return "$.: patch contains a line longer than 998 characters";
1495 }
1496 }
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -07001497 return;
Jeff King747bbff2008-01-18 09:19:48 -05001498}
Jeff King0706bd12008-03-28 17:28:33 -04001499
1500sub file_has_nonascii {
1501 my $fn = shift;
1502 open(my $fh, '<', $fn)
1503 or die "unable to open $fn: $!\n";
1504 while (my $line = <$fh>) {
1505 return 1 if $line =~ /[^[:ascii:]]/;
1506 }
1507 return 0;
1508}
Thomas Rast3cae7e52010-06-17 22:10:39 +02001509
1510sub body_or_subject_has_nonascii {
1511 my $fn = shift;
1512 open(my $fh, '<', $fn)
1513 or die "unable to open $fn: $!\n";
1514 while (my $line = <$fh>) {
1515 last if $line =~ /^$/;
1516 return 1 if $line =~ /^Subject.*[^[:ascii:]]/;
1517 }
1518 while (my $line = <$fh>) {
1519 return 1 if $line =~ /[^[:ascii:]]/;
1520 }
1521 return 0;
1522}