Class: Imap::Backup::Account::Folder
- Inherits:
-
Object
- Object
- Imap::Backup::Account::Folder
- 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
- CREATE_RETRY_CLASSES =
[::Net::IMAP::BadResponseError].freeze
- EXAMINE_RETRY_CLASSES =
[::Net::IMAP::BadResponseError].freeze
- PERMITTED_FLAGS =
%i(Answered Draft Flagged Seen).freeze
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #add_flags(uids, flags) ⇒ Object
- #append(message) ⇒ Object
- #clear ⇒ Object
- #create ⇒ Object
- #delete_multi(uids) ⇒ Object
- #exist? ⇒ Boolean
- #fetch_multi(uids, attr = [BODY_ATTRIBUTE, "FLAGS"]) ⇒ Object
-
#initialize(client, name) ⇒ Folder
constructor
A new instance of Folder.
- #remove_flags(uids, flags) ⇒ Object
- #set_flags(uids, flags) ⇒ Object
- #uid_validity ⇒ Object
- #uids ⇒ Object
- #unseen(uids) ⇒ Object
Methods included from RetryOnError
Constructor Details
#initialize(client, name) ⇒ Folder
Returns a new instance of Folder.
29 30 31 32 33 |
# File 'lib/imap/backup/account/folder.rb', line 29 def initialize(client, name) @client = client @name = name @uid_validity = nil end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
26 27 28 |
# File 'lib/imap/backup/account/folder.rb', line 26 def client @client end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
27 28 29 |
# File 'lib/imap/backup/account/folder.rb', line 27 def name @name end |
Instance Method Details
#add_flags(uids, flags) ⇒ Object
126 127 128 129 130 131 |
# File 'lib/imap/backup/account/folder.rb', line 126 def add_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 |
#append(message) ⇒ Object
105 106 107 108 109 110 111 112 113 |
# File 'lib/imap/backup/account/folder.rb', line 105 def append() body = .imap_body date = .date&.to_time flags = .flags & PERMITTED_FLAGS retry_on_error(errors: APPEND_RETRY_CLASSES, limit: 3) do response = client.append(utf7_encoded_name, body, flags, date) extract_uid(response) end end |
#clear ⇒ Object
138 139 140 141 142 143 144 |
# File 'lib/imap/backup/account/folder.rb', line 138 def clear existing = uids return if existing.empty? add_flags(existing, [:Deleted]) client.expunge end |
#create ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/imap/backup/account/folder.rb', line 51 def create return if exist? retry_on_error(errors: CREATE_RETRY_CLASSES) do client.create(utf7_encoded_name) end end |
#delete_multi(uids) ⇒ Object
115 116 117 118 |
# File 'lib/imap/backup/account/folder.rb', line 115 def delete_multi(uids) add_flags(uids, [:Deleted]) client.expunge end |
#exist? ⇒ Boolean
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/imap/backup/account/folder.rb', line 35 def exist? previous_level = Imap::Backup::Logger.logger.level previous_debug = Net::IMAP.debug Imap::Backup::Logger.logger.level = ::Logger::Severity::UNKNOWN Net::IMAP.debug = false retry_on_error(errors: EXAMINE_RETRY_CLASSES) do examine end true rescue FolderNotFound false ensure Imap::Backup::Logger.logger.level = previous_level Net::IMAP.debug = previous_debug end |
#fetch_multi(uids, attr = [BODY_ATTRIBUTE, "FLAGS"]) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/imap/backup/account/folder.rb', line 84 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 |
#remove_flags(uids, flags) ⇒ Object
133 134 135 136 |
# File 'lib/imap/backup/account/folder.rb', line 133 def remove_flags(uids, flags) client.select(utf7_encoded_name) client.uid_store(uids, "-FLAGS", flags) end |
#set_flags(uids, flags) ⇒ Object
120 121 122 123 124 |
# File 'lib/imap/backup/account/folder.rb', line 120 def set_flags(uids, flags) client.select(utf7_encoded_name) flags.reject! { |f| f == :Recent } client.uid_store(uids, "FLAGS", flags) end |
#uid_validity ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/imap/backup/account/folder.rb', line 59 def uid_validity @uid_validity ||= begin examine client.responses["UIDVALIDITY"][-1] end end |
#uids ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/imap/backup/account/folder.rb', line 67 def uids examine client.uid_search(["ALL"]).sort rescue FolderNotFound [] rescue NoMethodError = "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." Imap::Backup::Logger.logger.warn [] end |
#unseen(uids) ⇒ Object
146 147 148 149 150 151 152 153 154 155 |
# File 'lib/imap/backup/account/folder.rb', line 146 def unseen(uids) = uids.map(&:to_s).join(",") examine client.uid_search(["UID", , "UNSEEN"]) rescue NoMethodError # Apple Mail returns an empty response when searches have no results [] rescue FolderNotFound nil end |