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
Overview
Handles access to a folder on an IMAP server
Defined Under Namespace
Classes: FolderNotFound
Instance Attribute Summary collapse
- #client ⇒ Client::Default readonly
-
#name ⇒ String
readonly
The name of the folder.
Instance Method Summary collapse
-
#add_flags(uids, flags) ⇒ void
Adds one or more flags to a group of messages.
-
#append(message) ⇒ void
Uploads a message.
-
#clear ⇒ void
Deletes all messages from the folder.
-
#create ⇒ void
Creates the folder on the server.
-
#delete_multi(uids) ⇒ void
Deletes multiple messages.
- #exist? ⇒ Boolean
-
#fetch_multi(uids, attr = [BODY_ATTRIBUTE, "FLAGS"]) ⇒ Array<Hash>?
The requested messages.
-
#initialize(client, name) ⇒ Folder
constructor
A new instance of Folder.
-
#remove_flags(uids, flags) ⇒ void
Removes one or more flags from a group of messages.
-
#set_flags(uids, flags) ⇒ void
Sets one or more flags on a group of messages.
-
#uid_validity ⇒ Integer
The folder’s UID validity.
-
#uids ⇒ Array<Integer>
The folders message UIDs.
-
#unseen(uids) ⇒ Array<Integer>
The UIDs of messages with the ‘UNSEEN’ flag.
Methods included from RetryOnError
Constructor Details
#initialize(client, name) ⇒ Folder
Returns a new instance of Folder.
26 27 28 29 30 |
# File 'lib/imap/backup/account/folder.rb', line 26 def initialize(client, name) @client = client @name = name @uid_validity = nil end |
Instance Attribute Details
#client ⇒ Client::Default (readonly)
22 23 24 |
# File 'lib/imap/backup/account/folder.rb', line 22 def client @client end |
#name ⇒ String (readonly)
Returns the name of the folder.
24 25 26 |
# File 'lib/imap/backup/account/folder.rb', line 24 def name @name end |
Instance Method Details
#add_flags(uids, flags) ⇒ void
This method returns an undefined value.
Adds one or more flags to a group of messages
139 140 141 142 143 144 |
# File 'lib/imap/backup/account/folder.rb', line 139 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) ⇒ void
This method returns an undefined value.
Uploads a message
112 113 114 115 116 117 118 119 120 |
# File 'lib/imap/backup/account/folder.rb', line 112 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 ⇒ void
This method returns an undefined value.
Deletes all messages from the folder
155 156 157 158 159 160 161 |
# File 'lib/imap/backup/account/folder.rb', line 155 def clear existing = uids return if existing.empty? add_flags(existing, [:Deleted]) client.expunge end |
#create ⇒ void
This method returns an undefined value.
Creates the folder on the server
47 48 49 50 51 52 53 |
# File 'lib/imap/backup/account/folder.rb', line 47 def create return if exist? retry_on_error(errors: CREATE_RETRY_CLASSES) do client.create(utf7_encoded_name) end end |
#delete_multi(uids) ⇒ void
This method returns an undefined value.
Deletes multiple messages
124 125 126 127 |
# File 'lib/imap/backup/account/folder.rb', line 124 def delete_multi(uids) add_flags(uids, [:Deleted]) client.expunge end |
#exist? ⇒ Boolean
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/imap/backup/account/folder.rb', line 33 def exist? Logger.logger.debug "Checking whether folder '#{name}' exists" retry_on_error(errors: EXAMINE_RETRY_CLASSES) do examine end Logger.logger.debug "Folder '#{name}' exists" true rescue FolderNotFound Logger.logger.debug "Folder '#{name}' does not exist" false end |
#fetch_multi(uids, attr = [BODY_ATTRIBUTE, "FLAGS"]) ⇒ Array<Hash>?
Returns the requested messages.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/imap/backup/account/folder.rb', line 89 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) ⇒ void
This method returns an undefined value.
Removes one or more flags from a group of messages
148 149 150 151 |
# File 'lib/imap/backup/account/folder.rb', line 148 def remove_flags(uids, flags) client.select(utf7_encoded_name) client.uid_store(uids, "-FLAGS", flags) end |
#set_flags(uids, flags) ⇒ void
This method returns an undefined value.
Sets one or more flags on a group of messages
131 132 133 134 135 |
# File 'lib/imap/backup/account/folder.rb', line 131 def set_flags(uids, flags) client.select(utf7_encoded_name) flags.reject! { |f| f == :Recent } client.uid_store(uids, "FLAGS", flags) end |
#uid_validity ⇒ Integer
Returns the folder’s UID validity.
57 58 59 60 61 62 63 |
# File 'lib/imap/backup/account/folder.rb', line 57 def uid_validity @uid_validity ||= begin examine client.responses["UIDVALIDITY"][-1] end end |
#uids ⇒ Array<Integer>
Returns the folders message UIDs.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/imap/backup/account/folder.rb', line 67 def uids Logger.logger.debug "Fetching UIDs for folder '#{name}'" examine result = client.uid_search(["ALL"]).sort Logger.logger.debug "#{result.count} UIDs found for folder '#{name}'" result 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) ⇒ Array<Integer>
Returns the UIDs of messages with the ‘UNSEEN’ flag.
165 166 167 168 169 170 171 172 173 174 |
# File 'lib/imap/backup/account/folder.rb', line 165 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 |