My Ruby IRC Client

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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 
Last edited on
Topic archived. No new replies allowed.