summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Dessaux2010-05-29 16:59:41 +0200
committerJulien Dessaux2010-05-29 17:05:56 +0200
commit57bec4921b02e745f412e954ea4a4ec6b3c31b3b (patch)
tree3cca9e3630772cd9594442ff3ff2101a80599a4d
parentImplemented the clean killing of plugins' threads. (diff)
downloadhsbot-57bec4921b02e745f412e954ea4a4ec6b3c31b3b.tar.gz
hsbot-57bec4921b02e745f412e954ea4a4ec6b3c31b3b.tar.bz2
hsbot-57bec4921b02e745f412e954ea4a4ec6b3c31b3b.zip
Rationalized the way bot configs are handled.
-rw-r--r--Hsbot/Config.hs15
-rw-r--r--Hsbot/Core.hs12
-rw-r--r--Hsbot/Irc/Config.hs2
-rw-r--r--Hsbot/Plugin.hs24
-rw-r--r--Hsbot/Types.hs2
5 files changed, 24 insertions, 31 deletions
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
}