brewersfan1976

irc-write.rb

Jul 2nd, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.17 KB | None | 0 0
  1. # get sockets from standard library
  2.  
  3. require 'socket'
  4.  
  5. # get timeout from library to leave input and check for ping
  6.  
  7. require 'timeout'
  8.  
  9. # get hostname to connect to from the user
  10.  
  11. print "hostname: "
  12. hostname = gets.chomp
  13.  
  14. # get port to connect to from the user
  15.  
  16. print "port: "
  17. port = gets.chomp
  18.  
  19. # open the socket by connecting to the hostname and port
  20.  
  21. s = TCPSocket.open(hostname, port)
  22.  
  23. # send nickname to the server
  24.  
  25. s.puts "NICK ubuntu76\r\n"
  26.  
  27. # send user info to the server
  28.  
  29. s.puts "USER unbuntu76 8 * :linux\r\n"
  30.  
  31. # get channel to join from the user and join the channel
  32.  
  33. print "Enter channel to join: "
  34. channel = gets.chomp
  35. s.puts "JOIN #" + channel
  36.  
  37. puts "Enter text to send to channel: "
  38.  
  39. # write text to the chat room and if the server sends ping we send PONG and a message to keep the connection alive
  40.  
  41. while (1)
  42.     begin
  43.         input = ""
  44.  
  45.         Timeout::timeout(240) do
  46.            s.puts "PONG :Linux Rocks !!"
  47.            puts "PONG :Linux Rocks !!"
  48.            input = gets.chomp
  49.         end
  50.    
  51.         s.puts "PRIVMSG #" + channel + " :" + input
  52.         rescue Timeout::Error
  53.     end
  54. end
  55.  
  56. # close socket when done
  57.  
  58. s.close
Advertisement
Add Comment
Please, Sign In to add comment