]>
Commit | Line | Data |
---|---|---|
83b24437 | 1 | #!/usr/bin/perl -w |
83b24437 | 2 | # |
f3d9f354 RA |
3 | # Copyright 2002,2005 Greg Kroah-Hartman <greg@kroah.com> |
4 | # Copyright 2005 Ryan Anderson <ryan@michonline.com> | |
83b24437 RA |
5 | # |
6 | # GPL v2 (See COPYING) | |
5825e5b2 | 7 | # |
83b24437 RA |
8 | # Ported to support git "mbox" format files by Ryan Anderson <ryan@michonline.com> |
9 | # | |
f3d9f354 | 10 | # Sends a collection of emails to the given email addresses, disturbingly fast. |
5825e5b2 | 11 | # |
f3d9f354 RA |
12 | # 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: | |
5825e5b2 | 15 | # first line of the message is who to CC, |
f3d9f354 | 16 | # and second line is the subject of the message. |
5825e5b2 | 17 | # |
83b24437 RA |
18 | |
19 | use strict; | |
20 | use warnings; | |
21 | use Term::ReadLine; | |
83b24437 | 22 | use Getopt::Long; |
0e73b3ee | 23 | use Text::ParseWords; |
83b24437 | 24 | use Data::Dumper; |
412876dc | 25 | use Term::ANSIColor; |
eed6ca7c | 26 | use File::Temp qw/ tempdir tempfile /; |
5df9fcf6 | 27 | use Error qw(:try); |
3cb8caf7 | 28 | use Git; |
83b24437 | 29 | |
5df9fcf6 PH |
30 | Getopt::Long::Configure qw/ pass_through /; |
31 | ||
280242d1 JH |
32 | package FakeTerm; |
33 | sub new { | |
34 | my ($class, $reason) = @_; | |
35 | return bless \$reason, shift; | |
36 | } | |
37 | sub readline { | |
38 | my $self = shift; | |
39 | die "Cannot use readline on FakeTerm: $$self"; | |
40 | } | |
41 | package main; | |
42 | ||
1b0baf14 MC |
43 | |
44 | sub usage { | |
45 | print <<EOT; | |
5df9fcf6 | 46 | git send-email [options] <file | directory | rev-list options > |
4ed62b03 MW |
47 | |
48 | Composing: | |
49 | --from <str> * Email From: | |
f434c083 SB |
50 | --[no-]to <str> * Email To: |
51 | --[no-]cc <str> * Email Cc: | |
52 | --[no-]bcc <str> * Email Bcc: | |
4ed62b03 MW |
53 | --subject <str> * Email "Subject:" |
54 | --in-reply-to <str> * Email "In-Reply-To:" | |
8fd5bb7f | 55 | --annotate * Review each patch that will be sent in an editor. |
4ed62b03 | 56 | --compose * Open an editor for introduction. |
3cae7e5b | 57 | --8bit-encoding <str> * Encoding to assume 8bit mails if undeclared |
4ed62b03 MW |
58 | |
59 | Sending: | |
60 | --envelope-sender <str> * Email envelope sender. | |
61 | --smtp-server <str:int> * Outgoing SMTP server to use. The port | |
62 | is optional. Default 'localhost'. | |
63 | --smtp-server-port <int> * Outgoing SMTP server port. | |
64 | --smtp-user <str> * Username for SMTP-AUTH. | |
65 | --smtp-pass <str> * Password for SMTP-AUTH; not necessary. | |
66 | --smtp-encryption <str> * tls or ssl; anything else disables. | |
67 | --smtp-ssl * Deprecated. Use '--smtp-encryption ssl'. | |
134550fe | 68 | --smtp-domain <str> * The domain name sent to HELO/EHLO handshake |
f60812ef | 69 | --smtp-debug <0|1> * Disable, enable Net::SMTP debug. |
4ed62b03 MW |
70 | |
71 | Automating: | |
72 | --identity <str> * Use the sendemail.<id> options. | |
73 | --cc-cmd <str> * Email Cc: via `<str> \$patch_path` | |
3531e270 JS |
74 | --suppress-cc <str> * author, self, sob, cc, cccmd, body, bodycc, all. |
75 | --[no-]signed-off-by-cc * Send to Signed-off-by: addresses. Default on. | |
4ed62b03 | 76 | --[no-]suppress-from * Send to self. Default off. |
41fe87fa | 77 | --[no-]chain-reply-to * Chain In-Reply-To: fields. Default off. |
4ed62b03 MW |
78 | --[no-]thread * Use In-Reply-To: field. Default on. |
79 | ||
80 | Administering: | |
c1f2aa45 JS |
81 | --confirm <str> * Confirm recipients before sending; |
82 | auto, cc, compose, always, or never. | |
4ed62b03 MW |
83 | --quiet * Output one line of info per email. |
84 | --dry-run * Don't actually send the emails. | |
85 | --[no-]validate * Perform patch sanity checks. Default on. | |
5df9fcf6 PH |
86 | --[no-]format-patch * understand any non optional arguments as |
87 | `git format-patch` ones. | |
a03bc5b6 | 88 | --force * Send even if safety checks would prevent it. |
c764a0c2 | 89 | |
1b0baf14 MC |
90 | EOT |
91 | exit(1); | |
92 | } | |
93 | ||
4bc87a28 | 94 | # most mail servers generate the Date: header, but not all... |
6bdca890 JN |
95 | sub format_2822_time { |
96 | my ($time) = @_; | |
97 | my @localtm = localtime($time); | |
98 | my @gmttm = gmtime($time); | |
99 | my $localmin = $localtm[1] + $localtm[2] * 60; | |
100 | my $gmtmin = $gmttm[1] + $gmttm[2] * 60; | |
101 | if ($localtm[0] != $gmttm[0]) { | |
102 | die "local zone differs from GMT by a non-minute interval\n"; | |
103 | } | |
104 | if ((($gmttm[6] + 1) % 7) == $localtm[6]) { | |
105 | $localmin += 1440; | |
106 | } elsif ((($gmttm[6] - 1) % 7) == $localtm[6]) { | |
107 | $localmin -= 1440; | |
108 | } elsif ($gmttm[6] != $localtm[6]) { | |
109 | die "local time offset greater than or equal to 24 hours\n"; | |
110 | } | |
111 | my $offset = $localmin - $gmtmin; | |
112 | my $offhour = $offset / 60; | |
113 | my $offmin = abs($offset % 60); | |
114 | if (abs($offhour) >= 24) { | |
115 | die ("local time offset greater than or equal to 24 hours\n"); | |
116 | } | |
117 | ||
118 | return sprintf("%s, %2d %s %d %02d:%02d:%02d %s%02d%02d", | |
119 | qw(Sun Mon Tue Wed Thu Fri Sat)[$localtm[6]], | |
120 | $localtm[3], | |
121 | qw(Jan Feb Mar Apr May Jun | |
122 | Jul Aug Sep Oct Nov Dec)[$localtm[4]], | |
123 | $localtm[5]+1900, | |
124 | $localtm[2], | |
125 | $localtm[1], | |
126 | $localtm[0], | |
127 | ($offset >= 0) ? '+' : '-', | |
128 | abs($offhour), | |
129 | $offmin, | |
130 | ); | |
131 | } | |
4bc87a28 | 132 | |
567ffeb7 | 133 | my $have_email_valid = eval { require Email::Valid; 1 }; |
5012699d | 134 | my $have_mail_address = eval { require Mail::Address; 1 }; |
4bc87a28 | 135 | my $smtp; |
5f5b6118 | 136 | my $auth; |
4bc87a28 | 137 | |
e205735d | 138 | sub unique_email_list(@); |
1f038a0c RA |
139 | sub cleanup_compose_files(); |
140 | ||
83b24437 | 141 | # Variables we fill in automatically, or via prompting: |
f434c083 | 142 | my (@to,$no_to,@cc,$no_cc,@initial_cc,@bcclist,$no_bcc,@xh, |
8fd5bb7f PH |
143 | $initial_reply_to,$initial_subject,@files, |
144 | $author,$sender,$smtp_authpass,$annotate,$compose,$time); | |
83b24437 | 145 | |
f073a592 | 146 | my $envelope_sender; |
78488b2c | 147 | |
9133261f | 148 | # Example reply to: |
83b24437 | 149 | #$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>'; |
83b24437 | 150 | |
ad79c024 FL |
151 | my $repo = eval { Git->repository() }; |
152 | my @repo = $repo ? ($repo) : (); | |
280242d1 | 153 | my $term = eval { |
0fb7fc75 JS |
154 | $ENV{"GIT_SEND_EMAIL_NOTTY"} |
155 | ? new Term::ReadLine 'git-send-email', \*STDIN, \*STDOUT | |
156 | : new Term::ReadLine 'git-send-email'; | |
280242d1 JH |
157 | }; |
158 | if ($@) { | |
159 | $term = new FakeTerm "$@: going non-interactive"; | |
160 | } | |
83b24437 | 161 | |
5483c71d AR |
162 | # Behavior modification variables |
163 | my ($quiet, $dry_run) = (0, 0); | |
5df9fcf6 | 164 | my $format_patch; |
afe756c9 | 165 | my $compose_filename; |
a03bc5b6 | 166 | my $force = 0; |
5483c71d | 167 | |
8fd5bb7f PH |
168 | # Handle interactive edition of files. |
169 | my $multiedit; | |
0ce142c9 | 170 | my $editor; |
b4479f07 | 171 | |
8fd5bb7f | 172 | sub do_edit { |
0ce142c9 MG |
173 | if (!defined($editor)) { |
174 | $editor = Git::command_oneline('var', 'GIT_EDITOR'); | |
175 | } | |
8fd5bb7f | 176 | if (defined($multiedit) && !$multiedit) { |
beece9da PH |
177 | map { |
178 | system('sh', '-c', $editor.' "$@"', $editor, $_); | |
179 | if (($? & 127) || ($? >> 8)) { | |
180 | die("the editor exited uncleanly, aborting everything"); | |
181 | } | |
182 | } @_; | |
8fd5bb7f PH |
183 | } else { |
184 | system('sh', '-c', $editor.' "$@"', $editor, @_); | |
beece9da PH |
185 | if (($? & 127) || ($? >> 8)) { |
186 | die("the editor exited uncleanly, aborting everything"); | |
187 | } | |
8fd5bb7f PH |
188 | } |
189 | } | |
5483c71d AR |
190 | |
191 | # Variables with corresponding config settings | |
ddc3d4fe | 192 | my ($thread, $chain_reply_to, $suppress_from, $signed_off_by_cc, $cc_cmd); |
f6bebd12 | 193 | my ($smtp_server, $smtp_server_port, $smtp_authuser, $smtp_encryption); |
69cf7bfd | 194 | my ($identity, $aliasfiletype, @alias_files, @smtp_host_parts, $smtp_domain); |
c1f2aa45 | 195 | my ($validate, $confirm); |
65648283 | 196 | my (@suppress_cc); |
3cae7e5b | 197 | my ($auto_8bit_encoding); |
5483c71d | 198 | |
f60812ef JA |
199 | my ($debug_net_smtp) = 0; # Net::SMTP, see send_message() |
200 | ||
528fb087 NS |
201 | my $not_set_by_user = "true but not set by the user"; |
202 | ||
34cc60ce | 203 | my %config_bool_settings = ( |
5483c71d | 204 | "thread" => [\$thread, 1], |
528fb087 | 205 | "chainreplyto" => [\$chain_reply_to, $not_set_by_user], |
65648283 | 206 | "suppressfrom" => [\$suppress_from, undef], |
ddc3d4fe MW |
207 | "signedoffbycc" => [\$signed_off_by_cc, undef], |
208 | "signedoffcc" => [\$signed_off_by_cc, undef], # Deprecated | |
dbf5e1e9 | 209 | "validate" => [\$validate, 1], |
e46f7a0e AR |
210 | ); |
211 | ||
34cc60ce DS |
212 | my %config_settings = ( |
213 | "smtpserver" => \$smtp_server, | |
44b2476a | 214 | "smtpserverport" => \$smtp_server_port, |
34cc60ce DS |
215 | "smtpuser" => \$smtp_authuser, |
216 | "smtppass" => \$smtp_authpass, | |
69cf7bfd | 217 | "smtpdomain" => \$smtp_domain, |
2db9b49c | 218 | "to" => \@to, |
5f8b9fcd | 219 | "cc" => \@initial_cc, |
34cc60ce DS |
220 | "cccmd" => \$cc_cmd, |
221 | "aliasfiletype" => \$aliasfiletype, | |
222 | "bcc" => \@bcclist, | |
223 | "aliasesfile" => \@alias_files, | |
65648283 | 224 | "suppresscc" => \@suppress_cc, |
9f7820ae | 225 | "envelopesender" => \$envelope_sender, |
8fd5bb7f | 226 | "multiedit" => \$multiedit, |
c1f2aa45 | 227 | "confirm" => \$confirm, |
09caa24f | 228 | "from" => \$sender, |
3cae7e5b | 229 | "assume8bitencoding" => \$auto_8bit_encoding, |
34cc60ce | 230 | ); |
4a62d3f5 | 231 | |
528fb087 NS |
232 | # Help users prepare for 1.7.0 |
233 | sub chain_reply_to { | |
234 | if (defined $chain_reply_to && | |
235 | $chain_reply_to eq $not_set_by_user) { | |
236 | print STDERR | |
a19f101e | 237 | "In git 1.7.0, the default has changed to --no-chain-reply-to\n" . |
528fb087 NS |
238 | "Set sendemail.chainreplyto configuration variable to true if\n" . |
239 | "you want to keep --chain-reply-to as your default.\n"; | |
a19f101e | 240 | $chain_reply_to = 0; |
528fb087 NS |
241 | } |
242 | return $chain_reply_to; | |
243 | } | |
244 | ||
87429976 MW |
245 | # Handle Uncouth Termination |
246 | sub signal_handler { | |
247 | ||
248 | # Make text normal | |
249 | print color("reset"), "\n"; | |
250 | ||
251 | # SMTP password masked | |
252 | system "stty echo"; | |
253 | ||
254 | # tmp files from --compose | |
afe756c9 JS |
255 | if (defined $compose_filename) { |
256 | if (-e $compose_filename) { | |
257 | print "'$compose_filename' contains an intermediate version of the email you were composing.\n"; | |
258 | } | |
259 | if (-e ($compose_filename . ".final")) { | |
260 | print "'$compose_filename.final' contains the composed email.\n" | |
261 | } | |
87429976 MW |
262 | } |
263 | ||
264 | exit; | |
265 | }; | |
266 | ||
267 | $SIG{TERM} = \&signal_handler; | |
268 | $SIG{INT} = \&signal_handler; | |
269 | ||
83b24437 RA |
270 | # Begin by accumulating all the variables (defined above), that we will end up |
271 | # needing, first, from the command line: | |
272 | ||
94638f89 | 273 | my $rc = GetOptions("sender|from=s" => \$sender, |
83b24437 RA |
274 | "in-reply-to=s" => \$initial_reply_to, |
275 | "subject=s" => \$initial_subject, | |
276 | "to=s" => \@to, | |
f434c083 | 277 | "no-to" => \$no_to, |
da140f8b | 278 | "cc=s" => \@initial_cc, |
f434c083 | 279 | "no-cc" => \$no_cc, |
58063245 | 280 | "bcc=s" => \@bcclist, |
f434c083 | 281 | "no-bcc" => \$no_bcc, |
78488b2c | 282 | "chain-reply-to!" => \$chain_reply_to, |
3342d850 | 283 | "smtp-server=s" => \$smtp_server, |
44b2476a | 284 | "smtp-server-port=s" => \$smtp_server_port, |
34cc60ce | 285 | "smtp-user=s" => \$smtp_authuser, |
2363d746 | 286 | "smtp-pass:s" => \$smtp_authpass, |
f6bebd12 TR |
287 | "smtp-ssl" => sub { $smtp_encryption = 'ssl' }, |
288 | "smtp-encryption=s" => \$smtp_encryption, | |
f60812ef | 289 | "smtp-debug:i" => \$debug_net_smtp, |
69cf7bfd | 290 | "smtp-domain:s" => \$smtp_domain, |
34cc60ce | 291 | "identity=s" => \$identity, |
8fd5bb7f | 292 | "annotate" => \$annotate, |
1f038a0c | 293 | "compose" => \$compose, |
30d08b34 | 294 | "quiet" => \$quiet, |
324a8bd0 | 295 | "cc-cmd=s" => \$cc_cmd, |
5483c71d | 296 | "suppress-from!" => \$suppress_from, |
65648283 | 297 | "suppress-cc=s" => \@suppress_cc, |
ddc3d4fe | 298 | "signed-off-cc|signed-off-by-cc!" => \$signed_off_by_cc, |
c1f2aa45 | 299 | "confirm=s" => \$confirm, |
6130259c | 300 | "dry-run" => \$dry_run, |
f073a592 | 301 | "envelope-sender=s" => \$envelope_sender, |
5483c71d | 302 | "thread!" => \$thread, |
dbf5e1e9 | 303 | "validate!" => \$validate, |
5df9fcf6 | 304 | "format-patch!" => \$format_patch, |
3cae7e5b | 305 | "8bit-encoding=s" => \$auto_8bit_encoding, |
a03bc5b6 | 306 | "force" => \$force, |
83b24437 RA |
307 | ); |
308 | ||
1b0baf14 MC |
309 | unless ($rc) { |
310 | usage(); | |
311 | } | |
312 | ||
eed6ca7c JS |
313 | die "Cannot run git format-patch from outside a repository\n" |
314 | if $format_patch and not $repo; | |
315 | ||
34cc60ce DS |
316 | # Now, let's fill any that aren't set in with defaults: |
317 | ||
318 | sub read_config { | |
319 | my ($prefix) = @_; | |
320 | ||
321 | foreach my $setting (keys %config_bool_settings) { | |
322 | my $target = $config_bool_settings{$setting}->[0]; | |
ad79c024 | 323 | $$target = Git::config_bool(@repo, "$prefix.$setting") unless (defined $$target); |
34cc60ce DS |
324 | } |
325 | ||
326 | foreach my $setting (keys %config_settings) { | |
327 | my $target = $config_settings{$setting}; | |
f434c083 SB |
328 | next if $setting eq "to" and defined $no_to; |
329 | next if $setting eq "cc" and defined $no_cc; | |
330 | next if $setting eq "bcc" and defined $no_bcc; | |
34cc60ce DS |
331 | if (ref($target) eq "ARRAY") { |
332 | unless (@$target) { | |
ad79c024 | 333 | my @values = Git::config(@repo, "$prefix.$setting"); |
34cc60ce DS |
334 | @$target = @values if (@values && defined $values[0]); |
335 | } | |
336 | } | |
337 | else { | |
ad79c024 | 338 | $$target = Git::config(@repo, "$prefix.$setting") unless (defined $$target); |
34cc60ce DS |
339 | } |
340 | } | |
f6bebd12 TR |
341 | |
342 | if (!defined $smtp_encryption) { | |
343 | my $enc = Git::config(@repo, "$prefix.smtpencryption"); | |
344 | if (defined $enc) { | |
345 | $smtp_encryption = $enc; | |
346 | } elsif (Git::config_bool(@repo, "$prefix.smtpssl")) { | |
347 | $smtp_encryption = 'ssl'; | |
348 | } | |
349 | } | |
34cc60ce DS |
350 | } |
351 | ||
352 | # read configuration from [sendemail "$identity"], fall back on [sendemail] | |
ad79c024 | 353 | $identity = Git::config(@repo, "sendemail.identity") unless (defined $identity); |
34cc60ce DS |
354 | read_config("sendemail.$identity") if (defined $identity); |
355 | read_config("sendemail"); | |
356 | ||
357 | # fall back on builtin bool defaults | |
358 | foreach my $setting (values %config_bool_settings) { | |
359 | ${$setting->[0]} = $setting->[1] unless (defined (${$setting->[0]})); | |
360 | } | |
361 | ||
fa835cd5 TR |
362 | # 'default' encryption is none -- this only prevents a warning |
363 | $smtp_encryption = '' unless (defined $smtp_encryption); | |
364 | ||
65648283 DB |
365 | # Set CC suppressions |
366 | my(%suppress_cc); | |
367 | if (@suppress_cc) { | |
368 | foreach my $entry (@suppress_cc) { | |
369 | die "Unknown --suppress-cc field: '$entry'\n" | |
3531e270 | 370 | unless $entry =~ /^(all|cccmd|cc|author|self|sob|body|bodycc)$/; |
65648283 DB |
371 | $suppress_cc{$entry} = 1; |
372 | } | |
373 | } | |
374 | ||
375 | if ($suppress_cc{'all'}) { | |
cb8a9bd5 | 376 | foreach my $entry (qw (cccmd cc author self sob body bodycc)) { |
65648283 DB |
377 | $suppress_cc{$entry} = 1; |
378 | } | |
379 | delete $suppress_cc{'all'}; | |
380 | } | |
381 | ||
382 | # If explicit old-style ones are specified, they trump --suppress-cc. | |
383 | $suppress_cc{'self'} = $suppress_from if defined $suppress_from; | |
ddc3d4fe | 384 | $suppress_cc{'sob'} = !$signed_off_by_cc if defined $signed_off_by_cc; |
65648283 | 385 | |
3531e270 JS |
386 | if ($suppress_cc{'body'}) { |
387 | foreach my $entry (qw (sob bodycc)) { | |
388 | $suppress_cc{$entry} = 1; | |
389 | } | |
390 | delete $suppress_cc{'body'}; | |
391 | } | |
392 | ||
c1f2aa45 JS |
393 | # Set confirm's default value |
394 | my $confirm_unconfigured = !defined $confirm; | |
395 | if ($confirm_unconfigured) { | |
396 | $confirm = scalar %suppress_cc ? 'compose' : 'auto'; | |
397 | }; | |
398 | die "Unknown --confirm setting: '$confirm'\n" | |
399 | unless $confirm =~ /^(?:auto|cc|compose|always|never)/; | |
400 | ||
65648283 DB |
401 | # Debugging, print out the suppressions. |
402 | if (0) { | |
403 | print "suppressions:\n"; | |
404 | foreach my $entry (keys %suppress_cc) { | |
405 | printf " %-5s -> $suppress_cc{$entry}\n", $entry; | |
406 | } | |
407 | } | |
408 | ||
ad79c024 FL |
409 | my ($repoauthor, $repocommitter); |
410 | ($repoauthor) = Git::ident_person(@repo, 'author'); | |
411 | ($repocommitter) = Git::ident_person(@repo, 'committer'); | |
34cc60ce | 412 | |
79ee555b EB |
413 | # Verify the user input |
414 | ||
415 | foreach my $entry (@to) { | |
416 | die "Comma in --to entry: $entry'\n" unless $entry !~ m/,/; | |
417 | } | |
418 | ||
419 | foreach my $entry (@initial_cc) { | |
420 | die "Comma in --cc entry: $entry'\n" unless $entry !~ m/,/; | |
421 | } | |
422 | ||
423 | foreach my $entry (@bcclist) { | |
424 | die "Comma in --bcclist entry: $entry'\n" unless $entry !~ m/,/; | |
425 | } | |
426 | ||
5012699d JS |
427 | sub parse_address_line { |
428 | if ($have_mail_address) { | |
429 | return map { $_->format } Mail::Address->parse($_[0]); | |
430 | } else { | |
431 | return split_addrs($_[0]); | |
432 | } | |
433 | } | |
434 | ||
0e73b3ee | 435 | sub split_addrs { |
2f0e7cbb | 436 | return quotewords('\s*,\s*', 1, @_); |
0e73b3ee WF |
437 | } |
438 | ||
994d6c66 | 439 | my %aliases; |
994d6c66 EW |
440 | my %parse_alias = ( |
441 | # multiline formats can be supported in the future | |
442 | mutt => sub { my $fh = shift; while (<$fh>) { | |
ffc01f9b | 443 | if (/^\s*alias\s+(?:-group\s+\S+\s+)*(\S+)\s+(.*)$/) { |
994d6c66 EW |
444 | my ($alias, $addr) = ($1, $2); |
445 | $addr =~ s/#.*$//; # mutt allows # comments | |
446 | # commas delimit multiple addresses | |
0e73b3ee | 447 | $aliases{$alias} = [ split_addrs($addr) ]; |
994d6c66 EW |
448 | }}}, |
449 | mailrc => sub { my $fh = shift; while (<$fh>) { | |
450 | if (/^alias\s+(\S+)\s+(.*)$/) { | |
451 | # spaces delimit multiple addresses | |
fe87c921 | 452 | $aliases{$1} = [ quotewords('\s+', 0, $2) ]; |
994d6c66 | 453 | }}}, |
73c427eb TP |
454 | pine => sub { my $fh = shift; my $f='\t[^\t]*'; |
455 | for (my $x = ''; defined($x); $x = $_) { | |
456 | chomp $x; | |
457 | $x .= $1 while(defined($_ = <$fh>) && /^ +(.*)$/); | |
458 | $x =~ /^(\S+)$f\t\(?([^\t]+?)\)?(:?$f){0,2}$/ or next; | |
0e73b3ee | 459 | $aliases{$1} = [ split_addrs($2) ]; |
73c427eb | 460 | }}, |
7613ea35 BP |
461 | elm => sub { my $fh = shift; |
462 | while (<$fh>) { | |
463 | if (/^(\S+)\s+=\s+[^=]+=\s(\S+)/) { | |
464 | my ($alias, $addr) = ($1, $2); | |
465 | $aliases{$alias} = [ split_addrs($addr) ]; | |
466 | } | |
467 | } }, | |
468 | ||
994d6c66 EW |
469 | gnus => sub { my $fh = shift; while (<$fh>) { |
470 | if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) { | |
471 | $aliases{$1} = [ $2 ]; | |
472 | }}} | |
473 | ); | |
474 | ||
3cb8caf7 | 475 | if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) { |
994d6c66 EW |
476 | foreach my $file (@alias_files) { |
477 | open my $fh, '<', $file or die "opening $file: $!\n"; | |
478 | $parse_alias{$aliasfiletype}->($fh); | |
479 | close $fh; | |
480 | } | |
481 | } | |
482 | ||
94638f89 | 483 | ($sender) = expand_aliases($sender) if defined $sender; |
ae740a58 | 484 | |
5df9fcf6 PH |
485 | # returns 1 if the conflict must be solved using it as a format-patch argument |
486 | sub check_file_rev_conflict($) { | |
eed6ca7c | 487 | return unless $repo; |
5df9fcf6 PH |
488 | my $f = shift; |
489 | try { | |
490 | $repo->command('rev-parse', '--verify', '--quiet', $f); | |
491 | if (defined($format_patch)) { | |
5df9fcf6 PH |
492 | return $format_patch; |
493 | } | |
494 | die(<<EOF); | |
495 | File '$f' exists but it could also be the range of commits | |
496 | to produce patches for. Please disambiguate by... | |
497 | ||
498 | * Saying "./$f" if you mean a file; or | |
499 | * Giving --format-patch option if you mean a range. | |
500 | EOF | |
501 | } catch Git::Error::Command with { | |
502 | return 0; | |
503 | } | |
504 | } | |
505 | ||
aa54892f JK |
506 | # Now that all the defaults are set, process the rest of the command line |
507 | # arguments and collect up the files that need to be processed. | |
5df9fcf6 | 508 | my @rev_list_opts; |
69f4ce55 | 509 | while (defined(my $f = shift @ARGV)) { |
5df9fcf6 PH |
510 | if ($f eq "--") { |
511 | push @rev_list_opts, "--", @ARGV; | |
512 | @ARGV = (); | |
513 | } elsif (-d $f and !check_file_rev_conflict($f)) { | |
aa54892f JK |
514 | opendir(DH,$f) |
515 | or die "Failed to opendir $f: $!"; | |
516 | ||
517 | push @files, grep { -f $_ } map { +$f . "/" . $_ } | |
518 | sort readdir(DH); | |
8c178687 | 519 | closedir(DH); |
5df9fcf6 | 520 | } elsif ((-f $f or -p $f) and !check_file_rev_conflict($f)) { |
aa54892f | 521 | push @files, $f; |
aa54892f | 522 | } else { |
5df9fcf6 | 523 | push @rev_list_opts, $f; |
aa54892f JK |
524 | } |
525 | } | |
526 | ||
5df9fcf6 | 527 | if (@rev_list_opts) { |
eed6ca7c JS |
528 | die "Cannot run git format-patch from outside a repository\n" |
529 | unless $repo; | |
5df9fcf6 PH |
530 | push @files, $repo->command('format-patch', '-o', tempdir(CLEANUP => 1), @rev_list_opts); |
531 | } | |
532 | ||
dbf5e1e9 | 533 | if ($validate) { |
c764a0c2 | 534 | foreach my $f (@files) { |
300913bd KB |
535 | unless (-p $f) { |
536 | my $error = validate_patch($f); | |
537 | $error and die "fatal: $f: $error\nwarning: no patches were sent\n"; | |
538 | } | |
c764a0c2 | 539 | } |
747bbff9 JK |
540 | } |
541 | ||
aa54892f JK |
542 | if (@files) { |
543 | unless ($quiet) { | |
544 | print $_,"\n" for (@files); | |
545 | } | |
546 | } else { | |
547 | print STDERR "\nNo patch files specified!\n\n"; | |
548 | usage(); | |
549 | } | |
550 | ||
beece9da PH |
551 | sub get_patch_subject($) { |
552 | my $fn = shift; | |
553 | open (my $fh, '<', $fn); | |
554 | while (my $line = <$fh>) { | |
555 | next unless ($line =~ /^Subject: (.*)$/); | |
556 | close $fh; | |
557 | return "GIT: $1\n"; | |
558 | } | |
559 | close $fh; | |
560 | die "No subject line in $fn ?"; | |
561 | } | |
562 | ||
563 | if ($compose) { | |
564 | # Note that this does not need to be secure, but we will make a small | |
565 | # effort to have it be unique | |
afe756c9 JS |
566 | $compose_filename = ($repo ? |
567 | tempfile(".gitsendemail.msg.XXXXXX", DIR => $repo->repo_path()) : | |
568 | tempfile(".gitsendemail.msg.XXXXXX", DIR => "."))[1]; | |
beece9da PH |
569 | open(C,">",$compose_filename) |
570 | or die "Failed to open for writing $compose_filename: $!"; | |
571 | ||
572 | ||
573 | my $tpl_sender = $sender || $repoauthor || $repocommitter || ''; | |
574 | my $tpl_subject = $initial_subject || ''; | |
575 | my $tpl_reply_to = $initial_reply_to || ''; | |
576 | ||
577 | print C <<EOT; | |
578 | From $tpl_sender # This line is ignored. | |
40e6e8a0 | 579 | GIT: Lines beginning in "GIT:" will be removed. |
beece9da PH |
580 | GIT: Consider including an overall diffstat or table of contents |
581 | GIT: for the patch you are writing. | |
582 | GIT: | |
583 | GIT: Clear the body content if you don't wish to send a summary. | |
584 | From: $tpl_sender | |
585 | Subject: $tpl_subject | |
586 | In-Reply-To: $tpl_reply_to | |
587 | ||
588 | EOT | |
589 | for my $f (@files) { | |
590 | print C get_patch_subject($f); | |
591 | } | |
592 | close(C); | |
593 | ||
beece9da PH |
594 | if ($annotate) { |
595 | do_edit($compose_filename, @files); | |
596 | } else { | |
597 | do_edit($compose_filename); | |
598 | } | |
599 | ||
600 | open(C2,">",$compose_filename . ".final") | |
601 | or die "Failed to open $compose_filename.final : " . $!; | |
602 | ||
603 | open(C,"<",$compose_filename) | |
604 | or die "Failed to open $compose_filename : " . $!; | |
605 | ||
606 | my $need_8bit_cte = file_has_nonascii($compose_filename); | |
607 | my $in_body = 0; | |
608 | my $summary_empty = 1; | |
609 | while(<C>) { | |
40e6e8a0 | 610 | next if m/^GIT:/; |
beece9da PH |
611 | if ($in_body) { |
612 | $summary_empty = 0 unless (/^\n$/); | |
613 | } elsif (/^\n$/) { | |
614 | $in_body = 1; | |
615 | if ($need_8bit_cte) { | |
616 | print C2 "MIME-Version: 1.0\n", | |
617 | "Content-Type: text/plain; ", | |
d1fff6fc | 618 | "charset=UTF-8\n", |
beece9da PH |
619 | "Content-Transfer-Encoding: 8bit\n"; |
620 | } | |
621 | } elsif (/^MIME-Version:/i) { | |
622 | $need_8bit_cte = 0; | |
623 | } elsif (/^Subject:\s*(.+)\s*$/i) { | |
624 | $initial_subject = $1; | |
625 | my $subject = $initial_subject; | |
626 | $_ = "Subject: " . | |
627 | ($subject =~ /[^[:ascii:]]/ ? | |
628 | quote_rfc2047($subject) : | |
629 | $subject) . | |
630 | "\n"; | |
631 | } elsif (/^In-Reply-To:\s*(.+)\s*$/i) { | |
632 | $initial_reply_to = $1; | |
633 | next; | |
634 | } elsif (/^From:\s*(.+)\s*$/i) { | |
635 | $sender = $1; | |
636 | next; | |
637 | } elsif (/^(?:To|Cc|Bcc):/i) { | |
638 | print "To/Cc/Bcc fields are not interpreted yet, they have been ignored\n"; | |
639 | next; | |
640 | } | |
641 | print C2 $_; | |
642 | } | |
643 | close(C); | |
644 | close(C2); | |
645 | ||
646 | if ($summary_empty) { | |
647 | print "Summary email is empty, skipping it\n"; | |
648 | $compose = -1; | |
649 | } | |
650 | } elsif ($annotate) { | |
651 | do_edit(@files); | |
652 | } | |
653 | ||
6e182518 JS |
654 | sub ask { |
655 | my ($prompt, %arg) = @_; | |
0da43a68 | 656 | my $valid_re = $arg{valid_re}; |
6e182518 JS |
657 | my $default = $arg{default}; |
658 | my $resp; | |
659 | my $i = 0; | |
5906f54e JS |
660 | return defined $default ? $default : undef |
661 | unless defined $term->IN and defined fileno($term->IN) and | |
662 | defined $term->OUT and defined fileno($term->OUT); | |
6e182518 JS |
663 | while ($i++ < 10) { |
664 | $resp = $term->readline($prompt); | |
665 | if (!defined $resp) { # EOF | |
666 | print "\n"; | |
667 | return defined $default ? $default : undef; | |
668 | } | |
669 | if ($resp eq '' and defined $default) { | |
670 | return $default; | |
671 | } | |
0da43a68 | 672 | if (!defined $valid_re or $resp =~ /$valid_re/) { |
6e182518 JS |
673 | return $resp; |
674 | } | |
675 | } | |
676 | return undef; | |
677 | } | |
678 | ||
3cae7e5b TR |
679 | my %broken_encoding; |
680 | ||
681 | sub file_declares_8bit_cte($) { | |
682 | my $fn = shift; | |
683 | open (my $fh, '<', $fn); | |
684 | while (my $line = <$fh>) { | |
685 | last if ($line =~ /^$/); | |
686 | return 1 if ($line =~ /^Content-Transfer-Encoding: .*8bit.*$/); | |
687 | } | |
688 | close $fh; | |
689 | return 0; | |
690 | } | |
691 | ||
692 | foreach my $f (@files) { | |
693 | next unless (body_or_subject_has_nonascii($f) | |
694 | && !file_declares_8bit_cte($f)); | |
695 | $broken_encoding{$f} = 1; | |
696 | } | |
697 | ||
698 | if (!defined $auto_8bit_encoding && scalar %broken_encoding) { | |
699 | print "The following files are 8bit, but do not declare " . | |
700 | "a Content-Transfer-Encoding.\n"; | |
701 | foreach my $f (sort keys %broken_encoding) { | |
702 | print " $f\n"; | |
703 | } | |
704 | $auto_8bit_encoding = ask("Which 8bit encoding should I declare [UTF-8]? ", | |
705 | default => "UTF-8"); | |
706 | } | |
707 | ||
a03bc5b6 TR |
708 | if (!$force) { |
709 | for my $f (@files) { | |
710 | if (get_patch_subject($f) =~ /\*\*\* SUBJECT HERE \*\*\*/) { | |
711 | die "Refusing to send because the patch\n\t$f\n" | |
712 | . "has the template subject '*** SUBJECT HERE ***'. " | |
713 | . "Pass --force if you really want to send.\n"; | |
714 | } | |
715 | } | |
716 | } | |
717 | ||
1f038a0c | 718 | my $prompting = 0; |
94638f89 | 719 | if (!defined $sender) { |
ad79c024 | 720 | $sender = $repoauthor || $repocommitter || ''; |
6e182518 JS |
721 | $sender = ask("Who should the emails appear to be from? [$sender] ", |
722 | default => $sender); | |
94638f89 | 723 | print "Emails will be sent from: ", $sender, "\n"; |
1f038a0c | 724 | $prompting++; |
83b24437 RA |
725 | } |
726 | ||
727 | if (!@to) { | |
6e182518 JS |
728 | my $to = ask("Who should the emails be sent to? "); |
729 | push @to, parse_address_line($to) if defined $to; # sanitized/validated later | |
1f038a0c | 730 | $prompting++; |
83b24437 RA |
731 | } |
732 | ||
994d6c66 | 733 | sub expand_aliases { |
302e04ea JK |
734 | return map { expand_one_alias($_) } @_; |
735 | } | |
736 | ||
737 | my %EXPANDED_ALIASES; | |
738 | sub expand_one_alias { | |
739 | my $alias = shift; | |
740 | if ($EXPANDED_ALIASES{$alias}) { | |
741 | die "fatal: alias '$alias' expands to itself\n"; | |
742 | } | |
743 | local $EXPANDED_ALIASES{$alias} = 1; | |
744 | return $aliases{$alias} ? expand_aliases(@{$aliases{$alias}}) : $alias; | |
994d6c66 EW |
745 | } |
746 | ||
747 | @to = expand_aliases(@to); | |
5b56aaa2 | 748 | @to = (map { sanitize_address($_) } @to); |
994d6c66 | 749 | @initial_cc = expand_aliases(@initial_cc); |
58063245 | 750 | @bcclist = expand_aliases(@bcclist); |
994d6c66 | 751 | |
5483c71d | 752 | if ($thread && !defined $initial_reply_to && $prompting) { |
6e182518 JS |
753 | $initial_reply_to = ask( |
754 | "Message-ID to be used as In-Reply-To for the first email? "); | |
83b24437 | 755 | } |
1ca3d6ed | 756 | if (defined $initial_reply_to) { |
0fb7fc75 JS |
757 | $initial_reply_to =~ s/^\s*<?//; |
758 | $initial_reply_to =~ s/>?\s*$//; | |
759 | $initial_reply_to = "<$initial_reply_to>" if $initial_reply_to ne ''; | |
ace9c2a9 | 760 | } |
ace72086 | 761 | |
34cc60ce | 762 | if (!defined $smtp_server) { |
aca7ad76 EW |
763 | foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) { |
764 | if (-x $_) { | |
765 | $smtp_server = $_; | |
766 | last; | |
767 | } | |
768 | } | |
769 | $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug* | |
3342d850 RA |
770 | } |
771 | ||
c1f2aa45 JS |
772 | if ($compose && $compose > 0) { |
773 | @files = ($compose_filename . ".final", @files); | |
1f038a0c RA |
774 | } |
775 | ||
83b24437 | 776 | # Variables we set as part of the loop over files |
c1f2aa45 | 777 | our ($message_id, %mail, $subject, $reply_to, $references, $message, |
dc1460aa | 778 | $needs_confirm, $message_num, $ask_default); |
83b24437 | 779 | |
567ffeb7 EW |
780 | sub extract_valid_address { |
781 | my $address = shift; | |
ad9c18f5 | 782 | my $local_part_regexp = '[^<>"\s@]+'; |
09302e17 | 783 | my $domain_regexp = '[^.<>"\s@]+(?:\.[^.<>"\s@]+)+'; |
db3106b2 EW |
784 | |
785 | # check for a local address: | |
ad9c18f5 | 786 | return $address if ($address =~ /^($local_part_regexp)$/); |
db3106b2 | 787 | |
155197e6 | 788 | $address =~ s/^\s*<(.*)>\s*$/$1/; |
567ffeb7 | 789 | if ($have_email_valid) { |
ad9c18f5 | 790 | return scalar Email::Valid->address($address); |
567ffeb7 EW |
791 | } else { |
792 | # less robust/correct than the monster regexp in Email::Valid, | |
793 | # but still does a 99% job, and one less dependency | |
ad9c18f5 | 794 | $address =~ /($local_part_regexp\@$domain_regexp)/; |
e96fd305 | 795 | return $1; |
567ffeb7 EW |
796 | } |
797 | } | |
83b24437 RA |
798 | |
799 | # Usually don't need to change anything below here. | |
800 | ||
801 | # we make a "fake" message id by taking the current number | |
802 | # of seconds since the beginning of Unix time and tacking on | |
803 | # a random number to the end, in case we are called quicker than | |
804 | # 1 second since the last time we were called. | |
8037d1a3 RA |
805 | |
806 | # We'll setup a template for the message id, using the "from" address: | |
8037d1a3 | 807 | |
be510cfe | 808 | my ($message_id_stamp, $message_id_serial); |
68ce9330 | 809 | sub make_message_id { |
be510cfe JH |
810 | my $uniq; |
811 | if (!defined $message_id_stamp) { | |
812 | $message_id_stamp = sprintf("%s-%s", time, $$); | |
813 | $message_id_serial = 0; | |
814 | } | |
815 | $message_id_serial++; | |
816 | $uniq = "$message_id_stamp-$message_id_serial"; | |
817 | ||
aeb59328 | 818 | my $du_part; |
94638f89 UKK |
819 | for ($sender, $repocommitter, $repoauthor) { |
820 | $du_part = extract_valid_address(sanitize_address($_)); | |
821 | last if (defined $du_part and $du_part ne ''); | |
aeb59328 | 822 | } |
94638f89 | 823 | if (not defined $du_part or $du_part eq '') { |
aeb59328 JH |
824 | use Sys::Hostname qw(); |
825 | $du_part = 'user@' . Sys::Hostname::hostname(); | |
826 | } | |
be510cfe JH |
827 | my $message_id_template = "<%s-git-send-email-%s>"; |
828 | $message_id = sprintf($message_id_template, $uniq, $du_part); | |
8037d1a3 | 829 | #print "new message id = $message_id\n"; # Was useful for debugging |
83b24437 RA |
830 | } |
831 | ||
832 | ||
833 | ||
a5370b16 | 834 | $time = time - scalar $#files; |
83b24437 | 835 | |
374c5905 JR |
836 | sub unquote_rfc2047 { |
837 | local ($_) = @_; | |
8291db6f JK |
838 | my $encoding; |
839 | if (s/=\?([^?]+)\?q\?(.*)\?=/$2/g) { | |
840 | $encoding = $1; | |
374c5905 JR |
841 | s/_/ /g; |
842 | s/=([0-9A-F]{2})/chr(hex($1))/eg; | |
843 | } | |
8291db6f | 844 | return wantarray ? ($_, $encoding) : $_; |
374c5905 JR |
845 | } |
846 | ||
d54eaaa2 JK |
847 | sub quote_rfc2047 { |
848 | local $_ = shift; | |
d1fff6fc | 849 | my $encoding = shift || 'UTF-8'; |
d54eaaa2 JK |
850 | s/([^-a-zA-Z0-9!*+\/])/sprintf("=%02X", ord($1))/eg; |
851 | s/(.*)/=\?$encoding\?q\?$1\?=/; | |
852 | return $_; | |
853 | } | |
854 | ||
a3a8262b BC |
855 | sub is_rfc2047_quoted { |
856 | my $s = shift; | |
857 | my $token = '[^][()<>@,;:"\/?.= \000-\037\177-\377]+'; | |
858 | my $encoded_text = '[!->@-~]+'; | |
859 | length($s) <= 75 && | |
860 | $s =~ m/^(?:"[[:ascii:]]*"|=\?$token\?$token\?$encoded_text\?=)$/o; | |
861 | } | |
862 | ||
5b56aaa2 | 863 | # use the simplest quoting being able to handle the recipient |
68ce9330 | 864 | sub sanitize_address { |
732263d4 | 865 | my ($recipient) = @_; |
5b56aaa2 UKK |
866 | my ($recipient_name, $recipient_addr) = ($recipient =~ /^(.*?)\s*(<.*)/); |
867 | ||
868 | if (not $recipient_name) { | |
869 | return "$recipient"; | |
870 | } | |
871 | ||
872 | # if recipient_name is already quoted, do nothing | |
a3a8262b | 873 | if (is_rfc2047_quoted($recipient_name)) { |
5b56aaa2 UKK |
874 | return $recipient; |
875 | } | |
876 | ||
877 | # rfc2047 is needed if a non-ascii char is included | |
878 | if ($recipient_name =~ /[^[:ascii:]]/) { | |
a61c0ffa | 879 | $recipient_name =~ s/^"(.*)"$/$1/; |
d54eaaa2 | 880 | $recipient_name = quote_rfc2047($recipient_name); |
732263d4 | 881 | } |
5b56aaa2 UKK |
882 | |
883 | # double quotes are needed if specials or CTLs are included | |
884 | elsif ($recipient_name =~ /[][()<>@,;:\\".\000-\037\177]/) { | |
18023c20 | 885 | $recipient_name =~ s/(["\\\r])/\\$1/g; |
5b56aaa2 UKK |
886 | $recipient_name = "\"$recipient_name\""; |
887 | } | |
888 | ||
889 | return "$recipient_name $recipient_addr"; | |
890 | ||
732263d4 RJ |
891 | } |
892 | ||
134550fe JA |
893 | # Returns the local Fully Qualified Domain Name (FQDN) if available. |
894 | # | |
895 | # Tightly configured MTAa require that a caller sends a real DNS | |
896 | # domain name that corresponds the IP address in the HELO/EHLO | |
897 | # handshake. This is used to verify the connection and prevent | |
898 | # spammers from trying to hide their identity. If the DNS and IP don't | |
899 | # match, the receiveing MTA may deny the connection. | |
900 | # | |
901 | # Here is a deny example of Net::SMTP with the default "localhost.localdomain" | |
902 | # | |
903 | # Net::SMTP=GLOB(0x267ec28)>>> EHLO localhost.localdomain | |
904 | # Net::SMTP=GLOB(0x267ec28)<<< 550 EHLO argument does not match calling host | |
905 | # | |
906 | # This maildomain*() code is based on ideas in Perl library Test::Reporter | |
907 | # /usr/share/perl5/Test/Reporter/Mail/Util.pm ==> sub _maildomain () | |
908 | ||
59a86303 BG |
909 | sub valid_fqdn { |
910 | my $domain = shift; | |
61ef5e9b | 911 | return defined $domain && !($^O eq 'darwin' && $domain =~ /\.local$/) && $domain =~ /\./; |
59a86303 BG |
912 | } |
913 | ||
68ce9330 | 914 | sub maildomain_net { |
134550fe JA |
915 | my $maildomain; |
916 | ||
917 | if (eval { require Net::Domain; 1 }) { | |
918 | my $domain = Net::Domain::domainname(); | |
59a86303 | 919 | $maildomain = $domain if valid_fqdn($domain); |
134550fe JA |
920 | } |
921 | ||
922 | return $maildomain; | |
923 | } | |
924 | ||
68ce9330 | 925 | sub maildomain_mta { |
134550fe JA |
926 | my $maildomain; |
927 | ||
928 | if (eval { require Net::SMTP; 1 }) { | |
929 | for my $host (qw(mailhost localhost)) { | |
930 | my $smtp = Net::SMTP->new($host); | |
931 | if (defined $smtp) { | |
932 | my $domain = $smtp->domain; | |
933 | $smtp->quit; | |
934 | ||
59a86303 | 935 | $maildomain = $domain if valid_fqdn($domain); |
134550fe JA |
936 | |
937 | last if $maildomain; | |
938 | } | |
939 | } | |
940 | } | |
941 | ||
942 | return $maildomain; | |
943 | } | |
944 | ||
68ce9330 | 945 | sub maildomain { |
69cf7bfd | 946 | return maildomain_net() || maildomain_mta() || 'localhost.localdomain'; |
134550fe JA |
947 | } |
948 | ||
15da1084 | 949 | # Returns 1 if the message was sent, and 0 otherwise. |
a1b5b371 | 950 | # In actuality, the whole program dies when there |
15da1084 MW |
951 | # is an error sending a message. |
952 | ||
68ce9330 | 953 | sub send_message { |
4bc87a28 | 954 | my @recipients = unique_email_list(@to); |
7ac17529 | 955 | @cc = (grep { my $cc = extract_valid_address($_); |
83acaaec | 956 | not grep { $cc eq $_ || $_ =~ /<\Q${cc}\E>$/ } @recipients |
7ac17529 ABH |
957 | } |
958 | map { sanitize_address($_) } | |
959 | @cc); | |
4bc87a28 | 960 | my $to = join (",\n\t", @recipients); |
58063245 | 961 | @recipients = unique_email_list(@recipients,@cc,@bcclist); |
c38f0247 | 962 | @recipients = (map { extract_valid_address($_) } @recipients); |
6bdca890 | 963 | my $date = format_2822_time($time++); |
e923effb ML |
964 | my $gitversion = '@@GIT_VERSION@@'; |
965 | if ($gitversion =~ m/..GIT_VERSION../) { | |
3cb8caf7 | 966 | $gitversion = Git::version(); |
e923effb | 967 | } |
4bc87a28 | 968 | |
02461e0e | 969 | my $cc = join(",\n\t", unique_email_list(@cc)); |
f06a6a49 JH |
970 | my $ccline = ""; |
971 | if ($cc ne '') { | |
972 | $ccline = "\nCc: $cc"; | |
973 | } | |
94638f89 | 974 | my $sanitized_sender = sanitize_address($sender); |
4f3d3703 | 975 | make_message_id() unless defined($message_id); |
aeb59328 | 976 | |
94638f89 | 977 | my $header = "From: $sanitized_sender |
f06a6a49 | 978 | To: $to${ccline} |
4bc87a28 | 979 | Subject: $subject |
4bc87a28 EW |
980 | Date: $date |
981 | Message-Id: $message_id | |
e923effb | 982 | X-Mailer: git-send-email $gitversion |
4bc87a28 | 983 | "; |
3e0c4ffd | 984 | if ($reply_to) { |
7ccf7927 RA |
985 | |
986 | $header .= "In-Reply-To: $reply_to\n"; | |
987 | $header .= "References: $references\n"; | |
988 | } | |
ce91c2f6 JH |
989 | if (@xh) { |
990 | $header .= join("\n", @xh) . "\n"; | |
991 | } | |
4bc87a28 | 992 | |
c38f0247 | 993 | my @sendmail_parameters = ('-i', @recipients); |
94638f89 | 994 | my $raw_from = $sanitized_sender; |
c89e3241 FC |
995 | if (defined $envelope_sender && $envelope_sender ne "auto") { |
996 | $raw_from = $envelope_sender; | |
997 | } | |
f073a592 RJ |
998 | $raw_from = extract_valid_address($raw_from); |
999 | unshift (@sendmail_parameters, | |
1000 | '-f', $raw_from) if(defined $envelope_sender); | |
8e3d436b | 1001 | |
c1f2aa45 JS |
1002 | if ($needs_confirm && !$dry_run) { |
1003 | print "\n$header\n"; | |
1004 | if ($needs_confirm eq "inform") { | |
1005 | $confirm_unconfigured = 0; # squelch this message for the rest of this run | |
6e182518 | 1006 | $ask_default = "y"; # assume yes on EOF since user hasn't explicitly asked for confirmation |
c1f2aa45 JS |
1007 | print " The Cc list above has been expanded by additional\n"; |
1008 | print " addresses found in the patch commit message. By default\n"; | |
1009 | print " send-email prompts before sending whenever this occurs.\n"; | |
1010 | print " This behavior is controlled by the sendemail.confirm\n"; | |
1011 | print " configuration setting.\n"; | |
1012 | print "\n"; | |
1013 | print " For additional information, run 'git send-email --help'.\n"; | |
1014 | print " To retain the current behavior, but squelch this message,\n"; | |
1015 | print " run 'git config --global sendemail.confirm auto'.\n\n"; | |
1016 | } | |
6e182518 JS |
1017 | $_ = ask("Send this email? ([y]es|[n]o|[q]uit|[a]ll): ", |
1018 | valid_re => qr/^(?:yes|y|no|n|quit|q|all|a)/i, | |
1019 | default => $ask_default); | |
1020 | die "Send this email reply required" unless defined $_; | |
c1f2aa45 | 1021 | if (/^n/i) { |
15da1084 | 1022 | return 0; |
c1f2aa45 JS |
1023 | } elsif (/^q/i) { |
1024 | cleanup_compose_files(); | |
1025 | exit(0); | |
1026 | } elsif (/^a/i) { | |
1027 | $confirm = 'never'; | |
1028 | } | |
1029 | } | |
1030 | ||
6130259c MW |
1031 | if ($dry_run) { |
1032 | # We don't want to send the email. | |
1033 | } elsif ($smtp_server =~ m#^/#) { | |
aca7ad76 EW |
1034 | my $pid = open my $sm, '|-'; |
1035 | defined $pid or die $!; | |
1036 | if (!$pid) { | |
8e3d436b | 1037 | exec($smtp_server, @sendmail_parameters) or die $!; |
aca7ad76 EW |
1038 | } |
1039 | print $sm "$header\n$message"; | |
1040 | close $sm or die $?; | |
1041 | } else { | |
44b2476a JH |
1042 | |
1043 | if (!defined $smtp_server) { | |
1044 | die "The required SMTP server is not properly defined." | |
1045 | } | |
1046 | ||
f6bebd12 | 1047 | if ($smtp_encryption eq 'ssl') { |
44b2476a | 1048 | $smtp_server_port ||= 465; # ssmtp |
34cc60ce | 1049 | require Net::SMTP::SSL; |
69cf7bfd | 1050 | $smtp_domain ||= maildomain(); |
134550fe | 1051 | $smtp ||= Net::SMTP::SSL->new($smtp_server, |
69cf7bfd | 1052 | Hello => $smtp_domain, |
134550fe | 1053 | Port => $smtp_server_port); |
34cc60ce DS |
1054 | } |
1055 | else { | |
1056 | require Net::SMTP; | |
69cf7bfd | 1057 | $smtp_domain ||= maildomain(); |
44b2476a JH |
1058 | $smtp ||= Net::SMTP->new((defined $smtp_server_port) |
1059 | ? "$smtp_server:$smtp_server_port" | |
f60812ef | 1060 | : $smtp_server, |
69cf7bfd | 1061 | Hello => $smtp_domain, |
f60812ef | 1062 | Debug => $debug_net_smtp); |
fb3650ed | 1063 | if ($smtp_encryption eq 'tls' && $smtp) { |
f6bebd12 TR |
1064 | require Net::SMTP::SSL; |
1065 | $smtp->command('STARTTLS'); | |
1066 | $smtp->response(); | |
1067 | if ($smtp->code == 220) { | |
1068 | $smtp = Net::SMTP::SSL->start_SSL($smtp) | |
1069 | or die "STARTTLS failed! ".$smtp->message; | |
6cbf8b00 | 1070 | $smtp_encryption = ''; |
9d1ccf5e RS |
1071 | # Send EHLO again to receive fresh |
1072 | # supported commands | |
1073 | $smtp->hello(); | |
f6bebd12 TR |
1074 | } else { |
1075 | die "Server does not support STARTTLS! ".$smtp->message; | |
1076 | } | |
1077 | } | |
44b2476a JH |
1078 | } |
1079 | ||
1080 | if (!$smtp) { | |
f60812ef | 1081 | die "Unable to initialize SMTP properly. Check config and use --smtp-debug. ", |
e5afb3a6 JA |
1082 | "VALUES: server=$smtp_server ", |
1083 | "encryption=$smtp_encryption ", | |
69cf7bfd | 1084 | "hello=$smtp_domain", |
e5afb3a6 | 1085 | defined $smtp_server_port ? "port=$smtp_server_port" : ""; |
44b2476a JH |
1086 | } |
1087 | ||
2363d746 MW |
1088 | if (defined $smtp_authuser) { |
1089 | ||
1090 | if (!defined $smtp_authpass) { | |
1091 | ||
1092 | system "stty -echo"; | |
1093 | ||
1094 | do { | |
1095 | print "Password: "; | |
1096 | $_ = <STDIN>; | |
1097 | print "\n"; | |
1098 | } while (!defined $_); | |
1099 | ||
1100 | chomp($smtp_authpass = $_); | |
1101 | ||
1102 | system "stty echo"; | |
1103 | } | |
1104 | ||
5f5b6118 | 1105 | $auth ||= $smtp->auth( $smtp_authuser, $smtp_authpass ) or die $smtp->message; |
34cc60ce | 1106 | } |
2363d746 | 1107 | |
2b69bfc2 | 1108 | $smtp->mail( $raw_from ) or die $smtp->message; |
aca7ad76 EW |
1109 | $smtp->to( @recipients ) or die $smtp->message; |
1110 | $smtp->data or die $smtp->message; | |
1111 | $smtp->datasend("$header\n$message") or die $smtp->message; | |
1112 | $smtp->dataend() or die $smtp->message; | |
15da1084 | 1113 | $smtp->code =~ /250|200/ or die "Failed to send $subject\n".$smtp->message; |
aca7ad76 | 1114 | } |
2718435b | 1115 | if ($quiet) { |
71c7da94 | 1116 | printf (($dry_run ? "Dry-" : "")."Sent %s\n", $subject); |
2718435b | 1117 | } else { |
b7f30e0a | 1118 | print (($dry_run ? "Dry-" : "")."OK. Log says:\n"); |
2b69bfc2 | 1119 | if ($smtp_server !~ m#^/#) { |
aca7ad76 | 1120 | print "Server: $smtp_server\n"; |
2b69bfc2 | 1121 | print "MAIL FROM:<$raw_from>\n"; |
02461e0e JP |
1122 | foreach my $entry (@recipients) { |
1123 | print "RCPT TO:<$entry>\n"; | |
1124 | } | |
aca7ad76 | 1125 | } else { |
8e3d436b | 1126 | print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n"; |
aca7ad76 | 1127 | } |
b7f30e0a | 1128 | print $header, "\n"; |
aca7ad76 EW |
1129 | if ($smtp) { |
1130 | print "Result: ", $smtp->code, ' ', | |
1131 | ($smtp->message =~ /\n([^\n]+\n)$/s), "\n"; | |
1132 | } else { | |
1133 | print "Result: OK\n"; | |
1134 | } | |
30d08b34 | 1135 | } |
15da1084 MW |
1136 | |
1137 | return 1; | |
83b24437 RA |
1138 | } |
1139 | ||
83b24437 | 1140 | $reply_to = $initial_reply_to; |
2186d566 | 1141 | $references = $initial_reply_to || ''; |
83b24437 | 1142 | $subject = $initial_subject; |
c1f2aa45 | 1143 | $message_num = 0; |
83b24437 RA |
1144 | |
1145 | foreach my $t (@files) { | |
83b24437 RA |
1146 | open(F,"<",$t) or die "can't open file $t"; |
1147 | ||
94638f89 | 1148 | my $author = undef; |
8291db6f JK |
1149 | my $author_encoding; |
1150 | my $has_content_type; | |
1151 | my $body_encoding; | |
c1f2aa45 | 1152 | @cc = (); |
ce91c2f6 | 1153 | @xh = (); |
e6b0964a | 1154 | my $input_format = undef; |
5012699d | 1155 | my @header = (); |
83b24437 | 1156 | $message = ""; |
c1f2aa45 | 1157 | $message_num++; |
5012699d | 1158 | # First unfold multiline header fields |
83b24437 | 1159 | while(<F>) { |
5012699d JS |
1160 | last if /^\s*$/; |
1161 | if (/^\s+\S/ and @header) { | |
1162 | chomp($header[$#header]); | |
1163 | s/^\s+/ /; | |
1164 | $header[$#header] .= $_; | |
1165 | } else { | |
1166 | push(@header, $_); | |
1167 | } | |
1168 | } | |
1169 | # Now parse the header | |
1170 | foreach(@header) { | |
1171 | if (/^From /) { | |
1172 | $input_format = 'mbox'; | |
1173 | next; | |
1174 | } | |
1175 | chomp; | |
1176 | if (!defined $input_format && /^[-A-Za-z]+:\s/) { | |
1177 | $input_format = 'mbox'; | |
1178 | } | |
1179 | ||
1180 | if (defined $input_format && $input_format eq 'mbox') { | |
1181 | if (/^Subject:\s+(.*)$/) { | |
1182 | $subject = $1; | |
e6b0964a | 1183 | } |
5012699d JS |
1184 | elsif (/^From:\s+(.*)$/) { |
1185 | ($author, $author_encoding) = unquote_rfc2047($1); | |
1186 | next if $suppress_cc{'author'}; | |
1187 | next if $suppress_cc{'self'} and $author eq $sender; | |
1188 | printf("(mbox) Adding cc: %s from line '%s'\n", | |
1189 | $1, $_) unless $quiet; | |
1190 | push @cc, $1; | |
e6b0964a | 1191 | } |
5012699d JS |
1192 | elsif (/^Cc:\s+(.*)$/) { |
1193 | foreach my $addr (parse_address_line($1)) { | |
1194 | if (unquote_rfc2047($addr) eq $sender) { | |
65648283 | 1195 | next if ($suppress_cc{'self'}); |
65648283 DB |
1196 | } else { |
1197 | next if ($suppress_cc{'cc'}); | |
8a8e6235 | 1198 | } |
83b24437 | 1199 | printf("(mbox) Adding cc: %s from line '%s'\n", |
5012699d JS |
1200 | $addr, $_) unless $quiet; |
1201 | push @cc, $addr; | |
83b24437 | 1202 | } |
5012699d JS |
1203 | } |
1204 | elsif (/^Content-type:/i) { | |
1205 | $has_content_type = 1; | |
1206 | if (/charset="?([^ "]+)/) { | |
1207 | $body_encoding = $1; | |
83b24437 | 1208 | } |
5012699d | 1209 | push @xh, $_; |
83b24437 | 1210 | } |
5012699d JS |
1211 | elsif (/^Message-Id: (.*)/i) { |
1212 | $message_id = $1; | |
83b24437 | 1213 | } |
5012699d JS |
1214 | elsif (!/^Date:\s/ && /^[-A-Za-z]+:\s+\S/) { |
1215 | push @xh, $_; | |
1216 | } | |
1217 | ||
83b24437 | 1218 | } else { |
5012699d JS |
1219 | # In the traditional |
1220 | # "send lots of email" format, | |
1221 | # line 1 = cc | |
1222 | # line 2 = subject | |
1223 | # So let's support that, too. | |
1224 | $input_format = 'lots'; | |
1225 | if (@cc == 0 && !$suppress_cc{'cc'}) { | |
1226 | printf("(non-mbox) Adding cc: %s from line '%s'\n", | |
1227 | $_, $_) unless $quiet; | |
1228 | push @cc, $_; | |
1229 | } elsif (!defined $subject) { | |
1230 | $subject = $_; | |
83b24437 RA |
1231 | } |
1232 | } | |
1233 | } | |
5012699d JS |
1234 | # Now parse the message body |
1235 | while(<F>) { | |
1236 | $message .= $_; | |
1237 | if (/^(Signed-off-by|Cc): (.*)$/i) { | |
5012699d | 1238 | chomp; |
3531e270 | 1239 | my ($what, $c) = ($1, $2); |
5012699d | 1240 | chomp $c; |
3531e270 JS |
1241 | if ($c eq $sender) { |
1242 | next if ($suppress_cc{'self'}); | |
1243 | } else { | |
1244 | next if $suppress_cc{'sob'} and $what =~ /Signed-off-by/i; | |
1245 | next if $suppress_cc{'bodycc'} and $what =~ /Cc/i; | |
1246 | } | |
5012699d | 1247 | push @cc, $c; |
3531e270 | 1248 | printf("(body) Adding cc: %s from line '%s'\n", |
5012699d JS |
1249 | $c, $_) unless $quiet; |
1250 | } | |
1251 | } | |
83b24437 | 1252 | close F; |
324a8bd0 | 1253 | |
65648283 | 1254 | if (defined $cc_cmd && !$suppress_cc{'cccmd'}) { |
cb8a9bd5 | 1255 | open(F, "$cc_cmd \Q$t\E |") |
324a8bd0 JP |
1256 | or die "(cc-cmd) Could not execute '$cc_cmd'"; |
1257 | while(<F>) { | |
1258 | my $c = $_; | |
1259 | $c =~ s/^\s*//g; | |
1260 | $c =~ s/\n$//g; | |
620bb245 | 1261 | next if ($c eq $sender and $suppress_from); |
324a8bd0 JP |
1262 | push @cc, $c; |
1263 | printf("(cc-cmd) Adding cc: %s from: '%s'\n", | |
1264 | $c, $cc_cmd) unless $quiet; | |
1265 | } | |
1266 | close F | |
1267 | or die "(cc-cmd) failed to close pipe to '$cc_cmd'"; | |
1268 | } | |
1269 | ||
3cae7e5b TR |
1270 | if ($broken_encoding{$t} && !$has_content_type) { |
1271 | $has_content_type = 1; | |
1272 | push @xh, "MIME-Version: 1.0", | |
1273 | "Content-Type: text/plain; charset=$auto_8bit_encoding", | |
1274 | "Content-Transfer-Encoding: 8bit"; | |
1275 | $body_encoding = $auto_8bit_encoding; | |
1276 | } | |
1277 | ||
1278 | if ($broken_encoding{$t} && !is_rfc2047_quoted($subject)) { | |
1279 | $subject = quote_rfc2047($subject, $auto_8bit_encoding); | |
1280 | } | |
1281 | ||
5012699d | 1282 | if (defined $author and $author ne $sender) { |
94638f89 | 1283 | $message = "From: $author\n\n$message"; |
8291db6f JK |
1284 | if (defined $author_encoding) { |
1285 | if ($has_content_type) { | |
1286 | if ($body_encoding eq $author_encoding) { | |
1287 | # ok, we already have the right encoding | |
1288 | } | |
1289 | else { | |
1290 | # uh oh, we should re-encode | |
1291 | } | |
1292 | } | |
1293 | else { | |
3cae7e5b | 1294 | $has_content_type = 1; |
8291db6f JK |
1295 | push @xh, |
1296 | 'MIME-Version: 1.0', | |
8641ee3d JK |
1297 | "Content-Type: text/plain; charset=$author_encoding", |
1298 | 'Content-Transfer-Encoding: 8bit'; | |
8291db6f JK |
1299 | } |
1300 | } | |
8a8e6235 | 1301 | } |
83b24437 | 1302 | |
c1f2aa45 JS |
1303 | $needs_confirm = ( |
1304 | $confirm eq "always" or | |
1305 | ($confirm =~ /^(?:auto|cc)$/ && @cc) or | |
1306 | ($confirm =~ /^(?:auto|compose)$/ && $compose && $message_num == 1)); | |
1307 | $needs_confirm = "inform" if ($needs_confirm && $confirm_unconfigured && @cc); | |
1308 | ||
1309 | @cc = (@initial_cc, @cc); | |
1310 | ||
15da1084 | 1311 | my $message_was_sent = send_message(); |
83b24437 RA |
1312 | |
1313 | # set up for the next message | |
95a877a3 | 1314 | if ($thread && $message_was_sent && |
528fb087 | 1315 | (chain_reply_to() || !defined $reply_to || length($reply_to) == 0)) { |
78488b2c | 1316 | $reply_to = $message_id; |
7ccf7927 | 1317 | if (length $references > 0) { |
a925b89c | 1318 | $references .= "\n $message_id"; |
7ccf7927 RA |
1319 | } else { |
1320 | $references = "$message_id"; | |
1321 | } | |
78488b2c | 1322 | } |
4f3d3703 | 1323 | $message_id = undef; |
83b24437 | 1324 | } |
e205735d | 1325 | |
c1f2aa45 | 1326 | cleanup_compose_files(); |
1f038a0c RA |
1327 | |
1328 | sub cleanup_compose_files() { | |
c1f2aa45 | 1329 | unlink($compose_filename, $compose_filename . ".final") if $compose; |
1f038a0c RA |
1330 | } |
1331 | ||
4bc87a28 | 1332 | $smtp->quit if $smtp; |
e205735d RA |
1333 | |
1334 | sub unique_email_list(@) { | |
1335 | my %seen; | |
1336 | my @emails; | |
1337 | ||
1338 | foreach my $entry (@_) { | |
db3106b2 EW |
1339 | if (my $clean = extract_valid_address($entry)) { |
1340 | $seen{$clean} ||= 0; | |
1341 | next if $seen{$clean}++; | |
1342 | push @emails, $entry; | |
1343 | } else { | |
1344 | print STDERR "W: unable to extract a valid address", | |
1345 | " from: $entry\n"; | |
1346 | } | |
e205735d RA |
1347 | } |
1348 | return @emails; | |
1349 | } | |
747bbff9 JK |
1350 | |
1351 | sub validate_patch { | |
1352 | my $fn = shift; | |
1353 | open(my $fh, '<', $fn) | |
1354 | or die "unable to open $fn: $!\n"; | |
1355 | while (my $line = <$fh>) { | |
1356 | if (length($line) > 998) { | |
1357 | return "$.: patch contains a line longer than 998 characters"; | |
1358 | } | |
1359 | } | |
1360 | return undef; | |
1361 | } | |
0706bd19 JK |
1362 | |
1363 | sub file_has_nonascii { | |
1364 | my $fn = shift; | |
1365 | open(my $fh, '<', $fn) | |
1366 | or die "unable to open $fn: $!\n"; | |
1367 | while (my $line = <$fh>) { | |
1368 | return 1 if $line =~ /[^[:ascii:]]/; | |
1369 | } | |
1370 | return 0; | |
1371 | } | |
3cae7e5b TR |
1372 | |
1373 | sub body_or_subject_has_nonascii { | |
1374 | my $fn = shift; | |
1375 | open(my $fh, '<', $fn) | |
1376 | or die "unable to open $fn: $!\n"; | |
1377 | while (my $line = <$fh>) { | |
1378 | last if $line =~ /^$/; | |
1379 | return 1 if $line =~ /^Subject.*[^[:ascii:]]/; | |
1380 | } | |
1381 | while (my $line = <$fh>) { | |
1382 | return 1 if $line =~ /[^[:ascii:]]/; | |
1383 | } | |
1384 | return 0; | |
1385 | } |