Skip to content

Commit 272a3a5

Browse files
committed
🚧 update default_ssl_and_port ... WIP
1 parent 25199dd commit 272a3a5

File tree

2 files changed

+82
-15
lines changed

2 files changed

+82
-15
lines changed

lib/net/imap.rb

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

29052905
def default_ssl_and_port(tls, port)
2906-
if tls.nil? && port
2907-
tls = true if port == SSL_PORT || /\Aimaps\z/i === port
2908-
tls = false if port == PORT
2909-
elsif port.nil? && !tls.nil?
2910-
port = tls ? SSL_PORT : PORT
2911-
end
2912-
if tls.nil? && port.nil?
2913-
tls = config.default_tls.dup.freeze
2914-
port = tls ? SSL_PORT : PORT
2915-
if tls.nil?
2916-
warn "A future version of Net::IMAP::Config#default_tls " \
2917-
"will default to 'true', for secure connections by default. " \
2918-
"Use 'Net::IMAP.new(host, ssl: false)' or " \
2919-
"Net::IMAP.config.default_tls = false' to silence this warning."
2920-
end
2906+
case [tls && true, classify_port(port)]
2907+
in true, nil then return tls, SSL_PORT
2908+
in false, nil then return tls, PORT
2909+
in nil, :tls then return true, port
2910+
in nil, :plain then return false, port
2911+
in nil, nil then return use_default_ssl
2912+
in true, :tls | :other then return tls, port
2913+
in false, :plain | :other then return tls, port
2914+
in true, :plain then return warn_mismatched_port tls, port
2915+
in false, :tls then return warn_mismatched_port tls, port
2916+
in nil, :other then return warn_nonstandard_port port
29212917
end
2918+
# TODO: move this wherever is appropriate
29222919
tls &&= tls.respond_to?(:to_hash) ? tls.to_hash : {}
2920+
end
2921+
2922+
# classify_port(port) -> :tls | :plain | :other | nil
2923+
def classify_port(port)
2924+
case port
2925+
in (SSL_PORT | /\Aimaps\z/i) then :tls
2926+
in (PORT | /\Aimap\z/i) then :plain
2927+
in (Integer | String) then :other
2928+
in nil then nil
2929+
end
2930+
end
2931+
2932+
def warn_mismatched_port(tls, port)
2933+
if tls
2934+
warn "Using TLS on plaintext IMAP port"
2935+
else
2936+
warn "Using plaintext on TLS IMAP port"
2937+
end
2938+
[tls, port]
2939+
end
2940+
2941+
def warn_nonstandard_port(port)
2942+
tls = !!config.default_ssl
2943+
if config.warn_nonstandard_port_without_ssl
2944+
warn "Using #{tls ? "TLS" : "plaintext"} on port #{port}. " \
2945+
"Set ssl explicitly for non-standard IMAP ports."
2946+
end
2947+
# TODO: print default_ssl warning
29232948
[tls, port]
29242949
end
29252950

2951+
TLS_DEFAULT_WARNING =
2952+
"Net::IMAP.config.default_ssl will default to true in the future. " \
2953+
"To silence this warning, " \
2954+
"set Net::IMAP.config.default_ssl = (true | false)' or " \
2955+
"use 'Net::IMAP.new(host, ssl: (true | false))'."
2956+
private_constant :TLS_DEFAULT_WARNING
2957+
2958+
def use_default_ssl
2959+
case config.default_ssl
2960+
when true then [true, SSL_PORT]
2961+
when false then [false, PORT]
2962+
when :warn
2963+
warn TLS_DEFAULT_WARNING unless port
2964+
port ||= SSL_PORT
2965+
warn "Using TLS on port #{port}."
2966+
[true, port]
2967+
when nil
2968+
warn TLS_DEFAULT_WARNING unless port
2969+
port ||= PORT
2970+
warn "Using plain-text on port #{port}."
2971+
[false, port]
2972+
end
2973+
end
2974+
29262975
def start_imap_connection
29272976
@greeting = get_server_greeting
29282977
@capabilities = capabilities_from_resp_code @greeting

lib/net/imap/config.rb

Lines changed: 18 additions & 0 deletions
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)