Class: Imap::Backup::Account::Folder

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
RetryOnError
Defined in:
lib/imap/backup/account/folder.rb

Constant Summary collapse

BODY_ATTRIBUTE =
"BODY[]".freeze
UID_FETCH_RETRY_CLASSES =
[EOFError, Errno::ECONNRESET, IOError].freeze
APPEND_RETRY_CLASSES =
[Net::IMAP::BadResponseError].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RetryOnError

#retry_on_error

Constructor Details

#initialize(connection, name) ⇒ Folder

Returns a new instance of Folder.



23
24
25
26
27
# File 'lib/imap/backup/account/folder.rb', line 23

def initialize(connection, name)
  @connection = connection
  @name = name
  @uid_validity = nil
end

Instance Attribute Details

#connection ⇒ Object (readonly)

Returns the value of attribute connection.



18
19
20
# File 'lib/imap/backup/account/folder.rb', line 18

def connection
  @connection
end

#name ⇒ Object (readonly)

Returns the value of attribute name.



19
20
21
# File 'lib/imap/backup/account/folder.rb', line 19

def name
  @name
end

Instance Method Details

#append(message) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/imap/backup/account/folder.rb', line 93

def append(message)
  body = message.imap_body
  date = message.date&.to_time
  retry_on_error(errors: APPEND_RETRY_CLASSES, limit: 3) do
    response = client.append(utf7_encoded_name, body, message.flags, date)
    extract_uid(response)
  end
end

#apply_flags(uids, flags) ⇒ Object



107
108
109
110
111
# File 'lib/imap/backup/account/folder.rb', line 107

def apply_flags(uids, flags)
  client.select(utf7_encoded_name)
  flags.reject! { |f| f == :Recent }
  client.uid_store(uids, "FLAGS", flags)
end

#clear ⇒ Object



125
126
127
128
129
130
131
# File 'lib/imap/backup/account/folder.rb', line 125

def clear
  existing = uids
  return if existing.empty?

  set_flags(existing, [:Deleted])
  client.expunge
end

#create ⇒ Object



41
42
43
44
45
# File 'lib/imap/backup/account/folder.rb', line 41

def create
  return if exist?

  client.create(utf7_encoded_name)
end

#delete_multi(uids) ⇒ Object



102
103
104
105
# File 'lib/imap/backup/account/folder.rb', line 102

def delete_multi(uids)
  set_flags(uids, [:Deleted])
  client.expunge
end

#exist? ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
# File 'lib/imap/backup/account/folder.rb', line 34

def exist?
  examine
  true
rescue FolderNotFound
  false
end

#fetch_multi(uids, attr = [BODY_ATTRIBUTE, "FLAGS"]) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/imap/backup/account/folder.rb', line 72

def fetch_multi(uids, attr = [BODY_ATTRIBUTE, "FLAGS"])
  examine
  fetch_data_items =
    retry_on_error(errors: UID_FETCH_RETRY_CLASSES) do
      client.uid_fetch(uids, attr)
    end
  return nil if fetch_data_items.nil?

  fetch_data_items.map do |item|
    attributes = item.attr

    {
      uid: attributes["UID"],
      body: attributes[BODY_ATTRIBUTE],
      flags: attributes["FLAGS"]
    }
  end
rescue FolderNotFound
  nil
end

#folder ⇒ Object

Deprecated: use #name



30
31
32
# File 'lib/imap/backup/account/folder.rb', line 30

def folder
  name
end

#set_flags(uids, flags) ⇒ Object



113
114
115
116
117
118
# File 'lib/imap/backup/account/folder.rb', line 113

def set_flags(uids, flags)
  # Use read-write access, via `select`
  client.select(utf7_encoded_name)
  flags.reject! { |f| f == :Recent }
  client.uid_store(uids, "+FLAGS", flags)
end

#uid_validity ⇒ Object



47
48
49
50
51
52
53
# File 'lib/imap/backup/account/folder.rb', line 47

def uid_validity
  @uid_validity ||=
    begin
      examine
      client.responses["UIDVALIDITY"][-1]
    end
end

#uids ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/imap/backup/account/folder.rb', line 55

def uids
  examine
  client.uid_search(["ALL"]).sort
rescue FolderNotFound
  []
rescue NoMethodError
  message =
    "Folder '#{name}' caused a NoMethodError. " \
    "Probably this was `undefined method `[]' for nil:NilClass (NoMethodError)` " \
    "in `search_internal` in stdlib net/imap.rb. " \
    'This is caused by `@responses["SEARCH"] being unset/undefined. ' \
    "Among others, Apple Mail servers send empty responses when " \
    "folders are empty, causing this error."
  Logger.logger.warn message
  []
end

#unseen(uids) ⇒ Object



133
134
135
136
137
138
139
140
141
142
# File 'lib/imap/backup/account/folder.rb', line 133

def unseen(uids)
  messages = uids.map(&:to_s).join(",")
  examine
  client.uid_search([messages, "UNSEEN"])
rescue NoMethodError
  # Apple Mail returns an empty response when searches have no results
  []
rescue FolderNotFound
  nil
end

#unset_flags(uids, flags) ⇒ Object



120
121
122
123
# File 'lib/imap/backup/account/folder.rb', line 120

def unset_flags(uids, flags)
  client.select(utf7_encoded_name)
  client.uid_store(uids, "-FLAGS", flags)
end