• Home
  • Help
  • Search
  • Login
  • Register
Pages: [1]
Author Topic: Using the Sheevaplug Blue LED to notify of new mail.  (Read 7674 times)
mgillespie
Full Member
***

Karma: 8
Posts: 239



View Profile
« on: October 03, 2009, 05:56:00 AM »

Hi, I recently moved to my mail to my sheevaplug using postfix/dovecot/spamassassin combination, and it's working very well indeed.

One last tweak is that I want to is get is the LED on the sheevaplug to light up when I have unread mail in my inbox. I have the shell scripts working to turn the LED on and off, but I am wondering how I can integrate it with dovecots unread mail count. I have a dovecot sieve that filters out my spam and marks it as read, so I want my mail notifications to truly work on unread mail count, so that kinda excludes integrating it at the postfix stage. Any ideas? Can I run a cronjob that interrogates dovecots unread count every 10 minutes or so? Ideas, solutions?Huh

I am using Maildir format and it seems I can look at the directory and see if the "new" directory has any files in it.   Does this seem a reasonably robust solution?

I am also thinking it's not great to poll this via a cron job, but use inotify to do  the job.  It seems many languages can interface with inotify, but which would be the best to achieve this simple job?
Logged

mgillespie
Full Member
***

Karma: 8
Posts: 239



View Profile
« Reply #1 on: October 03, 2009, 07:19:39 AM »

This is where I am.   I have my bash script setup (but having some problems getting reliable unread message counts).

Code:
#!/bin/sh
messagecount=`find /usb/mail/mark/Maildir/new -type f | wc -l`

case $messagecount in
  0)
    echo "none" > /sys/class/leds/plug\:green\:health/trigger
    ;;
  [1-5])
    echo "default-on" > /sys/class/leds/plug\:green\:health/trigger
    ;;
  *)
    echo "heartbeat" > /sys/class/leds/plug\:green\:health/trigger
    ;;
esac

This will turn the blue LED off if there is no mail, turn it on steady if there is 5 or less messages, and make it flash if there are more than 5.   This part works.

I have hooked the script up using incron which is watching for changes in my mail dir too:

/usb/mail/mark/Maildir/new/ IN_MODIFY /usr/sbin/mailstatus.sh

Just trying to work out a couple of quirks..
Logged

DamonHD
Full Member
***

Karma: 4
Posts: 169


View Profile WWW
« Reply #2 on: October 03, 2009, 08:11:41 AM »

Nice idea... I'm now indicating power-status (ie how full my battery is)...

Rgds

Damon
Logged

mgillespie
Full Member
***

Karma: 8
Posts: 239



View Profile
« Reply #3 on: October 03, 2009, 12:23:04 PM »

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...

Code:
#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.

Code:
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.
« Last Edit: October 30, 2009, 11:20:46 AM by mgillespie » Logged

mgillespie
Full Member
***

Karma: 8
Posts: 239



View Profile
« Reply #4 on: October 08, 2009, 04:19:38 AM »

small update.  This is working fantastically, 100% reliable.   The only further tweak, is to turn the blue LED off on bootup..

I had the add the following to my /etc/rc.local

Code:
echo "none" > /sys/class/leds/plug\:green\:health/trigger
(just before the exit 0)
Logged

mgillespie
Full Member
***

Karma: 8
Posts: 239



View Profile
« Reply #5 on: May 06, 2012, 03:27:02 PM »

small update to this.   This has been bulletproof for many years.   However I recently updated my plug to a new kernel and new Debian install, and this stopped working.    It's because it finally uses the correct LED labels in the kernel now...   The Blue LED's are referenced by blue in the script...

echo "none" > /sys/class/leds/plug\:blue\:health/trigger

Just incase someone else uses this and wonders why it stopped working...
Logged

Pages: [1]
Print
Jump to: