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