# FQDN-alias override
#
# This is the simple interface to the db/fqdnalias file in classical
# aliasing style.
#
# We check in order following:
#   - address existing (in lower-case) in the  fqdnaliases  database
#   - address domain-part existing (@domain) in the  fqdnaliases  database
#   - address existing (in lower-case) in the MAILVAR/fqlists/ -directory
#     in similar manner to those in  MAILVAR/lists/  at the  aliases.cf
#   - owner-*, *-owner, and *-request -relation of the MAILVAR/fqlists/
#     likewise
#
# We do not support '+'-selectors in similar manner to those that aliases
# processing does!
#

# FQDN-aliases initializations
#
# Relations: fqdnalias

provide fqdnaliasdb

#
#  FQDN-aliases, in autoconfigured dbase type
#

if [ -f $MAILVAR/db/fqdnaliases.zmsh ]; then
	. $MAILVAR/db/fqdnaliases.zmsh
else
    if [ -f $MAILVAR/db/fqdnaliases ]; then
	# Need to be updated at boot ?
	if [ ! -f $MAILVAR/db/fqdnaliases$DBEXTtest -o \
	     $MAILVAR/db/fqdnaliases -nt $MAILVAR/db/fqdnaliases$DBEXTtest ]; then
		# Yes, so update!
		$MAILBIN/zmailer newfqdnaliases
	fi
	relation -lmt $DBTYPE -f $MAILVAR/db/fqdnaliases$DBEXT fqdnaliases_1
	fqdnaliases (key, o1, o2, o3, o4, o5, o6, o7, o8, o9) {
	  local a
	  if a="$(fqdnaliases_1 $key $o1 $o2 $o3 $o4 $o5 $o6 $o7 $o8 $o9)"; then
	    priv="$(filepriv -M 644 $MAILVAR/db/fqdnaliases$DBEXTtest \
			$(db owner fqdnaliases_1))" &&
	    return "$a"
	  fi
	  return 1
	}
    else
	fqdnaliases () { return 1 }
    fi
fi

#
# Define an LDAP hook, just in case...
#

if [ -f $MAILVAR/db/fqdnaliasesldap.zmsh ]; then
	. $MAILVAR/db/fqdnaliasesldap.zmsh
else
    if [ -f $MAILVAR/db/fqdnalias.ldap ]; then
	relation -lmt ldap -s 9999 -e 2000 \
		 -f $MAILVAR/db/fqdnalias.ldap fqdnaliasesldap
    else
	fqdnaliasesldap () { return 1 }
    fi
fi

require fqdnaliasdb
provide fqdnalias

