]>
Commit | Line | Data |
---|---|---|
3328aced | 1 | #!/usr/bin/perl |
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 | 18 | |
d48b2841 | 19 | use 5.008; |
83b24437 | 20 | use strict; |
ecc4ee9c | 21 | use warnings $ENV{GIT_PERL_FATAL_WARNINGS} ? qw(FATAL all) : (); |
83b24437 | 22 | use Getopt::Long; |
28654678 | 23 | use Git::LoadCPAN::Error qw(:try); |
3cb8caf7 | 24 | use Git; |
a4dde4c4 | 25 | use Git::I18N; |
83b24437 | 26 | |
5df9fcf6 PH |
27 | Getopt::Long::Configure qw/ pass_through /; |
28 | ||
1b0baf14 MC |
29 | sub usage { |
30 | print <<EOT; | |
a2ce6082 TP |
31 | git send-email' [<options>] <file|directory> |
32 | git send-email' [<options>] <format-patch options> | |
17b7a832 | 33 | git send-email --dump-aliases |
4ed62b03 MW |
34 | |
35 | Composing: | |
36 | --from <str> * Email From: | |
f434c083 SB |
37 | --[no-]to <str> * Email To: |
38 | --[no-]cc <str> * Email Cc: | |
39 | --[no-]bcc <str> * Email Bcc: | |
4ed62b03 | 40 | --subject <str> * Email "Subject:" |
d11c943c | 41 | --reply-to <str> * Email "Reply-To:" |
4ed62b03 | 42 | --in-reply-to <str> * Email "In-Reply-To:" |
ac1596a6 | 43 | --[no-]xmailer * Add "X-Mailer:" header (default). |
402596aa | 44 | --[no-]annotate * Review each patch that will be sent in an editor. |
4ed62b03 | 45 | --compose * Open an editor for introduction. |
62e00690 | 46 | --compose-encoding <str> * Encoding to assume for introduction. |
3cae7e5b | 47 | --8bit-encoding <str> * Encoding to assume 8bit mails if undeclared |
8d814084 | 48 | --transfer-encoding <str> * Transfer encoding to use (quoted-printable, 8bit, base64) |
4ed62b03 MW |
49 | |
50 | Sending: | |
51 | --envelope-sender <str> * Email envelope sender. | |
cd5b33fb | 52 | --sendmail-cmd <str> * Command to run to send email. |
4ed62b03 MW |
53 | --smtp-server <str:int> * Outgoing SMTP server to use. The port |
54 | is optional. Default 'localhost'. | |
052fbea2 | 55 | --smtp-server-option <str> * Outgoing SMTP server option to use. |
4ed62b03 MW |
56 | --smtp-server-port <int> * Outgoing SMTP server port. |
57 | --smtp-user <str> * Username for SMTP-AUTH. | |
58 | --smtp-pass <str> * Password for SMTP-AUTH; not necessary. | |
59 | --smtp-encryption <str> * tls or ssl; anything else disables. | |
60 | --smtp-ssl * Deprecated. Use '--smtp-encryption ssl'. | |
35035bbf RR |
61 | --smtp-ssl-cert-path <str> * Path to ca-certificates (either directory or file). |
62 | Pass an empty string to disable certificate | |
63 | verification. | |
134550fe | 64 | --smtp-domain <str> * The domain name sent to HELO/EHLO handshake |
8dd9b3f8 JW |
65 | --smtp-auth <str> * Space-separated list of allowed AUTH mechanisms, or |
66 | "none" to disable authentication. | |
0f2e68b5 | 67 | This setting forces to use one of the listed mechanisms. |
8dd9b3f8 JW |
68 | --no-smtp-auth Disable SMTP authentication. Shorthand for |
69 | `--smtp-auth=none` | |
f60812ef | 70 | --smtp-debug <0|1> * Disable, enable Net::SMTP debug. |
4ed62b03 | 71 | |
5453b83b | 72 | --batch-size <int> * send max <int> message per connection. |
73 | --relogin-delay <int> * delay <int> seconds between two successive login. | |
74 | This option can only be used with --batch-size | |
75 | ||
4ed62b03 MW |
76 | Automating: |
77 | --identity <str> * Use the sendemail.<id> options. | |
ba92106e MC |
78 | --to-cmd <str> * Email To: via `<str> \$patch_path`. |
79 | --cc-cmd <str> * Email Cc: via `<str> \$patch_path`. | |
80 | --header-cmd <str> * Add headers via `<str> \$patch_path`. | |
81 | --no-header-cmd * Disable any header command in use. | |
ef0cc1df | 82 | --suppress-cc <str> * author, self, sob, cc, cccmd, body, bodycc, misc-by, all. |
f515c904 MT |
83 | --[no-]cc-cover * Email Cc: addresses in the cover letter. |
84 | --[no-]to-cover * Email To: addresses in the cover letter. | |
3531e270 | 85 | --[no-]signed-off-by-cc * Send to Signed-off-by: addresses. Default on. |
4ed62b03 | 86 | --[no-]suppress-from * Send to self. Default off. |
41fe87fa | 87 | --[no-]chain-reply-to * Chain In-Reply-To: fields. Default off. |
4ed62b03 MW |
88 | --[no-]thread * Use In-Reply-To: field. Default on. |
89 | ||
90 | Administering: | |
c1f2aa45 JS |
91 | --confirm <str> * Confirm recipients before sending; |
92 | auto, cc, compose, always, or never. | |
4ed62b03 MW |
93 | --quiet * Output one line of info per email. |
94 | --dry-run * Don't actually send the emails. | |
95 | --[no-]validate * Perform patch sanity checks. Default on. | |
5df9fcf6 PH |
96 | --[no-]format-patch * understand any non optional arguments as |
97 | `git format-patch` ones. | |
a03bc5b6 | 98 | --force * Send even if safety checks would prevent it. |
c764a0c2 | 99 | |
17b7a832 JK |
100 | Information: |
101 | --dump-aliases * Dump configured aliases and exit. | |
102 | ||
1b0baf14 MC |
103 | EOT |
104 | exit(1); | |
105 | } | |
106 | ||
2b7b7585 TP |
107 | sub uniq { |
108 | my %seen; | |
109 | grep !$seen{$_}++, @_; | |
110 | } | |
111 | ||
13374987 | 112 | sub completion_helper { |
2b7b7585 TP |
113 | my ($original_opts) = @_; |
114 | my %not_for_completion = ( | |
115 | "git-completion-helper" => undef, | |
116 | "h" => undef, | |
117 | ); | |
118 | my @send_email_opts = (); | |
119 | ||
120 | foreach my $key (keys %$original_opts) { | |
121 | unless (exists $not_for_completion{$key}) { | |
122 | $key =~ s/!$//; | |
123 | ||
124 | if ($key =~ /[:=][si]$/) { | |
125 | $key =~ s/[:=][si]$//; | |
126 | push (@send_email_opts, "--$_=") foreach (split (/\|/, $key)); | |
127 | } else { | |
128 | push (@send_email_opts, "--$_") foreach (split (/\|/, $key)); | |
129 | } | |
130 | } | |
131 | } | |
132 | ||
133 | my @format_patch_opts = split(/ /, Git::command('format-patch', '--git-completion-helper')); | |
134 | my @opts = (@send_email_opts, @format_patch_opts); | |
135 | @opts = uniq (grep !/^$/, @opts); | |
136 | # There's an implicit '\n' here already, no need to add an explicit one. | |
137 | print "@opts"; | |
138 | exit(0); | |
13374987 NTND |
139 | } |
140 | ||
4bc87a28 | 141 | # most mail servers generate the Date: header, but not all... |
6bdca890 JN |
142 | sub format_2822_time { |
143 | my ($time) = @_; | |
144 | my @localtm = localtime($time); | |
145 | my @gmttm = gmtime($time); | |
146 | my $localmin = $localtm[1] + $localtm[2] * 60; | |
147 | my $gmtmin = $gmttm[1] + $gmttm[2] * 60; | |
148 | if ($localtm[0] != $gmttm[0]) { | |
46493105 | 149 | die __("local zone differs from GMT by a non-minute interval\n"); |
6bdca890 JN |
150 | } |
151 | if ((($gmttm[6] + 1) % 7) == $localtm[6]) { | |
152 | $localmin += 1440; | |
153 | } elsif ((($gmttm[6] - 1) % 7) == $localtm[6]) { | |
154 | $localmin -= 1440; | |
155 | } elsif ($gmttm[6] != $localtm[6]) { | |
46493105 | 156 | die __("local time offset greater than or equal to 24 hours\n"); |
6bdca890 JN |
157 | } |
158 | my $offset = $localmin - $gmtmin; | |
159 | my $offhour = $offset / 60; | |
160 | my $offmin = abs($offset % 60); | |
161 | if (abs($offhour) >= 24) { | |
46493105 | 162 | die __("local time offset greater than or equal to 24 hours\n"); |
6bdca890 JN |
163 | } |
164 | ||
165 | return sprintf("%s, %2d %s %d %02d:%02d:%02d %s%02d%02d", | |
166 | qw(Sun Mon Tue Wed Thu Fri Sat)[$localtm[6]], | |
167 | $localtm[3], | |
168 | qw(Jan Feb Mar Apr May Jun | |
169 | Jul Aug Sep Oct Nov Dec)[$localtm[4]], | |
170 | $localtm[5]+1900, | |
171 | $localtm[2], | |
172 | $localtm[1], | |
173 | $localtm[0], | |
174 | ($offset >= 0) ? '+' : '-', | |
175 | abs($offhour), | |
176 | $offmin, | |
177 | ); | |
178 | } | |
4bc87a28 EW |
179 | |
180 | my $smtp; | |
5f5b6118 | 181 | my $auth; |
5453b83b | 182 | my $num_sent = 0; |
4bc87a28 | 183 | |
11f70a7e РД |
184 | # Regexes for RFC 2047 productions. |
185 | my $re_token = qr/[^][()<>@,;:\\"\/?.= \000-\037\177-\377]+/; | |
186 | my $re_encoded_text = qr/[^? \000-\037\177-\377]+/; | |
187 | my $re_encoded_word = qr/=\?($re_token)\?($re_token)\?($re_encoded_text)\?=/; | |
188 | ||
83b24437 | 189 | # Variables we fill in automatically, or via prompting: |
3494dfd3 | 190 | my (@to,@cc,@xh,$envelope_sender, |
d11c943c | 191 | $initial_in_reply_to,$reply_to,$initial_subject,@files, |
3494dfd3 ÆAB |
192 | $author,$sender,$smtp_authpass,$annotate,$compose,$time); |
193 | # Things we either get from config, *or* are overridden on the | |
194 | # command-line. | |
ba92106e | 195 | my ($no_cc, $no_to, $no_bcc, $no_identity, $no_header_cmd); |
3494dfd3 ÆAB |
196 | my (@config_to, @getopt_to); |
197 | my (@config_cc, @getopt_cc); | |
198 | my (@config_bcc, @getopt_bcc); | |
78488b2c | 199 | |
9133261f | 200 | # Example reply to: |
15dc3b91 | 201 | #$initial_in_reply_to = ''; #<20050203173208.GA23964@foobar.com>'; |
83b24437 | 202 | |
ad79c024 FL |
203 | my $repo = eval { Git->repository() }; |
204 | my @repo = $repo ? ($repo) : (); | |
83b24437 | 205 | |
5483c71d AR |
206 | # Behavior modification variables |
207 | my ($quiet, $dry_run) = (0, 0); | |
5df9fcf6 | 208 | my $format_patch; |
afe756c9 | 209 | my $compose_filename; |
a03bc5b6 | 210 | my $force = 0; |
17b7a832 | 211 | my $dump_aliases = 0; |
5483c71d | 212 | |
8774aa56 KM |
213 | # Variables to prevent short format-patch options from being captured |
214 | # as abbreviated send-email options | |
215 | my $reroll_count; | |
216 | ||
8fd5bb7f PH |
217 | # Handle interactive edition of files. |
218 | my $multiedit; | |
0ce142c9 | 219 | my $editor; |
b4479f07 | 220 | |
d21616c0 | 221 | sub system_or_msg { |
a7555304 | 222 | my ($args, $msg, $cmd_name) = @_; |
d21616c0 ÆAB |
223 | system(@$args); |
224 | my $signalled = $? & 127; | |
225 | my $exit_code = $? >> 8; | |
226 | return unless $signalled or $exit_code; | |
227 | ||
a7555304 | 228 | my @sprintf_args = ($cmd_name ? $cmd_name : $args->[0], $exit_code); |
5b719b75 ÆAB |
229 | if (defined $msg) { |
230 | # Quiet the 'redundant' warning category, except we | |
231 | # need to support down to Perl 5.8, so we can't do a | |
232 | # "no warnings 'redundant'", since that category was | |
233 | # introduced in perl 5.22, and asking for it will die | |
234 | # on older perls. | |
235 | no warnings; | |
236 | return sprintf($msg, @sprintf_args); | |
237 | } | |
ea7811b3 | 238 | return sprintf(__("fatal: command '%s' died with exit code %d"), |
5b719b75 | 239 | @sprintf_args); |
d21616c0 ÆAB |
240 | } |
241 | ||
242 | sub system_or_die { | |
243 | my $msg = system_or_msg(@_); | |
244 | die $msg if $msg; | |
245 | } | |
246 | ||
8fd5bb7f | 247 | sub do_edit { |
0ce142c9 MG |
248 | if (!defined($editor)) { |
249 | $editor = Git::command_oneline('var', 'GIT_EDITOR'); | |
250 | } | |
d21616c0 | 251 | my $die_msg = __("the editor exited uncleanly, aborting everything"); |
8fd5bb7f | 252 | if (defined($multiedit) && !$multiedit) { |
d21616c0 | 253 | system_or_die(['sh', '-c', $editor.' "$@"', $editor, $_], $die_msg) for @_; |
8fd5bb7f | 254 | } else { |
d21616c0 | 255 | system_or_die(['sh', '-c', $editor.' "$@"', $editor, @_], $die_msg); |
8fd5bb7f PH |
256 | } |
257 | } | |
5483c71d AR |
258 | |
259 | # Variables with corresponding config settings | |
3494dfd3 | 260 | my ($suppress_from, $signed_off_by_cc); |
f515c904 | 261 | my ($cover_cc, $cover_to); |
ba92106e | 262 | my ($to_cmd, $cc_cmd, $header_cmd); |
052fbea2 | 263 | my ($smtp_server, $smtp_server_port, @smtp_server_options); |
35035bbf | 264 | my ($smtp_authuser, $smtp_encryption, $smtp_ssl_cert_path); |
5453b83b | 265 | my ($batch_size, $relogin_delay); |
0f2e68b5 | 266 | my ($identity, $aliasfiletype, @alias_files, $smtp_domain, $smtp_auth); |
3494dfd3 | 267 | my ($confirm); |
65648283 | 268 | my (@suppress_cc); |
3cae7e5b | 269 | my ($auto_8bit_encoding); |
62e00690 | 270 | my ($compose_encoding); |
cd5b33fb | 271 | my ($sendmail_cmd); |
3494dfd3 | 272 | # Variables with corresponding config settings & hardcoded defaults |
f60812ef | 273 | my ($debug_net_smtp) = 0; # Net::SMTP, see send_message() |
3494dfd3 ÆAB |
274 | my $thread = 1; |
275 | my $chain_reply_to = 0; | |
276 | my $use_xmailer = 1; | |
277 | my $validate = 1; | |
278 | my $target_xfer_encoding = 'auto'; | |
dd84e528 | 279 | my $forbid_sendmail_variables = 1; |
f60812ef | 280 | |
34cc60ce | 281 | my %config_bool_settings = ( |
3494dfd3 ÆAB |
282 | "thread" => \$thread, |
283 | "chainreplyto" => \$chain_reply_to, | |
284 | "suppressfrom" => \$suppress_from, | |
285 | "signedoffbycc" => \$signed_off_by_cc, | |
286 | "cccover" => \$cover_cc, | |
287 | "tocover" => \$cover_to, | |
288 | "signedoffcc" => \$signed_off_by_cc, | |
289 | "validate" => \$validate, | |
290 | "multiedit" => \$multiedit, | |
291 | "annotate" => \$annotate, | |
292 | "xmailer" => \$use_xmailer, | |
dd84e528 | 293 | "forbidsendmailvariables" => \$forbid_sendmail_variables, |
e46f7a0e AR |
294 | ); |
295 | ||
34cc60ce | 296 | my %config_settings = ( |
119974e9 | 297 | "smtpencryption" => \$smtp_encryption, |
34cc60ce | 298 | "smtpserver" => \$smtp_server, |
44b2476a | 299 | "smtpserverport" => \$smtp_server_port, |
052fbea2 | 300 | "smtpserveroption" => \@smtp_server_options, |
34cc60ce DS |
301 | "smtpuser" => \$smtp_authuser, |
302 | "smtppass" => \$smtp_authpass, | |
e1e9115d | 303 | "smtpdomain" => \$smtp_domain, |
0f2e68b5 | 304 | "smtpauth" => \$smtp_auth, |
5453b83b | 305 | "smtpbatchsize" => \$batch_size, |
306 | "smtprelogindelay" => \$relogin_delay, | |
3494dfd3 | 307 | "to" => \@config_to, |
6e74e075 | 308 | "tocmd" => \$to_cmd, |
3494dfd3 | 309 | "cc" => \@config_cc, |
34cc60ce | 310 | "cccmd" => \$cc_cmd, |
ba92106e | 311 | "headercmd" => \$header_cmd, |
34cc60ce | 312 | "aliasfiletype" => \$aliasfiletype, |
3494dfd3 | 313 | "bcc" => \@config_bcc, |
65648283 | 314 | "suppresscc" => \@suppress_cc, |
9f7820ae | 315 | "envelopesender" => \$envelope_sender, |
c1f2aa45 | 316 | "confirm" => \$confirm, |
09caa24f | 317 | "from" => \$sender, |
3cae7e5b | 318 | "assume8bitencoding" => \$auto_8bit_encoding, |
62e00690 | 319 | "composeencoding" => \$compose_encoding, |
8d814084 | 320 | "transferencoding" => \$target_xfer_encoding, |
cd5b33fb | 321 | "sendmailcmd" => \$sendmail_cmd, |
34cc60ce | 322 | ); |
4a62d3f5 | 323 | |
cec5dae8 CS |
324 | my %config_path_settings = ( |
325 | "aliasesfile" => \@alias_files, | |
6e07a3b5 | 326 | "smtpsslcertpath" => \$smtp_ssl_cert_path, |
cec5dae8 CS |
327 | ); |
328 | ||
87429976 MW |
329 | # Handle Uncouth Termination |
330 | sub signal_handler { | |
87429976 | 331 | # Make text normal |
f4dc9432 ÆAB |
332 | require Term::ANSIColor; |
333 | print Term::ANSIColor::color("reset"), "\n"; | |
87429976 MW |
334 | |
335 | # SMTP password masked | |
336 | system "stty echo"; | |
337 | ||
338 | # tmp files from --compose | |
afe756c9 JS |
339 | if (defined $compose_filename) { |
340 | if (-e $compose_filename) { | |
3c5cd20c VA |
341 | printf __("'%s' contains an intermediate version ". |
342 | "of the email you were composing.\n"), | |
343 | $compose_filename; | |
afe756c9 JS |
344 | } |
345 | if (-e ($compose_filename . ".final")) { | |
3c5cd20c VA |
346 | printf __("'%s.final' contains the composed email.\n"), |
347 | $compose_filename; | |
afe756c9 | 348 | } |
87429976 MW |
349 | } |
350 | ||
351 | exit; | |
352 | }; | |
353 | ||
354 | $SIG{TERM} = \&signal_handler; | |
355 | $SIG{INT} = \&signal_handler; | |
356 | ||
c573572c ÆAB |
357 | # Read our sendemail.* config |
358 | sub read_config { | |
9264d29b | 359 | my ($known_keys, $configured, $prefix) = @_; |
c573572c ÆAB |
360 | |
361 | foreach my $setting (keys %config_bool_settings) { | |
3494dfd3 | 362 | my $target = $config_bool_settings{$setting}; |
9264d29b ÆAB |
363 | my $key = "$prefix.$setting"; |
364 | next unless exists $known_keys->{$key}; | |
c95e3a3f ÆAB |
365 | my $v = (@{$known_keys->{$key}} == 1 && |
366 | (defined $known_keys->{$key}->[0] && | |
367 | $known_keys->{$key}->[0] =~ /^(?:true|false)$/s)) | |
368 | ? $known_keys->{$key}->[0] eq 'true' | |
369 | : Git::config_bool(@repo, $key); | |
3ff15040 ÆAB |
370 | next unless defined $v; |
371 | next if $configured->{$setting}++; | |
372 | $$target = $v; | |
c573572c ÆAB |
373 | } |
374 | ||
375 | foreach my $setting (keys %config_path_settings) { | |
376 | my $target = $config_path_settings{$setting}; | |
9264d29b ÆAB |
377 | my $key = "$prefix.$setting"; |
378 | next unless exists $known_keys->{$key}; | |
c573572c | 379 | if (ref($target) eq "ARRAY") { |
9264d29b | 380 | my @values = Git::config_path(@repo, $key); |
3ff15040 ÆAB |
381 | next unless @values; |
382 | next if $configured->{$setting}++; | |
383 | @$target = @values; | |
c573572c ÆAB |
384 | } |
385 | else { | |
3494dfd3 | 386 | my $v = Git::config_path(@repo, "$prefix.$setting"); |
3ff15040 ÆAB |
387 | next unless defined $v; |
388 | next if $configured->{$setting}++; | |
389 | $$target = $v; | |
c573572c ÆAB |
390 | } |
391 | } | |
392 | ||
393 | foreach my $setting (keys %config_settings) { | |
394 | my $target = $config_settings{$setting}; | |
9264d29b ÆAB |
395 | my $key = "$prefix.$setting"; |
396 | next unless exists $known_keys->{$key}; | |
c573572c | 397 | if (ref($target) eq "ARRAY") { |
c95e3a3f ÆAB |
398 | my @values = @{$known_keys->{$key}}; |
399 | @values = grep { defined } @values; | |
3ff15040 ÆAB |
400 | next if $configured->{$setting}++; |
401 | @$target = @values; | |
c573572c ÆAB |
402 | } |
403 | else { | |
b996f849 | 404 | my $v = $known_keys->{$key}->[-1]; |
3ff15040 ÆAB |
405 | next unless defined $v; |
406 | next if $configured->{$setting}++; | |
407 | $$target = $v; | |
c573572c ÆAB |
408 | } |
409 | } | |
c573572c | 410 | } |
c573572c | 411 | |
2b110e9b ÆAB |
412 | sub config_regexp { |
413 | my ($regex) = @_; | |
414 | my @ret; | |
415 | eval { | |
c95e3a3f | 416 | my $ret = Git::command( |
2b110e9b | 417 | 'config', |
c95e3a3f | 418 | '--null', |
2b110e9b ÆAB |
419 | '--get-regexp', |
420 | $regex, | |
421 | ); | |
c95e3a3f ÆAB |
422 | @ret = map { |
423 | # We must always return ($k, $v) here, since | |
424 | # empty config values will be just "key\0", | |
425 | # not "key\nvalue\0". | |
426 | my ($k, $v) = split /\n/, $_, 2; | |
427 | ($k, $v); | |
428 | } split /\0/, $ret; | |
2b110e9b ÆAB |
429 | 1; |
430 | } or do { | |
431 | # If we have no keys we're OK, otherwise re-throw | |
432 | die $@ if $@->value != 1; | |
433 | }; | |
434 | return @ret; | |
435 | } | |
436 | ||
9264d29b ÆAB |
437 | # Save ourselves a lot of work of shelling out to 'git config' (it |
438 | # parses 'bool' etc.) by only doing so for config keys that exist. | |
439 | my %known_config_keys; | |
440 | { | |
c95e3a3f ÆAB |
441 | my @kv = config_regexp("^sende?mail[.]"); |
442 | while (my ($k, $v) = splice @kv, 0, 2) { | |
443 | push @{$known_config_keys{$k}} => $v; | |
c573572c ÆAB |
444 | } |
445 | } | |
446 | ||
3ff15040 ÆAB |
447 | # sendemail.identity yields to --identity. We must parse this |
448 | # special-case first before the rest of the config is read. | |
9264d29b ÆAB |
449 | { |
450 | my $key = "sendemail.identity"; | |
451 | $identity = Git::config(@repo, $key) if exists $known_config_keys{$key}; | |
452 | } | |
2b7b7585 | 453 | my %identity_options = ( |
3ff15040 ÆAB |
454 | "identity=s" => \$identity, |
455 | "no-identity" => \$no_identity, | |
456 | ); | |
2b7b7585 | 457 | my $rc = GetOptions(%identity_options); |
3ff15040 ÆAB |
458 | usage() unless $rc; |
459 | undef $identity if $no_identity; | |
460 | ||
461 | # Now we know enough to read the config | |
462 | { | |
463 | my %configured; | |
9264d29b ÆAB |
464 | read_config(\%known_config_keys, \%configured, "sendemail.$identity") if defined $identity; |
465 | read_config(\%known_config_keys, \%configured, "sendemail"); | |
3ff15040 | 466 | } |
3494dfd3 | 467 | |
83b24437 RA |
468 | # Begin by accumulating all the variables (defined above), that we will end up |
469 | # needing, first, from the command line: | |
470 | ||
c5978246 | 471 | my $help; |
13374987 | 472 | my $git_completion_helper; |
2b7b7585 TP |
473 | my %dump_aliases_options = ( |
474 | "h" => \$help, | |
475 | "dump-aliases" => \$dump_aliases, | |
476 | ); | |
477 | $rc = GetOptions(%dump_aliases_options); | |
17b7a832 | 478 | usage() unless $rc; |
46493105 | 479 | die __("--dump-aliases incompatible with other options\n") |
17b7a832 | 480 | if !$help and $dump_aliases and @ARGV; |
2b7b7585 | 481 | my %options = ( |
c5978246 | 482 | "sender|from=s" => \$sender, |
2b7b7585 | 483 | "in-reply-to=s" => \$initial_in_reply_to, |
d11c943c | 484 | "reply-to=s" => \$reply_to, |
83b24437 | 485 | "subject=s" => \$initial_subject, |
3494dfd3 | 486 | "to=s" => \@getopt_to, |
6e74e075 | 487 | "to-cmd=s" => \$to_cmd, |
f434c083 | 488 | "no-to" => \$no_to, |
3494dfd3 | 489 | "cc=s" => \@getopt_cc, |
f434c083 | 490 | "no-cc" => \$no_cc, |
3494dfd3 | 491 | "bcc=s" => \@getopt_bcc, |
f434c083 | 492 | "no-bcc" => \$no_bcc, |
78488b2c | 493 | "chain-reply-to!" => \$chain_reply_to, |
f4714943 | 494 | "no-chain-reply-to" => sub {$chain_reply_to = 0}, |
cd5b33fb | 495 | "sendmail-cmd=s" => \$sendmail_cmd, |
3342d850 | 496 | "smtp-server=s" => \$smtp_server, |
052fbea2 | 497 | "smtp-server-option=s" => \@smtp_server_options, |
44b2476a | 498 | "smtp-server-port=s" => \$smtp_server_port, |
34cc60ce | 499 | "smtp-user=s" => \$smtp_authuser, |
2363d746 | 500 | "smtp-pass:s" => \$smtp_authpass, |
f6bebd12 TR |
501 | "smtp-ssl" => sub { $smtp_encryption = 'ssl' }, |
502 | "smtp-encryption=s" => \$smtp_encryption, | |
979e652a | 503 | "smtp-ssl-cert-path=s" => \$smtp_ssl_cert_path, |
f60812ef | 504 | "smtp-debug:i" => \$debug_net_smtp, |
69cf7bfd | 505 | "smtp-domain:s" => \$smtp_domain, |
0f2e68b5 | 506 | "smtp-auth=s" => \$smtp_auth, |
8dd9b3f8 | 507 | "no-smtp-auth" => sub {$smtp_auth = 'none'}, |
402596aa | 508 | "annotate!" => \$annotate, |
f4714943 | 509 | "no-annotate" => sub {$annotate = 0}, |
1f038a0c | 510 | "compose" => \$compose, |
30d08b34 | 511 | "quiet" => \$quiet, |
324a8bd0 | 512 | "cc-cmd=s" => \$cc_cmd, |
ba92106e MC |
513 | "header-cmd=s" => \$header_cmd, |
514 | "no-header-cmd" => \$no_header_cmd, | |
5483c71d | 515 | "suppress-from!" => \$suppress_from, |
f4714943 | 516 | "no-suppress-from" => sub {$suppress_from = 0}, |
65648283 | 517 | "suppress-cc=s" => \@suppress_cc, |
ddc3d4fe | 518 | "signed-off-cc|signed-off-by-cc!" => \$signed_off_by_cc, |
f4714943 | 519 | "no-signed-off-cc|no-signed-off-by-cc" => sub {$signed_off_by_cc = 0}, |
f515c904 | 520 | "cc-cover|cc-cover!" => \$cover_cc, |
f4714943 | 521 | "no-cc-cover" => sub {$cover_cc = 0}, |
f515c904 | 522 | "to-cover|to-cover!" => \$cover_to, |
f4714943 | 523 | "no-to-cover" => sub {$cover_to = 0}, |
c1f2aa45 | 524 | "confirm=s" => \$confirm, |
6130259c | 525 | "dry-run" => \$dry_run, |
f073a592 | 526 | "envelope-sender=s" => \$envelope_sender, |
5483c71d | 527 | "thread!" => \$thread, |
f4714943 | 528 | "no-thread" => sub {$thread = 0}, |
dbf5e1e9 | 529 | "validate!" => \$validate, |
f4714943 | 530 | "no-validate" => sub {$validate = 0}, |
8d814084 | 531 | "transfer-encoding=s" => \$target_xfer_encoding, |
5df9fcf6 | 532 | "format-patch!" => \$format_patch, |
f4714943 | 533 | "no-format-patch" => sub {$format_patch = 0}, |
3cae7e5b | 534 | "8bit-encoding=s" => \$auto_8bit_encoding, |
62e00690 | 535 | "compose-encoding=s" => \$compose_encoding, |
a03bc5b6 | 536 | "force" => \$force, |
ac1596a6 | 537 | "xmailer!" => \$use_xmailer, |
f4714943 | 538 | "no-xmailer" => sub {$use_xmailer = 0}, |
5453b83b | 539 | "batch-size=i" => \$batch_size, |
540 | "relogin-delay=i" => \$relogin_delay, | |
13374987 | 541 | "git-completion-helper" => \$git_completion_helper, |
8774aa56 | 542 | "v=s" => \$reroll_count, |
2b7b7585 TP |
543 | ); |
544 | $rc = GetOptions(%options); | |
83b24437 | 545 | |
3494dfd3 ÆAB |
546 | # Munge any "either config or getopt, not both" variables |
547 | my @initial_to = @getopt_to ? @getopt_to : ($no_to ? () : @config_to); | |
548 | my @initial_cc = @getopt_cc ? @getopt_cc : ($no_cc ? () : @config_cc); | |
549 | my @initial_bcc = @getopt_bcc ? @getopt_bcc : ($no_bcc ? () : @config_bcc); | |
550 | ||
c5978246 | 551 | usage() if $help; |
2b7b7585 TP |
552 | my %all_options = (%options, %dump_aliases_options, %identity_options); |
553 | completion_helper(\%all_options) if $git_completion_helper; | |
1b0baf14 MC |
554 | unless ($rc) { |
555 | usage(); | |
556 | } | |
557 | ||
9264d29b | 558 | if ($forbid_sendmail_variables && grep { /^sendmail/s } keys %known_config_keys) { |
dd84e528 DD |
559 | die __("fatal: found configuration options for 'sendmail'\n" . |
560 | "git-send-email is configured with the sendemail.* options - note the 'e'.\n" . | |
561 | "Set sendemail.forbidSendmailVariables to false to disable this check.\n"); | |
562 | } | |
563 | ||
46493105 | 564 | die __("Cannot run git format-patch from outside a repository\n") |
eed6ca7c JS |
565 | if $format_patch and not $repo; |
566 | ||
9caa7069 SB |
567 | die __("`batch-size` and `relogin` must be specified together " . |
568 | "(via command-line or configuration option)\n") | |
569 | if defined $relogin_delay and not defined $batch_size; | |
570 | ||
fa835cd5 TR |
571 | # 'default' encryption is none -- this only prevents a warning |
572 | $smtp_encryption = '' unless (defined $smtp_encryption); | |
573 | ||
65648283 DB |
574 | # Set CC suppressions |
575 | my(%suppress_cc); | |
576 | if (@suppress_cc) { | |
577 | foreach my $entry (@suppress_cc) { | |
5a59a230 NTND |
578 | # Please update $__git_send_email_suppresscc_options |
579 | # in git-completion.bash when you add new options. | |
3c5cd20c | 580 | die sprintf(__("Unknown --suppress-cc field: '%s'\n"), $entry) |
ef0cc1df | 581 | unless $entry =~ /^(?:all|cccmd|cc|author|self|sob|body|bodycc|misc-by)$/; |
65648283 DB |
582 | $suppress_cc{$entry} = 1; |
583 | } | |
584 | } | |
585 | ||
586 | if ($suppress_cc{'all'}) { | |
ef0cc1df | 587 | foreach my $entry (qw (cccmd cc author self sob body bodycc misc-by)) { |
65648283 DB |
588 | $suppress_cc{$entry} = 1; |
589 | } | |
590 | delete $suppress_cc{'all'}; | |
591 | } | |
592 | ||
593 | # If explicit old-style ones are specified, they trump --suppress-cc. | |
594 | $suppress_cc{'self'} = $suppress_from if defined $suppress_from; | |
ddc3d4fe | 595 | $suppress_cc{'sob'} = !$signed_off_by_cc if defined $signed_off_by_cc; |
65648283 | 596 | |
3531e270 | 597 | if ($suppress_cc{'body'}) { |
ef0cc1df | 598 | foreach my $entry (qw (sob bodycc misc-by)) { |
3531e270 JS |
599 | $suppress_cc{$entry} = 1; |
600 | } | |
601 | delete $suppress_cc{'body'}; | |
602 | } | |
603 | ||
c1f2aa45 JS |
604 | # Set confirm's default value |
605 | my $confirm_unconfigured = !defined $confirm; | |
606 | if ($confirm_unconfigured) { | |
607 | $confirm = scalar %suppress_cc ? 'compose' : 'auto'; | |
608 | }; | |
5a59a230 NTND |
609 | # Please update $__git_send_email_confirm_options in |
610 | # git-completion.bash when you add new options. | |
3c5cd20c | 611 | die sprintf(__("Unknown --confirm setting: '%s'\n"), $confirm) |
c1f2aa45 JS |
612 | unless $confirm =~ /^(?:auto|cc|compose|always|never)/; |
613 | ||
65648283 DB |
614 | # Debugging, print out the suppressions. |
615 | if (0) { | |
616 | print "suppressions:\n"; | |
617 | foreach my $entry (keys %suppress_cc) { | |
618 | printf " %-5s -> $suppress_cc{$entry}\n", $entry; | |
619 | } | |
620 | } | |
621 | ||
ad79c024 | 622 | my ($repoauthor, $repocommitter); |
fef381e6 ÆAB |
623 | { |
624 | my %cache; | |
625 | my ($author, $committer); | |
626 | my $common = sub { | |
627 | my ($what) = @_; | |
628 | return $cache{$what} if exists $cache{$what}; | |
629 | ($cache{$what}) = Git::ident_person(@repo, $what); | |
630 | return $cache{$what}; | |
631 | }; | |
632 | $repoauthor = sub { $common->('author') }; | |
633 | $repocommitter = sub { $common->('committer') }; | |
634 | } | |
34cc60ce | 635 | |
5012699d | 636 | sub parse_address_line { |
f4dc9432 | 637 | require Git::LoadCPAN::Mail::Address; |
bd869f67 | 638 | return map { $_->format } Mail::Address->parse($_[0]); |
5012699d JS |
639 | } |
640 | ||
0e73b3ee | 641 | sub split_addrs { |
f4dc9432 ÆAB |
642 | require Text::ParseWords; |
643 | return Text::ParseWords::quotewords('\s*,\s*', 1, @_); | |
0e73b3ee WF |
644 | } |
645 | ||
994d6c66 | 646 | my %aliases; |
09f1157b ES |
647 | |
648 | sub parse_sendmail_alias { | |
649 | local $_ = shift; | |
650 | if (/"/) { | |
3c5cd20c | 651 | printf STDERR __("warning: sendmail alias with quotes is not supported: %s\n"), $_; |
86b89848 | 652 | } elsif (/:include:/) { |
3c5cd20c | 653 | printf STDERR __("warning: `:include:` not supported: %s\n"), $_; |
86b89848 | 654 | } elsif (/[\/|]/) { |
3c5cd20c | 655 | printf STDERR __("warning: `/file` or `|pipe` redirection not supported: %s\n"), $_; |
09f1157b ES |
656 | } elsif (/^(\S+?)\s*:\s*(.+)$/) { |
657 | my ($alias, $addr) = ($1, $2); | |
658 | $aliases{$alias} = [ split_addrs($addr) ]; | |
659 | } else { | |
3c5cd20c | 660 | printf STDERR __("warning: sendmail line is not recognized: %s\n"), $_; |
09f1157b ES |
661 | } |
662 | } | |
663 | ||
664 | sub parse_sendmail_aliases { | |
665 | my $fh = shift; | |
2532dd06 | 666 | my $s = ''; |
09f1157b | 667 | while (<$fh>) { |
2532dd06 | 668 | chomp; |
020be85f | 669 | next if /^\s*$/ || /^\s*#/; |
2532dd06 ES |
670 | $s .= $_, next if $s =~ s/\\$// || s/^\s+//; |
671 | parse_sendmail_alias($s) if $s; | |
672 | $s = $_; | |
09f1157b | 673 | } |
2532dd06 ES |
674 | $s =~ s/\\$//; # silently tolerate stray '\' on last line |
675 | parse_sendmail_alias($s) if $s; | |
09f1157b ES |
676 | } |
677 | ||
994d6c66 EW |
678 | my %parse_alias = ( |
679 | # multiline formats can be supported in the future | |
680 | mutt => sub { my $fh = shift; while (<$fh>) { | |
ffc01f9b | 681 | if (/^\s*alias\s+(?:-group\s+\S+\s+)*(\S+)\s+(.*)$/) { |
994d6c66 EW |
682 | my ($alias, $addr) = ($1, $2); |
683 | $addr =~ s/#.*$//; # mutt allows # comments | |
2c510f21 EW |
684 | # commas delimit multiple addresses |
685 | my @addr = split_addrs($addr); | |
686 | ||
687 | # quotes may be escaped in the file, | |
688 | # unescape them so we do not double-escape them later. | |
689 | s/\\"/"/g foreach @addr; | |
690 | $aliases{$alias} = \@addr | |
994d6c66 | 691 | }}}, |
f4dc9432 | 692 | mailrc => sub { my $fh = shift; while (<$fh>) { |
a277d1ef | 693 | if (/^alias\s+(\S+)\s+(.*?)\s*$/) { |
f4dc9432 | 694 | require Text::ParseWords; |
994d6c66 | 695 | # spaces delimit multiple addresses |
f4dc9432 | 696 | $aliases{$1} = [ Text::ParseWords::quotewords('\s+', 0, $2) ]; |
994d6c66 | 697 | }}}, |
73c427eb TP |
698 | pine => sub { my $fh = shift; my $f='\t[^\t]*'; |
699 | for (my $x = ''; defined($x); $x = $_) { | |
700 | chomp $x; | |
701 | $x .= $1 while(defined($_ = <$fh>) && /^ +(.*)$/); | |
702 | $x =~ /^(\S+)$f\t\(?([^\t]+?)\)?(:?$f){0,2}$/ or next; | |
0e73b3ee | 703 | $aliases{$1} = [ split_addrs($2) ]; |
73c427eb | 704 | }}, |
7613ea35 BP |
705 | elm => sub { my $fh = shift; |
706 | while (<$fh>) { | |
707 | if (/^(\S+)\s+=\s+[^=]+=\s(\S+)/) { | |
708 | my ($alias, $addr) = ($1, $2); | |
709 | $aliases{$alias} = [ split_addrs($addr) ]; | |
710 | } | |
711 | } }, | |
09f1157b | 712 | sendmail => \&parse_sendmail_aliases, |
994d6c66 EW |
713 | gnus => sub { my $fh = shift; while (<$fh>) { |
714 | if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) { | |
715 | $aliases{$1} = [ $2 ]; | |
716 | }}} | |
5a59a230 NTND |
717 | # Please update _git_config() in git-completion.bash when you |
718 | # add new MUAs. | |
994d6c66 EW |
719 | ); |
720 | ||
3cb8caf7 | 721 | if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) { |
994d6c66 EW |
722 | foreach my $file (@alias_files) { |
723 | open my $fh, '<', $file or die "opening $file: $!\n"; | |
724 | $parse_alias{$aliasfiletype}->($fh); | |
725 | close $fh; | |
726 | } | |
727 | } | |
728 | ||
17b7a832 JK |
729 | if ($dump_aliases) { |
730 | print "$_\n" for (sort keys %aliases); | |
731 | exit(0); | |
732 | } | |
733 | ||
9b397039 RR |
734 | # is_format_patch_arg($f) returns 0 if $f names a patch, or 1 if |
735 | # $f is a revision list specification to be passed to format-patch. | |
736 | sub is_format_patch_arg { | |
eed6ca7c | 737 | return unless $repo; |
5df9fcf6 PH |
738 | my $f = shift; |
739 | try { | |
740 | $repo->command('rev-parse', '--verify', '--quiet', $f); | |
741 | if (defined($format_patch)) { | |
5df9fcf6 PH |
742 | return $format_patch; |
743 | } | |
4adbf387 | 744 | die sprintf(__(<<EOF), $f, $f); |
3c5cd20c | 745 | File '%s' exists but it could also be the range of commits |
5df9fcf6 PH |
746 | to produce patches for. Please disambiguate by... |
747 | ||
3c5cd20c | 748 | * Saying "./%s" if you mean a file; or |
5df9fcf6 PH |
749 | * Giving --format-patch option if you mean a range. |
750 | EOF | |
751 | } catch Git::Error::Command with { | |
9b397039 | 752 | # Not a valid revision. Treat it as a filename. |
5df9fcf6 PH |
753 | return 0; |
754 | } | |
755 | } | |
756 | ||
aa54892f JK |
757 | # Now that all the defaults are set, process the rest of the command line |
758 | # arguments and collect up the files that need to be processed. | |
5df9fcf6 | 759 | my @rev_list_opts; |
69f4ce55 | 760 | while (defined(my $f = shift @ARGV)) { |
5df9fcf6 PH |
761 | if ($f eq "--") { |
762 | push @rev_list_opts, "--", @ARGV; | |
763 | @ARGV = (); | |
9b397039 | 764 | } elsif (-d $f and !is_format_patch_arg($f)) { |
c6038169 | 765 | opendir my $dh, $f |
3c5cd20c | 766 | or die sprintf(__("Failed to opendir %s: %s"), $f, $!); |
aa54892f | 767 | |
f4dc9432 ÆAB |
768 | require File::Spec; |
769 | push @files, grep { -f $_ } map { File::Spec->catfile($f, $_) } | |
c6038169 ÆAB |
770 | sort readdir $dh; |
771 | closedir $dh; | |
9b397039 | 772 | } elsif ((-f $f or -p $f) and !is_format_patch_arg($f)) { |
aa54892f | 773 | push @files, $f; |
aa54892f | 774 | } else { |
5df9fcf6 | 775 | push @rev_list_opts, $f; |
aa54892f JK |
776 | } |
777 | } | |
778 | ||
5df9fcf6 | 779 | if (@rev_list_opts) { |
46493105 | 780 | die __("Cannot run git format-patch from outside a repository\n") |
eed6ca7c | 781 | unless $repo; |
f4dc9432 | 782 | require File::Temp; |
8774aa56 KM |
783 | push @files, $repo->command('format-patch', '-o', File::Temp::tempdir(CLEANUP => 1), |
784 | defined $reroll_count ? ('-v', $reroll_count) : (), | |
785 | @rev_list_opts); | |
5df9fcf6 PH |
786 | } |
787 | ||
a8022c5f MS |
788 | if (defined $sender) { |
789 | $sender =~ s/^\s+|\s+$//g; | |
790 | ($sender) = expand_aliases($sender); | |
791 | } else { | |
792 | $sender = $repoauthor->() || $repocommitter->() || ''; | |
793 | } | |
794 | ||
795 | # $sender could be an already sanitized address | |
796 | # (e.g. sendemail.from could be manually sanitized by user). | |
797 | # But it's a no-op to run sanitize_address on an already sanitized address. | |
798 | $sender = sanitize_address($sender); | |
799 | ||
800 | $time = time - scalar $#files; | |
531220ba | 801 | |
a8022c5f MS |
802 | @files = handle_backup_files(@files); |
803 | ||
aa54892f JK |
804 | if (@files) { |
805 | unless ($quiet) { | |
806 | print $_,"\n" for (@files); | |
807 | } | |
808 | } else { | |
46493105 | 809 | print STDERR __("\nNo patch files specified!\n\n"); |
aa54892f JK |
810 | usage(); |
811 | } | |
812 | ||
acf071b0 | 813 | sub get_patch_subject { |
beece9da PH |
814 | my $fn = shift; |
815 | open (my $fh, '<', $fn); | |
816 | while (my $line = <$fh>) { | |
817 | next unless ($line =~ /^Subject: (.*)$/); | |
818 | close $fh; | |
819 | return "GIT: $1\n"; | |
820 | } | |
821 | close $fh; | |
3c5cd20c | 822 | die sprintf(__("No subject line in %s?"), $fn); |
beece9da PH |
823 | } |
824 | ||
825 | if ($compose) { | |
826 | # Note that this does not need to be secure, but we will make a small | |
827 | # effort to have it be unique | |
f4dc9432 | 828 | require File::Temp; |
afe756c9 | 829 | $compose_filename = ($repo ? |
f4dc9432 ÆAB |
830 | File::Temp::tempfile(".gitsendemail.msg.XXXXXX", DIR => $repo->repo_path()) : |
831 | File::Temp::tempfile(".gitsendemail.msg.XXXXXX", DIR => "."))[1]; | |
fe0f944f | 832 | open my $c, ">", $compose_filename |
3c5cd20c | 833 | or die sprintf(__("Failed to open for writing %s: %s"), $compose_filename, $!); |
beece9da PH |
834 | |
835 | ||
fef381e6 | 836 | my $tpl_sender = $sender || $repoauthor->() || $repocommitter->() || ''; |
beece9da | 837 | my $tpl_subject = $initial_subject || ''; |
15dc3b91 | 838 | my $tpl_in_reply_to = $initial_in_reply_to || ''; |
d11c943c | 839 | my $tpl_reply_to = $reply_to || ''; |
3ec61675 JK |
840 | my $tpl_to = join(',', @initial_to); |
841 | my $tpl_cc = join(',', @initial_cc); | |
842 | my $tpl_bcc = join(', ', @initial_bcc); | |
beece9da | 843 | |
4adbf387 | 844 | print $c <<EOT1, Git::prefix_lines("GIT: ", __(<<EOT2)), <<EOT3; |
beece9da | 845 | From $tpl_sender # This line is ignored. |
70aedfb3 VA |
846 | EOT1 |
847 | Lines beginning in "GIT:" will be removed. | |
848 | Consider including an overall diffstat or table of contents | |
849 | for the patch you are writing. | |
850 | ||
851 | Clear the body content if you don't wish to send a summary. | |
852 | EOT2 | |
beece9da | 853 | From: $tpl_sender |
3ec61675 JK |
854 | To: $tpl_to |
855 | Cc: $tpl_cc | |
856 | Bcc: $tpl_bcc | |
d11c943c | 857 | Reply-To: $tpl_reply_to |
beece9da | 858 | Subject: $tpl_subject |
15dc3b91 | 859 | In-Reply-To: $tpl_in_reply_to |
beece9da | 860 | |
70aedfb3 | 861 | EOT3 |
beece9da | 862 | for my $f (@files) { |
fe0f944f | 863 | print $c get_patch_subject($f); |
beece9da | 864 | } |
fe0f944f | 865 | close $c; |
beece9da | 866 | |
beece9da PH |
867 | if ($annotate) { |
868 | do_edit($compose_filename, @files); | |
869 | } else { | |
870 | do_edit($compose_filename); | |
871 | } | |
872 | ||
637e8944 JK |
873 | open my $c2, ">", $compose_filename . ".final" |
874 | or die sprintf(__("Failed to open %s.final: %s"), $compose_filename, $!); | |
875 | ||
fe0f944f | 876 | open $c, "<", $compose_filename |
3c5cd20c | 877 | or die sprintf(__("Failed to open %s: %s"), $compose_filename, $!); |
beece9da | 878 | |
637e8944 JK |
879 | my $need_8bit_cte = file_has_nonascii($compose_filename); |
880 | my $in_body = 0; | |
881 | my $summary_empty = 1; | |
4a47a4dd KM |
882 | if (!defined $compose_encoding) { |
883 | $compose_encoding = "UTF-8"; | |
884 | } | |
637e8944 JK |
885 | while(<$c>) { |
886 | next if m/^GIT:/; | |
887 | if ($in_body) { | |
888 | $summary_empty = 0 unless (/^\n$/); | |
889 | } elsif (/^\n$/) { | |
890 | $in_body = 1; | |
891 | if ($need_8bit_cte) { | |
892 | print $c2 "MIME-Version: 1.0\n", | |
893 | "Content-Type: text/plain; ", | |
894 | "charset=$compose_encoding\n", | |
895 | "Content-Transfer-Encoding: 8bit\n"; | |
896 | } | |
897 | } elsif (/^MIME-Version:/i) { | |
898 | $need_8bit_cte = 0; | |
899 | } elsif (/^Subject:\s*(.+)\s*$/i) { | |
900 | $initial_subject = $1; | |
901 | my $subject = $initial_subject; | |
902 | $_ = "Subject: " . | |
903 | quote_subject($subject, $compose_encoding) . | |
904 | "\n"; | |
905 | } elsif (/^In-Reply-To:\s*(.+)\s*$/i) { | |
906 | $initial_in_reply_to = $1; | |
907 | next; | |
908 | } elsif (/^Reply-To:\s*(.+)\s*$/i) { | |
909 | $reply_to = $1; | |
910 | } elsif (/^From:\s*(.+)\s*$/i) { | |
911 | $sender = $1; | |
912 | next; | |
3ec61675 JK |
913 | } elsif (/^To:\s*(.+)\s*$/i) { |
914 | @initial_to = parse_address_line($1); | |
915 | next; | |
916 | } elsif (/^Cc:\s*(.+)\s*$/i) { | |
917 | @initial_cc = parse_address_line($1); | |
918 | next; | |
919 | } elsif (/^Bcc:/i) { | |
920 | @initial_bcc = parse_address_line($1); | |
637e8944 | 921 | next; |
beece9da | 922 | } |
637e8944 | 923 | print $c2 $_; |
beece9da | 924 | } |
fe0f944f | 925 | close $c; |
637e8944 | 926 | close $c2; |
beece9da | 927 | |
637e8944 | 928 | if ($summary_empty) { |
46493105 | 929 | print __("Summary email is empty, skipping it\n"); |
beece9da PH |
930 | $compose = -1; |
931 | } | |
932 | } elsif ($annotate) { | |
933 | do_edit(@files); | |
934 | } | |
935 | ||
c016726c JK |
936 | { |
937 | # Only instantiate one $term per program run, since some | |
938 | # Term::ReadLine providers refuse to create a second instance. | |
939 | my $term; | |
940 | sub term { | |
941 | require Term::ReadLine; | |
942 | if (!defined $term) { | |
943 | $term = $ENV{"GIT_SEND_EMAIL_NOTTY"} | |
944 | ? Term::ReadLine->new('git-send-email', \*STDIN, \*STDOUT) | |
945 | : Term::ReadLine->new('git-send-email'); | |
946 | } | |
947 | return $term; | |
948 | } | |
f4dc9432 ÆAB |
949 | } |
950 | ||
6e182518 JS |
951 | sub ask { |
952 | my ($prompt, %arg) = @_; | |
0da43a68 | 953 | my $valid_re = $arg{valid_re}; |
6e182518 | 954 | my $default = $arg{default}; |
51bbccfd | 955 | my $confirm_only = $arg{confirm_only}; |
6e182518 JS |
956 | my $resp; |
957 | my $i = 0; | |
f4dc9432 | 958 | my $term = term(); |
5906f54e JS |
959 | return defined $default ? $default : undef |
960 | unless defined $term->IN and defined fileno($term->IN) and | |
961 | defined $term->OUT and defined fileno($term->OUT); | |
6e182518 JS |
962 | while ($i++ < 10) { |
963 | $resp = $term->readline($prompt); | |
964 | if (!defined $resp) { # EOF | |
965 | print "\n"; | |
966 | return defined $default ? $default : undef; | |
967 | } | |
968 | if ($resp eq '' and defined $default) { | |
969 | return $default; | |
970 | } | |
0da43a68 | 971 | if (!defined $valid_re or $resp =~ /$valid_re/) { |
6e182518 JS |
972 | return $resp; |
973 | } | |
51bbccfd | 974 | if ($confirm_only) { |
3c5cd20c VA |
975 | my $yesno = $term->readline( |
976 | # TRANSLATORS: please keep [y/N] as is. | |
977 | sprintf(__("Are you sure you want to use <%s> [y/N]? "), $resp)); | |
51bbccfd JH |
978 | if (defined $yesno && $yesno =~ /y/i) { |
979 | return $resp; | |
980 | } | |
981 | } | |
6e182518 | 982 | } |
622bc930 | 983 | return; |
6e182518 JS |
984 | } |
985 | ||
3cae7e5b TR |
986 | my %broken_encoding; |
987 | ||
1d50bfd9 | 988 | sub file_declares_8bit_cte { |
3cae7e5b TR |
989 | my $fn = shift; |
990 | open (my $fh, '<', $fn); | |
991 | while (my $line = <$fh>) { | |
992 | last if ($line =~ /^$/); | |
993 | return 1 if ($line =~ /^Content-Transfer-Encoding: .*8bit.*$/); | |
994 | } | |
995 | close $fh; | |
996 | return 0; | |
997 | } | |
998 | ||
999 | foreach my $f (@files) { | |
1000 | next unless (body_or_subject_has_nonascii($f) | |
1001 | && !file_declares_8bit_cte($f)); | |
1002 | $broken_encoding{$f} = 1; | |
1003 | } | |
1004 | ||
1005 | if (!defined $auto_8bit_encoding && scalar %broken_encoding) { | |
a4dde4c4 VA |
1006 | print __("The following files are 8bit, but do not declare " . |
1007 | "a Content-Transfer-Encoding.\n"); | |
3cae7e5b TR |
1008 | foreach my $f (sort keys %broken_encoding) { |
1009 | print " $f\n"; | |
1010 | } | |
a4dde4c4 | 1011 | $auto_8bit_encoding = ask(__("Which 8bit encoding should I declare [UTF-8]? "), |
852a15d7 | 1012 | valid_re => qr/.{4}/, confirm_only => 1, |
3cae7e5b TR |
1013 | default => "UTF-8"); |
1014 | } | |
1015 | ||
a03bc5b6 TR |
1016 | if (!$force) { |
1017 | for my $f (@files) { | |
0d290a46 | 1018 | if (get_patch_subject($f) =~ /\Q*** SUBJECT HERE ***\E/) { |
3c5cd20c | 1019 | die sprintf(__("Refusing to send because the patch\n\t%s\n" |
a03bc5b6 | 1020 | . "has the template subject '*** SUBJECT HERE ***'. " |
3c5cd20c | 1021 | . "Pass --force if you really want to send.\n"), $f); |
a03bc5b6 TR |
1022 | } |
1023 | } | |
1024 | } | |
1025 | ||
a4dde4c4 | 1026 | my $to_whom = __("To whom should the emails be sent (if anyone)?"); |
8cac13dc | 1027 | my $prompting = 0; |
8796ff7f | 1028 | if (!@initial_to && !defined $to_cmd) { |
0d6b21e7 | 1029 | my $to = ask("$to_whom ", |
61837493 | 1030 | default => "", |
51bbccfd | 1031 | valid_re => qr/\@.*\./, confirm_only => 1); |
3c3bb51c | 1032 | push @initial_to, parse_address_line($to) if defined $to; # sanitized/validated later |
1f038a0c | 1033 | $prompting++; |
83b24437 RA |
1034 | } |
1035 | ||
994d6c66 | 1036 | sub expand_aliases { |
302e04ea JK |
1037 | return map { expand_one_alias($_) } @_; |
1038 | } | |
1039 | ||
1040 | my %EXPANDED_ALIASES; | |
1041 | sub expand_one_alias { | |
1042 | my $alias = shift; | |
1043 | if ($EXPANDED_ALIASES{$alias}) { | |
3c5cd20c | 1044 | die sprintf(__("fatal: alias '%s' expands to itself\n"), $alias); |
302e04ea JK |
1045 | } |
1046 | local $EXPANDED_ALIASES{$alias} = 1; | |
1047 | return $aliases{$alias} ? expand_aliases(@{$aliases{$alias}}) : $alias; | |
994d6c66 EW |
1048 | } |
1049 | ||
b5e112d8 RL |
1050 | @initial_to = process_address_list(@initial_to); |
1051 | @initial_cc = process_address_list(@initial_cc); | |
e60f6d13 | 1052 | @initial_bcc = process_address_list(@initial_bcc); |
994d6c66 | 1053 | |
15dc3b91 CL |
1054 | if ($thread && !defined $initial_in_reply_to && $prompting) { |
1055 | $initial_in_reply_to = ask( | |
a4dde4c4 | 1056 | __("Message-ID to be used as In-Reply-To for the first email (if any)? "), |
61837493 | 1057 | default => "", |
51bbccfd | 1058 | valid_re => qr/\@.*\./, confirm_only => 1); |
83b24437 | 1059 | } |
15dc3b91 CL |
1060 | if (defined $initial_in_reply_to) { |
1061 | $initial_in_reply_to =~ s/^\s*<?//; | |
1062 | $initial_in_reply_to =~ s/>?\s*$//; | |
1063 | $initial_in_reply_to = "<$initial_in_reply_to>" if $initial_in_reply_to ne ''; | |
ace9c2a9 | 1064 | } |
ace72086 | 1065 | |
d11c943c CL |
1066 | if (defined $reply_to) { |
1067 | $reply_to =~ s/^\s+|\s+$//g; | |
1068 | ($reply_to) = expand_aliases($reply_to); | |
1069 | $reply_to = sanitize_address($reply_to); | |
ace9c2a9 | 1070 | } |
ace72086 | 1071 | |
cd5b33fb | 1072 | if (!defined $sendmail_cmd && !defined $smtp_server) { |
1ab2fd4f FK |
1073 | my @sendmail_paths = qw( /usr/sbin/sendmail /usr/lib/sendmail ); |
1074 | push @sendmail_paths, map {"$_/sendmail"} split /:/, $ENV{PATH}; | |
1075 | foreach (@sendmail_paths) { | |
aca7ad76 | 1076 | if (-x $_) { |
cd5b33fb | 1077 | $sendmail_cmd = $_; |
aca7ad76 EW |
1078 | last; |
1079 | } | |
1080 | } | |
cd5b33fb GA |
1081 | |
1082 | if (!defined $sendmail_cmd) { | |
1083 | $smtp_server = 'localhost'; # could be 127.0.0.1, too... *shrug* | |
1084 | } | |
3342d850 RA |
1085 | } |
1086 | ||
c1f2aa45 JS |
1087 | if ($compose && $compose > 0) { |
1088 | @files = ($compose_filename . ".final", @files); | |
1f038a0c RA |
1089 | } |
1090 | ||
83b24437 | 1091 | # Variables we set as part of the loop over files |
15dc3b91 | 1092 | our ($message_id, %mail, $subject, $in_reply_to, $references, $message, |
dc1460aa | 1093 | $needs_confirm, $message_num, $ask_default); |
83b24437 | 1094 | |
567ffeb7 EW |
1095 | sub extract_valid_address { |
1096 | my $address = shift; | |
35b6ab95 ÆAB |
1097 | my $local_part_regexp = qr/[^<>"\s@]+/; |
1098 | my $domain_regexp = qr/[^.<>"\s@]+(?:\.[^.<>"\s@]+)+/; | |
db3106b2 EW |
1099 | |
1100 | # check for a local address: | |
ad9c18f5 | 1101 | return $address if ($address =~ /^($local_part_regexp)$/); |
db3106b2 | 1102 | |
155197e6 | 1103 | $address =~ s/^\s*<(.*)>\s*$/$1/; |
f4dc9432 | 1104 | my $have_email_valid = eval { require Email::Valid; 1 }; |
567ffeb7 | 1105 | if ($have_email_valid) { |
ad9c18f5 | 1106 | return scalar Email::Valid->address($address); |
567ffeb7 | 1107 | } |
95c0d4b6 KM |
1108 | |
1109 | # less robust/correct than the monster regexp in Email::Valid, | |
1110 | # but still does a 99% job, and one less dependency | |
1111 | return $1 if $address =~ /($local_part_regexp\@$domain_regexp)/; | |
622bc930 | 1112 | return; |
567ffeb7 | 1113 | } |
83b24437 | 1114 | |
e4312255 KM |
1115 | sub extract_valid_address_or_die { |
1116 | my $address = shift; | |
12288cc4 | 1117 | my $valid_address = extract_valid_address($address); |
3c5cd20c | 1118 | die sprintf(__("error: unable to extract a valid address from: %s\n"), $address) |
12288cc4 TB |
1119 | if !$valid_address; |
1120 | return $valid_address; | |
e4312255 KM |
1121 | } |
1122 | ||
1123 | sub validate_address { | |
1124 | my $address = shift; | |
d0e98107 | 1125 | while (!extract_valid_address($address)) { |
3c5cd20c | 1126 | printf STDERR __("error: unable to extract a valid address from: %s\n"), $address; |
a4dde4c4 VA |
1127 | # TRANSLATORS: Make sure to include [q] [d] [e] in your |
1128 | # translation. The program will only accept English input | |
1129 | # at this point. | |
1130 | $_ = ask(__("What to do with this address? ([q]uit|[d]rop|[e]dit): "), | |
d0e98107 | 1131 | valid_re => qr/^(?:quit|q|drop|d|edit|e)/i, |
5c80afed KM |
1132 | default => 'q'); |
1133 | if (/^d/i) { | |
1134 | return undef; | |
1135 | } elsif (/^q/i) { | |
1136 | cleanup_compose_files(); | |
1137 | exit(0); | |
1138 | } | |
0d6b21e7 | 1139 | $address = ask("$to_whom ", |
d0e98107 KM |
1140 | default => "", |
1141 | valid_re => qr/\@.*\./, confirm_only => 1); | |
e4312255 KM |
1142 | } |
1143 | return $address; | |
1144 | } | |
1145 | ||
1146 | sub validate_address_list { | |
1147 | return (grep { defined $_ } | |
1148 | map { validate_address($_) } @_); | |
567ffeb7 | 1149 | } |
83b24437 RA |
1150 | |
1151 | # Usually don't need to change anything below here. | |
1152 | ||
1153 | # we make a "fake" message id by taking the current number | |
1154 | # of seconds since the beginning of Unix time and tacking on | |
1155 | # a random number to the end, in case we are called quicker than | |
1156 | # 1 second since the last time we were called. | |
8037d1a3 RA |
1157 | |
1158 | # We'll setup a template for the message id, using the "from" address: | |
8037d1a3 | 1159 | |
be510cfe | 1160 | my ($message_id_stamp, $message_id_serial); |
68ce9330 | 1161 | sub make_message_id { |
be510cfe JH |
1162 | my $uniq; |
1163 | if (!defined $message_id_stamp) { | |
f4dc9432 ÆAB |
1164 | require POSIX; |
1165 | $message_id_stamp = POSIX::strftime("%Y%m%d%H%M%S.$$", gmtime(time)); | |
be510cfe JH |
1166 | $message_id_serial = 0; |
1167 | } | |
1168 | $message_id_serial++; | |
1169 | $uniq = "$message_id_stamp-$message_id_serial"; | |
1170 | ||
aeb59328 | 1171 | my $du_part; |
fef381e6 | 1172 | for ($sender, $repocommitter->(), $repoauthor->()) { |
94638f89 UKK |
1173 | $du_part = extract_valid_address(sanitize_address($_)); |
1174 | last if (defined $du_part and $du_part ne ''); | |
aeb59328 | 1175 | } |
94638f89 | 1176 | if (not defined $du_part or $du_part eq '') { |
529dd386 | 1177 | require Sys::Hostname; |
aeb59328 JH |
1178 | $du_part = 'user@' . Sys::Hostname::hostname(); |
1179 | } | |
f916ab0c | 1180 | my $message_id_template = "<%s-%s>"; |
be510cfe | 1181 | $message_id = sprintf($message_id_template, $uniq, $du_part); |
8037d1a3 | 1182 | #print "new message id = $message_id\n"; # Was useful for debugging |
83b24437 RA |
1183 | } |
1184 | ||
374c5905 JR |
1185 | sub unquote_rfc2047 { |
1186 | local ($_) = @_; | |
11f70a7e | 1187 | my $charset; |
ab47e2a5 РД |
1188 | my $sep = qr/[ \t]+/; |
1189 | s{$re_encoded_word(?:$sep$re_encoded_word)*}{ | |
1190 | my @words = split $sep, $&; | |
1191 | foreach (@words) { | |
1192 | m/$re_encoded_word/; | |
1193 | $charset = $1; | |
1194 | my $encoding = $2; | |
1195 | my $text = $3; | |
1196 | if ($encoding eq 'q' || $encoding eq 'Q') { | |
1197 | $_ = $text; | |
1198 | s/_/ /g; | |
1199 | s/=([0-9A-F]{2})/chr(hex($1))/egi; | |
1200 | } else { | |
1201 | # other encodings not supported yet | |
1202 | } | |
11f70a7e | 1203 | } |
ab47e2a5 | 1204 | join '', @words; |
b622d4d1 | 1205 | }eg; |
11f70a7e | 1206 | return wantarray ? ($_, $charset) : $_; |
374c5905 JR |
1207 | } |
1208 | ||
d54eaaa2 JK |
1209 | sub quote_rfc2047 { |
1210 | local $_ = shift; | |
d1fff6fc | 1211 | my $encoding = shift || 'UTF-8'; |
d54eaaa2 JK |
1212 | s/([^-a-zA-Z0-9!*+\/])/sprintf("=%02X", ord($1))/eg; |
1213 | s/(.*)/=\?$encoding\?q\?$1\?=/; | |
1214 | return $_; | |
1215 | } | |
1216 | ||
a3a8262b BC |
1217 | sub is_rfc2047_quoted { |
1218 | my $s = shift; | |
a3a8262b | 1219 | length($s) <= 75 && |
11f70a7e | 1220 | $s =~ m/^(?:"[[:ascii:]]*"|$re_encoded_word)$/o; |
a3a8262b BC |
1221 | } |
1222 | ||
ce547800 KM |
1223 | sub subject_needs_rfc2047_quoting { |
1224 | my $s = shift; | |
1225 | ||
ce1459f7 | 1226 | return ($s =~ /[^[:ascii:]]/) || ($s =~ /=\?/); |
ce547800 KM |
1227 | } |
1228 | ||
1229 | sub quote_subject { | |
1230 | local $subject = shift; | |
1231 | my $encoding = shift || 'UTF-8'; | |
1232 | ||
1233 | if (subject_needs_rfc2047_quoting($subject)) { | |
1234 | return quote_rfc2047($subject, $encoding); | |
1235 | } | |
1236 | return $subject; | |
1237 | } | |
1238 | ||
5b56aaa2 | 1239 | # use the simplest quoting being able to handle the recipient |
68ce9330 | 1240 | sub sanitize_address { |
732263d4 | 1241 | my ($recipient) = @_; |
831a488b KM |
1242 | |
1243 | # remove garbage after email address | |
1244 | $recipient =~ s/(.*>).*$/$1/; | |
1245 | ||
5b56aaa2 UKK |
1246 | my ($recipient_name, $recipient_addr) = ($recipient =~ /^(.*?)\s*(<.*)/); |
1247 | ||
1248 | if (not $recipient_name) { | |
ff483897 | 1249 | return $recipient; |
5b56aaa2 UKK |
1250 | } |
1251 | ||
1252 | # if recipient_name is already quoted, do nothing | |
a3a8262b | 1253 | if (is_rfc2047_quoted($recipient_name)) { |
5b56aaa2 UKK |
1254 | return $recipient; |
1255 | } | |
1256 | ||
1fe9703f RL |
1257 | # remove non-escaped quotes |
1258 | $recipient_name =~ s/(^|[^\\])"/$1/g; | |
1259 | ||
5b56aaa2 UKK |
1260 | # rfc2047 is needed if a non-ascii char is included |
1261 | if ($recipient_name =~ /[^[:ascii:]]/) { | |
d54eaaa2 | 1262 | $recipient_name = quote_rfc2047($recipient_name); |
732263d4 | 1263 | } |
5b56aaa2 UKK |
1264 | |
1265 | # double quotes are needed if specials or CTLs are included | |
1266 | elsif ($recipient_name =~ /[][()<>@,;:\\".\000-\037\177]/) { | |
1fe9703f | 1267 | $recipient_name =~ s/([\\\r])/\\$1/g; |
d5c7d69d | 1268 | $recipient_name = qq["$recipient_name"]; |
5b56aaa2 UKK |
1269 | } |
1270 | ||
1271 | return "$recipient_name $recipient_addr"; | |
1272 | ||
732263d4 RJ |
1273 | } |
1274 | ||
cb2922fe MM |
1275 | sub strip_garbage_one_address { |
1276 | my ($addr) = @_; | |
1277 | chomp $addr; | |
1278 | if ($addr =~ /^(("[^"]*"|[^"<]*)? *<[^>]*>).*/) { | |
1279 | # "Foo Bar" <foobar@example.com> [possibly garbage here] | |
1280 | # Foo Bar <foobar@example.com> [possibly garbage here] | |
1281 | return $1; | |
1282 | } | |
1283 | if ($addr =~ /^(<[^>]*>).*/) { | |
1284 | # <foo@example.com> [possibly garbage here] | |
1285 | # if garbage contains other addresses, they are ignored. | |
1286 | return $1; | |
1287 | } | |
1288 | if ($addr =~ /^([^"#,\s]*)/) { | |
1289 | # address without quoting: remove anything after the address | |
1290 | return $1; | |
1291 | } | |
1292 | return $addr; | |
1293 | } | |
1294 | ||
e4312255 KM |
1295 | sub sanitize_address_list { |
1296 | return (map { sanitize_address($_) } @_); | |
1297 | } | |
1298 | ||
b5e112d8 | 1299 | sub process_address_list { |
b1c8a11c RL |
1300 | my @addr_list = map { parse_address_line($_) } @_; |
1301 | @addr_list = expand_aliases(@addr_list); | |
b5e112d8 RL |
1302 | @addr_list = sanitize_address_list(@addr_list); |
1303 | @addr_list = validate_address_list(@addr_list); | |
1304 | return @addr_list; | |
1305 | } | |
1306 | ||
134550fe JA |
1307 | # Returns the local Fully Qualified Domain Name (FQDN) if available. |
1308 | # | |
1309 | # Tightly configured MTAa require that a caller sends a real DNS | |
1310 | # domain name that corresponds the IP address in the HELO/EHLO | |
1311 | # handshake. This is used to verify the connection and prevent | |
1312 | # spammers from trying to hide their identity. If the DNS and IP don't | |
15beaaa3 | 1313 | # match, the receiving MTA may deny the connection. |
134550fe JA |
1314 | # |
1315 | # Here is a deny example of Net::SMTP with the default "localhost.localdomain" | |
1316 | # | |
1317 | # Net::SMTP=GLOB(0x267ec28)>>> EHLO localhost.localdomain | |
1318 | # Net::SMTP=GLOB(0x267ec28)<<< 550 EHLO argument does not match calling host | |
1319 | # | |
1320 | # This maildomain*() code is based on ideas in Perl library Test::Reporter | |
1321 | # /usr/share/perl5/Test/Reporter/Mail/Util.pm ==> sub _maildomain () | |
1322 | ||
59a86303 BG |
1323 | sub valid_fqdn { |
1324 | my $domain = shift; | |
61ef5e9b | 1325 | return defined $domain && !($^O eq 'darwin' && $domain =~ /\.local$/) && $domain =~ /\./; |
59a86303 BG |
1326 | } |
1327 | ||
68ce9330 | 1328 | sub maildomain_net { |
134550fe JA |
1329 | my $maildomain; |
1330 | ||
f4dc9432 | 1331 | require Net::Domain; |
1046c118 ÆAB |
1332 | my $domain = Net::Domain::domainname(); |
1333 | $maildomain = $domain if valid_fqdn($domain); | |
134550fe JA |
1334 | |
1335 | return $maildomain; | |
1336 | } | |
1337 | ||
68ce9330 | 1338 | sub maildomain_mta { |
134550fe JA |
1339 | my $maildomain; |
1340 | ||
1046c118 | 1341 | for my $host (qw(mailhost localhost)) { |
f4dc9432 | 1342 | require Net::SMTP; |
1046c118 ÆAB |
1343 | my $smtp = Net::SMTP->new($host); |
1344 | if (defined $smtp) { | |
1345 | my $domain = $smtp->domain; | |
1346 | $smtp->quit; | |
134550fe | 1347 | |
1046c118 | 1348 | $maildomain = $domain if valid_fqdn($domain); |
134550fe | 1349 | |
1046c118 | 1350 | last if $maildomain; |
134550fe JA |
1351 | } |
1352 | } | |
1353 | ||
1354 | return $maildomain; | |
1355 | } | |
1356 | ||
68ce9330 | 1357 | sub maildomain { |
69cf7bfd | 1358 | return maildomain_net() || maildomain_mta() || 'localhost.localdomain'; |
134550fe JA |
1359 | } |
1360 | ||
4d31a44a MN |
1361 | sub smtp_host_string { |
1362 | if (defined $smtp_server_port) { | |
1363 | return "$smtp_server:$smtp_server_port"; | |
1364 | } else { | |
1365 | return $smtp_server; | |
1366 | } | |
1367 | } | |
1368 | ||
1369 | # Returns 1 if authentication succeeded or was not necessary | |
1370 | # (smtp_user was not specified), and 0 otherwise. | |
1371 | ||
1372 | sub smtp_auth_maybe { | |
8dd9b3f8 | 1373 | if (!defined $smtp_authuser || $auth || (defined $smtp_auth && $smtp_auth eq "none")) { |
4d31a44a MN |
1374 | return 1; |
1375 | } | |
1376 | ||
1377 | # Workaround AUTH PLAIN/LOGIN interaction defect | |
1378 | # with Authen::SASL::Cyrus | |
1379 | eval { | |
1380 | require Authen::SASL; | |
1381 | Authen::SASL->import(qw(Perl)); | |
1382 | }; | |
1383 | ||
0f2e68b5 JV |
1384 | # Check mechanism naming as defined in: |
1385 | # https://tools.ietf.org/html/rfc4422#page-8 | |
904f6e7c | 1386 | if ($smtp_auth && $smtp_auth !~ /^(\b[A-Z0-9-_]{1,20}\s*)*$/) { |
0f2e68b5 JV |
1387 | die "invalid smtp auth: '${smtp_auth}'"; |
1388 | } | |
1389 | ||
4d31a44a MN |
1390 | # TODO: Authentication may fail not because credentials were |
1391 | # invalid but due to other reasons, in which we should not | |
1392 | # reject credentials. | |
1393 | $auth = Git::credential({ | |
1394 | 'protocol' => 'smtp', | |
1395 | 'host' => smtp_host_string(), | |
1396 | 'username' => $smtp_authuser, | |
1397 | # if there's no password, "git credential fill" will | |
1398 | # give us one, otherwise it'll just pass this one. | |
1399 | 'password' => $smtp_authpass | |
1400 | }, sub { | |
1401 | my $cred = shift; | |
0f2e68b5 JV |
1402 | |
1403 | if ($smtp_auth) { | |
1404 | my $sasl = Authen::SASL->new( | |
1405 | mechanism => $smtp_auth, | |
1406 | callback => { | |
1407 | user => $cred->{'username'}, | |
1408 | pass => $cred->{'password'}, | |
1409 | authname => $cred->{'username'}, | |
1410 | } | |
1411 | ); | |
1412 | ||
1413 | return !!$smtp->auth($sasl); | |
1414 | } | |
1415 | ||
4d31a44a MN |
1416 | return !!$smtp->auth($cred->{'username'}, $cred->{'password'}); |
1417 | }); | |
1418 | ||
1419 | return $auth; | |
1420 | } | |
1421 | ||
35035bbf RR |
1422 | sub ssl_verify_params { |
1423 | eval { | |
1424 | require IO::Socket::SSL; | |
1425 | IO::Socket::SSL->import(qw/SSL_VERIFY_PEER SSL_VERIFY_NONE/); | |
1426 | }; | |
1427 | if ($@) { | |
1428 | print STDERR "Not using SSL_VERIFY_PEER due to out-of-date IO::Socket::SSL.\n"; | |
1429 | return; | |
1430 | } | |
1431 | ||
1432 | if (!defined $smtp_ssl_cert_path) { | |
01645b74 RK |
1433 | # use the OpenSSL defaults |
1434 | return (SSL_verify_mode => SSL_VERIFY_PEER()); | |
35035bbf RR |
1435 | } |
1436 | ||
1437 | if ($smtp_ssl_cert_path eq "") { | |
1438 | return (SSL_verify_mode => SSL_VERIFY_NONE()); | |
1439 | } elsif (-d $smtp_ssl_cert_path) { | |
1440 | return (SSL_verify_mode => SSL_VERIFY_PEER(), | |
1441 | SSL_ca_path => $smtp_ssl_cert_path); | |
1442 | } elsif (-f $smtp_ssl_cert_path) { | |
1443 | return (SSL_verify_mode => SSL_VERIFY_PEER(), | |
1444 | SSL_ca_file => $smtp_ssl_cert_path); | |
1445 | } else { | |
3c5cd20c | 1446 | die sprintf(__("CA path \"%s\" does not exist"), $smtp_ssl_cert_path); |
35035bbf RR |
1447 | } |
1448 | } | |
1449 | ||
cb005c1f EFL |
1450 | sub file_name_is_absolute { |
1451 | my ($path) = @_; | |
1452 | ||
1453 | # msys does not grok DOS drive-prefixes | |
1454 | if ($^O eq 'msys') { | |
f24ecf59 | 1455 | return ($path =~ m#^/# || $path =~ m#^[a-zA-Z]\:#) |
cb005c1f EFL |
1456 | } |
1457 | ||
1458 | require File::Spec::Functions; | |
1459 | return File::Spec::Functions::file_name_is_absolute($path); | |
1460 | } | |
1461 | ||
56adddaa | 1462 | sub gen_header { |
4bc87a28 | 1463 | my @recipients = unique_email_list(@to); |
e4312255 | 1464 | @cc = (grep { my $cc = extract_valid_address_or_die($_); |
83acaaec | 1465 | not grep { $cc eq $_ || $_ =~ /<\Q${cc}\E>$/ } @recipients |
7ac17529 | 1466 | } |
7ac17529 | 1467 | @cc); |
4bc87a28 | 1468 | my $to = join (",\n\t", @recipients); |
e60f6d13 | 1469 | @recipients = unique_email_list(@recipients,@cc,@initial_bcc); |
e4312255 | 1470 | @recipients = (map { extract_valid_address_or_die($_) } @recipients); |
6bdca890 | 1471 | my $date = format_2822_time($time++); |
e923effb ML |
1472 | my $gitversion = '@@GIT_VERSION@@'; |
1473 | if ($gitversion =~ m/..GIT_VERSION../) { | |
3cb8caf7 | 1474 | $gitversion = Git::version(); |
e923effb | 1475 | } |
4bc87a28 | 1476 | |
02461e0e | 1477 | my $cc = join(",\n\t", unique_email_list(@cc)); |
f06a6a49 JH |
1478 | my $ccline = ""; |
1479 | if ($cc ne '') { | |
1480 | $ccline = "\nCc: $cc"; | |
1481 | } | |
4f3d3703 | 1482 | make_message_id() unless defined($message_id); |
aeb59328 | 1483 | |
da18759e | 1484 | my $header = "From: $sender |
f06a6a49 | 1485 | To: $to${ccline} |
4bc87a28 | 1486 | Subject: $subject |
4bc87a28 | 1487 | Date: $date |
ba4324c4 | 1488 | Message-ID: $message_id |
4bc87a28 | 1489 | "; |
ac1596a6 LH |
1490 | if ($use_xmailer) { |
1491 | $header .= "X-Mailer: git-send-email $gitversion\n"; | |
1492 | } | |
15dc3b91 | 1493 | if ($in_reply_to) { |
7ccf7927 | 1494 | |
15dc3b91 | 1495 | $header .= "In-Reply-To: $in_reply_to\n"; |
7ccf7927 RA |
1496 | $header .= "References: $references\n"; |
1497 | } | |
d11c943c CL |
1498 | if ($reply_to) { |
1499 | $header .= "Reply-To: $reply_to\n"; | |
1500 | } | |
ce91c2f6 JH |
1501 | if (@xh) { |
1502 | $header .= join("\n", @xh) . "\n"; | |
1503 | } | |
56adddaa MS |
1504 | my $recipients_ref = \@recipients; |
1505 | return ($recipients_ref, $to, $date, $gitversion, $cc, $ccline, $header); | |
1506 | } | |
1507 | ||
1508 | # Prepares the email, then asks the user what to do. | |
1509 | # | |
1510 | # If the user chooses to send the email, it's sent and 1 is returned. | |
1511 | # If the user chooses not to send the email, 0 is returned. | |
1512 | # If the user decides they want to make further edits, -1 is returned and the | |
1513 | # caller is expected to call send_message again after the edits are performed. | |
1514 | # | |
1515 | # If an error occurs sending the email, this just dies. | |
1516 | ||
1517 | sub send_message { | |
1518 | my ($recipients_ref, $to, $date, $gitversion, $cc, $ccline, $header) = gen_header(); | |
1519 | my @recipients = @$recipients_ref; | |
4bc87a28 | 1520 | |
c38f0247 | 1521 | my @sendmail_parameters = ('-i', @recipients); |
da18759e | 1522 | my $raw_from = $sender; |
c89e3241 FC |
1523 | if (defined $envelope_sender && $envelope_sender ne "auto") { |
1524 | $raw_from = $envelope_sender; | |
1525 | } | |
f073a592 RJ |
1526 | $raw_from = extract_valid_address($raw_from); |
1527 | unshift (@sendmail_parameters, | |
1528 | '-f', $raw_from) if(defined $envelope_sender); | |
8e3d436b | 1529 | |
c1f2aa45 JS |
1530 | if ($needs_confirm && !$dry_run) { |
1531 | print "\n$header\n"; | |
1532 | if ($needs_confirm eq "inform") { | |
1533 | $confirm_unconfigured = 0; # squelch this message for the rest of this run | |
6e182518 | 1534 | $ask_default = "y"; # assume yes on EOF since user hasn't explicitly asked for confirmation |
a4dde4c4 VA |
1535 | print __ <<EOF ; |
1536 | The Cc list above has been expanded by additional | |
1537 | addresses found in the patch commit message. By default | |
1538 | send-email prompts before sending whenever this occurs. | |
1539 | This behavior is controlled by the sendemail.confirm | |
1540 | configuration setting. | |
1541 | ||
1542 | For additional information, run 'git send-email --help'. | |
1543 | To retain the current behavior, but squelch this message, | |
1544 | run 'git config --global sendemail.confirm auto'. | |
1545 | ||
1546 | EOF | |
c1f2aa45 | 1547 | } |
04c4a4e8 | 1548 | # TRANSLATORS: Make sure to include [y] [n] [e] [q] [a] in your |
a4dde4c4 VA |
1549 | # translation. The program will only accept English input |
1550 | # at this point. | |
04c4a4e8 DD |
1551 | $_ = ask(__("Send this email? ([y]es|[n]o|[e]dit|[q]uit|[a]ll): "), |
1552 | valid_re => qr/^(?:yes|y|no|n|edit|e|quit|q|all|a)/i, | |
6e182518 | 1553 | default => $ask_default); |
46493105 | 1554 | die __("Send this email reply required") unless defined $_; |
c1f2aa45 | 1555 | if (/^n/i) { |
15da1084 | 1556 | return 0; |
04c4a4e8 DD |
1557 | } elsif (/^e/i) { |
1558 | return -1; | |
c1f2aa45 JS |
1559 | } elsif (/^q/i) { |
1560 | cleanup_compose_files(); | |
1561 | exit(0); | |
1562 | } elsif (/^a/i) { | |
1563 | $confirm = 'never'; | |
1564 | } | |
1565 | } | |
1566 | ||
052fbea2 PO |
1567 | unshift (@sendmail_parameters, @smtp_server_options); |
1568 | ||
6130259c MW |
1569 | if ($dry_run) { |
1570 | # We don't want to send the email. | |
cd5b33fb | 1571 | } elsif (defined $sendmail_cmd || file_name_is_absolute($smtp_server)) { |
aca7ad76 EW |
1572 | my $pid = open my $sm, '|-'; |
1573 | defined $pid or die $!; | |
1574 | if (!$pid) { | |
cd5b33fb GA |
1575 | if (defined $sendmail_cmd) { |
1576 | exec ("sh", "-c", "$sendmail_cmd \"\$@\"", "-", @sendmail_parameters) | |
1577 | or die $!; | |
1578 | } else { | |
1579 | exec ($smtp_server, @sendmail_parameters) | |
1580 | or die $!; | |
1581 | } | |
aca7ad76 EW |
1582 | } |
1583 | print $sm "$header\n$message"; | |
5e2c2ab1 | 1584 | close $sm or die $!; |
aca7ad76 | 1585 | } else { |
44b2476a JH |
1586 | |
1587 | if (!defined $smtp_server) { | |
46493105 | 1588 | die __("The required SMTP server is not properly defined.") |
44b2476a JH |
1589 | } |
1590 | ||
0ead000c | 1591 | require Net::SMTP; |
bfbfc9a9 | 1592 | my $use_net_smtp_ssl = version->parse($Net::SMTP::VERSION) < version->parse("2.34"); |
0ead000c DK |
1593 | $smtp_domain ||= maildomain(); |
1594 | ||
f6bebd12 | 1595 | if ($smtp_encryption eq 'ssl') { |
44b2476a | 1596 | $smtp_server_port ||= 465; # ssmtp |
5508f3ed | 1597 | require IO::Socket::SSL; |
9d605249 JK |
1598 | |
1599 | # Suppress "variable accessed once" warning. | |
1600 | { | |
1601 | no warnings 'once'; | |
1602 | $IO::Socket::SSL::DEBUG = 1; | |
1603 | } | |
1604 | ||
5508f3ed TR |
1605 | # Net::SMTP::SSL->new() does not forward any SSL options |
1606 | IO::Socket::SSL::set_client_defaults( | |
1607 | ssl_verify_params()); | |
0ead000c DK |
1608 | |
1609 | if ($use_net_smtp_ssl) { | |
1610 | require Net::SMTP::SSL; | |
1611 | $smtp ||= Net::SMTP::SSL->new($smtp_server, | |
1612 | Hello => $smtp_domain, | |
1613 | Port => $smtp_server_port, | |
1614 | Debug => $debug_net_smtp); | |
1615 | } | |
1616 | else { | |
1617 | $smtp ||= Net::SMTP->new($smtp_server, | |
1618 | Hello => $smtp_domain, | |
1619 | Port => $smtp_server_port, | |
1620 | Debug => $debug_net_smtp, | |
1621 | SSL => 1); | |
1622 | } | |
34cc60ce | 1623 | } |
636f3d7a | 1624 | elsif (!$smtp) { |
1a741bf7 | 1625 | $smtp_server_port ||= 25; |
1626 | $smtp ||= Net::SMTP->new($smtp_server, | |
69cf7bfd | 1627 | Hello => $smtp_domain, |
1a741bf7 | 1628 | Debug => $debug_net_smtp, |
1629 | Port => $smtp_server_port); | |
fb3650ed | 1630 | if ($smtp_encryption eq 'tls' && $smtp) { |
0ead000c DK |
1631 | if ($use_net_smtp_ssl) { |
1632 | $smtp->command('STARTTLS'); | |
1633 | $smtp->response(); | |
1634 | if ($smtp->code != 220) { | |
1635 | die sprintf(__("Server does not support STARTTLS! %s"), $smtp->message); | |
1636 | } | |
1637 | require Net::SMTP::SSL; | |
35035bbf RR |
1638 | $smtp = Net::SMTP::SSL->start_SSL($smtp, |
1639 | ssl_verify_params()) | |
0ead000c DK |
1640 | or die sprintf(__("STARTTLS failed! %s"), IO::Socket::SSL::errstr()); |
1641 | } | |
1642 | else { | |
1643 | $smtp->starttls(ssl_verify_params()) | |
1644 | or die sprintf(__("STARTTLS failed! %s"), IO::Socket::SSL::errstr()); | |
f6bebd12 | 1645 | } |
0ead000c DK |
1646 | # Send EHLO again to receive fresh |
1647 | # supported commands | |
1648 | $smtp->hello($smtp_domain); | |
f6bebd12 | 1649 | } |
44b2476a JH |
1650 | } |
1651 | ||
1652 | if (!$smtp) { | |
3c5cd20c VA |
1653 | die __("Unable to initialize SMTP properly. Check config and use --smtp-debug."), |
1654 | " VALUES: server=$smtp_server ", | |
e5afb3a6 | 1655 | "encryption=$smtp_encryption ", |
69cf7bfd | 1656 | "hello=$smtp_domain", |
a1dd7e16 | 1657 | defined $smtp_server_port ? " port=$smtp_server_port" : ""; |
44b2476a JH |
1658 | } |
1659 | ||
4d31a44a | 1660 | smtp_auth_maybe or die $smtp->message; |
2363d746 | 1661 | |
2b69bfc2 | 1662 | $smtp->mail( $raw_from ) or die $smtp->message; |
aca7ad76 EW |
1663 | $smtp->to( @recipients ) or die $smtp->message; |
1664 | $smtp->data or die $smtp->message; | |
f60c483d SA |
1665 | $smtp->datasend("$header\n") or die $smtp->message; |
1666 | my @lines = split /^/, $message; | |
1667 | foreach my $line (@lines) { | |
1668 | $smtp->datasend("$line") or die $smtp->message; | |
1669 | } | |
aca7ad76 | 1670 | $smtp->dataend() or die $smtp->message; |
3c5cd20c | 1671 | $smtp->code =~ /250|200/ or die sprintf(__("Failed to send %s\n"), $subject).$smtp->message; |
aca7ad76 | 1672 | } |
2718435b | 1673 | if ($quiet) { |
3c5cd20c | 1674 | printf($dry_run ? __("Dry-Sent %s\n") : __("Sent %s\n"), $subject); |
2718435b | 1675 | } else { |
a4dde4c4 | 1676 | print($dry_run ? __("Dry-OK. Log says:\n") : __("OK. Log says:\n")); |
cd5b33fb | 1677 | if (!defined $sendmail_cmd && !file_name_is_absolute($smtp_server)) { |
aca7ad76 | 1678 | print "Server: $smtp_server\n"; |
2b69bfc2 | 1679 | print "MAIL FROM:<$raw_from>\n"; |
02461e0e JP |
1680 | foreach my $entry (@recipients) { |
1681 | print "RCPT TO:<$entry>\n"; | |
1682 | } | |
aca7ad76 | 1683 | } else { |
cd5b33fb GA |
1684 | my $sm; |
1685 | if (defined $sendmail_cmd) { | |
1686 | $sm = $sendmail_cmd; | |
1687 | } else { | |
1688 | $sm = $smtp_server; | |
1689 | } | |
1690 | ||
1691 | print "Sendmail: $sm ".join(' ',@sendmail_parameters)."\n"; | |
aca7ad76 | 1692 | } |
b7f30e0a | 1693 | print $header, "\n"; |
aca7ad76 | 1694 | if ($smtp) { |
46493105 | 1695 | print __("Result: "), $smtp->code, ' ', |
aca7ad76 EW |
1696 | ($smtp->message =~ /\n([^\n]+\n)$/s), "\n"; |
1697 | } else { | |
46493105 | 1698 | print __("Result: OK\n"); |
aca7ad76 | 1699 | } |
30d08b34 | 1700 | } |
15da1084 MW |
1701 | |
1702 | return 1; | |
83b24437 RA |
1703 | } |
1704 | ||
56adddaa MS |
1705 | sub pre_process_file { |
1706 | my ($t, $quiet) = @_; | |
04c4a4e8 | 1707 | |
3c5cd20c | 1708 | open my $fh, "<", $t or die sprintf(__("can't open file %s"), $t); |
83b24437 | 1709 | |
94638f89 | 1710 | my $author = undef; |
4cb46bdd | 1711 | my $sauthor = undef; |
8291db6f JK |
1712 | my $author_encoding; |
1713 | my $has_content_type; | |
1714 | my $body_encoding; | |
bb29456c PB |
1715 | my $xfer_encoding; |
1716 | my $has_mime_version; | |
3c3bb51c | 1717 | @to = (); |
c1f2aa45 | 1718 | @cc = (); |
ce91c2f6 | 1719 | @xh = (); |
e6b0964a | 1720 | my $input_format = undef; |
5012699d | 1721 | my @header = (); |
e0821134 | 1722 | $subject = $initial_subject; |
83b24437 | 1723 | $message = ""; |
c1f2aa45 | 1724 | $message_num++; |
3ece9bf0 | 1725 | undef $message_id; |
ba92106e MC |
1726 | # Retrieve and unfold header fields. |
1727 | my @header_lines = (); | |
f9237e61 | 1728 | while(<$fh>) { |
5012699d | 1729 | last if /^\s*$/; |
ba92106e MC |
1730 | push(@header_lines, $_); |
1731 | } | |
1732 | @header = unfold_headers(@header_lines); | |
1733 | # Add computed headers, if applicable. | |
1734 | unless ($no_header_cmd || ! $header_cmd) { | |
1735 | push @header, invoke_header_cmd($header_cmd, $t); | |
5012699d JS |
1736 | } |
1737 | # Now parse the header | |
1738 | foreach(@header) { | |
1739 | if (/^From /) { | |
1740 | $input_format = 'mbox'; | |
1741 | next; | |
1742 | } | |
1743 | chomp; | |
1744 | if (!defined $input_format && /^[-A-Za-z]+:\s/) { | |
1745 | $input_format = 'mbox'; | |
1746 | } | |
1747 | ||
1748 | if (defined $input_format && $input_format eq 'mbox') { | |
6310071a | 1749 | if (/^Subject:\s+(.*)$/i) { |
5012699d | 1750 | $subject = $1; |
e6b0964a | 1751 | } |
6310071a | 1752 | elsif (/^From:\s+(.*)$/i) { |
5012699d | 1753 | ($author, $author_encoding) = unquote_rfc2047($1); |
4cb46bdd | 1754 | $sauthor = sanitize_address($author); |
5012699d | 1755 | next if $suppress_cc{'author'}; |
da18759e | 1756 | next if $suppress_cc{'self'} and $sauthor eq $sender; |
a4dde4c4 | 1757 | printf(__("(mbox) Adding cc: %s from line '%s'\n"), |
5012699d JS |
1758 | $1, $_) unless $quiet; |
1759 | push @cc, $1; | |
e6b0964a | 1760 | } |
6310071a | 1761 | elsif (/^To:\s+(.*)$/i) { |
21802cd3 | 1762 | foreach my $addr (parse_address_line($1)) { |
a4dde4c4 | 1763 | printf(__("(mbox) Adding to: %s from line '%s'\n"), |
21802cd3 | 1764 | $addr, $_) unless $quiet; |
e4312255 | 1765 | push @to, $addr; |
21802cd3 SB |
1766 | } |
1767 | } | |
6310071a | 1768 | elsif (/^Cc:\s+(.*)$/i) { |
5012699d | 1769 | foreach my $addr (parse_address_line($1)) { |
da18759e MT |
1770 | my $qaddr = unquote_rfc2047($addr); |
1771 | my $saddr = sanitize_address($qaddr); | |
1772 | if ($saddr eq $sender) { | |
65648283 | 1773 | next if ($suppress_cc{'self'}); |
65648283 DB |
1774 | } else { |
1775 | next if ($suppress_cc{'cc'}); | |
8a8e6235 | 1776 | } |
a4dde4c4 | 1777 | printf(__("(mbox) Adding cc: %s from line '%s'\n"), |
5012699d JS |
1778 | $addr, $_) unless $quiet; |
1779 | push @cc, $addr; | |
83b24437 | 1780 | } |
5012699d JS |
1781 | } |
1782 | elsif (/^Content-type:/i) { | |
1783 | $has_content_type = 1; | |
1784 | if (/charset="?([^ "]+)/) { | |
1785 | $body_encoding = $1; | |
83b24437 | 1786 | } |
5012699d | 1787 | push @xh, $_; |
83b24437 | 1788 | } |
bb29456c PB |
1789 | elsif (/^MIME-Version/i) { |
1790 | $has_mime_version = 1; | |
1791 | push @xh, $_; | |
1792 | } | |
ba4324c4 | 1793 | elsif (/^Message-ID: (.*)/i) { |
5012699d | 1794 | $message_id = $1; |
83b24437 | 1795 | } |
bb29456c PB |
1796 | elsif (/^Content-Transfer-Encoding: (.*)/i) { |
1797 | $xfer_encoding = $1 if not defined $xfer_encoding; | |
1798 | } | |
256be1d3 | 1799 | elsif (/^In-Reply-To: (.*)/i) { |
f9f60d70 RA |
1800 | if (!$initial_in_reply_to || $thread) { |
1801 | $in_reply_to = $1; | |
1802 | } | |
256be1d3 SA |
1803 | } |
1804 | elsif (/^References: (.*)/i) { | |
f9f60d70 RA |
1805 | if (!$initial_in_reply_to || $thread) { |
1806 | $references = $1; | |
1807 | } | |
256be1d3 | 1808 | } |
6310071a | 1809 | elsif (!/^Date:\s/i && /^[-A-Za-z]+:\s+\S/) { |
5012699d JS |
1810 | push @xh, $_; |
1811 | } | |
83b24437 | 1812 | } else { |
5012699d JS |
1813 | # In the traditional |
1814 | # "send lots of email" format, | |
1815 | # line 1 = cc | |
1816 | # line 2 = subject | |
1817 | # So let's support that, too. | |
1818 | $input_format = 'lots'; | |
1819 | if (@cc == 0 && !$suppress_cc{'cc'}) { | |
a4dde4c4 | 1820 | printf(__("(non-mbox) Adding cc: %s from line '%s'\n"), |
5012699d JS |
1821 | $_, $_) unless $quiet; |
1822 | push @cc, $_; | |
1823 | } elsif (!defined $subject) { | |
1824 | $subject = $_; | |
83b24437 RA |
1825 | } |
1826 | } | |
1827 | } | |
5012699d | 1828 | # Now parse the message body |
f9237e61 | 1829 | while(<$fh>) { |
5012699d | 1830 | $message .= $_; |
9c545816 | 1831 | if (/^([a-z][a-z-]*-by|Cc): (.*)/i) { |
5012699d | 1832 | chomp; |
3531e270 | 1833 | my ($what, $c) = ($1, $2); |
cb2922fe MM |
1834 | # strip garbage for the address we'll use: |
1835 | $c = strip_garbage_one_address($c); | |
1836 | # sanitize a bit more to decide whether to suppress the address: | |
da18759e MT |
1837 | my $sc = sanitize_address($c); |
1838 | if ($sc eq $sender) { | |
3531e270 JS |
1839 | next if ($suppress_cc{'self'}); |
1840 | } else { | |
ef0cc1df RV |
1841 | if ($what =~ /^Signed-off-by$/i) { |
1842 | next if $suppress_cc{'sob'}; | |
1843 | } elsif ($what =~ /-by$/i) { | |
1844 | next if $suppress_cc{'misc-by'}; | |
1845 | } elsif ($what =~ /Cc/i) { | |
1846 | next if $suppress_cc{'bodycc'}; | |
1847 | } | |
3531e270 | 1848 | } |
fb34fe6a RV |
1849 | if ($c !~ /.+@.+|<.+>/) { |
1850 | printf("(body) Ignoring %s from line '%s'\n", | |
1851 | $what, $_) unless $quiet; | |
1852 | next; | |
1853 | } | |
5012699d | 1854 | push @cc, $c; |
a4dde4c4 | 1855 | printf(__("(body) Adding cc: %s from line '%s'\n"), |
5012699d JS |
1856 | $c, $_) unless $quiet; |
1857 | } | |
1858 | } | |
f9237e61 | 1859 | close $fh; |
324a8bd0 | 1860 | |
56adddaa | 1861 | push @to, recipients_cmd("to-cmd", "to", $to_cmd, $t, $quiet) |
6e74e075 | 1862 | if defined $to_cmd; |
56adddaa | 1863 | push @cc, recipients_cmd("cc-cmd", "cc", $cc_cmd, $t, $quiet) |
6e74e075 | 1864 | if defined $cc_cmd && !$suppress_cc{'cccmd'}; |
324a8bd0 | 1865 | |
3cae7e5b | 1866 | if ($broken_encoding{$t} && !$has_content_type) { |
bb29456c | 1867 | $xfer_encoding = '8bit' if not defined $xfer_encoding; |
3cae7e5b | 1868 | $has_content_type = 1; |
bb29456c | 1869 | push @xh, "Content-Type: text/plain; charset=$auto_8bit_encoding"; |
3cae7e5b TR |
1870 | $body_encoding = $auto_8bit_encoding; |
1871 | } | |
1872 | ||
ce547800 KM |
1873 | if ($broken_encoding{$t} && !is_rfc2047_quoted($subject)) { |
1874 | $subject = quote_subject($subject, $auto_8bit_encoding); | |
3cae7e5b TR |
1875 | } |
1876 | ||
4cb46bdd | 1877 | if (defined $sauthor and $sauthor ne $sender) { |
94638f89 | 1878 | $message = "From: $author\n\n$message"; |
8291db6f JK |
1879 | if (defined $author_encoding) { |
1880 | if ($has_content_type) { | |
1881 | if ($body_encoding eq $author_encoding) { | |
1882 | # ok, we already have the right encoding | |
1883 | } | |
1884 | else { | |
1885 | # uh oh, we should re-encode | |
1886 | } | |
1887 | } | |
1888 | else { | |
bb29456c | 1889 | $xfer_encoding = '8bit' if not defined $xfer_encoding; |
3cae7e5b | 1890 | $has_content_type = 1; |
8291db6f | 1891 | push @xh, |
bb29456c | 1892 | "Content-Type: text/plain; charset=$author_encoding"; |
8291db6f JK |
1893 | } |
1894 | } | |
8a8e6235 | 1895 | } |
e67a228c | 1896 | $xfer_encoding = '8bit' if not defined $xfer_encoding; |
1897 | ($message, $xfer_encoding) = apply_transfer_encoding( | |
1898 | $message, $xfer_encoding, $target_xfer_encoding); | |
1899 | push @xh, "Content-Transfer-Encoding: $xfer_encoding"; | |
1900 | unshift @xh, 'MIME-Version: 1.0' unless $has_mime_version; | |
83b24437 | 1901 | |
c1f2aa45 JS |
1902 | $needs_confirm = ( |
1903 | $confirm eq "always" or | |
1904 | ($confirm =~ /^(?:auto|cc)$/ && @cc) or | |
1905 | ($confirm =~ /^(?:auto|compose)$/ && $compose && $message_num == 1)); | |
1906 | $needs_confirm = "inform" if ($needs_confirm && $confirm_unconfigured && @cc); | |
1907 | ||
b5e112d8 RL |
1908 | @to = process_address_list(@to); |
1909 | @cc = process_address_list(@cc); | |
e4312255 | 1910 | |
3c3bb51c | 1911 | @to = (@initial_to, @to); |
c1f2aa45 JS |
1912 | @cc = (@initial_cc, @cc); |
1913 | ||
f515c904 MT |
1914 | if ($message_num == 1) { |
1915 | if (defined $cover_cc and $cover_cc) { | |
1916 | @initial_cc = @cc; | |
1917 | } | |
1918 | if (defined $cover_to and $cover_to) { | |
1919 | @initial_to = @to; | |
1920 | } | |
1921 | } | |
56adddaa MS |
1922 | } |
1923 | ||
1924 | # Prepares the email, prompts the user, and sends it out | |
1925 | # Returns 0 if an edit was done and the function should be called again, or 1 | |
1926 | # on the email being successfully sent out. | |
1927 | sub process_file { | |
1928 | my ($t) = @_; | |
1929 | ||
1930 | pre_process_file($t, $quiet); | |
f515c904 | 1931 | |
15da1084 | 1932 | my $message_was_sent = send_message(); |
04c4a4e8 DD |
1933 | if ($message_was_sent == -1) { |
1934 | do_edit($t); | |
1935 | return 0; | |
1936 | } | |
83b24437 RA |
1937 | |
1938 | # set up for the next message | |
e0821134 MH |
1939 | if ($thread) { |
1940 | if ($message_was_sent && | |
1941 | ($chain_reply_to || !defined $in_reply_to || length($in_reply_to) == 0 || | |
1942 | $message_num == 1)) { | |
1943 | $in_reply_to = $message_id; | |
1944 | if (length $references > 0) { | |
1945 | $references .= "\n $message_id"; | |
1946 | } else { | |
1947 | $references = "$message_id"; | |
1948 | } | |
7ccf7927 | 1949 | } |
e0821134 MH |
1950 | } elsif (!defined $initial_in_reply_to) { |
1951 | # --thread and --in-reply-to manage the "In-Reply-To" header and by | |
1952 | # extension the "References" header. If these commands are not used, reset | |
1953 | # the header values to their defaults. | |
1954 | $in_reply_to = undef; | |
1955 | $references = ''; | |
78488b2c | 1956 | } |
4f3d3703 | 1957 | $message_id = undef; |
5453b83b | 1958 | $num_sent++; |
1959 | if (defined $batch_size && $num_sent == $batch_size) { | |
1960 | $num_sent = 0; | |
1961 | $smtp->quit if defined $smtp; | |
1962 | undef $smtp; | |
1963 | undef $auth; | |
1964 | sleep($relogin_delay) if defined $relogin_delay; | |
1965 | } | |
04c4a4e8 DD |
1966 | |
1967 | return 1; | |
1968 | } | |
1969 | ||
0d864703 MS |
1970 | sub initialize_modified_loop_vars { |
1971 | $in_reply_to = $initial_in_reply_to; | |
1972 | $references = $initial_in_reply_to || ''; | |
1973 | $message_num = 0; | |
1974 | } | |
1975 | ||
1976 | if ($validate) { | |
1977 | # FIFOs can only be read once, exclude them from validation. | |
1978 | my @real_files = (); | |
1979 | foreach my $f (@files) { | |
1980 | unless (-p $f) { | |
1981 | push(@real_files, $f); | |
1982 | } | |
1983 | } | |
1984 | ||
1985 | # Run the loop once again to avoid gaps in the counter due to FIFO | |
1986 | # arguments provided by the user. | |
1987 | my $num = 1; | |
1988 | my $num_files = scalar @real_files; | |
1989 | $ENV{GIT_SENDEMAIL_FILE_TOTAL} = "$num_files"; | |
1990 | initialize_modified_loop_vars(); | |
1991 | foreach my $r (@real_files) { | |
1992 | $ENV{GIT_SENDEMAIL_FILE_COUNTER} = "$num"; | |
1993 | pre_process_file($r, 1); | |
1994 | validate_patch($r, $target_xfer_encoding); | |
1995 | $num += 1; | |
1996 | } | |
1997 | delete $ENV{GIT_SENDEMAIL_FILE_COUNTER}; | |
1998 | delete $ENV{GIT_SENDEMAIL_FILE_TOTAL}; | |
1999 | } | |
2000 | ||
2001 | initialize_modified_loop_vars(); | |
04c4a4e8 DD |
2002 | foreach my $t (@files) { |
2003 | while (!process_file($t)) { | |
2004 | # user edited the file | |
2005 | } | |
83b24437 | 2006 | } |
e205735d | 2007 | |
3a7a18a0 MC |
2008 | # Execute a command and return its output lines as an array. Blank |
2009 | # lines which do not appear at the end of the output are reported as | |
2010 | # errors. | |
03056ce7 MC |
2011 | sub execute_cmd { |
2012 | my ($prefix, $cmd, $file) = @_; | |
2013 | my @lines = (); | |
3a7a18a0 | 2014 | my $seen_blank_line = 0; |
03056ce7 MC |
2015 | open my $fh, "-|", "$cmd \Q$file\E" |
2016 | or die sprintf(__("(%s) Could not execute '%s'"), $prefix, $cmd); | |
2017 | while (my $line = <$fh>) { | |
3a7a18a0 MC |
2018 | die sprintf(__("(%s) Malformed output from '%s'"), $prefix, $cmd) |
2019 | if $seen_blank_line; | |
2020 | if ($line =~ /^$/) { | |
2021 | $seen_blank_line = $line =~ /^$/; | |
2022 | next; | |
2023 | } | |
03056ce7 MC |
2024 | push @lines, $line; |
2025 | } | |
2026 | close $fh | |
2027 | or die sprintf(__("(%s) failed to close pipe to '%s'"), $prefix, $cmd); | |
2028 | return @lines; | |
2029 | } | |
2030 | ||
ba92106e MC |
2031 | # Process headers lines, unfolding multiline headers as defined by RFC |
2032 | # 2822. | |
2033 | sub unfold_headers { | |
2034 | my @headers; | |
2035 | foreach(@_) { | |
2036 | last if /^\s*$/; | |
2037 | if (/^\s+\S/ and @headers) { | |
2038 | chomp($headers[$#headers]); | |
2039 | s/^\s+/ /; | |
2040 | $headers[$#headers] .= $_; | |
2041 | } else { | |
2042 | push(@headers, $_); | |
2043 | } | |
2044 | } | |
2045 | return @headers; | |
2046 | } | |
2047 | ||
2048 | # Invoke the provided CMD with FILE as an argument, which should | |
2049 | # output RFC 2822 email headers. Fold multiline headers and return the | |
2050 | # headers as an array. | |
2051 | sub invoke_header_cmd { | |
2052 | my ($cmd, $file) = @_; | |
2053 | my @lines = execute_cmd("header-cmd", $header_cmd, $file); | |
2054 | return unfold_headers(@lines); | |
2055 | } | |
2056 | ||
6e74e075 JP |
2057 | # Execute a command (e.g. $to_cmd) to get a list of email addresses |
2058 | # and return a results array | |
2059 | sub recipients_cmd { | |
56adddaa | 2060 | my ($prefix, $what, $cmd, $file, $quiet) = @_; |
03056ce7 | 2061 | my @lines = (); |
6e74e075 | 2062 | my @addresses = (); |
64477d20 | 2063 | |
03056ce7 MC |
2064 | @lines = execute_cmd($prefix, $cmd, $file); |
2065 | for my $address (@lines) { | |
6e74e075 JP |
2066 | $address =~ s/^\s*//g; |
2067 | $address =~ s/\s*$//g; | |
2068 | $address = sanitize_address($address); | |
da18759e | 2069 | next if ($address eq $sender and $suppress_cc{'self'}); |
6e74e075 | 2070 | push @addresses, $address; |
3c5cd20c VA |
2071 | printf(__("(%s) Adding %s: %s from: '%s'\n"), |
2072 | $prefix, $what, $address, $cmd) unless $quiet; | |
6e74e075 | 2073 | } |
6e74e075 JP |
2074 | return @addresses; |
2075 | } | |
2076 | ||
c1f2aa45 | 2077 | cleanup_compose_files(); |
1f038a0c | 2078 | |
4bf597ee | 2079 | sub cleanup_compose_files { |
c1f2aa45 | 2080 | unlink($compose_filename, $compose_filename . ".final") if $compose; |
1f038a0c RA |
2081 | } |
2082 | ||
4bc87a28 | 2083 | $smtp->quit if $smtp; |
e205735d | 2084 | |
8d814084 PB |
2085 | sub apply_transfer_encoding { |
2086 | my $message = shift; | |
2087 | my $from = shift; | |
2088 | my $to = shift; | |
2089 | ||
3c88e46f | 2090 | return ($message, $to) if ($from eq $to and $from ne '7bit'); |
8d814084 PB |
2091 | |
2092 | require MIME::QuotedPrint; | |
2093 | require MIME::Base64; | |
2094 | ||
2095 | $message = MIME::QuotedPrint::decode($message) | |
2096 | if ($from eq 'quoted-printable'); | |
2097 | $message = MIME::Base64::decode($message) | |
2098 | if ($from eq 'base64'); | |
2099 | ||
74d76a17 | 2100 | $to = ($message =~ /(?:.{999,}|\r)/) ? 'quoted-printable' : '8bit' |
7a36987f | 2101 | if $to eq 'auto'; |
2102 | ||
46493105 | 2103 | die __("cannot send message as 7bit") |
8d814084 | 2104 | if ($to eq '7bit' and $message =~ /[^[:ascii:]]/); |
7a36987f | 2105 | return ($message, $to) |
8d814084 | 2106 | if ($to eq '7bit' or $to eq '8bit'); |
7a36987f | 2107 | return (MIME::QuotedPrint::encode($message, "\n", 0), $to) |
8d814084 | 2108 | if ($to eq 'quoted-printable'); |
7a36987f | 2109 | return (MIME::Base64::encode($message, "\n"), $to) |
8d814084 | 2110 | if ($to eq 'base64'); |
46493105 | 2111 | die __("invalid transfer encoding"); |
8d814084 PB |
2112 | } |
2113 | ||
c438ea2a | 2114 | sub unique_email_list { |
e205735d RA |
2115 | my %seen; |
2116 | my @emails; | |
2117 | ||
2118 | foreach my $entry (@_) { | |
e4312255 KM |
2119 | my $clean = extract_valid_address_or_die($entry); |
2120 | $seen{$clean} ||= 0; | |
2121 | next if $seen{$clean}++; | |
2122 | push @emails, $entry; | |
e205735d RA |
2123 | } |
2124 | return @emails; | |
2125 | } | |
747bbff9 JK |
2126 | |
2127 | sub validate_patch { | |
f2d06fb1 | 2128 | my ($fn, $xfer_encoding) = @_; |
6489660b | 2129 | |
177409e5 | 2130 | if ($repo) { |
a7555304 | 2131 | my $hook_name = 'sendemail-validate'; |
7cbc0455 | 2132 | my $hooks_path = $repo->command_oneline('rev-parse', '--git-path', 'hooks'); |
f4dc9432 | 2133 | require File::Spec; |
a7555304 | 2134 | my $validate_hook = File::Spec->catfile($hooks_path, $hook_name); |
177409e5 JT |
2135 | my $hook_error; |
2136 | if (-x $validate_hook) { | |
f4dc9432 ÆAB |
2137 | require Cwd; |
2138 | my $target = Cwd::abs_path($fn); | |
177409e5 | 2139 | # The hook needs a correct cwd and GIT_DIR. |
17530b2e | 2140 | my $cwd_save = Cwd::getcwd(); |
177409e5 JT |
2141 | chdir($repo->wc_path() or $repo->repo_path()) |
2142 | or die("chdir: $!"); | |
2143 | local $ENV{"GIT_DIR"} = $repo->repo_path(); | |
a8022c5f MS |
2144 | |
2145 | my ($recipients_ref, $to, $date, $gitversion, $cc, $ccline, $header) = gen_header(); | |
2146 | ||
2147 | require File::Temp; | |
2148 | my ($header_filehandle, $header_filename) = File::Temp::tempfile( | |
2149 | TEMPLATE => ".gitsendemail.header.XXXXXX", | |
2150 | DIR => $repo->repo_path(), | |
2151 | UNLINK => 1, | |
2152 | ); | |
2153 | print $header_filehandle $header; | |
2154 | ||
a7555304 ES |
2155 | my @cmd = ("git", "hook", "run", "--ignore-missing", |
2156 | $hook_name, "--"); | |
a8022c5f MS |
2157 | my @cmd_msg = (@cmd, "<patch>", "<header>"); |
2158 | my @cmd_run = (@cmd, $target, $header_filename); | |
a7555304 | 2159 | $hook_error = system_or_msg(\@cmd_run, undef, "@cmd_msg"); |
177409e5 JT |
2160 | chdir($cwd_save) or die("chdir: $!"); |
2161 | } | |
d21616c0 | 2162 | if ($hook_error) { |
af15f84d JNA |
2163 | $hook_error = sprintf( |
2164 | __("fatal: %s: rejected by %s hook\n%s\nwarning: no patches were sent\n"), | |
2165 | $fn, $hook_name, $hook_error); | |
a7555304 | 2166 | die $hook_error; |
d21616c0 | 2167 | } |
177409e5 | 2168 | } |
6489660b | 2169 | |
f2d06fb1 | 2170 | # Any long lines will be automatically fixed if we use a suitable transfer |
2171 | # encoding. | |
2172 | unless ($xfer_encoding =~ /^(?:auto|quoted-printable|base64)$/) { | |
2173 | open(my $fh, '<', $fn) | |
2174 | or die sprintf(__("unable to open %s: %s\n"), $fn, $!); | |
2175 | while (my $line = <$fh>) { | |
2176 | if (length($line) > 998) { | |
ea7811b3 ÆAB |
2177 | die sprintf(__("fatal: %s:%d is longer than 998 characters\n" . |
2178 | "warning: no patches were sent\n"), $fn, $.); | |
f2d06fb1 | 2179 | } |
747bbff9 JK |
2180 | } |
2181 | } | |
622bc930 | 2182 | return; |
747bbff9 | 2183 | } |
0706bd19 | 2184 | |
531220ba JH |
2185 | sub handle_backup { |
2186 | my ($last, $lastlen, $file, $known_suffix) = @_; | |
2187 | my ($suffix, $skip); | |
2188 | ||
2189 | $skip = 0; | |
2190 | if (defined $last && | |
2191 | ($lastlen < length($file)) && | |
2192 | (substr($file, 0, $lastlen) eq $last) && | |
2193 | ($suffix = substr($file, $lastlen)) !~ /^[a-z0-9]/i) { | |
2194 | if (defined $known_suffix && $suffix eq $known_suffix) { | |
3c5cd20c | 2195 | printf(__("Skipping %s with backup suffix '%s'.\n"), $file, $known_suffix); |
531220ba JH |
2196 | $skip = 1; |
2197 | } else { | |
3c5cd20c VA |
2198 | # TRANSLATORS: please keep "[y|N]" as is. |
2199 | my $answer = ask(sprintf(__("Do you really want to send %s? [y|N]: "), $file), | |
531220ba JH |
2200 | valid_re => qr/^(?:y|n)/i, |
2201 | default => 'n'); | |
2202 | $skip = ($answer ne 'y'); | |
2203 | if ($skip) { | |
2204 | $known_suffix = $suffix; | |
2205 | } | |
2206 | } | |
2207 | } | |
2208 | return ($skip, $known_suffix); | |
2209 | } | |
2210 | ||
2211 | sub handle_backup_files { | |
2212 | my @file = @_; | |
2213 | my ($last, $lastlen, $known_suffix, $skip, @result); | |
2214 | for my $file (@file) { | |
2215 | ($skip, $known_suffix) = handle_backup($last, $lastlen, | |
2216 | $file, $known_suffix); | |
2217 | push @result, $file unless $skip; | |
2218 | $last = $file; | |
2219 | $lastlen = length($file); | |
2220 | } | |
2221 | return @result; | |
2222 | } | |
2223 | ||
0706bd19 JK |
2224 | sub file_has_nonascii { |
2225 | my $fn = shift; | |
2226 | open(my $fh, '<', $fn) | |
3c5cd20c | 2227 | or die sprintf(__("unable to open %s: %s\n"), $fn, $!); |
0706bd19 JK |
2228 | while (my $line = <$fh>) { |
2229 | return 1 if $line =~ /[^[:ascii:]]/; | |
2230 | } | |
2231 | return 0; | |
2232 | } | |
3cae7e5b TR |
2233 | |
2234 | sub body_or_subject_has_nonascii { | |
2235 | my $fn = shift; | |
2236 | open(my $fh, '<', $fn) | |
3c5cd20c | 2237 | or die sprintf(__("unable to open %s: %s\n"), $fn, $!); |
3cae7e5b TR |
2238 | while (my $line = <$fh>) { |
2239 | last if $line =~ /^$/; | |
2240 | return 1 if $line =~ /^Subject.*[^[:ascii:]]/; | |
2241 | } | |
2242 | while (my $line = <$fh>) { | |
2243 | return 1 if $line =~ /[^[:ascii:]]/; | |
2244 | } | |
2245 | return 0; | |
2246 | } |