Skip to content

Instantly share code, notes, and snippets.

@AzureKitsune
Created August 19, 2010 22:03
Show Gist options
  • Save AzureKitsune/539055 to your computer and use it in GitHub Desktop.
Save AzureKitsune/539055 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'socket'
# http://railsforum.com/viewtopic.php?id=10057 for string extension below
class String
def starts_with?(characters)
self.match(/^#{characters}/) ? true : false
end
end
class IRC
def initialize()
@prepend = "*"
end
def connect(server,port,nick)
@sock = TCPSocket.new(server,port)
@sock.puts("NICK #{nick}")
@sock.puts("USER #{nick} #{nick} #{server} :RubiBot")
thread = Thread.new() {
join("##XAMPP")
while 1
line = @sock.readline
puts ">> #{line}"
if line.starts_with?("PING :")
line["PING :"] = "PONG :"
sendraw(line)
else
splt = line.split(' ')
code = splt[1]
if code == "PRIVMSG"
channel = splt[2]
msg = line[line.rindex(':') +1..-1]
if channel == "##XAMPP"
if msg.starts_with?(@prepend)
msg = msg[1..-1]
if msg.starts_with?("say")
msg = msg[4..-1]
sendmsg(channel,msg)
end
end
end
end
end
end
}
end
def sendmsg(channel,msg)
sendraw("PRIVMSG #{channel} :#{msg}")
end
def sendraw(msg)
puts "<< #{msg}"
@sock.puts(msg)
end
def join(channel)
puts "Attempting to join #{channel}..."
sendraw("JOIN #{channel}")
end
end
if __FILE__ == $0
@irc = IRC.new()
@irc.connect("irc.freenode.net",6667,"RubiBot")
while 1
sleep(50)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment