Skip to content

Commit

Permalink
Added function log_motd <Name or #UserId> <website or HTML content> c…
Browse files Browse the repository at this point in the history
…ommand
  • Loading branch information
SmileYzn committed Aug 19, 2023
1 parent 5eafc31 commit 952dc78
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 14 deletions.
58 changes: 53 additions & 5 deletions LogApi/LogCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ CLogCommand gLogCommand;

void CLogCommand::ServerActivate()
{
g_engfuncs.pfnAddServerCommand(LOG_COMMAND_LIST[0], this->Say);
g_engfuncs.pfnAddServerCommand(LOG_COMMAND_LIST[1], this->TeamSay);
g_engfuncs.pfnAddServerCommand(LOG_COMMAND_LIST[2], this->CenterSay);
g_engfuncs.pfnAddServerCommand(LOG_COMMAND_LIST[3], this->PrivateSay);
g_engfuncs.pfnAddServerCommand("log_say", this->Say);
g_engfuncs.pfnAddServerCommand("log_tsay", this->TeamSay);
g_engfuncs.pfnAddServerCommand("log_csay", this->CenterSay);
g_engfuncs.pfnAddServerCommand("log_psay", this->PrivateSay);
g_engfuncs.pfnAddServerCommand("log_motd", this->OpenMotd);
}

void CLogCommand::Say()
Expand Down Expand Up @@ -87,7 +88,7 @@ void CLogCommand::PrivateSay()

if (Player)
{
Message.erase(0, Target.size());
Message.erase(0, Target.size() + 1);

gLogUtil.ClientPrint(Player->edict(), PRINT_CHAT, "%s", Message.c_str());
}
Expand All @@ -105,3 +106,50 @@ void CLogCommand::PrivateSay()

LOG_CONSOLE(PLID, "[%s] Usage: log_psay <name or #userid> <message>", Plugin_info.logtag);
}

void CLogCommand::OpenMotd()
{
if (g_engfuncs.pfnCmd_Argc() >= 3)
{
std::string Target = g_engfuncs.pfnCmd_Argv(1);

if (!Target.empty())
{
if (Target.length() > 1)
{
std::string Message = g_engfuncs.pfnCmd_Args();

if (!Message.empty())
{
if (Message.length() > 0)
{
auto Player = gLogUtil.FindPlayer(Target);

if (Player)
{
Message.erase(0, Target.size() + 1);

char Path[MAX_MOTD_LENGTH] = { 0 };

Q_memset(Path, 0, sizeof(Path));

Q_strncpy(Path, Message.data(), sizeof(Path));

gLogUtil.ShowMotd(Player->edict(), Path, strlen(Path));

return;
}
else
{
LOG_CONSOLE(PLID, "[%s] Client with that name or userid '%s' not found.", Plugin_info.logtag, Target.c_str());
}

return;
}
}
}
}
}

LOG_CONSOLE(PLID, "[%s] Usage: log_motd <name or #userid> <webpage or file name>", Plugin_info.logtag);
}
9 changes: 1 addition & 8 deletions LogApi/LogCommand.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
#pragma once

constexpr char* LOG_COMMAND_LIST[] =
{
"log_say",
"log_tsay",
"log_csay",
"log_psay"
};

class CLogCommand
{
public:
Expand All @@ -17,6 +9,7 @@ class CLogCommand
static void TeamSay();
static void CenterSay();
static void PrivateSay();
static void OpenMotd();

hudtextparms_t GetHudParameters(bool TeamSay)
{
Expand Down
67 changes: 66 additions & 1 deletion LogApi/LogUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ CBasePlayer* CLogUtil::FindPlayer(std::string Target)
{
std::string Find = "";

std::transform(Target.begin(), Target.end(), Target.begin(), [](unsigned char character)
{
return std::tolower(character);
});

for (auto i = 1; i <= gpGlobals->maxClients; i++)
{
auto Player = UTIL_PlayerByIndexSafe(i);
Expand Down Expand Up @@ -254,4 +259,64 @@ void CLogUtil::HudMessage(edict_t* pEntity, hudtextparms_t textparms, const char

g_engfuncs.pfnWriteString(Buffer);
g_engfuncs.pfnMessageEnd();
}
}

void CLogUtil::ShowMotd(edict_t* pEntity, char* Motd, int MotdLength)
{
static int iMsgMOTD;

if (iMsgMOTD || (iMsgMOTD = gpMetaUtilFuncs->pfnGetUserMsgID(PLID, "MOTD", NULL)))
{
if (MotdLength < 128)
{
struct stat FileBuffer;

if (stat(Motd, &FileBuffer) == 0)
{
int FileLength = 0;

char* FileContent = reinterpret_cast<char*>(g_engfuncs.pfnLoadFileForMe(Motd, &FileLength));

if (FileLength)
{
this->ShowMotd(pEntity, FileContent, FileLength);

g_engfuncs.pfnFreeFile(FileContent);

return;
}
}
}

char* Buffer = Motd;

char Character = 0;

int Size = 0;

while (*Buffer)
{
Size = MotdLength;

if (Size > 175)
{
Size = 175;
}

MotdLength -= Size;

Character = *(Buffer += Size);

*Buffer = 0;

g_engfuncs.pfnMessageBegin(MSG_ONE, iMsgMOTD, NULL, pEntity);
g_engfuncs.pfnWriteByte(Character ? FALSE : TRUE);
g_engfuncs.pfnWriteString(Motd);
g_engfuncs.pfnMessageEnd();

*Buffer = Character;

Motd = Buffer;
}
}
}
1 change: 1 addition & 0 deletions LogApi/LogUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class CLogUtil
short FixedSigned16(float value, float scale);
hudtextparms_t HudParam(int red, int green, int blue, float x, float y, int effects, float fxtime, float holdtime, float fadeintime, float fadeouttime, int channel);
void HudMessage(edict_t* pEntity, hudtextparms_t textparms, const char* Format, ...);
void ShowMotd(edict_t* pEntity, char* Motd, int MotdLength);

private:
std::map<std::string, cvar_t> m_Cvar;
Expand Down

0 comments on commit 952dc78

Please sign in to comment.