dbquery="SELECT vm_mboxes.mbox, vm_domains.domain, vm_mboxes.status, vm_mboxes.quota FROM vm_domains, vm_mboxes WHERE vm_domains.id=vm_mboxes.domain_id"
# check for and set email search term(s)
if[ -n "$1"];then
if[$1=="-h"];then
help
elif[[ ! $1=~ ^- ]];then
searchterm=$1
shift
if[[$searchterm=~ "@"]];then
mbox=${searchterm%@*}
domain=${searchterm##*@}
elif vmail::validate_domain $searchterm;then
domain=$searchterm
else
mbox=$searchterm
fi
fi
fi
# set any options that were passed
whilegetopts"chsw" opt;do
case"${opt}" in
c )
cvs="| sed 's/\t/,/g'"
;;
h )
help
exit;;
s )
dbcmdopts="-s -N $dbcmdopts"
;;
w )
wildcardsearch=true
;;
\?)
echo"Invalid option: $OPTARG" 1>&2
exit;;
esac
done
# build query
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 "$wildcardsearch"];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;";