fqdn_neighbour (domain, address, A) {
	# "domain" is dummy..
	local lcaddress key l a priv nattr user athost
	local plustail

	lcaddress="$(recase -l "$address")"
	key="$lcaddress"

	plustail=""

	tsift "$lcaddress" in
	@(.*):(.+)@(.+)
		return 1   # Source route
		;;
	(.+)@(.+)
		user="\1"
		athost="@\2"
		break
		;;
	.*
		return 1 # Not in  uu@hh  form!
		;;
	tfist

	# It may be a "sender" address!

	case "$(get $A type)" in
	sender)
		if $(didexpand userdbmailname) && 
		   a="$(userdb "$lcaddress:mailname" "$user")" ; then

		  db add expansions "$key.userdbmailname" 1
		  nattr=$(newattribute $A privilege $priv)
		  $(zapDSNnotify $nattr) ## FIXME!FIXME!
		  a=$(echo "$a" |				\
		      listaddresses -c "$a sender fqdn userdb mailname" \
				    -e root			 |	\
		      maprrouter $nattr "sender-fqdn-userdb-mailname"	\
					"$address" "$plustail" "$athost")
		  return $a
		fi
		return 1
		;;
	esac


	# For a recipient address we may do a FQDN-aliasing
	# We are better NOT to have aliasing loops here, though
	# we do expansion recursion pruning by falling thru to
	# normal target address locality testing, and such...

	# Already expanded ?  If yes, go away!
	#if [ -n "$(expansions "$lcaddress")" ] ; then
	#	return 1
	#fi

	#   Yes, add to the expansions.  Success ?
	#     Yes, Any success with filepriv from the alias db ?
	#       Yes, generate new privilege attribute for it. Success ?
	#         Yes, do the expansion! (echo .. | listexpand ...)
	#

	# key="$lcaddress"
	if $(didexpand fqdnaliases) &&
	   a="$(fqdnaliases "$lcaddress" "$user")" ; then
		db add expansions "$key.fqdnaliases" 1
		nattr=$(newattribute $A privilege $priv)
		$(zapDSNnotify $nattr) ## FIXME!FIXME!
		a=$(echo "$a" |						\
		    listaddresses	-c "$a fqdn alias expansion" 	\
					-e root			 |	\
		    maprrouter $nattr "fqdnalias" "$address"		\
				"$plustail" "$athost")
		return $a
	fi

	if $(didexpand fqdnaliasesldap) &&
	   a="$(fqdnaliasesldap "$lcaddress" "$user")" ; then
		db add expansions "$key.fqdnaliasesldap" 1
		nattr=$(newattribute $A privilege $nobodypriv)
		$(zapDSNnotify $nattr) ## FIXME!FIXME!
		a=$(echo "$a" |						\
		    listaddresses	-c "$a fqdn ldap expansion" 	\
					-e root			 |	\
		    maprrouter $nattr "fqdnalias" "$address"		\
				"$plustail" "$athost")
		return $a
	fi

	# Didn't match,

	# Plusfull usernames ??

	ssift "$user" in
	([^+]+)\+(.*)
	    local u2 pl2
	    u2="\1"
	    pl2="+\2"
	    key="$u2$athost"
	    if $(didexpand fqdnaliases) &&
	       a="$(fqdnaliases "$key")" ; then
		db add expansions "$key.fqdnaliases" 1
		nattr=$(newattribute $A privilege $priv)
		$(zapDSNnotify $nattr) ## FIXME!FIXME!
		a=$(echo "$a" |						\
		    listaddresses	-c "$a fqdn+ alias expansion" 	\
					-e root			 |	\
		    maprrouter $nattr "fqdnalias" "$address"	\
				"$pl2" "$athost")
		return $a

	    fi
	    if $(didexpand fqdnaliasesldap) &&
	       a="$(fqdnaliasesldap "$key" "$user")" ; then
		db add expansions "$key.fqdnaliasesldap" 1
		nattr=$(newattribute $A privilege $nobodypriv)
		$(zapDSNnotify $nattr) ## FIXME!FIXME!
		a=$(echo "$a" |						\
			 listaddresses	-c "$a fqdn+ ldap expansion" 	\
					-e root			 |	\
			 maprrouter $nattr "fqdnalias" "$address"	\
				"$plustail" "$athost")
		return $a
	    fi
	    ;;
	tfiss

	# Didn't match,

	priv=$(get $A privilege)

	# Do we have a file with that name ?
	a="$MAILVAR/fqlists/$lcaddress"
	key="$lcaddress"
	l="$preowner$user$postowner$athost"
	nattr=()
	if [ -f "$a" ] && $(didexpand fqlists); then
		db add expansions "$key.fqlists" 1
		priv=$(getpriv "644" $priv "$a" maillist)
		nattr=$(newattribute $A privilege $priv sender "$l")
		$(zapDSNnotify $nattr) ## FIXME!FIXME!
		a=$(runas $priv cat "$a" |				\
		    listexpand	-E "$l" -e "$l" -p $priv		\
				-c "$a file expansion" $nattr "$a"	\
				"$user$athost" "$plustail" "$athost")
		return $a
	fi

	# Ok, if the list (MAILVAR/fqlists/user@host) did exist, we did
	# process it's expansion above.  See if we are looking for *-owner,
	# owner-*, or *-request of same lists.

	ssift "$user" in
	(.+)-owner
		a="$MAILVAR/fqlists/\1$athost"
		key="\1$athost"
		if [ -f "$a" ] && $(didexpand fqlistowner); then
		  db add expansions "$key.fqlistowner" 1
		  nattr=$(newattribute $A privilege $nobodypriv)
		  $(zapDSNnotify $nattr) ## FIXME!FIXME!
		  a=$(rrouter "$(uid2login $(filepriv -M 664 "$a"))" \
				"$address" $nattr "" "" "$domain")
		  return $a
		fi
		break
		;;
	(.+)-request
		a="$MAILVAR/fqlists/\1$athost"
		key="\1$athost"
		if [ -f "$a" ] && $(didexpand fqlistrequest); then
		  db add expansions "$key.fqlistrequest" 1
		  nattr=$(newattribute $A privilege $nobodypriv)
		  $(zapDSNnotify $nattr) ## FIXME!FIXME!
		  a=$(rrouter "$(uid2login $(filepriv -M 664 "$a"))" \
				"$address" $nattr "" "" "$domain")
		  return $a
		fi
		break
		;;
	owner-(.+)
		a="$MAILVAR/fqlists/\1$athost"
		key="\1$athost"
		if [ -f "$a" ] && $(didexpand ownerfqlists); then
		  db add expansions "$key.ownerfqlists" 1
		  nattr=$(newattribute $A privilege $nobodypriv)
		  $(zapDSNnotify $nattr) ## FIXME!FIXME!
		  a=$(rrouter "$(uid2login $(filepriv -M 664 "$a"))" \
				"$address" $nattr "" "" "$domain")
		  return $a
		fi
		;;
	tfiss


	# We may also match for:  "@domain.name"

	#   Yes, add to the expansions.  Success ?
	#     Yes, Any success with filepriv from the alias db ?
	#       Yes, generate new privilege attribute for it. Success ?
	#         Yes, do the expansion! (echo .. | listexpand ...)
	#
	key="$athost"
	if $(didexpand fqdnaliases) &&
	   a="$(fqdnaliases "$key" "$user")" ; then
		db add expansions "$key.fqdnaliases" 1
		nattr=$(newattribute $A privilege $priv) &&
		$(zapDSNnotify $nattr) ## FIXME!FIXME!
		a=$(echo "$a" |						\
		    listaddresses	-c "$a fqdn alias expansion" 	\
					-e root			 |	\
		    maprrouter	$nattr "fqdnalias" "$address"		\
				"$plustail" "$athost")
		return $a
	fi
	if $(didexpand fqdnaliasesldap) &&
	   a="$(fqdnaliasesldap "$key" "$user")" ; then
		db add expansions "$key.fqdnaliasesldap" 1
		nattr=$(newattribute $A privilege $nobodypriv) &&
		$(zapDSNnotify $nattr) ## FIXME!FIXME!
		a=$(echo "$a" |						\
		    listaddresses	-c "$a fqdn ldap expansion" 	\
					-e root			 |	\
		    maprrouter $nattr "fqdnalias" "$address"		\
				"$plustail" "$athost")
		return $a
	fi

	return 1
}


mapfqrrouter (attribute, localpart, origaddr, plustail, domain) {
	local shh al

	al=()
	while read address
	do
		case "$address" in
		'')	shh=(((error expansion "$localpart")))
			lappend al $shh
			continue
			;;
		esac

		defer=''
		shh=$(rrouter "$address" "$origaddr" $attribute \
			"$plustail" "$domain")
		[ "$defer" ] &&
		    shh=(((hold "$defer" "$address" $attribute)))
		defer=''
		lappend al $shh
	done
	return $al
}
