Skip to content

Commit 3e070d8

Browse files
committed
🚧 update default_ssl_and_port ... WIP
1 parent 69cd279 commit 3e070d8

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
@@ -3116,26 +3116,75 @@ def remove_response_handler(handler)
31163116
SSL_PORT = 993 # :nodoc:
31173117

31183118
def default_ssl_and_port(tls, port)
3119-
if tls.nil? && port
3120-
tls = true if port == SSL_PORT || /\Aimaps\z/i === port
3121-
tls = false if port == PORT
3122-
elsif port.nil? && !tls.nil?
3123-
port = tls ? SSL_PORT : PORT
3124-
end
3125-
if tls.nil? && port.nil?
3126-
tls = config.default_tls.dup.freeze
3127-
port = tls ? SSL_PORT : PORT
3128-
if tls.nil?
3129-
warn "A future version of Net::IMAP::Config#default_tls " \
3130-
"will default to 'true', for secure connections by default. " \
3131-
"Use 'Net::IMAP.new(host, ssl: false)' or " \
3132-
"Net::IMAP.config.default_tls = false' to silence this warning."
3133-
end
3119+
case [tls && true, classify_port(port)]
3120+
in true, nil then return tls, SSL_PORT
3121+
in false, nil then return tls, PORT
3122+
in nil, :tls then return true, port
3123+
in nil, :plain then return false, port
3124+
in nil, nil then return use_default_ssl
3125+
in true, :tls | :other then return tls, port
3126+
in false, :plain | :other then return tls, port
3127+
in true, :plain then return warn_mismatched_port tls, port
3128+
in false, :tls then return warn_mismatched_port tls, port
3129+
in nil, :other then return warn_nonstandard_port port
31343130
end
3131+
# TODO: move this wherever is appropriate
31353132
tls &&= tls.respond_to?(:to_hash) ? tls.to_hash : {}
3133+
end
3134+
3135+
# classify_port(port) -> :tls | :plain | :other | nil
3136+
def classify_port(port)
3137+
case port
3138+
in (SSL_PORT | /\Aimaps\z/i) then :tls
3139+
in (PORT | /\Aimap\z/i) then :plain
3140+
in (Integer | String) then :other
3141+
in nil then nil
3142+
end
3143+
end
3144+
3145+
def warn_mismatched_port(tls, port)
3146+
if tls
3147+
warn "Using TLS on plaintext IMAP port"
3148+
else
3149+
warn "Using plaintext on TLS IMAP port"
3150+
end
3151+
[tls, port]
3152+
end
3153+
3154+
def warn_nonstandard_port(port)
3155+
tls = !!config.default_ssl
3156+
if config.warn_nonstandard_port_without_ssl
3157+
warn "Using #{tls ? "TLS" : "plaintext"} on port #{port}. " \
3158+
"Set ssl explicitly for non-standard IMAP ports."
3159+
end
3160+
# TODO: print default_ssl warning
31363161
[tls, port]
31373162
end
31383163

3164+
TLS_DEFAULT_WARNING =
3165+
"Net::IMAP.config.default_ssl will default to true in the future. " \
3166+
"To silence this warning, " \
3167+
"set Net::IMAP.config.default_ssl = (true | false)' or " \
3168+
"use 'Net::IMAP.new(host, ssl: (true | false))'."
3169+
private_constant :TLS_DEFAULT_WARNING
3170+
3171+
def use_default_ssl
3172+
case config.default_ssl
3173+
when true then [true, SSL_PORT]
3174+
when false then [false, PORT]
3175+
when :warn
3176+
warn TLS_DEFAULT_WARNING unless port
3177+
port ||= SSL_PORT
3178+
warn "Using TLS on port #{port}."
3179+
[true, port]
3180+
when nil
3181+
warn TLS_DEFAULT_WARNING unless port
3182+
port ||= PORT
3183+
warn "Using plain-text on port #{port}."
3184+
[false, port]
3185+
end
3186+
end
3187+
31393188
def start_imap_connection
31403189
@greeting = get_server_greeting
31413190
@capabilities = capabilities_from_resp_code @greeting

lib/net/imap/config.rb

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

239+
# Whether to warn for using default_ssl when the port is non-standard.
240+
#
241+
# Although default_ssl is used for non-standard ports, this warning is
242+
# different replaces the warning when default_ssl is +nil+ or +:warn+.
243+
# When this option is false but default_ssl is +nil+ or +:warn+, that
244+
# warning will be printed instead.
245+
#
246+
# ==== Valid options
247+
#
248+
# [+false+ <em>(original behavior)</em>]
249+
# Don't print a special warning for nonstandard ports without explicit
250+
# +ssl+.
251+
# [+true+ <em>(eventual future default)</em>]
252+
# Print a special warning for nonstandard ports without explicit +ssl+.
253+
attr_accessor :warn_nonstandard_port_without_ssl, type: :boolean
254+
239255
# Whether to use the +SASL-IR+ extension when the server and \SASL
240256
# mechanism both support it. Can be overridden by the +sasl_ir+ keyword
241257
# parameter to Net::IMAP#authenticate.
@@ -392,6 +408,7 @@ def defaults_hash
392408
open_timeout: 30,
393409
idle_response_timeout: 5,
394410
default_ssl: false,
411+
warn_nonstandard_port_without_ssl: false,
395412
sasl_ir: true,
396413
enforce_logindisabled: true,
397414
responses_without_block: :warn,
@@ -430,6 +447,7 @@ def defaults_hash
430447

431448
version_defaults[:future] = Config[0.7].dup.update(
432449
default_ssl: true,
450+
warn_nonstandard_port_without_ssl: true,
433451
).freeze
434452

435453
version_defaults.freeze

0 commit comments

Comments
 (0)