#
# indirect: generate direct/indirect delivery rules for SMTP
#

REVISION_ID='@(#)indirect	3.7 (motonori/WIDE) 2/26/94'

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
#|# relay host for indirect delivery (in case of none or "domain names")
#|##DEFAULT_RELAY='smtp:relay.host.name'

: ${DIRECT_DELIVER_DOMAINS=all}
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/direct deliver domains [indirect]'
	. $MASTERDIR/$CHARALLOC
	_IR_CHAR=$_ALLOC_CHAR
	_DD_CHAR=$_ALLOC_CHAR
	;;
esac

case "$DIRECT_DELIVER_DOMAINS" in

	all)	# all traffic should be resolved with MX lookup
SMTPDELIVERRULE='\
# resolve SMTP traffic\
R$*<@$*>$*		'"$DISPATCH"' <'"$M_SMTP"':$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\\
# direct deliver domains\\
$CLASS"

SMTPDELIVERRULE='\
# resolve SMTP traffic\
R$*<@$*$='$_DD_CHAR'>$*		'"$DISPATCH"' <'"$M_SMTP"':$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\\
# direct deliver domains\\
$CLASS"

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

esac


# ### rules for delivery with NIS host name lookup ###
#
# #|# resolv traffic with NIS hostname lookup (yes/no)
# #|# (This feature is only for sendmails suplied from Sun Micro.)
# #|WITH_NIS_LOOKUP=no
#
# : ${WITH_NIS_LOOKUP=no}
# if [ "$WITH_NIS_LOOKUP" != no ]
# then
# NISDELIVERRULE="\\
# # resolve SMTP traffic with NIS host name lookup\\
# R\$*<@\$%y>\$*		$DISPATCH <$M_SMTP:\$2>:\$1<@\$2>\$3\\
# R\$*<@\$%y.\$m>\$*		$DISPATCH <$M_SMTP:\$2>:$1<@\$2.\$m>\$3"
# fi
