[vhffs-dev] [458] Refactoring on Vhffs::Services::Mail.

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


Revision: 458
Author:   beuss
Date:     2007-02-16 15:39:56 +0000 (Fri, 16 Feb 2007)

Log Message:
-----------
Refactoring on Vhffs::Services::Mail. Added some tests.

Modified Paths:
--------------
    branches/vhffs_4.1/vhffs-api/src/Vhffs/Functions.pm
    branches/vhffs_4.1/vhffs-api/src/Vhffs/Services/Mail.pm

Added Paths:
-----------
    branches/vhffs_4.1/vhffs-tests/src/Services/Mail.pl


Modified: branches/vhffs_4.1/vhffs-api/src/Vhffs/Functions.pm
===================================================================
--- branches/vhffs_4.1/vhffs-api/src/Vhffs/Functions.pm	2007-02-16 15:16:38 UTC (rev 457)
+++ branches/vhffs_4.1/vhffs-api/src/Vhffs/Functions.pm	2007-02-16 15:39:56 UTC (rev 458)
@@ -454,7 +454,7 @@
 
 sub check_domain_name($) {
     my $domain_name = shift;
-    return (defined $domain_name && $domain_name =~ /^(?:[A-Za-z0-9\-]{1,63}[.])+([A-Za-z0-9]{2,10})$/);
+    return (defined $domain_name && length($domain_name) >= 5 && $domain_name =~ /^(?:[a-z0-9\-]{1,63}[.])+([a-z0-9]{2,10})$/);
 }
 
 sub rotate_log

Modified: branches/vhffs_4.1/vhffs-api/src/Vhffs/Services/Mail.pm
===================================================================
--- branches/vhffs_4.1/vhffs-api/src/Vhffs/Services/Mail.pm	2007-02-16 15:16:38 UTC (rev 457)
+++ branches/vhffs_4.1/vhffs-api/src/Vhffs/Services/Mail.pm	2007-02-16 15:39:56 UTC (rev 458)
@@ -106,10 +106,27 @@
     return 1;
 }
 
+sub create {
+    return old_create(@_) if($_[0]->isa('Vhffs::Services::Mail'));
+    my ($main, $domain, $description, $user, $group) = @_;
+    return undef unless(defined($user) && defined($group));
+    return undef unless(Vhffs::Functions::check_domain_name($domain));
 
+    my $parent = Vhffs::Object::create($main, $user->get_uid, $description);
 
+    return undef unless(defined $parent);
+
+    my $sql = 'INSERT INTO vhffs_mxdomain(domain, unix_user, boxes_path, max_popbox, catchall, owner_uid, owner_gid, object_id) VALUES(?, \'exim\', ?, 0, \'\', ?, ?, ?)';
+    my $domain_hash = Vhffs::Functions::hash_mxdomain($domain);
+    my $dbh = $main->get_db_object();
+    my $sth = $dbh->prepare($sql);
+    return undef unless($sth->execute($domain, $domain_hash, $user->get_uid(), $group->get_gid, $parent->get_oid));
+
+    return get_by_mxdomain($main, $domain);
+}
+
 # Create the object in the database
-sub create
+sub old_create
 {
 	my $self = shift;
 
@@ -176,6 +193,7 @@
     #                      - $ref->{'boxes'}{$local_part}{'mbox_name'}
     #                      - $ref->{'boxes'}{$local_part}{'password'}
     $request = $self->{'db'}->prepare("SELECT * FROM vhffs_boxes where domain='".$self->{'domain'}."'") or return -1;
+    print "SELECT * FROM vhffs_boxes where domain='".$self->{'domain'}."'";
     $request->execute() or return -3;
     $result = $request->fetchall_hashref('local_part');	
 
@@ -187,7 +205,6 @@
 			$self->{'boxes'}{$name}{$_} = $result->{$name}{$_};
 		}
 		$self->{'boxes'}{$name}{'password'} = $self->{'boxes'}{$name}{'password_hash'};
