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...
#v+
#!/usr/bin/env python
import imaplib
class ImapCheckResult:
def __init__(self):
self.unseen_messages= 0
self.new_messages = 0
def __repr__(self):
return self.unseen_messages
def check_imap_folder(host, user, passwd, foldername):
result = ImapCheckResult()
imap = imaplib.IMAP4(host)
imap.login(user, passwd)
typ, data = imap.select(foldername, 1)
typ, data = imap.search(None, 'UNSEEN')
result.unseen_messages = len(data[0].split())
typ, data = imap.search(None, 'NEW')
result.new_messages = len(data[0].split())
imap.logout()
return result.unseen_messages
def NoMail():
f=file("/sys/class/leds/plug:green:health/trigger","w")
f.write("none")
f.close()
def SomeMail():
f=file("/sys/class/leds/plug:green:health/trigger","w")
f.write("default-on")
f.close()
def LotsOfMail():
f=file("/sys/class/leds/plug:green:health/trigger","w")
f.write("heartbeat")
f.close()
def main():
mailcount = check_imap_folder("localhost", "myusername", "mypassword", "INBOX")
#print "there is this many messages:", mailcount
if mailcount == 0:
NoMail()
elif mailcount <5:
SomeMail()
else:
LotsOfMail()
if __name__ == '__main__':
main()
#v-
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.
incrontab -e
/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.