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

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

Instance Attribute Summary

Attributes inherited from Base

#folder, #path

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
54
55
56
57
58
59
60
# 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_serialized
    imap.write uid + "\n"
  rescue => e
    message = "      [\#{folder}] failed to save message \#{uid}:\n      \#{body}. \#{e}:\n      \#{e.backtrace.join(\"\\n\")}\"\n    ERROR\n    Imap::Backup.logger.warn message\n  ensure\n    mbox.close if mbox\n    imap.close if imap\n  end\nend\n".gsub(/^\s*/m, "")

#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 !exist?

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