class Net::IMAP::Config
Net::IMAP::Config (available since v0.4.13) stores configuration options for Net::IMAP clients. The global configuration can be seen at either Net::IMAP.config or Net::IMAP::Config.global, and the client-specific configuration can be seen at Net::IMAP#config.
When creating a new client, all unhandled keyword arguments to Net::IMAP.new are delegated to Config.new. Every client has its own config.
debug_client = Net::IMAP.new(hostname, debug: true) quiet_client = Net::IMAP.new(hostname, debug: false) debug_client.config.debug? # => true quiet_client.config.debug? # => false
Inheritance
Configs have a parent config, and any attributes which have not been set locally will inherit the parent’s value. Every client creates its own specific config. By default, client configs inherit from Config.global.
plain_client = Net::IMAP.new(hostname) debug_client = Net::IMAP.new(hostname, debug: true) quiet_client = Net::IMAP.new(hostname, debug: false) plain_client.config.inherited?(:debug) # => true debug_client.config.inherited?(:debug) # => false quiet_client.config.inherited?(:debug) # => false plain_client.config.debug? # => false debug_client.config.debug? # => true quiet_client.config.debug? # => false # Net::IMAP.debug is delegated to Net::IMAP::Config.global.debug Net::IMAP.debug = true plain_client.config.debug? # => true debug_client.config.debug? # => true quiet_client.config.debug? # => false Net::IMAP.debug = false plain_client.config.debug = true plain_client.config.inherited?(:debug) # => false plain_client.config.debug? # => true plain_client.config.reset(:debug) plain_client.config.inherited?(:debug) # => true plain_client.config.debug? # => false
Versioned defaults
The effective default configuration for a specific x.y version of net-imap can be loaded with the config keyword argument to Net::IMAP.new. Requesting default configurations for previous versions enables extra backward compatibility with those versions:
client = Net::IMAP.new(hostname, config: 0.3) client.config.sasl_ir # => false client.config.responses_without_block # => :silence_deprecation_warning client = Net::IMAP.new(hostname, config: 0.4) client.config.sasl_ir # => true client.config.responses_without_block # => :silence_deprecation_warning client = Net::IMAP.new(hostname, config: 0.5) client.config.sasl_ir # => true client.config.responses_without_block # => :warn client = Net::IMAP.new(hostname, config: :future) client.config.sasl_ir # => true client.config.responses_without_block # => :frozen_dup
The versioned default configs inherit certain specific config options from Config.global, for example debug:
client = Net::IMAP.new(hostname, config: 0.4) Net::IMAP.debug = false client.config.debug? # => false Net::IMAP.debug = true client.config.debug? # => true
Use load_defaults to globally behave like a specific version:
client = Net::IMAP.new(hostname) client.config.sasl_ir # => true Net::IMAP.config.load_defaults 0.3 client.config.sasl_ir # => false
Named defaults
In addition to x.y version numbers, the following aliases are supported:
:default-
An alias for
:current.NOTE: This is not the same as
Config.default. It inherits some attributes fromConfig.global, for example:debug. :current-
An alias for the current
x.yversion’s defaults. :next-
The planned config for the next
x.yversion. :future-
The planned eventual config for some future
x.yversion.
For example, to disable all currently deprecated behavior:
client = Net::IMAP.new(hostname, config: :future) client.config.response_without_args # => :frozen_dup client.responses.frozen? # => true client.responses.values.all?(&:frozen?) # => true
Thread Safety
NOTE: Updates to config objects are not synchronized for thread-safety.
Attributes
The debug mode (boolean). The default value is false.
When debug is true:
-
Datasent to and received from the server will be logged. -
ResponseParserwill print warnings with extra detail for parse errors. _This may include recoverable errors._ -
ResponseParsermakes extra assertions.
NOTE: Versioned default configs inherit debug from Config.global, and load_defaults will not override debug.
Controls the behavior of Net::IMAP#login when the LOGINDISABLED capability is present. When enforced, Net::IMAP will raise a LoginDisabledError when that capability is present.
(Support for LOGINDISABLED was added in v0.5.0.)
Valid options
false(original behavior, before support was added)-
Send the
LOGINcommand without checking forLOGINDISABLED. :when_capabilities_cached-
Enforce the requirement when
Net::IMAP#capabilities_cached?is true, but do not send aCAPABILITYcommand to discover the capabilities. true(default sincev0.5)-
Only send the
LOGINcommand if theLOGINDISABLEDcapability is not present. When capabilities are unknown,Net::IMAPwill automatically send aCAPABILITYcommand first before sendingLOGIN.
Seconds to wait until an IDLE response is received, after the client asks to leave the IDLE state.
See Net::IMAP#idle and Net::IMAP#idle_done.
The default value is 5 seconds.
The maximum allowed server response size. When nil, there is no limit on response size.
The default value (512 MiB, since v0.5.7) is very high and unlikely to be reached. A much lower value should be used with untrusted servers (for example, when connecting to a user-provided hostname). When using a lower limit, message bodies should be fetched in chunks rather than all at once.
Please Note: this only limits the size per response. It does not prevent a flood of individual responses and it does not limit how many unhandled responses may be stored on the responses hash. See Unbounded memory use at Net::IMAP.
Socket reads are limited to the maximum remaining bytes for the current response: max_response_size minus the bytes that have already been read. When the limit is reached, or reading a literal would go over the limit, ResponseTooLargeError is raised and the connection is closed.
Note that changes will not take effect immediately, because the receiver thread may already be waiting for the next response using the previous value. Net::IMAP#noop can force a response and enforce the new setting immediately.
Versioned Defaults
Net::IMAP#max_response_size was added in v0.2.5 and v0.3.9 as an attr_accessor, and in v0.4.20 and v0.5.7 as a delegator to this config attribute.
-
original:
nil(no limit) -
0.5: 512 MiB
Seconds to wait until a connection is opened.
Applied separately for establishing TCP connection and starting a TLS connection.
If the IMAP object cannot open a connection within this time, it raises a Net::OpenTimeout exception.
See Net::IMAP.new and Net::IMAP#starttls.
The default value is 30 seconds.
The maximum uid-set size that ResponseParser will parse into deprecated UIDPlusData. This limit only applies when parser_use_deprecated_uidplus_data is not false.
(Parser support for UIDPLUS added in v0.3.2.)
Support for limiting UIDPlusData to a maximum size was added in v0.3.8, v0.4.19, and v0.5.6.
UIDPlusData will be removed in v0.6.
Versioned Defaults
Because this limit guards against a remote server causing catastrophic memory exhaustion, the versioned default (used by load_defaults) also applies to versions without the feature.
-
0.3and prior:10,000 -
0.4:1,000 -
0.5:100 -
0.6:0
Whether ResponseParser should use the deprecated UIDPlusData or CopyUIDData for COPYUID response codes, and UIDPlusData or AppendUIDData for APPENDUID response codes.
UIDPlusData stores its data in arrays of numbers, which is vulnerable to a memory exhaustion denial of service attack from an untrusted or compromised server. Set this option to false to completely block this vulnerability. Otherwise, parser_max_deprecated_uidplus_data_size mitigates this vulnerability.
AppendUIDData and CopyUIDData are mostly backward-compatible with UIDPlusData. Most applications should be able to upgrade with little or no changes.
(Parser support for UIDPLUS added in v0.3.2.)
(Config option added in v0.4.19 and v0.5.6.)
UIDPlusData will be removed in v0.6 and this config setting will be ignored.
Valid options
true(original default)-
ResponseParseronly usesUIDPlusData. :up_to_max_size(default sincev0.5.6)-
ResponseParserusesUIDPlusDatawhen theuid-setsize is below parser_max_deprecated_uidplus_data_size. Above that size,ResponseParserusesAppendUIDDataorCopyUIDData. false(planned default forv0.6)-
ResponseParseronly usesAppendUIDDataandCopyUIDData.
Controls the behavior of Net::IMAP#responses when called without any arguments (type or block).
Valid options
:silence_deprecation_warning(original behavior)-
Returns the mutable responses hash (without any warnings). This is not thread-safe.
:warn(default sincev0.5)-
Prints a warning and returns the mutable responses hash. This is not thread-safe.
:frozen_dup(planned default forv0.6)-
Returns a frozen copy of the unhandled responses hash, with frozen array values.
Note that calling
IMAP#responseswith atypeand without a block is not configurable and always behaves like:frozen_dup.(
:frozen_dupconfig option was added inv0.4.17) :raise-
Raise an ArgumentError with the deprecation warning.
Note: responses_without_args is an alias for responses_without_block.
Whether to use the SASL-IR extension when the server and SASL mechanism both support it. Can be overridden by the sasl_ir keyword parameter to Net::IMAP#authenticate.
(Support for SASL-IR was added in v0.4.0.)
Valid options
false(original behavior, before support was added)-
Do not use
SASL-IR, even when it is supported by the server and the mechanism. :when_capabilities_cached-
Use
SASL-IRwhenNet::IMAP#capabilities_cached?istrueand it is supported by the server and the mechanism, but do not send aCAPABILITYcommand to discover the server capabilities.(
:when_capabilities_cachedoption was added byv0.6.0) true(default sincev0.4)-
Use
SASL-IRwhen it is supported by the server and the mechanism.
Public Class Methods
Source
# File lib/net/imap/config.rb, line 163 def self.[](config) if config.is_a?(Config) then config elsif config.nil? && global.nil? then nil elsif config.respond_to?(:to_hash) then new(global, **config).freeze else version_defaults[config] or case config when Numeric raise RangeError, "unknown config version: %p" % [config] when String, Symbol raise KeyError, "unknown config name: %p" % [config] else raise TypeError, "no implicit conversion of %s to %s" % [ config.class, Config ] end end end
Given a version number, returns the default configuration for the target version. See Versioned defaults at Config.
Given a version name, returns the default configuration for the target version. See Named defaults at Config.
Given a Hash, creates a new frozen config which inherits from Config.global. Use Config.new for an unfrozen config.
Given a config, returns that same config.
Source
# File lib/net/imap/config.rb, line 129 def self.default; @default end
The default config, which is hardcoded and frozen.
Source
# File lib/net/imap/config.rb, line 132 def self.global; @global if defined?(@global) end
The global config object. Also available from Net::IMAP.config.
Source
# File lib/net/imap/config.rb, line 439 def initialize(parent = Config.global, **attrs) super(parent) update(**attrs) yield self if block_given? end
Creates a new config object and initialize its attribute with attrs.
If parent is not given, the global config is used by default.
If a block is given, the new config object is yielded to it.
Net::IMAP::Config::AttrInheritance#new
Source
# File lib/net/imap/config.rb, line 145 def self.version_defaults; AttrVersionDefaults.version_defaults end
A hash of hard-coded configurations, indexed by version number or name. Values can be accessed with any object that responds to to_sym or to_r/to_f with a non-zero number.
Config::[] gets named or numbered versions from this hash.
For example:
Net::IMAP::Config.version_defaults[0.5] == Net::IMAP::Config[0.5] Net::IMAP::Config[0.5] == Net::IMAP::Config[0.5r] # => true Net::IMAP::Config["current"] == Net::IMAP::Config[:current] # => true Net::IMAP::Config["0.5.6"] == Net::IMAP::Config[0.5r] # => true
Public Instance Methods
Source
# File lib/net/imap/config.rb, line 543 def inspect; "#<#{inspect_recursive}>" end
Returns a string representation of overriden config attributes and the inheritance chain.
Attributes overridden by ancestors are also inspected, recursively. Attributes that are inherited from default configs are not shown (see Versioned defaults at Config and Named defaults at Config).
# (Line breaks have been added to the example output for legibility.) Net::IMAP::Config.new(0.4) .new(open_timeout: 10, enforce_logindisabled: true) .inspect #=> "#<Net::IMAP::Config:0x0000745871125410 open_timeout=10 enforce_logindisabled=true # inherits from Net::IMAP::Config[0.4] # inherits from Net::IMAP::Config.global # inherits from Net::IMAP::Config.default>"
Non-default attributes are listed after the ancestor config from which they are inherited.
# (Line breaks have been added to the example output for legibility.) config = Net::IMAP::Config.global .new(open_timeout: 10, idle_response_timeout: 2) .new(enforce_logindisabled: :when_capabilities_cached, sasl_ir: false) config.inspect #=> "#<Net::IMAP::Config:0x00007ce2a1e20e40 sasl_ir=false enforce_logindisabled=:when_capabilities_cached # inherits from Net::IMAP::Config:0x00007ce2a1e20f80 open_timeout=10 idle_response_timeout=2 # inherits from Net::IMAP::Config.global # inherits from Net::IMAP::Config.default>" Net::IMAP.debug = true config.inspect #=> "#<Net::IMAP::Config:0x00007ce2a1e20e40 sasl_ir=false enforce_logindisabled=:when_capabilities_cached # inherits from Net::IMAP::Config:0x00007ce2a1e20f80 open_timeout=10 idle_response_timeout=2 # inherits from Net::IMAP::Config.global debug=true # inherits from Net::IMAP::Config.default>"
Use pp (see pretty_print) to inspect all config attributes, including default values.
Use to_h to inspect all config attributes ignoring inheritance.
Source
# File lib/net/imap/config.rb, line 490 def load_defaults(version) [Numeric, Symbol, String].any? { _1 === version } or raise ArgumentError, "expected number or symbol, got %p" % [version] update(**Config[version].defaults_hash) end
Resets the current config to behave like the versioned default configuration for version. parent will not be changed.
Some config attributes default to inheriting from their parent (which is usually Config.global) and are left unchanged, for example: debug.
See Versioned defaults at Config and Named defaults at Config.
Source
# File lib/net/imap/config.rb, line 569 def pretty_print(pp) pp.group(2, "#<", ">") do pretty_print_recursive(pp) end end
Used by PP to create a string representation of all config attributes and the inheritance chain. Inherited attributes are listed with the ancestor config from which they are inherited.
pp Config.new[0.4].new(open_timeout: 10, idle_response_timeout: 10) # #<Net::IMAP::Config:0x0000745871125410 # open_timeout=10 # idle_response_timeout=10 # inherits from Net::IMAP::Config[0.4] # responses_without_block=:silence_deprecation_warning # max_response_size=nil # sasl_ir=true # enforce_logindisabled=false # parser_use_deprecated_uidplus_data=true # parser_max_deprecated_uidplus_data_size=1000 # inherits from Net::IMAP::Config.global # inherits from Net::IMAP::Config.default # debug=false>
Source
# File lib/net/imap/config.rb, line 499 def to_h; data.members.to_h { [_1, send(_1)] } end
Returns all config attributes in a hash.
Source
# File lib/net/imap/config.rb, line 455 def update(**attrs) unless (bad = attrs.keys.reject { respond_to?(:"#{_1}=") }).empty? raise ArgumentError, "invalid config options: #{bad.join(", ")}" end attrs.each do send(:"#{_1}=", _2) end self end
Assigns all of the provided attrs to this config, and returns self.
An ArgumentError is raised unless every key in attrs matches an assignment method on Config.
NOTE:
updateis not atomic. If an exception is raised due to an invalid attribute value,attrsmay be partially applied.
Source
# File lib/net/imap/config.rb, line 473 def with(**attrs) attrs.empty? and raise ArgumentError, "expected keyword arguments, none given" copy = new(**attrs) copy.freeze if frozen? block_given? ? yield(copy) : copy end
Without a block, returns a new config which inherits from self. With a block, yields the new config and returns the block’s result.
If no keyword arguments are given, an ArgumentError will be raised.
If self is frozen, the copy will also be frozen.