Come ottenere notmuch message-id e thread-id da un nome file di messaggio maildir già presente nel notmuch db?
Supponiamo che io faccia una query non molto che restituisce i file:
$ notmuch search --output=files tag:inbox from:love
Questo restituisce un elenco di file, che puntano ai messaggi Maildir. Ora scelgo uno di questi file (già nel database notmuch), ad esempio with
FILENAME=$(notmuch search --output=files tag:inbox from:love | fzf)
e vorrei ottenere il suo message-id e thread-id nel database notmuch. Dalla variabile $FILENAME, vorrei trovare il message-id in notmuch.
Un modo molto sciatto per farlo è analizzare il file, leggere le intestazioni from/subject/date e fare una query non molto notmuch search from:{...} subject:{...} date:{..}
. Ma dal momento che i nomi dei file sono già memorizzati nel database, immagino che dovrebbe esserci un modo canonico e robusto per ottenere il messaggio-id dal nome del file.
Grazie!
Risposte
Non ho trovato alcun modo per cercare un database non molto basato sul nome del file maildir in NOTMUCH-SEARCH-TERMS(7)
Puoi ottenere Message-Id e notmuch thread id direttamente dalla tua ricerca iterando su Message-Ids:
for message_id in $(notmuch search --output=messages 'tag:inbox from:love')
do
thread_id=$(notmuch search --output=threads $message_id)
echo "$thread_id - $message_id"
done
Oppure puoi iterare sui thread e ottenere gli ID messaggio associati:
for thread_id in $(notmuch search --output=threads 'tag:inbox from:love')
do
# sed is here only to provide the output in the same format as in the first example
notmuch search --output=messages $thread_id | sed "s/^/$thread_id - /"
done
Qualunque cosa si adatti meglio alle tue esigenze. Entrambe le stampe for loop danno come risultato il seguente formato:
thread:THREAD_ID - id:MESSAGE_ID
…
Se vuoi ottenere le intestazioni From , Date , Subject puoi anche estrarlo direttamente dal database notmuch usando jq senza bisogno di analizzare il file maildir usando formail (1) o uno strumento simile.
notmuch search --format=json id:MESSAGE_ID | jq -r '.[].subject'
Alla fine ho trovato un modo attraverso i non molti attacchi Python, vedihttps://notmuch.readthedocs.io/projects/notmuch-python/en/latest/database.html?highlight=filename#notmuch.Database.find_message_by_filename
Una fodera funzionante è una bash
threadId=$(python3 -c "import notmuch; db = notmuch.Database(); print(db.find_message_by_filename('$FILENAME').get_thread_id())");
e il codice python3 decompresso è
import notmuch
db = notmuch.Database()
msg = db.find_message_by_filename('filename of the maildir message')
msg.get_thread_id()