Class: Imap::Backup::Serializer::Mbox

Inherits:
Serializer::Base
  • Object
show all
Defined in:
lib/imap/backup/serializer/mbox.rb

Instance Method Summary collapse

Constructor Details

#initialize(path, folder) ⇒ Mbox

Returns a new instance of Mbox.



8
9
10
11
12
# File 'lib/imap/backup/serializer/mbox.rb', line 8

def initialize(path, folder)
  super
  create_containing_directory
  assert_files
end

Instance Method Details

#save(uid, message) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/imap/backup/serializer/mbox.rb', line 29

def save(uid, message)
  uid = uid.to_s
  if uids.include?(uid)
    Imap::Backup.logger.debug "[#{folder}] message #{uid} already downloaded - skipping"
    return
  end

  # invalidate cache
  @uids = nil

  body = message["RFC822"]
  mboxrd_message = Email::Mboxrd::Message.new(body)
  mbox = imap = nil
  begin
    mbox = File.open(mbox_pathname, "ab")
    imap = File.open(imap_pathname, "ab")
    mbox.write mboxrd_message.to_s
    imap.write uid + "\n"
  rescue => e
    Imap::Backup.logger.warn "[#{folder}] failed to save message #{uid}:\n#{body}. #{e}:\n#{e.backtrace.join("\n")}"
  ensure
    mbox.close if mbox
    imap.close if imap
  end
end

#uids ⇒ Object

TODO: cleanup locks, close file handles



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/imap/backup/serializer/mbox.rb', line 16

def uids
  return @uids if @uids

  @uids = []
  return @uids if not exist?

  CSV.foreach(imap_pathname) do |row|
    @uids << row[0]
  end
  @uids = @uids.map(&:to_i).sort
  @uids
end