csrd_name_filter.sp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * [CSRD] Name Filter
  3. *
  4. * An in-house plugin that tidies up names.
  5. * Made in response to a few script kiddies that are (ab)using newlines and other non-printing
  6. * characters in their name.
  7. *
  8. */
  9. #pragma semicolon 1
  10. #include <sourcemod>
  11. #include <sdktools_functions>
  12. #pragma newdecls required
  13. #define PLUGIN_VERSION "1.0.0"
  14. public Plugin myinfo = {
  15. name = "[CSRD] Name Filter",
  16. author = "nosoop",
  17. description = "Filters out rather unscrupulous characters from names and locks the name if "
  18. ... "they are using a completely non-printable name.",
  19. version = PLUGIN_VERSION,
  20. url = "https://pika.nom-nom-nom.us/git/"
  21. }
  22. #define UNPRINTABLE_NAME_USER_FORMAT "unprintable (%d)"
  23. bool g_bNameLockSupported;
  24. int g_FilteredWhitespace[] = {
  25. 0xe2a080, // braille pattern blank U+2800
  26. 0x0a, // linefeed
  27. 0x0d, // carriage return
  28. 0
  29. };
  30. public void OnPluginStart() {
  31. for (int i = 1; i <= MaxClients; i++) {
  32. if (IsClientConnected(i)) {
  33. OnNameUpdated(i);
  34. }
  35. }
  36. g_bNameLockSupported = CommandExists("namelockid");
  37. }
  38. public void OnClientConnected(int client) {
  39. OnNameUpdated(client);
  40. }
  41. public void OnClientSettingsChanged(int client) {
  42. OnNameUpdated(client);
  43. }
  44. void OnNameUpdated(int client) {
  45. char currentName[MAX_NAME_LENGTH], newNameBuffer[MAX_NAME_LENGTH];
  46. GetClientName(client, currentName, sizeof(currentName));
  47. bool bNameChanged;
  48. /**
  49. * Copies valid characters into another buffer (while also dealing with Unicode).
  50. * The buffer is used if any characters are filtered.
  51. */
  52. for (int i = 0; i < strlen(currentName); /* ... */) {
  53. int fullChar, nCharBytes;
  54. fullChar = GetMultiByteCharacterAt(currentName, i, nCharBytes);
  55. bool bCharFiltered;
  56. // Filter out whitespace
  57. int c;
  58. do {
  59. bCharFiltered |= (fullChar == g_FilteredWhitespace[c]);
  60. } while (g_FilteredWhitespace[++c] != 0 && !bCharFiltered);
  61. if (!bCharFiltered) {
  62. // it's a character that we don't really care about
  63. StrCatMultiByteChar(newNameBuffer, sizeof(newNameBuffer), fullChar, nCharBytes);
  64. } else {
  65. StrCat(newNameBuffer, sizeof(newNameBuffer), " ");
  66. }
  67. bNameChanged |= bCharFiltered;
  68. i += nCharBytes;
  69. }
  70. if (bNameChanged) {
  71. // Remove surrounding whitespace
  72. TrimString(newNameBuffer);
  73. // If there's no printable characters in the buffer, the player is a total dongbus.
  74. if (strlen(newNameBuffer) == 0) {
  75. Format(newNameBuffer, sizeof(newNameBuffer), UNPRINTABLE_NAME_USER_FORMAT,
  76. GetClientUserId(client));
  77. SetClientNameLock(client);
  78. }
  79. SetClientName(client, newNameBuffer);
  80. }
  81. // TODO else not name changed, but check if we can do other non-filter stuff? (trim)
  82. }
  83. /**
  84. * Attempts to lock the specified client's name.
  85. */
  86. stock void SetClientNameLock(int client, bool locked = true) {
  87. if (g_bNameLockSupported) {
  88. ServerCommand("namelockid %d %d", GetClientUserId(client), locked ? 0 : 1);
  89. }
  90. }
  91. /**
  92. * Gets a possibly multi-byte character at the specified position in a string as an integer.
  93. *
  94. * If the character is an ASCII character, the integer representation of the character will be
  95. * returned, and the number of characters read will be 1.
  96. *
  97. * This stock is made specifically for the purpose of handling strings with multi-byte
  98. * characters.
  99. */
  100. stock int GetMultiByteCharacterAt(const char[] text, int pos, int &read = 0) {
  101. int nCharBytes, charOutput;
  102. nCharBytes = GetCharBytes(text[pos]);
  103. for (int b = 0; b < nCharBytes; b++) {
  104. charOutput = charOutput << 8;
  105. // read in the current byte
  106. charOutput |= text[pos + b] & 0xFF;
  107. read++;
  108. }
  109. return charOutput;
  110. }
  111. /**
  112. * Appends a multi-byte character to a string.
  113. */
  114. stock void StrCatMultiByteChar(char[] buffer, int maxlen, int character, int size) {
  115. char[] appended = new char[size];
  116. MultiByteCharacterToCharArray(appended, size, character);
  117. StrCat(buffer, maxlen, appended);
  118. }
  119. /**
  120. * Converts a multi-byte character to a char array.
  121. */
  122. stock void MultiByteCharacterToCharArray(char[] buffer, int charsize, int character) {
  123. for (int i = 0; i < charsize; i++) {
  124. buffer[i] = (character >> ((charsize - i - 1) * 8 )) & 0xFF;
  125. }
  126. }