Reorganized code and types, changed slightly the architecture.
This commit is contained in:
parent
57f559f3a1
commit
884c6c9f2e
10 changed files with 272 additions and 164 deletions
47
Hsbot/IRC.hs
47
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 }
|
||||
|
||||
|
|
Reference in a new issue