[vhffs-dev] [990] Add robots to create and delete mailboxes

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


Revision: 990
Author:   gradator
Date:     2007-10-15 20:00:00 +0000 (Mon, 15 Oct 2007)

Log Message:
-----------
Add robots to create and delete mailboxes

Modified Paths:
--------------
    trunk/vhffs-api/src/Vhffs/Makefile.am
    trunk/vhffs-api/src/Vhffs/Services/Mail.pm
    trunk/vhffs-backend/conf/vhffs.conf.dist.in
    trunk/vhffs-robots/Makefile.am

Added Paths:
-----------
    trunk/vhffs-api/src/Vhffs/Robots/Mail.pm
    trunk/vhffs-robots/src/mail_createboxes.pl
    trunk/vhffs-robots/src/mail_deleteboxes.pl


Modified: trunk/vhffs-api/src/Vhffs/Makefile.am
===================================================================
--- trunk/vhffs-api/src/Vhffs/Makefile.am	2007-10-14 22:08:36 UTC (rev 989)
+++ trunk/vhffs-api/src/Vhffs/Makefile.am	2007-10-15 20:00:00 UTC (rev 990)
@@ -41,6 +41,7 @@
 	Panel/Template.pm \
 	Robots/Cvs.pm \
 	Robots/Group.pm \
+	Robots/Mail.pm \
 	Robots/Mailing.pm \
 	Robots/Mysql.pm \
 	Robots/Pgsql.pm \

Added: trunk/vhffs-api/src/Vhffs/Robots/Mail.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Robots/Mail.pm	                        (rev 0)
+++ trunk/vhffs-api/src/Vhffs/Robots/Mail.pm	2007-10-15 20:00:00 UTC (rev 990)
@@ -0,0 +1,66 @@
+#!%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.
+
+
+package Vhffs::Robots::Mail;
+
+use DBI;
+use POSIX qw(locale_h);
+use HTML::Template;
+use locale;
+use Locale::gettext;
+use CGI;
+use CGI::Session;
+use strict;
+use Vhffs::User;
+use Vhffs::Main;
+use Vhffs::Services::Mail;
+
+
+sub getall_boxes
+{
+	my ( $vhffs, $state ) = @_;
+
+	my @params;
+
+	my $sql = 'SELECT m.domain, m.boxes_path, b.local_part, b.mbox_name, b.state FROM vhffs_boxes b INNER JOIN vhffs_mxdomain m ON m.domain=b.domain';
+	if( defined $state)   {
+		$sql .= ' WHERE b.state=?';
+		push(@params, $state);
+	}
+
+	my $request = $vhffs->{'db'}->prepare( $sql );
+	return undef unless $request->execute( @params );
+	return $request->fetchall_arrayref( {} );
+}
+
+
+1;

Modified: trunk/vhffs-api/src/Vhffs/Services/Mail.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Services/Mail.pm	2007-10-14 22:08:36 UTC (rev 989)
+++ trunk/vhffs-api/src/Vhffs/Services/Mail.pm	2007-10-15 20:00:00 UTC (rev 990)
@@ -291,6 +291,17 @@
 	return 1;
 }
 
+sub get_box_dir
+{
+	my $self = shift;
+	my $local_part = shift;
+
+	return undef unless( defined $self  &&  defined $local_part  &&  defined $self->{'boxes'}{$local_part} );
+ 
+	return( $self->{'main'}->get_config->get_datadir . '/mail/boxes/' . $self->{'boxes_path'} . '/' . $self->{'boxes'}{$local_part}{'mbox_name'} );
+}
+
+
 =pod
 
 =head2 addforward
@@ -663,6 +674,7 @@
     return $objs;
 }
 
+
 =pod address_exists
 
     print ("Adress $local_part\@$domain already exists as a box or forward\n")

Modified: trunk/vhffs-backend/conf/vhffs.conf.dist.in
===================================================================
--- trunk/vhffs-backend/conf/vhffs.conf.dist.in	2007-10-14 22:08:36 UTC (rev 989)
+++ trunk/vhffs-backend/conf/vhffs.conf.dist.in	2007-10-15 20:00:00 UTC (rev 990)
@@ -358,6 +358,14 @@
 		# domain: only on the concerned domain
 		# open: any address
 		allowed_catchall = domain
+
+		# UID and GID of boxes
+		boxes_uid 	=	102
+		boxes_gid	=	102
+
+		# Where boxes are archived upon deletion (don't forget to chmod 700 this directory)
+		# if unset or if the directory doesn't exist, boxes will not be archived
+		archives_dir	=	/data/mail/archives
 	</mail>
 
 	# The mailuser service allows a user to get a mail account on a default domain

Modified: trunk/vhffs-robots/Makefile.am
===================================================================
--- trunk/vhffs-robots/Makefile.am	2007-10-14 22:08:36 UTC (rev 989)
+++ trunk/vhffs-robots/Makefile.am	2007-10-15 20:00:00 UTC (rev 990)
@@ -32,7 +32,9 @@
 	src/group_setquota.pl \
 	src/listengine_publicarchives.pl \
 	src/mail_create.pl \
