>From c2108baadd06a2301e379d41fcb5bbe9f03c2baf Mon Sep 17 00:00:00 2001 From: "F. Jason Park" Date: Thu, 29 Dec 2022 06:43:19 -0800 Subject: [PATCH 2/4] [POC] Offer TLS by default for M-x erc * lisp/erc/erc.el (erc-select-read-args): Offer to use TLS when user runs M-x erc and opts for default server and port. But 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. (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. --- lisp/erc/erc.el | 59 +++++++++++++++++++++----------------- test/lisp/erc/erc-tests.el | 42 +++++++++++++-------------- 2 files changed, 52 insertions(+), 49 deletions(-) diff --git a/lisp/erc/erc.el b/lisp/erc/erc.el index 98d55bb1ae..86149f7d2f 100644 --- a/lisp/erc/erc.el +++ b/lisp/erc/erc.el @@ -2192,29 +2192,19 @@ 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 erc-server-connect-function)) (when (and passwd (string= "" passwd)) (setq passwd nil)) - (when-let (((equal server erc-default-server)) - ((eql port erc-default-port)) - ((not (string-prefix-p "irc://" input))) ; ignore irc6:// - (key (substitute-command-keys "\\[erc-tls]")) - ((y-or-n-p (concat - "Switch to " key " now instead of connecting " - (propertize "unencrypted" 'face 'error) "?")))) - (run-at-time nil nil - (lambda () - (with-suppressed-warnings ((obsolete erc-password)) - (let ((erc-nick nick) - (erc-password passwd)) - (with-temp-message (format "Switching to %s ..." key) - (sit-for 1)) - (with-current-buffer (call-interactively #'erc-tls) - (erc-display-error-notice - nil (format "Please use %s directly from now on." - key))))))) - (user-error "Non-TLS connection to default server detected")) - (list :server server :port port :nick nick :password passwd))) + (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 passwd `(:password ,passwd)) connect-function ,opener))) + ;;;###autoload (cl-defun erc (&key (server (erc-compute-server)) @@ -2223,7 +2213,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 +2239,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 +2257,10 @@ erc-tls password (full-name (erc-compute-full-name)) client-certificate - id) + id + ;; For interactive use + ((connect-function erc-server-connect-function) + #'erc-open-tls-stream)) "ERC is a powerful, modular, and extensible IRC client. This function is the main entry point for ERC over TLS. @@ -2306,10 +2304,17 @@ 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." - (interactive (let ((erc-port (or erc-port erc-default-port-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))) (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 400b92d29e..fb23aa3c0a 100644 --- a/test/lisp/erc/erc-tests.el +++ b/test/lisp/erc/erc-tests.el @@ -1001,10 +1001,13 @@ erc--server-connect-dumb-ipv6-regexp (ert-deftest erc-select-read-args () - (ert-info ("Error signaled with default server and non-TLS port") - (should (pcase (should-error (ert-simulate-keys "\r\r\r\ry\r" - (erc-select-read-args))) - (`(user-error ,(rx "Non-TLS")) 'passed)))) + (ert-info ("Doesn't default to TLS") + (should (equal (ert-simulate-keys "\r\r\r\ry\r" + (erc-select-read-args)) + (list :server "irc.libera.chat" + :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" @@ -1012,7 +1015,7 @@ erc-select-read-args (list :server "irc.libera.chat" :port 6667 :nick (user-login-name) - :password nil)))) + 'connect-function #'erc-open-network-stream)))) (ert-info ("Override non-TLS warning via URL scheme") (should (equal (ert-simulate-keys "irc://irc.libera.chat\r\r\r\r" @@ -1020,29 +1023,27 @@ erc-select-read-args (list :server "irc.libera.chat" :port 6667 :nick (user-login-name) - :password nil)))) + 'connect-function #'erc-open-network-stream)))) (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)))) + 'connect-function #'erc-open-network-stream)))) (ert-info ("Address includes nick, password skipped via option") (should (equal (ert-simulate-keys "nick@localhost:6667\r" (let (erc-prompt-for-password) - (erc-select-read-args))) + (butlast (erc-select-read-args) 2))) (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" - (erc-select-read-args)) + (butlast (erc-select-read-args) 2)) (list :server "localhost" :port 6667 :nick "nick" @@ -1050,27 +1051,24 @@ erc-select-read-args (ert-info ("IPv6 address plain") (should (equal (ert-simulate-keys "::1\r\r\r\r" - (erc-select-read-args)) + (butlast (erc-select-read-args) 2)) (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)) + (butlast (erc-select-read-args) 2)) (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)) + (butlast (erc-select-read-args) 2)) (list :server "[::1]" :port 6667 - :nick "nick" - :password nil))))) + :nick "nick"))))) (ert-deftest erc-tls () (let (calls) -- 2.38.1