Guest User

Untitled

a guest
Jan 8th, 2016
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace Oxide.Plugins
  7. {
  8. [Info("BlockedNames", "BadWolf", "1.0.0")]
  9. [Description("Prevents players from connecting with unwanted names.")]
  10.  
  11. class BlockedNames : CovalencePlugin
  12. {
  13. // Do NOT edit this file, instead edit BlockedNames.json in oxide/config and BlockedNames.en.json in the oxide/lang directory,
  14. // or create a new language file for another language using the 'en' file as a default.
  15.  
  16. #region Localization
  17.  
  18. void LoadDefaultMessages()
  19. {
  20. lang.RegisterMessages(new Dictionary<string, string>
  21. {
  22. {"BlockedWords", "Name contains blocked words"},
  23. {"InvalidCharacters", "Invalid characters in name"},
  24. //{"InvalidCharactersLog", "{name} ({userid}) kicked for invalid characters in name"},
  25. {"UniqueNamesOnly", "Name must be unique"},
  26. //{"UniqueNamesOnlyLog", "{name} ({userid}) kicked for non-unique name"}
  27. }, this);
  28. }
  29.  
  30. #endregion
  31.  
  32. #region Configuration
  33.  
  34. char[] AllowedCharacters => GetConfig("AllowedCharacters", "✪abcdefghijklmnopqrstuvwxyz1234567890|;:~[](){}>!@#$%^&_-=+. ").ToCharArray();
  35. List<object> BlockedWords => GetConfig("BlockedWords", new List<object> { "admin", "moderator", "owner", "server" });
  36. bool StripHtmlTags => GetConfig("StripHtmlTags", true);
  37. bool UniqueNamesOnly => GetConfig("UniqueNamesOnly", true);
  38.  
  39. protected override void LoadDefaultConfig()
  40. {
  41. Config["AllowedCharacters"] = new string(AllowedCharacters);
  42. Config["BlockedWords"] = BlockedWords;
  43. Config["StripHtmlTags"] = StripHtmlTags;
  44. Config["UniqueNamesOnly"] = UniqueNamesOnly;
  45. SaveConfig();
  46. }
  47.  
  48. #endregion
  49.  
  50. #region Initialization
  51.  
  52. void Init()
  53. {
  54. #if !HURTWORLD && !REIGNOFKINGS && !RUST && !RUSTLEGACY
  55. throw new NotSupportedException("This plugin does not support this game");
  56. #endif
  57.  
  58. LoadDefaultConfig();
  59. LoadDefaultMessages();
  60. }
  61.  
  62. #endregion
  63.  
  64. #region Name Checking
  65.  
  66. bool IsNameBlocked(string name) => BlockedWords.Any(w => name.ToLower().Contains(w.ToString())); // TODO: Check casing
  67.  
  68. bool IsNameInvalid(string name) => !name.ToLower().All(AllowedCharacters.Contains);
  69.  
  70. bool IsNameUnique(string name) => players.Online.Any(p => !p.BasePlayer.Nickname.Equals(name));
  71.  
  72. string NameCheck(string name, string userId)
  73. {
  74. if (IsNameBlocked(name)) return GetMessage("BlockedWords", userId);
  75. if (IsNameInvalid(name)) return GetMessage("InvalidCharacters", userId);
  76. if (UniqueNamesOnly && IsNameUnique(name)) return GetMessage("UniqueNamesOnly", userId);
  77.  
  78. return null;
  79. }
  80.  
  81. #if HURTWORLD
  82. void OnPlayerConnected(PlayerSession session)
  83. {
  84. if (StripHtmlTags)
  85. {
  86. session.Name = session.Identity.Name = StripTags(session.Name);
  87. session.WorldPlayerEntity?.GetComponent<HurtMonoBehavior>().RPC("UpdateName", session.Player, session.Name);
  88. }
  89.  
  90. var player = players.GetOnlinePlayer(session.SteamId.ToString());
  91. if (IsNameInvalid(session.Name)) player.Kick(GetMessage("InvalidCharacters", session.SteamId.ToString()));
  92. if (IsNameBlocked(session.Name)) player.Kick(GetMessage("BlockedWords", session.SteamId.ToString()));
  93. //if (UniqueNamesOnly && IsNameUnique(session.Name)) player.Kick(GetMessage("UniqueNamesOnly", session.SteamId.ToString()));
  94. }
  95. #endif
  96.  
  97. #if REIGNOFKINGS
  98. object OnUserApprove(CodeHatch.Engine.Networking.Player player)
  99. {
  100. if (StripHtmlTags) player.Name = StripTags(player.Name);
  101. return NameCheck(player.Name, player.Id.ToString());
  102. }
  103. #endif
  104.  
  105. #if RUST
  106. object CanClientLogin(Network.Connection connection)
  107. {
  108. if (StripHtmlTags) connection.username = StripTags(connection.username);
  109. return NameCheck(connection.username, connection.userid.ToString());
  110. }
  111. #endif
  112.  
  113. #if RUSTLEGACY
  114. object CanClientLogin(Client.Connection connection)
  115. {
  116. if (StripHtmlTags) connection.username = StripTags(connection.username);
  117. return NameCheck(connection.username, connection.userid.ToString());
  118. }
  119. #endif
  120.  
  121. #endregion
  122.  
  123. #region Helper Methods
  124.  
  125. T GetConfig<T>(string name, T defaultValue)
  126. {
  127. if (Config[name] == null) return defaultValue;
  128. return (T)Convert.ChangeType(Config[name], typeof(T));
  129. }
  130.  
  131. string GetMessage(string key, string userId = null) => lang.GetMessage(key, this, userId);
  132.  
  133. static string StripTags(string name) => Regex.Replace(name, "<.*?>", string.Empty);
  134.  
  135. #endregion
  136. }
  137. }
Add Comment
Please, Sign In to add comment