Guest User

Untitled

a guest
Mar 13th, 2018
202
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. line = @sock.readline.chomp.strip
  21. log line, :in
  22. if line =~ /^PING :(.+)$/i
  23. pong($1)
  24. elsif line =~ /^.*?PRIVMSG (\S+) :!say (.+)/
  25. say($2, $1)
  26. elsif line =~ /^.*?PRIVMSG \S+ :!join (.+)/
  27. join($1)
  28. elsif line =~ /^.*?PRIVMSG \S+ :!part (\S+)\s*(\S)*/
  29. part($1, $2)
  30. elsif line =~ /^.*?001 #{@nick}.*$/
  31. join(@first_channel) #TODO Split this off into a separate function and handle things such as identifying and the like
  32. #elsif line =~ /^.*?352 \S+ (\S+) \S+ \S+ \S+ (\S+) \S+ :\S \S+/ #:server_name 352 botsnick channel name host server nick modes :0 name
  33. # who_response($1, $2)
  34. end
  35. end
  36. end
  37.  
  38. def send(message)
  39. log message, :out
  40. @sock.print "#{message}\r\n"
  41. end
  42.  
  43. def log(message, direction = :info)
  44. if direction == :in
  45. puts "--> #{message}"
  46. elsif direction == :out
  47. puts "<-- #{message}"
  48. else
  49. puts "#{message}"
  50. end
  51. end
  52.  
  53. def pong(target)
  54. send "PONG :#{target}"
  55. end
  56.  
  57. def say(text, channel)
  58. send "PRIVMSG #{channel} :#{text}"
  59. end
  60.  
  61. def join(channel)
  62. send "JOIN #{channel}"
  63. @channels[channel] = Channel.new(channel, self) unless @channels[channel]
  64. end
  65.  
  66. def part(channel, reason = "Leaving")
  67. send "PART #{channel} :#{reason}"
  68. @channels.delete(channel)
  69. end
  70.  
  71. def who(channel)
  72. send "WHO #{channel}"
  73. end
  74.  
  75. end
  76.  
  77. class Channel
  78. attr_accessor :name
  79. def initialize(name, bot)
  80. @name = name
  81. @bot = bot
  82. @users = Hash.new
  83. end
  84.  
  85. end
  86.  
  87. marcobot = IRCBot.new("irc.hackthissite.org", 6667, "MarcoB", "#bots")
  88. marcobot.start
Advertisement
Add Comment
Please, Sign In to add comment