From 87cce4e5e5136b5ae2fe6033d555e2a87eb557a2 Mon Sep 17 00:00:00 2001 From: nwrl Date: Thu, 14 Aug 2025 21:15:50 -0500 Subject: Complete some error handling, add kick and ban message convars --- nameblocker.sp | 130 ++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 86 insertions(+), 44 deletions(-) diff --git a/nameblocker.sp b/nameblocker.sp index af544a1..9351365 100644 --- a/nameblocker.sp +++ b/nameblocker.sp @@ -48,14 +48,16 @@ public Plugin myinfo = { url = "git.dabikers.online/smnameblocker" }; +// Size of a handle, because the actual sizeof() function/macro doesn't work on "scalar"s (which is fucking stupid because I'm asking for the literal size of the variable, not the length. If I wanted length, I'd use something like lengthof) #define HANDLE_SIZE (32) -// This exists because you can't sizeof() a Handle, but it IS specified to be a 32bit integer. This should also equal the size of -// any other methodmap or descendant of Handle (like Regex) -#define STEAMID64LENGTH 17 +// The literal character width of a steamid in 64bit representation. Not necessarily large enough to store a steamid64 in a string +#define LITERALSTEAMID64LENGTH (17) +// Length of a steamid64, suitably large enough to store in a string +#define STEAMID64LENGTH (LITERALSTEAMID64LENGTH + 1) #define PATTERN_MAX_LEN (512 + 1) /* 512 chars + null terminator */ -#define DATABASE_FAIL_MSG "Could not populate regex & pattern lists from database" +#define DATABASE_FAIL_MSG ("Could not populate regex & pattern lists from database") enum OperatingMode { OP_DISABLED, @@ -85,8 +87,9 @@ ConVar gcvarOperMode; static const char OPERMODENAME[] = "nameb ConVar gcvarAdmCmdFlag; static const char ADMCMDFLAGNAME[] = "nameblock_AdminCommandFlag"; const int DEFAULTADMCMDFLAG = ADMFLAG_BAN; ConVar gcvarRegexCompFlags; static const char REGEXCOMPFLAGSNAME[] = "nameblock_RegexCompilationFlags"; const int DEFAULTREGEXCOMPFLAGS = (PCRE_CASELESS | PCRE_DOTALL | PCRE_EXTENDED | PCRE_UTF8); ConVar gcvarAmdImmFlag; static const char ADMINIMMUNITYFLAGNAME[] = "nameblock_AdminImmunityFlag"; const int DEFAULTADMIMMFLAG = ADMFLAG_GENERIC; - - +ConVar gcvarBanTime; static const char BANTIMENAME[] = "nameblock_BanTime"; const int DEFAULTBANTIME = 10080; // 1 week in minutes +ConVar gcvarKickMsg; static const char KICKMSGNAME[] = "nameblock_KickMessage"; static const char DEFAULTKICKMSG[] = "Failed name check"; +ConVar gcvarBanMsg; static const char BANMSGNAME[] = "nameblock_BanMessage"; static const char DEFAULTBANMESSAGE[] = "Failed name check"; // Logs and throws an error with the same message of upto 2048 characters void logAndFail(const char[] format, any ...) { @@ -221,10 +224,17 @@ public void OnAllPluginsLoaded() { loadFromDatabase(); // Register convars - xRegisterIntConVar(gcvarOperMode, DEFAULTOPERMODE, OPERMODENAME, "Operating mode (disabled, kick, ban, etc.)"); - xRegisterIntConVar(gcvarAdmCmdFlag, DEFAULTADMCMDFLAG, ADMCMDFLAGNAME, "Admin flag to modify pattern list"); - xRegisterIntConVar(gcvarRegexCompFlags, DEFAULTREGEXCOMPFLAGS, REGEXCOMPFLAGSNAME, "Regular expression compilation flags"); - xRegisterIntConVar(gcvarAmdImmFlag, DEFAULTADMIMMFLAG, ADMINIMMUNITYFLAGNAME, "Admin immunity flag"); + xRegisterIntConVar(gcvarOperMode, DEFAULTOPERMODE, OPERMODENAME, "Operating mode (disabled, kick, ban, etc.)"); + xRegisterIntConVar(gcvarAdmCmdFlag, DEFAULTADMCMDFLAG, ADMCMDFLAGNAME, "Admin flag to modify pattern list"); + xRegisterIntConVar(gcvarRegexCompFlags, DEFAULTREGEXCOMPFLAGS, REGEXCOMPFLAGSNAME, "Regular expression compilation flags"); + xRegisterIntConVar(gcvarAmdImmFlag, DEFAULTADMIMMFLAG, ADMINIMMUNITYFLAGNAME, "Admin immunity flag"); + xRegisterIntConVar(gcvarBanTime, DEFAULTBANTIME, BANTIMENAME, "Time (in minutes) to ban players when operating in ban mode. 0 for permaban"); + + gcvarKickMsg = CreateConVar(KICKMSGNAME, DEFAULTKICKMSG, "Default kick message"); + if(gcvarKickMsg == null) logAndFail("Could not init convar \"gcvarKickMsg\""); + + gcvarBanMsg = CreateConVar(BANMSGNAME, DEFAULTBANMESSAGE, "Default ban message"); + if(gcvarBanMsg == null) logAndFail("Could not init convar \"gcvarBanMsg\""); AutoExecConfig(true, "nameblocker_cvars"); @@ -334,28 +344,56 @@ enum MOD_MODE { int __modPattern__insert(int index, char[] pattern, int patternlen, int client) { if(IsNullString(pattern) || patternlen < 0 || patternlen > PATTERN_MAX_LEN || client < 0) {return -1;} - char steamid64[STEAMID64LENGTH + 1]; - if(GetClientAuthId(client, AuthId_SteamID64, steamid64, sizeof(steamid64))) {} // TODO: Error handling + char steamid64[STEAMID64LENGTH]; + char sqlerr[512 + 1]; + bool flag = false; + + if(!GetClientAuthId(client, AuthId_SteamID64, steamid64, sizeof(steamid64))) { + LogError("Could not get client \"%N\"'s steamid64", client); + return -1; + } + SQL_LockDatabase(db); - SQL_BindParamString(dbInsert, 0, pattern, false); - SQL_BindParamString(dbInsert, 1, steamid64, false); - if(!SQL_Execute(dbInsert)) {} // TODO: Error handling + + SQL_BindParamString(dbInsert, 0, pattern, false); + SQL_BindParamString(dbInsert, 1, steamid64, false); + if(!SQL_Execute(dbInsert)) { + SQL_GetError(dbInsert, sqlerr, sizeof(sqlerr)); + LogError("Could not insert values into database: %s", sqlerr); + flag = true; + } + SQL_UnlockDatabase(db); + if(flag) + return -1; + if(aModPattern(MM_INSERT, index, pattern)) {} // TODO: Error handling return 0; } int __modPattern__delete(int index) { char pattern[PATTERN_MAX_LEN]; + char sqlerr[512 + 1]; + bool flag = false; + patternlist.GetString(index, pattern, sizeof(pattern), ByteCountToCells(PATTERN_MAX_LEN)); SQL_LockDatabase(db); - SQL_BindParamString(dbDelete, 0, pattern, false); - if(SQL_Execute(dbDelete)) {} // TODO: Error handling + + SQL_BindParamString(dbDelete, 0, pattern, false); + if(!SQL_Execute(dbDelete)) { + SQL_GetError(dbDelete, sqlerr, sizeof(sqlerr)); + LogError("Could not delete from database: %s", sqlerr); + flag = true; + } + SQL_UnlockDatabase(db); + if(flag) + return -1; + if(aModPattern(MM_DELETE, index)) {} // TODO: Error handling return 0; @@ -364,19 +402,33 @@ int __modPattern__replace(int index, char[] pattern, int patternlen, int client) if(IsNullString(pattern) || patternlen < 0 || patternlen > PATTERN_MAX_LEN || client < 0) {return -1;} char oldpattern[PATTERN_MAX_LEN]; - patternlist.GetString(index, oldpattern, sizeof(oldpattern), ByteCountToCells(PATTERN_MAX_LEN)); + char steamid64[STEAMID64LENGTH]; + char sqlerr[512]; + bool flag = false; - char steamid64[STEAMID64LENGTH + 1]; - if(GetClientAuthId(client, AuthId_SteamID64, steamid64, sizeof(steamid64))) {} // TODO: Error handling + patternlist.GetString(index, oldpattern, sizeof(oldpattern), ByteCountToCells(PATTERN_MAX_LEN)); + if(!GetClientAuthId(client, AuthId_SteamID64, steamid64, sizeof(steamid64))) { + LogError("Could not get \"%N\"'s steamid64"); + return -1; + } SQL_LockDatabase(db); - SQL_BindParamString(dbReplace, 0, pattern, false); - SQL_BindParamString(dbReplace, 1, steamid64, false); - SQL_BindParamString(dbReplace, 2, oldpattern, false); - if(SQL_Execute(dbReplace)) {} // TODO: Error handling + SQL_BindParamString(dbReplace, 0, pattern, false); + SQL_BindParamString(dbReplace, 1, steamid64, false); + SQL_BindParamString(dbReplace, 2, oldpattern, false); + + if(!SQL_Execute(dbReplace)) { + SQL_GetError(dbReplace, sqlerr, sizeof(sqlerr)); + LogError("Could not replace in database: %s", sqlerr); + flag = true; + } + SQL_UnlockDatabase(db); + if(flag) + return -1; + if(aModPattern(MM_REPLACE, index, pattern)) {} // TODO: Error handling return 0; @@ -452,23 +504,6 @@ int aModPattern(MOD_MODE mode, int index, char[] pattern="", int patternlen=0) { return 0; } -// int aInsertPattern(char pattern[PATTERN_MAX_LEN], Regex res, int index) { -// if(IsNullString(pattern) || res == null || index < 0 || index > regexlist.Length) return -1; - -// if(index == regexlist.Length) { -// regexlist.Push(res); -// patternlist.Push(pattern); - -// } else { -// regexlist.ShiftUp(index); -// patternlist.ShiftUp(index); -// regexlist.Set(index, res); -// patternlist.Set(index, pattern); - -// } - -// return 0; -// } @@ -514,14 +549,21 @@ int handleNameHit(int client) { switch(gcvarOperMode.IntValue) { case OP_DISABLED: {return 0;} case OP_KICK: { - KickClient(client, "Failed name check"); + char kickmsg[512]; + gcvarKickMsg.GetString(kickmsg, sizeof(kickmsg)); + + KickClient(client, kickmsg); LogAction(0, client, "Kicked %L for failing a name check", client); return 0; } case OP_BAN: { - // BanClient() + char banmsg[512]; + gcvarBanMsg.GetString(banmsg, sizeof(banmsg)); + + BanClient(client, gcvarBanTime.IntValue, BANFLAG_AUTHID, "Failed name check", banmsg); + LogAction(0, client, "Banned %L for failing a name check", client); + // TODO: Interop with other ban systems - // Log ban return 0; } default: { -- cgit v1.2.3