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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Connection

Returns a new instance of Connection.



12
13
14
15
16
17
18
19
20
21
# File 'lib/imap/backup/account/connection.rb', line 12

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.



7
8
9
# File 'lib/imap/backup/account/connection.rb', line 7

def connection_options
  @connection_options
end

#local_pathObject (readonly)

Returns the value of attribute local_path.



8
9
10
# File 'lib/imap/backup/account/connection.rb', line 8

def local_path
  @local_path
end

#passwordObject (readonly)

Returns the value of attribute password.



9
10
11
# File 'lib/imap/backup/account/connection.rb', line 9

def password
  @password
end

#usernameObject (readonly)

Returns the value of attribute username.



10
11
12
# File 'lib/imap/backup/account/connection.rb', line 10

def username
  @username
end

Instance Method Details

#disconnectObject



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

def disconnect
  imap.disconnect
end

#foldersObject



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/imap/backup/account/connection.rb', line 23

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



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

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)
  Imap::Backup.logger.debug "Logging in: #{username}/#{masked_password}"
  @imap.(username, password)
  Imap::Backup.logger.debug "Login complete"
  @imap
end

#reconnectObject



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

def reconnect
  disconnect
  @imap = nil
end

#restoreObject



58
59
60
61
62
# File 'lib/imap/backup/account/connection.rb', line 58

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

#run_backupObject



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

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

#statusObject



37
38
39
40
41
42
43
# File 'lib/imap/backup/account/connection.rb', line 37

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