@@ -725,6 +725,8 @@ class IMAP < Protocol
725
725
"UTF8=ONLY" => "UTF8=ACCEPT" ,
726
726
} . freeze
727
727
728
+ autoload :Config , File . expand_path ( "imap/config" , __dir__ )
729
+
728
730
autoload :SASL , File . expand_path ( "imap/sasl" , __dir__ )
729
731
autoload :SASLAdapter , File . expand_path ( "imap/sasl_adapter" , __dir__ )
730
732
autoload :StringPrep , File . expand_path ( "imap/stringprep" , __dir__ )
@@ -735,14 +737,15 @@ class IMAP < Protocol
735
737
include SSL
736
738
end
737
739
738
- # Returns the debug mode.
739
- def self . debug
740
- return @@debug
741
- end
740
+ # Returns the global Config object
741
+ def self . config ; Config . global end
742
742
743
- # Sets the debug mode.
743
+ # Returns the global debug mode.
744
+ def self . debug ; config . debug end
745
+
746
+ # Sets the global debug mode.
744
747
def self . debug = ( val )
745
- return @@ debug = val
748
+ config . debug = val
746
749
end
747
750
748
751
# The default port for IMAP connections, port 143
@@ -764,13 +767,18 @@ class << self
764
767
# Returns the initial greeting the server, an UntaggedResponse.
765
768
attr_reader :greeting
766
769
770
+ # The client configuration. See Net::IMAP::Config.
771
+ #
772
+ # By default, config inherits from the global Net::IMAP.config.
773
+ attr_reader :config
774
+
767
775
# Seconds to wait until a connection is opened.
768
776
# If the IMAP object cannot open a connection within this time,
769
777
# it raises a Net::OpenTimeout exception. The default value is 30 seconds.
770
- attr_reader : open_timeout
778
+ def open_timeout ; config . open_timeout end
771
779
772
780
# Seconds to wait until an IDLE response is received.
773
- attr_reader : idle_response_timeout
781
+ def idle_response_timeout ; config . idle_response_timeout end
774
782
775
783
# The hostname this client connected to
776
784
attr_reader :host
@@ -811,6 +819,15 @@ class << self
811
819
# the keys are names of attribute assignment methods on
812
820
# SSLContext[https://docs.ruby-lang.org/en/master/OpenSSL/SSL/SSLContext.html].
813
821
#
822
+ # [config]
823
+ # A Net::IMAP::Config object to base the client #config on. By default
824
+ # the global Net::IMAP.config is used. Note that this sets the _parent_
825
+ # config object for inheritance. Every Net::IMAP client has its own
826
+ # unique #config for overrides.
827
+ #
828
+ # Any other keyword arguments will be forwarded to Config.new, to create the
829
+ # client's #config. For example:
830
+ #
814
831
# [open_timeout]
815
832
# Seconds to wait until a connection is opened
816
833
# [idle_response_timeout]
@@ -872,13 +889,12 @@ class << self
872
889
# Connected to the host successfully, but it immediately said goodbye.
873
890
#
874
891
def initialize ( host , port : nil , ssl : nil ,
875
- open_timeout : 30 , idle_response_timeout : 5 )
892
+ config : Config . global , ** config_options )
876
893
super ( )
877
894
# Config options
878
895
@host = host
896
+ @config = Config . new ( config , **config_options )
879
897
@port = port || ( ssl ? SSL_PORT : PORT )
880
- @open_timeout = Integer ( open_timeout )
881
- @idle_response_timeout = Integer ( idle_response_timeout )
882
898
@ssl_ctx_params , @ssl_ctx = build_ssl_ctx ( ssl )
883
899
884
900
# Basic Client State
@@ -889,7 +905,7 @@ def initialize(host, port: nil, ssl: nil,
889
905
@capabilities = nil
890
906
891
907
# Client Protocol Receiver
892
- @parser = ResponseParser . new
908
+ @parser = ResponseParser . new ( config : @config )
893
909
@responses = Hash . new { |h , k | h [ k ] = [ ] }
894
910
@response_handlers = [ ]
895
911
@receiver_thread = nil
@@ -2434,7 +2450,7 @@ def idle(timeout = nil, &response_handler)
2434
2450
unless @receiver_thread_terminating
2435
2451
remove_response_handler ( response_handler )
2436
2452
put_string ( "DONE#{ CRLF } " )
2437
- response = get_tagged_response ( tag , "IDLE" , @ idle_response_timeout)
2453
+ response = get_tagged_response ( tag , "IDLE" , idle_response_timeout )
2438
2454
end
2439
2455
end
2440
2456
end
@@ -2590,8 +2606,6 @@ def remove_response_handler(handler)
2590
2606
PORT = 143 # :nodoc:
2591
2607
SSL_PORT = 993 # :nodoc:
2592
2608
2593
- @@debug = false
2594
-
2595
2609
def start_imap_connection
2596
2610
@greeting = get_server_greeting
2597
2611
@capabilities = capabilities_from_resp_code @greeting
@@ -2619,12 +2633,12 @@ def start_receiver_thread
2619
2633
end
2620
2634
2621
2635
def tcp_socket ( host , port )
2622
- s = Socket . tcp ( host , port , :connect_timeout => @ open_timeout)
2636
+ s = Socket . tcp ( host , port , :connect_timeout => open_timeout )
2623
2637
s . setsockopt ( :SOL_SOCKET , :SO_KEEPALIVE , true )
2624
2638
s
2625
2639
rescue Errno ::ETIMEDOUT
2626
2640
raise Net ::OpenTimeout , "Timeout to open TCP connection to " +
2627
- "#{ host } :#{ port } (exceeds #{ @ open_timeout} seconds)"
2641
+ "#{ host } :#{ port } (exceeds #{ open_timeout } seconds)"
2628
2642
end
2629
2643
2630
2644
def receive_responses
@@ -2736,7 +2750,7 @@ def get_response
2736
2750
end
2737
2751
end
2738
2752
return nil if buff . length == 0
2739
- if @@ debug
2753
+ if config . debug?
2740
2754
$stderr. print ( buff . gsub ( /^/n , "S: " ) )
2741
2755
end
2742
2756
return @parser . parse ( buff )
@@ -2815,7 +2829,7 @@ def generate_tag
2815
2829
2816
2830
def put_string ( str )
2817
2831
@sock . print ( str )
2818
- if @@ debug
2832
+ if config . debug?
2819
2833
if @debug_output_bol
2820
2834
$stderr. print ( "C: " )
2821
2835
end
@@ -2942,7 +2956,7 @@ def start_tls_session
2942
2956
@sock = SSLSocket . new ( @sock , ssl_ctx )
2943
2957
@sock . sync_close = true
2944
2958
@sock . hostname = @host if @sock . respond_to? :hostname=
2945
- ssl_socket_connect ( @sock , @ open_timeout)
2959
+ ssl_socket_connect ( @sock , open_timeout )
2946
2960
if ssl_ctx . verify_mode != VERIFY_NONE
2947
2961
@sock . post_connection_check ( @host )
2948
2962
@tls_verified = true
0 commit comments