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
|
module Hsbot.Config
|
||||||
( Config(..)
|
( BotConfig (..)
|
||||||
, defaultConfig
|
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Hsbot.Irc.Config (IrcConfig)
|
import Hsbot.Irc.Config
|
||||||
|
|
||||||
-- | Configuration data type
|
-- | Configuration data type
|
||||||
data Config = Config
|
data BotConfig = IrcBotConfig IrcConfig
|
||||||
{ ircConfigs :: [IrcConfig]
|
|
||||||
}
|
|
||||||
|
|
||||||
-- | User configuration
|
|
||||||
defaultConfig :: Config
|
|
||||||
defaultConfig = Config
|
|
||||||
{ ircConfigs = []
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
|
@ -17,14 +17,14 @@ import Hsbot.Plugin
|
||||||
import Hsbot.Types
|
import Hsbot.Types
|
||||||
|
|
||||||
-- | Bot's main entry point
|
-- | Bot's main entry point
|
||||||
hsbot :: Config -> IO ()
|
hsbot :: [BotConfig] -> IO ()
|
||||||
hsbot config = do
|
hsbot config = do
|
||||||
startTime <- getCurrentTime
|
startTime <- getCurrentTime
|
||||||
putStrLn "[Hsbot] Opening communication channel... "
|
putStrLn "[Hsbot] Opening communication channel... "
|
||||||
chan <- newChan :: IO (Chan BotMsg)
|
chan <- newChan :: IO (Chan BotMsg)
|
||||||
mvar <- newMVar M.empty :: IO (MVar BotResumeData)
|
mvar <- newMVar M.empty :: IO (MVar BotResumeData)
|
||||||
putStrLn "[Hsbot] Spawning IrcBot plugins... "
|
putStrLn "[Hsbot] Spawning IrcBot plugins... "
|
||||||
botState <- execStateT spawnIrcPlugins BotState { botStartTime = startTime
|
botState <- execStateT spawnPlugins BotState { botStartTime = startTime
|
||||||
, botPlugins = M.empty
|
, botPlugins = M.empty
|
||||||
, botChan = chan
|
, botChan = chan
|
||||||
, botConfig = config
|
, botConfig = config
|
||||||
|
|
|
@ -21,7 +21,7 @@ data IrcConfig = IrcConfig
|
||||||
-- | User configuration
|
-- | User configuration
|
||||||
ircDefaultConfig :: IrcConfig
|
ircDefaultConfig :: IrcConfig
|
||||||
ircDefaultConfig = IrcConfig
|
ircDefaultConfig = IrcConfig
|
||||||
{ ircConfigName = "localhost"
|
{ ircConfigName = "irc-alocalhost"
|
||||||
, ircConfigAddress = "localhost"
|
, ircConfigAddress = "localhost"
|
||||||
, ircConfigPort = PortNumber 6667
|
, ircConfigPort = PortNumber 6667
|
||||||
, ircConfigChannels = ["#hsbot"]
|
, ircConfigChannels = ["#hsbot"]
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
module Hsbot.Plugin
|
module Hsbot.Plugin
|
||||||
( killPlugin
|
( killPlugin
|
||||||
, spawnIrcPlugins
|
, spawnPlugins
|
||||||
, spawnIrcPlugin
|
, spawnPlugin
|
||||||
, unloadPlugin
|
, unloadPlugin
|
||||||
) where
|
) where
|
||||||
|
|
||||||
|
@ -17,24 +17,26 @@ import Hsbot.Irc.Config
|
||||||
import Hsbot.Irc.Core
|
import Hsbot.Irc.Core
|
||||||
import Hsbot.Types
|
import Hsbot.Types
|
||||||
|
|
||||||
-- | spawns IrcPlugins
|
-- | spawns plugins
|
||||||
spawnIrcPlugins :: Bot ()
|
spawnPlugins :: Bot ()
|
||||||
spawnIrcPlugins = do
|
spawnPlugins = do
|
||||||
config <- gets botConfig
|
config <- gets botConfig
|
||||||
mapM_ (spawnIrcPlugin) (ircConfigs config)
|
mapM_ (spawnPlugin) config
|
||||||
|
|
||||||
-- | spawns a single irc plugin
|
-- | spawns a single plugin
|
||||||
spawnIrcPlugin :: IrcConfig -> Bot ()
|
spawnPlugin :: BotConfig -> Bot ()
|
||||||
spawnIrcPlugin config = do
|
spawnPlugin (IrcBotConfig ircConfig) = do
|
||||||
bot <- get
|
bot <- get
|
||||||
let chan = botChan bot
|
let chan = botChan bot
|
||||||
pchan <- liftIO (newChan :: IO (Chan BotMsg))
|
pchan <- liftIO (newChan :: IO (Chan BotMsg))
|
||||||
threadId <- liftIO $ forkIO (startIrcbot config chan pchan)
|
threadId <- liftIO $ forkIO (startIrcbot ircConfig chan pchan)
|
||||||
let plugin = PluginState { pluginName = ircConfigName config
|
let plugin = PluginState { pluginName = ircConfigName ircConfig
|
||||||
, pluginChan = pchan
|
, pluginChan = pchan
|
||||||
, pluginHandles = M.empty }
|
, pluginHandles = M.empty }
|
||||||
plugins = botPlugins bot
|
plugins = botPlugins bot
|
||||||
put $ bot { botPlugins = M.insert (pluginName plugin) (plugin, threadId) plugins }
|
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
|
-- | Unloads a plugin
|
||||||
unloadPlugin :: String -> Bot ()
|
unloadPlugin :: String -> Bot ()
|
||||||
|
|
|
@ -27,7 +27,7 @@ data BotState = BotState
|
||||||
{ botStartTime :: UTCTime -- the bot's uptime
|
{ botStartTime :: UTCTime -- the bot's uptime
|
||||||
, botPlugins :: M.Map String (PluginState, ThreadId) -- Loaded plugins
|
, botPlugins :: M.Map String (PluginState, ThreadId) -- Loaded plugins
|
||||||
, botChan :: Chan BotMsg -- the bot's communication channel
|
, 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
|
, botResumeData :: MVar BotResumeData -- the necessary data to resume the bot's operations on reboot
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue