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