Skip to content

Commit

Permalink
/msg, /ban, /kick
Browse files Browse the repository at this point in the history
  • Loading branch information
necropotame committed Nov 14, 2016
1 parent 46527ac commit 18c6ee8
Show file tree
Hide file tree
Showing 8 changed files with 351 additions and 78 deletions.
10 changes: 7 additions & 3 deletions src/engine/console.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class IConsole : public IInterface
protected:
unsigned m_NumArgs;
int m_ClientID;
bool m_TeamChat;

public:
IResult() { m_NumArgs = 0; m_ClientID = -1; }
Expand All @@ -44,7 +45,10 @@ class IConsole : public IInterface
virtual const char *GetString(unsigned Index) = 0;

int GetClientID() { return m_ClientID; }
bool GetTeamChat() { return m_TeamChat; }

void SetClientID(int ClientID) { m_ClientID = ClientID; }
void SetTeamChat(bool TeamChat) { m_TeamChat = TeamChat; }

int NumArguments() const { return m_NumArgs; }
};
Expand Down Expand Up @@ -84,9 +88,9 @@ class IConsole : public IInterface
virtual void StoreCommands(bool Store) = 0;

virtual bool LineIsValid(const char *pStr) = 0;
virtual void ExecuteLine(const char *Sptr, int ClientID) = 0;
virtual void ExecuteLineFlag(const char *Sptr, int ClientID, int FlasgMask) = 0;
virtual void ExecuteLineStroked(int Stroke, const char *pStr, int ClientID) = 0;
virtual void ExecuteLine(const char *Sptr, int ClientID, bool TeamChat) = 0;
virtual void ExecuteLineFlag(const char *Sptr, int ClientID, bool TeamChat, int FlasgMask) = 0;
virtual void ExecuteLineStroked(int Stroke, const char *pStr, int ClientID, bool TeamChat) = 0;
virtual void ExecuteFile(const char *pFilename) = 0;

virtual int RegisterPrintCallback(int OutputLevel, FPrintCallback pfnPrintCallback, void *pUserData) = 0;
Expand Down
95 changes: 75 additions & 20 deletions src/engine/server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ void CServerBan::InitServerBan(IConsole *pConsole, IStorage *pStorage, CServer*
m_pServer = pServer;

// overwrites base command, todo: improve this
Console()->Register("ban", "s?ir", CFGFLAG_SERVER|CFGFLAG_STORE, ConBanExt, this, "Ban player with ip/client id for x minutes for any reason");
Console()->Register("ban", "s<username or uid> ?i<minutes> ?r<reason>", CFGFLAG_CHAT|CFGFLAG_SERVER|CFGFLAG_STORE, ConBanExt, this, "Ban player with ip/client id for x minutes for any reason");
}

template<class T>
Expand Down Expand Up @@ -273,7 +273,7 @@ int CServerBan::BanRange(const CNetRange *pRange, int Seconds, const char *pReas

bool CServerBan::ConBanExt(IConsole::IResult *pResult, void *pUser)
{
CServerBan *pThis = static_cast<CServerBan *>(pUser);
CServerBan *pThis = static_cast<CServerBan*>(pUser);

const char *pStr = pResult->GetString(0);
int Minutes = pResult->NumArguments()>1 ? clamp(pResult->GetInteger(1), 0, 44640) : 30;
Expand All @@ -290,7 +290,20 @@ bool CServerBan::ConBanExt(IConsole::IResult *pResult, void *pUser)
}
}
else
ConBan(pResult, pUser);
{
int NumPlayerFound = 0;
for(int i=0; i<MAX_CLIENTS; i++)
{
if(pThis->m_pServer->m_aClients[i].m_State != CServer::CClient::STATE_EMPTY && str_comp(pThis->m_pServer->ClientName(i), pStr) == 0)
{
NumPlayerFound++;
pThis->BanAddr(pThis->Server()->m_NetServer.ClientAddr(i), Minutes*60, pReason);
}
}

if(!NumPlayerFound)
ConBan(pResult, pUser);
}

return true;
}
Expand Down Expand Up @@ -383,6 +396,7 @@ CServer::~CServer()
int CServer::TrySetClientName(int ClientID, const char *pName)
{
char aTrimmedName[64];
char aTrimmedName2[64];

// trim the name
str_copy(aTrimmedName, StrLtrim(pName), sizeof(aTrimmedName));
Expand All @@ -392,30 +406,30 @@ int CServer::TrySetClientName(int ClientID, const char *pName)
if(!aTrimmedName[0])
return -1;

// check if new and old name are the same
if(m_aClients[ClientID].m_aName[0] && str_comp(m_aClients[ClientID].m_aName, aTrimmedName) == 0)
return 0;

char aBuf[256];
str_format(aBuf, sizeof(aBuf), "'%s' -> '%s'", pName, aTrimmedName);
Console()->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "server", aBuf);
pName = aTrimmedName;

