
Untitled
By: a guest on
Apr 17th, 2012 | syntax:
None | size: 1.10 KB | hits: 11 | expires: Never
require "socket"
class IRCBot
def initialize(options={})
@server, @port, @channel, @nick, @password = options[:server], options[:port] || 6667, options[:channel], options[:nick], options[:password]
@socket = TCPSocket.open(@server, @port)
start
end
def connect
ircsend("USER #{@nick} #{@nick} #{@nick} :#{@nick}")
ircsend("NICK #{@nick}")
#ircsend("NS ID #{@password}") if password?
ircsend("JOIN #{@channel}")
end
def handle(s)
puts s
case s
when /^PING\s(?<host>.+)/
ircsend("PONG #{host}")
end
end
def ircsend(s)
@socket.print("#{s}\r\n")
puts "*** #{s}"
end
def password?
@password
end
def start
connect
loop do
ready = IO.select([@socket], nil, nil, 10)
next unless ready
ready.first.each do |s|
handle(@socket.gets.chomp) if @socket == s
return if @socket.eof?
end
end
end
end
IRCBot.new(
server: "irc.freenode.net",
nick: "Jimmy`",
password: "watwat",
channel: "#craiggles"
)