From 57bec4921b02e745f412e954ea4a4ec6b3c31b3b Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Sat, 29 May 2010 16:59:41 +0200 Subject: Rationalized the way bot configs are handled. --- Hsbot/Config.hs | 15 +++------------ Hsbot/Core.hs | 12 ++++++------ Hsbot/Irc/Config.hs | 2 +- Hsbot/Plugin.hs | 24 +++++++++++++----------- Hsbot/Types.hs | 2 +- 5 files changed, 24 insertions(+), 31 deletions(-) (limited to 'Hsbot') diff --git a/Hsbot/Config.hs b/Hsbot/Config.hs index 9ce62c5..121cc98 100644 --- a/Hsbot/Config.hs +++ b/Hsbot/Config.hs @@ -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 diff --git a/Hsbot/Core.hs b/Hsbot/Core.hs index fac9f8e..025e0c5 100644 --- a/Hsbot/Core.hs +++ b/Hsbot/Core.hs @@ -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... " diff --git a/Hsbot/Irc/Config.hs b/Hsbot/Irc/Config.hs index 5075c36..0d1e5a2 100644 --- a/Hsbot/Irc/Config.hs +++ b/Hsbot/Irc/Config.hs @@ -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"] diff --git a/Hsbot/Plugin.hs b/Hsbot/Plugin.hs index 10c59af..e545e8d 100644 --- a/Hsbot/Plugin.hs +++ b/Hsbot/Plugin.hs @@ -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 () diff --git a/Hsbot/Types.hs b/Hsbot/Types.hs index 49e8e6b..4052e58 100644 --- a/Hsbot/Types.hs +++ b/Hsbot/Types.hs @@ -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 } -- cgit v1.2.3