@@ -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 || /\A imaps\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 | /\A imaps\z /i ) then :tls
3208+ in ( PORT | /\A imap\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
0 commit comments