Saturday, March 17, 2012

Mail Notification

I want to have a visual indicator for when I have unread mail in my inbox. I'm gonna use the SCROLL LOCK LED for this. For controlling the LED I'll use the ledcontrol package which consists of a daemon (ledd) and a few utilities like the ledcontrol command. These enable you to use your LEDs very effectively. There is also a ledcontrol-gtk package which is a graphical frontend for this program. It is useful for learning ledcontrol commands.

This is the code I'm currently using:
;; mail notification
(defvar *unread-mail-count* 0)
(defcommand check-mail () ()
  "Checks an email account and if there is unread mail in the inbox
makes the scroll lock led to slowly blink. Returns the number of
unread messages. Needs the ledcontrol command available and the ledd
daemon pipe file (/var/run/ledd-pipe by default) should be accessible
by normal users."
  (let ((sock (clonsigna:make-imap :host "imap.gmail.com" :port 993 :ssl-p t)))
    (unwind-protect
         (progn
           (clonsigna:cmd-connect sock)
           (clonsigna:cmd-login sock "username" "password")
           (clonsigna:cmd-select sock "inbox")

           ;; cmd-search will return something like (("* SEARCH 2461
           ;; 2465 2471")) The numbers are message ids, that's why we
           ;; split the string by spaces, count the parts and then
           ;; subtract two for the "* SEARCH" part
           (setf *unread-mail-count*
                 ( - (length
                      (cl-utilities:split-sequence
                       #\Space
                       (first
                        (clonsigna:cmd-search sock :criteria "UnSeen"))))
                     2))

           ;; if there is unread mail, have the scroll lock led blink
           ;; for one second every five seconds
           (if (zerop *unread-mail-count*)
               (run-shell-command "ledcontrol set s9 off")
               (run-shell-command "ledcontrol set s9 dutycycle 5000 1 100 20"))

           *unread-mail-count*)
      (clonsigna:cmd-close sock))))

(defvar *mail-notification-timer* (run-with-timer 1 120 'check-mail))
The check-mail function uses the clonsigna package (for IMAP).  When there is unread mail, it sets the SCROLL LOCK LED to blink for one second every five seconds. It also sets the global variable *unread-mail-count* to the number of unread messages.

StumpWM's run-with-timer function is used to have the function called every two minutes.

This simplistic approach is far from perfect. One major problem for me is that if I go to my mail program and read all the unread messages, it will take a while for the LED to stop blinking.

Another problem is that this is not how email clients show the unread mail notifications. They show the number of new unread messages. That is, if there are five unread messages, and you switch to your mail client but don't read them, the notification disappears. Next time when say, two new messages arrive, you are only notified of two new messages, not seven.

The obvious way to do this is to write a plugin for my mail client (currently Thunderbird), but I'm afraid I don't have the time (and interest) to read up on how I can do that, so I'll stick with this for now.

No comments:

Post a Comment

Better Console Font (in Arch)

In order to use Terminus as my console (tty) font, I simply installed the package terminus-font with pacman (includes both console and X11 ...