Well this is my latest creation in Ruby. Its an IRC client. It uses Ruby's multi-threading feature to display messages and to take commands from the user (you can type in any commands like: JOIN #somechannel ). Actually its not designed as an IRC client but more like a bot and my experiment with Ruby's socket, networking and multi-threading features. So please check it out and comment :D :D :D :D.
require 'socket'
#IRC Class
class IRC
def initialize(server, port) #Take server and port as parameter when creating instances
@server = server
@port = port
end
def connect() #Connect using the server and port
@cn = TCPSocket.open(@server, @port)
end
def requestNick(nick) #Request nick using the parameter as nick
@nick = nick
nickRequest = "NICK #{@nick}\r"
@cn.puts("#{nickRequest}")
end
def requestUser(user)
@user = user
userRequest = "USER #{user}\r"
@cn.puts("#{userRequest}")
end
def sendCommand(command) #Send a command to the server using parameter as command
@cn.puts("#{command}\r")
end
def disconnect() #Disconnect from the server
@cn.close
end
def getData() #Get the data from server and display it, if there is PING send a PONG back
while line = @cn.gets
if line.split(" ")[0] == "PING" then
sendCommand("PONG #{line.split("")[1]}")
puts "Pong sent on ping from server!"
end
puts line
end
end
end
#Create a new IRC object and connect to it
quake = IRC.new("irc.quakenet.org", 6667) #To connect to a different server you need to change this
quake.connect()
quake.requestNick("StormboysBot") #Change this for a different nickname
quake.requestUser("RubyScriptTest RubyScriptTest RubyScriptTest :Ruby IRC")
#Thread displaying messages
Thread.new{
quake.getData()
}
#Main thread sending commands(from user)
command = ""while(command != "quit") #Break if the command is "quit"
command = gets.chomp
if command != "quit" then
quake.sendCommand(command)
end
end
#Disconnect from the server
quake.disconnect()
puts "Press ENTER to exit..."
gets() #Wait for user to press ENTER