[vhffs-dev] [695] Impossible to create a mailing list with the address of an existing mail box /forward and vice versa.

[ Thread Index | Date Index | More vhffs.org/vhffs-dev Archives ]


Revision: 695
Author:   beuss
Date:     2007-07-08 17:57:18 +0000 (Sun, 08 Jul 2007)

Log Message:
-----------
Impossible to create a mailing list with the address of an existing mail box/forward and vice versa.

Modified Paths:
--------------
    trunk/vhffs-api/src/Vhffs/Services/Mail.pm
    trunk/vhffs-api/src/Vhffs/Services/Mailing.pm


Modified: trunk/vhffs-api/src/Vhffs/Services/Mail.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Services/Mail.pm	2007-07-08 17:34:37 UTC (rev 694)
+++ trunk/vhffs-api/src/Vhffs/Services/Mail.pm	2007-07-08 17:57:18 UTC (rev 695)
@@ -249,7 +249,8 @@
 	my $remote = shift;
 
     return -1 unless(defined($name) && defined($remote) && $name =~ /^[a-z0-9\_\-\.]+$/ && Vhffs::Functions::valid_mail( $remote ) );
-	return -2 if( ( defined ( $self->{'forward'}{$name} ) ) || ( defined( $self->{'boxes'}{$name} ) ) );
+	return -2 if( ( defined ( $self->{'forward'}{$name} ) ) || ( defined( $self->{'boxes'}{$name} ) ) ||
+        Vhffs::Services::Mailing::address_exists($self->get_main, $name, $self->{domain}) );
 
     my $sql = 'INSERT INTO vhffs_forward(domain, local_part, remote_name, password) VALUES(?, ?, ?, \'x\')';
     my $dbh = $self->{db};
@@ -281,7 +282,8 @@
 	my $password = shift;
 
     return -1 unless(defined($password) && defined($name) && $name =~ /^[a-z0-9\_\-\.]+$/ && $password =~ /^[a-zA-Z0-9\_\-]+$/);
-    return -2 if( ( defined ( $self->{'boxes'}{$name} ) ) || ( defined( $self->{'forward'}{$name} ) ) );
+    return -2 if( ( defined ( $self->{'boxes'}{$name} ) ) || ( defined( $self->{'forward'}{$name} ) ) 
+        || Vhffs::Services::Mailing::address_exists($self->get_main, $name, $self->{domain}) );
 
     $password = crypt_pwd( $password );
     my $domainhash = Vhffs::Functions::hash_mxdomain($self->{domain});
@@ -584,6 +586,29 @@
     return $objs;
 }
 
+=pod address_exists
 
+    print ("Adress $local_part\@$domain already exists as a box or forward\n")
+        if(Vhffs::Services::Mail::address_exists($vhffs, $local_part, $domain));
 
+Return true if a box or a forward C<$local_part>@C<$domain> already exists.
+It does B<not> check for mailing lists (use Vhffs::Services::Mailing::address_exists).
+
+=cut
+
+sub address_exists($$$) {
+    my ($vhffs, $local_part, $domain) = @_;
+
+    my $sql = <<EOQ;
+SELECT SUM(c) FROM ( 
+    (SELECT COUNT(*) AS c FROM vhffs_boxes b WHERE b.domain = ? AND b.local_part = ?)
+    UNION 
+    (SELECT COUNT(*) AS c FROM vhffs_forward f WHERE f.domain = ? AND f.local_part = ?))
+    AS counts
+EOQ
+    my $dbh = $vhffs->get_db_object();
+    my $res = $dbh->selectrow_array($sql, {}, $domain, $local_part, $domain, $local_part);
+    return (defined($res) && $res > 0);
+}
+
 1;

Modified: trunk/vhffs-api/src/Vhffs/Services/Mailing.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Services/Mailing.pm	2007-07-08 17:34:37 UTC (rev 694)
+++ trunk/vhffs-api/src/Vhffs/Services/Mailing.pm	2007-07-08 17:57:18 UTC (rev 695)
@@ -61,6 +61,8 @@
     die('Unable to create list') unless defined $ml;
 
 Creates a new mailing list in database and returns the corresponding fully functional object.
+Returns undef if an error occurs (box, forward or mailing list with the same address already
+exists, domain not found, ...).
 
 =cut
 sub create
@@ -70,6 +72,7 @@
     return undef unless(defined $user && defined $group);
     return undef unless($local =~ /^[a-z0-9\_\-]+$/);
     return undef unless(Vhffs::Functions::check_domain_name($domain));
+    return undef if(Vhffs::Services::Mail::address_exists($main, $local, $domain));
     $admin = $user->get_mail() unless(defined $admin);
 
     my $ml;
@@ -292,6 +295,24 @@
     return 1;
 }
 
+=head2 address_exists
+
+    print("A mailing list with the same address already exists\n")
+        if Vhffs::Mailing::address_exists($vhffs, $local_part, $domain);
+
+Return true if a mailing list C<$local_part>@C<$domain> already exists.
+
+=cut
+
+sub address_exists($$$) {
+    my ($vhffs, $local_part, $domain) = @_;
+
+    my $sql = 'SELECT COUNT(*) FROM vhffs_ml WHERE local_part = ? AND domain = ?';
+    my $dbh = $vhffs->get_db_object();
+    my $res = $dbh->selectrow_array($sql, {}, $local_part, $domain);
+    return (defined($res) && $res > 0);
+}
+
 sub del_sub_with_reply
 {
     use Digest::MD5;


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/