diff --git a/modules/email/email-diagnostics.sh b/modules/email/email-diagnostics.sh index 0fbb525..930ec88 100755 --- a/modules/email/email-diagnostics.sh +++ b/modules/email/email-diagnostics.sh @@ -27,40 +27,90 @@ echo "" # Ask what to check echo -e "${BOLD}What would you like to check?${NC}" echo "" -echo " 1) Specific email address (e.g., user@example.com)" -echo " 2) Entire domain (e.g., example.com)" +echo -e " ${CYAN}1)${NC} Specific email address (e.g., user@example.com)" +echo -e " ${CYAN}2)${NC} Entire domain (e.g., example.com)" echo "" -read -p "Enter choice [1]: " check_type -check_type=${check_type:-1} + +# Validate check_type input +while true; do + read -p "Enter choice [1]: " check_type + check_type=${check_type:-1} + + if ! [[ "$check_type" =~ ^[1-2]$ ]]; then + print_error "Invalid choice. Please enter 1 or 2" + continue + fi + break +done # Get email/domain to check echo "" -if [ "$check_type" = "2" ]; then - read -p "Enter domain to check (e.g., example.com): " target - search_pattern="@${target}" - check_label="domain $target" -else - read -p "Enter email address to check: " target - search_pattern="$target" - check_label="email $target" -fi -if [ -z "$target" ]; then - print_error "No email/domain provided" - exit 1 +if [ "$check_type" = "2" ]; then + # Domain input with validation + while true; do + read -p "Enter domain to check (e.g., example.com): " target + + # Validate domain format (basic check) + if [ -z "$target" ]; then + print_error "Domain cannot be empty" + continue + fi + + # Check for invalid characters (allow alphanumeric, dots, hyphens) + if ! [[ "$target" =~ ^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then + print_error "Invalid domain format. Use format like: example.com" + continue + fi + + search_pattern="@${target}" + check_label="domain $target" + break + done +else + # Email address input with validation + while true; do + read -p "Enter email address to check: " target + + # Validate email format (basic check) + if [ -z "$target" ]; then + print_error "Email address cannot be empty" + continue + fi + + # Check for valid email format (user@domain.com) + if ! [[ "$target" =~ ^[a-zA-Z0-9._+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then + print_error "Invalid email format. Use format like: user@example.com" + continue + fi + + search_pattern="$target" + check_label="email $target" + break + done fi # Time period to check echo "" echo "Check logs from:" -echo " 1) Last 1 hour" -echo " 2) Last 6 hours" -echo " 3) Last 24 hours (recommended)" -echo " 4) Last 48 hours" -echo " 5) Last week" +echo -e " ${CYAN}1)${NC} Last 1 hour" +echo -e " ${CYAN}2)${NC} Last 6 hours" +echo -e " ${CYAN}3)${NC} Last 24 hours (recommended)" +echo -e " ${CYAN}4)${NC} Last 48 hours" +echo -e " ${CYAN}5)${NC} Last week" echo "" -read -p "Enter choice [3]: " time_choice -time_choice=${time_choice:-3} + +# Validate time_choice input +while true; do + read -p "Enter choice [3]: " time_choice + time_choice=${time_choice:-3} + + if ! [[ "$time_choice" =~ ^[1-5]$ ]]; then + print_error "Invalid choice. Please enter 1-5" + continue + fi + break +done case "$time_choice" in 1) hours=1 ;; @@ -68,7 +118,6 @@ case "$time_choice" in 3) hours=24 ;; 4) hours=48 ;; 5) hours=168 ;; - *) hours=24 ;; esac echo ""