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.

This commit is contained in:
Griatch 2016-01-12 21:35:27 +01:00
parent 33c479fe04
commit 5d6d13bb12
3 changed files with 22 additions and 5 deletions

View file

@ -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":

View file

@ -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):

View file

@ -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,