Skip to content

Commit

Permalink
Prevent invalid characters from breaking the bans.json file, also imp…
Browse files Browse the repository at this point in the history
…roved auto-ban message and cleaned up the file.
  • Loading branch information
TomGrobbe committed Apr 16, 2018
1 parent 3e9282e commit ed2fed0
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions vMenuServer/BanManager.cs
Expand Up @@ -6,6 +6,7 @@
using Newtonsoft.Json;
using CitizenFX.Core;
using static CitizenFX.Core.Native.API;
using System.Text.RegularExpressions;

namespace vMenuServer
{
Expand Down Expand Up @@ -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;
}
Expand All @@ -155,18 +155,19 @@ private void CheckForBans([FromSource]Player source, string playerName, Callback
/// <param name="banReason">The reason why the player is getting banned.</param>
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<string>(),
playerName = target.Name
playerName = GetSafePlayerName(target.Name)
};
if (AddBan(ban))
{
Expand Down Expand Up @@ -198,18 +199,19 @@ private void BanPlayer([FromSource] Player source, int targetPlayer, string banR
/// <param name="banReason">Reason for the ban.</param>
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<string>(),
playerName = target.Name
playerName = GetSafePlayerName(target.Name)
};
if (AddBan(ban))
{
Expand Down Expand Up @@ -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.
}


/// <summary>
/// Returns the safe playername string to be used in json converter to prevent fuckups when saving the bans file.
/// </summary>
/// <param name="playerName"></param>
/// <returns></returns>
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[] { '.', ',', ' ', '!', '?' });
}
}
}

0 comments on commit ed2fed0

Please sign in to comment.