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

65 lines
2.1 KiB
Bash
Raw Permalink Normal View History

2021-02-10 16:16:23 -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-10 16:16:23 -08:00
2021-04-02 12:02:50 -07:00
# load include file
source $(dirname $0)/vmail.sh
2021-02-10 16:16:23 -08:00
help()
{
thisfilename=$(basename -- "$0")
echo "$thisfilename"
echo "Get email alias data from vmail database."
echo ""
2021-10-15 15:17:29 -07:00
echo "usage: $thisfilename [-a <alias>|-e <email>|-d <domain>] [-c] [-t] [-h]"
2021-02-10 16:16:23 -08:00
echo ""
2021-10-15 15:17:29 -07:00
echo " -h Print this help."
2021-02-10 16:16:23 -08:00
echo " -a Return info for specific alias."
2021-10-15 15:17:29 -07:00
echo " -e Return all aliases for specified email address."
echo " -d Return all aliases for specified domain."
2023-11-27 09:13:59 -08:00
echo " -c Output in csv format."
2021-10-15 15:17:29 -07:00
echo " -t Use tabs instead of tables for output, do not display column headers."
2021-02-10 16:16:23 -08:00
echo ""
echo " Search term is optional. If nothing specified all aliases for all email acccounts for all domains will be returned."
echo " Enter email address to get all aliases that route to that email address."
echo " Enter alias (in full email address format) with the -a option to get address that alias delivers to."
echo " Enter domain name to get all aliases for all email addresses under that domain."
exit
}
2021-10-15 15:17:29 -07:00
vmail:getoptions "$@"
2021-02-10 16:16:23 -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 alias, mbox, domain FROM vm_aliases"
2021-10-15 15:17:29 -07:00
if [[ -n $tab ]]; then
dbcmdopts="-s -N $dbcmdopts"
fi
2021-10-15 15:17:29 -07:00
if [[ -n $alias ]]; then
if [[ -n $domain ]] ; then
2022-11-29 16:33:15 -08:00
dbquery="$dbquery WHERE alias='$alias' AND domain='$domain'"
2021-10-15 15:17:29 -07:00
else
echo "ERROR: alias should be in full email address format."
exit 1
2021-02-10 16:16:23 -08:00
fi
2021-10-15 15:17:29 -07:00
elif [[ -n $email ]]; then
2022-11-29 16:33:15 -08:00
dbquery="$dbquery WHERE mbox='$mbox' AND domain='$domain'"
2021-10-15 15:17:29 -07:00
elif [[ -n $domain ]]; then
2022-11-29 16:33:15 -08:00
dbquery="$dbquery WHERE domain='$domain'"
2021-10-15 15:17:29 -07:00
else
echo "You must specify either <alias> or <email> or <domain>."
exit 1
2021-02-10 16:16:23 -08:00
fi
# set order by
2022-11-29 16:33:15 -08:00
dbquery="$dbquery ORDER BY domain, mbox, alias;";
2021-02-10 16:16:23 -08:00
# execute mysql query
2023-11-27 09:13:59 -08:00
eval $dbcmd $dbcmdopts "\"$dbquery\"" $csv