2021-02-15 09:51:04 -08:00
#!/bin/bash
#
# vmail-stack
# https://git.stack-source.com/msb/vmail-stack
# MIT License Copyright (c) 2021 Matthew Saunders Brown
# load config
source /usr/local/etc/vmail.conf
help( )
{
thisfilename = $( basename -- " $0 " )
echo " $thisfilename "
echo "Delete email account."
echo ""
echo " usage: $thisfilename fullemail "
echo ""
echo " -f Force - don't prompt for confirmation."
echo " -h Print this help."
echo ""
echo " Enter account to delete in full email address format."
exit
}
# check for and set email address info
if [ -n " $1 " ] ; then
if [ $1 = = "-h" ] ; then
help
elif [ [ ! $1 = ~ ^- ] ] ; then
email = $1
shift
if [ [ $email = ~ "@" ] ] ; then
2021-02-15 16:23:26 -08:00
mbox = ${ email %@* }
2021-02-15 09:51:04 -08:00
domain = ${ email ##*@ }
if ! vmail::validate_domain $domain ; then
echo " ERROR: $domain is not a valid domain name. "
exit 1
fi
else
echo " ERROR: $email is not a valid email address. "
help
fi
fi
fi
# set any options that were passed
while getopts "fh" opt; do
case " ${ opt } " in
f )
force = true
; ;
h )
help
exit; ;
\? )
echo " Invalid option: $OPTARG " 1>& 2
exit; ;
esac
done
# 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
2021-02-15 16:23:26 -08:00
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 ' "
2021-02-15 09:51:04 -08:00
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 " $force " ] || vmail::yesno " Delete $email now? " ; then
2021-02-15 16:23:26 -08:00
# once ON DELETE CASCADE is set only this first query is needed for vm_* tables
2021-02-15 09:51:04 -08:00
dbquery = " DELETE FROM vm_mboxes WHERE vm_mboxes.id=' $mboxes_id ' "
echo $dbcmd $dbcmdopts " \" $dbquery \" "
dbquery = " DELETE FROM sa_userpref WHERE username=' $email ' "
echo $dbcmd $dbcmdopts " \" $dbquery \" "
dbquery = " DELETE FROM vm_aliases WHERE mbox_id=' $mboxes_id ' "
echo $dbcmd $dbcmdopts " \" $dbquery \" "
dbquery = " DELETE FROM vm_autoresponders WHERE mbox_id=' $mboxes_id ' "
echo $dbcmd $dbcmdopts " \" $dbquery \" "
dbquery = " DELETE FROM vm_filters WHERE mbox_id=' $mboxes_id ' "
echo $dbcmd $dbcmdopts " \" $dbquery \" "
dbquery = " DELETE FROM vm_forwards WHERE mbox_id=' $mboxes_id ' "
echo $dbcmd $dbcmdopts " \" $dbquery \" "
dbquery = " DELETE FROM vm_greylisting WHERE recipient=' $email ' "
echo $dbcmd $dbcmdopts " \" $dbquery \" "
2021-02-15 16:23:26 -08:00
dbquery = " DELETE FROM vm_greylisting WHERE recipient LIKE ' $mbox +%@ $domain ' "
2021-02-15 09:51:04 -08:00
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 \" "
2021-02-15 16:23:26 -08:00
if [ -d " $VMAIL_DIR / $domain / $mbox " ] ; then
echo " rm -r $VMAIL_DIR / $domain / $mbox "
fi
2021-02-15 09:51:04 -08:00
echo " SUCCESS: $email removed from system. "
exit 0
fi
else
echo "ERROR: System error querying vmail database"
exit 1
fi