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