@@ -235,8 +235,9 @@ module Net
235
235
# Use paginated or limited versions of commands whenever possible.
236
236
#
237
237
# Use #add_response_handler to handle responses after each one is received.
238
- # Use #extract_responses, #clear_responses, or #responses (with a block) to
239
- # prune responses.
238
+ # The +response_handlers+ argument to ::new assigns response handlers before
239
+ # the receiver thread is started. Use #extract_responses, #clear_responses,
240
+ # or #responses (with a block) to prune responses.
240
241
#
241
242
# == Errors
242
243
#
@@ -961,6 +962,12 @@ def idle_response_timeout; config.idle_response_timeout end
961
962
#
962
963
# See DeprecatedClientOptions.new for deprecated SSL arguments.
963
964
#
965
+ # [response_handlers]
966
+ # A list of response handlers to be added before the receiver thread is
967
+ # started. This ensures every server response is handled, including the
968
+ # #greeting. Note that the greeting is handled in the current thread, but
969
+ # all other responses are handled in the receiver thread.
970
+ #
964
971
# [config]
965
972
# A Net::IMAP::Config object to use as the basis for #config. By default,
966
973
# the global Net::IMAP.config is used.
@@ -1032,7 +1039,7 @@ def idle_response_timeout; config.idle_response_timeout end
1032
1039
# [Net::IMAP::ByeResponseError]
1033
1040
# Connected to the host successfully, but it immediately said goodbye.
1034
1041
#
1035
- def initialize ( host , port : nil , ssl : nil ,
1042
+ def initialize ( host , port : nil , ssl : nil , response_handlers : nil ,
1036
1043
config : Config . global , **config_options )
1037
1044
super ( )
1038
1045
# Config options
@@ -1057,6 +1064,7 @@ def initialize(host, port: nil, ssl: nil,
1057
1064
@receiver_thread = nil
1058
1065
@receiver_thread_exception = nil
1059
1066
@receiver_thread_terminating = false
1067
+ response_handlers &.each do add_response_handler ( _1 ) end
1060
1068
1061
1069
# Client Protocol Sender (including state for currently running commands)
1062
1070
@tag_prefix = "RUBY"
@@ -3255,6 +3263,10 @@ def response_handlers
3255
3263
# end
3256
3264
# }
3257
3265
#
3266
+ # Response handlers can also be added when the client is created before the
3267
+ # receiver thread is started, by the +response_handlers+ argument to ::new.
3268
+ # This ensures every server response is handled, including the #greeting.
3269
+ #
3258
3270
# Related: #remove_response_handler, #response_handlers
3259
3271
def add_response_handler ( handler = nil , &block )
3260
3272
raise ArgumentError , "two Procs are passed" if handler && block
@@ -3281,6 +3293,7 @@ def remove_response_handler(handler)
3281
3293
def start_imap_connection
3282
3294
@greeting = get_server_greeting
3283
3295
@capabilities = capabilities_from_resp_code @greeting
3296
+ @response_handlers . each do |handler | handler . call ( @greeting ) end
3284
3297
@receiver_thread = start_receiver_thread
3285
3298
rescue Exception
3286
3299
state_logout!
0 commit comments