#!%PERL% -w
# 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 POSIX qw(locale_h);
use HTML::Template::Expr;
use locale;
use Locale::gettext;
use Encode;

use lib '%VHFFS_LIB_DIR%';
use Vhffs::User;
use Vhffs::Group;
use Vhffs::Main;
use Vhffs::Panel::Main;
use Vhffs::Panel::Menu;
use Vhffs::Panel::Group;
use Vhffs::Panel::Mail;

my $panel = new Vhffs::Panel::Main();
exit 0 unless $panel;
my $session = $panel->get_session;
exit 0 unless $session;

my $vhffs = $panel->{vhffs};
my $templatedir = $panel->{templatedir};
my $cgi = $panel->{cgi};
my $user = $panel->{user};
my $group = Vhffs::Group::get_by_groupname( $vhffs , $cgi->param('group') );

unless( defined $group && $user->can_modify( $group ) ) {
	my $template = new HTML::Template( filename => $templatedir.'/panel/misc/simplemsg.tmpl' );
	$template->param( MESSAGE => gettext( 'You\'re not allowed to do this (ACL rights)' ) );

	$panel->build( $template );
	$panel->display;
}
else  {
	my $submitted = $cgi->param('mailing_submit');
	my $localpart = '';
	my $domain = '';
	my $description = '';
	# Get default domain for list configured in the config file
	my $default_domain = $vhffs->get_config->get_service('mailinglist')->{'default_domain'};
	
	if( $submitted ) {
		$localpart = $cgi->param( 'localpart' );
		$domain = $cgi->param( 'domain' );
		$description = Encode::decode_utf8( $cgi->param( 'description' ) );

		unless( defined $localpart  &&  defined $domain  &&  defined $description ) {
			$panel->add_error( gettext('CGI Error !') );
		} elsif( $description =~ /^\s*$/ ) {
			$panel->add_error( gettext('You must enter a description') );
		} elsif( $localpart !~ /^[a-z0-9\_\-]+$/ ) {
			$panel->add_error( gettext('Invalid local part') );
		} elsif( $domain ne $default_domain &&  !Vhffs::Panel::Mail::is_owned_by_group( $vhffs, $domain, $group->get_gid ) ) {
			$panel->add_error( gettext('You do not own this domain !') );
		} elsif( defined Vhffs::Panel::MailingList::create_list( $vhffs, $localpart, $domain, $description, $user, $group ) ) {
			my $url = '/group/view.pl?group='.$group->get_groupname.'&msg='.gettext('The mailing list object was successfully created !');
			$panel->redirect( $url );
		} else {
			$panel->add_error( gettext('An error occured while creating the object.It probably already exists') );
		}
	}

	if( !$submitted || $panel->has_errors() ) {
		my $domains = Vhffs::Panel::MailingList::getall_mdomains_per_group( $vhffs, $group->get_gid );
		push(@$domains, { domain => $default_domain }) if( defined $default_domain );

		$panel->set_title( gettext('Create a new mailing list') );

		if( ( !@$domains ) && ( ! defined $default_domain ) ) {
			$panel->add_error( gettext('There is no default mail domain on this platform, you have to create a mail domain before creating a mailing list') );
		} else {
			my $template = new HTML::Template::Expr( filename => $templatedir.'/panel/mailinglist/create.tmpl', global_vars => 1 );

			$template->param( LIST_TEXT => gettext('Mail for the list') );
			$template->param( GROUP_NAME => gettext('Group owning this mailing list') );

			$template->param( DOMAINS => $domains );
			$template->param( GROUP => $group->get_groupname );
			$template->param( SEND => gettext('Send') );
			$template->param( MOTIVATION => gettext('Tell us what the use of this mailing list will be') );

			$template->param( LOCALPART_VALUE => $localpart );
			$template->param( DOMAIN_VALUE => $domain );
			$template->param( DESCRIPTION_VALUE => $description );

			$panel->build( $template );
			$panel->display;
		}
	}
}
