From 7a9d187421cf1d809d7d0bc8a46f3b7b6b5b028c Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Fri, 7 Aug 2009 00:20:58 +0200 Subject: Began to write the commands' stuff, and made a working reboot. --- Hsbot/IRC.hs | 2 +- Hsbot/Main.hs | 30 ++++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) (limited to 'Hsbot') 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 -- cgit v1.2.3