/// <summary> /// This is a copy of TShocks handlecommand method, sans the permission checks /// </summary> public static bool PermissionlessInvoke(this TShockAPI.TSPlayer player, string text) { IEnumerable <TShockAPI.Command> cmds; List <string> args; string cmdName; string cmdText; if (string.IsNullOrEmpty(text)) { return(false); } cmdText = text.Remove(0, 1); args = typeof(TShockAPI.Commands).CallPrivateMethod <List <string> >(true, "ParseParameters", cmdText); if (args.Count < 1) { return(false); } cmdName = args[0].ToLower(); args.RemoveAt(0); cmds = TShockAPI.Commands.ChatCommands.Where(c => c.HasAlias(cmdName)); if (Enumerable.Count(cmds) == 0) { if (player.AwaitingResponse.ContainsKey(cmdName)) { Action <TShockAPI.CommandArgs> call = player.AwaitingResponse[cmdName]; player.AwaitingResponse.Remove(cmdName); call(new TShockAPI.CommandArgs(cmdText, player, args)); return(true); } player.SendErrorMessage("Invalid command entered. Type /help for a list of valid commands."); return(true); } foreach (TShockAPI.Command cmd in cmds) { if (!cmd.AllowServer && !player.RealPlayer) { player.SendErrorMessage("You must use this command in-game."); } else { if (cmd.DoLog) { TShockAPI.TShock.Utils.SendLogs(string.Format("{0} executed: /{1}.", player.Name, cmdText), Color.Red); } cmd.RunWithoutPermissions(cmdText, player, args); } } return(true); }
/// <summary> /// Invokes a command ignoring permissions /// </summary> public static bool RunWithoutPermissions(this TShockAPI.Command cmd, string msg, TShockAPI.TSPlayer ply, List <string> parms) { try { TShockAPI.CommandDelegate cmdDelegateRef = SEconomyPlugin.GetPrivateField <TShockAPI.CommandDelegate>(cmd.GetType(), cmd, "command"); cmdDelegateRef(new TShockAPI.CommandArgs(msg, ply, parms)); } catch (Exception e) { ply.SendErrorMessage("Command failed, check logs for more details."); TShockAPI.Log.Error(e.ToString()); } return(true); }
/// <summary> /// Executes the AliasCommand. Will either forward the command to the tshock handler or do something else /// </summary> void DoCommands(AliasCommand alias, TShockAPI.TSPlayer player, List <string> parameters) { //loop through each alias and do the commands. foreach (string commandToExecute in alias.CommandsToExecute) { //todo: parse paramaters and dynamics string mangledString = commandToExecute; //specifies whether the command to run should be executed as a command, or ignored. //useful for functions like $msg that does other shit bool executeCommand = true; //replace parameter markers with actual parameter values ReplaceParameterMarkers(parameters, ref mangledString); mangledString = mangledString.Replace("$calleraccount", player.UserAccountName); mangledString = mangledString.Replace("$callername", player.Name); //$random(x,y) support. Returns a random number between x and y if (randomRegex.IsMatch(mangledString)) { foreach (Match match in randomRegex.Matches(mangledString)) { int randomFrom = 0; int randomTo = 0; if (!string.IsNullOrEmpty(match.Groups[2].Value) && int.TryParse(match.Groups[2].Value, out randomTo) && !string.IsNullOrEmpty(match.Groups[1].Value) && int.TryParse(match.Groups[1].Value, out randomFrom)) { Random random = new Random(); mangledString = mangledString.Replace(match.ToString(), random.Next(randomFrom, randomTo).ToString()); } else { TShockAPI.Log.ConsoleError(match.ToString() + " has some stupid shit in it, have a look at your AliasCmd config file."); mangledString = mangledString.Replace(match.ToString(), ""); } } } // $runas(u,cmd) support. Run command as user if (runasFunctionRegex.IsMatch(mangledString)) { foreach (Match match in runasFunctionRegex.Matches(mangledString)) { string impersonatedName = match.Groups[2].Value; Economy.EconomyPlayer impersonatedPlayer = SEconomyPlugin.GetEconomyPlayerSafe(impersonatedName); if (impersonatedPlayer != null) { string commandToRun = match.Groups[3].Value;; player = impersonatedPlayer.TSPlayer; mangledString = commandToRun.Trim(); } } } // $msg(u,msg) support. Sends the user a non-chat informational message if (msgRegex.IsMatch(mangledString)) { foreach (Match match in msgRegex.Matches(mangledString)) { string msgTarget = match.Groups[2].Value.Trim(); string message = match.Groups[3].Value.Trim(); Economy.EconomyPlayer destinationPlayer = SEconomyPlugin.GetEconomyPlayerSafe(msgTarget); if (destinationPlayer != null) { //custom command, skip forwarding of the command to the tshock executer executeCommand = false; destinationPlayer.TSPlayer.SendInfoMessage(message); } } } //and send the command to tshock to do. try { //prevent an infinite loop for a subcommand calling the alias again causing a commandloop string command = mangledString.Split(' ')[0].Substring(1); if (!command.Equals(alias.CommandAlias, StringComparison.CurrentCultureIgnoreCase)) { if (executeCommand) { HandleCommandWithoutPermissions(player, mangledString); } } else { TShockAPI.Log.ConsoleError(string.Format("cmdalias {0}: calling yourself in an alias will cause an infinite loop. Ignoring.", alias.CommandAlias)); } } catch { //execute the command disregarding permissions player.SendErrorMessage(alias.UsageHelpText); } } }