From 5d6d13bb12f0832b394df56aab84cdb43b816380 Mon Sep 17 00:00:00 2001 From: Griatch Date: Tue, 12 Jan 2016 21:35:27 +0100 Subject: [PATCH] Added a validation-check when changing encodings using the default commands (unloggedin/general). Also put in a safety catch in the sessionhandler to reset faulty encodings that slips by for some reason. This can otherwise lead to errors for shutting down the server since the server fails trying to inform the faulty session about the shutdown. --- evennia/commands/default/player.py | 10 ++++++++-- evennia/commands/default/unloggedin.py | 9 +++++++-- evennia/server/sessionhandler.py | 8 +++++++- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/evennia/commands/default/player.py b/evennia/commands/default/player.py index 55aab5eb2d..734bc45c3d 100644 --- a/evennia/commands/default/player.py +++ b/evennia/commands/default/player.py @@ -416,8 +416,14 @@ class CmdOption(MuxPlayerCommand): # change encoding old_encoding = self.session.encoding new_encoding = self.rhs.strip() or "utf-8" - self.session.encoding = new_encoding - self.caller.msg("Encoding was changed from %s to %s." % (old_encoding, new_encoding)) + try: + utils.to_str(utils.to_unicode("test-string"), encoding=new_encoding) + except LookupError: + string = "|rThe encoding '|w%s|r' is invalid. Keeping the previous encoding '|w%s|r'.|n" % (new_encoding, old_encoding) + else: + self.session.encoding = new_encoding + string = "Encoding was changed from '|w%s|n' to '|w%s|n'." % (old_encoding, new_encoding) + self.caller.msg(string) return if self.lhs == "screenreader": diff --git a/evennia/commands/default/unloggedin.py b/evennia/commands/default/unloggedin.py index 051bab4f3b..7a28eacd61 100644 --- a/evennia/commands/default/unloggedin.py +++ b/evennia/commands/default/unloggedin.py @@ -482,8 +482,13 @@ class CmdUnconnectedEncoding(MuxCommand): # change encoding old_encoding = self.session.encoding encoding = self.args - self.session.encoding = encoding - string = "Your custom text encoding was changed from '%s' to '%s'." % (old_encoding, encoding) + try: + utils.to_str(utils.to_unicode("test-string"), encoding=encoding) + except LookupError: + string = "|rThe encoding '|w%s|r' is invalid. Keeping the previous encoding '|w%s|r'.|n" % (encoding, old_encoding) + else: + self.session.encoding = encoding + string = "Your custom text encoding was changed from '|w%s|n' to '|w%s|n'." % (old_encoding, encoding) self.caller.msg(string.strip()) class CmdUnconnectedScreenreader(MuxCommand): diff --git a/evennia/server/sessionhandler.py b/evennia/server/sessionhandler.py index 47b7f90c90..82526dd432 100644 --- a/evennia/server/sessionhandler.py +++ b/evennia/server/sessionhandler.py @@ -524,7 +524,13 @@ class ServerSessionHandler(SessionHandler): #from evennia.server.profiling.timetrace import timetrace #text = timetrace(text, "ServerSessionHandler.data_out") - text = text and to_str(to_unicode(text), encoding=session.encoding) + try: + text = text and to_str(to_unicode(text), encoding=session.encoding) + except LookupError: + # wrong encoding set on the session. Set it to a safe one + session.encoding = "utf-8" + text = to_str(to_unicode(text), encoding=session.encoding) + # send across AMP self.server.amp_protocol.send_MsgServer2Portal(session,