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/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, 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.



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

def initialize(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]
  @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)


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

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)


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

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



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

def folders
  @folders
end

#local_pathString

Returns the path where backups will be saved.

Returns:

  • (String)

    the path where backups will be saved



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

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)


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

def mirror_mode
  @mirror_mode
end

#passwordString

Returns password of the Account.

Returns:

  • (String)

    password of the Account



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

def password
  @password
end

#reset_seen_flags_after_fetchBoolean

Should ‘Seen’ flags be cached before fetchiong 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)


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

def reset_seen_flags_after_fetch
  @reset_seen_flags_after_fetch
end

#serverString

The address of the IMAP server

Returns:

  • (String)


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

def server
  @server
end

#usernameString

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

Returns:

  • (String)


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

def username
  @username
end

Instance Method Details

#capabilitiesArray<String>

Returns the capabilites of the IMAP server

Returns:

  • (Array<String>)

    the capabilities



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

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



114
115
116
# File 'lib/imap/backup/account.rb', line 114

def clear_changes
  @changes = {}
end

#clientAccount::Client::Default

Initializes a client for the account’s IMAP server

Returns:

  • (Account::Client::Default)

    the client



82
83
84
# File 'lib/imap/backup/account.rb', line 82

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)


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

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.



203
204
205
206
207
208
209
210
211
212
213
# File 'lib/imap/backup/account.rb', line 203

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

#mark_for_deletionvoid

This method returns an undefined value.

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



121
122
123
# File 'lib/imap/backup/account.rb', line 121

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



126
127
128
# File 'lib/imap/backup/account.rb', line 126

def marked_for_deletion?
  @marked_for_deletion
end

#modified?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/imap/backup/account.rb', line 108

def modified?
  changes.any?
end

#multi_fetch_sizeInteger

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

Returns:

  • (Integer)


218
219
220
221
222
223
224
225
226
227
# File 'lib/imap/backup/account.rb', line 218

def multi_fetch_size
  @multi_fetch_size ||= begin
    int = @multi_fetch_size_orignal.to_i
    if int.positive?
      int
    else
      DEFAULT_MULTI_FETCH_SIZE
    end
  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



233
234
235
236
237
# File 'lib/imap/backup/account.rb', line 233

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



89
90
91
# File 'lib/imap/backup/account.rb', line 89

def namespaces
  client.namespace
end

#to_hHash

Returns all Account data for serialization.

Returns:

  • (Hash)

    all Account data for serialization



131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/imap/backup/account.rb', line 131

def to_h
  h = {username: @username, password: @password}
  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

#valid?Boolean

Indicates whether the account has been configured, and is ready to be used

Returns:

  • (Boolean)


104
105
106
# File 'lib/imap/backup/account.rb', line 104

def valid?
  username && password ? true : false
end