From 818a659ec61f6be3ebed6a000c747c5277c55507 Mon Sep 17 00:00:00 2001 From: Matthew Saunders Brown Date: Mon, 15 Feb 2021 16:23:26 -0800 Subject: [PATCH] initial vmail-mboxes-del.sh --- bin/vmail-mboxes-del.sh | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) mode change 100644 => 100755 bin/vmail-mboxes-del.sh diff --git a/bin/vmail-mboxes-del.sh b/bin/vmail-mboxes-del.sh old mode 100644 new mode 100755 index 7a7ecf1..2907a68 --- a/bin/vmail-mboxes-del.sh +++ b/bin/vmail-mboxes-del.sh @@ -30,7 +30,7 @@ if [ -n "$1" ]; then email=$1 shift if [[ $email =~ "@" ]] ; then - local_part=${email%@*} + mbox=${email%@*} domain=${email##*@} if ! vmail::validate_domain $domain; then echo "ERROR: $domain is not a valid domain name." @@ -63,7 +63,7 @@ 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='$local_part' AND vm_mboxes.domain_id=vm_domains.id AND vm_domains.domain='$domain'" +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 @@ -71,6 +71,7 @@ if [ -z "$mboxes_id" ]; then exit 1 elif [ "$mboxes_id" -gt '0' ]; then if [ -n "$force" ] || vmail::yesno "Delete $email now?"; then + # once ON DELETE CASCADE is set only this first query is needed for vm_* tables dbquery="DELETE FROM vm_mboxes WHERE vm_mboxes.id='$mboxes_id'" echo $dbcmd $dbcmdopts "\"$dbquery\"" dbquery="DELETE FROM sa_userpref WHERE username='$email'" @@ -85,12 +86,14 @@ elif [ "$mboxes_id" -gt '0' ]; then echo $dbcmd $dbcmdopts "\"$dbquery\"" dbquery="DELETE FROM vm_greylisting WHERE recipient='$email'" echo $dbcmd $dbcmdopts "\"$dbquery\"" - dbquery="DELETE FROM vm_greylisting WHERE recipient LIKE '$local_part+%@$domain'" + dbquery="DELETE FROM vm_greylisting WHERE recipient LIKE '$mbox+%@$domain'" echo $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'" echo $dbcmd $dbcmdopts "\"$dbquery\"" - echo "rm -r $VMAIL_DIR/$domain/$local_part" + if [ -d "$VMAIL_DIR/$domain/$mbox" ]; then + echo "rm -r $VMAIL_DIR/$domain/$mbox" + fi echo "SUCCESS: $email removed from system." exit 0 fi