Class: Imap::Backup::Account::Connection
- Inherits:
-
Object
- Object
- Imap::Backup::Account::Connection
- 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
-
#connection_options ⇒ Object
readonly
Returns the value of attribute connection_options.
-
#local_path ⇒ Object
readonly
Returns the value of attribute local_path.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
- #disconnect ⇒ Object
- #folders ⇒ Object
- #imap ⇒ Object
-
#initialize(options) ⇒ Connection
constructor
A new instance of Connection.
- #local_folders ⇒ Object
- #reconnect ⇒ Object
- #restore ⇒ Object
- #run_backup ⇒ Object
- #server ⇒ Object
- #status ⇒ Object
Methods included from RetryOnError
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() @username = [:username] @password = [:password] @local_path = [:local_path] @config_folders = [:folders] @server = [:server] = [:connection_options] || {} @folders = nil create_account_folder end |
Instance Attribute Details
#connection_options ⇒ Object (readonly)
Returns the value of attribute connection_options.
13 14 15 |
# File 'lib/imap/backup/account/connection.rb', line 13 def end |
#local_path ⇒ Object (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 |
#password ⇒ Object (readonly)
Returns the value of attribute password.
15 16 17 |
# File 'lib/imap/backup/account/connection.rb', line 15 def password @password end |
#username ⇒ Object (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
#disconnect ⇒ Object
91 92 93 |
# File 'lib/imap/backup/account/connection.rb', line 91 def disconnect imap.disconnect if @imap end |
#folders ⇒ Object
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? = "Unable to get folder list for account #{username}" Imap::Backup.logger.info raise end utf7_encoded = mailbox_lists.map(&:name) utf7_encoded.map { |n| Net::IMAP.decode_utf7(n) } end end |
#imap ⇒ Object
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: LOGIN_RETRY_CLASSES) do = Imap::Backup.logger.debug( "Creating IMAP instance: #{server}, options: #{options.inspect}" ) imap = Net::IMAP.new(server, ) Imap::Backup.logger.debug "Logging in: #{username}/#{masked_password}" imap.login(username, password) Imap::Backup.logger.debug "Login complete" imap end end |
#local_folders ⇒ Object
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 = Account::Folder.new(self, name) yield serializer, folder end end |
#reconnect ⇒ Object
95 96 97 98 |
# File 'lib/imap/backup/account/connection.rb', line 95 def reconnect disconnect @imap = nil end |
#restore ⇒ Object
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_backup ⇒ Object
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 |
#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
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 = 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 |