From 44df804d3d23cc49a223d4b9f871c5a15a399c99 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 5 Mar 2017 11:25:24 +1100 Subject: [PATCH] Make /tempban save, better, show time left in kick message --- MCGalaxy/Commands/Maintenance/CmdServer.cs | 3 +-- MCGalaxy/Commands/Moderation/CmdTempBan.cs | 28 +++++++--------------- MCGalaxy/Commands/Moderation/CmdUnban.cs | 7 +++--- MCGalaxy/CorePlugin/ConnectingHandler.cs | 19 +++++++++++---- MCGalaxy/Player/Ban.cs | 11 +++++++++ MCGalaxy/Server/Server.Fields.cs | 5 +--- MCGalaxy/Server/Server.Init.cs | 1 + 7 files changed, 40 insertions(+), 34 deletions(-) diff --git a/MCGalaxy/Commands/Maintenance/CmdServer.cs b/MCGalaxy/Commands/Maintenance/CmdServer.cs index eb87dd040..ae37133d9 100644 --- a/MCGalaxy/Commands/Maintenance/CmdServer.cs +++ b/MCGalaxy/Commands/Maintenance/CmdServer.cs @@ -163,8 +163,7 @@ namespace MCGalaxy.Commands { void SetToDefault() { foreach (var elem in Server.serverConfig) elem.Field.SetValue(null, elem.Attrib.DefaultValue); - - Server.tempBans = new List(); + Server.ircafkset = new List(); Server.messages = new List(); Server.chatmod = false; diff --git a/MCGalaxy/Commands/Moderation/CmdTempBan.cs b/MCGalaxy/Commands/Moderation/CmdTempBan.cs index 35ad4bcf4..479636240 100644 --- a/MCGalaxy/Commands/Moderation/CmdTempBan.cs +++ b/MCGalaxy/Commands/Moderation/CmdTempBan.cs @@ -43,18 +43,18 @@ namespace MCGalaxy.Commands.Moderation { TimeSpan time = TimeSpan.FromHours(1); if (args.Length > 1 && !args[1].TryParseShort(p, 'm', "temp ban for", out time)) return; - if (time.TotalDays > 1) { Player.Message(p, "Cannot temp ban for more than a day."); return; } if (time.TotalSeconds < 1) { Player.Message(p, "Cannot temp ban someone for less than a second."); return; } - Server.TempBan tBan; - tBan.name = target; - tBan.reason = ModActionCmd.ExpandReason(p, reason); - if (tBan.reason == null) return; + reason = ModActionCmd.ExpandReason(p, reason); + if (reason == null) return; + string banner = p == null ? "(console)" : p.truename; - tBan.expiryTime = DateTime.UtcNow.Add(time); - AddTempban(tBan); + Server.tempBans.AddOrReplace(target, + Ban.PackTempBanData(reason, banner, DateTime.UtcNow.Add(time))); + Server.tempBans.Save(); + if (who != null) { - string kickReason = tBan.reason == "" ? "" : " - (" + tBan.reason + ")"; + string kickReason = reason == "" ? "" : " - (" + reason + ")"; who.Kick("Banned for " + time.Shorten(true) + "." + kickReason); } @@ -63,20 +63,10 @@ namespace MCGalaxy.Commands.Moderation { else Player.AddNote(target, p, "T", reason); } - void AddTempban(Server.TempBan tBan) { - for (int i = 0; i < Server.tempBans.Count; i++) { - if (!Server.tempBans[i].name.CaselessEq(tBan.name)) continue; - Server.tempBans[i] = tBan; - return; - } - Server.tempBans.Add(tBan); - } - public override void Help(Player p) { Player.Message(p, "%T/tempban [name] [timespan] "); - Player.Message(p, "%HBans [name] for [timespan]. Max is 1 day, default is 1 hour."); + Player.Message(p, "%HBans [name] for [timespan]. Default is 1 hour."); Player.Message(p, "%H e.g. to tempban for 90 minutes, [timespan] would be %S1h30m"); - Player.Message(p, "%HTemp bans will reset on server restart"); Player.Message(p, "%HFor , @number can be used as a shortcut for that rule."); } } diff --git a/MCGalaxy/Commands/Moderation/CmdUnban.cs b/MCGalaxy/Commands/Moderation/CmdUnban.cs index c3b6bae56..da65ac78b 100644 --- a/MCGalaxy/Commands/Moderation/CmdUnban.cs +++ b/MCGalaxy/Commands/Moderation/CmdUnban.cs @@ -37,10 +37,9 @@ namespace MCGalaxy.Commands.Moderation { Group banned = Group.BannedRank; // Check tempbans first - foreach (Server.TempBan tban in Server.tempBans) { - if (!tban.name.CaselessEq(name)) continue; - - Server.tempBans.Remove(tban); + if (Server.tempBans.Remove(name)) { + Server.tempBans.Save(); + Chat.MessageAll("{0} had their temporary ban lifted by {1}.", name, srcFull); Server.s.Log("UNBANNED: " + name + " by " + src); Server.IRC.Say(name + " was unbanned by " + src + "."); diff --git a/MCGalaxy/CorePlugin/ConnectingHandler.cs b/MCGalaxy/CorePlugin/ConnectingHandler.cs index d2bcef89a..ee2b989e0 100644 --- a/MCGalaxy/CorePlugin/ConnectingHandler.cs +++ b/MCGalaxy/CorePlugin/ConnectingHandler.cs @@ -93,12 +93,21 @@ namespace MCGalaxy.Core { static bool CheckTempban(Player p) { try { - Server.TempBan tBan = Server.tempBans.Find(tB => tB.name.CaselessEnds(p.name)); - if (tBan.expiryTime < DateTime.UtcNow) { - Server.tempBans.Remove(tBan); + string data = Server.tempBans.FindData(p.name); + if (data == null) return true; + + string banner, reason; + DateTime expiry; + Ban.UnpackTempBanData(data, out reason, out banner, out expiry); + + if (expiry < DateTime.UtcNow) { + Server.tempBans.Remove(p.name); + Server.tempBans.Save(); } else { - string reason = String.IsNullOrEmpty(tBan.reason) ? "" :" (" + tBan.reason + ")"; - p.Kick(null, "You're still temp banned!" + reason, true); + reason = reason == "" ? "" :" (" + reason + ")"; + string delta = (expiry - DateTime.UtcNow).Shorten(true); + + p.Kick(null, "Banned by " + banner + " for another " + delta + reason, true); return false; } } catch { } diff --git a/MCGalaxy/Player/Ban.cs b/MCGalaxy/Player/Ban.cs index d2c7511af..a4549062b 100644 --- a/MCGalaxy/Player/Ban.cs +++ b/MCGalaxy/Player/Ban.cs @@ -35,6 +35,17 @@ namespace MCGalaxy { public static string FormatBan(string banner, string reason) { return "Banned by " + banner + ": " + reason; + } + + public static string PackTempBanData(string reason, string banner, DateTime expiry) { + return banner + " " + expiry.Ticks + " " + reason; + } + + public static void UnpackTempBanData(string line, out string reason, out string banner, out DateTime expiry) { + string[] parts = line.SplitSpaces(3); + banner = parts[0]; + expiry = new DateTime(long.Parse(parts[1]), DateTimeKind.Utc); + reason = parts.Length > 2 ? parts[2] : ""; } diff --git a/MCGalaxy/Server/Server.Fields.cs b/MCGalaxy/Server/Server.Fields.cs index a90ca2978..0beb42b24 100644 --- a/MCGalaxy/Server/Server.Fields.cs +++ b/MCGalaxy/Server/Server.Fields.cs @@ -93,7 +93,7 @@ namespace MCGalaxy { public static CTFGame ctf = null; public static PlayerList bannedIP, whiteList, ircControllers, muted, invalidIds; public static PlayerList ignored, frozen, hidden, agreed, vip, noEmotes, lockdown; - public static PlayerExtList jailed, models, skins, reach; + public static PlayerExtList jailed, models, skins, reach, tempBans; public static readonly List Devs = new List(), Mods = new List(); @@ -103,9 +103,6 @@ namespace MCGalaxy { ); public static List Opstats { get { return opstats; } } - public static List tempBans = new List(); - public struct TempBan { public string name, reason; public DateTime expiryTime; } - public static PerformanceCounter PCCounter = null; public static PerformanceCounter ProcessCounter = null; diff --git a/MCGalaxy/Server/Server.Init.cs b/MCGalaxy/Server/Server.Init.cs index c67457f20..b38225c87 100644 --- a/MCGalaxy/Server/Server.Init.cs +++ b/MCGalaxy/Server/Server.Init.cs @@ -71,6 +71,7 @@ namespace MCGalaxy { skins = PlayerExtList.Load("extra/skins.txt"); reach = PlayerExtList.Load("extra/reach.txt"); invalidIds = PlayerList.Load("extra/invalidids.txt"); + tempBans = PlayerExtList.Load("text/tempbans.txt"); if (useWhitelist) whiteList = PlayerList.Load("whitelist.txt");