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

Inherits:
Object
  • Object
show all
Includes:
RetryOnError
Defined in:
lib/imap/backup/account/connection.rb

Constant Summary collapse

LOGIN_RETRY_CLASSES =
[EOFError, Errno::ECONNRESET, SocketError].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RetryOnError

#retry_on_error

Constructor Details

#initialize(options) ⇒ Connection

Returns a new instance of Connection.



18
19
20
21
22
23
24
25
26
27
# File 'lib/imap/backup/account/connection.rb', line 18

def initialize(options)
  @username = options[:username]
  @password = options[:password]
  @local_path = options[:local_path]
  @config_folders = options[:folders]
  @server = options[:server]
  @connection_options = options[:connection_options] || {}
  @folders = nil
  
end

Instance Attribute Details

#connection_optionsObject (readonly)

Returns the value of attribute connection_options.



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

def connection_options
  @connection_options
end

#local_pathObject (readonly)

Returns the value of attribute local_path.



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

def local_path
  @local_path
end

#passwordObject (readonly)

Returns the value of attribute password.



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

def password
  @password
end

#usernameObject (readonly)

Returns the value of attribute username.



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

def username
  @username
end

Instance Method Details

#disconnectObject



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

def disconnect
  imap.disconnect if @imap
end

#foldersObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/imap/backup/account/connection.rb', line 29

def folders
  @folders ||=
    begin
      root = provider_root
      mailbox_lists = imap.list(root, "*")

      if mailbox_lists.nil?
        message = "Unable to get folder list for account #{username}"
        Imap::Backup.logger.info message
        raise message
      end

      utf7_encoded = mailbox_lists.map(&:name)
      utf7_encoded.map { |n| Net::IMAP.decode_utf7(n) }
    end
end

#imapObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/imap/backup/account/connection.rb', line 100

def imap
  @imap ||=
    retry_on_error(errors: ) do
      options = provider_options
      Imap::Backup.logger.debug(
        "Creating IMAP instance: #{server}, options: #{options.inspect}"
      )
      imap = Net::IMAP.new(server, options)
      Imap::Backup.logger.debug "Logging in: #{username}/#{masked_password}"
      imap.(username, password)
      Imap::Backup.logger.debug "Login complete"
      imap
    end
end

#local_foldersObject



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/imap/backup/account/connection.rb', line 72

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

#reconnectObject



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

def reconnect
  disconnect
  @imap = nil
end

#restoreObject



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

def restore
  local_folders do |serializer, folder|
    restore_folder serializer, folder
  end
end

#run_backupObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/imap/backup/account/connection.rb', line 54

def run_backup
  Imap::Backup.logger.debug "Running backup of account: #{username}"
  # start the connection so we get logging messages in the right order
  imap
  each_folder do |folder, serializer|
    next if !folder.exist?

    Imap::Backup.logger.debug "[#{folder.name}] running backup"
    serializer.apply_uid_validity(folder.uid_validity)
    begin
      Downloader.new(folder, serializer).run
    rescue Net::IMAP::ByeResponseError
      reconnect
      retry
    end
  end
end

#serverObject



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

def server
  return @server if @server
  return nil if provider.nil?

  @server = provider.host
end

#statusObject



46
47
48
49
50
51
52
# File 'lib/imap/backup/account/connection.rb', line 46

def status
  backup_folders.map do |backup_folder|
    f = ::Folder.new(self, backup_folder[:name])
    s = Serializer::Mbox.new(local_path, backup_folder[:name])
    {name: backup_folder[:name], local: s.uids, remote: f.uids}
  end
end