From 1fb099ceb0269e8e74bd0f8dc98eb1da156becbd Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Tue, 8 Dec 2020 18:22:47 +0100 Subject: [PATCH] Make rcirc nick renaming heed nickname max lengths * lisp/net/rcirc.el (rcirc-server-parameters): New variable (bug#6795). (rcirc-connect): Set it. (rcirc-handler-433): Use the length from the server. (rcirc-handler-005): Collect server data. (rcirc-server-parameter-value): New utility function. (rcirc--make-new-nick): New function. --- lisp/net/rcirc.el | 36 +++++++++++++++++++++++++++++++++--- test/lisp/net/rcirc-tests.el | 12 ++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/lisp/net/rcirc.el b/lisp/net/rcirc.el index 63e6eedb200..c4b68f1be4e 100644 --- a/lisp/net/rcirc.el +++ b/lisp/net/rcirc.el @@ -416,6 +416,9 @@ will be killed." (defvar rcirc-server-buffer nil "The server buffer associated with this channel buffer.") +(defvar rcirc-server-parameters nil + "List of parameters received from the server.") + (defvar rcirc-target nil "The channel or user associated with this buffer.") @@ -586,6 +589,7 @@ If ARG is non-nil, instead prompt for connection parameters." (setq-local rcirc-user-disconnect nil) (setq-local rcirc-user-authenticated nil) (setq-local rcirc-connecting t) + (setq-local rcirc-server-parameters nil) (add-hook 'auto-save-hook 'rcirc-log-write) @@ -2873,9 +2877,28 @@ Not in rfc1459.txt" (defun rcirc-handler-433 (process sender args text) "ERR_NICKNAMEINUSE" (rcirc-handler-generic process "433" sender args text) - (let* ((new-nick (concat (cadr args) "`"))) - (with-rcirc-process-buffer process - (rcirc-cmd-nick new-nick nil process)))) + (with-rcirc-process-buffer process + (let* ((length (string-to-number + (or (rcirc-server-parameter-value 'nicklen) + "16")))) + (rcirc-cmd-nick (rcirc--make-new-nick (cadr args) length) nil process)))) + +(defun rcirc--make-new-nick (nick length) + ;; If we already have some ` chars at the end, then shorten the + ;; non-` bit of the name. + (when (= (length nick) length) + (setq nick (replace-regexp-in-string "[^`]\\(`+\\)\\'" "\\1" nick))) + (concat + (if (>= (length nick) length) + (substring nick 0 (1- length)) + nick) + "`")) + +(defun rcirc-handler-005 (process sender args text) + "ERR_NICKNAMEINUSE" + (rcirc-handler-generic process "005" sender args text) + (with-rcirc-process-buffer process + (setq rcirc-server-parameters (append rcirc-server-parameters args)))) (defun rcirc-authenticate () "Send authentication to process associated with current buffer. @@ -3072,6 +3095,13 @@ Passwords are stored in `rcirc-authinfo' (which see)." (>= (point) rcirc-prompt-end-marker)) +(defun rcirc-server-parameter-value (parameter) + (cl-loop for elem in rcirc-server-parameters + for setting = (split-string elem "=") + when (and (= (length setting) 2) + (string-equal (downcase (car setting)) parameter)) + return (cadr setting))) + (provide 'rcirc) ;;; rcirc.el ends here diff --git a/test/lisp/net/rcirc-tests.el b/test/lisp/net/rcirc-tests.el index 285926af9d2..cbd1c2be830 100644 --- a/test/lisp/net/rcirc-tests.el +++ b/test/lisp/net/rcirc-tests.el @@ -51,4 +51,16 @@ "MODE #cchan +kl :a:b" nil "MODE" '("#cchan" "+kl" "a:b"))) +(ert-deftest rcirc-rename-nicks () + (should (equal (rcirc--make-new-nick "foo" 16) + "foo`")) + (should (equal (rcirc--make-new-nick "123456789012345" 16) + "123456789012345`")) + (should (equal (rcirc--make-new-nick "1234567890123456" 16) + "123456789012345`")) + (should (equal (rcirc--make-new-nick "123456789012345`" 16) + "12345678901234``")) + (should (equal (rcirc--make-new-nick "123456789012````" 16) + "12345678901`````"))) + ;;; rcirc-tests.el ends here -- 2.39.2