#!/bin/bash # # vmail-stack # https://git.stack-source.com/msb/vmail-stack # MIT License Copyright (c) 2021 Matthew Saunders Brown # load include file source $(dirname $0)/vmail.sh help() { thisfilename=$(basename -- "$0") echo "$thisfilename" echo "Delete email account." echo "" echo "usage: $thisfilename -e [-x]" echo "" echo " -h Print this help." echo " -e Email address to delete." echo " -x Execute (force) - don't prompt for confirmation." } vmail:getoptions "$@" # check for email if [[ -z $email ]]; then echo "email is required" exit fi # build query dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE" dbcmdopts="-s -r -N -e" # get email address id which also works to confirm address exists dbquery="SELECT vm_mboxes.id FROM vm_mboxes, vm_domains WHERE vm_mboxes.mbox='$mbox' AND vm_mboxes.domain_id=vm_domains.id AND vm_domains.domain='$domain'" mboxes_id=`$dbcmd $dbcmdopts "$dbquery"` if [ -z "$mboxes_id" ]; then echo "ERROR: Email address $email does not exist." exit 1 elif [ "$mboxes_id" -gt '0' ]; then if [[ -n $execute ]] || vmail::yesno "Delete $email now?"; then # this should be sufficient for vm_* tables due to ON DELETE CASCADE foreign key references dbquery="DELETE FROM vm_mboxes WHERE vm_mboxes.id='$mboxes_id';" eval $dbcmd $dbcmdopts "\"$dbquery\"" dbquery="DELETE FROM sa_userpref WHERE username='$email';" eval $dbcmd $dbcmdopts "\"$dbquery\"" dbquery="DELETE FROM vm_greylisting WHERE recipient='$email';" eval $dbcmd $dbcmdopts "\"$dbquery\"" dbquery="DELETE FROM vm_greylisting WHERE recipient LIKE '$mbox+%@$domain';" eval $dbcmd $dbcmdopts "\"$dbquery\"" # this should be sufficient for rc due to ON DELETE CASCADE foreign key references dbquery="DELETE FROM rc_users WHERE username='$email';" eval $dbcmd $dbcmdopts "\"$dbquery\"" if [ -d "$VMAIL_DIR/$domain/$mbox" ]; then rm -r $VMAIL_DIR/$domain/$mbox fi echo "SUCCESS: $email removed from system." exit 0 fi else echo "ERROR: System error querying vmail database" exit 1 fi