#!/usr/local/bin/perl
# list_aliases.cgi
# Display users and aliases in a domain

require './virtual-server-lib.pl';
&ReadParse();
$d = &get_domain($in{'dom'});
&can_edit_domain($d) && &can_edit_aliases() || &error($text{'aliases_ecannot'});
@aliases = &list_domain_aliases($d, 1);
&ui_print_header(&domain_in($d), $text{'aliases_title'}, "");

($mleft, $mreason, $mmax) = &count_feature("aliases");
$addprog = $virtualmin_pro ? "edit_simple.cgi" : "edit_alias.cgi";
if (@aliases) {
	print &ui_form_start("delete_aliases.cgi");
	print &ui_hidden("dom", $in{'dom'}),"\n";
	if ($mleft != 0 && $mleft != -1) {
		print "<b>",&text('aliases_canadd'.$mreason, $mleft),"</b><p>\n";
		}
	elsif ($mleft == 0) {
		print "<b>",&text('aliases_noadd'.$mreason, $mmax),"</b><p>\n";
		}
	print &select_all_link("d"),"\n";
	print &select_invert_link("d"),"\n";
	if ($mleft != 0) {
		print "<a href='$addprog?new=1&dom=$in{'dom'}'>",
		      "$text{'aliases_add'}</a><br>\n";
		}
	if ($can_alias_comments) {
		($anycmt) = grep { $_->{'cmt'} } @aliases;
		}
	print &ui_columns_start([ "", $text{'aliases_name'},
				      $text{'aliases_domain'},
				      $text{'aliases_dests'},
				      $anycmt ? ( $text{'aliases_cmt'} ) : ( )
				], 100, 0,
				[ "width=5" ]);
	foreach $a (sort { $a->{'from'} cmp $b->{'from'} } @aliases) {
		$name = $a->{'from'};
		$name =~ s/\@\S+$//;
		$name = "<i>$text{'alias_catchall'}</i>" if ($name eq "");
		$alines = "";
		$simple = $virtualmin_pro ? &get_simple_alias($d, $a) : undef;
		foreach $v (@{$a->{'to'}}) {
			($anum, $astr) = &alias_type($v);
			if ($anum == 5 && $simple) {
				$msg = $simple->{'autotext'};
				$msg = substr($msg, 0, 100)." ..."
					if (length($msg) > 100);
				$alines .= &text('aliases_reply',
					"<i>".&html_escape($msg)."</i>");
				}
			else {
				$alines .= &text("aliases_type$anum",
				   "<tt>".&html_escape($astr)."</tt>")."<br>\n";
				}
			}
		if (!@{$a->{'to'}}) {
			$alines = "<i>$text{'aliases_dnone'}</i>\n";
			}
		$prog = $simple ? "edit_simple.cgi" : "edit_alias.cgi";
		print &ui_checked_columns_row([
			"<a href='$prog?dom=$in{'dom'}&".
			"alias=$a->{'from'}'>$name</a>",
			$d->{'dom'},
			$alines,
			$anycmt ? ( $a->{'cmt'} ) : ( ) ],
			[ "width=5", "valign=top", "valign=top" ],
			"d", $a->{'from'});
		}
	print &ui_columns_end();
	}
else {
	print "<b>$text{'aliases_none'}</b><p>\n";
	}
print &select_all_link("d"),"\n";
print &select_invert_link("d"),"\n";
if ($mleft != 0) {
	print "<a href='$addprog?new=1&dom=$in{'dom'}'>",
	      "$text{'aliases_add'}</a><br>\n";
	}
if (@aliases) {
	print &ui_form_end([ [ "delete", $text{'aliases_delete'} ] ]);
	}

if ($single_domain_mode) {
	&ui_print_footer(&domain_footer_link($d),
			 "", $text{'index_return2'});
	}
else {
	&ui_print_footer(&domain_footer_link($d),
		"", $text{'index_return'});
	}

