Guest User

Untitled

a guest
Jul 17th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. require "socket"
  2.  
  3. class IRCSession
  4. def initialize(server, port, nick, user, channels)
  5. @server = server
  6. @port = port
  7. @nick = nick
  8. @user = user
  9. @channels = channels
  10.  
  11. end
  12.  
  13. ## Send to the server, output to console
  14. def send(msg)
  15. begin
  16. puts "send> #{msg}"
  17. @sock.send "#{msg}\n", 0
  18. rescue
  19. puts "## Message failed to send ##"
  20. end
  21. end
  22.  
  23. ## Connect to server, start the loop
  24. def connect()
  25. @sock = TCPSocket.open(@server, @port)
  26.  
  27. send "USER #{@user} #{@user} #{@user} #{@user}"
  28. send "NICK #{@nick}"
  29. @channels.each { |chan| send "JOIN #{chan}" }
  30.  
  31. start_loop
  32. end
  33.  
  34. ## Processes input from server -- Just have the one case there now
  35. ## to prevent ping timeouts, will develop this part later
  36. def server_input(s)
  37.  
  38. puts s
  39. case s.strip
  40. when /^ping /i
  41. puts "recv> PING {$'}"
  42. send "PONG #{$'}"
  43. end
  44. end
  45.  
  46. ## Loop that keeps connection open, processes input
  47. def start_loop()
  48. loop do
  49. input = select([@sock, $stdin], nil, nil)
  50. next if !input
  51.  
  52. for s in input[0]
  53. if (s == $stdin) then
  54. return if $stdin.eof?
  55. s = $stdin.gets
  56. command(s)
  57. else#if (s == @sock) then
  58. return if @sock.eof?
  59. s = @sock.gets
  60. #server_input(s)
  61. p s
  62. end
  63. end
  64. end
  65. end
  66.  
  67. ## Process console input -- Yeah, I know it's shitty, but it's just a
  68. ## quick fix to get it working
  69. def command(s)
  70. s.chomp!
  71. case s.strip
  72. when /^msg /i
  73. privmsg $'
  74. when /^join /i
  75. if !(@channels.include?($'))
  76. send "JOIN #{$'}"
  77. @channels.push($')
  78. end
  79. when /^part /i
  80. if @channels.include?($')
  81. send "PART #{$'}"
  82. @channels.delete($')
  83. end
  84. end
  85.  
  86. end
  87.  
  88. ## Send a message to all active channels
  89. def privmsg (msg)
  90. @channels.each { |chan| send "PRIVMSG #{chan} #{msg}" }
  91. end
  92.  
  93. end
  94.  
  95.  
  96. bot = IRCSession.new('irc.tddirc.net', 6667, 'scamper', 'scamper', ['#bots'])
  97. bot.connect
Add Comment
Please, Sign In to add comment