vmail-stack/bin/vmail-forwards-mod.sh
Matthew Saunders Brown d064fcba84 alter vmail db schema
2022-11-29 16:33:15 -08:00

79 lines
2.6 KiB
Bash
Executable File

#!/bin/bash
#
# vmail-stack
# https://git.stack-source.com/msb/vmail-stack
# 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)
# load include file
source $(dirname $0)/vmail.sh
help()
{
thisfilename=$(basename -- "$0")
echo "$thisfilename"
echo "Modify email forward"
echo ""
echo "usage: $thisfilename -e <email> -f <forward> [-k <0|1>] [-h]"
echo ""
echo " -h Print this help."
echo " -e <email> Email address to forward."
echo " -f <forward> Email address to forward to."
echo " -k <0|1> Local Copy (forward & Keep local copy). 0 = no/off, 1 = yes/on. Default is 0."
echo ""
echo " Forwarding is for delivering an email to another domain. Use aliases"
echo " for directing emails to another address in the same domain."
echo " 'email' and 'forward' should both be in full email address format."
echo " .e.g to have info@example.org delivered (forwarded) to joe@example.com do:"
echo " $thisfilename -e info@example.org -f joe@example.com"
exit
}
vmail:getoptions "$@"
# check for email
if [[ -z $email ]]; then
echo "email is required"
exit 1
fi
# build query
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE"
dbcmdopts="-s -r -N -e"
# get forward id, which also verfies that forward already exists
dbquery="SELECT id FROM vm_forwards WHERE mbox='$mbox' AND domain='$domain';"
fowards_id=`eval $dbcmd $dbcmdopts \"$dbquery\"`
if [[ -z $fowards_id ]]; then
# forward does not exist, can't create forward
echo "ERROR: Existing Forward for $email does not exist. Add new forward instead."
exit 1
elif [[ $fowards_id -gt '0' ]]; then
# verified forward exists, build update query
dbquery="UPDATE vm_forwards"
if [[ -z $forward ]] && [[ -z $keep ]]; then
# nothing to update
echo "ERROR: No values passed for update."
exit 1
elif [[ -n $forward ]] && [[ -n $keep ]]; then
# update forward_to & save_local
dbquery="$dbquery SET forward_to='$forward', save_local='$keep'"
elif [[ -n $forward ]] && [[ -z $keep ]]; then
# only update forward_to
dbquery="$dbquery SET forward_to='$forward'"
elif [[ -z $forward ]] && [[ -n $keep ]]; then
# only update save_local
dbquery="$dbquery SET save_local='$keep'"
else
echo "ERROR: Uknown error."
exit 1
fi
dbquery="$dbquery WHERE id='$fowards_id'"
eval $dbcmd $dbcmdopts \"$dbquery\;\"
else
# db query error
echo "ERROR: System error querying database."
exit 1
fi