brewersfan1976

irc-read.rb

Jul 2nd, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.92 KB | None | 0 0
  1. # get sockets from standard library
  2.  
  3. require 'socket'
  4.  
  5. # get hostname to connect to from the user
  6.  
  7. print "hostname: "
  8. hostname = gets.chomp
  9.  
  10. # get port to connect to from the user
  11.  
  12. print "port: "
  13. port = gets.chomp
  14.  
  15. # open the socket by connecting to the hostname and port
  16.  
  17. s = TCPSocket.open(hostname, port)
  18.  
  19. # send nickname to the server
  20.  
  21. s.puts "NICK linux1976\r\n"
  22.  
  23. # send user info to the server
  24.  
  25. s.puts "USER unbuntu76 8 * :linux\r\n"
  26.  
  27. # get channel to join from the user and join the channel
  28.  
  29. print "Enter channel to join: "
  30. channel = gets.chomp
  31. s.puts "JOIN #" + channel
  32.  
  33. # read text from the chat room and if the server sends ping we send PONG and a message to keep the connection alive
  34.  
  35. while line = s.gets
  36.       puts line.chop
  37.  
  38.       if line.include? "PING :" then
  39.          s.puts "PONG :Linux Rocks !!"
  40.          puts "PONG :Linux Rocks !!"
  41.       end
  42. end
  43.  
  44. # close socket when done
  45.  
  46. s.close
Advertisement
Add Comment
Please, Sign In to add comment