csrd_name_filter.sp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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.4"
  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. }
  72. bNameChanged |= bCharFiltered;
  73. i += nCharBytes;
  74. }
  75. if (bNameChanged) {
  76. // Remove surrounding whitespace
  77. TrimString(newNameBuffer);
  78. // TODO remove duplicate whitespace
  79. // If there's no printable characters in the buffer, the player is a total dongbus.
  80. if (strlen(newNameBuffer) == 0) {
  81. Format(newNameBuffer, sizeof(newNameBuffer), UNPRINTABLE_NAME_USER_FORMAT,
  82. GetClientUserId(client));
  83. SetClientNameLock(client);
  84. }
  85. SetClientName(client, newNameBuffer);
  86. }
  87. // TODO else not name changed, but check if we can do other non-filter stuff? (trim)
  88. }
  89. bool IsUnicodeCharInvisible(int uchar) {
  90. // handle invisible math operators, &c. (U+205F ... U+206F)
  91. // could do (char & 0xe281a0) plus check for U+205F, I think?
  92. if (uchar >= 0xe2819f && uchar <= 0xe281af) {
  93. return true;
  94. }
  95. // handle other blacklisted characters
  96. int c;
  97. do {
  98. if (uchar == g_FilteredMultiByteWhitespace[c]) {
  99. return true;
  100. }
  101. } while (g_FilteredMultiByteWhitespace[++c] != 0);
  102. return false;
  103. }
  104. /**
  105. * Attempts to lock the specified client's name with the existing `namelockid` command.
  106. */
  107. stock bool SetClientNameLock(int client, bool locked = true) {
  108. if (CommandExists("namelockid")) {
  109. ServerCommand("namelockid %d %d", GetClientUserId(client), locked ? 0 : 1);
  110. return true;
  111. }
  112. return false;
  113. }
  114. /**
  115. * Gets a possibly multi-byte character at the specified position in a string as an integer.
  116. *
  117. * If the character is an ASCII character, the integer representation of the character will be
  118. * returned, and the number of characters read will be 1.
  119. *
  120. * This stock is made specifically for the purpose of handling strings with multi-byte
  121. * characters.
  122. */
  123. stock int GetMultiByteCharacterAt(const char[] text, int pos, int &read = 0) {
  124. int nCharBytes, charOutput;
  125. nCharBytes = GetCharBytes(text[pos]);
  126. for (int b = 0; b < nCharBytes; b++) {
  127. charOutput = charOutput << 8;
  128. // read in the current byte
  129. charOutput |= text[pos + b] & 0xFF;
  130. read++;
  131. }
  132. return charOutput;
  133. }
  134. /**
  135. * Appends a multi-byte character to a string.
  136. */
  137. stock void StrCatMultiByteChar(char[] buffer, int maxlen, int character, int size) {
  138. char[] appended = new char[size];
  139. MultiByteCharacterToCharArray(appended, size, character);
  140. StrCat(buffer, maxlen, appended);
  141. }
  142. /**
  143. * Converts a multi-byte character to a char array.
  144. */
  145. stock void MultiByteCharacterToCharArray(char[] buffer, int charsize, int character) {
  146. for (int i = 0; i < charsize; i++) {
  147. buffer[i] = (character >> ((charsize - i - 1) * 8 )) & 0xFF;
  148. }
  149. }