Class: Imap::Backup::Account::Connection
- Inherits:
-
Object
- Object
- Imap::Backup::Account::Connection
show all
- Includes:
- RetryOnError
- Defined in:
- lib/imap/backup/account/connection.rb
Defined Under Namespace
Classes: InvalidGmailOauth2RefreshToken
Constant Summary
collapse
- LOGIN_RETRY_CLASSES =
[EOFError, Errno::ECONNRESET, SocketError].freeze
Instance Attribute Summary collapse
Instance Method Summary
collapse
#retry_on_error
Constructor Details
#initialize(options) ⇒ Connection
Returns a new instance of Connection.
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/imap/backup/account/connection.rb', line 22
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
create_account_folder
end
|
Instance Attribute Details
#connection_options ⇒ Object
Returns the value of attribute connection_options.
17
18
19
|
# File 'lib/imap/backup/account/connection.rb', line 17
def connection_options
@connection_options
end
|
#local_path ⇒ Object
Returns the value of attribute local_path.
18
19
20
|
# File 'lib/imap/backup/account/connection.rb', line 18
def local_path
@local_path
end
|
#password ⇒ Object
Returns the value of attribute password.
19
20
21
|
# File 'lib/imap/backup/account/connection.rb', line 19
def password
@password
end
|
#username ⇒ Object
Returns the value of attribute username.
20
21
22
|
# File 'lib/imap/backup/account/connection.rb', line 20
def username
@username
end
|
Instance Method Details
#disconnect ⇒ Object
82
83
84
|
# File 'lib/imap/backup/account/connection.rb', line 82
def disconnect
imap.disconnect
end
|
#folders ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/imap/backup/account/connection.rb', line 33
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
|
#imap ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/imap/backup/account/connection.rb', line 91
def imap
@imap ||=
retry_on_error(errors: LOGIN_RETRY_CLASSES) do
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.login(username, password)
end
Imap::Backup.logger.debug "Login complete"
imap
end
end
|
#reconnect ⇒ Object
86
87
88
89
|
# File 'lib/imap/backup/account/connection.rb', line 86
def reconnect
disconnect
@imap = nil
end
|
#restore ⇒ Object
76
77
78
79
80
|
# File 'lib/imap/backup/account/connection.rb', line 76
def restore
local_folders do |serializer, folder|
restore_folder serializer, folder
end
end
|
#run_backup ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/imap/backup/account/connection.rb', line 58
def run_backup
Imap::Backup.logger.debug "Running backup of account: #{username}"
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
|
#server ⇒ Object
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
|
#status ⇒ Object
50
51
52
53
54
55
56
|
# File 'lib/imap/backup/account/connection.rb', line 50
def status
backup_folders.map do |backup_folder|
f = Account::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
|