78 lines
2.4 KiB
Bash
78 lines
2.4 KiB
Bash
|
#!/bin/bash
|
||
|
#
|
||
|
# vmail-stack
|
||
|
# https://git.stack-source.com/msb/vmail-stack
|
||
|
# MIT License Copyright (c) 2021 Matthew Saunders Brown
|
||
|
|
||
|
# 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
|
||
|
|
||
|
# check for forward
|
||
|
if [[ -z $forward ]]; then
|
||
|
echo "forward is required"
|
||
|
exit 1
|
||
|
fi
|
||
|
|
||
|
# build query
|
||
|
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE"
|
||
|
dbcmdopts="-s -r -N -e"
|
||
|
|
||
|
# get mbox id, which also verfies that email address exists
|
||
|
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';"
|
||
|
mbox_id=`eval $dbcmd $dbcmdopts \"$dbquery\"`
|
||
|
|
||
|
if [ -z $mbox_id ]; then
|
||
|
# mbox does not exist, can't create forward
|
||
|
echo "ERROR: Address to Forward ($email) does not exist."
|
||
|
exit 1
|
||
|
elif [ "$mbox_id" -gt '0' ]; then
|
||
|
# verified mbox, check for existing forward
|
||
|
dbquery="SELECT id FROM vm_forwards WHERE mbox_id='$mbox_id';"
|
||
|
vm_forwards_id=`eval $dbcmd $dbcmdopts \"$dbquery\"`
|
||
|
if [ -z $vm_forwards_id ]; then
|
||
|
# existing forward does not exist, can't modify
|
||
|
echo "ERROR: Forward for $email does not exists, can't edit. Add new forward instead."
|
||
|
exit 1
|
||
|
else
|
||
|
dbquery="UPDATE vm_forwards SET forward_to='$forward'"
|
||
|
if [ ! -z $keep ]; then
|
||
|
dbquery="$dbquery, save_local='$keep'"
|
||
|
fi
|
||
|
dbquery="$dbquery WHERE mbox_id='$mbox_id'"
|
||
|
eval $dbcmd $dbcmdopts \"$dbquery\;\"
|
||
|
fi
|
||
|
else
|
||
|
# db query error
|
||
|
echo "ERROR: System error querying database."
|
||
|
exit 1
|
||
|
fi
|