{-# LANGUAGE OverloadedStrings #-} module DRcon.EvalParser ( CommandErrorType(..), EvalVar(..), VarValue(..), InputType(..), EvalCmd, parseCommand, internalAutoComplete, helpMessage, helpVars, showVar ) where import Prelude hiding (break, concat) import Data.Char (isSpace) import Data.Maybe import Data.Text hiding (filter, map) import qualified Data.Text as T import Data.Text.Read import Control.Monad.Error import Control.Applicative ((<$>)) import DRcon.CommandArgs (parseRconMode, parseEncoding, showEncoding) import DRcon.Prompt (readPrompt) import DarkPlaces.Rcon (RconMode) import DarkPlaces.Text (DecodeType(..)) import Data.Monoid import Text.Printf data CommandErrorType = UnknownCommand Text | WrongArgumentType Text Text | CommandTakesNoArgumens Text Text | OtherError Text deriving(Eq) data EvalVar = Mode | TimeDiff | Timeout | Encoding | Color | PromptVar deriving(Eq, Ord, Bounded, Enum) data VarValue = SetMode RconMode | SetTimeDiff Int | SetTimeout Float | SetEncoding DecodeType | SetColor Bool | SetPrompt String deriving(Eq) data InputType = Empty | Quit | Help | Version | RepeatLast | Login | ListVars | Show EvalVar | Set VarValue | History (Maybe Int) | RconCommand Text deriving(Show, Eq) type EvalCmd = Either CommandErrorType InputType instance Show CommandErrorType where show (UnknownCommand cmd) = printf badCmd $ unpack cmd where badCmd = "unknown command '%s'\nuse :? for help." show (WrongArgumentType cmd _) = case cmd of ":history" -> "Wrong argument\nSyntax: :history [n]" _ -> printf "Wrong argument for command \"%s\"" $ unpack cmd show (CommandTakesNoArgumens cmd _) = printf noArgs $ unpack cmd where noArgs = "Error: command \"%s\" takes no arguments" show (OtherError msg) = unpack msg instance Show EvalVar where show Mode = "mode" show TimeDiff = "timediff" show Timeout = "timeout" show Encoding = "encoding" show Color = "color" show PromptVar = "prompt" instance Show VarValue where show v = show (getVarName v) ++ " " ++ showVar v cmdInfo :: [(Text, Text)] cmdInfo = [ ("", "execute command via rcon"), (":", "repeat last command"), (":help, :?", "display list of commands"), (":history [n]", "show n (10 by default) last commands"), (":login", "promp for server password"), (":set", "list available options"), (":set