#!/bin/bash
# mail2news - Script interattivo per comporre e inviare post Usenet via Tor
#
# Flusso: Newsgroup → Subject → [References se Re:] → Messaggio → Revisione → Invio
#
# Dipendenze: torsocks, netcat (openbsd-netcat o ncat), vim, gpg
# Il mittente fisso e la chiave ECC sono configurati in accounts/mail2news
#
# Configurazione — adatta questi valori al tuo setup:
# ----------------------------------------------------------
SMTP_ONION="MAIL2NEWS_ONION_ADDRESS.onion"   # indirizzo .onion del server mail2news
SMTP_PORT="25"
MAIL2NEWS_TO="mail2news@MAIL2NEWS_ONION_ADDRESS.onion"  # destinatario mail2news
FROM_ADDR="anon@anonymous.invalid"           # deve corrispondere a accounts/mail2news
FROM_NAME="Anonymous"
GPG_KEY="FINGERPRINT_CHIAVE_ECC_MAIL2NEWS"  # fingerprint chiave ECC per firmare
SIGNATURE="-- \nAnonymous"                   # firma in calce al post
# ----------------------------------------------------------

clear
echo "========================================"
echo "        MAIL2NEWS - Nuovo Post"
echo "========================================"
echo

# 1. Newsgroup (supporta multipli separati da virgola)
echo -n "Newsgroup(s) (separa con virgola per crosspost): "
read NEWSGROUP

if [ -z "$NEWSGROUP" ]; then
    echo "Errore: newsgroup obbligatorio"
    exit 1
fi

# Rimuovi spazi attorno alle virgole
NEWSGROUP=$(echo "$NEWSGROUP" | sed 's/ *, */,/g')

# 2. Subject
echo -n "Subject: "
read SUBJECT

if [ -z "$SUBJECT" ]; then
    echo "Errore: subject obbligatorio"
    exit 1
fi

# 3. Se inizia con Re: chiedi References
REFERENCES=""
if [[ "$SUBJECT" =~ ^[Rr][Ee]:* ]]; then
    echo -n "References (Message-ID del post originale, es. <id@host>): "
    read REFERENCES
fi

# 4. Corpo del messaggio
MSGFILE=$(mktemp /tmp/m2n-msg.XXXXXX)
printf "\n\n%b\n" "$SIGNATURE" > "$MSGFILE"

echo
echo "Scrivi il messaggio (si aprirà vim)..."
echo "Premi INVIO per continuare..."
read

vim "$MSGFILE"

# 5. Costruisci il draft completo
DRAFTFILE=$(mktemp /tmp/m2n-draft.XXXXXX)

cat > "$DRAFTFILE" << EOF
To: $MAIL2NEWS_TO
From: $FROM_NAME <$FROM_ADDR>
Subject: $SUBJECT
Newsgroups: $NEWSGROUP
EOF

if [ -n "$REFERENCES" ]; then
    echo "References: $REFERENCES" >> "$DRAFTFILE"
fi

echo "" >> "$DRAFTFILE"
cat "$MSGFILE" >> "$DRAFTFILE"

# 6. Firma GPG/ECC (clearsign inline — compatibile con Usenet)
if [ -n "$GPG_KEY" ] && [ "$GPG_KEY" != "FINGERPRINT_CHIAVE_ECC_MAIL2NEWS" ]; then
    SIGNED_FILE=$(mktemp /tmp/m2n-signed.XXXXXX)
    # Firma solo il body (dopo la riga vuota degli header)
    BODY_START=$(grep -n "^$" "$DRAFTFILE" | head -1 | cut -d: -f1)
    HEADER_PART=$(head -n "$BODY_START" "$DRAFTFILE")
    BODY_PART=$(tail -n +"$((BODY_START + 1))" "$DRAFTFILE")

    SIGNED_BODY=$(echo "$BODY_PART" | gpg --clearsign --default-key "$GPG_KEY" 2>/dev/null)
    if [ $? -eq 0 ]; then
        echo "$HEADER_PART" > "$SIGNED_FILE"
        echo "" >> "$SIGNED_FILE"
        echo "$SIGNED_BODY" >> "$SIGNED_FILE"
        DRAFTFILE="$SIGNED_FILE"
        echo "[GPG] Messaggio firmato con chiave $GPG_KEY"
    else
        echo "[GPG] Attenzione: firma fallita, invio non firmato"
    fi
fi

# 7. Revisione
clear
echo "========================================"
echo "        REVISIONE EMAIL"
echo "========================================"
echo
cat "$DRAFTFILE"
echo
echo "========================================"
echo "[e] Modifica  [s] Spedisci  [q] Annulla"
echo -n "Scelta: "
read CHOICE

case "$CHOICE" in
    e|E)
        vim "$DRAFTFILE"
        echo
        echo "[s] Spedisci  [q] Annulla"
        echo -n "Scelta: "
        read CHOICE2
        if [[ "$CHOICE2" != "s" && "$CHOICE2" != "S" ]]; then
            echo "Annullato."
            rm -f "$MSGFILE" "$DRAFTFILE"
            exit 0
        fi
        ;;
    q|Q)
        echo "Annullato."
        rm -f "$MSGFILE" "$DRAFTFILE"
        exit 0
        ;;
esac

# 8. Invia via SMTP raw su Tor con torsocks + netcat
echo
echo "Invio in corso via Tor..."

{
    sleep 1
    echo "EHLO localhost"
    sleep 1
    echo "MAIL FROM:<$FROM_ADDR>"
    sleep 1
    echo "RCPT TO:<$MAIL2NEWS_TO>"
    sleep 1
    echo "DATA"
    sleep 1
    cat "$DRAFTFILE"
    echo ""
    echo "."
    sleep 1
    echo "QUIT"
} | torsocks nc -w 30 "$SMTP_ONION" "$SMTP_PORT" > /tmp/m2n-smtp.log 2>&1

if grep -q "250" /tmp/m2n-smtp.log; then
    echo "Messaggio inviato a: $NEWSGROUP"
    cat /tmp/m2n-smtp.log
else
    echo "Errore invio. Log SMTP:"
    cat /tmp/m2n-smtp.log
    echo ""
    echo "Draft salvato in: $DRAFTFILE"
    exit 1
fi

# Cleanup
rm -f "$MSGFILE"
echo
echo "Premi INVIO per tornare a neomutt..."
read
