Unescape tag values

This commit is contained in:
2023-09-10 18:53:24 +02:00
parent b33e72523c
commit 7f34d39118
2 changed files with 28 additions and 0 deletions

View File

@ -44,10 +44,26 @@
(write-command (:writer client) command))
(.flush (:writer client)))
(defn unescape-tag-value [v]
(str/replace v #"\\(.)" (fn [[_ c]]
(case c
":" ";"
"s" " "
"\\" "\\"
"r" "\r"
"n" "\n"
c))))
(defn parse-tag [[k v]]
(if v
[k (unescape-tag-value v)]
[k ""]))
(defn parse-tags [s]
(as-> s v
(str/split v #";")
(map #(str/split % #"=" 2) v)
(map parse-tag v)
(into {} v)))
(def line-re

View File

@ -52,10 +52,22 @@
(is (= (parse-command "@label=abc ACK")
{:tags {"label" "abc"} :source nil :cmd "ACK" :params []})))
(testing "parsing a command with a tag containing escaped chars"
(is (= (parse-command "@label=ab\\:cd\\sef\\rgh\\nij\\klm ACK")
{:tags {"label" "ab;cd ef\rgh\nijklm"} :source nil :cmd "ACK" :params []})))
(testing "parsing a command with tags"
(is (= (parse-command "@label=abc;time=123 ACK")
{:tags {"label" "abc", "time", "123"} :source nil :cmd "ACK" :params []})))
(testing "parsing a command with a tag with an empty value"
(is (= (parse-command "@tag= ACK")
{:tags {"tag" ""} :source nil :cmd "ACK" :params []})))
(testing "parsing a command with a tag with no value"
(is (= (parse-command "@tag ACK")
{:tags {"tag" ""} :source nil :cmd "ACK" :params []})))
(testing "parsing a command with source and tag"
(is (= (parse-command "@label=abc :nick!user@host AWAY")
{:tags {"label" "abc"} :source "nick!user@host" :cmd "AWAY" :params []})))