>From a9b47f5a6079fb3030c9e1514b4cbbda86dafff8 Mon Sep 17 00:00:00 2001 From: "F. Jason Park" Date: Mon, 11 Jul 2022 05:14:57 -0700 Subject: [PATCH 4/6] Default to TLS port when calling erc-tls from lisp * lisp/erc/erc.el (erc-legacy-port-names, erc-normalize-port): Add standard IANA port-name mappings for 6667 and 6697, as well as an option to opt in for saner but nonstandard behavior. (erc-open): Add note to doc string explaining that params `connect' and `channel' are mutually exclusive. (erc-tls): Call `erc-compute-port' with override. (erc-compute-port): Call `erc-normalize-port' with result'. * test/lisp/erc/erc-tests.el (erc-tls): Add simplistic test focusing on default parameters. --- lisp/erc/erc.el | 34 ++++++++++++++++++++++++++----- test/lisp/erc/erc-tests.el | 41 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 5 deletions(-) diff --git a/lisp/erc/erc.el b/lisp/erc/erc.el index 7f25afa8c5..01bb6f9f45 100644 --- a/lisp/erc/erc.el +++ b/lisp/erc/erc.el @@ -1534,6 +1534,15 @@ erc-reuse-buffers (make-obsolete-variable 'erc-reuse-buffers "old behavior when t now permanent" "29.1") +(defcustom erc-legacy-port-names 'legacy + "Interpret \"irc\" and \"ircs\" using IANA service mappings. +When non-nil, this yields 194 and 994 instead of 6667 and 6697. +When set to `legacy', it also emits a warning saying that the +default will change to nil in the future." + :group 'erc + :package-version '(ERC . "5.4.1") ; FIXME increment on ELPA release + :type '(choice (const nil) (const legacy) (const t))) + (defun erc-normalize-port (port) "Normalize the port specification PORT to integer form. PORT may be an integer, a string or a symbol. If it is a string or a @@ -1542,6 +1551,11 @@ erc-normalize-port * ircs -> 994 * ircd -> 6667 * ircd-dalnet -> 7000" + ;; These were updated in 2022 to reflect modern standards and + ;; practices. See also: + ;; + ;; https://datatracker.ietf.org/doc/html/rfc7194#section-1 + ;; https://www.iana.org/assignments/service-names-port-numbers (cond ((symbolp port) (erc-normalize-port (symbol-name port))) @@ -1550,8 +1564,16 @@ erc-normalize-port (cond ((> port-nr 0) port-nr) - ((string-equal port "irc") - 194) + ((string-equal port "ircu") 6667) + ((string-equal port "ircs-u") 6697) + ((member port '("irc" "ircs")) + (when (eq erc-legacy-port-names 'legacy) + (lwarn 'ERC 'warning + (concat "`erc-legacy-port-names' will default to nil " + "in a future version of ERC."))) + (if (string= port "irc") + (if erc-legacy-port-names 194 6667) + (if erc-legacy-port-names 994 6697))) ((string-equal port "ircs") 994) ((string-equal port "ircd") @@ -1924,7 +1946,9 @@ erc-open If CONNECT is non-nil, connect to the server. Otherwise assume already connected and just create a separate buffer for the new -target CHANNEL. +target given by CHANNEL, meaning these parameters are mutually +exclusive. Note that CHANNEL may also be a query; its name has +been retained for historical reasons. Use PASSWD as user password on the server. If TGT-LIST is non-nil, use it to initialize `erc-default-recipients'. @@ -2183,7 +2207,7 @@ 'erc-ssl ;;;###autoload (cl-defun erc-tls (&key (server (erc-compute-server)) - (port (erc-compute-port)) + (port (erc-compute-port 'ircs-u)) (nick (erc-compute-nick)) (user (erc-compute-user)) password @@ -6390,7 +6414,7 @@ erc-compute-port - PORT (the argument passed to this function) - The `erc-port' option - The `erc-default-port' variable" - (or port erc-port erc-default-port)) + (erc-normalize-port (or port erc-port erc-default-port))) ;; time routines diff --git a/test/lisp/erc/erc-tests.el b/test/lisp/erc/erc-tests.el index f72db816af..348c047b73 100644 --- a/test/lisp/erc/erc-tests.el +++ b/test/lisp/erc/erc-tests.el @@ -1042,4 +1042,45 @@ erc-select-read-args :nick "nick" :password nil))))) +(ert-deftest erc-tls () + (let (calls) + (cl-letf (((symbol-function 'user-login-name) + (lambda (&optional _) "tester")) + ((symbol-function 'erc-open) + (lambda (&rest r) (push r calls)))) + + (ert-info ("Defaults") + (erc-tls) + (should (equal (pop calls) + '("irc.libera.chat" 6697 "tester" "unknown" t + nil nil nil nil nil "user" nil)))) + + (ert-info ("Full") + (erc-tls :server "irc.gnu.org" + :port 7000 + :user "bobo" + :nick "bob" + :full-name "Bob's Name" + :password "bob:changeme" + :client-certificate t + :id 'GNU.org) + (should (equal (pop calls) + '("irc.gnu.org" 7000 "bob" "Bob's Name" t + "bob:changeme" nil nil nil t "bobo" GNU.org)))) + + ;; Values are often nil when called by lisp code, which leads to + ;; null params. This is why `erc-open' recomputes everything. + (ert-info ("Fallback") + (let ((erc-nick "bob") + (erc-server "irc.gnu.org") + (erc-email-userid "bobo") + (erc-user-full-name "Bob's Name")) + (erc-tls :server nil + :port 7000 + :nick nil + :password "bob:changeme")) + (should (equal (pop calls) + '(nil 7000 nil "Bob's Name" t + "bob:changeme" nil nil nil nil "bobo" nil))))))) + ;;; erc-tests.el ends here -- 2.38.1