Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Ban improvements #2428

Draft
wants to merge 3 commits into
base: general-devel
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Initial offline ban mess/design
QuiCM committed Aug 16, 2021
commit b6c339e12616224a401b1d552680e2a4a160c9a0
53 changes: 26 additions & 27 deletions TShockAPI/Commands.cs
Original file line number Diff line number Diff line change
@@ -1328,9 +1328,9 @@ void MoreHelp(string cmd)
args.Player.SendMessage($"- {Highlight("Duration", PinkHighlight)}: uses the format {Highlight("0d0m0s", PinkHighlight)} to determine the length of the ban.", Color.White);
args.Player.SendMessage($" Eg a value of {Highlight("10d30m0s", PinkHighlight)} would represent 10 days, 30 minutes, 0 seconds.", Color.White);
args.Player.SendMessage($" If no duration is provided, the ban will be permanent.", Color.White);
args.Player.SendMessage($"- {Highlight("Flags", GreenHighlight)}: -a (account name), -u (UUID), -n (character name), -ip (IP address), -e (exact, {Highlight("Target", RedHighlight)} will be treated as a raw identifier)", Color.White);
args.Player.SendMessage($"- {Highlight("Flags", GreenHighlight)}: -a (account name), -u (UUID), -n (character name), -ip (IP address)", Color.White);
args.Player.SendMessage($" -e (exact, {Highlight("Target", RedHighlight)} will be treated as a raw identifier)", Color.White);
args.Player.SendMessage($" -o (offline, {Highlight("Target", RedHighlight)} will be treated as a username)", Color.White);
args.Player.SendMessage($" If {Highlight("-e", GreenHighlight)} is used, {Highlight("Target", RedHighlight)} will be treated as a raw identifier. Otherwise it will be used to search for an online player or an offline account.", Color.White);
args.Player.SendMessage($" If no {Highlight("Flags", GreenHighlight)} are specified, the command uses {Highlight("-a -u -ip", GreenHighlight)} by default.", Color.White);
args.Player.SendMessage($"Example usage: {Highlight("ban add", BlueHighlight)} {Highlight(args.Player.Name, RedHighlight)} {Highlight("\"Cheating\"", BlueHighlight)} {Highlight("10d30m0s", PinkHighlight)} {Highlight("-a -u -ip", GreenHighlight)}", Color.White);
break;
@@ -1508,6 +1508,18 @@ void AddBan()
return;
}

if (banOffline)
{
UserAccount account = TShock.UserAccounts.GetUserAccountByName(target);
if (account == null)
{
args.Player.SendErrorMessage("Could not find the account target specified. Check that you have the correct spelling.");
return;
}


}

var players = TSPlayer.FindByNameOrID(target);

if (players.Count > 1)
@@ -1524,35 +1536,22 @@ void AddBan()
}

var player = players[0];
AddBanResult banResult = null;

if (banAccount)
{
if (player.Account != null)
{
banResult = DoBan($"{Identifier.Account}{player.Account.Name}", reason, expiration);
}
}

if (banUuid)
IEnumerable<AddBanResult> banResults = TShock.Bans.DoPlayerBan(player, new BanAction
{
banResult = DoBan($"{Identifier.UUID}{player.UUID}", reason, expiration);
}
BanAccount = banAccount,
BanIp = banIp,
BanName = banName,
BanUuid = banUuid,
ExecutingUser = args.Player,
Expiration = expiration,
Reason = reason
});

if (banName)
{
banResult = DoBan($"{Identifier.Name}{player.Name}", reason, expiration);
}

if (banIp)
{
banResult = DoBan($"{Identifier.IP}{player.IP}", reason, expiration);
}

if (banResult?.Ban != null)
//TODO: group bans better. Display parent ban ID to user
/*if (banResult?.Ban != null)
{
player.Disconnect($"#{banResult.Ban.TicketNumber} - You have been banned: {banResult.Ban.Reason}.");
}
}*/
}

void DelBan()
43 changes: 43 additions & 0 deletions TShockAPI/DB/BanManager.cs
Original file line number Diff line number Diff line change
@@ -259,6 +259,38 @@ internal void BanAddedCheck(object sender, BanPreAddEventArgs args)
}
}

public IEnumerable<AddBanResult> DoPlayerBan(TSPlayer bannedUser, BanAction banAction)
{
if (banAction.BanAccount)
{
if (bannedUser.Account != null)
{
yield return TShock.Bans.InsertBan($"{Identifier.Account}{bannedUser.Account.Name}", banAction.Reason, banAction.ExecutingUser.Account.Name, DateTime.UtcNow, banAction.Expiration);
}
}

if (banAction.BanUuid)
{
yield return TShock.Bans.InsertBan($"{Identifier.UUID}{bannedUser.UUID}", banAction.Reason, banAction.ExecutingUser.Account.Name, DateTime.UtcNow, banAction.Expiration);
}

if (banAction.BanName)
{
yield return TShock.Bans.InsertBan($"{Identifier.Name}{bannedUser.Name}", banAction.Reason, banAction.ExecutingUser.Account.Name, DateTime.UtcNow, banAction.Expiration);
}

if (banAction.BanIp)
{
yield return TShock.Bans.InsertBan($"{Identifier.Name}{bannedUser.IP}", banAction.Reason, banAction.ExecutingUser.Account.Name, DateTime.UtcNow, banAction.Expiration);
}
}

public IEnumerable<AddBanResult> DoAccountBan(UserAccount bannedAccount, BanAction banAction)
{
yield return TShock.Bans.InsertBan($"{Identifier.UUID}{bannedAccount.UUID}", banAction.Reason, banAction.ExecutingUser.Account.Name, DateTime.UtcNow, banAction.Expiration);
yield return TShock.Bans.InsertBan($"{Identifier.Account}{bannedAccount.Name}", banAction.Reason, banAction.ExecutingUser.Account.Name, DateTime.UtcNow, banAction.Expiration);
}

/// <summary>
/// Adds a new ban for the given identifier. Returns a Ban object if the ban was added, else null
/// </summary>
@@ -676,6 +708,17 @@ public static Identifier Register(string prefix, string description)
}
}

public class BanAction
{
public TSPlayer ExecutingUser { get; set; }
public string Reason { get; set; }
public DateTime Expiration { get; set; }
public bool BanAccount { get; set; }
public bool BanUuid { get; set; }
public bool BanName { get; set; }
public bool BanIp { get; set; }
}

/// <summary>
/// Model class that represents a ban entry in the TShock database.
/// </summary>