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
This commit is contained in:
Zack Piispanen 2013-09-22 16:04:12 -04:00
parent 222042cc4d
commit 856def83b3
6 changed files with 75 additions and 22 deletions

View file

@ -562,7 +562,7 @@ namespace TShockAPI
user.Group = TShock.Config.DefaultRegistrationGroupName; // FIXME -- we should get this from the DB. --Why? 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("Account " + user.Name + " has been registered.");
args.Player.SendSuccessMessage("Your password is " + user.Password); args.Player.SendSuccessMessage("Your password is " + user.Password);
@ -979,7 +979,7 @@ namespace TShockAPI
string reason = args.Parameters.Count > 2 string reason = args.Parameters.Count > 2
? String.Join(" ", args.Parameters.GetRange(2, args.Parameters.Count - 2)) ? String.Join(" ", args.Parameters.GetRange(2, args.Parameters.Count - 2))
: "Misbehavior."; : "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!"); args.Player.SendErrorMessage("You can't ban another admin!");
} }
@ -994,7 +994,7 @@ namespace TShockAPI
string reason = args.Parameters.Count > 2 string reason = args.Parameters.Count > 2
? String.Join(" ", args.Parameters.GetRange(2, args.Parameters.Count - 2)) ? String.Join(" ", args.Parameters.GetRange(2, args.Parameters.Count - 2))
: "Manually added IP address ban."; : "Manually added IP address ban.";
TShock.Bans.AddBan(ip, "", reason); TShock.Bans.AddBan(ip, "", reason, false, args.Player.UserAccountName);
args.Player.SendSuccessMessage(ip + " banned."); args.Player.SendSuccessMessage(ip + " banned.");
return; return;
#endregion Add ip ban #endregion Add ip ban

View file

@ -34,7 +34,10 @@ namespace TShockAPI.DB
var table = new SqlTable("Bans", var table = new SqlTable("Bans",
new SqlColumn("IP", MySqlDbType.String, 16) {Primary = true}, new SqlColumn("IP", MySqlDbType.String, 16) {Primary = true},
new SqlColumn("Name", MySqlDbType.Text), 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, var creator = new SqlTableCreator(db,
db.GetSqlType() == SqlType.Sqlite db.GetSqlType() == SqlType.Sqlite
@ -58,7 +61,7 @@ namespace TShockAPI.DB
using (var reader = database.QueryReader("SELECT * FROM Bans WHERE IP=@0", ip)) using (var reader = database.QueryReader("SELECT * FROM Bans WHERE IP=@0", ip))
{ {
if (reader.Read()) if (reader.Read())
return new Ban(reader.Get<string>("IP"), reader.Get<string>("Name"), reader.Get<string>("Reason")); return new Ban(reader.Get<string>("IP"), reader.Get<string>("Name"), reader.Get<string>("Reason"), reader.Get<string>("BanningUser"), reader.Get<string>("Date"), reader.Get<string>("Expiration"));
} }
} }
catch (Exception ex) catch (Exception ex)
@ -77,7 +80,7 @@ namespace TShockAPI.DB
{ {
while (reader.Read()) while (reader.Read())
{ {
banlist.Add(new Ban(reader.Get<string>("IP"), reader.Get<string>("Name"), reader.Get<string>("Reason"))); banlist.Add(new Ban(reader.Get<string>("IP"), reader.Get<string>("Name"), reader.Get<string>("Reason"), reader.Get<string>("BanningUser"), reader.Get<string>("Date"), reader.Get<string>("Expiration")));
} }
return banlist; return banlist;
} }
@ -100,7 +103,7 @@ namespace TShockAPI.DB
using (var reader = database.QueryReader("SELECT * FROM Bans WHERE " + namecol + "=@0", name)) using (var reader = database.QueryReader("SELECT * FROM Bans WHERE " + namecol + "=@0", name))
{ {
if (reader.Read()) if (reader.Read())
return new Ban(reader.Get<string>("IP"), reader.Get<string>("Name"), reader.Get<string>("Reason")); return new Ban(reader.Get<string>("IP"), reader.Get<string>("Name"), reader.Get<string>("Reason"), reader.Get<string>("BanningUser"), reader.Get<string>("Date"), reader.Get<string>("Expiration"));
} }
} }
catch (Exception ex) catch (Exception ex)
@ -114,14 +117,14 @@ namespace TShockAPI.DB
[Obsolete("This method is for signature compatibility for external code only")] [Obsolete("This method is for signature compatibility for external code only")]
public bool AddBan(string ip, string name, string reason) public bool AddBan(string ip, string name, string reason)
{ {
return AddBan(ip, name, reason, false); return AddBan(ip, name, reason, false, "", "");
} }
#endif #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 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) catch (Exception ex)
{ {
@ -180,11 +183,20 @@ namespace TShockAPI.DB
public string Reason { get; set; } 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; IP = ip;
Name = name; Name = name;
Reason = reason; Reason = reason;
BanningUser = banner;
Date = date;
Expiration = exp;
} }
public Ban() public Ban()
@ -192,6 +204,9 @@ namespace TShockAPI.DB
IP = string.Empty; IP = string.Empty;
Name = string.Empty; Name = string.Empty;
Reason = string.Empty; Reason = string.Empty;
BanningUser = "";
Date = "";
Expiration = "";
} }
} }
} }

