csrd_name_filter.sp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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.3"
  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_FilteredMultiByteWhitespace[] = {
  26. 0xe2a080, // braille pattern blank U+2800
  27. 0
  28. };
  29. public void OnPluginStart() {
  30. for (int i = 1; i <= MaxClients; i++) {
  31. if (IsClientConnected(i)) {
  32. OnNameUpdated(i);
  33. }
  34. }
  35. }
  36. public void OnClientConnected(int client) {
  37. OnNameUpdated(client);
  38. }
  39. public void OnClientSettingsChanged(int client) {
  40. OnNameUpdated(client);
  41. }
  42. void OnNameUpdated(int client) {
  43. char currentName[MAX_NAME_LENGTH], newNameBuffer[MAX_NAME_LENGTH];
  44. GetClientName(client, currentName, sizeof(currentName));
  45. bool bNameChanged;
  46. /**
  47. * Copies valid characters into another buffer (while also dealing with Unicode).
  48. * The buffer is used if any characters are filtered.
  49. */
  50. for (int i = 0; i < strlen(currentName); /* ... */) {
  51. int fullChar, nCharBytes;
  52. fullChar = GetMultiByteCharacterAt(currentName, i, nCharBytes);
  53. bool bCharFiltered;
  54. // Filter out non-printing characters that can't be handled elsewhere.
  55. switch (nCharBytes) {
  56. case 3: {
  57. // filter unicode invisible characters
  58. bCharFiltered |= IsUnicodeCharInvisible(fullChar);
  59. }
  60. case 1: {
  61. // filter out newlines
  62. bCharFiltered |= (fullChar == 0x0d || fullChar == 0x0a);
  63. }
  64. default: {
  65. // unhandled
  66. }
  67. }
  68. if (!bCharFiltered) {
  69. // it's a character we're not filtering
  70. StrCatMultiByteChar(newNameBuffer, sizeof(newNameBuffer), fullChar, nCharBytes);
  71. } else {
  72. // we filtered this one
  73. // we could be as rude as they are in using unprintables, but we're nice
  74. // TODO check if the last character in new buffer is a space to prevent multiples
  75. StrCat(newNameBuffer, sizeof(newNameBuffer), " ");
  76. }
  77. bNameChanged |= bCharFiltered;
  78. i += nCharBytes;
  79. }
  80. if (bNameChanged) {
  81. // Remove surrounding whitespace
  82. TrimString(newNameBuffer);
  83. // If there's no printable characters in the buffer, the player is a total dongbus.
  84. if (strlen(newNameBuffer) == 0) {
  85. Format(newNameBuffer, sizeof(newNameBuffer), UNPRINTABLE_NAME_USER_FORMAT,
  86. GetClientUserId(client));
  87. SetClientNameLock(client);
  88. }
  89. SetClientName(client, newNameBuffer);
  90. }
  91. // TODO else not name changed, but check if we can do other non-filter stuff? (trim)
  92. }
  93. bool IsUnicodeCharInvisible(int uchar) {
  94. // handle invisible math operators, &c. (U+205F ... U+206F)
  95. // could do (char & 0xe281a0) plus check for U+205F, I think?
  96. if (uchar >= 0xe2819f && uchar <= 0xe281af) {
  97. return true;
  98. }
  99. // handle other blacklisted characters
  100. int c;
  101. do {
  102. if (uchar == g_FilteredMultiByteWhitespace[c]) {
  103. return true;
  104. }
  105. } while (g_FilteredMultiByteWhitespace[++c] != 0);
  106. return false;
  107. }
  108. /**
  109. * Attempts to lock the specified client's name with the existing `namelockid` command.
  110. */
  111. stock bool SetClientNameLock(int client, bool locked = true) {
  112. if (CommandExists("namelockid")) {
  113. ServerCommand("namelockid %d %d", GetClientUserId(client), locked ? 0 : 1);
  114. return true;
  115. }
  116. return false;
  117. }
  118. /**
  119. * Gets a possibly multi-byte character at the specified position in a string as an integer.
  120. *
  121. * If the character is an ASCII character, the integer representation of the character will be
  122. * returned, and the number of characters read will be 1.
  123. *
  124. * This stock is made specifically for the purpose of handling strings with multi-byte
  125. * characters.
  126. */
  127. stock int GetMultiByteCharacterAt(const char[] text, int pos, int &read = 0) {
  128. int nCharBytes, charOutput;
  129. nCharBytes = GetCharBytes(text[pos]);
  130. for (int b = 0; b < nCharBytes; b++) {
  131. charOutput = charOutput << 8;
  132. // read in the current byte
  133. charOutput |= text[pos + b] & 0xFF;
  134. read++;
  135. }
  136. return charOutput;
  137. }
  138. /**
  139. * Appends a multi-byte character to a string.
  140. */
  141. stock void StrCatMultiByteChar(char[] buffer, int maxlen, int character, int size) {
  142. char[] appended = new char[size];
  143. MultiByteCharacterToCharArray(appended, size, character);
  144. StrCat(buffer, maxlen, appended);
  145. }
  146. /**
  147. * Converts a multi-byte character to a char array.
  148. */
  149. stock void MultiByteCharacterToCharArray(char[] buffer, int charsize, int character) {
  150. for (int i = 0; i < charsize; i++) {
  151. buffer[i] = (character >> ((charsize - i - 1) * 8 )) & 0xFF;
  152. }
  153. }