Subject: Re: bug#60428: 29.0.60; ERC 5.4.1: Fix default-port regression in erc-select-read-args

From: "J.P." <[email-masked]>
Date: Sun, 01 Jan 2023 17:41:12 -0800

v2. Respect `erc-prompt-for-password'. Remove `run-at-time' hack from
regression fix.

"J.P." <[email-masked]> writes:

> The proposed patch abandons most of the envisioned niceties and
> instead employs a hack to just force a non-local exit and schedule a
> call to `erc-tls' when appropriate. It also instructs users to do that
> themselves in the future (issue an M-x erc-tls, that is). Hopefully,
> this makes for an acceptable compromise.

I've forsaken the first part out of an abundance of caution. So this now
just prints a warning and proceeds with a non-TLS connection. If anyone
finds that unsatisfactory, please say something.

> BTW, a POC patch set far more faithful to the original breadth of the
> feature (and way less hacky) is also attached. Perhaps it'll inform
> further exploration in this area down the road (but not for Emacs 29).

IOW, the de facto goal here is to make interactive `erc' a "universal"
entry point for both plain and TLS connections.

>From 448686bf96105833cd3e3ee319118c1672d95ca0 Mon Sep 17 00:00:00 2001
From: "F. Jason Park" <[email-masked]>
Date: Sun, 1 Jan 2023 17:36:36 -0800
Subject: [PATCH 0/4] *** NOT A PATCH ***

*** BLURB HERE ***

F. Jason Park (4):
  Fix default-port regression in erc-select-read-args
  [5.6] Be smarter about switching to TLS from M-x erc
  [5.6] Add display option for interactive ERC invocations
  [5.6] Optionally prompt for more ERC entry-point params

 doc/misc/erc.texi          |  2 +-
 lisp/erc/erc.el            | 87 +++++++++++++++++++++++++++++---------
 test/lisp/erc/erc-tests.el | 66 +++++++++++++++++++++--------
 3 files changed, 118 insertions(+), 37 deletions(-)

Interdiff:
diff --git a/lisp/erc/erc.el b/lisp/erc/erc.el
index 2fcdb513b5d..7f2dd89342b 100644
--- a/lisp/erc/erc.el
+++ b/lisp/erc/erc.el
@@ -1486,7 +1486,8 @@ erc-interactive-display
 possible values."
   :package-version '(ERC . "5.4.1") ; FIXME increment upon publishing to ELPA
   :group 'erc-buffers
-  :type '(choice (const :tag "Split window and select" window)
+  :type '(choice (const :tag "Use value of `erc-join-buffer'" nil)
+                 (const :tag "Split window and select" window)
                  (const :tag "Split window, don't select" window-noselect)
                  (const :tag "New frame" frame)
                  (const :tag "Bury new and don't display existing" bury)
