Exemplo n.º 1
0
        public virtual bool Handle(string parameters, WorldSession _session = null)
        {
            SetSentErrorMessage(false);
            session = _session;
            // check if the user has enough privileges to access command group.
            // check if the user has enough privileges to invoke the command.
            if (session != null && this.Attributes.MinUserLevel > session.GetSecurity())
            {
                return(SendErrorMessage(CypherStrings.NoCmd));
            }

            string[] @params = null;

            if (parameters == string.Empty)
            {
                command = this.GetDefaultSubcommand();
            }
            else
            {
                @params = parameters.Split(' ');

                var          blah  = this.GetSubGroupCommand(@params[0]);
                CommandGroup group = null;
                if (blah.Key != null)
                {
                    group = blah.Value;
                    return(group.Handle(string.Join(" ", @params.Skip(1)), session));
                }
                else
                {
                    command = this.GetSubcommand(@params[0]) ?? this.GetDefaultSubcommand();
                }

                if (command != this.GetDefaultSubcommand())
                {
                    @params = @params.Skip(1).ToArray();
                }
            }

            // check if the user has enough privileges to invoke the command.
            if (session != null && command.MinUserLevel > session.GetSecurity())
            {
                return(SendErrorMessage(CypherStrings.NoCmd));
            }

            var success = (bool)this._commands[command].Invoke(this, new object[] { @params, this });

            if (!SentErrorMessage && !success)
            {
                return(SendErrorMessage(command.Help));
            }

            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Tries to parse given line as a server command.
        /// </summary>
        /// <param name="line">The line to be parsed.</param>
        /// <param name="invokerClient">The invoker client if any.</param>
        /// <returns><see cref="bool"/></returns>
        public static bool TryParse(string line, WorldSession session = null)
        {
            string       command;
            string       parameters;
            var          success = false;
            CommandGroup group   = null;

            if (line == null)
            {
                return(false);
            }
            if (line.Trim() == string.Empty)
            {
                return(false);
            }

            if (!ExtractCommandAndParameters(line, session == null, out command, out parameters))
            {
                return(false);
            }

            foreach (var pair in CommandGroups)
            {
                if (pair.Key.Name != command)
                {
                    continue;
                }

                group   = pair.Value;
                success = group.Handle(parameters, session);
                break;
            }

            if (group == null)
            {
                return(SendErrorMessage(CypherStrings.NoCmd, session));
            }

            if (!group.SentErrorMessage && !success)
            {
                return(SendErrorMessage(group.command.Help, session));
            }

            return(true);
        }