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.



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

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

Instance Attribute Details

#folderObject (readonly)

Returns the value of attribute folder.



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

def folder
  @folder
end

#loadedObject (readonly)

Returns the value of attribute loaded.



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

def loaded
  @loaded
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

Instance Method Details

#add(uid, message) ⇒ Object



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
73
# File 'lib/imap/backup/serializer/mbox_store.rb', line 43

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

#each_message(required_uids) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/imap/backup/serializer/mbox_store.rb', line 84

def each_message(required_uids)
  return enum_for(:each_message, required_uids) if !block_given?

  indexes = required_uids.each.with_object({}) do |uid, acc|
    index = uids.find_index(uid)
    acc[index] = uid if index
  end
  enumerator = Serializer::MboxEnumerator.new(mbox_pathname)
  enumerator.each.with_index do |raw, i|
    uid = indexes[i]
    next if !uid

    yield uid, Email::Mboxrd::Message.from_serialized(raw)
  end
end

#exist?Boolean

Returns:

  • (Boolean)


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

def exist?
  mbox_exist? && imap_exist?
end

#load(uid_maybe_string) ⇒ Object



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

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

  load_nth(message_index)
end

#rename(new_name) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/imap/backup/serializer/mbox_store.rb', line 116

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

#resetObject



108
109
110
111
112
113
114
# File 'lib/imap/backup/serializer/mbox_store.rb', line 108

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

#uid_validityObject



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

def uid_validity
  do_load if !loaded
  @uid_validity
end

#uid_validity=(value) ⇒ Object



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

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

#uidsObject



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

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

#update_uid(old, new) ⇒ Object



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

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