#!/usr/local/bin/perl
# edit_alias.cgi
# Display a form for editing or adding a mail alias

require './virtual-server-lib.pl';
&ReadParse();
$d = &get_domain($in{'dom'});
&can_edit_domain($d) && &can_edit_aliases() || &error($text{'aliases_ecannot'});
&require_mail();
if ($in{'new'}) {
	&ui_print_header(&domain_in($d), $text{'alias_create'}, "");
	}
else {
	&ui_print_header(&domain_in($d), $text{'alias_edit'}, "");
	@aliases = &list_domain_aliases($d);
	($virt) = grep { $_->{'from'} eq $in{'alias'} } @aliases;
	}

print &check_clicks_function();
print "<form action=save_alias.cgi>\n";
print "<input type=hidden name=new value='$in{'new'}'>\n";
print "<input type=hidden name=dom value='$in{'dom'}'>\n";
print "<input type=hidden name=old value='$in{'alias'}'>\n";

if ($virtualmin_pro && ($in{'new'} || &get_simple_alias($d, $virt))) {
	$complex = "<a href='edit_simple.cgi?dom=$in{'dom'}&".
		 "alias=$in{'alias'}&new=$in{'new'}'>$text{'alias_simple'}</a>";
	}
print "<table border>\n";
print "<tr $tb> <td><b>$text{'alias_header'} $complex</b></td> </tr>\n";
print "<tr $cb> <td><table>\n";

if ($can_alias_comments) {
	print "<tr> <td>",&hlink("<b>$text{'alias_cmt'}</b>",
				 "aliascmt"),"</td>\n";
	print "<td>",&ui_textbox("cmt", $virt->{'cmt'}, 50),"</td> </tr>\n";
	}

$name = $virt->{'from'};
$name =~ s/\@\S+$//;
print "<tr> <td>",&hlink("<b>$text{'alias_name'}</b>", "aliasname"),"</td>\n";
print "<td>",&ui_radio("name_def", $name eq "" && !$in{'new'} ? 1 : 0,
		       [ [ 1, $text{'alias_catchall'} ],
			 [ 0, $text{'alias_mailbox'} ] ]),"\n",
	     &ui_textbox("name", $name, 20),"</td> </tr>\n";

&alias_form($virt->{'to'}, &hlink("<b>$text{'alias_dests'}</b>", "aliasdest"),
	    $d, "alias", $in{'alias'});

print "</table></td></tr></table>\n";

print "<table><tr>\n";
if ($in{'new'}) {
	print "<td><input type=submit value='$text{'create'}'></td>\n";
	}
else {
	print "<td><input type=submit value='$text{'save'}'></td>\n";
	print "<td><input type=submit name=delete ",
	      "value='$text{'delete'}' onClick='check_clicks(form)'></td>\n";
	}
print "</table></form>\n";

if ($single_domain_mode) {
	&ui_print_footer("list_aliases.cgi?dom=$in{'dom'}",
		$text{'aliases_return'},
		"", $text{'index_return2'});
	}
else {
	&ui_print_footer("list_aliases.cgi?dom=$in{'dom'}",
		$text{'aliases_return'},
		&domain_footer_link($d),
		"", $text{'index_return'});
	}


