#
# hub: generate rules to send mails for non-local users to hub host
#
# (this file must be included after "address")

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

#|# hub host
#|##HUB_HOST='hub.host.name'
#|# list of users who should be recognized as remote
#|##REMOTE_USERS=/etc/users.remote
#|# list of users who should be recognized as local (higher preference)
#|##NON_REMOTE_USERS=/etc/users.local
#|# relay local to hub host (yes/no)
#|#RELAY_LOCAL_TO_HUBHOST=no
# #|# alias map
# #|##ALIAS_DB='hash:/etc/aliases'
# #|##ALIAS_DB='dbm:/etc/aliases'
# #|##ALIAS_DB='nis:mail.aliases'
# #|##ALIAS_DB='nisplus:mail_aliases.org_dir'

if [ "$HUB_HOST" ]
then

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

	# allocate macro character
	_ALLOC_MODE=normal
	_ALLOC_TYPE=macro
	_ALLOC_DIR=down
	_ALLOC_PREF=
	_ALLOC_USAGE='hub host name [hub]'
	. $MASTERDIR/$CHARALLOC
	_HH_CHAR=$_ALLOC_CHAR

	MACRO=${HUB_HOST+D$_HH_CHAR$HUB_HOST}
	if [ "$MACRO" = "" ]
	then
		MACRO='#D'$_HH_CHAR'hub.host.name'
	fi

	if [ "$REMOTE_USERS" ]
	then
		# allocate class character
		_ALLOC_MODE=normal
		_ALLOC_TYPE=class
		_ALLOC_DIR=down
		_ALLOC_PREF=
		_ALLOC_USAGE='remote users [hub]'
		. $MASTERDIR/$CHARALLOC
		_RU_CHAR=$_ALLOC_CHAR
	fi
	case "$REMOTE_USERS" in
	"") ;;
	/*|-*)	# file definition
		CLASS_RU="F$_RU_CHAR$REMOTE_USERS" ;;
	*)	# definition in .cf
		CLASS_RU="C$_RU_CHAR $REMOTE_USERS" ;;
	esac
	if [ "$CLASS_RU" = "" ]
	then
		CLASS_RU='#F'$_RU_CHAR'/etc/users.remote'
	fi

	if [ "$NON_REMOTE_USERS" ]
	then
		# allocate class character
		_ALLOC_MODE=normal
		_ALLOC_TYPE=class
		_ALLOC_DIR=down
		_ALLOC_PREF=
		_ALLOC_USAGE='non remote users [hub]'
		. $MASTERDIR/$CHARALLOC
		_NR_CHAR=$_ALLOC_CHAR
	fi
	case "$NON_REMOTE_USERS" in
	"") ;;
	/*|-*)	# file definition
		CLASS_NR="F$_NR_CHAR$NON_REMOTE_USERS" ;;
	*)	# definition in .cf
		CLASS_NR="C$_NR_CHAR $NON_REMOTE_USERS" ;;
	esac

#|# exact address specification
#|#WITH_HUB_ADDR=no
#|#IGNORE_HUB_MX=no

	: ${WITH_HUB_ADDR=no}
	if [ "$WITH_HUB_ADDR" != no ]
	then
		HUBADDR='<@$'$_HH_CHAR'>'
	fi

	: ${IGNORE_HUB_MX=no}
	if [ "$IGNORE_HUB_MX" != no ]
	then
		HUBRELAY='relay:[$'$_HH_CHAR']'
		IGNOREMXNUMERIC='R<$*[[$+]]>$*		$:<$1[$2]>$3			avoid nesting'
	else
		HUBRELAY='relay:$'$_HH_CHAR
	fi

HDRHUBHOST="\\
# hub host\\
$MACRO"

	if [ "$REMOTE_USERS" ]
	then

		HUBHOSTRULE='# see if we have a hub'

HDRHUBHOST="$HDRHUBHOST\\
# users who should be recognized as remote\\
$CLASS_RU"

	fi

	if [ "$NON_REMOTE_USERS" ]
	then

HDRHUBHOST="$HDRHUBHOST\\
# users who should be recognized as local\\
$CLASS_NR"

HUBHOSTRULE="$HUBHOSTRULE"'\
R$='$_NR_CHAR'			$#local$:$1			local users'

	fi

	if [ "$REMOTE_USERS" ]
	then

HUBHOSTRULE="$HUBHOSTRULE"'\
R$='$_RU_CHAR'			'"$DISPATCH"' <'"$HUBRELAY"'>.$1'"$HUBADDR"'		to hub host'

	fi

fi

: ${RELAY_LOCAL_TO_HUBHOST=no}
if [ "$HUB_HOST" != "" -a "$RELAY_LOCAL_TO_HUBHOST" != no ]
then

LOCALHUBRETRYRULE='R$+			$:.$1				retrymarker'

#	if [ "$_LU_CHAR" = "" -a "$ALIAS_DB" = "" ]
	if [ "$_LU_CHAR" = "" ]
	then
		echo "hub: character is not allocated for local users." 1>&2
	fi

	if [ "$ALIAS_DB" ]
	then

		case "$ALIAS_DB" in
		hash:*) # newdb hash style alias lookup
			HANAME=`expr "$ALIAS_DB" : 'hash:\(.*\)'`
			HAMATCH='Khaliases hash -m -a@ '$HANAME
			HADBLOOKUP=yes
			;;
		dbm:*)  # dbm style alias lookup
			HANAME=`expr "$ALIAS_DB" : 'dbm:\(.*\)'`
			HAMATCH='Khaliases dbm -m -a@ '$HANAME
			HADBLOOKUP=yes
			;;
		nis:*)  # NIS style alias lookup
			HANAME=`expr "$ALIAS_DB" : 'nis:\(.*\)'`
			HAMATCH='Khaliases nis -m -a@ '$HANAME
			HADBLOOKUP=yes
			;;
		nisplus:*)  # NIS style alias lookup
			HANAME=`expr "$ALIAS_DB" : 'nisplus:\(.*\)'`
			HAMATCH='Khaliases nisplus -m -a@ '$HANAME
			HADBLOOKUP=yes
			;;
		*)    # unknown map
			echo 'hub: unknown map specified in ALIAS_DB.' 1>&2
			;;
		esac

HDRHUBHOST="$HDRHUBHOST"'\
# alias map
'"$HAMATCH"

	fi


S0LOCALHUBRULE='# prepend send-hub marker on locals\
R$+			$:.$1				send-hub marker\
R..$+			$:$1				remove with retrymarker\
R.$*$=@$*		$:$1$2$3			remove send-hub marker'

	if [ "$_LU_CHAR" ]
	then

S0LOCALHUBRULE="$S0LOCALHUBRULE"'\
R.$='$_LU_CHAR'			$:$1				remove send-hub marker'

	fi

	if [ "$HADBLOOKUP" = yes ]
	then

S0LOCALHUBRULE="$S0LOCALHUBRULE"'\
R.$+			$:.$(haliases $1 $)\
R.$+@			$:$1				if found in aliases'

	fi

S5LOCALHUBRULE='# send locals to hub host except prog/file/include job\
R.|$+			$:|$1				prog mailer\
R./$+			$:/$1				file mailer\
R.:$+			$::$1				include mailer\
R.$+			'"$DISPATCH"' <'"$HUBRELAY"'>.$1'"$HUBADDR"'		send to hubhost'

fi

#|# make local sticky (yes/no)
#|#LOCAL_STICKY=no

: ${LOCAL_STICKY=no}
if [ "$LOCAL_STICKY" != no ]
then

STICKYRULE='\
# local sticky delivery\
R$-<@$=w>		$#local$:$1\
R$-.$-<@$=w>		$#local$:$1.$2'

fi