-		$self->{'boxes'}{$name}{'changed'} = 0;
 		$self->{'boxes'}{$name}{'nospam'} = 1 if( $self->{'boxes'}{$name}{'nospam'} eq 't' );
 		$self->{'boxes'}{$name}{'novirus'} = 1 if( $self->{'boxes'}{$name}{'novirus'} eq 't' );
 
@@ -216,7 +233,6 @@
 		{
 			$self->{'forward'}{$name}{$_} = $result->{$name}{$_};
 		}
-		$self->{'forward'}{$name}{'changed'} = 0;
 	}
 	######### End of forward fetching ######
 	
@@ -239,68 +255,6 @@
 	$request = $self->{'db'}->prepare( $query );
 	$request->execute;
 
-	#COMMIT THE BOX PART
-	foreach $name ( keys %{$self->{'boxes'}} )
-	{
-		next if( ( defined $self->{'boxes'}{$name}{'changed'} ) && ( $self->{'boxes'}{$name}{'changed'} == 0 ) );
-		
-    	$request = $self->{'db'}->prepare("SELECT * FROM vhffs_boxes WHERE domain='" . $self->{'domain'} . "' AND local_part='".$name."'");
-	    $rows = $request->execute;
-
-		#If boxes does not have nospam or novirus attribute
-		#so, we set t to 0
-		# This will create the attribute and initialize it
-		$self->{'boxes'}{$name}{'novirus'} = 0 if( ! defined( $self->{'boxes'}{$name}{'novirus'} ));
-		$self->{'boxes'}{$name}{'nospam'} = 0 if( ! defined( $self->{'boxes'}{$name}{'nospam'} ));
-
-		if( $rows == 0 ) #PopBox NOT created, we must create it
-		{
-			my $domainhash = Vhffs::Functions::hash_mxdomain( $self->{'domain'} );
-			my $userhash = Vhffs::Functions::hash_popuser( $name );
-			my $passwordhash = "";
-
-			$query = "INSERT INTO vhffs_boxes(domain,local_part,domain_hash,password_hash,mbox_name,password,nospam,novirus) VALUES('". $self->{'domain'} ."' , '". $name ."' , '". $domainhash ."' , '".$self->{'boxes'}{$name}{'password'}."' , '".$userhash."' , '' ,'".$self->{'boxes'}{$name}{'nospam'}."' , '".$self->{'boxes'}{$name}{'novirus'}."')";
-			$request = $self->{'db'}->prepare( $query );
-			$request->execute;
-		}
-		else	#Popbox already created
-		{
-
-			$query = "UPDATE vhffs_boxes SET nospam='".$self->{'boxes'}{$name}{'nospam'}."', novirus='".$self->{'boxes'}{$name}{'novirus'}."', password_hash='".$self->{'boxes'}{$name}{'password'}."' WHERE local_part='".$self->{'boxes'}{$name}{'local_part'}."' AND domain='".$self->{'domain'}."'";
-			$request = $self->{'db'}->prepare( $query );
-			$request->execute;
-		}
-	}	
-
-
-	#COMMIT THE FORWARD PART
-	foreach $name ( keys %{$self->{'forward'}} )
-	{
-		next if( ( defined $self->{'forward'}{$name}{'changed'} ) && ( $self->{'forward'}{$name}{'changed'} == 0 ) );
-#		next if( ! defined $self->{'forward'}{$name}{'remote'} );
-    	$request = $self->{'db'}->prepare("SELECT * FROM vhffs_forward WHERE domain='" . $self->{'domain'} . "' AND local_part='".$name."'");
-	    $rows = $request->execute;
-
-		if( $rows == 0 ) #PopBox NOT created, we must create it
-		{
-			$password = "";
-#			$query = "INSERT INTO vhffs_forward VALUES('". $self->{'domain'} ."' , '". $name ."' , '". $self->{'forward'}{$name}{'remote_name'} ."' , '".$self->{'forward'}{$name}{'password'}."')";
-			$query = "INSERT INTO vhffs_forward VALUES('". $self->{'domain'} ."' , '". $name ."' , '". $self->{'forward'}{$name}{'remote_name'} ."' , '4pr1lsUx')";
-			$request = $self->{'db'}->prepare( $query );
-			$request->execute;
-		}
-		else	#Popbox already created
-		{
-			$query = "UPDATE vhffs_forward SET remote_name='".$self->{'forward'}{$name}{'remote_name'}."' WHERE local_part='".$self->{'forward'}{$name}{'local_part'}."' AND domain='".$self->{'domain'}."'";
-			$request = $self->{'db'}->prepare( $query );
-			$request->execute;
-		}
-	}	
-
-
-
-
-
 	return -3 if( $self->SUPER::commit < 0 );
 
 	return 1;
