2017-04-17 16:27:28 +00:00
|
|
|
#! /bin/bash
|
|
|
|
|
2022-02-21 10:56:57 +00:00
|
|
|
# shellcheck source=../scripts/helpers/index.sh
|
|
|
|
source /usr/local/bin/helpers/index.sh
|
2017-04-17 16:27:28 +00:00
|
|
|
|
2022-04-20 00:29:28 +00:00
|
|
|
DATABASE='/tmp/docker-mailserver/postfix-virtual.cf'
|
2017-04-17 16:27:28 +00:00
|
|
|
|
2021-02-23 19:03:01 +00:00
|
|
|
function __usage
|
|
|
|
{
|
2022-05-10 15:50:33 +00:00
|
|
|
printf '%s' "${PURPLE}ADDALIAS${RED}(${YELLOW}8${RED})
|
2021-02-23 19:03:01 +00:00
|
|
|
|
2022-05-10 15:50:33 +00:00
|
|
|
${ORANGE}NAME${RESET}
|
2021-02-23 19:03:01 +00:00
|
|
|
addalias - add an email alias for an existing user
|
|
|
|
|
2022-05-10 15:50:33 +00:00
|
|
|
${ORANGE}SYNOPSIS${RESET}
|
2021-02-23 19:03:01 +00:00
|
|
|
./setup.sh alias add <EMAIL ADDRESS> <RECIPIENT>
|
|
|
|
|
2022-05-10 15:50:33 +00:00
|
|
|
${ORANGE}OPTIONS${RESET}
|
|
|
|
${BLUE}Generic Program Information${RESET}
|
2021-02-23 19:03:01 +00:00
|
|
|
help Print the usage information.
|
|
|
|
|
2022-05-10 15:50:33 +00:00
|
|
|
${ORANGE}EXAMPLES${RESET}
|
|
|
|
${LWHITE}./setup.sh alias add alias-for-me@domain.tld admin@domain.tld${RESET}
|
2021-02-23 19:03:01 +00:00
|
|
|
Add the alias alias-for-me@doamin.tld for the existing user
|
|
|
|
admin@domain.tld.
|
|
|
|
|
2022-05-10 15:50:33 +00:00
|
|
|
${ORANGE}EXIT STATUS${RESET}
|
2021-02-23 19:03:01 +00:00
|
|
|
Exit status is 0 if command was successful. If wrong arguments are provided
|
|
|
|
or arguments contain errors, the script will exit early with exit status 1.
|
|
|
|
|
|
|
|
"
|
|
|
|
}
|
|
|
|
|
|
|
|
[[ ${1:-} == 'help' ]] && { __usage ; exit 0 ; }
|
|
|
|
|
2020-10-21 16:16:32 +00:00
|
|
|
EMAIL="${1}"
|
|
|
|
RECIPIENT="${2}"
|
2017-04-17 16:27:28 +00:00
|
|
|
|
2022-03-21 14:01:07 +00:00
|
|
|
[[ -z ${EMAIL} ]] && { __usage ; _exit_with_error 'No alias specified' ; }
|
|
|
|
[[ -z ${RECIPIENT} ]] && { __usage ; _exit_with_error 'No recipient specified' ; }
|
2017-04-17 16:27:28 +00:00
|
|
|
|
2020-10-21 16:16:32 +00:00
|
|
|
grep \
|
2022-02-21 10:56:57 +00:00
|
|
|
-qi "^$(_escape "${EMAIL}")[a-zA-Z@.\ ]*$(_escape "${RECIPIENT}")" \
|
2022-03-21 14:01:07 +00:00
|
|
|
"${DATABASE}" 2>/dev/null && _exit_with_error "Alias \"${EMAIL} ${RECIPIENT}\" already exists"
|
2017-04-17 16:27:28 +00:00
|
|
|
|
2022-02-21 10:56:57 +00:00
|
|
|
if grep -qi "^$(_escape "${EMAIL}")" "${DATABASE}" 2>/dev/null
|
2020-10-21 16:16:32 +00:00
|
|
|
then
|
|
|
|
sed -i "/${EMAIL}/s/$/,${RECIPIENT}/" "${DATABASE}"
|
|
|
|
else
|
|
|
|
echo "${EMAIL} ${RECIPIENT}" >> "${DATABASE}"
|
2017-04-17 16:27:28 +00:00
|
|
|
fi
|