brewersfan1976

irc-write-new.rb

Feb 28th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.52 KB | None | 0 0
  1. # IRC Write Program
  2. # Written by Bryan Miller
  3.  
  4. # get sockets from standard library
  5.  
  6. require 'socket'
  7.  
  8. # get SSL sockets from standard library
  9.  
  10. require 'openssl'
  11.  
  12. # get timeout from library to leave input and check for ping
  13.  
  14. require 'timeout'
  15.  
  16. # get hostname to connect to from the user
  17.  
  18. print "hostname: "
  19. hostname = gets.chomp
  20.  
  21. # for SSL port is always 6697
  22.  
  23. port = 6697
  24.  
  25. # open the socket by connecting to the hostname and port
  26.  
  27. s = TCPSocket.open(hostname, port)
  28. ssl_context = OpenSSL::SSL::SSLContext.new()
  29. ssl_context.ssl_version = :SSLv23
  30. ssl_socket = OpenSSL::SSL::SSLSocket.new(s, ssl_context)
  31. ssl_socket.sync_close = true
  32. ssl_socket.connect
  33.  
  34. # send nickname to the server
  35.  
  36. ssl_socket.puts "NICK ubuntu76\r\n"
  37.  
  38. # send user info to the server
  39.  
  40. ssl_socket.puts "USER unbuntu76 8 * :linux\r\n"
  41.  
  42. # send registration info to the server to identify your registered nick
  43.  
  44. ssl_socket.puts "NICKSERV identify (enter your password here)\r\n"
  45.  
  46. # get channel to join from the user and join the channel
  47.  
  48. print "Enter channel to join: "
  49. channel = gets.chomp
  50. ssl_socket.puts "JOIN #" + channel
  51.  
  52. puts "Enter text to send to channel: "
  53.  
  54. while (1)
  55.     begin
  56.         input = ""
  57.  
  58.         Timeout::timeout(240) do
  59.            ssl_socket.puts "PONG :Linux Rocks !!"
  60.            puts "PONG :Linux Rocks !!"
  61.            input = gets.chomp
  62.         end
  63.        
  64.         ssl_socket.puts "PRIVMSG #" + channel + " :" + input
  65.         rescue Timeout::Error
  66.     end
  67. end
  68.  
  69. # close socket when done
  70.  
  71. ssl_socket.close
Advertisement
Add Comment
Please, Sign In to add comment