Class: Imap::Backup::Account

Inherits:
Object
  • Object
show all
Defined in:
lib/imap/backup/account.rb,
lib/imap/backup/account/backup.rb,
lib/imap/backup/account/folder.rb,
lib/imap/backup/account/locker.rb,
lib/imap/backup/account/restore.rb,
lib/imap/backup/account/folder_backup.rb,
lib/imap/backup/account/folder_mapper.rb,
lib/imap/backup/account/backup_folders.rb,
lib/imap/backup/account/client_factory.rb,
lib/imap/backup/account/folder_ensurer.rb,
lib/imap/backup/account/serialized_folders.rb,
lib/imap/backup/account/local_only_folder_deleter.rb

Overview

Contains the attributes relating to an email account.

Defined Under Namespace

Classes: Backup, BackupFolders, ClientFactory, Folder, FolderBackup, FolderEnsurer, FolderMapper, LocalOnlyFolderDeleter, Locker, Restore, SerializedFolders

Constant Summary collapse

DEFAULT_MULTI_FETCH_SIZE =

By default, the backup process fetches one email at a time

1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Account

Returns a new instance of Account.

Parameters:

  • options (Hash)

    Account attributes

  • opts (Hash)

    a customizable set of options



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/imap/backup/account.rb', line 87

def initialize(options)
  check_options!(options)
  @username = options[:username]
  @password = options[:password]
  @local_path = options[:local_path]
  @folders = options[:folders]
  @folder_blacklist = options[:folder_blacklist] || false
  @mirror_mode = options[:mirror_mode] || false
  @server = options[:server]
  @connection_options = nil
  @supplied_connection_options = options[:connection_options]
  @download_strategy = options[:download_strategy]
  @multi_fetch_size_orignal = options[:multi_fetch_size]
  @reset_seen_flags_after_fetch = options[:reset_seen_flags_after_fetch]
  @status = options[:status] || DEFAULT_STATUS
  @client = nil
  @changes = {}
  @marked_for_deletion = false
end

Instance Attribute Details

#download_strategyString

The name of the download strategy to adopt during backups

Returns:

  • (String)


47
48
49
# File 'lib/imap/backup/account.rb', line 47

def download_strategy
  @download_strategy
end

#folder_blacklistBoolean

Indicates whether the configured folders are a whitelist or a blacklist. When true, any folders attribute will be taken as a list of folders to skip when running backups. When false, the folders attribute is used as the list of folders to backup. If no folders are configured, all folders on the server are backed up irrespective of the folder_blacklist setting

Returns:

  • (Boolean)


37
38
39
# File 'lib/imap/backup/account.rb', line 37

def folder_blacklist
  @folder_blacklist
end

#foldersArray<String>, void #folders=(value) ⇒ void

Overloads:

  • #foldersArray<String>, void

    The list of folders that have been configured for the Account

    Returns:

    • (Array<String>, void)

    See Also:

  • #folders=(value) ⇒ void

    This method returns an undefined value.

    Sets the folders attribute and marks it as modified, storing the original value

    Parameters:

    • value (Array<String>)

      a list of folders



29
30
31
# File 'lib/imap/backup/account.rb', line 29

def folders
  @folders
end

#local_pathString

Returns the path where backups will be saved.

Returns:

  • (String)

    the path where backups will be saved



20
21
22
# File 'lib/imap/backup/account.rb', line 20

def local_path
  @local_path
end

#mirror_modeBoolean

Should all emails be backed up progressively, or should emails which are deleted from the server be deleted locally?

Returns:

  • (Boolean)


41
42
43
# File 'lib/imap/backup/account.rb', line 41

def mirror_mode
  @mirror_mode
end

#passwordString

Returns password of the Account.

Returns:

  • (String)

    password of the Account



18
19
20
# File 'lib/imap/backup/account.rb', line 18

def password
  @password
end

#reset_seen_flags_after_fetchBoolean

Should ‘Seen’ flags be cached before fetching emails and rewritten to the server afterwards?

