Saturday, November 1, 2008

my first emacs extension - A google mail notifier for Jabber

Google talk's XMPP protocol has a few nice extensions to the standard XMPP, one of them being new mail notifications. So for all those google talk lovers, now you can receive email notification, using my new emacs extension (very-very-easy-to-install):
http://atlantisbangalore.selfip.com/~joel/projects/jabber-mail-notify.html

Hacking an Art

Hacking is an art, where you take existing stuff, reuse it, add your own stuff to it, read docs, manuals whatever you can get your hands on, look for pieces of information and code examples that are critical to solving your problem or accomplishing your objective. Most important of all, having fun while doing it!

For the emacs jabber extension, the following helped me a great deal:
- The Emacs jabber docs
- Google talk's XMPP extensions
- The most powerful development environment (Emacs !!)
- A knowledge of Common Lisp

The power of emacs as an emacs-lisp development environment.

The fact that emacs allows you to eval code that changes its behavior on the fly while its running is simply a big win. Its simply something that has led to its success, and the huge emacs code base that there is today.

Bottom Up programming approach.

For every function that I wrote, I was able to test whether it did what it was supposed to by evaluating it at point. (C-x C-e). That way, I was confident as I wrote the program, that I wouldn't need to go back and debug it. I even forgot about the code as I wrote it, and as it got progressively more complicated. The reason I found it okay to forget about the code I had already written was I wrote the extension using a bottom up programming style.

The 2 pieces of the puzzle

I independently worked on 2 separate parts of the extension.
1. Getting google to send me notifications and understanding the response.
Fetching new unread mail.
2. Parsing google's XML response and formatting it for display.

The first part: The first part was easy thanks to the dynamic emacs programming environment, which allows you to eval code on the fly. By writing dummy samples of code, I was able to understand how the whole XMPP communication works, the data that google sends to me and that it expects me to respond with. I was able to test my functions, on the fly and see google's jabber server response in the emacs jabber xml logs. It felt like a pilot, pushing the joysticks and seeing the plane respond. That is what I call fun!!

The second part: Fetching the data was a small part of the deal. The XML returned by google is fairly complex, and I had before me the myriad task of parsing that huge bunch of gibberish thrown at my screen! I will spare the details, but trust me it really looked ugly. :-) Lisp being a functional language - bottom-up programming is natural and easy, and perfectly suited for the parsing problem at hand!
For parsing the email messages, I wrote functions to extract the subject, the sender-list, the body, etc, and these were inturn built using smaller functions. Bringing all these methods together, I was able to write complex working code in just a few hours. Testing and evaluating each function as I wrote it, part by part, building bigger ones from the smaller ones, and making sure that it worked at every step (oh its really peaceful when you're confident that what you've written so far works)

Once these 2 major parts of the extension were written and tested, all I had to do was connect the dots. And yes, as you expect, it worked the first time. These little programming experience make me a better programmer, and that is the whole motivation behind these adventures, besides the fun ofcourse.

1 comments:

Abhijith said...

Totally agree... Hacking is all about fun and I can see from your post that you had a great time building it!