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

#account ⇒ Object (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_folders ⇒ Object



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

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

#client ⇒ Object

TODO: make this private



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

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

#disconnect ⇒ Object



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

def disconnect
  client.disconnect if @client
  reset
end

#folder_names ⇒ Object



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

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

#local_folders ⇒ Object



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

#reconnect ⇒ Object



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

def reconnect
  disconnect
end

#reset ⇒ Object



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

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

#restore ⇒ Object



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

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

#run_backup(refresh: false) ⇒ Object



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
76
77
78
79
# File 'lib/imap/backup/account/connection.rb', line 38

def run_backup(refresh: false)
  Logger.logger.info "Running backup of account: #{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

#status ⇒ Object



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

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