From c3eec844e1513ef91cb108e3efbb4e70c0f66eeb Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 17 Sep 2017 22:47:31 +1000 Subject: [PATCH] Tidy up Ban class, use unix timestamp for ban/unban time so now /baninfo is accurate to the second. --- MCGalaxy/Commands/Information/CmdBanInfo.cs | 37 ++++------- MCGalaxy/CorePlugin/ConnectingHandler.cs | 10 ++- MCGalaxy/Database/Stats/OnlineStat.cs | 10 +-- MCGalaxy/Player/Ban.cs | 71 ++++++++++++--------- 4 files changed, 68 insertions(+), 60 deletions(-) diff --git a/MCGalaxy/Commands/Information/CmdBanInfo.cs b/MCGalaxy/Commands/Information/CmdBanInfo.cs index 0b8081a95..30005e1c9 100644 --- a/MCGalaxy/Commands/Information/CmdBanInfo.cs +++ b/MCGalaxy/Commands/Information/CmdBanInfo.cs @@ -43,22 +43,24 @@ namespace MCGalaxy.Commands.Info { else if (ipBanned && banned) msg += " %Sand their IP are &CBANNED"; else msg += " %Sis not banned, but their IP is &CBANNED"; - string[] data = Ban.GetBanData(plName); - if (data != null && permaBanned) { - string grpName = Group.GetColoredName(data[3]); + string banner, reason, prevRank; + DateTime time; + Ban.GetBanData(plName, out banner, out reason, out time, out prevRank); + if (banner != null && permaBanned) { + string grpName = Group.GetColoredName(prevRank); msg += " %S(Former rank: " + grpName + "%S)"; } Player.Message(p, msg); DisplayTempbanDetails(p, plName); - if (data != null) { - DisplayDetails(p, data, permaBanned ? "Banned" : "Last banned"); + if (banner != null) { + DisplayDetails(p, banner, reason, time, permaBanned ? "Banned" : "Last banned"); } else { Player.Message(p, "No previous bans recorded for {0}%S.", colName); } - data = Ban.GetUnbanData(plName); - DisplayDetails(p, data, permaBanned ? "Last unbanned" : "Unbanned"); + Ban.GetUnbanData(plName, out banner, out reason, out time); + DisplayDetails(p, banner, reason, time, permaBanned ? "Last unbanned" : "Unbanned"); } static void DisplayTempbanDetails(Player p, string target) { @@ -76,13 +78,13 @@ namespace MCGalaxy.Commands.Info { if (reason != "") Player.Message(p, "Reason: {0}", reason); } - static void DisplayDetails(Player p, string[] data, string type) { - if (data == null) return; + static void DisplayDetails(Player p, string banner, string reason, DateTime time, string type) { + if (banner == null) return; - TimeSpan delta = GetDelta(data[2]); + TimeSpan delta = DateTime.UtcNow - time; Player.Message(p, "{0} {1} ago by {2}", - type, delta.Shorten(), GetName(p, data[0])); - Player.Message(p, "Reason: {0}", data[1]); + type, delta.Shorten(), GetName(p, banner)); + Player.Message(p, "Reason: {0}", reason); } static string GetName(Player p, string user) { @@ -91,17 +93,6 @@ namespace MCGalaxy.Commands.Info { return PlayerInfo.GetColoredName(p, user); } - static TimeSpan GetDelta(string data) { - data = data.Replace(",", ""); - string[] date = data.SplitSpaces(); - string[] minuteHour = date[5].Split(':'); - - int hour = int.Parse(minuteHour[0]), minute = int.Parse(minuteHour[1]); - int day = int.Parse(date[1]), month = int.Parse(date[2]), year = int.Parse(date[3]); - DateTime time = new DateTime(year, month, day, hour, minute, 0); - return DateTime.Now - time; - } - public override void Help(Player p) { Player.Message(p, "%T/BanInfo [player]"); Player.Message(p, "%HOutputs information about current and/or previous ban/unban for that player."); diff --git a/MCGalaxy/CorePlugin/ConnectingHandler.cs b/MCGalaxy/CorePlugin/ConnectingHandler.cs index a9ca0b0a5..0eec5700f 100644 --- a/MCGalaxy/CorePlugin/ConnectingHandler.cs +++ b/MCGalaxy/CorePlugin/ConnectingHandler.cs @@ -165,10 +165,14 @@ namespace MCGalaxy.Core { p.Kick(null, ServerConfig.DefaultBanMessage, true); return false; } + if (group.Permission == LevelPermission.Banned) { - string[] data = Ban.GetBanData(p.name); - if (data != null) { - p.Kick(null, Ban.FormatBan(data[0], data[1]), true); + string banner, reason, prevRank; + DateTime time; + Ban.GetBanData(p.name, out banner, out reason, out time, out prevRank); + + if (banner != null) { + p.Kick(null, "Banned by " + banner + ": " + reason, true); } else { p.Kick(null, ServerConfig.DefaultBanMessage, true); } diff --git a/MCGalaxy/Database/Stats/OnlineStat.cs b/MCGalaxy/Database/Stats/OnlineStat.cs index 65a125acd..829db57a4 100644 --- a/MCGalaxy/Database/Stats/OnlineStat.cs +++ b/MCGalaxy/Database/Stats/OnlineStat.cs @@ -89,12 +89,14 @@ namespace MCGalaxy.DB { } internal static void BanLine(Player p, string name) { - if (!Group.BannedRank.Players.Contains(name)) return; + if (!Group.BannedRank.Players.Contains(name)) return; + string banner, reason, prevRank; + DateTime time; + Ban.GetBanData(name, out banner, out reason, out time, out prevRank); - string[] data = Ban.GetBanData(name); - if (data != null) { + if (banner != null) { Player.Message(p, " Banned for {0} by {1}", - data[1], PlayerInfo.GetColoredName(p, data[0])); + reason, PlayerInfo.GetColoredName(p, banner)); } else { Player.Message(p, " Is banned"); } diff --git a/MCGalaxy/Player/Ban.cs b/MCGalaxy/Player/Ban.cs index efae2d799..1203ad0da 100644 --- a/MCGalaxy/Player/Ban.cs +++ b/MCGalaxy/Player/Ban.cs @@ -33,10 +33,6 @@ namespace MCGalaxy { unbans.EnsureExists(); } - public static string FormatBan(string banner, string reason) { - return "Banned by " + banner + ": " + reason; - } - public static string PackTempBanData(string reason, string banner, DateTime expiry) { if (reason == null) reason = "-"; return banner + " " + expiry.ToUnixTime() + " " + reason; @@ -63,7 +59,7 @@ namespace MCGalaxy { reason = reason.Replace(" ", "%20"); string player = banner == null ? "(console)" : banner.truename; - AddBanEntry(player, target.ToLower(), reason, stealth.ToString(), FormatDate(), oldrank); + AddBanEntry(player, target.ToLower(), reason, stealth.ToString(), oldrank); } /// Adds a ban entry for the given user, and who banned them and why they were banned. @@ -72,56 +68,71 @@ namespace MCGalaxy { reason = reason.Replace(" ", "%20"); string player = unbanner == null ? "(console)" : unbanner.truename; - AddUnbanEntry(player, target.ToLower(), reason, FormatDate()); + AddUnbanEntry(player, target.ToLower(), reason); } - static string FormatDate() { - DateTime now = DateTime.Now; - return now.DayOfWeek + "%20" + now.Day + "%20" + now.Month + - "%20" + now.Year + ",%20at%20" + now.Hour + ":" + now.Minute; - } - - static void AddBanEntry(string pl, string who, string reason, - string stealth, string datetime, string oldrank) { + static void AddBanEntry(string pl, string who, string reason, string stealth, string oldrank) { + string datetime = DateTime.UtcNow.ToUnixTime().ToString(); string data = pl + " " + who + " " + reason + " " + stealth + " " + datetime + " " + oldrank; bans.Append(data); } - static void AddUnbanEntry(string pl, string who, string reason, string datetime) { + static void AddUnbanEntry(string pl, string who, string reason) { + string datetime = DateTime.UtcNow.ToUnixTime().ToString(); string data = pl + " " + who + " " + reason + " " + datetime; unbans.Append(data); } - /// Returns info about the current or last ban of a user, as a string array of - /// {banned by, ban reason, date and time, previous rank, stealth}, - /// or null if no ban data was found. - public static string[] GetBanData(string who) { + /// Returns info about the current or last ban of a user. + public static void GetBanData(string who, out string banner, out string reason, + out DateTime time, out string prevRank) { who = who.ToLower(); foreach (string line in File.ReadAllLines(bans.file)) { string[] parts = line.SplitSpaces(); if (parts.Length <= 5 || parts[1] != who) continue; - parts[2] = parts[2].Replace("%20", " "); - parts[4] = parts[4].Replace("%20", " "); - return new string[] { parts[0], parts[2], parts[4], parts[5], parts[3] }; + banner = parts[0]; + reason = parts[2].Replace("%20", " "); + time = GetDate(parts[4]); + prevRank = parts[5]; + return; } - return null; + banner = null; reason = null; time = DateTime.MinValue; prevRank = null; } - /// Returns info about the last unban of a user, as a string array of - /// {banned by, ban reason, date and time}, or null if no unban data was found. - public static string[] GetUnbanData(string who) { + /// Returns information about the last unban of a user. + public static void GetUnbanData(string who, out string unbanner, out string reason, + out DateTime time) { who = who.ToLower(); + unbanner = null; reason = null; foreach (string line in File.ReadAllLines(unbans.file)) { string[] parts = line.SplitSpaces(); if (parts.Length <= 3 || parts[1] != who) continue; - parts[2] = parts[2].Replace("%20", " "); - parts[3] = parts[3].Replace("%20", " "); - return new string[] { parts[0], parts[2], parts[3] }; + unbanner = parts[0]; + reason = parts[2].Replace("%20", " "); + time = GetDate(parts[3]); + return; } - return null; + unbanner = null; reason = null; time = DateTime.MinValue; + } + + static DateTime GetDate(string raw) { + raw = raw.Replace("%20", " ").Replace(",", ""); + long timestap; + if (long.TryParse(raw, out timestap)) return timestap.FromUnixTime(); + + /* Old form of timestamps in bans/unbans: + DateTime now = DateTime.Now; + return now.DayOfWeek + "%20" + now.Day + "%20" + now.Month + "%20" + now.Year + ",%20at%20" + now.Hour + ":" + now.Minute; + */ + string[] date = raw.SplitSpaces(); + string[] minuteHour = date[5].Split(':'); + + int hour = int.Parse(minuteHour[0]), minute = int.Parse(minuteHour[1]); + int day = int.Parse(date[1]), month = int.Parse(date[2]), year = int.Parse(date[3]); + return new DateTime(year, month, day, hour, minute, 0).ToUniversalTime(); }