blob: fdb0029b597898559376b1b28d692450794251f3 [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'.
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -070072 --smtp-ssl-cert-path <str> * Path to ca-certificates (either directory or file).
73 Pass an empty string to disable certificate
74 verification.
Jari Aalto134550f2010-03-14 17:16:45 +020075 --smtp-domain <str> * The domain name sent to HELO/EHLO handshake
Jari Aaltof60812e2010-03-14 17:16:09 +020076 --smtp-debug <0|1> * Disable, enable Net::SMTP debug.
Michael Witten4ed62b02008-09-30 07:58:30 -050077
78 Automating:
79 --identity <str> * Use the sendemail.<id> options.
Joe Perches6e74e072010-09-24 10:03:00 -070080 --to-cmd <str> * Email To: via `<str> \$patch_path`
Michael Witten4ed62b02008-09-30 07:58:30 -050081 --cc-cmd <str> * Email Cc: via `<str> \$patch_path`
Jay Soffian3531e272009-02-14 23:32:15 -050082 --suppress-cc <str> * author, self, sob, cc, cccmd, body, bodycc, all.
83 --[no-]signed-off-by-cc * Send to Signed-off-by: addresses. Default on.
Michael Witten4ed62b02008-09-30 07:58:30 -050084 --[no-]suppress-from * Send to self. Default off.
Junio C Hamano41fe87f2009-08-22 12:48:48 -070085 --[no-]chain-reply-to * Chain In-Reply-To: fields. Default off.
Michael Witten4ed62b02008-09-30 07:58:30 -050086 --[no-]thread * Use In-Reply-To: field. Default on.
87
88 Administering:
Jay Soffianc1f2aa42009-03-02 23:52:18 -050089 --confirm <str> * Confirm recipients before sending;
90 auto, cc, compose, always, or never.
Michael Witten4ed62b02008-09-30 07:58:30 -050091 --quiet * Output one line of info per email.
92 --dry-run * Don't actually send the emails.
93 --[no-]validate * Perform patch sanity checks. Default on.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +010094 --[no-]format-patch * understand any non optional arguments as
95 `git format-patch` ones.
Thomas Rasta03bc5b2009-06-08 23:34:12 +020096 --force * Send even if safety checks would prevent it.
Jeff Kingc764a0c2008-01-18 09:20:10 -050097
Michael Coleman1b0baf12007-02-27 22:47:54 -060098EOT
99 exit(1);
100}
101
Eric Wong4bc87a22006-03-25 17:20:48 -0800102# most mail servers generate the Date: header, but not all...
Jakub Narebski6bdca892006-07-07 20:57:55 +0200103sub format_2822_time {
104 my ($time) = @_;
105 my @localtm = localtime($time);
106 my @gmttm = gmtime($time);
107 my $localmin = $localtm[1] + $localtm[2] * 60;
108 my $gmtmin = $gmttm[1] + $gmttm[2] * 60;
109 if ($localtm[0] != $gmttm[0]) {
110 die "local zone differs from GMT by a non-minute interval\n";
111 }
112 if ((($gmttm[6] + 1) % 7) == $localtm[6]) {
113 $localmin += 1440;
114 } elsif ((($gmttm[6] - 1) % 7) == $localtm[6]) {
115 $localmin -= 1440;
116 } elsif ($gmttm[6] != $localtm[6]) {
117 die "local time offset greater than or equal to 24 hours\n";
118 }
119 my $offset = $localmin - $gmtmin;
120 my $offhour = $offset / 60;
121 my $offmin = abs($offset % 60);
122 if (abs($offhour) >= 24) {
123 die ("local time offset greater than or equal to 24 hours\n");
124 }
125
126 return sprintf("%s, %2d %s %d %02d:%02d:%02d %s%02d%02d",
127 qw(Sun Mon Tue Wed Thu Fri Sat)[$localtm[6]],
128 $localtm[3],
129 qw(Jan Feb Mar Apr May Jun
130 Jul Aug Sep Oct Nov Dec)[$localtm[4]],
131 $localtm[5]+1900,
132 $localtm[2],
133 $localtm[1],
134 $localtm[0],
135 ($offset >= 0) ? '+' : '-',
136 abs($offhour),
137 $offmin,
138 );
139}
Eric Wong4bc87a22006-03-25 17:20:48 -0800140
Eric Wong567ffeb2006-03-25 16:47:12 -0800141my $have_email_valid = eval { require Email::Valid; 1 };
Jay Soffian50126992009-02-14 23:32:14 -0500142my $have_mail_address = eval { require Mail::Address; 1 };
Eric Wong4bc87a22006-03-25 17:20:48 -0800143my $smtp;
Wincent Colaiuta5f5b6112007-11-21 13:35:05 +0100144my $auth;
Eric Wong4bc87a22006-03-25 17:20:48 -0800145
Ryan Anderson83b24432005-07-31 04:17:25 -0400146# Variables we fill in automatically, or via prompting:
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700147my (@to,$no_to,@initial_to,@cc,$no_cc,@initial_cc,@bcclist,$no_bcc,@xh,
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100148 $initial_reply_to,$initial_subject,@files,
149 $author,$sender,$smtp_authpass,$annotate,$compose,$time);
Ryan Anderson83b24432005-07-31 04:17:25 -0400150
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700151my $envelope_sender;
Ryan Anderson78488b22005-07-31 20:04:24 -0400152
Ryan Anderson91332612005-07-31 20:04:24 -0400153# Example reply to:
Ryan Anderson83b24432005-07-31 04:17:25 -0400154#$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
Ryan Anderson83b24432005-07-31 04:17:25 -0400155
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100156my $repo = eval { Git->repository() };
157my @repo = $repo ? ($repo) : ();
Junio C Hamano280242d2006-07-02 16:03:59 -0700158my $term = eval {
Jay Soffian0fb7fc72008-02-21 19:16:04 -0500159 $ENV{"GIT_SEND_EMAIL_NOTTY"}
160 ? new Term::ReadLine 'git-send-email', \*STDIN, \*STDOUT
161 : new Term::ReadLine 'git-send-email';
Junio C Hamano280242d2006-07-02 16:03:59 -0700162};
163if ($@) {
164 $term = new FakeTerm "$@: going non-interactive";
165}
Ryan Anderson83b24432005-07-31 04:17:25 -0400166
Adam Roben5483c712007-06-27 20:59:37 -0700167# Behavior modification variables
168my ($quiet, $dry_run) = (0, 0);
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100169my $format_patch;
Jay Soffianafe756c2009-02-23 13:51:37 -0500170my $compose_filename;
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200171my $force = 0;
Adam Roben5483c712007-06-27 20:59:37 -0700172
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100173# Handle interactive edition of files.
174my $multiedit;
Michael J Gruber0ce142c2010-03-22 17:12:53 +0100175my $editor;
Jonathan Niederb4479f02009-10-30 20:42:34 -0500176
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100177sub do_edit {
Michael J Gruber0ce142c2010-03-22 17:12:53 +0100178 if (!defined($editor)) {
179 $editor = Git::command_oneline('var', 'GIT_EDITOR');
180 }
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100181 if (defined($multiedit) && !$multiedit) {
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100182 map {
183 system('sh', '-c', $editor.' "$@"', $editor, $_);
184 if (($? & 127) || ($? >> 8)) {
185 die("the editor exited uncleanly, aborting everything");
186 }
187 } @_;
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100188 } else {
189 system('sh', '-c', $editor.' "$@"', $editor, @_);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100190 if (($? & 127) || ($? >> 8)) {
191 die("the editor exited uncleanly, aborting everything");
192 }
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100193 }
194}
Adam Roben5483c712007-06-27 20:59:37 -0700195
196# Variables with corresponding config settings
Joe Perches6e74e072010-09-24 10:03:00 -0700197my ($thread, $chain_reply_to, $suppress_from, $signed_off_by_cc);
198my ($to_cmd, $cc_cmd);
Pascal Obry052fbea2010-09-06 20:12:11 +0200199my ($smtp_server, $smtp_server_port, @smtp_server_options);
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -0700200my ($smtp_authuser, $smtp_encryption, $smtp_ssl_cert_path);
Pascal Obry1d02a002010-09-06 20:12:10 +0200201my ($identity, $aliasfiletype, @alias_files, $smtp_domain);
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500202my ($validate, $confirm);
David Brown65648282007-12-25 19:56:29 -0800203my (@suppress_cc);
Thomas Rast3cae7e52010-06-17 22:10:39 +0200204my ($auto_8bit_encoding);
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200205my ($compose_encoding);
Adam Roben5483c712007-06-27 20:59:37 -0700206
Jari Aaltof60812e2010-03-14 17:16:09 +0200207my ($debug_net_smtp) = 0; # Net::SMTP, see send_message()
208
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900209my %config_bool_settings = (
Adam Roben5483c712007-06-27 20:59:37 -0700210 "thread" => [\$thread, 1],
Felipe Contrerasb99d22f2013-05-24 22:44:52 -0500211 "chainreplyto" => [\$chain_reply_to, 0],
David Brown65648282007-12-25 19:56:29 -0800212 "suppressfrom" => [\$suppress_from, undef],
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500213 "signedoffbycc" => [\$signed_off_by_cc, undef],
214 "signedoffcc" => [\$signed_off_by_cc, undef], # Deprecated
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500215 "validate" => [\$validate, 1],
Felipe Contreras402596a2013-04-07 01:10:27 -0600216 "multiedit" => [\$multiedit, undef],
217 "annotate" => [\$annotate, undef]
Adam Robene46f7a02007-06-26 15:48:30 -0700218);
219
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900220my %config_settings = (
221 "smtpserver" => \$smtp_server,
Junio C Hamano44b24762007-09-25 17:27:54 -0700222 "smtpserverport" => \$smtp_server_port,
Pascal Obry052fbea2010-09-06 20:12:11 +0200223 "smtpserveroption" => \@smtp_server_options,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900224 "smtpuser" => \$smtp_authuser,
225 "smtppass" => \$smtp_authpass,
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -0700226 "smtpsslcertpath" => \$smtp_ssl_cert_path,
Pascal Obrye1e91152010-09-06 20:12:09 +0200227 "smtpdomain" => \$smtp_domain,
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700228 "to" => \@initial_to,
Joe Perches6e74e072010-09-24 10:03:00 -0700229 "tocmd" => \$to_cmd,
Miklos Vajna5f8b9fc2008-04-27 14:14:58 +0200230 "cc" => \@initial_cc,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900231 "cccmd" => \$cc_cmd,
232 "aliasfiletype" => \$aliasfiletype,
233 "bcc" => \@bcclist,
David Brown65648282007-12-25 19:56:29 -0800234 "suppresscc" => \@suppress_cc,
Ask Bjørn Hansen9f7820a2008-06-07 00:33:42 -0700235 "envelopesender" => \$envelope_sender,
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500236 "confirm" => \$confirm,
Trent Piepho09caa242009-05-12 15:48:56 -0700237 "from" => \$sender,
Thomas Rast3cae7e52010-06-17 22:10:39 +0200238 "assume8bitencoding" => \$auto_8bit_encoding,
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200239 "composeencoding" => \$compose_encoding,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900240);
Avi Kivity4a62d3f2007-03-11 19:19:44 +0200241
Cord Seelecec5dae2011-09-30 12:52:25 +0200242my %config_path_settings = (
243 "aliasesfile" => \@alias_files,
244);
245
Michael Witten87429972008-02-03 19:53:57 -0500246# Handle Uncouth Termination
247sub signal_handler {
248
249 # Make text normal
250 print color("reset"), "\n";
251
252 # SMTP password masked
253 system "stty echo";
254
255 # tmp files from --compose
Jay Soffianafe756c2009-02-23 13:51:37 -0500256 if (defined $compose_filename) {
257 if (-e $compose_filename) {
258 print "'$compose_filename' contains an intermediate version of the email you were composing.\n";
259 }
260 if (-e ($compose_filename . ".final")) {
261 print "'$compose_filename.final' contains the composed email.\n"
262 }
Michael Witten87429972008-02-03 19:53:57 -0500263 }
264
265 exit;
266};
267
268$SIG{TERM} = \&signal_handler;
269$SIG{INT} = \&signal_handler;
270
Ryan Anderson83b24432005-07-31 04:17:25 -0400271# Begin by accumulating all the variables (defined above), that we will end up
272# needing, first, from the command line:
273
Clemens Buchacherc5978242011-09-03 19:06:13 +0200274my $help;
275my $rc = GetOptions("h" => \$help,
276 "sender|from=s" => \$sender,
Ryan Anderson83b24432005-07-31 04:17:25 -0400277 "in-reply-to=s" => \$initial_reply_to,
278 "subject=s" => \$initial_subject,
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700279 "to=s" => \@initial_to,
Joe Perches6e74e072010-09-24 10:03:00 -0700280 "to-cmd=s" => \$to_cmd,
Stephen Boydf434c082010-03-07 14:46:48 -0800281 "no-to" => \$no_to,
Ryan Andersonda140f82006-02-13 03:05:15 -0500282 "cc=s" => \@initial_cc,
Stephen Boydf434c082010-03-07 14:46:48 -0800283 "no-cc" => \$no_cc,
Ryan Anderson58063242006-05-29 12:30:13 -0700284 "bcc=s" => \@bcclist,
Stephen Boydf434c082010-03-07 14:46:48 -0800285 "no-bcc" => \$no_bcc,
Ryan Anderson78488b22005-07-31 20:04:24 -0400286 "chain-reply-to!" => \$chain_reply_to,
Ryan Anderson3342d852005-07-31 20:04:24 -0400287 "smtp-server=s" => \$smtp_server,
Pascal Obry052fbea2010-09-06 20:12:11 +0200288 "smtp-server-option=s" => \@smtp_server_options,
Junio C Hamano44b24762007-09-25 17:27:54 -0700289 "smtp-server-port=s" => \$smtp_server_port,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900290 "smtp-user=s" => \$smtp_authuser,
Michael Witten2363d742008-02-03 19:53:56 -0500291 "smtp-pass:s" => \$smtp_authpass,
Thomas Rastf6bebd12008-06-25 21:42:43 +0200292 "smtp-ssl" => sub { $smtp_encryption = 'ssl' },
293 "smtp-encryption=s" => \$smtp_encryption,
Thomas Rast979e6522013-12-01 23:48:42 +0100294 "smtp-ssl-cert-path=s" => \$smtp_ssl_cert_path,
Jari Aaltof60812e2010-03-14 17:16:09 +0200295 "smtp-debug:i" => \$debug_net_smtp,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -0400296 "smtp-domain:s" => \$smtp_domain,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900297 "identity=s" => \$identity,
Felipe Contreras402596a2013-04-07 01:10:27 -0600298 "annotate!" => \$annotate,
Ryan Anderson1f038a02005-09-05 01:13:07 -0400299 "compose" => \$compose,
Ryan Anderson30d08b32006-02-02 11:56:06 -0500300 "quiet" => \$quiet,
Joe Perches324a8bd2007-08-17 18:51:12 -0700301 "cc-cmd=s" => \$cc_cmd,
Adam Roben5483c712007-06-27 20:59:37 -0700302 "suppress-from!" => \$suppress_from,
David Brown65648282007-12-25 19:56:29 -0800303 "suppress-cc=s" => \@suppress_cc,
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500304 "signed-off-cc|signed-off-by-cc!" => \$signed_off_by_cc,
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500305 "confirm=s" => \$confirm,
Matthew Wilcox61302592006-10-10 08:58:23 -0600306 "dry-run" => \$dry_run,
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700307 "envelope-sender=s" => \$envelope_sender,
Adam Roben5483c712007-06-27 20:59:37 -0700308 "thread!" => \$thread,
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500309 "validate!" => \$validate,
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100310 "format-patch!" => \$format_patch,
Thomas Rast3cae7e52010-06-17 22:10:39 +0200311 "8bit-encoding=s" => \$auto_8bit_encoding,
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200312 "compose-encoding=s" => \$compose_encoding,
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200313 "force" => \$force,
Ryan Anderson83b24432005-07-31 04:17:25 -0400314 );
315
Clemens Buchacherc5978242011-09-03 19:06:13 +0200316usage() if $help;
Michael Coleman1b0baf12007-02-27 22:47:54 -0600317unless ($rc) {
318 usage();
319}
320
Jay Soffianeed6ca72009-02-14 23:32:13 -0500321die "Cannot run git format-patch from outside a repository\n"
322 if $format_patch and not $repo;
323
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900324# Now, let's fill any that aren't set in with defaults:
325
326sub read_config {
327 my ($prefix) = @_;
328
329 foreach my $setting (keys %config_bool_settings) {
330 my $target = $config_bool_settings{$setting}->[0];
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100331 $$target = Git::config_bool(@repo, "$prefix.$setting") unless (defined $$target);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900332 }
333
Cord Seelecec5dae2011-09-30 12:52:25 +0200334 foreach my $setting (keys %config_path_settings) {
Cord Seele463b0ea2011-10-14 22:53:31 +0200335 my $target = $config_path_settings{$setting};
336 if (ref($target) eq "ARRAY") {
337 unless (@$target) {
338 my @values = Git::config_path(@repo, "$prefix.$setting");
339 @$target = @values if (@values && defined $values[0]);
340 }
341 }
342 else {
343 $$target = Git::config_path(@repo, "$prefix.$setting") unless (defined $$target);
344 }
Cord Seelecec5dae2011-09-30 12:52:25 +0200345 }
346
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900347 foreach my $setting (keys %config_settings) {
348 my $target = $config_settings{$setting};
Stephen Boydf434c082010-03-07 14:46:48 -0800349 next if $setting eq "to" and defined $no_to;
350 next if $setting eq "cc" and defined $no_cc;
351 next if $setting eq "bcc" and defined $no_bcc;
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900352 if (ref($target) eq "ARRAY") {
353 unless (@$target) {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100354 my @values = Git::config(@repo, "$prefix.$setting");
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900355 @$target = @values if (@values && defined $values[0]);
356 }
357 }
358 else {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100359 $$target = Git::config(@repo, "$prefix.$setting") unless (defined $$target);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900360 }
361 }
Thomas Rastf6bebd12008-06-25 21:42:43 +0200362
363 if (!defined $smtp_encryption) {
364 my $enc = Git::config(@repo, "$prefix.smtpencryption");
365 if (defined $enc) {
366 $smtp_encryption = $enc;
367 } elsif (Git::config_bool(@repo, "$prefix.smtpssl")) {
368 $smtp_encryption = 'ssl';
369 }
370 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900371}
372
373# read configuration from [sendemail "$identity"], fall back on [sendemail]
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100374$identity = Git::config(@repo, "sendemail.identity") unless (defined $identity);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900375read_config("sendemail.$identity") if (defined $identity);
376read_config("sendemail");
377
378# fall back on builtin bool defaults
379foreach my $setting (values %config_bool_settings) {
380 ${$setting->[0]} = $setting->[1] unless (defined (${$setting->[0]}));
381}
382
Thomas Rastfa835cd2008-06-26 23:03:21 +0200383# 'default' encryption is none -- this only prevents a warning
384$smtp_encryption = '' unless (defined $smtp_encryption);
385
David Brown65648282007-12-25 19:56:29 -0800386# Set CC suppressions
387my(%suppress_cc);
388if (@suppress_cc) {
389 foreach my $entry (@suppress_cc) {
390 die "Unknown --suppress-cc field: '$entry'\n"
Ævar Arnfjörð Bjarmasone9bf7412010-09-30 13:43:04 +0000391 unless $entry =~ /^(?:all|cccmd|cc|author|self|sob|body|bodycc)$/;
David Brown65648282007-12-25 19:56:29 -0800392 $suppress_cc{$entry} = 1;
393 }
394}
395
396if ($suppress_cc{'all'}) {
Paolo Bonzinicb8a9bd2009-06-18 14:31:32 +0200397 foreach my $entry (qw (cccmd cc author self sob body bodycc)) {
David Brown65648282007-12-25 19:56:29 -0800398 $suppress_cc{$entry} = 1;
399 }
400 delete $suppress_cc{'all'};
401}
402
403# If explicit old-style ones are specified, they trump --suppress-cc.
404$suppress_cc{'self'} = $suppress_from if defined $suppress_from;
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500405$suppress_cc{'sob'} = !$signed_off_by_cc if defined $signed_off_by_cc;
David Brown65648282007-12-25 19:56:29 -0800406
Jay Soffian3531e272009-02-14 23:32:15 -0500407if ($suppress_cc{'body'}) {
408 foreach my $entry (qw (sob bodycc)) {
409 $suppress_cc{$entry} = 1;
410 }
411 delete $suppress_cc{'body'};
412}
413
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500414# Set confirm's default value
415my $confirm_unconfigured = !defined $confirm;
416if ($confirm_unconfigured) {
417 $confirm = scalar %suppress_cc ? 'compose' : 'auto';
418};
419die "Unknown --confirm setting: '$confirm'\n"
420 unless $confirm =~ /^(?:auto|cc|compose|always|never)/;
421
David Brown65648282007-12-25 19:56:29 -0800422# Debugging, print out the suppressions.
423if (0) {
424 print "suppressions:\n";
425 foreach my $entry (keys %suppress_cc) {
426 printf " %-5s -> $suppress_cc{$entry}\n", $entry;
427 }
428}
429
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100430my ($repoauthor, $repocommitter);
431($repoauthor) = Git::ident_person(@repo, 'author');
432($repocommitter) = Git::ident_person(@repo, 'committer');
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900433
Eric W. Biederman79ee5552006-06-21 07:17:31 -0600434# Verify the user input
435
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700436foreach my $entry (@initial_to) {
Eric W. Biederman79ee5552006-06-21 07:17:31 -0600437 die "Comma in --to entry: $entry'\n" unless $entry !~ m/,/;
438}
439
440foreach my $entry (@initial_cc) {
441 die "Comma in --cc entry: $entry'\n" unless $entry !~ m/,/;
442}
443
444foreach my $entry (@bcclist) {
445 die "Comma in --bcclist entry: $entry'\n" unless $entry !~ m/,/;
446}
447
Jay Soffian50126992009-02-14 23:32:14 -0500448sub parse_address_line {
449 if ($have_mail_address) {
450 return map { $_->format } Mail::Address->parse($_[0]);
451 } else {
452 return split_addrs($_[0]);
453 }
454}
455
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800456sub split_addrs {
Junio C Hamano2f0e7cb2008-12-21 01:57:59 -0800457 return quotewords('\s*,\s*', 1, @_);
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800458}
459
Eric Wong994d6c62006-05-14 19:13:44 -0700460my %aliases;
Eric Wong994d6c62006-05-14 19:13:44 -0700461my %parse_alias = (
462 # multiline formats can be supported in the future
463 mutt => sub { my $fh = shift; while (<$fh>) {
Felipe Contrerasffc01f92009-09-30 17:49:36 +0300464 if (/^\s*alias\s+(?:-group\s+\S+\s+)*(\S+)\s+(.*)$/) {
Eric Wong994d6c62006-05-14 19:13:44 -0700465 my ($alias, $addr) = ($1, $2);
466 $addr =~ s/#.*$//; # mutt allows # comments
467 # commas delimit multiple addresses
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800468 $aliases{$alias} = [ split_addrs($addr) ];
Eric Wong994d6c62006-05-14 19:13:44 -0700469 }}},
470 mailrc => sub { my $fh = shift; while (<$fh>) {
471 if (/^alias\s+(\S+)\s+(.*)$/) {
472 # spaces delimit multiple addresses
Eric W. Biedermanfe87c922009-05-20 19:45:53 -0700473 $aliases{$1} = [ quotewords('\s+', 0, $2) ];
Eric Wong994d6c62006-05-14 19:13:44 -0700474 }}},
Trent Piepho73c427e2008-11-25 18:55:00 -0800475 pine => sub { my $fh = shift; my $f='\t[^\t]*';
476 for (my $x = ''; defined($x); $x = $_) {
477 chomp $x;
478 $x .= $1 while(defined($_ = <$fh>) && /^ +(.*)$/);
479 $x =~ /^(\S+)$f\t\(?([^\t]+?)\)?(:?$f){0,2}$/ or next;
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800480 $aliases{$1} = [ split_addrs($2) ];
Trent Piepho73c427e2008-11-25 18:55:00 -0800481 }},
Bill Pemberton7613ea32009-04-22 09:41:29 -0400482 elm => sub { my $fh = shift;
483 while (<$fh>) {
484 if (/^(\S+)\s+=\s+[^=]+=\s(\S+)/) {
485 my ($alias, $addr) = ($1, $2);
486 $aliases{$alias} = [ split_addrs($addr) ];
487 }
488 } },
489
Eric Wong994d6c62006-05-14 19:13:44 -0700490 gnus => sub { my $fh = shift; while (<$fh>) {
491 if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
492 $aliases{$1} = [ $2 ];
493 }}}
494);
495
Petr Baudis3cb8caf2006-07-03 22:47:58 +0200496if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) {
Eric Wong994d6c62006-05-14 19:13:44 -0700497 foreach my $file (@alias_files) {
498 open my $fh, '<', $file or die "opening $file: $!\n";
499 $parse_alias{$aliasfiletype}->($fh);
500 close $fh;
501 }
502}
503
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200504($sender) = expand_aliases($sender) if defined $sender;
Michael Hendricksae740a52007-07-04 19:11:36 -0600505
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700506# is_format_patch_arg($f) returns 0 if $f names a patch, or 1 if
507# $f is a revision list specification to be passed to format-patch.
508sub is_format_patch_arg {
Jay Soffianeed6ca72009-02-14 23:32:13 -0500509 return unless $repo;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100510 my $f = shift;
511 try {
512 $repo->command('rev-parse', '--verify', '--quiet', $f);
513 if (defined($format_patch)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100514 return $format_patch;
515 }
516 die(<<EOF);
517File '$f' exists but it could also be the range of commits
518to produce patches for. Please disambiguate by...
519
520 * Saying "./$f" if you mean a file; or
521 * Giving --format-patch option if you mean a range.
522EOF
523 } catch Git::Error::Command with {
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700524 # Not a valid revision. Treat it as a filename.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100525 return 0;
526 }
527}
528
Jeff Kingaa548922008-01-18 09:19:36 -0500529# Now that all the defaults are set, process the rest of the command line
530# arguments and collect up the files that need to be processed.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100531my @rev_list_opts;
Junio C Hamano69f4ce52008-11-30 22:38:20 -0800532while (defined(my $f = shift @ARGV)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100533 if ($f eq "--") {
534 push @rev_list_opts, "--", @ARGV;
535 @ARGV = ();
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700536 } elsif (-d $f and !is_format_patch_arg($f)) {
Ævar Arnfjörð Bjarmasonc6038162010-09-30 13:42:54 +0000537 opendir my $dh, $f
Jeff Kingaa548922008-01-18 09:19:36 -0500538 or die "Failed to opendir $f: $!";
539
Ævar Arnfjörð Bjarmason89bf1ba2010-09-14 19:02:24 +0000540 push @files, grep { -f $_ } map { catfile($f, $_) }
Ævar Arnfjörð Bjarmasonc6038162010-09-30 13:42:54 +0000541 sort readdir $dh;
542 closedir $dh;
Ramkumar Ramachandra9b397032013-03-31 18:40:41 -0700543 } elsif ((-f $f or -p $f) and !is_format_patch_arg($f)) {
Jeff Kingaa548922008-01-18 09:19:36 -0500544 push @files, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500545 } else {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100546 push @rev_list_opts, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500547 }
548}
549
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100550if (@rev_list_opts) {
Jay Soffianeed6ca72009-02-14 23:32:13 -0500551 die "Cannot run git format-patch from outside a repository\n"
552 unless $repo;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100553 push @files, $repo->command('format-patch', '-o', tempdir(CLEANUP => 1), @rev_list_opts);
554}
555
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500556if ($validate) {
Jeff Kingc764a0c2008-01-18 09:20:10 -0500557 foreach my $f (@files) {
Kevin Ballard300913b2008-06-25 15:44:40 -0700558 unless (-p $f) {
559 my $error = validate_patch($f);
560 $error and die "fatal: $f: $error\nwarning: no patches were sent\n";
561 }
Jeff Kingc764a0c2008-01-18 09:20:10 -0500562 }
Jeff King747bbff2008-01-18 09:19:48 -0500563}
564
Jeff Kingaa548922008-01-18 09:19:36 -0500565if (@files) {
566 unless ($quiet) {
567 print $_,"\n" for (@files);
568 }
569} else {
570 print STDERR "\nNo patch files specified!\n\n";
571 usage();
572}
573
Ævar Arnfjörð Bjarmasonacf071b2010-09-30 13:42:57 +0000574sub get_patch_subject {
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100575 my $fn = shift;
576 open (my $fh, '<', $fn);
577 while (my $line = <$fh>) {
578 next unless ($line =~ /^Subject: (.*)$/);
579 close $fh;
580 return "GIT: $1\n";
581 }
582 close $fh;
583 die "No subject line in $fn ?";
584}
585
586if ($compose) {
587 # Note that this does not need to be secure, but we will make a small
588 # effort to have it be unique
Jay Soffianafe756c2009-02-23 13:51:37 -0500589 $compose_filename = ($repo ?
590 tempfile(".gitsendemail.msg.XXXXXX", DIR => $repo->repo_path()) :
591 tempfile(".gitsendemail.msg.XXXXXX", DIR => "."))[1];
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000592 open my $c, ">", $compose_filename
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100593 or die "Failed to open for writing $compose_filename: $!";
594
595
596 my $tpl_sender = $sender || $repoauthor || $repocommitter || '';
597 my $tpl_subject = $initial_subject || '';
598 my $tpl_reply_to = $initial_reply_to || '';
599
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000600 print $c <<EOT;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100601From $tpl_sender # This line is ignored.
Michael Witten40e6e8a2009-04-13 13:23:50 -0500602GIT: Lines beginning in "GIT:" will be removed.
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100603GIT: Consider including an overall diffstat or table of contents
604GIT: for the patch you are writing.
605GIT:
606GIT: Clear the body content if you don't wish to send a summary.
607From: $tpl_sender
608Subject: $tpl_subject
609In-Reply-To: $tpl_reply_to
610
611EOT
612 for my $f (@files) {
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000613 print $c get_patch_subject($f);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100614 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000615 close $c;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100616
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100617 if ($annotate) {
618 do_edit($compose_filename, @files);
619 } else {
620 do_edit($compose_filename);
621 }
622
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000623 open my $c2, ">", $compose_filename . ".final"
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100624 or die "Failed to open $compose_filename.final : " . $!;
625
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000626 open $c, "<", $compose_filename
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100627 or die "Failed to open $compose_filename : " . $!;
628
629 my $need_8bit_cte = file_has_nonascii($compose_filename);
630 my $in_body = 0;
631 my $summary_empty = 1;
Krzysztof Mazur4a47a4d2012-10-22 14:41:48 +0200632 if (!defined $compose_encoding) {
633 $compose_encoding = "UTF-8";
634 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000635 while(<$c>) {
Michael Witten40e6e8a2009-04-13 13:23:50 -0500636 next if m/^GIT:/;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100637 if ($in_body) {
638 $summary_empty = 0 unless (/^\n$/);
639 } elsif (/^\n$/) {
640 $in_body = 1;
641 if ($need_8bit_cte) {
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000642 print $c2 "MIME-Version: 1.0\n",
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100643 "Content-Type: text/plain; ",
Krzysztof Mazur62e00692012-10-10 01:02:56 +0200644 "charset=$compose_encoding\n",
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100645 "Content-Transfer-Encoding: 8bit\n";
646 }
647 } elsif (/^MIME-Version:/i) {
648 $need_8bit_cte = 0;
649 } elsif (/^Subject:\s*(.+)\s*$/i) {
650 $initial_subject = $1;
651 my $subject = $initial_subject;
652 $_ = "Subject: " .
Krzysztof Mazurce547802012-10-24 23:08:26 +0200653 quote_subject($subject, $compose_encoding) .
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100654 "\n";
655 } elsif (/^In-Reply-To:\s*(.+)\s*$/i) {
656 $initial_reply_to = $1;
657 next;
658 } elsif (/^From:\s*(.+)\s*$/i) {
659 $sender = $1;
660 next;
661 } elsif (/^(?:To|Cc|Bcc):/i) {
662 print "To/Cc/Bcc fields are not interpreted yet, they have been ignored\n";
663 next;
664 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000665 print $c2 $_;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100666 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000667 close $c;
668 close $c2;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100669
670 if ($summary_empty) {
671 print "Summary email is empty, skipping it\n";
672 $compose = -1;
673 }
674} elsif ($annotate) {
675 do_edit(@files);
676}
677
Jay Soffian6e182512009-03-28 21:39:10 -0400678sub ask {
679 my ($prompt, %arg) = @_;
Jay Soffian0da43a62009-04-04 23:23:21 -0400680 my $valid_re = $arg{valid_re};
Jay Soffian6e182512009-03-28 21:39:10 -0400681 my $default = $arg{default};
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700682 my $confirm_only = $arg{confirm_only};
Jay Soffian6e182512009-03-28 21:39:10 -0400683 my $resp;
684 my $i = 0;
Jay Soffian5906f542009-03-31 12:22:11 -0400685 return defined $default ? $default : undef
686 unless defined $term->IN and defined fileno($term->IN) and
687 defined $term->OUT and defined fileno($term->OUT);
Jay Soffian6e182512009-03-28 21:39:10 -0400688 while ($i++ < 10) {
689 $resp = $term->readline($prompt);
690 if (!defined $resp) { # EOF
691 print "\n";
692 return defined $default ? $default : undef;
693 }
694 if ($resp eq '' and defined $default) {
695 return $default;
696 }
Jay Soffian0da43a62009-04-04 23:23:21 -0400697 if (!defined $valid_re or $resp =~ /$valid_re/) {
Jay Soffian6e182512009-03-28 21:39:10 -0400698 return $resp;
699 }
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700700 if ($confirm_only) {
701 my $yesno = $term->readline("Are you sure you want to use <$resp> [y/N]? ");
702 if (defined $yesno && $yesno =~ /y/i) {
703 return $resp;
704 }
705 }
Jay Soffian6e182512009-03-28 21:39:10 -0400706 }
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -0700707 return;
Jay Soffian6e182512009-03-28 21:39:10 -0400708}
709
Thomas Rast3cae7e52010-06-17 22:10:39 +0200710my %broken_encoding;
711
Ævar Arnfjörð Bjarmason1d50bfd2010-09-30 13:42:58 +0000712sub file_declares_8bit_cte {
Thomas Rast3cae7e52010-06-17 22:10:39 +0200713 my $fn = shift;
714 open (my $fh, '<', $fn);
715 while (my $line = <$fh>) {
716 last if ($line =~ /^$/);
717 return 1 if ($line =~ /^Content-Transfer-Encoding: .*8bit.*$/);
718 }
719 close $fh;
720 return 0;
721}
722
723foreach my $f (@files) {
724 next unless (body_or_subject_has_nonascii($f)
725 && !file_declares_8bit_cte($f));
726 $broken_encoding{$f} = 1;
727}
728
729if (!defined $auto_8bit_encoding && scalar %broken_encoding) {
730 print "The following files are 8bit, but do not declare " .
731 "a Content-Transfer-Encoding.\n";
732 foreach my $f (sort keys %broken_encoding) {
733 print " $f\n";
734 }
735 $auto_8bit_encoding = ask("Which 8bit encoding should I declare [UTF-8]? ",
736 default => "UTF-8");
737}
738
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200739if (!$force) {
740 for my $f (@files) {
Ævar Arnfjörð Bjarmason0d290a42010-09-30 13:43:01 +0000741 if (get_patch_subject($f) =~ /\Q*** SUBJECT HERE ***\E/) {
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200742 die "Refusing to send because the patch\n\t$f\n"
743 . "has the template subject '*** SUBJECT HERE ***'. "
744 . "Pass --force if you really want to send.\n";
745 }
746 }
747}
748
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200749if (!defined $sender) {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100750 $sender = $repoauthor || $repocommitter || '';
Ryan Anderson83b24432005-07-31 04:17:25 -0400751}
752
Michael S. Tsirkinda187592013-06-05 21:11:00 +0300753# $sender could be an already sanitized address
754# (e.g. sendemail.from could be manually sanitized by user).
755# But it's a no-op to run sanitize_address on an already sanitized address.
756$sender = sanitize_address($sender);
757
Felipe Contreras8cac13d2012-11-24 12:16:19 +0100758my $prompting = 0;
Junio C Hamano8796ff72010-10-26 22:02:03 -0700759if (!@initial_to && !defined $to_cmd) {
Stephen Boyd61837492012-09-06 11:31:11 -0700760 my $to = ask("Who should the emails be sent to (if any)? ",
761 default => "",
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700762 valid_re => qr/\@.*\./, confirm_only => 1);
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700763 push @initial_to, parse_address_line($to) if defined $to; # sanitized/validated later
Ryan Anderson1f038a02005-09-05 01:13:07 -0400764 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -0400765}
766
Eric Wong994d6c62006-05-14 19:13:44 -0700767sub expand_aliases {
Jeff King302e04e2009-07-23 07:09:29 -0400768 return map { expand_one_alias($_) } @_;
769}
770
771my %EXPANDED_ALIASES;
772sub expand_one_alias {
773 my $alias = shift;
774 if ($EXPANDED_ALIASES{$alias}) {
775 die "fatal: alias '$alias' expands to itself\n";
776 }
777 local $EXPANDED_ALIASES{$alias} = 1;
778 return $aliases{$alias} ? expand_aliases(@{$aliases{$alias}}) : $alias;
Eric Wong994d6c62006-05-14 19:13:44 -0700779}
780
Stephen Boyd3c3bb512010-10-04 00:05:24 -0700781@initial_to = expand_aliases(@initial_to);
Krzysztof Mazure4312252012-11-22 19:12:10 +0100782@initial_to = validate_address_list(sanitize_address_list(@initial_to));
Eric Wong994d6c62006-05-14 19:13:44 -0700783@initial_cc = expand_aliases(@initial_cc);
Krzysztof Mazure4312252012-11-22 19:12:10 +0100784@initial_cc = validate_address_list(sanitize_address_list(@initial_cc));
Ryan Anderson58063242006-05-29 12:30:13 -0700785@bcclist = expand_aliases(@bcclist);
Krzysztof Mazure4312252012-11-22 19:12:10 +0100786@bcclist = validate_address_list(sanitize_address_list(@bcclist));
Eric Wong994d6c62006-05-14 19:13:44 -0700787
Adam Roben5483c712007-06-27 20:59:37 -0700788if ($thread && !defined $initial_reply_to && $prompting) {
Jay Soffian6e182512009-03-28 21:39:10 -0400789 $initial_reply_to = ask(
Stephen Boyd61837492012-09-06 11:31:11 -0700790 "Message-ID to be used as In-Reply-To for the first email (if any)? ",
791 default => "",
Junio C Hamano51bbccf2012-08-14 15:15:53 -0700792 valid_re => qr/\@.*\./, confirm_only => 1);
Ryan Anderson83b24432005-07-31 04:17:25 -0400793}
Jay Soffian1ca3d6e2008-02-20 00:55:07 -0500794if (defined $initial_reply_to) {
Jay Soffian0fb7fc72008-02-21 19:16:04 -0500795 $initial_reply_to =~ s/^\s*<?//;
796 $initial_reply_to =~ s/>?\s*$//;
797 $initial_reply_to = "<$initial_reply_to>" if $initial_reply_to ne '';
Junio C Hamanoace9c2a2007-12-10 21:44:42 -0800798}
Mike Hommeyace72082007-12-09 18:17:28 +0100799
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900800if (!defined $smtp_server) {
Eric Wongaca7ad72006-05-15 02:34:44 -0700801 foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) {
802 if (-x $_) {
803 $smtp_server = $_;
804 last;
805 }
806 }
807 $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
Ryan Anderson3342d852005-07-31 20:04:24 -0400808}
809
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500810if ($compose && $compose > 0) {
811 @files = ($compose_filename . ".final", @files);
Ryan Anderson1f038a02005-09-05 01:13:07 -0400812}
813
Ryan Anderson83b24432005-07-31 04:17:25 -0400814# Variables we set as part of the loop over files
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500815our ($message_id, %mail, $subject, $reply_to, $references, $message,
Jay Soffiandc1460a2009-03-31 12:22:12 -0400816 $needs_confirm, $message_num, $ask_default);
Ryan Anderson83b24432005-07-31 04:17:25 -0400817
Eric Wong567ffeb2006-03-25 16:47:12 -0800818sub extract_valid_address {
819 my $address = shift;
Ævar Arnfjörð Bjarmason35b6ab92010-09-30 19:03:31 +0000820 my $local_part_regexp = qr/[^<>"\s@]+/;
821 my $domain_regexp = qr/[^.<>"\s@]+(?:\.[^.<>"\s@]+)+/;
Eric Wongdb3106b2006-05-15 02:41:01 -0700822
823 # check for a local address:
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700824 return $address if ($address =~ /^($local_part_regexp)$/);
Eric Wongdb3106b2006-05-15 02:41:01 -0700825
Uwe Kleine-König155197e2007-08-09 15:27:57 +0200826 $address =~ s/^\s*<(.*)>\s*$/$1/;
Eric Wong567ffeb2006-03-25 16:47:12 -0800827 if ($have_email_valid) {
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700828 return scalar Email::Valid->address($address);
Eric Wong567ffeb2006-03-25 16:47:12 -0800829 }
Krzysztof Mazur95c0d4b2012-11-22 19:12:09 +0100830
831 # less robust/correct than the monster regexp in Email::Valid,
832 # but still does a 99% job, and one less dependency
833 return $1 if $address =~ /($local_part_regexp\@$domain_regexp)/;
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -0700834 return;
Eric Wong567ffeb2006-03-25 16:47:12 -0800835}
Ryan Anderson83b24432005-07-31 04:17:25 -0400836
Krzysztof Mazure4312252012-11-22 19:12:10 +0100837sub extract_valid_address_or_die {
838 my $address = shift;
839 $address = extract_valid_address($address);
840 die "error: unable to extract a valid address from: $address\n"
841 if !$address;
842 return $address;
843}
844
845sub validate_address {
846 my $address = shift;
Krzysztof Mazurd0e98102012-11-22 19:12:12 +0100847 while (!extract_valid_address($address)) {
Krzysztof Mazur5c80afe2012-11-22 19:12:11 +0100848 print STDERR "error: unable to extract a valid address from: $address\n";
Krzysztof Mazurd0e98102012-11-22 19:12:12 +0100849 $_ = ask("What to do with this address? ([q]uit|[d]rop|[e]dit): ",
850 valid_re => qr/^(?:quit|q|drop|d|edit|e)/i,
Krzysztof Mazur5c80afe2012-11-22 19:12:11 +0100851 default => 'q');
852 if (/^d/i) {
853 return undef;
854 } elsif (/^q/i) {
855 cleanup_compose_files();
856 exit(0);
857 }
Krzysztof Mazurd0e98102012-11-22 19:12:12 +0100858 $address = ask("Who should the email be sent to (if any)? ",
859 default => "",
860 valid_re => qr/\@.*\./, confirm_only => 1);
Krzysztof Mazure4312252012-11-22 19:12:10 +0100861 }
862 return $address;
863}
864
865sub validate_address_list {
866 return (grep { defined $_ }
867 map { validate_address($_) } @_);
Ryan Anderson83b24432005-07-31 04:17:25 -0400868}
869
870# Usually don't need to change anything below here.
871
872# we make a "fake" message id by taking the current number
873# of seconds since the beginning of Unix time and tacking on
874# a random number to the end, in case we are called quicker than
875# 1 second since the last time we were called.
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400876
877# We'll setup a template for the message id, using the "from" address:
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400878
Junio C Hamanobe510cf2007-09-17 21:18:20 -0700879my ($message_id_stamp, $message_id_serial);
Brian Gernhardt68ce9332010-04-10 10:53:53 -0400880sub make_message_id {
Junio C Hamanobe510cf2007-09-17 21:18:20 -0700881 my $uniq;
882 if (!defined $message_id_stamp) {
883 $message_id_stamp = sprintf("%s-%s", time, $$);
884 $message_id_serial = 0;
885 }
886 $message_id_serial++;
887 $uniq = "$message_id_stamp-$message_id_serial";
888
Junio C Hamanoaeb59322007-06-20 13:47:34 -0700889 my $du_part;
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200890 for ($sender, $repocommitter, $repoauthor) {
891 $du_part = extract_valid_address(sanitize_address($_));
892 last if (defined $du_part and $du_part ne '');
Junio C Hamanoaeb59322007-06-20 13:47:34 -0700893 }
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200894 if (not defined $du_part or $du_part eq '') {
Ævar Arnfjörð Bjarmason529dd382010-09-30 13:43:08 +0000895 require Sys::Hostname;
Junio C Hamanoaeb59322007-06-20 13:47:34 -0700896 $du_part = 'user@' . Sys::Hostname::hostname();
897 }
Junio C Hamanobe510cf2007-09-17 21:18:20 -0700898 my $message_id_template = "<%s-git-send-email-%s>";
899 $message_id = sprintf($message_id_template, $uniq, $du_part);
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400900 #print "new message id = $message_id\n"; # Was useful for debugging
Ryan Anderson83b24432005-07-31 04:17:25 -0400901}
902
903
904
Eric Wonga5370b12006-03-25 03:01:01 -0800905$time = time - scalar $#files;
Ryan Anderson83b24432005-07-31 04:17:25 -0400906
Jürgen Rühle374c5902007-01-10 13:36:39 -0800907sub unquote_rfc2047 {
908 local ($_) = @_;
Jeff King8291db62007-11-16 05:49:09 -0500909 my $encoding;
Thomas Rastb622d4d2012-07-30 21:25:40 +0200910 s{=\?([^?]+)\?q\?(.*?)\?=}{
Jeff King8291db62007-11-16 05:49:09 -0500911 $encoding = $1;
Thomas Rastb622d4d2012-07-30 21:25:40 +0200912 my $e = $2;
913 $e =~ s/_/ /g;
914 $e =~ s/=([0-9A-F]{2})/chr(hex($1))/eg;
915 $e;
916 }eg;
Jeff King8291db62007-11-16 05:49:09 -0500917 return wantarray ? ($_, $encoding) : $_;
Jürgen Rühle374c5902007-01-10 13:36:39 -0800918}
919
Jeff Kingd54eaaa2008-03-28 17:29:01 -0400920sub quote_rfc2047 {
921 local $_ = shift;
Brandon Caseyd1fff6f2009-06-06 20:12:31 -0500922 my $encoding = shift || 'UTF-8';
Jeff Kingd54eaaa2008-03-28 17:29:01 -0400923 s/([^-a-zA-Z0-9!*+\/])/sprintf("=%02X", ord($1))/eg;
924 s/(.*)/=\?$encoding\?q\?$1\?=/;
925 return $_;
926}
927
Brandon Caseya3a82622009-06-07 19:25:58 -0500928sub is_rfc2047_quoted {
929 my $s = shift;
Ævar Arnfjörð Bjarmason49f73852010-09-30 13:43:05 +0000930 my $token = qr/[^][()<>@,;:"\/?.= \000-\037\177-\377]+/;
931 my $encoded_text = qr/[!->@-~]+/;
Brandon Caseya3a82622009-06-07 19:25:58 -0500932 length($s) <= 75 &&
933 $s =~ m/^(?:"[[:ascii:]]*"|=\?$token\?$token\?$encoded_text\?=)$/o;
934}
935
Krzysztof Mazurce547802012-10-24 23:08:26 +0200936sub subject_needs_rfc2047_quoting {
937 my $s = shift;
938
Krzysztof Mazurce1459f2012-10-24 23:28:29 +0200939 return ($s =~ /[^[:ascii:]]/) || ($s =~ /=\?/);
Krzysztof Mazurce547802012-10-24 23:08:26 +0200940}
941
942sub quote_subject {
943 local $subject = shift;
944 my $encoding = shift || 'UTF-8';
945
946 if (subject_needs_rfc2047_quoting($subject)) {
947 return quote_rfc2047($subject, $encoding);
948 }
949 return $subject;
950}
951
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +0200952# use the simplest quoting being able to handle the recipient
Brian Gernhardt68ce9332010-04-10 10:53:53 -0400953sub sanitize_address {
Robin H. Johnson732263d2007-04-25 19:37:19 -0700954 my ($recipient) = @_;
Krzysztof Mazur831a4882012-11-22 19:12:08 +0100955
956 # remove garbage after email address
957 $recipient =~ s/(.*>).*$/$1/;
958
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +0200959 my ($recipient_name, $recipient_addr) = ($recipient =~ /^(.*?)\s*(<.*)/);
960
961 if (not $recipient_name) {
Ævar Arnfjörð Bjarmasonff483892010-09-30 13:43:02 +0000962 return $recipient;
Robin H. Johnson732263d2007-04-25 19:37:19 -0700963 }
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +0200964
965 # if recipient_name is already quoted, do nothing
Brandon Caseya3a82622009-06-07 19:25:58 -0500966 if (is_rfc2047_quoted($recipient_name)) {
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +0200967 return $recipient;
968 }
969
970 # rfc2047 is needed if a non-ascii char is included
971 if ($recipient_name =~ /[^[:ascii:]]/) {
Jay Soffiana61c0ff2009-03-31 12:22:14 -0400972 $recipient_name =~ s/^"(.*)"$/$1/;
Jeff Kingd54eaaa2008-03-28 17:29:01 -0400973 $recipient_name = quote_rfc2047($recipient_name);
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +0200974 }
975
976 # double quotes are needed if specials or CTLs are included
977 elsif ($recipient_name =~ /[][()<>@,;:\\".\000-\037\177]/) {
Horst H. von Brand18023c22008-03-28 11:09:04 -0300978 $recipient_name =~ s/(["\\\r])/\\$1/g;
Ævar Arnfjörð Bjarmasond5c7d692010-09-30 13:43:03 +0000979 $recipient_name = qq["$recipient_name"];
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +0200980 }
981
982 return "$recipient_name $recipient_addr";
983
Robin H. Johnson732263d2007-04-25 19:37:19 -0700984}
985
Krzysztof Mazure4312252012-11-22 19:12:10 +0100986sub sanitize_address_list {
987 return (map { sanitize_address($_) } @_);
988}
989
Jari Aalto134550f2010-03-14 17:16:45 +0200990# Returns the local Fully Qualified Domain Name (FQDN) if available.
991#
992# Tightly configured MTAa require that a caller sends a real DNS
993# domain name that corresponds the IP address in the HELO/EHLO
994# handshake. This is used to verify the connection and prevent
995# spammers from trying to hide their identity. If the DNS and IP don't
996# match, the receiveing MTA may deny the connection.
997#
998# Here is a deny example of Net::SMTP with the default "localhost.localdomain"
999#
1000# Net::SMTP=GLOB(0x267ec28)>>> EHLO localhost.localdomain
1001# Net::SMTP=GLOB(0x267ec28)<<< 550 EHLO argument does not match calling host
1002#
1003# This maildomain*() code is based on ideas in Perl library Test::Reporter
1004# /usr/share/perl5/Test/Reporter/Mail/Util.pm ==> sub _maildomain ()
1005
Brian Gernhardt59a86302010-04-10 10:53:54 -04001006sub valid_fqdn {
1007 my $domain = shift;
Brandon Casey61ef5e92010-09-26 22:18:01 -05001008 return defined $domain && !($^O eq 'darwin' && $domain =~ /\.local$/) && $domain =~ /\./;
Brian Gernhardt59a86302010-04-10 10:53:54 -04001009}
1010
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001011sub maildomain_net {
Jari Aalto134550f2010-03-14 17:16:45 +02001012 my $maildomain;
1013
1014 if (eval { require Net::Domain; 1 }) {
1015 my $domain = Net::Domain::domainname();
Brian Gernhardt59a86302010-04-10 10:53:54 -04001016 $maildomain = $domain if valid_fqdn($domain);
Jari Aalto134550f2010-03-14 17:16:45 +02001017 }
1018
1019 return $maildomain;
1020}
1021
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001022sub maildomain_mta {
Jari Aalto134550f2010-03-14 17:16:45 +02001023 my $maildomain;
1024
1025 if (eval { require Net::SMTP; 1 }) {
1026 for my $host (qw(mailhost localhost)) {
1027 my $smtp = Net::SMTP->new($host);
1028 if (defined $smtp) {
1029 my $domain = $smtp->domain;
1030 $smtp->quit;
1031
Brian Gernhardt59a86302010-04-10 10:53:54 -04001032 $maildomain = $domain if valid_fqdn($domain);
Jari Aalto134550f2010-03-14 17:16:45 +02001033
1034 last if $maildomain;
1035 }
1036 }
1037 }
1038
1039 return $maildomain;
1040}
1041
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001042sub maildomain {
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001043 return maildomain_net() || maildomain_mta() || 'localhost.localdomain';
Jari Aalto134550f2010-03-14 17:16:45 +02001044}
1045
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001046sub smtp_host_string {
1047 if (defined $smtp_server_port) {
1048 return "$smtp_server:$smtp_server_port";
1049 } else {
1050 return $smtp_server;
1051 }
1052}
1053
1054# Returns 1 if authentication succeeded or was not necessary
1055# (smtp_user was not specified), and 0 otherwise.
1056
1057sub smtp_auth_maybe {
1058 if (!defined $smtp_authuser || $auth) {
1059 return 1;
1060 }
1061
1062 # Workaround AUTH PLAIN/LOGIN interaction defect
1063 # with Authen::SASL::Cyrus
1064 eval {
1065 require Authen::SASL;
1066 Authen::SASL->import(qw(Perl));
1067 };
1068
1069 # TODO: Authentication may fail not because credentials were
1070 # invalid but due to other reasons, in which we should not
1071 # reject credentials.
1072 $auth = Git::credential({
1073 'protocol' => 'smtp',
1074 'host' => smtp_host_string(),
1075 'username' => $smtp_authuser,
1076 # if there's no password, "git credential fill" will
1077 # give us one, otherwise it'll just pass this one.
1078 'password' => $smtp_authpass
1079 }, sub {
1080 my $cred = shift;
1081 return !!$smtp->auth($cred->{'username'}, $cred->{'password'});
1082 });
1083
1084 return $auth;
1085}
1086
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001087sub ssl_verify_params {
1088 eval {
1089 require IO::Socket::SSL;
1090 IO::Socket::SSL->import(qw/SSL_VERIFY_PEER SSL_VERIFY_NONE/);
1091 };
1092 if ($@) {
1093 print STDERR "Not using SSL_VERIFY_PEER due to out-of-date IO::Socket::SSL.\n";
1094 return;
1095 }
1096
1097 if (!defined $smtp_ssl_cert_path) {
Ruben Kerkhof01645b72014-01-15 21:31:11 +04001098 # use the OpenSSL defaults
1099 return (SSL_verify_mode => SSL_VERIFY_PEER());
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001100 }
1101
1102 if ($smtp_ssl_cert_path eq "") {
1103 return (SSL_verify_mode => SSL_VERIFY_NONE());
1104 } elsif (-d $smtp_ssl_cert_path) {
1105 return (SSL_verify_mode => SSL_VERIFY_PEER(),
1106 SSL_ca_path => $smtp_ssl_cert_path);
1107 } elsif (-f $smtp_ssl_cert_path) {
1108 return (SSL_verify_mode => SSL_VERIFY_PEER(),
1109 SSL_ca_file => $smtp_ssl_cert_path);
1110 } else {
1111 print STDERR "Not using SSL_VERIFY_PEER because the CA path does not exist.\n";
1112 return (SSL_verify_mode => SSL_VERIFY_NONE());
1113 }
1114}
1115
Michael Witten15da1082009-04-13 13:23:51 -05001116# Returns 1 if the message was sent, and 0 otherwise.
Markus Heidelberga1b5b372009-06-12 12:51:42 +02001117# In actuality, the whole program dies when there
Michael Witten15da1082009-04-13 13:23:51 -05001118# is an error sending a message.
1119
Brian Gernhardt68ce9332010-04-10 10:53:53 -04001120sub send_message {
Eric Wong4bc87a22006-03-25 17:20:48 -08001121 my @recipients = unique_email_list(@to);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001122 @cc = (grep { my $cc = extract_valid_address_or_die($_);
Joe Perches83acaae2010-11-20 15:06:05 -08001123 not grep { $cc eq $_ || $_ =~ /<\Q${cc}\E>$/ } @recipients
Ask Bjørn Hansen7ac17522007-11-19 03:00:26 -08001124 }
Ask Bjørn Hansen7ac17522007-11-19 03:00:26 -08001125 @cc);
Eric Wong4bc87a22006-03-25 17:20:48 -08001126 my $to = join (",\n\t", @recipients);
Ryan Anderson58063242006-05-29 12:30:13 -07001127 @recipients = unique_email_list(@recipients,@cc,@bcclist);
Krzysztof Mazure4312252012-11-22 19:12:10 +01001128 @recipients = (map { extract_valid_address_or_die($_) } @recipients);
Jakub Narebski6bdca892006-07-07 20:57:55 +02001129 my $date = format_2822_time($time++);
Martin Langhoffe923eff2006-05-03 09:44:36 +12001130 my $gitversion = '@@GIT_VERSION@@';
1131 if ($gitversion =~ m/..GIT_VERSION../) {
Petr Baudis3cb8caf2006-07-03 22:47:58 +02001132 $gitversion = Git::version();
Martin Langhoffe923eff2006-05-03 09:44:36 +12001133 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001134
Joe Perches02461e02009-10-08 10:03:26 -07001135 my $cc = join(",\n\t", unique_email_list(@cc));
Junio C Hamanof06a6a42007-04-16 16:51:47 -07001136 my $ccline = "";
1137 if ($cc ne '') {
1138 $ccline = "\nCc: $cc";
1139 }
Jeff King4f3d3702007-12-17 15:51:34 -05001140 make_message_id() unless defined($message_id);
Junio C Hamanoaeb59322007-06-20 13:47:34 -07001141
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001142 my $header = "From: $sender
Junio C Hamanof06a6a42007-04-16 16:51:47 -07001143To: $to${ccline}
Eric Wong4bc87a22006-03-25 17:20:48 -08001144Subject: $subject
Eric Wong4bc87a22006-03-25 17:20:48 -08001145Date: $date
1146Message-Id: $message_id
Martin Langhoffe923eff2006-05-03 09:44:36 +12001147X-Mailer: git-send-email $gitversion
Eric Wong4bc87a22006-03-25 17:20:48 -08001148";
Thomas Rast3e0c4ff2009-03-01 23:45:41 +01001149 if ($reply_to) {
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001150
1151 $header .= "In-Reply-To: $reply_to\n";
1152 $header .= "References: $references\n";
1153 }
Junio C Hamanoce91c2f2006-10-05 16:36:49 -07001154 if (@xh) {
1155 $header .= join("\n", @xh) . "\n";
1156 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001157
Robin H. Johnsonc38f0242007-04-25 19:37:20 -07001158 my @sendmail_parameters = ('-i', @recipients);
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001159 my $raw_from = $sender;
Felipe Contrerasc89e3242009-11-26 21:04:29 +02001160 if (defined $envelope_sender && $envelope_sender ne "auto") {
1161 $raw_from = $envelope_sender;
1162 }
Robin H. Johnsonf073a592007-04-25 19:37:22 -07001163 $raw_from = extract_valid_address($raw_from);
1164 unshift (@sendmail_parameters,
1165 '-f', $raw_from) if(defined $envelope_sender);
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001166
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001167 if ($needs_confirm && !$dry_run) {
1168 print "\n$header\n";
1169 if ($needs_confirm eq "inform") {
1170 $confirm_unconfigured = 0; # squelch this message for the rest of this run
Jay Soffian6e182512009-03-28 21:39:10 -04001171 $ask_default = "y"; # assume yes on EOF since user hasn't explicitly asked for confirmation
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001172 print " The Cc list above has been expanded by additional\n";
1173 print " addresses found in the patch commit message. By default\n";
1174 print " send-email prompts before sending whenever this occurs.\n";
1175 print " This behavior is controlled by the sendemail.confirm\n";
1176 print " configuration setting.\n";
1177 print "\n";
1178 print " For additional information, run 'git send-email --help'.\n";
1179 print " To retain the current behavior, but squelch this message,\n";
1180 print " run 'git config --global sendemail.confirm auto'.\n\n";
1181 }
Jay Soffian6e182512009-03-28 21:39:10 -04001182 $_ = ask("Send this email? ([y]es|[n]o|[q]uit|[a]ll): ",
1183 valid_re => qr/^(?:yes|y|no|n|quit|q|all|a)/i,
1184 default => $ask_default);
1185 die "Send this email reply required" unless defined $_;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001186 if (/^n/i) {
Michael Witten15da1082009-04-13 13:23:51 -05001187 return 0;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001188 } elsif (/^q/i) {
1189 cleanup_compose_files();
1190 exit(0);
1191 } elsif (/^a/i) {
1192 $confirm = 'never';
1193 }
1194 }
1195
Pascal Obry052fbea2010-09-06 20:12:11 +02001196 unshift (@sendmail_parameters, @smtp_server_options);
1197
Matthew Wilcox61302592006-10-10 08:58:23 -06001198 if ($dry_run) {
1199 # We don't want to send the email.
1200 } elsif ($smtp_server =~ m#^/#) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001201 my $pid = open my $sm, '|-';
1202 defined $pid or die $!;
1203 if (!$pid) {
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001204 exec($smtp_server, @sendmail_parameters) or die $!;
Eric Wongaca7ad72006-05-15 02:34:44 -07001205 }
1206 print $sm "$header\n$message";
Ævar Arnfjörð Bjarmason5e2c2ab2010-09-30 13:43:07 +00001207 close $sm or die $!;
Eric Wongaca7ad72006-05-15 02:34:44 -07001208 } else {
Junio C Hamano44b24762007-09-25 17:27:54 -07001209
1210 if (!defined $smtp_server) {
1211 die "The required SMTP server is not properly defined."
1212 }
1213
Thomas Rastf6bebd12008-06-25 21:42:43 +02001214 if ($smtp_encryption eq 'ssl') {
Junio C Hamano44b24762007-09-25 17:27:54 -07001215 $smtp_server_port ||= 465; # ssmtp
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001216 require Net::SMTP::SSL;
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001217 $smtp_domain ||= maildomain();
Thomas Rast5508f3e2013-12-01 23:48:43 +01001218 require IO::Socket::SSL;
1219 # Net::SMTP::SSL->new() does not forward any SSL options
1220 IO::Socket::SSL::set_client_defaults(
1221 ssl_verify_params());
Jari Aalto134550f2010-03-14 17:16:45 +02001222 $smtp ||= Net::SMTP::SSL->new($smtp_server,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001223 Hello => $smtp_domain,
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001224 Port => $smtp_server_port,
Thomas Rast5508f3e2013-12-01 23:48:43 +01001225 Debug => $debug_net_smtp);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001226 }
1227 else {
1228 require Net::SMTP;
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001229 $smtp_domain ||= maildomain();
brian m. carlson1a741bf2013-07-04 22:04:52 +00001230 $smtp_server_port ||= 25;
1231 $smtp ||= Net::SMTP->new($smtp_server,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001232 Hello => $smtp_domain,
brian m. carlson1a741bf2013-07-04 22:04:52 +00001233 Debug => $debug_net_smtp,
1234 Port => $smtp_server_port);
Yakov Lernerfb3650e2009-09-25 15:10:21 -07001235 if ($smtp_encryption eq 'tls' && $smtp) {
Thomas Rastf6bebd12008-06-25 21:42:43 +02001236 require Net::SMTP::SSL;
1237 $smtp->command('STARTTLS');
1238 $smtp->response();
1239 if ($smtp->code == 220) {
Ramkumar Ramachandra35035bb2013-07-18 09:53:11 -07001240 $smtp = Net::SMTP::SSL->start_SSL($smtp,
1241 ssl_verify_params())
Brian M. Carlson6cb0c882013-09-08 20:54:34 +00001242 or die "STARTTLS failed! ".IO::Socket::SSL::errstr();
Thomas Rast6cbf8b02008-07-03 00:11:31 +02001243 $smtp_encryption = '';
Robert Shearman9d1ccf52008-07-09 22:39:40 +01001244 # Send EHLO again to receive fresh
1245 # supported commands
Matthew Daley155b9402011-10-15 04:44:52 -04001246 $smtp->hello($smtp_domain);
Thomas Rastf6bebd12008-06-25 21:42:43 +02001247 } else {
1248 die "Server does not support STARTTLS! ".$smtp->message;
1249 }
1250 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001251 }
Junio C Hamano44b24762007-09-25 17:27:54 -07001252
1253 if (!$smtp) {
Jari Aaltof60812e2010-03-14 17:16:09 +02001254 die "Unable to initialize SMTP properly. Check config and use --smtp-debug. ",
Jari Aaltoe5afb3a2010-03-14 17:15:33 +02001255 "VALUES: server=$smtp_server ",
1256 "encryption=$smtp_encryption ",
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001257 "hello=$smtp_domain",
Sylvain Rabota1dd7e12011-04-29 20:23:24 +02001258 defined $smtp_server_port ? " port=$smtp_server_port" : "";
Junio C Hamano44b24762007-09-25 17:27:54 -07001259 }
1260
Michal Nazarewicz4d31a442013-02-12 15:02:33 +01001261 smtp_auth_maybe or die $smtp->message;
Michael Witten2363d742008-02-03 19:53:56 -05001262
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001263 $smtp->mail( $raw_from ) or die $smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001264 $smtp->to( @recipients ) or die $smtp->message;
1265 $smtp->data or die $smtp->message;
1266 $smtp->datasend("$header\n$message") or die $smtp->message;
1267 $smtp->dataend() or die $smtp->message;
Michael Witten15da1082009-04-13 13:23:51 -05001268 $smtp->code =~ /250|200/ or die "Failed to send $subject\n".$smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001269 }
Ryan Anderson27184352006-02-05 20:13:52 -05001270 if ($quiet) {
Robin H. Johnson71c7da92007-04-25 19:37:16 -07001271 printf (($dry_run ? "Dry-" : "")."Sent %s\n", $subject);
Ryan Anderson27184352006-02-05 20:13:52 -05001272 } else {
David D. Kilzerb7f30e02007-11-18 20:14:55 -08001273 print (($dry_run ? "Dry-" : "")."OK. Log says:\n");
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001274 if ($smtp_server !~ m#^/#) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001275 print "Server: $smtp_server\n";
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001276 print "MAIL FROM:<$raw_from>\n";
Joe Perches02461e02009-10-08 10:03:26 -07001277 foreach my $entry (@recipients) {
1278 print "RCPT TO:<$entry>\n";
1279 }
Eric Wongaca7ad72006-05-15 02:34:44 -07001280 } else {
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001281 print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001282 }
David D. Kilzerb7f30e02007-11-18 20:14:55 -08001283 print $header, "\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001284 if ($smtp) {
1285 print "Result: ", $smtp->code, ' ',
1286 ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
1287 } else {
1288 print "Result: OK\n";
1289 }
Ryan Anderson30d08b32006-02-02 11:56:06 -05001290 }
Michael Witten15da1082009-04-13 13:23:51 -05001291
1292 return 1;
Ryan Anderson83b24432005-07-31 04:17:25 -04001293}
1294
Ryan Anderson83b24432005-07-31 04:17:25 -04001295$reply_to = $initial_reply_to;
Junio C Hamano2186d562006-05-29 23:53:13 -07001296$references = $initial_reply_to || '';
Ryan Anderson83b24432005-07-31 04:17:25 -04001297$subject = $initial_subject;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001298$message_num = 0;
Ryan Anderson83b24432005-07-31 04:17:25 -04001299
1300foreach my $t (@files) {
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001301 open my $fh, "<", $t or die "can't open file $t";
Ryan Anderson83b24432005-07-31 04:17:25 -04001302
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001303 my $author = undef;
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001304 my $sauthor = undef;
Jeff King8291db62007-11-16 05:49:09 -05001305 my $author_encoding;
1306 my $has_content_type;
1307 my $body_encoding;
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001308 @to = ();
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001309 @cc = ();
Junio C Hamanoce91c2f2006-10-05 16:36:49 -07001310 @xh = ();
Junio C Hamanoe6b09642006-10-07 03:09:05 -07001311 my $input_format = undef;
Jay Soffian50126992009-02-14 23:32:14 -05001312 my @header = ();
Ryan Anderson83b24432005-07-31 04:17:25 -04001313 $message = "";
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001314 $message_num++;
Jay Soffian50126992009-02-14 23:32:14 -05001315 # First unfold multiline header fields
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001316 while(<$fh>) {
Jay Soffian50126992009-02-14 23:32:14 -05001317 last if /^\s*$/;
1318 if (/^\s+\S/ and @header) {
1319 chomp($header[$#header]);
1320 s/^\s+/ /;
1321 $header[$#header] .= $_;
1322 } else {
1323 push(@header, $_);
1324 }
1325 }
1326 # Now parse the header
1327 foreach(@header) {
1328 if (/^From /) {
1329 $input_format = 'mbox';
1330 next;
1331 }
1332 chomp;
1333 if (!defined $input_format && /^[-A-Za-z]+:\s/) {
1334 $input_format = 'mbox';
1335 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001336
Jay Soffian50126992009-02-14 23:32:14 -05001337 if (defined $input_format && $input_format eq 'mbox') {
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001338 if (/^Subject:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001339 $subject = $1;
1340 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001341 elsif (/^From:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001342 ($author, $author_encoding) = unquote_rfc2047($1);
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001343 $sauthor = sanitize_address($author);
Jay Soffian50126992009-02-14 23:32:14 -05001344 next if $suppress_cc{'author'};
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001345 next if $suppress_cc{'self'} and $sauthor eq $sender;
Jay Soffian50126992009-02-14 23:32:14 -05001346 printf("(mbox) Adding cc: %s from line '%s'\n",
1347 $1, $_) unless $quiet;
1348 push @cc, $1;
1349 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001350 elsif (/^To:\s+(.*)$/i) {
Stephen Boyd21802cd2010-09-29 00:26:44 -07001351 foreach my $addr (parse_address_line($1)) {
1352 printf("(mbox) Adding to: %s from line '%s'\n",
1353 $addr, $_) unless $quiet;
Krzysztof Mazure4312252012-11-22 19:12:10 +01001354 push @to, $addr;
Stephen Boyd21802cd2010-09-29 00:26:44 -07001355 }
1356 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001357 elsif (/^Cc:\s+(.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001358 foreach my $addr (parse_address_line($1)) {
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001359 my $qaddr = unquote_rfc2047($addr);
1360 my $saddr = sanitize_address($qaddr);
1361 if ($saddr eq $sender) {
David Brown65648282007-12-25 19:56:29 -08001362 next if ($suppress_cc{'self'});
David Brown65648282007-12-25 19:56:29 -08001363 } else {
1364 next if ($suppress_cc{'cc'});
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001365 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001366 printf("(mbox) Adding cc: %s from line '%s'\n",
Jay Soffian50126992009-02-14 23:32:14 -05001367 $addr, $_) unless $quiet;
1368 push @cc, $addr;
Ryan Anderson83b24432005-07-31 04:17:25 -04001369 }
1370 }
Jay Soffian50126992009-02-14 23:32:14 -05001371 elsif (/^Content-type:/i) {
1372 $has_content_type = 1;
1373 if (/charset="?([^ "]+)/) {
1374 $body_encoding = $1;
1375 }
1376 push @xh, $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001377 }
Jay Soffian50126992009-02-14 23:32:14 -05001378 elsif (/^Message-Id: (.*)/i) {
1379 $message_id = $1;
1380 }
Nickolai Zeldovich63100712013-01-06 20:34:58 -05001381 elsif (!/^Date:\s/i && /^[-A-Za-z]+:\s+\S/) {
Jay Soffian50126992009-02-14 23:32:14 -05001382 push @xh, $_;
1383 }
1384
Ryan Anderson83b24432005-07-31 04:17:25 -04001385 } else {
Jay Soffian50126992009-02-14 23:32:14 -05001386 # In the traditional
1387 # "send lots of email" format,
1388 # line 1 = cc
1389 # line 2 = subject
1390 # So let's support that, too.
1391 $input_format = 'lots';
1392 if (@cc == 0 && !$suppress_cc{'cc'}) {
1393 printf("(non-mbox) Adding cc: %s from line '%s'\n",
1394 $_, $_) unless $quiet;
1395 push @cc, $_;
1396 } elsif (!defined $subject) {
1397 $subject = $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001398 }
1399 }
1400 }
Jay Soffian50126992009-02-14 23:32:14 -05001401 # Now parse the message body
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001402 while(<$fh>) {
Jay Soffian50126992009-02-14 23:32:14 -05001403 $message .= $_;
1404 if (/^(Signed-off-by|Cc): (.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001405 chomp;
Jay Soffian3531e272009-02-14 23:32:15 -05001406 my ($what, $c) = ($1, $2);
Jay Soffian50126992009-02-14 23:32:14 -05001407 chomp $c;
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001408 my $sc = sanitize_address($c);
1409 if ($sc eq $sender) {
Jay Soffian3531e272009-02-14 23:32:15 -05001410 next if ($suppress_cc{'self'});
1411 } else {
1412 next if $suppress_cc{'sob'} and $what =~ /Signed-off-by/i;
1413 next if $suppress_cc{'bodycc'} and $what =~ /Cc/i;
1414 }
Jay Soffian50126992009-02-14 23:32:14 -05001415 push @cc, $c;
Jay Soffian3531e272009-02-14 23:32:15 -05001416 printf("(body) Adding cc: %s from line '%s'\n",
Jay Soffian50126992009-02-14 23:32:14 -05001417 $c, $_) unless $quiet;
1418 }
1419 }
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001420 close $fh;
Joe Perches324a8bd2007-08-17 18:51:12 -07001421
Joe Perches6e74e072010-09-24 10:03:00 -07001422 push @to, recipients_cmd("to-cmd", "to", $to_cmd, $t)
1423 if defined $to_cmd;
1424 push @cc, recipients_cmd("cc-cmd", "cc", $cc_cmd, $t)
1425 if defined $cc_cmd && !$suppress_cc{'cccmd'};
Joe Perches324a8bd2007-08-17 18:51:12 -07001426
Thomas Rast3cae7e52010-06-17 22:10:39 +02001427 if ($broken_encoding{$t} && !$has_content_type) {
1428 $has_content_type = 1;
1429 push @xh, "MIME-Version: 1.0",
1430 "Content-Type: text/plain; charset=$auto_8bit_encoding",
1431 "Content-Transfer-Encoding: 8bit";
1432 $body_encoding = $auto_8bit_encoding;
1433 }
1434
Krzysztof Mazurce547802012-10-24 23:08:26 +02001435 if ($broken_encoding{$t} && !is_rfc2047_quoted($subject)) {
1436 $subject = quote_subject($subject, $auto_8bit_encoding);
Thomas Rast3cae7e52010-06-17 22:10:39 +02001437 }
1438
Michael S. Tsirkin4cb46bd2013-06-18 15:49:26 +03001439 if (defined $sauthor and $sauthor ne $sender) {
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001440 $message = "From: $author\n\n$message";
Jeff King8291db62007-11-16 05:49:09 -05001441 if (defined $author_encoding) {
1442 if ($has_content_type) {
1443 if ($body_encoding eq $author_encoding) {
1444 # ok, we already have the right encoding
1445 }
1446 else {
1447 # uh oh, we should re-encode
1448 }
1449 }
1450 else {
Thomas Rast3cae7e52010-06-17 22:10:39 +02001451 $has_content_type = 1;
Jeff King8291db62007-11-16 05:49:09 -05001452 push @xh,
1453 'MIME-Version: 1.0',
Jeff King8641ee32007-11-20 07:54:04 -05001454 "Content-Type: text/plain; charset=$author_encoding",
1455 'Content-Transfer-Encoding: 8bit';
Jeff King8291db62007-11-16 05:49:09 -05001456 }
1457 }
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001458 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001459
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001460 $needs_confirm = (
1461 $confirm eq "always" or
1462 ($confirm =~ /^(?:auto|cc)$/ && @cc) or
1463 ($confirm =~ /^(?:auto|compose)$/ && $compose && $message_num == 1));
1464 $needs_confirm = "inform" if ($needs_confirm && $confirm_unconfigured && @cc);
1465
Krzysztof Mazure4312252012-11-22 19:12:10 +01001466 @to = validate_address_list(sanitize_address_list(@to));
1467 @cc = validate_address_list(sanitize_address_list(@cc));
1468
Stephen Boyd3c3bb512010-10-04 00:05:24 -07001469 @to = (@initial_to, @to);
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001470 @cc = (@initial_cc, @cc);
1471
Michael Witten15da1082009-04-13 13:23:51 -05001472 my $message_was_sent = send_message();
Ryan Anderson83b24432005-07-31 04:17:25 -04001473
1474 # set up for the next message
Junio C Hamano95a877a2009-06-12 09:23:43 -07001475 if ($thread && $message_was_sent &&
Felipe Contrerasb99d22f2013-05-24 22:44:52 -05001476 ($chain_reply_to || !defined $reply_to || length($reply_to) == 0 ||
Antonio Ospitedb54c8e2010-11-12 15:55:08 +01001477 $message_num == 1)) {
Ryan Anderson78488b22005-07-31 20:04:24 -04001478 $reply_to = $message_id;
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001479 if (length $references > 0) {
YOSHIFUJI Hideaki / 吉藤英明a925b892007-04-06 08:50:24 +09001480 $references .= "\n $message_id";
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001481 } else {
1482 $references = "$message_id";
1483 }
Ryan Anderson78488b22005-07-31 20:04:24 -04001484 }
Jeff King4f3d3702007-12-17 15:51:34 -05001485 $message_id = undef;
Ryan Anderson83b24432005-07-31 04:17:25 -04001486}
Ryan Andersone2057352005-08-02 21:45:22 -04001487
Joe Perches6e74e072010-09-24 10:03:00 -07001488# Execute a command (e.g. $to_cmd) to get a list of email addresses
1489# and return a results array
1490sub recipients_cmd {
1491 my ($prefix, $what, $cmd, $file) = @_;
1492
Joe Perches6e74e072010-09-24 10:03:00 -07001493 my @addresses = ();
Ramkumar Ramachandraa47eab02013-03-31 18:40:42 -07001494 open my $fh, "-|", "$cmd \Q$file\E"
Joe Perches6e74e072010-09-24 10:03:00 -07001495 or die "($prefix) Could not execute '$cmd'";
Junio C Hamano7ebee442010-10-26 22:02:52 -07001496 while (my $address = <$fh>) {
Joe Perches6e74e072010-09-24 10:03:00 -07001497 $address =~ s/^\s*//g;
1498 $address =~ s/\s*$//g;
1499 $address = sanitize_address($address);
Michael S. Tsirkinda187592013-06-05 21:11:00 +03001500 next if ($address eq $sender and $suppress_cc{'self'});
Joe Perches6e74e072010-09-24 10:03:00 -07001501 push @addresses, $address;
1502 printf("($prefix) Adding %s: %s from: '%s'\n",
1503 $what, $address, $cmd) unless $quiet;
1504 }
Junio C Hamano7ebee442010-10-26 22:02:52 -07001505 close $fh
Joe Perches6e74e072010-09-24 10:03:00 -07001506 or die "($prefix) failed to close pipe to '$cmd'";
1507 return @addresses;
1508}
1509
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001510cleanup_compose_files();
Ryan Anderson1f038a02005-09-05 01:13:07 -04001511
Ævar Arnfjörð Bjarmason4bf597e2010-09-30 13:43:00 +00001512sub cleanup_compose_files {
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001513 unlink($compose_filename, $compose_filename . ".final") if $compose;
Ryan Anderson1f038a02005-09-05 01:13:07 -04001514}
1515
Eric Wong4bc87a22006-03-25 17:20:48 -08001516$smtp->quit if $smtp;
Ryan Andersone2057352005-08-02 21:45:22 -04001517
Ævar Arnfjörð Bjarmasonc438ea22010-09-30 13:42:59 +00001518sub unique_email_list {
Ryan Andersone2057352005-08-02 21:45:22 -04001519 my %seen;
1520 my @emails;
1521
1522 foreach my $entry (@_) {
Krzysztof Mazure4312252012-11-22 19:12:10 +01001523 my $clean = extract_valid_address_or_die($entry);
1524 $seen{$clean} ||= 0;
1525 next if $seen{$clean}++;
1526 push @emails, $entry;
Ryan Andersone2057352005-08-02 21:45:22 -04001527 }
1528 return @emails;
1529}
Jeff King747bbff2008-01-18 09:19:48 -05001530
1531sub validate_patch {
1532 my $fn = shift;
1533 open(my $fh, '<', $fn)
1534 or die "unable to open $fn: $!\n";
1535 while (my $line = <$fh>) {
1536 if (length($line) > 998) {
1537 return "$.: patch contains a line longer than 998 characters";
1538 }
1539 }
Ramkumar Ramachandra622bc932013-03-31 18:40:40 -07001540 return;
Jeff King747bbff2008-01-18 09:19:48 -05001541}
Jeff King0706bd12008-03-28 17:28:33 -04001542
1543sub file_has_nonascii {
1544 my $fn = shift;
1545 open(my $fh, '<', $fn)
1546 or die "unable to open $fn: $!\n";
1547 while (my $line = <$fh>) {
1548 return 1 if $line =~ /[^[:ascii:]]/;
1549 }
1550 return 0;
1551}
Thomas Rast3cae7e52010-06-17 22:10:39 +02001552
1553sub body_or_subject_has_nonascii {
1554 my $fn = shift;
1555 open(my $fh, '<', $fn)
1556 or die "unable to open $fn: $!\n";
1557 while (my $line = <$fh>) {
1558 last if $line =~ /^$/;
1559 return 1 if $line =~ /^Subject.*[^[:ascii:]]/;
1560 }
1561 while (my $line = <$fh>) {
1562 return 1 if $line =~ /[^[:ascii:]]/;
1563 }
1564 return 0;
1565}