]>
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; | |
83b24437 | 24 | |
4bc87a28 EW |
25 | # most mail servers generate the Date: header, but not all... |
26 | $ENV{LC_ALL} = 'C'; | |
27 | use POSIX qw/strftime/; | |
28 | ||
567ffeb7 | 29 | my $have_email_valid = eval { require Email::Valid; 1 }; |
4bc87a28 EW |
30 | my $smtp; |
31 | ||
e205735d | 32 | sub unique_email_list(@); |
1f038a0c RA |
33 | sub cleanup_compose_files(); |
34 | ||
35 | # Constants (essentially) | |
36 | my $compose_filename = ".msg.$$"; | |
e205735d | 37 | |
83b24437 | 38 | # Variables we fill in automatically, or via prompting: |
58063245 RA |
39 | my (@to,@cc,@initial_cc,@bcclist, |
40 | $initial_reply_to,$initial_subject,@files,$from,$compose,$time); | |
83b24437 | 41 | |
78488b2c | 42 | # Behavior modification variables |
aca7ad76 EW |
43 | my ($chain_reply_to, $quiet, $suppress_from, $no_signed_off_cc) = (1, 0, 0, 0); |
44 | my $smtp_server; | |
78488b2c | 45 | |
9133261f | 46 | # Example reply to: |
83b24437 | 47 | #$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>'; |
83b24437 RA |
48 | |
49 | my $term = new Term::ReadLine 'git-send-email'; | |
50 | ||
51 | # Begin by accumulating all the variables (defined above), that we will end up | |
52 | # needing, first, from the command line: | |
53 | ||
54 | my $rc = GetOptions("from=s" => \$from, | |
55 | "in-reply-to=s" => \$initial_reply_to, | |
56 | "subject=s" => \$initial_subject, | |
57 | "to=s" => \@to, | |
da140f8b | 58 | "cc=s" => \@initial_cc, |
58063245 | 59 | "bcc=s" => \@bcclist, |
78488b2c | 60 | "chain-reply-to!" => \$chain_reply_to, |
3342d850 | 61 | "smtp-server=s" => \$smtp_server, |
1f038a0c | 62 | "compose" => \$compose, |
30d08b34 | 63 | "quiet" => \$quiet, |
a985d595 | 64 | "suppress-from" => \$suppress_from, |
8e69b31e | 65 | "no-signed-off-cc|no-signed-off-by-cc" => \$no_signed_off_cc, |
83b24437 RA |
66 | ); |
67 | ||
68 | # Now, let's fill any that aren't set in with defaults: | |
69 | ||
e415907d JH |
70 | sub gitvar { |
71 | my ($var) = @_; | |
72 | my $fh; | |
73 | my $pid = open($fh, '-|'); | |
74 | die "$!" unless defined $pid; | |
75 | if (!$pid) { | |
76 | exec('git-var', $var) or die "$!"; | |
77 | } | |
78 | my ($val) = <$fh>; | |
79 | close $fh or die "$!"; | |
80 | chomp($val); | |
81 | return $val; | |
82 | } | |
83b24437 | 83 | |
e415907d JH |
84 | sub gitvar_ident { |
85 | my ($name) = @_; | |
86 | my $val = gitvar($name); | |
87 | my @field = split(/\s+/, $val); | |
88 | return join(' ', @field[0...(@field-3)]); | |
83b24437 | 89 | } |
e415907d JH |
90 | |
91 | my ($author) = gitvar_ident('GIT_AUTHOR_IDENT'); | |
92 | my ($committer) = gitvar_ident('GIT_COMMITTER_IDENT'); | |
83b24437 | 93 | |
994d6c66 EW |
94 | my %aliases; |
95 | chomp(my @alias_files = `git-repo-config --get-all sendemail.aliasesfile`); | |
96 | chomp(my $aliasfiletype = `git-repo-config sendemail.aliasfiletype`); | |
97 | my %parse_alias = ( | |
98 | # multiline formats can be supported in the future | |
99 | mutt => sub { my $fh = shift; while (<$fh>) { | |
100 | if (/^alias\s+(\S+)\s+(.*)$/) { | |
101 | my ($alias, $addr) = ($1, $2); | |
102 | $addr =~ s/#.*$//; # mutt allows # comments | |
103 | # commas delimit multiple addresses | |
104 | $aliases{$alias} = [ split(/\s*,\s*/, $addr) ]; | |
105 | }}}, | |
106 | mailrc => sub { my $fh = shift; while (<$fh>) { | |
107 | if (/^alias\s+(\S+)\s+(.*)$/) { | |
108 | # spaces delimit multiple addresses | |
109 | $aliases{$1} = [ split(/\s+/, $2) ]; | |
110 | }}}, | |
111 | pine => sub { my $fh = shift; while (<$fh>) { | |
112 | if (/^(\S+)\s+(.*)$/) { | |
113 | $aliases{$1} = [ split(/\s*,\s*/, $2) ]; | |
114 | }}}, | |
115 | gnus => sub { my $fh = shift; while (<$fh>) { | |
116 | if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) { | |
117 | $aliases{$1} = [ $2 ]; | |
118 | }}} | |
119 | ); | |
120 | ||
121 | if (@alias_files && defined $parse_alias{$aliasfiletype}) { | |
122 | foreach my $file (@alias_files) { | |
123 | open my $fh, '<', $file or die "opening $file: $!\n"; | |
124 | $parse_alias{$aliasfiletype}->($fh); | |
125 | close $fh; | |
126 | } | |
127 | } | |
128 | ||
1f038a0c | 129 | my $prompting = 0; |
83b24437 RA |
130 | if (!defined $from) { |
131 | $from = $author || $committer; | |
8037d1a3 RA |
132 | do { |
133 | $_ = $term->readline("Who should the emails appear to be from? ", | |
134 | $from); | |
ca9a7d65 | 135 | } while (!defined $_); |
8037d1a3 | 136 | |
83b24437 RA |
137 | $from = $_; |
138 | print "Emails will be sent from: ", $from, "\n"; | |
1f038a0c | 139 | $prompting++; |
83b24437 RA |
140 | } |
141 | ||
142 | if (!@to) { | |
8037d1a3 | 143 | do { |
5825e5b2 | 144 | $_ = $term->readline("Who should the emails be sent to? ", |
8037d1a3 RA |
145 | ""); |
146 | } while (!defined $_); | |
83b24437 RA |
147 | my $to = $_; |
148 | push @to, split /,/, $to; | |
1f038a0c | 149 | $prompting++; |
83b24437 RA |
150 | } |
151 | ||
994d6c66 EW |
152 | sub expand_aliases { |
153 | my @cur = @_; | |
154 | my @last; | |
155 | do { | |
156 | @last = @cur; | |
157 | @cur = map { $aliases{$_} ? @{$aliases{$_}} : $_ } @last; | |
158 | } while (join(',',@cur) ne join(',',@last)); | |
159 | return @cur; | |
160 | } | |
161 | ||
162 | @to = expand_aliases(@to); | |
163 | @initial_cc = expand_aliases(@initial_cc); | |
58063245 | 164 | @bcclist = expand_aliases(@bcclist); |
994d6c66 | 165 | |
1f038a0c | 166 | if (!defined $initial_subject && $compose) { |
8037d1a3 | 167 | do { |
5825e5b2 | 168 | $_ = $term->readline("What subject should the emails start with? ", |
8037d1a3 RA |
169 | $initial_subject); |
170 | } while (!defined $_); | |
83b24437 | 171 | $initial_subject = $_; |
1f038a0c | 172 | $prompting++; |
83b24437 RA |
173 | } |
174 | ||
1f038a0c | 175 | if (!defined $initial_reply_to && $prompting) { |
8037d1a3 | 176 | do { |
1f038a0c | 177 | $_= $term->readline("Message-ID to be used as In-Reply-To for the first email? ", |
8037d1a3 RA |
178 | $initial_reply_to); |
179 | } while (!defined $_); | |
180 | ||
83b24437 | 181 | $initial_reply_to = $_; |
78488b2c | 182 | $initial_reply_to =~ s/(^\s+|\s+$)//g; |
83b24437 RA |
183 | } |
184 | ||
aca7ad76 EW |
185 | if (!$smtp_server) { |
186 | foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) { | |
187 | if (-x $_) { | |
188 | $smtp_server = $_; | |
189 | last; | |
190 | } | |
191 | } | |
192 | $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug* | |
3342d850 RA |
193 | } |
194 | ||
1f038a0c RA |
195 | if ($compose) { |
196 | # Note that this does not need to be secure, but we will make a small | |
197 | # effort to have it be unique | |
198 | open(C,">",$compose_filename) | |
199 | or die "Failed to open for writing $compose_filename: $!"; | |
d366c703 | 200 | print C "From $from # This line is ignored.\n"; |
1f038a0c RA |
201 | printf C "Subject: %s\n\n", $initial_subject; |
202 | printf C <<EOT; | |
203 | GIT: Please enter your email below. | |
204 | GIT: Lines beginning in "GIT: " will be removed. | |
205 | GIT: Consider including an overall diffstat or table of contents | |
206 | GIT: for the patch you are writing. | |
207 | ||
208 | EOT | |
209 | close(C); | |
210 | ||
211 | my $editor = $ENV{EDITOR}; | |
212 | $editor = 'vi' unless defined $editor; | |
213 | system($editor, $compose_filename); | |
214 | ||
215 | open(C2,">",$compose_filename . ".final") | |
216 | or die "Failed to open $compose_filename.final : " . $!; | |
217 | ||
218 | open(C,"<",$compose_filename) | |
219 | or die "Failed to open $compose_filename : " . $!; | |
220 | ||
221 | while(<C>) { | |
222 | next if m/^GIT: /; | |
223 | print C2 $_; | |
224 | } | |
225 | close(C); | |
226 | close(C2); | |
227 | ||
228 | do { | |
229 | $_ = $term->readline("Send this email? (y|n) "); | |
230 | } while (!defined $_); | |
231 | ||
232 | if (uc substr($_,0,1) ne 'Y') { | |
233 | cleanup_compose_files(); | |
234 | exit(0); | |
235 | } | |
236 | ||
237 | @files = ($compose_filename . ".final"); | |
238 | } | |
239 | ||
240 | ||
83b24437 RA |
241 | # Now that all the defaults are set, process the rest of the command line |
242 | # arguments and collect up the files that need to be processed. | |
243 | for my $f (@ARGV) { | |
244 | if (-d $f) { | |
245 | opendir(DH,$f) | |
246 | or die "Failed to opendir $f: $!"; | |
247 | ||
5825e5b2 | 248 | push @files, grep { -f $_ } map { +$f . "/" . $_ } |
8037d1a3 RA |
249 | sort readdir(DH); |
250 | ||
83b24437 RA |
251 | } elsif (-f $f) { |
252 | push @files, $f; | |
253 | ||
254 | } else { | |
255 | print STDERR "Skipping $f - not found.\n"; | |
256 | } | |
257 | } | |
258 | ||
259 | if (@files) { | |
2718435b RA |
260 | unless ($quiet) { |
261 | print $_,"\n" for (@files); | |
262 | } | |
83b24437 RA |
263 | } else { |
264 | print <<EOT; | |
215a7ad1 | 265 | git-send-email [options] <file | directory> [... file | directory ] |
83b24437 RA |
266 | Options: |
267 | --from Specify the "From:" line of the email to be sent. | |
1f038a0c | 268 | |
5825e5b2 | 269 | --to Specify the primary "To:" line of the email. |
1f038a0c | 270 | |
da140f8b RA |
271 | --cc Specify an initial "Cc:" list for the entire series |
272 | of emails. | |
273 | ||
58063245 RA |
274 | --bcc Specify a list of email addresses that should be Bcc: |
275 | on all the emails. | |
276 | ||
1f038a0c RA |
277 | --compose Use \$EDITOR to edit an introductory message for the |
278 | patch series. | |
279 | ||
83b24437 | 280 | --subject Specify the initial "Subject:" line. |
1f038a0c RA |
281 | Only necessary if --compose is also set. If --compose |
282 | is not set, this will be prompted for. | |
283 | ||
83b24437 | 284 | --in-reply-to Specify the first "In-Reply-To:" header line. |
1f038a0c RA |
285 | Only used if --compose is also set. If --compose is not |
286 | set, this will be prompted for. | |
287 | ||
e205735d | 288 | --chain-reply-to If set, the replies will all be to the previous |
5825e5b2 JH |
289 | email sent, rather than to the first email sent. |
290 | Defaults to on. | |
1f038a0c | 291 | |
a985d595 RA |
292 | --no-signed-off-cc Suppress the automatic addition of email addresses |
293 | that appear in a Signed-off-by: line, to the cc: list. | |
294 | Note: Using this option is not recommended. | |
295 | ||
3342d850 RA |
296 | --smtp-server If set, specifies the outgoing SMTP server to use. |
297 | Defaults to localhost. | |
83b24437 | 298 | |
a985d595 RA |
299 | --suppress-from Supress sending emails to yourself if your address |
300 | appears in a From: line. | |
301 | ||
2718435b RA |
302 | --quiet Make git-send-email less verbose. One line per email should be |
303 | all that is output. | |
304 | ||
83b24437 RA |
305 | Error: Please specify a file or a directory on the command line. |
306 | EOT | |
307 | exit(1); | |
308 | } | |
309 | ||
310 | # Variables we set as part of the loop over files | |
7ccf7927 | 311 | our ($message_id, $cc, %mail, $subject, $reply_to, $references, $message); |
83b24437 | 312 | |
567ffeb7 EW |
313 | sub extract_valid_address { |
314 | my $address = shift; | |
db3106b2 EW |
315 | |
316 | # check for a local address: | |
317 | return $address if ($address =~ /^([\w\-]+)$/); | |
318 | ||
567ffeb7 EW |
319 | if ($have_email_valid) { |
320 | return Email::Valid->address($address); | |
321 | } else { | |
322 | # less robust/correct than the monster regexp in Email::Valid, | |
323 | # but still does a 99% job, and one less dependency | |
8baf06a0 RA |
324 | my $cleaned_address; |
325 | if ($address =~ /([^\"<>\s]+@[^<>\s]+)/) { | |
326 | $cleaned_address = $1; | |
327 | } | |
328 | return $cleaned_address; | |
567ffeb7 EW |
329 | } |
330 | } | |
83b24437 RA |
331 | |
332 | # Usually don't need to change anything below here. | |
333 | ||
334 | # we make a "fake" message id by taking the current number | |
335 | # of seconds since the beginning of Unix time and tacking on | |
336 | # a random number to the end, in case we are called quicker than | |
337 | # 1 second since the last time we were called. | |
8037d1a3 RA |
338 | |
339 | # We'll setup a template for the message id, using the "from" address: | |
567ffeb7 | 340 | my $message_id_from = extract_valid_address($from); |
e205735d | 341 | my $message_id_template = "<%s-git-send-email-$message_id_from>"; |
8037d1a3 | 342 | |
83b24437 RA |
343 | sub make_message_id |
344 | { | |
72095d5c | 345 | my $date = time; |
83b24437 | 346 | my $pseudo_rand = int (rand(4200)); |
8037d1a3 RA |
347 | $message_id = sprintf $message_id_template, "$date$pseudo_rand"; |
348 | #print "new message id = $message_id\n"; # Was useful for debugging | |
83b24437 RA |
349 | } |
350 | ||
351 | ||
352 | ||
353 | $cc = ""; | |
a5370b16 | 354 | $time = time - scalar $#files; |
83b24437 RA |
355 | |
356 | sub send_message | |
357 | { | |
4bc87a28 EW |
358 | my @recipients = unique_email_list(@to); |
359 | my $to = join (",\n\t", @recipients); | |
58063245 | 360 | @recipients = unique_email_list(@recipients,@cc,@bcclist); |
a5370b16 | 361 | my $date = strftime('%a, %d %b %Y %H:%M:%S %z', localtime($time++)); |
e923effb ML |
362 | my $gitversion = '@@GIT_VERSION@@'; |
363 | if ($gitversion =~ m/..GIT_VERSION../) { | |
364 | $gitversion = `git --version`; | |
365 | chomp $gitversion; | |
366 | # keep only what's after the last space | |
367 | $gitversion =~ s/^.* //; | |
368 | } | |
4bc87a28 EW |
369 | |
370 | my $header = "From: $from | |
371 | To: $to | |
372 | Cc: $cc | |
373 | Subject: $subject | |
374 | Reply-To: $from | |
375 | Date: $date | |
376 | Message-Id: $message_id | |
e923effb | 377 | X-Mailer: git-send-email $gitversion |
4bc87a28 | 378 | "; |
7ccf7927 RA |
379 | if ($reply_to) { |
380 | ||
381 | $header .= "In-Reply-To: $reply_to\n"; | |
382 | $header .= "References: $references\n"; | |
383 | } | |
4bc87a28 | 384 | |
aca7ad76 EW |
385 | if ($smtp_server =~ m#^/#) { |
386 | my $pid = open my $sm, '|-'; | |
387 | defined $pid or die $!; | |
388 | if (!$pid) { | |
2186d566 JH |
389 | exec($smtp_server,'-i', |
390 | map { scalar extract_valid_address($_) } | |
391 | @recipients) or die $!; | |
aca7ad76 EW |
392 | } |
393 | print $sm "$header\n$message"; | |
394 | close $sm or die $?; | |
395 | } else { | |
87840620 | 396 | require Net::SMTP; |
aca7ad76 EW |
397 | $smtp ||= Net::SMTP->new( $smtp_server ); |
398 | $smtp->mail( $from ) or die $smtp->message; | |
399 | $smtp->to( @recipients ) or die $smtp->message; | |
400 | $smtp->data or die $smtp->message; | |
401 | $smtp->datasend("$header\n$message") or die $smtp->message; | |
402 | $smtp->dataend() or die $smtp->message; | |
403 | $smtp->ok or die "Failed to send $subject\n".$smtp->message; | |
404 | } | |
2718435b RA |
405 | if ($quiet) { |
406 | printf "Sent %s\n", $subject; | |
407 | } else { | |
aca7ad76 EW |
408 | print "OK. Log says:\nDate: $date\n"; |
409 | if ($smtp) { | |
410 | print "Server: $smtp_server\n"; | |
411 | } else { | |
412 | print "Sendmail: $smtp_server\n"; | |
413 | } | |
414 | print "From: $from\nSubject: $subject\nCc: $cc\nTo: $to\n\n"; | |
415 | if ($smtp) { | |
416 | print "Result: ", $smtp->code, ' ', | |
417 | ($smtp->message =~ /\n([^\n]+\n)$/s), "\n"; | |
418 | } else { | |
419 | print "Result: OK\n"; | |
420 | } | |
30d08b34 | 421 | } |
83b24437 RA |
422 | } |
423 | ||
83b24437 | 424 | $reply_to = $initial_reply_to; |
2186d566 | 425 | $references = $initial_reply_to || ''; |
83b24437 RA |
426 | make_message_id(); |
427 | $subject = $initial_subject; | |
428 | ||
429 | foreach my $t (@files) { | |
83b24437 RA |
430 | open(F,"<",$t) or die "can't open file $t"; |
431 | ||
8a8e6235 | 432 | my $author_not_sender = undef; |
da140f8b | 433 | @cc = @initial_cc; |
83b24437 RA |
434 | my $found_mbox = 0; |
435 | my $header_done = 0; | |
436 | $message = ""; | |
437 | while(<F>) { | |
438 | if (!$header_done) { | |
439 | $found_mbox = 1, next if (/^From /); | |
440 | chomp; | |
441 | ||
442 | if ($found_mbox) { | |
443 | if (/^Subject:\s+(.*)$/) { | |
444 | $subject = $1; | |
445 | ||
446 | } elsif (/^(Cc|From):\s+(.*)$/) { | |
8a8e6235 JH |
447 | if ($2 eq $from) { |
448 | next if ($suppress_from); | |
449 | } | |
450 | else { | |
451 | $author_not_sender = $2; | |
452 | } | |
83b24437 | 453 | printf("(mbox) Adding cc: %s from line '%s'\n", |
2718435b | 454 | $2, $_) unless $quiet; |
83b24437 RA |
455 | push @cc, $2; |
456 | } | |
457 | ||
458 | } else { | |
459 | # In the traditional | |
460 | # "send lots of email" format, | |
461 | # line 1 = cc | |
462 | # line 2 = subject | |
463 | # So let's support that, too. | |
464 | if (@cc == 0) { | |
465 | printf("(non-mbox) Adding cc: %s from line '%s'\n", | |
2718435b | 466 | $_, $_) unless $quiet; |
83b24437 RA |
467 | |
468 | push @cc, $_; | |
469 | ||
470 | } elsif (!defined $subject) { | |
471 | $subject = $_; | |
472 | } | |
473 | } | |
5825e5b2 | 474 | |
83b24437 RA |
475 | # A whitespace line will terminate the headers |
476 | if (m/^\s*$/) { | |
477 | $header_done = 1; | |
478 | } | |
479 | } else { | |
480 | $message .= $_; | |
a985d595 | 481 | if (/^Signed-off-by: (.*)$/i && !$no_signed_off_cc) { |
83b24437 RA |
482 | my $c = $1; |
483 | chomp $c; | |
484 | push @cc, $c; | |
485 | printf("(sob) Adding cc: %s from line '%s'\n", | |
2718435b | 486 | $c, $_) unless $quiet; |
83b24437 RA |
487 | } |
488 | } | |
489 | } | |
490 | close F; | |
8a8e6235 JH |
491 | if (defined $author_not_sender) { |
492 | $message = "From: $author_not_sender\n\n$message"; | |
493 | } | |
83b24437 | 494 | |
e205735d | 495 | $cc = join(", ", unique_email_list(@cc)); |
83b24437 RA |
496 | |
497 | send_message(); | |
498 | ||
499 | # set up for the next message | |
78488b2c RA |
500 | if ($chain_reply_to || length($reply_to) == 0) { |
501 | $reply_to = $message_id; | |
7ccf7927 RA |
502 | if (length $references > 0) { |
503 | $references .= " $message_id"; | |
504 | } else { | |
505 | $references = "$message_id"; | |
506 | } | |
78488b2c | 507 | } |
83b24437 | 508 | make_message_id(); |
83b24437 | 509 | } |
e205735d | 510 | |
1f038a0c RA |
511 | if ($compose) { |
512 | cleanup_compose_files(); | |
513 | } | |
514 | ||
515 | sub cleanup_compose_files() { | |
516 | unlink($compose_filename, $compose_filename . ".final"); | |
517 | ||
518 | } | |
519 | ||
4bc87a28 | 520 | $smtp->quit if $smtp; |
e205735d RA |
521 | |
522 | sub unique_email_list(@) { | |
523 | my %seen; | |
524 | my @emails; | |
525 | ||
526 | foreach my $entry (@_) { | |
db3106b2 EW |
527 | if (my $clean = extract_valid_address($entry)) { |
528 | $seen{$clean} ||= 0; | |
529 | next if $seen{$clean}++; | |
530 | push @emails, $entry; | |
531 | } else { | |
532 | print STDERR "W: unable to extract a valid address", | |
533 | " from: $entry\n"; | |
534 | } | |
e205735d RA |
535 | } |
536 | return @emails; | |
537 | } |