Skip to content

Commit 240035b

Browse files
committed
🚧 update default_ssl_and_port ... WIP
1 parent be14229 commit 240035b

File tree

2 files changed

+83
-15
lines changed

2 files changed

+83
-15
lines changed

lib/net/imap.rb

Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3358,26 +3358,75 @@ def remove_response_handler(handler)
33583358
SSL_PORT = 993 # :nodoc:
33593359

33603360
def default_ssl_and_port(tls, port)
3361-
if tls.nil? && port
3362-
tls = true if port == SSL_PORT || /\Aimaps\z/i === port
3363-
tls = false if port == PORT
3364-
elsif port.nil? && !tls.nil?
3365-
port = tls ? SSL_PORT : PORT
3366-
end
3367-
if tls.nil? && port.nil?
3368-
tls = config.default_tls.dup.freeze
3369-
port = tls ? SSL_PORT : PORT
3370-
if tls.nil?
3371-
warn "A future version of Net::IMAP::Config#default_tls " \
3372-
"will default to 'true', for secure connections by default. " \
3373-
"Use 'Net::IMAP.new(host, ssl: false)' or " \
3374-
"Net::IMAP.config.default_tls = false' to silence this warning."
3375-
end
3361+
case [tls && true, classify_port(port)]
3362+
in true, nil then return tls, SSL_PORT
3363+
in false, nil then return tls, PORT
3364+
in nil, :tls then return true, port
3365+
in nil, :plain then return false, port
3366+
in nil, nil then return use_default_ssl
3367+
in true, :tls | :other then return tls, port
3368+
in false, :plain | :other then return tls, port
3369+
in true, :plain then return warn_mismatched_port tls, port
3370+
in false, :tls then return warn_mismatched_port tls, port
3371+
in nil, :other then return warn_nonstandard_port port
33763372
end
3373+
# TODO: move this wherever is appropriate
33773374
tls &&= tls.respond_to?(:to_hash) ? tls.to_hash : {}
3375+
end
3376+
3377+
# classify_port(port) -> :tls | :plain | :other | nil
3378+
def classify_port(port)
3379+
case port
3380+
in (SSL_PORT | /\Aimaps\z/i) then :tls
3381+
in (PORT | /\Aimap\z/i) then :plain
3382+
in (Integer | String) then :other
3383+
in nil then nil
3384+
end
3385+
end
3386+
3387+
def warn_mismatched_port(tls, port)
3388+
if tls
3389+
warn "Using TLS on plaintext IMAP port"
3390+
else
3391+
warn "Using plaintext on TLS IMAP port"
3392+
end
3393+
[tls, port]
3394+
end
3395+
3396+
def warn_nonstandard_port(port)
3397+
tls = !!config.default_ssl
3398+
if config.warn_nonstandard_port_without_ssl
3399+
warn "Using #{tls ? "TLS" : "plaintext"} on port #{port}. " \
3400+
"Set ssl explicitly for non-standard IMAP ports."
3401+
end
3402+
# TODO: print default_ssl warning
33783403
[tls, port]
33793404
end
33803405

3406+
TLS_DEFAULT_WARNING =
3407+
"Net::IMAP.config.default_ssl will default to true in the future. " \
3408+
"To silence this warning, " \
3409+
"set Net::IMAP.config.default_ssl = (true | false)' or " \
3410+
"use 'Net::IMAP.new(host, ssl: (true | false))'."
3411+
private_constant :TLS_DEFAULT_WARNING
3412+
3413+
def use_default_ssl
3414+
case config.default_ssl
3415+
when true then [true, SSL_PORT]
3416+
when false then [false, PORT]
3417+
when :warn
3418+
warn TLS_DEFAULT_WARNING unless port
3419+
port ||= SSL_PORT
3420+
warn "Using TLS on port #{port}."
3421+
[true, port]
3422+
when nil
3423+
warn TLS_DEFAULT_WARNING unless port
3424+
port ||= PORT
3425+
warn "Using plain-text on port #{port}."
3426+
[false, port]
3427+
end
3428+
end
3429+
33813430
def start_imap_connection
33823431
@greeting = get_server_greeting
33833432
@capabilities = capabilities_from_resp_code @greeting

lib/net/imap/config.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,25 @@ def self.[](config)
254254
1.0r => true,
255255
}
256256

257+
# Whether to warn for using default_ssl when the port is non-standard.
258+
#
259+
# Although default_ssl is used for non-standard ports, this warning is
260+
# different replaces the warning when default_ssl is +nil+ or +:warn+.
261+
# When this option is false but default_ssl is +nil+ or +:warn+, that
262+
# warning will be printed instead.
263+
#
264+
# ==== Valid options
265+
#
266+
# [+false+ <em>(original behavior)</em>]
267+
# Don't print a special warning for nonstandard ports without explicit
268+
# +ssl+.
269+
# [+true+ <em>(eventual future default)</em>]
270+
# Print a special warning for nonstandard ports without explicit +ssl+.
271+
attr_accessor :warn_nonstandard_port_without_ssl, type: :boolean, defaults: {
272+
0.0r => false,
273+
0.7r => true,
274+
}
275+
257276
# Whether to use the +SASL-IR+ extension when the server and \SASL
258277
# mechanism both support it. Can be overridden by the +sasl_ir+ keyword
259278
# parameter to Net::IMAP#authenticate.

0 commit comments

Comments
 (0)