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

Inherits:
Object
  • Object
show all
Defined in:
lib/imap/backup/account/connection.rb,
lib/imap/backup/account/connection/folder_names.rb,
lib/imap/backup/account/connection/backup_folders.rb

Defined Under Namespace

Classes: BackupFolders, ClientFactory, FolderNames

Instance Attribute Summary collapse

Instance Method Summary collapse

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



25
26
27
28
# File 'lib/imap/backup/account/connection.rb', line 25

def backup_folders
  @backup_folders ||=
    Account::Connection::BackupFolders.new(client: client, account: ).run
end

#clientObject

TODO: make this private



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

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

#disconnectObject



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

def disconnect
  client.disconnect if @client
  reset
end

#folder_namesObject



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

def folder_names
  @folder_names ||= Account::Connection::FolderNames.new(client: client, account: ).run
end

#local_foldersObject



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/imap/backup/account/connection.rb', line 77

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.new(.local_path, name)
    folder = Account::Folder.new(self, name)
    yield serializer, folder
  end
end

#namespacesObject



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

def namespaces
  client.namespace
end

#reconnectObject



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

def reconnect
  disconnect
end

#resetObject



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

def reset
  @backup_folders = nil
  @client = nil
  @folder_names = nil
end

#restoreObject



91
92
93
94
95
# File 'lib/imap/backup/account/connection.rb', line 91

def restore
  local_folders do |serializer, folder|
    Uploader.new(folder, serializer).run
  end
end

#run_backup(refresh: false) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/imap/backup/account/connection.rb', line 34

def run_backup(refresh: false)
  Logger.logger.info "Running backup of account: #{.username}"
  # start the connection so we get logging messages in the right order
  client
  
  if .mirror_mode
    # Delete serialized folders that are not to be backed up
    wanted = backup_folders.map(&:name)
    local_folders do |serializer, _folder|
      serializer.delete if !wanted.include?(serializer.folder)
    end
  end
  each_folder do |folder, serializer|
    begin
      next if !folder.exist?
    rescue Encoding::UndefinedConversionError
      message = "Skipping backup for '#{folder.name}' " \
                "as it is not UTF-7 encoded correctly"
      Logger.logger.info message
      next
    end

    Logger.logger.debug "[#{folder.name}] running backup"
    serializer.apply_uid_validity(folder.uid_validity)
    begin
      Downloader.new(
        folder,
        serializer,
        multi_fetch_size: .multi_fetch_size,
        reset_seen_flags_after_fetch: .reset_seen_flags_after_fetch
      ).run
      if .mirror_mode
        Logger.logger.info "Mirror mode - Deleting messages only present locally"
        LocalOnlyMessageDeleter.new(folder, serializer).run
      end
      FlagRefresher.new(folder, serializer).run if .mirror_mode || refresh
    rescue Net::IMAP::ByeResponseError
      reconnect
      retry
    end
  end
end