vmail-stack/bin/vmail-mboxes-get.sh
Matthew Saunders Brown f4ee4773ff added filter to output
2021-11-20 15:25:33 -08:00

78 lines
3.0 KiB
Bash
Executable File

#!/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 "Get email account data from vmail database."
echo ""
echo "usage: $thisfilename [-m <mbox>|-d <domain>|-e <email>] [OPTIONS]"
echo ""
echo " -h Print this help."
echo " -m <mbox> Return all email accounts, accross all domains, with <mbox> for the local part."
echo " -d <domain> Return all email accounts from specified domain."
echo " -e <email> Specific email address to search for."
echo " -c Output in cvs format."
echo " -t Use tabs instead of tables for output, do not display column headers."
echo " -v Include encrypt password in output."
echo " -g Wildcard (blog) search when searching for username."
echo ""
echo " Search term is optional. If nothing specified all email acccounts for all domains will be returned."
echo " By default username searches are for exact matchs."
echo " Specify -g to turn them in to a wildcard (glob) search. Examples:"
echo " $thisfilename -e joe@example.com # search for specific email address 'joe@example.com'."
echo " $thisfilename -m joe # search for username 'joe' in all domains."
echo " $thisfilename -m joe -g # search for usernames containing 'joe' in all domains."
echo " $thisfilename -d example.com # search for all usernames in the 'example.com' domain."
exit
}
vmail:getoptions "$@"
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE"
dbcmdopts="-e"
if [[ -n $tab ]]; then
dbcmdopts="-s -N $dbcmdopts"
fi
# build query
dbquery="SELECT vm_mboxes.mbox, vm_domains.domain"
if [ -n "$verbose" ]; then
dbquery="$dbquery, vm_mboxes.passwd"
fi
dbquery="$dbquery, vm_mboxes.status, vm_mboxes.quota, vm_mboxes.filter FROM vm_domains, vm_mboxes WHERE vm_domains.id=vm_mboxes.domain_id"
if [ -n "$mbox" ] && [ -n "$domain" ]; then
# search for specific email address
dbquery="$dbquery AND vm_domains.domain='$domain' AND vm_mboxes.mbox='$mbox'"
elif [ -n "$mbox" ] && [ -z "$domain" ]; then
# search all domains for username
if [ -n "$glob" ]; then
# wildcard search
dbquery="$dbquery AND vm_mboxes.mbox LIKE '%$mbox%'"
else
# exact match
dbquery="$dbquery AND vm_mboxes.mbox='$mbox'"
fi
elif [ -z "$mbox" ] && [ -n "$domain" ]; then
# get all usernames for domain
dbquery="$dbquery AND vm_domains.domain='$domain'"
# elif [ -z "$mbox" ] && [ -z "$domain" ]; then
# echo "ERROR: No username or domain specified."
# help
# uncomment above 3 lines to force search term, otherwise all email addresses for all domains will be returned
fi
# set order by
dbquery="$dbquery ORDER BY vm_domains.domain, vm_mboxes.mbox;";
# execute mysql query
eval $dbcmd $dbcmdopts "\"$dbquery\"" $cvs