[vhffs-dev] [2063] Reworked mail robots

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


Revision: 2063
Author:   gradator
Date:     2012-02-26 00:10:49 +0100 (Sun, 26 Feb 2012)
Log Message:
-----------
Reworked mail robots

Modified Paths:
--------------
    trunk/vhffs-api/src/Vhffs/Robots/Group.pm
    trunk/vhffs-api/src/Vhffs/Robots/Mail.pm
    trunk/vhffs-robots/Makefile.am
    trunk/vhffs-robots/src/mail_createboxes.pl

Added Paths:
-----------
    trunk/vhffs-robots/src/mail.pl

Removed Paths:
-------------
    trunk/vhffs-robots/src/mail_create.pl
    trunk/vhffs-robots/src/mail_delete.pl

Modified: trunk/vhffs-api/src/Vhffs/Robots/Group.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Robots/Group.pm	2012-02-25 22:04:32 UTC (rev 2062)
+++ trunk/vhffs-api/src/Vhffs/Robots/Group.pm	2012-02-25 23:10:49 UTC (rev 2063)
@@ -34,14 +34,14 @@
 use File::Path;
 use File::Basename;
 
-package Vhffs::Robots::Group;
-
 use Vhffs::Group;
 use Vhffs::User;
 use Vhffs::Functions;
 use Vhffs::Constants;
 use Vhffs::Robots;
 