// make sure that two clients doesn't have the same name
for(int i = 0; i < MAX_CLIENTS; i++)
{
if(i != ClientID && m_aClients[i].m_State >= CClient::STATE_READY)
{
if(str_comp(pName, ClientName(i)) == 0)
str_copy(aTrimmedName2, ClientName(i), sizeof(aTrimmedName2));
StrRtrim(aTrimmedName2);

if(str_comp(pName, aTrimmedName2) == 0)
return -1;
}
}

// check if new and old name are the same
if(m_aClients[ClientID].m_aName[0] && str_comp(m_aClients[ClientID].m_aName, pName) == 0)
return 0;

// set the client name
str_copy(m_aClients[ClientID].m_aName, pName, MAX_NAME_LENGTH);
return 0;
}



void CServer::SetClientName(int ClientID, const char *pName)
{
if(ClientID < 0 || ClientID >= MAX_CLIENTS || m_aClients[ClientID].m_State < CClient::STATE_READY)
Expand Down Expand Up @@ -1290,7 +1304,7 @@ void CServer::ProcessClientPacket(CNetChunk *pPacket)
default:
Console()->SetAccessLevel(IConsole::ACCESS_LEVEL_USER);
}
Console()->ExecuteLineFlag(pCmd, ClientID, CFGFLAG_SERVER);
Console()->ExecuteLineFlag(pCmd, ClientID, false, CFGFLAG_SERVER);
Console()->SetAccessLevel(IConsole::ACCESS_LEVEL_ADMIN);
m_RconClientID = IServer::RCON_CID_SERV;
m_RconAuthLevel = AUTHED_ADMIN;
Expand Down Expand Up @@ -1975,6 +1989,25 @@ int CServer::Run()
m_CurrentGameTick++;
NewTicks++;

//Check for name collision. We add this because the login is in a different thread and can't check it himself.
for(int i=MAX_CLIENTS-1; i>=0; i--)
{
if(m_aClients[i].m_State >= CClient::STATE_READY && m_aClients[i].m_UserID < 0)
{
if(TrySetClientName(i, m_aClients[i].m_aName))
{
// auto rename
for(int j = 1;; j++)
{
char aNameTry[MAX_NAME_LENGTH];
str_format(aNameTry, sizeof(aNameTry), "(%d)%s", j, m_aClients[i].m_aName);
if(TrySetClientName(i, aNameTry) == 0)
break;
}
}
}
}

for(int i=0; i<MAX_CLIENTS; i++)
{
if(m_aClients[i].m_WaitingTime > 0)
Expand Down Expand Up @@ -2104,14 +2137,36 @@ int CServer::Run()

bool CServer::ConKick(IConsole::IResult *pResult, void *pUser)
{
if(pResult->NumArguments() > 1)
CServer* pThis = (CServer *)pUser;

char aBuf[128];
const char *pStr = pResult->GetString(0);
const char *pReason = pResult->NumArguments()>1 ? pResult->GetString(1) : "No reason given";
str_format(aBuf, sizeof(aBuf), "Kicked (%s)", pReason);

if(CNetDatabase::StrAllnum(pStr))
{
char aBuf[128];
str_format(aBuf, sizeof(aBuf), "Kicked (%s)", pResult->GetString(1));
((CServer *)pUser)->Kick(pResult->GetInteger(0), aBuf);
int ClientID = str_toint(pStr);
if(ClientID < 0 || ClientID >= MAX_CLIENTS || pThis->m_aClients[ClientID].m_State == CServer::CClient::STATE_EMPTY)
pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "Server", "Invalid client id");
else
pThis->Kick(ClientID, aBuf);
}
else
((CServer *)pUser)->Kick(pResult->GetInteger(0), "Kicked by console");
{
int NumPlayerFound = 0;
for(int i=0; i<MAX_CLIENTS; i++)
{
if(pThis->m_aClients[i].m_State != CServer::CClient::STATE_EMPTY && str_comp(pThis->ClientName(i), pStr) == 0)
{
NumPlayerFound++;
pThis->Kick(i, aBuf);
}
}

if(!NumPlayerFound)
pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "Server", "No player was found with this name");
}

return true;
}
Expand Down Expand Up @@ -2400,7 +2455,7 @@ void CServer::RegisterCommands()
m_pStorage = Kernel()->RequestInterface<IStorage>();

// register console commands
Console()->Register("kick", "i?r", CFGFLAG_SERVER, ConKick, this, "Kick player with specified id for any reason");
Console()->Register("kick", "s<username or uid> ?r<reason>", CFGFLAG_CHAT|CFGFLAG_SERVER, ConKick, this, "Kick player with specified id for any reason");
Console()->Register("status", "", CFGFLAG_SERVER, ConStatus, this, "List players");
Console()->Register("shutdown", "", CFGFLAG_SERVER, ConShutdown, this, "Shut down");
Console()->Register("logout", "", CFGFLAG_SERVER, ConLogout, this, "Logout of rcon");
Expand Down
21 changes: 11 additions & 10 deletions src/engine/shared/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,13 @@ bool CConsole::LineIsValid(const char *pStr)
return true;
}

