Building an IRC bot in Ruby from scratch

Tamás Michelberger

Secret Sauce Partners, Inc.

  • Mostly for learning, fun and curiousity
  • Low-level networking in Ruby
  • IRC protocol

Internet Relay Chat

  • It's been with us since forever (actually 1988)
  • Text-based chat protocol
  • Client-server
  • Mostly standardized (RFC-1459, RFC-2812)

Anatomy of a message

:tmichel!~tmichel@example.com PRIVMSG
##budapestrb :some message\r\n

What do we actually need to run a bot?

  1. Open a TCP connection to an IRC server
  2. Register a user (NICK + USER)
  3. Wait for incoming messages

Let's build one

Using sockets in RUby

require "socket"

sock = TCPSocket.new(host, port)
while message = sock.gets
    puts message
end
sock.close

TCPSocket is an IO like object

  • gets
  • read
  • read_nonblock
    + IO.select
    
  • read*

Timeout is evil

Timeout.timeout(5) do
    # Do something with a socket
    # and now you have a race condition 
    # on your hands
end

Thanks!

Questions?

We are hiring!

REferences

Building an IRC bot in Ruby from scratch

By Tamás Michelberger

Building an IRC bot in Ruby from scratch

Building a simple IRC bot in Ruby.

  • 823