Beispiel #1
0
        public static void Initialize()
        {
            commandHandlers = new Dictionary <string, CommandHandlerInfo>(StringComparer.OrdinalIgnoreCase);
            foreach (var type in Assembly.GetExecutingAssembly().GetTypes())
            {
                foreach (var method in type.GetMethods())
                {
                    foreach (var attribute in method.GetCustomAttributes <CommandHandlerAttribute>())
                    {
                        var commandHandler = new CommandHandlerInfo()
                        {
                            Handler   = (CommandHandler)Delegate.CreateDelegate(typeof(CommandHandler), method),
                            Attribute = attribute
                        };

                        commandHandlers[attribute.Command] = commandHandler;
                    }
                }
            }

            var thread = new Thread(new ThreadStart(CommandThread));

            thread.IsBackground = true;
            thread.Start();
        }
Beispiel #2
0
        public static CommandHandlerResponse GetCommandHandler(Session session, string command, string[] parameters, out CommandHandlerInfo commandInfo)
        {
            bool isSUDOauthorized = false;

            if (command.ToLower() == "sudo")
            {
                string   sudoCommand = "";
                string[] sudoParameters;
                if (parameters.Length > 0)
                {
                    sudoCommand = parameters[0];
                }

                if (!commandHandlers.TryGetValue(sudoCommand, out commandInfo))
                {
                    return(CommandHandlerResponse.InvalidCommand);
                }

                if (session == null)
                {
                    Console.WriteLine("SUDO does not work on the console because you already have full access. Remove SUDO from command and execute again.");
                    return(CommandHandlerResponse.InvalidCommand);
                }

                if (commandInfo.Attribute.Access <= session.AccessLevel)
                {
                    isSUDOauthorized = true;
                }

                if (isSUDOauthorized)
                {
                    command        = sudoCommand;
                    sudoParameters = new string[parameters.Length - 1];
                    for (int i = 1; i < parameters.Length; i++)
                    {
                        sudoParameters[i - 1] = parameters[i];
                    }
                    parameters = sudoParameters;
                }
            }

            if (!commandHandlers.TryGetValue(command, out commandInfo))
            {
                return(CommandHandlerResponse.InvalidCommand);
            }

            if ((commandInfo.Attribute.Flags & CommandHandlerFlag.ConsoleInvoke) != 0 && session != null)
            {
                return(CommandHandlerResponse.NoConsoleInvoke);
            }

            if (session != null)
            {
                bool isAdvocate = false;                  // we don't track/support this
                bool isSentinel = session.Player.IsEnvoy; // we map this to envoy
                bool isEnvoy    = isSentinel;
                bool isArch     = session.Player.IsArch;
                bool isAdmin    = session.Player.IsAdmin;

                if (commandInfo.Attribute.Access == AccessLevel.Advocate && !(isAdvocate || isSentinel || isEnvoy || isArch || isAdmin || isSUDOauthorized) ||
                    commandInfo.Attribute.Access == AccessLevel.Sentinel && !(isSentinel || isEnvoy || isArch || isAdmin || isSUDOauthorized) ||
                    commandInfo.Attribute.Access == AccessLevel.Envoy && !(isEnvoy || isArch || isAdmin || isSUDOauthorized) ||
                    commandInfo.Attribute.Access == AccessLevel.Developer && !(isArch || isAdmin || isSUDOauthorized) ||
                    commandInfo.Attribute.Access == AccessLevel.Admin && !(isAdmin || isSUDOauthorized))
                {
                    return(CommandHandlerResponse.NotAuthorized);
                }
            }

            if (commandInfo.Attribute.ParameterCount != -1 && parameters.Length < commandInfo.Attribute.ParameterCount)
            {
                return(CommandHandlerResponse.InvalidParameterCount);
            }

            if ((commandInfo.Attribute.Flags & CommandHandlerFlag.RequiresWorld) != 0 && (session == null || session.Player == null || !session.Player.InWorld))
            {
                return(CommandHandlerResponse.NotInWorld);
            }

            if (isSUDOauthorized)
            {
                return(CommandHandlerResponse.SudoOk);
            }

            return(CommandHandlerResponse.Ok);
        }
Beispiel #3
0
        public static CommandHandlerResponse GetCommandHandler(Session session, string command, string[] parameters, out CommandHandlerInfo commandInfo)
        {
            if (!commandHandlers.TryGetValue(command, out commandInfo))
            {
                return(CommandHandlerResponse.InvalidCommand);
            }

            if ((commandInfo.Attribute.Flags & CommandHandlerFlag.ConsoleInvoke) == 0 && session == null)
            {
                return(CommandHandlerResponse.NoConsoleInvoke);
            }

            if (commandInfo.Attribute.ParameterCount != -1 && parameters.Length < commandInfo.Attribute.ParameterCount)
            {
                return(CommandHandlerResponse.InvalidParameterCount);
            }

            if ((commandInfo.Attribute.Flags & CommandHandlerFlag.RequiresWorld) != 0 && (session == null || session.Player == null || !session.Player.InWorld))
            {
                return(CommandHandlerResponse.NotInWorld);
            }

            return(CommandHandlerResponse.Ok);
        }