예제 #1
0
        /// <summary>
        /// Validates if a command has the right amount of parameters
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        public static bool CanInterpret(ConsoleCommand cmd, out string parserMsg)
        {
            string         msg          = string.Empty;
            ConsoleCommand validVersion = ConsoleCommandWords.FindCommand(cmd.CommandWord);

            if (validVersion.Matches(cmd, out msg))
            {
                parserMsg = msg;
                return(true);
            }
            else
            {
                parserMsg = msg;
                return(false);
            }
        }
예제 #2
0
        /// <summary>
        /// Checks if a given keyword is valid command and saves his given parameters
        /// </summary>
        /// <param name="commandLine"></param>
        /// <param name="cmd"></param>
        /// <returns></returns>
        public static bool CanRead(string commandLine, out ConsoleCommand cmd)
        {
            string keyword = commandLine.Split(' ')[0];
            string param1;
            string param2;

            try
            {
                param1 = commandLine.Split(' ')[1];
            }
            catch
            {
                param1 = string.Empty;
            }

            try
            {
                param2 = commandLine.Split(' ')[2];
            }
            catch
            {
                param2 = string.Empty;
            }

            if (keyword != string.Empty)
            {
                if (ConsoleCommandWords.IsCommand(keyword))
                {
                    cmd = new ConsoleCommand(keyword);
                    cmd.Parameters[0] = param1;
                    cmd.Parameters[1] = param2;
                    return(true);
                }
                else
                {
                    cmd = new ConsoleCommand(commandLine);
                    return(false);
                }
            }
            else
            {
                cmd = new ConsoleCommand(commandLine);
                return(false);
            }
        }
        /// <summary>
        /// Executes any given command if known to the parser
        /// </summary>
        /// <param name="cmd"></param>
        public static string ExecuteCommand(ConsoleCommand cmd)
        {
            string param1        = cmd.Parameters[0];
            string param2        = cmd.Parameters[1];
            string param3        = cmd.Parameters[2];
            string command       = cmd.CommandWord;
            string parserMessage = string.Empty;

            if (!Parser.CanInterpret(cmd, out parserMessage))
            {
                LogConsole.WriteLine("ERROR WHILE PARSING COMMAND: " + command + " ::: INVALID");
                LogConsole.Break();
                return("PARSER_ERROR");
            }

            UserAuthorization userAuthorization     = UserAuthorization.None;
            UserAuthorization requiredAuthorization = ConsoleCommandWords.FindCommand(command).RequiredAuthorization;

            if (requiredAuthorization != UserAuthorization.None)
            {
                userAuthorization = UserManager.GetUserAuthorization(param1);

                if (userAuthorization != requiredAuthorization)
                {
                    LogConsole.WriteLine(UserManager.GetUserAuthorizationErrorMessage(requiredAuthorization, userAuthorization, param1));
                    LogConsole.Break();
                    return("UNAUTHORIZED");
                }
            }

            switch (command)
            {
            case "terminate":
            {
                LogConsole.PrintCommand(cmd);
                Actions.TerminateServer();
                return(string.Empty);
            }

            case "clear":
            {
                LogConsole.PrintCommand(cmd);
                LogConsole.Clear();
                return(string.Empty);
            }

            case "help":
            {
                LogConsole.PrintCommand(cmd);
                LogConsole.Break();
                LogConsole.Write(ConsoleCommandWords.CommandList());
                return(string.Empty);
            }

            case "add":
            {
                LogConsole.PrintCommand(cmd);
                LogConsole.Break();

                switch (param1)
                {
                case "session":
                {
                    LogConsole.Break();
                    Session newSession = new Session();
                    SessionManager.AddSession(newSession, string.Empty, false);
                    LogConsole.Write(newSession.Details());
                    return(newSession.Id);
                }

                default:
                {
                    LogConsole.Break();
                    LogConsole.ThrowParseError("Invalid Type", param1);
                    return(string.Empty);
                }
                }
            }

            /// DEPRECATED
            //case "select":
            //    {
            //        LogConsole.PrintCommand(cmd);
            //        LogConsole.Break();
            //        switch (param1)
            //        {
            //            case "session":
            //                {
            //                    if (LogConsole.SelectSession(param2, SessionManager.Sessions) != false)
            //                    {
            //                        LogConsole.Break();
            //                        LogConsole.Write("Selected session is now " + param2.ToUpper());
            //                        SessionManager.OnSelectedSessionChanged();
            //                    }
            //                    else
            //                    {
            //                        LogConsole.Break();
            //                        LogConsole.ThrowParseError("Unknown Session!", param2.ToUpper());
            //                    }
            //                    return string.Empty;
            //                }
            //            default:
            //                {
            //                    LogConsole.Break();
            //                    LogConsole.ThrowParseError("Invalid Type", param1);
            //                    return string.Empty;
            //                }
            //        }
            //    }
            case "selected":
            {
                LogConsole.PrintCommand(cmd);
                LogConsole.Break();

                if (LogConsole.currentSession == null)
                {
                    LogConsole.Write("No selected session");
                }
                else
                {
                    LogConsole.Write("Selected session is " + LogConsole.currentSession.Id);
                }
                return(string.Empty);
            }

            case "stop":
            {
                switch (param1)
                {
                case "session":
                {
                    LogConsole.Break();
                    if (SessionManager.SessionExists(param2))
                    {
                        SessionManager.FindSessionBySessionId(param2).Stop();
                        LogConsole.Write("Stopped Session " + param2);
                    }
                    else
                    {
                        LogConsole.ThrowParseError("unknown " + param1 + " : " + param2 + " - Session does not exist", cmd.CommandWord);
                    }
                    return(string.Empty);
                }

                default:
                {
                    LogConsole.Break();
                    LogConsole.ThrowParseError("Invalid Type", param1);
                    return(string.Empty);
                }
                }
            }

            case "delete":
            {
                switch (param1)
                {
                case "session":
                {
                    LogConsole.Break();
                    if (SessionManager.SessionExists(param2))
                    {
                        if (SessionManager.FindSessionBySessionId(param2).isStopped)
                        {
                            SessionManager.FindSessionBySessionId(param2).Terminate();
                            SessionManager.DeleteSession(param2);
                            LogConsole.Write("Deleted Session " + param2);
                        }
                        else
                        {
                            LogConsole.ThrowException(SessionManager.FindSessionBySessionId(param2).Id, "Session is still running! Session must be stopped before termination");
                        }
                        return(string.Empty);
                    }
                    else
                    {
                        LogConsole.ThrowParseError("unknown " + param1 + " : " + param2 + " - Session does not exist", cmd.CommandWord);
                    }
                    return(string.Empty);
                }

                default:
                {
                    LogConsole.Break();
                    LogConsole.ThrowParseError("Invalid Type", param1);
                    return(string.Empty);
                }
                }
            }

            default:
            {
                return(string.Empty);
            }
            }
        }