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"
# set any options that were passed
whilegetopts"cfhs" opt;do
case"${opt}" in
c )
cvs="| sed 's/\t/,/g'"
;;
f )
forwardsearch=true
;;
h )
help
exit;;
s )
dbcmdopts="-s -N $dbcmdopts"
;;
\?)
echo"Invalid option: $OPTARG" 1>&2
exit;;
esac
done
# build query
if[ -n "$domain"];then
# add specific domain
dbquery="$dbquery AND vm_domains.domain='$domain'"
if[ -n "$mbox"];then
# search for specific alias or mbox
if[ -n "$forwardsearch"];then
# search for specific forward to address
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 AND vm_forwards.forward_to='$mbox@$domain'"
else
# search for forward for specific email address
dbquery="$dbquery AND vm_mboxes.mbox='$mbox'"
fi
fi
fi
# set order by
dbquery="$dbquery ORDER BY vm_domains.domain, vm_mboxes.mbox, vm_forwards.forward_to;";