Began to write the commands' stuff, and made a working reboot.
This commit is contained in:
parent
14f87adb8b
commit
7a9d187421
2 changed files with 29 additions and 3 deletions
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Reference in a new issue