View file

@ -1309,8 +1309,13 @@ namespace TShockAPI
var ban = TShock.Bans.GetBanByName(name); var ban = TShock.Bans.GetBanByName(name);
if (ban != null) if (ban != null)
{ {
TShock.Utils.ForceKick(args.Player, string.Format("You are banned: {0}", ban.Reason), true); if (!TShock.Utils.HasBanExpired(ban, true))
return 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) if (args.Player.ReceivedInfo)
{ {
@ -2446,7 +2451,7 @@ namespace TShockAPI
{ {
if (TShock.Config.BanOnMediumcoreDeath) 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); TShock.Utils.ForceKick(args.Player, "Death results in a ban, but can't ban you.", true);
} }
else else

View file

@ -404,7 +404,7 @@ namespace TShockAPI
try try
{ {
TShock.Bans.AddBan(ip, name, parameters["reason"], true); TShock.Bans.AddBan(ip, name, parameters["reason"], true, tokenData.Username);
} }
catch (Exception e) catch (Exception e)
{ {

View file

@ -785,9 +785,14 @@ namespace TShockAPI
if (ban != null) if (ban != null)
{ {
Utils.ForceKick(player, string.Format("You are banned: {0}", ban.Reason), true, false); if (!Utils.HasBanExpired(ban))
handler.Handled = true; {
return; 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)) if (!FileTools.OnWhitelist(player.IP))
@ -838,9 +843,13 @@ namespace TShockAPI
if (ban != null) if (ban != null)
{ {
Utils.ForceKick(player, string.Format("You are banned: {0}", ban.Reason), true, false); if (!Utils.HasBanExpired(ban))
handler.Handled = true; {
return; 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;
}
} }
} }

View file

@ -25,6 +25,7 @@ using System.Net.Sockets;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
using Terraria; using Terraria;
using TShockAPI.DB;
namespace TShockAPI namespace TShockAPI
{ {
@ -673,7 +674,7 @@ namespace TShockAPI
{ {
string ip = player.IP; string ip = player.IP;
string playerName = player.Name; 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)); player.Disconnect(string.Format("Banned: {0}", reason));
Log.ConsoleInfo(string.Format("Banned {0} for : {1}", playerName, reason)); Log.ConsoleInfo(string.Format("Banned {0} for : {1}", playerName, reason));
string verb = force ? "force " : ""; string verb = force ? "force " : "";
@ -686,6 +687,29 @@ namespace TShockAPI
return false; 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;
}
/// <summary> /// <summary>
/// Shows a file to the user. /// Shows a file to the user.
/// </summary> /// </summary>