{
    my $dms = $DelegateMailServer;

    return "# DelegateMailServer is set" if ($dms && ($dms !~ /^\s*$/));

    # All virtual domains will be delivered locally according to the
    # instructions in /var/qmail/alias/.qmail-localdelivery-default.

    my $local_handler = "alias-localdelivery";

    use esmith::HostsDB;
    use esmith::DomainsDB;
    my $hostsdb = esmith::HostsDB->open;
    my $domainsdb = esmith::DomainsDB->open;

    # Handle each defined virtual domain
    foreach my $domain ($domainsdb->get_all_by_prop(type => "domain"))
    {
	$domain = $domain->key;
	$OUT .= "$domain:$local_handler\n";
    }

    # Handle each defined host
    foreach my $host ($hostsdb->get_all)
    {
	# Ignore this host if its external IP address is not the same
	# as the local machine.

    	my $hosttype = $host->prop('HostType');
	next unless (defined $hosttype);

	my $ip = $host->prop('ExternalIP') || "";
	next unless (
		$hosttype eq 'Self'
		||
		($ExternalIP && $ip eq $ExternalIP)
	    );

	$host = $host->key;
	if ($host =~ /\./)
	{
	    # Host is fully qualified

	    $OUT .= "$host:$local_handler\n";
	}
	else
	{
	    # Host is unqualified

	    # Handle the host within the local domain and sub domain

	    $OUT .= "${host}.${DomainName}:$local_handler\n";

	    # Handle the host for all of the virtual domains

	    foreach my $domain ($domainsdb->get_all_by_prop(type => "domain"))
	    {
		$domain = $domain->key;
		$OUT .= "${host}.${domain}:$local_handler\n";
	    }
	}
    }
    chomp($OUT);
}
