diff options
author | Julien Dessaux | 2009-08-07 00:20:58 +0200 |
---|---|---|
committer | Julien Dessaux | 2009-08-07 00:20:58 +0200 |
commit | 7a9d187421cf1d809d7d0bc8a46f3b7b6b5b028c (patch) | |
tree | 5ff8924f0b3839a80ab08f9d11c47767ffc3b6f0 | |
parent | Wrote the ping pong stuff (diff) | |
download | hsbot-7a9d187421cf1d809d7d0bc8a46f3b7b6b5b028c.tar.gz hsbot-7a9d187421cf1d809d7d0bc8a46f3b7b6b5b028c.tar.bz2 hsbot-7a9d187421cf1d809d7d0bc8a46f3b7b6b5b028c.zip |
Began to write the commands' stuff, and made a working reboot.
-rw-r--r-- | Hsbot/IRC.hs | 2 | ||||
-rw-r--r-- | Hsbot/Main.hs | 30 |
2 files changed, 29 insertions, 3 deletions
diff --git a/Hsbot/IRC.hs b/Hsbot/IRC.hs index cd6b7b8..abac6b2 100644 --- a/Hsbot/IRC.hs +++ b/Hsbot/IRC.hs @@ -38,7 +38,7 @@ data IrcOutput = Str String -- a regular string -- | Parses an IrcInput parseIrcMsg :: String -> IrcInput -parseIrcMsg _ = Err "Parsing not yet implemented" +parseIrcMsg str = (Cmd "user" "channel" (str, Just "args")) -- | Connects to a server connectServer :: IrcServer -> IO (IrcServer, Handle) diff --git a/Hsbot/Main.hs b/Hsbot/Main.hs index dad568a..3ced3a5 100644 --- a/Hsbot/Main.hs +++ b/Hsbot/Main.hs @@ -1,5 +1,6 @@ module Hsbot.Main ( imain + , imain' ) where import Control.Concurrent @@ -26,7 +27,23 @@ imain' modul' reboot bot = do servers' <- mapM connectServer (ircServers C.config) putStrLn "Joining channels..." mapM_ initServer servers' - return () + putStrLn "Spawning threads..." + chan <- newChan :: IO (Chan IrcOutput) + mapM_ (forkIO . listener chan) servers' + state <- monitor chan bot + reboot modul' bot + +-- | Bot main loop, monitors the threads states and handle reboot +monitor :: (Chan IrcOutput) -> Bot -> IO Bot +monitor chan bot = do + loop bot + where loop bot' = do + input <- readChan chan + case input of + Reboot ->do + putStrLn "Got reboot message, rebooting" + return bot' + Str str -> putStrLn ("received : " ++ str) >> loop bot' -- | Thread entry point for socket listeners listener :: (Chan IrcOutput) -> (IrcServer, Handle) -> IO () @@ -34,5 +51,14 @@ listener chan (server, handle) = forever $ do str <- hGetLine handle writeChan chan (Str str) if ping str then pong handle str - else return () + else eval (parseIrcMsg str) + where + eval str + | (Cmd user channel (cmd, args)) <- str = do + let cmd' = tail cmd + unless (null cmd') (parseCmds user cmd' args channel) + parseCmds user cmd args channel + | cmd == "reboot" = writeChan chan Reboot + | otherwise = do + putStrLn $"Unknown command : " ++ cmd |