#
# acceptaddr: generate rules for domain master
#
# (this file must be included after "canon")

REVISION_ID='@(#)acceptaddr	3.23 (motonori/WIDE) 19 Jul 1998'

#|# addresses which should be accepted as local
#|##ACCEPT_ADDRS='$m'	# can be used with R8 sendmail
#|##ACCEPT_ADDRS=$MY_DOMAIN
#|##ACCEPT_ADDRS='accept.domain.name1 accept.domain.name2 ...'
#|# use /etc/sendmail.aa file (yes/optional/no)
#|# ("optional" is for R8 only)
#|#USE_aa_FILECLASS=no
#|#aa_FILE_PATH='/etc/sendmail.aa'
#|# accept mails for all hosts within my domain
#|# (yes/no/"a specific domain with leading dot"; default for "yes" is .$m)
#|#ACCEPT_LOWER=no
#|# lower name matching style (lw-file/!lw-file/any/one)
#|#LOWER_MATCH_STYLE=any
#|#lw_FILE_PATH='/etc/sendmail.lw'
#|# if LOWER_NAMES is defined, it is used instead of lw_FILE
#|##LOWER_NAMES='lower1 lower2 lower3'
#|# consider local if the best MX for an address points to myself (yes/no)
#|# do not set "yes" if TryNullMXList(Ow) trick is used. (V6 or later)
#|#ACCEPT_BESTMX=no
#|# domains to be considered for best-mx match (all/"domain names")
#|#BESTMX_DOMAINS=all

: ${USE_aa_FILECLASS=no}
: ${ACCEPT_LOWER=no}
if [ "$ACCEPT_ADDRS" != "" -o "$USE_aa_FILECLASS" != no -o "$ACCEPT_LOWER" != no ]
then

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

	if [ "$CANON_ENVELOPE" = "" ]
	then
	    echo "acceptaddr: no default definition for CANON_ENVELOPE." 1>&2
	fi

fi

if [ "$ACCEPT_ADDRS" != "" -o "$USE_aa_FILECLASS" != no ]
then

	# allocate class character
	_ALLOC_MODE=normal
	_ALLOC_TYPE=class
	_ALLOC_DIR=down
	_ALLOC_PREF=
	_ALLOC_USAGE='accepting address [acceptaddr]'
	. $MASTERDIR/$CHARALLOC
	_AA_CHAR=$_ALLOC_CHAR

	# define a class to use /etc/sendmail.aa
	: ${aa_FILE_PATH='/etc/sendmail.aa'}
	case "$USE_aa_FILECLASS" in
	no)		FILECLASS='#F'"$_AA_CHAR$aa_FILE_PATH" ;;
	# options is R8 only
	optional)	FILECLASS='F'$_AA_CHAR' -o '"$aa_FILE_PATH" ;;
	*)		FILECLASS='F'"$_AA_CHAR$aa_FILE_PATH" ;;
	esac

	if [ "$ACCEPT_ADDRS" ]
	then

		exec << EoF
$ACCEPT_ADDRS
EoF
		CLASS=`sed 's/^/C'$_AA_CHAR' /'`

	else

		CLASS='#C'$_AA_CHAR' accept.domain.name'

	fi

HDRACCEPTADDR="\\
# address which should be accepted\\
$FILECLASS\\
$CLASS"

	if [ "$CF_FORMAT" = V1 ]
	then

S4ACCEPTADDR='\
R$+%$=w@$='$_AA_CHAR'		$1@$2				u%host@dom => u@host\
R$+%$='$_AA_CHAR'@$=w		$1@$2				u%dom@host => u@dom\
R$+%$='$_AA_CHAR'@$='$_AA_CHAR'		$1@$2				u%dom@dom => u@dom'

	else

S4ACCEPTADDR='\
R$+%$=w@$='$_AA_CHAR'		$1@$j				u%host@dom => u@host\
R$+%$='$_AA_CHAR'@$=w		$1@$2				u%dom@host => u@dom\
R$+%$='$_AA_CHAR'@$='$_AA_CHAR'		$1@$2				u%dom@dom => u@dom'

DSPTACCEPTRULE='R<$='$_AA_CHAR'>.$*<$*>$*	$@$>'"$R_S0RETRY"' $2$3$4			delete local host\
R<$-:$='$_AA_CHAR'>.$*<$*>$*	$@$>'"$R_S0RETRY"' $3$4$5			delete local host'

	fi

S0ACCEPTADDR='\
R<@$='$_AA_CHAR'>:$*		$@$>'"$R_S0RETRY"' $2			@here:... -> ...\
R$*<@$='$_AA_CHAR'>		$@$>'"$R_S0RETRY"' $1			...@here -> ...'

	if [ "$CANON_ENVELOPE" = no ]
	then

CANONACCEPTADDR='# acceptable domains are OK\
R$*<@$='$_AA_CHAR'>$*		$@$1<@$2>$3'

	else

CANONACCEPTADDR='# acceptable domains are OK\
R$*<@$='$_AA_CHAR'>$*		$@$1<@$2.>$3'

	fi

fi

