Sha256: 629695406d00ba94f392c686e434c768eb458cc38bd31a5d595ab215f3b2d931

Contents?: true

Size: 1.79 KB

Versions: 1

Compression:

Stored size: 1.79 KB

Contents

require 'socket'
require 'rlyeh/dispatcher'

module Rlyeh
  module DeepOnes
    class Auth
      include Rlyeh::Dispatcher

      attr_reader :nick, :pass, :user, :real, :host

      def initialize(app, &authenticator)
        @app = app
        @authenticator = authenticator
        @authorized = false
      end

      def call(env)
        dispatch env

        if authorized?
          env.auth = self
          @app.call env
        end
      end

      def load_session(env, id)
        env.server.sessions[id] ||= Rlyeh::Session.new(id)
      end

      def authorized?
        @authorized
      end

      def authorized(env)
        name = env.settings.server_name
        version = env.settings.server_version
        
        env.connection.tap do |conn|
          conn.send_numeric_reply :welcome, @host, "Welcome to the Internet Relay Network #{@nick}!#{@user}@#{@host}"
          conn.send_numeric_reply :yourhost, @host, "Your host is #{name}, running version #{version}"
          conn.send_numeric_reply :created,  @host, "This server was created #{Time.now}"
          conn.send_numeric_reply :myinfo, @host, "#{name} #{version} #{""} #{""}"
        end
      end

      on :pass do |env|
        @pass = env.message.params[0]
      end

      on :nick do |env|
        @nick = env.message.params[0]
      end

      on :user do |env|
        @user = env.message.params[0]
        @real = env.message.params[3]
        port, @host = Socket.unpack_sockaddr_in(env.connection.get_peername)

        session_id = @authenticator.call(self)
        if session_id.nil?
          p "Auth error"
        else
          @authorized = true
          session = load_session env, session_id
          session.attach env.connection
          authorized env
          p 'Session attached.'
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rlyeh-0.0.2 lib/rlyeh/deep_ones/auth.rb