From dbbacb4bb32976b1539648e052d1d3bbb438fc4d Mon Sep 17 00:00:00 2001 From: lu yijun Date: Sun, 7 Sep 2014 02:22:12 +0800 Subject: [PATCH] This fixes issue #569 --- src/commands/default/cmdset_unloggedin.py | 1 + src/commands/default/player.py | 14 ++--- src/commands/default/unloggedin.py | 63 +++++++++++++++++++++++ src/players/player.py | 3 -- src/server/amp.py | 4 +- src/server/serversession.py | 10 +++- 6 files changed, 82 insertions(+), 13 deletions(-) diff --git a/src/commands/default/cmdset_unloggedin.py b/src/commands/default/cmdset_unloggedin.py index 21d1dc3bd3..06b0d40c53 100644 --- a/src/commands/default/cmdset_unloggedin.py +++ b/src/commands/default/cmdset_unloggedin.py @@ -21,3 +21,4 @@ class UnloggedinCmdSet(CmdSet): self.add(unloggedin.CmdUnconnectedQuit()) self.add(unloggedin.CmdUnconnectedLook()) self.add(unloggedin.CmdUnconnectedHelp()) + self.add(unloggedin.CmdUnconnectedEncoding()) diff --git a/src/commands/default/player.py b/src/commands/default/player.py index 7e1c5e389a..8826d5bae6 100644 --- a/src/commands/default/player.py +++ b/src/commands/default/player.py @@ -457,19 +457,21 @@ class CmdEncoding(MuxPlayerCommand): """ Sets the encoding. """ - player = self.player + + if self.session is None: + return if 'clear' in self.switches: # remove customization - old_encoding = player.db.encoding + old_encoding = self.session.encoding if old_encoding: string = "Your custom text encoding ('%s') was cleared." % old_encoding else: string = "No custom encoding was set." - del player.db.encoding + self.session.encoding = "utf-8" elif not self.args: # just list the encodings supported - pencoding = player.db.encoding + pencoding = self.session.encoding string = "" if pencoding: string += "Default encoding: {g%s{n (change with {w@encoding {n)" % pencoding @@ -480,9 +482,9 @@ class CmdEncoding(MuxPlayerCommand): string = "No encodings found." else: # change encoding - old_encoding = player.db.encoding + old_encoding = self.session.encoding encoding = self.args - player.db.encoding = encoding + self.session.encoding = encoding string = "Your custom text encoding was changed from '%s' to '%s'." % (old_encoding, encoding) self.msg(string.strip()) diff --git a/src/commands/default/unloggedin.py b/src/commands/default/unloggedin.py index 898f9f4464..cd71362437 100644 --- a/src/commands/default/unloggedin.py +++ b/src/commands/default/unloggedin.py @@ -334,6 +334,69 @@ You can use the {wlook{n command if you want to see the connect screen again. self.caller.msg(string) +class CmdUnconnectedEncoding(MuxCommand): + """ + set which text encoding to use in unconnected-in state + + Usage: + @encoding/switches [] + + Switches: + clear - clear your custom encoding + + + This sets the text encoding for communicating with Evennia. This is mostly + an issue only if you want to use non-ASCII characters (i.e. letters/symbols + not found in English). If you see that your characters look strange (or you + get encoding errors), you should use this command to set the server + encoding to be the same used in your client program. + + Common encodings are utf-8 (default), latin-1, ISO-8859-1 etc. + + If you don't submit an encoding, the current encoding will be displayed + instead. + """ + + key = "@encoding" + aliases = "@encode" + locks = "cmd:all()" + + def func(self): + """ + Sets the encoding. + """ + + if self.session is None: + return + + if 'clear' in self.switches: + # remove customization + old_encoding = self.session.encoding + if old_encoding: + string = "Your custom text encoding ('%s') was cleared." % old_encoding + else: + string = "No custom encoding was set." + self.session.encoding = "utf-8" + elif not self.args: + # just list the encodings supported + pencoding = self.session.encoding + string = "" + if pencoding: + string += "Default encoding: {g%s{n (change with {w@encoding {n)" % pencoding + encodings = settings.ENCODINGS + if encodings: + string += "\nServer's alternative encodings (tested in this order):\n {g%s{n" % ", ".join(encodings) + if not string: + string = "No encodings found." + else: + # 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) + self.caller.msg(string.strip()) + + def _create_player(session, playername, password, default_home, permissions, typeclass=None): """ diff --git a/src/players/player.py b/src/players/player.py index 3615f607a5..142f8187fb 100644 --- a/src/players/player.py +++ b/src/players/player.py @@ -268,9 +268,6 @@ class Player(TypeClass): changing this method. """ - # the text encoding to use. - self.db.encoding = "utf-8" - # A basic security setup lockstring = "examine:perm(Wizards);edit:perm(Wizards);delete:perm(Wizards);boot:perm(Wizards);msg:all()" self.locks.add(lockstring) diff --git a/src/server/amp.py b/src/server/amp.py index 5a07362da0..b2dbe731ce 100644 --- a/src/server/amp.py +++ b/src/server/amp.py @@ -364,7 +364,7 @@ class AMPProtocol(amp.AMP): """ #print "msg portal->server (portal side):", sessid, msg, data return self.safe_send(MsgPortal2Server, sessid, - msg=to_str(msg) if msg is not None else "", + msg=msg if msg is not None else "", data=dumps(data)) # Server -> Portal message @@ -389,7 +389,7 @@ class AMPProtocol(amp.AMP): """ #print "msg server->portal (server side):", sessid, msg, data return self.safe_send(MsgServer2Portal, sessid, - msg=to_str(msg) if msg is not None else "", + msg=msg if msg is not None else "", data=dumps(data)) # Server administration from the Portal side diff --git a/src/server/serversession.py b/src/server/serversession.py index 674a89e17b..eab3b706ad 100644 --- a/src/server/serversession.py +++ b/src/server/serversession.py @@ -13,7 +13,7 @@ from django.conf import settings #from src.scripts.models import ScriptDB from src.comms.models import ChannelDB from src.utils import logger, utils -from src.utils.utils import make_iter, to_unicode, escape_control_sequences +from src.utils.utils import make_iter, to_unicode, to_str, escape_control_sequences from src.commands.cmdhandler import cmdhandler from src.commands.cmdsethandler import CmdSetHandler from src.server.session import Session @@ -198,7 +198,7 @@ class ServerSession(Session): """ if text: # this is treated as a command input - text = to_unicode(escape_control_sequences(text)) + text = to_unicode(escape_control_sequences(text), encoding=self.encoding) # handle the 'idle' command if text.strip() == IDLE_COMMAND: self.update_session_counters(idle=True) @@ -231,6 +231,12 @@ class ServerSession(Session): """ Send Evennia -> User """ + if text is None: + text = "" + else: + text = to_unicode(text) + text = to_str(text, self.encoding) + self.sessionhandler.data_out(self, text=text, **kwargs) def __eq__(self, other):