if [ "$ACCEPT_LOWER" != no ]
then

	case "$LOWER_MATCH_STYLE" in
	*lw-file)
		# allocate class character
		_ALLOC_MODE=normal
		_ALLOC_TYPE=class
		_ALLOC_DIR=down
		_ALLOC_PREF=
		_ALLOC_USAGE='lower matching hosts [acceptaddr]'
		. $MASTERDIR/$CHARALLOC
		AL_CHAR=$_ALLOC_CHAR

		if [ "$LOWER_NAMES" ]
		then

HDRACCEPTLOWER="\\
# lower names which should be accepted\\
C$AL_CHAR $LOWER_NAMES"

		else

			: ${lw_FILE_PATH='/etc/sendmail.lw'}
HDRACCEPTLOWER="\\
# lower names which should be accepted\\
F$AL_CHAR$lw_FILE_PATH"

		fi ;;
	esac

	: ${LOWER_MATCH_STYLE=any}
	case "$LOWER_MATCH_STYLE" in
	lw-file)
		RCVLWMATCH='$='$AL_CHAR ;;
	!lw-file)
		RCVLWMATCH='$='$AL_CHAR ;;
	one)
		RCVLWMATCH='$-' ;;
	*)
		RCVLWMATCH='$*' ;;
	esac

	if [ "$ACCEPT_LOWER" = yes ]
	then
		RCVLWMYDOM='.$m'
	else
		RCVLWMYDOM="$ACCEPT_LOWER"
	fi

	case "$LOWER_MATCH_STYLE" in
	!lw-file)

S0ACCEPTLOWER='\
R<@'"$RCVLWMATCH$RCVLWMYDOM"'>:$*		$:<@$1'"$RCVLWMYDOM"'@>:$2\
R$*<@'"$RCVLWMATCH$RCVLWMYDOM"'>		$:$1<@$2'"$RCVLWMYDOM"'@>\
R<@$*'"$RCVLWMYDOM"'>:$*		$@$>'"$R_S0RETRY"' $2			@here:... -> ...\
R$*<@$*'"$RCVLWMYDOM"'>		$@$>'"$R_S0RETRY"' $1			...@here -> ...\
R<@$*'"$RCVLWMYDOM"'@>:$*		$:<@$1'"$RCVLWMYDOM"'>:$2\
R$*<@$*'"$RCVLWMYDOM"'@>		$:$1<@$2'"$RCVLWMYDOM"'>'

		;;

	*)

S0ACCEPTLOWER='\
R<@'"$RCVLWMATCH$RCVLWMYDOM"'>:$*		$@$>'"$R_S0RETRY"' $2			@here:... -> ...\
R$*<@'"$RCVLWMATCH$RCVLWMYDOM"'>		$@$>'"$R_S0RETRY"' $1			...@here -> ...'

		if [ "$CF_FORMAT" != V1 ]
		then

DSPTACCEPTLOWER='R<'"$RCVLWMATCH$RCVLWMYDOM"'>.$*<$*>$*	$@$>'"$R_S0RETRY"' $2$3$4			delete local host\
R<$-:'"$RCVLWMATCH$RCVLWMYDOM"'>.$*<$*>$*	$@$>'"$R_S0RETRY"' $3$4$5			delete local host'

		fi

		if [ "$CANON_ENVELOPE" = no ]
		then

CANONACCEPTLOWER='# lowers are OK\
R$*<@'"$RCVLWMATCH$RCVLWMYDOM"'>$*		$@$1<@$2'"$RCVLWMYDOM"'>$3'

		else

CANONACCEPTLOWER='# lowers are OK\
R$*<@'"$RCVLWMATCH$RCVLWMYDOM"'>$*		$@$1<@$2'"$RCVLWMYDOM"'.>$3'

		fi

		;;

	esac
fi

: ${ACCEPT_BESTMX=no}
: ${BESTMX_DOMAINS=all}
if [ "$ACCEPT_BESTMX" != no ]
then
	if [ "$CF_FORMAT" = V1 -o "$CF_FORMAT" = V5 ]
	then
		echo "acceptaddr: ACCEPT_BESTMX can be used with v6 or later." 1>&2
	else
		HDRBESTMX='Kbestmx bestmx'

		if [ "$BESTMX_DOMAINS" != all ]
		then
			# allocate class character
			_ALLOC_MODE=normal
			_ALLOC_TYPE=class
			_ALLOC_DIR=down
			_ALLOC_PREF=
			_ALLOC_USAGE='bestmx matching domains [acceptaddr]'
			. $MASTERDIR/$CHARALLOC
			_BM_CHAR=$_ALLOC_CHAR

HDRBESTMX="$HDRBESTMX\\
# domains for bestmx matching\\
C$_BM_CHAR $BESTMX_DOMAINS"

S0BESTMX='# limit bestmx\
R$*<@$*$='$_BM_CHAR'>$*		$:$1<@$2$3@@$(bestmx $2$3 $)>$4'

		else

S0BESTMX='# unlimited bestmx\
R$*<@$*>$*		$:$1<@$2@@$(bestmx $2 $)>$3'

		fi

S0BESTMX="$S0BESTMX"'\
# name in MXRRs should be official name or my alias\
R<@$*@@$=w.>:$*		$@$>'"$R_S0RETRY"' $2\
R$*<@$*@@$=w.>		$@$>'"$R_S0RETRY"' $1\
R$*<@$*@@$*>$*		$:$1<@$2>$4'

	fi
fi
