Skip to content
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
42 changes: 21 additions & 21 deletions src/adminsystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1529,7 +1529,7 @@ void CAdminSystem::AddOrUpdateAdmin(uint64 iSteamID, uint64 iFlags, int iAdminIm

bool CAdminSystem::LoadInfractions()
{
m_vecInfractions.PurgeAndDeleteElements();
m_vecInfractions.clear();
KeyValues* pKV = new KeyValues("infractions");
KeyValues::AutoDelete autoDelete(pKV);

Expand Down Expand Up @@ -1568,13 +1568,13 @@ bool CAdminSystem::LoadInfractions()
switch (iType)
{
case CInfractionBase::Ban:
AddInfraction(new CBanInfraction(iEndTime, iSteamId, true));
AddInfraction(std::make_shared<CBanInfraction>(iEndTime, iSteamId, true));
break;
case CInfractionBase::Mute:
AddInfraction(new CMuteInfraction(iEndTime, iSteamId, true));
AddInfraction(std::make_shared<CMuteInfraction>(iEndTime, iSteamId, true));
break;
case CInfractionBase::Gag:
AddInfraction(new CGagInfraction(iEndTime, iSteamId, true));
AddInfraction(std::make_shared<CGagInfraction>(iEndTime, iSteamId, true));
break;
default:
Warning("Invalid infraction type %d\n", iType);
Expand All @@ -1590,7 +1590,7 @@ void CAdminSystem::SaveInfractions()
KeyValues* pSubKey;
KeyValues::AutoDelete autoDelete(pKV);

FOR_EACH_VEC(m_vecInfractions, i)
for (int i = 0; i < m_vecInfractions.size(); i++)
{
time_t timestamp = m_vecInfractions[i]->GetTimestamp();
if (timestamp != 0 && timestamp < std::time(0))
Expand All @@ -1616,17 +1616,17 @@ void CAdminSystem::SaveInfractions()
Warning("Failed to save infractions to %s\n", szPath);
}

void CAdminSystem::AddInfraction(CInfractionBase* infraction)
void CAdminSystem::AddInfraction(std::shared_ptr<CInfractionBase> pInfraction)
{
m_vecInfractions.AddToTail(infraction);
m_vecInfractions.push_back(pInfraction);
}

// This function can run at least twice when a player connects: Immediately upon client connection, and also upon getting authenticated by steam.
// It's also run when we're periodically checking for infraction expiry in the case of mutes/gags.
// This returns false only when called from ClientConnect and the player is banned in order to reject them.
bool CAdminSystem::ApplyInfractions(ZEPlayer* player)
{
FOR_EACH_VEC(m_vecInfractions, i)
for (int i = m_vecInfractions.size() - 1; i >= 0; i--)
{
// Because this can run without the player being authenticated, and the fact that we're applying a ban/mute here,
// we can immediately just use the steamid we got from the connecting player.
Expand All @@ -1642,7 +1642,7 @@ bool CAdminSystem::ApplyInfractions(ZEPlayer* player)
time_t timestamp = m_vecInfractions[i]->GetTimestamp();
if (timestamp != 0 && timestamp <= std::time(0))
{
m_vecInfractions.Remove(i);
m_vecInfractions.erase(m_vecInfractions.begin() + i);
continue;
}

Expand All @@ -1658,12 +1658,12 @@ bool CAdminSystem::ApplyInfractions(ZEPlayer* player)

bool CAdminSystem::FindAndRemoveInfraction(ZEPlayer* player, CInfractionBase::EInfractionType type)
{
FOR_EACH_VEC_BACK(m_vecInfractions, i)
for (int i = m_vecInfractions.size() - 1; i >= 0; i--)
{
if (m_vecInfractions[i]->GetSteamId64() == player->GetSteamId64() && m_vecInfractions[i]->GetType() == type)
{
m_vecInfractions[i]->UndoInfraction(player);
m_vecInfractions.Remove(i);
m_vecInfractions.erase(m_vecInfractions.begin() + i);

return true;
}
Expand All @@ -1674,11 +1674,11 @@ bool CAdminSystem::FindAndRemoveInfraction(ZEPlayer* player, CInfractionBase::EI

bool CAdminSystem::FindAndRemoveInfractionSteamId64(uint64 steamid64, CInfractionBase::EInfractionType type)
{
FOR_EACH_VEC_BACK(m_vecInfractions, i)
for (int i = m_vecInfractions.size() - 1; i >= 0; i--)
{
if (m_vecInfractions[i]->GetSteamId64() == steamid64 && m_vecInfractions[i]->GetType() == type)
{
m_vecInfractions.Remove(i);
m_vecInfractions.erase(m_vecInfractions.begin() + i);

return true;
}
Expand Down Expand Up @@ -1961,31 +1961,31 @@ void ParseInfraction(const CCommand& args, CCSPlayerController* pAdmin, bool bAd
g_pAdminSystem->FindAndRemoveInfraction(zpTarget, infType);
else
{
CInfractionBase* infraction;
std::shared_ptr<CInfractionBase> pInfraction;
switch (infType)
{
case CInfractionBase::Mute:
infraction = new CMuteInfraction(iDuration, zpTarget->GetSteamId64());
pInfraction = std::make_shared<CMuteInfraction>(iDuration, zpTarget->GetSteamId64());
break;
case CInfractionBase::Gag:
infraction = new CGagInfraction(iDuration, zpTarget->GetSteamId64());
pInfraction = std::make_shared<CGagInfraction>(iDuration, zpTarget->GetSteamId64());
break;
case CInfractionBase::Ban:
infraction = new CBanInfraction(iDuration, zpTarget->GetSteamId64());
pInfraction = std::make_shared<CBanInfraction>(iDuration, zpTarget->GetSteamId64());
break;
case CInfractionBase::Eban:
infraction = new CEbanInfraction(iDuration, zpTarget->GetSteamId64());
pInfraction = std::make_shared<CEbanInfraction>(iDuration, zpTarget->GetSteamId64());
break;
default:
// This should never be reached, since we it means we are trying to apply an unimplemented block type
// This should never be reached, since it means we are trying to apply an unimplemented block type
ClientPrint(pAdmin, HUD_PRINTTALK, CHAT_PREFIX "Improper block type... Send to a dev with the command used.");
return;
}

// We're overwriting the infraction, so remove the previous one first
g_pAdminSystem->FindAndRemoveInfraction(zpTarget, infType);
g_pAdminSystem->AddInfraction(infraction);
infraction->ApplyInfraction(zpTarget);
g_pAdminSystem->AddInfraction(pInfraction);
pInfraction->ApplyInfraction(zpTarget);
}

if (iNumClients == 1 || (bAdding && iDuration == 0))
Expand Down
5 changes: 2 additions & 3 deletions src/adminsystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#pragma once
#include "platform.h"
#include "playermanager.h"
#include "utlvector.h"
#include <ctime>

// clang-format off
Expand Down Expand Up @@ -179,7 +178,7 @@ class CAdminSystem
bool LoadAdmins();
void AddOrUpdateAdmin(uint64 iSteamID, uint64 iFlags = 0, int iAdminImmunity = 0);
bool LoadInfractions();
void AddInfraction(CInfractionBase*);
void AddInfraction(std::shared_ptr<CInfractionBase> pInfraction);
void SaveInfractions();
bool ApplyInfractions(ZEPlayer* player);
bool FindAndRemoveInfraction(ZEPlayer* player, CInfractionBase::EInfractionType type);
Expand All @@ -196,7 +195,7 @@ class CAdminSystem
private:
std::map<std::string, CAdminBase> m_mapAdminGroups;
std::map<uint64, CAdmin> m_mapAdmins;
CUtlVector<CInfractionBase*> m_vecInfractions;
std::vector<std::shared_ptr<CInfractionBase>> m_vecInfractions;

// Implemented as a circular buffer.
std::tuple<std::string, uint64, std::string> m_rgDCPly[20];
Expand Down
36 changes: 17 additions & 19 deletions src/discord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,43 +38,41 @@ void DiscordHttpCallback(HTTPRequestHandle request, json response)
Message("Discord post received response: %s\n", response.dump().c_str());
}

void CDiscordBotManager::PostDiscordMessage(const char* sDiscordBotName, const char* sMessage)
void CDiscordBotManager::PostDiscordMessage(const char* pszDiscordBotName, const char* pszMessage)
{
FOR_EACH_VEC(m_vecDiscordBots, i)
for (auto pBot : m_vecDiscordBots)
{
CDiscordBot bot = m_vecDiscordBots[i];

if (g_cvarDebugDiscordRequests.Get())
Message("The bot at %i is %s with %s webhook and %s avatar.\n", i, bot.GetName(), bot.GetWebhookUrl(), bot.GetAvatarUrl());
Message("The bot is %s with %s webhook and %s avatar.\n", pBot->GetName(), pBot->GetWebhookUrl(), pBot->GetAvatarUrl());

if (!V_stricmp(sDiscordBotName, bot.GetName()))
bot.PostMessage(sMessage);
if (!V_stricmp(pszDiscordBotName, pBot->GetName()))
pBot->PostMessage(pszMessage);
}
}

void CDiscordBot::PostMessage(const char* sMessage)
void CDiscordBot::PostMessage(std::string strMessage)
{
json jRequestBody;

// Fill up the Json fields
jRequestBody["content"] = sMessage;
jRequestBody["content"] = strMessage;

if (m_bOverrideName)
jRequestBody["username"] = m_pszName;
jRequestBody["username"] = m_strName;

if (V_strcmp(m_pszAvatarUrl, ""))
jRequestBody["avatar_url"] = m_pszAvatarUrl;
if (V_strcmp(GetAvatarUrl(), ""))
jRequestBody["avatar_url"] = m_strAvatarUrl;

// Send the request
std::string sRequestBody = jRequestBody.dump();
if (g_cvarDebugDiscordRequests.Get())
Message("Sending '%s' to %s.\n", sRequestBody.c_str(), GetWebhookUrl());
g_HTTPManager.Post(m_pszWebhookUrl, sRequestBody.c_str(), &DiscordHttpCallback);
g_HTTPManager.Post(m_strWebhookUrl, sRequestBody, &DiscordHttpCallback);
}

bool CDiscordBotManager::LoadDiscordBotsConfig()
{
m_vecDiscordBots.Purge();
m_vecDiscordBots.clear();
KeyValues* pKV = new KeyValues("discordbots");
KeyValues::AutoDelete autoDelete(pKV);

Expand All @@ -101,11 +99,11 @@ bool CDiscordBotManager::LoadDiscordBotsConfig()
pszAvatarUrl = "";

// We just append the bots as-is
CDiscordBot bot = CDiscordBot(pszName, pszWebhookUrl, pszAvatarUrl, bOverrideName);
ConMsg("Loaded DiscordBot config %s\n", bot.GetName());
ConMsg(" - Webhook URL: %s\n", bot.GetWebhookUrl());
ConMsg(" - Avatar URL: %s\n", bot.GetAvatarUrl());
m_vecDiscordBots.AddToTail(bot);
std::shared_ptr<CDiscordBot> pBot = std::make_shared<CDiscordBot>(pszName, pszWebhookUrl, pszAvatarUrl, bOverrideName);
ConMsg("Loaded DiscordBot config %s\n", pBot->GetName());
ConMsg(" - Webhook URL: %s\n", pBot->GetWebhookUrl());
ConMsg(" - Avatar URL: %s\n", pBot->GetAvatarUrl());
m_vecDiscordBots.push_back(pBot);
}

return true;
Expand Down
27 changes: 13 additions & 14 deletions src/discord.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,27 @@
*/

#include "httpmanager.h"
#include "utlvector.h"

class CDiscordBot
{
public:
CDiscordBot(const char* pszName, const char* pszWebhookUrl, const char* pszAvatarUrl, bool bOverrideName)
CDiscordBot(std::string strName, std::string strWebhookUrl, std::string strAvatarUrl, bool bOverrideName)
{
V_strcpy(m_pszName, pszName);
V_strcpy(m_pszWebhookUrl, pszWebhookUrl);
V_strcpy(m_pszAvatarUrl, pszAvatarUrl);
m_strName = strName;
m_strWebhookUrl = strWebhookUrl;
m_strAvatarUrl = strAvatarUrl;
m_bOverrideName = bOverrideName;
}

const char* GetName() { return (const char*)m_pszName; };
const char* GetWebhookUrl() { return m_pszWebhookUrl; };
const char* GetAvatarUrl() { return m_pszAvatarUrl; };
void PostMessage(const char* sMessage);
const char* GetName() { return m_strName.c_str(); };
const char* GetWebhookUrl() { return m_strWebhookUrl.c_str(); };
const char* GetAvatarUrl() { return m_strAvatarUrl.c_str(); };
void PostMessage(std::string strMessage);

private:
char m_pszName[32];
char m_pszWebhookUrl[256];
char m_pszAvatarUrl[256];
std::string m_strName;
std::string m_strWebhookUrl;
std::string m_strAvatarUrl;
bool m_bOverrideName;
};

Expand All @@ -51,11 +50,11 @@ class CDiscordBotManager
LoadDiscordBotsConfig();
}

void PostDiscordMessage(const char* sDiscordBotName, const char* sMessage);
void PostDiscordMessage(const char* pszDiscordBotName, const char* pszMessage);
bool LoadDiscordBotsConfig();

private:
CUtlVector<CDiscordBot> m_vecDiscordBots;
std::vector<std::shared_ptr<CDiscordBot>> m_vecDiscordBots;
};

extern CDiscordBotManager* g_pDiscordBotManager;
41 changes: 18 additions & 23 deletions src/httpmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,37 +107,37 @@ void HTTPManager::TrackedRequest::OnHTTPRequestCompleted(HTTPRequestCompleted_t*
delete this;
}

void HTTPManager::Get(const char* pszUrl, CompletedCallback callbackCompleted,
void HTTPManager::Get(std::string strUrl, CompletedCallback callbackCompleted,
ErrorCallback callbackError, std::vector<HTTPHeader>* headers)
{
GenerateRequest(k_EHTTPMethodGET, pszUrl, "", callbackCompleted, callbackError, headers);
GenerateRequest(k_EHTTPMethodGET, strUrl, "", callbackCompleted, callbackError, headers);
}

void HTTPManager::Post(const char* pszUrl, const char* pszText, CompletedCallback callbackCompleted,
void HTTPManager::Post(std::string strUrl, std::string strText, CompletedCallback callbackCompleted,
ErrorCallback callbackError, std::vector<HTTPHeader>* headers)
{
GenerateRequest(k_EHTTPMethodPOST, pszUrl, pszText, callbackCompleted, callbackError, headers);
GenerateRequest(k_EHTTPMethodPOST, strUrl, strText, callbackCompleted, callbackError, headers);
}

void HTTPManager::Put(const char* pszUrl, const char* pszText, CompletedCallback callbackCompleted,
void HTTPManager::Put(std::string strUrl, std::string strText, CompletedCallback callbackCompleted,
ErrorCallback callbackError, std::vector<HTTPHeader>* headers)
{
GenerateRequest(k_EHTTPMethodPUT, pszUrl, pszText, callbackCompleted, callbackError, headers);
GenerateRequest(k_EHTTPMethodPUT, strUrl, strText, callbackCompleted, callbackError, headers);
}

void HTTPManager::Patch(const char* pszUrl, const char* pszText, CompletedCallback callbackCompleted,
void HTTPManager::Patch(std::string strUrl, std::string strText, CompletedCallback callbackCompleted,
ErrorCallback callbackError, std::vector<HTTPHeader>* headers)
{
GenerateRequest(k_EHTTPMethodPATCH, pszUrl, pszText, callbackCompleted, callbackError, headers);
GenerateRequest(k_EHTTPMethodPATCH, strUrl, strText, callbackCompleted, callbackError, headers);
}

void HTTPManager::Delete(const char* pszUrl, const char* pszText, CompletedCallback callbackCompleted,
void HTTPManager::Delete(std::string strUrl, std::string strText, CompletedCallback callbackCompleted,
ErrorCallback callbackError, std::vector<HTTPHeader>* headers)
{
GenerateRequest(k_EHTTPMethodDELETE, pszUrl, pszText, callbackCompleted, callbackError, headers);
GenerateRequest(k_EHTTPMethodDELETE, strUrl, strText, callbackCompleted, callbackError, headers);
}

void HTTPManager::GenerateRequest(EHTTPMethod method, const char* pszUrl, const char* pszText,
void HTTPManager::GenerateRequest(EHTTPMethod method, std::string strUrl, std::string strText,
CompletedCallback callbackCompleted, ErrorCallback callbackError,
std::vector<HTTPHeader>* headers)
{
Expand All @@ -147,24 +147,19 @@ void HTTPManager::GenerateRequest(EHTTPMethod method, const char* pszUrl, const
return;
}

// Message("Sending HTTP:\n%s\n", pszText);
auto hReq = GetSteamHTTP()->CreateHTTPRequest(method, pszUrl);
int size = strlen(pszText);
// Message("HTTP request: %p\n", hReq);
#ifdef _DEBUG
Message("Sending HTTP:\n%s\n", strText.c_str());
#endif

HTTPRequestHandle hReq = GetSteamHTTP()->CreateHTTPRequest(method, strUrl.c_str());

bool shouldHaveBody = method == k_EHTTPMethodPOST
|| method == k_EHTTPMethodPATCH
|| method == k_EHTTPMethodPUT
|| method == k_EHTTPMethodDELETE;

if (shouldHaveBody && !GetSteamHTTP()->SetHTTPRequestRawPostBody(hReq, "application/json", (uint8*)pszText, size))
{
// Message("Failed to SetHTTPRequestRawPostBody\n");
if (shouldHaveBody && !GetSteamHTTP()->SetHTTPRequestRawPostBody(hReq, "application/json", (uint8*)(strText.c_str()), strText.length()))
return;
}

// Prevent HTTP error 411 (probably not necessary?)
// g_http->SetHTTPRequestHeaderValue(hReq, "Content-Length", std::to_string(size).c_str());

if (headers != nullptr)
for (HTTPHeader header : *headers)
Expand All @@ -173,5 +168,5 @@ void HTTPManager::GenerateRequest(EHTTPMethod method, const char* pszUrl, const
SteamAPICall_t hCall;
GetSteamHTTP()->SendHTTPRequest(hReq, &hCall);

new TrackedRequest(hReq, hCall, pszUrl, pszText, callbackCompleted, callbackError);
new TrackedRequest(hReq, hCall, strUrl, strText, callbackCompleted, callbackError);
}
Loading