Basti’s Buggy Blog

Poking at software.

Extracting Thunderbird Unread Emails Count

I wanted to display my unread email count in my i3bar.

  • I use Thunderbird as mail client
  • I don’t want my i3bar to query some IMAP servers

Thunderbird Persistence

  • Thunderbird stores all its configuration in ~/.thunderbird in different files.
  • The folder <profile name>/ImapMail contains non-binary *.msf files
  • The header of the files conveniently contains a description of the values
// <!-- <mdb:mork:z v="1.4"/> -->
< <(a=c)> // (f=iso-8859-1)
  • The field A2 contains the number of new (unread) messages in hexadecimal representation

Bash Magic

find ~/.thunderbird/<profile name>/ImapMail -name 'INBOX*.msf' -exec \
	sh -c " \
		echo '{}' && \
		grep '(^A2=' '{}' | \
		tail -n1 | \
		sed -r 's/.*\(\^A2=(\w+)\).*/\1/' | \
		xargs -n1 -L1 --replace=__ printf '%d\n' '0x__'" \;

This “neat” “little” bash command does the following:

  • Iterate over all INBOX*.msf files
  • Look for the last occurrence of the A2 field
  • Extract the value of the A2 field
  • Convert the value into decimal representation

It produces an overview that looks like this:

~/.thunderbird/<profile name>/ImapMail/<imap server 1>/INBOX.msf
~/.thunderbird/<profile name>/ImapMail/<imap server 2>/INBOX.msf
~/.thunderbird/<profile name>/ImapMail/<imap server 3>/INBOX.msf
~/.thunderbird/<profile name>/ImapMail/<imap server 3>-1/INBOX.msf
~/.thunderbird/<profile name>/ImapMail/<imap server 4>/INBOX.msf

Those numbers correspond to the unread count of the Thunderbird GUI.