2021-02-16 13:27:27 -08: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)
|
2021-02-16 13:27:27 -08:00
|
|
|
|
2021-04-02 12:02:50 -07:00
|
|
|
# load include file
|
|
|
|
source $(dirname $0)/vmail.sh
|
2021-02-16 13:27:27 -08:00
|
|
|
|
|
|
|
help()
|
|
|
|
{
|
|
|
|
thisfilename=$(basename -- "$0")
|
|
|
|
echo "$thisfilename"
|
|
|
|
echo "Add email forward to vmail system"
|
|
|
|
echo ""
|
2021-10-15 15:17:29 -07:00
|
|
|
echo "usage: $thisfilename -e <email> -f <forward> [-k <0|1>] [-h]"
|
2021-02-16 13:27:27 -08:00
|
|
|
echo ""
|
2021-10-15 15:17:29 -07:00
|
|
|
echo " -h Print this help."
|
|
|
|
echo " -e <email> Email address to forward."
|
|
|
|
echo " -f <forward> Email address to forward to."
|
|
|
|
echo " -k <0|1> Save Local (forward & Keep local copy). 0 = no/off, 1 = yes/on. Default is 0."
|
2021-02-16 13:27:27 -08:00
|
|
|
echo ""
|
2021-10-15 15:17:29 -07:00
|
|
|
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:"
|
2022-04-14 13:34:11 -07:00
|
|
|
echo " $thisfilename -e info@example.org -f joe@example.com"
|
2021-02-16 13:27:27 -08:00
|
|
|
exit
|
|
|
|
}
|
|
|
|
|
2021-10-15 15:17:29 -07:00
|
|
|
vmail:getoptions "$@"
|
|
|
|
|
|
|
|
# check for email
|
|
|
|
if [[ -z $email ]]; then
|
|
|
|
echo "email is required"
|
|
|
|
exit
|
2021-02-16 13:27:27 -08:00
|
|
|
fi
|
|
|
|
|
2021-10-15 15:17:29 -07:00
|
|
|
# check for forward
|
|
|
|
if [[ -z $forward ]]; then
|
|
|
|
echo "forward is required"
|
|
|
|
exit
|
|
|
|
fi
|
2021-02-16 13:27:27 -08:00
|
|
|
|
|
|
|
# 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 mbox id, which verfies that email address exists
|
|
|
|
dbquery="SELECT id FROM vm_mboxes WHERE mbox='$mbox' AND domain='$domain';"
|
2021-02-16 13:27:27 -08:00
|
|
|
mbox_id=`eval $dbcmd $dbcmdopts \"$dbquery\"`
|
|
|
|
|
2022-11-29 16:33:15 -08:00
|
|
|
if [[ -z $mbox_id ]]; then
|
2021-02-16 13:27:27 -08:00
|
|
|
# mbox does not exist, can't create forward
|
|
|
|
echo "ERROR: Address to Forward ($email) does not exist."
|
|
|
|
exit 1
|
2022-11-29 16:33:15 -08:00
|
|
|
elif [[ $mbox_id -gt '0' ]]; then
|
2021-02-16 13:27:27 -08:00
|
|
|
# verified mbox, check for existing forward
|
2022-11-29 16:33:15 -08:00
|
|
|
dbquery="SELECT id FROM vm_forwards WHERE mbox='$mbox' AND domain='$domain';"
|
|
|
|
forwards_id=`eval $dbcmd $dbcmdopts \"$dbquery\"`
|
|
|
|
if [[ -z $forwards_id ]]; then
|
2021-02-16 13:27:27 -08:00
|
|
|
# existing forward does not exist, add it now
|
2022-11-29 16:33:15 -08:00
|
|
|
dbquery="INSERT INTO vm_forwards SET mbox='$mbox', domain='$domain', forward_to='$forward'"
|
|
|
|
if [[ ! -z $keep ]]; then
|
2021-10-15 15:17:29 -07:00
|
|
|
dbquery="$dbquery, save_local='$keep'"
|
2021-02-16 13:27:27 -08:00
|
|
|
fi
|
2021-02-16 13:36:46 -08:00
|
|
|
eval $dbcmd $dbcmdopts \"$dbquery\;\"
|
2021-02-16 13:27:27 -08:00
|
|
|
else
|
2021-10-15 16:22:58 -07:00
|
|
|
echo "ERROR: Forward for $email already exists. To change either del then add."
|
2021-02-16 13:27:27 -08:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
# db query error
|
|
|
|
echo "ERROR: System error querying vmail database."
|
|
|
|
exit 1
|
|
|
|
fi
|