Archived
1
0
Fork 0

Rationalized the way bot configs are handled.

This commit is contained in:
Julien Dessaux 2010-05-29 16:59:41 +02:00
parent daea135424
commit 57bec4921b
5 changed files with 24 additions and 31 deletions

View file

@ -1,18 +1,9 @@
module Hsbot.Config
( Config(..)
, defaultConfig
( BotConfig (..)
) where
import Hsbot.Irc.Config (IrcConfig)
import Hsbot.Irc.Config
-- | Configuration data type
data Config = Config
{ ircConfigs :: [IrcConfig]
}
-- | User configuration
defaultConfig :: Config
defaultConfig = Config
{ ircConfigs = []
}
data BotConfig = IrcBotConfig IrcConfig

View file

@ -17,18 +17,18 @@ import Hsbot.Plugin
import Hsbot.Types
-- | Bot's main entry point
hsbot :: Config -> IO ()
hsbot :: [BotConfig] -> IO ()
hsbot config = do
startTime <- getCurrentTime
putStrLn "[Hsbot] Opening communication channel... "
chan <- newChan :: IO (Chan BotMsg)
mvar <- newMVar M.empty :: IO (MVar BotResumeData)
putStrLn "[Hsbot] Spawning IrcBot plugins... "
botState <- execStateT spawnIrcPlugins BotState { botStartTime = startTime
, botPlugins = M.empty
, botChan = chan
, botConfig = config
, botResumeData = mvar }
botState <- execStateT spawnPlugins BotState { botStartTime = startTime
, botPlugins = M.empty
, botChan = chan
, botConfig = config
, botResumeData = mvar }
putStrLn "[Hsbot] Entering main loop... "
(status, botState') <- runLoop botState
putStrLn "[Hsbot] Killing active plugins... "

View file

@ -21,7 +21,7 @@ data IrcConfig = IrcConfig
-- | User configuration
ircDefaultConfig :: IrcConfig
ircDefaultConfig = IrcConfig
{ ircConfigName = "localhost"
{ ircConfigName = "irc-alocalhost"
, ircConfigAddress = "localhost"
, ircConfigPort = PortNumber 6667
, ircConfigChannels = ["#hsbot"]

View file

@ -1,7 +1,7 @@
module Hsbot.Plugin
( killPlugin
, spawnIrcPlugins
, spawnIrcPlugin
, spawnPlugins
, spawnPlugin
, unloadPlugin
) where
@ -17,24 +17,26 @@ import Hsbot.Irc.Config
import Hsbot.Irc.Core
import Hsbot.Types
-- | spawns IrcPlugins
spawnIrcPlugins :: Bot ()
spawnIrcPlugins = do
-- | spawns plugins
spawnPlugins :: Bot ()
spawnPlugins = do
config <- gets botConfig
mapM_ (spawnIrcPlugin) (ircConfigs config)
mapM_ (spawnPlugin) config
-- | spawns a single irc plugin
spawnIrcPlugin :: IrcConfig -> Bot ()
spawnIrcPlugin config = do
-- | spawns a single plugin
spawnPlugin :: BotConfig -> Bot ()
spawnPlugin (IrcBotConfig ircConfig) = do
bot <- get
let chan = botChan bot
pchan <- liftIO (newChan :: IO (Chan BotMsg))
threadId <- liftIO $ forkIO (startIrcbot config chan pchan)
let plugin = PluginState { pluginName = ircConfigName config
threadId <- liftIO $ forkIO (startIrcbot ircConfig chan pchan)
let plugin = PluginState { pluginName = ircConfigName ircConfig
, pluginChan = pchan
, pluginHandles = M.empty }
plugins = botPlugins bot
put $ bot { botPlugins = M.insert (pluginName plugin) (plugin, threadId) plugins }
resumeData <- gets botResumeData
liftIO $ modifyMVar_ resumeData (\oldData -> return $ M.insert (ircConfigName ircConfig) M.empty oldData)
-- | Unloads a plugin
unloadPlugin :: String -> Bot ()

View file

@ -27,7 +27,7 @@ data BotState = BotState
{ botStartTime :: UTCTime -- the bot's uptime
, botPlugins :: M.Map String (PluginState, ThreadId) -- Loaded plugins
, botChan :: Chan BotMsg -- the bot's communication channel
, botConfig :: Config -- the bot's starting config
, botConfig :: [BotConfig] -- the bot's starting config
, botResumeData :: MVar BotResumeData -- the necessary data to resume the bot's operations on reboot
}