#!/bin/bash # shellcheck disable=SC1091 [ -r /var/lib/from_repos/scripts/shared_functions.sh ] && . /var/lib/from_repos/scripts/shared_functions.sh function usage() { echo "Usage:" echo "$(basename "${0}") -p|--porpouse [-o|--domain ] [-l|--length ] [-e|--destination ]" echo "" echo " -d|--debug Show more debug information." echo " -p|--porpouse Porpouse of this forwarding that will help you remember why you created it." echo " -o|--domain Domain name for the alias. From the list:" while read -r DOMAIN do echo " # ${DOMAIN}" done <<< "$(echo 'select domain FROM mail.domains' | mysql mail|grep -v '^domain$')" echo " -l|--length Length of pseudo-random character of the alias." echo " -e|--destination Destination of the forwarding." echo " -a|--alias Use a defined alias." } domain="susurrando.com" alias_length=6 destination="ad@susurrando.com" alias="" while [ $# -gt 0 ] do case "${1}" in "-h"|"-?"|"--help") shift usage exit 0 ;; "-d"|"--debug") shift export DEBUG=true ;; "-o"|"--domain") shift domain="${1}" existing=$(echo "select domain FROM mail.domains WHERE domain = '${domain}';" | mysql mail | grep -v '^domain$') if [ -z "${existing}" ]; then message "The domain '${domain}' doesn't exist in the database. Add it first." p echo "Current list of domains:" while read -r DOMAIN do echo " # ${DOMAIN}" done <<< "$(echo 'select domain FROM mail.domains' | mysql mail|grep -v '^domain$')" exit 4 fi shift ;; "-l"|"--length") shift alias_length="${1}" shift ;; "-e"|"--destination") shift destination="${1}" shift ;; "-p"|"--porpouse") shift porpouse="${1}" shift ;; "-a"|"--alias") shift alias="${1}" shift ;; *) message "Unknown parameter '$1'" p shift ;; esac done if [ -z "${porpouse}" ]; then message "You have to provide a porpouse." p usage exit 1 fi existing=$(echo "SELECT * FROM mail.forwardings where porpouse = '${porpouse}';" | mysql mail) if [ -n "${existing}" ]; then message "There is already an alias for that porpouse:" p message "${existing}" p exit 2 fi if [ -n "${alias}" ]; then existing=$(echo "SELECT source FROM mail.forwardings where source = '${alias}';" |mysql mail | grep -v '^source$') if [ -n "${existing}" ]; then message "Alias already exist in the database:" p message "'${existing}'" p exit 3 fi fi while [ "${alias}" == "" ] do random_string=$(LC_ALL=C tr -dc A-Za-z0-9