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.
This commit is contained in:
F. Jason Park 2022-12-12 23:58:03 -08:00
parent 09c0c6b2ba
commit f0c9088878
4 changed files with 45 additions and 27 deletions

View file

@ -60,6 +60,7 @@
(declare-function erc-buffer-filter "erc" (predicate &optional proc)) (declare-function erc-buffer-filter "erc" (predicate &optional proc))
(declare-function erc-current-nick "erc" nil) (declare-function erc-current-nick "erc" nil)
(declare-function erc-display-error-notice "erc" (parsed string)) (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-error "erc" (&rest args))
(declare-function erc-get-buffer "erc" (target &optional proc)) (declare-function erc-get-buffer "erc" (target &optional proc))
(declare-function erc-server-buffer "erc" nil) (declare-function erc-server-buffer "erc" nil)
@ -1260,24 +1261,45 @@ given by the `RPL_ISUPPORT' NETWORK parameter."
return name) return name)
(and-let* ((vanity (erc--get-isupport-entry 'NETWORK 'single)) (and-let* ((vanity (erc--get-isupport-entry 'NETWORK 'single))
((intern vanity)))) ((intern vanity))))
(erc-networks--id-given erc-networks--id)
erc-networks--name-missing-sentinel)) 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'. "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. ;; Always update (possibly clobber) current value, if any.
(let ((name (erc-networks--determine))) (pcase (setq erc-network (erc-networks--determine))
(when (eq name erc-networks--name-missing-sentinel) ((and (pred (eq (erc-networks--id-given erc-networks--id)))
;; This can happen theoretically, e.g., if you're editing some (let m (format "Couldn't determine network. Using given ID `%s'."
;; settings interactively on a proxy service that impersonates IRC erc-network)))
;; but aren't being proxied through to a real network. The (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). ;; service may send a 422 but no NETWORK param (or *any* 005s).
(let ((m (concat "Failed to determine network. Please set entry for " (let m (concat "Failed to determine network. Please set entry for \""
erc-server-announced-name " in `erc-networks-alist'."))) erc-server-announced-name "\" in `erc-networks-alist'"
(erc-display-error-notice parsed m) " or consider calling `erc-tls' with the keyword `:id'."
(erc-error "Failed to determine network"))) ; beep " See Info:\"(erc) Network Identifier\" for more.")))
(setq erc-network name)) (require 'info)
nil) (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" ;; This lives here in this file because all the other "on connect"
;; MOTD stuff ended up here (but perhaps that needs to change). ;; 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." 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 ;; The 004 handler never ran; see 2004-03-10 Diane Murray in change log
(unless erc-server-announced-name (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 (erc-display-error-notice
parsed (concat "If this was unexpected, consider reporting it via " parsed (concat "Failed to determine server name. Using \""
(substitute-command-keys "\\[erc-bug]") ".")) erc-server-announced-name "\" instead."
(setq erc-server-announced-name (erc-response.sender parsed))) " If this was unexpected, consider reporting it via "
(substitute-command-keys "\\[erc-bug]") ".")))
nil) nil)
(defun erc-unset-network-name (_nick _ip _reason) (defun erc-unset-network-name (_nick _ip _reason)

View file

@ -1171,6 +1171,8 @@
(let (erc-server-announced-name (let (erc-server-announced-name
(erc--isupport-params (make-hash-table)) (erc--isupport-params (make-hash-table))
erc-network erc-network
erc-quit-hook
(erc-server-process (erc-networks-tests--create-live-proc))
calls) calls)
(erc-mode) (erc-mode)
@ -1183,10 +1185,7 @@
(ert-info ("Signals when table empty and NETWORK param unset") (ert-info ("Signals when table empty and NETWORK param unset")
(setq erc-server-announced-name "irc.fake.gnu.org") (setq erc-server-announced-name "irc.fake.gnu.org")
(let ((err (should-error (erc-networks--set-name (should (eq 'error (erc-networks--set-name nil (make-erc-response))))
nil (make-erc-response)))))
(should (string-match-p "failed" (cadr err)))
(should (eq (car err) 'error)))
(should (string-match-p (rx "*** Failed") (car (pop calls))))))) (should (string-match-p (rx "*** Failed") (car (pop calls)))))))
(erc-networks-tests--clean-bufs))) (erc-networks-tests--clean-bufs)))

View file

@ -98,11 +98,10 @@
:nick "tester" :nick "tester"
:full-name "tester") :full-name "tester")
(should (string= (buffer-name) (format "127.0.0.1:%d" port))) (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")
(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")))))) (should (string= erc-server-announced-name "irc.foonet.org"))))))
;; Targets that are host/server masks like $*, $$*, and #* are routed ;; Targets that are host/server masks like $*, $$*, and #* are routed

View file

@ -3,6 +3,3 @@
((user 1 "USER user 0 * :tester") ((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 001 tester :Welcome to the FooNet Internet Relay Chat Network tester")
(0 ":irc.foonet.org 422 tester :MOTD File is missing")) (0 ":irc.foonet.org 422 tester :MOTD File is missing"))
((mode-user 1.2 "MODE tester +i")
(0 ":tester MODE tester :+Zi"))