{
    # vim: ft=perl:
    use esmith::config;
    use esmith::db;

    $OUT = "";

    my $local_delivery = "";
    my $dt = $qmail{DeliveryType} || '';
    my $preprocessing = "";
    if ($dt eq 'program')
    {
        $preprocessing .= '| ';
    }
    my $default = './Maildir/';
    $preprocessing .= $qmail{DeliveryInstruction} || '';

    $OUT = "$preprocessing\n" if $preprocessing;

    # Administrative mail will be redirected to the address specified
    # by $AdminEmail. We need to make sure that this address does NOT
    # result in a mail loop.
    #
    # Therefore, we need to see if the domain portion of the address is
    # a domain controlled by the server, and if so, is the user portion
    # of the address as system account. If both of these conditions
    # are true, then mail for admin is being redirected to admin which
    # is definitely a mail loop. In this case we simply tell qmail to
    # deliver to the admin users ./Maildir/.
    #
    # If one (or both) of these conditions are false, then we simply
    # accept the address with no further checking.

    unless (defined $AdminEmail)
    {
        # Undefined - must be a local delivery

        $OUT .= $default;
        return;
    }

    if ($AdminEmail =~ /^\s*$/)
    {
        # Blank - must be a local delivery

        $OUT .= $default;
        return;
    }

    # Get the user and domain components of the address.

    my (@addr) = split(/\@/, $AdminEmail);

    my $user_component = "";
    my $domain_component = "";

    if (scalar @addr > 2)
    {
        # More than one '@' char in address

        $domain_component = pop @addr;
        $user_component = join('\@', @addr);
    } elsif (scalar @addr == 2)
    {
        $domain_component = pop @addr;
        $user_component = pop @addr;
    }
    else
    {
        $user_component = pop @addr;
    }

    my %accounts;
    tie %accounts, 'esmith::config', '/home/e-smith/accounts';

    # Is the user component a system account?

    my $account = db_get_type(\%accounts, $user_component);

    if (defined $account && $account eq "system")
    {
        # User component is a system account. Does it match one of
        # our domains?

        unless ($domain_component)
        {
            # NO domain component. This will ALWAYS be a local domain.

            $OUT .= $default;
            return;
        }

        if ($domain_component eq $DomainName)
        {
            $OUT .= $default;
            return;
        }

        # Loop through the domains and hosts

        my %domains;
        tie %domains, 'esmith::config', '/home/e-smith/domains';

        my %hosts;
        tie %hosts, 'esmith::config', '/home/e-smith/hosts';

        my $host;
        my $domain;

        # Handle each defined virtual domain

        foreach $domain (sort keys %domains)
        {
            next unless db_get_type(\%domains, $domain) eq "domain";

            if ($domain_component eq $domain)
            {
                $OUT .= $default;
                return;
            }
        }

        # Handle each defined host

        foreach $host (sort keys %hosts)
        {
            my $ip = (defined db_get_prop(\%hosts, $host, 'ExternalIP')) ?
                      db_get_prop (\%hosts, $host, 'ExternalIP') : '';

            my $hosttype = db_get_prop (\%hosts, $host, 'HostType');

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

                my ($tempHost, $tempDomain) = split /\./, $host, 2;

                if ($domain_component eq $tempDomain)
                {
                    if ($hosttype eq 'Self' || $ip eq $ExternalIP)
                    {
                        $OUT .= $default;
                        return;
                    }
                    else
                    {
                        $OUT .= '&' . $AdminEmail;
                        return;
                    }
                }
            }
            else         #FIXME - this should no longer be necessary
            {
                # Host is unqualified

                # Handle the host within the local domain and sub domain

                if ($domain_component eq "${host}.${DomainName}")
                {
                    if ($hosttype eq 'Self' || $ip eq $ExternalIP)
                    {
                        $OUT .= $default;
                        return;
                    }
                    else
                    {
                        $OUT .= '&' . $AdminEmail;
                        return;
                    }
                }

                # Handle the host for all of the virtual domains

                foreach $domain (sort keys %domains)
                {
                    next unless db_get_type(\%domains, $domain) eq "domain";

                    if ($domain_component eq "${host}.${domain}")
                    {
                        if ($hosttype eq 'Self' || $ip eq '$ExternalIP')
                        {
                            $OUT .= $default;
                            return;
                        }
                        else
                        {
                            $OUT .= '&' . $AdminEmail;
                            return;
                        }
                    }
                }
            }
        }

        # Catch all. If nothing matched above, it should be safe to
        # use the supplied address.

        $OUT .= '&' . $AdminEmail;
        return;
    }
    else
    {
        # User component is not a system account, so we can happily use
        # the address, regardless of the domain component.

        $OUT .= '&' . $AdminEmail;
        return;
    }
}
