-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🗑️ Add deprecation warnings to .new and #starttls
Preparing for a (backwards-incompatible) secure-by-default configuration, Net::IMAP.default_ssl will be used when no explicit port or tls setting is provided. TODO: should truthy default_ssl be used to config params when port is 993 but ssl is implicit? Another var? Moved all deprecated option handling to DeprecatedClientOptions, which is prepended to Net::IMAP. Additionally, split `initialize` up into small helper methods making it easier to understand at a glance.
- Loading branch information
Showing
2 changed files
with
227 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# frozen_string_literal: true | ||
|
||
module Net | ||
class IMAP < Protocol | ||
|
||
# This module handles deprecated arguments to various methods. It will be | ||
# deleted in a future release. | ||
module DeprecatedClientOptions | ||
UNDEFINED = Module.new.freeze | ||
private_constant :UNDEFINED | ||
|
||
# Passing any arguments *both* positionally and as a keyword will raise an | ||
# ArgumentError. | ||
# | ||
# Allows port to be sent as an integer or string without warning or error. | ||
# | ||
# SSL options sent positionally will print a deprecation warning (and, | ||
# eventually, raise an argument error). | ||
def initialize(host, *deprecated, **options) | ||
unless deprecated.empty? | ||
port_or_options, sslopts = deprecated | ||
if !port_or_options.nil? && | ||
(port_or_options.respond_to?(:to_hash) ? | ||
options.empty? : options.key?(:port)) || | ||
!sslopts.nil? && options.key?(:ssl) | ||
raise ArgumentError, "Don't use both positional and keyword options" | ||
end | ||
# handle port_or_options => options | ||
if port_or_options.respond_to?(:to_hash) | ||
warn "DEPRECATED: options should be set by keyword arguments" | ||
options = port_or_options.to_hash | ||
elsif !port_or_options.nil? | ||
warn "DEPRECATED: port should be set by keyword argument" | ||
options[:port] = port_or_options | ||
end | ||
# handle ssl options | ||
unless sslopts.nil? | ||
warn "DEPRECATED: SSL options should be set by keyword argument" | ||
usessl, certs, verify = sslopts | ||
if usessl | ||
options[:ssl] = create_ssl_params(certs, verify) | ||
end | ||
end | ||
end | ||
super(host, **options) | ||
end | ||
|
||
# +call-seq: | ||
# starttls(options = {}) | ||
# starttls(certs, verify = true) | ||
# | ||
# For backward compatibility. A future release will only accept | ||
# OpenSSL::SSL::SSLContext.set_params options. | ||
def starttls(options = {}, verify = UNDEFINED) | ||
if options.respond_to?(:to_str) | ||
warn "DEPRECATED: starttls(certs, verify). Use starttls(ssl_params)" | ||
certs = options.to_str | ||
verify = true if verify == UNDEFINED | ||
options = create_ssl_params(certs, verify) | ||
end | ||
super(options) | ||
end | ||
|
||
private | ||
|
||
def create_ssl_params(certs, verify) | ||
params = {} | ||
if certs | ||
if File.file?(certs) | ||
params[:ca_file] = certs | ||
elsif File.directory?(certs) | ||
params[:ca_path] = certs | ||
end | ||
end | ||
params[:verify_mode] = verify ? VERIFY_PEER : VERIFY_NONE | ||
params | ||
end | ||
|
||
end | ||
|
||
prepend DeprecatedClientOptions | ||
end | ||
end |