@@ -3333,26 +3333,75 @@ def remove_response_handler(handler)
33333333 SSL_PORT = 993 # :nodoc:
33343334
33353335 def default_ssl_and_port ( tls , port )
3336- if tls . nil? && port
3337- tls = true if port == SSL_PORT || /\A imaps\z /i === port
3338- tls = false if port == PORT
3339- elsif port . nil? && !tls . nil?
3340- port = tls ? SSL_PORT : PORT
3341- end
3342- if tls . nil? && port . nil?
3343- tls = config . default_tls . dup . freeze
3344- port = tls ? SSL_PORT : PORT
3345- if tls . nil?
3346- warn "A future version of Net::IMAP::Config#default_tls " \
3347- "will default to 'true', for secure connections by default. " \
3348- "Use 'Net::IMAP.new(host, ssl: false)' or " \
3349- "Net::IMAP.config.default_tls = false' to silence this warning."
3350- end
3336+ case [ tls && true , classify_port ( port ) ]
3337+ in true , nil then return tls , SSL_PORT
3338+ in false , nil then return tls , PORT
3339+ in nil , :tls then return true , port
3340+ in nil , :plain then return false , port
3341+ in nil , nil then return use_default_ssl
3342+ in true , :tls | :other then return tls , port
3343+ in false , :plain | :other then return tls , port
3344+ in true , :plain then return warn_mismatched_port tls , port
3345+ in false , :tls then return warn_mismatched_port tls , port
3346+ in nil , :other then return warn_nonstandard_port port
33513347 end
3348+ # TODO: move this wherever is appropriate
33523349 tls &&= tls . respond_to? ( :to_hash ) ? tls . to_hash : { }
3350+ end
3351+
3352+ # classify_port(port) -> :tls | :plain | :other | nil
3353+ def classify_port ( port )
3354+ case port
3355+ in ( SSL_PORT | /\A imaps\z /i ) then :tls
3356+ in ( PORT | /\A imap\z /i ) then :plain
3357+ in ( Integer | String ) then :other
3358+ in nil then nil
3359+ end
3360+ end
3361+
3362+ def warn_mismatched_port ( tls , port )
3363+ if tls
3364+ warn "Using TLS on plaintext IMAP port"
3365+ else
3366+ warn "Using plaintext on TLS IMAP port"
3367+ end
3368+ [ tls , port ]
3369+ end
3370+
3371+ def warn_nonstandard_port ( port )
3372+ tls = !!config . default_ssl
3373+ if config . warn_nonstandard_port_without_ssl
3374+ warn "Using #{ tls ? "TLS" : "plaintext" } on port #{ port } . " \
3375+ "Set ssl explicitly for non-standard IMAP ports."
3376+ end
3377+ # TODO: print default_ssl warning
33533378 [ tls , port ]
33543379 end
33553380
3381+ TLS_DEFAULT_WARNING =
3382+ "Net::IMAP.config.default_ssl will default to true in the future. " \
3383+ "To silence this warning, " \
3384+ "set Net::IMAP.config.default_ssl = (true | false)' or " \
3385+ "use 'Net::IMAP.new(host, ssl: (true | false))'."
3386+ private_constant :TLS_DEFAULT_WARNING
3387+
3388+ def use_default_ssl
3389+ case config . default_ssl
3390+ when true then [ true , SSL_PORT ]
3391+ when false then [ false , PORT ]
3392+ when :warn
3393+ warn TLS_DEFAULT_WARNING unless port
3394+ port ||= SSL_PORT
3395+ warn "Using TLS on port #{ port } ."
3396+ [ true , port ]
3397+ when nil
3398+ warn TLS_DEFAULT_WARNING unless port
3399+ port ||= PORT
3400+ warn "Using plain-text on port #{ port } ."
3401+ [ false , port ]
3402+ end
3403+ end
3404+
33563405 def start_imap_connection
33573406 @greeting = get_server_greeting
33583407 @capabilities = capabilities_from_resp_code @greeting
0 commit comments