blob: 036e4b4259babdda1e76a5aa558c3600b43a89c0 [file] [log] [blame]
Ryan Anderson83b24432005-07-31 04:17:25 -04001#!/usr/bin/perl -w
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
19use strict;
20use warnings;
21use Term::ReadLine;
Ryan Anderson83b24432005-07-31 04:17:25 -040022use Getopt::Long;
Wu Fengguang0e73b3e2008-12-19 16:10:10 +080023use Text::ParseWords;
Ryan Anderson83b24432005-07-31 04:17:25 -040024use Data::Dumper;
Sean Estabrooks412876d2007-08-17 17:38:25 -040025use Term::ANSIColor;
Jay Soffianeed6ca72009-02-14 23:32:13 -050026use File::Temp qw/ tempdir tempfile /;
Ævar Arnfjörð Bjarmason89bf1ba2010-09-14 19:02:24 +000027use File::Spec::Functions qw(catfile);
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +010028use Error qw(:try);
Petr Baudis3cb8caf2006-07-03 22:47:58 +020029use Git;
Ryan Anderson83b24432005-07-31 04:17:25 -040030
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +010031Getopt::Long::Configure qw/ pass_through /;
32
Junio C Hamano280242d2006-07-02 16:03:59 -070033package FakeTerm;
34sub new {
35 my ($class, $reason) = @_;
36 return bless \$reason, shift;
37}
38sub readline {
39 my $self = shift;
40 die "Cannot use readline on FakeTerm: $$self";
41}
42package main;
43
Michael Coleman1b0baf12007-02-27 22:47:54 -060044
45sub usage {
46 print <<EOT;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +010047git send-email [options] <file | directory | rev-list options >
Michael Witten4ed62b02008-09-30 07:58:30 -050048
49 Composing:
50 --from <str> * Email From:
Stephen Boydf434c082010-03-07 14:46:48 -080051 --[no-]to <str> * Email To:
52 --[no-]cc <str> * Email Cc:
53 --[no-]bcc <str> * Email Bcc:
Michael Witten4ed62b02008-09-30 07:58:30 -050054 --subject <str> * Email "Subject:"
55 --in-reply-to <str> * Email "In-Reply-To:"
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +010056 --annotate * Review each patch that will be sent in an editor.
Michael Witten4ed62b02008-09-30 07:58:30 -050057 --compose * Open an editor for introduction.
Thomas Rast3cae7e52010-06-17 22:10:39 +020058 --8bit-encoding <str> * Encoding to assume 8bit mails if undeclared
Michael Witten4ed62b02008-09-30 07:58:30 -050059
60 Sending:
61 --envelope-sender <str> * Email envelope sender.
62 --smtp-server <str:int> * Outgoing SMTP server to use. The port
63 is optional. Default 'localhost'.
64 --smtp-server-port <int> * Outgoing SMTP server port.
65 --smtp-user <str> * Username for SMTP-AUTH.
66 --smtp-pass <str> * Password for SMTP-AUTH; not necessary.
67 --smtp-encryption <str> * tls or ssl; anything else disables.
68 --smtp-ssl * Deprecated. Use '--smtp-encryption ssl'.
Jari Aalto134550f2010-03-14 17:16:45 +020069 --smtp-domain <str> * The domain name sent to HELO/EHLO handshake
Jari Aaltof60812e2010-03-14 17:16:09 +020070 --smtp-debug <0|1> * Disable, enable Net::SMTP debug.
Michael Witten4ed62b02008-09-30 07:58:30 -050071
72 Automating:
73 --identity <str> * Use the sendemail.<id> options.
74 --cc-cmd <str> * Email Cc: via `<str> \$patch_path`
Jay Soffian3531e272009-02-14 23:32:15 -050075 --suppress-cc <str> * author, self, sob, cc, cccmd, body, bodycc, all.
76 --[no-]signed-off-by-cc * Send to Signed-off-by: addresses. Default on.
Michael Witten4ed62b02008-09-30 07:58:30 -050077 --[no-]suppress-from * Send to self. Default off.
Junio C Hamano41fe87f2009-08-22 12:48:48 -070078 --[no-]chain-reply-to * Chain In-Reply-To: fields. Default off.
Michael Witten4ed62b02008-09-30 07:58:30 -050079 --[no-]thread * Use In-Reply-To: field. Default on.
80
81 Administering:
Jay Soffianc1f2aa42009-03-02 23:52:18 -050082 --confirm <str> * Confirm recipients before sending;
83 auto, cc, compose, always, or never.
Michael Witten4ed62b02008-09-30 07:58:30 -050084 --quiet * Output one line of info per email.
85 --dry-run * Don't actually send the emails.
86 --[no-]validate * Perform patch sanity checks. Default on.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +010087 --[no-]format-patch * understand any non optional arguments as
88 `git format-patch` ones.
Thomas Rasta03bc5b2009-06-08 23:34:12 +020089 --force * Send even if safety checks would prevent it.
Jeff Kingc764a0c2008-01-18 09:20:10 -050090
Michael Coleman1b0baf12007-02-27 22:47:54 -060091EOT
92 exit(1);
93}
94
Eric Wong4bc87a22006-03-25 17:20:48 -080095# most mail servers generate the Date: header, but not all...
Jakub Narebski6bdca892006-07-07 20:57:55 +020096sub format_2822_time {
97 my ($time) = @_;
98 my @localtm = localtime($time);
99 my @gmttm = gmtime($time);
100 my $localmin = $localtm[1] + $localtm[2] * 60;
101 my $gmtmin = $gmttm[1] + $gmttm[2] * 60;
102 if ($localtm[0] != $gmttm[0]) {
103 die "local zone differs from GMT by a non-minute interval\n";
104 }
105 if ((($gmttm[6] + 1) % 7) == $localtm[6]) {
106 $localmin += 1440;
107 } elsif ((($gmttm[6] - 1) % 7) == $localtm[6]) {
108 $localmin -= 1440;
109 } elsif ($gmttm[6] != $localtm[6]) {
110 die "local time offset greater than or equal to 24 hours\n";
111 }
112 my $offset = $localmin - $gmtmin;
113 my $offhour = $offset / 60;
114 my $offmin = abs($offset % 60);
115 if (abs($offhour) >= 24) {
116 die ("local time offset greater than or equal to 24 hours\n");
117 }
118
119 return sprintf("%s, %2d %s %d %02d:%02d:%02d %s%02d%02d",
120 qw(Sun Mon Tue Wed Thu Fri Sat)[$localtm[6]],
121 $localtm[3],
122 qw(Jan Feb Mar Apr May Jun
123 Jul Aug Sep Oct Nov Dec)[$localtm[4]],
124 $localtm[5]+1900,
125 $localtm[2],
126 $localtm[1],
127 $localtm[0],
128 ($offset >= 0) ? '+' : '-',
129 abs($offhour),
130 $offmin,
131 );
132}
Eric Wong4bc87a22006-03-25 17:20:48 -0800133
Eric Wong567ffeb2006-03-25 16:47:12 -0800134my $have_email_valid = eval { require Email::Valid; 1 };
Jay Soffian50126992009-02-14 23:32:14 -0500135my $have_mail_address = eval { require Mail::Address; 1 };
Eric Wong4bc87a22006-03-25 17:20:48 -0800136my $smtp;
Wincent Colaiuta5f5b6112007-11-21 13:35:05 +0100137my $auth;
Eric Wong4bc87a22006-03-25 17:20:48 -0800138
Ryan Anderson83b24432005-07-31 04:17:25 -0400139# Variables we fill in automatically, or via prompting:
Stephen Boydf434c082010-03-07 14:46:48 -0800140my (@to,$no_to,@cc,$no_cc,@initial_cc,@bcclist,$no_bcc,@xh,
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100141 $initial_reply_to,$initial_subject,@files,
142 $author,$sender,$smtp_authpass,$annotate,$compose,$time);
Ryan Anderson83b24432005-07-31 04:17:25 -0400143
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700144my $envelope_sender;
Ryan Anderson78488b22005-07-31 20:04:24 -0400145
Ryan Anderson91332612005-07-31 20:04:24 -0400146# Example reply to:
Ryan Anderson83b24432005-07-31 04:17:25 -0400147#$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
Ryan Anderson83b24432005-07-31 04:17:25 -0400148
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100149my $repo = eval { Git->repository() };
150my @repo = $repo ? ($repo) : ();
Junio C Hamano280242d2006-07-02 16:03:59 -0700151my $term = eval {
Jay Soffian0fb7fc72008-02-21 19:16:04 -0500152 $ENV{"GIT_SEND_EMAIL_NOTTY"}
153 ? new Term::ReadLine 'git-send-email', \*STDIN, \*STDOUT
154 : new Term::ReadLine 'git-send-email';
Junio C Hamano280242d2006-07-02 16:03:59 -0700155};
156if ($@) {
157 $term = new FakeTerm "$@: going non-interactive";
158}
Ryan Anderson83b24432005-07-31 04:17:25 -0400159
Adam Roben5483c712007-06-27 20:59:37 -0700160# Behavior modification variables
161my ($quiet, $dry_run) = (0, 0);
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100162my $format_patch;
Jay Soffianafe756c2009-02-23 13:51:37 -0500163my $compose_filename;
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200164my $force = 0;
Adam Roben5483c712007-06-27 20:59:37 -0700165
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100166# Handle interactive edition of files.
167my $multiedit;
Michael J Gruber0ce142c2010-03-22 17:12:53 +0100168my $editor;
Jonathan Niederb4479f02009-10-30 20:42:34 -0500169
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100170sub do_edit {
Michael J Gruber0ce142c2010-03-22 17:12:53 +0100171 if (!defined($editor)) {
172 $editor = Git::command_oneline('var', 'GIT_EDITOR');
173 }
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100174 if (defined($multiedit) && !$multiedit) {
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100175 map {
176 system('sh', '-c', $editor.' "$@"', $editor, $_);
177 if (($? & 127) || ($? >> 8)) {
178 die("the editor exited uncleanly, aborting everything");
179 }
180 } @_;
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100181 } else {
182 system('sh', '-c', $editor.' "$@"', $editor, @_);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100183 if (($? & 127) || ($? >> 8)) {
184 die("the editor exited uncleanly, aborting everything");
185 }
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100186 }
187}
Adam Roben5483c712007-06-27 20:59:37 -0700188
189# Variables with corresponding config settings
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500190my ($thread, $chain_reply_to, $suppress_from, $signed_off_by_cc, $cc_cmd);
Thomas Rastf6bebd12008-06-25 21:42:43 +0200191my ($smtp_server, $smtp_server_port, $smtp_authuser, $smtp_encryption);
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -0400192my ($identity, $aliasfiletype, @alias_files, @smtp_host_parts, $smtp_domain);
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500193my ($validate, $confirm);
David Brown65648282007-12-25 19:56:29 -0800194my (@suppress_cc);
Thomas Rast3cae7e52010-06-17 22:10:39 +0200195my ($auto_8bit_encoding);
Adam Roben5483c712007-06-27 20:59:37 -0700196
Jari Aaltof60812e2010-03-14 17:16:09 +0200197my ($debug_net_smtp) = 0; # Net::SMTP, see send_message()
198
Nanako Shiraishi528fb082009-11-29 12:24:48 +0900199my $not_set_by_user = "true but not set by the user";
200
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900201my %config_bool_settings = (
Adam Roben5483c712007-06-27 20:59:37 -0700202 "thread" => [\$thread, 1],
Nanako Shiraishi528fb082009-11-29 12:24:48 +0900203 "chainreplyto" => [\$chain_reply_to, $not_set_by_user],
David Brown65648282007-12-25 19:56:29 -0800204 "suppressfrom" => [\$suppress_from, undef],
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500205 "signedoffbycc" => [\$signed_off_by_cc, undef],
206 "signedoffcc" => [\$signed_off_by_cc, undef], # Deprecated
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500207 "validate" => [\$validate, 1],
Adam Robene46f7a02007-06-26 15:48:30 -0700208);
209
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900210my %config_settings = (
211 "smtpserver" => \$smtp_server,
Junio C Hamano44b24762007-09-25 17:27:54 -0700212 "smtpserverport" => \$smtp_server_port,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900213 "smtpuser" => \$smtp_authuser,
214 "smtppass" => \$smtp_authpass,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -0400215 "smtpdomain" => \$smtp_domain,
Miklos Vajna2db9b492007-10-01 14:42:42 +0200216 "to" => \@to,
Miklos Vajna5f8b9fc2008-04-27 14:14:58 +0200217 "cc" => \@initial_cc,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900218 "cccmd" => \$cc_cmd,
219 "aliasfiletype" => \$aliasfiletype,
220 "bcc" => \@bcclist,
221 "aliasesfile" => \@alias_files,
David Brown65648282007-12-25 19:56:29 -0800222 "suppresscc" => \@suppress_cc,
Ask Bjørn Hansen9f7820a2008-06-07 00:33:42 -0700223 "envelopesender" => \$envelope_sender,
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100224 "multiedit" => \$multiedit,
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500225 "confirm" => \$confirm,
Trent Piepho09caa242009-05-12 15:48:56 -0700226 "from" => \$sender,
Thomas Rast3cae7e52010-06-17 22:10:39 +0200227 "assume8bitencoding" => \$auto_8bit_encoding,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900228);
Avi Kivity4a62d3f2007-03-11 19:19:44 +0200229
Nanako Shiraishi528fb082009-11-29 12:24:48 +0900230# Help users prepare for 1.7.0
231sub chain_reply_to {
232 if (defined $chain_reply_to &&
233 $chain_reply_to eq $not_set_by_user) {
234 print STDERR
Junio C Hamanoa19f1012009-12-26 14:03:17 -0800235 "In git 1.7.0, the default has changed to --no-chain-reply-to\n" .
Nanako Shiraishi528fb082009-11-29 12:24:48 +0900236 "Set sendemail.chainreplyto configuration variable to true if\n" .
237 "you want to keep --chain-reply-to as your default.\n";
Junio C Hamanoa19f1012009-12-26 14:03:17 -0800238 $chain_reply_to = 0;
Nanako Shiraishi528fb082009-11-29 12:24:48 +0900239 }
240 return $chain_reply_to;
241}
242
Michael Witten87429972008-02-03 19:53:57 -0500243# Handle Uncouth Termination
244sub signal_handler {
245
246 # Make text normal
247 print color("reset"), "\n";
248
249 # SMTP password masked
250 system "stty echo";
251
252 # tmp files from --compose
Jay Soffianafe756c2009-02-23 13:51:37 -0500253 if (defined $compose_filename) {
254 if (-e $compose_filename) {
255 print "'$compose_filename' contains an intermediate version of the email you were composing.\n";
256 }
257 if (-e ($compose_filename . ".final")) {
258 print "'$compose_filename.final' contains the composed email.\n"
259 }
Michael Witten87429972008-02-03 19:53:57 -0500260 }
261
262 exit;
263};
264
265$SIG{TERM} = \&signal_handler;
266$SIG{INT} = \&signal_handler;
267
Ryan Anderson83b24432005-07-31 04:17:25 -0400268# Begin by accumulating all the variables (defined above), that we will end up
269# needing, first, from the command line:
270
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200271my $rc = GetOptions("sender|from=s" => \$sender,
Ryan Anderson83b24432005-07-31 04:17:25 -0400272 "in-reply-to=s" => \$initial_reply_to,
273 "subject=s" => \$initial_subject,
274 "to=s" => \@to,
Stephen Boydf434c082010-03-07 14:46:48 -0800275 "no-to" => \$no_to,
Ryan Andersonda140f82006-02-13 03:05:15 -0500276 "cc=s" => \@initial_cc,
Stephen Boydf434c082010-03-07 14:46:48 -0800277 "no-cc" => \$no_cc,
Ryan Anderson58063242006-05-29 12:30:13 -0700278 "bcc=s" => \@bcclist,
Stephen Boydf434c082010-03-07 14:46:48 -0800279 "no-bcc" => \$no_bcc,
Ryan Anderson78488b22005-07-31 20:04:24 -0400280 "chain-reply-to!" => \$chain_reply_to,
Ryan Anderson3342d852005-07-31 20:04:24 -0400281 "smtp-server=s" => \$smtp_server,
Junio C Hamano44b24762007-09-25 17:27:54 -0700282 "smtp-server-port=s" => \$smtp_server_port,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900283 "smtp-user=s" => \$smtp_authuser,
Michael Witten2363d742008-02-03 19:53:56 -0500284 "smtp-pass:s" => \$smtp_authpass,
Thomas Rastf6bebd12008-06-25 21:42:43 +0200285 "smtp-ssl" => sub { $smtp_encryption = 'ssl' },
286 "smtp-encryption=s" => \$smtp_encryption,
Jari Aaltof60812e2010-03-14 17:16:09 +0200287 "smtp-debug:i" => \$debug_net_smtp,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -0400288 "smtp-domain:s" => \$smtp_domain,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900289 "identity=s" => \$identity,
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100290 "annotate" => \$annotate,
Ryan Anderson1f038a02005-09-05 01:13:07 -0400291 "compose" => \$compose,
Ryan Anderson30d08b32006-02-02 11:56:06 -0500292 "quiet" => \$quiet,
Joe Perches324a8bd2007-08-17 18:51:12 -0700293 "cc-cmd=s" => \$cc_cmd,
Adam Roben5483c712007-06-27 20:59:37 -0700294 "suppress-from!" => \$suppress_from,
David Brown65648282007-12-25 19:56:29 -0800295 "suppress-cc=s" => \@suppress_cc,
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500296 "signed-off-cc|signed-off-by-cc!" => \$signed_off_by_cc,
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500297 "confirm=s" => \$confirm,
Matthew Wilcox61302592006-10-10 08:58:23 -0600298 "dry-run" => \$dry_run,
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700299 "envelope-sender=s" => \$envelope_sender,
Adam Roben5483c712007-06-27 20:59:37 -0700300 "thread!" => \$thread,
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500301 "validate!" => \$validate,
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100302 "format-patch!" => \$format_patch,
Thomas Rast3cae7e52010-06-17 22:10:39 +0200303 "8bit-encoding=s" => \$auto_8bit_encoding,
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200304 "force" => \$force,
Ryan Anderson83b24432005-07-31 04:17:25 -0400305 );
306
Michael Coleman1b0baf12007-02-27 22:47:54 -0600307unless ($rc) {
308 usage();
309}
310
Jay Soffianeed6ca72009-02-14 23:32:13 -0500311die "Cannot run git format-patch from outside a repository\n"
312 if $format_patch and not $repo;
313
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900314# Now, let's fill any that aren't set in with defaults:
315
316sub read_config {
317 my ($prefix) = @_;
318
319 foreach my $setting (keys %config_bool_settings) {
320 my $target = $config_bool_settings{$setting}->[0];
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100321 $$target = Git::config_bool(@repo, "$prefix.$setting") unless (defined $$target);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900322 }
323
324 foreach my $setting (keys %config_settings) {
325 my $target = $config_settings{$setting};
Stephen Boydf434c082010-03-07 14:46:48 -0800326 next if $setting eq "to" and defined $no_to;
327 next if $setting eq "cc" and defined $no_cc;
328 next if $setting eq "bcc" and defined $no_bcc;
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900329 if (ref($target) eq "ARRAY") {
330 unless (@$target) {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100331 my @values = Git::config(@repo, "$prefix.$setting");
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900332 @$target = @values if (@values && defined $values[0]);
333 }
334 }
335 else {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100336 $$target = Git::config(@repo, "$prefix.$setting") unless (defined $$target);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900337 }
338 }
Thomas Rastf6bebd12008-06-25 21:42:43 +0200339
340 if (!defined $smtp_encryption) {
341 my $enc = Git::config(@repo, "$prefix.smtpencryption");
342 if (defined $enc) {
343 $smtp_encryption = $enc;
344 } elsif (Git::config_bool(@repo, "$prefix.smtpssl")) {
345 $smtp_encryption = 'ssl';
346 }
347 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900348}
349
350# read configuration from [sendemail "$identity"], fall back on [sendemail]
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100351$identity = Git::config(@repo, "sendemail.identity") unless (defined $identity);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900352read_config("sendemail.$identity") if (defined $identity);
353read_config("sendemail");
354
355# fall back on builtin bool defaults
356foreach my $setting (values %config_bool_settings) {
357 ${$setting->[0]} = $setting->[1] unless (defined (${$setting->[0]}));
358}
359
Thomas Rastfa835cd2008-06-26 23:03:21 +0200360# 'default' encryption is none -- this only prevents a warning
361$smtp_encryption = '' unless (defined $smtp_encryption);
362
David Brown65648282007-12-25 19:56:29 -0800363# Set CC suppressions
364my(%suppress_cc);
365if (@suppress_cc) {
366 foreach my $entry (@suppress_cc) {
367 die "Unknown --suppress-cc field: '$entry'\n"
Ævar Arnfjörð Bjarmasone9bf7412010-09-30 13:43:04 +0000368 unless $entry =~ /^(?:all|cccmd|cc|author|self|sob|body|bodycc)$/;
David Brown65648282007-12-25 19:56:29 -0800369 $suppress_cc{$entry} = 1;
370 }
371}
372
373if ($suppress_cc{'all'}) {
Paolo Bonzinicb8a9bd2009-06-18 14:31:32 +0200374 foreach my $entry (qw (cccmd cc author self sob body bodycc)) {
David Brown65648282007-12-25 19:56:29 -0800375 $suppress_cc{$entry} = 1;
376 }
377 delete $suppress_cc{'all'};
378}
379
380# If explicit old-style ones are specified, they trump --suppress-cc.
381$suppress_cc{'self'} = $suppress_from if defined $suppress_from;
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500382$suppress_cc{'sob'} = !$signed_off_by_cc if defined $signed_off_by_cc;
David Brown65648282007-12-25 19:56:29 -0800383
Jay Soffian3531e272009-02-14 23:32:15 -0500384if ($suppress_cc{'body'}) {
385 foreach my $entry (qw (sob bodycc)) {
386 $suppress_cc{$entry} = 1;
387 }
388 delete $suppress_cc{'body'};
389}
390
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500391# Set confirm's default value
392my $confirm_unconfigured = !defined $confirm;
393if ($confirm_unconfigured) {
394 $confirm = scalar %suppress_cc ? 'compose' : 'auto';
395};
396die "Unknown --confirm setting: '$confirm'\n"
397 unless $confirm =~ /^(?:auto|cc|compose|always|never)/;
398
David Brown65648282007-12-25 19:56:29 -0800399# Debugging, print out the suppressions.
400if (0) {
401 print "suppressions:\n";
402 foreach my $entry (keys %suppress_cc) {
403 printf " %-5s -> $suppress_cc{$entry}\n", $entry;
404 }
405}
406
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100407my ($repoauthor, $repocommitter);
408($repoauthor) = Git::ident_person(@repo, 'author');
409($repocommitter) = Git::ident_person(@repo, 'committer');
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900410
Eric W. Biederman79ee5552006-06-21 07:17:31 -0600411# Verify the user input
412
413foreach my $entry (@to) {
414 die "Comma in --to entry: $entry'\n" unless $entry !~ m/,/;
415}
416
417foreach my $entry (@initial_cc) {
418 die "Comma in --cc entry: $entry'\n" unless $entry !~ m/,/;
419}
420
421foreach my $entry (@bcclist) {
422 die "Comma in --bcclist entry: $entry'\n" unless $entry !~ m/,/;
423}
424
Jay Soffian50126992009-02-14 23:32:14 -0500425sub parse_address_line {
426 if ($have_mail_address) {
427 return map { $_->format } Mail::Address->parse($_[0]);
428 } else {
429 return split_addrs($_[0]);
430 }
431}
432
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800433sub split_addrs {
Junio C Hamano2f0e7cb2008-12-21 01:57:59 -0800434 return quotewords('\s*,\s*', 1, @_);
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800435}
436
Eric Wong994d6c62006-05-14 19:13:44 -0700437my %aliases;
Eric Wong994d6c62006-05-14 19:13:44 -0700438my %parse_alias = (
439 # multiline formats can be supported in the future
440 mutt => sub { my $fh = shift; while (<$fh>) {
Felipe Contrerasffc01f92009-09-30 17:49:36 +0300441 if (/^\s*alias\s+(?:-group\s+\S+\s+)*(\S+)\s+(.*)$/) {
Eric Wong994d6c62006-05-14 19:13:44 -0700442 my ($alias, $addr) = ($1, $2);
443 $addr =~ s/#.*$//; # mutt allows # comments
444 # commas delimit multiple addresses
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800445 $aliases{$alias} = [ split_addrs($addr) ];
Eric Wong994d6c62006-05-14 19:13:44 -0700446 }}},
447 mailrc => sub { my $fh = shift; while (<$fh>) {
448 if (/^alias\s+(\S+)\s+(.*)$/) {
449 # spaces delimit multiple addresses
Eric W. Biedermanfe87c922009-05-20 19:45:53 -0700450 $aliases{$1} = [ quotewords('\s+', 0, $2) ];
Eric Wong994d6c62006-05-14 19:13:44 -0700451 }}},
Trent Piepho73c427e2008-11-25 18:55:00 -0800452 pine => sub { my $fh = shift; my $f='\t[^\t]*';
453 for (my $x = ''; defined($x); $x = $_) {
454 chomp $x;
455 $x .= $1 while(defined($_ = <$fh>) && /^ +(.*)$/);
456 $x =~ /^(\S+)$f\t\(?([^\t]+?)\)?(:?$f){0,2}$/ or next;
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800457 $aliases{$1} = [ split_addrs($2) ];
Trent Piepho73c427e2008-11-25 18:55:00 -0800458 }},
Bill Pemberton7613ea32009-04-22 09:41:29 -0400459 elm => sub { my $fh = shift;
460 while (<$fh>) {
461 if (/^(\S+)\s+=\s+[^=]+=\s(\S+)/) {
462 my ($alias, $addr) = ($1, $2);
463 $aliases{$alias} = [ split_addrs($addr) ];
464 }
465 } },
466
Eric Wong994d6c62006-05-14 19:13:44 -0700467 gnus => sub { my $fh = shift; while (<$fh>) {
468 if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
469 $aliases{$1} = [ $2 ];
470 }}}
471);
472
Petr Baudis3cb8caf2006-07-03 22:47:58 +0200473if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) {
Eric Wong994d6c62006-05-14 19:13:44 -0700474 foreach my $file (@alias_files) {
475 open my $fh, '<', $file or die "opening $file: $!\n";
476 $parse_alias{$aliasfiletype}->($fh);
477 close $fh;
478 }
479}
480
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200481($sender) = expand_aliases($sender) if defined $sender;
Michael Hendricksae740a52007-07-04 19:11:36 -0600482
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100483# returns 1 if the conflict must be solved using it as a format-patch argument
484sub check_file_rev_conflict($) {
Jay Soffianeed6ca72009-02-14 23:32:13 -0500485 return unless $repo;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100486 my $f = shift;
487 try {
488 $repo->command('rev-parse', '--verify', '--quiet', $f);
489 if (defined($format_patch)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100490 return $format_patch;
491 }
492 die(<<EOF);
493File '$f' exists but it could also be the range of commits
494to produce patches for. Please disambiguate by...
495
496 * Saying "./$f" if you mean a file; or
497 * Giving --format-patch option if you mean a range.
498EOF
499 } catch Git::Error::Command with {
500 return 0;
501 }
502}
503
Jeff Kingaa548922008-01-18 09:19:36 -0500504# Now that all the defaults are set, process the rest of the command line
505# arguments and collect up the files that need to be processed.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100506my @rev_list_opts;
Junio C Hamano69f4ce52008-11-30 22:38:20 -0800507while (defined(my $f = shift @ARGV)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100508 if ($f eq "--") {
509 push @rev_list_opts, "--", @ARGV;
510 @ARGV = ();
511 } elsif (-d $f and !check_file_rev_conflict($f)) {
Ævar Arnfjörð Bjarmasonc6038162010-09-30 13:42:54 +0000512 opendir my $dh, $f
Jeff Kingaa548922008-01-18 09:19:36 -0500513 or die "Failed to opendir $f: $!";
514
Ævar Arnfjörð Bjarmason89bf1ba2010-09-14 19:02:24 +0000515 push @files, grep { -f $_ } map { catfile($f, $_) }
Ævar Arnfjörð Bjarmasonc6038162010-09-30 13:42:54 +0000516 sort readdir $dh;
517 closedir $dh;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100518 } elsif ((-f $f or -p $f) and !check_file_rev_conflict($f)) {
Jeff Kingaa548922008-01-18 09:19:36 -0500519 push @files, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500520 } else {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100521 push @rev_list_opts, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500522 }
523}
524
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100525if (@rev_list_opts) {
Jay Soffianeed6ca72009-02-14 23:32:13 -0500526 die "Cannot run git format-patch from outside a repository\n"
527 unless $repo;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100528 push @files, $repo->command('format-patch', '-o', tempdir(CLEANUP => 1), @rev_list_opts);
529}
530
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500531if ($validate) {
Jeff Kingc764a0c2008-01-18 09:20:10 -0500532 foreach my $f (@files) {
Kevin Ballard300913b2008-06-25 15:44:40 -0700533 unless (-p $f) {
534 my $error = validate_patch($f);
535 $error and die "fatal: $f: $error\nwarning: no patches were sent\n";
536 }
Jeff Kingc764a0c2008-01-18 09:20:10 -0500537 }
Jeff King747bbff2008-01-18 09:19:48 -0500538}
539
Jeff Kingaa548922008-01-18 09:19:36 -0500540if (@files) {
541 unless ($quiet) {
542 print $_,"\n" for (@files);
543 }
544} else {
545 print STDERR "\nNo patch files specified!\n\n";
546 usage();
547}
548
Ævar Arnfjörð Bjarmasonacf071b2010-09-30 13:42:57 +0000549sub get_patch_subject {
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100550 my $fn = shift;
551 open (my $fh, '<', $fn);
552 while (my $line = <$fh>) {
553 next unless ($line =~ /^Subject: (.*)$/);
554 close $fh;
555 return "GIT: $1\n";
556 }
557 close $fh;
558 die "No subject line in $fn ?";
559}
560
561if ($compose) {
562 # Note that this does not need to be secure, but we will make a small
563 # effort to have it be unique
Jay Soffianafe756c2009-02-23 13:51:37 -0500564 $compose_filename = ($repo ?
565 tempfile(".gitsendemail.msg.XXXXXX", DIR => $repo->repo_path()) :
566 tempfile(".gitsendemail.msg.XXXXXX", DIR => "."))[1];
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000567 open my $c, ">", $compose_filename
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100568 or die "Failed to open for writing $compose_filename: $!";
569
570
571 my $tpl_sender = $sender || $repoauthor || $repocommitter || '';
572 my $tpl_subject = $initial_subject || '';
573 my $tpl_reply_to = $initial_reply_to || '';
574
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000575 print $c <<EOT;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100576From $tpl_sender # This line is ignored.
Michael Witten40e6e8a2009-04-13 13:23:50 -0500577GIT: Lines beginning in "GIT:" will be removed.
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100578GIT: Consider including an overall diffstat or table of contents
579GIT: for the patch you are writing.
580GIT:
581GIT: Clear the body content if you don't wish to send a summary.
582From: $tpl_sender
583Subject: $tpl_subject
584In-Reply-To: $tpl_reply_to
585
586EOT
587 for my $f (@files) {
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000588 print $c get_patch_subject($f);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100589 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000590 close $c;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100591
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100592 if ($annotate) {
593 do_edit($compose_filename, @files);
594 } else {
595 do_edit($compose_filename);
596 }
597
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000598 open my $c2, ">", $compose_filename . ".final"
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100599 or die "Failed to open $compose_filename.final : " . $!;
600
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000601 open $c, "<", $compose_filename
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100602 or die "Failed to open $compose_filename : " . $!;
603
604 my $need_8bit_cte = file_has_nonascii($compose_filename);
605 my $in_body = 0;
606 my $summary_empty = 1;
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000607 while(<$c>) {
Michael Witten40e6e8a2009-04-13 13:23:50 -0500608 next if m/^GIT:/;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100609 if ($in_body) {
610 $summary_empty = 0 unless (/^\n$/);
611 } elsif (/^\n$/) {
612 $in_body = 1;
613 if ($need_8bit_cte) {
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000614 print $c2 "MIME-Version: 1.0\n",
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100615 "Content-Type: text/plain; ",
Brandon Caseyd1fff6f2009-06-06 20:12:31 -0500616 "charset=UTF-8\n",
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100617 "Content-Transfer-Encoding: 8bit\n";
618 }
619 } elsif (/^MIME-Version:/i) {
620 $need_8bit_cte = 0;
621 } elsif (/^Subject:\s*(.+)\s*$/i) {
622 $initial_subject = $1;
623 my $subject = $initial_subject;
624 $_ = "Subject: " .
625 ($subject =~ /[^[:ascii:]]/ ?
626 quote_rfc2047($subject) :
627 $subject) .
628 "\n";
629 } elsif (/^In-Reply-To:\s*(.+)\s*$/i) {
630 $initial_reply_to = $1;
631 next;
632 } elsif (/^From:\s*(.+)\s*$/i) {
633 $sender = $1;
634 next;
635 } elsif (/^(?:To|Cc|Bcc):/i) {
636 print "To/Cc/Bcc fields are not interpreted yet, they have been ignored\n";
637 next;
638 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000639 print $c2 $_;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100640 }
Ævar Arnfjörð Bjarmasonfe0f9442010-09-30 13:42:55 +0000641 close $c;
642 close $c2;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100643
644 if ($summary_empty) {
645 print "Summary email is empty, skipping it\n";
646 $compose = -1;
647 }
648} elsif ($annotate) {
649 do_edit(@files);
650}
651
Jay Soffian6e182512009-03-28 21:39:10 -0400652sub ask {
653 my ($prompt, %arg) = @_;
Jay Soffian0da43a62009-04-04 23:23:21 -0400654 my $valid_re = $arg{valid_re};
Jay Soffian6e182512009-03-28 21:39:10 -0400655 my $default = $arg{default};
656 my $resp;
657 my $i = 0;
Jay Soffian5906f542009-03-31 12:22:11 -0400658 return defined $default ? $default : undef
659 unless defined $term->IN and defined fileno($term->IN) and
660 defined $term->OUT and defined fileno($term->OUT);
Jay Soffian6e182512009-03-28 21:39:10 -0400661 while ($i++ < 10) {
662 $resp = $term->readline($prompt);
663 if (!defined $resp) { # EOF
664 print "\n";
665 return defined $default ? $default : undef;
666 }
667 if ($resp eq '' and defined $default) {
668 return $default;
669 }
Jay Soffian0da43a62009-04-04 23:23:21 -0400670 if (!defined $valid_re or $resp =~ /$valid_re/) {
Jay Soffian6e182512009-03-28 21:39:10 -0400671 return $resp;
672 }
673 }
674 return undef;
675}
676
Thomas Rast3cae7e52010-06-17 22:10:39 +0200677my %broken_encoding;
678
Ævar Arnfjörð Bjarmason1d50bfd2010-09-30 13:42:58 +0000679sub file_declares_8bit_cte {
Thomas Rast3cae7e52010-06-17 22:10:39 +0200680 my $fn = shift;
681 open (my $fh, '<', $fn);
682 while (my $line = <$fh>) {
683 last if ($line =~ /^$/);
684 return 1 if ($line =~ /^Content-Transfer-Encoding: .*8bit.*$/);
685 }
686 close $fh;
687 return 0;
688}
689
690foreach my $f (@files) {
691 next unless (body_or_subject_has_nonascii($f)
692 && !file_declares_8bit_cte($f));
693 $broken_encoding{$f} = 1;
694}
695
696if (!defined $auto_8bit_encoding && scalar %broken_encoding) {
697 print "The following files are 8bit, but do not declare " .
698 "a Content-Transfer-Encoding.\n";
699 foreach my $f (sort keys %broken_encoding) {
700 print " $f\n";
701 }
702 $auto_8bit_encoding = ask("Which 8bit encoding should I declare [UTF-8]? ",
703 default => "UTF-8");
704}
705
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200706if (!$force) {
707 for my $f (@files) {
Ævar Arnfjörð Bjarmason0d290a42010-09-30 13:43:01 +0000708 if (get_patch_subject($f) =~ /\Q*** SUBJECT HERE ***\E/) {
Thomas Rasta03bc5b2009-06-08 23:34:12 +0200709 die "Refusing to send because the patch\n\t$f\n"
710 . "has the template subject '*** SUBJECT HERE ***'. "
711 . "Pass --force if you really want to send.\n";
712 }
713 }
714}
715
Ryan Anderson1f038a02005-09-05 01:13:07 -0400716my $prompting = 0;
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200717if (!defined $sender) {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100718 $sender = $repoauthor || $repocommitter || '';
Jay Soffian6e182512009-03-28 21:39:10 -0400719 $sender = ask("Who should the emails appear to be from? [$sender] ",
720 default => $sender);
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200721 print "Emails will be sent from: ", $sender, "\n";
Ryan Anderson1f038a02005-09-05 01:13:07 -0400722 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -0400723}
724
725if (!@to) {
Jay Soffian6e182512009-03-28 21:39:10 -0400726 my $to = ask("Who should the emails be sent to? ");
727 push @to, parse_address_line($to) if defined $to; # sanitized/validated later
Ryan Anderson1f038a02005-09-05 01:13:07 -0400728 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -0400729}
730
Eric Wong994d6c62006-05-14 19:13:44 -0700731sub expand_aliases {
Jeff King302e04e2009-07-23 07:09:29 -0400732 return map { expand_one_alias($_) } @_;
733}
734
735my %EXPANDED_ALIASES;
736sub expand_one_alias {
737 my $alias = shift;
738 if ($EXPANDED_ALIASES{$alias}) {
739 die "fatal: alias '$alias' expands to itself\n";
740 }
741 local $EXPANDED_ALIASES{$alias} = 1;
742 return $aliases{$alias} ? expand_aliases(@{$aliases{$alias}}) : $alias;
Eric Wong994d6c62006-05-14 19:13:44 -0700743}
744
745@to = expand_aliases(@to);
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +0200746@to = (map { sanitize_address($_) } @to);
Eric Wong994d6c62006-05-14 19:13:44 -0700747@initial_cc = expand_aliases(@initial_cc);
Ryan Anderson58063242006-05-29 12:30:13 -0700748@bcclist = expand_aliases(@bcclist);
Eric Wong994d6c62006-05-14 19:13:44 -0700749
Adam Roben5483c712007-06-27 20:59:37 -0700750if ($thread && !defined $initial_reply_to && $prompting) {
Jay Soffian6e182512009-03-28 21:39:10 -0400751 $initial_reply_to = ask(
752 "Message-ID to be used as In-Reply-To for the first email? ");
Ryan Anderson83b24432005-07-31 04:17:25 -0400753}
Jay Soffian1ca3d6e2008-02-20 00:55:07 -0500754if (defined $initial_reply_to) {
Jay Soffian0fb7fc72008-02-21 19:16:04 -0500755 $initial_reply_to =~ s/^\s*<?//;
756 $initial_reply_to =~ s/>?\s*$//;
757 $initial_reply_to = "<$initial_reply_to>" if $initial_reply_to ne '';
Junio C Hamanoace9c2a2007-12-10 21:44:42 -0800758}
Mike Hommeyace72082007-12-09 18:17:28 +0100759
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900760if (!defined $smtp_server) {
Eric Wongaca7ad72006-05-15 02:34:44 -0700761 foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) {
762 if (-x $_) {
763 $smtp_server = $_;
764 last;
765 }
766 }
767 $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
Ryan Anderson3342d852005-07-31 20:04:24 -0400768}
769
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500770if ($compose && $compose > 0) {
771 @files = ($compose_filename . ".final", @files);
Ryan Anderson1f038a02005-09-05 01:13:07 -0400772}
773
Ryan Anderson83b24432005-07-31 04:17:25 -0400774# Variables we set as part of the loop over files
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500775our ($message_id, %mail, $subject, $reply_to, $references, $message,
Jay Soffiandc1460a2009-03-31 12:22:12 -0400776 $needs_confirm, $message_num, $ask_default);
Ryan Anderson83b24432005-07-31 04:17:25 -0400777
Eric Wong567ffeb2006-03-25 16:47:12 -0800778sub extract_valid_address {
779 my $address = shift;
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700780 my $local_part_regexp = '[^<>"\s@]+';
Junio C Hamano09302e12006-06-06 14:12:46 -0700781 my $domain_regexp = '[^.<>"\s@]+(?:\.[^.<>"\s@]+)+';
Eric Wongdb3106b2006-05-15 02:41:01 -0700782
783 # check for a local address:
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700784 return $address if ($address =~ /^($local_part_regexp)$/);
Eric Wongdb3106b2006-05-15 02:41:01 -0700785
Uwe Kleine-König155197e2007-08-09 15:27:57 +0200786 $address =~ s/^\s*<(.*)>\s*$/$1/;
Eric Wong567ffeb2006-03-25 16:47:12 -0800787 if ($have_email_valid) {
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700788 return scalar Email::Valid->address($address);
Eric Wong567ffeb2006-03-25 16:47:12 -0800789 } else {
790 # less robust/correct than the monster regexp in Email::Valid,
791 # but still does a 99% job, and one less dependency
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700792 $address =~ /($local_part_regexp\@$domain_regexp)/;
Horst H. von Brande96fd302006-06-03 13:11:48 -0400793 return $1;
Eric Wong567ffeb2006-03-25 16:47:12 -0800794 }
795}
Ryan Anderson83b24432005-07-31 04:17:25 -0400796
797# Usually don't need to change anything below here.
798
799# we make a "fake" message id by taking the current number
800# of seconds since the beginning of Unix time and tacking on
801# a random number to the end, in case we are called quicker than
802# 1 second since the last time we were called.
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400803
804# We'll setup a template for the message id, using the "from" address:
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400805
Junio C Hamanobe510cf2007-09-17 21:18:20 -0700806my ($message_id_stamp, $message_id_serial);
Brian Gernhardt68ce9332010-04-10 10:53:53 -0400807sub make_message_id {
Junio C Hamanobe510cf2007-09-17 21:18:20 -0700808 my $uniq;
809 if (!defined $message_id_stamp) {
810 $message_id_stamp = sprintf("%s-%s", time, $$);
811 $message_id_serial = 0;
812 }
813 $message_id_serial++;
814 $uniq = "$message_id_stamp-$message_id_serial";
815
Junio C Hamanoaeb59322007-06-20 13:47:34 -0700816 my $du_part;
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200817 for ($sender, $repocommitter, $repoauthor) {
818 $du_part = extract_valid_address(sanitize_address($_));
819 last if (defined $du_part and $du_part ne '');
Junio C Hamanoaeb59322007-06-20 13:47:34 -0700820 }
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200821 if (not defined $du_part or $du_part eq '') {
Ævar Arnfjörð Bjarmason529dd382010-09-30 13:43:08 +0000822 require Sys::Hostname;
Junio C Hamanoaeb59322007-06-20 13:47:34 -0700823 $du_part = 'user@' . Sys::Hostname::hostname();
824 }
Junio C Hamanobe510cf2007-09-17 21:18:20 -0700825 my $message_id_template = "<%s-git-send-email-%s>";
826 $message_id = sprintf($message_id_template, $uniq, $du_part);
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400827 #print "new message id = $message_id\n"; # Was useful for debugging
Ryan Anderson83b24432005-07-31 04:17:25 -0400828}
829
830
831
Eric Wonga5370b12006-03-25 03:01:01 -0800832$time = time - scalar $#files;
Ryan Anderson83b24432005-07-31 04:17:25 -0400833
Jürgen Rühle374c5902007-01-10 13:36:39 -0800834sub unquote_rfc2047 {
835 local ($_) = @_;
Jeff King8291db62007-11-16 05:49:09 -0500836 my $encoding;
837 if (s/=\?([^?]+)\?q\?(.*)\?=/$2/g) {
838 $encoding = $1;
Jürgen Rühle374c5902007-01-10 13:36:39 -0800839 s/_/ /g;
840 s/=([0-9A-F]{2})/chr(hex($1))/eg;
841 }
Jeff King8291db62007-11-16 05:49:09 -0500842 return wantarray ? ($_, $encoding) : $_;
Jürgen Rühle374c5902007-01-10 13:36:39 -0800843}
844
Jeff Kingd54eaaa2008-03-28 17:29:01 -0400845sub quote_rfc2047 {
846 local $_ = shift;
Brandon Caseyd1fff6f2009-06-06 20:12:31 -0500847 my $encoding = shift || 'UTF-8';
Jeff Kingd54eaaa2008-03-28 17:29:01 -0400848 s/([^-a-zA-Z0-9!*+\/])/sprintf("=%02X", ord($1))/eg;
849 s/(.*)/=\?$encoding\?q\?$1\?=/;
850 return $_;
851}
852
Brandon Caseya3a82622009-06-07 19:25:58 -0500853sub is_rfc2047_quoted {
854 my $s = shift;
855 my $token = '[^][()<>@,;:"\/?.= \000-\037\177-\377]+';
856 my $encoded_text = '[!->@-~]+';
857 length($s) <= 75 &&
858 $s =~ m/^(?:"[[:ascii:]]*"|=\?$token\?$token\?$encoded_text\?=)$/o;
859}
860
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +0200861# use the simplest quoting being able to handle the recipient
Brian Gernhardt68ce9332010-04-10 10:53:53 -0400862sub sanitize_address {
Robin H. Johnson732263d2007-04-25 19:37:19 -0700863 my ($recipient) = @_;
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +0200864 my ($recipient_name, $recipient_addr) = ($recipient =~ /^(.*?)\s*(<.*)/);
865
866 if (not $recipient_name) {
Ævar Arnfjörð Bjarmasonff483892010-09-30 13:43:02 +0000867 return $recipient;
Robin H. Johnson732263d2007-04-25 19:37:19 -0700868 }
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +0200869
870 # if recipient_name is already quoted, do nothing
Brandon Caseya3a82622009-06-07 19:25:58 -0500871 if (is_rfc2047_quoted($recipient_name)) {
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +0200872 return $recipient;
873 }
874
875 # rfc2047 is needed if a non-ascii char is included
876 if ($recipient_name =~ /[^[:ascii:]]/) {
Jay Soffiana61c0ff2009-03-31 12:22:14 -0400877 $recipient_name =~ s/^"(.*)"$/$1/;
Jeff Kingd54eaaa2008-03-28 17:29:01 -0400878 $recipient_name = quote_rfc2047($recipient_name);
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +0200879 }
880
881 # double quotes are needed if specials or CTLs are included
882 elsif ($recipient_name =~ /[][()<>@,;:\\".\000-\037\177]/) {
Horst H. von Brand18023c22008-03-28 11:09:04 -0300883 $recipient_name =~ s/(["\\\r])/\\$1/g;
Ævar Arnfjörð Bjarmasond5c7d692010-09-30 13:43:03 +0000884 $recipient_name = qq["$recipient_name"];
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +0200885 }
886
887 return "$recipient_name $recipient_addr";
888
Robin H. Johnson732263d2007-04-25 19:37:19 -0700889}
890
Jari Aalto134550f2010-03-14 17:16:45 +0200891# Returns the local Fully Qualified Domain Name (FQDN) if available.
892#
893# Tightly configured MTAa require that a caller sends a real DNS
894# domain name that corresponds the IP address in the HELO/EHLO
895# handshake. This is used to verify the connection and prevent
896# spammers from trying to hide their identity. If the DNS and IP don't
897# match, the receiveing MTA may deny the connection.
898#
899# Here is a deny example of Net::SMTP with the default "localhost.localdomain"
900#
901# Net::SMTP=GLOB(0x267ec28)>>> EHLO localhost.localdomain
902# Net::SMTP=GLOB(0x267ec28)<<< 550 EHLO argument does not match calling host
903#
904# This maildomain*() code is based on ideas in Perl library Test::Reporter
905# /usr/share/perl5/Test/Reporter/Mail/Util.pm ==> sub _maildomain ()
906
Brian Gernhardt59a86302010-04-10 10:53:54 -0400907sub valid_fqdn {
908 my $domain = shift;
Brandon Casey61ef5e92010-09-26 22:18:01 -0500909 return defined $domain && !($^O eq 'darwin' && $domain =~ /\.local$/) && $domain =~ /\./;
Brian Gernhardt59a86302010-04-10 10:53:54 -0400910}
911
Brian Gernhardt68ce9332010-04-10 10:53:53 -0400912sub maildomain_net {
Jari Aalto134550f2010-03-14 17:16:45 +0200913 my $maildomain;
914
915 if (eval { require Net::Domain; 1 }) {
916 my $domain = Net::Domain::domainname();
Brian Gernhardt59a86302010-04-10 10:53:54 -0400917 $maildomain = $domain if valid_fqdn($domain);
Jari Aalto134550f2010-03-14 17:16:45 +0200918 }
919
920 return $maildomain;
921}
922
Brian Gernhardt68ce9332010-04-10 10:53:53 -0400923sub maildomain_mta {
Jari Aalto134550f2010-03-14 17:16:45 +0200924 my $maildomain;
925
926 if (eval { require Net::SMTP; 1 }) {
927 for my $host (qw(mailhost localhost)) {
928 my $smtp = Net::SMTP->new($host);
929 if (defined $smtp) {
930 my $domain = $smtp->domain;
931 $smtp->quit;
932
Brian Gernhardt59a86302010-04-10 10:53:54 -0400933 $maildomain = $domain if valid_fqdn($domain);
Jari Aalto134550f2010-03-14 17:16:45 +0200934
935 last if $maildomain;
936 }
937 }
938 }
939
940 return $maildomain;
941}
942
Brian Gernhardt68ce9332010-04-10 10:53:53 -0400943sub maildomain {
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -0400944 return maildomain_net() || maildomain_mta() || 'localhost.localdomain';
Jari Aalto134550f2010-03-14 17:16:45 +0200945}
946
Michael Witten15da1082009-04-13 13:23:51 -0500947# Returns 1 if the message was sent, and 0 otherwise.
Markus Heidelberga1b5b372009-06-12 12:51:42 +0200948# In actuality, the whole program dies when there
Michael Witten15da1082009-04-13 13:23:51 -0500949# is an error sending a message.
950
Brian Gernhardt68ce9332010-04-10 10:53:53 -0400951sub send_message {
Eric Wong4bc87a22006-03-25 17:20:48 -0800952 my @recipients = unique_email_list(@to);
Ask Bjørn Hansen7ac17522007-11-19 03:00:26 -0800953 @cc = (grep { my $cc = extract_valid_address($_);
954 not grep { $cc eq $_ } @recipients
955 }
956 map { sanitize_address($_) }
957 @cc);
Eric Wong4bc87a22006-03-25 17:20:48 -0800958 my $to = join (",\n\t", @recipients);
Ryan Anderson58063242006-05-29 12:30:13 -0700959 @recipients = unique_email_list(@recipients,@cc,@bcclist);
Robin H. Johnsonc38f0242007-04-25 19:37:20 -0700960 @recipients = (map { extract_valid_address($_) } @recipients);
Jakub Narebski6bdca892006-07-07 20:57:55 +0200961 my $date = format_2822_time($time++);
Martin Langhoffe923eff2006-05-03 09:44:36 +1200962 my $gitversion = '@@GIT_VERSION@@';
963 if ($gitversion =~ m/..GIT_VERSION../) {
Petr Baudis3cb8caf2006-07-03 22:47:58 +0200964 $gitversion = Git::version();
Martin Langhoffe923eff2006-05-03 09:44:36 +1200965 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400966
Joe Perches02461e02009-10-08 10:03:26 -0700967 my $cc = join(",\n\t", unique_email_list(@cc));
Junio C Hamanof06a6a42007-04-16 16:51:47 -0700968 my $ccline = "";
969 if ($cc ne '') {
970 $ccline = "\nCc: $cc";
971 }
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200972 my $sanitized_sender = sanitize_address($sender);
Jeff King4f3d3702007-12-17 15:51:34 -0500973 make_message_id() unless defined($message_id);
Junio C Hamanoaeb59322007-06-20 13:47:34 -0700974
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200975 my $header = "From: $sanitized_sender
Junio C Hamanof06a6a42007-04-16 16:51:47 -0700976To: $to${ccline}
Eric Wong4bc87a22006-03-25 17:20:48 -0800977Subject: $subject
Eric Wong4bc87a22006-03-25 17:20:48 -0800978Date: $date
979Message-Id: $message_id
Martin Langhoffe923eff2006-05-03 09:44:36 +1200980X-Mailer: git-send-email $gitversion
Eric Wong4bc87a22006-03-25 17:20:48 -0800981";
Thomas Rast3e0c4ff2009-03-01 23:45:41 +0100982 if ($reply_to) {
Ryan Anderson7ccf7922006-05-29 12:30:12 -0700983
984 $header .= "In-Reply-To: $reply_to\n";
985 $header .= "References: $references\n";
986 }
Junio C Hamanoce91c2f2006-10-05 16:36:49 -0700987 if (@xh) {
988 $header .= join("\n", @xh) . "\n";
989 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400990
Robin H. Johnsonc38f0242007-04-25 19:37:20 -0700991 my @sendmail_parameters = ('-i', @recipients);
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200992 my $raw_from = $sanitized_sender;
Felipe Contrerasc89e3242009-11-26 21:04:29 +0200993 if (defined $envelope_sender && $envelope_sender ne "auto") {
994 $raw_from = $envelope_sender;
995 }
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700996 $raw_from = extract_valid_address($raw_from);
997 unshift (@sendmail_parameters,
998 '-f', $raw_from) if(defined $envelope_sender);
Robin H. Johnson8e3d4362007-04-25 19:37:17 -0700999
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001000 if ($needs_confirm && !$dry_run) {
1001 print "\n$header\n";
1002 if ($needs_confirm eq "inform") {
1003 $confirm_unconfigured = 0; # squelch this message for the rest of this run
Jay Soffian6e182512009-03-28 21:39:10 -04001004 $ask_default = "y"; # assume yes on EOF since user hasn't explicitly asked for confirmation
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001005 print " The Cc list above has been expanded by additional\n";
1006 print " addresses found in the patch commit message. By default\n";
1007 print " send-email prompts before sending whenever this occurs.\n";
1008 print " This behavior is controlled by the sendemail.confirm\n";
1009 print " configuration setting.\n";
1010 print "\n";
1011 print " For additional information, run 'git send-email --help'.\n";
1012 print " To retain the current behavior, but squelch this message,\n";
1013 print " run 'git config --global sendemail.confirm auto'.\n\n";
1014 }
Jay Soffian6e182512009-03-28 21:39:10 -04001015 $_ = ask("Send this email? ([y]es|[n]o|[q]uit|[a]ll): ",
1016 valid_re => qr/^(?:yes|y|no|n|quit|q|all|a)/i,
1017 default => $ask_default);
1018 die "Send this email reply required" unless defined $_;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001019 if (/^n/i) {
Michael Witten15da1082009-04-13 13:23:51 -05001020 return 0;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001021 } elsif (/^q/i) {
1022 cleanup_compose_files();
1023 exit(0);
1024 } elsif (/^a/i) {
1025 $confirm = 'never';
1026 }
1027 }
1028
Matthew Wilcox61302592006-10-10 08:58:23 -06001029 if ($dry_run) {
1030 # We don't want to send the email.
1031 } elsif ($smtp_server =~ m#^/#) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001032 my $pid = open my $sm, '|-';
1033 defined $pid or die $!;
1034 if (!$pid) {
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001035 exec($smtp_server, @sendmail_parameters) or die $!;
Eric Wongaca7ad72006-05-15 02:34:44 -07001036 }
1037 print $sm "$header\n$message";
Ævar Arnfjörð Bjarmason5e2c2ab2010-09-30 13:43:07 +00001038 close $sm or die $!;
Eric Wongaca7ad72006-05-15 02:34:44 -07001039 } else {
Junio C Hamano44b24762007-09-25 17:27:54 -07001040
1041 if (!defined $smtp_server) {
1042 die "The required SMTP server is not properly defined."
1043 }
1044
Thomas Rastf6bebd12008-06-25 21:42:43 +02001045 if ($smtp_encryption eq 'ssl') {
Junio C Hamano44b24762007-09-25 17:27:54 -07001046 $smtp_server_port ||= 465; # ssmtp
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001047 require Net::SMTP::SSL;
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001048 $smtp_domain ||= maildomain();
Jari Aalto134550f2010-03-14 17:16:45 +02001049 $smtp ||= Net::SMTP::SSL->new($smtp_server,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001050 Hello => $smtp_domain,
Jari Aalto134550f2010-03-14 17:16:45 +02001051 Port => $smtp_server_port);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001052 }
1053 else {
1054 require Net::SMTP;
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001055 $smtp_domain ||= maildomain();
Junio C Hamano44b24762007-09-25 17:27:54 -07001056 $smtp ||= Net::SMTP->new((defined $smtp_server_port)
1057 ? "$smtp_server:$smtp_server_port"
Jari Aaltof60812e2010-03-14 17:16:09 +02001058 : $smtp_server,
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001059 Hello => $smtp_domain,
Jari Aaltof60812e2010-03-14 17:16:09 +02001060 Debug => $debug_net_smtp);
Yakov Lernerfb3650e2009-09-25 15:10:21 -07001061 if ($smtp_encryption eq 'tls' && $smtp) {
Thomas Rastf6bebd12008-06-25 21:42:43 +02001062 require Net::SMTP::SSL;
1063 $smtp->command('STARTTLS');
1064 $smtp->response();
1065 if ($smtp->code == 220) {
1066 $smtp = Net::SMTP::SSL->start_SSL($smtp)
1067 or die "STARTTLS failed! ".$smtp->message;
Thomas Rast6cbf8b02008-07-03 00:11:31 +02001068 $smtp_encryption = '';
Robert Shearman9d1ccf52008-07-09 22:39:40 +01001069 # Send EHLO again to receive fresh
1070 # supported commands
1071 $smtp->hello();
Thomas Rastf6bebd12008-06-25 21:42:43 +02001072 } else {
1073 die "Server does not support STARTTLS! ".$smtp->message;
1074 }
1075 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001076 }
Junio C Hamano44b24762007-09-25 17:27:54 -07001077
1078 if (!$smtp) {
Jari Aaltof60812e2010-03-14 17:16:09 +02001079 die "Unable to initialize SMTP properly. Check config and use --smtp-debug. ",
Jari Aaltoe5afb3a2010-03-14 17:15:33 +02001080 "VALUES: server=$smtp_server ",
1081 "encryption=$smtp_encryption ",
Brian Gernhardt69cf7bf2010-04-10 10:53:56 -04001082 "hello=$smtp_domain",
Jari Aaltoe5afb3a2010-03-14 17:15:33 +02001083 defined $smtp_server_port ? "port=$smtp_server_port" : "";
Junio C Hamano44b24762007-09-25 17:27:54 -07001084 }
1085
Michael Witten2363d742008-02-03 19:53:56 -05001086 if (defined $smtp_authuser) {
1087
1088 if (!defined $smtp_authpass) {
1089
1090 system "stty -echo";
1091
1092 do {
1093 print "Password: ";
1094 $_ = <STDIN>;
1095 print "\n";
1096 } while (!defined $_);
1097
1098 chomp($smtp_authpass = $_);
1099
1100 system "stty echo";
1101 }
1102
Wincent Colaiuta5f5b6112007-11-21 13:35:05 +01001103 $auth ||= $smtp->auth( $smtp_authuser, $smtp_authpass ) or die $smtp->message;
Junio C Hamano44b24762007-09-25 17:27:54 -07001104 }
Michael Witten2363d742008-02-03 19:53:56 -05001105
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001106 $smtp->mail( $raw_from ) or die $smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001107 $smtp->to( @recipients ) or die $smtp->message;
1108 $smtp->data or die $smtp->message;
1109 $smtp->datasend("$header\n$message") or die $smtp->message;
1110 $smtp->dataend() or die $smtp->message;
Michael Witten15da1082009-04-13 13:23:51 -05001111 $smtp->code =~ /250|200/ or die "Failed to send $subject\n".$smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001112 }
Ryan Anderson27184352006-02-05 20:13:52 -05001113 if ($quiet) {
Robin H. Johnson71c7da92007-04-25 19:37:16 -07001114 printf (($dry_run ? "Dry-" : "")."Sent %s\n", $subject);
Ryan Anderson27184352006-02-05 20:13:52 -05001115 } else {
David D. Kilzerb7f30e02007-11-18 20:14:55 -08001116 print (($dry_run ? "Dry-" : "")."OK. Log says:\n");
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001117 if ($smtp_server !~ m#^/#) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001118 print "Server: $smtp_server\n";
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001119 print "MAIL FROM:<$raw_from>\n";
Joe Perches02461e02009-10-08 10:03:26 -07001120 foreach my $entry (@recipients) {
1121 print "RCPT TO:<$entry>\n";
1122 }
Eric Wongaca7ad72006-05-15 02:34:44 -07001123 } else {
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001124 print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001125 }
David D. Kilzerb7f30e02007-11-18 20:14:55 -08001126 print $header, "\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001127 if ($smtp) {
1128 print "Result: ", $smtp->code, ' ',
1129 ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
1130 } else {
1131 print "Result: OK\n";
1132 }
Ryan Anderson30d08b32006-02-02 11:56:06 -05001133 }
Michael Witten15da1082009-04-13 13:23:51 -05001134
1135 return 1;
Ryan Anderson83b24432005-07-31 04:17:25 -04001136}
1137
Ryan Anderson83b24432005-07-31 04:17:25 -04001138$reply_to = $initial_reply_to;
Junio C Hamano2186d562006-05-29 23:53:13 -07001139$references = $initial_reply_to || '';
Ryan Anderson83b24432005-07-31 04:17:25 -04001140$subject = $initial_subject;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001141$message_num = 0;
Ryan Anderson83b24432005-07-31 04:17:25 -04001142
1143foreach my $t (@files) {
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001144 open my $fh, "<", $t or die "can't open file $t";
Ryan Anderson83b24432005-07-31 04:17:25 -04001145
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001146 my $author = undef;
Jeff King8291db62007-11-16 05:49:09 -05001147 my $author_encoding;
1148 my $has_content_type;
1149 my $body_encoding;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001150 @cc = ();
Junio C Hamanoce91c2f2006-10-05 16:36:49 -07001151 @xh = ();
Junio C Hamanoe6b09642006-10-07 03:09:05 -07001152 my $input_format = undef;
Jay Soffian50126992009-02-14 23:32:14 -05001153 my @header = ();
Ryan Anderson83b24432005-07-31 04:17:25 -04001154 $message = "";
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001155 $message_num++;
Jay Soffian50126992009-02-14 23:32:14 -05001156 # First unfold multiline header fields
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001157 while(<$fh>) {
Jay Soffian50126992009-02-14 23:32:14 -05001158 last if /^\s*$/;
1159 if (/^\s+\S/ and @header) {
1160 chomp($header[$#header]);
1161 s/^\s+/ /;
1162 $header[$#header] .= $_;
1163 } else {
1164 push(@header, $_);
1165 }
1166 }
1167 # Now parse the header
1168 foreach(@header) {
1169 if (/^From /) {
1170 $input_format = 'mbox';
1171 next;
1172 }
1173 chomp;
1174 if (!defined $input_format && /^[-A-Za-z]+:\s/) {
1175 $input_format = 'mbox';
1176 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001177
Jay Soffian50126992009-02-14 23:32:14 -05001178 if (defined $input_format && $input_format eq 'mbox') {
1179 if (/^Subject:\s+(.*)$/) {
1180 $subject = $1;
1181 }
1182 elsif (/^From:\s+(.*)$/) {
1183 ($author, $author_encoding) = unquote_rfc2047($1);
1184 next if $suppress_cc{'author'};
1185 next if $suppress_cc{'self'} and $author eq $sender;
1186 printf("(mbox) Adding cc: %s from line '%s'\n",
1187 $1, $_) unless $quiet;
1188 push @cc, $1;
1189 }
1190 elsif (/^Cc:\s+(.*)$/) {
1191 foreach my $addr (parse_address_line($1)) {
1192 if (unquote_rfc2047($addr) eq $sender) {
David Brown65648282007-12-25 19:56:29 -08001193 next if ($suppress_cc{'self'});
David Brown65648282007-12-25 19:56:29 -08001194 } else {
1195 next if ($suppress_cc{'cc'});
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001196 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001197 printf("(mbox) Adding cc: %s from line '%s'\n",
Jay Soffian50126992009-02-14 23:32:14 -05001198 $addr, $_) unless $quiet;
1199 push @cc, $addr;
Ryan Anderson83b24432005-07-31 04:17:25 -04001200 }
1201 }
Jay Soffian50126992009-02-14 23:32:14 -05001202 elsif (/^Content-type:/i) {
1203 $has_content_type = 1;
1204 if (/charset="?([^ "]+)/) {
1205 $body_encoding = $1;
1206 }
1207 push @xh, $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001208 }
Jay Soffian50126992009-02-14 23:32:14 -05001209 elsif (/^Message-Id: (.*)/i) {
1210 $message_id = $1;
1211 }
1212 elsif (!/^Date:\s/ && /^[-A-Za-z]+:\s+\S/) {
1213 push @xh, $_;
1214 }
1215
Ryan Anderson83b24432005-07-31 04:17:25 -04001216 } else {
Jay Soffian50126992009-02-14 23:32:14 -05001217 # In the traditional
1218 # "send lots of email" format,
1219 # line 1 = cc
1220 # line 2 = subject
1221 # So let's support that, too.
1222 $input_format = 'lots';
1223 if (@cc == 0 && !$suppress_cc{'cc'}) {
1224 printf("(non-mbox) Adding cc: %s from line '%s'\n",
1225 $_, $_) unless $quiet;
1226 push @cc, $_;
1227 } elsif (!defined $subject) {
1228 $subject = $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001229 }
1230 }
1231 }
Jay Soffian50126992009-02-14 23:32:14 -05001232 # Now parse the message body
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001233 while(<$fh>) {
Jay Soffian50126992009-02-14 23:32:14 -05001234 $message .= $_;
1235 if (/^(Signed-off-by|Cc): (.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001236 chomp;
Jay Soffian3531e272009-02-14 23:32:15 -05001237 my ($what, $c) = ($1, $2);
Jay Soffian50126992009-02-14 23:32:14 -05001238 chomp $c;
Jay Soffian3531e272009-02-14 23:32:15 -05001239 if ($c eq $sender) {
1240 next if ($suppress_cc{'self'});
1241 } else {
1242 next if $suppress_cc{'sob'} and $what =~ /Signed-off-by/i;
1243 next if $suppress_cc{'bodycc'} and $what =~ /Cc/i;
1244 }
Jay Soffian50126992009-02-14 23:32:14 -05001245 push @cc, $c;
Jay Soffian3531e272009-02-14 23:32:15 -05001246 printf("(body) Adding cc: %s from line '%s'\n",
Jay Soffian50126992009-02-14 23:32:14 -05001247 $c, $_) unless $quiet;
1248 }
1249 }
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001250 close $fh;
Joe Perches324a8bd2007-08-17 18:51:12 -07001251
David Brown65648282007-12-25 19:56:29 -08001252 if (defined $cc_cmd && !$suppress_cc{'cccmd'}) {
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001253 open my $fh, "$cc_cmd \Q$t\E |"
Joe Perches324a8bd2007-08-17 18:51:12 -07001254 or die "(cc-cmd) Could not execute '$cc_cmd'";
Ævar Arnfjörð Bjarmason41ae8f12010-09-30 13:43:09 +00001255 while(my $c = <$fh>) {
1256 chomp $c;
Joe Perches324a8bd2007-08-17 18:51:12 -07001257 $c =~ s/^\s*//g;
Uwe Kleine-König620bb242007-11-07 08:34:12 +01001258 next if ($c eq $sender and $suppress_from);
Joe Perches324a8bd2007-08-17 18:51:12 -07001259 push @cc, $c;
1260 printf("(cc-cmd) Adding cc: %s from: '%s'\n",
1261 $c, $cc_cmd) unless $quiet;
1262 }
Ævar Arnfjörð Bjarmasonf9237e62010-09-30 13:42:56 +00001263 close $fh
Joe Perches324a8bd2007-08-17 18:51:12 -07001264 or die "(cc-cmd) failed to close pipe to '$cc_cmd'";
1265 }
1266
Thomas Rast3cae7e52010-06-17 22:10:39 +02001267 if ($broken_encoding{$t} && !$has_content_type) {
1268 $has_content_type = 1;
1269 push @xh, "MIME-Version: 1.0",
1270 "Content-Type: text/plain; charset=$auto_8bit_encoding",
1271 "Content-Transfer-Encoding: 8bit";
1272 $body_encoding = $auto_8bit_encoding;
1273 }
1274
1275 if ($broken_encoding{$t} && !is_rfc2047_quoted($subject)) {
1276 $subject = quote_rfc2047($subject, $auto_8bit_encoding);
1277 }
1278
Jay Soffian50126992009-02-14 23:32:14 -05001279 if (defined $author and $author ne $sender) {
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001280 $message = "From: $author\n\n$message";
Jeff King8291db62007-11-16 05:49:09 -05001281 if (defined $author_encoding) {
1282 if ($has_content_type) {
1283 if ($body_encoding eq $author_encoding) {
1284 # ok, we already have the right encoding
1285 }
1286 else {
1287 # uh oh, we should re-encode
1288 }
1289 }
1290 else {
Thomas Rast3cae7e52010-06-17 22:10:39 +02001291 $has_content_type = 1;
Jeff King8291db62007-11-16 05:49:09 -05001292 push @xh,
1293 'MIME-Version: 1.0',
Jeff King8641ee32007-11-20 07:54:04 -05001294 "Content-Type: text/plain; charset=$author_encoding",
1295 'Content-Transfer-Encoding: 8bit';
Jeff King8291db62007-11-16 05:49:09 -05001296 }
1297 }
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001298 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001299
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001300 $needs_confirm = (
1301 $confirm eq "always" or
1302 ($confirm =~ /^(?:auto|cc)$/ && @cc) or
1303 ($confirm =~ /^(?:auto|compose)$/ && $compose && $message_num == 1));
1304 $needs_confirm = "inform" if ($needs_confirm && $confirm_unconfigured && @cc);
1305
1306 @cc = (@initial_cc, @cc);
1307
Michael Witten15da1082009-04-13 13:23:51 -05001308 my $message_was_sent = send_message();
Ryan Anderson83b24432005-07-31 04:17:25 -04001309
1310 # set up for the next message
Junio C Hamano95a877a2009-06-12 09:23:43 -07001311 if ($thread && $message_was_sent &&
Nanako Shiraishi528fb082009-11-29 12:24:48 +09001312 (chain_reply_to() || !defined $reply_to || length($reply_to) == 0)) {
Ryan Anderson78488b22005-07-31 20:04:24 -04001313 $reply_to = $message_id;
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001314 if (length $references > 0) {
YOSHIFUJI Hideaki / 吉藤英明a925b892007-04-06 08:50:24 +09001315 $references .= "\n $message_id";
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001316 } else {
1317 $references = "$message_id";
1318 }
Ryan Anderson78488b22005-07-31 20:04:24 -04001319 }
Jeff King4f3d3702007-12-17 15:51:34 -05001320 $message_id = undef;
Ryan Anderson83b24432005-07-31 04:17:25 -04001321}
Ryan Andersone2057352005-08-02 21:45:22 -04001322
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001323cleanup_compose_files();
Ryan Anderson1f038a02005-09-05 01:13:07 -04001324
Ævar Arnfjörð Bjarmason4bf597e2010-09-30 13:43:00 +00001325sub cleanup_compose_files {
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001326 unlink($compose_filename, $compose_filename . ".final") if $compose;
Ryan Anderson1f038a02005-09-05 01:13:07 -04001327}
1328
Eric Wong4bc87a22006-03-25 17:20:48 -08001329$smtp->quit if $smtp;
Ryan Andersone2057352005-08-02 21:45:22 -04001330
Ævar Arnfjörð Bjarmasonc438ea22010-09-30 13:42:59 +00001331sub unique_email_list {
Ryan Andersone2057352005-08-02 21:45:22 -04001332 my %seen;
1333 my @emails;
1334
1335 foreach my $entry (@_) {
Eric Wongdb3106b2006-05-15 02:41:01 -07001336 if (my $clean = extract_valid_address($entry)) {
1337 $seen{$clean} ||= 0;
1338 next if $seen{$clean}++;
1339 push @emails, $entry;
1340 } else {
1341 print STDERR "W: unable to extract a valid address",
1342 " from: $entry\n";
1343 }
Ryan Andersone2057352005-08-02 21:45:22 -04001344 }
1345 return @emails;
1346}
Jeff King747bbff2008-01-18 09:19:48 -05001347
1348sub validate_patch {
1349 my $fn = shift;
1350 open(my $fh, '<', $fn)
1351 or die "unable to open $fn: $!\n";
1352 while (my $line = <$fh>) {
1353 if (length($line) > 998) {
1354 return "$.: patch contains a line longer than 998 characters";
1355 }
1356 }
1357 return undef;
1358}
Jeff King0706bd12008-03-28 17:28:33 -04001359
1360sub file_has_nonascii {
1361 my $fn = shift;
1362 open(my $fh, '<', $fn)
1363 or die "unable to open $fn: $!\n";
1364 while (my $line = <$fh>) {
1365 return 1 if $line =~ /[^[:ascii:]]/;
1366 }
1367 return 0;
1368}
Thomas Rast3cae7e52010-06-17 22:10:39 +02001369
1370sub body_or_subject_has_nonascii {
1371 my $fn = shift;
1372 open(my $fh, '<', $fn)
1373 or die "unable to open $fn: $!\n";
1374 while (my $line = <$fh>) {
1375 last if $line =~ /^$/;
1376 return 1 if $line =~ /^Subject.*[^[:ascii:]]/;
1377 }
1378 while (my $line = <$fh>) {
1379 return 1 if $line =~ /[^[:ascii:]]/;
1380 }
1381 return 0;
1382}