mirror of
https://github.com/docker-mailserver/docker-mailserver.git
synced 2024-01-19 02:48:50 +00:00
Issue 538 (#541)
* adapted setup.sh to handle email aliases * added needed scripts for alias management * added integration tests
This commit is contained in:
parent
7753d59d72
commit
a144f3811c
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -7,3 +7,4 @@ test/config/without-virtual/
|
|||
test/config/postfix-accounts.cf
|
||||
test/config/letsencrypt/mail.my-domain.com/combined.pem
|
||||
test/onedir
|
||||
config/opendkim/
|
||||
|
|
26
setup.sh
26
setup.sh
|
@ -47,6 +47,11 @@ SUBCOMMANDS:
|
|||
$0 email del <email>
|
||||
$0 email list
|
||||
|
||||
alias:
|
||||
$0 alias add <email> <recipient>
|
||||
$0 alias del <email> <recipient>
|
||||
$0 alias list
|
||||
|
||||
config:
|
||||
|
||||
$0 config dkim
|
||||
|
@ -133,6 +138,27 @@ case $1 in
|
|||
esac
|
||||
;;
|
||||
|
||||
alias)
|
||||
shift
|
||||
case $1 in
|
||||
add)
|
||||
shift
|
||||
_docker_image addalias $@
|
||||
;;
|
||||
del)
|
||||
shift
|
||||
_docker_image delalias $@
|
||||
;;
|
||||
list)
|
||||
shift
|
||||
_docker_image listalias $@
|
||||
;;
|
||||
*)
|
||||
_usage
|
||||
;;
|
||||
esac
|
||||
;;
|
||||
|
||||
config)
|
||||
shift
|
||||
case $1 in
|
||||
|
|
30
target/bin/addalias
Executable file
30
target/bin/addalias
Executable file
|
@ -0,0 +1,30 @@
|
|||
#! /bin/bash
|
||||
|
||||
DATABASE=${DATABASE:-/tmp/docker-mailserver/postfix-virtual.cf}
|
||||
|
||||
EMAIL="$1"
|
||||
RECIPIENT="$2"
|
||||
|
||||
usage() {
|
||||
echo "Usage: addalias <user@domain> <recipient@other>"
|
||||
}
|
||||
|
||||
errex() {
|
||||
echo "$@" 1>&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
escape() {
|
||||
echo "${1//./\\.}"
|
||||
}
|
||||
|
||||
[ -z "$EMAIL" ] && { usage; errex "no email specified"; }
|
||||
|
||||
grep -qi "^$(escape $EMAIL)[a-zA-Z@.\ ]*$(escape $RECIPIENT)" $DATABASE 2>/dev/null &&
|
||||
errex "Alias \"$EMAIL $RECIPIENT\" already exists"
|
||||
|
||||
if grep -qi "^$(escape $EMAIL)" $DATABASE 2>/dev/null; then
|
||||
sed -i "/$EMAIL/s/$/ $RECIPIENT,/" $DATABASE
|
||||
else
|
||||
echo "$EMAIL $RECIPIENT," >> $DATABASE
|
||||
fi
|
30
target/bin/delalias
Executable file
30
target/bin/delalias
Executable file
|
@ -0,0 +1,30 @@
|
|||
#! /bin/bash
|
||||
|
||||
DATABASE=${DATABASE:-/tmp/docker-mailserver/postfix-virtual.cf}
|
||||
|
||||
EMAIL="$1"
|
||||
RECIPIENT="$2"
|
||||
|
||||
usage() {
|
||||
echo "Usage: delalias <user@domain> <recipient@other>"
|
||||
}
|
||||
|
||||
errex() {
|
||||
echo "$@" 1>&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
escape() {
|
||||
echo "${1//./\\.}"
|
||||
}
|
||||
|
||||
[ -z "$EMAIL" ] || [ -z "$RECIPIENT" ] && { usage; errex "No email specifed"; }
|
||||
[ -s "$DATABASE" ] || exit 0
|
||||
|
||||
CNT=$(grep "^$EMAIL" $DATABASE | wc -w | awk '{print $1}')
|
||||
|
||||
if [[ $CNT -eq 2 ]]; then
|
||||
sed -i "/^$EMAIL/d" $DATABASE
|
||||
else
|
||||
sed -i "/^$EMAIL/s/ $RECIPIENT,//g" $DATABASE
|
||||
fi
|
13
target/bin/listalias
Executable file
13
target/bin/listalias
Executable file
|
@ -0,0 +1,13 @@
|
|||
#! /bin/bash
|
||||
|
||||
DATABASE=${DATABASE:-/tmp/docker-mailserver/postfix-virtual.cf}
|
||||
|
||||
errex() {
|
||||
echo "$@" 1>&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
[ -f $DATABASE ] || errex "No postfix-virtual.cf file"
|
||||
[ -s $DATABASE ] || errex "Empty postfix-virtual.cf - no aliases have been added"
|
||||
|
||||
cat $DATABASE
|
|
@ -936,6 +936,28 @@ load 'test_helper/bats-assert/load'
|
|||
[ -z "$value" ]
|
||||
}
|
||||
|
||||
# alias
|
||||
@test "checking setup.sh: setup.sh alias list" {
|
||||
echo "test@example.org test@forward.com" > ./config/postfix-virtual.cf
|
||||
run ./setup.sh -c mail alias list
|
||||
assert_success
|
||||
}
|
||||
@test "checking setup.sh: setup.sh alias add" {
|
||||
echo "" > ./config/postfix-virtual.cf
|
||||
./setup.sh -c mail alias add test1@example.org test1@forward.com
|
||||
./setup.sh -c mail alias add test1@example.org test2@forward.com
|
||||
|
||||
run /bin/sh -c 'cat ./config/postfix-virtual.cf | grep "test1@example.org test1@forward.com, test2@forward.com," | wc -l | grep 1'
|
||||
assert_success
|
||||
}
|
||||
@test "checking setup.sh: setup.sh alias del" {
|
||||
echo 'test1@example.org test1@forward.com, test2@forward.com,' > ./config/postfix-virtual.cf
|
||||
./setup.sh -c mail alias del test1@example.org test1@forward.com
|
||||
./setup.sh -c mail alias del test1@example.org test2@forward.com
|
||||
run cat ./config/postfix-virtual.cf | wc -l | grep 0
|
||||
assert_success
|
||||
}
|
||||
|
||||
# config
|
||||
@test "checking setup.sh: setup.sh config dkim" {
|
||||
run ./setup.sh -c mail config dkim
|
||||
|
|
Loading…
Reference in a new issue