Guest User

Untitled

a guest
Feb 18th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. require "socket"
  2.  
  3. class User
  4. attr_reader :nick, :realname, :login, :password
  5. def initialize(nick, realname, login, password)
  6. @nick = nick
  7. @realname = realname
  8. @login = login
  9. @password = password
  10. end
  11. end
  12.  
  13. class Server
  14. attr_reader :user, :server, :port, :channels
  15. def initialize(user, server, port, channels=[])
  16. @user = user
  17. @server = server
  18. @port = port
  19. @channels = channels
  20. @bot = Bot.new(@user)
  21. end
  22.  
  23. def connect
  24. @connection = TCPSocket.open(@server, @port)
  25. print("addr: ", @connection.addr.join(":"), "\n")
  26. print("peer: ", @connection.peeraddr.join(":"), "\n")
  27. @connection.puts "USER #{@user.login} 0 * :#{@user.realname}"
  28. @connection.puts "NICK #{@user.nick}"
  29. @connection.puts "PRIVMSG NickServ :IDENTIFY #{@user.password}"
  30. sleep 2
  31. @connection.puts "JOIN #{@channels.join(",")}"
  32. maintain
  33. end
  34.  
  35. def maintain
  36. until @connection.eof? do
  37. msg = @connection.gets
  38. puts msg
  39. response = @bot.send(msg)
  40. if response
  41. puts response
  42. send(response)
  43. end
  44. end
  45. end
  46.  
  47. def send(msg)
  48. @connection.puts("#{msg}")
  49. end
  50. private :maintain
  51. end
  52.  
  53. class Bot
  54. def initialize(user)
  55. @nick = user.nick
  56. @realname = user.realname
  57. @login = user.login
  58. end
  59.  
  60. def send(msg)
  61. a = msg.split(/\s+/)
  62. ping = check_ping(a[0])
  63. if ping
  64. "PONG #{a[1]}"
  65. else
  66. t = Time.new
  67. user = a[0].split(/!/)
  68. nick = user[0].sub(/^:/, "")
  69. type = a[1]
  70. target = a[2]
  71. msg = a[3..-1].join(" ").sub(/^:/, "")
  72. puts "[#{t.strftime("%H:%M:%S")}] #{a[2]} <#{nick}> #{msg}"
  73. msgbytes = msg.bytes.to_a
  74. if type == "PRIVMSG"
  75. if msgbytes[0] == 1 && msgbytes[-1] == 1 #ctcp
  76. msg = clear_ctcp_bytes msgbytes
  77. if msg == "VERSION"
  78. r = add_ctcp_bytes "VERSION oneechan:ichiban-saiko:ruby".bytes.to_a
  79. "NOTICE #{nick} #{r}"
  80. elsif msg == "FINGER"
  81. r = add_ctcp_bytes "FINGER :\u3884\u3863\u3868\u3888~"
  82. "NOTICE #{nick} #{r}"
  83. end
  84. else #privmsg
  85. nil
  86. end
  87. elsif type == "NOTICE"
  88. nil
  89. end
  90. end
  91. end
  92.  
  93. def clear_ctcp_bytes(msgbytes)
  94. msgbytes.shift
  95. msgbytes.pop
  96. msgbytes.pack("U*")
  97. end
  98.  
  99. def add_ctcp_bytes(msg)
  100. r = msg.bytes.to_a
  101. r.unshift 1
  102. r.push 1
  103. r.pack("U*")
  104. end
  105.  
  106. def check_ping(msg)
  107. if msg == "PING"
  108. 1
  109. end
  110. end
  111.  
  112. end
  113.  
  114. def sigint_handler
  115. system "stty echo" # just in case
  116. puts ObjectSpace.each_object(Server) { |a| a.send("QUIT :Fare ye well"); }
  117. exit
  118. end
  119.  
  120. Signal.trap("INT") { sigint_handler }
  121.  
  122. print "IRC Bot: Enter id password: "
  123. system "stty -echo"
  124. password = gets
  125. system "stty echo"
  126. servers = []
  127. me = User.new("oneechan", "Your favourite oneechan~", "b", password)
  128. rizon = Server.new(me, "irc.rizon.net", "6667", ["#oneechan"])
  129. servers.push(rizon)
  130. servers.each {|s| s.connect}
Add Comment
Please, Sign In to add comment