Rethought the way I handled IRC data.
This commit is contained in:
parent
8caf09e50b
commit
7a86235906
2 changed files with 7 additions and 13 deletions
14
Hsbot/IRC.hs
14
Hsbot/IRC.hs
|
@ -1,6 +1,5 @@
|
|||
module Hsbot.IRC
|
||||
( IrcInput(..)
|
||||
, IrcOutput(..)
|
||||
( IrcLine(..)
|
||||
, connectServer
|
||||
, initServer
|
||||
, parseIrcMsg
|
||||
|
@ -21,17 +20,12 @@ type Channel = String
|
|||
type Command = String
|
||||
type Args = [String]
|
||||
|
||||
-- | Information from IRC
|
||||
data IrcInput = Cmd User Channel (Command, Maybe String) -- a regular command
|
||||
| Line User Channel String -- a normal line of little significance
|
||||
| Err String -- an error occured in parsing
|
||||
deriving (Eq,Show)
|
||||
|
||||
-- | Data that can go over the remote channel
|
||||
data IrcOutput = Str String -- a regular string
|
||||
-- | An IRC line
|
||||
data IrcLine = Str String -- a regular string
|
||||
| Quit (IrcServer, Handle) -- a quit message from a server
|
||||
| Join (IrcServer, Channel) -- joined a channel
|
||||
| Part (IrcServer, Channel) -- parted the channel
|
||||
| Ping (String) -- pinged by the server
|
||||
| Reboot -- reboot message sent
|
||||
| Nil -- signifies thread death, only happens after reboot
|
||||
deriving (Eq,Show)
|
||||
|
|
|
@ -28,13 +28,13 @@ imain' modul' reboot bot = do
|
|||
putStrLn "Joining channels..."
|
||||
mapM_ initServer servers'
|
||||
putStrLn "Spawning threads..."
|
||||
chan <- newChan :: IO (Chan IrcOutput)
|
||||
chan <- newChan :: IO (Chan IrcLine)
|
||||
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 IrcLine) -> Bot -> IO Bot
|
||||
monitor chan bot = do
|
||||
loop bot
|
||||
where loop bot' = do
|
||||
|
@ -46,7 +46,7 @@ monitor chan bot = do
|
|||
Str str -> putStrLn ("received : " ++ str) >> loop bot'
|
||||
|
||||
-- | Thread entry point for socket listeners
|
||||
listener :: (Chan IrcOutput) -> (IrcServer, Handle) -> IO ()
|
||||
listener :: (Chan IrcLine) -> (IrcServer, Handle) -> IO ()
|
||||
listener chan (server, handle) = forever $ do
|
||||
str <- hGetLine handle
|
||||
writeChan chan (Str str)
|
||||
|
|
Reference in a new issue