#
# indirect: generate direct/indirect delivery rules for SMTP
#
# (this file must be included after "general")

REVISION_ID='@(#)indirect	3.21 (motonori/WIDE) 24 Jun 1997'

if [ "$REVISION_ID_LIST" ]
then
        REVISION_ID_LIST="$REVISION_ID_LIST\\
# $REVISION_ID"
else
        REVISION_ID_LIST="# $REVISION_ID"
fi

#|# for indirect delivery
#|# domain names to be deliverd directly (all/none/"domain names")
#|#DIRECT_DELIVER_DOMAINS=all
#|# mailer for direct delivery
#|#DIRECT_DELIVER_MAILER=smtp
#|# relay host for indirect delivery (in case of none or "domain names")
#|##DEFAULT_RELAY='smtp:relay.host.name'
#|##DEFAULT_RELAY='smtp:relay1.host.name.:relay2.host.name.'
#|## with the above definition, relay2 will be tried only if relay1 is down
#|##DEFAULT_RELAY='error:deliver'

: ${DIRECT_DELIVER_DOMAINS=all}
: ${DIRECT_DELIVER_MAILER=$M_SMTP}
if [ "$DIRECT_DELIVER_DOMAINS" != all -a "$DEFAULT_RELAY" = "" ]
then
	echo "indirect: DEFAULT_RELAY should be defined." 1>&2
fi

if [ "$DIRECT_DELIVER_DOMAINS" = all -a "$MX_SENDMAIL" = no ]
then
	echo 'indirect: DIRECT_DELIVER_DOMAINS should not be "all" for NOMX sendmails.' 1>&2
fi

case "$DIRECT_DELIVER_DOMAINS" in
all) ;;	# direct delivery
none)	# indirect delivery
	# allocate macro character
	_ALLOC_MODE=normal
	_ALLOC_TYPE=macro
	_ALLOC_DIR=down
	_ALLOC_PREF=R
	_ALLOC_USAGE='default gateway [indirect]'
	. $MASTERDIR/$CHARALLOC
	_IR_CHAR=$_ALLOC_CHAR
	;;
*)	# partially indirect delivery
	# allocate class/macro character pair
	_ALLOC_MODE=normal
	_ALLOC_TYPE=pair
	_ALLOC_DIR=down
	_ALLOC_PREF=R
	_ALLOC_USAGE='default gateway/domain for direct delivery [indirect]'
	. $MASTERDIR/$CHARALLOC
	_IR_CHAR=$_ALLOC_CHAR
	_DD_CHAR=$_ALLOC_CHAR
	;;
esac

case "$CF_FORMAT" in
V1|NMTC)

NO_SUPPORT_UUCP='R$*<@$-.UUCP>$*		$#error$: UUCP addressing is not supported'
	;;

V5)

NO_SUPPORT_UUCP='R$*<@$-.UUCP>$*		$#error$@UNAVAILABLE$: "552 UUCP addressing is not supported"'
	;;

*)

NO_SUPPORT_UUCP='R$*<@$-.UUCP>$*		$#error$@5.3.3$: UUCP addressing is not supported'
	;;

esac

case "$DIRECT_DELIVER_DOMAINS" in

	all)	# all traffic should be resolved with MX lookup
SMTPDELIVERRULE='\
# resolve SMTP traffic\
'"$NO_SUPPORT_UUCP"'\
R$*<@$*>$*		'"$DISPATCH"' <'"$DIRECT_DELIVER_MAILER"':$2>.$1<@$2>$3' ;;

	none)	# all traffic should be sent to relay host
		MACRO=${DEFAULT_RELAY+D$_IR_CHAR$DEFAULT_RELAY}
		if [ "$MACRO" = "" ]
		then
			MACRO='#D'$_IR_CHAR'smtp:relay.host.name'
		fi
HDRINDIRECTRELAY="\\
# default relay host\\
$MACRO"

SMTPDELIVERRULE='\
# send SMTP traffic to relay host\
R$*<@$*>$*		'"$DISPATCH"' <$'$_IR_CHAR'>.$1<@$2>$3' ;;

	/*|-*)	# deliver mails only for selected domains (file class)
		MACRO=${DEFAULT_RELAY+D$_IR_CHAR$DEFAULT_RELAY}
		if [ "$MACRO" = "" ]
		then
			MACRO='#D'$_IR_CHAR'smtp:relay.host.name'
		fi
		CLASS="F$_DD_CHAR$DIRECT_DELIVER_DOMAINS"
HDRINDIRECTRELAY="\\
# default relay host\\
$MACRO\\
# domains for direct delivery\\
$CLASS"

SMTPDELIVERRULE='\
# resolve SMTP traffic\
R$*<@$*$='$_DD_CHAR'>$*		'"$DISPATCH"' <'"$DIRECT_DELIVER_MAILER"':$2$3>.$1<@$2$3>$4\
R$*<@$*>$*		'"$DISPATCH"' <$'$_IR_CHAR'>.$1<@$2>$3' ;;

	*)	# deliver mails only for selected domains
		MACRO=${DEFAULT_RELAY+D$_IR_CHAR$DEFAULT_RELAY}
		if [ "$MACRO" = "" ]
		then
			MACRO='#D'$_IR_CHAR'smtp:relay.host.name'
		fi

		exec << EoF
$DIRECT_DELIVER_DOMAINS
EoF
		CLASS=`sed 's/^/C'$_DD_CHAR' /'`

HDRINDIRECTRELAY="\\
# default relay host\\
$MACRO\\
# domains for direct delivery\\
$CLASS"

SMTPDELIVERRULE='\
# resolve SMTP traffic\
R$*<@$*$='$_DD_CHAR'>$*		'"$DISPATCH"' <'"$DIRECT_DELIVER_MAILER"':$2$3>.$1<@$2$3>$4\
R$*<@$*>$*		'"$DISPATCH"' <$'$_IR_CHAR'>.$1<@$2>$3' ;;

esac
