blob: ce569a9c8f964b3cdc8920325bc817141035c2c3 [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 /;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +010027use Error qw(:try);
Petr Baudis3cb8caf2006-07-03 22:47:58 +020028use Git;
Ryan Anderson83b24432005-07-31 04:17:25 -040029
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +010030Getopt::Long::Configure qw/ pass_through /;
31
Junio C Hamano280242d2006-07-02 16:03:59 -070032package FakeTerm;
33sub new {
34 my ($class, $reason) = @_;
35 return bless \$reason, shift;
36}
37sub readline {
38 my $self = shift;
39 die "Cannot use readline on FakeTerm: $$self";
40}
41package main;
42
Michael Coleman1b0baf12007-02-27 22:47:54 -060043
44sub usage {
45 print <<EOT;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +010046git send-email [options] <file | directory | rev-list options >
Michael Witten4ed62b02008-09-30 07:58:30 -050047
48 Composing:
49 --from <str> * Email From:
Stephen Boydf434c082010-03-07 14:46:48 -080050 --[no-]to <str> * Email To:
51 --[no-]cc <str> * Email Cc:
52 --[no-]bcc <str> * Email Bcc:
Michael Witten4ed62b02008-09-30 07:58:30 -050053 --subject <str> * Email "Subject:"
54 --in-reply-to <str> * Email "In-Reply-To:"
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +010055 --annotate * Review each patch that will be sent in an editor.
Michael Witten4ed62b02008-09-30 07:58:30 -050056 --compose * Open an editor for introduction.
57
58 Sending:
59 --envelope-sender <str> * Email envelope sender.
60 --smtp-server <str:int> * Outgoing SMTP server to use. The port
61 is optional. Default 'localhost'.
62 --smtp-server-port <int> * Outgoing SMTP server port.
63 --smtp-user <str> * Username for SMTP-AUTH.
64 --smtp-pass <str> * Password for SMTP-AUTH; not necessary.
65 --smtp-encryption <str> * tls or ssl; anything else disables.
66 --smtp-ssl * Deprecated. Use '--smtp-encryption ssl'.
Jari Aalto134550f2010-03-14 17:16:45 +020067 --smtp-domain <str> * The domain name sent to HELO/EHLO handshake
Jari Aaltof60812e2010-03-14 17:16:09 +020068 --smtp-debug <0|1> * Disable, enable Net::SMTP debug.
Michael Witten4ed62b02008-09-30 07:58:30 -050069
70 Automating:
71 --identity <str> * Use the sendemail.<id> options.
72 --cc-cmd <str> * Email Cc: via `<str> \$patch_path`
Jay Soffian3531e272009-02-14 23:32:15 -050073 --suppress-cc <str> * author, self, sob, cc, cccmd, body, bodycc, all.
74 --[no-]signed-off-by-cc * Send to Signed-off-by: addresses. Default on.
Michael Witten4ed62b02008-09-30 07:58:30 -050075 --[no-]suppress-from * Send to self. Default off.
Junio C Hamano41fe87f2009-08-22 12:48:48 -070076 --[no-]chain-reply-to * Chain In-Reply-To: fields. Default off.
Michael Witten4ed62b02008-09-30 07:58:30 -050077 --[no-]thread * Use In-Reply-To: field. Default on.
78
79 Administering:
Jay Soffianc1f2aa42009-03-02 23:52:18 -050080 --confirm <str> * Confirm recipients before sending;
81 auto, cc, compose, always, or never.
Michael Witten4ed62b02008-09-30 07:58:30 -050082 --quiet * Output one line of info per email.
83 --dry-run * Don't actually send the emails.
84 --[no-]validate * Perform patch sanity checks. Default on.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +010085 --[no-]format-patch * understand any non optional arguments as
86 `git format-patch` ones.
Jeff Kingc764a0c2008-01-18 09:20:10 -050087
Michael Coleman1b0baf12007-02-27 22:47:54 -060088EOT
89 exit(1);
90}
91
Eric Wong4bc87a22006-03-25 17:20:48 -080092# most mail servers generate the Date: header, but not all...
Jakub Narebski6bdca892006-07-07 20:57:55 +020093sub format_2822_time {
94 my ($time) = @_;
95 my @localtm = localtime($time);
96 my @gmttm = gmtime($time);
97 my $localmin = $localtm[1] + $localtm[2] * 60;
98 my $gmtmin = $gmttm[1] + $gmttm[2] * 60;
99 if ($localtm[0] != $gmttm[0]) {
100 die "local zone differs from GMT by a non-minute interval\n";
101 }
102 if ((($gmttm[6] + 1) % 7) == $localtm[6]) {
103 $localmin += 1440;
104 } elsif ((($gmttm[6] - 1) % 7) == $localtm[6]) {
105 $localmin -= 1440;
106 } elsif ($gmttm[6] != $localtm[6]) {
107 die "local time offset greater than or equal to 24 hours\n";
108 }
109 my $offset = $localmin - $gmtmin;
110 my $offhour = $offset / 60;
111 my $offmin = abs($offset % 60);
112 if (abs($offhour) >= 24) {
113 die ("local time offset greater than or equal to 24 hours\n");
114 }
115
116 return sprintf("%s, %2d %s %d %02d:%02d:%02d %s%02d%02d",
117 qw(Sun Mon Tue Wed Thu Fri Sat)[$localtm[6]],
118 $localtm[3],
119 qw(Jan Feb Mar Apr May Jun
120 Jul Aug Sep Oct Nov Dec)[$localtm[4]],
121 $localtm[5]+1900,
122 $localtm[2],
123 $localtm[1],
124 $localtm[0],
125 ($offset >= 0) ? '+' : '-',
126 abs($offhour),
127 $offmin,
128 );
129}
Eric Wong4bc87a22006-03-25 17:20:48 -0800130
Eric Wong567ffeb2006-03-25 16:47:12 -0800131my $have_email_valid = eval { require Email::Valid; 1 };
Jay Soffian50126992009-02-14 23:32:14 -0500132my $have_mail_address = eval { require Mail::Address; 1 };
Eric Wong4bc87a22006-03-25 17:20:48 -0800133my $smtp;
Wincent Colaiuta5f5b6112007-11-21 13:35:05 +0100134my $auth;
Jari Aalto134550f2010-03-14 17:16:45 +0200135my $mail_domain_default = "localhost.localdomain";
136my $mail_domain;
Eric Wong4bc87a22006-03-25 17:20:48 -0800137
Ryan Andersone2057352005-08-02 21:45:22 -0400138sub unique_email_list(@);
Ryan Anderson1f038a02005-09-05 01:13:07 -0400139sub cleanup_compose_files();
140
Ryan Anderson83b24432005-07-31 04:17:25 -0400141# Variables we fill in automatically, or via prompting:
Stephen Boydf434c082010-03-07 14:46:48 -0800142my (@to,$no_to,@cc,$no_cc,@initial_cc,@bcclist,$no_bcc,@xh,
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100143 $initial_reply_to,$initial_subject,@files,
144 $author,$sender,$smtp_authpass,$annotate,$compose,$time);
Ryan Anderson83b24432005-07-31 04:17:25 -0400145
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700146my $envelope_sender;
Ryan Anderson78488b22005-07-31 20:04:24 -0400147
Ryan Anderson91332612005-07-31 20:04:24 -0400148# Example reply to:
Ryan Anderson83b24432005-07-31 04:17:25 -0400149#$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
Ryan Anderson83b24432005-07-31 04:17:25 -0400150
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100151my $repo = eval { Git->repository() };
152my @repo = $repo ? ($repo) : ();
Junio C Hamano280242d2006-07-02 16:03:59 -0700153my $term = eval {
Jay Soffian0fb7fc72008-02-21 19:16:04 -0500154 $ENV{"GIT_SEND_EMAIL_NOTTY"}
155 ? new Term::ReadLine 'git-send-email', \*STDIN, \*STDOUT
156 : new Term::ReadLine 'git-send-email';
Junio C Hamano280242d2006-07-02 16:03:59 -0700157};
158if ($@) {
159 $term = new FakeTerm "$@: going non-interactive";
160}
Ryan Anderson83b24432005-07-31 04:17:25 -0400161
Adam Roben5483c712007-06-27 20:59:37 -0700162# Behavior modification variables
163my ($quiet, $dry_run) = (0, 0);
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100164my $format_patch;
Jay Soffianafe756c2009-02-23 13:51:37 -0500165my $compose_filename;
Adam Roben5483c712007-06-27 20:59:37 -0700166
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100167# Handle interactive edition of files.
168my $multiedit;
Michael J Gruber0ce142c2010-03-22 17:12:53 +0100169my $editor;
Jonathan Niederb4479f02009-10-30 20:42:34 -0500170
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100171sub do_edit {
Michael J Gruber0ce142c2010-03-22 17:12:53 +0100172 if (!defined($editor)) {
173 $editor = Git::command_oneline('var', 'GIT_EDITOR');
174 }
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100175 if (defined($multiedit) && !$multiedit) {
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100176 map {
177 system('sh', '-c', $editor.' "$@"', $editor, $_);
178 if (($? & 127) || ($? >> 8)) {
179 die("the editor exited uncleanly, aborting everything");
180 }
181 } @_;
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100182 } else {
183 system('sh', '-c', $editor.' "$@"', $editor, @_);
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100184 if (($? & 127) || ($? >> 8)) {
185 die("the editor exited uncleanly, aborting everything");
186 }
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100187 }
188}
Adam Roben5483c712007-06-27 20:59:37 -0700189
190# Variables with corresponding config settings
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500191my ($thread, $chain_reply_to, $suppress_from, $signed_off_by_cc, $cc_cmd);
Thomas Rastf6bebd12008-06-25 21:42:43 +0200192my ($smtp_server, $smtp_server_port, $smtp_authuser, $smtp_encryption);
Junio C Hamano44b24762007-09-25 17:27:54 -0700193my ($identity, $aliasfiletype, @alias_files, @smtp_host_parts);
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500194my ($validate, $confirm);
David Brown65648282007-12-25 19:56:29 -0800195my (@suppress_cc);
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,
Miklos Vajna2db9b492007-10-01 14:42:42 +0200215 "to" => \@to,
Miklos Vajna5f8b9fc2008-04-27 14:14:58 +0200216 "cc" => \@initial_cc,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900217 "cccmd" => \$cc_cmd,
218 "aliasfiletype" => \$aliasfiletype,
219 "bcc" => \@bcclist,
220 "aliasesfile" => \@alias_files,
David Brown65648282007-12-25 19:56:29 -0800221 "suppresscc" => \@suppress_cc,
Ask Bjørn Hansen9f7820a2008-06-07 00:33:42 -0700222 "envelopesender" => \$envelope_sender,
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100223 "multiedit" => \$multiedit,
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500224 "confirm" => \$confirm,
Trent Piepho09caa242009-05-12 15:48:56 -0700225 "from" => \$sender,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900226);
Avi Kivity4a62d3f2007-03-11 19:19:44 +0200227
Nanako Shiraishi528fb082009-11-29 12:24:48 +0900228# Help users prepare for 1.7.0
229sub chain_reply_to {
230 if (defined $chain_reply_to &&
231 $chain_reply_to eq $not_set_by_user) {
232 print STDERR
Junio C Hamanoa19f1012009-12-26 14:03:17 -0800233 "In git 1.7.0, the default has changed to --no-chain-reply-to\n" .
Nanako Shiraishi528fb082009-11-29 12:24:48 +0900234 "Set sendemail.chainreplyto configuration variable to true if\n" .
235 "you want to keep --chain-reply-to as your default.\n";
Junio C Hamanoa19f1012009-12-26 14:03:17 -0800236 $chain_reply_to = 0;
Nanako Shiraishi528fb082009-11-29 12:24:48 +0900237 }
238 return $chain_reply_to;
239}
240
Michael Witten87429972008-02-03 19:53:57 -0500241# Handle Uncouth Termination
242sub signal_handler {
243
244 # Make text normal
245 print color("reset"), "\n";
246
247 # SMTP password masked
248 system "stty echo";
249
250 # tmp files from --compose
Jay Soffianafe756c2009-02-23 13:51:37 -0500251 if (defined $compose_filename) {
252 if (-e $compose_filename) {
253 print "'$compose_filename' contains an intermediate version of the email you were composing.\n";
254 }
255 if (-e ($compose_filename . ".final")) {
256 print "'$compose_filename.final' contains the composed email.\n"
257 }
Michael Witten87429972008-02-03 19:53:57 -0500258 }
259
260 exit;
261};
262
263$SIG{TERM} = \&signal_handler;
264$SIG{INT} = \&signal_handler;
265
Ryan Anderson83b24432005-07-31 04:17:25 -0400266# Begin by accumulating all the variables (defined above), that we will end up
267# needing, first, from the command line:
268
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200269my $rc = GetOptions("sender|from=s" => \$sender,
Ryan Anderson83b24432005-07-31 04:17:25 -0400270 "in-reply-to=s" => \$initial_reply_to,
271 "subject=s" => \$initial_subject,
272 "to=s" => \@to,
Stephen Boydf434c082010-03-07 14:46:48 -0800273 "no-to" => \$no_to,
Ryan Andersonda140f82006-02-13 03:05:15 -0500274 "cc=s" => \@initial_cc,
Stephen Boydf434c082010-03-07 14:46:48 -0800275 "no-cc" => \$no_cc,
Ryan Anderson58063242006-05-29 12:30:13 -0700276 "bcc=s" => \@bcclist,
Stephen Boydf434c082010-03-07 14:46:48 -0800277 "no-bcc" => \$no_bcc,
Ryan Anderson78488b22005-07-31 20:04:24 -0400278 "chain-reply-to!" => \$chain_reply_to,
Ryan Anderson3342d852005-07-31 20:04:24 -0400279 "smtp-server=s" => \$smtp_server,
Junio C Hamano44b24762007-09-25 17:27:54 -0700280 "smtp-server-port=s" => \$smtp_server_port,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900281 "smtp-user=s" => \$smtp_authuser,
Michael Witten2363d742008-02-03 19:53:56 -0500282 "smtp-pass:s" => \$smtp_authpass,
Thomas Rastf6bebd12008-06-25 21:42:43 +0200283 "smtp-ssl" => sub { $smtp_encryption = 'ssl' },
284 "smtp-encryption=s" => \$smtp_encryption,
Jari Aaltof60812e2010-03-14 17:16:09 +0200285 "smtp-debug:i" => \$debug_net_smtp,
Jari Aalto134550f2010-03-14 17:16:45 +0200286 "smtp-domain:s" => \$mail_domain,
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900287 "identity=s" => \$identity,
Pierre Habouzit8fd5bb72008-11-11 00:54:01 +0100288 "annotate" => \$annotate,
Ryan Anderson1f038a02005-09-05 01:13:07 -0400289 "compose" => \$compose,
Ryan Anderson30d08b32006-02-02 11:56:06 -0500290 "quiet" => \$quiet,
Joe Perches324a8bd2007-08-17 18:51:12 -0700291 "cc-cmd=s" => \$cc_cmd,
Adam Roben5483c712007-06-27 20:59:37 -0700292 "suppress-from!" => \$suppress_from,
David Brown65648282007-12-25 19:56:29 -0800293 "suppress-cc=s" => \@suppress_cc,
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500294 "signed-off-cc|signed-off-by-cc!" => \$signed_off_by_cc,
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500295 "confirm=s" => \$confirm,
Matthew Wilcox61302592006-10-10 08:58:23 -0600296 "dry-run" => \$dry_run,
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700297 "envelope-sender=s" => \$envelope_sender,
Adam Roben5483c712007-06-27 20:59:37 -0700298 "thread!" => \$thread,
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500299 "validate!" => \$validate,
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100300 "format-patch!" => \$format_patch,
Ryan Anderson83b24432005-07-31 04:17:25 -0400301 );
302
Michael Coleman1b0baf12007-02-27 22:47:54 -0600303unless ($rc) {
304 usage();
305}
306
Jay Soffianeed6ca72009-02-14 23:32:13 -0500307die "Cannot run git format-patch from outside a repository\n"
308 if $format_patch and not $repo;
309
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900310# Now, let's fill any that aren't set in with defaults:
311
312sub read_config {
313 my ($prefix) = @_;
314
315 foreach my $setting (keys %config_bool_settings) {
316 my $target = $config_bool_settings{$setting}->[0];
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100317 $$target = Git::config_bool(@repo, "$prefix.$setting") unless (defined $$target);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900318 }
319
320 foreach my $setting (keys %config_settings) {
321 my $target = $config_settings{$setting};
Stephen Boydf434c082010-03-07 14:46:48 -0800322 next if $setting eq "to" and defined $no_to;
323 next if $setting eq "cc" and defined $no_cc;
324 next if $setting eq "bcc" and defined $no_bcc;
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900325 if (ref($target) eq "ARRAY") {
326 unless (@$target) {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100327 my @values = Git::config(@repo, "$prefix.$setting");
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900328 @$target = @values if (@values && defined $values[0]);
329 }
330 }
331 else {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100332 $$target = Git::config(@repo, "$prefix.$setting") unless (defined $$target);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900333 }
334 }
Thomas Rastf6bebd12008-06-25 21:42:43 +0200335
336 if (!defined $smtp_encryption) {
337 my $enc = Git::config(@repo, "$prefix.smtpencryption");
338 if (defined $enc) {
339 $smtp_encryption = $enc;
340 } elsif (Git::config_bool(@repo, "$prefix.smtpssl")) {
341 $smtp_encryption = 'ssl';
342 }
343 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900344}
345
346# read configuration from [sendemail "$identity"], fall back on [sendemail]
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100347$identity = Git::config(@repo, "sendemail.identity") unless (defined $identity);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900348read_config("sendemail.$identity") if (defined $identity);
349read_config("sendemail");
350
351# fall back on builtin bool defaults
352foreach my $setting (values %config_bool_settings) {
353 ${$setting->[0]} = $setting->[1] unless (defined (${$setting->[0]}));
354}
355
Thomas Rastfa835cd2008-06-26 23:03:21 +0200356# 'default' encryption is none -- this only prevents a warning
357$smtp_encryption = '' unless (defined $smtp_encryption);
358
David Brown65648282007-12-25 19:56:29 -0800359# Set CC suppressions
360my(%suppress_cc);
361if (@suppress_cc) {
362 foreach my $entry (@suppress_cc) {
363 die "Unknown --suppress-cc field: '$entry'\n"
Jay Soffian3531e272009-02-14 23:32:15 -0500364 unless $entry =~ /^(all|cccmd|cc|author|self|sob|body|bodycc)$/;
David Brown65648282007-12-25 19:56:29 -0800365 $suppress_cc{$entry} = 1;
366 }
367}
368
369if ($suppress_cc{'all'}) {
Paolo Bonzinicb8a9bd2009-06-18 14:31:32 +0200370 foreach my $entry (qw (cccmd cc author self sob body bodycc)) {
David Brown65648282007-12-25 19:56:29 -0800371 $suppress_cc{$entry} = 1;
372 }
373 delete $suppress_cc{'all'};
374}
375
376# If explicit old-style ones are specified, they trump --suppress-cc.
377$suppress_cc{'self'} = $suppress_from if defined $suppress_from;
Michael Wittenddc3d4f2008-09-30 07:58:32 -0500378$suppress_cc{'sob'} = !$signed_off_by_cc if defined $signed_off_by_cc;
David Brown65648282007-12-25 19:56:29 -0800379
Jay Soffian3531e272009-02-14 23:32:15 -0500380if ($suppress_cc{'body'}) {
381 foreach my $entry (qw (sob bodycc)) {
382 $suppress_cc{$entry} = 1;
383 }
384 delete $suppress_cc{'body'};
385}
386
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500387# Set confirm's default value
388my $confirm_unconfigured = !defined $confirm;
389if ($confirm_unconfigured) {
390 $confirm = scalar %suppress_cc ? 'compose' : 'auto';
391};
392die "Unknown --confirm setting: '$confirm'\n"
393 unless $confirm =~ /^(?:auto|cc|compose|always|never)/;
394
David Brown65648282007-12-25 19:56:29 -0800395# Debugging, print out the suppressions.
396if (0) {
397 print "suppressions:\n";
398 foreach my $entry (keys %suppress_cc) {
399 printf " %-5s -> $suppress_cc{$entry}\n", $entry;
400 }
401}
402
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100403my ($repoauthor, $repocommitter);
404($repoauthor) = Git::ident_person(@repo, 'author');
405($repocommitter) = Git::ident_person(@repo, 'committer');
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900406
Eric W. Biederman79ee5552006-06-21 07:17:31 -0600407# Verify the user input
408
409foreach my $entry (@to) {
410 die "Comma in --to entry: $entry'\n" unless $entry !~ m/,/;
411}
412
413foreach my $entry (@initial_cc) {
414 die "Comma in --cc entry: $entry'\n" unless $entry !~ m/,/;
415}
416
417foreach my $entry (@bcclist) {
418 die "Comma in --bcclist entry: $entry'\n" unless $entry !~ m/,/;
419}
420
Jay Soffian50126992009-02-14 23:32:14 -0500421sub parse_address_line {
422 if ($have_mail_address) {
423 return map { $_->format } Mail::Address->parse($_[0]);
424 } else {
425 return split_addrs($_[0]);
426 }
427}
428
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800429sub split_addrs {
Junio C Hamano2f0e7cb2008-12-21 01:57:59 -0800430 return quotewords('\s*,\s*', 1, @_);
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800431}
432
Eric Wong994d6c62006-05-14 19:13:44 -0700433my %aliases;
Eric Wong994d6c62006-05-14 19:13:44 -0700434my %parse_alias = (
435 # multiline formats can be supported in the future
436 mutt => sub { my $fh = shift; while (<$fh>) {
Felipe Contrerasffc01f92009-09-30 17:49:36 +0300437 if (/^\s*alias\s+(?:-group\s+\S+\s+)*(\S+)\s+(.*)$/) {
Eric Wong994d6c62006-05-14 19:13:44 -0700438 my ($alias, $addr) = ($1, $2);
439 $addr =~ s/#.*$//; # mutt allows # comments
440 # commas delimit multiple addresses
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800441 $aliases{$alias} = [ split_addrs($addr) ];
Eric Wong994d6c62006-05-14 19:13:44 -0700442 }}},
443 mailrc => sub { my $fh = shift; while (<$fh>) {
444 if (/^alias\s+(\S+)\s+(.*)$/) {
445 # spaces delimit multiple addresses
Eric W. Biedermanfe87c922009-05-20 19:45:53 -0700446 $aliases{$1} = [ quotewords('\s+', 0, $2) ];
Eric Wong994d6c62006-05-14 19:13:44 -0700447 }}},
Trent Piepho73c427e2008-11-25 18:55:00 -0800448 pine => sub { my $fh = shift; my $f='\t[^\t]*';
449 for (my $x = ''; defined($x); $x = $_) {
450 chomp $x;
451 $x .= $1 while(defined($_ = <$fh>) && /^ +(.*)$/);
452 $x =~ /^(\S+)$f\t\(?([^\t]+?)\)?(:?$f){0,2}$/ or next;
Wu Fengguang0e73b3e2008-12-19 16:10:10 +0800453 $aliases{$1} = [ split_addrs($2) ];
Trent Piepho73c427e2008-11-25 18:55:00 -0800454 }},
Bill Pemberton7613ea32009-04-22 09:41:29 -0400455 elm => sub { my $fh = shift;
456 while (<$fh>) {
457 if (/^(\S+)\s+=\s+[^=]+=\s(\S+)/) {
458 my ($alias, $addr) = ($1, $2);
459 $aliases{$alias} = [ split_addrs($addr) ];
460 }
461 } },
462
Eric Wong994d6c62006-05-14 19:13:44 -0700463 gnus => sub { my $fh = shift; while (<$fh>) {
464 if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
465 $aliases{$1} = [ $2 ];
466 }}}
467);
468
Petr Baudis3cb8caf2006-07-03 22:47:58 +0200469if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) {
Eric Wong994d6c62006-05-14 19:13:44 -0700470 foreach my $file (@alias_files) {
471 open my $fh, '<', $file or die "opening $file: $!\n";
472 $parse_alias{$aliasfiletype}->($fh);
473 close $fh;
474 }
475}
476
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200477($sender) = expand_aliases($sender) if defined $sender;
Michael Hendricksae740a52007-07-04 19:11:36 -0600478
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100479# returns 1 if the conflict must be solved using it as a format-patch argument
480sub check_file_rev_conflict($) {
Jay Soffianeed6ca72009-02-14 23:32:13 -0500481 return unless $repo;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100482 my $f = shift;
483 try {
484 $repo->command('rev-parse', '--verify', '--quiet', $f);
485 if (defined($format_patch)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100486 return $format_patch;
487 }
488 die(<<EOF);
489File '$f' exists but it could also be the range of commits
490to produce patches for. Please disambiguate by...
491
492 * Saying "./$f" if you mean a file; or
493 * Giving --format-patch option if you mean a range.
494EOF
495 } catch Git::Error::Command with {
496 return 0;
497 }
498}
499
Jeff Kingaa548922008-01-18 09:19:36 -0500500# Now that all the defaults are set, process the rest of the command line
501# arguments and collect up the files that need to be processed.
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100502my @rev_list_opts;
Junio C Hamano69f4ce52008-11-30 22:38:20 -0800503while (defined(my $f = shift @ARGV)) {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100504 if ($f eq "--") {
505 push @rev_list_opts, "--", @ARGV;
506 @ARGV = ();
507 } elsif (-d $f and !check_file_rev_conflict($f)) {
Jeff Kingaa548922008-01-18 09:19:36 -0500508 opendir(DH,$f)
509 or die "Failed to opendir $f: $!";
510
511 push @files, grep { -f $_ } map { +$f . "/" . $_ }
512 sort readdir(DH);
Pierre Habouzit8c178682008-10-31 18:57:10 +0000513 closedir(DH);
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100514 } elsif ((-f $f or -p $f) and !check_file_rev_conflict($f)) {
Jeff Kingaa548922008-01-18 09:19:36 -0500515 push @files, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500516 } else {
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100517 push @rev_list_opts, $f;
Jeff Kingaa548922008-01-18 09:19:36 -0500518 }
519}
520
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100521if (@rev_list_opts) {
Jay Soffianeed6ca72009-02-14 23:32:13 -0500522 die "Cannot run git format-patch from outside a repository\n"
523 unless $repo;
Pierre Habouzit5df9fcf2008-11-11 00:54:00 +0100524 push @files, $repo->command('format-patch', '-o', tempdir(CLEANUP => 1), @rev_list_opts);
525}
526
Michael Wittendbf5e1e2008-09-30 07:58:27 -0500527if ($validate) {
Jeff Kingc764a0c2008-01-18 09:20:10 -0500528 foreach my $f (@files) {
Kevin Ballard300913b2008-06-25 15:44:40 -0700529 unless (-p $f) {
530 my $error = validate_patch($f);
531 $error and die "fatal: $f: $error\nwarning: no patches were sent\n";
532 }
Jeff Kingc764a0c2008-01-18 09:20:10 -0500533 }
Jeff King747bbff2008-01-18 09:19:48 -0500534}
535
Jeff Kingaa548922008-01-18 09:19:36 -0500536if (@files) {
537 unless ($quiet) {
538 print $_,"\n" for (@files);
539 }
540} else {
541 print STDERR "\nNo patch files specified!\n\n";
542 usage();
543}
544
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100545sub get_patch_subject($) {
546 my $fn = shift;
547 open (my $fh, '<', $fn);
548 while (my $line = <$fh>) {
549 next unless ($line =~ /^Subject: (.*)$/);
550 close $fh;
551 return "GIT: $1\n";
552 }
553 close $fh;
554 die "No subject line in $fn ?";
555}
556
557if ($compose) {
558 # Note that this does not need to be secure, but we will make a small
559 # effort to have it be unique
Jay Soffianafe756c2009-02-23 13:51:37 -0500560 $compose_filename = ($repo ?
561 tempfile(".gitsendemail.msg.XXXXXX", DIR => $repo->repo_path()) :
562 tempfile(".gitsendemail.msg.XXXXXX", DIR => "."))[1];
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100563 open(C,">",$compose_filename)
564 or die "Failed to open for writing $compose_filename: $!";
565
566
567 my $tpl_sender = $sender || $repoauthor || $repocommitter || '';
568 my $tpl_subject = $initial_subject || '';
569 my $tpl_reply_to = $initial_reply_to || '';
570
571 print C <<EOT;
572From $tpl_sender # This line is ignored.
Michael Witten40e6e8a2009-04-13 13:23:50 -0500573GIT: Lines beginning in "GIT:" will be removed.
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100574GIT: Consider including an overall diffstat or table of contents
575GIT: for the patch you are writing.
576GIT:
577GIT: Clear the body content if you don't wish to send a summary.
578From: $tpl_sender
579Subject: $tpl_subject
580In-Reply-To: $tpl_reply_to
581
582EOT
583 for my $f (@files) {
584 print C get_patch_subject($f);
585 }
586 close(C);
587
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100588 if ($annotate) {
589 do_edit($compose_filename, @files);
590 } else {
591 do_edit($compose_filename);
592 }
593
594 open(C2,">",$compose_filename . ".final")
595 or die "Failed to open $compose_filename.final : " . $!;
596
597 open(C,"<",$compose_filename)
598 or die "Failed to open $compose_filename : " . $!;
599
600 my $need_8bit_cte = file_has_nonascii($compose_filename);
601 my $in_body = 0;
602 my $summary_empty = 1;
603 while(<C>) {
Michael Witten40e6e8a2009-04-13 13:23:50 -0500604 next if m/^GIT:/;
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100605 if ($in_body) {
606 $summary_empty = 0 unless (/^\n$/);
607 } elsif (/^\n$/) {
608 $in_body = 1;
609 if ($need_8bit_cte) {
610 print C2 "MIME-Version: 1.0\n",
611 "Content-Type: text/plain; ",
Brandon Caseyd1fff6f2009-06-06 20:12:31 -0500612 "charset=UTF-8\n",
Pierre Habouzitbeece9d2008-11-11 00:54:02 +0100613 "Content-Transfer-Encoding: 8bit\n";
614 }
615 } elsif (/^MIME-Version:/i) {
616 $need_8bit_cte = 0;
617 } elsif (/^Subject:\s*(.+)\s*$/i) {
618 $initial_subject = $1;
619 my $subject = $initial_subject;
620 $_ = "Subject: " .
621 ($subject =~ /[^[:ascii:]]/ ?
622 quote_rfc2047($subject) :
623 $subject) .
624 "\n";
625 } elsif (/^In-Reply-To:\s*(.+)\s*$/i) {
626 $initial_reply_to = $1;
627 next;
628 } elsif (/^From:\s*(.+)\s*$/i) {
629 $sender = $1;
630 next;
631 } elsif (/^(?:To|Cc|Bcc):/i) {
632 print "To/Cc/Bcc fields are not interpreted yet, they have been ignored\n";
633 next;
634 }
635 print C2 $_;
636 }
637 close(C);
638 close(C2);
639
640 if ($summary_empty) {
641 print "Summary email is empty, skipping it\n";
642 $compose = -1;
643 }
644} elsif ($annotate) {
645 do_edit(@files);
646}
647
Jay Soffian6e182512009-03-28 21:39:10 -0400648sub ask {
649 my ($prompt, %arg) = @_;
Jay Soffian0da43a62009-04-04 23:23:21 -0400650 my $valid_re = $arg{valid_re};
Jay Soffian6e182512009-03-28 21:39:10 -0400651 my $default = $arg{default};
652 my $resp;
653 my $i = 0;
Jay Soffian5906f542009-03-31 12:22:11 -0400654 return defined $default ? $default : undef
655 unless defined $term->IN and defined fileno($term->IN) and
656 defined $term->OUT and defined fileno($term->OUT);
Jay Soffian6e182512009-03-28 21:39:10 -0400657 while ($i++ < 10) {
658 $resp = $term->readline($prompt);
659 if (!defined $resp) { # EOF
660 print "\n";
661 return defined $default ? $default : undef;
662 }
663 if ($resp eq '' and defined $default) {
664 return $default;
665 }
Jay Soffian0da43a62009-04-04 23:23:21 -0400666 if (!defined $valid_re or $resp =~ /$valid_re/) {
Jay Soffian6e182512009-03-28 21:39:10 -0400667 return $resp;
668 }
669 }
670 return undef;
671}
672
Ryan Anderson1f038a02005-09-05 01:13:07 -0400673my $prompting = 0;
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200674if (!defined $sender) {
Frank Lichtenheldad79c022008-03-14 18:29:30 +0100675 $sender = $repoauthor || $repocommitter || '';
Jay Soffian6e182512009-03-28 21:39:10 -0400676 $sender = ask("Who should the emails appear to be from? [$sender] ",
677 default => $sender);
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200678 print "Emails will be sent from: ", $sender, "\n";
Ryan Anderson1f038a02005-09-05 01:13:07 -0400679 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -0400680}
681
682if (!@to) {
Jay Soffian6e182512009-03-28 21:39:10 -0400683 my $to = ask("Who should the emails be sent to? ");
684 push @to, parse_address_line($to) if defined $to; # sanitized/validated later
Ryan Anderson1f038a02005-09-05 01:13:07 -0400685 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -0400686}
687
Eric Wong994d6c62006-05-14 19:13:44 -0700688sub expand_aliases {
Jeff King302e04e2009-07-23 07:09:29 -0400689 return map { expand_one_alias($_) } @_;
690}
691
692my %EXPANDED_ALIASES;
693sub expand_one_alias {
694 my $alias = shift;
695 if ($EXPANDED_ALIASES{$alias}) {
696 die "fatal: alias '$alias' expands to itself\n";
697 }
698 local $EXPANDED_ALIASES{$alias} = 1;
699 return $aliases{$alias} ? expand_aliases(@{$aliases{$alias}}) : $alias;
Eric Wong994d6c62006-05-14 19:13:44 -0700700}
701
702@to = expand_aliases(@to);
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +0200703@to = (map { sanitize_address($_) } @to);
Eric Wong994d6c62006-05-14 19:13:44 -0700704@initial_cc = expand_aliases(@initial_cc);
Ryan Anderson58063242006-05-29 12:30:13 -0700705@bcclist = expand_aliases(@bcclist);
Eric Wong994d6c62006-05-14 19:13:44 -0700706
Adam Roben5483c712007-06-27 20:59:37 -0700707if ($thread && !defined $initial_reply_to && $prompting) {
Jay Soffian6e182512009-03-28 21:39:10 -0400708 $initial_reply_to = ask(
709 "Message-ID to be used as In-Reply-To for the first email? ");
Ryan Anderson83b24432005-07-31 04:17:25 -0400710}
Jay Soffian1ca3d6e2008-02-20 00:55:07 -0500711if (defined $initial_reply_to) {
Jay Soffian0fb7fc72008-02-21 19:16:04 -0500712 $initial_reply_to =~ s/^\s*<?//;
713 $initial_reply_to =~ s/>?\s*$//;
714 $initial_reply_to = "<$initial_reply_to>" if $initial_reply_to ne '';
Junio C Hamanoace9c2a2007-12-10 21:44:42 -0800715}
Mike Hommeyace72082007-12-09 18:17:28 +0100716
Douglas Stockwell34cc60c2007-09-03 03:06:25 +0900717if (!defined $smtp_server) {
Eric Wongaca7ad72006-05-15 02:34:44 -0700718 foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) {
719 if (-x $_) {
720 $smtp_server = $_;
721 last;
722 }
723 }
724 $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
Ryan Anderson3342d852005-07-31 20:04:24 -0400725}
726
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500727if ($compose && $compose > 0) {
728 @files = ($compose_filename . ".final", @files);
Ryan Anderson1f038a02005-09-05 01:13:07 -0400729}
730
Ryan Anderson83b24432005-07-31 04:17:25 -0400731# Variables we set as part of the loop over files
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500732our ($message_id, %mail, $subject, $reply_to, $references, $message,
Jay Soffiandc1460a2009-03-31 12:22:12 -0400733 $needs_confirm, $message_num, $ask_default);
Ryan Anderson83b24432005-07-31 04:17:25 -0400734
Eric Wong567ffeb2006-03-25 16:47:12 -0800735sub extract_valid_address {
736 my $address = shift;
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700737 my $local_part_regexp = '[^<>"\s@]+';
Junio C Hamano09302e12006-06-06 14:12:46 -0700738 my $domain_regexp = '[^.<>"\s@]+(?:\.[^.<>"\s@]+)+';
Eric Wongdb3106b2006-05-15 02:41:01 -0700739
740 # check for a local address:
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700741 return $address if ($address =~ /^($local_part_regexp)$/);
Eric Wongdb3106b2006-05-15 02:41:01 -0700742
Uwe Kleine-König155197e2007-08-09 15:27:57 +0200743 $address =~ s/^\s*<(.*)>\s*$/$1/;
Eric Wong567ffeb2006-03-25 16:47:12 -0800744 if ($have_email_valid) {
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700745 return scalar Email::Valid->address($address);
Eric Wong567ffeb2006-03-25 16:47:12 -0800746 } else {
747 # less robust/correct than the monster regexp in Email::Valid,
748 # but still does a 99% job, and one less dependency
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700749 $address =~ /($local_part_regexp\@$domain_regexp)/;
Horst H. von Brande96fd302006-06-03 13:11:48 -0400750 return $1;
Eric Wong567ffeb2006-03-25 16:47:12 -0800751 }
752}
Ryan Anderson83b24432005-07-31 04:17:25 -0400753
754# Usually don't need to change anything below here.
755
756# we make a "fake" message id by taking the current number
757# of seconds since the beginning of Unix time and tacking on
758# a random number to the end, in case we are called quicker than
759# 1 second since the last time we were called.
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400760
761# We'll setup a template for the message id, using the "from" address:
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400762
Junio C Hamanobe510cf2007-09-17 21:18:20 -0700763my ($message_id_stamp, $message_id_serial);
Ryan Anderson83b24432005-07-31 04:17:25 -0400764sub make_message_id
765{
Junio C Hamanobe510cf2007-09-17 21:18:20 -0700766 my $uniq;
767 if (!defined $message_id_stamp) {
768 $message_id_stamp = sprintf("%s-%s", time, $$);
769 $message_id_serial = 0;
770 }
771 $message_id_serial++;
772 $uniq = "$message_id_stamp-$message_id_serial";
773
Junio C Hamanoaeb59322007-06-20 13:47:34 -0700774 my $du_part;
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200775 for ($sender, $repocommitter, $repoauthor) {
776 $du_part = extract_valid_address(sanitize_address($_));
777 last if (defined $du_part and $du_part ne '');
Junio C Hamanoaeb59322007-06-20 13:47:34 -0700778 }
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200779 if (not defined $du_part or $du_part eq '') {
Junio C Hamanoaeb59322007-06-20 13:47:34 -0700780 use Sys::Hostname qw();
781 $du_part = 'user@' . Sys::Hostname::hostname();
782 }
Junio C Hamanobe510cf2007-09-17 21:18:20 -0700783 my $message_id_template = "<%s-git-send-email-%s>";
784 $message_id = sprintf($message_id_template, $uniq, $du_part);
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400785 #print "new message id = $message_id\n"; # Was useful for debugging
Ryan Anderson83b24432005-07-31 04:17:25 -0400786}
787
788
789
Eric Wonga5370b12006-03-25 03:01:01 -0800790$time = time - scalar $#files;
Ryan Anderson83b24432005-07-31 04:17:25 -0400791
Jürgen Rühle374c5902007-01-10 13:36:39 -0800792sub unquote_rfc2047 {
793 local ($_) = @_;
Jeff King8291db62007-11-16 05:49:09 -0500794 my $encoding;
795 if (s/=\?([^?]+)\?q\?(.*)\?=/$2/g) {
796 $encoding = $1;
Jürgen Rühle374c5902007-01-10 13:36:39 -0800797 s/_/ /g;
798 s/=([0-9A-F]{2})/chr(hex($1))/eg;
799 }
Jeff King8291db62007-11-16 05:49:09 -0500800 return wantarray ? ($_, $encoding) : $_;
Jürgen Rühle374c5902007-01-10 13:36:39 -0800801}
802
Jeff Kingd54eaaa2008-03-28 17:29:01 -0400803sub quote_rfc2047 {
804 local $_ = shift;
Brandon Caseyd1fff6f2009-06-06 20:12:31 -0500805 my $encoding = shift || 'UTF-8';
Jeff Kingd54eaaa2008-03-28 17:29:01 -0400806 s/([^-a-zA-Z0-9!*+\/])/sprintf("=%02X", ord($1))/eg;
807 s/(.*)/=\?$encoding\?q\?$1\?=/;
808 return $_;
809}
810
Brandon Caseya3a82622009-06-07 19:25:58 -0500811sub is_rfc2047_quoted {
812 my $s = shift;
813 my $token = '[^][()<>@,;:"\/?.= \000-\037\177-\377]+';
814 my $encoded_text = '[!->@-~]+';
815 length($s) <= 75 &&
816 $s =~ m/^(?:"[[:ascii:]]*"|=\?$token\?$token\?$encoded_text\?=)$/o;
817}
818
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +0200819# use the simplest quoting being able to handle the recipient
820sub sanitize_address
Robin H. Johnson732263d2007-04-25 19:37:19 -0700821{
822 my ($recipient) = @_;
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +0200823 my ($recipient_name, $recipient_addr) = ($recipient =~ /^(.*?)\s*(<.*)/);
824
825 if (not $recipient_name) {
826 return "$recipient";
Robin H. Johnson732263d2007-04-25 19:37:19 -0700827 }
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +0200828
829 # if recipient_name is already quoted, do nothing
Brandon Caseya3a82622009-06-07 19:25:58 -0500830 if (is_rfc2047_quoted($recipient_name)) {
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +0200831 return $recipient;
832 }
833
834 # rfc2047 is needed if a non-ascii char is included
835 if ($recipient_name =~ /[^[:ascii:]]/) {
Jay Soffiana61c0ff2009-03-31 12:22:14 -0400836 $recipient_name =~ s/^"(.*)"$/$1/;
Jeff Kingd54eaaa2008-03-28 17:29:01 -0400837 $recipient_name = quote_rfc2047($recipient_name);
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +0200838 }
839
840 # double quotes are needed if specials or CTLs are included
841 elsif ($recipient_name =~ /[][()<>@,;:\\".\000-\037\177]/) {
Horst H. von Brand18023c22008-03-28 11:09:04 -0300842 $recipient_name =~ s/(["\\\r])/\\$1/g;
Uwe Kleine-K,Av(Bnig5b56aaa2007-08-06 22:34:50 +0200843 $recipient_name = "\"$recipient_name\"";
844 }
845
846 return "$recipient_name $recipient_addr";
847
Robin H. Johnson732263d2007-04-25 19:37:19 -0700848}
849
Jari Aalto134550f2010-03-14 17:16:45 +0200850# Returns the local Fully Qualified Domain Name (FQDN) if available.
851#
852# Tightly configured MTAa require that a caller sends a real DNS
853# domain name that corresponds the IP address in the HELO/EHLO
854# handshake. This is used to verify the connection and prevent
855# spammers from trying to hide their identity. If the DNS and IP don't
856# match, the receiveing MTA may deny the connection.
857#
858# Here is a deny example of Net::SMTP with the default "localhost.localdomain"
859#
860# Net::SMTP=GLOB(0x267ec28)>>> EHLO localhost.localdomain
861# Net::SMTP=GLOB(0x267ec28)<<< 550 EHLO argument does not match calling host
862#
863# This maildomain*() code is based on ideas in Perl library Test::Reporter
864# /usr/share/perl5/Test/Reporter/Mail/Util.pm ==> sub _maildomain ()
865
866sub maildomain_net
867{
868 my $maildomain;
869
870 if (eval { require Net::Domain; 1 }) {
871 my $domain = Net::Domain::domainname();
872 $maildomain = $domain
873 unless $^O eq 'darwin' && $domain =~ /\.local$/;
874 }
875
876 return $maildomain;
877}
878
879sub maildomain_mta
880{
881 my $maildomain;
882
883 if (eval { require Net::SMTP; 1 }) {
884 for my $host (qw(mailhost localhost)) {
885 my $smtp = Net::SMTP->new($host);
886 if (defined $smtp) {
887 my $domain = $smtp->domain;
888 $smtp->quit;
889
890 $maildomain = $domain
891 unless $^O eq 'darwin' && $domain =~ /\.local$/;
892
893 last if $maildomain;
894 }
895 }
896 }
897
898 return $maildomain;
899}
900
901sub maildomain
902{
903 return maildomain_net() || maildomain_mta() || $mail_domain_default;
904}
905
Michael Witten15da1082009-04-13 13:23:51 -0500906# Returns 1 if the message was sent, and 0 otherwise.
Markus Heidelberga1b5b372009-06-12 12:51:42 +0200907# In actuality, the whole program dies when there
Michael Witten15da1082009-04-13 13:23:51 -0500908# is an error sending a message.
909
Ryan Anderson83b24432005-07-31 04:17:25 -0400910sub send_message
911{
Eric Wong4bc87a22006-03-25 17:20:48 -0800912 my @recipients = unique_email_list(@to);
Ask Bjørn Hansen7ac17522007-11-19 03:00:26 -0800913 @cc = (grep { my $cc = extract_valid_address($_);
914 not grep { $cc eq $_ } @recipients
915 }
916 map { sanitize_address($_) }
917 @cc);
Eric Wong4bc87a22006-03-25 17:20:48 -0800918 my $to = join (",\n\t", @recipients);
Ryan Anderson58063242006-05-29 12:30:13 -0700919 @recipients = unique_email_list(@recipients,@cc,@bcclist);
Robin H. Johnsonc38f0242007-04-25 19:37:20 -0700920 @recipients = (map { extract_valid_address($_) } @recipients);
Jakub Narebski6bdca892006-07-07 20:57:55 +0200921 my $date = format_2822_time($time++);
Martin Langhoffe923eff2006-05-03 09:44:36 +1200922 my $gitversion = '@@GIT_VERSION@@';
923 if ($gitversion =~ m/..GIT_VERSION../) {
Petr Baudis3cb8caf2006-07-03 22:47:58 +0200924 $gitversion = Git::version();
Martin Langhoffe923eff2006-05-03 09:44:36 +1200925 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400926
Joe Perches02461e02009-10-08 10:03:26 -0700927 my $cc = join(",\n\t", unique_email_list(@cc));
Junio C Hamanof06a6a42007-04-16 16:51:47 -0700928 my $ccline = "";
929 if ($cc ne '') {
930 $ccline = "\nCc: $cc";
931 }
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200932 my $sanitized_sender = sanitize_address($sender);
Jeff King4f3d3702007-12-17 15:51:34 -0500933 make_message_id() unless defined($message_id);
Junio C Hamanoaeb59322007-06-20 13:47:34 -0700934
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200935 my $header = "From: $sanitized_sender
Junio C Hamanof06a6a42007-04-16 16:51:47 -0700936To: $to${ccline}
Eric Wong4bc87a22006-03-25 17:20:48 -0800937Subject: $subject
Eric Wong4bc87a22006-03-25 17:20:48 -0800938Date: $date
939Message-Id: $message_id
Martin Langhoffe923eff2006-05-03 09:44:36 +1200940X-Mailer: git-send-email $gitversion
Eric Wong4bc87a22006-03-25 17:20:48 -0800941";
Thomas Rast3e0c4ff2009-03-01 23:45:41 +0100942 if ($reply_to) {
Ryan Anderson7ccf7922006-05-29 12:30:12 -0700943
944 $header .= "In-Reply-To: $reply_to\n";
945 $header .= "References: $references\n";
946 }
Junio C Hamanoce91c2f2006-10-05 16:36:49 -0700947 if (@xh) {
948 $header .= join("\n", @xh) . "\n";
949 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400950
Robin H. Johnsonc38f0242007-04-25 19:37:20 -0700951 my @sendmail_parameters = ('-i', @recipients);
Uwe Kleine-König94638f82007-08-09 15:27:58 +0200952 my $raw_from = $sanitized_sender;
Felipe Contrerasc89e3242009-11-26 21:04:29 +0200953 if (defined $envelope_sender && $envelope_sender ne "auto") {
954 $raw_from = $envelope_sender;
955 }
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700956 $raw_from = extract_valid_address($raw_from);
957 unshift (@sendmail_parameters,
958 '-f', $raw_from) if(defined $envelope_sender);
Robin H. Johnson8e3d4362007-04-25 19:37:17 -0700959
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500960 if ($needs_confirm && !$dry_run) {
961 print "\n$header\n";
962 if ($needs_confirm eq "inform") {
963 $confirm_unconfigured = 0; # squelch this message for the rest of this run
Jay Soffian6e182512009-03-28 21:39:10 -0400964 $ask_default = "y"; # assume yes on EOF since user hasn't explicitly asked for confirmation
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500965 print " The Cc list above has been expanded by additional\n";
966 print " addresses found in the patch commit message. By default\n";
967 print " send-email prompts before sending whenever this occurs.\n";
968 print " This behavior is controlled by the sendemail.confirm\n";
969 print " configuration setting.\n";
970 print "\n";
971 print " For additional information, run 'git send-email --help'.\n";
972 print " To retain the current behavior, but squelch this message,\n";
973 print " run 'git config --global sendemail.confirm auto'.\n\n";
974 }
Jay Soffian6e182512009-03-28 21:39:10 -0400975 $_ = ask("Send this email? ([y]es|[n]o|[q]uit|[a]ll): ",
976 valid_re => qr/^(?:yes|y|no|n|quit|q|all|a)/i,
977 default => $ask_default);
978 die "Send this email reply required" unless defined $_;
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500979 if (/^n/i) {
Michael Witten15da1082009-04-13 13:23:51 -0500980 return 0;
Jay Soffianc1f2aa42009-03-02 23:52:18 -0500981 } elsif (/^q/i) {
982 cleanup_compose_files();
983 exit(0);
984 } elsif (/^a/i) {
985 $confirm = 'never';
986 }
987 }
988
Matthew Wilcox61302592006-10-10 08:58:23 -0600989 if ($dry_run) {
990 # We don't want to send the email.
991 } elsif ($smtp_server =~ m#^/#) {
Eric Wongaca7ad72006-05-15 02:34:44 -0700992 my $pid = open my $sm, '|-';
993 defined $pid or die $!;
994 if (!$pid) {
Robin H. Johnson8e3d4362007-04-25 19:37:17 -0700995 exec($smtp_server, @sendmail_parameters) or die $!;
Eric Wongaca7ad72006-05-15 02:34:44 -0700996 }
997 print $sm "$header\n$message";
998 close $sm or die $?;
999 } else {
Junio C Hamano44b24762007-09-25 17:27:54 -07001000
1001 if (!defined $smtp_server) {
1002 die "The required SMTP server is not properly defined."
1003 }
1004
Thomas Rastf6bebd12008-06-25 21:42:43 +02001005 if ($smtp_encryption eq 'ssl') {
Junio C Hamano44b24762007-09-25 17:27:54 -07001006 $smtp_server_port ||= 465; # ssmtp
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001007 require Net::SMTP::SSL;
Jari Aalto134550f2010-03-14 17:16:45 +02001008 $mail_domain ||= maildomain();
1009 $smtp ||= Net::SMTP::SSL->new($smtp_server,
1010 Hello => $mail_domain,
1011 Port => $smtp_server_port);
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001012 }
1013 else {
1014 require Net::SMTP;
Jari Aalto134550f2010-03-14 17:16:45 +02001015 $mail_domain ||= maildomain();
Junio C Hamano44b24762007-09-25 17:27:54 -07001016 $smtp ||= Net::SMTP->new((defined $smtp_server_port)
1017 ? "$smtp_server:$smtp_server_port"
Jari Aaltof60812e2010-03-14 17:16:09 +02001018 : $smtp_server,
Jari Aalto134550f2010-03-14 17:16:45 +02001019 Hello => $mail_domain,
Jari Aaltof60812e2010-03-14 17:16:09 +02001020 Debug => $debug_net_smtp);
Yakov Lernerfb3650e2009-09-25 15:10:21 -07001021 if ($smtp_encryption eq 'tls' && $smtp) {
Thomas Rastf6bebd12008-06-25 21:42:43 +02001022 require Net::SMTP::SSL;
1023 $smtp->command('STARTTLS');
1024 $smtp->response();
1025 if ($smtp->code == 220) {
1026 $smtp = Net::SMTP::SSL->start_SSL($smtp)
1027 or die "STARTTLS failed! ".$smtp->message;
Thomas Rast6cbf8b02008-07-03 00:11:31 +02001028 $smtp_encryption = '';
Robert Shearman9d1ccf52008-07-09 22:39:40 +01001029 # Send EHLO again to receive fresh
1030 # supported commands
1031 $smtp->hello();
Thomas Rastf6bebd12008-06-25 21:42:43 +02001032 } else {
1033 die "Server does not support STARTTLS! ".$smtp->message;
1034 }
1035 }
Douglas Stockwell34cc60c2007-09-03 03:06:25 +09001036 }
Junio C Hamano44b24762007-09-25 17:27:54 -07001037
1038 if (!$smtp) {
Jari Aaltof60812e2010-03-14 17:16:09 +02001039 die "Unable to initialize SMTP properly. Check config and use --smtp-debug. ",
Jari Aaltoe5afb3a2010-03-14 17:15:33 +02001040 "VALUES: server=$smtp_server ",
1041 "encryption=$smtp_encryption ",
Jari Aalto134550f2010-03-14 17:16:45 +02001042 "maildomain=$mail_domain",
Jari Aaltoe5afb3a2010-03-14 17:15:33 +02001043 defined $smtp_server_port ? "port=$smtp_server_port" : "";
Junio C Hamano44b24762007-09-25 17:27:54 -07001044 }
1045
Michael Witten2363d742008-02-03 19:53:56 -05001046 if (defined $smtp_authuser) {
1047
1048 if (!defined $smtp_authpass) {
1049
1050 system "stty -echo";
1051
1052 do {
1053 print "Password: ";
1054 $_ = <STDIN>;
1055 print "\n";
1056 } while (!defined $_);
1057
1058 chomp($smtp_authpass = $_);
1059
1060 system "stty echo";
1061 }
1062
Wincent Colaiuta5f5b6112007-11-21 13:35:05 +01001063 $auth ||= $smtp->auth( $smtp_authuser, $smtp_authpass ) or die $smtp->message;
Junio C Hamano44b24762007-09-25 17:27:54 -07001064 }
Michael Witten2363d742008-02-03 19:53:56 -05001065
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001066 $smtp->mail( $raw_from ) or die $smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001067 $smtp->to( @recipients ) or die $smtp->message;
1068 $smtp->data or die $smtp->message;
1069 $smtp->datasend("$header\n$message") or die $smtp->message;
1070 $smtp->dataend() or die $smtp->message;
Michael Witten15da1082009-04-13 13:23:51 -05001071 $smtp->code =~ /250|200/ or die "Failed to send $subject\n".$smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -07001072 }
Ryan Anderson27184352006-02-05 20:13:52 -05001073 if ($quiet) {
Robin H. Johnson71c7da92007-04-25 19:37:16 -07001074 printf (($dry_run ? "Dry-" : "")."Sent %s\n", $subject);
Ryan Anderson27184352006-02-05 20:13:52 -05001075 } else {
David D. Kilzerb7f30e02007-11-18 20:14:55 -08001076 print (($dry_run ? "Dry-" : "")."OK. Log says:\n");
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001077 if ($smtp_server !~ m#^/#) {
Eric Wongaca7ad72006-05-15 02:34:44 -07001078 print "Server: $smtp_server\n";
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -07001079 print "MAIL FROM:<$raw_from>\n";
Joe Perches02461e02009-10-08 10:03:26 -07001080 foreach my $entry (@recipients) {
1081 print "RCPT TO:<$entry>\n";
1082 }
Eric Wongaca7ad72006-05-15 02:34:44 -07001083 } else {
Robin H. Johnson8e3d4362007-04-25 19:37:17 -07001084 print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001085 }
David D. Kilzerb7f30e02007-11-18 20:14:55 -08001086 print $header, "\n";
Eric Wongaca7ad72006-05-15 02:34:44 -07001087 if ($smtp) {
1088 print "Result: ", $smtp->code, ' ',
1089 ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
1090 } else {
1091 print "Result: OK\n";
1092 }
Ryan Anderson30d08b32006-02-02 11:56:06 -05001093 }
Michael Witten15da1082009-04-13 13:23:51 -05001094
1095 return 1;
Ryan Anderson83b24432005-07-31 04:17:25 -04001096}
1097
Ryan Anderson83b24432005-07-31 04:17:25 -04001098$reply_to = $initial_reply_to;
Junio C Hamano2186d562006-05-29 23:53:13 -07001099$references = $initial_reply_to || '';
Ryan Anderson83b24432005-07-31 04:17:25 -04001100$subject = $initial_subject;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001101$message_num = 0;
Ryan Anderson83b24432005-07-31 04:17:25 -04001102
1103foreach my $t (@files) {
Ryan Anderson83b24432005-07-31 04:17:25 -04001104 open(F,"<",$t) or die "can't open file $t";
1105
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001106 my $author = undef;
Jeff King8291db62007-11-16 05:49:09 -05001107 my $author_encoding;
1108 my $has_content_type;
1109 my $body_encoding;
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001110 @cc = ();
Junio C Hamanoce91c2f2006-10-05 16:36:49 -07001111 @xh = ();
Junio C Hamanoe6b09642006-10-07 03:09:05 -07001112 my $input_format = undef;
Jay Soffian50126992009-02-14 23:32:14 -05001113 my @header = ();
Ryan Anderson83b24432005-07-31 04:17:25 -04001114 $message = "";
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001115 $message_num++;
Jay Soffian50126992009-02-14 23:32:14 -05001116 # First unfold multiline header fields
Ryan Anderson83b24432005-07-31 04:17:25 -04001117 while(<F>) {
Jay Soffian50126992009-02-14 23:32:14 -05001118 last if /^\s*$/;
1119 if (/^\s+\S/ and @header) {
1120 chomp($header[$#header]);
1121 s/^\s+/ /;
1122 $header[$#header] .= $_;
1123 } else {
1124 push(@header, $_);
1125 }
1126 }
1127 # Now parse the header
1128 foreach(@header) {
1129 if (/^From /) {
1130 $input_format = 'mbox';
1131 next;
1132 }
1133 chomp;
1134 if (!defined $input_format && /^[-A-Za-z]+:\s/) {
1135 $input_format = 'mbox';
1136 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001137
Jay Soffian50126992009-02-14 23:32:14 -05001138 if (defined $input_format && $input_format eq 'mbox') {
1139 if (/^Subject:\s+(.*)$/) {
1140 $subject = $1;
1141 }
1142 elsif (/^From:\s+(.*)$/) {
1143 ($author, $author_encoding) = unquote_rfc2047($1);
1144 next if $suppress_cc{'author'};
1145 next if $suppress_cc{'self'} and $author eq $sender;
1146 printf("(mbox) Adding cc: %s from line '%s'\n",
1147 $1, $_) unless $quiet;
1148 push @cc, $1;
1149 }
1150 elsif (/^Cc:\s+(.*)$/) {
1151 foreach my $addr (parse_address_line($1)) {
1152 if (unquote_rfc2047($addr) eq $sender) {
David Brown65648282007-12-25 19:56:29 -08001153 next if ($suppress_cc{'self'});
David Brown65648282007-12-25 19:56:29 -08001154 } else {
1155 next if ($suppress_cc{'cc'});
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001156 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001157 printf("(mbox) Adding cc: %s from line '%s'\n",
Jay Soffian50126992009-02-14 23:32:14 -05001158 $addr, $_) unless $quiet;
1159 push @cc, $addr;
Ryan Anderson83b24432005-07-31 04:17:25 -04001160 }
1161 }
Jay Soffian50126992009-02-14 23:32:14 -05001162 elsif (/^Content-type:/i) {
1163 $has_content_type = 1;
1164 if (/charset="?([^ "]+)/) {
1165 $body_encoding = $1;
1166 }
1167 push @xh, $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001168 }
Jay Soffian50126992009-02-14 23:32:14 -05001169 elsif (/^Message-Id: (.*)/i) {
1170 $message_id = $1;
1171 }
1172 elsif (!/^Date:\s/ && /^[-A-Za-z]+:\s+\S/) {
1173 push @xh, $_;
1174 }
1175
Ryan Anderson83b24432005-07-31 04:17:25 -04001176 } else {
Jay Soffian50126992009-02-14 23:32:14 -05001177 # In the traditional
1178 # "send lots of email" format,
1179 # line 1 = cc
1180 # line 2 = subject
1181 # So let's support that, too.
1182 $input_format = 'lots';
1183 if (@cc == 0 && !$suppress_cc{'cc'}) {
1184 printf("(non-mbox) Adding cc: %s from line '%s'\n",
1185 $_, $_) unless $quiet;
1186 push @cc, $_;
1187 } elsif (!defined $subject) {
1188 $subject = $_;
Ryan Anderson83b24432005-07-31 04:17:25 -04001189 }
1190 }
1191 }
Jay Soffian50126992009-02-14 23:32:14 -05001192 # Now parse the message body
1193 while(<F>) {
1194 $message .= $_;
1195 if (/^(Signed-off-by|Cc): (.*)$/i) {
Jay Soffian50126992009-02-14 23:32:14 -05001196 chomp;
Jay Soffian3531e272009-02-14 23:32:15 -05001197 my ($what, $c) = ($1, $2);
Jay Soffian50126992009-02-14 23:32:14 -05001198 chomp $c;
Jay Soffian3531e272009-02-14 23:32:15 -05001199 if ($c eq $sender) {
1200 next if ($suppress_cc{'self'});
1201 } else {
1202 next if $suppress_cc{'sob'} and $what =~ /Signed-off-by/i;
1203 next if $suppress_cc{'bodycc'} and $what =~ /Cc/i;
1204 }
Jay Soffian50126992009-02-14 23:32:14 -05001205 push @cc, $c;
Jay Soffian3531e272009-02-14 23:32:15 -05001206 printf("(body) Adding cc: %s from line '%s'\n",
Jay Soffian50126992009-02-14 23:32:14 -05001207 $c, $_) unless $quiet;
1208 }
1209 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001210 close F;
Joe Perches324a8bd2007-08-17 18:51:12 -07001211
David Brown65648282007-12-25 19:56:29 -08001212 if (defined $cc_cmd && !$suppress_cc{'cccmd'}) {
Paolo Bonzinicb8a9bd2009-06-18 14:31:32 +02001213 open(F, "$cc_cmd \Q$t\E |")
Joe Perches324a8bd2007-08-17 18:51:12 -07001214 or die "(cc-cmd) Could not execute '$cc_cmd'";
1215 while(<F>) {
1216 my $c = $_;
1217 $c =~ s/^\s*//g;
1218 $c =~ s/\n$//g;
Uwe Kleine-König620bb242007-11-07 08:34:12 +01001219 next if ($c eq $sender and $suppress_from);
Joe Perches324a8bd2007-08-17 18:51:12 -07001220 push @cc, $c;
1221 printf("(cc-cmd) Adding cc: %s from: '%s'\n",
1222 $c, $cc_cmd) unless $quiet;
1223 }
1224 close F
1225 or die "(cc-cmd) failed to close pipe to '$cc_cmd'";
1226 }
1227
Jay Soffian50126992009-02-14 23:32:14 -05001228 if (defined $author and $author ne $sender) {
Uwe Kleine-König94638f82007-08-09 15:27:58 +02001229 $message = "From: $author\n\n$message";
Jeff King8291db62007-11-16 05:49:09 -05001230 if (defined $author_encoding) {
1231 if ($has_content_type) {
1232 if ($body_encoding eq $author_encoding) {
1233 # ok, we already have the right encoding
1234 }
1235 else {
1236 # uh oh, we should re-encode
1237 }
1238 }
1239 else {
1240 push @xh,
1241 'MIME-Version: 1.0',
Jeff King8641ee32007-11-20 07:54:04 -05001242 "Content-Type: text/plain; charset=$author_encoding",
1243 'Content-Transfer-Encoding: 8bit';
Jeff King8291db62007-11-16 05:49:09 -05001244 }
1245 }
Junio C Hamano8a8e6232006-03-23 23:43:52 -08001246 }
Ryan Anderson83b24432005-07-31 04:17:25 -04001247
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001248 $needs_confirm = (
1249 $confirm eq "always" or
1250 ($confirm =~ /^(?:auto|cc)$/ && @cc) or
1251 ($confirm =~ /^(?:auto|compose)$/ && $compose && $message_num == 1));
1252 $needs_confirm = "inform" if ($needs_confirm && $confirm_unconfigured && @cc);
1253
1254 @cc = (@initial_cc, @cc);
1255
Michael Witten15da1082009-04-13 13:23:51 -05001256 my $message_was_sent = send_message();
Ryan Anderson83b24432005-07-31 04:17:25 -04001257
1258 # set up for the next message
Junio C Hamano95a877a2009-06-12 09:23:43 -07001259 if ($thread && $message_was_sent &&
Nanako Shiraishi528fb082009-11-29 12:24:48 +09001260 (chain_reply_to() || !defined $reply_to || length($reply_to) == 0)) {
Ryan Anderson78488b22005-07-31 20:04:24 -04001261 $reply_to = $message_id;
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001262 if (length $references > 0) {
YOSHIFUJI Hideaki / 吉藤英明a925b892007-04-06 08:50:24 +09001263 $references .= "\n $message_id";
Ryan Anderson7ccf7922006-05-29 12:30:12 -07001264 } else {
1265 $references = "$message_id";
1266 }
Ryan Anderson78488b22005-07-31 20:04:24 -04001267 }
Jeff King4f3d3702007-12-17 15:51:34 -05001268 $message_id = undef;
Ryan Anderson83b24432005-07-31 04:17:25 -04001269}
Ryan Andersone2057352005-08-02 21:45:22 -04001270
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001271cleanup_compose_files();
Ryan Anderson1f038a02005-09-05 01:13:07 -04001272
1273sub cleanup_compose_files() {
Jay Soffianc1f2aa42009-03-02 23:52:18 -05001274 unlink($compose_filename, $compose_filename . ".final") if $compose;
Ryan Anderson1f038a02005-09-05 01:13:07 -04001275}
1276
Eric Wong4bc87a22006-03-25 17:20:48 -08001277$smtp->quit if $smtp;
Ryan Andersone2057352005-08-02 21:45:22 -04001278
1279sub unique_email_list(@) {
1280 my %seen;
1281 my @emails;
1282
1283 foreach my $entry (@_) {
Eric Wongdb3106b2006-05-15 02:41:01 -07001284 if (my $clean = extract_valid_address($entry)) {
1285 $seen{$clean} ||= 0;
1286 next if $seen{$clean}++;
1287 push @emails, $entry;
1288 } else {
1289 print STDERR "W: unable to extract a valid address",
1290 " from: $entry\n";
1291 }
Ryan Andersone2057352005-08-02 21:45:22 -04001292 }
1293 return @emails;
1294}
Jeff King747bbff2008-01-18 09:19:48 -05001295
1296sub validate_patch {
1297 my $fn = shift;
1298 open(my $fh, '<', $fn)
1299 or die "unable to open $fn: $!\n";
1300 while (my $line = <$fh>) {
1301 if (length($line) > 998) {
1302 return "$.: patch contains a line longer than 998 characters";
1303 }
1304 }
1305 return undef;
1306}
Jeff King0706bd12008-03-28 17:28:33 -04001307
1308sub file_has_nonascii {
1309 my $fn = shift;
1310 open(my $fh, '<', $fn)
1311 or die "unable to open $fn: $!\n";
1312 while (my $line = <$fh>) {
1313 return 1 if $line =~ /[^[:ascii:]]/;
1314 }
1315 return 0;
1316}