From c96a626dce2a12d1415e0d2c6e9eaeac8c5a1d75 Mon Sep 17 00:00:00 2001 From: "F. Jason Park" <jp@neverwas.me> Date: Fri, 1 Sep 2023 20:05:35 -0700 Subject: [PATCH] Allow login to contain NUH delimiters in erc-parse-user * lisp/erc/erc.el (erc--parse-user-regexp-legacy, erc--parse-user-regexp-pedantic): New constants. The first is the original pattern that matches across line endings. The second disallows multiline strings and interprets excess delimiting characters as part of the middle, "user" component as per RFC1459. The latter distinction is largely academic because most servers reject such logins anyway, but bridges to other protocols and future extensions may need to exploit this for novel uses. (erc--parse-user-regexp): New variable, currently set to `erc--parse-user-regexp-legacy'. (erc-parse-user): Keep original pattern as default, but do so indirectly via `erc--parse-user-regexp'. * test/lisp/erc/erc-tests.el (erc-parse-user): New test. --- lisp/erc/erc.el | 12 +++++++++++- test/lisp/erc/erc-tests.el | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/lisp/erc/erc.el b/lisp/erc/erc.el index 7375b5308ea..f7237c9e334 100644 --- a/lisp/erc/erc.el +++ b/lisp/erc/erc.el @@ -6316,12 +6316,22 @@ EmacsSpeak support." (defalias 'erc-list 'ensure-list) +(defconst erc--parse-user-regexp-pedantic + (rx bot (group (* (not (any "!\r\n")))) + "!" (group (* nonl)) + "@" (group (* nonl)) eot)) + +(defconst erc--parse-user-regexp-legacy + "^\\([^!\n]*\\)!\\([^@\n]*\\)@\\(.*\\)$") + +(defvar erc--parse-user-regexp erc--parse-user-regexp-legacy) + (defun erc-parse-user (string) "Parse STRING as a user specification (nick!login@host). Return a list of the three separate tokens." (cond - ((string-match "^\\([^!\n]*\\)!\\([^@\n]*\\)@\\(.*\\)$" string) + ((string-match erc--parse-user-regexp string) (list (match-string 1 string) (match-string 2 string) (match-string 3 string))) diff --git a/test/lisp/erc/erc-tests.el b/test/lisp/erc/erc-tests.el index 9fdad823059..40334d7d894 100644 --- a/test/lisp/erc/erc-tests.el +++ b/test/lisp/erc/erc-tests.el @@ -576,6 +576,39 @@ (setq erc-lurker-ignore-chars "_-`") ; set of chars, not character alts (should (string= "nick" (erc-lurker-maybe-trim "nick-_`"))))) +(ert-deftest erc-parse-user () + (should (equal erc--parse-user-regexp erc--parse-user-regexp-legacy)) + + (should (equal '("" "" "") (erc-parse-user "!@"))) + (should (equal '("" "!" "") (erc-parse-user "!!@"))) + (should (equal '("" "" "@") (erc-parse-user "!@@"))) + (should (equal '("" "!" "@") (erc-parse-user "!!@@"))) + + (should (equal '("abc" "" "") (erc-parse-user "abc"))) + (should (equal '("" "123" "fake") (erc-parse-user "!123@fake"))) + (should (equal '("abc" "" "123") (erc-parse-user "abc!123"))) + + (should (equal '("abc" "123" "fake") (erc-parse-user "abc!123@fake"))) + (should (equal '("abc" "!123" "@xy") (erc-parse-user "abc!!123@@xy"))) + + (should (equal '("de" "fg" "xy") (erc-parse-user "abc\nde!fg@xy"))) + + (ert-info ("`erc--parse-user-regexp-pedantic'") + (let ((erc--parse-user-regexp erc--parse-user-regexp-pedantic)) + (should (equal '("" "" "") (erc-parse-user "!@"))) + (should (equal '("" "!" "") (erc-parse-user "!!@"))) + (should (equal '("" "@" "") (erc-parse-user "!@@"))) + (should (equal '("" "!@" "") (erc-parse-user "!!@@"))) + + (should (equal '("abc" "" "") (erc-parse-user "abc"))) + (should (equal '("" "123" "fake") (erc-parse-user "!123@fake"))) + (should (equal '("abc" "" "123") (erc-parse-user "abc!123"))) + + (should (equal '("abc" "123" "fake") (erc-parse-user "abc!123@fake"))) + (should (equal '("abc" "!123@" "xy") (erc-parse-user "abc!!123@@xy"))) + + (should (equal '("de" "" "fg@xy") (erc-parse-user "abc\nde!fg@xy")))))) + (ert-deftest erc--parse-isupport-value () (should (equal (erc--parse-isupport-value "a,b") '("a" "b"))) (should (equal (erc--parse-isupport-value "a,b,c") '("a" "b" "c"))) -- 2.39.5