Sha256: 013fea00346486ce2ee85a2afcb5ee93e03172f824718c58917dacf8478c6301

Contents?: true

Size: 1.28 KB

Versions: 2

Compression:

Stored size: 1.28 KB

Contents

require "retry_on_error"

module Imap::Backup
  class Account::Connection::ClientFactory
    include RetryOnError

    LOGIN_RETRY_CLASSES = [EOFError, Errno::ECONNRESET, SocketError].freeze

    attr_reader :account

    def initialize(account:)
      @account = account
      @provider = nil
      @server = nil
    end

    def run
      retry_on_error(errors: LOGIN_RETRY_CLASSES) do
        options = provider_options
        Logger.logger.debug(
          "Creating IMAP instance: #{server}, options: #{options.inspect}"
        )
        client =
          if provider.is_a?(Email::Provider::AppleMail)
            Client::AppleMail.new(server, options)
          else
            Client::Default.new(server, options)
          end
        Logger.logger.debug "Logging in: #{account.username}/#{masked_password}"
        client.login(account.username, account.password)
        Logger.logger.debug "Login complete"
        client
      end
    end

    private

    def masked_password
      account.password.gsub(/./, "x")
    end

    def provider
      @provider ||= Email::Provider.for_address(account.username)
    end

    def provider_options
      provider.options.merge(account.connection_options || {})
    end

    def server
      @server ||= account.server || provider.host
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
imap-backup-6.0.1 lib/imap/backup/account/connection/client_factory.rb
imap-backup-6.0.0 lib/imap/backup/account/connection/client_factory.rb