Ejemplo n.º 1
0
        public bool ExecuteCommandSilent(object Player, object Command)
        {
            TShockAPI.TSPlayer p = null;
            string             commandToExecute = "";

            if ((p = GetPlayer(Player)) == null)
            {
                return(false);
            }

            try
            {
                if (Command is List <string> )
                {
                    List <string> cmdList = Command as List <string>;
                    foreach (var param in cmdList.Skip(1))
                    {
                        commandToExecute += " " + param;
                    }
                }
                else if (Command is string)
                {
                    commandToExecute = Command.ToString();
                }

                if (string.IsNullOrEmpty((commandToExecute = commandToExecute.Trim())) == true)
                {
                    return(false);
                }

                p.PermissionlessInvoke(commandToExecute, true);

                return(true);
            }
            catch (Exception)
            {
                ScriptLog.ErrorFormat("tshock_exec_silent", "The command \"{0}\" failed.", commandToExecute.Trim());
                return(false);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Executes the AliasCommand.  Will either forward the command to the tshock handler or do something else
        /// </summary>
        protected 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.User.Name);
                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))
                        {
                            // this is a critical section
                            // Random class is seeded from the system clock if you construct one without a seed.
                            // therefore, calls to Next() at exactly the same point in time is likely to produce the same number.
                            lock (__rndLock) {
                                mangledString = mangledString.Replace(match.ToString(), randomGenerator.Next(randomFrom, randomTo).ToString());
                            }
                        }
                        else
                        {
                            TShock.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;
                        TSPlayer impersonatedPlayer = TShockAPI.TShock.Players.FirstOrDefault(i => i != null && i.Name == impersonatedName);

                        if (impersonatedPlayer != null)
                        {
                            string commandToRun = match.Groups[3].Value;
                            player = impersonatedPlayer;

                            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();
                        TSPlayer destinationPlayer = TShockAPI.TShock.Players.FirstOrDefault(i => i != null && i.Name == msgTarget);

                        if (destinationPlayer != null)
                        {
                            //custom command, skip forwarding of the command to the tshock executer
                            executeCommand = false;

                            destinationPlayer.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)
                        {
                            player.PermissionlessInvoke(mangledString);
                        }
                    }
                    else
                    {
                        TShock.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);
                }
            }
        }