Sha256: e74171496ac02b66c1cdb34263f4acb00be1bef404624cd91007b5b31285b48c

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

class IRC
  class Server
    attr_accessor :config, :connection, :handlers, :name, :irc
    config_accessor :address, :port, :nick, :ident, :realname, :ssl

    def initialize(irc, name, config)
      @irc = irc
      @name = name
      @config = config
      @handlers = {}
    end

    def send_cmd(cmd, *args)
      # remove nil entries
      args.compact!
      # prepend last arg with : only if it exists. it's really ugly
      args[-1] = ":#{args[-1]}" if args[-1]
      args = args.join(' ').delete("\r\n")
      connection.send_data("#{cmd.to_s.upcase} #{args}\r\n")
    end

    # basic IRC commands
    include Commands

    def on(event, &block)
      @handlers[event.to_s.downcase.to_sym] = Callback.new(block)
    end

    def handle_event(event)
      if @handlers[:all]
        @handlers[:all].call(@irc, event)
      elsif @irc.handlers[:all]
        @irc.handlers[:all].call(@irc, event)
      end

      if @handlers[event.command]
        @handlers[event.command].call(@irc, event)
      elsif @irc.handlers[event.command]
        @irc.handlers[event.command].call(@irc, event)
      end
    end

    # Eventmachine callbacks
    def receive_line(line)
      parsed_line = Parser.parse(line)
      event = Event.new(self, parsed_line[:prefix],
                        parsed_line[:command].downcase.to_sym,
                        parsed_line[:params])
      handle_event(event)
    end

    def unbind
      EM.add_timer(3) do
        connection.reconnect(config.address, config.port)
        connection.post_init
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
on_irc-2.1.5 lib/on_irc/server.rb