#!/bin/bash
# Mail2News - Script interattivo per comporre post Usenet
# Flusso: Newsgroup → Subject → [References se Re:] → Messaggio → Revisione → Invio

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 (misc.test, alt.test → misc.test,alt.test)
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): "
    read REFERENCES
fi

# 4. Componi messaggio
MSGFILE=$(mktemp /tmp/m2n-msg.XXXXXX)
echo "" > "$MSGFILE"
echo "-- " >> "$MSGFILE"
echo "Gabx" >> "$MSGFILE"

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

/home/gabriel1/bin/vim-yubisigner "$MSGFILE"

# 5. Indirizzo mail2news semplice (senza data)
TO="mail2news@xilb7y4kj6u6qfo45o3yk2kilfv54ffukzei3puonuqlncy7cn2afwyd.onion"

# 6. Crea draft completo per revisione
DRAFTFILE=$(mktemp /tmp/m2n-draft.XXXXXX)

cat > "$DRAFTFILE" << EOF
To: $TO
From: Gabx <mail2news@virebent.invalid>
Subject: $SUBJECT
EOF

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

echo "Newsgroups: $NEWSGROUP" >> "$DRAFTFILE"
echo "" >> "$DRAFTFILE"
cat "$MSGFILE" >> "$DRAFTFILE"

# 7. Mostra e permetti modifica
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
        ;;
    s|S)
        # continua sotto
        ;;
    *)
        # default: spedisci
        ;;
esac

# 8. Invia via sendmail/msmtp o salva per neomutt
# Generiamo il file per neomutt
echo "set my_m2n_draft = \"${DRAFTFILE}\"" > /tmp/m2n.rc
echo "set my_m2n_to = \"${TO}\"" >> /tmp/m2n.rc
echo "set my_m2n_subj = \"${SUBJECT}\"" >> /tmp/m2n.rc
if [ -n "$REFERENCES" ]; then
    echo "my_hdr References: ${REFERENCES}" >> /tmp/m2n.rc
fi
echo "my_hdr Newsgroups: ${NEWSGROUP}" >> /tmp/m2n.rc

# Salva draft per invio
cp "$DRAFTFILE" /tmp/m2n-final.eml

echo
echo "Email pronta. Invio in corso..."

# Invia via swaks attraverso Tor (gestisce correttamente la sincronizzazione SMTP)
SMTP_HOST="qee4i7sags6phsvb2yodwecfj7noimfhhalsjktsvikrwotxzis3raad.onion"
SMTP_PORT="25"

# SECURITY: Strip Message-ID to force server-side generation (anonymity)
CLEANFILE=$(mktemp /tmp/m2n-clean.XXXXXX)
grep -v "^Message-ID:" "$DRAFTFILE" > "$CLEANFILE"

torsocks swaks \
    --server "${SMTP_HOST}:${SMTP_PORT}" \
    --from "mail2news@virebent.invalid" \
    --to "${TO}" \
    --data "@${CLEANFILE}" \
    --timeout 60 \
    > /tmp/m2n-smtp.log 2>&1

rm -f "$CLEANFILE"

if grep -q "250 .*[Qq]ueued\|250 2.0.0 Ok" /tmp/m2n-smtp.log; then
    echo "✓ Messaggio inviato a $NEWSGROUP"
    cat /tmp/m2n-smtp.log
else
    echo "✗ Errore invio. Log:"
    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
