Had lots of problems getting reliable data on how many unread messages there were by reading the maildir directly (if my client was open, it would move them from /new to /cur).
In the end I moved to actually logging and and checking the unseen IMAP status. I did this by hacking some python code I found on the net: (excuse any newbie coding errors, python is a horrible language syntaxwise..)
It also has the advantage in that it can check many accounts, including gmail etc, and add up the total messages etc, with only basic hackery...
self.new_messages = 0
def check_imap_folder(host, user, passwd, foldername):
result = ImapCheckResult()
imap = imaplib.IMAP4(host)
typ, data = imap.select(foldername, 1)
typ, data = imap.search(None, 'UNSEEN')
result.unseen_messages = len(data.split())
typ, data = imap.search(None, 'NEW')
result.new_messages = len(data.split())
mailcount = check_imap_folder("localhost", "myusername", "mypassword", "INBOX")
#print "there is this many messages:", mailcount
if mailcount == 0:
elif mailcount <5:
if __name__ == '__main__':
To get the status LED updating correctly, without he needless cron-based polling of the IMAP status (which wakes everything up), I used incron, and got it to watch dovecot.index.log and then call my python script to reevaluate what needs changing.
/usb/mail/mark/Maildir/dovecot.index.log IN_MODIFY,IN_NO_LOOP python /root/unread.py
So far it's working well. When mail arrives, the Blue LED lights up, when I read them it goes out... Sweet... I'll monitor things over the next few days to see how things work.
A Side question, is it possible to control the GREEN LED? I can come up with plenty of other things to use that for, with regards to headless status reporting.