Cleanup git-send-email.perl:extract_valid_email
PATCH 1/1 Cleanup git-send-email.perl:extract_valid_email
- Fix the regular expressions for local addresses
- Fix the fallback regexp for non-local addresses, simplify the logic
--- git-send-email.perl | 9 +++------ 1 files changed, 3 insertions(+), 6 deletions(-)
git-send-email.perl +3 -6
@@ -314,18 +314,15 @@ sub extract_valid_address { my $address = shift; # check for a local address: - return $address if ($address =~ /^([\w\-]+)$/); + return $address if ($address =~ /^([\w\-.]+)$/); if ($have_email_valid) { return Email::Valid->address($address); } else { # less robust/correct than the monster regexp in Email::Valid, # but still does a 99% job, and one less dependency - my $cleaned_address; - if ($address =~ /([^\"<>\s]+@[^<>\s]+)/) { - $cleaned_address = $1; - } - return $cleaned_address; + $address =~ /([\w\-.]+@[\w\-.]+)/; + return $1; } }
Signature · 1 line
1.3.3.g86f7
Quoted 13 lines · Horst H. von Brand
- Fix the regular expressions for local addresses
- Fix the fallback regexp for non-local addresses, simplify the logicSigned-off-by Horst H. von Brand <[email protected]>--- git-send-email.perl | 9 +++------ 1 files changed, 3 insertions(+), 6 deletions(-)git-send-email.perl +0
-0
git-send-email.perl sub extract_valid_address { · line 317
Quoted 5 lines
git-send-email.perl +1
-1@@ -314,18 +314,15 @@ sub extract_valid_address { my $address = shift; # check for a local address: - return $address if ($address =~ /^([\w\-]+)$/); + return $address if ($address =~ /^([\w\-.]+)$/);
I keep forgetting this, '+' is a valid (and useful) setup, too.
git-send-email.perl sub extract_valid_address { · line 325
Quoted 13 lines
git-send-email.perl +2
-5if ($have_email_valid) { return Email::Valid->address($address); } else { # less robust/correct than the monster regexp in Email::Valid, # but still does a 99% job, and one less dependency - my $cleaned_address; - if ($address =~ /([^\"<>\s]+@[^<>\s]+)/) { - $cleaned_address = $1; - } - return $cleaned_address; + $address =~ /([\w\-.]+@[\w\-.]+)/; + return $1;
Actually, I'm retracting my earlier ack on this. This is way too restrictive. I'd rather allow an occasional invalid email address than to reject valid ones. I generally trust git users to know what they're doing when entering email addresses[1].
*, $, ^, +, = are all valid characters in the username portion (not sure about local accounts, though), and I'm sure there are more that I don't know about.
[1] - of course, without address book support in git-send-email, I think I would've left the 'k' out of Junio's email address by now. Of course there's also the issue of where I work and having several people using variations of rob/robert in their email address. I'm likely to make errors like these far more than entering addresses that email systems consider invalid, and I suspect I'm not the only one.
Signature · 1 line
Eric Wong
Quoted 16 lines · Eric Wong
Quoted 15 lines · Horst H. von Brand
- Fix the regular expressions for local addresses
- Fix the fallback regexp for non-local addresses, simplify the logicSigned-off-by Horst H. von Brand <[email protected]>--- git-send-email.perl | 9 +++------ 1 files changed, 3 insertions(+), 6 deletions(-)git-send-email.perl +0
-0@@ -314,18 +314,15 @@ sub extract_valid_address { my $address = shift;
Quoted 5 lines
git-send-email.perl sub extract_valid_address { · line 317
Quoted 3 lines
git-send-email.perl +1
-1# check for a local address: - return $address if ($address =~ /^([\w\-]+)$/); + return $address if ($address =~ /^([\w\-.]+)$/);I keep forgetting this, '+' is a valid (and useful) setup, too.
Oops...
Quoted 18 lines
git-send-email.perl sub extract_valid_address { · line 325
Quoted 12 lines
git-send-email.perl +2
-5if ($have_email_valid) { return Email::Valid->address($address); } else { # less robust/correct than the monster regexp in Email::Valid, # but still does a 99% job, and one less dependency - my $cleaned_address; - if ($address =~ /([^\"<>\s]+@[^<>\s]+)/) { - $cleaned_address = $1; - } - return $cleaned_address; + $address =~ /([\w\-.]+@[\w\-.]+)/; + return $1;Actually, I'm retracting my earlier ack on this. This is way too restrictive. I'd rather allow an occasional invalid email address than to reject valid ones. I generally trust git users to know what they're doing when entering email addresses[1].
Quoted 3 lines
*, $, ^, +, = are all valid characters in the username portion (not sure about local accounts, though), and I'm sure there are more that I don't know about.
As a general principle, I prefer to check what is legal instead of trying to filter out what isn't.
Signature · 4 lines
Dr. Horst H. von Brand User #22616 counter.li.org Departamento de Informatica Fono: +56 32 654431 Universidad Tecnica Federico Santa Maria +56 32 654239 Casilla 110-V, Valparaiso, Chile Fax: +56 32 797513
Quoted 13 lines · Horst von Brand
Quoted 5 lines
Review
Quoted 3 lines
(quoted diff) +1
-1# check for a local address: - return $address if ($address =~ /^([\w\-]+)$/); + return $address if ($address =~ /^([\w\-.]+)$/);I keep forgetting this, '+' is a valid (and useful) setup, too.
Oops...
Quoted 6 lines
Actually, I'm retracting my earlier ack on this. This is way too restrictive. I'd rather allow an occasional invalid email address than to reject valid ones. I generally trust git users to know what they're doing when entering email addresses[1].
Quoted 6 lines
Quoted 3 lines
*, $, ^, +, = are all valid characters in the username portion (not sure about local accounts, though), and I'm sure there are more that I don't know about.
As a general principle, I prefer to check what is legal instead of trying to filter out what isn't.
If we start doing addr-spec in RFC2822 (page 17) ourselves, we should rather be using Email::Valid. A permissive sanity check to catch obvious mistakes would be more appropriate here than being RFC police.
I think something like the attached, on top of your patch, would be appropriate for upcoming 1.4.0.
-- >8 --
send-email: be more lenient and just catch obvious mistakes.
This cleans up the pattern matching subroutine by introducing two variables to hold regexp to approximately match local-part and domain in the e-mail address. It is meant to catch obvious mistakes with a cheap check.
The patch also moves "scalar" to force Email::Valid->address() to work in !wantarray environment to extract_valid_address; earlier it was in the caller of the subroutine, which was way too error prone.
---
git-send-email.perl +6 -4
@@ -312,16 +312,18 @@ our ($message_id, $cc, %mail, $subject, sub extract_valid_address { my $address = shift; + my $local_part_regexp = '[^<>"\s@]+'; + my $domain_regexp = '[^.<>"\s@]+\.[^<>"\s@]+'; # check for a local address: - return $address if ($address =~ /^([\w\-.]+)$/); + return $address if ($address =~ /^($local_part_regexp)$/); if ($have_email_valid) { - return Email::Valid->address($address); + return scalar Email::Valid->address($address); } else { # less robust/correct than the monster regexp in Email::Valid, # but still does a 99% job, and one less dependency - $address =~ /([\w\-.]+@[\w\-.]+)/; + $address =~ /($local_part_regexp\@$domain_regexp)/; return $1; } }
@@ -384,7 +386,7 @@ X-Mailer: git-send-email $gitversion defined $pid or die $!; if (!$pid) { exec($smtp_server,'-i', - map { scalar extract_valid_address($_) } + map { extract_valid_address($_) } @recipients) or die $!; } print $sm "$header\n$message";
Quoted 19 lines · Junio C Hamano
Quoted 17 lines · Horst von Brand
Quoted 5 lines
Review
Quoted 3 lines
(quoted diff) +1
-1# check for a local address: - return $address if ($address =~ /^([\w\-]+)$/); + return $address if ($address =~ /^([\w\-.]+)$/);I keep forgetting this, '+' is a valid (and useful) setup, too.
Oops...
Quoted 9 lines
Actually, I'm retracting my earlier ack on this. This is way too restrictive. I'd rather allow an occasional invalid email address than to reject valid ones. I generally trust git users to know what they're doing when entering email addresses[1].
*, $, ^, +, = are all valid characters in the username portion (not sure about local accounts, though), and I'm sure there are more that I don't know about.
Quoted 7 lines
Quoted 2 lines
As a general principle, I prefer to check what is legal instead of trying to filter out what isn't.
If we start doing addr-spec in RFC2822 (page 17) ourselves, we should rather be using
Email::Valid. A permissive sanity check to catch obvious mistakes would be more appropriate here than being RFC police.
OK.
Quoted 11 lines
I think something like the attached, on top of your patch, would be appropriate for upcoming 1.4.0.
-- >8 --
send-email: be more lenient and just catch obvious mistakes.This cleans up the pattern matching subroutine by introducing two variables to hold regexp to approximately match local-part and domain in the e-mail address. It is meant to catch obvious mistakes with a cheap check.
Quoted 4 lines
The patch also moves "scalar" to force
Email::Valid->address()to work in !wantarray environment toextract_valid_address; earlier it was in the caller of the subroutine, which was way too error prone.
Right.
git-send-email.perl our ($message_id, $cc, %mail, $subject, · line 316
Quoted 11 lines
---
git-send-email.perl +2
-0@@ -312,16 +312,18 @@ our ($message_id, $cc, %mail, $subject, sub extract_valid_address { my $address = shift; + my $local_part_regexp = '[^<>"\s@]+'; + my $domain_regexp = '[^.<>"\s@]+\.[^<>"\s@]+';
This forces a '.' in the domain, while vonbrand@localhost is perfectly reasonable. Plus it doesn't disallow adyacent '.'s. What about:
my $domain_regexp = '[^.<>"\s@]+(\.[^<>"\s@]+)*';
(but this is probably nitpicking...)
Signature · 4 lines
Dr. Horst H. von Brand User #22616 counter.li.org Departamento de Informatica Fono: +56 32 654431 Universidad Tecnica Federico Santa Maria +56 32 654239 Casilla 110-V, Valparaiso, Chile Fax: +56 32 797513
Quoted 14 lines · Horst von Brand
git-send-email.perl our ($message_id, $cc, %mail, $subject, · line 316
Quoted 10 lines
git-send-email.perl +2
-0@@ -312,16 +312,18 @@ our ($message_id, $cc, %mail, $subject, sub extract_valid_address { my $address = shift; + my $local_part_regexp = '[^<>"\s@]+'; + my $domain_regexp = '[^.<>"\s@]+\.[^<>"\s@]+';This forces a '.' in the domain, while vonbrand@localhost is perfectly reasonable. Plus it doesn't disallow adyacent '.'s. What about:
Quoted 3 lines
my $domain_regexp = '[^.<>"\s@]+(\.[^<>"\s@]+)*';(but this is probably nitpicking...)
I do not have preference either way about allowing an address like tld-administrator@net myself, but Email::Valid->address does not seem to allow it, and I just copied that behaviour for consistency between two alternative implementations.
I think you meant to say:
Quoted 1 line
my $domain_regexp = '[^.<>"\s@]+(\.[^.<>"\s@]+)*';
(i.e. exclude dot from the latter character class), but I am inclined to do this instead:
my $domain_regexp = '[^.<>"\s@]+(?:\.[^.<>"\s@]+)+';
(i.e. still require at least two levels).
Quoted 16 lines · Junio C Hamano
Quoted 14 lines · Horst von Brand
git-send-email.perl our ($message_id, $cc, %mail, $subject, · line 316
Quoted 10 lines
git-send-email.perl +2
-0@@ -312,16 +312,18 @@ our ($message_id, $cc, %mail, $subject, sub extract_valid_address { my $address = shift; + my $local_part_regexp = '[^<>"\s@]+'; + my $domain_regexp = '[^.<>"\s@]+\.[^<>"\s@]+';This forces a '.' in the domain, while vonbrand@localhost is perfectly reasonable. Plus it doesn't disallow adyacent '.'s. What about:
Quoted 8 lines
Quoted 3 lines
my $domain_regexp = '[^.<>"\s@]+(\.[^<>"\s@]+)*';(but this is probably nitpicking...)
I do not have preference either way about allowing an address like tld-administrator@net myself, but
Email::Valid->addressdoes not seem to allow it, and I just copied that behaviour for consistency between two alternative implementations.
Reasonable.
Quoted 5 lines
I think you meant to say:
Quoted 1 line
my $domain_regexp = '[^.<>"\s@]+(\.[^.<>"\s@]+)*';(i.e. exclude dot from the latter character class),
Right, my bad.
Quoted 6 lines
but I am
inclined to do this instead:my $domain_regexp = '[^.<>"\s@]+(?:\.[^.<>"\s@]+)+';(i.e. still require at least two levels).
OK, but be careful as this (?:...) is an extended regexp (needs /x on match). I'd just leave it plain (the performance impact shouldn't be noticeable). I don't see any use except for $1, so the extra parenthesis should be safe.
Signature · 4 lines
Dr. Horst H. von Brand User #22616 counter.li.org Departamento de Informatica Fono: +56 32 654431 Universidad Tecnica Federico Santa Maria +56 32 654239 Casilla 110-V, Valparaiso, Chile Fax: +56 32 797513
Quoted 10 lines · Horst von Brand
Quoted 6 lines · Junio C Hamano
but I am
inclined to do this instead:my $domain_regexp = '[^.<>"\s@]+(?:\.[^.<>"\s@]+)+';(i.e. still require at least two levels).
OK, but be careful as this (?:...) is an extended regexp (needs /x on match).
Are you sure about /x?
Quoted 5 lines · Junio C Hamano
Quoted 4 lines · Horst von Brand
Quoted 3 lines · Junio C Hamano
but I am
inclined to do this instead:
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
my $domain_regexp = '[^.<>"\s@]+(?:\.[^.<>"\s@]+)+';(i.e. still require at least two levels).
OK, but be careful as this (?:...) is an extended regexp (needs /x on match).
Are you sure about /x?
The manual (perlop(1)) says you need /x to match extended regexps, and (?...) is the marker for such (perlre(1)). But my perl here (5.5.8-6 on Fedora rawhide) doesn't care...
Signature · 4 lines
Dr. Horst H. von Brand User #22616 counter.li.org Departamento de Informatica Fono: +56 32 654431 Universidad Tecnica Federico Santa Maria +56 32 654239 Casilla 110-V, Valparaiso, Chile Fax: +56 32 797513
Quoted 7 lines · Horst von Brand
Quoted 4 lines
Quoted 2 lines
OK, but be careful as this (?:...) is an extended regexp (needs /x on match).
Are you sure about /x?
The manual (perlop(1)) says you need /x to match extended regexps, and (?...) is the marker for such (perlre(1)).
I always had the impression that eXtended in the context to talk about /x was about ignoring whitespaces and forcing people to write \s (or perhaps \040) when they mean a whitespace and had nothing to do with (?...) stuff. Let me look up the fine manual.
Quoted 6 lines · Junio C Hamano
Quoted 5 lines · Horst von Brand
Quoted 4 lines
Quoted 2 lines
OK, but be careful as this (?:...) is an extended regexp (needs /x on match).
Are you sure about /x?
Quoted 8 lines
Quoted 2 lines
The manual (perlop(1)) says you need /x to match extended regexps, and (?...) is the marker for such (perlre(1)).
I always had the impression that eXtended in the context to talk about /x was about ignoring whitespaces and forcing people to write \s (or perhaps \040) when they mean a whitespace and had nothing to do with (?...) stuff. Let me look up the fine manual.
You might be right... and it even sounds sensible; but both (?...) stuff and the ignoring of space is described as extended here.
Note that \s is a space character (' ', '\t', ...), which is not the same as \040 (and that one assumes ASCII...).
Signature · 4 lines
Dr. Horst H. von Brand User #22616 counter.li.org Departamento de Informatica Fono: +56 32 654431 Universidad Tecnica Federico Santa Maria +56 32 654239 Casilla 110-V, Valparaiso, Chile Fax: +56 32 797513