diff options
| author | nwrl <n/a> | 2025-08-14 21:15:50 -0500 |
|---|---|---|
| committer | nwrl <n/a> | 2025-08-14 21:15:50 -0500 |
| commit | 87cce4e5e5136b5ae2fe6033d555e2a87eb557a2 (patch) | |
| tree | 7c765f7d029f60724692ff890137d0d079cc7666 /nameblocker.sp | |
| parent | e82de90e16a9be53abc13a372a2d7be1724e8ce6 (diff) | |
Complete some error handling, add kick and ban message convars
Diffstat (limited to 'nameblocker.sp')
| -rw-r--r-- | nameblocker.sp | 130 |
1 files 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 = { | |||
| 48 | url = "git.dabikers.online/smnameblocker" | 48 | url = "git.dabikers.online/smnameblocker" |
| 49 | }; | 49 | }; |
| 50 | 50 | ||
| 51 | // 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) | ||
| 51 | #define HANDLE_SIZE (32) | 52 | #define HANDLE_SIZE (32) |
| 52 | // 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 | ||
| 53 | // any other methodmap or descendant of Handle (like Regex) | ||
| 54 | 53 | ||
| 55 | #define STEAMID64LENGTH 17 | 54 | // The literal character width of a steamid in 64bit representation. Not necessarily large enough to store a steamid64 in a string |
| 55 | #define LITERALSTEAMID64LENGTH (17) | ||
| 56 | // Length of a steamid64, suitably large enough to store in a string | ||
| 57 | #define STEAMID64LENGTH (LITERALSTEAMID64LENGTH + 1) | ||
| 56 | 58 | ||
| 57 | #define PATTERN_MAX_LEN (512 + 1) /* 512 chars + null terminator */ | 59 | #define PATTERN_MAX_LEN (512 + 1) /* 512 chars + null terminator */ |
| 58 | #define DATABASE_FAIL_MSG "Could not populate regex & pattern lists from database" | 60 | #define DATABASE_FAIL_MSG ("Could not populate regex & pattern lists from database") |
| 59 | 61 | ||
| 60 | enum OperatingMode { | 62 | enum OperatingMode { |
| 61 | OP_DISABLED, | 63 | OP_DISABLED, |
| @@ -85,8 +87,9 @@ ConVar gcvarOperMode; static const char OPERMODENAME[] = "nameb | |||
| 85 | ConVar gcvarAdmCmdFlag; static const char ADMCMDFLAGNAME[] = "nameblock_AdminCommandFlag"; const int DEFAULTADMCMDFLAG = ADMFLAG_BAN; | 87 | ConVar gcvarAdmCmdFlag; static const char ADMCMDFLAGNAME[] = "nameblock_AdminCommandFlag"; const int DEFAULTADMCMDFLAG = ADMFLAG_BAN; |
| 86 | ConVar gcvarRegexCompFlags; static const char REGEXCOMPFLAGSNAME[] = "nameblock_RegexCompilationFlags"; const int DEFAULTREGEXCOMPFLAGS = (PCRE_CASELESS | PCRE_DOTALL | PCRE_EXTENDED | PCRE_UTF8); | 88 | ConVar gcvarRegexCompFlags; static const char REGEXCOMPFLAGSNAME[] = "nameblock_RegexCompilationFlags"; const int DEFAULTREGEXCOMPFLAGS = (PCRE_CASELESS | PCRE_DOTALL | PCRE_EXTENDED | PCRE_UTF8); |
| 87 | ConVar gcvarAmdImmFlag; static const char ADMINIMMUNITYFLAGNAME[] = "nameblock_AdminImmunityFlag"; const int DEFAULTADMIMMFLAG = ADMFLAG_GENERIC; | 89 | ConVar gcvarAmdImmFlag; static const char ADMINIMMUNITYFLAGNAME[] = "nameblock_AdminImmunityFlag"; const int DEFAULTADMIMMFLAG = ADMFLAG_GENERIC; |
| 88 | 90 | ConVar gcvarBanTime; static const char BANTIMENAME[] = "nameblock_BanTime"; const int DEFAULTBANTIME = 10080; // 1 week in minutes | |
| 89 | 91 | ConVar gcvarKickMsg; static const char KICKMSGNAME[] = "nameblock_KickMessage"; static const char DEFAULTKICKMSG[] = "Failed name check"; | |
| 92 | ConVar gcvarBanMsg; static const char BANMSGNAME[] = "nameblock_BanMessage"; static const char DEFAULTBANMESSAGE[] = "Failed name check"; | ||
| 90 | 93 | ||
| 91 | // Logs and throws an error with the same message of upto 2048 characters | 94 | // Logs and throws an error with the same message of upto 2048 characters |
| 92 | void logAndFail(const char[] format, any ...) { | 95 | void logAndFail(const char[] format, any ...) { |
| @@ -221,10 +224,17 @@ public void OnAllPluginsLoaded() { | |||
| 221 | loadFromDatabase(); | 224 | loadFromDatabase(); |
| 222 | 225 | ||
| 223 | // Register convars | 226 | // Register convars |
| 224 | xRegisterIntConVar(gcvarOperMode, DEFAULTOPERMODE, OPERMODENAME, "Operating mode (disabled, kick, ban, etc.)"); | 227 | xRegisterIntConVar(gcvarOperMode, DEFAULTOPERMODE, OPERMODENAME, "Operating mode (disabled, kick, ban, etc.)"); |
| 225 | xRegisterIntConVar(gcvarAdmCmdFlag, DEFAULTADMCMDFLAG, ADMCMDFLAGNAME, "Admin flag to modify pattern list"); | 228 | xRegisterIntConVar(gcvarAdmCmdFlag, DEFAULTADMCMDFLAG, ADMCMDFLAGNAME, "Admin flag to modify pattern list"); |
| 226 | xRegisterIntConVar(gcvarRegexCompFlags, DEFAULTREGEXCOMPFLAGS, REGEXCOMPFLAGSNAME, "Regular expression compilation flags"); | 229 | xRegisterIntConVar(gcvarRegexCompFlags, DEFAULTREGEXCOMPFLAGS, REGEXCOMPFLAGSNAME, "Regular expression compilation flags"); |
| 227 | xRegisterIntConVar(gcvarAmdImmFlag, DEFAULTADMIMMFLAG, ADMINIMMUNITYFLAGNAME, "Admin immunity flag"); | 230 | xRegisterIntConVar(gcvarAmdImmFlag, DEFAULTADMIMMFLAG, ADMINIMMUNITYFLAGNAME, "Admin immunity flag"); |
| 231 | xRegisterIntConVar(gcvarBanTime, DEFAULTBANTIME, BANTIMENAME, "Time (in minutes) to ban players when operating in ban mode. 0 for permaban"); | ||
| 232 | |||
| 233 | gcvarKickMsg = CreateConVar(KICKMSGNAME, DEFAULTKICKMSG, "Default kick message"); | ||
| 234 | if(gcvarKickMsg == null) logAndFail("Could not init convar \"gcvarKickMsg\""); | ||
| 235 | |||
| 236 | gcvarBanMsg = CreateConVar(BANMSGNAME, DEFAULTBANMESSAGE, "Default ban message"); | ||
| 237 | if(gcvarBanMsg == null) logAndFail("Could not init convar \"gcvarBanMsg\""); | ||
| 228 | 238 | ||
| 229 | AutoExecConfig(true, "nameblocker_cvars"); | 239 | AutoExecConfig(true, "nameblocker_cvars"); |
| 230 | 240 | ||
| @@ -334,28 +344,56 @@ enum MOD_MODE { | |||
| 334 | int __modPattern__insert(int index, char[] pattern, int patternlen, int client) { | 344 | int __modPattern__insert(int index, char[] pattern, int patternlen, int client) { |
| 335 | if(IsNullString(pattern) || patternlen < 0 || patternlen > PATTERN_MAX_LEN || client < 0) {return -1;} | 345 | if(IsNullString(pattern) || patternlen < 0 || patternlen > PATTERN_MAX_LEN || client < 0) {return -1;} |
| 336 | 346 | ||
| 337 | char steamid64[STEAMID64LENGTH + 1]; | 347 | char steamid64[STEAMID64LENGTH]; |
| 338 | if(GetClientAuthId(client, AuthId_SteamID64, steamid64, sizeof(steamid64))) {} // TODO: Error handling | 348 | char sqlerr[512 + 1]; |
| 349 | bool flag = false; | ||
| 350 | |||
| 351 | if(!GetClientAuthId(client, AuthId_SteamID64, steamid64, sizeof(steamid64))) { | ||
| 352 | LogError("Could not get client \"%N\"'s steamid64", client); | ||
| 353 | return -1; | ||
| 354 | } | ||
| 355 | |||
| 339 | 356 | ||
| 340 | SQL_LockDatabase(db); | 357 | SQL_LockDatabase(db); |
| 341 | SQL_BindParamString(dbInsert, 0, pattern, false); | 358 | |
| 342 | SQL_BindParamString(dbInsert, 1, steamid64, false); | 359 | SQL_BindParamString(dbInsert, 0, pattern, false); |
| 343 | if(!SQL_Execute(dbInsert)) {} // TODO: Error handling | 360 | SQL_BindParamString(dbInsert, 1, steamid64, false); |
| 361 | if(!SQL_Execute(dbInsert)) { | ||
| 362 | SQL_GetError(dbInsert, sqlerr, sizeof(sqlerr)); | ||
| 363 | LogError("Could not insert values into database: %s", sqlerr); | ||
| 364 | flag = true; | ||
| 365 | } | ||
| 366 | |||
| 344 | SQL_UnlockDatabase(db); | 367 | SQL_UnlockDatabase(db); |
| 345 | 368 | ||
| 369 | if(flag) | ||
| 370 | return -1; | ||
| 371 | |||
| 346 | if(aModPattern(MM_INSERT, index, pattern)) {} // TODO: Error handling | 372 | if(aModPattern(MM_INSERT, index, pattern)) {} // TODO: Error handling |
| 347 | 373 | ||
| 348 | return 0; | 374 | return 0; |
| 349 | } | 375 | } |
| 350 | int __modPattern__delete(int index) { | 376 | int __modPattern__delete(int index) { |
| 351 | char pattern[PATTERN_MAX_LEN]; | 377 | char pattern[PATTERN_MAX_LEN]; |
| 378 | char sqlerr[512 + 1]; | ||
| 379 | bool flag = false; | ||
| 380 | |||
| 352 | patternlist.GetString(index, pattern, sizeof(pattern), ByteCountToCells(PATTERN_MAX_LEN)); | 381 | patternlist.GetString(index, pattern, sizeof(pattern), ByteCountToCells(PATTERN_MAX_LEN)); |
| 353 | 382 | ||
| 354 | SQL_LockDatabase(db); | 383 | SQL_LockDatabase(db); |
| 355 | SQL_BindParamString(dbDelete, 0, pattern, false); | 384 | |
| 356 | if(SQL_Execute(dbDelete)) {} // TODO: Error handling | 385 | SQL_BindParamString(dbDelete, 0, pattern, false); |
| 386 | if(!SQL_Execute(dbDelete)) { | ||
| 387 | SQL_GetError(dbDelete, sqlerr, sizeof(sqlerr)); | ||
| 388 | LogError("Could not delete from database: %s", sqlerr); | ||
| 389 | flag = true; | ||
| 390 | } | ||
| 391 | |||
| 357 | SQL_UnlockDatabase(db); | 392 | SQL_UnlockDatabase(db); |
| 358 | 393 | ||
| 394 | if(flag) | ||
| 395 | return -1; | ||
| 396 | |||
| 359 | if(aModPattern(MM_DELETE, index)) {} // TODO: Error handling | 397 | if(aModPattern(MM_DELETE, index)) {} // TODO: Error handling |
| 360 | 398 | ||
| 361 | return 0; | 399 | return 0; |
| @@ -364,19 +402,33 @@ int __modPattern__replace(int index, char[] pattern, int patternlen, int client) | |||
| 364 | if(IsNullString(pattern) || patternlen < 0 || patternlen > PATTERN_MAX_LEN || client < 0) {return -1;} | 402 | if(IsNullString(pattern) || patternlen < 0 || patternlen > PATTERN_MAX_LEN || client < 0) {return -1;} |
| 365 | 403 | ||
| 366 | char oldpattern[PATTERN_MAX_LEN]; | 404 | char oldpattern[PATTERN_MAX_LEN]; |
| 367 | patternlist.GetString(index, oldpattern, sizeof(oldpattern), ByteCountToCells(PATTERN_MAX_LEN)); | 405 | char steamid64[STEAMID64LENGTH]; |
| 406 | char sqlerr[512]; | ||
| 407 | bool flag = false; | ||
| 368 | 408 | ||
| 369 | char steamid64[STEAMID64LENGTH + 1]; | 409 | patternlist.GetString(index, oldpattern, sizeof(oldpattern), ByteCountToCells(PATTERN_MAX_LEN)); |
| 370 | if(GetClientAuthId(client, AuthId_SteamID64, steamid64, sizeof(steamid64))) {} // TODO: Error handling | 410 | if(!GetClientAuthId(client, AuthId_SteamID64, steamid64, sizeof(steamid64))) { |
| 411 | LogError("Could not get \"%N\"'s steamid64"); | ||
| 412 | return -1; | ||
| 413 | } | ||
| 371 | 414 | ||
| 372 | SQL_LockDatabase(db); | 415 | SQL_LockDatabase(db); |
| 373 | SQL_BindParamString(dbReplace, 0, pattern, false); | ||
| 374 | SQL_BindParamString(dbReplace, 1, steamid64, false); | ||
| 375 | SQL_BindParamString(dbReplace, 2, oldpattern, false); | ||
| 376 | 416 | ||
| 377 | if(SQL_Execute(dbReplace)) {} // TODO: Error handling | 417 | SQL_BindParamString(dbReplace, 0, pattern, false); |
| 418 | SQL_BindParamString(dbReplace, 1, steamid64, false); | ||
| 419 | SQL_BindParamString(dbReplace, 2, oldpattern, false); | ||
| 420 | |||
| 421 | if(!SQL_Execute(dbReplace)) { | ||
| 422 | SQL_GetError(dbReplace, sqlerr, sizeof(sqlerr)); | ||
| 423 | LogError("Could not replace in database: %s", sqlerr); | ||
| 424 | flag = true; | ||
| 425 | } | ||
| 426 | |||
| 378 | SQL_UnlockDatabase(db); | 427 | SQL_UnlockDatabase(db); |
| 379 | 428 | ||
| 429 | if(flag) | ||
| 430 | return -1; | ||
| 431 | |||
| 380 | if(aModPattern(MM_REPLACE, index, pattern)) {} // TODO: Error handling | 432 | if(aModPattern(MM_REPLACE, index, pattern)) {} // TODO: Error handling |
| 381 | 433 | ||
| 382 | return 0; | 434 | return 0; |
| @@ -452,23 +504,6 @@ int aModPattern(MOD_MODE mode, int index, char[] pattern="", int patternlen=0) { | |||
| 452 | return 0; | 504 | return 0; |
| 453 | } | 505 | } |
| 454 | 506 | ||
| 455 | // int aInsertPattern(char pattern[PATTERN_MAX_LEN], Regex res, int index) { | ||
| 456 | // if(IsNullString(pattern) || res == null || index < 0 || index > regexlist.Length) return -1; | ||
| 457 | |||
| 458 | // if(index == regexlist.Length) { | ||
| 459 | // regexlist.Push(res); | ||
| 460 | // patternlist.Push(pattern); | ||
| 461 | |||
| 462 | // } else { | ||
| 463 | // regexlist.ShiftUp(index); | ||
| 464 | // patternlist.ShiftUp(index); | ||
| 465 | // regexlist.Set(index, res); | ||
| 466 | // patternlist.Set(index, pattern); | ||
| 467 | |||
| 468 | // } | ||
| 469 | |||
| 470 | // return 0; | ||
| 471 | // } | ||
| 472 | 507 | ||
| 473 | 508 | ||
| 474 | 509 | ||
| @@ -514,14 +549,21 @@ int handleNameHit(int client) { | |||
| 514 | switch(gcvarOperMode.IntValue) { | 549 | switch(gcvarOperMode.IntValue) { |
| 515 | case OP_DISABLED: {return 0;} | 550 | case OP_DISABLED: {return 0;} |
| 516 | case OP_KICK: { | 551 | case OP_KICK: { |
| 517 | KickClient(client, "Failed name check"); | 552 | char kickmsg[512]; |
| 553 | gcvarKickMsg.GetString(kickmsg, sizeof(kickmsg)); | ||
| 554 | |||
| 555 | KickClient(client, kickmsg); | ||
| 518 | LogAction(0, client, "Kicked %L for failing a name check", client); | 556 | LogAction(0, client, "Kicked %L for failing a name check", client); |
| 519 | return 0; | 557 | return 0; |
| 520 | } | 558 | } |
| 521 | case OP_BAN: { | 559 | case OP_BAN: { |
| 522 | // BanClient() | 560 | char banmsg[512]; |
| 561 | gcvarBanMsg.GetString(banmsg, sizeof(banmsg)); | ||
| 562 | |||
| 563 | BanClient(client, gcvarBanTime.IntValue, BANFLAG_AUTHID, "Failed name check", banmsg); | ||
| 564 | LogAction(0, client, "Banned %L for failing a name check", client); | ||
| 565 | |||
| 523 | // TODO: Interop with other ban systems | 566 | // TODO: Interop with other ban systems |
| 524 | // Log ban | ||
| 525 | return 0; | 567 | return 0; |
| 526 | } | 568 | } |
| 527 | default: { | 569 | default: { |
