vmail-stack/bin/vmail-forwards-mod.sh

79 lines
2.6 KiB
Bash
Raw Permalink Normal View History

2022-05-18 14:37:30 -07:00
#!/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)
2022-05-18 14:37:30 -07:00
# 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"
2022-11-29 16:33:15 -08:00
# 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\"`
2022-05-18 14:37:30 -07:00
2022-11-29 16:33:15 -08:00
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."
2022-05-18 14:37:30 -07:00
exit 1
2022-11-29 16:33:15 -08:00
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."
2022-05-18 14:37:30 -07:00
exit 1
2022-11-29 16:33:15 -08:00
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'"
2022-05-18 14:37:30 -07:00
else
2022-11-29 16:33:15 -08:00
echo "ERROR: Uknown error."
exit 1
2022-05-18 14:37:30 -07:00
fi
2022-11-29 16:33:15 -08:00
dbquery="$dbquery WHERE id='$fowards_id'"
eval $dbcmd $dbcmdopts \"$dbquery\;\"
2022-05-18 14:37:30 -07:00
else
# db query error
echo "ERROR: System error querying database."
exit 1
fi