From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: "F. Jason Park" Date: Mon, 3 May 2021 05:54:56 -0700 Subject: [PATCH 13/28] Address long-standing ERC buffer-naming issues * lisp/erc/erc-networks.el (erc-determine-network, erc-networks--determine): Deprecate former and partially replace with latter, which demotes RPL_ISUPPORT-derived NETWORK name to fallback in favor of known `erc-networks-alist' members as part of shift to session- and network-based connection-identity policy. Return sentinel on failure. Expect `erc-server-announced-name' to be set, and signal when it's not. (erc-networks--name-missing-sentinel): Value returned when new function `erc-networks--determine' fails to find network name. (erc-set-network-name, erc-networks--set-name): Deprecate former and partially replace with latter. Ding with helpful and don't set `erc-network' message when network name is not found. (erc-networks--ensure-announced): Add new fallback function to ensure `erc-server-announced-name' is set. Register with post-MOTD hooks. (erc-networks--copy-name): Add new function to copy over network name from server buffer. Prefer this over doing the same in `erc-open' to help sustain the idea of this "module" being anything other than a hard dependency. (erc-networks--init-session): Add new function to perform one-time session-related setup. This can (should?) be combined with `erc-set-network-name. (erc-networks--rename-server-buffer): New function replaces `erc-unset-network-name' as default `erc-disconnect-hook' member; renames server buffers once network is discovered; added to/removed from `erc-after-connect' hook on erc-networks minor mode. (erc-networks--insert-transplanted-content, erc-networks--maybe-reclaim-target-buffer, erc-networks--copy-over-server-buffer-contents, erc--update-server-session): Add helpers for `erc-networks--rename-server-buffer'. The first re-associates all existing target buffers that ought to be owned by the new server process. The second grabs buffer text from an old, dead server buffer before killing it. It then inserts that text above everything in the current, replacement server buffer. The other two massage the IDs of related sessions, possibly renaming them as well. They may also uniquify the current session's ID. (erc-networks-enable, erc-networks-mode): Add above hooks in appropriate order to 376/422 functions. * test/lisp/erc/erc-tests.el: add tests for the above functions. * lisp/erc/erc.el (erc-rename-buffers): Change this option's default to t, remove the only instance where it's actually used, and make it an obsolete variable. (erc--maybe-rename-surviving-target-buffer): Add new function that renames a target buffer when it becomes the sole bearer of a name based on a target across all sessions (and normally all networks). In other words, remove the @NETWORK or @SESSION-ID suffix from the last remaining channel or query buffer after its namesakes have all been killed off. Register this function with ERC's target-related kill-buffer hooks. (erc--refresh-session-buffer-names): Helper for `erc--shrink-ids-and-buffer-names'. (erc--visit-collisions): Add new function that visits all ERC buffers and calls callbacks when a buffer-name collision is encountered. (erc--construct-buffer-name, erc--maybe-update-buffer-name, erc--rename-session-buffers): Add helpers to support `erc--reconcile-buffer-names' and friends. (erc--reconcile-buffer-names, erc--reconcile-buffer-names-visit): Add new helper functions for `erc-generate-new-buffer-name' that only run in target buffers. (erc-generate-new-buffer-name): Replace current policy of appending a slash and the invocation host name. Favor instead temporary names for server buffers and session-/network-based uniquifying suffixes for channels and query buffers. Fall back to the TCP host:port convention when necessary. The signature has changed. Another optional param has been appended after the others. (erc-get-buffer-create): Don't generate a new name when reconnecting, just return the same buffer. `erc-open' starts from a clean slate anyway, so this just keeps things simple. Also add optional ID param. (erc-open): Add new id param to for canonical given session identifier, which must be a symbol. This is stored in the `given' slot of the session's erc--session object. (erc, erc-tls): Add new id option and pass it to erc-open. Accept a string as well as a symbol to comport better with other params. (erc-log-irc-protocol): Use `erc--session-id' instead of the function `erc-network' to determine preferred peer name. (erc-format-target-and/or-network): This is called frequently for mode-line updates. Don't rename buffers here. Instead, do so in `erc-update-server-buffer-name'. (erc-kill-channel-hook, erc-kill-buffer-hook): add `erc-maybe-rename-surviving-taget-buffer' as default member. * test/lisp/erc/erc-tests.el: add tests for the above functions. * lisp/erc/erc-backend.el (erc--sid): Define new struct that contains all info relevant to specifying a unique session identifier. (erc--session): Add a new variable for storing local `erc--sid' object, common to all buffers in a session. (erc--sid-given, erc--sid-create, erc--sid-on-connect, erc--sid--equal-p, erc--sid-dynamic-init-parts, erc--sid-dynamic-init-id, erc--sid-dynamic-grow-id, erc--sid-dynamic-reset-id, erc--sid-dynmaic-prefix-length, erc--sid-dynamic-update, erc--sid-reload, erc--sid-ensure-comparable): Add new helpers to support `erc--sid' struct. (erc--shrink-ids-and-buffer-names): Add function to reassess all session IDs and shrink them if necessary along with affected buffer names. Register this on all three of ERC's kill-buffer hooks, all three because an orphaned target buffer is enough to keep its session alive. (erc--sid-sep): New var for to help when formatting buffer names. (erc-server-reconnect): Call erc-open with new id param (also fix reconnect issue related to bug#47788). (erc-server-JOIN): pass given id when calling erc-open. (erc-server-NICK): apply same name-generation used by `erc-open'; except here, for the purpose of "re-nicking". Update session object and maybe buffer names after own nick changes. See bug#48598 for background on all of the above. --- lisp/erc/erc-backend.el | 295 +++++++- lisp/erc/erc-networks.el | 231 +++++- lisp/erc/erc.el | 300 +++++--- test/lisp/erc/erc-networks-tests.el | 525 ++++++++++++++ test/lisp/erc/erc-tests.el | 1019 ++++++++++++++++++++++++++- 5 files changed, 2264 insertions(+), 106 deletions(-) create mode 100644 test/lisp/erc/erc-networks-tests.el diff --git a/lisp/erc/erc-backend.el b/lisp/erc/erc-backend.el index e0cbe308fd..9e96a24e8c 100644 --- a/lisp/erc/erc-backend.el +++ b/lisp/erc/erc-backend.el @@ -124,6 +124,269 @@ erc-server-current-nick "Nickname on the current server. Use `erc-current-nick' to access this.") + +;;;; Logical session + +;; This section may be happier in erc.el. Right now, code mutating +;; `erc--sid' slots via "place" must come after this point in this +;; file. See above note re "mutual dependency." + +(defvar-local erc--session nil + "Persistent identifying info for a logical session. +\"Logical\" means a session can outlive a connection and survive changes +in connection type. Essential to this is ensuring recovery from the +loss of a server buffer, so this object must be shared among server and +target buffers to allow for reassociation.") + +(cl-defstruct erc--sid + "A unique session identifier. +Here, \"session\" means a logical IRC session that may span multiple +connection lifetimes." + (ts nil :type float :read-only t :documentation "Creation timestamp.") + (symbol nil :type symbol :documentation "ID as a symbol.")) + +(cl-defstruct (erc--sid-fixed + (:include erc--sid) + (:constructor erc--sid-fixed-create + (given + &aux + (ts (float-time)) + (symbol given))))) + +(cl-defstruct (erc--sid-dynamic + (:include erc--sid) + (:constructor erc--sid-dynamic-create + (&aux + (ts (float-time)) + (parts (erc--sid-dynamic-init-parts)) + (symbol (erc--sid-dynamic-init-id parts)) + (len 1)))) + "A session identifier and its constituent components. +Two sessions are considered equivalent when their non-empty `parts' +slots compare equal. Sessions sharing a common prefix of `parts' are +considered related. A session's ID is determined by concatenating the +shortest prefix (non-empty initial substring of `parts') unique among +those of its relatives. For example, related sessions [b a r d o] and +[b a z a r] would have IDs b/a/r and b/a/z respectively." + (parts nil :type sequence ; a vector of atoms + :documentation "Sequence of identifying components.") + (len 0 :type integer + :documentation "Length of active `parts' interval.")) + +;; Please use this instead of `erc--sid-fixed-p'. +(cl-defgeneric erc--sid-given ((_ erc--sid)) + "Return the session's preassigned identifier if any. +This may have come in the form of an :id arg to an \"entry-point\" +command like `erc-tls' or `erc'." + nil) + +(cl-defmethod erc--sid-given ((sid erc--sid-fixed)) + (erc--sid-symbol sid)) + +(defun erc--sid-create (id) + "Invoke an appropriate constructor for an `erc--sid' object." + ;; Trust a user-provided ID unconditionally. + (if id + (erc--sid-fixed-create id) + ;; If a user explicitly set the deprecated `erc-rename-buffers' to + ;; its former default of nil, honor that for compatibility's sake. + (if (not (with-suppressed-warnings ((obsolete erc-rename-buffers)) + erc-rename-buffers)) + (erc--sid-fixed-create (intern (buffer-name))) + ;; Otherwise, use an adaptive name derived from network params. + (erc--sid-dynamic-create)))) + +(cl-defgeneric erc--sid-on-connect ((_ erc--sid)) + "Update `erc--session' after session params are guaranteed known. +This is typically during or just after MOTD." + nil) + +(cl-defmethod erc--sid-on-connect ((sid erc--sid-dynamic)) + (erc--sid-dynamic-update sid (erc--sid-dynamic-create))) + +(cl-defgeneric erc--sid-equal-p ((self erc--sid) (other erc--sid)) + "Return non-nil when two sessions exhibit underlying equality. +SELF and OTHER are `erc--sid' struct instances. This should normally be +used only for SID recovery or merging, after which no two SIDs should be +equal that aren't also eq." + (eq self other)) + +(cl-defmethod erc--sid-equal-p ((a erc--sid-fixed) (b erc--sid-fixed)) + (or (eq a b) (eq (erc--sid-symbol a) (erc--sid-symbol b)))) + +(cl-defmethod erc--sid-equal-p ((a erc--sid-dynamic) (b erc--sid-dynamic)) + (or (eq a b) (equal (erc--sid-dynamic-parts a) (erc--sid-dynamic-parts b)))) + +;; It's likely cleaner to create a new type inheriting from +;; `erc--sid-dynamic' than to convert this to a generic. However, the +;; latter may be simpler, e.g., with &context (erc-v3-device +;; erc-v3--device-t) or similar if some future "device" extension with +;; three members, like [Libera.Chat "bob" laptop], ever comes along. + +(defun erc--sid-dynamic-init-parts () + "Return opaque list of atoms to serve as canonical session identifier." + (when-let ((network (erc-network)) + (nick (erc-current-nick))) + (vector network (erc-downcase nick)))) + +(defun erc--sid-dynamic-init-id (elts &optional len) + "Create and return symbol to represent session identified by ELTS. +Use leading interval of length LEN as contributing components. Combine +them with string separator `erc--sid-sep'." + (when elts + (unless len + (setq len 1)) + (intern (mapconcat (lambda (s) (prin1-to-string s t)) + (seq-subseq elts 0 len) + erc--sid-sep)))) + +(defun erc--sid-dynamic-grow-id (session) + "Grow session ID by one component or return nil when at capacity." + (unless (= (length (erc--sid-dynamic-parts session)) + (erc--sid-dynamic-len session)) + (setf (erc--sid-symbol session) + (erc--sid-dynamic-init-id (erc--sid-dynamic-parts session) + (cl-incf (erc--sid-dynamic-len + session)))))) + +(defun erc--sid-dynamic-reset-id (session) + "Restore session to its initial state." + (setf (erc--sid-dynamic-len session) 1 + (erc--sid-symbol session) (erc--sid-dynamic-init-id + (erc--sid-dynamic-parts session)))) + +(defun erc--sid-dynamic-prefix-length (sess-a sess-b) + "Return length of common initial prefix of SESS-A and SESS-B. +Return nil when no such sequence exists (instead of zero)." + (when-let* ((a (erc--sid-dynamic-parts sess-a)) + (b (erc--sid-dynamic-parts sess-b)) + (n (min (length a) (length b))) + ((> n 0)) + ((equal (elt a 0) (elt b 0))) + (i 1)) + (while (and (< i n) + (equal (elt a i) + (elt b i))) + (cl-incf i)) + i)) + +(defun erc--sid-dynamic-update (dest source &rest overrides) + "Update DEST from SOURCE in place. +Copy slots into DEST from SOURCE and recompute ID. Both SOURCE and DEST +must be `erc--sid' objects. OVERRIDES is an optional plist of SLOT VAL +pairs." + (setf (erc--sid-dynamic-parts dest) (or (plist-get overrides :parts) + (erc--sid-dynamic-parts source)) + (erc--sid-dynamic-len dest) (or (plist-get overrides :len) + (erc--sid-dynamic-len source)) + (erc--sid-symbol dest) (or (plist-get overrides :symbol) + (erc--sid-dynamic-init-id + (erc--sid-dynamic-parts dest) + (erc--sid-dynamic-len dest))))) + +(cl-defgeneric erc--sid-reload (_sid &optional _proc _parsed) + "Handle an update to the current session ID. +If provided, PROC should be the current `erc-server-process' and PARSED +the current `erc-response'. SID is an `erc--sid' object." + nil) + +(cl-defmethod erc--sid-reload ((sid erc--sid-dynamic) &optional proc parsed) + "Attempt to refresh a session after a `erc--sid-dynamic-parts' update." + (erc--sid-dynamic-update sid (erc--sid-dynamic-create) + :len (erc--sid-dynamic-len sid)) + (erc-networks--rename-server-buffer (or proc erc-server-process) parsed) + (erc-with-all-buffers-of-server erc-server-process #'erc--default-target + (erc--maybe-update-buffer-name))) + +(cl-defgeneric erc--sid-ensure-comparable ((_ erc--sid) (_ erc--sid)) + "Take measures to ensure two sessions are in comparable states." + nil) + +(cl-defmethod erc--sid-ensure-comparable ((sid erc--sid-dynamic) + (other erc--sid-dynamic)) + "Grow SESSION's ID along with that of the current buffer. +Rename the current buffer if it's session ID has grown." + (when-let ((n (erc--sid-dynamic-prefix-length other sid))) + (while (and (<= (erc--sid-dynamic-len sid) n) + (erc--sid-dynamic-grow-id sid))) + ;; Grow and rename a visited buffer and all its targets + (when (and (> (erc--sid-dynamic-len sid) + (erc--sid-dynamic-len other)) + (erc--sid-dynamic-grow-id other)) + ;; Rename SESSION's buffers using current ID + (erc-buffer-filter (lambda () + (when (eq erc--session other) + (erc--maybe-update-buffer-name))))))) + +(defun erc--sid-sort-buffers (buffers) + "Return a list of target BUFFERS, newest to oldest." + (sort buffers + (lambda (a b) + (> (with-current-buffer a (erc--sid-ts erc--session)) + (with-current-buffer b (erc--sid-ts erc--session)))))) + +;; This being here is a casualty of the cyclic dependency noted above. +;; It belongs alongside `erc-networks--maybe-reclaim-target-buffers', + +(cl-defgeneric erc--shrink-ids-and-buffer-names () + "Recompute session IDs and buffer names while a buffer is being killed. +Ignore the current buffer." + nil) + +(defun erc--refresh-session-buffer-names (session &optional omit) + "Ensure all colliding buffers for session have suffixes. +Then rename current buffer appropriately. Don't consider buffer OMIT +when determining collisions." + (if (erc--examine-targets session erc--buffer-target + #'ignore + (lambda () + (unless (or (not omit) (eq (current-buffer) omit)) + (erc--maybe-update-buffer-name) + t))) + (erc--maybe-update-buffer-name) + (rename-buffer (erc--target-string erc--buffer-target) 'unique))) + +;; This currently doesn't equalize related sessions that may have +;; become mismatched because that shouldn't happen after a connection +;; is up (other than for a brief moment while renicking or similar, +;; when states are inconsistent). + +(cl-defmethod erc--shrink-ids-and-buffer-names + (&context (erc--session erc--sid-dynamic)) + (let ((omit (current-buffer)) + grown) + ;; Gather all grown sessions. + (erc-buffer-filter + (lambda () + (when (and erc--session + (erc--sid-dynamic-p erc--session) + (not (eq omit (current-buffer))) + (not (memq erc--session grown)) + (> (erc--sid-dynamic-len erc--session) 1)) + (push erc--session grown)))) + ;; Check for other sessions with shared prefix. If none exists, + ;; and session is overlong, shrink it. + (dolist (session grown) + (let ((skip t)) + (catch 'found + (dolist (other grown) + (unless (eq session other) + (setq skip nil) + (when (erc--sid-dynamic-prefix-length session other) + (throw 'found (setq skip t)))))) + (unless (or skip (< (erc--sid-dynamic-len session) 2)) + (erc--sid-dynamic-reset-id session) + (erc-buffer-filter + (lambda () + (when (and (eq erc--session session) + (not (eq (current-buffer) omit))) + (if erc--buffer-target + (erc--refresh-session-buffer-names session omit) + (erc--maybe-update-buffer-name)))))))))) + +(defvar erc--sid-sep "/" + "Separator used when joining `erc--sid-dynamic-parts' to form a session ID.") + ;;; Server attributes (defvar-local erc-server-process nil @@ -594,7 +857,9 @@ erc-server-reconnect (let ((erc-server-connect-function (or erc-session-connector #'erc-open-network-stream))) (erc-open erc-session-server erc-session-port erc-server-current-nick - erc-session-user-full-name t erc-session-password))))) + erc-session-user-full-name t erc-session-password + nil nil nil erc-session-client-certificate + (erc--sid-given erc--session)))))) (defun erc-server-delayed-reconnect (buffer) (if (buffer-live-p buffer) @@ -1297,7 +1562,9 @@ define-erc-response-handler nick erc-session-user-full-name nil nil (list chnl) chnl - erc-server-process)) + erc-server-process + nil + (erc--session-id erc-session))) (when buffer (set-buffer buffer) (with-suppressed-warnings @@ -1388,19 +1655,27 @@ define-erc-response-handler ;; sent to the correct nick. also add to bufs, since the user will want ;; to see the nick change in the query, and if it's a newly begun query, ;; erc-channel-users won't contain it - (erc-buffer-filter - (lambda () - (when (equal (erc-default-target) nick) - (setq erc-default-recipients (cons nn (cdr erc-default-recipients)) - erc--buffer-target (erc--target-from-string nn)) - (rename-buffer nn t) ; bug#12002 - (erc-update-mode-line) - (cl-pushnew (current-buffer) bufs)))) + ;; + ;; Possibly still relevant: bug#12002 + (when-let ((buf (erc-get-buffer nick erc-server-process)) + (tgt (erc--target-from-string nn))) + (with-current-buffer buf + (setq erc-default-recipients (cons nn (cdr erc-default-recipients)) + erc--buffer-target tgt)) + (with-current-buffer (erc-get-buffer-create erc-session-server + erc-session-port nil tgt + (erc--sid-given + erc--session)) + ;; Current buffer is among bufs + (erc-update-mode-line))) (erc-update-user-nick nick nn host nil nil login) (cond ((string= nick (erc-current-nick)) (cl-pushnew (erc-server-buffer) bufs) (erc-set-current-nick nn) + ;; Rename session, possibly rename server buf and all targets + (when (erc-network) + (erc--sid-reload erc--session proc parsed)) (erc-update-mode-line) (setq erc-nick-change-attempt-count 0) (setq erc-default-nicks (if (consp erc-nick) erc-nick (list erc-nick))) diff --git a/lisp/erc/erc-networks.el b/lisp/erc/erc-networks.el index 6ec5bc74a8..6e9e867801 100644 --- a/lisp/erc/erc-networks.el +++ b/lisp/erc/erc-networks.el @@ -738,7 +738,11 @@ erc-determine-network Use the server parameter NETWORK if provided, otherwise parse the server name and search for a match in `erc-networks-alist'." ;; The server made it easy for us and told us the name of the NETWORK - (let ((network-name (cdr (assoc "NETWORK" erc-server-parameters)))) + (declare (obsolete "maybe see `erc-networks--determine'" "29.1")) + (let ((network-name (cdr (assoc "NETWORK" + (with-suppressed-warnings + ((obsolete erc-server-parameters)) + erc-server-parameters))))) (if network-name (intern network-name) (or @@ -760,23 +764,234 @@ erc-network-name (defun erc-set-network-name (_proc _parsed) "Set `erc-network' to the value returned by `erc-determine-network'." + (declare (obsolete "maybe see `erc-networks--set-name'" "29.1")) (unless erc-server-connected - (setq erc-network (erc-determine-network))) + (setq erc-network (with-suppressed-warnings + ((obsolete erc-determine-network)) + (erc-determine-network)))) nil) +(defconst erc-networks--name-missing-sentinel (make-symbol "Unknown") + "Value to cover rare case of a literal NETWORK=nil.") + +(defun erc-networks--determine () + "Return the name of the network as a symbol. +Search `erc-networks-alist' for a known entity matching +`erc-server-announced-name'. If that fails, use the display name given +by the `RPL_ISUPPORT' NETWORK parameter." + (or (cl-loop for (name matcher) in erc-networks-alist + when (and matcher (string-match (concat matcher "\\'") + erc-server-announced-name)) + return name) + (and-let* ((vanity (cadr (assq 'NETWORK erc-isupport-parameters))) + ((intern vanity)))) + erc-networks--name-missing-sentinel)) + +(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." + (cl-assert (not erc-server-connected)) + ;; 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 + ;; 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-network-alist'."))) + (erc-display-error-notice parsed m) + (erc-error "Failed to determine network"))) ; beep + (setq erc-network name)) + nil) + +;; This lives here in this file because all the other "on connect" +;; MOTD stuff ended up here (but perhaps that needs to change). + +(defun erc-networks--ensure-announced (_ parsed) + "Set a fallback `erc-server-announced-name' if still unset. +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 + (let ((m (concat "Failed to determine server name. " + "If this was unexpected, please M-x erc-bug RET."))) + (erc-display-error-notice parsed m)) + (setq erc-server-announced-name (erc-response.sender parsed))) + nil) + +(defun erc-networks--copy-name (_buffer) + "Copy `erc-network' from the server buffer." + ;; Arg _buffer is always current buffer. + (when erc--buffer-target + (setq erc-network (erc-network)))) + (defun erc-unset-network-name (_nick _ip _reason) "Set `erc-network' to nil." (setq erc-network nil) nil) +;; TODO add note in Commentary saying that this module is considered a +;; core module and that it's as much about buffer naming and session +;; identity as anything else. + +(defun erc-networks--insert-transplanted-content (content) + (let ((inhibit-read-only t)) + (save-excursion + (save-restriction + (widen) + (goto-char (point-min)) + (insert-before-markers content))))) + +;; This should run whenever a session ID is updated. + +(defun erc-networks--maybe-reclaim-target-buffers (new-proc session announced) + "Visit disowned buffers for same SESSION and associate with NEW-PROC. +ANNOUNCED is the server-reported host name stored as +`erc-server-announced-name'." + (erc-buffer-filter + (lambda () + (when (and erc--buffer-target + (not erc-server-connected) + (erc--sid-equal-p erc--session session) + (or (not (erc--target-local-p erc--buffer-target)) + (string= erc-server-announced-name announced))) + ;; If a target buffer exists for the current process, kill this + ;; stale one after transplanting its content; else reinstate. + (if-let ((existing (erc-get-buffer + (erc--target-string erc--buffer-target) new-proc))) + (progn + (widen) + (let ((content (buffer-substring (point-min) + erc-insert-marker))) + (kill-buffer) ; allow target-buf renaming hook to run + (with-current-buffer existing + (erc--maybe-update-buffer-name) + (erc-networks--insert-transplanted-content content)))) + (setq erc-server-process new-proc + erc-server-connected t + erc--session session)))))) + +(defun erc-networks--copy-over-server-buffer-contents (existing name) + "Kill off existing server buffer after copying its contents. +Must be called from the replacement buffer." + ;; ERC expects `erc-open' to be idempotent when setting up local + ;; vars and other context properties for a new session. Thus, it's + ;; unlikely we'll have to copy anything else over besides text. And + ;; no reconciling of user tables, etc. happens during a normal + ;; reconnect, so we should be fine just sticking to text. (Right?) + (let ((text (with-current-buffer existing + ;; This `erc--session' should be `erc--sid-equal-p' + ;; to caller's session and older if not eq. + ;; + ;; `erc-server-process' should be set but dead + ;; and eq `get-buffer-process' unless latter nil + (delete-process erc-server-process) + (buffer-substring (point-min) erc-insert-marker))) + erc-kill-server-hook + erc-kill-buffer-hook) + (erc-networks--insert-transplanted-content text) + (kill-buffer name))) + +;; If another session is "related", meaning its ID shares a common +;; prefix, ensure our ID is unique by extending it. However, if the +;; existing session is equivalent, just trust that it's the proper +;; length and adopt it by overwriting ours. (Note target buffers are +;; considered as well because server buffers are often killed.) + +(defun erc-networks--update-server-session () + "Maybe grow or replace the current session." + (let* ((session erc--session) + (buffer (current-buffer)) + (f (lambda () + (unless (or (eq (current-buffer) buffer) + (eq erc--session session)) + (if (erc--sid-equal-p session erc--session) + (throw 'buffer erc--session) + (erc--sid-ensure-comparable session erc--session) + nil)))) + (found (catch 'buffer (erc-buffer-filter f)))) + (when found + (setq erc--session found)))) + +;; This stuff is only meant to run when initializing a newly connected +;; server buffer, whereas `erc-networks--rename-server-buffer' can run +;; mid-session after the session's core components have changed. + +(defun erc-networks--init-session (_proc _parsed) + "Update session with real network name." + ;; Initialize session for real now that we know the network + (cl-assert erc-network) + (unless (erc--sid-symbol erc--session) ; unless we've just reconnected + (erc--sid-on-connect erc--session)) + ;; Find duplicate sessions or other conflicting sessions and act + ;; accordingly. + (erc-networks--update-server-session) + ;; + nil) + +(defun erc-networks--rename-server-buffer (new-proc &optional _parsed) + "Rename server buffer with newly fleshed out session ID. +Assume the current buffer is a server buffer with a newly established +connection. Assume the network name has just been discovered. Refresh +the session ID if necessary and rename the buffer after it, unless +`erc-reuse-buffers' is nil, in which case let `generate-new-buffer-name' +do the renaming instead. + +When a buffer already exists with the chosen name, copy over its +contents and delete it. However, when its process is still alive, kill +off the newer, current one. This can happen, for example, after a +perceived loss in network connectivity turns out to be a false alarm +and the endpoint is a bouncer." + (cl-assert erc-network) + (cl-assert (eq new-proc erc-server-process)) + (cl-assert (erc--sid-symbol erc--session)) + ;; Always look for targets to reassociate because original server + ;; buffer may have been deleted. + (erc-networks--maybe-reclaim-target-buffers new-proc erc--session + erc-server-announced-name) + (let* ((name (symbol-name (erc--sid-symbol erc--session))) + ;; When this ends up being the current buffer, either we have + ;; a "given" ID or the buffer was reused on reconnecting. + (existing (get-buffer name))) + (cond ((or (not existing) + (erc--sid-given erc--session) + (eq existing (current-buffer))) + (rename-buffer name)) + ;; Abort on accidental reconnect or failure to pass :id param for + ;; avoidable collisions. + ((erc-server-process-alive existing) + (kill-local-variable 'erc-network) + (delete-process new-proc) + (erc-display-error-notice nil (format "Buffer %s still connected" + name)) + (erc-set-active-buffer existing)) + ;; Copy over old buffer's contents and kill it + (erc-reuse-buffers + (erc-networks--copy-over-server-buffer-contents existing name) + (rename-buffer name)) + (t (rename-buffer (generate-new-buffer-name name))))) + nil) + (define-erc-module networks nil "Provide data about IRC networks." - ((add-hook 'erc-server-375-functions #'erc-set-network-name) - (add-hook 'erc-server-422-functions #'erc-set-network-name) - (add-hook 'erc-disconnected-hook #'erc-unset-network-name)) - ((remove-hook 'erc-server-375-functions #'erc-set-network-name) - (remove-hook 'erc-server-422-functions #'erc-set-network-name) - (remove-hook 'erc-disconnected-hook #'erc-unset-network-name))) + ((add-hook 'erc-server-376-functions #'erc-networks--rename-server-buffer) + (add-hook 'erc-server-422-functions #'erc-networks--rename-server-buffer) + (add-hook 'erc-server-376-functions #'erc-networks--init-session) + (add-hook 'erc-server-422-functions #'erc-networks--init-session) + (add-hook 'erc-server-376-functions #'erc-networks--set-name) + (add-hook 'erc-server-422-functions #'erc-networks--set-name) + (add-hook 'erc-server-376-functions #'erc-networks--ensure-announced) + (add-hook 'erc-server-422-functions #'erc-networks--ensure-announced) + (add-hook 'erc-connect-pre-hook #'erc-networks--copy-name)) + ((remove-hook 'erc-server-376-functions #'erc-networks--ensure-announced) + (remove-hook 'erc-server-422-functions #'erc-networks--ensure-announced) + (remove-hook 'erc-server-376-functions #'erc-networks--set-name) + (remove-hook 'erc-server-422-functions #'erc-networks--set-name) + (remove-hook 'erc-server-376-functions #'erc-networks--init-session) + (remove-hook 'erc-server-422-functions #'erc-networks--init-session) + (remove-hook 'erc-server-376-functions #'erc-networks--rename-server-buffer) + (remove-hook 'erc-server-422-functions #'erc-networks--rename-server-buffer) + (remove-hook 'erc-connect-pre-hook #'erc-networks--copy-name))) (defun erc-ports-list (ports) "Return a list of PORTS. diff --git a/lisp/erc/erc.el b/lisp/erc/erc.el index 63f7133f96..35d56c3b82 100644 --- a/lisp/erc/erc.el +++ b/lisp/erc/erc.el @@ -190,12 +190,21 @@ erc-user-full-name :set (lambda (sym val) (set sym (if (functionp val) (funcall val) val)))) -(defcustom erc-rename-buffers nil +(defcustom erc-rename-buffers t "Non-nil means rename buffers with network name, if available." :version "24.5" :group 'erc :type 'boolean) +;; For the sake of compatibility, an ID will be created on the user's +;; behalf when `erc-rename-buffers' is nil and one wasn't provided. +;; The name will simply be that of the buffer, usually SERVER:PORT. +;; This violates the policy of treating provided IDs as gospel, but +;; it'll have to do for now. + +(make-obsolete-variable 'erc-rename-buffers + "old behavior when t now permanent" "29.1") + (defvar erc-password nil "Password to use when authenticating to an IRC server. It is not strictly necessary to provide this, since ERC will @@ -1639,55 +1648,148 @@ erc-port-equal (declare-function 'erc-network "erc-networks") -(defun erc-generate-new-buffer-name (server port target) - "Create a new buffer name based on the arguments." - (when (numberp port) (setq port (number-to-string port))) - (let* ((buf-name (or target - (let ((name (concat server ":" port))) - (when (> (length name) 1) - name)) - ;; This fallback should in fact never happen. - "*erc-server-buffer*")) - (full-buf-name (concat buf-name "/" server)) - (dup-buf-name (buffer-name (car (erc-channel-list nil)))) - buffer-name) - ;; Reuse existing buffers, but not if the buffer is a connected server - ;; buffer and not if its associated with a different server than the - ;; current ERC buffer. - ;; If buf-name is taken by a different connection (or by something !erc) - ;; then see if "buf-name/server" meets the same criteria. - (if (and dup-buf-name (string-match-p (concat buf-name "/") dup-buf-name)) - (setq buffer-name full-buf-name) ; ERC buffer with full name already exists. - (dolist (candidate (list buf-name full-buf-name)) - (if (and (not buffer-name) - erc-reuse-buffers - (or (not (get-buffer candidate)) - ;; Looking for a server buffer, so there's no target. - (and (not target) - (with-current-buffer (get-buffer candidate) - (and (erc-server-buffer-p) - (not (erc-server-process-alive))))) - ;; Channel buffer; check that it's from the right server. - (and target - (with-current-buffer (get-buffer candidate) - (and (string= erc-session-server server) - (erc-port-equal erc-session-port port)))))) - (setq buffer-name candidate) - (when (and (not buffer-name) (get-buffer buf-name) erc-reuse-buffers) - ;; A new buffer will be created with the name buf-name/server, rename - ;; the existing name-duplicated buffer with the same format as well. - (with-current-buffer (get-buffer buf-name) - (when (derived-mode-p 'erc-mode) ; ensure it's an erc buffer - (rename-buffer - (concat buf-name "/" (or erc-session-server erc-server-announced-name))))))))) - ;; If buffer-name is unset, neither candidate worked out for us, - ;; fallback to the old uniquification method: - (or buffer-name (generate-new-buffer-name full-buf-name)))) - -(defun erc-get-buffer-create (server port target) +(defun erc--maybe-rename-surviving-target-buffer () + "Maybe drop session suffix from fellow target-buffer's name. +But only do so when there's a single survivor with a target matching +that of the dying buffer." + (when-let* + ((target erc--buffer-target) + ;; Buffer name includes session-ID suffix + ((not (string= (erc--target-symbol target) ; string= t "t" -> t + (erc-downcase (buffer-name))))) + (buf (current-buffer)) + ;; All buffers, not just those belonging to same process + (others (erc-buffer-filter + (lambda () + (when-let ((erc--buffer-target) + ((not (eq buf (current-buffer))))) + (eq (erc--target-symbol target) + (erc--target-symbol erc--buffer-target)))))) + ((not (cdr others)))) + (with-current-buffer (car others) + (rename-buffer (erc--target-string target))))) + +(defun erc--examine-targets (session target on-dupe on-collision) + "Visit all ERC target buffers with the same TARGET. +Call ON-DUPE when a buffer's session belongs to SESSION or \"should\" +belong to SESSION after reconciliation. Call ON-COLLISION otherwise. +Neither function should accept any args. Expect TARGET to be an +`erc--target' object." + (declare (indent 2)) + (let ((announced erc-server-announced-name)) + (erc-buffer-filter + (lambda () + (when (and erc--buffer-target + (eq (erc--target-symbol erc--buffer-target) + (erc--target-symbol target))) + (let ((oursp (if (erc--target-local-p target) + (equal announced erc-server-announced-name) + (erc--sid-equal-p session erc--session)))) + (funcall (if oursp on-dupe on-collision)))))))) + +(defun erc--construct-buffer-name () + "Assemble buffer name from relevant local session variables." + (if erc--buffer-target + (concat (erc--target-string erc--buffer-target) + "@" (if (erc--target-local-p erc--buffer-target) + erc-server-announced-name + (symbol-name (erc--sid-symbol erc--session)))) + (symbol-name (erc--sid-symbol erc--session)))) + +(defun erc--maybe-update-buffer-name () + "Update current buffer name to reflect SESSION info if necessary." + (when-let* ((new-name (erc--construct-buffer-name)) + ((not (equal (buffer-name) new-name)))) + (rename-buffer new-name 'unique))) + +(defun erc--reconcile-buffer-names (target session) + "Reserve preferred buffer name for TARGET and SESSION. +Expect TARGET to be an `erc--buffer-target' instance. Guarantee that at +most one existing buffer has the same `erc--session' and a case-mapped +target, i.e., `erc--target-symbol'. If other buffers with equivalent +targets exist, rename them to TARGET@their-session-id and return +TARGET@our-session-id. Otherwise return TARGET as a string. When +multiple buffers for TARGET exist for the current session, rename them +with suffixes going from newest to oldest." + (let* (existing ; Former selves or unexpected dupes (for now allow > 1) + ;; Renamed ERC buffers on other networks matching target + (namesakes (erc--examine-targets session target + (lambda () (push (current-buffer) existing) nil) + ;; Append session ID as TARGET@SESSION-ID, + ;; possibly qualifying to achieve uniqueness. + (lambda () + (unless (erc--target-local-p erc--buffer-target) + (erc--sid-ensure-comparable session erc--session)) + (erc--maybe-update-buffer-name) + t))) + ;; Must follow ^ because session may have been modified + (esid (if (erc--target-local-p target) + erc-server-announced-name + (erc--sid-symbol session))) + (tgt-name (erc--target-string target)) + (name (if namesakes (format "%s@%s" tgt-name esid) tgt-name)) + placeholder) + ;; If we don't exist, claim name temporarily while renaming others + (when-let* (namesakes + (ex (get-buffer name)) + ((not (memq ex existing))) + (temp-name (generate-new-buffer-name (format "*%s*" name)))) + (setq existing (remq ex existing)) + (with-current-buffer ex + (rename-buffer temp-name) + (setq placeholder (get-buffer-create name)) + (rename-buffer name 'unique))) + (dolist (ex (erc--sid-sort-buffers existing)) + (with-current-buffer ex + (rename-buffer name 'unique))) + (when placeholder (kill-buffer placeholder)) + name)) + +(defun erc-generate-new-buffer-name (server port target &optional tgt-info id) + "Determine the name of an ERC buffer. +When TGT-INFO is nil, assume this is a server buffer. If ID is non-nil, +return ID as a string unless a buffer already exists with a live server +process, in which case signal an error. When ID is nil, return a +temporary name based on SERVER and PORT to be replaced with the network +name when discovered (see `erc-networks--rename-server-buffer'). Allow +either SERVER or PORT (but not both) to be nil to accommodate oddball +`erc-server-connect-function's. + +When TGT-INFO is non-nil, expect its string field to match the redundant +param TARGET (retained for compatibility). Whenever possibly, prefer +returning TGT-INFO's string unmodified. But when a case-insensitive +collision prevents that, return target@ID when ID is non-nil or +target@network otherwise after renaming the conflicting buffer in the +same manner. If the `networks' module isn't loaded, return target or +target." + (when target ; compat + (setq tgt-info (erc--target-from-string target))) + (if tgt-info + (let* ((esid (erc--sid-symbol erc--session)) + (name (if esid + (erc--reconcile-buffer-names tgt-info erc--session) + (erc--target-string tgt-info)))) + (if (and esid erc-reuse-buffers) + name + (generate-new-buffer-name name))) + (if id + (progn + (when-let* ((buf (get-buffer (symbol-name id))) + ((erc-server-process-alive buf))) + (user-error "Session with ID %S already exists" id)) + (symbol-name id)) + (generate-new-buffer-name (if (and server port) + (format "%s:%s" server port) + (or server port)))))) + +(defun erc-get-buffer-create (server port target &optional tgt-info id) "Create a new buffer based on the arguments." - (get-buffer-create (erc-generate-new-buffer-name server port target))) - + (when target ; compat + (setq tgt-info (erc--target-from-string target))) + (if (and erc--server-reconnecting (not tgt-info)) + (current-buffer) + (get-buffer-create + (erc-generate-new-buffer-name server port nil tgt-info id)))) (defun erc-member-ignore-case (string list) "Return non-nil if STRING is a member of LIST. @@ -2030,7 +2132,7 @@ erc-setup-buffer (defun erc-open (&optional server port nick full-name connect passwd tgt-list channel process - client-certificate) + client-certificate id) "Connect to SERVER on PORT as NICK with FULL-NAME. If CONNECT is non-nil, connect to the server. Otherwise assume @@ -2047,11 +2149,14 @@ erc-open or t, which means that `auth-source' will be queried for the private key and the certificate. +When non-nil, ID should be a symbol for identifying the connection. + Returns the buffer for the given server or channel." - (let ((buffer (erc-get-buffer-create server port channel)) - (old-buffer (current-buffer)) - old-point - continued-session) + (let* ((target (and channel (erc--target-from-string channel))) + (buffer (erc-get-buffer-create server port nil target id)) + (old-buffer (current-buffer)) + old-point + continued-session) (when connect (run-hook-with-args 'erc-before-connect server port nick)) (erc-update-modules) (set-buffer buffer) @@ -2081,7 +2186,7 @@ erc-open (set-marker erc-insert-marker (point)) ;; stack of default recipients (setq erc-default-recipients tgt-list) - (setq erc--buffer-target (and channel (erc--target-from-string channel))) + (setq erc--buffer-target target) (setq erc-server-current-nick nil) ;; Initialize erc-server-users and erc-channel-users (if connect @@ -2129,6 +2234,9 @@ erc-open secret)))) ;; client certificate (only useful if connecting over TLS) (setq erc-session-client-certificate client-certificate) + (setq erc--session (if connect + (erc--sid-create id) + (with-current-buffer old-buffer erc--session))) ;; debug output buffer (setq erc-dbuf (when erc-log-p @@ -2265,7 +2373,8 @@ erc (port (erc-compute-port)) (nick (erc-compute-nick)) password - (full-name (erc-compute-full-name))) + (full-name (erc-compute-full-name)) + id) "ERC is a powerful, modular, and extensible IRC client. This function is the main entry point for ERC. @@ -2277,6 +2386,7 @@ erc (nick (erc-compute-nick)) password (full-name (erc-compute-full-name)) + id That is, if called with @@ -2284,9 +2394,14 @@ erc then the server and full-name will be set to those values, whereas `erc-compute-port' and `erc-compute-nick' will be invoked -for the values of the other parameters." +for the values of the other parameters. + +When present, ID should be a string or a symbol for identifying the +connection unequivocally. This is rarely needed and not available +interactively." (interactive (erc-select-read-args)) - (erc-open server port nick full-name t password)) + (erc-open server port nick full-name t password nil nil nil nil + (if (stringp id) (intern id) id))) ;;;###autoload (defalias 'erc-select #'erc) @@ -2298,7 +2413,8 @@ erc-tls (nick (erc-compute-nick)) password (full-name (erc-compute-full-name)) - client-certificate) + client-certificate + id) "ERC is a powerful, modular, and extensible IRC client. This function is the main entry point for ERC over TLS. @@ -2312,6 +2428,7 @@ erc-tls password (full-name (erc-compute-full-name)) client-certificate + id That is, if called with @@ -2334,12 +2451,19 @@ erc-tls (erc-tls :server \"irc.libera.chat\" :port 6697 :client-certificate '(\"/home/bandali/my-cert.key\" - \"/home/bandali/my-cert.crt\"))" + \"/home/bandali/my-cert.crt\")) + +When present, ID should be a string or a symbol for identifying the +connection unequivocally. This option is generally not needed. See +info node `(erc) Connecting' for use cases. (Self-quoting symbols or +strings evaluating as such when read are invalid.) Not available +interactively." (interactive (let ((erc-default-port erc-default-port-tls)) (erc-select-read-args))) (let ((erc-server-connect-function 'erc-open-tls-stream)) (erc-open server port nick full-name t password - nil nil nil client-certificate))) + nil nil nil client-certificate + (if (stringp id) (intern id) id)))) (defun erc-open-tls-stream (name buffer host port &rest parameters) "Open an TLS stream to an IRC server. @@ -2406,13 +2530,19 @@ erc-log-irc-protocol If OUTBOUND is non-nil, STRING is being sent to the IRC server and appears in face `erc-input-face' in the buffer. Lines must already -contain CRLF endings. Peer is identified by the most precise label -available at run time, starting with the network name, followed by the -announced host name, and falling back to the dialed :." +contain CRLF endings. A peer is identified by the most precise label +available, starting with the session ID followed by the server-reported +hostname, and falling back to the dialed : pair. + +When capturing logs for multiple peers and sorting them into buckets, +such inconsistent labeling may pose a problem until the MOTD is +received. Setting a fixed `erc--session' can serve as a workaround." (when erc-debug-irc-protocol - (let ((esid (or (and (erc-network) (erc-network-name)) - erc-server-announced-name - (format "%s:%s" erc-session-server erc-session-port))) + (let ((esid (if-let ((erc--session) + (esid (erc--sid-symbol erc--session))) + (symbol-name esid) + (or erc-server-announced-name + (format "%s:%s" erc-session-server erc-session-port)))) (ts (when erc-debug-irc-protocol-time-format (format-time-string erc-debug-irc-protocol-time-format)))) (with-current-buffer (get-buffer-create "*erc-protocol*") @@ -6573,21 +6703,13 @@ erc-format-target-and/or-network "Return the network or the current target and network combined. If the name of the network is not available, then use the shortened server name instead." - (let ((network-name (or (and (fboundp 'erc-network-name) (erc-network-name)) - (erc-shorten-server-name - (or erc-server-announced-name - erc-session-server))))) - (when (and network-name (symbolp network-name)) - (setq network-name (symbol-name network-name))) - (cond ((erc-default-target) - (concat (erc-string-no-properties (erc-default-target)) - "@" network-name)) - ((and network-name - (not (get-buffer network-name))) - (when erc-rename-buffers - (rename-buffer network-name)) - network-name) - (t (buffer-name (current-buffer)))))) + (if-let ((erc--buffer-target) + (name (if-let ((esid (erc--sid-symbol erc--session))) + (symbol-name esid) + (erc-shorten-server-name (or erc-server-announced-name + erc-session-server))))) + (concat (erc--target-string erc--buffer-target) "@" name) + (buffer-name))) (defun erc-format-away-status () "Return a formatted `erc-mode-line-away-status-format' if `erc-away' is non-nil." @@ -7007,18 +7129,22 @@ erc-format-message ;; FIXME: Don't set the hook globally! (add-hook 'kill-buffer-hook #'erc-kill-buffer-function) -(defcustom erc-kill-server-hook '(erc-kill-server) - "Invoked whenever a server buffer is killed via `kill-buffer'." +(defcustom erc-kill-server-hook '(erc-kill-server + erc--shrink-ids-and-buffer-names) + "Invoked whenever a live server buffer is killed via `kill-buffer'." :group 'erc-hooks :type 'hook) -(defcustom erc-kill-channel-hook '(erc-kill-channel) +(defcustom erc-kill-channel-hook '(erc-kill-channel + erc--shrink-ids-and-buffer-names + erc--maybe-rename-surviving-target-buffer) "Invoked whenever a channel-buffer is killed via `kill-buffer'." :group 'erc-hooks :type 'hook) -(defcustom erc-kill-buffer-hook nil - "Hook run whenever a non-server or channel buffer is killed. +(defcustom erc-kill-buffer-hook '(erc--shrink-ids-and-buffer-names + erc--maybe-rename-surviving-target-buffer) + "Hook run whenever a query buffer or a dead server buffer is killed. See also `kill-buffer'." :group 'erc-hooks diff --git a/test/lisp/erc/erc-networks-tests.el b/test/lisp/erc/erc-networks-tests.el new file mode 100644 index 0000000000..08781d2eb0 --- /dev/null +++ b/test/lisp/erc/erc-networks-tests.el @@ -0,0 +1,525 @@ +;;; erc-networks-tests.el --- Tests for erc-networks. -*- lexical-binding:t -*- + +;; Copyright (C) 2020-2021 Free Software Foundation, Inc. + +;; This file is part of GNU Emacs. +;; +;; GNU Emacs is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published +;; by the Free Software Foundation, either version 3 of the License, +;; or (at your option) any later version. +;; +;; GNU Emacs is distributed in the hope that it will be useful, but +;; WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +;; General Public License for more details. +;; +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs. If not, see . + +;;; Code: + +(require 'ert-x) ; cl-lib + +(require 'erc-networks) + +;; FIXME maybe create common helpers file; these three are copied from +;; test/lisp/erc/erc-tests.el + +(defun erc-tests--create-live-proc (&optional buf) + (let ((proc (start-process "sleep" (or buf (current-buffer)) "sleep" "1"))) + (set-process-query-on-exit-flag proc nil) + proc)) + +(defun erc-tests--create-dead-proc (&optional buf) + (let ((p (start-process "true" (or buf (current-buffer)) "true"))) + (while (process-live-p p) (sit-for 0.1)) + p)) + +(defun erc-tests--clean-bufs () + (let (erc-kill-channel-hook + erc-kill-server-hook + erc-kill-buffer-hook) + (dolist (buf (erc-buffer-list)) + (kill-buffer buf)))) + +(ert-deftest erc-networks--set-name () + (with-current-buffer (get-buffer-create "localhost:6667") + (let (erc-server-announced-name + erc-isupport-parameters + erc-network + calls) + (erc-mode) + (cl-letf (((symbol-function 'erc-display-line-1) + (lambda (&rest r) (push r calls)))) + (ert-info ("Errors when `erc-server-announced-name' unset") + (should-error (erc-networks--set-name nil (make-erc-response)))) + (should-not calls) + (setq erc-server-announced-name "irc.fake.gnu.org") + (ert-info ("Errors out when table empty and NETWORK param unset") + (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 (string-match-p "*** Failed" (car (pop calls))))))) + (erc-tests--clean-bufs))) + +(ert-deftest erc-networks--rename-server-buffer--no-existing--orphan () + (with-current-buffer (get-buffer-create "#chan") + (erc-mode) + (setq erc-network 'FooNet + erc-server-current-nick "tester" + erc--buffer-target (erc--target-from-string "#chan") + erc--session (erc--sid-create nil))) + + (with-current-buffer (get-buffer-create "irc.foonet.org") + (erc-mode) + (setq erc-network 'FooNet + erc-server-current-nick "tester" + erc-server-process (erc-tests--create-live-proc) + erc--session (erc--sid-create nil)) + (should-not (erc-networks--rename-server-buffer erc-server-process)) + (should (string= (buffer-name) "FooNet"))) + + (ert-info ("Channel buffer reassociated") + (erc-server-process-alive "#chan") + (with-current-buffer "#chan" + (should erc-server-connected) + (erc-with-server-buffer + (should (string= (buffer-name) "FooNet"))))) + + (erc-tests--clean-bufs)) + +(ert-deftest erc-networks--rename-server-buffer--existing--reuse () + (let* ((old-buf (get-buffer-create "FooNet")) + (old-proc (erc-tests--create-dead-proc old-buf))) + + (with-current-buffer old-buf + (erc-mode) + (insert "*** Old buf") + (setq erc-network 'FooNet + erc-server-current-nick "tester" + erc-insert-marker (set-marker (make-marker) (point-max)) + erc-server-process old-proc + erc--session (erc--sid-create nil))) + + (with-current-buffer (get-buffer-create "#chan") + (erc-mode) + (setq erc-network 'FooNet + erc-server-process old-proc + erc--session (erc--sid-create nil) + erc--buffer-target (erc--target-from-string "#chan"))) + + (ert-info ("New buffer steals name, content") + (with-current-buffer (get-buffer-create "irc.foonet.org") + (erc-mode) + (setq erc-network 'FooNet + erc-server-current-nick "tester" + erc-server-process (erc-tests--create-live-proc) + erc--session (erc--sid-create nil)) + (should-not (erc-networks--rename-server-buffer erc-server-process)) + (should (string= (buffer-name) "FooNet")) + (goto-char (point-min)) + (should (search-forward "Old buf")))) + + (ert-info ("Channel buffer reassociated") + (erc-server-process-alive "#chan") + (with-current-buffer "#chan" + (should erc-server-connected) + (should-not (eq erc-server-process old-proc)) + (erc-with-server-buffer + (should (string= (buffer-name) "FooNet"))))) + + (ert-info ("Original buffer killed off") + (should-not (buffer-live-p old-buf)))) + + (erc-tests--clean-bufs)) + +(ert-deftest erc-networks--rename-server-buffer--existing--noreuse () + (should erc-reuse-buffers) ; default + (let* ((old-buf (get-buffer-create "FooNet")) + (old-proc (erc-tests--create-dead-proc old-buf)) + erc-reuse-buffers) + (with-current-buffer old-buf + (erc-mode) + (insert "*** Old buf") + (setq erc-network 'FooNet + erc-server-current-nick "tester" + erc-insert-marker (set-marker (make-marker) (point-max)) + erc-server-process old-proc + erc--session (erc--sid-create nil))) + (with-current-buffer (get-buffer-create "#chan") + (erc-mode) + (setq erc-network 'FooNet + erc-server-process old-proc + erc--session (erc--sid-create nil) + erc--buffer-target (erc--target-from-string "#chan"))) + + (ert-info ("Server buffer uniquely renamed") + (with-current-buffer (get-buffer-create "irc.foonet.org") + (erc-mode) + (setq erc-network 'FooNet + erc-server-current-nick "tester" + erc-server-process (erc-tests--create-live-proc) + erc--session (erc--sid-create nil)) + (should-not (erc-networks--rename-server-buffer erc-server-process)) + (should (string= (buffer-name) "FooNet<2>")) + (goto-char (point-min)) + (should-not (search-forward "Old buf" nil t)))) + + (ert-info ("Channel buffer reassociated") + (erc-server-process-alive "#chan") + (with-current-buffer "#chan" + (should erc-server-connected) + (should-not (eq erc-server-process old-proc)) + (erc-with-server-buffer + (should (string= (buffer-name) "FooNet<2>"))))) + + (ert-info ("Old buffer still around") + (should (buffer-live-p old-buf)))) + + (erc-tests--clean-bufs)) + +(ert-deftest erc-networks--rename-server-buffer--reconnecting () + (let* ((old-buf (get-buffer-create "FooNet")) + (old-proc (erc-tests--create-dead-proc old-buf))) + + (with-current-buffer old-buf + (erc-mode) + (insert "*** Old buf") + (setq erc-network 'FooNet + erc-server-current-nick "tester" + erc-insert-marker (set-marker (make-marker) (point-max)) + erc-server-process old-proc + erc--session (erc--sid-create nil))) + + (with-current-buffer (get-buffer-create "#chan") + (erc-mode) + (setq erc-network 'FooNet + erc-server-process old-proc + erc--buffer-target (erc--target-from-string "#chan") + erc--session (erc--sid-create nil))) + + (ert-info ("No new buffer") + (with-current-buffer old-buf + (setq erc-server-process (erc-tests--create-live-proc)) + (should-not (erc-networks--rename-server-buffer erc-server-process)) + (should (string= (buffer-name) "FooNet")) + (goto-char (point-min)) + (should (search-forward "Old buf")))) + + (ert-info ("Channel buffer updated with live proc") + (erc-server-process-alive "#chan") + (with-current-buffer "#chan" + (should erc-server-connected) + (should-not (eq erc-server-process old-proc)) + (erc-with-server-buffer + (should (string= (buffer-name) "FooNet")))))) + + (erc-tests--clean-bufs)) + +(ert-deftest erc-networks--rename-server-buffer--session-id () + (let* ((old-buf (get-buffer-create "MySession")) + (old-proc (erc-tests--create-dead-proc old-buf))) + + (with-current-buffer old-buf + (erc-mode) + (insert "*** Old buf") + (setq erc-network 'FooNet + erc--session (erc--sid-create 'MySession) + erc-insert-marker (set-marker (make-marker) (point-max)) + erc-server-process old-proc)) + + (with-current-buffer (get-buffer-create "#chan") + (erc-mode) + (setq erc-network 'FooNet + erc--session (erc--sid-create 'MySession) + erc-server-process old-proc + erc--buffer-target (erc--target-from-string "#chan"))) + + (ert-info ("No new buffer") + (with-current-buffer old-buf + (setq erc-server-process (erc-tests--create-live-proc)) + (should-not (erc-networks--rename-server-buffer erc-server-process)) + (should (string= (buffer-name) "MySession")) + (goto-char (point-min)) + (should (search-forward "Old buf")))) + + (ert-info ("Channel buffer updated with live proc") + (erc-server-process-alive "#chan") + (with-current-buffer "#chan" + (should erc-server-connected) + (should-not (eq erc-server-process old-proc)) + (erc-with-server-buffer + (should (string= (buffer-name) "MySession")))))) + + (erc-tests--clean-bufs)) + +(ert-deftest erc-networks--rename-server-buffer--existing--live () + (let* (erc-kill-server-hook + erc-insert-modify-hook + (old-buf (get-buffer-create "FooNet")) + (old-proc (erc-tests--create-live-proc old-buf))) ; live + + (with-current-buffer old-buf + (erc-mode) + (insert "*** Old buf") + (setq erc-network 'FooNet + erc-server-current-nick "tester" + erc-insert-marker (set-marker (make-marker) (point-max)) + erc-server-process old-proc + erc--session (erc--sid-create nil)) + (should (erc-server-process-alive))) + + (with-current-buffer (get-buffer-create "#chan") + (erc-mode) + (setq erc-network 'FooNet + erc-server-process old-proc + erc--session (erc--sid-create nil) + erc-server-connected t + erc--buffer-target (erc--target-from-string "#chan"))) + + (ert-info ("New buffer rejected, abandoned, not killed") + (with-current-buffer (get-buffer-create "irc.foonet.org") + (erc-mode) + (setq erc-network 'FooNet + erc-server-current-nick "tester" + erc-insert-marker (set-marker (make-marker) (point-max)) + erc-server-process (erc-tests--create-live-proc) + erc--session (erc--sid-create nil)) + (should-not (erc-networks--rename-server-buffer erc-server-process)) + (should (eq erc-active-buffer old-buf)) + (should-not (erc-server-process-alive)) + (should (string= (buffer-name) "irc.foonet.org")) + (goto-char (point-min)) + (search-forward "still connected"))) + + (ert-info ("Channel buffer updated with live proc") + (should (erc-server-process-alive "#chan")) + (with-current-buffer "#chan" + (should erc-server-connected) + (should (erc-server-buffer-live-p)) + (should (eq erc-server-process old-proc)) + (should (buffer-live-p (process-buffer erc-server-process))) + (with-current-buffer (process-buffer erc-server-process) + (should (eq (current-buffer) (get-buffer "FooNet"))) + (should (eq (current-buffer) old-buf)))))) + + (should (get-buffer "FooNet")) + (should (get-buffer "irc.foonet.org")) + + (erc-tests--clean-bufs)) + +(ert-deftest erc-networks--rename-server-buffer--local-match () + (let* ((old-buf (get-buffer-create "FooNet")) + (old-proc (erc-tests--create-dead-proc old-buf)) + (erc-isupport-parameters '((CHANTYPES "&#")))) + + (with-current-buffer old-buf + (erc-mode) + (insert "*** Old buf") + (setq erc-network 'FooNet + erc-server-current-nick "tester" + erc-server-announced-name "us-east.foonet.org" + erc-insert-marker (set-marker (make-marker) (point-max)) + erc-server-process old-proc + erc--session (erc--sid-create nil))) + + (with-current-buffer (get-buffer-create "&chan") + (erc-mode) + (setq erc-network 'FooNet + erc-server-process old-proc + erc-server-announced-name "us-east.foonet.org" + erc--buffer-target (erc--target-from-string "&chan") + erc--session (erc--sid-create nil))) + + (ert-info ("New server buffer steals name, content") + (with-current-buffer (get-buffer-create "irc.foonet.org") + (erc-mode) + (setq erc-network 'FooNet + erc-server-current-nick "tester" + erc-server-announced-name "us-east.foonet.org" + erc-server-process (erc-tests--create-live-proc) + erc--session (erc--sid-create nil)) + (should-not (erc-networks--rename-server-buffer erc-server-process)) + (should (string= (buffer-name) "FooNet")) + (goto-char (point-min)) + (should (search-forward "Old buf")))) + + (ert-info ("Channel buffer reassociated when &local server matches") + (should (erc-server-process-alive "&chan")) + (with-current-buffer "&chan" + (should erc-server-connected) + (should-not (eq erc-server-process old-proc)) + (erc-with-server-buffer + (should (string= (buffer-name) "FooNet"))))) + + (ert-info ("Original buffer killed off") + (should-not (buffer-live-p old-buf))) + + (erc-tests--clean-bufs))) + +(ert-deftest erc-networks--rename-server-buffer--local-nomatch () + (let* ((old-buf (get-buffer-create "FooNet")) + (old-proc (erc-tests--create-dead-proc old-buf)) + (erc-isupport-parameters '((CHANTYPES "&#")))) + + (with-current-buffer old-buf + (erc-mode) + (insert "*** Old buf") + (setq erc-network 'FooNet + erc-server-current-nick "tester" + erc-server-announced-name "us-west.foonet.org" + erc-insert-marker (set-marker (make-marker) (point-max)) + erc-server-process old-proc + erc--session (erc--sid-create nil))) + + (with-current-buffer (get-buffer-create "&chan") + (erc-mode) + (setq erc-network 'FooNet + erc-server-process old-proc + erc-server-announced-name "us-west.foonet.org" ; west + erc--buffer-target (erc--target-from-string "&chan") + erc--session (erc--sid-create nil))) + + (ert-info ("New server buffer steals name, content") + (with-current-buffer (get-buffer-create "irc.foonet.org") + (erc-mode) + (setq erc-network 'FooNet + erc-server-current-nick "tester" + erc-server-announced-name "us-east.foonet.org" ; east + erc-server-process (erc-tests--create-live-proc) + erc--session (erc--sid-create nil)) + (should-not (erc-networks--rename-server-buffer erc-server-process)) + (should (string= (buffer-name) "FooNet")) + (goto-char (point-min)) + (should (search-forward "Old buf")))) + + (ert-info ("Channel buffer now orphaned even though network matches") + (should-not (erc-server-process-alive "&chan")) + (with-current-buffer "&chan" + (should-not erc-server-connected) + (should (eq erc-server-process old-proc)) + (erc-with-server-buffer + (should (string= (buffer-name) "FooNet"))))) + + (ert-info ("Original buffer killed off") + (should-not (buffer-live-p old-buf))) + + (erc-tests--clean-bufs))) + +(ert-deftest erc-networks--update-server-session--double-existing () + (with-temp-buffer + (erc-mode) + (setq erc--session (make-erc--sid-dynamic :parts [foonet "bob"] :len 1)) + + (with-current-buffer (get-buffer-create "#chan@foonet/bob") + (erc-mode) + (setq erc--session (make-erc--sid-dynamic :parts [foonet "bob"] :len 2))) + (with-current-buffer (get-buffer-create "foonet/alice") + (erc-mode) + (setq erc--session + (make-erc--sid-dynamic :parts [foonet "alice"] :len 2))) + + (ert-info ("Adopt equivalent session") + (should (eq (erc-networks--update-server-session) + (with-current-buffer "#chan@foonet/bob" erc--session)))) + + (ert-info ("Ignore non-matches") + (should-not (erc-networks--update-server-session)) + (should (eq erc--session + (with-current-buffer "#chan@foonet/bob" erc--session))))) + + (erc-tests--clean-bufs)) + +(ert-deftest erc-networks--update-server-session--double-new () + (with-temp-buffer + (erc-mode) + (setq erc--session (make-erc--sid-dynamic :parts [foonet "bob"] :len 1)) + + (with-current-buffer (get-buffer-create "foonet/alice") + (erc-mode) + (setq erc--session + (make-erc--sid-dynamic :parts [foonet "alice"] :len 2))) + (with-current-buffer (get-buffer-create "#chan@foonet/alice") + (erc-mode) + (setq erc--session (with-current-buffer "foonet/alice" erc--session))) + + (ert-info ("Evolve session to prevent ambiguity") + (should-not (erc-networks--update-server-session)) + (should (= (erc--sid-dynamic-len erc--session) 2)) + (should (eq (erc--sid-symbol erc--session) 'foonet/bob)))) + + (erc-tests--clean-bufs)) + +(ert-deftest erc-networks--update-server-session--double-bounded () + (with-temp-buffer + (erc-mode) + (setq erc--session (make-erc--sid-dynamic :parts [foonet "bob"] :len 1)) + + (with-current-buffer (get-buffer-create "foonet/alice/home") + (erc-mode) + (setq erc--session (make-erc--sid-dynamic :parts [foonet "alice" home] + :len 3))) + (with-current-buffer (get-buffer-create "#chan@foonet/alice/home") + (erc-mode) + (setq erc--session (with-current-buffer "foonet/alice/home" + erc--session))) + + (ert-info ("Evolve session to prevent ambiguity") + (should-not (erc-networks--update-server-session)) + (should (= (erc--sid-dynamic-len erc--session) 2)) + (should (eq (erc--sid-symbol erc--session) 'foonet/bob)))) + + (erc-tests--clean-bufs)) + +(ert-deftest erc-networks--update-server-session--double-even () + (with-temp-buffer + (erc-mode) + (setq erc--session (make-erc--sid-dynamic :parts [foonet "bob"] :len 1)) + + (with-current-buffer (get-buffer-create "foonet") + (erc-mode) + (setq erc--session + (make-erc--sid-dynamic :parts [foonet "alice"] :len 1))) + (with-current-buffer (get-buffer-create "#chan") + (erc-mode) + (setq erc--buffer-target (erc--target-from-string "#chan")) + (setq erc--session (with-current-buffer "foonet" erc--session))) + + (ert-info ("Evolve session to prevent ambiguity") + (should-not (erc-networks--update-server-session)) + (should (= (erc--sid-dynamic-len erc--session) 2)) + (should (eq (erc--sid-symbol erc--session) 'foonet/bob))) + + (ert-info ("Collision renamed") + (with-current-buffer "foonet/alice" + (should (eq (erc--sid-symbol erc--session) 'foonet/alice))) + + (with-current-buffer "#chan@foonet/alice" + (should (eq (erc--sid-symbol erc--session) 'foonet/alice))))) + + (erc-tests--clean-bufs)) + +(ert-deftest erc-networks--update-server-session--triple-new () + (with-temp-buffer + (erc-mode) + (setq erc--session + (make-erc--sid-dynamic :parts [foonet "bob" home] :len 1)) + + (with-current-buffer (get-buffer-create "foonet/bob/office") + (erc-mode) + (setq erc--session (make-erc--sid-dynamic :parts [foonet "bob" office] + :len 3))) + (with-current-buffer (get-buffer-create "#chan@foonet/bob/office") + (erc-mode) + (setq erc--session (with-current-buffer "foonet/bob/office" + erc--session))) + + (ert-info ("Extend our session name so that it's unique") + (should-not (erc-networks--update-server-session)) + (should (= (erc--sid-dynamic-len erc--session) 3)))) + + (erc-tests--clean-bufs)) + +;;; erc-networks-tests.el ends here diff --git a/test/lisp/erc/erc-tests.el b/test/lisp/erc/erc-tests.el index d9112c7b0a..8d078a2053 100644 --- a/test/lisp/erc/erc-tests.el +++ b/test/lisp/erc/erc-tests.el @@ -318,8 +318,9 @@ erc-log-irc-protocol (erc-log-irc-protocol ":irc.gnu.org 001 tester :Welcome") (erc-log-irc-protocol ":irc.gnu.org 002 tester :Your host is irc.gnu.org") (setq erc-network 'FooNet) + (setq erc--session (erc--sid-create nil)) (erc-log-irc-protocol ":irc.gnu.org 422 tester :MOTD missing") - (setq erc-network 'BarNet) + (setq erc--session (erc--sid-create 'BarNet)) (erc-log-irc-protocol ":irc.gnu.org 221 tester +i") (set-process-query-on-exit-flag erc-server-process nil))) (with-current-buffer "*erc-protocol*" @@ -410,4 +411,1020 @@ erc-process-input-line (should-not calls)))))) +(defun erc-tests--create-dead-proc (&optional buf) + (let ((p (start-process "true" (or buf (current-buffer)) "true"))) + (while (process-live-p p) (sit-for 0.1)) + p)) + +(defun erc-tests--create-live-proc (&optional buf) + (let ((proc (start-process "sleep" (or buf (current-buffer)) "sleep" "1"))) + (set-process-query-on-exit-flag proc nil) + proc)) + +(defun erc-tests--clean-bufs () + (let (erc-kill-channel-hook + erc-kill-server-hook + erc-kill-buffer-hook) + (dolist (buf (erc-buffer-list)) + (kill-buffer buf)))) + +(defun erc-tests--bufnames (prefix) + (let* ((case-fold-search) + (pred (lambda (b) (string-prefix-p prefix (buffer-name b)))) + (prefixed (seq-filter pred (buffer-list)))) + (sort (mapcar #'buffer-name prefixed) #'string<))) + +(ert-deftest erc--sid () + (cl-letf (((symbol-function 'float-time) + (lambda () 0.0))) + + ;; Fixed + (should (equal (erc--sid-fixed-create 'foo) + (make-erc--sid-fixed :ts (float-time) :symbol 'foo))) + + ;; Dynamic + (let* ((erc-network 'FooNet) + (erc-server-current-nick "Joe") + (session (erc--sid-create nil))) + + (should (equal session #s(erc--sid-dynamic 0.0 FooNet [FooNet "joe"] 1))) + + (should (equal (erc--sid-dynamic-grow-id session) 'FooNet/joe)) + (should (equal session + #s(erc--sid-dynamic 0.0 FooNet/joe [FooNet "joe"] 2))) + (should-not (erc--sid-dynamic-grow-id session)) + (should (equal session + #s(erc--sid-dynamic 0.0 FooNet/joe [FooNet "joe"] 2)))))) + +(ert-deftest erc--sid-dynamic-prefix-length () + (should-not (erc--sid-dynamic-prefix-length + (make-erc--sid-dynamic) + (make-erc--sid-dynamic))) + + (should-not (erc--sid-dynamic-prefix-length + (make-erc--sid-dynamic :parts [1 2]) + (make-erc--sid-dynamic :parts [2 3]))) + + (should (= 1 (erc--sid-dynamic-prefix-length + (make-erc--sid-dynamic :parts [1]) + (make-erc--sid-dynamic :parts [1 2])))) + + (should (= 1 (erc--sid-dynamic-prefix-length + (make-erc--sid-dynamic :parts [1 2]) + (make-erc--sid-dynamic :parts [1 3])))) + + (should (= 2 (erc--sid-dynamic-prefix-length + (make-erc--sid-dynamic :parts [1 2]) + (make-erc--sid-dynamic :parts [1 2])))) + + (should (= 1 (erc--sid-dynamic-prefix-length + (make-erc--sid-dynamic :parts ["1"]) + (make-erc--sid-dynamic :parts ["1"]))))) + +(ert-deftest erc--sid-sort-buffers () + (let (oldest middle newest) + + (with-temp-buffer + (setq erc--session (erc--sid-fixed-create 'oldest) + oldest (current-buffer)) + + (with-temp-buffer + (setq erc--session (erc--sid-fixed-create 'middle) + middle (current-buffer)) + + (with-temp-buffer + (setq erc--session (erc--sid-fixed-create 'newest) + newest (current-buffer)) + + (should (equal (erc--sid-sort-buffers (list oldest newest middle)) + (list newest middle oldest)))))))) + +(ert-deftest erc--maybe-rename-surviving-target-buffer--channel () + (should (memq #'erc--maybe-rename-surviving-target-buffer + erc-kill-channel-hook)) + + (let ((chan-foonet-buffer (get-buffer-create "#chan@foonet"))) + + (with-current-buffer chan-foonet-buffer + (erc-mode) + (setq erc--session (make-erc--sid-dynamic :parts [foonet "bob"] :len 1)) + (setq erc--buffer-target (erc--target-from-string "#chan"))) + + (with-current-buffer (get-buffer-create "#chan@barnet") + (erc-mode) + (setq erc--session (make-erc--sid-dynamic :parts [barnet "bob"] :len 1)) + (setq erc--buffer-target (erc--target-from-string "#chan"))) + + (kill-buffer "#chan@barnet") + (should (equal (erc-tests--bufnames "#chan") '("#chan"))) + (should (eq chan-foonet-buffer (get-buffer "#chan")))) + + (erc-tests--clean-bufs)) + +(ert-deftest erc--maybe-rename-surviving-target-buffer--query () + (should (memq #'erc--maybe-rename-surviving-target-buffer + erc-kill-buffer-hook)) + + (let ((bob-foonet (get-buffer-create "bob@foonet"))) + + (with-current-buffer bob-foonet + (erc-mode) + (setq erc--session (make-erc--sid-dynamic :parts [foonet "bob"] :len 1)) + (setq erc--buffer-target (erc--target-from-string "bob"))) + + (with-current-buffer (get-buffer-create "bob@barnet") + (erc-mode) + (setq erc--session (make-erc--sid-dynamic :parts [barnet "bob"] :len 1)) + (setq erc--buffer-target (erc--target-from-string "bob"))) + + (kill-buffer "bob@barnet") + (should (equal (erc-tests--bufnames "bob") '("bob"))) + (should (eq bob-foonet (get-buffer "bob")))) + + (erc-tests--clean-bufs)) + +(ert-deftest erc--maybe-rename-surviving-target-buffer--multi () + + (ert-info ("Multiple leftover channels untouched") + (with-current-buffer (get-buffer-create "#chan@foonet") + (erc-mode) + (setq erc--buffer-target (erc--target-from-string "#chan"))) + (with-current-buffer (get-buffer-create "#chan@barnet") + (erc-mode) + (setq erc--buffer-target (erc--target-from-string "#chan"))) + (with-current-buffer (get-buffer-create "#chan@baznet") + (erc-mode) + (setq erc--buffer-target (erc--target-from-string "#chan"))) + (kill-buffer "#chan@baznet") + (should (equal (erc-tests--bufnames "#chan") + '("#chan@barnet" "#chan@foonet"))) + (erc-tests--clean-bufs)) + + (ert-info ("Multiple leftover queries untouched") + (with-current-buffer (get-buffer-create "bob@foonet") + (erc-mode) + (setq erc--buffer-target (erc--target-from-string "bob"))) + (with-current-buffer (get-buffer-create "bob@barnet") + (erc-mode) + (setq erc--buffer-target (erc--target-from-string "bob"))) + (with-current-buffer (get-buffer-create "bob@baznet") + (erc-mode) + (setq erc--buffer-target (erc--target-from-string "bob"))) + (kill-buffer "bob@baznet") + (should (equal (erc-tests--bufnames "bob") '("bob@barnet" "bob@foonet"))) + (erc-tests--clean-bufs))) + +(ert-deftest erc--shrink-ids-and-buffer-names-perform--outstanding () + ;; Not collapsed because we have one collision outstanding. + ;; + ;; Overlaps with quite a bit with the + ;; `erc--shrink-ids-and-buffer-names--hook-outstanding-*' stuff + ;; below. If this ever fails, just delete this and fix those. + + ;; Presumably, some buffer foonet/chester was just killed + (with-current-buffer (get-buffer-create "foonet/tester") + (erc-mode) + (setq erc-network 'foonet + erc-server-current-nick "tester" + erc--session (make-erc--sid-dynamic :symbol 'foonet/tester + :parts [foonet "tester"] + :len 2) + erc-server-process (erc-tests--create-live-proc))) + + (with-current-buffer (get-buffer-create + (elt ["#a" "#a@foonet" "#a@foonet/tester"] (random 3))) + (erc-mode) + (setq erc-server-process (with-current-buffer "foonet/tester" + erc-server-process) + erc-network 'foonet + erc-server-current-nick "tester" + erc--session (with-current-buffer "foonet/tester" erc--session) + erc--buffer-target (erc--target-from-string "#a"))) + + (with-current-buffer (get-buffer-create "barnet/tester") + (erc-mode) + (setq erc-network 'barnet + erc-server-current-nick "tester" + erc--session (make-erc--sid-dynamic :symbol 'barnet/tester + :parts [barnet "tester"] + :len 2) + erc-server-process (erc-tests--create-live-proc))) + + (with-current-buffer (get-buffer-create "barnet/chester") + (erc-mode) + (setq erc-network 'barnet + erc-server-current-nick "chester" + erc--session (make-erc--sid-dynamic :symbol 'barnet/chester + :parts [barnet "chester"] + :len 2) + erc-server-process (erc-tests--create-live-proc))) + + ;; Presumably, some buffer #a@barnet/chester was just killed + (with-current-buffer (get-buffer-create + (elt ["#a@barnet" "#a@barnet/tester"] (random 2))) + (erc-mode) + (setq erc-network 'barnet + erc-server-current-nick "tester" + erc-server-process (with-current-buffer "barnet/tester" + erc-server-process) + erc--session (with-current-buffer "barnet/tester" erc--session) + erc--buffer-target (erc--target-from-string "#a"))) + + (with-temp-buffer + (setq erc--session (make-erc--sid-dynamic)) + (erc--shrink-ids-and-buffer-names)) + + (should (equal (mapcar #'buffer-name (erc-buffer-list)) + '("foonet" + "#a@foonet" + "barnet/tester" + "barnet/chester" + "#a@barnet/tester"))) + + (erc-tests--clean-bufs)) + +(ert-deftest erc--shrink-ids-and-buffer-names-perform--collapse () + ;; Overlaps with `erc--shrink-ids-and-buffer-names--collapse-hook-*' + ;; quite a bit. If this ever fails, just delete it and fix ^. + + ;; Presumably, some buffer foonet/chester was just killed + (with-current-buffer (get-buffer-create "foonet/tester") + (erc-mode) + (setq erc-network 'foonet + erc-server-current-nick "tester" + erc--session (make-erc--sid-dynamic :symbol 'foonet/tester + :parts [foonet "tester"] + :len 2) + erc-server-process (erc-tests--create-live-proc))) + + (with-current-buffer + (get-buffer-create (elt ["#a" "#a@foonet/tester"] (random 2))) + (erc-mode) + (setq erc-server-process (with-current-buffer "foonet/tester" + erc-server-process) + erc-network 'foonet + erc-server-current-nick "tester" + erc--session (with-current-buffer "foonet/tester" erc--session) + erc--buffer-target (erc--target-from-string "#a"))) + + (with-current-buffer (get-buffer-create "barnet/tester") + (erc-mode) + (setq erc-network 'barnet + erc-server-current-nick "tester" + erc--session (make-erc--sid-dynamic :symbol 'barnet/tester + :parts [barnet "tester"] + :len 2) + erc-server-process (erc-tests--create-live-proc))) + + (with-current-buffer + (get-buffer-create (elt ["#b" "#b@foonet/tester"] (random 2))) + (erc-mode) + (setq erc-network 'barnet + erc-server-current-nick "tester" + erc-server-process (with-current-buffer "barnet/tester" + erc-server-process) + erc--session (with-current-buffer "barnet/tester" erc--session) + erc--buffer-target (erc--target-from-string "#b"))) + + (with-temp-buffer + (setq erc--session (make-erc--sid-dynamic)) + (erc--shrink-ids-and-buffer-names)) + + (should (equal (mapcar #'buffer-name (erc-buffer-list)) + '("foonet" "#a" "barnet" "#b"))) + + (erc-tests--clean-bufs)) + +(defun erc--shrink-ids-and-buffer-names--hook-outstanding-common () + + (with-current-buffer (get-buffer-create "foonet/tester") + (erc-mode) + (setq erc-network 'foonet + erc-server-current-nick "tester" + erc--session (make-erc--sid-dynamic :symbol 'foonet/tester + :parts [foonet "tester"] + :len 2) + erc-server-process (erc-tests--create-live-proc))) + + (with-current-buffer (get-buffer-create "#a@foonet/tester") + (erc-mode) + (setq erc-server-process (with-current-buffer "foonet/tester" + erc-server-process) + erc-network 'foonet + erc-server-current-nick "tester" + erc--session (with-current-buffer "foonet/tester" erc--session) + erc--buffer-target (erc--target-from-string "#a"))) + + (with-current-buffer (get-buffer-create "barnet/tester") + (erc-mode) + (setq erc-network 'barnet + erc-server-current-nick "tester" + erc--session (make-erc--sid-dynamic :symbol 'barnet/tester + :parts [barnet "tester"] + :len 2) + erc-server-process (erc-tests--create-live-proc))) + + (with-current-buffer (get-buffer-create "barnet/chester") + (erc-mode) + (setq erc-network 'barnet + erc-server-current-nick "chester" + erc--session (make-erc--sid-dynamic :symbol 'barnet/chester + :parts [barnet "chester"] + :len 2) + erc-server-process (erc-tests--create-live-proc))) + + (with-current-buffer (get-buffer-create "#a@barnet/tester") + (erc-mode) + (setq erc-network 'barnet + erc-server-current-nick "tester" + erc-server-process (with-current-buffer "barnet/tester" + erc-server-process) + erc--session (with-current-buffer "barnet/tester" erc--session) + erc--buffer-target (erc--target-from-string "#a")))) + +(ert-deftest erc--shrink-ids-and-buffer-names--hook-outstanding-server () + (erc--shrink-ids-and-buffer-names--hook-outstanding-common) + (with-current-buffer (get-buffer-create "foonet/chester") + (erc-mode) + (setq erc-network 'foonet + erc-server-current-nick "chester" + erc--session (make-erc--sid-dynamic :symbol 'foonet/chester + :parts [foonet "chester"] + :len 2) + erc-server-process (erc-tests--create-live-proc))) + + (with-current-buffer "foonet/chester" (kill-buffer)) + + (should (equal (mapcar #'buffer-name (erc-buffer-list)) + '("foonet" + "#a@foonet" + "barnet/tester" + "barnet/chester" + "#a@barnet/tester"))) + (erc-tests--clean-bufs)) + +(ert-deftest erc--shrink-ids-and-buffer-names--hook-outstanding-target () + (erc--shrink-ids-and-buffer-names--hook-outstanding-common) + (with-current-buffer (get-buffer-create "#a@foonet/chester") + (erc-mode) + (setq erc-network 'foonet + erc-server-current-nick "chester" + erc--session (make-erc--sid-dynamic :symbol 'foonet/chester + :parts [foonet "chester"] + :len 2) + erc--buffer-target (erc--target-from-string "#a") + erc-server-process (with-temp-buffer + (erc-tests--create-dead-proc)))) + + (with-current-buffer "#a@foonet/chester" (kill-buffer)) + + ;; Identical to *-server variant above + (should (equal (mapcar #'buffer-name (erc-buffer-list)) + '("foonet" + "#a@foonet" + "barnet/tester" + "barnet/chester" + "#a@barnet/tester"))) + (erc-tests--clean-bufs)) + +(ert-deftest erc--maybe-rename-surviving-target-buffer--shrink () + (erc--shrink-ids-and-buffer-names--hook-outstanding-common) + + ;; This buffer isn't "#a@foonet" (yet) because the shrink-ids hook + ;; hasn't run. However, when it's the rename hook runs, its session + ;; id *is* "foonet", not "foonet/tester". + (with-current-buffer "#a@foonet/tester" (kill-buffer)) + + (should (equal (mapcar #'buffer-name (erc-buffer-list)) + '("foonet" + "barnet/tester" + "barnet/chester" + "#a"))) + + (erc-tests--clean-bufs)) + +(defun erc--shrink-ids-and-buffer-names--hook-collapse (check) + + (with-current-buffer (get-buffer-create "foonet/tester") + (erc-mode) + (setq erc-network 'foonet + erc-server-current-nick "tester" + erc--session (make-erc--sid-dynamic :symbol 'foonet/tester + :parts [foonet "tester"] + :len 2) + erc-server-process (erc-tests--create-live-proc))) + + (with-current-buffer (get-buffer-create "#a@foonet/tester") + (erc-mode) + (setq erc-server-process (with-current-buffer "foonet/tester" + erc-server-process) + erc-network 'foonet + erc-server-current-nick "tester" + erc--session (with-current-buffer "foonet/tester" erc--session) + erc--buffer-target (erc--target-from-string "#a"))) + + (with-current-buffer (get-buffer-create "barnet/tester") + (erc-mode) + (setq erc-network 'barnet + erc-server-current-nick "tester" + erc--session (make-erc--sid-dynamic :symbol 'barnet/tester + :parts [barnet "tester"] + :len 2) + erc-server-process (erc-tests--create-live-proc))) + + (with-current-buffer (get-buffer-create "#b@foonet/tester") + (erc-mode) + (setq erc-network 'barnet + erc-server-current-nick "tester" + erc-server-process (with-current-buffer "barnet/tester" + erc-server-process) + erc--session (with-current-buffer "barnet/tester" erc--session) + erc--buffer-target (erc--target-from-string "#b"))) + + (funcall check) + + (should (equal (mapcar #'buffer-name (erc-buffer-list)) + '("foonet" "#a" "barnet" "#b"))) + + (erc-tests--clean-bufs)) + +(ert-deftest erc--shrink-ids-and-buffer-names--hook-collapse-server () + (erc--shrink-ids-and-buffer-names--hook-collapse + (lambda () + (with-current-buffer (get-buffer-create "foonet/chester") + (erc-mode) + (setq erc-network 'foonet + erc-server-current-nick "chester" + erc--session (make-erc--sid-dynamic :symbol 'foonet/chester + :parts [foonet "chester"] + :len 2) + erc-server-process (erc-tests--create-live-proc))) + + (with-current-buffer "foonet/chester" + (kill-buffer))))) + +(ert-deftest erc--shrink-ids-and-buffer-names--hook-collapse-target () + (erc--shrink-ids-and-buffer-names--hook-collapse + (lambda () + (with-current-buffer (get-buffer-create "#a@foonet/chester") + (erc-mode) + (setq erc-network 'foonet + erc-server-current-nick "chester" + erc--session (make-erc--sid-dynamic :symbol 'foonet/chester + :parts [foonet "chester"] + :len 2) + ;; `erc-kill-buffer-function' uses legacy target detection + ;; but falls back on buffer name, so no need for: + ;; + ;; erc-default-recipients '("#a") + ;; + erc--buffer-target (erc--target-from-string "#a") + erc-server-process (with-temp-buffer + (erc-tests--create-dead-proc)))) + + (with-current-buffer "#a@foonet/chester" (kill-buffer))))) + +;; FIXME this test is old and may describe impossible states: +;; leftover sessions being qual-equal but not eq (implies +;; `erc-networks--maybe-reclaim-target-buffers' is somehow broken). +;; +;; Otherwise, the point of this test is to show that server process +;; identity does not impact the hunt for duplicates. + +(defun erc-tests--prep-erc-reconcile-buffer-names--duplicates (start) + + (with-current-buffer (get-buffer-create "foonet") + (erc-mode) + (setq erc-network 'foonet + erc-server-current-nick "tester" + erc--session (erc--sid-create nil) + erc-server-process (funcall start))) + + (with-current-buffer (get-buffer-create "#chan") ; prior session + (erc-mode) + (setq erc-server-process (with-current-buffer "foonet" erc-server-process) + erc--buffer-target (erc--target-from-string "#chan") + erc--session (erc--sid-create nil))) + + (ert-info ("Conflicts not recognized as ERC buffers and not renamed") + (get-buffer-create "#chan@foonet") + (should (equal (erc-tests--bufnames "#chan") '("#chan" "#chan@foonet")))) + + ;; These are dupes (not "collisions") + + (with-current-buffer "#chan@foonet" ; same proc + (erc-mode) + (setq erc--buffer-target (erc--target-from-string "#chan") + erc-network 'foonet + erc-server-current-nick "tester" + erc-server-process (with-current-buffer "foonet" erc-server-process) + erc--session (erc--sid-create nil))) + + (with-current-buffer (get-buffer-create "#chan@foonet") + (erc-mode) + (setq erc--buffer-target (erc--target-from-string "#chan") + erc-server-process (erc-tests--create-dead-proc) + erc-network 'foonet + erc-server-current-nick "tester" + erc--session (erc--sid-create nil))) + + (with-current-buffer (get-buffer-create "#chan@foonet") + (erc-mode) + (setq erc--buffer-target (erc--target-from-string "#chan") + erc-server-process (erc-tests--create-live-proc) + erc-network 'foonet + erc-server-current-nick "tester" + erc--session (erc--sid-create nil))) + + (let ((created (list (get-buffer "#chan@foonet") + (get-buffer "#chan@foonet") + (get-buffer "#chan@foonet")))) + + (with-current-buffer "foonet" + (should (string= (erc--reconcile-buffer-names + (erc--target-from-string "#chan") erc--session) + "#chan"))) + + (ert-info ("All buffers considered dupes renamed") + (should (equal (erc-tests--bufnames "#chan") + '("#chan" "#chan<2>" "#chan<3>" "#chan<4>")))) + + (ert-info ("All buffers renamed from newest to oldest") + (should (equal created (list (get-buffer "#chan<2>") + (get-buffer "#chan<3>") + (get-buffer "#chan<4>")))))) + + (erc-tests--clean-bufs)) + +(defun erc-tests--prep-erc-reconcile-buffer-names--duplicates-given (start) + + (with-current-buffer (get-buffer-create "oofnet") + (erc-mode) + (setq erc--session (erc--sid-create 'oofnet)) + ;; The network is determined before target bufs are created. At + ;; least this shows that the network doesn't matter when only + ;; assigned (given) IDs are present. + (setq erc-network 'foonet) + (setq erc-server-current-nick "tester") + (setq erc-server-process (funcall start))) + + (with-current-buffer (get-buffer-create "#chan") ; prior session + (erc-mode) + (setq erc--session (erc--sid-create 'oofnet) + erc-server-process (with-current-buffer "oofnet" erc-server-process) + erc--buffer-target (erc--target-from-string "#chan"))) + + (with-current-buffer (get-buffer-create "#chan@oofnet") ;dupe/not collision + (erc-mode) + (setq erc--session (erc--sid-create 'oofnet) + erc-server-process (with-current-buffer "oofnet" erc-server-process) + erc--buffer-target (erc--target-from-string "#chan"))) + + (with-current-buffer "oofnet" + (should (string= (erc--reconcile-buffer-names + (erc--target-from-string "#chan") erc--session) + "#chan"))) + + (ert-info ("All buffers matching target and network renamed") + (should (equal (erc-tests--bufnames "#chan") '("#chan" "#chan<2>")))) + + (erc-tests--clean-bufs)) + +(ert-deftest erc--reconcile-buffer-names--duplicates () + (ert-info ("Process live, no error") + (erc-tests--prep-erc-reconcile-buffer-names--duplicates + #'erc-tests--create-live-proc)) + + (ert-info ("Process live, no error, given ID") + (erc-tests--prep-erc-reconcile-buffer-names--duplicates-given + #'erc-tests--create-live-proc)) + + (ert-info ("Process dead") + (erc-tests--prep-erc-reconcile-buffer-names--duplicates + #'erc-tests--create-dead-proc)) + + (ert-info ("Process dead, given ID") + (erc-tests--prep-erc-reconcile-buffer-names--duplicates-given + #'erc-tests--create-dead-proc))) + +(defun erc-tests--prep-erc-reconcile-buffer-names--no-server-buf (check) + (let ((foonet-proc (with-temp-buffer (erc-tests--create-dead-proc)))) + (with-current-buffer (get-buffer-create "barnet") + (erc-mode) + (setq erc-network 'barnet) + (setq erc-server-current-nick "tester") + (setq erc--session (erc--sid-create nil)) + (setq erc-server-process (erc-tests--create-dead-proc))) + + ;; Different proc and not "qual-equal" (different elts) + (with-current-buffer (get-buffer-create "#chan") + (erc-mode) + (setq erc-network 'foonet) + (setq erc-server-current-nick "tester") + (setq erc--session (erc--sid-create nil)) + (setq erc--buffer-target (erc--target-from-string "#chan")) + (setq erc-server-process foonet-proc)) + (funcall check) + (erc-tests--clean-bufs))) + +(ert-deftest erc--reconcile-buffer-names--no-server-buf () + (ert-info ("Existing #chan buffer respected") + (erc-tests--prep-erc-reconcile-buffer-names--no-server-buf + (lambda () + (with-current-buffer "barnet" + (should (string= (erc--reconcile-buffer-names + (erc--target-from-string "#chan") erc--session) + "#chan@barnet"))) + (ert-info ("Existing #chan buffer found and renamed") + (should (equal (erc-tests--bufnames "#chan") '("#chan@foonet"))))))) + + (ert-info ("Existing #chan buffer") + (erc-tests--prep-erc-reconcile-buffer-names--no-server-buf + (lambda () + (with-current-buffer (get-buffer-create "foonet") + (erc-mode) + (setq erc-network 'foonet) + (setq erc-server-current-nick "tester") + (setq erc--session (erc--sid-create nil)) + (setq erc-server-process (erc-tests--create-dead-proc)) + (should (string= (erc--reconcile-buffer-names + (erc--target-from-string "#chan") erc--session) + "#chan"))) + (ert-info ("Nothing renamed") + (should (equal (erc-tests--bufnames "#chan") '("#chan"))))))) + + (ert-info ("Existing #chan@foonet and #chan@barnet buffers") + (erc-tests--prep-erc-reconcile-buffer-names--no-server-buf + (lambda () + (with-current-buffer "#chan" + (rename-buffer "#chan@foonet")) + (should-not (get-buffer "#chan@barnet")) + (with-current-buffer (get-buffer-create "#chan@barnet") + (erc-mode) + (setq erc--buffer-target (erc--target-from-string "#chan")) + (setq erc-server-process (with-current-buffer "barnet" + erc-server-process)) + (setq erc--session (erc--sid-create nil))) + (with-current-buffer (get-buffer-create "foonet") + (erc-mode) + (setq erc-network 'foonet) + (setq erc-server-current-nick "tester") + (setq erc-server-process (erc-tests--create-live-proc)) + (setq erc--session (erc--sid-create nil)) + (set-process-query-on-exit-flag erc-server-process nil) + (should (string= (erc--reconcile-buffer-names + (erc--target-from-string "#chan") erc--session) + "#chan@foonet"))) + (ert-info ("Nothing renamed") + (should (equal (erc-tests--bufnames "#chan") + '("#chan@barnet" "#chan@foonet")))))))) + +(defun erc-tests--prep-erc-reconcile-buffer-names--no-server-buf-given (check) + (let ((oofnet-proc (with-temp-buffer (erc-tests--create-dead-proc)))) + + (with-current-buffer (get-buffer-create "rabnet") + (erc-mode) + ;; Again, given name preempts network lookup (unrealistic but + ;; highlights priorities) + (setq erc--session (erc--sid-create 'rabnet)) + (setq erc-network 'barnet) + (setq erc-server-current-nick "tester") + (setq erc-server-process (erc-tests--create-dead-proc))) + + ;; Session is not "qual-equal" to above + (with-current-buffer (get-buffer-create "#chan") + (erc-mode) + (setq erc--session (erc--sid-create 'oofnet)) + (setq erc-network 'foonet) + (setq erc--buffer-target (erc--target-from-string "#chan")) + (setq erc-server-process oofnet-proc)) + (funcall check) + (erc-tests--clean-bufs))) + +(ert-deftest erc--reconcile-buffer-names--no-server-buf-given () + + (ert-info ("Existing #chan buffer respected") + (erc-tests--prep-erc-reconcile-buffer-names--no-server-buf-given + (lambda () + (with-current-buffer "rabnet" + (should (string= (erc--reconcile-buffer-names + (erc--target-from-string "#chan") erc--session) + "#chan@rabnet"))) + + (ert-info ("Existing #chan buffer found and renamed") + (should (equal (erc-tests--bufnames "#chan") '("#chan@oofnet"))))))) + + (ert-info ("Existing #chan@oofnet and #chan@rabnet buffers") + (erc-tests--prep-erc-reconcile-buffer-names--no-server-buf-given + (lambda () + ;; #chan has already been uniquified (but not grown) + (with-current-buffer "#chan" (rename-buffer "#chan@oofnet")) + (should-not (get-buffer "#chan@rabnet")) + + (with-current-buffer (get-buffer-create "#chan@rabnet") + (erc-mode) + (setq erc--buffer-target (erc--target-from-string "#chan")) + (setq erc-server-process (with-current-buffer "rabnet" + erc-server-process)) + (setq erc--session (with-current-buffer "rabnet" erc--session))) + + (with-current-buffer (get-buffer-create "oofnet") + (erc-mode) + (setq erc-network 'oofnet) + (setq erc-server-current-nick "tester") + (setq erc-server-process (erc-tests--create-live-proc)) + (setq erc--session (erc--sid-create 'oofnet)) ; given + (set-process-query-on-exit-flag erc-server-process nil) + (should (string= (erc--reconcile-buffer-names + (erc--target-from-string "#chan") erc--session) + "#chan@oofnet"))) + + (ert-info ("Nothing renamed") + (should (equal (erc-tests--bufnames "#chan") + '("#chan@oofnet" "#chan@rabnet")))))))) + +;; This is a corner case in which the user previously explicitly +;; assigned an ID via `erc-tls' but has since connected again, this +;; time without one. It would actually probably be better if the +;; given session were to win and the derived got an -suffix. +;; +;; If we just compared session-IDs, the two would match, but they +;; don't here because one has a given name and the other a +;; discovered/assembled one; so they are *not* qual-equal. +(ert-deftest erc--reconcile-buffer-names--no-server-buf-given-mismatch () + ;; Existing #chan buffer *not* respected + (erc-tests--prep-erc-reconcile-buffer-names--no-server-buf-given + (lambda () + (with-current-buffer (get-buffer-create "oofnet") + (erc-mode) + (setq erc-network 'oofnet) + (setq erc-server-current-nick "tester") + (setq erc-server-process (erc-tests--create-dead-proc)) + (setq erc--session (erc--sid-create nil)) ; derived + (should (string= (erc--reconcile-buffer-names + (erc--target-from-string "#chan") erc--session) + "#chan@oofnet"))) + + (ert-info ("Collision renamed but not grown (because it's a given)") + ;; Original chan uniquified and moved out of the way + (should (equal (erc-tests--bufnames "#chan") + '("#chan@oofnet<2>"))))))) + +(defun erc-tests--prep-erc-reconcile-buffer-names--multi-net (check) + + (with-current-buffer (get-buffer-create "foonet") + (erc-mode) + (setq erc-network 'foonet + erc-server-current-nick "tester" + erc-server-process (erc-tests--create-dead-proc) + erc--session (erc--sid-create nil))) ; derived + + (with-current-buffer (get-buffer-create "barnet") + (erc-mode) + (setq erc-network 'barnet + erc-server-current-nick "tester" + erc-server-process (erc-tests--create-dead-proc) + erc--session (erc--sid-create nil))) ; derived + + (with-current-buffer (get-buffer-create (elt ["#chan" "#chan@foonet"] + (random 2))) + (erc-mode) + (setq erc--buffer-target (erc--target-from-string "#chan")) + (cl-multiple-value-setq (erc-server-process erc--session) + (with-current-buffer "foonet" + (list erc-server-process erc--session)))) + + (with-current-buffer (get-buffer-create "#chan@barnet") + (erc-mode) + (setq erc--buffer-target (erc--target-from-string "#chan")) + (cl-multiple-value-setq (erc-server-process erc--session) + (with-current-buffer "barnet" + (list erc-server-process erc--session)))) + + (funcall check) + (erc-tests--clean-bufs)) + +(ert-deftest erc--reconcile-buffer-names--multi-net () + (ert-info ("Same network rename") + (erc-tests--prep-erc-reconcile-buffer-names--multi-net + (lambda () + (with-current-buffer "foonet" + (let ((result (erc--reconcile-buffer-names + (erc--target-from-string "#chan") erc--session))) + (should (string= result "#chan@foonet")))) + + (should (equal (erc-tests--bufnames "#chan") + '("#chan@barnet" "#chan@foonet")))))) + + (ert-info ("Same network keep name") + (erc-tests--prep-erc-reconcile-buffer-names--multi-net + (lambda () + (with-current-buffer "barnet" + (let ((result (erc--reconcile-buffer-names + (erc--target-from-string "#chan") erc--session))) + (should (string= result "#chan@barnet")))) + + (should (equal (erc-tests--bufnames "#chan") + '("#chan@barnet" "#chan@foonet"))))))) + +(defun erc-tests--prep-erc-reconcile-buffer-names--multi-net-given (check) + + (with-current-buffer (get-buffer-create "oofnet") + (erc-mode) + (setq erc-network 'foonet + erc-server-current-nick "tester" + erc--session (erc--sid-create 'oofnet) ; one given + erc-server-process (erc-tests--create-dead-proc))) + + (with-current-buffer (get-buffer-create "rabnet") + (erc-mode) + (setq erc-network 'barnet + erc-server-current-nick "tester" + erc--session (erc--sid-create 'rabnet) ; another given + erc-server-process (erc-tests--create-dead-proc))) + + (with-current-buffer (get-buffer-create (elt ["chan" "#chan@oofnet"] + (random 2))) + (erc-mode) + (setq erc--buffer-target (erc--target-from-string "#chan")) + (cl-multiple-value-setq (erc-server-process erc--session) + (with-current-buffer "oofnet" + (list erc-server-process erc--session)))) + + (with-current-buffer (get-buffer-create "#chan@barnet") + (erc-mode) + (setq erc--buffer-target (erc--target-from-string "#chan")) + (cl-multiple-value-setq (erc-server-process erc--session) + (with-current-buffer "rabnet" + (list erc-server-process erc--session)))) + + (funcall check) + (erc-tests--clean-bufs)) + +(ert-deftest erc--reconcile-buffer-names--multi-net-given () + (ert-info ("Same network rename") + (erc-tests--prep-erc-reconcile-buffer-names--multi-net-given + (lambda () + (with-current-buffer "oofnet" + (let ((result (erc--reconcile-buffer-names + (erc--target-from-string "#chan") erc--session))) + (should (string= result "#chan@oofnet")))) + + (should (equal (erc-tests--bufnames "#chan") + '("#chan@oofnet" "#chan@rabnet")))))) + + (ert-info ("Same network keep name") + (erc-tests--prep-erc-reconcile-buffer-names--multi-net-given + (lambda () + (with-current-buffer "rabnet" + (let ((result (erc--reconcile-buffer-names + (erc--target-from-string "#chan") erc--session))) + (should (string= result "#chan@rabnet")))) + + (should (equal (erc-tests--bufnames "#chan") + '("#chan@oofnet" "#chan@rabnet"))))))) + +(defun erc-tests--prep-erc-reconcile-buffer-names--multi-net-mixed (check) + + (with-current-buffer (get-buffer-create "foonet") + (erc-mode) + (setq erc-network 'foonet + erc-server-current-nick "tester" + erc--session (erc--sid-create nil) ; one derived + erc-server-process (erc-tests--create-dead-proc))) + + (with-current-buffer (get-buffer-create "my-conn") + (erc-mode) + (setq erc-network 'barnet + erc-server-current-nick "tester" + erc--session (erc--sid-create 'my-conn) ; one given + erc-server-process (erc-tests--create-dead-proc))) + + (with-current-buffer (get-buffer-create (elt ["#chan" "#chan@foonet"] + (random 2))) + (erc-mode) + (setq erc--buffer-target (erc--target-from-string "#chan")) + (cl-multiple-value-setq (erc-server-process erc--session) + (with-current-buffer "foonet" + (list erc-server-process erc--session)))) + + (with-current-buffer (get-buffer-create "#chan@my-conn") + (erc-mode) + (setq erc--buffer-target (erc--target-from-string "#chan")) + (cl-multiple-value-setq (erc-server-process erc--session) + (with-current-buffer "my-conn" + (list erc-server-process erc--session)))) + + (funcall check) + (erc-tests--clean-bufs)) + +(ert-deftest erc--reconcile-buffer-names--multi-net-existing () + + (ert-info ("Buf name derived from network") + (erc-tests--prep-erc-reconcile-buffer-names--multi-net-mixed + (lambda () + (with-current-buffer "foonet" + (let ((result (erc--reconcile-buffer-names + (erc--target-from-string "#chan") erc--session))) + (should (string= result "#chan@foonet")))) + + (should (equal (erc-tests--bufnames "#chan") + '("#chan@foonet" "#chan@my-conn")))))) + + (ert-info ("Buf name given") + (erc-tests--prep-erc-reconcile-buffer-names--multi-net-mixed + (lambda () + (with-current-buffer "my-conn" + (let ((result (erc--reconcile-buffer-names + (erc--target-from-string "#chan") erc--session))) + (should (string= result "#chan@my-conn")))) + + (should (equal (erc-tests--bufnames "#chan") + '("#chan@foonet" "#chan@my-conn"))))))) + +(ert-deftest erc--reconcile-buffer-names--multi-net-suffixed () + ;; Two networks, same channel. One network has two connections. + ;; When same channel joined on latter network with different nick, + ;; all buffer names invovling that net are suffixed with session-id. + + (with-current-buffer (get-buffer-create "foonet/bob") + (erc-mode) + (setq erc-network 'foonet + erc-server-current-nick "bob" + erc--session (make-erc--sid-dynamic :symbol 'foonet/bob + :parts [foonet "bob"] + :len 2) + erc-server-process (erc-tests--create-live-proc))) + + (with-current-buffer (get-buffer-create + (elt ["#chan@foonet" "#chan@foonet/bob"] (random 2))) + (erc-mode) + (setq erc--buffer-target (erc--target-from-string "#chan") + erc-server-process (with-current-buffer "foonet/bob" + erc-server-process) + erc--session (with-current-buffer "foonet/bob" + erc--session))) + + (with-current-buffer (get-buffer-create "barnet") + (erc-mode) + (setq erc-network 'barnet + erc-server-current-nick (elt ["alice" "bob"] (random 2)) + erc--session (erc--sid-create 'barnet) + erc-server-process (erc-tests--create-live-proc))) + + (with-current-buffer (get-buffer-create "#chan@barnet") + (erc-mode) + (setq erc--buffer-target (erc--target-from-string "#chan") + erc-server-process (with-current-buffer "barnet" + erc-server-process) + erc--session (with-current-buffer "barnet" + erc--session))) + + (with-current-buffer (get-buffer-create "foonet/alice") + (erc-mode) + (setq erc-network 'foonet + erc-server-current-nick "alice" + erc--session (make-erc--sid-dynamic :symbol 'foonet/alice + :parts [foonet "alice"] + :len 2) + erc-server-process (erc-tests--create-live-proc))) + + (with-current-buffer "foonet/alice" + (let ((result (erc--reconcile-buffer-names + (erc--target-from-string "#chan") erc--session))) + (should (string= result "#chan@foonet/alice")))) + + (should (equal (erc-tests--bufnames "#chan") + '("#chan@barnet" "#chan@foonet/bob"))) + + (erc-tests--clean-bufs)) + +(ert-deftest erc-reconcile-buffer-names--local () + (let ((erc-isupport-parameters '((CHANTYPES "&#")))) + (with-current-buffer (get-buffer-create "DALnet") + (erc-mode) + (setq erc-network 'DALnet + erc-server-announced-name "elysium.ga.us.dal.net" + erc-server-process (erc-tests--create-dead-proc) + erc--session (erc--sid-create nil))) + + (ert-info ("Local chan buffer from older, disconnected session") + (with-current-buffer (get-buffer-create "&chan") + (erc-mode) + (setq erc--buffer-target (erc--target-from-string "&chan") + erc-network 'DALnet + erc-server-announced-name "twisted.ma.us.dal.net" + erc-server-process (erc-tests--create-dead-proc) + erc--session (erc--sid-create nil)))) + + (ert-info ("Local channels renamed using network server names") + (with-current-buffer "DALnet" + (let ((result (erc--reconcile-buffer-names + (erc--target-from-string "&chan") erc--session))) + (should (string= result "&chan@elysium.ga.us.dal.net"))))) + + (should (get-buffer "&chan@twisted.ma.us.dal.net")) + (should-not (get-buffer "&chan")) + (erc-tests--clean-bufs))) + ;;; erc-tests.el ends here -- 2.31.1