@@ -312,9 +266,14 @@
 	my $local_part = shift;
 	my $destination = shift;
 
+    return -1 unless( defined($self->{foward}{$local_part}) && defined($local_part) && defined($destination) && Vhffs::Functions::valid_mail( $destination ) );
 
+    my $sql = 'UPDATE vhffs_forward SET remote_name = ? WHERE local_part = ? AND domain = ?';
+    my $dbh = $self->{db};
+    my $sth = $dbh->prepare($sql);
+    $sth->execute($destination, $local_part, $self->{domain}) or return -1;
+
 	$self->{'forward'}{$local_part}{'remote_name'} = $destination;
-	$self->{'forward'}{$local_part}{'changed'} = 1;
 
 	return 1;
 }
@@ -325,12 +284,17 @@
 	my $local_part = shift;
 	my $password = shift;
 
-	return -1 if( ! defined $self->{'boxes'}{$local_part} );
-	return -1 if( ! ( $password =~ /^[a-zA-Z0-9\_\-]+$/ ) );
+	return -1 unless( defined( $self->{'boxes'}{$local_part} ) && ( $password =~ /^[a-zA-Z0-9\_\-]+$/ ) );
 
-	$self->{'boxes'}{$local_part}{'password'} = crypt_pwd( $password );
-	$self->{'boxes'}{$local_part}{'changed'} = 1;
+    $password = crypt_pwd( $password );
 
+	$self->{'boxes'}{$local_part}{'password'} = $password;
+
+    my $sql = 'UPDATE vhffs_boxes SET password = ? WHERE local_part = ? AND domain = ?';
+    my $dbh = $self->{db};
+    my $sth = $dbh->prepare($sql);
+    $sth->execute($password, $local_part, $self->{domain}) or return -1;
+
 	return 1;
 }
 
@@ -344,13 +308,19 @@
 
 	return -1 if( ! defined $self->{'boxes'}{$local_part} );
 
-	if( ! defined( $self->{'boxes'}{$local_part}{'nospam'} ) )
-	{
-		$self->{'boxes'}{$local_part}{'nospam'} = 0;
-	}
+    my $nospam = $self->{boxes}{$local_part}{nospam};
+    if( defined( $nospam ) ) {
+        $nospam = ( $nospam + 1 ) % 2;
+    } else {
+        $nospam = 1;
+    }
+    $self->{boxes}{$local_part}{nospam} = $nospam;
 
-	$self->{'boxes'}{$local_part}{'nospam'} = ( ( $self->{'boxes'}{$local_part}{'nospam'} + 1 ) % 2 );
-	$self->{'boxes'}{$local_part}{'changed'} = 1;
+    my $sql = 'UPDATE vhffs_boxes SET nospam = ? WHERE domain = ? AND local_part = ?';
+    my $dbh = $self->{db};
+    my $sth = $dbh->prepare($sql);
+    $sth->execute($nospam, $self->{domain}, $local_part) or return -1;
+
 	return 1;
 }
 
@@ -363,13 +333,20 @@
 
 	return -1 if( ! defined $self->{'boxes'}{$local_part} );
 
-	if( ! defined( $self->{'boxes'}{$local_part}{'novirus'} ) )
-	{
-		$self->{'boxes'}{$local_part}{'novirus'} = 0;
-	}
+    my $novirus = $self->{boxes}{$local_part}{novirus};
+    if(defined $novirus) {
+        $novirus = ($novirus + 1) % 2;
+    } else {
+        $novirus = 1;
+    }
 
