Sha256: 4fc0fc649ced680be56b0b7ac85317baa7286ee01a19816c53f72951956de5f3

Contents?: true

Size: 1.31 KB

Versions: 20

Compression:

Stored size: 1.31 KB

Contents

require "email/provider"
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

20 entries across 20 versions & 1 rubygems

Version Path
imap-backup-9.3.2 lib/imap/backup/account/connection/client_factory.rb
imap-backup-9.4.0.pre1 lib/imap/backup/account/connection/client_factory.rb
imap-backup-9.3.1 lib/imap/backup/account/connection/client_factory.rb
imap-backup-9.3.0 lib/imap/backup/account/connection/client_factory.rb
imap-backup-9.2.0 lib/imap/backup/account/connection/client_factory.rb
imap-backup-9.1.1 lib/imap/backup/account/connection/client_factory.rb
imap-backup-9.1.0 lib/imap/backup/account/connection/client_factory.rb
imap-backup-9.0.2 lib/imap/backup/account/connection/client_factory.rb
imap-backup-9.0.1.rc1 lib/imap/backup/account/connection/client_factory.rb
imap-backup-9.0.0 lib/imap/backup/account/connection/client_factory.rb
imap-backup-9.0.0.rc1 lib/imap/backup/account/connection/client_factory.rb
imap-backup-8.0.2 lib/imap/backup/account/connection/client_factory.rb
imap-backup-8.0.1 lib/imap/backup/account/connection/client_factory.rb
imap-backup-8.0.0 lib/imap/backup/account/connection/client_factory.rb
imap-backup-8.0.0.rc1 lib/imap/backup/account/connection/client_factory.rb
imap-backup-7.0.2 lib/imap/backup/account/connection/client_factory.rb
imap-backup-7.0.0.rc1 lib/imap/backup/account/connection/client_factory.rb
imap-backup-6.3.0 lib/imap/backup/account/connection/client_factory.rb
imap-backup-6.2.1 lib/imap/backup/account/connection/client_factory.rb
imap-backup-6.1.0 lib/imap/backup/account/connection/client_factory.rb