@@ -2711,26 +2711,75 @@ def remove_response_handler(handler)
27112711 SSL_PORT = 993 # :nodoc:
27122712
27132713 def default_ssl_and_port ( tls , port )
2714- if tls . nil? && port
2715- tls = true if port == SSL_PORT || /\A imaps\z /i === port
2716- tls = false if port == PORT
2717- elsif port . nil? && !tls . nil?
2718- port = tls ? SSL_PORT : PORT
2719- end
2720- if tls . nil? && port . nil?
2721- tls = config . default_tls . dup . freeze
2722- port = tls ? SSL_PORT : PORT
2723- if tls . nil?
2724- warn "A future version of Net::IMAP::Config#default_tls " \
2725- "will default to 'true', for secure connections by default. " \
2726- "Use 'Net::IMAP.new(host, ssl: false)' or " \
2727- "Net::IMAP.config.default_tls = false' to silence this warning."
2728- end
2714+ case [ tls && true , classify_port ( port ) ]
2715+ in true , nil then return tls , SSL_PORT
2716+ in false , nil then return tls , PORT
2717+ in nil , :tls then return true , port
2718+ in nil , :plain then return false , port
2719+ in nil , nil then return use_default_ssl
2720+ in true , :tls | :other then return tls , port
2721+ in false , :plain | :other then return tls , port
2722+ in true , :plain then return warn_mismatched_port tls , port
2723+ in false , :tls then return warn_mismatched_port tls , port
2724+ in nil , :other then return warn_nonstandard_port port
27292725 end
2726+ # TODO: move this wherever is appropriate
27302727 tls &&= tls . respond_to? ( :to_hash ) ? tls . to_hash : { }
2728+ end
2729+
2730+ # classify_port(port) -> :tls | :plain | :other | nil
2731+ def classify_port ( port )
2732+ case port
2733+ in ( SSL_PORT | /\A imaps\z /i ) then :tls
2734+ in ( PORT | /\A imap\z /i ) then :plain
2735+ in ( Integer | String ) then :other
2736+ in nil then nil
2737+ end
2738+ end
2739+
2740+ def warn_mismatched_port ( tls , port )
2741+ if tls
2742+ warn "Using TLS on plaintext IMAP port"
2743+ else
2744+ warn "Using plaintext on TLS IMAP port"
2745+ end
2746+ [ tls , port ]
2747+ end
2748+
2749+ def warn_nonstandard_port ( port )
2750+ tls = !!config . default_ssl
2751+ if config . warn_nonstandard_port_without_ssl
2752+ warn "Using #{ tls ? "TLS" : "plaintext" } on port #{ port } . " \
2753+ "Set ssl explicitly for non-standard IMAP ports."
2754+ end
2755+ # TODO: print default_ssl warning
27312756 [ tls , port ]
27322757 end
27332758
2759+ TLS_DEFAULT_WARNING =
2760+ "Net::IMAP.config.default_ssl will default to true in the future. " \
2761+ "To silence this warning, " \
2762+ "set Net::IMAP.config.default_ssl = (true | false)' or " \
2763+ "use 'Net::IMAP.new(host, ssl: (true | false))'."
2764+ private_constant :TLS_DEFAULT_WARNING
2765+
2766+ def use_default_ssl
2767+ case config . default_ssl
2768+ when true then [ true , SSL_PORT ]
2769+ when false then [ false , PORT ]
2770+ when :warn
2771+ warn TLS_DEFAULT_WARNING unless port
2772+ port ||= SSL_PORT
2773+ warn "Using TLS on port #{ port } ."
2774+ [ true , port ]
2775+ when nil
2776+ warn TLS_DEFAULT_WARNING unless port
2777+ port ||= PORT
2778+ warn "Using plain-text on port #{ port } ."
2779+ [ false , port ]
2780+ end
2781+ end
2782+
27342783 def start_imap_connection
27352784 @greeting = get_server_greeting
27362785 @capabilities = capabilities_from_resp_code @greeting
0 commit comments