From 9400a13b541f60d1c09ca82eeb71e1c9c420eb91 Mon Sep 17 00:00:00 2001 From: Bjørn Lindeijer Date: Wed, 2 Aug 2006 18:50:19 +0000 Subject: Put handling of logout, unregister and password change messages into separate methods. --- ChangeLog | 7 +- src/accounthandler.cpp | 211 +++++++++++++++++++++++++++---------------------- src/accounthandler.h | 9 +++ 3 files changed, 132 insertions(+), 95 deletions(-) diff --git a/ChangeLog b/ChangeLog index 09df2da1..10c7ebb5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,9 +1,14 @@ +2006-08-02 Bjørn Lindeijer + + * src/accounthandler.cpp, src/accounthandler.h: Put handling of + logout, unregister and password change messages into separate methods. + 2006-08-02 Guillaume Melquiond * src/Makefile.am: Put object files in subdirectories. Removed compilation flags. -2006-08-01 Eugenio Favalli +2006-08-02 Eugenio Favalli * tmwserv.cbp, tmwserv.dev: Updated project files. diff --git a/src/accounthandler.cpp b/src/accounthandler.cpp index 2f3f1669..b08e34e4 100644 --- a/src/accounthandler.cpp +++ b/src/accounthandler.cpp @@ -78,22 +78,7 @@ void AccountHandler::processMessage(NetComputer *comp, MessageIn &message) break; case PAMSG_LOGOUT: - { - result.writeShort(APMSG_LOGOUT_RESPONSE); - - if ( computer.getAccount().get() == NULL ) - { - LOG_INFO("Can't logout. Not even logged in.", 1); - result.writeByte(ERRMSG_NO_LOGIN); - } - else - { - LOG_INFO(computer.getAccount()->getName() << " logs out.", 1); - // computer.unsetCharacter(); Done by unsetAccount(); - computer.unsetAccount(); - result.writeByte(ERRMSG_OK); - } - } + handleLogoutMessage(computer, message); break; case PAMSG_REGISTER: @@ -101,47 +86,7 @@ void AccountHandler::processMessage(NetComputer *comp, MessageIn &message) break; case PAMSG_UNREGISTER: - { - std::string username = message.readString(); - std::string password = message.readString(); - LOG_INFO(username << " wants to be deleted from our accounts.", 1); - - result.writeShort(APMSG_UNREGISTER_RESPONSE); - - if (stringFilter->findDoubleQuotes(username)) - { - result.writeByte(ERRMSG_INVALID_ARGUMENT); - LOG_INFO(username << ": has got double quotes in it.", 1); - break; - } - - // see if the account exists - AccountPtr accPtr = store.getAccount(username); - - if (!accPtr.get() || accPtr->getPassword() != password) { - LOG_INFO("Account does not exist or bad password for " - << username << ".", 1); - - result.writeByte(ERRMSG_INVALID_ARGUMENT); - } else { - - // If the account to delete is the current account we're logged in. - // Get out of it in memory. - if (computer.getAccount().get() != NULL ) - { - if (computer.getAccount()->getName() == username ) - { - // computer.unsetCharacter(); Done by unsetAccount(); - computer.unsetAccount(); - } - } - // delete account and associated characters - LOG_INFO("Farewell " << username << " ...", 1); - store.delAccount(username); - store.flush(); - result.writeByte(ERRMSG_OK); - } - } + handleUnregisterMessage(computer, message); break; case PAMSG_EMAIL_CHANGE: @@ -198,43 +143,7 @@ void AccountHandler::processMessage(NetComputer *comp, MessageIn &message) break; case PAMSG_PASSWORD_CHANGE: - { - result.writeShort(APMSG_PASSWORD_CHANGE_RESPONSE); - - if (computer.getAccount().get() == NULL) - { - result.writeByte(ERRMSG_NO_LOGIN); - LOG_INFO("Not logged in. Can't change your Account's Password.", 1); - break; - } - std::string oldPassword = message.readString(); - std::string newPassword = message.readString(); - if ( newPassword.length() < MIN_PASSWORD_LENGTH || - newPassword.length() > MAX_PASSWORD_LENGTH ) - { - result.writeByte(ERRMSG_INVALID_ARGUMENT); - LOG_INFO(computer.getAccount()->getName() << - ": New password too long or too short.", 1); - } - else if (stringFilter->findDoubleQuotes(newPassword)) - { - result.writeByte(ERRMSG_INVALID_ARGUMENT); - LOG_INFO(newPassword << ": has got double quotes in it.", 1); - } - else if ( oldPassword != computer.getAccount()->getPassword() ) - { - result.writeByte(ERRMSG_FAILURE); - LOG_INFO(computer.getAccount()->getName() << - ": Old password is wrong.", 1); - } - else - { - computer.getAccount()->setPassword(newPassword); - result.writeByte(ERRMSG_OK); - LOG_INFO(computer.getAccount()->getName() << - ": The password was changed.", 1); - } - } + handlePasswordChangeMessage(computer, message); break; case PAMSG_CHAR_CREATE: @@ -639,6 +548,26 @@ AccountHandler::handleLoginMessage(AccountClient &computer, MessageIn &msg) computer.send(reply.getPacket()); } +void +AccountHandler::handleLogoutMessage(AccountClient &computer, MessageIn &msg) +{ + MessageOut reply(APMSG_LOGOUT_RESPONSE); + + if (computer.getAccount().get() == NULL) + { + LOG_INFO("Can't logout. Not even logged in.", 1); + reply.writeByte(ERRMSG_NO_LOGIN); + } + else + { + LOG_INFO(computer.getAccount()->getName() << " logged out.", 1); + computer.unsetAccount(); + reply.writeByte(ERRMSG_OK); + } + + computer.send(reply.getPacket()); +} + void AccountHandler::handleRegisterMessage(AccountClient &computer, MessageIn &msg) { @@ -719,3 +648,97 @@ AccountHandler::handleRegisterMessage(AccountClient &computer, MessageIn &msg) computer.send(reply.getPacket()); } + +void +AccountHandler::handleUnregisterMessage(AccountClient &computer, + MessageIn &msg) +{ + std::string username = msg.readString(); + std::string password = msg.readString(); + + LOG_INFO(username << " wants to be deleted from our accounts.", 1); + + MessageOut reply(APMSG_UNREGISTER_RESPONSE); + + if (stringFilter->findDoubleQuotes(username)) + { + LOG_INFO(username << ": has got double quotes in it.", 1); + reply.writeByte(ERRMSG_INVALID_ARGUMENT); + } + else + { + // See if the account exists + Storage &store = Storage::instance("tmw"); + AccountPtr accPtr = store.getAccount(username); + + if (!accPtr.get() || accPtr->getPassword() != password) + { + LOG_INFO("Account does not exist or bad password for " + << username << ".", 1); + reply.writeByte(ERRMSG_INVALID_ARGUMENT); + } + else + { + // If the account to delete is the current account we're logged in. + // Get out of it in memory. + if (computer.getAccount().get() != NULL ) + { + if (computer.getAccount()->getName() == username) + { + computer.unsetAccount(); + } + } + + // Delete account and associated characters + LOG_INFO("Farewell " << username << " ...", 1); + store.delAccount(username); + store.flush(); + reply.writeByte(ERRMSG_OK); + } + } + + computer.send(reply.getPacket()); +} + +void +AccountHandler::handlePasswordChangeMessage(AccountClient &computer, + MessageIn &msg) +{ + std::string oldPassword = msg.readString(); + std::string newPassword = msg.readString(); + + MessageOut reply(APMSG_PASSWORD_CHANGE_RESPONSE); + + if (computer.getAccount().get() == NULL) + { + LOG_INFO("Not logged in. Can't change your Account's Password.", 1); + reply.writeByte(ERRMSG_NO_LOGIN); + } + else if (newPassword.length() < MIN_PASSWORD_LENGTH || + newPassword.length() > MAX_PASSWORD_LENGTH) + { + LOG_INFO(computer.getAccount()->getName() << + ": New password too long or too short.", 1); + reply.writeByte(ERRMSG_INVALID_ARGUMENT); + } + else if (stringFilter->findDoubleQuotes(newPassword)) + { + LOG_INFO(newPassword << ": has got double quotes in it.", 1); + reply.writeByte(ERRMSG_INVALID_ARGUMENT); + } + else if (oldPassword != computer.getAccount()->getPassword()) + { + LOG_INFO(computer.getAccount()->getName() << + ": Old password is wrong.", 1); + reply.writeByte(ERRMSG_FAILURE); + } + else + { + LOG_INFO(computer.getAccount()->getName() << + ": The password was changed.", 1); + computer.getAccount()->setPassword(newPassword); + reply.writeByte(ERRMSG_OK); + } + + computer.send(reply.getPacket()); +} diff --git a/src/accounthandler.h b/src/accounthandler.h index 9f23f207..a5a94d15 100644 --- a/src/accounthandler.h +++ b/src/accounthandler.h @@ -56,8 +56,17 @@ class AccountHandler : public ConnectionHandler void handleLoginMessage(AccountClient &computer, MessageIn &msg); + void + handleLogoutMessage(AccountClient &computer, MessageIn &msg); + void handleRegisterMessage(AccountClient &computer, MessageIn &msg); + + void + handleUnregisterMessage(AccountClient &computer, MessageIn &msg); + + void + handlePasswordChangeMessage(AccountClient &computer, MessageIn &msg); }; #endif -- cgit v1.2.3-60-g2f50