-	$self->{'boxes'}{$local_part}{'novirus'} = ( ( $self->{'boxes'}{$local_part}{'novirus'} + 1 ) % 2 );
-	$self->{'boxes'}{$local_part}{'changed'} = 1;
+	$self->{'boxes'}{$local_part}{'novirus'} = $novirus;
+
+    my $sql = 'UPDATE vhffs_boxes SET novirus = ? WHERE domain = ? AND local_part = ?';
+    my $dbh = $self->{db};
+    my $sth = $dbh->prepare($sql);
+    $sth->execute($novirus, $self->{domain}, $local_part) or return -1;
+
 	return 1;
 }
 
@@ -381,16 +358,18 @@
 	my $name = shift;
 	my $remote = shift;
 
-	return -1 if( ( ! defined $name ) || ( ! defined $remote ) );
-	return -1 if( ! ( $name =~ /^[a-z0-9\_\-\.]+$/ ) );
-	return -1 if( Vhffs::Functions::valid_mail( $remote ) != 1 );
-
+    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} ) ) );
 
+    my $sql = 'INSERT INTO vhffs_forward(domain, local_part, remote_name, password) VALUES(?, ?, ?, \'4pr1lsUx\')';
+    my $dbh = $self->{db};
+    my $sth = $dbh->prepare($sql);
+    $sth->execute($self->{domain}, $name, $remote) or return -1;
+
 	$self->{'forward'}{$name}{'local_part'} = $name;
 	$self->{'forward'}{$name}{'domain'} = $self->{'domain'};
 	$self->{'forward'}{$name}{'remote_name'} = $remote;
-    #self->{'forward'}{$name}{'changed'} = 1;
+    $self->{forward}{$name}{password} = '4pr1lsUx';
 	
 	return 1;
 }
@@ -402,15 +381,26 @@
 	my $name = shift;
 	my $password = shift;
 
-	return -1 if( ( ! defined $password ) || ( ! defined $name ) );
-	return -1 if( ! ( $name =~ /^[a-z0-9\_\-]+$/ ) );
-	return -1 if( ! ( $password =~ /^[a-zA-Z0-9\_\-]+$/ ) );
-	return -2 if( ( defined ( $self->{'boxes'}{$name} ) ) || ( defined( $self->{'forward'}{$name} ) ) );
+    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} ) ) );
 
+    $password = crypt_pwd( $password );
+    my $domainhash = Vhffs::Functions::hash_mxdomain($self->{domain});
+    my $userhash = Vhffs::Functions::hash_popuser( $name );
 	$self->{'boxes'}{$name}{'local_part'} = $name;
-	$self->{'boxes'}{$name}{'password'} = crypt_pwd( $password );
+	$self->{'boxes'}{$name}{'password'} = $password;
 	$self->{'boxes'}{$name}{'domain'} = $self->{'domain'};
+    $self->{boxes}{$name}{domain_hash} = $domainhash;
+    $self->{boxes}{$name}{password_hash} = $password;
+    $self->{boxes}{$name}{novirus} = 0;
+    $self->{boxes}{$name}{nospam} = 0;
+    $self->{boxes}{$name}{mbox_name} = $userhash;
 	
+    my $sql = 'INSERT INTO vhffs_boxes(domain, local_part, domain_hash, password_hash, mbox_name, password, nospam, novirus) VALUES(?, ?, ?, ?, ?, \'\', FALSE, FALSE)';
+    my $dbh = $self->{db};
+    my $sth = $dbh->prepare($sql);
+    $sth->execute($self->{domain}, $name, $domainhash, $password,  $userhash) or return -3;
+
 	return 1;
 }
 
@@ -419,8 +409,7 @@
 	my $self = shift;
 	my $name = shift;
 
-	return -1 if( ! defined $name );
-	return -1 if( ! ( $name =~ /^[a-zA-Z0-9\_\-]+$/ ) );
+	return -1 unless( defined $name && ( $name =~ /^[a-zA-Z0-9\_\-]+$/ ) );
 
 	undef $self->{boxes}{$name};
 
