Skip to content

Commit 350e96b

Browse files
committed
🚧 update default_ssl_and_port ... WIP
1 parent 28888ec commit 350e96b

File tree

2 files changed

+82
-15
lines changed

2 files changed

+82
-15
lines changed

lib/net/imap.rb

+64-15
Original file line numberDiff line numberDiff line change
@@ -3185,26 +3185,75 @@ def remove_response_handler(handler)
31853185
SSL_PORT = 993 # :nodoc:
31863186

31873187
def default_ssl_and_port(tls, port)
3188-
if tls.nil? && port
3189-
tls = true if port == SSL_PORT || /\Aimaps\z/i === port
3190-
tls = false if port == PORT
3191-
elsif port.nil? && !tls.nil?
3192-
port = tls ? SSL_PORT : PORT
3193-
end
3194-
if tls.nil? && port.nil?
3195-
tls = config.default_tls.dup.freeze
3196-
port = tls ? SSL_PORT : PORT
3197-
if tls.nil?
3198-
warn "A future version of Net::IMAP::Config#default_tls " \
3199-
"will default to 'true', for secure connections by default. " \
3200-
"Use 'Net::IMAP.new(host, ssl: false)' or " \
3201-
"Net::IMAP.config.default_tls = false' to silence this warning."
3202-
end
3188+
case [tls && true, classify_port(port)]
3189+
in true, nil then return tls, SSL_PORT
3190+
in false, nil then return tls, PORT
3191+
in nil, :tls then return true, port
3192+
in nil, :plain then return false, port
3193+
in nil, nil then return use_default_ssl
3194+
in true, :tls | :other then return tls, port
3195+
in false, :plain | :other then return tls, port
3196+
in true, :plain then return warn_mismatched_port tls, port
3197+
in false, :tls then return warn_mismatched_port tls, port
3198+
in nil, :other then return warn_nonstandard_port port
32033199
end
3200+
# TODO: move this wherever is appropriate
32043201
tls &&= tls.respond_to?(:to_hash) ? tls.to_hash : {}
3202+
end
3203+
3204+
# classify_port(port) -> :tls | :plain | :other | nil
3205+
def classify_port(port)
3206+
case port
3207+
in (SSL_PORT | /\Aimaps\z/i) then :tls
3208+
in (PORT | /\Aimap\z/i) then :plain
3209+
in (Integer | String) then :other
3210+
in nil then nil
3211+
end
3212+
end
3213+
3214+
def warn_mismatched_port(tls, port)
3215+
if tls
3216+
warn "Using TLS on plaintext IMAP port"
3217+
else
3218+
warn "Using plaintext on TLS IMAP port"
3219+
end
3220+
[tls, port]
3221+
end
3222+
3223+
def warn_nonstandard_port(port)
3224+
tls = !!config.default_ssl
3225+
if config.warn_nonstandard_port_without_ssl
3226+
warn "Using #{tls ? "TLS" : "plaintext"} on port #{port}. " \
3227+
"Set ssl explicitly for non-standard IMAP ports."
3228+
end
3229+
# TODO: print default_ssl warning
32053230
[tls, port]
32063231
end
32073232

3233+
TLS_DEFAULT_WARNING =
3234+
"Net::IMAP.config.default_ssl will default to true in the future. " \
3235+
"To silence this warning, " \
3236+
"set Net::IMAP.config.default_ssl = (true | false)' or " \
3237+
"use 'Net::IMAP.new(host, ssl: (true | false))'."
3238+
private_constant :TLS_DEFAULT_WARNING
3239+
3240+
def use_default_ssl
3241+
case config.default_ssl
3242+
when true then [true, SSL_PORT]
3243+
when false then [false, PORT]
3244+
when :warn
3245+
warn TLS_DEFAULT_WARNING unless port
3246+
port ||= SSL_PORT
3247+
warn "Using TLS on port #{port}."
3248+
[true, port]
3249+
when nil
3250+
warn TLS_DEFAULT_WARNING unless port
3251+
port ||= PORT
3252+
warn "Using plain-text on port #{port}."
3253+
[false, port]
3254+
end
3255+
end
3256+
32083257
def start_imap_connection
32093258
@greeting = get_server_greeting
32103259
@capabilities = capabilities_from_resp_code @greeting

lib/net/imap/config.rb

+18
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,22 @@ def self.[](config)
238238
# with no params.
239239
attr_accessor :default_ssl, type: [false, nil, :warn, true]
240240

241+
# Whether to warn for using default_ssl when the port is non-standard.
242+
#
243+
# Although default_ssl is used for non-standard ports, this warning is
244+
# different replaces the warning when default_ssl is +nil+ or +:warn+.
245+
# When this option is false but default_ssl is +nil+ or +:warn+, that
246+
# warning will be printed instead.
247+
#
248+
# ==== Valid options
249+
#
250+
# [+false+ <em>(original behavior)</em>]
251+
# Don't print a special warning for nonstandard ports without explicit
252+
# +ssl+.
253+
# [+true+ <em>(eventual future default)</em>]
254+
# Print a special warning for nonstandard ports without explicit +ssl+.
255+
attr_accessor :warn_nonstandard_port_without_ssl, type: :boolean
256+
241257
# Whether to use the +SASL-IR+ extension when the server and \SASL
242258
# mechanism both support it. Can be overridden by the +sasl_ir+ keyword
243259
# parameter to Net::IMAP#authenticate.
@@ -455,6 +471,7 @@ def defaults_hash
455471
open_timeout: 30,
456472
idle_response_timeout: 5,
457473
default_ssl: false,
474+
warn_nonstandard_port_without_ssl: false,
458475
sasl_ir: true,
459476
enforce_logindisabled: true,
460477
responses_without_block: :warn,
@@ -500,6 +517,7 @@ def defaults_hash
500517

501518
version_defaults[:future] = Config[0.7].dup.update(
502519
default_ssl: true,
520+
warn_nonstandard_port_without_ssl: true,
503521
).freeze
504522

505523
version_defaults.freeze

0 commit comments

Comments
 (0)