From ed2fed065b3a4fd5460258de9f126f4842611459 Mon Sep 17 00:00:00 2001 From: Vespura <31419184+TomGrobbe@users.noreply.github.com> Date: Mon, 16 Apr 2018 17:47:25 +0200 Subject: [PATCH] Prevent invalid characters from breaking the bans.json file, also improved auto-ban message and cleaned up the file. --- vMenuServer/BanManager.cs | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/vMenuServer/BanManager.cs b/vMenuServer/BanManager.cs index 37b464f0..5ab271ce 100644 --- a/vMenuServer/BanManager.cs +++ b/vMenuServer/BanManager.cs @@ -6,6 +6,7 @@ using Newtonsoft.Json; using CitizenFX.Core; using static CitizenFX.Core.Native.API; +using System.Text.RegularExpressions; namespace vMenuServer { @@ -139,7 +140,6 @@ private void CheckForBans([FromSource]Player source, string playerName, Callback Debug.WriteLine("Ban time expired, but an unknown error occurred while removing the player from the banlist!" + " They have been allowed to join the server, but please remove them from the ban list manually!"); } - } break; } @@ -155,18 +155,19 @@ private void CheckForBans([FromSource]Player source, string playerName, Callback /// The reason why the player is getting banned. private void BanPlayer([FromSource] Player source, int targetPlayer, string banReason) { - if (IsPlayerAceAllowed(source.Handle, "vMenu.OnlinePlayers.PermBan") || IsPlayerAceAllowed(source.Handle, "vMenu.Everything") || IsPlayerAceAllowed(source.Handle, "vMenu.OnlinePlayers.All")) + if (IsPlayerAceAllowed(source.Handle, "vMenu.OnlinePlayers.PermBan") || IsPlayerAceAllowed(source.Handle, "vMenu.Everything") || + IsPlayerAceAllowed(source.Handle, "vMenu.OnlinePlayers.All")) { Player target = new PlayerList()[targetPlayer]; if (!IsPlayerAceAllowed(target.Handle, "vMenu.DontBanMe")) { BanRecord ban = new BanRecord() { - bannedBy = source.Name, + bannedBy = GetSafePlayerName(source.Name), bannedUntil = new DateTime(3000, 1, 1), banReason = banReason, identifiers = target.Identifiers.ToList(), - playerName = target.Name + playerName = GetSafePlayerName(target.Name) }; if (AddBan(ban)) { @@ -198,18 +199,19 @@ private void BanPlayer([FromSource] Player source, int targetPlayer, string banR /// Reason for the ban. private void BanPlayer([FromSource] Player source, int targetPlayer, double banDurationHours, string banReason) { - if (IsPlayerAceAllowed(source.Handle, "vMenu.OnlinePlayers.TempBan") || IsPlayerAceAllowed(source.Handle, "vMenu.Everything") || IsPlayerAceAllowed(source.Handle, "vMenu.OnlinePlayers.All")) + if (IsPlayerAceAllowed(source.Handle, "vMenu.OnlinePlayers.TempBan") || IsPlayerAceAllowed(source.Handle, "vMenu.Everything") || + IsPlayerAceAllowed(source.Handle, "vMenu.OnlinePlayers.All")) { Player target = new PlayerList()[targetPlayer]; if (!IsPlayerAceAllowed(target.Handle, "vMenu.DontBanMe")) { BanRecord ban = new BanRecord() { - bannedBy = source.Name, + bannedBy = GetSafePlayerName(source.Name), bannedUntil = DateTime.Now.AddHours(banDurationHours <= 720.0 ? banDurationHours : 720.0), banReason = banReason, identifiers = target.Identifiers.ToList(), - playerName = target.Name + playerName = GetSafePlayerName(target.Name) }; if (AddBan(ban)) { @@ -331,14 +333,27 @@ public static void BanCheater(Player source) { AddBan(new BanRecord() { - bannedBy = "Yourself, idiot.", + bannedBy = "vMenu Auto Ban", bannedUntil = new DateTime(3000, 1, 1), - banReason = "You know exactly what you did wrong, you're a fucking idiot, but nobody needs to tell you that. Enjoy this: https://youtu.be/dQw4w9WgXcQ", + banReason = "You have been automatically banned. If you believe this was done by error, please contact the server owner for support.", identifiers = source.Identifiers.ToList(), - playerName = source.Name + playerName = GetSafePlayerName(source.Name) }); //source.Drop("Enjoy, idiot."); source.TriggerEvent("vMenu:GoodBye"); // this is much more fun than just kicking them. } + + + /// + /// Returns the safe playername string to be used in json converter to prevent fuckups when saving the bans file. + /// + /// + /// + public static string GetSafePlayerName(string playerName) + { + string safeName = playerName.Replace("^", "").Replace("<", "").Replace(">", "").Replace("~", ""); + safeName = Regex.Replace(safeName, @"[^\u0000-\u007F]+", string.Empty); + return safeName.Trim(new char[] { '.', ',', ' ', '!', '?' }); + } } }