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

58 lines
2.1 KiB
Bash
Raw Normal View History

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 "Get email forwards data from vmail database."
echo ""
2022-11-29 16:33:15 -08:00
echo "usage: $thisfilename [-e email|-f forward|-d domain] [-c] [-t] [-h]"
2021-02-16 13:27:27 -08:00
echo ""
2021-10-15 15:17:29 -07:00
echo " -h Print this help."
2022-05-23 09:30:18 -07:00
echo " -e <email> Email address to get forwarding for."
2021-10-15 15:17:29 -07:00
echo " -f <forward> Return info for specific forward."
2022-05-23 09:30:18 -07:00
echo " -d <domain> Get all forwards for specified domain."
2021-10-15 15:17:29 -07:00
echo " -c Output in cvs format."
echo " -t Use tabs instead of tables for output, do not display column headers."
2021-02-16 13:27:27 -08:00
echo ""
2021-10-15 15:17:29 -07:00
echo " Search term is optional. If nothing specified all forwards for all email acccounts for all domains will be returned."
echo " Enter email address to get forward info for that email address."
echo " Enter forwarding address (in full email address format) with the -f option to get address(es) that forward to specified address."
echo " Enter domain name to get all forwards for all email addresses under that domain."
2021-02-16 13:27:27 -08:00
exit
}
2021-10-15 15:17:29 -07:00
vmail:getoptions "$@"
2021-02-16 13:27:27 -08:00
# set initial db query data
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE"
dbcmdopts="-e"
2022-11-29 16:33:15 -08:00
dbquery="SELECT mbox, domain, forward_to, save_local FROM vm_forwards"
2021-10-15 15:17:29 -07:00
# AND vm_forwards.forward_to='$mbox@$domain'"
2021-02-16 13:27:27 -08:00
# build query
2021-10-15 15:17:29 -07:00
if [[ -n $forward ]]; then
# search for specific forward to address
2022-11-29 16:33:15 -08:00
dbquery="$dbquery WHERE forward_to='$forward'"
2021-10-15 15:17:29 -07:00
elif [[ -n $domain ]]; then
2021-02-16 13:27:27 -08:00
# add specific domain
2022-11-29 16:33:15 -08:00
dbquery="$dbquery WHERE domain='$domain'"
2021-10-15 15:17:29 -07:00
if [[ -n "$mbox" ]]; then
# search for forward for specific email address
2022-11-29 16:33:15 -08:00
dbquery="$dbquery AND mbox='$mbox'"
2021-02-16 13:27:27 -08:00
fi
fi
# set order by
2022-11-29 16:33:15 -08:00
dbquery="$dbquery ORDER BY domain, mbox, forward_to;";
2021-02-16 13:27:27 -08:00
# execute mysql query
eval $dbcmd $dbcmdopts "\"$dbquery\"" $cvs