3
0
mirror of https://github.com/jlu5/PyLink.git synced 2025-02-17 14:01:03 +01:00

relay: mangle <( to [ and >) to ] for better displays

This commit is contained in:
James Lu 2019-06-24 15:07:52 -07:00
parent 729abbd6bf
commit caa94f983f

View File

@ -133,6 +133,14 @@ IRC_ASCII_ALLOWED_CHARS = string.digits + string.ascii_letters + '/^|\\-_[]{}`'
FALLBACK_SEPARATOR = '|'
FALLBACK_CHARACTER = '-'
def _replace_special(text):
"""
Replaces brackets and spaces by similar IRC-representable characters.
"""
for pair in {('(', '['), (')', ']'), (' ', FALLBACK_CHARACTER), ('<', '['), ('>', ']')}:
text = text.replace(pair[0], pair[1])
return text
def _sanitize(text, extrachars=''):
"""Replaces characters not in IRC_ASCII_ALLOWED_CHARS with FALLBACK_CHARACTER."""
whitelist = IRC_ASCII_ALLOWED_CHARS + extrachars
@ -165,9 +173,9 @@ def normalize_nick(irc, netname, nick, times_tagged=0, uid=''):
nick = base64.b64encode(nick.encode(irc.encoding, 'replace'), altchars=b'[]')
nick = nick.decode()
# Normalize spaces to hyphens
nick = nick.replace(' ', FALLBACK_CHARACTER)
netname = netname.replace(' ', FALLBACK_CHARACTER)
# Normalize spaces to hyphens, () => []
nick = _replace_special(nick)
netname = _replace_special(netname)
# Get the nick/net separator
separator = irc.serverdata.get('separator') or \