Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add spam protection for score commands (and clean whitespaces a bit better) #9

Merged
merged 4 commits into from
Dec 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions src/game/server/race.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,7 @@ void CGameContext::ChatConRank(IConsole::IResult *pResult, void *pUser)
{
char aStr[256];
str_copy(aStr, pResult->GetString(0), sizeof(aStr));

// strip trailing spaces
int i = str_length(aStr);
while(i >= 0)
{
if (aStr[i] < 0 || aStr[i] > 32)
break;
aStr[i] = 0;
i--;
}

str_clean_whitespaces(aStr);
pSelf->Score()->ShowRank(pSelf->m_ChatConsoleClientID, aStr);
}
else
Expand Down
2 changes: 2 additions & 0 deletions src/game/server/score.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class IScore
protected:
CPlayerData m_aPlayerData[MAX_CLIENTS];
int m_CurrentRecord;
int m_LastPrintInChat[MAX_CLIENTS];

public:
IScore() { m_CurrentRecord = 0; }
Expand All @@ -78,6 +79,7 @@ class IScore
m_CurrentRecord = 0;
for(int i = 0; i < MAX_CLIENTS; i++)
m_aPlayerData[i].Reset();
mem_zero(m_LastPrintInChat, sizeof(m_LastPrintInChat));
}

virtual void Tick() { }
Expand Down
21 changes: 16 additions & 5 deletions src/game/server/score/file_score.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,16 +292,16 @@ void CFileScore::ShowTop5(int ClientID, int Debut)
{
char aBuf[512];
char aTime[64];
GameServer()->SendChat(-1, CHAT_ALL, ClientID, "----------- Top 5 -----------");
PrintInChat(ClientID, CHAT_ALL, ClientID, "----------- Top 5 -----------", true);
for(int i = 0; i < 5 && i + Debut - 1 < m_lTop.size(); i++)
{
const CPlayerScore *r = &m_lTop[i+Debut-1];
IRace::FormatTimeLong(aTime, sizeof(aTime), r->m_Time);
str_format(aBuf, sizeof(aBuf), "%d. %s Time: %s",
i + Debut, r->m_aName, aTime);
GameServer()->SendChat(-1, CHAT_ALL, ClientID, aBuf);
PrintInChat(ClientID, CHAT_ALL, ClientID, aBuf, true);
}
GameServer()->SendChat(-1, CHAT_ALL, ClientID, "------------------------------");
PrintInChat(ClientID, CHAT_ALL, ClientID, "------------------------------");
}

void CFileScore::ShowRank(int ClientID, const char *pName)
Expand Down Expand Up @@ -329,7 +329,7 @@ void CFileScore::ShowRank(int ClientID, const char *pName)
else
str_format(aBuf, sizeof(aBuf), "%s is not ranked", pName);

GameServer()->SendChat(-1, CHAT_ALL, To, aBuf);
PrintInChat(ClientID, CHAT_ALL, To, aBuf);
}

void CFileScore::ShowRank(int ClientID)
Expand All @@ -353,5 +353,16 @@ void CFileScore::ShowRank(int ClientID)
else
str_format(aBuf, sizeof(aBuf), "You are not ranked");

GameServer()->SendChat(-1, CHAT_ALL, To, aBuf);
PrintInChat(ClientID, CHAT_ALL, To, aBuf);
}

void CFileScore::PrintInChat(int From, int Mode, int To, const char *pText, bool IsBlock)
{
// 3 seconds cooldown for chat commands
if(g_Config.m_SvSpamprotection && m_LastPrintInChat[From] && m_LastPrintInChat[From]+Server()->TickSpeed()*3 > Server()->Tick())
return;

GameServer()->SendChat(-1, Mode, To, pText);
if(IsBlock)
m_LastPrintInChat[From] = Server()->Tick();
}
2 changes: 2 additions & 0 deletions src/game/server/score/file_score.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class CFileScore : public IScore
void WriteEntry(IOHANDLE File, const CPlayerScore *pEntry) const;
IOHANDLE OpenFile(int Flags) const;

void PrintInChat(int From, int Mode, int To, const char *pText, bool IsBlock = true);

public:
CFileScore(CGameContext *pGameServer);
~CFileScore();
Expand Down