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)
  …
  (9D=msgOffset)(9E=offlineMsgSize)
  (9F=ns:msg:db:row:scope:dbfolderinfo:all)
  (A0=ns:msg:db:table:kind:dbfolderinfo)(A1=numMsgs)(A2=numNewMsgs)
  (A3=folderSize)(A4=expungedBytes)(A5=folderDate)(A6=highWaterKey)
  (A7=mailboxName)(A8=UIDValidity)(A9=totPendingMsgs)
  (AA=unreadPendingMsgs)(AB=expiredMark)(AC=version)(AD=forceReparse)
  (AE=fixedBadRefThreading)(AF=onlineName)(B0=MRUTime)(B1=sortType)
  (B2=sortOrder)(B3=viewFlags)(B4=viewType)(B5=sortColumns)
  (B6=columnStates)(B7=highestModSeq)>
<(582=d7)(1748=5af2bafa)(81=0)(80=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
0
~/.thunderbird/<profile name>/ImapMail/<imap server 2>/INBOX.msf
1
~/.thunderbird/<profile name>/ImapMail/<imap server 3>/INBOX.msf
~/.thunderbird/<profile name>/ImapMail/<imap server 3>-1/INBOX.msf
6
~/.thunderbird/<profile name>/ImapMail/<imap server 4>/INBOX.msf
1292

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