]> git.ipfire.org Git - thirdparty/git.git/blame - git-send-email.perl
git-apply: fix whitespace stripping
[thirdparty/git.git] / git-send-email.perl
CommitLineData
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
19use strict;
20use warnings;
21use Term::ReadLine;
83b24437
RA
22use Getopt::Long;
23use Data::Dumper;
412876dc 24use Term::ANSIColor;
3cb8caf7 25use Git;
83b24437 26
412876dc
SE
27$SIG{INT} = sub { print color("reset"), "\n"; exit };
28
280242d1
JH
29package FakeTerm;
30sub new {
31 my ($class, $reason) = @_;
32 return bless \$reason, shift;
33}
34sub readline {
35 my $self = shift;
36 die "Cannot use readline on FakeTerm: $$self";
37}
38package main;
39
1b0baf14
MC
40
41sub usage {
42 print <<EOT;
43git-send-email [options] <file | directory>...
44Options:
45 --from Specify the "From:" line of the email to be sent.
46
47 --to Specify the primary "To:" line of the email.
48
49 --cc Specify an initial "Cc:" list for the entire series
50 of emails.
51
324a8bd0
JP
52 --cc-cmd Specify a command to execute per file which adds
53 per file specific cc address entries
54
1b0baf14
MC
55 --bcc Specify a list of email addresses that should be Bcc:
56 on all the emails.
57
ef0c2abf
AR
58 --compose Use \$GIT_EDITOR, core.editor, \$EDITOR, or \$VISUAL to edit
59 an introductory message for the patch series.
1b0baf14
MC
60
61 --subject Specify the initial "Subject:" line.
62 Only necessary if --compose is also set. If --compose
63 is not set, this will be prompted for.
64
65 --in-reply-to Specify the first "In-Reply-To:" header line.
66 Only used if --compose is also set. If --compose is not
67 set, this will be prompted for.
68
69 --chain-reply-to If set, the replies will all be to the previous
70 email sent, rather than to the first email sent.
71 Defaults to on.
72
5483c71d
AR
73 --signed-off-cc Automatically add email addresses that appear in
74 Signed-off-by: or Cc: lines to the cc: list. Defaults to on.
1b0baf14
MC
75
76 --smtp-server If set, specifies the outgoing SMTP server to use.
77 Defaults to localhost.
78
79 --suppress-from Suppress sending emails to yourself if your address
5483c71d 80 appears in a From: line. Defaults to off.
1b0baf14 81
5483c71d 82 --thread Specify that the "In-Reply-To:" header should be set on all
e46f7a0e
AR
83 emails. Defaults to on.
84
1b0baf14
MC
85 --quiet Make git-send-email less verbose. One line per email
86 should be all that is output.
87
238cc635
RJ
88 --dry-run Do everything except actually send the emails.
89
f073a592
RJ
90 --envelope-sender Specify the envelope sender used to send the emails.
91
1b0baf14
MC
92EOT
93 exit(1);
94}
95
4bc87a28 96# most mail servers generate the Date: header, but not all...
6bdca890
JN
97sub format_2822_time {
98 my ($time) = @_;
99 my @localtm = localtime($time);
100 my @gmttm = gmtime($time);
101 my $localmin = $localtm[1] + $localtm[2] * 60;
102 my $gmtmin = $gmttm[1] + $gmttm[2] * 60;
103 if ($localtm[0] != $gmttm[0]) {
104 die "local zone differs from GMT by a non-minute interval\n";
105 }
106 if ((($gmttm[6] + 1) % 7) == $localtm[6]) {
107 $localmin += 1440;
108 } elsif ((($gmttm[6] - 1) % 7) == $localtm[6]) {
109 $localmin -= 1440;
110 } elsif ($gmttm[6] != $localtm[6]) {
111 die "local time offset greater than or equal to 24 hours\n";
112 }
113 my $offset = $localmin - $gmtmin;
114 my $offhour = $offset / 60;
115 my $offmin = abs($offset % 60);
116 if (abs($offhour) >= 24) {
117 die ("local time offset greater than or equal to 24 hours\n");
118 }
119
120 return sprintf("%s, %2d %s %d %02d:%02d:%02d %s%02d%02d",
121 qw(Sun Mon Tue Wed Thu Fri Sat)[$localtm[6]],
122 $localtm[3],
123 qw(Jan Feb Mar Apr May Jun
124 Jul Aug Sep Oct Nov Dec)[$localtm[4]],
125 $localtm[5]+1900,
126 $localtm[2],
127 $localtm[1],
128 $localtm[0],
129 ($offset >= 0) ? '+' : '-',
130 abs($offhour),
131 $offmin,
132 );
133}
4bc87a28 134
567ffeb7 135my $have_email_valid = eval { require Email::Valid; 1 };
4bc87a28
EW
136my $smtp;
137
e205735d 138sub unique_email_list(@);
1f038a0c
RA
139sub cleanup_compose_files();
140
141# Constants (essentially)
142my $compose_filename = ".msg.$$";
e205735d 143
83b24437 144# Variables we fill in automatically, or via prompting:
ce91c2f6 145my (@to,@cc,@initial_cc,@bcclist,@xh,
94638f89 146 $initial_reply_to,$initial_subject,@files,$author,$sender,$compose,$time);
83b24437 147
aca7ad76 148my $smtp_server;
f073a592 149my $envelope_sender;
78488b2c 150
9133261f 151# Example reply to:
83b24437 152#$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
83b24437 153
3cb8caf7 154my $repo = Git->repository();
280242d1
JH
155my $term = eval {
156 new Term::ReadLine 'git-send-email';
157};
158if ($@) {
159 $term = new FakeTerm "$@: going non-interactive";
160}
83b24437 161
5483c71d
AR
162# Behavior modification variables
163my ($quiet, $dry_run) = (0, 0);
164
165# Variables with corresponding config settings
324a8bd0 166my ($thread, $chain_reply_to, $suppress_from, $signed_off_cc, $cc_cmd);
5483c71d 167
e46f7a0e 168my %config_settings = (
5483c71d
AR
169 "thread" => [\$thread, 1],
170 "chainreplyto" => [\$chain_reply_to, 1],
171 "suppressfrom" => [\$suppress_from, 0],
172 "signedoffcc" => [\$signed_off_cc, 1],
324a8bd0 173 "cccmd" => [\$cc_cmd, ""],
e46f7a0e
AR
174);
175
176foreach my $setting (keys %config_settings) {
5483c71d
AR
177 my $config = $repo->config_bool("sendemail.$setting");
178 ${$config_settings{$setting}->[0]} = (defined $config) ? $config : $config_settings{$setting}->[1];
4a62d3f5
AK
179}
180
181@bcclist = $repo->config('sendemail.bcc');
182if (!@bcclist or !$bcclist[0]) {
183 @bcclist = ();
184}
185
83b24437
RA
186# Begin by accumulating all the variables (defined above), that we will end up
187# needing, first, from the command line:
188
94638f89 189my $rc = GetOptions("sender|from=s" => \$sender,
83b24437
RA
190 "in-reply-to=s" => \$initial_reply_to,
191 "subject=s" => \$initial_subject,
192 "to=s" => \@to,
da140f8b 193 "cc=s" => \@initial_cc,
58063245 194 "bcc=s" => \@bcclist,
78488b2c 195 "chain-reply-to!" => \$chain_reply_to,
3342d850 196 "smtp-server=s" => \$smtp_server,
1f038a0c 197 "compose" => \$compose,
30d08b34 198 "quiet" => \$quiet,
324a8bd0 199 "cc-cmd=s" => \$cc_cmd,
5483c71d
AR
200 "suppress-from!" => \$suppress_from,
201 "signed-off-cc|signed-off-by-cc!" => \$signed_off_cc,
6130259c 202 "dry-run" => \$dry_run,
f073a592 203 "envelope-sender=s" => \$envelope_sender,
5483c71d 204 "thread!" => \$thread,
83b24437
RA
205 );
206
1b0baf14
MC
207unless ($rc) {
208 usage();
209}
210
79ee555b
EB
211# Verify the user input
212
213foreach my $entry (@to) {
214 die "Comma in --to entry: $entry'\n" unless $entry !~ m/,/;
215}
216
217foreach my $entry (@initial_cc) {
218 die "Comma in --cc entry: $entry'\n" unless $entry !~ m/,/;
219}
220
221foreach my $entry (@bcclist) {
222 die "Comma in --bcclist entry: $entry'\n" unless $entry !~ m/,/;
223}
224
83b24437
RA
225# Now, let's fill any that aren't set in with defaults:
226
94638f89
UKK
227my ($repoauthor) = $repo->ident_person('author');
228my ($repocommitter) = $repo->ident_person('committer');
83b24437 229
994d6c66 230my %aliases;
3cb8caf7
PB
231my @alias_files = $repo->config('sendemail.aliasesfile');
232my $aliasfiletype = $repo->config('sendemail.aliasfiletype');
994d6c66
EW
233my %parse_alias = (
234 # multiline formats can be supported in the future
235 mutt => sub { my $fh = shift; while (<$fh>) {
504ceab6 236 if (/^\s*alias\s+(\S+)\s+(.*)$/) {
994d6c66
EW
237 my ($alias, $addr) = ($1, $2);
238 $addr =~ s/#.*$//; # mutt allows # comments
239 # commas delimit multiple addresses
240 $aliases{$alias} = [ split(/\s*,\s*/, $addr) ];
241 }}},
242 mailrc => sub { my $fh = shift; while (<$fh>) {
243 if (/^alias\s+(\S+)\s+(.*)$/) {
244 # spaces delimit multiple addresses
245 $aliases{$1} = [ split(/\s+/, $2) ];
246 }}},
247 pine => sub { my $fh = shift; while (<$fh>) {
2d8ae400 248 if (/^(\S+)\t.*\t(.*)$/) {
994d6c66
EW
249 $aliases{$1} = [ split(/\s*,\s*/, $2) ];
250 }}},
251 gnus => sub { my $fh = shift; while (<$fh>) {
252 if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
253 $aliases{$1} = [ $2 ];
254 }}}
255);
256
3cb8caf7 257if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) {
994d6c66
EW
258 foreach my $file (@alias_files) {
259 open my $fh, '<', $file or die "opening $file: $!\n";
260 $parse_alias{$aliasfiletype}->($fh);
261 close $fh;
262 }
263}
264
94638f89 265($sender) = expand_aliases($sender) if defined $sender;
ae740a58 266
1f038a0c 267my $prompting = 0;
94638f89
UKK
268if (!defined $sender) {
269 $sender = $repoauthor || $repocommitter;
8037d1a3 270 do {
94638f89 271 $_ = $term->readline("Who should the emails appear to be from? [$sender] ");
ca9a7d65 272 } while (!defined $_);
8037d1a3 273
94638f89
UKK
274 $sender = $_ if ($_);
275 print "Emails will be sent from: ", $sender, "\n";
1f038a0c 276 $prompting++;
83b24437
RA
277}
278
279if (!@to) {
8037d1a3 280 do {
5825e5b2 281 $_ = $term->readline("Who should the emails be sent to? ",
8037d1a3
RA
282 "");
283 } while (!defined $_);
83b24437
RA
284 my $to = $_;
285 push @to, split /,/, $to;
1f038a0c 286 $prompting++;
83b24437
RA
287}
288
994d6c66
EW
289sub expand_aliases {
290 my @cur = @_;
291 my @last;
292 do {
293 @last = @cur;
294 @cur = map { $aliases{$_} ? @{$aliases{$_}} : $_ } @last;
295 } while (join(',',@cur) ne join(',',@last));
296 return @cur;
297}
298
299@to = expand_aliases(@to);
5b56aaa2 300@to = (map { sanitize_address($_) } @to);
994d6c66 301@initial_cc = expand_aliases(@initial_cc);
58063245 302@bcclist = expand_aliases(@bcclist);
994d6c66 303
1f038a0c 304if (!defined $initial_subject && $compose) {
8037d1a3 305 do {
5825e5b2 306 $_ = $term->readline("What subject should the emails start with? ",
8037d1a3
RA
307 $initial_subject);
308 } while (!defined $_);
83b24437 309 $initial_subject = $_;
1f038a0c 310 $prompting++;
83b24437
RA
311}
312
5483c71d 313if ($thread && !defined $initial_reply_to && $prompting) {
8037d1a3 314 do {
1f038a0c 315 $_= $term->readline("Message-ID to be used as In-Reply-To for the first email? ",
8037d1a3
RA
316 $initial_reply_to);
317 } while (!defined $_);
318
83b24437 319 $initial_reply_to = $_;
3803bcea
DK
320 $initial_reply_to =~ s/^\s+<?/</;
321 $initial_reply_to =~ s/>?\s+$/>/;
83b24437
RA
322}
323
6dcfa306
SV
324if (!$smtp_server) {
325 $smtp_server = $repo->config('sendemail.smtpserver');
326}
aca7ad76
EW
327if (!$smtp_server) {
328 foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) {
329 if (-x $_) {
330 $smtp_server = $_;
331 last;
332 }
333 }
334 $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
3342d850
RA
335}
336
1f038a0c
RA
337if ($compose) {
338 # Note that this does not need to be secure, but we will make a small
339 # effort to have it be unique
340 open(C,">",$compose_filename)
341 or die "Failed to open for writing $compose_filename: $!";
94638f89 342 print C "From $sender # This line is ignored.\n";
1f038a0c
RA
343 printf C "Subject: %s\n\n", $initial_subject;
344 printf C <<EOT;
345GIT: Please enter your email below.
346GIT: Lines beginning in "GIT: " will be removed.
347GIT: Consider including an overall diffstat or table of contents
348GIT: for the patch you are writing.
349
350EOT
351 close(C);
352
ef0c2abf 353 my $editor = $ENV{GIT_EDITOR} || $repo->config("core.editor") || $ENV{VISUAL} || $ENV{EDITOR} || "vi";
1f038a0c
RA
354 system($editor, $compose_filename);
355
356 open(C2,">",$compose_filename . ".final")
357 or die "Failed to open $compose_filename.final : " . $!;
358
359 open(C,"<",$compose_filename)
360 or die "Failed to open $compose_filename : " . $!;
361
362 while(<C>) {
363 next if m/^GIT: /;
364 print C2 $_;
365 }
366 close(C);
367 close(C2);
368
369 do {
370 $_ = $term->readline("Send this email? (y|n) ");
371 } while (!defined $_);
372
373 if (uc substr($_,0,1) ne 'Y') {
374 cleanup_compose_files();
375 exit(0);
376 }
377
378 @files = ($compose_filename . ".final");
379}
380
381
83b24437
RA
382# Now that all the defaults are set, process the rest of the command line
383# arguments and collect up the files that need to be processed.
384for my $f (@ARGV) {
385 if (-d $f) {
386 opendir(DH,$f)
387 or die "Failed to opendir $f: $!";
388
5825e5b2 389 push @files, grep { -f $_ } map { +$f . "/" . $_ }
8037d1a3
RA
390 sort readdir(DH);
391
83b24437
RA
392 } elsif (-f $f) {
393 push @files, $f;
394
395 } else {
396 print STDERR "Skipping $f - not found.\n";
397 }
398}
399
400if (@files) {
2718435b
RA
401 unless ($quiet) {
402 print $_,"\n" for (@files);
403 }
83b24437 404} else {
1b0baf14
MC
405 print STDERR "\nNo patch files specified!\n\n";
406 usage();
83b24437
RA
407}
408
409# Variables we set as part of the loop over files
af068d27 410our ($message_id, %mail, $subject, $reply_to, $references, $message);
83b24437 411
567ffeb7
EW
412sub extract_valid_address {
413 my $address = shift;
ad9c18f5 414 my $local_part_regexp = '[^<>"\s@]+';
09302e17 415 my $domain_regexp = '[^.<>"\s@]+(?:\.[^.<>"\s@]+)+';
db3106b2
EW
416
417 # check for a local address:
ad9c18f5 418 return $address if ($address =~ /^($local_part_regexp)$/);
db3106b2 419
155197e6 420 $address =~ s/^\s*<(.*)>\s*$/$1/;
567ffeb7 421 if ($have_email_valid) {
ad9c18f5 422 return scalar Email::Valid->address($address);
567ffeb7
EW
423 } else {
424 # less robust/correct than the monster regexp in Email::Valid,
425 # but still does a 99% job, and one less dependency
ad9c18f5 426 $address =~ /($local_part_regexp\@$domain_regexp)/;
e96fd305 427 return $1;
567ffeb7
EW
428 }
429}
83b24437
RA
430
431# Usually don't need to change anything below here.
432
433# we make a "fake" message id by taking the current number
434# of seconds since the beginning of Unix time and tacking on
435# a random number to the end, in case we are called quicker than
436# 1 second since the last time we were called.
8037d1a3
RA
437
438# We'll setup a template for the message id, using the "from" address:
8037d1a3 439
83b24437
RA
440sub make_message_id
441{
72095d5c 442 my $date = time;
83b24437 443 my $pseudo_rand = int (rand(4200));
aeb59328 444 my $du_part;
94638f89
UKK
445 for ($sender, $repocommitter, $repoauthor) {
446 $du_part = extract_valid_address(sanitize_address($_));
447 last if (defined $du_part and $du_part ne '');
aeb59328 448 }
94638f89 449 if (not defined $du_part or $du_part eq '') {
aeb59328
JH
450 use Sys::Hostname qw();
451 $du_part = 'user@' . Sys::Hostname::hostname();
452 }
453 my $message_id_template = "<%s-git-send-email-$du_part>";
8037d1a3
RA
454 $message_id = sprintf $message_id_template, "$date$pseudo_rand";
455 #print "new message id = $message_id\n"; # Was useful for debugging
83b24437
RA
456}
457
458
459
a5370b16 460$time = time - scalar $#files;
83b24437 461
374c5905
JR
462sub unquote_rfc2047 {
463 local ($_) = @_;
464 if (s/=\?utf-8\?q\?(.*)\?=/$1/g) {
465 s/_/ /g;
466 s/=([0-9A-F]{2})/chr(hex($1))/eg;
467 }
3740b04f 468 return "$_";
374c5905
JR
469}
470
5b56aaa2
UKK
471# use the simplest quoting being able to handle the recipient
472sub sanitize_address
732263d4
RJ
473{
474 my ($recipient) = @_;
5b56aaa2
UKK
475 my ($recipient_name, $recipient_addr) = ($recipient =~ /^(.*?)\s*(<.*)/);
476
477 if (not $recipient_name) {
478 return "$recipient";
479 }
480
481 # if recipient_name is already quoted, do nothing
482 if ($recipient_name =~ /^(".*"|=\?utf-8\?q\?.*\?=)$/) {
483 return $recipient;
484 }
485
486 # rfc2047 is needed if a non-ascii char is included
487 if ($recipient_name =~ /[^[:ascii:]]/) {
488 $recipient_name =~ s/([^-a-zA-Z0-9!*+\/])/sprintf("=%02X", ord($1))/eg;
489 $recipient_name =~ s/(.*)/=\?utf-8\?q\?$1\?=/;
732263d4 490 }
5b56aaa2
UKK
491
492 # double quotes are needed if specials or CTLs are included
493 elsif ($recipient_name =~ /[][()<>@,;:\\".\000-\037\177]/) {
494 $recipient_name =~ s/(["\\\r])/\\$1/;
495 $recipient_name = "\"$recipient_name\"";
496 }
497
498 return "$recipient_name $recipient_addr";
499
732263d4
RJ
500}
501
83b24437
RA
502sub send_message
503{
4bc87a28 504 my @recipients = unique_email_list(@to);
5b56aaa2 505 @cc = (map { sanitize_address($_) } @cc);
4bc87a28 506 my $to = join (",\n\t", @recipients);
58063245 507 @recipients = unique_email_list(@recipients,@cc,@bcclist);
c38f0247 508 @recipients = (map { extract_valid_address($_) } @recipients);
6bdca890 509 my $date = format_2822_time($time++);
e923effb
ML
510 my $gitversion = '@@GIT_VERSION@@';
511 if ($gitversion =~ m/..GIT_VERSION../) {
3cb8caf7 512 $gitversion = Git::version();
e923effb 513 }
4bc87a28 514
af068d27 515 my $cc = join(", ", unique_email_list(@cc));
f06a6a49
JH
516 my $ccline = "";
517 if ($cc ne '') {
518 $ccline = "\nCc: $cc";
519 }
94638f89 520 my $sanitized_sender = sanitize_address($sender);
aeb59328
JH
521 make_message_id();
522
94638f89 523 my $header = "From: $sanitized_sender
f06a6a49 524To: $to${ccline}
4bc87a28 525Subject: $subject
4bc87a28
EW
526Date: $date
527Message-Id: $message_id
e923effb 528X-Mailer: git-send-email $gitversion
4bc87a28 529";
5483c71d 530 if ($thread && $reply_to) {
7ccf7927
RA
531
532 $header .= "In-Reply-To: $reply_to\n";
533 $header .= "References: $references\n";
534 }
ce91c2f6
JH
535 if (@xh) {
536 $header .= join("\n", @xh) . "\n";
537 }
4bc87a28 538
c38f0247 539 my @sendmail_parameters = ('-i', @recipients);
94638f89 540 my $raw_from = $sanitized_sender;
f073a592
RJ
541 $raw_from = $envelope_sender if (defined $envelope_sender);
542 $raw_from = extract_valid_address($raw_from);
543 unshift (@sendmail_parameters,
544 '-f', $raw_from) if(defined $envelope_sender);
8e3d436b 545
6130259c
MW
546 if ($dry_run) {
547 # We don't want to send the email.
548 } elsif ($smtp_server =~ m#^/#) {
aca7ad76
EW
549 my $pid = open my $sm, '|-';
550 defined $pid or die $!;
551 if (!$pid) {
8e3d436b 552 exec($smtp_server, @sendmail_parameters) or die $!;
aca7ad76
EW
553 }
554 print $sm "$header\n$message";
555 close $sm or die $?;
556 } else {
87840620 557 require Net::SMTP;
aca7ad76 558 $smtp ||= Net::SMTP->new( $smtp_server );
2b69bfc2 559 $smtp->mail( $raw_from ) or die $smtp->message;
aca7ad76
EW
560 $smtp->to( @recipients ) or die $smtp->message;
561 $smtp->data or die $smtp->message;
562 $smtp->datasend("$header\n$message") or die $smtp->message;
563 $smtp->dataend() or die $smtp->message;
564 $smtp->ok or die "Failed to send $subject\n".$smtp->message;
565 }
2718435b 566 if ($quiet) {
71c7da94 567 printf (($dry_run ? "Dry-" : "")."Sent %s\n", $subject);
2718435b 568 } else {
71c7da94 569 print (($dry_run ? "Dry-" : "")."OK. Log says:\nDate: $date\n");
2b69bfc2 570 if ($smtp_server !~ m#^/#) {
aca7ad76 571 print "Server: $smtp_server\n";
2b69bfc2
RJ
572 print "MAIL FROM:<$raw_from>\n";
573 print "RCPT TO:".join(',',(map { "<$_>" } @recipients))."\n";
aca7ad76 574 } else {
8e3d436b 575 print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n";
aca7ad76 576 }
94638f89 577 print "From: $sanitized_sender\nSubject: $subject\nCc: $cc\nTo: $to\n\n";
aca7ad76
EW
578 if ($smtp) {
579 print "Result: ", $smtp->code, ' ',
580 ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
581 } else {
582 print "Result: OK\n";
583 }
30d08b34 584 }
83b24437
RA
585}
586
83b24437 587$reply_to = $initial_reply_to;
2186d566 588$references = $initial_reply_to || '';
83b24437
RA
589$subject = $initial_subject;
590
591foreach my $t (@files) {
83b24437
RA
592 open(F,"<",$t) or die "can't open file $t";
593
94638f89 594 my $author = undef;
da140f8b 595 @cc = @initial_cc;
ce91c2f6 596 @xh = ();
e6b0964a 597 my $input_format = undef;
83b24437
RA
598 my $header_done = 0;
599 $message = "";
600 while(<F>) {
601 if (!$header_done) {
e6b0964a
JH
602 if (/^From /) {
603 $input_format = 'mbox';
604 next;
605 }
83b24437 606 chomp;
e6b0964a
JH
607 if (!defined $input_format && /^[-A-Za-z]+:\s/) {
608 $input_format = 'mbox';
609 }
83b24437 610
e6b0964a 611 if (defined $input_format && $input_format eq 'mbox') {
83b24437
RA
612 if (/^Subject:\s+(.*)$/) {
613 $subject = $1;
614
615 } elsif (/^(Cc|From):\s+(.*)$/) {
94638f89 616 if (unquote_rfc2047($2) eq $sender) {
8a8e6235
JH
617 next if ($suppress_from);
618 }
68d42c41 619 elsif ($1 eq 'From') {
94638f89 620 $author = unquote_rfc2047($2);
8a8e6235 621 }
83b24437 622 printf("(mbox) Adding cc: %s from line '%s'\n",
2718435b 623 $2, $_) unless $quiet;
83b24437
RA
624 push @cc, $2;
625 }
1d6a003a 626 elsif (!/^Date:\s/ && /^[-A-Za-z]+:\s+\S/) {
ce91c2f6
JH
627 push @xh, $_;
628 }
83b24437
RA
629
630 } else {
631 # In the traditional
632 # "send lots of email" format,
633 # line 1 = cc
634 # line 2 = subject
635 # So let's support that, too.
e6b0964a 636 $input_format = 'lots';
83b24437
RA
637 if (@cc == 0) {
638 printf("(non-mbox) Adding cc: %s from line '%s'\n",
2718435b 639 $_, $_) unless $quiet;
83b24437
RA
640
641 push @cc, $_;
642
643 } elsif (!defined $subject) {
644 $subject = $_;
645 }
646 }
5825e5b2 647
83b24437
RA
648 # A whitespace line will terminate the headers
649 if (m/^\s*$/) {
650 $header_done = 1;
651 }
652 } else {
653 $message .= $_;
5483c71d 654 if (/^(Signed-off-by|Cc): (.*)$/i && $signed_off_cc) {
abec100c 655 my $c = $2;
83b24437
RA
656 chomp $c;
657 push @cc, $c;
658 printf("(sob) Adding cc: %s from line '%s'\n",
2718435b 659 $c, $_) unless $quiet;
83b24437
RA
660 }
661 }
662 }
663 close F;
324a8bd0
JP
664
665 if ($cc_cmd ne "") {
666 open(F, "$cc_cmd $t |")
667 or die "(cc-cmd) Could not execute '$cc_cmd'";
668 while(<F>) {
669 my $c = $_;
670 $c =~ s/^\s*//g;
671 $c =~ s/\n$//g;
672 push @cc, $c;
673 printf("(cc-cmd) Adding cc: %s from: '%s'\n",
674 $c, $cc_cmd) unless $quiet;
675 }
676 close F
677 or die "(cc-cmd) failed to close pipe to '$cc_cmd'";
678 }
679
94638f89
UKK
680 if (defined $author) {
681 $message = "From: $author\n\n$message";
8a8e6235 682 }
83b24437 683
83b24437
RA
684 send_message();
685
686 # set up for the next message
bc108f63 687 if ($chain_reply_to || !defined $reply_to || length($reply_to) == 0) {
78488b2c 688 $reply_to = $message_id;
7ccf7927 689 if (length $references > 0) {
a925b89c 690 $references .= "\n $message_id";
7ccf7927
RA
691 } else {
692 $references = "$message_id";
693 }
78488b2c 694 }
83b24437 695}
e205735d 696
1f038a0c
RA
697if ($compose) {
698 cleanup_compose_files();
699}
700
701sub cleanup_compose_files() {
702 unlink($compose_filename, $compose_filename . ".final");
703
704}
705
4bc87a28 706$smtp->quit if $smtp;
e205735d
RA
707
708sub unique_email_list(@) {
709 my %seen;
710 my @emails;
711
712 foreach my $entry (@_) {
db3106b2
EW
713 if (my $clean = extract_valid_address($entry)) {
714 $seen{$clean} ||= 0;
715 next if $seen{$clean}++;
716 push @emails, $entry;
717 } else {
718 print STDERR "W: unable to extract a valid address",
719 " from: $entry\n";
720 }
e205735d
RA
721 }
722 return @emails;
723}