diff options
| author | nwrl <n/a> | 2025-08-15 15:51:14 -0500 |
|---|---|---|
| committer | nwrl <n/a> | 2025-08-15 15:51:14 -0500 |
| commit | c68a770456b519a802d687a770a90328588d060d (patch) | |
| tree | 8f25a4e771c67567f47491481c797eba5e7e48cc /nameblocker.sp | |
| parent | 2462b2aa1013c42f215f311798a7a77e54c3eefd (diff) | |
Replace sourcemod logging functions with helper doSimpleLog function
Diffstat (limited to 'nameblocker.sp')
| -rw-r--r-- | nameblocker.sp | 100 |
1 files changed, 63 insertions, 37 deletions
diff --git a/nameblocker.sp b/nameblocker.sp index f125638..982b3e4 100644 --- a/nameblocker.sp +++ b/nameblocker.sp | |||
| @@ -42,7 +42,7 @@ | |||
| 42 | #include <sourcemod> | 42 | #include <sourcemod> |
| 43 | #include <regex> | 43 | #include <regex> |
| 44 | 44 | ||
| 45 | #define DEBUG_304142124110815 1 | 45 | #define _SMNB__DEBUG_304142124110815__VERBOSE 1 |
| 46 | 46 | ||
| 47 | public Plugin myinfo = { | 47 | public Plugin myinfo = { |
| 48 | name = "SM Name Blocker", | 48 | name = "SM Name Blocker", |
| @@ -95,13 +95,40 @@ ConVar gcvarBanTime; static const char BANTIMENAME[] = "nameb | |||
| 95 | ConVar gcvarKickMsg; static const char KICKMSGNAME[] = "nameblock_KickMessage"; static const char DEFAULTKICKMSG[] = "Failed name check"; | 95 | ConVar gcvarKickMsg; static const char KICKMSGNAME[] = "nameblock_KickMessage"; static const char DEFAULTKICKMSG[] = "Failed name check"; |
| 96 | ConVar gcvarBanMsg; static const char BANMSGNAME[] = "nameblock_BanMessage"; static const char DEFAULTBANMESSAGE[] = "Failed name check"; | 96 | ConVar gcvarBanMsg; static const char BANMSGNAME[] = "nameblock_BanMessage"; static const char DEFAULTBANMESSAGE[] = "Failed name check"; |
| 97 | 97 | ||
| 98 | // Logs and throws an error with the same message of upto 2048 characters | 98 | enum LOGMODE { |
| 99 | void logAndFail(const char[] format, any ...) { | 99 | LM_MSG = (1<<0), |
| 100 | char buf[2048 + 1]; | 100 | LM_ERROR = (1<<1), |
| 101 | VFormat(buf, sizeof(buf), format, 2); | 101 | LM_ACTION = (1<<2), // Unused, may implement later |
| 102 | LM_TOFILE = (1<<3), // Unused, may implement later | ||
| 103 | }; | ||
| 104 | |||
| 105 | void doSimpleLog(LOGMODE modeflags, bool fail=false, const char[] fmt, any ...) { | ||
| 106 | if(modeflags < LM_MSG || modeflags > (LM_MSG | LM_ERROR)) { | ||
| 107 | doSimpleLog(LM_ERROR, false, "<nameblocker::doSimpleLog> Mode oob. Expected: [%d, %d], Got: %d", LM_MSG, (LM_MSG | LM_ERROR), modeflags); | ||
| 108 | return; | ||
| 109 | } | ||
| 110 | |||
| 111 | /* Got fed up enough to look at sourcemod's code as to how they deal with Format type functions and they just make a 2048 char | ||
| 112 | // long buffer instead of just using vsnprintf twice to automatically determine the size of the buffer, which is fucking lame | ||
| 113 | // as hell. Literally, just make a copy of the va_args via va_copy(), run vsnprintf() to get the number of | ||
| 114 | // characters to be written, then run it again with the copy to actually put it into a buffer. I'm sure there's a better way | ||
| 115 | // to reimplement asprintf, but it's easy as balls and takes 2 seconds. Instead we get this, and they don't even offer the | ||
| 116 | // same functionality where VFormat determines the number of bytes to be written, so I have to settle for some bullshit */ | ||
| 117 | |||
| 118 | // This language sucks fuck dude | ||
| 119 | |||
| 120 | char buf[2048]; | ||
| 121 | VFormat(buf, sizeof(buf), fmt, 3); | ||
| 102 | 122 | ||
| 103 | LogError(buf); | 123 | if(modeflags & LM_MSG) {LogMessage(buf);} |
| 104 | SetFailState(buf); | 124 | if(modeflags & LM_ERROR) {LogError(buf);} |
| 125 | |||
| 126 | #if defined _SMNB__DEBUG_304142124110815__VERBOSE | ||
| 127 | PrintToServer(buf); | ||
| 128 | #endif | ||
| 129 | |||
| 130 | if(fail) SetFailState(buf); | ||
| 131 | return; | ||
| 105 | } | 132 | } |
| 106 | 133 | ||
| 107 | // Concatenate arguments into a single buffer | 134 | // Concatenate arguments into a single buffer |
| @@ -117,8 +144,8 @@ int concatArgs(char[] buf, int maxbuflen, int maxarglen, int end, int start=1) { | |||
| 117 | Format(tmp, maxbuflen, "%s %s", tmp, arg); | 144 | Format(tmp, maxbuflen, "%s %s", tmp, arg); |
| 118 | } | 145 | } |
| 119 | 146 | ||
| 120 | #if defined DEBUG_304142124110815 | 147 | #if defined _SMNB__DEBUG_304142124110815__VERBOSE |
| 121 | PrintToServer("<SM Name Blocker::concatArgs> Preformed concat: %s", tmp); | 148 | doSimpleLog(LM_MSG, false, "<concatArgs:VERBOSE> Preformed concat: %s", tmp); |
| 122 | #endif | 149 | #endif |
| 123 | return strcopy(buf, maxbuflen, tmp); | 150 | return strcopy(buf, maxbuflen, tmp); |
| 124 | } | 151 | } |
| @@ -135,9 +162,9 @@ int registerIntConVar(ConVar& cv, int defaultVal, const char[] name, const char[ | |||
| 135 | return 0; | 162 | return 0; |
| 136 | } | 163 | } |
| 137 | 164 | ||
| 138 | // `registerIntConVar` wrapper. Calls `logAndFail` if the convar couldn't be registered | 165 | // `registerIntConVar` wrapper. Calls `doSimpleLog` if the convar couldn't be registered |
| 139 | void xRegisterIntConVar(ConVar& cv, int defaultVal, const char[] name, const char[] desc) { | 166 | void xRegisterIntConVar(ConVar& cv, int defaultVal, const char[] name, const char[] desc) { |
| 140 | if(registerIntConVar(cv, defaultVal, name, desc)) logAndFail("Could not register cvar \"%s\"", name); | 167 | if(registerIntConVar(cv, defaultVal, name, desc)) doSimpleLog(LM_ERROR, true, "<nameblocker::xRegisterIntConVar> Error: could not register cvar \"%s\"", name); |
| 141 | } | 168 | } |
| 142 | 169 | ||
| 143 | 170 | ||
| @@ -148,7 +175,7 @@ void initDatabase() { | |||
| 148 | bool err = false; | 175 | bool err = false; |
| 149 | 176 | ||
| 150 | db = SQLite_UseDatabase("sourcemod-local", sqlerr, sizeof(sqlerr)); | 177 | db = SQLite_UseDatabase("sourcemod-local", sqlerr, sizeof(sqlerr)); |
| 151 | if(db == null) logAndFail("Could not connect to sql database: %s", sqlerr); | 178 | if(db == null) doSimpleLog(LM_ERROR, true, "<nameblocker::initDatabase> Error: Could not connect to sql database: %s", sqlerr); |
| 152 | 179 | ||
| 153 | // Prep table | 180 | // Prep table |
| 154 | SQL_LockDatabase(db); | 181 | SQL_LockDatabase(db); |
| @@ -158,8 +185,7 @@ void initDatabase() { | |||
| 158 | } | 185 | } |
| 159 | SQL_UnlockDatabase(db); | 186 | SQL_UnlockDatabase(db); |
| 160 | 187 | ||
| 161 | if(err) logAndFail("Could not create table: %s", sqlerr); | 188 | if(err) doSimpleLog(LM_ERROR, true, "<nameblocker::initDatabase> Error: Could not create table: %s", sqlerr); |
| 162 | |||
| 163 | return; | 189 | return; |
| 164 | } | 190 | } |
| 165 | 191 | ||
| @@ -182,10 +208,10 @@ void initPrepStatements() { | |||
| 182 | 208 | ||
| 183 | SQL_UnlockDatabase(db); | 209 | SQL_UnlockDatabase(db); |
| 184 | switch(err) { | 210 | switch(err) { |
| 185 | case 1: {logAndFail("Could not prepare insert statement: %s", sqlerr);} // Doesn't recognize "163687013_SMNameBlocker" as a valid token | 211 | case 1: {doSimpleLog(LM_ERROR, true, "<nameblocker::initPrepStatements> Error: Could not prepare insert statement: %s", sqlerr);} // Doesn't recognize "163687013_SMNameBlocker" as a valid token |
| 186 | case 2: {logAndFail("Could not prepare delete statement: %s", sqlerr);} | 212 | case 2: {doSimpleLog(LM_ERROR, true, "<nameblocker::initPrepStatements> Error: Could not prepare delete statement: %s", sqlerr);} |
| 187 | case 3: {logAndFail("Could not prepare replace statement: %s", sqlerr);} | 213 | case 3: {doSimpleLog(LM_ERROR, true, "<nameblocker::initPrepStatements> Error: Could not prepare replace statement: %s", sqlerr);} |
| 188 | case 4: {logAndFail("Could not prepare populate statement: %s", sqlerr);} | 214 | case 4: {doSimpleLog(LM_ERROR, true, "<nameblocker::initPrepStatements> Error: Could not prepare populate statement: %s", sqlerr);} |
| 189 | } | 215 | } |
| 190 | 216 | ||
| 191 | return; | 217 | return; |
| @@ -194,10 +220,10 @@ void initPrepStatements() { | |||
| 194 | void loadFromDatabase() { | 220 | void loadFromDatabase() { |
| 195 | // Initialize and populate datatypes | 221 | // Initialize and populate datatypes |
| 196 | regexlist = new ArrayList(ByteCountToCells(HANDLE_SIZE)); | 222 | regexlist = new ArrayList(ByteCountToCells(HANDLE_SIZE)); |
| 197 | if(regexlist == null) logAndFail("Could not initialize regexlist ArrayList"); | 223 | if(regexlist == null) doSimpleLog(LM_ERROR, true, "<nameblocker::loadFromDatabase> Error: Could not initialize regexlist ArrayList"); |
| 198 | 224 | ||
| 199 | patternlist = new ArrayList(ByteCountToCells(PATTERN_MAX_LEN)); | 225 | patternlist = new ArrayList(ByteCountToCells(PATTERN_MAX_LEN)); |
| 200 | if(patternlist == null) logAndFail("Could not initialize patternlist ArrayList"); | 226 | if(patternlist == null) doSimpleLog(LM_ERROR, true, "<nameblocker::loadFromDatabase> Error: Could not initialize patternlist ArrayList"); |
| 201 | 227 | ||
| 202 | // Initialize table if it doesn't exist | 228 | // Initialize table if it doesn't exist |
| 203 | int err = 0; | 229 | int err = 0; |
| @@ -213,8 +239,8 @@ void loadFromDatabase() { | |||
| 213 | 239 | ||
| 214 | SQL_UnlockDatabase(db); | 240 | SQL_UnlockDatabase(db); |
| 215 | switch(err) { | 241 | switch(err) { |
| 216 | case 1: {logAndFail("Could not initialize nameblocker table: %s", sqlerr);} | 242 | case 1: {doSimpleLog(LM_ERROR, true, "<nameblocker::loadFromDatabase> Error: Could not initialize nameblocker table: %s", sqlerr);} |
| 217 | case 2: {logAndFail("Population query failed: %s", sqlerr);} | 243 | case 2: {doSimpleLog(LM_ERROR, true, "<nameblocker::loadFromDatabase> Error: Population query failed: %s", sqlerr);} |
| 218 | } | 244 | } |
| 219 | 245 | ||
| 220 | 246 | ||
| @@ -224,12 +250,12 @@ void loadFromDatabase() { | |||
| 224 | SQL_FetchString(dbPopulate, 1, pattern, sizeof(pattern)); | 250 | SQL_FetchString(dbPopulate, 1, pattern, sizeof(pattern)); |
| 225 | cur = CompileRegex(pattern, gcvarRegexCompFlags, reerr, sizeof(reerr), reenum); | 251 | cur = CompileRegex(pattern, gcvarRegexCompFlags, reerr, sizeof(reerr), reenum); |
| 226 | if(cur == null) { | 252 | if(cur == null) { |
| 227 | LogError("Could not compile regex pattern \"%s\": %s (%d)", pattern, reerr, reenum); | 253 | doSimpleLog(LM_ERROR, false, "<nameblocker::loadFromDatabase> Error: Could not compile regex pattern \"%s\": %s (%d)", pattern, reerr, reenum); |
| 228 | continue; | 254 | continue; |
| 229 | } | 255 | } |
| 230 | 256 | ||
| 231 | if(aModPattern(MM_INSERT, regexlist.Length, pattern)) { | 257 | if(aModPattern(MM_INSERT, regexlist.Length, pattern)) { |
| 232 | LogError("Couldn't add regex \"%s\" to arraylists, continuing", pattern); | 258 | doSimpleLog(LM_ERROR, false, "<nameblocker::loadFromDatabase> Error: Couldn't add regex \"%s\" to arraylists, continuing", pattern); |
| 233 | CloseHandle(cur); | 259 | CloseHandle(cur); |
| 234 | } | 260 | } |
| 235 | } | 261 | } |
| @@ -252,10 +278,10 @@ public void OnAllPluginsLoaded() { | |||
| 252 | xRegisterIntConVar(gcvarBanTime, DEFAULTBANTIME, BANTIMENAME, "Time (in minutes) to ban players when operating in ban mode. 0 for permaban"); | 278 | xRegisterIntConVar(gcvarBanTime, DEFAULTBANTIME, BANTIMENAME, "Time (in minutes) to ban players when operating in ban mode. 0 for permaban"); |
| 253 | 279 | ||
| 254 | gcvarKickMsg = CreateConVar(KICKMSGNAME, DEFAULTKICKMSG, "Default kick message"); | 280 | gcvarKickMsg = CreateConVar(KICKMSGNAME, DEFAULTKICKMSG, "Default kick message"); |
| 255 | if(gcvarKickMsg == null) logAndFail("Could not init convar \"gcvarKickMsg\""); | 281 | if(gcvarKickMsg == null) doSimpleLog(LM_ERROR, true, "<nameblocker::OnAllPluginsLoaded> Error: Could not init convar \"gcvarKickMsg\""); |
| 256 | 282 | ||
| 257 | gcvarBanMsg = CreateConVar(BANMSGNAME, DEFAULTBANMESSAGE, "Default ban message"); | 283 | gcvarBanMsg = CreateConVar(BANMSGNAME, DEFAULTBANMESSAGE, "Default ban message"); |
| 258 | if(gcvarBanMsg == null) logAndFail("Could not init convar \"gcvarBanMsg\""); | 284 | if(gcvarBanMsg == null) doSimpleLog(LM_ERROR, true, "<nameblocker::OnAllPluginsLoaded> Error: Could not init convar \"gcvarBanMsg\""); |
| 259 | 285 | ||
| 260 | AutoExecConfig(true, "nameblocker_cvars"); | 286 | AutoExecConfig(true, "nameblocker_cvars"); |
| 261 | 287 | ||
| @@ -370,7 +396,7 @@ int __modPattern__insert(int index, char[] pattern, int patternlen, int client) | |||
| 370 | bool flag = false; | 396 | bool flag = false; |
| 371 | 397 | ||
| 372 | if(!GetClientAuthId(client, AuthId_SteamID64, steamid64, sizeof(steamid64))) { | 398 | if(!GetClientAuthId(client, AuthId_SteamID64, steamid64, sizeof(steamid64))) { |
| 373 | LogError("Could not get client \"%N\"'s steamid64", client); | 399 | doSimpleLog(LM_ERROR, false, "<nameblocker::__modPattern__insert> Error: Could not get client \"%N\"'s steamid64", client); |
| 374 | return -1; | 400 | return -1; |
| 375 | } | 401 | } |
| 376 | 402 | ||
| @@ -381,7 +407,7 @@ int __modPattern__insert(int index, char[] pattern, int patternlen, int client) | |||
| 381 | SQL_BindParamString(dbInsert, 1, steamid64, false); | 407 | SQL_BindParamString(dbInsert, 1, steamid64, false); |
| 382 | if(!SQL_Execute(dbInsert)) { | 408 | if(!SQL_Execute(dbInsert)) { |
| 383 | SQL_GetError(dbInsert, sqlerr, sizeof(sqlerr)); | 409 | SQL_GetError(dbInsert, sqlerr, sizeof(sqlerr)); |
| 384 | LogError("Could not insert values into database: %s", sqlerr); | 410 | doSimpleLog(LM_ERROR, false, "<nameblocker::__modPattern__insert> Error: Could not insert values into database: %s", sqlerr); |
| 385 | flag = true; | 411 | flag = true; |
| 386 | } | 412 | } |
| 387 | 413 | ||
| @@ -406,7 +432,7 @@ int __modPattern__delete(int index) { | |||
| 406 | SQL_BindParamString(dbDelete, 0, pattern, false); | 432 | SQL_BindParamString(dbDelete, 0, pattern, false); |
| 407 | if(!SQL_Execute(dbDelete)) { | 433 | if(!SQL_Execute(dbDelete)) { |
| 408 | SQL_GetError(dbDelete, sqlerr, sizeof(sqlerr)); | 434 | SQL_GetError(dbDelete, sqlerr, sizeof(sqlerr)); |
| 409 | LogError("Could not delete from database: %s", sqlerr); | 435 | doSimpleLog(LM_ERROR, false, "<nameblocker::__modPattern__delete> Error: Could not delete from database: %s", sqlerr); |
| 410 | flag = true; | 436 | flag = true; |
| 411 | } | 437 | } |
| 412 | 438 | ||
| @@ -429,7 +455,7 @@ int __modPattern__replace(int index, char[] pattern, int patternlen, int client) | |||
| 429 | 455 | ||
| 430 | patternlist.GetString(index, oldpattern, sizeof(oldpattern), ByteCountToCells(PATTERN_MAX_LEN)); | 456 | patternlist.GetString(index, oldpattern, sizeof(oldpattern), ByteCountToCells(PATTERN_MAX_LEN)); |
| 431 | if(!GetClientAuthId(client, AuthId_SteamID64, steamid64, sizeof(steamid64))) { | 457 | if(!GetClientAuthId(client, AuthId_SteamID64, steamid64, sizeof(steamid64))) { |
| 432 | LogError("Could not get \"%N\"'s steamid64"); | 458 | doSimpleLog(LM_ERROR, false, "<nameblocker::__modPattern__replace> Error: Could not get \"%N\"'s steamid64"); |
| 433 | return -1; | 459 | return -1; |
| 434 | } | 460 | } |
| 435 | 461 | ||
| @@ -441,7 +467,7 @@ int __modPattern__replace(int index, char[] pattern, int patternlen, int client) | |||
| 441 | 467 | ||
| 442 | if(!SQL_Execute(dbReplace)) { | 468 | if(!SQL_Execute(dbReplace)) { |
| 443 | SQL_GetError(dbReplace, sqlerr, sizeof(sqlerr)); | 469 | SQL_GetError(dbReplace, sqlerr, sizeof(sqlerr)); |
| 444 | LogError("Could not replace in database: %s", sqlerr); | 470 | doSimpleLog(LM_ERROR, false, "<nameblocker::__modPattern__replace> Error: Could not replace in database: %s", sqlerr); |
| 445 | flag = true; | 471 | flag = true; |
| 446 | } | 472 | } |
| 447 | 473 | ||
| @@ -463,7 +489,7 @@ int modPattern(MOD_MODE mode, int index, char[] pattern="", int patternlen=-1, i | |||
| 463 | case MM_DELETE: {return __modPattern__delete(index);} | 489 | case MM_DELETE: {return __modPattern__delete(index);} |
| 464 | case MM_REPLACE: {return __modPattern__replace(index, pattern, patternlen, client);} | 490 | case MM_REPLACE: {return __modPattern__replace(index, pattern, patternlen, client);} |
| 465 | default: { | 491 | default: { |
| 466 | LogError("Given invalid DBMOD_MODE"); | 492 | doSimpleLog(LM_ERROR, false, "<nameblocker::modPattern> Error: Given invalid DBMOD_MODE"); |
| 467 | return -1; | 493 | return -1; |
| 468 | } | 494 | } |
| 469 | } | 495 | } |
| @@ -479,7 +505,7 @@ int aModPattern(MOD_MODE mode, int index, char[] pattern="", int patternlen=0) { | |||
| 479 | char errstr[512]; RegexError reerr; | 505 | char errstr[512]; RegexError reerr; |
| 480 | res = CompileRegex(pattern, gcvarRegexCompFlags.IntValue, errstr, sizeof(errstr), reerr); | 506 | res = CompileRegex(pattern, gcvarRegexCompFlags.IntValue, errstr, sizeof(errstr), reerr); |
| 481 | if(res == null) { | 507 | if(res == null) { |
| 482 | LogError("Error: Could not compile regex pattern \"%s\": %s (%d)", pattern, errstr, reerr); | 508 | doSimpleLog(LM_ERROR, false, "<nameblocker::aModPattern> Error: Could not compile regex pattern \"%s\": %s (%d)", pattern, errstr, reerr); |
| 483 | return -1; | 509 | return -1; |
| 484 | } | 510 | } |
| 485 | } | 511 | } |
| @@ -518,7 +544,7 @@ int aModPattern(MOD_MODE mode, int index, char[] pattern="", int patternlen=0) { | |||
| 518 | 544 | ||
| 519 | 545 | ||
| 520 | default: { | 546 | default: { |
| 521 | logAndFail("Got impossible state"); | 547 | doSimpleLog(LM_ERROR, true, "<nameblocker::aModPattern> Error: Got impossible state"); |
| 522 | } | 548 | } |
| 523 | } | 549 | } |
| 524 | 550 | ||
| @@ -536,7 +562,7 @@ int checkName(int client) { | |||
| 536 | 562 | ||
| 537 | char name[64 + 1]; | 563 | char name[64 + 1]; |
| 538 | if(getName(client, name, sizeof(name)) <= 0) { | 564 | if(getName(client, name, sizeof(name)) <= 0) { |
| 539 | LogError("Tried to get a client's name for a name check, but could not"); | 565 | doSimpleLog(LM_ERROR, false, "<nameblocker::checkName> Error: Tried to get a client's name for a name check, but could not"); |
| 540 | return -1; | 566 | return -1; |
| 541 | } | 567 | } |
| 542 | 568 | ||
| @@ -588,13 +614,13 @@ int handleNameHit(int client) { | |||
| 588 | return 0; | 614 | return 0; |
| 589 | } | 615 | } |
| 590 | default: { | 616 | default: { |
| 591 | LogError("%L failed a name check, but the operating mode in an invalid state", client); | 617 | doSimpleLog(LM_ERROR, false, "<nameblocker::handleNameHit> Error: %L failed a name check, but the operating mode in an invalid state", client); |
| 592 | return -1; | 618 | return -1; |
| 593 | } | 619 | } |
| 594 | } | 620 | } |
| 595 | } | 621 | } |
| 596 | 622 | ||
| 597 | int handleFailedRegex(int client) { | 623 | int handleFailedRegex(int client) { |
| 598 | LogError("Ran into regex error when trying to check user %L's name", client); | 624 | doSimpleLog(LM_ERROR, false, "<nameblocker::handleFailedRegex> Error: Ran into regex error when trying to check user %L's name", client); |
| 599 | return 0; | 625 | return 0; |
| 600 | } \ No newline at end of file | 626 | } \ No newline at end of file |
