#!/usr/local/bin/perl
#
# postfix-module by Guillaume Cottenceau <gc@mandrakesoft.com>,
# for webmin by Jamie Cameron
#
# Copyright (c) 2000 by Mandrakesoft
#
# Permission to use, copy, modify, and distribute this software and its
# documentation under the terms of the GNU General Public License is hereby 
# granted. No representations are made about the suitability of this software 
# for any purpose. It is provided "as is" without express or implied warranty.
# See the GNU General Public License for more details.
#
#
# Save, modify, delete a map for Postfix


require './postfix-lib.pl';

&ReadParse();


#     &ui_print_header(undef, $text{'aliases_title'}, "");

&error_setup($text{'map_save_err'});


my $maps = &get_maps($in{'map_name'});
my $add = 1; my %map;

foreach $trans (@{$maps})
{
    if ($trans->{'number'} == $in{'num'}) { $add = 0; %map = %{$trans}; }
}



my @maps_files = &get_maps_files(&get_current_value($in{'map_name'}));

defined($maps_files[0]) || &error($text{'mapps_no_map_file'});

if ($in{'delete'})
{
    if ($add == 1)
    {
	$whatfailed = "";
	&error(&text('map_delete_failed', $text{'map_delete_create'}));
    }
    &delete_mapping($in{'map_name'}, \%map);
    # XXX rest of file
    # XXX test!
}
elsif ($add == 0)
{
    # modify an existing map
    local %newmap = ( 'name' => $in{'name'},
		      'value' => $in{'value'} );
    &modify_mapping($in{'map_name'}, \%map, \%newmap);
}
else
{
    # add a new map -- much more easy! :-)
    local %newmap = ( 'name' => $in{'name'},
		      'value' => $in{'value'} );
    &create_mapping($in{'map_name'}, \%newmap);
}
    


# re-creates aliases database

if ($in{'map_name'} =~ /canonical/) { &regenerate_canonical_table(); }
if ($in{'map_name'} =~ /relocated/) { &regenerate_relocated_table(); }
if ($in{'map_name'} =~ /virtual/) { &regenerate_virtual_table(); }
if ($in{'map_name'} =~ /transport/) { &regenerate_transport_table(); }

&reload_postfix();

if ($in{'map_name'} =~ /transport/) { &redirect("transport.cgi"); }
elsif ($in{'map_name'} =~ /canonical/) { &redirect("canonical.cgi"); }
elsif ($in{'map_name'} =~ /virtual/) { &redirect("virtual.cgi"); }
elsif ($in{'map_name'} =~ /relocated/) { &redirect("relocated.cgi"); }
else { &redirect(""); }

