diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/accounthandler.cpp | 28 | ||||
-rw-r--r-- | src/dalstorage.cpp | 34 |
2 files changed, 49 insertions, 13 deletions
diff --git a/src/accounthandler.cpp b/src/accounthandler.cpp index 0f7a8b5e..63b2d3d6 100644 --- a/src/accounthandler.cpp +++ b/src/accounthandler.cpp @@ -29,6 +29,7 @@ #include "state.h" #include "configuration.h" #include <iostream> +#include <cctype> using tmwserv::Account; using tmwserv::AccountPtr; @@ -114,13 +115,32 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) std::string email = message.readString(); // checking conditions for having a good account. - // TODO: Test if the email already exists using: - // REGISTER_EXISTS_EMAIL - // The DALstorage will first need to have some getListOf Email function. std::cout << username << " is trying to register." << std::endl; + bool emailValid = true; + // looking the email address already exists. + std::list<std::string> emailList = store.getEmailList(); + std::string upcasedEmail, upcasedIt; + for (std::list<std::string>::const_iterator it = emailList.begin(); it != emailList.end(); it++) + { + // Upcasing both mails for a good comparison + upcasedEmail = email; + std::transform(upcasedEmail.begin(), upcasedEmail.end(), upcasedEmail.begin(), (int(*)(int))std::toupper); + upcasedIt = *it; + std::transform(upcasedIt.begin(), upcasedIt.end(), upcasedIt.begin(), (int(*)(int))std::toupper); + if ( upcasedEmail == upcasedIt ) + { + result.writeShort(SMSG_REGISTER_RESPONSE); + result.writeByte(REGISTER_EXISTS_EMAIL); + std::cout << email << ": Email already exists" << std::endl; + emailValid = false; + break; + } + } + if (!emailValid) break; + // Testing Email validity - bool emailValid = false; + emailValid = false; if ((email.find_first_of('@') != std::string::npos)) // Searching for an @. { int atpos = email.find_first_of('@'); diff --git a/src/dalstorage.cpp b/src/dalstorage.cpp index 447e3a88..c62b0dc4 100644 --- a/src/dalstorage.cpp +++ b/src/dalstorage.cpp @@ -29,7 +29,6 @@ #include "utils/logger.h" #include "dalstorage.h" -#include "dalstoragesql.h" namespace tmwserv { @@ -346,15 +345,32 @@ DALStorage::delAccount(const std::string& userName) std::list<std::string> DALStorage::getEmailList() { + // If not opened already + open(); + std::list <std::string> emailList; - Accounts::iterator it = mAccounts.begin(); - Accounts::iterator it_end = mAccounts.end(); - for (; it != it_end; ) - { - emailList.push_front( ((it->first).get())->getEmail() ); - std::cout << ((it->first).get())->getEmail() << std::endl; - ++it; - } + try { + std::string sql("select email from "); + sql += ACCOUNTS_TBL_NAME; + sql += ";"; + const dal::RecordSet& accountInfo = mDb->execSql(sql); + + // if the account is not even in the database then + // we have no choice but to return nothing. + if (accountInfo.isEmpty()) { + return emailList; + } + for (unsigned int i = 0; i < accountInfo.rows(); i++) + { + // We add all these addresses to the list + emailList.push_front(accountInfo(i, 0)); + } + } + catch (const dal::DbSqlQueryExecFailure& e) { + // TODO: throw an exception. + LOG_ERROR("SQL query failure: " << e.what()) + } + return emailList; } |