<commit_before>#include "cloginserver.h"
#include "cloginisc.h"
#include "cloginclient.h"
#include "epackettype.h"
#include "database.h"

using namespace RoseCommon;

CLoginClient::CLoginClient() : CRoseClient(), access_rights_(0) {
}

CLoginClient::CLoginClient(tcp::socket _sock)
    : CRoseClient(std::move(_sock)), access_rights_(0) {
}

void CLoginClient::SendLoginReply(uint8_t Result) {
  CRosePacket* pak =
      new CRosePacket(ePacketType::PAKLC_LOGIN_REPLY, sizeof(pakLoginReply));
  pak->pLoginReply.Result = Result;
  pak->pLoginReply.Right = 0;
  pak->pLoginReply.Type = 0;

  if (Result == 0) {
    pak->pLoginReply.Right = access_rights_;

    // loop the server list here
    std::lock_guard<std::mutex> lock(CLoginServer::GetISCListMutex());
    for (auto& server : CLoginServer::GetISCList()) {
      if (server->GetType() == 1) {
        CLoginISC* svr = (CLoginISC*)server;

        // This if check is needed since the client actually looks for this.
        if (svr->IsTestServer())
          pak->Add<uint8_t>('@');
        else
          pak->Add<uint8_t>(' ');

        pak->AddString(svr->GetName().c_str(), true);
        pak->Add<uint32_t>(svr->GetId());
      }
    }
  }

  this->Send(pak);
}

bool CLoginClient::UserLogin(CRosePacket* P) {

  //todo(raven): check server count
  uint32_t serverCount = 0;
  if (serverCount < 1)
  {
    // Servers are under inspection
    SendLoginReply( 1 );
    return true;
  }

  uint8_t password_[33];
  P->GetBytes(0, 32, password_);
  password_[32] = 0;  // Null term the string

  std::string clientpass((char*)password_);
  P->GetString(32, 16, (char*)username_);

  //todo(raven): make sure the username is safe to exec
  std::unique_ptr<Core::IResult> res;
  std::string query = "CALL UserLogin(";
  query.append( (char*)username_ ).append(");");

  Core::IDatabase &database = Core::databasePool::getInstance().getDatabase();
  res = database.QStore(query);

  if (res != nullptr) {  // Query the DB
    if (res->size() != 0) {
      std::string pwd = "";
      res->getString("password", pwd);

      if (pwd == clientpass)
      {
        uint32_t onlineStatus = 0, accessRights = 0;<commit_msg>Fixed bug with logging into the server.<commit_after>#include "cloginserver.h"
#include "cloginisc.h"
#include "cloginclient.h"
#include "epackettype.h"
#include "database.h"

using namespace RoseCommon;

CLoginClient::CLoginClient() : CRoseClient(), access_rights_(0) {
}

CLoginClient::CLoginClient(tcp::socket _sock)
    : CRoseClient(std::move(_sock)), access_rights_(0) {
}

void CLoginClient::SendLoginReply(uint8_t Result) {
  CRosePacket* pak =
      new CRosePacket(ePacketType::PAKLC_LOGIN_REPLY, sizeof(pakLoginReply));
  pak->pLoginReply.Result = Result;
  pak->pLoginReply.Right = 0;
  pak->pLoginReply.Type = 0;

  if (Result == 0) {
    pak->pLoginReply.Right = access_rights_;

    // loop the server list here
    std::lock_guard<std::mutex> lock(CLoginServer::GetISCListMutex());
    for (auto& server : CLoginServer::GetISCList()) {
      if (server->GetType() == 1) {
        CLoginISC* svr = (CLoginISC*)server;

        // This if check is needed since the client actually looks for this.
        if (svr->IsTestServer())
          pak->Add<uint8_t>('@');
        else
          pak->Add<uint8_t>(' ');

        pak->AddString(svr->GetName().c_str(), true);
        pak->Add<uint32_t>(svr->GetId());
      }
    }
  }

  this->Send(pak);
}

bool CLoginClient::UserLogin(CRosePacket* P) {

  //todo(raven): check server count
  uint32_t serverCount = 0;

  std::lock_guard<std::mutex> lock(CLoginServer::GetISCListMutex());
  for (auto& server : CLoginServer::GetISCList()) {
    if (server->GetType() == 1) serverCount++;
  }

  if (serverCount < 1)
  {
    // Servers are under inspection
    SendLoginReply( 1 );
    return true;
  }

  uint8_t password_[33];
  P->GetBytes(0, 32, password_);
  password_[32] = 0;  // Null term the string

  std::string clientpass((char*)password_);
  P->GetString(32, 16, (char*)username_);

  //todo(raven): make sure the username is safe to exec
  std::unique_ptr<Core::IResult> res;
  std::string query = "CALL UserLogin('";
  query.append( (char*)username_ ).append("');");

  Core::IDatabase &database = Core::databasePool::getInstance().getDatabase();
  res = database.QStore(query);

  if (res != nullptr) {  // Query the DB
    if (res->size() != 0) {
      std::string pwd = "";
      res->getString("password", pwd);

      if (pwd == clientpass)
      {
        uint32_t onlineStatus = 0, accessRights = 0;
{'fraction_non_alphanumeric': 0.1589010989010989, 'fraction_numerical': 0.015164835164835164, 'mean_word_length': 3.601617795753286, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6935554', 'n_tokens_mistral': 1648, 'n_tokens_neox': 1583, 'n_words': 331}