From 856def83b3ba968f2f77a0ebfc272008f43f7a6e Mon Sep 17 00:00:00 2001 From: Zack Piispanen Date: Sun, 22 Sep 2013 16:04:12 -0400 Subject: [PATCH] Added basic framework for allowing for ban durations and banning admin. Added BanningUser, Date(of ban), and Expiration to the db On join, Expiration is checked, if ban is expired we remove it and the user is allowed on. When running the ban command, your UAN is used as the banning user, and the date is that moment, with no expiration. #531 --- TShockAPI/Commands.cs | 6 +++--- TShockAPI/DB/BanManager.cs | 31 +++++++++++++++++++++++-------- TShockAPI/GetDataHandlers.cs | 11 ++++++++--- TShockAPI/Rest/RestManager.cs | 2 +- TShockAPI/TShock.cs | 21 +++++++++++++++------ TShockAPI/Utils.cs | 26 +++++++++++++++++++++++++- 6 files changed, 75 insertions(+), 22 deletions(-) diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index f43aae1f..82705e92 100644 --- a/TShockAPI/Commands.cs +++ b/TShockAPI/Commands.cs @@ -562,7 +562,7 @@ namespace TShockAPI user.Group = TShock.Config.DefaultRegistrationGroupName; // FIXME -- we should get this from the DB. --Why? - if (TShock.Users.GetUserByName(user.Name) == null) // Cheap way of checking for existance of a user + if (TShock.Users.GetUserByName(user.Name) == null && user.Name != TSServerPlayer.AccountName) // Cheap way of checking for existance of a user { args.Player.SendSuccessMessage("Account " + user.Name + " has been registered."); args.Player.SendSuccessMessage("Your password is " + user.Password); @@ -979,7 +979,7 @@ namespace TShockAPI string reason = args.Parameters.Count > 2 ? String.Join(" ", args.Parameters.GetRange(2, args.Parameters.Count - 2)) : "Misbehavior."; - if (!TShock.Utils.Ban(players[0], reason, !args.Player.RealPlayer, args.Player.Name)) + if (!TShock.Utils.Ban(players[0], reason, !args.Player.RealPlayer, args.Player.UserAccountName)) { args.Player.SendErrorMessage("You can't ban another admin!"); } @@ -994,7 +994,7 @@ namespace TShockAPI string reason = args.Parameters.Count > 2 ? String.Join(" ", args.Parameters.GetRange(2, args.Parameters.Count - 2)) : "Manually added IP address ban."; - TShock.Bans.AddBan(ip, "", reason); + TShock.Bans.AddBan(ip, "", reason, false, args.Player.UserAccountName); args.Player.SendSuccessMessage(ip + " banned."); return; #endregion Add ip ban diff --git a/TShockAPI/DB/BanManager.cs b/TShockAPI/DB/BanManager.cs index ddcd4d8e..c2fef99e 100644 --- a/TShockAPI/DB/BanManager.cs +++ b/TShockAPI/DB/BanManager.cs @@ -34,7 +34,10 @@ namespace TShockAPI.DB var table = new SqlTable("Bans", new SqlColumn("IP", MySqlDbType.String, 16) {Primary = true}, new SqlColumn("Name", MySqlDbType.Text), - new SqlColumn("Reason", MySqlDbType.Text) + new SqlColumn("Reason", MySqlDbType.Text), + new SqlColumn("BanningUser", MySqlDbType.Text), + new SqlColumn("Date", MySqlDbType.Text), + new SqlColumn("Expiration", MySqlDbType.Text) ); var creator = new SqlTableCreator(db, db.GetSqlType() == SqlType.Sqlite @@ -58,7 +61,7 @@ namespace TShockAPI.DB using (var reader = database.QueryReader("SELECT * FROM Bans WHERE IP=@0", ip)) { if (reader.Read()) - return new Ban(reader.Get("IP"), reader.Get("Name"), reader.Get("Reason")); + return new Ban(reader.Get("IP"), reader.Get("Name"), reader.Get("Reason"), reader.Get("BanningUser"), reader.Get("Date"), reader.Get("Expiration")); } } catch (Exception ex) @@ -77,7 +80,7 @@ namespace TShockAPI.DB { while (reader.Read()) { - banlist.Add(new Ban(reader.Get("IP"), reader.Get("Name"), reader.Get("Reason"))); + banlist.Add(new Ban(reader.Get("IP"), reader.Get("Name"), reader.Get("Reason"), reader.Get("BanningUser"), reader.Get("Date"), reader.Get("Expiration"))); } return banlist; } @@ -100,7 +103,7 @@ namespace TShockAPI.DB using (var reader = database.QueryReader("SELECT * FROM Bans WHERE " + namecol + "=@0", name)) { if (reader.Read()) - return new Ban(reader.Get("IP"), reader.Get("Name"), reader.Get("Reason")); + return new Ban(reader.Get("IP"), reader.Get("Name"), reader.Get("Reason"), reader.Get("BanningUser"), reader.Get("Date"), reader.Get("Expiration")); } } catch (Exception ex) @@ -114,14 +117,14 @@ namespace TShockAPI.DB [Obsolete("This method is for signature compatibility for external code only")] public bool AddBan(string ip, string name, string reason) { - return AddBan(ip, name, reason, false); + return AddBan(ip, name, reason, false, "", ""); } #endif - public bool AddBan(string ip, string name = "", string reason = "", bool exceptions = false) + public bool AddBan(string ip, string name = "", string reason = "", bool exceptions = false, string banner = "", string expiration = "") { try { - return database.Query("INSERT INTO Bans (IP, Name, Reason) VALUES (@0, @1, @2);", ip, name, reason) != 0; + return database.Query("INSERT INTO Bans (IP, Name, Reason, BanningUser, Date, Expiration) VALUES (@0, @1, @2, @3, @4, @5);", ip, name, reason, banner, DateTime.Now.ToString("G"), expiration) != 0; } catch (Exception ex) { @@ -180,11 +183,20 @@ namespace TShockAPI.DB public string Reason { get; set; } - public Ban(string ip, string name, string reason) + public string BanningUser { get; set; } + + public string Date { get; set; } + + public string Expiration { get; set; } + + public Ban(string ip, string name, string reason, string banner, string date, string exp) { IP = ip; Name = name; Reason = reason; + BanningUser = banner; + Date = date; + Expiration = exp; } public Ban() @@ -192,6 +204,9 @@ namespace TShockAPI.DB IP = string.Empty; Name = string.Empty; Reason = string.Empty; + BanningUser = ""; + Date = ""; + Expiration = ""; } } } diff --git a/TShockAPI/GetDataHandlers.cs b/TShockAPI/GetDataHandlers.cs index 10a8e85b..906e56ff 100644 --- a/TShockAPI/GetDataHandlers.cs +++ b/TShockAPI/GetDataHandlers.cs @@ -1309,8 +1309,13 @@ namespace TShockAPI var ban = TShock.Bans.GetBanByName(name); if (ban != null) { - TShock.Utils.ForceKick(args.Player, string.Format("You are banned: {0}", ban.Reason), true); - return true; + if (!TShock.Utils.HasBanExpired(ban, true)) + { + DateTime exp; + string duration = DateTime.TryParse(ban.Expiration, out exp) ? String.Format("until {0}", exp.ToString("G")) : "forever"; + TShock.Utils.ForceKick(args.Player, string.Format("You are banned {0}: {1}", duration, ban.Reason), true, false); + return true; + } } if (args.Player.ReceivedInfo) { @@ -2446,7 +2451,7 @@ namespace TShockAPI { if (TShock.Config.BanOnMediumcoreDeath) { - if (!TShock.Utils.Ban(args.Player, TShock.Config.MediumcoreBanReason)) + if (!TShock.Utils.Ban(args.Player, TShock.Config.MediumcoreBanReason, false, "mediumcore-death")) TShock.Utils.ForceKick(args.Player, "Death results in a ban, but can't ban you.", true); } else diff --git a/TShockAPI/Rest/RestManager.cs b/TShockAPI/Rest/RestManager.cs index 1c170a88..8d872fe2 100644 --- a/TShockAPI/Rest/RestManager.cs +++ b/TShockAPI/Rest/RestManager.cs @@ -404,7 +404,7 @@ namespace TShockAPI try { - TShock.Bans.AddBan(ip, name, parameters["reason"], true); + TShock.Bans.AddBan(ip, name, parameters["reason"], true, tokenData.Username); } catch (Exception e) { diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index 1b15f63e..559fadf1 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -785,9 +785,14 @@ namespace TShockAPI if (ban != null) { - Utils.ForceKick(player, string.Format("You are banned: {0}", ban.Reason), true, false); - handler.Handled = true; - return; + if (!Utils.HasBanExpired(ban)) + { + DateTime exp; + string duration = DateTime.TryParse(ban.Expiration, out exp) ? String.Format("until {0}", exp.ToString("G")) : "forever"; + Utils.ForceKick(player, string.Format("You are banned {0}: {1}", duration, ban.Reason), true, false); + handler.Handled = true; + return; + } } if (!FileTools.OnWhitelist(player.IP)) @@ -838,9 +843,13 @@ namespace TShockAPI if (ban != null) { - Utils.ForceKick(player, string.Format("You are banned: {0}", ban.Reason), true, false); - handler.Handled = true; - return; + if (!Utils.HasBanExpired(ban)) + { + DateTime exp; + string duration = DateTime.TryParse(ban.Expiration, out exp) ? String.Format("until {0}", exp.ToString("G")) : "forever"; + Utils.ForceKick(player, string.Format("You are banned {0}: {1}", duration, ban.Reason), true, false); + handler.Handled = true; + } } } diff --git a/TShockAPI/Utils.cs b/TShockAPI/Utils.cs index 4d6cc106..4b485771 100644 --- a/TShockAPI/Utils.cs +++ b/TShockAPI/Utils.cs @@ -25,6 +25,7 @@ using System.Net.Sockets; using System.Security.Cryptography; using System.Text; using Terraria; +using TShockAPI.DB; namespace TShockAPI { @@ -673,7 +674,7 @@ namespace TShockAPI { string ip = player.IP; string playerName = player.Name; - TShock.Bans.AddBan(ip, playerName, reason); + TShock.Bans.AddBan(ip, playerName, reason, false, adminUserName); player.Disconnect(string.Format("Banned: {0}", reason)); Log.ConsoleInfo(string.Format("Banned {0} for : {1}", playerName, reason)); string verb = force ? "force " : ""; @@ -686,6 +687,29 @@ namespace TShockAPI return false; } + public bool HasBanExpired(Ban ban, bool byName = false) + { + DateTime exp; + bool expirationExists = DateTime.TryParse(ban.Expiration, out exp); + + if (!string.IsNullOrWhiteSpace(ban.Expiration) && (expirationExists) && + (DateTime.Now >= exp)) + { + if (byName) + { + TShock.Bans.RemoveBan(ban.Name, true, true, false); + } + else + { + TShock.Bans.RemoveBan(ban.IP, false, false, false); + } + + return true; + } + + return false; + } + /// /// Shows a file to the user. ///