Skip to content

Commit 460f3e0

Browse files
committed
Update initialize, start, etc to use kwargs
* update `POP3.foreach`, `POP3.delete_all`, and `POP3.auth_only` to forward all arguments (`*arg, **kwarg`) to `POP3.start`. * update `auth_only` to *only* forward args to `start` and return `true` * marked `POP3.create_ssl_params` with `:nodoc:` to discourage its use. * update `#initialize`, `.start`, `#start`, etc to allow kwargs for `port` and `apop`, * add the ability to set the basic configuration parameters to `#initialize`: `ssl`, `open_timeout`, `read_timeout`, and `debug_output`. * add `auth` keyword param to `start`, with the ability to forward it as keyword params into the `#auth` kw args
1 parent e8d0afe commit 460f3e0

File tree

1 file changed

+75
-42
lines changed

1 file changed

+75
-42
lines changed

lib/net/pop.rb

Lines changed: 75 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,12 @@ def POP3.APOP(isapop)
241241

242242
# Starts a POP3 session and iterates over each POPMail object,
243243
# yielding it to the +block+.
244-
# This method is equivalent to:
245244
#
246-
# Net::POP3.start(address, port, account, password) do |pop|
247-
# pop.each_mail do |m|
248-
# yield m
245+
# All other arguments are forwarded to Net::POP3.start, equivalent to:
246+
#
247+
# Net::POP3.start(*args, **kwargs) do |pop|
248+
# pop.each_mail do |mail|
249+
# yield mail
249250
# end
250251
# end
251252
#
@@ -259,36 +260,33 @@ def POP3.APOP(isapop)
259260
# m.delete if $DELETE
260261
# end
261262
#
262-
def POP3.foreach(address, port = nil,
263-
account = nil, password = nil,
264-
isapop = false, &block) # :yields: message
265-
start(address, port, account, password, isapop) {|pop|
263+
def POP3.foreach(*args, **kwargs, &block) # :yields: message
264+
start(*args, **kwargs) {|pop|
266265
pop.each_mail(&block)
267266
}
268267
end
269268

270269
# Starts a POP3 session and deletes all messages on the server.
271270
# If a block is given, each POPMail object is yielded to it before
272-
# being deleted.
271+
# being deleted. All other arguments are forwarded to Net::POP3.start.
273272
#
274273
# This method raises a POPAuthenticationError if authentication fails.
275274
#
276275
# === Example
277276
#
278277
# Net::POP3.delete_all('pop.example.com', 110,
279-
# 'YourAccount', 'YourPassword') do |m|
280-
# file.write m.pop
278+
# 'YourAccount', 'YourPassword') do |mail|
279+
# file.write mail.pop
281280
# end
282281
#
283-
def POP3.delete_all(address, port = nil,
284-
account = nil, password = nil,
285-
isapop = false, &block)
286-
start(address, port, account, password, isapop) {|pop|
282+
def POP3.delete_all(*args, **kwargs, &block) # :yields: message
283+
start(*args, **kwargs) {|pop|
287284
pop.delete_all(&block)
288285
}
289286
end
290287

291288
# Opens a POP3 session, attempts authentication, and quits.
289+
# All arguments are forwarded to ::start.
292290
#
293291
# This method raises POPAuthenticationError if authentication fails.
294292
#
@@ -302,20 +300,18 @@ def POP3.delete_all(address, port = nil,
302300
# Net::POP3.auth_only('pop.example.com', 110,
303301
# 'YourAccount', 'YourPassword', true)
304302
#
305-
def POP3.auth_only(address, port = nil,
306-
account = nil, password = nil,
307-
isapop = false)
308-
new(address, port, isapop).auth_only account, password
303+
def POP3.auth_only(*args, **kwargs)
304+
start(*args, **kwargs) { true }
309305
end
310306

311307
# Starts a pop3 session, attempts authentication, and quits.
312308
# This method must not be called while POP3 session is opened.
313309
# This method raises POPAuthenticationError if authentication fails.
314-
def auth_only(account, password)
310+
#
311+
# All arguments are forwarded to #start. See that method for details.
312+
def auth_only(*args, **kwargs)
315313
raise IOError, 'opening previously opened POP session' if started?
316-
start(account, password) {
317-
;
318-
}
314+
start(*args, **kwargs) { true }
319315
end
320316

321317
#
@@ -333,8 +329,7 @@ def POP3.enable_ssl(*args)
333329
@ssl_params = create_ssl_params(*args)
334330
end
335331

336-
# Constructs proper parameters from arguments
337-
def POP3.create_ssl_params(verify_or_params = {}, certs = nil)
332+
def POP3.create_ssl_params(verify_or_params = {}, certs = nil) # :nodoc:
338333
begin
339334
params = verify_or_params.to_hash
340335
rescue NoMethodError
@@ -382,9 +377,16 @@ def POP3.certs
382377
# Session management
383378
#
384379

380+
# :call-seq:
381+
# Net::POP3.start(address, port = nil, user = nil, pass = nil, auth: {}, **opts) -> pop
382+
# Net::POP3.start(address, port: nil, user: nil, pass: nil, auth: {}, **opts) -> pop
383+
# Net::POP3.start(address, port = nil, user = nil, pass = nil, auth: {}, **opts) {|pop| }
384+
# Net::POP3.start(address, port: nil, user: nil, pass: nil, auth: {}, **opts) {|pop| }
385+
#
385386
# Creates a new POP3 object and open the connection. Equivalent to
386387
#
387-
# Net::POP3.new(address, port, isapop).start(account, password)
388+
# Net::POP3.new(address, port, **opts)
389+
# .start(user, pass, **auth, &block)
388390
#
389391
# If +block+ is provided, yields the newly-opened POP3 object to it,
390392
# and automatically closes it at the end of the session.
@@ -400,32 +402,63 @@ def POP3.certs
400402
#
401403
def POP3.start(address, port = nil,
402404
account = nil, password = nil,
403-
isapop = false, &block) # :yield: pop
404-
new(address, port, isapop).start(account, password, &block)
405+
isapop = false,
406+
user: nil, pass: nil, auth: {},
407+
**kwargs, &block) # :yield: pop
408+
new(address, port, isapop, **kwargs)
409+
.start(user || account, pass || password, **auth, &block)
405410
end
406411

412+
# :call-seq:
413+
# new(addr, port = nil, **options)
414+
# new(addr, port: nil, ssl: POP3.ssl_params, apop: false,
415+
# read_timeout: 60, open_timeout: 30, debug_output: nil)
416+
#
407417
# Creates a new POP3 object.
408418
#
409419
# +address+ is the hostname or ip address of your POP3 server.
410420
#
411-
# The optional +port+ is the port to connect to.
412-
#
413-
# The optional +isapop+ specifies whether this connection is going
414-
# to use APOP authentication; it defaults to +false+.
415-
#
416-
# This method does *not* open the TCP connection.
417-
def initialize(addr, port = nil, isapop = false)
421+
# The optional +port+ is the port to connect to. The default port will be
422+
# chosen based on whether or not +ssl+ is used.
423+
#
424+
# The optional +ssl+ should be true to connect using TLS. When +tls+ is a
425+
# hash, it is used to set parameters with
426+
# {OpenSSL::SSL::SSLContext#set_params}[https://docs.ruby-lang.org/en/master/OpenSSL/SSL/SSLContext.html#method-i-set_params].
427+
#
428+
# The +open_timeout+ and +read_timeout+ options will set their
429+
# respective IO timeouts on the socket.
430+
#
431+
# See #set_debug_output for the use of +debug_output.
432+
#
433+
# The optional +apop+ specifies whether this connection is going
434+
# to use APOP authentication; it defaults to +false+. <em>Warning: APOP is
435+
# based on MD5 and it is insecure, obsolete, and should be avoided.</em>
436+
#
437+
# This method does *not* open the TCP connection. See #start.
438+
def initialize(addr, port_ = nil, isapop = false,
439+
port: nil,
440+
ssl: POP3.ssl_params,
441+
apop: false,
442+
read_timeout: 60,
443+
open_timeout: 30,
444+
debug_output: nil)
445+
if port_.respond_to?(:to_hash)
446+
return initialize nil, false, **port_
447+
elsif isapop.respond_to?(:to_hash)
448+
return initialize port_, false, **isapop
449+
end
418450
@address = addr
419-
@ssl_params = POP3.ssl_params
420-
@port = port
421-
@apop = isapop
451+
@ssl_params = Hash.try_convert(ssl).dup
452+
@ssl_params ||= {} if ssl
453+
@port = port || port_
454+
@apop = apop || isapop
455+
@open_timeout = open_timeout
456+
@read_timeout = read_timeout
457+
@debug_output = debug_output
422458

423459
@command = nil
424460
@socket = nil
425461
@started = false
426-
@open_timeout = 30
427-
@read_timeout = 60
428-
@debug_output = nil
429462

430463
@mails = nil
431464
@n_mails = nil
@@ -459,7 +492,7 @@ def enable_ssl(verify_or_params = {}, certs = nil, port = nil)
459492
end
460493
end
461494

462-
# Disable SSL for all new instances.
495+
# Disable SSL for this instance.
463496
def disable_ssl
464497
@ssl_params = nil
465498
end

0 commit comments

Comments
 (0)