diff --git a/evennia/objects/objects.py b/evennia/objects/objects.py index de9d09aba..23d623bfb 100644 --- a/evennia/objects/objects.py +++ b/evennia/objects/objects.py @@ -2063,6 +2063,13 @@ class DefaultCharacter(DefaultObject): # If no typeclass supplied, use this class kwargs["typeclass"] = kwargs.pop("typeclass", cls) + # Normalize to latin characters and validate, if necessary, the supplied key + key = cls.normalize_name(key) + + if not cls.validate_name(key): + errors.append("Invalid character name.") + return obj, errors + # Set the supplied key as the name of the intended object kwargs["key"] = key @@ -2112,6 +2119,37 @@ class DefaultCharacter(DefaultObject): return obj, errors + @classmethod + def normalize_name(cls, name): + """ + Normalize the character name prior to creating. Note that this should be refactored + to support i18n for non-latin scripts, but as we (currently) have no bug reports requesting better + support of non-latin character sets, requiring character names to be latinified is an acceptable option. + + Args: + name (str) : The name of the character + + Returns: + latin_name (str) : A valid name. + """ + + from evennia.utils.utils import latinify + latin_name = latinify(name, default="X") + return latin_name + + @classmethod + def validate_name(cls, name): + """ Validate the character name prior to creating. Overload this function to add custom validators + + Args: + name (str) : The name of the character + Returns: + valid (bool) : True if character creation should continue; False if it should fail + + """ + + return True # Default validator does not perform any operations + def basetype_setup(self): """ Setup character-specific security. diff --git a/evennia/objects/tests.py b/evennia/objects/tests.py index 75f91cba3..5944ee5aa 100644 --- a/evennia/objects/tests.py +++ b/evennia/objects/tests.py @@ -39,6 +39,12 @@ class DefaultObjectTest(EvenniaTest): self.assertFalse(errors, errors) self.assertEqual(obj.db_home, self.room1) + def test_character_create_weirdname(self): + obj, errors = DefaultCharacter.create("SigurðurÞórarinsson", self.account, home=self.room1.dbref) + self.assertTrue(obj, errors) + self.assertFalse(errors, errors) + self.assertEqual(obj.name, "SigurXurXorarinsson") + def test_room_create(self): description = "A dimly-lit alley behind the local Chinese restaurant." obj, errors = DefaultRoom.create("alley", self.account, description=description, ip=self.ip)