net-imap 0.3.7 → 0.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of net-imap might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/.github/workflows/pages.yml +46 -0
- data/.github/workflows/test.yml +5 -12
- data/.gitignore +1 -0
- data/Gemfile +3 -0
- data/README.md +15 -4
- data/Rakefile +0 -7
- data/lib/net/imap/authenticators.rb +26 -57
- data/lib/net/imap/command_data.rb +13 -6
- data/lib/net/imap/deprecated_client_options.rb +139 -0
- data/lib/net/imap/errors.rb +20 -0
- data/lib/net/imap/response_data.rb +92 -47
- data/lib/net/imap/response_parser/parser_utils.rb +240 -0
- data/lib/net/imap/response_parser.rb +1265 -986
- data/lib/net/imap/sasl/anonymous_authenticator.rb +69 -0
- data/lib/net/imap/sasl/authentication_exchange.rb +107 -0
- data/lib/net/imap/sasl/authenticators.rb +118 -0
- data/lib/net/imap/sasl/client_adapter.rb +72 -0
- data/lib/net/imap/{authenticators/cram_md5.rb → sasl/cram_md5_authenticator.rb} +21 -11
- data/lib/net/imap/sasl/digest_md5_authenticator.rb +180 -0
- data/lib/net/imap/sasl/external_authenticator.rb +83 -0
- data/lib/net/imap/sasl/gs2_header.rb +80 -0
- data/lib/net/imap/{authenticators/login.rb → sasl/login_authenticator.rb} +25 -16
- data/lib/net/imap/sasl/oauthbearer_authenticator.rb +199 -0
- data/lib/net/imap/sasl/plain_authenticator.rb +101 -0
- data/lib/net/imap/sasl/protocol_adapters.rb +45 -0
- data/lib/net/imap/sasl/scram_algorithm.rb +58 -0
- data/lib/net/imap/sasl/scram_authenticator.rb +287 -0
- data/lib/net/imap/sasl/stringprep.rb +6 -66
- data/lib/net/imap/sasl/xoauth2_authenticator.rb +106 -0
- data/lib/net/imap/sasl.rb +144 -43
- data/lib/net/imap/sasl_adapter.rb +21 -0
- data/lib/net/imap/stringprep/nameprep.rb +70 -0
- data/lib/net/imap/stringprep/saslprep.rb +69 -0
- data/lib/net/imap/stringprep/saslprep_tables.rb +96 -0
- data/lib/net/imap/stringprep/tables.rb +146 -0
- data/lib/net/imap/stringprep/trace.rb +85 -0
- data/lib/net/imap/stringprep.rb +159 -0
- data/lib/net/imap.rb +993 -609
- data/net-imap.gemspec +4 -3
- data/rakelib/benchmarks.rake +98 -0
- data/rakelib/saslprep.rake +4 -4
- data/rakelib/string_prep_tables_generator.rb +82 -60
- metadata +29 -13
- data/benchmarks/stringprep.yml +0 -65
- data/benchmarks/table-regexps.yml +0 -39
- data/lib/net/imap/authenticators/digest_md5.rb +0 -115
- data/lib/net/imap/authenticators/plain.rb +0 -41
- data/lib/net/imap/authenticators/xoauth2.rb +0 -20
- data/lib/net/imap/sasl/saslprep.rb +0 -55
- data/lib/net/imap/sasl/saslprep_tables.rb +0 -98
- data/lib/net/imap/sasl/stringprep_tables.rb +0 -153
data/lib/net/imap.rb
CHANGED
@@ -24,11 +24,9 @@ end
|
|
24
24
|
module Net
|
25
25
|
|
26
26
|
# Net::IMAP implements Internet Message Access Protocol (\IMAP) client
|
27
|
-
# functionality. The protocol is described
|
28
|
-
#
|
29
|
-
|
30
|
-
# TODO: and [IMAP4rev2[https://tools.ietf.org/html/rfc9051]].
|
31
|
-
#++
|
27
|
+
# functionality. The protocol is described
|
28
|
+
# in {IMAP4rev1 [RFC3501]}[https://tools.ietf.org/html/rfc3501]
|
29
|
+
# and {IMAP4rev2 [RFC9051]}[https://tools.ietf.org/html/rfc9051].
|
32
30
|
#
|
33
31
|
# == \IMAP Overview
|
34
32
|
#
|
@@ -77,31 +75,22 @@ module Net
|
|
77
75
|
# UIDs have to be reassigned. An \IMAP client thus cannot
|
78
76
|
# rearrange message orders.
|
79
77
|
#
|
80
|
-
# ===
|
78
|
+
# === Examples of Usage
|
81
79
|
#
|
82
|
-
#
|
83
|
-
# #capability. Users of the class must check for required capabilities before
|
84
|
-
# issuing commands. Special care should be taken to follow all #capability
|
85
|
-
# requirements for #starttls, #login, and #authenticate.
|
86
|
-
#
|
87
|
-
# See the #capability method for more information.
|
88
|
-
#
|
89
|
-
# == Examples of Usage
|
90
|
-
#
|
91
|
-
# === List sender and subject of all recent messages in the default mailbox
|
80
|
+
# ==== List sender and subject of all recent messages in the default mailbox
|
92
81
|
#
|
93
82
|
# imap = Net::IMAP.new('mail.example.com')
|
94
|
-
# imap.authenticate('
|
83
|
+
# imap.authenticate('PLAIN', 'joe_user', 'joes_password')
|
95
84
|
# imap.examine('INBOX')
|
96
85
|
# imap.search(["RECENT"]).each do |message_id|
|
97
86
|
# envelope = imap.fetch(message_id, "ENVELOPE")[0].attr["ENVELOPE"]
|
98
87
|
# puts "#{envelope.from[0].name}: \t#{envelope.subject}"
|
99
88
|
# end
|
100
89
|
#
|
101
|
-
#
|
90
|
+
# ==== Move all messages from April 2003 from "Mail/sent-mail" to "Mail/sent-apr03"
|
102
91
|
#
|
103
92
|
# imap = Net::IMAP.new('mail.example.com')
|
104
|
-
# imap.authenticate('
|
93
|
+
# imap.authenticate('PLAIN', 'joe_user', 'joes_password')
|
105
94
|
# imap.select('Mail/sent-mail')
|
106
95
|
# if not imap.list('Mail/', 'sent-apr03')
|
107
96
|
# imap.create('Mail/sent-apr03')
|
@@ -112,12 +101,96 @@ module Net
|
|
112
101
|
# end
|
113
102
|
# imap.expunge
|
114
103
|
#
|
104
|
+
# == Capabilities
|
105
|
+
#
|
106
|
+
# Most Net::IMAP methods do not _currently_ modify their behaviour according
|
107
|
+
# to the server's advertised #capabilities. Users of this class must check
|
108
|
+
# that the server is capable of extension commands or command arguments before
|
109
|
+
# sending them. Special care should be taken to follow the #capabilities
|
110
|
+
# requirements for #starttls, #login, and #authenticate.
|
111
|
+
#
|
112
|
+
# See #capable?, #auth_capable?, #capabilities, #auth_mechanisms to discover
|
113
|
+
# server capabilities. For relevant capability requirements, see the
|
114
|
+
# documentation on each \IMAP command.
|
115
|
+
#
|
116
|
+
# imap = Net::IMAP.new("mail.example.com")
|
117
|
+
# imap.capable?(:IMAP4rev1) or raise "Not an IMAP4rev1 server"
|
118
|
+
# imap.capable?(:starttls) or raise "Cannot start TLS"
|
119
|
+
# imap.starttls
|
120
|
+
#
|
121
|
+
# if imap.auth_capable?("PLAIN")
|
122
|
+
# imap.authenticate "PLAIN", username, password
|
123
|
+
# elsif !imap.capability?("LOGINDISABLED")
|
124
|
+
# imap.login username, password
|
125
|
+
# else
|
126
|
+
# raise "No acceptable authentication mechanisms"
|
127
|
+
# end
|
128
|
+
#
|
129
|
+
# # Support for "UTF8=ACCEPT" implies support for "ENABLE"
|
130
|
+
# imap.enable :utf8 if imap.capable?("UTF8=ACCEPT")
|
131
|
+
#
|
132
|
+
# namespaces = imap.namespace if imap.capable?(:namespace)
|
133
|
+
# mbox_prefix = namespaces&.personal&.first&.prefix || ""
|
134
|
+
# mbox_delim = namespaces&.personal&.first&.delim || "/"
|
135
|
+
# mbox_path = prefix + %w[path to my mailbox].join(delim)
|
136
|
+
# imap.create mbox_path
|
137
|
+
#
|
138
|
+
# === Basic IMAP4rev1 capabilities
|
139
|
+
#
|
140
|
+
# IMAP4rev1 servers must advertise +IMAP4rev1+ in their capabilities list.
|
141
|
+
# IMAP4rev1 servers must _implement_ the +STARTTLS+, <tt>AUTH=PLAIN</tt>,
|
142
|
+
# and +LOGINDISABLED+ capabilities. See #starttls, #login, and #authenticate
|
143
|
+
# for the implications of these capabilities.
|
144
|
+
#
|
145
|
+
# === Caching +CAPABILITY+ responses
|
146
|
+
#
|
147
|
+
# Net::IMAP automatically stores and discards capability data according to the
|
148
|
+
# the requirements and recommendations in
|
149
|
+
# {IMAP4rev2 §6.1.1}[https://www.rfc-editor.org/rfc/rfc9051#section-6.1.1],
|
150
|
+
# {§6.2}[https://www.rfc-editor.org/rfc/rfc9051#section-6.2], and
|
151
|
+
# {§7.1}[https://www.rfc-editor.org/rfc/rfc9051#section-7.1].
|
152
|
+
# Use #capable?, #auth_capable?, or #capabilities to use this cache and avoid
|
153
|
+
# sending the #capability command unnecessarily.
|
154
|
+
#
|
155
|
+
# The server may advertise its initial capabilities using the +CAPABILITY+
|
156
|
+
# ResponseCode in a +PREAUTH+ or +OK+ #greeting. When TLS has started
|
157
|
+
# (#starttls) and after authentication (#login or #authenticate), the server's
|
158
|
+
# capabilities may change and cached capabilities are discarded. The server
|
159
|
+
# may send updated capabilities with an +OK+ TaggedResponse to #login or
|
160
|
+
# #authenticate, and these will be cached by Net::IMAP. But the
|
161
|
+
# TaggedResponse to #starttls MUST be ignored--it is sent before TLS starts
|
162
|
+
# and is unprotected.
|
163
|
+
#
|
164
|
+
# When storing capability values to variables, be careful that they are
|
165
|
+
# discarded or reset appropriately, especially following #starttls.
|
166
|
+
#
|
167
|
+
# === Using IMAP4rev1 extensions
|
168
|
+
#
|
169
|
+
# See the {IANA IMAP4 capabilities
|
170
|
+
# registry}[http://www.iana.org/assignments/imap4-capabilities] for a list of
|
171
|
+
# all standard capabilities, and their reference RFCs.
|
172
|
+
#
|
173
|
+
# IMAP4rev1 servers must not activate behavior that is incompatible with the
|
174
|
+
# base specification until an explicit client action invokes a capability,
|
175
|
+
# e.g. sending a command or command argument specific to that capability.
|
176
|
+
# Servers may send data with backward compatible behavior, such as response
|
177
|
+
# codes or mailbox attributes, at any time without client action.
|
178
|
+
#
|
179
|
+
# Invoking capabilities which are unknown to Net::IMAP may cause unexpected
|
180
|
+
# behavior and errors. For example, ResponseParseError is raised when
|
181
|
+
# unknown response syntax is received. Invoking commands or command
|
182
|
+
# parameters that are unsupported by the server may raise NoResponseError,
|
183
|
+
# BadResponseError, or cause other unexpected behavior.
|
184
|
+
#
|
185
|
+
# Some capabilities must be explicitly activated using the #enable command.
|
186
|
+
# See #enable for details.
|
187
|
+
#
|
115
188
|
# == Thread Safety
|
116
189
|
#
|
117
190
|
# Net::IMAP supports concurrent threads. For example,
|
118
191
|
#
|
119
192
|
# imap = Net::IMAP.new("imap.foo.net", "imap2")
|
120
|
-
# imap.authenticate("
|
193
|
+
# imap.authenticate("scram-md5", "bar", "password")
|
121
194
|
# imap.select("inbox")
|
122
195
|
# fetch_thread = Thread.start { imap.fetch(1..-1, "UID") }
|
123
196
|
# search_result = imap.search(["BODY", "hello"])
|
@@ -173,24 +246,54 @@ module Net
|
|
173
246
|
# == What's here?
|
174
247
|
#
|
175
248
|
# * {Connection control}[rdoc-ref:Net::IMAP@Connection+control+methods]
|
176
|
-
# * {
|
177
|
-
# * {...for any state}[rdoc-ref:Net::IMAP@IMAP+commands+for+any+state]
|
178
|
-
# * {...for the "not authenticated" state}[rdoc-ref:Net::IMAP@IMAP+commands+for+the+-22Not+Authenticated-22+state]
|
179
|
-
# * {...for the "authenticated" state}[rdoc-ref:Net::IMAP@IMAP+commands+for+the+-22Authenticated-22+state]
|
180
|
-
# * {...for the "selected" state}[rdoc-ref:Net::IMAP@IMAP+commands+for+the+-22Selected-22+state]
|
181
|
-
# * {...for the "logout" state}[rdoc-ref:Net::IMAP@IMAP+commands+for+the+-22Logout-22+state]
|
182
|
-
# * {Supported IMAP extensions}[rdoc-ref:Net::IMAP@Supported+IMAP+extensions]
|
249
|
+
# * {Server capabilities}[rdoc-ref:Net::IMAP@Server+capabilities]
|
183
250
|
# * {Handling server responses}[rdoc-ref:Net::IMAP@Handling+server+responses]
|
251
|
+
# * {Core IMAP commands}[rdoc-ref:Net::IMAP@Core+IMAP+commands]
|
252
|
+
# * {for any state}[rdoc-ref:Net::IMAP@Any+state]
|
253
|
+
# * {for the "not authenticated" state}[rdoc-ref:Net::IMAP@Not+Authenticated+state]
|
254
|
+
# * {for the "authenticated" state}[rdoc-ref:Net::IMAP@Authenticated+state]
|
255
|
+
# * {for the "selected" state}[rdoc-ref:Net::IMAP@Selected+state]
|
256
|
+
# * {for the "logout" state}[rdoc-ref:Net::IMAP@Logout+state]
|
257
|
+
# * {IMAP extension support}[rdoc-ref:Net::IMAP@IMAP+extension+support]
|
184
258
|
#
|
185
259
|
# === Connection control methods
|
186
260
|
#
|
187
|
-
# - Net::IMAP.new:
|
188
|
-
# successful server greeting before
|
261
|
+
# - Net::IMAP.new: Creates a new \IMAP client which connects immediately and
|
262
|
+
# waits for a successful server greeting before the method returns.
|
189
263
|
# - #starttls: Asks the server to upgrade a clear-text connection to use TLS.
|
190
264
|
# - #logout: Tells the server to end the session. Enters the "_logout_" state.
|
191
265
|
# - #disconnect: Disconnects the connection (without sending #logout first).
|
192
266
|
# - #disconnected?: True if the connection has been closed.
|
193
267
|
#
|
268
|
+
# === Server capabilities
|
269
|
+
#
|
270
|
+
# - #capable?: Returns whether the server supports a given capability.
|
271
|
+
# - #capabilities: Returns the server's capabilities as an array of strings.
|
272
|
+
# - #auth_capable?: Returns whether the server advertises support for a given
|
273
|
+
# SASL mechanism, for use with #authenticate.
|
274
|
+
# - #auth_mechanisms: Returns the #authenticate SASL mechanisms which
|
275
|
+
# the server claims to support as an array of strings.
|
276
|
+
# - #clear_cached_capabilities: Clears cached capabilities.
|
277
|
+
#
|
278
|
+
# <em>The capabilities cache is automatically cleared after completing
|
279
|
+
# #starttls, #login, or #authenticate.</em>
|
280
|
+
# - #capability: Sends the +CAPABILITY+ command and returns the #capabilities.
|
281
|
+
#
|
282
|
+
# <em>In general, #capable? should be used rather than explicitly sending a
|
283
|
+
# +CAPABILITY+ command to the server.</em>
|
284
|
+
#
|
285
|
+
# === Handling server responses
|
286
|
+
#
|
287
|
+
# - #greeting: The server's initial untagged response, which can indicate a
|
288
|
+
# pre-authenticated connection.
|
289
|
+
# - #responses: Yields unhandled UntaggedResponse#data and <em>non-+nil+</em>
|
290
|
+
# ResponseCode#data.
|
291
|
+
# - #clear_responses: Deletes unhandled data from #responses and returns it.
|
292
|
+
# - #add_response_handler: Add a block to be called inside the receiver thread
|
293
|
+
# with every server response.
|
294
|
+
# - #response_handlers: Returns the list of response handlers.
|
295
|
+
# - #remove_response_handler: Remove a previously added response handler.
|
296
|
+
#
|
194
297
|
# === Core \IMAP commands
|
195
298
|
#
|
196
299
|
# The following commands are defined either by
|
@@ -199,69 +302,48 @@ module Net
|
|
199
302
|
# [IDLE[https://tools.ietf.org/html/rfc2177]],
|
200
303
|
# [NAMESPACE[https://tools.ietf.org/html/rfc2342]],
|
201
304
|
# [UNSELECT[https://tools.ietf.org/html/rfc3691]],
|
202
|
-
|
203
|
-
# TODO: [ENABLE[https://tools.ietf.org/html/rfc5161]],
|
204
|
-
# TODO: [LIST-EXTENDED[https://tools.ietf.org/html/rfc5258]],
|
205
|
-
# TODO: [LIST-STATUS[https://tools.ietf.org/html/rfc5819]],
|
206
|
-
#++
|
305
|
+
# [ENABLE[https://tools.ietf.org/html/rfc5161]],
|
207
306
|
# [MOVE[https://tools.ietf.org/html/rfc6851]].
|
208
307
|
# These extensions are widely supported by modern IMAP4rev1 servers and have
|
209
308
|
# all been integrated into [IMAP4rev2[https://tools.ietf.org/html/rfc9051]].
|
210
|
-
# <em
|
211
|
-
#
|
212
|
-
|
213
|
-
# TODO: When IMAP4rev2 is supported, add the following to the each of the
|
214
|
-
# appropriate commands below.
|
215
|
-
# Note:: CHECK has been removed from IMAP4rev2.
|
216
|
-
# Note:: LSUB is obsoleted by +LIST-EXTENDED and has been removed from IMAP4rev2.
|
217
|
-
# <em>Some arguments require the +LIST-EXTENDED+ or +IMAP4rev2+ capability.</em>
|
218
|
-
# <em>Requires either the +ENABLE+ or +IMAP4rev2+ capability.</em>
|
219
|
-
# <em>Requires either the +NAMESPACE+ or +IMAP4rev2+ capability.</em>
|
220
|
-
# <em>Requires either the +IDLE+ or +IMAP4rev2+ capability.</em>
|
221
|
-
# <em>Requires either the +UNSELECT+ or +IMAP4rev2+ capability.</em>
|
222
|
-
# <em>Requires either the +UIDPLUS+ or +IMAP4rev2+ capability.</em>
|
223
|
-
# <em>Requires either the +MOVE+ or +IMAP4rev2+ capability.</em>
|
224
|
-
#++
|
225
|
-
#
|
226
|
-
# ==== \IMAP commands for any state
|
309
|
+
# <em>*NOTE:* Net::IMAP doesn't support IMAP4rev2 yet.</em>
|
310
|
+
#
|
311
|
+
# ==== Any state
|
227
312
|
#
|
228
313
|
# - #capability: Returns the server's capabilities as an array of strings.
|
229
314
|
#
|
230
|
-
# <em>
|
231
|
-
#
|
315
|
+
# <em>In general, #capable? should be used rather than explicitly sending a
|
316
|
+
# +CAPABILITY+ command to the server.</em>
|
232
317
|
# - #noop: Allows the server to send unsolicited untagged #responses.
|
233
318
|
# - #logout: Tells the server to end the session. Enters the "_logout_" state.
|
234
319
|
#
|
235
|
-
# ====
|
320
|
+
# ==== Not Authenticated state
|
236
321
|
#
|
237
|
-
# In addition to the
|
238
|
-
# the "<em>not authenticated</em>" state:
|
322
|
+
# In addition to the commands for any state, the following commands are valid
|
323
|
+
# in the "<em>not authenticated</em>" state:
|
239
324
|
#
|
240
325
|
# - #starttls: Upgrades a clear-text connection to use TLS.
|
241
326
|
#
|
242
327
|
# <em>Requires the +STARTTLS+ capability.</em>
|
243
|
-
# - #authenticate: Identifies the client to the server using
|
244
|
-
# mechanism}[https://www.iana.org/assignments/sasl-mechanisms/sasl-mechanisms.xhtml]
|
245
|
-
# Enters the "_authenticated_" state.
|
328
|
+
# - #authenticate: Identifies the client to the server using the given
|
329
|
+
# {SASL mechanism}[https://www.iana.org/assignments/sasl-mechanisms/sasl-mechanisms.xhtml]
|
330
|
+
# and credentials. Enters the "_authenticated_" state.
|
246
331
|
#
|
247
|
-
# <em>
|
248
|
-
#
|
332
|
+
# <em>The server should list <tt>"AUTH=#{mechanism}"</tt> capabilities for
|
333
|
+
# supported mechanisms.</em>
|
249
334
|
# - #login: Identifies the client to the server using a plain text password.
|
250
335
|
# Using #authenticate is generally preferred. Enters the "_authenticated_"
|
251
336
|
# state.
|
252
337
|
#
|
253
338
|
# <em>The +LOGINDISABLED+ capability</em> <b>must NOT</b> <em>be listed.</em>
|
254
339
|
#
|
255
|
-
# ====
|
256
|
-
#
|
257
|
-
# In addition to the universal commands, the following commands are valid in
|
258
|
-
# the "_authenticated_" state:
|
340
|
+
# ==== Authenticated state
|
259
341
|
#
|
260
|
-
|
261
|
-
#
|
342
|
+
# In addition to the commands for any state, the following commands are valid
|
343
|
+
# in the "_authenticated_" state:
|
262
344
|
#
|
263
|
-
#
|
264
|
-
|
345
|
+
# - #enable: Enables backwards incompatible server extensions.
|
346
|
+
# <em>Requires the +ENABLE+ or +IMAP4rev2+ capability.</em>
|
265
347
|
# - #select: Open a mailbox and enter the "_selected_" state.
|
266
348
|
# - #examine: Open a mailbox read-only, and enter the "_selected_" state.
|
267
349
|
# - #create: Creates a new mailbox.
|
@@ -271,37 +353,31 @@ module Net
|
|
271
353
|
# - #unsubscribe: Removes a mailbox from the "subscribed" set.
|
272
354
|
# - #list: Returns names and attributes of mailboxes matching a given pattern.
|
273
355
|
# - #namespace: Returns mailbox namespaces, with path prefixes and delimiters.
|
274
|
-
#
|
275
|
-
# <em>Requires the +NAMESPACE+ capability.</em>
|
356
|
+
# <em>Requires the +NAMESPACE+ or +IMAP4rev2+ capability.</em>
|
276
357
|
# - #status: Returns mailbox information, e.g. message count, unseen message
|
277
358
|
# count, +UIDVALIDITY+ and +UIDNEXT+.
|
278
359
|
# - #append: Appends a message to the end of a mailbox.
|
279
360
|
# - #idle: Allows the server to send updates to the client, without the client
|
280
361
|
# needing to poll using #noop.
|
362
|
+
# <em>Requires the +IDLE+ or +IMAP4rev2+ capability.</em>
|
363
|
+
# - *Obsolete* #lsub: <em>Replaced by <tt>LIST-EXTENDED</tt> and removed from
|
364
|
+
# +IMAP4rev2+.</em> Lists mailboxes in the "subscribed" set.
|
281
365
|
#
|
282
|
-
# <em
|
283
|
-
# - #lsub: Lists mailboxes the user has declared "active" or "subscribed".
|
284
|
-
#--
|
285
|
-
# <em>Replaced by</em> <tt>LIST-EXTENDED</tt> <em>and removed from</em>
|
286
|
-
# +IMAP4rev2+. <em>However, Net::IMAP hasn't implemented</em>
|
287
|
-
# <tt>LIST-EXTENDED</tt> _yet_.
|
288
|
-
#++
|
366
|
+
# <em>*Note:* Net::IMAP hasn't implemented <tt>LIST-EXTENDED</tt> yet.</em>
|
289
367
|
#
|
290
|
-
# ====
|
368
|
+
# ==== Selected state
|
291
369
|
#
|
292
|
-
# In addition to the
|
293
|
-
# following commands are valid in the "_selected_" state:
|
370
|
+
# In addition to the commands for any state and the "_authenticated_"
|
371
|
+
# commands, the following commands are valid in the "_selected_" state:
|
294
372
|
#
|
295
373
|
# - #close: Closes the mailbox and returns to the "_authenticated_" state,
|
296
374
|
# expunging deleted messages, unless the mailbox was opened as read-only.
|
297
375
|
# - #unselect: Closes the mailbox and returns to the "_authenticated_" state,
|
298
376
|
# without expunging any messages.
|
299
|
-
#
|
300
|
-
# <em>Requires the +UNSELECT+ capability.</em>
|
377
|
+
# <em>Requires the +UNSELECT+ or +IMAP4rev2+ capability.</em>
|
301
378
|
# - #expunge: Permanently removes messages which have the Deleted flag set.
|
302
|
-
# - #uid_expunge: Restricts
|
303
|
-
#
|
304
|
-
# <em>Requires the +UIDPLUS+ capability.</em>
|
379
|
+
# - #uid_expunge: Restricts expunge to only remove the specified UIDs.
|
380
|
+
# <em>Requires the +UIDPLUS+ or +IMAP4rev2+ capability.</em>
|
305
381
|
# - #search, #uid_search: Returns sequence numbers or UIDs of messages that
|
306
382
|
# match the given searching criteria.
|
307
383
|
# - #fetch, #uid_fetch: Returns data associated with a set of messages,
|
@@ -311,45 +387,35 @@ module Net
|
|
311
387
|
# specified destination mailbox.
|
312
388
|
# - #move, #uid_move: Moves the specified messages to the end of the
|
313
389
|
# specified destination mailbox, expunging them from the current mailbox.
|
390
|
+
# <em>Requires the +MOVE+ or +IMAP4rev2+ capability.</em>
|
391
|
+
# - #check: <em>*Obsolete:* removed from +IMAP4rev2+.</em>
|
392
|
+
# Can be replaced with #noop or #idle.
|
314
393
|
#
|
315
|
-
#
|
316
|
-
# - #check: Mostly obsolete. Can be replaced with #noop or #idle.
|
317
|
-
#--
|
318
|
-
# <em>Removed from IMAP4rev2.</em>
|
319
|
-
#++
|
320
|
-
#
|
321
|
-
# ==== \IMAP commands for the "Logout" state
|
394
|
+
# ==== Logout state
|
322
395
|
#
|
323
|
-
# No \IMAP commands are valid in the
|
396
|
+
# No \IMAP commands are valid in the "_logout_" state. If the socket is still
|
324
397
|
# open, Net::IMAP will close it after receiving server confirmation.
|
325
398
|
# Exceptions will be raised by \IMAP commands that have already started and
|
326
399
|
# are waiting for a response, as well as any that are called after logout.
|
327
400
|
#
|
328
|
-
# ===
|
401
|
+
# === \IMAP extension support
|
329
402
|
#
|
330
403
|
# ==== RFC9051: +IMAP4rev2+
|
331
404
|
#
|
332
|
-
# Although IMAP4rev2[https://tools.ietf.org/html/rfc9051] is
|
333
|
-
# yet
|
334
|
-
#
|
335
|
-
|
336
|
-
#
|
337
|
-
#
|
338
|
-
#
|
339
|
-
#
|
340
|
-
#
|
341
|
-
#
|
342
|
-
#
|
343
|
-
#
|
344
|
-
#
|
345
|
-
#
|
346
|
-
# implicitly supported, but we can do better: Response codes: RFC5530, etc
|
347
|
-
# implicitly supported, but we can do better: <tt>STATUS=SIZE</tt>
|
348
|
-
# implicitly supported, but we can do better: <tt>STATUS DELETED</tt>
|
349
|
-
#++
|
350
|
-
# Commands for these extensions are included with the {Core IMAP
|
351
|
-
# commands}[rdoc-ref:Net::IMAP@Core+IMAP+commands], above. Other supported
|
352
|
-
# extensons are listed below.
|
405
|
+
# Although IMAP4rev2[https://tools.ietf.org/html/rfc9051] is not supported
|
406
|
+
# yet, Net::IMAP supports several extensions that have been folded into it:
|
407
|
+
# +ENABLE+, +IDLE+, +MOVE+, +NAMESPACE+, +SASL-IR+, +UIDPLUS+, and +UNSELECT+.
|
408
|
+
# Commands for these extensions are listed with the {Core IMAP
|
409
|
+
# commands}[rdoc-ref:Net::IMAP@Core+IMAP+commands], above.
|
410
|
+
#
|
411
|
+
# >>>
|
412
|
+
# <em>The following are folded into +IMAP4rev2+ but are currently
|
413
|
+
# unsupported or incompletely supported by</em> Net::IMAP<em>: RFC4466
|
414
|
+
# extensions, +ESEARCH+, +SEARCHRES+, +LIST-EXTENDED+,
|
415
|
+
# +LIST-STATUS+, +LITERAL-+, +BINARY+ fetch, and +SPECIAL-USE+. The
|
416
|
+
# following extensions are implicitly supported, but will be updated with
|
417
|
+
# more direct support: RFC5530 response codes, <tt>STATUS=SIZE</tt>, and
|
418
|
+
# <tt>STATUS=DELETED</tt>.</em>
|
353
419
|
#
|
354
420
|
# ==== RFC2087: +QUOTA+
|
355
421
|
# - #getquota: returns the resource usage and limits for a quota root
|
@@ -358,92 +424,47 @@ module Net
|
|
358
424
|
# - #setquota: sets the resource limits for a given quota root.
|
359
425
|
#
|
360
426
|
# ==== RFC2177: +IDLE+
|
361
|
-
# Folded into IMAP4rev2[https://tools.ietf.org/html/rfc9051]
|
362
|
-
#
|
427
|
+
# Folded into IMAP4rev2[https://tools.ietf.org/html/rfc9051] and also included
|
428
|
+
# above with {Core IMAP commands}[rdoc-ref:Net::IMAP@Core+IMAP+commands].
|
363
429
|
# - #idle: Allows the server to send updates to the client, without the client
|
364
430
|
# needing to poll using #noop.
|
365
431
|
#
|
366
432
|
# ==== RFC2342: +NAMESPACE+
|
367
|
-
# Folded into IMAP4rev2[https://tools.ietf.org/html/rfc9051]
|
368
|
-
#
|
433
|
+
# Folded into IMAP4rev2[https://tools.ietf.org/html/rfc9051] and also included
|
434
|
+
# above with {Core IMAP commands}[rdoc-ref:Net::IMAP@Core+IMAP+commands].
|
369
435
|
# - #namespace: Returns mailbox namespaces, with path prefixes and delimiters.
|
370
436
|
#
|
371
437
|
# ==== RFC2971: +ID+
|
372
438
|
# - #id: exchanges client and server implementation information.
|
373
439
|
#
|
374
|
-
#--
|
375
|
-
# ==== RFC3502: +MULTIAPPEND+
|
376
|
-
# TODO...
|
377
|
-
#++
|
378
|
-
#
|
379
|
-
#--
|
380
|
-
# ==== RFC3516: +BINARY+
|
381
|
-
# TODO...
|
382
|
-
#++
|
383
|
-
#
|
384
440
|
# ==== RFC3691: +UNSELECT+
|
385
|
-
# Folded into IMAP4rev2[https://tools.ietf.org/html/rfc9051]
|
386
|
-
#
|
441
|
+
# Folded into IMAP4rev2[https://tools.ietf.org/html/rfc9051] and also included
|
442
|
+
# above with {Core IMAP commands}[rdoc-ref:Net::IMAP@Core+IMAP+commands].
|
387
443
|
# - #unselect: Closes the mailbox and returns to the "_authenticated_" state,
|
388
444
|
# without expunging any messages.
|
389
445
|
#
|
390
446
|
# ==== RFC4314: +ACL+
|
391
447
|
# - #getacl: lists the authenticated user's access rights to a mailbox.
|
392
448
|
# - #setacl: sets the access rights for a user on a mailbox
|
393
|
-
|
394
|
-
#
|
395
|
-
#++
|
396
|
-
# - *_Note:_* +DELETEACL+, +LISTRIGHTS+, and +MYRIGHTS+ are not supported yet.
|
449
|
+
# >>>
|
450
|
+
# *NOTE:* +DELETEACL+, +LISTRIGHTS+, and +MYRIGHTS+ are not supported yet.
|
397
451
|
#
|
398
452
|
# ==== RFC4315: +UIDPLUS+
|
399
|
-
# Folded into IMAP4rev2[https://tools.ietf.org/html/rfc9051]
|
400
|
-
#
|
453
|
+
# Folded into IMAP4rev2[https://tools.ietf.org/html/rfc9051] and also included
|
454
|
+
# above with {Core IMAP commands}[rdoc-ref:Net::IMAP@Core+IMAP+commands].
|
401
455
|
# - #uid_expunge: Restricts #expunge to only remove the specified UIDs.
|
402
456
|
# - Updates #select, #examine with the +UIDNOTSTICKY+ ResponseCode
|
403
457
|
# - Updates #append with the +APPENDUID+ ResponseCode
|
404
458
|
# - Updates #copy, #move with the +COPYUID+ ResponseCode
|
405
459
|
#
|
406
|
-
#--
|
407
|
-
# ==== RFC4466: Collected Extensions to IMAP4 ABNF
|
408
|
-
# TODO...
|
409
|
-
# Folded into IMAP4rev2[https://tools.ietf.org/html/rfc9051], this RFC updates
|
410
|
-
# the protocol to enable new optional parameters to many commands: #select,
|
411
|
-
# #examine, #create, #rename, #fetch, #uid_fetch, #store, #uid_store, #search,
|
412
|
-
# #uid_search, and #append. However, specific parameters are not defined.
|
413
|
-
# Extensions to these commands use this syntax whenever possible. Net::IMAP
|
414
|
-
# may be partially compatible with extensions to these commands, even without
|
415
|
-
# any explicit support.
|
416
|
-
#++
|
417
|
-
#
|
418
|
-
#--
|
419
|
-
# ==== RFC4731 +ESEARCH+
|
420
|
-
# TODO...
|
421
|
-
# Folded into IMAP4rev2[https://tools.ietf.org/html/rfc9051].
|
422
|
-
# - Updates #search, #uid_search to accept result options: +MIN+, +MAX+,
|
423
|
-
# +ALL+, +COUNT+, and to return ExtendedSearchData.
|
424
|
-
#++
|
425
|
-
#
|
426
|
-
#--
|
427
460
|
# ==== RFC4959: +SASL-IR+
|
428
|
-
# TODO...
|
429
461
|
# Folded into IMAP4rev2[https://tools.ietf.org/html/rfc9051].
|
430
|
-
# - Updates #authenticate to
|
431
|
-
#++
|
432
|
-
#
|
433
|
-
#--
|
434
|
-
# ==== RFC4978: COMPRESS=DEFLATE
|
435
|
-
# TODO...
|
436
|
-
#++
|
462
|
+
# - Updates #authenticate with the option to send an initial response.
|
437
463
|
#
|
438
|
-
|
439
|
-
#
|
440
|
-
#
|
441
|
-
#
|
442
|
-
# - Updates #search, #uid_search with the +SAVE+ result option.
|
443
|
-
# - Updates #copy, #uid_copy, #fetch, #uid_fetch, #move, #uid_move, #search,
|
444
|
-
# #uid_search, #store, #uid_store, and #uid_expunge with ability to
|
445
|
-
# reference the saved result of a previous #search or #uid_search command.
|
446
|
-
#++
|
464
|
+
# ==== RFC5161: +ENABLE+
|
465
|
+
# Folded into IMAP4rev2[https://tools.ietf.org/html/rfc9051] and also included
|
466
|
+
# above with {Core IMAP commands}[rdoc-ref:Net::IMAP@Core+IMAP+commands].
|
467
|
+
# - #enable: Enables backwards incompatible server extensions.
|
447
468
|
#
|
448
469
|
# ==== RFC5256: +SORT+
|
449
470
|
# - #sort, #uid_sort: An alternate version of #search or #uid_search which
|
@@ -453,75 +474,20 @@ module Net
|
|
453
474
|
# which arranges the results into ordered groups or threads according to a
|
454
475
|
# chosen algorithm.
|
455
476
|
#
|
456
|
-
#--
|
457
|
-
# ==== RFC5258 +LIST-EXTENDED+
|
458
|
-
# TODO...
|
459
|
-
# Folded into IMAP4rev2[https://tools.ietf.org/html/rfc9051], this updates the
|
460
|
-
# protocol with new optional parameters to the #list command, adding a few of
|
461
|
-
# its own. Net::IMAP may be forward-compatible with future #list extensions,
|
462
|
-
# even without any explicit support.
|
463
|
-
# - Updates #list to accept selection options: +SUBSCRIBED+, +REMOTE+, and
|
464
|
-
# +RECURSIVEMATCH+, and return options: +SUBSCRIBED+ and +CHILDREN+.
|
465
|
-
#++
|
466
|
-
#
|
467
|
-
#--
|
468
|
-
# ==== RFC5819 +LIST-STATUS+
|
469
|
-
# TODO...
|
470
|
-
# Folded into IMAP4rev2[https://tools.ietf.org/html/rfc9051].
|
471
|
-
# - Updates #list with +STATUS+ return option.
|
472
|
-
#++
|
473
|
-
#
|
474
477
|
# ==== +XLIST+ (non-standard, deprecated)
|
475
478
|
# - #xlist: replaced by +SPECIAL-USE+ attributes in #list responses.
|
476
479
|
#
|
477
|
-
#--
|
478
|
-
# ==== RFC6154 +SPECIAL-USE+
|
479
|
-
# TODO...
|
480
|
-
# Folded into IMAP4rev2[https://tools.ietf.org/html/rfc9051].
|
481
|
-
# - Updates #list with the +SPECIAL-USE+ selection and return options.
|
482
|
-
#++
|
483
|
-
#
|
484
480
|
# ==== RFC6851: +MOVE+
|
485
|
-
# Folded into IMAP4rev2[https://tools.ietf.org/html/rfc9051]
|
486
|
-
#
|
481
|
+
# Folded into IMAP4rev2[https://tools.ietf.org/html/rfc9051] and also included
|
482
|
+
# above with {Core IMAP commands}[rdoc-ref:Net::IMAP@Core+IMAP+commands].
|
487
483
|
# - #move, #uid_move: Moves the specified messages to the end of the
|
488
484
|
# specified destination mailbox, expunging them from the current mailbox.
|
489
485
|
#
|
490
|
-
|
491
|
-
# ==== RFC6855: UTF8=ACCEPT
|
492
|
-
# TODO...
|
493
|
-
# ==== RFC6855: UTF8=ONLY
|
494
|
-
# TODO...
|
495
|
-
#++
|
496
|
-
#
|
497
|
-
#--
|
498
|
-
# ==== RFC7888: <tt>LITERAL+</tt>, +LITERAL-+
|
499
|
-
# TODO...
|
500
|
-
# ==== RFC7162: +QRESYNC+
|
501
|
-
# TODO...
|
502
|
-
# ==== RFC7162: +CONDSTORE+
|
503
|
-
# TODO...
|
504
|
-
# ==== RFC8474: +OBJECTID+
|
505
|
-
# TODO...
|
506
|
-
# ==== RFC9208: +QUOTA+
|
507
|
-
# TODO...
|
508
|
-
#++
|
509
|
-
#
|
510
|
-
# === Handling server responses
|
511
|
-
#
|
512
|
-
# - #greeting: The server's initial untagged response, which can indicate a
|
513
|
-
# pre-authenticated connection.
|
514
|
-
# - #responses: A hash with arrays of unhandled <em>non-+nil+</em>
|
515
|
-
# UntaggedResponse and ResponseCode +#data+, keyed by +#name+.
|
516
|
-
# - #add_response_handler: Add a block to be called inside the receiver thread
|
517
|
-
# with every server response.
|
518
|
-
# - #remove_response_handler: Remove a previously added response handler.
|
486
|
+
# ==== RFC6855: <tt>UTF8=ACCEPT</tt>, <tt>UTF8=ONLY</tt>
|
519
487
|
#
|
488
|
+
# - See #enable for information about support for UTF-8 string encoding.
|
520
489
|
#
|
521
490
|
# == References
|
522
|
-
#--
|
523
|
-
# TODO: Consider moving references list to REFERENCES.md or REFERENCES.rdoc.
|
524
|
-
#++
|
525
491
|
#
|
526
492
|
# [{IMAP4rev1}[https://www.rfc-editor.org/rfc/rfc3501.html]]::
|
527
493
|
# Crispin, M., "INTERNET MESSAGE ACCESS PROTOCOL - \VERSION 4rev1",
|
@@ -622,27 +588,21 @@ module Net
|
|
622
588
|
# RFC 1864, DOI 10.17487/RFC1864, October 1995,
|
623
589
|
# <https://www.rfc-editor.org/info/rfc1864>.
|
624
590
|
#
|
625
|
-
|
626
|
-
#
|
627
|
-
#
|
628
|
-
#
|
629
|
-
#
|
630
|
-
# profile for Internet Message Access Protocol (IMAP)",
|
631
|
-
# RFC 3503, DOI 10.17487/RFC3503, March 2003,
|
632
|
-
# <https://www.rfc-editor.org/info/rfc3503>.
|
633
|
-
#++
|
591
|
+
# [RFC3503[https://tools.ietf.org/html/rfc3503]]::
|
592
|
+
# Melnikov, A., "Message Disposition Notification (MDN)
|
593
|
+
# profile for Internet Message Access Protocol (IMAP)",
|
594
|
+
# RFC 3503, DOI 10.17487/RFC3503, March 2003,
|
595
|
+
# <https://www.rfc-editor.org/info/rfc3503>.
|
634
596
|
#
|
635
|
-
# ===
|
597
|
+
# === \IMAP Extensions
|
636
598
|
#
|
637
|
-
# [QUOTA[https://tools.ietf.org/html/rfc2087]]::
|
638
|
-
# Myers, J., "IMAP4 QUOTA extension", RFC 2087, DOI 10.17487/RFC2087,
|
639
|
-
# January 1997, <https://www.rfc-editor.org/info/rfc2087>.
|
640
|
-
#--
|
641
|
-
# TODO: test compatibility with updated QUOTA extension:
|
642
599
|
# [QUOTA[https://tools.ietf.org/html/rfc9208]]::
|
643
600
|
# Melnikov, A., "IMAP QUOTA Extension", RFC 9208, DOI 10.17487/RFC9208,
|
644
601
|
# March 2022, <https://www.rfc-editor.org/info/rfc9208>.
|
645
|
-
|
602
|
+
#
|
603
|
+
# <em>Note: obsoletes</em>
|
604
|
+
# RFC-2087[https://tools.ietf.org/html/rfc2087]<em> (January 1997)</em>.
|
605
|
+
# <em>Net::IMAP does not fully support the RFC9208 updates yet.</em>
|
646
606
|
# [IDLE[https://tools.ietf.org/html/rfc2177]]::
|
647
607
|
# Leiba, B., "IMAP4 IDLE command", RFC 2177, DOI 10.17487/RFC2177,
|
648
608
|
# June 1997, <https://www.rfc-editor.org/info/rfc2177>.
|
@@ -675,31 +635,44 @@ module Net
|
|
675
635
|
# Gulbrandsen, A. and N. Freed, Ed., "Internet Message Access Protocol
|
676
636
|
# (\IMAP) - MOVE Extension", RFC 6851, DOI 10.17487/RFC6851, January 2013,
|
677
637
|
# <https://www.rfc-editor.org/info/rfc6851>.
|
638
|
+
# [UTF8=ACCEPT[https://tools.ietf.org/html/rfc6855]]::
|
639
|
+
# [UTF8=ONLY[https://tools.ietf.org/html/rfc6855]]::
|
640
|
+
# Resnick, P., Ed., Newman, C., Ed., and S. Shen, Ed.,
|
641
|
+
# "IMAP Support for UTF-8", RFC 6855, DOI 10.17487/RFC6855, March 2013,
|
642
|
+
# <https://www.rfc-editor.org/info/rfc6855>.
|
678
643
|
#
|
679
644
|
# === IANA registries
|
680
|
-
#
|
681
645
|
# * {IMAP Capabilities}[http://www.iana.org/assignments/imap4-capabilities]
|
682
646
|
# * {IMAP Response Codes}[https://www.iana.org/assignments/imap-response-codes/imap-response-codes.xhtml]
|
683
647
|
# * {IMAP Mailbox Name Attributes}[https://www.iana.org/assignments/imap-mailbox-name-attributes/imap-mailbox-name-attributes.xhtml]
|
684
648
|
# * {IMAP and JMAP Keywords}[https://www.iana.org/assignments/imap-jmap-keywords/imap-jmap-keywords.xhtml]
|
685
649
|
# * {IMAP Threading Algorithms}[https://www.iana.org/assignments/imap-threading-algorithms/imap-threading-algorithms.xhtml]
|
686
|
-
#--
|
687
|
-
# * {IMAP Quota Resource Types}[http://www.iana.org/assignments/imap4-capabilities#imap-capabilities-2]
|
688
|
-
# * [{LIST-EXTENDED options and responses}[https://www.iana.org/assignments/imap-list-extended/imap-list-extended.xhtml]
|
689
|
-
# * {IMAP METADATA Server Entry and Mailbox Entry Registries}[https://www.iana.org/assignments/imap-metadata/imap-metadata.xhtml]
|
690
|
-
# * {IMAP ANNOTATE Extension Entries and Attributes}[https://www.iana.org/assignments/imap-annotate-extension/imap-annotate-extension.xhtml]
|
691
|
-
# * {IMAP URLAUTH Access Identifiers and Prefixes}[https://www.iana.org/assignments/urlauth-access-ids/urlauth-access-ids.xhtml]
|
692
|
-
# * {IMAP URLAUTH Authorization Mechanism Registry}[https://www.iana.org/assignments/urlauth-authorization-mechanism-registry/urlauth-authorization-mechanism-registry.xhtml]
|
693
|
-
#++
|
694
650
|
# * {SASL Mechanisms and SASL SCRAM Family Mechanisms}[https://www.iana.org/assignments/sasl-mechanisms/sasl-mechanisms.xhtml]
|
695
651
|
# * {Service Name and Transport Protocol Port Number Registry}[https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xml]:
|
696
652
|
# +imap+: tcp/143, +imaps+: tcp/993
|
697
653
|
# * {GSSAPI/Kerberos/SASL Service Names}[https://www.iana.org/assignments/gssapi-service-names/gssapi-service-names.xhtml]:
|
698
654
|
# +imap+
|
699
655
|
# * {Character sets}[https://www.iana.org/assignments/character-sets/character-sets.xhtml]
|
656
|
+
# ===== For currently unsupported features:
|
657
|
+
# * {IMAP Quota Resource Types}[http://www.iana.org/assignments/imap4-capabilities#imap-capabilities-2]
|
658
|
+
# * {LIST-EXTENDED options and responses}[https://www.iana.org/assignments/imap-list-extended/imap-list-extended.xhtml]
|
659
|
+
# * {IMAP METADATA Server Entry and Mailbox Entry Registries}[https://www.iana.org/assignments/imap-metadata/imap-metadata.xhtml]
|
660
|
+
# * {IMAP ANNOTATE Extension Entries and Attributes}[https://www.iana.org/assignments/imap-annotate-extension/imap-annotate-extension.xhtml]
|
661
|
+
# * {IMAP URLAUTH Access Identifiers and Prefixes}[https://www.iana.org/assignments/urlauth-access-ids/urlauth-access-ids.xhtml]
|
662
|
+
# * {IMAP URLAUTH Authorization Mechanism Registry}[https://www.iana.org/assignments/urlauth-authorization-mechanism-registry/urlauth-authorization-mechanism-registry.xhtml]
|
700
663
|
#
|
701
664
|
class IMAP < Protocol
|
702
|
-
VERSION = "0.
|
665
|
+
VERSION = "0.4.4"
|
666
|
+
|
667
|
+
# Aliases for supported capabilities, to be used with the #enable command.
|
668
|
+
ENABLE_ALIASES = {
|
669
|
+
utf8: "UTF8=ACCEPT",
|
670
|
+
"UTF8=ONLY" => "UTF8=ACCEPT",
|
671
|
+
}.freeze
|
672
|
+
|
673
|
+
autoload :SASL, File.expand_path("imap/sasl", __dir__)
|
674
|
+
autoload :SASLAdapter, File.expand_path("imap/sasl_adapter", __dir__)
|
675
|
+
autoload :StringPrep, File.expand_path("imap/stringprep", __dir__)
|
703
676
|
|
704
677
|
include MonitorMixin
|
705
678
|
if defined?(OpenSSL::SSL)
|
@@ -707,35 +680,6 @@ module Net
|
|
707
680
|
include SSL
|
708
681
|
end
|
709
682
|
|
710
|
-
# Returns the initial greeting the server, an UntaggedResponse.
|
711
|
-
attr_reader :greeting
|
712
|
-
|
713
|
-
# Returns a hash with arrays of unhandled <em>non-+nil+</em>
|
714
|
-
# UntaggedResponse#data keyed by UntaggedResponse#name, and
|
715
|
-
# ResponseCode#data keyed by ResponseCode#name.
|
716
|
-
#
|
717
|
-
# For example:
|
718
|
-
#
|
719
|
-
# imap.select("inbox")
|
720
|
-
# p imap.responses["EXISTS"][-1]
|
721
|
-
# #=> 2
|
722
|
-
# p imap.responses["UIDVALIDITY"][-1]
|
723
|
-
# #=> 968263756
|
724
|
-
attr_reader :responses
|
725
|
-
|
726
|
-
# Returns all response handlers.
|
727
|
-
attr_reader :response_handlers
|
728
|
-
|
729
|
-
# Seconds to wait until a connection is opened.
|
730
|
-
# If the IMAP object cannot open a connection within this time,
|
731
|
-
# it raises a Net::OpenTimeout exception. The default value is 30 seconds.
|
732
|
-
attr_reader :open_timeout
|
733
|
-
|
734
|
-
# Seconds to wait until an IDLE response is received.
|
735
|
-
attr_reader :idle_response_timeout
|
736
|
-
|
737
|
-
attr_accessor :client_thread # :nodoc:
|
738
|
-
|
739
683
|
# Returns the debug mode.
|
740
684
|
def self.debug
|
741
685
|
return @@debug
|
@@ -762,9 +706,175 @@ module Net
|
|
762
706
|
alias default_ssl_port default_tls_port
|
763
707
|
end
|
764
708
|
|
709
|
+
# Returns the initial greeting the server, an UntaggedResponse.
|
710
|
+
attr_reader :greeting
|
711
|
+
|
712
|
+
# Seconds to wait until a connection is opened.
|
713
|
+
# If the IMAP object cannot open a connection within this time,
|
714
|
+
# it raises a Net::OpenTimeout exception. The default value is 30 seconds.
|
715
|
+
attr_reader :open_timeout
|
716
|
+
|
717
|
+
# Seconds to wait until an IDLE response is received.
|
718
|
+
attr_reader :idle_response_timeout
|
719
|
+
|
720
|
+
# The hostname this client connected to
|
721
|
+
attr_reader :host
|
722
|
+
|
723
|
+
# The port this client connected to
|
724
|
+
attr_reader :port
|
725
|
+
|
726
|
+
# Returns the
|
727
|
+
# {SSLContext}[https://docs.ruby-lang.org/en/master/OpenSSL/SSL/SSLContext.html]
|
728
|
+
# used by the SSLSocket when TLS is attempted, even when the TLS handshake
|
729
|
+
# is unsuccessful. The context object will be frozen.
|
730
|
+
#
|
731
|
+
# Returns +nil+ for a plaintext connection.
|
732
|
+
attr_reader :ssl_ctx
|
733
|
+
|
734
|
+
# Returns the parameters that were sent to #ssl_ctx
|
735
|
+
# {set_params}[https://docs.ruby-lang.org/en/master/OpenSSL/SSL/SSLContext.html#method-i-set_params]
|
736
|
+
# when the connection tries to use TLS (even when unsuccessful).
|
737
|
+
#
|
738
|
+
# Returns +false+ for a plaintext connection.
|
739
|
+
attr_reader :ssl_ctx_params
|
740
|
+
|
741
|
+
# Creates a new Net::IMAP object and connects it to the specified
|
742
|
+
# +host+.
|
743
|
+
#
|
744
|
+
# ==== Options
|
745
|
+
#
|
746
|
+
# Accepts the following options:
|
747
|
+
#
|
748
|
+
# [port]
|
749
|
+
# Port number. Defaults to 993 when +ssl+ is truthy, and 143 otherwise.
|
750
|
+
#
|
751
|
+
# [ssl]
|
752
|
+
# If +true+, the connection will use TLS with the default params set by
|
753
|
+
# {OpenSSL::SSL::SSLContext#set_params}[https://docs.ruby-lang.org/en/master/OpenSSL/SSL/SSLContext.html#method-i-set_params].
|
754
|
+
# If +ssl+ is a hash, it's passed to
|
755
|
+
# {OpenSSL::SSL::SSLContext#set_params}[https://docs.ruby-lang.org/en/master/OpenSSL/SSL/SSLContext.html#method-i-set_params];
|
756
|
+
# the keys are names of attribute assignment methods on
|
757
|
+
# SSLContext[https://docs.ruby-lang.org/en/master/OpenSSL/SSL/SSLContext.html].
|
758
|
+
#
|
759
|
+
# [open_timeout]
|
760
|
+
# Seconds to wait until a connection is opened
|
761
|
+
# [idle_response_timeout]
|
762
|
+
# Seconds to wait until an IDLE response is received
|
763
|
+
#
|
764
|
+
# See DeprecatedClientOptions.new for deprecated arguments.
|
765
|
+
#
|
766
|
+
# ==== Examples
|
767
|
+
#
|
768
|
+
# Connect to cleartext port 143 at mail.example.com and recieve the server greeting:
|
769
|
+
# imap = Net::IMAP.new('mail.example.com', ssl: false) # => #<Net::IMAP:0x00007f79b0872bd0>
|
770
|
+
# imap.port => 143
|
771
|
+
# imap.tls_verified? => false
|
772
|
+
# imap.greeting => name: ("OK" | "PREAUTH") => status
|
773
|
+
# status # => "OK"
|
774
|
+
# # The client is connected in the "Not Authenticated" state.
|
775
|
+
#
|
776
|
+
# Connect with TLS to port 993
|
777
|
+
# imap = Net::IMAP.new('mail.example.com', ssl: true) # => #<Net::IMAP:0x00007f79b0872bd0>
|
778
|
+
# imap.port => 993
|
779
|
+
# imap.tls_verified? => true
|
780
|
+
# imap.greeting => name: (/OK/i | /PREAUTH/i) => status
|
781
|
+
# case status
|
782
|
+
# in /OK/i
|
783
|
+
# # The client is connected in the "Not Authenticated" state.
|
784
|
+
# imap.authenticate("PLAIN", "joe_user", "joes_password")
|
785
|
+
# in /PREAUTH/i
|
786
|
+
# # The client is connected in the "Authenticated" state.
|
787
|
+
# end
|
788
|
+
#
|
789
|
+
# Connect with prior authentication, for example using an SSL certificate:
|
790
|
+
# ssl_ctx_params = {
|
791
|
+
# cert: OpenSSL::X509::Certificate.new(File.read("client.crt")),
|
792
|
+
# key: OpenSSL::PKey::EC.new(File.read('client.key')),
|
793
|
+
# extra_chain_cert: [
|
794
|
+
# OpenSSL::X509::Certificate.new(File.read("intermediate.crt")),
|
795
|
+
# ],
|
796
|
+
# }
|
797
|
+
# imap = Net::IMAP.new('mail.example.com', ssl: ssl_ctx_params)
|
798
|
+
# imap.port => 993
|
799
|
+
# imap.tls_verified? => true
|
800
|
+
# imap.greeting => name: "PREAUTH"
|
801
|
+
# # The client is connected in the "Authenticated" state.
|
802
|
+
#
|
803
|
+
# ==== Exceptions
|
804
|
+
#
|
805
|
+
# The most common errors are:
|
806
|
+
#
|
807
|
+
# [Errno::ECONNREFUSED]
|
808
|
+
# Connection refused by +host+ or an intervening firewall.
|
809
|
+
# [Errno::ETIMEDOUT]
|
810
|
+
# Connection timed out (possibly due to packets being dropped by an
|
811
|
+
# intervening firewall).
|
812
|
+
# [Errno::ENETUNREACH]
|
813
|
+
# There is no route to that network.
|
814
|
+
# [SocketError]
|
815
|
+
# Hostname not known or other socket error.
|
816
|
+
# [Net::IMAP::ByeResponseError]
|
817
|
+
# Connected to the host successfully, but it immediately said goodbye.
|
818
|
+
#
|
819
|
+
def initialize(host, port: nil, ssl: nil,
|
820
|
+
open_timeout: 30, idle_response_timeout: 5)
|
821
|
+
super()
|
822
|
+
# Config options
|
823
|
+
@host = host
|
824
|
+
@port = port || (ssl ? SSL_PORT : PORT)
|
825
|
+
@open_timeout = Integer(open_timeout)
|
826
|
+
@idle_response_timeout = Integer(idle_response_timeout)
|
827
|
+
@ssl_ctx_params, @ssl_ctx = build_ssl_ctx(ssl)
|
828
|
+
|
829
|
+
# Basic Client State
|
830
|
+
@utf8_strings = false
|
831
|
+
@debug_output_bol = true
|
832
|
+
@exception = nil
|
833
|
+
@greeting = nil
|
834
|
+
@capabilities = nil
|
835
|
+
|
836
|
+
# Client Protocol Reciever
|
837
|
+
@parser = ResponseParser.new
|
838
|
+
@responses = Hash.new {|h, k| h[k] = [] }
|
839
|
+
@response_handlers = []
|
840
|
+
@receiver_thread = nil
|
841
|
+
@receiver_thread_exception = nil
|
842
|
+
@receiver_thread_terminating = false
|
843
|
+
|
844
|
+
# Client Protocol Sender (including state for currently running commands)
|
845
|
+
@tag_prefix = "RUBY"
|
846
|
+
@tagno = 0
|
847
|
+
@tagged_responses = {}
|
848
|
+
@tagged_response_arrival = new_cond
|
849
|
+
@continued_command_tag = nil
|
850
|
+
@continuation_request_arrival = new_cond
|
851
|
+
@continuation_request_exception = nil
|
852
|
+
@idle_done_cond = nil
|
853
|
+
@logout_command_tag = nil
|
854
|
+
|
855
|
+
# Connection
|
856
|
+
@tls_verified = false
|
857
|
+
@sock = tcp_socket(@host, @port)
|
858
|
+
start_tls_session if ssl_ctx
|
859
|
+
start_imap_connection
|
860
|
+
|
861
|
+
# DEPRECATED: to remove in next version
|
862
|
+
@client_thread = Thread.current
|
863
|
+
end
|
864
|
+
|
865
|
+
# Returns true after the TLS negotiation has completed and the remote
|
866
|
+
# hostname has been verified. Returns false when TLS has been established
|
867
|
+
# but peer verification was disabled.
|
868
|
+
def tls_verified?; @tls_verified end
|
869
|
+
|
870
|
+
def client_thread # :nodoc:
|
871
|
+
warn "Net::IMAP#client_thread is deprecated and will be removed soon."
|
872
|
+
@client_thread
|
873
|
+
end
|
874
|
+
|
765
875
|
# Disconnects from the server.
|
766
876
|
#
|
767
|
-
# Related: #logout
|
877
|
+
# Related: #logout, #logout!
|
768
878
|
def disconnect
|
769
879
|
return if disconnected?
|
770
880
|
begin
|
@@ -794,62 +904,123 @@ module Net
|
|
794
904
|
return @sock.closed?
|
795
905
|
end
|
796
906
|
|
797
|
-
#
|
798
|
-
#
|
799
|
-
#
|
907
|
+
# Returns whether the server supports a given +capability+. When available,
|
908
|
+
# cached #capabilities are used without sending a new #capability command to
|
909
|
+
# the server.
|
800
910
|
#
|
801
|
-
#
|
802
|
-
#
|
803
|
-
# of all standard capabilities, and their reference RFCs.
|
911
|
+
# <em>*NOTE:* Most Net::IMAP methods do not _currently_ modify their
|
912
|
+
# behaviour according to the server's advertised #capabilities.</em>
|
804
913
|
#
|
805
|
-
#
|
806
|
-
#
|
807
|
-
#
|
808
|
-
|
809
|
-
|
810
|
-
|
914
|
+
# See Net::IMAP@Capabilities for more about \IMAP capabilities.
|
915
|
+
#
|
916
|
+
# Related: #auth_capable?, #capabilities, #capability, #enable
|
917
|
+
def capable?(capability) capabilities.include? capability.to_s.upcase end
|
918
|
+
alias capability? capable?
|
919
|
+
|
920
|
+
# Returns the server capabilities. When available, cached capabilities are
|
921
|
+
# used without sending a new #capability command to the server.
|
811
922
|
#
|
812
|
-
#
|
813
|
-
# documentation for each command method.
|
923
|
+
# To ensure a case-insensitive comparison, #capable? can be used instead.
|
814
924
|
#
|
815
|
-
#
|
925
|
+
# <em>*NOTE:* Most Net::IMAP methods do not _currently_ modify their
|
926
|
+
# behaviour according to the server's advertised #capabilities.</em>
|
927
|
+
#
|
928
|
+
# See Net::IMAP@Capabilities for more about \IMAP capabilities.
|
929
|
+
#
|
930
|
+
# Related: #capable?, #auth_capable?, #auth_mechanisms, #capability, #enable
|
931
|
+
def capabilities
|
932
|
+
@capabilities || capability
|
933
|
+
end
|
934
|
+
|
935
|
+
# Returns the #authenticate mechanisms that the server claims to support.
|
936
|
+
# These are derived from the #capabilities with an <tt>AUTH=</tt> prefix.
|
816
937
|
#
|
817
|
-
#
|
818
|
-
#
|
819
|
-
#
|
820
|
-
# respect their presence or absence. See the capabilites requirements on
|
821
|
-
# #starttls, #login, and #authenticate.
|
938
|
+
# This may be different when the connection is cleartext or using TLS. Most
|
939
|
+
# servers will drop all <tt>AUTH=</tt> mechanisms from #capabilities after
|
940
|
+
# the connection has authenticated.
|
822
941
|
#
|
823
|
-
#
|
942
|
+
# imap = Net::IMAP.new(hostname, ssl: false)
|
943
|
+
# imap.capabilities # => ["IMAP4REV1", "LOGINDISABLED"]
|
944
|
+
# imap.auth_mechanisms # => []
|
824
945
|
#
|
825
|
-
#
|
826
|
-
#
|
827
|
-
#
|
828
|
-
#
|
829
|
-
# be sent at any time.
|
946
|
+
# imap.starttls
|
947
|
+
# imap.capabilities # => ["IMAP4REV1", "AUTH=PLAIN", "AUTH=XOAUTH2",
|
948
|
+
# # "AUTH=OAUTHBEARER"]
|
949
|
+
# imap.auth_mechanisms # => ["PLAIN", "XOAUTH2", "OAUTHBEARER"]
|
830
950
|
#
|
831
|
-
#
|
832
|
-
#
|
833
|
-
#
|
834
|
-
#
|
835
|
-
|
951
|
+
# imap.authenticate("XOAUTH2", username, oauth2_access_token)
|
952
|
+
# imap.auth_mechanisms # => []
|
953
|
+
#
|
954
|
+
# Related: #authenticate, #auth_capable?, #capabilities
|
955
|
+
def auth_mechanisms
|
956
|
+
capabilities
|
957
|
+
.grep(/\AAUTH=/i)
|
958
|
+
.map { _1.delete_prefix("AUTH=") }
|
959
|
+
end
|
960
|
+
|
961
|
+
# Returns whether the server supports a given SASL +mechanism+ for use with
|
962
|
+
# the #authenticate command. The +mechanism+ is supported when
|
963
|
+
# #capabilities includes <tt>"AUTH=#{mechanism.to_s.upcase}"</tt>. When
|
964
|
+
# available, cached capabilities are used without sending a new #capability
|
965
|
+
# command to the server.
|
966
|
+
#
|
967
|
+
# imap.capable? "AUTH=PLAIN" # => true
|
968
|
+
# imap.auth_capable? "PLAIN" # => true
|
969
|
+
# imap.auth_capable? "blurdybloop" # => false
|
970
|
+
#
|
971
|
+
# Related: #authenticate, #auth_mechanisms, #capable?, #capabilities
|
972
|
+
def auth_capable?(mechanism)
|
973
|
+
capable? "AUTH=#{mechanism}"
|
974
|
+
end
|
975
|
+
|
976
|
+
# Returns whether capabilities have been cached. When true, #capable? and
|
977
|
+
# #capabilities don't require sending a #capability command to the server.
|
978
|
+
#
|
979
|
+
# See Net::IMAP@Capabilities for more about \IMAP capabilities.
|
980
|
+
#
|
981
|
+
# Related: #capable?, #capability, #clear_cached_capabilities
|
982
|
+
def capabilities_cached?
|
983
|
+
!!@capabilities
|
984
|
+
end
|
985
|
+
|
986
|
+
# Clears capabilities that have been remembered by the Net::IMAP client.
|
987
|
+
# This forces a #capability command to be sent the next time a #capabilities
|
988
|
+
# query method is called.
|
989
|
+
#
|
990
|
+
# Net::IMAP automatically discards its cached capabilities when they can
|
991
|
+
# change. Explicitly calling this _should_ be unnecessary for well-behaved
|
992
|
+
# servers.
|
993
|
+
#
|
994
|
+
# Related: #capable?, #capability, #capabilities_cached?
|
995
|
+
def clear_cached_capabilities
|
996
|
+
synchronize do
|
997
|
+
clear_responses("CAPABILITY")
|
998
|
+
@capabilities = nil
|
999
|
+
end
|
1000
|
+
end
|
1001
|
+
|
1002
|
+
# Sends a {CAPABILITY command [IMAP4rev1 §6.1.1]}[https://www.rfc-editor.org/rfc/rfc3501#section-6.1.1]
|
1003
|
+
# and returns an array of capabilities that are supported by the server.
|
1004
|
+
# The result is stored for use by #capable? and #capabilities.
|
836
1005
|
#
|
837
|
-
#
|
1006
|
+
# <em>*NOTE:* Most Net::IMAP methods do not _currently_ modify their
|
1007
|
+
# behaviour according to the server's advertised #capabilities.</em>
|
838
1008
|
#
|
839
|
-
#
|
840
|
-
#
|
841
|
-
#
|
842
|
-
# #
|
1009
|
+
# Net::IMAP automatically stores and discards capability data according to
|
1010
|
+
# the requirements and recommendations in
|
1011
|
+
# {IMAP4rev2 §6.1.1}[https://www.rfc-editor.org/rfc/rfc9051#section-6.1.1],
|
1012
|
+
# {§6.2}[https://www.rfc-editor.org/rfc/rfc9051#section-6.2], and
|
1013
|
+
# {§7.1}[https://www.rfc-editor.org/rfc/rfc9051#section-7.1].
|
1014
|
+
# Use #capable?, #auth_capable?, or #capabilities to this cache and avoid
|
1015
|
+
# sending the #capability command unnecessarily.
|
843
1016
|
#
|
844
|
-
#
|
845
|
-
# #authenticate. The OK TaggedResponse to #login and #authenticate may
|
846
|
-
# include +CAPABILITY+ response code data, but the TaggedResponse for
|
847
|
-
# #starttls is sent clear-text and cannot be trusted.
|
1017
|
+
# See Net::IMAP@Capabilities for more about \IMAP capabilities.
|
848
1018
|
#
|
1019
|
+
# Related: #capable?, #auth_capable?, #capability, #enable
|
849
1020
|
def capability
|
850
1021
|
synchronize do
|
851
1022
|
send_command("CAPABILITY")
|
852
|
-
|
1023
|
+
@capabilities = clear_responses("CAPABILITY").last.freeze
|
853
1024
|
end
|
854
1025
|
end
|
855
1026
|
|
@@ -860,8 +1031,7 @@ module Net
|
|
860
1031
|
# Note that the user should first check if the server supports the ID
|
861
1032
|
# capability. For example:
|
862
1033
|
#
|
863
|
-
#
|
864
|
-
# if capabilities.include?("ID")
|
1034
|
+
# if capable?(:ID)
|
865
1035
|
# id = imap.id(
|
866
1036
|
# name: "my IMAP client (ruby)",
|
867
1037
|
# version: MyIMAP::VERSION,
|
@@ -875,11 +1045,11 @@ module Net
|
|
875
1045
|
# ===== Capabilities
|
876
1046
|
#
|
877
1047
|
# The server's capabilities must include +ID+
|
878
|
-
# [RFC2971[https://tools.ietf.org/html/rfc2971]]
|
1048
|
+
# [RFC2971[https://tools.ietf.org/html/rfc2971]].
|
879
1049
|
def id(client_id=nil)
|
880
1050
|
synchronize do
|
881
1051
|
send_command("ID", ClientID.new(client_id))
|
882
|
-
|
1052
|
+
clear_responses("ID").last
|
883
1053
|
end
|
884
1054
|
end
|
885
1055
|
|
@@ -888,7 +1058,7 @@ module Net
|
|
888
1058
|
#
|
889
1059
|
# This allows the server to send unsolicited untagged EXPUNGE #responses,
|
890
1060
|
# but does not execute any client request. \IMAP servers are permitted to
|
891
|
-
# send unsolicited untagged responses at any time, except for
|
1061
|
+
# send unsolicited untagged responses at any time, except for +EXPUNGE+:
|
892
1062
|
#
|
893
1063
|
# * +EXPUNGE+ can only be sent while a command is in progress.
|
894
1064
|
# * +EXPUNGE+ must _not_ be sent during #fetch, #store, or #search.
|
@@ -903,15 +1073,43 @@ module Net
|
|
903
1073
|
# to inform the command to inform the server that the client is done with
|
904
1074
|
# the connection.
|
905
1075
|
#
|
906
|
-
# Related: #disconnect
|
1076
|
+
# Related: #disconnect, #logout!
|
907
1077
|
def logout
|
908
1078
|
send_command("LOGOUT")
|
909
1079
|
end
|
910
1080
|
|
1081
|
+
# Calls #logout then, after receiving the TaggedResponse for the +LOGOUT+,
|
1082
|
+
# calls #disconnect. Returns the TaggedResponse from +LOGOUT+. Returns
|
1083
|
+
# +nil+ when the client is already disconnected, in contrast to #logout
|
1084
|
+
# which raises an exception.
|
1085
|
+
#
|
1086
|
+
# If #logout raises a StandardError, a warning will be printed but the
|
1087
|
+
# exception will not be re-raised.
|
1088
|
+
#
|
1089
|
+
# This is useful in situations where the connection must be dropped, for
|
1090
|
+
# example for security or after tests. If logout errors need to be handled,
|
1091
|
+
# use #logout and #disconnect instead.
|
1092
|
+
#
|
1093
|
+
# Related: #logout, #disconnect
|
1094
|
+
def logout!
|
1095
|
+
logout unless disconnected?
|
1096
|
+
rescue => ex
|
1097
|
+
warn "%s during <Net::IMAP %s:%s> logout!: %s" % [
|
1098
|
+
ex.class, host, port, ex
|
1099
|
+
]
|
1100
|
+
ensure
|
1101
|
+
disconnect
|
1102
|
+
end
|
1103
|
+
|
911
1104
|
# Sends a {STARTTLS command [IMAP4rev1 §6.2.1]}[https://www.rfc-editor.org/rfc/rfc3501#section-6.2.1]
|
912
1105
|
# to start a TLS session.
|
913
1106
|
#
|
914
|
-
# Any +options+ are forwarded to
|
1107
|
+
# Any +options+ are forwarded directly to
|
1108
|
+
# {OpenSSL::SSL::SSLContext#set_params}[https://docs.ruby-lang.org/en/master/OpenSSL/SSL/SSLContext.html#method-i-set_params];
|
1109
|
+
# the keys are names of attribute assignment methods on
|
1110
|
+
# SSLContext[https://docs.ruby-lang.org/en/master/OpenSSL/SSL/SSLContext.html].
|
1111
|
+
#
|
1112
|
+
# See DeprecatedClientOptions#starttls for deprecated arguments.
|
915
1113
|
#
|
916
1114
|
# This method returns after TLS negotiation and hostname verification are
|
917
1115
|
# both successful. Any error indicates that the connection has not been
|
@@ -921,132 +1119,156 @@ module Net
|
|
921
1119
|
# >>>
|
922
1120
|
# Any #response_handlers added before STARTTLS should be aware that the
|
923
1121
|
# TaggedResponse to STARTTLS is sent clear-text, _before_ TLS negotiation.
|
924
|
-
# TLS
|
1122
|
+
# TLS starts immediately _after_ that response. Any response code sent
|
1123
|
+
# with the response (e.g. CAPABILITY) is insecure and cannot be trusted.
|
925
1124
|
#
|
926
1125
|
# Related: Net::IMAP.new, #login, #authenticate
|
927
1126
|
#
|
928
1127
|
# ===== Capability
|
929
|
-
#
|
930
|
-
#
|
1128
|
+
# Clients should not call #starttls unless the server advertises the
|
1129
|
+
# +STARTTLS+ capability.
|
931
1130
|
#
|
932
1131
|
# Server capabilities may change after #starttls, #login, and #authenticate.
|
933
|
-
# Cached capabilities
|
934
|
-
#
|
935
|
-
# The TaggedResponse to #starttls is sent clear-text, so the server <em>must
|
936
|
-
# *not*</em> send capabilities in the #starttls response and clients <em>must
|
937
|
-
# not</em> use them if they are sent. Servers will generally send an
|
938
|
-
# unsolicited untagged response immeditely _after_ #starttls completes.
|
1132
|
+
# Cached #capabilities will be cleared when this method completes.
|
939
1133
|
#
|
940
|
-
def starttls(options
|
1134
|
+
def starttls(**options)
|
1135
|
+
@ssl_ctx_params, @ssl_ctx = build_ssl_ctx(options)
|
941
1136
|
send_command("STARTTLS") do |resp|
|
942
1137
|
if resp.kind_of?(TaggedResponse) && resp.name == "OK"
|
943
|
-
|
944
|
-
|
945
|
-
|
946
|
-
options = create_ssl_params(certs, verify)
|
947
|
-
rescue NoMethodError
|
948
|
-
end
|
949
|
-
start_tls_session(options)
|
1138
|
+
clear_cached_capabilities
|
1139
|
+
clear_responses
|
1140
|
+
start_tls_session
|
950
1141
|
end
|
951
1142
|
end
|
952
1143
|
end
|
953
1144
|
|
954
1145
|
# :call-seq:
|
955
|
-
# authenticate(mechanism,
|
956
|
-
# authenticate(mech, *creds, **props) {|prop, auth| val } -> ok_resp
|
957
|
-
# authenticate(mechanism, authnid, credentials, authzid=nil) -> ok_resp
|
958
|
-
# authenticate(mechanism, **properties) -> ok_resp
|
959
|
-
# authenticate(mechanism) {|propname, authctx| prop_value } -> ok_resp
|
1146
|
+
# authenticate(mechanism, *, sasl_ir: true, registry: Net::IMAP::SASL.authenticators, **, &) -> ok_resp
|
960
1147
|
#
|
961
1148
|
# Sends an {AUTHENTICATE command [IMAP4rev1 §6.2.2]}[https://www.rfc-editor.org/rfc/rfc3501#section-6.2.2]
|
962
1149
|
# to authenticate the client. If successful, the connection enters the
|
963
1150
|
# "_authenticated_" state.
|
964
1151
|
#
|
965
1152
|
# +mechanism+ is the name of the \SASL authentication mechanism to be used.
|
966
|
-
# All other arguments are forwarded to the authenticator for the requested
|
967
|
-
# mechanism. The listed call signatures are suggestions. <em>The
|
968
|
-
# documentation for each individual mechanism must be consulted for its
|
969
|
-
# specific parameters.</em>
|
970
1153
|
#
|
971
|
-
#
|
1154
|
+
# +sasl_ir+ allows or disallows sending an "initial response" (see the
|
1155
|
+
# +SASL-IR+ capability, below).
|
972
1156
|
#
|
973
|
-
#
|
1157
|
+
# All other arguments are forwarded to the registered SASL authenticator for
|
1158
|
+
# the requested mechanism. <em>The documentation for each individual
|
1159
|
+
# mechanism must be consulted for its specific parameters.</em>
|
974
1160
|
#
|
975
|
-
#
|
1161
|
+
# Related: #login, #starttls, #auth_capable?, #auth_mechanisms
|
976
1162
|
#
|
977
|
-
#
|
978
|
-
# Login using clear-text username and password.
|
1163
|
+
# ==== Mechanisms
|
979
1164
|
#
|
980
|
-
#
|
981
|
-
#
|
982
|
-
# Non-standard and obsoleted by +OAUTHBEARER+, but widely
|
983
|
-
# supported.
|
1165
|
+
# Each mechanism has different properties and requirements. Please consult
|
1166
|
+
# the documentation for the specific mechanisms you are using:
|
984
1167
|
#
|
985
|
-
#
|
986
|
-
#
|
987
|
-
#
|
1168
|
+
# +ANONYMOUS+::
|
1169
|
+
# See AnonymousAuthenticator[rdoc-ref:Net::IMAP::SASL::AnonymousAuthenticator].
|
1170
|
+
#
|
1171
|
+
# Allows the user to gain access to public services or resources without
|
1172
|
+
# authenticating or disclosing an identity.
|
1173
|
+
#
|
1174
|
+
# +EXTERNAL+::
|
1175
|
+
# See ExternalAuthenticator[rdoc-ref:Net::IMAP::SASL::ExternalAuthenticator].
|
1176
|
+
#
|
1177
|
+
# Authenticates using already established credentials, such as a TLS
|
1178
|
+
# certificate or IPsec.
|
1179
|
+
#
|
1180
|
+
# +OAUTHBEARER+::
|
1181
|
+
# See OAuthBearerAuthenticator[rdoc-ref:Net::IMAP::SASL::OAuthBearerAuthenticator].
|
1182
|
+
#
|
1183
|
+
# Login using an OAuth2 Bearer token. This is the standard mechanism
|
1184
|
+
# for using OAuth2 with \SASL, but it is not yet deployed as widely as
|
1185
|
+
# +XOAUTH2+.
|
1186
|
+
#
|
1187
|
+
# +PLAIN+::
|
1188
|
+
# See PlainAuthenticator[rdoc-ref:Net::IMAP::SASL::PlainAuthenticator].
|
1189
|
+
#
|
1190
|
+
# Login using clear-text username and password.
|
988
1191
|
#
|
989
|
-
#
|
1192
|
+
# +SCRAM-SHA-1+::
|
1193
|
+
# +SCRAM-SHA-256+::
|
1194
|
+
# See ScramAuthenticator[rdoc-ref:Net::IMAP::SASL::ScramAuthenticator].
|
990
1195
|
#
|
991
|
-
#
|
1196
|
+
# Login by username and password. The password is not sent to the
|
1197
|
+
# server but is used in a salted challenge/response exchange.
|
1198
|
+
# +SCRAM-SHA-1+ and +SCRAM-SHA-256+ are directly supported by
|
1199
|
+
# Net::IMAP::SASL. New authenticators can easily be added for any other
|
1200
|
+
# <tt>SCRAM-*</tt> mechanism if the digest algorithm is supported by
|
1201
|
+
# OpenSSL::Digest.
|
992
1202
|
#
|
993
|
-
#
|
1203
|
+
# +XOAUTH2+::
|
1204
|
+
# See XOAuth2Authenticator[rdoc-ref:Net::IMAP::SASL::XOAuth2Authenticator].
|
994
1205
|
#
|
995
|
-
#
|
1206
|
+
# Login using a username and an OAuth2 access token. Non-standard and
|
1207
|
+
# obsoleted by +OAUTHBEARER+, but widely supported.
|
996
1208
|
#
|
997
|
-
# See
|
998
|
-
# authenticators for other mechanisms. See the {SASL mechanism
|
1209
|
+
# See the {SASL mechanism
|
999
1210
|
# registry}[https://www.iana.org/assignments/sasl-mechanisms/sasl-mechanisms.xhtml]
|
1000
|
-
# for
|
1211
|
+
# for a list of all SASL mechanisms and their specifications. To register
|
1212
|
+
# new authenticators, see Authenticators.
|
1001
1213
|
#
|
1002
|
-
# =====
|
1214
|
+
# ===== Deprecated mechanisms
|
1003
1215
|
#
|
1004
|
-
#
|
1005
|
-
#
|
1216
|
+
# <em>Obsolete mechanisms should be avoided, but are still available for
|
1217
|
+
# backwards compatibility. See</em> Net::IMAP::SASL@Deprecated+mechanisms.
|
1218
|
+
# <em>Using a deprecated mechanism will print a warning.</em>
|
1006
1219
|
#
|
1007
|
-
#
|
1008
|
-
#
|
1009
|
-
#
|
1010
|
-
#
|
1011
|
-
#
|
1012
|
-
#
|
1013
|
-
#
|
1014
|
-
#
|
1015
|
-
#
|
1016
|
-
#
|
1017
|
-
#
|
1018
|
-
# creds = {
|
1019
|
-
# authcid: username,
|
1020
|
-
# password: proc { password ||= ui.prompt_for_password },
|
1021
|
-
# oauth2_token: proc { accesstok ||= kms.fresh_access_token },
|
1022
|
-
# }
|
1023
|
-
# capa = imap.capability
|
1024
|
-
# if capa.include? "AUTH=OAUTHBEARER"
|
1025
|
-
# imap.authenticate "OAUTHBEARER", **creds # authcid, oauth2_token
|
1026
|
-
# elsif capa.include? "AUTH=XOAUTH2"
|
1027
|
-
# imap.authenticate "XOAUTH2", **creds # authcid, oauth2_token
|
1028
|
-
# elsif capa.include? "AUTH=SCRAM-SHA-256"
|
1029
|
-
# imap.authenticate "SCRAM-SHA-256", **creds # authcid, password
|
1030
|
-
# elsif capa.include? "AUTH=PLAIN"
|
1031
|
-
# imap.authenticate "PLAIN", **creds # authcid, password
|
1032
|
-
# elsif capa.include? "AUTH=DIGEST-MD5"
|
1033
|
-
# imap.authenticate "DIGEST-MD5", **creds # authcid, password
|
1034
|
-
# elsif capa.include? "LOGINDISABLED"
|
1035
|
-
# raise "the server has disabled login"
|
1036
|
-
# else
|
1220
|
+
# ==== Capabilities
|
1221
|
+
#
|
1222
|
+
# <tt>"AUTH=#{mechanism}"</tt> capabilities indicate server support for
|
1223
|
+
# mechanisms. Use #auth_capable? or #auth_mechanisms to check for support
|
1224
|
+
# before using a particular mechanism.
|
1225
|
+
#
|
1226
|
+
# if imap.auth_capable? "XOAUTH2"
|
1227
|
+
# imap.authenticate "XOAUTH2", username, oauth2_access_token
|
1228
|
+
# elsif imap.auth_capable? "PLAIN"
|
1229
|
+
# imap.authenticate "PLAIN", username, password
|
1230
|
+
# elsif !imap.capability? "LOGINDISABLED"
|
1037
1231
|
# imap.login username, password
|
1232
|
+
# else
|
1233
|
+
# raise "No acceptable authentication mechanism is available"
|
1038
1234
|
# end
|
1039
1235
|
#
|
1040
|
-
|
1041
|
-
|
1042
|
-
|
1043
|
-
|
1044
|
-
|
1045
|
-
|
1046
|
-
|
1047
|
-
|
1048
|
-
|
1236
|
+
# Although servers should list all supported \SASL mechanisms, they may
|
1237
|
+
# allow authentication with an unlisted +mechanism+.
|
1238
|
+
#
|
1239
|
+
# If [SASL-IR[https://www.rfc-editor.org/rfc/rfc4959.html]] is supported
|
1240
|
+
# and the appropriate <tt>"AUTH=#{mechanism}"</tt> capability is present,
|
1241
|
+
# an "initial response" may be sent as an argument to the +AUTHENTICATE+
|
1242
|
+
# command, saving a round-trip. The SASL exchange allows for server
|
1243
|
+
# challenges and client responses, but many mechanisms expect the client to
|
1244
|
+
# "respond" first. The initial response will only be sent for
|
1245
|
+
# "client-first" mechanisms.
|
1246
|
+
#
|
1247
|
+
# Server capabilities may change after #starttls, #login, and #authenticate.
|
1248
|
+
# Previously cached #capabilities will be cleared when this method
|
1249
|
+
# completes. If the TaggedResponse to #authenticate includes updated
|
1250
|
+
# capabilities, they will be cached.
|
1251
|
+
def authenticate(mechanism, *creds, sasl_ir: true, **props, &callback)
|
1252
|
+
mechanism = mechanism.to_s.tr("_", "-").upcase
|
1253
|
+
authenticator = SASL.authenticator(mechanism, *creds, **props, &callback)
|
1254
|
+
cmdargs = ["AUTHENTICATE", mechanism]
|
1255
|
+
if sasl_ir && capable?("SASL-IR") && auth_capable?(mechanism) &&
|
1256
|
+
authenticator.respond_to?(:initial_response?) &&
|
1257
|
+
authenticator.initial_response?
|
1258
|
+
response = authenticator.process(nil)
|
1259
|
+
cmdargs << (response.empty? ? "=" : [response].pack("m0"))
|
1260
|
+
end
|
1261
|
+
result = send_command_with_continuations(*cmdargs) {|data|
|
1262
|
+
challenge = data.unpack1("m")
|
1263
|
+
response = authenticator.process challenge
|
1264
|
+
[response].pack("m0")
|
1265
|
+
}
|
1266
|
+
if authenticator.respond_to?(:done?) && !authenticator.done?
|
1267
|
+
logout!
|
1268
|
+
raise SASL::AuthenticationIncomplete, result
|
1049
1269
|
end
|
1270
|
+
@capabilities = capabilities_from_resp_code result
|
1271
|
+
result
|
1050
1272
|
end
|
1051
1273
|
|
1052
1274
|
# Sends a {LOGIN command [IMAP4rev1 §6.2.3]}[https://www.rfc-editor.org/rfc/rfc3501#section-6.2.3]
|
@@ -1054,16 +1276,25 @@ module Net
|
|
1054
1276
|
# this +user+. If successful, the connection enters the "_authenticated_"
|
1055
1277
|
# state.
|
1056
1278
|
#
|
1057
|
-
# Using #authenticate
|
1058
|
-
#
|
1279
|
+
# Using #authenticate {should be
|
1280
|
+
# preferred}[https://www.rfc-editor.org/rfc/rfc9051.html#name-login-command]
|
1281
|
+
# over #login. The LOGIN command is not the same as #authenticate with the
|
1282
|
+
# "LOGIN" +mechanism+.
|
1059
1283
|
#
|
1060
1284
|
# A Net::IMAP::NoResponseError is raised if authentication fails.
|
1061
1285
|
#
|
1062
1286
|
# Related: #authenticate, #starttls
|
1063
1287
|
#
|
1064
|
-
#
|
1065
|
-
#
|
1066
|
-
#
|
1288
|
+
# ===== Capabilities
|
1289
|
+
#
|
1290
|
+
# An IMAP client MUST NOT call #login when the server advertises the
|
1291
|
+
# +LOGINDISABLED+ capability.
|
1292
|
+
#
|
1293
|
+
# if imap.capability? "LOGINDISABLED"
|
1294
|
+
# raise "Remote server has disabled the login command"
|
1295
|
+
# else
|
1296
|
+
# imap.login username, password
|
1297
|
+
# end
|
1067
1298
|
#
|
1068
1299
|
# Server capabilities may change after #starttls, #login, and #authenticate.
|
1069
1300
|
# Cached capabilities _must_ be invalidated after this method completes.
|
@@ -1072,17 +1303,18 @@ module Net
|
|
1072
1303
|
#
|
1073
1304
|
def login(user, password)
|
1074
1305
|
send_command("LOGIN", user, password)
|
1306
|
+
.tap { @capabilities = capabilities_from_resp_code _1 }
|
1075
1307
|
end
|
1076
1308
|
|
1077
1309
|
# Sends a {SELECT command [IMAP4rev1 §6.3.1]}[https://www.rfc-editor.org/rfc/rfc3501#section-6.3.1]
|
1078
1310
|
# to select a +mailbox+ so that messages in the +mailbox+ can be accessed.
|
1079
1311
|
#
|
1080
1312
|
# After you have selected a mailbox, you may retrieve the number of items in
|
1081
|
-
# that mailbox from <tt>imap.responses
|
1082
|
-
# recent messages from <tt>imap.responses
|
1083
|
-
# these values can change if new messages arrive during a session
|
1084
|
-
# existing messages are expunged; see #add_response_handler for a
|
1085
|
-
# detect these events.
|
1313
|
+
# that mailbox from <tt>imap.responses("EXISTS", &:last)</tt>, and the
|
1314
|
+
# number of recent messages from <tt>imap.responses("RECENT", &:last)</tt>.
|
1315
|
+
# Note that these values can change if new messages arrive during a session
|
1316
|
+
# or when existing messages are expunged; see #add_response_handler for a
|
1317
|
+
# way to detect these events.
|
1086
1318
|
#
|
1087
1319
|
# A Net::IMAP::NoResponseError is raised if the mailbox does not
|
1088
1320
|
# exist or is for some reason non-selectable.
|
@@ -1095,7 +1327,7 @@ module Net
|
|
1095
1327
|
# the server may return an untagged "NO" response with a "UIDNOTSTICKY"
|
1096
1328
|
# response code indicating that the mailstore does not support persistent
|
1097
1329
|
# UIDs:
|
1098
|
-
#
|
1330
|
+
# imap.responses("NO", &:last)&.code&.name == "UIDNOTSTICKY"
|
1099
1331
|
def select(mailbox)
|
1100
1332
|
synchronize do
|
1101
1333
|
@responses.clear
|
@@ -1185,10 +1417,10 @@ module Net
|
|
1185
1417
|
# to the client. +refname+ provides a context (for instance, a base
|
1186
1418
|
# directory in a directory-based mailbox hierarchy). +mailbox+ specifies a
|
1187
1419
|
# mailbox or (via wildcards) mailboxes under that context. Two wildcards
|
1188
|
-
# may be used in +mailbox+:
|
1189
|
-
# the hierarchy delimiter (for instance,
|
1190
|
-
# directory-based mailbox hierarchy); and
|
1191
|
-
# *except* the hierarchy delimiter.
|
1420
|
+
# may be used in +mailbox+: <tt>"*"</tt>, which matches all characters
|
1421
|
+
# *including* the hierarchy delimiter (for instance, "/" on a UNIX-hosted
|
1422
|
+
# directory-based mailbox hierarchy); and <tt>"%"</tt>, which matches all
|
1423
|
+
# characters *except* the hierarchy delimiter.
|
1192
1424
|
#
|
1193
1425
|
# If +refname+ is empty, +mailbox+ is used directly to determine
|
1194
1426
|
# which mailboxes to match. If +mailbox+ is empty, the root
|
@@ -1213,7 +1445,7 @@ module Net
|
|
1213
1445
|
def list(refname, mailbox)
|
1214
1446
|
synchronize do
|
1215
1447
|
send_command("LIST", refname, mailbox)
|
1216
|
-
|
1448
|
+
clear_responses("LIST")
|
1217
1449
|
end
|
1218
1450
|
end
|
1219
1451
|
|
@@ -1236,23 +1468,22 @@ module Net
|
|
1236
1468
|
# servers, then folder creation (and listing, moving, etc) can lead to
|
1237
1469
|
# errors.
|
1238
1470
|
#
|
1239
|
-
# From RFC2342:
|
1240
|
-
#
|
1241
|
-
# Although typically a server will support only a single Personal
|
1471
|
+
# From RFC2342[https://tools.ietf.org/html/rfc2342]:
|
1472
|
+
# >>>
|
1473
|
+
# <em>Although typically a server will support only a single Personal
|
1242
1474
|
# Namespace, and a single Other User's Namespace, circumstances exist
|
1243
1475
|
# where there MAY be multiples of these, and a client MUST be prepared
|
1244
1476
|
# for them. If a client is configured such that it is required to create
|
1245
1477
|
# a certain mailbox, there can be circumstances where it is unclear which
|
1246
1478
|
# Personal Namespaces it should create the mailbox in. In these
|
1247
1479
|
# situations a client SHOULD let the user select which namespaces to
|
1248
|
-
# create the mailbox in
|
1480
|
+
# create the mailbox in.</em>
|
1249
1481
|
#
|
1250
1482
|
# Related: #list, Namespaces, Namespace
|
1251
1483
|
#
|
1252
1484
|
# ===== For example:
|
1253
1485
|
#
|
1254
|
-
#
|
1255
|
-
# if capabilities.include?("NAMESPACE")
|
1486
|
+
# if capable?("NAMESPACE")
|
1256
1487
|
# namespaces = imap.namespace
|
1257
1488
|
# if namespace = namespaces.personal.first
|
1258
1489
|
# prefix = namespace.prefix # e.g. "" or "INBOX."
|
@@ -1271,7 +1502,7 @@ module Net
|
|
1271
1502
|
def namespace
|
1272
1503
|
synchronize do
|
1273
1504
|
send_command("NAMESPACE")
|
1274
|
-
|
1505
|
+
clear_responses("NAMESPACE").last
|
1275
1506
|
end
|
1276
1507
|
end
|
1277
1508
|
|
@@ -1315,7 +1546,7 @@ module Net
|
|
1315
1546
|
def xlist(refname, mailbox)
|
1316
1547
|
synchronize do
|
1317
1548
|
send_command("XLIST", refname, mailbox)
|
1318
|
-
|
1549
|
+
clear_responses("XLIST")
|
1319
1550
|
end
|
1320
1551
|
end
|
1321
1552
|
|
@@ -1334,8 +1565,8 @@ module Net
|
|
1334
1565
|
synchronize do
|
1335
1566
|
send_command("GETQUOTAROOT", mailbox)
|
1336
1567
|
result = []
|
1337
|
-
result.concat(
|
1338
|
-
result.concat(
|
1568
|
+
result.concat(clear_responses("QUOTAROOT"))
|
1569
|
+
result.concat(clear_responses("QUOTA"))
|
1339
1570
|
return result
|
1340
1571
|
end
|
1341
1572
|
end
|
@@ -1354,7 +1585,7 @@ module Net
|
|
1354
1585
|
def getquota(mailbox)
|
1355
1586
|
synchronize do
|
1356
1587
|
send_command("GETQUOTA", mailbox)
|
1357
|
-
|
1588
|
+
clear_responses("QUOTA")
|
1358
1589
|
end
|
1359
1590
|
end
|
1360
1591
|
|
@@ -1410,7 +1641,7 @@ module Net
|
|
1410
1641
|
def getacl(mailbox)
|
1411
1642
|
synchronize do
|
1412
1643
|
send_command("GETACL", mailbox)
|
1413
|
-
|
1644
|
+
clear_responses("ACL").last
|
1414
1645
|
end
|
1415
1646
|
end
|
1416
1647
|
|
@@ -1425,7 +1656,7 @@ module Net
|
|
1425
1656
|
def lsub(refname, mailbox)
|
1426
1657
|
synchronize do
|
1427
1658
|
send_command("LSUB", refname, mailbox)
|
1428
|
-
|
1659
|
+
clear_responses("LSUB")
|
1429
1660
|
end
|
1430
1661
|
end
|
1431
1662
|
|
@@ -1434,9 +1665,9 @@ module Net
|
|
1434
1665
|
# or more attributes whose statuses are to be requested. Supported
|
1435
1666
|
# attributes include:
|
1436
1667
|
#
|
1437
|
-
#
|
1438
|
-
#
|
1439
|
-
#
|
1668
|
+
# MESSAGES:: the number of messages in the mailbox.
|
1669
|
+
# RECENT:: the number of recent messages in the mailbox.
|
1670
|
+
# UNSEEN:: the number of unseen messages in the mailbox.
|
1440
1671
|
#
|
1441
1672
|
# The return value is a hash of attributes. For example:
|
1442
1673
|
#
|
@@ -1449,7 +1680,7 @@ module Net
|
|
1449
1680
|
def status(mailbox, attr)
|
1450
1681
|
synchronize do
|
1451
1682
|
send_command("STATUS", mailbox, attr)
|
1452
|
-
|
1683
|
+
clear_responses("STATUS").last&.attr
|
1453
1684
|
end
|
1454
1685
|
end
|
1455
1686
|
|
@@ -1538,7 +1769,7 @@ module Net
|
|
1538
1769
|
def expunge
|
1539
1770
|
synchronize do
|
1540
1771
|
send_command("EXPUNGE")
|
1541
|
-
|
1772
|
+
clear_responses("EXPUNGE")
|
1542
1773
|
end
|
1543
1774
|
end
|
1544
1775
|
|
@@ -1570,7 +1801,7 @@ module Net
|
|
1570
1801
|
def uid_expunge(uid_set)
|
1571
1802
|
synchronize do
|
1572
1803
|
send_command("UID EXPUNGE", MessageSet.new(uid_set))
|
1573
|
-
|
1804
|
+
clear_responses("EXPUNGE")
|
1574
1805
|
end
|
1575
1806
|
end
|
1576
1807
|
|
@@ -1589,7 +1820,7 @@ module Net
|
|
1589
1820
|
# or [{IMAP4rev2 §6.4.4}[https://www.rfc-editor.org/rfc/rfc9051.html#section-6.4.4]],
|
1590
1821
|
# in addition to documentation for
|
1591
1822
|
# any [CAPABILITIES[https://www.iana.org/assignments/imap-capabilities/imap-capabilities.xhtml]]
|
1592
|
-
# reported by #
|
1823
|
+
# reported by #capabilities which may define additional search filters, e.g:
|
1593
1824
|
# +CONDSTORE+, +WITHIN+, +FILTERS+, <tt>SEARCH=FUZZY</tt>, +OBJECTID+, or
|
1594
1825
|
# +SAVEDATE+. The following are some common search criteria:
|
1595
1826
|
#
|
@@ -1656,8 +1887,7 @@ module Net
|
|
1656
1887
|
# +attr+ is a list of attributes to fetch; see the documentation
|
1657
1888
|
# for FetchData for a list of valid attributes.
|
1658
1889
|
#
|
1659
|
-
# The return value is an array of FetchData
|
1660
|
-
# (instead of an empty array) if there is no matching message.
|
1890
|
+
# The return value is an array of FetchData.
|
1661
1891
|
#
|
1662
1892
|
# Related: #uid_search, FetchData
|
1663
1893
|
#
|
@@ -1702,11 +1932,11 @@ module Net
|
|
1702
1932
|
# to alter data associated with messages in the mailbox, in particular their
|
1703
1933
|
# flags. The +set+ parameter is a number, an array of numbers, or a Range
|
1704
1934
|
# object. Each number is a message sequence number. +attr+ is the name of a
|
1705
|
-
# data item to store:
|
1706
|
-
# provided one,
|
1707
|
-
# remove them. +flags+ is a list of flags.
|
1935
|
+
# data item to store: <tt>"FLAGS"</tt> will replace the message's flag list
|
1936
|
+
# with the provided one, <tt>"+FLAGS"</tt> will add the provided flags, and
|
1937
|
+
# <tt>"-FLAGS"</tt> will remove them. +flags+ is a list of flags.
|
1708
1938
|
#
|
1709
|
-
# The return value is an array of FetchData
|
1939
|
+
# The return value is an array of FetchData.
|
1710
1940
|
#
|
1711
1941
|
# Related: #uid_store
|
1712
1942
|
#
|
@@ -1884,6 +2114,87 @@ module Net
|
|
1884
2114
|
return thread_internal("UID THREAD", algorithm, search_keys, charset)
|
1885
2115
|
end
|
1886
2116
|
|
2117
|
+
# Sends an {ENABLE command [RFC5161 §3.2]}[https://www.rfc-editor.org/rfc/rfc5161#section-3.1]
|
2118
|
+
# {[IMAP4rev2 §6.3.1]}[https://www.rfc-editor.org/rfc/rfc9051#section-6.3.1]
|
2119
|
+
# to enable the specified server +capabilities+. Each capability may be an
|
2120
|
+
# array, string, or symbol. Returns a list of the capabilities that were
|
2121
|
+
# enabled.
|
2122
|
+
#
|
2123
|
+
# The +ENABLE+ command is only valid in the _authenticated_ state, before
|
2124
|
+
# any mailbox is selected.
|
2125
|
+
#
|
2126
|
+
# Related: #capable?, #capabilities, #capability
|
2127
|
+
#
|
2128
|
+
# ===== Capabilities
|
2129
|
+
#
|
2130
|
+
# The server's capabilities must include
|
2131
|
+
# +ENABLE+ [RFC5161[https://tools.ietf.org/html/rfc5161]]
|
2132
|
+
# or +IMAP4REV2+ [RFC9051[https://tools.ietf.org/html/rfc9051]].
|
2133
|
+
#
|
2134
|
+
# Additionally, the server capabilities must include a capability matching
|
2135
|
+
# each enabled extension (usually the same name as the enabled extension).
|
2136
|
+
# The following capabilities may be enabled:
|
2137
|
+
#
|
2138
|
+
# [+:utf8+ --- an alias for <tt>"UTF8=ACCEPT"</tt>]
|
2139
|
+
#
|
2140
|
+
# In a future release, <tt>enable(:utf8)</tt> will enable either
|
2141
|
+
# <tt>"UTF8=ACCEPT"</tt> or <tt>"IMAP4rev2"</tt>, depending on server
|
2142
|
+
# capabilities.
|
2143
|
+
#
|
2144
|
+
# [<tt>"UTF8=ACCEPT"</tt> [RFC6855[https://tools.ietf.org/html/rfc6855]]]
|
2145
|
+
#
|
2146
|
+
# The server's capabilities must include <tt>UTF8=ACCEPT</tt> _or_
|
2147
|
+
# <tt>UTF8=ONLY</tt>.
|
2148
|
+
#
|
2149
|
+
# This allows the server to send strings encoded as UTF-8 which might
|
2150
|
+
# otherwise need to use a 7-bit encoding, such as {modified
|
2151
|
+
# UTF-7}[::decode_utf7] for mailbox names, or RFC2047 encoded-words for
|
2152
|
+
# message headers.
|
2153
|
+
#
|
2154
|
+
# *Note:* <em>A future update may set string encodings slightly
|
2155
|
+
# differently</em>, e.g: "US-ASCII" when UTF-8 is not enabled, and "UTF-8"
|
2156
|
+
# when it is. Currently, the encoding of strings sent as "quoted" or
|
2157
|
+
# "text" will _always_ be "UTF-8", even when only ASCII characters are
|
2158
|
+
# used (e.g. "Subject: Agenda") And currently, string "literals" sent
|
2159
|
+
# by the server will always have an "ASCII-8BIT" (binary)
|
2160
|
+
# encoding, even if they generally contain UTF-8 data, if they are
|
2161
|
+
# text at all.
|
2162
|
+
#
|
2163
|
+
# [<tt>"UTF8=ONLY"</tt> [RFC6855[https://tools.ietf.org/html/rfc6855]]]
|
2164
|
+
#
|
2165
|
+
# A server that reports the <tt>UTF8=ONLY</tt> capability _requires_ that
|
2166
|
+
# the client <tt>enable("UTF8=ACCEPT")</tt> before any mailboxes may be
|
2167
|
+
# selected. For convenience, <tt>enable("UTF8=ONLY")</tt> is aliased to
|
2168
|
+
# <tt>enable("UTF8=ACCEPT")</tt>.
|
2169
|
+
#
|
2170
|
+
# ===== Unsupported capabilities
|
2171
|
+
#
|
2172
|
+
# *Note:* Some extensions that use ENABLE permit the server to send syntax
|
2173
|
+
# that Net::IMAP cannot parse, which may raise an exception and disconnect.
|
2174
|
+
# Some extensions may work, but the support may be incomplete, untested, or
|
2175
|
+
# experimental.
|
2176
|
+
#
|
2177
|
+
# Until a capability is documented here as supported, enabling it may result
|
2178
|
+
# in undocumented behavior and a future release may update with incompatible
|
2179
|
+
# behavior <em>without warning or deprecation</em>.
|
2180
|
+
#
|
2181
|
+
# <em>Caution is advised.</em>
|
2182
|
+
#
|
2183
|
+
def enable(*capabilities)
|
2184
|
+
capabilities = capabilities
|
2185
|
+
.flatten
|
2186
|
+
.map {|e| ENABLE_ALIASES[e] || e }
|
2187
|
+
.uniq
|
2188
|
+
.join(' ')
|
2189
|
+
synchronize do
|
2190
|
+
send_command("ENABLE #{capabilities}")
|
2191
|
+
result = clear_responses("ENABLED").last || []
|
2192
|
+
@utf8_strings ||= result.include? "UTF8=ACCEPT"
|
2193
|
+
@utf8_strings ||= result.include? "IMAP4REV2"
|
2194
|
+
result
|
2195
|
+
end
|
2196
|
+
end
|
2197
|
+
|
1887
2198
|
# Sends an {IDLE command [RFC2177 §3]}[https://www.rfc-editor.org/rfc/rfc6851#section-3]
|
1888
2199
|
# {[IMAP4rev2 §6.3.13]}[https://www.rfc-editor.org/rfc/rfc9051#section-6.3.13]
|
1889
2200
|
# that waits for notifications of new or expunged messages. Yields
|
@@ -1948,6 +2259,104 @@ module Net
|
|
1948
2259
|
end
|
1949
2260
|
end
|
1950
2261
|
|
2262
|
+
# :call-seq:
|
2263
|
+
# responses {|hash| ...} -> block result
|
2264
|
+
# responses(type) {|array| ...} -> block result
|
2265
|
+
#
|
2266
|
+
# Yields unhandled responses and returns the result of the block.
|
2267
|
+
#
|
2268
|
+
# Unhandled responses are stored in a hash, with arrays of
|
2269
|
+
# <em>non-+nil+</em> UntaggedResponse#data keyed by UntaggedResponse#name
|
2270
|
+
# and ResponseCode#data keyed by ResponseCode#name. Call without +type+ to
|
2271
|
+
# yield the entire responses hash. Call with +type+ to yield only the array
|
2272
|
+
# of responses for that type.
|
2273
|
+
#
|
2274
|
+
# For example:
|
2275
|
+
#
|
2276
|
+
# imap.select("inbox")
|
2277
|
+
# p imap.responses("EXISTS", &:last)
|
2278
|
+
# #=> 2
|
2279
|
+
# p imap.responses("UIDVALIDITY", &:last)
|
2280
|
+
# #=> 968263756
|
2281
|
+
#
|
2282
|
+
# >>>
|
2283
|
+
# *Note:* Access to the responses hash is synchronized for thread-safety.
|
2284
|
+
# The receiver thread and response_handlers cannot process new responses
|
2285
|
+
# until the block completes. Accessing either the response hash or its
|
2286
|
+
# response type arrays outside of the block is unsafe.
|
2287
|
+
#
|
2288
|
+
# Calling without a block is unsafe and deprecated. Future releases will
|
2289
|
+
# raise ArgumentError unless a block is given.
|
2290
|
+
#
|
2291
|
+
# Previously unhandled responses are automatically cleared before entering a
|
2292
|
+
# mailbox with #select or #examine. Long-lived connections can receive many
|
2293
|
+
# unhandled server responses, which must be pruned or they will continually
|
2294
|
+
# consume more memory. Update or clear the responses hash or arrays inside
|
2295
|
+
# the block, or use #clear_responses.
|
2296
|
+
#
|
2297
|
+
# Only non-+nil+ data is stored. Many important response codes have no data
|
2298
|
+
# of their own, but are used as "tags" on the ResponseText object they are
|
2299
|
+
# attached to. ResponseText will be accessible by its response types:
|
2300
|
+
# "+OK+", "+NO+", "+BAD+", "+BYE+", or "+PREAUTH+".
|
2301
|
+
#
|
2302
|
+
# TaggedResponse#data is not saved to #responses, nor is any
|
2303
|
+
# ResponseCode#data on tagged responses. Although some command methods do
|
2304
|
+
# return the TaggedResponse directly, #add_response_handler must be used to
|
2305
|
+
# handle all response codes.
|
2306
|
+
#
|
2307
|
+
# Related: #clear_responses, #response_handlers, #greeting
|
2308
|
+
def responses(type = nil)
|
2309
|
+
if block_given?
|
2310
|
+
synchronize { yield(type ? @responses[type.to_s.upcase] : @responses) }
|
2311
|
+
elsif type
|
2312
|
+
raise ArgumentError, "Pass a block or use #clear_responses"
|
2313
|
+
else
|
2314
|
+
# warn("DEPRECATED: pass a block or use #clear_responses", uplevel: 1)
|
2315
|
+
@responses
|
2316
|
+
end
|
2317
|
+
end
|
2318
|
+
|
2319
|
+
# :call-seq:
|
2320
|
+
# clear_responses -> hash
|
2321
|
+
# clear_responses(type) -> array
|
2322
|
+
#
|
2323
|
+
# Clears and returns the unhandled #responses hash or the unhandled
|
2324
|
+
# responses array for a single response +type+.
|
2325
|
+
#
|
2326
|
+
# Clearing responses is synchronized with other threads. The lock is
|
2327
|
+
# released before returning.
|
2328
|
+
#
|
2329
|
+
# Related: #responses, #response_handlers
|
2330
|
+
def clear_responses(type = nil)
|
2331
|
+
synchronize {
|
2332
|
+
if type
|
2333
|
+
@responses.delete(type) || []
|
2334
|
+
else
|
2335
|
+
@responses.dup.transform_values(&:freeze)
|
2336
|
+
.tap { _1.default = [].freeze }
|
2337
|
+
.tap { @responses.clear }
|
2338
|
+
end
|
2339
|
+
}
|
2340
|
+
.freeze
|
2341
|
+
end
|
2342
|
+
|
2343
|
+
# Returns all response handlers, including those that are added internally
|
2344
|
+
# by commands. Each response handler will be called with every new
|
2345
|
+
# UntaggedResponse, TaggedResponse, and ContinuationRequest.
|
2346
|
+
#
|
2347
|
+
# Response handlers are called with a mutex inside the receiver thread. New
|
2348
|
+
# responses cannot be processed and commands from other threads must wait
|
2349
|
+
# until all response_handlers return. An exception will shut-down the
|
2350
|
+
# receiver thread and close the connection.
|
2351
|
+
#
|
2352
|
+
# For thread-safety, the returned array is a frozen copy of the internal
|
2353
|
+
# array.
|
2354
|
+
#
|
2355
|
+
# Related: #add_response_handler, #remove_response_handler
|
2356
|
+
def response_handlers
|
2357
|
+
synchronize { @response_handlers.clone.freeze }
|
2358
|
+
end
|
2359
|
+
|
1951
2360
|
# Adds a response handler. For example, to detect when
|
1952
2361
|
# the server sends a new EXISTS response (which normally
|
1953
2362
|
# indicates new messages being added to the mailbox),
|
@@ -1960,14 +2369,21 @@ module Net
|
|
1960
2369
|
# end
|
1961
2370
|
# }
|
1962
2371
|
#
|
2372
|
+
# Related: #remove_response_handler, #response_handlers
|
1963
2373
|
def add_response_handler(handler = nil, &block)
|
1964
2374
|
raise ArgumentError, "two Procs are passed" if handler && block
|
1965
|
-
|
2375
|
+
synchronize do
|
2376
|
+
@response_handlers.push(block || handler)
|
2377
|
+
end
|
1966
2378
|
end
|
1967
2379
|
|
1968
2380
|
# Removes the response handler.
|
2381
|
+
#
|
2382
|
+
# Related: #add_response_handler, #response_handlers
|
1969
2383
|
def remove_response_handler(handler)
|
1970
|
-
|
2384
|
+
synchronize do
|
2385
|
+
@response_handlers.delete(handler)
|
2386
|
+
end
|
1971
2387
|
end
|
1972
2388
|
|
1973
2389
|
private
|
@@ -1978,93 +2394,29 @@ module Net
|
|
1978
2394
|
|
1979
2395
|
@@debug = false
|
1980
2396
|
|
1981
|
-
|
1982
|
-
|
1983
|
-
|
1984
|
-
|
1985
|
-
|
1986
|
-
|
1987
|
-
|
1988
|
-
|
1989
|
-
|
1990
|
-
|
1991
|
-
|
1992
|
-
|
1993
|
-
|
1994
|
-
|
1995
|
-
|
1996
|
-
|
1997
|
-
|
1998
|
-
|
1999
|
-
|
2000
|
-
|
2001
|
-
|
2002
|
-
|
2003
|
-
|
2004
|
-
# being dropped by an intervening firewall).
|
2005
|
-
# Errno::ENETUNREACH:: There is no route to that network.
|
2006
|
-
# SocketError:: Hostname not known or other socket error.
|
2007
|
-
# Net::IMAP::ByeResponseError:: The connected to the host was successful, but
|
2008
|
-
# it immediately said goodbye.
|
2009
|
-
def initialize(host, port_or_options = {},
|
2010
|
-
usessl = false, certs = nil, verify = true)
|
2011
|
-
super()
|
2012
|
-
@host = host
|
2013
|
-
begin
|
2014
|
-
options = port_or_options.to_hash
|
2015
|
-
rescue NoMethodError
|
2016
|
-
# for backward compatibility
|
2017
|
-
options = {}
|
2018
|
-
options[:port] = port_or_options
|
2019
|
-
if usessl
|
2020
|
-
options[:ssl] = create_ssl_params(certs, verify)
|
2021
|
-
end
|
2022
|
-
end
|
2023
|
-
@port = options[:port] || (options[:ssl] ? SSL_PORT : PORT)
|
2024
|
-
@tag_prefix = "RUBY"
|
2025
|
-
@tagno = 0
|
2026
|
-
@open_timeout = options[:open_timeout] || 30
|
2027
|
-
@idle_response_timeout = options[:idle_response_timeout] || 5
|
2028
|
-
@parser = ResponseParser.new
|
2029
|
-
@sock = tcp_socket(@host, @port)
|
2030
|
-
begin
|
2031
|
-
if options[:ssl]
|
2032
|
-
start_tls_session(options[:ssl])
|
2033
|
-
@usessl = true
|
2034
|
-
else
|
2035
|
-
@usessl = false
|
2036
|
-
end
|
2037
|
-
@responses = Hash.new([].freeze)
|
2038
|
-
@tagged_responses = {}
|
2039
|
-
@response_handlers = []
|
2040
|
-
@tagged_response_arrival = new_cond
|
2041
|
-
@continued_command_tag = nil
|
2042
|
-
@continuation_request_arrival = new_cond
|
2043
|
-
@continuation_request_exception = nil
|
2044
|
-
@idle_done_cond = nil
|
2045
|
-
@logout_command_tag = nil
|
2046
|
-
@debug_output_bol = true
|
2047
|
-
@exception = nil
|
2048
|
-
|
2049
|
-
@greeting = get_response
|
2050
|
-
if @greeting.nil?
|
2051
|
-
raise Error, "connection closed"
|
2052
|
-
end
|
2053
|
-
if @greeting.name == "BYE"
|
2054
|
-
raise ByeResponseError, @greeting
|
2055
|
-
end
|
2056
|
-
|
2057
|
-
@client_thread = Thread.current
|
2058
|
-
@receiver_thread = Thread.start {
|
2059
|
-
begin
|
2060
|
-
receive_responses
|
2061
|
-
rescue Exception
|
2062
|
-
end
|
2063
|
-
}
|
2064
|
-
@receiver_thread_terminating = false
|
2065
|
-
rescue Exception
|
2066
|
-
@sock.close
|
2067
|
-
raise
|
2397
|
+
def start_imap_connection
|
2398
|
+
@greeting = get_server_greeting
|
2399
|
+
@capabilities = capabilities_from_resp_code @greeting
|
2400
|
+
@receiver_thread = start_receiver_thread
|
2401
|
+
rescue Exception
|
2402
|
+
@sock.close
|
2403
|
+
raise
|
2404
|
+
end
|
2405
|
+
|
2406
|
+
def get_server_greeting
|
2407
|
+
greeting = get_response
|
2408
|
+
raise Error, "No server greeting - connection closed" unless greeting
|
2409
|
+
record_untagged_response_code greeting
|
2410
|
+
raise ByeResponseError, greeting if greeting.name == "BYE"
|
2411
|
+
greeting
|
2412
|
+
end
|
2413
|
+
|
2414
|
+
def start_receiver_thread
|
2415
|
+
Thread.start do
|
2416
|
+
receive_responses
|
2417
|
+
rescue Exception => ex
|
2418
|
+
@receiver_thread_exception = ex
|
2419
|
+
# don't exit the thread with an exception
|
2068
2420
|
end
|
2069
2421
|
end
|
2070
2422
|
|
@@ -2113,11 +2465,7 @@ module Net
|
|
2113
2465
|
@continuation_request_arrival.signal
|
2114
2466
|
end
|
2115
2467
|
when UntaggedResponse
|
2116
|
-
|
2117
|
-
if resp.data.instance_of?(ResponseText) &&
|
2118
|
-
(code = resp.data.code)
|
2119
|
-
record_response(code.name, code.data)
|
2120
|
-
end
|
2468
|
+
record_untagged_response(resp)
|
2121
2469
|
if resp.name == "BYE" && @logout_command_tag.nil?
|
2122
2470
|
@sock.close
|
2123
2471
|
@exception = ByeResponseError.new(resp)
|
@@ -2171,7 +2519,8 @@ module Net
|
|
2171
2519
|
when /\A(?:BAD)\z/ni
|
2172
2520
|
raise BadResponseError, resp
|
2173
2521
|
else
|
2174
|
-
|
2522
|
+
disconnect
|
2523
|
+
raise InvalidResponseError, "invalid tagged resp: %p" % [resp.raw.chomp]
|
2175
2524
|
end
|
2176
2525
|
end
|
2177
2526
|
|
@@ -2195,11 +2544,42 @@ module Net
|
|
2195
2544
|
return @parser.parse(buff)
|
2196
2545
|
end
|
2197
2546
|
|
2198
|
-
|
2199
|
-
|
2200
|
-
|
2547
|
+
#############################
|
2548
|
+
# built-in response handlers
|
2549
|
+
|
2550
|
+
# store name => [..., data]
|
2551
|
+
def record_untagged_response(resp)
|
2552
|
+
@responses[resp.name] << resp.data
|
2553
|
+
record_untagged_response_code resp
|
2554
|
+
end
|
2555
|
+
|
2556
|
+
# store code.name => [..., code.data]
|
2557
|
+
def record_untagged_response_code(resp)
|
2558
|
+
return unless resp.data.is_a?(ResponseText)
|
2559
|
+
return unless (code = resp.data.code)
|
2560
|
+
@responses[code.name] << code.data
|
2561
|
+
end
|
2562
|
+
|
2563
|
+
# NOTE: only call this for greeting, login, and authenticate
|
2564
|
+
def capabilities_from_resp_code(resp)
|
2565
|
+
return unless %w[PREAUTH OK].any? { _1.casecmp? resp.name }
|
2566
|
+
return unless (code = resp.data.code)
|
2567
|
+
return unless code.name.casecmp?("CAPABILITY")
|
2568
|
+
code.data.freeze
|
2569
|
+
end
|
2570
|
+
|
2571
|
+
#############################
|
2572
|
+
|
2573
|
+
# Calls send_command, yielding the text of each ContinuationRequest and
|
2574
|
+
# responding with each block result. Returns TaggedResponse. Raises
|
2575
|
+
# NoResponseError or BadResponseError.
|
2576
|
+
def send_command_with_continuations(cmd, *args)
|
2577
|
+
send_command(cmd, *args) do |server_response|
|
2578
|
+
if server_response.instance_of?(ContinuationRequest)
|
2579
|
+
client_response = yield server_response.data.text
|
2580
|
+
put_string(client_response + CRLF)
|
2581
|
+
end
|
2201
2582
|
end
|
2202
|
-
@responses[name].push(data)
|
2203
2583
|
end
|
2204
2584
|
|
2205
2585
|
def send_command(cmd, *args, &block)
|
@@ -2241,8 +2621,8 @@ module Net
|
|
2241
2621
|
if @debug_output_bol
|
2242
2622
|
$stderr.print("C: ")
|
2243
2623
|
end
|
2244
|
-
$stderr.print(str.gsub(/\n
|
2245
|
-
if /\
|
2624
|
+
$stderr.print(str.gsub(/\n/n) { $'.empty? ? $& : "\nC: " })
|
2625
|
+
if /\n\z/n.match(str)
|
2246
2626
|
@debug_output_bol = true
|
2247
2627
|
else
|
2248
2628
|
@debug_output_bol = false
|
@@ -2262,7 +2642,7 @@ module Net
|
|
2262
2642
|
else
|
2263
2643
|
send_command(cmd, *keys)
|
2264
2644
|
end
|
2265
|
-
|
2645
|
+
clear_responses("SEARCH").last || []
|
2266
2646
|
end
|
2267
2647
|
end
|
2268
2648
|
|
@@ -2277,13 +2657,13 @@ module Net
|
|
2277
2657
|
end
|
2278
2658
|
|
2279
2659
|
synchronize do
|
2280
|
-
|
2660
|
+
clear_responses("FETCH")
|
2281
2661
|
if mod
|
2282
2662
|
send_command(cmd, MessageSet.new(set), attr, mod)
|
2283
2663
|
else
|
2284
2664
|
send_command(cmd, MessageSet.new(set), attr)
|
2285
2665
|
end
|
2286
|
-
|
2666
|
+
clear_responses("FETCH")
|
2287
2667
|
end
|
2288
2668
|
end
|
2289
2669
|
|
@@ -2292,9 +2672,9 @@ module Net
|
|
2292
2672
|
attr = RawData.new(attr)
|
2293
2673
|
end
|
2294
2674
|
synchronize do
|
2295
|
-
|
2675
|
+
clear_responses("FETCH")
|
2296
2676
|
send_command(cmd, MessageSet.new(set), attr, flags)
|
2297
|
-
|
2677
|
+
clear_responses("FETCH")
|
2298
2678
|
end
|
2299
2679
|
end
|
2300
2680
|
|
@@ -2311,7 +2691,7 @@ module Net
|
|
2311
2691
|
normalize_searching_criteria(search_keys)
|
2312
2692
|
synchronize do
|
2313
2693
|
send_command(cmd, sort_keys, charset, *search_keys)
|
2314
|
-
|
2694
|
+
clear_responses("SORT").last || []
|
2315
2695
|
end
|
2316
2696
|
end
|
2317
2697
|
|
@@ -2322,8 +2702,10 @@ module Net
|
|
2322
2702
|
normalize_searching_criteria(search_keys)
|
2323
2703
|
end
|
2324
2704
|
normalize_searching_criteria(search_keys)
|
2325
|
-
|
2326
|
-
|
2705
|
+
synchronize do
|
2706
|
+
send_command(cmd, algorithm, charset, *search_keys)
|
2707
|
+
clear_responses("THREAD").last || []
|
2708
|
+
end
|
2327
2709
|
end
|
2328
2710
|
|
2329
2711
|
def normalize_searching_criteria(keys)
|
@@ -2337,49 +2719,49 @@ module Net
|
|
2337
2719
|
end
|
2338
2720
|
end
|
2339
2721
|
|
2340
|
-
def
|
2341
|
-
|
2342
|
-
|
2343
|
-
|
2344
|
-
|
2345
|
-
|
2346
|
-
|
2722
|
+
def build_ssl_ctx(ssl)
|
2723
|
+
if ssl
|
2724
|
+
params = (Hash.try_convert(ssl) || {}).freeze
|
2725
|
+
context = SSLContext.new
|
2726
|
+
context.set_params(params)
|
2727
|
+
if defined?(VerifyCallbackProc)
|
2728
|
+
context.verify_callback = VerifyCallbackProc
|
2347
2729
|
end
|
2348
|
-
|
2349
|
-
|
2350
|
-
params[:verify_mode] = VERIFY_PEER
|
2730
|
+
context.freeze
|
2731
|
+
[params, context]
|
2351
2732
|
else
|
2352
|
-
|
2733
|
+
false
|
2353
2734
|
end
|
2354
|
-
return params
|
2355
2735
|
end
|
2356
2736
|
|
2357
|
-
def start_tls_session
|
2358
|
-
unless defined?(OpenSSL::SSL)
|
2359
|
-
|
2360
|
-
|
2361
|
-
|
2362
|
-
raise RuntimeError, "already using SSL"
|
2363
|
-
end
|
2364
|
-
begin
|
2365
|
-
params = params.to_hash
|
2366
|
-
rescue NoMethodError
|
2367
|
-
params = {}
|
2368
|
-
end
|
2369
|
-
context = SSLContext.new
|
2370
|
-
context.set_params(params)
|
2371
|
-
if defined?(VerifyCallbackProc)
|
2372
|
-
context.verify_callback = VerifyCallbackProc
|
2373
|
-
end
|
2374
|
-
@sock = SSLSocket.new(@sock, context)
|
2737
|
+
def start_tls_session
|
2738
|
+
raise "SSL extension not installed" unless defined?(OpenSSL::SSL)
|
2739
|
+
raise "already using SSL" if @sock.kind_of?(OpenSSL::SSL::SSLSocket)
|
2740
|
+
raise "cannot start TLS without SSLContext" unless ssl_ctx
|
2741
|
+
@sock = SSLSocket.new(@sock, ssl_ctx)
|
2375
2742
|
@sock.sync_close = true
|
2376
2743
|
@sock.hostname = @host if @sock.respond_to? :hostname=
|
2377
2744
|
ssl_socket_connect(@sock, @open_timeout)
|
2378
|
-
if
|
2745
|
+
if ssl_ctx.verify_mode != VERIFY_NONE
|
2379
2746
|
@sock.post_connection_check(@host)
|
2747
|
+
@tls_verified = true
|
2380
2748
|
end
|
2381
2749
|
end
|
2382
2750
|
|
2751
|
+
def sasl_adapter
|
2752
|
+
SASLAdapter.new(self, &method(:send_command_with_continuations))
|
2753
|
+
end
|
2754
|
+
|
2755
|
+
#--
|
2756
|
+
# We could get the saslprep method by extending the SASLprep module
|
2757
|
+
# directly. It's done indirectly, so SASLprep can be lazily autoloaded,
|
2758
|
+
# because most users won't need it.
|
2759
|
+
#++
|
2760
|
+
# Delegates to Net::IMAP::StringPrep::SASLprep#saslprep.
|
2761
|
+
def self.saslprep(string, **opts)
|
2762
|
+
Net::IMAP::StringPrep::SASLprep.saslprep(string, **opts)
|
2763
|
+
end
|
2764
|
+
|
2383
2765
|
end
|
2384
2766
|
end
|
2385
2767
|
|
@@ -2390,4 +2772,6 @@ require_relative "imap/flags"
|
|
2390
2772
|
require_relative "imap/response_data"
|
2391
2773
|
require_relative "imap/response_parser"
|
2392
2774
|
require_relative "imap/authenticators"
|
2393
|
-
|
2775
|
+
|
2776
|
+
require_relative "imap/deprecated_client_options"
|
2777
|
+
Net::IMAP.prepend Net::IMAP::DeprecatedClientOptions
|