diff options
| author | nwrl <n/a> | 2025-08-25 15:30:24 -0500 |
|---|---|---|
| committer | nwrl <n/a> | 2025-08-25 15:30:24 -0500 |
| commit | 9409139548ef92b0dbd1c9921cb260fc7ea3d74f (patch) | |
| tree | 212a7aefdd126aeb8a3c3dd7d4f909cc349f8d49 /nameblocker.sp | |
| parent | c68a770456b519a802d687a770a90328588d060d (diff) | |
Diffstat (limited to 'nameblocker.sp')
| -rw-r--r-- | nameblocker.sp | 112 |
1 files changed, 86 insertions, 26 deletions
diff --git a/nameblocker.sp b/nameblocker.sp index 982b3e4..97370c2 100644 --- a/nameblocker.sp +++ b/nameblocker.sp | |||
| @@ -34,8 +34,6 @@ | |||
| 34 | 34 | ||
| 35 | // If database locking takes too long I might need to use threaded calls, which sounds like a fucking nightmare | 35 | // If database locking takes too long I might need to use threaded calls, which sounds like a fucking nightmare |
| 36 | 36 | ||
| 37 | // TODO: Overhaul logging and print everything to console. I can't debug this as of now | ||
| 38 | |||
| 39 | #pragma newdecls required | 37 | #pragma newdecls required |
| 40 | #pragma semicolon 1 | 38 | #pragma semicolon 1 |
| 41 | 39 | ||
| @@ -102,21 +100,12 @@ enum LOGMODE { | |||
| 102 | LM_TOFILE = (1<<3), // Unused, may implement later | 100 | LM_TOFILE = (1<<3), // Unused, may implement later |
| 103 | }; | 101 | }; |
| 104 | 102 | ||
| 105 | void doSimpleLog(LOGMODE modeflags, bool fail=false, const char[] fmt, any ...) { | 103 | void doSimpleLog(LOGMODE modeflags = LM_MSG, bool fail=false, const char[] fmt, any ...) { |
| 106 | if(modeflags < LM_MSG || modeflags > (LM_MSG | LM_ERROR)) { | 104 | 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); | 105 | LogError("<nameblocker::doSimpleLog> Mode oob. Expected: [%d, %d], Got: %d", LM_MSG, (LM_MSG | LM_ERROR), modeflags); |
| 108 | return; | 106 | return; |
| 109 | } | 107 | } |
| 110 | 108 | ||
| 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]; | 109 | char buf[2048]; |
| 121 | VFormat(buf, sizeof(buf), fmt, 3); | 110 | VFormat(buf, sizeof(buf), fmt, 3); |
| 122 | 111 | ||
| @@ -133,7 +122,10 @@ void doSimpleLog(LOGMODE modeflags, bool fail=false, const char[] fmt, any ...) | |||
| 133 | 122 | ||
| 134 | // Concatenate arguments into a single buffer | 123 | // Concatenate arguments into a single buffer |
| 135 | int concatArgs(char[] buf, int maxbuflen, int maxarglen, int end, int start=1) { | 124 | int concatArgs(char[] buf, int maxbuflen, int maxarglen, int end, int start=1) { |
| 136 | if(maxbuflen <= 0 || start < 0) return -1; | 125 | if(maxbuflen <= 0 || start < 0) { |
| 126 | doSimpleLog(LM_ERROR, false, "<nameblocker::concatArgs> Error: maxbuflen less than 1 or start less than 0"); | ||
| 127 | return -1; | ||
| 128 | } | ||
| 137 | 129 | ||
| 138 | char[] arg = new char[maxarglen]; | 130 | char[] arg = new char[maxarglen]; |
| 139 | char[] tmp = new char[maxbuflen]; | 131 | char[] tmp = new char[maxbuflen]; |
| @@ -147,6 +139,7 @@ int concatArgs(char[] buf, int maxbuflen, int maxarglen, int end, int start=1) { | |||
| 147 | #if defined _SMNB__DEBUG_304142124110815__VERBOSE | 139 | #if defined _SMNB__DEBUG_304142124110815__VERBOSE |
| 148 | doSimpleLog(LM_MSG, false, "<concatArgs:VERBOSE> Preformed concat: %s", tmp); | 140 | doSimpleLog(LM_MSG, false, "<concatArgs:VERBOSE> Preformed concat: %s", tmp); |
| 149 | #endif | 141 | #endif |
| 142 | |||
| 150 | return strcopy(buf, maxbuflen, tmp); | 143 | return strcopy(buf, maxbuflen, tmp); |
| 151 | } | 144 | } |
| 152 | 145 | ||
| @@ -156,7 +149,10 @@ int registerIntConVar(ConVar& cv, int defaultVal, const char[] name, const char[ | |||
| 156 | Format(tmp, sizeof(tmp), "%d", defaultVal); | 149 | Format(tmp, sizeof(tmp), "%d", defaultVal); |
| 157 | 150 | ||
| 158 | cv = CreateConVar(name, tmp, desc); | 151 | cv = CreateConVar(name, tmp, desc); |
| 159 | if(cv == null) return -1; | 152 | if(cv == null) { |
| 153 | doSimpleLog(LM_MSG, false, "<nameblocker::registerIntConVar> Could not create convar \"%s\"", name); | ||
| 154 | return -1; | ||
| 155 | } | ||
| 160 | cv.IntValue = defaultVal; | 156 | cv.IntValue = defaultVal; |
| 161 | 157 | ||
| 162 | return 0; | 158 | return 0; |
| @@ -389,7 +385,22 @@ enum MOD_MODE { | |||
| 389 | }; | 385 | }; |
| 390 | 386 | ||
| 391 | int __modPattern__insert(int index, char[] pattern, int patternlen, int client) { | 387 | int __modPattern__insert(int index, char[] pattern, int patternlen, int client) { |
| 392 | if(IsNullString(pattern) || patternlen < 0 || patternlen > PATTERN_MAX_LEN || client < 0) {return -1;} | 388 | int err = 0; |
| 389 | if(IsNullString(pattern)) {err |= 1<<0;} | ||
| 390 | if(patternlen < 0) {err |= 1<<1;} | ||
| 391 | if(patternlen > PATTERN_MAX_LEN) {err |= 1<<2;} | ||
| 392 | if(client < 0) {err |= 1<<3;} | ||
| 393 | |||
| 394 | if(err) { | ||
| 395 | doSimpleLog(LM_ERROR, false, "<nameblocker::__modPattern__insert> Error: Invalid argument: %s%s%s%s", | ||
| 396 | (err & (1<<0)) ? "pattern is a null string " : "", | ||
| 397 | (err & (1<<1)) ? "patternlen is less than zero" : "", | ||
| 398 | (err & (1<<2)) ? "patternlen is greater than PATTERN_MAX_LEN" : "", | ||
| 399 | (err & (1<<3)) ? "client is less than zero" : "" | ||
| 400 | ); | ||
| 401 | return -1; | ||
| 402 | } | ||
| 403 | // This is coupled and gross either way, but this way I'm not duplicating the if statement | ||
| 393 | 404 | ||
| 394 | char steamid64[STEAMID64LENGTH]; | 405 | char steamid64[STEAMID64LENGTH]; |
| 395 | char sqlerr[512 + 1]; | 406 | char sqlerr[512 + 1]; |
| @@ -416,7 +427,10 @@ int __modPattern__insert(int index, char[] pattern, int patternlen, int client) | |||
| 416 | if(flag) | 427 | if(flag) |
| 417 | return -1; | 428 | return -1; |
| 418 | 429 | ||
| 419 | if(aModPattern(MM_INSERT, index, pattern)) {} // TODO: Error handling | 430 | if(aModPattern(MM_INSERT, index, pattern)) { |
| 431 | doSimpleLog(LM_ERROR, false, "<nameblocker::__modPattern__insert> Error: aModPattern failed"); | ||
| 432 | return -1; | ||
| 433 | } // TODO: Error handling | ||
| 420 | 434 | ||
| 421 | return 0; | 435 | return 0; |
| 422 | } | 436 | } |
| @@ -441,12 +455,30 @@ int __modPattern__delete(int index) { | |||
| 441 | if(flag) | 455 | if(flag) |
| 442 | return -1; | 456 | return -1; |
| 443 | 457 | ||
| 444 | if(aModPattern(MM_DELETE, index)) {} // TODO: Error handling | 458 | if(aModPattern(MM_DELETE, index)) { |
| 459 | doSimpleLog(LM_ERROR, false, "<nameblocker::__modPattern__delete> Error: aModPattern failed"); | ||
| 460 | return -1; | ||
| 461 | } // TODO: Error handling | ||
| 445 | 462 | ||
| 446 | return 0; | 463 | return 0; |
| 447 | } | 464 | } |
| 448 | int __modPattern__replace(int index, char[] pattern, int patternlen, int client) { | 465 | int __modPattern__replace(int index, char[] pattern, int patternlen, int client) { |
| 449 | if(IsNullString(pattern) || patternlen < 0 || patternlen > PATTERN_MAX_LEN || client < 0) {return -1;} | 466 | int err = 0; |
| 467 | |||
| 468 | if(IsNullString(pattern)) {err |= 1<<0;} | ||
| 469 | if(patternlen < 0) {err |= 1<<1;} | ||
| 470 | if(patternlen > PATTERN_MAX_LEN) {err |= 1<<2;} | ||
| 471 | if(client < 0) {err |= 1<<3;} | ||
| 472 | |||
| 473 | if(err) { | ||
| 474 | doSimpleLog(LM_ERROR, false, "<nameblocker::__modPattern__replace> Error: Invalid argument(s): %s%s%s%s", | ||
| 475 | (err & (1<<0)) ? "pattern is a null string" : "", | ||
| 476 | (err & (1<<1)) ? "patternlen is less than zero" : "", | ||
| 477 | (err & (1<<2)) ? "patternlen is greater than PATTERN_MAX_LEN" : "", | ||
| 478 | (err & (1<<3)) ? "client is less than zero" : "" | ||
| 479 | ); | ||
| 480 | return -1; | ||
| 481 | } | ||
| 450 | 482 | ||
| 451 | char oldpattern[PATTERN_MAX_LEN]; | 483 | char oldpattern[PATTERN_MAX_LEN]; |
| 452 | char steamid64[STEAMID64LENGTH]; | 484 | char steamid64[STEAMID64LENGTH]; |
| @@ -476,13 +508,19 @@ int __modPattern__replace(int index, char[] pattern, int patternlen, int client) | |||
| 476 | if(flag) | 508 | if(flag) |
| 477 | return -1; | 509 | return -1; |
| 478 | 510 | ||
| 479 | if(aModPattern(MM_REPLACE, index, pattern)) {} // TODO: Error handling | 511 | if(aModPattern(MM_REPLACE, index, pattern)) { |
| 512 | doSimpleLog(LM_ERROR, false, "<nameblocker::__modPattern__replace> Error: aModPattern failed"); | ||
| 513 | return -1; | ||
| 514 | } // TODO: Error handling | ||
| 480 | 515 | ||
| 481 | return 0; | 516 | return 0; |
| 482 | } | 517 | } |
| 483 | 518 | ||
| 484 | int modPattern(MOD_MODE mode, int index, char[] pattern="", int patternlen=-1, int client=-1) { | 519 | int modPattern(MOD_MODE mode, int index, char[] pattern="", int patternlen=-1, int client=-1) { |
| 485 | if(index < 0 || index > regexlist.Length) return -1; | 520 | if(index < 0 || index > regexlist.Length) { |
| 521 | doSimpleLog(LM_ERROR, false, "<nameblocker::modPattern> Error: Index oob. Expected: [0, %d), Got: %d", regexlist.Length, index); | ||
| 522 | return -1; | ||
| 523 | } | ||
| 486 | 524 | ||
| 487 | switch(mode) { | 525 | switch(mode) { |
| 488 | case MM_INSERT: {return __modPattern__insert(index, pattern, patternlen, client);} | 526 | case MM_INSERT: {return __modPattern__insert(index, pattern, patternlen, client);} |
| @@ -496,11 +534,23 @@ int modPattern(MOD_MODE mode, int index, char[] pattern="", int patternlen=-1, i | |||
| 496 | } | 534 | } |
| 497 | 535 | ||
| 498 | int aModPattern(MOD_MODE mode, int index, char[] pattern="", int patternlen=0) { | 536 | int aModPattern(MOD_MODE mode, int index, char[] pattern="", int patternlen=0) { |
| 499 | if(mode <= MM_UNDEF || mode >= MM_TOOBIG || index < 0 || index > patternlist.Length) return -1; | 537 | if(mode <= MM_UNDEF || mode >= MM_TOOBIG || index < 0 || index > patternlist.Length) { |
| 538 | doSimpleLog(LM_ERROR, false, "<nameblocker::aModPattern> Error: Invalid Argument(s): %s%s", | ||
| 539 | (mode <= MM_UNDEF || mode >= MM_TOOBIG) ? "Mode OOB. Expected: [MM_DELETE, MM_REPLACE]" : "", | ||
| 540 | (index < 0 || index > patternlist.Length) ? "Index OOB. Expected: [0, patternlist.Length]" : "" | ||
| 541 | ); | ||
| 542 | return -1; | ||
| 543 | } | ||
| 500 | 544 | ||
| 501 | Regex res; | 545 | Regex res; |
| 502 | if(mode > MM_DELETE) { | 546 | if(mode > MM_DELETE) { |
| 503 | if(IsNullString(pattern) || patternlen < 0 || patternlen > PATTERN_MAX_LEN) return -1; | 547 | if(IsNullString(pattern) || patternlen < 0 || patternlen > PATTERN_MAX_LEN) { |
| 548 | doSimpleLog(LM_ERROR, false, "<nameblocker::aModPattern> Error: Invalid Arguments: %s%s", | ||
| 549 | (IsNullString(pattern)) ? "pattern is null " : "", | ||
| 550 | (patternlen < 0 || patternlen > PATTERN_MAX_LEN) ? "patternlen oob" : "" | ||
| 551 | ); | ||
| 552 | return -1; | ||
| 553 | } | ||
| 504 | 554 | ||
| 505 | char errstr[512]; RegexError reerr; | 555 | char errstr[512]; RegexError reerr; |
| 506 | res = CompileRegex(pattern, gcvarRegexCompFlags.IntValue, errstr, sizeof(errstr), reerr); | 556 | res = CompileRegex(pattern, gcvarRegexCompFlags.IntValue, errstr, sizeof(errstr), reerr); |
| @@ -580,18 +630,28 @@ int checkName(int client) { | |||
| 580 | break; | 630 | break; |
| 581 | } | 631 | } |
| 582 | 632 | ||
| 633 | doSimpleLog(LM_ACTION, false, "<nameblocker::checkName> User %L \"%s\" failed name check", client, name); | ||
| 583 | return 1; | 634 | return 1; |
| 584 | } | 635 | } |
| 585 | 636 | ||
| 586 | int getName(int client, char[] buf, int buflen) { | 637 | int getName(int client, char[] buf, int buflen) { |
| 587 | if(client <= 0 || buflen <= 0) return -1; | 638 | if(client <= 0 || buflen <= 0 || IsNullString(buf)) { |
| 588 | if(IsNullString(buf)) return -1; | 639 | doSimpleLog(LM_ERROR, false, "<nameblocker::checkname> Error: Invalid argument: %s%s%s", |
| 640 | (client <= 0) ? "client index is invalid" : "", | ||
| 641 | (buflen <= 0) ? "buflen is invalid" : "", | ||
| 642 | (IsNullString(buf)) ? "given buffer is null" : "" | ||
| 643 | ); | ||
| 644 | return -1; | ||
| 645 | } | ||
| 589 | 646 | ||
| 590 | return Format(buf, buflen, "%N", client); | 647 | return Format(buf, buflen, "%N", client); |
| 591 | } | 648 | } |
| 592 | 649 | ||
| 593 | int handleNameHit(int client) { | 650 | int handleNameHit(int client) { |
| 594 | if(client <= 0) return -1; | 651 | if(client <= 0) { |
| 652 | doSimpleLog(LM_ERROR, false, "<nameblocker::handleNameHit> Error: given client index is invalid. Expected: [1, inf), Got: %d", client); | ||
| 653 | return -1; | ||
| 654 | } | ||
| 595 | 655 | ||
| 596 | switch(gcvarOperMode.IntValue) { | 656 | switch(gcvarOperMode.IntValue) { |
| 597 | case OP_DISABLED: {return 0;} | 657 | case OP_DISABLED: {return 0;} |
