#!/bin/bash # # vmail-stack # https://git.stack-source.com/msb/vmail-stack # Copyright (c) 2022 Matthew Saunders Brown # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) # load include file source $(dirname $0)/vmail.sh help() { thisfilename=$(basename -- "$0") echo "$thisfilename" echo "Delete email alias from vmail database." echo "" echo "usage: $thisfilename -d |-e -a " echo "" echo " -h Print this help." echo " -a Alias to be deleted. Local part only." echo " -d Domain of alias to delete." echo " -e Email that alias directs to to delete." echo "" echo " Enter OR . If is specified" echo " then just the alias to that address is deleted. If" echo " domain is specific then all @ aliases" echo " are deleted (aliases can direct to multiple emails)." } vmail:getoptions "$@" # check for alias if [[ -z $alias ]]; then echo "alias is required" exit fi # start building query dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE" dbcmdopts="-s -r -N -e" # if email set delete single alias if [[ -n $email ]]; then # get aliases id which also works to confirm alias exists dbquery="SELECT id FROM vm_aliases WHERE alias='$alias' AND mbox='$mbox' AND domain='$domain'" aliases_id=`$dbcmd $dbcmdopts "$dbquery"` if [[ -z $aliases_id ]]; then echo "ERROR: Alias $alias@$domain -> $email does not exist." exit 1 elif [ "$aliases_id" -gt '0' ]; then dbquery="DELETE FROM vm_aliases WHERE id='$aliases_id'" eval $dbcmd $dbcmdopts "\"$dbquery\"" echo "SUCCESS: Alias $alias@$domain -> $email removed from system." exit 0 else echo "ERROR: System error querying vmail database" exit 1 fi # if domain set then delete any and all aliases elif [[ -n $domain ]]; then # get alias id array dbquery="SELECT id FROM vm_aliases WHERE alias='$alias' AND domain='$domain'" mapfile -t aliases_ids < <( $dbcmd $dbcmdopts "$dbquery" ) if [[ -z $aliases_ids ]]; then echo "ERROR: Alias $alias@$domain does not exist." exit 1 elif [[ ${#aliases_ids[@]} -gt '0' ]]; then for aliases_id in "${aliases_ids[@]}"; do dbquery="DELETE FROM vm_aliases WHERE id='$aliases_id'" eval $dbcmd $dbcmdopts "\"$dbquery\"" done echo "SUCCESS: Alias(s) for $alias@$domain removed from system." else echo "ERROR: Unknown error. vm_alias_count=0?" exit 1 fi else echo "You must specify either or ." exit 1 fi