Class: Imap::Backup::Account::Connection

Inherits:
Object
  • Object
show all
Includes:
RetryOnError
Defined in:
lib/imap/backup/account/connection.rb

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RetryOnError

#retry_on_error

Constructor Details

#initialize(account) ⇒ Connection

Returns a new instance of Connection.



16
17
18
19
# File 'lib/imap/backup/account/connection.rb', line 16

def initialize()
  @account = 
  reset
end

Instance Attribute Details

#accountObject (readonly)

Returns the value of attribute account.



14
15
16
# File 'lib/imap/backup/account/connection.rb', line 14

def 
  @account
end

Instance Method Details

#backup_foldersObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/imap/backup/account/connection.rb', line 36

def backup_folders
  @backup_folders ||=
    begin
      names =
        if .folders&.any?
          .folders.map { |af| af[:name] }
        else
          folder_names
        end

      names.map do |name|
        Account::Folder.new(self, name)
      end
    end
end

#clientObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/imap/backup/account/connection.rb', line 119

def client
  @client ||=
    retry_on_error(errors: LOGIN_RETRY_CLASSES) do
      options = provider_options
      Imap::Backup::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
      Imap::Backup::Logger.logger.debug "Logging in: #{.username}/#{masked_password}"
      client.(.username, .password)
      Imap::Backup::Logger.logger.debug "Login complete"
      client
    end
end

#disconnectObject



101
102
103
104
# File 'lib/imap/backup/account/connection.rb', line 101

def disconnect
  client.disconnect if @client
  reset
end

#folder_namesObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/imap/backup/account/connection.rb', line 21

def folder_names
  @folder_names ||=
    begin
      folder_names = client.list

      if folder_names.empty?
        message = "Unable to get folder list for account #{.username}"
        Imap::Backup::Logger.logger.info message
        raise message
      end

      folder_names
    end
end

#local_foldersObject



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/imap/backup/account/connection.rb', line 81

def local_folders
  return enum_for(:local_folders) if !block_given?

  
  glob = File.join(.local_path, "**", "*.imap")
  base = Pathname.new(.local_path)
  Pathname.glob(glob) do |path|
    name = path.relative_path_from(base).to_s[0..-6]
    serializer = Serializer::Mbox.new(.local_path, name)
    folder = Account::Folder.new(self, name)
    yield serializer, folder
  end
end

#reconnectObject



106
107
108
# File 'lib/imap/backup/account/connection.rb', line 106

def reconnect
  disconnect
end

#resetObject



110
111
112
113
114
115
116
117
# File 'lib/imap/backup/account/connection.rb', line 110

def reset
  @backup_folders = nil
  @client = nil
  @config = nil
  @folder_names = nil
  @provider = nil
  @server = nil
end

#restoreObject



95
96
97
98
99
# File 'lib/imap/backup/account/connection.rb', line 95

def restore
  local_folders do |serializer, folder|
    restore_folder serializer, folder
  end
end

#run_backupObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/imap/backup/account/connection.rb', line 60

def run_backup
  Imap::Backup::Logger.logger.debug "Running backup of account: #{.username}"
  # start the connection so we get logging messages in the right order
  client
  
  each_folder do |folder, serializer|
    next if !folder.exist?

    Imap::Backup::Logger.logger.debug "[#{folder.name}] running backup"
    serializer.apply_uid_validity(folder.uid_validity)
    begin
      Downloader.new(
        folder, serializer, block_size: config.download_block_size
      ).run
    rescue Net::IMAP::ByeResponseError
      reconnect
      retry
    end
  end
end

#serverObject



139
140
141
# File 'lib/imap/backup/account/connection.rb', line 139

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

#statusObject



52
53
54
55
56
57
58
# File 'lib/imap/backup/account/connection.rb', line 52

def status
  
  backup_folders.map do |folder|
    s = Serializer::Mbox.new(.local_path, folder.name)
    {name: folder.name, local: s.uids, remote: folder.uids}
  end
end