From 480cca82fa58a542cbbc250312f47fa6962c787c Mon Sep 17 00:00:00 2001 From: darkredtitan <70135297+darkredtitan@users.noreply.github.com> Date: Wed, 9 Sep 2020 14:36:35 +0200 Subject: [PATCH] Make name checking case insensitive. Co-authored-by: kamilprzyb --- src/CNLoginServer.cpp | 32 ++++++++++++++++++++++---------- src/CNLoginServer.hpp | 1 + src/Database.cpp | 7 +++++-- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/CNLoginServer.cpp b/src/CNLoginServer.cpp index 1c2f880..d90b0ce 100644 --- a/src/CNLoginServer.cpp +++ b/src/CNLoginServer.cpp @@ -131,7 +131,6 @@ void CNLoginServer::handlePacket(CNSocket* sock, CNPacketData* data) { //Equip info for (int i = 0; i < AEQUIP_COUNT; i++) { - //equip char creation clothes and lightning rifle charInfo.aEquip[i] = it->Equip[i]; } @@ -162,13 +161,22 @@ void CNLoginServer::handlePacket(CNSocket* sock, CNPacketData* data) { if (data->size != sizeof(sP_CL2LS_REQ_CHECK_CHAR_NAME)) return; - // naughty words allowed!!!!!!!! (also for some reason, the client will always show 'Player 0' if you manually type a name. It will show up for other connected players though) sP_CL2LS_REQ_CHECK_CHAR_NAME* nameCheck = (sP_CL2LS_REQ_CHECK_CHAR_NAME*)data->buf; - //check if name is occupied - if (Database::isNameFree(nameCheck)) - { - // naughty words allowed!!!!!!!! (also for some reason, the client will always show 'Player + ID' if you manually type a name. It will show up for other connected players though) + bool success = true; + int errorcode = 0; + //check regex + if (!CNLoginServer::isCharacterNameGood(U16toU8(nameCheck->szFirstName), U16toU8(nameCheck->szLastName))) { + success = false; + errorcode = 4; + } + //check if name isn't already occupied + else if (!Database::isNameFree(nameCheck)){ + success = false; + errorcode = 1; + } + + if (success){ INITSTRUCT(sP_LS2CL_REP_CHECK_CHAR_NAME_SUCC, resp); DEBUGLOG( @@ -178,17 +186,15 @@ void CNLoginServer::handlePacket(CNSocket* sock, CNPacketData* data) { memcpy(resp.szFirstName, nameCheck->szFirstName, sizeof(char16_t) * 9); memcpy(resp.szLastName, nameCheck->szLastName, sizeof(char16_t) * 17); - - // fr*ck allowed!!! + sock->sendPacket((void*)&resp, P_LS2CL_REP_CHECK_CHAR_NAME_SUCC, sizeof(sP_LS2CL_REP_CHECK_CHAR_NAME_SUCC)); } else { INITSTRUCT(sP_LS2CL_REP_CHECK_CHAR_NAME_FAIL, resp); - resp.iErrorCode = 1; + resp.iErrorCode = errorcode; sock->sendPacket((void*)&resp, P_LS2CL_REP_CHECK_CHAR_NAME_FAIL, sizeof(sP_LS2CL_REP_CHECK_CHAR_NAME_FAIL)); } break; - } case P_CL2LS_REQ_SAVE_CHAR_NAME: { if (data->size != sizeof(sP_CL2LS_REQ_SAVE_CHAR_NAME)) @@ -411,4 +417,10 @@ bool CNLoginServer::isPasswordCorrect(std::string actualPassword, std::string tr { return BCrypt::validatePassword(tryPassword, actualPassword); } +bool CNLoginServer::isCharacterNameGood(std::string Firstname, std::string Lastname) +{ + std::regex firstnamecheck("[a-zA-Z0-9]+(?: [a-zA-Z0-9]+)*$"); + std::regex lastnamecheck("[a-zA-Z0-9]+(?: [a-zA-Z0-9]+)*$"); + return (std::regex_match(Firstname, firstnamecheck) && std::regex_match(Lastname, lastnamecheck)); +} #pragma endregion helperMethods diff --git a/src/CNLoginServer.hpp b/src/CNLoginServer.hpp index d76722f..6ca38c9 100644 --- a/src/CNLoginServer.hpp +++ b/src/CNLoginServer.hpp @@ -33,6 +33,7 @@ private: static bool isLoginDataGood(std::string login, std::string password); static bool isPasswordCorrect(std::string actualPassword, std::string tryPassword); static bool isAccountInUse(int accountId); + static bool isCharacterNameGood(std::string Firstname, std::string Lastname); //returns true if success static bool exitDuplicate(int accountId); public: diff --git a/src/Database.cpp b/src/Database.cpp index 6f6e520..1ef5f90 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -23,8 +23,8 @@ auto db = make_storage("database.db", make_column("PlayerID", &Database::DbPlayer::PlayerID, autoincrement(), primary_key()), make_column("AccountID", &Database::DbPlayer::AccountID), make_column("Slot", &Database::DbPlayer::slot), - make_column("Firstname", &Database::DbPlayer::FirstName), - make_column("LastName", &Database::DbPlayer::LastName), + make_column("Firstname", &Database::DbPlayer::FirstName, collate_nocase()), + make_column("LastName", &Database::DbPlayer::LastName, collate_nocase()), make_column("Level", &Database::DbPlayer::Level), make_column("Nano1", &Database::DbPlayer::Nano1), make_column("Nano2", &Database::DbPlayer::Nano2), @@ -361,6 +361,8 @@ Player Database::getPlayer(int id) { #pragma endregion LoginServer +#pragma region ShardServer + void Database::updatePlayer(Player player) { DbPlayer toUpdate = playerToDb(player); db.update(toUpdate); @@ -459,3 +461,4 @@ void Database::getNanos(Player* player) { toSet->iStamina = current.iStamina; } } +#pragma endregion ShardServer