Advertisement
Guest User

Untitled

a guest
Mar 13th, 2018
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. require 'socket'
  2.  
  3. class IRCBot
  4. attr_accessor :sock
  5. def initialize(server, port, nick, channel)
  6. @sock = TCPSocket.new(server, port)
  7. @nick = nick
  8. @first_channel = channel
  9. @channels = Hash.new
  10. @priveleged_buffer = Hash.new(Array.new)
  11. end
  12.  
  13. def start
  14. send "USER #{@nick} #{@nick} #{@nick} :#{@nick}"
  15. send "NICK #{@nick}"
  16. wait
  17. end
  18.  
  19. def wait #TODO Handle this more efficiently and simply
  20. while !@sock.closed?
  21. line = @sock.readline.chomp.strip
  22. log line, :in
  23. if line =~ /^PING :(.+)$/i
  24. pong($1)
  25. elsif line =~ /^.*?PRIVMSG (\S+) :!say (.+)/
  26. say($2, $1)
  27. elsif line =~ /^.*?PRIVMSG \S+ :!join (.+)/
  28. join($1)
  29. elsif line =~ /^.*?PRIVMSG \S+ :!part (\S+)\s*(\S)*/
  30. part($1, $2)
  31. elsif line =~ /^.*?001 #{@nick}.*$/
  32. join(@first_channel) #TODO Split this off into a separate function and handle things such as identifying and the like
  33. #elsif line =~ /^.*?352 \S+ (\S+) \S+ \S+ \S+ (\S+) \S+ :\S \S+/ #:server_name 352 botsnick channel name host server nick modes :0 name
  34. # who_response($1, $2)
  35. end
  36. end
  37. end
  38.  
  39. def send(message)
  40. log message, :out
  41. @sock.print "#{message}\r\n"
  42. end
  43.  
  44. def log(message, direction = :info)
  45. if direction == :in
  46. puts "--> #{message}"
  47. elsif direction == :out
  48. puts "<-- #{message}"
  49. else
  50. puts "#{message}"
  51. end
  52. end
  53.  
  54. def pong(target)
  55. send "PONG :#{target}"
  56. end
  57.  
  58. def say(text, channel)
  59. send "PRIVMSG #{channel} :#{text}"
  60. end
  61.  
  62. def join(channel)
  63. send "JOIN #{channel}"
  64. @channels[channel] = Channel.new(channel, self) unless @channels[channel]
  65. end
  66.  
  67. def part(channel, reason = "Leaving")
  68. send "PART #{channel} :#{reason}"
  69. @channels.delete(channel)
  70. end
  71.  
  72. def who(channel)
  73. send "WHO #{channel}"
  74. end
  75.  
  76. end
  77.  
  78. class Channel
  79. attr_accessor :name
  80. def initialize(name, bot)
  81. @name = name
  82. @bot = bot
  83. @users = Hash.new
  84. end
  85.  
  86. end
  87.  
  88. marcobot = IRCBot.new("irc.hackthissite.org", 6667, "MarcoB", "#bots")
  89. marcobot.start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement