module Potato module IRC # Module for handling lines sent from an IRC client. # @api private module Events # @param [IRC::Packet] pkt # @return [void] def on_nick pkt @config.nick = pkt.args[0] @config.host = host_for(pkt.args[0]) end # @param [IRC::Packet] pkt # @return [void] def on_user pkt welcome! end # @param [IRC::Packet] pkt # @return [void] def on_mode pkt if pkt.args[0] =~ /#/ if pkt.args[1] == "b" send_packet :cmd => "localhost", :args => [368, @config.nick, pkt.args[0]], :content => "End of Channel Ban List" else send_packet :cmd => "localhost", :args => [324, @config.nick, pkt.args[0], "+"] send_packet :cmd => "localhost", :args => [329, @config.nick, pkt.args[0], Time.now.to_i] end end end # @param [IRC::Packet] pkt # @return [void] def on_who pkt users = @users[pkt.args[0].sub("#", "")].map(&:first) users.uniq.each do |user| send_packet :cmd => "localhost", :args => [352, @config.nick, pkt.args[0], user, host_for(user), "localhost", user], :content => "0 #{user}" end send_packet :cmd => "localhost", :args => [315, @config.nick, pkt.args[0]], :content => "End of /WHO list." end # @param [IRC::Packet] pkt # @return [void] def on_whois pkt @client.whoising = true @client.whois(pkt.args[0]) end # @param [IRC::Packet] pkt # @return [void] def on_ping pkt @socket.write("PONG #{pkt.args[0]}\r\n") end # @param [IRC::Packet] pkt # @return [void] def on_join pkt rooms = pkt.args[0].include?(",") ? pkt.args[0].split(",") : pkt.args unless @logged_in cannot_join(*rooms) else rooms.each do |room| @client.join(room) end end end # @param [IRC::Packet] pkt # @return [void] def on_pass pkt notice "Attempting to retrieve authtoken." begin tok = Timeout::timeout(30) { Potato::DAmn::Token.get(@config.nick, pkt.args[0]) } rescue Timeout::Error notice "dAmn server is down. Try again later." @client.quit @socket.close return end if tok.nil? notice "Unable to retrieve an authtoken (server didn't respond)." @client.quit @socket.close return elsif tok.empty? notice "Wrong password or username. (no authtoken retrieved)" else notice "You have successfully logged in." @logged_in = true @config.token = tok @client.connect_with(@config.nick, @config.token) unless Server.opts.rooms.empty? Server.opts.rooms.each do |room| @client.join(room) end end end end # @param [IRC::Packet] pkt # @return [void] def on_part pkt @client.part(pkt.room) end # @param [IRC::Packet] pkt # @return [void] def on_privmsg pkt pkt.args.each do |room| if pkt.content =~ /\x01ACTION/ @client.action(room, pkt.content.gsub(/\x01(ACTION|\x00)?\s*/, "").encode_entities) else @client.say(room, pkt.content.encode_entities) end end @config.last_line = pkt.content.gsub(/\x01(ACTION|\x00)?\s*/, "") end # @param [IRC::Packet] pkt # @return [void] def on_topic pkt @client.topic(pkt.room, pkt.content) end # @param [IRC::Packet] pkt # @return [void] def on_quit pkt @client.quit @socket.close end end end end