@@ -3358,26 +3358,75 @@ def remove_response_handler(handler)
33583358 SSL_PORT = 993 # :nodoc:
33593359
33603360 def default_ssl_and_port ( tls , port )
3361- if tls . nil? && port
3362- tls = true if port == SSL_PORT || /\A imaps\z /i === port
3363- tls = false if port == PORT
3364- elsif port . nil? && !tls . nil?
3365- port = tls ? SSL_PORT : PORT
3366- end
3367- if tls . nil? && port . nil?
3368- tls = config . default_tls . dup . freeze
3369- port = tls ? SSL_PORT : PORT
3370- if tls . nil?
3371- warn "A future version of Net::IMAP::Config#default_tls " \
3372- "will default to 'true', for secure connections by default. " \
3373- "Use 'Net::IMAP.new(host, ssl: false)' or " \
3374- "Net::IMAP.config.default_tls = false' to silence this warning."
3375- end
3361+ case [ tls && true , classify_port ( port ) ]
3362+ in true , nil then return tls , SSL_PORT
3363+ in false , nil then return tls , PORT
3364+ in nil , :tls then return true , port
3365+ in nil , :plain then return false , port
3366+ in nil , nil then return use_default_ssl
3367+ in true , :tls | :other then return tls , port
3368+ in false , :plain | :other then return tls , port
3369+ in true , :plain then return warn_mismatched_port tls , port
3370+ in false , :tls then return warn_mismatched_port tls , port
3371+ in nil , :other then return warn_nonstandard_port port
33763372 end
3373+ # TODO: move this wherever is appropriate
33773374 tls &&= tls . respond_to? ( :to_hash ) ? tls . to_hash : { }
3375+ end
3376+
3377+ # classify_port(port) -> :tls | :plain | :other | nil
3378+ def classify_port ( port )
3379+ case port
3380+ in ( SSL_PORT | /\A imaps\z /i ) then :tls
3381+ in ( PORT | /\A imap\z /i ) then :plain
3382+ in ( Integer | String ) then :other
3383+ in nil then nil
3384+ end
3385+ end
3386+
3387+ def warn_mismatched_port ( tls , port )
3388+ if tls
3389+ warn "Using TLS on plaintext IMAP port"
3390+ else
3391+ warn "Using plaintext on TLS IMAP port"
3392+ end
3393+ [ tls , port ]
3394+ end
3395+
3396+ def warn_nonstandard_port ( port )
3397+ tls = !!config . default_ssl
3398+ if config . warn_nonstandard_port_without_ssl
3399+ warn "Using #{ tls ? "TLS" : "plaintext" } on port #{ port } . " \
3400+ "Set ssl explicitly for non-standard IMAP ports."
3401+ end
3402+ # TODO: print default_ssl warning
33783403 [ tls , port ]
33793404 end
33803405
3406+ TLS_DEFAULT_WARNING =
3407+ "Net::IMAP.config.default_ssl will default to true in the future. " \
3408+ "To silence this warning, " \
3409+ "set Net::IMAP.config.default_ssl = (true | false)' or " \
3410+ "use 'Net::IMAP.new(host, ssl: (true | false))'."
3411+ private_constant :TLS_DEFAULT_WARNING
3412+
3413+ def use_default_ssl
3414+ case config . default_ssl
3415+ when true then [ true , SSL_PORT ]
3416+ when false then [ false , PORT ]
3417+ when :warn
3418+ warn TLS_DEFAULT_WARNING unless port
3419+ port ||= SSL_PORT
3420+ warn "Using TLS on port #{ port } ."
3421+ [ true , port ]
3422+ when nil
3423+ warn TLS_DEFAULT_WARNING unless port
3424+ port ||= PORT
3425+ warn "Using plain-text on port #{ port } ."
3426+ [ false , port ]
3427+ end
3428+ end
3429+
33813430 def start_imap_connection
33823431 @greeting = get_server_greeting
33833432 @capabilities = capabilities_from_resp_code @greeting
0 commit comments