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

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

Defined Under Namespace

Classes: InvalidGmailOauth2RefreshToken

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Connection

Returns a new instance of Connection.



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

def initialize(options)
  @username = options[:username]
  @password = options[:password]
  @local_path = options[:local_path]
  @backup_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.



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

def connection_options
  @connection_options
end

#local_pathObject (readonly)

Returns the value of attribute local_path.



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

def local_path
  @local_path
end

#passwordObject (readonly)

Returns the value of attribute password.



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

def password
  @password
end

#usernameObject (readonly)

Returns the value of attribute username.



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

def username
  @username
end

Instance Method Details

#backup_foldersObject



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

def backup_folders
  return @backup_folders if @backup_folders && !@backup_folders.empty?

  (folders || []).map { |f| {name: f.name} }
end

#disconnectObject



69
70
71
# File 'lib/imap/backup/account/connection.rb', line 69

def disconnect
  imap.disconnect
end

#foldersObject



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

def folders
  @folders ||=
    begin
      root = provider_root
      @folders = imap.list(root, "*")
      if @folders.nil?
        Imap::Backup.logger.warn(
          "Unable to get folder list for account #{username}"
        )
      end
      @folders
    end
end

#imapObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/imap/backup/account/connection.rb', line 78

def imap
  return @imap unless @imap.nil?

  options = provider_options
  Imap::Backup.logger.debug(
    "Creating IMAP instance: #{server}, options: #{options.inspect}"
  )
  @imap = Net::IMAP.new(server, options)
  if gmail? && Gmail::Authenticator.refresh_token?(password)
    authenticator = Gmail::Authenticator.new(email: username, token: password)
    credentials = authenticator.credentials
    raise InvalidGmailOauth2RefreshToken if !credentials

    Imap::Backup.logger.debug "Logging in with OAuth2 token: #{username}"
    @imap.authenticate("XOAUTH2", username, credentials.access_token)
  else
    Imap::Backup.logger.debug "Logging in: #{username}/#{masked_password}"
    @imap.(username, password)
  end
  Imap::Backup.logger.debug "Login complete"
  @imap
end

#reconnectObject



73
74
75
76
# File 'lib/imap/backup/account/connection.rb', line 73

def reconnect
  disconnect
  @imap = nil
end

#restoreObject



63
64
65
66
67
# File 'lib/imap/backup/account/connection.rb', line 63

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

#run_backupObject



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/imap/backup/account/connection.rb', line 50

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)
    Downloader.new(folder, serializer).run
  end
end

#serverObject



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

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

  @server = provider.host
end

#statusObject



42
43
44
45
46
47
48
# File 'lib/imap/backup/account/connection.rb', line 42

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