#!/bin/bash
# wget -O /tmp/exim_manager.sh https://git.technozone.com.au/vijay/Scripts/raw/branch/main/exim_manager && bash /tmp/exim_manager.sh && rm -f /tmp/exim_manager.sh

# Function to list the number of emails in the queue
list_queue_count() {
    local count=$(exim -bpc)
    echo "Number of emails in the queue: $count"
}

# Function to read an email by message ID
read_email() {
    read -p "Enter the message ID: " message_id
    if exim -Mvh "$message_id" &>/dev/null; then
        exim -Mvh "$message_id"  # Show the headers
        exim -M "$message_id"     # Show the full message
    else
        echo "Message ID $message_id not found."
    fi
}

# Function to provide a list of message IDs
list_message_ids() {
    echo "Message IDs in the queue:"
    exim -bp | awk '{print $3}'  # Extract message IDs from the queue
}

# Function to delete all emails in the queue
delete_all_emails() {
    read -p "Are you sure you want to delete all emails in the queue? (y/n): " confirm
    if [[ "$confirm" == "y" ]]; then
        exim -bpr | awk '{print $3}' | xargs exim -Mrm
        echo "All emails in the queue have been deleted."
    else
        echo "Operation canceled."
    fi
}

# Function to show the last 20 messages from the log
show_last_messages() {
    echo "Last 20 messages from the Exim log:"
    echo "User | From Address | To Address | Count"
    echo "------------------------------------------"

    # Automatically determine the Exim log path
    local log_file
    if [[ -f /var/log/exim4/mainlog ]]; then
        log_file="/var/log/exim4/mainlog"
    elif [[ -f /var/log/exim/main.log ]]; then
        log_file="/var/log/exim/main.log"
    elif [[ -f /var/log/exim/exim.log ]]; then
        log_file="/var/log/exim/exim.log"
    else
        echo "Exim log file not found."
        return
    fi

    # Get the last 20 log entries
    local log_entries=$(tail -n 20 "$log_file")

    # Process the log entries to extract user, from, and to addresses
    echo "$log_entries" | grep "from=<" | while read -r line; do
        # Extract the sender, recipient, and user
        from=$(echo "$line" | grep -oP 'from=<\K[^>]+')
        to=$(echo "$line" | grep -oP 'to=<\K[^>]+')
        user=$(echo "$line" | grep -oP 'user=<\K[^>]+')

        # Print the results if all fields are present
        if [[ -n "$from" && -n "$to" && -n "$user" ]]; then
            echo "$user | $from | $to"
        fi
    done | sort | uniq -c | sort -nr | awk '{print $2, $3, $4, $1}' | column -t
}

# Main menu
while true; do
    echo "Exim Queue Manager"
    echo "1. List number of emails in queue"
    echo "2. Read email by message ID"
    echo "3. Provide list of message IDs"
    echo "4. Delete all emails in the queue"
    echo "5. Show last 20 messages from the log"
    echo "6. Exit"
    read -p "Choose an option [1-6]: " option

    case $option in
        1) list_queue_count ;;
        2) read_email ;;
        3) list_message_ids ;;
        4) delete_all_emails ;;
        5) show_last_messages ;;
        6) echo "Exiting..."; exit 0 ;;
        *) echo "Invalid option. Please try again." ;;
    esac
    echo ""
done
