diff --git a/TShockAPI/DB/BanManager.cs b/TShockAPI/DB/BanManager.cs index 8bfe9d86..8fa40375 100644 --- a/TShockAPI/DB/BanManager.cs +++ b/TShockAPI/DB/BanManager.cs @@ -43,6 +43,7 @@ namespace TShockAPI.DB new SqlColumn("IP", MySqlDbType.String, 16) { Primary = true }, new SqlColumn("Name", MySqlDbType.Text), new SqlColumn("UUID", MySqlDbType.Text), + new SqlColumn("AccountName", MySqlDbType.Text), new SqlColumn("Reason", MySqlDbType.Text), new SqlColumn("BanningUser", MySqlDbType.Text), new SqlColumn("Date", MySqlDbType.Text), @@ -75,7 +76,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("UUID"), reader.Get("Reason"), reader.Get("BanningUser"), reader.Get("Date"), reader.Get("Expiration")); + return new Ban(reader.Get("IP"), reader.Get("Name"), reader.Get("UUID"), reader.Get("AccountName"), reader.Get("Reason"), reader.Get("BanningUser"), reader.Get("Date"), reader.Get("Expiration")); } } catch (Exception ex) @@ -104,7 +105,7 @@ namespace TShockAPI.DB { while (reader.Read()) { - banlist.Add(new Ban(reader.Get("IP"), reader.Get("Name"), reader.Get("UUID"), reader.Get("Reason"), reader.Get("BanningUser"), reader.Get("Date"), reader.Get("Expiration"))); + banlist.Add(new Ban(reader.Get("IP"), reader.Get("Name"), reader.Get("UUID"), reader.Get("AccountName"), reader.Get("Reason"), reader.Get("BanningUser"), reader.Get("Date"), reader.Get("Expiration"))); } banlist.Sort(new BanComparer(sortMethod)); @@ -135,7 +136,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("UUID"), reader.Get("Reason"), reader.Get("BanningUser"), reader.Get("Date"), reader.Get("Expiration")); + return new Ban(reader.Get("IP"), reader.Get("Name"), reader.Get("UUID"), reader.Get("AccountName"), reader.Get("Reason"), reader.Get("BanningUser"), reader.Get("Date"), reader.Get("Expiration")); } } catch (Exception ex) @@ -157,7 +158,7 @@ namespace TShockAPI.DB using (var reader = database.QueryReader("SELECT * FROM Bans WHERE UUID=@0", uuid)) { if (reader.Read()) - return new Ban(reader.Get("IP"), reader.Get("Name"), reader.Get("UUID"), reader.Get("Reason"), reader.Get("BanningUser"), reader.Get("Date"), reader.Get("Expiration")); + return new Ban(reader.Get("IP"), reader.Get("Name"), reader.Get("UUID"), reader.Get("AccountName"), reader.Get("Reason"), reader.Get("BanningUser"), reader.Get("Date"), reader.Get("Expiration")); } } catch (Exception ex) @@ -179,6 +180,22 @@ namespace TShockAPI.DB /// Banner. /// Expiration date. public bool AddBan(string ip, string name = "", string uuid = "", string reason = "", bool exceptions = false, string banner = "", string expiration = "") + { + return AddBan2(ip, name, uuid, "", reason, exceptions, banner, expiration); + } + + /// + /// Adds a ban. + /// + /// true, if ban was added, false otherwise. + /// Ip. + /// Name. + /// UUID. + /// Reason. + /// If set to true enable throwing exceptions. + /// Banner. + /// Expiration date. + public bool AddBan2(string ip, string name = "", string uuid = "", string accountName = "", string reason = "", bool exceptions = false, string banner = "", string expiration = "") { try { @@ -192,7 +209,7 @@ namespace TShockAPI.DB } else { - return database.Query("INSERT INTO Bans (IP, Name, UUID, Reason, BanningUser, Date, Expiration) VALUES (@0, @1, @2, @3, @4, @5, @6);", ip, name, uuid, reason, banner, DateTime.UtcNow.ToString("s"), expiration) != 0; + return database.Query("INSERT INTO Bans (IP, Name, UUID, AccountName, Reason, BanningUser, Date, Expiration) VALUES (@0, @1, @2, @3, @4, @5, @6, @7);", ip, name, uuid, accountName, reason, banner, DateTime.UtcNow.ToString("s"), expiration) != 0; } } catch (Exception ex) @@ -231,6 +248,26 @@ namespace TShockAPI.DB return false; } + /// + /// Removes a ban by account name (not character/player name). + /// + /// true, if ban was removed, false otherwise. + /// Match. + /// If set to true casesensitive. + public bool RemoveBanByAccountName(string match, bool casesensitive = true) + { + try + { + var namecol = casesensitive ? "AccountName" : "UPPER(AccountName)"; + return database.Query("DELETE FROM Bans WHERE " + namecol + "=@0", casesensitive ? match : match.ToUpper()) != 0; + } + catch (Exception ex) + { + TShock.Log.Error(ex.ToString()); + } + return false; + } + /// /// Clears bans. /// @@ -380,6 +417,12 @@ namespace TShockAPI.DB /// The UUID public string UUID { get; set; } + /// + /// Gets or sets the account name of the ban + /// + /// The account name + public String AccountName { get; set; } + /// /// Gets or sets the ban reason. /// @@ -424,11 +467,12 @@ namespace TShockAPI.DB /// Banner. /// UTC ban date. /// Expiration time - public Ban(string ip, string name, string uuid, string reason, string banner, string date, string exp) + public Ban(string ip, string name, string uuid, string accountName, string reason, string banner, string date, string exp) { IP = ip; Name = name; UUID = uuid; + AccountName = accountName; Reason = reason; BanningUser = banner; Date = date; @@ -454,10 +498,11 @@ namespace TShockAPI.DB IP = string.Empty; Name = string.Empty; UUID = string.Empty; + AccountName = string.Empty; Reason = string.Empty; - BanningUser = ""; - Date = ""; - Expiration = ""; + BanningUser = string.Empty; + Date = string.Empty; + Expiration = string.Empty; } } }