# File lib/larch/imap.rb, line 52
  def initialize(uri, options = {})
    raise ArgumentError, "not an IMAP URI: #{uri}" unless uri.is_a?(URI) || uri =~ REGEX_URI
    raise ArgumentError, "options must be a Hash" unless options.is_a?(Hash)

    @uri     = uri.is_a?(URI) ? uri : URI(uri)
    @options = {
      :log_label   => "[#{username}@#{host}]",
      :max_retries => 3,
      :ssl_verify  => false
    }.merge(options)

    raise ArgumentError, "must provide a username and password" unless @uri.user && @uri.password

    @conn      = nil
    @mailboxes = {}

    @quirks    = {
      :gmail => false,
      :yahoo => false
    }

    @db_account = Database::Account.find_or_create(
      :hostname => host,
      :username => username
    )

    @db_account.touch

    # Create private convenience methods (debug, info, warn, etc.) to make
    # logging easier.
    Logger::LEVELS.each_key do |level|
      next if IMAP.private_method_defined?(level)

      IMAP.class_eval do
        define_method(level) do |msg|
          Larch.log.log(level, "#{@options[:log_label]} #{msg}")
        end

        private level
      end
    end
  end