@@ -3116,26 +3116,75 @@ def remove_response_handler(handler)
31163116 SSL_PORT = 993 # :nodoc:
31173117
31183118 def default_ssl_and_port ( tls , port )
3119- if tls . nil? && port
3120- tls = true if port == SSL_PORT || /\A imaps\z /i === port
3121- tls = false if port == PORT
3122- elsif port . nil? && !tls . nil?
3123- port = tls ? SSL_PORT : PORT
3124- end
3125- if tls . nil? && port . nil?
3126- tls = config . default_tls . dup . freeze
3127- port = tls ? SSL_PORT : PORT
3128- if tls . nil?
3129- warn "A future version of Net::IMAP::Config#default_tls " \
3130- "will default to 'true', for secure connections by default. " \
3131- "Use 'Net::IMAP.new(host, ssl: false)' or " \
3132- "Net::IMAP.config.default_tls = false' to silence this warning."
3133- end
3119+ case [ tls && true , classify_port ( port ) ]
3120+ in true , nil then return tls , SSL_PORT
3121+ in false , nil then return tls , PORT
3122+ in nil , :tls then return true , port
3123+ in nil , :plain then return false , port
3124+ in nil , nil then return use_default_ssl
3125+ in true , :tls | :other then return tls , port
3126+ in false , :plain | :other then return tls , port
3127+ in true , :plain then return warn_mismatched_port tls , port
3128+ in false , :tls then return warn_mismatched_port tls , port
3129+ in nil , :other then return warn_nonstandard_port port
31343130 end
3131+ # TODO: move this wherever is appropriate
31353132 tls &&= tls . respond_to? ( :to_hash ) ? tls . to_hash : { }
3133+ end
3134+
3135+ # classify_port(port) -> :tls | :plain | :other | nil
3136+ def classify_port ( port )
3137+ case port
3138+ in ( SSL_PORT | /\A imaps\z /i ) then :tls
3139+ in ( PORT | /\A imap\z /i ) then :plain
3140+ in ( Integer | String ) then :other
3141+ in nil then nil
3142+ end
3143+ end
3144+
3145+ def warn_mismatched_port ( tls , port )
3146+ if tls
3147+ warn "Using TLS on plaintext IMAP port"
3148+ else
3149+ warn "Using plaintext on TLS IMAP port"
3150+ end
3151+ [ tls , port ]
3152+ end
3153+
3154+ def warn_nonstandard_port ( port )
3155+ tls = !!config . default_ssl
3156+ if config . warn_nonstandard_port_without_ssl
3157+ warn "Using #{ tls ? "TLS" : "plaintext" } on port #{ port } . " \
3158+ "Set ssl explicitly for non-standard IMAP ports."
3159+ end
3160+ # TODO: print default_ssl warning
31363161 [ tls , port ]
31373162 end
31383163
3164+ TLS_DEFAULT_WARNING =
3165+ "Net::IMAP.config.default_ssl will default to true in the future. " \
3166+ "To silence this warning, " \
3167+ "set Net::IMAP.config.default_ssl = (true | false)' or " \
3168+ "use 'Net::IMAP.new(host, ssl: (true | false))'."
3169+ private_constant :TLS_DEFAULT_WARNING
3170+
3171+ def use_default_ssl
3172+ case config . default_ssl
3173+ when true then [ true , SSL_PORT ]
3174+ when false then [ false , PORT ]
3175+ when :warn
3176+ warn TLS_DEFAULT_WARNING unless port
3177+ port ||= SSL_PORT
3178+ warn "Using TLS on port #{ port } ."
3179+ [ true , port ]
3180+ when nil
3181+ warn TLS_DEFAULT_WARNING unless port
3182+ port ||= PORT
3183+ warn "Using plain-text on port #{ port } ."
3184+ [ false , port ]
3185+ end
3186+ end
3187+
31393188 def start_imap_connection
31403189 @greeting = get_server_greeting
31413190 @capabilities = capabilities_from_resp_code @greeting
0 commit comments