From f0c908887815a9cc668a5a03cbc1b7d6eaf8c91d Mon Sep 17 00:00:00 2001 From: "F. Jason Park" Date: Mon, 12 Dec 2022 23:58:03 -0800 Subject: [PATCH] Set erc-network to a "given" ID instead of failing * lisp/erc/erc-networks.el (erc-networks--determine): Return the so-called "given" ID from a non-nil `:id' keyword arg passed to `erc' or `erc-tls'. (erc-networks--allow-unknown-network): Add internal variable to allow IRC session to continue despite the network being unknown. (erc-networks--set-name): Tell the user about falling back to a given ID when the network can't be determined. When that's so, end the session by destroying the connection unless `erc-networks--allow-unknown-network' is enabled. (Bug#59976.) (erc-networks--ensure-announced): Include the fallback announced server name in the error message. * test/lisp/erc/erc-networks-tests.el (erc-networks--set-name): Add dummy server process and don't expect an error to be signaled. * test/lisp/erc/erc-scenarios-misc.el (erc-scenarios-networks-announced-missing): Don't expect an error to be signaled. * test/lisp/erc/resources/networks/announced-missing/foonet.eld: Remove "mode" match pattern. --- lisp/erc/erc-networks.el | 57 +++++++++++++------ test/lisp/erc/erc-networks-tests.el | 7 +-- test/lisp/erc/erc-scenarios-misc.el | 5 +- .../networks/announced-missing/foonet.eld | 3 - 4 files changed, 45 insertions(+), 27 deletions(-) diff --git a/lisp/erc/erc-networks.el b/lisp/erc/erc-networks.el index 19a7ab86437..fd8bed470ad 100644 --- a/lisp/erc/erc-networks.el +++ b/lisp/erc/erc-networks.el @@ -60,6 +60,7 @@ (declare-function erc-buffer-filter "erc" (predicate &optional proc)) (declare-function erc-current-nick "erc" nil) (declare-function erc-display-error-notice "erc" (parsed string)) +(declare-function erc-display-message "erc" (parsed type buffer msg &rest args)) (declare-function erc-error "erc" (&rest args)) (declare-function erc-get-buffer "erc" (target &optional proc)) (declare-function erc-server-buffer "erc" nil) @@ -1260,24 +1261,45 @@ given by the `RPL_ISUPPORT' NETWORK parameter." return name) (and-let* ((vanity (erc--get-isupport-entry 'NETWORK 'single)) ((intern vanity)))) + (erc-networks--id-given erc-networks--id) erc-networks--name-missing-sentinel)) -(defun erc-networks--set-name (_proc parsed) +(defvar erc-networks--allow-unknown-network nil + "Whether to ignore a failure in identifying the network. +If you need this as a user option, please say so via \\[erc-bug]. +Otherwise, expect it to vanish at any time.") ; Bug#59976 + +(defun erc-networks--set-name (proc parsed) "Set `erc-network' to the value returned by `erc-networks--determine'. -Signal an error when the network cannot be determined." +Print an error message when the network cannot be determined before +shutting down the connection." ;; Always update (possibly clobber) current value, if any. - (let ((name (erc-networks--determine))) - (when (eq name erc-networks--name-missing-sentinel) - ;; This can happen theoretically, e.g., if you're editing some - ;; settings interactively on a proxy service that impersonates IRC - ;; but aren't being proxied through to a real network. The + (pcase (setq erc-network (erc-networks--determine)) + ((and (pred (eq (erc-networks--id-given erc-networks--id))) + (let m (format "Couldn't determine network. Using given ID `%s'." + erc-network))) + (erc-display-message parsed 'notice nil m) + nil) + ((and + (guard (eq erc-network erc-networks--name-missing-sentinel)) + ;; This can happen theoretically, e.g., when adjusting settings + ;; on a proxy service that partially impersonates IRC but isn't + ;; currently conveying anything through to a real network. The ;; service may send a 422 but no NETWORK param (or *any* 005s). - (let ((m (concat "Failed to determine network. Please set entry for " - erc-server-announced-name " in `erc-networks-alist'."))) - (erc-display-error-notice parsed m) - (erc-error "Failed to determine network"))) ; beep - (setq erc-network name)) - nil) + (let m (concat "Failed to determine network. Please set entry for \"" + erc-server-announced-name "\" in `erc-networks-alist'" + " or consider calling `erc-tls' with the keyword `:id'." + " See Info:\"(erc) Network Identifier\" for more."))) + (require 'info) + (erc-display-error-notice parsed m) + (if erc-networks--allow-unknown-network + (progn + (erc-display-error-notice + parsed (format "Continuing anyway with network set to `%s'." + erc-network)) + nil) + (delete-process proc) + 'error)))) ;; This lives here in this file because all the other "on connect" ;; MOTD stuff ended up here (but perhaps that needs to change). @@ -1287,11 +1309,12 @@ Signal an error when the network cannot be determined." Copy source (prefix) from MOTD-ish message as a last resort." ;; The 004 handler never ran; see 2004-03-10 Diane Murray in change log (unless erc-server-announced-name - (erc-display-error-notice parsed "Failed to determine server name.") + (setq erc-server-announced-name (erc-response.sender parsed)) (erc-display-error-notice - parsed (concat "If this was unexpected, consider reporting it via " - (substitute-command-keys "\\[erc-bug]") ".")) - (setq erc-server-announced-name (erc-response.sender parsed))) + parsed (concat "Failed to determine server name. Using \"" + erc-server-announced-name "\" instead." + " If this was unexpected, consider reporting it via " + (substitute-command-keys "\\[erc-bug]") "."))) nil) (defun erc-unset-network-name (_nick _ip _reason) diff --git a/test/lisp/erc/erc-networks-tests.el b/test/lisp/erc/erc-networks-tests.el index fc12bf7ce37..e883174e28a 100644 --- a/test/lisp/erc/erc-networks-tests.el +++ b/test/lisp/erc/erc-networks-tests.el @@ -1171,6 +1171,8 @@ (let (erc-server-announced-name (erc--isupport-params (make-hash-table)) erc-network + erc-quit-hook + (erc-server-process (erc-networks-tests--create-live-proc)) calls) (erc-mode) @@ -1183,10 +1185,7 @@ (ert-info ("Signals when table empty and NETWORK param unset") (setq erc-server-announced-name "irc.fake.gnu.org") - (let ((err (should-error (erc-networks--set-name - nil (make-erc-response))))) - (should (string-match-p "failed" (cadr err))) - (should (eq (car err) 'error))) + (should (eq 'error (erc-networks--set-name nil (make-erc-response)))) (should (string-match-p (rx "*** Failed") (car (pop calls))))))) (erc-networks-tests--clean-bufs))) diff --git a/test/lisp/erc/erc-scenarios-misc.el b/test/lisp/erc/erc-scenarios-misc.el index 3fa05ee8a60..f1696088a48 100644 --- a/test/lisp/erc/erc-scenarios-misc.el +++ b/test/lisp/erc/erc-scenarios-misc.el @@ -98,11 +98,10 @@ :nick "tester" :full-name "tester") (should (string= (buffer-name) (format "127.0.0.1:%d" port))) - (let ((err (should-error (sleep-for 1)))) - (should (string-match-p "Failed to determine" (cadr err)))) (funcall expect 1 "Failed to determine") (funcall expect 1 "Failed to determine") - (should-not erc-network) + (funcall expect 1 "Connection failed") + (should (string-prefix-p "Unknown" (erc-network-name))) (should (string= erc-server-announced-name "irc.foonet.org")))))) ;; Targets that are host/server masks like $*, $$*, and #* are routed diff --git a/test/lisp/erc/resources/networks/announced-missing/foonet.eld b/test/lisp/erc/resources/networks/announced-missing/foonet.eld index 79b0fb462a8..4481f27663d 100644 --- a/test/lisp/erc/resources/networks/announced-missing/foonet.eld +++ b/test/lisp/erc/resources/networks/announced-missing/foonet.eld @@ -3,6 +3,3 @@ ((user 1 "USER user 0 * :tester") (0 ":irc.foonet.org 001 tester :Welcome to the FooNet Internet Relay Chat Network tester") (0 ":irc.foonet.org 422 tester :MOTD File is missing")) - -((mode-user 1.2 "MODE tester +i") - (0 ":tester MODE tester :+Zi")) -- 2.39.2