I'm pretty sure I took most of this from the hipchat plugin.  :)