Removed thread ids from plugins data structure.
This commit is contained in:
parent
c1662ba7b9
commit
738ad9e9bb
11 changed files with 37 additions and 46 deletions
|
@ -13,7 +13,7 @@ import System.IO()
|
||||||
|
|
||||||
import Hsbot.Config
|
import Hsbot.Config
|
||||||
import Hsbot.Irc.Config
|
import Hsbot.Irc.Config
|
||||||
import Hsbot.Irc.Core (ircbot)
|
import Hsbot.Irc.Core
|
||||||
import Hsbot.Message
|
import Hsbot.Message
|
||||||
import Hsbot.Plugin
|
import Hsbot.Plugin
|
||||||
|
|
||||||
|
@ -23,7 +23,7 @@ type Bot = StateT BotState IO
|
||||||
-- | An Hsbot state
|
-- | An Hsbot state
|
||||||
data BotState = BotState
|
data BotState = BotState
|
||||||
{ botStartTime :: UTCTime -- the bot's uptime
|
{ botStartTime :: UTCTime -- the bot's uptime
|
||||||
, botPlugins :: M.Map String PluginState -- 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 :: Config -- the bot's starting config
|
||||||
}
|
}
|
||||||
|
@ -62,8 +62,10 @@ spawnIrcPlugins = 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 (ircbot config chan pchan)
|
threadId <- liftIO $ forkIO (startIrcbot config chan pchan)
|
||||||
let plugin = PluginState (ircConfigName config) threadId pchan M.empty
|
let plugin = PluginState { pluginName = ircConfigName config
|
||||||
|
, pluginChan = pchan
|
||||||
|
, pluginHandles = M.empty }
|
||||||
plugins = botPlugins bot
|
plugins = botPlugins bot
|
||||||
put $ bot { botPlugins = M.insert (pluginName plugin) plugin plugins }
|
put $ bot { botPlugins = M.insert (pluginName plugin) (plugin, threadId) plugins }
|
||||||
|
|
||||||
|
|
|
@ -40,7 +40,7 @@ processInternalCommand (IntIrcCmd ircCmd)
|
||||||
| otherwise = do
|
| otherwise = do
|
||||||
plugins <- gets ircBotPlugins
|
plugins <- gets ircBotPlugins
|
||||||
case M.lookup (ircCmdTo ircCmd) plugins of
|
case M.lookup (ircCmdTo ircCmd) plugins of
|
||||||
Just plugin -> sendToPlugin (IntIrcCmd ircCmd) plugin
|
Just (plugin, _) -> sendToPlugin (IntIrcCmd ircCmd) plugin
|
||||||
Nothing -> return ()
|
Nothing -> return ()
|
||||||
processInternalCommand _ = return ()
|
processInternalCommand _ = return ()
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
module Hsbot.Irc.Core
|
module Hsbot.Irc.Core
|
||||||
( ircbot
|
( startIrcbot
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Control.Concurrent
|
import Control.Concurrent
|
||||||
|
@ -21,8 +21,8 @@ import Hsbot.Irc.Types
|
||||||
import Hsbot.Message (BotMsg)
|
import Hsbot.Message (BotMsg)
|
||||||
|
|
||||||
-- | IrcBot's main entry point
|
-- | IrcBot's main entry point
|
||||||
ircbot :: IrcConfig -> Chan BotMsg -> Chan BotMsg -> IO ()
|
startIrcbot :: IrcConfig -> Chan BotMsg -> Chan BotMsg -> IO ()
|
||||||
ircbot config masterChan myChan = do
|
startIrcbot config masterChan myChan = do
|
||||||
startTime <- getCurrentTime
|
startTime <- getCurrentTime
|
||||||
putStrLn "[IrcBot] Opening communication channel... "
|
putStrLn "[IrcBot] Opening communication channel... "
|
||||||
chan <- newChan :: IO (Chan IrcBotMsg)
|
chan <- newChan :: IO (Chan IrcBotMsg)
|
||||||
|
@ -55,6 +55,9 @@ ircbot config masterChan myChan = do
|
||||||
_ <- ircBotLoop ircBotState' `catch` (\(_ :: IOException) -> return ())
|
_ <- ircBotLoop ircBotState' `catch` (\(_ :: IOException) -> return ())
|
||||||
return ()
|
return ()
|
||||||
|
|
||||||
|
--resumeIrcBot
|
||||||
|
--resumeIrcBot
|
||||||
|
|
||||||
-- | Runs the IrcBot's reader loop
|
-- | Runs the IrcBot's reader loop
|
||||||
ircBotReader :: Handle -> Chan IrcBotMsg -> ThreadId -> IO ()
|
ircBotReader :: Handle -> Chan IrcBotMsg -> ThreadId -> IO ()
|
||||||
ircBotReader handle chan fatherThreadId = forever $ do
|
ircBotReader handle chan fatherThreadId = forever $ do
|
||||||
|
@ -126,9 +129,9 @@ dispatchMessage (InIrcMsg inIrcMsg) = do
|
||||||
let key = tail . head $ words getMsgContent
|
let key = tail . head $ words getMsgContent
|
||||||
pluginNames = fromMaybe [] $ M.lookup key cmds
|
pluginNames = fromMaybe [] $ M.lookup key cmds
|
||||||
plugins' = fromMaybe [] $ mapM (flip M.lookup plugins) pluginNames
|
plugins' = fromMaybe [] $ mapM (flip M.lookup plugins) pluginNames
|
||||||
in mapM_ (sendRunCommand $ tail getMsgContent) plugins'
|
in mapM_ (sendRunCommand (tail getMsgContent) . fst) plugins'
|
||||||
else
|
else
|
||||||
mapM_ (sendToPlugin $ InIrcMsg inIrcMsg) (M.elems plugins)
|
mapM_ (sendToPlugin (InIrcMsg inIrcMsg) . fst) (M.elems plugins)
|
||||||
where
|
where
|
||||||
isPluginCommand :: IrcConfig -> Bool
|
isPluginCommand :: IrcConfig -> Bool
|
||||||
isPluginCommand config =
|
isPluginCommand config =
|
||||||
|
|
|
@ -48,14 +48,13 @@ loadIrcPlugin pluginName = do
|
||||||
let oldPlugins = ircBotPlugins ircbot
|
let oldPlugins = ircBotPlugins ircbot
|
||||||
-- We check for unicity
|
-- We check for unicity
|
||||||
case M.lookup pluginName oldPlugins of
|
case M.lookup pluginName oldPlugins of
|
||||||
Just plugin -> return ()
|
Just _ -> return ()
|
||||||
Nothing -> do
|
Nothing -> do
|
||||||
threadId <- liftIO $ forkIO (entryPoint pluginChan masterChan)
|
threadId <- liftIO $ forkIO (entryPoint pluginChan masterChan)
|
||||||
let plugin = IrcPluginState { ircPluginName = pluginName
|
let plugin = IrcPluginState { ircPluginName = pluginName
|
||||||
, ircPluginThreadId = threadId
|
|
||||||
, ircPluginChan = pluginChan
|
, ircPluginChan = pluginChan
|
||||||
, ircPluginMasterChan = masterChan }
|
, ircPluginMasterChan = masterChan }
|
||||||
put $ ircbot { ircBotPlugins = M.insert pluginName plugin oldPlugins }
|
put $ ircbot { ircBotPlugins = M.insert pluginName (plugin, threadId) oldPlugins }
|
||||||
|
|
||||||
-- | Sends a list of loaded plugins
|
-- | Sends a list of loaded plugins
|
||||||
listPlugins :: IrcMsg -> String -> IrcBot ()
|
listPlugins :: IrcMsg -> String -> IrcBot ()
|
||||||
|
@ -63,7 +62,7 @@ listPlugins originalRequest dest = do
|
||||||
plugins <- gets ircBotPlugins
|
plugins <- gets ircBotPlugins
|
||||||
let listing = unwords $ M.keys plugins
|
let listing = unwords $ M.keys plugins
|
||||||
case M.lookup dest plugins of
|
case M.lookup dest plugins of
|
||||||
Just plugin -> sendToPlugin (IntIrcCmd $ IrcCmd "ANSWER" "CORE" dest listing originalRequest) plugin
|
Just (plugin, _) -> sendToPlugin (IntIrcCmd $ IrcCmd "ANSWER" "CORE" dest listing originalRequest) plugin
|
||||||
Nothing -> return ()
|
Nothing -> return ()
|
||||||
|
|
||||||
-- | Unloads a plugin
|
-- | Unloads a plugin
|
||||||
|
@ -72,9 +71,9 @@ unloadPlugin name = do
|
||||||
bot <- get
|
bot <- get
|
||||||
let oldPlugins = ircBotPlugins bot
|
let oldPlugins = ircBotPlugins bot
|
||||||
case M.lookup name oldPlugins of
|
case M.lookup name oldPlugins of
|
||||||
Just plugin -> do
|
Just (_, threadId) -> do
|
||||||
let newPlugins = M.delete name oldPlugins
|
let newPlugins = M.delete name oldPlugins
|
||||||
liftIO $ throwTo (ircPluginThreadId plugin) UserInterrupt
|
liftIO $ throwTo threadId UserInterrupt
|
||||||
put $ bot { ircBotPlugins = newPlugins }
|
put $ bot { ircBotPlugins = newPlugins }
|
||||||
Nothing -> return ()
|
Nothing -> return ()
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ module Hsbot.Irc.Plugin.Core
|
||||||
( ircBotPluginCore
|
( ircBotPluginCore
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Control.Concurrent (Chan, myThreadId)
|
import Control.Concurrent (Chan)
|
||||||
import Control.Exception
|
import Control.Exception
|
||||||
import Control.Monad.State
|
import Control.Monad.State
|
||||||
import Prelude hiding (catch)
|
import Prelude hiding (catch)
|
||||||
|
@ -13,9 +13,7 @@ import Hsbot.Irc.PluginCommons
|
||||||
-- | The plugin's main entry point
|
-- | The plugin's main entry point
|
||||||
ircBotPluginCore :: Chan IrcBotMsg -> Chan IrcBotMsg -> IO ()
|
ircBotPluginCore :: Chan IrcBotMsg -> Chan IrcBotMsg -> IO ()
|
||||||
ircBotPluginCore myChan masterChan = do
|
ircBotPluginCore myChan masterChan = do
|
||||||
threadId <- myThreadId
|
|
||||||
let plugin = IrcPluginState { ircPluginName = "Core"
|
let plugin = IrcPluginState { ircPluginName = "Core"
|
||||||
, ircPluginThreadId = threadId
|
|
||||||
, ircPluginChan = myChan
|
, ircPluginChan = myChan
|
||||||
, ircPluginMasterChan = masterChan }
|
, ircPluginMasterChan = masterChan }
|
||||||
evalStateT (mapM_ sendRegisterCommand ["list", "load", "reload", "unload"]) plugin
|
evalStateT (mapM_ sendRegisterCommand ["list", "load", "reload", "unload"]) plugin
|
||||||
|
|
|
@ -2,7 +2,6 @@ module Hsbot.Irc.Plugin.Dummy
|
||||||
( ircBotPluginDummy
|
( ircBotPluginDummy
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Control.Concurrent (myThreadId)
|
|
||||||
import Control.Concurrent.Chan
|
import Control.Concurrent.Chan
|
||||||
import Control.Exception
|
import Control.Exception
|
||||||
import Control.Monad.State
|
import Control.Monad.State
|
||||||
|
@ -14,9 +13,7 @@ import Hsbot.Irc.PluginCommons
|
||||||
-- | The plugin's main entry point
|
-- | The plugin's main entry point
|
||||||
ircBotPluginDummy :: Chan IrcBotMsg -> Chan IrcBotMsg -> IO ()
|
ircBotPluginDummy :: Chan IrcBotMsg -> Chan IrcBotMsg -> IO ()
|
||||||
ircBotPluginDummy myChan masterChan = do
|
ircBotPluginDummy myChan masterChan = do
|
||||||
threadId <- myThreadId
|
|
||||||
let plugin = IrcPluginState { ircPluginName = "Dummy"
|
let plugin = IrcPluginState { ircPluginName = "Dummy"
|
||||||
, ircPluginThreadId = threadId
|
|
||||||
, ircPluginChan = myChan
|
, ircPluginChan = myChan
|
||||||
, ircPluginMasterChan = masterChan }
|
, ircPluginMasterChan = masterChan }
|
||||||
_ <- (execStateT run plugin) `catch` (\(_ :: AsyncException) -> return plugin)
|
_ <- (execStateT run plugin) `catch` (\(_ :: AsyncException) -> return plugin)
|
||||||
|
|
|
@ -2,7 +2,6 @@ module Hsbot.Irc.Plugin.Ping
|
||||||
( ircBotPluginPing
|
( ircBotPluginPing
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Control.Concurrent (myThreadId)
|
|
||||||
import Control.Concurrent.Chan
|
import Control.Concurrent.Chan
|
||||||
import Control.Exception
|
import Control.Exception
|
||||||
import Control.Monad.State
|
import Control.Monad.State
|
||||||
|
@ -14,9 +13,7 @@ import Hsbot.Irc.PluginCommons
|
||||||
-- | The plugin's main entry point
|
-- | The plugin's main entry point
|
||||||
ircBotPluginPing :: Chan IrcBotMsg -> Chan IrcBotMsg -> IO ()
|
ircBotPluginPing :: Chan IrcBotMsg -> Chan IrcBotMsg -> IO ()
|
||||||
ircBotPluginPing myChan masterChan = do
|
ircBotPluginPing myChan masterChan = do
|
||||||
threadId <- myThreadId
|
|
||||||
let plugin = IrcPluginState { ircPluginName = "Ping"
|
let plugin = IrcPluginState { ircPluginName = "Ping"
|
||||||
, ircPluginThreadId = threadId
|
|
||||||
, ircPluginChan = myChan
|
, ircPluginChan = myChan
|
||||||
, ircPluginMasterChan = masterChan }
|
, ircPluginMasterChan = masterChan }
|
||||||
_ <- (execStateT run plugin) `catch` (\(_ :: AsyncException) -> return plugin)
|
_ <- (execStateT run plugin) `catch` (\(_ :: AsyncException) -> return plugin)
|
||||||
|
|
|
@ -2,7 +2,6 @@ module Hsbot.Irc.Plugin.Quote
|
||||||
( ircBotPluginQuote
|
( ircBotPluginQuote
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Control.Concurrent (myThreadId)
|
|
||||||
import Control.Concurrent.Chan
|
import Control.Concurrent.Chan
|
||||||
import Control.Exception
|
import Control.Exception
|
||||||
import Control.Monad.State
|
import Control.Monad.State
|
||||||
|
@ -61,9 +60,7 @@ ircBotPluginQuote myChan masterChan = do
|
||||||
txtQuoteBot <- TIO.readFile $ dbfile
|
txtQuoteBot <- TIO.readFile $ dbfile
|
||||||
let quoteBot = read (T.unpack txtQuoteBot) :: QuoteBotState
|
let quoteBot = read (T.unpack txtQuoteBot) :: QuoteBotState
|
||||||
-- The plugin main loop
|
-- The plugin main loop
|
||||||
threadId <- myThreadId
|
|
||||||
let plugin = IrcPluginState { ircPluginName = "Quote"
|
let plugin = IrcPluginState { ircPluginName = "Quote"
|
||||||
, ircPluginThreadId = threadId
|
|
||||||
, ircPluginChan = myChan
|
, ircPluginChan = myChan
|
||||||
, ircPluginMasterChan = masterChan }
|
, ircPluginMasterChan = masterChan }
|
||||||
evalStateT (mapM_ sendRegisterCommand ["quote"]) plugin
|
evalStateT (mapM_ sendRegisterCommand ["quote"]) plugin
|
||||||
|
|
|
@ -23,7 +23,6 @@ type IrcPlugin = StateT IrcPluginState IO
|
||||||
-- | A plugin state
|
-- | A plugin state
|
||||||
data IrcPluginState = IrcPluginState
|
data IrcPluginState = IrcPluginState
|
||||||
{ ircPluginName :: String -- The plugin's name
|
{ ircPluginName :: String -- The plugin's name
|
||||||
, ircPluginThreadId :: ThreadId -- The plugin thread
|
|
||||||
, ircPluginChan :: Chan IrcBotMsg -- The plugin chan
|
, ircPluginChan :: Chan IrcBotMsg -- The plugin chan
|
||||||
, ircPluginMasterChan :: Chan IrcBotMsg -- The master's chan
|
, ircPluginMasterChan :: Chan IrcBotMsg -- The master's chan
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,7 +22,7 @@ type IrcBot = StateT IrcBotState IO
|
||||||
-- | An Ircbot state
|
-- | An Ircbot state
|
||||||
data IrcBotState = IrcBotState
|
data IrcBotState = IrcBotState
|
||||||
{ ircBotStartTime :: UTCTime -- the bot's uptime
|
{ ircBotStartTime :: UTCTime -- the bot's uptime
|
||||||
, ircBotPlugins :: M.Map String IrcPluginState -- Loaded plugins
|
, ircBotPlugins :: M.Map String (IrcPluginState, ThreadId) -- Loaded plugins
|
||||||
, ircBotCommands :: M.Map String [String] -- Loaded plugins
|
, ircBotCommands :: M.Map String [String] -- Loaded plugins
|
||||||
, ircBotChan :: Chan IrcBotMsg -- The IrcBot's communication channel
|
, ircBotChan :: Chan IrcBotMsg -- The IrcBot's communication channel
|
||||||
, ircBotMasterChan :: Chan BotMsg -- The Hsbot communication channel
|
, ircBotMasterChan :: Chan BotMsg -- The Hsbot communication channel
|
||||||
|
|
|
@ -17,7 +17,6 @@ type Plugin = StateT PluginState IO
|
||||||
-- | A plugin state
|
-- | A plugin state
|
||||||
data PluginState = PluginState
|
data PluginState = PluginState
|
||||||
{ pluginName :: String -- The plugin's name
|
{ pluginName :: String -- The plugin's name
|
||||||
, pluginThreadId :: ThreadId -- The plugin thread
|
|
||||||
, pluginChan :: Chan BotMsg -- The plugin chan
|
, pluginChan :: Chan BotMsg -- The plugin chan
|
||||||
, pluginHandles :: M.Map String Handle -- the plugins's handles
|
, pluginHandles :: M.Map String Handle -- the plugins's handles
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue