Rationalized the way bot configs are handled.
This commit is contained in:
parent
daea135424
commit
57bec4921b
5 changed files with 24 additions and 31 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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... "
|
||||
|
|
|
@ -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"]
|
||||
|
|
|
@ -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 ()
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue