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

57 lines
2.2 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
# MIT License Copyright (c) 2021 Matthew Saunders Brown
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-05-23 09:30:18 -07:00
echo "usage: $thisfilename [-e email|-f forward|-f 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"
dbquery="SELECT vm_mboxes.mbox, vm_domains.domain, vm_forwards.forward_to, vm_forwards.save_local FROM vm_forwards, vm_mboxes, vm_domains WHERE vm_forwards.mbox_id = vm_mboxes.id AND vm_mboxes.domain_id = vm_domains.id"
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
dbquery="$dbquery AND vm_forwards.forward_to='$mbox@$domain'"
elif [[ -n $domain ]]; then
2021-02-16 13:27:27 -08:00
# add specific domain
2021-10-29 15:16:55 -07:00
dbquery="$dbquery AND vm_domains.domain='$domain'"
2021-10-15 15:17:29 -07:00
if [[ -n "$mbox" ]]; then
# search for forward for specific email address
dbquery="$dbquery AND vm_mboxes.mbox='$mbox'"
2021-02-16 13:27:27 -08:00
fi
fi
# set order by
dbquery="$dbquery ORDER BY vm_domains.domain, vm_mboxes.mbox, vm_forwards.forward_to;";
# execute mysql query
eval $dbcmd $dbcmdopts "\"$dbquery\"" $cvs