2017-04-17 16:27:28 +00:00
|
|
|
#! /bin/bash
|
|
|
|
|
2021-01-16 09:16:05 +00:00
|
|
|
# shellcheck source=../scripts/helper-functions.sh
|
|
|
|
. /usr/local/bin/helper-functions.sh
|
2017-04-17 16:27:28 +00:00
|
|
|
|
2020-10-21 16:16:32 +00:00
|
|
|
DATABASE=${DATABASE:-/tmp/docker-mailserver/postfix-virtual.cf}
|
2017-04-17 16:27:28 +00:00
|
|
|
|
2020-10-21 16:16:32 +00:00
|
|
|
EMAIL="${1}"
|
|
|
|
RECIPIENT="${2}"
|
2017-04-17 16:27:28 +00:00
|
|
|
|
2020-10-21 16:16:32 +00:00
|
|
|
function usage { echo "Usage: addalias <alias@domain> <recipient@other>" ; }
|
2017-04-17 16:27:28 +00:00
|
|
|
|
2020-10-21 16:16:32 +00:00
|
|
|
[[ -z ${EMAIL} ]] && { usage ; errex "Error: No alias specified" ; }
|
|
|
|
[[ -z ${RECIPIENT} ]] && { usage ; errex "Error: No recipient specified" ; }
|
2017-04-17 16:27:28 +00:00
|
|
|
|
2020-10-21 16:16:32 +00:00
|
|
|
grep \
|
|
|
|
-qi "^$(escape "${EMAIL}")[a-zA-Z@.\ ]*$(escape "${RECIPIENT}")" \
|
|
|
|
"${DATABASE}" 2>/dev/null && errex "Alias \"${EMAIL} ${RECIPIENT}\" already exists"
|
2017-04-17 16:27:28 +00:00
|
|
|
|
2020-10-21 16:16:32 +00:00
|
|
|
if grep -qi "^$(escape "${EMAIL}")" "${DATABASE}" 2>/dev/null
|
|
|
|
then
|
|
|
|
sed -i "/${EMAIL}/s/$/,${RECIPIENT}/" "${DATABASE}"
|
|
|
|
else
|
|
|
|
echo "${EMAIL} ${RECIPIENT}" >> "${DATABASE}"
|
2017-04-17 16:27:28 +00:00
|
|
|
fi
|