Class: Imap::Backup::Serializer::MboxStore

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

Constant Summary collapse

CURRENT_VERSION =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, folder) ⇒ MboxStore

Returns a new instance of MboxStore.



13
14
15
16
17
18
19
# File 'lib/imap/backup/serializer/mbox_store.rb', line 13

def initialize(path, folder)
  @path = path
  @folder = folder
  @loaded = false
  @uids = nil
  @uid_validity = nil
end

Instance Attribute Details

#folder ⇒ Object (readonly)

Returns the value of attribute folder.



9
10
11
# File 'lib/imap/backup/serializer/mbox_store.rb', line 9

def folder
  @folder
end

#loaded ⇒ Object (readonly)

Returns the value of attribute loaded.



11
12
13
# File 'lib/imap/backup/serializer/mbox_store.rb', line 11

def loaded
  @loaded
end

#path ⇒ Object (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/imap/backup/serializer/mbox_store.rb', line 10

def path
  @path
end

Instance Method Details

#add(uid, message) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/imap/backup/serializer/mbox_store.rb', line 42

def add(uid, message)
  do_load if !loaded
  raise "Can't add messages without uid_validity" if !uid_validity

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

  body = message["RFC822"]
  mboxrd_message = Email::Mboxrd::Message.new(body)
  mbox = nil
  begin
    mbox = File.open(mbox_pathname, "ab")
    mbox.write mboxrd_message.to_serialized
    @uids << uid
    write_imap_file
  rescue StandardError => e
    message = <<-ERROR.gsub(/^\s*/m, "")
      [#{folder}] failed to save message #{uid}:
      #{body}. #{e}:
      #{e.backtrace.join("\n")}"
    ERROR
    Imap::Backup.logger.warn message
  ensure
    mbox&.close
  end
end

#exist? ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/imap/backup/serializer/mbox_store.rb', line 21

def exist?
  mbox_exist? && imap_exist?
end

#load(uid) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/imap/backup/serializer/mbox_store.rb', line 74

def load(uid)
  do_load if !loaded
  message_index = uids.find_index(uid)
  return nil if message_index.nil?

  load_nth(message_index)
end

#relative_path ⇒ Object



106
107
108
# File 'lib/imap/backup/serializer/mbox_store.rb', line 106

def relative_path
  File.dirname(folder)
end

#rename(new_name) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/imap/backup/serializer/mbox_store.rb', line 98

def rename(new_name)
  new_mbox_pathname = absolute_path(new_name + ".mbox")
  new_imap_pathname = absolute_path(new_name + ".imap")
  File.rename(mbox_pathname, new_mbox_pathname)
  File.rename(imap_pathname, new_imap_pathname)
  @folder = new_name
end

#reset ⇒ Object



90
91
92
93
94
95
96
# File 'lib/imap/backup/serializer/mbox_store.rb', line 90

def reset
  @uids = nil
  @uid_validity = nil
  @loaded = false
  delete_files
  write_blank_mbox_file
end

#uid_validity ⇒ Object



25
26
27
28
# File 'lib/imap/backup/serializer/mbox_store.rb', line 25

def uid_validity
  do_load if !loaded
  @uid_validity
end

#uid_validity=(value) ⇒ Object



30
31
32
33
34
35
# File 'lib/imap/backup/serializer/mbox_store.rb', line 30

def uid_validity=(value)
  do_load if !loaded
  @uid_validity = value
  @uids ||= []
  write_imap_file
end

#uids ⇒ Object



37
38
39
40
# File 'lib/imap/backup/serializer/mbox_store.rb', line 37

def uids
  do_load if !loaded
  @uids || []
end

#update_uid(old, new) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/imap/backup/serializer/mbox_store.rb', line 82

def update_uid(old, new)
  index = uids.find_index(old.to_i)
  return if index.nil?

  uids[index] = new.to_i
  write_imap_file
end