Skip to content

Commit

Permalink
Merge pull request #205 from progval/patch-2
Browse files Browse the repository at this point in the history
Fix parsing of message tags
  • Loading branch information
jaraco authored Aug 13, 2023
2 parents 77af272 + f856fd3 commit 021eee2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
21 changes: 16 additions & 5 deletions irc/message.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import re


_TAG_UNESCAPE_RE = re.compile(r'\\(.)')
_TAG_UNESCAPE_MAP = {':': ';', 's': ' ', 'n': '\n', 'r': '\r', '\\': '\\'}


class Tag:
"""
An IRC message tag ircv3.net/specs/core/message-tags-3.2.html
Expand All @@ -23,13 +30,17 @@ def parse(item):
>>> Tag.parse('x=a\\nb\\nc')['value']
'a\nb\nc'
>>> Tag.parse(r'x=a\bb\bc')['value']
'abbbc'
>>> Tag.parse(r'x=a\\nb\\nc')['value']
'a\\nb\\nc'
"""
key, sep, value = item.partition('=')
value = value.replace('\\:', ';')
value = value.replace('\\s', ' ')
value = value.replace('\\n', '\n')
value = value.replace('\\r', '\r')
value = value.replace('\\\\', '\\')
value = _TAG_UNESCAPE_RE.sub(
lambda char: _TAG_UNESCAPE_MAP.get(char.group(1), char.group(1)), value
)
value = value or None
return {'key': key, 'value': value}

Expand Down
1 change: 1 addition & 0 deletions newsfragments/205.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Better handling of escape sequences in message tags.

0 comments on commit 021eee2

Please sign in to comment.