Some IMAP providers, notably Apple Mail, set the ‘Seen’ flag on emails when they are fetched. By setting ‘:reset_seen_flags_after_fetch`, a workaround is activated which checks which emails are ’unseen’ before and after the fetch, and removes the ‘Seen’ flag from those which have changed. As this check is susceptible to ‘race conditions’, i.e. when a different client sets the ‘Seen’ flag while imap-backup is fetching, it is best to only use it when required (i.e. for IMAP providers which always mark messages as ‘Seen’ when accessed).

Returns:

  • (Boolean)


60
61
62
# File 'lib/imap/backup/account.rb', line 60

def reset_seen_flags_after_fetch
  @reset_seen_flags_after_fetch
end

#serverString

The address of the IMAP server

Returns:

  • (String)


44
45
46
# File 'lib/imap/backup/account.rb', line 44

def server
  @server
end

#statusString

The status of the account - controls backup and migration behaviour “active” - the account is available for backup and migration, “archived” - the account is available for migration, but not backup, “offline” - the account is not available for backup or migration.

Returns:

  • (String)

    one of “active” (the default), “archived”, or “offline”



66
67
68
# File 'lib/imap/backup/account.rb', line 66

def status
  @status
end

#usernameString

The username of the account (usually the same as the email address)

Returns:

  • (String)


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

def username
  @username
end

Instance Method Details

#active?Boolean

Returns true if the account is active.

Returns:

  • (Boolean)

    true if the account is active



285
286
287
# File 'lib/imap/backup/account.rb', line 285

def active?
  @status == "active"
end

#archived?Boolean

Returns true if the account is archived.

Returns:

  • (Boolean)

    true if the account is archived



290
291
292
# File 'lib/imap/backup/account.rb', line 290

def archived?
  @status == "archived"
end

#available_for_backup?Boolean

Returns true if the account is available for backup operations.

Returns:

  • (Boolean)

    true if the account is available for backup operations



300
301
302
# File 'lib/imap/backup/account.rb', line 300

def available_for_backup?
  active?
end

#available_for_migration?Boolean

Returns true if the account is available for migration operations.

Returns:

  • (Boolean)

    true if the account is available for migration operations



305
306
307
# File 'lib/imap/backup/account.rb', line 305

def available_for_migration?
  active? || archived?
end

#capabilitiesArray<String>

Returns the capabilites of the IMAP server

Returns:

  • (Array<String>)

    the capabilities



124
125
126
# File 'lib/imap/backup/account.rb', line 124

def capabilities
  client.capability
end

#clear_changesvoid

This method returns an undefined value.

Resets the store of changes, indicating that the current state is the saved state



134
135
136
# File 'lib/imap/backup/account.rb', line 134

def clear_changes
  @changes = {}
end

#clientAccount::Client::Default

Initializes a client for the account’s IMAP server

Returns:

  • (Account::Client::Default)

    the client



110
111
112
# File 'lib/imap/backup/account.rb', line 110

def client
  @client ||= Account::ClientFactory.new(account: self).run
end

#connection_optionsHash, void

Extra options to be passed to the IMAP server when connecting

Returns:

  • (Hash, void)


224
225
226
227
228
229
230
231
232
# File 'lib/imap/backup/account.rb', line 224

def connection_options
  @connection_options ||=
    case @supplied_connection_options
    when String
      JSON.parse(@supplied_connection_options, symbolize_names: true)
    else
      @supplied_connection_options
    end
end

#connection_options=(value) ⇒ void

This method returns an undefined value.



235
236
237
238
239
240
241
242
243
244
# File 'lib/imap/backup/account.rb', line 235

def connection_options=(value)
  # Ensure we've loaded the connection_options
  connection_options
  parsed = if value == ""
             nil
           else
             JSON.parse(value, symbolize_names: true)
           end
  update(:connection_options, parsed)
end

#lockfile_pathString

Returns the path to the lockfile for the account.

Returns:

  • (String)

    the path to the lockfile for the account

Raises:

  • (RuntimeError)

    if the local_path is not set



193
194
195
196
197
# File 'lib/imap/backup/account.rb', line 193

def lockfile_path
  raise "local_path is not set" if !local_path

  File.join(local_path, "imap-backup.lock")
end

#mark_for_deletionvoid

This method returns an undefined value.

Sets a flag indicating the Account should be excluded from the next save operation



141
142
143
# File 'lib/imap/backup/account.rb', line 141

def mark_for_deletion
  @marked_for_deletion = true
end

#marked_for_deletion?Boolean

Returns whether the account has been flagged for deletion during setup.

Returns:

  • (Boolean)

    whether the account has been flagged for deletion during setup



146
147
148
# File 'lib/imap/backup/account.rb', line 146

def marked_for_deletion?
  @marked_for_deletion
end

#modified?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/imap/backup/account.rb', line 128

def modified?
  changes.any?
end

#multi_fetch_sizeInteger

The number of emails to fetch from the IMAP server at a time

Returns:

  • (Integer)


249
250
251
252
253
254
# File 'lib/imap/backup/account.rb', line 249

def multi_fetch_size
  @multi_fetch_size ||= begin
    int = @multi_fetch_size_orignal.to_i
    int.positive? ? int : DEFAULT_MULTI_FETCH_SIZE
  end
end

#multi_fetch_size=(value) ⇒ void

This method returns an undefined value.

Sets the multi_fetch_size attribute and marks it as modified, storing the original value. If the supplied value is not a positive integer, uses DEFAULT_MULTI_FETCH_SIZE



260
261
262
263
264
# File 'lib/imap/backup/account.rb', line 260

def multi_fetch_size=(value)
  parsed = value.to_i
  parsed = DEFAULT_MULTI_FETCH_SIZE if !parsed.positive?
  update(:multi_fetch_size, parsed)
end

#namespacesArray<String>

Returns the namespaces configured for the account on the IMAP server

Returns:

  • (Array<String>)

    the namespaces



117
118
119
# File 'lib/imap/backup/account.rb', line 117

def namespaces
  client.namespace
end

#offline?Boolean

Returns true if the account is offline.

Returns:

  • (Boolean)

    true if the account is offline



295
296
297
# File 'lib/imap/backup/account.rb', line 295

def offline?
  @status == "offline"
end

#to_hHash

Returns all Account data for serialization.

Returns:

  • (Hash)

    all Account data for serialization



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/imap/backup/account.rb', line 151

def to_h
  h = {
    username: username,
    password: password,
    status: status
  }
  h[:local_path] = local_path if local_path
  h[:folders] = folders if folders
  h[:folder_blacklist] = true if folder_blacklist
  h[:mirror_mode] = true if mirror_mode
  h[:server] = server if server
  h[:connection_options] = connection_options if connection_options
  h[:multi_fetch_size] = multi_fetch_size
  if reset_seen_flags_after_fetch
    h[:reset_seen_flags_after_fetch] = reset_seen_flags_after_fetch
  end
  h
end