From b8951a61d312a16469a6b977e9c95d5bce84fdfb Mon Sep 17 00:00:00 2001 From: lotodore Date: Sun, 15 Nov 2009 08:41:29 +0000 Subject: [PATCH] Implementing guest login. --- src/gamedata.h | 1 + .../internetgamelogindialogimpl.cpp | 13 +++- src/gui/qt/startwindow/startwindowimpl.cpp | 2 +- src/net/common/clientstate.cpp | 63 ++++++++++++------- src/net/common/serverlobbythread.cpp | 15 ++++- 5 files changed, 67 insertions(+), 27 deletions(-) diff --git a/src/gamedata.h b/src/gamedata.h index c9c835fb..a4aad630 100644 --- a/src/gamedata.h +++ b/src/gamedata.h @@ -26,6 +26,7 @@ #include #define SERVER_COMPUTER_PLAYER_NAME "Computer" +#define SERVER_GUEST_PLAYER_NAME "Guest" typedef std::list PlayerIdList; diff --git a/src/gui/qt/internetgamelogindialog/internetgamelogindialogimpl.cpp b/src/gui/qt/internetgamelogindialog/internetgamelogindialogimpl.cpp index e82133ad..689f70f7 100644 --- a/src/gui/qt/internetgamelogindialog/internetgamelogindialogimpl.cpp +++ b/src/gui/qt/internetgamelogindialog/internetgamelogindialogimpl.cpp @@ -1,5 +1,6 @@ #include "internetgamelogindialogimpl.h" #include "configfile.h" +#include #include internetGameLoginDialogImpl::internetGameLoginDialogImpl(QWidget *parent, ConfigFile *c) : @@ -59,7 +60,17 @@ void internetGameLoginDialogImpl::accept() { if(groupBox_reguser->isChecked()) { myConfig->writeConfigInt("InternetLoginMode", 0); - myConfig->writeConfigString("MyName", lineEdit_username->text().toUtf8().constData()); + if(checkBox_guest->isChecked()) { + // Generate a valid guest name. + QString guestName; + int guestId; + Tools::getRandNumber(1, 99999, 1, &guestId, false); + guestName.sprintf("Guest%05d", guestId); + myConfig->writeConfigString("MyName", guestName.toUtf8().constData()); + } + else { + myConfig->writeConfigString("MyName", lineEdit_username->text().toUtf8().constData()); + } if(checkBox_rememberPassword->isChecked()) { myConfig->writeConfigInt("InternetSavePassword", 1); myConfig->writeConfigString("InternetLoginPassword", lineEdit_password->text().toUtf8().toBase64().constData()); diff --git a/src/gui/qt/startwindow/startwindowimpl.cpp b/src/gui/qt/startwindow/startwindowimpl.cpp index 6c2f27e9..dcdd4b41 100644 --- a/src/gui/qt/startwindow/startwindowimpl.cpp +++ b/src/gui/qt/startwindow/startwindowimpl.cpp @@ -334,7 +334,7 @@ void startWindowImpl::callInternetGameLoginDialog() { if(myInternetGameLoginDialog->result() == QDialog::Accepted) { //send login infos mySession->setLogin( - myInternetGameLoginDialog->lineEdit_username->text().toUtf8().constData(), + myConfig->readConfigString("MyName"), myInternetGameLoginDialog->lineEdit_password->text().toUtf8().constData(), myInternetGameLoginDialog->checkBox_guest->isChecked()); } diff --git a/src/net/common/clientstate.cpp b/src/net/common/clientstate.cpp index ae8b6d92..170b04b1 100644 --- a/src/net/common/clientstate.cpp +++ b/src/net/common/clientstate.cpp @@ -1078,37 +1078,52 @@ ClientStateWaitEnterLogin::TimerLoop(const boost::system::error_code& ec, boost: if (client->GetLoginData(loginData)) { ClientContext &context = client->GetContext(); - context.SetPlayerName(loginData.userName); - context.SetPassword(loginData.password); - // TODO handle guest login. boost::shared_ptr init(new NetPacket(NetPacket::Alloc)); init->GetMsg()->present = PokerTHMessage_PR_initMessage; InitMessage_t *netInit = &init->GetMsg()->choice.initMessage; netInit->requestedVersion.major = NET_VERSION_MAJOR; netInit->requestedVersion.minor = NET_VERSION_MINOR; - netInit->login.present = login_PR_authenticatedLogin; - AuthenticatedLogin_t *authLogin = &netInit->login.choice.authenticatedLogin; - // Send authentication user data for challenge/response in init. - boost::shared_ptr tmpSession = context.GetSessionData(); - tmpSession->CreateClientAuthSession(client->GetAuthContext(), context.GetPlayerName(), context.GetPassword()); - if (!tmpSession->AuthStep(1, "")) - throw ClientException(__FILE__, __LINE__, ERR_NET_INVALID_PASSWORD, 0); - string outUserData(tmpSession->AuthGetNextOutMsg()); - OCTET_STRING_fromBuf(&authLogin->clientUserData, - outUserData.c_str(), - outUserData.length()); - string avatarFile = client->GetQtToolsInterface().stringFromUtf8(context.GetAvatarFile()); - if (!avatarFile.empty()) + + context.SetPlayerName(loginData.userName); + + // Handle guest login first. + if (loginData.isGuest) { - MD5Buf tmpMD5; - if (client->GetAvatarManager().GetHashForAvatar(avatarFile, tmpMD5)) + context.SetPassword(""); + netInit->login.present = login_PR_guestLogin; + GuestLogin_t *guestLogin = &netInit->login.choice.guestLogin; + OCTET_STRING_fromBuf(&guestLogin->nickName, + context.GetPlayerName().c_str(), + context.GetPlayerName().length()); + } + // If the player is not a guest, authenticate. + else + { + context.SetPassword(loginData.password); + netInit->login.present = login_PR_authenticatedLogin; + AuthenticatedLogin_t *authLogin = &netInit->login.choice.authenticatedLogin; + // Send authentication user data for challenge/response in init. + boost::shared_ptr tmpSession = context.GetSessionData(); + tmpSession->CreateClientAuthSession(client->GetAuthContext(), context.GetPlayerName(), context.GetPassword()); + if (!tmpSession->AuthStep(1, "")) + throw ClientException(__FILE__, __LINE__, ERR_NET_INVALID_PASSWORD, 0); + string outUserData(tmpSession->AuthGetNextOutMsg()); + OCTET_STRING_fromBuf(&authLogin->clientUserData, + outUserData.c_str(), + outUserData.length()); + string avatarFile = client->GetQtToolsInterface().stringFromUtf8(context.GetAvatarFile()); + if (!avatarFile.empty()) { - // TODO: use sha1. - authLogin->avatar = - OCTET_STRING_new_fromBuf( - &asn_DEF_OCTET_STRING, - (const char *)tmpMD5.data, - MD5_DATA_SIZE); + MD5Buf tmpMD5; + if (client->GetAvatarManager().GetHashForAvatar(avatarFile, tmpMD5)) + { + // TODO: use sha1. + authLogin->avatar = + OCTET_STRING_new_fromBuf( + &asn_DEF_OCTET_STRING, + (const char *)tmpMD5.data, + MD5_DATA_SIZE); + } } } diff --git a/src/net/common/serverlobbythread.cpp b/src/net/common/serverlobbythread.cpp index 8f6b6e91..b22745a7 100644 --- a/src/net/common/serverlobbythread.cpp +++ b/src/net/common/serverlobbythread.cpp @@ -941,11 +941,24 @@ ServerLobbyThread::HandleNetPacketInit(SessionWrapper session, const InitMessage string playerName; MD5Buf avatarMD5; bool noAuth = false; + bool validGuest = false; if (initMessage.login.present == login_PR_guestLogin) { const GuestLogin_t *guestLogin = &initMessage.login.choice.guestLogin; playerName = STL_STRING_FROM_OCTET_STRING(guestLogin->nickName); - noAuth = true; + // Verify guest player name. + if (playerName.length() > sizeof(SERVER_GUEST_PLAYER_NAME - 1) + && playerName.substr(0, sizeof(SERVER_GUEST_PLAYER_NAME) - 1) == SERVER_GUEST_PLAYER_NAME) + { + string guestId(playerName.substr(sizeof(SERVER_GUEST_PLAYER_NAME))); + if (count_if(guestId.begin(), guestId.end(), isdigit) == guestId.size()) + { + validGuest = true; + noAuth = true; + } + } + if (!validGuest) + SessionError(session, ERR_NET_INVALID_PLAYER_NAME); } #ifdef POKERTH_OFFICIAL_SERVER else if (initMessage.login.present == login_PR_authenticatedLogin)