From 6be96eabeee6299edf9665a34ec4eb35c5dee112 Mon Sep 17 00:00:00 2001 From: nick evans Date: Wed, 11 Oct 2023 13:46:20 -0400 Subject: [PATCH] Do not require positional args for `#authenticate` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This API is a little bit confusing, IMO. But it does preserve backward compatibility, while allowing authenticators that don't allow positional parameters to work without crashing. But, authenticators that require only one parameter—or more than three—will still be inaccessible. --- lib/net/smtp.rb | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/net/smtp.rb b/lib/net/smtp.rb index d600446..917bc5e 100644 --- a/lib/net/smtp.rb +++ b/lib/net/smtp.rb @@ -878,6 +878,10 @@ def open_message_stream(from_addr, *to_addrs, &block) # :yield: stream DEFAULT_AUTH_TYPE = :plain + # call-seq: + # authenticate(authtype = DEFAULT_AUTH_TYPE, **, &) + # authenticate(username, secret, authtype = DEFAULT_AUTH_TYPE, **, &) + # # Authenticates with the server, using the "AUTH" command. # # +authtype+ is the name of a SASL authentication mechanism. @@ -885,10 +889,17 @@ def open_message_stream(from_addr, *to_addrs, &block) # :yield: stream # All arguments—other than +authtype+—are forwarded to the authenticator. # Different authenticators may interpret the +username+ and +secret+ # arguments differently. - def authenticate(username, secret, authtype = DEFAULT_AUTH_TYPE, **kwargs, &block) - check_auth_args authtype, username, secret, **kwargs + def authenticate(*args, **kwargs, &block) + case args.length + when 1, 3 then authtype = args.pop + when (4..) + raise ArgumentError, "wrong number of arguments " \ + "(given %d, expected 0..3)" % [args.length] + end + authtype ||= DEFAULT_AUTH_TYPE + check_auth_args authtype, *args, **kwargs authenticator = Authenticator.auth_class(authtype).new(self) - authenticator.auth(username, secret, **kwargs, &block) + authenticator.auth(*args, **kwargs, &block) end private