2021-02-10 16:16:23 -08:00
#!/bin/bash
#
# vmail-stack
# https://git.stack-source.com/msb/vmail-stack
# MIT License Copyright (c) 2021 Matthew Saunders Brown
# load config
2021-03-19 11:58:35 -07:00
source /usr/local/etc/vmail.conf || echo "ERROR: Either you do not have vmail user permissions or the config file is missing." && exit
2021-02-10 16:16:23 -08:00
help( )
{
thisfilename = $( basename -- " $0 " )
echo " $thisfilename "
echo "Delete email alias from vmail database."
echo ""
echo " usage: $thisfilename alias "
echo ""
echo " -h Print this help."
echo ""
echo " Enter alias in full email address format."
exit
}
# check for and set alias search term(s)
if [ -n " $1 " ] ; then
if [ $1 = = "-h" ] ; then
help
elif [ [ ! $1 = ~ ^- ] ] ; then
searchterm = $1
shift
if [ [ $searchterm = ~ "@" ] ] ; then
alias = ${ searchterm %@* }
domain = ${ searchterm ##*@ }
if ! vmail::validate_domain $domain ; then
2021-02-11 15:07:44 -08:00
echo " ERROR: $domain is not a valid domain name. "
2021-02-11 14:21:45 -08:00
exit 1
2021-02-10 16:16:23 -08:00
fi
else
2021-02-11 15:07:44 -08:00
echo " ERROR: $searchterm is not a valid email alias. "
2021-02-10 16:16:23 -08:00
help
fi
fi
fi
2021-02-12 09:47:26 -08:00
# set any options that were passed
while getopts "h" opt; do
case " ${ opt } " in
h )
help
exit; ;
\? )
echo " Invalid option: $OPTARG " 1>& 2
exit; ;
2021-03-18 16:49:17 -07:00
: )
echo " Invalid option: $OPTARG requires an argument " 1>& 2
exit; ;
2021-02-12 09:47:26 -08:00
esac
done
2021-02-11 15:07:44 -08:00
# build query
dbcmd = " mysql --defaults-extra-file= $MYSQL_CONNECTION_INFO_FILE "
dbcmdopts = "-s -r -N -e"
2021-02-11 14:21:45 -08:00
2021-02-11 15:07:44 -08:00
# get aliases id which also works to confirm alias exists
dbquery = " SELECT vm_aliases.id FROM vm_aliases, vm_mboxes, vm_domains WHERE vm_aliases.alias=' $alias ' AND vm_aliases.mbox_id=vm_mboxes.id AND vm_mboxes.domain_id=vm_domains.id AND vm_domains.domain=' $domain ' "
vm_aliases_id = ` $dbcmd $dbcmdopts " $dbquery " `
2021-02-10 16:16:23 -08:00
2021-02-11 15:07:44 -08:00
if [ -z " $vm_aliases_id " ] ; then
echo " ERROR: Alias $searchterm does not exist. "
exit 1
elif [ " $vm_aliases_id " -gt '0' ] ; then
dbquery = " DELETE FROM vm_aliases WHERE vm_aliases.id=' $vm_aliases_id ' "
eval $dbcmd $dbcmdopts " \" $dbquery \" "
echo " SUCCESS: Alias $searchterm removed from system. "
exit 0
else
echo "ERROR: System error querying vmail database"
exit 1
2021-02-10 16:16:23 -08:00
fi