[pve-devel] [PATCH common 1/2] move email regex from JSONSchema to Tools

Stoiko Ivanov s.ivanov at proxmox.com
Tue Sep 1 11:18:03 CEST 2020


Move the Regular expression used for matching e-mail addresses from
the verify sub in PVE::JSONSchema to PVE::Tools, so that it can be reused
in PVE::Tools::sendmail. This ensures that both uses have the same
definition of valid email addresses.

Signed-off-by: Stoiko Ivanov <s.ivanov at proxmox.com>
---
 src/PVE/JSONSchema.pm | 4 ++--
 src/PVE/Tools.pm      | 3 +++
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/src/PVE/JSONSchema.pm b/src/PVE/JSONSchema.pm
index 2ceb1bd..b27ffa7 100644
--- a/src/PVE/JSONSchema.pm
+++ b/src/PVE/JSONSchema.pm
@@ -7,7 +7,7 @@ use Getopt::Long;
 use Encode::Locale;
 use Encode;
 use Devel::Cycle -quiet; # todo: remove?
-use PVE::Tools qw(split_list $IPV6RE $IPV4RE);
+use PVE::Tools qw(split_list $IPV6RE $IPV4RE $EMAILRE);
 use PVE::Exception qw(raise);
 use HTTP::Status qw(:constants);
 use Net::IP qw(:PROC);
@@ -469,7 +469,7 @@ register_format('email', \&pve_verify_email);
 sub pve_verify_email {
     my ($email, $noerr) = @_;
 
-    if ($email !~ /^[\w\+\-\~]+(\.[\w\+\-\~]+)*@[a-zA-Z0-9\-]+(\.[a-zA-Z0-9\-]+)*$/) {
+    if ($email !~ /^${EMAILRE}$/) {
 	   return undef if $noerr;
 	   die "value does not look like a valid email address\n";
     }
diff --git a/src/PVE/Tools.pm b/src/PVE/Tools.pm
index f9270d9..e849bdf 100644
--- a/src/PVE/Tools.pm
+++ b/src/PVE/Tools.pm
@@ -33,6 +33,7 @@ no warnings 'portable'; # Support for 64-bit ints required
 our @EXPORT_OK = qw(
 $IPV6RE
 $IPV4RE
+$EMAILRE
 lock_file
 lock_file_full
 run_command
@@ -84,6 +85,8 @@ our $IPV6RE = "(?:" .
 
 our $IPRE = "(?:$IPV4RE|$IPV6RE)";
 
+our $EMAILRE = qr{[\w\+\-\~]+(\.[\w\+\-\~]+)*@[a-zA-Z0-9\-]+(\.[a-zA-Z0-9\-]+)*};
+
 use constant {CLONE_NEWNS   => 0x00020000,
               CLONE_NEWUTS  => 0x04000000,
               CLONE_NEWIPC  => 0x08000000,
-- 
2.20.1






More information about the pve-devel mailing list