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.



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

def initialize()
  @account = 
  reset
end

Instance Attribute Details

#accountObject (readonly)

Returns the value of attribute account.



12
13
14
# File 'lib/imap/backup/account/connection.rb', line 12

def 
  @account
end

Instance Method Details

#backup_foldersObject



23
24
25
26
# File 'lib/imap/backup/account/connection.rb', line 23

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

#clientObject

TODO: make this private



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

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

#disconnectObject



87
88
89
90
# File 'lib/imap/backup/account/connection.rb', line 87

def disconnect
  client.disconnect if @client
  reset
end

#folder_namesObject



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

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

#local_foldersObject



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/imap/backup/account/connection.rb', line 67

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

#reconnectObject



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

def reconnect
  disconnect
end

#resetObject



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

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

#restoreObject



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

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

#run_backupObject



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
# File 'lib/imap/backup/account/connection.rb', line 36

def run_backup
  Logger.logger.debug "Running backup of account: #{account.username}"
  # start the connection so we get logging messages in the right order
  client
  
  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
    rescue Net::IMAP::ByeResponseError
      reconnect
      retry
    end
  end
end

#statusObject



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

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