Skip to content

Commit 06c3eba

Browse files
committed
🚧 update default_ssl_and_port ... WIP
1 parent 78a5bba commit 06c3eba

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

27132713
def default_ssl_and_port(tls, port)
2714-
if tls.nil? && port
2715-
tls = true if port == SSL_PORT || /\Aimaps\z/i === port
2716-
tls = false if port == PORT
2717-
elsif port.nil? && !tls.nil?
2718-
port = tls ? SSL_PORT : PORT
2719-
end
2720-
if tls.nil? && port.nil?
2721-
tls = config.default_tls.dup.freeze
2722-
port = tls ? SSL_PORT : PORT
2723-
if tls.nil?
2724-
warn "A future version of Net::IMAP::Config#default_tls " \
2725-
"will default to 'true', for secure connections by default. " \
2726-
"Use 'Net::IMAP.new(host, ssl: false)' or " \
2727-
"Net::IMAP.config.default_tls = false' to silence this warning."
2728-
end
2714+
case [tls && true, classify_port(port)]
2715+
in true, nil then return tls, SSL_PORT
2716+
in false, nil then return tls, PORT
2717+
in nil, :tls then return true, port
2718+
in nil, :plain then return false, port
2719+
in nil, nil then return use_default_ssl
2720+
in true, :tls | :other then return tls, port
2721+
in false, :plain | :other then return tls, port
2722+
in true, :plain then return warn_mismatched_port tls, port
2723+
in false, :tls then return warn_mismatched_port tls, port
2724+
in nil, :other then return warn_nonstandard_port port
27292725
end
2726+
# TODO: move this wherever is appropriate
27302727
tls &&= tls.respond_to?(:to_hash) ? tls.to_hash : {}
2728+
end
2729+
2730+
# classify_port(port) -> :tls | :plain | :other | nil
2731+
def classify_port(port)
2732+
case port
2733+
in (SSL_PORT | /\Aimaps\z/i) then :tls
2734+
in (PORT | /\Aimap\z/i) then :plain
2735+
in (Integer | String) then :other
2736+
in nil then nil
2737+
end
2738+
end
2739+
2740+
def warn_mismatched_port(tls, port)
2741+
if tls
2742+
warn "Using TLS on plaintext IMAP port"
2743+
else
2744+
warn "Using plaintext on TLS IMAP port"
2745+
end
2746+
[tls, port]
2747+
end
2748+
2749+
def warn_nonstandard_port(port)
2750+
tls = !!config.default_ssl
2751+
if config.warn_nonstandard_port_without_ssl
2752+
warn "Using #{tls ? "TLS" : "plaintext"} on port #{port}. " \
2753+
"Set ssl explicitly for non-standard IMAP ports."
2754+
end
2755+
# TODO: print default_ssl warning
27312756
[tls, port]
27322757
end
27332758

2759+
TLS_DEFAULT_WARNING =
2760+
"Net::IMAP.config.default_ssl will default to true in the future. " \
2761+
"To silence this warning, " \
2762+
"set Net::IMAP.config.default_ssl = (true | false)' or " \
2763+
"use 'Net::IMAP.new(host, ssl: (true | false))'."
2764+
private_constant :TLS_DEFAULT_WARNING
2765+
2766+
def use_default_ssl
2767+
case config.default_ssl
2768+
when true then [true, SSL_PORT]
2769+
when false then [false, PORT]
2770+
when :warn
2771+
warn TLS_DEFAULT_WARNING unless port
2772+
port ||= SSL_PORT
2773+
warn "Using TLS on port #{port}."
2774+
[true, port]
2775+
when nil
2776+
warn TLS_DEFAULT_WARNING unless port
2777+
port ||= PORT
2778+
warn "Using plain-text on port #{port}."
2779+
[false, port]
2780+
end
2781+
end
2782+
27342783
def start_imap_connection
27352784
@greeting = get_server_greeting
27362785
@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)