+	src/mail_createboxes.pl \
 	src/mail_delete.pl \
+	src/mail_deleteboxes.pl \
 	src/mailinglist_create.pl \
 	src/mailinglist_delete.pl \
 	src/mailing.pl \

Added: trunk/vhffs-robots/src/mail_createboxes.pl
===================================================================
--- trunk/vhffs-robots/src/mail_createboxes.pl	                        (rev 0)
+++ trunk/vhffs-robots/src/mail_createboxes.pl	2007-10-15 20:00:00 UTC (rev 990)
@@ -0,0 +1,73 @@
+#!%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 boxes
+
+use lib '%VHFFS_LIB_DIR%';
+use Vhffs::Services::Mail;
+use Vhffs::Robots::Mail;
+use Vhffs::Robots;
+use Vhffs::Main;
+
+my $vhffs = init Vhffs::Main;
+return 1 unless defined $vhffs;
+my $mailconf = $vhffs->get_config->get_service('mail');
+return 1 unless ( defined $mailconf && defined $mailconf->{'boxes_uid'} && $mailconf->{'boxes_gid'} );
+
+Vhffs::Robots::lock( $vhffs , 'mail' );
+
+my $boxes = Vhffs::Robots::Mail::getall_boxes( $vhffs , Vhffs::Constants::WAITING_FOR_CREATION );
+foreach my $b ( @{$boxes} )
+{
+	my $mail = Vhffs::Services::Mail::get_by_mxdomain( $vhffs, $b->{domain} );
+	if( defined $mail )  {
+
+		my $dir = $mail->get_box_dir( $b->{local_part} );
+		Vhffs::Functions::create_dir( $dir );
+		if( -d $dir )  {
+			Vhffs::Functions::create_dir( $dir.'/Maildir' );
+			Vhffs::Functions::create_dir( $dir.'/Maildir/cur' );
+			Vhffs::Functions::create_dir( $dir.'/Maildir/new' );
+			Vhffs::Functions::create_dir( $dir.'/Maildir/tmp' );
+			Vhffs::Functions::chmod_recur( $dir , 0600 , 0700 );
+			Vhffs::Functions::change_owner_recur( $dir , $mailconf->{'boxes_uid'} , $mailconf->{'boxes_gid'} );
+
+			$mail->set_box_status( $b->{local_part} , Vhffs::Constants::ACTIVATED );
+		} else  {
+			$mail->set_box_status( $b->{local_part} , Vhffs::Constants::CREATING_ERROR );
+		}
+	}
+}
+
+Vhffs::Robots::unlock( $vhffs , 'mail' );
+
+
+exit 0;


Property changes on: trunk/vhffs-robots/src/mail_createboxes.pl
___________________________________________________________________
Name: svn:executable
   + *

Added: trunk/vhffs-robots/src/mail_deleteboxes.pl
===================================================================
--- trunk/vhffs-robots/src/mail_deleteboxes.pl	                        (rev 0)
+++ trunk/vhffs-robots/src/mail_deleteboxes.pl	2007-10-15 20:00:00 UTC (rev 990)
@@ -0,0 +1,71 @@
+#!%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 archives and delete mail boxes
+
+use lib '%VHFFS_LIB_DIR%';
+use Vhffs::Services::Mail;
+use Vhffs::Robots::Mail;
+use Vhffs::Robots;
+use Vhffs::Main;
+
+my $vhffs = init Vhffs::Main;
+return 1 unless defined $vhffs;
+my $mailconf = $vhffs->get_config->get_service('mail');
+return 1 unless defined $mailconf;
+
+Vhffs::Robots::lock( $vhffs , 'mail' );
+
+my $boxes = Vhffs::Robots::Mail::getall_boxes( $vhffs , Vhffs::Constants::TO_DELETE );
+foreach my $b ( @{$boxes} )
+{
+	my $mail = Vhffs::Services::Mail::get_by_mxdomain( $vhffs, $b->{domain} );
+	if( defined $mail )  {
+
+		my $dir = $mail->get_box_dir( $b->{local_part} );
+		if( -d $dir ) {
+			if(  chdir( $dir )  )  {
+				if( defined $mailconf->{'archives_dir'}  &&  -d $mailconf->{'archives_dir'} )  {
+					my $cmd = 'tar cf '.$mailconf->{'archives_dir'}.'/'.time().'_'.$b->{local_part}.'@'.$b->{domain}.'.tar . 1> /dev/null 2> /dev/null';
+					system( $cmd );
+				}
+				my $cmd = 'rm -rf '.$dir.' 2> /dev/null';
+				system( $cmd );
+			}
+		}
+		$mail->delbox( $b->{local_part} );
+	}
+}
+
+Vhffs::Robots::unlock( $vhffs , 'mail' );
+
+
+exit 0;


Property changes on: trunk/vhffs-robots/src/mail_deleteboxes.pl
___________________________________________________________________
Name: svn:executable
   + *


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