initial vmail-mboxes-del.sh

This commit is contained in:
Matthew Saunders Brown 2021-02-15 16:23:26 -08:00
parent c851e72c57
commit 818a659ec6

11
bin/vmail-mboxes-del.sh Normal file → Executable file
View File

@ -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