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