mirror of
git://git.sv.gnu.org/emacs.git
synced 2026-02-17 01:34:21 +00:00
Address long-standing ERC buffer-naming issues
* lisp/erc/erc-backend.el (erc-server-connected): Revise doc string. (erc-server-reconnect, erc-server-JOIN): Reuse original ID param from the first connection when calling `erc-open'. (erc-server-NICK): Apply same name generation process used by `erc-open'; except here, do so for the purpose of "re-nicking". Update network identifier and maybe buffer names after a user's own nick changes. * lisp/erc/erc-networks.el (erc-networks--id, erc-networks--id-fixed, erc-networks--id-qualifying): Define new set of structs to contain all info relevant to specifying a unique identifier for a network context. Add a new variable `erc-networks--id' to store a local reference to a `erc-networks--id' object, shared among all buffers in a logical session. (erc-networks--id-given, erc-networks--id-create, erc-networks--id-on-connect, erc-networks--id--equal-p, erc-networks--id-qualifying-init-parts, erc-networks--id-qualifying-init-symbol, erc-networks--id-qualifying-grow-id, erc-networks--id-qualifying-reset-id, erc-networks--id-qualifying-prefix-length, erc-networks--id-qualifying-update, erc-networks--id-reload, erc-networks--id-ensure-comparable, erc-networks--id-sort-buffers): Add new functions to support management of `erc-networks--id' struct instances. (erc-networks--id-sep): New variable for to help when formatting buffer names. (erc-obsolete-var): Define new generic context rewriter. (erc-networks-shrink-ids-and-buffer-names, erc-networks--refresh-buffer-names, erc-networks--shrink-ids-and-buffer-names-any): Add functions to reassess all network IDs and shrink them if necessary along with affected buffer names. Also add function to rename buffers so that their names are unique. Register these on all three of ERC's kill-buffer hooks because an orphaned target buffer is enough to keep its session alive. (erc-networks-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 that has become unique across all sessions and, in most cases, all networks. IOW, remove the @NETWORK-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-networks--examine-targets): Add new utility function that visits all ERC buffers and runs callbacks when a buffer-name collision is encountered. (erc-networks--qualified-sep): Add constant to hold separator between target and suffix. (erc-networks--construct-target-buffer-name, erc-networks--ensure-unique-target-buffer-name, erc-networks--ensure-unique-server-buffer-name, erc-networks--maybe-update-buffer-name): Add helpers to support `erc-networks--reconcile-buffer-names' and friends. (erc-networks--reconcile-buffer-names): Add new buffer-naming strategy function and helper for `erc-generate-new-buffer-name' that only run in target buffers. (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 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. The rationale for not making this customizable is that the value signifies the pathological case where a user of an uncommon IRC setup has not yet set a mapping from announced- to network name. And the chances of there being multiple unknown networks is low. (erc-set-network-name, erc-networks--set-name): Deprecate former and partially replace with latter. Ding with helpful message, and don't set `erc-network' 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-unset-network-name): Deprecate function unused internally. (erc-networks--insert-transplanted-content, erc-networks--reclaim-orphaned-target-buffers, erc-networks--copy-over-server-buffer-contents, erc--update-server-identity): 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 network ID. (erc-networks--init-identity): Add new function to perform one-time session-related setup. This could be combined with `erc-set-network-name'. (erc-networks--rename-server-buffer): Add new function to replace `erc-unset-network-name' as default `erc-disconnected-hook' member; renames server buffers once network is discovered; added to/removed from `erc-after-connect' hook on `erc-networks' minor mode. (erc-networks--bouncer-targets): Add constant to hold target symbols of well known bouncer-configuration bots. (erc-networks-on-MOTD-end): Add primary network-context handler to run on 376/422 functions, just before logical connection is officially established. (erc-networks-enable, erc-networks-mode): Register main network-setup handler with 376/422 hooks. * 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-reuse-buffers): Make this an obsolete variable, but take pains to ensure its pre-28.1 behavior is preserved. That is, undo the regression involving unwanted automatic reassociation of channel buffers during joins, which arrived in ERC 5.4 and effectively inverted the meaning of this variable, when nil, for channel buffers, all without accompanying documentation or announcement. (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 network-based uniquifying suffixes for channels and query buffers. Fall back to the TCP host:port<n> convention when necessary. Accept additional optional params 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 params. (erc-open): Add new ID param to for a network identifier explicitly passed to an entry-point command. This is stored in the `given' slot of the `erc-network--id' object. Also initialize the latter in new connections and otherwise copy it over. As part of the push to recast erc-networks.el as an essential library, set `erc-network' explicitly, when known, rather than via hooks. (erc, erc-tls): Add new ID keyword parameter and pass it to `erc-open'. (erc-log-irc-protocol): Use `erc--network-id' instead of the function `erc-network' to determine preferred peer name. (erc-format-target-and/or-network): This is called frequently from mode-line updates, so renaming buffers here is not ideal. Instead, do so in `erc-networks--rename-server-buffer'. (erc-kill-server-hook): Add `erc-networks-shrink-ids-and-buffer-names' as default member. (erc-kill-channel-hook, erc-kill-buffer-hook): Add `erc-networks-shrink-ids-and-buffer-names' and `erc-networks-rename-surviving-target-buffer' as default member. * test/lisp/erc/erc-tests.el (erc-log-irc-protocol): Use network-ID focused internal API. * test/lisp/erc/erc-networks-tests.el: Add new file that includes tests for the above network-ID focused functions. See bug#48598 for background on all of the above.
This commit is contained in:
parent
9be08ceb31
commit
752e860db4
5 changed files with 2575 additions and 110 deletions
|
|
@ -196,11 +196,9 @@ escapes removed.")
|
|||
"Mapping of server buffers to their specific ping timer.")
|
||||
|
||||
(defvar-local erc-server-connected nil
|
||||
"Non-nil if the current buffer has been used by ERC to establish
|
||||
an IRC connection.
|
||||
|
||||
If you wish to determine whether an IRC connection is currently
|
||||
active, use the `erc-server-process-alive' function instead.")
|
||||
"Non-nil if the current buffer belongs to an active IRC connection.
|
||||
To determine whether an underlying transport is connected, use the
|
||||
function `erc-server-process-alive' instead.")
|
||||
|
||||
(defvar-local erc-server-reconnect-count 0
|
||||
"Number of times we have failed to reconnect to the current server.")
|
||||
|
|
@ -602,7 +600,11 @@ Make sure you are in an ERC buffer when running this."
|
|||
(erc-open erc-session-server erc-session-port erc-server-current-nick
|
||||
erc-session-user-full-name t erc-session-password
|
||||
nil nil nil erc-session-client-certificate
|
||||
erc-session-username)))))
|
||||
erc-session-username
|
||||
(erc-networks--id-given erc-networks--id))
|
||||
(unless (with-suppressed-warnings ((obsolete erc-reuse-buffers))
|
||||
erc-reuse-buffers)
|
||||
(cl-assert (not (eq buffer (current-buffer)))))))))
|
||||
|
||||
(defun erc-server-delayed-reconnect (buffer)
|
||||
(if (buffer-live-p buffer)
|
||||
|
|
@ -1336,7 +1338,11 @@ add things to `%s' instead."
|
|||
nick erc-session-user-full-name
|
||||
nil nil
|
||||
(list chnl) chnl
|
||||
erc-server-process))
|
||||
erc-server-process
|
||||
nil
|
||||
erc-session-username
|
||||
(erc-networks--id-given
|
||||
erc-networks--id)))
|
||||
(when buffer
|
||||
(set-buffer buffer)
|
||||
(with-suppressed-warnings
|
||||
|
|
@ -1427,19 +1433,27 @@ add things to `%s' instead."
|
|||
;; 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--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--target tgt))
|
||||
(with-current-buffer (erc-get-buffer-create erc-session-server
|
||||
erc-session-port nil tgt
|
||||
(erc-networks--id-given
|
||||
erc-networks--id))
|
||||
;; 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-networks--id-reload erc-networks--id 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)))
|
||||
|
|
|
|||
|
|
@ -731,6 +731,466 @@ MATCHER is used to find a corresponding network to a server while
|
|||
(defvar-local erc-network nil
|
||||
"The name of the network you are connected to (a symbol).")
|
||||
|
||||
|
||||
;;;; Identifying session context
|
||||
|
||||
;; This section is concerned with identifying and managing the
|
||||
;; relationship between an IRC connection and its unique identity on a
|
||||
;; given network (as seen by that network's nick-granting system).
|
||||
;; This relationship is quasi-permanent and transcends IRC connections
|
||||
;; and Emacs sessions. As of mid 2022, only nicknames matter, and
|
||||
;; whether a user is authenticated does not directly impact network
|
||||
;; identity from a client's perspective. However, ERC must be
|
||||
;; equipped to adapt should this ever change. And while a connection
|
||||
;; is normally associated with exactly one nick, some networks (or
|
||||
;; intermediaries) may allow multiple clients to control the same nick
|
||||
;; by combining instance activity into a single logical client. ERC
|
||||
;; must be limber enough to handle such situations.
|
||||
|
||||
(defvar-local erc-networks--id nil
|
||||
"Server-local instance of its namesake struct.
|
||||
Also shared among all target buffers for a given connection. See
|
||||
\\[describe-symbol] `erc-networks--id' for more.")
|
||||
|
||||
(cl-defstruct erc-networks--id
|
||||
"Persistent identifying info for a network presence.
|
||||
|
||||
Here, \"presence\" refers to some local state representing a
|
||||
client's existence on a network. Some clients refer to this as a
|
||||
\"context\" or a \"net-id\". The management of this state
|
||||
involves tracking associated buffers and what they're displaying.
|
||||
Since a presence can outlast physical connections and survive
|
||||
changes in back-end transports (and even outlive Emacs sessions),
|
||||
its identity must be resilient.
|
||||
|
||||
Essential to this notion of an enduring existence on a network is
|
||||
ensuring recovery from the loss of a server buffer. Thus, any
|
||||
useful identifier must be shared among server and target buffers
|
||||
to allow for reassociation. Beyond that, it must ideally be
|
||||
derivable from the same set of connection parameters. See the
|
||||
constructor `erc-networks--id-create' for more info."
|
||||
(ts nil :type float :read-only t :documentation "Creation timestamp.")
|
||||
(symbol nil :type symbol :documentation "ID as a symbol."))
|
||||
|
||||
(cl-defstruct (erc-networks--id-fixed
|
||||
(:include erc-networks--id)
|
||||
(:constructor erc-networks--id-fixed-create
|
||||
(given &aux (ts (float-time)) (symbol given)))))
|
||||
|
||||
(cl-defstruct (erc-networks--id-qualifying
|
||||
(:include erc-networks--id)
|
||||
(:constructor erc-networks--id-qualifying-create
|
||||
(&aux
|
||||
(ts (float-time))
|
||||
(parts (erc-networks--id-qualifying-init-parts))
|
||||
(symbol (erc-networks--id-qualifying-init-symbol
|
||||
parts))
|
||||
(len 1))))
|
||||
"A session context composed of hierarchical connection parameters.
|
||||
Two identifiers are considered equivalent when their non-empty
|
||||
`parts' slots compare equal. Related identifiers share a common
|
||||
prefix of `parts' taken from connection parameters (given or
|
||||
discovered). An identifier's unique `symbol', intended for
|
||||
display purposes, is created by concatenating the shortest common
|
||||
prefix among its relatives. For example, related presences [b a
|
||||
r d o] and [b a z a r] would have symbols b/a/r and b/a/z
|
||||
respectively. The separator is given by `erc-networks--id-sep'."
|
||||
(parts nil :type sequence ; a vector of atoms
|
||||
:documentation "Sequence of identifying components.")
|
||||
(len 0 :type integer
|
||||
:documentation "Length of active `parts' interval."))
|
||||
|
||||
;; For now, please use this instead of `erc-networks--id-fixed-p'.
|
||||
(cl-defgeneric erc-networks--id-given (net-id)
|
||||
"Return the preassigned identifier for a network presence, if any.
|
||||
This may have originated from an `:id' arg to entry-point commands
|
||||
`erc-tls' or `erc'.")
|
||||
|
||||
(cl-defmethod erc-networks--id-given ((_ erc-networks--id))
|
||||
nil)
|
||||
|
||||
(cl-defmethod erc-networks--id-given ((nid erc-networks--id-fixed))
|
||||
(erc-networks--id-symbol nid))
|
||||
|
||||
(cl-generic-define-context-rewriter erc-obsolete-var (var spec)
|
||||
`((with-suppressed-warnings ((obsolete ,var)) ,var) ,spec))
|
||||
|
||||
;; As a catch-all, derive the symbol from the unquoted printed repr.
|
||||
(cl-defgeneric erc-networks--id-create (id)
|
||||
"Invoke an appropriate constructor for an `erc-networks--id' object."
|
||||
(erc-networks--id-fixed-create (intern (format "%s" id))))
|
||||
|
||||
;; When a given ID is a symbol, trust it unequivocally.
|
||||
(cl-defmethod erc-networks--id-create ((id symbol))
|
||||
(erc-networks--id-fixed-create id))
|
||||
|
||||
;; Otherwise, use an adaptive name derived from network params.
|
||||
(cl-defmethod erc-networks--id-create ((_ null))
|
||||
(erc-networks--id-qualifying-create))
|
||||
|
||||
;; But honor an explicitly set `erc-rename-buffers' (compat).
|
||||
(cl-defmethod erc-networks--id-create
|
||||
((_ null) &context (erc-obsolete-var erc-rename-buffers null))
|
||||
(erc-networks--id-fixed-create (intern (buffer-name))))
|
||||
|
||||
;; But honor an explicitly set `erc-reuse-buffers' (compat).
|
||||
(cl-defmethod erc-networks--id-create
|
||||
((_ null) &context (erc-obsolete-var erc-reuse-buffers null))
|
||||
(erc-networks--id-fixed-create (intern (buffer-name))))
|
||||
|
||||
(cl-defmethod erc-networks--id-create
|
||||
((_ symbol) &context (erc-obsolete-var erc-reuse-buffers null))
|
||||
(erc-networks--id-fixed-create (intern (buffer-name))))
|
||||
|
||||
(cl-defgeneric erc-networks--id-on-connect (net-id)
|
||||
"Update NET-ID `erc-networks--id' after connection params known.
|
||||
This is typically during or just after MOTD.")
|
||||
|
||||
(cl-defmethod erc-networks--id-on-connect ((_ erc-networks--id))
|
||||
nil)
|
||||
|
||||
(cl-defmethod erc-networks--id-on-connect ((id erc-networks--id-qualifying))
|
||||
(erc-networks--id-qualifying-update id (erc-networks--id-qualifying-create)))
|
||||
|
||||
(cl-defgeneric erc-networks--id-equal-p (self other)
|
||||
"Return non-nil when two network identities exhibit underlying equality.
|
||||
SELF and OTHER are `erc-networks--id' struct instances. This
|
||||
should normally be used only for ID recovery or merging, after
|
||||
which no two identities should be `equal' (timestamps aside) that
|
||||
aren't also `eq'.")
|
||||
|
||||
(cl-defmethod erc-networks--id-equal-p ((self erc-networks--id)
|
||||
(other erc-networks--id))
|
||||
(eq self other))
|
||||
|
||||
(cl-defmethod erc-networks--id-equal-p ((a erc-networks--id-fixed)
|
||||
(b erc-networks--id-fixed))
|
||||
(or (eq a b) (eq (erc-networks--id-symbol a) (erc-networks--id-symbol b))))
|
||||
|
||||
(cl-defmethod erc-networks--id-equal-p ((a erc-networks--id-qualifying)
|
||||
(b erc-networks--id-qualifying))
|
||||
(or (eq a b) (equal (erc-networks--id-qualifying-parts a)
|
||||
(erc-networks--id-qualifying-parts b))))
|
||||
|
||||
;; ERASE-ME: if some future extension were to come along offering
|
||||
;; additional members, e.g., [Libera.Chat "bob" laptop], it'd likely
|
||||
;; be cleaner to create a new struct type descending from
|
||||
;; `erc-networks--id-qualifying' than to convert this function into a
|
||||
;; generic. However, the latter would be simpler because it'd just
|
||||
;; require something like &context (erc-v3-device erc-v3--device-t).
|
||||
|
||||
(defun erc-networks--id-qualifying-init-parts ()
|
||||
"Return opaque list of atoms to serve as canonical identifier."
|
||||
(when-let ((network (erc-network))
|
||||
(nick (erc-current-nick)))
|
||||
(vector network (erc-downcase nick))))
|
||||
|
||||
(defvar erc-networks--id-sep "/"
|
||||
"Separator for joining `erc-networks--id-qualifying-parts' into a net ID.")
|
||||
|
||||
(defun erc-networks--id-qualifying-init-symbol (elts &optional len)
|
||||
"Return symbol appropriate for network context identified by ELTS.
|
||||
Use leading interval of length LEN as contributing components.
|
||||
Combine them with string separator `erc-networks--id-sep'."
|
||||
(when elts
|
||||
(unless len
|
||||
(setq len 1))
|
||||
(intern (mapconcat (lambda (s) (prin1-to-string s t))
|
||||
(seq-subseq elts 0 len)
|
||||
erc-networks--id-sep))))
|
||||
|
||||
(defun erc-networks--id-qualifying-grow-id (nid)
|
||||
"Grow NID by one component or return nil when at capacity."
|
||||
(unless (= (length (erc-networks--id-qualifying-parts nid))
|
||||
(erc-networks--id-qualifying-len nid))
|
||||
(setf (erc-networks--id-symbol nid)
|
||||
(erc-networks--id-qualifying-init-symbol
|
||||
(erc-networks--id-qualifying-parts nid)
|
||||
(cl-incf (erc-networks--id-qualifying-len nid))))))
|
||||
|
||||
(defun erc-networks--id-qualifying-reset-id (nid)
|
||||
"Restore NID to its initial state."
|
||||
(setf (erc-networks--id-qualifying-len nid) 1
|
||||
(erc-networks--id-symbol nid)
|
||||
(erc-networks--id-qualifying-init-symbol
|
||||
(erc-networks--id-qualifying-parts nid))))
|
||||
|
||||
(defun erc-networks--id-qualifying-prefix-length (nid-a nid-b)
|
||||
"Return length of common initial prefix of NID-A and NID-B.
|
||||
Return nil when no such sequence exists (instead of zero)."
|
||||
(when-let* ((a (erc-networks--id-qualifying-parts nid-a))
|
||||
(b (erc-networks--id-qualifying-parts nid-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-networks--id-qualifying-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-networks--id' objects. OVERRIDES is an
|
||||
optional plist of SLOT VAL pairs."
|
||||
(setf (erc-networks--id-qualifying-parts dest)
|
||||
(or (plist-get overrides :parts)
|
||||
(erc-networks--id-qualifying-parts source))
|
||||
(erc-networks--id-qualifying-len dest)
|
||||
(or (plist-get overrides :len)
|
||||
(erc-networks--id-qualifying-len source))
|
||||
(erc-networks--id-symbol dest)
|
||||
(or (plist-get overrides :symbol)
|
||||
(erc-networks--id-qualifying-init-symbol
|
||||
(erc-networks--id-qualifying-parts dest)
|
||||
(erc-networks--id-qualifying-len dest)))))
|
||||
|
||||
(cl-defgeneric erc-networks--id-reload (_nid &optional _proc _parsed)
|
||||
"Handle an update to the current network identity.
|
||||
If provided, PROC should be the current `erc-server-process' and
|
||||
PARSED the current `erc-response'. NID is an `erc-networks--id'
|
||||
object."
|
||||
nil)
|
||||
|
||||
(cl-defmethod erc-networks--id-reload ((nid erc-networks--id-qualifying)
|
||||
&optional proc parsed)
|
||||
"Refresh identity after an `erc-networks--id-qualifying-parts'update."
|
||||
(erc-networks--id-qualifying-update nid (erc-networks--id-qualifying-create)
|
||||
:len
|
||||
(erc-networks--id-qualifying-len nid))
|
||||
(erc-networks--rename-server-buffer (or proc erc-server-process) parsed)
|
||||
(erc-networks--shrink-ids-and-buffer-names-any)
|
||||
(erc-with-all-buffers-of-server
|
||||
erc-server-process #'erc--default-target
|
||||
(when-let* ((new-name (erc-networks--reconcile-buffer-names erc--target
|
||||
nid))
|
||||
((not (equal (buffer-name) new-name))))
|
||||
(rename-buffer new-name 'unique))))
|
||||
|
||||
(cl-defgeneric erc-networks--id-ensure-comparable (self other)
|
||||
"Take measures to ensure two net identities are in comparable states.")
|
||||
|
||||
(cl-defmethod erc-networks--id-ensure-comparable ((_ erc-networks--id)
|
||||
(_ erc-networks--id))
|
||||
nil)
|
||||
|
||||
(cl-defmethod erc-networks--id-ensure-comparable
|
||||
((nid erc-networks--id-qualifying) (other erc-networks--id-qualifying))
|
||||
"Grow NID along with that of the current buffer.
|
||||
Rename the current buffer if its NID has grown."
|
||||
(when-let ((n (erc-networks--id-qualifying-prefix-length other nid)))
|
||||
(while (and (<= (erc-networks--id-qualifying-len nid) n)
|
||||
(erc-networks--id-qualifying-grow-id nid)))
|
||||
;; Grow and rename a visited buffer and all its targets
|
||||
(when (and (> (erc-networks--id-qualifying-len nid)
|
||||
(erc-networks--id-qualifying-len other))
|
||||
(erc-networks--id-qualifying-grow-id other))
|
||||
;; Rename NID's buffers using current ID
|
||||
(erc-buffer-filter (lambda ()
|
||||
(when (eq erc-networks--id other)
|
||||
(erc-networks--maybe-update-buffer-name)))))))
|
||||
|
||||
(defun erc-networks--id-sort-buffers (buffers)
|
||||
"Return a list of target BUFFERS, newest to oldest."
|
||||
(sort buffers
|
||||
(lambda (a b)
|
||||
(> (with-current-buffer a (erc-networks--id-ts erc-networks--id))
|
||||
(with-current-buffer b (erc-networks--id-ts erc-networks--id))))))
|
||||
|
||||
|
||||
;;;; Buffer association
|
||||
|
||||
(cl-defgeneric erc-networks--shrink-ids-and-buffer-names ()
|
||||
nil) ; concrete default implementation for non-eliding IDs
|
||||
|
||||
(defun erc-networks--refresh-buffer-names (identity &optional omit)
|
||||
"Ensure all colliding buffers for network IDENTITY have suffixes.
|
||||
Then rename current buffer appropriately. Don't consider buffer OMIT
|
||||
when determining collisions."
|
||||
(if (erc-networks--examine-targets identity erc--target
|
||||
#'ignore
|
||||
(lambda ()
|
||||
(unless (or (not omit) (eq (current-buffer) omit))
|
||||
(erc-networks--ensure-unique-target-buffer-name)
|
||||
t)))
|
||||
(erc-networks--ensure-unique-target-buffer-name)
|
||||
(rename-buffer (erc--target-string erc--target) 'unique)))
|
||||
|
||||
;; This currently doesn't equalize related identities 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).
|
||||
(defun erc-networks--shrink-ids-and-buffer-names-any (&rest omit)
|
||||
(let (grown)
|
||||
;; Gather all grown identities.
|
||||
(erc-buffer-filter
|
||||
(lambda ()
|
||||
(when (and erc-networks--id
|
||||
(erc-networks--id-qualifying-p erc-networks--id)
|
||||
(not (memq (current-buffer) omit))
|
||||
(not (memq erc-networks--id grown))
|
||||
(> (erc-networks--id-qualifying-len erc-networks--id) 1))
|
||||
(push erc-networks--id grown))))
|
||||
;; Check for other identities with shared prefix. If none exists,
|
||||
;; and an identity is overlong, shrink it.
|
||||
(dolist (nid grown)
|
||||
(let ((skip (not (null omit))))
|
||||
(catch 'found
|
||||
(if (cdr grown)
|
||||
(dolist (other grown)
|
||||
(unless (eq nid other)
|
||||
(setq skip nil)
|
||||
(when (erc-networks--id-qualifying-prefix-length nid other)
|
||||
(throw 'found (setq skip t)))))
|
||||
(setq skip nil)))
|
||||
(unless (or skip (< (erc-networks--id-qualifying-len nid) 2))
|
||||
(erc-networks--id-qualifying-reset-id nid)
|
||||
(erc-buffer-filter
|
||||
(lambda ()
|
||||
(when (and (eq erc-networks--id nid)
|
||||
(not (memq (current-buffer) omit)))
|
||||
(if erc--target
|
||||
(erc-networks--refresh-buffer-names nid omit)
|
||||
(erc-networks--maybe-update-buffer-name))))))))))
|
||||
|
||||
(cl-defmethod erc-networks--shrink-ids-and-buffer-names
|
||||
(&context (erc-networks--id erc-networks--id-qualifying))
|
||||
(erc-networks--shrink-ids-and-buffer-names-any (current-buffer)))
|
||||
|
||||
(defun erc-networks-rename-surviving-target-buffer ()
|
||||
"Maybe drop qualifying 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*
|
||||
(((with-suppressed-warnings ((obsolete erc-reuse-buffers))
|
||||
erc-reuse-buffers))
|
||||
(target erc--target)
|
||||
;; Buffer name includes 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 ()
|
||||
(and-let* ((erc--target)
|
||||
((not (eq buf (current-buffer))))
|
||||
((eq (erc--target-symbol target)
|
||||
(erc--target-symbol erc--target))))))))
|
||||
((not (cdr others))))
|
||||
(with-current-buffer (car others)
|
||||
(rename-buffer (erc--target-string target)))))
|
||||
|
||||
(defun erc-networks-shrink-ids-and-buffer-names ()
|
||||
"Recompute network IDs and buffer names, ignoring the current buffer.
|
||||
Only do so when an IRC connection's context supports qualified
|
||||
naming. Do not discriminate based on whether a buffer's
|
||||
connection is active."
|
||||
(erc-networks--shrink-ids-and-buffer-names))
|
||||
|
||||
(defun erc-networks--examine-targets (identity target on-dupe on-collision)
|
||||
"Visit all ERC target buffers with the same TARGET.
|
||||
Call ON-DUPE when a buffer's identity belongs to a network
|
||||
IDENTITY or \"should\" 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--target (eq (erc--target-symbol erc--target)
|
||||
(erc--target-symbol target)))
|
||||
(let ((oursp (if (erc--target-channel-local-p target)
|
||||
(equal announced erc-server-announced-name)
|
||||
(erc-networks--id-equal-p identity erc-networks--id))))
|
||||
(funcall (if oursp on-dupe on-collision))))))))
|
||||
|
||||
(defconst erc-networks--qualified-sep "@"
|
||||
"Separator used for naming a target buffer.")
|
||||
|
||||
(defun erc-networks--construct-target-buffer-name (target)
|
||||
"Return TARGET@suffix."
|
||||
(concat (erc--target-string target)
|
||||
(if (with-suppressed-warnings ((obsolete erc-reuse-buffers))
|
||||
erc-reuse-buffers)
|
||||
erc-networks--qualified-sep "/")
|
||||
(cond
|
||||
((not (with-suppressed-warnings ((obsolete erc-reuse-buffers))
|
||||
erc-reuse-buffers))
|
||||
(cadr (split-string
|
||||
(symbol-name (erc-networks--id-symbol erc-networks--id))
|
||||
"/")))
|
||||
((erc--target-channel-local-p target) erc-server-announced-name)
|
||||
(t (symbol-name (erc-networks--id-symbol erc-networks--id))))))
|
||||
|
||||
(defun erc-networks--ensure-unique-target-buffer-name ()
|
||||
(when-let* ((new-name (erc-networks--construct-target-buffer-name
|
||||
erc--target))
|
||||
((not (equal (buffer-name) new-name))))
|
||||
(rename-buffer new-name 'unique)))
|
||||
|
||||
(defun erc-networks--ensure-unique-server-buffer-name ()
|
||||
(when-let* ((new-name (symbol-name (erc-networks--id-symbol
|
||||
erc-networks--id)))
|
||||
((not (equal (buffer-name) new-name))))
|
||||
(rename-buffer new-name 'unique)))
|
||||
|
||||
(defun erc-networks--maybe-update-buffer-name ()
|
||||
"Update current buffer name to reflect display ID if necessary."
|
||||
(if erc--target
|
||||
(erc-networks--ensure-unique-target-buffer-name)
|
||||
(erc-networks--ensure-unique-server-buffer-name)))
|
||||
|
||||
(defun erc-networks--reconcile-buffer-names (target nid)
|
||||
"Reserve preferred buffer name for TARGET and network identifier.
|
||||
Expect TARGET to be an `erc--target' instance. Guarantee that at
|
||||
most one existing buffer has the same `erc-networks--id' and a
|
||||
case-mapped target, i.e., `erc--target-symbol'. If other buffers
|
||||
with equivalent targets exist, rename them to TARGET@their-NID
|
||||
and return TARGET@our-NID. Otherwise return TARGET as a string.
|
||||
When multiple buffers for TARGET exist for the current NID,
|
||||
rename them with <n> 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-networks--examine-targets nid target
|
||||
(lambda () (push (current-buffer) existing) nil)
|
||||
;; Append network ID as TARGET@NID,
|
||||
;; possibly qualifying to achieve uniqueness.
|
||||
(lambda ()
|
||||
(unless (erc--target-channel-local-p erc--target)
|
||||
(erc-networks--id-ensure-comparable
|
||||
nid erc-networks--id))
|
||||
(erc-networks--ensure-unique-target-buffer-name)
|
||||
t)))
|
||||
;; Must follow ^ because NID may have been modified
|
||||
(name (if (or namesakes (not (with-suppressed-warnings
|
||||
((obsolete erc-reuse-buffers))
|
||||
erc-reuse-buffers)))
|
||||
(erc-networks--construct-target-buffer-name target)
|
||||
(erc--target-string target)))
|
||||
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)))
|
||||
(unless (with-suppressed-warnings ((obsolete erc-reuse-buffers))
|
||||
erc-reuse-buffers)
|
||||
(when (string-suffix-p ">" name)
|
||||
(setq name (substring name 0 -3))))
|
||||
(dolist (ex (erc-networks--id-sort-buffers existing))
|
||||
(with-current-buffer ex
|
||||
(rename-buffer name 'unique)))
|
||||
(when placeholder (kill-buffer placeholder))
|
||||
name))
|
||||
|
||||
|
||||
;; Functions:
|
||||
|
||||
;;;###autoload
|
||||
|
|
@ -739,6 +1199,7 @@ MATCHER is used to find a corresponding network to a server while
|
|||
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
|
||||
(declare (obsolete "maybe see `erc-networks--determine'" "29.1"))
|
||||
(let ((network-name (cdr (assoc "NETWORK" erc-server-parameters))))
|
||||
(if network-name
|
||||
(intern network-name)
|
||||
|
|
@ -761,23 +1222,242 @@ server name and search for a match in `erc-networks-alist'."
|
|||
|
||||
(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 (gensym "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 (erc--get-isupport-entry 'NETWORK 'single))
|
||||
((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."
|
||||
;; 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
|
||||
(erc-display-error-notice parsed "Failed to determine server name.")
|
||||
(erc-display-error-notice
|
||||
parsed (concat "If this was unexpected, consider reporting it via "
|
||||
(substitute-command-keys "\\[erc-bug]") "."))
|
||||
(setq erc-server-announced-name (erc-response.sender parsed)))
|
||||
nil)
|
||||
|
||||
(defun erc-unset-network-name (_nick _ip _reason)
|
||||
"Set `erc-network' to nil."
|
||||
(declare (obsolete "`erc-network' is now effectively read-only" "29.1"))
|
||||
(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 network
|
||||
;; identity as anything else.
|
||||
|
||||
(defun erc-networks--insert-transplanted-content (content)
|
||||
(let ((inhibit-read-only t)
|
||||
(buffer-undo-list t))
|
||||
(save-excursion
|
||||
(save-restriction
|
||||
(widen)
|
||||
(goto-char (point-min))
|
||||
(insert-before-markers content)))))
|
||||
|
||||
;; This should run whenever a network identity is updated.
|
||||
|
||||
(defun erc-networks--reclaim-orphaned-target-buffers (new-proc nid announced)
|
||||
"Visit disowned buffers for same NID and associate with NEW-PROC.
|
||||
ANNOUNCED is the server's reported host name."
|
||||
(erc-buffer-filter
|
||||
(lambda ()
|
||||
(when (and erc--target
|
||||
(not erc-server-connected)
|
||||
(erc-networks--id-equal-p erc-networks--id nid)
|
||||
(or (not (erc--target-channel-local-p erc--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--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-networks--ensure-unique-target-buffer-name)
|
||||
(erc-networks--insert-transplanted-content content))))
|
||||
(setq erc-server-process new-proc
|
||||
erc-server-connected t
|
||||
erc-networks--id nid))))))
|
||||
|
||||
(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 identity. 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-networks--id' should be
|
||||
;; `erc-networks--id-equal-p' to caller's network
|
||||
;; identity 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)))
|
||||
|
||||
;; This stands alone for testing purposes
|
||||
|
||||
(defun erc-networks--update-server-identity ()
|
||||
"Maybe grow or replace the current network identity.
|
||||
If a dupe is found, adopt its identity by overwriting ours.
|
||||
Otherwise, take steps to ensure it can effectively be compared to
|
||||
ours, now and into the future. Note that target buffers are
|
||||
considered as well because server buffers are often killed."
|
||||
(let* ((identity erc-networks--id)
|
||||
(buffer (current-buffer))
|
||||
(f (lambda ()
|
||||
(unless (or (eq (current-buffer) buffer)
|
||||
(eq erc-networks--id identity))
|
||||
(if (erc-networks--id-equal-p identity erc-networks--id)
|
||||
(throw 'buffer erc-networks--id)
|
||||
(erc-networks--id-ensure-comparable identity
|
||||
erc-networks--id)
|
||||
nil))))
|
||||
(found (catch 'buffer (erc-buffer-filter f))))
|
||||
(when found
|
||||
(setq erc-networks--id found))))
|
||||
|
||||
;; These steps should only run when initializing a newly connected
|
||||
;; server buffer, whereas `erc-networks--rename-server-buffer' can run
|
||||
;; mid-session, after an identity's core components have changed.
|
||||
|
||||
(defun erc-networks--init-identity (_proc _parsed)
|
||||
"Update identity with real network name."
|
||||
;; Initialize identity for real now that we know the network
|
||||
(cl-assert erc-network)
|
||||
(unless (erc-networks--id-symbol erc-networks--id) ; unless just reconnected
|
||||
(erc-networks--id-on-connect erc-networks--id))
|
||||
;; Find duplicate identities or other conflicting ones and act
|
||||
;; accordingly.
|
||||
(erc-networks--update-server-identity)
|
||||
;;
|
||||
nil)
|
||||
|
||||
(defun erc-networks--rename-server-buffer (new-proc &optional _parsed)
|
||||
"Rename a server buffer based on its network identity.
|
||||
Assume that the current buffer is a server buffer, either one
|
||||
with a newly established connection whose identity has just been
|
||||
fully fleshed out, or an existing one whose identity has just
|
||||
been updated. Either way, assume the current identity is ready
|
||||
to serve as a canonical identifier.
|
||||
|
||||
When a server buffer already exists with the chosen name, copy
|
||||
over its contents and kill it. However, when its process is
|
||||
still alive, kill off the current buffer. This can happen, for
|
||||
example, after a perceived loss in network connectivity turns out
|
||||
to be a false alarm. If `erc-reuse-buffers' is nil, let
|
||||
`generate-new-buffer-name' do the actual renaming."
|
||||
(cl-assert (eq new-proc erc-server-process))
|
||||
(cl-assert (erc-networks--id-symbol erc-networks--id))
|
||||
;; Always look for targets to reassociate because original server
|
||||
;; buffer may have been deleted.
|
||||
(erc-networks--reclaim-orphaned-target-buffers new-proc erc-networks--id
|
||||
erc-server-announced-name)
|
||||
(let* ((name (symbol-name (erc-networks--id-symbol erc-networks--id)))
|
||||
;; 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-networks--id-given erc-networks--id)
|
||||
(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
|
||||
((with-suppressed-warnings ((obsolete erc-reuse-buffers))
|
||||
erc-reuse-buffers)
|
||||
(erc-networks--copy-over-server-buffer-contents existing name)
|
||||
(rename-buffer name))
|
||||
(t (rename-buffer (generate-new-buffer-name name)))))
|
||||
nil)
|
||||
|
||||
;; Soju v0.4.0 only sends ISUPPORT on upstream reconnect, so this
|
||||
;; doesn't apply. ZNC 1.8.2, however, still sends the entire burst.
|
||||
(defconst erc-networks--bouncer-targets '(*status bouncerserv)
|
||||
"Case-mapped symbols matching known bouncer service-bot targets.")
|
||||
|
||||
(defun erc-networks-on-MOTD-end (proc parsed)
|
||||
"Call on-connect functions with server PROC and PARSED message.
|
||||
This must run before `erc-server-connected' is set."
|
||||
(when erc-server-connected
|
||||
(unless (erc-buffer-filter (lambda ()
|
||||
(and erc--target
|
||||
(memq (erc--target-symbol erc--target)
|
||||
erc-networks--bouncer-targets)))
|
||||
proc)
|
||||
(let ((m (concat "Unexpected state detected. Please report via "
|
||||
(substitute-command-keys "\\[erc-bug]") ".")))
|
||||
(erc-display-error-notice parsed m))))
|
||||
|
||||
;; For now, retain compatibility with erc-server-NNN-functions.
|
||||
(or (erc-networks--ensure-announced proc parsed)
|
||||
(erc-networks--set-name proc parsed)
|
||||
(erc-networks--init-identity proc parsed)
|
||||
(erc-networks--rename-server-buffer proc parsed)))
|
||||
|
||||
(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-on-MOTD-end)
|
||||
(add-hook 'erc-server-422-functions #'erc-networks-on-MOTD-end))
|
||||
((remove-hook 'erc-server-376-functions #'erc-networks-on-MOTD-end)
|
||||
(remove-hook 'erc-server-422-functions #'erc-networks-on-MOTD-end)))
|
||||
|
||||
(defun erc-ports-list (ports)
|
||||
"Return a list of PORTS.
|
||||
|
|
|
|||
237
lisp/erc/erc.el
237
lisp/erc/erc.el
|
|
@ -134,6 +134,8 @@
|
|||
(defvar erc--server-last-reconnect-count)
|
||||
(defvar erc--server-reconnecting)
|
||||
(defvar erc-channel-members-changed-hook)
|
||||
(defvar erc-network)
|
||||
(defvar erc-networks--id)
|
||||
(defvar erc-server-367-functions)
|
||||
(defvar erc-server-announced-name)
|
||||
(defvar erc-server-connect-function)
|
||||
|
|
@ -210,12 +212,21 @@ parameters and authentication."
|
|||
: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
|
||||
|
|
@ -1660,6 +1671,14 @@ effect when `erc-join-buffer' is set to `frame'."
|
|||
(erc-channel-p (erc-default-target))))
|
||||
(t nil)))
|
||||
|
||||
;; For the sake of compatibility, a historical quirk concerning this
|
||||
;; option, when nil, has been preserved: all buffers are suffixed with
|
||||
;; the original dialed host name, which is usually something like
|
||||
;; irc.libera.chat. Collisions are handled by adding a uniquifying
|
||||
;; numeric suffix of the form <N>. Note that channel reassociation
|
||||
;; behavior involving this option (when nil) was inverted in 28.1 (ERC
|
||||
;; 5.4 and 5.4.1). This was regrettable and has since been undone.
|
||||
|
||||
(defcustom erc-reuse-buffers t
|
||||
"If nil, create new buffers on joining a channel/query.
|
||||
If non-nil, a new buffer will only be created when you join
|
||||
|
|
@ -1669,6 +1688,9 @@ the existing buffers will be reused."
|
|||
:group 'erc-buffers
|
||||
:type 'boolean)
|
||||
|
||||
(make-obsolete-variable 'erc-reuse-buffers
|
||||
"old behavior when t now permanent" "29.1")
|
||||
|
||||
(defun erc-normalize-port (port)
|
||||
"Normalize the port specification PORT to integer form.
|
||||
PORT may be an integer, a string or a symbol. If it is a string or a
|
||||
|
|
@ -1704,55 +1726,61 @@ symbol, it may have these values:
|
|||
"Check whether ports A and B are equal."
|
||||
(= (erc-normalize-port a) (erc-normalize-port b)))
|
||||
|
||||
(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 <N> uniquification method:
|
||||
(or buffer-name (generate-new-buffer-name full-buf-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.
|
||||
|
||||
(defun erc-get-buffer-create (server port target)
|
||||
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."
|
||||
(when target ; compat
|
||||
(setq tgt-info (erc--target-from-string target)))
|
||||
(if tgt-info
|
||||
(let* ((esid (erc-networks--id-symbol erc-networks--id))
|
||||
(name (if esid
|
||||
(erc-networks--reconcile-buffer-names tgt-info
|
||||
erc-networks--id)
|
||||
(erc--target-string tgt-info))))
|
||||
(if (and esid (with-suppressed-warnings ((obsolete erc-reuse-buffers))
|
||||
erc-reuse-buffers))
|
||||
name
|
||||
(generate-new-buffer-name name)))
|
||||
(if (and (with-suppressed-warnings ((obsolete erc-reuse-buffers))
|
||||
erc-reuse-buffers)
|
||||
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)
|
||||
(if (with-suppressed-warnings
|
||||
((obsolete erc-reuse-buffers))
|
||||
erc-reuse-buffers)
|
||||
(format "%s:%s" server port)
|
||||
(format "%s:%s/%s" server port server))
|
||||
(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)
|
||||
(with-suppressed-warnings ((obsolete erc-reuse-buffers))
|
||||
erc-reuse-buffers))
|
||||
(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.
|
||||
|
|
@ -2094,7 +2122,7 @@ removed from the list will be disabled."
|
|||
|
||||
(defun erc-open (&optional server port nick full-name
|
||||
connect passwd tgt-list channel process
|
||||
client-certificate user)
|
||||
client-certificate user id)
|
||||
"Connect to SERVER on PORT as NICK with USER and FULL-NAME.
|
||||
|
||||
If CONNECT is non-nil, connect to the server. Otherwise assume
|
||||
|
|
@ -2111,11 +2139,17 @@ of the client certificate itself to use when connecting over TLS,
|
|||
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 (and erc-reuse-buffers erc--server-reconnecting)))
|
||||
(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 (and erc--server-reconnecting
|
||||
(with-suppressed-warnings
|
||||
((obsolete erc-reuse-buffers))
|
||||
erc-reuse-buffers))))
|
||||
(when connect (run-hook-with-args 'erc-before-connect server port nick))
|
||||
(erc-update-modules)
|
||||
(set-buffer buffer)
|
||||
|
|
@ -2145,7 +2179,9 @@ Returns the buffer for the given server or channel."
|
|||
(set-marker erc-insert-marker (point))
|
||||
;; stack of default recipients
|
||||
(setq erc-default-recipients tgt-list)
|
||||
(setq erc--target (and channel (erc--target-from-string channel)))
|
||||
(when target
|
||||
(setq erc--target target
|
||||
erc-network (erc-network)))
|
||||
(setq erc-server-current-nick nil)
|
||||
;; Initialize erc-server-users and erc-channel-users
|
||||
(if connect
|
||||
|
|
@ -2184,6 +2220,10 @@ Returns the buffer for the given server or channel."
|
|||
:require '(:secret))))
|
||||
;; client certificate (only useful if connecting over TLS)
|
||||
(setq erc-session-client-certificate client-certificate)
|
||||
(setq erc-networks--id (if connect
|
||||
(erc-networks--id-create id)
|
||||
(buffer-local-value 'erc-networks--id
|
||||
old-buffer)))
|
||||
;; debug output buffer
|
||||
(setq erc-dbuf
|
||||
(when erc-log-p
|
||||
|
|
@ -2322,7 +2362,8 @@ parameters SERVER and NICK."
|
|||
(nick (erc-compute-nick))
|
||||
(user (erc-compute-user))
|
||||
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.
|
||||
|
||||
|
|
@ -2335,6 +2376,7 @@ Non-interactively, it takes the keyword arguments
|
|||
(user (erc-compute-user))
|
||||
password
|
||||
(full-name (erc-compute-full-name))
|
||||
id
|
||||
|
||||
That is, if called with
|
||||
|
||||
|
|
@ -2342,9 +2384,13 @@ That is, if called with
|
|||
|
||||
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 an opaque object used to identify 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 nil nil nil nil user))
|
||||
(erc-open server port nick full-name t password nil nil nil nil user id))
|
||||
|
||||
;;;###autoload
|
||||
(defalias 'erc-select #'erc)
|
||||
|
|
@ -2357,7 +2403,8 @@ for the values of the other parameters."
|
|||
(user (erc-compute-user))
|
||||
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.
|
||||
|
||||
|
|
@ -2371,6 +2418,7 @@ Non-interactively, it takes the keyword arguments
|
|||
password
|
||||
(full-name (erc-compute-full-name))
|
||||
client-certificate
|
||||
id
|
||||
|
||||
That is, if called with
|
||||
|
||||
|
|
@ -2393,12 +2441,18 @@ Example usage:
|
|||
(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 an opaque object for identifying the
|
||||
connection unequivocally. (In most cases, this would be a string or a
|
||||
symbol composed of letters from the Latin alphabet.) This option is
|
||||
generally unneeded, however. See info node `(erc) Connecting' for use
|
||||
cases. 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 user)))
|
||||
nil nil nil client-certificate user id)))
|
||||
|
||||
(defun erc-open-tls-stream (name buffer host port &rest parameters)
|
||||
"Open an TLS stream to an IRC server.
|
||||
|
|
@ -2463,13 +2517,20 @@ The buffer is created if it doesn't exist.
|
|||
|
||||
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 <server>:<port>."
|
||||
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 <server>:<port> 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-networks--id' 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-networks--id)
|
||||
(esid (erc-networks--id-symbol erc-networks--id)))
|
||||
(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*")
|
||||
|
|
@ -3866,7 +3927,8 @@ the message given by REASON."
|
|||
(when process
|
||||
(delete-process process))
|
||||
(erc-server-reconnect)
|
||||
(with-suppressed-warnings ((obsolete erc-server-reconnecting))
|
||||
(with-suppressed-warnings ((obsolete erc-server-reconnecting)
|
||||
((obsolete erc-reuse-buffers)))
|
||||
(if erc-reuse-buffers
|
||||
(progn (cl-assert (not erc--server-reconnecting))
|
||||
(cl-assert (not erc-server-reconnecting)))
|
||||
|
|
@ -6626,21 +6688,13 @@ This should be a string with substitution variables recognized by
|
|||
"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--target)
|
||||
(name (if-let ((esid (erc-networks--id-symbol erc-networks--id)))
|
||||
(symbol-name esid)
|
||||
(erc-shorten-server-name (or erc-server-announced-name
|
||||
erc-session-server)))))
|
||||
(concat (erc--target-string erc--target) "@" name)
|
||||
(buffer-name)))
|
||||
|
||||
(defun erc-format-away-status ()
|
||||
"Return a formatted `erc-mode-line-away-status-format' if `erc-away' is non-nil."
|
||||
|
|
@ -7060,20 +7114,29 @@ See also `format-spec'."
|
|||
;; 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-networks-shrink-ids-and-buffer-names)
|
||||
"Invoked whenever a live server buffer is killed via `kill-buffer'."
|
||||
:package-version '(ERC . "5.4.1") ; FIXME increment upon publishing to ELPA
|
||||
:group 'erc-hooks
|
||||
:type 'hook)
|
||||
|
||||
(defcustom erc-kill-channel-hook '(erc-kill-channel)
|
||||
(defcustom erc-kill-channel-hook
|
||||
'(erc-kill-channel
|
||||
erc-networks-shrink-ids-and-buffer-names
|
||||
erc-networks-rename-surviving-target-buffer)
|
||||
"Invoked whenever a channel-buffer is killed via `kill-buffer'."
|
||||
:package-version '(ERC . "5.4.1") ; FIXME increment upon publishing to ELPA
|
||||
: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-networks-shrink-ids-and-buffer-names
|
||||
erc-networks-rename-surviving-target-buffer)
|
||||
"Hook run whenever a query buffer is killed.
|
||||
|
||||
See also `kill-buffer'."
|
||||
:package-version '(ERC . "5.4.1") ; FIXME increment upon publishing to ELPA
|
||||
:group 'erc-hooks
|
||||
:type 'hook)
|
||||
|
||||
|
|
|
|||
1707
test/lisp/erc/erc-networks-tests.el
Normal file
1707
test/lisp/erc/erc-networks-tests.el
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -566,8 +566,9 @@
|
|||
(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-networks--id (erc-networks--id-create nil))
|
||||
(erc-log-irc-protocol ":irc.gnu.org 422 tester :MOTD missing")
|
||||
(setq erc-network 'BarNet)
|
||||
(setq erc-networks--id (erc-networks--id-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*"
|
||||
|
|
|
|||
Loading…
Reference in a new issue