+package Vhffs::Robots::Group;
+
 sub create {
 	my $group = shift;
 	return undef unless defined $group and $group->get_status == Vhffs::Constants::WAITING_FOR_CREATION;

Modified: trunk/vhffs-api/src/Vhffs/Robots/Mail.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Robots/Mail.pm	2012-02-25 22:04:32 UTC (rev 2062)
+++ trunk/vhffs-api/src/Vhffs/Robots/Mail.pm	2012-02-25 23:10:49 UTC (rev 2063)
@@ -31,20 +31,17 @@
 
 use strict;
 use utf8;
+use File::Path;
+use File::Basename;
 
-package Vhffs::Robots::Mail;
-
-use DBI;
-use POSIX qw(locale_h);
-use locale;
-use Locale::gettext;
-use Vhffs::User;
-use Vhffs::Main;
+use Vhffs::Constants;
+use Vhffs::Functions;
+use Vhffs::Robots;
 use Vhffs::Services::Mail;
 
+package Vhffs::Robots::Mail;
 
-sub getall_boxes
-{
+sub getall_boxes {
 	my ( $vhffs, $state ) = @_;
 
 	my @params;
@@ -60,5 +57,80 @@
 	return $request->fetchall_arrayref( {} );
 }
 
+sub create {
+	my $mail = shift;
+	return undef unless defined $mail and $mail->get_status == Vhffs::Constants::WAITING_FOR_CREATION;
 
+	my $vhffs = $mail->get_main;
+
+	$mail->set_status( Vhffs::Constants::ACTIVATED );
+	$mail->commit;
+	Vhffs::Robots::vhffs_log( $vhffs, 'Created mail domain '.$mail->get_domain );
+
+	return 1;
+}
+
+sub delete {
+	my $mail = shift;
+	return undef unless defined $mail and $mail->get_status == Vhffs::Constants::WAITING_FOR_DELETION;
+
+	my $vhffs = $mail->get_main;
+	my $dir = $mail->get_dir();
+
+	require Vhffs::Services::MailingList;
+	my $lists = Vhffs::Services::MailingList::getall( $vhffs, undef, undef, undef, $mail->get_domain );
+	if( @$lists ) {
+		foreach( @{$lists} ) {
+			next if $_->get_status == Vhffs::Constants::WAITING_FOR_DELETION;	
+			$_->set_status( Vhffs::Constants::WAITING_FOR_DELETION );
+			$_->commit;
+		}
+		Vhffs::Robots::vhffs_log( $vhffs, 'Cannot delete mail domain '.$mail->get_domain.', remaining mailing lists, requests have been sent to delete them' );
+		# Do not call next right now, check for boxes to avoir further failure
+	}
+
+	if( $mail->nb_boxes ) {
+		foreach ( values( %{$mail->get_boxes} ) ) {
+			$mail->set_box_status( $_->{'local_part'}, Vhffs::Constants::WAITING_FOR_DELETION );
+		}
+		Vhffs::Robots::vhffs_log( $vhffs, 'Cannot delete mail domain '.$mail->get_domain.', '.$mail->nb_boxes.' boxes remaining, requests have been sent to delete them' );
+		return 1;
+	} 
+
+	return 1 if @$lists;
+
+	File::Path::remove_tree( $dir, { error => \my $errors });
+	# Mail domain directories are hashed on two levels, so we've potentially two empty directories to delete
+	my $parent = File::Basename::dirname($dir);
+	rmdir $parent;
+	$parent = File::Basename::dirname($parent);
+	rmdir $parent;
+
+	if(@$errors) {
+		$mail->set_status( Vhffs::Constants::DELETION_ERROR );
+		$mail->commit;
+		Vhffs::Robots::vhffs_log( $vhffs, 'An error occured while removing mail domain '.$mail->get_domain.' from the filesystem: '.join(', ', @$errors) );
+		return undef;
+	}
+
+	if( $mail->delete ) {
+		Vhffs::Robots::vhffs_log( $vhffs, 'Deleted mail domain '.$mail->get_domain );
+	} else {
+		Vhffs::Robots::vhffs_log( $vhffs, 'An error occured while deleting mail domain '.$mail->get_domain );
+		$mail->set_status( Vhffs::Constants::DELETION_ERROR );
+		$mail->commit;
+		return undef;
+	}
+
+	return 1;
+}
+
+sub modify {
+	my $mail = shift;
+	return undef unless defined $mail and $mail->get_status == Vhffs::Constants::WAITING_FOR_MODIFICATION;
+	$mail->set_status( Vhffs::Constants::ACTIVATED );
+	$mail->commit;
+	return 1;
+}
+
 1;

Modified: trunk/vhffs-robots/Makefile.am
===================================================================
--- trunk/vhffs-robots/Makefile.am	2012-02-25 22:04:32 UTC (rev 2062)
+++ trunk/vhffs-robots/Makefile.am	2012-02-25 23:10:49 UTC (rev 2063)
@@ -25,9 +25,8 @@
 	src/group.pl \
 	src/group_quota.pl \
 	src/listengine_publicarchives.pl \
-	src/mail_create.pl \
+	src/mail.pl \
 	src/mail_createboxes.pl \
-	src/mail_delete.pl \
 	src/mail_deleteboxes.pl \
 	src/mailinglist.pl \
 	src/mailing.pl \

Copied: trunk/vhffs-robots/src/mail.pl (from rev 2056, trunk/vhffs-robots/src/mail_create.pl)
===================================================================
--- trunk/vhffs-robots/src/mail.pl	                        (rev 0)
+++ trunk/vhffs-robots/src/mail.pl	2012-02-25 23:10:49 UTC (rev 2063)
@@ -0,0 +1,59 @@
+#!%PERL%
+# Copyright (c) vhffs project and its contributors
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without 
+# modification, are permitted provided that the following conditions 
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright 
+#   notice, this list of conditions and the following disclaimer.
+#2. Redistributions in binary form must reproduce the above copyright
+#   notice, this list of conditions and the following disclaimer in 
+#   the documentation and/or other materials provided with the 
+#   distribution.
+#3. Neither the name of vhffs nor the names of its contributors 
+#   may be used to endorse or promote products derived from this 
+#   software without specific prior written permission.
+#
+#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
+#"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
+#LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
+#FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
+#COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
+#INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
+#BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
+#LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
+#CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
+# POSSIBILITY OF SUCH DAMAGE.
+
+use strict;
+use utf8;
+
+use lib '%VHFFS_LIB_DIR%';
+use Vhffs::Robots::Mail;
+
+my $vhffs = init Vhffs::Main;
+exit 1 unless defined $vhffs;
+
+Vhffs::Robots::lock( $vhffs, 'mail' );
+
+my $mails = Vhffs::Services::Mail::getall( $vhffs, Vhffs::Constants::WAITING_FOR_CREATION );
+foreach ( @{$mails} ) {
+	Vhffs::Robots::Mail::create( $_ );
+}
+
+$mails = Vhffs::Services::Mail::getall( $vhffs, Vhffs::Constants::WAITING_FOR_DELETION );
+foreach ( @{$mails} ) {
+	Vhffs::Robots::Mail::delete( $_ );
+}
+
+$mails = Vhffs::Services::Mail::getall( $vhffs, Vhffs::Constants::WAITING_FOR_MODIFICATION );
+foreach ( @{$mails} ) {
+	Vhffs::Robots::Mail::modify( $_ );
+}
+
+Vhffs::Robots::unlock( $vhffs, 'mail' );
+exit 0;

Deleted: trunk/vhffs-robots/src/mail_create.pl
===================================================================
--- trunk/vhffs-robots/src/mail_create.pl	2012-02-25 22:04:32 UTC (rev 2062)
+++ trunk/vhffs-robots/src/mail_create.pl	2012-02-25 23:10:49 UTC (rev 2063)
@@ -1,71 +0,0 @@
-#!%PERL%
-# Copyright (c) vhffs project and its contributors
-# All rights reserved.
-#
-# Redistribution and use in source and binary forms, with or without 
-# modification, are permitted provided that the following conditions 
-# are met:
-#
-# 1. Redistributions of source code must retain the above copyright 
-#   notice, this list of conditions and the following disclaimer.
-#2. Redistributions in binary form must reproduce the above copyright
-#   notice, this list of conditions and the following disclaimer in 
-#   the documentation and/or other materials provided with the 
-#   distribution.
-#3. Neither the name of vhffs nor the names of its contributors 
-#   may be used to endorse or promote products derived from this 
-#   software without specific prior written permission.
-#
-#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
-#"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
-#LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
-#FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
-#COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
-#INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
-#BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
-#LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
-#CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
-# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
-# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
-# POSSIBILITY OF SUCH DAMAGE.
-
-#This robot create mail domain
-#It actives it
-
-# FIXME : This robot is unnecessary, we just have to activate mailbox on creation
-
-use strict;
-use utf8;
-
-use lib '%VHFFS_LIB_DIR%';
-use Vhffs::Services::Mail;
-use Vhffs::Robots;
-use Vhffs::Main;
-
-
-my $vhffs = init Vhffs::Main;
-
-Vhffs::Robots::lock( $vhffs , "mail" );
-
-my $mails = Vhffs::Services::Mail::getall( $vhffs , Vhffs::Constants::WAITING_FOR_CREATION );
-my $m;
-
-
-foreach $m ( @{$mails} )
-{
-	$m->add_history( "Robots will activated this mail domain" );
-	$m->set_status( Vhffs::Constants::ACTIVATED );
-	if( $m->commit > 0 )
-	{
-		Vhffs::Robots::vhffs_log( $vhffs, sprintf( "Create mail domain %s" , $m->get_domain ) );
-	}
-	else
-	{
-		Vhffs::Robots::vhffs_log( $vhffs, sprintf( "cannot create mail domain %s" , $m->get_domain ) );
-	}
-}
-
-Vhffs::Robots::unlock( $vhffs , "mail" );
-
-
-exit 0;

Modified: trunk/vhffs-robots/src/mail_createboxes.pl
===================================================================
--- trunk/vhffs-robots/src/mail_createboxes.pl	2012-02-25 22:04:32 UTC (rev 2062)
+++ trunk/vhffs-robots/src/mail_createboxes.pl	2012-02-25 23:10:49 UTC (rev 2063)
@@ -55,14 +55,16 @@
 	if( defined $mail )  {
 
 		my $dir = $mail->get_box_dir( $b->{local_part} );
-		my $prevumask = umask 0077;
-		File::Path::make_path( $dir.'/Maildir/cur', $dir.'/Maildir/new', $dir.'/Maildir/tmp',
-			{ owner => $mailconf->{'boxes_uid'}, group => $mailconf->{'boxes_gid'}, mode => 0700, error => \my $errors });
+		my $prevumask = umask 0;
+		File::Path::make_path( $dir, { mode => 0755, error => \my $errors });
+		File::Path::make_path( $dir.'/Maildir/cur', $dir.'/Maildir/new', $dir.'/Maildir/tmp', { mode => 0700, error => \$errors }) unless @$errors;
 		umask $prevumask;
-		# make_path owner and group directives do not seem to work if owner and group and numeric
-		Vhffs::Robots::chown_recur( $dir, $mailconf->{'boxes_uid'}, $mailconf->{'boxes_gid'} );
+		
+		unless( @$errors ) {
+			# make_path owner and group directives do not seem to work if owner and group and numeric
+			Vhffs::Robots::chown_recur( $dir, $mailconf->{'boxes_uid'}, $mailconf->{'boxes_gid'} );
+			chmod 0700, $dir;
 
-		unless( @$errors ) {
 			$mail->set_box_status( $b->{local_part} , Vhffs::Constants::ACTIVATED );
 		} else  {
 			$mail->set_box_status( $b->{local_part} , Vhffs::Constants::CREATION_ERROR );

Deleted: trunk/vhffs-robots/src/mail_delete.pl
===================================================================
--- trunk/vhffs-robots/src/mail_delete.pl	2012-02-25 22:04:32 UTC (rev 2062)
+++ trunk/vhffs-robots/src/mail_delete.pl	2012-02-25 23:10:49 UTC (rev 2063)
@@ -1,98 +0,0 @@
-#!%PERL%
-# Copyright (c) vhffs project and its contributors
-# All rights reserved.
-#
-# Redistribution and use in source and binary forms, with or without 
-# modification, are permitted provided that the following conditions 
-# are met:
-#
-# 1. Redistributions of source code must retain the above copyright 
-#   notice, this list of conditions and the following disclaimer.
-#2. Redistributions in binary form must reproduce the above copyright
-#   notice, this list of conditions and the following disclaimer in 
-#   the documentation and/or other materials provided with the 
-#   distribution.
-#3. Neither the name of vhffs nor the names of its contributors 
-#   may be used to endorse or promote products derived from this 
-#   software without specific prior written permission.
-#
-#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
-#"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
-#LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
-#FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
-#COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
-#INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
-#BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
-#LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
-#CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
-# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
-# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
-# POSSIBILITY OF SUCH DAMAGE.
-
-use strict;
-use utf8;
-use File::Path;
-use File::Basename;
-
-use lib '%VHFFS_LIB_DIR%';
-
-use Vhffs::Services::Mail;
-use Vhffs::Services::MailingList;
-use Vhffs::Robots;
-use Vhffs::Main;
-
-
-my $vhffs = init Vhffs::Main;
-
-Vhffs::Robots::lock( $vhffs , "mail" );
-
-my $mails = Vhffs::Services::Mail::getall( $vhffs , Vhffs::Constants::WAITING_FOR_DELETION );
-my $mail;
-my $lists;
-my $list;
-foreach $mail ( @{$mails} )
-{
-	my $lists = Vhffs::Services::MailingList::getall( $vhffs , undef , undef , undef , $mail->get_domain );
-	if( @$lists != 0) {
-		foreach $list ( @{$lists} ) {
-			$list->set_status( Vhffs::Constants::WAITING_FOR_DELETION );
-			$list->commit;
-		}
-		Vhffs::Robots::vhffs_log( $vhffs, sprintf( 'Cannot delete mail domain %s, remains lists, requests have been sent to delete them' , $mail->get_domain ) );
-		# Do not call next right now, check for boxes to avoir further failure
-	}
-
-	if( $mail->nb_boxes() > 0 ) {
-		foreach my $bo ( values( %{$mail->get_boxes()} ) ) {
-			$mail->set_box_status( $bo->{'local_part'} , Vhffs::Constants::WAITING_FOR_DELETION );
-		}
-		Vhffs::Robots::vhffs_log( $vhffs, sprintf( 'Cannot delete mail domain %s, remains %d boxes, requests have been sent to delete them' , $mail->get_domain , $mail->nb_boxes() ) );
-		next;
-	} 
-
-	next if(@$lists);
-
-	Vhffs::Robots::vhffs_log( $vhffs, sprintf( "Delete mail domain %s" , $mail->get_domain ) );
-	my $dir = $mail->get_dir();
-
-	if( defined($dir) && -d $dir && -r _ && -w _ && -x _ )  {
-		my $dir = $mail->get_dir;
-		File::Path::remove_tree( $mail->get_dir, { error => \my $errors });
-		# Mail domain directories are hashed on two levels, so we've potentially two empty directories to delete
-		$dir = File::Basename::dirname($dir);
-		rmdir $dir;
-		$dir = File::Basename::dirname($dir);
-		rmdir $dir;
-
-		if(@$errors) {
-			Vhffs::Robots::vhffs_log( $vhffs, sprintf( 'Something went wrong during %s mail domain deletion: %s', $mail->get_domain, join(', ', @$errors) ) );
-		}
-	} else {
-		Vhffs::Robots::vhffs_log( $vhffs, sprintf( 'Unable to get mail domain directory or bad permission for mail domain %s, aborting file system deletion', $mail->get_domain ) );
-	}
-	$mail->delete;
-}
-
-Vhffs::Robots::unlock( $vhffs , "mail" );
-exit 0;
-


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