November 14, 2008

Posted by John

Tagged api and sinatra

Older: Creating and Integrating Bookmarklets with Rails

Newer: HappyMapper, Making XML Fun Again

Sinatra for IRC

My favorite Campfire feature is the history. I love that it persists between Campfire sessions. On the opposite side of the spectrum, I hate that IRC doesn’t. I decided it would be nice to have a bot that sits in the room and stores all the chatter to the database. Once that was working, it would be handy to have a simple web front end that allowed searching and viewing by day. I have seen sites out there that do that but like any programmer decided to make something for myself.

Finding Some Code

First up, I needed to find a good IRC library for Ruby. I did research for about 30 minutes and found a ton of libraries on GitHub: Net/IRC, on_irc, irc4r, irkr, rbot, minibot, kirby and autumn. I even came across the code that runs rails.loglibrary.com, which is kind of what I want to do but not quite. Each library did things a bit differently and they all had their strengths and weaknesses, but one thing stood out: they almost all required that I was familiar with IRC protocols and terminologies.

The Solution

If I was getting paid to do this, I would want to know inside and out how things work. Because this is a side project, I just wanted to get up and running quickly. At that moment, I remembered SearchParty (which incidentally runs on Sinatra) and headed over there to search for irc. Right at the top of my delicious items was Isaac, which I had totally forgotten about.

“You want to create an IRC bot quickly? Then Isaac is you.”

Yes, I want to create an IRC bot quickly. That is exactly what I want to do.

“Be aware…a large portion of the IRC standard has not been implemented, simply because I haven’t needed it yet.”

I don’t care if the entire spec is implemented. All I care about is whether or not the stuff I want is implemented and what the API is in Ruby to do that stuff.

The Code

A few minutes later, I had the following snippet of code running which allowed my bot to login to an IRC room and listen to all the chatter. Wow. Awesome.


require 'rubygems'
require 'isaac'

config do |c|
  c.nick    = "fantasticalbot"
  c.server  = "irc.freenode.net"
  c.port    = 6667
end

on :connect do
  join "#fantasticalroom"
end

on :channel, /.*/ do
  msg channel, "Got your message #{nick} (#{match[0]})"
end

If you have used Sinatra, you can immediately see the similarities. You can even define helpers in the same way that you would with Sinatra. Now this doesn’t do everything I mentioned that I want to do, but it was a nice start. All that is left is to drop in ActiveRecord or DataMapper or implement some kind of a web hook to an app and I am good to go.

Conclusion

There are two points to this article. The first is that Isaac is really cool if you want to dabble with IRC from Ruby. The second point, and probably more important, is when you create a library, think about the API that other programmers will use more than you think about the spec you are implementing. I suspect few people out there want to learn every spec that they work with. Most people are probably like me and have a simple need and want a simple solution.

This is one of the reasons HTTParty was created and why I am now working on IMAParty. Spec is important, but every “to the spec” library needs a simplistic DSL on top that makes them easy to use for the 80% of people that only need 20% of the features.

4 Comments

  1. I started looking at isaac after you suggested it on twitter yesterday. And blogged about it today . Funny how that worked out :)

  2. @Josh Nice! Thanks for linking that up.

  3. Nice write-up. I took this as an opportunity to fix the gems. Both GitHub and Rubyforge gems should be available now.

  4. @Harry – Sweet. Thanks for fixing those. I’ll update the article when I get a chance.

Sorry, comments are closed for this article to ease the burden of pruning spam.

About

Authored by John Nunemaker (Noo-neh-maker), a web developer and programmer who has fallen deeply in love with Ruby. More about John.

Syndication

Feed IconRailsTips Articles - An assortment of howto's and thoughts on Ruby and Rails.

Feed IconRails Quick Tips - Ruby and Rails related links.