send-email: lazy-load Email::Valid and make it optional
It's not installed on enough machines, and is overkill most of the time. We'll fallback to a very basic regexp just in case, but nothing like the monster regexp Email::Valid has to offer :) Small cleanup from Merlyn. Signed-off-by: Eric Wong <normalperson@yhbt.net> Signed-off-by: Junio C Hamano <junkio@cox.net>
Eric Wong committed
Mar 25, 2006 at 16:47 UTC
567ffeb7722eefab3991cb894c96548b92b57cc2
1 file changed
+13
-3
git-send-email.perl
+13
-3
@@ -22,12 +22,12 @@ use Term::ReadLine;
22
use Getopt::Long;
23
use Data::Dumper;
24
use Net::SMTP;
25
-use Email::Valid;
25
26
# most mail servers generate the Date: header, but not all...
27
$ENV{LC_ALL} = 'C';
28
use POSIX qw/strftime/;
29
30
+my $have_email_valid = eval { require Email::Valid; 1 };
31
my $smtp;
32
33
sub unique_email_list(@);
@@ -250,6 +250,16 @@ EOT
250
# Variables we set as part of the loop over files
251
our ($message_id, $cc, %mail, $subject, $reply_to, $message);
252
253
+sub extract_valid_address {
254
+ my $address = shift;
255
+ if ($have_email_valid) {
256
+ return Email::Valid->address($address);
257
+ } else {
258
+ # less robust/correct than the monster regexp in Email::Valid,
259
+ # but still does a 99% job, and one less dependency
260
+ return ($address =~ /([^\"<>\s]+@[^<>\s]+)/);
261
+ }
262
+}
263
264
# Usually don't need to change anything below here.
265
@@ -259,7 +269,7 @@ our ($message_id, $cc, %mail, $subject, $reply_to, $message);
269
# 1 second since the last time we were called.
270
271
# We'll setup a template for the message id, using the "from" address:
262
-my $message_id_from = Email::Valid->address($from);
272
+my $message_id_from = extract_valid_address($from);
273
my $message_id_template = "<%s-git-send-email-$message_id_from>";
274
275
sub make_message_id
@@ -413,7 +423,7 @@ sub unique_email_list(@) {
423
my @emails;
424
425
foreach my $entry (@_) {
416
- my $clean = Email::Valid->address($entry);
426
+ my $clean = extract_valid_address($entry);
427
next if $seen{$clean}++;
428
push @emails, $entry;
429
}