summaryrefslogtreecommitdiff
path: root/HsBot/Base
diff options
context:
space:
mode:
Diffstat (limited to 'HsBot/Base')
-rw-r--r--HsBot/Base/Cmd.hs16
-rw-r--r--HsBot/Base/Conf.hs28
-rw-r--r--HsBot/Base/Env.hs19
-rw-r--r--HsBot/Base/State.hs32
4 files changed, 95 insertions, 0 deletions
diff --git a/HsBot/Base/Cmd.hs b/HsBot/Base/Cmd.hs
new file mode 100644
index 0000000..c4963ac
--- /dev/null
+++ b/HsBot/Base/Cmd.hs
@@ -0,0 +1,16 @@
+module HsBot.Base.Cmd where
+
+import HsBot.Base.State
+
+data Cmd = Cmd String String (State -> IO ())
+
+instance Show Cmd where
+ show (Cmd a b _) = a ++ " - " ++ b
+
+cmdGet :: String -> [Cmd] -> Maybe Cmd
+cmdGet x commands =
+ let command = [ (Cmd a b c) | (Cmd a b c) <- commands, a == x ]
+ in if length command == 0
+ then Nothing
+ else Just (head command)
+
diff --git a/HsBot/Base/Conf.hs b/HsBot/Base/Conf.hs
new file mode 100644
index 0000000..54749ba
--- /dev/null
+++ b/HsBot/Base/Conf.hs
@@ -0,0 +1,28 @@
+module HsBot.Base.Conf where
+
+import qualified Data.Map as M
+
+type Conf = M.Map String String
+
+makeConf = M.fromList
+ [ ("name", "HsBot")
+ , ("version", "v0.0")
+ , ("databaseFile", "hsbot.db")
+ , ("maxMessageSize", "400")
+ , ("admin", "rantanplan")
+ , ("ircServer", "irc.german-elite.net")
+ , ("ircChannel", "#buetow.org")
+ , ("ircNick", "hotdog")
+ , ("ircPort", "6667")
+ , ("ircUser", "hsbot.haskell.eu")
+ ]
+
+get :: (Monad m) => String -> Conf -> m String
+get = M.lookup
+
+getUnwrappedInt :: String -> Conf -> Int
+getUnwrappedInt key conf = read (getUnwrapped key conf) :: Int
+
+getUnwrapped :: String -> Conf -> String
+getUnwrapped key conf = do { val <- get key conf; val }
+
diff --git a/HsBot/Base/Env.hs b/HsBot/Base/Env.hs
new file mode 100644
index 0000000..58b2347
--- /dev/null
+++ b/HsBot/Base/Env.hs
@@ -0,0 +1,19 @@
+module HsBot.Base.Env where
+
+import HsBot.Base.Conf
+import HsBot.Base.State
+
+type Dispatch = String -> (String -> IO ()) -> Env -> IO Env
+data Env = DispatchEnv State Conf Dispatch | Env State Conf
+
+castEnv :: Env -> Env
+castEnv (DispatchEnv state conf _) = Env state conf
+
+envGetInt :: String -> Env -> Int
+envGetInt key (Env _ conf) = getUnwrappedInt key conf
+envGetInt key env = envGetInt key (castEnv env)
+
+envGet :: String -> Env -> String
+envGet key (Env _ conf) = getUnwrapped key conf
+envGet key env = envGet key (castEnv env)
+
diff --git a/HsBot/Base/State.hs b/HsBot/Base/State.hs
new file mode 100644
index 0000000..abcf479
--- /dev/null
+++ b/HsBot/Base/State.hs
@@ -0,0 +1,32 @@
+module HsBot.Base.State where
+
+import qualified Data.Map as M
+
+import List
+import HsBot.IRC.User
+
+data State = State {
+ currentChannel :: String,
+ line :: String,
+ users :: [User]
+ } deriving (Show, Read)
+
+stateNumUsers :: State -> Int
+stateNumUsers state = length $ users state
+
+stateSortedUsers :: State -> [User]
+stateSortedUsers state = sort $ users state
+
+stateLoad :: String -> IO State
+stateLoad databaseFile = do
+ file <- readFile databaseFile
+ return ( read file :: State )
+
+stateSave :: String -> State -> IO ()
+stateSave databaseFile = writeFile databaseFile . show
+
+stateSaveIO :: String -> IO State -> IO ()
+stateSaveIO databaseFile state = do
+ state' <- state
+ writeFile databaseFile (show state')
+