From 9dee0aee7d6b1a401c8855d60035cc8fc01615d9 Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Fri, 1 Dec 2017 23:12:06 -0700 Subject: [PATCH 01/11] Rewrote the /ban add sub command /ban add now replaces add, addtemp, and addip. New syntax: /ban add [time] [reason] Examples: - /ban add Shank 10d Rewrote the ban system. - /ban add Ash - /ban add 127.0.0.1 5d Go work on homework. Note that if you want to specify a reason and a permaban, you need to use 0 (zero) as the duration. Examples: - /ban add Ash 0 Love ya. - /ban add Shank 0 Hacking. Closes #1510 --- TShockAPI/Commands.cs | 341 +++++++++++++++++++++--------------------- 1 file changed, 171 insertions(+), 170 deletions(-) diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index 493aa956..87559392 100644 --- a/TShockAPI/Commands.cs +++ b/TShockAPI/Commands.cs @@ -35,6 +35,7 @@ using Terraria.GameContent.Events; using Microsoft.Xna.Framework; using OTAPI.Tile; using TShockAPI.Localization; +using System.Text.RegularExpressions; namespace TShockAPI { @@ -1273,200 +1274,202 @@ namespace TShockAPI switch (subcmd) { case "add": - #region Add ban + #region Add Ban { if (args.Parameters.Count < 2) { - args.Player.SendErrorMessage("Invalid syntax! Proper syntax: {0}ban add [reason]", Specifier); + args.Player.SendErrorMessage("Invalid command. Format: {0}ban add [time] [reason]", Specifier); + args.Player.SendErrorMessage("Example: {0}ban add Shank 10d Hacking and cheating", Specifier); + args.Player.SendErrorMessage("Example: {0}ban add Ash", Specifier); + args.Player.SendErrorMessage("Use the time 0 (zero) for a permanent ban."); return; } + // Used only to notify if a ban was successful and who the ban was about + bool success = false; + string targetGeneralizedName = ""; + + // What if caller is console? Name? + + // Effective ban target assignment List players = TShock.Utils.FindPlayer(args.Parameters[1]); - string reason = args.Parameters.Count > 2 ? String.Join(" ", args.Parameters.Skip(2)) : "Misbehavior."; - if (players.Count == 0) + User offlineUser = TShock.Users.GetUserByName(args.Parameters[1]); + + // Determines if the caller is effective root for overriding permissions + bool callerIsEffectiveRoot = false; + + // A ban source is effective root if they are not not real and not rest + // Super admins ingame who attempt to run this will have to remove + // the immune to ban permission if they want to ban an immune player + if (!(args.Player is TSRestPlayer) && !args.Player.RealPlayer) { - var user = TShock.Users.GetUserByName(args.Parameters[1]); - if (user != null) - { - bool force = !args.Player.RealPlayer; - - if (user.Name == args.Player.Name && !force) - { - args.Player.SendErrorMessage("You can't ban yourself!"); - return; - } - - if (TShock.Groups.GetGroupByName(user.Group).HasPermission(Permissions.immunetoban) && !force) - args.Player.SendErrorMessage("You can't ban {0}!", user.Name); - else - { - if (user.KnownIps == null) - { - args.Player.SendErrorMessage("Cannot ban {0} because they have no IPs to ban.", user.Name); - return; - } - var knownIps = JsonConvert.DeserializeObject>(user.KnownIps); - TShock.Bans.AddBan(knownIps.Last(), user.Name, user.UUID, reason, false, args.Player.User.Name); - if (String.IsNullOrWhiteSpace(args.Player.User.Name)) - { - if (args.Silent) - { - args.Player.SendInfoMessage("{0} was {1}banned for '{2}'.", user.Name, force ? "Force " : "", reason); - } - else - { - TSPlayer.All.SendInfoMessage("{0} was {1}banned for '{2}'.", user.Name, force ? "Force " : "", reason); - } - } - else - { - if (args.Silent) - { - args.Player.SendInfoMessage("{0}banned {1} for '{2}'.", force ? "Force " : "", user.Name, reason); - } - else - { - TSPlayer.All.SendInfoMessage("{0} {1}banned {2} for '{3}'.", args.Player.Name, force ? "Force " : "", user.Name, reason); - } - } - } - } - else - args.Player.SendErrorMessage("Invalid player or account!"); + callerIsEffectiveRoot = true; } - else if (players.Count > 1) + + // The ban reason the ban is going to have + string banReason = "Unknown."; + + // The default ban length + // 0 is permanent ban, otherwise temp ban + int banLengthInSeconds = 0; + + // Figure out if param 2 is a time or 0 or garbage + if (args.Parameters.Count >= 3) + { + bool parsedOkay = false; + if (!(args.Parameters[2] == "0")) + { + parsedOkay = TShock.Utils.TryParseTime(args.Parameters[2], out banLengthInSeconds); + } else { + parsedOkay = true; + } + + if (!parsedOkay) + { + args.Player.SendErrorMessage("Invalid time format. Example: 10d+5h+3m-2s."); + args.Player.SendErrorMessage("Use 0 (zero) for a permanent ban."); + return; + } + } + + // If a reason exists, use the given reason. + if (args.Parameters.Count > 3) + { + banReason = String.Join(" ", args.Parameters.Skip(3)); + } + + // Bad case: Players contains more than 1 person so we can't ban them + if (players.Count > 1) + { TShock.Utils.SendMultipleMatchError(args.Player, players.Select(p => p.Name)); - else - { - if (!TShock.Utils.Ban(players[0], reason, !args.Player.RealPlayer, args.Player.User.Name)) - args.Player.SendErrorMessage("You can't ban {0}!", players[0].Name); - } - } - #endregion - return; - case "addip": - #region Add IP ban - { - if (args.Parameters.Count < 2) - { - args.Player.SendErrorMessage("Invalid syntax! Proper syntax: {0}ban addip [reason]", Specifier); return; } - string ip = args.Parameters[1]; - 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, false, args.Player.User.Name); - args.Player.SendSuccessMessage("Banned IP {0}.", ip); - } - #endregion - return; - case "addtemp": - #region Add temp ban - { - if (args.Parameters.Count < 3) + // Good case: Online ban for matching character. + if (players.Count == 1) { - args.Player.SendErrorMessage("Invalid syntax! Proper syntax: {0}ban addtemp