csrd_name_filter.sp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. * If their existing name has no printable characters, their name will be changed to
  9. * "unprintable" plus their userid and their name will be locked.
  10. */
  11. #pragma semicolon 1
  12. #include <sourcemod>
  13. #include <sdktools_functions>
  14. #pragma newdecls required
  15. #define PLUGIN_VERSION "1.0.1"
  16. public Plugin myinfo = {
  17. name = "[CSRD] Name Filter",
  18. author = "nosoop",
  19. description = "Filters out rather unscrupulous characters from names and locks the name if "
  20. ... "they are using a completely non-printable name.",
  21. version = PLUGIN_VERSION,
  22. url = "https://pika.nom-nom-nom.us/git/"
  23. }
  24. #define UNPRINTABLE_NAME_USER_FORMAT "unprintable (%d)"
  25. int g_FilteredWhitespace[] = {
  26. 0xe2a080, // braille pattern blank U+2800
  27. 0x0a, // linefeed
  28. 0x0d, // carriage return
  29. 0
  30. };
  31. public void OnPluginStart() {
  32. for (int i = 1; i <= MaxClients; i++) {
  33. if (IsClientConnected(i)) {
  34. OnNameUpdated(i);
  35. }
  36. }
  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 non-printing characters that can't be handled elsewhere.
  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 we're not filtering
  63. StrCatMultiByteChar(newNameBuffer, sizeof(newNameBuffer), fullChar, nCharBytes);
  64. } else {
  65. // we filtered this one
  66. // we could be as rude as they are in using unprintables, but we're nice
  67. // TODO check if the last character in new buffer is a space to prevent multiples
  68. StrCat(newNameBuffer, sizeof(newNameBuffer), " ");
  69. }
  70. bNameChanged |= bCharFiltered;
  71. i += nCharBytes;
  72. }
  73. if (bNameChanged) {
  74. // Remove surrounding whitespace
  75. TrimString(newNameBuffer);
  76. // If there's no printable characters in the buffer, the player is a total dongbus.
  77. if (strlen(newNameBuffer) == 0) {
  78. Format(newNameBuffer, sizeof(newNameBuffer), UNPRINTABLE_NAME_USER_FORMAT,
  79. GetClientUserId(client));
  80. SetClientNameLock(client);
  81. }
  82. SetClientName(client, newNameBuffer);
  83. }
  84. // TODO else not name changed, but check if we can do other non-filter stuff? (trim)
  85. }
  86. /**
  87. * Attempts to lock the specified client's name with the existing `namelockid` command.
  88. */
  89. stock bool SetClientNameLock(int client, bool locked = true) {
  90. if (CommandExists("namelockid")) {
  91. ServerCommand("namelockid %d %d", GetClientUserId(client), locked ? 0 : 1);
  92. return true;
  93. }
  94. return false;
  95. }
  96. /**
  97. * Gets a possibly multi-byte character at the specified position in a string as an integer.
  98. *
  99. * If the character is an ASCII character, the integer representation of the character will be
  100. * returned, and the number of characters read will be 1.
  101. *
  102. * This stock is made specifically for the purpose of handling strings with multi-byte
  103. * characters.
  104. */
  105. stock int GetMultiByteCharacterAt(const char[] text, int pos, int &read = 0) {
  106. int nCharBytes, charOutput;
  107. nCharBytes = GetCharBytes(text[pos]);
  108. for (int b = 0; b < nCharBytes; b++) {
  109. charOutput = charOutput << 8;
  110. // read in the current byte
  111. charOutput |= text[pos + b] & 0xFF;
  112. read++;
  113. }
  114. return charOutput;
  115. }
  116. /**
  117. * Appends a multi-byte character to a string.
  118. */
  119. stock void StrCatMultiByteChar(char[] buffer, int maxlen, int character, int size) {
  120. char[] appended = new char[size];
  121. MultiByteCharacterToCharArray(appended, size, character);
  122. StrCat(buffer, maxlen, appended);
  123. }
  124. /**
  125. * Converts a multi-byte character to a char array.
  126. */
  127. stock void MultiByteCharacterToCharArray(char[] buffer, int charsize, int character) {
  128. for (int i = 0; i < charsize; i++) {
  129. buffer[i] = (character >> ((charsize - i - 1) * 8 )) & 0xFF;
  130. }
  131. }