vmail-stack/bin/vmail-mboxes-del.sh

67 lines
2.1 KiB
Bash
Raw Permalink Normal View History

#!/bin/bash
#
# vmail-stack
# https://git.stack-source.com/msb/vmail-stack
2022-08-22 13:34:20 -07:00
# Copyright (c) 2022 Matthew Saunders Brown <matthewsaundersbrown@gmail.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
2021-04-02 12:02:50 -07:00
# load include file
source $(dirname $0)/vmail.sh
help()
{
thisfilename=$(basename -- "$0")
echo "$thisfilename"
echo "Delete email account."
echo ""
2021-10-15 15:17:29 -07:00
echo "usage: $thisfilename -e <email> [-x]"
echo ""
echo " -h Print this help."
2021-10-15 15:17:29 -07:00
echo " -e <email> Email address to delete."
echo " -x Execute (force) - don't prompt for confirmation."
}
2021-10-15 15:17:29 -07:00
vmail:getoptions "$@"
2021-10-15 15:17:29 -07:00
# 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
2022-11-29 16:33:15 -08:00
dbquery="SELECT id FROM vm_mboxes WHERE mbox='$mbox' AND domain='$domain'"
mboxes_id=`$dbcmd $dbcmdopts "$dbquery"`
2022-11-29 16:33:15 -08:00
if [[ -z $mboxes_id ]]; then
echo "ERROR: Email address $email does not exist."
exit 1
2022-11-29 16:33:15 -08:00
elif [[ $mboxes_id -gt '0' ]]; then
2021-10-15 15:17:29 -07:00
if [[ -n $execute ]] || vmail::yesno "Delete $email now?"; then
2021-02-15 16:58:40 -08:00
# this should be sufficient for vm_* tables due to ON DELETE CASCADE foreign key references
2022-11-29 16:33:15 -08:00
dbquery="DELETE FROM vm_mboxes WHERE id='$mboxes_id';"
2021-02-19 17:03:56 -08:00
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
2021-02-19 17:03:56 -08:00
dbquery="DELETE FROM rc_users WHERE username='$email';"
eval $dbcmd $dbcmdopts "\"$dbquery\""
2021-02-15 16:23:26 -08:00
if [ -d "$VMAIL_DIR/$domain/$mbox" ]; then
2021-02-19 17:03:56 -08:00
rm -r $VMAIL_DIR/$domain/$mbox
2021-02-15 16:23:26 -08:00
fi
echo "SUCCESS: $email removed from system."
exit 0
fi
else
echo "ERROR: System error querying vmail database"
exit 1
fi