@@ -2180,7 +2181,7 @@ erc-select-read-args
 With prefix arg, also prompt for user and full name."
   (require 'url-parse)
   (let* ((input (let ((d (erc-compute-server)))
-                  (read-string (format "Server (default is %S): " d)
+                  (read-string (format "Server or URL (default is %S): " d)
                                nil 'erc-server-history-list d)))
          ;; For legacy reasons, also accept a URL without a scheme.
          (url (url-generic-parse-url (erc--ensure-url input)))
@@ -2199,34 +2200,33 @@ erc-select-read-args
                      (read-string (format "Nickname (default is %S): " d)
                                   nil 'erc-nick-history-list d))))
          (user (and current-prefix-arg
-                    (let ((d (or (url-user url) (erc-compute-user))))
+                    (let ((d (erc-compute-user (url-user url))))
                       (read-string (format "User (default is %S): " d)
                                    nil nil d))))
          (full (and current-prefix-arg
-                    (let ((d (or (url-user url) (erc-compute-full-name))))
+                    (let ((d (erc-compute-full-name (url-user url))))
                       (read-string (format "Full name (default is %S): " d)
                                    nil nil d))))
-         (passwd (or (url-password url)
-                     (let*
-                         ((p (with-suppressed-warnings
-                                 ((obsolete erc-password))
-                               erc-password))
-                          (m (if p
-                                 (format "Server password (default is %S): " p)
-                               "Server password (optional): ")))
-                       (if erc-prompt-for-password (read-passwd m nil p) p))))
-         (opener erc-server-connect-function))
+         (passwd (let* ((p (with-suppressed-warnings ((obsolete erc-password))
+                             (or (url-password url) erc-password)))
+                        (m (if p
+                               (format "Server password (default is %S): " p)
+                             "Server password (optional): ")))
+                   (if erc-prompt-for-password (read-passwd m nil p) p)))
+         (opener (and (or sp (eql port erc-default-port-tls)
+                          (and (equal server erc-default-server)
+                               (not (string-prefix-p "irc://" input))
+                               (eql port erc-default-port)
+                               (y-or-n-p "Connect using TLS instead? ")
+                               (setq port erc-default-port-tls)))
+                      #'erc-open-tls-stream)))
     (when (and passwd (string= "" passwd))
       (setq passwd nil))
-    (when (and (equal server erc-default-server)
-               (not (string-prefix-p "irc://" input))
-               (or (not (eql port erc-default-port))
-                   (and (y-or-n-p "Connect using TLS instead? ")
-                        (setq port erc-default-port-tls))))
-      (setq opener #'erc-open-tls-stream))
     `( :server ,server :port ,port :nick ,nick ,@(and user `(:user ,user))
        ,@(and passwd `(:password ,passwd)) ,@(and full `(:full-name ,full))
-       buffer-display ,erc-interactive-display connect-function ,opener)))
+       ,@(and erc-interactive-display
+              `(buffer-display ,erc-interactive-display))
+       ,@(and opener `(connect-function ,opener)))))

 ;;;###autoload
@@ -2238,8 +2238,7 @@ erc
                     (full-name (erc-compute-full-name))
                     id
                     ;; For interactive use
-                    ((buffer-display erc-buffer-display)
-                     erc-buffer-display)
+                    ((buffer-display erc-join-buffer) erc-join-buffer)
                     ((connect-function erc-server-connect-function)
                      erc-server-connect-function))
   "ERC is a powerful, modular, and extensible IRC client.
@@ -2284,10 +2283,12 @@ erc-tls
                         client-certificate
                         id
                         ;; For interactive use
-                        ((buffer-display erc-buffer-display)
-                         erc-buffer-display)
+                        ((buffer-display erc-join-buffer) erc-join-buffer)
                         ((connect-function erc-server-connect-function)
-                         #'erc-open-tls-stream))
+                         (if (eq erc-server-connect-function
+                                 #'erc-open-network-stream)
+                             #'erc-open-tls-stream
+                           erc-server-connect-function)))
   "ERC is a powerful, modular, and extensible IRC client.
 This function is the main entry point for ERC over TLS.
 
@@ -2334,12 +2335,7 @@ erc-tls
 interactively.
 
 \(fn &key SERVER PORT NICK USER PASSWORD FULL-NAME CLIENT-CERTIFICATE ID)"
-  (interactive (let ((erc-server-connect-function
-                      (if (eq erc-server-connect-function
-                              #'erc-open-network-stream)
-                          #'erc-open-tls-stream
-                        erc-server-connect-function))
-                     (erc-port (or erc-port erc-default-port-tls)))
+  (interactive (let ((erc-default-port erc-default-port-tls))
 		 (erc-select-read-args)))
   (progn
     (erc-open server port nick full-name t password
diff --git a/test/lisp/erc/erc-tests.el b/test/lisp/erc/erc-tests.el
index 9d09df44883..d05b0efe2ed 100644
--- a/test/lisp/erc/erc-tests.el
+++ b/test/lisp/erc/erc-tests.el
@@ -1001,7 +1001,7 @@ erc--server-connect-dumb-ipv6-regexp
 
 (ert-deftest erc-select-read-args ()
 
-  (ert-info ("Doesn't default to TLS")
+  (ert-info ("Prompts for switch to TLS by default")
     (should (equal (ert-simulate-keys "\r\r\r\ry\r"
                      (erc-select-read-args))
                    (list :server "irc.libera.chat"
@@ -1010,44 +1010,58 @@ erc-select-read-args
                          'buffer-display 'buffer
                          'connect-function #'erc-open-tls-stream))))
 
+  (ert-info ("Switches to TLS when port matches default TLS port")
+    (should (equal (ert-simulate-keys "irc.gnu.org\r6697\r\r\r"
+                     (erc-select-read-args))
+                   (list :server "irc.gnu.org"
+                         :port 6697
+                         :nick (user-login-name)
+                         'buffer-display 'buffer
+                         'connect-function #'erc-open-tls-stream))))
+
+  (ert-info ("Switches to TLS when URL is ircs://")
+    (should (equal (ert-simulate-keys "ircs://irc.gnu.org\r\r\r\r"
+                     (erc-select-read-args))
+                   (list :server "irc.gnu.org"
+                         :port 6697
+                         :nick (user-login-name)
+                         'buffer-display 'buffer
+                         'connect-function #'erc-open-tls-stream))))
+
+  (setq-local erc-interactive-display nil) ; cheat to save space
+
   (ert-info ("Opt out of non-TLS warning manually")
     (should (equal (ert-simulate-keys "\r\r\r\rn\r"
                      (erc-select-read-args))
                    (list :server "irc.libera.chat"
                          :port 6667
-                         :nick (user-login-name)
-                         'buffer-display 'buffer
-                         'connect-function #'erc-open-network-stream))))
+                         :nick (user-login-name)))))
 
-  (ert-info ("Override non-TLS warning via URL scheme")
+  (ert-info ("Override default TLS")
     (should (equal (ert-simulate-keys "irc://irc.libera.chat\r\r\r\r"
                      (erc-select-read-args))
                    (list :server "irc.libera.chat"
                          :port 6667
-                         :nick (user-login-name)
-                         'buffer-display 'buffer
-                         'connect-function #'erc-open-network-stream))))
+                         :nick (user-login-name)))))
 
   (ert-info ("Address includes port")
     (should (equal (ert-simulate-keys "localhost:6667\rnick\r\r"
                      (erc-select-read-args))
                    (list :server "localhost"
                          :port 6667
-                         :nick "nick"
-                         'buffer-display 'buffer
-                         'connect-function #'erc-open-network-stream))))
+                         :nick "nick"))))
 
   (ert-info ("Address includes nick, password skipped via option")
     (should (equal (ert-simulate-keys "nick@localhost:6667\r"
                      (let (erc-prompt-for-password)
-                       (butlast (erc-select-read-args) 4)))
+                       (erc-select-read-args)))
                    (list :server "localhost"
                          :port 6667
                          :nick "nick"))))
 
   (ert-info ("Address includes nick and password")
-    (should (equal (ert-simulate-keys "nick:sesame@localhost:6667\r"
-                     (butlast (erc-select-read-args) 4))
+    (should (equal (ert-simulate-keys "nick:sesame@localhost:6667\r\r"
+                     (erc-select-read-args))
                    (list :server "localhost"
                          :port 6667
                          :nick "nick"
@@ -1055,29 +1069,29 @@ erc-select-read-args
 
   (ert-info ("IPv6 address plain")
     (should (equal (ert-simulate-keys "::1\r\r\r\r"
-                     (butlast (erc-select-read-args) 4))
+                     (erc-select-read-args))
                    (list :server "[::1]"
                          :port 6667
                          :nick (user-login-name)))))
 
   (ert-info ("IPv6 address with port")
     (should (equal (ert-simulate-keys "[::1]:6667\r\r\r"
-                     (butlast (erc-select-read-args) 4))
+                     (erc-select-read-args))
                    (list :server "[::1]"
                          :port 6667
                          :nick (user-login-name)))))
 
   (ert-info ("IPv6 address includes nick")
     (should (equal (ert-simulate-keys "nick@[::1]:6667\r\r"
-                     (butlast (erc-select-read-args) 4))
+                     (erc-select-read-args))
                    (list :server "[::1]"
                          :port 6667
                          :nick "nick"))))
 
   (ert-info ("Extra args use URL nick by default")
-    (should (equal (ert-simulate-keys "nick:sesame@localhost:6667\r\r\r"
+    (should (equal (ert-simulate-keys "nick:sesame@localhost:6667\r\r\r\r"
                      (let ((current-prefix-arg '(4)))
-                       (butlast (erc-select-read-args) 4)))
+                       (erc-select-read-args)))
                    (list :server "localhost"
                          :port 6667
                          :nick "nick"
-- 
2.38.1

>From 574671c8bdc1cb5a56b0c4ffe0f35be9fa0161f7 Mon Sep 17 00:00:00 2001
From: "F. Jason Park" <[email-masked]>
Date: Thu, 29 Dec 2022 06:43:19 -0800
Subject: [PATCH 1/4] Fix default-port regression in erc-select-read-args

* lisp/erc/erc.el (erc--warn-unencrypted): New function, likely
temporary, to warn new users connecting interactively to the default
server, "irc.libara.chat", via the default non-TLS port, 6667.
(erc-select-read-args): Remove stray code from incomplete feature
introduced by bug#56514.  Ensure connecting always works with default
port, which is non-TLS.  Respect `erc-prompt-for-password' when user
pastes URL containing password component into "server" prompt.  Maybe
add `erc--warn-unencrypted' as one-off hook for impending connection.
* test/lisp/erc/erc-tests.el (erc-select-read-args): Always expect
password prompt and sometimes a non-TLS port when `erc' called
interactively.  (Bug#60428.)
---
 lisp/erc/erc.el            | 38 +++++++++++++++++++++++++++++---------
 test/lisp/erc/erc-tests.el |  6 +++---
 2 files changed, 32 insertions(+), 12 deletions(-)

diff --git a/lisp/erc/erc.el b/lisp/erc/erc.el
index fe1201dd3a8..6315d5aa482 100644
--- a/lisp/erc/erc.el
+++ b/lisp/erc/erc.el
@@ -2161,6 +2161,23 @@ erc--ensure-url
     (setq input (concat "irc://" input)))
   input)
 
+;; A temporary means of addressing the problem of ERC's namesake entry
+;; point defaulting to a non-TLS connection with its default server
+;; (bug#60428).
+(defun erc--warn-unencrypted ()
+  ;; Remove unconditionally to avoid wrong context due to races from
+  ;; simultaneous dialing or aborting (e.g., via `keybaord-quit').
+  (remove-hook 'erc--server-post-connect-hook #'erc--warn-unencrypted)
+  (when (and (process-contact erc-server-process :nowait)
+             (equal erc-session-server erc-default-server)
+             (eql erc-session-port erc-default-port))
+    ;; FIXME use the autoloaded `info' instead of `Info-goto-node' in
+    ;; `erc-button-alist'.
+    (require 'info nil t)
+    (erc-display-error-notice
+     nil (concat "This connection is unencrypted.  Please use `erc-tls'"
+                 " from now on.  See Info:\"(erc) connecting\" for more."))))
+
 ;;;###autoload
 (defun erc-select-read-args ()
   "Prompt the user for values of nick, server, port, and password."
@@ -2171,10 +2188,7 @@ erc-select-read-args
          ;; For legacy reasons, also accept a URL without a scheme.
          (url (url-generic-parse-url (erc--ensure-url input)))
          (server (url-host url))
-         (sp (and (or (string-suffix-p "s" (url-type url))
-                      (and (equal server erc-default-server)
-                           (not (string-prefix-p "irc://" input))))
-                  'ircs-u))
+         (sp (and (string-suffix-p "s" (url-type url)) erc-default-port-tls))
          (port (or (url-portspec url)
                    (erc-compute-port
                     (let ((d (erc-compute-port sp))) ; may be a string
@@ -2187,13 +2201,19 @@ erc-select-read-args
                    (let ((d (erc-compute-nick)))
                      (read-string (format "Nickname (default is %S): " d)
                                   nil 'erc-nick-history-list d))))
-         (passwd (or (url-password url)
-                     (if erc-prompt-for-password
-                         (read-passwd "Server password (optional): ")
-                       (with-suppressed-warnings ((obsolete erc-password))
-                         erc-password)))))
+         (passwd (let* ((p (with-suppressed-warnings ((obsolete erc-password))
+                             (or (url-password url) erc-password)))
+                        (m (if p
+                               (format "Server password (default is %S): " p)
+                             "Server password (optional): ")))
+                   (if erc-prompt-for-password (read-passwd m nil p) p))))
     (when (and passwd (string= "" passwd))
       (setq passwd nil))
+    (when (and (equal server erc-default-server)
+               (eql port erc-default-port)
+               (not (eql port erc-default-port-tls)) ; not `erc-tls'
+               (not (string-prefix-p "irc://" input))) ; not yanked URL
+      (add-hook 'erc--server-post-connect-hook #'erc--warn-unencrypted))
     (list :server server :port port :nick nick :password passwd)))
 
 ;;;###autoload
diff --git a/test/lisp/erc/erc-tests.el b/test/lisp/erc/erc-tests.el
index 6807b24bfc6..85506c3d27e 100644
--- a/test/lisp/erc/erc-tests.el
+++ b/test/lisp/erc/erc-tests.el
@@ -1001,11 +1001,11 @@ erc--server-connect-dumb-ipv6-regexp
 
 (ert-deftest erc-select-read-args ()
 
-  (ert-info ("Defaults to TLS")
+  (ert-info ("Does not default to TLS")
     (should (equal (ert-simulate-keys "\r\r\r\r"
                      (erc-select-read-args))
                    (list :server "irc.libera.chat"
-                         :port 6697
+                         :port 6667
                          :nick (user-login-name)
                          :password nil))))
 
@@ -1036,7 +1036,7 @@ erc-select-read-args
                          :password nil))))
 
   (ert-info ("Address includes nick and password")
-    (should (equal (ert-simulate-keys "nick:sesame@localhost:6667\r"
+    (should (equal (ert-simulate-keys "nick:sesame@localhost:6667\r\r"
                      (erc-select-read-args))
                    (list :server "localhost"
                          :port 6667
-- 
2.38.1

>From 7a8659ed2132858e5c52cf09c9050cc00463607f Mon Sep 17 00:00:00 2001
From: "F. Jason Park" <[email-masked]>
Date: Thu, 29 Dec 2022 06:43:19 -0800
Subject: [PATCH 2/4] [5.6] Be smarter about switching to TLS from M-x erc

* lisp/erc/erc.el (erc--warn-unencrypted): Remove newly unused
internal function.
(erc-select-read-args): Offer to use TLS when user runs M-x erc and
opts for default server and port or provides the well-known IANA TLS
port or enters an ircs:// URL at the server prompt.  For the last two,
do this immediately instead of calling `erc-tls' interactively and
imposing a review of just-chosen values.  Also remove error warnings
and ensure `erc-tls' still works by setting
`erc-server-connect-function' to `erc-open-tls-stream' when
appropriate.  Include the word "URL" in server prompt.
(erc, erc-tls): Add internal keyword arguments for interactive use,
but don't make them colon-prefixed, i.e., `keywordp'.
* test/lisp/erc/erc-tests.el (erc-select-read-args): Modify return
values to expect additional `connect-function' keyword argument.
(Bug#60428.)
---
 lisp/erc/erc.el            | 61 +++++++++++++++++++-------------------
 test/lisp/erc/erc-tests.el | 52 +++++++++++++++++++++-----------
 2 files changed, 65 insertions(+), 48 deletions(-)

diff --git a/lisp/erc/erc.el b/lisp/erc/erc.el
index 6315d5aa482..96866432ebd 100644
--- a/lisp/erc/erc.el
+++ b/lisp/erc/erc.el
@@ -2161,29 +2161,12 @@ erc--ensure-url
     (setq input (concat "irc://" input)))
   input)
 
-;; A temporary means of addressing the problem of ERC's namesake entry
-;; point defaulting to a non-TLS connection with its default server
-;; (bug#60428).
-(defun erc--warn-unencrypted ()
-  ;; Remove unconditionally to avoid wrong context due to races from
-  ;; simultaneous dialing or aborting (e.g., via `keybaord-quit').
-  (remove-hook 'erc--server-post-connect-hook #'erc--warn-unencrypted)
-  (when (and (process-contact erc-server-process :nowait)
-             (equal erc-session-server erc-default-server)
-             (eql erc-session-port erc-default-port))
-    ;; FIXME use the autoloaded `info' instead of `Info-goto-node' in
-    ;; `erc-button-alist'.
-    (require 'info nil t)
-    (erc-display-error-notice
-     nil (concat "This connection is unencrypted.  Please use `erc-tls'"
-                 " from now on.  See Info:\"(erc) connecting\" for more."))))
-
 ;;;###autoload
 (defun erc-select-read-args ()
   "Prompt the user for values of nick, server, port, and password."
   (require 'url-parse)
   (let* ((input (let ((d (erc-compute-server)))
-                  (read-string (format "Server (default is %S): " d)
+                  (read-string (format "Server or URL (default is %S): " d)
                                nil 'erc-server-history-list d)))
          ;; For legacy reasons, also accept a URL without a scheme.
          (url (url-generic-parse-url (erc--ensure-url input)))
@@ -2206,15 +2189,20 @@ erc-select-read-args
                         (m (if p
                                (format "Server password (default is %S): " p)
                              "Server password (optional): ")))
-                   (if erc-prompt-for-password (read-passwd m nil p) p))))
+                   (if erc-prompt-for-password (read-passwd m nil p) p)))
+         (opener (and (or sp (eql port erc-default-port-tls)
+                          (and (equal server erc-default-server)
+                               (not (string-prefix-p "irc://" input))
+                               (eql port erc-default-port)
+                               (y-or-n-p "Connect using TLS instead? ")
+                               (setq port erc-default-port-tls)))
+                      #'erc-open-tls-stream)))
     (when (and passwd (string= "" passwd))
       (setq passwd nil))
-    (when (and (equal server erc-default-server)
-               (eql port erc-default-port)
-               (not (eql port erc-default-port-tls)) ; not `erc-tls'
-               (not (string-prefix-p "irc://" input))) ; not yanked URL
-      (add-hook 'erc--server-post-connect-hook #'erc--warn-unencrypted))
-    (list :server server :port port :nick nick :password passwd)))
+    `( :server ,server :port ,port :nick ,nick
+       ,@(and passwd `(:password ,passwd))
+       ,@(and opener `(connect-function ,opener)))))
+
 
 ;;;###autoload
 (cl-defun erc (&key (server (erc-compute-server))
@@ -2223,7 +2211,10 @@ erc
                     (user   (erc-compute-user))
                     password
                     (full-name (erc-compute-full-name))
-                    id)
+                    id
+                    ;; For interactive use
+                    ((connect-function erc-server-connect-function)
+                     erc-server-connect-function))
   "ERC is a powerful, modular, and extensible IRC client.
 This function is the main entry point for ERC.
 
@@ -2246,7 +2237,9 @@ erc
 whereas `erc-compute-port' and `erc-compute-nick' will be invoked
 for the values of the other parameters.
 
-See `erc-tls' for the meaning of ID."
+See `erc-tls' for the meaning of ID.
+
+\(fn &key SERVER PORT NICK USER PASSWORD FULL-NAME ID)"
   (interactive (erc-select-read-args))
   (erc-open server port nick full-name t password nil nil nil nil user id))
 
@@ -2262,7 +2255,13 @@ erc-tls
                         password
                         (full-name (erc-compute-full-name))
                         client-certificate
-                        id)
+                        id
+                        ;; For interactive use
+                        ((connect-function erc-server-connect-function)
+                         (if (eq erc-server-connect-function
+                                 #'erc-open-network-stream)
+                             #'erc-open-tls-stream
+                           erc-server-connect-function)))
   "ERC is a powerful, modular, and extensible IRC client.
 This function is the main entry point for ERC over TLS.
 
@@ -2306,10 +2305,12 @@ erc-tls
 the server buffer and identifying the connection unequivocally.
 See info node `(erc) Network Identifier' for details.  Like USER
 and CLIENT-CERTIFICATE, this parameter cannot be specified
-interactively."
+interactively.
+
+\(fn &key SERVER PORT NICK USER PASSWORD FULL-NAME CLIENT-CERTIFICATE ID)"
   (interactive (let ((erc-default-port erc-default-port-tls))
 		 (erc-select-read-args)))
-  (let ((erc-server-connect-function 'erc-open-tls-stream))
+  (progn
     (erc-open server port nick full-name t password
               nil nil nil client-certificate user id)))
 
diff --git a/test/lisp/erc/erc-tests.el b/test/lisp/erc/erc-tests.el
index 85506c3d27e..10adc9d53a7 100644
--- a/test/lisp/erc/erc-tests.el
+++ b/test/lisp/erc/erc-tests.el
@@ -1001,30 +1001,50 @@ erc--server-connect-dumb-ipv6-regexp
 
 (ert-deftest erc-select-read-args ()
 
-  (ert-info ("Does not default to TLS")
-    (should (equal (ert-simulate-keys "\r\r\r\r"
+  (ert-info ("Prompts for switch to TLS by default")
+    (should (equal (ert-simulate-keys "\r\r\r\ry\r"
                      (erc-select-read-args))
                    (list :server "irc.libera.chat"
-                         :port 6667
+                         :port 6697
                          :nick (user-login-name)
-                         :password nil))))
+                         'connect-function #'erc-open-tls-stream))))
+
+  (ert-info ("Switches to TLS when port matches default TLS port")
+    (should (equal (ert-simulate-keys "irc.gnu.org\r6697\r\r\r"
+                     (erc-select-read-args))
+                   (list :server "irc.gnu.org"
+                         :port 6697
+                         :nick (user-login-name)
+                         'connect-function #'erc-open-tls-stream))))
+
+  (ert-info ("Switches to TLS when URL is ircs://")
+    (should (equal (ert-simulate-keys "ircs://irc.gnu.org\r\r\r\r"
+                     (erc-select-read-args))
+                   (list :server "irc.gnu.org"
+                         :port 6697
+                         :nick (user-login-name)
+                         'connect-function #'erc-open-tls-stream))))
+
+  (ert-info ("Opt out of non-TLS warning manually")
+    (should (equal (ert-simulate-keys "\r\r\r\rn\r"
+                     (erc-select-read-args))
+                   (list :server "irc.libera.chat"
+                         :port 6667
+                         :nick (user-login-name)))))
 
   (ert-info ("Override default TLS")
     (should (equal (ert-simulate-keys "irc://irc.libera.chat\r\r\r\r"
                      (erc-select-read-args))
                    (list :server "irc.libera.chat"
                          :port 6667
-                         :nick (user-login-name)
-                         :password nil))))
+                         :nick (user-login-name)))))
 
   (ert-info ("Address includes port")
-    (should (equal (ert-simulate-keys
-                       "localhost:6667\rnick\r\r"
+    (should (equal (ert-simulate-keys "localhost:6667\rnick\r\r"
                      (erc-select-read-args))
                    (list :server "localhost"
                          :port 6667
-                         :nick "nick"
-                         :password nil))))
+                         :nick "nick"))))
 
   (ert-info ("Address includes nick, password skipped via option")
     (should (equal (ert-simulate-keys "nick@localhost:6667\r"
@@ -1032,8 +1052,7 @@ erc-select-read-args
                        (erc-select-read-args)))
                    (list :server "localhost"
                          :port 6667
-                         :nick "nick"
-                         :password nil))))
+                         :nick "nick"))))
 
   (ert-info ("Address includes nick and password")
     (should (equal (ert-simulate-keys "nick:sesame@localhost:6667\r\r"
@@ -1048,24 +1067,21 @@ erc-select-read-args
                      (erc-select-read-args))
                    (list :server "[::1]"
                          :port 6667
-                         :nick (user-login-name)
-                         :password nil))))
+                         :nick (user-login-name)))))
 
   (ert-info ("IPv6 address with port")
     (should (equal (ert-simulate-keys "[::1]:6667\r\r\r"
                      (erc-select-read-args))
                    (list :server "[::1]"
                          :port 6667
-                         :nick (user-login-name)
-                         :password nil))))
+                         :nick (user-login-name)))))
 
   (ert-info ("IPv6 address includes nick")
     (should (equal (ert-simulate-keys "nick@[::1]:6667\r\r"
                      (erc-select-read-args))
                    (list :server "[::1]"
                          :port 6667
-                         :nick "nick"
-                         :password nil)))))
+                         :nick "nick")))))
 
 (ert-deftest erc-tls ()
   (let (calls)
-- 
2.38.1

>From 5afc6f4d40af031258bcda8fe440448ce8a1aa01 Mon Sep 17 00:00:00 2001
From: "F. Jason Park" <[email-masked]>
Date: Thu, 29 Dec 2022 06:43:19 -0800
Subject: [PATCH 3/4] [5.6] Add display option for interactive ERC invocations

* lisp/erc/erc.el (erc-buffer-display, erc-receive-query-display):
Add aliases for `erc-join-buffer' and `erc-auto-query'.
(erc-interactive-display): Add new option to control display of server
buffers during interactive entry-point invocations.
(erc-select-read-args): Pass `erc-interactive-display' to entry
points.
(erc, erc-tls): Add interactive-only to control initial buffer
display.
* test/lisp/erc/erc-tests.el (erc-select-read-args): Expect
buffer-display values from `erc-interactive-display'.  (Bug#60428.)
---
 lisp/erc/erc.el            | 19 +++++++++++++++++++
 test/lisp/erc/erc-tests.el |  5 +++++
 2 files changed, 24 insertions(+)

diff --git a/lisp/erc/erc.el b/lisp/erc/erc.el
index 96866432ebd..09d55f60990 100644
--- a/lisp/erc/erc.el
+++ b/lisp/erc/erc.el
@@ -1459,6 +1459,7 @@ erc-default-port-tls
   "IRC port to use for encrypted connections if it cannot be \
 detected otherwise.")
 
+(defvaralias 'erc-buffer-display 'erc-join-buffer)
 (defcustom erc-join-buffer 'bury
   "Determines how to display a newly created IRC buffer.
 
@@ -1479,6 +1480,19 @@ erc-join-buffer
                  (const :tag "Use current buffer" buffer)
                  (const :tag "Use current buffer" t)))
 
+(defcustom erc-interactive-display 'buffer
+  "How and whether to display server buffers for M-x erc.
+See `erc-buffer-display' and friends for a description of
+possible values."
+  :package-version '(ERC . "5.4.1") ; FIXME increment upon publishing to ELPA
+  :group 'erc-buffers
+  :type '(choice (const :tag "Use value of `erc-join-buffer'" nil)
+                 (const :tag "Split window and select" window)
+                 (const :tag "Split window, don't select" window-noselect)
+                 (const :tag "New frame" frame)
+                 (const :tag "Bury new and don't display existing" bury)
+                 (const :tag "Use current buffer" buffer)))
+
 (defcustom erc-reconnect-display nil
   "How (and whether) to display a channel buffer upon reconnecting.
 
@@ -2201,6 +2215,8 @@ erc-select-read-args
       (setq passwd nil))
     `( :server ,server :port ,port :nick ,nick
        ,@(and passwd `(:password ,passwd))
+       ,@(and erc-interactive-display
+              `(buffer-display ,erc-interactive-display))
        ,@(and opener `(connect-function ,opener)))))

@@ -2213,6 +2229,7 @@ erc
                     (full-name (erc-compute-full-name))
                     id
                     ;; For interactive use
+                    ((buffer-display erc-join-buffer) erc-join-buffer)
                     ((connect-function erc-server-connect-function)
                      erc-server-connect-function))
   "ERC is a powerful, modular, and extensible IRC client.
@@ -2257,6 +2274,7 @@ erc-tls
                         client-certificate
                         id
                         ;; For interactive use
+                        ((buffer-display erc-join-buffer) erc-join-buffer)
                         ((connect-function erc-server-connect-function)
                          (if (eq erc-server-connect-function
                                  #'erc-open-network-stream)
@@ -4492,6 +4510,7 @@ erc-query
   (with-current-buffer server-buffer
     (erc--open-target target)))
 
+(defvaralias 'erc-receive-query-display 'erc-auto-query)
 (defcustom erc-auto-query 'window-noselect
   "If non-nil, create a query buffer each time you receive a private message.
 If the buffer doesn't already exist, it is created.
diff --git a/test/lisp/erc/erc-tests.el b/test/lisp/erc/erc-tests.el
index 10adc9d53a7..91cdf2f1770 100644
--- a/test/lisp/erc/erc-tests.el
+++ b/test/lisp/erc/erc-tests.el
@@ -1007,6 +1007,7 @@ erc-select-read-args
                    (list :server "irc.libera.chat"
                          :port 6697
                          :nick (user-login-name)
+                         'buffer-display 'buffer
                          'connect-function #'erc-open-tls-stream))))
 
   (ert-info ("Switches to TLS when port matches default TLS port")
@@ -1015,6 +1016,7 @@ erc-select-read-args
                    (list :server "irc.gnu.org"
                          :port 6697
                          :nick (user-login-name)
+                         'buffer-display 'buffer
                          'connect-function #'erc-open-tls-stream))))
 
   (ert-info ("Switches to TLS when URL is ircs://")
@@ -1023,8 +1025,11 @@ erc-select-read-args
                    (list :server "irc.gnu.org"
                          :port 6697
                          :nick (user-login-name)
+                         'buffer-display 'buffer
                          'connect-function #'erc-open-tls-stream))))
 
+  (setq-local erc-interactive-display nil) ; cheat to save space
+
   (ert-info ("Opt out of non-TLS warning manually")
     (should (equal (ert-simulate-keys "\r\r\r\rn\r"
                      (erc-select-read-args))
-- 
2.38.1

>From 448686bf96105833cd3e3ee319118c1672d95ca0 Mon Sep 17 00:00:00 2001
From: "F. Jason Park" <[email-masked]>
Date: Thu, 29 Dec 2022 06:43:19 -0800
Subject: [PATCH 4/4] [5.6] Optionally prompt for more ERC entry-point params

* doc/misc/erc.texi: Update statement about availability of user param
when entry points called interactively.
* lisp/erc/erc.el (erc-select-read-args): Allow optionally calling
entry points with a prefix arg to access params `user' and
`:full-name'.
(erc-tls): Update doc string.
* test/lisp/erc/erc-tests.el (erc-select-read-args): Add test for
extra args.  (Bug#60428.)
---
 doc/misc/erc.texi          |  2 +-
 lisp/erc/erc.el            | 19 ++++++++++++++-----
 test/lisp/erc/erc-tests.el | 13 ++++++++++++-
 3 files changed, 27 insertions(+), 7 deletions(-)

diff --git a/doc/misc/erc.texi b/doc/misc/erc.texi
index 23cdcbff575..40ecd00b226 100644
--- a/doc/misc/erc.texi
+++ b/doc/misc/erc.texi
@@ -932,7 +932,7 @@ SASL
 your @samp{NickServ} password.  To make this work, customize
 @code{erc-sasl-user} and @code{erc-sasl-password} or specify the
 @code{:user} and @code{:password} keyword arguments when invoking
-@code{erc-tls}.  Note that @code{:user} cannot be given interactively.
+@code{erc-tls}.
 
 @item @code{external} (via Client TLS Certificate)
 This works in conjunction with the @code{:client-certificate} keyword
diff --git a/lisp/erc/erc.el b/lisp/erc/erc.el
index 09d55f60990..7f2dd89342b 100644
--- a/lisp/erc/erc.el
+++ b/lisp/erc/erc.el
@@ -2177,7 +2177,8 @@ erc--ensure-url
 
 ;;;###autoload
 (defun erc-select-read-args ()
-  "Prompt the user for values of nick, server, port, and password."
+  "Prompt the user for values of nick, server, port, and password.
+With prefix arg, also prompt for user and full name."
   (require 'url-parse)
   (let* ((input (let ((d (erc-compute-server)))
                   (read-string (format "Server or URL (default is %S): " d)
@@ -2198,6 +2199,14 @@ erc-select-read-args
                    (let ((d (erc-compute-nick)))
                      (read-string (format "Nickname (default is %S): " d)
                                   nil 'erc-nick-history-list d))))
+         (user (and current-prefix-arg
+                    (let ((d (erc-compute-user (url-user url))))
+                      (read-string (format "User (default is %S): " d)
+                                   nil nil d))))
+         (full (and current-prefix-arg
+                    (let ((d (erc-compute-full-name (url-user url))))
+                      (read-string (format "Full name (default is %S): " d)
+                                   nil nil d))))
          (passwd (let* ((p (with-suppressed-warnings ((obsolete erc-password))
                              (or (url-password url) erc-password)))
                         (m (if p
@@ -2213,8 +2222,8 @@ erc-select-read-args
                       #'erc-open-tls-stream)))
     (when (and passwd (string= "" passwd))
       (setq passwd nil))
-    `( :server ,server :port ,port :nick ,nick
-       ,@(and passwd `(:password ,passwd))
+    `( :server ,server :port ,port :nick ,nick ,@(and user `(:user ,user))
+       ,@(and passwd `(:password ,passwd)) ,@(and full `(:full-name ,full))
        ,@(and erc-interactive-display
               `(buffer-display ,erc-interactive-display))
        ,@(and opener `(connect-function ,opener)))))
@@ -2321,8 +2330,8 @@ erc-tls
 
 When present, ID should be a symbol or a string to use for naming
 the server buffer and identifying the connection unequivocally.
-See info node `(erc) Network Identifier' for details.  Like USER
-and CLIENT-CERTIFICATE, this parameter cannot be specified
+See info node `(erc) Network Identifier' for details.  Like
+CLIENT-CERTIFICATE, this parameter cannot be specified
 interactively.
 
 \(fn &key SERVER PORT NICK USER PASSWORD FULL-NAME CLIENT-CERTIFICATE ID)"
diff --git a/test/lisp/erc/erc-tests.el b/test/lisp/erc/erc-tests.el
index 91cdf2f1770..d05b0efe2ed 100644
--- a/test/lisp/erc/erc-tests.el
+++ b/test/lisp/erc/erc-tests.el
@@ -1086,7 +1086,18 @@ erc-select-read-args
                      (erc-select-read-args))
                    (list :server "[::1]"
                          :port 6667
-                         :nick "nick")))))
+                         :nick "nick"))))
+
+  (ert-info ("Extra args use URL nick by default")
+    (should (equal (ert-simulate-keys "nick:sesame@localhost:6667\r\r\r\r"
+                     (let ((current-prefix-arg '(4)))
+                       (erc-select-read-args)))
+                   (list :server "localhost"
+                         :port 6667
+                         :nick "nick"
+                         :user "nick"
+                         :password "sesame"
+                         :full-name "nick")))))
 
 (ert-deftest erc-tls ()
   (let (calls)
-- 
2.38.1