#!/usr/local/bin/perl
# save_mailer.cgi
# Save, create or delete a mailertable entry

require './sendmail-lib.pl';
require './mailers-lib.pl';
&ReadParse();
$access{'mailers'} || &error($text{'msave_ecannot'});
$conf = &get_sendmailcf();
$mfile = &mailers_file($conf);
&lock_file($mfile);
($mdbm,$mdbmtype) = &mailers_dbm($conf);
@mailers = &list_mailers($mfile);
foreach $ml (@mailers) { $already{$ml->{'domain'}}++; }
if (!$in{'new'}) { $m = $mailers[$in{'num'}]; }

if ($in{'delete'}) {
	# delete some mailer entry
	$logm = $m;
	&delete_mailer($m, $mfile, $mdbm, $mdbmtype);
	}
else {
	# Saving or creating.. check inputs
	&error_setup($text{'msave_err'});
	$domain = $in{'from_type'} ? ".$in{'from_dom'}" : $in{'from_host'};
	$domain =~ /^[A-z0-9\-\.]+$/ ||
		&error(&text('msave_edomain', $domain));
	if ($already{$domain} && ($in{'new'} || $m->{'domain'} ne $domain)) {
		&error(&text('msave_edup', $domain));
		}
	$mailer = $in{'mailer'};
	$dest = $in{'dest'};

	$newm{'domain'} = $domain;
	$newm{'mailer'} = $mailer;
	$newm{'dest'} = $in{'nomx'} && $mailer =~ /smtp|relay/ ? "[$dest]"
							       : $dest;
	if ($in{'new'}) { &create_mailer(\%newm, $mfile, $mdbm, $mdbmtype); }
	else { &modify_mailer($m, \%newm, $mfile, $mdbm, $mdbmtype); }
	$logm = \%newm;
	}
&unlock_file($mfile);
&webmin_log($in{'delete'} ? 'delete' : $in{'new'} ? 'create' : 'modify',
	    "mailer", $logm->{'domain'}, $logm);
&redirect("list_mailers.cgi");

