From 131f7157c4616caf1378a990a4d87721e076655c Mon Sep 17 00:00:00 2001 From: Ryan Stein Date: Fri, 3 Nov 2017 12:45:24 -0400 Subject: [PATCH] Use a more robust method of validating an encoding. --- evennia/commands/default/account.py | 3 ++- evennia/commands/default/unloggedin.py | 3 ++- evennia/server/inputfuncs.py | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/evennia/commands/default/account.py b/evennia/commands/default/account.py index 85a25a5ca..73078ce46 100644 --- a/evennia/commands/default/account.py +++ b/evennia/commands/default/account.py @@ -21,6 +21,7 @@ method. Otherwise all text will be returned to all connected sessions. from builtins import range import time +from codecs import lookup as codecs_lookup from django.conf import settings from evennia.server.sessionhandler import SESSIONS from evennia.utils import utils, create, search, evtable @@ -533,7 +534,7 @@ class CmdOption(COMMAND_DEFAULT_CLASS): def validate_encoding(new_encoding): # helper: change encoding try: - b"test-string".decode(new_encoding) + codecs_lookup(new_encoding) except LookupError: raise RuntimeError("The encoding '|w%s|n' is invalid. " % new_encoding) return val diff --git a/evennia/commands/default/unloggedin.py b/evennia/commands/default/unloggedin.py index 149fd0382..784d1981c 100644 --- a/evennia/commands/default/unloggedin.py +++ b/evennia/commands/default/unloggedin.py @@ -3,6 +3,7 @@ Commands that are available from the connect screen. """ import re import time +from codecs import lookup as codecs_lookup from collections import defaultdict from random import getrandbits from django.conf import settings @@ -481,7 +482,7 @@ class CmdUnconnectedEncoding(COMMAND_DEFAULT_CLASS): old_encoding = self.session.protocol_flags.get("ENCODING", None) encoding = self.args try: - utils.to_str(b"test-string".decode(encoding)) + codecs_lookup(encoding) except LookupError: string = "|rThe encoding '|w%s|r' is invalid. Keeping the previous encoding '|w%s|r'.|n"\ % (encoding, old_encoding) diff --git a/evennia/server/inputfuncs.py b/evennia/server/inputfuncs.py index 9ed439ae1..76953bc48 100644 --- a/evennia/server/inputfuncs.py +++ b/evennia/server/inputfuncs.py @@ -21,6 +21,7 @@ settings.INPUT_FUNC_MODULES. from future.utils import viewkeys import importlib +from codecs import lookup as codecs_lookup from django.conf import settings from evennia.commands.cmdhandler import cmdhandler from evennia.accounts.models import AccountDB @@ -176,7 +177,7 @@ def client_options(session, *args, **kwargs): def validate_encoding(val): # helper: change encoding try: - b"test-string".decode(val) + codecs_lookup(val) except LookupError: raise RuntimeError("The encoding '|w%s|n' is invalid. " % val) return val