@@ -436,10 +425,8 @@
 	my $self = shift;
 	my $name = shift;
 
-	return -1 if( ! defined $name );
-
+	return -1 unless( defined $name  && ( $name =~ /^[a-zA-Z0-9\_\-]+$/ ) );
 	undef $self->{forward}{$name};
-	return -1 if( ! ( $name =~ /^[a-zA-Z0-9\_\-]+$/ ) );
 
 	my $query = "DELETE FROM vhffs_forward WHERE local_part='".$name."' AND domain='".$self->{'domain'}."'";
 	my $request = $self->{'db'}->prepare( $query );
@@ -484,11 +471,8 @@
 	return 0;
 }
 
+#Following function is needed by courier to fetch accounts
 
-
-
-#Following functions is needed by courier to fetch accounts
-
 sub crypt_pwd
 {
 	my $clear = shift;
@@ -611,33 +595,80 @@
 }
 
 
+sub get_by_mxdomain {
+    my ($main, $domain) = @_;
+
+    my $sql = 'SELECT m.mxdomain_id, m.owner_gid, m.domain, m.unix_user, m.boxes_path, m.max_popbox, m.catchall, o.object_id, o.owner_uid, o.date_creation, o.state, o.description FROM vhffs_mxdomain m INNER JOIN vhffs_object o ON o.object_id = m.object_id WHERE domain = ?';
+    my $dbh = $main->get_db_object();
+    my @params = $dbh->selectrow_array($sql, undef, $domain);
+
+    # We now need the boxes and forwards
+    $sql = 'SELECT domain, local_part, domain_hash, password_hash, mbox_name, password_hash AS password, nospam::integer, novirus::integer FROM vhffs_boxes WHERE domain = ?';
+    my $sth = $dbh->prepare($sql);
+    $sth->execute($domain);
+    my $boxes = $sth->fetchall_hashref('local_part');
+
+    $sql = 'SELECT domain, local_part, remote_name, password FROM vhffs_forward WHERE domain = ?';
+    $sth = $dbh->prepare($sql);
+    $sth->execute($domain);
+    my $forwards = $sth->fetchall_hashref('local_part');
+
+
+    return _new Vhffs::Services::Mail($main, @params, $boxes, $forwards);
+
+}
+
+sub _new {
+    my ($class, $main, $mxdomain_id, $owner_gid, $domain, $unix_user, $boxes_path, $max_popbox, $catchall, $oid, $owner_uid, $date_creation, $state, $description, $boxes, $forwards) = @_;
+    my $self = $class->SUPER::_new($main, $oid, $owner_uid, $date_creation, $state, $description);
+    return undef unless defined($self);
+    
+    $self->{mxdomain_id} = $mxdomain_id;
+    $self->{owner_gid} = $owner_gid;
+    $self->{domain} = $domain;
+    $self->{unix_user} = $unix_user;
+    $self->{boxes_path} = $boxes_path;
+    $self->{max_popbox} = $max_popbox;
+    $self->{catchall} = $catchall;
+    $self->{boxes} = $boxes;
+    $self->{forward} = $forwards;
+    
+    return $self;
+}
+
 sub getall
 {
-    my $vhffs = shift;
-    my $state = shift;
-    my $name = shift;
-    my $group = shift;
+    my ($vhffs, $state, $name, $group) = @_;
 	
-    my $query;
+    my $sql;
     my $request;
+    my @params;
 
     my $objs;
     my $result;
     my $tmp;
 
-    $query = "SELECT m.domain FROM  vhffs_mxdomain m, vhffs_object o WHERE m.object_id = o.object_id";
-	$query .= " AND o.state='".$state."'" if( defined $state );
-	$query .= " AND m.domain LIKE '%".$name."%'" if( defined $name );
-	$query .= " AND m.owner_gid='".$group->get_gid."'" if( defined $group );
-    $request = $vhffs->{'db'}->prepare( $query );
-    my $rows = $request->execute;
+    $sql = 'SELECT m.domain FROM  vhffs_mxdomain m, vhffs_object o WHERE m.object_id = o.object_id';
+    if(defined $state) {
+        $sql .= ' AND o.state=?';
+        push(@params, $state);
+    }
+    if(defined $name) {
+        $sql .= ' AND m.domain LIKE ?';
+        push(@params, '%'.$name.'%');
+    }
+    if(defined $group) {
+        $sql .= ' AND m.owner_gid= ?';
+        push(@params, $group->get_gid);
+    }
+    $request = $vhffs->{'db'}->prepare( $sql );
+    my $rows = $request->execute(@params);
 
-    return undef if( $rows <= 0);
+    return undef if( !$rows );
 
     while( $result = $request->fetchrow_hashref() )
     {
-    	$tmp = new Vhffs::Services::Mail( $vhffs , $result->{'domain'} );
-    	push( @{$objs} , $tmp ) if( ( defined ( $tmp ) ) && ( $tmp->fetch > 0 ) )
+        push( @$objs, Vhffs::Services::Mail::get_by_mxdomain( $vhffs, $result->{domain} ) );
     }
     return $objs;
 }

