mxoemu/Reality/Source/ConsoleThread.cpp

190 lines
5.2 KiB
C++
Raw Normal View History

// ***************************************************************************
//
// Reality - The Matrix Online Server Emulator
2009-08-11 12:17:38 +00:00
// Copyright (C) 2006-2010 Rajko Stojadinovic
// http://mxoemu.info
2009-08-11 12:17:38 +00:00
//
// ---------------------------------------------------------------------------
2009-08-11 12:17:38 +00:00
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
2009-08-11 12:17:38 +00:00
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
2009-08-11 12:17:38 +00:00
//
// ---------------------------------------------------------------------------
2009-08-11 12:17:38 +00:00
//
// ***************************************************************************
2009-08-11 12:17:38 +00:00
#include "Common.h"
#include "Log.h"
#include "ConsoleThread.h"
#include "Util.h"
#include "Master.h"
#include "Crypto.h"
#include "GameServer.h"
#include "AuthServer.h"
2009-08-11 12:17:38 +00:00
2010-02-17 14:24:29 +00:00
#include <boost/algorithm/string.hpp>
using boost::iequals;
2009-08-11 12:17:38 +00:00
bool ConsoleThread::run()
{
SetThreadName("Console Thread");
for (;;)
{
string command;
cin >> command;
2009-08-11 12:17:38 +00:00
2010-02-17 14:24:29 +00:00
if (iequals(command, "exit"))
2009-08-11 12:17:38 +00:00
{
Master::m_stopEvent = true;
INFO_LOG("Got exit command. Shutting down...");
break;
}
2010-02-17 14:24:29 +00:00
else if (iequals(command,"register"))
{
string theLine;
getline(cin,theLine);
stringstream lineParser;
lineParser.str(theLine);
string username,password;
lineParser >> username;
lineParser >> password;
2010-02-13 14:32:04 +00:00
if (username.length() < 1 || password.length() < 1)
{
WARNING_LOG("Invalid username or password");
}
else
{
bool accountCreated = sAuth.CreateAccount(username,password);
if (accountCreated)
INFO_LOG(format("Created account with username %1% password %2%") % username % password );
}
}
2010-02-17 14:24:29 +00:00
else if (iequals(command,"changePassword"))
{
string theLine;
getline(cin,theLine);
stringstream lineParser;
lineParser.str(theLine);
string username,newPassword;
lineParser >> username;
lineParser >> newPassword;
bool passwordChanged = sAuth.ChangePassword(username,newPassword);
if (passwordChanged)
INFO_LOG(format("Account %1% now has password %2%") % username % newPassword );
}
else if (iequals(command,"createWorld"))
{
string theLine;
getline(cin,theLine);
stringstream lineParser;
lineParser.str(theLine);
string worldName;
lineParser >> worldName;
bool worldCreated = sAuth.CreateWorld(worldName);
if (worldCreated)
INFO_LOG(format("Created world named %1%") % worldName );
}
else if (iequals(command,"createCharacter"))
{
string theLine;
getline(cin,theLine);
stringstream lineParser;
lineParser.str(theLine);
string worldName,userName,charHandle,firstName,lastName;
lineParser >> worldName;
lineParser >> userName;
lineParser >> charHandle;
lineParser >> firstName;
lineParser >> lastName;
bool characterCreated = sAuth.CreateCharacter(worldName,userName,charHandle,firstName,lastName);
if (characterCreated)
INFO_LOG(format("Inserted character %1% into world %2% for user %3% with name %4% %5%")
% charHandle % worldName % userName % firstName % lastName );
}
else if (iequals(command, "broadcastMsg") || iequals(command, "modalMsg"))
2010-02-18 18:52:55 +01:00
{
string theAnnouncement;
getline(cin,theAnnouncement);
while (theAnnouncement.c_str()[0] == ' ' || theAnnouncement.c_str()[0] == '\n')
{
theAnnouncement = theAnnouncement.substr(1);
}
if (theAnnouncement.size() > 0)
{
if (iequals(command, "broadcastMsg"))
sGame.AnnounceCommand(NULL,make_shared<BroadcastMsg>(theAnnouncement));
else if (iequals(command, "modalMsg"))
sGame.AnnounceCommand(NULL,make_shared<ModalMsg>(theAnnouncement));
2010-02-18 18:52:55 +01:00
cout << "OK" << std::endl;
}
else
{
cout << "EMPTY MESSAGE" << std::endl;
}
}
else if (iequals(command, "send") || iequals(command, "sendCmd") )
{
stringstream hexStream;
for (;;)
{
string word;
cin >> word;
string::size_type semicolonPos = word.find_first_of(";");
if (semicolonPos != string::npos)
{
word = word.substr(0,semicolonPos);
if (word.length() > 0)
{
hexStream << word;
}
break;
}
else
{
hexStream << word;
}
}
string binaryOutput;
try
{
CryptoPP::HexDecoder hexDecoder(new CryptoPP::StringSink(binaryOutput));
hexDecoder.Put((const byte*)hexStream.str().data(),hexStream.str().size(),true);
hexDecoder.MessageEnd();
}
catch (...)
{
2010-02-14 15:35:36 +00:00
cout << "Invalid hex string" << std::endl;
continue;
}
bool rpcCmd=false;
if (iequals(command, "sendCmd"))
rpcCmd=true;
sGame.Broadcast(ByteBuffer(binaryOutput),rpcCmd);
2010-02-14 15:35:36 +00:00
cout << "OK" << std::endl;
}
2009-08-11 12:17:38 +00:00
}
return true;
}