#!%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;

package Vhffs::Robots::User;

use Vhffs::User;
use Vhffs::Constants;

sub create
{
	my $user = shift;
	return if( ! defined $user );

	create_home( $user->{'main'} , $user->get_username );
}

sub delete
{
	my $vhffs = shift;
	my $user = shift;
	return if( ! defined $user );
	delete_home( $vhffs , $user );
}

sub delete_home
{
	my( $main , $user , $homedir , $cmd );

	$main = shift;
	$user = shift;

	if( defined $user )
	{
		$user->add_history( "Ok, robots takes now the destruction of this user" );
		$homedir = $user->get_home;

		if( $homedir && -d $homedir )
		{
			$cmd = "rm -rf $homedir";
			system( $cmd );
		}
	}

	return 1;
}


sub create_home
{
	my $main = shift;
	my $user = shift;

	return -1 unless defined $user;

	my $homedir = $user->get_home;
	
	unless( Vhffs::Functions::create_dir( $homedir ) )  {
		chown $user->get_uid , $user->get_gid , $homedir;
		chmod 0700, $homedir;
		
		$user->add_history( 'homedir created' );
		$user->set_status( Vhffs::Constants::ACTIVATED );
		return -1 if( $user->commit < 0 );

		my $subject = sprintf("Account created on %s", $main->get_config->get_host_name );
		my $content = sprintf("Hello %s %s,\n\nWe are pleased to announce that your account is now fully created on\n%s.\nYou can now login on the panel.\n\n%s Administrators\n", $user->get_firstname, $user->get_lastname, $main->get_config->get_host_name, $main->get_config->get_host_name );
		$user->send_mail_user( $subject, $content );
	}
	else  {
		$user->add_history( 'cannot create the homedir directory' );
#		$user->set_status( Vhffs::Constants::CREATING_ERROR );
#		$user->commit;
		return -1;
	}

	return 0;
}


1;
