diff --git a/cockatrice/src/window_main.cpp b/cockatrice/src/window_main.cpp index de65d4d1..ce85f37f 100644 --- a/cockatrice/src/window_main.cpp +++ b/cockatrice/src/window_main.cpp @@ -83,7 +83,7 @@ void MainWindow::processConnectionClosedEvent(const Event_ConnectionClosed &even break; } case Event_ConnectionClosed::SERVER_SHUTDOWN: reasonStr = tr("Scheduled server shutdown."); break; - case Event_ConnectionClosed::USERNAMEINVALID: reasonStr = tr("Invalid username.\nYou may only use A-Z, a-z, 0-9, _, ., and - in your username."); break; + case Event_ConnectionClosed::USERNAMEINVALID: reasonStr = tr("Invalid username."); break; default: reasonStr = QString::fromStdString(event.reason_str()); } QMessageBox::critical(this, tr("Connection closed"), tr("The server has terminated your connection.\nReason: %1").arg(reasonStr)); @@ -306,9 +306,12 @@ void MainWindow::loginError(Response::ResponseCode r, QString reasonStr, quint32 QMessageBox::critical(this, tr("Error"), bannedStr); break; } - case Response::RespUsernameInvalid: - QMessageBox::critical(this, tr("Error"), tr("Invalid username.\nYou may only use A-Z, a-z, 0-9, _, ., and - in your username.")); + case Response::RespUsernameInvalid: { + QString errorStr; + extractInvalidUsernameMessage(reasonStr, errorStr); + QMessageBox::critical(this, tr("Error"), errorStr); break; + } case Response::RespRegistrationRequired: if (QMessageBox::question(this, tr("Error"), tr("This server requires user registration. Do you want to register now?"), QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) { actRegister(); @@ -332,6 +335,36 @@ void MainWindow::loginError(Response::ResponseCode r, QString reasonStr, quint32 actConnect(); } +void MainWindow::extractInvalidUsernameMessage(QString & in, QString & out) +{ + out = tr("Invalid username.") + "
"; + QStringList rules = in.split(QChar('|')); + if (rules.size() == 7) + { + out += tr("The username must respect these rules:") + "
"; + } else { + out += tr("You may only use A-Z, a-z, 0-9, _, ., and - in your username."); + } +} + void MainWindow::registerError(Response::ResponseCode r, QString reasonStr, quint32 endTime) { switch (r) { @@ -362,36 +395,10 @@ void MainWindow::registerError(Response::ResponseCode r, QString reasonStr, quin QMessageBox::critical(this, tr("Error"), bannedStr); break; } - case Response::RespUsernameInvalid: - { - QString error = tr("Invalid username.") + "
"; - QStringList rules = reasonStr.split(QChar('|')); - if (rules.size() == 7) - { - error += tr("The username must respect these rules:") + "
"; - } else { - error += tr("You may only use A-Z, a-z, 0-9, _, ., and - in your username."); - } - - QMessageBox::critical(this, tr("Error"), error); + case Response::RespUsernameInvalid: { + QString errorStr; + extractInvalidUsernameMessage(reasonStr, errorStr); + QMessageBox::critical(this, tr("Error"), errorStr); break; } case Response::RespRegistrationFailed: diff --git a/cockatrice/src/window_main.h b/cockatrice/src/window_main.h index 57b9e780..9bde26b1 100644 --- a/cockatrice/src/window_main.h +++ b/cockatrice/src/window_main.h @@ -109,6 +109,7 @@ public: protected: void closeEvent(QCloseEvent *event); void changeEvent(QEvent *event); + void extractInvalidUsernameMessage(QString & in, QString & out); }; #endif diff --git a/common/server_protocolhandler.cpp b/common/server_protocolhandler.cpp index 46e2bb83..7df87189 100644 --- a/common/server_protocolhandler.cpp +++ b/common/server_protocolhandler.cpp @@ -388,7 +388,12 @@ Response::ResponseCode Server_ProtocolHandler::cmdLogin(const Command_Login &cmd } case NotLoggedIn: return Response::RespWrongPassword; case WouldOverwriteOldSession: return Response::RespWouldOverwriteOldSession; - case UsernameInvalid: return Response::RespUsernameInvalid; + case UsernameInvalid: { + Response_Login *re = new Response_Login; + re->set_denied_reason_str(reasonStr.toStdString()); + rc.setResponseExtension(re); + return Response::RespUsernameInvalid; + } case RegistrationRequired: return Response::RespRegistrationRequired; case UserIsInactive: return Response::RespAccountNotActivated; default: authState = res; diff --git a/servatrice/src/servatrice_database_interface.cpp b/servatrice/src/servatrice_database_interface.cpp index 7aa5fa06..b48fb738 100644 --- a/servatrice/src/servatrice_database_interface.cpp +++ b/servatrice/src/servatrice_database_interface.cpp @@ -247,8 +247,7 @@ AuthenticationResult Servatrice_DatabaseInterface::checkUserPassword(Server_Prot if (!checkSql()) return UnknownUser; - QString error; - if (!usernameIsValid(user, error)) + if (!usernameIsValid(user, reasonStr)) return UsernameInvalid; if (checkUserIsBanned(handler->getAddress(), user, reasonStr, banSecondsLeft))