Guest User

Untitled

a guest
Apr 25th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. require 'eventmachine'
  2. require 'ostruct'
  3.  
  4. module IRCClient
  5. class Message
  6. attr_reader :client, :raw, :hostmask, :nick, :hostname, :ident, :sender, :command, :rarg, :arguments
  7.  
  8. def initialize(client)
  9. @client = client
  10. end
  11.  
  12. def parse!(line)
  13. @raw = line
  14. words = line.chomp.split(" ")
  15. if words[0][0] == ":"
  16. @hostmask = words[0][1..-1]
  17. @nick = @hostmask.split("!", 2).first
  18. @hostname = @hostmask.split("@", 2).last
  19. @ident = @hostmask.split("!", 2).last.split("@", 2).first
  20. @sender = client.find_user(@nick, @ident, @hostname)
  21. @command = words[1]
  22. @rarg = words[2..-1].join(" ")
  23. else
  24. @sender = @nick = @hostname = @hostmask = @ident = nil
  25. @command = words[0]
  26. @rarg = words[1..-1].join(" ")
  27. end
  28. @arguments = @rarg.split(" ")
  29. i = @arguments.find_index {|a| a[0] == ":" }
  30. @arguments[i..-1] = @arguments[i..-1].join(" ")[1..-1] if i
  31. end
  32. end
  33.  
  34. def initialize(*args)
  35. super
  36. @buffer = BufferedTokenizer.new("\r\n")
  37. end
  38.  
  39. def post_init
  40. puts "[CLIENT] Registering"
  41. send_frame "NICK", "Lime"
  42. send_frame "USER", "lime", "*", "*", ":Lime, the IRC bot"
  43. puts "[CLIENT] Registered"
  44. end
  45.  
  46. def send_frame(command, *arguments)
  47. puts "[CLIENT] Sent: #{command} #{arguments.join(" ")}"
  48. send_data "#{command} #{arguments.join(" ")}\r\n"
  49. end
  50.  
  51. def privmsg(to, msg)
  52. to = to.nick if to.respond_to?(:nick)
  53. send_frame "PRIVMSG", to, ":"+msg
  54. end
  55.  
  56. def join(channel)
  57. send_frame "JOIN", channel
  58. end
  59.  
  60. def receive_data(data)
  61. @buffer.extract(data).each do |line|
  62. receive_line(line.chomp)
  63. end
  64. rescue StandardError => e
  65. receive_error(e.to_s)
  66. close_connection
  67. return
  68. end
  69.  
  70. def receive_line(line)
  71. @irc_message = Message.new(self)
  72. @irc_message.parse!(line)
  73.  
  74. puts "[CLIENT] Received: #{@irc_message.raw}"
  75.  
  76. case @irc_message.command
  77. when 'PING'
  78. send_frame "PONG", @irc_message.rarg
  79. when 'INVITE'
  80. join(@irc_message.arguments[1])
  81. privmsg(@irc_message.arguments[1], "#{@irc_message.nick}: Thanks for inviting me!")
  82. end
  83. end
  84.  
  85. def receive_error(error)
  86. puts "[CLIENT] {ERROR} #{error}"
  87. end
  88.  
  89. def unbind
  90. puts "[CLIENT] Connection lost"
  91. EventMachine.stop_event_loop
  92. end
  93.  
  94. def find_user(nick, ident=nil, hostname=nil)
  95. @users ||= {}
  96. user = @users[nick.downcase]
  97. return user unless ident || hostname
  98. if user && user.nick == nick && user.ident == ident && user.hostname == hostname
  99. return user
  100. else
  101. user = OpenStruct.new
  102. user.nick = nick
  103. user.ident = ident
  104. user.hostname = hostname
  105. @users[nick.downcase] = user
  106. return user
  107. end
  108. end
  109. end
  110.  
  111. EventMachine.run do
  112. EventMachine.connect("irc.cluenet.org", 6667, IRCClient)
  113. end
Add Comment
Please, Sign In to add comment