#!%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 file is a part of VHFFS4 software, a hosting platform suite
# Please respect the licence of this file and whole program

# Author : Sylvain Rochet < gradator at gradator dot net >

#This module helps you to manage a simple maildomain
#for all hosted people

use strict;
use utf8;

package Vhffs::Services::MailGroup;

use DBI;
use Vhffs::Group;
use Vhffs::Services::Mail;


# Create a new instance of the current class
sub init
{
	my $class = shift;
	my $vhffs = shift;
	my $group = shift;

	return undef unless ( defined $vhffs  &&  defined  $group  &&  $vhffs->get_config->get_service_availability('mailgroup') );
	my $config = $vhffs->get_config->get_service('mailgroup');

	# Fetches the mail domain defined in config
	my $mail_service = Vhffs::Services::Mail::get_by_mxdomain( $vhffs, $config->{domain} );
	return undef unless defined $mail_service;

	my $this = {};
	$this->{main} = $vhffs;
	$this->{config} = $config;
	$this->{group} = $group;
	$this->{localpart} = $group->get_groupname;
	$this->{domain} = $config->{domain};
	$this->{mail_service} = $mail_service;
	bless( $this , $class );
	return $this;
}

sub exists
{
	my $self = shift;
	return $self->{mail_service}->exists( $self->{localpart} );
}

sub exists_forward
{
	my $self = shift;
	return $self->{mail_service}->exists_forward( $self->{localpart} );
}

sub exists_box
{
	my $self = shift;
	return $self->{mail_service}->exists_box( $self->{localpart} );
}

sub use_nospam
{
	my $self = shift;
	return $self->{mail_service}->use_nospam( $self->{localpart} );
}

sub use_novirus
{
	my $self = shift;
	return $self->{mail_service}->use_novirus( $self->{localpart} );
}

sub change_spam_status
{
	my $self = shift;
	$self->{mail_service}->change_spam_status( $self->{localpart} );
	return 1;
}

sub change_virus_status
{
	my $self = shift;
	$self->{mail_service}->change_virus_status( $self->{localpart} );
	return 1;
}

sub addforward
{
	my $self = shift;
	my $remote = shift;
	
	$self->{mail_service}->delforward( $self->{localpart} ) if( $self->exists_forward == 1 );
	$self->{mail_service}->set_box_status( $self->{localpart} , Vhffs::Constants::TO_DELETE ) if( $self->exists_box == 1 );
	return -1 if ( $self->{mail_service}->addforward( $self->{localpart} , $remote ) < 0);
}

sub getforward
{
	my $self = shift;
	return undef unless $self->{mail_service}->{'forward'}->{$self->{localpart}};
	return $self->{mail_service}->{'forward'}->{$self->{localpart}}->{'remote_name'};
}

sub delforward
{
	my $self = shift;
	$self->{mail_service}->delforward( $self->{localpart} );
}

sub delbox
{
	my $self = shift;
	$self->{mail_service}->set_box_status( $self->{localpart} , Vhffs::Constants::TO_DELETE );
}

sub addbox
{
	my $self = shift;
	my $password = shift;

	$self->{mail_service}->delforward( $self->{localpart} ) if( $self->exists_forward == 1 );
	return -1 if( $self->{mail_service}->addbox( $self->{localpart} , $password ) < 0);
}

sub changepassword
{
	my ($self, $newpass) = @_;
	return -1 if( $self->{mail_service}->change_box_password( $self->{localpart}, $newpass ) < 0 );
}

1;