Added: branches/vhffs_4.1/vhffs-tests/src/Services/Mail.pl
===================================================================
--- branches/vhffs_4.1/vhffs-tests/src/Services/Mail.pl	2007-02-16 15:16:38 UTC (rev 457)
+++ branches/vhffs_4.1/vhffs-tests/src/Services/Mail.pl	2007-02-16 15:39:56 UTC (rev 458)
@@ -0,0 +1,41 @@
+use strict;
+use Vhffs::Tests::Main;
+use Vhffs::Tests::Utils;
+use Vhffs::Constants;
+use Vhffs::User;
+use Vhffs::Services::Mail;
+use Test::More 'no_plan';
+
+my $main = init Vhffs::Tests::Main;
+isa_ok($main, 'Vhffs::Tests::Main', '$main');
+
+Vhffs::Tests::Utils::init_db($main->get_db_object);
+
+my $user1 = Vhffs::User::create($main, 'test1', 'abcdef', 0, 'test1@xxxxxxxx');
+isa_ok($user1, 'Vhffs::User', '$user1');
+
+my $group1 = Vhffs::Group::create($main, 'mailgroup1', $user1->get_uid, undef, 'Test group for Mail Domain');
+isa_ok($group1, 'Vhffs::Group', '$group1');
+
+my $mail1 = Vhffs::Services::Mail::create($main, 'test.com', 'Test mail domain 1', $user1, $group1);
+isa_ok($mail1, 'Vhffs::Services::Mail', '$mail1');
+cmp_ok($mail1->get_owneruid, '==', $user1->get_uid, 'uid matches');
+cmp_ok($mail1->get_ownergid, '==', $group1->get_gid, 'gid matches');
+
+$mail1->addforward('forward1', 'test@xxxxxxxx');
+my $forwards = $mail1->get_forwards;
+cmp_ok(scalar(%$forwards), '==', '1', 'Forward added');
+ok(defined $forwards->{forward1}, 'Forward name ok');
+cmp_ok($forwards->{forward1}{local_part}, 'eq', 'forward1', 'Forward local part OK');
+cmp_ok($forwards->{forward1}{domain}, 'eq', 'test.com', 'Domain name propagated');
+cmp_ok($forwards->{forward1}{remote_name}, 'eq', 'test@xxxxxxxx', 'Remote name OK');
+
+$mail1->addbox('box1', 'dummy');
+my $boxes = $mail1->get_boxes;
+cmp_ok(scalar(%$boxes), '==', '1', 'Box added');
+cmp_ok($boxes->{box1}{local_part}, '==', 'box1', 'Local part OK');
+cmp_ok($boxes->{box1}{domain}, '==', 'test.com', 'Domain name propagated');
+
+my $mail2 = Vhffs::Services::Mail::get_by_mxdomain($main, 'test.com');
+isa_ok($mail2, 'Vhffs::Services::Mail', '$mail2');
+is_deeply($mail1, $mail2, 'Fetched Mail matches created Mail');


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