Skip to content

Commit

Permalink
fix(irc): Discord username with no valid IRC character become empty I…
Browse files Browse the repository at this point in the history
…RC username (#68)

This results in a gazilion problems, ranging from the IRC server
rejecting such username, to highlights being done on random text.

This solution is far from perfect, but with the restrictions on
the username on IRC, it is better than empty.
  • Loading branch information
TrueBrain authored Sep 16, 2022
1 parent d2a3d7c commit 97ffdab
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions dibridge/irc.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,23 @@ async def _relay_mesage(self, irc_username, message):
relay.DISCORD.send_message(irc_username, message)

def _sanitize_discord_username(self, discord_username):
original_discord_username = discord_username

discord_username = discord_username.strip()
# Remove all characters not allowed in IRC usernames.
discord_username = re.sub(r"[^a-zA-Z0-9_\-\[\]\{\}\|]", "", discord_username)
# Make sure a username doesn't start with a number or "-".
discord_username = re.sub(r"^[0-9\-]", "", discord_username)

# On Discord you can create usernames that don't contain any character valid
# on IRC, leaving an empty username. In that case we have no option but to
# replace it with a default placeholder. To make sure the names are somewhat
# stable over multiple runs, we user a partial of the MD5 of the original
# discord name. It is not perfect, but at least it is better than nothing.
if discord_username == "":
postfix = hashlib.sha256(original_discord_username.encode()).hexdigest()
discord_username = f"discord_user_{postfix[0:8]}"

# Make sure a username is no more than 20 character.
# Depending on the IRC network, different lengths are allowed.
discord_username = discord_username[:20]
Expand Down

0 comments on commit 97ffdab

Please sign in to comment.