From 884c6c9f2e3a03d6666c8dd6c6d6b6513db88ad5 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Thu, 4 Feb 2010 20:36:58 +0100 Subject: Reorganized code and types, changed slightly the architecture. --- Hsbot/IRC.hs | 47 +++++++++++++++++------------------------------ 1 file changed, 17 insertions(+), 30 deletions(-) (limited to 'Hsbot/IRC.hs') diff --git a/Hsbot/IRC.hs b/Hsbot/IRC.hs index d3a3114..3fe2181 100644 --- a/Hsbot/IRC.hs +++ b/Hsbot/IRC.hs @@ -5,50 +5,38 @@ module Hsbot.IRC import Control.Concurrent.Chan import Control.Monad.State -import System.IO -import Hsbot.Core import Hsbot.IRCParser +import Hsbot.Plugin +import Hsbot.Types +import Hsbot.Utils -- | Setup a newly connected server by sending nick and join stuff initServer :: IrcBot () initServer = do server <- gets serverConfig - writeMsg $ IrcMsg Nothing "NICK" [(nickname server)] - writeMsg $ IrcMsg Nothing "USER" [(nickname server), "0", "*", (realname server)] + sendstr $ serializeIrcMsg $ IrcMsg Nothing "NICK" [(nickname server)] + sendstr $ serializeIrcMsg $ IrcMsg Nothing "USER" [(nickname server), "0", "*", (realname server)] when (not . null $ password server) $ do - writeMsg $ IrcMsg Nothing "PRIVMSG" ["nickserv", "identify", (password server)] - joinChans - return () + sendstr $ serializeIrcMsg $ IrcMsg Nothing "PRIVMSG" ["nickserv", "identify", (password server)] + mapM_ joinChan (channels server) -- | Run a server runServer :: IrcBot () runServer = do - handle <- gets botHandle + chan <- gets botChannel plugins <- gets botPlugins - str <- liftIO $ hGetLine handle - traceM $ inColor ("<-- " ++ str) [33] - let msg = parseIrcMsg str + let input = readChan chan + msg <- liftIO input case msg of - Right msg' -> do - mapM_ (sendPlugin msg') plugins - return () - _ -> do - return () - traceM $ show msg + InputMsg inputMsg -> + mapM_ (sendToPlugin $ InputMsg inputMsg) plugins + OutputMsg outputMsg -> + sendstr (serializeIrcMsg outputMsg) + InternalCmd internalCmd -> + traceM "TODO" runServer -sendPlugin :: IrcMsg -> Plugin -> IrcBot () -sendPlugin msg plugin = do - let chan = pluginChannel plugin - liftIO $ writeChan chan msg - --- | Join chans -joinChans :: IrcBot () -joinChans = do - server <- gets serverConfig - mapM_ joinChan (channels server) - -- | Joins a chan joinChan :: String -> IrcBot () joinChan name = do @@ -57,7 +45,6 @@ joinChan name = do newChannel = Channel name (nickname $ serverConfig bot) (administrators $ serverConfig bot) - traceM $ " Joining " ++ name - writeMsg $ IrcMsg Nothing "JOIN" [name] + sendstr $ serializeIrcMsg $ IrcMsg Nothing "JOIN" [name] put $ bot { chans = newChannel : oldChannels } -- cgit v1.2.3