DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Ruby IRC Socket

04.26.2010
| 1795 views |
  • submit to reddit
        I simple IRC socket using the irc-socket gem. Documentation available at http://rdoc.injekt.net/irc-socket

require 'rubygems'
require 'irc-socket'

IRCSocket.new("irc.myserver.net") do |irc|
  irc.nick "mrbot"
  irc.user "mrbot", 0, "*", "irc-socket bot"

  while line = irc.read
    data = line.split

    if data[0] == 'PING'
      irc.pong data[1]
    end

    # Wait until end of motd before attempting to join
    if data[1] == '376'
      irc.join "#channel"
      irc.privmsg "#channel", "Hi there!"
      irc.privmsg "mynick", "Hi there in private message"
      irc.notice "#channel", "Notice message!"
      irc.part "#channel"
    end
  
    puts line
  end
end