void CConsole::ExecuteLineStroked(int Stroke, const char *pStr, int ClientID)
void CConsole::ExecuteLineStroked(int Stroke, const char *pStr, int ClientID, bool TeamChat)
{
while(pStr && *pStr)
{
CResult Result;
Result.SetClientID(ClientID);
Result.SetTeamChat(TeamChat);
const char *pEnd = pStr;
const char *pNextPart = 0;
int InString = 0;
Expand Down Expand Up @@ -418,17 +419,17 @@ CConsole::CCommand *CConsole::FindCommand(const char *pName, int FlagMask)
return 0x0;
}

void CConsole::ExecuteLine(const char *pStr, int ClientID)
void CConsole::ExecuteLine(const char *pStr, int ClientID, bool TeamChat)
{
CConsole::ExecuteLineStroked(1, pStr, ClientID); // press it
CConsole::ExecuteLineStroked(0, pStr, ClientID); // then release it
CConsole::ExecuteLineStroked(1, pStr, ClientID, TeamChat); // press it
CConsole::ExecuteLineStroked(0, pStr, ClientID, TeamChat); // then release it
}

void CConsole::ExecuteLineFlag(const char *pStr, int ClientID, int FlagMask)
void CConsole::ExecuteLineFlag(const char *pStr, int ClientID, bool TeamChat, int FlagMask)
{
int Temp = m_FlagMask;
m_FlagMask = FlagMask;
ExecuteLine(pStr, ClientID);
ExecuteLine(pStr, ClientID, TeamChat);
m_FlagMask = Temp;
}

Expand Down Expand Up @@ -466,7 +467,7 @@ void CConsole::ExecuteFile(const char *pFilename)
lr.Init(File);

while((pLine = lr.Get()))
ExecuteLine(pLine, -1);
ExecuteLine(pLine, -1, false);

io_close(File);
}
Expand Down Expand Up @@ -645,7 +646,7 @@ bool CConsole::ConToggle(IConsole::IResult *pResult, void *pUser)
CIntVariableData *pData = static_cast<CIntVariableData *>(pUserData);
int Val = *(pData->m_pVariable)==pResult->GetInteger(1) ? pResult->GetInteger(2) : pResult->GetInteger(1);
str_format(aBuf, sizeof(aBuf), "%s %i", pResult->GetString(0), Val);
pConsole->ExecuteLine(aBuf, -1);
pConsole->ExecuteLine(aBuf, -1, false);
aBuf[0] = 0;
}
else
Expand Down Expand Up @@ -680,7 +681,7 @@ bool CConsole::ConToggleStroke(IConsole::IResult *pResult, void *pUser)
{
int Val = pResult->GetInteger(0)==0 ? pResult->GetInteger(3) : pResult->GetInteger(2);
str_format(aBuf, sizeof(aBuf), "%s %i", pResult->GetString(1), Val);
pConsole->ExecuteLine(aBuf, -1);
pConsole->ExecuteLine(aBuf, -1, false);
aBuf[0] = 0;
}
else
Expand Down Expand Up @@ -760,7 +761,7 @@ void CConsole::ParseArguments(int NumArgs, const char **ppArguments)
else
{
// search arguments for overrides
ExecuteLine(ppArguments[i], -1);
ExecuteLine(ppArguments[i], -1, false);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/engine/shared/console.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class CConsole : public IConsole
static bool ConModCommandStatus(IConsole::IResult *pResult, void *pUser);

void ExecuteFileRecurse(const char *pFilename);
void ExecuteLineStroked(int Stroke, const char *pStr, int ClientID);
void ExecuteLineStroked(int Stroke, const char *pStr, int ClientID, bool TeamChat);

struct
{
Expand Down Expand Up @@ -174,8 +174,8 @@ class CConsole : public IConsole
virtual void StoreCommands(bool Store);

virtual bool LineIsValid(const char *pStr);
virtual void ExecuteLine(const char *pStr, int ClientID);
virtual void ExecuteLineFlag(const char *pStr, int ClientID, int FlagMask);
virtual void ExecuteLine(const char *pStr, int ClientID, bool TeamChat);
virtual void ExecuteLineFlag(const char *pStr, int ClientID, bool TeamChat, int FlagMask);
virtual void ExecuteFile(const char *pFilename);

virtual int RegisterPrintCallback(int OutputLevel, FPrintCallback pfnPrintCallback, void *pUserData);
Expand Down
2 changes: 1 addition & 1 deletion src/engine/shared/econ.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ void CEcon::Update()
str_format(aFormatted, sizeof(aFormatted), "cid=%d cmd='%s'", ClientID, aBuf);
Console()->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "server", aFormatted);
m_UserClientID = ClientID;
Console()->ExecuteLine(aBuf, ClientID);
Console()->ExecuteLine(aBuf, ClientID, false);
m_UserClientID = -1;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/engine/shared/netdatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@ class CNetDatabase
str_format(pBuffer, BufferSize, "'%s' - '%s'", aAddrStr1, aAddrStr2);
return pBuffer;
}


public:
static bool StrAllnum(const char *pStr);
};

Expand Down
Loading

0 comments on commit 18c6ee8

Please sign in to comment.